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