16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 */
24
25 /*****************************************************************************
26
27 GIF construction tools
28
29 ****************************************************************************/
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 #include "gif_lib.h"
36
37 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
38
39 /******************************************************************************
40 Miscellaneous utility functions
41 ******************************************************************************/
42
43 /* return smallest bitfield size n will fit in */
44 int
45 GifBitSize(int n)
46 {
47 register int i;
48
49 for (i = 1; i <= 8; i++)
50 if ((1 << i) >= n)
51 break;
52 return (i);
53 }
54
55 /******************************************************************************
331
332 /* Deallocate any extensions */
333 GifFreeExtensions(&sp->ExtensionBlockCount, &sp->ExtensionBlocks);
334
335 /*** FIXME: We could realloc the GifFile->SavedImages structure but is
336 * there a point to it? Saves some memory but we'd have to do it every
337 * time. If this is used in GifFreeSavedImages then it would be inefficient
338 * (The whole array is going to be deallocated.) If we just use it when
339 * we want to free the last Image it's convenient to do it here.
340 */
341 }
342
343 /*
344 * Append an image block to the SavedImages array
345 */
346 SavedImage *
347 GifMakeSavedImage(GifFileType *GifFile, const SavedImage *CopyFrom)
348 {
349 if (GifFile->SavedImages == NULL)
350 GifFile->SavedImages = (SavedImage *)malloc(sizeof(SavedImage));
351 else
352 GifFile->SavedImages = (SavedImage *)reallocarray(GifFile->SavedImages,
353 (GifFile->ImageCount + 1), sizeof(SavedImage));
354
355 if (GifFile->SavedImages == NULL)
356 return ((SavedImage *)NULL);
357 else {
358 SavedImage *sp = &GifFile->SavedImages[GifFile->ImageCount++];
359 memset((char *)sp, '\0', sizeof(SavedImage));
360
361 if (CopyFrom != NULL) {
362 memcpy((char *)sp, CopyFrom, sizeof(SavedImage));
363
364 /*
365 * Make our own allocated copies of the heap fields in the
366 * copied record. This guards against potential aliasing
367 * problems.
368 */
369
370 /* first, the local color map */
371 if (sp->ImageDesc.ColorMap != NULL) {
372 sp->ImageDesc.ColorMap = GifMakeMapObject(
373 CopyFrom->ImageDesc.ColorMap->ColorCount,
374 CopyFrom->ImageDesc.ColorMap->Colors);
375 if (sp->ImageDesc.ColorMap == NULL) {
376 FreeLastSavedImage(GifFile);
377 return (SavedImage *)(NULL);
378 }
379 }
380
381 /* next, the raster */
382 sp->RasterBits = (unsigned char *)reallocarray(NULL,
383 (CopyFrom->ImageDesc.Height *
384 CopyFrom->ImageDesc.Width),
385 sizeof(GifPixelType));
386 if (sp->RasterBits == NULL) {
387 FreeLastSavedImage(GifFile);
388 return (SavedImage *)(NULL);
389 }
390 memcpy(sp->RasterBits, CopyFrom->RasterBits,
391 sizeof(GifPixelType) * CopyFrom->ImageDesc.Height *
392 CopyFrom->ImageDesc.Width);
393
394 /* finally, the extension blocks */
395 if (sp->ExtensionBlocks != NULL) {
396 sp->ExtensionBlocks = (ExtensionBlock *)reallocarray(NULL,
397 CopyFrom->ExtensionBlockCount,
398 sizeof(ExtensionBlock));
399 if (sp->ExtensionBlocks == NULL) {
400 FreeLastSavedImage(GifFile);
401 return (SavedImage *)(NULL);
402 }
403 memcpy(sp->ExtensionBlocks, CopyFrom->ExtensionBlocks,
404 sizeof(ExtensionBlock) * CopyFrom->ExtensionBlockCount);
405 }
406 }
407
408 return (sp);
409 }
410 }
411
412 void
413 GifFreeSavedImages(GifFileType *GifFile)
414 {
415 SavedImage *sp;
416
417 if ((GifFile == NULL) || (GifFile->SavedImages == NULL)) {
418 return;
419 }
420 for (sp = GifFile->SavedImages;
421 sp < GifFile->SavedImages + GifFile->ImageCount; sp++) {
422 if (sp->ImageDesc.ColorMap != NULL) {
423 GifFreeMapObject(sp->ImageDesc.ColorMap);
424 sp->ImageDesc.ColorMap = NULL;
425 }
426
|
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 */
24
25 /*****************************************************************************
26
27 GIF construction tools
28
29 ****************************************************************************/
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 #include "gif_lib.h"
36 #include "gif_lib_private.h"
37
38 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
39
40 /******************************************************************************
41 Miscellaneous utility functions
42 ******************************************************************************/
43
44 /* return smallest bitfield size n will fit in */
45 int
46 GifBitSize(int n)
47 {
48 register int i;
49
50 for (i = 1; i <= 8; i++)
51 if ((1 << i) >= n)
52 break;
53 return (i);
54 }
55
56 /******************************************************************************
332
333 /* Deallocate any extensions */
334 GifFreeExtensions(&sp->ExtensionBlockCount, &sp->ExtensionBlocks);
335
336 /*** FIXME: We could realloc the GifFile->SavedImages structure but is
337 * there a point to it? Saves some memory but we'd have to do it every
338 * time. If this is used in GifFreeSavedImages then it would be inefficient
339 * (The whole array is going to be deallocated.) If we just use it when
340 * we want to free the last Image it's convenient to do it here.
341 */
342 }
343
344 /*
345 * Append an image block to the SavedImages array
346 */
347 SavedImage *
348 GifMakeSavedImage(GifFileType *GifFile, const SavedImage *CopyFrom)
349 {
350 if (GifFile->SavedImages == NULL)
351 GifFile->SavedImages = (SavedImage *)malloc(sizeof(SavedImage));
352 else {
353 SavedImage* newSavedImages = (SavedImage *)reallocarray(GifFile->SavedImages,
354 (GifFile->ImageCount + 1), sizeof(SavedImage));
355 if( newSavedImages == NULL)
356 return ((SavedImage *)NULL);
357 GifFile->SavedImages = newSavedImages;
358 }
359 if (GifFile->SavedImages == NULL)
360 return ((SavedImage *)NULL);
361 else {
362 SavedImage *sp = &GifFile->SavedImages[GifFile->ImageCount++];
363
364 if (CopyFrom != NULL) {
365 memcpy((char *)sp, CopyFrom, sizeof(SavedImage));
366
367 /*
368 * Make our own allocated copies of the heap fields in the
369 * copied record. This guards against potential aliasing
370 * problems.
371 */
372
373 /* first, the local color map */
374 if (CopyFrom->ImageDesc.ColorMap != NULL) {
375 sp->ImageDesc.ColorMap = GifMakeMapObject(
376 CopyFrom->ImageDesc.ColorMap->ColorCount,
377 CopyFrom->ImageDesc.ColorMap->Colors);
378 if (sp->ImageDesc.ColorMap == NULL) {
379 FreeLastSavedImage(GifFile);
380 return (SavedImage *)(NULL);
381 }
382 }
383
384 /* next, the raster */
385 sp->RasterBits = (unsigned char *)reallocarray(NULL,
386 (CopyFrom->ImageDesc.Height *
387 CopyFrom->ImageDesc.Width),
388 sizeof(GifPixelType));
389 if (sp->RasterBits == NULL) {
390 FreeLastSavedImage(GifFile);
391 return (SavedImage *)(NULL);
392 }
393 memcpy(sp->RasterBits, CopyFrom->RasterBits,
394 sizeof(GifPixelType) * CopyFrom->ImageDesc.Height *
395 CopyFrom->ImageDesc.Width);
396
397 /* finally, the extension blocks */
398 if (CopyFrom->ExtensionBlocks != NULL) {
399 sp->ExtensionBlocks = (ExtensionBlock *)reallocarray(NULL,
400 CopyFrom->ExtensionBlockCount,
401 sizeof(ExtensionBlock));
402 if (sp->ExtensionBlocks == NULL) {
403 FreeLastSavedImage(GifFile);
404 return (SavedImage *)(NULL);
405 }
406 memcpy(sp->ExtensionBlocks, CopyFrom->ExtensionBlocks,
407 sizeof(ExtensionBlock) * CopyFrom->ExtensionBlockCount);
408 }
409 }
410 else {
411 memset((char *)sp, '\0', sizeof(SavedImage));
412 }
413
414 return (sp);
415 }
416 }
417
418 void
419 GifFreeSavedImages(GifFileType *GifFile)
420 {
421 SavedImage *sp;
422
423 if ((GifFile == NULL) || (GifFile->SavedImages == NULL)) {
424 return;
425 }
426 for (sp = GifFile->SavedImages;
427 sp < GifFile->SavedImages + GifFile->ImageCount; sp++) {
428 if (sp->ImageDesc.ColorMap != NULL) {
429 GifFreeMapObject(sp->ImageDesc.ColorMap);
430 sp->ImageDesc.ColorMap = NULL;
431 }
432
|