Skip to content
Myr

image_test.c

This file

Path

tests/image_test.c

173 lines · 6.0 KiB · C

Last changed by

image_test: tweak testing to open image in more modesSean Barrettbefore adoption846d15c

History of this file

Take it

1#define STBI_WINDOWS_UTF823#define STB_IMAGE_WRITE_IMPLEMENTATION4#include "stb_image_write.h"56#define STB_IMAGE_IMPLEMENTATION7#include "stb_image.h"89#define STB_DEFINE10#include "stb.h"1112//#define PNGSUITE_PRIMARY1314#if 015void test_ycbcr(void)16{17   STBI_SIMD_ALIGN(unsigned char, y[256]);18   STBI_SIMD_ALIGN(unsigned char, cb[256]);19   STBI_SIMD_ALIGN(unsigned char, cr[256]);20   STBI_SIMD_ALIGN(unsigned char, out1[256][4]);21   STBI_SIMD_ALIGN(unsigned char, out2[256][4]);2223   int i,j,k;24   int count = 0, bigcount=0, total=0;2526   for (i=0; i < 256; ++i) {27      for (j=0; j < 256; ++j) {28         for (k=0; k < 256; ++k) {29            y [k] = k;30            cb[k] = j;31            cr[k] = i;32         }33         stbi__YCbCr_to_RGB_row(out1[0], y, cb, cr, 256, 4);34         stbi__YCbCr_to_RGB_sse2(out2[0], y, cb, cr, 256, 4);35         for (k=0; k < 256; ++k) {36            // inaccurate proxy for values outside of RGB cube37            if (out1[k][0] == 0 || out1[k][1] == 0 || out1[k][2] == 0 || out1[k][0] == 255 || out1[k][1] == 255 || out1[k][2] == 255)38               continue;39            ++total;40            if (out1[k][0] != out2[k][0] || out1[k][1] != out2[k][1] || out1[k][2] != out2[k][2]) {41               int dist1 = abs(out1[k][0] - out2[k][0]);42               int dist2 = abs(out1[k][1] - out2[k][1]);43               int dist3 = abs(out1[k][2] - out2[k][2]);44               ++count;45               if (out1[k][1] > out2[k][1])46                  ++bigcount;47            }48         }49      }50      printf("So far: %d (%d big) of %d\n", count, bigcount, total);51   }52   printf("Final: %d (%d big) of %d\n", count, bigcount, total);53}54#endif5556float hdr_data[200][200][3];5758void dummy_write(void *context, void *data, int len)59{60   static char dummy[1024];61   if (len > 1024) len = 1024;62   memcpy(dummy, data, len);63}6465extern void image_write_test(void);6667int main(int argc, char **argv)68{69   int w,h;70   //test_ycbcr();7172   image_write_test();7374   #if 075   // test hdr asserts76   for (h=0; h < 100; h += 2)77      for (w=0; w < 200; ++w)78         hdr_data[h][w][0] = (float) rand(),79         hdr_data[h][w][1] = (float) rand(),80         hdr_data[h][w][2] = (float) rand();8182   stbi_write_hdr("output/test.hdr", 200,200,3,hdr_data[0][0]);83   #endif8485   if (argc > 1) {86      int i, n;8788      for (i=1; i < argc; ++i) {89         int res;90         int w2,h2,n2;91         unsigned char *data;92         printf("%s\n", argv[i]);93         res = stbi_info(argv[i], &w2, &h2, &n2);94         data = stbi_load(argv[i], &w, &h, &n, 0); if (data) free(data); else printf("Failed &n\n");95         data = stbi_load(argv[i], &w, &h, &n, 4); if (data) free(data); else printf("Failed &n\n");96         data = stbi_load(argv[i], &w, &h,  0, 1); if (data) free(data); else printf("Failed 1\n");97         data = stbi_load(argv[i], &w, &h,  0, 2); if (data) free(data); else printf("Failed 2\n");98         data = stbi_load(argv[i], &w, &h,  0, 3); if (data) free(data); else printf("Failed 3\n");99         data = stbi_load(argv[i], &w, &h, &n, 4);100         assert(data);101         assert(w == w2 && h == h2 && n == n2);102         assert(res);103         if (data) {104            char fname[512];105            stb_splitpath(fname, argv[i], STB_FILE);106            stbi_write_png(stb_sprintf("output/%s.png", fname), w, h, 4, data, w*4);107            stbi_write_bmp(stb_sprintf("output/%s.bmp", fname), w, h, 4, data);108            stbi_write_tga(stb_sprintf("output/%s.tga", fname), w, h, 4, data);109            stbi_write_png_to_func(dummy_write,0, w, h, 4, data, w*4);110            stbi_write_bmp_to_func(dummy_write,0, w, h, 4, data);111            stbi_write_tga_to_func(dummy_write,0, w, h, 4, data);112            free(data);113         } else114            printf("FAILED 4\n");115      }116   } else {117      int i;118      #ifdef PNGSUITE_PRIMARY119      char **files = stb_readdir_files("pngsuite/primary");120      #else121      char **files = stb_readdir_files("images");122      #endif123      for (i=0; i < stb_arr_len(files); ++i) {124         int n;125         char **failed = NULL;126         unsigned char *data;127         printf(".");128         //printf("%s\n", files[i]);129         data = stbi_load(files[i], &w, &h, &n, 0); if (data) free(data); else stb_arr_push(failed, "&n");130         data = stbi_load(files[i], &w, &h,  0, 1); if (data) free(data); else stb_arr_push(failed, "1");131         data = stbi_load(files[i], &w, &h,  0, 2); if (data) free(data); else stb_arr_push(failed, "2");132         data = stbi_load(files[i], &w, &h,  0, 3); if (data) free(data); else stb_arr_push(failed, "3");133         data = stbi_load(files[i], &w, &h,  0, 4); if (data)           ; else stb_arr_push(failed, "4");134         if (data) {135            char fname[512];136137            #ifdef PNGSUITE_PRIMARY138            int w2,h2;139            unsigned char *data2;140            stb_splitpath(fname, files[i], STB_FILE_EXT);141            data2 = stbi_load(stb_sprintf("pngsuite/primary_check/%s", fname), &w2, &h2, 0, 4);142            if (!data2)143               printf("FAILED: couldn't load 'pngsuite/primary_check/%s\n", fname);144            else {145               if (w != w2 || h != w2 || 0 != memcmp(data, data2, w*h*4)) {146                  int x,y,c;147                  if (w == w2 && h == h2)148                     for (y=0; y < h; ++y)149                        for (x=0; x < w; ++x)150                           for (c=0; c < 4; ++c)151                              assert(data[y*w*4+x*4+c] == data2[y*w*4+x*4+c]);152                  printf("FAILED: %s loaded but didn't match PRIMARY_check 32-bit version\n", files[i]);153               }154               free(data2);155            }156            #else157            stb_splitpath(fname, files[i], STB_FILE);158            stbi_write_png(stb_sprintf("output/%s.png", fname), w, h, 4, data, w*4);159            #endif160            free(data);161         }162         if (failed) {163            int j;164            printf("FAILED: ");165            for (j=0; j < stb_arr_len(failed); ++j)166               printf("%s ", failed[j]);167            printf(" -- %s\n", files[i]);168         }169      }170      printf("Tested %d files.\n", i);171   }172   return 0;173}