Skip to content
Myr

stb_image_resize2.h 2.00

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 · parent 5736b15 · Browse files · 15 files+17963 added−2649 removed
This commit

State

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

Diff stats

15 files+17963 added−2649 removed

  • tests:+14 added−12 removed
  • other:+17949 added−2637 removed

15 files+17963 added−2649 removed

added:deprecated/stb_image_resize.h+2634 added
1/* stb_image_resize - v0.97 - 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.97 (2020-02-02) fixed warning163 0.96 (2019-03-04) fixed warnings164 0.95 (2017-07-23) fixed warnings165 0.94 (2017-03-18) fixed warnings166 0.93 (2017-03-03) fixed bug with certain combinations of heights167 0.92 (2017-01-02) fix integer overflow on large (>2GB) images168 0.91 (2016-04-02) fix warnings; fix handling of subpixel regions169 0.90 (2014-09-17) first released version170171 LICENSE172 See end of file for license information.173174 TODO175 Don't decode all of the image data when only processing a partial tile176 Don't use full-width decode buffers when only processing a partial tile177 When processing wide images, break processing into tiles so data fits in L1 cache178 Installable filters?179 Resize that respects alpha test coverage180 (Reference code: FloatImage::alphaTestCoverage and FloatImage::scaleAlphaToCoverage:181 https://code.google.com/p/nvidia-texture-tools/source/browse/trunk/src/nvimage/FloatImage.cpp )182*/183184#ifndef STBIR_INCLUDE_STB_IMAGE_RESIZE_H185#define STBIR_INCLUDE_STB_IMAGE_RESIZE_H186187#ifdef _MSC_VER188typedef unsigned char stbir_uint8;189typedef unsigned short stbir_uint16;190typedef unsigned int stbir_uint32;191#else192#include <stdint.h>193typedef uint8_t stbir_uint8;194typedef uint16_t stbir_uint16;195typedef uint32_t stbir_uint32;196#endif197198#ifndef STBIRDEF199#ifdef STB_IMAGE_RESIZE_STATIC200#define STBIRDEF static201#else202#ifdef __cplusplus203#define STBIRDEF extern "C"204#else205#define STBIRDEF extern206#endif207#endif208#endif209210//////////////////////////////////////////////////////////////////////////////211//212// Easy-to-use API:213//214// * "input pixels" points to an array of image data with 'num_channels' channels (e.g. RGB=3, RGBA=4)215// * input_w is input image width (x-axis), input_h is input image height (y-axis)216// * stride is the offset between successive rows of image data in memory, in bytes. you can217// specify 0 to mean packed continuously in memory218// * alpha channel is treated identically to other channels.219// * colorspace is linear or sRGB as specified by function name220// * returned result is 1 for success or 0 in case of an error.221// #define STBIR_ASSERT() to trigger an assert on parameter validation errors.222// * Memory required grows approximately linearly with input and output size, but with223// discontinuities at input_w == output_w and input_h == output_h.224// * These functions use a "default" resampling filter defined at compile time. To change the filter,225// you can change the compile-time defaults by #defining STBIR_DEFAULT_FILTER_UPSAMPLE226// and STBIR_DEFAULT_FILTER_DOWNSAMPLE, or you can use the medium-complexity API.227228STBIRDEF int stbir_resize_uint8( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,229 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,230 int num_channels);231232STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,233 float *output_pixels, int output_w, int output_h, int output_stride_in_bytes,234 int num_channels);235236237// The following functions interpret image data as gamma-corrected sRGB.238// Specify STBIR_ALPHA_CHANNEL_NONE if you have no alpha channel,239// or otherwise provide the index of the alpha channel. Flags value240// of 0 will probably do the right thing if you're not sure what241// the flags mean.242243#define STBIR_ALPHA_CHANNEL_NONE -1244245// Set this flag if your texture has premultiplied alpha. Otherwise, stbir will246// use alpha-weighted resampling (effectively premultiplying, resampling,247// then unpremultiplying).248#define STBIR_FLAG_ALPHA_PREMULTIPLIED (1 << 0)249// The specified alpha channel should be handled as gamma-corrected value even250// when doing sRGB operations.251#define STBIR_FLAG_ALPHA_USES_COLORSPACE (1 << 1)252253STBIRDEF int stbir_resize_uint8_srgb(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,254 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,255 int num_channels, int alpha_channel, int flags);256257258typedef enum259{260 STBIR_EDGE_CLAMP = 1,261 STBIR_EDGE_REFLECT = 2,262 STBIR_EDGE_WRAP = 3,263 STBIR_EDGE_ZERO = 4,264} stbir_edge;265266// This function adds the ability to specify how requests to sample off the edge of the image are handled.267STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,268 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,269 int num_channels, int alpha_channel, int flags,270 stbir_edge edge_wrap_mode);271272//////////////////////////////////////////////////////////////////////////////273//274// Medium-complexity API275//276// This extends the easy-to-use API as follows:277//278// * Alpha-channel can be processed separately279// * If alpha_channel is not STBIR_ALPHA_CHANNEL_NONE280// * Alpha channel will not be gamma corrected (unless flags&STBIR_FLAG_GAMMA_CORRECT)281// * Filters will be weighted by alpha channel (unless flags&STBIR_FLAG_ALPHA_PREMULTIPLIED)282// * Filter can be selected explicitly283// * uint16 image type284// * sRGB colorspace available for all types285// * context parameter for passing to STBIR_MALLOC286287typedef enum288{289 STBIR_FILTER_DEFAULT = 0, // use same filter type that easy-to-use API chooses290 STBIR_FILTER_BOX = 1, // A trapezoid w/1-pixel wide ramps, same result as box for integer scale ratios291 STBIR_FILTER_TRIANGLE = 2, // On upsampling, produces same results as bilinear texture filtering292 STBIR_FILTER_CUBICBSPLINE = 3, // The cubic b-spline (aka Mitchell-Netrevalli with B=1,C=0), gaussian-esque293 STBIR_FILTER_CATMULLROM = 4, // An interpolating cubic spline294 STBIR_FILTER_MITCHELL = 5, // Mitchell-Netrevalli filter with B=1/3, C=1/3295} stbir_filter;296297typedef enum298{299 STBIR_COLORSPACE_LINEAR,300 STBIR_COLORSPACE_SRGB,301302 STBIR_MAX_COLORSPACES,303} stbir_colorspace;304305// The following functions are all identical except for the type of the image data306307STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,308 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,309 int num_channels, int alpha_channel, int flags,310 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,311 void *alloc_context);312313STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int input_w , int input_h , int input_stride_in_bytes,314 stbir_uint16 *output_pixels , int output_w, int output_h, int output_stride_in_bytes,315 int num_channels, int alpha_channel, int flags,316 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,317 void *alloc_context);318319STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,320 float *output_pixels , int output_w, int output_h, int output_stride_in_bytes,321 int num_channels, int alpha_channel, int flags,322 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,323 void *alloc_context);324325326327//////////////////////////////////////////////////////////////////////////////328//329// Full-complexity API330//331// This extends the medium API as follows:332//333// * uint32 image type334// * not typesafe335// * separate filter types for each axis336// * separate edge modes for each axis337// * can specify scale explicitly for subpixel correctness338// * can specify image source tile using texture coordinates339340typedef enum341{342 STBIR_TYPE_UINT8 ,343 STBIR_TYPE_UINT16,344 STBIR_TYPE_UINT32,345 STBIR_TYPE_FLOAT ,346347 STBIR_MAX_TYPES348} stbir_datatype;349350STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,351 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,352 stbir_datatype datatype,353 int num_channels, int alpha_channel, int flags,354 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,355 stbir_filter filter_horizontal, stbir_filter filter_vertical,356 stbir_colorspace space, void *alloc_context);357358STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,359 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,360 stbir_datatype datatype,361 int num_channels, int alpha_channel, int flags,362 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,363 stbir_filter filter_horizontal, stbir_filter filter_vertical,364 stbir_colorspace space, void *alloc_context,365 float x_scale, float y_scale,366 float x_offset, float y_offset);367368STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,369 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,370 stbir_datatype datatype,371 int num_channels, int alpha_channel, int flags,372 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,373 stbir_filter filter_horizontal, stbir_filter filter_vertical,374 stbir_colorspace space, void *alloc_context,375 float s0, float t0, float s1, float t1);376// (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.377378//379//380//// end header file /////////////////////////////////////////////////////381#endif // STBIR_INCLUDE_STB_IMAGE_RESIZE_H382383384385386387#ifdef STB_IMAGE_RESIZE_IMPLEMENTATION388389#ifndef STBIR_ASSERT390#include <assert.h>391#define STBIR_ASSERT(x) assert(x)392#endif393394// For memset395#include <string.h>396397#include <math.h>398399#ifndef STBIR_MALLOC400#include <stdlib.h>401// use comma operator to evaluate c, to avoid "unused parameter" warnings402#define STBIR_MALLOC(size,c) ((void)(c), malloc(size))403#define STBIR_FREE(ptr,c) ((void)(c), free(ptr))404#endif405406#ifndef _MSC_VER407#ifdef __cplusplus408#define stbir__inline inline409#else410#define stbir__inline411#endif412#else413#define stbir__inline __forceinline414#endif415416417// should produce compiler error if size is wrong418typedef unsigned char stbir__validate_uint32[sizeof(stbir_uint32) == 4 ? 1 : -1];419420#ifdef _MSC_VER421#define STBIR__NOTUSED(v) (void)(v)422#else423#define STBIR__NOTUSED(v) (void)sizeof(v)424#endif425426#define STBIR__ARRAY_SIZE(a) (sizeof((a))/sizeof((a)[0]))427428#ifndef STBIR_DEFAULT_FILTER_UPSAMPLE429#define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_CATMULLROM430#endif431432#ifndef STBIR_DEFAULT_FILTER_DOWNSAMPLE433#define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_MITCHELL434#endif435436#ifndef STBIR_PROGRESS_REPORT437#define STBIR_PROGRESS_REPORT(float_0_to_1)438#endif439440#ifndef STBIR_MAX_CHANNELS441#define STBIR_MAX_CHANNELS 64442#endif443444#if STBIR_MAX_CHANNELS > 65536445#error "Too many channels; STBIR_MAX_CHANNELS must be no more than 65536."446// because we store the indices in 16-bit variables447#endif448449// This value is added to alpha just before premultiplication to avoid450// zeroing out color values. It is equivalent to 2^-80. If you don't want451// that behavior (it may interfere if you have floating point images with452// very small alpha values) then you can define STBIR_NO_ALPHA_EPSILON to453// disable it.454#ifndef STBIR_ALPHA_EPSILON455#define STBIR_ALPHA_EPSILON ((float)1 / (1 << 20) / (1 << 20) / (1 << 20) / (1 << 20))456#endif457458459460#ifdef _MSC_VER461#define STBIR__UNUSED_PARAM(v) (void)(v)462#else463#define STBIR__UNUSED_PARAM(v) (void)sizeof(v)464#endif465466// must match stbir_datatype467static unsigned char stbir__type_size[] = {468 1, // STBIR_TYPE_UINT8469 2, // STBIR_TYPE_UINT16470 4, // STBIR_TYPE_UINT32471 4, // STBIR_TYPE_FLOAT472};473474// Kernel function centered at 0475typedef float (stbir__kernel_fn)(float x, float scale);476typedef float (stbir__support_fn)(float scale);477478typedef struct479{480 stbir__kernel_fn* kernel;481 stbir__support_fn* support;482} stbir__filter_info;483484// When upsampling, the contributors are which source pixels contribute.485// When downsampling, the contributors are which destination pixels are contributed to.486typedef struct487{488 int n0; // First contributing pixel489 int n1; // Last contributing pixel490} stbir__contributors;491492typedef struct493{494 const void* input_data;495 int input_w;496 int input_h;497 int input_stride_bytes;498499 void* output_data;500 int output_w;501 int output_h;502 int output_stride_bytes;503504 float s0, t0, s1, t1;505506 float horizontal_shift; // Units: output pixels507 float vertical_shift; // Units: output pixels508 float horizontal_scale;509 float vertical_scale;510511 int channels;512 int alpha_channel;513 stbir_uint32 flags;514 stbir_datatype type;515 stbir_filter horizontal_filter;516 stbir_filter vertical_filter;517 stbir_edge edge_horizontal;518 stbir_edge edge_vertical;519 stbir_colorspace colorspace;520521 stbir__contributors* horizontal_contributors;522 float* horizontal_coefficients;523524 stbir__contributors* vertical_contributors;525 float* vertical_coefficients;526527 int decode_buffer_pixels;528 float* decode_buffer;529530 float* horizontal_buffer;531532 // cache these because ceil/floor are inexplicably showing up in profile533 int horizontal_coefficient_width;534 int vertical_coefficient_width;535 int horizontal_filter_pixel_width;536 int vertical_filter_pixel_width;537 int horizontal_filter_pixel_margin;538 int vertical_filter_pixel_margin;539 int horizontal_num_contributors;540 int vertical_num_contributors;541542 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)543 int ring_buffer_num_entries; // Total number of entries in the ring buffer.544 int ring_buffer_first_scanline;545 int ring_buffer_last_scanline;546 int ring_buffer_begin_index; // first_scanline is at this index in the ring buffer547 float* ring_buffer;548549 float* encode_buffer; // A temporary buffer to store floats so we don't lose precision while we do multiply-adds.550551 int horizontal_contributors_size;552 int horizontal_coefficients_size;553 int vertical_contributors_size;554 int vertical_coefficients_size;555 int decode_buffer_size;556 int horizontal_buffer_size;557 int ring_buffer_size;558 int encode_buffer_size;559} stbir__info;560561562static const float stbir__max_uint8_as_float = 255.0f;563static const float stbir__max_uint16_as_float = 65535.0f;564static const double stbir__max_uint32_as_float = 4294967295.0;565566567static stbir__inline int stbir__min(int a, int b)568{569 return a < b ? a : b;570}571572static stbir__inline float stbir__saturate(float x)573{574 if (x < 0)575 return 0;576577 if (x > 1)578 return 1;579580 return x;581}582583#ifdef STBIR_SATURATE_INT584static stbir__inline stbir_uint8 stbir__saturate8(int x)585{586 if ((unsigned int) x <= 255)587 return x;588589 if (x < 0)590 return 0;591592 return 255;593}594595static stbir__inline stbir_uint16 stbir__saturate16(int x)596{597 if ((unsigned int) x <= 65535)598 return x;599600 if (x < 0)601 return 0;602603 return 65535;604}605#endif606607static float stbir__srgb_uchar_to_linear_float[256] = {608 0.000000f, 0.000304f, 0.000607f, 0.000911f, 0.001214f, 0.001518f, 0.001821f, 0.002125f, 0.002428f, 0.002732f, 0.003035f,609 0.003347f, 0.003677f, 0.004025f, 0.004391f, 0.004777f, 0.005182f, 0.005605f, 0.006049f, 0.006512f, 0.006995f, 0.007499f,610 0.008023f, 0.008568f, 0.009134f, 0.009721f, 0.010330f, 0.010960f, 0.011612f, 0.012286f, 0.012983f, 0.013702f, 0.014444f,611 0.015209f, 0.015996f, 0.016807f, 0.017642f, 0.018500f, 0.019382f, 0.020289f, 0.021219f, 0.022174f, 0.023153f, 0.024158f,612 0.025187f, 0.026241f, 0.027321f, 0.028426f, 0.029557f, 0.030713f, 0.031896f, 0.033105f, 0.034340f, 0.035601f, 0.036889f,613 0.038204f, 0.039546f, 0.040915f, 0.042311f, 0.043735f, 0.045186f, 0.046665f, 0.048172f, 0.049707f, 0.051269f, 0.052861f,614 0.054480f, 0.056128f, 0.057805f, 0.059511f, 0.061246f, 0.063010f, 0.064803f, 0.066626f, 0.068478f, 0.070360f, 0.072272f,615 0.074214f, 0.076185f, 0.078187f, 0.080220f, 0.082283f, 0.084376f, 0.086500f, 0.088656f, 0.090842f, 0.093059f, 0.095307f,616 0.097587f, 0.099899f, 0.102242f, 0.104616f, 0.107023f, 0.109462f, 0.111932f, 0.114435f, 0.116971f, 0.119538f, 0.122139f,617 0.124772f, 0.127438f, 0.130136f, 0.132868f, 0.135633f, 0.138432f, 0.141263f, 0.144128f, 0.147027f, 0.149960f, 0.152926f,618 0.155926f, 0.158961f, 0.162029f, 0.165132f, 0.168269f, 0.171441f, 0.174647f, 0.177888f, 0.181164f, 0.184475f, 0.187821f,619 0.191202f, 0.194618f, 0.198069f, 0.201556f, 0.205079f, 0.208637f, 0.212231f, 0.215861f, 0.219526f, 0.223228f, 0.226966f,620 0.230740f, 0.234551f, 0.238398f, 0.242281f, 0.246201f, 0.250158f, 0.254152f, 0.258183f, 0.262251f, 0.266356f, 0.270498f,621 0.274677f, 0.278894f, 0.283149f, 0.287441f, 0.291771f, 0.296138f, 0.300544f, 0.304987f, 0.309469f, 0.313989f, 0.318547f,622 0.323143f, 0.327778f, 0.332452f, 0.337164f, 0.341914f, 0.346704f, 0.351533f, 0.356400f, 0.361307f, 0.366253f, 0.371238f,623 0.376262f, 0.381326f, 0.386430f, 0.391573f, 0.396755f, 0.401978f, 0.407240f, 0.412543f, 0.417885f, 0.423268f, 0.428691f,624 0.434154f, 0.439657f, 0.445201f, 0.450786f, 0.456411f, 0.462077f, 0.467784f, 0.473532f, 0.479320f, 0.485150f, 0.491021f,625 0.496933f, 0.502887f, 0.508881f, 0.514918f, 0.520996f, 0.527115f, 0.533276f, 0.539480f, 0.545725f, 0.552011f, 0.558340f,626 0.564712f, 0.571125f, 0.577581f, 0.584078f, 0.590619f, 0.597202f, 0.603827f, 0.610496f, 0.617207f, 0.623960f, 0.630757f,627 0.637597f, 0.644480f, 0.651406f, 0.658375f, 0.665387f, 0.672443f, 0.679543f, 0.686685f, 0.693872f, 0.701102f, 0.708376f,628 0.715694f, 0.723055f, 0.730461f, 0.737911f, 0.745404f, 0.752942f, 0.760525f, 0.768151f, 0.775822f, 0.783538f, 0.791298f,629 0.799103f, 0.806952f, 0.814847f, 0.822786f, 0.830770f, 0.838799f, 0.846873f, 0.854993f, 0.863157f, 0.871367f, 0.879622f,630 0.887923f, 0.896269f, 0.904661f, 0.913099f, 0.921582f, 0.930111f, 0.938686f, 0.947307f, 0.955974f, 0.964686f, 0.973445f,631 0.982251f, 0.991102f, 1.0f632};633634static float stbir__srgb_to_linear(float f)635{636 if (f <= 0.04045f)637 return f / 12.92f;638 else639 return (float)pow((f + 0.055f) / 1.055f, 2.4f);640}641642static float stbir__linear_to_srgb(float f)643{644 if (f <= 0.0031308f)645 return f * 12.92f;646 else647 return 1.055f * (float)pow(f, 1 / 2.4f) - 0.055f;648}649650#ifndef STBIR_NON_IEEE_FLOAT651// From https://gist.github.com/rygorous/2203834652653typedef union654{655 stbir_uint32 u;656 float f;657} stbir__FP32;658659static const stbir_uint32 fp32_to_srgb8_tab4[104] = {660 0x0073000d, 0x007a000d, 0x0080000d, 0x0087000d, 0x008d000d, 0x0094000d, 0x009a000d, 0x00a1000d,661 0x00a7001a, 0x00b4001a, 0x00c1001a, 0x00ce001a, 0x00da001a, 0x00e7001a, 0x00f4001a, 0x0101001a,662 0x010e0033, 0x01280033, 0x01410033, 0x015b0033, 0x01750033, 0x018f0033, 0x01a80033, 0x01c20033,663 0x01dc0067, 0x020f0067, 0x02430067, 0x02760067, 0x02aa0067, 0x02dd0067, 0x03110067, 0x03440067,664 0x037800ce, 0x03df00ce, 0x044600ce, 0x04ad00ce, 0x051400ce, 0x057b00c5, 0x05dd00bc, 0x063b00b5,665 0x06970158, 0x07420142, 0x07e30130, 0x087b0120, 0x090b0112, 0x09940106, 0x0a1700fc, 0x0a9500f2,666 0x0b0f01cb, 0x0bf401ae, 0x0ccb0195, 0x0d950180, 0x0e56016e, 0x0f0d015e, 0x0fbc0150, 0x10630143,667 0x11070264, 0x1238023e, 0x1357021d, 0x14660201, 0x156601e9, 0x165a01d3, 0x174401c0, 0x182401af,668 0x18fe0331, 0x1a9602fe, 0x1c1502d2, 0x1d7e02ad, 0x1ed4028d, 0x201a0270, 0x21520256, 0x227d0240,669 0x239f0443, 0x25c003fe, 0x27bf03c4, 0x29a10392, 0x2b6a0367, 0x2d1d0341, 0x2ebe031f, 0x304d0300,670 0x31d105b0, 0x34a80555, 0x37520507, 0x39d504c5, 0x3c37048b, 0x3e7c0458, 0x40a8042a, 0x42bd0401,671 0x44c20798, 0x488e071e, 0x4c1c06b6, 0x4f76065d, 0x52a50610, 0x55ac05cc, 0x5892058f, 0x5b590559,672 0x5e0c0a23, 0x631c0980, 0x67db08f6, 0x6c55087f, 0x70940818, 0x74a007bd, 0x787d076c, 0x7c330723,673};674675static stbir_uint8 stbir__linear_to_srgb_uchar(float in)676{677 static const stbir__FP32 almostone = { 0x3f7fffff }; // 1-eps678 static const stbir__FP32 minval = { (127-13) << 23 };679 stbir_uint32 tab,bias,scale,t;680 stbir__FP32 f;681682 // Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively.683 // The tests are carefully written so that NaNs map to 0, same as in the reference684 // implementation.685 if (!(in > minval.f)) // written this way to catch NaNs686 in = minval.f;687 if (in > almostone.f)688 in = almostone.f;689690 // Do the table lookup and unpack bias, scale691 f.f = in;692 tab = fp32_to_srgb8_tab4[(f.u - minval.u) >> 20];693 bias = (tab >> 16) << 9;694 scale = tab & 0xffff;695696 // Grab next-highest mantissa bits and perform linear interpolation697 t = (f.u >> 12) & 0xff;698 return (unsigned char) ((bias + scale*t) >> 16);699}700701#else702// sRGB transition values, scaled by 1<<28703static int stbir__srgb_offset_to_linear_scaled[256] =704{705 0, 40738, 122216, 203693, 285170, 366648, 448125, 529603,706 611080, 692557, 774035, 855852, 942009, 1033024, 1128971, 1229926,707 1335959, 1447142, 1563542, 1685229, 1812268, 1944725, 2082664, 2226148,708 2375238, 2529996, 2690481, 2856753, 3028870, 3206888, 3390865, 3580856,709 3776916, 3979100, 4187460, 4402049, 4622919, 4850123, 5083710, 5323731,710 5570236, 5823273, 6082892, 6349140, 6622065, 6901714, 7188133, 7481369,711 7781466, 8088471, 8402427, 8723380, 9051372, 9386448, 9728650, 10078021,712 10434603, 10798439, 11169569, 11548036, 11933879, 12327139, 12727857, 13136073,713 13551826, 13975156, 14406100, 14844697, 15290987, 15745007, 16206795, 16676389,714 17153826, 17639142, 18132374, 18633560, 19142734, 19659934, 20185196, 20718552,715 21260042, 21809696, 22367554, 22933648, 23508010, 24090680, 24681686, 25281066,716 25888850, 26505076, 27129772, 27762974, 28404716, 29055026, 29713942, 30381490,717 31057708, 31742624, 32436272, 33138682, 33849884, 34569912, 35298800, 36036568,718 36783260, 37538896, 38303512, 39077136, 39859796, 40651528, 41452360, 42262316,719 43081432, 43909732, 44747252, 45594016, 46450052, 47315392, 48190064, 49074096,720 49967516, 50870356, 51782636, 52704392, 53635648, 54576432, 55526772, 56486700,721 57456236, 58435408, 59424248, 60422780, 61431036, 62449032, 63476804, 64514376,722 65561776, 66619028, 67686160, 68763192, 69850160, 70947088, 72053992, 73170912,723 74297864, 75434880, 76581976, 77739184, 78906536, 80084040, 81271736, 82469648,724 83677792, 84896192, 86124888, 87363888, 88613232, 89872928, 91143016, 92423512,725 93714432, 95015816, 96327688, 97650056, 98982952, 100326408, 101680440, 103045072,726 104420320, 105806224, 107202800, 108610064, 110028048, 111456776, 112896264, 114346544,727 115807632, 117279552, 118762328, 120255976, 121760536, 123276016, 124802440, 126339832,728 127888216, 129447616, 131018048, 132599544, 134192112, 135795792, 137410592, 139036528,729 140673648, 142321952, 143981456, 145652208, 147334208, 149027488, 150732064, 152447968,730 154175200, 155913792, 157663776, 159425168, 161197984, 162982240, 164777968, 166585184,731 168403904, 170234160, 172075968, 173929344, 175794320, 177670896, 179559120, 181458992,732 183370528, 185293776, 187228736, 189175424, 191133888, 193104112, 195086128, 197079968,733 199085648, 201103184, 203132592, 205173888, 207227120, 209292272, 211369392, 213458480,734 215559568, 217672656, 219797792, 221934976, 224084240, 226245600, 228419056, 230604656,735 232802400, 235012320, 237234432, 239468736, 241715280, 243974080, 246245120, 248528464,736 250824112, 253132064, 255452368, 257785040, 260130080, 262487520, 264857376, 267239664,737};738739static stbir_uint8 stbir__linear_to_srgb_uchar(float f)740{741 int x = (int) (f * (1 << 28)); // has headroom so you don't need to clamp742 int v = 0;743 int i;744745 // Refine the guess with a short binary search.746 i = v + 128; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;747 i = v + 64; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;748 i = v + 32; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;749 i = v + 16; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;750 i = v + 8; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;751 i = v + 4; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;752 i = v + 2; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;753 i = v + 1; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;754755 return (stbir_uint8) v;756}757#endif758759static float stbir__filter_trapezoid(float x, float scale)760{761 float halfscale = scale / 2;762 float t = 0.5f + halfscale;763 STBIR_ASSERT(scale <= 1);764765 x = (float)fabs(x);766767 if (x >= t)768 return 0;769 else770 {771 float r = 0.5f - halfscale;772 if (x <= r)773 return 1;774 else775 return (t - x) / scale;776 }777}778779static float stbir__support_trapezoid(float scale)780{781 STBIR_ASSERT(scale <= 1);782 return 0.5f + scale / 2;783}784785static float stbir__filter_triangle(float x, float s)786{787 STBIR__UNUSED_PARAM(s);788789 x = (float)fabs(x);790791 if (x <= 1.0f)792 return 1 - x;793 else794 return 0;795}796797static float stbir__filter_cubic(float x, float s)798{799 STBIR__UNUSED_PARAM(s);800801 x = (float)fabs(x);802803 if (x < 1.0f)804 return (4 + x*x*(3*x - 6))/6;805 else if (x < 2.0f)806 return (8 + x*(-12 + x*(6 - x)))/6;807808 return (0.0f);809}810811static float stbir__filter_catmullrom(float x, float s)812{813 STBIR__UNUSED_PARAM(s);814815 x = (float)fabs(x);816817 if (x < 1.0f)818 return 1 - x*x*(2.5f - 1.5f*x);819 else if (x < 2.0f)820 return 2 - x*(4 + x*(0.5f*x - 2.5f));821822 return (0.0f);823}824825static float stbir__filter_mitchell(float x, float s)826{827 STBIR__UNUSED_PARAM(s);828829 x = (float)fabs(x);830831 if (x < 1.0f)832 return (16 + x*x*(21 * x - 36))/18;833 else if (x < 2.0f)834 return (32 + x*(-60 + x*(36 - 7*x)))/18;835836 return (0.0f);837}838839static float stbir__support_zero(float s)840{841 STBIR__UNUSED_PARAM(s);842 return 0;843}844845static float stbir__support_one(float s)846{847 STBIR__UNUSED_PARAM(s);848 return 1;849}850851static float stbir__support_two(float s)852{853 STBIR__UNUSED_PARAM(s);854 return 2;855}856857static stbir__filter_info stbir__filter_info_table[] = {858 { NULL, stbir__support_zero },859 { stbir__filter_trapezoid, stbir__support_trapezoid },860 { stbir__filter_triangle, stbir__support_one },861 { stbir__filter_cubic, stbir__support_two },862 { stbir__filter_catmullrom, stbir__support_two },863 { stbir__filter_mitchell, stbir__support_two },864};865866stbir__inline static int stbir__use_upsampling(float ratio)867{868 return ratio > 1;869}870871stbir__inline static int stbir__use_width_upsampling(stbir__info* stbir_info)872{873 return stbir__use_upsampling(stbir_info->horizontal_scale);874}875876stbir__inline static int stbir__use_height_upsampling(stbir__info* stbir_info)877{878 return stbir__use_upsampling(stbir_info->vertical_scale);879}880881// This is the maximum number of input samples that can affect an output sample882// with the given filter883static int stbir__get_filter_pixel_width(stbir_filter filter, float scale)884{885 STBIR_ASSERT(filter != 0);886 STBIR_ASSERT(filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));887888 if (stbir__use_upsampling(scale))889 return (int)ceil(stbir__filter_info_table[filter].support(1/scale) * 2);890 else891 return (int)ceil(stbir__filter_info_table[filter].support(scale) * 2 / scale);892}893894// This is how much to expand buffers to account for filters seeking outside895// the image boundaries.896static int stbir__get_filter_pixel_margin(stbir_filter filter, float scale)897{898 return stbir__get_filter_pixel_width(filter, scale) / 2;899}900901static int stbir__get_coefficient_width(stbir_filter filter, float scale)902{903 if (stbir__use_upsampling(scale))904 return (int)ceil(stbir__filter_info_table[filter].support(1 / scale) * 2);905 else906 return (int)ceil(stbir__filter_info_table[filter].support(scale) * 2);907}908909static int stbir__get_contributors(float scale, stbir_filter filter, int input_size, int output_size)910{911 if (stbir__use_upsampling(scale))912 return output_size;913 else914 return (input_size + stbir__get_filter_pixel_margin(filter, scale) * 2);915}916917static int stbir__get_total_horizontal_coefficients(stbir__info* info)918{919 return info->horizontal_num_contributors920 * stbir__get_coefficient_width (info->horizontal_filter, info->horizontal_scale);921}922923static int stbir__get_total_vertical_coefficients(stbir__info* info)924{925 return info->vertical_num_contributors926 * stbir__get_coefficient_width (info->vertical_filter, info->vertical_scale);927}928929static stbir__contributors* stbir__get_contributor(stbir__contributors* contributors, int n)930{931 return &contributors[n];932}933934// For perf reasons this code is duplicated in stbir__resample_horizontal_upsample/downsample,935// if you change it here change it there too.936static float* stbir__get_coefficient(float* coefficients, stbir_filter filter, float scale, int n, int c)937{938 int width = stbir__get_coefficient_width(filter, scale);939 return &coefficients[width*n + c];940}941942static int stbir__edge_wrap_slow(stbir_edge edge, int n, int max)943{944 switch (edge)945 {946 case STBIR_EDGE_ZERO:947 return 0; // we'll decode the wrong pixel here, and then overwrite with 0s later948949 case STBIR_EDGE_CLAMP:950 if (n < 0)951 return 0;952953 if (n >= max)954 return max - 1;955956 return n; // NOTREACHED957958 case STBIR_EDGE_REFLECT:959 {960 if (n < 0)961 {962 if (n < max)963 return -n;964 else965 return max - 1;966 }967968 if (n >= max)969 {970 int max2 = max * 2;971 if (n >= max2)972 return 0;973 else974 return max2 - n - 1;975 }976977 return n; // NOTREACHED978 }979980 case STBIR_EDGE_WRAP:981 if (n >= 0)982 return (n % max);983 else984 {985 int m = (-n) % max;986987 if (m != 0)988 m = max - m;989990 return (m);991 }992 // NOTREACHED993994 default:995 STBIR_ASSERT(!"Unimplemented edge type");996 return 0;997 }998}9991000stbir__inline static int stbir__edge_wrap(stbir_edge edge, int n, int max)1001{1002 // avoid per-pixel switch1003 if (n >= 0 && n < max)1004 return n;1005 return stbir__edge_wrap_slow(edge, n, max);1006}10071008// What input pixels contribute to this output pixel?1009static 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)1010{1011 float out_pixel_center = (float)n + 0.5f;1012 float out_pixel_influence_lowerbound = out_pixel_center - out_filter_radius;1013 float out_pixel_influence_upperbound = out_pixel_center + out_filter_radius;10141015 float in_pixel_influence_lowerbound = (out_pixel_influence_lowerbound + out_shift) / scale_ratio;1016 float in_pixel_influence_upperbound = (out_pixel_influence_upperbound + out_shift) / scale_ratio;10171018 *in_center_of_out = (out_pixel_center + out_shift) / scale_ratio;1019 *in_first_pixel = (int)(floor(in_pixel_influence_lowerbound + 0.5));1020 *in_last_pixel = (int)(floor(in_pixel_influence_upperbound - 0.5));1021}10221023// What output pixels does this input pixel contribute to?1024static 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)1025{1026 float in_pixel_center = (float)n + 0.5f;1027 float in_pixel_influence_lowerbound = in_pixel_center - in_pixels_radius;1028 float in_pixel_influence_upperbound = in_pixel_center + in_pixels_radius;10291030 float out_pixel_influence_lowerbound = in_pixel_influence_lowerbound * scale_ratio - out_shift;1031 float out_pixel_influence_upperbound = in_pixel_influence_upperbound * scale_ratio - out_shift;10321033 *out_center_of_in = in_pixel_center * scale_ratio - out_shift;1034 *out_first_pixel = (int)(floor(out_pixel_influence_lowerbound + 0.5));1035 *out_last_pixel = (int)(floor(out_pixel_influence_upperbound - 0.5));1036}10371038static 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)1039{1040 int i;1041 float total_filter = 0;1042 float filter_scale;10431044 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.10451046 contributor->n0 = in_first_pixel;1047 contributor->n1 = in_last_pixel;10481049 STBIR_ASSERT(contributor->n1 >= contributor->n0);10501051 for (i = 0; i <= in_last_pixel - in_first_pixel; i++)1052 {1053 float in_pixel_center = (float)(i + in_first_pixel) + 0.5f;1054 coefficient_group[i] = stbir__filter_info_table[filter].kernel(in_center_of_out - in_pixel_center, 1 / scale);10551056 // If the coefficient is zero, skip it. (Don't do the <0 check here, we want the influence of those outside pixels.)1057 if (i == 0 && !coefficient_group[i])1058 {1059 contributor->n0 = ++in_first_pixel;1060 i--;1061 continue;1062 }10631064 total_filter += coefficient_group[i];1065 }10661067 // NOTE(fg): Not actually true in general, nor is there any reason to expect it should be.1068 // It would be true in exact math but is at best approximately true in floating-point math,1069 // and it would not make sense to try and put actual bounds on this here because it depends1070 // on the image aspect ratio which can get pretty extreme.1071 //STBIR_ASSERT(stbir__filter_info_table[filter].kernel((float)(in_last_pixel + 1) + 0.5f - in_center_of_out, 1/scale) == 0);10721073 STBIR_ASSERT(total_filter > 0.9);1074 STBIR_ASSERT(total_filter < 1.1f); // Make sure it's not way off.10751076 // Make sure the sum of all coefficients is 1.1077 filter_scale = 1 / total_filter;10781079 for (i = 0; i <= in_last_pixel - in_first_pixel; i++)1080 coefficient_group[i] *= filter_scale;10811082 for (i = in_last_pixel - in_first_pixel; i >= 0; i--)1083 {1084 if (coefficient_group[i])1085 break;10861087 // This line has no weight. We can skip it.1088 contributor->n1 = contributor->n0 + i - 1;1089 }1090}10911092static 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)1093{1094 int i;10951096 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.10971098 contributor->n0 = out_first_pixel;1099 contributor->n1 = out_last_pixel;11001101 STBIR_ASSERT(contributor->n1 >= contributor->n0);11021103 for (i = 0; i <= out_last_pixel - out_first_pixel; i++)1104 {1105 float out_pixel_center = (float)(i + out_first_pixel) + 0.5f;1106 float x = out_pixel_center - out_center_of_in;1107 coefficient_group[i] = stbir__filter_info_table[filter].kernel(x, scale_ratio) * scale_ratio;1108 }11091110 // NOTE(fg): Not actually true in general, nor is there any reason to expect it should be.1111 // It would be true in exact math but is at best approximately true in floating-point math,1112 // and it would not make sense to try and put actual bounds on this here because it depends1113 // on the image aspect ratio which can get pretty extreme.1114 //STBIR_ASSERT(stbir__filter_info_table[filter].kernel((float)(out_last_pixel + 1) + 0.5f - out_center_of_in, scale_ratio) == 0);11151116 for (i = out_last_pixel - out_first_pixel; i >= 0; i--)1117 {1118 if (coefficient_group[i])1119 break;11201121 // This line has no weight. We can skip it.1122 contributor->n1 = contributor->n0 + i - 1;1123 }1124}11251126static void stbir__normalize_downsample_coefficients(stbir__contributors* contributors, float* coefficients, stbir_filter filter, float scale_ratio, int input_size, int output_size)1127{1128 int num_contributors = stbir__get_contributors(scale_ratio, filter, input_size, output_size);1129 int num_coefficients = stbir__get_coefficient_width(filter, scale_ratio);1130 int i, j;1131 int skip;11321133 for (i = 0; i < output_size; i++)1134 {1135 float scale;1136 float total = 0;11371138 for (j = 0; j < num_contributors; j++)1139 {1140 if (i >= contributors[j].n0 && i <= contributors[j].n1)1141 {1142 float coefficient = *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i - contributors[j].n0);1143 total += coefficient;1144 }1145 else if (i < contributors[j].n0)1146 break;1147 }11481149 STBIR_ASSERT(total > 0.9f);1150 STBIR_ASSERT(total < 1.1f);11511152 scale = 1 / total;11531154 for (j = 0; j < num_contributors; j++)1155 {1156 if (i >= contributors[j].n0 && i <= contributors[j].n1)1157 *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i - contributors[j].n0) *= scale;1158 else if (i < contributors[j].n0)1159 break;1160 }1161 }11621163 // Optimize: Skip zero coefficients and contributions outside of image bounds.1164 // Do this after normalizing because normalization depends on the n0/n1 values.1165 for (j = 0; j < num_contributors; j++)1166 {1167 int range, max, width;11681169 skip = 0;1170 while (*stbir__get_coefficient(coefficients, filter, scale_ratio, j, skip) == 0)1171 skip++;11721173 contributors[j].n0 += skip;11741175 while (contributors[j].n0 < 0)1176 {1177 contributors[j].n0++;1178 skip++;1179 }11801181 range = contributors[j].n1 - contributors[j].n0 + 1;1182 max = stbir__min(num_coefficients, range);11831184 width = stbir__get_coefficient_width(filter, scale_ratio);1185 for (i = 0; i < max; i++)1186 {1187 if (i + skip >= width)1188 break;11891190 *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i) = *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i + skip);1191 }11921193 continue;1194 }11951196 // Using min to avoid writing into invalid pixels.1197 for (i = 0; i < num_contributors; i++)1198 contributors[i].n1 = stbir__min(contributors[i].n1, output_size - 1);1199}12001201// Each scan line uses the same kernel values so we should calculate the kernel1202// values once and then we can use them for every scan line.1203static void stbir__calculate_filters(stbir__contributors* contributors, float* coefficients, stbir_filter filter, float scale_ratio, float shift, int input_size, int output_size)1204{1205 int n;1206 int total_contributors = stbir__get_contributors(scale_ratio, filter, input_size, output_size);12071208 if (stbir__use_upsampling(scale_ratio))1209 {1210 float out_pixels_radius = stbir__filter_info_table[filter].support(1 / scale_ratio) * scale_ratio;12111212 // Looping through out pixels1213 for (n = 0; n < total_contributors; n++)1214 {1215 float in_center_of_out; // Center of the current out pixel in the in pixel space1216 int in_first_pixel, in_last_pixel;12171218 stbir__calculate_sample_range_upsample(n, out_pixels_radius, scale_ratio, shift, &in_first_pixel, &in_last_pixel, &in_center_of_out);12191220 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));1221 }1222 }1223 else1224 {1225 float in_pixels_radius = stbir__filter_info_table[filter].support(scale_ratio) / scale_ratio;12261227 // Looping through in pixels1228 for (n = 0; n < total_contributors; n++)1229 {1230 float out_center_of_in; // Center of the current out pixel in the in pixel space1231 int out_first_pixel, out_last_pixel;1232 int n_adjusted = n - stbir__get_filter_pixel_margin(filter, scale_ratio);12331234 stbir__calculate_sample_range_downsample(n_adjusted, in_pixels_radius, scale_ratio, shift, &out_first_pixel, &out_last_pixel, &out_center_of_in);12351236 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));1237 }12381239 stbir__normalize_downsample_coefficients(contributors, coefficients, filter, scale_ratio, input_size, output_size);1240 }1241}12421243static float* stbir__get_decode_buffer(stbir__info* stbir_info)1244{1245 // The 0 index of the decode buffer starts after the margin. This makes1246 // it okay to use negative indexes on the decode buffer.1247 return &stbir_info->decode_buffer[stbir_info->horizontal_filter_pixel_margin * stbir_info->channels];1248}12491250#define STBIR__DECODE(type, colorspace) ((int)(type) * (STBIR_MAX_COLORSPACES) + (int)(colorspace))12511252static void stbir__decode_scanline(stbir__info* stbir_info, int n)1253{1254 int c;1255 int channels = stbir_info->channels;1256 int alpha_channel = stbir_info->alpha_channel;1257 int type = stbir_info->type;1258 int colorspace = stbir_info->colorspace;1259 int input_w = stbir_info->input_w;1260 size_t input_stride_bytes = stbir_info->input_stride_bytes;1261 float* decode_buffer = stbir__get_decode_buffer(stbir_info);1262 stbir_edge edge_horizontal = stbir_info->edge_horizontal;1263 stbir_edge edge_vertical = stbir_info->edge_vertical;1264 size_t in_buffer_row_offset = stbir__edge_wrap(edge_vertical, n, stbir_info->input_h) * input_stride_bytes;1265 const void* input_data = (char *) stbir_info->input_data + in_buffer_row_offset;1266 int max_x = input_w + stbir_info->horizontal_filter_pixel_margin;1267 int decode = STBIR__DECODE(type, colorspace);12681269 int x = -stbir_info->horizontal_filter_pixel_margin;12701271 // special handling for STBIR_EDGE_ZERO because it needs to return an item that doesn't appear in the input,1272 // and we want to avoid paying overhead on every pixel if not STBIR_EDGE_ZERO1273 if (edge_vertical == STBIR_EDGE_ZERO && (n < 0 || n >= stbir_info->input_h))1274 {1275 for (; x < max_x; x++)1276 for (c = 0; c < channels; c++)1277 decode_buffer[x*channels + c] = 0;1278 return;1279 }12801281 switch (decode)1282 {1283 case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_LINEAR):1284 for (; x < max_x; x++)1285 {1286 int decode_pixel_index = x * channels;1287 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1288 for (c = 0; c < channels; c++)1289 decode_buffer[decode_pixel_index + c] = ((float)((const unsigned char*)input_data)[input_pixel_index + c]) / stbir__max_uint8_as_float;1290 }1291 break;12921293 case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_SRGB):1294 for (; x < max_x; x++)1295 {1296 int decode_pixel_index = x * channels;1297 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1298 for (c = 0; c < channels; c++)1299 decode_buffer[decode_pixel_index + c] = stbir__srgb_uchar_to_linear_float[((const unsigned char*)input_data)[input_pixel_index + c]];13001301 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1302 decode_buffer[decode_pixel_index + alpha_channel] = ((float)((const unsigned char*)input_data)[input_pixel_index + alpha_channel]) / stbir__max_uint8_as_float;1303 }1304 break;13051306 case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR):1307 for (; x < max_x; x++)1308 {1309 int decode_pixel_index = x * channels;1310 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1311 for (c = 0; c < channels; c++)1312 decode_buffer[decode_pixel_index + c] = ((float)((const unsigned short*)input_data)[input_pixel_index + c]) / stbir__max_uint16_as_float;1313 }1314 break;13151316 case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB):1317 for (; x < max_x; x++)1318 {1319 int decode_pixel_index = x * channels;1320 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1321 for (c = 0; c < channels; c++)1322 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);13231324 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1325 decode_buffer[decode_pixel_index + alpha_channel] = ((float)((const unsigned short*)input_data)[input_pixel_index + alpha_channel]) / stbir__max_uint16_as_float;1326 }1327 break;13281329 case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR):1330 for (; x < max_x; x++)1331 {1332 int decode_pixel_index = x * channels;1333 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1334 for (c = 0; c < channels; c++)1335 decode_buffer[decode_pixel_index + c] = (float)(((double)((const unsigned int*)input_data)[input_pixel_index + c]) / stbir__max_uint32_as_float);1336 }1337 break;13381339 case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB):1340 for (; x < max_x; x++)1341 {1342 int decode_pixel_index = x * channels;1343 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1344 for (c = 0; c < channels; c++)1345 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));13461347 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1348 decode_buffer[decode_pixel_index + alpha_channel] = (float)(((double)((const unsigned int*)input_data)[input_pixel_index + alpha_channel]) / stbir__max_uint32_as_float);1349 }1350 break;13511352 case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR):1353 for (; x < max_x; x++)1354 {1355 int decode_pixel_index = x * channels;1356 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1357 for (c = 0; c < channels; c++)1358 decode_buffer[decode_pixel_index + c] = ((const float*)input_data)[input_pixel_index + c];1359 }1360 break;13611362 case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB):1363 for (; x < max_x; x++)1364 {1365 int decode_pixel_index = x * channels;1366 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1367 for (c = 0; c < channels; c++)1368 decode_buffer[decode_pixel_index + c] = stbir__srgb_to_linear(((const float*)input_data)[input_pixel_index + c]);13691370 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1371 decode_buffer[decode_pixel_index + alpha_channel] = ((const float*)input_data)[input_pixel_index + alpha_channel];1372 }13731374 break;13751376 default:1377 STBIR_ASSERT(!"Unknown type/colorspace/channels combination.");1378 break;1379 }13801381 if (!(stbir_info->flags & STBIR_FLAG_ALPHA_PREMULTIPLIED))1382 {1383 for (x = -stbir_info->horizontal_filter_pixel_margin; x < max_x; x++)1384 {1385 int decode_pixel_index = x * channels;13861387 // If the alpha value is 0 it will clobber the color values. Make sure it's not.1388 float alpha = decode_buffer[decode_pixel_index + alpha_channel];1389#ifndef STBIR_NO_ALPHA_EPSILON1390 if (stbir_info->type != STBIR_TYPE_FLOAT) {1391 alpha += STBIR_ALPHA_EPSILON;1392 decode_buffer[decode_pixel_index + alpha_channel] = alpha;1393 }1394#endif1395 for (c = 0; c < channels; c++)1396 {1397 if (c == alpha_channel)1398 continue;13991400 decode_buffer[decode_pixel_index + c] *= alpha;1401 }1402 }1403 }14041405 if (edge_horizontal == STBIR_EDGE_ZERO)1406 {1407 for (x = -stbir_info->horizontal_filter_pixel_margin; x < 0; x++)1408 {1409 for (c = 0; c < channels; c++)1410 decode_buffer[x*channels + c] = 0;1411 }1412 for (x = input_w; x < max_x; x++)1413 {1414 for (c = 0; c < channels; c++)1415 decode_buffer[x*channels + c] = 0;1416 }1417 }1418}14191420static float* stbir__get_ring_buffer_entry(float* ring_buffer, int index, int ring_buffer_length)1421{1422 return &ring_buffer[index * ring_buffer_length];1423}14241425static float* stbir__add_empty_ring_buffer_entry(stbir__info* stbir_info, int n)1426{1427 int ring_buffer_index;1428 float* ring_buffer;14291430 stbir_info->ring_buffer_last_scanline = n;14311432 if (stbir_info->ring_buffer_begin_index < 0)1433 {1434 ring_buffer_index = stbir_info->ring_buffer_begin_index = 0;1435 stbir_info->ring_buffer_first_scanline = n;1436 }1437 else1438 {1439 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;1440 STBIR_ASSERT(ring_buffer_index != stbir_info->ring_buffer_begin_index);1441 }14421443 ring_buffer = stbir__get_ring_buffer_entry(stbir_info->ring_buffer, ring_buffer_index, stbir_info->ring_buffer_length_bytes / sizeof(float));1444 memset(ring_buffer, 0, stbir_info->ring_buffer_length_bytes);14451446 return ring_buffer;1447}144814491450static void stbir__resample_horizontal_upsample(stbir__info* stbir_info, float* output_buffer)1451{1452 int x, k;1453 int output_w = stbir_info->output_w;1454 int channels = stbir_info->channels;1455 float* decode_buffer = stbir__get_decode_buffer(stbir_info);1456 stbir__contributors* horizontal_contributors = stbir_info->horizontal_contributors;1457 float* horizontal_coefficients = stbir_info->horizontal_coefficients;1458 int coefficient_width = stbir_info->horizontal_coefficient_width;14591460 for (x = 0; x < output_w; x++)1461 {1462 int n0 = horizontal_contributors[x].n0;1463 int n1 = horizontal_contributors[x].n1;14641465 int out_pixel_index = x * channels;1466 int coefficient_group = coefficient_width * x;1467 int coefficient_counter = 0;14681469 STBIR_ASSERT(n1 >= n0);1470 STBIR_ASSERT(n0 >= -stbir_info->horizontal_filter_pixel_margin);1471 STBIR_ASSERT(n1 >= -stbir_info->horizontal_filter_pixel_margin);1472 STBIR_ASSERT(n0 < stbir_info->input_w + stbir_info->horizontal_filter_pixel_margin);1473 STBIR_ASSERT(n1 < stbir_info->input_w + stbir_info->horizontal_filter_pixel_margin);14741475 switch (channels) {1476 case 1:1477 for (k = n0; k <= n1; k++)1478 {1479 int in_pixel_index = k * 1;1480 float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++];1481 STBIR_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_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_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_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_ASSERT(coefficient != 0);1525 for (c = 0; c < channels; c++)1526 output_buffer[out_pixel_index + c] += decode_buffer[in_pixel_index + c] * coefficient;1527 }1528 break;1529 }1530 }1531}15321533static void stbir__resample_horizontal_downsample(stbir__info* stbir_info, float* output_buffer)1534{1535 int x, k;1536 int input_w = stbir_info->input_w;1537 int channels = stbir_info->channels;1538 float* decode_buffer = stbir__get_decode_buffer(stbir_info);1539 stbir__contributors* horizontal_contributors = stbir_info->horizontal_contributors;1540 float* horizontal_coefficients = stbir_info->horizontal_coefficients;1541 int coefficient_width = stbir_info->horizontal_coefficient_width;1542 int filter_pixel_margin = stbir_info->horizontal_filter_pixel_margin;1543 int max_x = input_w + filter_pixel_margin * 2;15441545 STBIR_ASSERT(!stbir__use_width_upsampling(stbir_info));15461547 switch (channels) {1548 case 1:1549 for (x = 0; x < max_x; x++)1550 {1551 int n0 = horizontal_contributors[x].n0;1552 int n1 = horizontal_contributors[x].n1;15531554 int in_x = x - filter_pixel_margin;1555 int in_pixel_index = in_x * 1;1556 int max_n = n1;1557 int coefficient_group = coefficient_width * x;15581559 for (k = n0; k <= max_n; k++)1560 {1561 int out_pixel_index = k * 1;1562 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1563 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1564 }1565 }1566 break;15671568 case 2:1569 for (x = 0; x < max_x; x++)1570 {1571 int n0 = horizontal_contributors[x].n0;1572 int n1 = horizontal_contributors[x].n1;15731574 int in_x = x - filter_pixel_margin;1575 int in_pixel_index = in_x * 2;1576 int max_n = n1;1577 int coefficient_group = coefficient_width * x;15781579 for (k = n0; k <= max_n; k++)1580 {1581 int out_pixel_index = k * 2;1582 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1583 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1584 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1585 }1586 }1587 break;15881589 case 3:1590 for (x = 0; x < max_x; x++)1591 {1592 int n0 = horizontal_contributors[x].n0;1593 int n1 = horizontal_contributors[x].n1;15941595 int in_x = x - filter_pixel_margin;1596 int in_pixel_index = in_x * 3;1597 int max_n = n1;1598 int coefficient_group = coefficient_width * x;15991600 for (k = n0; k <= max_n; k++)1601 {1602 int out_pixel_index = k * 3;1603 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1604 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1605 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1606 output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient;1607 }1608 }1609 break;16101611 case 4:1612 for (x = 0; x < max_x; x++)1613 {1614 int n0 = horizontal_contributors[x].n0;1615 int n1 = horizontal_contributors[x].n1;16161617 int in_x = x - filter_pixel_margin;1618 int in_pixel_index = in_x * 4;1619 int max_n = n1;1620 int coefficient_group = coefficient_width * x;16211622 for (k = n0; k <= max_n; k++)1623 {1624 int out_pixel_index = k * 4;1625 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1626 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1627 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1628 output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient;1629 output_buffer[out_pixel_index + 3] += decode_buffer[in_pixel_index + 3] * coefficient;1630 }1631 }1632 break;16331634 default: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 * channels;1642 int max_n = n1;1643 int coefficient_group = coefficient_width * x;16441645 for (k = n0; k <= max_n; k++)1646 {1647 int c;1648 int out_pixel_index = k * channels;1649 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1650 for (c = 0; c < channels; c++)1651 output_buffer[out_pixel_index + c] += decode_buffer[in_pixel_index + c] * coefficient;1652 }1653 }1654 break;1655 }1656}16571658static void stbir__decode_and_resample_upsample(stbir__info* stbir_info, int n)1659{1660 // Decode the nth scanline from the source image into the decode buffer.1661 stbir__decode_scanline(stbir_info, n);16621663 // Now resample it into the ring buffer.1664 if (stbir__use_width_upsampling(stbir_info))1665 stbir__resample_horizontal_upsample(stbir_info, stbir__add_empty_ring_buffer_entry(stbir_info, n));1666 else1667 stbir__resample_horizontal_downsample(stbir_info, stbir__add_empty_ring_buffer_entry(stbir_info, n));16681669 // Now it's sitting in the ring buffer ready to be used as source for the vertical sampling.1670}16711672static void stbir__decode_and_resample_downsample(stbir__info* stbir_info, int n)1673{1674 // Decode the nth scanline from the source image into the decode buffer.1675 stbir__decode_scanline(stbir_info, n);16761677 memset(stbir_info->horizontal_buffer, 0, stbir_info->output_w * stbir_info->channels * sizeof(float));16781679 // Now resample it into the horizontal buffer.1680 if (stbir__use_width_upsampling(stbir_info))1681 stbir__resample_horizontal_upsample(stbir_info, stbir_info->horizontal_buffer);1682 else1683 stbir__resample_horizontal_downsample(stbir_info, stbir_info->horizontal_buffer);16841685 // Now it's sitting in the horizontal buffer ready to be distributed into the ring buffers.1686}16871688// Get the specified scan line from the ring buffer.1689static 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)1690{1691 int ring_buffer_index = (begin_index + (get_scanline - first_scanline)) % ring_buffer_num_entries;1692 return stbir__get_ring_buffer_entry(ring_buffer, ring_buffer_index, ring_buffer_length);1693}169416951696static void stbir__encode_scanline(stbir__info* stbir_info, int num_pixels, void *output_buffer, float *encode_buffer, int channels, int alpha_channel, int decode)1697{1698 int x;1699 int n;1700 int num_nonalpha;1701 stbir_uint16 nonalpha[STBIR_MAX_CHANNELS];17021703 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_PREMULTIPLIED))1704 {1705 for (x=0; x < num_pixels; ++x)1706 {1707 int pixel_index = x*channels;17081709 float alpha = encode_buffer[pixel_index + alpha_channel];1710 float reciprocal_alpha = alpha ? 1.0f / alpha : 0;17111712 // unrolling this produced a 1% slowdown upscaling a large RGBA linear-space image on my machine - stb1713 for (n = 0; n < channels; n++)1714 if (n != alpha_channel)1715 encode_buffer[pixel_index + n] *= reciprocal_alpha;17161717 // We added in a small epsilon to prevent the color channel from being deleted with zero alpha.1718 // Because we only add it for integer types, it will automatically be discarded on integer1719 // conversion, so we don't need to subtract it back out (which would be problematic for1720 // numeric precision reasons).1721 }1722 }17231724 // build a table of all channels that need colorspace correction, so1725 // we don't perform colorspace correction on channels that don't need it.1726 for (x = 0, num_nonalpha = 0; x < channels; ++x)1727 {1728 if (x != alpha_channel || (stbir_info->flags & STBIR_FLAG_ALPHA_USES_COLORSPACE))1729 {1730 nonalpha[num_nonalpha++] = (stbir_uint16)x;1731 }1732 }17331734 #define STBIR__ROUND_INT(f) ((int) ((f)+0.5))1735 #define STBIR__ROUND_UINT(f) ((stbir_uint32) ((f)+0.5))17361737 #ifdef STBIR__SATURATE_INT1738 #define STBIR__ENCODE_LINEAR8(f) stbir__saturate8 (STBIR__ROUND_INT((f) * stbir__max_uint8_as_float ))1739 #define STBIR__ENCODE_LINEAR16(f) stbir__saturate16(STBIR__ROUND_INT((f) * stbir__max_uint16_as_float))1740 #else1741 #define STBIR__ENCODE_LINEAR8(f) (unsigned char ) STBIR__ROUND_INT(stbir__saturate(f) * stbir__max_uint8_as_float )1742 #define STBIR__ENCODE_LINEAR16(f) (unsigned short) STBIR__ROUND_INT(stbir__saturate(f) * stbir__max_uint16_as_float)1743 #endif17441745 switch (decode)1746 {1747 case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_LINEAR):1748 for (x=0; x < num_pixels; ++x)1749 {1750 int pixel_index = x*channels;17511752 for (n = 0; n < channels; n++)1753 {1754 int index = pixel_index + n;1755 ((unsigned char*)output_buffer)[index] = STBIR__ENCODE_LINEAR8(encode_buffer[index]);1756 }1757 }1758 break;17591760 case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_SRGB):1761 for (x=0; x < num_pixels; ++x)1762 {1763 int pixel_index = x*channels;17641765 for (n = 0; n < num_nonalpha; n++)1766 {1767 int index = pixel_index + nonalpha[n];1768 ((unsigned char*)output_buffer)[index] = stbir__linear_to_srgb_uchar(encode_buffer[index]);1769 }17701771 if (!(stbir_info->flags & STBIR_FLAG_ALPHA_USES_COLORSPACE))1772 ((unsigned char *)output_buffer)[pixel_index + alpha_channel] = STBIR__ENCODE_LINEAR8(encode_buffer[pixel_index+alpha_channel]);1773 }1774 break;17751776 case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR):1777 for (x=0; x < num_pixels; ++x)1778 {1779 int pixel_index = x*channels;17801781 for (n = 0; n < channels; n++)1782 {1783 int index = pixel_index + n;1784 ((unsigned short*)output_buffer)[index] = STBIR__ENCODE_LINEAR16(encode_buffer[index]);1785 }1786 }1787 break;17881789 case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB):1790 for (x=0; x < num_pixels; ++x)1791 {1792 int pixel_index = x*channels;17931794 for (n = 0; n < num_nonalpha; n++)1795 {1796 int index = pixel_index + nonalpha[n];1797 ((unsigned short*)output_buffer)[index] = (unsigned short)STBIR__ROUND_INT(stbir__linear_to_srgb(stbir__saturate(encode_buffer[index])) * stbir__max_uint16_as_float);1798 }17991800 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1801 ((unsigned short*)output_buffer)[pixel_index + alpha_channel] = STBIR__ENCODE_LINEAR16(encode_buffer[pixel_index + alpha_channel]);1802 }18031804 break;18051806 case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR):1807 for (x=0; x < num_pixels; ++x)1808 {1809 int pixel_index = x*channels;18101811 for (n = 0; n < channels; n++)1812 {1813 int index = pixel_index + n;1814 ((unsigned int*)output_buffer)[index] = (unsigned int)STBIR__ROUND_UINT(((double)stbir__saturate(encode_buffer[index])) * stbir__max_uint32_as_float);1815 }1816 }1817 break;18181819 case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB):1820 for (x=0; x < num_pixels; ++x)1821 {1822 int pixel_index = x*channels;18231824 for (n = 0; n < num_nonalpha; n++)1825 {1826 int index = pixel_index + nonalpha[n];1827 ((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);1828 }18291830 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1831 ((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);1832 }1833 break;18341835 case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR):1836 for (x=0; x < num_pixels; ++x)1837 {1838 int pixel_index = x*channels;18391840 for (n = 0; n < channels; n++)1841 {1842 int index = pixel_index + n;1843 ((float*)output_buffer)[index] = encode_buffer[index];1844 }1845 }1846 break;18471848 case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB):1849 for (x=0; x < num_pixels; ++x)1850 {1851 int pixel_index = x*channels;18521853 for (n = 0; n < num_nonalpha; n++)1854 {1855 int index = pixel_index + nonalpha[n];1856 ((float*)output_buffer)[index] = stbir__linear_to_srgb(encode_buffer[index]);1857 }18581859 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1860 ((float*)output_buffer)[pixel_index + alpha_channel] = encode_buffer[pixel_index + alpha_channel];1861 }1862 break;18631864 default:1865 STBIR_ASSERT(!"Unknown type/colorspace/channels combination.");1866 break;1867 }1868}18691870static void stbir__resample_vertical_upsample(stbir__info* stbir_info, int n)1871{1872 int x, k;1873 int output_w = stbir_info->output_w;1874 stbir__contributors* vertical_contributors = stbir_info->vertical_contributors;1875 float* vertical_coefficients = stbir_info->vertical_coefficients;1876 int channels = stbir_info->channels;1877 int alpha_channel = stbir_info->alpha_channel;1878 int type = stbir_info->type;1879 int colorspace = stbir_info->colorspace;1880 int ring_buffer_entries = stbir_info->ring_buffer_num_entries;1881 void* output_data = stbir_info->output_data;1882 float* encode_buffer = stbir_info->encode_buffer;1883 int decode = STBIR__DECODE(type, colorspace);1884 int coefficient_width = stbir_info->vertical_coefficient_width;1885 int coefficient_counter;1886 int contributor = n;18871888 float* ring_buffer = stbir_info->ring_buffer;1889 int ring_buffer_begin_index = stbir_info->ring_buffer_begin_index;1890 int ring_buffer_first_scanline = stbir_info->ring_buffer_first_scanline;1891 int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float);18921893 int n0,n1, output_row_start;1894 int coefficient_group = coefficient_width * contributor;18951896 n0 = vertical_contributors[contributor].n0;1897 n1 = vertical_contributors[contributor].n1;18981899 output_row_start = n * stbir_info->output_stride_bytes;19001901 STBIR_ASSERT(stbir__use_height_upsampling(stbir_info));19021903 memset(encode_buffer, 0, output_w * sizeof(float) * channels);19041905 // I tried reblocking this for better cache usage of encode_buffer1906 // (using x_outer, k, x_inner), but it lost speed. -- stb19071908 coefficient_counter = 0;1909 switch (channels) {1910 case 1:1911 for (k = n0; k <= n1; k++)1912 {1913 int coefficient_index = coefficient_counter++;1914 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);1915 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];1916 for (x = 0; x < output_w; ++x)1917 {1918 int in_pixel_index = x * 1;1919 encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;1920 }1921 }1922 break;1923 case 2:1924 for (k = n0; k <= n1; k++)1925 {1926 int coefficient_index = coefficient_counter++;1927 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);1928 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];1929 for (x = 0; x < output_w; ++x)1930 {1931 int in_pixel_index = x * 2;1932 encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;1933 encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient;1934 }1935 }1936 break;1937 case 3:1938 for (k = n0; k <= n1; k++)1939 {1940 int coefficient_index = coefficient_counter++;1941 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);1942 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];1943 for (x = 0; x < output_w; ++x)1944 {1945 int in_pixel_index = x * 3;1946 encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;1947 encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient;1948 encode_buffer[in_pixel_index + 2] += ring_buffer_entry[in_pixel_index + 2] * coefficient;1949 }1950 }1951 break;1952 case 4:1953 for (k = n0; k <= n1; k++)1954 {1955 int coefficient_index = coefficient_counter++;1956 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);1957 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];1958 for (x = 0; x < output_w; ++x)1959 {1960 int in_pixel_index = x * 4;1961 encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;1962 encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient;1963 encode_buffer[in_pixel_index + 2] += ring_buffer_entry[in_pixel_index + 2] * coefficient;1964 encode_buffer[in_pixel_index + 3] += ring_buffer_entry[in_pixel_index + 3] * coefficient;1965 }1966 }1967 break;1968 default:1969 for (k = n0; k <= n1; k++)1970 {1971 int coefficient_index = coefficient_counter++;1972 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);1973 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];1974 for (x = 0; x < output_w; ++x)1975 {1976 int in_pixel_index = x * channels;1977 int c;1978 for (c = 0; c < channels; c++)1979 encode_buffer[in_pixel_index + c] += ring_buffer_entry[in_pixel_index + c] * coefficient;1980 }1981 }1982 break;1983 }1984 stbir__encode_scanline(stbir_info, output_w, (char *) output_data + output_row_start, encode_buffer, channels, alpha_channel, decode);1985}19861987static void stbir__resample_vertical_downsample(stbir__info* stbir_info, int n)1988{1989 int x, k;1990 int output_w = stbir_info->output_w;1991 stbir__contributors* vertical_contributors = stbir_info->vertical_contributors;1992 float* vertical_coefficients = stbir_info->vertical_coefficients;1993 int channels = stbir_info->channels;1994 int ring_buffer_entries = stbir_info->ring_buffer_num_entries;1995 float* horizontal_buffer = stbir_info->horizontal_buffer;1996 int coefficient_width = stbir_info->vertical_coefficient_width;1997 int contributor = n + stbir_info->vertical_filter_pixel_margin;19981999 float* ring_buffer = stbir_info->ring_buffer;2000 int ring_buffer_begin_index = stbir_info->ring_buffer_begin_index;2001 int ring_buffer_first_scanline = stbir_info->ring_buffer_first_scanline;2002 int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float);2003 int n0,n1;20042005 n0 = vertical_contributors[contributor].n0;2006 n1 = vertical_contributors[contributor].n1;20072008 STBIR_ASSERT(!stbir__use_height_upsampling(stbir_info));20092010 for (k = n0; k <= n1; k++)2011 {2012 int coefficient_index = k - n0;2013 int coefficient_group = coefficient_width * contributor;2014 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];20152016 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);20172018 switch (channels) {2019 case 1:2020 for (x = 0; x < output_w; x++)2021 {2022 int in_pixel_index = x * 1;2023 ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;2024 }2025 break;2026 case 2:2027 for (x = 0; x < output_w; x++)2028 {2029 int in_pixel_index = x * 2;2030 ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;2031 ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient;2032 }2033 break;2034 case 3:2035 for (x = 0; x < output_w; x++)2036 {2037 int in_pixel_index = x * 3;2038 ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;2039 ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient;2040 ring_buffer_entry[in_pixel_index + 2] += horizontal_buffer[in_pixel_index + 2] * coefficient;2041 }2042 break;2043 case 4:2044 for (x = 0; x < output_w; x++)2045 {2046 int in_pixel_index = x * 4;2047 ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;2048 ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient;2049 ring_buffer_entry[in_pixel_index + 2] += horizontal_buffer[in_pixel_index + 2] * coefficient;2050 ring_buffer_entry[in_pixel_index + 3] += horizontal_buffer[in_pixel_index + 3] * coefficient;2051 }2052 break;2053 default:2054 for (x = 0; x < output_w; x++)2055 {2056 int in_pixel_index = x * channels;20572058 int c;2059 for (c = 0; c < channels; c++)2060 ring_buffer_entry[in_pixel_index + c] += horizontal_buffer[in_pixel_index + c] * coefficient;2061 }2062 break;2063 }2064 }2065}20662067static void stbir__buffer_loop_upsample(stbir__info* stbir_info)2068{2069 int y;2070 float scale_ratio = stbir_info->vertical_scale;2071 float out_scanlines_radius = stbir__filter_info_table[stbir_info->vertical_filter].support(1/scale_ratio) * scale_ratio;20722073 STBIR_ASSERT(stbir__use_height_upsampling(stbir_info));20742075 for (y = 0; y < stbir_info->output_h; y++)2076 {2077 float in_center_of_out = 0; // Center of the current out scanline in the in scanline space2078 int in_first_scanline = 0, in_last_scanline = 0;20792080 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);20812082 STBIR_ASSERT(in_last_scanline - in_first_scanline + 1 <= stbir_info->ring_buffer_num_entries);20832084 if (stbir_info->ring_buffer_begin_index >= 0)2085 {2086 // Get rid of whatever we don't need anymore.2087 while (in_first_scanline > stbir_info->ring_buffer_first_scanline)2088 {2089 if (stbir_info->ring_buffer_first_scanline == stbir_info->ring_buffer_last_scanline)2090 {2091 // We just popped the last scanline off the ring buffer.2092 // Reset it to the empty state.2093 stbir_info->ring_buffer_begin_index = -1;2094 stbir_info->ring_buffer_first_scanline = 0;2095 stbir_info->ring_buffer_last_scanline = 0;2096 break;2097 }2098 else2099 {2100 stbir_info->ring_buffer_first_scanline++;2101 stbir_info->ring_buffer_begin_index = (stbir_info->ring_buffer_begin_index + 1) % stbir_info->ring_buffer_num_entries;2102 }2103 }2104 }21052106 // Load in new ones.2107 if (stbir_info->ring_buffer_begin_index < 0)2108 stbir__decode_and_resample_upsample(stbir_info, in_first_scanline);21092110 while (in_last_scanline > stbir_info->ring_buffer_last_scanline)2111 stbir__decode_and_resample_upsample(stbir_info, stbir_info->ring_buffer_last_scanline + 1);21122113 // Now all buffers should be ready to write a row of vertical sampling.2114 stbir__resample_vertical_upsample(stbir_info, y);21152116 STBIR_PROGRESS_REPORT((float)y / stbir_info->output_h);2117 }2118}21192120static void stbir__empty_ring_buffer(stbir__info* stbir_info, int first_necessary_scanline)2121{2122 int output_stride_bytes = stbir_info->output_stride_bytes;2123 int channels = stbir_info->channels;2124 int alpha_channel = stbir_info->alpha_channel;2125 int type = stbir_info->type;2126 int colorspace = stbir_info->colorspace;2127 int output_w = stbir_info->output_w;2128 void* output_data = stbir_info->output_data;2129 int decode = STBIR__DECODE(type, colorspace);21302131 float* ring_buffer = stbir_info->ring_buffer;2132 int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float);21332134 if (stbir_info->ring_buffer_begin_index >= 0)2135 {2136 // Get rid of whatever we don't need anymore.2137 while (first_necessary_scanline > stbir_info->ring_buffer_first_scanline)2138 {2139 if (stbir_info->ring_buffer_first_scanline >= 0 && stbir_info->ring_buffer_first_scanline < stbir_info->output_h)2140 {2141 int output_row_start = stbir_info->ring_buffer_first_scanline * output_stride_bytes;2142 float* ring_buffer_entry = stbir__get_ring_buffer_entry(ring_buffer, stbir_info->ring_buffer_begin_index, ring_buffer_length);2143 stbir__encode_scanline(stbir_info, output_w, (char *) output_data + output_row_start, ring_buffer_entry, channels, alpha_channel, decode);2144 STBIR_PROGRESS_REPORT((float)stbir_info->ring_buffer_first_scanline / stbir_info->output_h);2145 }21462147 if (stbir_info->ring_buffer_first_scanline == stbir_info->ring_buffer_last_scanline)2148 {2149 // We just popped the last scanline off the ring buffer.2150 // Reset it to the empty state.2151 stbir_info->ring_buffer_begin_index = -1;2152 stbir_info->ring_buffer_first_scanline = 0;2153 stbir_info->ring_buffer_last_scanline = 0;2154 break;2155 }2156 else2157 {2158 stbir_info->ring_buffer_first_scanline++;2159 stbir_info->ring_buffer_begin_index = (stbir_info->ring_buffer_begin_index + 1) % stbir_info->ring_buffer_num_entries;2160 }2161 }2162 }2163}21642165static void stbir__buffer_loop_downsample(stbir__info* stbir_info)2166{2167 int y;2168 float scale_ratio = stbir_info->vertical_scale;2169 int output_h = stbir_info->output_h;2170 float in_pixels_radius = stbir__filter_info_table[stbir_info->vertical_filter].support(scale_ratio) / scale_ratio;2171 int pixel_margin = stbir_info->vertical_filter_pixel_margin;2172 int max_y = stbir_info->input_h + pixel_margin;21732174 STBIR_ASSERT(!stbir__use_height_upsampling(stbir_info));21752176 for (y = -pixel_margin; y < max_y; y++)2177 {2178 float out_center_of_in; // Center of the current out scanline in the in scanline space2179 int out_first_scanline, out_last_scanline;21802181 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);21822183 STBIR_ASSERT(out_last_scanline - out_first_scanline + 1 <= stbir_info->ring_buffer_num_entries);21842185 if (out_last_scanline < 0 || out_first_scanline >= output_h)2186 continue;21872188 stbir__empty_ring_buffer(stbir_info, out_first_scanline);21892190 stbir__decode_and_resample_downsample(stbir_info, y);21912192 // Load in new ones.2193 if (stbir_info->ring_buffer_begin_index < 0)2194 stbir__add_empty_ring_buffer_entry(stbir_info, out_first_scanline);21952196 while (out_last_scanline > stbir_info->ring_buffer_last_scanline)2197 stbir__add_empty_ring_buffer_entry(stbir_info, stbir_info->ring_buffer_last_scanline + 1);21982199 // Now the horizontal buffer is ready to write to all ring buffer rows.2200 stbir__resample_vertical_downsample(stbir_info, y);2201 }22022203 stbir__empty_ring_buffer(stbir_info, stbir_info->output_h);2204}22052206static void stbir__setup(stbir__info *info, int input_w, int input_h, int output_w, int output_h, int channels)2207{2208 info->input_w = input_w;2209 info->input_h = input_h;2210 info->output_w = output_w;2211 info->output_h = output_h;2212 info->channels = channels;2213}22142215static void stbir__calculate_transform(stbir__info *info, float s0, float t0, float s1, float t1, float *transform)2216{2217 info->s0 = s0;2218 info->t0 = t0;2219 info->s1 = s1;2220 info->t1 = t1;22212222 if (transform)2223 {2224 info->horizontal_scale = transform[0];2225 info->vertical_scale = transform[1];2226 info->horizontal_shift = transform[2];2227 info->vertical_shift = transform[3];2228 }2229 else2230 {2231 info->horizontal_scale = ((float)info->output_w / info->input_w) / (s1 - s0);2232 info->vertical_scale = ((float)info->output_h / info->input_h) / (t1 - t0);22332234 info->horizontal_shift = s0 * info->output_w / (s1 - s0);2235 info->vertical_shift = t0 * info->output_h / (t1 - t0);2236 }2237}22382239static void stbir__choose_filter(stbir__info *info, stbir_filter h_filter, stbir_filter v_filter)2240{2241 if (h_filter == 0)2242 h_filter = stbir__use_upsampling(info->horizontal_scale) ? STBIR_DEFAULT_FILTER_UPSAMPLE : STBIR_DEFAULT_FILTER_DOWNSAMPLE;2243 if (v_filter == 0)2244 v_filter = stbir__use_upsampling(info->vertical_scale) ? STBIR_DEFAULT_FILTER_UPSAMPLE : STBIR_DEFAULT_FILTER_DOWNSAMPLE;2245 info->horizontal_filter = h_filter;2246 info->vertical_filter = v_filter;2247}22482249static stbir_uint32 stbir__calculate_memory(stbir__info *info)2250{2251 int pixel_margin = stbir__get_filter_pixel_margin(info->horizontal_filter, info->horizontal_scale);2252 int filter_height = stbir__get_filter_pixel_width(info->vertical_filter, info->vertical_scale);22532254 info->horizontal_num_contributors = stbir__get_contributors(info->horizontal_scale, info->horizontal_filter, info->input_w, info->output_w);2255 info->vertical_num_contributors = stbir__get_contributors(info->vertical_scale , info->vertical_filter , info->input_h, info->output_h);22562257 // One extra entry because floating point precision problems sometimes cause an extra to be necessary.2258 info->ring_buffer_num_entries = filter_height + 1;22592260 info->horizontal_contributors_size = info->horizontal_num_contributors * sizeof(stbir__contributors);2261 info->horizontal_coefficients_size = stbir__get_total_horizontal_coefficients(info) * sizeof(float);2262 info->vertical_contributors_size = info->vertical_num_contributors * sizeof(stbir__contributors);2263 info->vertical_coefficients_size = stbir__get_total_vertical_coefficients(info) * sizeof(float);2264 info->decode_buffer_size = (info->input_w + pixel_margin * 2) * info->channels * sizeof(float);2265 info->horizontal_buffer_size = info->output_w * info->channels * sizeof(float);2266 info->ring_buffer_size = info->output_w * info->channels * info->ring_buffer_num_entries * sizeof(float);2267 info->encode_buffer_size = info->output_w * info->channels * sizeof(float);22682269 STBIR_ASSERT(info->horizontal_filter != 0);2270 STBIR_ASSERT(info->horizontal_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late2271 STBIR_ASSERT(info->vertical_filter != 0);2272 STBIR_ASSERT(info->vertical_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late22732274 if (stbir__use_height_upsampling(info))2275 // The horizontal buffer is for when we're downsampling the height and we2276 // can't output the result of sampling the decode buffer directly into the2277 // ring buffers.2278 info->horizontal_buffer_size = 0;2279 else2280 // The encode buffer is to retain precision in the height upsampling method2281 // and isn't used when height downsampling.2282 info->encode_buffer_size = 0;22832284 return info->horizontal_contributors_size + info->horizontal_coefficients_size2285 + info->vertical_contributors_size + info->vertical_coefficients_size2286 + info->decode_buffer_size + info->horizontal_buffer_size2287 + info->ring_buffer_size + info->encode_buffer_size;2288}22892290static int stbir__resize_allocated(stbir__info *info,2291 const void* input_data, int input_stride_in_bytes,2292 void* output_data, int output_stride_in_bytes,2293 int alpha_channel, stbir_uint32 flags, stbir_datatype type,2294 stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace,2295 void* tempmem, size_t tempmem_size_in_bytes)2296{2297 size_t memory_required = stbir__calculate_memory(info);22982299 int width_stride_input = input_stride_in_bytes ? input_stride_in_bytes : info->channels * info->input_w * stbir__type_size[type];2300 int width_stride_output = output_stride_in_bytes ? output_stride_in_bytes : info->channels * info->output_w * stbir__type_size[type];23012302#ifdef STBIR_DEBUG_OVERWRITE_TEST2303#define OVERWRITE_ARRAY_SIZE 82304 unsigned char overwrite_output_before_pre[OVERWRITE_ARRAY_SIZE];2305 unsigned char overwrite_tempmem_before_pre[OVERWRITE_ARRAY_SIZE];2306 unsigned char overwrite_output_after_pre[OVERWRITE_ARRAY_SIZE];2307 unsigned char overwrite_tempmem_after_pre[OVERWRITE_ARRAY_SIZE];23082309 size_t begin_forbidden = width_stride_output * (info->output_h - 1) + info->output_w * info->channels * stbir__type_size[type];2310 memcpy(overwrite_output_before_pre, &((unsigned char*)output_data)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE);2311 memcpy(overwrite_output_after_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE);2312 memcpy(overwrite_tempmem_before_pre, &((unsigned char*)tempmem)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE);2313 memcpy(overwrite_tempmem_after_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE);2314#endif23152316 STBIR_ASSERT(info->channels >= 0);2317 STBIR_ASSERT(info->channels <= STBIR_MAX_CHANNELS);23182319 if (info->channels < 0 || info->channels > STBIR_MAX_CHANNELS)2320 return 0;23212322 STBIR_ASSERT(info->horizontal_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));2323 STBIR_ASSERT(info->vertical_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));23242325 if (info->horizontal_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table))2326 return 0;2327 if (info->vertical_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table))2328 return 0;23292330 if (alpha_channel < 0)2331 flags |= STBIR_FLAG_ALPHA_USES_COLORSPACE | STBIR_FLAG_ALPHA_PREMULTIPLIED;23322333 if (!(flags&STBIR_FLAG_ALPHA_USES_COLORSPACE) || !(flags&STBIR_FLAG_ALPHA_PREMULTIPLIED)) {2334 STBIR_ASSERT(alpha_channel >= 0 && alpha_channel < info->channels);2335 }23362337 if (alpha_channel >= info->channels)2338 return 0;23392340 STBIR_ASSERT(tempmem);23412342 if (!tempmem)2343 return 0;23442345 STBIR_ASSERT(tempmem_size_in_bytes >= memory_required);23462347 if (tempmem_size_in_bytes < memory_required)2348 return 0;23492350 memset(tempmem, 0, tempmem_size_in_bytes);23512352 info->input_data = input_data;2353 info->input_stride_bytes = width_stride_input;23542355 info->output_data = output_data;2356 info->output_stride_bytes = width_stride_output;23572358 info->alpha_channel = alpha_channel;2359 info->flags = flags;2360 info->type = type;2361 info->edge_horizontal = edge_horizontal;2362 info->edge_vertical = edge_vertical;2363 info->colorspace = colorspace;23642365 info->horizontal_coefficient_width = stbir__get_coefficient_width (info->horizontal_filter, info->horizontal_scale);2366 info->vertical_coefficient_width = stbir__get_coefficient_width (info->vertical_filter , info->vertical_scale );2367 info->horizontal_filter_pixel_width = stbir__get_filter_pixel_width (info->horizontal_filter, info->horizontal_scale);2368 info->vertical_filter_pixel_width = stbir__get_filter_pixel_width (info->vertical_filter , info->vertical_scale );2369 info->horizontal_filter_pixel_margin = stbir__get_filter_pixel_margin(info->horizontal_filter, info->horizontal_scale);2370 info->vertical_filter_pixel_margin = stbir__get_filter_pixel_margin(info->vertical_filter , info->vertical_scale );23712372 info->ring_buffer_length_bytes = info->output_w * info->channels * sizeof(float);2373 info->decode_buffer_pixels = info->input_w + info->horizontal_filter_pixel_margin * 2;23742375#define STBIR__NEXT_MEMPTR(current, newtype) (newtype*)(((unsigned char*)current) + current##_size)23762377 info->horizontal_contributors = (stbir__contributors *) tempmem;2378 info->horizontal_coefficients = STBIR__NEXT_MEMPTR(info->horizontal_contributors, float);2379 info->vertical_contributors = STBIR__NEXT_MEMPTR(info->horizontal_coefficients, stbir__contributors);2380 info->vertical_coefficients = STBIR__NEXT_MEMPTR(info->vertical_contributors, float);2381 info->decode_buffer = STBIR__NEXT_MEMPTR(info->vertical_coefficients, float);23822383 if (stbir__use_height_upsampling(info))2384 {2385 info->horizontal_buffer = NULL;2386 info->ring_buffer = STBIR__NEXT_MEMPTR(info->decode_buffer, float);2387 info->encode_buffer = STBIR__NEXT_MEMPTR(info->ring_buffer, float);23882389 STBIR_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->encode_buffer, unsigned char) == (size_t)tempmem + tempmem_size_in_bytes);2390 }2391 else2392 {2393 info->horizontal_buffer = STBIR__NEXT_MEMPTR(info->decode_buffer, float);2394 info->ring_buffer = STBIR__NEXT_MEMPTR(info->horizontal_buffer, float);2395 info->encode_buffer = NULL;23962397 STBIR_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->ring_buffer, unsigned char) == (size_t)tempmem + tempmem_size_in_bytes);2398 }23992400#undef STBIR__NEXT_MEMPTR24012402 // This signals that the ring buffer is empty2403 info->ring_buffer_begin_index = -1;24042405 stbir__calculate_filters(info->horizontal_contributors, info->horizontal_coefficients, info->horizontal_filter, info->horizontal_scale, info->horizontal_shift, info->input_w, info->output_w);2406 stbir__calculate_filters(info->vertical_contributors, info->vertical_coefficients, info->vertical_filter, info->vertical_scale, info->vertical_shift, info->input_h, info->output_h);24072408 STBIR_PROGRESS_REPORT(0);24092410 if (stbir__use_height_upsampling(info))2411 stbir__buffer_loop_upsample(info);2412 else2413 stbir__buffer_loop_downsample(info);24142415 STBIR_PROGRESS_REPORT(1);24162417#ifdef STBIR_DEBUG_OVERWRITE_TEST2418 STBIR_ASSERT(memcmp(overwrite_output_before_pre, &((unsigned char*)output_data)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE) == 0);2419 STBIR_ASSERT(memcmp(overwrite_output_after_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE) == 0);2420 STBIR_ASSERT(memcmp(overwrite_tempmem_before_pre, &((unsigned char*)tempmem)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE) == 0);2421 STBIR_ASSERT(memcmp(overwrite_tempmem_after_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE) == 0);2422#endif24232424 return 1;2425}242624272428static int stbir__resize_arbitrary(2429 void *alloc_context,2430 const void* input_data, int input_w, int input_h, int input_stride_in_bytes,2431 void* output_data, int output_w, int output_h, int output_stride_in_bytes,2432 float s0, float t0, float s1, float t1, float *transform,2433 int channels, int alpha_channel, stbir_uint32 flags, stbir_datatype type,2434 stbir_filter h_filter, stbir_filter v_filter,2435 stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace)2436{2437 stbir__info info;2438 int result;2439 size_t memory_required;2440 void* extra_memory;24412442 stbir__setup(&info, input_w, input_h, output_w, output_h, channels);2443 stbir__calculate_transform(&info, s0,t0,s1,t1,transform);2444 stbir__choose_filter(&info, h_filter, v_filter);2445 memory_required = stbir__calculate_memory(&info);2446 extra_memory = STBIR_MALLOC(memory_required, alloc_context);24472448 if (!extra_memory)2449 return 0;24502451 result = stbir__resize_allocated(&info, input_data, input_stride_in_bytes,2452 output_data, output_stride_in_bytes,2453 alpha_channel, flags, type,2454 edge_horizontal, edge_vertical,2455 colorspace, extra_memory, memory_required);24562457 STBIR_FREE(extra_memory, alloc_context);24582459 return result;2460}24612462STBIRDEF int stbir_resize_uint8( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2463 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2464 int num_channels)2465{2466 return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,2467 output_pixels, output_w, output_h, output_stride_in_bytes,2468 0,0,1,1,NULL,num_channels,-1,0, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,2469 STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR);2470}24712472STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2473 float *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2474 int num_channels)2475{2476 return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,2477 output_pixels, output_w, output_h, output_stride_in_bytes,2478 0,0,1,1,NULL,num_channels,-1,0, STBIR_TYPE_FLOAT, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,2479 STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR);2480}24812482STBIRDEF int stbir_resize_uint8_srgb(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2483 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2484 int num_channels, int alpha_channel, int flags)2485{2486 return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,2487 output_pixels, output_w, output_h, output_stride_in_bytes,2488 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,2489 STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);2490}24912492STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2493 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2494 int num_channels, int alpha_channel, int flags,2495 stbir_edge edge_wrap_mode)2496{2497 return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,2498 output_pixels, output_w, output_h, output_stride_in_bytes,2499 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,2500 edge_wrap_mode, edge_wrap_mode, STBIR_COLORSPACE_SRGB);2501}25022503STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2504 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2505 int num_channels, int alpha_channel, int flags,2506 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,2507 void *alloc_context)2508{2509 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2510 output_pixels, output_w, output_h, output_stride_in_bytes,2511 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, filter, filter,2512 edge_wrap_mode, edge_wrap_mode, space);2513}25142515STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2516 stbir_uint16 *output_pixels , int output_w, int output_h, int output_stride_in_bytes,2517 int num_channels, int alpha_channel, int flags,2518 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,2519 void *alloc_context)2520{2521 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2522 output_pixels, output_w, output_h, output_stride_in_bytes,2523 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT16, filter, filter,2524 edge_wrap_mode, edge_wrap_mode, space);2525}252625272528STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2529 float *output_pixels , int output_w, int output_h, int output_stride_in_bytes,2530 int num_channels, int alpha_channel, int flags,2531 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,2532 void *alloc_context)2533{2534 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2535 output_pixels, output_w, output_h, output_stride_in_bytes,2536 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_FLOAT, filter, filter,2537 edge_wrap_mode, edge_wrap_mode, space);2538}253925402541STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2542 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2543 stbir_datatype datatype,2544 int num_channels, int alpha_channel, int flags,2545 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,2546 stbir_filter filter_horizontal, stbir_filter filter_vertical,2547 stbir_colorspace space, void *alloc_context)2548{2549 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2550 output_pixels, output_w, output_h, output_stride_in_bytes,2551 0,0,1,1,NULL,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical,2552 edge_mode_horizontal, edge_mode_vertical, space);2553}255425552556STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2557 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2558 stbir_datatype datatype,2559 int num_channels, int alpha_channel, int flags,2560 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,2561 stbir_filter filter_horizontal, stbir_filter filter_vertical,2562 stbir_colorspace space, void *alloc_context,2563 float x_scale, float y_scale,2564 float x_offset, float y_offset)2565{2566 float transform[4];2567 transform[0] = x_scale;2568 transform[1] = y_scale;2569 transform[2] = x_offset;2570 transform[3] = y_offset;2571 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2572 output_pixels, output_w, output_h, output_stride_in_bytes,2573 0,0,1,1,transform,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical,2574 edge_mode_horizontal, edge_mode_vertical, space);2575}25762577STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2578 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2579 stbir_datatype datatype,2580 int num_channels, int alpha_channel, int flags,2581 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,2582 stbir_filter filter_horizontal, stbir_filter filter_vertical,2583 stbir_colorspace space, void *alloc_context,2584 float s0, float t0, float s1, float t1)2585{2586 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2587 output_pixels, output_w, output_h, output_stride_in_bytes,2588 s0,t0,s1,t1,NULL,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical,2589 edge_mode_horizontal, edge_mode_vertical, space);2590}25912592#endif // STB_IMAGE_RESIZE_IMPLEMENTATION25932594/*2595------------------------------------------------------------------------------2596This software is available under 2 licenses -- choose whichever you prefer.2597------------------------------------------------------------------------------2598ALTERNATIVE A - MIT License2599Copyright (c) 2017 Sean Barrett2600Permission is hereby granted, free of charge, to any person obtaining a copy of2601this software and associated documentation files (the "Software"), to deal in2602the Software without restriction, including without limitation the rights to2603use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies2604of the Software, and to permit persons to whom the Software is furnished to do2605so, subject to the following conditions:2606The above copyright notice and this permission notice shall be included in all2607copies or substantial portions of the Software.2608THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR2609IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,2610FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE2611AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER2612LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,2613OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE2614SOFTWARE.2615------------------------------------------------------------------------------2616ALTERNATIVE B - Public Domain (www.unlicense.org)2617This is free and unencumbered software released into the public domain.2618Anyone is free to copy, modify, publish, use, compile, sell, or distribute this2619software, either in source code form or as a compiled binary, for any purpose,2620commercial or non-commercial, and by any means.2621In jurisdictions that recognize copyright laws, the author or authors of this2622software dedicate any and all copyright interest in the software to the public2623domain. We make this dedication for the benefit of the public at large and to2624the detriment of our heirs and successors. We intend this dedication to be an2625overt act of relinquishment in perpetuity of all present and future rights to2626this software under copyright law.2627THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR2628IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,2629FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE2630AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN2631ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION2632WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.2633------------------------------------------------------------------------------2634*/
added:stb_image_resize_test/dotimings.c+224 added
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>45#ifdef _MSC_VER67#define stop() __debugbreak()8#include <windows.h>9#define int64 __int6410#pragma warning(disable:4127)1112#define get_milliseconds GetTickCount1314#else1516#define stop() __builtin_trap()17#define int64 long long1819typedef unsigned int U32;20typedef unsigned long long U64;2122#include <time.h>23static int get_milliseconds()24{25 struct timespec ts;26 clock_gettime( CLOCK_MONOTONIC, &ts );27 return (U32) ( ( ((U64)(U32)ts.tv_sec) * 1000LL ) + (U64)(((U32)ts.tv_nsec+500000)/1000000) );28}2930#endif3132#if defined(TIME_SIMD)33 // default for most platforms34#elif defined(TIME_SCALAR)35 #define STBIR_NO_SIMD36#else37 #error You must define TIME_SIMD or TIME_SCALAR when compiling this file.38#endif3940#define STBIR_PROFILE41#define STB_IMAGE_RESIZE_IMPLEMENTATION42#define STBIR__V_FIRST_INFO_BUFFER v_info43#include "stb_image_resize2.h" // new one!4445#if defined(TIME_SIMD) && !defined(STBIR_SIMD)46#error Timing SIMD, but scalar was ON!47#endif4849#if defined(TIME_SCALAR) && defined(STBIR_SIMD)50#error Timing scalar, but SIMD was ON!51#endif5253#define HEADER 32545556static int file_write( const char *filename, void * buffer, size_t size ) 57{58 FILE * f = fopen( filename, "wb" );59 if ( f == 0 ) return 0;60 if ( fwrite( buffer, 1, size, f) != size ) return 0;61 fclose(f);62 return 1;63}6465int64 nresize( void * o, int ox, int oy, int op, void * i, int ix, int iy, int ip, int buf, int type, int edg, int flt )66{67 STBIR_RESIZE resize;68 int t;69 int64 b;7071 stbir_resize_init( &resize, i, ix, iy, ip, o, ox, oy, op, buf, type );72 stbir_set_edgemodes( &resize, edg, edg );73 stbir_set_filters( &resize, flt, flt );74 75 stbir_build_samplers_with_splits( &resize, 1 );7677 b = 0x7fffffffffffffffULL;78 for( t = 0 ; t < 16 ; t++ )79 {80 STBIR_PROFILE_INFO profile;81 int64 v;82 if(!stbir_resize_extended( &resize ) )83 stop();84 stbir_resize_extended_profile_info( &profile, &resize );85 v = profile.clocks[1]+profile.clocks[2];86 if ( v < b )87 {88 b = v;89 t = 0;90 }91 }9293 stbir_free_samplers( &resize );9495 return b;96}979899#define INSIZES 5100#define TYPESCOUNT 5101#define NUM 64102103static const int sizes[INSIZES]={63,126,252,520,772};104static const int types[TYPESCOUNT]={STBIR_1CHANNEL,STBIR_2CHANNEL,STBIR_RGB,STBIR_4CHANNEL,STBIR_RGBA};105static const int effective[TYPESCOUNT]={1,2,3,4,7};106107int main( int argc, char ** argv )108{109 unsigned char * input;110 unsigned char * output;111 int dimensionx, dimensiony;112 int scalex, scaley;113 int totalms;114 int timing_count;115 int ir;116 int * file;117 int * ts;118 int64 totalcycles;119120 if ( argc != 6 )121 {122 printf("command: dotimings x_samps y_samps x_scale y_scale outfilename\n");123 exit(1);124 }125126 input = malloc( 4*1200*1200 );127 memset( input, 0x80, 4*1200*1200 );128 output = malloc( 4*10000*10000ULL );129 130 dimensionx = atoi( argv[1] );131 dimensiony = atoi( argv[2] );132 scalex = atoi( argv[3] );133 scaley = atoi( argv[4] );134135 timing_count = dimensionx * dimensiony * INSIZES * TYPESCOUNT;136137 file = malloc( sizeof(int) * ( 2 * timing_count + HEADER ) );138 ts = file + HEADER;139140 totalms = get_milliseconds(); 141 totalcycles = STBIR_PROFILE_FUNC();142 for( ir = 0 ; ir < INSIZES ; ir++ )143 {144 int ix, iy, ty;145 ix = iy = sizes[ir];146147 for( ty = 0 ; ty < TYPESCOUNT ; ty++ )148 {149 int h, hh;150151 h = 1;152 for( hh = 0 ; hh < dimensiony; hh++ )153 {154 int ww, w = 1;155 for( ww = 0 ; ww < dimensionx; ww++ )156 {157 int64 VF, HF;158 int good;159 160 v_info.control_v_first = 2; // vertical first161 VF = nresize( output, w, h, (w*4*1)&~3, input, ix, iy, ix*4*1, types[ty], STBIR_TYPE_UINT8, STBIR_EDGE_CLAMP, STBIR_FILTER_MITCHELL );162 v_info.control_v_first = 1; // horizonal first163 HF = nresize( output, w, h, (w*4*1)&~3, input, ix, iy, ix*4*1, types[ty], STBIR_TYPE_UINT8, STBIR_EDGE_CLAMP, STBIR_FILTER_MITCHELL );164165 good = ( ((HF<=VF) && (!v_info.v_first)) || ((VF<=HF) && (v_info.v_first)));166167// printf("\r%d,%d, %d,%d, %d, %I64d,%I64d, // Good: %c(%c-%d) CompEst: %.1f %.1f\n", ix, iy, w, h, ty, VF, HF, good?'y':'n', v_info.v_first?'v':'h', v_info.v_resize_classification, v_info.v_cost,v_info.h_cost );168 ts[0] = (int)VF;169 ts[1] = (int)HF;170171 ts += 2;172173 w += scalex;174 }175 printf(".");176 h += scaley; 177 }178 }179 }180 totalms = get_milliseconds() - totalms; 181 totalcycles = STBIR_PROFILE_FUNC() - totalcycles;182183 printf("\n");184185 file[0] = 'VFT1';186187 #if defined(_x86_64) || defined( __x86_64__ ) || defined( _M_X64 ) || defined(__x86_64) || defined(__SSE2__) || defined( _M_IX86_FP ) || defined(__i386) || defined( __i386__ ) || defined( _M_IX86 ) || defined( _X86_ )188 file[1] = 1; // x64189 #elif defined( _M_AMD64 ) || defined( __aarch64__ ) || defined( __arm64__ ) || defined(__ARM_NEON__) || defined(__ARM_NEON) || defined(__arm__) || defined( _M_ARM )190 file[1] = 2; // arm191 #else192 file[1] = 99; // who knows???193 #endif194 195 #ifdef STBIR_SIMD8196 file[2] = 2; // simd-8197 #elif defined( STBIR_SIMD )198 file[2] = 1; // simd-4199 #else200 file[2] = 0; // nosimd201 #endif202 203 file[3] = dimensionx; // dimx204 file[4] = dimensiony; // dimy205 file[5] = TYPESCOUNT; // channel types206 file[ 6] = types[0]; file[7] = types[1]; file[8] = types[2]; file[9] = types[3]; file[10] = types[4]; // buffer_type 207 file[11] = effective[0]; file[12] = effective[1]; file[13] = effective[2]; file[14] = effective[3]; file[15] = effective[4]; // effective channels 208 file[16] = INSIZES; // resizes209 file[17] = sizes[0]; file[18] = sizes[0]; // input sizes (w x h)210 file[19] = sizes[1]; file[20] = sizes[1];211 file[21] = sizes[2]; file[22] = sizes[2];212 file[23] = sizes[3]; file[24] = sizes[3];213 file[25] = sizes[4]; file[26] = sizes[4];214 file[27] = scalex; file[28] = scaley; // scale the dimx and dimy amount ( for(i=0;i<dimx) outputx = 1 + i*scalex; )215 file[29] = totalms;216 ((int64*)(file+30))[0] = totalcycles;217218 if ( !file_write( argv[5], file, sizeof(int) * ( 2 * timing_count + HEADER ) ) )219 printf( "Error writing file: %s\n", argv[5] );220 else221 printf( "Successfully wrote timing file: %s\n", argv[5] );222223 return 0;224}
added:stb_image_resize_test/old_image_resize.h+2738 added
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 = &current_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];2001 for (x = 0; x < output_w; ++x)2002 {2003 int in_pixel_index = x * 1;2004 encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;2005 }2006 }2007 break;2008 case 2:2009 for (k = n0; k <= n1; k++)2010 {2011 int coefficient_index = coefficient_counter++;2012 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);2013 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];2014 for (x = 0; x < output_w; ++x)2015 {2016 int in_pixel_index = x * 2;2017 encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;2018 encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient;2019 }2020 }2021 break;2022 case 3:2023 for (k = n0; k <= n1; k++)2024 {2025 int coefficient_index = coefficient_counter++;2026 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);2027 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];2028 for (x = 0; x < output_w; ++x)2029 {2030 int in_pixel_index = x * 3;2031 encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;2032 encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient;2033 encode_buffer[in_pixel_index + 2] += ring_buffer_entry[in_pixel_index + 2] * coefficient;2034 }2035 }2036 break;2037 case 4:2038 for (k = n0; k <= n1; k++)2039 {2040 int coefficient_index = coefficient_counter++;2041 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);2042 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];2043 for (x = 0; x < output_w; ++x)2044 {2045 int in_pixel_index = x * 4;2046 encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;2047 encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient;2048 encode_buffer[in_pixel_index + 2] += ring_buffer_entry[in_pixel_index + 2] * coefficient;2049 encode_buffer[in_pixel_index + 3] += ring_buffer_entry[in_pixel_index + 3] * coefficient;2050 }2051 }2052 break;2053 default:2054 for (k = n0; k <= n1; k++)2055 {2056 int coefficient_index = coefficient_counter++;2057 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);2058 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];2059 for (x = 0; x < output_w; ++x)2060 {2061 int in_pixel_index = x * channels;2062 int c;2063 for (c = 0; c < channels; c++)2064 encode_buffer[in_pixel_index + c] += ring_buffer_entry[in_pixel_index + c] * coefficient;2065 }2066 }2067 break;2068 }2069 STBIR_PROFILE_END( vertical );2070 stbir__encode_scanline(stbir_info, output_w, (char *) output_data + output_row_start, encode_buffer, channels, alpha_channel, decode);2071}20722073static void stbir__resample_vertical_downsample(ostbir__info* stbir_info, int n)2074{2075 int x, k;2076 int output_w = stbir_info->output_w;2077 stbir__contributors* vertical_contributors = stbir_info->vertical_contributors;2078 float* vertical_coefficients = stbir_info->vertical_coefficients;2079 int channels = stbir_info->channels;2080 int ring_buffer_entries = stbir_info->ring_buffer_num_entries;2081 float* horizontal_buffer = stbir_info->horizontal_buffer;2082 int coefficient_width = stbir_info->vertical_coefficient_width;2083 int contributor = n + stbir_info->vertical_filter_pixel_margin;20842085 float* ring_buffer = stbir_info->ring_buffer;2086 int ring_buffer_begin_index = stbir_info->ring_buffer_begin_index;2087 int ring_buffer_first_scanline = stbir_info->ring_buffer_first_scanline;2088 int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float);2089 int n0,n1;20902091 n0 = vertical_contributors[contributor].n0;2092 n1 = vertical_contributors[contributor].n1;20932094 STBIR_ASSERT(!stbir__use_height_upsampling(stbir_info));20952096 STBIR_PROFILE_START( );2097 for (k = n0; k <= n1; k++)2098 {2099 int coefficient_index = k - n0;2100 int coefficient_group = coefficient_width * contributor;2101 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];21022103 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);21042105 switch (channels) {2106 case 1:2107 for (x = 0; x < output_w; x++)2108 {2109 int in_pixel_index = x * 1;2110 ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;2111 }2112 break;2113 case 2:2114 for (x = 0; x < output_w; x++)2115 {2116 int in_pixel_index = x * 2;2117 ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;2118 ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient;2119 }2120 break;2121 case 3:2122 for (x = 0; x < output_w; x++)2123 {2124 int in_pixel_index = x * 3;2125 ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;2126 ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient;2127 ring_buffer_entry[in_pixel_index + 2] += horizontal_buffer[in_pixel_index + 2] * coefficient;2128 }2129 break;2130 case 4:2131 for (x = 0; x < output_w; x++)2132 {2133 int in_pixel_index = x * 4;2134 ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;2135 ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient;2136 ring_buffer_entry[in_pixel_index + 2] += horizontal_buffer[in_pixel_index + 2] * coefficient;2137 ring_buffer_entry[in_pixel_index + 3] += horizontal_buffer[in_pixel_index + 3] * coefficient;2138 }2139 break;2140 default:2141 for (x = 0; x < output_w; x++)2142 {2143 int in_pixel_index = x * channels;21442145 int c;2146 for (c = 0; c < channels; c++)2147 ring_buffer_entry[in_pixel_index + c] += horizontal_buffer[in_pixel_index + c] * coefficient;2148 }2149 break;2150 }2151 }2152 STBIR_PROFILE_END( vertical );2153}21542155static void stbir__buffer_loop_upsample(ostbir__info* stbir_info)2156{2157 int y;2158 float scale_ratio = stbir_info->vertical_scale;2159 float out_scanlines_radius = stbir__filter_info_table[stbir_info->vertical_filter].support(1/scale_ratio) * scale_ratio;21602161 STBIR_ASSERT(stbir__use_height_upsampling(stbir_info));21622163 for (y = 0; y < stbir_info->output_h; y++)2164 {2165 float in_center_of_out = 0; // Center of the current out scanline in the in scanline space2166 int in_first_scanline = 0, in_last_scanline = 0;21672168 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);21692170 STBIR_ASSERT(in_last_scanline - in_first_scanline + 1 <= stbir_info->ring_buffer_num_entries);21712172 if (stbir_info->ring_buffer_begin_index >= 0)2173 {2174 // Get rid of whatever we don't need anymore.2175 while (in_first_scanline > stbir_info->ring_buffer_first_scanline)2176 {2177 if (stbir_info->ring_buffer_first_scanline == stbir_info->ring_buffer_last_scanline)2178 {2179 // We just popped the last scanline off the ring buffer.2180 // Reset it to the empty state.2181 stbir_info->ring_buffer_begin_index = -1;2182 stbir_info->ring_buffer_first_scanline = 0;2183 stbir_info->ring_buffer_last_scanline = 0;2184 break;2185 }2186 else2187 {2188 stbir_info->ring_buffer_first_scanline++;2189 stbir_info->ring_buffer_begin_index = (stbir_info->ring_buffer_begin_index + 1) % stbir_info->ring_buffer_num_entries;2190 }2191 }2192 }21932194 // Load in new ones.2195 if (stbir_info->ring_buffer_begin_index < 0)2196 stbir__decode_and_resample_upsample(stbir_info, in_first_scanline);21972198 while (in_last_scanline > stbir_info->ring_buffer_last_scanline)2199 stbir__decode_and_resample_upsample(stbir_info, stbir_info->ring_buffer_last_scanline + 1);22002201 // Now all buffers should be ready to write a row of vertical sampling.2202 stbir__resample_vertical_upsample(stbir_info, y);22032204 STBIR_PROGRESS_REPORT((float)y / stbir_info->output_h);2205 }2206}22072208static void stbir__empty_ring_buffer(ostbir__info* stbir_info, int first_necessary_scanline)2209{2210 int output_stride_bytes = stbir_info->output_stride_bytes;2211 int channels = stbir_info->channels;2212 int alpha_channel = stbir_info->alpha_channel;2213 int type = stbir_info->type;2214 int colorspace = stbir_info->colorspace;2215 int output_w = stbir_info->output_w;2216 void* output_data = stbir_info->output_data;2217 int decode = STBIR__DECODE(type, colorspace);22182219 float* ring_buffer = stbir_info->ring_buffer;2220 int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float);22212222 if (stbir_info->ring_buffer_begin_index >= 0)2223 {2224 // Get rid of whatever we don't need anymore.2225 while (first_necessary_scanline > stbir_info->ring_buffer_first_scanline)2226 {2227 if (stbir_info->ring_buffer_first_scanline >= 0 && stbir_info->ring_buffer_first_scanline < stbir_info->output_h)2228 {2229 int output_row_start = stbir_info->ring_buffer_first_scanline * output_stride_bytes;2230 float* ring_buffer_entry = stbir__get_ring_buffer_entry(ring_buffer, stbir_info->ring_buffer_begin_index, ring_buffer_length);2231 stbir__encode_scanline(stbir_info, output_w, (char *) output_data + output_row_start, ring_buffer_entry, channels, alpha_channel, decode);2232 STBIR_PROGRESS_REPORT((float)stbir_info->ring_buffer_first_scanline / stbir_info->output_h);2233 }22342235 if (stbir_info->ring_buffer_first_scanline == stbir_info->ring_buffer_last_scanline)2236 {2237 // We just popped the last scanline off the ring buffer.2238 // Reset it to the empty state.2239 stbir_info->ring_buffer_begin_index = -1;2240 stbir_info->ring_buffer_first_scanline = 0;2241 stbir_info->ring_buffer_last_scanline = 0;2242 break;2243 }2244 else2245 {2246 stbir_info->ring_buffer_first_scanline++;2247 stbir_info->ring_buffer_begin_index = (stbir_info->ring_buffer_begin_index + 1) % stbir_info->ring_buffer_num_entries;2248 }2249 }2250 }2251}22522253static void stbir__buffer_loop_downsample(ostbir__info* stbir_info)2254{2255 int y;2256 float scale_ratio = stbir_info->vertical_scale;2257 int output_h = stbir_info->output_h;2258 float in_pixels_radius = stbir__filter_info_table[stbir_info->vertical_filter].support(scale_ratio) / scale_ratio;2259 int pixel_margin = stbir_info->vertical_filter_pixel_margin;2260 int max_y = stbir_info->input_h + pixel_margin;22612262 STBIR_ASSERT(!stbir__use_height_upsampling(stbir_info));22632264 for (y = -pixel_margin; y < max_y; y++)2265 {2266 float out_center_of_in; // Center of the current out scanline in the in scanline space2267 int out_first_scanline, out_last_scanline;22682269 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);22702271 STBIR_ASSERT(out_last_scanline - out_first_scanline + 1 <= stbir_info->ring_buffer_num_entries);22722273 if (out_last_scanline < 0 || out_first_scanline >= output_h)2274 continue;22752276 stbir__empty_ring_buffer(stbir_info, out_first_scanline);22772278 stbir__decode_and_resample_downsample(stbir_info, y);22792280 // Load in new ones.2281 if (stbir_info->ring_buffer_begin_index < 0)2282 stbir__add_empty_ring_buffer_entry(stbir_info, out_first_scanline);22832284 while (out_last_scanline > stbir_info->ring_buffer_last_scanline)2285 stbir__add_empty_ring_buffer_entry(stbir_info, stbir_info->ring_buffer_last_scanline + 1);22862287 // Now the horizontal buffer is ready to write to all ring buffer rows.2288 stbir__resample_vertical_downsample(stbir_info, y);2289 }22902291 stbir__empty_ring_buffer(stbir_info, stbir_info->output_h);2292}22932294static void stbir__setup(ostbir__info *info, int input_w, int input_h, int output_w, int output_h, int channels)2295{2296 info->input_w = input_w;2297 info->input_h = input_h;2298 info->output_w = output_w;2299 info->output_h = output_h;2300 info->channels = channels;2301}23022303static void stbir__calculate_transform(ostbir__info *info, float s0, float t0, float s1, float t1, float *transform)2304{2305 info->s0 = s0;2306 info->t0 = t0;2307 info->s1 = s1;2308 info->t1 = t1;23092310 if (transform)2311 {2312 info->horizontal_scale = transform[0];2313 info->vertical_scale = transform[1];2314 info->horizontal_shift = transform[2];2315 info->vertical_shift = transform[3];2316 }2317 else2318 {2319 info->horizontal_scale = ((float)info->output_w / info->input_w) / (s1 - s0);2320 info->vertical_scale = ((float)info->output_h / info->input_h) / (t1 - t0);23212322 info->horizontal_shift = s0 * info->output_w / (s1 - s0);2323 info->vertical_shift = t0 * info->output_h / (t1 - t0);2324 }2325}23262327static void stbir__choose_filter(ostbir__info *info, stbir_filter h_filter, stbir_filter v_filter)2328{2329 if (h_filter == 0)2330 h_filter = stbir__use_upsampling(info->horizontal_scale) ? STBIR_DEFAULT_FILTER_UPSAMPLE : STBIR_DEFAULT_FILTER_DOWNSAMPLE;2331 if (v_filter == 0)2332 v_filter = stbir__use_upsampling(info->vertical_scale) ? STBIR_DEFAULT_FILTER_UPSAMPLE : STBIR_DEFAULT_FILTER_DOWNSAMPLE;2333 info->horizontal_filter = h_filter;2334 info->vertical_filter = v_filter;2335}23362337static stbir_uint32 stbir__calculate_memory(ostbir__info *info)2338{2339 int pixel_margin = stbir__get_filter_pixel_margin(info->horizontal_filter, info->horizontal_scale);2340 int filter_height = stbir__get_filter_pixel_width(info->vertical_filter, info->vertical_scale);23412342 info->horizontal_num_contributors = stbir__get_contributors(info->horizontal_scale, info->horizontal_filter, info->input_w, info->output_w);2343 info->vertical_num_contributors = stbir__get_contributors(info->vertical_scale , info->vertical_filter , info->input_h, info->output_h);23442345 // One extra entry because floating point precision problems sometimes cause an extra to be necessary.2346 info->ring_buffer_num_entries = filter_height + 1;23472348 info->horizontal_contributors_size = info->horizontal_num_contributors * sizeof(stbir__contributors);2349 info->horizontal_coefficients_size = stbir__get_total_horizontal_coefficients(info) * sizeof(float);2350 info->vertical_contributors_size = info->vertical_num_contributors * sizeof(stbir__contributors);2351 info->vertical_coefficients_size = stbir__get_total_vertical_coefficients(info) * sizeof(float);2352 info->decode_buffer_size = (info->input_w + pixel_margin * 2) * info->channels * sizeof(float);2353 info->horizontal_buffer_size = info->output_w * info->channels * sizeof(float);2354 info->ring_buffer_size = info->output_w * info->channels * info->ring_buffer_num_entries * sizeof(float);2355 info->encode_buffer_size = info->output_w * info->channels * sizeof(float);23562357 STBIR_ASSERT(info->horizontal_filter != 0);2358 STBIR_ASSERT(info->horizontal_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late2359 STBIR_ASSERT(info->vertical_filter != 0);2360 STBIR_ASSERT(info->vertical_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late23612362 if (stbir__use_height_upsampling(info))2363 // The horizontal buffer is for when we're downsampling the height and we2364 // can't output the result of sampling the decode buffer directly into the2365 // ring buffers.2366 info->horizontal_buffer_size = 0;2367 else2368 // The encode buffer is to retain precision in the height upsampling method2369 // and isn't used when height downsampling.2370 info->encode_buffer_size = 0;23712372 return info->horizontal_contributors_size + info->horizontal_coefficients_size2373 + info->vertical_contributors_size + info->vertical_coefficients_size2374 + info->decode_buffer_size + info->horizontal_buffer_size2375 + info->ring_buffer_size + info->encode_buffer_size;2376}23772378static int stbir__resize_allocated(ostbir__info *info,2379 const void* input_data, int input_stride_in_bytes,2380 void* output_data, int output_stride_in_bytes,2381 int alpha_channel, stbir_uint32 flags, stbir_datatype type,2382 stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace,2383 void* tempmem, size_t tempmem_size_in_bytes)2384{2385 size_t memory_required = stbir__calculate_memory(info);23862387 int width_stride_input = input_stride_in_bytes ? input_stride_in_bytes : info->channels * info->input_w * stbir__type_size[type];2388 int width_stride_output = output_stride_in_bytes ? output_stride_in_bytes : info->channels * info->output_w * stbir__type_size[type];23892390#ifdef STBIR_DEBUG_OVERWRITE_TEST2391#define OVERWRITE_ARRAY_SIZE 82392 unsigned char overwrite_output_before_pre[OVERWRITE_ARRAY_SIZE];2393 unsigned char overwrite_tempmem_before_pre[OVERWRITE_ARRAY_SIZE];2394 unsigned char overwrite_output_after_pre[OVERWRITE_ARRAY_SIZE];2395 unsigned char overwrite_tempmem_after_pre[OVERWRITE_ARRAY_SIZE];23962397 size_t begin_forbidden = width_stride_output * (info->output_h - 1) + info->output_w * info->channels * stbir__type_size[type];2398 memcpy(overwrite_output_before_pre, &((unsigned char*)output_data)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE);2399 memcpy(overwrite_output_after_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE);2400 memcpy(overwrite_tempmem_before_pre, &((unsigned char*)tempmem)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE);2401 memcpy(overwrite_tempmem_after_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE);2402#endif24032404 STBIR_ASSERT(info->channels >= 0);2405 STBIR_ASSERT(info->channels <= STBIR_MAX_CHANNELS);24062407 if (info->channels < 0 || info->channels > STBIR_MAX_CHANNELS)2408 return 0;24092410 STBIR_ASSERT(info->horizontal_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));2411 STBIR_ASSERT(info->vertical_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));24122413 if (info->horizontal_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table))2414 return 0;2415 if (info->vertical_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table))2416 return 0;24172418 if (alpha_channel < 0)2419 flags |= STBIR_FLAG_ALPHA_USES_COLORSPACE | STBIR_FLAG_ALPHA_PREMULTIPLIED;24202421 if (!(flags&STBIR_FLAG_ALPHA_USES_COLORSPACE) || !(flags&STBIR_FLAG_ALPHA_PREMULTIPLIED)) {2422 STBIR_ASSERT(alpha_channel >= 0 && alpha_channel < info->channels);2423 }24242425 if (alpha_channel >= info->channels)2426 return 0;24272428 STBIR_ASSERT(tempmem);24292430 if (!tempmem)2431 return 0;24322433 STBIR_ASSERT(tempmem_size_in_bytes >= memory_required);24342435 if (tempmem_size_in_bytes < memory_required)2436 return 0;24372438 memset(tempmem, 0, tempmem_size_in_bytes);24392440 info->input_data = input_data;2441 info->input_stride_bytes = width_stride_input;24422443 info->output_data = output_data;2444 info->output_stride_bytes = width_stride_output;24452446 info->alpha_channel = alpha_channel;2447 info->flags = flags;2448 info->type = type;2449 info->edge_horizontal = edge_horizontal;2450 info->edge_vertical = edge_vertical;2451 info->colorspace = colorspace;24522453 STBIR_PROFILE_START();24542455 info->horizontal_coefficient_width = stbir__get_coefficient_width (info->horizontal_filter, info->horizontal_scale);2456 info->vertical_coefficient_width = stbir__get_coefficient_width (info->vertical_filter , info->vertical_scale );2457 info->horizontal_filter_pixel_width = stbir__get_filter_pixel_width (info->horizontal_filter, info->horizontal_scale);2458 info->vertical_filter_pixel_width = stbir__get_filter_pixel_width (info->vertical_filter , info->vertical_scale );2459 info->horizontal_filter_pixel_margin = stbir__get_filter_pixel_margin(info->horizontal_filter, info->horizontal_scale);2460 info->vertical_filter_pixel_margin = stbir__get_filter_pixel_margin(info->vertical_filter , info->vertical_scale );24612462 info->ring_buffer_length_bytes = info->output_w * info->channels * sizeof(float);2463 info->decode_buffer_pixels = info->input_w + info->horizontal_filter_pixel_margin * 2;24642465#define STBIR__NEXT_MEMPTR(current, newtype) (newtype*)(((unsigned char*)current) + current##_size)24662467 info->horizontal_contributors = (stbir__contributors *) tempmem;2468 info->horizontal_coefficients = STBIR__NEXT_MEMPTR(info->horizontal_contributors, float);2469 info->vertical_contributors = STBIR__NEXT_MEMPTR(info->horizontal_coefficients, stbir__contributors);2470 info->vertical_coefficients = STBIR__NEXT_MEMPTR(info->vertical_contributors, float);2471 info->decode_buffer = STBIR__NEXT_MEMPTR(info->vertical_coefficients, float);24722473 if (stbir__use_height_upsampling(info))2474 {2475 info->horizontal_buffer = NULL;2476 info->ring_buffer = STBIR__NEXT_MEMPTR(info->decode_buffer, float);2477 info->encode_buffer = STBIR__NEXT_MEMPTR(info->ring_buffer, float);24782479 STBIR_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->encode_buffer, unsigned char) == (size_t)tempmem + tempmem_size_in_bytes);2480 }2481 else2482 {2483 info->horizontal_buffer = STBIR__NEXT_MEMPTR(info->decode_buffer, float);2484 info->ring_buffer = STBIR__NEXT_MEMPTR(info->horizontal_buffer, float);2485 info->encode_buffer = NULL;24862487 STBIR_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->ring_buffer, unsigned char) == (size_t)tempmem + tempmem_size_in_bytes);2488 }24892490#undef STBIR__NEXT_MEMPTR24912492 // This signals that the ring buffer is empty2493 info->ring_buffer_begin_index = -1;24942495 stbir__calculate_filters(info->horizontal_contributors, info->horizontal_coefficients, info->horizontal_filter, info->horizontal_scale, info->horizontal_shift, info->input_w, info->output_w);2496 stbir__calculate_filters(info->vertical_contributors, info->vertical_coefficients, info->vertical_filter, info->vertical_scale, info->vertical_shift, info->input_h, info->output_h);2497 STBIR_PROFILE_END( filters );24982499 STBIR_PROGRESS_REPORT(0);25002501 STBIR_PROFILE_START();2502 if (stbir__use_height_upsampling(info))2503 {2504 stbir__buffer_loop_upsample(info);2505 }2506 else2507 {2508 stbir__buffer_loop_downsample(info);2509 }2510 STBIR_PROFILE_END( looping );251125122513 STBIR_PROGRESS_REPORT(1);25142515#ifdef STBIR_DEBUG_OVERWRITE_TEST2516 STBIR_ASSERT(memcmp(overwrite_output_before_pre, &((unsigned char*)output_data)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE) == 0);2517 STBIR_ASSERT(memcmp(overwrite_output_after_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE) == 0);2518 STBIR_ASSERT(memcmp(overwrite_tempmem_before_pre, &((unsigned char*)tempmem)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE) == 0);2519 STBIR_ASSERT(memcmp(overwrite_tempmem_after_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE) == 0);2520#endif25212522 return 1;2523}252425252526static int stbir__resize_arbitrary(2527 void *alloc_context,2528 const void* input_data, int input_w, int input_h, int input_stride_in_bytes,2529 void* output_data, int output_w, int output_h, int output_stride_in_bytes,2530 float s0, float t0, float s1, float t1, float *transform,2531 int channels, int alpha_channel, stbir_uint32 flags, stbir_datatype type,2532 stbir_filter h_filter, stbir_filter v_filter,2533 stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace)2534{2535 ostbir__info info;2536 int result;2537 size_t memory_required;2538 void* extra_memory;2539 2540 STBIR_PROFILE_FIRST_START();25412542 stbir__setup(&info, input_w, input_h, output_w, output_h, channels);2543 stbir__calculate_transform(&info, s0,t0,s1,t1,transform);2544 stbir__choose_filter(&info, h_filter, v_filter);2545 memory_required = stbir__calculate_memory(&info);2546 extra_memory = STBIR_MALLOC(memory_required, alloc_context);25472548 if (!extra_memory)2549 {2550 return 0;2551 }25522553 result = stbir__resize_allocated(&info, input_data, input_stride_in_bytes,2554 output_data, output_stride_in_bytes, 2555 alpha_channel, flags, type,2556 edge_horizontal, edge_vertical,2557 colorspace, extra_memory, memory_required);25582559 STBIR_PROFILE_END( setup);25602561 STBIR_FREE(extra_memory, alloc_context);25622563 return result;2564}25652566STBIRDEF int stbir_resize_uint8( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2567 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2568 int num_channels)2569{2570 return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,2571 output_pixels, output_w, output_h, output_stride_in_bytes,2572 0,0,1,1,NULL,num_channels,-1,0, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,2573 STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR);2574}25752576STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2577 float *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2578 int num_channels)2579{2580 return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,2581 output_pixels, output_w, output_h, output_stride_in_bytes,2582 0,0,1,1,NULL,num_channels,-1,0, STBIR_TYPE_FLOAT, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,2583 STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR);2584}25852586STBIRDEF int stbir_resize_uint8_srgb(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2587 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2588 int num_channels, int alpha_channel, int flags)2589{2590 return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,2591 output_pixels, output_w, output_h, output_stride_in_bytes,2592 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,2593 STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);2594}25952596STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2597 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2598 int num_channels, int alpha_channel, int flags,2599 stbir_edge edge_wrap_mode)2600{2601 return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,2602 output_pixels, output_w, output_h, output_stride_in_bytes,2603 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,2604 edge_wrap_mode, edge_wrap_mode, STBIR_COLORSPACE_SRGB);2605}26062607STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2608 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2609 int num_channels, int alpha_channel, int flags,2610 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 2611 void *alloc_context)2612{2613 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2614 output_pixels, output_w, output_h, output_stride_in_bytes,2615 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, filter, filter,2616 edge_wrap_mode, edge_wrap_mode, space);2617}26182619STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2620 stbir_uint16 *output_pixels , int output_w, int output_h, int output_stride_in_bytes,2621 int num_channels, int alpha_channel, int flags,2622 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 2623 void *alloc_context)2624{2625 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2626 output_pixels, output_w, output_h, output_stride_in_bytes,2627 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT16, filter, filter,2628 edge_wrap_mode, edge_wrap_mode, space);2629}263026312632STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2633 float *output_pixels , int output_w, int output_h, int output_stride_in_bytes,2634 int num_channels, int alpha_channel, int flags,2635 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 2636 void *alloc_context)2637{2638 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2639 output_pixels, output_w, output_h, output_stride_in_bytes,2640 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_FLOAT, filter, filter,2641 edge_wrap_mode, edge_wrap_mode, space);2642}264326442645STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2646 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2647 stbir_datatype datatype,2648 int num_channels, int alpha_channel, int flags,2649 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, 2650 stbir_filter filter_horizontal, stbir_filter filter_vertical,2651 stbir_colorspace space, void *alloc_context)2652{2653 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2654 output_pixels, output_w, output_h, output_stride_in_bytes,2655 0,0,1,1,NULL,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical,2656 edge_mode_horizontal, edge_mode_vertical, space);2657}265826592660STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2661 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2662 stbir_datatype datatype,2663 int num_channels, int alpha_channel, int flags,2664 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, 2665 stbir_filter filter_horizontal, stbir_filter filter_vertical,2666 stbir_colorspace space, void *alloc_context,2667 float x_scale, float y_scale,2668 float x_offset, float y_offset)2669{2670 float transform[4];2671 transform[0] = x_scale;2672 transform[1] = y_scale;2673 transform[2] = x_offset;2674 transform[3] = y_offset;2675 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2676 output_pixels, output_w, output_h, output_stride_in_bytes,2677 0,0,1,1,transform,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical,2678 edge_mode_horizontal, edge_mode_vertical, space);2679}26802681STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2682 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2683 stbir_datatype datatype,2684 int num_channels, int alpha_channel, int flags,2685 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, 2686 stbir_filter filter_horizontal, stbir_filter filter_vertical,2687 stbir_colorspace space, void *alloc_context,2688 float s0, float t0, float s1, float t1)2689{2690 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2691 output_pixels, output_w, output_h, output_stride_in_bytes,2692 s0,t0,s1,t1,NULL,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical,2693 edge_mode_horizontal, edge_mode_vertical, space);2694}26952696#endif // STB_IMAGE_RESIZE_IMPLEMENTATION26972698/*2699------------------------------------------------------------------------------2700This software is available under 2 licenses -- choose whichever you prefer.2701------------------------------------------------------------------------------2702ALTERNATIVE A - MIT License2703Copyright (c) 2017 Sean Barrett2704Permission is hereby granted, free of charge, to any person obtaining a copy of 2705this software and associated documentation files (the "Software"), to deal in 2706the Software without restriction, including without limitation the rights to 2707use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 2708of the Software, and to permit persons to whom the Software is furnished to do 2709so, subject to the following conditions:2710The above copyright notice and this permission notice shall be included in all 2711copies or substantial portions of the Software.2712THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 2713IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2714FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 2715AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2716LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 2717OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 2718SOFTWARE.2719------------------------------------------------------------------------------2720ALTERNATIVE B - Public Domain (www.unlicense.org)2721This is free and unencumbered software released into the public domain.2722Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 2723software, either in source code form or as a compiled binary, for any purpose, 2724commercial or non-commercial, and by any means.2725In jurisdictions that recognize copyright laws, the author or authors of this 2726software dedicate any and all copyright interest in the software to the public 2727domain. We make this dedication for the benefit of the public at large and to 2728the detriment of our heirs and successors. We intend this dedication to be an 2729overt act of relinquishment in perpetuity of all present and future rights to 2730this software under copyright law.2731THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 2732IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2733FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 2734AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 2735ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 2736WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.2737------------------------------------------------------------------------------2738*/
added:stb_image_resize_test/oldir.c+56 added
1#include <stdio.h>2#include <stdlib.h>34#ifdef _MSC_VER5#define stop() __debugbreak()6#else7#define stop() __builtin_trap()8#endif910//#define HEAVYTM11#include "tm.h"1213#define STBIR_SATURATE_INT14#define STB_IMAGE_RESIZE_STATIC15#define STB_IMAGE_RESIZE_IMPLEMENTATION16#include "old_image_resize.h" 171819static int types[4] = { STBIR_TYPE_UINT8, STBIR_TYPE_UINT8, STBIR_TYPE_UINT16, STBIR_TYPE_FLOAT };20static int edges[4] = { STBIR_EDGE_CLAMP, STBIR_EDGE_REFLECT, STBIR_EDGE_ZERO, STBIR_EDGE_WRAP };21static int flts[5] = { STBIR_FILTER_BOX, STBIR_FILTER_TRIANGLE, STBIR_FILTER_CUBICBSPLINE, STBIR_FILTER_CATMULLROM, STBIR_FILTER_MITCHELL };22static int channels[20] = { 1, 2, 3, 4, 4,4, 2,2, 4,4, 2,2, 4,4, 2,2, 4,4, 2,2 }; 23static int alphapos[20] = { -1, -1, -1, -1, 3,0, 1,0, 3,0, 1,0, 3,0, 1,0, 3,0, 1,0 }; 242526void oresize( void * o, int ox, int oy, int op, void * i, int ix, int iy, int ip, int buf, int type, int edg, int flt )27{28 int t = types[type];29 int ic = channels[buf];30 int alpha = alphapos[buf];31 int e = edges[edg];32 int f = flts[flt];33 int space = ( type == 1 ) ? STBIR_COLORSPACE_SRGB : 0;34 int flags = ( buf >= 16 ) ? STBIR_FLAG_ALPHA_PREMULTIPLIED : ( ( buf >= 12 ) ? STBIR_FLAG_ALPHA_OUT_PREMULTIPLIED : ( ( buf >= 8 ) ? (STBIR_FLAG_ALPHA_PREMULTIPLIED|STBIR_FLAG_ALPHA_OUT_PREMULTIPLIED) : 0 ) );35 stbir_uint64 start;3637 ENTER( "Resize (old)" );38 start = tmGetAccumulationStart( tm_mask );3940 if(!stbir_resize( i, ix, iy, ip, o, ox, oy, op, t, ic, alpha, flags, e, e, f, f, space, 0 ) )41 stop();4243 #ifdef STBIR_PROFILE44 tmEmitAccumulationZone( 0, 0, (tm_uint64 *)&start, 0, oldprofile.named.setup, "Setup (old)" );45 tmEmitAccumulationZone( 0, 0, (tm_uint64 *)&start, 0, oldprofile.named.filters, "Filters (old)" );46 tmEmitAccumulationZone( 0, 0, (tm_uint64 *)&start, 0, oldprofile.named.looping, "Looping (old)" );47 tmEmitAccumulationZone( 0, 0, (tm_uint64 *)&start, 0, oldprofile.named.vertical, "Vertical (old)" );48 tmEmitAccumulationZone( 0, 0, (tm_uint64 *)&start, 0, oldprofile.named.horizontal, "Horizontal (old)" );49 tmEmitAccumulationZone( 0, 0, (tm_uint64 *)&start, 0, oldprofile.named.decode, "Scanline input (old)" );50 tmEmitAccumulationZone( 0, 0, (tm_uint64 *)&start, 0, oldprofile.named.encode, "Scanline output (old)" );51 tmEmitAccumulationZone( 0, 0, (tm_uint64 *)&start, 0, oldprofile.named.alpha, "Alpha weighting (old)" );52 tmEmitAccumulationZone( 0, 0, (tm_uint64 *)&start, 0, oldprofile.named.unalpha, "Alpha unweighting (old)" );53 #endif5455 LEAVE();56}
added:stb_image_resize_test/stbirtest.c+992 added
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>45//#define HEAVYTM6#include "tm.h"78#ifdef RADUSETM39tm_api * g_tm_api;10//#define PROFILE_MODE11#endif1213#include <math.h>1415#ifdef _MSC_VER16#define stop() __debugbreak()17#include <windows.h>18#define int64 __int6419#define uint64 unsigned __int6420#else21#define stop() __builtin_trap()22#define int64 long long23#define uint64 unsigned long long24#endif2526#ifdef _MSC_VER27#pragma warning(disable:4127)28#endif2930//#define NOCOMP313233//#define PROFILE_NEW_ONLY34//#define PROFILE_MODE353637#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_ )3839#ifdef _MSC_VER4041 uint64 __rdtsc();42 #define __cycles() __rdtsc()4344#else // non msvc4546 static inline uint64 __cycles() 47 {48 unsigned int lo, hi;49 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi) );50 return ( ( (uint64) hi ) << 32 ) | ( (uint64) lo );51 }5253#endif // msvc5455#elif defined( _M_ARM64 ) || defined( __aarch64__ ) || defined( __arm64__ ) || defined(__ARM_NEON__) 5657#ifdef _MSC_VER5859 #define __cycles() _ReadStatusReg(ARM64_CNTVCT)6061#else6263 static inline uint64 __cycles()64 {65 uint64 tsc;66 asm volatile("mrs %0, cntvct_el0" : "=r" (tsc));67 return tsc;68 }6970#endif7172#else // x64, arm7374#error Unknown platform for timing.7576#endif //x64 and 777879#ifdef PROFILE_MODE8081#define STBIR_ASSERT(cond)8283#endif8485#ifdef _DEBUG86#undef STBIR_ASSERT87#define STBIR_ASSERT(cond) { if (!(cond)) stop(); }88#endif899091#define SHRINKBYW 292#define ZOOMBYW 293#define SHRINKBYH 294#define ZOOMBYH 2959697int mem_count = 0;9899#ifdef TEST_WITH_VALLOC100101#define STBIR__SEPARATE_ALLOCATIONS102103#if TEST_WITH_LIMIT_AT_FRONT104105 void * wmalloc(SIZE_T size)106 {107 static unsigned int pagesize=0;108 void* p;109 SIZE_T s;110111 // get the page size, if we haven't yet112 if (pagesize==0)113 {114 SYSTEM_INFO si;115 GetSystemInfo(&si);116 pagesize=si.dwPageSize;117 }118119 // we need room for the size, 8 bytes to hide the original pointer and a120 // validation dword, and enough data to completely fill one page121 s=(size+(pagesize-1))&~(pagesize-1);122123 // allocate the size plus a page (for the guard)124 p=VirtualAlloc(0,(SIZE_T)s,MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE);125 126 return p;127 }128129 void wfree(void * ptr)130 {131 if (ptr)132 {133 if ( ((ptrdiff_t)ptr) & 4095 ) stop();134 if ( VirtualFree(ptr,0,MEM_RELEASE) == 0 ) stop();135 }136 }137138#else139140 void * wmalloc(SIZE_T size)141 {142 static unsigned int pagesize=0;143 void* p;144 SIZE_T s;145146 // get the page size, if we haven't yet147 if (pagesize==0)148 {149 SYSTEM_INFO si;150 GetSystemInfo(&si);151 pagesize=si.dwPageSize;152 }153154 // we need room for the size, 8 bytes to hide the original pointer and a155 // validation dword, and enough data to completely fill one page156 s=(size+16+(pagesize-1))&~(pagesize-1);157158 // allocate the size plus a page (for the guard)159 p=VirtualAlloc(0,(SIZE_T)(s+pagesize+pagesize),MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE);160161 if (p)162 {163 DWORD oldprot;164 void* orig=p;165166 // protect the first page167 VirtualProtect(((char*)p),pagesize,PAGE_NOACCESS,&oldprot);168169 // protect the final page170 VirtualProtect(((char*)p)+s+pagesize,pagesize,PAGE_NOACCESS,&oldprot);171172 // now move the returned pointer so that it bumps right up against the173 // the next (protected) page (this may result in unaligned return174 // addresses - pre-align the sizes if you always want aligned ptrs)175//#define ERROR_ON_FRONT176#ifdef ERROR_ON_FRONT177 p=((char*)p)+pagesize+16;178#else179 p=((char*)p)+(s-size)+pagesize;180#endif181182 // hide the validation value and the original pointer (which we'll183 // need used for freeing) right behind the returned pointer184 ((unsigned int*)p)[-1]=0x98765432;185 ((void**)p)[-2]=orig;186 ++mem_count;187//printf("aloc: %p bytes: %d\n",p,(int)size);188 return(p);189 }190191 return 0;192 }193194 void wfree(void * ptr)195 {196 if (ptr)197 {198 int err=0;199200 // is this one of our allocations?201 if (((((unsigned int*)ptr)[-1])!=0x98765432) || ((((void**)ptr)[-2])==0))202 {203 err=1;204 }205206 if (err)207 {208 __debugbreak();209 }210 else211 {212213 // back up to find the original pointer214 void* p=((void**)ptr)[-2];215216 // clear the validation value and the original pointer217 ((unsigned int*)ptr)[-1]=0;218 ((void**)ptr)[-2]=0;219220//printf("free: %p\n",ptr);221222 --mem_count;223224 // now free the pages225 if (p)226 VirtualFree(p,0,MEM_RELEASE);227228 }229 }230 }231232#endif233234#define STBIR_MALLOC(size,user_data) ((void)(user_data), wmalloc(size))235#define STBIR_FREE(ptr,user_data) ((void)(user_data), wfree(ptr))236237#endif238239#define STBIR_PROFILE240//#define STBIR_NO_SIMD241//#define STBIR_AVX242//#define STBIR_AVX2243#define STB_IMAGE_RESIZE_IMPLEMENTATION244#include "stb_image_resize2.h" // new one!245246#define STB_IMAGE_WRITE_IMPLEMENTATION247#include "stb_image_write.h"248249int tsizes[5] = { 1, 1, 2, 4, 2 };250int ttypes[5] = { STBIR_TYPE_UINT8, STBIR_TYPE_UINT8_SRGB, STBIR_TYPE_UINT16, STBIR_TYPE_FLOAT, STBIR_TYPE_HALF_FLOAT };251252int cedges[4] = { STBIR_EDGE_CLAMP, STBIR_EDGE_REFLECT, STBIR_EDGE_ZERO, STBIR_EDGE_WRAP };253int flts[5] = { STBIR_FILTER_BOX, STBIR_FILTER_TRIANGLE, STBIR_FILTER_CUBICBSPLINE, STBIR_FILTER_CATMULLROM, STBIR_FILTER_MITCHELL };254int buffers[20] = { STBIR_1CHANNEL, STBIR_2CHANNEL, STBIR_RGB, STBIR_4CHANNEL, 255 STBIR_BGRA, STBIR_ARGB, STBIR_RA, STBIR_AR,256 STBIR_RGBA_PM, STBIR_ARGB_PM, STBIR_RA_PM, STBIR_AR_PM,257 STBIR_RGBA, STBIR_ARGB, STBIR_RA, STBIR_AR,258 STBIR_RGBA_PM, STBIR_ARGB_PM, STBIR_RA_PM, STBIR_AR_PM,259 };260int obuffers[20] = { STBIR_1CHANNEL, STBIR_2CHANNEL, STBIR_RGB, STBIR_4CHANNEL, 261 STBIR_BGRA, STBIR_ARGB, STBIR_RA, STBIR_AR,262 STBIR_RGBA_PM, STBIR_ARGB_PM, STBIR_RA_PM, STBIR_AR_PM,263 STBIR_RGBA_PM, STBIR_ARGB_PM, STBIR_RA_PM, STBIR_AR_PM,264 STBIR_RGBA, STBIR_ARGB, STBIR_RA, STBIR_AR,265 };266267int bchannels[20] = { 1, 2, 3, 4, 4,4, 2,2, 4,4, 2,2, 4,4, 2,2, 4,4, 2,2 }; 268int alphapos[20] = { -1, -1, -1, -1, 3,0, 1,0, 3,0, 1,0, 3,0, 1,0,3,0, 1,0 }; 269270271char const * buffstrs[20] = { "1ch", "2ch", "3ch", "4ch", "RGBA", "ARGB", "RA", "AR", "RGBA_both_pre", "ARGB_both_pre", "RA_both_pre", "AR_both_pre", "RGBA_out_pre", "ARGB_out_pre", "RA_out_pre", "AR_out_pre", "RGBA_in_pre", "ARGB_in_pre", "RA_in_pre", "AR_in_pre" };272char const * typestrs[5] = { "Bytes", "BytesSRGB", "Shorts", "Floats", "Half Floats"};273char const * edgestrs[4] = { "Clamp", "Reflect", "Zero", "Wrap" };274char const * fltstrs[5] = { "Box", "Triangle", "Cubic", "Catmullrom", "Mitchell" };275276#ifdef STBIR_PROFILE277 static void do_acc_zones( STBIR_PROFILE_INFO * profile )278 {279 stbir_uint32 j;280 stbir_uint64 start = tmGetAccumulationStart( tm_mask ); start=start;281282 for( j = 0 ; j < profile->count ; j++ )283 {284 if ( profile->clocks[j] )285 tmEmitAccumulationZone( 0, 0, (tm_uint64*)&start, 0, profile->clocks[j], profile->descriptions[j] );286 }287 }288#else289 #define do_acc_zones(...)290#endif291292int64 vert;293294//#define WINTHREADTEST295#ifdef WINTHREADTEST296297static STBIR_RESIZE * thread_resize;298static LONG which;299static int threads_started = 0;300static HANDLE threads[32];301static HANDLE starts,stops;302 303static DWORD resize_shim( LPVOID p )304{305 for(;;)306 {307 LONG wh;308309 WaitForSingleObject( starts, INFINITE );310311 wh = InterlockedAdd( &which, 1 ) - 1;312313 ENTER( "Split %d", wh );314 stbir_resize_split( thread_resize, wh, 1 );315 #ifdef STBIR_PROFILE316 { STBIR_PROFILE_INFO profile; stbir_resize_split_profile_info( &profile, thread_resize, wh, 1 ); do_acc_zones( &profile ); vert = profile.clocks[1]; }317 #endif318 LEAVE();319320 ReleaseSemaphore( stops, 1, 0 );321 }322}323324#endif325326void nresize( void * o, int ox, int oy, int op, void * i, int ix, int iy, int ip, int buf, int type, int edg, int flt )327{328 STBIR_RESIZE resize;329 330 stbir_resize_init( &resize, i, ix, iy, ip, o, ox, oy, op, buffers[buf], ttypes[type] );331 stbir_set_pixel_layouts( &resize, buffers[buf], obuffers[buf] );332 stbir_set_edgemodes( &resize, cedges[edg], cedges[edg] );333 stbir_set_filters( &resize, flts[flt], /*STBIR_FILTER_POINT_SAMPLE */ flts[flt] );334 //stbir_set_input_subrect( &resize, 0.55f,0.333f,0.75f,0.50f);335 //stbir_set_output_pixel_subrect( &resize, 00, 00, ox/2,oy/2);336 //stbir_set_pixel_subrect(&resize, 1430,1361,30,30);337 338 ENTER( "Resize" );339 340 #ifndef WINTHREADTEST341342 ENTER( "Filters" );343 stbir_build_samplers_with_splits( &resize, 1 );344 #ifdef STBIR_PROFILE345 { STBIR_PROFILE_INFO profile; stbir_resize_build_profile_info( &profile, &resize ); do_acc_zones( &profile ); }346 #endif347 LEAVE();348 349 ENTER( "Resize" );350 if(!stbir_resize_extended( &resize ) )351 stop();352 #ifdef STBIR_PROFILE353 { STBIR_PROFILE_INFO profile; stbir_resize_extended_profile_info( &profile, &resize ); do_acc_zones( &profile ); vert = profile.clocks[1]; }354 #endif355 LEAVE();356357 #else358 {359 int c, cnt;360 361 ENTER( "Filters" );362 cnt = stbir_build_samplers_with_splits( &resize, 4 );363 #ifdef STBIR_PROFILE364 { STBIR_PROFILE_INFO profile; stbir_resize_build_profile_info( &profile, &resize ); do_acc_zones( &profile ); }365 #endif366 LEAVE();367 368 ENTER( "Thread start" );369 if ( threads_started == 0 )370 {371 starts = CreateSemaphore( 0, 0, 32, 0 );372 stops = CreateSemaphore( 0, 0, 32, 0 );373 }374 for( c = threads_started ; c < cnt ; c++ )375 threads[ c ] = CreateThread( 0, 2048*1024, resize_shim, 0, 0, 0 );376 377 threads_started = cnt;378 thread_resize = &resize;379 which = 0;380 LEAVE();381382 // starts the threads383 ReleaseSemaphore( starts, cnt, 0 );384385 ENTER( "Wait" );386 for( c = 0 ; c < cnt; c++ )387 WaitForSingleObject( stops, INFINITE );388 LEAVE();389 }390 #endif391392 ENTER( "Free" );393 stbir_free_samplers( &resize );394 LEAVE();395 LEAVE();396}397398399#define STB_IMAGE_IMPLEMENTATION400#include "stb_image.h"401402extern void oresize( void * o, int ox, int oy, int op, void * i, int ix, int iy, int ip, int buf, int type, int edg, int flt );403404405406#define TYPESTART 0407#define TYPEEND 4408409#define LAYOUTSTART 0410#define LAYOUTEND 19411412#define SIZEWSTART 0413#define SIZEWEND 2414415#define SIZEHSTART 0416#define SIZEHEND 2417418#define EDGESTART 0419#define EDGEEND 3420421#define FILTERSTART 0422#define FILTEREND 4423424#define HEIGHTSTART 0425#define HEIGHTEND 2426427#define WIDTHSTART 0428#define WIDTHEND 2429430431432433static void * convert8to16( unsigned char * i, int w, int h, int c )434{435 unsigned short * ret;436 int p;437438 ret = malloc( w*h*c*sizeof(short) );439 for(p = 0 ; p < (w*h*c) ; p++ )440 {441 ret[p]=(short)((((int)i[p])<<8)+i[p]);442 }443444 return ret;445}446447static void * convert8tof( unsigned char * i, int w, int h, int c )448{449 float * ret;450 int p;451452 ret = malloc( w*h*c*sizeof(float) );453 for(p = 0 ; p < (w*h*c) ; p++ )454 {455 ret[p]=((float)i[p])*(1.0f/255.0f);456 }457458 return ret;459}460461static void * convert8tohf( unsigned char * i, int w, int h, int c )462{463 stbir__FP16 * ret;464 int p;465466 ret = malloc( w*h*c*sizeof(stbir__FP16) );467 for(p = 0 ; p < (w*h*c) ; p++ )468 {469 ret[p]=stbir__float_to_half(((float)i[p])*(1.0f/255.0f));470 }471472 return ret;473}474475static void * convert8tohff( unsigned char * i, int w, int h, int c )476{477 float * ret;478 int p;479480 ret = malloc( w*h*c*sizeof(float) );481 for(p = 0 ; p < (w*h*c) ; p++ )482 {483 ret[p]=stbir__half_to_float(stbir__float_to_half(((float)i[p])*(1.0f/255.0f)));484 }485486 return ret;487}488489static int isprime( int v )490{491 int i;492493 if ( v <= 3 )494 return ( v > 1 );495 if ( ( v & 1 ) == 0 )496 return 0;497 if ( ( v % 3 ) == 0 )498 return 0;499 i = 5;500 while ( (i*i) <= v )501 {502 if ( ( v % i ) == 0 )503 return 0;504 if ( ( v % ( i + 2 ) ) == 0 )505 return 0;506 i += 6;507 }508509 return 1;510}511512static int getprime( int v )513{514 int i;515 i = 0;516 for(;;)517 {518 if ( i >= v )519 return v; // can't find any, just return orig520 if (isprime(v - i))521 return v - i;522 if (isprime(v + i))523 return v + i;524 ++i;525 }526}527528529int main( int argc, char ** argv )530{531 int ix, iy, ic;532 unsigned char * input[6];533 char * ir1;534 char * ir2;535 int szhs[3];536 int szws[3];537 int aw, ah, ac;538 unsigned char * correctalpha;539 int layouts, types, heights, widths, edges, filters;540541 if ( argc != 2 )542 {543 printf("command: stbirtest [imagefile]\n");544 exit(1);545 }546547 SetupTM( "127.0.0.1" );548549 correctalpha = stbi_load( "correctalpha.png", &aw, &ah, &ac, 0 );550551 input[0] = stbi_load( argv[1], &ix, &iy, &ic, 0 );552 input[1] = input[0];553 input[2] = convert8to16( input[0], ix, iy, ic );554 input[3] = convert8tof( input[0], ix, iy, ic );555 input[4] = convert8tohf( input[0], ix, iy, ic );556 input[5] = convert8tohff( input[0], ix, iy, ic );557558 printf("Input %dx%d (%d channels)\n",ix,iy,ic);559560 ir1 = malloc( 4 * 4 * 3000 * 3000ULL );561 ir2 = malloc( 4 * 4 * 3000 * 3000ULL );562563 szhs[0] = getprime( iy/SHRINKBYH );564 szhs[1] = iy;565 szhs[2] = getprime( iy*ZOOMBYH );566567 szws[0] = getprime( ix/SHRINKBYW );568 szws[1] = ix;569 szws[2] = getprime( ix*ZOOMBYW );570571 #if 1572 for( types = TYPESTART ; types <= TYPEEND ; types++ )573 #else574 for( types = 1 ; types <= 1 ; types++ )575 #endif576 {577 ENTER( "Test type: %s",typestrs[types]);578 #if 1579 for( layouts = LAYOUTSTART ; layouts <= LAYOUTEND ; layouts++ )580 #else581 for( layouts = 16; layouts <= 16 ; layouts++ )582 #endif583 {584 ENTER( "Test layout: %s",buffstrs[layouts]);585 586 #if 0587 for( heights = HEIGHTSTART ; heights <= HEIGHTEND ; heights++ )588 {589 int w, h = szhs[heights];590 #else591 for( heights = 0 ; heights <= 11 ; heights++ )592 {593 static int szhsz[12]={32, 200, 350, 400, 450, 509, 532, 624, 700, 824, 1023, 2053 };594 int w, h = szhsz[heights];595 #endif596597 ENTER( "Test height: %d %s %d",iy,(h<iy)?"Down":((h>iy)?"Up":"Same"),h);598 599 #if 0600 for( widths = WIDTHSTART ; widths <= WIDTHEND ; widths++ )601 {602 w = szws[widths];603 #else604 for( widths = 0 ; widths <= 12 ; widths++ )605 {606 static int szwsz[13]={2, 32, 200, 350, 400, 450, 509, 532, 624, 700, 824, 1023, 2053 };607 w = szwsz[widths];608 #endif609610 ENTER( "Test width: %d %s %d",ix, (w<ix)?"Down":((w>ix)?"Up":"Same"), w);611612 #if 0613 for( edges = EDGESTART ; edges <= EDGEEND ; edges++ )614 #else615 for( edges = 0 ; edges <= 0 ; edges++ )616 #endif617 {618 ENTER( "Test edge: %s",edgestrs[edges]);619 #if 0620 for( filters = FILTERSTART ; filters <= FILTEREND ; filters++ )621 #else622 for( filters = 3 ; filters <= 3 ; filters++ )623 #endif624 {625 int op, opw, np,npw, c, a;626 #ifdef COMPARE_SAME 627 int oldtypes = types;628 #else629 int oldtypes = (types==4)?3:types;630 #endif631 632 ENTER( "Test filter: %s",fltstrs[filters]);633 {634 c = bchannels[layouts];635 a = alphapos[layouts];636637 op = w*tsizes[oldtypes]*c + 60;638 opw = w*tsizes[oldtypes]*c;639640 np = w*tsizes[types]*c + 60;641 npw = w*tsizes[types]*c;642643 printf( "%s:layout: %s w: %d h: %d edge: %s filt: %s\n", typestrs[types],buffstrs[layouts], w, h, edgestrs[edges], fltstrs[filters] );644645646 // clear pixel area to different, right edge to zero647 #ifndef NOCLEAR648 ENTER( "Test clear padding" );649 {650 int d;651 for( d = 0 ; d < h ; d++ )652 {653 int oofs = d * op;654 int nofs = d * np;655 memset( ir1 + oofs, 192, opw );656 memset( ir1 + oofs+opw, 79, op-opw );657 memset( ir2 + nofs, 255, npw );658 memset( ir2 + nofs+npw, 79, np-npw );659 }660 }661 LEAVE();662663 #endif664665 #ifdef COMPARE_SAME 666 #define TIMINGS 1667 #else 668 #define TIMINGS 1669 #endif670 ENTER( "Test both" );671 {672 #ifndef PROFILE_NEW_ONLY673 {674 int ttt, max = 0x7fffffff;675 ENTER( "Test old" );676 for( ttt = 0 ; ttt < TIMINGS ; ttt++ )677 {678 int64 m = __cycles();679680 oresize( ir1, w, h, op, 681 #ifdef COMPARE_SAME 682 input[types], 683 #else684 input[(types==4)?5:types], 685 #endif686 ix, iy, ix*ic*tsizes[oldtypes], layouts, oldtypes, edges, filters );687688 m = __cycles() - m;689 if ( ( (int)m ) < max )690 max = (int) m;691 }692 LEAVE();693 printf("old: %d\n", max );694 }695 #endif696 697 {698 int ttt, max = 0x7fffffff, maxv = 0x7fffffff;699 ENTER( "Test new" );700 for( ttt = 0 ; ttt < TIMINGS ; ttt++ )701 {702 int64 m = __cycles();703704 nresize( ir2, w, h, np, input[types], ix, iy, ix*ic*tsizes[types], layouts, types, edges, filters );705 706 m = __cycles() - m;707 if ( ( (int)m ) < max )708 max = (int) m;709 if ( ( (int)vert ) < maxv )710 maxv = (int) vert;711 }712 LEAVE(); // test new713 printf("new: %d (v: %d)\n", max, maxv );714 }715 }716 LEAVE(); // test both717718 if ( mem_count!= 0 )719 stop();720721 #ifndef NOCOMP722 ENTER( "Test compare" );723 {724 int x,y,ch;725 int nums = 0;726 for( y = 0 ; y < h ; y++ )727 {728 for( x = 0 ; x < w ; x++ )729 {730 switch(types)731 {732 case 0:733 case 1: //SRGB 734 {735 unsigned char * p1 = (unsigned char *)&ir1[y*op+x*c];736 unsigned char * p2 = (unsigned char *)&ir2[y*np+x*c];737 for( ch = 0 ; ch < c ; ch++ )738 {739 float pp1,pp2,d;740 float av = (a==-1)?1.0f:((float)p1[a]/255.0f);741742 pp1 = p1[ch];743 pp2 = p2[ch];744745 // compare in premult space746 #ifndef COMPARE_SAME 747 if ( ( ( layouts >=4 ) && ( layouts <= 7 ) ) || ( ( layouts >=16 ) && ( layouts <= 19 ) ) )748 {749 pp1 *= av;750 pp2 *= av;751 }752 #endif753754 d = pp1 - pp2;755 if ( d < 0 ) d = -d;756757 #ifdef COMPARE_SAME 758 if ( d > 0 ) 759 #else 760 if ( d > 1 )761 #endif762 {763 printf("Error at %d x %d (chan %d) (d: %g a: %g) [%d %d %d %d] [%d %d %d %d]\n",x,y,ch, d,av, p1[0],p1[1],p1[2],p1[3], p2[0],p2[1],p2[2],p2[3]);764 ++nums;765 if ( nums > 16 ) goto ex;766 //if (d) exit(1);767 //goto ex;768 }769 } 770 }771 break;772773 case 2:774 {775 unsigned short * p1 = (unsigned short *)&ir1[y*op+x*c*sizeof(short)];776 unsigned short * p2 = (unsigned short *)&ir2[y*np+x*c*sizeof(short)];777 for( ch = 0 ; ch < c ; ch++ )778 {779 float thres,pp1,pp2,d;780 float av = (a==-1)?1.0f:((float)p1[a]/65535.0f);781782 pp1 = p1[ch];783 pp2 = p2[ch];784785 // compare in premult space786 #ifndef COMPARE_SAME 787 if ( ( ( layouts >=4 ) && ( layouts <= 7 ) ) || ( ( layouts >= 16 ) && ( layouts <= 19 ) ) )788 {789 pp1 *= av;790 pp2 *= av;791 }792 #endif793794 d = pp1 - pp2;795 if ( d < 0 ) d = -d;796797 thres=((float)p1[ch]*0.007f)+2.0f;798 if (thres<4) thres = 4;799 800 #ifdef COMPARE_SAME 801 if ( d > 0 ) 802 #else 803 if ( d > thres)804 #endif805 {806 printf("Error at %d x %d (chan %d) %d %d [df: %g th: %g al: %g] (%d %d %d %d) (%d %d %d %d)\n",x,y,ch, p1[ch],p2[ch],d,thres,av,p1[0],p1[1],p1[2],p1[3],p2[0],p2[1],p2[2],p2[3]);807 ++nums;808 if ( nums > 16 ) goto ex;809 //if (d) exit(1);810 //goto ex;811 }812 }813 }814 break;815816 case 3:817 {818 float * p1 = (float *)&ir1[y*op+x*c*sizeof(float)];819 float * p2 = (float *)&ir2[y*np+x*c*sizeof(float)];820 for( ch = 0 ; ch < c ; ch++ )821 {822 float pp1 = p1[ch], pp2 = p2[ch];823 float av = (a==-1)?1.0f:p1[a];824 float thres, d;825826 // clamp827 if (pp1<=0.0f) pp1 = 0;828 if (pp2<=0.0f) pp2 = 0;829 if (av<=0.0f) av = 0;830 if (pp1>1.0f) pp1 = 1.0f;831 if (pp2>1.0f) pp2 = 1.0f;832 if (av>1.0f) av = 1.0f;833834 // compare in premult space835 #ifndef COMPARE_SAME 836 if ( ( ( layouts >=4 ) && ( layouts <= 7 ) ) || ( ( layouts >= 16 ) && ( layouts <= 19 ) ) )837 {838 pp1 *= av;839 pp2 *= av;840 }841 #endif842843 d = pp1 - pp2;844 if ( d < 0 ) d = -d;845846 thres=(p1[ch]*0.002f)+0.0002f;847 if ( thres < 0 ) thres = -thres;848849 #ifdef COMPARE_SAME 850 if ( d != 0.0f ) 851 #else 852 if ( d > thres )853 #endif854 {855 printf("Error at %d x %d (chan %d) %g %g [df: %g th: %g al: %g] (%g %g %g %g) (%g %g %g %g)\n",x,y,ch, p1[ch],p2[ch],d,thres,av,p1[0],p1[1],p1[2],p1[3],p2[0],p2[1],p2[2],p2[3]);856 ++nums;857 if ( nums > 16 ) goto ex;858 //if (d) exit(1);859 //goto ex;860 }861 }862 }863 break;864865 case 4:866 {867 #ifdef COMPARE_SAME 868 stbir__FP16 * p1 = (stbir__FP16 *)&ir1[y*op+x*c*sizeof(stbir__FP16)];869 #else870 float * p1 = (float *)&ir1[y*op+x*c*sizeof(float)];871 #endif872 stbir__FP16 * p2 = (stbir__FP16 *)&ir2[y*np+x*c*sizeof(stbir__FP16)];873 for( ch = 0 ; ch < c ; ch++ )874 {875 #ifdef COMPARE_SAME 876 float pp1 = stbir__half_to_float(p1[ch]);877 float av = (a==-1)?1.0f:stbir__half_to_float(p1[a]);878 #else879 float pp1 = stbir__half_to_float(stbir__float_to_half(p1[ch]));880 float av = (a==-1)?1.0f:stbir__half_to_float(stbir__float_to_half(p1[a]));881 #endif882 float pp2 = stbir__half_to_float(p2[ch]);883 float d, thres;884885 // clamp886 if (pp1<=0.0f) pp1 = 0;887 if (pp2<=0.0f) pp2 = 0;888 if (av<=0.0f) av = 0;889 if (pp1>1.0f) pp1 = 1.0f;890 if (pp2>1.0f) pp2 = 1.0f;891 if (av>1.0f) av = 1.0f;892893 thres=(pp1*0.002f)+0.0002f;894895 // compare in premult space896 #ifndef COMPARE_SAME 897 if ( ( ( layouts >=4 ) && ( layouts <= 7 ) ) || ( ( layouts >= 16 ) && ( layouts <= 19 ) ) )898 {899 pp1 *= av;900 pp2 *= av;901 }902 #endif903904 d = pp1 - pp2;905 if ( d < 0 ) d = -d;906907908 #ifdef COMPARE_SAME 909 if ( d != 0.0f ) 910 #else 911 if ( d > thres )912 #endif913 {914 printf("Error at %d x %d (chan %d) %g %g [df: %g th: %g al: %g] (%g %g %g %g) (%g %g %g %g)\n",x,y,ch, 915 #ifdef COMPARE_SAME 916 stbir__half_to_float(p1[ch]), 917 #else918 p1[ch],919 #endif920 stbir__half_to_float(p2[ch]), 921 d,thres,av,922 #ifdef COMPARE_SAME 923 stbir__half_to_float(p1[0]),stbir__half_to_float(p1[1]),stbir__half_to_float(p1[2]),stbir__half_to_float(p1[3]),924 #else925 p1[0],p1[1],p1[2],p1[3],926 #endif927 stbir__half_to_float(p2[0]),stbir__half_to_float(p2[1]),stbir__half_to_float(p2[2]),stbir__half_to_float(p2[3]) );928 ++nums;929 if ( nums > 16 ) goto ex;930 //if (d) exit(1);931 //goto ex;932 }933 }934 }935 break;936 }937 }938939 for( x = (w*c)*tsizes[oldtypes]; x < op; x++ )940 {941 if ( ir1[y*op+x] != 79 )942 {943 printf("Margin error at %d x %d %d (should be 79) OLD!\n",x,y,(unsigned char)ir1[y*op+x]);944 goto ex;945 }946 }947948 for( x = (w*c)*tsizes[types]; x < np; x++ )949 {950 if ( ir2[y*np+x] != 79 )951 {952 printf("Margin error at %d x %d %d (should be 79) NEW\n",x,y,(unsigned char)ir2[y*np+x]);953 goto ex;954 }955 }956 }957 958 ex:959 ENTER( "OUTPUT IMAGES" );960 printf(" tot pix: %d, errs: %d\n", w*h*c,nums );961962 if (nums)963 {964 stbi_write_png("old.png", w, h, c, ir1, op);965 stbi_write_png("new.png", w, h, c, ir2, np);966 exit(1);967 }968969 LEAVE(); // output images970 }971 LEAVE(); //test compare972 #endif973974975976 }977 LEAVE(); // test filter978 }979 LEAVE(); // test edge980 }981 LEAVE(); // test width982 }983 LEAVE(); // test height984 }985 LEAVE(); // test type986 }987 LEAVE(); // test layout988 }989990 CloseTM();991 return 0;992}
added:stb_image_resize_test/vf_train.c+999 added
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>45#define stop() __debugbreak()6#include <windows.h>7#define int64 __int6489#pragma warning(disable:4127)1011#define STBIR__WEIGHT_TABLES 12#define STBIR_PROFILE13#define STB_IMAGE_RESIZE_IMPLEMENTATION14#include "stb_image_resize2.h" 1516static int * file_read( char const * filename )17{18 size_t s;19 int * m;20 FILE * f = fopen( filename, "rb" );21 if ( f == 0 ) return 0;22 23 fseek( f, 0, SEEK_END);24 s = ftell( f );25 fseek( f, 0, SEEK_SET);26 m = malloc( s + 4 );27 m[0] = (int)s;28 fread( m+1, 1, s, f);29 fclose(f);3031 return( m );32}3334typedef struct fileinfo35{36 int * timings;37 int timing_count;38 int dimensionx, dimensiony;39 int numtypes;40 int * types;41 int * effective;42 int cpu;43 int simd;44 int numinputrects;45 int * inputrects;46 int outputscalex, outputscaley;47 int milliseconds;48 int64 cycles;49 double scale_time;50 int bitmapx, bitmapy;51 char const * filename;52} fileinfo;5354int numfileinfo;55fileinfo fi[256];56unsigned char * bitmap;57int bitmapw, bitmaph, bitmapp;5859static int use_timing_file( char const * filename, int index )60{61 int * base = file_read( filename );62 int * file = base;6364 if ( base == 0 ) return 0;6566 ++file; // skip file image size;67 if ( *file++ != 'VFT1' ) return 0;68 fi[index].cpu = *file++;69 fi[index].simd = *file++;70 fi[index].dimensionx = *file++;71 fi[index].dimensiony = *file++;72 fi[index].numtypes = *file++;73 fi[index].types = file; file += fi[index].numtypes;74 fi[index].effective = file; file += fi[index].numtypes;75 fi[index].numinputrects = *file++;76 fi[index].inputrects = file; file += fi[index].numinputrects * 2;77 fi[index].outputscalex = *file++;78 fi[index].outputscaley = *file++;79 fi[index].milliseconds = *file++;80 fi[index].cycles = ((int64*)file)[0]; file += 2;81 fi[index].filename = filename;8283 fi[index].timings = file;84 fi[index].timing_count = (int) ( ( base[0] - ( ((char*)file - (char*)base - sizeof(int) ) ) ) / (sizeof(int)*2) );8586 fi[index].scale_time = (double)fi[index].milliseconds / (double)fi[index].cycles;8788 return 1;89}9091static int vert_first( float weights_table[STBIR_RESIZE_CLASSIFICATIONS][4], int ox, int oy, int ix, int iy, int filter, STBIR__V_FIRST_INFO * v_info )92{93 float h_scale=(float)ox/(float)(ix);94 float v_scale=(float)oy/(float)(iy);95 stbir__support_callback * support = stbir__builtin_supports[filter];96 int vertical_filter_width = stbir__get_filter_pixel_width(support,v_scale,0);97 int vertical_gather = ( v_scale >= ( 1.0f - stbir__small_float ) ) || ( vertical_filter_width <= STBIR_FORCE_GATHER_FILTER_SCANLINES_AMOUNT );9899 return stbir__should_do_vertical_first( weights_table, stbir__get_filter_pixel_width(support,h_scale,0), h_scale, ox, vertical_filter_width, v_scale, oy, vertical_gather, v_info );100} 101102#define STB_IMAGE_WRITE_IMPLEMENTATION103#include "stb_image_write.h"104105static void alloc_bitmap()106{107 int findex;108 int x = 0, y = 0;109 int w = 0, h = 0;110111 for( findex = 0 ; findex < numfileinfo ; findex++ )112 {113 int nx, ny;114 int thisw, thish;115116 thisw = ( fi[findex].dimensionx * fi[findex].numtypes ) + ( fi[findex].numtypes - 1 );117 thish = ( fi[findex].dimensiony * fi[findex].numinputrects ) + ( fi[findex].numinputrects - 1 );118119 for(;;)120 {121 nx = x + ((x)?4:0) + thisw;122 ny = y + ((y)?4:0) + thish;123 if ( ( nx <= 3600 ) || ( x == 0 ) )124 { 125 fi[findex].bitmapx = x + ((x)?4:0);126 fi[findex].bitmapy = y + ((y)?4:0);127 x = nx;128 if ( x > w ) w = x;129 if ( ny > h ) h = ny;130 break;131 }132 else133 {134 x = 0;135 y = h;136 }137 }138 }139140 w = (w+3) & ~3;141 bitmapw = w;142 bitmaph = h;143 bitmapp = w * 3; // RGB144 bitmap = malloc( bitmapp * bitmaph );145146 memset( bitmap, 0, bitmapp * bitmaph );147}148149static void build_bitmap( float weights[STBIR_RESIZE_CLASSIFICATIONS][4], int do_channel_count_index, int findex )150{151 static int colors[STBIR_RESIZE_CLASSIFICATIONS];152 STBIR__V_FIRST_INFO v_info = {0};153154 int * ts;155 int ir;156 unsigned char * bitm = bitmap + ( fi[findex].bitmapx*3 ) + ( fi[findex].bitmapy*bitmapp) ;157158 for( ir = 0; ir < STBIR_RESIZE_CLASSIFICATIONS ; ir++ ) colors[ ir ] = 127*ir/STBIR_RESIZE_CLASSIFICATIONS+128;159160 ts = fi[findex].timings;161162 for( ir = 0 ; ir < fi[findex].numinputrects ; ir++ )163 {164 int ix, iy, chanind;165 ix = fi[findex].inputrects[ir*2];166 iy = fi[findex].inputrects[ir*2+1];167168 for( chanind = 0 ; chanind < fi[findex].numtypes ; chanind++ )169 {170 int ofs, h, hh;171172 // just do the type that we're on173 if ( chanind != do_channel_count_index )174 {175 ts += 2 * fi[findex].dimensionx * fi[findex].dimensiony;176 continue;177 }178179 // bitmap offset180 ofs=chanind*(fi[findex].dimensionx+1)*3+ir*(fi[findex].dimensiony+1)*bitmapp;181182 h = 1;183 for( hh = 0 ; hh < fi[findex].dimensiony; hh++ )184 {185 int ww, w = 1;186 for( ww = 0 ; ww < fi[findex].dimensionx; ww++ )187 {188 int good, v_first, VF, HF;189190 VF = ts[0];191 HF = ts[1];192193 v_first = vert_first( weights, w, h, ix, iy, STBIR_FILTER_MITCHELL, &v_info );194195 good = ( ((HF<=VF) && (!v_first)) || ((VF<=HF) && (v_first)));196197 if ( good )198 {199 bitm[ofs+2] = 0;200 bitm[ofs+1] = (unsigned char)colors[v_info.v_resize_classification];201 }202 else203 {204 double r;205206 if ( HF < VF )207 r = (double)(VF-HF)/(double)HF;208 else209 r = (double)(HF-VF)/(double)VF;210 211 if ( r > 0.4f) r = 0.4;212 r *= 1.0f/0.4f; 213214 bitm[ofs+2] = (char)(255.0f*r);215 bitm[ofs+1] = (char)(((float)colors[v_info.v_resize_classification])*(1.0f-r));216 }217 bitm[ofs] = 0;218219 ofs += 3;220 ts += 2;221 w += fi[findex].outputscalex;222 }223 ofs += bitmapp - fi[findex].dimensionx*3;224 h += fi[findex].outputscaley;225 }226 }227 }228}229230static void build_comp_bitmap( float weights[STBIR_RESIZE_CLASSIFICATIONS][4], int do_channel_count_index )231{232 int * ts0;233 int * ts1;234 int ir;235 unsigned char * bitm = bitmap + ( fi[0].bitmapx*3 ) + ( fi[0].bitmapy*bitmapp) ;236237 ts0 = fi[0].timings;238 ts1 = fi[1].timings;239240 for( ir = 0 ; ir < fi[0].numinputrects ; ir++ )241 {242 int ix, iy, chanind;243 ix = fi[0].inputrects[ir*2];244 iy = fi[0].inputrects[ir*2+1];245246 for( chanind = 0 ; chanind < fi[0].numtypes ; chanind++ )247 {248 int ofs, h, hh;249250 // just do the type that we're on251 if ( chanind != do_channel_count_index )252 {253 ts0 += 2 * fi[0].dimensionx * fi[0].dimensiony;254 ts1 += 2 * fi[0].dimensionx * fi[0].dimensiony;255 continue;256 }257258 // bitmap offset259 ofs=chanind*(fi[0].dimensionx+1)*3+ir*(fi[0].dimensiony+1)*bitmapp;260261 h = 1;262 for( hh = 0 ; hh < fi[0].dimensiony; hh++ )263 {264 int ww, w = 1;265 for( ww = 0 ; ww < fi[0].dimensionx; ww++ )266 {267 int v_first, time0, time1;268269 v_first = vert_first( weights, w, h, ix, iy, STBIR_FILTER_MITCHELL, 0 );270271 time0 = ( v_first ) ? ts0[0] : ts0[1];272 time1 = ( v_first ) ? ts1[0] : ts1[1];273274 if ( time0 < time1 )275 {276 double r = (double)(time1-time0)/(double)time0;277 if ( r > 0.4f) r = 0.4;278 r *= 1.0f/0.4f; 279 bitm[ofs+2] = 0;280 bitm[ofs+1] = (char)(255.0f*r);281 bitm[ofs] = (char)(64.0f*(1.0f-r));282 }283 else284 {285 double r = (double)(time0-time1)/(double)time1;286 if ( r > 0.4f) r = 0.4;287 r *= 1.0f/0.4f; 288 bitm[ofs+2] = (char)(255.0f*r);289 bitm[ofs+1] = 0;290 bitm[ofs] = (char)(64.0f*(1.0f-r));291 }292 ofs += 3;293 ts0 += 2;294 ts1 += 2;295 w += fi[0].outputscalex;296 }297 ofs += bitmapp - fi[0].dimensionx*3;298 h += fi[0].outputscaley;299 }300 }301 }302}303304static void write_bitmap()305{306 stbi_write_png( "results.png", bitmapp / 3, bitmaph, 3|STB_IMAGE_BGR, bitmap, bitmapp );307}308309310static void calc_errors( float weights_table[STBIR_RESIZE_CLASSIFICATIONS][4], int * curtot, double * curerr, int do_channel_count_index )311{312 int th, findex;313 STBIR__V_FIRST_INFO v_info = {0};314315 for(th=0;th<STBIR_RESIZE_CLASSIFICATIONS;th++)316 {317 curerr[th]=0;318 curtot[th]=0;319 }320321 for( findex = 0 ; findex < numfileinfo ; findex++ )322 {323 int * ts;324 int ir;325 ts = fi[findex].timings;326327 for( ir = 0 ; ir < fi[findex].numinputrects ; ir++ )328 {329 int ix, iy, chanind;330 ix = fi[findex].inputrects[ir*2];331 iy = fi[findex].inputrects[ir*2+1];332333 for( chanind = 0 ; chanind < fi[findex].numtypes ; chanind++ )334 {335 int h, hh;336337 // just do the type that we're on338 if ( chanind != do_channel_count_index )339 {340 ts += 2 * fi[findex].dimensionx * fi[findex].dimensiony;341 continue;342 }343344 h = 1;345 for( hh = 0 ; hh < fi[findex].dimensiony; hh++ )346 {347 int ww, w = 1;348 for( ww = 0 ; ww < fi[findex].dimensionx; ww++ )349 {350 int good, v_first, VF, HF;351352 VF = ts[0];353 HF = ts[1];354355 v_first = vert_first( weights_table, w, h, ix, iy, STBIR_FILTER_MITCHELL, &v_info );356357 good = ( ((HF<=VF) && (!v_first)) || ((VF<=HF) && (v_first)));358359 if ( !good )360 {361 double diff;362 if ( VF < HF )363 diff = ((double)HF-(double)VF) * fi[findex].scale_time;364 else365 diff = ((double)VF-(double)HF) * fi[findex].scale_time;366367 curtot[v_info.v_resize_classification] += 1;368 curerr[v_info.v_resize_classification] += diff;369 }370371 ts += 2;372 w += fi[findex].outputscalex;373 }374 h += fi[findex].outputscaley;375 }376 }377 }378 }379}380381#define TRIESPERWEIGHT 32382#define MAXRANGE ((TRIESPERWEIGHT+1) * (TRIESPERWEIGHT+1) * (TRIESPERWEIGHT+1) * (TRIESPERWEIGHT+1) - 1)383384static void expand_to_floats( float * weights, int range )385{386 weights[0] = (float)( range % (TRIESPERWEIGHT+1) ) / (float)TRIESPERWEIGHT;387 weights[1] = (float)( range/(TRIESPERWEIGHT+1) % (TRIESPERWEIGHT+1) ) / (float)TRIESPERWEIGHT;388 weights[2] = (float)( range/(TRIESPERWEIGHT+1)/(TRIESPERWEIGHT+1) % (TRIESPERWEIGHT+1) ) / (float)TRIESPERWEIGHT;389 weights[3] = (float)( range/(TRIESPERWEIGHT+1)/(TRIESPERWEIGHT+1)/(TRIESPERWEIGHT+1) % (TRIESPERWEIGHT+1) ) / (float)TRIESPERWEIGHT;390}391392static char const * expand_to_string( int range )393{394 static char str[128];395 int w0,w1,w2,w3;396 w0 = range % (TRIESPERWEIGHT+1);397 w1 = range/(TRIESPERWEIGHT+1) % (TRIESPERWEIGHT+1);398 w2 = range/(TRIESPERWEIGHT+1)/(TRIESPERWEIGHT+1) % (TRIESPERWEIGHT+1);399 w3 = range/(TRIESPERWEIGHT+1)/(TRIESPERWEIGHT+1)/(TRIESPERWEIGHT+1) % (TRIESPERWEIGHT+1);400 sprintf( str, "[ %2d/%d %2d/%d %2d/%d %2d/%d ]",w0,TRIESPERWEIGHT,w1,TRIESPERWEIGHT,w2,TRIESPERWEIGHT,w3,TRIESPERWEIGHT );401 return str;402}403404static void print_weights( float weights[STBIR_RESIZE_CLASSIFICATIONS][4], int channel_count_index, int * tots, double * errs )405{406 int th;407 printf("ChInd: %d Weights:\n",channel_count_index);408 for(th=0;th<STBIR_RESIZE_CLASSIFICATIONS;th++)409 {410 float * w = weights[th];411 printf(" %d: [%1.5f %1.5f %1.5f %1.5f] (%d %.4f)\n",th, w[0], w[1], w[2], w[3], tots[th], errs[th] );412 }413 printf("\n");414}415416static int windowranges[ 16 ];417static int windowstatus = 0;418static DWORD trainstart = 0;419420static void opt_channel( float best_output_weights[STBIR_RESIZE_CLASSIFICATIONS][4], int channel_count_index )421{422 int newbest = 0;423 float weights[STBIR_RESIZE_CLASSIFICATIONS][4] = {0};424 double besterr[STBIR_RESIZE_CLASSIFICATIONS];425 int besttot[STBIR_RESIZE_CLASSIFICATIONS];426 int best[STBIR_RESIZE_CLASSIFICATIONS]={0};427428 double curerr[STBIR_RESIZE_CLASSIFICATIONS];429 int curtot[STBIR_RESIZE_CLASSIFICATIONS];430 int th, range;431 DWORD lasttick = 0;432433 for(th=0;th<STBIR_RESIZE_CLASSIFICATIONS;th++) 434 {435 besterr[th]=1000000000000.0;436 besttot[th]=0x7fffffff;437 }438439 newbest = 0;440441 // try the whole range 442 range = MAXRANGE;443 do444 {445 for(th=0;th<STBIR_RESIZE_CLASSIFICATIONS;th++)446 expand_to_floats( weights[th], range );447448 calc_errors( weights, curtot, curerr, channel_count_index );449450 for(th=0;th<STBIR_RESIZE_CLASSIFICATIONS;th++)451 {452 if ( curerr[th] < besterr[th] )453 {454 besterr[th] = curerr[th];455 besttot[th] = curtot[th];456 best[th] = range;457 expand_to_floats( best_output_weights[th], best[th] );458 newbest = 1;459 }460 }461462 {463 DWORD t = GetTickCount();464 if ( range == 0 )465 goto do_bitmap;466467 if ( newbest )468 {469 if ( ( GetTickCount() - lasttick ) > 200 )470 {471 int findex;472 473 do_bitmap:474 lasttick = t;475 newbest = 0;476477 for( findex = 0 ; findex < numfileinfo ; findex++ )478 build_bitmap( best_output_weights, channel_count_index, findex );479480 lasttick = GetTickCount();481 }482 }483 }484 485 windowranges[ channel_count_index ] = range;486487 // advance all the weights and loop488 --range;489 } while( ( range >= 0 ) && ( !windowstatus ) );490491 // if we hit here, then we tried all weights for this opt, so save them 492}493494static void print_struct( float weight[5][STBIR_RESIZE_CLASSIFICATIONS][4], char const * name )495{496 printf("\n\nstatic float %s[5][STBIR_RESIZE_CLASSIFICATIONS][4]=\n{", name );497 {498 int i;499 for(i=0;i<5;i++) 500 { 501 int th;502 for(th=0;th<STBIR_RESIZE_CLASSIFICATIONS;th++)503 {504 int j;505 printf("\n "); 506 for(j=0;j<4;j++) 507 printf("%1.5ff, ", weight[i][th][j] ); 508 }509 printf("\n");510 }511 printf("\n};\n");512 }513}514515static float retrain_weights[5][STBIR_RESIZE_CLASSIFICATIONS][4];516517static DWORD __stdcall retrain_shim( LPVOID p )518{519 int chanind = (int) (size_t)p;520 opt_channel( retrain_weights[chanind], chanind );521 return 0;522}523524static char const * gettime( int ms )525{526 static char time[32];527 if (ms > 60000)528 sprintf( time, "%dm %ds",ms/60000, (ms/1000)%60 );529 else 530 sprintf( time, "%ds",ms/1000 );531 return time;532}533534static BITMAPINFOHEADER bmiHeader;535static DWORD extrawindoww, extrawindowh;536static HINSTANCE instance;537static int curzoom = 1;538539static LRESULT WINAPI WindowProc( HWND window,540 UINT message,541 WPARAM wparam,542 LPARAM lparam )543{544 switch( message )545 {546 case WM_CHAR:547 if ( wparam != 27 )548 break;549 // falls through550551 case WM_CLOSE:552 {553 int i;554 int max = 0;555 556 for( i = 0 ; i < fi[0].numtypes ; i++ )557 if( windowranges[i] > max ) max = windowranges[i];558 559 if ( ( max == 0 ) || ( MessageBox( window, "Cancel before training is finished?", "Vertical First Training", MB_OKCANCEL|MB_ICONSTOP ) == IDOK ) )560 {561 for( i = 0 ; i < fi[0].numtypes ; i++ )562 if( windowranges[i] > max ) max = windowranges[i];563 if ( max )564 windowstatus = 1;565 DestroyWindow( window );566 }567 }568 return 0;569 570 case WM_PAINT:571 {572 PAINTSTRUCT ps;573 HDC dc;574575 dc = BeginPaint( window, &ps );576 StretchDIBits( dc, 577 0, 0, bitmapw*curzoom, bitmaph*curzoom,578 0, 0, bitmapw, bitmaph,579 bitmap, (BITMAPINFO*)&bmiHeader, DIB_RGB_COLORS, SRCCOPY );580581 PatBlt( dc, bitmapw*curzoom, 0, 4096, 4096, WHITENESS );582 PatBlt( dc, 0, bitmaph*curzoom, 4096, 4096, WHITENESS );583584 SetTextColor( dc, RGB(0,0,0) );585 SetBkColor( dc, RGB(255,255,255) );586 SetBkMode( dc, OPAQUE );587588 {589 int i, l = 0, max = 0;590 char buf[1024];591 RECT rc;592 POINT p;593594 for( i = 0 ; i < fi[0].numtypes ; i++ )595 {596 l += sprintf( buf + l, "channels: %d %s\n", fi[0].effective[i], windowranges[i] ? expand_to_string( windowranges[i] ) : "Done." );597 if ( windowranges[i] > max ) max = windowranges[i];598 }599600 rc.left = 32; rc.top = bitmaph*curzoom+10;601 rc.right = 512; rc.bottom = rc.top + 512;602 DrawText( dc, buf, -1, &rc, DT_TOP );603604 l = 0;605 if ( max == 0 )606 {607 static DWORD traindone = 0;608 if ( traindone == 0 ) traindone = GetTickCount();609 l = sprintf( buf, "Finished in %s.", gettime( traindone - trainstart ) );610 }611 else if ( max != MAXRANGE )612 l = sprintf( buf, "Done in %s...", gettime( (int) ( ( ( (int64)max * ( (int64)GetTickCount() - (int64)trainstart ) ) ) / (int64) ( MAXRANGE - max ) ) ) );613614 GetCursorPos( &p );615 ScreenToClient( window, &p );616617 if ( ( p.x >= 0 ) && ( p.y >= 0 ) && ( p.x < (bitmapw*curzoom) ) && ( p.y < (bitmaph*curzoom) ) )618 {619 int findex;620 int x, y, w, h, sx, sy, ix, iy, ox, oy;621 int ir, chanind;622 int * ts;623 char badstr[64];624 STBIR__V_FIRST_INFO v_info={0};625626 p.x /= curzoom;627 p.y /= curzoom;628629 for( findex = 0 ; findex < numfileinfo ; findex++ )630 {631 x = fi[findex].bitmapx;632 y = fi[findex].bitmapy;633 w = x + ( fi[findex].dimensionx + 1 ) * fi[findex].numtypes;634 h = y + ( fi[findex].dimensiony + 1 ) * fi[findex].numinputrects;635636 if ( ( p.x >= x ) && ( p.y >= y ) && ( p.x < w ) && ( p.y < h ) )637 goto found;638 }639 goto nope;640 641 found:642 643 ir = ( p.y - y ) / ( fi[findex].dimensiony + 1 );644 sy = ( p.y - y ) % ( fi[findex].dimensiony + 1 );645 if ( sy >= fi[findex].dimensiony ) goto nope;646647 chanind = ( p.x - x ) / ( fi[findex].dimensionx + 1 );648 sx = ( p.x - x ) % ( fi[findex].dimensionx + 1 );649 if ( sx >= fi[findex].dimensionx ) goto nope;650651 ix = fi[findex].inputrects[ir*2];652 iy = fi[findex].inputrects[ir*2+1];653654 ts = fi[findex].timings + ( ( fi[findex].dimensionx * fi[findex].dimensiony * fi[findex].numtypes * ir ) + ( fi[findex].dimensionx * fi[findex].dimensiony * chanind ) + ( fi[findex].dimensionx * sy ) + sx ) * 2;655656 ox = 1+fi[findex].outputscalex*sx;657 oy = 1+fi[findex].outputscaley*sy;658659 if ( windowstatus != 2 )660 {661 int VF, HF, v_first, good;662 VF = ts[0];663 HF = ts[1];664665 v_first = vert_first( retrain_weights[chanind], ox, oy, ix, iy, STBIR_FILTER_MITCHELL, &v_info );666667 good = ( ((HF<=VF) && (!v_first)) || ((VF<=HF) && (v_first)));668669 if ( good )670 badstr[0] = 0;671 else672 {673 double r;674675 if ( HF < VF )676 r = (double)(VF-HF)/(double)HF;677 else678 r = (double)(HF-VF)/(double)VF;679 sprintf( badstr, " %.1f%% off", r*100 );680 }681 sprintf( buf + l, "\n\n%s\nCh: %d Resize: %dx%d to %dx%d\nV: %d H: %d Order: %c (%s%s)\nClass: %d Scale: %.2f %s", fi[findex].filename,fi[findex].effective[chanind], ix,iy,ox,oy, VF, HF, v_first?'V':'H', good?"Good":"Wrong", badstr, v_info.v_resize_classification, (double)oy/(double)iy, v_info.is_gather ? "Gather" : "Scatter" );682 }683 else684 {685 int v_first, time0, time1;686 float (* weights)[4] = stbir__compute_weights[chanind];687 int * ts1;688 char b0[32], b1[32];689690 ts1 = fi[1].timings + ( ts - fi[0].timings );691692 v_first = vert_first( weights, ox, oy, ix, iy, STBIR_FILTER_MITCHELL, &v_info );693694 time0 = ( v_first ) ? ts[0] : ts[1];695 time1 = ( v_first ) ? ts1[0] : ts1[1];696 697 b0[0] = b1[0] = 0;698 if ( time0 < time1 )699 sprintf( b0," (%.f%% better)", ((double)time1-(double)time0)*100.0f/(double)time0);700 else701 sprintf( b1," (%.f%% better)", ((double)time0-(double)time1)*100.0f/(double)time1);702703 sprintf( buf + l, "\n\n0: %s\n1: %s\nCh: %d Resize: %dx%d to %dx%d\nClass: %d Scale: %.2f %s\nTime0: %d%s\nTime1: %d%s", fi[0].filename, fi[1].filename, fi[0].effective[chanind], ix,iy,ox,oy, v_info.v_resize_classification, (double)oy/(double)iy, v_info.is_gather ? "Gather" : "Scatter", time0, b0, time1, b1 );704 }705 }706 nope:707708 rc.left = 32+320; rc.right = 512+320; 709 SetTextColor( dc, RGB(0,0,128) );710 DrawText( dc, buf, -1, &rc, DT_TOP );711712 }713 EndPaint( window, &ps );714 return 0;715 }716717 case WM_TIMER:718 InvalidateRect( window, 0, 0 );719 return 0;720721 case WM_DESTROY:722 PostQuitMessage( 0 );723 return 0;724 }725 726727 return DefWindowProc( window, message, wparam, lparam );728}729730static void SetHighDPI(void)731{732 typedef HRESULT WINAPI setdpitype(int v);733 HMODULE h=LoadLibrary("Shcore.dll");734 if (h)735 {736 setdpitype * sd = (setdpitype*)GetProcAddress(h,"SetProcessDpiAwareness");737 if (sd )738 sd(1);739 }740} 741742static void draw_window()743{744 WNDCLASS wc;745 HWND w;746 MSG msg;747 748 instance = GetModuleHandle(NULL);749750 wc.style = 0;751 wc.lpfnWndProc = WindowProc;752 wc.cbClsExtra = 0;753 wc.cbWndExtra = 0;754 wc.hInstance = instance;755 wc.hIcon = 0;756 wc.hCursor = LoadCursor(NULL, IDC_ARROW);757 wc.hbrBackground = 0;758 wc.lpszMenuName = 0;759 wc.lpszClassName = "WHTrain";760761 if ( !RegisterClass( &wc ) )762 exit(1);763764 SetHighDPI();765766 bmiHeader.biSize = sizeof(BITMAPINFOHEADER);767 bmiHeader.biWidth = bitmapp/3;768 bmiHeader.biHeight = -bitmaph;769 bmiHeader.biPlanes = 1;770 bmiHeader.biBitCount = 24;771 bmiHeader.biCompression = BI_RGB;772773 w = CreateWindow( "WHTrain",774 "Vertical First Training",775 WS_CAPTION | WS_POPUP| WS_CLIPCHILDREN |776 WS_SYSMENU | WS_MINIMIZEBOX | WS_SIZEBOX,777 CW_USEDEFAULT,CW_USEDEFAULT,778 CW_USEDEFAULT,CW_USEDEFAULT,779 0, 0, instance, 0 );780781 {782 RECT r, c;783 GetWindowRect( w, &r );784 GetClientRect( w, &c );785 extrawindoww = ( r.right - r.left ) - ( c.right - c.left );786 extrawindowh = ( r.bottom - r.top ) - ( c.bottom - c.top );787 SetWindowPos( w, 0, 0, 0, bitmapw * curzoom + extrawindoww, bitmaph * curzoom + extrawindowh + 164, SWP_NOMOVE );788 }789790 ShowWindow( w, SW_SHOWNORMAL );791 SetTimer( w, 1, 250, 0 );792793 {794 BOOL ret;795 while( ( ret = GetMessage( &msg, w, 0, 0 ) ) != 0 )796 { 797 if ( ret == -1 )798 break;799 TranslateMessage( &msg ); 800 DispatchMessage( &msg ); 801 }802 }803}804805static void retrain()806{807 HANDLE threads[ 16 ];808 int chanind;809810 trainstart = GetTickCount();811 for( chanind = 0 ; chanind < fi[0].numtypes ; chanind++ )812 threads[ chanind ] = CreateThread( 0, 2048*1024, retrain_shim, (LPVOID)(size_t)chanind, 0, 0 );813814 draw_window();815816 for( chanind = 0 ; chanind < fi[0].numtypes ; chanind++ )817 {818 WaitForSingleObject( threads[ chanind ], INFINITE );819 CloseHandle( threads[ chanind ] );820 }821822 write_bitmap();823824 print_struct( retrain_weights, "retained_weights" );825 if ( windowstatus ) printf( "CANCELLED!\n" );826}827828static void info()829{830 int findex;831832 // display info about each input file833 for( findex = 0 ; findex < numfileinfo ; findex++ )834 {835 int i, h,m,s;836 if ( findex ) printf( "\n" );837 printf( "Timing file: %s\n", fi[findex].filename );838 printf( "CPU type: %d %s\n", fi[findex].cpu, fi[findex].simd?(fi[findex].simd==2?"SIMD8":"SIMD4"):"Scalar" );839 h = fi[findex].milliseconds/3600000;840 m = (fi[findex].milliseconds-h*3600000)/60000;841 s = (fi[findex].milliseconds-h*3600000-m*60000)/1000;842 printf( "Total time in test: %dh %dm %ds Cycles/sec: %.f\n", h,m,s, 1000.0/fi[findex].scale_time );843 printf( "Each tile of samples is %dx%d, and is scaled by %dx%d.\n", fi[findex].dimensionx,fi[findex].dimensiony, fi[findex].outputscalex,fi[findex].outputscaley );844 printf( "So the x coords are: " );845 for( i=0; i < fi[findex].dimensionx ; i++ ) printf( "%d ",1+i*fi[findex].outputscalex );846 printf( "\n" );847 printf( "And the y coords are: " );848 for( i=0; i < fi[findex].dimensiony ; i++ ) printf( "%d ",1+i*fi[findex].outputscaley );849 printf( "\n" );850 printf( "There are %d channel counts and they are: ", fi[findex].numtypes );851 for( i=0; i < fi[findex].numtypes ; i++ ) printf( "%d ",fi[findex].effective[i] );852 printf( "\n" );853 printf( "There are %d input rect sizes and they are: ", fi[findex].numinputrects );854 for( i=0; i < fi[findex].numtypes ; i++ ) printf( "%dx%d ",fi[findex].inputrects[i*2],fi[findex].inputrects[i*2+1] );855 printf( "\n" );856 }857}858859static void current( int do_win, int do_bitmap )860{861 int i, findex;862863 trainstart = GetTickCount();864865 // clear progress866 memset( windowranges, 0, sizeof( windowranges ) );867 // copy in appropriate weights868 memcpy( retrain_weights, stbir__compute_weights, sizeof( retrain_weights ) );869870 // build and print current errors and build current bitmap871 for( i = 0 ; i < fi[0].numtypes ; i++ )872 {873 double curerr[STBIR_RESIZE_CLASSIFICATIONS];874 int curtot[STBIR_RESIZE_CLASSIFICATIONS];875 float (* weights)[4] = retrain_weights[i];876 877 calc_errors( weights, curtot, curerr, i );878 if ( !do_bitmap )879 print_weights( weights, i, curtot, curerr );880881 for( findex = 0 ; findex < numfileinfo ; findex++ )882 build_bitmap( weights, i, findex );883 }884885 if ( do_win )886 draw_window();887888 if ( do_bitmap )889 write_bitmap();890}891892static void compare()893{894 int i;895896 trainstart = GetTickCount();897 windowstatus = 2; // comp mode898899 // clear progress900 memset( windowranges, 0, sizeof( windowranges ) );901902 if ( ( fi[0].numtypes != fi[1].numtypes ) || ( fi[0].numinputrects != fi[1].numinputrects ) ||903 ( fi[0].dimensionx != fi[1].dimensionx ) || ( fi[0].dimensiony != fi[1].dimensiony ) || 904 ( fi[0].outputscalex != fi[1].outputscalex ) || ( fi[0].outputscaley != fi[1].outputscaley ) )905 {906 err:907 printf( "Timing files don't match.\n" );908 exit(5);909 }910911 for( i=0; i < fi[0].numtypes ; i++ )912 {913 if ( fi[0].effective[i] != fi[1].effective[i] ) goto err;914 if ( fi[0].inputrects[i*2] != fi[1].inputrects[i*2] ) goto err;915 if ( fi[0].inputrects[i*2+1] != fi[1].inputrects[i*2+1] ) goto err;916 }917 918 alloc_bitmap( 1 );919 920 for( i = 0 ; i < fi[0].numtypes ; i++ )921 {922 float (* weights)[4] = stbir__compute_weights[i];923 build_comp_bitmap( weights, i );924 }925926 draw_window();927}928929static void load_files( char ** args, int count )930{931 int i;932933 if ( count == 0 )934 {935 printf( "No timing files listed!" );936 exit(3);937 }938939 for ( i = 0 ; i < count ; i++ )940 {941 if ( !use_timing_file( args[i], i ) )942 {943 printf( "Bad timing file %s\n", args[i] );944 exit(2);945 }946 }947 numfileinfo = count;948} 949950int main( int argc, char ** argv )951{952 int check;953 if ( argc < 3 )954 {955 err:956 printf( "vf_train retrain [timing_filenames....] - recalcs weights for all the files on the command line.\n");957 printf( "vf_train info [timing_filenames....] - shows info about each timing file.\n");958 printf( "vf_train check [timing_filenames...] - show results for the current weights for all files listed.\n");959 printf( "vf_train compare <timing file1> <timing file2> - compare two timing files (must only be two files and same resolution).\n");960 printf( "vf_train bitmap [timing_filenames...] - write out results.png, comparing against the current weights for all files listed.\n");961 exit(1);962 }963 964 check = ( strcmp( argv[1], "check" ) == 0 );965 if ( ( check ) || ( strcmp( argv[1], "bitmap" ) == 0 ) )966 {967 load_files( argv + 2, argc - 2 );968 alloc_bitmap( numfileinfo );969 current( check, !check );970 }971 else if ( strcmp( argv[1], "info" ) == 0 ) 972 {973 load_files( argv + 2, argc - 2 );974 info();975 }976 else if ( strcmp( argv[1], "compare" ) == 0 ) 977 {978 if ( argc != 4 )979 {980 printf( "You must specify two files to compare.\n" );981 exit(4);982 }983984 load_files( argv + 2, argc - 2 );985 compare();986 }987 else if ( strcmp( argv[1], "retrain" ) == 0 ) 988 {989 load_files( argv + 2, argc - 2 );990 alloc_bitmap( numfileinfo );991 retrain(); 992 }993 else994 {995 goto err;996 }997998 return 0;999}
modified:tests/resample_test.cpp+9 added-4 removed
62 unmodified lines
6364#define STBIR_PROGRESS_REPORT stbir_progress65#define STB_IMAGE_RESIZE_IMPLEMENTATION66#define STB_IMAGE_RESIZE_STATIC67#include "stb_image_resize.h"67#include "stb_image_resize2.h"6869#define STB_IMAGE_WRITE_IMPLEMENTATION70#include "stb_image_write.h"71
70 unmodified lines
142 out_w = w*3;143 out_h = h*3;144 output_pixels = (unsigned char*) malloc(out_w*out_h*n);145 //stbir_resize_uint8_srgb(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, n, -1,0);146 stbir_resize_uint8(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, n);146 stbir_resize_uint8_linear(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, (stbir_pixel_layout) n);147 stbi_write_png("output.png", out_w, out_h, n, output_pixels, 0);148 exit(0);149}150
19 unmodified lines
170171 output_pixels = (unsigned char*) malloc(out_w*out_h*n);172 for (i=0; i < count; ++i)173 if (srgb)174 stbir_resize_uint8_srgb(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, n,-1,0);174 stbir_resize_uint8_srgb(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, (stbir_pixel_layout) n);175 else176 stbir_resize(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, STBIR_TYPE_UINT8, n,-1, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT, STBIR_COLORSPACE_LINEAR, NULL);176 stbir_resize_uint8_linear(input_pixels, w, h, 0, output_pixels, out_w, out_h, 0, (stbir_pixel_layout) n);177 exit(0);178}179180void test_suite(int argc, char **argv);
6 unmodified lines
187 test_suite(argc, argv);188 return 0;189}190191#if 0192void resize_image(const char* filename, float width_percent, float height_percent, stbir_filter filter, stbir_edge edge, stbir_colorspace colorspace, const char* output_filename)193{194 int w, h, n;195
924 unmodified lines
1120 resize_image("gamma_colors.jpg", .5f, .5f, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_SRGB, "test-output/gamma_colors.jpg");1121 resize_image("gamma_2.2.jpg", .5f, .5f, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_SRGB, "test-output/gamma_2.2.jpg");1122 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");1123}1124#endif1125void test_suite(int argc, char **argv)1126{1127}
modified:tests/resize.dsp+1 added-1 removed
86 unmodified lines
87SOURCE=.\resample_test.cpp88# End Source File89# Begin Source File9091SOURCE=..\stb_image_resize.h91SOURCE=..\stb_image_resize2.h92# End Source File93# End Target94# End Project
modified:tests/stb.dsp-4 removed
128 unmodified lines
129SOURCE=..\stb_image.h130# End Source File131# Begin Source File132133SOURCE=..\stb_image_resize.h134# End Source File135# Begin Source File136133SOURCE=..\stb_image_write.h134# End Source File135# Begin Source File136
104 unmodified lines
modified:tests/test_c_compilation.c+3 added-2 removed
1#define STB_IMAGE_RESIZE_IMPLEMENTATION2#include "stb_image_resize2.h"34#define STB_SPRINTF_IMPLEMENTATION5#include "stb_sprintf.h"67#define STB_PERLIN_IMPLEMENTATION
1 unmodified line
9#define STB_C_LEXER_IMPLEMENTATIOn10#define STB_DIVIDE_IMPLEMENTATION11#define STB_IMAGE_IMPLEMENTATION12#define STB_HERRINGBONE_WANG_TILE_IMEPLEMENTATIOn10#define STB_IMAGE_RESIZE_IMPLEMENTATION13#define STB_RECT_PACK_IMPLEMENTATION14#define STB_VOXEL_RENDER_IMPLEMENTATION15#define STB_EASY_FONT_IMPLEMENTATION16#define STB_DXT_IMPLEMENTATION
4 unmodified lines
21#include "stb_image_write.h"22#include "stb_perlin.h"23#include "stb_c_lexer.h"24#include "stb_divide.h"23#include "stb_image_resize.h"25#include "stb_rect_pack.h"26#include "stb_dxt.h"27#include "stb_include.h"28
22 unmodified lines
modified:tests/test_cpp_compilation.cpp+1 added-1 removed
68 unmodified lines
69#define STB_LEAKCHECK_IMPLEMENTATION70#include "stb_leakcheck.h"7172#define STB_IMAGE_RESIZE_IMPLEMENTATION73#include "stb_image_resize.h"73#include "stb_image_resize2.h"7475//#include "stretchy_buffer.h" // deprecating7677
109 unmodified lines
modified:tools/README.list+1 added-1 removed
1 unmodified line
2stb_hexwave.h | audio | audio waveform synthesizer3stb_image.h | graphics | image loading/decoding from file/memory: JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC4stb_truetype.h | graphics | parse, decode, and rasterize characters from truetype fonts5stb_image_write.h | graphics | image writing to disk: PNG, TGA, BMP6stb_image_resize.h | graphics | resize images larger/smaller with good quality6stb_image_resize2.h | graphics | resize images larger/smaller with good quality7stb_rect_pack.h | graphics | simple 2D rectangle packer with decent quality8stb_perlin.h | graphics | perlin's revised simplex noise w/ different seeds9stb_ds.h | utility | typesafe dynamic array and hash tables for C, will compile in C++10stb_sprintf.h | utility | fast sprintf, snprintf for C/C++
11 unmodified lines
modified:README.md+2 added-2 removed
deleted:stb_image_resize.h-2634 removed
1/* stb_image_resize - v0.97 - 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.97 (2020-02-02) fixed warning163 0.96 (2019-03-04) fixed warnings164 0.95 (2017-07-23) fixed warnings165 0.94 (2017-03-18) fixed warnings166 0.93 (2017-03-03) fixed bug with certain combinations of heights167 0.92 (2017-01-02) fix integer overflow on large (>2GB) images168 0.91 (2016-04-02) fix warnings; fix handling of subpixel regions169 0.90 (2014-09-17) first released version170171 LICENSE172 See end of file for license information.173174 TODO175 Don't decode all of the image data when only processing a partial tile176 Don't use full-width decode buffers when only processing a partial tile177 When processing wide images, break processing into tiles so data fits in L1 cache178 Installable filters?179 Resize that respects alpha test coverage180 (Reference code: FloatImage::alphaTestCoverage and FloatImage::scaleAlphaToCoverage:181 https://code.google.com/p/nvidia-texture-tools/source/browse/trunk/src/nvimage/FloatImage.cpp )182*/183184#ifndef STBIR_INCLUDE_STB_IMAGE_RESIZE_H185#define STBIR_INCLUDE_STB_IMAGE_RESIZE_H186187#ifdef _MSC_VER188typedef unsigned char stbir_uint8;189typedef unsigned short stbir_uint16;190typedef unsigned int stbir_uint32;191#else192#include <stdint.h>193typedef uint8_t stbir_uint8;194typedef uint16_t stbir_uint16;195typedef uint32_t stbir_uint32;196#endif197198#ifndef STBIRDEF199#ifdef STB_IMAGE_RESIZE_STATIC200#define STBIRDEF static201#else202#ifdef __cplusplus203#define STBIRDEF extern "C"204#else205#define STBIRDEF extern206#endif207#endif208#endif209210//////////////////////////////////////////////////////////////////////////////211//212// Easy-to-use API:213//214// * "input pixels" points to an array of image data with 'num_channels' channels (e.g. RGB=3, RGBA=4)215// * input_w is input image width (x-axis), input_h is input image height (y-axis)216// * stride is the offset between successive rows of image data in memory, in bytes. you can217// specify 0 to mean packed continuously in memory218// * alpha channel is treated identically to other channels.219// * colorspace is linear or sRGB as specified by function name220// * returned result is 1 for success or 0 in case of an error.221// #define STBIR_ASSERT() to trigger an assert on parameter validation errors.222// * Memory required grows approximately linearly with input and output size, but with223// discontinuities at input_w == output_w and input_h == output_h.224// * These functions use a "default" resampling filter defined at compile time. To change the filter,225// you can change the compile-time defaults by #defining STBIR_DEFAULT_FILTER_UPSAMPLE226// and STBIR_DEFAULT_FILTER_DOWNSAMPLE, or you can use the medium-complexity API.227228STBIRDEF int stbir_resize_uint8( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,229 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,230 int num_channels);231232STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,233 float *output_pixels, int output_w, int output_h, int output_stride_in_bytes,234 int num_channels);235236237// The following functions interpret image data as gamma-corrected sRGB.238// Specify STBIR_ALPHA_CHANNEL_NONE if you have no alpha channel,239// or otherwise provide the index of the alpha channel. Flags value240// of 0 will probably do the right thing if you're not sure what241// the flags mean.242243#define STBIR_ALPHA_CHANNEL_NONE -1244245// Set this flag if your texture has premultiplied alpha. Otherwise, stbir will246// use alpha-weighted resampling (effectively premultiplying, resampling,247// then unpremultiplying).248#define STBIR_FLAG_ALPHA_PREMULTIPLIED (1 << 0)249// The specified alpha channel should be handled as gamma-corrected value even250// when doing sRGB operations.251#define STBIR_FLAG_ALPHA_USES_COLORSPACE (1 << 1)252253STBIRDEF int stbir_resize_uint8_srgb(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,254 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,255 int num_channels, int alpha_channel, int flags);256257258typedef enum259{260 STBIR_EDGE_CLAMP = 1,261 STBIR_EDGE_REFLECT = 2,262 STBIR_EDGE_WRAP = 3,263 STBIR_EDGE_ZERO = 4,264} stbir_edge;265266// This function adds the ability to specify how requests to sample off the edge of the image are handled.267STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,268 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,269 int num_channels, int alpha_channel, int flags,270 stbir_edge edge_wrap_mode);271272//////////////////////////////////////////////////////////////////////////////273//274// Medium-complexity API275//276// This extends the easy-to-use API as follows:277//278// * Alpha-channel can be processed separately279// * If alpha_channel is not STBIR_ALPHA_CHANNEL_NONE280// * Alpha channel will not be gamma corrected (unless flags&STBIR_FLAG_GAMMA_CORRECT)281// * Filters will be weighted by alpha channel (unless flags&STBIR_FLAG_ALPHA_PREMULTIPLIED)282// * Filter can be selected explicitly283// * uint16 image type284// * sRGB colorspace available for all types285// * context parameter for passing to STBIR_MALLOC286287typedef enum288{289 STBIR_FILTER_DEFAULT = 0, // use same filter type that easy-to-use API chooses290 STBIR_FILTER_BOX = 1, // A trapezoid w/1-pixel wide ramps, same result as box for integer scale ratios291 STBIR_FILTER_TRIANGLE = 2, // On upsampling, produces same results as bilinear texture filtering292 STBIR_FILTER_CUBICBSPLINE = 3, // The cubic b-spline (aka Mitchell-Netrevalli with B=1,C=0), gaussian-esque293 STBIR_FILTER_CATMULLROM = 4, // An interpolating cubic spline294 STBIR_FILTER_MITCHELL = 5, // Mitchell-Netrevalli filter with B=1/3, C=1/3295} stbir_filter;296297typedef enum298{299 STBIR_COLORSPACE_LINEAR,300 STBIR_COLORSPACE_SRGB,301302 STBIR_MAX_COLORSPACES,303} stbir_colorspace;304305// The following functions are all identical except for the type of the image data306307STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,308 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,309 int num_channels, int alpha_channel, int flags,310 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,311 void *alloc_context);312313STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int input_w , int input_h , int input_stride_in_bytes,314 stbir_uint16 *output_pixels , int output_w, int output_h, int output_stride_in_bytes,315 int num_channels, int alpha_channel, int flags,316 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,317 void *alloc_context);318319STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,320 float *output_pixels , int output_w, int output_h, int output_stride_in_bytes,321 int num_channels, int alpha_channel, int flags,322 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,323 void *alloc_context);324325326327//////////////////////////////////////////////////////////////////////////////328//329// Full-complexity API330//331// This extends the medium API as follows:332//333// * uint32 image type334// * not typesafe335// * separate filter types for each axis336// * separate edge modes for each axis337// * can specify scale explicitly for subpixel correctness338// * can specify image source tile using texture coordinates339340typedef enum341{342 STBIR_TYPE_UINT8 ,343 STBIR_TYPE_UINT16,344 STBIR_TYPE_UINT32,345 STBIR_TYPE_FLOAT ,346347 STBIR_MAX_TYPES348} stbir_datatype;349350STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,351 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,352 stbir_datatype datatype,353 int num_channels, int alpha_channel, int flags,354 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,355 stbir_filter filter_horizontal, stbir_filter filter_vertical,356 stbir_colorspace space, void *alloc_context);357358STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,359 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,360 stbir_datatype datatype,361 int num_channels, int alpha_channel, int flags,362 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,363 stbir_filter filter_horizontal, stbir_filter filter_vertical,364 stbir_colorspace space, void *alloc_context,365 float x_scale, float y_scale,366 float x_offset, float y_offset);367368STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,369 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,370 stbir_datatype datatype,371 int num_channels, int alpha_channel, int flags,372 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,373 stbir_filter filter_horizontal, stbir_filter filter_vertical,374 stbir_colorspace space, void *alloc_context,375 float s0, float t0, float s1, float t1);376// (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.377378//379//380//// end header file /////////////////////////////////////////////////////381#endif // STBIR_INCLUDE_STB_IMAGE_RESIZE_H382383384385386387#ifdef STB_IMAGE_RESIZE_IMPLEMENTATION388389#ifndef STBIR_ASSERT390#include <assert.h>391#define STBIR_ASSERT(x) assert(x)392#endif393394// For memset395#include <string.h>396397#include <math.h>398399#ifndef STBIR_MALLOC400#include <stdlib.h>401// use comma operator to evaluate c, to avoid "unused parameter" warnings402#define STBIR_MALLOC(size,c) ((void)(c), malloc(size))403#define STBIR_FREE(ptr,c) ((void)(c), free(ptr))404#endif405406#ifndef _MSC_VER407#ifdef __cplusplus408#define stbir__inline inline409#else410#define stbir__inline411#endif412#else413#define stbir__inline __forceinline414#endif415416417// should produce compiler error if size is wrong418typedef unsigned char stbir__validate_uint32[sizeof(stbir_uint32) == 4 ? 1 : -1];419420#ifdef _MSC_VER421#define STBIR__NOTUSED(v) (void)(v)422#else423#define STBIR__NOTUSED(v) (void)sizeof(v)424#endif425426#define STBIR__ARRAY_SIZE(a) (sizeof((a))/sizeof((a)[0]))427428#ifndef STBIR_DEFAULT_FILTER_UPSAMPLE429#define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_CATMULLROM430#endif431432#ifndef STBIR_DEFAULT_FILTER_DOWNSAMPLE433#define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_MITCHELL434#endif435436#ifndef STBIR_PROGRESS_REPORT437#define STBIR_PROGRESS_REPORT(float_0_to_1)438#endif439440#ifndef STBIR_MAX_CHANNELS441#define STBIR_MAX_CHANNELS 64442#endif443444#if STBIR_MAX_CHANNELS > 65536445#error "Too many channels; STBIR_MAX_CHANNELS must be no more than 65536."446// because we store the indices in 16-bit variables447#endif448449// This value is added to alpha just before premultiplication to avoid450// zeroing out color values. It is equivalent to 2^-80. If you don't want451// that behavior (it may interfere if you have floating point images with452// very small alpha values) then you can define STBIR_NO_ALPHA_EPSILON to453// disable it.454#ifndef STBIR_ALPHA_EPSILON455#define STBIR_ALPHA_EPSILON ((float)1 / (1 << 20) / (1 << 20) / (1 << 20) / (1 << 20))456#endif457458459460#ifdef _MSC_VER461#define STBIR__UNUSED_PARAM(v) (void)(v)462#else463#define STBIR__UNUSED_PARAM(v) (void)sizeof(v)464#endif465466// must match stbir_datatype467static unsigned char stbir__type_size[] = {468 1, // STBIR_TYPE_UINT8469 2, // STBIR_TYPE_UINT16470 4, // STBIR_TYPE_UINT32471 4, // STBIR_TYPE_FLOAT472};473474// Kernel function centered at 0475typedef float (stbir__kernel_fn)(float x, float scale);476typedef float (stbir__support_fn)(float scale);477478typedef struct479{480 stbir__kernel_fn* kernel;481 stbir__support_fn* support;482} stbir__filter_info;483484// When upsampling, the contributors are which source pixels contribute.485// When downsampling, the contributors are which destination pixels are contributed to.486typedef struct487{488 int n0; // First contributing pixel489 int n1; // Last contributing pixel490} stbir__contributors;491492typedef struct493{494 const void* input_data;495 int input_w;496 int input_h;497 int input_stride_bytes;498499 void* output_data;500 int output_w;501 int output_h;502 int output_stride_bytes;503504 float s0, t0, s1, t1;505506 float horizontal_shift; // Units: output pixels507 float vertical_shift; // Units: output pixels508 float horizontal_scale;509 float vertical_scale;510511 int channels;512 int alpha_channel;513 stbir_uint32 flags;514 stbir_datatype type;515 stbir_filter horizontal_filter;516 stbir_filter vertical_filter;517 stbir_edge edge_horizontal;518 stbir_edge edge_vertical;519 stbir_colorspace colorspace;520521 stbir__contributors* horizontal_contributors;522 float* horizontal_coefficients;523524 stbir__contributors* vertical_contributors;525 float* vertical_coefficients;526527 int decode_buffer_pixels;528 float* decode_buffer;529530 float* horizontal_buffer;531532 // cache these because ceil/floor are inexplicably showing up in profile533 int horizontal_coefficient_width;534 int vertical_coefficient_width;535 int horizontal_filter_pixel_width;536 int vertical_filter_pixel_width;537 int horizontal_filter_pixel_margin;538 int vertical_filter_pixel_margin;539 int horizontal_num_contributors;540 int vertical_num_contributors;541542 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)543 int ring_buffer_num_entries; // Total number of entries in the ring buffer.544 int ring_buffer_first_scanline;545 int ring_buffer_last_scanline;546 int ring_buffer_begin_index; // first_scanline is at this index in the ring buffer547 float* ring_buffer;548549 float* encode_buffer; // A temporary buffer to store floats so we don't lose precision while we do multiply-adds.550551 int horizontal_contributors_size;552 int horizontal_coefficients_size;553 int vertical_contributors_size;554 int vertical_coefficients_size;555 int decode_buffer_size;556 int horizontal_buffer_size;557 int ring_buffer_size;558 int encode_buffer_size;559} stbir__info;560561562static const float stbir__max_uint8_as_float = 255.0f;563static const float stbir__max_uint16_as_float = 65535.0f;564static const double stbir__max_uint32_as_float = 4294967295.0;565566567static stbir__inline int stbir__min(int a, int b)568{569 return a < b ? a : b;570}571572static stbir__inline float stbir__saturate(float x)573{574 if (x < 0)575 return 0;576577 if (x > 1)578 return 1;579580 return x;581}582583#ifdef STBIR_SATURATE_INT584static stbir__inline stbir_uint8 stbir__saturate8(int x)585{586 if ((unsigned int) x <= 255)587 return x;588589 if (x < 0)590 return 0;591592 return 255;593}594595static stbir__inline stbir_uint16 stbir__saturate16(int x)596{597 if ((unsigned int) x <= 65535)598 return x;599600 if (x < 0)601 return 0;602603 return 65535;604}605#endif606607static float stbir__srgb_uchar_to_linear_float[256] = {608 0.000000f, 0.000304f, 0.000607f, 0.000911f, 0.001214f, 0.001518f, 0.001821f, 0.002125f, 0.002428f, 0.002732f, 0.003035f,609 0.003347f, 0.003677f, 0.004025f, 0.004391f, 0.004777f, 0.005182f, 0.005605f, 0.006049f, 0.006512f, 0.006995f, 0.007499f,610 0.008023f, 0.008568f, 0.009134f, 0.009721f, 0.010330f, 0.010960f, 0.011612f, 0.012286f, 0.012983f, 0.013702f, 0.014444f,611 0.015209f, 0.015996f, 0.016807f, 0.017642f, 0.018500f, 0.019382f, 0.020289f, 0.021219f, 0.022174f, 0.023153f, 0.024158f,612 0.025187f, 0.026241f, 0.027321f, 0.028426f, 0.029557f, 0.030713f, 0.031896f, 0.033105f, 0.034340f, 0.035601f, 0.036889f,613 0.038204f, 0.039546f, 0.040915f, 0.042311f, 0.043735f, 0.045186f, 0.046665f, 0.048172f, 0.049707f, 0.051269f, 0.052861f,614 0.054480f, 0.056128f, 0.057805f, 0.059511f, 0.061246f, 0.063010f, 0.064803f, 0.066626f, 0.068478f, 0.070360f, 0.072272f,615 0.074214f, 0.076185f, 0.078187f, 0.080220f, 0.082283f, 0.084376f, 0.086500f, 0.088656f, 0.090842f, 0.093059f, 0.095307f,616 0.097587f, 0.099899f, 0.102242f, 0.104616f, 0.107023f, 0.109462f, 0.111932f, 0.114435f, 0.116971f, 0.119538f, 0.122139f,617 0.124772f, 0.127438f, 0.130136f, 0.132868f, 0.135633f, 0.138432f, 0.141263f, 0.144128f, 0.147027f, 0.149960f, 0.152926f,618 0.155926f, 0.158961f, 0.162029f, 0.165132f, 0.168269f, 0.171441f, 0.174647f, 0.177888f, 0.181164f, 0.184475f, 0.187821f,619 0.191202f, 0.194618f, 0.198069f, 0.201556f, 0.205079f, 0.208637f, 0.212231f, 0.215861f, 0.219526f, 0.223228f, 0.226966f,620 0.230740f, 0.234551f, 0.238398f, 0.242281f, 0.246201f, 0.250158f, 0.254152f, 0.258183f, 0.262251f, 0.266356f, 0.270498f,621 0.274677f, 0.278894f, 0.283149f, 0.287441f, 0.291771f, 0.296138f, 0.300544f, 0.304987f, 0.309469f, 0.313989f, 0.318547f,622 0.323143f, 0.327778f, 0.332452f, 0.337164f, 0.341914f, 0.346704f, 0.351533f, 0.356400f, 0.361307f, 0.366253f, 0.371238f,623 0.376262f, 0.381326f, 0.386430f, 0.391573f, 0.396755f, 0.401978f, 0.407240f, 0.412543f, 0.417885f, 0.423268f, 0.428691f,624 0.434154f, 0.439657f, 0.445201f, 0.450786f, 0.456411f, 0.462077f, 0.467784f, 0.473532f, 0.479320f, 0.485150f, 0.491021f,625 0.496933f, 0.502887f, 0.508881f, 0.514918f, 0.520996f, 0.527115f, 0.533276f, 0.539480f, 0.545725f, 0.552011f, 0.558340f,626 0.564712f, 0.571125f, 0.577581f, 0.584078f, 0.590619f, 0.597202f, 0.603827f, 0.610496f, 0.617207f, 0.623960f, 0.630757f,627 0.637597f, 0.644480f, 0.651406f, 0.658375f, 0.665387f, 0.672443f, 0.679543f, 0.686685f, 0.693872f, 0.701102f, 0.708376f,628 0.715694f, 0.723055f, 0.730461f, 0.737911f, 0.745404f, 0.752942f, 0.760525f, 0.768151f, 0.775822f, 0.783538f, 0.791298f,629 0.799103f, 0.806952f, 0.814847f, 0.822786f, 0.830770f, 0.838799f, 0.846873f, 0.854993f, 0.863157f, 0.871367f, 0.879622f,630 0.887923f, 0.896269f, 0.904661f, 0.913099f, 0.921582f, 0.930111f, 0.938686f, 0.947307f, 0.955974f, 0.964686f, 0.973445f,631 0.982251f, 0.991102f, 1.0f632};633634static float stbir__srgb_to_linear(float f)635{636 if (f <= 0.04045f)637 return f / 12.92f;638 else639 return (float)pow((f + 0.055f) / 1.055f, 2.4f);640}641642static float stbir__linear_to_srgb(float f)643{644 if (f <= 0.0031308f)645 return f * 12.92f;646 else647 return 1.055f * (float)pow(f, 1 / 2.4f) - 0.055f;648}649650#ifndef STBIR_NON_IEEE_FLOAT651// From https://gist.github.com/rygorous/2203834652653typedef union654{655 stbir_uint32 u;656 float f;657} stbir__FP32;658659static const stbir_uint32 fp32_to_srgb8_tab4[104] = {660 0x0073000d, 0x007a000d, 0x0080000d, 0x0087000d, 0x008d000d, 0x0094000d, 0x009a000d, 0x00a1000d,661 0x00a7001a, 0x00b4001a, 0x00c1001a, 0x00ce001a, 0x00da001a, 0x00e7001a, 0x00f4001a, 0x0101001a,662 0x010e0033, 0x01280033, 0x01410033, 0x015b0033, 0x01750033, 0x018f0033, 0x01a80033, 0x01c20033,663 0x01dc0067, 0x020f0067, 0x02430067, 0x02760067, 0x02aa0067, 0x02dd0067, 0x03110067, 0x03440067,664 0x037800ce, 0x03df00ce, 0x044600ce, 0x04ad00ce, 0x051400ce, 0x057b00c5, 0x05dd00bc, 0x063b00b5,665 0x06970158, 0x07420142, 0x07e30130, 0x087b0120, 0x090b0112, 0x09940106, 0x0a1700fc, 0x0a9500f2,666 0x0b0f01cb, 0x0bf401ae, 0x0ccb0195, 0x0d950180, 0x0e56016e, 0x0f0d015e, 0x0fbc0150, 0x10630143,667 0x11070264, 0x1238023e, 0x1357021d, 0x14660201, 0x156601e9, 0x165a01d3, 0x174401c0, 0x182401af,668 0x18fe0331, 0x1a9602fe, 0x1c1502d2, 0x1d7e02ad, 0x1ed4028d, 0x201a0270, 0x21520256, 0x227d0240,669 0x239f0443, 0x25c003fe, 0x27bf03c4, 0x29a10392, 0x2b6a0367, 0x2d1d0341, 0x2ebe031f, 0x304d0300,670 0x31d105b0, 0x34a80555, 0x37520507, 0x39d504c5, 0x3c37048b, 0x3e7c0458, 0x40a8042a, 0x42bd0401,671 0x44c20798, 0x488e071e, 0x4c1c06b6, 0x4f76065d, 0x52a50610, 0x55ac05cc, 0x5892058f, 0x5b590559,672 0x5e0c0a23, 0x631c0980, 0x67db08f6, 0x6c55087f, 0x70940818, 0x74a007bd, 0x787d076c, 0x7c330723,673};674675static stbir_uint8 stbir__linear_to_srgb_uchar(float in)676{677 static const stbir__FP32 almostone = { 0x3f7fffff }; // 1-eps678 static const stbir__FP32 minval = { (127-13) << 23 };679 stbir_uint32 tab,bias,scale,t;680 stbir__FP32 f;681682 // Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively.683 // The tests are carefully written so that NaNs map to 0, same as in the reference684 // implementation.685 if (!(in > minval.f)) // written this way to catch NaNs686 in = minval.f;687 if (in > almostone.f)688 in = almostone.f;689690 // Do the table lookup and unpack bias, scale691 f.f = in;692 tab = fp32_to_srgb8_tab4[(f.u - minval.u) >> 20];693 bias = (tab >> 16) << 9;694 scale = tab & 0xffff;695696 // Grab next-highest mantissa bits and perform linear interpolation697 t = (f.u >> 12) & 0xff;698 return (unsigned char) ((bias + scale*t) >> 16);699}700701#else702// sRGB transition values, scaled by 1<<28703static int stbir__srgb_offset_to_linear_scaled[256] =704{705 0, 40738, 122216, 203693, 285170, 366648, 448125, 529603,706 611080, 692557, 774035, 855852, 942009, 1033024, 1128971, 1229926,707 1335959, 1447142, 1563542, 1685229, 1812268, 1944725, 2082664, 2226148,708 2375238, 2529996, 2690481, 2856753, 3028870, 3206888, 3390865, 3580856,709 3776916, 3979100, 4187460, 4402049, 4622919, 4850123, 5083710, 5323731,710 5570236, 5823273, 6082892, 6349140, 6622065, 6901714, 7188133, 7481369,711 7781466, 8088471, 8402427, 8723380, 9051372, 9386448, 9728650, 10078021,712 10434603, 10798439, 11169569, 11548036, 11933879, 12327139, 12727857, 13136073,713 13551826, 13975156, 14406100, 14844697, 15290987, 15745007, 16206795, 16676389,714 17153826, 17639142, 18132374, 18633560, 19142734, 19659934, 20185196, 20718552,715 21260042, 21809696, 22367554, 22933648, 23508010, 24090680, 24681686, 25281066,716 25888850, 26505076, 27129772, 27762974, 28404716, 29055026, 29713942, 30381490,717 31057708, 31742624, 32436272, 33138682, 33849884, 34569912, 35298800, 36036568,718 36783260, 37538896, 38303512, 39077136, 39859796, 40651528, 41452360, 42262316,719 43081432, 43909732, 44747252, 45594016, 46450052, 47315392, 48190064, 49074096,720 49967516, 50870356, 51782636, 52704392, 53635648, 54576432, 55526772, 56486700,721 57456236, 58435408, 59424248, 60422780, 61431036, 62449032, 63476804, 64514376,722 65561776, 66619028, 67686160, 68763192, 69850160, 70947088, 72053992, 73170912,723 74297864, 75434880, 76581976, 77739184, 78906536, 80084040, 81271736, 82469648,724 83677792, 84896192, 86124888, 87363888, 88613232, 89872928, 91143016, 92423512,725 93714432, 95015816, 96327688, 97650056, 98982952, 100326408, 101680440, 103045072,726 104420320, 105806224, 107202800, 108610064, 110028048, 111456776, 112896264, 114346544,727 115807632, 117279552, 118762328, 120255976, 121760536, 123276016, 124802440, 126339832,728 127888216, 129447616, 131018048, 132599544, 134192112, 135795792, 137410592, 139036528,729 140673648, 142321952, 143981456, 145652208, 147334208, 149027488, 150732064, 152447968,730 154175200, 155913792, 157663776, 159425168, 161197984, 162982240, 164777968, 166585184,731 168403904, 170234160, 172075968, 173929344, 175794320, 177670896, 179559120, 181458992,732 183370528, 185293776, 187228736, 189175424, 191133888, 193104112, 195086128, 197079968,733 199085648, 201103184, 203132592, 205173888, 207227120, 209292272, 211369392, 213458480,734 215559568, 217672656, 219797792, 221934976, 224084240, 226245600, 228419056, 230604656,735 232802400, 235012320, 237234432, 239468736, 241715280, 243974080, 246245120, 248528464,736 250824112, 253132064, 255452368, 257785040, 260130080, 262487520, 264857376, 267239664,737};738739static stbir_uint8 stbir__linear_to_srgb_uchar(float f)740{741 int x = (int) (f * (1 << 28)); // has headroom so you don't need to clamp742 int v = 0;743 int i;744745 // Refine the guess with a short binary search.746 i = v + 128; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;747 i = v + 64; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;748 i = v + 32; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;749 i = v + 16; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;750 i = v + 8; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;751 i = v + 4; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;752 i = v + 2; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;753 i = v + 1; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;754755 return (stbir_uint8) v;756}757#endif758759static float stbir__filter_trapezoid(float x, float scale)760{761 float halfscale = scale / 2;762 float t = 0.5f + halfscale;763 STBIR_ASSERT(scale <= 1);764765 x = (float)fabs(x);766767 if (x >= t)768 return 0;769 else770 {771 float r = 0.5f - halfscale;772 if (x <= r)773 return 1;774 else775 return (t - x) / scale;776 }777}778779static float stbir__support_trapezoid(float scale)780{781 STBIR_ASSERT(scale <= 1);782 return 0.5f + scale / 2;783}784785static float stbir__filter_triangle(float x, float s)786{787 STBIR__UNUSED_PARAM(s);788789 x = (float)fabs(x);790791 if (x <= 1.0f)792 return 1 - x;793 else794 return 0;795}796797static float stbir__filter_cubic(float x, float s)798{799 STBIR__UNUSED_PARAM(s);800801 x = (float)fabs(x);802803 if (x < 1.0f)804 return (4 + x*x*(3*x - 6))/6;805 else if (x < 2.0f)806 return (8 + x*(-12 + x*(6 - x)))/6;807808 return (0.0f);809}810811static float stbir__filter_catmullrom(float x, float s)812{813 STBIR__UNUSED_PARAM(s);814815 x = (float)fabs(x);816817 if (x < 1.0f)818 return 1 - x*x*(2.5f - 1.5f*x);819 else if (x < 2.0f)820 return 2 - x*(4 + x*(0.5f*x - 2.5f));821822 return (0.0f);823}824825static float stbir__filter_mitchell(float x, float s)826{827 STBIR__UNUSED_PARAM(s);828829 x = (float)fabs(x);830831 if (x < 1.0f)832 return (16 + x*x*(21 * x - 36))/18;833 else if (x < 2.0f)834 return (32 + x*(-60 + x*(36 - 7*x)))/18;835836 return (0.0f);837}838839static float stbir__support_zero(float s)840{841 STBIR__UNUSED_PARAM(s);842 return 0;843}844845static float stbir__support_one(float s)846{847 STBIR__UNUSED_PARAM(s);848 return 1;849}850851static float stbir__support_two(float s)852{853 STBIR__UNUSED_PARAM(s);854 return 2;855}856857static stbir__filter_info stbir__filter_info_table[] = {858 { NULL, stbir__support_zero },859 { stbir__filter_trapezoid, stbir__support_trapezoid },860 { stbir__filter_triangle, stbir__support_one },861 { stbir__filter_cubic, stbir__support_two },862 { stbir__filter_catmullrom, stbir__support_two },863 { stbir__filter_mitchell, stbir__support_two },864};865866stbir__inline static int stbir__use_upsampling(float ratio)867{868 return ratio > 1;869}870871stbir__inline static int stbir__use_width_upsampling(stbir__info* stbir_info)872{873 return stbir__use_upsampling(stbir_info->horizontal_scale);874}875876stbir__inline static int stbir__use_height_upsampling(stbir__info* stbir_info)877{878 return stbir__use_upsampling(stbir_info->vertical_scale);879}880881// This is the maximum number of input samples that can affect an output sample882// with the given filter883static int stbir__get_filter_pixel_width(stbir_filter filter, float scale)884{885 STBIR_ASSERT(filter != 0);886 STBIR_ASSERT(filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));887888 if (stbir__use_upsampling(scale))889 return (int)ceil(stbir__filter_info_table[filter].support(1/scale) * 2);890 else891 return (int)ceil(stbir__filter_info_table[filter].support(scale) * 2 / scale);892}893894// This is how much to expand buffers to account for filters seeking outside895// the image boundaries.896static int stbir__get_filter_pixel_margin(stbir_filter filter, float scale)897{898 return stbir__get_filter_pixel_width(filter, scale) / 2;899}900901static int stbir__get_coefficient_width(stbir_filter filter, float scale)902{903 if (stbir__use_upsampling(scale))904 return (int)ceil(stbir__filter_info_table[filter].support(1 / scale) * 2);905 else906 return (int)ceil(stbir__filter_info_table[filter].support(scale) * 2);907}908909static int stbir__get_contributors(float scale, stbir_filter filter, int input_size, int output_size)910{911 if (stbir__use_upsampling(scale))912 return output_size;913 else914 return (input_size + stbir__get_filter_pixel_margin(filter, scale) * 2);915}916917static int stbir__get_total_horizontal_coefficients(stbir__info* info)918{919 return info->horizontal_num_contributors920 * stbir__get_coefficient_width (info->horizontal_filter, info->horizontal_scale);921}922923static int stbir__get_total_vertical_coefficients(stbir__info* info)924{925 return info->vertical_num_contributors926 * stbir__get_coefficient_width (info->vertical_filter, info->vertical_scale);927}928929static stbir__contributors* stbir__get_contributor(stbir__contributors* contributors, int n)930{931 return &contributors[n];932}933934// For perf reasons this code is duplicated in stbir__resample_horizontal_upsample/downsample,935// if you change it here change it there too.936static float* stbir__get_coefficient(float* coefficients, stbir_filter filter, float scale, int n, int c)937{938 int width = stbir__get_coefficient_width(filter, scale);939 return &coefficients[width*n + c];940}941942static int stbir__edge_wrap_slow(stbir_edge edge, int n, int max)943{944 switch (edge)945 {946 case STBIR_EDGE_ZERO:947 return 0; // we'll decode the wrong pixel here, and then overwrite with 0s later948949 case STBIR_EDGE_CLAMP:950 if (n < 0)951 return 0;952953 if (n >= max)954 return max - 1;955956 return n; // NOTREACHED957958 case STBIR_EDGE_REFLECT:959 {960 if (n < 0)961 {962 if (n < max)963 return -n;964 else965 return max - 1;966 }967968 if (n >= max)969 {970 int max2 = max * 2;971 if (n >= max2)972 return 0;973 else974 return max2 - n - 1;975 }976977 return n; // NOTREACHED978 }979980 case STBIR_EDGE_WRAP:981 if (n >= 0)982 return (n % max);983 else984 {985 int m = (-n) % max;986987 if (m != 0)988 m = max - m;989990 return (m);991 }992 // NOTREACHED993994 default:995 STBIR_ASSERT(!"Unimplemented edge type");996 return 0;997 }998}9991000stbir__inline static int stbir__edge_wrap(stbir_edge edge, int n, int max)1001{1002 // avoid per-pixel switch1003 if (n >= 0 && n < max)1004 return n;1005 return stbir__edge_wrap_slow(edge, n, max);1006}10071008// What input pixels contribute to this output pixel?1009static 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)1010{1011 float out_pixel_center = (float)n + 0.5f;1012 float out_pixel_influence_lowerbound = out_pixel_center - out_filter_radius;1013 float out_pixel_influence_upperbound = out_pixel_center + out_filter_radius;10141015 float in_pixel_influence_lowerbound = (out_pixel_influence_lowerbound + out_shift) / scale_ratio;1016 float in_pixel_influence_upperbound = (out_pixel_influence_upperbound + out_shift) / scale_ratio;10171018 *in_center_of_out = (out_pixel_center + out_shift) / scale_ratio;1019 *in_first_pixel = (int)(floor(in_pixel_influence_lowerbound + 0.5));1020 *in_last_pixel = (int)(floor(in_pixel_influence_upperbound - 0.5));1021}10221023// What output pixels does this input pixel contribute to?1024static 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)1025{1026 float in_pixel_center = (float)n + 0.5f;1027 float in_pixel_influence_lowerbound = in_pixel_center - in_pixels_radius;1028 float in_pixel_influence_upperbound = in_pixel_center + in_pixels_radius;10291030 float out_pixel_influence_lowerbound = in_pixel_influence_lowerbound * scale_ratio - out_shift;1031 float out_pixel_influence_upperbound = in_pixel_influence_upperbound * scale_ratio - out_shift;10321033 *out_center_of_in = in_pixel_center * scale_ratio - out_shift;1034 *out_first_pixel = (int)(floor(out_pixel_influence_lowerbound + 0.5));1035 *out_last_pixel = (int)(floor(out_pixel_influence_upperbound - 0.5));1036}10371038static 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)1039{1040 int i;1041 float total_filter = 0;1042 float filter_scale;10431044 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.10451046 contributor->n0 = in_first_pixel;1047 contributor->n1 = in_last_pixel;10481049 STBIR_ASSERT(contributor->n1 >= contributor->n0);10501051 for (i = 0; i <= in_last_pixel - in_first_pixel; i++)1052 {1053 float in_pixel_center = (float)(i + in_first_pixel) + 0.5f;1054 coefficient_group[i] = stbir__filter_info_table[filter].kernel(in_center_of_out - in_pixel_center, 1 / scale);10551056 // If the coefficient is zero, skip it. (Don't do the <0 check here, we want the influence of those outside pixels.)1057 if (i == 0 && !coefficient_group[i])1058 {1059 contributor->n0 = ++in_first_pixel;1060 i--;1061 continue;1062 }10631064 total_filter += coefficient_group[i];1065 }10661067 // NOTE(fg): Not actually true in general, nor is there any reason to expect it should be.1068 // It would be true in exact math but is at best approximately true in floating-point math,1069 // and it would not make sense to try and put actual bounds on this here because it depends1070 // on the image aspect ratio which can get pretty extreme.1071 //STBIR_ASSERT(stbir__filter_info_table[filter].kernel((float)(in_last_pixel + 1) + 0.5f - in_center_of_out, 1/scale) == 0);10721073 STBIR_ASSERT(total_filter > 0.9);1074 STBIR_ASSERT(total_filter < 1.1f); // Make sure it's not way off.10751076 // Make sure the sum of all coefficients is 1.1077 filter_scale = 1 / total_filter;10781079 for (i = 0; i <= in_last_pixel - in_first_pixel; i++)1080 coefficient_group[i] *= filter_scale;10811082 for (i = in_last_pixel - in_first_pixel; i >= 0; i--)1083 {1084 if (coefficient_group[i])1085 break;10861087 // This line has no weight. We can skip it.1088 contributor->n1 = contributor->n0 + i - 1;1089 }1090}10911092static 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)1093{1094 int i;10951096 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.10971098 contributor->n0 = out_first_pixel;1099 contributor->n1 = out_last_pixel;11001101 STBIR_ASSERT(contributor->n1 >= contributor->n0);11021103 for (i = 0; i <= out_last_pixel - out_first_pixel; i++)1104 {1105 float out_pixel_center = (float)(i + out_first_pixel) + 0.5f;1106 float x = out_pixel_center - out_center_of_in;1107 coefficient_group[i] = stbir__filter_info_table[filter].kernel(x, scale_ratio) * scale_ratio;1108 }11091110 // NOTE(fg): Not actually true in general, nor is there any reason to expect it should be.1111 // It would be true in exact math but is at best approximately true in floating-point math,1112 // and it would not make sense to try and put actual bounds on this here because it depends1113 // on the image aspect ratio which can get pretty extreme.1114 //STBIR_ASSERT(stbir__filter_info_table[filter].kernel((float)(out_last_pixel + 1) + 0.5f - out_center_of_in, scale_ratio) == 0);11151116 for (i = out_last_pixel - out_first_pixel; i >= 0; i--)1117 {1118 if (coefficient_group[i])1119 break;11201121 // This line has no weight. We can skip it.1122 contributor->n1 = contributor->n0 + i - 1;1123 }1124}11251126static void stbir__normalize_downsample_coefficients(stbir__contributors* contributors, float* coefficients, stbir_filter filter, float scale_ratio, int input_size, int output_size)1127{1128 int num_contributors = stbir__get_contributors(scale_ratio, filter, input_size, output_size);1129 int num_coefficients = stbir__get_coefficient_width(filter, scale_ratio);1130 int i, j;1131 int skip;11321133 for (i = 0; i < output_size; i++)1134 {1135 float scale;1136 float total = 0;11371138 for (j = 0; j < num_contributors; j++)1139 {1140 if (i >= contributors[j].n0 && i <= contributors[j].n1)1141 {1142 float coefficient = *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i - contributors[j].n0);1143 total += coefficient;1144 }1145 else if (i < contributors[j].n0)1146 break;1147 }11481149 STBIR_ASSERT(total > 0.9f);1150 STBIR_ASSERT(total < 1.1f);11511152 scale = 1 / total;11531154 for (j = 0; j < num_contributors; j++)1155 {1156 if (i >= contributors[j].n0 && i <= contributors[j].n1)1157 *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i - contributors[j].n0) *= scale;1158 else if (i < contributors[j].n0)1159 break;1160 }1161 }11621163 // Optimize: Skip zero coefficients and contributions outside of image bounds.1164 // Do this after normalizing because normalization depends on the n0/n1 values.1165 for (j = 0; j < num_contributors; j++)1166 {1167 int range, max, width;11681169 skip = 0;1170 while (*stbir__get_coefficient(coefficients, filter, scale_ratio, j, skip) == 0)1171 skip++;11721173 contributors[j].n0 += skip;11741175 while (contributors[j].n0 < 0)1176 {1177 contributors[j].n0++;1178 skip++;1179 }11801181 range = contributors[j].n1 - contributors[j].n0 + 1;1182 max = stbir__min(num_coefficients, range);11831184 width = stbir__get_coefficient_width(filter, scale_ratio);1185 for (i = 0; i < max; i++)1186 {1187 if (i + skip >= width)1188 break;11891190 *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i) = *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i + skip);1191 }11921193 continue;1194 }11951196 // Using min to avoid writing into invalid pixels.1197 for (i = 0; i < num_contributors; i++)1198 contributors[i].n1 = stbir__min(contributors[i].n1, output_size - 1);1199}12001201// Each scan line uses the same kernel values so we should calculate the kernel1202// values once and then we can use them for every scan line.1203static void stbir__calculate_filters(stbir__contributors* contributors, float* coefficients, stbir_filter filter, float scale_ratio, float shift, int input_size, int output_size)1204{1205 int n;1206 int total_contributors = stbir__get_contributors(scale_ratio, filter, input_size, output_size);12071208 if (stbir__use_upsampling(scale_ratio))1209 {1210 float out_pixels_radius = stbir__filter_info_table[filter].support(1 / scale_ratio) * scale_ratio;12111212 // Looping through out pixels1213 for (n = 0; n < total_contributors; n++)1214 {1215 float in_center_of_out; // Center of the current out pixel in the in pixel space1216 int in_first_pixel, in_last_pixel;12171218 stbir__calculate_sample_range_upsample(n, out_pixels_radius, scale_ratio, shift, &in_first_pixel, &in_last_pixel, &in_center_of_out);12191220 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));1221 }1222 }1223 else1224 {1225 float in_pixels_radius = stbir__filter_info_table[filter].support(scale_ratio) / scale_ratio;12261227 // Looping through in pixels1228 for (n = 0; n < total_contributors; n++)1229 {1230 float out_center_of_in; // Center of the current out pixel in the in pixel space1231 int out_first_pixel, out_last_pixel;1232 int n_adjusted = n - stbir__get_filter_pixel_margin(filter, scale_ratio);12331234 stbir__calculate_sample_range_downsample(n_adjusted, in_pixels_radius, scale_ratio, shift, &out_first_pixel, &out_last_pixel, &out_center_of_in);12351236 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));1237 }12381239 stbir__normalize_downsample_coefficients(contributors, coefficients, filter, scale_ratio, input_size, output_size);1240 }1241}12421243static float* stbir__get_decode_buffer(stbir__info* stbir_info)1244{1245 // The 0 index of the decode buffer starts after the margin. This makes1246 // it okay to use negative indexes on the decode buffer.1247 return &stbir_info->decode_buffer[stbir_info->horizontal_filter_pixel_margin * stbir_info->channels];1248}12491250#define STBIR__DECODE(type, colorspace) ((int)(type) * (STBIR_MAX_COLORSPACES) + (int)(colorspace))12511252static void stbir__decode_scanline(stbir__info* stbir_info, int n)1253{1254 int c;1255 int channels = stbir_info->channels;1256 int alpha_channel = stbir_info->alpha_channel;1257 int type = stbir_info->type;1258 int colorspace = stbir_info->colorspace;1259 int input_w = stbir_info->input_w;1260 size_t input_stride_bytes = stbir_info->input_stride_bytes;1261 float* decode_buffer = stbir__get_decode_buffer(stbir_info);1262 stbir_edge edge_horizontal = stbir_info->edge_horizontal;1263 stbir_edge edge_vertical = stbir_info->edge_vertical;1264 size_t in_buffer_row_offset = stbir__edge_wrap(edge_vertical, n, stbir_info->input_h) * input_stride_bytes;1265 const void* input_data = (char *) stbir_info->input_data + in_buffer_row_offset;1266 int max_x = input_w + stbir_info->horizontal_filter_pixel_margin;1267 int decode = STBIR__DECODE(type, colorspace);12681269 int x = -stbir_info->horizontal_filter_pixel_margin;12701271 // special handling for STBIR_EDGE_ZERO because it needs to return an item that doesn't appear in the input,1272 // and we want to avoid paying overhead on every pixel if not STBIR_EDGE_ZERO1273 if (edge_vertical == STBIR_EDGE_ZERO && (n < 0 || n >= stbir_info->input_h))1274 {1275 for (; x < max_x; x++)1276 for (c = 0; c < channels; c++)1277 decode_buffer[x*channels + c] = 0;1278 return;1279 }12801281 switch (decode)1282 {1283 case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_LINEAR):1284 for (; x < max_x; x++)1285 {1286 int decode_pixel_index = x * channels;1287 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1288 for (c = 0; c < channels; c++)1289 decode_buffer[decode_pixel_index + c] = ((float)((const unsigned char*)input_data)[input_pixel_index + c]) / stbir__max_uint8_as_float;1290 }1291 break;12921293 case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_SRGB):1294 for (; x < max_x; x++)1295 {1296 int decode_pixel_index = x * channels;1297 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1298 for (c = 0; c < channels; c++)1299 decode_buffer[decode_pixel_index + c] = stbir__srgb_uchar_to_linear_float[((const unsigned char*)input_data)[input_pixel_index + c]];13001301 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1302 decode_buffer[decode_pixel_index + alpha_channel] = ((float)((const unsigned char*)input_data)[input_pixel_index + alpha_channel]) / stbir__max_uint8_as_float;1303 }1304 break;13051306 case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR):1307 for (; x < max_x; x++)1308 {1309 int decode_pixel_index = x * channels;1310 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1311 for (c = 0; c < channels; c++)1312 decode_buffer[decode_pixel_index + c] = ((float)((const unsigned short*)input_data)[input_pixel_index + c]) / stbir__max_uint16_as_float;1313 }1314 break;13151316 case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB):1317 for (; x < max_x; x++)1318 {1319 int decode_pixel_index = x * channels;1320 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1321 for (c = 0; c < channels; c++)1322 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);13231324 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1325 decode_buffer[decode_pixel_index + alpha_channel] = ((float)((const unsigned short*)input_data)[input_pixel_index + alpha_channel]) / stbir__max_uint16_as_float;1326 }1327 break;13281329 case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR):1330 for (; x < max_x; x++)1331 {1332 int decode_pixel_index = x * channels;1333 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1334 for (c = 0; c < channels; c++)1335 decode_buffer[decode_pixel_index + c] = (float)(((double)((const unsigned int*)input_data)[input_pixel_index + c]) / stbir__max_uint32_as_float);1336 }1337 break;13381339 case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB):1340 for (; x < max_x; x++)1341 {1342 int decode_pixel_index = x * channels;1343 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1344 for (c = 0; c < channels; c++)1345 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));13461347 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1348 decode_buffer[decode_pixel_index + alpha_channel] = (float)(((double)((const unsigned int*)input_data)[input_pixel_index + alpha_channel]) / stbir__max_uint32_as_float);1349 }1350 break;13511352 case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR):1353 for (; x < max_x; x++)1354 {1355 int decode_pixel_index = x * channels;1356 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1357 for (c = 0; c < channels; c++)1358 decode_buffer[decode_pixel_index + c] = ((const float*)input_data)[input_pixel_index + c];1359 }1360 break;13611362 case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB):1363 for (; x < max_x; x++)1364 {1365 int decode_pixel_index = x * channels;1366 int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;1367 for (c = 0; c < channels; c++)1368 decode_buffer[decode_pixel_index + c] = stbir__srgb_to_linear(((const float*)input_data)[input_pixel_index + c]);13691370 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1371 decode_buffer[decode_pixel_index + alpha_channel] = ((const float*)input_data)[input_pixel_index + alpha_channel];1372 }13731374 break;13751376 default:1377 STBIR_ASSERT(!"Unknown type/colorspace/channels combination.");1378 break;1379 }13801381 if (!(stbir_info->flags & STBIR_FLAG_ALPHA_PREMULTIPLIED))1382 {1383 for (x = -stbir_info->horizontal_filter_pixel_margin; x < max_x; x++)1384 {1385 int decode_pixel_index = x * channels;13861387 // If the alpha value is 0 it will clobber the color values. Make sure it's not.1388 float alpha = decode_buffer[decode_pixel_index + alpha_channel];1389#ifndef STBIR_NO_ALPHA_EPSILON1390 if (stbir_info->type != STBIR_TYPE_FLOAT) {1391 alpha += STBIR_ALPHA_EPSILON;1392 decode_buffer[decode_pixel_index + alpha_channel] = alpha;1393 }1394#endif1395 for (c = 0; c < channels; c++)1396 {1397 if (c == alpha_channel)1398 continue;13991400 decode_buffer[decode_pixel_index + c] *= alpha;1401 }1402 }1403 }14041405 if (edge_horizontal == STBIR_EDGE_ZERO)1406 {1407 for (x = -stbir_info->horizontal_filter_pixel_margin; x < 0; x++)1408 {1409 for (c = 0; c < channels; c++)1410 decode_buffer[x*channels + c] = 0;1411 }1412 for (x = input_w; x < max_x; x++)1413 {1414 for (c = 0; c < channels; c++)1415 decode_buffer[x*channels + c] = 0;1416 }1417 }1418}14191420static float* stbir__get_ring_buffer_entry(float* ring_buffer, int index, int ring_buffer_length)1421{1422 return &ring_buffer[index * ring_buffer_length];1423}14241425static float* stbir__add_empty_ring_buffer_entry(stbir__info* stbir_info, int n)1426{1427 int ring_buffer_index;1428 float* ring_buffer;14291430 stbir_info->ring_buffer_last_scanline = n;14311432 if (stbir_info->ring_buffer_begin_index < 0)1433 {1434 ring_buffer_index = stbir_info->ring_buffer_begin_index = 0;1435 stbir_info->ring_buffer_first_scanline = n;1436 }1437 else1438 {1439 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;1440 STBIR_ASSERT(ring_buffer_index != stbir_info->ring_buffer_begin_index);1441 }14421443 ring_buffer = stbir__get_ring_buffer_entry(stbir_info->ring_buffer, ring_buffer_index, stbir_info->ring_buffer_length_bytes / sizeof(float));1444 memset(ring_buffer, 0, stbir_info->ring_buffer_length_bytes);14451446 return ring_buffer;1447}144814491450static void stbir__resample_horizontal_upsample(stbir__info* stbir_info, float* output_buffer)1451{1452 int x, k;1453 int output_w = stbir_info->output_w;1454 int channels = stbir_info->channels;1455 float* decode_buffer = stbir__get_decode_buffer(stbir_info);1456 stbir__contributors* horizontal_contributors = stbir_info->horizontal_contributors;1457 float* horizontal_coefficients = stbir_info->horizontal_coefficients;1458 int coefficient_width = stbir_info->horizontal_coefficient_width;14591460 for (x = 0; x < output_w; x++)1461 {1462 int n0 = horizontal_contributors[x].n0;1463 int n1 = horizontal_contributors[x].n1;14641465 int out_pixel_index = x * channels;1466 int coefficient_group = coefficient_width * x;1467 int coefficient_counter = 0;14681469 STBIR_ASSERT(n1 >= n0);1470 STBIR_ASSERT(n0 >= -stbir_info->horizontal_filter_pixel_margin);1471 STBIR_ASSERT(n1 >= -stbir_info->horizontal_filter_pixel_margin);1472 STBIR_ASSERT(n0 < stbir_info->input_w + stbir_info->horizontal_filter_pixel_margin);1473 STBIR_ASSERT(n1 < stbir_info->input_w + stbir_info->horizontal_filter_pixel_margin);14741475 switch (channels) {1476 case 1:1477 for (k = n0; k <= n1; k++)1478 {1479 int in_pixel_index = k * 1;1480 float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++];1481 STBIR_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_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_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_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_ASSERT(coefficient != 0);1525 for (c = 0; c < channels; c++)1526 output_buffer[out_pixel_index + c] += decode_buffer[in_pixel_index + c] * coefficient;1527 }1528 break;1529 }1530 }1531}15321533static void stbir__resample_horizontal_downsample(stbir__info* stbir_info, float* output_buffer)1534{1535 int x, k;1536 int input_w = stbir_info->input_w;1537 int channels = stbir_info->channels;1538 float* decode_buffer = stbir__get_decode_buffer(stbir_info);1539 stbir__contributors* horizontal_contributors = stbir_info->horizontal_contributors;1540 float* horizontal_coefficients = stbir_info->horizontal_coefficients;1541 int coefficient_width = stbir_info->horizontal_coefficient_width;1542 int filter_pixel_margin = stbir_info->horizontal_filter_pixel_margin;1543 int max_x = input_w + filter_pixel_margin * 2;15441545 STBIR_ASSERT(!stbir__use_width_upsampling(stbir_info));15461547 switch (channels) {1548 case 1:1549 for (x = 0; x < max_x; x++)1550 {1551 int n0 = horizontal_contributors[x].n0;1552 int n1 = horizontal_contributors[x].n1;15531554 int in_x = x - filter_pixel_margin;1555 int in_pixel_index = in_x * 1;1556 int max_n = n1;1557 int coefficient_group = coefficient_width * x;15581559 for (k = n0; k <= max_n; k++)1560 {1561 int out_pixel_index = k * 1;1562 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1563 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1564 }1565 }1566 break;15671568 case 2:1569 for (x = 0; x < max_x; x++)1570 {1571 int n0 = horizontal_contributors[x].n0;1572 int n1 = horizontal_contributors[x].n1;15731574 int in_x = x - filter_pixel_margin;1575 int in_pixel_index = in_x * 2;1576 int max_n = n1;1577 int coefficient_group = coefficient_width * x;15781579 for (k = n0; k <= max_n; k++)1580 {1581 int out_pixel_index = k * 2;1582 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1583 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1584 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1585 }1586 }1587 break;15881589 case 3:1590 for (x = 0; x < max_x; x++)1591 {1592 int n0 = horizontal_contributors[x].n0;1593 int n1 = horizontal_contributors[x].n1;15941595 int in_x = x - filter_pixel_margin;1596 int in_pixel_index = in_x * 3;1597 int max_n = n1;1598 int coefficient_group = coefficient_width * x;15991600 for (k = n0; k <= max_n; k++)1601 {1602 int out_pixel_index = k * 3;1603 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1604 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1605 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1606 output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient;1607 }1608 }1609 break;16101611 case 4:1612 for (x = 0; x < max_x; x++)1613 {1614 int n0 = horizontal_contributors[x].n0;1615 int n1 = horizontal_contributors[x].n1;16161617 int in_x = x - filter_pixel_margin;1618 int in_pixel_index = in_x * 4;1619 int max_n = n1;1620 int coefficient_group = coefficient_width * x;16211622 for (k = n0; k <= max_n; k++)1623 {1624 int out_pixel_index = k * 4;1625 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1626 output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;1627 output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;1628 output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient;1629 output_buffer[out_pixel_index + 3] += decode_buffer[in_pixel_index + 3] * coefficient;1630 }1631 }1632 break;16331634 default: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 * channels;1642 int max_n = n1;1643 int coefficient_group = coefficient_width * x;16441645 for (k = n0; k <= max_n; k++)1646 {1647 int c;1648 int out_pixel_index = k * channels;1649 float coefficient = horizontal_coefficients[coefficient_group + k - n0];1650 for (c = 0; c < channels; c++)1651 output_buffer[out_pixel_index + c] += decode_buffer[in_pixel_index + c] * coefficient;1652 }1653 }1654 break;1655 }1656}16571658static void stbir__decode_and_resample_upsample(stbir__info* stbir_info, int n)1659{1660 // Decode the nth scanline from the source image into the decode buffer.1661 stbir__decode_scanline(stbir_info, n);16621663 // Now resample it into the ring buffer.1664 if (stbir__use_width_upsampling(stbir_info))1665 stbir__resample_horizontal_upsample(stbir_info, stbir__add_empty_ring_buffer_entry(stbir_info, n));1666 else1667 stbir__resample_horizontal_downsample(stbir_info, stbir__add_empty_ring_buffer_entry(stbir_info, n));16681669 // Now it's sitting in the ring buffer ready to be used as source for the vertical sampling.1670}16711672static void stbir__decode_and_resample_downsample(stbir__info* stbir_info, int n)1673{1674 // Decode the nth scanline from the source image into the decode buffer.1675 stbir__decode_scanline(stbir_info, n);16761677 memset(stbir_info->horizontal_buffer, 0, stbir_info->output_w * stbir_info->channels * sizeof(float));16781679 // Now resample it into the horizontal buffer.1680 if (stbir__use_width_upsampling(stbir_info))1681 stbir__resample_horizontal_upsample(stbir_info, stbir_info->horizontal_buffer);1682 else1683 stbir__resample_horizontal_downsample(stbir_info, stbir_info->horizontal_buffer);16841685 // Now it's sitting in the horizontal buffer ready to be distributed into the ring buffers.1686}16871688// Get the specified scan line from the ring buffer.1689static 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)1690{1691 int ring_buffer_index = (begin_index + (get_scanline - first_scanline)) % ring_buffer_num_entries;1692 return stbir__get_ring_buffer_entry(ring_buffer, ring_buffer_index, ring_buffer_length);1693}169416951696static void stbir__encode_scanline(stbir__info* stbir_info, int num_pixels, void *output_buffer, float *encode_buffer, int channels, int alpha_channel, int decode)1697{1698 int x;1699 int n;1700 int num_nonalpha;1701 stbir_uint16 nonalpha[STBIR_MAX_CHANNELS];17021703 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_PREMULTIPLIED))1704 {1705 for (x=0; x < num_pixels; ++x)1706 {1707 int pixel_index = x*channels;17081709 float alpha = encode_buffer[pixel_index + alpha_channel];1710 float reciprocal_alpha = alpha ? 1.0f / alpha : 0;17111712 // unrolling this produced a 1% slowdown upscaling a large RGBA linear-space image on my machine - stb1713 for (n = 0; n < channels; n++)1714 if (n != alpha_channel)1715 encode_buffer[pixel_index + n] *= reciprocal_alpha;17161717 // We added in a small epsilon to prevent the color channel from being deleted with zero alpha.1718 // Because we only add it for integer types, it will automatically be discarded on integer1719 // conversion, so we don't need to subtract it back out (which would be problematic for1720 // numeric precision reasons).1721 }1722 }17231724 // build a table of all channels that need colorspace correction, so1725 // we don't perform colorspace correction on channels that don't need it.1726 for (x = 0, num_nonalpha = 0; x < channels; ++x)1727 {1728 if (x != alpha_channel || (stbir_info->flags & STBIR_FLAG_ALPHA_USES_COLORSPACE))1729 {1730 nonalpha[num_nonalpha++] = (stbir_uint16)x;1731 }1732 }17331734 #define STBIR__ROUND_INT(f) ((int) ((f)+0.5))1735 #define STBIR__ROUND_UINT(f) ((stbir_uint32) ((f)+0.5))17361737 #ifdef STBIR__SATURATE_INT1738 #define STBIR__ENCODE_LINEAR8(f) stbir__saturate8 (STBIR__ROUND_INT((f) * stbir__max_uint8_as_float ))1739 #define STBIR__ENCODE_LINEAR16(f) stbir__saturate16(STBIR__ROUND_INT((f) * stbir__max_uint16_as_float))1740 #else1741 #define STBIR__ENCODE_LINEAR8(f) (unsigned char ) STBIR__ROUND_INT(stbir__saturate(f) * stbir__max_uint8_as_float )1742 #define STBIR__ENCODE_LINEAR16(f) (unsigned short) STBIR__ROUND_INT(stbir__saturate(f) * stbir__max_uint16_as_float)1743 #endif17441745 switch (decode)1746 {1747 case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_LINEAR):1748 for (x=0; x < num_pixels; ++x)1749 {1750 int pixel_index = x*channels;17511752 for (n = 0; n < channels; n++)1753 {1754 int index = pixel_index + n;1755 ((unsigned char*)output_buffer)[index] = STBIR__ENCODE_LINEAR8(encode_buffer[index]);1756 }1757 }1758 break;17591760 case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_SRGB):1761 for (x=0; x < num_pixels; ++x)1762 {1763 int pixel_index = x*channels;17641765 for (n = 0; n < num_nonalpha; n++)1766 {1767 int index = pixel_index + nonalpha[n];1768 ((unsigned char*)output_buffer)[index] = stbir__linear_to_srgb_uchar(encode_buffer[index]);1769 }17701771 if (!(stbir_info->flags & STBIR_FLAG_ALPHA_USES_COLORSPACE))1772 ((unsigned char *)output_buffer)[pixel_index + alpha_channel] = STBIR__ENCODE_LINEAR8(encode_buffer[pixel_index+alpha_channel]);1773 }1774 break;17751776 case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR):1777 for (x=0; x < num_pixels; ++x)1778 {1779 int pixel_index = x*channels;17801781 for (n = 0; n < channels; n++)1782 {1783 int index = pixel_index + n;1784 ((unsigned short*)output_buffer)[index] = STBIR__ENCODE_LINEAR16(encode_buffer[index]);1785 }1786 }1787 break;17881789 case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB):1790 for (x=0; x < num_pixels; ++x)1791 {1792 int pixel_index = x*channels;17931794 for (n = 0; n < num_nonalpha; n++)1795 {1796 int index = pixel_index + nonalpha[n];1797 ((unsigned short*)output_buffer)[index] = (unsigned short)STBIR__ROUND_INT(stbir__linear_to_srgb(stbir__saturate(encode_buffer[index])) * stbir__max_uint16_as_float);1798 }17991800 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1801 ((unsigned short*)output_buffer)[pixel_index + alpha_channel] = STBIR__ENCODE_LINEAR16(encode_buffer[pixel_index + alpha_channel]);1802 }18031804 break;18051806 case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR):1807 for (x=0; x < num_pixels; ++x)1808 {1809 int pixel_index = x*channels;18101811 for (n = 0; n < channels; n++)1812 {1813 int index = pixel_index + n;1814 ((unsigned int*)output_buffer)[index] = (unsigned int)STBIR__ROUND_UINT(((double)stbir__saturate(encode_buffer[index])) * stbir__max_uint32_as_float);1815 }1816 }1817 break;18181819 case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB):1820 for (x=0; x < num_pixels; ++x)1821 {1822 int pixel_index = x*channels;18231824 for (n = 0; n < num_nonalpha; n++)1825 {1826 int index = pixel_index + nonalpha[n];1827 ((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);1828 }18291830 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1831 ((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);1832 }1833 break;18341835 case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR):1836 for (x=0; x < num_pixels; ++x)1837 {1838 int pixel_index = x*channels;18391840 for (n = 0; n < channels; n++)1841 {1842 int index = pixel_index + n;1843 ((float*)output_buffer)[index] = encode_buffer[index];1844 }1845 }1846 break;18471848 case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB):1849 for (x=0; x < num_pixels; ++x)1850 {1851 int pixel_index = x*channels;18521853 for (n = 0; n < num_nonalpha; n++)1854 {1855 int index = pixel_index + nonalpha[n];1856 ((float*)output_buffer)[index] = stbir__linear_to_srgb(encode_buffer[index]);1857 }18581859 if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))1860 ((float*)output_buffer)[pixel_index + alpha_channel] = encode_buffer[pixel_index + alpha_channel];1861 }1862 break;18631864 default:1865 STBIR_ASSERT(!"Unknown type/colorspace/channels combination.");1866 break;1867 }1868}18691870static void stbir__resample_vertical_upsample(stbir__info* stbir_info, int n)1871{1872 int x, k;1873 int output_w = stbir_info->output_w;1874 stbir__contributors* vertical_contributors = stbir_info->vertical_contributors;1875 float* vertical_coefficients = stbir_info->vertical_coefficients;1876 int channels = stbir_info->channels;1877 int alpha_channel = stbir_info->alpha_channel;1878 int type = stbir_info->type;1879 int colorspace = stbir_info->colorspace;1880 int ring_buffer_entries = stbir_info->ring_buffer_num_entries;1881 void* output_data = stbir_info->output_data;1882 float* encode_buffer = stbir_info->encode_buffer;1883 int decode = STBIR__DECODE(type, colorspace);1884 int coefficient_width = stbir_info->vertical_coefficient_width;1885 int coefficient_counter;1886 int contributor = n;18871888 float* ring_buffer = stbir_info->ring_buffer;1889 int ring_buffer_begin_index = stbir_info->ring_buffer_begin_index;1890 int ring_buffer_first_scanline = stbir_info->ring_buffer_first_scanline;1891 int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float);18921893 int n0,n1, output_row_start;1894 int coefficient_group = coefficient_width * contributor;18951896 n0 = vertical_contributors[contributor].n0;1897 n1 = vertical_contributors[contributor].n1;18981899 output_row_start = n * stbir_info->output_stride_bytes;19001901 STBIR_ASSERT(stbir__use_height_upsampling(stbir_info));19021903 memset(encode_buffer, 0, output_w * sizeof(float) * channels);19041905 // I tried reblocking this for better cache usage of encode_buffer1906 // (using x_outer, k, x_inner), but it lost speed. -- stb19071908 coefficient_counter = 0;1909 switch (channels) {1910 case 1:1911 for (k = n0; k <= n1; k++)1912 {1913 int coefficient_index = coefficient_counter++;1914 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);1915 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];1916 for (x = 0; x < output_w; ++x)1917 {1918 int in_pixel_index = x * 1;1919 encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;1920 }1921 }1922 break;1923 case 2:1924 for (k = n0; k <= n1; k++)1925 {1926 int coefficient_index = coefficient_counter++;1927 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);1928 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];1929 for (x = 0; x < output_w; ++x)1930 {1931 int in_pixel_index = x * 2;1932 encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;1933 encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient;1934 }1935 }1936 break;1937 case 3:1938 for (k = n0; k <= n1; k++)1939 {1940 int coefficient_index = coefficient_counter++;1941 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);1942 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];1943 for (x = 0; x < output_w; ++x)1944 {1945 int in_pixel_index = x * 3;1946 encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;1947 encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient;1948 encode_buffer[in_pixel_index + 2] += ring_buffer_entry[in_pixel_index + 2] * coefficient;1949 }1950 }1951 break;1952 case 4:1953 for (k = n0; k <= n1; k++)1954 {1955 int coefficient_index = coefficient_counter++;1956 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);1957 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];1958 for (x = 0; x < output_w; ++x)1959 {1960 int in_pixel_index = x * 4;1961 encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;1962 encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient;1963 encode_buffer[in_pixel_index + 2] += ring_buffer_entry[in_pixel_index + 2] * coefficient;1964 encode_buffer[in_pixel_index + 3] += ring_buffer_entry[in_pixel_index + 3] * coefficient;1965 }1966 }1967 break;1968 default:1969 for (k = n0; k <= n1; k++)1970 {1971 int coefficient_index = coefficient_counter++;1972 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);1973 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];1974 for (x = 0; x < output_w; ++x)1975 {1976 int in_pixel_index = x * channels;1977 int c;1978 for (c = 0; c < channels; c++)1979 encode_buffer[in_pixel_index + c] += ring_buffer_entry[in_pixel_index + c] * coefficient;1980 }1981 }1982 break;1983 }1984 stbir__encode_scanline(stbir_info, output_w, (char *) output_data + output_row_start, encode_buffer, channels, alpha_channel, decode);1985}19861987static void stbir__resample_vertical_downsample(stbir__info* stbir_info, int n)1988{1989 int x, k;1990 int output_w = stbir_info->output_w;1991 stbir__contributors* vertical_contributors = stbir_info->vertical_contributors;1992 float* vertical_coefficients = stbir_info->vertical_coefficients;1993 int channels = stbir_info->channels;1994 int ring_buffer_entries = stbir_info->ring_buffer_num_entries;1995 float* horizontal_buffer = stbir_info->horizontal_buffer;1996 int coefficient_width = stbir_info->vertical_coefficient_width;1997 int contributor = n + stbir_info->vertical_filter_pixel_margin;19981999 float* ring_buffer = stbir_info->ring_buffer;2000 int ring_buffer_begin_index = stbir_info->ring_buffer_begin_index;2001 int ring_buffer_first_scanline = stbir_info->ring_buffer_first_scanline;2002 int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float);2003 int n0,n1;20042005 n0 = vertical_contributors[contributor].n0;2006 n1 = vertical_contributors[contributor].n1;20072008 STBIR_ASSERT(!stbir__use_height_upsampling(stbir_info));20092010 for (k = n0; k <= n1; k++)2011 {2012 int coefficient_index = k - n0;2013 int coefficient_group = coefficient_width * contributor;2014 float coefficient = vertical_coefficients[coefficient_group + coefficient_index];20152016 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);20172018 switch (channels) {2019 case 1:2020 for (x = 0; x < output_w; x++)2021 {2022 int in_pixel_index = x * 1;2023 ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;2024 }2025 break;2026 case 2:2027 for (x = 0; x < output_w; x++)2028 {2029 int in_pixel_index = x * 2;2030 ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;2031 ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient;2032 }2033 break;2034 case 3:2035 for (x = 0; x < output_w; x++)2036 {2037 int in_pixel_index = x * 3;2038 ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;2039 ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient;2040 ring_buffer_entry[in_pixel_index + 2] += horizontal_buffer[in_pixel_index + 2] * coefficient;2041 }2042 break;2043 case 4:2044 for (x = 0; x < output_w; x++)2045 {2046 int in_pixel_index = x * 4;2047 ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;2048 ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient;2049 ring_buffer_entry[in_pixel_index + 2] += horizontal_buffer[in_pixel_index + 2] * coefficient;2050 ring_buffer_entry[in_pixel_index + 3] += horizontal_buffer[in_pixel_index + 3] * coefficient;2051 }2052 break;2053 default:2054 for (x = 0; x < output_w; x++)2055 {2056 int in_pixel_index = x * channels;20572058 int c;2059 for (c = 0; c < channels; c++)2060 ring_buffer_entry[in_pixel_index + c] += horizontal_buffer[in_pixel_index + c] * coefficient;2061 }2062 break;2063 }2064 }2065}20662067static void stbir__buffer_loop_upsample(stbir__info* stbir_info)2068{2069 int y;2070 float scale_ratio = stbir_info->vertical_scale;2071 float out_scanlines_radius = stbir__filter_info_table[stbir_info->vertical_filter].support(1/scale_ratio) * scale_ratio;20722073 STBIR_ASSERT(stbir__use_height_upsampling(stbir_info));20742075 for (y = 0; y < stbir_info->output_h; y++)2076 {2077 float in_center_of_out = 0; // Center of the current out scanline in the in scanline space2078 int in_first_scanline = 0, in_last_scanline = 0;20792080 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);20812082 STBIR_ASSERT(in_last_scanline - in_first_scanline + 1 <= stbir_info->ring_buffer_num_entries);20832084 if (stbir_info->ring_buffer_begin_index >= 0)2085 {2086 // Get rid of whatever we don't need anymore.2087 while (in_first_scanline > stbir_info->ring_buffer_first_scanline)2088 {2089 if (stbir_info->ring_buffer_first_scanline == stbir_info->ring_buffer_last_scanline)2090 {2091 // We just popped the last scanline off the ring buffer.2092 // Reset it to the empty state.2093 stbir_info->ring_buffer_begin_index = -1;2094 stbir_info->ring_buffer_first_scanline = 0;2095 stbir_info->ring_buffer_last_scanline = 0;2096 break;2097 }2098 else2099 {2100 stbir_info->ring_buffer_first_scanline++;2101 stbir_info->ring_buffer_begin_index = (stbir_info->ring_buffer_begin_index + 1) % stbir_info->ring_buffer_num_entries;2102 }2103 }2104 }21052106 // Load in new ones.2107 if (stbir_info->ring_buffer_begin_index < 0)2108 stbir__decode_and_resample_upsample(stbir_info, in_first_scanline);21092110 while (in_last_scanline > stbir_info->ring_buffer_last_scanline)2111 stbir__decode_and_resample_upsample(stbir_info, stbir_info->ring_buffer_last_scanline + 1);21122113 // Now all buffers should be ready to write a row of vertical sampling.2114 stbir__resample_vertical_upsample(stbir_info, y);21152116 STBIR_PROGRESS_REPORT((float)y / stbir_info->output_h);2117 }2118}21192120static void stbir__empty_ring_buffer(stbir__info* stbir_info, int first_necessary_scanline)2121{2122 int output_stride_bytes = stbir_info->output_stride_bytes;2123 int channels = stbir_info->channels;2124 int alpha_channel = stbir_info->alpha_channel;2125 int type = stbir_info->type;2126 int colorspace = stbir_info->colorspace;2127 int output_w = stbir_info->output_w;2128 void* output_data = stbir_info->output_data;2129 int decode = STBIR__DECODE(type, colorspace);21302131 float* ring_buffer = stbir_info->ring_buffer;2132 int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float);21332134 if (stbir_info->ring_buffer_begin_index >= 0)2135 {2136 // Get rid of whatever we don't need anymore.2137 while (first_necessary_scanline > stbir_info->ring_buffer_first_scanline)2138 {2139 if (stbir_info->ring_buffer_first_scanline >= 0 && stbir_info->ring_buffer_first_scanline < stbir_info->output_h)2140 {2141 int output_row_start = stbir_info->ring_buffer_first_scanline * output_stride_bytes;2142 float* ring_buffer_entry = stbir__get_ring_buffer_entry(ring_buffer, stbir_info->ring_buffer_begin_index, ring_buffer_length);2143 stbir__encode_scanline(stbir_info, output_w, (char *) output_data + output_row_start, ring_buffer_entry, channels, alpha_channel, decode);2144 STBIR_PROGRESS_REPORT((float)stbir_info->ring_buffer_first_scanline / stbir_info->output_h);2145 }21462147 if (stbir_info->ring_buffer_first_scanline == stbir_info->ring_buffer_last_scanline)2148 {2149 // We just popped the last scanline off the ring buffer.2150 // Reset it to the empty state.2151 stbir_info->ring_buffer_begin_index = -1;2152 stbir_info->ring_buffer_first_scanline = 0;2153 stbir_info->ring_buffer_last_scanline = 0;2154 break;2155 }2156 else2157 {2158 stbir_info->ring_buffer_first_scanline++;2159 stbir_info->ring_buffer_begin_index = (stbir_info->ring_buffer_begin_index + 1) % stbir_info->ring_buffer_num_entries;2160 }2161 }2162 }2163}21642165static void stbir__buffer_loop_downsample(stbir__info* stbir_info)2166{2167 int y;2168 float scale_ratio = stbir_info->vertical_scale;2169 int output_h = stbir_info->output_h;2170 float in_pixels_radius = stbir__filter_info_table[stbir_info->vertical_filter].support(scale_ratio) / scale_ratio;2171 int pixel_margin = stbir_info->vertical_filter_pixel_margin;2172 int max_y = stbir_info->input_h + pixel_margin;21732174 STBIR_ASSERT(!stbir__use_height_upsampling(stbir_info));21752176 for (y = -pixel_margin; y < max_y; y++)2177 {2178 float out_center_of_in; // Center of the current out scanline in the in scanline space2179 int out_first_scanline, out_last_scanline;21802181 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);21822183 STBIR_ASSERT(out_last_scanline - out_first_scanline + 1 <= stbir_info->ring_buffer_num_entries);21842185 if (out_last_scanline < 0 || out_first_scanline >= output_h)2186 continue;21872188 stbir__empty_ring_buffer(stbir_info, out_first_scanline);21892190 stbir__decode_and_resample_downsample(stbir_info, y);21912192 // Load in new ones.2193 if (stbir_info->ring_buffer_begin_index < 0)2194 stbir__add_empty_ring_buffer_entry(stbir_info, out_first_scanline);21952196 while (out_last_scanline > stbir_info->ring_buffer_last_scanline)2197 stbir__add_empty_ring_buffer_entry(stbir_info, stbir_info->ring_buffer_last_scanline + 1);21982199 // Now the horizontal buffer is ready to write to all ring buffer rows.2200 stbir__resample_vertical_downsample(stbir_info, y);2201 }22022203 stbir__empty_ring_buffer(stbir_info, stbir_info->output_h);2204}22052206static void stbir__setup(stbir__info *info, int input_w, int input_h, int output_w, int output_h, int channels)2207{2208 info->input_w = input_w;2209 info->input_h = input_h;2210 info->output_w = output_w;2211 info->output_h = output_h;2212 info->channels = channels;2213}22142215static void stbir__calculate_transform(stbir__info *info, float s0, float t0, float s1, float t1, float *transform)2216{2217 info->s0 = s0;2218 info->t0 = t0;2219 info->s1 = s1;2220 info->t1 = t1;22212222 if (transform)2223 {2224 info->horizontal_scale = transform[0];2225 info->vertical_scale = transform[1];2226 info->horizontal_shift = transform[2];2227 info->vertical_shift = transform[3];2228 }2229 else2230 {2231 info->horizontal_scale = ((float)info->output_w / info->input_w) / (s1 - s0);2232 info->vertical_scale = ((float)info->output_h / info->input_h) / (t1 - t0);22332234 info->horizontal_shift = s0 * info->output_w / (s1 - s0);2235 info->vertical_shift = t0 * info->output_h / (t1 - t0);2236 }2237}22382239static void stbir__choose_filter(stbir__info *info, stbir_filter h_filter, stbir_filter v_filter)2240{2241 if (h_filter == 0)2242 h_filter = stbir__use_upsampling(info->horizontal_scale) ? STBIR_DEFAULT_FILTER_UPSAMPLE : STBIR_DEFAULT_FILTER_DOWNSAMPLE;2243 if (v_filter == 0)2244 v_filter = stbir__use_upsampling(info->vertical_scale) ? STBIR_DEFAULT_FILTER_UPSAMPLE : STBIR_DEFAULT_FILTER_DOWNSAMPLE;2245 info->horizontal_filter = h_filter;2246 info->vertical_filter = v_filter;2247}22482249static stbir_uint32 stbir__calculate_memory(stbir__info *info)2250{2251 int pixel_margin = stbir__get_filter_pixel_margin(info->horizontal_filter, info->horizontal_scale);2252 int filter_height = stbir__get_filter_pixel_width(info->vertical_filter, info->vertical_scale);22532254 info->horizontal_num_contributors = stbir__get_contributors(info->horizontal_scale, info->horizontal_filter, info->input_w, info->output_w);2255 info->vertical_num_contributors = stbir__get_contributors(info->vertical_scale , info->vertical_filter , info->input_h, info->output_h);22562257 // One extra entry because floating point precision problems sometimes cause an extra to be necessary.2258 info->ring_buffer_num_entries = filter_height + 1;22592260 info->horizontal_contributors_size = info->horizontal_num_contributors * sizeof(stbir__contributors);2261 info->horizontal_coefficients_size = stbir__get_total_horizontal_coefficients(info) * sizeof(float);2262 info->vertical_contributors_size = info->vertical_num_contributors * sizeof(stbir__contributors);2263 info->vertical_coefficients_size = stbir__get_total_vertical_coefficients(info) * sizeof(float);2264 info->decode_buffer_size = (info->input_w + pixel_margin * 2) * info->channels * sizeof(float);2265 info->horizontal_buffer_size = info->output_w * info->channels * sizeof(float);2266 info->ring_buffer_size = info->output_w * info->channels * info->ring_buffer_num_entries * sizeof(float);2267 info->encode_buffer_size = info->output_w * info->channels * sizeof(float);22682269 STBIR_ASSERT(info->horizontal_filter != 0);2270 STBIR_ASSERT(info->horizontal_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late2271 STBIR_ASSERT(info->vertical_filter != 0);2272 STBIR_ASSERT(info->vertical_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late22732274 if (stbir__use_height_upsampling(info))2275 // The horizontal buffer is for when we're downsampling the height and we2276 // can't output the result of sampling the decode buffer directly into the2277 // ring buffers.2278 info->horizontal_buffer_size = 0;2279 else2280 // The encode buffer is to retain precision in the height upsampling method2281 // and isn't used when height downsampling.2282 info->encode_buffer_size = 0;22832284 return info->horizontal_contributors_size + info->horizontal_coefficients_size2285 + info->vertical_contributors_size + info->vertical_coefficients_size2286 + info->decode_buffer_size + info->horizontal_buffer_size2287 + info->ring_buffer_size + info->encode_buffer_size;2288}22892290static int stbir__resize_allocated(stbir__info *info,2291 const void* input_data, int input_stride_in_bytes,2292 void* output_data, int output_stride_in_bytes,2293 int alpha_channel, stbir_uint32 flags, stbir_datatype type,2294 stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace,2295 void* tempmem, size_t tempmem_size_in_bytes)2296{2297 size_t memory_required = stbir__calculate_memory(info);22982299 int width_stride_input = input_stride_in_bytes ? input_stride_in_bytes : info->channels * info->input_w * stbir__type_size[type];2300 int width_stride_output = output_stride_in_bytes ? output_stride_in_bytes : info->channels * info->output_w * stbir__type_size[type];23012302#ifdef STBIR_DEBUG_OVERWRITE_TEST2303#define OVERWRITE_ARRAY_SIZE 82304 unsigned char overwrite_output_before_pre[OVERWRITE_ARRAY_SIZE];2305 unsigned char overwrite_tempmem_before_pre[OVERWRITE_ARRAY_SIZE];2306 unsigned char overwrite_output_after_pre[OVERWRITE_ARRAY_SIZE];2307 unsigned char overwrite_tempmem_after_pre[OVERWRITE_ARRAY_SIZE];23082309 size_t begin_forbidden = width_stride_output * (info->output_h - 1) + info->output_w * info->channels * stbir__type_size[type];2310 memcpy(overwrite_output_before_pre, &((unsigned char*)output_data)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE);2311 memcpy(overwrite_output_after_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE);2312 memcpy(overwrite_tempmem_before_pre, &((unsigned char*)tempmem)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE);2313 memcpy(overwrite_tempmem_after_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE);2314#endif23152316 STBIR_ASSERT(info->channels >= 0);2317 STBIR_ASSERT(info->channels <= STBIR_MAX_CHANNELS);23182319 if (info->channels < 0 || info->channels > STBIR_MAX_CHANNELS)2320 return 0;23212322 STBIR_ASSERT(info->horizontal_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));2323 STBIR_ASSERT(info->vertical_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));23242325 if (info->horizontal_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table))2326 return 0;2327 if (info->vertical_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table))2328 return 0;23292330 if (alpha_channel < 0)2331 flags |= STBIR_FLAG_ALPHA_USES_COLORSPACE | STBIR_FLAG_ALPHA_PREMULTIPLIED;23322333 if (!(flags&STBIR_FLAG_ALPHA_USES_COLORSPACE) || !(flags&STBIR_FLAG_ALPHA_PREMULTIPLIED)) {2334 STBIR_ASSERT(alpha_channel >= 0 && alpha_channel < info->channels);2335 }23362337 if (alpha_channel >= info->channels)2338 return 0;23392340 STBIR_ASSERT(tempmem);23412342 if (!tempmem)2343 return 0;23442345 STBIR_ASSERT(tempmem_size_in_bytes >= memory_required);23462347 if (tempmem_size_in_bytes < memory_required)2348 return 0;23492350 memset(tempmem, 0, tempmem_size_in_bytes);23512352 info->input_data = input_data;2353 info->input_stride_bytes = width_stride_input;23542355 info->output_data = output_data;2356 info->output_stride_bytes = width_stride_output;23572358 info->alpha_channel = alpha_channel;2359 info->flags = flags;2360 info->type = type;2361 info->edge_horizontal = edge_horizontal;2362 info->edge_vertical = edge_vertical;2363 info->colorspace = colorspace;23642365 info->horizontal_coefficient_width = stbir__get_coefficient_width (info->horizontal_filter, info->horizontal_scale);2366 info->vertical_coefficient_width = stbir__get_coefficient_width (info->vertical_filter , info->vertical_scale );2367 info->horizontal_filter_pixel_width = stbir__get_filter_pixel_width (info->horizontal_filter, info->horizontal_scale);2368 info->vertical_filter_pixel_width = stbir__get_filter_pixel_width (info->vertical_filter , info->vertical_scale );2369 info->horizontal_filter_pixel_margin = stbir__get_filter_pixel_margin(info->horizontal_filter, info->horizontal_scale);2370 info->vertical_filter_pixel_margin = stbir__get_filter_pixel_margin(info->vertical_filter , info->vertical_scale );23712372 info->ring_buffer_length_bytes = info->output_w * info->channels * sizeof(float);2373 info->decode_buffer_pixels = info->input_w + info->horizontal_filter_pixel_margin * 2;23742375#define STBIR__NEXT_MEMPTR(current, newtype) (newtype*)(((unsigned char*)current) + current##_size)23762377 info->horizontal_contributors = (stbir__contributors *) tempmem;2378 info->horizontal_coefficients = STBIR__NEXT_MEMPTR(info->horizontal_contributors, float);2379 info->vertical_contributors = STBIR__NEXT_MEMPTR(info->horizontal_coefficients, stbir__contributors);2380 info->vertical_coefficients = STBIR__NEXT_MEMPTR(info->vertical_contributors, float);2381 info->decode_buffer = STBIR__NEXT_MEMPTR(info->vertical_coefficients, float);23822383 if (stbir__use_height_upsampling(info))2384 {2385 info->horizontal_buffer = NULL;2386 info->ring_buffer = STBIR__NEXT_MEMPTR(info->decode_buffer, float);2387 info->encode_buffer = STBIR__NEXT_MEMPTR(info->ring_buffer, float);23882389 STBIR_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->encode_buffer, unsigned char) == (size_t)tempmem + tempmem_size_in_bytes);2390 }2391 else2392 {2393 info->horizontal_buffer = STBIR__NEXT_MEMPTR(info->decode_buffer, float);2394 info->ring_buffer = STBIR__NEXT_MEMPTR(info->horizontal_buffer, float);2395 info->encode_buffer = NULL;23962397 STBIR_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->ring_buffer, unsigned char) == (size_t)tempmem + tempmem_size_in_bytes);2398 }23992400#undef STBIR__NEXT_MEMPTR24012402 // This signals that the ring buffer is empty2403 info->ring_buffer_begin_index = -1;24042405 stbir__calculate_filters(info->horizontal_contributors, info->horizontal_coefficients, info->horizontal_filter, info->horizontal_scale, info->horizontal_shift, info->input_w, info->output_w);2406 stbir__calculate_filters(info->vertical_contributors, info->vertical_coefficients, info->vertical_filter, info->vertical_scale, info->vertical_shift, info->input_h, info->output_h);24072408 STBIR_PROGRESS_REPORT(0);24092410 if (stbir__use_height_upsampling(info))2411 stbir__buffer_loop_upsample(info);2412 else2413 stbir__buffer_loop_downsample(info);24142415 STBIR_PROGRESS_REPORT(1);24162417#ifdef STBIR_DEBUG_OVERWRITE_TEST2418 STBIR_ASSERT(memcmp(overwrite_output_before_pre, &((unsigned char*)output_data)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE) == 0);2419 STBIR_ASSERT(memcmp(overwrite_output_after_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE) == 0);2420 STBIR_ASSERT(memcmp(overwrite_tempmem_before_pre, &((unsigned char*)tempmem)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE) == 0);2421 STBIR_ASSERT(memcmp(overwrite_tempmem_after_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE) == 0);2422#endif24232424 return 1;2425}242624272428static int stbir__resize_arbitrary(2429 void *alloc_context,2430 const void* input_data, int input_w, int input_h, int input_stride_in_bytes,2431 void* output_data, int output_w, int output_h, int output_stride_in_bytes,2432 float s0, float t0, float s1, float t1, float *transform,2433 int channels, int alpha_channel, stbir_uint32 flags, stbir_datatype type,2434 stbir_filter h_filter, stbir_filter v_filter,2435 stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace)2436{2437 stbir__info info;2438 int result;2439 size_t memory_required;2440 void* extra_memory;24412442 stbir__setup(&info, input_w, input_h, output_w, output_h, channels);2443 stbir__calculate_transform(&info, s0,t0,s1,t1,transform);2444 stbir__choose_filter(&info, h_filter, v_filter);2445 memory_required = stbir__calculate_memory(&info);2446 extra_memory = STBIR_MALLOC(memory_required, alloc_context);24472448 if (!extra_memory)2449 return 0;24502451 result = stbir__resize_allocated(&info, input_data, input_stride_in_bytes,2452 output_data, output_stride_in_bytes,2453 alpha_channel, flags, type,2454 edge_horizontal, edge_vertical,2455 colorspace, extra_memory, memory_required);24562457 STBIR_FREE(extra_memory, alloc_context);24582459 return result;2460}24612462STBIRDEF int stbir_resize_uint8( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2463 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2464 int num_channels)2465{2466 return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,2467 output_pixels, output_w, output_h, output_stride_in_bytes,2468 0,0,1,1,NULL,num_channels,-1,0, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,2469 STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR);2470}24712472STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2473 float *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2474 int num_channels)2475{2476 return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,2477 output_pixels, output_w, output_h, output_stride_in_bytes,2478 0,0,1,1,NULL,num_channels,-1,0, STBIR_TYPE_FLOAT, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,2479 STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR);2480}24812482STBIRDEF int stbir_resize_uint8_srgb(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2483 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2484 int num_channels, int alpha_channel, int flags)2485{2486 return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,2487 output_pixels, output_w, output_h, output_stride_in_bytes,2488 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,2489 STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);2490}24912492STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2493 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2494 int num_channels, int alpha_channel, int flags,2495 stbir_edge edge_wrap_mode)2496{2497 return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,2498 output_pixels, output_w, output_h, output_stride_in_bytes,2499 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,2500 edge_wrap_mode, edge_wrap_mode, STBIR_COLORSPACE_SRGB);2501}25022503STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2504 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2505 int num_channels, int alpha_channel, int flags,2506 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,2507 void *alloc_context)2508{2509 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2510 output_pixels, output_w, output_h, output_stride_in_bytes,2511 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, filter, filter,2512 edge_wrap_mode, edge_wrap_mode, space);2513}25142515STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2516 stbir_uint16 *output_pixels , int output_w, int output_h, int output_stride_in_bytes,2517 int num_channels, int alpha_channel, int flags,2518 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,2519 void *alloc_context)2520{2521 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2522 output_pixels, output_w, output_h, output_stride_in_bytes,2523 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT16, filter, filter,2524 edge_wrap_mode, edge_wrap_mode, space);2525}252625272528STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2529 float *output_pixels , int output_w, int output_h, int output_stride_in_bytes,2530 int num_channels, int alpha_channel, int flags,2531 stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,2532 void *alloc_context)2533{2534 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2535 output_pixels, output_w, output_h, output_stride_in_bytes,2536 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_FLOAT, filter, filter,2537 edge_wrap_mode, edge_wrap_mode, space);2538}253925402541STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2542 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2543 stbir_datatype datatype,2544 int num_channels, int alpha_channel, int flags,2545 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,2546 stbir_filter filter_horizontal, stbir_filter filter_vertical,2547 stbir_colorspace space, void *alloc_context)2548{2549 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2550 output_pixels, output_w, output_h, output_stride_in_bytes,2551 0,0,1,1,NULL,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical,2552 edge_mode_horizontal, edge_mode_vertical, space);2553}255425552556STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2557 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2558 stbir_datatype datatype,2559 int num_channels, int alpha_channel, int flags,2560 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,2561 stbir_filter filter_horizontal, stbir_filter filter_vertical,2562 stbir_colorspace space, void *alloc_context,2563 float x_scale, float y_scale,2564 float x_offset, float y_offset)2565{2566 float transform[4];2567 transform[0] = x_scale;2568 transform[1] = y_scale;2569 transform[2] = x_offset;2570 transform[3] = y_offset;2571 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2572 output_pixels, output_w, output_h, output_stride_in_bytes,2573 0,0,1,1,transform,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical,2574 edge_mode_horizontal, edge_mode_vertical, space);2575}25762577STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,2578 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,2579 stbir_datatype datatype,2580 int num_channels, int alpha_channel, int flags,2581 stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,2582 stbir_filter filter_horizontal, stbir_filter filter_vertical,2583 stbir_colorspace space, void *alloc_context,2584 float s0, float t0, float s1, float t1)2585{2586 return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,2587 output_pixels, output_w, output_h, output_stride_in_bytes,2588 s0,t0,s1,t1,NULL,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical,2589 edge_mode_horizontal, edge_mode_vertical, space);2590}25912592#endif // STB_IMAGE_RESIZE_IMPLEMENTATION25932594/*2595------------------------------------------------------------------------------2596This software is available under 2 licenses -- choose whichever you prefer.2597------------------------------------------------------------------------------2598ALTERNATIVE A - MIT License2599Copyright (c) 2017 Sean Barrett2600Permission is hereby granted, free of charge, to any person obtaining a copy of2601this software and associated documentation files (the "Software"), to deal in2602the Software without restriction, including without limitation the rights to2603use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies2604of the Software, and to permit persons to whom the Software is furnished to do2605so, subject to the following conditions:2606The above copyright notice and this permission notice shall be included in all2607copies or substantial portions of the Software.2608THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR2609IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,2610FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE2611AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER2612LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,2613OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE2614SOFTWARE.2615------------------------------------------------------------------------------2616ALTERNATIVE B - Public Domain (www.unlicense.org)2617This is free and unencumbered software released into the public domain.2618Anyone is free to copy, modify, publish, use, compile, sell, or distribute this2619software, either in source code form or as a compiled binary, for any purpose,2620commercial or non-commercial, and by any means.2621In jurisdictions that recognize copyright laws, the author or authors of this2622software dedicate any and all copyright interest in the software to the public2623domain. We make this dedication for the benefit of the public at large and to2624the detriment of our heirs and successors. We intend this dedication to be an2625overt act of relinquishment in perpetuity of all present and future rights to2626this software under copyright law.2627THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR2628IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,2629FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE2630AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN2631ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION2632WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.2633------------------------------------------------------------------------------2634*/
added:stb_image_resize2.h+10303 added
1/* stb_image_resize2 - v2.00 - public domain image resizing2 3 by Jeff Roberts (v2) and Jorge L Rodriguez 4 http://github.com/nothings/stb56 Can be threaded with the extended API. SSE2, AVX, Neon and WASM SIMD support. Only 7 scaling and translation is supported, no rotations or shears.89 COMPILING & LINKING10 In one C/C++ file that #includes this file, do this:11 #define STB_IMAGE_RESIZE_IMPLEMENTATION12 before the #include. That will create the implementation in that file.1314 PORTING FROM VERSION 11516 The API has changed. You can continue to use the old version of stb_image_resize.h,17 which is available in the "deprecated/" directory.1819 If you're using the old simple-to-use API, porting is straightforward.20 (For more advanced APIs, read the documentation.)2122 stbir_resize_uint8():23 - call `stbir_resize_uint8_linear`, cast channel count to `stbir_pixel_layout`2425 stbir_resize_float():26 - call `stbir_resize_float_linear`, cast channel count to `stbir_pixel_layout`2728 stbir_resize_uint8_srgb():29 - function name is unchanged30 - cast channel count to `stbir_pixel_layout`31 - above is sufficient unless your image has alpha and it's not RGBA/BGRA32 - in that case, follow the below instructions for stbir_resize_uint8_srgb_edgemode3334 stbir_resize_uint8_srgb_edgemode()35 - switch to the "medium complexity" API36 - stbir_resize(), very similar API but a few more parameters:37 - pixel_layout: cast channel count to `stbir_pixel_layout`38 - data_type: STBIR_TYPE_UINT8_SRGB39 - edge: unchanged (STBIR_EDGE_WRAP, etc.)40 - filter: STBIR_FILTER_DEFAULT41 - which channel is alpha is specified in stbir_pixel_layout, see enum for details4243 EASY API CALLS:44 Easy API downsamples w/Mitchell filter, upsamples w/cubic interpolation, clamps to edge.4546 stbir_resize_uint8_srgb( input_pixels, input_w, input_h, input_stride_in_bytes,47 output_pixels, output_w, output_h, output_stride_in_bytes,48 pixel_layout_enum )4950 stbir_resize_uint8_linear( input_pixels, input_w, input_h, input_stride_in_bytes,51 output_pixels, output_w, output_h, output_stride_in_bytes,52 pixel_layout_enum )5354 stbir_resize_float_linear( input_pixels, input_w, input_h, input_stride_in_bytes,55 output_pixels, output_w, output_h, output_stride_in_bytes,56 pixel_layout_enum )5758 If you pass NULL or zero for the output_pixels, we will allocate the output buffer59 for you and return it from the function (free with free() or STBIR_FREE).60 As a special case, XX_stride_in_bytes of 0 means packed continuously in memory.6162 API LEVELS63 There are three levels of API - easy-to-use, medium-complexity and extended-complexity.6465 See the "header file" section of the source for API documentation.6667 ADDITIONAL DOCUMENTATION6869 MEMORY ALLOCATION70 By default, we use malloc and free for memory allocation. To override the 71 memory allocation, before the implementation #include, add a:7273 #define STBIR_MALLOC(size,user_data) ...74 #define STBIR_FREE(ptr,user_data) ...7576 Each resize makes exactly one call to malloc/free (unless you use the 77 extended API where you can do one allocation for many resizes). Under78 address sanitizer, we do separate allocations to find overread/writes.7980 PERFORMANCE81 This library was written with an emphasis on performance. When testing82 stb_image_resize with RGBA, the fastest mode is STBIR_4CHANNEL with 83 STBIR_TYPE_UINT8 pixels and CLAMPed edges (which is what many other resize84 libs do by default). Also, make sure SIMD is turned on of course (default 85 for 64-bit targets). Avoid WRAP edge mode if you want the fastest speed.8687 This library also comes with profiling built-in. If you define STBIR_PROFILE,88 you can use the advanced API and get low-level profiling information by 89 calling stbir_resize_extended_profile_info() or stbir_resize_split_profile_info()90 after a resize.9192 SIMD93 Most of the routines have optimized SSE2, AVX, NEON and WASM versions. 9495 On Microsoft compilers, we automatically turn on SIMD for 64-bit x64 and 96 ARM; for 32-bit x86 and ARM, you select SIMD mode by defining STBIR_SSE2 or 97 STBIR_NEON. For AVX and AVX2, we auto-select it by detecting the /arch:AVX98 or /arch:AVX2 switches. You can also always manually turn SSE2, AVX or AVX2 99 support on by defining STBIR_SSE2, STBIR_AVX or STBIR_AVX2.100101 On Linux, SSE2 and Neon is on by default for 64-bit x64 or ARM64. For 32-bit,102 we select x86 SIMD mode by whether you have -msse2, -mavx or -mavx2 enabled103 on the command line. For 32-bit ARM, you must pass -mfpu=neon-vfpv4 for both104 clang and GCC, but GCC also requires an additional -mfp16-format=ieee to 105 automatically enable NEON.106107 On x86 platforms, you can also define STBIR_FP16C to turn on FP16C instructions108 for converting back and forth to half-floats. This is autoselected when we109 are using AVX2. Clang and GCC also require the -mf16c switch. ARM always uses 110 the built-in half float hardware NEON instructions. 111112 You can also tell us to use multiply-add instructions with STBIR_USE_FMA. 113 Because x86 doesn't always have fma, we turn it off by default to maintain114 determinism across all platforms. If you don't care about non-FMA determinism115 and are willing to restrict yourself to more recent x86 CPUs (around the AVX 116 timeframe), then fma will give you around a 15% speedup.117118 You can force off SIMD in all cases by defining STBIR_NO_SIMD. You can turn119 off AVX or AVX2 specifically with STBIR_NO_AVX or STBIR_NO_AVX2. AVX is 10%120 to 40% faster, and AVX2 is generally another 12%.121 122 ALPHA CHANNEL123 Most of the resizing functions provide the ability to control how the alpha 124 channel of an image is processed.125126 When alpha represents transparency, it is important that when combining127 colors with filtering, the pixels should not be treated equally; they128 should use a weighted average based on their alpha values. For example,129 if a pixel is 1% opaque bright green and another pixel is 99% opaque130 black and you average them, the average will be 50% opaque, but the131 unweighted average and will be a middling green color, while the weighted132 average will be nearly black. This means the unweighted version introduced133 green energy that didn't exist in the source image.134135 (If you want to know why this makes sense, you can work out the math for136 the following: consider what happens if you alpha composite a source image137 over a fixed color and then average the output, vs. if you average the138 source image pixels and then composite that over the same fixed color.139 Only the weighted average produces the same result as the ground truth140 composite-then-average result.)141142 Therefore, it is in general best to "alpha weight" the pixels when applying143 filters to them. This essentially means multiplying the colors by the alpha144 values before combining them, and then dividing by the alpha value at the145 end.146147 The computer graphics industry introduced a technique called "premultiplied148 alpha" or "associated alpha" in which image colors are stored in image files149 already multiplied by their alpha. This saves some math when compositing,150 and also avoids the need to divide by the alpha at the end (which is quite151 inefficient). However, while premultiplied alpha is common in the movie CGI152 industry, it is not commonplace in other industries like videogames, and most153 consumer file formats are generally expected to contain not-premultiplied154 colors. For example, Photoshop saves PNG files "unpremultiplied", and web155 browsers like Chrome and Firefox expect PNG images to be unpremultiplied.156157 Note that there are three possibilities that might describe your image158 and resize expectation:159160 1. images are not premultiplied, alpha weighting is desired161 2. images are not premultiplied, alpha weighting is not desired162 3. images are premultiplied163164 Both case #2 and case #3 require the exact same math: no alpha weighting165 should be applied or removed. Only case 1 requires extra math operations;166 the other two cases can be handled identically.167168 stb_image_resize expects case #1 by default, applying alpha weighting to169 images, expecting the input images to be unpremultiplied. This is what the170 COLOR+ALPHA buffer types tell the resizer to do. 171172 When you use the pixel layouts STBIR_RGBA, STBIR_BGRA, STBIR_ARGB, 173 STBIR_ABGR, STBIR_RX, or STBIR_XR you are telling us that the pixels are 174 non-premultiplied. In these cases, the resizer will alpha weight the colors 175 (effectively creating the premultiplied image), do the filtering, and then 176 convert back to non-premult on exit.177178 When you use the pixel layouts STBIR_RGBA_PM, STBIR_RGBA_PM, STBIR_RGBA_PM,179 STBIR_RGBA_PM, STBIR_RX_PM or STBIR_XR_PM, you are telling that the pixels 180 ARE premultiplied. In this case, the resizer doesn't have to do the 181 premultipling - it can filter directly on the input. This about twice as 182 fast as the non-premultiplied case, so it's the right option if your data is 183 already setup correctly.184185 When you use the pixel layout STBIR_4CHANNEL or STBIR_2CHANNEL, you are 186 telling us that there is no channel that represents transparency; it may be 187 RGB and some unrelated fourth channel that has been stored in the alpha 188 channel, but it is actually not alpha. No special processing will be 189 performed. 190191 The difference between the generic 4 or 2 channel layouts, and the 192 specialized _PM versions is with the _PM versions you are telling us that193 the data *is* alpha, just don't premultiply it. That's important when194 using SRGB pixel formats, we need to know where the alpha is, because195 it is converted linearly (rather than with the SRGB converters).196 197 Because alpha weighting produces the same effect as premultiplying, you198 even have the option with non-premultiplied inputs to let the resizer199 produce a premultiplied output. Because the intially computed alpha-weighted200 output image is effectively premultiplied, this is actually more performant201 than the normal path which un-premultiplies the output image as a final step.202203 Finally, when converting both in and out of non-premulitplied space (for204 example, when using STBIR_RGBA), we go to somewhat heroic measures to 205 ensure that areas with zero alpha value pixels get something reasonable 206 in the RGB values. If you don't care about the RGB values of zero alpha 207 pixels, you can call the stbir_set_non_pm_alpha_speed_over_quality() 208 function - this runs a premultiplied resize about 25% faster. That said,209 when you really care about speed, using premultiplied pixels for both in210 and out (STBIR_RGBA_PM, etc) much faster than both of these premultiplied211 options.212213 PIXEL LAYOUT CONVERSION214 The resizer can convert from some pixel layouts to others. When using the215 stbir_set_pixel_layouts(), you can, for example, specify STBIR_RGBA216 on input, and STBIR_ARGB on output, and it will re-organize the channels217 during the resize. Currently, you can only convert between two pixel218 layouts with the same number of channels.219220 DETERMINISM221 We commit to being deterministic (from x64 to ARM to scalar to SIMD, etc). 222 This requires compiling with fast-math off (using at least /fp:precise). 223 Also, you must turn off fp-contracting (which turns mult+adds into fmas)!224 We attempt to do this with pragmas, but with Clang, you usually want to add 225 -ffp-contract=off to the command line as well.226227 For 32-bit x86, you must use SSE and SSE2 codegen for determinism. That is, 228 if the scalar x87 unit gets used at all, we immediately lose determinism. 229 On Microsoft Visual Studio 2008 and earlier, from what we can tell there is230 no way to be deterministic in 32-bit x86 (some x87 always leaks in, even 231 with fp:strict). On 32-bit x86 GCC, determinism requires both -msse2 and 232 -fpmath=sse.233234 Note that we will not be deterministic with float data containing NaNs -235 the NaNs will propagate differently on different SIMD and platforms. 236237 If you turn on STBIR_USE_FMA, then we will be deterministic with other 238 fma targets, but we will differ from non-fma targets (this is unavoidable, 239 because a fma isn't simply an add with a mult - it also introduces a 240 rounding difference compared to non-fma instruction sequences. 241242 FLOAT PIXEL FORMAT RANGE243 Any range of values can be used for the non-alpha float data that you pass 244 in (0 to 1, -1 to 1, whatever). However, if you are inputting float values 245 but *outputting* bytes or shorts, you must use a range of 0 to 1 so that we 246 scale back properly. The alpha channel must also be 0 to 1 for any format 247 that does premultiplication prior to resizing. 248249 Note also that with float output, using filters with negative lobes, the 250 output filtered values might go slightly out of range. You can define 251 STBIR_FLOAT_LOW_CLAMP and/or STBIR_FLOAT_HIGH_CLAMP to specify the range 252 to clamp to on output, if that's important. 253254 MAX/MIN SCALE FACTORS255 The input pixel resolutions are in integers, and we do the internal pointer256 resolution in size_t sized integers. However, the scale ratio from input257 resolution to output resolution is calculated in float form. This means258 the effective possible scale ratio is limited to 24 bits (or 16 million259 to 1). As you get close to the size of the float resolution (again, 16260 million pixels wide or high), you might start seeing float inaccuracy261 issues in general in the pipeline. If you have to do extreme resizes,262 you can usually do this is multiple stages (using float intermediate263 buffers).264265 FLIPPED IMAGES266 Stride is just the delta from one scanline to the next. This means you can 267 use a negative stride to handle inverted images (point to the final 268 scanline and use a negative stride). You can invert the input or output,269 using negative strides.270271 DEFAULT FILTERS272 For functions which don't provide explicit control over what filters to 273 use, you can change the compile-time defaults with:274275 #define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_something276 #define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_something277278 See stbir_filter in the header-file section for the list of filters.279280 NEW FILTERS281 A number of 1D filter kernels are supplied. For a list of supported 282 filters, see the stbir_filter enum. You can install your own filters by 283 using the stbir_set_filter_callbacks function.284285 PROGRESS286 For interactive use with slow resize operations, you can use the the 287 scanline callbacks in the extended API. It would have to be a *very* large 288 image resample to need progress though - we're very fast.289290 CEIL and FLOOR291 In scalar mode, the only functions we use from math.h are ceilf and floorf, 292 but if you have your own versions, you can define the STBIR_CEILF(v) and 293 STBIR_FLOORF(v) macros and we'll use them instead. In SIMD, we just use294 our own versions.295296 ASSERT297 Define STBIR_ASSERT(boolval) to override assert() and not use assert.h298299 FUTURE TODOS300 * For polyphase integral filters, we just memcpy the coeffs to dupe301 them, but we should indirect and use the same coeff memory.302 * Add pixel layout conversions for sensible different channel counts303 (maybe, 1->3/4, 3->4, 4->1, 3->1).304 * For SIMD encode and decode scanline routines, do any pre-aligning305 for bad input/output buffer alignments and pitch?306 * For very wide scanlines, we should we do vertical strips to stay within307 L2 cache. Maybe do chunks of 1K pixels at a time. There would be 308 some pixel reconversion, but probably dwarfed by things falling out309 of cache. Probably also something possible with alternating between310 scattering and gathering at high resize scales?311 * Rewrite the coefficient generator to do many at once.312 * AVX-512 vertical kernels - worried about downclocking here.313 * Convert the reincludes to macros when we know they aren't changing.314 * Experiment with pivoting the horizontal and always using the315 vertical filters (which are faster, but perhaps not enough to overcome316 the pivot cost and the extra memory touches). Need to buffer the whole317 image so have to balance memory use.318 * Most of our code is internally function pointers, should we compile319 all the SIMD stuff always and dynamically dispatch? 320321 CONTRIBUTORS322 Jeff Roberts: 2.0 implementation, optimizations, SIMD323 Martins Mozeiko: NEON simd, WASM simd, clang and GCC whisperer.324 Fabian Giesen: half float and srgb converters325 Sean Barrett: API design, optimizations326 Jorge L Rodriguez: Original 1.0 implementation327 Aras Pranckevicius: bugfixes for 1.0328 Nathan Reed: warning fixes for 1.0329330 REVISIONS331 2.00 (2022-02-20) mostly new source: new api, optimizations, simd, vertical-first, etc 332 (2x-5x faster without simd, 4x-12x faster with simd)333 (in some cases, 20x to 40x faster - resizing to very small for example)334 0.96 (2019-03-04) fixed warnings335 0.95 (2017-07-23) fixed warnings336 0.94 (2017-03-18) fixed warnings337 0.93 (2017-03-03) fixed bug with certain combinations of heights338 0.92 (2017-01-02) fix integer overflow on large (>2GB) images339 0.91 (2016-04-02) fix warnings; fix handling of subpixel regions340 0.90 (2014-09-17) first released version341342 LICENSE343 See end of file for license information.344*/345346#if !defined(STB_IMAGE_RESIZE_DO_HORIZONTALS) && !defined(STB_IMAGE_RESIZE_DO_VERTICALS) && !defined(STB_IMAGE_RESIZE_DO_CODERS) // for internal re-includes347348#ifndef STBIR_INCLUDE_STB_IMAGE_RESIZE2_H349#define STBIR_INCLUDE_STB_IMAGE_RESIZE2_H350351#include <stddef.h>352#ifdef _MSC_VER353typedef unsigned char stbir_uint8;354typedef unsigned short stbir_uint16;355typedef unsigned int stbir_uint32;356typedef unsigned __int64 stbir_uint64;357#else358#include <stdint.h>359typedef uint8_t stbir_uint8;360typedef uint16_t stbir_uint16;361typedef uint32_t stbir_uint32;362typedef uint64_t stbir_uint64;363#endif364365#ifdef _M_IX86_FP366#if ( _M_IX86_FP >= 1 )367#ifndef STBIR_SSE368#define STBIR_SSE369#endif370#endif371#endif 372373#if defined(_x86_64) || defined( __x86_64__ ) || defined( _M_X64 ) || defined(__x86_64) || defined(_M_AMD64) || defined(__SSE2__) || defined(STBIR_SSE) || defined(STBIR_SSE2)374 #ifndef STBIR_SSE2375 #define STBIR_SSE2376 #endif377 #if defined(__AVX__) || defined(STBIR_AVX2)378 #ifndef STBIR_AVX379 #ifndef STBIR_NO_AVX380 #define STBIR_AVX381 #endif382 #endif383 #endif384 #if defined(__AVX2__) || defined(STBIR_AVX2)385 #ifndef STBIR_NO_AVX2386 #ifndef STBIR_AVX2 387 #define STBIR_AVX2388 #endif389 #if defined( _MSC_VER ) && !defined(__clang__)390 #ifndef STBIR_FP16C // FP16C instructions are on all AVX2 cpus, so we can autoselect it here on microsoft - clang needs -m16c391 #define STBIR_FP16C392 #endif393 #endif394 #endif395 #endif396 #ifdef __F16C__397 #ifndef STBIR_FP16C // turn on FP16C instructions if the define is set (for clang and gcc)398 #define STBIR_FP16C399 #endif400 #endif401#endif402403#if defined( _M_ARM64 ) || defined( __aarch64__ ) || defined( __arm64__ ) || defined(_M_ARM) || (__ARM_NEON_FP & 4) != 0 && __ARM_FP16_FORMAT_IEEE != 0404#ifndef STBIR_NEON405#define STBIR_NEON406#endif407#endif408409#if defined(_M_ARM)410#ifdef STBIR_USE_FMA411#undef STBIR_USE_FMA // no FMA for 32-bit arm on MSVC 412#endif413#endif414415#if defined(__wasm__) && defined(__wasm_simd128__)416#ifndef STBIR_WASM417#define STBIR_WASM418#endif419#endif420421#ifndef STBIRDEF422#ifdef STB_IMAGE_RESIZE_STATIC423#define STBIRDEF static424#else425#ifdef __cplusplus426#define STBIRDEF extern "C"427#else428#define STBIRDEF extern429#endif430#endif431#endif432433//////////////////////////////////////////////////////////////////////////////434//// start "header file" ///////////////////////////////////////////////////435//436// Easy-to-use API:437//438// * stride is the offset between successive rows of image data 439// in memory, in bytes. specify 0 for packed continuously in memory440// * colorspace is linear or sRGB as specified by function name441// * Uses the default filters442// * Uses edge mode clamped443// * returned result is 1 for success or 0 in case of an error.444445446// stbir_pixel_layout specifies:447// number of channels448// order of channels449// whether color is premultiplied by alpha450// for back compatibility, you can cast the old channel count to an stbir_pixel_layout451typedef enum 452{453 STBIR_BGR = 0, // 3-chan, with order specified (for channel flipping)454 STBIR_1CHANNEL = 1, 455 STBIR_2CHANNEL = 2,456 STBIR_RGB = 3, // 3-chan, with order specified (for channel flipping) 457 STBIR_RGBA = 4, // alpha formats, alpha is NOT premultiplied into color channels458459 STBIR_4CHANNEL = 5,460 STBIR_BGRA = 6,461 STBIR_ARGB = 7,462 STBIR_ABGR = 8,463 STBIR_RA = 9,464 STBIR_AR = 10,465466 STBIR_RGBA_PM = 11, // alpha formats, alpha is premultiplied into color channels467 STBIR_BGRA_PM = 12,468 STBIR_ARGB_PM = 13,469 STBIR_ABGR_PM = 14,470 STBIR_RA_PM = 15,471 STBIR_AR_PM = 16,472} stbir_pixel_layout;473474//===============================================================475// Simple-complexity API476//477// If output_pixels is NULL (0), then we will allocate the buffer and return it to you.478//--------------------------------479480STBIRDEF unsigned char * stbir_resize_uint8_srgb( const unsigned char *input_pixels , int input_w , int input_h, int input_stride_in_bytes,481 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,482 stbir_pixel_layout pixel_type );483484STBIRDEF unsigned char * stbir_resize_uint8_linear( const unsigned char *input_pixels , int input_w , int input_h, int input_stride_in_bytes,485 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,486 stbir_pixel_layout pixel_type );487488STBIRDEF float * stbir_resize_float_linear( const float *input_pixels , int input_w , int input_h, int input_stride_in_bytes,489 float *output_pixels, int output_w, int output_h, int output_stride_in_bytes,490 stbir_pixel_layout pixel_type );491//===============================================================492493//===============================================================494// Medium-complexity API495//496// This extends the easy-to-use API as follows:497//498// * Can specify the datatype - U8, U8_SRGB, U16, FLOAT, HALF_FLOAT499// * Edge wrap can selected explicitly500// * Filter can be selected explicitly501//--------------------------------502503typedef enum504{505 STBIR_EDGE_CLAMP = 0,506 STBIR_EDGE_REFLECT = 1,507 STBIR_EDGE_WRAP = 2, // this edge mode is slower and uses more memory508 STBIR_EDGE_ZERO = 3,509} stbir_edge;510511typedef enum512{513 STBIR_FILTER_DEFAULT = 0, // use same filter type that easy-to-use API chooses514 STBIR_FILTER_BOX = 1, // A trapezoid w/1-pixel wide ramps, same result as box for integer scale ratios515 STBIR_FILTER_TRIANGLE = 2, // On upsampling, produces same results as bilinear texture filtering516 STBIR_FILTER_CUBICBSPLINE = 3, // The cubic b-spline (aka Mitchell-Netrevalli with B=1,C=0), gaussian-esque517 STBIR_FILTER_CATMULLROM = 4, // An interpolating cubic spline518 STBIR_FILTER_MITCHELL = 5, // Mitchell-Netrevalli filter with B=1/3, C=1/3519 STBIR_FILTER_POINT_SAMPLE = 6, // Simple point sampling520 STBIR_FILTER_OTHER = 7, // User callback specified521} stbir_filter;522523typedef enum524{525 STBIR_TYPE_UINT8 = 0,526 STBIR_TYPE_UINT8_SRGB = 1,527 STBIR_TYPE_UINT8_SRGB_ALPHA = 2, // alpha channel, when present, should also be SRGB (this is very unusual)528 STBIR_TYPE_UINT16 = 3,529 STBIR_TYPE_FLOAT = 4,530 STBIR_TYPE_HALF_FLOAT = 5531} stbir_datatype;532533// medium api534STBIRDEF void * stbir_resize( const void *input_pixels , int input_w , int input_h, int input_stride_in_bytes,535 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,536 stbir_pixel_layout pixel_layout, stbir_datatype data_type,537 stbir_edge edge, stbir_filter filter );538//===============================================================539540541542//===============================================================543// Extended-complexity API544//545// This API exposes all resize functionality.546//547// * Separate filter types for each axis548// * Separate edge modes for each axis549// * Separate input and output data types550// * Can specify regions with subpixel correctness551// * Can specify alpha flags552// * Can specify a memory callback 553// * Can specify a callback data type for pixel input and output 554// * Can be threaded for a single resize555// * Can be used to resize many frames without recalculating the sampler info556//557// Use this API as follows:558// 1) Call the stbir_resize_init function on a local STBIR_RESIZE structure559// 2) Call any of the stbir_set functions560// 3) Optionally call stbir_build_samplers() if you are going to resample multiple times561// with the same input and output dimensions (like resizing video frames)562// 4) Resample by calling stbir_resize_extended().563// 5) Call stbir_free_samplers() if you called stbir_build_samplers()564//--------------------------------565566567// Types:568569// INPUT CALLBACK: this callback is used for input scanlines570typedef void const * stbir_input_callback( void * optional_output, void const * input_ptr, int num_pixels, int x, int y, void * context );571572// OUTPUT CALLBACK: this callback is used for output scanlines573typedef void stbir_output_callback( void const * output_ptr, int num_pixels, int y, void * context );574575// callbacks for user installed filters576typedef float stbir__kernel_callback( float x, float scale, void * user_data ); // centered at zero577typedef float stbir__support_callback( float scale, void * user_data );578579// internal structure with precomputed scaling580typedef struct stbir__info stbir__info; 581582typedef struct STBIR_RESIZE // use the stbir_resize_init and stbir_override functions to set these values for future compatibility583{584 void * user_data;585 void const * input_pixels;586 int input_w, input_h;587 double input_s0, input_t0, input_s1, input_t1;588 stbir_input_callback * input_cb;589 void * output_pixels;590 int output_w, output_h;591 int output_subx, output_suby, output_subw, output_subh;592 stbir_output_callback * output_cb;593 int input_stride_in_bytes;594 int output_stride_in_bytes;595 int splits;596 int fast_alpha;597 int needs_rebuild;598 int called_alloc;599 stbir_pixel_layout input_pixel_layout_public;600 stbir_pixel_layout output_pixel_layout_public;601 stbir_datatype input_data_type;602 stbir_datatype output_data_type;603 stbir_filter horizontal_filter, vertical_filter;604 stbir_edge horizontal_edge, vertical_edge;605 stbir__kernel_callback * horizontal_filter_kernel; stbir__support_callback * horizontal_filter_support;606 stbir__kernel_callback * vertical_filter_kernel; stbir__support_callback * vertical_filter_support;607 stbir__info * samplers; 608} STBIR_RESIZE;609610// extended complexity api611612613// First off, you must ALWAYS call stbir_resize_init on your resize structure before any of the other calls!614STBIRDEF void stbir_resize_init( STBIR_RESIZE * resize,615 const void *input_pixels, int input_w, int input_h, int input_stride_in_bytes, // stride can be zero616 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, // stride can be zero617 stbir_pixel_layout pixel_layout, stbir_datatype data_type );618619//===============================================================620// You can update these parameters any time after resize_init and there is no cost621//--------------------------------622623STBIRDEF void stbir_set_datatypes( STBIR_RESIZE * resize, stbir_datatype input_type, stbir_datatype output_type ); 624STBIRDEF void stbir_set_pixel_callbacks( STBIR_RESIZE * resize, stbir_input_callback * input_cb, stbir_output_callback * output_cb ); // no callbacks by default625STBIRDEF void stbir_set_user_data( STBIR_RESIZE * resize, void * user_data ); // pass back STBIR_RESIZE* by default626STBIRDEF void stbir_set_buffer_ptrs( STBIR_RESIZE * resize, const void * input_pixels, int input_stride_in_bytes, void * output_pixels, int output_stride_in_bytes );627628//===============================================================629630631//===============================================================632// If you call any of these functions, you will trigger a sampler rebuild!633//--------------------------------634635STBIRDEF int stbir_set_pixel_layouts( STBIR_RESIZE * resize, stbir_pixel_layout input_pixel_layout, stbir_pixel_layout output_pixel_layout ); // sets new buffer layouts636STBIRDEF int stbir_set_edgemodes( STBIR_RESIZE * resize, stbir_edge horizontal_edge, stbir_edge vertical_edge ); // CLAMP by default637638STBIRDEF int stbir_set_filters( STBIR_RESIZE * resize, stbir_filter horizontal_filter, stbir_filter vertical_filter ); // STBIR_DEFAULT_FILTER_UPSAMPLE/DOWNSAMPLE by default639STBIRDEF int stbir_set_filter_callbacks( STBIR_RESIZE * resize, stbir__kernel_callback * horizontal_filter, stbir__support_callback * horizontal_support, stbir__kernel_callback * vertical_filter, stbir__support_callback * vertical_support ); 640641STBIRDEF int stbir_set_pixel_subrect( STBIR_RESIZE * resize, int subx, int suby, int subw, int subh ); // sets both sub-regions (full regions by default)642STBIRDEF int stbir_set_input_subrect( STBIR_RESIZE * resize, double s0, double t0, double s1, double t1 ); // sets input sub-region (full region by default)643STBIRDEF int stbir_set_output_pixel_subrect( STBIR_RESIZE * resize, int subx, int suby, int subw, int subh ); // sets output sub-region (full region by default)644645// when inputting AND outputting non-premultiplied alpha pixels, we use a slower but higher quality technique646// that fills the zero alpha pixel's RGB values with something plausible. If you don't care about areas of647// zero alpha, you can call this function to get about a 25% speed improvement for STBIR_RGBA to STBIR_RGBA648// types of resizes.649STBIRDEF int stbir_set_non_pm_alpha_speed_over_quality( STBIR_RESIZE * resize, int non_pma_alpha_speed_over_quality );650//===============================================================651652653//===============================================================654// You can call build_samplers to prebuild all the internal data we need to resample.655// Then, if you call resize_extended many times with the same resize, you only pay the656// cost once.657// If you do call build_samplers, you MUST call free_samplers eventually.658//--------------------------------659660// This builds the samplers and does one allocation661STBIRDEF int stbir_build_samplers( STBIR_RESIZE * resize ); 662663// You MUST call this, if you call stbir_build_samplers or stbir_build_samplers_with_splits664STBIRDEF void stbir_free_samplers( STBIR_RESIZE * resize );665//===============================================================666667668// And this is the main function to perform the resize synchronously on one thread.669STBIRDEF int stbir_resize_extended( STBIR_RESIZE * resize );670671672//===============================================================673// Use these functions for multithreading.674// 1) You call stbir_build_samplers_with_splits first on the main thread675// 2) Then stbir_resize_with_split on each thread676// 3) stbir_free_samplers when done on the main thread677//--------------------------------678679// This will build samplers for threading.680// You can pass in the number of threads you'd like to use (try_splits).681// It returns the number of splits (threads) that you can call it with.682/// It might be less if the image resize can't be split up that many ways.683684STBIRDEF int stbir_build_samplers_with_splits( STBIR_RESIZE * resize, int try_splits ); 685686// This function does a split of the resizing (you call this fuction for each687// split, on multiple threads). A split is a piece of the output resize pixel space.688689// Note that you MUST call stbir_build_samplers_with_splits before stbir_resize_extended_split!690691// Usually, you will always call stbir_resize_split with split_start as the thread_index692// and "1" for the split_count.693// But, if you have a weird situation where you MIGHT want 8 threads, but sometimes694// only 4 threads, you can use 0,2,4,6 for the split_start's and use "2" for the 695// split_count each time to turn in into a 4 thread resize. (This is unusual).696697STBIRDEF int stbir_resize_extended_split( STBIR_RESIZE * resize, int split_start, int split_count ); 698//===============================================================699700701//===============================================================702// Pixel Callbacks info:703//--------------------------------704705// The input callback is super flexible - it calls you with the input address706// (based on the stride and base pointer), it gives you an optional_output707// pointer that you can fill, or you can just return your own pointer into708// your own data. 709//710// You can also do conversion from non-supported data types if necessary - in 711// this case, you ignore the input_ptr and just use the x and y parameters to 712// calculate your own input_ptr based on the size of each non-supported pixel.713// (Something like the third example below.)714//715// You can also install just an input or just an output callback by setting the716// callback that you don't want to zero.717//718// First example, progress: (getting a callback that you can monitor the progress):719// void const * my_callback( void * optional_output, void const * input_ptr, int num_pixels, int x, int y, void * context )720// {721// percentage_done = y / input_height;722// return input_ptr; // use buffer from call723// }724//725// Next example, copying: (copy from some other buffer or stream): 726// void const * my_callback( void * optional_output, void const * input_ptr, int num_pixels, int x, int y, void * context )727// {728// CopyOrStreamData( optional_output, other_data_src, num_pixels * pixel_width_in_bytes );729// return optional_output; // return the optional buffer that we filled730// }731//732// Third example, input another buffer without copying: (zero-copy from other buffer): 733// void const * my_callback( void * optional_output, void const * input_ptr, int num_pixels, int x, int y, void * context )734// {735// void * pixels = ( (char*) other_image_base ) + ( y * other_image_stride ) + ( x * other_pixel_width_in_bytes );736// return pixels; // return pointer to your data without copying737// }738//739//740// The output callback is considerably simpler - it just calls you so that you can dump741// out each scanline. You could even directly copy out to disk if you have a simple format742// like TGA or BMP. You can also convert to other output types here if you want.743//744// Simple example:745// void const * my_output( void * output_ptr, int num_pixels, int y, void * context )746// {747// percentage_done = y / output_height;748// fwrite( output_ptr, pixel_width_in_bytes, num_pixels, output_file );749// }750//===============================================================751752753754755//===============================================================756// optional built-in profiling API757//--------------------------------758759#ifdef STBIR_PROFILE760761typedef struct STBIR_PROFILE_INFO 762{763 stbir_uint64 total_clocks;764765 // how many clocks spent (of total_clocks) in the various resize routines, along with a string description766 // there are "resize_count" number of zones767 stbir_uint64 clocks[ 8 ];768 char const ** descriptions;769 770 // count of clocks and descriptions771 stbir_uint32 count;772} STBIR_PROFILE_INFO;773774// use after calling stbir_resize_extended (or stbir_build_samplers or stbir_build_samplers_with_splits)775STBIRDEF void stbir_resize_build_profile_info( STBIR_PROFILE_INFO * out_info, STBIR_RESIZE const * resize );776777// use after calling stbir_resize_extended778STBIRDEF void stbir_resize_extended_profile_info( STBIR_PROFILE_INFO * out_info, STBIR_RESIZE const * resize );779780// use after calling stbir_resize_extended_split781STBIRDEF void stbir_resize_split_profile_info( STBIR_PROFILE_INFO * out_info, STBIR_RESIZE const * resize, int split_start, int split_num );782783//===============================================================784785#endif786787788//// end header file /////////////////////////////////////////////////////789#endif // STBIR_INCLUDE_STB_IMAGE_RESIZE2_H790791#if defined(STB_IMAGE_RESIZE_IMPLEMENTATION) || defined(STB_IMAGE_RESIZE2_IMPLEMENTATION)792793#ifndef STBIR_ASSERT794#include <assert.h>795#define STBIR_ASSERT(x) assert(x)796#endif797798#ifndef STBIR_MALLOC799#include <stdlib.h>800#define STBIR_MALLOC(size,user_data) ((void)(user_data), malloc(size))801#define STBIR_FREE(ptr,user_data) ((void)(user_data), free(ptr))802// (we used the comma operator to evaluate user_data, to avoid "unused parameter" warnings)803#endif804805#ifdef _MSC_VER806807#define stbir__inline __forceinline808809#else810811#define stbir__inline __inline__812813// Clang address sanitizer814#if defined(__has_feature)815 #if __has_feature(address_sanitizer) || __has_feature(memory_sanitizer)816 #ifndef STBIR__SEPARATE_ALLOCATIONS817 #define STBIR__SEPARATE_ALLOCATIONS818 #endif819 #endif820#endif821822#endif823824// GCC and MSVC825#if defined(__SANITIZE_ADDRESS__)826 #ifndef STBIR__SEPARATE_ALLOCATIONS827 #define STBIR__SEPARATE_ALLOCATIONS828 #endif829#endif830831// Always turn off automatic FMA use - use STBIR_USE_FMA if you want.832// Otherwise, this is a determinism disaster.833#ifndef STBIR_DONT_CHANGE_FP_CONTRACT // override in case you don't want this behavior834#if defined(_MSC_VER) && !defined(__clang__)835#if _MSC_VER > 1200836#pragma fp_contract(off)837#endif838#elif defined(__GNUC__) && !defined(__clang__)839#pragma GCC optimize("fp-contract=off")840#else841#pragma STDC FP_CONTRACT OFF842#endif843#endif844845#ifdef _MSC_VER846#define STBIR__UNUSED(v) (void)(v)847#else848#define STBIR__UNUSED(v) (void)sizeof(v)849#endif850851#define STBIR__ARRAY_SIZE(a) (sizeof((a))/sizeof((a)[0]))852853854#ifndef STBIR_DEFAULT_FILTER_UPSAMPLE855#define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_CATMULLROM856#endif857858#ifndef STBIR_DEFAULT_FILTER_DOWNSAMPLE859#define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_MITCHELL860#endif861862863#ifndef STBIR__HEADER_FILENAME864#define STBIR__HEADER_FILENAME "stb_image_resize2.h"865#endif866867// the internal pixel layout enums are in a different order, so we can easily do range comparisons of types868// the public pixel layout is ordered in a way that if you cast num_channels (1-4) to the enum, you get something sensible 869typedef enum 870{871 STBIRI_1CHANNEL = 0,872 STBIRI_2CHANNEL = 1,873 STBIRI_RGB = 2,874 STBIRI_BGR = 3,875 STBIRI_4CHANNEL = 4,876 877 STBIRI_RGBA = 5,878 STBIRI_BGRA = 6,879 STBIRI_ARGB = 7,880 STBIRI_ABGR = 8,881 STBIRI_RA = 9,882 STBIRI_AR = 10,883884 STBIRI_RGBA_PM = 11,885 STBIRI_BGRA_PM = 12,886 STBIRI_ARGB_PM = 13,887 STBIRI_ABGR_PM = 14,888 STBIRI_RA_PM = 15,889 STBIRI_AR_PM = 16,890} stbir_internal_pixel_layout;891892// define the public pixel layouts to not compile inside the implementation (to avoid accidental use)893#define STBIR_BGR bad_dont_use_in_implementation894#define STBIR_1CHANNEL STBIR_BGR895#define STBIR_2CHANNEL STBIR_BGR896#define STBIR_RGB STBIR_BGR897#define STBIR_RGBA STBIR_BGR898#define STBIR_4CHANNEL STBIR_BGR899#define STBIR_BGRA STBIR_BGR900#define STBIR_ARGB STBIR_BGR901#define STBIR_ABGR STBIR_BGR902#define STBIR_RA STBIR_BGR903#define STBIR_AR STBIR_BGR904#define STBIR_RGBA_PM STBIR_BGR905#define STBIR_BGRA_PM STBIR_BGR906#define STBIR_ARGB_PM STBIR_BGR907#define STBIR_ABGR_PM STBIR_BGR908#define STBIR_RA_PM STBIR_BGR909#define STBIR_AR_PM STBIR_BGR910911// must match stbir_datatype912static unsigned char stbir__type_size[] = {913 1,1,1,2,4,2 // STBIR_TYPE_UINT8,STBIR_TYPE_UINT8_SRGB,STBIR_TYPE_UINT8_SRGB_ALPHA,STBIR_TYPE_UINT16,STBIR_TYPE_FLOAT,STBIR_TYPE_HALF_FLOAT914};915916// When gathering, the contributors are which source pixels contribute.917// When scattering, the contributors are which destination pixels are contributed to.918typedef struct919{920 int n0; // First contributing pixel921 int n1; // Last contributing pixel922} stbir__contributors;923924typedef struct925{926 int lowest; // First sample index for whole filter927 int highest; // Last sample index for whole filter928 int widest; // widest single set of samples for an output929} stbir__filter_extent_info;930931typedef struct932{933 int n0; // First pixel of decode buffer to write to934 int n1; // Last pixel of decode that will be written to935 int pixel_offset_for_input; // Pixel offset into input_scanline936} stbir__span;937938typedef struct stbir__scale_info939{940 int input_full_size;941 int output_sub_size;942 float scale;943 float inv_scale;944 float pixel_shift; // starting shift in output pixel space (in pixels)945 int scale_is_rational;946 stbir_uint32 scale_numerator, scale_denominator;947} stbir__scale_info;948949typedef struct950{951 stbir__contributors * contributors;952 float* coefficients;953 stbir__contributors * gather_prescatter_contributors;954 float * gather_prescatter_coefficients;955 stbir__scale_info scale_info;956 float support;957 stbir_filter filter_enum;958 stbir__kernel_callback * filter_kernel;959 stbir__support_callback * filter_support;960 stbir_edge edge;961 int coefficient_width;962 int filter_pixel_width;963 int filter_pixel_margin;964 int num_contributors;965 int contributors_size;966 int coefficients_size;967 stbir__filter_extent_info extent_info;968 int is_gather; // 0 = scatter, 1 = gather with scale >= 1, 2 = gather with scale < 1969 int gather_prescatter_num_contributors;970 int gather_prescatter_coefficient_width;971 int gather_prescatter_contributors_size;972 int gather_prescatter_coefficients_size;973} stbir__sampler;974975typedef struct976{977 stbir__contributors conservative;978 int edge_sizes[2]; // this can be less than filter_pixel_margin, if the filter and scaling falls off979 stbir__span spans[2]; // can be two spans, if doing input subrect with clamp mode WRAP980} stbir__extents;981982typedef struct 983{984#ifdef STBIR_PROFILE985 union986 {987 struct { stbir_uint64 total, looping, vertical, horizontal, decode, encode, alpha, unalpha; } named;988 stbir_uint64 array[8];989 } profile;990 stbir_uint64 * current_zone_excluded_ptr;991#endif992 float* decode_buffer;993994 int ring_buffer_first_scanline;995 int ring_buffer_last_scanline;996 int ring_buffer_begin_index; // first_scanline is at this index in the ring buffer997 int start_output_y, end_output_y;998 int start_input_y, end_input_y; // used in scatter only9991000 #ifdef STBIR__SEPARATE_ALLOCATIONS1001 float** ring_buffers; // one pointer for each ring buffer1002 #else1003 float* ring_buffer; // one big buffer that we index into1004 #endif10051006 float* vertical_buffer;10071008 char no_cache_straddle[64];1009} stbir__per_split_info;10101011typedef void stbir__decode_pixels_func( float * decode, int width_times_channels, void const * input );1012typedef void stbir__alpha_weight_func( float * decode_buffer, int width_times_channels );1013typedef void stbir__horizontal_gather_channels_func( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, 1014 stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width );1015typedef void stbir__alpha_unweight_func(float * encode_buffer, int width_times_channels );1016typedef void stbir__encode_pixels_func( void * output, int width_times_channels, float const * encode );10171018struct stbir__info1019{1020#ifdef STBIR_PROFILE1021 union1022 {1023 struct { stbir_uint64 total, build, alloc, horizontal, vertical, cleanup, pivot; } named;1024 stbir_uint64 array[7];1025 } profile;1026 stbir_uint64 * current_zone_excluded_ptr;1027#endif1028 stbir__sampler horizontal;1029 stbir__sampler vertical;10301031 void const * input_data;1032 void * output_data;10331034 int input_stride_bytes;1035 int output_stride_bytes;1036 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)1037 int ring_buffer_num_entries; // Total number of entries in the ring buffer.10381039 stbir_datatype input_type;1040 stbir_datatype output_type;10411042 stbir_input_callback * in_pixels_cb;1043 void * user_data;1044 stbir_output_callback * out_pixels_cb;10451046 stbir__extents scanline_extents;10471048 void * alloced_mem;1049 stbir__per_split_info * split_info; // by default 1, but there will be N of these allocated based on the thread init you did10501051 stbir__decode_pixels_func * decode_pixels;1052 stbir__alpha_weight_func * alpha_weight;1053 stbir__horizontal_gather_channels_func * horizontal_gather_channels;1054 stbir__alpha_unweight_func * alpha_unweight;1055 stbir__encode_pixels_func * encode_pixels;1056 1057 int alloced_total;1058 int splits; // count of splits1059 1060 stbir_internal_pixel_layout input_pixel_layout_internal;1061 stbir_internal_pixel_layout output_pixel_layout_internal;10621063 int input_color_and_type;1064 int offset_x, offset_y; // offset within output_data1065 int vertical_first;1066 int channels;1067 int effective_channels; // same as channels, except on RGBA/ARGB (7), or XA/AX (3)1068 int alloc_ring_buffer_num_entries; // Number of entries in the ring buffer that will be allocated1069};107010711072#define stbir__max_uint8_as_float 255.0f1073#define stbir__max_uint16_as_float 65535.0f1074#define stbir__max_uint8_as_float_inverted (1.0f/255.0f)1075#define stbir__max_uint16_as_float_inverted (1.0f/65535.0f)1076#define stbir__small_float ((float)1 / (1 << 20) / (1 << 20) / (1 << 20) / (1 << 20) / (1 << 20) / (1 << 20))10771078// min/max friendly1079#define STBIR_CLAMP(x, xmin, xmax) do { \1080 if ( (x) < (xmin) ) (x) = (xmin); \1081 if ( (x) > (xmax) ) (x) = (xmax); \1082} while (0)10831084static stbir__inline int stbir__min(int a, int b)1085{1086 return a < b ? a : b;1087}10881089static stbir__inline int stbir__max(int a, int b)1090{1091 return a > b ? a : b;1092}10931094static float stbir__srgb_uchar_to_linear_float[256] = {1095 0.000000f, 0.000304f, 0.000607f, 0.000911f, 0.001214f, 0.001518f, 0.001821f, 0.002125f, 0.002428f, 0.002732f, 0.003035f,1096 0.003347f, 0.003677f, 0.004025f, 0.004391f, 0.004777f, 0.005182f, 0.005605f, 0.006049f, 0.006512f, 0.006995f, 0.007499f,1097 0.008023f, 0.008568f, 0.009134f, 0.009721f, 0.010330f, 0.010960f, 0.011612f, 0.012286f, 0.012983f, 0.013702f, 0.014444f,1098 0.015209f, 0.015996f, 0.016807f, 0.017642f, 0.018500f, 0.019382f, 0.020289f, 0.021219f, 0.022174f, 0.023153f, 0.024158f,1099 0.025187f, 0.026241f, 0.027321f, 0.028426f, 0.029557f, 0.030713f, 0.031896f, 0.033105f, 0.034340f, 0.035601f, 0.036889f,1100 0.038204f, 0.039546f, 0.040915f, 0.042311f, 0.043735f, 0.045186f, 0.046665f, 0.048172f, 0.049707f, 0.051269f, 0.052861f,1101 0.054480f, 0.056128f, 0.057805f, 0.059511f, 0.061246f, 0.063010f, 0.064803f, 0.066626f, 0.068478f, 0.070360f, 0.072272f,1102 0.074214f, 0.076185f, 0.078187f, 0.080220f, 0.082283f, 0.084376f, 0.086500f, 0.088656f, 0.090842f, 0.093059f, 0.095307f,1103 0.097587f, 0.099899f, 0.102242f, 0.104616f, 0.107023f, 0.109462f, 0.111932f, 0.114435f, 0.116971f, 0.119538f, 0.122139f,1104 0.124772f, 0.127438f, 0.130136f, 0.132868f, 0.135633f, 0.138432f, 0.141263f, 0.144128f, 0.147027f, 0.149960f, 0.152926f,1105 0.155926f, 0.158961f, 0.162029f, 0.165132f, 0.168269f, 0.171441f, 0.174647f, 0.177888f, 0.181164f, 0.184475f, 0.187821f,1106 0.191202f, 0.194618f, 0.198069f, 0.201556f, 0.205079f, 0.208637f, 0.212231f, 0.215861f, 0.219526f, 0.223228f, 0.226966f,1107 0.230740f, 0.234551f, 0.238398f, 0.242281f, 0.246201f, 0.250158f, 0.254152f, 0.258183f, 0.262251f, 0.266356f, 0.270498f,1108 0.274677f, 0.278894f, 0.283149f, 0.287441f, 0.291771f, 0.296138f, 0.300544f, 0.304987f, 0.309469f, 0.313989f, 0.318547f,1109 0.323143f, 0.327778f, 0.332452f, 0.337164f, 0.341914f, 0.346704f, 0.351533f, 0.356400f, 0.361307f, 0.366253f, 0.371238f,1110 0.376262f, 0.381326f, 0.386430f, 0.391573f, 0.396755f, 0.401978f, 0.407240f, 0.412543f, 0.417885f, 0.423268f, 0.428691f,1111 0.434154f, 0.439657f, 0.445201f, 0.450786f, 0.456411f, 0.462077f, 0.467784f, 0.473532f, 0.479320f, 0.485150f, 0.491021f,1112 0.496933f, 0.502887f, 0.508881f, 0.514918f, 0.520996f, 0.527115f, 0.533276f, 0.539480f, 0.545725f, 0.552011f, 0.558340f,1113 0.564712f, 0.571125f, 0.577581f, 0.584078f, 0.590619f, 0.597202f, 0.603827f, 0.610496f, 0.617207f, 0.623960f, 0.630757f,1114 0.637597f, 0.644480f, 0.651406f, 0.658375f, 0.665387f, 0.672443f, 0.679543f, 0.686685f, 0.693872f, 0.701102f, 0.708376f,1115 0.715694f, 0.723055f, 0.730461f, 0.737911f, 0.745404f, 0.752942f, 0.760525f, 0.768151f, 0.775822f, 0.783538f, 0.791298f,1116 0.799103f, 0.806952f, 0.814847f, 0.822786f, 0.830770f, 0.838799f, 0.846873f, 0.854993f, 0.863157f, 0.871367f, 0.879622f,1117 0.887923f, 0.896269f, 0.904661f, 0.913099f, 0.921582f, 0.930111f, 0.938686f, 0.947307f, 0.955974f, 0.964686f, 0.973445f,1118 0.982251f, 0.991102f, 1.0f1119};11201121typedef union1122{1123 unsigned int u;1124 float f;1125} stbir__FP32;11261127// From https://gist.github.com/rygorous/220383411281129static const stbir_uint32 fp32_to_srgb8_tab4[104] = {1130 0x0073000d, 0x007a000d, 0x0080000d, 0x0087000d, 0x008d000d, 0x0094000d, 0x009a000d, 0x00a1000d,1131 0x00a7001a, 0x00b4001a, 0x00c1001a, 0x00ce001a, 0x00da001a, 0x00e7001a, 0x00f4001a, 0x0101001a,1132 0x010e0033, 0x01280033, 0x01410033, 0x015b0033, 0x01750033, 0x018f0033, 0x01a80033, 0x01c20033,1133 0x01dc0067, 0x020f0067, 0x02430067, 0x02760067, 0x02aa0067, 0x02dd0067, 0x03110067, 0x03440067,1134 0x037800ce, 0x03df00ce, 0x044600ce, 0x04ad00ce, 0x051400ce, 0x057b00c5, 0x05dd00bc, 0x063b00b5,1135 0x06970158, 0x07420142, 0x07e30130, 0x087b0120, 0x090b0112, 0x09940106, 0x0a1700fc, 0x0a9500f2,1136 0x0b0f01cb, 0x0bf401ae, 0x0ccb0195, 0x0d950180, 0x0e56016e, 0x0f0d015e, 0x0fbc0150, 0x10630143,1137 0x11070264, 0x1238023e, 0x1357021d, 0x14660201, 0x156601e9, 0x165a01d3, 0x174401c0, 0x182401af,1138 0x18fe0331, 0x1a9602fe, 0x1c1502d2, 0x1d7e02ad, 0x1ed4028d, 0x201a0270, 0x21520256, 0x227d0240,1139 0x239f0443, 0x25c003fe, 0x27bf03c4, 0x29a10392, 0x2b6a0367, 0x2d1d0341, 0x2ebe031f, 0x304d0300,1140 0x31d105b0, 0x34a80555, 0x37520507, 0x39d504c5, 0x3c37048b, 0x3e7c0458, 0x40a8042a, 0x42bd0401,1141 0x44c20798, 0x488e071e, 0x4c1c06b6, 0x4f76065d, 0x52a50610, 0x55ac05cc, 0x5892058f, 0x5b590559,1142 0x5e0c0a23, 0x631c0980, 0x67db08f6, 0x6c55087f, 0x70940818, 0x74a007bd, 0x787d076c, 0x7c330723,1143};1144 1145static stbir__inline stbir_uint8 stbir__linear_to_srgb_uchar(float in)1146{1147 static const stbir__FP32 almostone = { 0x3f7fffff }; // 1-eps1148 static const stbir__FP32 minval = { (127-13) << 23 };1149 stbir_uint32 tab,bias,scale,t;1150 stbir__FP32 f;11511152 // Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively.1153 // The tests are carefully written so that NaNs map to 0, same as in the reference1154 // implementation.1155 if (!(in > minval.f)) // written this way to catch NaNs1156 return 0;1157 if (in > almostone.f)1158 return 255;11591160 // Do the table lookup and unpack bias, scale1161 f.f = in;1162 tab = fp32_to_srgb8_tab4[(f.u - minval.u) >> 20];1163 bias = (tab >> 16) << 9;1164 scale = tab & 0xffff;11651166 // Grab next-highest mantissa bits and perform linear interpolation1167 t = (f.u >> 12) & 0xff;1168 return (unsigned char) ((bias + scale*t) >> 16);1169}11701171#ifndef STBIR_FORCE_GATHER_FILTER_SCANLINES_AMOUNT1172#define STBIR_FORCE_GATHER_FILTER_SCANLINES_AMOUNT 32 // when downsampling and <= 32 scanlines of buffering, use gather. gather used down to 1/8th scaling for 25% win.1173#endif11741175// restrict pointers for the output pointers1176#if defined( _MSC_VER ) && !defined(__clang__)1177 #define STBIR_STREAMOUT_PTR( star ) star __restrict1178 #define STBIR_NO_UNROLL( ptr ) __assume(ptr) // this oddly keeps msvc from unrolling a loop1179#elif defined( __clang__ )1180 #define STBIR_STREAMOUT_PTR( star ) star __restrict__1181 #define STBIR_NO_UNROLL( ptr ) asm (""::"r"(ptr))1182#elif defined( __GNUC__ )1183 #define STBIR_STREAMOUT_PTR( star ) star __restrict__1184 #define STBIR_NO_UNROLL( ptr ) asm (""::"r"(ptr))1185#else1186 #define STBIR_STREAMOUT_PTR( star ) star1187 #define STBIR_NO_UNROLL( ptr )1188#endif11891190#ifdef STBIR_NO_SIMD // force simd off for whatever reason11911192// force simd off overrides everything else, so clear it all11931194#ifdef STBIR_SSE21195#undef STBIR_SSE21196#endif11971198#ifdef STBIR_AVX1199#undef STBIR_AVX1200#endif12011202#ifdef STBIR_NEON1203#undef STBIR_NEON1204#endif12051206#ifdef STBIR_AVX21207#undef STBIR_AVX21208#endif12091210#ifdef STBIR_FP16C1211#undef STBIR_FP16C1212#endif12131214#ifdef STBIR_WASM1215#undef STBIR_WASM1216#endif12171218#ifdef STBIR_SIMD1219#undef STBIR_SIMD1220#endif12211222#else // STBIR_SIMD12231224#ifdef STBIR_SSE21225 #include <emmintrin.h>1226 1227 #define stbir__simdf __m1281228 #define stbir__simdi __m128i12291230 #define stbir_simdi_castf( reg ) _mm_castps_si128(reg)1231 #define stbir_simdf_casti( reg ) _mm_castsi128_ps(reg)12321233 #define stbir__simdf_load( reg, ptr ) (reg) = _mm_loadu_ps( (float const*)(ptr) )1234 #define stbir__simdi_load( reg, ptr ) (reg) = _mm_loadu_si128 ( (stbir__simdi const*)(ptr) )1235 #define stbir__simdf_load1( out, ptr ) (out) = _mm_load_ss( (float const*)(ptr) ) // top values can be random (not denormal or nan for perf)1236 #define stbir__simdi_load1( out, ptr ) (out) = _mm_castps_si128( _mm_load_ss( (float const*)(ptr) ))1237 #define stbir__simdf_load1z( out, ptr ) (out) = _mm_load_ss( (float const*)(ptr) ) // top values must be zero1238 #define stbir__simdf_frep4( fvar ) _mm_set_ps1( fvar )1239 #define stbir__simdf_load1frep4( out, fvar ) (out) = _mm_set_ps1( fvar )1240 #define stbir__simdf_load2( out, ptr ) (out) = _mm_castsi128_ps( _mm_loadl_epi64( (__m128i*)(ptr)) ) // top values can be random (not denormal or nan for perf)1241 #define stbir__simdf_load2z( out, ptr ) (out) = _mm_castsi128_ps( _mm_loadl_epi64( (__m128i*)(ptr)) ) // top values must be zero1242 #define stbir__simdf_load2hmerge( out, reg, ptr ) (out) = _mm_castpd_ps(_mm_loadh_pd( _mm_castps_pd(reg), (double*)(ptr) ))12431244 #define stbir__simdf_zeroP() _mm_setzero_ps()1245 #define stbir__simdf_zero( reg ) (reg) = _mm_setzero_ps()12461247 #define stbir__simdf_store( ptr, reg ) _mm_storeu_ps( (float*)(ptr), reg )1248 #define stbir__simdf_store1( ptr, reg ) _mm_store_ss( (float*)(ptr), reg )1249 #define stbir__simdf_store2( ptr, reg ) _mm_storel_epi64( (__m128i*)(ptr), _mm_castps_si128(reg) )1250 #define stbir__simdf_store2h( ptr, reg ) _mm_storeh_pd( (double*)(ptr), _mm_castps_pd(reg) )12511252 #define stbir__simdi_store( ptr, reg ) _mm_storeu_si128( (__m128i*)(ptr), reg )1253 #define stbir__simdi_store1( ptr, reg ) _mm_store_ss( (float*)(ptr), _mm_castsi128_ps(reg) )1254 #define stbir__simdi_store2( ptr, reg ) _mm_storel_epi64( (__m128i*)(ptr), (reg) )12551256 #define stbir__prefetch( ptr ) _mm_prefetch((char*)(ptr), _MM_HINT_T0 )1257 1258 #define stbir__simdi_expand_u8_to_u32(out0,out1,out2,out3,ireg) \1259 { \1260 stbir__simdi zero = _mm_setzero_si128(); \1261 out2 = _mm_unpacklo_epi8( ireg, zero ); \1262 out3 = _mm_unpackhi_epi8( ireg, zero ); \1263 out0 = _mm_unpacklo_epi16( out2, zero ); \1264 out1 = _mm_unpackhi_epi16( out2, zero ); \1265 out2 = _mm_unpacklo_epi16( out3, zero ); \1266 out3 = _mm_unpackhi_epi16( out3, zero ); \1267 }12681269#define stbir__simdi_expand_u8_to_1u32(out,ireg) \1270 { \1271 stbir__simdi zero = _mm_setzero_si128(); \1272 out = _mm_unpacklo_epi8( ireg, zero ); \1273 out = _mm_unpacklo_epi16( out, zero ); \1274 }12751276 #define stbir__simdi_expand_u16_to_u32(out0,out1,ireg) \1277 { \1278 stbir__simdi zero = _mm_setzero_si128(); \1279 out0 = _mm_unpacklo_epi16( ireg, zero ); \1280 out1 = _mm_unpackhi_epi16( ireg, zero ); \1281 }12821283 #define stbir__simdf_convert_float_to_i32( i, f ) (i) = _mm_cvttps_epi32(f)1284 #define stbir__simdf_convert_float_to_int( f ) _mm_cvtt_ss2si(f)1285 #define stbir__simdf_convert_float_to_uint8( f ) ((unsigned char)_mm_cvtsi128_si32(_mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(f,STBIR__CONSTF(STBIR_max_uint8_as_float)),_mm_setzero_ps()))))1286 #define stbir__simdf_convert_float_to_short( f ) ((unsigned short)_mm_cvtsi128_si32(_mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(f,STBIR__CONSTF(STBIR_max_uint16_as_float)),_mm_setzero_ps()))))12871288 #define stbir__simdi_to_int( i ) _mm_cvtsi128_si32(i) 1289 #define stbir__simdi_convert_i32_to_float(out, ireg) (out) = _mm_cvtepi32_ps( ireg )1290 #define stbir__simdf_add( out, reg0, reg1 ) (out) = _mm_add_ps( reg0, reg1 )1291 #define stbir__simdf_mult( out, reg0, reg1 ) (out) = _mm_mul_ps( reg0, reg1 )1292 #define stbir__simdf_mult_mem( out, reg, ptr ) (out) = _mm_mul_ps( reg, _mm_loadu_ps( (float const*)(ptr) ) )1293 #define stbir__simdf_mult1_mem( out, reg, ptr ) (out) = _mm_mul_ss( reg, _mm_load_ss( (float const*)(ptr) ) )1294 #define stbir__simdf_add_mem( out, reg, ptr ) (out) = _mm_add_ps( reg, _mm_loadu_ps( (float const*)(ptr) ) )1295 #define stbir__simdf_add1_mem( out, reg, ptr ) (out) = _mm_add_ss( reg, _mm_load_ss( (float const*)(ptr) ) )12961297 #ifdef STBIR_USE_FMA // not on by default to maintain bit identical simd to non-simd1298 #include <immintrin.h>1299 #define stbir__simdf_madd( out, add, mul1, mul2 ) (out) = _mm_fmadd_ps( mul1, mul2, add )1300 #define stbir__simdf_madd1( out, add, mul1, mul2 ) (out) = _mm_fmadd_ss( mul1, mul2, add )1301 #define stbir__simdf_madd_mem( out, add, mul, ptr ) (out) = _mm_fmadd_ps( mul, _mm_loadu_ps( (float const*)(ptr) ), add )1302 #define stbir__simdf_madd1_mem( out, add, mul, ptr ) (out) = _mm_fmadd_ss( mul, _mm_load_ss( (float const*)(ptr) ), add )1303 #else1304 #define stbir__simdf_madd( out, add, mul1, mul2 ) (out) = _mm_add_ps( add, _mm_mul_ps( mul1, mul2 ) )1305 #define stbir__simdf_madd1( out, add, mul1, mul2 ) (out) = _mm_add_ss( add, _mm_mul_ss( mul1, mul2 ) )1306 #define stbir__simdf_madd_mem( out, add, mul, ptr ) (out) = _mm_add_ps( add, _mm_mul_ps( mul, _mm_loadu_ps( (float const*)(ptr) ) ) )1307 #define stbir__simdf_madd1_mem( out, add, mul, ptr ) (out) = _mm_add_ss( add, _mm_mul_ss( mul, _mm_load_ss( (float const*)(ptr) ) ) )1308 #endif13091310 #define stbir__simdf_add1( out, reg0, reg1 ) (out) = _mm_add_ss( reg0, reg1 )1311 #define stbir__simdf_mult1( out, reg0, reg1 ) (out) = _mm_mul_ss( reg0, reg1 )13121313 #define stbir__simdf_and( out, reg0, reg1 ) (out) = _mm_and_ps( reg0, reg1 )1314 #define stbir__simdf_or( out, reg0, reg1 ) (out) = _mm_or_ps( reg0, reg1 )13151316 #define stbir__simdf_min( out, reg0, reg1 ) (out) = _mm_min_ps( reg0, reg1 )1317 #define stbir__simdf_max( out, reg0, reg1 ) (out) = _mm_max_ps( reg0, reg1 )1318 #define stbir__simdf_min1( out, reg0, reg1 ) (out) = _mm_min_ss( reg0, reg1 )1319 #define stbir__simdf_max1( out, reg0, reg1 ) (out) = _mm_max_ss( reg0, reg1 )13201321 #define stbir__simdf_0123ABCDto3ABx( out, reg0, reg1 ) (out)=_mm_castsi128_ps( _mm_shuffle_epi32( _mm_castps_si128( _mm_shuffle_ps( reg1,reg0, (0<<0) + (1<<2) + (2<<4) + (3<<6) )), (3<<0) + (0<<2) + (1<<4) + (2<<6) ) )1322 #define stbir__simdf_0123ABCDto23Ax( out, reg0, reg1 ) (out)=_mm_castsi128_ps( _mm_shuffle_epi32( _mm_castps_si128( _mm_shuffle_ps( reg1,reg0, (0<<0) + (1<<2) + (2<<4) + (3<<6) )), (2<<0) + (3<<2) + (0<<4) + (1<<6) ) )13231324 static const stbir__simdf STBIR_zeroones = { 0.0f,1.0f,0.0f,1.0f };1325 static const stbir__simdf STBIR_onezeros = { 1.0f,0.0f,1.0f,0.0f };1326 #define stbir__simdf_aaa1( out, alp, ones ) (out)=_mm_castsi128_ps( _mm_shuffle_epi32( _mm_castps_si128( _mm_movehl_ps( ones, alp ) ), (1<<0) + (1<<2) + (1<<4) + (2<<6) ) )1327 #define stbir__simdf_1aaa( out, alp, ones ) (out)=_mm_castsi128_ps( _mm_shuffle_epi32( _mm_castps_si128( _mm_movelh_ps( ones, alp ) ), (0<<0) + (2<<2) + (2<<4) + (2<<6) ) )1328 #define stbir__simdf_a1a1( out, alp, ones) (out) = _mm_or_ps( _mm_castsi128_ps( _mm_srli_epi64( _mm_castps_si128(alp), 32 ) ), STBIR_zeroones )1329 #define stbir__simdf_1a1a( out, alp, ones) (out) = _mm_or_ps( _mm_castsi128_ps( _mm_slli_epi64( _mm_castps_si128(alp), 32 ) ), STBIR_onezeros )13301331 #define stbir__simdf_swiz( reg, one, two, three, four ) _mm_castsi128_ps( _mm_shuffle_epi32( _mm_castps_si128( reg ), (one<<0) + (two<<2) + (three<<4) + (four<<6) ) )13321333 #define stbir__simdi_and( out, reg0, reg1 ) (out) = _mm_and_si128( reg0, reg1 )1334 #define stbir__simdi_or( out, reg0, reg1 ) (out) = _mm_or_si128( reg0, reg1 )1335 #define stbir__simdi_16madd( out, reg0, reg1 ) (out) = _mm_madd_epi16( reg0, reg1 )13361337 #define stbir__simdf_pack_to_8bytes(out,aa,bb) \1338 { \1339 stbir__simdf af,bf; \1340 stbir__simdi a,b; \1341 af = _mm_min_ps( aa, STBIR_max_uint8_as_float ); \1342 bf = _mm_min_ps( bb, STBIR_max_uint8_as_float ); \1343 af = _mm_max_ps( af, _mm_setzero_ps() ); \1344 bf = _mm_max_ps( bf, _mm_setzero_ps() ); \1345 a = _mm_cvttps_epi32( af ); \1346 b = _mm_cvttps_epi32( bf ); \1347 a = _mm_packs_epi32( a, b ); \1348 out = _mm_packus_epi16( a, a ); \1349 }13501351 #define stbir__simdf_load4_transposed( o0, o1, o2, o3, ptr ) \1352 stbir__simdf_load( o0, (ptr) ); \1353 stbir__simdf_load( o1, (ptr)+4 ); \1354 stbir__simdf_load( o2, (ptr)+8 ); \1355 stbir__simdf_load( o3, (ptr)+12 ); \1356 { \1357 __m128 tmp0, tmp1, tmp2, tmp3; \1358 tmp0 = _mm_unpacklo_ps(o0, o1); \1359 tmp2 = _mm_unpacklo_ps(o2, o3); \1360 tmp1 = _mm_unpackhi_ps(o0, o1); \1361 tmp3 = _mm_unpackhi_ps(o2, o3); \1362 o0 = _mm_movelh_ps(tmp0, tmp2); \1363 o1 = _mm_movehl_ps(tmp2, tmp0); \1364 o2 = _mm_movelh_ps(tmp1, tmp3); \1365 o3 = _mm_movehl_ps(tmp3, tmp1); \1366 }13671368 #define stbir__interleave_pack_and_store_16_u8( ptr, r0, r1, r2, r3 ) \1369 r0 = _mm_packs_epi32( r0, r1 ); \1370 r2 = _mm_packs_epi32( r2, r3 ); \1371 r1 = _mm_unpacklo_epi16( r0, r2 ); \1372 r3 = _mm_unpackhi_epi16( r0, r2 ); \1373 r0 = _mm_unpacklo_epi16( r1, r3 ); \1374 r2 = _mm_unpackhi_epi16( r1, r3 ); \1375 r0 = _mm_packus_epi16( r0, r2 ); \1376 stbir__simdi_store( ptr, r0 ); \13771378 #define stbir__simdi_32shr( out, reg, imm ) out = _mm_srli_epi32( reg, imm )13791380 #if defined(_MSC_VER) && !defined(__clang__)1381 // msvc inits with 8 bytes1382 #define STBIR__CONST_32_TO_8( v ) (char)(unsigned char)((v)&255),(char)(unsigned char)(((v)>>8)&255),(char)(unsigned char)(((v)>>16)&255),(char)(unsigned char)(((v)>>24)&255)1383 #define STBIR__CONST_4_32i( v ) STBIR__CONST_32_TO_8( v ), STBIR__CONST_32_TO_8( v ), STBIR__CONST_32_TO_8( v ), STBIR__CONST_32_TO_8( v )1384 #define STBIR__CONST_4d_32i( v0, v1, v2, v3 ) STBIR__CONST_32_TO_8( v0 ), STBIR__CONST_32_TO_8( v1 ), STBIR__CONST_32_TO_8( v2 ), STBIR__CONST_32_TO_8( v3 )1385 #else1386 // everything else inits with long long's1387 #define STBIR__CONST_4_32i( v ) (long long)((((stbir_uint64)(stbir_uint32)(v))<<32)|((stbir_uint64)(stbir_uint32)(v))),(long long)((((stbir_uint64)(stbir_uint32)(v))<<32)|((stbir_uint64)(stbir_uint32)(v)))1388 #define STBIR__CONST_4d_32i( v0, v1, v2, v3 ) (long long)((((stbir_uint64)(stbir_uint32)(v1))<<32)|((stbir_uint64)(stbir_uint32)(v0))),(long long)((((stbir_uint64)(stbir_uint32)(v3))<<32)|((stbir_uint64)(stbir_uint32)(v2)))1389 #endif13901391 #define STBIR__SIMDF_CONST(var, x) stbir__simdf var = { x, x, x, x }1392 #define STBIR__SIMDI_CONST(var, x) stbir__simdi var = { STBIR__CONST_4_32i(x) }1393 #define STBIR__CONSTF(var) (var)1394 #define STBIR__CONSTI(var) (var)13951396 #if defined(STBIR_AVX) || defined(__SSE4_1__)1397 #include <smmintrin.h>1398 #define stbir__simdf_pack_to_8words(out,reg0,reg1) out = _mm_packus_epi32(_mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(reg0,STBIR__CONSTF(STBIR_max_uint16_as_float)),_mm_setzero_ps())), _mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(reg1,STBIR__CONSTF(STBIR_max_uint16_as_float)),_mm_setzero_ps())))1399 #else1400 STBIR__SIMDI_CONST(stbir__s32_32768, 32768);1401 STBIR__SIMDI_CONST(stbir__s16_32768, ((32768<<16)|32768));14021403 #define stbir__simdf_pack_to_8words(out,reg0,reg1) \1404 { \1405 stbir__simdi tmp0,tmp1; \1406 tmp0 = _mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(reg0,STBIR__CONSTF(STBIR_max_uint16_as_float)),_mm_setzero_ps())); \1407 tmp1 = _mm_cvttps_epi32(_mm_max_ps(_mm_min_ps(reg1,STBIR__CONSTF(STBIR_max_uint16_as_float)),_mm_setzero_ps())); \1408 tmp0 = _mm_sub_epi32( tmp0, stbir__s32_32768 ); \1409 tmp1 = _mm_sub_epi32( tmp1, stbir__s32_32768 ); \1410 out = _mm_packs_epi32( tmp0, tmp1 ); \1411 out = _mm_sub_epi16( out, stbir__s16_32768 ); \1412 }14131414 #endif14151416 #define STBIR_SIMD14171418 // if we detect AVX, set the simd8 defines1419 #ifdef STBIR_AVX1420 #include <immintrin.h>1421 #define STBIR_SIMD81422 #define stbir__simdf8 __m2561423 #define stbir__simdi8 __m256i1424 #define stbir__simdf8_load( out, ptr ) (out) = _mm256_loadu_ps( (float const *)(ptr) )1425 #define stbir__simdi8_load( out, ptr ) (out) = _mm256_loadu_si256( (__m256i const *)(ptr) )1426 #define stbir__simdf8_mult( out, a, b ) (out) = _mm256_mul_ps( (a), (b) )1427 #define stbir__simdf8_store( ptr, out ) _mm256_storeu_ps( (float*)(ptr), out )1428 #define stbir__simdi8_store( ptr, reg ) _mm256_storeu_si256( (__m256i*)(ptr), reg )1429 #define stbir__simdf8_frep8( fval ) _mm256_set1_ps( fval )14301431 #define stbir__simdf8_min( out, reg0, reg1 ) (out) = _mm256_min_ps( reg0, reg1 )1432 #define stbir__simdf8_max( out, reg0, reg1 ) (out) = _mm256_max_ps( reg0, reg1 )14331434 #define stbir__simdf8_add4halves( out, bot4, top8 ) (out) = _mm_add_ps( bot4, _mm256_extractf128_ps( top8, 1 ) )1435 #define stbir__simdf8_mult_mem( out, reg, ptr ) (out) = _mm256_mul_ps( reg, _mm256_loadu_ps( (float const*)(ptr) ) )1436 #define stbir__simdf8_add_mem( out, reg, ptr ) (out) = _mm256_add_ps( reg, _mm256_loadu_ps( (float const*)(ptr) ) )1437 #define stbir__simdf8_add( out, a, b ) (out) = _mm256_add_ps( a, b )1438 #define stbir__simdf8_load1b( out, ptr ) (out) = _mm256_broadcast_ss( ptr )1439 #define stbir__simdf_load1rep4( out, ptr ) (out) = _mm_broadcast_ss( ptr ) // avx load instruction14401441 #define stbir__simdi8_convert_i32_to_float(out, ireg) (out) = _mm256_cvtepi32_ps( ireg )1442 #define stbir__simdf8_convert_float_to_i32( i, f ) (i) = _mm256_cvttps_epi32(f)1443 1444 #define stbir__simdf8_bot4s( out, a, b ) (out) = _mm256_permute2f128_ps(a,b, (0<<0)+(2<<4) )1445 #define stbir__simdf8_top4s( out, a, b ) (out) = _mm256_permute2f128_ps(a,b, (1<<0)+(3<<4) )1446 1447 #define stbir__simdf8_gettop4( reg ) _mm256_extractf128_ps(reg,1)14481449 #ifdef STBIR_AVX214501451 #define stbir__simdi8_expand_u8_to_u32(out0,out1,ireg) \1452 { \1453 stbir__simdi8 a, zero =_mm256_setzero_si256();\1454 a = _mm256_permute4x64_epi64( _mm256_unpacklo_epi8( _mm256_permute4x64_epi64(_mm256_castsi128_si256(ireg),(0<<0)+(2<<2)+(1<<4)+(3<<6)), zero ),(0<<0)+(2<<2)+(1<<4)+(3<<6)); \1455 out0 = _mm256_unpacklo_epi16( a, zero ); \1456 out1 = _mm256_unpackhi_epi16( a, zero ); \1457 }14581459 #define stbir__simdf8_pack_to_16bytes(out,aa,bb) \1460 { \1461 stbir__simdi8 t; \1462 stbir__simdf8 af,bf; \1463 stbir__simdi8 a,b; \1464 af = _mm256_min_ps( aa, STBIR_max_uint8_as_floatX ); \1465 bf = _mm256_min_ps( bb, STBIR_max_uint8_as_floatX ); \1466 af = _mm256_max_ps( af, _mm256_setzero_ps() ); \1467 bf = _mm256_max_ps( bf, _mm256_setzero_ps() ); \1468 a = _mm256_cvttps_epi32( af ); \1469 b = _mm256_cvttps_epi32( bf ); \1470 t = _mm256_permute4x64_epi64( _mm256_packs_epi32( a, b ), (0<<0)+(2<<2)+(1<<4)+(3<<6) ); \1471 out = _mm256_castsi256_si128( _mm256_permute4x64_epi64( _mm256_packus_epi16( t, t ), (0<<0)+(2<<2)+(1<<4)+(3<<6) ) ); \1472 }14731474 #define stbir__simdi8_expand_u16_to_u32(out,ireg) out = _mm256_unpacklo_epi16( _mm256_permute4x64_epi64(_mm256_castsi128_si256(ireg),(0<<0)+(2<<2)+(1<<4)+(3<<6)), _mm256_setzero_si256() ); 1475 1476 #define stbir__simdf8_pack_to_16words(out,aa,bb) \1477 { \1478 stbir__simdf8 af,bf; \1479 stbir__simdi8 a,b; \1480 af = _mm256_min_ps( aa, STBIR_max_uint16_as_floatX ); \1481 bf = _mm256_min_ps( bb, STBIR_max_uint16_as_floatX ); \1482 af = _mm256_max_ps( af, _mm256_setzero_ps() ); \1483 bf = _mm256_max_ps( bf, _mm256_setzero_ps() ); \1484 a = _mm256_cvttps_epi32( af ); \1485 b = _mm256_cvttps_epi32( bf ); \1486 (out) = _mm256_permute4x64_epi64( _mm256_packus_epi32(a, b), (0<<0)+(2<<2)+(1<<4)+(3<<6) ); \1487 }14881489 #else14901491 #define stbir__simdi8_expand_u8_to_u32(out0,out1,ireg) \1492 { \1493 stbir__simdi a,zero = _mm_setzero_si128(); \1494 a = _mm_unpacklo_epi8( ireg, zero ); \1495 out0 = _mm256_setr_m128i( _mm_unpacklo_epi16( a, zero ), _mm_unpackhi_epi16( a, zero ) ); \1496 a = _mm_unpackhi_epi8( ireg, zero ); \1497 out1 = _mm256_setr_m128i( _mm_unpacklo_epi16( a, zero ), _mm_unpackhi_epi16( a, zero ) ); \1498 }1499 1500 #define stbir__simdf8_pack_to_16bytes(out,aa,bb) \1501 { \1502 stbir__simdi t; \1503 stbir__simdf8 af,bf; \1504 stbir__simdi8 a,b; \1505 af = _mm256_min_ps( aa, STBIR_max_uint8_as_floatX ); \1506 bf = _mm256_min_ps( bb, STBIR_max_uint8_as_floatX ); \1507 af = _mm256_max_ps( af, _mm256_setzero_ps() ); \1508 bf = _mm256_max_ps( bf, _mm256_setzero_ps() ); \1509 a = _mm256_cvttps_epi32( af ); \1510 b = _mm256_cvttps_epi32( bf ); \1511 out = _mm_packs_epi32( _mm256_castsi256_si128(a), _mm256_extractf128_si256( a, 1 ) ); \1512 out = _mm_packus_epi16( out, out ); \1513 t = _mm_packs_epi32( _mm256_castsi256_si128(b), _mm256_extractf128_si256( b, 1 ) ); \1514 t = _mm_packus_epi16( t, t ); \1515 out = _mm_castps_si128( _mm_shuffle_ps( _mm_castsi128_ps(out), _mm_castsi128_ps(t), (0<<0)+(1<<2)+(0<<4)+(1<<6) ) ); \1516 }1517 1518 #define stbir__simdi8_expand_u16_to_u32(out,ireg) \1519 { \1520 stbir__simdi a,b,zero = _mm_setzero_si128(); \1521 a = _mm_unpacklo_epi16( ireg, zero ); \1522 b = _mm_unpackhi_epi16( ireg, zero ); \1523 out = _mm256_insertf128_si256( _mm256_castsi128_si256( a ), b, 1 ); \1524 }15251526 #define stbir__simdf8_pack_to_16words(out,aa,bb) \1527 { \1528 stbir__simdi t0,t1; \1529 stbir__simdf8 af,bf; \1530 stbir__simdi8 a,b; \1531 af = _mm256_min_ps( aa, STBIR_max_uint16_as_floatX ); \1532 bf = _mm256_min_ps( bb, STBIR_max_uint16_as_floatX ); \1533 af = _mm256_max_ps( af, _mm256_setzero_ps() ); \1534 bf = _mm256_max_ps( bf, _mm256_setzero_ps() ); \1535 a = _mm256_cvttps_epi32( af ); \1536 b = _mm256_cvttps_epi32( bf ); \1537 t0 = _mm_packus_epi32( _mm256_castsi256_si128(a), _mm256_extractf128_si256( a, 1 ) ); \1538 t1 = _mm_packus_epi32( _mm256_castsi256_si128(b), _mm256_extractf128_si256( b, 1 ) ); \1539 out = _mm256_setr_m128i( t0, t1 ); \1540 }15411542 #endif15431544 static __m256i stbir_00001111 = { STBIR__CONST_4d_32i( 0, 0, 0, 0 ), STBIR__CONST_4d_32i( 1, 1, 1, 1 ) };1545 #define stbir__simdf8_0123to00001111( out, in ) (out) = _mm256_permutevar_ps ( in, stbir_00001111 )15461547 static __m256i stbir_22223333 = { STBIR__CONST_4d_32i( 2, 2, 2, 2 ), STBIR__CONST_4d_32i( 3, 3, 3, 3 ) };1548 #define stbir__simdf8_0123to22223333( out, in ) (out) = _mm256_permutevar_ps ( in, stbir_22223333 )15491550 #define stbir__simdf8_0123to2222( out, in ) (out) = stbir__simdf_swiz(_mm256_castps256_ps128(in), 2,2,2,2 )15511552 #define stbir__simdf8_load2( out, ptr ) (out) = _mm256_castsi256_ps(_mm256_castsi128_si256( _mm_loadl_epi64( (__m128i*)(ptr)) )) // top values can be random (not denormal or nan for perf)1553 #define stbir__simdf8_load4b( out, ptr ) (out) = _mm256_broadcast_ps( (__m128 const *)(ptr) )15541555 static __m256i stbir_00112233 = { STBIR__CONST_4d_32i( 0, 0, 1, 1 ), STBIR__CONST_4d_32i( 2, 2, 3, 3 ) };1556 #define stbir__simdf8_0123to00112233( out, in ) (out) = _mm256_permutevar_ps ( in, stbir_00112233 )1557 #define stbir__simdf8_add4( out, a8, b ) (out) = _mm256_add_ps( a8, _mm256_castps128_ps256( b ) )15581559 static __m256i stbir_load6 = { STBIR__CONST_4_32i( 0x80000000 ), STBIR__CONST_4d_32i( 0x80000000, 0x80000000, 0, 0 ) };1560 #define stbir__simdf8_load6z( out, ptr ) (out) = _mm256_maskload_ps( ptr, stbir_load6 )15611562 #define stbir__simdf8_0123to00000000( out, in ) (out) = _mm256_shuffle_ps ( in, in, (0<<0)+(0<<2)+(0<<4)+(0<<6) )1563 #define stbir__simdf8_0123to11111111( out, in ) (out) = _mm256_shuffle_ps ( in, in, (1<<0)+(1<<2)+(1<<4)+(1<<6) )1564 #define stbir__simdf8_0123to22222222( out, in ) (out) = _mm256_shuffle_ps ( in, in, (2<<0)+(2<<2)+(2<<4)+(2<<6) )1565 #define stbir__simdf8_0123to33333333( out, in ) (out) = _mm256_shuffle_ps ( in, in, (3<<0)+(3<<2)+(3<<4)+(3<<6) )1566 #define stbir__simdf8_0123to21032103( out, in ) (out) = _mm256_shuffle_ps ( in, in, (2<<0)+(1<<2)+(0<<4)+(3<<6) )1567 #define stbir__simdf8_0123to32103210( out, in ) (out) = _mm256_shuffle_ps ( in, in, (3<<0)+(2<<2)+(1<<4)+(0<<6) )1568 #define stbir__simdf8_0123to12301230( out, in ) (out) = _mm256_shuffle_ps ( in, in, (1<<0)+(2<<2)+(3<<4)+(0<<6) )1569 #define stbir__simdf8_0123to10321032( out, in ) (out) = _mm256_shuffle_ps ( in, in, (1<<0)+(0<<2)+(3<<4)+(2<<6) )1570 #define stbir__simdf8_0123to30123012( out, in ) (out) = _mm256_shuffle_ps ( in, in, (3<<0)+(0<<2)+(1<<4)+(2<<6) )15711572 #define stbir__simdf8_0123to11331133( out, in ) (out) = _mm256_shuffle_ps ( in, in, (1<<0)+(1<<2)+(3<<4)+(3<<6) )1573 #define stbir__simdf8_0123to00220022( out, in ) (out) = _mm256_shuffle_ps ( in, in, (0<<0)+(0<<2)+(2<<4)+(2<<6) )15741575 #define stbir__simdf8_aaa1( out, alp, ones ) (out) = _mm256_blend_ps( alp, ones, (1<<0)+(1<<1)+(1<<2)+(0<<3)+(1<<4)+(1<<5)+(1<<6)+(0<<7)); (out)=_mm256_shuffle_ps( out,out, (3<<0) + (3<<2) + (3<<4) + (0<<6) )1576 #define stbir__simdf8_1aaa( out, alp, ones ) (out) = _mm256_blend_ps( alp, ones, (0<<0)+(1<<1)+(1<<2)+(1<<3)+(0<<4)+(1<<5)+(1<<6)+(1<<7)); (out)=_mm256_shuffle_ps( out,out, (1<<0) + (0<<2) + (0<<4) + (0<<6) )1577 #define stbir__simdf8_a1a1( out, alp, ones) (out) = _mm256_blend_ps( alp, ones, (1<<0)+(0<<1)+(1<<2)+(0<<3)+(1<<4)+(0<<5)+(1<<6)+(0<<7)); (out)=_mm256_shuffle_ps( out,out, (1<<0) + (0<<2) + (3<<4) + (2<<6) )1578 #define stbir__simdf8_1a1a( out, alp, ones) (out) = _mm256_blend_ps( alp, ones, (0<<0)+(1<<1)+(0<<2)+(1<<3)+(0<<4)+(1<<5)+(0<<6)+(1<<7)); (out)=_mm256_shuffle_ps( out,out, (1<<0) + (0<<2) + (3<<4) + (2<<6) )15791580 #define stbir__simdf8_zero( reg ) (reg) = _mm256_setzero_ps()15811582 #ifdef STBIR_USE_FMA // not on by default to maintain bit identical simd to non-simd1583 #define stbir__simdf8_madd( out, add, mul1, mul2 ) (out) = _mm256_fmadd_ps( mul1, mul2, add )1584 #define stbir__simdf8_madd_mem( out, add, mul, ptr ) (out) = _mm256_fmadd_ps( mul, _mm256_loadu_ps( (float const*)(ptr) ), add )1585 #define stbir__simdf8_madd_mem4( out, add, mul, ptr ) (out) = _mm256_fmadd_ps( _mm256_castps128_ps256( mul ), _mm256_castps128_ps256( _mm_loadu_ps( (float const*)(ptr) ) ), add )1586 #else1587 #define stbir__simdf8_madd( out, add, mul1, mul2 ) (out) = _mm256_add_ps( add, _mm256_mul_ps( mul1, mul2 ) )1588 #define stbir__simdf8_madd_mem( out, add, mul, ptr ) (out) = _mm256_add_ps( add, _mm256_mul_ps( mul, _mm256_loadu_ps( (float const*)(ptr) ) ) )1589 #define stbir__simdf8_madd_mem4( out, add, mul, ptr ) (out) = _mm256_add_ps( add, _mm256_castps128_ps256( _mm_mul_ps( mul, _mm_loadu_ps( (float const*)(ptr) ) ) ) )1590 #endif1591 #define stbir__if_simdf8_cast_to_simdf4( val ) _mm256_castps256_ps128( val )15921593 #endif15941595 #ifdef STBIR_FLOORF1596 #undef STBIR_FLOORF1597 #endif1598 #define STBIR_FLOORF stbir_simd_floorf1599 static stbir__inline float stbir_simd_floorf(float x) // martins floorf1600 {1601 #if defined(STBIR_AVX) || defined(__SSE4_1__) || defined(STBIR_SSE41)1602 __m128 t = _mm_set_ss(x);1603 return _mm_cvtss_f32( _mm_floor_ss(t, t) );1604 #else1605 __m128 f = _mm_set_ss(x);1606 __m128 t = _mm_cvtepi32_ps(_mm_cvttps_epi32(f));1607 __m128 r = _mm_add_ss(t, _mm_and_ps(_mm_cmplt_ss(f, t), _mm_set_ss(-1.0f)));1608 return _mm_cvtss_f32(r);1609 #endif1610 }16111612 #ifdef STBIR_CEILF1613 #undef STBIR_CEILF1614 #endif1615 #define STBIR_CEILF stbir_simd_ceilf1616 static stbir__inline float stbir_simd_ceilf(float x) // martins ceilf1617 {1618 #if defined(STBIR_AVX) || defined(__SSE4_1__) || defined(STBIR_SSE41)1619 __m128 t = _mm_set_ss(x);1620 return _mm_cvtss_f32( _mm_ceil_ss(t, t) );1621 #else1622 __m128 f = _mm_set_ss(x);1623 __m128 t = _mm_cvtepi32_ps(_mm_cvttps_epi32(f));1624 __m128 r = _mm_add_ss(t, _mm_and_ps(_mm_cmplt_ss(t, f), _mm_set_ss(1.0f)));1625 return _mm_cvtss_f32(r);1626 #endif1627 }16281629#elif defined(STBIR_NEON)1630 1631 #include <arm_neon.h>16321633 #define stbir__simdf float32x4_t1634 #define stbir__simdi uint32x4_t16351636 #define stbir_simdi_castf( reg ) vreinterpretq_u32_f32(reg)1637 #define stbir_simdf_casti( reg ) vreinterpretq_f32_u32(reg)16381639 #define stbir__simdf_load( reg, ptr ) (reg) = vld1q_f32( (float const*)(ptr) )1640 #define stbir__simdi_load( reg, ptr ) (reg) = vld1q_u32( (uint32_t const*)(ptr) )1641 #define stbir__simdf_load1( out, ptr ) (out) = vld1q_dup_f32( (float const*)(ptr) ) // top values can be random (not denormal or nan for perf)1642 #define stbir__simdi_load1( out, ptr ) (out) = vld1q_dup_u32( (uint32_t const*)(ptr) )1643 #define stbir__simdf_load1z( out, ptr ) (out) = vld1q_lane_f32( (float const*)(ptr), vdupq_n_f32(0), 0 ) // top values must be zero1644 #define stbir__simdf_frep4( fvar ) vdupq_n_f32( fvar )1645 #define stbir__simdf_load1frep4( out, fvar ) (out) = vdupq_n_f32( fvar )1646 #define stbir__simdf_load2( out, ptr ) (out) = vcombine_f32( vld1_f32( (float const*)(ptr) ), vcreate_f32(0) ) // top values can be random (not denormal or nan for perf)1647 #define stbir__simdf_load2z( out, ptr ) (out) = vcombine_f32( vld1_f32( (float const*)(ptr) ), vcreate_f32(0) ) // top values must be zero1648 #define stbir__simdf_load2hmerge( out, reg, ptr ) (out) = vcombine_f32( vget_low_f32(reg), vld1_f32( (float const*)(ptr) ) )16491650 #define stbir__simdf_zeroP() vdupq_n_f32(0)1651 #define stbir__simdf_zero( reg ) (reg) = vdupq_n_f32(0)16521653 #define stbir__simdf_store( ptr, reg ) vst1q_f32( (float*)(ptr), reg )1654 #define stbir__simdf_store1( ptr, reg ) vst1q_lane_f32( (float*)(ptr), reg, 0)1655 #define stbir__simdf_store2( ptr, reg ) vst1_f32( (float*)(ptr), vget_low_f32(reg) )1656 #define stbir__simdf_store2h( ptr, reg ) vst1_f32( (float*)(ptr), vget_high_f32(reg) )16571658 #define stbir__simdi_store( ptr, reg ) vst1q_u32( (uint32_t*)(ptr), reg )1659 #define stbir__simdi_store1( ptr, reg ) vst1q_lane_u32( (uint32_t*)(ptr), reg, 0 )1660 #define stbir__simdi_store2( ptr, reg ) vst1_u32( (uint32_t*)(ptr), vget_low_u32(reg) )16611662 #define stbir__prefetch( ptr )16631664 #define stbir__simdi_expand_u8_to_u32(out0,out1,out2,out3,ireg) \1665 { \1666 uint16x8_t l = vmovl_u8( vget_low_u8 ( vreinterpretq_u8_u32(ireg) ) ); \1667 uint16x8_t h = vmovl_u8( vget_high_u8( vreinterpretq_u8_u32(ireg) ) ); \1668 out0 = vmovl_u16( vget_low_u16 ( l ) ); \1669 out1 = vmovl_u16( vget_high_u16( l ) ); \1670 out2 = vmovl_u16( vget_low_u16 ( h ) ); \1671 out3 = vmovl_u16( vget_high_u16( h ) ); \1672 }16731674 #define stbir__simdi_expand_u8_to_1u32(out,ireg) \1675 { \1676 uint16x8_t tmp = vmovl_u8( vget_low_u8( vreinterpretq_u8_u32(ireg) ) ); \1677 out = vmovl_u16( vget_low_u16( tmp ) ); \1678 }16791680 #define stbir__simdi_expand_u16_to_u32(out0,out1,ireg) \1681 { \1682 uint16x8_t tmp = vreinterpretq_u16_u32(ireg); \1683 out0 = vmovl_u16( vget_low_u16 ( tmp ) ); \1684 out1 = vmovl_u16( vget_high_u16( tmp ) ); \1685 }16861687 #define stbir__simdf_convert_float_to_i32( i, f ) (i) = vreinterpretq_u32_s32( vcvtq_s32_f32(f) )1688 #define stbir__simdf_convert_float_to_int( f ) vgetq_lane_s32(vcvtq_s32_f32(f), 0)1689 #define stbir__simdi_to_int( i ) (int)vgetq_lane_u32(i, 0) 1690 #define stbir__simdf_convert_float_to_uint8( f ) ((unsigned char)vgetq_lane_s32(vcvtq_s32_f32(vmaxq_f32(vminq_f32(f,STBIR__CONSTF(STBIR_max_uint8_as_float)),vdupq_n_f32(0))), 0))1691 #define stbir__simdf_convert_float_to_short( f ) ((unsigned short)vgetq_lane_s32(vcvtq_s32_f32(vmaxq_f32(vminq_f32(f,STBIR__CONSTF(STBIR_max_uint16_as_float)),vdupq_n_f32(0))), 0))1692 #define stbir__simdi_convert_i32_to_float(out, ireg) (out) = vcvtq_f32_s32( vreinterpretq_s32_u32(ireg) )1693 #define stbir__simdf_add( out, reg0, reg1 ) (out) = vaddq_f32( reg0, reg1 )1694 #define stbir__simdf_mult( out, reg0, reg1 ) (out) = vmulq_f32( reg0, reg1 )1695 #define stbir__simdf_mult_mem( out, reg, ptr ) (out) = vmulq_f32( reg, vld1q_f32( (float const*)(ptr) ) )1696 #define stbir__simdf_mult1_mem( out, reg, ptr ) (out) = vmulq_f32( reg, vld1q_dup_f32( (float const*)(ptr) ) )1697 #define stbir__simdf_add_mem( out, reg, ptr ) (out) = vaddq_f32( reg, vld1q_f32( (float const*)(ptr) ) )1698 #define stbir__simdf_add1_mem( out, reg, ptr ) (out) = vaddq_f32( reg, vld1q_dup_f32( (float const*)(ptr) ) )16991700 #ifdef STBIR_USE_FMA // not on by default to maintain bit identical simd to non-simd (and also x64 no madd to arm madd)1701 #define stbir__simdf_madd( out, add, mul1, mul2 ) (out) = vfmaq_f32( add, mul1, mul2 )1702 #define stbir__simdf_madd1( out, add, mul1, mul2 ) (out) = vfmaq_f32( add, mul1, mul2 )1703 #define stbir__simdf_madd_mem( out, add, mul, ptr ) (out) = vfmaq_f32( add, mul, vld1q_f32( (float const*)(ptr) ) )1704 #define stbir__simdf_madd1_mem( out, add, mul, ptr ) (out) = vfmaq_f32( add, mul, vld1q_dup_f32( (float const*)(ptr) ) )1705 #else1706 #define stbir__simdf_madd( out, add, mul1, mul2 ) (out) = vaddq_f32( add, vmulq_f32( mul1, mul2 ) )1707 #define stbir__simdf_madd1( out, add, mul1, mul2 ) (out) = vaddq_f32( add, vmulq_f32( mul1, mul2 ) )1708 #define stbir__simdf_madd_mem( out, add, mul, ptr ) (out) = vaddq_f32( add, vmulq_f32( mul, vld1q_f32( (float const*)(ptr) ) ) )1709 #define stbir__simdf_madd1_mem( out, add, mul, ptr ) (out) = vaddq_f32( add, vmulq_f32( mul, vld1q_dup_f32( (float const*)(ptr) ) ) )1710 #endif17111712 #define stbir__simdf_add1( out, reg0, reg1 ) (out) = vaddq_f32( reg0, reg1 )1713 #define stbir__simdf_mult1( out, reg0, reg1 ) (out) = vmulq_f32( reg0, reg1 )17141715 #define stbir__simdf_and( out, reg0, reg1 ) (out) = vreinterpretq_f32_u32( vandq_u32( vreinterpretq_u32_f32(reg0), vreinterpretq_u32_f32(reg1) ) )1716 #define stbir__simdf_or( out, reg0, reg1 ) (out) = vreinterpretq_f32_u32( vorrq_u32( vreinterpretq_u32_f32(reg0), vreinterpretq_u32_f32(reg1) ) )17171718 #define stbir__simdf_min( out, reg0, reg1 ) (out) = vminq_f32( reg0, reg1 )1719 #define stbir__simdf_max( out, reg0, reg1 ) (out) = vmaxq_f32( reg0, reg1 )1720 #define stbir__simdf_min1( out, reg0, reg1 ) (out) = vminq_f32( reg0, reg1 )1721 #define stbir__simdf_max1( out, reg0, reg1 ) (out) = vmaxq_f32( reg0, reg1 )17221723 #define stbir__simdf_0123ABCDto3ABx( out, reg0, reg1 ) (out) = vextq_f32( reg0, reg1, 3 )1724 #define stbir__simdf_0123ABCDto23Ax( out, reg0, reg1 ) (out) = vextq_f32( reg0, reg1, 2 )17251726 #define stbir__simdf_a1a1( out, alp, ones ) (out) = vzipq_f32(vuzpq_f32(alp, alp).val[1], ones).val[0]1727 #define stbir__simdf_1a1a( out, alp, ones ) (out) = vzipq_f32(ones, vuzpq_f32(alp, alp).val[0]).val[0]17281729 #if defined( _M_ARM64 ) || defined( __aarch64__ ) || defined( __arm64__ )17301731 #define stbir__simdf_aaa1( out, alp, ones ) (out) = vcopyq_laneq_f32(vdupq_n_f32(vgetq_lane_f32(alp, 3)), 3, ones, 3)1732 #define stbir__simdf_1aaa( out, alp, ones ) (out) = vcopyq_laneq_f32(vdupq_n_f32(vgetq_lane_f32(alp, 0)), 0, ones, 0)17331734 #if defined( _MSC_VER ) && !defined(__clang__)1735 #define stbir_make16(a,b,c,d) vcombine_u8( \1736 vcreate_u8( (4*a+0) | ((4*a+1)<<8) | ((4*a+2)<<16) | ((4*a+3)<<24) | \1737 ((stbir_uint64)(4*b+0)<<32) | ((stbir_uint64)(4*b+1)<<40) | ((stbir_uint64)(4*b+2)<<48) | ((stbir_uint64)(4*b+3)<<56)), \1738 vcreate_u8( (4*c+0) | ((4*c+1)<<8) | ((4*c+2)<<16) | ((4*c+3)<<24) | \1739 ((stbir_uint64)(4*d+0)<<32) | ((stbir_uint64)(4*d+1)<<40) | ((stbir_uint64)(4*d+2)<<48) | ((stbir_uint64)(4*d+3)<<56) ) )1740 #else1741 #define stbir_make16(a,b,c,d) (uint8x16_t){4*a+0,4*a+1,4*a+2,4*a+3,4*b+0,4*b+1,4*b+2,4*b+3,4*c+0,4*c+1,4*c+2,4*c+3,4*d+0,4*d+1,4*d+2,4*d+3}1742 #endif17431744 #define stbir__simdf_swiz( reg, one, two, three, four ) vreinterpretq_f32_u8( vqtbl1q_u8( vreinterpretq_u8_f32(reg), stbir_make16(one, two, three, four) ) )1745 1746 #define stbir__simdi_16madd( out, reg0, reg1 ) \1747 { \1748 int16x8_t r0 = vreinterpretq_s16_u32(reg0); \1749 int16x8_t r1 = vreinterpretq_s16_u32(reg1); \1750 int32x4_t tmp0 = vmull_s16( vget_low_s16(r0), vget_low_s16(r1) ); \1751 int32x4_t tmp1 = vmull_s16( vget_high_s16(r0), vget_high_s16(r1) ); \1752 (out) = vreinterpretq_u32_s32( vpaddq_s32(tmp0, tmp1) ); \1753 }17541755 #else17561757 #define stbir__simdf_aaa1( out, alp, ones ) (out) = vsetq_lane_f32(1.0f, vdupq_n_f32(vgetq_lane_f32(alp, 3)), 3)1758 #define stbir__simdf_1aaa( out, alp, ones ) (out) = vsetq_lane_f32(1.0f, vdupq_n_f32(vgetq_lane_f32(alp, 0)), 0)17591760 #if defined( _MSC_VER ) && !defined(__clang__)1761 static stbir__inline uint8x8x2_t stbir_make8x2(float32x4_t reg)1762 {1763 uint8x8x2_t r = { { vget_low_u8(vreinterpretq_u8_f32(reg)), vget_high_u8(vreinterpretq_u8_f32(reg)) } };1764 return r;1765 }1766 #define stbir_make8(a,b) vcreate_u8( \1767 (4*a+0) | ((4*a+1)<<8) | ((4*a+2)<<16) | ((4*a+3)<<24) | \1768 ((stbir_uint64)(4*b+0)<<32) | ((stbir_uint64)(4*b+1)<<40) | ((stbir_uint64)(4*b+2)<<48) | ((stbir_uint64)(4*b+3)<<56) )1769 #else1770 #define stbir_make8x2(reg) (uint8x8x2_t){ { vget_low_u8(vreinterpretq_u8_f32(reg)), vget_high_u8(vreinterpretq_u8_f32(reg)) } }1771 #define stbir_make8(a,b) (uint8x8_t){4*a+0,4*a+1,4*a+2,4*a+3,4*b+0,4*b+1,4*b+2,4*b+3}1772 #endif17731774 #define stbir__simdf_swiz( reg, one, two, three, four ) vreinterpretq_f32_u8( vcombine_u8( \1775 vtbl2_u8( stbir_make8x2( reg ), stbir_make8( one, two ) ), \1776 vtbl2_u8( stbir_make8x2( reg ), stbir_make8( three, four ) ) ) )17771778 #define stbir__simdi_16madd( out, reg0, reg1 ) \1779 { \1780 int16x8_t r0 = vreinterpretq_s16_u32(reg0); \1781 int16x8_t r1 = vreinterpretq_s16_u32(reg1); \1782 int32x4_t tmp0 = vmull_s16( vget_low_s16(r0), vget_low_s16(r1) ); \1783 int32x4_t tmp1 = vmull_s16( vget_high_s16(r0), vget_high_s16(r1) ); \1784 int32x2_t out0 = vpadd_s32( vget_low_s32(tmp0), vget_high_s32(tmp0) ); \1785 int32x2_t out1 = vpadd_s32( vget_low_s32(tmp1), vget_high_s32(tmp1) ); \1786 (out) = vreinterpretq_u32_s32( vcombine_s32(out0, out1) ); \1787 }17881789 #endif17901791 #define stbir__simdi_and( out, reg0, reg1 ) (out) = vandq_u32( reg0, reg1 )1792 #define stbir__simdi_or( out, reg0, reg1 ) (out) = vorrq_u32( reg0, reg1 )17931794 #define stbir__simdf_pack_to_8bytes(out,aa,bb) \1795 { \1796 float32x4_t af = vmaxq_f32( vminq_f32(aa,STBIR__CONSTF(STBIR_max_uint8_as_float) ), vdupq_n_f32(0) ); \1797 float32x4_t bf = vmaxq_f32( vminq_f32(bb,STBIR__CONSTF(STBIR_max_uint8_as_float) ), vdupq_n_f32(0) ); \1798 int16x4_t ai = vqmovn_s32( vcvtq_s32_f32( af ) ); \1799 int16x4_t bi = vqmovn_s32( vcvtq_s32_f32( bf ) ); \1800 uint8x8_t out8 = vqmovun_s16( vcombine_s16(ai, bi) ); \1801 out = vreinterpretq_u32_u8( vcombine_u8(out8, out8) ); \1802 }18031804 #define stbir__simdf_pack_to_8words(out,aa,bb) \1805 { \1806 float32x4_t af = vmaxq_f32( vminq_f32(aa,STBIR__CONSTF(STBIR_max_uint16_as_float) ), vdupq_n_f32(0) ); \1807 float32x4_t bf = vmaxq_f32( vminq_f32(bb,STBIR__CONSTF(STBIR_max_uint16_as_float) ), vdupq_n_f32(0) ); \1808 int32x4_t ai = vcvtq_s32_f32( af ); \1809 int32x4_t bi = vcvtq_s32_f32( bf ); \1810 out = vreinterpretq_u32_u16( vcombine_u16(vqmovun_s32(ai), vqmovun_s32(bi)) ); \1811 }18121813 #define stbir__interleave_pack_and_store_16_u8( ptr, r0, r1, r2, r3 ) \1814 { \1815 int16x4x2_t tmp0 = vzip_s16( vqmovn_s32(vreinterpretq_s32_u32(r0)), vqmovn_s32(vreinterpretq_s32_u32(r2)) ); \1816 int16x4x2_t tmp1 = vzip_s16( vqmovn_s32(vreinterpretq_s32_u32(r1)), vqmovn_s32(vreinterpretq_s32_u32(r3)) ); \1817 uint8x8x2_t out = \1818 { { \1819 vqmovun_s16( vcombine_s16(tmp0.val[0], tmp0.val[1]) ), \1820 vqmovun_s16( vcombine_s16(tmp1.val[0], tmp1.val[1]) ), \1821 } }; \1822 vst2_u8(ptr, out); \1823 }18241825 #define stbir__simdf_load4_transposed( o0, o1, o2, o3, ptr ) \1826 { \1827 float32x4x4_t tmp = vld4q_f32(ptr); \1828 o0 = tmp.val[0]; \1829 o1 = tmp.val[1]; \1830 o2 = tmp.val[2]; \1831 o3 = tmp.val[3]; \1832 }18331834 #define stbir__simdi_32shr( out, reg, imm ) out = vshrq_n_u32( reg, imm )18351836 #if defined( _MSC_VER ) && !defined(__clang__)1837 #define STBIR__SIMDF_CONST(var, x) __declspec(align(8)) float var[] = { x, x, x, x }1838 #define STBIR__SIMDI_CONST(var, x) __declspec(align(8)) uint32_t var[] = { x, x, x, x }1839 #define STBIR__CONSTF(var) (*(const float32x4_t*)var)1840 #define STBIR__CONSTI(var) (*(const uint32x4_t*)var)1841 #else1842 #define STBIR__SIMDF_CONST(var, x) stbir__simdf var = { x, x, x, x }1843 #define STBIR__SIMDI_CONST(var, x) stbir__simdi var = { x, x, x, x }1844 #define STBIR__CONSTF(var) (var)1845 #define STBIR__CONSTI(var) (var)1846 #endif18471848 #ifdef STBIR_FLOORF1849 #undef STBIR_FLOORF1850 #endif1851 #define STBIR_FLOORF stbir_simd_floorf1852 static stbir__inline float stbir_simd_floorf(float x)1853 {1854 #if defined( _M_ARM64 ) || defined( __aarch64__ ) || defined( __arm64__ )1855 return vget_lane_f32( vrndm_f32( vdup_n_f32(x) ), 0);1856 #else1857 float32x2_t f = vdup_n_f32(x);1858 float32x2_t t = vcvt_f32_s32(vcvt_s32_f32(f));1859 uint32x2_t a = vclt_f32(f, t);1860 uint32x2_t b = vreinterpret_u32_f32(vdup_n_f32(-1.0f));1861 float32x2_t r = vadd_f32(t, vreinterpret_f32_u32(vand_u32(a, b)));1862 return vget_lane_f32(r, 0);1863 #endif1864 }18651866 #ifdef STBIR_CEILF1867 #undef STBIR_CEILF1868 #endif1869 #define STBIR_CEILF stbir_simd_ceilf1870 static stbir__inline float stbir_simd_ceilf(float x)1871 {1872 #if defined( _M_ARM64 ) || defined( __aarch64__ ) || defined( __arm64__ )1873 return vget_lane_f32( vrndp_f32( vdup_n_f32(x) ), 0);1874 #else1875 float32x2_t f = vdup_n_f32(x);1876 float32x2_t t = vcvt_f32_s32(vcvt_s32_f32(f));1877 uint32x2_t a = vclt_f32(t, f);1878 uint32x2_t b = vreinterpret_u32_f32(vdup_n_f32(1.0f));1879 float32x2_t r = vadd_f32(t, vreinterpret_f32_u32(vand_u32(a, b)));1880 return vget_lane_f32(r, 0);1881 #endif1882 }18831884 #define STBIR_SIMD18851886#elif defined(STBIR_WASM)18871888 #include <wasm_simd128.h>18891890 #define stbir__simdf v128_t1891 #define stbir__simdi v128_t18921893 #define stbir_simdi_castf( reg ) (reg)1894 #define stbir_simdf_casti( reg ) (reg)18951896 #define stbir__simdf_load( reg, ptr ) (reg) = wasm_v128_load( (void const*)(ptr) )1897 #define stbir__simdi_load( reg, ptr ) (reg) = wasm_v128_load( (void const*)(ptr) )1898 #define stbir__simdf_load1( out, ptr ) (out) = wasm_v128_load32_splat( (void const*)(ptr) ) // top values can be random (not denormal or nan for perf)1899 #define stbir__simdi_load1( out, ptr ) (out) = wasm_v128_load32_splat( (void const*)(ptr) )1900 #define stbir__simdf_load1z( out, ptr ) (out) = wasm_v128_load32_zero( (void const*)(ptr) ) // top values must be zero1901 #define stbir__simdf_frep4( fvar ) wasm_f32x4_splat( fvar )1902 #define stbir__simdf_load1frep4( out, fvar ) (out) = wasm_f32x4_splat( fvar )1903 #define stbir__simdf_load2( out, ptr ) (out) = wasm_v128_load64_splat( (void const*)(ptr) ) // top values can be random (not denormal or nan for perf)1904 #define stbir__simdf_load2z( out, ptr ) (out) = wasm_v128_load64_zero( (void const*)(ptr) ) // top values must be zero1905 #define stbir__simdf_load2hmerge( out, reg, ptr ) (out) = wasm_v128_load64_lane( (void const*)(ptr), reg, 1 )19061907 #define stbir__simdf_zeroP() wasm_f32x4_const_splat(0)1908 #define stbir__simdf_zero( reg ) (reg) = wasm_f32x4_const_splat(0)19091910 #define stbir__simdf_store( ptr, reg ) wasm_v128_store( (void*)(ptr), reg )1911 #define stbir__simdf_store1( ptr, reg ) wasm_v128_store32_lane( (void*)(ptr), reg, 0 )1912 #define stbir__simdf_store2( ptr, reg ) wasm_v128_store64_lane( (void*)(ptr), reg, 0 )1913 #define stbir__simdf_store2h( ptr, reg ) wasm_v128_store64_lane( (void*)(ptr), reg, 1 )19141915 #define stbir__simdi_store( ptr, reg ) wasm_v128_store( (void*)(ptr), reg )1916 #define stbir__simdi_store1( ptr, reg ) wasm_v128_store32_lane( (void*)(ptr), reg, 0 )1917 #define stbir__simdi_store2( ptr, reg ) wasm_v128_store64_lane( (void*)(ptr), reg, 0 )19181919 #define stbir__prefetch( ptr )19201921 #define stbir__simdi_expand_u8_to_u32(out0,out1,out2,out3,ireg) \1922 { \1923 v128_t l = wasm_u16x8_extend_low_u8x16 ( ireg ); \1924 v128_t h = wasm_u16x8_extend_high_u8x16( ireg ); \1925 out0 = wasm_u32x4_extend_low_u16x8 ( l ); \1926 out1 = wasm_u32x4_extend_high_u16x8( l ); \1927 out2 = wasm_u32x4_extend_low_u16x8 ( h ); \1928 out3 = wasm_u32x4_extend_high_u16x8( h ); \1929 }19301931 #define stbir__simdi_expand_u8_to_1u32(out,ireg) \1932 { \1933 v128_t tmp = wasm_u16x8_extend_low_u8x16(ireg); \1934 out = wasm_u32x4_extend_low_u16x8(tmp); \1935 }19361937 #define stbir__simdi_expand_u16_to_u32(out0,out1,ireg) \1938 { \1939 out0 = wasm_u32x4_extend_low_u16x8 ( ireg ); \1940 out1 = wasm_u32x4_extend_high_u16x8( ireg ); \1941 }19421943 #define stbir__simdf_convert_float_to_i32( i, f ) (i) = wasm_i32x4_trunc_sat_f32x4(f)1944 #define stbir__simdf_convert_float_to_int( f ) wasm_i32x4_extract_lane(wasm_i32x4_trunc_sat_f32x4(f), 0)1945 #define stbir__simdi_to_int( i ) wasm_i32x4_extract_lane(i, 0) 1946 #define stbir__simdf_convert_float_to_uint8( f ) ((unsigned char)wasm_i32x4_extract_lane(wasm_i32x4_trunc_sat_f32x4(wasm_f32x4_max(wasm_f32x4_min(f,STBIR_max_uint8_as_float),wasm_f32x4_const_splat(0))), 0))1947 #define stbir__simdf_convert_float_to_short( f ) ((unsigned short)wasm_i32x4_extract_lane(wasm_i32x4_trunc_sat_f32x4(wasm_f32x4_max(wasm_f32x4_min(f,STBIR_max_uint16_as_float),wasm_f32x4_const_splat(0))), 0))1948 #define stbir__simdi_convert_i32_to_float(out, ireg) (out) = wasm_f32x4_convert_i32x4(ireg)1949 #define stbir__simdf_add( out, reg0, reg1 ) (out) = wasm_f32x4_add( reg0, reg1 )1950 #define stbir__simdf_mult( out, reg0, reg1 ) (out) = wasm_f32x4_mul( reg0, reg1 )1951 #define stbir__simdf_mult_mem( out, reg, ptr ) (out) = wasm_f32x4_mul( reg, wasm_v128_load( (void const*)(ptr) ) )1952 #define stbir__simdf_mult1_mem( out, reg, ptr ) (out) = wasm_f32x4_mul( reg, wasm_v128_load32_splat( (void const*)(ptr) ) )1953 #define stbir__simdf_add_mem( out, reg, ptr ) (out) = wasm_f32x4_add( reg, wasm_v128_load( (void const*)(ptr) ) )1954 #define stbir__simdf_add1_mem( out, reg, ptr ) (out) = wasm_f32x4_add( reg, wasm_v128_load32_splat( (void const*)(ptr) ) )19551956 #define stbir__simdf_madd( out, add, mul1, mul2 ) (out) = wasm_f32x4_add( add, wasm_f32x4_mul( mul1, mul2 ) )1957 #define stbir__simdf_madd1( out, add, mul1, mul2 ) (out) = wasm_f32x4_add( add, wasm_f32x4_mul( mul1, mul2 ) )1958 #define stbir__simdf_madd_mem( out, add, mul, ptr ) (out) = wasm_f32x4_add( add, wasm_f32x4_mul( mul, wasm_v128_load( (void const*)(ptr) ) ) )1959 #define stbir__simdf_madd1_mem( out, add, mul, ptr ) (out) = wasm_f32x4_add( add, wasm_f32x4_mul( mul, wasm_v128_load32_splat( (void const*)(ptr) ) ) )19601961 #define stbir__simdf_add1( out, reg0, reg1 ) (out) = wasm_f32x4_add( reg0, reg1 )1962 #define stbir__simdf_mult1( out, reg0, reg1 ) (out) = wasm_f32x4_mul( reg0, reg1 )19631964 #define stbir__simdf_and( out, reg0, reg1 ) (out) = wasm_v128_and( reg0, reg1 )1965 #define stbir__simdf_or( out, reg0, reg1 ) (out) = wasm_v128_or( reg0, reg1 )19661967 #define stbir__simdf_min( out, reg0, reg1 ) (out) = wasm_f32x4_min( reg0, reg1 )1968 #define stbir__simdf_max( out, reg0, reg1 ) (out) = wasm_f32x4_max( reg0, reg1 )1969 #define stbir__simdf_min1( out, reg0, reg1 ) (out) = wasm_f32x4_min( reg0, reg1 )1970 #define stbir__simdf_max1( out, reg0, reg1 ) (out) = wasm_f32x4_max( reg0, reg1 )19711972 #define stbir__simdf_0123ABCDto3ABx( out, reg0, reg1 ) (out) = wasm_i32x4_shuffle( reg0, reg1, 3, 4, 5, -1 )1973 #define stbir__simdf_0123ABCDto23Ax( out, reg0, reg1 ) (out) = wasm_i32x4_shuffle( reg0, reg1, 2, 3, 4, -1 )19741975 #define stbir__simdf_aaa1(out,alp,ones) (out) = wasm_i32x4_shuffle(alp, ones, 3, 3, 3, 4)1976 #define stbir__simdf_1aaa(out,alp,ones) (out) = wasm_i32x4_shuffle(alp, ones, 4, 0, 0, 0)1977 #define stbir__simdf_a1a1(out,alp,ones) (out) = wasm_i32x4_shuffle(alp, ones, 1, 4, 3, 4)1978 #define stbir__simdf_1a1a(out,alp,ones) (out) = wasm_i32x4_shuffle(alp, ones, 4, 0, 4, 2)19791980 #define stbir__simdf_swiz( reg, one, two, three, four ) wasm_i32x4_shuffle(reg, reg, one, two, three, four)19811982 #define stbir__simdi_and( out, reg0, reg1 ) (out) = wasm_v128_and( reg0, reg1 )1983 #define stbir__simdi_or( out, reg0, reg1 ) (out) = wasm_v128_or( reg0, reg1 )1984 #define stbir__simdi_16madd( out, reg0, reg1 ) (out) = wasm_i32x4_dot_i16x8( reg0, reg1 )19851986 #define stbir__simdf_pack_to_8bytes(out,aa,bb) \1987 { \1988 v128_t af = wasm_f32x4_max( wasm_f32x4_min(aa, STBIR_max_uint8_as_float), wasm_f32x4_const_splat(0) ); \1989 v128_t bf = wasm_f32x4_max( wasm_f32x4_min(bb, STBIR_max_uint8_as_float), wasm_f32x4_const_splat(0) ); \1990 v128_t ai = wasm_i32x4_trunc_sat_f32x4( af ); \1991 v128_t bi = wasm_i32x4_trunc_sat_f32x4( bf ); \1992 v128_t out16 = wasm_i16x8_narrow_i32x4( ai, bi ); \1993 out = wasm_u8x16_narrow_i16x8( out16, out16 ); \1994 }19951996 #define stbir__simdf_pack_to_8words(out,aa,bb) \1997 { \1998 v128_t af = wasm_f32x4_max( wasm_f32x4_min(aa, STBIR_max_uint16_as_float), wasm_f32x4_const_splat(0)); \1999 v128_t bf = wasm_f32x4_max( wasm_f32x4_min(bb, STBIR_max_uint16_as_float), wasm_f32x4_const_splat(0)); \2000 v128_t ai = wasm_i32x4_trunc_sat_f32x4( af ); \2001 v128_t bi = wasm_i32x4_trunc_sat_f32x4( bf ); \2002 out = wasm_u16x8_narrow_i32x4( ai, bi ); \2003 }20042005 #define stbir__interleave_pack_and_store_16_u8( ptr, r0, r1, r2, r3 ) \2006 { \2007 v128_t tmp0 = wasm_i16x8_narrow_i32x4(r0, r1); \2008 v128_t tmp1 = wasm_i16x8_narrow_i32x4(r2, r3); \2009 v128_t tmp = wasm_u8x16_narrow_i16x8(tmp0, tmp1); \2010 tmp = wasm_i8x16_shuffle(tmp, tmp, 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15); \2011 wasm_v128_store( (void*)(ptr), tmp); \2012 }20132014 #define stbir__simdf_load4_transposed( o0, o1, o2, o3, ptr ) \2015 { \2016 v128_t t0 = wasm_v128_load( ptr ); \2017 v128_t t1 = wasm_v128_load( ptr+4 ); \2018 v128_t t2 = wasm_v128_load( ptr+8 ); \2019 v128_t t3 = wasm_v128_load( ptr+12 ); \2020 v128_t s0 = wasm_i32x4_shuffle(t0, t1, 0, 4, 2, 6); \2021 v128_t s1 = wasm_i32x4_shuffle(t0, t1, 1, 5, 3, 7); \2022 v128_t s2 = wasm_i32x4_shuffle(t2, t3, 0, 4, 2, 6); \2023 v128_t s3 = wasm_i32x4_shuffle(t2, t3, 1, 5, 3, 7); \2024 o0 = wasm_i32x4_shuffle(s0, s2, 0, 1, 4, 5); \2025 o1 = wasm_i32x4_shuffle(s1, s3, 0, 1, 4, 5); \2026 o2 = wasm_i32x4_shuffle(s0, s2, 2, 3, 6, 7); \2027 o3 = wasm_i32x4_shuffle(s1, s3, 2, 3, 6, 7); \2028 }20292030 #define stbir__simdi_32shr( out, reg, imm ) out = wasm_u32x4_shr( reg, imm )20312032 typedef float stbir__f32x4 __attribute__((__vector_size__(16), __aligned__(16)));2033 #define STBIR__SIMDF_CONST(var, x) stbir__simdf var = (v128_t)(stbir__f32x4){ x, x, x, x }2034 #define STBIR__SIMDI_CONST(var, x) stbir__simdi var = { x, x, x, x }2035 #define STBIR__CONSTF(var) (var)2036 #define STBIR__CONSTI(var) (var)20372038 #ifdef STBIR_FLOORF2039 #undef STBIR_FLOORF2040 #endif2041 #define STBIR_FLOORF stbir_simd_floorf2042 static stbir__inline float stbir_simd_floorf(float x)2043 {2044 return wasm_f32x4_extract_lane( wasm_f32x4_floor( wasm_f32x4_splat(x) ), 0);2045 }20462047 #ifdef STBIR_CEILF2048 #undef STBIR_CEILF2049 #endif2050 #define STBIR_CEILF stbir_simd_ceilf2051 static stbir__inline float stbir_simd_ceilf(float x)2052 {2053 return wasm_f32x4_extract_lane( wasm_f32x4_ceil( wasm_f32x4_splat(x) ), 0);2054 }20552056 #define STBIR_SIMD20572058#endif // SSE2/NEON/WASM20592060#endif // NO SIMD20612062#ifdef STBIR_SIMD82063 #define stbir__simdfX stbir__simdf82064 #define stbir__simdiX stbir__simdi82065 #define stbir__simdfX_load stbir__simdf8_load2066 #define stbir__simdiX_load stbir__simdi8_load2067 #define stbir__simdfX_mult stbir__simdf8_mult2068 #define stbir__simdfX_add_mem stbir__simdf8_add_mem2069 #define stbir__simdfX_madd_mem stbir__simdf8_madd_mem2070 #define stbir__simdfX_store stbir__simdf8_store2071 #define stbir__simdiX_store stbir__simdi8_store2072 #define stbir__simdf_frepX stbir__simdf8_frep82073 #define stbir__simdfX_madd stbir__simdf8_madd2074 #define stbir__simdfX_min stbir__simdf8_min2075 #define stbir__simdfX_max stbir__simdf8_max2076 #define stbir__simdfX_aaa1 stbir__simdf8_aaa12077 #define stbir__simdfX_1aaa stbir__simdf8_1aaa2078 #define stbir__simdfX_a1a1 stbir__simdf8_a1a12079 #define stbir__simdfX_1a1a stbir__simdf8_1a1a2080 #define stbir__simdfX_convert_float_to_i32 stbir__simdf8_convert_float_to_i322081 #define stbir__simdfX_pack_to_words stbir__simdf8_pack_to_16words2082 #define stbir__simdfX_zero stbir__simdf8_zero2083 #define STBIR_onesX STBIR_ones82084 #define STBIR_max_uint8_as_floatX STBIR_max_uint8_as_float82085 #define STBIR_max_uint16_as_floatX STBIR_max_uint16_as_float82086 #define STBIR_simd_point5X STBIR_simd_point582087 #define stbir__simdfX_float_count 82088 #define stbir__simdfX_0123to1230 stbir__simdf8_0123to123012302089 #define stbir__simdfX_0123to2103 stbir__simdf8_0123to210321032090 static const stbir__simdf8 STBIR_max_uint16_as_float_inverted8 = { stbir__max_uint16_as_float_inverted,stbir__max_uint16_as_float_inverted,stbir__max_uint16_as_float_inverted,stbir__max_uint16_as_float_inverted,stbir__max_uint16_as_float_inverted,stbir__max_uint16_as_float_inverted,stbir__max_uint16_as_float_inverted,stbir__max_uint16_as_float_inverted };2091 static const stbir__simdf8 STBIR_max_uint8_as_float_inverted8 = { stbir__max_uint8_as_float_inverted,stbir__max_uint8_as_float_inverted,stbir__max_uint8_as_float_inverted,stbir__max_uint8_as_float_inverted,stbir__max_uint8_as_float_inverted,stbir__max_uint8_as_float_inverted,stbir__max_uint8_as_float_inverted,stbir__max_uint8_as_float_inverted };2092 static const stbir__simdf8 STBIR_ones8 = { 1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0 };2093 static const stbir__simdf8 STBIR_simd_point58 = { 0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5 };2094 static const stbir__simdf8 STBIR_max_uint8_as_float8 = { stbir__max_uint8_as_float,stbir__max_uint8_as_float,stbir__max_uint8_as_float,stbir__max_uint8_as_float, stbir__max_uint8_as_float,stbir__max_uint8_as_float,stbir__max_uint8_as_float,stbir__max_uint8_as_float };2095 static const stbir__simdf8 STBIR_max_uint16_as_float8 = { stbir__max_uint16_as_float,stbir__max_uint16_as_float,stbir__max_uint16_as_float,stbir__max_uint16_as_float, stbir__max_uint16_as_float,stbir__max_uint16_as_float,stbir__max_uint16_as_float,stbir__max_uint16_as_float };2096#else2097 #define stbir__simdfX stbir__simdf2098 #define stbir__simdiX stbir__simdi2099 #define stbir__simdfX_load stbir__simdf_load2100 #define stbir__simdiX_load stbir__simdi_load2101 #define stbir__simdfX_mult stbir__simdf_mult2102 #define stbir__simdfX_add_mem stbir__simdf_add_mem2103 #define stbir__simdfX_madd_mem stbir__simdf_madd_mem2104 #define stbir__simdfX_store stbir__simdf_store2105 #define stbir__simdiX_store stbir__simdi_store2106 #define stbir__simdf_frepX stbir__simdf_frep42107 #define stbir__simdfX_madd stbir__simdf_madd2108 #define stbir__simdfX_min stbir__simdf_min2109 #define stbir__simdfX_max stbir__simdf_max2110 #define stbir__simdfX_aaa1 stbir__simdf_aaa12111 #define stbir__simdfX_1aaa stbir__simdf_1aaa2112 #define stbir__simdfX_a1a1 stbir__simdf_a1a12113 #define stbir__simdfX_1a1a stbir__simdf_1a1a2114 #define stbir__simdfX_convert_float_to_i32 stbir__simdf_convert_float_to_i322115 #define stbir__simdfX_pack_to_words stbir__simdf_pack_to_8words2116 #define stbir__simdfX_zero stbir__simdf_zero2117 #define STBIR_onesX STBIR__CONSTF(STBIR_ones)2118 #define STBIR_simd_point5X STBIR__CONSTF(STBIR_simd_point5)2119 #define STBIR_max_uint8_as_floatX STBIR__CONSTF(STBIR_max_uint8_as_float)2120 #define STBIR_max_uint16_as_floatX STBIR__CONSTF(STBIR_max_uint16_as_float)2121 #define stbir__simdfX_float_count 42122 #define stbir__if_simdf8_cast_to_simdf4( val ) ( val )2123 #define stbir__simdfX_0123to1230 stbir__simdf_0123to12302124 #define stbir__simdfX_0123to2103 stbir__simdf_0123to21032125#endif212621272128#if defined(STBIR_NEON) && !defined(_M_ARM)21292130 #if defined( _MSC_VER ) && !defined(__clang__)2131 typedef __int16 stbir__FP16;2132 #else2133 typedef float16_t stbir__FP16;2134 #endif21352136#else // no NEON, or 32-bit ARM for MSVC21372138 typedef union stbir__FP162139 {2140 unsigned short u;2141 } stbir__FP16;21422143#endif21442145#if !defined(STBIR_NEON) && !defined(STBIR_FP16C) || defined(STBIR_NEON) && defined(_M_ARM)21462147 // Fabian's half float routines, see: https://gist.github.com/rygorous/215666821482149 static stbir__inline float stbir__half_to_float( stbir__FP16 h )2150 {2151 static const stbir__FP32 magic = { (254 - 15) << 23 };2152 static const stbir__FP32 was_infnan = { (127 + 16) << 23 };2153 stbir__FP32 o;21542155 o.u = (h.u & 0x7fff) << 13; // exponent/mantissa bits2156 o.f *= magic.f; // exponent adjust2157 if (o.f >= was_infnan.f) // make sure Inf/NaN survive2158 o.u |= 255 << 23;2159 o.u |= (h.u & 0x8000) << 16; // sign bit2160 return o.f;2161 }21622163 static stbir__inline stbir__FP16 stbir__float_to_half(float val)2164 {2165 stbir__FP32 f32infty = { 255 << 23 };2166 stbir__FP32 f16max = { (127 + 16) << 23 };2167 stbir__FP32 denorm_magic = { ((127 - 15) + (23 - 10) + 1) << 23 };2168 unsigned int sign_mask = 0x80000000u;2169 stbir__FP16 o = { 0 };2170 stbir__FP32 f;2171 unsigned int sign; 21722173 f.f = val;2174 sign = f.u & sign_mask;2175 f.u ^= sign;21762177 if (f.u >= f16max.u) // result is Inf or NaN (all exponent bits set)2178 o.u = (f.u > f32infty.u) ? 0x7e00 : 0x7c00; // NaN->qNaN and Inf->Inf2179 else // (De)normalized number or zero2180 {2181 if (f.u < (113 << 23)) // resulting FP16 is subnormal or zero2182 {2183 // use a magic value to align our 10 mantissa bits at the bottom of2184 // the float. as long as FP addition is round-to-nearest-even this2185 // just works.2186 f.f += denorm_magic.f;2187 // and one integer subtract of the bias later, we have our final float!2188 o.u = (unsigned short) ( f.u - denorm_magic.u );2189 }2190 else2191 {2192 unsigned int mant_odd = (f.u >> 13) & 1; // resulting mantissa is odd2193 // update exponent, rounding bias part 12194 f.u = f.u + ((15u - 127) << 23) + 0xfff;2195 // rounding bias part 22196 f.u += mant_odd;2197 // take the bits!2198 o.u = (unsigned short) ( f.u >> 13 );2199 }2200 }22012202 o.u |= sign >> 16;2203 return o;2204 }22052206#endif220722082209#if defined(STBIR_FP16C)22102211 #include <immintrin.h>22122213 static stbir__inline void stbir__half_to_float_SIMD(float * output, stbir__FP16 const * input)2214 {2215 _mm256_storeu_ps( (float*)output, _mm256_cvtph_ps( _mm_loadu_si128( (__m128i const* )input ) ) );2216 }22172218 static stbir__inline void stbir__float_to_half_SIMD(stbir__FP16 * output, float const * input)2219 {2220 _mm_storeu_si128( (__m128i*)output, _mm256_cvtps_ph( _mm256_loadu_ps( input ), 0 ) );2221 }22222223 static stbir__inline float stbir__half_to_float( stbir__FP16 h )2224 {2225 return _mm_cvtss_f32( _mm_cvtph_ps( _mm_cvtsi32_si128( (int)h.u ) ) );2226 }22272228 static stbir__inline stbir__FP16 stbir__float_to_half( float f )2229 {2230 stbir__FP16 h;2231 h.u = (unsigned short) _mm_cvtsi128_si32( _mm_cvtps_ph( _mm_set_ss( f ), 0 ) );2232 return h;2233 }22342235#elif defined(STBIR_SSE2)22362237 // Fabian's half float routines, see: https://gist.github.com/rygorous/21566682238 stbir__inline static void stbir__half_to_float_SIMD(float * output, void const * input)2239 {2240 static const STBIR__SIMDI_CONST(mask_nosign, 0x7fff);2241 static const STBIR__SIMDI_CONST(smallest_normal, 0x0400);2242 static const STBIR__SIMDI_CONST(infinity, 0x7c00);2243 static const STBIR__SIMDI_CONST(expadjust_normal, (127 - 15) << 23);2244 static const STBIR__SIMDI_CONST(magic_denorm, 113 << 23);22452246 __m128i i = _mm_loadu_si128 ( (__m128i const*)(input) );2247 __m128i h = _mm_unpacklo_epi16 ( i, _mm_setzero_si128() );2248 __m128i mnosign = STBIR__CONSTI(mask_nosign);2249 __m128i eadjust = STBIR__CONSTI(expadjust_normal);2250 __m128i smallest = STBIR__CONSTI(smallest_normal);2251 __m128i infty = STBIR__CONSTI(infinity);2252 __m128i expmant = _mm_and_si128(mnosign, h);2253 __m128i justsign = _mm_xor_si128(h, expmant);2254 __m128i b_notinfnan = _mm_cmpgt_epi32(infty, expmant);2255 __m128i b_isdenorm = _mm_cmpgt_epi32(smallest, expmant);2256 __m128i shifted = _mm_slli_epi32(expmant, 13);2257 __m128i adj_infnan = _mm_andnot_si128(b_notinfnan, eadjust);2258 __m128i adjusted = _mm_add_epi32(eadjust, shifted);2259 __m128i den1 = _mm_add_epi32(shifted, STBIR__CONSTI(magic_denorm));2260 __m128i adjusted2 = _mm_add_epi32(adjusted, adj_infnan);2261 __m128 den2 = _mm_sub_ps(_mm_castsi128_ps(den1), *(const __m128 *)&magic_denorm);2262 __m128 adjusted3 = _mm_and_ps(den2, _mm_castsi128_ps(b_isdenorm));2263 __m128 adjusted4 = _mm_andnot_ps(_mm_castsi128_ps(b_isdenorm), _mm_castsi128_ps(adjusted2));2264 __m128 adjusted5 = _mm_or_ps(adjusted3, adjusted4);2265 __m128i sign = _mm_slli_epi32(justsign, 16);2266 __m128 final = _mm_or_ps(adjusted5, _mm_castsi128_ps(sign));2267 stbir__simdf_store( output + 0, final );22682269 h = _mm_unpackhi_epi16 ( i, _mm_setzero_si128() );2270 expmant = _mm_and_si128(mnosign, h);2271 justsign = _mm_xor_si128(h, expmant);2272 b_notinfnan = _mm_cmpgt_epi32(infty, expmant);2273 b_isdenorm = _mm_cmpgt_epi32(smallest, expmant);2274 shifted = _mm_slli_epi32(expmant, 13);2275 adj_infnan = _mm_andnot_si128(b_notinfnan, eadjust);2276 adjusted = _mm_add_epi32(eadjust, shifted);2277 den1 = _mm_add_epi32(shifted, STBIR__CONSTI(magic_denorm));2278 adjusted2 = _mm_add_epi32(adjusted, adj_infnan);2279 den2 = _mm_sub_ps(_mm_castsi128_ps(den1), *(const __m128 *)&magic_denorm);2280 adjusted3 = _mm_and_ps(den2, _mm_castsi128_ps(b_isdenorm));2281 adjusted4 = _mm_andnot_ps(_mm_castsi128_ps(b_isdenorm), _mm_castsi128_ps(adjusted2));2282 adjusted5 = _mm_or_ps(adjusted3, adjusted4);2283 sign = _mm_slli_epi32(justsign, 16);2284 final = _mm_or_ps(adjusted5, _mm_castsi128_ps(sign));2285 stbir__simdf_store( output + 4, final );22862287 // ~38 SSE2 ops for 8 values2288 }22892290 // Fabian's round-to-nearest-even float to half2291 // ~48 SSE2 ops for 8 output2292 stbir__inline static void stbir__float_to_half_SIMD(void * output, float const * input)2293 {2294 static const STBIR__SIMDI_CONST(mask_sign, 0x80000000u);2295 static const STBIR__SIMDI_CONST(c_f16max, (127 + 16) << 23); // all FP32 values >=this round to +inf2296 static const STBIR__SIMDI_CONST(c_nanbit, 0x200);2297 static const STBIR__SIMDI_CONST(c_infty_as_fp16, 0x7c00);2298 static const STBIR__SIMDI_CONST(c_min_normal, (127 - 14) << 23); // smallest FP32 that yields a normalized FP162299 static const STBIR__SIMDI_CONST(c_subnorm_magic, ((127 - 15) + (23 - 10) + 1) << 23);2300 static const STBIR__SIMDI_CONST(c_normal_bias, 0xfff - ((127 - 15) << 23)); // adjust exponent and add mantissa rounding23012302 __m128 f = _mm_loadu_ps(input);2303 __m128 msign = _mm_castsi128_ps(STBIR__CONSTI(mask_sign));2304 __m128 justsign = _mm_and_ps(msign, f);2305 __m128 absf = _mm_xor_ps(f, justsign);2306 __m128i absf_int = _mm_castps_si128(absf); // the cast is "free" (extra bypass latency, but no thruput hit)2307 __m128i f16max = STBIR__CONSTI(c_f16max);2308 __m128 b_isnan = _mm_cmpunord_ps(absf, absf); // is this a NaN?2309 __m128i b_isregular = _mm_cmpgt_epi32(f16max, absf_int); // (sub)normalized or special?2310 __m128i nanbit = _mm_and_si128(_mm_castps_si128(b_isnan), STBIR__CONSTI(c_nanbit));2311 __m128i inf_or_nan = _mm_or_si128(nanbit, STBIR__CONSTI(c_infty_as_fp16)); // output for specials23122313 __m128i min_normal = STBIR__CONSTI(c_min_normal);2314 __m128i b_issub = _mm_cmpgt_epi32(min_normal, absf_int);23152316 // "result is subnormal" path2317 __m128 subnorm1 = _mm_add_ps(absf, _mm_castsi128_ps(STBIR__CONSTI(c_subnorm_magic))); // magic value to round output mantissa2318 __m128i subnorm2 = _mm_sub_epi32(_mm_castps_si128(subnorm1), STBIR__CONSTI(c_subnorm_magic)); // subtract out bias23192320 // "result is normal" path2321 __m128i mantoddbit = _mm_slli_epi32(absf_int, 31 - 13); // shift bit 13 (mantissa LSB) to sign2322 __m128i mantodd = _mm_srai_epi32(mantoddbit, 31); // -1 if FP16 mantissa odd, else 023232324 __m128i round1 = _mm_add_epi32(absf_int, STBIR__CONSTI(c_normal_bias));2325 __m128i round2 = _mm_sub_epi32(round1, mantodd); // if mantissa LSB odd, bias towards rounding up (RTNE)2326 __m128i normal = _mm_srli_epi32(round2, 13); // rounded result23272328 // combine the two non-specials2329 __m128i nonspecial = _mm_or_si128(_mm_and_si128(subnorm2, b_issub), _mm_andnot_si128(b_issub, normal));23302331 // merge in specials as well2332 __m128i joined = _mm_or_si128(_mm_and_si128(nonspecial, b_isregular), _mm_andnot_si128(b_isregular, inf_or_nan));23332334 __m128i sign_shift = _mm_srai_epi32(_mm_castps_si128(justsign), 16);2335 __m128i final2, final= _mm_or_si128(joined, sign_shift);23362337 f = _mm_loadu_ps(input+4);2338 justsign = _mm_and_ps(msign, f);2339 absf = _mm_xor_ps(f, justsign);2340 absf_int = _mm_castps_si128(absf); // the cast is "free" (extra bypass latency, but no thruput hit)2341 b_isnan = _mm_cmpunord_ps(absf, absf); // is this a NaN?2342 b_isregular = _mm_cmpgt_epi32(f16max, absf_int); // (sub)normalized or special?2343 nanbit = _mm_and_si128(_mm_castps_si128(b_isnan), c_nanbit);2344 inf_or_nan = _mm_or_si128(nanbit, STBIR__CONSTI(c_infty_as_fp16)); // output for specials23452346 b_issub = _mm_cmpgt_epi32(min_normal, absf_int);23472348 // "result is subnormal" path2349 subnorm1 = _mm_add_ps(absf, _mm_castsi128_ps(STBIR__CONSTI(c_subnorm_magic))); // magic value to round output mantissa2350 subnorm2 = _mm_sub_epi32(_mm_castps_si128(subnorm1), STBIR__CONSTI(c_subnorm_magic)); // subtract out bias23512352 // "result is normal" path2353 mantoddbit = _mm_slli_epi32(absf_int, 31 - 13); // shift bit 13 (mantissa LSB) to sign2354 mantodd = _mm_srai_epi32(mantoddbit, 31); // -1 if FP16 mantissa odd, else 023552356 round1 = _mm_add_epi32(absf_int, STBIR__CONSTI(c_normal_bias));2357 round2 = _mm_sub_epi32(round1, mantodd); // if mantissa LSB odd, bias towards rounding up (RTNE)2358 normal = _mm_srli_epi32(round2, 13); // rounded result23592360 // combine the two non-specials2361 nonspecial = _mm_or_si128(_mm_and_si128(subnorm2, b_issub), _mm_andnot_si128(b_issub, normal));23622363 // merge in specials as well2364 joined = _mm_or_si128(_mm_and_si128(nonspecial, b_isregular), _mm_andnot_si128(b_isregular, inf_or_nan));23652366 sign_shift = _mm_srai_epi32(_mm_castps_si128(justsign), 16);2367 final2 = _mm_or_si128(joined, sign_shift);2368 final = _mm_packs_epi32(final, final2);2369 stbir__simdi_store( output,final );2370 }23712372#elif defined(STBIR_WASM) || (defined(STBIR_NEON) && defined(_MSC_VER) && defined(_M_ARM)) // WASM or 32-bit ARM on MSVC/clang23732374 static stbir__inline void stbir__half_to_float_SIMD(float * output, stbir__FP16 const * input)2375 {2376 for (int i=0; i<8; i++)2377 {2378 output[i] = stbir__half_to_float(input[i]);2379 }2380 }23812382 static stbir__inline void stbir__float_to_half_SIMD(stbir__FP16 * output, float const * input)2383 {2384 for (int i=0; i<8; i++)2385 {2386 output[i] = stbir__float_to_half(input[i]);2387 }2388 }23892390#elif defined(STBIR_NEON) && defined(_MSC_VER) && defined(_M_ARM64) && !defined(__clang__) // 64-bit ARM on MSVC (not clang)23912392 static stbir__inline void stbir__half_to_float_SIMD(float * output, stbir__FP16 const * input)2393 {2394 float16x4_t in0 = vld1_f16(input + 0);2395 float16x4_t in1 = vld1_f16(input + 4);2396 vst1q_f32(output + 0, vcvt_f32_f16(in0));2397 vst1q_f32(output + 4, vcvt_f32_f16(in1));2398 }23992400 static stbir__inline void stbir__float_to_half_SIMD(stbir__FP16 * output, float const * input)2401 {2402 float16x4_t out0 = vcvt_f16_f32(vld1q_f32(input + 0));2403 float16x4_t out1 = vcvt_f16_f32(vld1q_f32(input + 4));2404 vst1_f16(output+0, out0);2405 vst1_f16(output+4, out1);2406 }24072408 static stbir__inline float stbir__half_to_float( stbir__FP16 h )2409 {2410 return vgetq_lane_f32(vcvt_f32_f16(vld1_dup_f16(&h)), 0);2411 }24122413 static stbir__inline stbir__FP16 stbir__float_to_half( float f )2414 {2415 return vget_lane_f16(vcvt_f16_f32(vdupq_n_f32(f)), 0).n16_u16[0];2416 }24172418#elif defined(STBIR_NEON) // 64-bit ARM24192420 static stbir__inline void stbir__half_to_float_SIMD(float * output, stbir__FP16 const * input)2421 {2422 float16x8_t in = vld1q_f16(input);2423 vst1q_f32(output + 0, vcvt_f32_f16(vget_low_f16(in)));2424 vst1q_f32(output + 4, vcvt_f32_f16(vget_high_f16(in)));2425 }24262427 static stbir__inline void stbir__float_to_half_SIMD(stbir__FP16 * output, float const * input)2428 {2429 float16x4_t out0 = vcvt_f16_f32(vld1q_f32(input + 0));2430 float16x4_t out1 = vcvt_f16_f32(vld1q_f32(input + 4));2431 vst1q_f16(output, vcombine_f16(out0, out1));2432 }24332434 static stbir__inline float stbir__half_to_float( stbir__FP16 h )2435 {2436 return vgetq_lane_f32(vcvt_f32_f16(vdup_n_f16(h)), 0);2437 }24382439 static stbir__inline stbir__FP16 stbir__float_to_half( float f )2440 {2441 return vget_lane_f16(vcvt_f16_f32(vdupq_n_f32(f)), 0);2442 }24432444#endif244524462447#ifdef STBIR_SIMD24482449#define stbir__simdf_0123to3333( out, reg ) (out) = stbir__simdf_swiz( reg, 3,3,3,3 )2450#define stbir__simdf_0123to2222( out, reg ) (out) = stbir__simdf_swiz( reg, 2,2,2,2 )2451#define stbir__simdf_0123to1111( out, reg ) (out) = stbir__simdf_swiz( reg, 1,1,1,1 )2452#define stbir__simdf_0123to0000( out, reg ) (out) = stbir__simdf_swiz( reg, 0,0,0,0 )2453#define stbir__simdf_0123to0003( out, reg ) (out) = stbir__simdf_swiz( reg, 0,0,0,3 )2454#define stbir__simdf_0123to0001( out, reg ) (out) = stbir__simdf_swiz( reg, 0,0,0,1 )2455#define stbir__simdf_0123to1122( out, reg ) (out) = stbir__simdf_swiz( reg, 1,1,2,2 )2456#define stbir__simdf_0123to2333( out, reg ) (out) = stbir__simdf_swiz( reg, 2,3,3,3 )2457#define stbir__simdf_0123to0023( out, reg ) (out) = stbir__simdf_swiz( reg, 0,0,2,3 )2458#define stbir__simdf_0123to1230( out, reg ) (out) = stbir__simdf_swiz( reg, 1,2,3,0 )2459#define stbir__simdf_0123to2103( out, reg ) (out) = stbir__simdf_swiz( reg, 2,1,0,3 )2460#define stbir__simdf_0123to3210( out, reg ) (out) = stbir__simdf_swiz( reg, 3,2,1,0 )2461#define stbir__simdf_0123to2301( out, reg ) (out) = stbir__simdf_swiz( reg, 2,3,0,1 )2462#define stbir__simdf_0123to3012( out, reg ) (out) = stbir__simdf_swiz( reg, 3,0,1,2 )2463#define stbir__simdf_0123to0011( out, reg ) (out) = stbir__simdf_swiz( reg, 0,0,1,1 )2464#define stbir__simdf_0123to1100( out, reg ) (out) = stbir__simdf_swiz( reg, 1,1,0,0 )2465#define stbir__simdf_0123to2233( out, reg ) (out) = stbir__simdf_swiz( reg, 2,2,3,3 ) 2466#define stbir__simdf_0123to1133( out, reg ) (out) = stbir__simdf_swiz( reg, 1,1,3,3 ) 2467#define stbir__simdf_0123to0022( out, reg ) (out) = stbir__simdf_swiz( reg, 0,0,2,2 ) 2468#define stbir__simdf_0123to1032( out, reg ) (out) = stbir__simdf_swiz( reg, 1,0,3,2 ) 24692470typedef union stbir__simdi_u322471{2472 stbir_uint32 m128i_u32[4];2473 int m128i_i32[4];2474 stbir__simdi m128i_i128;2475} stbir__simdi_u32;24762477static const int STBIR_mask[9] = { 0,0,0,-1,-1,-1,0,0,0 };24782479static const STBIR__SIMDF_CONST(STBIR_max_uint8_as_float, stbir__max_uint8_as_float);2480static const STBIR__SIMDF_CONST(STBIR_max_uint16_as_float, stbir__max_uint16_as_float);2481static const STBIR__SIMDF_CONST(STBIR_max_uint8_as_float_inverted, stbir__max_uint8_as_float_inverted);2482static const STBIR__SIMDF_CONST(STBIR_max_uint16_as_float_inverted, stbir__max_uint16_as_float_inverted);24832484static const STBIR__SIMDF_CONST(STBIR_simd_point5, 0.5f);2485static const STBIR__SIMDF_CONST(STBIR_ones, 1.0f);2486static const STBIR__SIMDI_CONST(STBIR_almost_zero, (127 - 13) << 23);2487static const STBIR__SIMDI_CONST(STBIR_almost_one, 0x3f7fffff);2488static const STBIR__SIMDI_CONST(STBIR_mastissa_mask, 0xff);2489static const STBIR__SIMDI_CONST(STBIR_topscale, 0x02000000);24902491// Basically, in simd mode, we unroll the proper amount, and we don't want2492// the non-simd remnant loops to be unroll because they only run a few times2493// Adding this switch saves about 5K on clang which is Captain Unroll the 3rd.2494#define STBIR_SIMD_STREAMOUT_PTR( star ) STBIR_STREAMOUT_PTR( star )2495#define STBIR_SIMD_NO_UNROLL(ptr) STBIR_NO_UNROLL(ptr)24962497#ifdef STBIR_MEMCPY2498#undef STBIR_MEMCPY2499#define STBIR_MEMCPY stbir_simd_memcpy2500#endif25012502// override normal use of memcpy with much simpler copy (faster and smaller with our sized copies)2503static void stbir_simd_memcpy( void * dest, void const * src, size_t bytes ) 2504{2505 char STBIR_SIMD_STREAMOUT_PTR (*) d = (char*) dest;2506 char STBIR_SIMD_STREAMOUT_PTR( * ) d_end = ((char*) dest) + bytes;2507 ptrdiff_t ofs_to_src = (char*)src - (char*)dest;25082509 // check overlaps2510 STBIR_ASSERT( ( ( d >= ( (char*)src) + bytes ) ) || ( ( d + bytes ) <= (char*)src ) );25112512 if ( bytes < (16*stbir__simdfX_float_count) )2513 {2514 if ( bytes < 16 )2515 {2516 if ( bytes ) 2517 {2518 do2519 {2520 STBIR_SIMD_NO_UNROLL(d);2521 d[ 0 ] = d[ ofs_to_src ];2522 ++d;2523 } while ( d < d_end );2524 }2525 }2526 else2527 {2528 stbir__simdf x;2529 // do one unaligned to get us aligned for the stream out below2530 stbir__simdf_load( x, ( d + ofs_to_src ) );2531 stbir__simdf_store( d, x );2532 d = (char*)( ( ( (ptrdiff_t)d ) + 16 ) & ~15 );25332534 for(;;)2535 {2536 STBIR_SIMD_NO_UNROLL(d);25372538 if ( d > ( d_end - 16 ) )2539 {2540 if ( d == d_end )2541 return;2542 d = d_end - 16;2543 }25442545 stbir__simdf_load( x, ( d + ofs_to_src ) );2546 stbir__simdf_store( d, x );2547 d += 16;2548 }2549 }2550 }2551 else2552 {2553 stbir__simdfX x0,x1,x2,x3;25542555 // do one unaligned to get us aligned for the stream out below2556 stbir__simdfX_load( x0, ( d + ofs_to_src ) + 0*stbir__simdfX_float_count );2557 stbir__simdfX_load( x1, ( d + ofs_to_src ) + 4*stbir__simdfX_float_count );2558 stbir__simdfX_load( x2, ( d + ofs_to_src ) + 8*stbir__simdfX_float_count );2559 stbir__simdfX_load( x3, ( d + ofs_to_src ) + 12*stbir__simdfX_float_count );2560 stbir__simdfX_store( d + 0*stbir__simdfX_float_count, x0 );2561 stbir__simdfX_store( d + 4*stbir__simdfX_float_count, x1 );2562 stbir__simdfX_store( d + 8*stbir__simdfX_float_count, x2 );2563 stbir__simdfX_store( d + 12*stbir__simdfX_float_count, x3 );2564 d = (char*)( ( ( (ptrdiff_t)d ) + (16*stbir__simdfX_float_count) ) & ~((16*stbir__simdfX_float_count)-1) );25652566 for(;;)2567 {2568 STBIR_SIMD_NO_UNROLL(d);2569 2570 if ( d > ( d_end - (16*stbir__simdfX_float_count) ) )2571 {2572 if ( d == d_end )2573 return;2574 d = d_end - (16*stbir__simdfX_float_count);2575 }25762577 stbir__simdfX_load( x0, ( d + ofs_to_src ) + 0*stbir__simdfX_float_count );2578 stbir__simdfX_load( x1, ( d + ofs_to_src ) + 4*stbir__simdfX_float_count );2579 stbir__simdfX_load( x2, ( d + ofs_to_src ) + 8*stbir__simdfX_float_count );2580 stbir__simdfX_load( x3, ( d + ofs_to_src ) + 12*stbir__simdfX_float_count );2581 stbir__simdfX_store( d + 0*stbir__simdfX_float_count, x0 );2582 stbir__simdfX_store( d + 4*stbir__simdfX_float_count, x1 );2583 stbir__simdfX_store( d + 8*stbir__simdfX_float_count, x2 );2584 stbir__simdfX_store( d + 12*stbir__simdfX_float_count, x3 );2585 d += (16*stbir__simdfX_float_count);2586 }2587 }2588}25892590// memcpy that is specically intentionally overlapping (src is smaller then dest, so can be2591// a normal forward copy, bytes is divisible by 4 and bytes is greater than or equal to2592// the diff between dest and src)2593static void stbir_overlapping_memcpy( void * dest, void const * src, size_t bytes ) 2594{2595 char STBIR_SIMD_STREAMOUT_PTR (*) sd = (char*) src;2596 char STBIR_SIMD_STREAMOUT_PTR( * ) s_end = ((char*) src) + bytes;2597 ptrdiff_t ofs_to_dest = (char*)dest - (char*)src;25982599 if ( ofs_to_dest >= 16 ) // is the overlap more than 16 away?2600 {2601 char STBIR_SIMD_STREAMOUT_PTR( * ) s_end16 = ((char*) src) + (bytes&~15);2602 do2603 {2604 stbir__simdf x;2605 STBIR_SIMD_NO_UNROLL(sd);2606 stbir__simdf_load( x, sd );2607 stbir__simdf_store( ( sd + ofs_to_dest ), x );2608 sd += 16;2609 } while ( sd < s_end16 );26102611 if ( sd == s_end )2612 return;2613 }26142615 do2616 {2617 STBIR_SIMD_NO_UNROLL(sd);2618 *(int*)( sd + ofs_to_dest ) = *(int*) sd; 2619 sd += 4;2620 } while ( sd < s_end );2621}26222623#else // no SSE226242625// when in scalar mode, we let unrolling happen, so this macro just does the __restrict2626#define STBIR_SIMD_STREAMOUT_PTR( star ) STBIR_STREAMOUT_PTR( star )2627#define STBIR_SIMD_NO_UNROLL(ptr) 26282629#endif // SSE2263026312632#ifdef STBIR_PROFILE26332634#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_ )26352636#ifdef _MSC_VER26372638 STBIRDEF stbir_uint64 __rdtsc();2639 #define STBIR_PROFILE_FUNC() __rdtsc()26402641#else // non msvc26422643 static stbir__inline stbir_uint64 STBIR_PROFILE_FUNC() 2644 {2645 stbir_uint32 lo, hi;2646 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi) );2647 return ( ( (stbir_uint64) hi ) << 32 ) | ( (stbir_uint64) lo );2648 }26492650#endif // msvc26512652#elif defined( _M_ARM64 ) || defined( __aarch64__ ) || defined( __arm64__ ) || defined(__ARM_NEON__) 26532654#if defined( _MSC_VER ) && !defined(__clang__)26552656 #define STBIR_PROFILE_FUNC() _ReadStatusReg(ARM64_CNTVCT)26572658#else26592660 static stbir__inline stbir_uint64 STBIR_PROFILE_FUNC()2661 {2662 stbir_uint64 tsc;2663 asm volatile("mrs %0, cntvct_el0" : "=r" (tsc));2664 return tsc;2665 }26662667#endif26682669#else // x64, arm26702671#error Unknown platform for profiling.26722673#endif //x64 and 267426752676#define STBIR_ONLY_PROFILE_GET_SPLIT_INFO ,stbir__per_split_info * split_info2677#define STBIR_ONLY_PROFILE_SET_SPLIT_INFO ,split_info26782679#define STBIR_ONLY_PROFILE_BUILD_GET_INFO ,stbir__info * profile_info2680#define STBIR_ONLY_PROFILE_BUILD_SET_INFO ,profile_info26812682// super light-weight micro profiler2683#define STBIR_PROFILE_START_ll( info, wh ) { stbir_uint64 wh##thiszonetime = STBIR_PROFILE_FUNC(); stbir_uint64 * wh##save_parent_excluded_ptr = info->current_zone_excluded_ptr; stbir_uint64 wh##current_zone_excluded = 0; info->current_zone_excluded_ptr = &wh##current_zone_excluded; 2684#define STBIR_PROFILE_END_ll( info, wh ) wh##thiszonetime = STBIR_PROFILE_FUNC() - wh##thiszonetime; info->profile.named.wh += wh##thiszonetime - wh##current_zone_excluded; *wh##save_parent_excluded_ptr += wh##thiszonetime; info->current_zone_excluded_ptr = wh##save_parent_excluded_ptr; }2685#define STBIR_PROFILE_FIRST_START_ll( info, wh ) { int i; info->current_zone_excluded_ptr = &info->profile.named.total; for(i=0;i<STBIR__ARRAY_SIZE(info->profile.array);i++) info->profile.array[i]=0; } STBIR_PROFILE_START_ll( info, wh );2686#define STBIR_PROFILE_CLEAR_EXTRAS_ll( info, num ) { int extra; for(extra=1;extra<(num);extra++) { int i; for(i=0;i<STBIR__ARRAY_SIZE((info)->profile.array);i++) (info)[extra].profile.array[i]=0; } }26872688// for thread data2689#define STBIR_PROFILE_START( wh ) STBIR_PROFILE_START_ll( split_info, wh )2690#define STBIR_PROFILE_END( wh ) STBIR_PROFILE_END_ll( split_info, wh )2691#define STBIR_PROFILE_FIRST_START( wh ) STBIR_PROFILE_FIRST_START_ll( split_info, wh )2692#define STBIR_PROFILE_CLEAR_EXTRAS() STBIR_PROFILE_CLEAR_EXTRAS_ll( split_info, split_count )26932694// for build data2695#define STBIR_PROFILE_BUILD_START( wh ) STBIR_PROFILE_START_ll( profile_info, wh )2696#define STBIR_PROFILE_BUILD_END( wh ) STBIR_PROFILE_END_ll( profile_info, wh )2697#define STBIR_PROFILE_BUILD_FIRST_START( wh ) STBIR_PROFILE_FIRST_START_ll( profile_info, wh )2698#define STBIR_PROFILE_BUILD_CLEAR( info ) { int i; for(i=0;i<STBIR__ARRAY_SIZE(info->profile.array);i++) info->profile.array[i]=0; }26992700#else // no profile27012702#define STBIR_ONLY_PROFILE_GET_SPLIT_INFO2703#define STBIR_ONLY_PROFILE_SET_SPLIT_INFO27042705#define STBIR_ONLY_PROFILE_BUILD_GET_INFO2706#define STBIR_ONLY_PROFILE_BUILD_SET_INFO27072708#define STBIR_PROFILE_START( wh )2709#define STBIR_PROFILE_END( wh )2710#define STBIR_PROFILE_FIRST_START( wh )2711#define STBIR_PROFILE_CLEAR_EXTRAS( )27122713#define STBIR_PROFILE_BUILD_START( wh ) 2714#define STBIR_PROFILE_BUILD_END( wh ) 2715#define STBIR_PROFILE_BUILD_FIRST_START( wh )2716#define STBIR_PROFILE_BUILD_CLEAR( info )27172718#endif // stbir_profile27192720#ifndef STBIR_CEILF2721#include <math.h>2722#if _MSC_VER <= 1200 // support VC6 for Sean2723#define STBIR_CEILF(x) ((float)ceil((float)(x)))2724#define STBIR_FLOORF(x) ((float)floor((float)(x)))2725#else2726#define STBIR_CEILF(x) ceilf(x)2727#define STBIR_FLOORF(x) floorf(x)2728#endif2729#endif27302731#ifndef STBIR_MEMCPY2732// For memcpy2733#include <string.h>2734#define STBIR_MEMCPY( dest, src, len ) memcpy( dest, src, len )2735#endif27362737#ifndef STBIR_SIMD27382739// memcpy that is specically intentionally overlapping (src is smaller then dest, so can be2740// a normal forward copy, bytes is divisible by 4 and bytes is greater than or equal to2741// the diff between dest and src)2742static void stbir_overlapping_memcpy( void * dest, void const * src, size_t bytes ) 2743{2744 char STBIR_SIMD_STREAMOUT_PTR (*) sd = (char*) src;2745 char STBIR_SIMD_STREAMOUT_PTR( * ) s_end = ((char*) src) + bytes;2746 ptrdiff_t ofs_to_dest = (char*)dest - (char*)src;27472748 if ( ofs_to_dest >= 8 ) // is the overlap more than 8 away?2749 {2750 char STBIR_SIMD_STREAMOUT_PTR( * ) s_end8 = ((char*) src) + (bytes&~7);2751 do2752 {2753 STBIR_NO_UNROLL(sd);2754 *(stbir_uint64*)( sd + ofs_to_dest ) = *(stbir_uint64*) sd; 2755 sd += 8;2756 } while ( sd < s_end8 );27572758 if ( sd == s_end )2759 return;2760 }27612762 do2763 {2764 STBIR_NO_UNROLL(sd);2765 *(int*)( sd + ofs_to_dest ) = *(int*) sd; 2766 sd += 4;2767 } while ( sd < s_end );2768}27692770#endif27712772static float stbir__filter_trapezoid(float x, float scale, void * user_data)2773{2774 float halfscale = scale / 2;2775 float t = 0.5f + halfscale;2776 STBIR_ASSERT(scale <= 1);2777 STBIR__UNUSED(user_data);27782779 if ( x < 0.0f ) x = -x;27802781 if (x >= t)2782 return 0.0f;2783 else2784 {2785 float r = 0.5f - halfscale;2786 if (x <= r)2787 return 1.0f;2788 else2789 return (t - x) / scale;2790 }2791}27922793static float stbir__support_trapezoid(float scale, void * user_data)2794{2795 STBIR__UNUSED(user_data);2796 return 0.5f + scale / 2.0f;2797}27982799static float stbir__filter_triangle(float x, float s, void * user_data)2800{2801 STBIR__UNUSED(s);2802 STBIR__UNUSED(user_data);28032804 if ( x < 0.0f ) x = -x;28052806 if (x <= 1.0f)2807 return 1.0f - x;2808 else2809 return 0.0f;2810}28112812static float stbir__filter_point(float x, float s, void * user_data)2813{2814 STBIR__UNUSED(x);2815 STBIR__UNUSED(s);2816 STBIR__UNUSED(user_data);28172818 return 1.0f;2819}28202821static float stbir__filter_cubic(float x, float s, void * user_data)2822{2823 STBIR__UNUSED(s);2824 STBIR__UNUSED(user_data);28252826 if ( x < 0.0f ) x = -x;28272828 if (x < 1.0f)2829 return (4.0f + x*x*(3.0f*x - 6.0f))/6.0f;2830 else if (x < 2.0f)2831 return (8.0f + x*(-12.0f + x*(6.0f - x)))/6.0f;28322833 return (0.0f);2834}28352836static float stbir__filter_catmullrom(float x, float s, void * user_data)2837{2838 STBIR__UNUSED(s);2839 STBIR__UNUSED(user_data);28402841 if ( x < 0.0f ) x = -x;28422843 if (x < 1.0f)2844 return 1.0f - x*x*(2.5f - 1.5f*x);2845 else if (x < 2.0f)2846 return 2.0f - x*(4.0f + x*(0.5f*x - 2.5f));28472848 return (0.0f);2849}28502851static float stbir__filter_mitchell(float x, float s, void * user_data)2852{2853 STBIR__UNUSED(s);2854 STBIR__UNUSED(user_data);28552856 if ( x < 0.0f ) x = -x;28572858 if (x < 1.0f)2859 return (16.0f + x*x*(21.0f * x - 36.0f))/18.0f;2860 else if (x < 2.0f)2861 return (32.0f + x*(-60.0f + x*(36.0f - 7.0f*x)))/18.0f;28622863 return (0.0f);2864}28652866static float stbir__support_zero(float s, void * user_data)2867{2868 STBIR__UNUSED(s);2869 STBIR__UNUSED(user_data);2870 return 0;2871}28722873static float stbir__support_zeropoint5(float s, void * user_data)2874{2875 STBIR__UNUSED(s);2876 STBIR__UNUSED(user_data);2877 return 0.5f;2878}28792880static float stbir__support_one(float s, void * user_data)2881{2882 STBIR__UNUSED(s);2883 STBIR__UNUSED(user_data);2884 return 1;2885}28862887static float stbir__support_two(float s, void * user_data) 2888{2889 STBIR__UNUSED(s);2890 STBIR__UNUSED(user_data);2891 return 2;2892}28932894// This is the maximum number of input samples that can affect an output sample2895// with the given filter from the output pixel's perspective2896static int stbir__get_filter_pixel_width(stbir__support_callback * support, float scale, void * user_data)2897{2898 STBIR_ASSERT(support != 0);28992900 if ( scale >= ( 1.0f-stbir__small_float ) ) // upscale2901 return (int)STBIR_CEILF(support(1.0f/scale,user_data) * 2.0f);2902 else2903 return (int)STBIR_CEILF(support(scale,user_data) * 2.0f / scale);2904}29052906// this is how many coefficents per run of the filter (which is different 2907// from the filter_pixel_width depending on if we are scattering or gathering)2908static int stbir__get_coefficient_width(stbir__sampler * samp, int is_gather, void * user_data)2909{2910 float scale = samp->scale_info.scale;2911 stbir__support_callback * support = samp->filter_support;29122913 switch( is_gather )2914 {2915 case 1:2916 return (int)STBIR_CEILF(support(1.0f / scale, user_data) * 2.0f);2917 case 2:2918 return (int)STBIR_CEILF(support(scale, user_data) * 2.0f / scale);2919 case 0:2920 return (int)STBIR_CEILF(support(scale, user_data) * 2.0f);2921 default:2922 STBIR_ASSERT( (is_gather >= 0 ) && (is_gather <= 2 ) );2923 return 0;2924 }2925}29262927static int stbir__get_contributors(stbir__sampler * samp, int is_gather) 2928{2929 if (is_gather)2930 return samp->scale_info.output_sub_size;2931 else2932 return (samp->scale_info.input_full_size + samp->filter_pixel_margin * 2);2933}29342935static int stbir__edge_zero_full( int n, int max )2936{2937 STBIR__UNUSED(n);2938 STBIR__UNUSED(max);2939 return 0; // NOTREACHED2940}29412942static int stbir__edge_clamp_full( int n, int max )2943{2944 if (n < 0)2945 return 0;29462947 if (n >= max)2948 return max - 1;29492950 return n; // NOTREACHED2951}29522953static int stbir__edge_reflect_full( int n, int max )2954{2955 if (n < 0)2956 {2957 if (n > -max) 2958 return -n;2959 else2960 return max - 1;2961 }29622963 if (n >= max)2964 {2965 int max2 = max * 2;2966 if (n >= max2)2967 return 0;2968 else2969 return max2 - n - 1;2970 }29712972 return n; // NOTREACHED2973}29742975static int stbir__edge_wrap_full( int n, int max )2976{2977 if (n >= 0)2978 return (n % max);2979 else2980 {2981 int m = (-n) % max;29822983 if (m != 0)2984 m = max - m;29852986 return (m);2987 }2988}29892990typedef int stbir__edge_wrap_func( int n, int max );2991static stbir__edge_wrap_func * stbir__edge_wrap_slow[] =2992{2993 stbir__edge_clamp_full, // STBIR_EDGE_CLAMP2994 stbir__edge_reflect_full, // STBIR_EDGE_REFLECT2995 stbir__edge_wrap_full, // STBIR_EDGE_WRAP2996 stbir__edge_zero_full, // STBIR_EDGE_ZERO2997};29982999stbir__inline static int stbir__edge_wrap(stbir_edge edge, int n, int max)3000{3001 // avoid per-pixel switch3002 if (n >= 0 && n < max)3003 return n;3004 return stbir__edge_wrap_slow[edge]( n, max );3005}30063007#define STBIR__MERGE_RUNS_PIXEL_THRESHOLD 1630083009// get information on the extents of a sampler3010static void stbir__get_extents( stbir__sampler * samp, stbir__extents * scanline_extents )3011{3012 int j, stop;3013 int left_margin, right_margin;3014 int min_n = 0x7fffffff, max_n = -0x7fffffff;3015 int min_left = 0x7fffffff, max_left = -0x7fffffff;3016 int min_right = 0x7fffffff, max_right = -0x7fffffff;3017 stbir_edge edge = samp->edge;3018 stbir__contributors* contributors = samp->contributors;3019 int output_sub_size = samp->scale_info.output_sub_size;3020 int input_full_size = samp->scale_info.input_full_size;3021 int filter_pixel_margin = samp->filter_pixel_margin;30223023 STBIR_ASSERT( samp->is_gather );30243025 stop = output_sub_size;3026 for (j = 0; j < stop; j++ )3027 {3028 STBIR_ASSERT( contributors[j].n1 >= contributors[j].n0 );3029 if ( contributors[j].n0 < min_n )3030 {3031 min_n = contributors[j].n0;3032 stop = j + filter_pixel_margin; // if we find a new min, only scan another filter width3033 if ( stop > output_sub_size ) stop = output_sub_size;3034 }3035 }30363037 stop = 0;3038 for (j = output_sub_size - 1; j >= stop; j-- )3039 {3040 STBIR_ASSERT( contributors[j].n1 >= contributors[j].n0 );3041 if ( contributors[j].n1 > max_n )3042 {3043 max_n = contributors[j].n1;3044 stop = j - filter_pixel_margin; // if we find a new max, only scan another filter width3045 if (stop<0) stop = 0;3046 }3047 }30483049 STBIR_ASSERT( scanline_extents->conservative.n0 <= min_n );3050 STBIR_ASSERT( scanline_extents->conservative.n1 >= max_n );30513052 // now calculate how much into the margins we really read3053 left_margin = 0;3054 if ( min_n < 0 )3055 {3056 left_margin = -min_n;3057 min_n = 0;3058 }3059 3060 right_margin = 0;3061 if ( max_n >= input_full_size )3062 {3063 right_margin = max_n - input_full_size + 1;3064 max_n = input_full_size - 1;3065 }30663067 // index 1 is margin pixel extents (how many pixels we hang over the edge)3068 scanline_extents->edge_sizes[0] = left_margin;3069 scanline_extents->edge_sizes[1] = right_margin;30703071 // index 2 is pixels read from the input3072 scanline_extents->spans[0].n0 = min_n;3073 scanline_extents->spans[0].n1 = max_n;3074 scanline_extents->spans[0].pixel_offset_for_input = min_n;30753076 // default to no other input range3077 scanline_extents->spans[1].n0 = 0;3078 scanline_extents->spans[1].n1 = -1;3079 scanline_extents->spans[1].pixel_offset_for_input = 0;30803081 // don't have to do edge calc for zero clamp3082 if ( edge == STBIR_EDGE_ZERO )3083 return;3084 3085 // convert margin pixels to the pixels within the input (min and max)3086 for( j = -left_margin ; j < 0 ; j++ )3087 {3088 int p = stbir__edge_wrap( edge, j, input_full_size );3089 if ( p < min_left )3090 min_left = p;3091 if ( p > max_left )3092 max_left = p;3093 }30943095 for( j = input_full_size ; j < (input_full_size + right_margin) ; j++ )3096 {3097 int p = stbir__edge_wrap( edge, j, input_full_size );3098 if ( p < min_right )3099 min_right = p;3100 if ( p > max_right )3101 max_right = p;3102 }31033104 // merge the left margin pixel region if it connects within 4 pixels of main pixel region3105 if ( min_left != 0x7fffffff )3106 {3107 if ( ( ( min_left <= min_n ) && ( ( max_left + STBIR__MERGE_RUNS_PIXEL_THRESHOLD ) >= min_n ) ) ||3108 ( ( min_n <= min_left ) && ( ( max_n + STBIR__MERGE_RUNS_PIXEL_THRESHOLD ) >= max_left ) ) )3109 {3110 scanline_extents->spans[0].n0 = min_n = stbir__min( min_n, min_left );3111 scanline_extents->spans[0].n1 = max_n = stbir__max( max_n, max_left );3112 scanline_extents->spans[0].pixel_offset_for_input = min_n;3113 left_margin = 0;3114 }3115 }31163117 // merge the right margin pixel region if it connects within 4 pixels of main pixel region3118 if ( min_right != 0x7fffffff )3119 {3120 if ( ( ( min_right <= min_n ) && ( ( max_right + STBIR__MERGE_RUNS_PIXEL_THRESHOLD ) >= min_n ) ) ||3121 ( ( min_n <= min_right ) && ( ( max_n + STBIR__MERGE_RUNS_PIXEL_THRESHOLD ) >= max_right ) ) )3122 {3123 scanline_extents->spans[0].n0 = min_n = stbir__min( min_n, min_right );3124 scanline_extents->spans[0].n1 = max_n = stbir__max( max_n, max_right );3125 scanline_extents->spans[0].pixel_offset_for_input = min_n;3126 right_margin = 0;3127 }3128 }31293130 STBIR_ASSERT( scanline_extents->conservative.n0 <= min_n );3131 STBIR_ASSERT( scanline_extents->conservative.n1 >= max_n );31323133 // you get two ranges when you have the WRAP edge mode and you are doing just the a piece of the resize3134 // so you need to get a second run of pixels from the opposite side of the scanline (which you3135 // wouldn't need except for WRAP)313631373138 // if we can't merge the min_left range, add it as a second range3139 if ( ( left_margin ) && ( min_left != 0x7fffffff ) )3140 {3141 stbir__span * newspan = scanline_extents->spans + 1;3142 STBIR_ASSERT( right_margin == 0 );3143 if ( min_left < scanline_extents->spans[0].n0 )3144 {3145 scanline_extents->spans[1].pixel_offset_for_input = scanline_extents->spans[0].n0;3146 scanline_extents->spans[1].n0 = scanline_extents->spans[0].n0;3147 scanline_extents->spans[1].n1 = scanline_extents->spans[0].n1;3148 --newspan;3149 }3150 newspan->pixel_offset_for_input = min_left;3151 newspan->n0 = -left_margin;3152 newspan->n1 = ( max_left - min_left ) - left_margin;3153 scanline_extents->edge_sizes[0] = 0; // don't need to copy the left margin, since we are directly decoding into the margin3154 return;3155 }31563157 // if we can't merge the min_left range, add it as a second range3158 if ( ( right_margin ) && ( min_right != 0x7fffffff ) )3159 {3160 stbir__span * newspan = scanline_extents->spans + 1;3161 if ( min_right < scanline_extents->spans[0].n0 )3162 {3163 scanline_extents->spans[1].pixel_offset_for_input = scanline_extents->spans[0].n0;3164 scanline_extents->spans[1].n0 = scanline_extents->spans[0].n0;3165 scanline_extents->spans[1].n1 = scanline_extents->spans[0].n1;3166 --newspan;3167 }3168 newspan->pixel_offset_for_input = min_right;3169 newspan->n0 = scanline_extents->spans[1].n1 + 1;3170 newspan->n1 = scanline_extents->spans[1].n1 + 1 + ( max_right - min_right );3171 scanline_extents->edge_sizes[1] = 0; // don't need to copy the right margin, since we are directly decoding into the margin3172 return;3173 }3174}31753176static void stbir__calculate_in_pixel_range( int * first_pixel, int * last_pixel, float out_pixel_center, float out_filter_radius, float inv_scale, float out_shift, int input_size, stbir_edge edge )3177{3178 int first, last;3179 float out_pixel_influence_lowerbound = out_pixel_center - out_filter_radius;3180 float out_pixel_influence_upperbound = out_pixel_center + out_filter_radius;31813182 float in_pixel_influence_lowerbound = (out_pixel_influence_lowerbound + out_shift) * inv_scale; 3183 float in_pixel_influence_upperbound = (out_pixel_influence_upperbound + out_shift) * inv_scale; 31843185 first = (int)(STBIR_FLOORF(in_pixel_influence_lowerbound + 0.5f));3186 last = (int)(STBIR_FLOORF(in_pixel_influence_upperbound - 0.5f));31873188 if ( edge == STBIR_EDGE_WRAP )3189 {3190 if ( first <= -input_size )3191 first = -(input_size-1);3192 if ( last >= (input_size*2))3193 last = (input_size*2) - 1;3194 }3195 3196 *first_pixel = first;3197 *last_pixel = last;3198}31993200static void stbir__calculate_coefficients_for_gather_upsample( float out_filter_radius, stbir__kernel_callback * kernel, stbir__scale_info * scale_info, int num_contributors, stbir__contributors* contributors, float* coefficient_group, int coefficient_width, stbir_edge edge, void * user_data )3201{3202 int n, end;3203 float inv_scale = scale_info->inv_scale;3204 float out_shift = scale_info->pixel_shift;3205 int input_size = scale_info->input_full_size;3206 int numerator = scale_info->scale_numerator;3207 int polyphase = ( ( scale_info->scale_is_rational ) && ( numerator < num_contributors ) );32083209 // Looping through out pixels3210 end = num_contributors; if ( polyphase ) end = numerator;3211 for (n = 0; n < end; n++)3212 {3213 int i;3214 int last_non_zero;3215 float out_pixel_center = (float)n + 0.5f;3216 float in_center_of_out = (out_pixel_center + out_shift) * inv_scale; 32173218 int in_first_pixel, in_last_pixel;3219 3220 stbir__calculate_in_pixel_range( &in_first_pixel, &in_last_pixel, out_pixel_center, out_filter_radius, inv_scale, out_shift, input_size, edge );32213222 last_non_zero = -1;3223 for (i = 0; i <= in_last_pixel - in_first_pixel; i++)3224 {3225 float in_pixel_center = (float)(i + in_first_pixel) + 0.5f;3226 float coeff = kernel(in_center_of_out - in_pixel_center, inv_scale, user_data);32273228 // kill denormals3229 if ( ( ( coeff < stbir__small_float ) && ( coeff > -stbir__small_float ) ) )3230 {3231 if ( i == 0 ) // if we're at the front, just eat zero contributors3232 { 3233 STBIR_ASSERT ( ( in_last_pixel - in_first_pixel ) != 0 ); // there should be at least one contrib3234 ++in_first_pixel;3235 i--;3236 continue;3237 }3238 coeff = 0; // make sure is fully zero (should keep denormals away)3239 }3240 else3241 last_non_zero = i;3242 3243 coefficient_group[i] = coeff;3244 }3245 3246 in_last_pixel = last_non_zero+in_first_pixel; // kills trailing zeros3247 contributors->n0 = in_first_pixel;3248 contributors->n1 = in_last_pixel;32493250 STBIR_ASSERT(contributors->n1 >= contributors->n0);32513252 ++contributors;3253 coefficient_group += coefficient_width;3254 }3255}32563257static void stbir__insert_coeff( stbir__contributors * contribs, float * coeffs, int new_pixel, float new_coeff )3258{3259 if ( new_pixel <= contribs->n1 ) // before the end3260 {3261 if ( new_pixel < contribs->n0 ) // before the front?3262 {3263 int j, o = contribs->n0 - new_pixel;3264 for ( j = contribs->n1 - contribs->n0 ; j <= 0 ; j-- )3265 coeffs[ j + o ] = coeffs[ j ];3266 for ( j = 1 ; j < o ; j-- )3267 coeffs[ j ] = coeffs[ 0 ];3268 coeffs[ 0 ] = new_coeff;3269 contribs->n0 = new_pixel;3270 }3271 else3272 {3273 coeffs[ new_pixel - contribs->n0 ] += new_coeff;3274 }3275 }3276 else3277 {3278 int j, e = new_pixel - contribs->n0;3279 for( j = ( contribs->n1 - contribs->n0 ) + 1 ; j < e ; j++ ) // clear in-betweens coeffs if there are any3280 coeffs[j] = 0;32813282 coeffs[ e ] = new_coeff;3283 contribs->n1 = new_pixel;3284 }3285}32863287static void stbir__calculate_out_pixel_range( int * first_pixel, int * last_pixel, float in_pixel_center, float in_pixels_radius, float scale, float out_shift, int out_size )3288{3289 float in_pixel_influence_lowerbound = in_pixel_center - in_pixels_radius;3290 float in_pixel_influence_upperbound = in_pixel_center + in_pixels_radius;3291 float out_pixel_influence_lowerbound = in_pixel_influence_lowerbound * scale - out_shift;3292 float out_pixel_influence_upperbound = in_pixel_influence_upperbound * scale - out_shift;3293 int out_first_pixel = (int)(STBIR_FLOORF(out_pixel_influence_lowerbound + 0.5f));3294 int out_last_pixel = (int)(STBIR_FLOORF(out_pixel_influence_upperbound - 0.5f));32953296 if ( out_first_pixel < 0 )3297 out_first_pixel = 0;3298 if ( out_last_pixel >= out_size )3299 out_last_pixel = out_size - 1;3300 *first_pixel = out_first_pixel;3301 *last_pixel = out_last_pixel;3302}33033304static void stbir__calculate_coefficients_for_gather_downsample( int start, int end, float in_pixels_radius, stbir__kernel_callback * kernel, stbir__scale_info * scale_info, int coefficient_width, int num_contributors, stbir__contributors * contributors, float * coefficient_group, void * user_data )3305{3306 int in_pixel;3307 int i;3308 int first_out_inited = -1;3309 float scale = scale_info->scale;3310 float out_shift = scale_info->pixel_shift;3311 int out_size = scale_info->output_sub_size;3312 int numerator = scale_info->scale_numerator;3313 int polyphase = ( ( scale_info->scale_is_rational ) && ( numerator < out_size ) );33143315 STBIR__UNUSED(num_contributors);33163317 // Loop through the input pixels3318 for (in_pixel = start; in_pixel < end; in_pixel++)3319 {3320 float in_pixel_center = (float)in_pixel + 0.5f;3321 float out_center_of_in = in_pixel_center * scale - out_shift;3322 int out_first_pixel, out_last_pixel;33233324 stbir__calculate_out_pixel_range( &out_first_pixel, &out_last_pixel, in_pixel_center, in_pixels_radius, scale, out_shift, out_size );33253326 if ( out_first_pixel > out_last_pixel )3327 continue;33283329 // clamp or exit if we are using polyphase filtering, and the limit is up3330 if ( polyphase )3331 {3332 // when polyphase, you only have to do coeffs up to the numerator count3333 if ( out_first_pixel == numerator )3334 break;33353336 // don't do any extra work, clamp last pixel at numerator too3337 if ( out_last_pixel >= numerator )3338 out_last_pixel = numerator - 1;3339 }33403341 for (i = 0; i <= out_last_pixel - out_first_pixel; i++)3342 {3343 float out_pixel_center = (float)(i + out_first_pixel) + 0.5f;3344 float x = out_pixel_center - out_center_of_in;3345 float coeff = kernel(x, scale, user_data) * scale;33463347 // kill the coeff if it's too small (avoid denormals)3348 if ( ( ( coeff < stbir__small_float ) && ( coeff > -stbir__small_float ) ) )3349 coeff = 0.0f;33503351 {3352 int out = i + out_first_pixel;3353 float * coeffs = coefficient_group + out * coefficient_width;3354 stbir__contributors * contribs = contributors + out;33553356 // is this the first time this output pixel has been seen? Init it.3357 if ( out > first_out_inited ) 3358 {3359 STBIR_ASSERT( out == ( first_out_inited + 1 ) ); // ensure we have only advanced one at time3360 first_out_inited = out;3361 contribs->n0 = in_pixel;3362 contribs->n1 = in_pixel;3363 coeffs[0] = coeff;3364 }3365 else 3366 {3367 // insert on end (always in order)3368 if ( coeffs[0] == 0.0f ) // if the first coefficent is zero, then zap it for this coeffs3369 {3370 STBIR_ASSERT( ( in_pixel - contribs->n0 ) == 1 ); // ensure that when we zap, we're at the 2nd pos3371 contribs->n0 = in_pixel;3372 }3373 contribs->n1 = in_pixel;3374 STBIR_ASSERT( ( in_pixel - contribs->n0 ) < coefficient_width );3375 coeffs[in_pixel - contribs->n0] = coeff;3376 }3377 }3378 }3379 }3380}33813382static void stbir__cleanup_gathered_coefficients( stbir_edge edge, stbir__filter_extent_info* filter_info, stbir__scale_info * scale_info, int num_contributors, stbir__contributors* contributors, float * coefficient_group, int coefficient_width )3383{3384 int input_size = scale_info->input_full_size;3385 int input_last_n1 = input_size - 1; 3386 int n, end;3387 int lowest = 0x7fffffff;3388 int highest = -0x7fffffff;3389 int widest = -1;3390 int numerator = scale_info->scale_numerator;3391 int denominator = scale_info->scale_denominator;3392 int polyphase = ( ( scale_info->scale_is_rational ) && ( numerator < num_contributors ) );3393 float * coeffs;3394 stbir__contributors * contribs;33953396 // weight all the coeffs for each sample3397 coeffs = coefficient_group;3398 contribs = contributors;3399 end = num_contributors; if ( polyphase ) end = numerator;3400 for (n = 0; n < end; n++)3401 {3402 int i;3403 float filter_scale, total_filter = 0;3404 int e;34053406 // add all contribs3407 e = contribs->n1 - contribs->n0;3408 for( i = 0 ; i <= e ; i++ )3409 {3410 total_filter += coeffs[i];3411 STBIR_ASSERT( ( coeffs[i] >= -2.0f ) && ( coeffs[i] <= 2.0f ) ); // check for wonky weights3412 }34133414 // rescale3415 if ( ( total_filter < stbir__small_float ) && ( total_filter > -stbir__small_float ) )3416 {3417 // all coeffs are extremely small, just zero it3418 contribs->n1 = contribs->n0;3419 coeffs[0] = 0.0f;3420 }3421 else3422 {3423 // if the total isn't 1.0, rescale everything3424 if ( ( total_filter < (1.0f-stbir__small_float) ) || ( total_filter > (1.0f+stbir__small_float) ) )3425 {3426 filter_scale = 1.0f / total_filter;3427 // scale them all3428 for (i = 0; i <= e; i++)3429 coeffs[i] *= filter_scale;3430 }3431 }3432 ++contribs;3433 coeffs += coefficient_width;3434 }34353436 // if we have a rational for the scale, we can exploit the polyphaseness to not calculate3437 // most of the coefficients, so we copy them here3438 if ( polyphase )3439 {3440 stbir__contributors * prev_contribs = contributors;3441 stbir__contributors * cur_contribs = contributors + numerator;34423443 for( n = numerator ; n < num_contributors ; n++ )3444 {3445 cur_contribs->n0 = prev_contribs->n0 + denominator;3446 cur_contribs->n1 = prev_contribs->n1 + denominator;3447 ++cur_contribs;3448 ++prev_contribs;3449 }3450 stbir_overlapping_memcpy( coefficient_group + numerator * coefficient_width, coefficient_group, ( num_contributors - numerator ) * coefficient_width * sizeof( coeffs[ 0 ] ) );3451 }34523453 coeffs = coefficient_group;3454 contribs = contributors;3455 for (n = 0; n < num_contributors; n++)3456 {3457 int i;34583459 // in zero edge mode, just remove out of bounds contribs completely (since their weights are accounted for now)3460 if ( edge == STBIR_EDGE_ZERO )3461 {3462 // shrink the right side if necessary3463 if ( contribs->n1 > input_last_n1 )3464 contribs->n1 = input_last_n1;34653466 // shrink the left side3467 if ( contribs->n0 < 0 )3468 {3469 int j, left, skips = 0;34703471 skips = -contribs->n0;3472 contribs->n0 = 0;34733474 // now move down the weights3475 left = contribs->n1 - contribs->n0 + 1;3476 if ( left > 0 )3477 {3478 for( j = 0 ; j < left ; j++ )3479 coeffs[ j ] = coeffs[ j + skips ];3480 }3481 }3482 }3483 else if ( ( edge == STBIR_EDGE_CLAMP ) || ( edge == STBIR_EDGE_REFLECT ) )3484 {3485 // for clamp and reflect, calculate the true inbounds position (based on edge type) and just add that to the existing weight3486 3487 // right hand side first3488 if ( contribs->n1 > input_last_n1 )3489 {3490 int start = contribs->n0;3491 int endi = contribs->n1;3492 contribs->n1 = input_last_n1; 3493 for( i = input_size; i <= endi; i++ )3494 stbir__insert_coeff( contribs, coeffs, stbir__edge_wrap_slow[edge]( i, input_size ), coeffs[i-start] );3495 }34963497 // now check left hand edge3498 if ( contribs->n0 < 0 )3499 {3500 int save_n0;3501 float save_n0_coeff;3502 float * c = coeffs - ( contribs->n0 + 1 );3503 3504 // reinsert the coeffs with it reflected or clamped (insert accumulates, if the coeffs exist)3505 for( i = -1 ; i > contribs->n0 ; i-- ) 3506 stbir__insert_coeff( contribs, coeffs, stbir__edge_wrap_slow[edge]( i, input_size ), *c-- );3507 save_n0 = contribs->n0;3508 save_n0_coeff = c[0]; // save it, since we didn't do the final one (i==n0), because there might be too many coeffs to hold (before we resize)!35093510 // now slide all the coeffs down (since we have accumulated them in the positive contribs) and reset the first contrib3511 contribs->n0 = 0; 3512 for(i = 0 ; i <= contribs->n1 ; i++ )3513 coeffs[i] = coeffs[i-save_n0];3514 3515 // now that we have shrunk down the contribs, we insert the first one safely3516 stbir__insert_coeff( contribs, coeffs, stbir__edge_wrap_slow[edge]( save_n0, input_size ), save_n0_coeff );3517 }3518 }35193520 if ( contribs->n0 <= contribs->n1 )3521 {3522 int diff = contribs->n1 - contribs->n0 + 1;3523 while ( diff && ( coeffs[ diff-1 ] == 0.0f ) )3524 --diff;3525 contribs->n1 = contribs->n0 + diff - 1;35263527 if ( contribs->n0 <= contribs->n1 )3528 {3529 if ( contribs->n0 < lowest )3530 lowest = contribs->n0;3531 if ( contribs->n1 > highest )3532 highest = contribs->n1;3533 if ( diff > widest )3534 widest = diff;3535 }35363537 // re-zero out unused coefficients (if any)3538 for( i = diff ; i < coefficient_width ; i++ )3539 coeffs[i] = 0.0f;3540 }35413542 ++contribs;3543 coeffs += coefficient_width;3544 }3545 filter_info->lowest = lowest;3546 filter_info->highest = highest;3547 filter_info->widest = widest;3548}35493550static int stbir__pack_coefficients( int num_contributors, stbir__contributors* contributors, float * coefficents, int coefficient_width, int widest, int row_width )3551{3552 #define STBIR_MOVE_1( dest, src ) { STBIR_NO_UNROLL(dest); ((stbir_uint32*)(dest))[0] = ((stbir_uint32*)(src))[0]; }3553 #define STBIR_MOVE_2( dest, src ) { STBIR_NO_UNROLL(dest); ((stbir_uint64*)(dest))[0] = ((stbir_uint64*)(src))[0]; }3554 #ifdef STBIR_SIMD3555 #define STBIR_MOVE_4( dest, src ) { stbir__simdf t; STBIR_NO_UNROLL(dest); stbir__simdf_load( t, src ); stbir__simdf_store( dest, t ); }3556 #else3557 #define STBIR_MOVE_4( dest, src ) { STBIR_NO_UNROLL(dest); ((stbir_uint64*)(dest))[0] = ((stbir_uint64*)(src))[0]; ((stbir_uint64*)(dest))[1] = ((stbir_uint64*)(src))[1]; }3558 #endif3559 if ( coefficient_width != widest )3560 {3561 float * pc = coefficents;3562 float * coeffs = coefficents;3563 float * pc_end = coefficents + num_contributors * widest;3564 switch( widest )3565 {3566 case 1:3567 do {3568 STBIR_MOVE_1( pc, coeffs );3569 ++pc;3570 coeffs += coefficient_width;3571 } while ( pc < pc_end );3572 break;3573 case 2:3574 do {3575 STBIR_MOVE_2( pc, coeffs );3576 pc += 2;3577 coeffs += coefficient_width;3578 } while ( pc < pc_end );3579 break;3580 case 3:3581 do {3582 STBIR_MOVE_2( pc, coeffs );3583 STBIR_MOVE_1( pc+2, coeffs+2 );3584 pc += 3;3585 coeffs += coefficient_width;3586 } while ( pc < pc_end );3587 break;3588 case 4:3589 do {3590 STBIR_MOVE_4( pc, coeffs );3591 pc += 4;3592 coeffs += coefficient_width;3593 } while ( pc < pc_end );3594 break;3595 case 5:3596 do {3597 STBIR_MOVE_4( pc, coeffs );3598 STBIR_MOVE_1( pc+4, coeffs+4 );3599 pc += 5;3600 coeffs += coefficient_width;3601 } while ( pc < pc_end );3602 break;3603 case 6:3604 do {3605 STBIR_MOVE_4( pc, coeffs );3606 STBIR_MOVE_2( pc+4, coeffs+4 );3607 pc += 6;3608 coeffs += coefficient_width;3609 } while ( pc < pc_end );3610 break;3611 case 7:3612 do {3613 STBIR_MOVE_4( pc, coeffs );3614 STBIR_MOVE_2( pc+4, coeffs+4 );3615 STBIR_MOVE_1( pc+6, coeffs+6 );3616 pc += 7;3617 coeffs += coefficient_width;3618 } while ( pc < pc_end );3619 break;3620 case 8:3621 do {3622 STBIR_MOVE_4( pc, coeffs );3623 STBIR_MOVE_4( pc+4, coeffs+4 );3624 pc += 8;3625 coeffs += coefficient_width;3626 } while ( pc < pc_end );3627 break;3628 case 9:3629 do {3630 STBIR_MOVE_4( pc, coeffs );3631 STBIR_MOVE_4( pc+4, coeffs+4 );3632 STBIR_MOVE_1( pc+8, coeffs+8 );3633 pc += 9;3634 coeffs += coefficient_width;3635 } while ( pc < pc_end );3636 break;3637 case 10:3638 do {3639 STBIR_MOVE_4( pc, coeffs );3640 STBIR_MOVE_4( pc+4, coeffs+4 );3641 STBIR_MOVE_2( pc+8, coeffs+8 );3642 pc += 10;3643 coeffs += coefficient_width;3644 } while ( pc < pc_end );3645 break;3646 case 11:3647 do {3648 STBIR_MOVE_4( pc, coeffs );3649 STBIR_MOVE_4( pc+4, coeffs+4 );3650 STBIR_MOVE_2( pc+8, coeffs+8 );3651 STBIR_MOVE_1( pc+10, coeffs+10 );3652 pc += 11;3653 coeffs += coefficient_width;3654 } while ( pc < pc_end );3655 break;3656 case 12:3657 do {3658 STBIR_MOVE_4( pc, coeffs );3659 STBIR_MOVE_4( pc+4, coeffs+4 );3660 STBIR_MOVE_4( pc+8, coeffs+8 );3661 pc += 12;3662 coeffs += coefficient_width;3663 } while ( pc < pc_end );3664 break;3665 default:3666 do {3667 float * copy_end = pc + widest - 4;3668 float * c = coeffs;3669 do {3670 STBIR_NO_UNROLL( pc );3671 STBIR_MOVE_4( pc, c );3672 pc += 4;3673 c += 4;3674 } while ( pc <= copy_end );3675 copy_end += 4;3676 while ( pc < copy_end )3677 {3678 STBIR_MOVE_1( pc, c );3679 ++pc; ++c;3680 }3681 coeffs += coefficient_width;3682 } while ( pc < pc_end );3683 break;3684 }3685 }36863687 // some horizontal routines read one float off the end (which is then masked off), so put in a sentinal so we don't read an snan or denormal3688 coefficents[ widest * num_contributors ] = 8888.0f;36893690 // the minimum we might read for unrolled filters widths is 12. So, we need to3691 // make sure we never read outside the decode buffer, by possibly moving 3692 // the sample area back into the scanline, and putting zeros weights first.3693 // we start on the right edge and check until we're well past the possible3694 // clip area (2*widest).3695 {3696 stbir__contributors * contribs = contributors + num_contributors - 1;3697 float * coeffs = coefficents + widest * ( num_contributors - 1 );36983699 // go until no chance of clipping (this is usually less than 8 lops)3700 while ( ( ( contribs->n0 + widest*2 ) >= row_width ) && ( contribs >= contributors ) )3701 {3702 // might we clip??3703 if ( ( contribs->n0 + widest ) > row_width )3704 {3705 int stop_range = widest;3706 3707 // if range is larger than 12, it will be handled by generic loops that can terminate on the exact length3708 // of this contrib n1, instead of a fixed widest amount - so calculate this3709 if ( widest > 12 )3710 {3711 int mod;37123713 // how far will be read in the n_coeff loop (which depends on the widest count mod4);3714 mod = widest & 3;3715 stop_range = ( ( ( contribs->n1 - contribs->n0 + 1 ) - mod + 3 ) & ~3 ) + mod; 37163717 // the n_coeff loops do a minimum amount of coeffs, so factor that in!3718 if ( stop_range < ( 8 + mod ) ) stop_range = 8 + mod;3719 }37203721 // now see if we still clip with the refined range3722 if ( ( contribs->n0 + stop_range ) > row_width )3723 {3724 int new_n0 = row_width - stop_range;3725 int num = contribs->n1 - contribs->n0 + 1;3726 int backup = contribs->n0 - new_n0;3727 float * from_co = coeffs + num - 1;3728 float * to_co = from_co + backup;37293730 STBIR_ASSERT( ( new_n0 >= 0 ) && ( new_n0 < contribs->n0 ) );37313732 // move the coeffs over3733 while( num )3734 {3735 *to_co-- = *from_co--;3736 --num;3737 }3738 // zero new positions3739 while ( to_co >= coeffs )3740 *to_co-- = 0;3741 // set new start point3742 contribs->n0 = new_n0;3743 if ( widest > 12 )3744 {3745 int mod;37463747 // how far will be read in the n_coeff loop (which depends on the widest count mod4);3748 mod = widest & 3;3749 stop_range = ( ( ( contribs->n1 - contribs->n0 + 1 ) - mod + 3 ) & ~3 ) + mod; 37503751 // the n_coeff loops do a minimum amount of coeffs, so factor that in!3752 if ( stop_range < ( 8 + mod ) ) stop_range = 8 + mod;3753 }3754 }3755 }3756 --contribs;3757 coeffs -= widest;3758 }3759 }37603761 return widest;3762 #undef STBIR_MOVE_13763 #undef STBIR_MOVE_23764 #undef STBIR_MOVE_43765}37663767static void stbir__calculate_filters( stbir__sampler * samp, stbir__sampler * other_axis_for_pivot, void * user_data STBIR_ONLY_PROFILE_BUILD_GET_INFO )3768{3769 int n;3770 float scale = samp->scale_info.scale;3771 stbir__kernel_callback * kernel = samp->filter_kernel;3772 stbir__support_callback * support = samp->filter_support;3773 float inv_scale = samp->scale_info.inv_scale;3774 int input_full_size = samp->scale_info.input_full_size;3775 int gather_num_contributors = samp->num_contributors;3776 stbir__contributors* gather_contributors = samp->contributors;3777 float * gather_coeffs = samp->coefficients; 3778 int gather_coefficient_width = samp->coefficient_width;37793780 switch ( samp->is_gather )3781 {3782 case 1: // gather upsample3783 {3784 float out_pixels_radius = support(inv_scale,user_data) * scale;37853786 stbir__calculate_coefficients_for_gather_upsample( out_pixels_radius, kernel, &samp->scale_info, gather_num_contributors, gather_contributors, gather_coeffs, gather_coefficient_width, samp->edge, user_data );37873788 STBIR_PROFILE_BUILD_START( cleanup );3789 stbir__cleanup_gathered_coefficients( samp->edge, &samp->extent_info, &samp->scale_info, gather_num_contributors, gather_contributors, gather_coeffs, gather_coefficient_width );3790 STBIR_PROFILE_BUILD_END( cleanup );3791 }3792 break;37933794 case 0: // scatter downsample (only on vertical)3795 case 2: // gather downsample 3796 {3797 float in_pixels_radius = support(scale,user_data) * inv_scale;3798 int filter_pixel_margin = samp->filter_pixel_margin;3799 int input_end = input_full_size + filter_pixel_margin;3800 3801 // if this is a scatter, we do a downsample gather to get the coeffs, and then pivot after3802 if ( !samp->is_gather )3803 {3804 // check if we are using the same gather downsample on the horizontal as this vertical, 3805 // if so, then we don't have to generate them, we can just pivot from the horizontal.3806 if ( other_axis_for_pivot )3807 {3808 gather_contributors = other_axis_for_pivot->contributors;3809 gather_coeffs = other_axis_for_pivot->coefficients;3810 gather_coefficient_width = other_axis_for_pivot->coefficient_width;3811 gather_num_contributors = other_axis_for_pivot->num_contributors;3812 samp->extent_info.lowest = other_axis_for_pivot->extent_info.lowest;3813 samp->extent_info.highest = other_axis_for_pivot->extent_info.highest;3814 samp->extent_info.widest = other_axis_for_pivot->extent_info.widest;3815 goto jump_right_to_pivot;3816 }38173818 gather_contributors = samp->gather_prescatter_contributors;3819 gather_coeffs = samp->gather_prescatter_coefficients;3820 gather_coefficient_width = samp->gather_prescatter_coefficient_width;3821 gather_num_contributors = samp->gather_prescatter_num_contributors;3822 }38233824 stbir__calculate_coefficients_for_gather_downsample( -filter_pixel_margin, input_end, in_pixels_radius, kernel, &samp->scale_info, gather_coefficient_width, gather_num_contributors, gather_contributors, gather_coeffs, user_data );38253826 STBIR_PROFILE_BUILD_START( cleanup );3827 stbir__cleanup_gathered_coefficients( samp->edge, &samp->extent_info, &samp->scale_info, gather_num_contributors, gather_contributors, gather_coeffs, gather_coefficient_width );3828 STBIR_PROFILE_BUILD_END( cleanup );38293830 if ( !samp->is_gather )3831 {3832 // if this is a scatter (vertical only), then we need to pivot the coeffs3833 stbir__contributors * scatter_contributors;3834 int highest_set;38353836 jump_right_to_pivot:38373838 STBIR_PROFILE_BUILD_START( pivot );38393840 highest_set = (-filter_pixel_margin) - 1;3841 for (n = 0; n < gather_num_contributors; n++)3842 {3843 int k;3844 int gn0 = gather_contributors->n0, gn1 = gather_contributors->n1;3845 int scatter_coefficient_width = samp->coefficient_width;3846 float * scatter_coeffs = samp->coefficients + ( gn0 + filter_pixel_margin ) * scatter_coefficient_width;3847 float * g_coeffs = gather_coeffs;3848 scatter_contributors = samp->contributors + ( gn0 + filter_pixel_margin );3849 3850 for (k = gn0 ; k <= gn1 ; k++ )3851 {3852 float gc = *g_coeffs++;3853 if ( ( k > highest_set ) || ( scatter_contributors->n0 > scatter_contributors->n1 ) )3854 {3855 {3856 // if we are skipping over several contributors, we need to clear the skipped ones3857 stbir__contributors * clear_contributors = samp->contributors + ( highest_set + filter_pixel_margin + 1);3858 while ( clear_contributors < scatter_contributors )3859 {3860 clear_contributors->n0 = 0; 3861 clear_contributors->n1 = -1;3862 ++clear_contributors;3863 }3864 }3865 scatter_contributors->n0 = n;3866 scatter_contributors->n1 = n;3867 scatter_coeffs[0] = gc;3868 highest_set = k;3869 }3870 else3871 {3872 stbir__insert_coeff( scatter_contributors, scatter_coeffs, n, gc );3873 }3874 ++scatter_contributors;3875 scatter_coeffs += scatter_coefficient_width;3876 }38773878 ++gather_contributors;3879 gather_coeffs += gather_coefficient_width;3880 }38813882 // now clear any unset contribs3883 {3884 stbir__contributors * clear_contributors = samp->contributors + ( highest_set + filter_pixel_margin + 1);3885 stbir__contributors * end_contributors = samp->contributors + samp->num_contributors;3886 while ( clear_contributors < end_contributors )3887 {3888 clear_contributors->n0 = 0;3889 clear_contributors->n1 = -1;3890 ++clear_contributors;3891 }3892 }38933894 STBIR_PROFILE_BUILD_END( pivot );3895 }3896 }3897 break;3898 }3899}390039013902//========================================================================================================3903// scanline decoders and encoders39043905#define stbir__coder_min_num 13906#define STB_IMAGE_RESIZE_DO_CODERS3907#include STBIR__HEADER_FILENAME39083909#define stbir__decode_suffix BGRA3910#define stbir__decode_swizzle3911#define stbir__decode_order0 2 3912#define stbir__decode_order1 13913#define stbir__decode_order2 03914#define stbir__decode_order3 33915#define stbir__encode_order0 2 3916#define stbir__encode_order1 13917#define stbir__encode_order2 03918#define stbir__encode_order3 33919#define stbir__coder_min_num 43920#define STB_IMAGE_RESIZE_DO_CODERS3921#include STBIR__HEADER_FILENAME39223923#define stbir__decode_suffix ARGB3924#define stbir__decode_swizzle3925#define stbir__decode_order0 1 3926#define stbir__decode_order1 23927#define stbir__decode_order2 33928#define stbir__decode_order3 03929#define stbir__encode_order0 3 3930#define stbir__encode_order1 03931#define stbir__encode_order2 13932#define stbir__encode_order3 23933#define stbir__coder_min_num 43934#define STB_IMAGE_RESIZE_DO_CODERS3935#include STBIR__HEADER_FILENAME39363937#define stbir__decode_suffix ABGR3938#define stbir__decode_swizzle3939#define stbir__decode_order0 3 3940#define stbir__decode_order1 23941#define stbir__decode_order2 13942#define stbir__decode_order3 03943#define stbir__encode_order0 3 3944#define stbir__encode_order1 23945#define stbir__encode_order2 13946#define stbir__encode_order3 03947#define stbir__coder_min_num 43948#define STB_IMAGE_RESIZE_DO_CODERS3949#include STBIR__HEADER_FILENAME39503951#define stbir__decode_suffix AR3952#define stbir__decode_swizzle3953#define stbir__decode_order0 1 3954#define stbir__decode_order1 0 3955#define stbir__decode_order2 33956#define stbir__decode_order3 23957#define stbir__encode_order0 1 3958#define stbir__encode_order1 0 3959#define stbir__encode_order2 33960#define stbir__encode_order3 23961#define stbir__coder_min_num 23962#define STB_IMAGE_RESIZE_DO_CODERS3963#include STBIR__HEADER_FILENAME396439653966// fancy alpha means we expand to keep both premultipied and non-premultiplied color channels3967static void stbir__fancy_alpha_weight_4ch( float * out_buffer, int width_times_channels )3968{3969 float STBIR_STREAMOUT_PTR(*) out = out_buffer;3970 float const * end_decode = out_buffer + ( width_times_channels / 4 ) * 7; // decode buffer aligned to end of out_buffer3971 float STBIR_STREAMOUT_PTR(*) decode = (float*)end_decode - width_times_channels;39723973 // fancy alpha is stored internally as R G B A Rpm Gpm Bpm39743975 #ifdef STBIR_SIMD3976 3977 #ifdef STBIR_SIMD83978 decode += 16;3979 while ( decode <= end_decode )3980 {3981 stbir__simdf8 d0,d1,a0,a1,p0,p1;3982 STBIR_NO_UNROLL(decode);3983 stbir__simdf8_load( d0, decode-16 );3984 stbir__simdf8_load( d1, decode-16+8 );3985 stbir__simdf8_0123to33333333( a0, d0 );3986 stbir__simdf8_0123to33333333( a1, d1 );3987 stbir__simdf8_mult( p0, a0, d0 );3988 stbir__simdf8_mult( p1, a1, d1 );3989 stbir__simdf8_bot4s( a0, d0, p0 );3990 stbir__simdf8_bot4s( a1, d1, p1 );3991 stbir__simdf8_top4s( d0, d0, p0 );3992 stbir__simdf8_top4s( d1, d1, p1 );3993 stbir__simdf8_store ( out, a0 );3994 stbir__simdf8_store ( out+7, d0 );3995 stbir__simdf8_store ( out+14, a1 );3996 stbir__simdf8_store ( out+21, d1 );3997 decode += 16;3998 out += 28;3999 }4000 decode -= 16;4001 #else 4002 decode += 8;4003 while ( decode <= end_decode )4004 {4005 stbir__simdf d0,a0,d1,a1,p0,p1;4006 STBIR_NO_UNROLL(decode);4007 stbir__simdf_load( d0, decode-8 );4008 stbir__simdf_load( d1, decode-8+4 );4009 stbir__simdf_0123to3333( a0, d0 );4010 stbir__simdf_0123to3333( a1, d1 );4011 stbir__simdf_mult( p0, a0, d0 );4012 stbir__simdf_mult( p1, a1, d1 );4013 stbir__simdf_store ( out, d0 );4014 stbir__simdf_store ( out+4, p0 );4015 stbir__simdf_store ( out+7, d1 );4016 stbir__simdf_store ( out+7+4, p1 );4017 decode += 8;4018 out += 14;4019 }4020 decode -= 8;4021 #endif40224023 // might be one last odd pixel4024 #ifdef STBIR_SIMD84025 while ( decode < end_decode )4026 #else4027 if ( decode < end_decode )4028 #endif4029 {4030 stbir__simdf d,a,p;4031 stbir__simdf_load( d, decode );4032 stbir__simdf_0123to3333( a, d );4033 stbir__simdf_mult( p, a, d );4034 stbir__simdf_store ( out, d );4035 stbir__simdf_store ( out+4, p );4036 decode += 4;4037 out += 7;4038 }40394040 #else40414042 while( decode < end_decode )4043 {4044 float r = decode[0], g = decode[1], b = decode[2], alpha = decode[3];4045 out[0] = r;4046 out[1] = g;4047 out[2] = b;4048 out[3] = alpha;4049 out[4] = r * alpha;4050 out[5] = g * alpha;4051 out[6] = b * alpha;4052 out += 7;4053 decode += 4;4054 }40554056 #endif4057}40584059static void stbir__fancy_alpha_weight_2ch( float * out_buffer, int width_times_channels )4060{4061 float STBIR_STREAMOUT_PTR(*) out = out_buffer;4062 float const * end_decode = out_buffer + ( width_times_channels / 2 ) * 3;4063 float STBIR_STREAMOUT_PTR(*) decode = (float*)end_decode - width_times_channels;40644065 // for fancy alpha, turns into: [X A Xpm][X A Xpm],etc40664067 #ifdef STBIR_SIMD40684069 decode += 8;4070 if ( decode <= end_decode )4071 {4072 do {4073 #ifdef STBIR_SIMD84074 stbir__simdf8 d0,a0,p0;4075 STBIR_NO_UNROLL(decode);4076 stbir__simdf8_load( d0, decode-8 );4077 stbir__simdf8_0123to11331133( p0, d0 );4078 stbir__simdf8_0123to00220022( a0, d0 );4079 stbir__simdf8_mult( p0, p0, a0 );4080 4081 stbir__simdf_store2( out, stbir__if_simdf8_cast_to_simdf4( d0 ) );4082 stbir__simdf_store( out+2, stbir__if_simdf8_cast_to_simdf4( p0 ) );4083 stbir__simdf_store2h( out+3, stbir__if_simdf8_cast_to_simdf4( d0 ) );4084 4085 stbir__simdf_store2( out+6, stbir__simdf8_gettop4( d0 ) );4086 stbir__simdf_store( out+8, stbir__simdf8_gettop4( p0 ) );4087 stbir__simdf_store2h( out+9, stbir__simdf8_gettop4( d0 ) );4088 #else4089 stbir__simdf d0,a0,d1,a1,p0,p1;4090 STBIR_NO_UNROLL(decode);4091 stbir__simdf_load( d0, decode-8 );4092 stbir__simdf_load( d1, decode-8+4 );4093 stbir__simdf_0123to1133( p0, d0 );4094 stbir__simdf_0123to1133( p1, d1 );4095 stbir__simdf_0123to0022( a0, d0 );4096 stbir__simdf_0123to0022( a1, d1 );4097 stbir__simdf_mult( p0, p0, a0 );4098 stbir__simdf_mult( p1, p1, a1 );40994100 stbir__simdf_store2( out, d0 );4101 stbir__simdf_store( out+2, p0 );4102 stbir__simdf_store2h( out+3, d0 );41034104 stbir__simdf_store2( out+6, d1 );4105 stbir__simdf_store( out+8, p1 );4106 stbir__simdf_store2h( out+9, d1 );4107 #endif4108 decode += 8;4109 out += 12;4110 } while ( decode <= end_decode );4111 }4112 decode -= 8;4113 #endif41144115 while( decode < end_decode )4116 {4117 float x = decode[0], y = decode[1];4118 STBIR_SIMD_NO_UNROLL(decode);4119 out[0] = x;4120 out[1] = y;4121 out[2] = x * y;4122 out += 3;4123 decode += 2;4124 }4125}41264127static void stbir__fancy_alpha_unweight_4ch( float * encode_buffer, int width_times_channels )4128{4129 float STBIR_SIMD_STREAMOUT_PTR(*) encode = encode_buffer;4130 float STBIR_SIMD_STREAMOUT_PTR(*) input = encode_buffer;4131 float const * end_output = encode_buffer + width_times_channels;41324133 // fancy RGBA is stored internally as R G B A Rpm Gpm Bpm41344135 do {4136 float alpha = input[3];4137#ifdef STBIR_SIMD4138 stbir__simdf i,ia;4139 STBIR_SIMD_NO_UNROLL(encode);4140 if ( alpha < stbir__small_float )4141 {4142 stbir__simdf_load( i, input );4143 stbir__simdf_store( encode, i );4144 }4145 else4146 {4147 stbir__simdf_load1frep4( ia, 1.0f / alpha );4148 stbir__simdf_load( i, input+4 );4149 stbir__simdf_mult( i, i, ia );4150 stbir__simdf_store( encode, i );4151 encode[3] = alpha;4152 }4153#else4154 if ( alpha < stbir__small_float )4155 {4156 encode[0] = input[0];4157 encode[1] = input[1];4158 encode[2] = input[2];4159 }4160 else4161 {4162 float ialpha = 1.0f / alpha;4163 encode[0] = input[4] * ialpha;4164 encode[1] = input[5] * ialpha;4165 encode[2] = input[6] * ialpha;4166 }4167 encode[3] = alpha;4168#endif41694170 input += 7;4171 encode += 4;4172 } while ( encode < end_output );4173}41744175// format: [X A Xpm][X A Xpm] etc4176static void stbir__fancy_alpha_unweight_2ch( float * encode_buffer, int width_times_channels )4177{4178 float STBIR_SIMD_STREAMOUT_PTR(*) encode = encode_buffer;4179 float STBIR_SIMD_STREAMOUT_PTR(*) input = encode_buffer;4180 float const * end_output = encode_buffer + width_times_channels;41814182 do {4183 float alpha = input[1];4184 encode[0] = input[0];4185 if ( alpha >= stbir__small_float )4186 encode[0] = input[2] / alpha;4187 encode[1] = alpha;41884189 input += 3;4190 encode += 2;4191 } while ( encode < end_output );4192}41934194static void stbir__simple_alpha_weight_4ch( float * decode_buffer, int width_times_channels )4195{4196 float STBIR_STREAMOUT_PTR(*) decode = decode_buffer;4197 float const * end_decode = decode_buffer + width_times_channels;41984199 #ifdef STBIR_SIMD4200 {4201 decode += 2 * stbir__simdfX_float_count;4202 while ( decode <= end_decode )4203 {4204 stbir__simdfX d0,a0,d1,a1;4205 STBIR_NO_UNROLL(decode);4206 stbir__simdfX_load( d0, decode-2*stbir__simdfX_float_count );4207 stbir__simdfX_load( d1, decode-2*stbir__simdfX_float_count+stbir__simdfX_float_count );4208 stbir__simdfX_aaa1( a0, d0, STBIR_onesX );4209 stbir__simdfX_aaa1( a1, d1, STBIR_onesX );4210 stbir__simdfX_mult( d0, d0, a0 );4211 stbir__simdfX_mult( d1, d1, a1 );4212 stbir__simdfX_store ( decode-2*stbir__simdfX_float_count, d0 );4213 stbir__simdfX_store ( decode-2*stbir__simdfX_float_count+stbir__simdfX_float_count, d1 );4214 decode += 2 * stbir__simdfX_float_count;4215 }4216 decode -= 2 * stbir__simdfX_float_count;42174218 // few last pixels remnants4219 #ifdef STBIR_SIMD84220 while ( decode < end_decode )4221 #else4222 if ( decode < end_decode )4223 #endif4224 {4225 stbir__simdf d,a;4226 stbir__simdf_load( d, decode );4227 stbir__simdf_aaa1( a, d, STBIR__CONSTF(STBIR_ones) );4228 stbir__simdf_mult( d, d, a );4229 stbir__simdf_store ( decode, d );4230 decode += 4;4231 }4232 }42334234 #else42354236 while( decode < end_decode )4237 {4238 float alpha = decode[3];4239 decode[0] *= alpha;4240 decode[1] *= alpha;4241 decode[2] *= alpha;4242 decode += 4;4243 }42444245 #endif4246}42474248static void stbir__simple_alpha_weight_2ch( float * decode_buffer, int width_times_channels )4249{4250 float STBIR_STREAMOUT_PTR(*) decode = decode_buffer;4251 float const * end_decode = decode_buffer + width_times_channels;42524253 #ifdef STBIR_SIMD4254 decode += 2 * stbir__simdfX_float_count;4255 while ( decode <= end_decode )4256 {4257 stbir__simdfX d0,a0,d1,a1;4258 STBIR_NO_UNROLL(decode);4259 stbir__simdfX_load( d0, decode-2*stbir__simdfX_float_count );4260 stbir__simdfX_load( d1, decode-2*stbir__simdfX_float_count+stbir__simdfX_float_count );4261 stbir__simdfX_a1a1( a0, d0, STBIR_onesX );4262 stbir__simdfX_a1a1( a1, d1, STBIR_onesX );4263 stbir__simdfX_mult( d0, d0, a0 );4264 stbir__simdfX_mult( d1, d1, a1 );4265 stbir__simdfX_store ( decode-2*stbir__simdfX_float_count, d0 );4266 stbir__simdfX_store ( decode-2*stbir__simdfX_float_count+stbir__simdfX_float_count, d1 );4267 decode += 2 * stbir__simdfX_float_count;4268 }4269 decode -= 2 * stbir__simdfX_float_count;4270 #endif42714272 while( decode < end_decode )4273 {4274 float alpha = decode[1];4275 STBIR_SIMD_NO_UNROLL(decode);4276 decode[0] *= alpha;4277 decode += 2;4278 }4279}42804281static void stbir__simple_alpha_unweight_4ch( float * encode_buffer, int width_times_channels )4282{4283 float STBIR_SIMD_STREAMOUT_PTR(*) encode = encode_buffer;4284 float const * end_output = encode_buffer + width_times_channels;42854286 do {4287 float alpha = encode[3];42884289#ifdef STBIR_SIMD4290 stbir__simdf i,ia;4291 STBIR_SIMD_NO_UNROLL(encode);4292 if ( alpha >= stbir__small_float )4293 {4294 stbir__simdf_load1frep4( ia, 1.0f / alpha );4295 stbir__simdf_load( i, encode );4296 stbir__simdf_mult( i, i, ia );4297 stbir__simdf_store( encode, i );4298 encode[3] = alpha;4299 }4300#else4301 if ( alpha >= stbir__small_float )4302 {4303 float ialpha = 1.0f / alpha;4304 encode[0] *= ialpha;4305 encode[1] *= ialpha;4306 encode[2] *= ialpha;4307 }4308#endif4309 encode += 4;4310 } while ( encode < end_output );4311}43124313static void stbir__simple_alpha_unweight_2ch( float * encode_buffer, int width_times_channels )4314{4315 float STBIR_SIMD_STREAMOUT_PTR(*) encode = encode_buffer;4316 float const * end_output = encode_buffer + width_times_channels;43174318 do {4319 float alpha = encode[1];4320 if ( alpha >= stbir__small_float )4321 encode[0] /= alpha;4322 encode += 2;4323 } while ( encode < end_output );4324}432543264327// only used in RGB->BGR or BGR->RGB4328static void stbir__simple_flip_3ch( float * decode_buffer, int width_times_channels )4329{4330 float STBIR_STREAMOUT_PTR(*) decode = decode_buffer;4331 float const * end_decode = decode_buffer + width_times_channels;43324333 decode += 12;4334 while( decode <= end_decode )4335 {4336 float t0,t1,t2,t3;4337 STBIR_NO_UNROLL(decode);4338 t0 = decode[0]; t1 = decode[3]; t2 = decode[6]; t3 = decode[9];4339 decode[0] = decode[2]; decode[3] = decode[5]; decode[6] = decode[8]; decode[9] = decode[11];4340 decode[2] = t0; decode[5] = t1; decode[8] = t2; decode[11] = t3;4341 decode += 12;4342 }4343 decode -= 12;43444345 while( decode < end_decode )4346 {4347 float t = decode[0];4348 STBIR_NO_UNROLL(decode);4349 decode[0] = decode[2];4350 decode[2] = t;4351 decode += 3;4352 }4353}4354435543564357static void stbir__decode_scanline(stbir__info const * stbir_info, int n, float * output_buffer STBIR_ONLY_PROFILE_GET_SPLIT_INFO )4358{4359 int channels = stbir_info->channels;4360 int effective_channels = stbir_info->effective_channels;4361 int input_sample_in_bytes = stbir__type_size[stbir_info->input_type] * channels;4362 stbir_edge edge_horizontal = stbir_info->horizontal.edge;4363 stbir_edge edge_vertical = stbir_info->vertical.edge;4364 int row = stbir__edge_wrap(edge_vertical, n, stbir_info->vertical.scale_info.input_full_size);4365 const void* input_plane_data = ( (char *) stbir_info->input_data ) + (ptrdiff_t)row * (ptrdiff_t) stbir_info->input_stride_bytes;4366 stbir__span const * spans = stbir_info->scanline_extents.spans;4367 float* full_decode_buffer = output_buffer - stbir_info->scanline_extents.conservative.n0 * effective_channels;43684369 // if we are on edge_zero, and we get in here with an out of bounds n, then the calculate filters has failed4370 STBIR_ASSERT( !(edge_vertical == STBIR_EDGE_ZERO && (n < 0 || n >= stbir_info->vertical.scale_info.input_full_size)) );43714372 do 4373 {4374 float * decode_buffer;4375 void const * input_data;4376 float * end_decode;4377 int width_times_channels;4378 int width;43794380 if ( spans->n1 < spans->n0 ) 4381 break;43824383 width = spans->n1 + 1 - spans->n0;4384 decode_buffer = full_decode_buffer + spans->n0 * effective_channels;4385 end_decode = full_decode_buffer + ( spans->n1 + 1 ) * effective_channels;4386 width_times_channels = width * channels;43874388 // read directly out of input plane by default4389 input_data = ( (char*)input_plane_data ) + spans->pixel_offset_for_input * input_sample_in_bytes;43904391 // if we have an input callback, call it to get the input data4392 if ( stbir_info->in_pixels_cb )4393 {4394 // call the callback with a temp buffer (that they can choose to use or not). the temp is just right aligned memory in the decode_buffer itself4395 input_data = stbir_info->in_pixels_cb( ( (char*) end_decode ) - ( width * input_sample_in_bytes ), input_plane_data, width, spans->pixel_offset_for_input, row, stbir_info->user_data );4396 }4397 4398 STBIR_PROFILE_START( decode );4399 // convert the pixels info the float decode_buffer, (we index from end_decode, so that when channels<effective_channels, we are right justified in the buffer)4400 stbir_info->decode_pixels( (float*)end_decode - width_times_channels, width_times_channels, input_data );4401 STBIR_PROFILE_END( decode );44024403 if (stbir_info->alpha_weight)4404 {4405 STBIR_PROFILE_START( alpha );4406 stbir_info->alpha_weight( decode_buffer, width_times_channels );4407 STBIR_PROFILE_END( alpha );4408 }44094410 ++spans;4411 } while ( spans <= ( &stbir_info->scanline_extents.spans[1] ) );44124413 // handle the edge_wrap filter (all other types are handled back out at the calculate_filter stage)4414 // basically the idea here is that if we have the whole scanline in memory, we don't redecode the4415 // wrapped edge pixels, and instead just memcpy them from the scanline into the edge positions4416 if ( ( edge_horizontal == STBIR_EDGE_WRAP ) && ( stbir_info->scanline_extents.edge_sizes[0] | stbir_info->scanline_extents.edge_sizes[1] ) )4417 {4418 // this code only runs if we're in edge_wrap, and we're doing the entire scanline4419 int e, start_x[2];4420 int input_full_size = stbir_info->horizontal.scale_info.input_full_size;4421 4422 start_x[0] = -stbir_info->scanline_extents.edge_sizes[0]; // left edge start x4423 start_x[1] = input_full_size; // right edge44244425 for( e = 0; e < 2 ; e++ )4426 {4427 // do each margin4428 int margin = stbir_info->scanline_extents.edge_sizes[e];4429 if ( margin )4430 {4431 int x = start_x[e];4432 float * marg = full_decode_buffer + x * effective_channels;4433 float const * src = full_decode_buffer + stbir__edge_wrap(edge_horizontal, x, input_full_size) * effective_channels;4434 STBIR_MEMCPY( marg, src, margin * effective_channels * sizeof(float) );4435 }4436 }4437 }4438}443944404441//=================4442// Do 1 channel horizontal routines44434444#ifdef STBIR_SIMD44454446#define stbir__1_coeff_only() \4447 stbir__simdf tot,c; \4448 STBIR_SIMD_NO_UNROLL(decode); \4449 stbir__simdf_load1( c, hc ); \4450 stbir__simdf_mult1_mem( tot, c, decode ); 44514452#define stbir__2_coeff_only() \4453 stbir__simdf tot,c,d; \4454 STBIR_SIMD_NO_UNROLL(decode); \4455 stbir__simdf_load2z( c, hc ); \4456 stbir__simdf_load2( d, decode ); \4457 stbir__simdf_mult( tot, c, d ); \4458 stbir__simdf_0123to1230( c, tot ); \4459 stbir__simdf_add1( tot, tot, c ); 44604461#define stbir__3_coeff_only() \4462 stbir__simdf tot,c,t; \4463 STBIR_SIMD_NO_UNROLL(decode); \4464 stbir__simdf_load( c, hc ); \4465 stbir__simdf_mult_mem( tot, c, decode ); \4466 stbir__simdf_0123to1230( c, tot ); \4467 stbir__simdf_0123to2301( t, tot ); \4468 stbir__simdf_add1( tot, tot, c ); \4469 stbir__simdf_add1( tot, tot, t ); 44704471#define stbir__store_output_tiny() \4472 stbir__simdf_store1( output, tot ); \4473 horizontal_coefficients += coefficient_width; \4474 ++horizontal_contributors; \4475 output += 1;44764477#define stbir__4_coeff_start() \4478 stbir__simdf tot,c; \4479 STBIR_SIMD_NO_UNROLL(decode); \4480 stbir__simdf_load( c, hc ); \4481 stbir__simdf_mult_mem( tot, c, decode ); \44824483#define stbir__4_coeff_continue_from_4( ofs ) \4484 STBIR_SIMD_NO_UNROLL(decode); \4485 stbir__simdf_load( c, hc + (ofs) ); \4486 stbir__simdf_madd_mem( tot, tot, c, decode+(ofs) ); 44874488#define stbir__1_coeff_remnant( ofs ) \4489 { stbir__simdf d; \4490 stbir__simdf_load1z( c, hc + (ofs) ); \4491 stbir__simdf_load1( d, decode + (ofs) ); \4492 stbir__simdf_madd( tot, tot, d, c ); }44934494#define stbir__2_coeff_remnant( ofs ) \4495 { stbir__simdf d; \4496 stbir__simdf_load2z( c, hc+(ofs) ); \4497 stbir__simdf_load2( d, decode+(ofs) ); \4498 stbir__simdf_madd( tot, tot, d, c ); } 44994500#define stbir__3_coeff_setup() \4501 stbir__simdf mask; \4502 stbir__simdf_load( mask, STBIR_mask + 3 );45034504#define stbir__3_coeff_remnant( ofs ) \4505 stbir__simdf_load( c, hc+(ofs) ); \4506 stbir__simdf_and( c, c, mask ); \4507 stbir__simdf_madd_mem( tot, tot, c, decode+(ofs) );45084509#define stbir__store_output() \4510 stbir__simdf_0123to2301( c, tot ); \4511 stbir__simdf_add( tot, tot, c ); \4512 stbir__simdf_0123to1230( c, tot ); \4513 stbir__simdf_add1( tot, tot, c ); \4514 stbir__simdf_store1( output, tot ); \4515 horizontal_coefficients += coefficient_width; \4516 ++horizontal_contributors; \4517 output += 1;45184519#else45204521#define stbir__1_coeff_only() \4522 float tot; \4523 tot = decode[0]*hc[0]; 45244525#define stbir__2_coeff_only() \4526 float tot; \4527 tot = decode[0] * hc[0]; \4528 tot += decode[1] * hc[1]; 45294530#define stbir__3_coeff_only() \4531 float tot; \4532 tot = decode[0] * hc[0]; \4533 tot += decode[1] * hc[1]; \4534 tot += decode[2] * hc[2]; 45354536#define stbir__store_output_tiny() \4537 output[0] = tot; \4538 horizontal_coefficients += coefficient_width; \4539 ++horizontal_contributors; \4540 output += 1;45414542#define stbir__4_coeff_start() \4543 float tot0,tot1,tot2,tot3; \4544 tot0 = decode[0] * hc[0]; \4545 tot1 = decode[1] * hc[1]; \4546 tot2 = decode[2] * hc[2]; \4547 tot3 = decode[3] * hc[3]; 45484549#define stbir__4_coeff_continue_from_4( ofs ) \4550 tot0 += decode[0+(ofs)] * hc[0+(ofs)]; \4551 tot1 += decode[1+(ofs)] * hc[1+(ofs)]; \4552 tot2 += decode[2+(ofs)] * hc[2+(ofs)]; \4553 tot3 += decode[3+(ofs)] * hc[3+(ofs)]; 45544555#define stbir__1_coeff_remnant( ofs ) \4556 tot0 += decode[0+(ofs)] * hc[0+(ofs)]; 45574558#define stbir__2_coeff_remnant( ofs ) \4559 tot0 += decode[0+(ofs)] * hc[0+(ofs)]; \4560 tot1 += decode[1+(ofs)] * hc[1+(ofs)]; \45614562#define stbir__3_coeff_remnant( ofs ) \4563 tot0 += decode[0+(ofs)] * hc[0+(ofs)]; \4564 tot1 += decode[1+(ofs)] * hc[1+(ofs)]; \4565 tot2 += decode[2+(ofs)] * hc[2+(ofs)]; 45664567#define stbir__store_output() \4568 output[0] = (tot0+tot2)+(tot1+tot3); \4569 horizontal_coefficients += coefficient_width; \4570 ++horizontal_contributors; \4571 output += 1;45724573#endif 45744575#define STBIR__horizontal_channels 14576#define STB_IMAGE_RESIZE_DO_HORIZONTALS4577#include STBIR__HEADER_FILENAME457845794580//=================4581// Do 2 channel horizontal routines45824583#ifdef STBIR_SIMD45844585#define stbir__1_coeff_only() \4586 stbir__simdf tot,c,d; \4587 STBIR_SIMD_NO_UNROLL(decode); \4588 stbir__simdf_load1z( c, hc ); \4589 stbir__simdf_0123to0011( c, c ); \4590 stbir__simdf_load2( d, decode ); \4591 stbir__simdf_mult( tot, d, c ); 45924593#define stbir__2_coeff_only() \4594 stbir__simdf tot,c; \4595 STBIR_SIMD_NO_UNROLL(decode); \4596 stbir__simdf_load2( c, hc ); \4597 stbir__simdf_0123to0011( c, c ); \4598 stbir__simdf_mult_mem( tot, c, decode ); 45994600#define stbir__3_coeff_only() \4601 stbir__simdf tot,c,cs,d; \4602 STBIR_SIMD_NO_UNROLL(decode); \4603 stbir__simdf_load( cs, hc ); \4604 stbir__simdf_0123to0011( c, cs ); \4605 stbir__simdf_mult_mem( tot, c, decode ); \4606 stbir__simdf_0123to2222( c, cs ); \4607 stbir__simdf_load2z( d, decode+4 ); \4608 stbir__simdf_madd( tot, tot, d, c ); 46094610#define stbir__store_output_tiny() \4611 stbir__simdf_0123to2301( c, tot ); \4612 stbir__simdf_add( tot, tot, c ); \4613 stbir__simdf_store2( output, tot ); \4614 horizontal_coefficients += coefficient_width; \4615 ++horizontal_contributors; \4616 output += 2;46174618#ifdef STBIR_SIMD846194620#define stbir__4_coeff_start() \4621 stbir__simdf8 tot0,c,cs; \4622 STBIR_SIMD_NO_UNROLL(decode); \4623 stbir__simdf8_load4b( cs, hc ); \4624 stbir__simdf8_0123to00112233( c, cs ); \4625 stbir__simdf8_mult_mem( tot0, c, decode );46264627#define stbir__4_coeff_continue_from_4( ofs ) \4628 STBIR_SIMD_NO_UNROLL(decode); \4629 stbir__simdf8_load4b( cs, hc + (ofs) ); \4630 stbir__simdf8_0123to00112233( c, cs ); \4631 stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*2 ); 46324633#define stbir__1_coeff_remnant( ofs ) \4634 { stbir__simdf t; \4635 stbir__simdf_load1z( t, hc + (ofs) ); \4636 stbir__simdf_0123to0011( t, t ); \4637 stbir__simdf_mult_mem( t, t, decode+(ofs)*2 ); \4638 stbir__simdf8_add4( tot0, tot0, t ); }46394640#define stbir__2_coeff_remnant( ofs ) \4641 { stbir__simdf t; \4642 stbir__simdf_load2( t, hc + (ofs) ); \4643 stbir__simdf_0123to0011( t, t ); \4644 stbir__simdf_mult_mem( t, t, decode+(ofs)*2 ); \4645 stbir__simdf8_add4( tot0, tot0, t ); }46464647#define stbir__3_coeff_remnant( ofs ) \4648 { stbir__simdf8 d; \4649 stbir__simdf8_load4b( cs, hc + (ofs) ); \4650 stbir__simdf8_0123to00112233( c, cs ); \4651 stbir__simdf8_load6z( d, decode+(ofs)*2 ); \4652 stbir__simdf8_madd( tot0, tot0, c, d ); } 46534654#define stbir__store_output() \4655 { stbir__simdf t,c; \4656 stbir__simdf8_add4halves( t, stbir__if_simdf8_cast_to_simdf4(tot0), tot0 ); \4657 stbir__simdf_0123to2301( c, t ); \4658 stbir__simdf_add( t, t, c ); \4659 stbir__simdf_store2( output, t ); \4660 horizontal_coefficients += coefficient_width; \4661 ++horizontal_contributors; \4662 output += 2; }46634664#else46654666#define stbir__4_coeff_start() \4667 stbir__simdf tot0,tot1,c,cs; \4668 STBIR_SIMD_NO_UNROLL(decode); \4669 stbir__simdf_load( cs, hc ); \4670 stbir__simdf_0123to0011( c, cs ); \4671 stbir__simdf_mult_mem( tot0, c, decode ); \4672 stbir__simdf_0123to2233( c, cs ); \4673 stbir__simdf_mult_mem( tot1, c, decode+4 ); 46744675#define stbir__4_coeff_continue_from_4( ofs ) \4676 STBIR_SIMD_NO_UNROLL(decode); \4677 stbir__simdf_load( cs, hc + (ofs) ); \4678 stbir__simdf_0123to0011( c, cs ); \4679 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*2 ); \4680 stbir__simdf_0123to2233( c, cs ); \4681 stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*2+4 ); 46824683#define stbir__1_coeff_remnant( ofs ) \4684 { stbir__simdf d; \4685 stbir__simdf_load1z( cs, hc + (ofs) ); \4686 stbir__simdf_0123to0011( c, cs ); \4687 stbir__simdf_load2( d, decode + (ofs) * 2 ); \4688 stbir__simdf_madd( tot0, tot0, d, c ); }46894690#define stbir__2_coeff_remnant( ofs ) \4691 stbir__simdf_load2( cs, hc + (ofs) ); \4692 stbir__simdf_0123to0011( c, cs ); \4693 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*2 ); 46944695#define stbir__3_coeff_remnant( ofs ) \4696 { stbir__simdf d; \4697 stbir__simdf_load( cs, hc + (ofs) ); \4698 stbir__simdf_0123to0011( c, cs ); \4699 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*2 ); \4700 stbir__simdf_0123to2222( c, cs ); \4701 stbir__simdf_load2z( d, decode + (ofs) * 2 + 4 ); \4702 stbir__simdf_madd( tot1, tot1, d, c ); } 47034704#define stbir__store_output() \4705 stbir__simdf_add( tot0, tot0, tot1 ); \4706 stbir__simdf_0123to2301( c, tot0 ); \4707 stbir__simdf_add( tot0, tot0, c ); \4708 stbir__simdf_store2( output, tot0 ); \4709 horizontal_coefficients += coefficient_width; \4710 ++horizontal_contributors; \4711 output += 2;47124713#endif47144715#else47164717#define stbir__1_coeff_only() \4718 float tota,totb,c; \4719 c = hc[0]; \4720 tota = decode[0]*c; \4721 totb = decode[1]*c; 47224723#define stbir__2_coeff_only() \4724 float tota,totb,c; \4725 c = hc[0]; \4726 tota = decode[0]*c; \4727 totb = decode[1]*c; \4728 c = hc[1]; \4729 tota += decode[2]*c; \4730 totb += decode[3]*c; 47314732// this weird order of add matches the simd4733#define stbir__3_coeff_only() \4734 float tota,totb,c; \4735 c = hc[0]; \4736 tota = decode[0]*c; \4737 totb = decode[1]*c; \4738 c = hc[2]; \4739 tota += decode[4]*c; \4740 totb += decode[5]*c; \4741 c = hc[1]; \4742 tota += decode[2]*c; \4743 totb += decode[3]*c; 47444745#define stbir__store_output_tiny() \4746 output[0] = tota; \4747 output[1] = totb; \4748 horizontal_coefficients += coefficient_width; \4749 ++horizontal_contributors; \4750 output += 2;47514752#define stbir__4_coeff_start() \4753 float tota0,tota1,tota2,tota3,totb0,totb1,totb2,totb3,c; \4754 c = hc[0]; \4755 tota0 = decode[0]*c; \4756 totb0 = decode[1]*c; \4757 c = hc[1]; \4758 tota1 = decode[2]*c; \4759 totb1 = decode[3]*c; \4760 c = hc[2]; \4761 tota2 = decode[4]*c; \4762 totb2 = decode[5]*c; \4763 c = hc[3]; \4764 tota3 = decode[6]*c; \4765 totb3 = decode[7]*c; 47664767#define stbir__4_coeff_continue_from_4( ofs ) \4768 c = hc[0+(ofs)]; \4769 tota0 += decode[0+(ofs)*2]*c; \4770 totb0 += decode[1+(ofs)*2]*c; \4771 c = hc[1+(ofs)]; \4772 tota1 += decode[2+(ofs)*2]*c; \4773 totb1 += decode[3+(ofs)*2]*c; \4774 c = hc[2+(ofs)]; \4775 tota2 += decode[4+(ofs)*2]*c; \4776 totb2 += decode[5+(ofs)*2]*c; \4777 c = hc[3+(ofs)]; \4778 tota3 += decode[6+(ofs)*2]*c; \4779 totb3 += decode[7+(ofs)*2]*c; 47804781#define stbir__1_coeff_remnant( ofs ) \4782 c = hc[0+(ofs)]; \4783 tota0 += decode[0+(ofs)*2] * c; \4784 totb0 += decode[1+(ofs)*2] * c; 47854786#define stbir__2_coeff_remnant( ofs ) \4787 c = hc[0+(ofs)]; \4788 tota0 += decode[0+(ofs)*2] * c; \4789 totb0 += decode[1+(ofs)*2] * c; \4790 c = hc[1+(ofs)]; \4791 tota1 += decode[2+(ofs)*2] * c; \4792 totb1 += decode[3+(ofs)*2] * c; 47934794#define stbir__3_coeff_remnant( ofs ) \4795 c = hc[0+(ofs)]; \4796 tota0 += decode[0+(ofs)*2] * c; \4797 totb0 += decode[1+(ofs)*2] * c; \4798 c = hc[1+(ofs)]; \4799 tota1 += decode[2+(ofs)*2] * c; \4800 totb1 += decode[3+(ofs)*2] * c; \4801 c = hc[2+(ofs)]; \4802 tota2 += decode[4+(ofs)*2] * c; \4803 totb2 += decode[5+(ofs)*2] * c; 48044805#define stbir__store_output() \4806 output[0] = (tota0+tota2)+(tota1+tota3); \4807 output[1] = (totb0+totb2)+(totb1+totb3); \4808 horizontal_coefficients += coefficient_width; \4809 ++horizontal_contributors; \4810 output += 2;48114812#endif 48134814#define STBIR__horizontal_channels 24815#define STB_IMAGE_RESIZE_DO_HORIZONTALS4816#include STBIR__HEADER_FILENAME481748184819//=================4820// Do 3 channel horizontal routines48214822#ifdef STBIR_SIMD48234824#define stbir__1_coeff_only() \4825 stbir__simdf tot,c,d; \4826 STBIR_SIMD_NO_UNROLL(decode); \4827 stbir__simdf_load1z( c, hc ); \4828 stbir__simdf_0123to0001( c, c ); \4829 stbir__simdf_load( d, decode ); \4830 stbir__simdf_mult( tot, d, c ); 48314832#define stbir__2_coeff_only() \4833 stbir__simdf tot,c,cs,d; \4834 STBIR_SIMD_NO_UNROLL(decode); \4835 stbir__simdf_load2( cs, hc ); \4836 stbir__simdf_0123to0000( c, cs ); \4837 stbir__simdf_load( d, decode ); \4838 stbir__simdf_mult( tot, d, c ); \4839 stbir__simdf_0123to1111( c, cs ); \4840 stbir__simdf_load( d, decode+3 ); \4841 stbir__simdf_madd( tot, tot, d, c ); 48424843#define stbir__3_coeff_only() \4844 stbir__simdf tot,c,d,cs; \4845 STBIR_SIMD_NO_UNROLL(decode); \4846 stbir__simdf_load( cs, hc ); \4847 stbir__simdf_0123to0000( c, cs ); \4848 stbir__simdf_load( d, decode ); \4849 stbir__simdf_mult( tot, d, c ); \4850 stbir__simdf_0123to1111( c, cs ); \4851 stbir__simdf_load( d, decode+3 ); \4852 stbir__simdf_madd( tot, tot, d, c ); \4853 stbir__simdf_0123to2222( c, cs ); \4854 stbir__simdf_load( d, decode+6 ); \4855 stbir__simdf_madd( tot, tot, d, c ); 48564857#define stbir__store_output_tiny() \4858 stbir__simdf_store2( output, tot ); \4859 stbir__simdf_0123to2301( tot, tot ); \4860 stbir__simdf_store1( output+2, tot ); \4861 horizontal_coefficients += coefficient_width; \4862 ++horizontal_contributors; \4863 output += 3;48644865#ifdef STBIR_SIMD848664867// we're loading from the XXXYYY decode by -1 to get the XXXYYY into different halves of the AVX reg fyi4868#define stbir__4_coeff_start() \4869 stbir__simdf8 tot0,tot1,c,cs; stbir__simdf t; \4870 STBIR_SIMD_NO_UNROLL(decode); \4871 stbir__simdf8_load4b( cs, hc ); \4872 stbir__simdf8_0123to00001111( c, cs ); \4873 stbir__simdf8_mult_mem( tot0, c, decode - 1 ); \4874 stbir__simdf8_0123to22223333( c, cs ); \4875 stbir__simdf8_mult_mem( tot1, c, decode+6 - 1 ); 48764877#define stbir__4_coeff_continue_from_4( ofs ) \4878 STBIR_SIMD_NO_UNROLL(decode); \4879 stbir__simdf8_load4b( cs, hc + (ofs) ); \4880 stbir__simdf8_0123to00001111( c, cs ); \4881 stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*3 - 1 ); \4882 stbir__simdf8_0123to22223333( c, cs ); \4883 stbir__simdf8_madd_mem( tot1, tot1, c, decode+(ofs)*3 + 6 - 1 ); 48844885#define stbir__1_coeff_remnant( ofs ) \4886 STBIR_SIMD_NO_UNROLL(decode); \4887 stbir__simdf_load1rep4( t, hc + (ofs) ); \4888 stbir__simdf8_madd_mem4( tot0, tot0, t, decode+(ofs)*3 - 1 ); 48894890#define stbir__2_coeff_remnant( ofs ) \4891 STBIR_SIMD_NO_UNROLL(decode); \4892 stbir__simdf8_load4b( cs, hc + (ofs) - 2 ); \4893 stbir__simdf8_0123to22223333( c, cs ); \4894 stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*3 - 1 ); 4895 4896 #define stbir__3_coeff_remnant( ofs ) \4897 STBIR_SIMD_NO_UNROLL(decode); \4898 stbir__simdf8_load4b( cs, hc + (ofs) ); \4899 stbir__simdf8_0123to00001111( c, cs ); \4900 stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*3 - 1 ); \4901 stbir__simdf8_0123to2222( t, cs ); \4902 stbir__simdf8_madd_mem4( tot1, tot1, t, decode+(ofs)*3 + 6 - 1 ); 49034904#define stbir__store_output() \4905 stbir__simdf8_add( tot0, tot0, tot1 ); \4906 stbir__simdf_0123to1230( t, stbir__if_simdf8_cast_to_simdf4( tot0 ) ); \4907 stbir__simdf8_add4halves( t, t, tot0 ); \4908 horizontal_coefficients += coefficient_width; \4909 ++horizontal_contributors; \4910 output += 3; \4911 if ( output < output_end ) \4912 { \4913 stbir__simdf_store( output-3, t ); \4914 continue; \4915 } \4916 { stbir__simdf tt; stbir__simdf_0123to2301( tt, t ); \4917 stbir__simdf_store2( output-3, t ); \4918 stbir__simdf_store1( output+2-3, tt ); } \4919 break;492049214922#else49234924#define stbir__4_coeff_start() \4925 stbir__simdf tot0,tot1,tot2,c,cs; \4926 STBIR_SIMD_NO_UNROLL(decode); \4927 stbir__simdf_load( cs, hc ); \4928 stbir__simdf_0123to0001( c, cs ); \4929 stbir__simdf_mult_mem( tot0, c, decode ); \4930 stbir__simdf_0123to1122( c, cs ); \4931 stbir__simdf_mult_mem( tot1, c, decode+4 ); \4932 stbir__simdf_0123to2333( c, cs ); \4933 stbir__simdf_mult_mem( tot2, c, decode+8 ); 49344935#define stbir__4_coeff_continue_from_4( ofs ) \4936 STBIR_SIMD_NO_UNROLL(decode); \4937 stbir__simdf_load( cs, hc + (ofs) ); \4938 stbir__simdf_0123to0001( c, cs ); \4939 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*3 ); \4940 stbir__simdf_0123to1122( c, cs ); \4941 stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*3+4 ); \4942 stbir__simdf_0123to2333( c, cs ); \4943 stbir__simdf_madd_mem( tot2, tot2, c, decode+(ofs)*3+8 ); 49444945#define stbir__1_coeff_remnant( ofs ) \4946 STBIR_SIMD_NO_UNROLL(decode); \4947 stbir__simdf_load1z( c, hc + (ofs) ); \4948 stbir__simdf_0123to0001( c, c ); \4949 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*3 ); 49504951#define stbir__2_coeff_remnant( ofs ) \4952 { stbir__simdf d; \4953 STBIR_SIMD_NO_UNROLL(decode); \4954 stbir__simdf_load2z( cs, hc + (ofs) ); \4955 stbir__simdf_0123to0001( c, cs ); \4956 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*3 ); \4957 stbir__simdf_0123to1122( c, cs ); \4958 stbir__simdf_load2z( d, decode+(ofs)*3+4 ); \4959 stbir__simdf_madd( tot1, tot1, c, d ); } 49604961#define stbir__3_coeff_remnant( ofs ) \4962 { stbir__simdf d; \4963 STBIR_SIMD_NO_UNROLL(decode); \4964 stbir__simdf_load( cs, hc + (ofs) ); \4965 stbir__simdf_0123to0001( c, cs ); \4966 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*3 ); \4967 stbir__simdf_0123to1122( c, cs ); \4968 stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*3+4 ); \4969 stbir__simdf_0123to2222( c, cs ); \4970 stbir__simdf_load1z( d, decode+(ofs)*3+8 ); \4971 stbir__simdf_madd( tot2, tot2, c, d ); } 49724973#define stbir__store_output() \4974 stbir__simdf_0123ABCDto3ABx( c, tot0, tot1 ); \4975 stbir__simdf_0123ABCDto23Ax( cs, tot1, tot2 ); \4976 stbir__simdf_0123to1230( tot2, tot2 ); \4977 stbir__simdf_add( tot0, tot0, cs ); \4978 stbir__simdf_add( c, c, tot2 ); \4979 stbir__simdf_add( tot0, tot0, c ); \4980 horizontal_coefficients += coefficient_width; \4981 ++horizontal_contributors; \4982 output += 3; \4983 if ( output < output_end ) \4984 { \4985 stbir__simdf_store( output-3, tot0 ); \4986 continue; \4987 } \4988 stbir__simdf_0123to2301( tot1, tot0 ); \4989 stbir__simdf_store2( output-3, tot0 ); \4990 stbir__simdf_store1( output+2-3, tot1 ); \4991 break;49924993#endif49944995#else49964997#define stbir__1_coeff_only() \4998 float tot0, tot1, tot2, c; \4999 c = hc[0]; \5000 tot0 = decode[0]*c; \5001 tot1 = decode[1]*c; \5002 tot2 = decode[2]*c; 50035004#define stbir__2_coeff_only() \5005 float tot0, tot1, tot2, c; \5006 c = hc[0]; \5007 tot0 = decode[0]*c; \5008 tot1 = decode[1]*c; \5009 tot2 = decode[2]*c; \5010 c = hc[1]; \5011 tot0 += decode[3]*c; \5012 tot1 += decode[4]*c; \5013 tot2 += decode[5]*c; 50145015#define stbir__3_coeff_only() \5016 float tot0, tot1, tot2, c; \5017 c = hc[0]; \5018 tot0 = decode[0]*c; \5019 tot1 = decode[1]*c; \5020 tot2 = decode[2]*c; \5021 c = hc[1]; \5022 tot0 += decode[3]*c; \5023 tot1 += decode[4]*c; \5024 tot2 += decode[5]*c; \5025 c = hc[2]; \5026 tot0 += decode[6]*c; \5027 tot1 += decode[7]*c; \5028 tot2 += decode[8]*c; 50295030#define stbir__store_output_tiny() \5031 output[0] = tot0; \5032 output[1] = tot1; \5033 output[2] = tot2; \5034 horizontal_coefficients += coefficient_width; \5035 ++horizontal_contributors; \5036 output += 3;50375038#define stbir__4_coeff_start() \5039 float tota0,tota1,tota2,totb0,totb1,totb2,totc0,totc1,totc2,totd0,totd1,totd2,c; \5040 c = hc[0]; \5041 tota0 = decode[0]*c; \5042 tota1 = decode[1]*c; \5043 tota2 = decode[2]*c; \5044 c = hc[1]; \5045 totb0 = decode[3]*c; \5046 totb1 = decode[4]*c; \5047 totb2 = decode[5]*c; \5048 c = hc[2]; \5049 totc0 = decode[6]*c; \5050 totc1 = decode[7]*c; \5051 totc2 = decode[8]*c; \5052 c = hc[3]; \5053 totd0 = decode[9]*c; \5054 totd1 = decode[10]*c; \5055 totd2 = decode[11]*c; 50565057#define stbir__4_coeff_continue_from_4( ofs ) \5058 c = hc[0+(ofs)]; \5059 tota0 += decode[0+(ofs)*3]*c; \5060 tota1 += decode[1+(ofs)*3]*c; \5061 tota2 += decode[2+(ofs)*3]*c; \5062 c = hc[1+(ofs)]; \5063 totb0 += decode[3+(ofs)*3]*c; \5064 totb1 += decode[4+(ofs)*3]*c; \5065 totb2 += decode[5+(ofs)*3]*c; \5066 c = hc[2+(ofs)]; \5067 totc0 += decode[6+(ofs)*3]*c; \5068 totc1 += decode[7+(ofs)*3]*c; \5069 totc2 += decode[8+(ofs)*3]*c; \5070 c = hc[3+(ofs)]; \5071 totd0 += decode[9+(ofs)*3]*c; \5072 totd1 += decode[10+(ofs)*3]*c; \5073 totd2 += decode[11+(ofs)*3]*c; 50745075#define stbir__1_coeff_remnant( ofs ) \5076 c = hc[0+(ofs)]; \5077 tota0 += decode[0+(ofs)*3]*c; \5078 tota1 += decode[1+(ofs)*3]*c; \5079 tota2 += decode[2+(ofs)*3]*c;50805081#define stbir__2_coeff_remnant( ofs ) \5082 c = hc[0+(ofs)]; \5083 tota0 += decode[0+(ofs)*3]*c; \5084 tota1 += decode[1+(ofs)*3]*c; \5085 tota2 += decode[2+(ofs)*3]*c; \5086 c = hc[1+(ofs)]; \5087 totb0 += decode[3+(ofs)*3]*c; \5088 totb1 += decode[4+(ofs)*3]*c; \5089 totb2 += decode[5+(ofs)*3]*c; \50905091#define stbir__3_coeff_remnant( ofs ) \5092 c = hc[0+(ofs)]; \5093 tota0 += decode[0+(ofs)*3]*c; \5094 tota1 += decode[1+(ofs)*3]*c; \5095 tota2 += decode[2+(ofs)*3]*c; \5096 c = hc[1+(ofs)]; \5097 totb0 += decode[3+(ofs)*3]*c; \5098 totb1 += decode[4+(ofs)*3]*c; \5099 totb2 += decode[5+(ofs)*3]*c; \5100 c = hc[2+(ofs)]; \5101 totc0 += decode[6+(ofs)*3]*c; \5102 totc1 += decode[7+(ofs)*3]*c; \5103 totc2 += decode[8+(ofs)*3]*c; 51045105#define stbir__store_output() \5106 output[0] = (tota0+totc0)+(totb0+totd0); \5107 output[1] = (tota1+totc1)+(totb1+totd1); \5108 output[2] = (tota2+totc2)+(totb2+totd2); \5109 horizontal_coefficients += coefficient_width; \5110 ++horizontal_contributors; \5111 output += 3;51125113#endif 51145115#define STBIR__horizontal_channels 35116#define STB_IMAGE_RESIZE_DO_HORIZONTALS5117#include STBIR__HEADER_FILENAME51185119//=================5120// Do 4 channel horizontal routines51215122#ifdef STBIR_SIMD51235124#define stbir__1_coeff_only() \5125 stbir__simdf tot,c; \5126 STBIR_SIMD_NO_UNROLL(decode); \5127 stbir__simdf_load1( c, hc ); \5128 stbir__simdf_0123to0000( c, c ); \5129 stbir__simdf_mult_mem( tot, c, decode ); 51305131#define stbir__2_coeff_only() \5132 stbir__simdf tot,c,cs; \5133 STBIR_SIMD_NO_UNROLL(decode); \5134 stbir__simdf_load2( cs, hc ); \5135 stbir__simdf_0123to0000( c, cs ); \5136 stbir__simdf_mult_mem( tot, c, decode ); \5137 stbir__simdf_0123to1111( c, cs ); \5138 stbir__simdf_madd_mem( tot, tot, c, decode+4 ); 51395140#define stbir__3_coeff_only() \5141 stbir__simdf tot,c,cs; \5142 STBIR_SIMD_NO_UNROLL(decode); \5143 stbir__simdf_load( cs, hc ); \5144 stbir__simdf_0123to0000( c, cs ); \5145 stbir__simdf_mult_mem( tot, c, decode ); \5146 stbir__simdf_0123to1111( c, cs ); \5147 stbir__simdf_madd_mem( tot, tot, c, decode+4 ); \5148 stbir__simdf_0123to2222( c, cs ); \5149 stbir__simdf_madd_mem( tot, tot, c, decode+8 ); 51505151#define stbir__store_output_tiny() \5152 stbir__simdf_store( output, tot ); \5153 horizontal_coefficients += coefficient_width; \5154 ++horizontal_contributors; \5155 output += 4;51565157#ifdef STBIR_SIMD851585159#define stbir__4_coeff_start() \5160 stbir__simdf8 tot0,c,cs; stbir__simdf t; \5161 STBIR_SIMD_NO_UNROLL(decode); \5162 stbir__simdf8_load4b( cs, hc ); \5163 stbir__simdf8_0123to00001111( c, cs ); \5164 stbir__simdf8_mult_mem( tot0, c, decode ); \5165 stbir__simdf8_0123to22223333( c, cs ); \5166 stbir__simdf8_madd_mem( tot0, tot0, c, decode+8 ); 51675168#define stbir__4_coeff_continue_from_4( ofs ) \5169 STBIR_SIMD_NO_UNROLL(decode); \5170 stbir__simdf8_load4b( cs, hc + (ofs) ); \5171 stbir__simdf8_0123to00001111( c, cs ); \5172 stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*4 ); \5173 stbir__simdf8_0123to22223333( c, cs ); \5174 stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*4+8 ); 51755176#define stbir__1_coeff_remnant( ofs ) \5177 STBIR_SIMD_NO_UNROLL(decode); \5178 stbir__simdf_load1rep4( t, hc + (ofs) ); \5179 stbir__simdf8_madd_mem4( tot0, tot0, t, decode+(ofs)*4 ); 51805181#define stbir__2_coeff_remnant( ofs ) \5182 STBIR_SIMD_NO_UNROLL(decode); \5183 stbir__simdf8_load4b( cs, hc + (ofs) - 2 ); \5184 stbir__simdf8_0123to22223333( c, cs ); \5185 stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*4 ); 5186 5187 #define stbir__3_coeff_remnant( ofs ) \5188 STBIR_SIMD_NO_UNROLL(decode); \5189 stbir__simdf8_load4b( cs, hc + (ofs) ); \5190 stbir__simdf8_0123to00001111( c, cs ); \5191 stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*4 ); \5192 stbir__simdf8_0123to2222( t, cs ); \5193 stbir__simdf8_madd_mem4( tot0, tot0, t, decode+(ofs)*4+8 ); 51945195#define stbir__store_output() \5196 stbir__simdf8_add4halves( t, stbir__if_simdf8_cast_to_simdf4(tot0), tot0 ); \5197 stbir__simdf_store( output, t ); \5198 horizontal_coefficients += coefficient_width; \5199 ++horizontal_contributors; \5200 output += 4;52015202#else 52035204#define stbir__4_coeff_start() \5205 stbir__simdf tot0,tot1,c,cs; \5206 STBIR_SIMD_NO_UNROLL(decode); \5207 stbir__simdf_load( cs, hc ); \5208 stbir__simdf_0123to0000( c, cs ); \5209 stbir__simdf_mult_mem( tot0, c, decode ); \5210 stbir__simdf_0123to1111( c, cs ); \5211 stbir__simdf_mult_mem( tot1, c, decode+4 ); \5212 stbir__simdf_0123to2222( c, cs ); \5213 stbir__simdf_madd_mem( tot0, tot0, c, decode+8 ); \5214 stbir__simdf_0123to3333( c, cs ); \5215 stbir__simdf_madd_mem( tot1, tot1, c, decode+12 ); 52165217#define stbir__4_coeff_continue_from_4( ofs ) \5218 STBIR_SIMD_NO_UNROLL(decode); \5219 stbir__simdf_load( cs, hc + (ofs) ); \5220 stbir__simdf_0123to0000( c, cs ); \5221 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*4 ); \5222 stbir__simdf_0123to1111( c, cs ); \5223 stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*4+4 ); \5224 stbir__simdf_0123to2222( c, cs ); \5225 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*4+8 ); \5226 stbir__simdf_0123to3333( c, cs ); \5227 stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*4+12 ); 52285229#define stbir__1_coeff_remnant( ofs ) \5230 STBIR_SIMD_NO_UNROLL(decode); \5231 stbir__simdf_load1( c, hc + (ofs) ); \5232 stbir__simdf_0123to0000( c, c ); \5233 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*4 ); 52345235#define stbir__2_coeff_remnant( ofs ) \5236 STBIR_SIMD_NO_UNROLL(decode); \5237 stbir__simdf_load2( cs, hc + (ofs) ); \5238 stbir__simdf_0123to0000( c, cs ); \5239 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*4 ); \5240 stbir__simdf_0123to1111( c, cs ); \5241 stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*4+4 ); 5242 5243#define stbir__3_coeff_remnant( ofs ) \5244 STBIR_SIMD_NO_UNROLL(decode); \5245 stbir__simdf_load( cs, hc + (ofs) ); \5246 stbir__simdf_0123to0000( c, cs ); \5247 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*4 ); \5248 stbir__simdf_0123to1111( c, cs ); \5249 stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*4+4 ); \5250 stbir__simdf_0123to2222( c, cs ); \5251 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*4+8 );52525253#define stbir__store_output() \5254 stbir__simdf_add( tot0, tot0, tot1 ); \5255 stbir__simdf_store( output, tot0 ); \5256 horizontal_coefficients += coefficient_width; \5257 ++horizontal_contributors; \5258 output += 4;52595260#endif52615262#else52635264#define stbir__1_coeff_only() \5265 float p0,p1,p2,p3,c; \5266 STBIR_SIMD_NO_UNROLL(decode); \5267 c = hc[0]; \5268 p0 = decode[0] * c; \5269 p1 = decode[1] * c; \5270 p2 = decode[2] * c; \5271 p3 = decode[3] * c;52725273#define stbir__2_coeff_only() \5274 float p0,p1,p2,p3,c; \5275 STBIR_SIMD_NO_UNROLL(decode); \5276 c = hc[0]; \5277 p0 = decode[0] * c; \5278 p1 = decode[1] * c; \5279 p2 = decode[2] * c; \5280 p3 = decode[3] * c; \5281 c = hc[1]; \5282 p0 += decode[4] * c; \5283 p1 += decode[5] * c; \5284 p2 += decode[6] * c; \5285 p3 += decode[7] * c;52865287#define stbir__3_coeff_only() \5288 float p0,p1,p2,p3,c; \5289 STBIR_SIMD_NO_UNROLL(decode); \5290 c = hc[0]; \5291 p0 = decode[0] * c; \5292 p1 = decode[1] * c; \5293 p2 = decode[2] * c; \5294 p3 = decode[3] * c; \5295 c = hc[1]; \5296 p0 += decode[4] * c; \5297 p1 += decode[5] * c; \5298 p2 += decode[6] * c; \5299 p3 += decode[7] * c; \5300 c = hc[2]; \5301 p0 += decode[8] * c; \5302 p1 += decode[9] * c; \5303 p2 += decode[10] * c; \5304 p3 += decode[11] * c;53055306#define stbir__store_output_tiny() \5307 output[0] = p0; \5308 output[1] = p1; \5309 output[2] = p2; \5310 output[3] = p3; \5311 horizontal_coefficients += coefficient_width; \5312 ++horizontal_contributors; \5313 output += 4;53145315#define stbir__4_coeff_start() \5316 float x0,x1,x2,x3,y0,y1,y2,y3,c; \5317 STBIR_SIMD_NO_UNROLL(decode); \5318 c = hc[0]; \5319 x0 = decode[0] * c; \5320 x1 = decode[1] * c; \5321 x2 = decode[2] * c; \5322 x3 = decode[3] * c; \5323 c = hc[1]; \5324 y0 = decode[4] * c; \5325 y1 = decode[5] * c; \5326 y2 = decode[6] * c; \5327 y3 = decode[7] * c; \5328 c = hc[2]; \5329 x0 += decode[8] * c; \5330 x1 += decode[9] * c; \5331 x2 += decode[10] * c; \5332 x3 += decode[11] * c; \5333 c = hc[3]; \5334 y0 += decode[12] * c; \5335 y1 += decode[13] * c; \5336 y2 += decode[14] * c; \5337 y3 += decode[15] * c;53385339#define stbir__4_coeff_continue_from_4( ofs ) \5340 STBIR_SIMD_NO_UNROLL(decode); \5341 c = hc[0+(ofs)]; \5342 x0 += decode[0+(ofs)*4] * c; \5343 x1 += decode[1+(ofs)*4] * c; \5344 x2 += decode[2+(ofs)*4] * c; \5345 x3 += decode[3+(ofs)*4] * c; \5346 c = hc[1+(ofs)]; \5347 y0 += decode[4+(ofs)*4] * c; \5348 y1 += decode[5+(ofs)*4] * c; \5349 y2 += decode[6+(ofs)*4] * c; \5350 y3 += decode[7+(ofs)*4] * c; \5351 c = hc[2+(ofs)]; \5352 x0 += decode[8+(ofs)*4] * c; \5353 x1 += decode[9+(ofs)*4] * c; \5354 x2 += decode[10+(ofs)*4] * c; \5355 x3 += decode[11+(ofs)*4] * c; \5356 c = hc[3+(ofs)]; \5357 y0 += decode[12+(ofs)*4] * c; \5358 y1 += decode[13+(ofs)*4] * c; \5359 y2 += decode[14+(ofs)*4] * c; \5360 y3 += decode[15+(ofs)*4] * c;53615362#define stbir__1_coeff_remnant( ofs ) \5363 STBIR_SIMD_NO_UNROLL(decode); \5364 c = hc[0+(ofs)]; \5365 x0 += decode[0+(ofs)*4] * c; \5366 x1 += decode[1+(ofs)*4] * c; \5367 x2 += decode[2+(ofs)*4] * c; \5368 x3 += decode[3+(ofs)*4] * c; 53695370#define stbir__2_coeff_remnant( ofs ) \5371 STBIR_SIMD_NO_UNROLL(decode); \5372 c = hc[0+(ofs)]; \5373 x0 += decode[0+(ofs)*4] * c; \5374 x1 += decode[1+(ofs)*4] * c; \5375 x2 += decode[2+(ofs)*4] * c; \5376 x3 += decode[3+(ofs)*4] * c; \5377 c = hc[1+(ofs)]; \5378 y0 += decode[4+(ofs)*4] * c; \5379 y1 += decode[5+(ofs)*4] * c; \5380 y2 += decode[6+(ofs)*4] * c; \5381 y3 += decode[7+(ofs)*4] * c; 5382 5383#define stbir__3_coeff_remnant( ofs ) \5384 STBIR_SIMD_NO_UNROLL(decode); \5385 c = hc[0+(ofs)]; \5386 x0 += decode[0+(ofs)*4] * c; \5387 x1 += decode[1+(ofs)*4] * c; \5388 x2 += decode[2+(ofs)*4] * c; \5389 x3 += decode[3+(ofs)*4] * c; \5390 c = hc[1+(ofs)]; \5391 y0 += decode[4+(ofs)*4] * c; \5392 y1 += decode[5+(ofs)*4] * c; \5393 y2 += decode[6+(ofs)*4] * c; \5394 y3 += decode[7+(ofs)*4] * c; \5395 c = hc[2+(ofs)]; \5396 x0 += decode[8+(ofs)*4] * c; \5397 x1 += decode[9+(ofs)*4] * c; \5398 x2 += decode[10+(ofs)*4] * c; \5399 x3 += decode[11+(ofs)*4] * c; 54005401#define stbir__store_output() \5402 output[0] = x0 + y0; \5403 output[1] = x1 + y1; \5404 output[2] = x2 + y2; \5405 output[3] = x3 + y3; \5406 horizontal_coefficients += coefficient_width; \5407 ++horizontal_contributors; \5408 output += 4;54095410#endif 54115412#define STBIR__horizontal_channels 45413#define STB_IMAGE_RESIZE_DO_HORIZONTALS5414#include STBIR__HEADER_FILENAME5415541654175418//=================5419// Do 7 channel horizontal routines54205421#ifdef STBIR_SIMD54225423#define stbir__1_coeff_only() \5424 stbir__simdf tot0,tot1,c; \5425 STBIR_SIMD_NO_UNROLL(decode); \5426 stbir__simdf_load1( c, hc ); \5427 stbir__simdf_0123to0000( c, c ); \5428 stbir__simdf_mult_mem( tot0, c, decode ); \5429 stbir__simdf_mult_mem( tot1, c, decode+3 ); 54305431#define stbir__2_coeff_only() \5432 stbir__simdf tot0,tot1,c,cs; \5433 STBIR_SIMD_NO_UNROLL(decode); \5434 stbir__simdf_load2( cs, hc ); \5435 stbir__simdf_0123to0000( c, cs ); \5436 stbir__simdf_mult_mem( tot0, c, decode ); \5437 stbir__simdf_mult_mem( tot1, c, decode+3 ); \5438 stbir__simdf_0123to1111( c, cs ); \5439 stbir__simdf_madd_mem( tot0, tot0, c, decode+7 ); \5440 stbir__simdf_madd_mem( tot1, tot1, c,decode+10 ); 54415442#define stbir__3_coeff_only() \5443 stbir__simdf tot0,tot1,c,cs; \5444 STBIR_SIMD_NO_UNROLL(decode); \5445 stbir__simdf_load( cs, hc ); \5446 stbir__simdf_0123to0000( c, cs ); \5447 stbir__simdf_mult_mem( tot0, c, decode ); \5448 stbir__simdf_mult_mem( tot1, c, decode+3 ); \5449 stbir__simdf_0123to1111( c, cs ); \5450 stbir__simdf_madd_mem( tot0, tot0, c, decode+7 ); \5451 stbir__simdf_madd_mem( tot1, tot1, c, decode+10 ); \5452 stbir__simdf_0123to2222( c, cs ); \5453 stbir__simdf_madd_mem( tot0, tot0, c, decode+14 ); \5454 stbir__simdf_madd_mem( tot1, tot1, c, decode+17 ); 54555456#define stbir__store_output_tiny() \5457 stbir__simdf_store( output+3, tot1 ); \5458 stbir__simdf_store( output, tot0 ); \5459 horizontal_coefficients += coefficient_width; \5460 ++horizontal_contributors; \5461 output += 7;54625463#ifdef STBIR_SIMD854645465#define stbir__4_coeff_start() \5466 stbir__simdf8 tot0,tot1,c,cs; \5467 STBIR_SIMD_NO_UNROLL(decode); \5468 stbir__simdf8_load4b( cs, hc ); \5469 stbir__simdf8_0123to00000000( c, cs ); \5470 stbir__simdf8_mult_mem( tot0, c, decode ); \5471 stbir__simdf8_0123to11111111( c, cs ); \5472 stbir__simdf8_mult_mem( tot1, c, decode+7 ); \5473 stbir__simdf8_0123to22222222( c, cs ); \5474 stbir__simdf8_madd_mem( tot0, tot0, c, decode+14 ); \5475 stbir__simdf8_0123to33333333( c, cs ); \5476 stbir__simdf8_madd_mem( tot1, tot1, c, decode+21 ); 54775478#define stbir__4_coeff_continue_from_4( ofs ) \5479 STBIR_SIMD_NO_UNROLL(decode); \5480 stbir__simdf8_load4b( cs, hc + (ofs) ); \5481 stbir__simdf8_0123to00000000( c, cs ); \5482 stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*7 ); \5483 stbir__simdf8_0123to11111111( c, cs ); \5484 stbir__simdf8_madd_mem( tot1, tot1, c, decode+(ofs)*7+7 ); \5485 stbir__simdf8_0123to22222222( c, cs ); \5486 stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*7+14 ); \5487 stbir__simdf8_0123to33333333( c, cs ); \5488 stbir__simdf8_madd_mem( tot1, tot1, c, decode+(ofs)*7+21 ); 54895490#define stbir__1_coeff_remnant( ofs ) \5491 STBIR_SIMD_NO_UNROLL(decode); \5492 stbir__simdf8_load1b( c, hc + (ofs) ); \5493 stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*7 ); 54945495#define stbir__2_coeff_remnant( ofs ) \5496 STBIR_SIMD_NO_UNROLL(decode); \5497 stbir__simdf8_load1b( c, hc + (ofs) ); \5498 stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*7 ); \5499 stbir__simdf8_load1b( c, hc + (ofs)+1 ); \5500 stbir__simdf8_madd_mem( tot1, tot1, c, decode+(ofs)*7+7 ); 55015502#define stbir__3_coeff_remnant( ofs ) \5503 STBIR_SIMD_NO_UNROLL(decode); \5504 stbir__simdf8_load4b( cs, hc + (ofs) ); \5505 stbir__simdf8_0123to00000000( c, cs ); \5506 stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*7 ); \5507 stbir__simdf8_0123to11111111( c, cs ); \5508 stbir__simdf8_madd_mem( tot1, tot1, c, decode+(ofs)*7+7 ); \5509 stbir__simdf8_0123to22222222( c, cs ); \5510 stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*7+14 ); 55115512#define stbir__store_output() \5513 stbir__simdf8_add( tot0, tot0, tot1 ); \5514 horizontal_coefficients += coefficient_width; \5515 ++horizontal_contributors; \5516 output += 7; \5517 if ( output < output_end ) \5518 { \5519 stbir__simdf8_store( output-7, tot0 ); \5520 continue; \5521 } \5522 stbir__simdf_store( output-7+3, stbir__simdf_swiz(stbir__simdf8_gettop4(tot0),0,0,1,2) ); \5523 stbir__simdf_store( output-7, stbir__if_simdf8_cast_to_simdf4(tot0) ); \5524 break;55255526#else55275528#define stbir__4_coeff_start() \5529 stbir__simdf tot0,tot1,tot2,tot3,c,cs; \5530 STBIR_SIMD_NO_UNROLL(decode); \5531 stbir__simdf_load( cs, hc ); \5532 stbir__simdf_0123to0000( c, cs ); \5533 stbir__simdf_mult_mem( tot0, c, decode ); \5534 stbir__simdf_mult_mem( tot1, c, decode+3 ); \5535 stbir__simdf_0123to1111( c, cs ); \5536 stbir__simdf_mult_mem( tot2, c, decode+7 ); \5537 stbir__simdf_mult_mem( tot3, c, decode+10 ); \5538 stbir__simdf_0123to2222( c, cs ); \5539 stbir__simdf_madd_mem( tot0, tot0, c, decode+14 ); \5540 stbir__simdf_madd_mem( tot1, tot1, c, decode+17 ); \5541 stbir__simdf_0123to3333( c, cs ); \5542 stbir__simdf_madd_mem( tot2, tot2, c, decode+21 ); \5543 stbir__simdf_madd_mem( tot3, tot3, c, decode+24 ); 55445545#define stbir__4_coeff_continue_from_4( ofs ) \5546 STBIR_SIMD_NO_UNROLL(decode); \5547 stbir__simdf_load( cs, hc + (ofs) ); \5548 stbir__simdf_0123to0000( c, cs ); \5549 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*7 ); \5550 stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*7+3 ); \5551 stbir__simdf_0123to1111( c, cs ); \5552 stbir__simdf_madd_mem( tot2, tot2, c, decode+(ofs)*7+7 ); \5553 stbir__simdf_madd_mem( tot3, tot3, c, decode+(ofs)*7+10 ); \5554 stbir__simdf_0123to2222( c, cs ); \5555 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*7+14 ); \5556 stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*7+17 ); \5557 stbir__simdf_0123to3333( c, cs ); \5558 stbir__simdf_madd_mem( tot2, tot2, c, decode+(ofs)*7+21 ); \5559 stbir__simdf_madd_mem( tot3, tot3, c, decode+(ofs)*7+24 ); 55605561#define stbir__1_coeff_remnant( ofs ) \5562 STBIR_SIMD_NO_UNROLL(decode); \5563 stbir__simdf_load1( c, hc + (ofs) ); \5564 stbir__simdf_0123to0000( c, c ); \5565 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*7 ); \5566 stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*7+3 ); \55675568#define stbir__2_coeff_remnant( ofs ) \5569 STBIR_SIMD_NO_UNROLL(decode); \5570 stbir__simdf_load2( cs, hc + (ofs) ); \5571 stbir__simdf_0123to0000( c, cs ); \5572 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*7 ); \5573 stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*7+3 ); \5574 stbir__simdf_0123to1111( c, cs ); \5575 stbir__simdf_madd_mem( tot2, tot2, c, decode+(ofs)*7+7 ); \5576 stbir__simdf_madd_mem( tot3, tot3, c, decode+(ofs)*7+10 ); 5577 5578#define stbir__3_coeff_remnant( ofs ) \5579 STBIR_SIMD_NO_UNROLL(decode); \5580 stbir__simdf_load( cs, hc + (ofs) ); \5581 stbir__simdf_0123to0000( c, cs ); \5582 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*7 ); \5583 stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*7+3 ); \5584 stbir__simdf_0123to1111( c, cs ); \5585 stbir__simdf_madd_mem( tot2, tot2, c, decode+(ofs)*7+7 ); \5586 stbir__simdf_madd_mem( tot3, tot3, c, decode+(ofs)*7+10 ); \5587 stbir__simdf_0123to2222( c, cs ); \5588 stbir__simdf_madd_mem( tot0, tot0, c, decode+(ofs)*7+14 ); \5589 stbir__simdf_madd_mem( tot1, tot1, c, decode+(ofs)*7+17 ); 55905591#define stbir__store_output() \5592 stbir__simdf_add( tot0, tot0, tot2 ); \5593 stbir__simdf_add( tot1, tot1, tot3 ); \5594 stbir__simdf_store( output+3, tot1 ); \5595 stbir__simdf_store( output, tot0 ); \5596 horizontal_coefficients += coefficient_width; \5597 ++horizontal_contributors; \5598 output += 7;55995600#endif56015602#else56035604#define stbir__1_coeff_only() \5605 float tot0, tot1, tot2, tot3, tot4, tot5, tot6, c; \5606 c = hc[0]; \5607 tot0 = decode[0]*c; \5608 tot1 = decode[1]*c; \5609 tot2 = decode[2]*c; \5610 tot3 = decode[3]*c; \5611 tot4 = decode[4]*c; \5612 tot5 = decode[5]*c; \5613 tot6 = decode[6]*c; 56145615#define stbir__2_coeff_only() \5616 float tot0, tot1, tot2, tot3, tot4, tot5, tot6, c; \5617 c = hc[0]; \5618 tot0 = decode[0]*c; \5619 tot1 = decode[1]*c; \5620 tot2 = decode[2]*c; \5621 tot3 = decode[3]*c; \5622 tot4 = decode[4]*c; \5623 tot5 = decode[5]*c; \5624 tot6 = decode[6]*c; \5625 c = hc[1]; \5626 tot0 += decode[7]*c; \5627 tot1 += decode[8]*c; \5628 tot2 += decode[9]*c; \5629 tot3 += decode[10]*c; \5630 tot4 += decode[11]*c; \5631 tot5 += decode[12]*c; \5632 tot6 += decode[13]*c; \56335634#define stbir__3_coeff_only() \5635 float tot0, tot1, tot2, tot3, tot4, tot5, tot6, c; \5636 c = hc[0]; \5637 tot0 = decode[0]*c; \5638 tot1 = decode[1]*c; \5639 tot2 = decode[2]*c; \5640 tot3 = decode[3]*c; \5641 tot4 = decode[4]*c; \5642 tot5 = decode[5]*c; \5643 tot6 = decode[6]*c; \5644 c = hc[1]; \5645 tot0 += decode[7]*c; \5646 tot1 += decode[8]*c; \5647 tot2 += decode[9]*c; \5648 tot3 += decode[10]*c; \5649 tot4 += decode[11]*c; \5650 tot5 += decode[12]*c; \5651 tot6 += decode[13]*c; \5652 c = hc[2]; \5653 tot0 += decode[14]*c; \5654 tot1 += decode[15]*c; \5655 tot2 += decode[16]*c; \5656 tot3 += decode[17]*c; \5657 tot4 += decode[18]*c; \5658 tot5 += decode[19]*c; \5659 tot6 += decode[20]*c; \56605661#define stbir__store_output_tiny() \5662 output[0] = tot0; \5663 output[1] = tot1; \5664 output[2] = tot2; \5665 output[3] = tot3; \5666 output[4] = tot4; \5667 output[5] = tot5; \5668 output[6] = tot6; \5669 horizontal_coefficients += coefficient_width; \5670 ++horizontal_contributors; \5671 output += 7;56725673#define stbir__4_coeff_start() \5674 float x0,x1,x2,x3,x4,x5,x6,y0,y1,y2,y3,y4,y5,y6,c; \5675 STBIR_SIMD_NO_UNROLL(decode); \5676 c = hc[0]; \5677 x0 = decode[0] * c; \5678 x1 = decode[1] * c; \5679 x2 = decode[2] * c; \5680 x3 = decode[3] * c; \5681 x4 = decode[4] * c; \5682 x5 = decode[5] * c; \5683 x6 = decode[6] * c; \5684 c = hc[1]; \5685 y0 = decode[7] * c; \5686 y1 = decode[8] * c; \5687 y2 = decode[9] * c; \5688 y3 = decode[10] * c; \5689 y4 = decode[11] * c; \5690 y5 = decode[12] * c; \5691 y6 = decode[13] * c; \5692 c = hc[2]; \5693 x0 += decode[14] * c; \5694 x1 += decode[15] * c; \5695 x2 += decode[16] * c; \5696 x3 += decode[17] * c; \5697 x4 += decode[18] * c; \5698 x5 += decode[19] * c; \5699 x6 += decode[20] * c; \5700 c = hc[3]; \5701 y0 += decode[21] * c; \5702 y1 += decode[22] * c; \5703 y2 += decode[23] * c; \5704 y3 += decode[24] * c; \5705 y4 += decode[25] * c; \5706 y5 += decode[26] * c; \5707 y6 += decode[27] * c; 57085709#define stbir__4_coeff_continue_from_4( ofs ) \5710 STBIR_SIMD_NO_UNROLL(decode); \5711 c = hc[0+(ofs)]; \5712 x0 += decode[0+(ofs)*7] * c; \5713 x1 += decode[1+(ofs)*7] * c; \5714 x2 += decode[2+(ofs)*7] * c; \5715 x3 += decode[3+(ofs)*7] * c; \5716 x4 += decode[4+(ofs)*7] * c; \5717 x5 += decode[5+(ofs)*7] * c; \5718 x6 += decode[6+(ofs)*7] * c; \5719 c = hc[1+(ofs)]; \5720 y0 += decode[7+(ofs)*7] * c; \5721 y1 += decode[8+(ofs)*7] * c; \5722 y2 += decode[9+(ofs)*7] * c; \5723 y3 += decode[10+(ofs)*7] * c; \5724 y4 += decode[11+(ofs)*7] * c; \5725 y5 += decode[12+(ofs)*7] * c; \5726 y6 += decode[13+(ofs)*7] * c; \5727 c = hc[2+(ofs)]; \5728 x0 += decode[14+(ofs)*7] * c; \5729 x1 += decode[15+(ofs)*7] * c; \5730 x2 += decode[16+(ofs)*7] * c; \5731 x3 += decode[17+(ofs)*7] * c; \5732 x4 += decode[18+(ofs)*7] * c; \5733 x5 += decode[19+(ofs)*7] * c; \5734 x6 += decode[20+(ofs)*7] * c; \5735 c = hc[3+(ofs)]; \5736 y0 += decode[21+(ofs)*7] * c; \5737 y1 += decode[22+(ofs)*7] * c; \5738 y2 += decode[23+(ofs)*7] * c; \5739 y3 += decode[24+(ofs)*7] * c; \5740 y4 += decode[25+(ofs)*7] * c; \5741 y5 += decode[26+(ofs)*7] * c; \5742 y6 += decode[27+(ofs)*7] * c; 57435744#define stbir__1_coeff_remnant( ofs ) \5745 STBIR_SIMD_NO_UNROLL(decode); \5746 c = hc[0+(ofs)]; \5747 x0 += decode[0+(ofs)*7] * c; \5748 x1 += decode[1+(ofs)*7] * c; \5749 x2 += decode[2+(ofs)*7] * c; \5750 x3 += decode[3+(ofs)*7] * c; \5751 x4 += decode[4+(ofs)*7] * c; \5752 x5 += decode[5+(ofs)*7] * c; \5753 x6 += decode[6+(ofs)*7] * c; \57545755#define stbir__2_coeff_remnant( ofs ) \5756 STBIR_SIMD_NO_UNROLL(decode); \5757 c = hc[0+(ofs)]; \5758 x0 += decode[0+(ofs)*7] * c; \5759 x1 += decode[1+(ofs)*7] * c; \5760 x2 += decode[2+(ofs)*7] * c; \5761 x3 += decode[3+(ofs)*7] * c; \5762 x4 += decode[4+(ofs)*7] * c; \5763 x5 += decode[5+(ofs)*7] * c; \5764 x6 += decode[6+(ofs)*7] * c; \5765 c = hc[1+(ofs)]; \5766 y0 += decode[7+(ofs)*7] * c; \5767 y1 += decode[8+(ofs)*7] * c; \5768 y2 += decode[9+(ofs)*7] * c; \5769 y3 += decode[10+(ofs)*7] * c; \5770 y4 += decode[11+(ofs)*7] * c; \5771 y5 += decode[12+(ofs)*7] * c; \5772 y6 += decode[13+(ofs)*7] * c; \5773 5774#define stbir__3_coeff_remnant( ofs ) \5775 STBIR_SIMD_NO_UNROLL(decode); \5776 c = hc[0+(ofs)]; \5777 x0 += decode[0+(ofs)*7] * c; \5778 x1 += decode[1+(ofs)*7] * c; \5779 x2 += decode[2+(ofs)*7] * c; \5780 x3 += decode[3+(ofs)*7] * c; \5781 x4 += decode[4+(ofs)*7] * c; \5782 x5 += decode[5+(ofs)*7] * c; \5783 x6 += decode[6+(ofs)*7] * c; \5784 c = hc[1+(ofs)]; \5785 y0 += decode[7+(ofs)*7] * c; \5786 y1 += decode[8+(ofs)*7] * c; \5787 y2 += decode[9+(ofs)*7] * c; \5788 y3 += decode[10+(ofs)*7] * c; \5789 y4 += decode[11+(ofs)*7] * c; \5790 y5 += decode[12+(ofs)*7] * c; \5791 y6 += decode[13+(ofs)*7] * c; \5792 c = hc[2+(ofs)]; \5793 x0 += decode[14+(ofs)*7] * c; \5794 x1 += decode[15+(ofs)*7] * c; \5795 x2 += decode[16+(ofs)*7] * c; \5796 x3 += decode[17+(ofs)*7] * c; \5797 x4 += decode[18+(ofs)*7] * c; \5798 x5 += decode[19+(ofs)*7] * c; \5799 x6 += decode[20+(ofs)*7] * c; \58005801#define stbir__store_output() \5802 output[0] = x0 + y0; \5803 output[1] = x1 + y1; \5804 output[2] = x2 + y2; \5805 output[3] = x3 + y3; \5806 output[4] = x4 + y4; \5807 output[5] = x5 + y5; \5808 output[6] = x6 + y6; \5809 horizontal_coefficients += coefficient_width; \5810 ++horizontal_contributors; \5811 output += 7;58125813#endif 58145815#define STBIR__horizontal_channels 75816#define STB_IMAGE_RESIZE_DO_HORIZONTALS5817#include STBIR__HEADER_FILENAME581858195820// include all of the vertical resamplers (both scatter and gather versions)58215822#define STBIR__vertical_channels 15823#define STB_IMAGE_RESIZE_DO_VERTICALS5824#include STBIR__HEADER_FILENAME58255826#define STBIR__vertical_channels 15827#define STB_IMAGE_RESIZE_DO_VERTICALS5828#define STB_IMAGE_RESIZE_VERTICAL_CONTINUE5829#include STBIR__HEADER_FILENAME58305831#define STBIR__vertical_channels 25832#define STB_IMAGE_RESIZE_DO_VERTICALS5833#include STBIR__HEADER_FILENAME58345835#define STBIR__vertical_channels 25836#define STB_IMAGE_RESIZE_DO_VERTICALS5837#define STB_IMAGE_RESIZE_VERTICAL_CONTINUE5838#include STBIR__HEADER_FILENAME58395840#define STBIR__vertical_channels 35841#define STB_IMAGE_RESIZE_DO_VERTICALS5842#include STBIR__HEADER_FILENAME58435844#define STBIR__vertical_channels 35845#define STB_IMAGE_RESIZE_DO_VERTICALS5846#define STB_IMAGE_RESIZE_VERTICAL_CONTINUE5847#include STBIR__HEADER_FILENAME58485849#define STBIR__vertical_channels 45850#define STB_IMAGE_RESIZE_DO_VERTICALS5851#include STBIR__HEADER_FILENAME58525853#define STBIR__vertical_channels 45854#define STB_IMAGE_RESIZE_DO_VERTICALS5855#define STB_IMAGE_RESIZE_VERTICAL_CONTINUE5856#include STBIR__HEADER_FILENAME58575858#define STBIR__vertical_channels 55859#define STB_IMAGE_RESIZE_DO_VERTICALS5860#include STBIR__HEADER_FILENAME58615862#define STBIR__vertical_channels 55863#define STB_IMAGE_RESIZE_DO_VERTICALS5864#define STB_IMAGE_RESIZE_VERTICAL_CONTINUE5865#include STBIR__HEADER_FILENAME58665867#define STBIR__vertical_channels 65868#define STB_IMAGE_RESIZE_DO_VERTICALS5869#include STBIR__HEADER_FILENAME58705871#define STBIR__vertical_channels 65872#define STB_IMAGE_RESIZE_DO_VERTICALS5873#define STB_IMAGE_RESIZE_VERTICAL_CONTINUE5874#include STBIR__HEADER_FILENAME58755876#define STBIR__vertical_channels 75877#define STB_IMAGE_RESIZE_DO_VERTICALS5878#include STBIR__HEADER_FILENAME58795880#define STBIR__vertical_channels 75881#define STB_IMAGE_RESIZE_DO_VERTICALS5882#define STB_IMAGE_RESIZE_VERTICAL_CONTINUE5883#include STBIR__HEADER_FILENAME58845885#define STBIR__vertical_channels 85886#define STB_IMAGE_RESIZE_DO_VERTICALS5887#include STBIR__HEADER_FILENAME58885889#define STBIR__vertical_channels 85890#define STB_IMAGE_RESIZE_DO_VERTICALS5891#define STB_IMAGE_RESIZE_VERTICAL_CONTINUE5892#include STBIR__HEADER_FILENAME58935894typedef void STBIR_VERTICAL_GATHERFUNC( float * output, float const * coeffs, float const ** inputs, float const * input0_end );58955896static STBIR_VERTICAL_GATHERFUNC * stbir__vertical_gathers[ 8 ] =5897{5898 stbir__vertical_gather_with_1_coeffs,stbir__vertical_gather_with_2_coeffs,stbir__vertical_gather_with_3_coeffs,stbir__vertical_gather_with_4_coeffs,stbir__vertical_gather_with_5_coeffs,stbir__vertical_gather_with_6_coeffs,stbir__vertical_gather_with_7_coeffs,stbir__vertical_gather_with_8_coeffs5899};59005901static STBIR_VERTICAL_GATHERFUNC * stbir__vertical_gathers_continues[ 8 ] =5902{5903 stbir__vertical_gather_with_1_coeffs_cont,stbir__vertical_gather_with_2_coeffs_cont,stbir__vertical_gather_with_3_coeffs_cont,stbir__vertical_gather_with_4_coeffs_cont,stbir__vertical_gather_with_5_coeffs_cont,stbir__vertical_gather_with_6_coeffs_cont,stbir__vertical_gather_with_7_coeffs_cont,stbir__vertical_gather_with_8_coeffs_cont5904};59055906typedef void STBIR_VERTICAL_SCATTERFUNC( float ** outputs, float const * coeffs, float const * input, float const * input_end );59075908static STBIR_VERTICAL_SCATTERFUNC * stbir__vertical_scatter_sets[ 8 ] =5909{5910 stbir__vertical_scatter_with_1_coeffs,stbir__vertical_scatter_with_2_coeffs,stbir__vertical_scatter_with_3_coeffs,stbir__vertical_scatter_with_4_coeffs,stbir__vertical_scatter_with_5_coeffs,stbir__vertical_scatter_with_6_coeffs,stbir__vertical_scatter_with_7_coeffs,stbir__vertical_scatter_with_8_coeffs5911};59125913static STBIR_VERTICAL_SCATTERFUNC * stbir__vertical_scatter_blends[ 8 ] =5914{5915 stbir__vertical_scatter_with_1_coeffs_cont,stbir__vertical_scatter_with_2_coeffs_cont,stbir__vertical_scatter_with_3_coeffs_cont,stbir__vertical_scatter_with_4_coeffs_cont,stbir__vertical_scatter_with_5_coeffs_cont,stbir__vertical_scatter_with_6_coeffs_cont,stbir__vertical_scatter_with_7_coeffs_cont,stbir__vertical_scatter_with_8_coeffs_cont5916};591759185919static void stbir__encode_scanline( stbir__info const * stbir_info, void *output_buffer_data, float * encode_buffer, int row STBIR_ONLY_PROFILE_GET_SPLIT_INFO )5920{5921 int num_pixels = stbir_info->horizontal.scale_info.output_sub_size;5922 int channels = stbir_info->channels;5923 int width_times_channels = num_pixels * channels;5924 void * output_buffer;59255926 // un-alpha weight if we need to5927 if ( stbir_info->alpha_unweight )5928 {5929 STBIR_PROFILE_START( unalpha );5930 stbir_info->alpha_unweight( encode_buffer, width_times_channels );5931 STBIR_PROFILE_END( unalpha );5932 }59335934 // write directly into output by default5935 output_buffer = output_buffer_data;59365937 // if we have an output callback, we first convert the decode buffer in place (and then hand that to the callback)5938 if ( stbir_info->out_pixels_cb )5939 output_buffer = encode_buffer;5940 5941 STBIR_PROFILE_START( encode );5942 // convert into the output buffer5943 stbir_info->encode_pixels( output_buffer, width_times_channels, encode_buffer );5944 STBIR_PROFILE_END( encode );59455946 // if we have an output callback, call it to send the data5947 if ( stbir_info->out_pixels_cb )5948 stbir_info->out_pixels_cb( output_buffer_data, num_pixels, row, stbir_info->user_data );5949}595059515952// Get the ring buffer pointer for an index5953static float* stbir__get_ring_buffer_entry(stbir__info const * stbir_info, stbir__per_split_info const * split_info, int index )5954{5955 STBIR_ASSERT( index < stbir_info->ring_buffer_num_entries );59565957 #ifdef STBIR__SEPARATE_ALLOCATIONS5958 return split_info->ring_buffers[ index ];5959 #else5960 return (float*) ( ( (char*) split_info->ring_buffer ) + ( index * stbir_info->ring_buffer_length_bytes ) );5961 #endif5962}59635964// Get the specified scan line from the ring buffer5965static float* stbir__get_ring_buffer_scanline(stbir__info const * stbir_info, stbir__per_split_info const * split_info, int get_scanline)5966{5967 int ring_buffer_index = (split_info->ring_buffer_begin_index + (get_scanline - split_info->ring_buffer_first_scanline)) % stbir_info->ring_buffer_num_entries;5968 return stbir__get_ring_buffer_entry( stbir_info, split_info, ring_buffer_index );5969}59705971static void stbir__resample_horizontal_gather(stbir__info const * stbir_info, float* output_buffer, float const * input_buffer STBIR_ONLY_PROFILE_GET_SPLIT_INFO )5972{5973 float const * decode_buffer = input_buffer - ( stbir_info->scanline_extents.conservative.n0 * stbir_info->effective_channels );59745975 STBIR_PROFILE_START( horizontal );5976 if ( ( stbir_info->horizontal.filter_enum == STBIR_FILTER_POINT_SAMPLE ) && ( stbir_info->horizontal.scale_info.scale == 1.0f ) )5977 STBIR_MEMCPY( output_buffer, input_buffer, stbir_info->horizontal.scale_info.output_sub_size * sizeof( float ) * stbir_info->effective_channels );5978 else5979 stbir_info->horizontal_gather_channels( output_buffer, stbir_info->horizontal.scale_info.output_sub_size, decode_buffer, stbir_info->horizontal.contributors, stbir_info->horizontal.coefficients, stbir_info->horizontal.coefficient_width );5980 STBIR_PROFILE_END( horizontal );5981}59825983static void stbir__resample_vertical_gather(stbir__info const * stbir_info, stbir__per_split_info* split_info, int n, int contrib_n0, int contrib_n1, float const * vertical_coefficients )5984{5985 float* encode_buffer = split_info->vertical_buffer;5986 float* decode_buffer = split_info->decode_buffer;5987 int vertical_first = stbir_info->vertical_first;5988 int width = (vertical_first) ? ( stbir_info->scanline_extents.conservative.n1-stbir_info->scanline_extents.conservative.n0+1 ) : stbir_info->horizontal.scale_info.output_sub_size;5989 int width_times_channels = stbir_info->effective_channels * width;59905991 STBIR_ASSERT( stbir_info->vertical.is_gather );59925993 // loop over the contributing scanlines and scale into the buffer5994 STBIR_PROFILE_START( vertical );5995 {5996 int k = 0, total = contrib_n1 - contrib_n0 + 1;5997 STBIR_ASSERT( total > 0 );5998 do {5999 float const * inputs[8];6000 int i, cnt = total; if ( cnt > 8 ) cnt = 8;6001 for( i = 0 ; i < cnt ; i++ )6002 inputs[ i ] = stbir__get_ring_buffer_scanline(stbir_info, split_info, k+i+contrib_n0 );60036004 // call the N scanlines at a time function (up to 8 scanlines of blending at once)6005 ((k==0)?stbir__vertical_gathers:stbir__vertical_gathers_continues)[cnt-1]( (vertical_first) ? decode_buffer : encode_buffer, vertical_coefficients + k, inputs, inputs[0] + width_times_channels );6006 k += cnt;6007 total -= cnt;6008 } while ( total );6009 }6010 STBIR_PROFILE_END( vertical );60116012 if ( vertical_first )6013 {6014 // Now resample the gathered vertical data in the horizontal axis into the encode buffer6015 stbir__resample_horizontal_gather(stbir_info, encode_buffer, decode_buffer STBIR_ONLY_PROFILE_SET_SPLIT_INFO );6016 }60176018 stbir__encode_scanline( stbir_info, ( (char *) stbir_info->output_data ) + ((ptrdiff_t)n * (ptrdiff_t)stbir_info->output_stride_bytes), 6019 encode_buffer, n STBIR_ONLY_PROFILE_SET_SPLIT_INFO );6020}60216022static void stbir__decode_and_resample_for_vertical_gather_loop(stbir__info const * stbir_info, stbir__per_split_info* split_info, int n)6023{6024 int ring_buffer_index;6025 float* ring_buffer;60266027 // Decode the nth scanline from the source image into the decode buffer.6028 stbir__decode_scanline( stbir_info, n, split_info->decode_buffer STBIR_ONLY_PROFILE_SET_SPLIT_INFO );60296030 // update new end scanline6031 split_info->ring_buffer_last_scanline = n;60326033 // get ring buffer 6034 ring_buffer_index = (split_info->ring_buffer_begin_index + (split_info->ring_buffer_last_scanline - split_info->ring_buffer_first_scanline)) % stbir_info->ring_buffer_num_entries;6035 ring_buffer = stbir__get_ring_buffer_entry(stbir_info, split_info, ring_buffer_index);60366037 // Now resample it into the ring buffer.6038 stbir__resample_horizontal_gather( stbir_info, ring_buffer, split_info->decode_buffer STBIR_ONLY_PROFILE_SET_SPLIT_INFO );60396040 // Now it's sitting in the ring buffer ready to be used as source for the vertical sampling.6041}60426043static void stbir__vertical_gather_loop( stbir__info const * stbir_info, stbir__per_split_info* split_info, int split_count )6044{6045 int y, start_output_y, end_output_y;6046 stbir__contributors* vertical_contributors = stbir_info->vertical.contributors;6047 float const * vertical_coefficients = stbir_info->vertical.coefficients;60486049 STBIR_ASSERT( stbir_info->vertical.is_gather );60506051 start_output_y = split_info->start_output_y;6052 end_output_y = split_info[split_count-1].end_output_y;60536054 vertical_contributors += start_output_y;6055 vertical_coefficients += start_output_y * stbir_info->vertical.coefficient_width;60566057 // initialize the ring buffer for gathering6058 split_info->ring_buffer_begin_index = 0;6059 split_info->ring_buffer_first_scanline = stbir_info->vertical.extent_info.lowest; 6060 split_info->ring_buffer_last_scanline = split_info->ring_buffer_first_scanline - 1; // means "empty"60616062 for (y = start_output_y; y < end_output_y; y++)6063 {6064 int in_first_scanline, in_last_scanline;60656066 in_first_scanline = vertical_contributors->n0;6067 in_last_scanline = vertical_contributors->n1;60686069 // make sure the indexing hasn't broken6070 STBIR_ASSERT( in_first_scanline >= split_info->ring_buffer_first_scanline );60716072 // Load in new scanlines6073 while (in_last_scanline > split_info->ring_buffer_last_scanline)6074 {6075 STBIR_ASSERT( ( split_info->ring_buffer_last_scanline - split_info->ring_buffer_first_scanline + 1 ) <= stbir_info->ring_buffer_num_entries );60766077 // make sure there was room in the ring buffer when we add new scanlines6078 if ( ( split_info->ring_buffer_last_scanline - split_info->ring_buffer_first_scanline + 1 ) == stbir_info->ring_buffer_num_entries )6079 {6080 split_info->ring_buffer_first_scanline++;6081 split_info->ring_buffer_begin_index++;6082 }6083 6084 if ( stbir_info->vertical_first )6085 {6086 float * ring_buffer = stbir__get_ring_buffer_scanline( stbir_info, split_info, ++split_info->ring_buffer_last_scanline );6087 // Decode the nth scanline from the source image into the decode buffer.6088 stbir__decode_scanline( stbir_info, split_info->ring_buffer_last_scanline, ring_buffer STBIR_ONLY_PROFILE_SET_SPLIT_INFO ); 6089 }6090 else6091 {6092 stbir__decode_and_resample_for_vertical_gather_loop(stbir_info, split_info, split_info->ring_buffer_last_scanline + 1);6093 }6094 }60956096 // Now all buffers should be ready to write a row of vertical sampling, so do it.6097 stbir__resample_vertical_gather(stbir_info, split_info, y, in_first_scanline, in_last_scanline, vertical_coefficients );60986099 ++vertical_contributors;6100 vertical_coefficients += stbir_info->vertical.coefficient_width;6101 }6102}61036104#define STBIR__FLOAT_EMPTY_MARKER 3.0e+38F6105#define STBIR__FLOAT_BUFFER_IS_EMPTY(ptr) ((ptr)[0]==STBIR__FLOAT_EMPTY_MARKER)61066107static void stbir__encode_first_scanline_from_scatter(stbir__info const * stbir_info, stbir__per_split_info* split_info)6108{6109 // evict a scanline out into the output buffer6110 float* ring_buffer_entry = stbir__get_ring_buffer_entry(stbir_info, split_info, split_info->ring_buffer_begin_index );6111 6112 // dump the scanline out6113 stbir__encode_scanline( stbir_info, ( (char *)stbir_info->output_data ) + ( (ptrdiff_t)split_info->ring_buffer_first_scanline * (ptrdiff_t)stbir_info->output_stride_bytes ), ring_buffer_entry, split_info->ring_buffer_first_scanline STBIR_ONLY_PROFILE_SET_SPLIT_INFO );6114 6115 // mark it as empty6116 ring_buffer_entry[ 0 ] = STBIR__FLOAT_EMPTY_MARKER;61176118 // advance the first scanline6119 split_info->ring_buffer_first_scanline++;6120 if ( ++split_info->ring_buffer_begin_index == stbir_info->ring_buffer_num_entries )6121 split_info->ring_buffer_begin_index = 0;6122}61236124static void stbir__horizontal_resample_and_encode_first_scanline_from_scatter(stbir__info const * stbir_info, stbir__per_split_info* split_info)6125{6126 // evict a scanline out into the output buffer61276128 float* ring_buffer_entry = stbir__get_ring_buffer_entry(stbir_info, split_info, split_info->ring_buffer_begin_index );61296130 // Now resample it into the buffer.6131 stbir__resample_horizontal_gather( stbir_info, split_info->vertical_buffer, ring_buffer_entry STBIR_ONLY_PROFILE_SET_SPLIT_INFO );6132 6133 // dump the scanline out6134 stbir__encode_scanline( stbir_info, ( (char *)stbir_info->output_data ) + ( (ptrdiff_t)split_info->ring_buffer_first_scanline * (ptrdiff_t)stbir_info->output_stride_bytes ), split_info->vertical_buffer, split_info->ring_buffer_first_scanline STBIR_ONLY_PROFILE_SET_SPLIT_INFO );6135 6136 // mark it as empty6137 ring_buffer_entry[ 0 ] = STBIR__FLOAT_EMPTY_MARKER;61386139 // advance the first scanline6140 split_info->ring_buffer_first_scanline++;6141 if ( ++split_info->ring_buffer_begin_index == stbir_info->ring_buffer_num_entries )6142 split_info->ring_buffer_begin_index = 0;6143}61446145static void stbir__resample_vertical_scatter(stbir__info const * stbir_info, stbir__per_split_info* split_info, int n0, int n1, float const * vertical_coefficients, float const * vertical_buffer, float const * vertical_buffer_end )6146{6147 STBIR_ASSERT( !stbir_info->vertical.is_gather );61486149 STBIR_PROFILE_START( vertical );6150 {6151 int k = 0, total = n1 - n0 + 1;6152 STBIR_ASSERT( total > 0 );6153 do {6154 float * outputs[8];6155 int i, n = total; if ( n > 8 ) n = 8;6156 for( i = 0 ; i < n ; i++ )6157 {6158 outputs[ i ] = stbir__get_ring_buffer_scanline(stbir_info, split_info, k+i+n0 );6159 if ( ( i ) && ( STBIR__FLOAT_BUFFER_IS_EMPTY( outputs[i] ) != STBIR__FLOAT_BUFFER_IS_EMPTY( outputs[0] ) ) ) // make sure runs are of the same type6160 {6161 n = i;6162 break;6163 }6164 }6165 // call the scatter to N scanlines at a time function (up to 8 scanlines of scattering at once)6166 ((STBIR__FLOAT_BUFFER_IS_EMPTY( outputs[0] ))?stbir__vertical_scatter_sets:stbir__vertical_scatter_blends)[n-1]( outputs, vertical_coefficients + k, vertical_buffer, vertical_buffer_end );6167 k += n;6168 total -= n;6169 } while ( total );6170 }61716172 STBIR_PROFILE_END( vertical );6173}61746175typedef void stbir__handle_scanline_for_scatter_func(stbir__info const * stbir_info, stbir__per_split_info* split_info); 61766177static void stbir__vertical_scatter_loop( stbir__info const * stbir_info, stbir__per_split_info* split_info, int split_count )6178{6179 int y, start_output_y, end_output_y, start_input_y, end_input_y;6180 stbir__contributors* vertical_contributors = stbir_info->vertical.contributors;6181 float const * vertical_coefficients = stbir_info->vertical.coefficients;6182 stbir__handle_scanline_for_scatter_func * handle_scanline_for_scatter;6183 void * scanline_scatter_buffer;6184 void * scanline_scatter_buffer_end;6185 int on_first_input_y, last_input_y;61866187 STBIR_ASSERT( !stbir_info->vertical.is_gather );61886189 start_output_y = split_info->start_output_y;6190 end_output_y = split_info[split_count-1].end_output_y; // may do multiple split counts61916192 start_input_y = split_info->start_input_y;6193 end_input_y = split_info[split_count-1].end_input_y;61946195 // adjust for starting offset start_input_y6196 y = start_input_y + stbir_info->vertical.filter_pixel_margin; 6197 vertical_contributors += y ;6198 vertical_coefficients += stbir_info->vertical.coefficient_width * y;61996200 if ( stbir_info->vertical_first )6201 {6202 handle_scanline_for_scatter = stbir__horizontal_resample_and_encode_first_scanline_from_scatter;6203 scanline_scatter_buffer = split_info->decode_buffer;6204 scanline_scatter_buffer_end = ( (char*) scanline_scatter_buffer ) + sizeof( float ) * stbir_info->effective_channels * (stbir_info->scanline_extents.conservative.n1-stbir_info->scanline_extents.conservative.n0+1);6205 }6206 else6207 {6208 handle_scanline_for_scatter = stbir__encode_first_scanline_from_scatter;6209 scanline_scatter_buffer = split_info->vertical_buffer;6210 scanline_scatter_buffer_end = ( (char*) scanline_scatter_buffer ) + sizeof( float ) * stbir_info->effective_channels * stbir_info->horizontal.scale_info.output_sub_size;6211 }62126213 // initialize the ring buffer for scattering6214 split_info->ring_buffer_first_scanline = start_output_y;6215 split_info->ring_buffer_last_scanline = -1;6216 split_info->ring_buffer_begin_index = -1;62176218 // mark all the buffers as empty to start6219 for( y = 0 ; y < stbir_info->ring_buffer_num_entries ; y++ )6220 stbir__get_ring_buffer_entry( stbir_info, split_info, y )[0] = STBIR__FLOAT_EMPTY_MARKER; // only used on scatter62216222 // do the loop in input space6223 on_first_input_y = 1; last_input_y = start_input_y;6224 for (y = start_input_y ; y < end_input_y; y++)6225 {6226 int out_first_scanline, out_last_scanline;62276228 out_first_scanline = vertical_contributors->n0;6229 out_last_scanline = vertical_contributors->n1;62306231 STBIR_ASSERT(out_last_scanline - out_first_scanline + 1 <= stbir_info->ring_buffer_num_entries);62326233 if ( ( out_last_scanline >= out_first_scanline ) && ( ( ( out_first_scanline >= start_output_y ) && ( out_first_scanline < end_output_y ) ) || ( ( out_last_scanline >= start_output_y ) && ( out_last_scanline < end_output_y ) ) ) )6234 {6235 float const * vc = vertical_coefficients;62366237 // keep track of the range actually seen for the next resize6238 last_input_y = y;6239 if ( ( on_first_input_y ) && ( y > start_input_y ) )6240 split_info->start_input_y = y;6241 on_first_input_y = 0;62426243 // clip the region 6244 if ( out_first_scanline < start_output_y )6245 {6246 vc += start_output_y - out_first_scanline;6247 out_first_scanline = start_output_y;6248 }62496250 if ( out_last_scanline >= end_output_y )6251 out_last_scanline = end_output_y - 1;62526253 // if very first scanline, init the index6254 if (split_info->ring_buffer_begin_index < 0)6255 split_info->ring_buffer_begin_index = out_first_scanline - start_output_y;6256 6257 STBIR_ASSERT( split_info->ring_buffer_begin_index <= out_first_scanline );62586259 // Decode the nth scanline from the source image into the decode buffer.6260 stbir__decode_scanline( stbir_info, y, split_info->decode_buffer STBIR_ONLY_PROFILE_SET_SPLIT_INFO ); 62616262 // When horizontal first, we resample horizontally into the vertical buffer before we scatter it out6263 if ( !stbir_info->vertical_first )6264 stbir__resample_horizontal_gather( stbir_info, split_info->vertical_buffer, split_info->decode_buffer STBIR_ONLY_PROFILE_SET_SPLIT_INFO );62656266 // Now it's sitting in the buffer ready to be distributed into the ring buffers.62676268 // evict from the ringbuffer, if we need are full6269 if ( ( ( split_info->ring_buffer_last_scanline - split_info->ring_buffer_first_scanline + 1 ) == stbir_info->ring_buffer_num_entries ) &&6270 ( out_last_scanline > split_info->ring_buffer_last_scanline ) )6271 handle_scanline_for_scatter( stbir_info, split_info );6272 6273 // Now the horizontal buffer is ready to write to all ring buffer rows, so do it.6274 stbir__resample_vertical_scatter(stbir_info, split_info, out_first_scanline, out_last_scanline, vc, (float*)scanline_scatter_buffer, (float*)scanline_scatter_buffer_end );62756276 // update the end of the buffer6277 if ( out_last_scanline > split_info->ring_buffer_last_scanline )6278 split_info->ring_buffer_last_scanline = out_last_scanline;6279 }6280 ++vertical_contributors;6281 vertical_coefficients += stbir_info->vertical.coefficient_width;6282 }62836284 // now evict the scanlines that are left over in the ring buffer6285 while ( split_info->ring_buffer_first_scanline < end_output_y )6286 handle_scanline_for_scatter(stbir_info, split_info);62876288 // update the end_input_y if we do multiple resizes with the same data6289 ++last_input_y;6290 for( y = 0 ; y < split_count; y++ )6291 if ( split_info[y].end_input_y > last_input_y )6292 split_info[y].end_input_y = last_input_y;6293}629462956296static stbir__kernel_callback * stbir__builtin_kernels[] = { 0, stbir__filter_trapezoid, stbir__filter_triangle, stbir__filter_cubic, stbir__filter_catmullrom, stbir__filter_mitchell, stbir__filter_point };6297static stbir__support_callback * stbir__builtin_supports[] = { 0, stbir__support_trapezoid, stbir__support_one, stbir__support_two, stbir__support_two, stbir__support_two, stbir__support_zeropoint5 };62986299static void stbir__set_sampler(stbir__sampler * samp, stbir_filter filter, stbir__kernel_callback * kernel, stbir__support_callback * support, stbir_edge edge, stbir__scale_info * scale_info, int always_gather, void * user_data )6300{6301 // set filter6302 if (filter == 0)6303 {6304 filter = STBIR_DEFAULT_FILTER_DOWNSAMPLE; // default to downsample6305 if (scale_info->scale >= ( 1.0f - stbir__small_float ) )6306 {6307 if ( (scale_info->scale <= ( 1.0f + stbir__small_float ) ) && ( STBIR_CEILF(scale_info->pixel_shift) == scale_info->pixel_shift ) )6308 filter = STBIR_FILTER_POINT_SAMPLE; 6309 else6310 filter = STBIR_DEFAULT_FILTER_UPSAMPLE;6311 }6312 }6313 samp->filter_enum = filter;63146315 STBIR_ASSERT(samp->filter_enum != 0);6316 STBIR_ASSERT((unsigned)samp->filter_enum < STBIR_FILTER_OTHER); 6317 samp->filter_kernel = stbir__builtin_kernels[ filter ];6318 samp->filter_support = stbir__builtin_supports[ filter ];63196320 if ( kernel && support )6321 {6322 samp->filter_kernel = kernel;6323 samp->filter_support = support;6324 samp->filter_enum = STBIR_FILTER_OTHER;6325 }63266327 samp->edge = edge;6328 samp->filter_pixel_width = stbir__get_filter_pixel_width (samp->filter_support, scale_info->scale, user_data );6329 // Gather is always better, but in extreme downsamples, you have to most or all of the data in memory6330 // For horizontal, we always have all the pixels, so we always use gather here (always_gather==1).6331 // For vertical, we use gather if scaling up (which means we will have samp->filter_pixel_width6332 // scanlines in memory at once).6333 samp->is_gather = 0;6334 if ( scale_info->scale >= ( 1.0f - stbir__small_float ) )6335 samp->is_gather = 1;6336 else if ( ( always_gather ) || ( samp->filter_pixel_width <= STBIR_FORCE_GATHER_FILTER_SCANLINES_AMOUNT ) )6337 samp->is_gather = 2;63386339 // pre calculate stuff based on the above6340 samp->coefficient_width = stbir__get_coefficient_width(samp, samp->is_gather, user_data);63416342 if ( edge == STBIR_EDGE_WRAP )6343 if ( samp->filter_pixel_width > ( scale_info->input_full_size * 2 ) ) // this can only happen when shrinking to a single pixel6344 samp->filter_pixel_width = scale_info->input_full_size * 2;63456346 // This is how much to expand buffers to account for filters seeking outside6347 // the image boundaries.6348 samp->filter_pixel_margin = samp->filter_pixel_width / 2;63496350 samp->num_contributors = stbir__get_contributors(samp, samp->is_gather);6351 samp->contributors_size = samp->num_contributors * sizeof(stbir__contributors);6352 samp->coefficients_size = samp->num_contributors * samp->coefficient_width * sizeof(float) + sizeof(float); // extra sizeof(float) is padding63536354 samp->gather_prescatter_contributors = 0;6355 samp->gather_prescatter_coefficients = 0;6356 if ( samp->is_gather == 0 )6357 {6358 samp->gather_prescatter_coefficient_width = samp->filter_pixel_width;6359 samp->gather_prescatter_num_contributors = stbir__get_contributors(samp, 2);6360 samp->gather_prescatter_contributors_size = samp->gather_prescatter_num_contributors * sizeof(stbir__contributors);6361 samp->gather_prescatter_coefficients_size = samp->gather_prescatter_num_contributors * samp->gather_prescatter_coefficient_width * sizeof(float);6362 }6363}63646365static void stbir__get_conservative_extents( stbir__sampler * samp, stbir__contributors * range, void * user_data )6366{6367 float scale = samp->scale_info.scale;6368 float out_shift = samp->scale_info.pixel_shift;6369 stbir__support_callback * support = samp->filter_support;6370 int input_full_size = samp->scale_info.input_full_size;6371 stbir_edge edge = samp->edge;6372 float inv_scale = samp->scale_info.inv_scale;63736374 STBIR_ASSERT( samp->is_gather != 0 );63756376 if ( samp->is_gather == 1 )6377 {6378 int in_first_pixel, in_last_pixel;6379 float out_filter_radius = support(inv_scale, user_data) * scale;63806381 stbir__calculate_in_pixel_range( &in_first_pixel, &in_last_pixel, 0.5, out_filter_radius, inv_scale, out_shift, input_full_size, edge );6382 range->n0 = in_first_pixel;6383 stbir__calculate_in_pixel_range( &in_first_pixel, &in_last_pixel, ( (float)(samp->scale_info.output_sub_size-1) ) + 0.5f, out_filter_radius, inv_scale, out_shift, input_full_size, edge );6384 range->n1 = in_last_pixel;6385 }6386 else if ( samp->is_gather == 2 ) // downsample gather, refine6387 {6388 float in_pixels_radius = support(scale, user_data) * inv_scale;6389 int filter_pixel_margin = samp->filter_pixel_margin;6390 int output_sub_size = samp->scale_info.output_sub_size;6391 int input_end;6392 int n;6393 int in_first_pixel, in_last_pixel;63946395 // get a conservative area of the input range6396 stbir__calculate_in_pixel_range( &in_first_pixel, &in_last_pixel, 0, 0, inv_scale, out_shift, input_full_size, edge );6397 range->n0 = in_first_pixel;6398 stbir__calculate_in_pixel_range( &in_first_pixel, &in_last_pixel, (float)output_sub_size, 0, inv_scale, out_shift, input_full_size, edge );6399 range->n1 = in_last_pixel;6400 6401 // now go through the margin to the start of area to find bottom 6402 n = range->n0 + 1;6403 input_end = -filter_pixel_margin;6404 while( n >= input_end )6405 {6406 int out_first_pixel, out_last_pixel;6407 stbir__calculate_out_pixel_range( &out_first_pixel, &out_last_pixel, ((float)n)+0.5f, in_pixels_radius, scale, out_shift, output_sub_size );6408 if ( out_first_pixel > out_last_pixel )6409 break;64106411 if ( ( out_first_pixel < output_sub_size ) || ( out_last_pixel >= 0 ) )6412 range->n0 = n;6413 --n;6414 }64156416 // now go through the end of the area through the margin to find top 6417 n = range->n1 - 1;6418 input_end = n + 1 + filter_pixel_margin;6419 while( n <= input_end )6420 {6421 int out_first_pixel, out_last_pixel;6422 stbir__calculate_out_pixel_range( &out_first_pixel, &out_last_pixel, ((float)n)+0.5f, in_pixels_radius, scale, out_shift, output_sub_size );6423 if ( out_first_pixel > out_last_pixel )6424 break;6425 if ( ( out_first_pixel < output_sub_size ) || ( out_last_pixel >= 0 ) )6426 range->n1 = n;6427 ++n;6428 }6429 }64306431 if ( samp->edge == STBIR_EDGE_WRAP )6432 {6433 // if we are wrapping, and we are very close to the image size (so the edges might merge), just use the scanline up to the edge6434 if ( ( range->n0 > 0 ) && ( range->n1 >= input_full_size ) )6435 {6436 int marg = range->n1 - input_full_size + 1;6437 if ( ( marg + STBIR__MERGE_RUNS_PIXEL_THRESHOLD ) >= range->n0 )6438 range->n0 = 0;6439 }6440 if ( ( range->n0 < 0 ) && ( range->n1 < (input_full_size-1) ) )6441 {6442 int marg = -range->n0;6443 if ( ( input_full_size - marg - STBIR__MERGE_RUNS_PIXEL_THRESHOLD - 1 ) <= range->n1 )6444 range->n1 = input_full_size - 1;6445 }6446 }6447 else6448 {6449 // for non-edge-wrap modes, we never read over the edge, so clamp6450 if ( range->n0 < 0 )6451 range->n0 = 0;6452 if ( range->n1 >= input_full_size )6453 range->n1 = input_full_size - 1;6454 }6455}64566457static void stbir__get_split_info( stbir__per_split_info* split_info, int splits, int output_height, int vertical_pixel_margin, int input_full_height )6458{6459 int i, cur;6460 int left = output_height;64616462 cur = 0;6463 for( i = 0 ; i < splits ; i++ )6464 {6465 int each; 6466 split_info[i].start_output_y = cur;6467 each = left / ( splits - i );6468 split_info[i].end_output_y = cur + each;6469 cur += each;6470 left -= each;64716472 // scatter range (updated to minimum as you run it)6473 split_info[i].start_input_y = -vertical_pixel_margin;6474 split_info[i].end_input_y = input_full_height + vertical_pixel_margin;6475 }6476}64776478static void stbir__free_internal_mem( stbir__info *info )6479{6480 #define STBIR__FREE_AND_CLEAR( ptr ) { if ( ptr ) { void * p = (ptr); (ptr) = 0; STBIR_FREE( p, info->user_data); } }6481 6482 if ( info )6483 {6484 #ifndef STBIR__SEPARATE_ALLOCATIONS6485 STBIR__FREE_AND_CLEAR( info->alloced_mem );6486 #else6487 int i,j;64886489 if ( ( info->vertical.gather_prescatter_contributors ) && ( (void*)info->vertical.gather_prescatter_contributors != (void*)info->split_info[0].decode_buffer ) )6490 {6491 STBIR__FREE_AND_CLEAR( info->vertical.gather_prescatter_coefficients );6492 STBIR__FREE_AND_CLEAR( info->vertical.gather_prescatter_contributors );6493 }6494 for( i = 0 ; i < info->splits ; i++ )6495 {6496 for( j = 0 ; j < info->alloc_ring_buffer_num_entries ; j++ )6497 {6498 #ifdef STBIR_SIMD86499 if ( info->effective_channels == 3 ) 6500 --info->split_info[i].ring_buffers[j]; // avx in 3 channel mode needs one float at the start of the buffer6501 #endif 6502 STBIR__FREE_AND_CLEAR( info->split_info[i].ring_buffers[j] );6503 }65046505 #ifdef STBIR_SIMD86506 if ( info->effective_channels == 3 ) 6507 --info->split_info[i].decode_buffer; // avx in 3 channel mode needs one float at the start of the buffer6508 #endif 6509 STBIR__FREE_AND_CLEAR( info->split_info[i].decode_buffer );6510 STBIR__FREE_AND_CLEAR( info->split_info[i].ring_buffers );6511 STBIR__FREE_AND_CLEAR( info->split_info[i].vertical_buffer );6512 }6513 STBIR__FREE_AND_CLEAR( info->split_info );6514 if ( info->vertical.coefficients != info->horizontal.coefficients )6515 {6516 STBIR__FREE_AND_CLEAR( info->vertical.coefficients );6517 STBIR__FREE_AND_CLEAR( info->vertical.contributors );6518 }6519 STBIR__FREE_AND_CLEAR( info->horizontal.coefficients );6520 STBIR__FREE_AND_CLEAR( info->horizontal.contributors );6521 STBIR__FREE_AND_CLEAR( info->alloced_mem );6522 STBIR__FREE_AND_CLEAR( info );6523 #endif6524 }6525 6526 #undef STBIR__FREE_AND_CLEAR6527}65286529static int stbir__get_max_split( int splits, int height )6530{6531 int i;6532 int max = 0;65336534 for( i = 0 ; i < splits ; i++ )6535 {6536 int each = height / ( splits - i );6537 if ( each > max ) 6538 max = each;6539 height -= each;6540 }6541 return max;6542}65436544static stbir__horizontal_gather_channels_func ** stbir__horizontal_gather_n_coeffs_funcs[8] = 6545{ 6546 0, stbir__horizontal_gather_1_channels_with_n_coeffs_funcs, stbir__horizontal_gather_2_channels_with_n_coeffs_funcs, stbir__horizontal_gather_3_channels_with_n_coeffs_funcs, stbir__horizontal_gather_4_channels_with_n_coeffs_funcs, 0,0, stbir__horizontal_gather_7_channels_with_n_coeffs_funcs6547};65486549static stbir__horizontal_gather_channels_func ** stbir__horizontal_gather_channels_funcs[8] = 6550{ 6551 0, stbir__horizontal_gather_1_channels_funcs, stbir__horizontal_gather_2_channels_funcs, stbir__horizontal_gather_3_channels_funcs, stbir__horizontal_gather_4_channels_funcs, 0,0, stbir__horizontal_gather_7_channels_funcs6552};65536554// there are six resize classifications: 0 == vertical scatter, 1 == vertical gather < 1x scale, 2 == vertical gather 1x-2x scale, 4 == vertical gather < 3x scale, 4 == vertical gather > 3x scale, 5 == <=4 pixel height, 6 == <=4 pixel wide column6555#define STBIR_RESIZE_CLASSIFICATIONS 865566557static float stbir__compute_weights[5][STBIR_RESIZE_CLASSIFICATIONS][4]= // 5 = 0=1chan, 1=2chan, 2=3chan, 3=4chan, 4=7chan6558{6559 {6560 { 1.00000f, 1.00000f, 0.31250f, 1.00000f },6561 { 0.56250f, 0.59375f, 0.00000f, 0.96875f },6562 { 1.00000f, 0.06250f, 0.00000f, 1.00000f },6563 { 0.00000f, 0.09375f, 1.00000f, 1.00000f },6564 { 1.00000f, 1.00000f, 1.00000f, 1.00000f },6565 { 0.03125f, 0.12500f, 1.00000f, 1.00000f },6566 { 0.06250f, 0.12500f, 0.00000f, 1.00000f },6567 { 0.00000f, 1.00000f, 0.00000f, 0.03125f },6568 }, {6569 { 0.00000f, 0.84375f, 0.00000f, 0.03125f },6570 { 0.09375f, 0.93750f, 0.00000f, 0.78125f },6571 { 0.87500f, 0.21875f, 0.00000f, 0.96875f },6572 { 0.09375f, 0.09375f, 1.00000f, 1.00000f },6573 { 1.00000f, 1.00000f, 1.00000f, 1.00000f },6574 { 0.03125f, 0.12500f, 1.00000f, 1.00000f },6575 { 0.06250f, 0.12500f, 0.00000f, 1.00000f },6576 { 0.00000f, 1.00000f, 0.00000f, 0.53125f },6577 }, {6578 { 0.00000f, 0.53125f, 0.00000f, 0.03125f },6579 { 0.06250f, 0.96875f, 0.00000f, 0.53125f },6580 { 0.87500f, 0.18750f, 0.00000f, 0.93750f },6581 { 0.00000f, 0.09375f, 1.00000f, 1.00000f },6582 { 1.00000f, 1.00000f, 1.00000f, 1.00000f },6583 { 0.03125f, 0.12500f, 1.00000f, 1.00000f },6584 { 0.06250f, 0.12500f, 0.00000f, 1.00000f },6585 { 0.00000f, 1.00000f, 0.00000f, 0.56250f },6586 }, {6587 { 0.00000f, 0.50000f, 0.00000f, 0.71875f },6588 { 0.06250f, 0.84375f, 0.00000f, 0.87500f },6589 { 1.00000f, 0.50000f, 0.50000f, 0.96875f },6590 { 1.00000f, 0.09375f, 0.31250f, 0.50000f },6591 { 1.00000f, 1.00000f, 1.00000f, 1.00000f },6592 { 1.00000f, 0.03125f, 0.03125f, 0.53125f },6593 { 0.18750f, 0.12500f, 0.00000f, 1.00000f },6594 { 0.00000f, 1.00000f, 0.03125f, 0.18750f },6595 }, {6596 { 0.00000f, 0.59375f, 0.00000f, 0.96875f },6597 { 0.06250f, 0.81250f, 0.06250f, 0.59375f },6598 { 0.75000f, 0.43750f, 0.12500f, 0.96875f },6599 { 0.87500f, 0.06250f, 0.18750f, 0.43750f },6600 { 1.00000f, 1.00000f, 1.00000f, 1.00000f },6601 { 0.15625f, 0.12500f, 1.00000f, 1.00000f },6602 { 0.06250f, 0.12500f, 0.00000f, 1.00000f },6603 { 0.00000f, 1.00000f, 0.03125f, 0.34375f },6604 }6605};66066607// structure that allow us to query and override info for training the costs6608typedef struct STBIR__V_FIRST_INFO6609{6610 double v_cost, h_cost;6611 int control_v_first; // 0 = no control, 1 = force hori, 2 = force vert6612 int v_first;6613 int v_resize_classification;6614 int is_gather;6615} STBIR__V_FIRST_INFO;66166617#ifdef STBIR__V_FIRST_INFO_BUFFER6618static STBIR__V_FIRST_INFO STBIR__V_FIRST_INFO_BUFFER = {0};6619#define STBIR__V_FIRST_INFO_POINTER &STBIR__V_FIRST_INFO_BUFFER6620#else6621#define STBIR__V_FIRST_INFO_POINTER 06622#endif66236624// Figure out whether to scale along the horizontal or vertical first.6625// This only *super* important when you are scaling by a massively 6626// different amount in the vertical vs the horizontal (for example, if 6627// you are scaling by 2x in the width, and 0.5x in the height, then you 6628// want to do the vertical scale first, because it's around 3x faster 6629// in that order.6630//6631// In more normal circumstances, this makes a 20-40% differences, so 6632// it's good to get right, but not critical. The normal way that you6633// decide which direction goes first is just figuring out which 6634// direction does more multiplies. But with modern CPUs with their 6635// fancy caches and SIMD and high IPC abilities, so there's just a lot6636// more that goes into it. 6637//6638// My handwavy sort of solution is to have an app that does a whole 6639// bunch of timing for both vertical and horizontal first modes,6640// and then another app that can read lots of these timing files6641// and try to search for the best weights to use. Dotimings.c6642// is the app that does a bunch of timings, and vf_train.c is the6643// app that solves for the best weights (and shows how well it 6644// does currently).66456646static int stbir__should_do_vertical_first( float weights_table[STBIR_RESIZE_CLASSIFICATIONS][4], int horizontal_filter_pixel_width, float horizontal_scale, int horizontal_output_size, int vertical_filter_pixel_width, float vertical_scale, int vertical_output_size, int is_gather, STBIR__V_FIRST_INFO * info ) 6647{6648 double v_cost, h_cost;6649 float * weights;6650 int vertical_first;6651 int v_classification;66526653 // categorize the resize into buckets6654 if ( ( vertical_output_size <= 4 ) || ( horizontal_output_size <= 4 ) )6655 v_classification = ( vertical_output_size < horizontal_output_size ) ? 6 : 7;6656 else if ( vertical_scale <= 1.0f )6657 v_classification = ( is_gather ) ? 1 : 0;6658 else if ( vertical_scale <= 2.0f) 6659 v_classification = 2;6660 else if ( vertical_scale <= 3.0f) 6661 v_classification = 3;6662 else if ( vertical_scale <= 4.0f) 6663 v_classification = 5;6664 else 6665 v_classification = 6;6666 6667 // use the right weights6668 weights = weights_table[ v_classification ];66696670 // this is the costs when you don't take into account modern CPUs with high ipc and simd and caches - wish we had a better estimate6671 h_cost = (float)horizontal_filter_pixel_width * weights[0] + horizontal_scale * (float)vertical_filter_pixel_width * weights[1];6672 v_cost = (float)vertical_filter_pixel_width * weights[2] + vertical_scale * (float)horizontal_filter_pixel_width * weights[3];66736674 // use computation estimate to decide vertical first or not6675 vertical_first = ( v_cost <= h_cost ) ? 1 : 0;66766677 // save these, if requested6678 if ( info )6679 {6680 info->h_cost = h_cost;6681 info->v_cost = v_cost;6682 info->v_resize_classification = v_classification;6683 info->v_first = vertical_first;6684 info->is_gather = is_gather;6685 }66866687 // and this allows us to override everything for testing (see dotiming.c) 6688 if ( ( info ) && ( info->control_v_first ) ) 6689 vertical_first = ( info->control_v_first == 2 ) ? 1 : 0;6690 6691 return vertical_first;6692}66936694// layout lookups - must match stbir_internal_pixel_layout6695static unsigned char stbir__pixel_channels[] = {6696 1,2,3,3,4, // 1ch, 2ch, rgb, bgr, 4ch6697 4,4,4,4,2,2, // RGBA,BGRA,ARGB,ABGR,RA,AR6698 4,4,4,4,2,2, // RGBA_PM,BGRA_PM,ARGB_PM,ABGR_PM,RA_PM,AR_PM6699};67006701// the internal pixel layout enums are in a different order, so we can easily do range comparisons of types6702// the public pixel layout is ordered in a way that if you cast num_channels (1-4) to the enum, you get something sensible 6703static stbir_internal_pixel_layout stbir__pixel_layout_convert_public_to_internal[] = {6704 STBIRI_BGR, STBIRI_1CHANNEL, STBIRI_2CHANNEL, STBIRI_RGB, STBIRI_RGBA, 6705 STBIRI_4CHANNEL, STBIRI_BGRA, STBIRI_ARGB, STBIRI_ABGR, STBIRI_RA, STBIRI_AR,6706 STBIRI_RGBA_PM, STBIRI_BGRA_PM, STBIRI_ARGB_PM, STBIRI_ABGR_PM, STBIRI_RA_PM, STBIRI_AR_PM,6707};67086709static stbir__info * stbir__alloc_internal_mem_and_build_samplers( stbir__sampler * horizontal, stbir__sampler * vertical, stbir__contributors * conservative, stbir_pixel_layout input_pixel_layout_public, stbir_pixel_layout output_pixel_layout_public, int splits, int new_x, int new_y, int fast_alpha, void * user_data STBIR_ONLY_PROFILE_BUILD_GET_INFO )6710{6711 static char stbir_channel_count_index[8]={ 9,0,1,2, 3,9,9,4 };67126713 stbir__info * info = 0;6714 void * alloced = 0;6715 int alloced_total = 0;6716 int vertical_first;6717 int decode_buffer_size, ring_buffer_length_bytes, ring_buffer_size, vertical_buffer_size, alloc_ring_buffer_num_entries;67186719 int alpha_weighting_type = 0; // 0=none, 1=simple, 2=fancy6720 int conservative_split_output_size = stbir__get_max_split( splits, vertical->scale_info.output_sub_size ); 6721 stbir_internal_pixel_layout input_pixel_layout = stbir__pixel_layout_convert_public_to_internal[ input_pixel_layout_public ]; 6722 stbir_internal_pixel_layout output_pixel_layout = stbir__pixel_layout_convert_public_to_internal[ output_pixel_layout_public ];6723 int channels = stbir__pixel_channels[ input_pixel_layout ]; 6724 int effective_channels = channels;6725 6726 // first figure out what type of alpha weighting to use (if any)6727 if ( ( horizontal->filter_enum != STBIR_FILTER_POINT_SAMPLE ) || ( vertical->filter_enum != STBIR_FILTER_POINT_SAMPLE ) ) // no alpha weighting on point sampling6728 {6729 if ( ( input_pixel_layout >= STBIRI_RGBA ) && ( input_pixel_layout <= STBIRI_AR ) && ( output_pixel_layout >= STBIRI_RGBA ) && ( output_pixel_layout <= STBIRI_AR ) )6730 {6731 if ( fast_alpha )6732 {6733 alpha_weighting_type = 4;6734 }6735 else6736 {6737 static int fancy_alpha_effective_cnts[6] = { 7, 7, 7, 7, 3, 3 };6738 alpha_weighting_type = 2;6739 effective_channels = fancy_alpha_effective_cnts[ input_pixel_layout - STBIRI_RGBA ];6740 }6741 }6742 else if ( ( input_pixel_layout >= STBIRI_RGBA_PM ) && ( input_pixel_layout <= STBIRI_AR_PM ) && ( output_pixel_layout >= STBIRI_RGBA ) && ( output_pixel_layout <= STBIRI_AR ) )6743 {6744 // input premult, output non-premult6745 alpha_weighting_type = 3;6746 }6747 else if ( ( input_pixel_layout >= STBIRI_RGBA ) && ( input_pixel_layout <= STBIRI_AR ) && ( output_pixel_layout >= STBIRI_RGBA_PM ) && ( output_pixel_layout <= STBIRI_AR_PM ) )6748 {6749 // input non-premult, output premult6750 alpha_weighting_type = 1;6751 }6752 }67536754 // channel in and out count must match currently6755 if ( channels != stbir__pixel_channels[ output_pixel_layout ] )6756 return 0;67576758 // get vertical first6759 vertical_first = stbir__should_do_vertical_first( stbir__compute_weights[ (int)stbir_channel_count_index[ effective_channels ] ], horizontal->filter_pixel_width, horizontal->scale_info.scale, horizontal->scale_info.output_sub_size, vertical->filter_pixel_width, vertical->scale_info.scale, vertical->scale_info.output_sub_size, vertical->is_gather, STBIR__V_FIRST_INFO_POINTER );67606761 // sometimes read one float off in some of the unrolled loops (with a weight of zero coeff, so it doesn't have an effect)6762 decode_buffer_size = ( conservative->n1 - conservative->n0 + 1 ) * effective_channels * sizeof(float) + sizeof(float); // extra float for padding6763 6764#if defined( STBIR__SEPARATE_ALLOCATIONS ) && defined(STBIR_SIMD8)6765 if ( effective_channels == 3 )6766 decode_buffer_size += sizeof(float); // avx in 3 channel mode needs one float at the start of the buffer (only with separate allocations)6767#endif 67686769 ring_buffer_length_bytes = horizontal->scale_info.output_sub_size * effective_channels * sizeof(float) + sizeof(float); // extra float for padding67706771 // if we do vertical first, the ring buffer holds a whole decoded line6772 if ( vertical_first )6773 ring_buffer_length_bytes = ( decode_buffer_size + 15 ) & ~15;67746775 if ( ( ring_buffer_length_bytes & 4095 ) == 0 ) ring_buffer_length_bytes += 64*3; // avoid 4k alias67766777 // One extra entry because floating point precision problems sometimes cause an extra to be necessary.6778 alloc_ring_buffer_num_entries = vertical->filter_pixel_width + 1;67796780 // we never need more ring buffer entries than the scanlines we're outputting when in scatter mode6781 if ( ( !vertical->is_gather ) && ( alloc_ring_buffer_num_entries > conservative_split_output_size ) )6782 alloc_ring_buffer_num_entries = conservative_split_output_size;67836784 ring_buffer_size = alloc_ring_buffer_num_entries * ring_buffer_length_bytes;67856786 // The vertical buffer is used differently, depending on whether we are scattering6787 // the vertical scanlines, or gathering them.6788 // If scattering, it's used at the temp buffer to accumulate each output.6789 // If gathering, it's just the output buffer.6790 vertical_buffer_size = horizontal->scale_info.output_sub_size * effective_channels * sizeof(float) + sizeof(float); // extra float for padding67916792 // we make two passes through this loop, 1st to add everything up, 2nd to allocate and init6793 for(;;)6794 {6795 int i;6796 void * advance_mem = alloced;6797 int copy_horizontal = 0;6798 stbir__sampler * possibly_use_horizontal_for_pivot = 0;67996800#ifdef STBIR__SEPARATE_ALLOCATIONS6801 #define STBIR__NEXT_PTR( ptr, size, ntype ) if ( alloced ) { void * p = STBIR_MALLOC( size, user_data); if ( p == 0 ) { stbir__free_internal_mem( info ); return 0; } (ptr) = (ntype*)p; }6802#else6803 #define STBIR__NEXT_PTR( ptr, size, ntype ) advance_mem = (void*) ( ( ((size_t)advance_mem) + 15 ) & ~15 ); if ( alloced ) ptr = (ntype*)advance_mem; advance_mem = ((char*)advance_mem) + (size);6804#endif68056806 STBIR__NEXT_PTR( info, sizeof( stbir__info ), stbir__info ); 68076808 STBIR__NEXT_PTR( info->split_info, sizeof( stbir__per_split_info ) * splits, stbir__per_split_info ); 68096810 if ( info )6811 {6812 static stbir__alpha_weight_func * fancy_alpha_weights[6] = { stbir__fancy_alpha_weight_4ch, stbir__fancy_alpha_weight_4ch, stbir__fancy_alpha_weight_4ch, stbir__fancy_alpha_weight_4ch, stbir__fancy_alpha_weight_2ch, stbir__fancy_alpha_weight_2ch };6813 static stbir__alpha_unweight_func * fancy_alpha_unweights[6] = { stbir__fancy_alpha_unweight_4ch, stbir__fancy_alpha_unweight_4ch, stbir__fancy_alpha_unweight_4ch, stbir__fancy_alpha_unweight_4ch, stbir__fancy_alpha_unweight_2ch, stbir__fancy_alpha_unweight_2ch };6814 static stbir__alpha_weight_func * simple_alpha_weights[6] = { stbir__simple_alpha_weight_4ch, stbir__simple_alpha_weight_4ch, stbir__simple_alpha_weight_4ch, stbir__simple_alpha_weight_4ch, stbir__simple_alpha_weight_2ch, stbir__simple_alpha_weight_2ch };6815 static stbir__alpha_unweight_func * simple_alpha_unweights[6] = { stbir__simple_alpha_unweight_4ch, stbir__simple_alpha_unweight_4ch, stbir__simple_alpha_unweight_4ch, stbir__simple_alpha_unweight_4ch, stbir__simple_alpha_unweight_2ch, stbir__simple_alpha_unweight_2ch };68166817 // initialize info fields6818 info->alloced_mem = alloced;6819 info->alloced_total = alloced_total;68206821 info->channels = channels;6822 info->effective_channels = effective_channels;6823 6824 info->offset_x = new_x;6825 info->offset_y = new_y;6826 info->alloc_ring_buffer_num_entries = alloc_ring_buffer_num_entries;6827 info->ring_buffer_num_entries = 0; 6828 info->ring_buffer_length_bytes = ring_buffer_length_bytes;6829 info->splits = splits;6830 info->vertical_first = vertical_first;68316832 info->input_pixel_layout_internal = input_pixel_layout; 6833 info->output_pixel_layout_internal = output_pixel_layout;68346835 // setup alpha weight functions6836 info->alpha_weight = 0;6837 info->alpha_unweight = 0;6838 6839 // handle alpha weighting functions and overrides6840 if ( alpha_weighting_type == 2 )6841 {6842 // high quality alpha multiplying on the way in, dividing on the way out6843 info->alpha_weight = fancy_alpha_weights[ input_pixel_layout - STBIRI_RGBA ]; 6844 info->alpha_unweight = fancy_alpha_unweights[ output_pixel_layout - STBIRI_RGBA ];6845 }6846 else if ( alpha_weighting_type == 4 )6847 {6848 // fast alpha multiplying on the way in, dividing on the way out6849 info->alpha_weight = simple_alpha_weights[ input_pixel_layout - STBIRI_RGBA ]; 6850 info->alpha_unweight = simple_alpha_unweights[ output_pixel_layout - STBIRI_RGBA ];6851 }6852 else if ( alpha_weighting_type == 1 )6853 {6854 // fast alpha on the way in, leave in premultiplied form on way out6855 info->alpha_weight = simple_alpha_weights[ input_pixel_layout - STBIRI_RGBA ]; 6856 }6857 else if ( alpha_weighting_type == 3 )6858 {6859 // incoming is premultiplied, fast alpha dividing on the way out - non-premultiplied output6860 info->alpha_unweight = simple_alpha_unweights[ output_pixel_layout - STBIRI_RGBA ];6861 }68626863 // handle 3-chan color flipping, using the alpha weight path6864 if ( ( ( input_pixel_layout == STBIRI_RGB ) && ( output_pixel_layout == STBIRI_BGR ) ) ||6865 ( ( input_pixel_layout == STBIRI_BGR ) && ( output_pixel_layout == STBIRI_RGB ) ) )6866 {6867 // do the flipping on the smaller of the two ends6868 if ( horizontal->scale_info.scale < 1.0f )6869 info->alpha_unweight = stbir__simple_flip_3ch;6870 else6871 info->alpha_weight = stbir__simple_flip_3ch;6872 }68736874 } 68756876 // get all the per-split buffers6877 for( i = 0 ; i < splits ; i++ )6878 {6879 STBIR__NEXT_PTR( info->split_info[i].decode_buffer, decode_buffer_size, float );68806881#ifdef STBIR__SEPARATE_ALLOCATIONS68826883 #ifdef STBIR_SIMD86884 if ( ( info ) && ( effective_channels == 3 ) )6885 ++info->split_info[i].decode_buffer; // avx in 3 channel mode needs one float at the start of the buffer6886 #endif 68876888 STBIR__NEXT_PTR( info->split_info[i].ring_buffers, alloc_ring_buffer_num_entries * sizeof(float*), float* );6889 {6890 int j;6891 for( j = 0 ; j < alloc_ring_buffer_num_entries ; j++ )6892 {6893 STBIR__NEXT_PTR( info->split_info[i].ring_buffers[j], ring_buffer_length_bytes, float );6894 #ifdef STBIR_SIMD86895 if ( ( info ) && ( effective_channels == 3 ) )6896 ++info->split_info[i].ring_buffers[j]; // avx in 3 channel mode needs one float at the start of the buffer6897 #endif 6898 }6899 }6900#else6901 STBIR__NEXT_PTR( info->split_info[i].ring_buffer, ring_buffer_size, float );6902#endif6903 STBIR__NEXT_PTR( info->split_info[i].vertical_buffer, vertical_buffer_size, float );6904 }69056906 // alloc memory for to-be-pivoted coeffs (if necessary)6907 if ( vertical->is_gather == 0 )6908 {6909 int both;6910 int temp_mem_amt;69116912 // when in vertical scatter mode, we first build the coefficients in gather mode, and then pivot after,6913 // that means we need two buffers, so we try to use the decode buffer and ring buffer for this. if that6914 // is too small, we just allocate extra memory to use as this temp.69156916 both = vertical->gather_prescatter_contributors_size + vertical->gather_prescatter_coefficients_size;69176918#ifdef STBIR__SEPARATE_ALLOCATIONS6919 temp_mem_amt = decode_buffer_size;6920#else6921 temp_mem_amt = ( decode_buffer_size + ring_buffer_size + vertical_buffer_size ) * splits;6922#endif6923 if ( temp_mem_amt >= both )6924 {6925 if ( info ) 6926 { 6927 vertical->gather_prescatter_contributors = (stbir__contributors*)info->split_info[0].decode_buffer; 6928 vertical->gather_prescatter_coefficients = (float*) ( ( (char*)info->split_info[0].decode_buffer ) + vertical->gather_prescatter_contributors_size ); 6929 }6930 }6931 else6932 {6933 // ring+decode memory is too small, so allocate temp memory6934 STBIR__NEXT_PTR( vertical->gather_prescatter_contributors, vertical->gather_prescatter_contributors_size, stbir__contributors );6935 STBIR__NEXT_PTR( vertical->gather_prescatter_coefficients, vertical->gather_prescatter_coefficients_size, float );6936 }6937 }69386939 STBIR__NEXT_PTR( horizontal->contributors, horizontal->contributors_size, stbir__contributors );6940 STBIR__NEXT_PTR( horizontal->coefficients, horizontal->coefficients_size, float );69416942 // are the two filters identical?? (happens a lot with mipmap generation)6943 if ( ( horizontal->filter_kernel == vertical->filter_kernel ) && ( horizontal->filter_support == vertical->filter_support ) && ( horizontal->edge == vertical->edge ) && ( horizontal->scale_info.output_sub_size == vertical->scale_info.output_sub_size ) )6944 {6945 float diff_scale = horizontal->scale_info.scale - vertical->scale_info.scale;6946 float diff_shift = horizontal->scale_info.pixel_shift - vertical->scale_info.pixel_shift;6947 if ( diff_scale < 0.0f ) diff_scale = -diff_scale;6948 if ( diff_shift < 0.0f ) diff_shift = -diff_shift;6949 if ( ( diff_scale <= stbir__small_float ) && ( diff_shift <= stbir__small_float ) )6950 {6951 if ( horizontal->is_gather == vertical->is_gather ) 6952 {6953 copy_horizontal = 1;6954 goto no_vert_alloc;6955 }6956 // everything matches, but vertical is scatter, horizontal is gather, use horizontal coeffs for vertical pivot coeffs6957 possibly_use_horizontal_for_pivot = horizontal;6958 }6959 }69606961 STBIR__NEXT_PTR( vertical->contributors, vertical->contributors_size, stbir__contributors );6962 STBIR__NEXT_PTR( vertical->coefficients, vertical->coefficients_size, float );69636964 no_vert_alloc:69656966 if ( info )6967 {6968 STBIR_PROFILE_BUILD_START( horizontal );69696970 stbir__calculate_filters( horizontal, 0, user_data STBIR_ONLY_PROFILE_BUILD_SET_INFO );69716972 // setup the horizontal gather functions6973 // start with defaulting to the n_coeffs functions (specialized on channels and remnant leftover)6974 info->horizontal_gather_channels = stbir__horizontal_gather_n_coeffs_funcs[ effective_channels ][ horizontal->extent_info.widest & 3 ];6975 // but if the number of coeffs <= 12, use another set of special cases. <=12 coeffs is any enlarging resize, or shrinking resize down to about 1/3 size6976 if ( horizontal->extent_info.widest <= 12 )6977 info->horizontal_gather_channels = stbir__horizontal_gather_channels_funcs[ effective_channels ][ horizontal->extent_info.widest - 1 ];6978 6979 info->scanline_extents.conservative.n0 = conservative->n0;6980 info->scanline_extents.conservative.n1 = conservative->n1;6981 6982 // get exact extents6983 stbir__get_extents( horizontal, &info->scanline_extents );69846985 // pack the horizontal coeffs6986 horizontal->coefficient_width = stbir__pack_coefficients(horizontal->num_contributors, horizontal->contributors, horizontal->coefficients, horizontal->coefficient_width, horizontal->extent_info.widest, info->scanline_extents.conservative.n1 + 1 );6987 6988 STBIR_MEMCPY( &info->horizontal, horizontal, sizeof( stbir__sampler ) );69896990 STBIR_PROFILE_BUILD_END( horizontal );69916992 if ( copy_horizontal )6993 {6994 STBIR_MEMCPY( &info->vertical, horizontal, sizeof( stbir__sampler ) );6995 }6996 else6997 {6998 STBIR_PROFILE_BUILD_START( vertical );69997000 stbir__calculate_filters( vertical, possibly_use_horizontal_for_pivot, user_data STBIR_ONLY_PROFILE_BUILD_SET_INFO );7001 STBIR_MEMCPY( &info->vertical, vertical, sizeof( stbir__sampler ) );70027003 STBIR_PROFILE_BUILD_END( vertical );7004 }70057006 // setup the vertical split ranges7007 stbir__get_split_info( info->split_info, info->splits, info->vertical.scale_info.output_sub_size, info->vertical.filter_pixel_margin, info->vertical.scale_info.input_full_size );70087009 // now we know precisely how many entries we need7010 info->ring_buffer_num_entries = info->vertical.extent_info.widest;70117012 // we never need more ring buffer entries than the scanlines we're outputting7013 if ( ( !info->vertical.is_gather ) && ( info->ring_buffer_num_entries > conservative_split_output_size ) )7014 info->ring_buffer_num_entries = conservative_split_output_size;7015 STBIR_ASSERT( info->ring_buffer_num_entries <= info->alloc_ring_buffer_num_entries );70167017 // a few of the horizontal gather functions read one dword past the end (but mask it out), so put in a normal value so no snans or denormals accidentally sneak in7018 for( i = 0 ; i < splits ; i++ )7019 {7020 int width, ofs;7021 7022 // find the right most span7023 if ( info->scanline_extents.spans[0].n1 > info->scanline_extents.spans[1].n1 )7024 width = info->scanline_extents.spans[0].n1 - info->scanline_extents.spans[0].n0;7025 else7026 width = info->scanline_extents.spans[1].n1 - info->scanline_extents.spans[1].n0;7027 7028 // this calc finds the exact end of the decoded scanline for all filter modes.7029 // usually this is just the width * effective channels. But we have to account 7030 // for the area to the left of the scanline for wrap filtering and alignment, this 7031 // is stored as a negative value in info->scanline_extents.conservative.n0. Next,7032 // we need to skip the exact size of the right hand size filter area (again for7033 // wrap mode), this is in info->scanline_extents.edge_sizes[1]).7034 ofs = ( width + 1 - info->scanline_extents.conservative.n0 + info->scanline_extents.edge_sizes[1] ) * effective_channels;7035 7036 // place a known, but numerically valid value in the decode buffer7037 info->split_info[i].decode_buffer[ ofs ] = 9999.0f;70387039 // if vertical filtering first, place a known, but numerically valid value in the all7040 // of the ring buffer accumulators7041 if ( vertical_first )7042 {7043 int j; 7044 for( j = 0; j < info->ring_buffer_num_entries ; j++ )7045 {7046 stbir__get_ring_buffer_entry( info, info->split_info + i, j )[ ofs ] = 9999.0f;7047 }7048 }7049 }7050 }70517052 #undef STBIR__NEXT_PTR705370547055 // is this the first time through loop?7056 if ( info == 0 )7057 {7058 alloced_total = (int) ( 15 + (size_t)advance_mem );7059 alloced = STBIR_MALLOC( alloced_total, user_data );7060 if ( alloced == 0 )7061 return 0;7062 }7063 else7064 return info; // success7065 }7066}70677068static int stbir__perform_resize( stbir__info const * info, int split_start, int split_count ) 7069{7070 stbir__per_split_info * split_info = info->split_info + split_start;70717072 STBIR_PROFILE_CLEAR_EXTRAS();70737074 STBIR_PROFILE_FIRST_START( looping );7075 if (info->vertical.is_gather)7076 stbir__vertical_gather_loop( info, split_info, split_count );7077 else7078 stbir__vertical_scatter_loop( info, split_info, split_count );7079 STBIR_PROFILE_END( looping );70807081 return 1;7082}70837084static void stbir__update_info_from_resize( stbir__info * info, STBIR_RESIZE * resize )7085{7086 static stbir__decode_pixels_func * decode_simple[STBIR_TYPE_HALF_FLOAT-STBIR_TYPE_UINT8_SRGB+1]=7087 {7088 /* 1ch-4ch */ stbir__decode_uint8_srgb, stbir__decode_uint8_srgb, 0, stbir__decode_float_linear, stbir__decode_half_float_linear, 7089 };70907091 static stbir__decode_pixels_func * decode_alphas[STBIRI_AR-STBIRI_RGBA+1][STBIR_TYPE_HALF_FLOAT-STBIR_TYPE_UINT8_SRGB+1]=7092 {7093 { /* RGBA */ stbir__decode_uint8_srgb4_linearalpha, stbir__decode_uint8_srgb, 0, stbir__decode_float_linear, stbir__decode_half_float_linear },7094 { /* BGRA */ stbir__decode_uint8_srgb4_linearalpha_BGRA, stbir__decode_uint8_srgb_BGRA, 0, stbir__decode_float_linear_BGRA, stbir__decode_half_float_linear_BGRA },7095 { /* ARGB */ stbir__decode_uint8_srgb4_linearalpha_ARGB, stbir__decode_uint8_srgb_ARGB, 0, stbir__decode_float_linear_ARGB, stbir__decode_half_float_linear_ARGB },7096 { /* ABGR */ stbir__decode_uint8_srgb4_linearalpha_ABGR, stbir__decode_uint8_srgb_ABGR, 0, stbir__decode_float_linear_ABGR, stbir__decode_half_float_linear_ABGR },7097 { /* RA */ stbir__decode_uint8_srgb2_linearalpha, stbir__decode_uint8_srgb, 0, stbir__decode_float_linear, stbir__decode_half_float_linear },7098 { /* AR */ stbir__decode_uint8_srgb2_linearalpha_AR, stbir__decode_uint8_srgb_AR, 0, stbir__decode_float_linear_AR, stbir__decode_half_float_linear_AR },7099 };71007101 static stbir__decode_pixels_func * decode_simple_scaled_or_not[2][2]=7102 {7103 { stbir__decode_uint8_linear_scaled, stbir__decode_uint8_linear }, { stbir__decode_uint16_linear_scaled, stbir__decode_uint16_linear },7104 };71057106 static stbir__decode_pixels_func * decode_alphas_scaled_or_not[STBIRI_AR-STBIRI_RGBA+1][2][2]=7107 {7108 { /* RGBA */ { stbir__decode_uint8_linear_scaled, stbir__decode_uint8_linear }, { stbir__decode_uint16_linear_scaled, stbir__decode_uint16_linear } },7109 { /* BGRA */ { stbir__decode_uint8_linear_scaled_BGRA, stbir__decode_uint8_linear_BGRA }, { stbir__decode_uint16_linear_scaled_BGRA, stbir__decode_uint16_linear_BGRA } },7110 { /* ARGB */ { stbir__decode_uint8_linear_scaled_ARGB, stbir__decode_uint8_linear_ARGB }, { stbir__decode_uint16_linear_scaled_ARGB, stbir__decode_uint16_linear_ARGB } },7111 { /* ABGR */ { stbir__decode_uint8_linear_scaled_ABGR, stbir__decode_uint8_linear_ABGR }, { stbir__decode_uint16_linear_scaled_ABGR, stbir__decode_uint16_linear_ABGR } },7112 { /* RA */ { stbir__decode_uint8_linear_scaled, stbir__decode_uint8_linear }, { stbir__decode_uint16_linear_scaled, stbir__decode_uint16_linear } },7113 { /* AR */ { stbir__decode_uint8_linear_scaled_AR, stbir__decode_uint8_linear_AR }, { stbir__decode_uint16_linear_scaled_AR, stbir__decode_uint16_linear_AR } }7114 };71157116 static stbir__encode_pixels_func * encode_simple[STBIR_TYPE_HALF_FLOAT-STBIR_TYPE_UINT8_SRGB+1]=7117 {7118 /* 1ch-4ch */ stbir__encode_uint8_srgb, stbir__encode_uint8_srgb, 0, stbir__encode_float_linear, stbir__encode_half_float_linear,7119 };71207121 static stbir__encode_pixels_func * encode_alphas[STBIRI_AR-STBIRI_RGBA+1][STBIR_TYPE_HALF_FLOAT-STBIR_TYPE_UINT8_SRGB+1]=7122 {7123 { /* RGBA */ stbir__encode_uint8_srgb4_linearalpha, stbir__encode_uint8_srgb, 0, stbir__encode_float_linear, stbir__encode_half_float_linear },7124 { /* BGRA */ stbir__encode_uint8_srgb4_linearalpha_BGRA, stbir__encode_uint8_srgb_BGRA, 0, stbir__encode_float_linear_BGRA, stbir__encode_half_float_linear_BGRA },7125 { /* ARGB */ stbir__encode_uint8_srgb4_linearalpha_ARGB, stbir__encode_uint8_srgb_ARGB, 0, stbir__encode_float_linear_ARGB, stbir__encode_half_float_linear_ARGB },7126 { /* ABGR */ stbir__encode_uint8_srgb4_linearalpha_ABGR, stbir__encode_uint8_srgb_ABGR, 0, stbir__encode_float_linear_ABGR, stbir__encode_half_float_linear_ABGR },7127 { /* RA */ stbir__encode_uint8_srgb2_linearalpha, stbir__encode_uint8_srgb, 0, stbir__encode_float_linear, stbir__encode_half_float_linear },7128 { /* AR */ stbir__encode_uint8_srgb2_linearalpha_AR, stbir__encode_uint8_srgb_AR, 0, stbir__encode_float_linear_AR, stbir__encode_half_float_linear_AR }7129 };71307131 static stbir__encode_pixels_func * encode_simple_scaled_or_not[2][2]=7132 {7133 { stbir__encode_uint8_linear_scaled, stbir__encode_uint8_linear }, { stbir__encode_uint16_linear_scaled, stbir__encode_uint16_linear },7134 };71357136 static stbir__encode_pixels_func * encode_alphas_scaled_or_not[STBIRI_AR-STBIRI_RGBA+1][2][2]=7137 {7138 { /* RGBA */ { stbir__encode_uint8_linear_scaled, stbir__encode_uint8_linear }, { stbir__encode_uint16_linear_scaled, stbir__encode_uint16_linear } },7139 { /* BGRA */ { stbir__encode_uint8_linear_scaled_BGRA, stbir__encode_uint8_linear_BGRA }, { stbir__encode_uint16_linear_scaled_BGRA, stbir__encode_uint16_linear_BGRA } },7140 { /* ARGB */ { stbir__encode_uint8_linear_scaled_ARGB, stbir__encode_uint8_linear_ARGB }, { stbir__encode_uint16_linear_scaled_ARGB, stbir__encode_uint16_linear_ARGB } },7141 { /* ABGR */ { stbir__encode_uint8_linear_scaled_ABGR, stbir__encode_uint8_linear_ABGR }, { stbir__encode_uint16_linear_scaled_ABGR, stbir__encode_uint16_linear_ABGR } },7142 { /* RA */ { stbir__encode_uint8_linear_scaled, stbir__encode_uint8_linear }, { stbir__encode_uint16_linear_scaled, stbir__encode_uint16_linear } },7143 { /* AR */ { stbir__encode_uint8_linear_scaled_AR, stbir__encode_uint8_linear_AR }, { stbir__encode_uint16_linear_scaled_AR, stbir__encode_uint16_linear_AR } }7144 };71457146 stbir__decode_pixels_func * decode_pixels = 0;7147 stbir__encode_pixels_func * encode_pixels = 0;7148 stbir_datatype input_type, output_type;71497150 input_type = resize->input_data_type;7151 output_type = resize->output_data_type; 7152 info->input_data = resize->input_pixels;7153 info->input_stride_bytes = resize->input_stride_in_bytes;7154 info->output_stride_bytes = resize->output_stride_in_bytes;71557156 // if we're completely point sampling, then we can turn off SRGB7157 if ( ( info->horizontal.filter_enum == STBIR_FILTER_POINT_SAMPLE ) && ( info->vertical.filter_enum == STBIR_FILTER_POINT_SAMPLE ) )7158 {7159 if ( ( ( input_type == STBIR_TYPE_UINT8_SRGB ) || ( input_type == STBIR_TYPE_UINT8_SRGB_ALPHA ) ) && 7160 ( ( output_type == STBIR_TYPE_UINT8_SRGB ) || ( output_type == STBIR_TYPE_UINT8_SRGB_ALPHA ) ) )7161 {7162 input_type = STBIR_TYPE_UINT8;7163 output_type = STBIR_TYPE_UINT8;7164 }7165 }71667167 // recalc the output and input strides 7168 if ( info->input_stride_bytes == 0 )7169 info->input_stride_bytes = info->channels * info->horizontal.scale_info.input_full_size * stbir__type_size[input_type];71707171 if ( info->output_stride_bytes == 0 )7172 info->output_stride_bytes = info->channels * info->horizontal.scale_info.output_sub_size * stbir__type_size[output_type];71737174 // calc offset7175 info->output_data = ( (char*) resize->output_pixels ) + ( (ptrdiff_t) info->offset_y * (ptrdiff_t) resize->output_stride_in_bytes ) + ( info->offset_x * info->channels * stbir__type_size[output_type] );71767177 info->in_pixels_cb = resize->input_cb;7178 info->user_data = resize->user_data;7179 info->out_pixels_cb = resize->output_cb;71807181 // setup the input format converters7182 if ( ( input_type == STBIR_TYPE_UINT8 ) || ( input_type == STBIR_TYPE_UINT16 ) )7183 {7184 int non_scaled = 0;71857186 // check if we can run unscaled - 0-255.0/0-65535.0 instead of 0-1.0 (which is a tiny bit faster when doing linear 8->8 or 16->16)7187 if ( ( !info->alpha_weight ) && ( !info->alpha_unweight ) ) // don't short circuit when alpha weighting (get everything to 0-1.0 as usual)7188 if ( ( ( input_type == STBIR_TYPE_UINT8 ) && ( output_type == STBIR_TYPE_UINT8 ) ) || ( ( input_type == STBIR_TYPE_UINT16 ) && ( output_type == STBIR_TYPE_UINT16 ) ) )7189 non_scaled = 1;71907191 if ( info->input_pixel_layout_internal <= STBIRI_4CHANNEL )7192 decode_pixels = decode_simple_scaled_or_not[ input_type == STBIR_TYPE_UINT16 ][ non_scaled ];7193 else7194 decode_pixels = decode_alphas_scaled_or_not[ ( info->input_pixel_layout_internal - STBIRI_RGBA ) % ( STBIRI_AR-STBIRI_RGBA+1 ) ][ input_type == STBIR_TYPE_UINT16 ][ non_scaled ];7195 }7196 else7197 {7198 if ( info->input_pixel_layout_internal <= STBIRI_4CHANNEL )7199 decode_pixels = decode_simple[ input_type - STBIR_TYPE_UINT8_SRGB ];7200 else7201 decode_pixels = decode_alphas[ ( info->input_pixel_layout_internal - STBIRI_RGBA ) % ( STBIRI_AR-STBIRI_RGBA+1 ) ][ input_type - STBIR_TYPE_UINT8_SRGB ];7202 }72037204 // setup the output format converters7205 if ( ( output_type == STBIR_TYPE_UINT8 ) || ( output_type == STBIR_TYPE_UINT16 ) )7206 {7207 int non_scaled = 0;7208 7209 // check if we can run unscaled - 0-255.0/0-65535.0 instead of 0-1.0 (which is a tiny bit faster when doing linear 8->8 or 16->16)7210 if ( ( !info->alpha_weight ) && ( !info->alpha_unweight ) ) // don't short circuit when alpha weighting (get everything to 0-1.0 as usual)7211 if ( ( ( input_type == STBIR_TYPE_UINT8 ) && ( output_type == STBIR_TYPE_UINT8 ) ) || ( ( input_type == STBIR_TYPE_UINT16 ) && ( output_type == STBIR_TYPE_UINT16 ) ) )7212 non_scaled = 1;72137214 if ( info->output_pixel_layout_internal <= STBIRI_4CHANNEL )7215 encode_pixels = encode_simple_scaled_or_not[ output_type == STBIR_TYPE_UINT16 ][ non_scaled ];7216 else7217 encode_pixels = encode_alphas_scaled_or_not[ ( info->output_pixel_layout_internal - STBIRI_RGBA ) % ( STBIRI_AR-STBIRI_RGBA+1 ) ][ output_type == STBIR_TYPE_UINT16 ][ non_scaled ];7218 }7219 else7220 {7221 if ( info->output_pixel_layout_internal <= STBIRI_4CHANNEL )7222 encode_pixels = encode_simple[ output_type - STBIR_TYPE_UINT8_SRGB ];7223 else7224 encode_pixels = encode_alphas[ ( info->output_pixel_layout_internal - STBIRI_RGBA ) % ( STBIRI_AR-STBIRI_RGBA+1 ) ][ output_type - STBIR_TYPE_UINT8_SRGB ];7225 }72267227 info->input_type = input_type;7228 info->output_type = output_type; 7229 info->decode_pixels = decode_pixels;7230 info->encode_pixels = encode_pixels; 7231}72327233static void stbir__clip( int * outx, int * outsubw, int outw, double * u0, double * u1 )7234{7235 double per, adj;7236 int over;7237 7238 // do left/top edge7239 if ( *outx < 0 )7240 {7241 per = ( (double)*outx ) / ( (double)*outsubw ); // is negative7242 adj = per * ( *u1 - *u0 );7243 *u0 -= adj; // increases u07244 *outx = 0;7245 }72467247 // do right/bot edge7248 over = outw - ( *outx + *outsubw );7249 if ( over < 0 )7250 {7251 per = ( (double)over ) / ( (double)*outsubw ); // is negative7252 adj = per * ( *u1 - *u0 );7253 *u1 += adj; // decrease u17254 *outsubw = outw - *outx;7255 }7256} 72577258// converts a double to a rational that has less than one float bit of error (returns 0 if unable to do so)7259static int stbir__double_to_rational(double f, stbir_uint32 limit, stbir_uint32 *numer, stbir_uint32 *denom, int limit_denom ) // limit_denom (1) or limit numer (0)7260{7261 double err;7262 stbir_uint64 top, bot;7263 stbir_uint64 numer_last = 0;7264 stbir_uint64 denom_last = 1;7265 stbir_uint64 numer_estimate = 1;7266 stbir_uint64 denom_estimate = 0;72677268 // scale to past float error range7269 top = (stbir_uint64)( f * (double)(1 << 25) );7270 bot = 1 << 25;72717272 // keep refining, but usually stops in a few loops - usually 5 for bad cases7273 for(;;) 7274 {7275 stbir_uint64 est, temp;72767277 // hit limit, break out and do best full range estimate7278 if ( ( ( limit_denom ) ? denom_estimate : numer_estimate ) >= limit )7279 break;72807281 // is the current error less than 1 bit of a float? if so, we're done7282 if ( denom_estimate )7283 {7284 err = ( (double)numer_estimate / (double)denom_estimate ) - f;7285 if ( err < 0.0 ) err = -err;7286 if ( err < ( 1.0 / (double)(1<<24) ) )7287 {7288 // yup, found it7289 *numer = (stbir_uint32) numer_estimate;7290 *denom = (stbir_uint32) denom_estimate;7291 return 1;7292 }7293 }72947295 // no more refinement bits left? break out and do full range estimate7296 if ( bot == 0 )7297 break;72987299 // gcd the estimate bits7300 est = top / bot;7301 temp = top % bot;7302 top = bot;7303 bot = temp;73047305 // move remainders7306 temp = est * denom_estimate + denom_last; 7307 denom_last = denom_estimate; 7308 denom_estimate = temp;73097310 // move remainders7311 temp = est * numer_estimate + numer_last; 7312 numer_last = numer_estimate; 7313 numer_estimate = temp;7314 }73157316 // we didn't fine anything good enough for float, use a full range estimate7317 if ( limit_denom )7318 {7319 numer_estimate= (stbir_uint64)( f * (double)limit + 0.5 );7320 denom_estimate = limit;7321 }7322 else7323 {7324 numer_estimate = limit;7325 denom_estimate = (stbir_uint64)( ( (double)limit / f ) + 0.5 );7326 }73277328 *numer = (stbir_uint32) numer_estimate;7329 *denom = (stbir_uint32) denom_estimate;73307331 err = ( denom_estimate ) ? ( ( (double)(stbir_uint32)numer_estimate / (double)(stbir_uint32)denom_estimate ) - f ) : 1.0;7332 if ( err < 0.0 ) err = -err;7333 return ( err < ( 1.0 / (double)(1<<24) ) ) ? 1 : 0;7334}73357336static int stbir__calculate_region_transform( stbir__scale_info * scale_info, int output_full_range, int * output_offset, int output_sub_range, int input_full_range, double input_s0, double input_s1 )7337{7338 double output_range, input_range, output_s, input_s, ratio, scale;73397340 input_s = input_s1 - input_s0;73417342 // null area7343 if ( ( output_full_range == 0 ) || ( input_full_range == 0 ) ||7344 ( output_sub_range == 0 ) || ( input_s <= stbir__small_float ) )7345 return 0;73467347 // are either of the ranges completely out of bounds?7348 if ( ( *output_offset >= output_full_range ) || ( ( *output_offset + output_sub_range ) <= 0 ) || ( input_s0 >= (1.0f-stbir__small_float) ) || ( input_s1 <= stbir__small_float ) )7349 return 0;73507351 output_range = (double)output_full_range;7352 input_range = (double)input_full_range;73537354 output_s = ( (double)output_sub_range) / output_range;73557356 // figure out the scaling to use 7357 ratio = output_s / input_s; 73587359 // save scale before clipping7360 scale = ( output_range / input_range ) * ratio; 7361 scale_info->scale = (float)scale;7362 scale_info->inv_scale = (float)( 1.0 / scale );73637364 // clip output area to left/right output edges (and adjust input area)7365 stbir__clip( output_offset, &output_sub_range, output_full_range, &input_s0, &input_s1 );73667367 // recalc input area7368 input_s = input_s1 - input_s0;73697370 // after clipping do we have zero input area?7371 if ( input_s <= stbir__small_float ) 7372 return 0;73737374 // calculate and store the starting source offsets in output pixel space 7375 scale_info->pixel_shift = (float) ( input_s0 * ratio * output_range ); 73767377 scale_info->scale_is_rational = stbir__double_to_rational( scale, ( scale <= 1.0 ) ? output_full_range : input_full_range, &scale_info->scale_numerator, &scale_info->scale_denominator, ( scale >= 1.0 ) );73787379 scale_info->input_full_size = input_full_range;7380 scale_info->output_sub_size = output_sub_range;73817382 return 1;7383}738473857386static void stbir__init_and_set_layout( STBIR_RESIZE * resize, stbir_pixel_layout pixel_layout, stbir_datatype data_type )7387{7388 resize->input_cb = 0;7389 resize->output_cb = 0;7390 resize->user_data = resize;7391 resize->samplers = 0;7392 resize->needs_rebuild = 1;7393 resize->called_alloc = 0;7394 resize->horizontal_filter = STBIR_FILTER_DEFAULT;7395 resize->horizontal_filter_kernel = 0; resize->horizontal_filter_support = 0;7396 resize->vertical_filter = STBIR_FILTER_DEFAULT;7397 resize->vertical_filter_kernel = 0; resize->vertical_filter_support = 0;7398 resize->horizontal_edge = STBIR_EDGE_CLAMP;7399 resize->vertical_edge = STBIR_EDGE_CLAMP;7400 resize->input_s0 = 0; resize->input_t0 = 0; resize->input_s1 = 1; resize->input_t1 = 1;7401 resize->output_subx = 0; resize->output_suby = 0; resize->output_subw = resize->output_w; resize->output_subh = resize->output_h;7402 resize->input_data_type = data_type;7403 resize->output_data_type = data_type;7404 resize->input_pixel_layout_public = pixel_layout;7405 resize->output_pixel_layout_public = pixel_layout;7406}74077408STBIRDEF void stbir_resize_init( STBIR_RESIZE * resize, 7409 const void *input_pixels, int input_w, int input_h, int input_stride_in_bytes, // stride can be zero7410 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, // stride can be zero7411 stbir_pixel_layout pixel_layout, stbir_datatype data_type )7412{7413 resize->input_pixels = input_pixels;7414 resize->input_w = input_w;7415 resize->input_h = input_h;7416 resize->input_stride_in_bytes = input_stride_in_bytes;7417 resize->output_pixels = output_pixels;7418 resize->output_w = output_w;7419 resize->output_h = output_h;7420 resize->output_stride_in_bytes = output_stride_in_bytes;7421 resize->fast_alpha = 0;74227423 stbir__init_and_set_layout( resize, pixel_layout, data_type );7424}74257426// You can update parameters any time after resize_init7427STBIRDEF void stbir_set_datatypes( STBIR_RESIZE * resize, stbir_datatype input_type, stbir_datatype output_type ) // by default, datatype from resize_init7428{7429 resize->input_data_type = input_type;7430 resize->output_data_type = output_type;7431}74327433STBIRDEF void stbir_set_pixel_callbacks( STBIR_RESIZE * resize, stbir_input_callback * input_cb, stbir_output_callback * output_cb ) // no callbacks by default7434{7435 resize->input_cb = input_cb;7436 resize->output_cb = output_cb;7437}74387439STBIRDEF void stbir_set_user_data( STBIR_RESIZE * resize, void * user_data ) // pass back STBIR_RESIZE* by default7440{7441 resize->user_data = user_data;7442}74437444STBIRDEF void stbir_set_buffer_ptrs( STBIR_RESIZE * resize, const void * input_pixels, int input_stride_in_bytes, void * output_pixels, int output_stride_in_bytes )7445{7446 resize->input_pixels = input_pixels;7447 resize->input_stride_in_bytes = input_stride_in_bytes;7448 resize->output_pixels = output_pixels;7449 resize->output_stride_in_bytes = output_stride_in_bytes;7450}745174527453STBIRDEF int stbir_set_edgemodes( STBIR_RESIZE * resize, stbir_edge horizontal_edge, stbir_edge vertical_edge ) // CLAMP by default7454{7455 resize->horizontal_edge = horizontal_edge;7456 resize->vertical_edge = vertical_edge;7457 resize->needs_rebuild = 1;7458 return 1;7459}74607461STBIRDEF int stbir_set_filters( STBIR_RESIZE * resize, stbir_filter horizontal_filter, stbir_filter vertical_filter ) // STBIR_DEFAULT_FILTER_UPSAMPLE/DOWNSAMPLE by default7462{7463 resize->horizontal_filter = horizontal_filter;7464 resize->vertical_filter = vertical_filter;7465 resize->needs_rebuild = 1;7466 return 1;7467}74687469STBIRDEF int stbir_set_filter_callbacks( STBIR_RESIZE * resize, stbir__kernel_callback * horizontal_filter, stbir__support_callback * horizontal_support, stbir__kernel_callback * vertical_filter, stbir__support_callback * vertical_support )7470{7471 resize->horizontal_filter_kernel = horizontal_filter; resize->horizontal_filter_support = horizontal_support;7472 resize->vertical_filter_kernel = vertical_filter; resize->vertical_filter_support = vertical_support;7473 resize->needs_rebuild = 1;7474 return 1;7475}74767477STBIRDEF int stbir_set_pixel_layouts( STBIR_RESIZE * resize, stbir_pixel_layout input_pixel_layout, stbir_pixel_layout output_pixel_layout ) // sets new pixel layouts7478{7479 resize->input_pixel_layout_public = input_pixel_layout;7480 resize->output_pixel_layout_public = output_pixel_layout;7481 resize->needs_rebuild = 1;7482 return 1;7483}748474857486STBIRDEF int stbir_set_non_pm_alpha_speed_over_quality( STBIR_RESIZE * resize, int non_pma_alpha_speed_over_quality ) // sets alpha speed7487{7488 resize->fast_alpha = non_pma_alpha_speed_over_quality;7489 resize->needs_rebuild = 1;7490 return 1;7491}74927493STBIRDEF int stbir_set_input_subrect( STBIR_RESIZE * resize, double s0, double t0, double s1, double t1 ) // sets input region (full region by default)7494{7495 resize->input_s0 = s0;7496 resize->input_t0 = t0;7497 resize->input_s1 = s1;7498 resize->input_t1 = t1;7499 resize->needs_rebuild = 1;75007501 // are we inbounds?7502 if ( ( s1 < stbir__small_float ) || ( (s1-s0) < stbir__small_float ) ||7503 ( t1 < stbir__small_float ) || ( (t1-t0) < stbir__small_float ) ||7504 ( s0 > (1.0f-stbir__small_float) ) ||7505 ( t0 > (1.0f-stbir__small_float) ) )7506 return 0;75077508 return 1;7509}75107511STBIRDEF int stbir_set_output_pixel_subrect( STBIR_RESIZE * resize, int subx, int suby, int subw, int subh ) // sets input region (full region by default)7512{7513 resize->output_subx = subx;7514 resize->output_suby = suby;7515 resize->output_subw = subw;7516 resize->output_subh = subh;7517 resize->needs_rebuild = 1;75187519 // are we inbounds?7520 if ( ( subx >= resize->output_w ) || ( ( subx + subw ) <= 0 ) || ( suby >= resize->output_h ) || ( ( suby + subh ) <= 0 ) || ( subw == 0 ) || ( subh == 0 ) )7521 return 0;75227523 return 1;7524}75257526STBIRDEF int stbir_set_pixel_subrect( STBIR_RESIZE * resize, int subx, int suby, int subw, int subh ) // sets both regions (full regions by default)7527{7528 double s0, t0, s1, t1;75297530 s0 = ( (double)subx ) / ( (double)resize->output_w );7531 t0 = ( (double)suby ) / ( (double)resize->output_h );7532 s1 = ( (double)(subx+subw) ) / ( (double)resize->output_w );7533 t1 = ( (double)(suby+subh) ) / ( (double)resize->output_h );75347535 resize->input_s0 = s0;7536 resize->input_t0 = t0;7537 resize->input_s1 = s1;7538 resize->input_t1 = t1;7539 resize->output_subx = subx;7540 resize->output_suby = suby;7541 resize->output_subw = subw;7542 resize->output_subh = subh;7543 resize->needs_rebuild = 1;75447545 // are we inbounds?7546 if ( ( subx >= resize->output_w ) || ( ( subx + subw ) <= 0 ) || ( suby >= resize->output_h ) || ( ( suby + subh ) <= 0 ) || ( subw == 0 ) || ( subh == 0 ) )7547 return 0;75487549 return 1;7550}75517552static int stbir__perform_build( STBIR_RESIZE * resize, int splits ) 7553{7554 stbir__contributors conservative = { 0, 0 };7555 stbir__sampler horizontal, vertical;7556 int new_output_subx, new_output_suby;7557 stbir__info * out_info;7558 #ifdef STBIR_PROFILE7559 stbir__info profile_infod; // used to contain building profile info before everything is allocated7560 stbir__info * profile_info = &profile_infod;7561 #endif75627563 // have we already built the samplers?7564 if ( resize->samplers )7565 return 0;7566 7567 #define STBIR_RETURN_ERROR_AND_ASSERT( exp ) STBIR_ASSERT( !(exp) ); if (exp) return 0;7568 STBIR_RETURN_ERROR_AND_ASSERT( (unsigned)resize->horizontal_filter >= STBIR_FILTER_OTHER)7569 STBIR_RETURN_ERROR_AND_ASSERT( (unsigned)resize->vertical_filter >= STBIR_FILTER_OTHER)7570 #undef STBIR_RETURN_ERROR_AND_ASSERT75717572 if ( splits <= 0 ) 7573 return 0;75747575 STBIR_PROFILE_BUILD_FIRST_START( build );75767577 new_output_subx = resize->output_subx;7578 new_output_suby = resize->output_suby;75797580 // do horizontal clip and scale calcs7581 if ( !stbir__calculate_region_transform( &horizontal.scale_info, resize->output_w, &new_output_subx, resize->output_subw, resize->input_w, resize->input_s0, resize->input_s1 ) )7582 return 0;75837584 // do vertical clip and scale calcs7585 if ( !stbir__calculate_region_transform( &vertical.scale_info, resize->output_h, &new_output_suby, resize->output_subh, resize->input_h, resize->input_t0, resize->input_t1 ) )7586 return 0;75877588 // if nothing to do, just return7589 if ( ( horizontal.scale_info.output_sub_size == 0 ) || ( vertical.scale_info.output_sub_size == 0 ) )7590 return 0;75917592 stbir__set_sampler(&horizontal, resize->horizontal_filter, resize->horizontal_filter_kernel, resize->horizontal_filter_support, resize->horizontal_edge, &horizontal.scale_info, 1, resize->user_data );7593 stbir__get_conservative_extents( &horizontal, &conservative, resize->user_data );7594 stbir__set_sampler(&vertical, resize->vertical_filter, resize->horizontal_filter_kernel, resize->vertical_filter_support, resize->vertical_edge, &vertical.scale_info, 0, resize->user_data );75957596 if ( ( vertical.scale_info.output_sub_size / splits ) < 4 ) // each split should be a minimum of 4 scanlines (handwavey choice)7597 {7598 splits = vertical.scale_info.output_sub_size / 4;7599 if ( splits == 0 ) splits = 1;7600 }76017602 STBIR_PROFILE_BUILD_START( alloc );7603 out_info = stbir__alloc_internal_mem_and_build_samplers( &horizontal, &vertical, &conservative, resize->input_pixel_layout_public, resize->output_pixel_layout_public, splits, new_output_subx, new_output_suby, resize->fast_alpha, resize->user_data STBIR_ONLY_PROFILE_BUILD_SET_INFO );7604 STBIR_PROFILE_BUILD_END( alloc );7605 STBIR_PROFILE_BUILD_END( build );7606 7607 if ( out_info )7608 {7609 resize->splits = splits;7610 resize->samplers = out_info;7611 resize->needs_rebuild = 0;7612 #ifdef STBIR_PROFILE7613 STBIR_MEMCPY( &out_info->profile, &profile_infod.profile, sizeof( out_info->profile ) );7614 #endif7615 return splits;7616 }76177618 return 0;7619}76207621void stbir_free_samplers( STBIR_RESIZE * resize )7622{7623 if ( resize->samplers )7624 {7625 stbir__free_internal_mem( resize->samplers );7626 resize->samplers = 0;7627 resize->called_alloc = 0;7628 }7629}76307631STBIRDEF int stbir_build_samplers_with_splits( STBIR_RESIZE * resize, int splits )7632{7633 if ( ( resize->samplers == 0 ) || ( resize->needs_rebuild ) )7634 {7635 if ( resize->samplers )7636 stbir_free_samplers( resize );76377638 resize->called_alloc = 1;7639 return stbir__perform_build( resize, splits );7640 }76417642 STBIR_PROFILE_BUILD_CLEAR( resize->samplers );7643 7644 return 1;7645}76467647STBIRDEF int stbir_build_samplers( STBIR_RESIZE * resize )7648{7649 return stbir_build_samplers_with_splits( resize, 1 );7650}76517652STBIRDEF int stbir_resize_extended( STBIR_RESIZE * resize )7653{7654 int result;7655 7656 if ( ( resize->samplers == 0 ) || ( resize->needs_rebuild ) )7657 {7658 int alloc_state = resize->called_alloc; // remember allocated state76597660 if ( resize->samplers )7661 {7662 stbir__free_internal_mem( resize->samplers );7663 resize->samplers = 0;7664 }76657666 if ( !stbir_build_samplers( resize ) )7667 return 0;7668 7669 resize->called_alloc = alloc_state;76707671 // if build_samplers succeeded (above), but there are no samplers set, then 7672 // the area to stretch into was zero pixels, so don't do anything and return7673 // success7674 if ( resize->samplers == 0 )7675 return 1;7676 }7677 else7678 {7679 // didn't build anything - clear it7680 STBIR_PROFILE_BUILD_CLEAR( resize->samplers );7681 }768276837684 // update anything that can be changed without recalcing samplers7685 stbir__update_info_from_resize( resize->samplers, resize );76867687 // do resize7688 result = stbir__perform_resize( resize->samplers, 0, resize->splits );76897690 // if we alloced, then free7691 if ( !resize->called_alloc )7692 {7693 stbir_free_samplers( resize );7694 resize->samplers = 0;7695 } 76967697 return result;7698}76997700STBIRDEF int stbir_resize_extended_split( STBIR_RESIZE * resize, int split_start, int split_count )7701{7702 STBIR_ASSERT( resize->samplers );77037704 // if we're just doing the whole thing, call full7705 if ( ( split_start == -1 ) || ( ( split_start == 0 ) && ( split_count == resize->splits ) ) )7706 return stbir_resize_extended( resize );77077708 // you **must** build samplers first when using split resize7709 if ( ( resize->samplers == 0 ) || ( resize->needs_rebuild ) )7710 return 0; 7711 7712 if ( ( split_start >= resize->splits ) || ( split_start < 0 ) || ( ( split_start + split_count ) > resize->splits ) || ( split_count <= 0 ) )7713 return 0;7714 7715 // update anything that can be changed without recalcing samplers7716 stbir__update_info_from_resize( resize->samplers, resize );7717 7718 // do resize7719 return stbir__perform_resize( resize->samplers, split_start, split_count );7720}77217722static int stbir__check_output_stuff( void ** ret_ptr, int * ret_pitch, void * output_pixels, int type_size, int output_w, int output_h, int output_stride_in_bytes, stbir_internal_pixel_layout pixel_layout )7723{7724 size_t size;7725 int pitch;7726 void * ptr;77277728 pitch = output_w * type_size * stbir__pixel_channels[ pixel_layout ];7729 if ( pitch == 0 )7730 return 0;77317732 if ( output_stride_in_bytes == 0 )7733 output_stride_in_bytes = pitch;77347735 if ( output_stride_in_bytes < pitch )7736 return 0;77377738 size = output_stride_in_bytes * output_h;7739 if ( size == 0 )7740 return 0;77417742 *ret_ptr = 0;7743 *ret_pitch = output_stride_in_bytes;77447745 if ( output_pixels == 0 )7746 {7747 ptr = STBIR_MALLOC( size, 0 );7748 if ( ptr == 0 )7749 return 0;77507751 *ret_ptr = ptr;7752 *ret_pitch = pitch;7753 }77547755 return 1; 7756}775777587759STBIRDEF unsigned char * stbir_resize_uint8_linear( const unsigned char *input_pixels , int input_w , int input_h, int input_stride_in_bytes,7760 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,7761 stbir_pixel_layout pixel_layout )7762{7763 STBIR_RESIZE resize;7764 unsigned char * optr;7765 int opitch;77667767 if ( !stbir__check_output_stuff( (void**)&optr, &opitch, output_pixels, sizeof( unsigned char ), output_w, output_h, output_stride_in_bytes, stbir__pixel_layout_convert_public_to_internal[ pixel_layout ] ) )7768 return 0;77697770 stbir_resize_init( &resize, 7771 input_pixels, input_w, input_h, input_stride_in_bytes, 7772 (optr) ? optr : output_pixels, output_w, output_h, opitch, 7773 pixel_layout, STBIR_TYPE_UINT8 );77747775 if ( !stbir_resize_extended( &resize ) )7776 {7777 if ( optr )7778 STBIR_FREE( optr, 0 );7779 return 0;7780 }77817782 return (optr) ? optr : output_pixels;7783}77847785STBIRDEF unsigned char * stbir_resize_uint8_srgb( const unsigned char *input_pixels , int input_w , int input_h, int input_stride_in_bytes,7786 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,7787 stbir_pixel_layout pixel_layout )7788{7789 STBIR_RESIZE resize;7790 unsigned char * optr;7791 int opitch;77927793 if ( !stbir__check_output_stuff( (void**)&optr, &opitch, output_pixels, sizeof( unsigned char ), output_w, output_h, output_stride_in_bytes, stbir__pixel_layout_convert_public_to_internal[ pixel_layout ] ) )7794 return 0;77957796 stbir_resize_init( &resize, 7797 input_pixels, input_w, input_h, input_stride_in_bytes, 7798 (optr) ? optr : output_pixels, output_w, output_h, opitch, 7799 pixel_layout, STBIR_TYPE_UINT8_SRGB );78007801 if ( !stbir_resize_extended( &resize ) )7802 {7803 if ( optr )7804 STBIR_FREE( optr, 0 );7805 return 0;7806 }78077808 return (optr) ? optr : output_pixels;7809}781078117812STBIRDEF float * stbir_resize_float_linear( const float *input_pixels , int input_w , int input_h, int input_stride_in_bytes,7813 float *output_pixels, int output_w, int output_h, int output_stride_in_bytes,7814 stbir_pixel_layout pixel_layout )7815{7816 STBIR_RESIZE resize;7817 float * optr;7818 int opitch;78197820 if ( !stbir__check_output_stuff( (void**)&optr, &opitch, output_pixels, sizeof( float ), output_w, output_h, output_stride_in_bytes, stbir__pixel_layout_convert_public_to_internal[ pixel_layout ] ) )7821 return 0;78227823 stbir_resize_init( &resize, 7824 input_pixels, input_w, input_h, input_stride_in_bytes, 7825 (optr) ? optr : output_pixels, output_w, output_h, opitch, 7826 pixel_layout, STBIR_TYPE_FLOAT );78277828 if ( !stbir_resize_extended( &resize ) )7829 {7830 if ( optr )7831 STBIR_FREE( optr, 0 );7832 return 0;7833 }78347835 return (optr) ? optr : output_pixels;7836}783778387839STBIRDEF void * stbir_resize( const void *input_pixels , int input_w , int input_h, int input_stride_in_bytes,7840 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,7841 stbir_pixel_layout pixel_layout, stbir_datatype data_type, 7842 stbir_edge edge, stbir_filter filter )7843{7844 STBIR_RESIZE resize;7845 float * optr;7846 int opitch;78477848 if ( !stbir__check_output_stuff( (void**)&optr, &opitch, output_pixels, stbir__type_size[data_type], output_w, output_h, output_stride_in_bytes, stbir__pixel_layout_convert_public_to_internal[ pixel_layout ] ) )7849 return 0;78507851 stbir_resize_init( &resize, 7852 input_pixels, input_w, input_h, input_stride_in_bytes, 7853 (optr) ? optr : output_pixels, output_w, output_h, output_stride_in_bytes, 7854 pixel_layout, data_type );78557856 resize.horizontal_edge = edge;7857 resize.vertical_edge = edge;7858 resize.horizontal_filter = filter;7859 resize.vertical_filter = filter;78607861 if ( !stbir_resize_extended( &resize ) )7862 {7863 if ( optr )7864 STBIR_FREE( optr, 0 );7865 return 0;7866 }78677868 return (optr) ? optr : output_pixels;7869}78707871#ifdef STBIR_PROFILE78727873STBIRDEF void stbir_resize_build_profile_info( STBIR_PROFILE_INFO * info, STBIR_RESIZE const * resize )7874{7875 static char const * bdescriptions[6] = { "Building", "Allocating", "Horizontal sampler", "Vertical sampler", "Coefficient cleanup", "Coefficient piovot" } ;7876 stbir__info* samp = resize->samplers;7877 int i;78787879 typedef int testa[ (STBIR__ARRAY_SIZE( bdescriptions ) == (STBIR__ARRAY_SIZE( samp->profile.array )-1) )?1:-1];7880 typedef int testb[ (sizeof( samp->profile.array ) == (sizeof(samp->profile.named)) )?1:-1];7881 typedef int testc[ (sizeof( info->clocks ) >= (sizeof(samp->profile.named)) )?1:-1];78827883 for( i = 0 ; i < STBIR__ARRAY_SIZE( bdescriptions ) ; i++)7884 info->clocks[i] = samp->profile.array[i+1];78857886 info->total_clocks = samp->profile.named.total;7887 info->descriptions = bdescriptions;7888 info->count = STBIR__ARRAY_SIZE( bdescriptions );7889}78907891STBIRDEF void stbir_resize_split_profile_info( STBIR_PROFILE_INFO * info, STBIR_RESIZE const * resize, int split_start, int split_count )7892{7893 static char const * descriptions[7] = { "Looping", "Vertical sampling", "Horizontal sampling", "Scanline input", "Scanline output", "Alpha weighting", "Alpha unweighting" };7894 stbir__per_split_info * split_info;7895 int s, i;78967897 typedef int testa[ (STBIR__ARRAY_SIZE( descriptions ) == (STBIR__ARRAY_SIZE( split_info->profile.array )-1) )?1:-1];7898 typedef int testb[ (sizeof( split_info->profile.array ) == (sizeof(split_info->profile.named)) )?1:-1];7899 typedef int testc[ (sizeof( info->clocks ) >= (sizeof(split_info->profile.named)) )?1:-1];79007901 if ( split_start == -1 )7902 {7903 split_start = 0;7904 split_count = resize->samplers->splits;7905 }79067907 if ( ( split_start >= resize->splits ) || ( split_start < 0 ) || ( ( split_start + split_count ) > resize->splits ) || ( split_count <= 0 ) )7908 {7909 info->total_clocks = 0;7910 info->descriptions = 0;7911 info->count = 0;7912 return;7913 }79147915 split_info = resize->samplers->split_info + split_start;79167917 // sum up the profile from all the splits7918 for( i = 0 ; i < STBIR__ARRAY_SIZE( descriptions ) ; i++ )7919 {7920 stbir_uint64 sum = 0;7921 for( s = 0 ; s < split_count ; s++ )7922 sum += split_info[s].profile.array[i+1];7923 info->clocks[i] = sum;7924 }79257926 info->total_clocks = split_info->profile.named.total;7927 info->descriptions = descriptions;7928 info->count = STBIR__ARRAY_SIZE( descriptions );7929}79307931STBIRDEF void stbir_resize_extended_profile_info( STBIR_PROFILE_INFO * info, STBIR_RESIZE const * resize )7932{7933 stbir_resize_split_profile_info( info, resize, -1, 0 );7934}79357936#endif // STBIR_PROFILE79377938#undef STBIR_BGR7939#undef STBIR_1CHANNEL7940#undef STBIR_2CHANNEL7941#undef STBIR_RGB7942#undef STBIR_RGBA7943#undef STBIR_4CHANNEL7944#undef STBIR_BGRA7945#undef STBIR_ARGB7946#undef STBIR_ABGR7947#undef STBIR_RA7948#undef STBIR_AR7949#undef STBIR_RGBA_PM7950#undef STBIR_BGRA_PM7951#undef STBIR_ARGB_PM7952#undef STBIR_ABGR_PM7953#undef STBIR_RA_PM7954#undef STBIR_AR_PM79557956#endif // STB_IMAGE_RESIZE_IMPLEMENTATION79577958#else // STB_IMAGE_RESIZE_HORIZONTALS&STB_IMAGE_RESIZE_DO_VERTICALS79597960// we reinclude the header file to define all the horizontal functions7961// specializing each function for the number of coeffs is 20-40% faster *OVERALL* 79627963// by including the header file again this way, we can still debug the functions79647965#define STBIR_strs_join2( start, mid, end ) start##mid##end7966#define STBIR_strs_join1( start, mid, end ) STBIR_strs_join2( start, mid, end )79677968#define STBIR_strs_join24( start, mid1, mid2, end ) start##mid1##mid2##end7969#define STBIR_strs_join14( start, mid1, mid2, end ) STBIR_strs_join24( start, mid1, mid2, end )79707971#ifdef STB_IMAGE_RESIZE_DO_CODERS79727973#ifdef stbir__decode_suffix7974#define STBIR__CODER_NAME( name ) STBIR_strs_join1( name, _, stbir__decode_suffix )7975#else7976#define STBIR__CODER_NAME( name ) name7977#endif79787979#ifdef stbir__decode_swizzle7980#define stbir__decode_simdf8_flip(reg) STBIR_strs_join1( STBIR_strs_join1( STBIR_strs_join1( STBIR_strs_join1( stbir__simdf8_0123to,stbir__decode_order0,stbir__decode_order1),stbir__decode_order2,stbir__decode_order3),stbir__decode_order0,stbir__decode_order1),stbir__decode_order2,stbir__decode_order3)(reg, reg)7981#define stbir__decode_simdf4_flip(reg) STBIR_strs_join1( STBIR_strs_join1( stbir__simdf_0123to,stbir__decode_order0,stbir__decode_order1),stbir__decode_order2,stbir__decode_order3)(reg, reg)7982#define stbir__encode_simdf8_unflip(reg) STBIR_strs_join1( STBIR_strs_join1( STBIR_strs_join1( STBIR_strs_join1( stbir__simdf8_0123to,stbir__encode_order0,stbir__encode_order1),stbir__encode_order2,stbir__encode_order3),stbir__encode_order0,stbir__encode_order1),stbir__encode_order2,stbir__encode_order3)(reg, reg)7983#define stbir__encode_simdf4_unflip(reg) STBIR_strs_join1( STBIR_strs_join1( stbir__simdf_0123to,stbir__encode_order0,stbir__encode_order1),stbir__encode_order2,stbir__encode_order3)(reg, reg)7984#else7985#define stbir__decode_order0 07986#define stbir__decode_order1 17987#define stbir__decode_order2 27988#define stbir__decode_order3 37989#define stbir__encode_order0 07990#define stbir__encode_order1 17991#define stbir__encode_order2 27992#define stbir__encode_order3 37993#define stbir__decode_simdf8_flip(reg)7994#define stbir__decode_simdf4_flip(reg) 7995#define stbir__encode_simdf8_unflip(reg)7996#define stbir__encode_simdf4_unflip(reg) 7997#endif79987999#ifdef STBIR_SIMD88000#define stbir__encode_simdfX_unflip stbir__encode_simdf8_unflip8001#else8002#define stbir__encode_simdfX_unflip stbir__encode_simdf4_unflip8003#endif 80048005static void STBIR__CODER_NAME( stbir__decode_uint8_linear_scaled )( float * decodep, int width_times_channels, void const * inputp )8006{8007 float STBIR_STREAMOUT_PTR( * ) decode = decodep;8008 float * decode_end = (float*) decode + width_times_channels;8009 unsigned char const * input = (unsigned char const*)inputp;80108011 #ifdef STBIR_SIMD8012 unsigned char const * end_input_m16 = input + width_times_channels - 16;8013 if ( width_times_channels >= 16 )8014 {8015 decode_end -= 16;8016 for(;;)8017 {8018 #ifdef STBIR_SIMD88019 stbir__simdi i; stbir__simdi8 o0,o1;8020 stbir__simdf8 of0, of1;8021 STBIR_NO_UNROLL(decode);8022 stbir__simdi_load( i, input );8023 stbir__simdi8_expand_u8_to_u32( o0, o1, i );8024 stbir__simdi8_convert_i32_to_float( of0, o0 );8025 stbir__simdi8_convert_i32_to_float( of1, o1 );8026 stbir__simdf8_mult( of0, of0, STBIR_max_uint8_as_float_inverted8);8027 stbir__simdf8_mult( of1, of1, STBIR_max_uint8_as_float_inverted8);8028 stbir__decode_simdf8_flip( of0 );8029 stbir__decode_simdf8_flip( of1 );8030 stbir__simdf8_store( decode + 0, of0 );8031 stbir__simdf8_store( decode + 8, of1 );8032 #else8033 stbir__simdi i, o0, o1, o2, o3;8034 stbir__simdf of0, of1, of2, of3;8035 STBIR_NO_UNROLL(decode);8036 stbir__simdi_load( i, input );8037 stbir__simdi_expand_u8_to_u32( o0,o1,o2,o3,i);8038 stbir__simdi_convert_i32_to_float( of0, o0 );8039 stbir__simdi_convert_i32_to_float( of1, o1 );8040 stbir__simdi_convert_i32_to_float( of2, o2 );8041 stbir__simdi_convert_i32_to_float( of3, o3 );8042 stbir__simdf_mult( of0, of0, STBIR__CONSTF(STBIR_max_uint8_as_float_inverted) );8043 stbir__simdf_mult( of1, of1, STBIR__CONSTF(STBIR_max_uint8_as_float_inverted) );8044 stbir__simdf_mult( of2, of2, STBIR__CONSTF(STBIR_max_uint8_as_float_inverted) );8045 stbir__simdf_mult( of3, of3, STBIR__CONSTF(STBIR_max_uint8_as_float_inverted) );8046 stbir__decode_simdf4_flip( of0 );8047 stbir__decode_simdf4_flip( of1 );8048 stbir__decode_simdf4_flip( of2 );8049 stbir__decode_simdf4_flip( of3 );8050 stbir__simdf_store( decode + 0, of0 );8051 stbir__simdf_store( decode + 4, of1 );8052 stbir__simdf_store( decode + 8, of2 );8053 stbir__simdf_store( decode + 12, of3 );8054 #endif8055 decode += 16;8056 input += 16;8057 if ( decode <= decode_end ) 8058 continue;8059 if ( decode == ( decode_end + 16 ) )8060 break;8061 decode = decode_end; // backup and do last couple8062 input = end_input_m16;8063 }8064 return;8065 }8066 #endif80678068 // try to do blocks of 4 when you can8069 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four8070 decode += 4;8071 while( decode <= decode_end )8072 {8073 STBIR_SIMD_NO_UNROLL(decode);8074 decode[0-4] = ((float)(input[stbir__decode_order0])) * stbir__max_uint8_as_float_inverted;8075 decode[1-4] = ((float)(input[stbir__decode_order1])) * stbir__max_uint8_as_float_inverted;8076 decode[2-4] = ((float)(input[stbir__decode_order2])) * stbir__max_uint8_as_float_inverted;8077 decode[3-4] = ((float)(input[stbir__decode_order3])) * stbir__max_uint8_as_float_inverted;8078 decode += 4;8079 input += 4;8080 }8081 decode -= 4;8082 #endif80838084 // do the remnants8085 #if stbir__coder_min_num < 48086 while( decode < decode_end )8087 {8088 STBIR_NO_UNROLL(decode);8089 decode[0] = ((float)(input[stbir__decode_order0])) * stbir__max_uint8_as_float_inverted;8090 #if stbir__coder_min_num >= 28091 decode[1] = ((float)(input[stbir__decode_order1])) * stbir__max_uint8_as_float_inverted;8092 #endif8093 #if stbir__coder_min_num >= 38094 decode[2] = ((float)(input[stbir__decode_order2])) * stbir__max_uint8_as_float_inverted;8095 #endif8096 decode += stbir__coder_min_num;8097 input += stbir__coder_min_num;8098 }8099 #endif8100}81018102static void STBIR__CODER_NAME( stbir__encode_uint8_linear_scaled )( void * outputp, int width_times_channels, float const * encode )8103{8104 unsigned char STBIR_SIMD_STREAMOUT_PTR( * ) output = (unsigned char *) outputp;8105 unsigned char * end_output = ( (unsigned char *) output ) + width_times_channels;81068107 #ifdef STBIR_SIMD8108 if ( width_times_channels >= stbir__simdfX_float_count*2 )8109 {8110 float const * end_encode_m8 = encode + width_times_channels - stbir__simdfX_float_count*2;8111 end_output -= stbir__simdfX_float_count*2;8112 for(;;)8113 {8114 stbir__simdfX e0, e1;8115 stbir__simdi i;8116 STBIR_SIMD_NO_UNROLL(encode);8117 stbir__simdfX_madd_mem( e0, STBIR_simd_point5X, STBIR_max_uint8_as_floatX, encode );8118 stbir__simdfX_madd_mem( e1, STBIR_simd_point5X, STBIR_max_uint8_as_floatX, encode+stbir__simdfX_float_count );8119 stbir__encode_simdfX_unflip( e0 );8120 stbir__encode_simdfX_unflip( e1 );8121 #ifdef STBIR_SIMD88122 stbir__simdf8_pack_to_16bytes( i, e0, e1 ); 8123 stbir__simdi_store( output, i );8124 #else8125 stbir__simdf_pack_to_8bytes( i, e0, e1 ); 8126 stbir__simdi_store2( output, i );8127 #endif8128 encode += stbir__simdfX_float_count*2;8129 output += stbir__simdfX_float_count*2;8130 if ( output <= end_output ) 8131 continue;8132 if ( output == ( end_output + stbir__simdfX_float_count*2 ) )8133 break;8134 output = end_output; // backup and do last couple8135 encode = end_encode_m8;8136 }8137 return;8138 }81398140 // try to do blocks of 4 when you can8141 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four8142 output += 4;8143 while( output <= end_output )8144 {8145 stbir__simdf e0;8146 stbir__simdi i0;8147 STBIR_NO_UNROLL(encode);8148 stbir__simdf_load( e0, encode );8149 stbir__simdf_madd( e0, STBIR__CONSTF(STBIR_simd_point5), STBIR__CONSTF(STBIR_max_uint8_as_float), e0 );8150 stbir__encode_simdf4_unflip( e0 );8151 stbir__simdf_pack_to_8bytes( i0, e0, e0 ); // only use first 48152 *(int*)(output-4) = stbir__simdi_to_int( i0 );8153 output += 4;8154 encode += 4;8155 }8156 output -= 4;8157 #endif81588159 // do the remnants8160 #if stbir__coder_min_num < 48161 while( output < end_output )8162 {8163 stbir__simdf e0; 8164 STBIR_NO_UNROLL(encode);8165 stbir__simdf_madd1_mem( e0, STBIR__CONSTF(STBIR_simd_point5), STBIR__CONSTF(STBIR_max_uint8_as_float), encode+stbir__encode_order0 ); output[0] = stbir__simdf_convert_float_to_uint8( e0 );8166 #if stbir__coder_min_num >= 28167 stbir__simdf_madd1_mem( e0, STBIR__CONSTF(STBIR_simd_point5), STBIR__CONSTF(STBIR_max_uint8_as_float), encode+stbir__encode_order1 ); output[1] = stbir__simdf_convert_float_to_uint8( e0 );8168 #endif8169 #if stbir__coder_min_num >= 38170 stbir__simdf_madd1_mem( e0, STBIR__CONSTF(STBIR_simd_point5), STBIR__CONSTF(STBIR_max_uint8_as_float), encode+stbir__encode_order2 ); output[2] = stbir__simdf_convert_float_to_uint8( e0 );8171 #endif8172 output += stbir__coder_min_num;8173 encode += stbir__coder_min_num;8174 }8175 #endif8176 8177 #else81788179 // try to do blocks of 4 when you can8180 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four8181 output += 4;8182 while( output <= end_output )8183 {8184 float f;8185 f = encode[stbir__encode_order0] * stbir__max_uint8_as_float + 0.5f; STBIR_CLAMP(f, 0, 255); output[0-4] = (unsigned char)f;8186 f = encode[stbir__encode_order1] * stbir__max_uint8_as_float + 0.5f; STBIR_CLAMP(f, 0, 255); output[1-4] = (unsigned char)f;8187 f = encode[stbir__encode_order2] * stbir__max_uint8_as_float + 0.5f; STBIR_CLAMP(f, 0, 255); output[2-4] = (unsigned char)f;8188 f = encode[stbir__encode_order3] * stbir__max_uint8_as_float + 0.5f; STBIR_CLAMP(f, 0, 255); output[3-4] = (unsigned char)f;8189 output += 4;8190 encode += 4;8191 }8192 output -= 4;8193 #endif81948195 // do the remnants8196 #if stbir__coder_min_num < 48197 while( output < end_output )8198 {8199 float f;8200 STBIR_NO_UNROLL(encode);8201 f = encode[stbir__encode_order0] * stbir__max_uint8_as_float + 0.5f; STBIR_CLAMP(f, 0, 255); output[0] = (unsigned char)f;8202 #if stbir__coder_min_num >= 28203 f = encode[stbir__encode_order1] * stbir__max_uint8_as_float + 0.5f; STBIR_CLAMP(f, 0, 255); output[1] = (unsigned char)f;8204 #endif8205 #if stbir__coder_min_num >= 38206 f = encode[stbir__encode_order2] * stbir__max_uint8_as_float + 0.5f; STBIR_CLAMP(f, 0, 255); output[2] = (unsigned char)f;8207 #endif8208 output += stbir__coder_min_num;8209 encode += stbir__coder_min_num;8210 }8211 #endif8212 #endif8213}82148215static void STBIR__CODER_NAME(stbir__decode_uint8_linear)( float * decodep, int width_times_channels, void const * inputp )8216{8217 float STBIR_STREAMOUT_PTR( * ) decode = decodep;8218 float * decode_end = (float*) decode + width_times_channels;8219 unsigned char const * input = (unsigned char const*)inputp;82208221 #ifdef STBIR_SIMD8222 unsigned char const * end_input_m16 = input + width_times_channels - 16;8223 if ( width_times_channels >= 16 )8224 {8225 decode_end -= 16;8226 for(;;)8227 {8228 #ifdef STBIR_SIMD88229 stbir__simdi i; stbir__simdi8 o0,o1;8230 stbir__simdf8 of0, of1;8231 STBIR_NO_UNROLL(decode);8232 stbir__simdi_load( i, input );8233 stbir__simdi8_expand_u8_to_u32( o0, o1, i );8234 stbir__simdi8_convert_i32_to_float( of0, o0 );8235 stbir__simdi8_convert_i32_to_float( of1, o1 );8236 stbir__decode_simdf8_flip( of0 );8237 stbir__decode_simdf8_flip( of1 );8238 stbir__simdf8_store( decode + 0, of0 );8239 stbir__simdf8_store( decode + 8, of1 );8240 #else8241 stbir__simdi i, o0, o1, o2, o3;8242 stbir__simdf of0, of1, of2, of3;8243 STBIR_NO_UNROLL(decode);8244 stbir__simdi_load( i, input );8245 stbir__simdi_expand_u8_to_u32( o0,o1,o2,o3,i);8246 stbir__simdi_convert_i32_to_float( of0, o0 );8247 stbir__simdi_convert_i32_to_float( of1, o1 );8248 stbir__simdi_convert_i32_to_float( of2, o2 );8249 stbir__simdi_convert_i32_to_float( of3, o3 );8250 stbir__decode_simdf4_flip( of0 );8251 stbir__decode_simdf4_flip( of1 );8252 stbir__decode_simdf4_flip( of2 );8253 stbir__decode_simdf4_flip( of3 );8254 stbir__simdf_store( decode + 0, of0 );8255 stbir__simdf_store( decode + 4, of1 );8256 stbir__simdf_store( decode + 8, of2 );8257 stbir__simdf_store( decode + 12, of3 );8258#endif8259 decode += 16;8260 input += 16;8261 if ( decode <= decode_end ) 8262 continue;8263 if ( decode == ( decode_end + 16 ) )8264 break;8265 decode = decode_end; // backup and do last couple8266 input = end_input_m16;8267 }8268 return;8269 }8270 #endif82718272 // try to do blocks of 4 when you can8273 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four8274 decode += 4;8275 while( decode <= decode_end )8276 {8277 STBIR_SIMD_NO_UNROLL(decode);8278 decode[0-4] = ((float)(input[stbir__decode_order0]));8279 decode[1-4] = ((float)(input[stbir__decode_order1]));8280 decode[2-4] = ((float)(input[stbir__decode_order2]));8281 decode[3-4] = ((float)(input[stbir__decode_order3]));8282 decode += 4;8283 input += 4;8284 }8285 decode -= 4;8286 #endif82878288 // do the remnants8289 #if stbir__coder_min_num < 48290 while( decode < decode_end )8291 {8292 STBIR_NO_UNROLL(decode);8293 decode[0] = ((float)(input[stbir__decode_order0]));8294 #if stbir__coder_min_num >= 28295 decode[1] = ((float)(input[stbir__decode_order1]));8296 #endif8297 #if stbir__coder_min_num >= 38298 decode[2] = ((float)(input[stbir__decode_order2]));8299 #endif8300 decode += stbir__coder_min_num;8301 input += stbir__coder_min_num;8302 }8303 #endif8304}83058306static void STBIR__CODER_NAME( stbir__encode_uint8_linear )( void * outputp, int width_times_channels, float const * encode )8307{8308 unsigned char STBIR_SIMD_STREAMOUT_PTR( * ) output = (unsigned char *) outputp;8309 unsigned char * end_output = ( (unsigned char *) output ) + width_times_channels;83108311 #ifdef STBIR_SIMD8312 if ( width_times_channels >= stbir__simdfX_float_count*2 )8313 {8314 float const * end_encode_m8 = encode + width_times_channels - stbir__simdfX_float_count*2;8315 end_output -= stbir__simdfX_float_count*2;8316 for(;;)8317 {8318 stbir__simdfX e0, e1;8319 stbir__simdi i;8320 STBIR_SIMD_NO_UNROLL(encode);8321 stbir__simdfX_add_mem( e0, STBIR_simd_point5X, encode );8322 stbir__simdfX_add_mem( e1, STBIR_simd_point5X, encode+stbir__simdfX_float_count );8323 stbir__encode_simdfX_unflip( e0 );8324 stbir__encode_simdfX_unflip( e1 );8325 #ifdef STBIR_SIMD88326 stbir__simdf8_pack_to_16bytes( i, e0, e1 ); 8327 stbir__simdi_store( output, i );8328 #else8329 stbir__simdf_pack_to_8bytes( i, e0, e1 ); 8330 stbir__simdi_store2( output, i );8331 #endif8332 encode += stbir__simdfX_float_count*2;8333 output += stbir__simdfX_float_count*2;8334 if ( output <= end_output ) 8335 continue;8336 if ( output == ( end_output + stbir__simdfX_float_count*2 ) )8337 break;8338 output = end_output; // backup and do last couple8339 encode = end_encode_m8;8340 }8341 return;8342 }83438344 // try to do blocks of 4 when you can8345 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four8346 output += 4;8347 while( output <= end_output )8348 {8349 stbir__simdf e0;8350 stbir__simdi i0;8351 STBIR_NO_UNROLL(encode);8352 stbir__simdf_load( e0, encode );8353 stbir__simdf_add( e0, STBIR__CONSTF(STBIR_simd_point5), e0 );8354 stbir__encode_simdf4_unflip( e0 );8355 stbir__simdf_pack_to_8bytes( i0, e0, e0 ); // only use first 48356 *(int*)(output-4) = stbir__simdi_to_int( i0 );8357 output += 4;8358 encode += 4;8359 }8360 output -= 4;8361 #endif83628363 #else83648365 // try to do blocks of 4 when you can8366 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four8367 output += 4;8368 while( output <= end_output )8369 {8370 float f;8371 f = encode[stbir__encode_order0] + 0.5f; STBIR_CLAMP(f, 0, 255); output[0-4] = (unsigned char)f;8372 f = encode[stbir__encode_order1] + 0.5f; STBIR_CLAMP(f, 0, 255); output[1-4] = (unsigned char)f;8373 f = encode[stbir__encode_order2] + 0.5f; STBIR_CLAMP(f, 0, 255); output[2-4] = (unsigned char)f;8374 f = encode[stbir__encode_order3] + 0.5f; STBIR_CLAMP(f, 0, 255); output[3-4] = (unsigned char)f;8375 output += 4;8376 encode += 4;8377 }8378 output -= 4;8379 #endif83808381 #endif83828383 // do the remnants8384 #if stbir__coder_min_num < 48385 while( output < end_output )8386 {8387 float f;8388 STBIR_NO_UNROLL(encode);8389 f = encode[stbir__encode_order0] + 0.5f; STBIR_CLAMP(f, 0, 255); output[0] = (unsigned char)f;8390 #if stbir__coder_min_num >= 28391 f = encode[stbir__encode_order1] + 0.5f; STBIR_CLAMP(f, 0, 255); output[1] = (unsigned char)f;8392 #endif8393 #if stbir__coder_min_num >= 38394 f = encode[stbir__encode_order2] + 0.5f; STBIR_CLAMP(f, 0, 255); output[2] = (unsigned char)f;8395 #endif8396 output += stbir__coder_min_num;8397 encode += stbir__coder_min_num;8398 }8399 #endif8400}84018402static void STBIR__CODER_NAME(stbir__decode_uint8_srgb)( float * decodep, int width_times_channels, void const * inputp )8403{8404 float STBIR_STREAMOUT_PTR( * ) decode = decodep;8405 float const * decode_end = (float*) decode + width_times_channels;8406 unsigned char const * input = (unsigned char const *)inputp;84078408 // try to do blocks of 4 when you can8409 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four8410 decode += 4;8411 while( decode <= decode_end )8412 {8413 decode[0-4] = stbir__srgb_uchar_to_linear_float[ input[ stbir__decode_order0 ] ];8414 decode[1-4] = stbir__srgb_uchar_to_linear_float[ input[ stbir__decode_order1 ] ];8415 decode[2-4] = stbir__srgb_uchar_to_linear_float[ input[ stbir__decode_order2 ] ];8416 decode[3-4] = stbir__srgb_uchar_to_linear_float[ input[ stbir__decode_order3 ] ];8417 decode += 4;8418 input += 4;8419 }8420 decode -= 4;8421 #endif84228423 // do the remnants8424 #if stbir__coder_min_num < 48425 while( decode < decode_end )8426 {8427 STBIR_NO_UNROLL(decode);8428 decode[0] = stbir__srgb_uchar_to_linear_float[ input[ stbir__decode_order0 ] ];8429 #if stbir__coder_min_num >= 28430 decode[1] = stbir__srgb_uchar_to_linear_float[ input[ stbir__decode_order1 ] ];8431 #endif8432 #if stbir__coder_min_num >= 38433 decode[2] = stbir__srgb_uchar_to_linear_float[ input[ stbir__decode_order2 ] ];8434 #endif8435 decode += stbir__coder_min_num;8436 input += stbir__coder_min_num;8437 }8438 #endif8439}84408441#define stbir__min_max_shift20( i, f ) \8442 stbir__simdf_max( f, f, stbir_simdf_casti(STBIR__CONSTI( STBIR_almost_zero )) ); \8443 stbir__simdf_min( f, f, stbir_simdf_casti(STBIR__CONSTI( STBIR_almost_one )) ); \8444 stbir__simdi_32shr( i, stbir_simdi_castf( f ), 20 ); 84458446#define stbir__scale_and_convert( i, f ) \8447 stbir__simdf_madd( f, STBIR__CONSTF( STBIR_simd_point5 ), STBIR__CONSTF( STBIR_max_uint8_as_float ), f ); \8448 stbir__simdf_max( f, f, stbir__simdf_zeroP() ); \8449 stbir__simdf_min( f, f, STBIR__CONSTF( STBIR_max_uint8_as_float ) ); \8450 stbir__simdf_convert_float_to_i32( i, f );84518452#define stbir__linear_to_srgb_finish( i, f ) \8453{ \8454 stbir__simdi temp; \8455 stbir__simdi_32shr( temp, stbir_simdi_castf( f ), 12 ) ; \8456 stbir__simdi_and( temp, temp, STBIR__CONSTI(STBIR_mastissa_mask) ); \8457 stbir__simdi_or( temp, temp, STBIR__CONSTI(STBIR_topscale) ); \8458 stbir__simdi_16madd( i, i, temp ); \8459 stbir__simdi_32shr( i, i, 16 ); \8460}84618462#define stbir__simdi_table_lookup2( v0,v1, table ) \8463{ \8464 stbir__simdi_u32 temp0,temp1; \8465 temp0.m128i_i128 = v0; \8466 temp1.m128i_i128 = v1; \8467 temp0.m128i_u32[0] = table[temp0.m128i_i32[0]]; temp0.m128i_u32[1] = table[temp0.m128i_i32[1]]; temp0.m128i_u32[2] = table[temp0.m128i_i32[2]]; temp0.m128i_u32[3] = table[temp0.m128i_i32[3]]; \8468 temp1.m128i_u32[0] = table[temp1.m128i_i32[0]]; temp1.m128i_u32[1] = table[temp1.m128i_i32[1]]; temp1.m128i_u32[2] = table[temp1.m128i_i32[2]]; temp1.m128i_u32[3] = table[temp1.m128i_i32[3]]; \8469 v0 = temp0.m128i_i128; \8470 v1 = temp1.m128i_i128; \8471} 84728473#define stbir__simdi_table_lookup3( v0,v1,v2, table ) \8474{ \8475 stbir__simdi_u32 temp0,temp1,temp2; \8476 temp0.m128i_i128 = v0; \8477 temp1.m128i_i128 = v1; \8478 temp2.m128i_i128 = v2; \8479 temp0.m128i_u32[0] = table[temp0.m128i_i32[0]]; temp0.m128i_u32[1] = table[temp0.m128i_i32[1]]; temp0.m128i_u32[2] = table[temp0.m128i_i32[2]]; temp0.m128i_u32[3] = table[temp0.m128i_i32[3]]; \8480 temp1.m128i_u32[0] = table[temp1.m128i_i32[0]]; temp1.m128i_u32[1] = table[temp1.m128i_i32[1]]; temp1.m128i_u32[2] = table[temp1.m128i_i32[2]]; temp1.m128i_u32[3] = table[temp1.m128i_i32[3]]; \8481 temp2.m128i_u32[0] = table[temp2.m128i_i32[0]]; temp2.m128i_u32[1] = table[temp2.m128i_i32[1]]; temp2.m128i_u32[2] = table[temp2.m128i_i32[2]]; temp2.m128i_u32[3] = table[temp2.m128i_i32[3]]; \8482 v0 = temp0.m128i_i128; \8483 v1 = temp1.m128i_i128; \8484 v2 = temp2.m128i_i128; \8485}84868487#define stbir__simdi_table_lookup4( v0,v1,v2,v3, table ) \8488{ \8489 stbir__simdi_u32 temp0,temp1,temp2,temp3; \8490 temp0.m128i_i128 = v0; \8491 temp1.m128i_i128 = v1; \8492 temp2.m128i_i128 = v2; \8493 temp3.m128i_i128 = v3; \8494 temp0.m128i_u32[0] = table[temp0.m128i_i32[0]]; temp0.m128i_u32[1] = table[temp0.m128i_i32[1]]; temp0.m128i_u32[2] = table[temp0.m128i_i32[2]]; temp0.m128i_u32[3] = table[temp0.m128i_i32[3]]; \8495 temp1.m128i_u32[0] = table[temp1.m128i_i32[0]]; temp1.m128i_u32[1] = table[temp1.m128i_i32[1]]; temp1.m128i_u32[2] = table[temp1.m128i_i32[2]]; temp1.m128i_u32[3] = table[temp1.m128i_i32[3]]; \8496 temp2.m128i_u32[0] = table[temp2.m128i_i32[0]]; temp2.m128i_u32[1] = table[temp2.m128i_i32[1]]; temp2.m128i_u32[2] = table[temp2.m128i_i32[2]]; temp2.m128i_u32[3] = table[temp2.m128i_i32[3]]; \8497 temp3.m128i_u32[0] = table[temp3.m128i_i32[0]]; temp3.m128i_u32[1] = table[temp3.m128i_i32[1]]; temp3.m128i_u32[2] = table[temp3.m128i_i32[2]]; temp3.m128i_u32[3] = table[temp3.m128i_i32[3]]; \8498 v0 = temp0.m128i_i128; \8499 v1 = temp1.m128i_i128; \8500 v2 = temp2.m128i_i128; \8501 v3 = temp3.m128i_i128; \8502} 85038504static void STBIR__CODER_NAME( stbir__encode_uint8_srgb )( void * outputp, int width_times_channels, float const * encode )8505{8506 unsigned char STBIR_SIMD_STREAMOUT_PTR( * ) output = (unsigned char*) outputp;8507 unsigned char * end_output = ( (unsigned char*) output ) + width_times_channels;85088509 #ifdef STBIR_SIMD8510 stbir_uint32 const * to_srgb = fp32_to_srgb8_tab4 - (127-13)*8;85118512 if ( width_times_channels >= 16 )8513 {8514 float const * end_encode_m16 = encode + width_times_channels - 16;8515 end_output -= 16;8516 for(;;)8517 {8518 stbir__simdf f0, f1, f2, f3;8519 stbir__simdi i0, i1, i2, i3; 8520 STBIR_SIMD_NO_UNROLL(encode);85218522 stbir__simdf_load4_transposed( f0, f1, f2, f3, encode );85238524 stbir__min_max_shift20( i0, f0 );8525 stbir__min_max_shift20( i1, f1 );8526 stbir__min_max_shift20( i2, f2 );8527 stbir__min_max_shift20( i3, f3 );8528 8529 stbir__simdi_table_lookup4( i0, i1, i2, i3, to_srgb );8530 8531 stbir__linear_to_srgb_finish( i0, f0 );8532 stbir__linear_to_srgb_finish( i1, f1 );8533 stbir__linear_to_srgb_finish( i2, f2 );8534 stbir__linear_to_srgb_finish( i3, f3 );85358536 stbir__interleave_pack_and_store_16_u8( output, STBIR_strs_join1(i, ,stbir__encode_order0), STBIR_strs_join1(i, ,stbir__encode_order1), STBIR_strs_join1(i, ,stbir__encode_order2), STBIR_strs_join1(i, ,stbir__encode_order3) );85378538 encode += 16;8539 output += 16;8540 if ( output <= end_output ) 8541 continue;8542 if ( output == ( end_output + 16 ) )8543 break;8544 output = end_output; // backup and do last couple8545 encode = end_encode_m16;8546 }8547 return;8548 }8549 #endif85508551 // try to do blocks of 4 when you can8552 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four8553 output += 4;8554 while ( output <= end_output )8555 {8556 STBIR_SIMD_NO_UNROLL(encode);85578558 output[0-4] = stbir__linear_to_srgb_uchar( encode[stbir__encode_order0] );8559 output[1-4] = stbir__linear_to_srgb_uchar( encode[stbir__encode_order1] );8560 output[2-4] = stbir__linear_to_srgb_uchar( encode[stbir__encode_order2] );8561 output[3-4] = stbir__linear_to_srgb_uchar( encode[stbir__encode_order3] );85628563 output += 4;8564 encode += 4;8565 }8566 output -= 4;8567 #endif85688569 // do the remnants8570 #if stbir__coder_min_num < 48571 while( output < end_output ) 8572 {8573 STBIR_NO_UNROLL(encode);8574 output[0] = stbir__linear_to_srgb_uchar( encode[stbir__encode_order0] );8575 #if stbir__coder_min_num >= 28576 output[1] = stbir__linear_to_srgb_uchar( encode[stbir__encode_order1] );8577 #endif8578 #if stbir__coder_min_num >= 38579 output[2] = stbir__linear_to_srgb_uchar( encode[stbir__encode_order2] );8580 #endif8581 output += stbir__coder_min_num;8582 encode += stbir__coder_min_num;8583 }8584 #endif8585}85868587#if ( stbir__coder_min_num == 4 ) || ( ( stbir__coder_min_num == 1 ) && ( !defined(stbir__decode_swizzle) ) )85888589static void STBIR__CODER_NAME(stbir__decode_uint8_srgb4_linearalpha)( float * decodep, int width_times_channels, void const * inputp )8590{8591 float STBIR_STREAMOUT_PTR( * ) decode = decodep;8592 float const * decode_end = (float*) decode + width_times_channels;8593 unsigned char const * input = (unsigned char const *)inputp;8594 do {8595 decode[0] = stbir__srgb_uchar_to_linear_float[ input[stbir__decode_order0] ];8596 decode[1] = stbir__srgb_uchar_to_linear_float[ input[stbir__decode_order1] ];8597 decode[2] = stbir__srgb_uchar_to_linear_float[ input[stbir__decode_order2] ];8598 decode[3] = ( (float) input[stbir__decode_order3] ) * stbir__max_uint8_as_float_inverted;8599 input += 4;8600 decode += 4;8601 } while( decode < decode_end );8602}860386048605static void STBIR__CODER_NAME( stbir__encode_uint8_srgb4_linearalpha )( void * outputp, int width_times_channels, float const * encode )8606{8607 unsigned char STBIR_SIMD_STREAMOUT_PTR( * ) output = (unsigned char*) outputp;8608 unsigned char * end_output = ( (unsigned char*) output ) + width_times_channels;86098610 #ifdef STBIR_SIMD8611 stbir_uint32 const * to_srgb = fp32_to_srgb8_tab4 - (127-13)*8;86128613 if ( width_times_channels >= 16 )8614 {8615 float const * end_encode_m16 = encode + width_times_channels - 16;8616 end_output -= 16;8617 for(;;)8618 {8619 stbir__simdf f0, f1, f2, f3;8620 stbir__simdi i0, i1, i2, i3;86218622 STBIR_SIMD_NO_UNROLL(encode);8623 stbir__simdf_load4_transposed( f0, f1, f2, f3, encode );86248625 stbir__min_max_shift20( i0, f0 );8626 stbir__min_max_shift20( i1, f1 );8627 stbir__min_max_shift20( i2, f2 );8628 stbir__scale_and_convert( i3, f3 ); 8629 8630 stbir__simdi_table_lookup3( i0, i1, i2, to_srgb );8631 8632 stbir__linear_to_srgb_finish( i0, f0 );8633 stbir__linear_to_srgb_finish( i1, f1 );8634 stbir__linear_to_srgb_finish( i2, f2 );86358636 stbir__interleave_pack_and_store_16_u8( output, STBIR_strs_join1(i, ,stbir__encode_order0), STBIR_strs_join1(i, ,stbir__encode_order1), STBIR_strs_join1(i, ,stbir__encode_order2), STBIR_strs_join1(i, ,stbir__encode_order3) );86378638 output += 16;8639 encode += 16;86408641 if ( output <= end_output ) 8642 continue;8643 if ( output == ( end_output + 16 ) )8644 break;8645 output = end_output; // backup and do last couple8646 encode = end_encode_m16;8647 }8648 return;8649 }8650 #endif86518652 do {8653 float f;8654 STBIR_SIMD_NO_UNROLL(encode); 86558656 output[stbir__decode_order0] = stbir__linear_to_srgb_uchar( encode[0] );8657 output[stbir__decode_order1] = stbir__linear_to_srgb_uchar( encode[1] );8658 output[stbir__decode_order2] = stbir__linear_to_srgb_uchar( encode[2] );86598660 f = encode[3] * stbir__max_uint8_as_float + 0.5f;8661 STBIR_CLAMP(f, 0, 255);8662 output[stbir__decode_order3] = (unsigned char) f;86638664 output += 4;8665 encode += 4;8666 } while( output < end_output );8667}86688669#endif86708671#if ( stbir__coder_min_num == 2 ) || ( ( stbir__coder_min_num == 1 ) && ( !defined(stbir__decode_swizzle) ) )86728673static void STBIR__CODER_NAME(stbir__decode_uint8_srgb2_linearalpha)( float * decodep, int width_times_channels, void const * inputp )8674{8675 float STBIR_STREAMOUT_PTR( * ) decode = decodep;8676 float const * decode_end = (float*) decode + width_times_channels;8677 unsigned char const * input = (unsigned char const *)inputp;8678 decode += 4;8679 while( decode <= decode_end )8680 {8681 decode[0-4] = stbir__srgb_uchar_to_linear_float[ input[stbir__decode_order0] ];8682 decode[1-4] = ( (float) input[stbir__decode_order1] ) * stbir__max_uint8_as_float_inverted;8683 decode[2-4] = stbir__srgb_uchar_to_linear_float[ input[stbir__decode_order0+2] ];8684 decode[3-4] = ( (float) input[stbir__decode_order1+2] ) * stbir__max_uint8_as_float_inverted;8685 input += 4;8686 decode += 4;8687 }8688 decode -= 4;8689 if( decode < decode_end ) 8690 {8691 decode[0] = stbir__srgb_uchar_to_linear_float[ stbir__decode_order0 ];8692 decode[1] = ( (float) input[stbir__decode_order1] ) * stbir__max_uint8_as_float_inverted;8693 }8694}86958696static void STBIR__CODER_NAME( stbir__encode_uint8_srgb2_linearalpha )( void * outputp, int width_times_channels, float const * encode )8697{8698 unsigned char STBIR_SIMD_STREAMOUT_PTR( * ) output = (unsigned char*) outputp;8699 unsigned char * end_output = ( (unsigned char*) output ) + width_times_channels;87008701 #ifdef STBIR_SIMD8702 stbir_uint32 const * to_srgb = fp32_to_srgb8_tab4 - (127-13)*8;87038704 if ( width_times_channels >= 16 )8705 {8706 float const * end_encode_m16 = encode + width_times_channels - 16;8707 end_output -= 16;8708 for(;;)8709 {8710 stbir__simdf f0, f1, f2, f3;8711 stbir__simdi i0, i1, i2, i3; 87128713 STBIR_SIMD_NO_UNROLL(encode);8714 stbir__simdf_load4_transposed( f0, f1, f2, f3, encode );87158716 stbir__min_max_shift20( i0, f0 );8717 stbir__scale_and_convert( i1, f1 );8718 stbir__min_max_shift20( i2, f2 );8719 stbir__scale_and_convert( i3, f3 );8720 8721 stbir__simdi_table_lookup2( i0, i2, to_srgb );8722 8723 stbir__linear_to_srgb_finish( i0, f0 );8724 stbir__linear_to_srgb_finish( i2, f2 );87258726 stbir__interleave_pack_and_store_16_u8( output, STBIR_strs_join1(i, ,stbir__encode_order0), STBIR_strs_join1(i, ,stbir__encode_order1), STBIR_strs_join1(i, ,stbir__encode_order2), STBIR_strs_join1(i, ,stbir__encode_order3) );87278728 output += 16;8729 encode += 16;8730 if ( output <= end_output ) 8731 continue;8732 if ( output == ( end_output + 16 ) )8733 break;8734 output = end_output; // backup and do last couple8735 encode = end_encode_m16;8736 }8737 return;8738 }8739 #endif87408741 do {8742 float f;8743 STBIR_SIMD_NO_UNROLL(encode);87448745 output[stbir__decode_order0] = stbir__linear_to_srgb_uchar( encode[0] );87468747 f = encode[1] * stbir__max_uint8_as_float + 0.5f;8748 STBIR_CLAMP(f, 0, 255);8749 output[stbir__decode_order1] = (unsigned char) f;87508751 output += 2;8752 encode += 2;8753 } while( output < end_output );8754}87558756#endif87578758static void STBIR__CODER_NAME(stbir__decode_uint16_linear_scaled)( float * decodep, int width_times_channels, void const * inputp )8759{8760 float STBIR_STREAMOUT_PTR( * ) decode = decodep;8761 float * decode_end = (float*) decode + width_times_channels;8762 unsigned short const * input = (unsigned short const *)inputp;87638764 #ifdef STBIR_SIMD8765 unsigned short const * end_input_m8 = input + width_times_channels - 8;8766 if ( width_times_channels >= 8 )8767 {8768 decode_end -= 8;8769 for(;;)8770 {8771 #ifdef STBIR_SIMD88772 stbir__simdi i; stbir__simdi8 o;8773 stbir__simdf8 of;8774 STBIR_NO_UNROLL(decode);8775 stbir__simdi_load( i, input );8776 stbir__simdi8_expand_u16_to_u32( o, i );8777 stbir__simdi8_convert_i32_to_float( of, o );8778 stbir__simdf8_mult( of, of, STBIR_max_uint16_as_float_inverted8);8779 stbir__decode_simdf8_flip( of );8780 stbir__simdf8_store( decode + 0, of );8781 #else8782 stbir__simdi i, o0, o1;8783 stbir__simdf of0, of1;8784 STBIR_NO_UNROLL(decode);8785 stbir__simdi_load( i, input );8786 stbir__simdi_expand_u16_to_u32( o0,o1,i );8787 stbir__simdi_convert_i32_to_float( of0, o0 );8788 stbir__simdi_convert_i32_to_float( of1, o1 );8789 stbir__simdf_mult( of0, of0, STBIR__CONSTF(STBIR_max_uint16_as_float_inverted) );8790 stbir__simdf_mult( of1, of1, STBIR__CONSTF(STBIR_max_uint16_as_float_inverted));8791 stbir__decode_simdf4_flip( of0 );8792 stbir__decode_simdf4_flip( of1 );8793 stbir__simdf_store( decode + 0, of0 );8794 stbir__simdf_store( decode + 4, of1 );8795 #endif8796 decode += 8; 8797 input += 8;8798 if ( decode <= decode_end ) 8799 continue;8800 if ( decode == ( decode_end + 8 ) )8801 break;8802 decode = decode_end; // backup and do last couple8803 input = end_input_m8;8804 }8805 return;8806 }8807 #endif88088809 // try to do blocks of 4 when you can8810 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four8811 decode += 4;8812 while( decode <= decode_end )8813 {8814 STBIR_SIMD_NO_UNROLL(decode);8815 decode[0-4] = ((float)(input[stbir__decode_order0])) * stbir__max_uint16_as_float_inverted;8816 decode[1-4] = ((float)(input[stbir__decode_order1])) * stbir__max_uint16_as_float_inverted;8817 decode[2-4] = ((float)(input[stbir__decode_order2])) * stbir__max_uint16_as_float_inverted;8818 decode[3-4] = ((float)(input[stbir__decode_order3])) * stbir__max_uint16_as_float_inverted;8819 decode += 4;8820 input += 4;8821 }8822 decode -= 4;8823 #endif88248825 // do the remnants8826 #if stbir__coder_min_num < 48827 while( decode < decode_end )8828 {8829 STBIR_NO_UNROLL(decode);8830 decode[0] = ((float)(input[stbir__decode_order0])) * stbir__max_uint16_as_float_inverted;8831 #if stbir__coder_min_num >= 28832 decode[1] = ((float)(input[stbir__decode_order1])) * stbir__max_uint16_as_float_inverted;8833 #endif8834 #if stbir__coder_min_num >= 38835 decode[2] = ((float)(input[stbir__decode_order2])) * stbir__max_uint16_as_float_inverted;8836 #endif8837 decode += stbir__coder_min_num;8838 input += stbir__coder_min_num;8839 }8840 #endif8841}884288438844static void STBIR__CODER_NAME(stbir__encode_uint16_linear_scaled)( void * outputp, int width_times_channels, float const * encode )8845{8846 unsigned short STBIR_SIMD_STREAMOUT_PTR( * ) output = (unsigned short*) outputp;8847 unsigned short * end_output = ( (unsigned short*) output ) + width_times_channels;88488849 #ifdef STBIR_SIMD8850 {8851 if ( width_times_channels >= stbir__simdfX_float_count*2 )8852 {8853 float const * end_encode_m8 = encode + width_times_channels - stbir__simdfX_float_count*2;8854 end_output -= stbir__simdfX_float_count*2;8855 for(;;)8856 {8857 stbir__simdfX e0, e1;8858 stbir__simdiX i;8859 STBIR_SIMD_NO_UNROLL(encode);8860 stbir__simdfX_madd_mem( e0, STBIR_simd_point5X, STBIR_max_uint16_as_floatX, encode );8861 stbir__simdfX_madd_mem( e1, STBIR_simd_point5X, STBIR_max_uint16_as_floatX, encode+stbir__simdfX_float_count );8862 stbir__encode_simdfX_unflip( e0 );8863 stbir__encode_simdfX_unflip( e1 );8864 stbir__simdfX_pack_to_words( i, e0, e1 );8865 stbir__simdiX_store( output, i );8866 encode += stbir__simdfX_float_count*2;8867 output += stbir__simdfX_float_count*2;8868 if ( output <= end_output ) 8869 continue;8870 if ( output == ( end_output + stbir__simdfX_float_count*2 ) )8871 break;8872 output = end_output; // backup and do last couple8873 encode = end_encode_m8;8874 }8875 return;8876 }8877 }88788879 // try to do blocks of 4 when you can8880 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four8881 output += 4;8882 while( output <= end_output )8883 {8884 stbir__simdf e;8885 stbir__simdi i;8886 STBIR_NO_UNROLL(encode);8887 stbir__simdf_load( e, encode );8888 stbir__simdf_madd( e, STBIR__CONSTF(STBIR_simd_point5), STBIR__CONSTF(STBIR_max_uint16_as_float), e );8889 stbir__encode_simdf4_unflip( e );8890 stbir__simdf_pack_to_8words( i, e, e ); // only use first 48891 stbir__simdi_store2( output-4, i );8892 output += 4;8893 encode += 4;8894 }8895 output -= 4;8896 #endif88978898 // do the remnants8899 #if stbir__coder_min_num < 48900 while( output < end_output )8901 {8902 stbir__simdf e;8903 STBIR_NO_UNROLL(encode);8904 stbir__simdf_madd1_mem( e, STBIR__CONSTF(STBIR_simd_point5), STBIR__CONSTF(STBIR_max_uint16_as_float), encode+stbir__encode_order0 ); output[0] = stbir__simdf_convert_float_to_short( e );8905 #if stbir__coder_min_num >= 28906 stbir__simdf_madd1_mem( e, STBIR__CONSTF(STBIR_simd_point5), STBIR__CONSTF(STBIR_max_uint16_as_float), encode+stbir__encode_order1 ); output[1] = stbir__simdf_convert_float_to_short( e );8907 #endif8908 #if stbir__coder_min_num >= 38909 stbir__simdf_madd1_mem( e, STBIR__CONSTF(STBIR_simd_point5), STBIR__CONSTF(STBIR_max_uint16_as_float), encode+stbir__encode_order2 ); output[2] = stbir__simdf_convert_float_to_short( e );8910 #endif8911 output += stbir__coder_min_num;8912 encode += stbir__coder_min_num;8913 }8914 #endif8915 8916 #else89178918 // try to do blocks of 4 when you can8919 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four8920 output += 4;8921 while( output <= end_output )8922 {8923 float f;8924 STBIR_SIMD_NO_UNROLL(encode);8925 f = encode[stbir__encode_order0] * stbir__max_uint16_as_float + 0.5f; STBIR_CLAMP(f, 0, 65535); output[0-4] = (unsigned short)f;8926 f = encode[stbir__encode_order1] * stbir__max_uint16_as_float + 0.5f; STBIR_CLAMP(f, 0, 65535); output[1-4] = (unsigned short)f;8927 f = encode[stbir__encode_order2] * stbir__max_uint16_as_float + 0.5f; STBIR_CLAMP(f, 0, 65535); output[2-4] = (unsigned short)f;8928 f = encode[stbir__encode_order3] * stbir__max_uint16_as_float + 0.5f; STBIR_CLAMP(f, 0, 65535); output[3-4] = (unsigned short)f;8929 output += 4;8930 encode += 4;8931 }8932 output -= 4;8933 #endif89348935 // do the remnants8936 #if stbir__coder_min_num < 48937 while( output < end_output )8938 {8939 float f;8940 STBIR_NO_UNROLL(encode);8941 f = encode[stbir__encode_order0] * stbir__max_uint16_as_float + 0.5f; STBIR_CLAMP(f, 0, 65535); output[0] = (unsigned short)f;8942 #if stbir__coder_min_num >= 28943 f = encode[stbir__encode_order1] * stbir__max_uint16_as_float + 0.5f; STBIR_CLAMP(f, 0, 65535); output[1] = (unsigned short)f;8944 #endif8945 #if stbir__coder_min_num >= 38946 f = encode[stbir__encode_order2] * stbir__max_uint16_as_float + 0.5f; STBIR_CLAMP(f, 0, 65535); output[2] = (unsigned short)f;8947 #endif8948 output += stbir__coder_min_num;8949 encode += stbir__coder_min_num;8950 }8951 #endif8952 #endif8953}89548955static void STBIR__CODER_NAME(stbir__decode_uint16_linear)( float * decodep, int width_times_channels, void const * inputp )8956{8957 float STBIR_STREAMOUT_PTR( * ) decode = decodep;8958 float * decode_end = (float*) decode + width_times_channels;8959 unsigned short const * input = (unsigned short const *)inputp;89608961 #ifdef STBIR_SIMD8962 unsigned short const * end_input_m8 = input + width_times_channels - 8;8963 if ( width_times_channels >= 8 )8964 {8965 decode_end -= 8;8966 for(;;)8967 {8968 #ifdef STBIR_SIMD88969 stbir__simdi i; stbir__simdi8 o;8970 stbir__simdf8 of;8971 STBIR_NO_UNROLL(decode);8972 stbir__simdi_load( i, input );8973 stbir__simdi8_expand_u16_to_u32( o, i );8974 stbir__simdi8_convert_i32_to_float( of, o );8975 stbir__decode_simdf8_flip( of );8976 stbir__simdf8_store( decode + 0, of );8977 #else8978 stbir__simdi i, o0, o1;8979 stbir__simdf of0, of1;8980 STBIR_NO_UNROLL(decode);8981 stbir__simdi_load( i, input );8982 stbir__simdi_expand_u16_to_u32( o0, o1, i );8983 stbir__simdi_convert_i32_to_float( of0, o0 );8984 stbir__simdi_convert_i32_to_float( of1, o1 );8985 stbir__decode_simdf4_flip( of0 );8986 stbir__decode_simdf4_flip( of1 );8987 stbir__simdf_store( decode + 0, of0 );8988 stbir__simdf_store( decode + 4, of1 );8989 #endif8990 decode += 8;8991 input += 8;8992 if ( decode <= decode_end ) 8993 continue;8994 if ( decode == ( decode_end + 8 ) )8995 break;8996 decode = decode_end; // backup and do last couple8997 input = end_input_m8;8998 }8999 return;9000 }9001 #endif90029003 // try to do blocks of 4 when you can9004 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four9005 decode += 4;9006 while( decode <= decode_end )9007 {9008 STBIR_SIMD_NO_UNROLL(decode);9009 decode[0-4] = ((float)(input[stbir__decode_order0]));9010 decode[1-4] = ((float)(input[stbir__decode_order1]));9011 decode[2-4] = ((float)(input[stbir__decode_order2]));9012 decode[3-4] = ((float)(input[stbir__decode_order3]));9013 decode += 4;9014 input += 4;9015 }9016 decode -= 4;9017 #endif90189019 // do the remnants9020 #if stbir__coder_min_num < 49021 while( decode < decode_end )9022 {9023 STBIR_NO_UNROLL(decode);9024 decode[0] = ((float)(input[stbir__decode_order0]));9025 #if stbir__coder_min_num >= 29026 decode[1] = ((float)(input[stbir__decode_order1]));9027 #endif9028 #if stbir__coder_min_num >= 39029 decode[2] = ((float)(input[stbir__decode_order2]));9030 #endif9031 decode += stbir__coder_min_num;9032 input += stbir__coder_min_num;9033 }9034 #endif9035}90369037static void STBIR__CODER_NAME(stbir__encode_uint16_linear)( void * outputp, int width_times_channels, float const * encode )9038{9039 unsigned short STBIR_SIMD_STREAMOUT_PTR( * ) output = (unsigned short*) outputp;9040 unsigned short * end_output = ( (unsigned short*) output ) + width_times_channels;90419042 #ifdef STBIR_SIMD9043 {9044 if ( width_times_channels >= stbir__simdfX_float_count*2 )9045 {9046 float const * end_encode_m8 = encode + width_times_channels - stbir__simdfX_float_count*2;9047 end_output -= stbir__simdfX_float_count*2;9048 for(;;)9049 {9050 stbir__simdfX e0, e1;9051 stbir__simdiX i;9052 STBIR_SIMD_NO_UNROLL(encode);9053 stbir__simdfX_add_mem( e0, STBIR_simd_point5X, encode );9054 stbir__simdfX_add_mem( e1, STBIR_simd_point5X, encode+stbir__simdfX_float_count );9055 stbir__encode_simdfX_unflip( e0 );9056 stbir__encode_simdfX_unflip( e1 );9057 stbir__simdfX_pack_to_words( i, e0, e1 );9058 stbir__simdiX_store( output, i );9059 encode += stbir__simdfX_float_count*2;9060 output += stbir__simdfX_float_count*2;9061 if ( output <= end_output ) 9062 continue;9063 if ( output == ( end_output + stbir__simdfX_float_count*2 ) )9064 break;9065 output = end_output; // backup and do last couple9066 encode = end_encode_m8;9067 }9068 return;9069 }9070 }90719072 // try to do blocks of 4 when you can9073 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four9074 output += 4;9075 while( output <= end_output )9076 {9077 stbir__simdf e;9078 stbir__simdi i;9079 STBIR_NO_UNROLL(encode);9080 stbir__simdf_load( e, encode );9081 stbir__simdf_add( e, STBIR__CONSTF(STBIR_simd_point5), e );9082 stbir__encode_simdf4_unflip( e );9083 stbir__simdf_pack_to_8words( i, e, e ); // only use first 49084 stbir__simdi_store2( output-4, i );9085 output += 4;9086 encode += 4;9087 }9088 output -= 4;9089 #endif90909091 #else90929093 // try to do blocks of 4 when you can9094 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four9095 output += 4;9096 while( output <= end_output )9097 {9098 float f;9099 STBIR_SIMD_NO_UNROLL(encode);9100 f = encode[stbir__encode_order0] + 0.5f; STBIR_CLAMP(f, 0, 65535); output[0-4] = (unsigned short)f;9101 f = encode[stbir__encode_order1] + 0.5f; STBIR_CLAMP(f, 0, 65535); output[1-4] = (unsigned short)f;9102 f = encode[stbir__encode_order2] + 0.5f; STBIR_CLAMP(f, 0, 65535); output[2-4] = (unsigned short)f;9103 f = encode[stbir__encode_order3] + 0.5f; STBIR_CLAMP(f, 0, 65535); output[3-4] = (unsigned short)f;9104 output += 4;9105 encode += 4;9106 }9107 output -= 4;9108 #endif91099110 #endif91119112 // do the remnants9113 #if stbir__coder_min_num < 49114 while( output < end_output )9115 {9116 float f;9117 STBIR_NO_UNROLL(encode);9118 f = encode[stbir__encode_order0] + 0.5f; STBIR_CLAMP(f, 0, 65535); output[0] = (unsigned short)f;9119 #if stbir__coder_min_num >= 29120 f = encode[stbir__encode_order1] + 0.5f; STBIR_CLAMP(f, 0, 65535); output[1] = (unsigned short)f;9121 #endif9122 #if stbir__coder_min_num >= 39123 f = encode[stbir__encode_order2] + 0.5f; STBIR_CLAMP(f, 0, 65535); output[2] = (unsigned short)f;9124 #endif9125 output += stbir__coder_min_num;9126 encode += stbir__coder_min_num;9127 }9128 #endif9129}91309131static void STBIR__CODER_NAME(stbir__decode_half_float_linear)( float * decodep, int width_times_channels, void const * inputp )9132{9133 float STBIR_STREAMOUT_PTR( * ) decode = decodep;9134 float * decode_end = (float*) decode + width_times_channels;9135 stbir__FP16 const * input = (stbir__FP16 const *)inputp;91369137 #ifdef STBIR_SIMD9138 if ( width_times_channels >= 8 )9139 {9140 stbir__FP16 const * end_input_m8 = input + width_times_channels - 8;9141 decode_end -= 8;9142 for(;;)9143 {9144 STBIR_NO_UNROLL(decode);91459146 stbir__half_to_float_SIMD( decode, input );9147 #ifdef stbir__decode_swizzle9148 #ifdef STBIR_SIMD89149 {9150 stbir__simdf8 of;9151 stbir__simdf8_load( of, decode );9152 stbir__decode_simdf8_flip( of );9153 stbir__simdf8_store( decode, of );9154 }9155 #else9156 {9157 stbir__simdf of0,of1;9158 stbir__simdf_load( of0, decode );9159 stbir__simdf_load( of1, decode+4 );9160 stbir__decode_simdf4_flip( of0 );9161 stbir__decode_simdf4_flip( of1 );9162 stbir__simdf_store( decode, of0 );9163 stbir__simdf_store( decode+4, of1 );9164 }9165 #endif9166 #endif9167 decode += 8;9168 input += 8;9169 if ( decode <= decode_end ) 9170 continue;9171 if ( decode == ( decode_end + 8 ) )9172 break;9173 decode = decode_end; // backup and do last couple9174 input = end_input_m8;9175 }9176 return;9177 }9178 #endif91799180 // try to do blocks of 4 when you can9181 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four9182 decode += 4;9183 while( decode <= decode_end )9184 {9185 STBIR_SIMD_NO_UNROLL(decode);9186 decode[0-4] = stbir__half_to_float(input[stbir__decode_order0]);9187 decode[1-4] = stbir__half_to_float(input[stbir__decode_order1]);9188 decode[2-4] = stbir__half_to_float(input[stbir__decode_order2]);9189 decode[3-4] = stbir__half_to_float(input[stbir__decode_order3]);9190 decode += 4;9191 input += 4;9192 }9193 decode -= 4;9194 #endif91959196 // do the remnants9197 #if stbir__coder_min_num < 49198 while( decode < decode_end )9199 {9200 STBIR_NO_UNROLL(decode);9201 decode[0] = stbir__half_to_float(input[stbir__decode_order0]);9202 #if stbir__coder_min_num >= 29203 decode[1] = stbir__half_to_float(input[stbir__decode_order1]);9204 #endif9205 #if stbir__coder_min_num >= 39206 decode[2] = stbir__half_to_float(input[stbir__decode_order2]);9207 #endif9208 decode += stbir__coder_min_num;9209 input += stbir__coder_min_num;9210 }9211 #endif9212}92139214static void STBIR__CODER_NAME( stbir__encode_half_float_linear )( void * outputp, int width_times_channels, float const * encode )9215{9216 stbir__FP16 STBIR_SIMD_STREAMOUT_PTR( * ) output = (stbir__FP16*) outputp;9217 stbir__FP16 * end_output = ( (stbir__FP16*) output ) + width_times_channels;92189219 #ifdef STBIR_SIMD9220 if ( width_times_channels >= 8 )9221 {9222 float const * end_encode_m8 = encode + width_times_channels - 8;9223 end_output -= 8;9224 for(;;)9225 {9226 STBIR_SIMD_NO_UNROLL(encode);9227 #ifdef stbir__decode_swizzle9228 #ifdef STBIR_SIMD89229 {9230 stbir__simdf8 of;9231 stbir__simdf8_load( of, encode );9232 stbir__encode_simdf8_unflip( of );9233 stbir__float_to_half_SIMD( output, (float*)&of );9234 }9235 #else9236 {9237 stbir__simdf of[2];9238 stbir__simdf_load( of[0], encode );9239 stbir__simdf_load( of[1], encode+4 );9240 stbir__encode_simdf4_unflip( of[0] );9241 stbir__encode_simdf4_unflip( of[1] );9242 stbir__float_to_half_SIMD( output, (float*)of );9243 }9244 #endif9245 #else9246 stbir__float_to_half_SIMD( output, encode );9247 #endif9248 encode += 8;9249 output += 8;9250 if ( output <= end_output ) 9251 continue;9252 if ( output == ( end_output + 8 ) )9253 break;9254 output = end_output; // backup and do last couple9255 encode = end_encode_m8;9256 }9257 return;9258 }9259 #endif92609261 // try to do blocks of 4 when you can9262 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four9263 output += 4;9264 while( output <= end_output )9265 {9266 STBIR_SIMD_NO_UNROLL(output);9267 output[0-4] = stbir__float_to_half(encode[stbir__encode_order0]);9268 output[1-4] = stbir__float_to_half(encode[stbir__encode_order1]);9269 output[2-4] = stbir__float_to_half(encode[stbir__encode_order2]);9270 output[3-4] = stbir__float_to_half(encode[stbir__encode_order3]);9271 output += 4;9272 encode += 4;9273 }9274 output -= 4;9275 #endif92769277 // do the remnants9278 #if stbir__coder_min_num < 49279 while( output < end_output )9280 {9281 STBIR_NO_UNROLL(output);9282 output[0] = stbir__float_to_half(encode[stbir__encode_order0]);9283 #if stbir__coder_min_num >= 29284 output[1] = stbir__float_to_half(encode[stbir__encode_order1]);9285 #endif9286 #if stbir__coder_min_num >= 39287 output[2] = stbir__float_to_half(encode[stbir__encode_order2]);9288 #endif9289 output += stbir__coder_min_num;9290 encode += stbir__coder_min_num;9291 }9292 #endif9293}92949295static void STBIR__CODER_NAME(stbir__decode_float_linear)( float * decodep, int width_times_channels, void const * inputp )9296{9297 #ifdef stbir__decode_swizzle9298 float STBIR_STREAMOUT_PTR( * ) decode = decodep;9299 float * decode_end = (float*) decode + width_times_channels;9300 float const * input = (float const *)inputp;93019302 #ifdef STBIR_SIMD9303 if ( width_times_channels >= 16 )9304 {9305 float const * end_input_m16 = input + width_times_channels - 16;9306 decode_end -= 16;9307 for(;;)9308 {9309 STBIR_NO_UNROLL(decode);9310 #ifdef stbir__decode_swizzle9311 #ifdef STBIR_SIMD89312 {9313 stbir__simdf8 of0,of1;9314 stbir__simdf8_load( of0, input );9315 stbir__simdf8_load( of1, input+8 );9316 stbir__decode_simdf8_flip( of0 );9317 stbir__decode_simdf8_flip( of1 );9318 stbir__simdf8_store( decode, of0 );9319 stbir__simdf8_store( decode+8, of1 );9320 }9321 #else9322 {9323 stbir__simdf of0,of1,of2,of3;9324 stbir__simdf_load( of0, input );9325 stbir__simdf_load( of1, input+4 );9326 stbir__simdf_load( of2, input+8 );9327 stbir__simdf_load( of3, input+12 );9328 stbir__decode_simdf4_flip( of0 );9329 stbir__decode_simdf4_flip( of1 );9330 stbir__decode_simdf4_flip( of2 );9331 stbir__decode_simdf4_flip( of3 );9332 stbir__simdf_store( decode, of0 );9333 stbir__simdf_store( decode+4, of1 );9334 stbir__simdf_store( decode+8, of2 );9335 stbir__simdf_store( decode+12, of3 );9336 }9337 #endif9338 #endif9339 decode += 16;9340 input += 16;9341 if ( decode <= decode_end ) 9342 continue;9343 if ( decode == ( decode_end + 16 ) )9344 break;9345 decode = decode_end; // backup and do last couple9346 input = end_input_m16;9347 }9348 return;9349 }9350 #endif93519352 // try to do blocks of 4 when you can9353 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four9354 decode += 4;9355 while( decode <= decode_end )9356 {9357 STBIR_SIMD_NO_UNROLL(decode);9358 decode[0-4] = input[stbir__decode_order0];9359 decode[1-4] = input[stbir__decode_order1];9360 decode[2-4] = input[stbir__decode_order2];9361 decode[3-4] = input[stbir__decode_order3];9362 decode += 4;9363 input += 4;9364 }9365 decode -= 4;9366 #endif93679368 // do the remnants9369 #if stbir__coder_min_num < 49370 while( decode < decode_end )9371 {9372 STBIR_NO_UNROLL(decode);9373 decode[0] = input[stbir__decode_order0];9374 #if stbir__coder_min_num >= 29375 decode[1] = input[stbir__decode_order1];9376 #endif9377 #if stbir__coder_min_num >= 39378 decode[2] = input[stbir__decode_order2];9379 #endif9380 decode += stbir__coder_min_num;9381 input += stbir__coder_min_num;9382 }9383 #endif93849385 #else9386 9387 if ( (void*)decodep != inputp )9388 STBIR_MEMCPY( decodep, inputp, width_times_channels * sizeof( float ) );9389 9390 #endif9391}93929393static void STBIR__CODER_NAME( stbir__encode_float_linear )( void * outputp, int width_times_channels, float const * encode )9394{9395 #if !defined( STBIR_FLOAT_HIGH_CLAMP ) && !defined(STBIR_FLOAT_LO_CLAMP) && !defined(stbir__decode_swizzle)93969397 if ( (void*)outputp != (void*) encode )9398 STBIR_MEMCPY( outputp, encode, width_times_channels * sizeof( float ) );93999400 #else94019402 float STBIR_SIMD_STREAMOUT_PTR( * ) output = (float*) outputp;9403 float * end_output = ( (float*) output ) + width_times_channels;94049405 #ifdef STBIR_FLOAT_HIGH_CLAMP9406 #define stbir_scalar_hi_clamp( v ) if ( v > STBIR_FLOAT_HIGH_CLAMP ) v = STBIR_FLOAT_HIGH_CLAMP;9407 #else9408 #define stbir_scalar_hi_clamp( v )9409 #endif9410 #ifdef STBIR_FLOAT_LOW_CLAMP9411 #define stbir_scalar_lo_clamp( v ) if ( v < STBIR_FLOAT_LOW_CLAMP ) v = STBIR_FLOAT_LOW_CLAMP;9412 #else9413 #define stbir_scalar_lo_clamp( v )9414 #endif94159416 #ifdef STBIR_SIMD94179418 #ifdef STBIR_FLOAT_HIGH_CLAMP9419 const stbir__simdfX high_clamp = stbir__simdf_frepX(STBIR_FLOAT_HIGH_CLAMP);9420 #endif9421 #ifdef STBIR_FLOAT_LOW_CLAMP9422 const stbir__simdfX low_clamp = stbir__simdf_frepX(STBIR_FLOAT_LOW_CLAMP);9423 #endif94249425 if ( width_times_channels >= ( stbir__simdfX_float_count * 2 ) )9426 {9427 float const * end_encode_m8 = encode + width_times_channels - ( stbir__simdfX_float_count * 2 );9428 end_output -= ( stbir__simdfX_float_count * 2 );9429 for(;;)9430 {9431 stbir__simdfX e0, e1;9432 STBIR_SIMD_NO_UNROLL(encode);9433 stbir__simdfX_load( e0, encode );9434 stbir__simdfX_load( e1, encode+stbir__simdfX_float_count );9435#ifdef STBIR_FLOAT_HIGH_CLAMP9436 stbir__simdfX_min( e0, e0, high_clamp );9437 stbir__simdfX_min( e1, e1, high_clamp );9438#endif 9439#ifdef STBIR_FLOAT_LOW_CLAMP9440 stbir__simdfX_max( e0, e0, low_clamp );9441 stbir__simdfX_max( e1, e1, low_clamp );9442#endif 9443 stbir__encode_simdfX_unflip( e0 );9444 stbir__encode_simdfX_unflip( e1 );9445 stbir__simdfX_store( output, e0 );9446 stbir__simdfX_store( output+stbir__simdfX_float_count, e1 );9447 encode += stbir__simdfX_float_count * 2;9448 output += stbir__simdfX_float_count * 2;9449 if ( output < end_output ) 9450 continue;9451 if ( output == ( end_output + ( stbir__simdfX_float_count * 2 ) ) )9452 break;9453 output = end_output; // backup and do last couple9454 encode = end_encode_m8;9455 }9456 return;9457 }94589459 // try to do blocks of 4 when you can9460 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four9461 output += 4;9462 while( output <= end_output )9463 {9464 stbir__simdf e0;9465 STBIR_NO_UNROLL(encode);9466 stbir__simdf_load( e0, encode );9467#ifdef STBIR_FLOAT_HIGH_CLAMP9468 stbir__simdf_min( e0, e0, high_clamp );9469#endif 9470#ifdef STBIR_FLOAT_LOW_CLAMP9471 stbir__simdf_max( e0, e0, low_clamp );9472#endif 9473 stbir__encode_simdf4_unflip( e0 );9474 stbir__simdf_store( output-4, e0 );9475 output += 4;9476 encode += 4;9477 }9478 output -= 4;9479 #endif94809481 #else94829483 // try to do blocks of 4 when you can9484 #if stbir__coder_min_num != 3 // doesn't divide cleanly by four9485 output += 4;9486 while( output <= end_output )9487 {9488 float e;9489 STBIR_SIMD_NO_UNROLL(encode);9490 e = encode[ stbir__encode_order0 ]; stbir_scalar_hi_clamp( e ); stbir_scalar_lo_clamp( e ); output[0-4] = e;9491 e = encode[ stbir__encode_order1 ]; stbir_scalar_hi_clamp( e ); stbir_scalar_lo_clamp( e ); output[1-4] = e;9492 e = encode[ stbir__encode_order2 ]; stbir_scalar_hi_clamp( e ); stbir_scalar_lo_clamp( e ); output[2-4] = e;9493 e = encode[ stbir__encode_order3 ]; stbir_scalar_hi_clamp( e ); stbir_scalar_lo_clamp( e ); output[3-4] = e;9494 output += 4;9495 encode += 4;9496 }9497 output -= 4;94989499 #endif95009501 #endif95029503 // do the remnants9504 #if stbir__coder_min_num < 49505 while( output < end_output )9506 {9507 float e;9508 STBIR_NO_UNROLL(encode);9509 e = encode[ stbir__encode_order0 ]; stbir_scalar_hi_clamp( e ); stbir_scalar_lo_clamp( e ); output[0] = e;9510 #if stbir__coder_min_num >= 29511 e = encode[ stbir__encode_order1 ]; stbir_scalar_hi_clamp( e ); stbir_scalar_lo_clamp( e ); output[1] = e;9512 #endif9513 #if stbir__coder_min_num >= 39514 e = encode[ stbir__encode_order2 ]; stbir_scalar_hi_clamp( e ); stbir_scalar_lo_clamp( e ); output[2] = e;9515 #endif9516 output += stbir__coder_min_num;9517 encode += stbir__coder_min_num;9518 }9519 #endif9520 9521 #endif9522}95239524#undef stbir__decode_suffix 9525#undef stbir__decode_simdf8_flip9526#undef stbir__decode_simdf4_flip9527#undef stbir__decode_order0 9528#undef stbir__decode_order19529#undef stbir__decode_order29530#undef stbir__decode_order39531#undef stbir__encode_order0 9532#undef stbir__encode_order19533#undef stbir__encode_order29534#undef stbir__encode_order39535#undef stbir__encode_simdf8_unflip9536#undef stbir__encode_simdf4_unflip9537#undef stbir__encode_simdfX_unflip9538#undef STBIR__CODER_NAME9539#undef stbir__coder_min_num9540#undef stbir__decode_swizzle9541#undef stbir_scalar_hi_clamp9542#undef stbir_scalar_lo_clamp9543#undef STB_IMAGE_RESIZE_DO_CODERS95449545#elif defined( STB_IMAGE_RESIZE_DO_VERTICALS)95469547#ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE9548#define STBIR_chans( start, end ) STBIR_strs_join14(start,STBIR__vertical_channels,end,_cont)9549#else9550#define STBIR_chans( start, end ) STBIR_strs_join1(start,STBIR__vertical_channels,end)9551#endif95529553#if STBIR__vertical_channels >= 19554#define stbIF0( code ) code9555#else9556#define stbIF0( code )9557#endif9558#if STBIR__vertical_channels >= 29559#define stbIF1( code ) code9560#else9561#define stbIF1( code )9562#endif9563#if STBIR__vertical_channels >= 39564#define stbIF2( code ) code9565#else9566#define stbIF2( code )9567#endif9568#if STBIR__vertical_channels >= 49569#define stbIF3( code ) code9570#else9571#define stbIF3( code )9572#endif9573#if STBIR__vertical_channels >= 59574#define stbIF4( code ) code9575#else9576#define stbIF4( code )9577#endif9578#if STBIR__vertical_channels >= 69579#define stbIF5( code ) code9580#else9581#define stbIF5( code )9582#endif9583#if STBIR__vertical_channels >= 79584#define stbIF6( code ) code9585#else9586#define stbIF6( code )9587#endif9588#if STBIR__vertical_channels >= 89589#define stbIF7( code ) code9590#else9591#define stbIF7( code )9592#endif95939594static void STBIR_chans( stbir__vertical_scatter_with_,_coeffs)( float ** outputs, float const * vertical_coefficients, float const * input, float const * input_end )9595{9596 stbIF0( float STBIR_SIMD_STREAMOUT_PTR( * ) output0 = outputs[0]; float c0s = vertical_coefficients[0]; )9597 stbIF1( float STBIR_SIMD_STREAMOUT_PTR( * ) output1 = outputs[1]; float c1s = vertical_coefficients[1]; )9598 stbIF2( float STBIR_SIMD_STREAMOUT_PTR( * ) output2 = outputs[2]; float c2s = vertical_coefficients[2]; )9599 stbIF3( float STBIR_SIMD_STREAMOUT_PTR( * ) output3 = outputs[3]; float c3s = vertical_coefficients[3]; )9600 stbIF4( float STBIR_SIMD_STREAMOUT_PTR( * ) output4 = outputs[4]; float c4s = vertical_coefficients[4]; )9601 stbIF5( float STBIR_SIMD_STREAMOUT_PTR( * ) output5 = outputs[5]; float c5s = vertical_coefficients[5]; )9602 stbIF6( float STBIR_SIMD_STREAMOUT_PTR( * ) output6 = outputs[6]; float c6s = vertical_coefficients[6]; )9603 stbIF7( float STBIR_SIMD_STREAMOUT_PTR( * ) output7 = outputs[7]; float c7s = vertical_coefficients[7]; )96049605 #ifdef STBIR_SIMD9606 {9607 stbIF0(stbir__simdfX c0 = stbir__simdf_frepX( c0s ); )9608 stbIF1(stbir__simdfX c1 = stbir__simdf_frepX( c1s ); )9609 stbIF2(stbir__simdfX c2 = stbir__simdf_frepX( c2s ); )9610 stbIF3(stbir__simdfX c3 = stbir__simdf_frepX( c3s ); )9611 stbIF4(stbir__simdfX c4 = stbir__simdf_frepX( c4s ); )9612 stbIF5(stbir__simdfX c5 = stbir__simdf_frepX( c5s ); )9613 stbIF6(stbir__simdfX c6 = stbir__simdf_frepX( c6s ); )9614 stbIF7(stbir__simdfX c7 = stbir__simdf_frepX( c7s ); )9615 while ( ( (char*)input_end - (char*) input ) >= (16*stbir__simdfX_float_count) ) 9616 {9617 stbir__simdfX o0, o1, o2, o3, r0, r1, r2, r3;9618 STBIR_SIMD_NO_UNROLL(output0);96199620 stbir__simdfX_load( r0, input ); stbir__simdfX_load( r1, input+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input+(3*stbir__simdfX_float_count) );96219622 #ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE9623 stbIF0( stbir__simdfX_load( o0, output0 ); stbir__simdfX_load( o1, output0+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output0+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( o3, output0+(3*stbir__simdfX_float_count) );9624 stbir__simdfX_madd( o0, o0, r0, c0 ); stbir__simdfX_madd( o1, o1, r1, c0 ); stbir__simdfX_madd( o2, o2, r2, c0 ); stbir__simdfX_madd( o3, o3, r3, c0 ); 9625 stbir__simdfX_store( output0, o0 ); stbir__simdfX_store( output0+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output0+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output0+(3*stbir__simdfX_float_count), o3 ); )9626 stbIF1( stbir__simdfX_load( o0, output1 ); stbir__simdfX_load( o1, output1+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output1+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( o3, output1+(3*stbir__simdfX_float_count) );9627 stbir__simdfX_madd( o0, o0, r0, c1 ); stbir__simdfX_madd( o1, o1, r1, c1 ); stbir__simdfX_madd( o2, o2, r2, c1 ); stbir__simdfX_madd( o3, o3, r3, c1 ); 9628 stbir__simdfX_store( output1, o0 ); stbir__simdfX_store( output1+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output1+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output1+(3*stbir__simdfX_float_count), o3 ); )9629 stbIF2( stbir__simdfX_load( o0, output2 ); stbir__simdfX_load( o1, output2+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output2+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( o3, output2+(3*stbir__simdfX_float_count) );9630 stbir__simdfX_madd( o0, o0, r0, c2 ); stbir__simdfX_madd( o1, o1, r1, c2 ); stbir__simdfX_madd( o2, o2, r2, c2 ); stbir__simdfX_madd( o3, o3, r3, c2 ); 9631 stbir__simdfX_store( output2, o0 ); stbir__simdfX_store( output2+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output2+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output2+(3*stbir__simdfX_float_count), o3 ); )9632 stbIF3( stbir__simdfX_load( o0, output3 ); stbir__simdfX_load( o1, output3+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output3+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( o3, output3+(3*stbir__simdfX_float_count) );9633 stbir__simdfX_madd( o0, o0, r0, c3 ); stbir__simdfX_madd( o1, o1, r1, c3 ); stbir__simdfX_madd( o2, o2, r2, c3 ); stbir__simdfX_madd( o3, o3, r3, c3 ); 9634 stbir__simdfX_store( output3, o0 ); stbir__simdfX_store( output3+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output3+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output3+(3*stbir__simdfX_float_count), o3 ); )9635 stbIF4( stbir__simdfX_load( o0, output4 ); stbir__simdfX_load( o1, output4+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output4+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( o3, output4+(3*stbir__simdfX_float_count) );9636 stbir__simdfX_madd( o0, o0, r0, c4 ); stbir__simdfX_madd( o1, o1, r1, c4 ); stbir__simdfX_madd( o2, o2, r2, c4 ); stbir__simdfX_madd( o3, o3, r3, c4 ); 9637 stbir__simdfX_store( output4, o0 ); stbir__simdfX_store( output4+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output4+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output4+(3*stbir__simdfX_float_count), o3 ); )9638 stbIF5( stbir__simdfX_load( o0, output5 ); stbir__simdfX_load( o1, output5+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output5+(2*stbir__simdfX_float_count)); stbir__simdfX_load( o3, output5+(3*stbir__simdfX_float_count) );9639 stbir__simdfX_madd( o0, o0, r0, c5 ); stbir__simdfX_madd( o1, o1, r1, c5 ); stbir__simdfX_madd( o2, o2, r2, c5 ); stbir__simdfX_madd( o3, o3, r3, c5 ); 9640 stbir__simdfX_store( output5, o0 ); stbir__simdfX_store( output5+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output5+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output5+(3*stbir__simdfX_float_count), o3 ); )9641 stbIF6( stbir__simdfX_load( o0, output6 ); stbir__simdfX_load( o1, output6+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output6+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( o3, output6+(3*stbir__simdfX_float_count) );9642 stbir__simdfX_madd( o0, o0, r0, c6 ); stbir__simdfX_madd( o1, o1, r1, c6 ); stbir__simdfX_madd( o2, o2, r2, c6 ); stbir__simdfX_madd( o3, o3, r3, c6 ); 9643 stbir__simdfX_store( output6, o0 ); stbir__simdfX_store( output6+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output6+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output6+(3*stbir__simdfX_float_count), o3 ); )9644 stbIF7( stbir__simdfX_load( o0, output7 ); stbir__simdfX_load( o1, output7+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output7+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( o3, output7+(3*stbir__simdfX_float_count) );9645 stbir__simdfX_madd( o0, o0, r0, c7 ); stbir__simdfX_madd( o1, o1, r1, c7 ); stbir__simdfX_madd( o2, o2, r2, c7 ); stbir__simdfX_madd( o3, o3, r3, c7 ); 9646 stbir__simdfX_store( output7, o0 ); stbir__simdfX_store( output7+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output7+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output7+(3*stbir__simdfX_float_count), o3 ); )9647 #else9648 stbIF0( stbir__simdfX_mult( o0, r0, c0 ); stbir__simdfX_mult( o1, r1, c0 ); stbir__simdfX_mult( o2, r2, c0 ); stbir__simdfX_mult( o3, r3, c0 ); 9649 stbir__simdfX_store( output0, o0 ); stbir__simdfX_store( output0+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output0+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output0+(3*stbir__simdfX_float_count), o3 ); )9650 stbIF1( stbir__simdfX_mult( o0, r0, c1 ); stbir__simdfX_mult( o1, r1, c1 ); stbir__simdfX_mult( o2, r2, c1 ); stbir__simdfX_mult( o3, r3, c1 ); 9651 stbir__simdfX_store( output1, o0 ); stbir__simdfX_store( output1+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output1+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output1+(3*stbir__simdfX_float_count), o3 ); )9652 stbIF2( stbir__simdfX_mult( o0, r0, c2 ); stbir__simdfX_mult( o1, r1, c2 ); stbir__simdfX_mult( o2, r2, c2 ); stbir__simdfX_mult( o3, r3, c2 ); 9653 stbir__simdfX_store( output2, o0 ); stbir__simdfX_store( output2+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output2+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output2+(3*stbir__simdfX_float_count), o3 ); )9654 stbIF3( stbir__simdfX_mult( o0, r0, c3 ); stbir__simdfX_mult( o1, r1, c3 ); stbir__simdfX_mult( o2, r2, c3 ); stbir__simdfX_mult( o3, r3, c3 ); 9655 stbir__simdfX_store( output3, o0 ); stbir__simdfX_store( output3+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output3+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output3+(3*stbir__simdfX_float_count), o3 ); )9656 stbIF4( stbir__simdfX_mult( o0, r0, c4 ); stbir__simdfX_mult( o1, r1, c4 ); stbir__simdfX_mult( o2, r2, c4 ); stbir__simdfX_mult( o3, r3, c4 ); 9657 stbir__simdfX_store( output4, o0 ); stbir__simdfX_store( output4+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output4+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output4+(3*stbir__simdfX_float_count), o3 ); )9658 stbIF5( stbir__simdfX_mult( o0, r0, c5 ); stbir__simdfX_mult( o1, r1, c5 ); stbir__simdfX_mult( o2, r2, c5 ); stbir__simdfX_mult( o3, r3, c5 ); 9659 stbir__simdfX_store( output5, o0 ); stbir__simdfX_store( output5+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output5+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output5+(3*stbir__simdfX_float_count), o3 ); )9660 stbIF6( stbir__simdfX_mult( o0, r0, c6 ); stbir__simdfX_mult( o1, r1, c6 ); stbir__simdfX_mult( o2, r2, c6 ); stbir__simdfX_mult( o3, r3, c6 ); 9661 stbir__simdfX_store( output6, o0 ); stbir__simdfX_store( output6+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output6+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output6+(3*stbir__simdfX_float_count), o3 ); )9662 stbIF7( stbir__simdfX_mult( o0, r0, c7 ); stbir__simdfX_mult( o1, r1, c7 ); stbir__simdfX_mult( o2, r2, c7 ); stbir__simdfX_mult( o3, r3, c7 ); 9663 stbir__simdfX_store( output7, o0 ); stbir__simdfX_store( output7+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output7+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output7+(3*stbir__simdfX_float_count), o3 ); )9664 #endif96659666 input += (4*stbir__simdfX_float_count);9667 stbIF0( output0 += (4*stbir__simdfX_float_count); ) stbIF1( output1 += (4*stbir__simdfX_float_count); ) stbIF2( output2 += (4*stbir__simdfX_float_count); ) stbIF3( output3 += (4*stbir__simdfX_float_count); ) stbIF4( output4 += (4*stbir__simdfX_float_count); ) stbIF5( output5 += (4*stbir__simdfX_float_count); ) stbIF6( output6 += (4*stbir__simdfX_float_count); ) stbIF7( output7 += (4*stbir__simdfX_float_count); )9668 }9669 while ( ( (char*)input_end - (char*) input ) >= 16 ) 9670 {9671 stbir__simdf o0, r0;9672 STBIR_SIMD_NO_UNROLL(output0);96739674 stbir__simdf_load( r0, input );96759676 #ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE9677 stbIF0( stbir__simdf_load( o0, output0 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c0 ) ); stbir__simdf_store( output0, o0 ); )9678 stbIF1( stbir__simdf_load( o0, output1 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c1 ) ); stbir__simdf_store( output1, o0 ); )9679 stbIF2( stbir__simdf_load( o0, output2 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c2 ) ); stbir__simdf_store( output2, o0 ); )9680 stbIF3( stbir__simdf_load( o0, output3 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c3 ) ); stbir__simdf_store( output3, o0 ); )9681 stbIF4( stbir__simdf_load( o0, output4 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c4 ) ); stbir__simdf_store( output4, o0 ); )9682 stbIF5( stbir__simdf_load( o0, output5 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c5 ) ); stbir__simdf_store( output5, o0 ); )9683 stbIF6( stbir__simdf_load( o0, output6 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c6 ) ); stbir__simdf_store( output6, o0 ); )9684 stbIF7( stbir__simdf_load( o0, output7 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c7 ) ); stbir__simdf_store( output7, o0 ); )9685 #else9686 stbIF0( stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c0 ) ); stbir__simdf_store( output0, o0 ); )9687 stbIF1( stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c1 ) ); stbir__simdf_store( output1, o0 ); )9688 stbIF2( stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c2 ) ); stbir__simdf_store( output2, o0 ); )9689 stbIF3( stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c3 ) ); stbir__simdf_store( output3, o0 ); )9690 stbIF4( stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c4 ) ); stbir__simdf_store( output4, o0 ); )9691 stbIF5( stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c5 ) ); stbir__simdf_store( output5, o0 ); )9692 stbIF6( stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c6 ) ); stbir__simdf_store( output6, o0 ); )9693 stbIF7( stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c7 ) ); stbir__simdf_store( output7, o0 ); )9694 #endif9695 9696 input += 4;9697 stbIF0( output0 += 4; ) stbIF1( output1 += 4; ) stbIF2( output2 += 4; ) stbIF3( output3 += 4; ) stbIF4( output4 += 4; ) stbIF5( output5 += 4; ) stbIF6( output6 += 4; ) stbIF7( output7 += 4; )9698 }9699 }9700 #else9701 while ( ( (char*)input_end - (char*) input ) >= 16 ) 9702 {9703 float r0, r1, r2, r3;9704 STBIR_NO_UNROLL(input);97059706 r0 = input[0], r1 = input[1], r2 = input[2], r3 = input[3];97079708 #ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE9709 stbIF0( output0[0] += ( r0 * c0s ); output0[1] += ( r1 * c0s ); output0[2] += ( r2 * c0s ); output0[3] += ( r3 * c0s ); )9710 stbIF1( output1[0] += ( r0 * c1s ); output1[1] += ( r1 * c1s ); output1[2] += ( r2 * c1s ); output1[3] += ( r3 * c1s ); )9711 stbIF2( output2[0] += ( r0 * c2s ); output2[1] += ( r1 * c2s ); output2[2] += ( r2 * c2s ); output2[3] += ( r3 * c2s ); )9712 stbIF3( output3[0] += ( r0 * c3s ); output3[1] += ( r1 * c3s ); output3[2] += ( r2 * c3s ); output3[3] += ( r3 * c3s ); )9713 stbIF4( output4[0] += ( r0 * c4s ); output4[1] += ( r1 * c4s ); output4[2] += ( r2 * c4s ); output4[3] += ( r3 * c4s ); )9714 stbIF5( output5[0] += ( r0 * c5s ); output5[1] += ( r1 * c5s ); output5[2] += ( r2 * c5s ); output5[3] += ( r3 * c5s ); )9715 stbIF6( output6[0] += ( r0 * c6s ); output6[1] += ( r1 * c6s ); output6[2] += ( r2 * c6s ); output6[3] += ( r3 * c6s ); )9716 stbIF7( output7[0] += ( r0 * c7s ); output7[1] += ( r1 * c7s ); output7[2] += ( r2 * c7s ); output7[3] += ( r3 * c7s ); )9717 #else9718 stbIF0( output0[0] = ( r0 * c0s ); output0[1] = ( r1 * c0s ); output0[2] = ( r2 * c0s ); output0[3] = ( r3 * c0s ); )9719 stbIF1( output1[0] = ( r0 * c1s ); output1[1] = ( r1 * c1s ); output1[2] = ( r2 * c1s ); output1[3] = ( r3 * c1s ); )9720 stbIF2( output2[0] = ( r0 * c2s ); output2[1] = ( r1 * c2s ); output2[2] = ( r2 * c2s ); output2[3] = ( r3 * c2s ); )9721 stbIF3( output3[0] = ( r0 * c3s ); output3[1] = ( r1 * c3s ); output3[2] = ( r2 * c3s ); output3[3] = ( r3 * c3s ); )9722 stbIF4( output4[0] = ( r0 * c4s ); output4[1] = ( r1 * c4s ); output4[2] = ( r2 * c4s ); output4[3] = ( r3 * c4s ); )9723 stbIF5( output5[0] = ( r0 * c5s ); output5[1] = ( r1 * c5s ); output5[2] = ( r2 * c5s ); output5[3] = ( r3 * c5s ); )9724 stbIF6( output6[0] = ( r0 * c6s ); output6[1] = ( r1 * c6s ); output6[2] = ( r2 * c6s ); output6[3] = ( r3 * c6s ); )9725 stbIF7( output7[0] = ( r0 * c7s ); output7[1] = ( r1 * c7s ); output7[2] = ( r2 * c7s ); output7[3] = ( r3 * c7s ); )9726 #endif97279728 input += 4;9729 stbIF0( output0 += 4; ) stbIF1( output1 += 4; ) stbIF2( output2 += 4; ) stbIF3( output3 += 4; ) stbIF4( output4 += 4; ) stbIF5( output5 += 4; ) stbIF6( output6 += 4; ) stbIF7( output7 += 4; )9730 }9731 #endif9732 while ( input < input_end ) 9733 {9734 float r = input[0];9735 STBIR_NO_UNROLL(output0);97369737 #ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE9738 stbIF0( output0[0] += ( r * c0s ); )9739 stbIF1( output1[0] += ( r * c1s ); )9740 stbIF2( output2[0] += ( r * c2s ); )9741 stbIF3( output3[0] += ( r * c3s ); )9742 stbIF4( output4[0] += ( r * c4s ); )9743 stbIF5( output5[0] += ( r * c5s ); )9744 stbIF6( output6[0] += ( r * c6s ); )9745 stbIF7( output7[0] += ( r * c7s ); )9746 #else9747 stbIF0( output0[0] = ( r * c0s ); )9748 stbIF1( output1[0] = ( r * c1s ); )9749 stbIF2( output2[0] = ( r * c2s ); )9750 stbIF3( output3[0] = ( r * c3s ); )9751 stbIF4( output4[0] = ( r * c4s ); )9752 stbIF5( output5[0] = ( r * c5s ); )9753 stbIF6( output6[0] = ( r * c6s ); )9754 stbIF7( output7[0] = ( r * c7s ); )9755 #endif97569757 ++input;9758 stbIF0( ++output0; ) stbIF1( ++output1; ) stbIF2( ++output2; ) stbIF3( ++output3; ) stbIF4( ++output4; ) stbIF5( ++output5; ) stbIF6( ++output6; ) stbIF7( ++output7; )9759 }9760}97619762static void STBIR_chans( stbir__vertical_gather_with_,_coeffs)( float * outputp, float const * vertical_coefficients, float const ** inputs, float const * input0_end )9763{9764 float STBIR_SIMD_STREAMOUT_PTR( * ) output = outputp;97659766 stbIF0( float const * input0 = inputs[0]; float c0s = vertical_coefficients[0]; )9767 stbIF1( float const * input1 = inputs[1]; float c1s = vertical_coefficients[1]; )9768 stbIF2( float const * input2 = inputs[2]; float c2s = vertical_coefficients[2]; )9769 stbIF3( float const * input3 = inputs[3]; float c3s = vertical_coefficients[3]; )9770 stbIF4( float const * input4 = inputs[4]; float c4s = vertical_coefficients[4]; )9771 stbIF5( float const * input5 = inputs[5]; float c5s = vertical_coefficients[5]; )9772 stbIF6( float const * input6 = inputs[6]; float c6s = vertical_coefficients[6]; )9773 stbIF7( float const * input7 = inputs[7]; float c7s = vertical_coefficients[7]; )97749775#if ( STBIR__vertical_channels == 1 ) && !defined(STB_IMAGE_RESIZE_VERTICAL_CONTINUE)9776 // check single channel one weight9777 if ( ( c0s >= (1.0f-0.000001f) ) && ( c0s <= (1.0f+0.000001f) ) )9778 {9779 STBIR_MEMCPY( output, input0, (char*)input0_end - (char*)input0 );9780 return;9781 }9782#endif 97839784 #ifdef STBIR_SIMD9785 {9786 stbIF0(stbir__simdfX c0 = stbir__simdf_frepX( c0s ); )9787 stbIF1(stbir__simdfX c1 = stbir__simdf_frepX( c1s ); )9788 stbIF2(stbir__simdfX c2 = stbir__simdf_frepX( c2s ); )9789 stbIF3(stbir__simdfX c3 = stbir__simdf_frepX( c3s ); )9790 stbIF4(stbir__simdfX c4 = stbir__simdf_frepX( c4s ); )9791 stbIF5(stbir__simdfX c5 = stbir__simdf_frepX( c5s ); )9792 stbIF6(stbir__simdfX c6 = stbir__simdf_frepX( c6s ); )9793 stbIF7(stbir__simdfX c7 = stbir__simdf_frepX( c7s ); )9794 9795 while ( ( (char*)input0_end - (char*) input0 ) >= (16*stbir__simdfX_float_count) ) 9796 {9797 stbir__simdfX o0, o1, o2, o3, r0, r1, r2, r3;9798 STBIR_SIMD_NO_UNROLL(output);97999800 // prefetch four loop iterations ahead (doesn't affect much for small resizes, but helps with big ones)9801 stbIF0( stbir__prefetch( input0 + (16*stbir__simdfX_float_count) ); ) 9802 stbIF1( stbir__prefetch( input1 + (16*stbir__simdfX_float_count) ); )9803 stbIF2( stbir__prefetch( input2 + (16*stbir__simdfX_float_count) ); )9804 stbIF3( stbir__prefetch( input3 + (16*stbir__simdfX_float_count) ); )9805 stbIF4( stbir__prefetch( input4 + (16*stbir__simdfX_float_count) ); )9806 stbIF5( stbir__prefetch( input5 + (16*stbir__simdfX_float_count) ); )9807 stbIF6( stbir__prefetch( input6 + (16*stbir__simdfX_float_count) ); )9808 stbIF7( stbir__prefetch( input7 + (16*stbir__simdfX_float_count) ); )98099810 #ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE9811 stbIF0( stbir__simdfX_load( o0, output ); stbir__simdfX_load( o1, output+stbir__simdfX_float_count ); stbir__simdfX_load( o2, output+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( o3, output+(3*stbir__simdfX_float_count) );9812 stbir__simdfX_load( r0, input0 ); stbir__simdfX_load( r1, input0+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input0+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input0+(3*stbir__simdfX_float_count) );9813 stbir__simdfX_madd( o0, o0, r0, c0 ); stbir__simdfX_madd( o1, o1, r1, c0 ); stbir__simdfX_madd( o2, o2, r2, c0 ); stbir__simdfX_madd( o3, o3, r3, c0 ); )9814 #else9815 stbIF0( stbir__simdfX_load( r0, input0 ); stbir__simdfX_load( r1, input0+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input0+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input0+(3*stbir__simdfX_float_count) );9816 stbir__simdfX_mult( o0, r0, c0 ); stbir__simdfX_mult( o1, r1, c0 ); stbir__simdfX_mult( o2, r2, c0 ); stbir__simdfX_mult( o3, r3, c0 ); )9817 #endif98189819 stbIF1( stbir__simdfX_load( r0, input1 ); stbir__simdfX_load( r1, input1+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input1+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input1+(3*stbir__simdfX_float_count) );9820 stbir__simdfX_madd( o0, o0, r0, c1 ); stbir__simdfX_madd( o1, o1, r1, c1 ); stbir__simdfX_madd( o2, o2, r2, c1 ); stbir__simdfX_madd( o3, o3, r3, c1 ); )9821 stbIF2( stbir__simdfX_load( r0, input2 ); stbir__simdfX_load( r1, input2+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input2+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input2+(3*stbir__simdfX_float_count) );9822 stbir__simdfX_madd( o0, o0, r0, c2 ); stbir__simdfX_madd( o1, o1, r1, c2 ); stbir__simdfX_madd( o2, o2, r2, c2 ); stbir__simdfX_madd( o3, o3, r3, c2 ); )9823 stbIF3( stbir__simdfX_load( r0, input3 ); stbir__simdfX_load( r1, input3+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input3+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input3+(3*stbir__simdfX_float_count) );9824 stbir__simdfX_madd( o0, o0, r0, c3 ); stbir__simdfX_madd( o1, o1, r1, c3 ); stbir__simdfX_madd( o2, o2, r2, c3 ); stbir__simdfX_madd( o3, o3, r3, c3 ); )9825 stbIF4( stbir__simdfX_load( r0, input4 ); stbir__simdfX_load( r1, input4+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input4+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input4+(3*stbir__simdfX_float_count) );9826 stbir__simdfX_madd( o0, o0, r0, c4 ); stbir__simdfX_madd( o1, o1, r1, c4 ); stbir__simdfX_madd( o2, o2, r2, c4 ); stbir__simdfX_madd( o3, o3, r3, c4 ); )9827 stbIF5( stbir__simdfX_load( r0, input5 ); stbir__simdfX_load( r1, input5+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input5+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input5+(3*stbir__simdfX_float_count) );9828 stbir__simdfX_madd( o0, o0, r0, c5 ); stbir__simdfX_madd( o1, o1, r1, c5 ); stbir__simdfX_madd( o2, o2, r2, c5 ); stbir__simdfX_madd( o3, o3, r3, c5 ); )9829 stbIF6( stbir__simdfX_load( r0, input6 ); stbir__simdfX_load( r1, input6+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input6+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input6+(3*stbir__simdfX_float_count) );9830 stbir__simdfX_madd( o0, o0, r0, c6 ); stbir__simdfX_madd( o1, o1, r1, c6 ); stbir__simdfX_madd( o2, o2, r2, c6 ); stbir__simdfX_madd( o3, o3, r3, c6 ); )9831 stbIF7( stbir__simdfX_load( r0, input7 ); stbir__simdfX_load( r1, input7+stbir__simdfX_float_count ); stbir__simdfX_load( r2, input7+(2*stbir__simdfX_float_count) ); stbir__simdfX_load( r3, input7+(3*stbir__simdfX_float_count) );9832 stbir__simdfX_madd( o0, o0, r0, c7 ); stbir__simdfX_madd( o1, o1, r1, c7 ); stbir__simdfX_madd( o2, o2, r2, c7 ); stbir__simdfX_madd( o3, o3, r3, c7 ); )98339834 stbir__simdfX_store( output, o0 ); stbir__simdfX_store( output+stbir__simdfX_float_count, o1 ); stbir__simdfX_store( output+(2*stbir__simdfX_float_count), o2 ); stbir__simdfX_store( output+(3*stbir__simdfX_float_count), o3 );9835 output += (4*stbir__simdfX_float_count);9836 stbIF0( input0 += (4*stbir__simdfX_float_count); ) stbIF1( input1 += (4*stbir__simdfX_float_count); ) stbIF2( input2 += (4*stbir__simdfX_float_count); ) stbIF3( input3 += (4*stbir__simdfX_float_count); ) stbIF4( input4 += (4*stbir__simdfX_float_count); ) stbIF5( input5 += (4*stbir__simdfX_float_count); ) stbIF6( input6 += (4*stbir__simdfX_float_count); ) stbIF7( input7 += (4*stbir__simdfX_float_count); )9837 }98389839 while ( ( (char*)input0_end - (char*) input0 ) >= 16 ) 9840 {9841 stbir__simdf o0, r0;9842 STBIR_SIMD_NO_UNROLL(output);98439844 #ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE9845 stbIF0( stbir__simdf_load( o0, output ); stbir__simdf_load( r0, input0 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c0 ) ); )9846 #else9847 stbIF0( stbir__simdf_load( r0, input0 ); stbir__simdf_mult( o0, r0, stbir__if_simdf8_cast_to_simdf4( c0 ) ); )9848 #endif9849 stbIF1( stbir__simdf_load( r0, input1 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c1 ) ); )9850 stbIF2( stbir__simdf_load( r0, input2 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c2 ) ); )9851 stbIF3( stbir__simdf_load( r0, input3 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c3 ) ); )9852 stbIF4( stbir__simdf_load( r0, input4 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c4 ) ); )9853 stbIF5( stbir__simdf_load( r0, input5 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c5 ) ); )9854 stbIF6( stbir__simdf_load( r0, input6 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c6 ) ); )9855 stbIF7( stbir__simdf_load( r0, input7 ); stbir__simdf_madd( o0, o0, r0, stbir__if_simdf8_cast_to_simdf4( c7 ) ); )98569857 stbir__simdf_store( output, o0 );9858 output += 4;9859 stbIF0( input0 += 4; ) stbIF1( input1 += 4; ) stbIF2( input2 += 4; ) stbIF3( input3 += 4; ) stbIF4( input4 += 4; ) stbIF5( input5 += 4; ) stbIF6( input6 += 4; ) stbIF7( input7 += 4; )9860 }9861 }9862 #else9863 while ( ( (char*)input0_end - (char*) input0 ) >= 16 ) 9864 {9865 float o0, o1, o2, o3;9866 STBIR_NO_UNROLL(output);9867 #ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE9868 stbIF0( o0 = output[0] + input0[0] * c0s; o1 = output[1] + input0[1] * c0s; o2 = output[2] + input0[2] * c0s; o3 = output[3] + input0[3] * c0s; )9869 #else9870 stbIF0( o0 = input0[0] * c0s; o1 = input0[1] * c0s; o2 = input0[2] * c0s; o3 = input0[3] * c0s; )9871 #endif9872 stbIF1( o0 += input1[0] * c1s; o1 += input1[1] * c1s; o2 += input1[2] * c1s; o3 += input1[3] * c1s; )9873 stbIF2( o0 += input2[0] * c2s; o1 += input2[1] * c2s; o2 += input2[2] * c2s; o3 += input2[3] * c2s; )9874 stbIF3( o0 += input3[0] * c3s; o1 += input3[1] * c3s; o2 += input3[2] * c3s; o3 += input3[3] * c3s; )9875 stbIF4( o0 += input4[0] * c4s; o1 += input4[1] * c4s; o2 += input4[2] * c4s; o3 += input4[3] * c4s; )9876 stbIF5( o0 += input5[0] * c5s; o1 += input5[1] * c5s; o2 += input5[2] * c5s; o3 += input5[3] * c5s; )9877 stbIF6( o0 += input6[0] * c6s; o1 += input6[1] * c6s; o2 += input6[2] * c6s; o3 += input6[3] * c6s; )9878 stbIF7( o0 += input7[0] * c7s; o1 += input7[1] * c7s; o2 += input7[2] * c7s; o3 += input7[3] * c7s; )9879 output[0] = o0; output[1] = o1; output[2] = o2; output[3] = o3;9880 output += 4;9881 stbIF0( input0 += 4; ) stbIF1( input1 += 4; ) stbIF2( input2 += 4; ) stbIF3( input3 += 4; ) stbIF4( input4 += 4; ) stbIF5( input5 += 4; ) stbIF6( input6 += 4; ) stbIF7( input7 += 4; )9882 }9883 #endif9884 while ( input0 < input0_end ) 9885 {9886 float o0;9887 STBIR_NO_UNROLL(output);9888 #ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE9889 stbIF0( o0 = output[0] + input0[0] * c0s; )9890 #else9891 stbIF0( o0 = input0[0] * c0s; )9892 #endif9893 stbIF1( o0 += input1[0] * c1s; )9894 stbIF2( o0 += input2[0] * c2s; )9895 stbIF3( o0 += input3[0] * c3s; )9896 stbIF4( o0 += input4[0] * c4s; )9897 stbIF5( o0 += input5[0] * c5s; )9898 stbIF6( o0 += input6[0] * c6s; )9899 stbIF7( o0 += input7[0] * c7s; )9900 output[0] = o0; 9901 ++output;9902 stbIF0( ++input0; ) stbIF1( ++input1; ) stbIF2( ++input2; ) stbIF3( ++input3; ) stbIF4( ++input4; ) stbIF5( ++input5; ) stbIF6( ++input6; ) stbIF7( ++input7; )9903 }9904}99059906#undef stbIF09907#undef stbIF19908#undef stbIF29909#undef stbIF39910#undef stbIF49911#undef stbIF59912#undef stbIF69913#undef stbIF79914#undef STB_IMAGE_RESIZE_DO_VERTICALS9915#undef STBIR__vertical_channels9916#undef STB_IMAGE_RESIZE_DO_HORIZONTALS9917#undef STBIR_strs_join249918#undef STBIR_strs_join149919#undef STBIR_chans9920#ifdef STB_IMAGE_RESIZE_VERTICAL_CONTINUE9921#undef STB_IMAGE_RESIZE_VERTICAL_CONTINUE9922#endif99239924#else // !STB_IMAGE_RESIZE_DO_VERTICALS99259926#define STBIR_chans( start, end ) STBIR_strs_join1(start,STBIR__horizontal_channels,end)99279928#ifndef stbir__2_coeff_only9929#define stbir__2_coeff_only() \9930 stbir__1_coeff_only(); \9931 stbir__1_coeff_remnant(1); 9932#endif99339934#ifndef stbir__2_coeff_remnant9935#define stbir__2_coeff_remnant( ofs ) \9936 stbir__1_coeff_remnant(ofs); \9937 stbir__1_coeff_remnant((ofs)+1); 9938#endif9939 9940#ifndef stbir__3_coeff_only9941#define stbir__3_coeff_only() \9942 stbir__2_coeff_only(); \9943 stbir__1_coeff_remnant(2); 9944#endif9945 9946#ifndef stbir__3_coeff_remnant9947#define stbir__3_coeff_remnant( ofs ) \9948 stbir__2_coeff_remnant(ofs); \9949 stbir__1_coeff_remnant((ofs)+2); 9950#endif99519952#ifndef stbir__3_coeff_setup9953#define stbir__3_coeff_setup()9954#endif99559956#ifndef stbir__4_coeff_start9957#define stbir__4_coeff_start() \9958 stbir__2_coeff_only(); \9959 stbir__2_coeff_remnant(2); 9960#endif9961 9962#ifndef stbir__4_coeff_continue_from_49963#define stbir__4_coeff_continue_from_4( ofs ) \9964 stbir__2_coeff_remnant(ofs); \9965 stbir__2_coeff_remnant((ofs)+2); 9966#endif99679968#ifndef stbir__store_output_tiny9969#define stbir__store_output_tiny stbir__store_output9970#endif99719972static void STBIR_chans( stbir__horizontal_gather_,_channels_with_1_coeff)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width )9973{9974 float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels;9975 float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer;9976 do {9977 float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; 9978 float const * hc = horizontal_coefficients;9979 stbir__1_coeff_only();9980 stbir__store_output_tiny();9981 } while ( output < output_end );9982}99839984static void STBIR_chans( stbir__horizontal_gather_,_channels_with_2_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width )9985{9986 float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels;9987 float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer;9988 do {9989 float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; 9990 float const * hc = horizontal_coefficients;9991 stbir__2_coeff_only();9992 stbir__store_output_tiny();9993 } while ( output < output_end );9994}99959996static void STBIR_chans( stbir__horizontal_gather_,_channels_with_3_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width )9997{9998 float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels;9999 float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer;10000 do {10001 float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; 10002 float const * hc = horizontal_coefficients;10003 stbir__3_coeff_only();10004 stbir__store_output_tiny();10005 } while ( output < output_end );10006}1000710008static void STBIR_chans( stbir__horizontal_gather_,_channels_with_4_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width )10009{10010 float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels;10011 float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer;10012 do {10013 float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; 10014 float const * hc = horizontal_coefficients;10015 stbir__4_coeff_start();10016 stbir__store_output();10017 } while ( output < output_end );10018}1001910020static void STBIR_chans( stbir__horizontal_gather_,_channels_with_5_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width )10021{10022 float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels;10023 float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer;10024 do {10025 float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; 10026 float const * hc = horizontal_coefficients;10027 stbir__4_coeff_start();10028 stbir__1_coeff_remnant(4);10029 stbir__store_output();10030 } while ( output < output_end );10031}1003210033static void STBIR_chans( stbir__horizontal_gather_,_channels_with_6_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width )10034{10035 float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels;10036 float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer;10037 do {10038 float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; 10039 float const * hc = horizontal_coefficients;10040 stbir__4_coeff_start();10041 stbir__2_coeff_remnant(4);10042 stbir__store_output();10043 } while ( output < output_end );10044}1004510046static void STBIR_chans( stbir__horizontal_gather_,_channels_with_7_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width )10047{10048 float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels;10049 float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer;10050 stbir__3_coeff_setup();10051 do {10052 float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; 10053 float const * hc = horizontal_coefficients;10054 10055 stbir__4_coeff_start();10056 stbir__3_coeff_remnant(4);10057 stbir__store_output();10058 } while ( output < output_end );10059}1006010061static void STBIR_chans( stbir__horizontal_gather_,_channels_with_8_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width )10062{10063 float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels;10064 float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer;10065 do {10066 float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; 10067 float const * hc = horizontal_coefficients;10068 stbir__4_coeff_start();10069 stbir__4_coeff_continue_from_4(4);10070 stbir__store_output();10071 } while ( output < output_end );10072}1007310074static void STBIR_chans( stbir__horizontal_gather_,_channels_with_9_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width )10075{10076 float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels;10077 float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer;10078 do {10079 float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; 10080 float const * hc = horizontal_coefficients;10081 stbir__4_coeff_start();10082 stbir__4_coeff_continue_from_4(4);10083 stbir__1_coeff_remnant(8);10084 stbir__store_output();10085 } while ( output < output_end );10086}1008710088static void STBIR_chans( stbir__horizontal_gather_,_channels_with_10_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width )10089{10090 float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels;10091 float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer;10092 do {10093 float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; 10094 float const * hc = horizontal_coefficients;10095 stbir__4_coeff_start();10096 stbir__4_coeff_continue_from_4(4);10097 stbir__2_coeff_remnant(8);10098 stbir__store_output();10099 } while ( output < output_end );10100}1010110102static void STBIR_chans( stbir__horizontal_gather_,_channels_with_11_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width )10103{10104 float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels;10105 float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer;10106 stbir__3_coeff_setup();10107 do {10108 float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; 10109 float const * hc = horizontal_coefficients;10110 stbir__4_coeff_start();10111 stbir__4_coeff_continue_from_4(4);10112 stbir__3_coeff_remnant(8);10113 stbir__store_output();10114 } while ( output < output_end );10115}1011610117static void STBIR_chans( stbir__horizontal_gather_,_channels_with_12_coeffs)( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width )10118{10119 float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels;10120 float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer;10121 do {10122 float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; 10123 float const * hc = horizontal_coefficients;10124 stbir__4_coeff_start();10125 stbir__4_coeff_continue_from_4(4);10126 stbir__4_coeff_continue_from_4(8);10127 stbir__store_output();10128 } while ( output < output_end );10129}1013010131static void STBIR_chans( stbir__horizontal_gather_,_channels_with_n_coeffs_mod0 )( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width )10132{10133 float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels;10134 float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer;10135 do {10136 float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; 10137 int n = ( ( horizontal_contributors->n1 - horizontal_contributors->n0 + 1 ) - 4 + 3 ) >> 2; 10138 float const * hc = horizontal_coefficients;1013910140 stbir__4_coeff_start();10141 do {10142 hc += 4;10143 decode += STBIR__horizontal_channels * 4;10144 stbir__4_coeff_continue_from_4( 0 );10145 --n;10146 } while ( n > 0 );10147 stbir__store_output();10148 } while ( output < output_end );10149}1015010151static void STBIR_chans( stbir__horizontal_gather_,_channels_with_n_coeffs_mod1 )( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width )10152{10153 float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels;10154 float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer;10155 do {10156 float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; 10157 int n = ( ( horizontal_contributors->n1 - horizontal_contributors->n0 + 1 ) - 5 + 3 ) >> 2; 10158 float const * hc = horizontal_coefficients;1015910160 stbir__4_coeff_start();10161 do {10162 hc += 4;10163 decode += STBIR__horizontal_channels * 4;10164 stbir__4_coeff_continue_from_4( 0 );10165 --n;10166 } while ( n > 0 );10167 stbir__1_coeff_remnant( 4 ); 10168 stbir__store_output();10169 } while ( output < output_end );10170}1017110172static void STBIR_chans( stbir__horizontal_gather_,_channels_with_n_coeffs_mod2 )( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width )10173{10174 float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels;10175 float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer;10176 do {10177 float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; 10178 int n = ( ( horizontal_contributors->n1 - horizontal_contributors->n0 + 1 ) - 6 + 3 ) >> 2; 10179 float const * hc = horizontal_coefficients;1018010181 stbir__4_coeff_start();10182 do {10183 hc += 4;10184 decode += STBIR__horizontal_channels * 4;10185 stbir__4_coeff_continue_from_4( 0 );10186 --n;10187 } while ( n > 0 );10188 stbir__2_coeff_remnant( 4 ); 1018910190 stbir__store_output();10191 } while ( output < output_end );10192}1019310194static void STBIR_chans( stbir__horizontal_gather_,_channels_with_n_coeffs_mod3 )( float * output_buffer, unsigned int output_sub_size, float const * decode_buffer, stbir__contributors const * horizontal_contributors, float const * horizontal_coefficients, int coefficient_width )10195{10196 float const * output_end = output_buffer + output_sub_size * STBIR__horizontal_channels;10197 float STBIR_SIMD_STREAMOUT_PTR( * ) output = output_buffer;10198 stbir__3_coeff_setup();10199 do {10200 float const * decode = decode_buffer + horizontal_contributors->n0 * STBIR__horizontal_channels; 10201 int n = ( ( horizontal_contributors->n1 - horizontal_contributors->n0 + 1 ) - 7 + 3 ) >> 2; 10202 float const * hc = horizontal_coefficients;1020310204 stbir__4_coeff_start();10205 do {10206 hc += 4;10207 decode += STBIR__horizontal_channels * 4;10208 stbir__4_coeff_continue_from_4( 0 );10209 --n;10210 } while ( n > 0 );10211 stbir__3_coeff_remnant( 4 ); 1021210213 stbir__store_output();10214 } while ( output < output_end );10215}1021610217static stbir__horizontal_gather_channels_func * STBIR_chans(stbir__horizontal_gather_,_channels_with_n_coeffs_funcs)[4]=10218{10219 STBIR_chans(stbir__horizontal_gather_,_channels_with_n_coeffs_mod0), 10220 STBIR_chans(stbir__horizontal_gather_,_channels_with_n_coeffs_mod1), 10221 STBIR_chans(stbir__horizontal_gather_,_channels_with_n_coeffs_mod2), 10222 STBIR_chans(stbir__horizontal_gather_,_channels_with_n_coeffs_mod3), 10223};1022410225static stbir__horizontal_gather_channels_func * STBIR_chans(stbir__horizontal_gather_,_channels_funcs)[12]=10226{10227 STBIR_chans(stbir__horizontal_gather_,_channels_with_1_coeff), 10228 STBIR_chans(stbir__horizontal_gather_,_channels_with_2_coeffs), 10229 STBIR_chans(stbir__horizontal_gather_,_channels_with_3_coeffs),10230 STBIR_chans(stbir__horizontal_gather_,_channels_with_4_coeffs), 10231 STBIR_chans(stbir__horizontal_gather_,_channels_with_5_coeffs), 10232 STBIR_chans(stbir__horizontal_gather_,_channels_with_6_coeffs), 10233 STBIR_chans(stbir__horizontal_gather_,_channels_with_7_coeffs),10234 STBIR_chans(stbir__horizontal_gather_,_channels_with_8_coeffs), 10235 STBIR_chans(stbir__horizontal_gather_,_channels_with_9_coeffs), 10236 STBIR_chans(stbir__horizontal_gather_,_channels_with_10_coeffs), 10237 STBIR_chans(stbir__horizontal_gather_,_channels_with_11_coeffs), 10238 STBIR_chans(stbir__horizontal_gather_,_channels_with_12_coeffs), 10239};1024010241#undef STBIR__horizontal_channels10242#undef STB_IMAGE_RESIZE_DO_HORIZONTALS10243#undef stbir__1_coeff_only10244#undef stbir__1_coeff_remnant10245#undef stbir__2_coeff_only10246#undef stbir__2_coeff_remnant10247#undef stbir__3_coeff_only10248#undef stbir__3_coeff_remnant10249#undef stbir__3_coeff_setup10250#undef stbir__4_coeff_start10251#undef stbir__4_coeff_continue_from_410252#undef stbir__store_output10253#undef stbir__store_output_tiny10254#undef STBIR_chans1025510256#endif // HORIZONALS1025710258#undef STBIR_strs_join210259#undef STBIR_strs_join11026010261#endif // STB_IMAGE_RESIZE_DO_HORIZONTALS/VERTICALS/CODERS1026210263/*10264------------------------------------------------------------------------------10265This software is available under 2 licenses -- choose whichever you prefer.10266------------------------------------------------------------------------------10267ALTERNATIVE A - MIT License10268Copyright (c) 2017 Sean Barrett10269Permission is hereby granted, free of charge, to any person obtaining a copy of 10270this software and associated documentation files (the "Software"), to deal in 10271the Software without restriction, including without limitation the rights to 10272use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 10273of the Software, and to permit persons to whom the Software is furnished to do 10274so, subject to the following conditions:10275The above copyright notice and this permission notice shall be included in all 10276copies or substantial portions of the Software.10277THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 10278IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 10279FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 10280AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 10281LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 10282OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 10283SOFTWARE.10284------------------------------------------------------------------------------10285ALTERNATIVE B - Public Domain (www.unlicense.org)10286This is free and unencumbered software released into the public domain.10287Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 10288software, either in source code form or as a compiled binary, for any purpose, 10289commercial or non-commercial, and by any means.10290In jurisdictions that recognize copyright laws, the author or authors of this 10291software dedicate any and all copyright interest in the software to the public 10292domain. We make this dedication for the benefit of the public at large and to 10293the detriment of our heirs and successors. We intend this dedication to be an 10294overt act of relinquishment in perpetuity of all present and future rights to 10295this software under copyright law.10296THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 10297IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 10298FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 10299AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 10300ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 10301WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.10302------------------------------------------------------------------------------10303*/