-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBMP2PAS.PAS
262 lines (247 loc) · 6.17 KB
/
BMP2PAS.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
{ @author: Sylvain Maltais ([email protected])
@created: 2023
@website(https://www.gladir.com/corail)
@abstract(Target: Turbo Pascal 7, Free Pascal 3.2)
}
Program BMP2PAS;
{$A-}
Uses DOS;
Const
{Type de compression Windows}
bi_RGB=0;
bi_RLE8=1;
bi_RLE4=2;
Type
RGB32=Record
B,G,R,FreeByte:Byte;
End;
HeaderBMP=Record
Sign:Array[0..1]of Char;
Size,Reserved0,OffBits:LongInt;
biSize,NumXPixels,NumYPixels:LongInt;
Planes,BitCount:Word;
Compression,SizeImage:LongInt;
XPelsPerMeter,YPelsPerMeter,ClrUsed,ClrImportant:LongInt;
End;
BitmapInfoHeader=Record
biSize:LongInt; { Sp‚cifie le nombre d'octets requis pour la structure }
biWidth:LongInt; { Sp‚cifie la largeur du BitMap en pixels }
biHeight:LongInt; { Sp‚cifie la hauteur du BitMap en pixels }
biPlanes:Word; { Sp‚cifie le nombre de plane pour la destination }
biBitCount:Word; { Sp‚cifie le nombre de bits par pixel (1, 4, 8, 24)}
biCompression:LongInt; { Sp‚cifie le style de compression: BI_RGB, BI_RLE8, BI_RLE4 }
biSizeImage:LongInt; { Sp‚cifie la taille en octets pour l'image }
biXPelsPerMeter:LongInt;{ Sp‚cifie le nombre horizontal de pixels par mStre }
biYPelsPerMeter:LongInt;{ Sp‚cifie le nombre vertical de pixels par mStre }
biClrUsed:LongInt; { Sp‚cifie le nombre de couleurs index‚s dans la table}
biClrImportant:LongInt; { Sp‚cifie le nombre de couleurs index‚s dans la table
en comptant ceux ‚tant vraiment indispensable … l'affichage}
End;
Var
First:Boolean;
CountByLine:Word;
SourceBMP:File;
Header:HeaderBMP;
Size,I,J,K,Min,Max,BytesPerLine,ByteReaded:Word;
Value,CountValue:Integer;
R,G,B:Byte;
BIH:BitMapInfoHeader;
PixelX,PixelY:Word;
StartPos:LongInt;
NumPal:Word;
ImageName:String;
Palette:Array[0..255]of RGB32;
Buffer:Array[0..4095]of Byte;
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;
Function RGBTo16Color(R,G,B:Byte):Integer;
Var
Kr:Byte;
Begin
Kr:=0;
If(R)and$80=$80Then Kr:=Kr or$04;
If(R)and$40=$40Then Kr:=Kr or$20;
If(G)and$80=$80Then Kr:=Kr or$02;
If(G)and$40=$40Then Kr:=Kr or$10;
If(B)and$80=$80Then Kr:=Kr or$01;
If(B)and$40=$40Then Kr:=Kr or$08;
RGBTo16Color:=Kr and$F;
End;
Procedure AddPixel;
Var
RawValue:Byte;
Begin
Case(Header.BitCount)of
4:Begin
If NumPal=0 Then Begin
If(First)Then First:=False
Else
Begin
Write(',');
Inc(CountByLine);
If CountByLine>12 Then Begin
CountByLine:=0;
WriteLn;
Write(' ');
End;
End;
Write('$',ByteHex2Str(Value));
End
Else
Begin
If(PixelX<Header.NumXPixels)Then Begin
If(First)Then First:=False
Else
Begin
Write(',');
Inc(CountByLine);
If CountByLine>12 Then Begin
CountByLine:=0;
WriteLn;
Write(' ');
End;
End;
Write('$',ByteHex2Str(Value));
End;
End;
Inc(PixelX,2);
End;
8:Begin
If(First)Then First:=False
Else
Begin
Write(',');
Inc(CountByLine);
If CountByLine>12 Then Begin
CountByLine:=0;
WriteLn;
Write(' ');
End;
End;
Write('$',ByteHex2Str(Value));
Inc(PixelX);
End;
24:Begin
Case CountValue of
0:R:=Value;
1:G:=Value;
2:Begin
B:=Value;
If(First)Then First:=False
Else
Begin
Write(',');
Inc(CountByLine,3);
If CountByLine>12 Then Begin
CountByLine:=0;
WriteLn;
Write(' ');
End;
End;
Write('$',ByteHex2Str(R),',','$',ByteHex2Str(G),',','$',ByteHex2Str(B));
Inc(PixelX)
End;
End;
CountValue:=(CountValue+1)mod 3;
End;
End;
End;
BEGIN
If(ParamStr(1)='/?')or(ParamStr(1)='--help')or(ParamStr(1)='-h')Then Begin
WriteLn('BMP2PAS : Cette commande permet de convertir une image BitMap (.BMP) ',
'en code source Pascal.');
WriteLn;
WriteLn('Syntaxe : BMP2PAS nomdufichier.BMP');
WriteLn;
WriteLn(' nomdufichier Ce paramŠtre permet d''indiquer le nom du fichier ".BMP".');
End
Else
If ParamCount>0Then Begin
{$I-}Assign(SourceBMP,ParamStr(1));
Reset(SourceBMP,1);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible de lire le fichier d''image');
Halt;
End;
ImageName:=Path2Name(ParamStr(1));
BlockRead(SourceBMP,Header,SizeOf(Header),ByteReaded);
If Header.Sign<>'BM'Then Begin
WriteLn('Signature d''entˆte Bitmap introuvable !');
Halt;
End;
Seek(SourceBMP,14);
BlockRead(SourceBMP,BIH,SizeOf(BIH),ByteReaded);
If BIH.biSizeImage=0Then Begin
WriteLn('Taille de l''image = 0 !');
Halt;
End
Else
Begin
BytesPerLine:=Word(LongInt(LongInt(BIH.biSizeImage) div LongInt(BIH.biHeight)));
If BytesPerLine>SizeOf(Buffer)Then Begin
WriteLn('Largeur de l''image trop grande !');
Halt;
End;
End;
PixelX:=0;
PixelY:=0;
CountValue:=0;
NumPal:=0;
Case(BIH.biBitCount)of
8:NumPal:=256;
4:NumPal:=16;
Else NumPal:=0;
End;
If NumPal>0Then Begin
Seek(SourceBMP,14+SizeOf(BitmapInfoHeader));
BlockRead(SourceBMP,Palette,SizeOf(Palette),ByteReaded);
End;
StartPos:=Header.OffBits;
First:=True;
If BIH.biCompression in[bi_RGB]Then Begin
CountByLine:=0;
WriteLn('Const');
WriteLn(' NumXPixels=',Header.NumXPixels,';');
WriteLn(' NumYPixels=',Header.NumYPixels,';');
WriteLn(' BitsPerPixel=',BIH.biBitCount,';');
WriteLn(' BytesPerLine=',BytesPerLine,';');
WriteLn(' BitmapData:Array[0..',
Pred(BytesPerLine*Header.NumYPixels),
']of Byte=(');
Write(' ');
For I:=Header.NumYPixels-1 downto 0 do Begin
Seek(SourceBMP,StartPos+I*BytesPerLine);
BlockRead(SourceBMP,Buffer,BytesPerLine,ByteReaded);
PixelX:=0;
For J:=0 to BytesPerLine do Begin
Value:=Buffer[J];
AddPixel;
End;
Inc(PixelY);
End;
WriteLn;
WriteLn(');');
End
Else
Begin
WriteLn('Compression RLE4 ou RL8 non support‚');
Halt(3);
End;
Close(SourceBMP);
End
Else
WriteLn('ParamŠtre requis !');
END.