old_image_resize.h
This file
Path
stb_image_resize_test/old_image_resize.h
2,738 lines · 116.6 KiB · C
Last changed by
Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d
History of this fileTake it
1/* stb_image_resize - v0.96 - public domain image resizing2 by Jorge L Rodriguez (@VinoBS) - 20143 http://github.com/nothings/stb45 Written with emphasis on usability, portability, and efficiency. (No6 SIMD or threads, so it be easily outperformed by libs that use those.)7 Only scaling and translation is supported, no rotations or shears.8 Easy API downsamples w/Mitchell filter, upsamples w/cubic interpolation.910 COMPILING & LINKING11 In one C/C++ file that #includes this file, do this:12 #define STB_IMAGE_RESIZE_IMPLEMENTATION13 before the #include. That will create the implementation in that file.1415 QUICKSTART16 stbir_resize_uint8( input_pixels , in_w , in_h , 0,17 output_pixels, out_w, out_h, 0, num_channels)18 stbir_resize_float(...)19 stbir_resize_uint8_srgb( input_pixels , in_w , in_h , 0,20 output_pixels, out_w, out_h, 0,21 num_channels , alpha_chan , 0)22 stbir_resize_uint8_srgb_edgemode(23 input_pixels , in_w , in_h , 0, 24 output_pixels, out_w, out_h, 0, 25 num_channels , alpha_chan , 0, STBIR_EDGE_CLAMP)26 // WRAP/REFLECT/ZERO2728 FULL API29 See the "header file" section of the source for API documentation.3031 ADDITIONAL DOCUMENTATION3233 SRGB & FLOATING POINT REPRESENTATION34 The sRGB functions presume IEEE floating point. If you do not have35 IEEE floating point, define STBIR_NON_IEEE_FLOAT. This will use36 a slower implementation.3738 MEMORY ALLOCATION39 The resize functions here perform a single memory allocation using40 malloc. To control the memory allocation, before the #include that41 triggers the implementation, do:4243 #define STBIR_MALLOC(size,context) ...44 #define STBIR_FREE(ptr,context) ...4546 Each resize function makes exactly one call to malloc/free, so to use47 temp memory, store the temp memory in the context and return that.4849 ASSERT50 Define STBIR_ASSERT(boolval) to override assert() and not use assert.h5152 OPTIMIZATION53 Define STBIR_SATURATE_INT to compute clamp values in-range using54 integer operations instead of float operations. This may be faster55 on some platforms.5657 DEFAULT FILTERS58 For functions which don't provide explicit control over what filters59 to use, you can change the compile-time defaults with6061 #define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_something62 #define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_something6364 See stbir_filter in the header-file section for the list of filters.6566 NEW FILTERS67 A number of 1D filter kernels are used. For a list of68 supported filters see the stbir_filter enum. To add a new filter,69 write a filter function and add it to stbir__filter_info_table.7071 PROGRESS72 For interactive use with slow resize operations, you can install73 a progress-report callback:7475 #define STBIR_PROGRESS_REPORT(val) some_func(val)7677 The parameter val is a float which goes from 0 to 1 as progress is made.7879 For example:8081 static void my_progress_report(float progress);82 #define STBIR_PROGRESS_REPORT(val) my_progress_report(val)8384 #define STB_IMAGE_RESIZE_IMPLEMENTATION85 #include "stb_image_resize.h"8687 static void my_progress_report(float progress)88 {89 printf("Progress: %f%%\n", progress*100);90 }9192 MAX CHANNELS93 If your image has more than 64 channels, define STBIR_MAX_CHANNELS94 to the max you'll have.9596 ALPHA CHANNEL97 Most of the resizing functions provide the ability to control how98 the alpha channel of an image is processed. The important things99 to know about this:100101 1. The best mathematically-behaved version of alpha to use is102 called "premultiplied alpha", in which the other color channels103 have had the alpha value multiplied in. If you use premultiplied104 alpha, linear filtering (such as image resampling done by this105 library, or performed in texture units on GPUs) does the "right106 thing". While premultiplied alpha is standard in the movie CGI107 industry, it is still uncommon in the videogame/real-time world.108109 If you linearly filter non-premultiplied alpha, strange effects110 occur. (For example, the 50/50 average of 99% transparent bright green111 and 1% transparent black produces 50% transparent dark green when112 non-premultiplied, whereas premultiplied it produces 50%113 transparent near-black. The former introduces green energy114 that doesn't exist in the source image.)115116 2. Artists should not edit premultiplied-alpha images; artists117 want non-premultiplied alpha images. Thus, art tools generally output118 non-premultiplied alpha images.119120 3. You will get best results in most cases by converting images121 to premultiplied alpha before processing them mathematically.122123 4. If you pass the flag STBIR_FLAG_ALPHA_PREMULTIPLIED, the124 resizer does not do anything special for the alpha channel;125 it is resampled identically to other channels. This produces126 the correct results for premultiplied-alpha images, but produces127 less-than-ideal results for non-premultiplied-alpha images.128129 5. If you do not pass the flag STBIR_FLAG_ALPHA_PREMULTIPLIED,130 then the resizer weights the contribution of input pixels131 based on their alpha values, or, equivalently, it multiplies132 the alpha value into the color channels, resamples, then divides133 by the resultant alpha value. Input pixels which have alpha=0 do134 not contribute at all to output pixels unless _all_ of the input135 pixels affecting that output pixel have alpha=0, in which case136 the result for that pixel is the same as it would be without137 STBIR_FLAG_ALPHA_PREMULTIPLIED. However, this is only true for138 input images in integer formats. For input images in float format,139 input pixels with alpha=0 have no effect, and output pixels140 which have alpha=0 will be 0 in all channels. (For float images,141 you can manually achieve the same result by adding a tiny epsilon142 value to the alpha channel of every image, and then subtracting143 or clamping it at the end.)144145 6. You can suppress the behavior described in #5 and make146 all-0-alpha pixels have 0 in all channels by #defining147 STBIR_NO_ALPHA_EPSILON.148149 7. You can separately control whether the alpha channel is150 interpreted as linear or affected by the colorspace. By default151 it is linear; you almost never want to apply the colorspace.152 (For example, graphics hardware does not apply sRGB conversion153 to the alpha channel.)154155 CONTRIBUTORS156 Jorge L Rodriguez: Implementation157 Sean Barrett: API design, optimizations158 Aras Pranckevicius: bugfix159 Nathan Reed: warning fixes160161 REVISIONS162 0.96 (2019-03-04) fixed warnings163 0.95 (2017-07-23) fixed warnings164 0.94 (2017-03-18) fixed warnings165 0.93 (2017-03-03) fixed bug with certain combinations of heights166 0.92 (2017-01-02) fix integer overflow on large (>2GB) images167 0.91 (2016-04-02) fix warnings; fix handling of subpixel regions168 0.90 (2014-09-17) first released version169170 LICENSE171 See end of file for license information.172173 TODO174 Don't decode all of the image data when only processing a partial tile175 Don't use full-width decode buffers when only processing a partial tile176 When processing wide images, break processing into tiles so data fits in L1 cache177 Installable filters?178 Resize that respects alpha test coverage179 (Reference code: FloatImage::alphaTestCoverage and FloatImage::scaleAlphaToCoverage:180 https://code.google.com/p/nvidia-texture-tools/source/browse/trunk/src/nvimage/FloatImage.cpp )181*/182183#ifndef STBIR_INCLUDE_STB_IMAGE_RESIZE_H184#define STBIR_INCLUDE_STB_IMAGE_RESIZE_H185186#ifdef _MSC_VER187typedef unsigned char stbir_uint8;188typedef unsigned short stbir_uint16;189typedef unsigned int stbir_uint32;190typedef unsigned __int64 stbir_uint64;191#else192#include <stdint.h>193typedef uint8_t stbir_uint8;194typedef uint16_t stbir_uint16;195typedef uint32_t stbir_uint32;196typedef uint64_t stbir_uint64;197#endif198199#ifndef STBIRDEF200#ifdef STB_IMAGE_RESIZE_STATIC201#define STBIRDEF static202#else203#ifdef __cplusplus204#define STBIRDEF extern "C"205#else206#define STBIRDEF extern207#endif208#endif209#endif210211//////////////////////////////////////////////////////////////////////////////212//213// Easy-to-use API:214//215// * "input pixels" points to an array of image data with 'num_channels' channels (e.g. RGB=3, RGBA=4)216// * input_w is input image width (x-axis), input_h is input image height (y-axis)217// * stride is the offset between successive rows of image data in memory, in bytes. you can218// specify 0 to mean packed continuously in memory219// * alpha channel is treated identically to other channels.220// * colorspace is linear or sRGB as specified by function name221// * returned result is 1 for success or 0 in case of an error.222// #define STBIR_ASSERT() to trigger an assert on parameter validation errors.223// * Memory required grows approximately linearly with input and output size, but with224// discontinuities at input_w == output_w and input_h == output_h.225// * These functions use a "default" resampling filter defined at compile time. To change the filter,226// you can change the compile-time defaults by #defining STBIR_DEFAULT_FILTER_UPSAMPLE227// and STBIR_DEFAULT_FILTER_DOWNSAMPLE, or you can use the medium-complexity API.228229STBIRDEF int stbir_resize_uint8( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,230 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,231 int num_channels);232233STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,234 float *output_pixels, int output_w, int output_h, int output_stride_in_bytes,235 int num_channels);236237238// The following functions interpret image data as gamma-corrected sRGB. 239// Specify STBIR_ALPHA_CHANNEL_NONE if you have no alpha channel,240// or otherwise provide the index of the alpha channel. Flags value241// of 0 will probably do the right thing if you're not sure what242// the flags mean.243244#define STBIR_ALPHA_CHANNEL_NONE -1245246// Set this flag if your texture has premultiplied alpha. Otherwise, stbir will247// use alpha-weighted resampling (effectively premultiplying, resampling,248// then unpremultiplying).249#define STBIR_FLAG_ALPHA_PREMULTIPLIED (1 << 0)250// The specified alpha channel should be handled as gamma-corrected value even251// when doing sRGB operations.252#define STBIR_FLAG_ALPHA_USES_COLORSPACE (1 << 1)253254#define STBIR_FLAG_ALPHA_OUT_PREMULTIPLIED (1 << 2)255256STBIRDEF int stbir_resize_uint8_srgb(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,257 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,258 int num_channels, int alpha_channel, int flags);259260261typedef enum262{263 STBIR_EDGE_CLAMP = 1,264 STBIR_EDGE_REFLECT = 2,265 STBIR_EDGE_WRAP = 3,266 STBIR_EDGE_ZERO = 4,267} stbir_edge;268269// This function adds the ability to specify how requests to sample off the edge of the image are handled.270STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,271 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,272 int num_channels, int alpha_channel, int flags,273 stbir_edge edge_wrap_mode);274275//////////////////////////////////////////////////////////////////////////////276//277// Medium-complexity API278//279// This extends the easy-to-use API as follows:280//281// * Alpha-channel can be processed separately282// * If alpha_channel is not STBIR_ALPHA_CHANNEL_NONE283// * Alpha channel will not be gamma corrected (unless flags&STBIR_FLAG_GAMMA_CORRECT)284// * Filters will be weighted by alpha channel (unless flags&STBIR_FLAG_ALPHA_PREMULTIPLIED)285// * Filter can be selected explicitly286// * uint16 image type287// * sRGB colorspace available for all types288// * context parameter for passing to STBIR_MALLOC289290typedef enum291{292 STBIR_FILTER_DEFAULT = 0, // use same filter type that easy-to-use API chooses293 STBIR_FILTER_BOX = 1, // A trapezoid w/1-pixel wide ramps, same result as box for integer scale ratios294 STBIR_FILTER_TRIANGLE = 2, // On upsampling, produces same results as bilinear texture filtering295 STBIR_FILTER_CUBICBSPLINE = 3, // The cubic b-spline (aka Mitchell-Netrevalli with B=1,C=0), gaussian-esque296 STBIR_FILTER_CATMULLROM = 4, // An interpolating cubic spline297 STBIR_FILTER_MITCHELL = 5, // Mitchell-Netrevalli filter with B=1/3, C=1/3298} stbir_filter;299300typedef enum301{302 STBIR_COLORSPACE_LINEAR,303 STBIR_COLORSPACE_SRGB,304305 STBIR_MAX_COLORSPACES,306} stbir_colorspace;307308// The following functions are all identical except for the type of the image data309310STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,311 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,312 int num_channels, int alpha_channel, int flags,313 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 314 void *alloc_context);315316STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int input_w , int input_h , int input_stride_in_bytes,317 stbir_uint16 *output_pixels , int output_w, int output_h, int output_stride_in_bytes,318 int num_channels, int alpha_channel, int flags,319 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 320 void *alloc_context);321322STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,323 float *output_pixels , int output_w, int output_h, int output_stride_in_bytes,324 int num_channels, int alpha_channel, int flags,325 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 326 void *alloc_context);327328329330//////////////////////////////////////////////////////////////////////////////331//332// Full-complexity API333//334// This extends the medium API as follows:335//336// * uint32 image type337// * not typesafe338// * separate filter types for each axis339// * separate edge modes for each axis340// * can specify scale explicitly for subpixel correctness341// * can specify image source tile using texture coordinates342343typedef enum344{345 STBIR_TYPE_UINT8 ,346 STBIR_TYPE_UINT16,347 STBIR_TYPE_FLOAT ,348 STBIR_TYPE_UINT32,349350 STBIR_MAX_TYPES351} stbir_datatype;352353STBIRDEF int stbir_resize( 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);360361STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,362 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,363 stbir_datatype datatype,364 int num_channels, int alpha_channel, int flags,365 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, 366 stbir_filter filter_horizontal, stbir_filter filter_vertical,367 stbir_colorspace space, void *alloc_context,368 float x_scale, float y_scale,369 float x_offset, float y_offset);370371STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,372 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,373 stbir_datatype datatype,374 int num_channels, int alpha_channel, int flags,375 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, 376 stbir_filter filter_horizontal, stbir_filter filter_vertical,377 stbir_colorspace space, void *alloc_context,378 float s0, float t0, float s1, float t1);379// (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.380381//382//383//// end header file /////////////////////////////////////////////////////384#endif // STBIR_INCLUDE_STB_IMAGE_RESIZE_H385386387388389390#ifdef STB_IMAGE_RESIZE_IMPLEMENTATION391392#ifndef STBIR_ASSERT393#include <assert.h>394#define STBIR_ASSERT(x) assert(x)395#endif396397// For memset398#include <string.h>399400#include <math.h>401402#ifndef STBIR_MALLOC403#include <stdlib.h>404// use comma operator to evaluate c, to avoid "unused parameter" warnings405#define STBIR_MALLOC(size,c) ((void)(c), malloc(size))406#define STBIR_FREE(ptr,c) ((void)(c), free(ptr))407#endif408409#ifndef _MSC_VER410#ifdef __cplusplus411#define stbir__inline inline412#else413#define stbir__inline414#endif415#else416#define stbir__inline __forceinline417#endif418419#ifdef STBIR_PROFILE420421union422{423 struct { stbir_uint64 total, setup, filters, looping, vertical, horizontal, decode, encode, alpha, unalpha; } named;424 stbir_uint64 array[10];425} oldprofile;426stbir_uint64 * current_zone_excluded_ptr;427428#if defined(_x86_64) || defined( __x86_64__ ) || defined( _M_X64 ) || defined(__x86_64) || defined(__SSE2__) || defined(STBIR_SSE) || defined( _M_IX86_FP ) || defined(__i386) || defined( __i386__ ) || defined( _M_IX86 ) || defined( _X86_ )429430#ifdef _MSC_VER431432 STBIRDEF stbir_uint64 __rdtsc();433 #define STBIR_PROFILE_FUNC() __rdtsc()434435#else // non msvc436437 static stbir__inline stbir_uint64 STBIR_PROFILE_FUNC() 438 {439 stbir_uint32 lo, hi;440 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi) );441 return ( ( (stbir_uint64) hi ) << 32 ) | ( (stbir_uint64) lo );442 }443444#endif // msvc445446#elif defined( _M_AMD64 ) || defined( __aarch64__ ) || defined( __arm64__ ) || defined(__ARM_NEON__) || defined(__ARM_NEON)447448#ifdef _MSC_VER449450 #error Not sure what the intrinsic for cntvct_el0 is on MSVC451452#else // no msvc453454 static stbir__inline stbir_uint64 STBIR_PROFILE_FUNC()455 {456 stbir_uint64 tsc;457 asm volatile("mrs %0, cntvct_el0" : "=r" (tsc));458 return tsc;459 }460461#endif462463#elif // x64, arm464465#error Unknown platform for profiling.466467#endif //x64 and 468469#define STBIR_PROFILE_START() { stbir_uint64 thiszonetime = STBIR_PROFILE_FUNC(); stbir_uint64 * save_parent_excluded_ptr = current_zone_excluded_ptr; stbir_uint64 current_zone_excluded = 0; current_zone_excluded_ptr = ¤t_zone_excluded; 470#define STBIR_PROFILE_END( wh ) thiszonetime = STBIR_PROFILE_FUNC() - thiszonetime; oldprofile.named.wh += thiszonetime - current_zone_excluded; *save_parent_excluded_ptr += thiszonetime; current_zone_excluded_ptr = save_parent_excluded_ptr; }471#define STBIR_PROFILE_FIRST_START() { int i; current_zone_excluded_ptr = &oldprofile.named.total; for(i=0;i<STBIR__ARRAY_SIZE(oldprofile.array);i++) oldprofile.array[i]=0; } STBIR_PROFILE_START();472473#else474475#define STBIR_PROFILE_START()476#define STBIR_PROFILE_END( wh )477#define STBIR_PROFILE_FIRST_START()478479#endif480481// should produce compiler error if size is wrong482typedef unsigned char stbir__validate_uint32[sizeof(stbir_uint32) == 4 ? 1 : -1];483484#ifdef _MSC_VER485#define STBIR__NOTUSED(v) (void)(v)486#else487#define STBIR__NOTUSED(v) (void)sizeof(v)488#endif489490#define STBIR__ARRAY_SIZE(a) (sizeof((a))/sizeof((a)[0]))491492#ifndef STBIR_DEFAULT_FILTER_UPSAMPLE493#define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_CATMULLROM494#endif495496#ifndef STBIR_DEFAULT_FILTER_DOWNSAMPLE497#define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_MITCHELL498#endif499500#ifndef STBIR_PROGRESS_REPORT501#define STBIR_PROGRESS_REPORT(float_0_to_1)502#endif503504#ifndef STBIR_MAX_CHANNELS505#define STBIR_MAX_CHANNELS 64506#endif507508#if STBIR_MAX_CHANNELS > 65536509#error "Too many channels; STBIR_MAX_CHANNELS must be no more than 65536."510// because we store the indices in 16-bit variables511#endif512513// This value is added to alpha just before premultiplication to avoid514// zeroing out color values. It is equivalent to 2^-80. If you don't want515// that behavior (it may interfere if you have floating point images with516// very small alpha values) then you can define STBIR_NO_ALPHA_EPSILON to517// disable it.518#ifndef STBIR_ALPHA_EPSILON519#define STBIR_ALPHA_EPSILON ((float)1 / (1 << 20) / (1 << 20) / (1 << 20) / (1 << 20))520#endif521522523524#ifdef _MSC_VER525#define STBIR__UNUSED_PARAM(v) (void)(v)526#else527#define STBIR__UNUSED_PARAM(v) (void)sizeof(v)528#endif529530// must match stbir_datatype531static unsigned char stbir__type_size[] = {532 1, // STBIR_TYPE_UINT8533 2, // STBIR_TYPE_UINT16534 4, // STBIR_TYPE_UINT32535 4, // STBIR_TYPE_FLOAT536};537538// Kernel function centered at 0539typedef float (stbir__kernel_fn)(float x, float scale);540typedef float (stbir__support_fn)(float scale);541542typedef struct543{544 stbir__kernel_fn* kernel;545 stbir__support_fn* support;546} stbir__filter_info;547548// When upsampling, the contributors are which source pixels contribute.549// When downsampling, the contributors are which destination pixels are contributed to.550typedef struct551{552 int n0; // First contributing pixel553 int n1; // Last contributing pixel554} stbir__contributors;555556typedef struct557{558 const void* input_data;559 int input_w;560 int input_h;561 int input_stride_bytes;562563 void* output_data;564 int output_w;565 int output_h;566 int output_stride_bytes;567568 float s0, t0, s1, t1;569570 float horizontal_shift; // Units: output pixels571 float vertical_shift; // Units: output pixels572 float horizontal_scale;573 float vertical_scale;574575 int channels;576 int alpha_channel;577 stbir_uint32 flags;578 stbir_datatype type;579 stbir_filter horizontal_filter;580 stbir_filter vertical_filter;581 stbir_edge edge_horizontal;582 stbir_edge edge_vertical;583 stbir_colorspace colorspace;584585 stbir__contributors* horizontal_contributors;586 float* horizontal_coefficients;587588 stbir__contributors* vertical_contributors;589 float* vertical_coefficients;590591 int decode_buffer_pixels;592 float* decode_buffer;593594 float* horizontal_buffer;595596 // cache these because ceil/floor are inexplicably showing up in profile597 int horizontal_coefficient_width;598 int vertical_coefficient_width;599 int horizontal_filter_pixel_width;600 int vertical_filter_pixel_width;601 int horizontal_filter_pixel_margin;602 int vertical_filter_pixel_margin;603 int horizontal_num_contributors;604 int vertical_num_contributors;605606 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)607 int ring_buffer_num_entries; // Total number of entries in the ring buffer.608 int ring_buffer_first_scanline;609 int ring_buffer_last_scanline;610 int ring_buffer_begin_index; // first_scanline is at this index in the ring buffer611 float* ring_buffer;612613 float* encode_buffer; // A temporary buffer to store floats so we don't lose precision while we do multiply-adds.614615 int horizontal_contributors_size;616 int horizontal_coefficients_size;617 int vertical_contributors_size;618 int vertical_coefficients_size;619 int decode_buffer_size;620 int horizontal_buffer_size;621 int ring_buffer_size;622 int encode_buffer_size;623} ostbir__info;624625626static const float stbir__max_uint8_as_float = 255.0f;627static const float stbir__max_uint16_as_float = 65535.0f;628static const double stbir__max_uint32_as_float = 4294967295.0;629630631static stbir__inline int stbir__min(int a, int b)632{633 return a < b ? a : b;634}635636static stbir__inline float stbir__saturate(float x)637{638 if (x < 0)639 return 0;640641 if (x > 1)642 return 1;643644 return x;645}646647#ifdef STBIR_SATURATE_INT648static stbir__inline stbir_uint8 stbir__saturate8(int x)649{650 if ((unsigned int) x <= 255)651 return (stbir_uint8) x;652653 if (x < 0)654 return 0;655656 return 255;657}658659static stbir__inline stbir_uint16 stbir__saturate16(int x)660{661 if ((unsigned int) x <= 65535)662 return (stbir_uint16) x;663664 if (x < 0)665 return 0;666667 return 65535;668}669#endif670671static float stbir__srgb_uchar_to_linear_float[256] = {672 0.000000f, 0.000304f, 0.000607f, 0.000911f, 0.001214f, 0.001518f, 0.001821f, 0.002125f, 0.002428f, 0.002732f, 0.003035f,673 0.003347f, 0.003677f, 0.004025f, 0.004391f, 0.004777f, 0.005182f, 0.005605f, 0.006049f, 0.006512f, 0.006995f, 0.007499f,674 0.008023f, 0.008568f, 0.009134f, 0.009721f, 0.010330f, 0.010960f, 0.011612f, 0.012286f, 0.012983f, 0.013702f, 0.014444f,675 0.015209f, 0.015996f, 0.016807f, 0.017642f, 0.018500f, 0.019382f, 0.020289f, 0.021219f, 0.022174f, 0.023153f, 0.024158f,676 0.025187f, 0.026241f, 0.027321f, 0.028426f, 0.029557f, 0.030713f, 0.031896f, 0.033105f, 0.034340f, 0.035601f, 0.036889f,677 0.038204f, 0.039546f, 0.040915f, 0.042311f, 0.043735f, 0.045186f, 0.046665f, 0.048172f, 0.049707f, 0.051269f, 0.052861f,678 0.054480f, 0.056128f, 0.057805f, 0.059511f, 0.061246f, 0.063010f, 0.064803f, 0.066626f, 0.068478f, 0.070360f, 0.072272f,679 0.074214f, 0.076185f, 0.078187f, 0.080220f, 0.082283f, 0.084376f, 0.086500f, 0.088656f, 0.090842f, 0.093059f, 0.095307f,680 0.097587f, 0.099899f, 0.102242f, 0.104616f, 0.107023f, 0.109462f, 0.111932f, 0.114435f, 0.116971f, 0.119538f, 0.122139f,681 0.124772f, 0.127438f, 0.130136f, 0.132868f, 0.135633f, 0.138432f, 0.141263f, 0.144128f, 0.147027f, 0.149960f, 0.152926f,682 0.155926f, 0.158961f, 0.162029f, 0.165132f, 0.168269f, 0.171441f, 0.174647f, 0.177888f, 0.181164f, 0.184475f, 0.187821f,683 0.191202f, 0.194618f, 0.198069f, 0.201556f, 0.205079f, 0.208637f, 0.212231f, 0.215861f, 0.219526f, 0.223228f, 0.226966f,684 0.230740f, 0.234551f, 0.238398f, 0.242281f, 0.246201f, 0.250158f, 0.254152f, 0.258183f, 0.262251f, 0.266356f, 0.270498f,685 0.274677f, 0.278894f, 0.283149f, 0.287441f, 0.291771f, 0.296138f, 0.300544f, 0.304987f, 0.309469f, 0.313989f, 0.318547f,686 0.323143f, 0.327778f, 0.332452f, 0.337164f, 0.341914f, 0.346704f, 0.351533f, 0.356400f, 0.361307f, 0.366253f, 0.371238f,687 0.376262f, 0.381326f, 0.386430f, 0.391573f, 0.396755f, 0.401978f, 0.407240f, 0.412543f, 0.417885f, 0.423268f, 0.428691f,688 0.434154f, 0.439657f, 0.445201f, 0.450786f, 0.456411f, 0.462077f, 0.467784f, 0.473532f, 0.479320f, 0.485150f, 0.491021f,689 0.496933f, 0.502887f, 0.508881f, 0.514918f, 0.520996f, 0.527115f, 0.533276f, 0.539480f, 0.545725f, 0.552011f, 0.558340f,690 0.564712f, 0.571125f, 0.577581f, 0.584078f, 0.590619f, 0.597202f, 0.603827f, 0.610496f, 0.617207f, 0.623960f, 0.630757f,691 0.637597f, 0.644480f, 0.651406f, 0.658375f, 0.665387f, 0.672443f, 0.679543f, 0.686685f, 0.693872f, 0.701102f, 0.708376f,692 0.715694f, 0.723055f, 0.730461f, 0.737911f, 0.745404f, 0.752942f, 0.760525f, 0.768151f, 0.775822f, 0.783538f, 0.791298f,693 0.799103f, 0.806952f, 0.814847f, 0.822786f, 0.830770f, 0.838799f, 0.846873f, 0.854993f, 0.863157f, 0.871367f, 0.879622f,694 0.887923f, 0.896269f, 0.904661f, 0.913099f, 0.921582f, 0.930111f, 0.938686f, 0.947307f, 0.955974f, 0.964686f, 0.973445f,695 0.982251f, 0.991102f, 1.0f696};697698static float stbir__srgb_to_linear(float f)699{700 if (f <= 0.04045f)701 return f / 12.92f;702 else703 return (float)pow((f + 0.055f) / 1.055f, 2.4f);704}705706static float stbir__linear_to_srgb(float f)707{708 if (f <= 0.0031308f)709 return f * 12.92f;710 else711 return 1.055f * (float)pow(f, 1 / 2.4f) - 0.055f;712}713714#ifndef STBIR_NON_IEEE_FLOAT715// From https://gist.github.com/rygorous/2203834716717typedef union718{719 stbir_uint32 u;720 float f;721} stbir__FP32;722723static const stbir_uint32 fp32_to_srgb8_tab4[104] = {724 0x0073000d, 0x007a000d, 0x0080000d, 0x0087000d, 0x008d000d, 0x0094000d, 0x009a000d, 0x00a1000d,725 0x00a7001a, 0x00b4001a, 0x00c1001a, 0x00ce001a, 0x00da001a, 0x00e7001a, 0x00f4001a, 0x0101001a,726 0x010e0033, 0x01280033, 0x01410033, 0x015b0033, 0x01750033, 0x018f0033, 0x01a80033, 0x01c20033,727 0x01dc0067, 0x020f0067, 0x02430067, 0x02760067, 0x02aa0067, 0x02dd0067, 0x03110067, 0x03440067,728 0x037800ce, 0x03df00ce, 0x044600ce, 0x04ad00ce, 0x051400ce, 0x057b00c5, 0x05dd00bc, 0x063b00b5,729 0x06970158, 0x07420142, 0x07e30130, 0x087b0120, 0x090b0112, 0x09940106, 0x0a1700fc, 0x0a9500f2,730 0x0b0f01cb, 0x0bf401ae, 0x0ccb0195, 0x0d950180, 0x0e56016e, 0x0f0d015e, 0x0fbc0150, 0x10630143,731 0x11070264, 0x1238023e, 0x1357021d, 0x14660201, 0x156601e9, 0x165a01d3, 0x174401c0, 0x182401af,732 0x18fe0331, 0x1a9602fe, 0x1c1502d2, 0x1d7e02ad, 0x1ed4028d, 0x201a0270, 0x21520256, 0x227d0240,733 0x239f0443, 0x25c003fe, 0x27bf03c4, 0x29a10392, 0x2b6a0367, 0x2d1d0341, 0x2ebe031f, 0x304d0300,734 0x31d105b0, 0x34a80555, 0x37520507, 0x39d504c5, 0x3c37048b, 0x3e7c0458, 0x40a8042a, 0x42bd0401,735 0x44c20798, 0x488e071e, 0x4c1c06b6, 0x4f76065d, 0x52a50610, 0x55ac05cc, 0x5892058f, 0x5b590559,736 0x5e0c0a23, 0x631c0980, 0x67db08f6, 0x6c55087f, 0x70940818, 0x74a007bd, 0x787d076c, 0x7c330723,737};738 739static stbir_uint8 stbir__linear_to_srgb_uchar(float in)740{741 static const stbir__FP32 almostone = { 0x3f7fffff }; // 1-eps742 static const stbir__FP32 minval = { (127-13) << 23 };743 stbir_uint32 tab,bias,scale,t;744 stbir__FP32 f;745 746 // Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively.747 // The tests are carefully written so that NaNs map to 0, same as in the reference748 // implementation.749 if (!(in > minval.f)) // written this way to catch NaNs750 in = minval.f;751 if (in > almostone.f)752 in = almostone.f;753 754 // Do the table lookup and unpack bias, scale755 f.f = in;756 tab = fp32_to_srgb8_tab4[(f.u - minval.u) >> 20];757 bias = (tab >> 16) << 9;758 scale = tab & 0xffff;759 760 // Grab next-highest mantissa bits and perform linear interpolation761 t = (f.u >> 12) & 0xff;762 return (unsigned char) ((bias + scale*t) >> 16);763}764765#else766// sRGB transition values, scaled by 1<<28767static int stbir__srgb_offset_to_linear_scaled[256] =768{769 0, 40738, 122216, 203693, 285170, 366648, 448125, 529603,770 611080, 692557, 774035, 855852, 942009, 1033024, 1128971, 1229926,771 1335959, 1447142, 1563542, 1685229, 1812268, 1944725, 2082664, 2226148,772 2375238, 2529996, 2690481, 2856753, 3028870, 3206888, 3390865, 3580856,773 3776916, 3979100, 4187460, 4402049, 4622919, 4850123, 5083710, 5323731,774 5570236, 5823273, 6082892, 6349140, 6622065, 6901714, 7188133, 7481369,775 7781466, 8088471, 8402427, 8723380, 9051372, 9386448, 9728650, 10078021,776 10434603, 10798439, 11169569, 11548036, 11933879, 12327139, 12727857, 13136073,777 13551826, 13975156, 14406100, 14844697, 15290987, 15745007, 16206795, 16676389,778 17153826, 17639142, 18132374, 18633560, 19142734, 19659934, 20185196, 20718552,779 21260042, 21809696, 22367554, 22933648, 23508010, 24090680, 24681686, 25281066,780 25888850, 26505076, 27129772, 27762974, 28404716, 29055026, 29713942, 30381490,781 31057708, 31742624, 32436272, 33138682, 33849884, 34569912, 35298800, 36036568,782 36783260, 37538896, 38303512, 39077136, 39859796, 40651528, 41452360, 42262316,783 43081432, 43909732, 44747252, 45594016, 46450052, 47315392, 48190064, 49074096,784 49967516, 50870356, 51782636, 52704392, 53635648, 54576432, 55526772, 56486700,785 57456236, 58435408, 59424248, 60422780, 61431036, 62449032, 63476804, 64514376,786 65561776, 66619028, 67686160, 68763192, 69850160, 70947088, 72053992, 73170912,787 74297864, 75434880, 76581976, 77739184, 78906536, 80084040, 81271736, 82469648,788 83677792, 84896192, 86124888, 87363888, 88613232, 89872928, 91143016, 92423512,789 93714432, 95015816, 96327688, 97650056, 98982952, 100326408, 101680440, 103045072,790 104420320, 105806224, 107202800, 108610064, 110028048, 111456776, 112896264, 114346544,791 115807632, 117279552, 118762328, 120255976, 121760536, 123276016, 124802440, 126339832,792 127888216, 129447616, 131018048, 132599544, 134192112, 135795792, 137410592, 139036528,793 140673648, 142321952, 143981456, 145652208, 147334208, 149027488, 150732064, 152447968,794 154175200, 155913792, 157663776, 159425168, 161197984, 162982240, 164777968, 166585184,795 168403904, 170234160, 172075968, 173929344, 175794320, 177670896, 179559120, 181458992,796 183370528, 185293776, 187228736, 189175424, 191133888, 193104112, 195086128, 197079968,797 199085648, 201103184, 203132592, 205173888, 207227120, 209292272, 211369392, 213458480,798 215559568, 217672656, 219797792, 221934976, 224084240, 226245600, 228419056, 230604656,799 232802400, 235012320, 237234432, 239468736, 241715280, 243974080, 246245120, 248528464,800 250824112, 253132064, 255452368, 257785040, 260130080, 262487520, 264857376, 267239664,801};802803static stbir_uint8 stbir__linear_to_srgb_uchar(float f)804{805 int x = (int) (f * (1 << 28)); // has headroom so you don't need to clamp806 int v = 0;807 int i;808809 // Refine the guess with a short binary search.810 i = v + 128; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;811 i = v + 64; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;812 i = v + 32; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;813 i = v + 16; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;814 i = v + 8; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;815 i = v + 4; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;816 i = v + 2; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;817 i = v + 1; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;818819 return (stbir_uint8) v;820}821#endif822823static float stbir__filter_trapezoid(float x, float scale)824{825 float halfscale = scale / 2;826 float t = 0.5f + halfscale;827 STBIR_ASSERT(scale <= 1);828829 x = (float)fabs(x);830831 if (x >= t)832 return 0;833 else834 {835 float r = 0.5f - halfscale;836 if (x <= r)837 return 1;838 else839 return (t - x) / scale;840 }841}842843static float stbir__support_trapezoid(float scale)844{845 STBIR_ASSERT(scale <= 1);846 return 0.5f + scale / 2;847}848849static float stbir__filter_triangle(float x, float s)850{851 STBIR__UNUSED_PARAM(s);852853 x = (float)fabs(x);854855 if (x <= 1.0f)856 return 1 - x;857 else858 return 0;859}860861static float stbir__filter_cubic(float x, float s)862{863 STBIR__UNUSED_PARAM(s);864865 x = (float)fabs(x);866867 if (x < 1.0f)868 return (4 + x*x*(3*x - 6))/6;869 else if (x < 2.0f)870 return (8 + x*(-12 + x*(6 - x)))/6;871872 return (0.0f);873}874875static float stbir__filter_catmullrom(float x, float s)876{877 STBIR__UNUSED_PARAM(s);878879 x = (float)fabs(x);880881 if (x < 1.0f)882 return 1 - x*x*(2.5f - 1.5f*x);883 else if (x < 2.0f)884 return 2 - x*(4 + x*(0.5f*x - 2.5f));885886 return (0.0f);887}888889static float stbir__filter_mitchell(float x, float s)890{891 STBIR__UNUSED_PARAM(s);892893 x = (float)fabs(x);894895 if (x < 1.0f)896 return (16 + x*x*(21 * x - 36))/18;897 else if (x < 2.0f)898 return (32 + x*(-60 + x*(36 - 7*x)))/18;899900 return (0.0f);901}902903static float stbir__support_zero(float s)904{905 STBIR__UNUSED_PARAM(s);906 return 0;907}908909static float stbir__support_one(float s)910{911 STBIR__UNUSED_PARAM(s);912 return 1;913}914915static float stbir__support_two(float s)916{917 STBIR__UNUSED_PARAM(s);918 return 2;919}920921static stbir__filter_info stbir__filter_info_table[] = {922 { NULL, stbir__support_zero },923 { stbir__filter_trapezoid, stbir__support_trapezoid },924 { stbir__filter_triangle, stbir__support_one },925 { stbir__filter_cubic, stbir__support_two },926 { stbir__filter_catmullrom, stbir__support_two },927 { stbir__filter_mitchell, stbir__support_two },928};929930stbir__inline static int stbir__use_upsampling(float ratio)931{932 return ratio > 1;933}934935stbir__inline static int stbir__use_width_upsampling(ostbir__info* stbir_info)936{937 return stbir__use_upsampling(stbir_info->horizontal_scale);938}939940stbir__inline static int stbir__use_height_upsampling(ostbir__info* stbir_info)941{942 return stbir__use_upsampling(stbir_info->vertical_scale);943}944945// This is the maximum number of input samples that can affect an output sample946// with the given filter947static int stbir__get_filter_pixel_width(stbir_filter filter, float scale)948{949 STBIR_ASSERT(filter != 0);950 STBIR_ASSERT(filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));951952 if (stbir__use_upsampling(scale))953 return (int)ceil(stbir__filter_info_table[filter].support(1/scale) * 2);954 else955 return (int)ceil(stbir__filter_info_table[filter].support(scale) * 2 / scale);956}957958// This is how much to expand buffers to account for filters seeking outside959// the image boundaries.960static int stbir__get_filter_pixel_margin(stbir_filter filter, float scale)961{962 return stbir__get_filter_pixel_width(filter, scale) / 2;963}964965static int stbir__get_coefficient_width(stbir_filter filter, float scale)966{967 if (stbir__use_upsampling(scale))968 return (int)ceil(stbir__filter_info_table[filter].support(1 / scale) * 2);969 else970 return (int)ceil(stbir__filter_info_table[filter].support(scale) * 2);971}972973static int stbir__get_contributors(float scale, stbir_filter filter, int input_size, int output_size)974{975 if (stbir__use_upsampling(scale))976 return output_size;977 else978 return (input_size + stbir__get_filter_pixel_margin(filter, scale) * 2);979}980981static int stbir__get_total_horizontal_coefficients(ostbir__info* info)982{983 return info->horizontal_num_contributors984 * stbir__get_coefficient_width (info->horizontal_filter, info->horizontal_scale);985}986987static int stbir__get_total_vertical_coefficients(ostbir__info* info)988{989 return info->vertical_num_contributors990 * stbir__get_coefficient_width (info->vertical_filter, info->vertical_scale);991}992993static stbir__contributors* stbir__get_contributor(stbir__contributors* contributors, int n)994{995 return &contributors[n];996}997998// For perf reasons this code is duplicated in stbir__resample_horizontal_upsample/downsample,999// if you change it here change it there too.1000static float* stbir__get_coefficient(float* coefficients, stbir_filter filter, float scale, int n, int c)1001{1002 int width = stbir__get_coefficient_width(filter, scale);1003 return &coefficients[width*n + c];1004}10051006static int stbir__edge_wrap_slow(stbir_edge edge, int n, int max)1007{1008 switch (edge)1009 {1010 case STBIR_EDGE_ZERO:1011 return 0; // we'll decode the wrong pixel here, and then overwrite with 0s later10121013 case STBIR_EDGE_CLAMP:1014 if (n < 0)1015 return 0;10161017 if (n >= max)1018 return max - 1;10191020 return n; // NOTREACHED10211022 case STBIR_EDGE_REFLECT:1023 {1024 if (n < 0)1025 {1026 if (n > -max)1027 return -n;1028 else1029 return max - 1;1030 }10311032 if (n >= max)1033 {1034 int max2 = max * 2;1035 if (n >= max2)1036 return 0;1037 else1038 return max2 - n - 1;1039 }10401041 return n; // NOTREACHED1042 }10431044 case STBIR_EDGE_WRAP:1045 if (n >= 0)1046 return (n % max);1047 else1048 {1049 int m = (-n) % max;10501051 if (m != 0)1052 m = max - m;10531054 return (m);1055 }1056 // NOTREACHED10571058 default:1059 STBIR_ASSERT(!"Unimplemented edge type");1060 return 0;1061 }1062}10631064stbir__inline static int stbir__edge_wrap(stbir_edge edge, int n, int max)1065{1066 // avoid per-pixel switch1067 if (n >= 0 && n < max)1068 return n;1069 return stbir__edge_wrap_slow(edge, n, max);1070}10711072// What input pixels contribute to this output pixel?1073static 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)1074{1075 float out_pixel_center = (float)n + 0.5f;1076 float out_pixel_influence_lowerbound = out_pixel_center - out_filter_radius;1077 float out_pixel_influence_upperbound = out_pixel_center + out_filter_radius;10781079 float in_pixel_influence_lowerbound = (out_pixel_influence_lowerbound + out_shift) / scale_ratio;1080 float in_pixel_influence_upperbound = (out_pixel_influence_upperbound + out_shift) / scale_ratio;10811082 *in_center_of_out = (out_pixel_center + out_shift) / scale_ratio;1083 *in_first_pixel = (int)(floor(in_pixel_influence_lowerbound + 0.5));1084 *in_last_pixel = (int)(floor(in_pixel_influence_upperbound - 0.5));1085}10861087// What output pixels does this input pixel contribute to?1088static 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)1089{1090 float in_pixel_center = (float)n + 0.5f;1091 float in_pixel_influence_lowerbound = in_pixel_center - in_pixels_radius;1092 float in_pixel_influence_upperbound = in_pixel_center + in_pixels_radius;10931094 float out_pixel_influence_lowerbound = in_pixel_influence_lowerbound * scale_ratio - out_shift;1095 float out_pixel_influence_upperbound = in_pixel_influence_upperbound * scale_ratio - out_shift;10961097 *out_center_of_in = in_pixel_center * scale_ratio - out_shift;1098 *out_first_pixel = (int)(floor(out_pixel_influence_lowerbound + 0.5));1099 *out_last_pixel = (int)(floor(out_pixel_influence_upperbound - 0.5));1100}11011102static void stbir__calculate_coefficients_upsample(stbir_filter filter, float scale, int in_first_pixel, int in_last_pixel, float in_center_of_out, stbir__contributors* contributor, float* coefficient_group)1103{1104 int i;1105 float total_filter = 0;1106 float filter_scale;11071108 STBIR_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.11091110 contributor->n0 = in_first_pixel;1111 contributor->n1 = in_last_pixel;11121113 STBIR_ASSERT(contributor->n1 >= contributor->n0);11141115 for (i = 0; i <= in_last_pixel - in_first_pixel; i++)1116 {1117 float in_pixel_center = (float)(i + in_first_pixel) + 0.5f;1118 coefficient_group[i] = stbir__filter_info_table[filter].kernel(in_center_of_out - in_pixel_center, 1 / scale);11191120 // If the coefficient is zero, skip it. (Don't do the <0 check here, we want the influence of those outside pixels.)1121 if (i == 0 && !coefficient_group[i])1122 {1123 contributor->n0 = ++in_first_pixel;1124 i--;1125 continue;1126 }11271128 total_filter += coefficient_group[i];1129 }11301131 STBIR_ASSERT(stbir__filter_info_table[filter].kernel((float)(in_last_pixel + 1) + 0.5f - in_center_of_out, 1/scale) == 0);11321133 STBIR_ASSERT(total_filter > 0.9);1134 STBIR_ASSERT(total_filter < 1.1f); // Make sure it's not way off.11351136 // Make sure the sum of all coefficients is 1.1137 filter_scale = 1 / total_filter;11381139 for (i = 0; i <= in_last_pixel - in_first_pixel; i++)1140 coefficient_group[i] *= filter_scale;11411142 for (i = in_last_pixel - in_first_pixel; i >= 0; i--)1143 {1144 if (coefficient_group[i])1145 break;11461147 // This line has no weight. We can skip it.1148 contributor->n1 = contributor->n0 + i - 1;1149 }1150}11511152static void stbir__calculate_coefficients_downsample(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)1153{1154 int i;11551156 STBIR_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.11571158 contributor->n0 = out_first_pixel;1159 contributor->n1 = out_last_pixel;11601161 STBIR_ASSERT(contributor->n1 >= contributor->n0);11621163 for (i = 0; i <= out_last_pixel - out_first_pixel; i++)1164 {1165 float out_pixel_center = (float)(i + out_first_pixel) + 0.5f;1166 float x = out_pixel_center - out_center_of_in;1167 coefficient_group[i] = stbir__filter_info_table[filter].kernel(x, scale_ratio) * scale_ratio;1168 }11691170 STBIR_ASSERT(stbir__filter_info_table[filter].kernel((float)(out_last_pixel + 1) + 0.5f - out_center_of_in, scale_ratio) == 0);11711172 for (i = out_last_pixel - out_first_pixel; i >= 0; i--)1173 {1174 if (coefficient_group[i])1175 break;11761177 // This line has no weight. We can skip it.1178 contributor->n1 = contributor->n0 + i - 1;1179 }1180}11811182static void stbir__normalize_downsample_coefficients(stbir__contributors* contributors, float* coefficients, stbir_filter filter, float scale_ratio, int input_size, int output_size)1183{1184 int num_contributors = stbir__get_contributors(scale_ratio, filter, input_size, output_size);1185 int num_coefficients = stbir__get_coefficient_width(filter, scale_ratio);1186 int i, j;1187 int skip;11881189 for (i = 0; i < output_size; i++)1190 {1191 float scale;1192 float total = 0;11931194 for (j = 0; j < num_contributors; j++)1195 {1196 if (i >= contributors[j].n0 && i <= contributors[j].n1)1197 {1198 float coefficient = *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i - contributors[j].n0);1199 total += coefficient;1200 }1201 else if (i < contributors[j].n0)1202 break;1203 }12041205 //STBIR_ASSERT(total > 0.9f);1206 //STBIR_ASSERT(total < 1.5f);12071208 scale = 1 / total;12091210 for (j = 0; j < num_contributors; j++)1211 {1212 if (i >= contributors[j].n0 && i <= contributors[j].n1)1213 *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i - contributors[j].n0) *= scale;1214 else if (i < contributors[j].n0)1215 break;1216 }1217 }12181219 // Optimize: Skip zero coefficients and contributions outside of image bounds.1220 // Do this after normalizing because normalization depends on the n0/n1 values.1221 for (j = 0; j < num_contributors; j++)1222 {1223 int range, max, width;12241225 skip = 0;1226 while (*stbir__get_coefficient(coefficients, filter, scale_ratio, j, skip) == 0)1227 skip++;12281229 contributors[j].n0 += skip;12301231 while (contributors[j].n0 < 0)1232 {1233 contributors[j].n0++;1234 skip++;1235 }12361237 range = contributors[j].n1 - contributors[j].n0 + 1;1238 max = stbir__min(num_coefficients, range);12391240 width = stbir__get_coefficient_width(filter, scale_ratio);1241 for (i = 0; i < max; i++)1242 {1243 if (i + skip >= width)1244 break;12451246 *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i) = *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i + skip);1247 }12481249 continue;1250 }12511252 // Using min to avoid writing into invalid pixels.1253 for (i = 0; i < num_contributors; i++)1254 contributors[i].n1 = stbir__min(contributors[i].n1, output_size - 1);1255}12561257// Each scan line uses the same kernel values so we should calculate the kernel1258// values once and then we can use them for every scan line.1259static void stbir__calculate_filters(stbir__contributors* contributors, float* coefficients, stbir_filter filter, float scale_ratio, float shift, int input_size, int output_size)1260{1261 int n;1262 int total_contributors = stbir__get_contributors(scale_ratio, filter, input_size, output_size);12631264 if (stbir__use_upsampling(scale_ratio))1265 {1266 float out_pixels_radius = stbir__filter_info_table[filter].support(1 / scale_ratio) * scale_ratio;12671268 // Looping through out pixels1269 for (n = 0; n < total_contributors; n++)1270 {1271 float in_center_of_out; // Center of the current out pixel in the in pixel space1272 int in_first_pixel, in_last_pixel;12731274 stbir__calculate_sample_range_upsample(n, out_pixels_radius, scale_ratio, shift, &in_first_pixel, &in_last_pixel, &in_center_of_out);12751276 stbir__calculate_coefficients_upsample(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));1277 }1278 }1279 else1280 {1281 float in_pixels_radius = stbir__filter_info_table[filter].support(scale_ratio) / scale_ratio;12821283 // Looping through in pixels1284 for (n = 0; n < total_contributors; n++)1285 {1286 float out_center_of_in; // Center of the current out pixel in the in pixel space1287 int out_first_pixel, out_last_pixel;1288 int n_adjusted = n - stbir__get_filter_pixel_margin(filter, scale_ratio);12891290 stbir__calculate_sample_range_downsample(n_adjusted, in_pixels_radius, scale_ratio, shift, &out_first_pixel, &out_last_pixel, &out_center_of_in);12911292 stbir__calculate_coefficients_downsample(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));1293 }12941295 stbir__normalize_downsample_coefficients(contributors, coefficients, filter, scale_ratio, input_size, output_size);1296 }1297}12981299static float* stbir__get_decode_buffer(ostbir__info* stbir_info)1300{1301 // The 0 index of the decode buffer starts after the margin. This makes1302 // it okay to use negative indexes on the decode buffer.1303 return &stbir_info->decode_buffer[stbir_info->horizontal_filter_pixel_margin * stbir_info->channels];1304}13051306#define STBIR__DECODE(type, colorspace) ((type) * (STBIR_MAX_COLORSPACES) + (colorspace))13071308static void stbir__decode_scanline(ostbir__info* stbir_info, int n)1309{1310 int c;1311 int channels = stbir_info->channels;1312 int alpha_channel = stbir_info->alpha_channel;1313 int type = stbir_info->type;1314 int colorspace = stbir_info->colorspace;1315 int input_w = stbir_info->input_w;1316 size_t input_stride_bytes = stbir_info->input_stride_bytes;1317 float* decode_buffer = stbir__get_decode_buffer(stbir_info);1318 stbir_edge edge_horizontal = stbir_info->edge_horizontal;1319 stbir_edge edge_vertical = stbir_info->edge_vertical;1320 size_t in_buffer_row_offset = stbir__edge_wrap(edge_vertical, n, stbir_info->input_h) * input_stride_bytes;1321 const void* input_data = (char *) stbir_info->input_data + in_buffer_row_offset;1322 int max_x = input_w + stbir_info->horizontal_filter_pixel_margin;1323 int decode = STBIR__DECODE(type, colorspace);13241325 int x = -stbir_info->horizontal_filter_pixel_margin;13261327 // special handling for STBIR_EDGE_ZERO because it needs to return an item that doesn't appear in the input,1328 // and we want to avoid paying overhead on every pixel if not STBIR_EDGE_ZERO1329 if (edge_vertical == STBIR_EDGE_ZERO && (n < 0 || n >= stbir_info->input_h))1330 {1331 for (; x < max_x; x++)1332 for (c = 0; c < channels; c++)1333 decode_buffer[x*channels + c] = 0;1334 return;1335 }13361337 STBIR_PROFILE_START( );1338 switch (decode)1339 {1340 case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_LINEAR):1341 for (; x < max_x; x++)1342 {1343 int decode_pixel_index = x * channels;1344 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1345 for (c = 0; c < channels; c++)1346 decode_buffer[decode_pixel_index + c] = ((float)((const unsigned char*)input_data)[input_pixel_index + c]) / stbir__max_uint8_as_float;1347 }1348 break;13491350 case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_SRGB):1351 for (; x < max_x; x++)1352 {1353 int decode_pixel_index = x * channels;1354 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1355 for (c = 0; c < channels; c++)1356 decode_buffer[decode_pixel_index + c] = stbir__srgb_uchar_to_linear_float[((const unsigned char*)input_data)[input_pixel_index + c]];13571358 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1359 decode_buffer[decode_pixel_index + alpha_channel] = ((float)((const unsigned char*)input_data)[input_pixel_index + alpha_channel]) / stbir__max_uint8_as_float;1360 }1361 break;13621363 case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR):1364 for (; x < max_x; x++)1365 {1366 int decode_pixel_index = x * channels;1367 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1368 for (c = 0; c < channels; c++)1369 decode_buffer[decode_pixel_index + c] = ((float)((const unsigned short*)input_data)[input_pixel_index + c]) / stbir__max_uint16_as_float;1370 }1371 break;13721373 case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB):1374 for (; x < max_x; x++)1375 {1376 int decode_pixel_index = x * channels;1377 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1378 for (c = 0; c < channels; c++)1379 decode_buffer[decode_pixel_index + c] = stbir__srgb_to_linear(((float)((const unsigned short*)input_data)[input_pixel_index + c]) / stbir__max_uint16_as_float);13801381 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1382 decode_buffer[decode_pixel_index + alpha_channel] = ((float)((const unsigned short*)input_data)[input_pixel_index + alpha_channel]) / stbir__max_uint16_as_float;1383 }1384 break;13851386 case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR):1387 for (; x < max_x; x++)1388 {1389 int decode_pixel_index = x * channels;1390 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1391 for (c = 0; c < channels; c++)1392 decode_buffer[decode_pixel_index + c] = (float)(((double)((const unsigned int*)input_data)[input_pixel_index + c]) / stbir__max_uint32_as_float);1393 }1394 break;13951396 case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB):1397 for (; x < max_x; x++)1398 {1399 int decode_pixel_index = x * channels;1400 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1401 for (c = 0; c < channels; c++)1402 decode_buffer[decode_pixel_index + c] = stbir__srgb_to_linear((float)(((double)((const unsigned int*)input_data)[input_pixel_index + c]) / stbir__max_uint32_as_float));14031404 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1405 decode_buffer[decode_pixel_index + alpha_channel] = (float)(((double)((const unsigned int*)input_data)[input_pixel_index + alpha_channel]) / stbir__max_uint32_as_float);1406 }1407 break;14081409 case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR):1410 for (; x < max_x; x++)1411 {1412 int decode_pixel_index = x * channels;1413 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1414 for (c = 0; c < channels; c++)1415 decode_buffer[decode_pixel_index + c] = ((const float*)input_data)[input_pixel_index + c];1416 }1417 break;14181419 case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB):1420 for (; x < max_x; x++)1421 {1422 int decode_pixel_index = x * channels;1423 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1424 for (c = 0; c < channels; c++)1425 decode_buffer[decode_pixel_index + c] = stbir__srgb_to_linear(((const float*)input_data)[input_pixel_index + c]);14261427 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1428 decode_buffer[decode_pixel_index + alpha_channel] = ((const float*)input_data)[input_pixel_index + alpha_channel];1429 }14301431 break;14321433 default:1434 STBIR_ASSERT(!"Unknown type/colorspace/channels combination.");1435 break;1436 }1437 STBIR_PROFILE_END( decode );14381439 if (!(stbir_info->flags & STBIR_FLAG_ALPHA_PREMULTIPLIED))1440 {1441 STBIR_PROFILE_START();14421443 for (x = -stbir_info->horizontal_filter_pixel_margin; x < max_x; x++)1444 {1445 int decode_pixel_index = x * channels;14461447 // If the alpha value is 0 it will clobber the color values. Make sure it's not.1448 float alpha = decode_buffer[decode_pixel_index + alpha_channel];1449#ifndef STBIR_NO_ALPHA_EPSILON1450 if (stbir_info->type != STBIR_TYPE_FLOAT) {1451 alpha += STBIR_ALPHA_EPSILON;1452 decode_buffer[decode_pixel_index + alpha_channel] = alpha;1453 }1454#endif1455 for (c = 0; c < channels; c++)1456 {1457 if (c == alpha_channel)1458 continue;14591460 decode_buffer[decode_pixel_index + c] *= alpha;1461 }1462 }1463 STBIR_PROFILE_END( alpha );1464 }14651466 if (edge_horizontal == STBIR_EDGE_ZERO)1467 {1468 for (x = -stbir_info->horizontal_filter_pixel_margin; x < 0; x++)1469 {1470 for (c = 0; c < channels; c++)1471 decode_buffer[x*channels + c] = 0;1472 }1473 for (x = input_w; x < max_x; x++)1474 {1475 for (c = 0; c < channels; c++)1476 decode_buffer[x*channels + c] = 0;1477 }1478 }1479}14801481static float* stbir__get_ring_buffer_entry(float* ring_buffer, int index, int ring_buffer_length)1482{1483 return &ring_buffer[index * ring_buffer_length];1484}14851486static float* stbir__add_empty_ring_buffer_entry(ostbir__info* stbir_info, int n)1487{1488 int ring_buffer_index;1489 float* ring_buffer;14901491 stbir_info->ring_buffer_last_scanline = n;14921493 if (stbir_info->ring_buffer_begin_index < 0)1494 {1495 ring_buffer_index = stbir_info->ring_buffer_begin_index = 0;1496 stbir_info->ring_buffer_first_scanline = n;1497 }1498 else1499 {1500 ring_buffer_index = (stbir_info->ring_buffer_begin_index + (stbir_info->ring_buffer_last_scanline - stbir_info->ring_buffer_first_scanline)) % stbir_info->ring_buffer_num_entries;1501 STBIR_ASSERT(ring_buffer_index != stbir_info->ring_buffer_begin_index);1502 }15031504 ring_buffer = stbir__get_ring_buffer_entry(stbir_info->ring_buffer, ring_buffer_index, stbir_info->ring_buffer_length_bytes / sizeof(float));1505 1506 memset(ring_buffer, 0, stbir_info->ring_buffer_length_bytes);15071508 return ring_buffer;1509}151015111512static void stbir__resample_horizontal_upsample(ostbir__info* stbir_info, float* output_buffer)1513{1514 int x, k;1515 int output_w = stbir_info->output_w;1516 int channels = stbir_info->channels;1517 float* decode_buffer = stbir__get_decode_buffer(stbir_info);1518 stbir__contributors* horizontal_contributors = stbir_info->horizontal_contributors;1519 float* horizontal_coefficients = stbir_info->horizontal_coefficients;1520 int coefficient_width = stbir_info->horizontal_coefficient_width;15211522 STBIR_PROFILE_START( );1523 for (x = 0; x < output_w; x++)1524 {1525 int n0 = horizontal_contributors[x].n0;1526 int n1 = horizontal_contributors[x].n1;15271528 int out_pixel_index = x * channels;1529 int coefficient_group = coefficient_width * x;1530 int coefficient_counter = 0;15311532 STBIR_ASSERT(n1 >= n0);1533 STBIR_ASSERT(n0 >= -stbir_info->horizontal_filter_pixel_margin);1534 STBIR_ASSERT(n1 >= -stbir_info->horizontal_filter_pixel_margin);1535 STBIR_ASSERT(n0 < stbir_info->input_w + stbir_info->horizontal_filter_pixel_margin);1536 STBIR_ASSERT(n1 < stbir_info->input_w + stbir_info->horizontal_filter_pixel_margin);15371538 switch (channels) {1539 case 1:1540 for (k = n0; k <= n1; k++)1541 {1542 int in_pixel_index = k * 1;1543 float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++];1544 STBIR_ASSERT(coefficient != 0);1545 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1546 }1547 break;1548 case 2:1549 for (k = n0; k <= n1; k++)1550 {1551 int in_pixel_index = k * 2;1552 float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++];1553 STBIR_ASSERT(coefficient != 0);1554 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1555 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1556 }1557 break;1558 case 3:1559 for (k = n0; k <= n1; k++)1560 {1561 int in_pixel_index = k * 3;1562 float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++];1563 STBIR_ASSERT(coefficient != 0);1564 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1565 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1566 output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient;1567 }1568 break;1569 case 4:1570 for (k = n0; k <= n1; k++)1571 {1572 int in_pixel_index = k * 4;1573 float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++];1574 STBIR_ASSERT(coefficient != 0);1575 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1576 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1577 output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient;1578 output_buffer[out_pixel_index + 3] += decode_buffer[in_pixel_index + 3] * coefficient;1579 }1580 break;1581 default:1582 for (k = n0; k <= n1; k++)1583 {1584 int in_pixel_index = k * channels;1585 float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++];1586 int c;1587 STBIR_ASSERT(coefficient != 0);1588 for (c = 0; c < channels; c++)1589 output_buffer[out_pixel_index + c] += decode_buffer[in_pixel_index + c] * coefficient;1590 }1591 break;1592 }1593 }1594 STBIR_PROFILE_END( horizontal );1595}15961597static void stbir__resample_horizontal_downsample(ostbir__info* stbir_info, float* output_buffer)1598{1599 int x, k;1600 int input_w = stbir_info->input_w;1601 int channels = stbir_info->channels;1602 float* decode_buffer = stbir__get_decode_buffer(stbir_info);1603 stbir__contributors* horizontal_contributors = stbir_info->horizontal_contributors;1604 float* horizontal_coefficients = stbir_info->horizontal_coefficients;1605 int coefficient_width = stbir_info->horizontal_coefficient_width;1606 int filter_pixel_margin = stbir_info->horizontal_filter_pixel_margin;1607 int max_x = input_w + filter_pixel_margin * 2;16081609 STBIR_ASSERT(!stbir__use_width_upsampling(stbir_info));16101611 STBIR_PROFILE_START( );1612 switch (channels) {1613 case 1:1614 for (x = 0; x < max_x; x++)1615 {1616 int n0 = horizontal_contributors[x].n0;1617 int n1 = horizontal_contributors[x].n1;16181619 int in_x = x - filter_pixel_margin;1620 int in_pixel_index = in_x * 1;1621 int max_n = n1;1622 int coefficient_group = coefficient_width * x;16231624 for (k = n0; k <= max_n; k++)1625 {1626 int out_pixel_index = k * 1;1627 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1628 STBIR_ASSERT(coefficient != 0);1629 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1630 }1631 }1632 break;16331634 case 2:1635 for (x = 0; x < max_x; x++)1636 {1637 int n0 = horizontal_contributors[x].n0;1638 int n1 = horizontal_contributors[x].n1;16391640 int in_x = x - filter_pixel_margin;1641 int in_pixel_index = in_x * 2;1642 int max_n = n1;1643 int coefficient_group = coefficient_width * x;16441645 for (k = n0; k <= max_n; k++)1646 {1647 int out_pixel_index = k * 2;1648 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1649 STBIR_ASSERT(coefficient != 0);1650 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1651 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1652 }1653 }1654 break;16551656 case 3:1657 for (x = 0; x < max_x; x++)1658 {1659 int n0 = horizontal_contributors[x].n0;1660 int n1 = horizontal_contributors[x].n1;16611662 int in_x = x - filter_pixel_margin;1663 int in_pixel_index = in_x * 3;1664 int max_n = n1;1665 int coefficient_group = coefficient_width * x;16661667 for (k = n0; k <= max_n; k++)1668 {1669 int out_pixel_index = k * 3;1670 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1671 //STBIR_ASSERT(coefficient != 0);1672 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1673 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1674 output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient;1675 }1676 }1677 break;16781679 case 4:1680 for (x = 0; x < max_x; x++)1681 {1682 int n0 = horizontal_contributors[x].n0;1683 int n1 = horizontal_contributors[x].n1;16841685 int in_x = x - filter_pixel_margin;1686 int in_pixel_index = in_x * 4;1687 int max_n = n1;1688 int coefficient_group = coefficient_width * x;16891690 for (k = n0; k <= max_n; k++)1691 {1692 int out_pixel_index = k * 4;1693 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1694 STBIR_ASSERT(coefficient != 0);1695 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1696 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1697 output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient;1698 output_buffer[out_pixel_index + 3] += decode_buffer[in_pixel_index + 3] * coefficient;1699 }1700 }1701 break;17021703 default:1704 for (x = 0; x < max_x; x++)1705 {1706 int n0 = horizontal_contributors[x].n0;1707 int n1 = horizontal_contributors[x].n1;17081709 int in_x = x - filter_pixel_margin;1710 int in_pixel_index = in_x * channels;1711 int max_n = n1;1712 int coefficient_group = coefficient_width * x;17131714 for (k = n0; k <= max_n; k++)1715 {1716 int c;1717 int out_pixel_index = k * channels;1718 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1719 STBIR_ASSERT(coefficient != 0);1720 for (c = 0; c < channels; c++)1721 output_buffer[out_pixel_index + c] += decode_buffer[in_pixel_index + c] * coefficient;1722 }1723 }1724 break;1725 }1726 STBIR_PROFILE_END( horizontal );1727}17281729static void stbir__decode_and_resample_upsample(ostbir__info* stbir_info, int n)1730{1731 // Decode the nth scanline from the source image into the decode buffer.1732 stbir__decode_scanline(stbir_info, n);17331734 // Now resample it into the ring buffer.1735 if (stbir__use_width_upsampling(stbir_info))1736 stbir__resample_horizontal_upsample(stbir_info, stbir__add_empty_ring_buffer_entry(stbir_info, n));1737 else1738 stbir__resample_horizontal_downsample(stbir_info, stbir__add_empty_ring_buffer_entry(stbir_info, n));17391740 // Now it's sitting in the ring buffer ready to be used as source for the vertical sampling.1741}17421743static void stbir__decode_and_resample_downsample(ostbir__info* stbir_info, int n)1744{1745 // Decode the nth scanline from the source image into the decode buffer.1746 stbir__decode_scanline(stbir_info, n);17471748 memset(stbir_info->horizontal_buffer, 0, stbir_info->output_w * stbir_info->channels * sizeof(float));17491750 // Now resample it into the horizontal buffer.1751 if (stbir__use_width_upsampling(stbir_info))1752 stbir__resample_horizontal_upsample(stbir_info, stbir_info->horizontal_buffer);1753 else1754 stbir__resample_horizontal_downsample(stbir_info, stbir_info->horizontal_buffer);17551756 // Now it's sitting in the horizontal buffer ready to be distributed into the ring buffers.1757}17581759// Get the specified scan line from the ring buffer.1760static float* stbir__get_ring_buffer_scanline(int get_scanline, float* ring_buffer, int begin_index, int first_scanline, int ring_buffer_num_entries, int ring_buffer_length)1761{1762 int ring_buffer_index = (begin_index + (get_scanline - first_scanline)) % ring_buffer_num_entries;1763 return stbir__get_ring_buffer_entry(ring_buffer, ring_buffer_index, ring_buffer_length);1764}176517661767static void stbir__encode_scanline(ostbir__info* stbir_info, int num_pixels, void *output_buffer, float *encode_buffer, int channels, int alpha_channel, int decode)1768{1769 int x;1770 int n;1771 int num_nonalpha;1772 stbir_uint16 nonalpha[STBIR_MAX_CHANNELS];17731774 if ((!(stbir_info->flags&STBIR_FLAG_ALPHA_OUT_PREMULTIPLIED))&&(alpha_channel!=-1))1775 {1776 STBIR_PROFILE_START( );17771778 for (x=0; x < num_pixels; ++x)1779 {1780 int pixel_index = x*channels;17811782 float alpha = encode_buffer[pixel_index + alpha_channel];1783 float reciprocal_alpha = alpha ? 1.0f / alpha : 0;17841785 // unrolling this produced a 1% slowdown upscaling a large RGBA linear-space image on my machine - stb1786 for (n = 0; n < channels; n++)1787 if (n != alpha_channel)1788 encode_buffer[pixel_index + n] *= reciprocal_alpha;17891790 // We added in a small epsilon to prevent the color channel from being deleted with zero alpha.1791 // Because we only add it for integer types, it will automatically be discarded on integer1792 // conversion, so we don't need to subtract it back out (which would be problematic for1793 // numeric precision reasons).1794 }1795 STBIR_PROFILE_END( unalpha );1796 }17971798 // build a table of all channels that need colorspace correction, so1799 // we don't perform colorspace correction on channels that don't need it.1800 for (x = 0, num_nonalpha = 0; x < channels; ++x)1801 {1802 if (x != alpha_channel || (stbir_info->flags & STBIR_FLAG_ALPHA_USES_COLORSPACE))1803 {1804 nonalpha[num_nonalpha++] = (stbir_uint16)x;1805 }1806 }18071808 #define STBIR__ROUND_INT(f) ((int) ((f)+0.5))1809 #define STBIR__ROUND_UINT(f) ((stbir_uint32) ((f)+0.5))18101811 #ifdef STBIR__SATURATE_INT1812 #define STBIR__ENCODE_LINEAR8(f) stbir__saturate8 (STBIR__ROUND_INT((f) * stbir__max_uint8_as_float ))1813 #define STBIR__ENCODE_LINEAR16(f) stbir__saturate16(STBIR__ROUND_INT((f) * stbir__max_uint16_as_float))1814 #else1815 #define STBIR__ENCODE_LINEAR8(f) (unsigned char ) STBIR__ROUND_INT(stbir__saturate(f) * stbir__max_uint8_as_float )1816 #define STBIR__ENCODE_LINEAR16(f) (unsigned short) STBIR__ROUND_INT(stbir__saturate(f) * stbir__max_uint16_as_float)1817 #endif18181819 STBIR_PROFILE_START( );18201821 switch (decode)1822 {1823 case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_LINEAR):1824 for (x=0; x < num_pixels; ++x)1825 {1826 int pixel_index = x*channels;18271828 for (n = 0; n < channels; n++)1829 {1830 int index = pixel_index + n;1831 ((unsigned char*)output_buffer)[index] = STBIR__ENCODE_LINEAR8(encode_buffer[index]);1832 }1833 }1834 break;18351836 case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_SRGB):1837 for (x=0; x < num_pixels; ++x)1838 {1839 int pixel_index = x*channels;18401841 for (n = 0; n < num_nonalpha; n++)1842 {1843 int index = pixel_index + nonalpha[n];1844 ((unsigned char*)output_buffer)[index] = stbir__linear_to_srgb_uchar(encode_buffer[index]);1845 }18461847 if (!(stbir_info->flags & STBIR_FLAG_ALPHA_USES_COLORSPACE))1848 ((unsigned char *)output_buffer)[pixel_index + alpha_channel] = STBIR__ENCODE_LINEAR8(encode_buffer[pixel_index+alpha_channel]);1849 }1850 break;18511852 case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR):1853 for (x=0; x < num_pixels; ++x)1854 {1855 int pixel_index = x*channels;18561857 for (n = 0; n < channels; n++)1858 {1859 int index = pixel_index + n;1860 ((unsigned short*)output_buffer)[index] = STBIR__ENCODE_LINEAR16(encode_buffer[index]);1861 }1862 }1863 break;18641865 case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB):1866 for (x=0; x < num_pixels; ++x)1867 {1868 int pixel_index = x*channels;18691870 for (n = 0; n < num_nonalpha; n++)1871 {1872 int index = pixel_index + nonalpha[n];1873 ((unsigned short*)output_buffer)[index] = (unsigned short)STBIR__ROUND_INT(stbir__linear_to_srgb(stbir__saturate(encode_buffer[index])) * stbir__max_uint16_as_float);1874 }18751876 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1877 ((unsigned short*)output_buffer)[pixel_index + alpha_channel] = STBIR__ENCODE_LINEAR16(encode_buffer[pixel_index + alpha_channel]);1878 }18791880 break;18811882 case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR):1883 for (x=0; x < num_pixels; ++x)1884 {1885 int pixel_index = x*channels;18861887 for (n = 0; n < channels; n++)1888 {1889 int index = pixel_index + n;1890 ((unsigned int*)output_buffer)[index] = (unsigned int)STBIR__ROUND_UINT(((double)stbir__saturate(encode_buffer[index])) * stbir__max_uint32_as_float);1891 }1892 }1893 break;18941895 case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB):1896 for (x=0; x < num_pixels; ++x)1897 {1898 int pixel_index = x*channels;18991900 for (n = 0; n < num_nonalpha; n++)1901 {1902 int index = pixel_index + nonalpha[n];1903 ((unsigned int*)output_buffer)[index] = (unsigned int)STBIR__ROUND_UINT(((double)stbir__linear_to_srgb(stbir__saturate(encode_buffer[index]))) * stbir__max_uint32_as_float);1904 }19051906 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1907 ((unsigned int*)output_buffer)[pixel_index + alpha_channel] = (unsigned int)STBIR__ROUND_INT(((double)stbir__saturate(encode_buffer[pixel_index + alpha_channel])) * stbir__max_uint32_as_float);1908 }1909 break;19101911 case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR):1912 for (x=0; x < num_pixels; ++x)1913 {1914 int pixel_index = x*channels;19151916 for (n = 0; n < channels; n++)1917 {1918 int index = pixel_index + n;1919 ((float*)output_buffer)[index] = encode_buffer[index];1920 }1921 }1922 break;19231924 case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB):1925 for (x=0; x < num_pixels; ++x)1926 {1927 int pixel_index = x*channels;19281929 for (n = 0; n < num_nonalpha; n++)1930 {1931 int index = pixel_index + nonalpha[n];1932 float p = encode_buffer[index];1933 if ( p <= 0 ) p = 0; if ( p >= 1.0 ) p = 1.0;1934 ((float*)output_buffer)[index] = stbir__linear_to_srgb(p);1935 }19361937 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1938 {1939 float p = encode_buffer[pixel_index + alpha_channel];1940 if ( p <= 0 ) p = 0; if ( p >= 1.0 ) p = 1.0;1941 ((float*)output_buffer)[pixel_index + alpha_channel] = p;1942 }1943 }1944 break;19451946 default:1947 STBIR_ASSERT(!"Unknown type/colorspace/channels combination.");1948 break;1949 }1950 STBIR_PROFILE_END( encode );1951}19521953static void stbir__resample_vertical_upsample(ostbir__info* stbir_info, int n)1954{1955 int x, k;1956 int output_w = stbir_info->output_w;1957 stbir__contributors* vertical_contributors = stbir_info->vertical_contributors;1958 float* vertical_coefficients = stbir_info->vertical_coefficients;1959 int channels = stbir_info->channels;1960 int alpha_channel = stbir_info->alpha_channel;1961 int type = stbir_info->type;1962 int colorspace = stbir_info->colorspace;1963 int ring_buffer_entries = stbir_info->ring_buffer_num_entries;1964 void* output_data = stbir_info->output_data;1965 float* encode_buffer = stbir_info->encode_buffer;1966 int decode = STBIR__DECODE(type, colorspace);1967 int coefficient_width = stbir_info->vertical_coefficient_width;1968 int coefficient_counter;1969 int contributor = n;19701971 float* ring_buffer = stbir_info->ring_buffer;1972 int ring_buffer_begin_index = stbir_info->ring_buffer_begin_index;1973 int ring_buffer_first_scanline = stbir_info->ring_buffer_first_scanline;1974 int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float);19751976 int n0,n1, output_row_start;1977 int coefficient_group = coefficient_width * contributor;19781979 n0 = vertical_contributors[contributor].n0;1980 n1 = vertical_contributors[contributor].n1;19811982 output_row_start = n * stbir_info->output_stride_bytes;19831984 STBIR_ASSERT(stbir__use_height_upsampling(stbir_info));19851986 STBIR_PROFILE_START( );19871988 memset(encode_buffer, 0, output_w * sizeof(float) * channels);19891990 // I tried reblocking this for better cache usage of encode_buffer1991 // (using x_outer, k, x_inner), but it lost speed. -- stb19921993 coefficient_counter = 0;1994 switch (channels) {1995 case 1:1996 for (k = n0; k <= n1; k++)1997 {1998 int coefficient_index = coefficient_counter++;1999 float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, ring_buffer_entries, ring_buffer_length);2000 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];The first 2,000 of 2,738 lines.