File: bm_dgif_lib.c

package info (click to toggle)
ted 2.11-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 11,064 kB
  • ctags: 13,935
  • sloc: ansic: 120,446; makefile: 7,469; sh: 253
file content (493 lines) | stat: -rw-r--r-- 13,419 bytes parent folder | download
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
/******************************************************************************
*   "Gif-Lib" - Yet another gif library.				      *
*									      *
* Written by:  Gershon Elber			IBM PC Ver 1.1,	Aug. 1990     *
*******************************************************************************
* The kernel of the GIF Decoding process can be found here.		      *
*******************************************************************************
* History:								      *
* 16 Jun 89 - Version 1.0 by Gershon Elber.				      *
*  3 Sep 90 - Version 1.1 by Gershon Elber (Support for Gif89, Unique names). *
******************************************************************************/

#   include	<stdio.h>
#   include	<stdlib.h>
#   include	<string.h>
#   include	"bm_gif_lib.h"
#   include	"bm_gif_lib_private.h"

#   include	<sioBlocked.h>
#   include	<sioLzw.h>
#   include	<sioEndian.h>
#   include	<appDebugon.h>

#define COMMENT_EXT_FUNC_CODE	0xfe /* Extension function code for comment. */

/************************************************************************/
/*									*/
/*  Read a palette from the input file.					*/
/*									*/
/************************************************************************/

void bmGifInitGifColorMap(	GifColorMap *	gcm )
    {
    int		i;
    RGB8Color *	rgb8;

    gcm->gcmColorCount= 0;
    gcm->gcmBitsPerPixel= 0;

    rgb8= gcm->gcmColors;
    for ( i = 0; i < 256; rgb8++, i++ )
	{
	rgb8->rgb8Red= 255;
	rgb8->rgb8Green= 255;
	rgb8->rgb8Blue= 255;
	rgb8->rgb8Alpha= 255;
	}

    return;
    }

static int bmGifReadColorMap(	GifFilePrivateType *	gfpt,
				GifColorMap *		gcm,
				int			bitsPerPixel )
    {
    RGB8Color *		rgb8;
    int			i;

    bmGifInitGifColorMap( gcm );

    gcm->gcmBitsPerPixel= bitsPerPixel;
    gcm->gcmColorCount= 1 << bitsPerPixel;

    rgb8= gcm->gcmColors;
    for ( i = 0; i < gcm->gcmColorCount; rgb8++, i++ )
	{
	rgb8->rgb8Red= sioInGetCharacter( gfpt->gfptSis );
	rgb8->rgb8Green= sioInGetCharacter( gfpt->gfptSis );
	rgb8->rgb8Blue= sioInGetCharacter( gfpt->gfptSis );
	}

    return 0;
    }

/************************************************************************/
/*									*/
/*  Read the screen descriptor from the input file.			*/
/*  This is the first information that is read from the file by the	*/
/*  open routine.							*/
/*									*/
/*  1)  Opened for reading?						*/
/*  2)  Read screen size.						*/
/*  3)  Read the description of the global color map.			*/
/*  4)  And interpret it.						*/
/*  5)  Read global palette (if any).					*/
/*									*/
/************************************************************************/

static int bmGifReadScreenDescriptor(	GifFileType *	gft )
{
    GifFilePrivateType *	gfpt= (GifFilePrivateType *)gft->Private;
    GifScreenDescriptor *	gsd= &(gft->gftScreenDescriptor);

    int				bitsPerPixel;

    /*  1  */
    if  ( ! IS_READABLE( gfpt ) )
	{
	_GifError= D_GIF_ERR_NOT_READABLE;
	return GIF_ERROR;
	}

    bmGifInitGifColorMap( &(gsd->gsdScreenColorMap) );

    /*  2  */
    gsd->gsdScreenWide= sioEndianGetLeInt16( gfpt->gfptSis );
    gsd->gsdScreenHigh= sioEndianGetLeInt16( gfpt->gfptSis );

    /*  3  */
    gsd->gsdPackedFields= sioInGetCharacter( gfpt->gfptSis );
    gsd->gsdScreenBackgroundColor= sioInGetCharacter( gfpt->gfptSis );
    gsd->gsdScreenAspectRatio= sioInGetCharacter( gfpt->gfptSis );

    /*  4  */
    gsd->gsdScreenBitsPerPixel = (((gsd->gsdPackedFields & 0x70) + 1) >> 4) + 1;
    bitsPerPixel= ( gsd->gsdPackedFields & 0x07 ) + 1;

    /*  5  */
    if  ( gsd->gsdPackedFields & 0x80 )
	{
	if  ( bmGifReadColorMap( gfpt,
			    &(gsd->gsdScreenColorMap), bitsPerPixel ) )
	    { LDEB(bitsPerPixel); return GIF_ERROR;	}
	}

    return GIF_OK;
}

