-
Notifications
You must be signed in to change notification settings - Fork 12
/
bgrareadgif.pas
416 lines (374 loc) · 11.4 KB
/
bgrareadgif.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
{ ***************************************************************************
* *
* This file is part of BGRABitmap library which is distributed under the *
* modified LGPL. *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
************************* BGRABitmap library ******************************
- Drawing routines with transparency and antialiasing with Lazarus.
Offers also various transforms.
- These routines allow to manipulate 32bit images in BGRA format or RGBA
format (depending on the platform).
- This code is under modified LGPL (see COPYING.modifiedLGPL.txt).
This means that you can link this library inside your programs for any purpose.
Only the included part of the code must remain LGPL.
- If you make some improvements to this library, please notify here:
http://www.lazarus.freepascal.org/index.php/topic,12037.0.html
********************* Contact : Circular at operamail.com *******************
******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | [email protected]
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
//This Unit provides some optimisations of TFPReaderGif: decompression algorithm and direct pixel access of TBGRABitmap.
//Note: to read an animation use TBGRAAnimatedGif instead. }
unit BGRAReadGif;
{$i bgrabitmap.inc}{$H+}
{.$POINTERMATH ON}
interface
uses
Classes, SysUtils, BGRATypes,{$IFNDEF FPC}Types, GraphType, BGRAGraphics,{$ENDIF} FPImage, FPReadGif;
type
PGifRGB = ^TGifRGB;
{ TBGRAReaderGif }
TBGRAReaderGif = class(TFPReaderGif)
protected
procedure ReadPaletteAtOnce(Stream: TStream; Size: integer);
procedure InternalRead(Stream: TStream; Img: TFPCustomImage); override;
function ReadScanLine(Stream: TStream): boolean; override;
function WriteScanLineBGRA(Img: TFPCustomImage): Boolean; virtual;
end;
implementation
uses BGRABitmapTypes;
{ TBGRAReaderGif }
procedure TBGRAReaderGif.ReadPaletteAtOnce(Stream: TStream; Size: integer);
Var
RGBEntries, RGBEntry : PGifRGB;
I : Integer;
c : TFPColor;
begin
FPalette.count := 0;
getmem(RGBEntries, sizeof(TGifRGB)*Size);
Stream.Read(RGBEntries^, sizeof(TGifRGB)*Size);
For I:=0 To Size-1 Do
Begin
RGBEntry := RGBEntries+I;
With c do
begin
Red:=RGBEntry^.Red or (RGBEntry^.Red shl 8);
Green:=RGBEntry^.Green or (RGBEntry^.Green shl 8);
Blue:=RGBEntry^.Blue or (RGBEntry^.Blue shl 8);
Alpha:=alphaOpaque;
end;
FPalette.Add(C);
End;
FreeMem(RGBEntries);
end;
procedure TBGRAReaderGif.InternalRead(Stream: TStream; Img: TFPCustomImage);
var
Introducer:byte;
ColorTableSize :Integer;
ContProgress: Boolean;
begin
FPalette:=nil;
FScanLine:=nil;
try
ContProgress:=true;
Progress(psStarting, 0, False, Rect(0,0,0,0), '', ContProgress);
if not ContProgress then exit;
FPalette := TFPPalette.Create(0);
Stream.Position:=0;
// header
Stream.Read(FHeader,SizeOf(FHeader));
Progress(psRunning, 0, False, Rect(0,0,0,0), '', ContProgress);
if not ContProgress then exit;
// Endian Fix Mantis 8541. Gif is always little endian
{$IFDEF ENDIAN_BIG}
with FHeader do
begin
ScreenWidth := {$IFNDEF BDS}LEtoN{$ENDIF}(ScreenWidth);
ScreenHeight := {$IFNDEF BDS}LEtoN{$ENDIF}(ScreenHeight);
end;
{$ENDIF}
// global palette
if (FHeader.Packedbit and $80) <> 0 then
begin
ColorTableSize := FHeader.Packedbit and 7 + 1;
ReadPaletteAtOnce(stream, 1 shl ColorTableSize);
end;
// skip extensions
Repeat
Introducer:=SkipBlock(Stream);
until (Introducer = $2C) or (Introducer = $3B);
// descriptor
Stream.Read(FDescriptor, SizeOf(FDescriptor));
{$IFDEF ENDIAN_BIG}
with FDescriptor do
begin
Left := {$IFNDEF BDS}LEtoN{$ENDIF}(Left);
Top := {$IFNDEF BDS}LEtoN{$ENDIF}(Top);
Width := {$IFNDEF BDS}LEtoN{$ENDIF}(Width);
Height := {$IFNDEF BDS}LEtoN{$ENDIF}(Height);
end;
{$ENDIF}
// local palette
if (FDescriptor.Packedbit and $80) <> 0 then
begin
ColorTableSize := FDescriptor.Packedbit and 7 + 1;
ReadPaletteAtOnce(stream, 1 shl ColorTableSize);
end;
// parse header
if not AnalyzeHeader then exit;
// create image
if Assigned(OnCreateImage) then
OnCreateImage(Self,Img);
Img.SetSize(FWidth,FHeight);
// read pixels
if not ReadScanLine(Stream) then exit;
if Img is TBGRACustomBitmap then
begin
if not WriteScanLineBGRA(Img) then exit;
end else
if not WriteScanLine(Img) then exit;
// ToDo: read further images
finally
FreeAndNil(FPalette);
ReAllocMem(FScanLine,0);
end;
Progress(FPimage.psEnding, 100, false, Rect(0,0,FWidth,FHeight), '', ContProgress);
end;
function TBGRAReaderGif.ReadScanLine(Stream: TStream): Boolean;
var
OldPos,
UnpackedSize,
PackedSize:BGRALongInt;
I: Integer;
Data,
Bits,
Code: BGRACardinal;
SourcePtr: PByte;
InCode: BGRACardinal;
CodeSize: BGRACardinal;
CodeMask: BGRACardinal;
FreeCode: BGRACardinal;
OldCode: BGRACardinal;
Prefix: array[0..4095] of BGRACardinal;
Suffix,
Stack: array [0..4095] of Byte;
StackPointer, StackTop: PByte;
StackSize: integer;
DataComp,
Target: PByte;
{%H-}B,
{%H-}FInitialCodeSize,
FirstChar: Byte;
ClearCode,
EOICode: BGRAWord;
ContProgress: Boolean;
begin
DataComp:=nil;
ContProgress:=true;
try
// read dictionary size
Stream.read({%H-}FInitialCodeSize, 1);
// search end of compressor table
OldPos:=Stream.Position;
PackedSize := 0;
Repeat
Stream.read({%H-}B, 1);
if B > 0 then
begin
inc(PackedSize, B);
Stream.Seek(B, soFromCurrent);
end;
until B = 0;
Progress(psRunning, trunc(100.0 * (Stream.position / Stream.size)),
False, Rect(0,0,0,0), '', ContProgress);
if not ContProgress then exit(false);
Getmem(DataComp, PackedSize);
// read compressor table
SourcePtr:=DataComp;
Stream.Position:=OldPos;
Repeat
Stream.read(B, 1);
if B > 0 then
begin
Stream.ReadBuffer(SourcePtr^, B);
Inc(SourcePtr,B);
end;
until B = 0;
Progress(psRunning, trunc(100.0 * (Stream.position / Stream.size)),
False, Rect(0,0,0,0), '', ContProgress);
if not ContProgress then exit(false);
SourcePtr:=DataComp;
Target := FScanLine;
CodeSize := FInitialCodeSize + 1;
ClearCode := 1 shl FInitialCodeSize;
EOICode := ClearCode + 1;
FreeCode := ClearCode + 2;
OldCode := 4096;
CodeMask := (1 shl CodeSize) - 1;
UnpackedSize:=FWidth * FHeight;
for I := 0 to ClearCode - 1 do
begin
Prefix[I] := 4096;
Suffix[I] := I;
end;
StackTop := @Stack[high(Stack)];
StackPointer := StackTop;
FirstChar := 0;
Data := 0;
Bits := 0;
// LZW decompression gif
while (UnpackedSize > 0) and (PackedSize > 0) do
begin
Inc(Data, SourcePtr^ shl Bits);
Inc(Bits, 8);
while Bits >= CodeSize do
begin
Code := Data and CodeMask;
Data := Data shr CodeSize;
Dec(Bits, CodeSize);
if Code = EOICode then Break;
if Code = ClearCode then
begin
CodeSize := FInitialCodeSize + 1;
CodeMask := (1 shl CodeSize) - 1;
FreeCode := ClearCode + 2;
OldCode := 4096;
Continue;
end;
if Code > FreeCode then Break;
if OldCode = 4096 then
begin
FirstChar := Suffix[Code];
Target^ := FirstChar;
Inc(Target);
Dec(UnpackedSize);
OldCode := Code;
Continue;
end;
InCode := Code;
if Code = FreeCode then
begin
StackPointer^ := FirstChar;
dec(StackPointer);
Code := OldCode;
end;
while Code > ClearCode do
begin
StackPointer^ := Suffix[Code];
dec(StackPointer);
Code := Prefix[Code];
end;
FirstChar := Suffix[Code];
StackPointer^ := FirstChar;
dec(StackPointer);
Prefix[FreeCode] := OldCode;
Suffix[FreeCode] := FirstChar;
if (FreeCode = CodeMask) and
(CodeSize < 12) then
begin
Inc(CodeSize);
CodeMask := (1 shl CodeSize) - 1;
end;
if FreeCode < 4095 then Inc(FreeCode);
OldCode := InCode;
StackSize := StackTop-StackPointer;
if StackSize > 0 then
begin
Move((StackPointer+1)^, Target^, StackSize);
inc(Target, StackSize);
StackPointer:= StackTop;
dec(UnpackedSize, StackSize);
end;
end;
Inc(SourcePtr);
Dec(PackedSize);
end;
Progress(psRunning, trunc(100.0 * (Stream.position / Stream.size)),
False, Rect(0,0,0,0), '', ContProgress);
if not ContProgress then exit(false);
finally
if DataComp<>nil then
FreeMem(DataComp);
end;
Result:=true;
end;
function TBGRAReaderGif.WriteScanLineBGRA(Img: TFPCustomImage): Boolean;
Var
Row, Col,i : Integer;
Pass, Every : byte;
P : PByte;
PBGRAPalette: PBGRAPixel;
PDest: PBGRAPixel;
function IsMultiple(NumberA, NumberB: Integer): Boolean;
begin
Result := (NumberA >= NumberB) and
(NumberB > 0) and
(NumberA mod NumberB = 0);
end;
begin
Result:=false;
P:=FScanLine;
getmem(PBGRAPalette, (FPalette.Count)*sizeof(TBGRAPixel));
for i := 0 to FPalette.Count-1 do PBGRAPalette[i] := FPColorToBGRA(FPalette.Color[i]);
If FInterlace then
begin
For Pass := 1 to 4 do
begin
Case Pass of
1 : begin
Row := 0;
Every := 8;
end;
2 : begin
Row := 4;
Every := 8;
end;
3 : begin
Row := 2;
Every := 4;
end;
else{4}
begin
Row := 1;
Every := 2;
end;
end;
Repeat
PDest := TBGRACustomBitmap(Img).ScanLine[Row];
for Col:=Img.Width-1 downto 0 do
begin
PDest^ := PBGRAPalette[P^];
Inc(P);
Inc(PDest);
end;
Inc(Row, Every);
until Row >= Img.Height;
end;
end
else
begin
for Row:=0 to Img.Height-1 do
begin
PDest := TBGRACustomBitmap(Img).ScanLine[Row];
for Col:=Img.Width-1 downto 0 do
begin
PDest^ := PBGRAPalette[P^];
Inc(P);
Inc(PDest);
end;
end;
end;
FreeMem(PBGRAPalette);
Result:=true;
end;
initialization
DefaultBGRAImageReader[ifGif] := TBGRAReaderGif;
end.