1 #define STBI_WINDOWS_UTF8
2 
3 #define STB_IMAGE_WRITE_IMPLEMENTATION
4 #include "stb_image_write.h"
5 
6 #define STB_IMAGE_IMPLEMENTATION
7 #include "stb_image.h"
8 
9 #define STB_DEFINE
10 #include "stb.h"
11 
12 //#define PNGSUITE_PRIMARY
13 
14 #if 0
15 void 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]);
22 
23    int i,j,k;
24    int count = 0, bigcount=0, total=0;
25 
26    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 cube
37             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 #endif
55 
56 float hdr_data[200][200][3];
57 
dummy_write(void * context,void * data,int len)58 void 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 }
64 
65 extern void image_write_test(void);
66 
main(int argc,char ** argv)67 int main(int argc, char **argv)
68 {
69    int w,h;
70    //test_ycbcr();
71 
72    image_write_test();
73 
74    #if 0
75    // test hdr asserts
76    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();
81 
82    stbi_write_hdr("output/test.hdr", 200,200,3,hdr_data[0][0]);
83    #endif
84 
85    if (argc > 1) {
86       int i, n;
87 
88       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, 4); if (data) free(data); else printf("Failed &n\n");
95          data = stbi_load(argv[i], &w, &h,  0, 1); if (data) free(data); else printf("Failed 1\n");
96          data = stbi_load(argv[i], &w, &h,  0, 2); if (data) free(data); else printf("Failed 2\n");
97          data = stbi_load(argv[i], &w, &h,  0, 3); if (data) free(data); else printf("Failed 3\n");
98          data = stbi_load(argv[i], &w, &h, &n, 4);
99          assert(data);
100          assert(w == w2 && h == h2 && n == n2);
101          assert(res);
102          if (data) {
103             char fname[512];
104             stb_splitpath(fname, argv[i], STB_FILE);
105             stbi_write_png(stb_sprintf("output/%s.png", fname), w, h, 4, data, w*4);
106             stbi_write_bmp(stb_sprintf("output/%s.bmp", fname), w, h, 4, data);
107             stbi_write_tga(stb_sprintf("output/%s.tga", fname), w, h, 4, data);
108             stbi_write_png_to_func(dummy_write,0, w, h, 4, data, w*4);
109             stbi_write_bmp_to_func(dummy_write,0, w, h, 4, data);
110             stbi_write_tga_to_func(dummy_write,0, w, h, 4, data);
111             free(data);
112          } else
113             printf("FAILED 4\n");
114       }
115    } else {
116       int i;
117       #ifdef PNGSUITE_PRIMARY
118       char **files = stb_readdir_files("pngsuite/primary");
119       #else
120       char **files = stb_readdir_files("images");
121       #endif
122       for (i=0; i < stb_arr_len(files); ++i) {
123          int n;
124          char **failed = NULL;
125          unsigned char *data;
126          printf(".");
127          //printf("%s\n", files[i]);
128          data = stbi_load(files[i], &w, &h, &n, 0); if (data) free(data); else stb_arr_push(failed, "&n");
129          data = stbi_load(files[i], &w, &h,  0, 1); if (data) free(data); else stb_arr_push(failed, "1");
130          data = stbi_load(files[i], &w, &h,  0, 2); if (data) free(data); else stb_arr_push(failed, "2");
131          data = stbi_load(files[i], &w, &h,  0, 3); if (data) free(data); else stb_arr_push(failed, "3");
132          data = stbi_load(files[i], &w, &h,  0, 4); if (data)           ; else stb_arr_push(failed, "4");
133          if (data) {
134             char fname[512];
135 
136             #ifdef PNGSUITE_PRIMARY
137             int w2,h2;
138             unsigned char *data2;
139             stb_splitpath(fname, files[i], STB_FILE_EXT);
140             data2 = stbi_load(stb_sprintf("pngsuite/primary_check/%s", fname), &w2, &h2, 0, 4);
141             if (!data2)
142                printf("FAILED: couldn't load 'pngsuite/primary_check/%s\n", fname);
143             else {
144                if (w != w2 || h != w2 || 0 != memcmp(data, data2, w*h*4)) {
145                   int x,y,c;
146                   if (w == w2 && h == h2)
147                      for (y=0; y < h; ++y)
148                         for (x=0; x < w; ++x)
149                            for (c=0; c < 4; ++c)
150                               assert(data[y*w*4+x*4+c] == data2[y*w*4+x*4+c]);
151                   printf("FAILED: %s loaded but didn't match PRIMARY_check 32-bit version\n", files[i]);
152                }
153                free(data2);
154             }
155             #else
156             stb_splitpath(fname, files[i], STB_FILE);
157             stbi_write_png(stb_sprintf("output/%s.png", fname), w, h, 4, data, w*4);
158             #endif
159             free(data);
160          }
161          if (failed) {
162             int j;
163             printf("FAILED: ");
164             for (j=0; j < stb_arr_len(failed); ++j)
165                printf("%s ", failed[j]);
166             printf(" -- %s\n", files[i]);
167          }
168       }
169       printf("Tested %d files.\n", i);
170    }
171    return 0;
172 }
173