/************************************************************************/
/*									*/
/*  Setup the LZ decompression for this image				*/
/*									*/
/*  3)  Open the blocked input stream. (Push it on the unstructured	*/
/*	one.)								*/
/*  4)  Open the LZW stream (Push it on the blocked one.)		*/
/*									*/
/************************************************************************/

static int DGifSetupDecompress(	GifFileType *	gft )
{
    int				codeSize;
    GifFilePrivateType *	gfpt= (GifFilePrivateType *)gft->Private;

    codeSize= sioInGetCharacter( gfpt->gfptSis );
    if  ( codeSize == EOF )
	{ LDEB(codeSize); return GIF_ERROR;	}

    /*  3  */
    if  ( gfpt->gfptSisBlocked )
	{ XDEB(gfpt->gfptSisBlocked);	}

    gfpt->gfptSisBlocked= sioInBlockedOpen( gfpt->gfptSis );
    if  ( ! gfpt->gfptSisBlocked )
	{ XDEB(gfpt->gfptSisBlocked); return GIF_ERROR;	}

    /*  4  */
    if  ( gfpt->gfptSisLzw )
	{ XDEB(gfpt->gfptSisLzw);	}

    gfpt->gfptSisLzw= sioInLzwOpen( gfpt->gfptSisBlocked, codeSize );
    if  ( ! gfpt->gfptSisLzw )
	{ XDEB(gfpt->gfptSisLzw); return GIF_ERROR;	}

    return GIF_OK;
}

static void bmGifCleanupDecompress(	GifFilePrivateType *	gfpt )
    {
    if  ( gfpt->gfptSisLzw )
	{
	if  ( sioInClose( gfpt->gfptSisLzw ) )
	    { XDEB(gfpt->gfptSisLzw);	}

	gfpt->gfptSisLzw= (SimpleInputStream *)0;
	}

    if  ( gfpt->gfptSisBlocked )
	{
	if  ( sioInClose( gfpt->gfptSisBlocked ) )
	    { XDEB(gfpt->gfptSisBlocked);	}

	gfpt->gfptSisBlocked= (SimpleInputStream *)0;
	}

    return;
    }

/******************************************************************************
*   Update a new gif file, given its file handle.                             *
*   Returns GifFileType pointer dynamically allocated which serves as the gif *
*   info record. _GifError is cleared if succesfull.                          *
******************************************************************************/

GifFileType *DGifOpenFileHandle(	SimpleInputStream *	sis )
{
    GifFileType *		gft;
    GifFilePrivateType *	gfpt;

    gft= (GifFileType *)malloc( sizeof(GifFileType) );
    if  ( ! gft )
	{ XDEB(gft); return (GifFileType *)0;	}

    memset(gft, '\0', sizeof(GifFileType));

    strncpy( gft->gftVersionString, GIF87_STAMP, 6 )[6]= '\0';

    gfpt= (GifFilePrivateType *)malloc( sizeof(GifFilePrivateType) );
    if  ( ! gfpt )
	{
        _GifError= D_GIF_ERR_NOT_ENOUGH_MEM;
        free( gft );
        return (GifFileType *)0;
	}

    gft->Private= (void *)gfpt;
    gfpt->gfptSis= sis;
    gfpt->gfptSisBlocked= (SimpleInputStream *)0;
    gfpt->gfptSisLzw= (SimpleInputStream *)0;
    gfpt->FileState= FILE_STATE_READ;

    /* Lets see if this is a GIF file: */
    if  ( sioInReadBytes( gfpt->gfptSis,
			    (unsigned char *)gft->gftVersionString, 6 ) != 6 )
	{
	LDEB(6);
        _GifError= D_GIF_ERR_READ_FAILED;
        free( gfpt ); free( gft );
        return (GifFileType *)0;
	}

    if  ( strcmp( gft->gftVersionString, GIF87_STAMP )	&&
	  strcmp( gft->gftVersionString, GIF89_STAMP )	)
	{
	SDEB(gft->gftVersionString);
        _GifError= D_GIF_ERR_NOT_GIF_FILE;
        free( gfpt ); free( gft );
        return (GifFileType *)0;
	}

    if  ( bmGifReadScreenDescriptor( gft ) == GIF_ERROR )
	{
	LDEB(1);
        free( gfpt ); free( gft );
        return (GifFileType *)0;
	}

    _GifError = 0;

    return gft;
}

