1 /*****************************************************************************
3 gifclrmap - extract colormaps from GIF images
5 *****************************************************************************/
18 #define PROGRAM_NAME "gifclrmp"
20 __attribute__((__section__(".tizen.build-id")))
26 __DATE__ ", " __TIME__ "\n"
27 "(C) Copyright 1989 Gershon Elber.\n";
31 " v%- s%- t%-TranslationFile!s l%-ColorMapFile!s g%-Gamma!F i%-Image#!d h%- GifFile!*s";
35 TranslateFlag = false,
41 FILE *ColorFile = NULL;
42 FILE *TranslateFile = NULL;
44 GifPixelType Translation[256];
46 static ColorMapObject *ModifyColorMap(ColorMapObject *ColorMap);
47 static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
49 /******************************************************************************
50 Interpret the command line and scan the given GIF file.
51 ******************************************************************************/
52 int main(int argc, char **argv)
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;
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)) {
70 else if (NumFiles > 1)
71 GIF_MESSAGE("Error in command line parsing - one GIF file please.");
72 GAPrintHowTo(CtrlStr);
77 (void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR);
78 GAPrintHowTo(CtrlStr);
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.");
85 /* Default action is to dump colormaps */
86 if (!SaveFlag && !LoadFlag && !GammaFlag && !TranslateFlag)
90 if ((GifFileIn = DGifOpenFileName(*FileName, &ErrorCode)) == NULL) {
91 PrintGifError(ErrorCode);
96 /* Use stdin instead: */
97 if ((GifFileIn = DGifOpenFileHandle(0, &ErrorCode)) == NULL) {
98 PrintGifError(ErrorCode);
104 /* We are dumping out the color map as text file to stdout: */
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.");
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.");
121 if ((HasGIFOutput = (LoadFlag || TranslateFlag || GammaFlag)) != 0) {
122 /* Open stdout for GIF output file: */
123 if ((GifFileOut = EGifOpenFileHandle(1, &ErrorCode)) == NULL) {
124 PrintGifError(ErrorCode);
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);
135 /* We can quit here, as we have the color map: */
136 DGifCloseFile(GifFileIn, NULL);
141 /* And dump out its new possible repositioned screen information: */
143 if (EGifPutScreenDesc(GifFileOut,
144 GifFileIn->SWidth, GifFileIn->SHeight,
145 GifFileIn->SColorResolution, GifFileIn->SBackGroundColor,
146 GifFileIn->SColorMap) == GIF_ERROR)
147 QuitGifError(GifFileIn, GifFileOut);
149 /* Scan the content of the GIF file and load the image(s) in: */
151 if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
152 QuitGifError(GifFileIn, GifFileOut);
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);
162 /* We can quit here, as we have the color map: */
163 DGifCloseFile(GifFileIn, NULL);
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);
176 if (!TranslateFlag || (ImageNFlag && (ImageN != ImageNum)))
178 /* Now read image itself in decoded form as we don't */
179 /* really care what we have there, and this is much */
181 if (DGifGetCode(GifFileIn, &CodeSize, &CodeBlock) == GIF_ERROR)
182 QuitGifError(GifFileIn, GifFileOut);
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);
190 if (EGifPutCodeNext(GifFileOut, CodeBlock) == GIF_ERROR)
191 QuitGifError(GifFileIn, GifFileOut);
194 else /* we need to mung pixels intices */
197 register GifPixelType *cp;
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)
205 QuitGifError(GifFileIn, GifFileOut);
208 /* translation step goes here */
209 for (cp = Line; cp < Line+GifFileIn->Image.Width; cp++)
210 *cp = Translation[*cp];
212 if (EGifPutLine(GifFileOut,
213 Line, GifFileIn->Image.Width)
215 QuitGifError(GifFileIn, GifFileOut);
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)
228 if (EGifPutExtensionLeader(GifFileOut, ExtCode) == GIF_ERROR)
229 QuitGifError(GifFileIn, GifFileOut);
230 if (EGifPutExtensionBlock(GifFileOut,
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,
240 Extension + 1) == GIF_ERROR)
241 QuitGifError(GifFileIn, GifFileOut);
243 if (EGifPutExtensionTrailer(GifFileOut) == GIF_ERROR)
244 QuitGifError(GifFileIn, GifFileOut);
246 case TERMINATE_RECORD_TYPE:
248 default: /* Should be trapped by DGifGetRecordType. */
252 while (RecordType != TERMINATE_RECORD_TYPE);
254 if (DGifCloseFile(GifFileIn, &ErrorCode) == GIF_ERROR)
256 PrintGifError(ErrorCode);
260 if (EGifCloseFile(GifFileOut, &ErrorCode) == GIF_ERROR)
262 PrintGifError(ErrorCode);
269 /******************************************************************************
270 Modify the given colormap according to global variables setting.
271 ******************************************************************************/
272 static ColorMapObject *ModifyColorMap(ColorMapObject *ColorMap)
274 int i, Dummy, Red, Green, Blue;
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);
286 /* Read the color map in ColorFile into this color map: */
287 for (i = 0; i < ColorMap->ColorCount; i++) {
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;
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)));
311 else if (TranslateFlag) {
312 ColorMapObject *NewMap;
315 /* Read the translation table in TranslateFile: */
316 for (i = 0; i < ColorMap->ColorCount; i++) {
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];
327 if ((NewMap = GifMakeMapObject(1 << GifBitSize(Max+1), NULL)) == NULL)
328 GIF_EXIT("Out of memory while allocating color map!");
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]];
339 GIF_EXIT("Nothing to do!");
344 /******************************************************************************
345 Close both input and output file (if open), and exit.
346 ******************************************************************************/
347 static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
349 if (GifFileIn != NULL) {
350 PrintGifError(GifFileIn->Error);
351 EGifCloseFile(GifFileIn, NULL);
353 if (GifFileOut != NULL) {
354 PrintGifError(GifFileOut->Error);
355 EGifCloseFile(GifFileOut, NULL);