File: bm_egif_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 (568 lines) | stat: -rw-r--r-- 16,352 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
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
/************************************************************************/
/*									*/
/*  Gif writing basic functionality.					*/
/*									*/
/*  ORIGIN: "Gif-Lib" - Yet another gif library.			*/
/*  Written by:  Gershon Elber, Ver 1.1, Aug. 1990			*/
/*  Version 3.0 by Eric S. Raymond (Full GIF89 support)			*/
/*									*/
/*  Modified to what it is now by Mark de Does				*/
/*									*/
/************************************************************************/

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

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

/************************************************************************/
/*									*/
/*  Write a color map to file.						*/
/*									*/
/************************************************************************/

static int bmGifWriteColorMap(	GifFilePrivateType *	gfpt,
				const GifColorMap *	gcm )
    {
    int			i;
    const RGB8Color *	rgb8;
    int			count= 1 << gcm->gcmBitsPerPixel;

    if  ( gcm->gcmColorCount > count )
	{ LLDEB(gcm->gcmColorCount,count);	}

    rgb8= gcm->gcmColors;
    for ( i= 0; i < gcm->gcmColorCount && i < count; rgb8++, i++ )
	{
	sioOutPutCharacter( rgb8->rgb8Red, gfpt->gfptSos );
	sioOutPutCharacter( rgb8->rgb8Green, gfpt->gfptSos );
	sioOutPutCharacter( rgb8->rgb8Blue, gfpt->gfptSos );
	}

    for ( i= gcm->gcmColorCount; i < count; rgb8++, i++ )
	{
	sioOutPutCharacter( 255, gfpt->gfptSos );
	sioOutPutCharacter( 255, gfpt->gfptSos );
	sioOutPutCharacter( 255, gfpt->gfptSos );
	}

    return 0;
    }

/************************************************************************/
/*									*/
/*  Initialize GIF writing to an OutputStream.				*/
/*									*/
/*  _GifError is cleared if succesfull.					*/
/*									*/
/************************************************************************/

GifFileType * EGifOpenFileHandle(	SimpleOutputStream *	sos )
{
    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 )
	{ XDEB(gfpt); free(gft); return (GifFileType *)0;	}

    gft->Private = (void *) gfpt;
    gfpt->gfptSos= sos;
    gfpt->gfptSosBlocked= (SimpleOutputStream *)0;
    gfpt->gfptSosLzw= (SimpleOutputStream *)0;
    gfpt->FileState = FILE_STATE_WRITE;
    
    _GifError = 0;

    return gft;
}

/******************************************************************************
*   Routine to set current GIF version. All files open for write will be      *
* using this version until next call to this routine. Version consists of     *
* 3 characters as "87a" or "89a". No test is made to validate the version.    *
******************************************************************************/

void bmGifSetVersion(	GifFileType *	gft,
			const char *	version )
{
    if  ( strcmp( version, GIF87_STAMP+ 3 )	&&
	  strcmp( version, GIF89_STAMP+ 3 )	)
	{ SDEB(version); return;	}

    strncpy( gft->gftVersionString+ 3, version, 3 );
}

/************************************************************************/
/*									*/
/*  Write the Screen descriptor to the output.				*/
/*									*/
/*  This routine should be called before any other EGif calls, I.E	*/
/*  immediately after the GIF file openning.				*/
/*									*/
/*  1)  First write the version prefix into the file.			*/
/*  2)  Put the screen descriptor into the file				*/
/*  3)  If we have Global color map - dump it also			*/
/*									*/
/************************************************************************/

