File: gifread.c

package info (click to toggle)
xwrits 2.19-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 564 kB
  • ctags: 641
  • sloc: ansic: 5,242; sh: 330; makefile: 93
file content (902 lines) | stat: -rw-r--r-- 22,241 bytes parent folder | download | duplicates (4)
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/* gifread.c - Functions to read GIFs.
   Copyright (C) 1997-9 Eddie Kohler, eddietwo@lcs.mit.edu
   This file is part of the GIF library.

   The GIF library is free software*. It is distributed under the GNU General
   Public License, version 2 or later; you can copy, distribute, or alter it
   at will, as long as this notice is kept intact and this source code is made
   available. There is no warranty, express or implied.

   *There is a patent on the LZW compression method used by GIFs, and included
   in gifwrite.c. Unisys, the patent holder, allows the compression algorithm
   to be used without a license in software distributed at no cost to the
   user. The decompression algorithm is not patented. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#elif !defined(__cplusplus)
/* Assume we don't have inline by default */
# define inline
#endif
#include "gif.h"
#include <stdarg.h>
#include <assert.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
  
  Gif_Stream *stream;
  
  Gif_Code *prefix;
  byte *suffix;
  uint16_t *length;
  
  uint16_t width;
  uint16_t height;
  
  byte *image;
  byte *maximage;
  
  unsigned decodepos;

  Gif_ReadErrorHandler handler;
  void *handler_thunk;
  
} Gif_Context;


typedef struct Gif_Reader {
  
  FILE *f;
  const byte *v;
  uint32_t w;
  uint32_t length;
  int is_record;
  int is_eoi;
  byte (*byte_getter)(struct Gif_Reader *);
  void (*block_getter)(byte *, uint32_t, struct Gif_Reader *);
  uint32_t (*offseter)(struct Gif_Reader *);
  int (*eofer)(struct Gif_Reader *);
  
} Gif_Reader;


#define gifgetc(grr)	((char)(*grr->byte_getter)(grr))
#define gifgetbyte(grr) ((*grr->byte_getter)(grr))
#define gifgetblock(ptr, size, grr) ((*grr->block_getter)(ptr, size, grr))
#define gifgetoffset(grr) ((*grr->offseter)(grr))
#define gifeof(grr)	((*grr->eofer)(grr))

static inline uint16_t
gifgetunsigned(Gif_Reader *grr)
{
  byte one = gifgetbyte(grr);
  byte two = gifgetbyte(grr);
  return one | (two << 8);
}


static byte
file_byte_getter(Gif_Reader *grr)
{
  int i = getc(grr->f);
  return i == EOF ? 0 : (byte)i;
}

static void
file_block_getter(byte *p, uint32_t s, Gif_Reader *grr)
{
  fread(p, 1, s, grr->f);
}

static uint32_t
file_offseter(Gif_Reader *grr)
{
  return ftell(grr->f);
}

static int
file_eofer(Gif_Reader *grr)
{
  return feof(grr->f);
}


static byte
record_byte_getter(Gif_Reader *grr)
{
  return grr->w ? (grr->w--, *grr->v++) : 0;
}

static void
record_block_getter(byte *p, uint32_t s, Gif_Reader *grr)
{
  if (s > grr->w) s = grr->w;
  memcpy(p, grr->v, s);
  grr->w -= s, grr->v += s;
}

static uint32_t
record_offseter(Gif_Reader *grr)
{
  return grr->length - grr->w;
}

static int
record_eofer(Gif_Reader *grr)
{
  return grr->w == 0;
}


static void
make_data_reader(Gif_Reader *grr, const byte *data, uint32_t length)
{
  grr->v = data;
  grr->length = length;
  grr->w = length;
  grr->is_record = 1;
  grr->byte_getter = record_byte_getter;
  grr->block_getter = record_block_getter;
  grr->offseter = record_offseter;
  grr->eofer = record_eofer;
}


