1 const char deanimate_rcs[] = "$Id: deanimate.c,v 1.1 2001/07/13 13:40:09 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 Andreas S. Oesterhelt
13 * for the SourceForge 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 $
41 **********************************************************************/
46 #include "deanimate.h"
50 const char deanimate_h_rcs[] = DEANIMATE_H_VERSION;
52 /*********************************************************************
56 * Description : Safely frees a struct binbuffer
59 * 1 : buf = Pointer to the binbuffer to be freed
63 *********************************************************************/
64 void buf_free(struct binbuffer *buf)
66 if (buf == NULL) return;
68 if (buf->buffer != NULL)
78 /*********************************************************************
82 * Description : Safely copies a given amount of bytes from one
83 * struct binbuffer to another, advancing the
84 * offsets appropriately.
87 * 1 : src = Pointer to the source binbuffer
88 * 2 : dst = Pointer to the destination binbuffer
89 * 3 : length = Number of bytes to be copied
91 * Returns : 0 on success, 1 on failiure.
93 *********************************************************************/
94 int buf_copy(struct binbuffer *src, struct binbuffer *dst, int length)
99 * Sanity check: Can't copy more data than we have
101 if (src->offset + length > src->size)
107 * If dst can't hold the new data, get mem first. (In chunks
108 * of 1000 bytes, so we don't have to realloc() too often)
110 if (dst->offset + length > dst->size)
112 dst->size = dst->size + length + 1000 - (dst->size + length) % 1000;
114 dst->buffer = (char *)realloc(dst->buffer, dst->size);
116 if (dst->buffer == NULL)
124 * Now that it's safe, memcpy() the desired amount of
125 * data from src to dst and adjust the offsets
127 memcpy(dst->buffer + dst->offset, src->buffer + src->offset, length);
128 src->offset += length;
129 dst->offset += length;
136 /*********************************************************************
138 * Function : buf_getbyte
140 * Description : Safely gets a byte from a given binbuffer at a
144 * 1 : buf = Pointer to the source binbuffer
145 * 2 : offset = Offset to the desired byte
147 * Returns : The byte on success, or 0 on failiure
149 *********************************************************************/
150 unsigned char buf_getbyte(struct binbuffer *src, int offset)
152 if (src->offset + offset < src->size)
154 return (unsigned char)*(src->buffer + src->offset + offset);
164 /*********************************************************************
166 * Function : gif_skip_data_block
168 * Description : Safely advances the offset of a given struct binbuffer
169 * that contains a GIF image and whose offset is
170 * positioned at the start of a data block behind
174 * 1 : buf = Pointer to the binbuffer
176 * Returns : 0 on success, or 1 on failiure
178 *********************************************************************/
179 int gif_skip_data_block(struct binbuffer *buf)
184 * Data blocks are sequences of chunks, which are headed
185 * by a one-byte length field, with the last chunk having
188 while(c = buf_getbyte(buf, 0))
190 if ((buf->offset += c + 1) >= buf->size - 1)
202 /*********************************************************************
204 * Function : gif_extract_image
206 * Description : Safely extracts an image data block from a given
207 * struct binbuffer that contains a GIF image and whose
208 * offset is positioned at the start of a data block
209 * into a given destination binbuffer.
212 * 1 : src = Pointer to the source binbuffer
213 * 2 : dst = Pointer to the destination binbuffer
215 * Returns : 0 on success, or 1 on failiure
217 *********************************************************************/
218 int gif_extract_image(struct binbuffer *src, struct binbuffer *dst)
223 * Remember the colormap flag and copy the image head
225 c = buf_getbyte(src, 9);
226 if (buf_copy(src, dst, 10))
232 * If the image has a local colormap, copy it.
236 if (buf_copy(src, dst, 3 * (1 << ((c & 0x07) + 1))))
241 if (buf_copy(src, dst, 1)) return 1;
244 * Copy the image chunk by chunk.
246 while(c = buf_getbyte(src, 0))
248 if (buf_copy(src, dst, c + 1)) return 1;
250 if (buf_copy(src, dst, 1)) return 1;
253 * Trim and rewind the dst buffer
255 dst->buffer = (char *)realloc(dst->buffer, dst->offset);
256 dst->size = dst->offset;
263 /*********************************************************************
265 * Function : gif_deanimate
267 * Description : Deanimate a given GIF image, i.e. given a GIF with
268 * an (optional) image block and an arbitrary number
269 * of image extension blocks, produce an output GIF with
270 * only one image block that contains the last image
271 * (extenstion) block of the original.
272 * Also strip Comments, Application extenstions, etc.
275 * 1 : src = Pointer to the source binbuffer
276 * 2 : dst = Pointer to the destination binbuffer
278 * Returns : 0 on success, or 1 on failiure
280 *********************************************************************/
281 int gif_deanimate(struct binbuffer *src, struct binbuffer *dst)
284 struct binbuffer *image;
286 if (NULL == src || NULL == dst)
291 c = buf_getbyte(src, 10);
294 * Check & copy GIF header
296 if (strncmp(src->buffer, "GIF89a", 6))
298 fprintf(stderr, "This is not a GIF98a!\n");
303 if (buf_copy(src, dst, 13))
310 * Look for global colormap and copy if found.
314 if (buf_copy(src, dst, 3 * (1 << ((c & 0x07) + 1))))
321 * Reserve a buffer for the current image block
323 if (NULL == (image = (struct binbuffer *)zalloc(sizeof(*image))))
329 * Parse the GIF block by block and copy the relevant
332 while(src->offset < src->size)
334 switch(buf_getbyte(src, 0))
337 * End-of-GIF Marker: Append current image and return
340 if (buf_copy(image, dst, image->size) || buf_copy(src, dst, 1))
348 * Image block: Extract to current image buffer
352 if (gif_extract_image(src, image))
359 * Extension block: Look at next byte and decide
362 switch (buf_getbyte(src, 1))
365 * Image extension: Copy extension header and image
366 * to the current image buffer
370 if (buf_copy(src, image, 8) || buf_getbyte(src, 0) != 0x2c) goto failed;
371 if (gif_extract_image(src, image)) goto failed;
375 * Application extension: Skip
378 if ((src->offset += 14) >= src->size || gif_skip_data_block(src)) goto failed;
382 * Comment extension: Skip
385 if ((src->offset += 2) >= src->size || gif_skip_data_block(src)) goto failed;
389 * Plain text extension: Skip
392 if ((src->offset += 15) >= src->size || gif_skip_data_block(src)) goto failed;
396 * Ooops, what type of extension is that?
404 * Ooops, what type of block is that?
410 } /* -END- while src */
413 * Either we got here by goto, or because the GIF is
414 * bogus and EOF was reached before an end-of-gif marker