xref: /aosp_15_r20/external/giflib/gifsponge.c (revision 324bb76b8d05e2a05aa88511fff61cf3f9ca5892)
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 <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "getarg.h"
29 #include "gif_lib.h"
30 
31 #define PROGRAM_NAME "gifsponge"
32 
main(int argc,char ** argv)33 int main(int argc, char **argv) {
34 	int i, ErrorCode;
35 	GifFileType *GifFileIn, *GifFileOut = (GifFileType *)NULL;
36 
37 	if ((GifFileIn = DGifOpenFileHandle(0, &ErrorCode)) == NULL) {
38 		PrintGifError(ErrorCode);
39 		exit(EXIT_FAILURE);
40 	}
41 	if (DGifSlurp(GifFileIn) == GIF_ERROR) {
42 		PrintGifError(GifFileIn->Error);
43 		exit(EXIT_FAILURE);
44 	}
45 	if ((GifFileOut = EGifOpenFileHandle(1, &ErrorCode)) == NULL) {
46 		PrintGifError(ErrorCode);
47 		exit(EXIT_FAILURE);
48 	}
49 
50 	/*
51 	 * Your operations on in-core structures go here.
52 	 * This code just copies the header and each image from the incoming
53 	 * 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 =
61 		    GifMakeMapObject(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 	/*
72 	 * Note: don't do DGifCloseFile early, as this will
73 	 * deallocate all the memory containing the GIF data!
74 	 *
75 	 * Further note: EGifSpew() doesn't try to validity-check any of this
76 	 * data; it's *your* responsibility to keep your changes consistent.
77 	 * Caveat hacker!
78 	 */
79 	if (EGifSpew(GifFileOut) == GIF_ERROR) {
80 		PrintGifError(GifFileOut->Error);
81 	}
82 
83 	if (DGifCloseFile(GifFileIn, &ErrorCode) == GIF_ERROR) {
84 		PrintGifError(ErrorCode);
85 	}
86 
87 	return 0;
88 }
89 
90 /* end */
91