int EGifPutScreenDesc(	GifFileType *		gft,
			int			Width,
			int			Height,
			int			bitsPerPixel,
			int			BackGround,
			const GifColorMap *	gcm )
{
    GifFilePrivateType *	gfpt= (GifFilePrivateType *)gft->Private;
    GifScreenDescriptor *	gsd= &(gft->gftScreenDescriptor);

    if (gfpt->FileState & FILE_STATE_SCREEN) {
	/* If already has screen descriptor - something is wrong! */
	_GifError = E_GIF_ERR_HAS_SCRN_DSCR;
	return GIF_ERROR;
    }

    if (!IS_WRITEABLE(gfpt)) {
	/* This file was NOT open for writing: */
	_GifError = E_GIF_ERR_NOT_WRITEABLE;
	return GIF_ERROR;
    }

    /*  1  */
    sioOutPutString( gft->gftVersionString, gfpt->gfptSos );

    gsd->gsdScreenWide= Width;
    gsd->gsdScreenHigh= Height;
    gsd->gsdScreenBitsPerPixel= bitsPerPixel;
    gsd->gsdScreenBackgroundColor= BackGround;
    gsd->gsdScreenAspectRatio= 0;

    bmGifInitGifColorMap( &(gsd->gsdScreenColorMap) );

    if  ( gcm )
	{ gsd->gsdScreenColorMap= *gcm;	}

    /*  2  */
    sioEndianPutLeInt16( gsd->gsdScreenWide, gfpt->gfptSos );
    sioEndianPutLeInt16( gsd->gsdScreenHigh, gfpt->gfptSos );

    gsd->gsdPackedFields= 0;
    if  ( gcm && gcm->gcmColorCount > 0 )
	{
	gsd->gsdPackedFields |= 0x80;
	gsd->gsdPackedFields |= gcm->gcmBitsPerPixel -1;
	}
    gsd->gsdPackedFields |= ( bitsPerPixel -1 ) << 4;

    sioOutPutCharacter( gsd->gsdPackedFields, gfpt->gfptSos );
    sioOutPutCharacter( gsd->gsdScreenBackgroundColor, gfpt->gfptSos );
    sioOutPutCharacter( gsd->gsdScreenAspectRatio, gfpt->gfptSos );

    /*  3  */
    if  ( gcm && gcm->gcmColorCount > 0 )
	{
	if  ( bmGifWriteColorMap( gfpt, &(gsd->gsdScreenColorMap) ) )
	    { XDEB(gcm); return GIF_ERROR;	}
	}

    /* Mark this file as has screen descriptor, and no pixel written yet: */
    gfpt->FileState |= FILE_STATE_SCREEN;

    return GIF_OK;
}

/************************************************************************/
/*									*/
/*  Emit an image descriptor to file.					*/
/*									*/
/*  This routine should be called after a screen descripto is emmitted	*/
/*  and before any of the image data is emitted. If the image has	*/
/*  transparent parts, the graphics extension saying so should have	*/
/*  been emitted before.						*/
/*									*/
/*  In general, any extensions and comments relating to an image should	*/
/*  be emitted BEFORE the image.					*/
/*									*/
/*  Setup the LZ compression for this image:				*/
/*									*/
/*  2)  Find out what color map is relevant and determine BitsPerPixel.	*/
/*  3)  Determine code size and insert it in the output stream.		*/
/*  4)  Open a blocked stream to divide the compressed output in	*/
/*	packets. (Push it on the unstructured one.)			*/
/*  5)  Open an LZW compressed stream, push it on the blocked one.	*/
/*									*/
/*  The result is that pixels emitted to the image are LZW compressed,	*/
/*  the compressed data is divided in packets and the packets are	*/
/*  emitted over the output stream for the image.			*/
/*									*/
/************************************************************************/

static int bmGifSetupCompress(	GifFilePrivateType *	gfpt,
				int			codeSize )
    {
    /*  4  */
    if  ( gfpt->gfptSosBlocked )
	{ XDEB(gfpt->gfptSosBlocked);	}

    gfpt->gfptSosBlocked= sioOutBlockedOpen( gfpt->gfptSos );
    if  ( ! gfpt->gfptSosBlocked )
	{ XDEB(gfpt->gfptSosBlocked); return -1;	}

    /*  5  */
    if  ( gfpt->gfptSosLzw )
	{ XDEB(gfpt->gfptSosLzw);	}

    gfpt->gfptSosLzw= sioOutLzwOpen( gfpt->gfptSosBlocked, codeSize );
    if  ( ! gfpt->gfptSosLzw )
	{ XDEB(gfpt->gfptSosLzw); return -1;	}

    return 0;
    }

static int bmGifCleanupCompress(	GifFilePrivateType *	gfpt )
    {
    if  ( gfpt->gfptSosLzw )
	{
	if  ( sioOutClose( gfpt->gfptSosLzw ) )
	    { LDEB(1); return -1;	}
	gfpt->gfptSosLzw= (SimpleOutputStream *)0;
	}

    if  ( gfpt->gfptSosBlocked )
	{
	if  ( sioOutClose( gfpt->gfptSosBlocked ) )
	    { LDEB(1); return -1;	}
	gfpt->gfptSosBlocked= (SimpleOutputStream *)0;
	}

    return 0;
    }