static void
gif_read_error(Gif_Context *gfc, const char *error)
{
  gfc->stream->errors++;
  if (gfc->handler)
    gfc->handler(error, gfc->stream->nimages, gfc->handler_thunk);
}


static byte
one_code(Gif_Context *gfc, Gif_Code code)
{
  byte *suffixes = gfc->suffix;
  Gif_Code *prefixes = gfc->prefix;
  byte *ptr;
  int lastsuffix;
  uint16_t codelength = gfc->length[code];

  gfc->decodepos += codelength;
  ptr = gfc->image + gfc->decodepos;
  if (ptr > gfc->maximage || !codelength) {
    gif_read_error(gfc, (!codelength ? "bad code" : "too much image data"));
    /* 5/26/98 It's not good enough simply to count an error, because in the
       read_image_data function, if code == next_code, we will store a byte in
       gfc->image[gfc->decodepos-1]. Thus, fix decodepos so it's w/in the
       image. */
    gfc->decodepos = gfc->maximage - gfc->image;
    return 0;
  }
  
  /* codelength will always be greater than 0. */
  do {
    lastsuffix = suffixes[code];
    *--ptr = lastsuffix;
    code = prefixes[code];
  } while (--codelength > 0);
  
  /* return the first pixel in the code, which, since we walked backwards
     through the code, was the last suffix we processed. */
  return lastsuffix;
}

static int
read_image_block(Gif_Reader *grr, byte *buffer, int *bit_pos_store,
		 int *bit_len_store, int bits_needed)
{
  int bit_position = *bit_pos_store;
  int bit_length = *bit_len_store;
  byte block_len;
  
  while (bit_position + bits_needed > bit_length) {
    /* Read in the next data block. */
    if (bit_position >= 8) {
      /* Need to shift down the upper, unused part of `buffer' */
      int i = bit_position / 8;
      buffer[0] = buffer[i];
      buffer[1] = buffer[i+1];
      bit_position -= i * 8;
      bit_length -= i * 8;
    }
    block_len = gifgetbyte(grr);
    GIF_DEBUG(("\nimage_block(%d)", block_len));
    if (block_len == 0) return 0;
    gifgetblock(buffer + bit_length / 8, block_len, grr);
    bit_length += block_len * 8;
  }
  
  *bit_pos_store = bit_position;
  *bit_len_store = bit_length;
  return 1;
}


