Imported Upstream version 5.1.9 into tizen
[platform/upstream/giflib.git] / gifclrmp.c
1 /*****************************************************************************
2
3 gifclrmap - extract colormaps from GIF images
4
5 *****************************************************************************/
6
7 #include <math.h>
8 #include <stdio.h>
9 #include <ctype.h>
10 #include <string.h>
11 #include <stdbool.h>
12 #include <stdlib.h>
13 #include <assert.h>
14
15 #include "gif_lib.h"
16 #include "getarg.h"
17
18 #define PROGRAM_NAME    "gifclrmp"
19
20 __attribute__((__section__(".tizen.build-id")))
21 static char
22     VersionStr[] =
23         PROGRAM_NAME
24         VERSION_COOKIE
25         "       Gershon Elber,  "
26         __DATE__ ",   " __TIME__ "\n"
27         "(C) Copyright 1989 Gershon Elber.\n";
28 static char
29     *CtrlStr =
30         PROGRAM_NAME
31         " v%- s%- t%-TranslationFile!s l%-ColorMapFile!s g%-Gamma!F i%-Image#!d h%- GifFile!*s";
32
33 static bool
34     SaveFlag = false,
35     TranslateFlag = false,
36     LoadFlag = false,
37     GammaFlag = false;
38 static
39     double Gamma = 1.0;
40 static
41     FILE *ColorFile = NULL;
42     FILE *TranslateFile = NULL;
43 static
44     GifPixelType Translation[256];
45
46 static ColorMapObject *ModifyColorMap(ColorMapObject *ColorMap);
47 static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
48
49 /******************************************************************************
50  Interpret the command line and scan the given GIF file.
51 ******************************************************************************/
52 int main(int argc, char **argv)
53 {
54     int NumFiles, ExtCode, CodeSize, ImageNum = 0, 
55         ImageN, HasGIFOutput, ErrorCode;
56     bool Error, ImageNFlag = false, HelpFlag = false;
57     GifRecordType RecordType;
58     GifByteType *Extension, *CodeBlock;
59     char **FileName = NULL, *ColorFileName, *TranslateFileName;
60     GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
61
62     if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifNoisyPrint, &SaveFlag, 
63                 &TranslateFlag, &TranslateFileName,
64                 &LoadFlag, &ColorFileName,
65                 &GammaFlag, &Gamma, &ImageNFlag, &ImageN,
66                 &HelpFlag, &NumFiles, &FileName)) != false ||
67                 (NumFiles > 1 && !HelpFlag)) {
68         if (Error)
69             GAPrintErrMsg(Error);
70         else if (NumFiles > 1)
71             GIF_MESSAGE("Error in command line parsing - one GIF file please.");
72         GAPrintHowTo(CtrlStr);
73         exit(EXIT_FAILURE);
74     }
75
76     if (HelpFlag) {
77         (void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR);
78         GAPrintHowTo(CtrlStr);
79         exit(EXIT_SUCCESS);
80     }
81
82     if (SaveFlag + LoadFlag + GammaFlag + TranslateFlag > 1)
83         GIF_EXIT("Can not handle more than one of -s -l, -t, or -g at the same time.");
84
85     /* Default action is to dump colormaps */
86     if (!SaveFlag && !LoadFlag && !GammaFlag && !TranslateFlag)
87         SaveFlag = true;
88
89     if (NumFiles == 1) {
90         if ((GifFileIn = DGifOpenFileName(*FileName, &ErrorCode)) == NULL) {
91             PrintGifError(ErrorCode);
92             exit(EXIT_FAILURE);
93         }
94     }
95     else {
96         /* Use stdin instead: */
97         if ((GifFileIn = DGifOpenFileHandle(0, &ErrorCode)) == NULL) {
98             PrintGifError(ErrorCode);
99             exit(EXIT_FAILURE);
100         }
101     }
102
103     if (SaveFlag) {
104         /* We are dumping out the color map as text file to stdout: */
105         ColorFile = stdout;
106     }
107     else {
108         if (TranslateFlag) {
109             /* We are loading new color map from specified file: */
110             if ((TranslateFile = fopen(TranslateFileName, "rt")) == NULL)
111                 GIF_EXIT("Failed to open specified color translation file.");
112         }
113
114         if (LoadFlag) {
115             /* We are loading new color map from specified file: */
116             if ((ColorFile = fopen(ColorFileName, "rt")) == NULL)
117                 GIF_EXIT("Failed to open specified color map file.");
118         }
119     }
120
121     if ((HasGIFOutput = (LoadFlag || TranslateFlag || GammaFlag)) != 0) {
122         /* Open stdout for GIF output file: */
123         if ((GifFileOut = EGifOpenFileHandle(1, &ErrorCode)) == NULL) {
124             PrintGifError(ErrorCode);
125             exit(EXIT_FAILURE);
126         }
127     }
128
129     if (!ImageNFlag) {
130         /* We are supposed to modify the screen color map, so do it: */
131         if (!GifFileIn->SColorMap)
132             GIF_EXIT("No colormap to modify");
133         GifFileIn->SColorMap = ModifyColorMap(GifFileIn->SColorMap);
134         if (!HasGIFOutput) {
135             /* We can quit here, as we have the color map: */
136             DGifCloseFile(GifFileIn, NULL);
137             fclose(ColorFile);
138             exit(EXIT_SUCCESS);
139         }
140     }
141     /* And dump out its new possible repositioned screen information: */
142     if (HasGIFOutput)
143         if (EGifPutScreenDesc(GifFileOut,
144             GifFileIn->SWidth, GifFileIn->SHeight,
145             GifFileIn->SColorResolution, GifFileIn->SBackGroundColor,
146             GifFileIn->SColorMap) == GIF_ERROR)
147             QuitGifError(GifFileIn, GifFileOut);
148
149     /* Scan the content of the GIF file and load the image(s) in: */
150     do {
151         if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
152             QuitGifError(GifFileIn, GifFileOut);
153
154         switch (RecordType) {
155             case IMAGE_DESC_RECORD_TYPE:
156                 if (DGifGetImageDesc(GifFileIn) == GIF_ERROR)
157                     QuitGifError(GifFileIn, GifFileOut);
158                 if ((++ImageNum == ImageN) && ImageNFlag) {
159                     /* We are suppose to modify this image color map, do it: */
160                     GifFileIn->SColorMap =ModifyColorMap(GifFileIn->SColorMap);
161                     if (!HasGIFOutput) {
162                         /* We can quit here, as we have the color map: */
163                         DGifCloseFile(GifFileIn, NULL);
164                         fclose(ColorFile);
165                         exit(EXIT_SUCCESS);
166                     }
167                 }
168                 if (HasGIFOutput)
169                     if (EGifPutImageDesc(GifFileOut,
170                         GifFileIn->Image.Left, GifFileIn->Image.Top,
171                         GifFileIn->Image.Width, GifFileIn->Image.Height,
172                         GifFileIn->Image.Interlace,
173                         GifFileIn->Image.ColorMap) == GIF_ERROR)
174                         QuitGifError(GifFileIn, GifFileOut);
175
176                 if (!TranslateFlag || (ImageNFlag && (ImageN != ImageNum)))
177                 {
178                     /* Now read image itself in decoded form as we don't */
179                     /* really care what we have there, and this is much  */
180                     /* faster.                                           */
181                     if (DGifGetCode(GifFileIn, &CodeSize, &CodeBlock) == GIF_ERROR)
182                         QuitGifError(GifFileIn, GifFileOut);
183                     if (HasGIFOutput)
184                         if (EGifPutCode(GifFileOut, CodeSize, CodeBlock) == GIF_ERROR)
185                             QuitGifError(GifFileIn, GifFileOut);
186                     while (CodeBlock != NULL) {
187                         if (DGifGetCodeNext(GifFileIn, &CodeBlock) == GIF_ERROR)
188                             QuitGifError(GifFileIn, GifFileOut);
189                         if (HasGIFOutput)
190                             if (EGifPutCodeNext(GifFileOut, CodeBlock) == GIF_ERROR)
191                                 QuitGifError(GifFileIn, GifFileOut);
192                     }
193                 }
194                 else    /* we need to mung pixels intices */
195                 {
196                     int i;
197                     register GifPixelType *cp;
198
199                     GifPixelType *Line
200                         = (GifPixelType *) malloc(GifFileIn->Image.Width *
201                                                   sizeof(GifPixelType));
202                     for (i = 0; i < GifFileIn->Image.Height; i++) {
203                         if (DGifGetLine(GifFileIn, Line,GifFileIn->Image.Width)
204                             == GIF_ERROR) {
205                             QuitGifError(GifFileIn, GifFileOut);
206                         }
207
208                         /* translation step goes here */
209                         for (cp = Line; cp < Line+GifFileIn->Image.Width; cp++)
210                             *cp = Translation[*cp];
211
212                         if (EGifPutLine(GifFileOut,
213                                         Line, GifFileIn->Image.Width)
214                             == GIF_ERROR) {
215                             QuitGifError(GifFileIn, GifFileOut);
216                         }
217                     }
218                     free((char *) Line);
219                 }
220                 break;
221             case EXTENSION_RECORD_TYPE:
222                 assert(GifFileOut != NULL);     /* might pacify Coverity */
223                 /* pass through extension records */
224                 if (DGifGetExtension(GifFileIn, &ExtCode, &Extension) == GIF_ERROR)
225                     QuitGifError(GifFileIn, GifFileOut);
226                 if (Extension == NULL)
227                     break;
228                 if (EGifPutExtensionLeader(GifFileOut, ExtCode) == GIF_ERROR)
229                     QuitGifError(GifFileIn, GifFileOut);
230                 if (EGifPutExtensionBlock(GifFileOut, 
231                                           Extension[0],
232                                           Extension + 1) == GIF_ERROR)
233                     QuitGifError(GifFileIn, GifFileOut);
234                 while (Extension != NULL) {
235                     if (DGifGetExtensionNext(GifFileIn, &Extension)==GIF_ERROR)
236                         QuitGifError(GifFileIn, GifFileOut);
237                     if (Extension != NULL)
238                         if (EGifPutExtensionBlock(GifFileOut, 
239                                                   Extension[0],
240                                                   Extension + 1) == GIF_ERROR)
241                             QuitGifError(GifFileIn, GifFileOut);
242                 }
243                 if (EGifPutExtensionTrailer(GifFileOut) == GIF_ERROR)
244                     QuitGifError(GifFileIn, GifFileOut);
245                 break;
246             case TERMINATE_RECORD_TYPE:
247                 break;
248             default:                /* Should be trapped by DGifGetRecordType. */
249                 break;
250         }
251     }
252     while (RecordType != TERMINATE_RECORD_TYPE);
253
254     if (DGifCloseFile(GifFileIn, &ErrorCode) == GIF_ERROR)
255     {
256         PrintGifError(ErrorCode);
257         exit(EXIT_FAILURE);
258     }
259     if (HasGIFOutput)
260         if (EGifCloseFile(GifFileOut, &ErrorCode) == GIF_ERROR)
261         {
262             PrintGifError(ErrorCode);
263             exit(EXIT_FAILURE);
264         }
265
266     return 0;
267 }
268
269 /******************************************************************************
270  Modify the given colormap according to global variables setting.
271 ******************************************************************************/
272 static ColorMapObject *ModifyColorMap(ColorMapObject *ColorMap)
273 {
274     int i, Dummy, Red, Green, Blue;
275
276     if (SaveFlag) {
277         /* Save this color map to ColorFile: */
278         for (i = 0; i < ColorMap->ColorCount; i++)
279             fprintf(ColorFile, "%3d %3d %3d %3d\n", i,
280                     ColorMap->Colors[i].Red,
281                     ColorMap->Colors[i].Green,
282                     ColorMap->Colors[i].Blue);
283         return(ColorMap);
284     }
285     else if (LoadFlag) {
286         /* Read the color map in ColorFile into this color map: */
287         for (i = 0; i < ColorMap->ColorCount; i++) {
288             if (feof(ColorFile))
289                 GIF_EXIT("Color file to load color map from, too small.");
290             if (fscanf(ColorFile, "%3d %3d %3d %3d\n", &Dummy, &Red, &Green, &Blue) == 4) {
291                 ColorMap->Colors[i].Red = Red;
292                 ColorMap->Colors[i].Green = Green;
293                 ColorMap->Colors[i].Blue = Blue;
294             }
295         }
296         return(ColorMap);
297     }
298     else if (GammaFlag) {
299         /* Apply gamma correction to this color map: */
300         double Gamma1 = 1.0 / Gamma;
301         for (i = 0; i < ColorMap->ColorCount; i++) {
302             ColorMap->Colors[i].Red =
303                 ((int) (255 * pow(ColorMap->Colors[i].Red / 255.0, Gamma1)));
304             ColorMap->Colors[i].Green =
305                 ((int) (255 * pow(ColorMap->Colors[i].Green / 255.0, Gamma1)));
306             ColorMap->Colors[i].Blue =
307                 ((int) (255 * pow(ColorMap->Colors[i].Blue / 255.0, Gamma1)));
308         }
309         return(ColorMap);
310     }
311     else if (TranslateFlag) {
312         ColorMapObject *NewMap;
313         int Max = 0;
314
315         /* Read the translation table in TranslateFile: */
316         for (i = 0; i < ColorMap->ColorCount; i++) {
317             int tmp;
318             if (feof(TranslateFile))
319                 GIF_EXIT("Color file to load color map from, too small.");
320             if (fscanf(TranslateFile, "%3d %3d\n", &Dummy, &tmp) == 2) {
321                 Translation[i] = tmp & 0xff;
322                 if (Translation[i] > Max)
323                     Max = Translation[i];
324             }
325         }
326
327         if ((NewMap = GifMakeMapObject(1 << GifBitSize(Max+1), NULL)) == NULL)
328             GIF_EXIT("Out of memory while allocating color map!");
329
330         /* Apply the translation; we'll do it to the pixels, too */
331         for (i = 0; i < ColorMap->ColorCount; i++) {
332             NewMap->Colors[i] = ColorMap->Colors[Translation[i]];
333         }
334         
335         return(NewMap);
336     }
337     else
338     {
339         GIF_EXIT("Nothing to do!");
340         return(ColorMap);
341     }
342 }
343
344 /******************************************************************************
345  Close both input and output file (if open), and exit.
346 ******************************************************************************/
347 static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
348 {
349     if (GifFileIn != NULL) {
350         PrintGifError(GifFileIn->Error);
351         EGifCloseFile(GifFileIn, NULL);
352     }
353     if (GifFileOut != NULL) {
354         PrintGifError(GifFileOut->Error);
355         EGifCloseFile(GifFileOut, NULL);
356     }
357     exit(EXIT_FAILURE);
358 }
359
360 /* end */