File: readgif.cxx

package info (click to toggle)
imview 1.1.9c-7
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,076 kB
  • ctags: 4,780
  • sloc: cpp: 28,836; sh: 2,624; ansic: 1,818; makefile: 731; exp: 112; python: 88
file content (496 lines) | stat: -rw-r--r-- 13,508 bytes parent folder | download | duplicates (9)
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
/*
 * $Id: readgif.cxx,v 4.1 2004/05/25 06:40:20 hut66au Exp $
 *
 * Imview, the portable image analysis application
 * http://www.cmis.csiro.au/Hugues.Talbot/imview
 * ----------------------------------------------------------
 *
 *  Imview is an attempt to provide an image display application
 *  suitable for professional image analysis. It was started in
 *  1997 and is mostly the result of the efforts of Hugues Talbot,
 *  Image Analysis Project, CSIRO Mathematical and Information
 *  Sciences, with help from others (see the CREDITS files for
 *  more information)
 *
 *  Imview is Copyrighted (C) 1997-2001 by Hugues Talbot and was
 *  supported in parts by the Australian Commonwealth Science and 
 *  Industry Research Organisation. Please see the COPYRIGHT file 
 *  for full details. Imview also includes the contributions of 
 *  many others. Please see the CREDITS file for full details.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  
 *  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.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 * */

// readgif.C

// adapted for my own purposed by Hugues Talbot	21 Jan 1998
// in particular got rid of the silly conversion to pixmap!
// now returns the data in a simple GL or RGB buffer

// Extensively modified from original code for gif2ras by
// Patrick J. Naughton of Sun Microsystems.  The original
// copyright notice follows:

/* gif2ras.c - Converts from a Compuserve GIF (tm) image to a Sun Raster image.
 *
 * Copyright (c) 1988 by Patrick J. Naughton
 *
 * Author: Patrick J. Naughton
 * naughton@wind.sun.com
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation.
 *
 */
#include "imnmspc.hxx" // name space definition if needed 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "imview.hxx"
#include "imSystem.hxx"
#include "imageIO.hxx"
#include "readgif.hxx"

extern imageIO *IOBlackBox;

// DEC cxx doesn't like that!
static uchar  *loadGifImage(FILE *GifFile, int *imWidth,int *imHeight, int *imDepth, int inumber=0);


int readGifImage(const char *GifFileName,	// file to read
		 int inumber)		// which image in movie (0 = first)
{
    FILE *fp; 
    uchar *outbuf;
    int    imWidth, imHeight, imDepth;
    
    if ((fp = im_fopen(GifFileName, "rb")) == 0) {
	errprintf("Cannot open %s\n", GifFileName);
	return 0;
    }

    outbuf = loadGifImage(fp, &imWidth, &imHeight, &imDepth, inumber);

    // this will require no translation
    IOBlackBox->setImData(outbuf);
    IOBlackBox->setCurrImgWidth(imWidth);
    IOBlackBox->setCurrImgHeight(imHeight);
    IOBlackBox->setCurrImgThickness(1);
    IOBlackBox->setXOffset(0);
    IOBlackBox->setYOffset(0);
    IOBlackBox->setZOffset(0);
    IOBlackBox->setCurrImgNbComps(1); // only a single image in a GIF file
    IOBlackBox->setCurrImgNbSamples(imDepth);
    IOBlackBox->setCurrImgSpp(imDepth); // needed here because no currBuffp
    IOBlackBox->setCurrPixType(IM_UINT1);
    IOBlackBox->setCurrImgType((imDepth == 3) ? IM_RGB : IM_SPECTRUM);
    IOBlackBox->setImgDesc("GIF");
    
    fclose(fp);

    return (outbuf == 0);
}


#define NEXTBYTE getc(GifFile)
#define GETSHORT(var) var = NEXTBYTE; var += NEXTBYTE << 8

