Skip to content
Myr

resample_test.cpp

This file

Path

tests/resample_test.cpp

1,118 lines · 38.3 KiB · C++

Last changed by

Merge branch 'stbir_warnings' of https://github.com/BSVino/stb into workingSean Barrettbefore adoptionbc58f37

History of this file

Take it

The walk stopped after 200 commits: lines older than 5d9423f point at it.

fix -Wcast-qual in stb_rect_pack, stb_textedit, stb_truetypeSean Barrettbefore adoption5d9423f

lines 1–72why, one step back
1#include <stdlib.h>
2#include <stdio.h>34#if defined(_WIN32) && _MSC_VER > 12005#define STBIR_ASSERT(x) \6 if (!(x)) { \7 __debugbreak(); \8 } else9#else10#include <assert.h>11#define STBIR_ASSERT(x) assert(x)12#endif1314#define STBIR_MALLOC stbir_malloc15#define STBIR_FREE stbir_free1617class stbir_context {18public:19 stbir_context()20 {21 size = 1000000;22 memory = malloc(size);23 }2425 ~stbir_context()26 {27 free(memory);28 }2930 size_t size;31 void* memory;32} g_context;3334void* stbir_malloc(size_t size, void* context)35{36 if (!context)37 return malloc(size);3839 stbir_context* real_context = (stbir_context*)context;40 if (size > real_context->size)41 return 0;4243 return real_context->memory;44}4546void stbir_free(void* memory, void* context)47{48 if (!context)49 free(memory);50}5152//#include <stdio.h>53void stbir_progress(float p)54{55 //printf("%f\n", p);56 STBIR_ASSERT(p >= 0 && p <= 1);57}5859#define STBIR_PROGRESS_REPORT stbir_progress6061#define STB_IMAGE_RESIZE_IMPLEMENTATION62#define STB_IMAGE_RESIZE_STATIC63#include "stb_image_resize.h"6465#define STB_IMAGE_WRITE_IMPLEMENTATION66#include "stb_image_write.h"6768#define STB_IMAGE_IMPLEMENTATION69#include "stb_image.h"7071#ifdef _WIN3272#include <sys/timeb.h>

Merge branch 'stbir_warnings' of https://github.com/BSVino/stb into workingSean Barrettbefore adoptionbc58f37

lines 73–76why, one step back
73#include <direct.h>
74#define mkdir(a, b) _mkdir(a)75#else76#include <sys/stat.h>

fix -Wcast-qual in stb_rect_pack, stb_textedit, stb_truetypeSean Barrettbefore adoption5d9423f