int EGifPutImageDesc(	GifFileType *		gft,
			int			Left,
			int			Top,
			int			Width,
			int			Height,
			int			Interlace,
			const GifColorMap *	gcm )
{
    unsigned char		flags;
    GifFilePrivateType *	gfpt= (GifFilePrivateType *)gft->Private;
    GifScreenDescriptor *	gsd= &(gft->gftScreenDescriptor);
    GifImageDesc *		gid= &(gft->gftCurrentImageDescriptor);

    int				codeSize;

    if (gfpt->FileState & FILE_STATE_IMAGE &&
#if defined(__MSDOS__) || defined(__GNUC__)
	gfpt->PixelCount > 0xffff0000UL) {
#else
	gfpt->PixelCount > 0xffff0000) {
#endif /* __MSDOS__ */
	/* If already has active image descriptor - something is wrong! */
	_GifError = E_GIF_ERR_HAS_IMAG_DSCR;
	return GIF_ERROR;
    }
    if (!IS_WRITEABLE(gfpt)) {
	/* This file was NOT open for writing: */
	_GifError = E_GIF_ERR_NOT_WRITEABLE;
	return GIF_ERROR;
    }

    gid->Left = Left;
    gid->Top = Top;
    gid->Width = Width;
    gid->Height = Height;
    gid->Interlace = Interlace;

    bmGifInitGifColorMap( &(gid->gidImageColorMap) );

    if  ( gcm )
	{ gid->gidImageColorMap= *gcm;	}

    /* Put the image descriptor into the file: */
    sioOutPutCharacter( ',', gfpt->gfptSos );
    sioEndianPutLeInt16( gid->Left, gfpt->gfptSos );
    sioEndianPutLeInt16( gid->Top, gfpt->gfptSos );
    sioEndianPutLeInt16( gid->Width, gfpt->gfptSos );
    sioEndianPutLeInt16( gid->Height, gfpt->gfptSos );

    flags= 0;
    if  ( gcm && gcm->gcmColorCount > 0 )
	{
	flags |= 0x80;
	flags |= gcm->gcmBitsPerPixel - 1;
	}
    if  ( Interlace )
	{
	flags |= 0x40;
	}
    sioOutPutCharacter( flags, gfpt->gfptSos );

    /* If we have Global color map - dump it also: */
    if  ( gcm && gcm->gcmColorCount > 0 )
	{
	if  ( bmGifWriteColorMap( gfpt, gcm ) )
	    { XDEB(gcm); return GIF_ERROR;	}
	}

    /* Mark this file as having an image: */
    gfpt->FileState |= FILE_STATE_IMAGE;
    gfpt->PixelCount = (long) Width * (long) Height;

    /*  2  */
    if  ( gcm && gcm->gcmColorCount > 0 )
	{ codeSize= gcm->gcmBitsPerPixel;	}
    else{
	if  ( gsd->gsdScreenColorMap.gcmColorCount > 0 )
	    { codeSize= gsd->gsdScreenColorMap.gcmBitsPerPixel;	}
	else{
	    if  ( gcm )
		{ LDEB(gcm->gcmColorCount);	}
	    else{ XDEB(gcm);			}
	    LDEB( gsd->gsdScreenColorMap.gcmBitsPerPixel );

	    _GifError = E_GIF_ERR_NO_COLOR_MAP;
	    return GIF_ERROR;
	    }
	}

    /*  3  */
    codeSize= ( codeSize < 2 ? 2 : codeSize );
    sioOutPutCharacter( codeSize, gfpt->gfptSos );

    if  ( bmGifSetupCompress( gfpt, codeSize ) )
	{ LDEB(1); return GIF_ERROR;	}

    return GIF_OK;
}

/************************************************************************/
/*									*/
/*  Emit a series of pixel values to file.. typically a scanline.	*/
/*									*/
/*  1)  File not writable.. Refuse.					*/
/*  2)  Refuse to exceed the number of pixels in the image.		*/
/*  3)  Subtract the number of pixels from that left in the file.	*/
/*  4)  Actually emit the pixels.					*/
/*  5)  When the image is finished, clean compressor.			*/
/*									*/
/************************************************************************/

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

    /*  1  */
    if  ( ! IS_WRITEABLE( gfpt ) )
	{
	_GifError= E_GIF_ERR_NOT_WRITEABLE;
	return GIF_ERROR;
	}

    /*  2  */
    if  ( gfpt->PixelCount < (unsigned)LineLen )
	{
	LLDEB(gfpt->PixelCount,LineLen);
	_GifError = E_GIF_ERR_DATA_TOO_BIG;
	return GIF_ERROR;
	}

    /*  3  */
    gfpt->PixelCount -= LineLen;

    /*  4  */
    if  ( sioOutWriteBytes( gfpt->gfptSosLzw, Line, LineLen ) != LineLen )
	{
	LDEB(LineLen);
	_GifError = E_GIF_ERR_DISK_IS_FULL; return GIF_ERROR;
	}

    /*  5  */
    if  ( gfpt->PixelCount == 0			&&
	  bmGifCleanupCompress( gfpt )		)
	{ LDEB(gfpt->PixelCount); return GIF_ERROR;	}

    return GIF_OK;
}

/************************************************************************/
/*									*/
/*  Put a comment into GIF file using the GIF89 comment extension	*/
/*  block.								*/
/*									*/
/************************************************************************/

int EGifPutComment(	GifFileType *	gft,
			const char *	Comment )
{
    return EGifPutExtension(gft, COMMENT_EXT_FUNC_CODE, strlen(Comment),
								Comment);
}