static void
read_image_data(Gif_Context *gfc, Gif_Reader *grr)
{
  /* we need a bit more than GIF_MAX_BLOCK in case a single code is split
     across blocks */
  byte buffer[GIF_MAX_BLOCK + 5];
  int i;
  uint32_t accum;
  
  int bit_position;
  int bit_length;
  
  Gif_Code code;
  Gif_Code old_code;
  Gif_Code clear_code;
  Gif_Code eoi_code;
  Gif_Code next_code;
#define CUR_BUMP_CODE (1 << bits_needed)
#define CUR_CODE_MASK ((1 << bits_needed) - 1)
  
  int min_code_size;
  int bits_needed;
  
  gfc->decodepos = 0;
  
  min_code_size = gifgetbyte(grr);
  GIF_DEBUG(("\n\nmin_code_size(%d)", min_code_size));
  if (min_code_size >= GIF_MAX_CODE_BITS) {
    gif_read_error(gfc, "min_code_size too big");
    min_code_size = GIF_MAX_CODE_BITS - 1;
  } else if (min_code_size < 2) {
    gif_read_error(gfc, "min_code_size too small");
    min_code_size = 2;
  }
  clear_code = 1 << min_code_size;
  for (code = 0; code < clear_code; code++) {
    gfc->prefix[code] = 49428;
    gfc->suffix[code] = (byte)code;
    gfc->length[code] = 1;
  }
  eoi_code = clear_code + 1;
  
  next_code = eoi_code;
  bits_needed = min_code_size + 1;
  
  code = clear_code;
  
  bit_length = bit_position = 0;
  /* Thus the `Read in the next data block.' code below will be invoked on the
     first time through: exactly right! */
  
  while (1) {
    
    old_code = code;
    
    /* GET A CODE INTO THE `code' VARIABLE.
     * 
     * 9.Dec.1998 - Rather than maintain a byte pointer and a bit offset into
     * the current byte (and the processing associated with that), we maintain
     * one number: the offset, in bits, from the beginning of `buffer'. This
     * much cleaner choice was inspired by Patrick J. Naughton
     * <naughton@wind.sun.com>'s GIF-reading code, which does the same thing.
     * His code distributed as part of XV in xvgif.c. */
    
    if (bit_position + bits_needed > bit_length)
      /* Read in the next data block. */
      if (!read_image_block(grr, buffer, &bit_position, &bit_length,
			    bits_needed))
	goto zero_length_block;
    
    i = bit_position / 8;
    accum = buffer[i] + (buffer[i+1] << 8);
    if (bits_needed >= 8)
      accum |= (buffer[i+2]) << 16;
    code = (Gif_Code)((accum >> (bit_position % 8)) & CUR_CODE_MASK);
    bit_position += bits_needed;
    
    GIF_DEBUG(("%d", code));
    
    /* CHECK FOR SPECIAL OR BAD CODES: clear_code, eoi_code, or a code that is
     * too large. */
    if (code == clear_code) {
      bits_needed = min_code_size + 1;
      next_code = eoi_code;
      continue;
      
    } else if (code == eoi_code)
      break;
    
    else if (code > next_code && next_code) {
      /* code > next_code: a (hopefully recoverable) error.
	 
	 Bug fix, 5/27: Do this even if old_code == clear_code, and set code
	 to 0 to prevent errors later. (If we didn't zero code, we'd later set
	 old_code = code; then we had old_code >= next_code; so the prefixes
	 array got all screwed up!) */
      gif_read_error(gfc, "unexpected code");
      code = 0;
    }
    
    /* PROCESS THE CURRENT CODE and define the next code. If no meaningful
     * next code should be defined, then we have set next_code to either
     * `eoi_code' or `clear_code' -- so we'll store useless prefix/suffix data
     * in a useless place. */
    
    /* *First,* set up the prefix and length for the next code
       (in case code == next_code). */
    gfc->prefix[next_code] = old_code;
    gfc->length[next_code] = gfc->length[old_code] + 1;
    
    /* Use one_code to process code. It's nice that it returns the first
       pixel in code: that's what we need. */
    gfc->suffix[next_code] = one_code(gfc, code);
    
    /* Special processing if code == next_code: we didn't know code's final
       suffix when we called one_code, but we do now. */
    if (code == next_code)
      gfc->image[gfc->decodepos - 1] = gfc->suffix[next_code];
    
    /* Increment next_code except for the `clear_code' special case (that's
       when we're reading at the end of a GIF) */
    if (next_code != clear_code) {
      next_code++;
      if (next_code == CUR_BUMP_CODE) {
	if (bits_needed < GIF_MAX_CODE_BITS)
	  bits_needed++;
	else
	  next_code = clear_code;
      }
    }
    
  }
  
  /* read blocks until zero-length reached. */
  i = gifgetbyte(grr);
  GIF_DEBUG(("\nafter_image(%d)\n", i));
  while (i > 0) {
    gifgetblock(buffer, i, grr);
    i = gifgetbyte(grr);
    GIF_DEBUG(("\nafter_image(%d)\n", i));
  }
  
  /* zero-length block reached. */
 zero_length_block:
  
  if (gfc->image + gfc->decodepos < gfc->maximage)
    gif_read_error(gfc, "not enough image data for image size");
  else if (gfc->image + gfc->decodepos > gfc->maximage)
    gif_read_error(gfc, "too much image data for image size");
}


