-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathGifAnalyzer.cpp
301 lines (251 loc) Β· 9.57 KB
/
GifAnalyzer.cpp
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
//
// Created by succlz123 on 2017/11/4.
//
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "GifAnalyzer.h"
#include "Logger.h"
using namespace blk;
static const size_t BUF_SIZE = 1024;
static void analyzeError() {
fprintf(stderr, "Parse failed\n");
exit(1);
}
static void readNbytes(FILE *file, uint8_t *buf, size_t nbytes) {
size_t retCode = fread(buf, 1, nbytes, file);
if (retCode == nbytes) {
buf[nbytes] = 0;
} else {
analyzeError();
}
}
static void analyzeColorTable(FILE *file, unsigned table_size) {
uint8_t buf[BUF_SIZE] = {0};
printf("%8s%8s%8s%8s\n", "INDEX", "RED", "GREEN", "BLUE");
printf("--------------------------------\n");
for (unsigned index = 0; index < table_size; ++index) {
readNbytes(file, buf, 3);
printf("%8u%8u%8u%8u\n", index, buf[0], buf[1], buf[2]);
}
}
static unsigned analyzeDataBlock(FILE *file) {
unsigned totalSize = 0;
uint8_t buf[BUF_SIZE] = {0};
for (;;) {
readNbytes(file, buf, 1);
unsigned blockSize = *buf;
if (blockSize) {
readNbytes(file, buf, blockSize);
totalSize += blockSize;
} else {
break;
}
}
if (totalSize) {
printf("Data Block Size: %u (%.2fKB)\n", totalSize, (float) totalSize / 1024);
}
return totalSize;
}
static void analyzeApplicationExtension(FILE *file) {
uint8_t buf[BUF_SIZE] = {0};
readNbytes(file, buf, 1);
unsigned blockSize = *buf;
if (blockSize != 11) {
analyzeError();
}
readNbytes(file, buf, 8);
const char *applicationIdentifier = (const char *) buf;
printf("Application Identifier: %s\n", applicationIdentifier);
int isNetscapeExtension = memcmp(applicationIdentifier, "NETSCAPE", strlen(applicationIdentifier)) == 0;
readNbytes(file, buf, 3);
printf("Application Authentication Code: 0x%02X%02X%02X\n", buf[0], buf[1], buf[2]);
if (isNetscapeExtension) {
uint8_t buf[BUF_SIZE] = {0};
readNbytes(file, buf, 1);
unsigned blockSize = *buf;
if (blockSize != 3) {
analyzeError();
}
readNbytes(file, buf, 1);
unsigned loopEnabled = *buf;
printf("Loop Enabled: %u\n", loopEnabled);
readNbytes(file, buf, 2);
unsigned loopCount = buf[0] + (buf[1] << 8);
printf("Loop Count: %u\n", loopCount);
}
analyzeDataBlock(file);
}
static void analyzeGraphicControlExtension(FILE *file) {
uint8_t buf[BUF_SIZE] = {0};
readNbytes(file, buf, 1);
unsigned blockSize = *buf;
if (blockSize != 4) {
analyzeError();
}
readNbytes(file, buf, 1);
unsigned disposalMethod = (*buf & 0b00011100) >> 2;
printf("Disposal Method: %u\n", disposalMethod);
unsigned userInputFlag = (*buf & 0b00000010) >> 1;
printf("User Input Flag: %u\n", userInputFlag);
unsigned transparentColorFlag = *buf & 0b00000001;
printf("Transparent Color Flag: %u\n", transparentColorFlag);
readNbytes(file, buf, 2);
unsigned delayTime = buf[0] + (buf[1] << 8);
printf("Delay Time: %u (%.2fs)\n", delayTime, (float) delayTime / 100);
readNbytes(file, buf, 1);
unsigned transparencyIndex = *buf;
printf("Transparency Index: %u\n", transparencyIndex);
analyzeDataBlock(file);
}
static void analyzeCommentExtension(FILE *file) {
analyzeDataBlock(file);
}
static void analyzeImage(FILE *file, unsigned rawImageWidth, unsigned rawImageHeight) {
uint8_t buf[BUF_SIZE] = {0};
readNbytes(file, buf, 2);
unsigned imageLeftPosition = buf[0] + (buf[1] << 8);
printf("Image left Position: %u\n", imageLeftPosition);
readNbytes(file, buf, 2);
unsigned imageTopPosition = buf[0] + (buf[1] << 8);
printf("Image Top Position: %u\n", imageTopPosition);
readNbytes(file, buf, 2);
unsigned imageWidth = buf[0] + (buf[1] << 8);
printf("Image Width: %u\n", imageWidth);
readNbytes(file, buf, 2);
unsigned imageHeight = buf[0] + (buf[1] << 8);
printf("Image Height: %u\n", imageHeight);
readNbytes(file, buf, 1);
unsigned localColorTableFlag = (*buf & 0b10000000) >> 7;
printf("Local Color Table Flag: %u\n", localColorTableFlag);
unsigned interlaceFlag = (*buf & 0b01000000) >> 6;
printf("Interlace Flag: %u\n", interlaceFlag);
unsigned sortFlag = (*buf & 0b00100000) >> 5;
printf("Sort Flag: %u\n", sortFlag);
unsigned sizeOfLocalColorTable = *buf & 0b00000111;
unsigned realSizeOfLocalColorTable = 2 << sizeOfLocalColorTable;
printf("Size of Local Color Table: %u (%u)\n", sizeOfLocalColorTable, realSizeOfLocalColorTable);
if (localColorTableFlag) {
printf("Local Color Table:\n");
printf("\n");
analyzeColorTable(file, realSizeOfLocalColorTable);
printf("\n");
}
readNbytes(file, buf, 1);
unsigned lzwMinimumCodeSize = *buf;
printf("LZW Minimum Code Size: %u\n", lzwMinimumCodeSize);
unsigned rawSizeImageDataLength = rawImageWidth * rawImageHeight * lzwMinimumCodeSize / 8;
unsigned uncompressedImageDataLength = imageWidth * imageHeight * lzwMinimumCodeSize / 8;
unsigned compressedImageDataLength = analyzeDataBlock(file);
printf("Compression Rate: %.2f (%.2f)\n",
(float) uncompressedImageDataLength / compressedImageDataLength,
(float) rawSizeImageDataLength / compressedImageDataLength);
}
static void analyzeGif(FILE *file) {
uint8_t buf[BUF_SIZE] = {0};
// fixed-length header
printf("================================\n");
printf("Header\n");
printf("================================\n");
printf("\n");
readNbytes(file, buf, 3);
printf("Signature: %s\n", buf);
readNbytes(file, buf, 3);
printf("Version: %s\n", buf);
printf("\n");
// fixed-length Logical Screen Descriptor
printf("================================\n");
printf("Logical Screen Descriptor\n");
printf("================================\n");
printf("\n");
readNbytes(file, buf, 2);
unsigned logicalScreenWidth = buf[0] + (buf[1] << 8);
printf("Logical Screen Width: %u\n", logicalScreenWidth);
readNbytes(file, buf, 2);
unsigned logicalScreenHeight = buf[0] + (buf[1] << 8);
printf("Logical Screen Height: %u\n", logicalScreenHeight);
readNbytes(file, buf, 1);
unsigned globalColorTableFlag = (*buf & 0b10000000) >> 7;
printf("Global Color Table Flag: %u\n", globalColorTableFlag);
unsigned colorResolution = (*buf & 0b01110000) >> 4;
unsigned realColorResolution = colorResolution + 1;
printf("Color Resolution: %u (%u)\n", colorResolution, realColorResolution);
unsigned sortFlag = (*buf & 0b00001000) >> 3;
printf("Sort Flag: %u\n", sortFlag);
unsigned sizeOfGlobalColorTable = *buf & 0b00000111;
unsigned realSizeOfGlobalColorTable = 2 << sizeOfGlobalColorTable;
printf("Size of Global Color Table: %u (%u)\n", sizeOfGlobalColorTable, realSizeOfGlobalColorTable);
readNbytes(file, buf, 1);
unsigned backgroundColorIndex = *buf;
printf("Background Color Index: %u\n", backgroundColorIndex);
readNbytes(file, buf, 1);
unsigned pixelAspectRatio = *buf;
double realPixelAspectRatio = pixelAspectRatio ? ((double) pixelAspectRatio + 15.0) / 64.0 : 1.0;
printf("RGB Aspect Ratio: %u (%lf)\n", pixelAspectRatio, realPixelAspectRatio);
unsigned rawImageDataLength = realColorResolution * logicalScreenWidth * logicalScreenHeight / 8;
printf("Raw Image Size: %u (%.2fKB)\n", rawImageDataLength, (float) rawImageDataLength / 1024);
if (globalColorTableFlag) {
printf("Local Color Table:\n");
printf("\n");
analyzeColorTable(file, realSizeOfGlobalColorTable);
printf("\n");
}
printf("\n");
// Data Stream
printf("================================\n");
printf("Data Stream\n");
printf("================================\n");
printf("\n");
for (;;) {
readNbytes(file, buf, 1);
unsigned identifier = *buf;
switch (identifier) {
case 0x21: {
readNbytes(file, buf, 1);
unsigned extensionLabel = *buf;
switch (extensionLabel) {
case 0xFF:
printf("# Application Extension\n");
analyzeApplicationExtension(file);
printf("\n");
break;
case 0xF9:
printf("# Graphic Control Extension\n");
analyzeGraphicControlExtension(file);
printf("\n");
break;
case 0xFE:
printf("# Comment Extension\n");
analyzeCommentExtension(file);
printf("\n");
break;
default:
printf("# Unknown Extension: 0x%02X\n", extensionLabel);
break;
}
break;
}
case 0x2C: {
printf("# Image\n");
analyzeImage(file, logicalScreenWidth, logicalScreenHeight);
printf("\n");
break;
}
case 0x3B:
printf("# Trailer\n");
return;
default:
printf("# Unknown Block: 0x%02X\n", identifier);
break;
}
}
}
void GifAnalyzer::showGifInfo(const char *path) {
FILE *file = fopen(path, "rb");
if (!file) {
Logger::log(true, "GifAnalyzer : Could not open the file");
exit(1);
}
analyzeGif(file);
fclose(file);
}