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