/******************************************************************************
*   This routine should be called before any attemp to read an image.         *
******************************************************************************/

int DGifGetRecordType(GifFileType *gft, GifRecordType *Type)
{
    GifByteType			Buf;
    GifFilePrivateType *	gfpt= (GifFilePrivateType *)gft->Private;

    if (!IS_READABLE( gfpt )) {
	/* This file was NOT open for reading: */
	_GifError = D_GIF_ERR_NOT_READABLE;
	return GIF_ERROR;
    }

    Buf= sioInGetCharacter( gfpt->gfptSis );

    switch( Buf )
	{
	case ',':
	    *Type= IMAGE_DESC_RECORD_TYPE;
	    break;

	case '!':
	    *Type= EXTENSION_RECORD_TYPE;
	    break;

	case ';':
	    *Type= TERMINATE_RECORD_TYPE;
	    break;

	default:
	    *Type= UNDEFINED_RECORD_TYPE;
	    _GifError = D_GIF_ERR_WRONG_RECORD;
	    return GIF_ERROR;
	}

    return GIF_OK;
}

/************************************************************************/
/*									*/
/*  Read an image descriptor from the input file.			*/
/*  NOTE that it is assumed the Image desc. header (',') has been read.	*/
/*									*/
/*  1)  Opened for reading?						*/
/*  2)  Read image geometry.						*/
/*  3)  Get image flag bits.						*/
/*  4)  Does the image have its own palette.. If so read it.		*/
/*									*/
/************************************************************************/

int DGifGetImageDesc(	GifFileType *	gft )
{
    int				bitsPerPixel;
    GifByteType			Buf[3];
    GifFilePrivateType *	gfpt= (GifFilePrivateType *)gft->Private;
    GifImageDesc *		gid= &(gft->gftCurrentImageDescriptor);

    /*  1  */
    if  ( ! IS_READABLE( gfpt ) )
	{
	_GifError = D_GIF_ERR_NOT_READABLE;
	return GIF_ERROR;
	}

    /*  2  */
    gid->Left= sioEndianGetLeInt16( gfpt->gfptSis );
    gid->Top= sioEndianGetLeInt16( gfpt->gfptSis );
    gid->Width= sioEndianGetLeInt16( gfpt->gfptSis );
    gid->Height= sioEndianGetLeInt16( gfpt->gfptSis );

    /*  3  */
    Buf[0]= sioInGetCharacter( gfpt->gfptSis );

    bitsPerPixel= (Buf[0] & 0x07) + 1;
    gid->Interlace= (Buf[0] & 0x40);

    bmGifInitGifColorMap( &(gid->gidImageColorMap) );

    /*  4  */
    if  ( Buf[0] & 0x80 )
	{
	if  ( bmGifReadColorMap( gfpt, &(gid->gidImageColorMap),
							    bitsPerPixel ) )
	    { LDEB(bitsPerPixel); return GIF_ERROR;	}
	}

    gfpt->PixelCount= (long)gid->Width *(long)gid->Height;

    DGifSetupDecompress( gft );  /* Reset decompress algorithm parameters. */

    return GIF_OK;
}

/************************************************************************/
/*									*/
/*  Get one full scanned line (Line) of length LineLen from GIF file.	*/
/*									*/
/*  3)  The image is finished.. Close (and drain) to blocked input	*/
/*	stream.								*/
/*									*/
/************************************************************************/

