1 /*****************************************************************************
2
3 giffix - attempt to fix a truncated GIF
4
5 SPDX-License-Identifier: MIT
6
7 *****************************************************************************/
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include <stdbool.h>
14
15 #include "gif_lib.h"
16 #include "getarg.h"
17
18 #define PROGRAM_NAME "giffix"
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%- h%- GifFile!*s";
31
32 static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
33
34 /******************************************************************************
35 Interpret the command line and scan the given GIF file.
36 ******************************************************************************/
main(int argc,char ** argv)37 int main(int argc, char **argv)
38 {
39 int i, j, NumFiles, ExtCode, Row, Col, Width, Height, ErrorCode,
40 DarkestColor = 0, ColorIntens = 10000;
41 bool Error, HelpFlag = false;
42 GifRecordType RecordType;
43 GifByteType *Extension;
44 char **FileName = NULL;
45 GifRowType LineBuffer;
46 ColorMapObject *ColorMap;
47 GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
48 int ImageNum = 0;
49
50 if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifNoisyPrint, &HelpFlag,
51 &NumFiles, &FileName)) != false ||
52 (NumFiles > 1 && !HelpFlag)) {
53 if (Error)
54 GAPrintErrMsg(Error);
55 else if (NumFiles > 1)
56 GIF_MESSAGE("Error in command line parsing - one GIF file please.");
57 GAPrintHowTo(CtrlStr);
58 exit(EXIT_FAILURE);
59 }
60
61 if (HelpFlag) {
62 (void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR);
63 GAPrintHowTo(CtrlStr);
64 exit(EXIT_SUCCESS);
65 }
66
67 if (NumFiles == 1) {
68 if ((GifFileIn = DGifOpenFileName(*FileName, &ErrorCode)) == NULL) {
69 PrintGifError(ErrorCode);
70 exit(EXIT_FAILURE);
71 }
72 }
73 else
74 {
75 /* Use stdin instead: */
76 if ((GifFileIn = DGifOpenFileHandle(0, &ErrorCode)) == NULL) {
77 PrintGifError(ErrorCode);
78 exit(EXIT_FAILURE);
79 }
80 }
81
82 /* Open stdout for the output file: */
83 if ((GifFileOut = EGifOpenFileHandle(1, &ErrorCode)) == NULL) {
84 PrintGifError(ErrorCode);
85 exit(EXIT_FAILURE);
86 }
87
88 /* Dump out exactly same screen information: */
89 /* coverity[var_deref_op] */
90 if (EGifPutScreenDesc(GifFileOut,
91 GifFileIn->SWidth, GifFileIn->SHeight,
92 GifFileIn->SColorResolution, GifFileIn->SBackGroundColor,
93 GifFileIn->SColorMap) == GIF_ERROR)
94 QuitGifError(GifFileIn, GifFileOut);
95
96 if ((LineBuffer = (GifRowType) malloc(GifFileIn->SWidth)) == NULL)
97 GIF_EXIT("Failed to allocate memory required, aborted.");
98
99 /* Scan the content of the GIF file and load the image(s) in: */
100 do {
101 if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
102 QuitGifError(GifFileIn, GifFileOut);
103
104 switch (RecordType) {
105 case IMAGE_DESC_RECORD_TYPE:
106 if (DGifGetImageDesc(GifFileIn) == GIF_ERROR)
107 QuitGifError(GifFileIn, GifFileOut);
108 if (GifFileIn->Image.Interlace)
109 GIF_EXIT("Cannot fix interlaced images.");
110
111 Row = GifFileIn->Image.Top; /* Image Position relative to Screen. */
112 Col = GifFileIn->Image.Left;
113 Width = GifFileIn->Image.Width;
114 Height = GifFileIn->Image.Height;
115 GifQprintf("\n%s: Image %d at (%d, %d) [%dx%d]: ",
116 PROGRAM_NAME, ++ImageNum, Col, Row, Width, Height);
117 if (Width > GifFileIn->SWidth)
118 GIF_EXIT("Image is wider than total");
119
120 /* Put the image descriptor to out file: */
121 if (EGifPutImageDesc(GifFileOut, Col, Row, Width, Height,
122 false, GifFileIn->Image.ColorMap) == GIF_ERROR)
123 QuitGifError(GifFileIn, GifFileOut);
124
125 /* Find the darkest color in color map to use as a filler. */
126 ColorMap = (GifFileIn->Image.ColorMap ? GifFileIn->Image.ColorMap :
127 GifFileIn->SColorMap);
128 for (i = 0; i < ColorMap->ColorCount; i++) {
129 j = ((int) ColorMap->Colors[i].Red) * 30 +
130 ((int) ColorMap->Colors[i].Green) * 59 +
131 ((int) ColorMap->Colors[i].Blue) * 11;
132 if (j < ColorIntens) {
133 ColorIntens = j;
134 DarkestColor = i;
135 }
136 }
137
138 /* Load the image, and dump it. */
139 for (i = 0; i < Height; i++) {
140 GifQprintf("\b\b\b\b%-4d", i);
141 if (DGifGetLine(GifFileIn, LineBuffer, Width)
142 == GIF_ERROR) break;
143 if (EGifPutLine(GifFileOut, LineBuffer, Width)
144 == GIF_ERROR) QuitGifError(GifFileIn, GifFileOut);
145 }
146
147 if (i < Height) {
148 fprintf(stderr,"\nFollowing error occurred (and ignored):");
149 PrintGifError(GifFileIn->Error);
150
151 /* Fill in with the darkest color in color map. */
152 for (j = 0; j < Width; j++)
153 LineBuffer[j] = DarkestColor;
154 for (; i < Height; i++)
155 if (EGifPutLine(GifFileOut, LineBuffer, Width)
156 == GIF_ERROR) QuitGifError(GifFileIn, GifFileOut);
157 }
158 break;
159 case EXTENSION_RECORD_TYPE:
160 /* pass through extension records */
161 if (DGifGetExtension(GifFileIn, &ExtCode, &Extension) == GIF_ERROR)
162 QuitGifError(GifFileIn, GifFileOut);
163 if (EGifPutExtensionLeader(GifFileOut, ExtCode) == GIF_ERROR)
164 QuitGifError(GifFileIn, GifFileOut);
165 if (Extension != NULL)
166 if (EGifPutExtensionBlock(GifFileOut,
167 Extension[0],
168 Extension + 1) == GIF_ERROR)
169 QuitGifError(GifFileIn, GifFileOut);
170 while (Extension != NULL) {
171 if (DGifGetExtensionNext(GifFileIn, &Extension)==GIF_ERROR)
172 QuitGifError(GifFileIn, GifFileOut);
173 if (Extension != NULL)
174 if (EGifPutExtensionBlock(GifFileOut,
175 Extension[0],
176 Extension + 1) == GIF_ERROR)
177 QuitGifError(GifFileIn, GifFileOut);
178 }
179 if (EGifPutExtensionTrailer(GifFileOut) == GIF_ERROR)
180 QuitGifError(GifFileIn, GifFileOut);
181 break;
182 case TERMINATE_RECORD_TYPE:
183 break;
184 default: /* Should be trapped by DGifGetRecordType. */
185 break;
186 }
187 }
188 while (RecordType != TERMINATE_RECORD_TYPE);
189
190 if (DGifCloseFile(GifFileIn, &ErrorCode) == GIF_ERROR) {
191 PrintGifError(ErrorCode);
192 exit(EXIT_FAILURE);
193 }
194 if (EGifCloseFile(GifFileOut, &ErrorCode) == GIF_ERROR) {
195 PrintGifError(ErrorCode);
196 exit(EXIT_FAILURE);
197 }
198 return 0;
199 }
200
201 /******************************************************************************
202 Close both input and output file (if open), and exit.
203 ******************************************************************************/
QuitGifError(GifFileType * GifFileIn,GifFileType * GifFileOut)204 static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
205 {
206 fprintf(stderr, "\nFollowing unrecoverable error occurred:");
207 if (GifFileIn != NULL) {
208 PrintGifError(GifFileIn->Error);
209 EGifCloseFile(GifFileIn, NULL);
210 }
211 if (GifFileOut != NULL) {
212 PrintGifError(GifFileOut->Error);
213 EGifCloseFile(GifFileOut, NULL);
214 }
215 exit(EXIT_FAILURE);
216 }
217
218 /* end */
219