static Gif_Colormap *
read_color_table(int size, Gif_Reader *grr)
{
  Gif_Colormap *gfcm = Gif_NewFullColormap(size, size);
  Gif_Color *c;
  if (!gfcm) return 0;
  
  GIF_DEBUG(("colormap(%d)", size));
  for (c = gfcm->col; size; size--, c++) {
    c->red = gifgetbyte(grr);
    c->green = gifgetbyte(grr);
    c->blue = gifgetbyte(grr);
    c->haspixel = 0;
  }
  
  return gfcm;
}


static int
read_logical_screen_descriptor(Gif_Stream *gfs, Gif_Reader *grr)
     /* returns 0 on memory error */
{
  byte packed;
  
  /* we don't care about logical screen width or height */
  gfs->screen_width = gifgetunsigned(grr);
  gfs->screen_height = gifgetunsigned(grr);
  
  packed = gifgetbyte(grr);
  gfs->background = gifgetbyte(grr);
  
  /* don't care about pixel aspect ratio */
  gifgetbyte(grr);
  
  if (packed & 0x80) { /* have a global color table */
    int ncol = 1 << ((packed & 0x07) + 1);
    gfs->global = read_color_table(ncol, grr);
    if (!gfs->global) return 0;
    gfs->global->refcount = 1;
  }
  
  return 1;
}


static int
read_compressed_image(Gif_Image *gfi, Gif_Reader *grr, int read_flags)
{
  if (grr->is_record) {
    const byte *first = grr->v;
    uint32_t pos;
    
    /* scan over image */
    pos = 1;			/* skip min code size */
    while (pos < grr->w) {
      int amt = grr->v[pos];
      pos += amt + 1;
      if (amt == 0) break;
    }
    if (pos > grr->w) pos = grr->w;
    
    gfi->compressed_len = pos;
    if (read_flags & GIF_READ_CONST_RECORD) {
      gfi->compressed = (byte *)first;
      gfi->free_compressed = 0;
    } else {
      gfi->compressed = Gif_NewArray(byte, gfi->compressed_len);
      gfi->free_compressed = Gif_DeleteArrayFunc;
      if (!gfi->compressed) return 0;
      memcpy(gfi->compressed, first, gfi->compressed_len);
    }
    
    /* move reader over that image */
    grr->v += pos;
    grr->w -= pos;
    
  } else {
    /* non-record; have to read it block by block. */
    uint32_t comp_cap = 1024;
    uint32_t comp_len;
    byte *comp = Gif_NewArray(byte, comp_cap);
    int i;
    if (!comp) return 0;
    
    /* min code size */
    i = gifgetbyte(grr);
    comp[0] = i;
    comp_len = 1;
    
    i = gifgetbyte(grr);
    while (i > 0) {
      /* add 2 before check so we don't have to check after loop when appending
	 0 block */
      if (comp_len + i + 2 > comp_cap) {
	comp_cap *= 2;
	Gif_ReArray(comp, byte, comp_cap);
	if (!comp) return 0;
      }
      comp[comp_len] = i;
      gifgetblock(comp + comp_len + 1, i, grr);
      comp_len += i + 1;
      i = gifgetbyte(grr);
    }
    comp[comp_len++] = 0;
    
    gfi->compressed = comp;
    gfi->compressed_len = comp_len;
    gfi->free_compressed = Gif_DeleteArrayFunc;
  }
  
  return 1;
}


static int
uncompress_image(Gif_Context *gfc, Gif_Image *gfi, Gif_Reader *grr)
{
  if (!Gif_CreateUncompressedImage(gfi)) return 0;
  gfc->width = gfi->width;
  gfc->height = gfi->height;
  gfc->image = gfi->image_data;
  gfc->maximage = gfi->image_data + gfi->width * gfi->height;
  read_image_data(gfc, grr);
  return 1;
}


