added stb, more binaryout changes"
[henge/apc.git] / stb / tests / image_test.c
1 #define STB_IMAGE_WRITE_IMPLEMENTATION
2 #include "stb_image_write.h"
3
4 #define STB_IMAGE_IMPLEMENTATION
5 #include "stb_image.h"
6
7 #define STB_DEFINE
8 #include "stb.h"
9
10 //#define PNGSUITE_PRIMARY
11
12 #if 0
13 void test_ycbcr(void)
14 {
15 STBI_SIMD_ALIGN(unsigned char, y[256]);
16 STBI_SIMD_ALIGN(unsigned char, cb[256]);
17 STBI_SIMD_ALIGN(unsigned char, cr[256]);
18 STBI_SIMD_ALIGN(unsigned char, out1[256][4]);
19 STBI_SIMD_ALIGN(unsigned char, out2[256][4]);
20
21 int i,j,k;
22 int count = 0, bigcount=0, total=0;
23
24 for (i=0; i < 256; ++i) {
25 for (j=0; j < 256; ++j) {
26 for (k=0; k < 256; ++k) {
27 y [k] = k;
28 cb[k] = j;
29 cr[k] = i;
30 }
31 stbi__YCbCr_to_RGB_row(out1[0], y, cb, cr, 256, 4);
32 stbi__YCbCr_to_RGB_sse2(out2[0], y, cb, cr, 256, 4);
33 for (k=0; k < 256; ++k) {
34 // inaccurate proxy for values outside of RGB cube
35 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)
36 continue;
37 ++total;
38 if (out1[k][0] != out2[k][0] || out1[k][1] != out2[k][1] || out1[k][2] != out2[k][2]) {
39 int dist1 = abs(out1[k][0] - out2[k][0]);
40 int dist2 = abs(out1[k][1] - out2[k][1]);
41 int dist3 = abs(out1[k][2] - out2[k][2]);
42 ++count;
43 if (out1[k][1] > out2[k][1])
44 ++bigcount;
45 }
46 }
47 }
48 printf("So far: %d (%d big) of %d\n", count, bigcount, total);
49 }
50 printf("Final: %d (%d big) of %d\n", count, bigcount, total);
51 }
52 #endif
53
54 float hdr_data[200][200][3];
55
56 void dummy_write(void *context, void *data, int len)
57 {
58 static char dummy[1024];
59 if (len > 1024) len = 1024;
60 memcpy(dummy, data, len);
61 }
62
63 int main(int argc, char **argv)
64 {
65 int w,h;
66 //test_ycbcr();
67
68 #if 0
69 // test hdr asserts
70 for (h=0; h < 100; h += 2)
71 for (w=0; w < 200; ++w)
72 hdr_data[h][w][0] = (float) rand(),
73 hdr_data[h][w][1] = (float) rand(),
74 hdr_data[h][w][2] = (float) rand();
75
76 stbi_write_hdr("output/test.hdr", 200,200,3,hdr_data[0][0]);
77 #endif
78
79 if (argc > 1) {
80 int i, n;
81
82 for (i=1; i < argc; ++i) {
83 int res;
84 int w2,h2,n2;
85 unsigned char *data;
86 printf("%s\n", argv[i]);
87 res = stbi_info(argv[1], &w2, &h2, &n2);
88 data = stbi_load(argv[i], &w, &h, &n, 4); if (data) free(data); else printf("Failed &n\n");
89 data = stbi_load(argv[i], &w, &h, 0, 1); if (data) free(data); else printf("Failed 1\n");
90 data = stbi_load(argv[i], &w, &h, 0, 2); if (data) free(data); else printf("Failed 2\n");
91 data = stbi_load(argv[i], &w, &h, 0, 3); if (data) free(data); else printf("Failed 3\n");
92 data = stbi_load(argv[i], &w, &h, &n, 4);
93 assert(data);
94 assert(w == w2 && h == h2 && n == n2);
95 assert(res);
96 if (data) {
97 char fname[512];
98 stb_splitpath(fname, argv[i], STB_FILE);
99 stbi_write_png(stb_sprintf("output/%s.png", fname), w, h, 4, data, w*4);
100 stbi_write_bmp(stb_sprintf("output/%s.bmp", fname), w, h, 4, data);
101 stbi_write_tga(stb_sprintf("output/%s.tga", fname), w, h, 4, data);
102 stbi_write_png_to_func(dummy_write,0, w, h, 4, data, w*4);
103 stbi_write_bmp_to_func(dummy_write,0, w, h, 4, data);
104 stbi_write_tga_to_func(dummy_write,0, w, h, 4, data);
105 free(data);
106 } else
107 printf("FAILED 4\n");
108 }
109 } else {
110 int i, nope=0;
111 #ifdef PNGSUITE_PRIMARY
112 char **files = stb_readdir_files("pngsuite/primary");
113 #else
114 char **files = stb_readdir_files("images");
115 #endif
116 for (i=0; i < stb_arr_len(files); ++i) {
117 int n;
118 char **failed = NULL;
119 unsigned char *data;
120 printf(".");
121 //printf("%s\n", files[i]);
122 data = stbi_load(files[i], &w, &h, &n, 0); if (data) free(data); else stb_arr_push(failed, "&n");
123 data = stbi_load(files[i], &w, &h, 0, 1); if (data) free(data); else stb_arr_push(failed, "1");
124 data = stbi_load(files[i], &w, &h, 0, 2); if (data) free(data); else stb_arr_push(failed, "2");
125 data = stbi_load(files[i], &w, &h, 0, 3); if (data) free(data); else stb_arr_push(failed, "3");
126 data = stbi_load(files[i], &w, &h, 0, 4); if (data) ; else stb_arr_push(failed, "4");
127 if (data) {
128 char fname[512];
129
130 #ifdef PNGSUITE_PRIMARY
131 int w2,h2;
132 unsigned char *data2;
133 stb_splitpath(fname, files[i], STB_FILE_EXT);
134 data2 = stbi_load(stb_sprintf("pngsuite/primary_check/%s", fname), &w2, &h2, 0, 4);
135 if (!data2)
136 printf("FAILED: couldn't load 'pngsuite/primary_check/%s\n", fname);
137 else {
138 if (w != w2 || h != w2 || 0 != memcmp(data, data2, w*h*4)) {
139 int x,y,c;
140 if (w == w2 && h == h2)
141 for (y=0; y < h; ++y)
142 for (x=0; x < w; ++x)
143 for (c=0; c < 4; ++c)
144 assert(data[y*w*4+x*4+c] == data2[y*w*4+x*4+c]);
145 printf("FAILED: %s loaded but didn't match PRIMARY_check 32-bit version\n", files[i]);
146 }
147 free(data2);
148 }
149 #else
150 stb_splitpath(fname, files[i], STB_FILE);
151 stbi_write_png(stb_sprintf("output/%s.png", fname), w, h, 4, data, w*4);
152 #endif
153 free(data);
154 }
155 if (failed) {
156 int j;
157 printf("FAILED: ");
158 for (j=0; j < stb_arr_len(failed); ++j)
159 printf("%s ", failed[j]);
160 printf(" -- %s\n", files[i]);
161 }
162 }
163 printf("Tested %d files.\n", i);
164 }
165 return 0;
166 }