1 /****************************************************************************
2 
3 gifsponge.c - skeleton file for generic GIF `sponge' program
4 
5 Slurp a GIF into core, operate on it, spew it out again.  Most of the
6 junk above `int main' isn't needed for the skeleton, but is likely to
7 be for what you'll do with it.
8 
9 If you compile this, it will turn into an expensive GIF copying routine;
10 stdin to stdout with no changes and minimal validation.  Well, it's a
11 decent test of DGifSlurp() and EGifSpew(), anyway.
12 
13 Note: due to the vicissitudes of Lempel-Ziv compression, the output of this
14 copier may not be bitwise identical to its input.  This can happen if you
15 copy an image from a much more (or much *less*) memory-limited system; your
16 compression may use more (or fewer) bits.  The uncompressed rasters should,
17 however, be identical (you can check this with gifbuild -d).
18 
19 SPDX-License-Identifier: MIT
20 
21 ****************************************************************************/
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <fcntl.h>
27 
28 #include "gif_lib.h"
29 #include "getarg.h"
30 
31 #define PROGRAM_NAME	"gifsponge"
32 
main(int argc,char ** argv)33 int main(int argc, char **argv)
34 {
35     int	i, ErrorCode;
36     GifFileType *GifFileIn, *GifFileOut = (GifFileType *)NULL;
37 
38     if ((GifFileIn = DGifOpenFileHandle(0, &ErrorCode)) == NULL) {
39 	PrintGifError(ErrorCode);
40 	exit(EXIT_FAILURE);
41     }
42     if (DGifSlurp(GifFileIn) == GIF_ERROR) {
43 	PrintGifError(GifFileIn->Error);
44 	exit(EXIT_FAILURE);
45     }
46     if ((GifFileOut = EGifOpenFileHandle(1, &ErrorCode)) == NULL) {
47 	PrintGifError(ErrorCode);
48 	exit(EXIT_FAILURE);
49     }
50 
51     /*
52      * Your operations on in-core structures go here.
53      * This code just copies the header and each image from the incoming file.
54      */
55     GifFileOut->SWidth = GifFileIn->SWidth;
56     GifFileOut->SHeight = GifFileIn->SHeight;
57     GifFileOut->SColorResolution = GifFileIn->SColorResolution;
58     GifFileOut->SBackGroundColor = GifFileIn->SBackGroundColor;
59     if (GifFileIn->SColorMap) {
60 	GifFileOut->SColorMap = GifMakeMapObject(
61 				   GifFileIn->SColorMap->ColorCount,
62 				   GifFileIn->SColorMap->Colors);
63     } else {
64 	GifFileOut->SColorMap = NULL;
65     }
66 
67     for (i = 0; i < GifFileIn->ImageCount; i++)
68 	(void) GifMakeSavedImage(GifFileOut, &GifFileIn->SavedImages[i]);
69 
70     /*
71      * Note: don't do DGifCloseFile early, as this will
72      * deallocate all the memory containing the GIF data!
73      *
74      * Further note: EGifSpew() doesn't try to validity-check any of this
75      * data; it's *your* responsibility to keep your changes consistent.
76      * Caveat hacker!
77      */
78     if (EGifSpew(GifFileOut) == GIF_ERROR)
79 	PrintGifError(GifFileOut->Error);
80 
81     if (DGifCloseFile(GifFileIn, &ErrorCode) == GIF_ERROR)
82 	PrintGifError(ErrorCode);
83 
84     return 0;
85 }
86 
87 /* end */
88