/******************************************************************************
*   Put a first extension block (see GIF manual) into gif file.  Here more    *
* extensions can be dumped using EGifPutExtensionMid until		      *
* EGifPutExtensionLast is invoked.					      *
******************************************************************************/

int EGifPutExtensionFirst(	GifFileType *	gft,
				int		ExtCode,
				int		ExtLen,
				const void *	Extension)
{
    GifFilePrivateType *	gfpt= (GifFilePrivateType *) gft->Private;

    if (!IS_WRITEABLE(gfpt)) {
	/* This file was NOT open for writing: */
	_GifError = E_GIF_ERR_NOT_WRITEABLE;
	return GIF_ERROR;
    }

    if  ( ExtCode == 0 )
	{
	sioOutPutCharacter( ExtLen, gfpt->gfptSos );
	}
    else{
	sioOutPutCharacter( '!', gfpt->gfptSos );
	sioOutPutCharacter( ExtCode, gfpt->gfptSos );
	sioOutPutCharacter( ExtLen, gfpt->gfptSos );
	}

    if  ( sioOutWriteBytes( gfpt->gfptSos, Extension, ExtLen ) != ExtLen )
	{ LDEB(ExtLen); return GIF_ERROR;	}

    return GIF_OK;
}

/******************************************************************************
*   Put a middle extension block (see GIF manual) into gif file.	      *
******************************************************************************/

int EGifPutExtensionNext(	GifFileType *	gft,
				int		ExtCode,
				int		ExtLen,
				const void *	Extension)
{
    GifFilePrivateType *	gfpt= (GifFilePrivateType *)gft->Private;

    if (!IS_WRITEABLE(gfpt)) {
	/* This file was NOT open for writing: */
	_GifError = E_GIF_ERR_NOT_WRITEABLE;
	return GIF_ERROR;
    }

    sioOutPutCharacter( ExtLen, gfpt->gfptSos );

    if  ( sioOutWriteBytes( gfpt->gfptSos, Extension, ExtLen ) != ExtLen )
	{ LDEB(ExtLen); return GIF_ERROR;	}

    return GIF_OK;
}

/******************************************************************************
*   Put a last extension block (see GIF manual) into gif file.		      *
******************************************************************************/

int EGifPutExtensionLast(	GifFileType *	gft,
				int		ExtCode,
				int		ExtLen,
				const void *	Extension )
{
    GifFilePrivateType *	gfpt= (GifFilePrivateType *)gft->Private;

    if (!IS_WRITEABLE(gfpt)) {
	/* This file was NOT open for writing: */
	_GifError = E_GIF_ERR_NOT_WRITEABLE;
	return GIF_ERROR;
    }

    sioOutPutCharacter( ExtLen, gfpt->gfptSos );

    if  ( sioOutWriteBytes( gfpt->gfptSos, Extension, ExtLen ) != ExtLen )
	{ LDEB(ExtLen); return GIF_ERROR;	}

    sioOutPutCharacter( '\0', gfpt->gfptSos );

    return GIF_OK;
}

/******************************************************************************
*   Put an extension block (see GIF manual) into gif file.		      *
******************************************************************************/

int EGifPutExtension(	GifFileType *	gft,
			int		ExtCode,
			int		ExtLen,
			const void *	Extension )
{
    GifFilePrivateType *	gfpt= (GifFilePrivateType *)gft->Private;

    if (!IS_WRITEABLE(gfpt)) {
	    /* This file was NOT open for writing: */
	    _GifError = E_GIF_ERR_NOT_WRITEABLE;
	    return GIF_ERROR;
    }

    if  ( ExtCode == 0 )
	{
	sioOutPutCharacter( ExtLen, gfpt->gfptSos );
	}
    else{
	sioOutPutCharacter( '!', gfpt->gfptSos );
	sioOutPutCharacter( ExtCode, gfpt->gfptSos );
	sioOutPutCharacter( ExtLen, gfpt->gfptSos );
	}

    if  ( sioOutWriteBytes( gfpt->gfptSos, Extension, ExtLen ) != ExtLen )
	{ LDEB(ExtLen); return GIF_ERROR;	}

    sioOutPutCharacter( '\0', gfpt->gfptSos );

    return GIF_OK;
}

/************************************************************************/
/*									*/
/*  Close a gif file for writing.					*/
/*									*/
/************************************************************************/

int EGifCloseFile(	GifFileType *	gft )
{
    GifFilePrivateType *	gfpt= (GifFilePrivateType *)gft->Private;

    if (!IS_WRITEABLE(gfpt)) {
	/* This file was NOT open for writing: */
	_GifError = E_GIF_ERR_NOT_WRITEABLE;
	return GIF_ERROR;
    }

    sioOutPutCharacter( ';', gfpt->gfptSos );

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

    free( gft );

    return GIF_OK;
}