acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

unity_support.c (5784B)

      1 /**
      2  * @file lv_test_assert.c
      3  *
      4  * Copyright 2002-2010 Guillaume Cottenceau.
      5  *
      6  * This software may be freely redistributed under the terms
      7  * of the X11 license.
      8  *
      9  */
     10 
     11 /*********************
     12  *      INCLUDES
     13  *********************/
     14 #if LV_BUILD_TEST
     15 #include "../lvgl.h"
     16 
     17 #include <unistd.h>
     18 #include <stdlib.h>
     19 #include <stdio.h>
     20 #include <string.h>
     21 #include <stdarg.h>
     22 #include "unity.h"
     23 #define PNG_DEBUG 3
     24 #include <png.h>
     25 
     26 /*********************
     27  *      DEFINES
     28  *********************/
     29 //#define REF_IMGS_PATH "lvgl/tests/lv_test_ref_imgs/"
     30 #define REF_IMGS_PATH "ref_imgs/"
     31 
     32 /**********************
     33  *      TYPEDEFS
     34  **********************/
     35 typedef struct {
     36     int width, height;
     37     png_byte color_type;
     38     png_byte bit_depth;
     39 
     40     png_structp png_ptr;
     41     png_infop info_ptr;
     42     int number_of_passes;
     43     png_bytep * row_pointers;
     44 }png_img_t;
     45 
     46 /**********************
     47  *  STATIC PROTOTYPES
     48  **********************/
     49 static int read_png_file(png_img_t * p, const char* file_name);
     50 static void png_release(png_img_t * p);
     51 
     52 /**********************
     53  *  STATIC VARIABLES
     54  **********************/
     55 
     56 /**********************
     57  *      MACROS
     58  **********************/
     59 
     60 /**********************
     61  *   GLOBAL FUNCTIONS
     62  **********************/
     63 
     64 bool lv_test_assert_img_eq(const char * fn_ref)
     65 {
     66   char fn_ref_full[512];
     67   sprintf(fn_ref_full, "%s%s", REF_IMGS_PATH, fn_ref);
     68 
     69   png_img_t p;
     70   int res = read_png_file(&p, fn_ref_full);
     71   if(res < 0) return false;
     72   uint8_t * screen_buf;
     73 
     74   lv_obj_invalidate(lv_scr_act());
     75   lv_refr_now(NULL);
     76 
     77   extern lv_color_t test_fb[];
     78 
     79   screen_buf = (uint8_t *)test_fb;
     80 
     81   uint8_t * ptr_act = NULL;
     82   const png_byte* ptr_ref = NULL;
     83 
     84   bool err = false;
     85   int x, y, i_buf = 0;
     86   for (y = 0; y < p.height; y++) {
     87     png_byte* row = p.row_pointers[y];
     88 
     89     for (x = 0; x < p.width; x++) {
     90       ptr_ref = &(row[x*3]);
     91       ptr_act = &(screen_buf[i_buf*4]);
     92 
     93       uint32_t ref_px = 0;
     94       uint32_t act_px = 0;
     95       memcpy(&ref_px, ptr_ref, 3);
     96       memcpy(&act_px, ptr_act, 3);
     97       //printf("0xFF%06x, ", act_px);
     98 
     99       uint8_t act_swap[3] = {ptr_act[2], ptr_act[1], ptr_act[0]};
    100 
    101       if(memcmp(act_swap, ptr_ref, 3) != 0) {
    102         err = true;
    103         break;
    104       }
    105       i_buf++;
    106     }
    107     if(err) break;
    108   }
    109 
    110   if(err) {
    111       uint32_t ref_px = 0;
    112       uint32_t act_px = 0;
    113       memcpy(&ref_px, ptr_ref, 3);
    114       memcpy(&act_px, ptr_act, 3);
    115 
    116       FILE * f = fopen("../test_screenshot_error.h", "w");
    117 
    118       fprintf(f, "//Diff in %s at (%d;%d), %x instead of %x)\n\n", fn_ref, x, y, act_px, ref_px);
    119       fprintf(f, "static const uint32_t test_screenshot_error_data[] = {\n");
    120 
    121       i_buf = 0;
    122       for (y = 0; y < 480; y++) {
    123         fprintf(f, "\n");
    124         for (x = 0; x < 800; x++) {
    125           ptr_act = &(screen_buf[i_buf * 4]);
    126           act_px = 0;
    127           memcpy(&act_px, ptr_act, 3);
    128           fprintf(f, "0xFF%06X, ", act_px);
    129           i_buf++;
    130         }
    131       }
    132       fprintf(f, "};\n\n");
    133 
    134       fprintf(f, "static lv_img_dsc_t test_screenshot_error_dsc = { \n"
    135       "  .header.w = 800,\n"
    136       "  .header.h = 480,\n"
    137       "  .header.always_zero = 0,\n"
    138       "  .header.cf = LV_IMG_CF_TRUE_COLOR,\n"
    139       "  .data_size = 800 * 480 * 4,\n"
    140       "  .data = test_screenshot_error_data};\n\n"
    141       "static inline void test_screenshot_error_show(void)\n"
    142       "{\n"
    143       "  lv_obj_t * img = lv_img_create(lv_scr_act());\n"
    144       "  lv_img_set_src(img, &test_screenshot_error_dsc);\n"
    145       "}\n");
    146 
    147       fclose(f);
    148 
    149   }
    150 
    151 
    152   png_release(&p);
    153 
    154   return !err;
    155 
    156 }
    157 
    158 /**********************
    159  *   STATIC FUNCTIONS
    160  **********************/
    161 
    162 static int read_png_file(png_img_t * p, const char* file_name)
    163 {
    164     char header[8];    // 8 is the maximum size that can be checked
    165 
    166     /*open file and test for it being a png*/
    167     FILE *fp = fopen(file_name, "rb");
    168     if (!fp) {
    169        TEST_PRINTF("%s", "PNG file %s could not be opened for reading");
    170         return -1;
    171     }
    172 
    173     size_t rcnt = fread(header, 1, 8, fp);
    174     if (rcnt != 8 || png_sig_cmp((png_const_bytep)header, 0, 8)) {
    175         TEST_PRINTF("%s is not recognized as a PNG file", file_name);
    176         return -1;
    177     }
    178 
    179     /*initialize stuff*/
    180     p->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    181 
    182     if (!p->png_ptr) {
    183         TEST_PRINTF("%s", "png_create_read_struct failed");
    184         return -1;
    185     }
    186 
    187     p->info_ptr = png_create_info_struct(p->png_ptr);
    188     if (!p->info_ptr) {
    189         TEST_PRINTF("%s", "png_create_info_struct failed");
    190         return -1;
    191     }
    192     if (setjmp(png_jmpbuf(p->png_ptr))) {
    193         TEST_PRINTF("%s", "Error during init_io");
    194         return -1;
    195     }
    196     png_init_io(p->png_ptr, fp);
    197     png_set_sig_bytes(p->png_ptr, 8);
    198 
    199     png_read_info(p->png_ptr, p->info_ptr);
    200 
    201     p->width = png_get_image_width(p->png_ptr, p->info_ptr);
    202     p->height = png_get_image_height(p->png_ptr, p->info_ptr);
    203     p->color_type = png_get_color_type(p->png_ptr, p->info_ptr);
    204     p->bit_depth = png_get_bit_depth(p->png_ptr, p->info_ptr);
    205 
    206     p->number_of_passes = png_set_interlace_handling(p->png_ptr);
    207     png_read_update_info(p->png_ptr, p->info_ptr);
    208 
    209     /*read file*/
    210     if (setjmp(png_jmpbuf(p->png_ptr))) {
    211         TEST_PRINTF("%s", "Error during read_image");
    212         return -1;
    213     }
    214     p->row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * p->height);
    215 
    216     int y;
    217     for (y=0; y<p->height; y++)
    218         p->row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(p->png_ptr,p->info_ptr));
    219 
    220     png_read_image(p->png_ptr, p->row_pointers);
    221 
    222     fclose(fp);
    223     return 0;
    224 }
    225 
    226 static void png_release(png_img_t * p)
    227 {
    228     int y;
    229     for (y=0; y<p->height; y++) free(p->row_pointers[y]);
    230 
    231     free(p->row_pointers);
    232 
    233     png_destroy_read_struct(&p->png_ptr, &p->info_ptr, NULL);
    234 }
    235 
    236 
    237 #endif