-
Notifications
You must be signed in to change notification settings - Fork 3
/
gifhisto.c
259 lines (222 loc) · 7.92 KB
/
gifhisto.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
/*****************************************************************************
gifhisto - make a color histogram from image color frequencies
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 "gifhisto"
#define DEFAULT_HISTO_WIDTH 100 /* Histogram image diemnsions. */
#define DEFAULT_HISTO_HEIGHT 256
#define HISTO_BITS_PER_PIXEL 2 /* Size of bitmap for histogram GIF. */
static char
*VersionStr =
PROGRAM_NAME
VERSION_COOKIE
" Gershon Elber, "
__DATE__ ", " __TIME__ "\n"
"(C) Copyright 1989 Gershon Elber.\n";
static char
*CtrlStr =
PROGRAM_NAME
" v%- t%- s%-Width|Height!d!d n%-ImageNumber!d b%- h%- GifFile!*s";
static int
ImageWidth = DEFAULT_HISTO_WIDTH,
ImageHeight = DEFAULT_HISTO_HEIGHT,
ImageN = 1;
static GifColorType
HistoColorMap[] = { /* Constant bit map for histograms: */
{ 0, 0, 0 },
{ 255, 0, 0 },
{ 0, 255, 0 },
{ 0, 0, 255 }
};
static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
/******************************************************************************
Interpret the command line and scan the given GIF file.
******************************************************************************/
int main(int argc, char **argv)
{
int i, j, ErrorCode, NumFiles, ExtCode, CodeSize, NumColors = 2, ImageNum = 0;
bool Error, TextFlag = false, SizeFlag = false,
ImageNFlag = false, BackGroundFlag = false, HelpFlag = false;
long Histogram[256];
GifRecordType RecordType;
GifByteType *Extension, *CodeBlock;
char **FileName = NULL;
GifRowType Line;
GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
/* Same image dimension vars for both Image & ImageN as only one allowed */
if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifNoisyPrint,
&TextFlag, &SizeFlag, &ImageWidth, &ImageHeight,
&ImageNFlag, &ImageN, &BackGroundFlag,
&HelpFlag, &NumFiles, &FileName)) != false ||
(NumFiles > 1 && !HelpFlag)) {
if (Error)
GAPrintErrMsg(Error);
else if (NumFiles > 1)
GIF_MESSAGE("Error in command line parsing - one GIF file please.");
GAPrintHowTo(CtrlStr);
exit(EXIT_FAILURE);
}
if (HelpFlag) {
(void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR);
GAPrintHowTo(CtrlStr);
exit(EXIT_SUCCESS);
}
if (NumFiles == 1) {
if ((GifFileIn = DGifOpenFileName(*FileName, &ErrorCode)) == NULL) {
PrintGifError(ErrorCode);
exit(EXIT_FAILURE);
}
}
else {
/* Use stdin instead: */
if ((GifFileIn = DGifOpenFileHandle(0, &ErrorCode)) == NULL) {
PrintGifError(ErrorCode);
exit(EXIT_FAILURE);
}
}
for (i = 0; i < 256; i++) Histogram[i] = 0; /* Reset counters. */
/* Scan the content of the GIF file and load the image(s) in: */
do {
if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
switch (RecordType) {
case IMAGE_DESC_RECORD_TYPE:
if (DGifGetImageDesc(GifFileIn) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
if (GifFileIn->Image.ColorMap)
NumColors = GifFileIn->Image.ColorMap->ColorCount;
else if (GifFileIn->SColorMap)
NumColors = GifFileIn->SColorMap->ColorCount;
else
GIF_EXIT("Neither Screen nor Image color map exists.");
if ((ImageHeight / NumColors) * NumColors != ImageHeight)
GIF_EXIT("Image height specified not dividable by #colors.");
if (++ImageNum == ImageN) {
/* This is the image we should make histogram for: */
Line = (GifRowType) malloc(GifFileIn->Image.Width *
sizeof(GifPixelType));
GifQprintf("\n%s: Image %d at (%d, %d) [%dx%d]: ",
PROGRAM_NAME, ImageNum,
GifFileIn->Image.Left, GifFileIn->Image.Top,
GifFileIn->Image.Width, GifFileIn->Image.Height);
for (i = 0; i < GifFileIn->Image.Height; i++) {
if (DGifGetLine(GifFileIn, Line, GifFileIn->Image.Width)
== GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
for (j = 0; j < GifFileIn->Image.Width; j++)
Histogram[Line[j]]++;
GifQprintf("\b\b\b\b%-4d", i);
}
free((char *) Line);
}
else {
/* Skip the image: */
/* Now read image itself in decoded form as we dont */
/* really care what is there, and this is much faster. */
if (DGifGetCode(GifFileIn, &CodeSize, &CodeBlock) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
while (CodeBlock != NULL)
if (DGifGetCodeNext(GifFileIn, &CodeBlock) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
}
break;
case EXTENSION_RECORD_TYPE:
/* Skip any extension blocks in file: */
if (DGifGetExtension(GifFileIn, &ExtCode, &Extension) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
while (Extension != NULL) {
if (DGifGetExtensionNext(GifFileIn, &Extension) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
}
break;
case TERMINATE_RECORD_TYPE:
break;
default: /* Should be trapped by DGifGetRecordType. */
break;
}
}
while (RecordType != TERMINATE_RECORD_TYPE);
/* We requested suppression of the background count: */
if (BackGroundFlag) Histogram[GifFileIn->SBackGroundColor] = 0;
if (DGifCloseFile(GifFileIn, &ErrorCode) == GIF_ERROR)
{
PrintGifError(ErrorCode);
exit(EXIT_FAILURE);
}
/* We may required to dump out the histogram as text file: */
if (TextFlag) {
for (i = 0; i < NumColors; i++)
printf("%12ld %3d\n", Histogram[i], i);
}
else {
int Color, Count;
long Scaler;
/* Open stdout for the histogram output file: */
if ((GifFileOut = EGifOpenFileHandle(1, &ErrorCode)) == NULL) {
PrintGifError(ErrorCode);
exit(EXIT_FAILURE);
}
/* Dump out screen descriptor to fit histogram dimensions: */
if (EGifPutScreenDesc(GifFileOut,
ImageWidth, ImageHeight, HISTO_BITS_PER_PIXEL, 0,
GifMakeMapObject(4, HistoColorMap)) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
/* Dump out image descriptor to fit histogram dimensions: */
if (EGifPutImageDesc(GifFileOut,
0, 0, ImageWidth, ImageHeight, false, NULL) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
/* Prepare scan line for histogram file, and find scaler to scale */
/* histogram to be between 0 and ImageWidth: */
Line = (GifRowType) malloc(ImageWidth * sizeof(GifPixelType));
for (Scaler = 0, i = 0; i < NumColors; i++) if (Histogram[i] > Scaler)
Scaler = Histogram[i];
Scaler /= ImageWidth;
if (Scaler == 0) Scaler = 1; /* In case maximum is less than width. */
/* Dump out the image itself: */
for (Count = ImageHeight, i = 0, Color = 1; i < NumColors; i++) {
int Size;
if ((Size = Histogram[i] / Scaler) > ImageWidth) Size = ImageWidth;
for (j = 0; j < Size; j++)
Line[j] = Color;
for (j = Size; j < ImageWidth; j++)
Line[j] = GifFileOut->SBackGroundColor;
/* Move to next color: */
if (++Color >= (1 << HISTO_BITS_PER_PIXEL)) Color = 1;
/* Dump this histogram entry as many times as required: */
for (j = 0; j < ImageHeight / NumColors; j++) {
if (EGifPutLine(GifFileOut, Line, ImageWidth) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
GifQprintf("\b\b\b\b%-4d", Count--);
}
}
if (EGifCloseFile(GifFileOut, &ErrorCode) == GIF_ERROR)
{
PrintGifError(ErrorCode);
exit(EXIT_FAILURE);
}
}
return 0;
}
/******************************************************************************
Close both input and output file (if open), and exit.
******************************************************************************/
static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
{
if (GifFileIn != NULL) {
PrintGifError(GifFileIn->Error);
EGifCloseFile(GifFileIn, NULL);
}
if (GifFileOut != NULL) {
PrintGifError(GifFileOut->Error);
EGifCloseFile(GifFileOut, NULL);
}
exit(EXIT_FAILURE);
}
/* end */