-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
290 lines (278 loc) · 6.58 KB
/
main.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
/*
(c) 2014-2015 Zou Wei
Website: http:\\zwcloud.net
Email: [email protected]
This code is licensed under the Apache 2.0 license. See Licence.
*/
#ifdef _DEBUG
#include <vld.h>
#endif
#include <cstdlib>
#include <cstdio>
extern "C"
{
#include "../cGIF/cGIF.h"
}
#pragma comment(lib, "../Debug/cGIF.lib")
void usage()
{
printf("Usage: cGIF [/D] <gif file path>\n"
"\tUse /D if it is a dynamic picture.\n");
}
typedef enum _PictureType {Static, Dynamic} PictureType;
int main(int argc, char* argv[])
{
if (!(argc == 2 || argc ==3))
{
usage();
return 1;
}
PictureType type = Static;
char* path = argv[1];
if (argc == 3)//imply it is a dynamic picture
{
if (strcmp("/D", argv[1]) == 0)
{
type = Dynamic;
path = argv[2];
}
else
{
usage();
return 1;
}
}
if (type == Static)
{
unsigned char* pColorIndexArray = NULL;
unsigned int canvasWidth = 0, canvasHeight = 0;
unsigned int image_position_x = 0, image_position_y = 0;
unsigned int imageWidth = 0, imageHeight = 0;
unsigned char* palette = NULL;
unsigned char palette_color_count = 0;
unsigned char background_color_index = 0;
unsigned char transparent_color_index = 0;
char flag = 0;
cGif_Error error = cGif_decode_static_indexed(
path,
&pColorIndexArray,
&canvasWidth, &canvasHeight,
&background_color_index,
&image_position_x, &image_position_y,
&imageWidth, &imageHeight,
(unsigned char**)&palette, &palette_color_count,
&transparent_color_index,
&flag);
if (error != cGif_Success)
{
printf(cGif_error_text(error));
}
else//print info of this picture
{
unsigned int i = 0, j = 0;
printf("The canvas size is %d×%d\n", canvasWidth, canvasHeight);
printf("The image is located at (%d,%d), size: %d×%d\n",
image_position_x, image_position_y,
imageWidth, imageHeight);
puts("palette:");
for (i = 0; i < palette_color_count; i++)
{
printf("%d: (0xx, 0xx, 0xx)\t",
i, *(palette + 3*i), *(palette + 3*i + 1), *(palette + 3*i + 2));
if ((i+1) % 4 == 0)
putchar('\n');
}
putchar('\n');
if (flag & 0x01)
{
printf("Background color: exist, index: 0xx\n", background_color_index);
}
else
{
puts("Background color: none");
}
if (flag & 0x01)
{
printf("Transparent color: exist, index: 0xx\n", transparent_color_index);
}
else
{
puts("Transparent color: none");
}
puts("Disposal method:");
int disposalMethod = ((flag & 0x1C) >> 2);
switch (disposalMethod)
{
case 0:
puts("No disposal specified.");
break;
case 1:
puts("Do not dispose.");
break;
case 2:
puts("Restore to background color.");
break;
case 3:
puts("Restore to previous.");
break;
default:
puts("Not defined.");
break;
}
puts("Image data(indexes in hex):");
for (i = 0; i < imageHeight; ++i)
{
for (j = 0; j < imageWidth; ++j)
{
printf("x ", *(pColorIndexArray + imageWidth*i + j));
}
putchar('\n');
}
free(pColorIndexArray);
free(palette);
}
}
else//Dynamic picture
{
unsigned canvasWidth;
unsigned canvasHeight;
unsigned char* globalPalette;
unsigned char globalPalette_color_count;
unsigned char background_color_index;
unsigned frameCount;
unsigned* frameTimeInMS = NULL;
unsigned char** ppColorIndexArray = NULL;//indexes
unsigned* image_position_x = NULL;
unsigned* image_position_y = NULL;
unsigned* imageWidth = NULL;
unsigned* imageHeight = NULL;
unsigned char** palette = NULL;
unsigned char* palette_color_count = NULL;
unsigned char* transparent_color_index = NULL;
char* flag;
char globalFlag;
cGif_Error error = cGif_decode_dynamic_indexed(
path,
&canvasWidth, &canvasHeight,
&globalPalette,
&globalPalette_color_count,
&background_color_index,
&globalFlag,
&frameCount,
&frameTimeInMS,
&ppColorIndexArray,
&image_position_x, &image_position_y,
&imageWidth, &imageHeight,
&palette, &palette_color_count,
&transparent_color_index,
&flag);
if (error != cGif_Success)
{
puts(cGif_error_text(error));
}
else//print info of this picture
{
unsigned int i = 0, j = 0, k = 0;
printf("The canvas size is %d×%d.\n", canvasWidth, canvasHeight);
if (globalPalette != nullptr)
{
puts("A global color table exists.");
if (globalFlag & 0x01)
{
puts("Using background color.");
}
else
{
puts("Not using background color.");
}
printf("The background color index is %d\n", background_color_index);
puts("Global palette:");
int colorCount = globalPalette_color_count;
if (colorCount == 0)
{
colorCount = 256;
}
for (i = 0U; i < colorCount; ++i)
{
printf("0xx: (0xx, 0xx, 0xx)\t",
i, *(globalPalette + 3 * i), *(globalPalette + 3 * i + 1), *(globalPalette + 3 * i + 2));
if ((i + 1) % 4 == 0)
putchar('\n');
}
}
else
{
puts("No global palette.");
}
putchar('\n');
puts("Per frame info:");
for (i = 0; i < frameCount; ++i)
{
unsigned char* pColorIndexArray = ppColorIndexArray[i];
printf("--Frame %d--\n", i);
printf("The image is located at (%d,%d), size: %d×%d\n",
image_position_x[i], image_position_y[i],
imageWidth[i], imageHeight[i]);
if (palette[i] == nullptr)
{
puts("No frame palette.");
}
else
{
int colorCount = palette_color_count[i];
if (colorCount == 0)
{
colorCount = 256;
}
puts("Frame palette:");
for (j = 0U; j < colorCount; j++)
{
printf("0xx: (0xx, 0xx, 0xx)\t",
j, *(palette[i] + 3 * j), *(palette[i] + 3 * j + 1), *(palette[i] + 3 * j + 2));
if ((j + 1) % 4 == 0)
putchar('\n');
}
putchar('\n');
}
if (flag[i] & 0x01)
{
printf("Using transparent color, the index is 0xx\n", transparent_color_index[i]);
}
else
{
puts("Not using transparent color.");
}
puts("Disposal method:");
int disposalMethod = ((flag[i] & 0x1C) >> 2);
switch (disposalMethod)
{
case 0:
puts("No disposal specified.");
break;
case 1:
puts("Do not dispose.");
break;
case 2:
puts("Restore to background color.");
break;
case 3:
puts("Restore to previous.");
break;
default:
puts("Not defined.");
break;
}
puts("Image data(indexes in hex):");
for (j = 0; j < imageHeight[i]; ++j)
{
for (k = 0; k < imageWidth[i]; ++k)
{
printf("x ", *(pColorIndexArray + j*imageWidth[i] + k));
}
putchar('\n');
}
putchar('\n');
}
}
}//END of dynamic picture
}