// This function returns the number of frames
// in an animated GIF file. Negative numbers indicate
// errors
int gifnbsubfiles(const char *fname)
{
    int   i;
    FILE *GifFile;
    int   inumber = 0;
    
    if ((GifFile = im_fopen(fname, "rb")) == NULL)
	return -1; // can't read the file
    
    {
	char b[6];
	if (fread(b,1,6,GifFile)<6) return 0; /* quit on eof */
	if (b[0]!='G' || b[1]!='I' || b[2] != 'F') {
	    dbgprintf("not a GIF file!\n"); return 0;}
	if (b[3]!='8' || b[4]>'9' || b[5]!= 'a')
	    dbgprintf("GIF version %c%c%c. May cause trouble\n",b[3],b[4],b[5]);
    }

    int Width; GETSHORT(Width);
    int Height; GETSHORT(Height);
    
    uchar ch = NEXTBYTE;
    char HasColormap = ((ch & 0x80) != 0);
    int BitsPerPixel = (ch & 7) + 1;
    int ColorMapSize = 1 << BitsPerPixel;
    // int OriginalResolution = ((ch>>4)&7)+1;
    // int SortedTable = (ch&8)!=0;
    NEXTBYTE; // Background Color index
    NEXTBYTE; // Aspect ratio is N/64

    // Read in global colormap:
    uchar transparent_pixel = 0;
    char has_transparent = 0;
    if (HasColormap) {
	for (i=0; i < ColorMapSize; i++) {
	    NEXTBYTE; NEXTBYTE; NEXTBYTE;
	}
    } else {
	dbgprintf("File does not have a colormap.\n");
    }

    int CodeSize;		/* Code size, init from GIF header, increases... */
    char Interlace;

    for (;;) {

	i = NEXTBYTE;
	if (i<0) {dbgprintf("reached EOF\n"); break;}
	int blocklen;

	if (i == 0x21) {		// a "gif extension"

	    ch = NEXTBYTE;
	    blocklen = NEXTBYTE;

	    if (ch==0xF9 && blocklen==4) { // Netscape animation extension

		char bits;
		bits = NEXTBYTE;
		NEXTBYTE; NEXTBYTE; // GETSHORT(delay);
		transparent_pixel = NEXTBYTE;
		if (bits & 1) has_transparent = 1;
		blocklen = NEXTBYTE;

	    } else if (ch == 0xFF) {
		; // Netscape repeat count

	    } else {
		dbgprintf("unknown gif extension 0x%02x\n", ch);

	    }

	} else if (i == 0x2c) {	// an image

	    NEXTBYTE; NEXTBYTE; // GETSHORT(x_position);
	    NEXTBYTE; NEXTBYTE; // GETSHORT(y_position);
	    GETSHORT(Width);
	    GETSHORT(Height);
	    ch = NEXTBYTE;
	    Interlace = ((ch & 0x40) != 0);
	    if (ch&0x80) { 
		// skip over local color map
		int n = 1<<((ch&7)+1); // does this replace ColorMapSize ??
		for (i=0; i < n; i++) {	
		    NEXTBYTE; NEXTBYTE; NEXTBYTE;
		}
	    }
	    CodeSize = NEXTBYTE+1;

	    inumber++;  // count the next block
	    blocklen = NEXTBYTE;
      
	} else {
	    dbgprintf("unknown gif code 0x%02x, piking out.\n", i);
	    blocklen = 0;
	    break; // taking no chance there.
	}

	// skip the data:
	while (blocklen>0) {while (blocklen--) {NEXTBYTE;} blocklen=NEXTBYTE;}
    }

    fclose(GifFile);

    return inumber;
}