lines 77–837why, one step back
77#endif
7879#define MT_SIZE 62480static size_t g_aiMT[MT_SIZE];81static size_t g_iMTI = 0;8283// Mersenne Twister implementation from Wikipedia.84// Avoiding use of the system rand() to be sure that our tests generate the same test data on any system.85void mtsrand(size_t iSeed)86{87 g_aiMT[0] = iSeed;88 for (size_t i = 1; i < MT_SIZE; i++)89 {90 size_t inner1 = g_aiMT[i - 1];91 size_t inner2 = (g_aiMT[i - 1] >> 30);92 size_t inner = inner1 ^ inner2;93 g_aiMT[i] = (0x6c078965 * inner) + i;94 }9596 g_iMTI = 0;97}9899size_t mtrand()100{101 if (g_iMTI == 0)102 {103 for (size_t i = 0; i < MT_SIZE; i++)104 {105 size_t y = (0x80000000 & (g_aiMT[i])) + (0x7fffffff & (g_aiMT[(i + 1) % MT_SIZE]));106 g_aiMT[i] = g_aiMT[(i + 397) % MT_SIZE] ^ (y >> 1);107 if ((y % 2) == 1)108 g_aiMT[i] = g_aiMT[i] ^ 0x9908b0df;109 }110 }111112 size_t y = g_aiMT[g_iMTI];113 y = y ^ (y >> 11);114 y = y ^ ((y << 7) & (0x9d2c5680));115 y = y ^ ((y << 15) & (0xefc60000));116 y = y ^ (y >> 18);117118 g_iMTI = (g_iMTI + 1) % MT_SIZE;119120 return y;121}122123124inline float mtfrand()125{126 const int ninenine = 999999;127 return (float)(mtrand() % ninenine)/ninenine;128}129130static void resizer(int argc, char **argv)131{132 unsigned char* input_pixels;133 unsigned char* output_pixels;134 int w, h;135 int n;136 int out_w, out_h;137 input_pixels = stbi_load(argv[1], &w, &h, &n, 0);138 out_w = w*3;139 out_h = h*3;140 output_pixels = (unsigned char*) malloc(out_w*out_h*n);141 //stbir_resize_uint8_srgb(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, n, -1,0);142 stbir_resize_uint8(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, n);143 stbi_write_png("output.png", out_w, out_h, n, output_pixels, 0);144 exit(0);145}146147static void performance(int argc, char **argv)148{149 unsigned char* input_pixels;150 unsigned char* output_pixels;151 int w, h, count;152 int n, i;153 int out_w, out_h, srgb=1;154 input_pixels = stbi_load(argv[1], &w, &h, &n, 0);155 #if 0156 out_w = w/4; out_h = h/4; count=100; // 1157 #elif 0158 out_w = w*2; out_h = h/4; count=20; // 2 // note this is structured pessimily, would be much faster to downsample vertically first159 #elif 0160 out_w = w/4; out_h = h*2; count=50; // 3161 #elif 0162 out_w = w*3; out_h = h*3; count=2; srgb=0; // 4163 #else164 out_w = w*3; out_h = h*3; count=2; // 5 // this is dominated by linear->sRGB conversion165 #endif166167 output_pixels = (unsigned char*) malloc(out_w*out_h*n);168 for (i=0; i < count; ++i)169 if (srgb)170 stbir_resize_uint8_srgb(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, n,-1,0);171 else172 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);173 exit(0);174}175176void test_suite(int argc, char **argv);177178int main(int argc, char** argv)179{180 //resizer(argc, argv);181 //performance(argc, argv);182183 test_suite(argc, argv);184 return 0;185}186187void resize_image(const char* filename, float width_percent, float height_percent, stbir_filter filter, stbir_edge edge, stbir_colorspace colorspace, const char* output_filename)188{189 int w, h, n;190191 unsigned char* input_data = stbi_load(filename, &w, &h, &n, 0);192 if (!input_data)193 {194 printf("Input image could not be loaded\n");195 return;196 }197198 int out_w = (int)(w * width_percent);199 int out_h = (int)(h * height_percent);200201 unsigned char* output_data = (unsigned char*)malloc(out_w * out_h * n);202203 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);204205 stbi_image_free(input_data);206207 stbi_write_png(output_filename, out_w, out_h, n, output_data, 0);208209 free(output_data);210}211212template <typename F, typename T>213void convert_image(const F* input, T* output, int length)214{215 double f = (pow(2.0, 8.0 * sizeof(T)) - 1) / (pow(2.0, 8.0 * sizeof(F)) - 1);216 for (int i = 0; i < length; i++)217 output[i] = (T)(((double)input[i]) * f);218}219220template <typename T>221void test_format(const char* file, float width_percent, float height_percent, stbir_datatype type, stbir_colorspace colorspace)222{223 int w, h, n;224 unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);225226 if (input_data == NULL)227 return;228229230 int new_w = (int)(w * width_percent);231 int new_h = (int)(h * height_percent);232233 T* T_data = (T*)malloc(w * h * n * sizeof(T));234 memset(T_data, 0, w*h*n*sizeof(T));235 convert_image<unsigned char, T>(input_data, T_data, w * h * n);236237 T* output_data = (T*)malloc(new_w * new_h * n * sizeof(T));238239 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);240241 free(T_data);242 stbi_image_free(input_data);243244 unsigned char* char_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(char));245 convert_image<T, unsigned char>(output_data, char_data, new_w * new_h * n);246247 char output[200];248 sprintf(output, "test-output/type-%d-%d-%d-%d-%s", type, colorspace, new_w, new_h, file);249 stbi_write_png(output, new_w, new_h, n, char_data, 0);250251 free(char_data);252 free(output_data);253}254255void convert_image_float(const unsigned char* input, float* output, int length)256{257 for (int i = 0; i < length; i++)258 output[i] = ((float)input[i])/255;259}260261void convert_image_float(const float* input, unsigned char* output, int length)262{263 for (int i = 0; i < length; i++)264 output[i] = (unsigned char)(stbir__saturate(input[i]) * 255);265}266267void test_float(const char* file, float width_percent, float height_percent, stbir_datatype type, stbir_colorspace colorspace)268{269 int w, h, n;270 unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);271272 if (input_data == NULL)273 return;274275 int new_w = (int)(w * width_percent);276 int new_h = (int)(h * height_percent);277278 float* T_data = (float*)malloc(w * h * n * sizeof(float));279 convert_image_float(input_data, T_data, w * h * n);280281 float* output_data = (float*)malloc(new_w * new_h * n * sizeof(float));282283 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);284285 free(T_data);286 stbi_image_free(input_data);287288 unsigned char* char_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(char));289 convert_image_float(output_data, char_data, new_w * new_h * n);290291 char output[200];292 sprintf(output, "test-output/type-%d-%d-%d-%d-%s", type, colorspace, new_w, new_h, file);293 stbi_write_png(output, new_w, new_h, n, char_data, 0);294295 free(char_data);296 free(output_data);297}298299void test_channels(const char* file, float width_percent, float height_percent, int channels)300{301 int w, h, n;302 unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);303304 if (input_data == NULL)305 return;306307 int new_w = (int)(w * width_percent);308 int new_h = (int)(h * height_percent);309310 unsigned char* channels_data = (unsigned char*)malloc(w * h * channels * sizeof(unsigned char));311312 for (int i = 0; i < w * h; i++)313 {314 int input_position = i * n;315 int output_position = i * channels;316317 for (int c = 0; c < channels; c++)318 channels_data[output_position + c] = input_data[input_position + stbir__min(c, n)];319 }320321 unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * channels * sizeof(unsigned char));322323 stbir_resize_uint8_srgb(channels_data, w, h, 0, output_data, new_w, new_h, 0, channels, STBIR_ALPHA_CHANNEL_NONE, 0);324325 free(channels_data);326 stbi_image_free(input_data);327328 char output[200];329 sprintf(output, "test-output/channels-%d-%d-%d-%s", channels, new_w, new_h, file);330 stbi_write_png(output, new_w, new_h, channels, output_data, 0);331332 free(output_data);333}334335void test_subpixel(const char* file, float width_percent, float height_percent, float s1, float t1)336{337 int w, h, n;338 unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);339340 if (input_data == NULL)341 return;342343 s1 = ((float)w - 1 + s1)/w;344 t1 = ((float)h - 1 + t1)/h;345346 int new_w = (int)(w * width_percent);347 int new_h = (int)(h * height_percent);348349 unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));350351 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);352353 stbi_image_free(input_data);354355 char output[200];356 sprintf(output, "test-output/subpixel-%d-%d-%f-%f-%s", new_w, new_h, s1, t1, file);357 stbi_write_png(output, new_w, new_h, n, output_data, 0);358359 free(output_data);360}361362void test_subpixel_region(const char* file, float width_percent, float height_percent, float s0, float t0, float s1, float t1)363{364 int w, h, n;365 unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);366367 if (input_data == NULL)368 return;369370 int new_w = (int)(w * width_percent);371 int new_h = (int)(h * height_percent);372373 unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));374375 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);376377 stbi_image_free(input_data);378379 char output[200];380 sprintf(output, "test-output/subpixel-region-%d-%d-%f-%f-%f-%f-%s", new_w, new_h, s0, t0, s1, t1, file);381 stbi_write_png(output, new_w, new_h, n, output_data, 0);382383 free(output_data);384}385386void test_subpixel_command(const char* file, float width_percent, float height_percent, float x_scale, float y_scale, float x_offset, float y_offset)387{388 int w, h, n;389 unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);390391 if (input_data == NULL)392 return;393394 int new_w = (int)(w * width_percent);395 int new_h = (int)(h * height_percent);396397 unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));398399 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);400401 stbi_image_free(input_data);402403 char output[200];404 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);405 stbi_write_png(output, new_w, new_h, n, output_data, 0);406407 free(output_data);408}409410unsigned int* pixel(unsigned int* buffer, int x, int y, int c, int w, int n)411{412 return &buffer[y*w*n + x*n + c];413}414415void test_premul()416{417 unsigned int input[2 * 2 * 4];418 unsigned int output[1 * 1 * 4];419 unsigned int output2[2 * 2 * 4];420421 memset(input, 0, sizeof(input));422423 // First a test to make sure premul is working properly.424425 // Top left - solid red426 *pixel(input, 0, 0, 0, 2, 4) = 255;427 *pixel(input, 0, 0, 3, 2, 4) = 255;428429 // Bottom left - solid red430 *pixel(input, 0, 1, 0, 2, 4) = 255;431 *pixel(input, 0, 1, 3, 2, 4) = 255;432433 // Top right - transparent green434 *pixel(input, 1, 0, 1, 2, 4) = 255;435 *pixel(input, 1, 0, 3, 2, 4) = 25;436437 // Bottom right - transparent green438 *pixel(input, 1, 1, 1, 2, 4) = 255;439 *pixel(input, 1, 1, 3, 2, 4) = 25;440441 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);442443 float r = (float)255 / 4294967296;444 float g = (float)255 / 4294967296;445 float ra = (float)255 / 4294967296;446 float ga = (float)25 / 4294967296;447 float a = (ra + ga) / 2;448449 STBIR_ASSERT(output[0] == (unsigned int)(r * ra / 2 / a * 4294967296 + 0.5f)); // 232450 STBIR_ASSERT(output[1] == (unsigned int)(g * ga / 2 / a * 4294967296 + 0.5f)); // 23451 STBIR_ASSERT(output[2] == 0);452 STBIR_ASSERT(output[3] == (unsigned int)(a * 4294967296 + 0.5f)); // 140453454 // Now a test to make sure it doesn't clobber existing values.455456 // Top right - completely transparent green457 *pixel(input, 1, 0, 1, 2, 4) = 255;458 *pixel(input, 1, 0, 3, 2, 4) = 0;459460 // Bottom right - completely transparent green461 *pixel(input, 1, 1, 1, 2, 4) = 255;462 *pixel(input, 1, 1, 3, 2, 4) = 0;463464 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);465466 STBIR_ASSERT(*pixel(output2, 0, 0, 0, 2, 4) == 255);467 STBIR_ASSERT(*pixel(output2, 0, 0, 1, 2, 4) == 0);468 STBIR_ASSERT(*pixel(output2, 0, 0, 2, 2, 4) == 0);469 STBIR_ASSERT(*pixel(output2, 0, 0, 3, 2, 4) == 255);470471 STBIR_ASSERT(*pixel(output2, 0, 1, 0, 2, 4) == 255);472 STBIR_ASSERT(*pixel(output2, 0, 1, 1, 2, 4) == 0);473 STBIR_ASSERT(*pixel(output2, 0, 1, 2, 2, 4) == 0);474 STBIR_ASSERT(*pixel(output2, 0, 1, 3, 2, 4) == 255);475476 STBIR_ASSERT(*pixel(output2, 1, 0, 0, 2, 4) == 0);477 STBIR_ASSERT(*pixel(output2, 1, 0, 1, 2, 4) == 255);478 STBIR_ASSERT(*pixel(output2, 1, 0, 2, 2, 4) == 0);479 STBIR_ASSERT(*pixel(output2, 1, 0, 3, 2, 4) == 0);480481 STBIR_ASSERT(*pixel(output2, 1, 1, 0, 2, 4) == 0);482 STBIR_ASSERT(*pixel(output2, 1, 1, 1, 2, 4) == 255);483 STBIR_ASSERT(*pixel(output2, 1, 1, 2, 2, 4) == 0);484 STBIR_ASSERT(*pixel(output2, 1, 1, 3, 2, 4) == 0);485}486487// test that splitting a pow-2 image into tiles produces identical results488void test_subpixel_1()489{490 unsigned char image[8 * 8];491492 mtsrand(0);493494 for (int i = 0; i < sizeof(image); i++)495 image[i] = mtrand() & 255;496497 unsigned char output_data[16 * 16];498499 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);500501 unsigned char output_left[8 * 16];502 unsigned char output_right[8 * 16];503504 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);505 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);506507 for (int x = 0; x < 8; x++)508 {509 for (int y = 0; y < 16; y++)510 {511 STBIR_ASSERT(output_data[y * 16 + x] == output_left[y * 8 + x]);512 STBIR_ASSERT(output_data[y * 16 + x + 8] == output_right[y * 8 + x]);513 }514 }515516 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);517 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);518519 {for (int x = 0; x < 8; x++)520 {521 for (int y = 0; y < 16; y++)522 {523 STBIR_ASSERT(output_data[y * 16 + x] == output_left[y * 8 + x]);524 STBIR_ASSERT(output_data[y * 16 + x + 8] == output_right[y * 8 + x]);525 }526 }}527}528529// test that replicating an image and using a subtile of it produces same results as wraparound530void test_subpixel_2()531{532 unsigned char image[8 * 8];533534 mtsrand(0);535536 for (int i = 0; i < sizeof(image); i++)537 image[i] = mtrand() & 255;538539 unsigned char large_image[32 * 32];540541 for (int x = 0; x < 8; x++)542 {543 for (int y = 0; y < 8; y++)544 {545 for (int i = 0; i < 4; i++)546 {547 for (int j = 0; j < 4; j++)548 large_image[j*4*8*8 + i*8 + y*4*8 + x] = image[y*8 + x];549 }550 }551 }552553 unsigned char output_data_1[16 * 16];554 unsigned char output_data_2[16 * 16];555556 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);557 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);558559 {for (int x = 0; x < 16; x++)560 {561 for (int y = 0; y < 16; y++)562 STBIR_ASSERT(output_data_1[y * 16 + x] == output_data_2[y * 16 + x]);563 }}564565 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);566567 {for (int x = 0; x < 16; x++)568 {569 for (int y = 0; y < 16; y++)570 STBIR_ASSERT(output_data_1[y * 16 + x] == output_data_2[y * 16 + x]);571 }}572}573574// test that 0,0,1,1 subpixel produces same result as no-rect575void test_subpixel_3()576{577 unsigned char image[8 * 8];578579 mtsrand(0);580581 for (int i = 0; i < sizeof(image); i++)582 image[i] = mtrand() & 255;583584 unsigned char output_data_1[32 * 32];585 unsigned char output_data_2[32 * 32];586587 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);588 stbir_resize_uint8(image, 8, 8, 0, output_data_2, 32, 32, 0, 1);589590 for (int x = 0; x < 32; x++)591 {592 for (int y = 0; y < 32; y++)593 STBIR_ASSERT(output_data_1[y * 32 + x] == output_data_2[y * 32 + x]);594 }595596 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);597598 {for (int x = 0; x < 32; x++)599 {600 for (int y = 0; y < 32; y++)601 STBIR_ASSERT(output_data_1[y * 32 + x] == output_data_2[y * 32 + x]);602 }}603}604605// test that 1:1 resample using s,t=0,0,1,1 with bilinear produces original image606void test_subpixel_4()607{608 unsigned char image[8 * 8];609610 mtsrand(0);611612 for (int i = 0; i < sizeof(image); i++)613 image[i] = mtrand() & 255;614615 unsigned char output[8 * 8];616617 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);618 STBIR_ASSERT(memcmp(image, output, 8 * 8) == 0);619620 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);621 STBIR_ASSERT(memcmp(image, output, 8 * 8) == 0);622}623624static unsigned int image88_int[8][8];625static unsigned char image88 [8][8];626static unsigned char output88[8][8];627static unsigned char output44[4][4];628static unsigned char output22[2][2];629static unsigned char output11[1][1];630631void resample_88(stbir_filter filter)632{633 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);634 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);635 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);636 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);637}638639void verify_box(void)640{641 int i,j,t;642643 resample_88(STBIR_FILTER_BOX);644645 for (i=0; i < sizeof(image88); ++i)646 STBIR_ASSERT(image88[0][i] == output88[0][i]);647648 t = 0;649 for (j=0; j < 4; ++j)650 for (i=0; i < 4; ++i) {651 int n = image88[j*2+0][i*2+0]652 + image88[j*2+0][i*2+1]653 + image88[j*2+1][i*2+0]654 + image88[j*2+1][i*2+1];655 STBIR_ASSERT(output44[j][i] == ((n+2)>>2) || output44[j][i] == ((n+1)>>2)); // can't guarantee exact rounding due to numerical precision656 t += n;657 }658 STBIR_ASSERT(output11[0][0] == ((t+32)>>6) || output11[0][0] == ((t+31)>>6)); // can't guarantee exact rounding due to numerical precision659}660661void verify_filter_normalized(stbir_filter filter, int output_size, unsigned int value)662{663 int i, j;664 unsigned int output[64];665666 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);667668 for (j = 0; j < output_size; ++j)669 for (i = 0; i < output_size; ++i)670 STBIR_ASSERT(value == output[j*output_size + i]);671}672673float round2(float f)674{675 return (float) floor(f+0.5f); // round() isn't C standard pre-C99676}677678void test_filters(void)679{680 int i,j;681682 mtsrand(0);683684 for (i=0; i < sizeof(image88); ++i)685 image88[0][i] = mtrand() & 255;686 verify_box();687688 for (i=0; i < sizeof(image88); ++i)689 image88[0][i] = 0;690 image88[4][4] = 255;691 verify_box();692693 for (j=0; j < 8; ++j)694 for (i=0; i < 8; ++i)695 image88[j][i] = (j^i)&1 ? 255 : 0;696 verify_box();697698 for (j=0; j < 8; ++j)699 for (i=0; i < 8; ++i)700 image88[j][i] = i&2 ? 255 : 0;701 verify_box();702703 int value = 64;704705 for (j = 0; j < 8; ++j)706 for (i = 0; i < 8; ++i)707 image88_int[j][i] = value;708709 verify_filter_normalized(STBIR_FILTER_BOX, 8, value);710 verify_filter_normalized(STBIR_FILTER_TRIANGLE, 8, value);711 verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 8, value);712 verify_filter_normalized(STBIR_FILTER_CATMULLROM, 8, value);713 verify_filter_normalized(STBIR_FILTER_MITCHELL, 8, value);714715 verify_filter_normalized(STBIR_FILTER_BOX, 4, value);716 verify_filter_normalized(STBIR_FILTER_TRIANGLE, 4, value);717 verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 4, value);718 verify_filter_normalized(STBIR_FILTER_CATMULLROM, 4, value);719 verify_filter_normalized(STBIR_FILTER_MITCHELL, 4, value);720721 verify_filter_normalized(STBIR_FILTER_BOX, 2, value);722 verify_filter_normalized(STBIR_FILTER_TRIANGLE, 2, value);723 verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 2, value);724 verify_filter_normalized(STBIR_FILTER_CATMULLROM, 2, value);725 verify_filter_normalized(STBIR_FILTER_MITCHELL, 2, value);726727 verify_filter_normalized(STBIR_FILTER_BOX, 1, value);728 verify_filter_normalized(STBIR_FILTER_TRIANGLE, 1, value);729 verify_filter_normalized(STBIR_FILTER_CUBICBSPLINE, 1, value);730 verify_filter_normalized(STBIR_FILTER_CATMULLROM, 1, value);731 verify_filter_normalized(STBIR_FILTER_MITCHELL, 1, value);732733 {734 // This test is designed to produce coefficients that are very badly denormalized.735 unsigned int v = 556;736737 unsigned int input[100 * 100];738 unsigned int output[11 * 11];739740 for (j = 0; j < 100 * 100; ++j)741 input[j] = v;742743 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);744745 for (j = 0; j < 11 * 11; ++j)746 STBIR_ASSERT(v == output[j]);747 }748749 {750 // Now test the trapezoid filter for downsampling.751 unsigned int input[3 * 1];752 unsigned int output[2 * 1];753754 input[0] = 0;755 input[1] = 255;756 input[2] = 127;757758 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);759760 STBIR_ASSERT(output[0] == (unsigned int)round2((float)(input[0] * 2 + input[1]) / 3));761 STBIR_ASSERT(output[1] == (unsigned int)round2((float)(input[2] * 2 + input[1]) / 3));762763 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);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));767 }768769 {770 // Now test the trapezoid filter for upsampling.771 unsigned int input[2 * 1];772 unsigned int output[3 * 1];773774 input[0] = 0;775 input[1] = 255;776777 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);778779 STBIR_ASSERT(output[0] == input[0]);780 STBIR_ASSERT(output[1] == (input[0] + input[1]) / 2);781 STBIR_ASSERT(output[2] == input[1]);782783 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);784785 STBIR_ASSERT(output[0] == input[0]);786 STBIR_ASSERT(output[1] == (input[0] + input[1]) / 2);787 STBIR_ASSERT(output[2] == input[1]);788 }789790 // checkerboard791 {792 unsigned char input[64][64];793 unsigned char output[16][16];794 int i,j;795 for (j=0; j < 64; ++j)796 for (i=0; i < 64; ++i)797 input[j][i] = (i^j)&1 ? 255 : 0;798 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);799 for (j=0; j < 16; ++j)800 for (i=0; i < 16; ++i)801 STBIR_ASSERT(output[j][i] == 128);802 stbir_resize_uint8_srgb_edgemode(input[0], 64, 64, 0, output[0],16,16,0, 1,-1,0,STBIR_EDGE_WRAP);803 for (j=0; j < 16; ++j)804 for (i=0; i < 16; ++i)805 STBIR_ASSERT(output[j][i] == 188);806807808 }809810 {811 // Test trapezoid box filter812 unsigned char input[2 * 1];813 unsigned char output[127 * 1];814815 input[0] = 0;816 input[1] = 255;817818 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);819 STBIR_ASSERT(output[0] == 0);820 STBIR_ASSERT(output[127 / 2 - 1] == 0);821 STBIR_ASSERT(output[127 / 2] == 128);822 STBIR_ASSERT(output[127 / 2 + 1] == 255);823 STBIR_ASSERT(output[126] == 255);824 stbi_write_png("test-output/trapezoid-upsample-horizontal.png", 127, 1, 1, output, 0);825826 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);827 STBIR_ASSERT(output[0] == 0);828 STBIR_ASSERT(output[127 / 2 - 1] == 0);829 STBIR_ASSERT(output[127 / 2] == 128);830 STBIR_ASSERT(output[127 / 2 + 1] == 255);831 STBIR_ASSERT(output[126] == 255);832 stbi_write_png("test-output/trapezoid-upsample-vertical.png", 1, 127, 1, output, 0);833 }834}835836#define UMAX32 4294967295U837

