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