1 /*****************************************************************************
3 giffix - attempt to fix a truncated GIF
5 *****************************************************************************/
16 #define PROGRAM_NAME "giffix"
18 __attribute__((__section__(".tizen.build-id")))
24 __DATE__ ", " __TIME__ "\n"
25 "(C) Copyright 1989 Gershon Elber.\n";
29 " v%- h%- GifFile!*s";
31 static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
33 /******************************************************************************
34 Interpret the command line and scan the given GIF file.
35 ******************************************************************************/
36 int main(int argc, char **argv)
38 int i, j, NumFiles, ExtCode, Row, Col, Width, Height, ErrorCode,
39 DarkestColor = 0, ColorIntens = 10000;
40 bool Error, HelpFlag = false;
41 GifRecordType RecordType;
42 GifByteType *Extension;
43 char **FileName = NULL;
44 GifRowType LineBuffer;
45 ColorMapObject *ColorMap;
46 GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
49 if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifNoisyPrint, &HelpFlag,
50 &NumFiles, &FileName)) != false ||
51 (NumFiles > 1 && !HelpFlag)) {
54 else if (NumFiles > 1)
55 GIF_MESSAGE("Error in command line parsing - one GIF file please.");
56 GAPrintHowTo(CtrlStr);
61 (void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR);
62 GAPrintHowTo(CtrlStr);
67 if ((GifFileIn = DGifOpenFileName(*FileName, &ErrorCode)) == NULL) {
68 PrintGifError(ErrorCode);
74 /* Use stdin instead: */
75 if ((GifFileIn = DGifOpenFileHandle(0, &ErrorCode)) == NULL) {
76 PrintGifError(ErrorCode);
81 /* Open stdout for the output file: */
82 if ((GifFileOut = EGifOpenFileHandle(1, &ErrorCode)) == NULL) {
83 PrintGifError(ErrorCode);
87 /* Dump out exactly same screen information: */
88 /* coverity[var_deref_op] */
89 if (EGifPutScreenDesc(GifFileOut,
90 GifFileIn->SWidth, GifFileIn->SHeight,
91 GifFileIn->SColorResolution, GifFileIn->SBackGroundColor,
92 GifFileIn->SColorMap) == GIF_ERROR)
93 QuitGifError(GifFileIn, GifFileOut);
95 if ((LineBuffer = (GifRowType) malloc(GifFileIn->SWidth)) == NULL)
96 GIF_EXIT("Failed to allocate memory required, aborted.");
98 /* Scan the content of the GIF file and load the image(s) in: */
100 if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
101 QuitGifError(GifFileIn, GifFileOut);
103 switch (RecordType) {
104 case IMAGE_DESC_RECORD_TYPE:
105 if (DGifGetImageDesc(GifFileIn) == GIF_ERROR)
106 QuitGifError(GifFileIn, GifFileOut);
107 if (GifFileIn->Image.Interlace)
108 GIF_EXIT("Cannot fix interlaced images.");
110 Row = GifFileIn->Image.Top; /* Image Position relative to Screen. */
111 Col = GifFileIn->Image.Left;
112 Width = GifFileIn->Image.Width;
113 Height = GifFileIn->Image.Height;
114 GifQprintf("\n%s: Image %d at (%d, %d) [%dx%d]: ",
115 PROGRAM_NAME, ++ImageNum, Col, Row, Width, Height);
116 if (Width > GifFileIn->SWidth)
117 GIF_EXIT("Image is wider than total");
119 /* Put the image descriptor to out file: */
120 if (EGifPutImageDesc(GifFileOut, Col, Row, Width, Height,
121 false, GifFileIn->Image.ColorMap) == GIF_ERROR)
122 QuitGifError(GifFileIn, GifFileOut);
124 /* Find the darkest color in color map to use as a filler. */
125 ColorMap = (GifFileIn->Image.ColorMap ? GifFileIn->Image.ColorMap :
126 GifFileIn->SColorMap);
127 for (i = 0; i < ColorMap->ColorCount; i++) {
128 j = ((int) ColorMap->Colors[i].Red) * 30 +
129 ((int) ColorMap->Colors[i].Green) * 59 +
130 ((int) ColorMap->Colors[i].Blue) * 11;
131 if (j < ColorIntens) {
137 /* Load the image, and dump it. */
138 for (i = 0; i < Height; i++) {
139 GifQprintf("\b\b\b\b%-4d", i);
140 if (DGifGetLine(GifFileIn, LineBuffer, Width)
142 if (EGifPutLine(GifFileOut, LineBuffer, Width)
143 == GIF_ERROR) QuitGifError(GifFileIn, GifFileOut);
147 fprintf(stderr,"\nFollowing error occurred (and ignored):");
148 PrintGifError(GifFileIn->Error);
150 /* Fill in with the darkest color in color map. */
151 for (j = 0; j < Width; j++)
152 LineBuffer[j] = DarkestColor;
153 for (; i < Height; i++)
154 if (EGifPutLine(GifFileOut, LineBuffer, Width)
155 == GIF_ERROR) QuitGifError(GifFileIn, GifFileOut);
158 case EXTENSION_RECORD_TYPE:
159 /* pass through extension records */
160 if (DGifGetExtension(GifFileIn, &ExtCode, &Extension) == GIF_ERROR)
161 QuitGifError(GifFileIn, GifFileOut);
162 if (EGifPutExtensionLeader(GifFileOut, ExtCode) == GIF_ERROR)
163 QuitGifError(GifFileIn, GifFileOut);
164 if (Extension != NULL)
165 if (EGifPutExtensionBlock(GifFileOut,
167 Extension + 1) == GIF_ERROR)
168 QuitGifError(GifFileIn, GifFileOut);
169 while (Extension != NULL) {
170 if (DGifGetExtensionNext(GifFileIn, &Extension)==GIF_ERROR)
171 QuitGifError(GifFileIn, GifFileOut);
172 if (Extension != NULL)
173 if (EGifPutExtensionBlock(GifFileOut,
175 Extension + 1) == GIF_ERROR)
176 QuitGifError(GifFileIn, GifFileOut);
178 if (EGifPutExtensionTrailer(GifFileOut) == GIF_ERROR)
179 QuitGifError(GifFileIn, GifFileOut);
181 case TERMINATE_RECORD_TYPE:
183 default: /* Should be trapped by DGifGetRecordType. */
187 while (RecordType != TERMINATE_RECORD_TYPE);
189 if (DGifCloseFile(GifFileIn, &ErrorCode) == GIF_ERROR) {
190 PrintGifError(ErrorCode);
193 if (EGifCloseFile(GifFileOut, &ErrorCode) == GIF_ERROR) {
194 PrintGifError(ErrorCode);
200 /******************************************************************************
201 Close both input and output file (if open), and exit.
202 ******************************************************************************/
203 static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
205 fprintf(stderr, "\nFollowing unrecoverable error occurred:");
206 if (GifFileIn != NULL) {
207 PrintGifError(GifFileIn->Error);
208 EGifCloseFile(GifFileIn, NULL);
210 if (GifFileOut != NULL) {
211 PrintGifError(GifFileOut->Error);
212 EGifCloseFile(GifFileOut, NULL);