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
|
/**
* @file lv_test_assert.c
*
* Copyright 2002-2010 Guillaume Cottenceau.
*
* This software may be freely redistributed under the terms
* of the X11 license.
*
*/
/*********************
* INCLUDES
*********************/
#if LV_BUILD_TEST
#include "../lvgl.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "unity.h"
#define PNG_DEBUG 3
#include <png.h>
/*********************
* DEFINES
*********************/
//#define REF_IMGS_PATH "lvgl/tests/lv_test_ref_imgs/"
#define REF_IMGS_PATH "ref_imgs/"
#define ERR_FILE_NOT_FOUND -1
#define ERR_PNG -2
/**********************
* TYPEDEFS
**********************/
typedef struct {
int width, height;
png_byte color_type;
png_byte bit_depth;
png_structp png_ptr;
png_infop info_ptr;
int number_of_passes;
png_bytep * row_pointers;
} png_image_t;
/**********************
* STATIC PROTOTYPES
**********************/
static int read_png_file(png_image_t * p, const char * file_name);
static int write_png_file(void * raw_img, uint32_t width, uint32_t height, char * file_name);
static void png_release(png_image_t * p);
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
bool lv_test_assert_image_eq(const char * fn_ref)
{
char fn_ref_full[512];
sprintf(fn_ref_full, "%s%s", REF_IMGS_PATH, fn_ref);
//lv_obj_invalidate(lv_screen_active());
lv_refr_now(NULL);
extern uint8_t * last_flushed_buf;
uint8_t * screen_buf = lv_draw_buf_align(last_flushed_buf, LV_COLOR_FORMAT_XRGB8888);
png_image_t p;
int res = read_png_file(&p, fn_ref_full);
if(res == ERR_FILE_NOT_FOUND) {
TEST_PRINTF("%s%s", fn_ref_full, " was not found, creating is now from the rendered screen");
fflush(stderr);
write_png_file(screen_buf, 800, 480, fn_ref_full);
return true;
}
else if(res == ERR_PNG) {
return false;
}
uint8_t * ptr_act = NULL;
const png_byte * ptr_ref = NULL;
bool err = false;
int x, y, i_buf = 0;
for(y = 0; y < p.height; y++) {
png_byte * row = p.row_pointers[y];
for(x = 0; x < p.width; x++) {
ptr_ref = &(row[x * 3]);
ptr_act = &(screen_buf[i_buf * 4]);
uint32_t ref_px = 0;
uint32_t act_px = 0;
memcpy(&ref_px, ptr_ref, 3);
memcpy(&act_px, ptr_act, 3);
uint8_t act_swap[3] = {ptr_act[2], ptr_act[1], ptr_act[0]};
if(memcmp(act_swap, ptr_ref, 3) != 0) {
TEST_PRINTF("Error on x:%d, y:%d. Expected %X, Actual %X", x, y, ref_px, act_px);
fflush(stderr);
err = true;
break;
}
i_buf++;
}
if(err) break;
}
if(err) {
char fn_ref_no_ext[64];
strcpy(fn_ref_no_ext, fn_ref);
fn_ref_no_ext[strlen(fn_ref_no_ext) - 4] = '\0';
char fn_err_full[512];
sprintf(fn_err_full, "%s%s_err.png", REF_IMGS_PATH, fn_ref_no_ext);
write_png_file(screen_buf, 800, 480, fn_err_full);
}
png_release(&p);
fflush(stdout);
return !err;
}
/**********************
* STATIC FUNCTIONS
**********************/
static int read_png_file(png_image_t * p, const char * file_name)
{
char header[8]; // 8 is the maximum size that can be checked
/*open file and test for it being a png*/
FILE * fp = fopen(file_name, "rb");
if(!fp) {
TEST_PRINTF("[read_png_file %s] could not be opened for reading", file_name);
return ERR_FILE_NOT_FOUND;
}
size_t rcnt = fread(header, 1, 8, fp);
if(rcnt != 8 || png_sig_cmp((png_const_bytep)header, 0, 8)) {
TEST_PRINTF("[read_png_file %s] not recognized as a PNG file", file_name);
return ERR_PNG;
}
/*initialize stuff*/
p->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!p->png_ptr) {
TEST_PRINTF("[read_png_file %s] png_create_read_struct failed", file_name);
return ERR_PNG;
}
p->info_ptr = png_create_info_struct(p->png_ptr);
if(!p->info_ptr) {
TEST_PRINTF("[read_png_file %s] png_create_info_struct failed", file_name);
return ERR_PNG;
}
if(setjmp(png_jmpbuf(p->png_ptr))) {
TEST_PRINTF("[read_png_file %s] Error during init_io", file_name);
return ERR_PNG;
}
png_init_io(p->png_ptr, fp);
png_set_sig_bytes(p->png_ptr, 8);
png_read_info(p->png_ptr, p->info_ptr);
p->width = png_get_image_width(p->png_ptr, p->info_ptr);
p->height = png_get_image_height(p->png_ptr, p->info_ptr);
p->color_type = png_get_color_type(p->png_ptr, p->info_ptr);
p->bit_depth = png_get_bit_depth(p->png_ptr, p->info_ptr);
p->number_of_passes = png_set_interlace_handling(p->png_ptr);
png_read_update_info(p->png_ptr, p->info_ptr);
/*read file*/
if(setjmp(png_jmpbuf(p->png_ptr))) {
TEST_PRINTF("[read_png_file %s] Error during read_image", file_name);
return ERR_PNG;
}
p->row_pointers = (png_bytep *) malloc(sizeof(png_bytep) * p->height);
int y;
for(y = 0; y < p->height; y++)
p->row_pointers[y] = (png_byte *) malloc(png_get_rowbytes(p->png_ptr, p->info_ptr));
png_read_image(p->png_ptr, p->row_pointers);
fclose(fp);
return 0;
}
static int write_png_file(void * raw_img, uint32_t width, uint32_t height, char * file_name)
{
png_structp png_ptr;
png_infop info_ptr;
/* create file */
FILE * fp = fopen(file_name, "wb");
if(!fp) {
printf("###### %s\n", file_name);
fflush(stdout);
TEST_PRINTF("[write_png_file %s] could not be opened for writing", file_name);
TEST_PRINTF("%s", file_name);
return -1;
}
/* initialize stuff */
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!png_ptr) {
TEST_PRINTF("[write_png_file %s] png_create_write_struct failed", file_name);
return -1;
}
info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr) {
TEST_PRINTF("[write_png_file %s] png_create_info_struct failed", file_name);
return -1;
}
if(setjmp(png_jmpbuf(png_ptr))) {
TEST_PRINTF("[write_png_file %s] Error during init_io", file_name);
return -1;
}
png_init_io(png_ptr, fp);
/* write header */
if(setjmp(png_jmpbuf(png_ptr))) {
TEST_PRINTF("[write_png_file %s] Error during writing header", file_name);
return -1;
}
png_set_IHDR(png_ptr, info_ptr, width, height,
8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
/* write bytes */
if(setjmp(png_jmpbuf(png_ptr))) {
TEST_PRINTF("[write_png_file %s] Error during writing bytes", file_name);
return -1;
}
uint8_t * raw_img8 = (uint8_t *)raw_img;
png_bytep * row_pointers = (png_bytep *) malloc(sizeof(png_bytep) * height);
for(uint32_t y = 0; y < height; y++) {
row_pointers[y] = malloc(3 * width);
uint8_t * line = raw_img8 + y * width * 4;
for(uint32_t x = 0; x < width; x++) {
row_pointers[y][x * 3 + 0] = line[x * 4 + 2];
row_pointers[y][x * 3 + 1] = line[x * 4 + 1];
row_pointers[y][x * 3 + 2] = line[x * 4 + 0];
}
}
png_write_image(png_ptr, row_pointers);
/* end write */
if(setjmp(png_jmpbuf(png_ptr))) {
TEST_PRINTF("[write_png_file %s] Error during end of write", file_name);
return -1;
}
png_write_end(png_ptr, NULL);
/* cleanup heap allocation */
for(uint32_t y = 0; y < height; y++) free(row_pointers[y]);
free(row_pointers);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return 0;
}
static void png_release(png_image_t * p)
{
int y;
for(y = 0; y < p->height; y++) free(p->row_pointers[y]);
free(p->row_pointers);
png_destroy_read_struct(&p->png_ptr, &p->info_ptr, NULL);
}
#endif
|