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