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