Imported Upstream version 5.1.9 into tizen
[platform/upstream/giflib.git] / gifbg.c
1 /*****************************************************************************
2
3 gifbg - generate a test-pattern GIF
4
5 *****************************************************************************/
6
7 #include <stdio.h>
8 #include <ctype.h>
9 #include <string.h>
10 #include <stdbool.h>
11 #include <stdlib.h>
12
13 #include "gif_lib.h"
14 #include "getarg.h"
15
16 #define PROGRAM_NAME    "gifbg"
17
18 #define DEFAULT_WIDTH   640
19 #define DEFAULT_HEIGHT  350
20
21 #define DEFAULT_COLOR_RED       0
22 #define DEFAULT_COLOR_GREEN     0
23 #define DEFAULT_COLOR_BLUE      255
24
25 #define DEFAULT_MIN_INTENSITY   10                            /* In percent. */
26 #define DEFAULT_MAX_INTENSITY   100
27
28 #define DEFAULT_NUM_LEVELS      16     /* Number of colors to gen in image. */
29
30 #define DIR_NONE        0            /* Direction the levels can be changed: */
31 #define DIR_TOP         1
32 #define DIR_TOP_RIGHT   2
33 #define DIR_RIGHT       3
34 #define DIR_BOT_RIGHT   4
35 #define DIR_BOT         5
36 #define DIR_BOT_LEFT    6
37 #define DIR_LEFT        7
38 #define DIR_TOP_LEFT    8
39
40 #define DEFAULT_DIR     "T"                        /* TOP (North) direction. */
41
42 __attribute__((__section__(".tizen.build-id")))
43 static char
44     VersionStr[] =
45         PROGRAM_NAME
46         VERSION_COOKIE
47         "       Gershon Elber,  "
48         __DATE__ ",   " __TIME__ "\n"
49         "(C) Copyright 1989 Gershon Elber.\n";
50 static char
51     *CtrlStr =
52         PROGRAM_NAME
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%-";
54
55 static int
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,
61     Direction;
62 static unsigned int
63     RedColor = DEFAULT_COLOR_RED,
64     GreenColor = DEFAULT_COLOR_GREEN,
65     BlueColor = DEFAULT_COLOR_BLUE;
66
67 static void QuitGifError(GifFileType *GifFile);
68
69 /******************************************************************************
70  Interpret the command line and scan the given GIF file.
71 ******************************************************************************/
72 int main(int argc, char **argv)
73 {
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;
78     GifPixelType Color;
79     char *DirectionStr = DEFAULT_DIR;
80     GifRowType Line;
81     ColorMapObject *ColorMap;
82     GifFileType *GifFile;
83
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) {
90         GAPrintErrMsg(Error);
91         GAPrintHowTo(CtrlStr);
92         exit(EXIT_FAILURE);
93     }
94
95     if (HelpFlag) {
96         (void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR);
97         GAPrintHowTo(CtrlStr);
98         exit(EXIT_SUCCESS);
99     }
100
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).");
105
106     /* Convert DirectionStr to our local representation: */
107     Direction = DIR_NONE;
108     FlipDir = false;
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]);
113
114     switch(DirectionStr[0]) {
115         case 'T': /* Top or North */
116         case 'N':
117             if (strlen(DirectionStr) < 2)
118                 Direction = DIR_TOP;
119             else
120                 switch(DirectionStr[1]) {
121                     case 'R':
122                     case 'E':
123                         Direction = DIR_TOP_RIGHT;
124                         break;
125                     case 'L':
126                     case 'W':
127                         Direction = DIR_TOP_LEFT;
128                         FlipDir = true;
129                         break;
130                 }
131             break;
132         case 'R': /* Right or East */
133         case 'E':
134             Direction = DIR_RIGHT;
135             break;
136         case 'B': /* Bottom or South */
137         case 'S':
138             if (strlen(DirectionStr) < 2) {
139                 Direction = DIR_BOT;
140                 FlipDir = true;
141             }
142             else
143                 switch(DirectionStr[1]) {
144                     case 'R':
145                     case 'E':
146                         Direction = DIR_BOT_RIGHT;
147                         break;
148                     case 'L':
149                     case 'W':
150                         Direction = DIR_BOT_LEFT;
151                         FlipDir = true;
152                         break;
153                 }
154             break;
155         case 'L': /* Left or West */
156         case 'W':
157             Direction = DIR_LEFT;
158             FlipDir = true;
159             break;
160     }
161     if (Direction == DIR_NONE)
162         GIF_EXIT("Direction requested (-d option) is weird!");
163
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).                               */
167     if (FlipDir) {
168         switch (Direction) {
169             case DIR_BOT:
170                 Direction = DIR_TOP;
171                 break;
172             case DIR_BOT_LEFT:
173                 Direction = DIR_TOP_RIGHT;
174                 break;
175             case DIR_LEFT:
176                 Direction = DIR_RIGHT;
177                 break;
178             case DIR_TOP_LEFT:
179                 Direction = DIR_BOT_RIGHT;
180                 break;
181         }
182     }
183
184     /* If binary mask is requested (special case): */
185     if (MinimumIntensity == 100 && MaximumIntensity == 100 && NumLevels == 2) {
186         MinimumIntensity = 0;
187         DoAllMaximum = true;
188         Direction = DIR_RIGHT;
189     }
190
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].");
194
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.");
198     LogNumLevels = i;
199
200     /* Open stdout for the output file: */
201     if ((GifFile = EGifOpenFileHandle(1, &ErrorCode)) == NULL) {
202         PrintGifError(ErrorCode);
203         exit(EXIT_FAILURE);
204     }
205
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.");
209
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))) /
214             256;
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;
218     }
219     if (EGifPutScreenDesc(GifFile,
220         ImageWidth, ImageHeight, LogNumLevels, 0, ColorMap)
221         == GIF_ERROR)
222         QuitGifError(GifFile);
223
224     /* Dump out the image descriptor: */
225     if (EGifPutImageDesc(GifFile,
226         0, 0, ImageWidth, ImageHeight, false, NULL) == GIF_ERROR)
227         QuitGifError(GifFile);
228
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);
232
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.");
237
238     if (Direction == DIR_TOP) {
239         int LevelHeight;
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++) {
243             if (i == l) {
244                 int j;
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);
249                 l += LevelHeight;
250             }
251             if (EGifPutLine(GifFile, Line, ImageWidth) == GIF_ERROR)
252                 QuitGifError(GifFile);
253             GifQprintf("\b\b\b\b%-4d", Count++);
254         }
255     }
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;
261         if (DoAllMaximum) {
262             /* Special case - do all in maximum color: */
263             for (i = 0; i < ImageWidth; i++) Line[i] = 1;
264         }
265         else {
266             for (Color = i = 0, l = LevelWidth; i < ImageWidth; i++, l--) {
267                 if (l == 0) {
268                     l = LevelWidth;
269                     if (Color < NumLevels - 1) Color++;
270                 }
271                 Line[i] = (FlipDir ? NumLevels - Color - 1 : Color);
272             }
273         }
274
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++);
280         }
281     }
282     else {
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--) {
290             if (l == 0) {
291                 l = LevelWidth;
292                 if (Color < NumLevels - 1) Color++;
293             }
294             Line[i] = (FlipDir ? NumLevels - Color - 1 : Color);
295         }
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:  */
298         Accumulator = 0;
299         switch(Direction) {
300             case DIR_TOP_RIGHT:
301                 StartX = ImageWidth;
302                 StepX = -1;
303                 break;
304             case DIR_BOT_RIGHT:
305             default:
306                 StartX = 0;
307                 StepX = 1;
308                 break;
309         }
310
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;
319                     StartX += StepX;
320                 }
321                 if (Direction < 0) Direction = 0;
322                 if (Direction > ImageWidth) Direction = ImageWidth;
323             }
324         }
325     }
326
327     if (EGifCloseFile(GifFile, &ErrorCode) == GIF_ERROR)
328     {
329         PrintGifError(ErrorCode);
330         exit(EXIT_FAILURE);
331     }
332
333     return 0;
334 }
335
336 /******************************************************************************
337  Close output file (if open), and exit.
338 ******************************************************************************/
339 static void QuitGifError(GifFileType *GifFile)
340 {
341     if (GifFile != NULL) {
342         PrintGifError(GifFile->Error);
343         EGifCloseFile(GifFile, NULL);
344     }
345     exit(EXIT_FAILURE);
346 }
347
348 /* end */