int
Gif_FullUncompressImage(Gif_Image *gfi, Gif_ReadErrorHandler h, void *hthunk)
{
  Gif_Context gfc;
  Gif_Stream fake_gfs;
  Gif_Reader grr;
  int ok = 0;
  
  /* return right away if image is already uncompressed. this might screw over
     people who expect re-uncompressing to restore the compressed version. */
  if (gfi->img)
    return 1;
  if (gfi->image_data)
    /* we have uncompressed data, but not an `img' array;
       this shouldn't happen */
    return 0;
  
  fake_gfs.errors = 0;
  gfc.stream = &fake_gfs;
  gfc.prefix = Gif_NewArray(Gif_Code, GIF_MAX_CODE);
  gfc.suffix = Gif_NewArray(byte, GIF_MAX_CODE);
  gfc.length = Gif_NewArray(uint16_t, GIF_MAX_CODE);
  gfc.handler = h;
  gfc.handler_thunk = hthunk;
  
  if (gfi && gfc.prefix && gfc.suffix && gfc.length && gfi->compressed) {
    make_data_reader(&grr, gfi->compressed, gfi->compressed_len);
    ok = uncompress_image(&gfc, gfi, &grr);
  }
  
  Gif_DeleteArray(gfc.prefix);
  Gif_DeleteArray(gfc.suffix);
  Gif_DeleteArray(gfc.length);
  return ok && !fake_gfs.errors;
}


static int
read_image(Gif_Reader *grr, Gif_Context *gfc, Gif_Image *gfi, int read_flags)
     /* returns 0 on memory error */
{
  byte packed;
  
  gfi->left = gifgetunsigned(grr);
  gfi->top = gifgetunsigned(grr);
  gfi->width = gifgetunsigned(grr);
  gfi->height = gifgetunsigned(grr);
  packed = gifgetbyte(grr);
  GIF_DEBUG(("<%ux%u>", gfi->width, gfi->height));
  
  if (packed & 0x80) { /* have a local color table */
    int ncol = 1 << ((packed & 0x07) + 1);
    gfi->local = read_color_table(ncol, grr);
    if (!gfi->local) return 0;
    gfi->local->refcount = 1;
  }
  
  gfi->interlace = (packed & 0x40) != 0;
  
  /* Keep the compressed data if asked */
  if (read_flags & GIF_READ_COMPRESSED) {
    if (!read_compressed_image(gfi, grr, read_flags))
      return 0;
    if (read_flags & GIF_READ_UNCOMPRESSED) {
      Gif_Reader new_grr;
      make_data_reader(&new_grr, gfi->compressed, gfi->compressed_len);
      if (!uncompress_image(gfc, gfi, &new_grr))
	return 0;
    }
    
  } else if (read_flags & GIF_READ_UNCOMPRESSED) {
    if (!uncompress_image(gfc, gfi, grr))
      return 0;
    
  } else {
    /* skip over the image */
    byte buffer[GIF_MAX_BLOCK];
    int i = gifgetbyte(grr);
    while (i > 0) {
      gifgetblock(buffer, i, grr);
      i = gifgetbyte(grr);
    }
  }
  
  return 1;
}


static void
read_graphic_control_extension(Gif_Context *gfc, Gif_Image *gfi,
			       Gif_Reader *grr)
{
  byte len;
  byte crap[GIF_MAX_BLOCK];
  
  len = gifgetbyte(grr);
  
  if (len == 4) {
    byte packed = gifgetbyte(grr);
    gfi->disposal = (packed >> 2) & 0x07;
    gfi->delay = gifgetunsigned(grr);
    gfi->transparent = gifgetbyte(grr);
    if (!(packed & 0x01)) /* transparent color doesn't exist */
      gfi->transparent = -1;
    len -= 4;
  }
  
  if (len > 0) {
    gif_read_error(gfc, "odd graphic extension format");
    gifgetblock(crap, len, grr);
  }
  
  len = gifgetbyte(grr);
  while (len > 0) {
    gif_read_error(gfc, "odd graphic extension format");
    gifgetblock(crap, len, grr);
    len = gifgetbyte(grr);
  }
}


static char *last_name;


