Skip to content
Myr

Merge branch 'master' into working

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sean Barrett · committed by Sean Barrett · parents 671f04b, dea604b · Browse files · 13 files+3764 added−21 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

13 files+3764 added−21 removed

  • tests:+1103 added−0 removed
  • other:+2661 added−21 removed

13 files+3764 added−21 removed

modified:docs/stb_resample_ideas.txt+9 added
188 unmodified lines
189 f(x) = a*x^3 - 5*a*x^2 + 8*a*x - 4*a for 1 < x <= 2190 f(x) = 0 otherwise191 "a" is configurable, try -1/2 (from http://pixinsight.com/forum/index.php?topic=556.0 )192193194195Wish list:196 s0, t0, s1, t1 vs scale_x, scale_y, offset_x, offset_y - What's the best interface?197 Separate wrap modes and filter modes per axis198 Alpha test coverage respecting resize (FloatImage::alphaTestCoverage and FloatImage::scaleAlphaToCoverage: https://code.google.com/p/nvidia-texture-tools/source/browse/trunk/src/nvimage/FloatImage.cpp)199 Installable filter kernels200201
added:tests/resample_test_c.c+5 added
1#define STB_IMAGE_RESIZE_IMPLEMENTATION2#define STB_IMAGE_RESIZE_STATIC3#include "stb_image_resize.h"45// Just to make sure it will build properly with a c compiler
added:tests/resample_test.cpp+983 added
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}
added:tests/resize.dsp+93 added
1# Microsoft Developer Studio Project File - Name="resize" - Package Owner=<4>2# Microsoft Developer Studio Generated Build File, Format Version 6.003# ** DO NOT EDIT **45# TARGTYPE "Win32 (x86) Console Application" 0x010367CFG=resize - Win32 Debug8!MESSAGE This is not a valid makefile. To build this project using NMAKE,9!MESSAGE use the Export Makefile command and run10!MESSAGE 11!MESSAGE NMAKE /f "resize.mak".12!MESSAGE 13!MESSAGE You can specify a configuration when running NMAKE14!MESSAGE by defining the macro CFG on the command line. For example:15!MESSAGE 16!MESSAGE NMAKE /f "resize.mak" CFG="resize - Win32 Debug"17!MESSAGE 18!MESSAGE Possible choices for configuration are:19!MESSAGE 20!MESSAGE "resize - Win32 Release" (based on "Win32 (x86) Console Application")21!MESSAGE "resize - Win32 Debug" (based on "Win32 (x86) Console Application")22!MESSAGE 2324# Begin Project25# PROP AllowPerConfigDependencies 026# PROP Scc_ProjName ""27# PROP Scc_LocalPath ""28CPP=cl.exe29RSC=rc.exe3031!IF "$(CFG)" == "resize - Win32 Release"3233# PROP BASE Use_MFC 034# PROP BASE Use_Debug_Libraries 035# PROP BASE Output_Dir "Release"36# PROP BASE Intermediate_Dir "Release"37# PROP BASE Target_Dir ""38# PROP Use_MFC 039# PROP Use_Debug_Libraries 040# PROP Output_Dir "Release"41# PROP Intermediate_Dir "Release"42# PROP Ignore_Export_Lib 043# PROP Target_Dir ""44# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c45# ADD CPP /nologo /G6 /W3 /GX /Z7 /O2 /I ".." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c46# ADD BASE RSC /l 0x409 /d "NDEBUG"47# ADD RSC /l 0x409 /d "NDEBUG"48BSC32=bscmake.exe49# ADD BASE BSC32 /nologo50# ADD BSC32 /nologo51LINK32=link.exe52# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I38653# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I3865455!ELSEIF "$(CFG)" == "resize - Win32 Debug"5657# PROP BASE Use_MFC 058# PROP BASE Use_Debug_Libraries 159# PROP BASE Output_Dir "Debug"60# PROP BASE Intermediate_Dir "Debug"61# PROP BASE Target_Dir ""62# PROP Use_MFC 063# PROP Use_Debug_Libraries 164# PROP Output_Dir "Debug"65# PROP Intermediate_Dir "Debug"66# PROP Target_Dir ""67# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c68# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c69# ADD BASE RSC /l 0x409 /d "_DEBUG"70# ADD RSC /l 0x409 /d "_DEBUG"71BSC32=bscmake.exe72# ADD BASE BSC32 /nologo73# ADD BSC32 /nologo74LINK32=link.exe75# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept76# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept7778!ENDIF 7980# Begin Target8182# Name "resize - Win32 Release"83# Name "resize - Win32 Debug"84# Begin Source File8586SOURCE=.\resample_test.cpp87# End Source File88# Begin Source File8990SOURCE=..\stb_image_resize.h91# End Source File92# End Target93# End Project
modified:tests/stb.dsp+8 added
104 unmodified lines
105SOURCE=..\stb_dxt.h106# End Source File107# Begin Source File108109SOURCE=..\stb_herringbone_wang_tile.h110# End Source File111# Begin Source File112113SOURCE=..\stb_image.h114# End Source File115# Begin Source File116117SOURCE=..\stb_image_resize.h118# End Source File119# Begin Source File120121SOURCE=..\stb_image_write.h122# End Source File123# Begin Source File124
36 unmodified lines
modified:tests/stb.dsw+12 added
61 unmodified lines
62}}}6364###############################################################################6566Project: "resize"=.\resize\resize.dsp - Package Owner=<4>6768Package=<5>69{{{70}}}7172Package=<4>73{{{74}}}7576###############################################################################7778Project: "stb"=.\stb.dsp - Package Owner=<4>7980Package=<5>81{{{
56 unmodified lines
modified:tests/test_c_compilation.c+2 added
3 unmodified lines
4#define STB_C_LEXER_IMPLEMENTATIOn5#define STB_DIVIDE_IMPLEMENTATION6#define STB_IMAGE_IMPLEMENTATION7#define STB_HERRINGBONE_WANG_TILE_IMEPLEMENTATIOn8#define STB_IMAGE_RESIZE_IMPLEMENTATION910#include "stb_herringbone_wang_tile.h"11#include "stb_image.h"12#include "stb_image_write.h"13#include "stb_perlin.h"14#include "stb_dxt.h"15#include "stb_c_lexer.h"16#include "stb_divide.h"17#include "stb_image_resize.h"
modified:tools/make_readme.dsp+4 added
82 unmodified lines
83# Begin Source File8485SOURCE=.\make_readme.c86# End Source File87# Begin Source File8889SOURCE=.\README.list90# End Source File91# End Target92# End Project
modified:tools/README.list+13 added-12 removed
1stb_vorbis.c | audio | decode ogg vorbis files from file/memory to float/16-bit signed output2stb_image.h | graphics | image loading/decoding from file/memory: JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC3stb_truetype.h | graphics | parse, decode, and rasterize characters from truetype fonts4stb_image_write.h | graphics | image writing to disk: PNG, TGA, BMP5stretchy_buffer.h | utility | typesafe dynamic array for C (i.e. approximation to vector<>), doesn't compile as C++6stb_textedit.h | UI | guts of a text editor for games etc implementing them from scratch7stb_dxt.h | 3D graphics | Fabian "ryg" Giesen's real-time DXT compressor8stb_herringbone_wang_tile.h | games | herringbone Wang tile map generator9stb_perlin.h | 3D graphics | revised Perlin noise (3D input, 1D output)10stb_c_lexer.h | parsing | simplify writing parsers for C-like languages11stb_divide.h | math | more useful 32-bit modulus e.g. "euclidean divide"12stb.h | misc | helper functions for C, mostly redundant in C++; basically author's personal stuff1stb_vorbis.c | audio | decode ogg vorbis files from file/memory to float/16-bit signed output2stb_image.h | graphics | image loading/decoding from file/memory: JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC3stb_truetype.h | graphics | parse, decode, and rasterize characters from truetype fonts4stb_image_write.h | graphics | image writing to disk: PNG, TGA, BMP5stb_image_resize.h | graphics | resize images larger/smaller with good quality6stretchy_buffer.h | utility | typesafe dynamic array for C (i.e. approximation to vector<>), doesn't compile as C++7stb_textedit.h | UI | guts of a text editor for games etc implementing them from scratch8stb_dxt.h | 3D graphics | Fabian "ryg" Giesen's real-time DXT compressor9stb_perlin.h | 3D graphics | revised Perlin noise (3D input, 1D output)10stb_herringbone_wang_tile.h | games | herringbone Wang tile map generator11stb_c_lexer.h | parsing | simplify writing parsers for C-like languages12stb_divide.h | math | more useful 32-bit modulus e.g. "euclidean divide"13stb.h | misc | helper functions for C, mostly redundant in C++; basically author's personal stuff
modified:README.md+3 added-2 removed
added:stb_image_resize.h+2595 added
1/* stb_image_resize - v0.90 - public domain image resizing2 by Jorge L Rodriguez (@VinoBS) - 20143 http://github.com/nothings/stb45 Written with emphasis on usability, portability, and efficiency. (No6 SIMD or threads, so it be easily outperformed by libs that use those.)7 Only scaling and translation is supported, no rotations or shears.8 Easy API downsamples w/Mitchell filter, upsamples w/cubic interpolation.910 COMPILING & LINKING11 In one C/C++ file that #includes this file, do this:12 #define STB_IMAGE_RESIZE_IMPLEMENTATION13 before the #include. That will create the implementation in that file.1415 QUICKSTART16 stbir_resize_uint8( input_pixels , in_w , in_h , 0,17 output_pixels, out_w, out_h, 0, num_channels)18 stbir_resize_float(...)19 stbir_resize_uint8_srgb( input_pixels , in_w , in_h , 0,20 output_pixels, out_w, out_h, 0,21 num_channels , alpha_chan , 0)22 stbir_resize_uint8_srgb_edgemode(23 input_pixels , in_w , in_h , 0, 24 output_pixels, out_w, out_h, 0, 25 num_channels , alpha_chan , 0, STBIR_EDGE_CLAMP)26 // WRAP/REFLECT/ZERO2728 FULL API29 See the "header file" section of the source for API documentation.3031 ADDITIONAL DOCUMENTATION3233 SRGB & FLOATING POINT REPRESENTATION34 Some srgb-related code in this library relies on floats being 32-bit35 IEEE floating point, and relies on a specific bitpacking order of C36 bitfields. If you are on a system that uses non-IEEE floats or packs37 C bitfields in the opposite order, then you can use a slower fallback38 codepath by defining STBIR_NON_IEEE_FLOAT. (We didn't make this choice39 idly; using mostly-but-not-100%-portable-code for this is a massive40 speedup, especially upsampling where colorspace conversion dominates.)4142 MEMORY ALLOCATION43 The resize functions here perform a single memory allocation using44 malloc. To control the memory allocation, before the #include that45 triggers the implementation, do:4647 #define STBIR_MALLOC(size,context) ...48 #define STBIR_FREE(ptr,context) ...4950 Each resize function makes exactly one call to malloc/free, so to use51 temp memory, store the temp memory in the context and return that.5253 ASSERT54 Define STBIR_ASSERT(boolval) to override assert() and not use assert.h5556 OPTIMIZATION57 Define STBIR_SATURATE_INT to compute clamp values in-range using58 integer operations instead of float operations. This may be faster59 on some platforms.6061 DEFAULT FILTERS62 For functions which don't provide explicit control over what filters63 to use, you can change the compile-time defaults with6465 #define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_something66 #define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_something6768 See stbir_filter in the header-file section for the list of filters.6970 NEW FILTERS71 A number of 1D filter kernels are used. For a list of72 supported filters see the stbir_filter enum. To add a new filter,73 write a filter function and add it to stbir__filter_info_table.7475 PROGRESS76 For interactive use with slow resize operations, you can install77 a progress-report callback:7879 #define STBIR_PROGRESS_REPORT(val) some_func(val)8081 The parameter val is a float which goes from 0 to 1 as progress is made.8283 For example:8485 static void my_progress_report(float progress);86 #define STBIR_PROGRESS_REPORT(val) my_progress_report(val)8788 #define STB_IMAGE_RESIZE_IMPLEMENTATION89 #include "stb_image_resize.h"9091 static void my_progress_report(float progress)92 {93 printf("Progress: %f%%\n", progress*100);94 }9596 MAX CHANNELS97 If your image has more than 64 channels, define STBIR_MAX_CHANNELS98 to the max you'll have.99100 ALPHA CHANNEL101 Most of the resizing functions provide the ability to control how102 the alpha channel of an image is processed. The important things103 to know about this:104105 1. The best mathematically-behaved version of alpha to use is106 called "premultiplied alpha", in which the other color channels107 have had the alpha value multiplied in. If you use premultiplied108 alpha, linear filtering (such as image resampling done by this109 library, or performed in texture units on GPUs) does the "right110 thing". While premultiplied alpha is standard in the movie CGI111 industry, it is still uncommon in the videogame/real-time world.112113 If you linearly filter non-premultiplied alpha, strange effects114 occur. (For example, the average of 1% opaque bright green115 and 99% opaque black produces 50% transparent dark green when116 non-premultiplied, whereas premultiplied it produces 50%117 transparent near-black. The former introduces green energy118 that doesn't exist in the source image.)119120 2. Artists should not edit premultiplied-alpha images; artists121 want non-premultiplied alpha images. Thus, art tools generally output122 non-premultiplied alpha images.123124 3. You will get best results in most cases by converting images125 to premultiplied alpha before processing them mathematically.126127 4. If you pass the flag STBIR_FLAG_ALPHA_PREMULTIPLIED, the128 resizer does not do anything special for the alpha channel;129 it is resampled identically to other channels. This produces130 the correct results for premultiplied-alpha images, but produces131 less-than-ideal results for non-premultiplied-alpha images.132133 5. If you do not pass the flag STBIR_FLAG_ALPHA_PREMULTIPLIED,134 then the resizer weights the contribution of input pixels135 based on their alpha values, or, equivalently, it multiplies136 the alpha value into the color channels, resamples, then divides137 by the resultant alpha value. Input pixels which have alpha=0 do138 not contribute at all to output pixels unless _all_ of the input139 pixels affecting that output pixel have alpha=0, in which case140 the result for that pixel is the same as it would be without141 STBIR_FLAG_ALPHA_PREMULTIPLIED. However, this is only true for142 input images in integer formats. For input images in float format,143 input pixels with alpha=0 have no effect, and output pixels144 which have alpha=0 will be 0 in all channels. (For float images,145 you can manually achieve the same result by adding a tiny epsilon146 value to the alpha channel of every image, and then subtracting147 or clamping it at the end.)148149 6. You can suppress the behavior described in #5 and make150 all-0-alpha pixels have 0 in all channels by #defining151 STBIR_NO_ALPHA_EPSILON.152153 7. You can separately control whether the alpha channel is154 interpreted as linear or affected by the colorspace. By default155 it is linear; you almost never want to apply the colorspace.156 (For example, graphics hardware does not apply sRGB conversion157 to the alpha channel.)158159 ADDITIONAL CONTRIBUTORS160 Sean Barrett: API design, optimizations161 162 REVISIONS163 0.90 (2014-09-17) first released version164165 LICENSE166 This software is in the public domain. Where that dedication is not167 recognized, you are granted a perpetual, irrevocable license to copy168 and modify this file as you see fit.169170 TODO171 Don't decode all of the image data when only processing a partial tile172 Don't use full-width decode buffers when only processing a partial tile173 When processing wide images, break processing into tiles so data fits in L1 cache174 Installable filters?175 Resize that respects alpha test coverage176 (Reference code: FloatImage::alphaTestCoverage and FloatImage::scaleAlphaToCoverage:177 https://code.google.com/p/nvidia-texture-tools/source/browse/trunk/src/nvimage/FloatImage.cpp )178*/179180#ifndef STBIR_INCLUDE_STB_IMAGE_RESIZE_H181#define STBIR_INCLUDE_STB_IMAGE_RESIZE_H182183#ifdef _MSC_VER184typedef unsigned char stbir_uint8;185typedef unsigned short stbir_uint16;186typedef unsigned int stbir_uint32;187#else188#include <stdint.h>189typedef uint8_t stbir_uint8;190typedef uint16_t stbir_uint16;191typedef uint32_t stbir_uint32;192#endif193194#ifdef STB_IMAGE_RESIZE_STATIC195#define STBIRDEF static196#else197#ifdef __cplusplus198#define STBIRDEF extern "C"199#else200#define STBIRDEF extern201#endif202#endif203204205//////////////////////////////////////////////////////////////////////////////206//207// Easy-to-use API:208//209// * "input pixels" points to an array of image data with 'num_channels' channels (e.g. RGB=3, RGBA=4)210// * input_w is input image width (x-axis), input_h is input image height (y-axis)211// * stride is the offset between successive rows of image data in memory, in bytes. you can212// specify 0 to mean packed continuously in memory213// * alpha channel is treated identically to other channels.214// * colorspace is linear or sRGB as specified by function name215// * returned result is 1 for success or 0 in case of an error.216// #define STBIR_ASSERT() to trigger an assert on parameter validation errors.217// * Memory required grows approximately linearly with input and output size, but with218// discontinuities at input_w == output_w and input_h == output_h.219// * These functions use a "default" resampling filter defined at compile time. To change the filter,220// you can change the compile-time defaults by #defining STBIR_DEFAULT_FILTER_UPSAMPLE221// and STBIR_DEFAULT_FILTER_DOWNSAMPLE, or you can use the medium-complexity API.222223STBIRDEF int stbir_resize_uint8( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,224 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,225 int num_channels);226227STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,228 float *output_pixels, int output_w, int output_h, int output_stride_in_bytes,229 int num_channels);230231232// The following functions interpret image data as gamma-corrected sRGB. 233// Specify STBIR_ALPHA_CHANNEL_NONE if you have no alpha channel,234// or otherwise provide the index of the alpha channel. Flags value235// of 0 will probably do the right thing if you're not sure what236// the flags mean.237238#define STBIR_ALPHA_CHANNEL_NONE -1239240// Set this flag if your texture has premultiplied alpha. Otherwise, stbir will241// use alpha-weighted resampling (effectively premultiplying, resampling,242// then unpremultiplying).243#define STBIR_FLAG_ALPHA_PREMULTIPLIED (1 << 0)244// The specified alpha channel should be handled as gamma-corrected value even245// when doing sRGB operations.246#define STBIR_FLAG_ALPHA_USES_COLORSPACE (1 << 1)247248STBIRDEF int stbir_resize_uint8_srgb(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,249 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,250 int num_channels, int alpha_channel, int flags);251252253typedef enum254{255 STBIR_EDGE_CLAMP = 1,256 STBIR_EDGE_REFLECT = 2,257 STBIR_EDGE_WRAP = 3,258 STBIR_EDGE_ZERO = 4,259} stbir_edge;260261// This function adds the ability to specify how requests to sample off the edge of the image are handled.262STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,263 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,264 int num_channels, int alpha_channel, int flags,265 stbir_edge edge_wrap_mode);266267//////////////////////////////////////////////////////////////////////////////268//269// Medium-complexity API270//271// This extends the easy-to-use API as follows:272//273// * Alpha-channel can be processed separately274// * If alpha_channel is not STBIR_ALPHA_CHANNEL_NONE275// * Alpha channel will not be gamma corrected (unless flags&STBIR_FLAG_GAMMA_CORRECT)276// * Filters will be weighted by alpha channel (unless flags&STBIR_FLAG_ALPHA_PREMULTIPLIED)277// * Filter can be selected explicitly278// * uint16 image type279// * sRGB colorspace available for all types280// * context parameter for passing to STBIR_MALLOC281282typedef enum283{284 STBIR_FILTER_DEFAULT = 0, // use same filter type that easy-to-use API chooses285 STBIR_FILTER_BOX = 1, // A trapezoid w/1-pixel wide ramps, same result as box for integer scale ratios286 STBIR_FILTER_TRIANGLE = 2, // On upsampling, produces same results as bilinear texture filtering287 STBIR_FILTER_CUBICBSPLINE = 3, // The cubic b-spline (aka Mitchell-Netrevalli with B=1,C=0), gaussian-esque288 STBIR_FILTER_CATMULLROM = 4, // An interpolating cubic spline289 STBIR_FILTER_MITCHELL = 5, // Mitchell-Netrevalli filter with B=1/3, C=1/3290} stbir_filter;291292typedef enum293{294 STBIR_COLORSPACE_LINEAR,295 STBIR_COLORSPACE_SRGB,296297 STBIR_MAX_COLORSPACES,298} stbir_colorspace;299300// The following functions are all identical except for the type of the image data301302STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,303 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,304 int num_channels, int alpha_channel, int flags,305 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 306 void *alloc_context);307308STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int input_w , int input_h , int input_stride_in_bytes,309 stbir_uint16 *output_pixels , int output_w, int output_h, int output_stride_in_bytes,310 int num_channels, int alpha_channel, int flags,311 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 312 void *alloc_context);313314STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,315 float *output_pixels , int output_w, int output_h, int output_stride_in_bytes,316 int num_channels, int alpha_channel, int flags,317 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 318 void *alloc_context);319320321322//////////////////////////////////////////////////////////////////////////////323//324// Full-complexity API325//326// This extends the medium API as follows:327//328// * uint32 image type329// * not typesafe330// * separate filter types for each axis331// * separate edge modes for each axis332// * can specify scale explicitly for subpixel correctness333// * can specify image source tile using texture coordinates334335typedef enum336{337 STBIR_TYPE_UINT8 ,338 STBIR_TYPE_UINT16,339 STBIR_TYPE_UINT32,340 STBIR_TYPE_FLOAT ,341342 STBIR_MAX_TYPES343} stbir_datatype;344345STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,346 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,347 stbir_datatype datatype,348 int num_channels, int alpha_channel, int flags,349 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, 350 stbir_filter filter_horizontal, stbir_filter filter_vertical,351 stbir_colorspace space, void *alloc_context);352353STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,354 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,355 stbir_datatype datatype,356 int num_channels, int alpha_channel, int flags,357 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, 358 stbir_filter filter_horizontal, stbir_filter filter_vertical,359 stbir_colorspace space, void *alloc_context,360 float x_scale, float y_scale,361 float x_offset, float y_offset);362363STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,364 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,365 stbir_datatype datatype,366 int num_channels, int alpha_channel, int flags,367 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, 368 stbir_filter filter_horizontal, stbir_filter filter_vertical,369 stbir_colorspace space, void *alloc_context,370 float s0, float t0, float s1, float t1);371// (s0, t0) & (s1, t1) are the top-left and bottom right corner (uv addressing style: [0, 1]x[0, 1]) of a region of the input image to use.372373//374//375//// end header file /////////////////////////////////////////////////////376#endif // STBIR_INCLUDE_STB_IMAGE_RESIZE_H377378379380381382#ifdef STB_IMAGE_RESIZE_IMPLEMENTATION383384#ifndef STBIR_ASSERT385#include <assert.h>386#define STBIR_ASSERT(x) assert(x)387#endif388389#ifdef STBIR_DEBUG390#define STBIR__DEBUG_ASSERT STBIR_ASSERT391#else392#define STBIR__DEBUG_ASSERT393#endif394395// If you hit this it means I haven't done it yet.396#define STBIR__UNIMPLEMENTED(x) STBIR_ASSERT(!(x))397398// For memset399#include <string.h>400401#include <math.h>402403#ifndef STBIR_MALLOC404#include <stdlib.h>405#define STBIR_MALLOC(size,c) malloc(size)406#define STBIR_FREE(ptr,c) free(ptr)407#endif408409#ifndef _MSC_VER410#ifdef __cplusplus411#define stbir__inline inline412#else413#define stbir__inline414#endif415#else416#define stbir__inline __forceinline417#endif418419420// should produce compiler error if size is wrong421typedef unsigned char stbir__validate_uint32[sizeof(stbir_uint32) == 4 ? 1 : -1];422423#ifdef _MSC_VER424#define STBIR__NOTUSED(v) (void)(v)425#else426#define STBIR__NOTUSED(v) (void)sizeof(v)427#endif428429#define STBIR__ARRAY_SIZE(a) (sizeof((a))/sizeof((a)[0]))430431#ifndef STBIR_DEFAULT_FILTER_UPSAMPLE432#define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_CATMULLROM433#endif434435#ifndef STBIR_DEFAULT_FILTER_DOWNSAMPLE436#define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_MITCHELL437#endif438439#ifndef STBIR_PROGRESS_REPORT440#define STBIR_PROGRESS_REPORT(float_0_to_1)441#endif442443#ifndef STBIR_MAX_CHANNELS444#define STBIR_MAX_CHANNELS 64445#endif446447#if STBIR_MAX_CHANNELS > 65536448#error "Too many channels; STBIR_MAX_CHANNELS must be no more than 65536."449// because we store the indices in 16-bit variables450#endif451452// This value is added to alpha just before premultiplication to avoid453// zeroing out color values. It is equivalent to 2^-80. If you don't want454// that behavior (it may interfere if you have floating point images with455// very small alpha values) then you can define STBIR_NO_ALPHA_EPSILON to456// disable it.457#ifndef STBIR_ALPHA_EPSILON458#define STBIR_ALPHA_EPSILON ((float)1 / (1 << 20) / (1 << 20) / (1 << 20) / (1 << 20))459#endif460461462463#ifdef _MSC_VER464#define STBIR__UNUSED_PARAM(v) (void)(v)465#else466#define STBIR__UNUSED_PARAM(v) (void)sizeof(v)467#endif468469// must match stbir_datatype470static unsigned char stbir__type_size[] = {471 1, // STBIR_TYPE_UINT8472 2, // STBIR_TYPE_UINT16473 4, // STBIR_TYPE_UINT32474 4, // STBIR_TYPE_FLOAT475};476477// Kernel function centered at 0478typedef float (stbir__kernel_fn)(float x, float scale);479typedef float (stbir__support_fn)(float scale);480481typedef struct482{483 stbir__kernel_fn* kernel;484 stbir__support_fn* support;485} stbir__filter_info;486487// When upsampling, the contributors are which source pixels contribute.488// When downsampling, the contributors are which destination pixels are contributed to.489typedef struct490{491 int n0; // First contributing pixel492 int n1; // Last contributing pixel493} stbir__contributors;494495typedef struct496{497 const void* input_data;498 int input_w;499 int input_h;500 int input_stride_bytes;501502 void* output_data;503 int output_w;504 int output_h;505 int output_stride_bytes;506507 float s0, t0, s1, t1;508509 float horizontal_shift; // Units: output pixels510 float vertical_shift; // Units: output pixels511 float horizontal_scale;512 float vertical_scale;513514 int channels;515 int alpha_channel;516 stbir_uint32 flags;517 stbir_datatype type;518 stbir_filter horizontal_filter;519 stbir_filter vertical_filter;520 stbir_edge edge_horizontal;521 stbir_edge edge_vertical;522 stbir_colorspace colorspace;523524 stbir__contributors* horizontal_contributors;525 float* horizontal_coefficients;526527 stbir__contributors* vertical_contributors;528 float* vertical_coefficients;529530 int decode_buffer_pixels;531 float* decode_buffer;532533 float* horizontal_buffer;534535 // cache these because ceil/floor are inexplicably showing up in profile536 int horizontal_coefficient_width;537 int vertical_coefficient_width;538 int horizontal_filter_pixel_width;539 int vertical_filter_pixel_width;540 int horizontal_filter_pixel_margin;541 int vertical_filter_pixel_margin;542 int horizontal_num_contributors;543 int vertical_num_contributors;544545 int ring_buffer_length_bytes; // The length of an individual entry in the ring buffer. The total number of ring buffers is stbir__get_filter_pixel_width(filter)546 int ring_buffer_first_scanline;547 int ring_buffer_last_scanline;548 int ring_buffer_begin_index;549 float* ring_buffer;550551 float* encode_buffer; // A temporary buffer to store floats so we don't lose precision while we do multiply-adds.552553 int horizontal_contributors_size;554 int horizontal_coefficients_size;555 int vertical_contributors_size;556 int vertical_coefficients_size;557 int decode_buffer_size;558 int horizontal_buffer_size;559 int ring_buffer_size;560 int encode_buffer_size;561} stbir__info;562563static stbir__inline int stbir__min(int a, int b)564{565 return a < b ? a : b;566}567568static stbir__inline int stbir__max(int a, int b)569{570 return a > b ? a : b;571}572573static stbir__inline float stbir__saturate(float x)574{575 if (x < 0)576 return 0;577578 if (x > 1)579 return 1;580581 return x;582}583584#ifdef STBIR_SATURATE_INT585static stbir__inline stbir_uint8 stbir__saturate8(int x)586{587 if ((unsigned int) x <= 255)588 return x;589590 if (x < 0)591 return 0;592593 return 255;594}595596static stbir__inline stbir_uint16 stbir__saturate16(int x)597{598 if ((unsigned int) x <= 65535)599 return x;600601 if (x < 0)602 return 0;603604 return 65535;605}606#endif607608static float stbir__srgb_uchar_to_linear_float[256] = {609 0.000000f, 0.000304f, 0.000607f, 0.000911f, 0.001214f, 0.001518f, 0.001821f, 0.002125f, 0.002428f, 0.002732f, 0.003035f,610 0.003347f, 0.003677f, 0.004025f, 0.004391f, 0.004777f, 0.005182f, 0.005605f, 0.006049f, 0.006512f, 0.006995f, 0.007499f,611 0.008023f, 0.008568f, 0.009134f, 0.009721f, 0.010330f, 0.010960f, 0.011612f, 0.012286f, 0.012983f, 0.013702f, 0.014444f,612 0.015209f, 0.015996f, 0.016807f, 0.017642f, 0.018500f, 0.019382f, 0.020289f, 0.021219f, 0.022174f, 0.023153f, 0.024158f,613 0.025187f, 0.026241f, 0.027321f, 0.028426f, 0.029557f, 0.030713f, 0.031896f, 0.033105f, 0.034340f, 0.035601f, 0.036889f,614 0.038204f, 0.039546f, 0.040915f, 0.042311f, 0.043735f, 0.045186f, 0.046665f, 0.048172f, 0.049707f, 0.051269f, 0.052861f,615 0.054480f, 0.056128f, 0.057805f, 0.059511f, 0.061246f, 0.063010f, 0.064803f, 0.066626f, 0.068478f, 0.070360f, 0.072272f,616 0.074214f, 0.076185f, 0.078187f, 0.080220f, 0.082283f, 0.084376f, 0.086500f, 0.088656f, 0.090842f, 0.093059f, 0.095307f,617 0.097587f, 0.099899f, 0.102242f, 0.104616f, 0.107023f, 0.109462f, 0.111932f, 0.114435f, 0.116971f, 0.119538f, 0.122139f,618 0.124772f, 0.127438f, 0.130136f, 0.132868f, 0.135633f, 0.138432f, 0.141263f, 0.144128f, 0.147027f, 0.149960f, 0.152926f,619 0.155926f, 0.158961f, 0.162029f, 0.165132f, 0.168269f, 0.171441f, 0.174647f, 0.177888f, 0.181164f, 0.184475f, 0.187821f,620 0.191202f, 0.194618f, 0.198069f, 0.201556f, 0.205079f, 0.208637f, 0.212231f, 0.215861f, 0.219526f, 0.223228f, 0.226966f,621 0.230740f, 0.234551f, 0.238398f, 0.242281f, 0.246201f, 0.250158f, 0.254152f, 0.258183f, 0.262251f, 0.266356f, 0.270498f,622 0.274677f, 0.278894f, 0.283149f, 0.287441f, 0.291771f, 0.296138f, 0.300544f, 0.304987f, 0.309469f, 0.313989f, 0.318547f,623 0.323143f, 0.327778f, 0.332452f, 0.337164f, 0.341914f, 0.346704f, 0.351533f, 0.356400f, 0.361307f, 0.366253f, 0.371238f,624 0.376262f, 0.381326f, 0.386430f, 0.391573f, 0.396755f, 0.401978f, 0.407240f, 0.412543f, 0.417885f, 0.423268f, 0.428691f,625 0.434154f, 0.439657f, 0.445201f, 0.450786f, 0.456411f, 0.462077f, 0.467784f, 0.473532f, 0.479320f, 0.485150f, 0.491021f,626 0.496933f, 0.502887f, 0.508881f, 0.514918f, 0.520996f, 0.527115f, 0.533276f, 0.539480f, 0.545725f, 0.552011f, 0.558340f,627 0.564712f, 0.571125f, 0.577581f, 0.584078f, 0.590619f, 0.597202f, 0.603827f, 0.610496f, 0.617207f, 0.623960f, 0.630757f,628 0.637597f, 0.644480f, 0.651406f, 0.658375f, 0.665387f, 0.672443f, 0.679543f, 0.686685f, 0.693872f, 0.701102f, 0.708376f,629 0.715694f, 0.723055f, 0.730461f, 0.737911f, 0.745404f, 0.752942f, 0.760525f, 0.768151f, 0.775822f, 0.783538f, 0.791298f,630 0.799103f, 0.806952f, 0.814847f, 0.822786f, 0.830770f, 0.838799f, 0.846873f, 0.854993f, 0.863157f, 0.871367f, 0.879622f,631 0.887923f, 0.896269f, 0.904661f, 0.913099f, 0.921582f, 0.930111f, 0.938686f, 0.947307f, 0.955974f, 0.964686f, 0.973445f,632 0.982251f, 0.991102f, 1.0f633};634635static float stbir__srgb_to_linear(float f)636{637 if (f <= 0.04045f)638 return f / 12.92f;639 else640 return (float)pow((f + 0.055f) / 1.055f, 2.4f);641}642643static float stbir__linear_to_srgb(float f)644{645 if (f <= 0.0031308f)646 return f * 12.92f;647 else648 return 1.055f * (float)pow(f, 1 / 2.4f) - 0.055f;649}650651#ifndef STBIR_NON_IEEE_FLOAT652// From https://gist.github.com/rygorous/2203834653654typedef union655{656 stbir_uint32 u;657 float f;658 struct659 {660 stbir_uint32 Mantissa : 23;661 stbir_uint32 Exponent : 8;662 stbir_uint32 Sign : 1;663 };664} stbir__FP32;665666static const stbir_uint32 fp32_to_srgb8_tab4[104] = {667 0x0073000d, 0x007a000d, 0x0080000d, 0x0087000d, 0x008d000d, 0x0094000d, 0x009a000d, 0x00a1000d,668 0x00a7001a, 0x00b4001a, 0x00c1001a, 0x00ce001a, 0x00da001a, 0x00e7001a, 0x00f4001a, 0x0101001a,669 0x010e0033, 0x01280033, 0x01410033, 0x015b0033, 0x01750033, 0x018f0033, 0x01a80033, 0x01c20033,670 0x01dc0067, 0x020f0067, 0x02430067, 0x02760067, 0x02aa0067, 0x02dd0067, 0x03110067, 0x03440067,671 0x037800ce, 0x03df00ce, 0x044600ce, 0x04ad00ce, 0x051400ce, 0x057b00c5, 0x05dd00bc, 0x063b00b5,672 0x06970158, 0x07420142, 0x07e30130, 0x087b0120, 0x090b0112, 0x09940106, 0x0a1700fc, 0x0a9500f2,673 0x0b0f01cb, 0x0bf401ae, 0x0ccb0195, 0x0d950180, 0x0e56016e, 0x0f0d015e, 0x0fbc0150, 0x10630143,674 0x11070264, 0x1238023e, 0x1357021d, 0x14660201, 0x156601e9, 0x165a01d3, 0x174401c0, 0x182401af,675 0x18fe0331, 0x1a9602fe, 0x1c1502d2, 0x1d7e02ad, 0x1ed4028d, 0x201a0270, 0x21520256, 0x227d0240,676 0x239f0443, 0x25c003fe, 0x27bf03c4, 0x29a10392, 0x2b6a0367, 0x2d1d0341, 0x2ebe031f, 0x304d0300,677 0x31d105b0, 0x34a80555, 0x37520507, 0x39d504c5, 0x3c37048b, 0x3e7c0458, 0x40a8042a, 0x42bd0401,678 0x44c20798, 0x488e071e, 0x4c1c06b6, 0x4f76065d, 0x52a50610, 0x55ac05cc, 0x5892058f, 0x5b590559,679 0x5e0c0a23, 0x631c0980, 0x67db08f6, 0x6c55087f, 0x70940818, 0x74a007bd, 0x787d076c, 0x7c330723,680};681 682static stbir_uint8 stbir__linear_to_srgb_uchar(float in)683{684 static const stbir__FP32 almostone = { 0x3f7fffff }; // 1-eps685 static const stbir__FP32 minval = { (127-13) << 23 };686 stbir_uint32 tab,bias,scale,t;687 stbir__FP32 f;688 689 // Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively.690 // The tests are carefully written so that NaNs map to 0, same as in the reference691 // implementation.692 if (!(in > minval.f)) // written this way to catch NaNs693 in = minval.f;694 if (in > almostone.f)695 in = almostone.f;696 697 // Do the table lookup and unpack bias, scale698 f.f = in;699 tab = fp32_to_srgb8_tab4[(f.u - minval.u) >> 20];700 bias = (tab >> 16) << 9;701 scale = tab & 0xffff;702 703 // Grab next-highest mantissa bits and perform linear interpolation704 t = (f.u >> 12) & 0xff;705 return (unsigned char) ((bias + scale*t) >> 16);706}707708#else709// sRGB transition values, scaled by 1<<28710static int stbir__srgb_offset_to_linear_scaled[256] =711{712 0, 40738, 122216, 203693, 285170, 366648, 448125, 529603,713 611080, 692557, 774035, 855852, 942009, 1033024, 1128971, 1229926,714 1335959, 1447142, 1563542, 1685229, 1812268, 1944725, 2082664, 2226148,715 2375238, 2529996, 2690481, 2856753, 3028870, 3206888, 3390865, 3580856,716 3776916, 3979100, 4187460, 4402049, 4622919, 4850123, 5083710, 5323731,717 5570236, 5823273, 6082892, 6349140, 6622065, 6901714, 7188133, 7481369,718 7781466, 8088471, 8402427, 8723380, 9051372, 9386448, 9728650, 10078021,719 10434603, 10798439, 11169569, 11548036, 11933879, 12327139, 12727857, 13136073,720 13551826, 13975156, 14406100, 14844697, 15290987, 15745007, 16206795, 16676389,721 17153826, 17639142, 18132374, 18633560, 19142734, 19659934, 20185196, 20718552,722 21260042, 21809696, 22367554, 22933648, 23508010, 24090680, 24681686, 25281066,723 25888850, 26505076, 27129772, 27762974, 28404716, 29055026, 29713942, 30381490,724 31057708, 31742624, 32436272, 33138682, 33849884, 34569912, 35298800, 36036568,725 36783260, 37538896, 38303512, 39077136, 39859796, 40651528, 41452360, 42262316,726 43081432, 43909732, 44747252, 45594016, 46450052, 47315392, 48190064, 49074096,727 49967516, 50870356, 51782636, 52704392, 53635648, 54576432, 55526772, 56486700,728 57456236, 58435408, 59424248, 60422780, 61431036, 62449032, 63476804, 64514376,729 65561776, 66619028, 67686160, 68763192, 69850160, 70947088, 72053992, 73170912,730 74297864, 75434880, 76581976, 77739184, 78906536, 80084040, 81271736, 82469648,731 83677792, 84896192, 86124888, 87363888, 88613232, 89872928, 91143016, 92423512,732 93714432, 95015816, 96327688, 97650056, 98982952, 100326408, 101680440, 103045072,733 104420320, 105806224, 107202800, 108610064, 110028048, 111456776, 112896264, 114346544,734 115807632, 117279552, 118762328, 120255976, 121760536, 123276016, 124802440, 126339832,735 127888216, 129447616, 131018048, 132599544, 134192112, 135795792, 137410592, 139036528,736 140673648, 142321952, 143981456, 145652208, 147334208, 149027488, 150732064, 152447968,737 154175200, 155913792, 157663776, 159425168, 161197984, 162982240, 164777968, 166585184,738 168403904, 170234160, 172075968, 173929344, 175794320, 177670896, 179559120, 181458992,739 183370528, 185293776, 187228736, 189175424, 191133888, 193104112, 195086128, 197079968,740 199085648, 201103184, 203132592, 205173888, 207227120, 209292272, 211369392, 213458480,741 215559568, 217672656, 219797792, 221934976, 224084240, 226245600, 228419056, 230604656,742 232802400, 235012320, 237234432, 239468736, 241715280, 243974080, 246245120, 248528464,743 250824112, 253132064, 255452368, 257785040, 260130080, 262487520, 264857376, 267239664,744};745746static stbir_uint8 stbir__linear_to_srgb_uchar(float f)747{748 int x = (int) (f * (1 << 28)); // has headroom so you don't need to clamp749 int v = 0;750 int i;751752 // Refine the guess with a short binary search.753 i = v + 128; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;754 i = v + 64; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;755 i = v + 32; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;756 i = v + 16; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;757 i = v + 8; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;758 i = v + 4; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;759 i = v + 2; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;760 i = v + 1; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;761762 return (stbir_uint8) v;763}764#endif765766static float stbir__filter_trapezoid(float x, float scale)767{768 float halfscale = scale / 2;769 float t = 0.5f + halfscale;770 STBIR__DEBUG_ASSERT(scale <= 1);771772 x = (float)fabs(x);773774 if (x >= t)775 return 0;776 else777 {778 float r = 0.5f - halfscale;779 if (x <= r)780 return 1;781 else782 return (t - x) / scale;783 }784}785786static float stbir__support_trapezoid(float scale)787{788 STBIR__DEBUG_ASSERT(scale <= 1);789 return 0.5f + scale / 2;790}791792static float stbir__filter_triangle(float x, float s)793{794 STBIR__UNUSED_PARAM(s);795796 x = (float)fabs(x);797798 if (x <= 1.0f)799 return 1 - x;800 else801 return 0;802}803804static float stbir__filter_cubic(float x, float s)805{806 STBIR__UNUSED_PARAM(s);807808 x = (float)fabs(x);809810 if (x < 1.0f)811 return (4 + x*x*(3*x - 6))/6;812 else if (x < 2.0f)813 return (8 + x*(-12 + x*(6 - x)))/6;814815 return (0.0f);816}817818static float stbir__filter_catmullrom(float x, float s)819{820 STBIR__UNUSED_PARAM(s);821822 x = (float)fabs(x);823824 if (x < 1.0f)825 return 1 - x*x*(2.5f - 1.5f*x);826 else if (x < 2.0f)827 return 2 - x*(4 + x*(0.5f*x - 2.5f));828829 return (0.0f);830}831832static float stbir__filter_mitchell(float x, float s)833{834 STBIR__UNUSED_PARAM(s);835836 x = (float)fabs(x);837838 if (x < 1.0f)839 return (16 + x*x*(21 * x - 36))/18;840 else if (x < 2.0f)841 return (32 + x*(-60 + x*(36 - 7*x)))/18;842843 return (0.0f);844}845846static float stbir__support_zero(float s)847{848 STBIR__UNUSED_PARAM(s);849 return 0;850}851852static float stbir__support_one(float s)853{854 STBIR__UNUSED_PARAM(s);855 return 1;856}857858static float stbir__support_two(float s)859{860 STBIR__UNUSED_PARAM(s);861 return 2;862}863864static stbir__filter_info stbir__filter_info_table[] = {865 { NULL, stbir__support_zero },866 { stbir__filter_trapezoid, stbir__support_trapezoid },867 { stbir__filter_triangle, stbir__support_one },868 { stbir__filter_cubic, stbir__support_two },869 { stbir__filter_catmullrom, stbir__support_two },870 { stbir__filter_mitchell, stbir__support_two },871};872873stbir__inline static int stbir__use_upsampling(float ratio)874{875 return ratio > 1;876}877878stbir__inline static int stbir__use_width_upsampling(stbir__info* stbir_info)879{880 return stbir__use_upsampling(stbir_info->horizontal_scale);881}882883stbir__inline static int stbir__use_height_upsampling(stbir__info* stbir_info)884{885 return stbir__use_upsampling(stbir_info->vertical_scale);886}887888// This is the maximum number of input samples that can affect an output sample889// with the given filter890static int stbir__get_filter_pixel_width(stbir_filter filter, float scale)891{892 STBIR_ASSERT(filter != 0);893 STBIR_ASSERT(filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));894895 if (stbir__use_upsampling(scale))896 return (int)ceil(stbir__filter_info_table[filter].support(1/scale) * 2);897 else898 return (int)ceil(stbir__filter_info_table[filter].support(scale) * 2 / scale);899}900901// This is how much to expand buffers to account for filters seeking outside902// the image boundaries.903static int stbir__get_filter_pixel_margin(stbir_filter filter, float scale)904{905 return stbir__get_filter_pixel_width(filter, scale) / 2;906}907908static int stbir__get_coefficient_width(stbir_filter filter, float scale)909{910 if (stbir__use_upsampling(scale))911 return (int)ceil(stbir__filter_info_table[filter].support(1 / scale) * 2);912 else913 return (int)ceil(stbir__filter_info_table[filter].support(scale) * 2);914}915916static int stbir__get_contributors(float scale, stbir_filter filter, int input_size, int output_size)917{918 if (stbir__use_upsampling(scale))919 return output_size;920 else921 return (input_size + stbir__get_filter_pixel_margin(filter, scale) * 2);922}923924static int stbir__get_total_horizontal_coefficients(stbir__info* info)925{926 return info->horizontal_num_contributors927 * stbir__get_coefficient_width (info->horizontal_filter, info->horizontal_scale);928}929930static int stbir__get_total_vertical_coefficients(stbir__info* info)931{932 return info->vertical_num_contributors933 * stbir__get_coefficient_width (info->vertical_filter, info->vertical_scale);934}935936static stbir__contributors* stbir__get_contributor(stbir__contributors* contributors, int n)937{938 return &contributors[n];939}940941// For perf reasons this code is duplicated in stbir__resample_horizontal_upsample/downsample,942// if you change it here change it there too.943static float* stbir__get_coefficient(float* coefficients, stbir_filter filter, float scale, int n, int c)944{945 int width = stbir__get_coefficient_width(filter, scale);946 return &coefficients[width*n + c];947}948949static int stbir__edge_wrap_slow(stbir_edge edge, int n, int max)950{951 switch (edge)952 {953 case STBIR_EDGE_ZERO:954 return 0; // we'll decode the wrong pixel here, and then overwrite with 0s later955956 case STBIR_EDGE_CLAMP:957 if (n < 0)958 return 0;959960 if (n >= max)961 return max - 1;962963 return n; // NOTREACHED964965 case STBIR_EDGE_REFLECT:966 {967 if (n < 0)968 {969 if (n < max)970 return -n;971 else972 return max - 1;973 }974975 if (n >= max)976 {977 int max2 = max * 2;978 if (n >= max2)979 return 0;980 else981 return max2 - n - 1;982 }983984 return n; // NOTREACHED985 }986987 case STBIR_EDGE_WRAP:988 if (n >= 0)989 return (n % max);990 else991 {992 int m = (-n) % max;993994 if (m != 0)995 m = max - m;996997 return (m);998 }999 return n; // NOTREACHED10001001 default:1002 STBIR__UNIMPLEMENTED("Unimplemented edge type");1003 return 0;1004 }1005}10061007stbir__inline static int stbir__edge_wrap(stbir_edge edge, int n, int max)1008{1009 // avoid per-pixel switch1010 if (n >= 0 && n < max)1011 return n;1012 return stbir__edge_wrap_slow(edge, n, max);1013}10141015// What input pixels contribute to this output pixel?1016static void stbir__calculate_sample_range_upsample(int n, float out_filter_radius, float scale_ratio, float out_shift, int* in_first_pixel, int* in_last_pixel, float* in_center_of_out)1017{1018 float out_pixel_center = (float)n + 0.5f;1019 float out_pixel_influence_lowerbound = out_pixel_center - out_filter_radius;1020 float out_pixel_influence_upperbound = out_pixel_center + out_filter_radius;10211022 float in_pixel_influence_lowerbound = (out_pixel_influence_lowerbound + out_shift) / scale_ratio;1023 float in_pixel_influence_upperbound = (out_pixel_influence_upperbound + out_shift) / scale_ratio;10241025 *in_center_of_out = (out_pixel_center + out_shift) / scale_ratio;1026 *in_first_pixel = (int)(floor(in_pixel_influence_lowerbound + 0.5));1027 *in_last_pixel = (int)(floor(in_pixel_influence_upperbound - 0.5));1028}10291030// What output pixels does this input pixel contribute to?1031static void stbir__calculate_sample_range_downsample(int n, float in_pixels_radius, float scale_ratio, float out_shift, int* out_first_pixel, int* out_last_pixel, float* out_center_of_in)1032{1033 float in_pixel_center = (float)n + 0.5f;1034 float in_pixel_influence_lowerbound = in_pixel_center - in_pixels_radius;1035 float in_pixel_influence_upperbound = in_pixel_center + in_pixels_radius;10361037 float out_pixel_influence_lowerbound = in_pixel_influence_lowerbound * scale_ratio - out_shift;1038 float out_pixel_influence_upperbound = in_pixel_influence_upperbound * scale_ratio - out_shift;10391040 *out_center_of_in = in_pixel_center * scale_ratio - out_shift;1041 *out_first_pixel = (int)(floor(out_pixel_influence_lowerbound + 0.5));1042 *out_last_pixel = (int)(floor(out_pixel_influence_upperbound - 0.5));1043}10441045static void stbir__calculate_coefficients_upsample(stbir__info* stbir_info, stbir_filter filter, float scale, int in_first_pixel, int in_last_pixel, float in_center_of_out, stbir__contributors* contributor, float* coefficient_group)1046{1047 int i;1048 float total_filter = 0;1049 float filter_scale;10501051 STBIR__DEBUG_ASSERT(in_last_pixel - in_first_pixel <= (int)ceil(stbir__filter_info_table[filter].support(1/scale) * 2)); // Taken directly from stbir__get_coefficient_width() which we can't call because we don't know if we're horizontal or vertical.10521053 contributor->n0 = in_first_pixel;1054 contributor->n1 = in_last_pixel;10551056 STBIR__DEBUG_ASSERT(contributor->n1 >= contributor->n0);10571058 for (i = 0; i <= in_last_pixel - in_first_pixel; i++)1059 {1060 float in_pixel_center = (float)(i + in_first_pixel) + 0.5f;1061 coefficient_group[i] = stbir__filter_info_table[filter].kernel(in_center_of_out - in_pixel_center, 1 / scale);10621063 // If the coefficient is zero, skip it. (Don't do the <0 check here, we want the influence of those outside pixels.)1064 if (i == 0 && !coefficient_group[i])1065 {1066 contributor->n0 = ++in_first_pixel;1067 i--;1068 continue;1069 }10701071 total_filter += coefficient_group[i];1072 }10731074 STBIR__DEBUG_ASSERT(stbir__filter_info_table[filter].kernel((float)(in_last_pixel + 1) + 0.5f - in_center_of_out, 1/scale) == 0);10751076 STBIR__DEBUG_ASSERT(total_filter > 0.9);1077 STBIR__DEBUG_ASSERT(total_filter < 1.1f); // Make sure it's not way off.10781079 // Make sure the sum of all coefficients is 1.1080 filter_scale = 1 / total_filter;10811082 for (i = 0; i <= in_last_pixel - in_first_pixel; i++)1083 coefficient_group[i] *= filter_scale;10841085 for (i = in_last_pixel - in_first_pixel; i >= 0; i--)1086 {1087 if (coefficient_group[i])1088 break;10891090 // This line has no weight. We can skip it.1091 contributor->n1 = contributor->n0 + i - 1;1092 }1093}10941095static void stbir__calculate_coefficients_downsample(stbir__info* stbir_info, stbir_filter filter, float scale_ratio, int out_first_pixel, int out_last_pixel, float out_center_of_in, stbir__contributors* contributor, float* coefficient_group)1096{1097 int i;10981099 STBIR__DEBUG_ASSERT(out_last_pixel - out_first_pixel <= (int)ceil(stbir__filter_info_table[filter].support(scale_ratio) * 2)); // Taken directly from stbir__get_coefficient_width() which we can't call because we don't know if we're horizontal or vertical.11001101 contributor->n0 = out_first_pixel;1102 contributor->n1 = out_last_pixel;11031104 STBIR__DEBUG_ASSERT(contributor->n1 >= contributor->n0);11051106 for (i = 0; i <= out_last_pixel - out_first_pixel; i++)1107 {1108 float out_pixel_center = (float)(i + out_first_pixel) + 0.5f;1109 float x = out_pixel_center - out_center_of_in;1110 coefficient_group[i] = stbir__filter_info_table[filter].kernel(x, scale_ratio) * scale_ratio;1111 }11121113 STBIR__DEBUG_ASSERT(stbir__filter_info_table[filter].kernel((float)(out_last_pixel + 1) + 0.5f - out_center_of_in, scale_ratio) == 0);11141115 for (i = out_last_pixel - out_first_pixel; i >= 0; i--)1116 {1117 if (coefficient_group[i])1118 break;11191120 // This line has no weight. We can skip it.1121 contributor->n1 = contributor->n0 + i - 1;1122 }1123}11241125static void stbir__normalize_downsample_coefficients(stbir__info* stbir_info, stbir__contributors* contributors, float* coefficients, stbir_filter filter, float scale_ratio, float shift, int input_size, int output_size)1126{1127 int num_contributors = stbir__get_contributors(scale_ratio, filter, input_size, output_size);1128 int num_coefficients = stbir__get_coefficient_width(filter, scale_ratio);1129 int i, j;1130 int skip;11311132 for (i = 0; i < output_size; i++)1133 {1134 float scale;1135 float total = 0;11361137 for (j = 0; j < num_contributors; j++)1138 {1139 if (i >= contributors[j].n0 && i <= contributors[j].n1)1140 {1141 float coefficient = *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i - contributors[j].n0);1142 total += coefficient;1143 }1144 else if (i < contributors[j].n0)1145 break;1146 }11471148 STBIR__DEBUG_ASSERT(total > 0.9f);1149 STBIR__DEBUG_ASSERT(total < 1.1f);11501151 scale = 1 / total;11521153 for (j = 0; j < num_contributors; j++)1154 {1155 if (i >= contributors[j].n0 && i <= contributors[j].n1)1156 *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i - contributors[j].n0) *= scale;1157 else if (i < contributors[j].n0)1158 break;1159 }1160 }11611162 // Optimize: Skip zero coefficients and contributions outside of image bounds.1163 // Do this after normalizing because normalization depends on the n0/n1 values.1164 for (j = 0; j < num_contributors; j++)1165 {1166 int range, max, width;11671168 skip = 0;1169 while (*stbir__get_coefficient(coefficients, filter, scale_ratio, j, skip) == 0)1170 skip++;11711172 contributors[j].n0 += skip;11731174 while (contributors[j].n0 < 0)1175 {1176 contributors[j].n0++;1177 skip++;1178 }11791180 range = contributors[j].n1 - contributors[j].n0 + 1;1181 max = stbir__min(num_coefficients, range);11821183 width = stbir__get_coefficient_width(filter, scale_ratio);1184 for (i = 0; i < max; i++)1185 {1186 if (i + skip >= width)1187 break;11881189 *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i) = *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i + skip);1190 }11911192 continue;1193 }11941195 // Using min to avoid writing into invalid pixels.1196 for (i = 0; i < num_contributors; i++)1197 contributors[i].n1 = stbir__min(contributors[i].n1, output_size - 1);1198}11991200// Each scan line uses the same kernel values so we should calculate the kernel1201// values once and then we can use them for every scan line.1202static void stbir__calculate_filters(stbir__info* stbir_info, stbir__contributors* contributors, float* coefficients, stbir_filter filter, float scale_ratio, float shift, int input_size, int output_size)1203{1204 int n;1205 int total_contributors = stbir__get_contributors(scale_ratio, filter, input_size, output_size);12061207 if (stbir__use_upsampling(scale_ratio))1208 {1209 float out_pixels_radius = stbir__filter_info_table[filter].support(1 / scale_ratio) * scale_ratio;12101211 // Looping through out pixels1212 for (n = 0; n < total_contributors; n++)1213 {1214 float in_center_of_out; // Center of the current out pixel in the in pixel space1215 int in_first_pixel, in_last_pixel;12161217 stbir__calculate_sample_range_upsample(n, out_pixels_radius, scale_ratio, shift, &in_first_pixel, &in_last_pixel, &in_center_of_out);12181219 stbir__calculate_coefficients_upsample(stbir_info, filter, scale_ratio, in_first_pixel, in_last_pixel, in_center_of_out, stbir__get_contributor(contributors, n), stbir__get_coefficient(coefficients, filter, scale_ratio, n, 0));1220 }1221 }1222 else1223 {1224 float in_pixels_radius = stbir__filter_info_table[filter].support(scale_ratio) / scale_ratio;12251226 // Looping through in pixels1227 for (n = 0; n < total_contributors; n++)1228 {1229 float out_center_of_in; // Center of the current out pixel in the in pixel space1230 int out_first_pixel, out_last_pixel;1231 int n_adjusted = n - stbir__get_filter_pixel_margin(filter, scale_ratio);12321233 stbir__calculate_sample_range_downsample(n_adjusted, in_pixels_radius, scale_ratio, shift, &out_first_pixel, &out_last_pixel, &out_center_of_in);12341235 stbir__calculate_coefficients_downsample(stbir_info, filter, scale_ratio, out_first_pixel, out_last_pixel, out_center_of_in, stbir__get_contributor(contributors, n), stbir__get_coefficient(coefficients, filter, scale_ratio, n, 0));1236 }12371238 stbir__normalize_downsample_coefficients(stbir_info, contributors, coefficients, filter, scale_ratio, shift, input_size, output_size);1239 }1240}12411242static float* stbir__get_decode_buffer(stbir__info* stbir_info)1243{1244 // The 0 index of the decode buffer starts after the margin. This makes1245 // it okay to use negative indexes on the decode buffer.1246 return &stbir_info->decode_buffer[stbir_info->horizontal_filter_pixel_margin * stbir_info->channels];1247}12481249#define STBIR__DECODE(type, colorspace) ((type) * (STBIR_MAX_COLORSPACES) + (colorspace))12501251static void stbir__decode_scanline(stbir__info* stbir_info, int n)1252{1253 int c;1254 int channels = stbir_info->channels;1255 int alpha_channel = stbir_info->alpha_channel;1256 int type = stbir_info->type;1257 int colorspace = stbir_info->colorspace;1258 int input_w = stbir_info->input_w;1259 int input_stride_bytes = stbir_info->input_stride_bytes;1260 float* decode_buffer = stbir__get_decode_buffer(stbir_info);1261 stbir_edge edge_horizontal = stbir_info->edge_horizontal;1262 stbir_edge edge_vertical = stbir_info->edge_vertical;1263 int in_buffer_row_offset = stbir__edge_wrap(edge_vertical, n, stbir_info->input_h) * input_stride_bytes;1264 const void* input_data = (char *) stbir_info->input_data + in_buffer_row_offset;1265 int max_x = input_w + stbir_info->horizontal_filter_pixel_margin;1266 int decode = STBIR__DECODE(type, colorspace);12671268 int x = -stbir_info->horizontal_filter_pixel_margin;12691270 // special handling for STBIR_EDGE_ZERO because it needs to return an item that doesn't appear in the input,1271 // and we want to avoid paying overhead on every pixel if not STBIR_EDGE_ZERO1272 if (edge_vertical == STBIR_EDGE_ZERO && (n < 0 || n >= stbir_info->input_h))1273 {1274 for (; x < max_x; x++)1275 for (c = 0; c < channels; c++)1276 decode_buffer[x*channels + c] = 0;1277 return;1278 }12791280 switch (decode)1281 {1282 case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_LINEAR):1283 for (; x < max_x; x++)1284 {1285 int decode_pixel_index = x * channels;1286 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1287 for (c = 0; c < channels; c++)1288 decode_buffer[decode_pixel_index + c] = ((float)((const unsigned char*)input_data)[input_pixel_index + c]) / 255;1289 }1290 break;12911292 case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_SRGB):1293 for (; x < max_x; x++)1294 {1295 int decode_pixel_index = x * channels;1296 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1297 for (c = 0; c < channels; c++)1298 decode_buffer[decode_pixel_index + c] = stbir__srgb_uchar_to_linear_float[((const unsigned char*)input_data)[input_pixel_index + c]];12991300 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1301 decode_buffer[decode_pixel_index + alpha_channel] = ((float)((const unsigned char*)input_data)[input_pixel_index + alpha_channel]) / 255;1302 }1303 break;13041305 case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR):1306 for (; x < max_x; x++)1307 {1308 int decode_pixel_index = x * channels;1309 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1310 for (c = 0; c < channels; c++)1311 decode_buffer[decode_pixel_index + c] = ((float)((const unsigned short*)input_data)[input_pixel_index + c]) / 65535;1312 }1313 break;13141315 case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB):1316 for (; x < max_x; x++)1317 {1318 int decode_pixel_index = x * channels;1319 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1320 for (c = 0; c < channels; c++)1321 decode_buffer[decode_pixel_index + c] = stbir__srgb_to_linear(((float)((const unsigned short*)input_data)[input_pixel_index + c]) / 65535);13221323 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1324 decode_buffer[decode_pixel_index + alpha_channel] = ((float)((const unsigned short*)input_data)[input_pixel_index + alpha_channel]) / 65535;1325 }1326 break;13271328 case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR):1329 for (; x < max_x; x++)1330 {1331 int decode_pixel_index = x * channels;1332 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1333 for (c = 0; c < channels; c++)1334 decode_buffer[decode_pixel_index + c] = (float)(((double)((const unsigned int*)input_data)[input_pixel_index + c]) / 4294967295);1335 }1336 break;13371338 case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB):1339 for (; x < max_x; x++)1340 {1341 int decode_pixel_index = x * channels;1342 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1343 for (c = 0; c < channels; c++)1344 decode_buffer[decode_pixel_index + c] = stbir__srgb_to_linear((float)(((double)((const unsigned int*)input_data)[input_pixel_index + c]) / 4294967295));13451346 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1347 decode_buffer[decode_pixel_index + alpha_channel] = (float)(((double)((const unsigned int*)input_data)[input_pixel_index + alpha_channel]) / 4294967295);1348 }1349 break;13501351 case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR):1352 for (; x < max_x; x++)1353 {1354 int decode_pixel_index = x * channels;1355 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1356 for (c = 0; c < channels; c++)1357 decode_buffer[decode_pixel_index + c] = ((const float*)input_data)[input_pixel_index + c];1358 }1359 break;13601361 case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB):1362 for (; x < max_x; x++)1363 {1364 int decode_pixel_index = x * channels;1365 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1366 for (c = 0; c < channels; c++)1367 decode_buffer[decode_pixel_index + c] = stbir__srgb_to_linear(((const float*)input_data)[input_pixel_index + c]);13681369 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1370 decode_buffer[decode_pixel_index + alpha_channel] = ((const float*)input_data)[input_pixel_index + alpha_channel];1371 }13721373 break;13741375 default:1376 STBIR__UNIMPLEMENTED("Unknown type/colorspace/channels combination.");1377 break;1378 }13791380 if (!(stbir_info->flags & STBIR_FLAG_ALPHA_PREMULTIPLIED))1381 {1382 for (x = -stbir_info->horizontal_filter_pixel_margin; x < max_x; x++)1383 {1384 int decode_pixel_index = x * channels;13851386 // If the alpha value is 0 it will clobber the color values. Make sure it's not.1387 float alpha = decode_buffer[decode_pixel_index + alpha_channel];1388#ifndef STBIR_NO_ALPHA_EPSILON1389 if (stbir_info->type != STBIR_TYPE_FLOAT) {1390 alpha += STBIR_ALPHA_EPSILON;1391 decode_buffer[decode_pixel_index + alpha_channel] = alpha;1392 }1393#endif1394 for (c = 0; c < channels; c++)1395 {1396 if (c == alpha_channel)1397 continue;13981399 decode_buffer[decode_pixel_index + c] *= alpha;1400 }1401 }1402 }14031404 if (edge_horizontal == STBIR_EDGE_ZERO)1405 {1406 for (x = -stbir_info->horizontal_filter_pixel_margin; x < 0; x++)1407 {1408 for (c = 0; c < channels; c++)1409 decode_buffer[x*channels + c] = 0;1410 }1411 for (x = input_w; x < max_x; x++)1412 {1413 for (c = 0; c < channels; c++)1414 decode_buffer[x*channels + c] = 0;1415 }1416 }1417}14181419static float* stbir__get_ring_buffer_entry(float* ring_buffer, int index, int ring_buffer_length)1420{1421 return &ring_buffer[index * ring_buffer_length];1422}14231424static float* stbir__add_empty_ring_buffer_entry(stbir__info* stbir_info, int n)1425{1426 int ring_buffer_index;1427 float* ring_buffer;14281429 if (stbir_info->ring_buffer_begin_index < 0)1430 {1431 ring_buffer_index = stbir_info->ring_buffer_begin_index = 0;1432 stbir_info->ring_buffer_first_scanline = n;1433 }1434 else1435 {1436 ring_buffer_index = (stbir_info->ring_buffer_begin_index + (stbir_info->ring_buffer_last_scanline - stbir_info->ring_buffer_first_scanline) + 1) % stbir_info->vertical_filter_pixel_width;1437 STBIR__DEBUG_ASSERT(ring_buffer_index != stbir_info->ring_buffer_begin_index);1438 }14391440 ring_buffer = stbir__get_ring_buffer_entry(stbir_info->ring_buffer, ring_buffer_index, stbir_info->ring_buffer_length_bytes / sizeof(float));1441 memset(ring_buffer, 0, stbir_info->ring_buffer_length_bytes);14421443 stbir_info->ring_buffer_last_scanline = n;14441445 return ring_buffer;1446}144714481449static void stbir__resample_horizontal_upsample(stbir__info* stbir_info, int n, float* output_buffer)1450{1451 int x, k;1452 int output_w = stbir_info->output_w;1453 int kernel_pixel_width = stbir_info->horizontal_filter_pixel_width;1454 int channels = stbir_info->channels;1455 float* decode_buffer = stbir__get_decode_buffer(stbir_info);1456 stbir__contributors* horizontal_contributors = stbir_info->horizontal_contributors;1457 float* horizontal_coefficients = stbir_info->horizontal_coefficients;1458 int coefficient_width = stbir_info->horizontal_coefficient_width;14591460 for (x = 0; x < output_w; x++)1461 {1462 int n0 = horizontal_contributors[x].n0;1463 int n1 = horizontal_contributors[x].n1;14641465 int out_pixel_index = x * channels;1466 int coefficient_group = coefficient_width * x;1467 int coefficient_counter = 0;14681469 STBIR__DEBUG_ASSERT(n1 >= n0);1470 STBIR__DEBUG_ASSERT(n0 >= -stbir_info->horizontal_filter_pixel_margin);1471 STBIR__DEBUG_ASSERT(n1 >= -stbir_info->horizontal_filter_pixel_margin);1472 STBIR__DEBUG_ASSERT(n0 < stbir_info->input_w + stbir_info->horizontal_filter_pixel_margin);1473 STBIR__DEBUG_ASSERT(n1 < stbir_info->input_w + stbir_info->horizontal_filter_pixel_margin);14741475 switch (channels) {1476 case 1:1477 for (k = n0; k <= n1; k++)1478 {1479 int in_pixel_index = k * 1;1480 float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++];1481 STBIR__DEBUG_ASSERT(coefficient != 0);1482 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1483 }1484 break;1485 case 2:1486 for (k = n0; k <= n1; k++)1487 {1488 int in_pixel_index = k * 2;1489 float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++];1490 STBIR__DEBUG_ASSERT(coefficient != 0);1491 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1492 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1493 }1494 break;1495 case 3:1496 for (k = n0; k <= n1; k++)1497 {1498 int in_pixel_index = k * 3;1499 float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++];1500 STBIR__DEBUG_ASSERT(coefficient != 0);1501 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1502 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1503 output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient;1504 }1505 break;1506 case 4:1507 for (k = n0; k <= n1; k++)1508 {1509 int in_pixel_index = k * 4;1510 float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++];1511 STBIR__DEBUG_ASSERT(coefficient != 0);1512 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1513 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1514 output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient;1515 output_buffer[out_pixel_index + 3] += decode_buffer[in_pixel_index + 3] * coefficient;1516 }1517 break;1518 default:1519 for (k = n0; k <= n1; k++)1520 {1521 int in_pixel_index = k * channels;1522 float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++];1523 int c;1524 STBIR__DEBUG_ASSERT(coefficient != 0);1525 for (c = 0; c < channels; c++)1526 output_buffer[out_pixel_index + c] += decode_buffer[in_pixel_index + c] * coefficient;1527 }1528 break;1529 }1530 }1531}15321533static void stbir__resample_horizontal_downsample(stbir__info* stbir_info, int n, float* output_buffer)1534{1535 int x, k;1536 int input_w = stbir_info->input_w;1537 int output_w = stbir_info->output_w;1538 int kernel_pixel_width = stbir_info->horizontal_filter_pixel_width;1539 int channels = stbir_info->channels;1540 float* decode_buffer = stbir__get_decode_buffer(stbir_info);1541 stbir__contributors* horizontal_contributors = stbir_info->horizontal_contributors;1542 float* horizontal_coefficients = stbir_info->horizontal_coefficients;1543 int coefficient_width = stbir_info->horizontal_coefficient_width;1544 int filter_pixel_margin = stbir_info->horizontal_filter_pixel_margin;1545 int max_x = input_w + filter_pixel_margin * 2;15461547 STBIR__DEBUG_ASSERT(!stbir__use_width_upsampling(stbir_info));15481549 switch (channels) {1550 case 1:1551 for (x = 0; x < max_x; x++)1552 {1553 int n0 = horizontal_contributors[x].n0;1554 int n1 = horizontal_contributors[x].n1;15551556 int in_x = x - filter_pixel_margin;1557 int in_pixel_index = in_x * 1;1558 int max_n = n1;1559 int coefficient_group = coefficient_width * x;15601561 for (k = n0; k <= max_n; k++)1562 {1563 int out_pixel_index = k * 1;1564 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1565 STBIR__DEBUG_ASSERT(coefficient != 0);1566 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1567 }1568 }1569 break;15701571 case 2:1572 for (x = 0; x < max_x; x++)1573 {1574 int n0 = horizontal_contributors[x].n0;1575 int n1 = horizontal_contributors[x].n1;15761577 int in_x = x - filter_pixel_margin;1578 int in_pixel_index = in_x * 2;1579 int max_n = n1;1580 int coefficient_group = coefficient_width * x;15811582 for (k = n0; k <= max_n; k++)1583 {1584 int out_pixel_index = k * 2;1585 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1586 STBIR__DEBUG_ASSERT(coefficient != 0);1587 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1588 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1589 }1590 }1591 break;15921593 case 3:1594 for (x = 0; x < max_x; x++)1595 {1596 int n0 = horizontal_contributors[x].n0;1597 int n1 = horizontal_contributors[x].n1;15981599 int in_x = x - filter_pixel_margin;1600 int in_pixel_index = in_x * 3;1601 int max_n = n1;1602 int coefficient_group = coefficient_width * x;16031604 for (k = n0; k <= max_n; k++)1605 {1606 int out_pixel_index = k * 3;1607 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1608 STBIR__DEBUG_ASSERT(coefficient != 0);1609 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1610 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1611 output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient;1612 }1613 }1614 break;16151616 case 4:1617 for (x = 0; x < max_x; x++)1618 {1619 int n0 = horizontal_contributors[x].n0;1620 int n1 = horizontal_contributors[x].n1;16211622 int in_x = x - filter_pixel_margin;1623 int in_pixel_index = in_x * 4;1624 int max_n = n1;1625 int coefficient_group = coefficient_width * x;16261627 for (k = n0; k <= max_n; k++)1628 {1629 int out_pixel_index = k * 4;1630 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1631 STBIR__DEBUG_ASSERT(coefficient != 0);1632 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1633 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1634 output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient;1635 output_buffer[out_pixel_index + 3] += decode_buffer[in_pixel_index + 3] * coefficient;1636 }1637 }1638 break;16391640 default:1641 for (x = 0; x < max_x; x++)1642 {1643 int n0 = horizontal_contributors[x].n0;1644 int n1 = horizontal_contributors[x].n1;16451646 int in_x = x - filter_pixel_margin;1647 int in_pixel_index = in_x * channels;1648 int max_n = n1;1649 int coefficient_group = coefficient_width * x;16501651 for (k = n0; k <= max_n; k++)1652 {1653 int c;1654 int out_pixel_index = k * channels;1655 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1656 STBIR__DEBUG_ASSERT(coefficient != 0);1657 for (c = 0; c < channels; c++)1658 output_buffer[out_pixel_index + c] += decode_buffer[in_pixel_index + c] * coefficient;1659 }1660 }1661 break;1662 }1663}16641665static void stbir__decode_and_resample_upsample(stbir__info* stbir_info, int n)1666{1667 // Decode the nth scanline from the source image into the decode buffer.1668 stbir__decode_scanline(stbir_info, n);16691670 // Now resample it into the ring buffer.1671 if (stbir__use_width_upsampling(stbir_info))1672 stbir__resample_horizontal_upsample(stbir_info, n, stbir__add_empty_ring_buffer_entry(stbir_info, n));1673 else1674 stbir__resample_horizontal_downsample(stbir_info, n, stbir__add_empty_ring_buffer_entry(stbir_info, n));16751676 // Now it's sitting in the ring buffer ready to be used as source for the vertical sampling.1677}16781679static void stbir__decode_and_resample_downsample(stbir__info* stbir_info, int n)1680{1681 // Decode the nth scanline from the source image into the decode buffer.1682 stbir__decode_scanline(stbir_info, n);16831684 memset(stbir_info->horizontal_buffer, 0, stbir_info->output_w * stbir_info->channels * sizeof(float));16851686 // Now resample it into the horizontal buffer.1687 if (stbir__use_width_upsampling(stbir_info))1688 stbir__resample_horizontal_upsample(stbir_info, n, stbir_info->horizontal_buffer);1689 else1690 stbir__resample_horizontal_downsample(stbir_info, n, stbir_info->horizontal_buffer);16911692 // Now it's sitting in the horizontal buffer ready to be distributed into the ring buffers.1693}16941695// Get the specified scan line from the ring buffer.1696static float* stbir__get_ring_buffer_scanline(int get_scanline, float* ring_buffer, int begin_index, int first_scanline, int ring_buffer_size, int ring_buffer_length)1697{1698 int ring_buffer_index = (begin_index + (get_scanline - first_scanline)) % ring_buffer_size;1699 return stbir__get_ring_buffer_entry(ring_buffer, ring_buffer_index, ring_buffer_length);1700}170117021703static void stbir__encode_scanline(stbir__info* stbir_info, int num_pixels, void *output_buffer, float *encode_buffer, int channels, int alpha_channel, int decode)1704{1705 int x;1706 int n;1707 int num_nonalpha;1708 stbir_uint16 nonalpha[STBIR_MAX_CHANNELS];17091710 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_PREMULTIPLIED))1711 {1712 for (x=0; x < num_pixels; ++x)1713 {1714 int pixel_index = x*channels;17151716 float alpha = encode_buffer[pixel_index + alpha_channel];1717 float reciprocal_alpha = alpha ? 1.0f / alpha : 0;17181719 // unrolling this produced a 1% slowdown upscaling a large RGBA linear-space image on my machine - stb1720 for (n = 0; n < channels; n++)1721 if (n != alpha_channel)1722 encode_buffer[pixel_index + n] *= reciprocal_alpha;17231724 // We added in a small epsilon to prevent the color channel from being deleted with zero alpha.1725 // Because we only add it for integer types, it will automatically be discarded on integer1726 // conversion, so we don't need to subtract it back out (which would be problematic for1727 // numeric precision reasons).1728 }1729 }17301731 // build a table of all channels that need colorspace correction, so1732 // we don't perform colorspace correction on channels that don't need it.1733 for (x=0, num_nonalpha=0; x < channels; ++x)1734 if (x != alpha_channel || (stbir_info->flags & STBIR_FLAG_ALPHA_USES_COLORSPACE))1735 nonalpha[num_nonalpha++] = x;17361737 #define STBIR__ROUND_INT(f) ((int) ((f)+0.5))1738 #define STBIR__ROUND_UINT(f) ((stbir_uint32) ((f)+0.5))17391740 #ifdef STBIR__SATURATE_INT1741 #define STBIR__ENCODE_LINEAR8(f) stbir__saturate8 (STBIR__ROUND_INT((f) * 255 ))1742 #define STBIR__ENCODE_LINEAR16(f) stbir__saturate16(STBIR__ROUND_INT((f) * 65535))1743 #else1744 #define STBIR__ENCODE_LINEAR8(f) (unsigned char ) STBIR__ROUND_INT(stbir__saturate(f) * 255 )1745 #define STBIR__ENCODE_LINEAR16(f) (unsigned short) STBIR__ROUND_INT(stbir__saturate(f) * 65535)1746 #endif17471748 switch (decode)1749 {1750 case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_LINEAR):1751 for (x=0; x < num_pixels; ++x)1752 {1753 int pixel_index = x*channels;17541755 for (n = 0; n < channels; n++)1756 {1757 int index = pixel_index + n;1758 ((unsigned char*)output_buffer)[index] = STBIR__ENCODE_LINEAR8(encode_buffer[index]);1759 }1760 }1761 break;17621763 case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_SRGB):1764 for (x=0; x < num_pixels; ++x)1765 {1766 int pixel_index = x*channels;17671768 for (n = 0; n < num_nonalpha; n++)1769 {1770 int index = pixel_index + nonalpha[n];1771 ((unsigned char*)output_buffer)[index] = stbir__linear_to_srgb_uchar(encode_buffer[index]);1772 }17731774 if (!(stbir_info->flags & STBIR_FLAG_ALPHA_USES_COLORSPACE))1775 ((unsigned char *)output_buffer)[pixel_index + alpha_channel] = STBIR__ENCODE_LINEAR8(encode_buffer[pixel_index+alpha_channel]);1776 }1777 break;17781779 case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR):1780 for (x=0; x < num_pixels; ++x)1781 {1782 int pixel_index = x*channels;17831784 for (n = 0; n < channels; n++)1785 {1786 int index = pixel_index + n;1787 ((unsigned short*)output_buffer)[index] = STBIR__ENCODE_LINEAR16(encode_buffer[index]);1788 }1789 }1790 break;17911792 case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB):1793 for (x=0; x < num_pixels; ++x)1794 {1795 int pixel_index = x*channels;17961797 for (n = 0; n < num_nonalpha; n++)1798 {1799 int index = pixel_index + nonalpha[n];1800 ((unsigned short*)output_buffer)[index] = (unsigned short)STBIR__ROUND_INT(stbir__linear_to_srgb(stbir__saturate(encode_buffer[index])) * 65535);1801 }18021803 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1804 ((unsigned short*)output_buffer)[pixel_index + alpha_channel] = STBIR__ENCODE_LINEAR16(encode_buffer[pixel_index + alpha_channel]);1805 }18061807 break;18081809 case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR):1810 for (x=0; x < num_pixels; ++x)1811 {1812 int pixel_index = x*channels;18131814 for (n = 0; n < channels; n++)1815 {1816 int index = pixel_index + n;1817 ((unsigned int*)output_buffer)[index] = (unsigned int)STBIR__ROUND_UINT(((double)stbir__saturate(encode_buffer[index])) * 4294967295);1818 }1819 }1820 break;18211822 case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB):1823 for (x=0; x < num_pixels; ++x)1824 {1825 int pixel_index = x*channels;18261827 for (n = 0; n < num_nonalpha; n++)1828 {1829 int index = pixel_index + nonalpha[n];1830 ((unsigned int*)output_buffer)[index] = (unsigned int)STBIR__ROUND_UINT(((double)stbir__linear_to_srgb(stbir__saturate(encode_buffer[index]))) * 4294967295);1831 }18321833 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1834 ((unsigned int*)output_buffer)[pixel_index + alpha_channel] = (unsigned int)STBIR__ROUND_INT(((double)stbir__saturate(encode_buffer[pixel_index + alpha_channel])) * 4294967295);1835 }1836 break;18371838 case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR):1839 for (x=0; x < num_pixels; ++x)1840 {1841 int pixel_index = x*channels;18421843 for (n = 0; n < channels; n++)1844 {1845 int index = pixel_index + n;1846 ((float*)output_buffer)[index] = encode_buffer[index];1847 }1848 }1849 break;18501851 case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB):1852 for (x=0; x < num_pixels; ++x)1853 {1854 int pixel_index = x*channels;18551856 for (n = 0; n < num_nonalpha; n++)1857 {1858 int index = pixel_index + nonalpha[n];1859 ((float*)output_buffer)[index] = stbir__linear_to_srgb(encode_buffer[index]);1860 }18611862 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1863 ((float*)output_buffer)[pixel_index + alpha_channel] = encode_buffer[pixel_index + alpha_channel];1864 }1865 break;18661867 default:1868 STBIR__UNIMPLEMENTED("Unknown type/colorspace/channels combination.");1869 break;1870 }1871}18721873static void stbir__resample_vertical_upsample(stbir__info* stbir_info, int n, int in_first_scanline, int in_last_scanline, float in_center_of_out)1874{1875 int x, k;1876 int output_w = stbir_info->output_w;1877 stbir__contributors* vertical_contributors = stbir_info->vertical_contributors;1878 float* vertical_coefficients = stbir_info->vertical_coefficients;1879 int channels = stbir_info->channels;1880 int alpha_channel = stbir_info->alpha_channel;1881 int type = stbir_info->type;1882 int colorspace = stbir_info->colorspace;1883 int kernel_pixel_width = stbir_info->vertical_filter_pixel_width;1884 void* output_data = stbir_info->output_data;1885 float* encode_buffer = stbir_info->encode_buffer;1886 int decode = STBIR__DECODE(type, colorspace);1887 int coefficient_width = stbir_info->vertical_coefficient_width;1888 int coefficient_counter;1889 int contributor = n;18901891 float* ring_buffer = stbir_info->ring_buffer;1892 int ring_buffer_begin_index = stbir_info->ring_buffer_begin_index;1893 int ring_buffer_first_scanline = stbir_info->ring_buffer_first_scanline;1894 int ring_buffer_last_scanline = stbir_info->ring_buffer_last_scanline;1895 int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float);18961897 int n0,n1, output_row_start;1898 int coefficient_group = coefficient_width * contributor;18991900 n0 = vertical_contributors[contributor].n0;1901 n1 = vertical_contributors[contributor].n1;19021903 output_row_start = n * stbir_info->output_stride_bytes;19041905 STBIR__DEBUG_ASSERT(stbir__use_height_upsampling(stbir_info));19061907 memset(encode_buffer, 0, output_w * sizeof(float) * channels);19081909 // I tried reblocking this for better cache usage of encode_buffer1910 // (using x_outer, k, x_inner), but it lost speed. -- stb19111912 coefficient_counter = 0;1913 switch (channels) {1914 case 1:1915 for (k = n0; k <= n1; k++)1916 {1917 int coefficient_index = coefficient_counter++;1918 float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, kernel_pixel_width, ring_buffer_length);1919 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];1920 for (x = 0; x < output_w; ++x)1921 {1922 int in_pixel_index = x * 1;1923 encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;1924 }1925 }1926 break;1927 case 2:1928 for (k = n0; k <= n1; k++)1929 {1930 int coefficient_index = coefficient_counter++;1931 float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, kernel_pixel_width, ring_buffer_length);1932 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];1933 for (x = 0; x < output_w; ++x)1934 {1935 int in_pixel_index = x * 2;1936 encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;1937 encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient;1938 }1939 }1940 break;1941 case 3:1942 for (k = n0; k <= n1; k++)1943 {1944 int coefficient_index = coefficient_counter++;1945 float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, kernel_pixel_width, ring_buffer_length);1946 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];1947 for (x = 0; x < output_w; ++x)1948 {1949 int in_pixel_index = x * 3;1950 encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;1951 encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient;1952 encode_buffer[in_pixel_index + 2] += ring_buffer_entry[in_pixel_index + 2] * coefficient;1953 }1954 }1955 break;1956 case 4:1957 for (k = n0; k <= n1; k++)1958 {1959 int coefficient_index = coefficient_counter++;1960 float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, kernel_pixel_width, ring_buffer_length);1961 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];1962 for (x = 0; x < output_w; ++x)1963 {1964 int in_pixel_index = x * 4;1965 encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;1966 encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient;1967 encode_buffer[in_pixel_index + 2] += ring_buffer_entry[in_pixel_index + 2] * coefficient;1968 encode_buffer[in_pixel_index + 3] += ring_buffer_entry[in_pixel_index + 3] * coefficient;1969 }1970 }1971 break;1972 default:1973 for (k = n0; k <= n1; k++)1974 {1975 int coefficient_index = coefficient_counter++;1976 float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, kernel_pixel_width, ring_buffer_length);1977 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];1978 for (x = 0; x < output_w; ++x)1979 {1980 int in_pixel_index = x * channels;1981 int c;1982 for (c = 0; c < channels; c++)1983 encode_buffer[in_pixel_index + c] += ring_buffer_entry[in_pixel_index + c] * coefficient;1984 }1985 }1986 break;1987 }1988 stbir__encode_scanline(stbir_info, output_w, (char *) output_data + output_row_start, encode_buffer, channels, alpha_channel, decode);1989}19901991static void stbir__resample_vertical_downsample(stbir__info* stbir_info, int n, int in_first_scanline, int in_last_scanline, float in_center_of_out)1992{1993 int x, k;1994 int output_w = stbir_info->output_w;1995 int output_h = stbir_info->output_h;1996 stbir__contributors* vertical_contributors = stbir_info->vertical_contributors;1997 float* vertical_coefficients = stbir_info->vertical_coefficients;1998 int channels = stbir_info->channels;1999 int kernel_pixel_width = stbir_info->vertical_filter_pixel_width;2000 void* output_data = stbir_info->output_data;2001 float* horizontal_buffer = stbir_info->horizontal_buffer;2002 int coefficient_width = stbir_info->vertical_coefficient_width;2003 int contributor = n + stbir_info->vertical_filter_pixel_margin;20042005 float* ring_buffer = stbir_info->ring_buffer;2006 int ring_buffer_begin_index = stbir_info->ring_buffer_begin_index;2007 int ring_buffer_first_scanline = stbir_info->ring_buffer_first_scanline;2008 int ring_buffer_last_scanline = stbir_info->ring_buffer_last_scanline;2009 int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float);2010 int n0,n1;20112012 n0 = vertical_contributors[contributor].n0;2013 n1 = vertical_contributors[contributor].n1;20142015 STBIR__DEBUG_ASSERT(!stbir__use_height_upsampling(stbir_info));20162017 for (k = n0; k <= n1; k++)2018 {2019 int coefficient_index = k - n0;2020 int coefficient_group = coefficient_width * contributor;2021 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];20222023 float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, kernel_pixel_width, ring_buffer_length);20242025 switch (channels) {2026 case 1:2027 for (x = 0; x < output_w; x++)2028 {2029 int in_pixel_index = x * 1;2030 ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;2031 }2032 break;2033 case 2:2034 for (x = 0; x < output_w; x++)2035 {2036 int in_pixel_index = x * 2;2037 ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;2038 ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient;2039 }2040 break;2041 case 3:2042 for (x = 0; x < output_w; x++)2043 {2044 int in_pixel_index = x * 3;2045 ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;2046 ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient;2047 ring_buffer_entry[in_pixel_index + 2] += horizontal_buffer[in_pixel_index + 2] * coefficient;2048 }2049 break;2050 case 4:2051 for (x = 0; x < output_w; x++)2052 {2053 int in_pixel_index = x * 4;2054 ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;2055 ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient;2056 ring_buffer_entry[in_pixel_index + 2] += horizontal_buffer[in_pixel_index + 2] * coefficient;2057 ring_buffer_entry[in_pixel_index + 3] += horizontal_buffer[in_pixel_index + 3] * coefficient;2058 }2059 break;2060 default:2061 for (x = 0; x < output_w; x++)2062 {2063 int in_pixel_index = x * channels;20642065 int c;2066 for (c = 0; c < channels; c++)2067 ring_buffer_entry[in_pixel_index + c] += horizontal_buffer[in_pixel_index + c] * coefficient;2068 }2069 break;2070 }2071 }2072}20732074static void stbir__buffer_loop_upsample(stbir__info* stbir_info)2075{2076 int y;2077 float scale_ratio = stbir_info->vertical_scale;2078 float out_scanlines_radius = stbir__filter_info_table[stbir_info->vertical_filter].support(1/scale_ratio) * scale_ratio;20792080 STBIR__DEBUG_ASSERT(stbir__use_height_upsampling(stbir_info));20812082 for (y = 0; y < stbir_info->output_h; y++)2083 {2084 float in_center_of_out = 0; // Center of the current out scanline in the in scanline space2085 int in_first_scanline = 0, in_last_scanline = 0;20862087 stbir__calculate_sample_range_upsample(y, out_scanlines_radius, scale_ratio, stbir_info->vertical_shift, &in_first_scanline, &in_last_scanline, &in_center_of_out);20882089 STBIR__DEBUG_ASSERT(in_last_scanline - in_first_scanline <= stbir_info->vertical_filter_pixel_width);20902091 if (stbir_info->ring_buffer_begin_index >= 0)2092 {2093 // Get rid of whatever we don't need anymore.2094 while (in_first_scanline > stbir_info->ring_buffer_first_scanline)2095 {2096 if (stbir_info->ring_buffer_first_scanline == stbir_info->ring_buffer_last_scanline)2097 {2098 // We just popped the last scanline off the ring buffer.2099 // Reset it to the empty state.2100 stbir_info->ring_buffer_begin_index = -1;2101 stbir_info->ring_buffer_first_scanline = 0;2102 stbir_info->ring_buffer_last_scanline = 0;2103 break;2104 }2105 else2106 {2107 stbir_info->ring_buffer_first_scanline++;2108 stbir_info->ring_buffer_begin_index = (stbir_info->ring_buffer_begin_index + 1) % stbir_info->vertical_filter_pixel_width;2109 }2110 }2111 }21122113 // Load in new ones.2114 if (stbir_info->ring_buffer_begin_index < 0)2115 stbir__decode_and_resample_upsample(stbir_info, in_first_scanline);21162117 while (in_last_scanline > stbir_info->ring_buffer_last_scanline)2118 stbir__decode_and_resample_upsample(stbir_info, stbir_info->ring_buffer_last_scanline + 1);21192120 // Now all buffers should be ready to write a row of vertical sampling.2121 stbir__resample_vertical_upsample(stbir_info, y, in_first_scanline, in_last_scanline, in_center_of_out);21222123 STBIR_PROGRESS_REPORT((float)y / stbir_info->output_h);2124 }2125}21262127static void stbir__empty_ring_buffer(stbir__info* stbir_info, int first_necessary_scanline)2128{2129 int output_stride_bytes = stbir_info->output_stride_bytes;2130 int channels = stbir_info->channels;2131 int alpha_channel = stbir_info->alpha_channel;2132 int type = stbir_info->type;2133 int colorspace = stbir_info->colorspace;2134 int output_w = stbir_info->output_w;2135 void* output_data = stbir_info->output_data;2136 int decode = STBIR__DECODE(type, colorspace);21372138 float* ring_buffer = stbir_info->ring_buffer;2139 int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float);21402141 if (stbir_info->ring_buffer_begin_index >= 0)2142 {2143 // Get rid of whatever we don't need anymore.2144 while (first_necessary_scanline > stbir_info->ring_buffer_first_scanline)2145 {2146 if (stbir_info->ring_buffer_first_scanline >= 0 && stbir_info->ring_buffer_first_scanline < stbir_info->output_h)2147 {2148 int output_row_start = stbir_info->ring_buffer_first_scanline * output_stride_bytes;2149 float* ring_buffer_entry = stbir__get_ring_buffer_entry(ring_buffer, stbir_info->ring_buffer_begin_index, ring_buffer_length);2150 stbir__encode_scanline(stbir_info, output_w, (char *) output_data + output_row_start, ring_buffer_entry, channels, alpha_channel, decode);2151 STBIR_PROGRESS_REPORT((float)stbir_info->ring_buffer_first_scanline / stbir_info->output_h);2152 }21532154 if (stbir_info->ring_buffer_first_scanline == stbir_info->ring_buffer_last_scanline)2155 {2156 // We just popped the last scanline off the ring buffer.2157 // Reset it to the empty state.2158 stbir_info->ring_buffer_begin_index = -1;2159 stbir_info->ring_buffer_first_scanline = 0;2160 stbir_info->ring_buffer_last_scanline = 0;2161 break;2162 }2163 else2164 {2165 stbir_info->ring_buffer_first_scanline++;2166 stbir_info->ring_buffer_begin_index = (stbir_info->ring_buffer_begin_index + 1) % stbir_info->vertical_filter_pixel_width;2167 }2168 }2169 }2170}21712172static void stbir__buffer_loop_downsample(stbir__info* stbir_info)2173{2174 int y;2175 float scale_ratio = stbir_info->vertical_scale;2176 int output_h = stbir_info->output_h;2177 float in_pixels_radius = stbir__filter_info_table[stbir_info->vertical_filter].support(scale_ratio) / scale_ratio;2178 int pixel_margin = stbir_info->vertical_filter_pixel_margin;2179 int max_y = stbir_info->input_h + pixel_margin;21802181 STBIR__DEBUG_ASSERT(!stbir__use_height_upsampling(stbir_info));21822183 for (y = -pixel_margin; y < max_y; y++)2184 {2185 float out_center_of_in; // Center of the current out scanline in the in scanline space2186 int out_first_scanline, out_last_scanline;21872188 stbir__calculate_sample_range_downsample(y, in_pixels_radius, scale_ratio, stbir_info->vertical_shift, &out_first_scanline, &out_last_scanline, &out_center_of_in);21892190 STBIR__DEBUG_ASSERT(out_last_scanline - out_first_scanline <= stbir_info->vertical_filter_pixel_width);21912192 if (out_last_scanline < 0 || out_first_scanline >= output_h)2193 continue;21942195 stbir__empty_ring_buffer(stbir_info, out_first_scanline);21962197 stbir__decode_and_resample_downsample(stbir_info, y);21982199 // Load in new ones.2200 if (stbir_info->ring_buffer_begin_index < 0)2201 stbir__add_empty_ring_buffer_entry(stbir_info, out_first_scanline);22022203 while (out_last_scanline > stbir_info->ring_buffer_last_scanline)2204 stbir__add_empty_ring_buffer_entry(stbir_info, stbir_info->ring_buffer_last_scanline + 1);22052206 // Now the horizontal buffer is ready to write to all ring buffer rows.2207 stbir__resample_vertical_downsample(stbir_info, y, out_first_scanline, out_last_scanline, out_center_of_in);2208 }22092210 stbir__empty_ring_buffer(stbir_info, stbir_info->output_h);2211}22122213static void stbir__setup(stbir__info *info, int input_w, int input_h, int output_w, int output_h, int channels)2214{2215 info->input_w = input_w;2216 info->input_h = input_h;2217 info->output_w = output_w;2218 info->output_h = output_h;2219 info->channels = channels;2220}22212222static void stbir__calculate_transform(stbir__info *info, float s0, float t0, float s1, float t1, float *transform)2223{2224 info->s0 = s0;2225 info->t0 = t0;2226 info->s1 = s1;2227 info->t1 = t1;22282229 if (transform)2230 {2231 info->horizontal_scale = transform[0];2232 info->vertical_scale = transform[1];2233 info->horizontal_shift = transform[2];2234 info->vertical_shift = transform[3];2235 }2236 else2237 {2238 info->horizontal_scale = ((float)info->output_w / info->input_w) / (s1 - s0);2239 info->vertical_scale = ((float)info->output_h / info->input_h) / (t1 - t0);22402241 info->horizontal_shift = s0 * info->input_w / (s1 - s0);2242 info->vertical_shift = t0 * info->input_h / (t1 - t0);2243 }2244}22452246static void stbir__choose_filter(stbir__info *info, stbir_filter h_filter, stbir_filter v_filter)2247{2248 if (h_filter == 0)2249 h_filter = stbir__use_upsampling(info->horizontal_scale) ? STBIR_DEFAULT_FILTER_UPSAMPLE : STBIR_DEFAULT_FILTER_DOWNSAMPLE;2250 if (v_filter == 0)2251 v_filter = stbir__use_upsampling(info->vertical_scale) ? STBIR_DEFAULT_FILTER_UPSAMPLE : STBIR_DEFAULT_FILTER_DOWNSAMPLE;2252 info->horizontal_filter = h_filter;2253 info->vertical_filter = v_filter;2254}22552256static stbir_uint32 stbir__calculate_memory(stbir__info *info)2257{2258 int pixel_margin = stbir__get_filter_pixel_margin(info->horizontal_filter, info->horizontal_scale);2259 int filter_height = stbir__get_filter_pixel_width(info->vertical_filter, info->vertical_scale);22602261 info->horizontal_num_contributors = stbir__get_contributors(info->horizontal_scale, info->horizontal_filter, info->input_w, info->output_w);2262 info->vertical_num_contributors = stbir__get_contributors(info->vertical_scale , info->vertical_filter , info->input_h, info->output_h);22632264 info->horizontal_contributors_size = info->horizontal_num_contributors * sizeof(stbir__contributors);2265 info->horizontal_coefficients_size = stbir__get_total_horizontal_coefficients(info) * sizeof(float);2266 info->vertical_contributors_size = info->vertical_num_contributors * sizeof(stbir__contributors);2267 info->vertical_coefficients_size = stbir__get_total_vertical_coefficients(info) * sizeof(float);2268 info->decode_buffer_size = (info->input_w + pixel_margin * 2) * info->channels * sizeof(float);2269 info->horizontal_buffer_size = info->output_w * info->channels * sizeof(float);2270 info->ring_buffer_size = info->output_w * info->channels * filter_height * sizeof(float);2271 info->encode_buffer_size = info->output_w * info->channels * sizeof(float);22722273 STBIR_ASSERT(info->horizontal_filter != 0);2274 STBIR_ASSERT(info->horizontal_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late2275 STBIR_ASSERT(info->vertical_filter != 0);2276 STBIR_ASSERT(info->vertical_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late22772278 if (stbir__use_height_upsampling(info))2279 // The horizontal buffer is for when we're downsampling the height and we2280 // can't output the result of sampling the decode buffer directly into the2281 // ring buffers.2282 info->horizontal_buffer_size = 0;2283 else2284 // The encode buffer is to retain precision in the height upsampling method2285 // and isn't used when height downsampling.2286 info->encode_buffer_size = 0;22872288 return info->horizontal_contributors_size + info->horizontal_coefficients_size2289 + info->vertical_contributors_size + info->vertical_coefficients_size2290 + info->decode_buffer_size + info->horizontal_buffer_size2291 + info->ring_buffer_size + info->encode_buffer_size;2292}22932294static int stbir__resize_allocated(stbir__info *info,2295 const void* input_data, int input_stride_in_bytes,2296 void* output_data, int output_stride_in_bytes,2297 int alpha_channel, stbir_uint32 flags, stbir_datatype type,2298 stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace,2299 void* tempmem, size_t tempmem_size_in_bytes)2300{2301 size_t memory_required = stbir__calculate_memory(info);23022303 int width_stride_input = input_stride_in_bytes ? input_stride_in_bytes : info->channels * info->input_w * stbir__type_size[type];2304 int width_stride_output = output_stride_in_bytes ? output_stride_in_bytes : info->channels * info->output_w * stbir__type_size[type];23052306#ifdef STBIR_DEBUG_OVERWRITE_TEST2307#define OVERWRITE_ARRAY_SIZE 82308 unsigned char overwrite_output_before_pre[OVERWRITE_ARRAY_SIZE];2309 unsigned char overwrite_tempmem_before_pre[OVERWRITE_ARRAY_SIZE];2310 unsigned char overwrite_output_after_pre[OVERWRITE_ARRAY_SIZE];2311 unsigned char overwrite_tempmem_after_pre[OVERWRITE_ARRAY_SIZE];23122313 size_t begin_forbidden = width_stride_output * (info->output_h - 1) + info->output_w * info->channels * stbir__type_size[type];2314 memcpy(overwrite_output_before_pre, &((unsigned char*)output_data)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE);2315 memcpy(overwrite_output_after_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE);2316 memcpy(overwrite_tempmem_before_pre, &((unsigned char*)tempmem)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE);2317 memcpy(overwrite_tempmem_after_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE);2318#endif23192320 STBIR_ASSERT(info->channels >= 0);2321 STBIR_ASSERT(info->channels <= STBIR_MAX_CHANNELS);23222323 if (info->channels < 0 || info->channels > STBIR_MAX_CHANNELS)2324 return 0;23252326 STBIR_ASSERT(info->horizontal_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));2327 STBIR_ASSERT(info->vertical_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));23282329 if (info->horizontal_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table))2330 return 0;2331 if (info->vertical_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table))2332 return 0;23332334 if (alpha_channel < 0)2335 flags |= STBIR_FLAG_ALPHA_USES_COLORSPACE | STBIR_FLAG_ALPHA_PREMULTIPLIED;23362337 if (!(flags&STBIR_FLAG_ALPHA_USES_COLORSPACE) || !(flags&STBIR_FLAG_ALPHA_PREMULTIPLIED))2338 STBIR_ASSERT(alpha_channel >= 0 && alpha_channel < info->channels);23392340 if (alpha_channel >= info->channels)2341 return 0;23422343 STBIR_ASSERT(tempmem);23442345 if (!tempmem)2346 return 0;23472348 STBIR_ASSERT(tempmem_size_in_bytes >= memory_required);23492350 if (tempmem_size_in_bytes < memory_required)2351 return 0;23522353 memset(tempmem, 0, tempmem_size_in_bytes);23542355 info->input_data = input_data;2356 info->input_stride_bytes = width_stride_input;23572358 info->output_data = output_data;2359 info->output_stride_bytes = width_stride_output;23602361 info->alpha_channel = alpha_channel;2362 info->flags = flags;2363 info->type = type;2364 info->edge_horizontal = edge_horizontal;2365 info->edge_vertical = edge_vertical;2366 info->colorspace = colorspace;23672368 info->horizontal_coefficient_width = stbir__get_coefficient_width (info->horizontal_filter, info->horizontal_scale);2369 info->vertical_coefficient_width = stbir__get_coefficient_width (info->vertical_filter , info->vertical_scale );2370 info->horizontal_filter_pixel_width = stbir__get_filter_pixel_width (info->horizontal_filter, info->horizontal_scale);2371 info->vertical_filter_pixel_width = stbir__get_filter_pixel_width (info->vertical_filter , info->vertical_scale );2372 info->horizontal_filter_pixel_margin = stbir__get_filter_pixel_margin(info->horizontal_filter, info->horizontal_scale);2373 info->vertical_filter_pixel_margin = stbir__get_filter_pixel_margin(info->vertical_filter , info->vertical_scale );23742375 info->ring_buffer_length_bytes = info->output_w * info->channels * sizeof(float);2376 info->decode_buffer_pixels = info->input_w + info->horizontal_filter_pixel_margin * 2;23772378#define STBIR__NEXT_MEMPTR(current, newtype) (newtype*)(((unsigned char*)current) + current##_size)23792380 info->horizontal_contributors = (stbir__contributors *) tempmem;2381 info->horizontal_coefficients = STBIR__NEXT_MEMPTR(info->horizontal_contributors, float);2382 info->vertical_contributors = STBIR__NEXT_MEMPTR(info->horizontal_coefficients, stbir__contributors);2383 info->vertical_coefficients = STBIR__NEXT_MEMPTR(info->vertical_contributors, float);2384 info->decode_buffer = STBIR__NEXT_MEMPTR(info->vertical_coefficients, float);23852386 if (stbir__use_height_upsampling(info))2387 {2388 info->horizontal_buffer = NULL;2389 info->ring_buffer = STBIR__NEXT_MEMPTR(info->decode_buffer, float);2390 info->encode_buffer = STBIR__NEXT_MEMPTR(info->ring_buffer, float);23912392 STBIR__DEBUG_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->encode_buffer, unsigned char) == (size_t)tempmem + tempmem_size_in_bytes);2393 }2394 else2395 {2396 info->horizontal_buffer = STBIR__NEXT_MEMPTR(info->decode_buffer, float);2397 info->ring_buffer = STBIR__NEXT_MEMPTR(info->horizontal_buffer, float);2398 info->encode_buffer = NULL;23992400 STBIR__DEBUG_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->ring_buffer, unsigned char) == (size_t)tempmem + tempmem_size_in_bytes);2401 }24022403#undef STBIR__NEXT_MEMPTR24042405 // This signals that the ring buffer is empty2406 info->ring_buffer_begin_index = -1;24072408 stbir__calculate_filters(info, info->horizontal_contributors, info->horizontal_coefficients, info->horizontal_filter, info->horizontal_scale, info->horizontal_shift, info->input_w, info->output_w);2409 stbir__calculate_filters(info, info->vertical_contributors, info->vertical_coefficients, info->vertical_filter, info->vertical_scale, info->vertical_shift, info->input_h, info->output_h);24102411 STBIR_PROGRESS_REPORT(0);24122413 if (stbir__use_height_upsampling(info))2414 stbir__buffer_loop_upsample(info);2415 else2416 stbir__buffer_loop_downsample(info);24172418 STBIR_PROGRESS_REPORT(1);24192420#ifdef STBIR_DEBUG_OVERWRITE_TEST2421 STBIR__DEBUG_ASSERT(memcmp(overwrite_output_before_pre, &((unsigned char*)output_data)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE) == 0);2422 STBIR__DEBUG_ASSERT(memcmp(overwrite_output_after_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE) == 0);2423 STBIR__DEBUG_ASSERT(memcmp(overwrite_tempmem_before_pre, &((unsigned char*)tempmem)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE) == 0);2424 STBIR__DEBUG_ASSERT(memcmp(overwrite_tempmem_after_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE) == 0);2425#endif24262427 return 1;2428}242924302431static int stbir__resize_arbitrary(2432 void *alloc_context,2433 const void* input_data, int input_w, int input_h, int input_stride_in_bytes,2434 void* output_data, int output_w, int output_h, int output_stride_in_bytes,2435 float s0, float t0, float s1, float t1, float *transform,2436 int channels, int alpha_channel, stbir_uint32 flags, stbir_datatype type,2437 stbir_filter h_filter, stbir_filter v_filter,2438 stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace)2439{2440 stbir__info info;2441 int result;2442 size_t memory_required;2443 void* extra_memory;24442445 stbir__setup(&info, input_w, input_h, output_w, output_h, channels);2446 stbir__calculate_transform(&info, s0,t0,s1,t1,transform);2447 stbir__choose_filter(&info, h_filter, v_filter);2448 memory_required = stbir__calculate_memory(&info);2449 extra_memory = STBIR_MALLOC(memory_required, alloc_context);24502451 if (!extra_memory)2452 return 0;24532454 result = stbir__resize_allocated(&info, input_data, input_stride_in_bytes,2455 output_data, output_stride_in_bytes, 2456 alpha_channel, flags, type,2457 edge_horizontal, edge_vertical,2458 colorspace, extra_memory, memory_required);24592460 STBIR_FREE(extra_memory, alloc_context);24612462 return result;2463}24642465STBIRDEF int stbir_resize_uint8( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2466 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2467 int num_channels)2468{2469 return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,2470 output_pixels, output_w, output_h, output_stride_in_bytes,2471 0,0,1,1,NULL,num_channels,-1,0, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,2472 STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR);2473}24742475STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2476 float *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2477 int num_channels)2478{2479 return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,2480 output_pixels, output_w, output_h, output_stride_in_bytes,2481 0,0,1,1,NULL,num_channels,-1,0, STBIR_TYPE_FLOAT, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,2482 STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR);2483}24842485STBIRDEF int stbir_resize_uint8_srgb(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2486 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2487 int num_channels, int alpha_channel, int flags)2488{2489 return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,2490 output_pixels, output_w, output_h, output_stride_in_bytes,2491 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,2492 STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);2493}24942495STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2496 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2497 int num_channels, int alpha_channel, int flags,2498 stbir_edge edge_wrap_mode)2499{2500 return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,2501 output_pixels, output_w, output_h, output_stride_in_bytes,2502 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,2503 edge_wrap_mode, edge_wrap_mode, STBIR_COLORSPACE_SRGB);2504}25052506STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2507 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2508 int num_channels, int alpha_channel, int flags,2509 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 2510 void *alloc_context)2511{2512 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2513 output_pixels, output_w, output_h, output_stride_in_bytes,2514 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, filter, filter,2515 edge_wrap_mode, edge_wrap_mode, space);2516}25172518STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2519 stbir_uint16 *output_pixels , int output_w, int output_h, int output_stride_in_bytes,2520 int num_channels, int alpha_channel, int flags,2521 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 2522 void *alloc_context)2523{2524 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2525 output_pixels, output_w, output_h, output_stride_in_bytes,2526 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT16, filter, filter,2527 edge_wrap_mode, edge_wrap_mode, space);2528}252925302531STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2532 float *output_pixels , int output_w, int output_h, int output_stride_in_bytes,2533 int num_channels, int alpha_channel, int flags,2534 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 2535 void *alloc_context)2536{2537 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2538 output_pixels, output_w, output_h, output_stride_in_bytes,2539 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_FLOAT, filter, filter,2540 edge_wrap_mode, edge_wrap_mode, space);2541}254225432544STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2545 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2546 stbir_datatype datatype,2547 int num_channels, int alpha_channel, int flags,2548 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, 2549 stbir_filter filter_horizontal, stbir_filter filter_vertical,2550 stbir_colorspace space, void *alloc_context)2551{2552 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2553 output_pixels, output_w, output_h, output_stride_in_bytes,2554 0,0,1,1,NULL,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical,2555 edge_mode_horizontal, edge_mode_vertical, space);2556}255725582559STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2560 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2561 stbir_datatype datatype,2562 int num_channels, int alpha_channel, int flags,2563 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, 2564 stbir_filter filter_horizontal, stbir_filter filter_vertical,2565 stbir_colorspace space, void *alloc_context,2566 float x_scale, float y_scale,2567 float x_offset, float y_offset)2568{2569 float transform[4];2570 transform[0] = x_scale;2571 transform[1] = y_scale;2572 transform[2] = x_offset;2573 transform[3] = y_offset;2574 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2575 output_pixels, output_w, output_h, output_stride_in_bytes,2576 0,0,1,1,transform,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical,2577 edge_mode_horizontal, edge_mode_vertical, space);2578}25792580STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2581 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2582 stbir_datatype datatype,2583 int num_channels, int alpha_channel, int flags,2584 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, 2585 stbir_filter filter_horizontal, stbir_filter filter_vertical,2586 stbir_colorspace space, void *alloc_context,2587 float s0, float t0, float s1, float t1)2588{2589 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2590 output_pixels, output_w, output_h, output_stride_in_bytes,2591 s0,t0,s1,t1,NULL,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical,2592 edge_mode_horizontal, edge_mode_vertical, space);2593}25942595#endif // STB_IMAGE_RESIZE_IMPLEMENTATION
modified:stb_image.h+6 added-6 removed
764 unmodified lines
765{766 int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen);767 if (n == 0) {768 // at end of file, treat same as if from memory, but need to handle case769 // where s->img_buffer isn't pointing to safe memory, stbi__err.g. 0-byte file769 // where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file770 s->read_from_callbacks = 0;771 s->img_buffer = s->buffer_start;772 s->img_buffer_end = s->buffer_start+1;773 *s->img_buffer = 0;
89 unmodified lines
863864//////////////////////////////////////////////////////////////////////////////865//866// generic converter from built-in img_n to req_comp867// individual types do this automatically as much as possible (stbi__err.g. jpeg867// individual types do this automatically as much as possible (e.g. jpeg868// does all cases internally since it needs to colorspace convert anyway,869// and it never has alpha, so very few cases ). png can automatically870// interleave an alpha=255 channel, but falls back to this for other cases871//
738 unmodified lines
1610 z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w;1611 z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h;16121613 for (i=0; i < s->img_n; ++i) {1614 // number of effective pixels (stbi__err.g. for non-interleaved MCU)1614 // number of effective pixels (e.g. for non-interleaved MCU)1615 z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max;1616 z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max;1617 // to simplify generation, we'll allocate enough memory to decode1618 // the bogus oversized data from using interleaved MCUs and their1619 // big blocks (stbi__err.g. a 16x16 iMCU on an image of width 33); we won't1619 // big blocks (e.g. a 16x16 iMCU on an image of width 33); we won't1620 // discard the extra data until colorspace conversion1621 z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8;1622 z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8;1623 z->img_comp[i].raw_data = stbi__malloc(z->img_comp[i].w2 * z->img_comp[i].h2+15);
11 unmodified lines
16351636 return 1;1637}16381639// use comparisons since in some cases we handle more than one case (stbi__err.g. stbi__SOF)1639// use comparisons since in some cases we handle more than one case (e.g. stbi__SOF)1640#define stbi__DNL(x) ((x) == 0xdc)1641#define stbi__SOI(x) ((x) == 0xd8)1642#define stbi__EOI(x) ((x) == 0xd9)1643#define stbi__SOF(x) ((x) == 0xc0 || (x) == 0xc1)
380 unmodified lines
2024stbi_inline static int stbi__bit_reverse(int v, int bits)2025{2026 STBI_ASSERT(bits <= 16);2027 // to bit reverse n bits, reverse 16 and shift2028 // stbi__err.g. 11 bits, bit reverse and shift away 52028 // e.g. 11 bits, bit reverse and shift away 52029 return stbi__bitreverse16(v) >> (16-bits);2030}20312032static int stbi__zbuild_huffman(stbi__zhuffman *z, stbi_uc *sizelist, int num)
2655 unmodified lines
modified:stb_textedit.h+31 added-1 removed
1// stb_textedit.h - v1.4 - public domain - Sean Barrett1// stb_textedit.h - v1.5 - public domain - Sean Barrett2// Development of this library was sponsored by RAD Game Tools3//4// This C header file implements the guts of a multi-line text-editing5// widget; you implement display, word-wrapping, and low-level string
23 unmodified lines
29//30//31// VERSION HISTORY32//33// 1.5 (2014-09-10) add support for secondary keys for OS X34// 1.4 (2014-08-17) fix signed/unsigned warnings35// 1.3 (2014-06-19) fix mouse clicking to round to nearest char boundary36// 1.2 (2014-05-27) fix some RAD types that had crept into the new code37// 1.1 (2013-12-15) move-by-word (requires STB_TEXTEDIT_IS_SPACE )
5 unmodified lines
43// ADDITIONAL CONTRIBUTORS44//45// Ulf Winklemann: move-by-word in 1.146// Scott Graham: mouse selection bugfix in 1.347// Fabian Giesen: secondary key inputs in 1.548//49// USAGE50//51// This file behaves differently depending on what symbols you define
91 unmodified lines
143// STB_TEXTEDIT_IS_SPACE(ch) true if character is whitespace (e.g. 'isspace'),144// required for WORDLEFT/WORDRIGHT145// STB_TEXTEDIT_K_WORDLEFT keyboard input to move cursor left one word // e.g. ctrl-LEFT146// STB_TEXTEDIT_K_WORDRIGHT keyboard input to move cursor right one word // e.g. ctrl-RIGHT147// STB_TEXTEDIT_K_LINESTART2 secondary keyboard input to move cursor to start of line148// STB_TEXTEDIT_K_LINEEND2 secondary keyboard input to move cursor to end of line149// STB_TEXTEDIT_K_TEXTSTART2 secondary keyboard input to move cursor to start of text150// STB_TEXTEDIT_K_TEXTEND2 secondary keyboard input to move cursor to end of text151//152// Todo:153// STB_TEXTEDIT_K_PGUP keyboard input to move cursor up a page154// STB_TEXTEDIT_K_PGDOWN keyboard input to move cursor down a page
767 unmodified lines
922 }923 state->has_preferred_x = 0;924 break;925 926#ifdef STB_TEXTEDIT_K_TEXTSTART2927 case STB_TEXTEDIT_K_TEXTSTART2:928#endif929 case STB_TEXTEDIT_K_TEXTSTART:930 state->cursor = state->select_start = state->select_end = 0;931 state->has_preferred_x = 0;932 break;933934#ifdef STB_TEXTEDIT_K_TEXTEND2935 case STB_TEXTEDIT_K_TEXTEND2:936#endif937 case STB_TEXTEDIT_K_TEXTEND:938 state->cursor = STB_TEXTEDIT_STRINGLEN(str);939 state->select_start = state->select_end = 0;940 state->has_preferred_x = 0;941 break;942 943#ifdef STB_TEXTEDIT_K_TEXTSTART2944 case STB_TEXTEDIT_K_TEXTSTART2 | STB_TEXTEDIT_K_SHIFT:945#endif946 case STB_TEXTEDIT_K_TEXTSTART | STB_TEXTEDIT_K_SHIFT:947 stb_textedit_prep_selection_at_cursor(state);948 state->cursor = state->select_end = 0;949 state->has_preferred_x = 0;950 break;951952#ifdef STB_TEXTEDIT_K_TEXTEND2953 case STB_TEXTEDIT_K_TEXTEND2 | STB_TEXTEDIT_K_SHIFT:954#endif955 case STB_TEXTEDIT_K_TEXTEND | STB_TEXTEDIT_K_SHIFT:956 stb_textedit_prep_selection_at_cursor(state);957 state->cursor = state->select_end = STB_TEXTEDIT_STRINGLEN(str);958 state->has_preferred_x = 0;959 break;960961962#ifdef STB_TEXTEDIT_K_LINESTART2963 case STB_TEXTEDIT_K_LINESTART2:964#endif965 case STB_TEXTEDIT_K_LINESTART: {966 StbFindState find;967 stb_textedit_clamp(str, state);968 stb_textedit_move_to_first(state);
2 unmodified lines
971 state->has_preferred_x = 0;972 break;973 }974975#ifdef STB_TEXTEDIT_K_LINEEND2976 case STB_TEXTEDIT_K_LINEEND2:977#endif978 case STB_TEXTEDIT_K_LINEEND: {979 StbFindState find;980 stb_textedit_clamp(str, state);981 stb_textedit_move_to_first(state);
2 unmodified lines
984 state->has_preferred_x = 0;985 break;986 }987988#ifdef STB_TEXTEDIT_K_LINESTART2989 case STB_TEXTEDIT_K_LINESTART2 | STB_TEXTEDIT_K_SHIFT:990#endif991 case STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_SHIFT: {992 StbFindState find;993 stb_textedit_clamp(str, state);994 stb_textedit_prep_selection_at_cursor(state);
2 unmodified lines
997 state->has_preferred_x = 0;998 break;999 }10001001#ifdef STB_TEXTEDIT_K_LINEEND21002 case STB_TEXTEDIT_K_LINEEND2 | STB_TEXTEDIT_K_SHIFT:1003#endif1004 case STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_SHIFT: {1005 StbFindState find;1006 stb_textedit_clamp(str, state);1007 stb_textedit_prep_selection_at_cursor(state);
277 unmodified lines