1 /*****************************************************************************
2
3 gifwedge - create a GIF test pattern
4
5 SPDX-License-Identifier: MIT
6
7 *****************************************************************************/
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include <stdbool.h>
14
15 #include "gif_lib.h"
16 #include "getarg.h"
17
18 #define PROGRAM_NAME "gifwedge"
19
20 #define DEFAULT_WIDTH 640
21 #define DEFAULT_HEIGHT 350
22
23 #define DEFAULT_NUM_LEVELS 16 /* Number of colors to gen the image. */
24
25 static char
26 *VersionStr =
27 PROGRAM_NAME
28 VERSION_COOKIE
29 " Gershon Elber, "
30 __DATE__ ", " __TIME__ "\n"
31 "(C) Copyright 1989 Gershon Elber.\n";
32 static char
33 *CtrlStr =
34 PROGRAM_NAME
35 " v%- l%-#Lvls!d s%-Width|Height!d!d h%-";
36
37 static int
38 NumLevels = DEFAULT_NUM_LEVELS,
39 ImageWidth = DEFAULT_WIDTH,
40 ImageHeight = DEFAULT_HEIGHT;
41
42 /******************************************************************************
43 Interpret the command line and scan the given GIF file.
44 ******************************************************************************/
main(int argc,char ** argv)45 int main(int argc, char **argv)
46 {
47 int i, j, l, c, LevelStep, LogNumLevels, ErrorCode, Count = 0;
48 bool Error, LevelsFlag = false, SizeFlag = false, HelpFlag = false;
49 GifRowType Line;
50 ColorMapObject *ColorMap;
51 GifFileType *GifFile;
52
53 if ((Error = GAGetArgs(argc, argv, CtrlStr,
54 &GifNoisyPrint, &LevelsFlag, &NumLevels,
55 &SizeFlag, &ImageWidth, &ImageHeight,
56 &HelpFlag)) != false) {
57 GAPrintErrMsg(Error);
58 GAPrintHowTo(CtrlStr);
59 exit(EXIT_FAILURE);
60 }
61
62 if (HelpFlag) {
63 (void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR);
64 GAPrintHowTo(CtrlStr);
65 exit(EXIT_SUCCESS);
66 }
67
68 /* Make sure the number of levels is power of 2 (up to 32 levels.). */
69 for (i = 1; i < 6; i++) if (NumLevels == (1 << i)) break;
70 if (i == 6) GIF_EXIT("#Lvls (-l option) is not power of 2 up to 32.");
71 LogNumLevels = i + 3; /* Multiple by 8 (see below). */
72 LevelStep = 256 / NumLevels;
73
74 /* Make sure the image dimension is a multiple of NumLevels horizontally */
75 /* and 7 (White, Red, Green, Blue and Yellow Cyan Magenta) vertically. */
76 ImageWidth = (ImageWidth / NumLevels) * NumLevels;
77 ImageHeight = (ImageHeight / 7) * 7;
78
79 /* Open stdout for the output file: */
80 if ((GifFile = EGifOpenFileHandle(1, &ErrorCode)) == NULL) {
81 PrintGifError(ErrorCode);
82 exit(EXIT_FAILURE);
83 }
84
85 /* Dump out screen description with given size and generated color map: */
86 /* The color map has 7 NumLevels colors for White, Red, Green and then */
87 /* The secondary colors Yellow Cyan and magenta. */
88 if ((ColorMap = GifMakeMapObject(8 * NumLevels, NULL)) == NULL)
89 GIF_EXIT("Failed to allocate memory required, aborted.");
90
91 for (i = 0; i < 8; i++) /* Set color map. */
92 for (j = 0; j < NumLevels; j++) {
93 l = LevelStep * j;
94 c = i * NumLevels + j;
95 ColorMap->Colors[c].Red = (i == 0 || i == 1 || i == 4 || i == 6) * l;
96 ColorMap->Colors[c].Green = (i == 0 || i == 2 || i == 4 || i == 5) * l;
97 ColorMap->Colors[c].Blue = (i == 0 || i == 3 || i == 5 || i == 6) * l;
98 }
99
100 if (EGifPutScreenDesc(GifFile, ImageWidth, ImageHeight, LogNumLevels, 0, ColorMap) == GIF_ERROR) {
101 PrintGifError(GifFile->Error);
102 }
103
104 /* Dump out the image descriptor: */
105 if (EGifPutImageDesc(GifFile,
106 0, 0, ImageWidth, ImageHeight,
107 false, NULL) == GIF_ERROR) {
108
109 PrintGifError(GifFile->Error);
110 exit(EXIT_FAILURE);
111 }
112
113 GifQprintf("\n%s: Image 1 at (%d, %d) [%dx%d]: ",
114 PROGRAM_NAME, GifFile->Image.Left, GifFile->Image.Top,
115 GifFile->Image.Width, GifFile->Image.Height);
116
117 /* Allocate one scan line to be used for all image. */
118 if ((Line = (GifRowType) malloc(sizeof(GifPixelType) * ImageWidth)) == NULL)
119 GIF_EXIT("Failed to allocate memory required, aborted.");
120
121 /* Dump the pixels: */
122 for (c = 0; c < 7; c++) {
123 for (i = 0, l = 0; i < NumLevels; i++)
124 for (j = 0; j < ImageWidth / NumLevels; j++)
125 Line[l++] = i + NumLevels * c;
126 for (i = 0; i < ImageHeight / 7; i++) {
127 if (EGifPutLine(GifFile, Line, ImageWidth) == GIF_ERROR) {
128 PrintGifError(GifFile->Error);
129 exit(EXIT_FAILURE);
130 }
131 GifQprintf("\b\b\b\b%-4d", Count++);
132 }
133 }
134
135 if (EGifCloseFile(GifFile, &ErrorCode) == GIF_ERROR) {
136 PrintGifError(ErrorCode);
137 exit(EXIT_FAILURE);
138 }
139
140 return 0;
141 }
142
143 /* end */
144