summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/fcl-image/src/fpreadgif.pas
blob: 0b867896201fffac40e9df681320e381ca8b838e (plain)
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 2008 by the Free Pascal development team

    GIF reader for fpImage.

    See the file COPYING.FPC, 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.

 **********************************************************************

  ToDo: read further images
}
unit FPReadGif;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FPimage;

type
  TGifRGB = packed record
    Red, Green, Blue : Byte;
  end;

  TGIFHeader = packed record
    Signature:array[0..2] of Char;    //* Header Signature (always "GIF") */
    Version:array[0..2] of Char;      //* GIF format version("87a" or "89a") */
    // Logical Screen Descriptor
    ScreenWidth:word;                 //* Width of Display Screen in Pixels */
    ScreenHeight:word;                //* Height of Display Screen in Pixels */
    Packedbit,                        //* Screen and Color Map Information */
    BackgroundColor,                  //* Background Color Index */
    AspectRatio:byte;                 //* Pixel Aspect Ratio */
  end;

  TGifImageDescriptor = packed record
    Left,              //* X position of image on the display */
    Top,               //* Y position of image on the display */
    Width,             //* Width of the image in pixels */
    Height:word;       //* Height of the image in pixels */
    Packedbit:byte;    //* Image and Color Table Data Information */
  end;

  TGifGraphicsControlExtension = packed record
    BlockSize,         //* Size of remaining fields (always 04h) */
    Packedbit:byte;    //* Method of graphics disposal to use */
    DelayTime:word;    //* Hundredths of seconds to wait	*/
    ColorIndex,        //* Transparent Color Index */
    Terminator:byte;   //* Block Terminator (always 0) */
  end;

  TFPReaderGif = class;

  TGifCreateCompatibleImgEvent = procedure(Sender: TFPReaderGif;
                                        var NewImage: TFPCustomImage) of object;

  { TFPReaderGif }

  TFPReaderGif = class(TFPCustomImageReader)
  protected
    FHeader: TGIFHeader;
    FDescriptor: TGifImageDescriptor;
    FGraphicsCtrlExt: TGifGraphicsControlExtension;
    FTransparent: Boolean;
    FGraphCtrlExt: Boolean;
    FScanLine: PByte;
    FLineSize: Integer;
    FPalette: TFPPalette;
    FWidth: integer;
    FHeight: Integer;
    FInterlace: boolean;
    FBitsPerPixel: byte;
    FBackground: byte;
    FResolution: byte;
    FOnCreateImage: TGifCreateCompatibleImgEvent;
    procedure ReadPalette(Stream: TStream; Size: integer);
    function AnalyzeHeader: Boolean;
    procedure InternalRead(Stream: TStream; Img: TFPCustomImage); override;
    function ReadScanLine(Stream: TStream): boolean; virtual;
    function WriteScanLine(Img: TFPCustomImage): Boolean; virtual;
    function InternalCheck (Stream: TStream) : boolean; override;
    function SkipBlock(Stream: TStream): byte;
  public
    constructor Create; override;
    destructor Destroy; override;
    property Header: TGIFHeader read FHeader;
    property Descriptor: TGifImageDescriptor read FDescriptor;
    property GraphCtrlExt: Boolean read FGraphCtrlExt;
    property GraphicsCtrlExt: TGifGraphicsControlExtension read FGraphicsCtrlExt;
    property Transparent: Boolean read FTransparent;
    property Palette: TFPPalette read FPalette;
    property Width: integer read FWidth;
    property Height: Integer read FHeight;
    property Interlace: boolean read FInterlace;
    property BitsPerPixel: byte read FBitsPerPixel;
    property Background: byte read FBackground;
    property Resolution: byte read FResolution;
    property OnCreateImage: TGifCreateCompatibleImgEvent read FOnCreateImage write FOnCreateImage;
  end;

implementation

{ TFPReaderGif }

procedure TFPReaderGif.ReadPalette(Stream: TStream; Size: integer);
Var
  RGBEntry : TGifRGB;
  I : Integer;
  c : TFPColor;
begin
  FPalette.count := 0;
  For I:=0 To Size-1 Do
  Begin
    Stream.Read(RGBEntry, SizeOf(RGBEntry));
    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;
end;

function TFPReaderGif.AnalyzeHeader: Boolean;
var
    C : TFPColor;
begin
  Result:=false;
  With FHeader do
  begin
    if (Signature = 'GIF') and
       ((Version = '87a') or
       (Version = '89a')) then
    else
    Raise Exception.Create('Unknown/Unsupported GIF image type');

    FResolution := Packedbit and $70 shr 5 + 1;
    FBitsPerPixel:=Packedbit and 7 + 1;
    FBackground := BackgroundColor;

    With FDescriptor do
    begin
      fWidth:=Width;
      fHeight:=Height;
      FInterlace := (Packedbit and $40 = $40);
    end;
    FTransparent:= FBackground <> 0;
    if FGraphCtrlExt then
    begin
      FTransparent:=(FGraphicsCtrlExt.Packedbit and $01)<>0;
      If FTransparent then
        FBackground:=FGraphicsCtrlExt.ColorIndex;
    end;
    FLineSize:=FWidth*(FHeight+1);
    GetMem(FScanLine,FLineSize);
    If FTransparent then
    begin
      C:=FPalette.Color[FBackground];
      C.alpha:=alphaTransparent;
      FPalette.Color[FBackground]:=C;
    end;
  end;
  Result:=true;
