forked from StevensDeptECE/CPE553-CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
359 lines (316 loc) · 9.78 KB
/
test.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#if defined(__MINGW32__)
/* Use Gnu-style printf and scanf - see: https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/ */
#define __USE_MINGW_ANSI_STDIO 1
#endif
#if !defined(_MSC_VER)
/* Use inttypes.h format macros for greater portability */
#include <inttypes.h>
#endif
#include <flif.h>
#include <stdlib.h>
#include <stdio.h>
#pragma pack(push,1)
typedef struct RGBA
{
uint8_t r,g,b,a;
} RGBA;
#pragma pack(pop)
/* based partly on:
https://stackoverflow.com/questions/1546789/clean-code-to-printf-size-t-in-c-or-nearest-equivalent-of-c99s-z-in-c
*/
/*
For Windows, we use the 64-bit api variants of function calls
to accomodate larger files instead of using the standard C
versions so the result will be same size for both 32-bit and
64-bit versions of Windows. The size_t grows from 32-bits in
Windows to 64-bits.
Making this WORSE is that older versions of the Microsoft C
run-time-library do NOT use the standard GNU identifiers so I had
to barrow constants from:
https://code.google.com/archive/p/msinttypes/
*/
#if defined(_MSC_VER)
#define F_I_FILE_SIZE "%lu"
#if defined(_WIN64)
#define F_BLOB_SIZE_T "%I64u"
#else
#define F_BLOB_SIZE_T "PRIu32"
#endif
#elif defined(__GNUC__)
#if defined(__MINGW32__)
#define F_I_FILE_SIZE PRId64
#define F_BLOB_SIZE_T PRIuPTR
#else
#define F_I_FILE_SIZE PRIiPTR
#define F_BLOB_SIZE_T PRIuPTR
#endif
#endif
void fill_dummy_image(FLIF_IMAGE* image)
{
uint32_t w = flif_image_get_width(image);
uint32_t h = flif_image_get_height(image);
RGBA* row = (RGBA*)malloc(w * sizeof(RGBA));
if(row)
{
uint32_t y;
for(y = 0; y < h; ++y)
{
uint32_t x;
for(x = 0; x < w; ++x)
{
row[x].r = (x+y) % 255;
row[x].g = (x) % 255;
row[x].b = (y) % 255;
row[x].a = 255;
}
flif_image_write_row_RGBA8(image, y, row, w * sizeof(RGBA));
}
free(row);
}
}
int compare_images(FLIF_IMAGE* image1, FLIF_IMAGE* image2)
{
int result = 0;
uint32_t w1 = flif_image_get_width(image1);
uint32_t h1 = flif_image_get_height(image1);
uint32_t w2 = flif_image_get_width(image2);
uint32_t h2 = flif_image_get_height(image2);
if(w1 != w2 || h1 != h2)
{
printf("Error: Images have different width/height\n");
result = 1;
}
RGBA* row1 = (RGBA*)malloc(w1 * sizeof(RGBA));
if(row1 == 0)
{
printf("Error: Out of memory\n");
result = 1;
}
else
{
RGBA* row2 = (RGBA*)malloc(w1 * sizeof(RGBA));
if(row2 == 0)
{
printf("Error: Out of memory\n");
result = 1;
}
else
{
int difference = 0;
uint32_t y;
for(y = 0; y < h1; ++y)
{
flif_image_read_row_RGBA8(image1, y, row1, w1 * sizeof(RGBA));
flif_image_read_row_RGBA8(image2, y, row2, w2 * sizeof(RGBA));
uint32_t x;
for(x = 0; x < w1; ++x)
{
if( row1[x].r != row2[x].r ||
row1[x].g != row2[x].g ||
row1[x].b != row2[x].b ||
row1[x].a != row2[x].a)
{
// stop flooding the log if the image has many differences
if(difference < 20)
{
printf("Error: Color difference at %u,%u: %02X%02X%02X%02X -> %02X%02X%02X%02X\n", x, y, row1[x].r, row1[x].g, row1[x].b, row1[x].a, row2[x].r, row2[x].g, row2[x].b, row2[x].a);
result = 1;
}
difference++;
}
}
}
free(row2);
}
free(row1);
}
return result;
}
int compare_file_and_blob(const void* blob, size_t blob_size, const char* filename)
{
int result = 0;
FILE* f = fopen(filename, "rb");
if(f)
{
const uint8_t* blob_ptr = (const uint8_t*)blob;
fseek(f, 0, SEEK_END);
#ifdef _WIN32
int64_t file_size = _ftelli64(f);
fseek(f, 0, SEEK_SET);
if(file_size != (int64_t)blob_size)
#else
long int file_size = ftell(f);
fseek(f, 0, SEEK_SET);
if(file_size != (long int)blob_size)
#endif
{
printf("Error: flif file size: %"F_I_FILE_SIZE" <---> flif blob size: %"F_BLOB_SIZE_T"\n", file_size, blob_size);
result = 1;
}
else
{
size_t i;
for(i = 0; i < blob_size; ++i)
{
int c = fgetc(f);
if(c == EOF)
{
printf("IO error: premature EOF at %" F_BLOB_SIZE_T "\n", i);
result = 1;
break;
}
if(c != *(blob_ptr + i))
{
printf("Error: file and blob do not match at %" F_BLOB_SIZE_T "\n", i);
result = 1;
break;
}
}
}
fclose(f);
}
return result;
}
int main(int argc, char** argv)
{
if (argc < 2)
{
printf("first argument must be a file path for the test image");
return 1;
}
int result = 0;
const size_t WIDTH = 256;
const size_t HEIGHT = 256;
const char* dummy_file = argv[1];
FLIF_IMAGE* im = flif_create_image(WIDTH, HEIGHT);
if(im == 0)
{
printf("Error: flif_create_image failed\n");
result = 1;
}
else
{
fill_dummy_image(im);
void* blob = 0;
size_t blob_size = 0;
FLIF_ENCODER* e = flif_create_encoder();
if(e)
{
flif_encoder_set_interlaced(e, 1);
flif_encoder_set_learn_repeat(e, 3);
flif_encoder_set_auto_color_buckets(e, 1);
flif_encoder_set_palette_size(e, 512);
flif_encoder_set_lookback(e, 1);
flif_encoder_add_image(e, im);
if(!flif_encoder_encode_file(e, dummy_file))
{
printf("Error: encoding file failed\n");
result = 1;
}
flif_destroy_encoder(e);
e = 0;
}
e = flif_create_encoder();
if(e)
{
flif_encoder_set_interlaced(e, 1);
flif_encoder_set_learn_repeat(e, 3);
flif_encoder_set_auto_color_buckets(e, 1);
flif_encoder_set_palette_size(e, 512);
flif_encoder_set_lookback(e, 1);
flif_encoder_add_image(e, im);
if(!flif_encoder_encode_memory(e, &blob, &blob_size))
{
printf("Error: encoding blob failed\n");
result = 1;
}
// TODO: uncommenting this causes subsequent test to fail???
/*if(!compare_file_and_blob(blob, blob_size, dummy_file))
{
result = 1;
}*/
flif_destroy_encoder(e);
e = 0;
}
FLIF_DECODER* d = flif_create_decoder();
if(d)
{
flif_decoder_set_quality(d, 100);
flif_decoder_set_scale(d, 1);
{
if(!flif_decoder_decode_file(d, dummy_file))
{
printf("Error: decoding file failed\n");
result = 1;
}
FLIF_IMAGE* decoded = flif_decoder_get_image(d, 0);
if(decoded == 0)
{
printf("Error: No decoded image found\n");
result = 1;
}
else if(compare_images(im, decoded) != 0)
{
result = 1;
}
}
{
if(!flif_decoder_decode_memory(d, blob, blob_size))
{
printf("Error: decoding memory failed\n");
result = 1;
}
FLIF_IMAGE* decoded = flif_decoder_get_image(d, 0);
if(decoded == 0)
{
printf("Error: No decoded image found\n");
result = 1;
}
else if(compare_images(im, decoded) != 0)
{
result = 1;
}
}
flif_destroy_decoder(d);
d = 0;
}
FLIF_INFO* info = flif_read_info_from_memory(blob, blob_size);
if(info)
{
int w = flif_info_get_width(info);
int h = flif_info_get_height(info);
int channels = flif_info_get_nb_channels(info);
int depth = flif_info_get_depth(info);
int n = flif_info_num_images(info);
if((size_t) w != WIDTH ||
(size_t) h != HEIGHT ||
channels != 4 ||
depth != 8 ||
n != 1)
{
printf("Error: info should be %"F_BLOB_SIZE_T"%"F_BLOB_SIZE_T", %d channels, %d bit, %d images.\n"
" Instead it is %dx%d, %d channels, %d bit, %d images.\n",
WIDTH, HEIGHT, 4, 8, 1,
w, h, channels, depth, n);
result = 1;
}
flif_destroy_info(info);
info = 0;
}
else
{
printf("Error: flif_read_info_from_memory failed\n");
result = 1;
}
flif_destroy_image(im);
im = 0;
if(blob)
{
flif_free_memory(blob);
blob = 0;
}
}
if (result) printf("interface test has FAILED.\n");
else printf("interface test has succeeded.\n");
return result;
}