resample_test.cpp 38 KB

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