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