Skip to content
Myr

resample_test.cpp

This file

Path

tests/resample_test.cpp

1,127 lines · 38.4 KiB · C++

Last changed by

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

History of this file

Take it

1#define _CRT_SECURE_NO_WARNINGS2#include <stdlib.h>3#include <stdio.h>45#if defined(_WIN32) && _MSC_VER > 12006#define STBIR_ASSERT(x) \7	if (!(x)) {         \8		__debugbreak();  \9	} else10#else11#include <assert.h>12#define STBIR_ASSERT(x) assert(x)13#endif1415#define STBIR_MALLOC stbir_malloc16#define STBIR_FREE stbir_free1718class stbir_context {19public:20	stbir_context()21	{22		size = 1000000;23		memory = malloc(size);24	}2526	~stbir_context()27	{28		free(memory);29	}3031	size_t size;32	void* memory;33} g_context;3435void* stbir_malloc(size_t size, void* context)36{37	if (!context)38		return malloc(size);3940	stbir_context* real_context = (stbir_context*)context;41	if (size > real_context->size)42		return 0;4344	return real_context->memory;45}4647void stbir_free(void* memory, void* context)48{49	if (!context)50		free(memory);51}5253//#include <stdio.h>54void stbir_progress(float p)55{56	//printf("%f\n", p);57	STBIR_ASSERT(p >= 0 && p <= 1);58}5960#ifdef __clang__61#define STBIRDEF static inline62#endif6364#define STBIR_PROGRESS_REPORT stbir_progress65#define STB_IMAGE_RESIZE_IMPLEMENTATION66#define STB_IMAGE_RESIZE_STATIC67#include "stb_image_resize2.h"6869#define STB_IMAGE_WRITE_IMPLEMENTATION70#include "stb_image_write.h"7172#define STB_IMAGE_IMPLEMENTATION73#include "stb_image.h"7475#ifdef _WIN3276#include <sys/timeb.h>77#include <direct.h>78#define mkdir(a, b) _mkdir(a)79#else80#include <sys/stat.h>81#endif8283#define MT_SIZE 62484static size_t g_aiMT[MT_SIZE];85static size_t g_iMTI = 0;8687// 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.89void 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	}99100	g_iMTI = 0;101}102103size_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	}115116	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);121122	g_iMTI = (g_iMTI + 1) % MT_SIZE;123124	return y;125}126127128inline float mtfrand()129{130	const int ninenine = 999999;131	return (float)(mtrand() % ninenine)/ninenine;132}133134void 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_linear(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, (stbir_pixel_layout) n);147	stbi_write_png("output.png", out_w, out_h, n, output_pixels, 0);148	exit(0);149}150151void 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 0160    out_w = w/4; out_h = h/4; count=100; // 1161    #elif 0162	out_w = w*2; out_h = h/4; count=20; // 2   // note this is structured pessimily, would be much faster to downsample vertically first163    #elif 0164    out_w = w/4; out_h = h*2; count=50; // 3165    #elif 0166    out_w = w*3; out_h = h*3; count=2; srgb=0; // 4167    #else168    out_w = w*3; out_h = h*3; count=2; // 5   // this is dominated by linear->sRGB conversion169    #endif170171	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, (stbir_pixel_layout) n);175        else176	        stbir_resize_uint8_linear(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, (stbir_pixel_layout) n);177	exit(0);178}179180void test_suite(int argc, char **argv);181182int main(int argc, char** argv)183{184	//resizer(argc, argv);185    //performance(argc, argv);186187	test_suite(argc, argv);188	return 0;189}190191#if 0192void resize_image(const char* filename, float width_percent, float height_percent, stbir_filter filter, stbir_edge edge, stbir_colorspace colorspace, const char* output_filename)193{194	int w, h, n;195196	unsigned char* input_data = stbi_load(filename, &w, &h, &n, 0);197	if (!input_data)198	{199		printf("Input image could not be loaded\n");200		return;201	}202203	int out_w = (int)(w * width_percent);204	int out_h = (int)(h * height_percent);205206	unsigned char* output_data = (unsigned char*)malloc(out_w * out_h * n);207208	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);209210	stbi_image_free(input_data);211212	stbi_write_png(output_filename, out_w, out_h, n, output_data, 0);213214	free(output_data);215}216217template <typename F, typename T>218void convert_image(const F* input, T* output, int length)219{220	double f = (pow(2.0, 8.0 * sizeof(T)) - 1) / (pow(2.0, 8.0 * sizeof(F)) - 1);221	for (int i = 0; i < length; i++)222		output[i] = (T)(((double)input[i]) * f);223}224225template <typename T>226void test_format(const char* file, float width_percent, float height_percent, stbir_datatype type, stbir_colorspace colorspace)227{228	int w, h, n;229	unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);230231	if (input_data == NULL)232		return;233234235	int new_w = (int)(w * width_percent);236	int new_h = (int)(h * height_percent);237238	T* T_data = (T*)malloc(w * h * n * sizeof(T));239    memset(T_data, 0, w*h*n*sizeof(T));240	convert_image<unsigned char, T>(input_data, T_data, w * h * n);241242	T* output_data = (T*)malloc(new_w * new_h * n * sizeof(T));243244	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);245246	free(T_data);247	stbi_image_free(input_data);248249	unsigned char* char_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(char));250	convert_image<T, unsigned char>(output_data, char_data, new_w * new_h * n);251252	char output[200];253	sprintf(output, "test-output/type-%d-%d-%d-%d-%s", type, colorspace, new_w, new_h, file);254	stbi_write_png(output, new_w, new_h, n, char_data, 0);255256	free(char_data);257	free(output_data);258}259260void convert_image_float(const unsigned char* input, float* output, int length)261{262	for (int i = 0; i < length; i++)263		output[i] = ((float)input[i])/255;264}265266void convert_image_float(const float* input, unsigned char* output, int length)267{268	for (int i = 0; i < length; i++)269		output[i] = (unsigned char)(stbir__saturate(input[i]) * 255);270}271272void test_float(const char* file, float width_percent, float height_percent, stbir_datatype type, stbir_colorspace colorspace)273{274	int w, h, n;275	unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);276277	if (input_data == NULL)278		return;279280	int new_w = (int)(w * width_percent);281	int new_h = (int)(h * height_percent);282283	float* T_data = (float*)malloc(w * h * n * sizeof(float));284	convert_image_float(input_data, T_data, w * h * n);285286	float* output_data = (float*)malloc(new_w * new_h * n * sizeof(float));287288	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);289290	free(T_data);291	stbi_image_free(input_data);292293	unsigned char* char_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(char));294	convert_image_float(output_data, char_data, new_w * new_h * n);295296	char output[200];297	sprintf(output, "test-output/type-%d-%d-%d-%d-%s", type, colorspace, new_w, new_h, file);298	stbi_write_png(output, new_w, new_h, n, char_data, 0);299300	free(char_data);301	free(output_data);302}303304void test_channels(const char* file, float width_percent, float height_percent, int channels)305{306	int w, h, n;307	unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);308309	if (input_data == NULL)310		return;311312	int new_w = (int)(w * width_percent);313	int new_h = (int)(h * height_percent);314315	unsigned char* channels_data = (unsigned char*)malloc(w * h * channels * sizeof(unsigned char));316317	for (int i = 0; i < w * h; i++)318	{319		int input_position = i * n;320		int output_position = i * channels;321322		for (int c = 0; c < channels; c++)323			channels_data[output_position + c] = input_data[input_position + stbir__min(c, n)];324	}325326	unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * channels * sizeof(unsigned char));327328	stbir_resize_uint8_srgb(channels_data, w, h, 0, output_data, new_w, new_h, 0, channels, STBIR_ALPHA_CHANNEL_NONE, 0);329330	free(channels_data);331	stbi_image_free(input_data);332333	char output[200];334	sprintf(output, "test-output/channels-%d-%d-%d-%s", channels, new_w, new_h, file);335	stbi_write_png(output, new_w, new_h, channels, output_data, 0);336337	free(output_data);338}339340void test_subpixel(const char* file, float width_percent, float height_percent, float s1, float t1)341{342	int w, h, n;343	unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);344345	if (input_data == NULL)346		return;347348	s1 = ((float)w - 1 + s1)/w;349	t1 = ((float)h - 1 + t1)/h;350351	int new_w = (int)(w * width_percent);352	int new_h = (int)(h * height_percent);353354	unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));355356	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);357358	stbi_image_free(input_data);359360	char output[200];361	sprintf(output, "test-output/subpixel-%d-%d-%f-%f-%s", new_w, new_h, s1, t1, file);362	stbi_write_png(output, new_w, new_h, n, output_data, 0);363364	free(output_data);365}366367void test_subpixel_region(const char* file, float width_percent, float height_percent, float s0, float t0, float s1, float t1)368{369	int w, h, n;370	unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);371372	if (input_data == NULL)373		return;374375	int new_w = (int)(w * width_percent);376	int new_h = (int)(h * height_percent);377378	unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));379380	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);381382	stbi_image_free(input_data);383384	char output[200];385	sprintf(output, "test-output/subpixel-region-%d-%d-%f-%f-%f-%f-%s", new_w, new_h, s0, t0, s1, t1, file);386	stbi_write_png(output, new_w, new_h, n, output_data, 0);387388	free(output_data);389}390391void test_subpixel_command(const char* file, float width_percent, float height_percent, float x_scale, float y_scale, float x_offset, float y_offset)392{393	int w, h, n;394	unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);395396	if (input_data == NULL)397		return;398399	int new_w = (int)(w * width_percent);400	int new_h = (int)(h * height_percent);401402	unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));403404	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);405406	stbi_image_free(input_data);407408	char output[200];409	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);410	stbi_write_png(output, new_w, new_h, n, output_data, 0);411412	free(output_data);413}414415unsigned int* pixel(unsigned int* buffer, int x, int y, int c, int w, int n)416{417	return &buffer[y*w*n + x*n + c];418}419420void test_premul()421{422	unsigned int input[2 * 2 * 4];423	unsigned int output[1 * 1 * 4];424	unsigned int output2[2 * 2 * 4];425426	memset(input, 0, sizeof(input));427428	// First a test to make sure premul is working properly.429430	// Top left - solid red431	*pixel(input, 0, 0, 0, 2, 4) = 255;432	*pixel(input, 0, 0, 3, 2, 4) = 255;433434	// Bottom left - solid red435	*pixel(input, 0, 1, 0, 2, 4) = 255;436	*pixel(input, 0, 1, 3, 2, 4) = 255;437438	// Top right - transparent green439	*pixel(input, 1, 0, 1, 2, 4) = 255;440	*pixel(input, 1, 0, 3, 2, 4) = 25;441442	// Bottom right - transparent green443	*pixel(input, 1, 1, 1, 2, 4) = 255;444	*pixel(input, 1, 1, 3, 2, 4) = 25;445446	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);447448	float r = (float)255 / 4294967296;449	float g = (float)255 / 4294967296;450	float ra = (float)255 / 4294967296;451	float ga = (float)25 / 4294967296;452	float a = (ra + ga) / 2;453454	STBIR_ASSERT(output[0] == (unsigned int)(r * ra / 2 / a * 4294967296 + 0.5f)); // 232455	STBIR_ASSERT(output[1] == (unsigned int)(g * ga / 2 / a * 4294967296 + 0.5f)); // 23456	STBIR_ASSERT(output[2] == 0);457	STBIR_ASSERT(output[3] == (unsigned int)(a * 4294967296 + 0.5f)); // 140458459	// Now a test to make sure it doesn't clobber existing values.460461	// Top right - completely transparent green462	*pixel(input, 1, 0, 1, 2, 4) = 255;463	*pixel(input, 1, 0, 3, 2, 4) = 0;464465	// Bottom right - completely transparent green466	*pixel(input, 1, 1, 1, 2, 4) = 255;467	*pixel(input, 1, 1, 3, 2, 4) = 0;468469	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);470471	STBIR_ASSERT(*pixel(output2, 0, 0, 0, 2, 4) == 255);472	STBIR_ASSERT(*pixel(output2, 0, 0, 1, 2, 4) == 0);473	STBIR_ASSERT(*pixel(output2, 0, 0, 2, 2, 4) == 0);474	STBIR_ASSERT(*pixel(output2, 0, 0, 3, 2, 4) == 255);475476	STBIR_ASSERT(*pixel(output2, 0, 1, 0, 2, 4) == 255);477	STBIR_ASSERT(*pixel(output2, 0, 1, 1, 2, 4) == 0);478	STBIR_ASSERT(*pixel(output2, 0, 1, 2, 2, 4) == 0);479	STBIR_ASSERT(*pixel(output2, 0, 1, 3, 2, 4) == 255);480481	STBIR_ASSERT(*pixel(output2, 1, 0, 0, 2, 4) == 0);482	STBIR_ASSERT(*pixel(output2, 1, 0, 1, 2, 4) == 255);483	STBIR_ASSERT(*pixel(output2, 1, 0, 2, 2, 4) == 0);484	STBIR_ASSERT(*pixel(output2, 1, 0, 3, 2, 4) == 0);485486	STBIR_ASSERT(*pixel(output2, 1, 1, 0, 2, 4) == 0);487	STBIR_ASSERT(*pixel(output2, 1, 1, 1, 2, 4) == 255);488	STBIR_ASSERT(*pixel(output2, 1, 1, 2, 2, 4) == 0);489	STBIR_ASSERT(*pixel(output2, 1, 1, 3, 2, 4) == 0);490}491492// test that splitting a pow-2 image into tiles produces identical results493void test_subpixel_1()494{495	unsigned char image[8 * 8];496497	mtsrand(0);498499	for (int i = 0; i < sizeof(image); i++)500		image[i] = mtrand() & 255;501502	unsigned char output_data[16 * 16];503504	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);505506	unsigned char output_left[8 * 16];507	unsigned char output_right[8 * 16];508509	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);510	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);511512	for (int x = 0; x < 8; x++)513	{514		for (int y = 0; y < 16; y++)515		{516			STBIR_ASSERT(output_data[y * 16 + x] == output_left[y * 8 + x]);517			STBIR_ASSERT(output_data[y * 16 + x + 8] == output_right[y * 8 + x]);518		}519	}520521	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);522	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);523524	{for (int x = 0; x < 8; x++)525	{526		for (int y = 0; y < 16; y++)527		{528			STBIR_ASSERT(output_data[y * 16 + x] == output_left[y * 8 + x]);529			STBIR_ASSERT(output_data[y * 16 + x + 8] == output_right[y * 8 + x]);530		}531	}}532}533534// test that replicating an image and using a subtile of it produces same results as wraparound535void test_subpixel_2()536{537	unsigned char image[8 * 8];538539	mtsrand(0);540541	for (int i = 0; i < sizeof(image); i++)542		image[i] = mtrand() & 255;543544	unsigned char large_image[32 * 32];545546	for (int x = 0; x < 8; x++)547	{548		for (int y = 0; y < 8; y++)549		{550			for (int i = 0; i < 4; i++)551			{552				for (int j = 0; j < 4; j++)553					large_image[j*4*8*8 + i*8 + y*4*8 + x] = image[y*8 + x];554			}555		}556	}557558	unsigned char output_data_1[16 * 16];559	unsigned char output_data_2[16 * 16];560561	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);562	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);563564	{for (int x = 0; x < 16; x++)565	{566		for (int y = 0; y < 16; y++)567			STBIR_ASSERT(output_data_1[y * 16 + x] == output_data_2[y * 16 + x]);568	}}569570	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);571572	{for (int x = 0; x < 16; x++)573	{574		for (int y = 0; y < 16; y++)575			STBIR_ASSERT(output_data_1[y * 16 + x] == output_data_2[y * 16 + x]);576	}}577}578579// test that 0,0,1,1 subpixel produces same result as no-rect580void test_subpixel_3()581{582	unsigned char image[8 * 8];583584	mtsrand(0);585586	for (int i = 0; i < sizeof(image); i++)587		image[i] = mtrand() & 255;588589	unsigned char output_data_1[32 * 32];590	unsigned char output_data_2[32 * 32];591592	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);593	stbir_resize_uint8(image, 8, 8, 0, output_data_2, 32, 32, 0, 1);594595	for (int x = 0; x < 32; x++)596	{597		for (int y = 0; y < 32; y++)598			STBIR_ASSERT(output_data_1[y * 32 + x] == output_data_2[y * 32 + x]);599	}600601	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);602603	{for (int x = 0; x < 32; x++)604	{605		for (int y = 0; y < 32; y++)606			STBIR_ASSERT(output_data_1[y * 32 + x] == output_data_2[y * 32 + x]);607	}}608}609610// test that 1:1 resample using s,t=0,0,1,1 with bilinear produces original image611void test_subpixel_4()612{613	unsigned char image[8 * 8];614615	mtsrand(0);616617	for (int i = 0; i < sizeof(image); i++)618		image[i] = mtrand() & 255;619620	unsigned char output[8 * 8];621622	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);623	STBIR_ASSERT(memcmp(image, output, 8 * 8) == 0);624625	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);626	STBIR_ASSERT(memcmp(image, output, 8 * 8) == 0);627}628629static unsigned int  image88_int[8][8];630static unsigned char image88 [8][8];631static unsigned char output88[8][8];632static unsigned char output44[4][4];633static unsigned char output22[2][2];634static unsigned char output11[1][1];635636void resample_88(stbir_filter filter)637{638	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);639	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);640	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);641	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);642}643644void verify_box(void)645{646	int i,j,t;647648	resample_88(STBIR_FILTER_BOX);649650	for (i=0; i < sizeof(image88); ++i)651		STBIR_ASSERT(image88[0][i] == output88[0][i]);652653	t = 0;654	for (j=0; j < 4; ++j)655		for (i=0; i < 4; ++i) {656			int n = image88[j*2+0][i*2+0]657			      + image88[j*2+0][i*2+1]658				  + image88[j*2+1][i*2+0]659				  + image88[j*2+1][i*2+1];660			STBIR_ASSERT(output44[j][i] == ((n+2)>>2) || output44[j][i] == ((n+1)>>2)); // can't guarantee exact rounding due to numerical precision661			t += n;662		}663	STBIR_ASSERT(output11[0][0] == ((t+32)>>6) || output11[0][0] == ((t+31)>>6)); // can't guarantee exact rounding due to numerical precision664}665666void verify_filter_normalized(stbir_filter filter, int output_size, unsigned int value)667{668	int i, j;669	unsigned int output[64];670671	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);672673	for (j = 0; j < output_size; ++j)674		for (i = 0; i < output_size; ++i)675			STBIR_ASSERT(value == output[j*output_size + i]);676}677678float round2(float f)679{680	return (float) floor(f+0.5f); // round() isn't C standard pre-C99681}682683void test_filters(void)684{685	int i,j;686687	mtsrand(0);688689	for (i=0; i < sizeof(image88); ++i)690		image88[0][i] = mtrand() & 255;691	verify_box();692693	for (i=0; i < sizeof(image88); ++i)694		image88[0][i] = 0;695	image88[4][4] = 255;696	verify_box();697698	for (j=0; j < 8; ++j)699		for (i=0; i < 8; ++i)700			image88[j][i] = (j^i)&1 ? 255 : 0;701	verify_box();702703	for (j=0; j < 8; ++j)704		for (i=0; i < 8; ++i)705			image88[j][i] = i&2 ? 255 : 0;706	verify_box();707708	int value = 64;709710	for (j = 0; j < 8; ++j)711		for (i = 0; i < 8; ++i)712			image88_int[j][i] = value;713714	verify_filter_normalized(STBIR_FILTER_BOX, 8, value);715	verify_filter_normalized(STBIR_FILTER_TRIANGLE, 8, value);716	verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 8, value);717	verify_filter_normalized(STBIR_FILTER_CATMULLROM, 8, value);718	verify_filter_normalized(STBIR_FILTER_MITCHELL, 8, value);719720	verify_filter_normalized(STBIR_FILTER_BOX, 4, value);721	verify_filter_normalized(STBIR_FILTER_TRIANGLE, 4, value);722	verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 4, value);723	verify_filter_normalized(STBIR_FILTER_CATMULLROM, 4, value);724	verify_filter_normalized(STBIR_FILTER_MITCHELL, 4, value);725726	verify_filter_normalized(STBIR_FILTER_BOX, 2, value);727	verify_filter_normalized(STBIR_FILTER_TRIANGLE, 2, value);728	verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 2, value);729	verify_filter_normalized(STBIR_FILTER_CATMULLROM, 2, value);730	verify_filter_normalized(STBIR_FILTER_MITCHELL, 2, value);731732	verify_filter_normalized(STBIR_FILTER_BOX, 1, value);733	verify_filter_normalized(STBIR_FILTER_TRIANGLE, 1, value);734	verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 1, value);735	verify_filter_normalized(STBIR_FILTER_CATMULLROM, 1, value);736	verify_filter_normalized(STBIR_FILTER_MITCHELL, 1, value);737738	{739		// This test is designed to produce coefficients that are very badly denormalized.740		unsigned int v = 556;741742		unsigned int input[100 * 100];743		unsigned int output[11 * 11];744745		for (j = 0; j < 100 * 100; ++j)746			input[j] = v;747748		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);749750		for (j = 0; j < 11 * 11; ++j)751			STBIR_ASSERT(v == output[j]);752	}753754	{755		// Now test the trapezoid filter for downsampling.756		unsigned int input[3 * 1];757		unsigned int output[2 * 1];758759		input[0] = 0;760		input[1] = 255;761		input[2] = 127;762763		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);764765		STBIR_ASSERT(output[0] == (unsigned int)round2((float)(input[0] * 2 + input[1]) / 3));766		STBIR_ASSERT(output[1] == (unsigned int)round2((float)(input[2] * 2 + input[1]) / 3));767768		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);769770		STBIR_ASSERT(output[0] == (unsigned int)round2((float)(input[0] * 2 + input[1]) / 3));771		STBIR_ASSERT(output[1] == (unsigned int)round2((float)(input[2] * 2 + input[1]) / 3));772	}773774	{775		// Now test the trapezoid filter for upsampling.776		unsigned int input[2 * 1];777		unsigned int output[3 * 1];778779		input[0] = 0;780		input[1] = 255;781782		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);783784		STBIR_ASSERT(output[0] == input[0]);785		STBIR_ASSERT(output[1] == (input[0] + input[1]) / 2);786		STBIR_ASSERT(output[2] == input[1]);787788		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);789790		STBIR_ASSERT(output[0] == input[0]);791		STBIR_ASSERT(output[1] == (input[0] + input[1]) / 2);792		STBIR_ASSERT(output[2] == input[1]);793	}794795	// checkerboard796	{797		unsigned char input[64][64];798		unsigned char output[16][16];799		int i,j;800		for (j=0; j < 64; ++j)801			for (i=0; i < 64; ++i)802				input[j][i] = (i^j)&1 ? 255 : 0;803		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);804		for (j=0; j < 16; ++j)805			for (i=0; i < 16; ++i)806				STBIR_ASSERT(output[j][i] == 128);807		stbir_resize_uint8_srgb_edgemode(input[0], 64, 64, 0, output[0],16,16,0, 1,-1,0,STBIR_EDGE_WRAP);808		for (j=0; j < 16; ++j)809			for (i=0; i < 16; ++i)810				STBIR_ASSERT(output[j][i] == 188);811812813	}814815	{816		// Test trapezoid box filter817		unsigned char input[2 * 1];818		unsigned char output[127 * 1];819820		input[0] = 0;821		input[1] = 255;822823		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);824		STBIR_ASSERT(output[0] == 0);825		STBIR_ASSERT(output[127 / 2 - 1] == 0);826		STBIR_ASSERT(output[127 / 2] == 128);827		STBIR_ASSERT(output[127 / 2 + 1] == 255);828		STBIR_ASSERT(output[126] == 255);829		stbi_write_png("test-output/trapezoid-upsample-horizontal.png", 127, 1, 1, output, 0);830831		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);832		STBIR_ASSERT(output[0] == 0);833		STBIR_ASSERT(output[127 / 2 - 1] == 0);834		STBIR_ASSERT(output[127 / 2] == 128);835		STBIR_ASSERT(output[127 / 2 + 1] == 255);836		STBIR_ASSERT(output[126] == 255);837		stbi_write_png("test-output/trapezoid-upsample-vertical.png", 1, 127, 1, output, 0);838	}839}840841#define UMAX32   4294967295U842843static void write32(const char *filename, stbir_uint32 *output, int w, int h)844{845    stbir_uint8 *data = (stbir_uint8*) malloc(w*h*3);846    for (int i=0; i < w*h*3; ++i)847        data[i] = output[i]>>24;848    stbi_write_png(filename, w, h, 3, data, 0);849    free(data);850}851852static void test_32(void)853{854    int w=100,h=120,x,y, out_w,out_h;855    stbir_uint32 *input  = (stbir_uint32*) malloc(4 * 3 * w * h);856    stbir_uint32 *output = (stbir_uint32*) malloc(4 * 3 * 3*w * 3*h);857    for (y=0; y < h; ++y) {858        for (x=0; x < w; ++x) {859            input[y*3*w + x*3 + 0] = x * ( UMAX32/w );860            input[y*3*w + x*3 + 1] = y * ( UMAX32/h );861            input[y*3*w + x*3 + 2] = UMAX32/2;862        }863    }864    out_w = w*33/16;865    out_h = h*33/16;866    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);867    write32("test-output/seantest_1.png", output,out_w,out_h);868869    out_w = w*16/33;870    out_h = h*16/33;871    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);872    write32("test-output/seantest_2.png", output,out_w,out_h);873}874875876void test_suite(int argc, char **argv)877{878	int i;879	const char *barbara;880881	mkdir("test-output", 777);882883	if (argc > 1)884		barbara = argv[1];885	else886		barbara = "barbara.png";887888	// check what cases we need normalization for889#if 1890	{891		float x, y;892		for (x = -1; x < 1; x += 0.05f) {893			float sums[5] = { 0 };894			float o;895			for (o = -5; o <= 5; ++o) {896				sums[0] += stbir__filter_mitchell(x + o, 1);897				sums[1] += stbir__filter_catmullrom(x + o, 1);898				sums[2] += stbir__filter_cubic(x + o, 1);899				sums[3] += stbir__filter_triangle(x + o, 1);900				sums[4] += stbir__filter_trapezoid(x + o, 0.5f);901			}902			for (i = 0; i < 5; ++i)903				STBIR_ASSERT(sums[i] >= 1.0 - 0.001 && sums[i] <= 1.0 + 0.001);904		}905906#if 1	907		for (y = 0.11f; y < 1; y += 0.01f) {  // Step908			for (x = -1; x < 1; x += 0.05f) { // Phase909				float sums[5] = { 0 };910				float o;911				for (o = -5; o <= 5; o += y) {912					sums[0] += y * stbir__filter_mitchell(x + o, 1);913					sums[1] += y * stbir__filter_catmullrom(x + o, 1);914					sums[2] += y * stbir__filter_cubic(x + o, 1);915					sums[4] += y * stbir__filter_trapezoid(x + o, 0.5f);916					sums[3] += y * stbir__filter_triangle(x + o, 1);917				}918				for (i = 0; i < 3; ++i)919					STBIR_ASSERT(sums[i] >= 1.0 - 0.0170 && sums[i] <= 1.0 + 0.0170);920			}921		}922#endif923	}924#endif925926#if 0 // linear_to_srgb_uchar table927	for (i=0; i < 256; ++i) {928		float f = stbir__srgb_to_linear((i-0.5f)/255.0f);929		printf("%9d, ", (int) ((f) * (1<<28)));930		if ((i & 7) == 7)931			printf("\n");932	}933#endif934935	// old tests that hacky fix worked on - test that936	// every uint8 maps to itself937	for (i = 0; i < 256; i++) {938		float f = stbir__srgb_to_linear(float(i) / 255);939		int n = stbir__linear_to_srgb_uchar(f);940		STBIR_ASSERT(n == i);941	}942943	// new tests that hacky fix failed for - test that944	// values adjacent to uint8 round to nearest uint8945	for (i = 0; i < 256; i++) {946		for (float y = -0.42f; y <= 0.42f; y += 0.01f) {947			float f = stbir__srgb_to_linear((i+y) / 255.0f);948			int n = stbir__linear_to_srgb_uchar(f);949			STBIR_ASSERT(n == i);950		}951	}952953	test_filters();954955	test_subpixel_1();956	test_subpixel_2();957	test_subpixel_3();958	test_subpixel_4();959960	test_premul();961962	test_32();963964	// Some tests to make sure errors don't pop up with strange filter/dimension combinations.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_BOX, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context);966	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);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_BOX, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context);968	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);969970	int barbara_width, barbara_height, barbara_channels;971	stbi_image_free(stbi_load(barbara, &barbara_width, &barbara_height, &barbara_channels, 0));972973	int res = 10;974	// Downscaling975	{for (int i = 0; i <= res; i++)976	{977		float t = (float)i/res;978		float scale = 0.5;979		float out_scale = 2.0f/3;980		float x_shift = (barbara_width*out_scale - barbara_width*scale) * t;981		float y_shift = (barbara_height*out_scale - barbara_height*scale) * t;982983		test_subpixel_command(barbara, scale, scale, out_scale, out_scale, x_shift, y_shift);984	}}985986	// Upscaling987	{for (int i = 0; i <= res; i++)988	{989		float t = (float)i/res;990		float scale = 2;991		float out_scale = 3;992		float x_shift = (barbara_width*out_scale - barbara_width*scale) * t;993		float y_shift = (barbara_height*out_scale - barbara_height*scale) * t;994995		test_subpixel_command(barbara, scale, scale, out_scale, out_scale, x_shift, y_shift);996	}}997998	// Downscaling999	{for (int i = 0; i <= res; i++)1000	{1001		float t = (float)i/res / 2;1002		test_subpixel_region(barbara, 0.25f, 0.25f, t, t, t+0.5f, t+0.5f);1003	}}10041005	// No scaling1006	{for (int i = 0; i <= res; i++)1007	{1008		float t = (float)i/res / 2;1009		test_subpixel_region(barbara, 0.5f, 0.5f, t, t, t+0.5f, t+0.5f);1010	}}10111012	// Upscaling1013	{for (int i = 0; i <= res; i++)1014	{1015		float t = (float)i/res / 2;1016		test_subpixel_region(barbara, 1, 1, t, t, t+0.5f, t+0.5f);1017	}}10181019	{for (i = 0; i < 10; i++)1020		test_subpixel(barbara, 0.5f, 0.5f, (float)i / 10, 1);1021   }10221023	{for (i = 0; i < 10; i++)1024		test_subpixel(barbara, 0.5f, 0.5f, 1, (float)i / 10);1025   }10261027	{for (i = 0; i < 10; i++)1028		test_subpixel(barbara, 2, 2, (float)i / 10, 1);1029   }10301031	{for (i = 0; i < 10; i++)1032		test_subpixel(barbara, 2, 2, 1, (float)i / 10);1033   }10341035	// Channels test1036	test_channels(barbara, 0.5f, 0.5f, 1);1037	test_channels(barbara, 0.5f, 0.5f, 2);1038	test_channels(barbara, 0.5f, 0.5f, 3);1039	test_channels(barbara, 0.5f, 0.5f, 4);10401041	test_channels(barbara, 2, 2, 1);1042	test_channels(barbara, 2, 2, 2);1043	test_channels(barbara, 2, 2, 3);1044	test_channels(barbara, 2, 2, 4);10451046	// filter tests1047	resize_image(barbara, 2, 2, STBIR_FILTER_BOX         , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-nearest.png");1048	resize_image(barbara, 2, 2, STBIR_FILTER_TRIANGLE    , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-bilinear.png");1049	resize_image(barbara, 2, 2, STBIR_FILTER_CUBICBSPLINE, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-bicubic.png");1050	resize_image(barbara, 2, 2, STBIR_FILTER_CATMULLROM  , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-catmullrom.png");1051	resize_image(barbara, 2, 2, STBIR_FILTER_MITCHELL    , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-mitchell.png");10521053	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_BOX         , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-nearest.png");1054	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_TRIANGLE    , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-bilinear.png");1055	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_CUBICBSPLINE, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-bicubic.png");1056	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_CATMULLROM  , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-catmullrom.png");1057	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_MITCHELL    , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-mitchell.png");10581059	{for (i = 10; i < 100; i++)1060	{1061		char outname[200];1062		sprintf(outname, "test-output/barbara-width-%d.jpg", i);1063		resize_image(barbara, (float)i / 100, 1, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);1064	}}10651066	{for (i = 110; i < 500; i += 10)1067	{1068		char outname[200];1069		sprintf(outname, "test-output/barbara-width-%d.jpg", i);1070		resize_image(barbara, (float)i / 100, 1, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);1071	}}10721073	{for (i = 10; i < 100; i++)1074	{1075		char outname[200];1076		sprintf(outname, "test-output/barbara-height-%d.jpg", i);1077		resize_image(barbara, 1, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);1078	}}10791080	{for (i = 110; i < 500; i += 10)1081	{1082		char outname[200];1083		sprintf(outname, "test-output/barbara-height-%d.jpg", i);1084		resize_image(barbara, 1, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);1085	}}10861087	{for (i = 50; i < 200; i += 10)1088	{1089		char outname[200];1090		sprintf(outname, "test-output/barbara-width-height-%d.jpg", i);1091		resize_image(barbara, 100 / (float)i, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);1092	}}10931094	test_format<unsigned short>(barbara, 0.5, 2.0, STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB);1095	test_format<unsigned short>(barbara, 0.5, 2.0, STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR);1096	test_format<unsigned short>(barbara, 2.0, 0.5, STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB);1097	test_format<unsigned short>(barbara, 2.0, 0.5, STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR);10981099	test_format<unsigned int>(barbara, 0.5, 2.0, STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB);1100	test_format<unsigned int>(barbara, 0.5, 2.0, STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR);1101	test_format<unsigned int>(barbara, 2.0, 0.5, STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB);1102	test_format<unsigned int>(barbara, 2.0, 0.5, STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR);11031104	test_float(barbara, 0.5, 2.0, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB);1105	test_float(barbara, 0.5, 2.0, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR);1106	test_float(barbara, 2.0, 0.5, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB);1107	test_float(barbara, 2.0, 0.5, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR);11081109	// Edge behavior tests1110	resize_image("hgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR, "test-output/hgradient-clamp.png");1111	resize_image("hgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_WRAP, STBIR_COLORSPACE_LINEAR, "test-output/hgradient-wrap.png");11121113	resize_image("vgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR, "test-output/vgradient-clamp.png");1114	resize_image("vgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_WRAP, STBIR_COLORSPACE_LINEAR, "test-output/vgradient-wrap.png");11151116	resize_image("1px-border.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_LINEAR, "test-output/1px-border-reflect.png");1117	resize_image("1px-border.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR, "test-output/1px-border-clamp.png");11181119	// sRGB tests1120	resize_image("gamma_colors.jpg", .5f, .5f, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_SRGB, "test-output/gamma_colors.jpg");1121	resize_image("gamma_2.2.jpg", .5f, .5f, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_SRGB, "test-output/gamma_2.2.jpg");1122	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");1123}1124#endif1125void test_suite(int argc, char **argv)1126{1127}