xref: /aosp_15_r20/external/giflib/gifbg.c (revision 324bb76b8d05e2a05aa88511fff61cf3f9ca5892)
1 /*****************************************************************************
2 
3 gifbg - generate a test-pattern GIF
4 
5 SPDX-License-Identifier: MIT
6 
7 *****************************************************************************/
8 
9 #include <ctype.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "getarg.h"
16 #include "gif_lib.h"
17 
18 #define PROGRAM_NAME "gifbg"
19 
20 #define DEFAULT_WIDTH 640
21 #define DEFAULT_HEIGHT 350
22 
23 #define DEFAULT_COLOR_RED 0
24 #define DEFAULT_COLOR_GREEN 0
25 #define DEFAULT_COLOR_BLUE 255
26 
27 #define DEFAULT_MIN_INTENSITY 10 /* In percent. */
28 #define DEFAULT_MAX_INTENSITY 100
29 
30 #define DEFAULT_NUM_LEVELS 16 /* Number of colors to gen in image. */
31 
32 #define DIR_NONE 0 /* Direction the levels can be changed: */
33 #define DIR_TOP 1
34 #define DIR_TOP_RIGHT 2
35 #define DIR_RIGHT 3
36 #define DIR_BOT_RIGHT 4
37 #define DIR_BOT 5
38 #define DIR_BOT_LEFT 6
39 #define DIR_LEFT 7
40 #define DIR_TOP_LEFT 8
41 
42 #define DEFAULT_DIR "T" /* TOP (North) direction. */
43 
44 static char *VersionStr = PROGRAM_NAME VERSION_COOKIE
45     "	Gershon Elber,	" __DATE__ ",   " __TIME__ "\n"
46     "(C) Copyright 1989 Gershon Elber.\n";
47 static char *CtrlStr = PROGRAM_NAME " v%- d%-Dir!s l%-#Lvls!d c%-R|G|B!d!d!d "
48                                     "m%-MinI!d M%-MaxI!d s%-W|H!d!d h%-";
49 
50 static int MaximumIntensity = DEFAULT_MAX_INTENSITY, /* In percent. */
51     MinimumIntensity = DEFAULT_MIN_INTENSITY, NumLevels = DEFAULT_NUM_LEVELS,
52            ImageWidth = DEFAULT_WIDTH, ImageHeight = DEFAULT_HEIGHT, Direction;
53 static unsigned int RedColor = DEFAULT_COLOR_RED,
54                     GreenColor = DEFAULT_COLOR_GREEN,
55                     BlueColor = DEFAULT_COLOR_BLUE;
56 
57 static void QuitGifError(GifFileType *GifFile);
58 
59 /******************************************************************************
60  Interpret the command line and scan the given GIF file.
61 ******************************************************************************/
main(int argc,char ** argv)62 int main(int argc, char **argv) {
63 	int i, l, LevelWidth, LogNumLevels, ErrorCode, Count = 0;
64 	bool Error, FlipDir, DoAllMaximum = false, DirectionFlag = false,
65 	                     LevelsFlag = false, ColorFlag = false,
66 	                     MinFlag = false, MaxFlag = false, SizeFlag = false,
67 	                     HelpFlag = false, GifNoisyPrint;
68 	GifPixelType Color;
69 	char *DirectionStr = DEFAULT_DIR;
70 	GifRowType Line;
71 	ColorMapObject *ColorMap;
72 	GifFileType *GifFile;
73 
74 	if ((Error = GAGetArgs(
75 	         argc, argv, CtrlStr, &GifNoisyPrint, &DirectionFlag,
76 	         &DirectionStr, &LevelsFlag, &NumLevels, &ColorFlag, &RedColor,
77 	         &GreenColor, &BlueColor, &MinFlag, &MinimumIntensity, &MaxFlag,
78 	         &MaximumIntensity, &SizeFlag, &ImageWidth, &ImageHeight,
79 	         &HelpFlag)) != false) {
80 		GAPrintErrMsg(Error);
81 		GAPrintHowTo(CtrlStr);
82 		exit(EXIT_FAILURE);
83 	}
84 
85 	if (HelpFlag) {
86 		(void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR);
87 		GAPrintHowTo(CtrlStr);
88 		exit(EXIT_SUCCESS);
89 	}
90 
91 	/* Make sure intensities are in the right range: */
92 	if (MinimumIntensity < 0 || MinimumIntensity > 100 ||
93 	    MaximumIntensity < 0 || MaximumIntensity > 100) {
94 		GIF_EXIT("Intensities (-m or -M options) are not in [0..100] "
95 		         "range (percent).");
96 	}
97 
98 	/* Convert DirectionStr to our local representation: */
99 	Direction = DIR_NONE;
100 	FlipDir = false;
101 	/* Make sure it's upper case. */
102 	for (i = 0; i < (int)strlen(DirectionStr); i++) {
103 		if (islower(DirectionStr[i])) {
104 			DirectionStr[i] = toupper(DirectionStr[i]);
105 		}
106 	}
107 
108 	switch (DirectionStr[0]) {
109 	case 'T': /* Top or North */
110 	case 'N':
111 		if (strlen(DirectionStr) < 2) {
112 			Direction = DIR_TOP;
113 		} else {
114 			switch (DirectionStr[1]) {
115 			case 'R':
116 			case 'E':
117 				Direction = DIR_TOP_RIGHT;
118 				break;
119 			case 'L':
120 			case 'W':
121 				Direction = DIR_TOP_LEFT;
122 				FlipDir = true;
123 				break;
124 			}
125 		}
126 		break;
127 	case 'R': /* Right or East */
128 	case 'E':
129 		Direction = DIR_RIGHT;
130 		break;
131 	case 'B': /* Bottom or South */
132 	case 'S':
133 		if (strlen(DirectionStr) < 2) {
134 			Direction = DIR_BOT;
135 			FlipDir = true;
136 		} else {
137 			switch (DirectionStr[1]) {
138 			case 'R':
139 			case 'E':
140 				Direction = DIR_BOT_RIGHT;
141 				break;
142 			case 'L':
143 			case 'W':
144 				Direction = DIR_BOT_LEFT;
145 				FlipDir = true;
146 				break;
147 			}
148 		}
149 		break;
150 	case 'L': /* Left or West */
151 	case 'W':
152 		Direction = DIR_LEFT;
153 		FlipDir = true;
154 		break;
155 	}
156 	if (Direction == DIR_NONE) {
157 		GIF_EXIT("Direction requested (-d option) is weird!");
158 	}
159 
160 	/* We are going to handle only TOP, TOP_RIGHT, RIGHT, BOT_RIGHT  so flip
161 	 */
162 	/* the complement cases (TOP <-> BOT for example) by flipping the
163 	 */
164 	/* Color i with color (NumLevels - i - 1). */
165 	if (FlipDir) {
166 		switch (Direction) {
167 		case DIR_BOT:
168 			Direction = DIR_TOP;
169 			break;
170 		case DIR_BOT_LEFT:
171 			Direction = DIR_TOP_RIGHT;
172 			break;
173 		case DIR_LEFT:
174 			Direction = DIR_RIGHT;
175 			break;
176 		case DIR_TOP_LEFT:
177 			Direction = DIR_BOT_RIGHT;
178 			break;
179 		}
180 	}
181 
182 	/* If binary mask is requested (special case): */
183 	if (MinimumIntensity == 100 && MaximumIntensity == 100 &&
184 	    NumLevels == 2) {
185 		MinimumIntensity = 0;
186 		DoAllMaximum = true;
187 		Direction = DIR_RIGHT;
188 	}
189 
190 	/* Make sure colors are in the right range: */
191 	if (RedColor > 255 || GreenColor > 255 || BlueColor > 255) {
192 		GIF_EXIT("Colors are not in the ragne [0..255].");
193 	}
194 
195 	/* Make sure number of levels is power of 2 (up to 8 bits per pixel). */
196 	for (i = 1; i < 8; i++) {
197 		if (NumLevels == (1 << i)) {
198 			break;
199 		}
200 	}
201 	if (i == 8) {
202 		GIF_EXIT("#Lvls (-l option) is not power of 2.");
203 	}
204 	LogNumLevels = i;
205 
206 	/* Open stdout for the output file: */
207 	if ((GifFile = EGifOpenFileHandle(1, &ErrorCode)) == NULL) {
208 		PrintGifError(ErrorCode);
209 		exit(EXIT_FAILURE);
210 	}
211 
212 	/* Dump out screen description with given size and generated color map:
213 	 */
214 	if ((ColorMap = GifMakeMapObject(NumLevels, NULL)) == NULL) {
215 		GIF_EXIT("Failed to allocate memory required, aborted.");
216 	}
217 
218 	for (i = 1; i <= NumLevels; i++) {
219 		/* Ratio will be in the range of 0..100 for required intensity:
220 		 */
221 		unsigned int Ratio =
222 		    (MaximumIntensity * (i * (256 / NumLevels)) +
223 		     MinimumIntensity * ((NumLevels - i) * (256 / NumLevels))) /
224 		    256;
225 		ColorMap->Colors[i - 1].Red = (RedColor * Ratio) / 100;
226 		ColorMap->Colors[i - 1].Green = (GreenColor * Ratio) / 100;
227 		ColorMap->Colors[i - 1].Blue = (BlueColor * Ratio) / 100;
228 	}
229 	if (EGifPutScreenDesc(GifFile, ImageWidth, ImageHeight, LogNumLevels, 0,
230 	                      ColorMap) == GIF_ERROR) {
231 		QuitGifError(GifFile);
232 	}
233 
234 	/* Dump out the image descriptor: */
235 	if (EGifPutImageDesc(GifFile, 0, 0, ImageWidth, ImageHeight, false,
236 	                     NULL) == GIF_ERROR) {
237 		QuitGifError(GifFile);
238 	}
239 
240 	GifQprintf("\n%s: Image 1 at (%d, %d) [%dx%d]:     ", PROGRAM_NAME,
241 	           GifFile->Image.Left, GifFile->Image.Top,
242 	           GifFile->Image.Width, GifFile->Image.Height);
243 
244 	/* Allocate one scan line twice as big as image is, as we are going to
245 	 */
246 	/* shift along it, while we dump the scan lines: */
247 	if ((Line = (GifRowType)malloc(sizeof(GifPixelType) * ImageWidth *
248 	                               2)) == NULL) {
249 		GIF_EXIT("Failed to allocate memory required, aborted.");
250 	}
251 
252 	if (Direction == DIR_TOP) {
253 		int LevelHeight;
254 		/* We must evaluate the line each time level is changing: */
255 		LevelHeight = ImageHeight / NumLevels;
256 		for (Color = NumLevels, i = l = 0; i < ImageHeight; i++) {
257 			if (i == l) {
258 				int j;
259 				/* Time to update the line to next color level:
260 				 */
261 				if (Color != 0) {
262 					Color--;
263 				}
264 				for (j = 0; j < ImageWidth; j++) {
265 					Line[j] =
266 					    (FlipDir ? NumLevels - Color - 1
267 					             : Color);
268 				}
269 				l += LevelHeight;
270 			}
271 			if (EGifPutLine(GifFile, Line, ImageWidth) ==
272 			    GIF_ERROR) {
273 				QuitGifError(GifFile);
274 			}
275 			GifQprintf("\b\b\b\b%-4d", Count++);
276 		}
277 	} else if (Direction == DIR_RIGHT) {
278 		/* We pre-prepare the scan lines as going from color zero to
279 		 * maximum */
280 		/* color and dump the same scan line Height times:
281 		 */
282 		/* Note this case should handle the Boolean Mask special case.
283 		 */
284 		LevelWidth = ImageWidth / NumLevels;
285 		if (DoAllMaximum) {
286 			/* Special case - do all in maximum color: */
287 			for (i = 0; i < ImageWidth; i++) {
288 				Line[i] = 1;
289 			}
290 		} else {
291 			for (Color = i = 0, l = LevelWidth; i < ImageWidth;
292 			     i++, l--) {
293 				if (l == 0) {
294 					l = LevelWidth;
295 					if (Color < NumLevels - 1) {
296 						Color++;
297 					}
298 				}
299 				Line[i] =
300 				    (FlipDir ? NumLevels - Color - 1 : Color);
301 			}
302 		}
303 
304 		for (i = 0; i < ImageHeight; i++) {
305 			/* coverity[uninit_use_in_call] */
306 			if (EGifPutLine(GifFile, Line, ImageWidth) ==
307 			    GIF_ERROR) {
308 				QuitGifError(GifFile);
309 			}
310 			GifQprintf("\b\b\b\b%-4d", Count++);
311 		}
312 	} else {
313 		int Accumulator, StartX, StepX;
314 		/* We are in one of the TOP_RIGHT, BOT_RIGHT cases: we will */
315 		/* initialize the Line with its double ImageWidth length from
316 		 * the    */
317 		/* minimum intensity to the maximum intensity and shift along it
318 		 */
319 		/* while we go along the image height. */
320 		LevelWidth = ImageWidth * 2 / NumLevels;
321 		for (Color = i = 0, l = LevelWidth; i < ImageWidth * 2;
322 		     i++, l--) {
323 			if (l == 0) {
324 				l = LevelWidth;
325 				if (Color < NumLevels - 1) {
326 					Color++;
327 				}
328 			}
329 			Line[i] = (FlipDir ? NumLevels - Color - 1 : Color);
330 		}
331 		/* We need to implement a DDA to know how much to shift Line
332 		 * while   */
333 		/* we go down along image height. we set the parameters for it
334 		 * now:  */
335 		Accumulator = 0;
336 		switch (Direction) {
337 		case DIR_TOP_RIGHT:
338 			StartX = ImageWidth;
339 			StepX = -1;
340 			break;
341 		case DIR_BOT_RIGHT:
342 		default:
343 			StartX = 0;
344 			StepX = 1;
345 			break;
346 		}
347 
348 		/* Time to dump information out: */
349 		for (i = 0; i < ImageHeight; i++) {
350 			if (EGifPutLine(GifFile, &Line[StartX], ImageWidth) ==
351 			    GIF_ERROR) {
352 				QuitGifError(GifFile);
353 			}
354 			GifQprintf("\b\b\b\b%-4d", Count++);
355 			if ((Accumulator += ImageWidth) > ImageHeight) {
356 				while (Accumulator > ImageHeight) {
357 					Accumulator -= ImageHeight;
358 					StartX += StepX;
359 				}
360 				if (Direction < 0) {
361 					Direction = 0;
362 				}
363 				if (Direction > ImageWidth) {
364 					Direction = ImageWidth;
365 				}
366 			}
367 		}
368 	}
369 
370 	if (EGifCloseFile(GifFile, &ErrorCode) == GIF_ERROR) {
371 		PrintGifError(ErrorCode);
372 		exit(EXIT_FAILURE);
373 	}
374 
375 	return 0;
376 }
377 
378 /******************************************************************************
379  Close output file (if open), and exit.
380 ******************************************************************************/
QuitGifError(GifFileType * GifFile)381 static void QuitGifError(GifFileType *GifFile) {
382 	if (GifFile != NULL) {
383 		PrintGifError(GifFile->Error);
384 		EGifCloseFile(GifFile, NULL);
385 	}
386 	exit(EXIT_FAILURE);
387 }
388 
389 /* end */
390