-
Notifications
You must be signed in to change notification settings - Fork 0
/
PPM2C.PAS
118 lines (109 loc) · 2.91 KB
/
PPM2C.PAS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
{ @author: Sylvain Maltais (support@gladir.com)
@created: 2023
@website(https://www.gladir.com/c-tools)
@abstract(Target: Turbo Pascal 7, Free Pascal 3.2)
}
Program PPM2C;
Uses DOS;
Type
RGB=Record
R,G,B:Byte;
End;
Var
SourcePPM:File;
SourceFileName,CurrWord,ImageName:String;
ImageWidth,ImageHeight,ByteReaded:Word;
Value,BytesPerLine,BitsPerPixel,LineCount,StepCount,CountByLine:Integer;
Err:Word;
CurrChar:Char;
First:Boolean;
RGBBuffer:RGB;
Function ByteHex2Str(value:Byte):String;
Const
matrix:Array[0..15]of Char = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
Begin
ByteHex2Str:=matrix[(value shr 4) and $0F]+matrix[value and $F];
End;
Function Path2Name(Const Path:String):String;
Var
D:DirStr;
N:NameStr;
E:ExtStr;
Begin
FSplit(Path,D,N,E);
Path2Name:=N;
End;
BEGIN
If(ParamStr(1)='/?')or(ParamStr(1)='--help')or(ParamStr(1)='-h')Then Begin
WriteLn('PPM2C : Cette commande permet de convertir une image ',
'de format PPM (Portable PixMap) en code source C.');
WriteLn;
WriteLn('Syntaxe : PPM2C nomdufichier.PPM');
WriteLn;
WriteLn(' nomdufichier Ce paramŠtre permet d''indiquer le nom du fichier PPM.');
End
Else
If ParamCount>0Then Begin
SourceFileName:=ParamStr(1);
ImageName:=Path2Name(SourceFileName);
ImageWidth:=0;
ImageHeight:=0;
{$I-}Assign(SourcePPM,SourceFileName);
Reset(SourcePPM,1);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible d''ouvrir le fichier PPM : ',SourceFileName);
Halt;
End;
LineCount:=0;StepCount:=0;
CurrWord:='';
While Not(EOF(SourcePPM))do Begin
BlockRead(SourcePPM,CurrChar,1,ByteReaded);
If CurrChar in[#10,' ']Then Begin
Inc(StepCount);
Case(StepCount)of
1:If Not((CurrWord='P5')or(CurrWord='P6'))Then Begin
WriteLn('Seul les formats P5 et P6 de PPM sont reconnu !');
Halt;
End;
2:Val(CurrWord,ImageWidth,Err);
3:Val(CurrWord,IMageHeight,Err);
4:Val(CurrWord,Value,Err);
End;
CurrWord:='';
End
Else
CurrWord:=CurrWord+CurrChar;
If CurrChar=#10 Then Inc(LineCount);
If LineCount>=3 Then Break;
End;
BytesPerLine:=ImageWidth*3;
WriteLn('#define NumXPixels ',ImageWidth);
WriteLn('#define NumYPixels ',ImageHeight);
WriteLn('#define BitsPerPixel 24');
WriteLn('#define BytesPerLine ',BytesPerLine);
WriteLn('const unsigned char BitmapData[',
BytesPerLine*ImageHeight,
'] = {');
First:=True;
CountByLine:=0;
While Not(EOF(SourcePPM))do Begin
If Not(First)Then Write(',');
If CountByLine>3 Then Begin
WriteLn;
CountByLine:=0;
End
Else
Inc(CountByLine);
BlockRead(SourcePPM,RGBBuffer,SizeOf(RGB),ByteReaded);
Write('0x',ByteHex2Str(RGBBuffer.R),',');
Write('0x',ByteHex2Str(RGBBuffer.G),',');
Write('0x',ByteHex2Str(RGBBuffer.B));
First:=False;
End;
WriteLn;
WriteLn('};');
Close(SourcePPM);
End
Else
WriteLn('ParamŠtre attendu !');
END.