end;

procedure TFPReaderGif.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, trunc(100.0 * (Stream.position / Stream.size)), 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 := LEtoN(ScreenWidth);
          ScreenHeight := LEtoN(ScreenHeight);
        end; 
    {$ENDIF}
    // global palette
    if (FHeader.Packedbit and $80) <> 0 then
    begin
      ColorTableSize := FHeader.Packedbit and 7 + 1;
      ReadPalette(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 := LEtoN(Left);
          Top := LEtoN(Top);
          Width := LEtoN(Width);
          Height := LEtoN(Height);
        end;
    {$ENDIF}
    // local palette
    if (FDescriptor.Packedbit and $80) <> 0 then
    begin
      ColorTableSize := FDescriptor.Packedbit and 7 + 1;
      ReadPalette(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 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 TFPReaderGif.ReadScanLine(Stream: TStream): Boolean;
var
  OldPos,
  UnpackedSize,
  PackedSize:longint;
  I: Integer;
  Data,
  Bits,
  Code: Cardinal;
  SourcePtr: PByte;
  InCode: Cardinal;

  CodeSize: Cardinal;
  CodeMask: Cardinal;
  FreeCode: Cardinal;
  OldCode: Cardinal;
  Prefix: array[0..4095] of Cardinal;
  Suffix,
  Stack: array [0..4095] of Byte;
  StackPointer: PByte;
  DataComp,
  Target: PByte;
  B,
  FInitialCodeSize,
  FirstChar: Byte;
  ClearCode,
  EOICode: Word;
  ContProgress: Boolean;

begin
  DataComp:=nil;
  ContProgress:=true;
  try
    // read dictionary size
    Stream.read(FInitialCodeSize, 1);

    // search end of compressor table
    OldPos:=Stream.Position;
    PackedSize := 0;
    Repeat
      Stream.read(B, 1);
      if B > 0 then
      begin
        inc(PackedSize, B);
        Stream.Seek(B, soFromCurrent);
        CodeMask := (1 shl CodeSize) - 1;
      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;
    StackPointer := @Stack;
    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;
          Inc(StackPointer);
          Code := OldCode;
        end;
        while Code > ClearCode do
        begin
          StackPointer^ := Suffix[Code];
          Inc(StackPointer);
          Code := Prefix[Code];
        end;
        FirstChar := Suffix[Code];
        StackPointer^ := FirstChar;
        Inc(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;
        repeat
          Dec(StackPointer);
          Target^ := StackPointer^;
          Inc(Target);
          Dec(UnpackedSize);
        until StackPointer = @Stack;
      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 TFPReaderGif.WriteScanLine(Img: TFPCustomImage): Boolean;
Var
  Row, Col : Integer;
  Pass, Every : byte;
  P : PByte;
  function IsMultiple(NumberA, NumberB: Integer): Boolean;
  begin
    Result := (NumberA >= NumberB) and
              (NumberB > 0) and
              (NumberA mod NumberB = 0);
  end;
begin
  Result:=false;
  P:=FScanLine;
  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;
         4 : begin
               Row := 1;
               Every := 2;
             end;
        end;
      Repeat
        for Col:=0 to Img.Width-1 do
        begin
          Img.Colors[Col,Row]:=FPalette[P^];
          Inc(P);
        end;
        Inc(Row, Every);
      until Row >= Img.Height;
    end;
  end
  else
  begin
    for Row:=0 to Img.Height-1 do
      for Col:=0 to Img.Width-1 do
      begin
        Img.Colors[Col,Row]:=FPalette[P^];
        Inc(P);
      end;
  end;
  Result:=true;
end;

function TFPReaderGif.InternalCheck(Stream: TStream): boolean;
var
  OldPos: Int64;
begin
  try
    OldPos:=Stream.Position;
    Stream.Read(FHeader,SizeOf(FHeader));
    Result:=(FHeader.Signature = 'GIF') and
            ((FHeader.Version = '87a') or (FHeader.Version = '89a'));
    Stream.Position:=OldPos;
  except
    Result:=False;
  end;
end;

function TFPReaderGif.SkipBlock(Stream: TStream): byte;
var
  Introducer,
  Labels,
  SkipByte : byte;
begin
  Stream.read(Introducer,1);
  if Introducer = $21 then
  begin
     Stream.read(Labels,1);
     Case Labels of
       $FE, $FF :     // Comment Extension block or Application Extension block
            while true do
            begin
              Stream.Read(SkipByte, 1);
              if SkipByte = 0 then Break;
              Stream.Seek(SkipByte, soFromCurrent);
            end;
       $F9 :         // Graphics Control Extension block
            begin
              Stream.Read(FGraphicsCtrlExt, SizeOf(FGraphicsCtrlExt));
              FGraphCtrlExt:=True;
            end;
       $01 :        // Plain Text Extension block
            begin
              Stream.Read(SkipByte, 1);
              Stream.Seek(SkipByte, soFromCurrent);
              while true do
              begin
                Stream.Read(SkipByte, 1);
                if SkipByte = 0 then Break;
                Stream.Seek(SkipByte, soFromCurrent);
              end;
            end;
      end;
  end;
  Result:=Introducer;
end;

constructor TFPReaderGif.Create;
begin
  inherited Create;

end;

destructor TFPReaderGif.Destroy;
begin

  inherited Destroy;
end;

initialization
  ImageHandlers.RegisterImageReader ('GIF Graphics', 'gif', TFPReaderGif);
end.