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