image_test.c 6.0 KB

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