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
Provenance
- Commit
17e82a4- Tree
2ea51e3- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent 1
671f04b- Parent 2
dea604b
13 files changed+3764 added−21 removed
modified:docs/stb_resample_ideas.txt+9 added
188 unmodified lines189 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 )192 193+194+195+Wish 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 kernels200+201+added:tests/resample_test_c.c+5 added
1+#define STB_IMAGE_RESIZE_IMPLEMENTATION2+#define STB_IMAGE_RESIZE_STATIC3+#include "stb_image_resize.h"4+5+// Just to make sure it will build properly with a c compileradded:tests/resample_test.cpp+983 added
1+#include <malloc.h>2+3+#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+#endif12+13+#define STBIR_MALLOC stbir_malloc14+#define STBIR_FREE stbir_free15+16+class stbir_context {17+public:18+ stbir_context()19+ {20+ size = 1000000;21+ memory = malloc(size);22+ }23+24+ ~stbir_context()25+ {26+ free(memory);27+ }28+29+ size_t size;30+ void* memory;31+} g_context;32+33+void* stbir_malloc(size_t size, void* context)34+{35+ if (!context)36+ return malloc(size);37+38+ stbir_context* real_context = (stbir_context*)context;39+ if (size > real_context->size)40+ return 0;41+42+ return real_context->memory;43+}44+45+void stbir_free(void* memory, void* context)46+{47+ if (!context)48+ free(memory);49+}50+51+//#include <stdio.h>52+void stbir_progress(float p)53+{54+ //printf("%f\n", p);55+ STBIR_ASSERT(p >= 0 && p <= 1);56+}57+58+#define STBIR_PROGRESS_REPORT stbir_progress59+60+#define STB_IMAGE_RESIZE_IMPLEMENTATION61+#define STB_IMAGE_RESIZE_STATIC62+#include "stb_image_resize.h"63+64+#define STB_IMAGE_WRITE_IMPLEMENTATION65+#include "stb_image_write.h"66+67+#define STB_IMAGE_IMPLEMENTATION68+#include "stb_image.h"69+70+#ifdef _WIN3271+#include <sys/timeb.h>72+#endif73+74+#include <direct.h>75+76+#define MT_SIZE 62477+static size_t g_aiMT[MT_SIZE];78+static size_t g_iMTI = 0;79+80+// 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.82+void 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+ }92+93+ g_iMTI = 0;94+}95+96+size_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+ }108+109+ 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);114+115+ g_iMTI = (g_iMTI + 1) % MT_SIZE;116+117+ return y;118+}119+120+121+inline float mtfrand()122+{123+ const int ninenine = 999999;124+ return (float)(mtrand() % ninenine)/ninenine;125+}126+127+static 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+}143+144+static 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+ #endif163+164+ 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+}172+173+void test_suite(int argc, char **argv);174+175+int main(int argc, char** argv)176+{177+ //resizer(argc, argv);178+ //performance(argc, argv);179+180+ test_suite(argc, argv);181+ return 0;182+}183+184+void 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;187+188+ 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+ }194+195+ int out_w = (int)(w * width_percent);196+ int out_h = (int)(h * height_percent);197+198+ unsigned char* output_data = (unsigned char*)malloc(out_w * out_h * n);199+200+ 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);201+202+ stbi_image_free(input_data);203+204+ stbi_write_png(output_filename, out_w, out_h, n, output_data, 0);205+206+ free(output_data);207+}208+209+template <typename F, typename T>210+void 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+}216+217+template <typename T>218+void 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);222+223+ if (input_data == NULL)224+ return;225+226+227+ int new_w = (int)(w * width_percent);228+ int new_h = (int)(h * height_percent);229+230+ 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);233+234+ T* output_data = (T*)malloc(new_w * new_h * n * sizeof(T));235+236+ 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);237+238+ free(T_data);239+ stbi_image_free(input_data);240+241+ 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);243+244+ 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);247+248+ free(char_data);249+ free(output_data);250+}251+252+void 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+}257+258+void 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+}263+264+void 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);268+269+ if (input_data == NULL)270+ return;271+272+ int new_w = (int)(w * width_percent);273+ int new_h = (int)(h * height_percent);274+275+ float* T_data = (float*)malloc(w * h * n * sizeof(float));276+ convert_image_float(input_data, T_data, w * h * n);277+278+ float* output_data = (float*)malloc(new_w * new_h * n * sizeof(float));279+280+ 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);281+282+ free(T_data);283+ stbi_image_free(input_data);284+285+ 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);287+288+ 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);291+292+ free(char_data);293+ free(output_data);294+}295+296+void 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);300+301+ if (input_data == NULL)302+ return;303+304+ int new_w = (int)(w * width_percent);305+ int new_h = (int)(h * height_percent);306+307+ unsigned char* channels_data = (unsigned char*)malloc(w * h * channels * sizeof(unsigned char));308+309+ for (int i = 0; i < w * h; i++)310+ {311+ int input_position = i * n;312+ int output_position = i * channels;313+314+ for (int c = 0; c < channels; c++)315+ channels_data[output_position + c] = input_data[input_position + stbir__min(c, n)];316+ }317+318+ unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * channels * sizeof(unsigned char));319+320+ stbir_resize_uint8_srgb(channels_data, w, h, 0, output_data, new_w, new_h, 0, channels, STBIR_ALPHA_CHANNEL_NONE, 0);321+322+ free(channels_data);323+ stbi_image_free(input_data);324+325+ 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);328+329+ free(output_data);330+}331+332+void 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);336+337+ if (input_data == NULL)338+ return;339+340+ s1 = ((float)w - 1 + s1)/w;341+ t1 = ((float)h - 1 + t1)/h;342+343+ int new_w = (int)(w * width_percent);344+ int new_h = (int)(h * height_percent);345+346+ unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));347+348+ 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);349+350+ stbi_image_free(input_data);351+352+ 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);355+356+ free(output_data);357+}358+359+unsigned 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+}363+364+void test_premul()365+{366+ unsigned int input[2 * 2 * 4];367+ unsigned int output[1 * 1 * 4];368+ unsigned int output2[2 * 2 * 4];369+370+ memset(input, 0, sizeof(input));371+372+ // First a test to make sure premul is working properly.373+374+ // Top left - solid red375+ *pixel(input, 0, 0, 0, 2, 4) = 255;376+ *pixel(input, 0, 0, 3, 2, 4) = 255;377+378+ // Bottom left - solid red379+ *pixel(input, 0, 1, 0, 2, 4) = 255;380+ *pixel(input, 0, 1, 3, 2, 4) = 255;381+382+ // Top right - transparent green383+ *pixel(input, 1, 0, 1, 2, 4) = 255;384+ *pixel(input, 1, 0, 3, 2, 4) = 25;385+386+ // Bottom right - transparent green387+ *pixel(input, 1, 1, 1, 2, 4) = 255;388+ *pixel(input, 1, 1, 3, 2, 4) = 25;389+390+ 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);391+392+ 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;397+398+ 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)); // 140402+403+ // Now a test to make sure it doesn't clobber existing values.404+405+ // Top right - completely transparent green406+ *pixel(input, 1, 0, 1, 2, 4) = 255;407+ *pixel(input, 1, 0, 3, 2, 4) = 0;408+409+ // Bottom right - completely transparent green410+ *pixel(input, 1, 1, 1, 2, 4) = 255;411+ *pixel(input, 1, 1, 3, 2, 4) = 0;412+413+ 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);414+415+ 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);419+420+ 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);424+425+ 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);429+430+ 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+}435+436+// test that splitting a pow-2 image into tiles produces identical results437+void test_subpixel_1()438+{439+ unsigned char image[8 * 8];440+441+ mtsrand(0);442+443+ for (int i = 0; i < sizeof(image); i++)444+ image[i] = mtrand() & 255;445+446+ unsigned char output_data[16 * 16];447+448+ 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);449+450+ unsigned char output_left[8 * 16];451+ unsigned char output_right[8 * 16];452+453+ 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);455+456+ 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+}465+466+// test that replicating an image and using a subtile of it produces same results as wraparound467+void test_subpixel_2()468+{469+ unsigned char image[8 * 8];470+471+ mtsrand(0);472+473+ for (int i = 0; i < sizeof(image); i++)474+ image[i] = mtrand() & 255;475+476+ unsigned char large_image[32 * 32];477+478+ 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+ }489+490+ unsigned char output_data_1[16 * 16];491+ unsigned char output_data_2[16 * 16];492+493+ 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);495+496+ {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+}502+503+// test that 0,0,1,1 subpixel produces same result as no-rect504+void test_subpixel_3()505+{506+ unsigned char image[8 * 8];507+508+ mtsrand(0);509+510+ for (int i = 0; i < sizeof(image); i++)511+ image[i] = mtrand() & 255;512+513+ unsigned char output_data_1[32 * 32];514+ unsigned char output_data_2[32 * 32];515+516+ 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);518+519+ 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+}525+526+// test that 1:1 resample using s,t=0,0,1,1 with bilinear produces original image527+void test_subpixel_4()528+{529+ unsigned char image[8 * 8];530+531+ mtsrand(0);532+533+ for (int i = 0; i < sizeof(image); i++)534+ image[i] = mtrand() & 255;535+536+ unsigned char output[8 * 8];537+538+ 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+}541+542+static unsigned int image88_int[8][8];543+static unsigned char image88 [8][8];544+static unsigned char output88[8][8];545+static unsigned char output44[4][4];546+static unsigned char output22[2][2];547+static unsigned char output11[1][1];548+549+void 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+}556+557+void verify_box(void)558+{559+ int i,j,t;560+561+ resample_88(STBIR_FILTER_BOX);562+563+ for (i=0; i < sizeof(image88); ++i)564+ STBIR_ASSERT(image88[0][i] == output88[0][i]);565+566+ 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+}578+579+void verify_filter_normalized(stbir_filter filter, int output_size, unsigned int value)580+{581+ int i, j;582+ unsigned int output[64];583+584+ 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);585+586+ 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+}590+591+float round2(float f)592+{593+ return (float) floor(f+0.5f); // round() isn't C standard pre-C99594+}595+596+void test_filters(void)597+{598+ int i,j;599+600+ mtsrand(0);601+602+ for (i=0; i < sizeof(image88); ++i)603+ image88[0][i] = mtrand() & 255;604+ verify_box();605+606+ for (i=0; i < sizeof(image88); ++i)607+ image88[0][i] = 0;608+ image88[4][4] = 255;609+ verify_box();610+611+ 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();615+616+ for (j=0; j < 8; ++j)617+ for (i=0; i < 8; ++i)618+ image88[j][i] = i&2 ? 255 : 0;619+ verify_box();620+621+ int value = 64;622+623+ for (j = 0; j < 8; ++j)624+ for (i = 0; i < 8; ++i)625+ image88_int[j][i] = value;626+627+ 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);632+633+ 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);638+639+ 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);644+645+ 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);650+651+ {652+ // This test is designed to produce coefficients that are very badly denormalized.653+ unsigned int v = 556;654+655+ unsigned int input[100 * 100];656+ unsigned int output[11 * 11];657+658+ for (j = 0; j < 100 * 100; ++j)659+ input[j] = v;660+661+ 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);662+663+ for (j = 0; j < 11 * 11; ++j)664+ STBIR_ASSERT(v == output[j]);665+ }666+667+ {668+ // Now test the trapezoid filter for downsampling.669+ unsigned int input[3 * 1];670+ unsigned int output[2 * 1];671+672+ input[0] = 0;673+ input[1] = 255;674+ input[2] = 127;675+676+ 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);677+678+ 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));680+681+ 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);682+683+ 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+ }686+687+ {688+ // Now test the trapezoid filter for upsampling.689+ unsigned int input[2 * 1];690+ unsigned int output[3 * 1];691+692+ input[0] = 0;693+ input[1] = 255;694+695+ 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);696+697+ STBIR_ASSERT(output[0] == input[0]);698+ STBIR_ASSERT(output[1] == (input[0] + input[1]) / 2);699+ STBIR_ASSERT(output[2] == input[1]);700+701+ 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);702+703+ STBIR_ASSERT(output[0] == input[0]);704+ STBIR_ASSERT(output[1] == (input[0] + input[1]) / 2);705+ STBIR_ASSERT(output[2] == input[1]);706+ }707+708+ // 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);724+725+726+ }727+728+ {729+ // Test trapezoid box filter730+ unsigned char input[2 * 1];731+ unsigned char output[127 * 1];732+733+ input[0] = 0;734+ input[1] = 255;735+736+ 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);743+744+ 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+}753+754+#define UMAX32 4294967295U755+756+static 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+}764+765+static 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);781+782+ 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+}787+788+789+void test_suite(int argc, char **argv)790+{791+ int i;792+ char *barbara;793+794+ _mkdir("test-output");795+796+ if (argc > 1)797+ barbara = argv[1];798+ else799+ barbara = "barbara.png";800+801+ // 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+ }818+819+#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+#endif838+839+#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+#endif847+848+ // 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+ }855+856+ // 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+ }865+866+ test_filters();867+868+ test_subpixel_1();869+ test_subpixel_2();870+ test_subpixel_3();871+ test_subpixel_4();872+873+ test_premul();874+875+ test_32();876+877+ // 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);882+883+ for (i = 0; i < 10; i++)884+ test_subpixel(barbara, 0.5f, 0.5f, (float)i / 10, 1);885+886+ for (i = 0; i < 10; i++)887+ test_subpixel(barbara, 0.5f, 0.5f, 1, (float)i / 10);888+889+ for (i = 0; i < 10; i++)890+ test_subpixel(barbara, 2, 2, (float)i / 10, 1);891+892+ for (i = 0; i < 10; i++)893+ test_subpixel(barbara, 2, 2, 1, (float)i / 10);894+895+ // 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);900+901+ 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);905+906+ // 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");912+913+ 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");918+919+ 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+ }925+926+ 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+ }932+933+ 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+ }939+940+ 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+ }946+947+ 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+ }953+954+ 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);958+959+ 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);963+964+ 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);968+969+ // 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");972+973+ 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");975+976+ 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");978+979+ // 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 **4+5+# TARGTYPE "Win32 (x86) Console Application" 0x01036+7+CFG=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 23+24+# Begin Project25+# PROP AllowPerConfigDependencies 026+# PROP Scc_ProjName ""27+# PROP Scc_LocalPath ""28+CPP=cl.exe29+RSC=rc.exe30+31+!IF "$(CFG)" == "resize - Win32 Release"32+33+# 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"48+BSC32=bscmake.exe49+# ADD BASE BSC32 /nologo50+# ADD BSC32 /nologo51+LINK32=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:I38654+55+!ELSEIF "$(CFG)" == "resize - Win32 Debug"56+57+# 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"71+BSC32=bscmake.exe72+# ADD BASE BSC32 /nologo73+# ADD BSC32 /nologo74+LINK32=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:sept77+78+!ENDIF 79+80+# Begin Target81+82+# Name "resize - Win32 Release"83+# Name "resize - Win32 Debug"84+# Begin Source File85+86+SOURCE=.\resample_test.cpp87+# End Source File88+# Begin Source File89+90+SOURCE=..\stb_image_resize.h91+# End Source File92+# End Target93+# End Projectmodified:tests/stb.dsp+8 added
104 unmodified lines105 SOURCE=..\stb_dxt.h106 # End Source File107 # Begin Source File108 109+SOURCE=..\stb_herringbone_wang_tile.h110+# End Source File111+# Begin Source File112+113 SOURCE=..\stb_image.h114 # End Source File115 # Begin Source File116 117+SOURCE=..\stb_image_resize.h118+# End Source File119+# Begin Source File120+121 SOURCE=..\stb_image_write.h122 # End Source File123 # Begin Source File124 36 unmodified linesmodified:tests/stb.dsw+12 added
61 unmodified lines62 }}}63 64 ###############################################################################65 66+Project: "resize"=.\resize\resize.dsp - Package Owner=<4>67+68+Package=<5>69+{{{70+}}}71+72+Package=<4>73+{{{74+}}}75+76+###############################################################################77+78 Project: "stb"=.\stb.dsp - Package Owner=<4>79 80 Package=<5>81 {{{56 unmodified linesmodified:tests/test_c_compilation.c+2 added
3 unmodified lines4 #define STB_C_LEXER_IMPLEMENTATIOn5 #define STB_DIVIDE_IMPLEMENTATION6 #define STB_IMAGE_IMPLEMENTATION7 #define STB_HERRINGBONE_WANG_TILE_IMEPLEMENTATIOn8+#define STB_IMAGE_RESIZE_IMPLEMENTATION9 10 #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 lines83 # Begin Source File84 85 SOURCE=.\make_readme.c86 # End Source File87+# Begin Source File88+89+SOURCE=.\README.list90+# End Source File91 # End Target92 # End Projectmodified:tools/README.list+13 added-12 removed
1-stb_vorbis.c | audio | decode ogg vorbis files from file/memory to float/16-bit signed output2-stb_image.h | graphics | image loading/decoding from file/memory: JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC3-stb_truetype.h | graphics | parse, decode, and rasterize characters from truetype fonts4-stb_image_write.h | graphics | image writing to disk: PNG, TGA, BMP5-stretchy_buffer.h | utility | typesafe dynamic array for C (i.e. approximation to vector<>), doesn't compile as C++6-stb_textedit.h | UI | guts of a text editor for games etc implementing them from scratch7-stb_dxt.h | 3D graphics | Fabian "ryg" Giesen's real-time DXT compressor8-stb_herringbone_wang_tile.h | games | herringbone Wang tile map generator9-stb_perlin.h | 3D graphics | revised Perlin noise (3D input, 1D output)10-stb_c_lexer.h | parsing | simplify writing parsers for C-like languages11-stb_divide.h | math | more useful 32-bit modulus e.g. "euclidean divide"12-stb.h | misc | helper functions for C, mostly redundant in C++; basically author's personal stuff1+stb_vorbis.c | audio | decode ogg vorbis files from file/memory to float/16-bit signed output2+stb_image.h | graphics | image loading/decoding from file/memory: JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC3+stb_truetype.h | graphics | parse, decode, and rasterize characters from truetype fonts4+stb_image_write.h | graphics | image writing to disk: PNG, TGA, BMP5+stb_image_resize.h | graphics | resize images larger/smaller with good quality6+stretchy_buffer.h | utility | typesafe dynamic array for C (i.e. approximation to vector<>), doesn't compile as C++7+stb_textedit.h | UI | guts of a text editor for games etc implementing them from scratch8+stb_dxt.h | 3D graphics | Fabian "ryg" Giesen's real-time DXT compressor9+stb_perlin.h | 3D graphics | revised Perlin noise (3D input, 1D output)10+stb_herringbone_wang_tile.h | games | herringbone Wang tile map generator11+stb_c_lexer.h | parsing | simplify writing parsers for C-like languages12+stb_divide.h | math | more useful 32-bit modulus e.g. "euclidean divide"13+stb.h | misc | helper functions for C, mostly redundant in C++; basically author's personal stuffmodified:README.md+3 added-2 removed
7 unmodified lines89101112131314151516171819202164 unmodified lines7 unmodified lines**stb_vorbis.c** | 1.04 | audio | decode ogg vorbis files from file/memory to float/16-bit signed output**stb_image.h** | 1.46 | graphics | image loading/decoding from file/memory: JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC**stb_truetype.h** | 0.9 | graphics | parse, decode, and rasterize characters from truetype fonts**stb_image_write.h** | 0.95 | graphics | image writing to disk: PNG, TGA, BMP**stb_image_resize.h** | 0.90 | graphics | resize images larger/smaller with good quality**stretchy_buffer.h** | 1.01 | utility | typesafe dynamic array for C (i.e. approximation to vector<>), doesn't compile as C++**stb_textedit.h** | 1.4 | UI | guts of a text editor for games etc implementing them from scratch**stb_textedit.h** | 1.5 | UI | guts of a text editor for games etc implementing them from scratch**stb_dxt.h** | 1.04 | 3D graphics | Fabian "ryg" Giesen's real-time DXT compressor**stb_herringbone_wang_tile.h** | 0.6 | games | herringbone Wang tile map generator**stb_perlin.h** | 0.2 | 3D graphics | revised Perlin noise (3D input, 1D output)**stb_herringbone_wang_tile.h** | 0.6 | games | herringbone Wang tile map generator**stb_c_lexer.h** | 0.06 | parsing | simplify writing parsers for C-like languages**stb_divide.h** | 0.91 | math | more useful 32-bit modulus e.g. "euclidean divide"**stb.h** | 2.23 | misc | helper functions for C, mostly redundant in C++; basically author's personal stuff
64 unmodified linesadded: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/stb4+5+ 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.9+10+ 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.14+15+ 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/ZERO27+28+ FULL API29+ See the "header file" section of the source for API documentation.30+31+ ADDITIONAL DOCUMENTATION32+33+ 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.)41+42+ 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:46+47+ #define STBIR_MALLOC(size,context) ...48+ #define STBIR_FREE(ptr,context) ...49+50+ 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.52+53+ ASSERT54+ Define STBIR_ASSERT(boolval) to override assert() and not use assert.h55+56+ 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.60+61+ DEFAULT FILTERS62+ For functions which don't provide explicit control over what filters63+ to use, you can change the compile-time defaults with64+65+ #define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_something66+ #define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_something67+68+ See stbir_filter in the header-file section for the list of filters.69+70+ 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.74+75+ PROGRESS76+ For interactive use with slow resize operations, you can install77+ a progress-report callback:78+79+ #define STBIR_PROGRESS_REPORT(val) some_func(val)80+81+ The parameter val is a float which goes from 0 to 1 as progress is made.82+83+ For example:84+85+ static void my_progress_report(float progress);86+ #define STBIR_PROGRESS_REPORT(val) my_progress_report(val)87+88+ #define STB_IMAGE_RESIZE_IMPLEMENTATION89+ #include "stb_image_resize.h"90+91+ static void my_progress_report(float progress)92+ {93+ printf("Progress: %f%%\n", progress*100);94+ }95+96+ MAX CHANNELS97+ If your image has more than 64 channels, define STBIR_MAX_CHANNELS98+ to the max you'll have.99+100+ 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:104+105+ 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.112+113+ 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.)119+120+ 2. Artists should not edit premultiplied-alpha images; artists121+ want non-premultiplied alpha images. Thus, art tools generally output122+ non-premultiplied alpha images.123+124+ 3. You will get best results in most cases by converting images125+ to premultiplied alpha before processing them mathematically.126+127+ 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.132+133+ 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.)148+149+ 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.152+153+ 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.)158+159+ ADDITIONAL CONTRIBUTORS160+ Sean Barrett: API design, optimizations161+ 162+ REVISIONS163+ 0.90 (2014-09-17) first released version164+165+ 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.169+170+ 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+*/179+180+#ifndef STBIR_INCLUDE_STB_IMAGE_RESIZE_H181+#define STBIR_INCLUDE_STB_IMAGE_RESIZE_H182+183+#ifdef _MSC_VER184+typedef unsigned char stbir_uint8;185+typedef unsigned short stbir_uint16;186+typedef unsigned int stbir_uint32;187+#else188+#include <stdint.h>189+typedef uint8_t stbir_uint8;190+typedef uint16_t stbir_uint16;191+typedef uint32_t stbir_uint32;192+#endif193+194+#ifdef STB_IMAGE_RESIZE_STATIC195+#define STBIRDEF static196+#else197+#ifdef __cplusplus198+#define STBIRDEF extern "C"199+#else200+#define STBIRDEF extern201+#endif202+#endif203+204+205+//////////////////////////////////////////////////////////////////////////////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.222+223+STBIRDEF 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);226+227+STBIRDEF 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);230+231+232+// 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.237+238+#define STBIR_ALPHA_CHANNEL_NONE -1239+240+// 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)247+248+STBIRDEF 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);251+252+253+typedef enum254+{255+ STBIR_EDGE_CLAMP = 1,256+ STBIR_EDGE_REFLECT = 2,257+ STBIR_EDGE_WRAP = 3,258+ STBIR_EDGE_ZERO = 4,259+} stbir_edge;260+261+// This function adds the ability to specify how requests to sample off the edge of the image are handled.262+STBIRDEF 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);266+267+//////////////////////////////////////////////////////////////////////////////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_MALLOC281+282+typedef 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;291+292+typedef enum293+{294+ STBIR_COLORSPACE_LINEAR,295+ STBIR_COLORSPACE_SRGB,296+297+ STBIR_MAX_COLORSPACES,298+} stbir_colorspace;299+300+// The following functions are all identical except for the type of the image data301+302+STBIRDEF 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);307+308+STBIRDEF 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);313+314+STBIRDEF 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);319+320+321+322+//////////////////////////////////////////////////////////////////////////////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 coordinates334+335+typedef enum336+{337+ STBIR_TYPE_UINT8 ,338+ STBIR_TYPE_UINT16,339+ STBIR_TYPE_UINT32,340+ STBIR_TYPE_FLOAT ,341+342+ STBIR_MAX_TYPES343+} stbir_datatype;344+345+STBIRDEF 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);352+353+STBIRDEF 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);362+363+STBIRDEF 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.372+373+//374+//375+//// end header file /////////////////////////////////////////////////////376+#endif // STBIR_INCLUDE_STB_IMAGE_RESIZE_H377+378+379+380+381+382+#ifdef STB_IMAGE_RESIZE_IMPLEMENTATION383+384+#ifndef STBIR_ASSERT385+#include <assert.h>386+#define STBIR_ASSERT(x) assert(x)387+#endif388+389+#ifdef STBIR_DEBUG390+#define STBIR__DEBUG_ASSERT STBIR_ASSERT391+#else392+#define STBIR__DEBUG_ASSERT393+#endif394+395+// If you hit this it means I haven't done it yet.396+#define STBIR__UNIMPLEMENTED(x) STBIR_ASSERT(!(x))397+398+// For memset399+#include <string.h>400+401+#include <math.h>402+403+#ifndef STBIR_MALLOC404+#include <stdlib.h>405+#define STBIR_MALLOC(size,c) malloc(size)406+#define STBIR_FREE(ptr,c) free(ptr)407+#endif408+409+#ifndef _MSC_VER410+#ifdef __cplusplus411+#define stbir__inline inline412+#else413+#define stbir__inline414+#endif415+#else416+#define stbir__inline __forceinline417+#endif418+419+420+// should produce compiler error if size is wrong421+typedef unsigned char stbir__validate_uint32[sizeof(stbir_uint32) == 4 ? 1 : -1];422+423+#ifdef _MSC_VER424+#define STBIR__NOTUSED(v) (void)(v)425+#else426+#define STBIR__NOTUSED(v) (void)sizeof(v)427+#endif428+429+#define STBIR__ARRAY_SIZE(a) (sizeof((a))/sizeof((a)[0]))430+431+#ifndef STBIR_DEFAULT_FILTER_UPSAMPLE432+#define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_CATMULLROM433+#endif434+435+#ifndef STBIR_DEFAULT_FILTER_DOWNSAMPLE436+#define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_MITCHELL437+#endif438+439+#ifndef STBIR_PROGRESS_REPORT440+#define STBIR_PROGRESS_REPORT(float_0_to_1)441+#endif442+443+#ifndef STBIR_MAX_CHANNELS444+#define STBIR_MAX_CHANNELS 64445+#endif446+447+#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+#endif451+452+// 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+#endif460+461+462+463+#ifdef _MSC_VER464+#define STBIR__UNUSED_PARAM(v) (void)(v)465+#else466+#define STBIR__UNUSED_PARAM(v) (void)sizeof(v)467+#endif468+469+// must match stbir_datatype470+static unsigned char stbir__type_size[] = {471+ 1, // STBIR_TYPE_UINT8472+ 2, // STBIR_TYPE_UINT16473+ 4, // STBIR_TYPE_UINT32474+ 4, // STBIR_TYPE_FLOAT475+};476+477+// Kernel function centered at 0478+typedef float (stbir__kernel_fn)(float x, float scale);479+typedef float (stbir__support_fn)(float scale);480+481+typedef struct482+{483+ stbir__kernel_fn* kernel;484+ stbir__support_fn* support;485+} stbir__filter_info;486+487+// When upsampling, the contributors are which source pixels contribute.488+// When downsampling, the contributors are which destination pixels are contributed to.489+typedef struct490+{491+ int n0; // First contributing pixel492+ int n1; // Last contributing pixel493+} stbir__contributors;494+495+typedef struct496+{497+ const void* input_data;498+ int input_w;499+ int input_h;500+ int input_stride_bytes;501+502+ void* output_data;503+ int output_w;504+ int output_h;505+ int output_stride_bytes;506+507+ float s0, t0, s1, t1;508+509+ float horizontal_shift; // Units: output pixels510+ float vertical_shift; // Units: output pixels511+ float horizontal_scale;512+ float vertical_scale;513+514+ 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;523+524+ stbir__contributors* horizontal_contributors;525+ float* horizontal_coefficients;526+527+ stbir__contributors* vertical_contributors;528+ float* vertical_coefficients;529+530+ int decode_buffer_pixels;531+ float* decode_buffer;532+533+ float* horizontal_buffer;534+535+ // 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;544+545+ 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;550+551+ float* encode_buffer; // A temporary buffer to store floats so we don't lose precision while we do multiply-adds.552+553+ 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;562+563+static stbir__inline int stbir__min(int a, int b)564+{565+ return a < b ? a : b;566+}567+568+static stbir__inline int stbir__max(int a, int b)569+{570+ return a > b ? a : b;571+}572+573+static stbir__inline float stbir__saturate(float x)574+{575+ if (x < 0)576+ return 0;577+578+ if (x > 1)579+ return 1;580+581+ return x;582+}583+584+#ifdef STBIR_SATURATE_INT585+static stbir__inline stbir_uint8 stbir__saturate8(int x)586+{587+ if ((unsigned int) x <= 255)588+ return x;589+590+ if (x < 0)591+ return 0;592+593+ return 255;594+}595+596+static stbir__inline stbir_uint16 stbir__saturate16(int x)597+{598+ if ((unsigned int) x <= 65535)599+ return x;600+601+ if (x < 0)602+ return 0;603+604+ return 65535;605+}606+#endif607+608+static 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+};634+635+static 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+}642+643+static 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+}650+651+#ifndef STBIR_NON_IEEE_FLOAT652+// From https://gist.github.com/rygorous/2203834653+654+typedef 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;665+666+static 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+ 682+static 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+}707+708+#else709+// sRGB transition values, scaled by 1<<28710+static 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+};745+746+static 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;751+752+ // 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;761+762+ return (stbir_uint8) v;763+}764+#endif765+766+static 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);771+772+ x = (float)fabs(x);773+774+ 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+}785+786+static float stbir__support_trapezoid(float scale)787+{788+ STBIR__DEBUG_ASSERT(scale <= 1);789+ return 0.5f + scale / 2;790+}791+792+static float stbir__filter_triangle(float x, float s)793+{794+ STBIR__UNUSED_PARAM(s);795+796+ x = (float)fabs(x);797+798+ if (x <= 1.0f)799+ return 1 - x;800+ else801+ return 0;802+}803+804+static float stbir__filter_cubic(float x, float s)805+{806+ STBIR__UNUSED_PARAM(s);807+808+ x = (float)fabs(x);809+810+ 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;814+815+ return (0.0f);816+}817+818+static float stbir__filter_catmullrom(float x, float s)819+{820+ STBIR__UNUSED_PARAM(s);821+822+ x = (float)fabs(x);823+824+ 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));828+829+ return (0.0f);830+}831+832+static float stbir__filter_mitchell(float x, float s)833+{834+ STBIR__UNUSED_PARAM(s);835+836+ x = (float)fabs(x);837+838+ 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;842+843+ return (0.0f);844+}845+846+static float stbir__support_zero(float s)847+{848+ STBIR__UNUSED_PARAM(s);849+ return 0;850+}851+852+static float stbir__support_one(float s)853+{854+ STBIR__UNUSED_PARAM(s);855+ return 1;856+}857+858+static float stbir__support_two(float s)859+{860+ STBIR__UNUSED_PARAM(s);861+ return 2;862+}863+864+static 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+};872+873+stbir__inline static int stbir__use_upsampling(float ratio)874+{875+ return ratio > 1;876+}877+878+stbir__inline static int stbir__use_width_upsampling(stbir__info* stbir_info)879+{880+ return stbir__use_upsampling(stbir_info->horizontal_scale);881+}882+883+stbir__inline static int stbir__use_height_upsampling(stbir__info* stbir_info)884+{885+ return stbir__use_upsampling(stbir_info->vertical_scale);886+}887+888+// This is the maximum number of input samples that can affect an output sample889+// with the given filter890+static 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));894+895+ 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+}900+901+// This is how much to expand buffers to account for filters seeking outside902+// the image boundaries.903+static int stbir__get_filter_pixel_margin(stbir_filter filter, float scale)904+{905+ return stbir__get_filter_pixel_width(filter, scale) / 2;906+}907+908+static 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+}915+916+static 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+}923+924+static 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+}929+930+static 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+}935+936+static stbir__contributors* stbir__get_contributor(stbir__contributors* contributors, int n)937+{938+ return &contributors[n];939+}940+941+// For perf reasons this code is duplicated in stbir__resample_horizontal_upsample/downsample,942+// if you change it here change it there too.943+static 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+}948+949+static 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 later955+956+ case STBIR_EDGE_CLAMP:957+ if (n < 0)958+ return 0;959+960+ if (n >= max)961+ return max - 1;962+963+ return n; // NOTREACHED964+965+ case STBIR_EDGE_REFLECT:966+ {967+ if (n < 0)968+ {969+ if (n < max)970+ return -n;971+ else972+ return max - 1;973+ }974+975+ if (n >= max)976+ {977+ int max2 = max * 2;978+ if (n >= max2)979+ return 0;980+ else981+ return max2 - n - 1;982+ }983+984+ return n; // NOTREACHED985+ }986+987+ case STBIR_EDGE_WRAP:988+ if (n >= 0)989+ return (n % max);990+ else991+ {992+ int m = (-n) % max;993+994+ if (m != 0)995+ m = max - m;996+997+ return (m);998+ }999+ return n; // NOTREACHED1000+1001+ default:1002+ STBIR__UNIMPLEMENTED("Unimplemented edge type");1003+ return 0;1004+ }1005+}1006+1007+stbir__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+}1014+1015+// What input pixels contribute to this output pixel?1016+static 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;1021+1022+ 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;1024+1025+ *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+}1029+1030+// What output pixels does this input pixel contribute to?1031+static 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;1036+1037+ 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;1039+1040+ *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+}1044+1045+static 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;1050+1051+ 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.1052+1053+ contributor->n0 = in_first_pixel;1054+ contributor->n1 = in_last_pixel;1055+1056+ STBIR__DEBUG_ASSERT(contributor->n1 >= contributor->n0);1057+1058+ 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);1062+1063+ // 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+ }1070+1071+ total_filter += coefficient_group[i];1072+ }1073+1074+ STBIR__DEBUG_ASSERT(stbir__filter_info_table[filter].kernel((float)(in_last_pixel + 1) + 0.5f - in_center_of_out, 1/scale) == 0);1075+1076+ STBIR__DEBUG_ASSERT(total_filter > 0.9);1077+ STBIR__DEBUG_ASSERT(total_filter < 1.1f); // Make sure it's not way off.1078+1079+ // Make sure the sum of all coefficients is 1.1080+ filter_scale = 1 / total_filter;1081+1082+ for (i = 0; i <= in_last_pixel - in_first_pixel; i++)1083+ coefficient_group[i] *= filter_scale;1084+1085+ for (i = in_last_pixel - in_first_pixel; i >= 0; i--)1086+ {1087+ if (coefficient_group[i])1088+ break;1089+1090+ // This line has no weight. We can skip it.1091+ contributor->n1 = contributor->n0 + i - 1;1092+ }1093+}1094+1095+static 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;1098+1099+ 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.1100+1101+ contributor->n0 = out_first_pixel;1102+ contributor->n1 = out_last_pixel;1103+1104+ STBIR__DEBUG_ASSERT(contributor->n1 >= contributor->n0);1105+1106+ 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+ }1112+1113+ STBIR__DEBUG_ASSERT(stbir__filter_info_table[filter].kernel((float)(out_last_pixel + 1) + 0.5f - out_center_of_in, scale_ratio) == 0);1114+1115+ for (i = out_last_pixel - out_first_pixel; i >= 0; i--)1116+ {1117+ if (coefficient_group[i])1118+ break;1119+1120+ // This line has no weight. We can skip it.1121+ contributor->n1 = contributor->n0 + i - 1;1122+ }1123+}1124+1125+static 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;1131+1132+ for (i = 0; i < output_size; i++)1133+ {1134+ float scale;1135+ float total = 0;1136+1137+ 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+ }1147+1148+ STBIR__DEBUG_ASSERT(total > 0.9f);1149+ STBIR__DEBUG_ASSERT(total < 1.1f);1150+1151+ scale = 1 / total;1152+1153+ 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+ }1161+1162+ // 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;1167+1168+ skip = 0;1169+ while (*stbir__get_coefficient(coefficients, filter, scale_ratio, j, skip) == 0)1170+ skip++;1171+1172+ contributors[j].n0 += skip;1173+1174+ while (contributors[j].n0 < 0)1175+ {1176+ contributors[j].n0++;1177+ skip++;1178+ }1179+1180+ range = contributors[j].n1 - contributors[j].n0 + 1;1181+ max = stbir__min(num_coefficients, range);1182+1183+ width = stbir__get_coefficient_width(filter, scale_ratio);1184+ for (i = 0; i < max; i++)1185+ {1186+ if (i + skip >= width)1187+ break;1188+1189+ *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i) = *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i + skip);1190+ }1191+1192+ continue;1193+ }1194+1195+ // 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+}1199+1200+// 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.1202+static 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);1206+1207+ if (stbir__use_upsampling(scale_ratio))1208+ {1209+ float out_pixels_radius = stbir__filter_info_table[filter].support(1 / scale_ratio) * scale_ratio;1210+1211+ // 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;1216+1217+ stbir__calculate_sample_range_upsample(n, out_pixels_radius, scale_ratio, shift, &in_first_pixel, &in_last_pixel, &in_center_of_out);1218+1219+ 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;1225+1226+ // 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);1232+1233+ stbir__calculate_sample_range_downsample(n_adjusted, in_pixels_radius, scale_ratio, shift, &out_first_pixel, &out_last_pixel, &out_center_of_in);1234+1235+ 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+ }1237+1238+ stbir__normalize_downsample_coefficients(stbir_info, contributors, coefficients, filter, scale_ratio, shift, input_size, output_size);1239+ }1240+}1241+1242+static 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+}1248+1249+#define STBIR__DECODE(type, colorspace) ((type) * (STBIR_MAX_COLORSPACES) + (colorspace))1250+1251+static 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);1267+1268+ int x = -stbir_info->horizontal_filter_pixel_margin;1269+1270+ // 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+ }1279+1280+ 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;1291+1292+ 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]];1299+1300+ 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;1304+1305+ 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;1314+1315+ 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);1322+1323+ 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;1327+1328+ 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;1337+1338+ 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));1345+1346+ 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;1350+1351+ 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;1360+1361+ 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]);1368+1369+ 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+ }1372+1373+ break;1374+1375+ default:1376+ STBIR__UNIMPLEMENTED("Unknown type/colorspace/channels combination.");1377+ break;1378+ }1379+1380+ 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;1385+1386+ // 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;1398+1399+ decode_buffer[decode_pixel_index + c] *= alpha;1400+ }1401+ }1402+ }1403+1404+ 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+}1418+1419+static 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+}1423+1424+static float* stbir__add_empty_ring_buffer_entry(stbir__info* stbir_info, int n)1425+{1426+ int ring_buffer_index;1427+ float* ring_buffer;1428+1429+ 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+ }1439+1440+ 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);1442+1443+ stbir_info->ring_buffer_last_scanline = n;1444+1445+ return ring_buffer;1446+}1447+1448+1449+static 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;1459+1460+ for (x = 0; x < output_w; x++)1461+ {1462+ int n0 = horizontal_contributors[x].n0;1463+ int n1 = horizontal_contributors[x].n1;1464+1465+ int out_pixel_index = x * channels;1466+ int coefficient_group = coefficient_width * x;1467+ int coefficient_counter = 0;1468+1469+ 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);1474+1475+ 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+}1532+1533+static 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;1546+1547+ STBIR__DEBUG_ASSERT(!stbir__use_width_upsampling(stbir_info));1548+1549+ 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;1555+1556+ 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;1560+1561+ 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;1570+1571+ 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;1576+1577+ 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;1581+1582+ 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;1592+1593+ 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;1598+1599+ 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;1603+1604+ 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;1615+1616+ 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;1621+1622+ 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;1626+1627+ 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;1639+1640+ default:1641+ for (x = 0; x < max_x; x++)1642+ {1643+ int n0 = horizontal_contributors[x].n0;1644+ int n1 = horizontal_contributors[x].n1;1645+1646+ 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;1650+1651+ 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+}1664+1665+static 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);1669+1670+ // 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));1675+1676+ // Now it's sitting in the ring buffer ready to be used as source for the vertical sampling.1677+}1678+1679+static 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);1683+1684+ memset(stbir_info->horizontal_buffer, 0, stbir_info->output_w * stbir_info->channels * sizeof(float));1685+1686+ // 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);1691+1692+ // Now it's sitting in the horizontal buffer ready to be distributed into the ring buffers.1693+}1694+1695+// Get the specified scan line from the ring buffer.1696+static 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+}1701+1702+1703+static 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];1709+1710+ if (!(stbir_info->flags&STBIR_FLAG_ALPHA_PREMULTIPLIED))1711+ {1712+ for (x=0; x < num_pixels; ++x)1713+ {1714+ int pixel_index = x*channels;1715+1716+ float alpha = encode_buffer[pixel_index + alpha_channel];1717+ float reciprocal_alpha = alpha ? 1.0f / alpha : 0;1718+1719+ // 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;1723+1724+ // 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+ }1730+1731+ // 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;1736+1737+ #define STBIR__ROUND_INT(f) ((int) ((f)+0.5))1738+ #define STBIR__ROUND_UINT(f) ((stbir_uint32) ((f)+0.5))1739+1740+ #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+ #endif1747+1748+ 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;1754+1755+ 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;1762+1763+ case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_SRGB):1764+ for (x=0; x < num_pixels; ++x)1765+ {1766+ int pixel_index = x*channels;1767+1768+ 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+ }1773+1774+ 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;1778+1779+ case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR):1780+ for (x=0; x < num_pixels; ++x)1781+ {1782+ int pixel_index = x*channels;1783+1784+ 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;1791+1792+ case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB):1793+ for (x=0; x < num_pixels; ++x)1794+ {1795+ int pixel_index = x*channels;1796+1797+ 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+ }1802+1803+ 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+ }1806+1807+ break;1808+1809+ case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR):1810+ for (x=0; x < num_pixels; ++x)1811+ {1812+ int pixel_index = x*channels;1813+1814+ 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;1821+1822+ case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB):1823+ for (x=0; x < num_pixels; ++x)1824+ {1825+ int pixel_index = x*channels;1826+1827+ 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+ }1832+1833+ 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;1837+1838+ case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR):1839+ for (x=0; x < num_pixels; ++x)1840+ {1841+ int pixel_index = x*channels;1842+1843+ 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;1850+1851+ case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB):1852+ for (x=0; x < num_pixels; ++x)1853+ {1854+ int pixel_index = x*channels;1855+1856+ 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+ }1861+1862+ 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;1866+1867+ default:1868+ STBIR__UNIMPLEMENTED("Unknown type/colorspace/channels combination.");1869+ break;1870+ }1871+}1872+1873+static 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;1890+1891+ 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);1896+1897+ int n0,n1, output_row_start;1898+ int coefficient_group = coefficient_width * contributor;1899+1900+ n0 = vertical_contributors[contributor].n0;1901+ n1 = vertical_contributors[contributor].n1;1902+1903+ output_row_start = n * stbir_info->output_stride_bytes;1904+1905+ STBIR__DEBUG_ASSERT(stbir__use_height_upsampling(stbir_info));1906+1907+ memset(encode_buffer, 0, output_w * sizeof(float) * channels);1908+1909+ // I tried reblocking this for better cache usage of encode_buffer1910+ // (using x_outer, k, x_inner), but it lost speed. -- stb1911+1912+ 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+}1990+1991+static 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;2004+2005+ 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;2011+2012+ n0 = vertical_contributors[contributor].n0;2013+ n1 = vertical_contributors[contributor].n1;2014+2015+ STBIR__DEBUG_ASSERT(!stbir__use_height_upsampling(stbir_info));2016+2017+ 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];2022+2023+ 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);2024+2025+ 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;2064+2065+ 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+}2073+2074+static 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;2079+2080+ STBIR__DEBUG_ASSERT(stbir__use_height_upsampling(stbir_info));2081+2082+ 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;2086+2087+ 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);2088+2089+ STBIR__DEBUG_ASSERT(in_last_scanline - in_first_scanline <= stbir_info->vertical_filter_pixel_width);2090+2091+ 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+ }2112+2113+ // Load in new ones.2114+ if (stbir_info->ring_buffer_begin_index < 0)2115+ stbir__decode_and_resample_upsample(stbir_info, in_first_scanline);2116+2117+ 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);2119+2120+ // 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);2122+2123+ STBIR_PROGRESS_REPORT((float)y / stbir_info->output_h);2124+ }2125+}2126+2127+static 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);2137+2138+ float* ring_buffer = stbir_info->ring_buffer;2139+ int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float);2140+2141+ 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+ }2153+2154+ 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+}2171+2172+static 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;2180+2181+ STBIR__DEBUG_ASSERT(!stbir__use_height_upsampling(stbir_info));2182+2183+ 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;2187+2188+ 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);2189+2190+ STBIR__DEBUG_ASSERT(out_last_scanline - out_first_scanline <= stbir_info->vertical_filter_pixel_width);2191+2192+ if (out_last_scanline < 0 || out_first_scanline >= output_h)2193+ continue;2194+2195+ stbir__empty_ring_buffer(stbir_info, out_first_scanline);2196+2197+ stbir__decode_and_resample_downsample(stbir_info, y);2198+2199+ // 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);2202+2203+ 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);2205+2206+ // 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+ }2209+2210+ stbir__empty_ring_buffer(stbir_info, stbir_info->output_h);2211+}2212+2213+static 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+}2221+2222+static 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;2228+2229+ 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);2240+2241+ info->horizontal_shift = s0 * info->input_w / (s1 - s0);2242+ info->vertical_shift = t0 * info->input_h / (t1 - t0);2243+ }2244+}2245+2246+static 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+}2255+2256+static 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);2260+2261+ 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);2263+2264+ 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);2272+2273+ 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 late2277+2278+ 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;2287+2288+ 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+}2293+2294+static 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);2302+2303+ 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];2305+2306+#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];2312+2313+ 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+#endif2319+2320+ STBIR_ASSERT(info->channels >= 0);2321+ STBIR_ASSERT(info->channels <= STBIR_MAX_CHANNELS);2322+2323+ if (info->channels < 0 || info->channels > STBIR_MAX_CHANNELS)2324+ return 0;2325+2326+ 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));2328+2329+ 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;2333+2334+ if (alpha_channel < 0)2335+ flags |= STBIR_FLAG_ALPHA_USES_COLORSPACE | STBIR_FLAG_ALPHA_PREMULTIPLIED;2336+2337+ if (!(flags&STBIR_FLAG_ALPHA_USES_COLORSPACE) || !(flags&STBIR_FLAG_ALPHA_PREMULTIPLIED))2338+ STBIR_ASSERT(alpha_channel >= 0 && alpha_channel < info->channels);2339+2340+ if (alpha_channel >= info->channels)2341+ return 0;2342+2343+ STBIR_ASSERT(tempmem);2344+2345+ if (!tempmem)2346+ return 0;2347+2348+ STBIR_ASSERT(tempmem_size_in_bytes >= memory_required);2349+2350+ if (tempmem_size_in_bytes < memory_required)2351+ return 0;2352+2353+ memset(tempmem, 0, tempmem_size_in_bytes);2354+2355+ info->input_data = input_data;2356+ info->input_stride_bytes = width_stride_input;2357+2358+ info->output_data = output_data;2359+ info->output_stride_bytes = width_stride_output;2360+2361+ 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;2367+2368+ 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 );2374+2375+ 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;2377+2378+#define STBIR__NEXT_MEMPTR(current, newtype) (newtype*)(((unsigned char*)current) + current##_size)2379+2380+ 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);2385+2386+ 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);2391+2392+ 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;2399+2400+ STBIR__DEBUG_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->ring_buffer, unsigned char) == (size_t)tempmem + tempmem_size_in_bytes);2401+ }2402+2403+#undef STBIR__NEXT_MEMPTR2404+2405+ // This signals that the ring buffer is empty2406+ info->ring_buffer_begin_index = -1;2407+2408+ 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);2410+2411+ STBIR_PROGRESS_REPORT(0);2412+2413+ if (stbir__use_height_upsampling(info))2414+ stbir__buffer_loop_upsample(info);2415+ else2416+ stbir__buffer_loop_downsample(info);2417+2418+ STBIR_PROGRESS_REPORT(1);2419+2420+#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+#endif2426+2427+ return 1;2428+}2429+2430+2431+static 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;2444+2445+ 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);2450+2451+ if (!extra_memory)2452+ return 0;2453+2454+ 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);2459+2460+ STBIR_FREE(extra_memory, alloc_context);2461+2462+ return result;2463+}2464+2465+STBIRDEF 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+}2474+2475+STBIRDEF 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+}2484+2485+STBIRDEF 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+}2494+2495+STBIRDEF 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+}2505+2506+STBIRDEF 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+}2517+2518+STBIRDEF 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+}2529+2530+2531+STBIRDEF 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+}2542+2543+2544+STBIRDEF 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+}2557+2558+2559+STBIRDEF 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+}2579+2580+STBIRDEF 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+}2594+2595+#endif // STB_IMAGE_RESIZE_IMPLEMENTATIONmodified:stb_image.h+6 added-6 removed
764 unmodified lines765 {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 lines863 864 //////////////////////////////////////////////////////////////////////////////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 lines1610 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;1612 1613 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 lines1635 1636 return 1;1637 }1638 1639-// 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 lines2024 stbi_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 }2031 2032 static int stbi__zbuild_huffman(stbi__zhuffman *z, stbi_uc *sizelist, int num)2655 unmodified linesmodified: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 string23 unmodified lines29 //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 lines43 // 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 define91 unmodified lines143 // 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 page767 unmodified lines922 }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;933 934+#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;951 952+#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;960 961 962+#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 lines971 state->has_preferred_x = 0;972 break;973 }974 975+#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 lines984 state->has_preferred_x = 0;985 break;986 }987 988+#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 lines997 state->has_preferred_x = 0;998 break;999 }1000 1001+#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