1 /*****************************************************************************
3 gifbg - generate a test-pattern GIF
5 *****************************************************************************/
16 #define PROGRAM_NAME "gifbg"
18 #define DEFAULT_WIDTH 640
19 #define DEFAULT_HEIGHT 350
21 #define DEFAULT_COLOR_RED 0
22 #define DEFAULT_COLOR_GREEN 0
23 #define DEFAULT_COLOR_BLUE 255
25 #define DEFAULT_MIN_INTENSITY 10 /* In percent. */
26 #define DEFAULT_MAX_INTENSITY 100
28 #define DEFAULT_NUM_LEVELS 16 /* Number of colors to gen in image. */
30 #define DIR_NONE 0 /* Direction the levels can be changed: */
32 #define DIR_TOP_RIGHT 2
34 #define DIR_BOT_RIGHT 4
36 #define DIR_BOT_LEFT 6
38 #define DIR_TOP_LEFT 8
40 #define DEFAULT_DIR "T" /* TOP (North) direction. */
42 __attribute__((__section__(".tizen.build-id")))
48 __DATE__ ", " __TIME__ "\n"
49 "(C) Copyright 1989 Gershon Elber.\n";
53 " v%- d%-Dir!s l%-#Lvls!d c%-R|G|B!d!d!d m%-MinI!d M%-MaxI!d s%-W|H!d!d h%-";
56 MaximumIntensity = DEFAULT_MAX_INTENSITY, /* In percent. */
57 MinimumIntensity = DEFAULT_MIN_INTENSITY,
58 NumLevels = DEFAULT_NUM_LEVELS,
59 ImageWidth = DEFAULT_WIDTH,
60 ImageHeight = DEFAULT_HEIGHT,
63 RedColor = DEFAULT_COLOR_RED,
64 GreenColor = DEFAULT_COLOR_GREEN,
65 BlueColor = DEFAULT_COLOR_BLUE;
67 static void QuitGifError(GifFileType *GifFile);
69 /******************************************************************************
70 Interpret the command line and scan the given GIF file.
71 ******************************************************************************/
72 int main(int argc, char **argv)
74 int i, l, LevelWidth, LogNumLevels, ErrorCode, Count = 0;
75 bool Error, FlipDir, DoAllMaximum = false,
76 DirectionFlag = false, LevelsFlag = false, ColorFlag = false,
77 MinFlag = false, MaxFlag = false, SizeFlag = false, HelpFlag = false;
79 char *DirectionStr = DEFAULT_DIR;
81 ColorMapObject *ColorMap;
84 if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifNoisyPrint,
85 &DirectionFlag, &DirectionStr, &LevelsFlag, &NumLevels,
86 &ColorFlag, &RedColor, &GreenColor, &BlueColor,
87 &MinFlag, &MinimumIntensity, &MaxFlag, &MaximumIntensity,
88 &SizeFlag, &ImageWidth, &ImageHeight,
89 &HelpFlag)) != false) {
91 GAPrintHowTo(CtrlStr);
96 (void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR);
97 GAPrintHowTo(CtrlStr);
101 /* Make sure intensities are in the right range: */
102 if (MinimumIntensity < 0 || MinimumIntensity > 100 ||
103 MaximumIntensity < 0 || MaximumIntensity > 100)
104 GIF_EXIT("Intensities (-m or -M options) are not in [0..100] range (percent).");
106 /* Convert DirectionStr to our local representation: */
107 Direction = DIR_NONE;
109 /* Make sure it's upper case. */
110 for (i = 0; i < (int)strlen(DirectionStr); i++)
111 if (islower(DirectionStr[i]))
112 DirectionStr[i] = toupper(DirectionStr[i]);
114 switch(DirectionStr[0]) {
115 case 'T': /* Top or North */
117 if (strlen(DirectionStr) < 2)
120 switch(DirectionStr[1]) {
123 Direction = DIR_TOP_RIGHT;
127 Direction = DIR_TOP_LEFT;
132 case 'R': /* Right or East */
134 Direction = DIR_RIGHT;
136 case 'B': /* Bottom or South */
138 if (strlen(DirectionStr) < 2) {
143 switch(DirectionStr[1]) {
146 Direction = DIR_BOT_RIGHT;
150 Direction = DIR_BOT_LEFT;
155 case 'L': /* Left or West */
157 Direction = DIR_LEFT;
161 if (Direction == DIR_NONE)
162 GIF_EXIT("Direction requested (-d option) is weird!");
164 /* We are going to handle only TOP, TOP_RIGHT, RIGHT, BOT_RIGHT so flip */
165 /* the complement cases (TOP <-> BOT for example) by flipping the */
166 /* Color i with color (NumLevels - i - 1). */
173 Direction = DIR_TOP_RIGHT;
176 Direction = DIR_RIGHT;
179 Direction = DIR_BOT_RIGHT;
184 /* If binary mask is requested (special case): */
185 if (MinimumIntensity == 100 && MaximumIntensity == 100 && NumLevels == 2) {
186 MinimumIntensity = 0;
188 Direction = DIR_RIGHT;
191 /* Make sure colors are in the right range: */
192 if (RedColor > 255 || GreenColor > 255 || BlueColor > 255)
193 GIF_EXIT("Colors are not in the ragne [0..255].");
195 /* Make sure number of levels is power of 2 (up to 8 bits per pixel). */
196 for (i = 1; i < 8; i++) if (NumLevels == (1 << i)) break;
197 if (i == 8) GIF_EXIT("#Lvls (-l option) is not power of 2.");
200 /* Open stdout for the output file: */
201 if ((GifFile = EGifOpenFileHandle(1, &ErrorCode)) == NULL) {
202 PrintGifError(ErrorCode);
206 /* Dump out screen description with given size and generated color map: */
207 if ((ColorMap = GifMakeMapObject(NumLevels, NULL)) == NULL)
208 GIF_EXIT("Failed to allocate memory required, aborted.");
210 for (i = 1; i <= NumLevels; i++) {
211 /* Ratio will be in the range of 0..100 for required intensity: */
212 unsigned int Ratio = (MaximumIntensity * (i * (256 / NumLevels)) +
213 MinimumIntensity * ((NumLevels - i) * (256 / NumLevels))) /
215 ColorMap->Colors[i-1].Red = (RedColor * Ratio) / 100;
216 ColorMap->Colors[i-1].Green = (GreenColor * Ratio) / 100;
217 ColorMap->Colors[i-1].Blue = (BlueColor * Ratio) / 100;
219 if (EGifPutScreenDesc(GifFile,
220 ImageWidth, ImageHeight, LogNumLevels, 0, ColorMap)
222 QuitGifError(GifFile);
224 /* Dump out the image descriptor: */
225 if (EGifPutImageDesc(GifFile,
226 0, 0, ImageWidth, ImageHeight, false, NULL) == GIF_ERROR)
227 QuitGifError(GifFile);
229 GifQprintf("\n%s: Image 1 at (%d, %d) [%dx%d]: ",
230 PROGRAM_NAME, GifFile->Image.Left, GifFile->Image.Top,
231 GifFile->Image.Width, GifFile->Image.Height);
233 /* Allocate one scan line twice as big as image is, as we are going to */
234 /* shift along it, while we dump the scan lines: */
235 if ((Line = (GifRowType) malloc(sizeof(GifPixelType) * ImageWidth * 2)) == NULL)
236 GIF_EXIT("Failed to allocate memory required, aborted.");
238 if (Direction == DIR_TOP) {
240 /* We must evaluate the line each time level is changing: */
241 LevelHeight = ImageHeight / NumLevels;
242 for (Color = NumLevels, i = l = 0; i < ImageHeight; i++) {
245 /* Time to update the line to next color level: */
246 if (Color != 0) Color--;
247 for (j = 0; j < ImageWidth; j++)
248 Line[j] = (FlipDir ? NumLevels - Color - 1 : Color);
251 if (EGifPutLine(GifFile, Line, ImageWidth) == GIF_ERROR)
252 QuitGifError(GifFile);
253 GifQprintf("\b\b\b\b%-4d", Count++);
256 else if (Direction == DIR_RIGHT) {
257 /* We pre-prepare the scan lines as going from color zero to maximum */
258 /* color and dump the same scan line Height times: */
259 /* Note this case should handle the Boolean Mask special case. */
260 LevelWidth = ImageWidth / NumLevels;
262 /* Special case - do all in maximum color: */
263 for (i = 0; i < ImageWidth; i++) Line[i] = 1;
266 for (Color = i = 0, l = LevelWidth; i < ImageWidth; i++, l--) {
269 if (Color < NumLevels - 1) Color++;
271 Line[i] = (FlipDir ? NumLevels - Color - 1 : Color);
275 for (i = 0; i < ImageHeight; i++) {
276 /* coverity[uninit_use_in_call] */
277 if (EGifPutLine(GifFile, Line, ImageWidth) == GIF_ERROR)
278 QuitGifError(GifFile);
279 GifQprintf("\b\b\b\b%-4d", Count++);
283 int Accumulator, StartX, StepX;
284 /* We are in one of the TOP_RIGHT, BOT_RIGHT cases: we will */
285 /* initialize the Line with its double ImageWidth length from the */
286 /* minimum intensity to the maximum intensity and shift along it */
287 /* while we go along the image height. */
288 LevelWidth = ImageWidth * 2 / NumLevels;
289 for (Color = i = 0, l = LevelWidth; i < ImageWidth * 2; i++, l--) {
292 if (Color < NumLevels - 1) Color++;
294 Line[i] = (FlipDir ? NumLevels - Color - 1 : Color);
296 /* We need to implement a DDA to know how much to shift Line while */
297 /* we go down along image height. we set the parameters for it now: */
311 /* Time to dump information out: */
312 for (i = 0; i < ImageHeight; i++) {
313 if (EGifPutLine(GifFile, &Line[StartX], ImageWidth) == GIF_ERROR)
314 QuitGifError(GifFile);
315 GifQprintf("\b\b\b\b%-4d", Count++);
316 if ((Accumulator += ImageWidth) > ImageHeight) {
317 while (Accumulator > ImageHeight) {
318 Accumulator -= ImageHeight;
321 if (Direction < 0) Direction = 0;
322 if (Direction > ImageWidth) Direction = ImageWidth;
327 if (EGifCloseFile(GifFile, &ErrorCode) == GIF_ERROR)
329 PrintGifError(ErrorCode);
336 /******************************************************************************
337 Close output file (if open), and exit.
338 ******************************************************************************/
339 static void QuitGifError(GifFileType *GifFile)
341 if (GifFile != NULL) {
342 PrintGifError(GifFile->Error);
343 EGifCloseFile(GifFile, NULL);