static char *
suck_data(char *data, int *store_len, Gif_Reader *grr)
{
  byte len = gifgetbyte(grr);
  int total_len = 0;
  
  while (len > 0) {
    Gif_ReArray(data, char, total_len + len + 1);
    if (!data) return 0;
    gifgetblock((byte *)data, len, grr);
    
    total_len += len;
    data[total_len] = 0;
    
    len = gifgetbyte(grr);
  }
  
  if (store_len) *store_len = total_len;
  return data;
}


static int
read_unknown_extension(Gif_Stream *gfs, int kind, char *app_name, int position,
		       Gif_Reader *grr)
{
  byte block_len = gifgetbyte(grr);
  byte *data = 0;
  byte data_len = 0;
  Gif_Extension *gfex = 0;

  while (block_len > 0) {
    if (data) Gif_ReArray(data, byte, data_len + block_len + 1);
    else data = Gif_NewArray(byte, block_len + 1);
    if (!data) goto done;
    gifgetblock(data + data_len, block_len, grr);
    data_len += block_len;
    block_len = gifgetbyte(grr);
  }

  if (data)
    gfex = Gif_NewExtension(kind, app_name);
  if (gfex) {
    gfex->data = data;
    gfex->length = data_len;
    data[data_len] = 0;
    Gif_AddExtension(gfs, gfex, position);
  }
  
 done:
  if (!gfex) Gif_DeleteArray(data);
  while (block_len > 0) {
    byte buffer[GIF_MAX_BLOCK];
    gifgetblock(buffer, block_len, grr);
    block_len = gifgetbyte(grr);
  }
  return gfex != 0;
}


static int
read_application_extension(Gif_Context *gfc, int position, Gif_Reader *grr)
{
  Gif_Stream *gfs = gfc->stream;
  byte buffer[GIF_MAX_BLOCK + 1];
  byte len = gifgetbyte(grr);
  gifgetblock(buffer, len, grr);
  
  /* Read the Netscape loop extension. */
  if (len == 11 && memcmp(buffer, "NETSCAPE2.0", 11) == 0) {
    
    len = gifgetbyte(grr);
    if (len == 3) {
      gifgetbyte(grr); /* throw away the 1 */
      gfs->loopcount = gifgetunsigned(grr);
      len = gifgetbyte(grr);
      if (len) gif_read_error(gfc, "bad loop extension");
    } else
      gif_read_error(gfc, "bad loop extension");
    
    while (len > 0) {
      gifgetblock(buffer, len, grr);
      len = gifgetbyte(grr);
    }
    return 1;
    
  } else {
    buffer[len] = 0;
    return read_unknown_extension(gfs, 0xFF, (char *)buffer, position, grr);
  }
}


static int
read_comment_extension(Gif_Image *gfi, Gif_Reader *grr)
{
  int len;
  Gif_Comment *gfcom = gfi->comment;
  char *m = suck_data(0, &len, grr);
  if (m) {
    if (!gfcom)
      gfcom = gfi->comment = Gif_NewComment();
    if (!gfcom || !Gif_AddCommentTake(gfcom, m, len))
      return 0;
  }
  return 1;
}


