Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.8 KiB

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