int bmGifGetPixels(	GifFileType *	gft,
			GifPixelType *	Line,
			int		LineLen )
{
    GifFilePrivateType *	gfpt= (GifFilePrivateType *)gft->Private;

    if  ( ! IS_READABLE( gfpt ) )
	{
	_GifError = D_GIF_ERR_NOT_READABLE;
	return GIF_ERROR;
	}

    if  ( LineLen > gfpt->PixelCount )
	{
	LLDEB(LineLen,gfpt->PixelCount);
	_GifError = D_GIF_ERR_DATA_TOO_BIG;
	return GIF_ERROR;
	}

    gfpt->PixelCount -= LineLen;

    if  ( sioInReadBytes( gfpt->gfptSisLzw, Line, LineLen ) == LineLen )
	{
	if  ( gfpt->PixelCount == 0 )
	    { bmGifCleanupDecompress( gfpt );	}

	return GIF_OK;
	}
    else{ LDEB(LineLen); return GIF_ERROR;	}
}

/************************************************************************/
/*									*/
/*  Get an extension block (see GIF manual) from gif file. This		*/
/*  routine only returns the first data block, and			*/
/*  DGifGetExtensionNext shouldbe called after this one until NULL	*/
/*  extension is returned.						*/
/*  The Extension should NOT be freed by the user (not dynamically	*/
/*  allocated).								*/
/*  Note it is assumed the Extension desc. header ('!') has been read.	*/
/*									*/
/************************************************************************/

int DGifGetExtension(	GifFileType *		gft,
			int *			ExtCode,
			GifByteType		Extension[256] )
{
    GifByteType			Buf;
    GifFilePrivateType *	gfpt= (GifFilePrivateType *)gft->Private;
    int				got;

    if (!IS_READABLE( gfpt )) {
	/* This file was NOT open for reading: */
	_GifError = D_GIF_ERR_NOT_READABLE;
	return GIF_ERROR;
    }

    Buf= sioInGetCharacter( gfpt->gfptSis );

    *ExtCode= Buf;

    if  ( DGifGetExtensionNext( gft, &got, Extension) != GIF_OK )
	{ LDEB(1); return GIF_ERROR;	}
    if  ( ! got )
	{ LDEB(got); return GIF_ERROR;	}

    return GIF_OK;
}

/************************************************************************/
/*									*/
/*  Get a following extension block (see GIF manual) from gif file.	*/
/*  This routine should be called until NULL Extension is returned.	*/
/*  The Extension should NOT be freed by the user (not dynamically	*/
/*  allocated).								*/
/*									*/
/************************************************************************/

int DGifGetExtensionNext(	GifFileType *	gft,
				int *		pGot,
				GifByteType	Extension[256] )
{
    int				byte;
    GifFilePrivateType *	gfpt= (GifFilePrivateType *)gft->Private;

    byte= sioInGetCharacter( gfpt->gfptSis );
    if  ( byte == EOF )
	{
	XDEB(byte);
	_GifError = D_GIF_ERR_READ_FAILED;
	return GIF_ERROR;
	}

    if  ( byte == 0 )
	{ *pGot= 0; return GIF_OK; }

    if  ( sioInReadBytes( gfpt->gfptSis, Extension+ 1, byte ) != byte )
	{
	LDEB(byte);
	_GifError = D_GIF_ERR_READ_FAILED; return GIF_ERROR;
	}
    *pGot= 1;
    Extension[0]= byte;
    return GIF_OK;
}

/************************************************************************/
/*									*/
/*  Close a gif file that was opened for reading: Do the necessary	*/
/*  cleanup.								*/
/*									*/
/************************************************************************/

int DGifCloseFile(GifFileType *gft)
{
    GifFilePrivateType *	gfpt;

    if (gft == NULL) return GIF_ERROR;

    gfpt= (GifFilePrivateType *)gft->Private;

    if  ( ! IS_READABLE( gfpt ) )
	{
	_GifError = D_GIF_ERR_NOT_READABLE;
	return GIF_ERROR;
	}

    if  ( gfpt )
    {
	bmGifCleanupDecompress( gfpt );
	free( gfpt );
    }

    free(gft);

    return GIF_OK;
}