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.

1118 lines
38 KiB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #if defined(_WIN32) && _MSC_VER > 1200
  4. #define STBIR_ASSERT(x) \
  5. if (!(x)) { \
  6. __debugbreak(); \
  7. } else
  8. #else
  9. #include <assert.h>
  10. #define STBIR_ASSERT(x) assert(x)
  11. #endif
  12. #define STBIR_MALLOC stbir_malloc
  13. #define STBIR_FREE stbir_free
  14. class stbir_context {
  15. public:
  16. stbir_context()
  17. {
  18. size = 1000000;
  19. memory = malloc(size);
  20. }
  21. ~stbir_context()
  22. {
  23. free(memory);
  24. }
  25. size_t size;
  26. void* memory;
  27. } g_context;
  28. void* stbir_malloc(size_t size, void* context)
  29. {
  30. if (!context)
  31. return malloc(size);
  32. stbir_context* real_context = (stbir_context*)context;
  33. if (size > real_context->size)
  34. return 0;
  35. return real_context->memory;
  36. }
  37. void stbir_free(void* memory, void* context)
  38. {
  39. if (!context)
  40. free(memory);
  41. }
  42. //#include <stdio.h>
  43. void stbir_progress(float p)
  44. {
  45. //printf("%f\n", p);
  46. STBIR_ASSERT(p >= 0 && p <= 1);
  47. }
  48. #define STBIR_PROGRESS_REPORT stbir_progress
  49. #define STB_IMAGE_RESIZE_IMPLEMENTATION
  50. #define STB_IMAGE_RESIZE_STATIC
  51. #include "stb_image_resize.h"
  52. #define STB_IMAGE_WRITE_IMPLEMENTATION
  53. #include "stb_image_write.h"
  54. #define STB_IMAGE_IMPLEMENTATION
  55. #include "stb_image.h"
  56. #ifdef _WIN32
  57. #include <sys/timeb.h>
  58. #include <direct.h>
  59. #define mkdir(a, b) _mkdir(a)
  60. #else
  61. #include <sys/stat.h>
  62. #endif
  63. #define MT_SIZE 624
  64. static size_t g_aiMT[MT_SIZE];
  65. static size_t g_iMTI = 0;
  66. // Mersenne Twister implementation from Wikipedia.
  67. // Avoiding use of the system rand() to be sure that our tests generate the same test data on any system.
  68. void mtsrand(size_t iSeed)
  69. {
  70. g_aiMT[0] = iSeed;
  71. for (size_t i = 1; i < MT_SIZE; i++)
  72. {
  73. size_t inner1 = g_aiMT[i - 1];
  74. size_t inner2 = (g_aiMT[i - 1] >> 30);
  75. size_t inner = inner1 ^ inner2;
  76. g_aiMT[i] = (0x6c078965 * inner) + i;
  77. }
  78. g_iMTI = 0;
  79. }
  80. size_t mtrand()
  81. {
  82. if (g_iMTI == 0)
  83. {
  84. for (size_t i = 0; i < MT_SIZE; i++)
  85. {
  86. size_t y = (0x80000000 & (g_aiMT[i])) + (0x7fffffff & (g_aiMT[(i + 1) % MT_SIZE]));
  87. g_aiMT[i] = g_aiMT[(i + 397) % MT_SIZE] ^ (y >> 1);
  88. if ((y % 2) == 1)
  89. g_aiMT[i] = g_aiMT[i] ^ 0x9908b0df;
  90. }
  91. }
  92. size_t y = g_aiMT[g_iMTI];
  93. y = y ^ (y >> 11);
  94. y = y ^ ((y << 7) & (0x9d2c5680));
  95. y = y ^ ((y << 15) & (0xefc60000));
  96. y = y ^ (y >> 18);
  97. g_iMTI = (g_iMTI + 1) % MT_SIZE;
  98. return y;
  99. }
  100. inline float mtfrand()
  101. {
  102. const int ninenine = 999999;
  103. return (float)(mtrand() % ninenine)/ninenine;
  104. }
  105. static void resizer(int argc, char **argv)
  106. {
  107. unsigned char* input_pixels;
  108. unsigned char* output_pixels;
  109. int w, h;
  110. int n;
  111. int out_w, out_h;
  112. input_pixels = stbi_load(argv[1], &w, &h, &n, 0);
  113. out_w = w*3;
  114. out_h = h*3;
  115. output_pixels = (unsigned char*) malloc(out_w*out_h*n);
  116. //stbir_resize_uint8_srgb(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, n, -1,0);
  117. stbir_resize_uint8(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, n);
  118. stbi_write_png("output.png", out_w, out_h, n, output_pixels, 0);
  119. exit(0);
  120. }
  121. static void performance(int argc, char **argv)
  122. {
  123. unsigned char* input_pixels;
  124. unsigned char* output_pixels;
  125. int w, h, count;
  126. int n, i;
  127. int out_w, out_h, srgb=1;
  128. input_pixels = stbi_load(argv[1], &w, &h, &n, 0);
  129. #if 0
  130. out_w = w/4; out_h = h/4; count=100; // 1
  131. #elif 0
  132. out_w = w*2; out_h = h/4; count=20; // 2 // note this is structured pessimily, would be much faster to downsample vertically first
  133. #elif 0
  134. out_w = w/4; out_h = h*2; count=50; // 3
  135. #elif 0
  136. out_w = w*3; out_h = h*3; count=2; srgb=0; // 4
  137. #else
  138. out_w = w*3; out_h = h*3; count=2; // 5 // this is dominated by linear->sRGB conversion
  139. #endif
  140. output_pixels = (unsigned char*) malloc(out_w*out_h*n);
  141. for (i=0; i < count; ++i)
  142. if (srgb)
  143. stbir_resize_uint8_srgb(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, n,-1,0);
  144. else
  145. stbir_resize(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, STBIR_TYPE_UINT8, n,-1, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT, STBIR_COLORSPACE_LINEAR, NULL);
  146. exit(0);
  147. }
  148. void test_suite(int argc, char **argv);
  149. int main(int argc, char** argv)
  150. {
  151. //resizer(argc, argv);
  152. //performance(argc, argv);
  153. test_suite(argc, argv);
  154. return 0;
  155. }
  156. void resize_image(const char* filename, float width_percent, float height_percent, stbir_filter filter, stbir_edge edge, stbir_colorspace colorspace, const char* output_filename)
  157. {
  158. int w, h, n;
  159. unsigned char* input_data = stbi_load(filename, &w, &h, &n, 0);
  160. if (!input_data)
  161. {
  162. printf("Input image could not be loaded\n");
  163. return;
  164. }
  165. int out_w = (int)(w * width_percent);
  166. int out_h = (int)(h * height_percent);
  167. unsigned char* output_data = (unsigned char*)malloc(out_w * out_h * n);
  168. stbir_resize(input_data, w, h, 0, output_data, out_w, out_h, 0, STBIR_TYPE_UINT8, n, STBIR_ALPHA_CHANNEL_NONE, 0, edge, edge, filter, filter, colorspace, &g_context);
  169. stbi_image_free(input_data);
  170. stbi_write_png(output_filename, out_w, out_h, n, output_data, 0);
  171. free(output_data);
  172. }
  173. template <typename F, typename T>
  174. void convert_image(const F* input, T* output, int length)
  175. {
  176. double f = (pow(2.0, 8.0 * sizeof(T)) - 1) / (pow(2.0, 8.0 * sizeof(F)) - 1);
  177. for (int i = 0; i < length; i++)
  178. output[i] = (T)(((double)input[i]) * f);
  179. }
  180. template <typename T>
  181. void test_format(const char* file, float width_percent, float height_percent, stbir_datatype type, stbir_colorspace colorspace)
  182. {
  183. int w, h, n;
  184. unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);
  185. if (input_data == NULL)
  186. return;
  187. int new_w = (int)(w * width_percent);
  188. int new_h = (int)(h * height_percent);
  189. T* T_data = (T*)malloc(w * h * n * sizeof(T));
  190. memset(T_data, 0, w*h*n*sizeof(T));
  191. convert_image<unsigned char, T>(input_data, T_data, w * h * n);
  192. T* output_data = (T*)malloc(new_w * new_h * n * sizeof(T));
  193. stbir_resize(T_data, w, h, 0, output_data, new_w, new_h, 0, type, n, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, colorspace, &g_context);
  194. free(T_data);
  195. stbi_image_free(input_data);
  196. unsigned char* char_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(char));
  197. convert_image<T, unsigned char>(output_data, char_data, new_w * new_h * n);
  198. char output[200];
  199. sprintf(output, "test-output/type-%d-%d-%d-%d-%s", type, colorspace, new_w, new_h, file);
  200. stbi_write_png(output, new_w, new_h, n, char_data, 0);
  201. free(char_data);
  202. free(output_data);
  203. }
  204. void convert_image_float(const unsigned char* input, float* output, int length)
  205. {
  206. for (int i = 0; i < length; i++)
  207. output[i] = ((float)input[i])/255;
  208. }
  209. void convert_image_float(const float* input, unsigned char* output, int length)
  210. {
  211. for (int i = 0; i < length; i++)
  212. output[i] = (unsigned char)(stbir__saturate(input[i]) * 255);
  213. }
  214. void test_float(const char* file, float width_percent, float height_percent, stbir_datatype type, stbir_colorspace colorspace)
  215. {
  216. int w, h, n;
  217. unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);
  218. if (input_data == NULL)
  219. return;
  220. int new_w = (int)(w * width_percent);
  221. int new_h = (int)(h * height_percent);
  222. float* T_data = (float*)malloc(w * h * n * sizeof(float));
  223. convert_image_float(input_data, T_data, w * h * n);
  224. float* output_data = (float*)malloc(new_w * new_h * n * sizeof(float));
  225. stbir_resize_float_generic(T_data, w, h, 0, output_data, new_w, new_h, 0, n, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, colorspace, &g_context);
  226. free(T_data);
  227. stbi_image_free(input_data);
  228. unsigned char* char_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(char));
  229. convert_image_float(output_data, char_data, new_w * new_h * n);
  230. char output[200];
  231. sprintf(output, "test-output/type-%d-%d-%d-%d-%s", type, colorspace, new_w, new_h, file);
  232. stbi_write_png(output, new_w, new_h, n, char_data, 0);
  233. free(char_data);
  234. free(output_data);
  235. }
  236. void test_channels(const char* file, float width_percent, float height_percent, int channels)
  237. {
  238. int w, h, n;
  239. unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);
  240. if (input_data == NULL)
  241. return;
  242. int new_w = (int)(w * width_percent);
  243. int new_h = (int)(h * height_percent);
  244. unsigned char* channels_data = (unsigned char*)malloc(w * h * channels * sizeof(unsigned char));
  245. for (int i = 0; i < w * h; i++)
  246. {
  247. int input_position = i * n;
  248. int output_position = i * channels;
  249. for (int c = 0; c < channels; c++)
  250. channels_data[output_position + c] = input_data[input_position + stbir__min(c, n)];
  251. }
  252. unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * channels * sizeof(unsigned char));
  253. stbir_resize_uint8_srgb(channels_data, w, h, 0, output_data, new_w, new_h, 0, channels, STBIR_ALPHA_CHANNEL_NONE, 0);
  254. free(channels_data);
  255. stbi_image_free(input_data);
  256. char output[200];
  257. sprintf(output, "test-output/channels-%d-%d-%d-%s", channels, new_w, new_h, file);
  258. stbi_write_png(output, new_w, new_h, channels, output_data, 0);
  259. free(output_data);
  260. }
  261. void test_subpixel(const char* file, float width_percent, float height_percent, float s1, float t1)
  262. {
  263. int w, h, n;
  264. unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);
  265. if (input_data == NULL)
  266. return;
  267. s1 = ((float)w - 1 + s1)/w;
  268. t1 = ((float)h - 1 + t1)/h;
  269. int new_w = (int)(w * width_percent);
  270. int new_h = (int)(h * height_percent);
  271. unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));
  272. stbir_resize_region(input_data, w, h, 0, output_data, new_w, new_h, 0, STBIR_TYPE_UINT8, n, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context, 0, 0, s1, t1);
  273. stbi_image_free(input_data);
  274. char output[200];
  275. sprintf(output, "test-output/subpixel-%d-%d-%f-%f-%s", new_w, new_h, s1, t1, file);
  276. stbi_write_png(output, new_w, new_h, n, output_data, 0);
  277. free(output_data);
  278. }
  279. void test_subpixel_region(const char* file, float width_percent, float height_percent, float s0, float t0, float s1, float t1)
  280. {
  281. int w, h, n;
  282. unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);
  283. if (input_data == NULL)
  284. return;
  285. int new_w = (int)(w * width_percent);
  286. int new_h = (int)(h * height_percent);
  287. unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));
  288. stbir_resize_region(input_data, w, h, 0, output_data, new_w, new_h, 0, STBIR_TYPE_UINT8, n, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context, s0, t0, s1, t1);
  289. stbi_image_free(input_data);
  290. char output[200];
  291. sprintf(output, "test-output/subpixel-region-%d-%d-%f-%f-%f-%f-%s", new_w, new_h, s0, t0, s1, t1, file);
  292. stbi_write_png(output, new_w, new_h, n, output_data, 0);
  293. free(output_data);
  294. }
  295. void test_subpixel_command(const char* file, float width_percent, float height_percent, float x_scale, float y_scale, float x_offset, float y_offset)
  296. {
  297. int w, h, n;
  298. unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);
  299. if (input_data == NULL)
  300. return;
  301. int new_w = (int)(w * width_percent);
  302. int new_h = (int)(h * height_percent);
  303. unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));
  304. stbir_resize_subpixel(input_data, w, h, 0, output_data, new_w, new_h, 0, STBIR_TYPE_UINT8, n, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context, x_scale, y_scale, x_offset, y_offset);
  305. stbi_image_free(input_data);
  306. char output[200];
  307. sprintf(output, "test-output/subpixel-command-%d-%d-%f-%f-%f-%f-%s", new_w, new_h, x_scale, y_scale, x_offset, y_offset, file);
  308. stbi_write_png(output, new_w, new_h, n, output_data, 0);
  309. free(output_data);
  310. }
  311. unsigned int* pixel(unsigned int* buffer, int x, int y, int c, int w, int n)
  312. {
  313. return &buffer[y*w*n + x*n + c];
  314. }
  315. void test_premul()
  316. {
  317. unsigned int input[2 * 2 * 4];
  318. unsigned int output[1 * 1 * 4];
  319. unsigned int output2[2 * 2 * 4];
  320. memset(input, 0, sizeof(input));
  321. // First a test to make sure premul is working properly.
  322. // Top left - solid red
  323. *pixel(input, 0, 0, 0, 2, 4) = 255;
  324. *pixel(input, 0, 0, 3, 2, 4) = 255;
  325. // Bottom left - solid red
  326. *pixel(input, 0, 1, 0, 2, 4) = 255;
  327. *pixel(input, 0, 1, 3, 2, 4) = 255;
  328. // Top right - transparent green
  329. *pixel(input, 1, 0, 1, 2, 4) = 255;
  330. *pixel(input, 1, 0, 3, 2, 4) = 25;
  331. // Bottom right - transparent green
  332. *pixel(input, 1, 1, 1, 2, 4) = 255;
  333. *pixel(input, 1, 1, 3, 2, 4) = 25;
  334. stbir_resize(input, 2, 2, 0, output, 1, 1, 0, STBIR_TYPE_UINT32, 4, 3, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, STBIR_FILTER_BOX, STBIR_COLORSPACE_LINEAR, &g_context);
  335. float r = (float)255 / 4294967296;
  336. float g = (float)255 / 4294967296;
  337. float ra = (float)255 / 4294967296;
  338. float ga = (float)25 / 4294967296;
  339. float a = (ra + ga) / 2;
  340. STBIR_ASSERT(output[0] == (unsigned int)(r * ra / 2 / a * 4294967296 + 0.5f)); // 232
  341. STBIR_ASSERT(output[1] == (unsigned int)(g * ga / 2 / a * 4294967296 + 0.5f)); // 23
  342. STBIR_ASSERT(output[2] == 0);
  343. STBIR_ASSERT(output[3] == (unsigned int)(a * 4294967296 + 0.5f)); // 140
  344. // Now a test to make sure it doesn't clobber existing values.
  345. // Top right - completely transparent green
  346. *pixel(input, 1, 0, 1, 2, 4) = 255;
  347. *pixel(input, 1, 0, 3, 2, 4) = 0;
  348. // Bottom right - completely transparent green
  349. *pixel(input, 1, 1, 1, 2, 4) = 255;
  350. *pixel(input, 1, 1, 3, 2, 4) = 0;
  351. stbir_resize(input, 2, 2, 0, output2, 2, 2, 0, STBIR_TYPE_UINT32, 4, 3, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, STBIR_FILTER_BOX, STBIR_COLORSPACE_LINEAR, &g_context);
  352. STBIR_ASSERT(*pixel(output2, 0, 0, 0, 2, 4) == 255);
  353. STBIR_ASSERT(*pixel(output2, 0, 0, 1, 2, 4) == 0);
  354. STBIR_ASSERT(*pixel(output2, 0, 0, 2, 2, 4) == 0);
  355. STBIR_ASSERT(*pixel(output2, 0, 0, 3, 2, 4) == 255);
  356. STBIR_ASSERT(*pixel(output2, 0, 1, 0, 2, 4) == 255);
  357. STBIR_ASSERT(*pixel(output2, 0, 1, 1, 2, 4) == 0);
  358. STBIR_ASSERT(*pixel(output2, 0, 1, 2, 2, 4) == 0);
  359. STBIR_ASSERT(*pixel(output2, 0, 1, 3, 2, 4) == 255);
  360. STBIR_ASSERT(*pixel(output2, 1, 0, 0, 2, 4) == 0);
  361. STBIR_ASSERT(*pixel(output2, 1, 0, 1, 2, 4) == 255);
  362. STBIR_ASSERT(*pixel(output2, 1, 0, 2, 2, 4) == 0);
  363. STBIR_ASSERT(*pixel(output2, 1, 0, 3, 2, 4) == 0);
  364. STBIR_ASSERT(*pixel(output2, 1, 1, 0, 2, 4) == 0);
  365. STBIR_ASSERT(*pixel(output2, 1, 1, 1, 2, 4) == 255);
  366. STBIR_ASSERT(*pixel(output2, 1, 1, 2, 2, 4) == 0);
  367. STBIR_ASSERT(*pixel(output2, 1, 1, 3, 2, 4) == 0);
  368. }
  369. // test that splitting a pow-2 image into tiles produces identical results
  370. void test_subpixel_1()
  371. {
  372. unsigned char image[8 * 8];
  373. mtsrand(0);
  374. for (int i = 0; i < sizeof(image); i++)
  375. image[i] = mtrand() & 255;
  376. unsigned char output_data[16 * 16];
  377. stbir_resize_region(image, 8, 8, 0, output_data, 16, 16, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context, 0, 0, 1, 1);
  378. unsigned char output_left[8 * 16];
  379. unsigned char output_right[8 * 16];
  380. stbir_resize_region(image, 8, 8, 0, output_left, 8, 16, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context, 0, 0, 0.5f, 1);
  381. stbir_resize_region(image, 8, 8, 0, output_right, 8, 16, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context, 0.5f, 0, 1, 1);
  382. for (int x = 0; x < 8; x++)
  383. {
  384. for (int y = 0; y < 16; y++)
  385. {
  386. STBIR_ASSERT(output_data[y * 16 + x] == output_left[y * 8 + x]);
  387. STBIR_ASSERT(output_data[y * 16 + x + 8] == output_right[y * 8 + x]);
  388. }
  389. }
  390. stbir_resize_subpixel(image, 8, 8, 0, output_left, 8, 16, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context, 2, 2, 0, 0);
  391. stbir_resize_subpixel(image, 8, 8, 0, output_right, 8, 16, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context, 2, 2, 8, 0);
  392. {for (int x = 0; x < 8; x++)
  393. {
  394. for (int y = 0; y < 16; y++)
  395. {
  396. STBIR_ASSERT(output_data[y * 16 + x] == output_left[y * 8 + x]);
  397. STBIR_ASSERT(output_data[y * 16 + x + 8] == output_right[y * 8 + x]);
  398. }
  399. }}
  400. }
  401. // test that replicating an image and using a subtile of it produces same results as wraparound
  402. void test_subpixel_2()
  403. {
  404. unsigned char image[8 * 8];
  405. mtsrand(0);
  406. for (int i = 0; i < sizeof(image); i++)
  407. image[i] = mtrand() & 255;
  408. unsigned char large_image[32 * 32];
  409. for (int x = 0; x < 8; x++)
  410. {
  411. for (int y = 0; y < 8; y++)
  412. {
  413. for (int i = 0; i < 4; i++)
  414. {
  415. for (int j = 0; j < 4; j++)
  416. large_image[j*4*8*8 + i*8 + y*4*8 + x] = image[y*8 + x];
  417. }
  418. }
  419. }
  420. unsigned char output_data_1[16 * 16];
  421. unsigned char output_data_2[16 * 16];
  422. stbir_resize(image, 8, 8, 0, output_data_1, 16, 16, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_WRAP, STBIR_EDGE_WRAP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context);
  423. stbir_resize_region(large_image, 32, 32, 0, output_data_2, 16, 16, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_WRAP, STBIR_EDGE_WRAP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context, 0.25f, 0.25f, 0.5f, 0.5f);
  424. {for (int x = 0; x < 16; x++)
  425. {
  426. for (int y = 0; y < 16; y++)
  427. STBIR_ASSERT(output_data_1[y * 16 + x] == output_data_2[y * 16 + x]);
  428. }}
  429. stbir_resize_subpixel(large_image, 32, 32, 0, output_data_2, 16, 16, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_WRAP, STBIR_EDGE_WRAP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context, 2, 2, 16, 16);
  430. {for (int x = 0; x < 16; x++)
  431. {
  432. for (int y = 0; y < 16; y++)
  433. STBIR_ASSERT(output_data_1[y * 16 + x] == output_data_2[y * 16 + x]);
  434. }}
  435. }
  436. // test that 0,0,1,1 subpixel produces same result as no-rect
  437. void test_subpixel_3()
  438. {
  439. unsigned char image[8 * 8];
  440. mtsrand(0);
  441. for (int i = 0; i < sizeof(image); i++)
  442. image[i] = mtrand() & 255;
  443. unsigned char output_data_1[32 * 32];
  444. unsigned char output_data_2[32 * 32];
  445. stbir_resize_region(image, 8, 8, 0, output_data_1, 32, 32, 0, STBIR_TYPE_UINT8, 1, 0, STBIR_ALPHA_CHANNEL_NONE, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_LINEAR, NULL, 0, 0, 1, 1);
  446. stbir_resize_uint8(image, 8, 8, 0, output_data_2, 32, 32, 0, 1);
  447. for (int x = 0; x < 32; x++)
  448. {
  449. for (int y = 0; y < 32; y++)
  450. STBIR_ASSERT(output_data_1[y * 32 + x] == output_data_2[y * 32 + x]);
  451. }
  452. stbir_resize_subpixel(image, 8, 8, 0, output_data_1, 32, 32, 0, STBIR_TYPE_UINT8, 1, 0, STBIR_ALPHA_CHANNEL_NONE, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_LINEAR, NULL, 4, 4, 0, 0);
  453. {for (int x = 0; x < 32; x++)
  454. {
  455. for (int y = 0; y < 32; y++)
  456. STBIR_ASSERT(output_data_1[y * 32 + x] == output_data_2[y * 32 + x]);
  457. }}
  458. }
  459. // test that 1:1 resample using s,t=0,0,1,1 with bilinear produces original image
  460. void test_subpixel_4()
  461. {
  462. unsigned char image[8 * 8];
  463. mtsrand(0);
  464. for (int i = 0; i < sizeof(image); i++)
  465. image[i] = mtrand() & 255;
  466. unsigned char output[8 * 8];
  467. stbir_resize_region(image, 8, 8, 0, output, 8, 8, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_TRIANGLE, STBIR_FILTER_TRIANGLE, STBIR_COLORSPACE_LINEAR, &g_context, 0, 0, 1, 1);
  468. STBIR_ASSERT(memcmp(image, output, 8 * 8) == 0);
  469. stbir_resize_subpixel(image, 8, 8, 0, output, 8, 8, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_TRIANGLE, STBIR_FILTER_TRIANGLE, STBIR_COLORSPACE_LINEAR, &g_context, 1, 1, 0, 0);
  470. STBIR_ASSERT(memcmp(image, output, 8 * 8) == 0);
  471. }
  472. static unsigned int image88_int[8][8];
  473. static unsigned char image88 [8][8];
  474. static unsigned char output88[8][8];
  475. static unsigned char output44[4][4];
  476. static unsigned char output22[2][2];
  477. static unsigned char output11[1][1];
  478. void resample_88(stbir_filter filter)
  479. {
  480. stbir_resize_uint8_generic(image88[0],8,8,0, output88[0],8,8,0, 1,-1,0, STBIR_EDGE_CLAMP, filter, STBIR_COLORSPACE_LINEAR, NULL);
  481. stbir_resize_uint8_generic(image88[0],8,8,0, output44[0],4,4,0, 1,-1,0, STBIR_EDGE_CLAMP, filter, STBIR_COLORSPACE_LINEAR, NULL);
  482. stbir_resize_uint8_generic(image88[0],8,8,0, output22[0],2,2,0, 1,-1,0, STBIR_EDGE_CLAMP, filter, STBIR_COLORSPACE_LINEAR, NULL);
  483. stbir_resize_uint8_generic(image88[0],8,8,0, output11[0],1,1,0, 1,-1,0, STBIR_EDGE_CLAMP, filter, STBIR_COLORSPACE_LINEAR, NULL);
  484. }
  485. void verify_box(void)
  486. {
  487. int i,j,t;
  488. resample_88(STBIR_FILTER_BOX);
  489. for (i=0; i < sizeof(image88); ++i)
  490. STBIR_ASSERT(image88[0][i] == output88[0][i]);
  491. t = 0;
  492. for (j=0; j < 4; ++j)
  493. for (i=0; i < 4; ++i) {
  494. int n = image88[j*2+0][i*2+0]
  495. + image88[j*2+0][i*2+1]
  496. + image88[j*2+1][i*2+0]
  497. + image88[j*2+1][i*2+1];
  498. STBIR_ASSERT(output44[j][i] == ((n+2)>>2) || output44[j][i] == ((n+1)>>2)); // can't guarantee exact rounding due to numerical precision
  499. t += n;
  500. }
  501. STBIR_ASSERT(output11[0][0] == ((t+32)>>6) || output11[0][0] == ((t+31)>>6)); // can't guarantee exact rounding due to numerical precision
  502. }
  503. void verify_filter_normalized(stbir_filter filter, int output_size, unsigned int value)
  504. {
  505. int i, j;
  506. unsigned int output[64];
  507. stbir_resize(image88_int[0], 8, 8, 0, output, output_size, output_size, 0, STBIR_TYPE_UINT32, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, filter, filter, STBIR_COLORSPACE_LINEAR, NULL);
  508. for (j = 0; j < output_size; ++j)
  509. for (i = 0; i < output_size; ++i)
  510. STBIR_ASSERT(value == output[j*output_size + i]);
  511. }
  512. float round2(float f)
  513. {
  514. return (float) floor(f+0.5f); // round() isn't C standard pre-C99
  515. }
  516. void test_filters(void)
  517. {
  518. int i,j;
  519. mtsrand(0);
  520. for (i=0; i < sizeof(image88); ++i)
  521. image88[0][i] = mtrand() & 255;
  522. verify_box();
  523. for (i=0; i < sizeof(image88); ++i)
  524. image88[0][i] = 0;
  525. image88[4][4] = 255;
  526. verify_box();
  527. for (j=0; j < 8; ++j)
  528. for (i=0; i < 8; ++i)
  529. image88[j][i] = (j^i)&1 ? 255 : 0;
  530. verify_box();
  531. for (j=0; j < 8; ++j)
  532. for (i=0; i < 8; ++i)
  533. image88[j][i] = i&2 ? 255 : 0;
  534. verify_box();
  535. int value = 64;
  536. for (j = 0; j < 8; ++j)
  537. for (i = 0; i < 8; ++i)
  538. image88_int[j][i] = value;
  539. verify_filter_normalized(STBIR_FILTER_BOX, 8, value);
  540. verify_filter_normalized(STBIR_FILTER_TRIANGLE, 8, value);
  541. verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 8, value);
  542. verify_filter_normalized(STBIR_FILTER_CATMULLROM, 8, value);
  543. verify_filter_normalized(STBIR_FILTER_MITCHELL, 8, value);
  544. verify_filter_normalized(STBIR_FILTER_BOX, 4, value);
  545. verify_filter_normalized(STBIR_FILTER_TRIANGLE, 4, value);
  546. verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 4, value);
  547. verify_filter_normalized(STBIR_FILTER_CATMULLROM, 4, value);
  548. verify_filter_normalized(STBIR_FILTER_MITCHELL, 4, value);
  549. verify_filter_normalized(STBIR_FILTER_BOX, 2, value);
  550. verify_filter_normalized(STBIR_FILTER_TRIANGLE, 2, value);
  551. verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 2, value);
  552. verify_filter_normalized(STBIR_FILTER_CATMULLROM, 2, value);
  553. verify_filter_normalized(STBIR_FILTER_MITCHELL, 2, value);
  554. verify_filter_normalized(STBIR_FILTER_BOX, 1, value);
  555. verify_filter_normalized(STBIR_FILTER_TRIANGLE, 1, value);
  556. verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 1, value);
  557. verify_filter_normalized(STBIR_FILTER_CATMULLROM, 1, value);
  558. verify_filter_normalized(STBIR_FILTER_MITCHELL, 1, value);
  559. {
  560. // This test is designed to produce coefficients that are very badly denormalized.
  561. unsigned int v = 556;
  562. unsigned int input[100 * 100];
  563. unsigned int output[11 * 11];
  564. for (j = 0; j < 100 * 100; ++j)
  565. input[j] = v;
  566. stbir_resize(input, 100, 100, 0, output, 11, 11, 0, STBIR_TYPE_UINT32, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_TRIANGLE, STBIR_FILTER_TRIANGLE, STBIR_COLORSPACE_LINEAR, NULL);
  567. for (j = 0; j < 11 * 11; ++j)
  568. STBIR_ASSERT(v == output[j]);
  569. }
  570. {
  571. // Now test the trapezoid filter for downsampling.
  572. unsigned int input[3 * 1];
  573. unsigned int output[2 * 1];
  574. input[0] = 0;
  575. input[1] = 255;
  576. input[2] = 127;
  577. stbir_resize(input, 3, 1, 0, output, 2, 1, 0, STBIR_TYPE_UINT32, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, STBIR_FILTER_BOX, STBIR_COLORSPACE_LINEAR, NULL);
  578. STBIR_ASSERT(output[0] == (unsigned int)round2((float)(input[0] * 2 + input[1]) / 3));
  579. STBIR_ASSERT(output[1] == (unsigned int)round2((float)(input[2] * 2 + input[1]) / 3));
  580. stbir_resize(input, 1, 3, 0, output, 1, 2, 0, STBIR_TYPE_UINT32, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, STBIR_FILTER_BOX, STBIR_COLORSPACE_LINEAR, NULL);
  581. STBIR_ASSERT(output[0] == (unsigned int)round2((float)(input[0] * 2 + input[1]) / 3));
  582. STBIR_ASSERT(output[1] == (unsigned int)round2((float)(input[2] * 2 + input[1]) / 3));
  583. }
  584. {
  585. // Now test the trapezoid filter for upsampling.
  586. unsigned int input[2 * 1];
  587. unsigned int output[3 * 1];
  588. input[0] = 0;
  589. input[1] = 255;
  590. stbir_resize(input, 2, 1, 0, output, 3, 1, 0, STBIR_TYPE_UINT32, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, STBIR_FILTER_BOX, STBIR_COLORSPACE_LINEAR, NULL);
  591. STBIR_ASSERT(output[0] == input[0]);
  592. STBIR_ASSERT(output[1] == (input[0] + input[1]) / 2);
  593. STBIR_ASSERT(output[2] == input[1]);
  594. stbir_resize(input, 1, 2, 0, output, 1, 3, 0, STBIR_TYPE_UINT32, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, STBIR_FILTER_BOX, STBIR_COLORSPACE_LINEAR, NULL);
  595. STBIR_ASSERT(output[0] == input[0]);
  596. STBIR_ASSERT(output[1] == (input[0] + input[1]) / 2);
  597. STBIR_ASSERT(output[2] == input[1]);
  598. }
  599. // checkerboard
  600. {
  601. unsigned char input[64][64];
  602. unsigned char output[16][16];
  603. int i,j;
  604. for (j=0; j < 64; ++j)
  605. for (i=0; i < 64; ++i)
  606. input[j][i] = (i^j)&1 ? 255 : 0;
  607. stbir_resize_uint8_generic(input[0], 64, 64, 0, output[0],16,16,0, 1,-1,0,STBIR_EDGE_WRAP,STBIR_FILTER_DEFAULT,STBIR_COLORSPACE_LINEAR,0);
  608. for (j=0; j < 16; ++j)
  609. for (i=0; i < 16; ++i)
  610. STBIR_ASSERT(output[j][i] == 128);
  611. stbir_resize_uint8_srgb_edgemode(input[0], 64, 64, 0, output[0],16,16,0, 1,-1,0,STBIR_EDGE_WRAP);
  612. for (j=0; j < 16; ++j)
  613. for (i=0; i < 16; ++i)
  614. STBIR_ASSERT(output[j][i] == 188);
  615. }
  616. {
  617. // Test trapezoid box filter
  618. unsigned char input[2 * 1];
  619. unsigned char output[127 * 1];
  620. input[0] = 0;
  621. input[1] = 255;
  622. stbir_resize(input, 2, 1, 0, output, 127, 1, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, STBIR_FILTER_BOX, STBIR_COLORSPACE_LINEAR, NULL);
  623. STBIR_ASSERT(output[0] == 0);
  624. STBIR_ASSERT(output[127 / 2 - 1] == 0);
  625. STBIR_ASSERT(output[127 / 2] == 128);
  626. STBIR_ASSERT(output[127 / 2 + 1] == 255);
  627. STBIR_ASSERT(output[126] == 255);
  628. stbi_write_png("test-output/trapezoid-upsample-horizontal.png", 127, 1, 1, output, 0);
  629. stbir_resize(input, 1, 2, 0, output, 1, 127, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, STBIR_FILTER_BOX, STBIR_COLORSPACE_LINEAR, NULL);
  630. STBIR_ASSERT(output[0] == 0);
  631. STBIR_ASSERT(output[127 / 2 - 1] == 0);
  632. STBIR_ASSERT(output[127 / 2] == 128);
  633. STBIR_ASSERT(output[127 / 2 + 1] == 255);
  634. STBIR_ASSERT(output[126] == 255);
  635. stbi_write_png("test-output/trapezoid-upsample-vertical.png", 1, 127, 1, output, 0);
  636. }
  637. }
  638. #define UMAX32 4294967295U
  639. static void write32(const char *filename, stbir_uint32 *output, int w, int h)
  640. {
  641. stbir_uint8 *data = (stbir_uint8*) malloc(w*h*3);
  642. for (int i=0; i < w*h*3; ++i)
  643. data[i] = output[i]>>24;
  644. stbi_write_png(filename, w, h, 3, data, 0);
  645. free(data);
  646. }
  647. static void test_32(void)
  648. {
  649. int w=100,h=120,x,y, out_w,out_h;
  650. stbir_uint32 *input = (stbir_uint32*) malloc(4 * 3 * w * h);
  651. stbir_uint32 *output = (stbir_uint32*) malloc(4 * 3 * 3*w * 3*h);
  652. for (y=0; y < h; ++y) {
  653. for (x=0; x < w; ++x) {
  654. input[y*3*w + x*3 + 0] = x * ( UMAX32/w );
  655. input[y*3*w + x*3 + 1] = y * ( UMAX32/h );
  656. input[y*3*w + x*3 + 2] = UMAX32/2;
  657. }
  658. }
  659. out_w = w*33/16;
  660. out_h = h*33/16;
  661. stbir_resize(input,w,h,0,output,out_w,out_h,0,STBIR_TYPE_UINT32,3,-1,0,STBIR_EDGE_CLAMP,STBIR_EDGE_CLAMP,STBIR_FILTER_DEFAULT,STBIR_FILTER_DEFAULT,STBIR_COLORSPACE_LINEAR,NULL);
  662. write32("test-output/seantest_1.png", output,out_w,out_h);
  663. out_w = w*16/33;
  664. out_h = h*16/33;
  665. stbir_resize(input,w,h,0,output,out_w,out_h,0,STBIR_TYPE_UINT32,3,-1,0,STBIR_EDGE_CLAMP,STBIR_EDGE_CLAMP,STBIR_FILTER_DEFAULT,STBIR_FILTER_DEFAULT,STBIR_COLORSPACE_LINEAR,NULL);
  666. write32("test-output/seantest_2.png", output,out_w,out_h);
  667. }
  668. void test_suite(int argc, char **argv)
  669. {
  670. int i;
  671. const char *barbara;
  672. mkdir("test-output", 777);
  673. if (argc > 1)
  674. barbara = argv[1];
  675. else
  676. barbara = "barbara.png";
  677. // check what cases we need normalization for
  678. #if 1
  679. {
  680. float x, y;
  681. for (x = -1; x < 1; x += 0.05f) {
  682. float sums[5] = { 0 };
  683. float o;
  684. for (o = -5; o <= 5; ++o) {
  685. sums[0] += stbir__filter_mitchell(x + o, 1);
  686. sums[1] += stbir__filter_catmullrom(x + o, 1);
  687. sums[2] += stbir__filter_cubic(x + o, 1);
  688. sums[3] += stbir__filter_triangle(x + o, 1);
  689. sums[4] += stbir__filter_trapezoid(x + o, 0.5f);
  690. }
  691. for (i = 0; i < 5; ++i)
  692. STBIR_ASSERT(sums[i] >= 1.0 - 0.001 && sums[i] <= 1.0 + 0.001);
  693. }
  694. #if 1
  695. for (y = 0.11f; y < 1; y += 0.01f) { // Step
  696. for (x = -1; x < 1; x += 0.05f) { // Phase
  697. float sums[5] = { 0 };
  698. float o;
  699. for (o = -5; o <= 5; o += y) {
  700. sums[0] += y * stbir__filter_mitchell(x + o, 1);
  701. sums[1] += y * stbir__filter_catmullrom(x + o, 1);
  702. sums[2] += y * stbir__filter_cubic(x + o, 1);
  703. sums[4] += y * stbir__filter_trapezoid(x + o, 0.5f);
  704. sums[3] += y * stbir__filter_triangle(x + o, 1);
  705. }
  706. for (i = 0; i < 3; ++i)
  707. STBIR_ASSERT(sums[i] >= 1.0 - 0.0170 && sums[i] <= 1.0 + 0.0170);
  708. }
  709. }
  710. #endif
  711. }
  712. #endif
  713. #if 0 // linear_to_srgb_uchar table
  714. for (i=0; i < 256; ++i) {
  715. float f = stbir__srgb_to_linear((i-0.5f)/255.0f);
  716. printf("%9d, ", (int) ((f) * (1<<28)));
  717. if ((i & 7) == 7)
  718. printf("\n");
  719. }
  720. #endif
  721. // old tests that hacky fix worked on - test that
  722. // every uint8 maps to itself
  723. for (i = 0; i < 256; i++) {
  724. float f = stbir__srgb_to_linear(float(i) / 255);
  725. int n = stbir__linear_to_srgb_uchar(f);
  726. STBIR_ASSERT(n == i);
  727. }
  728. // new tests that hacky fix failed for - test that
  729. // values adjacent to uint8 round to nearest uint8
  730. for (i = 0; i < 256; i++) {
  731. for (float y = -0.42f; y <= 0.42f; y += 0.01f) {
  732. float f = stbir__srgb_to_linear((i+y) / 255.0f);
  733. int n = stbir__linear_to_srgb_uchar(f);
  734. STBIR_ASSERT(n == i);
  735. }
  736. }
  737. test_filters();
  738. test_subpixel_1();
  739. test_subpixel_2();
  740. test_subpixel_3();
  741. test_subpixel_4();
  742. test_premul();
  743. test_32();
  744. // Some tests to make sure errors don't pop up with strange filter/dimension combinations.
  745. stbir_resize(image88, 8, 8, 0, output88, 4, 16, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context);
  746. stbir_resize(image88, 8, 8, 0, output88, 4, 16, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_BOX, STBIR_COLORSPACE_SRGB, &g_context);
  747. stbir_resize(image88, 8, 8, 0, output88, 16, 4, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context);
  748. stbir_resize(image88, 8, 8, 0, output88, 16, 4, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_BOX, STBIR_COLORSPACE_SRGB, &g_context);
  749. int barbara_width, barbara_height, barbara_channels;
  750. stbi_image_free(stbi_load(barbara, &barbara_width, &barbara_height, &barbara_channels, 0));
  751. int res = 10;
  752. // Downscaling
  753. {for (int i = 0; i <= res; i++)
  754. {
  755. float t = (float)i/res;
  756. float scale = 0.5;
  757. float out_scale = 2.0f/3;
  758. float x_shift = (barbara_width*out_scale - barbara_width*scale) * t;
  759. float y_shift = (barbara_height*out_scale - barbara_height*scale) * t;
  760. test_subpixel_command(barbara, scale, scale, out_scale, out_scale, x_shift, y_shift);
  761. }}
  762. // Upscaling
  763. {for (int i = 0; i <= res; i++)
  764. {
  765. float t = (float)i/res;
  766. float scale = 2;
  767. float out_scale = 3;
  768. float x_shift = (barbara_width*out_scale - barbara_width*scale) * t;
  769. float y_shift = (barbara_height*out_scale - barbara_height*scale) * t;
  770. test_subpixel_command(barbara, scale, scale, out_scale, out_scale, x_shift, y_shift);
  771. }}
  772. // Downscaling
  773. {for (int i = 0; i <= res; i++)
  774. {
  775. float t = (float)i/res / 2;
  776. test_subpixel_region(barbara, 0.25f, 0.25f, t, t, t+0.5f, t+0.5f);
  777. }}
  778. // No scaling
  779. {for (int i = 0; i <= res; i++)
  780. {
  781. float t = (float)i/res / 2;
  782. test_subpixel_region(barbara, 0.5f, 0.5f, t, t, t+0.5f, t+0.5f);
  783. }}
  784. // Upscaling
  785. {for (int i = 0; i <= res; i++)
  786. {
  787. float t = (float)i/res / 2;
  788. test_subpixel_region(barbara, 1, 1, t, t, t+0.5f, t+0.5f);
  789. }}
  790. {for (i = 0; i < 10; i++)
  791. test_subpixel(barbara, 0.5f, 0.5f, (float)i / 10, 1);
  792. }
  793. {for (i = 0; i < 10; i++)
  794. test_subpixel(barbara, 0.5f, 0.5f, 1, (float)i / 10);
  795. }
  796. {for (i = 0; i < 10; i++)
  797. test_subpixel(barbara, 2, 2, (float)i / 10, 1);
  798. }
  799. {for (i = 0; i < 10; i++)
  800. test_subpixel(barbara, 2, 2, 1, (float)i / 10);
  801. }
  802. // Channels test
  803. test_channels(barbara, 0.5f, 0.5f, 1);
  804. test_channels(barbara, 0.5f, 0.5f, 2);
  805. test_channels(barbara, 0.5f, 0.5f, 3);
  806. test_channels(barbara, 0.5f, 0.5f, 4);
  807. test_channels(barbara, 2, 2, 1);
  808. test_channels(barbara, 2, 2, 2);
  809. test_channels(barbara, 2, 2, 3);
  810. test_channels(barbara, 2, 2, 4);
  811. // filter tests
  812. resize_image(barbara, 2, 2, STBIR_FILTER_BOX , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-nearest.png");
  813. resize_image(barbara, 2, 2, STBIR_FILTER_TRIANGLE , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-bilinear.png");
  814. resize_image(barbara, 2, 2, STBIR_FILTER_CUBICBSPLINE, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-bicubic.png");
  815. resize_image(barbara, 2, 2, STBIR_FILTER_CATMULLROM , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-catmullrom.png");
  816. resize_image(barbara, 2, 2, STBIR_FILTER_MITCHELL , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-mitchell.png");
  817. resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_BOX , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-nearest.png");
  818. resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_TRIANGLE , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-bilinear.png");
  819. resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_CUBICBSPLINE, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-bicubic.png");
  820. resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_CATMULLROM , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-catmullrom.png");
  821. resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_MITCHELL , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-mitchell.png");
  822. {for (i = 10; i < 100; i++)
  823. {
  824. char outname[200];
  825. sprintf(outname, "test-output/barbara-width-%d.jpg", i);
  826. resize_image(barbara, (float)i / 100, 1, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);
  827. }}
  828. {for (i = 110; i < 500; i += 10)
  829. {
  830. char outname[200];
  831. sprintf(outname, "test-output/barbara-width-%d.jpg", i);
  832. resize_image(barbara, (float)i / 100, 1, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);
  833. }}
  834. {for (i = 10; i < 100; i++)
  835. {
  836. char outname[200];
  837. sprintf(outname, "test-output/barbara-height-%d.jpg", i);
  838. resize_image(barbara, 1, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);
  839. }}
  840. {for (i = 110; i < 500; i += 10)
  841. {
  842. char outname[200];
  843. sprintf(outname, "test-output/barbara-height-%d.jpg", i);
  844. resize_image(barbara, 1, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);
  845. }}
  846. {for (i = 50; i < 200; i += 10)
  847. {
  848. char outname[200];
  849. sprintf(outname, "test-output/barbara-width-height-%d.jpg", i);
  850. resize_image(barbara, 100 / (float)i, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);
  851. }}
  852. test_format<unsigned short>(barbara, 0.5, 2.0, STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB);
  853. test_format<unsigned short>(barbara, 0.5, 2.0, STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR);
  854. test_format<unsigned short>(barbara, 2.0, 0.5, STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB);
  855. test_format<unsigned short>(barbara, 2.0, 0.5, STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR);
  856. test_format<unsigned int>(barbara, 0.5, 2.0, STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB);
  857. test_format<unsigned int>(barbara, 0.5, 2.0, STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR);
  858. test_format<unsigned int>(barbara, 2.0, 0.5, STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB);
  859. test_format<unsigned int>(barbara, 2.0, 0.5, STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR);
  860. test_float(barbara, 0.5, 2.0, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB);
  861. test_float(barbara, 0.5, 2.0, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR);
  862. test_float(barbara, 2.0, 0.5, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB);
  863. test_float(barbara, 2.0, 0.5, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR);
  864. // Edge behavior tests
  865. resize_image("hgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR, "test-output/hgradient-clamp.png");
  866. resize_image("hgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_WRAP, STBIR_COLORSPACE_LINEAR, "test-output/hgradient-wrap.png");
  867. resize_image("vgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR, "test-output/vgradient-clamp.png");
  868. resize_image("vgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_WRAP, STBIR_COLORSPACE_LINEAR, "test-output/vgradient-wrap.png");
  869. resize_image("1px-border.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_LINEAR, "test-output/1px-border-reflect.png");
  870. resize_image("1px-border.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR, "test-output/1px-border-clamp.png");
  871. // sRGB tests
  872. resize_image("gamma_colors.jpg", .5f, .5f, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_SRGB, "test-output/gamma_colors.jpg");
  873. resize_image("gamma_2.2.jpg", .5f, .5f, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_SRGB, "test-output/gamma_2.2.jpg");
  874. resize_image("gamma_dalai_lama_gray.jpg", .5f, .5f, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_SRGB, "test-output/gamma_dalai_lama_gray.jpg");
  875. }