Repository navigation
Expand file tree
/
Copy pathBMP2GIF.PAS
More file actions
372 lines (338 loc) · 9.77 KB
/
Copy pathBMP2GIF.PAS
File metadata and controls
372 lines (338 loc) · 9.77 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
{ @author: Sylvain Maltais (support@gladir.com)
@created: 2024
@website(https://www.gladir.com/corail)
@abstract(Target: Turbo Pascal 7, Free Pascal 3.2)
}
Program BMP2GIF;
{$A-}
Uses DOS;
Const
{Type de compression Windows}
bi_RGB=0;
bi_RLE8=1;
bi_RLE4=2;
{Param�tres LZW pour le format GIF}
HashSize=5003;
MaxLZWBits=12;
MaxLZWCode=4096; { 1 shl MaxLZWBits }
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
SourceBMP,DestGIF:File;
Header:HeaderBMP;
BIH:BitMapInfoHeader;
BytesPerLine,ByteReaded,ByteWritten:Word;
StartPos:LongInt;
NumPal:Word;
Palette:Array[0..255]of RGB32;
Buffer:Array[0..4095]of Byte;
PixelLine:Array[0..4095]of Byte;
OutputName:String;
{Etat de l'encodeur LZW}
HashKey:Array[0..HashSize-1]of LongInt;
HashCode:Array[0..HashSize-1]of Word;
MinCodeSize:Byte;
ClearCode,EOICode:Word;
CurCodeSize:Byte;
NextCode:Word;
PrefixCode:LongInt;
BitBuffer:LongInt;
BitsInBuffer:Byte;
SubBlock:Array[0..254]of Byte;
SubBlockLen:Byte;
Function Path2NameExt(Const Path:String):String;
Var
D:DirStr;
N:NameStr;
E:ExtStr;
Begin
FSplit(Path,D,N,E);
Path2NameExt:=D+N+'.GIF';
End;
Procedure WriteByteToFile(B:Byte);
Begin
BlockWrite(DestGIF,B,1,ByteWritten);
End;
Procedure WriteWordToFile(W:Word);
Var
Lo,Hi:Byte;
Begin
Lo:=Byte(W and $FF);
Hi:=Byte((W shr 8) and $FF);
BlockWrite(DestGIF,Lo,1,ByteWritten);
BlockWrite(DestGIF,Hi,1,ByteWritten);
End;
Procedure FlushSubBlock;
Begin
If SubBlockLen>0 Then Begin
BlockWrite(DestGIF,SubBlockLen,1,ByteWritten);
BlockWrite(DestGIF,SubBlock,SubBlockLen,ByteWritten);
SubBlockLen:=0;
End;
End;
Procedure EmitOutputByte(B:Byte);
Begin
SubBlock[SubBlockLen]:=B;
Inc(SubBlockLen);
If SubBlockLen=255Then FlushSubBlock;
End;
Procedure EmitCode(Code:Word);
Begin
BitBuffer:=BitBuffer or(LongInt(Code)shl BitsInBuffer);
Inc(BitsInBuffer,CurCodeSize);
While BitsInBuffer>=8Do Begin
EmitOutputByte(Byte(BitBuffer and $FF));
BitBuffer:=BitBuffer shr 8;
Dec(BitsInBuffer,8);
End;
End;
Procedure FlushBits;
Begin
If BitsInBuffer>0Then Begin
EmitOutputByte(Byte(BitBuffer and $FF));
BitBuffer:=0;
BitsInBuffer:=0;
End;
End;
Procedure ResetHash;
Var
I:Word;
Begin
For I:=0 to HashSize-1 Do HashKey[I]:=-1;
End;
{ Recherche d'une entr�e (Prefix, Suffix) dans la table de hachage.
Retourne True si trouv� (Idx pointe sur l'entr�e existante).
Retourne False sinon (Idx pointe sur l'emplacement libre o� ins�rer). }
Procedure HashFind(Prefix:Word;Suffix:Byte;Var Idx:Word;Var Found:Boolean);
Var
Key:LongInt;
H:Word;
Begin
Key:=(LongInt(Prefix)shl 8)or Suffix;
H:=Word(((Word(Suffix)shl 5)xor Prefix)mod HashSize);
While HashKey[H]<>-1Do Begin
If HashKey[H]=Key Then Begin
Idx:=H;
Found:=True;
Exit;
End;
Inc(H);
If H>=HashSize Then H:=0;
End;
Idx:=H;
Found:=False;
End;
Procedure ResetLZWTable;
Begin
ResetHash;
CurCodeSize:=MinCodeSize+1;
NextCode:=EOICode+1;
End;
Var
I,X:Word;
Width,Height:Word;
Idx:Word;
Found:Boolean;
K:Byte;
GCTBits:Byte;
GCTSize:Word;
PackedField:Byte;
ZeroByte:Byte;
GIFHeader:String[6];
BEGIN
If(ParamStr(1)='/?')or(ParamStr(1)='--help')or(ParamStr(1)='-h')Then Begin
WriteLn('BMP2GIF : Cette commande permet de convertir une image BitMap (.BMP) ',
'en format GIF (Graphics Interchange Format).');
WriteLn;
WriteLn('Syntaxe : BMP2GIF nomdufichier.BMP');
WriteLn;
WriteLn(' nomdufichier Ce param�tre permet d''indiquer le nom du fichier ".BMP".');
WriteLn;
WriteLn('Le fichier r�sultant porte le m�me nom avec l''extension .GIF.');
WriteLn('Seuls les BitMaps 1, 4 et 8 bits par pixel non compress�s sont support�s.');
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;
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;
If(BIH.biBitCount<>1)and(BIH.biBitCount<>4)and(BIH.biBitCount<>8)Then Begin
WriteLn('Seuls les BitMaps 1, 4 ou 8 bits par pixel sont support�s pour la conversion en GIF !');
Halt(2);
End;
If BIH.biCompression<>bi_RGB Then Begin
WriteLn('Compression RLE4 ou RL8 non support�');
Halt(3);
End;
Case(BIH.biBitCount)of
8:Begin NumPal:=256; GCTBits:=7; End; { 2^(7+1)=256 }
4:Begin NumPal:=16; GCTBits:=3; End; { 2^(3+1)=16 }
1:Begin NumPal:=2; GCTBits:=0; End; { 2^(0+1)=2 }
Else Begin NumPal:=0; GCTBits:=0; End;
End;
GCTSize:=Word(1)shl(GCTBits+1);
Seek(SourceBMP,14+SizeOf(BitmapInfoHeader));
BlockRead(SourceBMP,Palette,LongInt(NumPal)*4,ByteReaded);
StartPos:=Header.OffBits;
Width:=Word(Header.NumXPixels);
Height:=Word(Header.NumYPixels);
{Cr�ation du fichier GIF de destination}
OutputName:=Path2NameExt(ParamStr(1));
{$I-}Assign(DestGIF,OutputName);
Rewrite(DestGIF,1);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible de cr�er le fichier ',OutputName);
Close(SourceBMP);
Halt;
End;
{Ent�te GIF89a}
GIFHeader:='GIF89a';
BlockWrite(DestGIF,GIFHeader[1],6,ByteWritten);
{Logical Screen Descriptor}
WriteWordToFile(Width);
WriteWordToFile(Height);
PackedField:=$80 or(GCTBits shl 4)or GCTBits;
WriteByteToFile(PackedField); { GCT pr�sent, profondeur, taille }
WriteByteToFile(0); { Index de couleur d'arri�re-plan }
WriteByteToFile(0); { Ratio d'aspect des pixels }
{Global Color Table}
For I:=0 to GCTSize-1 Do Begin
If I<NumPal Then Begin
WriteByteToFile(Palette[I].R);
WriteByteToFile(Palette[I].G);
WriteByteToFile(Palette[I].B);
End
Else
Begin
WriteByteToFile(0);
WriteByteToFile(0);
WriteByteToFile(0);
End;
End;
{Image Descriptor}
WriteByteToFile($2C);
WriteWordToFile(0); { Position gauche }
WriteWordToFile(0); { Position haute }
WriteWordToFile(Width);
WriteWordToFile(Height);
WriteByteToFile(0); { Pas de table de couleurs locale, pas d'entrelacement }
{Donn�es image LZW}
If BIH.biBitCount<2 Then MinCodeSize:=2 { Le format GIF exige MinCodeSize >= 2 }
Else MinCodeSize:=BIH.biBitCount;
WriteByteToFile(MinCodeSize);
ClearCode:=Word(1)shl MinCodeSize;
EOICode:=ClearCode+1;
BitBuffer:=0;
BitsInBuffer:=0;
SubBlockLen:=0;
ResetLZWTable;
EmitCode(ClearCode);
PrefixCode:=-1;
For I:=0 to Height-1 Do Begin
Seek(SourceBMP,StartPos+LongInt(Height-1-I)*LongInt(BytesPerLine));
BlockRead(SourceBMP,Buffer,BytesPerLine,ByteReaded);
Case BIH.biBitCount of
8:Begin
For X:=0 to Width-1 Do PixelLine[X]:=Buffer[X];
End;
4:Begin
For X:=0 to Width-1 Do
If(X and 1)=0 Then PixelLine[X]:=(Buffer[X shr 1]shr 4)and $0F
Else PixelLine[X]:=Buffer[X shr 1]and $0F;
End;
1:Begin
For X:=0 to Width-1 Do
PixelLine[X]:=(Buffer[X shr 3]shr(7-(X and 7)))and 1;
End;
End;
For X:=0 to Width-1 Do Begin
K:=PixelLine[X];
If PrefixCode<0 Then PrefixCode:=K
Else
Begin
HashFind(Word(PrefixCode),K,Idx,Found);
If Found Then PrefixCode:=HashCode[Idx]
Else
Begin
EmitCode(Word(PrefixCode));
If NextCode<MaxLZWCode Then Begin
HashKey[Idx]:=(LongInt(PrefixCode)shl 8)or K;
HashCode[Idx]:=NextCode;
Inc(NextCode);
{Augmente la taille de code lorsque le compteur d�passe la
capacit� actuelle. La comparaison est strictement sup�rieure
car le d�codeur GIF ajoute son entr�e une �tape plus tard que
l'encodeur ; un test '=' provoquerait une d�synchronisation
des largeurs de code entre encodeur et d�codeur.}
If(NextCode>(Word(1)shl CurCodeSize))and(CurCodeSize<MaxLZWBits)Then
Inc(CurCodeSize);
End
Else
Begin
EmitCode(ClearCode);
ResetLZWTable;
End;
PrefixCode:=K;
End;
End;
End;
End;
If PrefixCode>=0 Then EmitCode(Word(PrefixCode));
EmitCode(EOICode);
FlushBits;
FlushSubBlock;
ZeroByte:=0;
WriteByteToFile(ZeroByte); { Terminateur de bloc de donn�es }
WriteByteToFile($3B); { Fin du fichier GIF }
Close(DestGIF);
Close(SourceBMP);
End
Else
WriteLn('Param�tre requis !');
END.