Skip to content
Myr

resample_test.cpp

This file

Path

tests/resample_test.cpp

983 lines · 33.3 KiB · C++

Last changed by

Merge branch 'master' into workingSean Barrettbefore adoption17e82a4

History of this file

Take it

1#include <malloc.h>23#if defined(_WIN32) && _MSC_VER > 12004#define STBIR_ASSERT(x) \5	if (!(x)) {         \6		__debugbreak();  \7	} else8#else9#include <assert.h>10#define STBIR_ASSERT(x) assert(x)11#endif1213#define STBIR_MALLOC stbir_malloc14#define STBIR_FREE stbir_free1516class stbir_context {17public:18	stbir_context()19	{20		size = 1000000;21		memory = malloc(size);22	}2324	~stbir_context()25	{26		free(memory);27	}2829	size_t size;30	void* memory;31} g_context;3233void* stbir_malloc(size_t size, void* context)34{35	if (!context)36		return malloc(size);3738	stbir_context* real_context = (stbir_context*)context;39	if (size > real_context->size)40		return 0;4142	return real_context->memory;43}4445void stbir_free(void* memory, void* context)46{47	if (!context)48		free(memory);49}5051//#include <stdio.h>52void stbir_progress(float p)53{54	//printf("%f\n", p);55	STBIR_ASSERT(p >= 0 && p <= 1);56}5758#define STBIR_PROGRESS_REPORT stbir_progress5960#define STB_IMAGE_RESIZE_IMPLEMENTATION61#define STB_IMAGE_RESIZE_STATIC62#include "stb_image_resize.h"6364#define STB_IMAGE_WRITE_IMPLEMENTATION65#include "stb_image_write.h"6667#define STB_IMAGE_IMPLEMENTATION68#include "stb_image.h"6970#ifdef _WIN3271#include <sys/timeb.h>72#endif7374#include <direct.h>7576#define MT_SIZE 62477static size_t g_aiMT[MT_SIZE];78static size_t g_iMTI = 0;7980// Mersenne Twister implementation from Wikipedia.81// Avoiding use of the system rand() to be sure that our tests generate the same test data on any system.82void mtsrand(size_t iSeed)83{84	g_aiMT[0] = iSeed;85	for (size_t i = 1; i < MT_SIZE; i++)86	{87		size_t inner1 = g_aiMT[i - 1];88		size_t inner2 = (g_aiMT[i - 1] >> 30);89		size_t inner = inner1 ^ inner2;90		g_aiMT[i] = (0x6c078965 * inner) + i;91	}9293	g_iMTI = 0;94}9596size_t mtrand()97{98	if (g_iMTI == 0)99	{100		for (size_t i = 0; i < MT_SIZE; i++)101		{102			size_t y = (0x80000000 & (g_aiMT[i])) + (0x7fffffff & (g_aiMT[(i + 1) % MT_SIZE]));103			g_aiMT[i] = g_aiMT[(i + 397) % MT_SIZE] ^ (y >> 1);104			if ((y % 2) == 1)105				g_aiMT[i] = g_aiMT[i] ^ 0x9908b0df;106		}107	}108109	size_t y = g_aiMT[g_iMTI];110	y = y ^ (y >> 11);111	y = y ^ ((y << 7) & (0x9d2c5680));112	y = y ^ ((y << 15) & (0xefc60000));113	y = y ^ (y >> 18);114115	g_iMTI = (g_iMTI + 1) % MT_SIZE;116117	return y;118}119120121inline float mtfrand()122{123	const int ninenine = 999999;124	return (float)(mtrand() % ninenine)/ninenine;125}126127static void resizer(int argc, char **argv)128{129	unsigned char* input_pixels;130	unsigned char* output_pixels;131	int w, h;132	int n;133	int out_w, out_h;134	input_pixels = stbi_load(argv[1], &w, &h, &n, 0);135	out_w = w*3;136	out_h = h*3;137	output_pixels = (unsigned char*) malloc(out_w*out_h*n);138	//stbir_resize_uint8_srgb(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, n, -1,0);139	stbir_resize_uint8(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, n);140	stbi_write_png("output.png", out_w, out_h, n, output_pixels, 0);141	exit(0);142}143144static void performance(int argc, char **argv)145{146	unsigned char* input_pixels;147	unsigned char* output_pixels;148	int w, h, count;149	int n, i;150	int out_w, out_h, srgb=1;151	input_pixels = stbi_load(argv[1], &w, &h, &n, 0);152    #if 0153    out_w = w/4; out_h = h/4; count=100; // 1154    #elif 0155	out_w = w*2; out_h = h/4; count=20; // 2   // note this is structured pessimily, would be much faster to downsample vertically first156    #elif 0157    out_w = w/4; out_h = h*2; count=50; // 3158    #elif 0159    out_w = w*3; out_h = h*3; count=2; srgb=0; // 4160    #else161    out_w = w*3; out_h = h*3; count=2; // 5   // this is dominated by linear->sRGB conversion162    #endif163164	output_pixels = (unsigned char*) malloc(out_w*out_h*n);165    for (i=0; i < count; ++i)166        if (srgb)167	        stbir_resize_uint8_srgb(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, n,-1,0);168        else169	        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);170	exit(0);171}172173void test_suite(int argc, char **argv);174175int main(int argc, char** argv)176{177	//resizer(argc, argv);178    //performance(argc, argv);179180	test_suite(argc, argv);181	return 0;182}183184void resize_image(const char* filename, float width_percent, float height_percent, stbir_filter filter, stbir_edge edge, stbir_colorspace colorspace, const char* output_filename)185{186	int w, h, n;187188	unsigned char* input_data = stbi_load(filename, &w, &h, &n, 0);189	if (!input_data)190	{191		printf("Input image could not be loaded\n");192		return;193	}194195	int out_w = (int)(w * width_percent);196	int out_h = (int)(h * height_percent);197198	unsigned char* output_data = (unsigned char*)malloc(out_w * out_h * n);199200	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);201202	stbi_image_free(input_data);203204	stbi_write_png(output_filename, out_w, out_h, n, output_data, 0);205206	free(output_data);207}208209template <typename F, typename T>210void convert_image(const F* input, T* output, int length)211{212	double f = (pow(2.0, 8.0 * sizeof(T)) - 1) / (pow(2.0, 8.0 * sizeof(F)) - 1);213	for (int i = 0; i < length; i++)214		output[i] = (T)(((double)input[i]) * f);215}216217template <typename T>218void test_format(const char* file, float width_percent, float height_percent, stbir_datatype type, stbir_colorspace colorspace)219{220	int w, h, n;221	unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);222223	if (input_data == NULL)224		return;225226227	int new_w = (int)(w * width_percent);228	int new_h = (int)(h * height_percent);229230	T* T_data = (T*)malloc(w * h * n * sizeof(T));231    memset(T_data, 0, w*h*n*sizeof(T));232	convert_image<unsigned char, T>(input_data, T_data, w * h * n);233234	T* output_data = (T*)malloc(new_w * new_h * n * sizeof(T));235236	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);237238	free(T_data);239	stbi_image_free(input_data);240241	unsigned char* char_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(char));242	convert_image<T, unsigned char>(output_data, char_data, new_w * new_h * n);243244	char output[200];245	sprintf(output, "test-output/type-%d-%d-%d-%d-%s", type, colorspace, new_w, new_h, file);246	stbi_write_png(output, new_w, new_h, n, char_data, 0);247248	free(char_data);249	free(output_data);250}251252void convert_image_float(const unsigned char* input, float* output, int length)253{254	for (int i = 0; i < length; i++)255		output[i] = ((float)input[i])/255;256}257258void convert_image_float(const float* input, unsigned char* output, int length)259{260	for (int i = 0; i < length; i++)261		output[i] = (unsigned char)(stbir__saturate(input[i]) * 255);262}263264void test_float(const char* file, float width_percent, float height_percent, stbir_datatype type, stbir_colorspace colorspace)265{266	int w, h, n;267	unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);268269	if (input_data == NULL)270		return;271272	int new_w = (int)(w * width_percent);273	int new_h = (int)(h * height_percent);274275	float* T_data = (float*)malloc(w * h * n * sizeof(float));276	convert_image_float(input_data, T_data, w * h * n);277278	float* output_data = (float*)malloc(new_w * new_h * n * sizeof(float));279280	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);281282	free(T_data);283	stbi_image_free(input_data);284285	unsigned char* char_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(char));286	convert_image_float(output_data, char_data, new_w * new_h * n);287288	char output[200];289	sprintf(output, "test-output/type-%d-%d-%d-%d-%s", type, colorspace, new_w, new_h, file);290	stbi_write_png(output, new_w, new_h, n, char_data, 0);291292	free(char_data);293	free(output_data);294}295296void test_channels(const char* file, float width_percent, float height_percent, int channels)297{298	int w, h, n;299	unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);300301	if (input_data == NULL)302		return;303304	int new_w = (int)(w * width_percent);305	int new_h = (int)(h * height_percent);306307	unsigned char* channels_data = (unsigned char*)malloc(w * h * channels * sizeof(unsigned char));308309	for (int i = 0; i < w * h; i++)310	{311		int input_position = i * n;312		int output_position = i * channels;313314		for (int c = 0; c < channels; c++)315			channels_data[output_position + c] = input_data[input_position + stbir__min(c, n)];316	}317318	unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * channels * sizeof(unsigned char));319320	stbir_resize_uint8_srgb(channels_data, w, h, 0, output_data, new_w, new_h, 0, channels, STBIR_ALPHA_CHANNEL_NONE, 0);321322	free(channels_data);323	stbi_image_free(input_data);324325	char output[200];326	sprintf(output, "test-output/channels-%d-%d-%d-%s", channels, new_w, new_h, file);327	stbi_write_png(output, new_w, new_h, channels, output_data, 0);328329	free(output_data);330}331332void test_subpixel(const char* file, float width_percent, float height_percent, float s1, float t1)333{334	int w, h, n;335	unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);336337	if (input_data == NULL)338		return;339340	s1 = ((float)w - 1 + s1)/w;341	t1 = ((float)h - 1 + t1)/h;342343	int new_w = (int)(w * width_percent);344	int new_h = (int)(h * height_percent);345346	unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));347348	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);349350	stbi_image_free(input_data);351352	char output[200];353	sprintf(output, "test-output/subpixel-%d-%d-%f-%f-%s", new_w, new_h, s1, t1, file);354	stbi_write_png(output, new_w, new_h, n, output_data, 0);355356	free(output_data);357}358359unsigned int* pixel(unsigned int* buffer, int x, int y, int c, int w, int n)360{361	return &buffer[y*w*n + x*n + c];362}363364void test_premul()365{366	unsigned int input[2 * 2 * 4];367	unsigned int output[1 * 1 * 4];368	unsigned int output2[2 * 2 * 4];369370	memset(input, 0, sizeof(input));371372	// First a test to make sure premul is working properly.373374	// Top left - solid red375	*pixel(input, 0, 0, 0, 2, 4) = 255;376	*pixel(input, 0, 0, 3, 2, 4) = 255;377378	// Bottom left - solid red379	*pixel(input, 0, 1, 0, 2, 4) = 255;380	*pixel(input, 0, 1, 3, 2, 4) = 255;381382	// Top right - transparent green383	*pixel(input, 1, 0, 1, 2, 4) = 255;384	*pixel(input, 1, 0, 3, 2, 4) = 25;385386	// Bottom right - transparent green387	*pixel(input, 1, 1, 1, 2, 4) = 255;388	*pixel(input, 1, 1, 3, 2, 4) = 25;389390	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);391392	float r = (float)255 / 4294967296;393	float g = (float)255 / 4294967296;394	float ra = (float)255 / 4294967296;395	float ga = (float)25 / 4294967296;396	float a = (ra + ga) / 2;397398	STBIR_ASSERT(output[0] == (unsigned int)(r * ra / 2 / a * 4294967296 + 0.5f)); // 232399	STBIR_ASSERT(output[1] == (unsigned int)(g * ga / 2 / a * 4294967296 + 0.5f)); // 23400	STBIR_ASSERT(output[2] == 0);401	STBIR_ASSERT(output[3] == (unsigned int)(a * 4294967296 + 0.5f)); // 140402403	// Now a test to make sure it doesn't clobber existing values.404405	// Top right - completely transparent green406	*pixel(input, 1, 0, 1, 2, 4) = 255;407	*pixel(input, 1, 0, 3, 2, 4) = 0;408409	// Bottom right - completely transparent green410	*pixel(input, 1, 1, 1, 2, 4) = 255;411	*pixel(input, 1, 1, 3, 2, 4) = 0;412413	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);414415	STBIR_ASSERT(*pixel(output2, 0, 0, 0, 2, 4) == 255);416	STBIR_ASSERT(*pixel(output2, 0, 0, 1, 2, 4) == 0);417	STBIR_ASSERT(*pixel(output2, 0, 0, 2, 2, 4) == 0);418	STBIR_ASSERT(*pixel(output2, 0, 0, 3, 2, 4) == 255);419420	STBIR_ASSERT(*pixel(output2, 0, 1, 0, 2, 4) == 255);421	STBIR_ASSERT(*pixel(output2, 0, 1, 1, 2, 4) == 0);422	STBIR_ASSERT(*pixel(output2, 0, 1, 2, 2, 4) == 0);423	STBIR_ASSERT(*pixel(output2, 0, 1, 3, 2, 4) == 255);424425	STBIR_ASSERT(*pixel(output2, 1, 0, 0, 2, 4) == 0);426	STBIR_ASSERT(*pixel(output2, 1, 0, 1, 2, 4) == 255);427	STBIR_ASSERT(*pixel(output2, 1, 0, 2, 2, 4) == 0);428	STBIR_ASSERT(*pixel(output2, 1, 0, 3, 2, 4) == 0);429430	STBIR_ASSERT(*pixel(output2, 1, 1, 0, 2, 4) == 0);431	STBIR_ASSERT(*pixel(output2, 1, 1, 1, 2, 4) == 255);432	STBIR_ASSERT(*pixel(output2, 1, 1, 2, 2, 4) == 0);433	STBIR_ASSERT(*pixel(output2, 1, 1, 3, 2, 4) == 0);434}435436// test that splitting a pow-2 image into tiles produces identical results437void test_subpixel_1()438{439	unsigned char image[8 * 8];440441	mtsrand(0);442443	for (int i = 0; i < sizeof(image); i++)444		image[i] = mtrand() & 255;445446	unsigned char output_data[16 * 16];447448	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);449450	unsigned char output_left[8 * 16];451	unsigned char output_right[8 * 16];452453	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);454	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);455456	for (int x = 0; x < 8; x++)457	{458		for (int y = 0; y < 16; y++)459		{460			STBIR_ASSERT(output_data[y * 16 + x] == output_left[y * 8 + x]);461			STBIR_ASSERT(output_data[y * 16 + x + 8] == output_right[y * 8 + x]);462		}463	}464}465466// test that replicating an image and using a subtile of it produces same results as wraparound467void test_subpixel_2()468{469	unsigned char image[8 * 8];470471	mtsrand(0);472473	for (int i = 0; i < sizeof(image); i++)474		image[i] = mtrand() & 255;475476	unsigned char large_image[32 * 32];477478	for (int x = 0; x < 8; x++)479	{480		for (int y = 0; y < 8; y++)481		{482			for (int i = 0; i < 4; i++)483			{484				for (int j = 0; j < 4; j++)485					large_image[j*4*8*8 + i*8 + y*4*8 + x] = image[y*8 + x];486			}487		}488	}489490	unsigned char output_data_1[16 * 16];491	unsigned char output_data_2[16 * 16];492493	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);494	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);495496	{for (int x = 0; x < 16; x++)497	{498		for (int y = 0; y < 16; y++)499			STBIR_ASSERT(output_data_1[y * 16 + x] == output_data_2[y * 16 + x]);500	}}501}502503// test that 0,0,1,1 subpixel produces same result as no-rect504void test_subpixel_3()505{506	unsigned char image[8 * 8];507508	mtsrand(0);509510	for (int i = 0; i < sizeof(image); i++)511		image[i] = mtrand() & 255;512513	unsigned char output_data_1[32 * 32];514	unsigned char output_data_2[32 * 32];515516	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);517	stbir_resize_uint8(image, 8, 8, 0, output_data_2, 32, 32, 0, 1);518519	for (int x = 0; x < 32; x++)520	{521		for (int y = 0; y < 32; y++)522			STBIR_ASSERT(output_data_1[y * 32 + x] == output_data_2[y * 32 + x]);523	}524}525526// test that 1:1 resample using s,t=0,0,1,1 with bilinear produces original image527void test_subpixel_4()528{529	unsigned char image[8 * 8];530531	mtsrand(0);532533	for (int i = 0; i < sizeof(image); i++)534		image[i] = mtrand() & 255;535536	unsigned char output[8 * 8];537538	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);539	STBIR_ASSERT(memcmp(image, output, 8 * 8) == 0);540}541542static unsigned int  image88_int[8][8];543static unsigned char image88 [8][8];544static unsigned char output88[8][8];545static unsigned char output44[4][4];546static unsigned char output22[2][2];547static unsigned char output11[1][1];548549void resample_88(stbir_filter filter)550{551	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);552	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);553	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);554	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);555}556557void verify_box(void)558{559	int i,j,t;560561	resample_88(STBIR_FILTER_BOX);562563	for (i=0; i < sizeof(image88); ++i)564		STBIR_ASSERT(image88[0][i] == output88[0][i]);565566	t = 0;567	for (j=0; j < 4; ++j)568		for (i=0; i < 4; ++i) {569			int n = image88[j*2+0][i*2+0]570			      + image88[j*2+0][i*2+1]571				  + image88[j*2+1][i*2+0]572				  + image88[j*2+1][i*2+1];573			STBIR_ASSERT(output44[j][i] == ((n+2)>>2) || output44[j][i] == ((n+1)>>2)); // can't guarantee exact rounding due to numerical precision574			t += n;575		}576	STBIR_ASSERT(output11[0][0] == ((t+32)>>6) || output11[0][0] == ((t+31)>>6)); // can't guarantee exact rounding due to numerical precision577}578579void verify_filter_normalized(stbir_filter filter, int output_size, unsigned int value)580{581	int i, j;582	unsigned int output[64];583584	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);585586	for (j = 0; j < output_size; ++j)587		for (i = 0; i < output_size; ++i)588			STBIR_ASSERT(value == output[j*output_size + i]);589}590591float round2(float f)592{593	return (float) floor(f+0.5f); // round() isn't C standard pre-C99594}595596void test_filters(void)597{598	int i,j;599600	mtsrand(0);601602	for (i=0; i < sizeof(image88); ++i)603		image88[0][i] = mtrand() & 255;604	verify_box();605606	for (i=0; i < sizeof(image88); ++i)607		image88[0][i] = 0;608	image88[4][4] = 255;609	verify_box();610611	for (j=0; j < 8; ++j)612		for (i=0; i < 8; ++i)613			image88[j][i] = (j^i)&1 ? 255 : 0;614	verify_box();615616	for (j=0; j < 8; ++j)617		for (i=0; i < 8; ++i)618			image88[j][i] = i&2 ? 255 : 0;619	verify_box();620621	int value = 64;622623	for (j = 0; j < 8; ++j)624		for (i = 0; i < 8; ++i)625			image88_int[j][i] = value;626627	verify_filter_normalized(STBIR_FILTER_BOX, 8, value);628	verify_filter_normalized(STBIR_FILTER_TRIANGLE, 8, value);629	verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 8, value);630	verify_filter_normalized(STBIR_FILTER_CATMULLROM, 8, value);631	verify_filter_normalized(STBIR_FILTER_MITCHELL, 8, value);632633	verify_filter_normalized(STBIR_FILTER_BOX, 4, value);634	verify_filter_normalized(STBIR_FILTER_TRIANGLE, 4, value);635	verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 4, value);636	verify_filter_normalized(STBIR_FILTER_CATMULLROM, 4, value);637	verify_filter_normalized(STBIR_FILTER_MITCHELL, 4, value);638639	verify_filter_normalized(STBIR_FILTER_BOX, 2, value);640	verify_filter_normalized(STBIR_FILTER_TRIANGLE, 2, value);641	verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 2, value);642	verify_filter_normalized(STBIR_FILTER_CATMULLROM, 2, value);643	verify_filter_normalized(STBIR_FILTER_MITCHELL, 2, value);644645	verify_filter_normalized(STBIR_FILTER_BOX, 1, value);646	verify_filter_normalized(STBIR_FILTER_TRIANGLE, 1, value);647	verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 1, value);648	verify_filter_normalized(STBIR_FILTER_CATMULLROM, 1, value);649	verify_filter_normalized(STBIR_FILTER_MITCHELL, 1, value);650651	{652		// This test is designed to produce coefficients that are very badly denormalized.653		unsigned int v = 556;654655		unsigned int input[100 * 100];656		unsigned int output[11 * 11];657658		for (j = 0; j < 100 * 100; ++j)659			input[j] = v;660661		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);662663		for (j = 0; j < 11 * 11; ++j)664			STBIR_ASSERT(v == output[j]);665	}666667	{668		// Now test the trapezoid filter for downsampling.669		unsigned int input[3 * 1];670		unsigned int output[2 * 1];671672		input[0] = 0;673		input[1] = 255;674		input[2] = 127;675676		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);677678		STBIR_ASSERT(output[0] == (unsigned int)round2((float)(input[0] * 2 + input[1]) / 3));679		STBIR_ASSERT(output[1] == (unsigned int)round2((float)(input[2] * 2 + input[1]) / 3));680681		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);682683		STBIR_ASSERT(output[0] == (unsigned int)round2((float)(input[0] * 2 + input[1]) / 3));684		STBIR_ASSERT(output[1] == (unsigned int)round2((float)(input[2] * 2 + input[1]) / 3));685	}686687	{688		// Now test the trapezoid filter for upsampling.689		unsigned int input[2 * 1];690		unsigned int output[3 * 1];691692		input[0] = 0;693		input[1] = 255;694695		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);696697		STBIR_ASSERT(output[0] == input[0]);698		STBIR_ASSERT(output[1] == (input[0] + input[1]) / 2);699		STBIR_ASSERT(output[2] == input[1]);700701		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);702703		STBIR_ASSERT(output[0] == input[0]);704		STBIR_ASSERT(output[1] == (input[0] + input[1]) / 2);705		STBIR_ASSERT(output[2] == input[1]);706	}707708	// checkerboard709	{710		unsigned char input[64][64];711		unsigned char output[16][16];712		int i,j;713		for (j=0; j < 64; ++j)714			for (i=0; i < 64; ++i)715				input[j][i] = (i^j)&1 ? 255 : 0;716		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);717		for (j=0; j < 16; ++j)718			for (i=0; i < 16; ++i)719				STBIR_ASSERT(output[j][i] == 128);720		stbir_resize_uint8_srgb_edgemode(input[0], 64, 64, 0, output[0],16,16,0, 1,-1,0,STBIR_EDGE_WRAP);721		for (j=0; j < 16; ++j)722			for (i=0; i < 16; ++i)723				STBIR_ASSERT(output[j][i] == 188);724725726	}727728	{729		// Test trapezoid box filter730		unsigned char input[2 * 1];731		unsigned char output[127 * 1];732733		input[0] = 0;734		input[1] = 255;735736		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);737		STBIR_ASSERT(output[0] == 0);738		STBIR_ASSERT(output[127 / 2 - 1] == 0);739		STBIR_ASSERT(output[127 / 2] == 128);740		STBIR_ASSERT(output[127 / 2 + 1] == 255);741		STBIR_ASSERT(output[126] == 255);742		stbi_write_png("test-output/trapezoid-upsample-horizontal.png", 127, 1, 1, output, 0);743744		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);745		STBIR_ASSERT(output[0] == 0);746		STBIR_ASSERT(output[127 / 2 - 1] == 0);747		STBIR_ASSERT(output[127 / 2] == 128);748		STBIR_ASSERT(output[127 / 2 + 1] == 255);749		STBIR_ASSERT(output[126] == 255);750		stbi_write_png("test-output/trapezoid-upsample-vertical.png", 1, 127, 1, output, 0);751	}752}753754#define UMAX32   4294967295U755756static void write32(char *filename, stbir_uint32 *output, int w, int h)757{758    stbir_uint8 *data = (stbir_uint8*) malloc(w*h*3);759    for (int i=0; i < w*h*3; ++i)760        data[i] = output[i]>>24;761    stbi_write_png(filename, w, h, 3, data, 0);762    free(data);763}764765static void test_32(void)766{767    int w=100,h=120,x,y, out_w,out_h;768    stbir_uint32 *input  = (stbir_uint32*) malloc(4 * 3 * w * h);769    stbir_uint32 *output = (stbir_uint32*) malloc(4 * 3 * 3*w * 3*h);770    for (y=0; y < h; ++y) {771        for (x=0; x < w; ++x) {772            input[y*3*w + x*3 + 0] = x * ( UMAX32/w );773            input[y*3*w + x*3 + 1] = y * ( UMAX32/h );774            input[y*3*w + x*3 + 2] = UMAX32/2;775        }776    }777    out_w = w*33/16;778    out_h = h*33/16;779    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);780    write32("test-output/seantest_1.png", output,out_w,out_h);781782    out_w = w*16/33;783    out_h = h*16/33;784    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);785    write32("test-output/seantest_2.png", output,out_w,out_h);786}787788789void test_suite(int argc, char **argv)790{791	int i;792	char *barbara;793794	_mkdir("test-output");795796	if (argc > 1)797		barbara = argv[1];798	else799		barbara = "barbara.png";800801	// check what cases we need normalization for802#if 1803	{804		float x, y;805		for (x = -1; x < 1; x += 0.05f) {806			float sums[5] = { 0 };807			float o;808			for (o = -5; o <= 5; ++o) {809				sums[0] += stbir__filter_mitchell(x + o, 1);810				sums[1] += stbir__filter_catmullrom(x + o, 1);811				sums[2] += stbir__filter_cubic(x + o, 1);812				sums[3] += stbir__filter_triangle(x + o, 1);813				sums[4] += stbir__filter_trapezoid(x + o, 0.5f);814			}815			for (i = 0; i < 5; ++i)816				STBIR_ASSERT(sums[i] >= 1.0 - 0.001 && sums[i] <= 1.0 + 0.001);817		}818819#if 1	820		for (y = 0.11f; y < 1; y += 0.01f) {  // Step821			for (x = -1; x < 1; x += 0.05f) { // Phase822				float sums[5] = { 0 };823				float o;824				for (o = -5; o <= 5; o += y) {825					sums[0] += y * stbir__filter_mitchell(x + o, 1);826					sums[1] += y * stbir__filter_catmullrom(x + o, 1);827					sums[2] += y * stbir__filter_cubic(x + o, 1);828					sums[4] += y * stbir__filter_trapezoid(x + o, 0.5f);829					sums[3] += y * stbir__filter_triangle(x + o, 1);830				}831				for (i = 0; i < 3; ++i)832					STBIR_ASSERT(sums[i] >= 1.0 - 0.0170 && sums[i] <= 1.0 + 0.0170);833			}834		}835#endif836	}837#endif838839#if 0 // linear_to_srgb_uchar table840	for (i=0; i < 256; ++i) {841		float f = stbir__srgb_to_linear((i-0.5f)/255.0f);842		printf("%9d, ", (int) ((f) * (1<<28)));843		if ((i & 7) == 7)844			printf("\n");845	}846#endif847848	// old tests that hacky fix worked on - test that849	// every uint8 maps to itself850	for (i = 0; i < 256; i++) {851		float f = stbir__srgb_to_linear(float(i) / 255);852		int n = stbir__linear_to_srgb_uchar(f);853		STBIR_ASSERT(n == i);854	}855856	// new tests that hacky fix failed for - test that857	// values adjacent to uint8 round to nearest uint8858	for (i = 0; i < 256; i++) {859		for (float y = -0.42f; y <= 0.42f; y += 0.01f) {860			float f = stbir__srgb_to_linear((i+y) / 255.0f);861			int n = stbir__linear_to_srgb_uchar(f);862			STBIR_ASSERT(n == i);863		}864	}865866	test_filters();867868	test_subpixel_1();869	test_subpixel_2();870	test_subpixel_3();871	test_subpixel_4();872873	test_premul();874875	test_32();876877	// Some tests to make sure errors don't pop up with strange filter/dimension combinations.878	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);879	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);880	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);881	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);882883	for (i = 0; i < 10; i++)884		test_subpixel(barbara, 0.5f, 0.5f, (float)i / 10, 1);885886	for (i = 0; i < 10; i++)887		test_subpixel(barbara, 0.5f, 0.5f, 1, (float)i / 10);888889	for (i = 0; i < 10; i++)890		test_subpixel(barbara, 2, 2, (float)i / 10, 1);891892	for (i = 0; i < 10; i++)893		test_subpixel(barbara, 2, 2, 1, (float)i / 10);894895	// Channels test896	test_channels(barbara, 0.5f, 0.5f, 1);897	test_channels(barbara, 0.5f, 0.5f, 2);898	test_channels(barbara, 0.5f, 0.5f, 3);899	test_channels(barbara, 0.5f, 0.5f, 4);900901	test_channels(barbara, 2, 2, 1);902	test_channels(barbara, 2, 2, 2);903	test_channels(barbara, 2, 2, 3);904	test_channels(barbara, 2, 2, 4);905906	// filter tests907	resize_image(barbara, 2, 2, STBIR_FILTER_BOX         , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-nearest.png");908	resize_image(barbara, 2, 2, STBIR_FILTER_TRIANGLE    , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-bilinear.png");909	resize_image(barbara, 2, 2, STBIR_FILTER_CUBICBSPLINE, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-bicubic.png");910	resize_image(barbara, 2, 2, STBIR_FILTER_CATMULLROM  , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-catmullrom.png");911	resize_image(barbara, 2, 2, STBIR_FILTER_MITCHELL    , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-mitchell.png");912913	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_BOX         , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-nearest.png");914	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_TRIANGLE    , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-bilinear.png");915	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_CUBICBSPLINE, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-bicubic.png");916	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_CATMULLROM  , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-catmullrom.png");917	resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_MITCHELL    , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-mitchell.png");918919	for (i = 10; i < 100; i++)920	{921		char outname[200];922		sprintf(outname, "test-output/barbara-width-%d.jpg", i);923		resize_image(barbara, (float)i / 100, 1, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);924	}925926	for (i = 110; i < 500; i += 10)927	{928		char outname[200];929		sprintf(outname, "test-output/barbara-width-%d.jpg", i);930		resize_image(barbara, (float)i / 100, 1, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);931	}932933	for (i = 10; i < 100; i++)934	{935		char outname[200];936		sprintf(outname, "test-output/barbara-height-%d.jpg", i);937		resize_image(barbara, 1, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);938	}939940	for (i = 110; i < 500; i += 10)941	{942		char outname[200];943		sprintf(outname, "test-output/barbara-height-%d.jpg", i);944		resize_image(barbara, 1, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);945	}946947	for (i = 50; i < 200; i += 10)948	{949		char outname[200];950		sprintf(outname, "test-output/barbara-width-height-%d.jpg", i);951		resize_image(barbara, 100 / (float)i, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);952	}953954	test_format<unsigned short>(barbara, 0.5, 2.0, STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB);955	test_format<unsigned short>(barbara, 0.5, 2.0, STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR);956	test_format<unsigned short>(barbara, 2.0, 0.5, STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB);957	test_format<unsigned short>(barbara, 2.0, 0.5, STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR);958959	test_format<unsigned int>(barbara, 0.5, 2.0, STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB);960	test_format<unsigned int>(barbara, 0.5, 2.0, STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR);961	test_format<unsigned int>(barbara, 2.0, 0.5, STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB);962	test_format<unsigned int>(barbara, 2.0, 0.5, STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR);963964	test_float(barbara, 0.5, 2.0, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB);965	test_float(barbara, 0.5, 2.0, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR);966	test_float(barbara, 2.0, 0.5, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB);967	test_float(barbara, 2.0, 0.5, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR);968969	// Edge behavior tests970	resize_image("hgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR, "test-output/hgradient-clamp.png");971	resize_image("hgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_WRAP, STBIR_COLORSPACE_LINEAR, "test-output/hgradient-wrap.png");972973	resize_image("vgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR, "test-output/vgradient-clamp.png");974	resize_image("vgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_WRAP, STBIR_COLORSPACE_LINEAR, "test-output/vgradient-wrap.png");975976	resize_image("1px-border.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_LINEAR, "test-output/1px-border-reflect.png");977	resize_image("1px-border.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR, "test-output/1px-border-clamp.png");978979	// sRGB tests980	resize_image("gamma_colors.jpg", .5f, .5f, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_SRGB, "test-output/gamma_colors.jpg");981	resize_image("gamma_2.2.jpg", .5f, .5f, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_SRGB, "test-output/gamma_2.2.jpg");982	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");983}