Merge branch 'stbir_warnings' of https://github.com/BSVino/stb into workingSean Barrettbefore adoptionbc58f37

line 838why, one step back
838static void write32(const char *filename, stbir_uint32 *output, int w, int h)

fix -Wcast-qual in stb_rect_pack, stb_textedit, stb_truetypeSean Barrettbefore adoption5d9423f

lines 839–873why, one step back
839{
840 stbir_uint8 *data = (stbir_uint8*) malloc(w*h*3);841 for (int i=0; i < w*h*3; ++i)842 data[i] = output[i]>>24;843 stbi_write_png(filename, w, h, 3, data, 0);844 free(data);845}846847static void test_32(void)848{849 int w=100,h=120,x,y, out_w,out_h;850 stbir_uint32 *input = (stbir_uint32*) malloc(4 * 3 * w * h);851 stbir_uint32 *output = (stbir_uint32*) malloc(4 * 3 * 3*w * 3*h);852 for (y=0; y < h; ++y) {853 for (x=0; x < w; ++x) {854 input[y*3*w + x*3 + 0] = x * ( UMAX32/w );855 input[y*3*w + x*3 + 1] = y * ( UMAX32/h );856 input[y*3*w + x*3 + 2] = UMAX32/2;857 }858 }859 out_w = w*33/16;860 out_h = h*33/16;861 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);862 write32("test-output/seantest_1.png", output,out_w,out_h);863864 out_w = w*16/33;865 out_h = h*16/33;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_2.png", output,out_w,out_h);868}869870871void test_suite(int argc, char **argv)872{873 int i;

