resample_test.cpp
This file
Path
tests/resample_test.cpp
1,122 lines · 38.4 KiB · C++
Last changed by
Merge branch 'master' into workingSean Barrettbefore adoptiond940053
History of this fileTake 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_resize.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(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}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, n,-1,0);175 else176 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}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}190191void 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;194195 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 }201202 int out_w = (int)(w * width_percent);203 int out_h = (int)(h * height_percent);204205 unsigned char* output_data = (unsigned char*)malloc(out_w * out_h * n);206207 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);208209 stbi_image_free(input_data);210211 stbi_write_png(output_filename, out_w, out_h, n, output_data, 0);212213 free(output_data);214}215216template <typename F, typename T>217void 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}223224template <typename T>225void 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);229230 if (input_data == NULL)231 return;232233234 int new_w = (int)(w * width_percent);235 int new_h = (int)(h * height_percent);236237 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);240241 T* output_data = (T*)malloc(new_w * new_h * n * sizeof(T));242243 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);244245 free(T_data);246 stbi_image_free(input_data);247248 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);250251 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);254255 free(char_data);256 free(output_data);257}258259void 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}264265void 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}270271void 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);275276 if (input_data == NULL)277 return;278279 int new_w = (int)(w * width_percent);280 int new_h = (int)(h * height_percent);281282 float* T_data = (float*)malloc(w * h * n * sizeof(float));283 convert_image_float(input_data, T_data, w * h * n);284285 float* output_data = (float*)malloc(new_w * new_h * n * sizeof(float));286287 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);288289 free(T_data);290 stbi_image_free(input_data);291292 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);294295 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);298299 free(char_data);300 free(output_data);301}302303void 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);307308 if (input_data == NULL)309 return;310311 int new_w = (int)(w * width_percent);312 int new_h = (int)(h * height_percent);313314 unsigned char* channels_data = (unsigned char*)malloc(w * h * channels * sizeof(unsigned char));315316 for (int i = 0; i < w * h; i++)317 {318 int input_position = i * n;319 int output_position = i * channels;320321 for (int c = 0; c < channels; c++)322 channels_data[output_position + c] = input_data[input_position + stbir__min(c, n)];323 }324325 unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * channels * sizeof(unsigned char));326327 stbir_resize_uint8_srgb(channels_data, w, h, 0, output_data, new_w, new_h, 0, channels, STBIR_ALPHA_CHANNEL_NONE, 0);328329 free(channels_data);330 stbi_image_free(input_data);331332 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);335336 free(output_data);337}338339void 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);343344 if (input_data == NULL)345 return;346347 s1 = ((float)w - 1 + s1)/w;348 t1 = ((float)h - 1 + t1)/h;349350 int new_w = (int)(w * width_percent);351 int new_h = (int)(h * height_percent);352353 unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));354355 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);356357 stbi_image_free(input_data);358359 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);362363 free(output_data);364}365366void 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);370371 if (input_data == NULL)372 return;373374 int new_w = (int)(w * width_percent);375 int new_h = (int)(h * height_percent);376377 unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));378379 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);380381 stbi_image_free(input_data);382383 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);386387 free(output_data);388}389390void 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);394395 if (input_data == NULL)396 return;397398 int new_w = (int)(w * width_percent);399 int new_h = (int)(h * height_percent);400401 unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));402403 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);404405 stbi_image_free(input_data);406407 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);410411 free(output_data);412}413414unsigned 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}418419void test_premul()420{421 unsigned int input[2 * 2 * 4];422 unsigned int output[1 * 1 * 4];423 unsigned int output2[2 * 2 * 4];424425 memset(input, 0, sizeof(input));426427 // First a test to make sure premul is working properly.428429 // Top left - solid red430 *pixel(input, 0, 0, 0, 2, 4) = 255;431 *pixel(input, 0, 0, 3, 2, 4) = 255;432433 // Bottom left - solid red434 *pixel(input, 0, 1, 0, 2, 4) = 255;435 *pixel(input, 0, 1, 3, 2, 4) = 255;436437 // Top right - transparent green438 *pixel(input, 1, 0, 1, 2, 4) = 255;439 *pixel(input, 1, 0, 3, 2, 4) = 25;440441 // Bottom right - transparent green442 *pixel(input, 1, 1, 1, 2, 4) = 255;443 *pixel(input, 1, 1, 3, 2, 4) = 25;444445 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);446447 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;452453 STBIR_ASSERT(output[0] == (unsigned int)(r * ra / 2 / a * 4294967296 + 0.5f)); // 232454 STBIR_ASSERT(output[1] == (unsigned int)(g * ga / 2 / a * 4294967296 + 0.5f)); // 23455 STBIR_ASSERT(output[2] == 0);456 STBIR_ASSERT(output[3] == (unsigned int)(a * 4294967296 + 0.5f)); // 140457458 // Now a test to make sure it doesn't clobber existing values.459460 // Top right - completely transparent green461 *pixel(input, 1, 0, 1, 2, 4) = 255;462 *pixel(input, 1, 0, 3, 2, 4) = 0;463464 // Bottom right - completely transparent green465 *pixel(input, 1, 1, 1, 2, 4) = 255;466 *pixel(input, 1, 1, 3, 2, 4) = 0;467468 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);469470 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);474475 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);479480 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);484485 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}490491// test that splitting a pow-2 image into tiles produces identical results492void test_subpixel_1()493{494 unsigned char image[8 * 8];495496 mtsrand(0);497498 for (int i = 0; i < sizeof(image); i++)499 image[i] = mtrand() & 255;500501 unsigned char output_data[16 * 16];502503 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);504505 unsigned char output_left[8 * 16];506 unsigned char output_right[8 * 16];507508 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);510511 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 }519520 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);522523 {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}532533// test that replicating an image and using a subtile of it produces same results as wraparound534void test_subpixel_2()535{536 unsigned char image[8 * 8];537538 mtsrand(0);539540 for (int i = 0; i < sizeof(image); i++)541 image[i] = mtrand() & 255;542543 unsigned char large_image[32 * 32];544545 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 }556557 unsigned char output_data_1[16 * 16];558 unsigned char output_data_2[16 * 16];559560 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);562563 {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 }}568569 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);570571 {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}577578// test that 0,0,1,1 subpixel produces same result as no-rect579void test_subpixel_3()580{581 unsigned char image[8 * 8];582583 mtsrand(0);584585 for (int i = 0; i < sizeof(image); i++)586 image[i] = mtrand() & 255;587588 unsigned char output_data_1[32 * 32];589 unsigned char output_data_2[32 * 32];590591 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);593594 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 }599600 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);601602 {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}608609// test that 1:1 resample using s,t=0,0,1,1 with bilinear produces original image610void test_subpixel_4()611{612 unsigned char image[8 * 8];613614 mtsrand(0);615616 for (int i = 0; i < sizeof(image); i++)617 image[i] = mtrand() & 255;618619 unsigned char output[8 * 8];620621 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);623624 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}627628static unsigned int image88_int[8][8];629static unsigned char image88 [8][8];630static unsigned char output88[8][8];631static unsigned char output44[4][4];632static unsigned char output22[2][2];633static unsigned char output11[1][1];634635void 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}642643void verify_box(void)644{645 int i,j,t;646647 resample_88(STBIR_FILTER_BOX);648649 for (i=0; i < sizeof(image88); ++i)650 STBIR_ASSERT(image88[0][i] == output88[0][i]);651652 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 precision660 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 precision663}664665void verify_filter_normalized(stbir_filter filter, int output_size, unsigned int value)666{667 int i, j;668 unsigned int output[64];669670 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);671672 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}676677float round2(float f)678{679 return (float) floor(f+0.5f); // round() isn't C standard pre-C99680}681682void test_filters(void)683{684 int i,j;685686 mtsrand(0);687688 for (i=0; i < sizeof(image88); ++i)689 image88[0][i] = mtrand() & 255;690 verify_box();691692 for (i=0; i < sizeof(image88); ++i)693 image88[0][i] = 0;694 image88[4][4] = 255;695 verify_box();696697 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();701702 for (j=0; j < 8; ++j)703 for (i=0; i < 8; ++i)704 image88[j][i] = i&2 ? 255 : 0;705 verify_box();706707 int value = 64;708709 for (j = 0; j < 8; ++j)710 for (i = 0; i < 8; ++i)711 image88_int[j][i] = value;712713 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);718719 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);724725 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);730731 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);736737 {738 // This test is designed to produce coefficients that are very badly denormalized.739 unsigned int v = 556;740741 unsigned int input[100 * 100];742 unsigned int output[11 * 11];743744 for (j = 0; j < 100 * 100; ++j)745 input[j] = v;746747 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);748749 for (j = 0; j < 11 * 11; ++j)750 STBIR_ASSERT(v == output[j]);751 }752753 {754 // Now test the trapezoid filter for downsampling.755 unsigned int input[3 * 1];756 unsigned int output[2 * 1];757758 input[0] = 0;759 input[1] = 255;760 input[2] = 127;761762 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);763764 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));766767 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);768769 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 }772773 {774 // Now test the trapezoid filter for upsampling.775 unsigned int input[2 * 1];776 unsigned int output[3 * 1];777778 input[0] = 0;779 input[1] = 255;780781 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);782783 STBIR_ASSERT(output[0] == input[0]);784 STBIR_ASSERT(output[1] == (input[0] + input[1]) / 2);785 STBIR_ASSERT(output[2] == input[1]);786787 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);788789 STBIR_ASSERT(output[0] == input[0]);790 STBIR_ASSERT(output[1] == (input[0] + input[1]) / 2);791 STBIR_ASSERT(output[2] == input[1]);792 }793794 // checkerboard795 {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);810811812 }813814 {815 // Test trapezoid box filter816 unsigned char input[2 * 1];817 unsigned char output[127 * 1];818819 input[0] = 0;820 input[1] = 255;821822 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);829830 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}839840#define UMAX32 4294967295U841842static 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}850851static 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);867868 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}873874875void test_suite(int argc, char **argv)876{877 int i;878 const char *barbara;879880 mkdir("test-output", 777);881882 if (argc > 1)883 barbara = argv[1];884 else885 barbara = "barbara.png";886887 // check what cases we need normalization for888#if 1889 {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 }904905#if 1 906 for (y = 0.11f; y < 1; y += 0.01f) { // Step907 for (x = -1; x < 1; x += 0.05f) { // Phase908 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#endif922 }923#endif924925#if 0 // linear_to_srgb_uchar table926 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#endif933934 // old tests that hacky fix worked on - test that935 // every uint8 maps to itself936 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 }941942 // new tests that hacky fix failed for - test that943 // values adjacent to uint8 round to nearest uint8944 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 }951952 test_filters();953954 test_subpixel_1();955 test_subpixel_2();956 test_subpixel_3();957 test_subpixel_4();958959 test_premul();960961 test_32();962963 // 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);968969 int barbara_width, barbara_height, barbara_channels;970 stbi_image_free(stbi_load(barbara, &barbara_width, &barbara_height, &barbara_channels, 0));971972 int res = 10;973 // Downscaling974 {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;981982 test_subpixel_command(barbara, scale, scale, out_scale, out_scale, x_shift, y_shift);983 }}984985 // Upscaling986 {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;993994 test_subpixel_command(barbara, scale, scale, out_scale, out_scale, x_shift, y_shift);995 }}996997 // Downscaling998 {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 }}10031004 // No scaling1005 {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 }}10101011 // Upscaling1012 {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 }}10171018 {for (i = 0; i < 10; i++)1019 test_subpixel(barbara, 0.5f, 0.5f, (float)i / 10, 1);1020 }10211022 {for (i = 0; i < 10; i++)1023 test_subpixel(barbara, 0.5f, 0.5f, 1, (float)i / 10);1024 }10251026 {for (i = 0; i < 10; i++)1027 test_subpixel(barbara, 2, 2, (float)i / 10, 1);1028 }10291030 {for (i = 0; i < 10; i++)1031 test_subpixel(barbara, 2, 2, 1, (float)i / 10);1032 }10331034 // Channels test1035 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);10391040 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);10441045 // filter tests1046 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");10511052 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");10571058 {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 }}10641065 {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 }}10711072 {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 }}10781079 {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 }}10851086 {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 }}10921093 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);10971098 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);11021103 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);11071108 // Edge behavior tests1109 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");11111112 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");11141115 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");11171118 // sRGB tests1119 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}