1 const char deanimate_rcs[] = "$Id: deanimate.c,v 1.4 2001/07/18 12:28:49 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/deanimate.c,v $
6 * Purpose : Declares functions to deanimate GIF images on the fly.
8 * Functions declared include: gif_deanimate, buf_free,
9 * buf_copy, buf_getbyte, gif_skip_data_block, and
12 * Copyright : Written by and Copyright (C) 2001 by the the SourceForge
13 * IJBSWA team. http://ijbswa.sourceforge.net
15 * Based on the GIF file format specification (see
16 * http://tronche.com/computer-graphics/gif/gif89a.html)
17 * and ideas from the Image::DeAnim Perl module by
18 * Ken MacFarlane, <ksm+cpan@universal.dca.net>
20 * This program is free software; you can redistribute it
21 * and/or modify it under the terms of the GNU General
22 * Public License as published by the Free Software
23 * Foundation; either version 2 of the License, or (at
24 * your option) any later version.
26 * This program is distributed in the hope that it will
27 * be useful, but WITHOUT ANY WARRANTY; without even the
28 * implied warranty of MERCHANTABILITY or FITNESS FOR A
29 * PARTICULAR PURPOSE. See the GNU General Public
30 * License for more details.
32 * The GNU General Public License should be included with
33 * this file. If not, you can view it at
34 * http://www.gnu.org/copyleft/gpl.html
35 * or write to the Free Software Foundation, Inc., 59
36 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39 * $Log: deanimate.c,v $
40 * Revision 1.4 2001/07/18 12:28:49 oes
41 * - Added feature for extracting the first frame
43 * - Separated image buffer extension into buf_extend
44 * - Extended gif deanimation to GIF87a (untested!)
47 * Revision 1.3 2001/07/15 13:57:50 jongfoster
48 * Adding #includes string.h and miscutil.h
50 * Revision 1.2 2001/07/13 13:46:20 oes
51 * Introduced GIF deanimation feature
54 **********************************************************************/
63 #include "deanimate.h"
66 const char deanimate_h_rcs[] = DEANIMATE_H_VERSION;
68 /*********************************************************************
72 * Description : Safely frees a struct binbuffer
75 * 1 : buf = Pointer to the binbuffer to be freed
79 *********************************************************************/
80 void buf_free(struct binbuffer *buf)
82 if (buf == NULL) return;
84 if (buf->buffer != NULL)
94 /*********************************************************************
96 * Function : buf_extend
98 * Description : Ensure that a given binbuffer can hold a given amount
99 * of bytes, by reallocating its buffer if necessary.
100 * Allocate new mem in chunks of 1000 bytes, so we don't
101 * have to realloc() too often.
104 * 1 : buf = Pointer to the binbuffer
105 * 2 : length = Desired minimum size
108 * Returns : 0 on success, 1 on failiure.
110 *********************************************************************/
111 int buf_extend(struct binbuffer *buf, int length)
115 if (buf->offset + length > buf->size)
117 buf->size = buf->size + length + 1000 - (buf->size + length) % 1000;
118 newbuf = (char *)realloc(buf->buffer, buf->size);
127 buf->buffer = newbuf;
136 /*********************************************************************
138 * Function : buf_copy
140 * Description : Safely copies a given amount of bytes from one
141 * struct binbuffer to another, advancing the
142 * offsets appropriately.
145 * 1 : src = Pointer to the source binbuffer
146 * 2 : dst = Pointer to the destination binbuffer
147 * 3 : length = Number of bytes to be copied
149 * Returns : 0 on success, 1 on failiure.
151 *********************************************************************/
152 int buf_copy(struct binbuffer *src, struct binbuffer *dst, int length)
156 * Sanity check: Can't copy more data than we have
158 if (src->offset + length > src->size)
164 * Ensure that dst can hold the new data
166 if (buf_extend(dst, length))
172 * Now that it's safe, memcpy() the desired amount of
173 * data from src to dst and adjust the offsets
175 memcpy(dst->buffer + dst->offset, src->buffer + src->offset, length);
176 src->offset += length;
177 dst->offset += length;
184 /*********************************************************************
186 * Function : buf_getbyte
188 * Description : Safely gets a byte from a given binbuffer at a
192 * 1 : buf = Pointer to the source binbuffer
193 * 2 : offset = Offset to the desired byte
195 * Returns : The byte on success, or 0 on failiure
197 *********************************************************************/
198 unsigned char buf_getbyte(struct binbuffer *src, int offset)
200 if (src->offset + offset < src->size)
202 return (unsigned char)*(src->buffer + src->offset + offset);
212 /*********************************************************************
214 * Function : gif_skip_data_block
216 * Description : Safely advances the offset of a given struct binbuffer
217 * that contains a GIF image and whose offset is
218 * positioned at the start of a data block, behind
222 * 1 : buf = Pointer to the binbuffer
224 * Returns : 0 on success, or 1 on failiure
226 *********************************************************************/
227 int gif_skip_data_block(struct binbuffer *buf)
232 * Data blocks are sequences of chunks, which are headed
233 * by a one-byte length field, with the last chunk having
236 while((c = buf_getbyte(buf, 0)))
238 if ((buf->offset += c + 1) >= buf->size - 1)
250 /*********************************************************************
252 * Function : gif_extract_image
254 * Description : Safely extracts an image data block from a given
255 * struct binbuffer that contains a GIF image and whose
256 * offset is positioned at the start of a data block
257 * into a given destination binbuffer.
260 * 1 : src = Pointer to the source binbuffer
261 * 2 : dst = Pointer to the destination binbuffer
263 * Returns : 0 on success, or 1 on failiure
265 *********************************************************************/
266 int gif_extract_image(struct binbuffer *src, struct binbuffer *dst)
271 * Remember the colormap flag and copy the image head
273 c = buf_getbyte(src, 9);
274 if (buf_copy(src, dst, 10))
280 * If the image has a local colormap, copy it.
284 if (buf_copy(src, dst, 3 * (1 << ((c & 0x07) + 1))))
289 if (buf_copy(src, dst, 1)) return 1;
292 * Copy the image chunk by chunk.
294 while((c = buf_getbyte(src, 0)))
296 if (buf_copy(src, dst, c + 1)) return 1;
298 if (buf_copy(src, dst, 1)) return 1;
301 * Trim and rewind the dst buffer
303 if (NULL == (dst->buffer = (char *)realloc(dst->buffer, dst->offset))) return 1;
304 dst->size = dst->offset;
311 /*********************************************************************
313 * Function : gif_deanimate
315 * Description : Deanimate a given GIF image, i.e. given a GIF with
316 * an (optional) image block and an arbitrary number
317 * of image extension blocks, produce an output GIF with
318 * only one image block that contains the last image
319 * (extenstion) block of the original.
320 * Also strip Comments, Application extenstions, etc.
323 * 1 : src = Pointer to the source binbuffer
324 * 2 : dst = Pointer to the destination binbuffer
325 * 3 : get_first_image = Flag: If set, get the first image
326 * If unset (default), get the last
328 * Returns : 0 on success, or 1 on failiure
330 *********************************************************************/
331 int gif_deanimate(struct binbuffer *src, struct binbuffer *dst, int get_first_image)
334 struct binbuffer *image;
336 if (NULL == src || NULL == dst)
341 c = buf_getbyte(src, 10);
344 * Check & copy GIF header
346 if (strncmp(src->buffer, "GIF89a", 6) && strncmp(src->buffer, "GIF87a", 6))
352 if (buf_copy(src, dst, 13))
359 * Look for global colormap and copy if found.
363 if (buf_copy(src, dst, 3 * (1 << ((c & 0x07) + 1))))
370 * Reserve a buffer for the current image block
372 if (NULL == (image = (struct binbuffer *)zalloc(sizeof(*image))))
378 * Parse the GIF block by block and copy the relevant
381 while(src->offset < src->size)
383 switch(buf_getbyte(src, 0))
386 * End-of-GIF Marker: Append current image and return
392 * Image block: Extract to current image buffer.
396 if (gif_extract_image(src, image)) goto failed;
397 if (get_first_image) goto write;
401 * Extension block: Look at next byte and decide
404 switch (buf_getbyte(src, 1))
407 * Image extension: Copy extension header and image
408 * to the current image buffer
412 if (buf_copy(src, image, 8) || buf_getbyte(src, 0) != 0x2c) goto failed;
413 if (gif_extract_image(src, image)) goto failed;
414 if (get_first_image) goto write;
418 * Application extension: Skip
421 if ((src->offset += 14) >= src->size || gif_skip_data_block(src)) goto failed;
425 * Comment extension: Skip
428 if ((src->offset += 2) >= src->size || gif_skip_data_block(src)) goto failed;
432 * Plain text extension: Skip
435 if ((src->offset += 15) >= src->size || gif_skip_data_block(src)) goto failed;
439 * Ooops, what type of extension is that?
447 * Ooops, what type of block is that?
453 } /* -END- while src */
456 * Either we got here by goto, or because the GIF is
457 * bogus and EOF was reached before an end-of-gif marker
466 * Append the current image to dst and return
470 if (buf_copy(image, dst, image->size)) goto failed;
471 if (buf_extend(dst, 1)) goto failed;
472 *(dst->buffer + dst->offset++) = 0x3b;