Merge branch 'stbir_warnings' of https://github.com/BSVino/stb into workingSean Barrettbefore adoptionbc58f37

line 874why, one step back
874 const char *barbara;

fix -Wcast-qual in stb_rect_pack, stb_textedit, stb_truetypeSean Barrettbefore adoption5d9423f

line 875why, one step back
875

Merge branch 'stbir_warnings' of https://github.com/BSVino/stb into workingSean Barrettbefore adoptionbc58f37

line 876why, one step back
876 mkdir("test-output", 777);

fix -Wcast-qual in stb_rect_pack, stb_textedit, stb_truetypeSean Barrettbefore adoption5d9423f

lines 877–1118why, one step back
877
878 if (argc > 1)879 barbara = argv[1];880 else881 barbara = "barbara.png";882883 // check what cases we need normalization for884#if 1885 {886 float x, y;887 for (x = -1; x < 1; x += 0.05f) {888 float sums[5] = { 0 };889 float o;890 for (o = -5; o <= 5; ++o) {891 sums[0] += stbir__filter_mitchell(x + o, 1);892 sums[1] += stbir__filter_catmullrom(x + o, 1);893 sums[2] += stbir__filter_cubic(x + o, 1);894 sums[3] += stbir__filter_triangle(x + o, 1);895 sums[4] += stbir__filter_trapezoid(x + o, 0.5f);896 }897 for (i = 0; i < 5; ++i)898 STBIR_ASSERT(sums[i] >= 1.0 - 0.001 && sums[i] <= 1.0 + 0.001);899 }900901#if 1 902 for (y = 0.11f; y < 1; y += 0.01f) { // Step903 for (x = -1; x < 1; x += 0.05f) { // Phase904 float sums[5] = { 0 };905 float o;906 for (o = -5; o <= 5; o += y) {907 sums[0] += y * stbir__filter_mitchell(x + o, 1);908 sums[1] += y * stbir__filter_catmullrom(x + o, 1);909 sums[2] += y * stbir__filter_cubic(x + o, 1);910 sums[4] += y * stbir__filter_trapezoid(x + o, 0.5f);911 sums[3] += y * stbir__filter_triangle(x + o, 1);912 }913 for (i = 0; i < 3; ++i)914 STBIR_ASSERT(sums[i] >= 1.0 - 0.0170 && sums[i] <= 1.0 + 0.0170);915 }916 }917#endif918 }919#endif920921#if 0 // linear_to_srgb_uchar table922 for (i=0; i < 256; ++i) {923 float f = stbir__srgb_to_linear((i-0.5f)/255.0f);924 printf("%9d, ", (int) ((f) * (1<<28)));925 if ((i & 7) == 7)926 printf("\n");927 }928#endif929930 // old tests that hacky fix worked on - test that931 // every uint8 maps to itself932 for (i = 0; i < 256; i++) {933 float f = stbir__srgb_to_linear(float(i) / 255);934 int n = stbir__linear_to_srgb_uchar(f);935 STBIR_ASSERT(n == i);936 }937938 // new tests that hacky fix failed for - test that939 // values adjacent to uint8 round to nearest uint8940 for (i = 0; i < 256; i++) {941 for (float y = -0.42f; y <= 0.42f; y += 0.01f) {942 float f = stbir__srgb_to_linear((i+y) / 255.0f);943 int n = stbir__linear_to_srgb_uchar(f);944 STBIR_ASSERT(n == i);945 }946 }947948 test_filters();949950 test_subpixel_1();951 test_subpixel_2();952 test_subpixel_3();953 test_subpixel_4();954955 test_premul();956957 test_32();958959 // Some tests to make sure errors don't pop up with strange filter/dimension combinations.960 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);961 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);962 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);963 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);964965 int barbara_width, barbara_height, barbara_channels;966 stbi_image_free(stbi_load(barbara, &barbara_width, &barbara_height, &barbara_channels, 0));967968 int res = 10;969 // Downscaling970 {for (int i = 0; i <= res; i++)971 {972 float t = (float)i/res;973 float scale = 0.5;974 float out_scale = 2.0f/3;975 float x_shift = (barbara_width*out_scale - barbara_width*scale) * t;976 float y_shift = (barbara_height*out_scale - barbara_height*scale) * t;977978 test_subpixel_command(barbara, scale, scale, out_scale, out_scale, x_shift, y_shift);979 }}980981 // Upscaling982 {for (int i = 0; i <= res; i++)983 {984 float t = (float)i/res;985 float scale = 2;986 float out_scale = 3;987 float x_shift = (barbara_width*out_scale - barbara_width*scale) * t;988 float y_shift = (barbara_height*out_scale - barbara_height*scale) * t;989990 test_subpixel_command(barbara, scale, scale, out_scale, out_scale, x_shift, y_shift);991 }}992993 // Downscaling994 {for (int i = 0; i <= res; i++)995 {996 float t = (float)i/res / 2;997 test_subpixel_region(barbara, 0.25f, 0.25f, t, t, t+0.5f, t+0.5f);998 }}9991000 // No scaling1001 {for (int i = 0; i <= res; i++)1002 {1003 float t = (float)i/res / 2;1004 test_subpixel_region(barbara, 0.5f, 0.5f, t, t, t+0.5f, t+0.5f);1005 }}10061007 // Upscaling1008 {for (int i = 0; i <= res; i++)1009 {1010 float t = (float)i/res / 2;1011 test_subpixel_region(barbara, 1, 1, t, t, t+0.5f, t+0.5f);1012 }}10131014 {for (i = 0; i < 10; i++)1015 test_subpixel(barbara, 0.5f, 0.5f, (float)i / 10, 1);1016 }10171018 {for (i = 0; i < 10; i++)1019 test_subpixel(barbara, 0.5f, 0.5f, 1, (float)i / 10);1020 }10211022 {for (i = 0; i < 10; i++)1023 test_subpixel(barbara, 2, 2, (float)i / 10, 1);1024 }10251026 {for (i = 0; i < 10; i++)1027 test_subpixel(barbara, 2, 2, 1, (float)i / 10);1028 }10291030 // Channels test1031 test_channels(barbara, 0.5f, 0.5f, 1);1032 test_channels(barbara, 0.5f, 0.5f, 2);1033 test_channels(barbara, 0.5f, 0.5f, 3);1034 test_channels(barbara, 0.5f, 0.5f, 4);10351036 test_channels(barbara, 2, 2, 1);1037 test_channels(barbara, 2, 2, 2);1038 test_channels(barbara, 2, 2, 3);1039 test_channels(barbara, 2, 2, 4);10401041 // filter tests1042 resize_image(barbara, 2, 2, STBIR_FILTER_BOX , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-nearest.png");1043 resize_image(barbara, 2, 2, STBIR_FILTER_TRIANGLE , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-bilinear.png");1044 resize_image(barbara, 2, 2, STBIR_FILTER_CUBICBSPLINE, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-bicubic.png");1045 resize_image(barbara, 2, 2, STBIR_FILTER_CATMULLROM , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-catmullrom.png");1046 resize_image(barbara, 2, 2, STBIR_FILTER_MITCHELL , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-mitchell.png");10471048 resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_BOX , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-nearest.png");1049 resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_TRIANGLE , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-bilinear.png");1050 resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_CUBICBSPLINE, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-bicubic.png");1051 resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_CATMULLROM , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-catmullrom.png");1052 resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_MITCHELL , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-mitchell.png");10531054 {for (i = 10; i < 100; i++)1055 {1056 char outname[200];1057 sprintf(outname, "test-output/barbara-width-%d.jpg", i);1058 resize_image(barbara, (float)i / 100, 1, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);1059 }}10601061 {for (i = 110; i < 500; i += 10)1062 {1063 char outname[200];1064 sprintf(outname, "test-output/barbara-width-%d.jpg", i);1065 resize_image(barbara, (float)i / 100, 1, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);1066 }}10671068 {for (i = 10; i < 100; i++)1069 {1070 char outname[200];1071 sprintf(outname, "test-output/barbara-height-%d.jpg", i);1072 resize_image(barbara, 1, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);1073 }}10741075 {for (i = 110; i < 500; i += 10)1076 {1077 char outname[200];1078 sprintf(outname, "test-output/barbara-height-%d.jpg", i);1079 resize_image(barbara, 1, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);1080 }}10811082 {for (i = 50; i < 200; i += 10)1083 {1084 char outname[200];1085 sprintf(outname, "test-output/barbara-width-height-%d.jpg", i);1086 resize_image(barbara, 100 / (float)i, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);1087 }}10881089 test_format<unsigned short>(barbara, 0.5, 2.0, STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB);1090 test_format<unsigned short>(barbara, 0.5, 2.0, STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR);1091 test_format<unsigned short>(barbara, 2.0, 0.5, STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB);1092 test_format<unsigned short>(barbara, 2.0, 0.5, STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR);10931094 test_format<unsigned int>(barbara, 0.5, 2.0, STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB);1095 test_format<unsigned int>(barbara, 0.5, 2.0, STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR);1096 test_format<unsigned int>(barbara, 2.0, 0.5, STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB);1097 test_format<unsigned int>(barbara, 2.0, 0.5, STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR);10981099 test_float(barbara, 0.5, 2.0, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB);1100 test_float(barbara, 0.5, 2.0, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR);1101 test_float(barbara, 2.0, 0.5, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB);1102 test_float(barbara, 2.0, 0.5, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR);11031104 // Edge behavior tests1105 resize_image("hgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR, "test-output/hgradient-clamp.png");1106 resize_image("hgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_WRAP, STBIR_COLORSPACE_LINEAR, "test-output/hgradient-wrap.png");11071108 resize_image("vgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR, "test-output/vgradient-clamp.png");1109 resize_image("vgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_WRAP, STBIR_COLORSPACE_LINEAR, "test-output/vgradient-wrap.png");11101111 resize_image("1px-border.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_LINEAR, "test-output/1px-border-reflect.png");1112 resize_image("1px-border.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR, "test-output/1px-border-clamp.png");11131114 // sRGB tests1115 resize_image("gamma_colors.jpg", .5f, .5f, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_SRGB, "test-output/gamma_colors.jpg");1116 resize_image("gamma_2.2.jpg", .5f, .5f, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_SRGB, "test-output/gamma_2.2.jpg");1117 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");1118}