2
0

resample_test.cpp 38 KB

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