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
/
gifwedge.c
143 lines (116 loc) · 4.34 KB
/
gifwedge.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
/*****************************************************************************
gifwedge - create a GIF test pattern
SPDX-License-Identifier: MIT
*****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include "gif_lib.h"
#include "getarg.h"
#define PROGRAM_NAME "gifwedge"
#define DEFAULT_WIDTH 640
#define DEFAULT_HEIGHT 350
#define DEFAULT_NUM_LEVELS 16 /* Number of colors to gen the image. */
static char
*VersionStr =
PROGRAM_NAME
VERSION_COOKIE
" Gershon Elber, "
__DATE__ ", " __TIME__ "\n"
"(C) Copyright 1989 Gershon Elber.\n";
static char
*CtrlStr =
PROGRAM_NAME
" v%- l%-#Lvls!d s%-Width|Height!d!d h%-";
static int
NumLevels = DEFAULT_NUM_LEVELS,
ImageWidth = DEFAULT_WIDTH,
ImageHeight = DEFAULT_HEIGHT;
/******************************************************************************
Interpret the command line and scan the given GIF file.
******************************************************************************/
int main(int argc, char **argv)
{
int i, j, l, c, LevelStep, LogNumLevels, ErrorCode, Count = 0;
bool Error, LevelsFlag = false, SizeFlag = false, HelpFlag = false;
GifRowType Line;
ColorMapObject *ColorMap;
GifFileType *GifFile;
if ((Error = GAGetArgs(argc, argv, CtrlStr,
&GifNoisyPrint, &LevelsFlag, &NumLevels,
&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 the number of levels is power of 2 (up to 32 levels.). */
for (i = 1; i < 6; i++) if (NumLevels == (1 << i)) break;
if (i == 6) GIF_EXIT("#Lvls (-l option) is not power of 2 up to 32.");
LogNumLevels = i + 3; /* Multiple by 8 (see below). */
LevelStep = 256 / NumLevels;
/* Make sure the image dimension is a multiple of NumLevels horizontally */
/* and 7 (White, Red, Green, Blue and Yellow Cyan Magenta) vertically. */
ImageWidth = (ImageWidth / NumLevels) * NumLevels;
ImageHeight = (ImageHeight / 7) * 7;
/* 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: */
/* The color map has 7 NumLevels colors for White, Red, Green and then */
/* The secondary colors Yellow Cyan and magenta. */
if ((ColorMap = GifMakeMapObject(8 * NumLevels, NULL)) == NULL)
GIF_EXIT("Failed to allocate memory required, aborted.");
for (i = 0; i < 8; i++) /* Set color map. */
for (j = 0; j < NumLevels; j++) {
l = LevelStep * j;
c = i * NumLevels + j;
ColorMap->Colors[c].Red = (i == 0 || i == 1 || i == 4 || i == 6) * l;
ColorMap->Colors[c].Green = (i == 0 || i == 2 || i == 4 || i == 5) * l;
ColorMap->Colors[c].Blue = (i == 0 || i == 3 || i == 5 || i == 6) * l;
}
if (EGifPutScreenDesc(GifFile, ImageWidth, ImageHeight, LogNumLevels, 0, ColorMap) == GIF_ERROR) {
PrintGifError(GifFile->Error);
}
/* Dump out the image descriptor: */
if (EGifPutImageDesc(GifFile,
0, 0, ImageWidth, ImageHeight,
false, NULL) == GIF_ERROR) {
PrintGifError(GifFile->Error);
exit(EXIT_FAILURE);
}
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 to be used for all image. */
if ((Line = (GifRowType) malloc(sizeof(GifPixelType) * ImageWidth)) == NULL)
GIF_EXIT("Failed to allocate memory required, aborted.");
/* Dump the pixels: */
for (c = 0; c < 7; c++) {
for (i = 0, l = 0; i < NumLevels; i++)
for (j = 0; j < ImageWidth / NumLevels; j++)
Line[l++] = i + NumLevels * c;
for (i = 0; i < ImageHeight / 7; i++) {
if (EGifPutLine(GifFile, Line, ImageWidth) == GIF_ERROR) {
PrintGifError(GifFile->Error);
exit(EXIT_FAILURE);
}
GifQprintf("\b\b\b\b%-4d", Count++);
}
}
if (EGifCloseFile(GifFile, &ErrorCode) == GIF_ERROR) {
PrintGifError(ErrorCode);
exit(EXIT_FAILURE);
}
return 0;
}
/* end */