static uchar  *loadGifImage(FILE *GifFile,	// file to read
			     int *imWidth,
			     int *imHeight,
			     int *imDepth,
			     int inumber		// which image in movie (0 = first)
    )
{
    uchar *outbuf = 0;
    int i;
    
    

    {
	char b[6];
	if (fread(b,1,6,GifFile)<6) return 0; /* quit on eof */
	if (b[0]!='G' || b[1]!='I' || b[2] != 'F') {
	    dbgprintf("not a GIF file!\n"); return 0;}
	if (b[3]!='8' || b[4]>'9' || b[5]!= 'a')
	    dbgprintf("GIF version %c%c%c. May cause trouble\n",b[3],b[4],b[5]);
    }

    int Width; GETSHORT(Width);
    int Height; GETSHORT(Height);
    
    uchar ch = NEXTBYTE;
    char HasColormap = ((ch & 0x80) != 0);
    int BitsPerPixel = (ch & 7) + 1;
    int ColorMapSize = 1 << BitsPerPixel;
    // int OriginalResolution = ((ch>>4)&7)+1;
    // int SortedTable = (ch&8)!=0;
    NEXTBYTE; // Background Color index
    NEXTBYTE; // Aspect ratio is N/64

    // Read in global colormap:
    uchar transparent_pixel = 0;
    char has_transparent = 0;
    uchar Red[256], Green[256], Blue[256]; /* color map */
    if (HasColormap) {
	for (i=0; i < ColorMapSize; i++) {	
	    Red[i] = NEXTBYTE;
	    Green[i] = NEXTBYTE;
	    Blue[i] = NEXTBYTE;
	}
    } else {
	dbgprintf("File does not have a colormap.\n");
	for (i = 0; i < ColorMapSize; i++)
	    Red[i] = Green[i] = Blue[i] = (i*256+ColorMapSize-1)/ColorMapSize;
    }

    int CodeSize;		/* Code size, init from GIF header, increases... */
    char Interlace;

    for (;;) {

	i = NEXTBYTE;
	if (i<0) {dbgprintf("unexpected EOF\n"); return 0;}
	int blocklen;

	//  if (i == 0x3B) return 0;  eof code

	if (i == 0x21) {		// a "gif extension"

	    ch = NEXTBYTE;
	    blocklen = NEXTBYTE;

	    if (ch==0xF9 && blocklen==4) { // Netscape animation extension

		char bits;
		bits = NEXTBYTE;
		NEXTBYTE; NEXTBYTE; // GETSHORT(delay);
		transparent_pixel = NEXTBYTE;
		if (bits & 1) has_transparent = 1;
		blocklen = NEXTBYTE;

	    } else if (ch == 0xFF) {
		; // Netscape repeat count

	    } else {
		dbgprintf("unknown gif extension 0x%02x\n", ch);

	    }

	} else if (i == 0x2c) {	// an image

	    NEXTBYTE; NEXTBYTE; // GETSHORT(x_position);
	    NEXTBYTE; NEXTBYTE; // GETSHORT(y_position);
	    GETSHORT(Width);
	    GETSHORT(Height);
	    ch = NEXTBYTE;
	    Interlace = ((ch & 0x40) != 0);
	    if (ch&0x80) { 
		// read local color map
		int n = 1<<((ch&7)+1); // does this replace ColorMapSize ??
		for (i=0; i < n; i++) {	
		    Red[i] = NEXTBYTE;
		    Green[i] = NEXTBYTE;
		    Blue[i] = NEXTBYTE;
		}
	    }
	    CodeSize = NEXTBYTE+1;

	    if (!inumber--) break; // okay, this is the image we want
	    blocklen = NEXTBYTE;
      
	} else {
	    dbgprintf("unknown gif code 0x%02x\n", i);
	    blocklen = 0;
	}

	// skip the data:
	while (blocklen>0) {while (blocklen--) {NEXTBYTE;} blocklen=NEXTBYTE;}
    }

    uchar *Image = new uchar[Width*Height];
    if (!Image) {
	errprintf("Insufficient memory\n");
	return(outbuf);
    }
    int YC = 0, Pass = 0; /* Used to de-interlace the picture */
    uchar *p = Image;
    uchar *eol = p+Width;

    int InitCodeSize = CodeSize;
    int ClearCode = (1 << (CodeSize-1));
    int EOFCode = ClearCode + 1;
    int FirstFree = ClearCode + 2;
    int FinChar = 0;
    int ReadMask = (1<<CodeSize) - 1;
    int FreeCode = FirstFree;
    int OldCode = ClearCode;

    // tables used by LZW decompresser:
    short int Prefix[4096];
    uchar Suffix[4096];

    int blocklen = NEXTBYTE;
    uchar thisbyte = NEXTBYTE; blocklen--;
    int frombit = 0;

    for (;;) {

/* Fetch the next code from the raster data stream.  The codes can be
 * any length from 3 to 12 bits, packed into 8-bit bytes, so we have to
 * maintain our location as a pointer and a bit offset.
 * In addition, gif adds totally useless and annoying block counts
 * that must be correctly skipped over. */
	int CurCode = thisbyte;
	if (frombit+CodeSize > 7) {
	    if (blocklen <= 0) {
		blocklen = NEXTBYTE;
		if (blocklen <= 0) break;
	    }
	    thisbyte = NEXTBYTE; blocklen--;
	    CurCode |= thisbyte<<8;
	}
	if (frombit+CodeSize > 15) {
	    if (blocklen <= 0) {
		blocklen = NEXTBYTE;
		if (blocklen <= 0) break;
	    }
	    thisbyte = NEXTBYTE; blocklen--;
	    CurCode |= thisbyte<<16;
	}
	CurCode = (CurCode>>frombit)&ReadMask;
	frombit = (frombit+CodeSize)%8;

	if (CurCode == ClearCode) {
	    CodeSize = InitCodeSize;
	    ReadMask = (1<<CodeSize) - 1;
	    FreeCode = FirstFree;
	    OldCode = ClearCode;
	    continue;
	}

	if (CurCode == EOFCode) break;

	uchar OutCode[1025]; // temporary array for reversing codes
	uchar *tp = OutCode;
	if (CurCode < FreeCode) i = CurCode;
	else if (CurCode == FreeCode) {*tp++ = FinChar; i = OldCode;}
	else {dbgprintf("LZW Barf!\n"); break;}

	while (i >= ColorMapSize) {*tp++ = Suffix[i]; i = Prefix[i];}
	*tp++ = FinChar = i;
	while (tp > OutCode) {
	    *p++ = *--tp;
	    if (p >= eol) {
		if (!Interlace) YC++;
		else switch (Pass) {
		  case 0: YC += 8; if (YC >= Height) {Pass++; YC = 4;} break;
		  case 1: YC += 8; if (YC >= Height) {Pass++; YC = 2;} break;
		  case 2: YC += 4; if (YC >= Height) {Pass++; YC = 1;} break;
		  case 3: YC += 2; break;
		}
		if (YC>=Height) YC=0; /* cheap bug fix when excess data */
		p = Image + YC*Width;
		eol = p+Width;
	    }
	}

	if (OldCode != ClearCode) {
	    Prefix[FreeCode] = OldCode;
	    Suffix[FreeCode] = FinChar;
	    FreeCode++;
	    if (FreeCode > ReadMask) {
		if (CodeSize < 12) {
		    CodeSize++;
		    ReadMask = (1 << CodeSize) - 1;
		}
		else FreeCode--;
	    }
	}
	OldCode = CurCode;
    }

    // We are done reading the file, now convert to xpm:



    
    // transparent pixel must be zero, swap if it isn't:
    if (has_transparent && transparent_pixel != 0) {
	// swap transparent pixel with zero
	p = Image+Width*Height;
	while (p-- > Image) {
	    if (*p==transparent_pixel) *p = 0;
	    else if (!*p) *p = transparent_pixel;
	}
	uchar t;
	t = Red[0]; Red[0] = Red[transparent_pixel]; Red[transparent_pixel] = t;
	t =Green[0];Green[0]=Green[transparent_pixel];Green[transparent_pixel]=t;
	t =Blue[0];Blue[0] =Blue[transparent_pixel];Blue[transparent_pixel] = t;
    }

    // is the image colour or Grey-level?
    *imDepth = 1;
    for (i = 0 ; i < ColorMapSize ; i++) {
	if ((Red[i] != Green[i]) || (Red[i] != Blue[i])) {
	    *imDepth = 3;
	    break;
	}
    }

    *imWidth = Width;
    *imHeight = Height;

    int nbBytes = (*imWidth)*(*imHeight)*(*imDepth);
    outbuf = new uchar[nbBytes];

    if (outbuf != 0) {
	uchar *end;
	uchar *q = Image;

	p = outbuf;
	end = p + nbBytes;
	// interprets the colour LUT
	if (*imDepth == 1) {
	    while (p != end) {
		*p++ = Red[*q++];
	    }
	} else {
	    while (p != end) {
		*p++ = Red[*q];
		*p++ = Green[*q];
		*p++ = Blue[*q++];
	    }
	}
    }

    delete[] Image;
    return outbuf;
}