Imported Upstream version 5.1.9
[platform/upstream/giflib.git] / gifsponge.c
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 ****************************************************************************/
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <fcntl.h>
25
26 #include "getarg.h"
27 #include "gif_lib.h"
28
29 #define PROGRAM_NAME    "gifsponge"
30
31 int main(int argc, char **argv)
32 {
33     int i, ErrorCode;
34     GifFileType *GifFileIn, *GifFileOut = (GifFileType *)NULL;
35
36     if ((GifFileIn = DGifOpenFileHandle(0, &ErrorCode)) == NULL) {
37         PrintGifError(ErrorCode);
38         exit(EXIT_FAILURE);
39     }
40     if (DGifSlurp(GifFileIn) == GIF_ERROR) {
41         PrintGifError(GifFileIn->Error);
42         exit(EXIT_FAILURE);
43     }
44     if ((GifFileOut = EGifOpenFileHandle(1, &ErrorCode)) == NULL) {
45         PrintGifError(ErrorCode);
46         exit(EXIT_FAILURE);
47     }
48
49     /*
50      * Your operations on in-core structures go here.  
51      * This code just copies the header and each image from the incoming file.
52      */
53     GifFileOut->SWidth = GifFileIn->SWidth;
54     GifFileOut->SHeight = GifFileIn->SHeight;
55     GifFileOut->SColorResolution = GifFileIn->SColorResolution;
56     GifFileOut->SBackGroundColor = GifFileIn->SBackGroundColor;
57     if (GifFileIn->SColorMap) {
58         GifFileOut->SColorMap = GifMakeMapObject(
59                                    GifFileIn->SColorMap->ColorCount,
60                                    GifFileIn->SColorMap->Colors);
61     } else {
62         GifFileOut->SColorMap = NULL;
63     }
64
65     for (i = 0; i < GifFileIn->ImageCount; i++)
66         (void) GifMakeSavedImage(GifFileOut, &GifFileIn->SavedImages[i]);
67
68     /*
69      * Note: don't do DGifCloseFile early, as this will
70      * deallocate all the memory containing the GIF data!
71      *
72      * Further note: EGifSpew() doesn't try to validity-check any of this
73      * data; it's *your* responsibility to keep your changes consistent.
74      * Caveat hacker!
75      */
76     if (EGifSpew(GifFileOut) == GIF_ERROR)
77         PrintGifError(GifFileOut->Error);
78
79     if (DGifCloseFile(GifFileIn, &ErrorCode) == GIF_ERROR)
80         PrintGifError(ErrorCode);
81
82     return 0;
83 }
84
85 /* end */