static Gif_Stream *
read_gif(Gif_Reader *grr, int read_flags,
	 Gif_ReadErrorHandler handler, void *handler_thunk)
{
  Gif_Stream *gfs;
  Gif_Image *gfi;
  Gif_Image *new_gfi;
  Gif_Context gfc;
  int extension_position = 0;
  int unknown_block_type = 0;
  
  if (gifgetc(grr) != 'G' ||
      gifgetc(grr) != 'I' ||
      gifgetc(grr) != 'F')
    return 0;
  (void)gifgetc(grr);
  (void)gifgetc(grr);
  (void)gifgetc(grr);
  
  gfs = Gif_NewStream();
  gfi = Gif_NewImage();
  
  gfc.stream = gfs;
  gfc.prefix = Gif_NewArray(Gif_Code, GIF_MAX_CODE);
  gfc.suffix = Gif_NewArray(byte, GIF_MAX_CODE);
  gfc.length = Gif_NewArray(uint16_t, GIF_MAX_CODE);
  gfc.handler = handler;
  gfc.handler_thunk = handler_thunk;
  
  if (!gfs || !gfi || !gfc.prefix || !gfc.suffix || !gfc.length)
    goto done;
  
  GIF_DEBUG(("\nGIF"));
  if (!read_logical_screen_descriptor(gfs, grr))
    goto done;
  GIF_DEBUG(("logscrdesc"));
  
  while (!gifeof(grr)) {
    
    byte block = gifgetbyte(grr);
    
    switch (block) {
      
     case ',': /* image block */
      GIF_DEBUG(("imageread %d", gfs->nimages));
      
      gfi->identifier = last_name;
      last_name = 0;
      if (!read_image(grr, &gfc, gfi, read_flags)
	  || !Gif_AddImage(gfs, gfi)) {
	Gif_DeleteImage(gfi);
	goto done;
      }
      
      new_gfi = Gif_NewImage();
      if (!new_gfi) goto done;
      
      gfi = new_gfi;
      extension_position++;
      break;
      
     case ';': /* terminator */
      GIF_DEBUG(("term\n"));
      goto done;
      
     case '!': /* extension */
      block = gifgetbyte(grr);
      GIF_DEBUG(("ext(0x%02X)", block));
      switch (block) {
	
       case 0xF9:
	read_graphic_control_extension(&gfc, gfi, grr);
	break;
	
       case 0xCE:
	last_name = suck_data(last_name, 0, grr);
	break;
	
       case 0xFE:
	if (!read_comment_extension(gfi, grr)) goto done;
	break;
	
       case 0xFF:
	read_application_extension(&gfc, extension_position, grr);
	break;
	
       default:
	read_unknown_extension(gfs, block, 0, extension_position, grr);
	break;

      }
      break;
      
     default:
       if (!unknown_block_type) {
	 char buf[256];
	 sprintf(buf, "unknown block type %d at file offset %d", block, gifgetoffset(grr) - 1);
	 gif_read_error(&gfc, buf);
	 unknown_block_type = 1;
       }
       break;
      
    }
    
  }
  
 done:
  
  /* Move comments after last image into stream. */
  gfs->comment = gfi->comment;
  gfi->comment = 0;
  
  Gif_DeleteImage(gfi);
  Gif_DeleteArray(last_name);
  Gif_DeleteArray(gfc.prefix);
  Gif_DeleteArray(gfc.suffix);
  Gif_DeleteArray(gfc.length);
  return gfs;
}


Gif_Stream *
Gif_FullReadFile(FILE *f, int read_flags,
		 Gif_ReadErrorHandler h, void *hthunk)
{
  Gif_Reader grr;
  if (!f) return 0;
  grr.f = f;
  grr.is_record = 0;
  grr.byte_getter = file_byte_getter;
  grr.block_getter = file_block_getter;
  grr.offseter = file_offseter;
  grr.eofer = file_eofer;
  return read_gif(&grr, read_flags, h, hthunk);
}

Gif_Stream *
Gif_FullReadRecord(const Gif_Record *gifrec, int read_flags,
		   Gif_ReadErrorHandler h, void *hthunk)
{
  Gif_Reader grr;
  if (!gifrec) return 0;
  make_data_reader(&grr, gifrec->data, gifrec->length);
  if (read_flags & GIF_READ_CONST_RECORD)
    read_flags |= GIF_READ_COMPRESSED;
  return read_gif(&grr, read_flags, h, hthunk);
}


#undef Gif_ReadFile
#undef Gif_ReadRecord

Gif_Stream *
Gif_ReadFile(FILE *f)
{
  return Gif_FullReadFile(f, GIF_READ_UNCOMPRESSED, 0, 0);
}

Gif_Stream *
Gif_ReadRecord(const Gif_Record *gifrec)
{
  return Gif_FullReadRecord(gifrec, GIF_READ_UNCOMPRESSED, 0, 0);
}


#ifdef __cplusplus
}
#endif