diff --git a/stb_image_resize.h b/stb_image_resize.h index d247ade..c68a778 100644 --- a/stb_image_resize.h +++ b/stb_image_resize.h @@ -7,8 +7,9 @@ #define STBIR_ASSERT(x) to avoid using assert.h. - #define STBIR_MALLOC and STBIR_FREE to avoid using stdlib.h malloc. This will apply - to all functions except stbir_resize_arbitrary(), which doesn't allocate memory. + #define STBIR_MALLOC(context,size) and STBIR_FREE(context,ptr) to avoid using stdlib.h malloc. + Each function makes exactly one call to malloc/free, so to avoid allocations, + pass in a temp memory block as context and return that from MALLOC. QUICK NOTES: Written with emphasis on usage and speed. Only the resize operation is @@ -18,10 +19,10 @@ supported filters see the stbir_filter enum. To add a new filter, write a filter function and add it to stbir__filter_info_table. - Latest revisions: - 0.50 (2014-07-29) first released version + STBIR_MAX_CHANNELS: defaults to 16, if you need more, bump it up - See end of file for full revision history. + Revisions: + 0.50 (2014-??-??) first released version TODO: Installable filters @@ -36,82 +37,6 @@ #ifndef STBIR_INCLUDE_STB_IMAGE_RESIZE_H #define STBIR_INCLUDE_STB_IMAGE_RESIZE_H -// Basic usage: -// result = stbir_resize_uint8_srgb(input_data, input_w, input_h, output_data, output_w, output_h, channels, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP); -// * input_data is your supplied pixels. -// * output_data will be the resized pixels. It should be of size output_w * output_h * channels -// * Returned result is 1 for success or 0 in case of an error. In the case of an error an assert with be triggered, #define STBIR_ASSERT() to see it. -// * If you're unsure of which filter to use, Catmull-Rom is a good upsampling filter and Mitchell is a good downsampling filter. -// -// -// Data types provided: uint8, uint16, uint32, float. -// -// -// Other function groups are provided, one for each data type, for more advanced functionality: -// -// stbir_resize_type_alphaweighted(input_data, input_w, input_h, output_data, output_w, output_h, channels, alpha_channel, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB) -// * alpha_channel - if nonnegative, this channel will be multiplied into all other channels before resampling, then divided back out after. -// -// stbir_resize_type_subpixel(input_data, input_w, input_h, output_data, output_w, output_h, s0, t0, s1, t1, channels, filter, edge) -// * 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. -// -// -// All functionality is offered in this function: -// -// result = stbir_resize_arbitrary(input_data, input_w, input_h, input_stride_in_bytes, -// output_data, output_w, output_h, output_stride_in_bytes, -// s0, t0, s1, t1, -// channels, alpha_channel, flags, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); -// -// * input_stride_in_bytes and output_stride_in_bytes can be 0. If so they will be automatically calculated as width * channels. -// * 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. -// * flags are from the stbir_flags enum and should be bitwise OR'd together. -// * First edge parameter is for horizontal edge behavior, second is for vertical. -// * Returned result is 1 for success or 0 in case of an error. In the case of an error an assert with be triggered, #define STBIR_ASSERT() to see it. -// * Memory required grows approximately linearly with input and output size, but with discontinuities at input_w == output_w and input_h == output_height. -// * To use temporary memory, define an STBIR_MALLOC that returns the temp memory and make STBIR_FREE do nothing--each function only ever allocates one block - - -typedef enum -{ - STBIR_FILTER_NEAREST = 1, - STBIR_FILTER_BILINEAR = 2, - STBIR_FILTER_BICUBIC = 3, // A cubic b spline - STBIR_FILTER_CATMULLROM = 4, - STBIR_FILTER_MITCHELL = 5, -} stbir_filter; - -typedef enum -{ - STBIR_EDGE_CLAMP = 1, - STBIR_EDGE_REFLECT = 2, - STBIR_EDGE_WRAP = 3, -} stbir_edge; - -typedef enum -{ - STBIR_COLORSPACE_LINEAR, - STBIR_COLORSPACE_SRGB, - - STBIR_MAX_COLORSPACES, -} stbir_colorspace; - -typedef enum -{ - STBIR_TYPE_UINT8 , - STBIR_TYPE_UINT16, - STBIR_TYPE_UINT32, - STBIR_TYPE_FLOAT , - - STBIR_MAX_TYPES -} stbir_type; - -typedef enum -{ - STBIR_FLAG_NONPREMUL_ALPHA = (1 << 0), // The specified alpha channel will be multiplied into all other channels before resampling, then divided back out after. - STBIR_FLAG_GAMMA_CORRECT_ALPHA = (1 << 1), // The specified alpha channel should be handled as a linear value even when doing sRGB operations. -} stbir_flags; - typedef unsigned char stbir_uint8; #ifdef _MSC_VER @@ -123,88 +48,190 @@ typedef uint16_t stbir_uint16; typedef uint32_t stbir_uint32; #endif -typedef unsigned int stbir_size_t; // to avoid including a header for size_t - #ifdef STB_IMAGE_RESIZE_STATIC -#define STBRDEF static +#define STBIRDEF static #else #ifdef __cplusplus -#define STBRDEF extern "C" +#define STBIRDEF extern "C" #else -#define STBRDEF extern +#define STBIRDEF extern #endif #endif + ////////////////////////////////////////////////////////////////////////////// // -// PRIMARY API - sRGB type-safe image resizing. +// Easy-to-use API: // +// * "input pixels" points to an array of image data with 'num_channels' channels (e.g. RGB=3, RGBA=4) +// * input_w is input image width (x-axis), input_h is input image height (y-axis) +// * stride is the offset between successive rows of image data in memory, in bytes. you can +// specify 0 to mean packed continuously in memory +// * alpha channel is treated identically to other channels. +// * colorspace is linear or sRGB as specified by function name +// * returned result is 1 for success or 0 in case of an error. +// #define STBIR_ASSERT() to trigger an assert on parameter validation errors. +// * Memory required grows approximately linearly with input and output size, but with +// discontinuities at input_w == output_w and input_h == output_h. +// * These functions use a "default" resampling filter defined at compile time. To change the filter, +// you can change the compile-time defaults by #defining STBIR_DEFAULT_FILTER_UPSAMPLE +// and STBIR_DEFAULT_FILTER_DOWNSAMPLE, or you can use the medium-complexity API. -STBRDEF int stbir_resize_uint8_srgb(const stbir_uint8* input_data, int input_w, int input_h, - stbir_uint8* output_data, int output_w, int output_h, - int channels, stbir_filter filter, stbir_edge edge); +STBIRDEF int stbir_resize_uint8( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + int num_channels); -STBRDEF int stbir_resize_uint16_srgb(const stbir_uint16* input_data, int input_w, int input_h, - stbir_uint16* output_data, int output_w, int output_h, - int channels, stbir_filter filter, stbir_edge edge); - -STBRDEF int stbir_resize_uint32_srgb(const stbir_uint32* input_data, int input_w, int input_h, - stbir_uint32* output_data, int output_w, int output_h, - int channels, stbir_filter filter, stbir_edge edge); - -STBRDEF int stbir_resize_float_srgb(const float* input_data, int input_w, int input_h, - float* output_data, int output_w, int output_h, - int channels, stbir_filter filter, stbir_edge edge); +STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + float *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + int num_channels); -STBRDEF int stbir_resize_uint8_alphaweighted(const stbir_uint8* input_data, int input_w, int input_h, - stbir_uint8* output_data, int output_w, int output_h, - int channels, int alpha_channel, stbir_filter filter, stbir_edge edge, stbir_colorspace colorspace); +// The following functions interpret image data as gamma-corrected sRGB. +// Specify STBIR_ALPHA_CHANNEL_NONE if you have no alpha channel, +// or otherwise provide the index of the alpha channel. Flags value +// of 0 will probably do the right thing if you're not sure what +// the flags mean. -STBRDEF int stbir_resize_uint16_alphaweighted(const stbir_uint16* input_data, int input_w, int input_h, - stbir_uint16* output_data, int output_w, int output_h, - int channels, int alpha_channel, stbir_filter filter, stbir_edge edge, stbir_colorspace colorspace); +#define STBIR_ALPHA_CHANNEL_NONE -1 -STBRDEF int stbir_resize_uint32_alphaweighted(const stbir_uint32* input_data, int input_w, int input_h, - stbir_uint32* output_data, int output_w, int output_h, - int channels, int alpha_channel, stbir_filter filter, stbir_edge edge, stbir_colorspace colorspace); +#define STBIR_FLAG_PREMULTIPLIED_ALPHA (1 << 0) // If this flag is not set, the specified alpha channel will be multiplied into all other channels before resampling, then divided back out after. +#define STBIR_FLAG_ALPHA_USES_COLORSPACE (1 << 1) // The specified alpha channel should be handled as gamma-corrected value even when doing sRGB operations. -STBRDEF int stbir_resize_float_alphaweighted(const float* input_data, int input_w, int input_h, - float* output_data, int output_w, int output_h, - int channels, int alpha_channel, stbir_filter filter, stbir_edge edge, stbir_colorspace colorspace); +STBIRDEF int stbir_resize_uint8_srgb(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + int num_channels, int alpha_channel, int flags); -STBRDEF int stbir_resize_uint8_subpixel(const stbir_uint8* input_data, int input_w, int input_h, - stbir_uint8* output_data, int output_w, int output_h, - float s0, float t0, float s1, float t1, - int channels, stbir_filter filter, stbir_edge edge); +typedef enum +{ + STBIR_EDGE_CLAMP = 1, + STBIR_EDGE_REFLECT = 2, + STBIR_EDGE_WRAP = 3, + STBIR_EDGE_ZERO = 4, +} stbir_edge; -STBRDEF int stbir_resize_uint16_subpixel(const stbir_uint16* input_data, int input_w, int input_h, - stbir_uint16* output_data, int output_w, int output_h, - float s0, float t0, float s1, float t1, - int channels, stbir_filter filter, stbir_edge edge); +// This function adds the ability to specify how requests to sample off the edge of the image are handled. +STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + int num_channels, int alpha_channel, int flags, + stbir_edge edge_wrap_mode); -STBRDEF int stbir_resize_uint32_subpixel(const stbir_uint32* input_data, int input_w, int input_h, - stbir_uint32* output_data, int output_w, int output_h, - float s0, float t0, float s1, float t1, - int channels, stbir_filter filter, stbir_edge edge); +////////////////////////////////////////////////////////////////////////////// +// +// Medium-complexity API +// +// This extends the easy-to-use API as follows: +// +// * Alpha-channel can be processed separately +// * If alpha_channel is not STBIR_ALPHA_CHANNEL_NONE +// * Alpha channel will not be gamma corrected (unless flags&STBIR_FLAG_GAMMA_CORRECT) +// * Filters can be weighted by alpha channel (if flags&STBIR_FLAG_NONPREMUL_ALPHA) +// * Filter can be selected explicitly +// * uint16 image type +// * sRGB colorspace available for all types +// * context parameter for passing to STBIR_MALLOC -STBRDEF int stbir_resize_float_subpixel(const float* input_data, int input_w, int input_h, - float* output_data, int output_w, int output_h, - float s0, float t0, float s1, float t1, - int channels, stbir_filter filter, stbir_edge edge); +typedef enum +{ + STBIR_FILTER_DEFAULT = 0, // use same filter type that easy-to-use API chooses + STBIR_FILTER_NEAREST = 1, + STBIR_FILTER_BILINEAR = 2, + STBIR_FILTER_BICUBIC = 3, // A cubic b spline + STBIR_FILTER_CATMULLROM = 4, + STBIR_FILTER_MITCHELL = 5, +} stbir_filter; + +typedef enum +{ + STBIR_COLORSPACE_LINEAR, + STBIR_COLORSPACE_SRGB, + + STBIR_MAX_COLORSPACES, +} stbir_colorspace; + +// The following functions are all identical except for the type of the image data + +STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + int num_channels, int alpha_channel, int flags, + stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, + void *alloc_context); + +STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + stbir_uint16 *output_pixels , int output_w, int output_h, int output_stride_in_bytes, + int num_channels, int alpha_channel, int flags, + stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, + void *alloc_context); + +STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + float *output_pixels , int output_w, int output_h, int output_stride_in_bytes, + int num_channels, int alpha_channel, int flags, + stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, + void *alloc_context); -STBRDEF int stbir_resize_arbitrary(const void* input_data, int input_w, int input_h, int input_stride_in_bytes, - void* output_data, int output_w, int output_h, int output_stride_in_bytes, - float s0, float t0, float s1, float t1, - int channels, int alpha_channel, stbir_uint32 flags, stbir_type type, stbir_filter filter, stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace); + +////////////////////////////////////////////////////////////////////////////// +// +// Full-complexity API +// +// This extends the medium API as follows: +// +// * uint32 image type +// * not typesafe +// * separate filter types for each axis +// * separate edge modes for each axis +// * can specify scale explicitly for subpixel correctness +// * can specify image source tile using texture coordinates + +typedef enum +{ + STBIR_TYPE_UINT8 , + STBIR_TYPE_UINT16, + STBIR_TYPE_UINT32, + STBIR_TYPE_FLOAT , + + STBIR_MAX_TYPES +} stbir_datatype; + +STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + stbir_datatype datatype, + int num_channels, int alpha_channel, int flags, + stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, + stbir_filter filter_horizontal, stbir_filter filter_vertical, + stbir_colorspace space, void *alloc_context); + +STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + stbir_datatype datatype, + int num_channels, int alpha_channel, int flags, + stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, + stbir_filter filter_horizontal, stbir_filter filter_vertical, + stbir_colorspace space, void *alloc_context, + float x_scale, float y_scale, + float x_offset, float y_offset); + +STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + stbir_datatype datatype, + int num_channels, int alpha_channel, int flags, + stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, + stbir_filter filter_horizontal, stbir_filter filter_vertical, + stbir_colorspace space, void *alloc_context, + float s0, float t0, float s1, float t1); +// (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. + // // //// end header file ///////////////////////////////////////////////////// #endif // STBIR_INCLUDE_STB_IMAGE_RESIZE_H + + + + #ifdef STB_IMAGE_RESIZE_IMPLEMENTATION #ifndef STBIR_ASSERT @@ -229,11 +256,10 @@ STBRDEF int stbir_resize_arbitrary(const void* input_data, int input_w, int inpu #ifndef STBIR_MALLOC #include -#define STBIR_MALLOC(x) malloc(x) -#define STBIR_FREE(x) free(x) +#define STBIR_MALLOC(c,x) malloc(x) +#define STBIR_FREE(c,x) free(x) #endif - #ifndef _MSC_VER #ifdef __cplusplus #define stbir__inline inline @@ -256,7 +282,19 @@ typedef unsigned char stbir__validate_uint32[sizeof(stbir_uint32) == 4 ? 1 : -1] #define STBIR__ARRAY_SIZE(a) (sizeof((a))/sizeof((a)[0])) -// must match stbir_type +#ifndef STBIR_DEFAULT_FILTER_UPSAMPLE +#define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_CATMULLROM +#endif + +#ifndef STBIR_DEFAULT_FILTER_DOWNSAMPLE +#define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_MITCHELL +#endif + +#ifndef STBIR_MAX_CHANNELS +#define STBIR_MAX_CHANNELS 16 +#endif + +// must match stbir_datatype static unsigned char stbir__type_size[] = { 1, // STBIR_TYPE_UINT8 2, // STBIR_TYPE_UINT16 @@ -303,8 +341,9 @@ typedef struct int channels; int alpha_channel; stbir_uint32 flags; - stbir_type type; - stbir_filter filter; + stbir_datatype type; + stbir_filter horizontal_filter; + stbir_filter vertical_filter; stbir_edge edge_horizontal; stbir_edge edge_vertical; stbir_colorspace colorspace; @@ -377,21 +416,44 @@ static float stbir__srgb_uchar_to_linear_float[256] = { 0.982251f, 0.991102f, 1.0f }; -static unsigned char stbir__linear_uchar_to_srgb_uchar[256] = { - 0, 12, 21, 28, 33, 38, 42, 46, 49, 52, 55, 58, 61, 63, 66, 68, 70, 73, 75, 77, 79, 81, 82, 84, 86, 88, 89, 91, 93, 94, - 96, 97, 99, 100, 102, 103, 104, 106, 107, 109, 110, 111, 112, 114, 115, 116, 117, 118, 120, 121, 122, 123, 124, 125, 126, - 127, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 142, 143, 144, 145, 146, 147, 148, 149, 150, - 151, 151, 152, 153, 154, 155, 156, 157, 157, 158, 159, 160, 161, 161, 162, 163, 164, 165, 165, 166, 167, 168, 168, 169, - 170, 171, 171, 172, 173, 174, 174, 175, 176, 176, 177, 178, 179, 179, 180, 181, 181, 182, 183, 183, 184, 185, 185, 186, - 187, 187, 188, 189, 189, 190, 191, 191, 192, 193, 193, 194, 194, 195, 196, 196, 197, 197, 198, 199, 199, 200, 201, 201, - 202, 202, 203, 204, 204, 205, 205, 206, 206, 207, 208, 208, 209, 209, 210, 210, 211, 212, 212, 213, 213, 214, 214, 215, - 215, 216, 217, 217, 218, 218, 219, 219, 220, 220, 221, 221, 222, 222, 223, 223, 224, 224, 225, 226, 226, 227, 227, 228, - 228, 229, 229, 230, 230, 231, 231, 232, 232, 233, 233, 234, 234, 235, 235, 236, 236, 237, 237, 237, 238, 238, 239, 239, - 240, 240, 241, 241, 242, 242, 243, 243, 244, 244, 245, 245, 245, 246, 246, 247, 247, 248, 248, 249, 249, 250, 250, 251, - 251, 251, 252, 252, 253, 253, 254, 254, 255 +// sRGB transition values, scaled by 1<<28 +static int stbir__srgb_offset_to_linear_scaled[256] = +{ + 40579, 121738, 202897, 284056, 365216, 446375, 527534, 608693, + 689852, 771011, 852421, 938035, 1028466, 1123787, 1224073, 1329393, + 1439819, 1555418, 1676257, 1802402, 1933917, 2070867, 2213313, 2361317, + 2514938, 2674237, 2839271, 3010099, 3186776, 3369359, 3557903, 3752463, + 3953090, 4159840, 4372764, 4591913, 4817339, 5049091, 5287220, 5531775, + 5782804, 6040356, 6304477, 6575216, 6852618, 7136729, 7427596, 7725263, + 8029775, 8341176, 8659511, 8984821, 9317151, 9656544, 10003040, 10356683, + 10717513, 11085572, 11460901, 11843540, 12233529, 12630908, 13035717, 13447994, + 13867779, 14295110, 14730025, 15172563, 15622760, 16080655, 16546285, 17019686, + 17500894, 17989948, 18486882, 18991734, 19504536, 20025326, 20554138, 21091010, + 21635972, 22189062, 22750312, 23319758, 23897432, 24483368, 25077600, 25680162, + 26291086, 26910406, 27538152, 28174360, 28819058, 29472282, 30134062, 30804430, + 31483418, 32171058, 32867378, 33572412, 34286192, 35008744, 35740104, 36480296, + 37229356, 37987316, 38754196, 39530036, 40314860, 41108700, 41911584, 42723540, + 43544600, 44374792, 45214140, 46062680, 46920440, 47787444, 48663720, 49549300, + 50444212, 51348480, 52262136, 53185204, 54117712, 55059688, 56011160, 56972156, + 57942704, 58922824, 59912552, 60911908, 61920920, 62939616, 63968024, 65006168, + 66054072, 67111760, 68179272, 69256616, 70343832, 71440936, 72547952, 73664920, + 74791848, 75928776, 77075720, 78232704, 79399760, 80576904, 81764168, 82961576, + 84169152, 85386920, 86614904, 87853120, 89101608, 90360384, 91629480, 92908904, + 94198688, 95498864, 96809440, 98130456, 99461928, 100803872, 102156320, 103519296, + 104892824, 106276920, 107671616, 109076928, 110492880, 111919504, 113356808, 114804824, + 116263576, 117733080, 119213360, 120704448, 122206352, 123719104, 125242720, 126777232, + 128322648, 129879000, 131446312, 133024600, 134613888, 136214192, 137825552, 139447968, + 141081456, 142726080, 144381808, 146048704, 147726768, 149416016, 151116496, 152828192, + 154551168, 156285408, 158030944, 159787808, 161556000, 163335568, 165126512, 166928864, + 168742640, 170567856, 172404544, 174252704, 176112384, 177983568, 179866320, 181760640, + 183666528, 185584032, 187513168, 189453952, 191406400, 193370544, 195346384, 197333952, + 199333264, 201344352, 203367216, 205401904, 207448400, 209506752, 211576960, 213659056, + 215753056, 217858976, 219976832, 222106656, 224248464, 226402272, 228568096, 230745952, + 232935872, 235137872, 237351968, 239578176, 241816512, 244066992, 246329648, 248604512, + 250891568, 253190848, 255502368, 257826160, 260162240, 262510608, 264871312, 267244336, }; -float stbir__srgb_to_linear(float f) +static float stbir__srgb_to_linear(float f) { if (f <= 0.04045f) return f / 12.92f; @@ -399,7 +461,7 @@ float stbir__srgb_to_linear(float f) return (float)pow((f + 0.055f) / 1.055f, 2.4f); } -float stbir__linear_to_srgb(float f) +static float stbir__linear_to_srgb(float f) { if (f <= 0.0031308f) return f * 12.92f; @@ -407,7 +469,21 @@ float stbir__linear_to_srgb(float f) return 1.055f * (float)pow(f, 1 / 2.4f) - 0.055f; } +static unsigned char stbir__linear_to_srgb_uchar(float f) +{ + int x = (int) (f * (1 << 28)); // has headroom so you don't need to clamp + int v = 0; + if (x >= stbir__srgb_offset_to_linear_scaled[ v+128 ]) v += 128; + if (x >= stbir__srgb_offset_to_linear_scaled[ v+ 64 ]) v += 64; + if (x >= stbir__srgb_offset_to_linear_scaled[ v+ 32 ]) v += 32; + if (x >= stbir__srgb_offset_to_linear_scaled[ v+ 16 ]) v += 16; + if (x >= stbir__srgb_offset_to_linear_scaled[ v+ 8 ]) v += 8; + if (x >= stbir__srgb_offset_to_linear_scaled[ v+ 4 ]) v += 4; + if (x >= stbir__srgb_offset_to_linear_scaled[ v+ 2 ]) v += 2; + if (x >= stbir__srgb_offset_to_linear_scaled[ v+ 1 ]) v += 1; + return (unsigned char) v; +} static float stbir__filter_nearest(float x) { @@ -467,7 +543,7 @@ static float stbir__filter_mitchell(float x) static stbir__filter_info stbir__filter_info_table[] = { { NULL, 0.0f }, - { stbir__filter_nearest, 0.5f }, + { stbir__filter_nearest, 0.5f }, // 0.000001? { stbir__filter_bilinear, 1.0f }, { stbir__filter_bicubic, 2.0f }, { stbir__filter_catmullrom, 2.0f }, @@ -504,12 +580,12 @@ stbir__inline static int stbir__get_filter_pixel_width(stbir_filter filter, int stbir__inline static int stbir__get_filter_pixel_width_horizontal(stbir__info* stbir_info) { - return stbir__get_filter_pixel_width(stbir_info->filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale); + return stbir__get_filter_pixel_width(stbir_info->horizontal_filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale); } stbir__inline static int stbir__get_filter_pixel_width_vertical(stbir__info* stbir_info) { - return stbir__get_filter_pixel_width(stbir_info->filter, stbir_info->input_h, stbir_info->output_h, stbir_info->vertical_scale); + return stbir__get_filter_pixel_width(stbir_info->vertical_filter, stbir_info->input_h, stbir_info->output_h, stbir_info->vertical_scale); } // This is how much to expand buffers to account for filters seeking outside @@ -521,35 +597,26 @@ stbir__inline static int stbir__get_filter_pixel_margin(stbir_filter filter, int stbir__inline static int stbir__get_filter_pixel_margin_horizontal(stbir__info* stbir_info) { - return stbir__get_filter_pixel_width(stbir_info->filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale) / 2; + return stbir__get_filter_pixel_width(stbir_info->horizontal_filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale) / 2; } stbir__inline static int stbir__get_filter_pixel_margin_vertical(stbir__info* stbir_info) { - return stbir__get_filter_pixel_width(stbir_info->filter, stbir_info->input_h, stbir_info->output_h, stbir_info->vertical_scale) / 2; + return stbir__get_filter_pixel_width(stbir_info->vertical_filter, stbir_info->input_h, stbir_info->output_h, stbir_info->vertical_scale) / 2; } -stbir__inline static int stbir__get_horizontal_contributors_noinfo(stbir_filter filter, int input_w, int output_w, float horizontal_scale) +stbir__inline static int stbir__get_horizontal_contributors(stbir__info* info) { - if (stbir__use_upsampling(horizontal_scale)) - return output_w; + if (stbir__use_upsampling(info->horizontal_scale)) + return info->output_w; else - return (input_w + stbir__get_filter_pixel_margin(filter, input_w, output_w, horizontal_scale) * 2); + return (info->input_w + stbir__get_filter_pixel_margin(info->horizontal_filter, info->input_w, info->output_w, info->horizontal_scale) * 2); } -stbir__inline static int stbir__get_horizontal_contributors(stbir__info* stbir_info) +stbir__inline static int stbir__get_total_coefficients(stbir__info* info) { - return stbir__get_horizontal_contributors_noinfo(stbir_info->filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale); -} - -stbir__inline static int stbir__get_total_coefficients_noinfo(stbir_filter filter, int input_w, int output_w, float horizontal_scale) -{ - return stbir__get_horizontal_contributors_noinfo(filter, input_w, output_w, horizontal_scale) * stbir__get_filter_pixel_width(filter, input_w, output_w, horizontal_scale); -} - -stbir__inline static int stbir__get_total_coefficients(stbir__info* stbir_info) -{ - return stbir__get_total_coefficients_noinfo(stbir_info->filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale); + return stbir__get_horizontal_contributors(info) + * stbir__get_filter_pixel_width (info->horizontal_filter, info->input_w, info->output_w, info->horizontal_scale); } stbir__inline static stbir__contributors* stbir__get_contributor(stbir__info* stbir_info, int n) @@ -560,13 +627,16 @@ stbir__inline static stbir__contributors* stbir__get_contributor(stbir__info* st stbir__inline static float* stbir__get_coefficient(stbir__info* stbir_info, int n, int c) { - return &stbir_info->horizontal_coefficients[stbir__get_filter_pixel_width(stbir_info->filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale)*n + c]; + return &stbir_info->horizontal_coefficients[stbir__get_filter_pixel_width(stbir_info->horizontal_filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale)*n + c]; } static int stbir__edge_wrap_slow(stbir_edge edge, int n, int max) { switch (edge) { + case STBIR_EDGE_ZERO: + return 0; // we'll decode the wrong pixel here, and then overwrite with 0s later + case STBIR_EDGE_CLAMP: if (n < 0) return 0; @@ -574,7 +644,7 @@ static int stbir__edge_wrap_slow(stbir_edge edge, int n, int max) if (n >= max) return max - 1; - return n; + return n; // NOTREACHED case STBIR_EDGE_REFLECT: { @@ -595,7 +665,7 @@ static int stbir__edge_wrap_slow(stbir_edge edge, int n, int max) return max2 - n - 1; } - return n; + return n; // NOTREACHED } case STBIR_EDGE_WRAP: @@ -610,6 +680,7 @@ static int stbir__edge_wrap_slow(stbir_edge edge, int n, int max) return (m); } + return n; // NOTREACHED default: STBIR__UNIMPLEMENTED("Unimplemented edge type"); @@ -655,12 +726,11 @@ static void stbir__calculate_sample_range_downsample(int n, float in_pixels_radi *out_last_pixel = (int)(floor(out_pixel_influence_upperbound - 0.5)); } -static void stbir__calculate_coefficients_upsample(stbir__info* stbir_info, int in_first_pixel, int in_last_pixel, float in_center_of_out, stbir__contributors* contributor, float* coefficient_group) +static void stbir__calculate_coefficients_upsample(stbir__info* stbir_info, stbir_filter filter, int in_first_pixel, int in_last_pixel, float in_center_of_out, stbir__contributors* contributor, float* coefficient_group) { int i; float total_filter = 0; float filter_scale; - stbir_filter filter = stbir_info->filter; STBIR__DEBUG_ASSERT(in_last_pixel - in_first_pixel <= stbir__get_filter_pixel_width_horizontal(stbir_info)); @@ -685,10 +755,9 @@ static void stbir__calculate_coefficients_upsample(stbir__info* stbir_info, int coefficient_group[i] *= filter_scale; } -static void stbir__calculate_coefficients_downsample(stbir__info* stbir_info, float scale_ratio, int out_first_pixel, int out_last_pixel, float out_center_of_in, stbir__contributors* contributor, float* coefficient_group) +static void stbir__calculate_coefficients_downsample(stbir__info* stbir_info, stbir_filter filter, float scale_ratio, int out_first_pixel, int out_last_pixel, float out_center_of_in, stbir__contributors* contributor, float* coefficient_group) { int i; - stbir_filter filter = stbir_info->filter; STBIR__DEBUG_ASSERT(out_last_pixel - out_first_pixel <= stbir__get_filter_pixel_width_horizontal(stbir_info)); @@ -740,7 +809,7 @@ static void stbir__calculate_horizontal_filters(stbir__info* stbir_info) if (stbir__use_width_upsampling(stbir_info)) { - float out_pixels_radius = stbir__filter_info_table[stbir_info->filter].support * scale_ratio; + float out_pixels_radius = stbir__filter_info_table[stbir_info->horizontal_filter].support * scale_ratio; // Looping through out pixels for (n = 0; n < total_contributors; n++) @@ -750,12 +819,12 @@ static void stbir__calculate_horizontal_filters(stbir__info* stbir_info) stbir__calculate_sample_range_upsample(n, out_pixels_radius, scale_ratio, stbir_info->horizontal_shift, &in_first_pixel, &in_last_pixel, &in_center_of_out); - stbir__calculate_coefficients_upsample(stbir_info, in_first_pixel, in_last_pixel, in_center_of_out, stbir__get_contributor(stbir_info, n), stbir__get_coefficient(stbir_info, n, 0)); + stbir__calculate_coefficients_upsample(stbir_info, stbir_info->horizontal_filter, in_first_pixel, in_last_pixel, in_center_of_out, stbir__get_contributor(stbir_info, n), stbir__get_coefficient(stbir_info, n, 0)); } } else { - float in_pixels_radius = stbir__filter_info_table[stbir_info->filter].support / scale_ratio; + float in_pixels_radius = stbir__filter_info_table[stbir_info->horizontal_filter].support / scale_ratio; // Looping through in pixels for (n = 0; n < total_contributors; n++) @@ -766,7 +835,7 @@ static void stbir__calculate_horizontal_filters(stbir__info* stbir_info) stbir__calculate_sample_range_downsample(n_adjusted, in_pixels_radius, scale_ratio, stbir_info->horizontal_shift, &out_first_pixel, &out_last_pixel, &out_center_of_in); - stbir__calculate_coefficients_downsample(stbir_info, scale_ratio, out_first_pixel, out_last_pixel, out_center_of_in, stbir__get_contributor(stbir_info, n), stbir__get_coefficient(stbir_info, n, 0)); + stbir__calculate_coefficients_downsample(stbir_info, stbir_info->horizontal_filter, scale_ratio, out_first_pixel, out_last_pixel, out_center_of_in, stbir__get_contributor(stbir_info, n), stbir__get_coefficient(stbir_info, n, 0)); } #ifdef STBIR_DEBUG @@ -803,6 +872,16 @@ static void stbir__decode_scanline(stbir__info* stbir_info, int n) int x = -stbir__get_filter_pixel_margin_horizontal(stbir_info); + // special handling for STBIR_EDGE_ZERO because it needs to return an item that doesn't appear in the input, + // and we want to avoid paying overhead on every pixel if not STBIR_EDGE_ZERO + if (edge_vertical == STBIR_EDGE_ZERO && (n < 0 || n >= stbir_info->input_h)) + { + for (; x < max_x; x++) + for (c = 0; c < channels; c++) + decode_buffer[x*channels + c] = 0; + return; + } + switch (decode) { case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_LINEAR): @@ -823,7 +902,7 @@ static void stbir__decode_scanline(stbir__info* stbir_info, int n) for (c = 0; c < channels; c++) decode_buffer[decode_pixel_index + c] = stbir__srgb_uchar_to_linear_float[((const unsigned char*)input_data)[input_pixel_index + c]]; - if (!(stbir_info->flags&STBIR_FLAG_GAMMA_CORRECT_ALPHA)) + if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE)) decode_buffer[decode_pixel_index + alpha_channel] = ((float)((const unsigned char*)input_data)[input_pixel_index + alpha_channel]) / 255; } break; @@ -846,7 +925,7 @@ static void stbir__decode_scanline(stbir__info* stbir_info, int n) for (c = 0; c < channels; c++) decode_buffer[decode_pixel_index + c] = stbir__srgb_to_linear(((float)((const unsigned short*)input_data)[input_pixel_index + c]) / 65535); - if (!(stbir_info->flags&STBIR_FLAG_GAMMA_CORRECT_ALPHA)) + if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE)) decode_buffer[decode_pixel_index + alpha_channel] = ((float)((const unsigned short*)input_data)[input_pixel_index + alpha_channel]) / 65535; } break; @@ -869,7 +948,7 @@ static void stbir__decode_scanline(stbir__info* stbir_info, int n) for (c = 0; c < channels; c++) decode_buffer[decode_pixel_index + c] = stbir__srgb_to_linear((float)(((double)((const unsigned int*)input_data)[input_pixel_index + c]) / 4294967295)); - if (!(stbir_info->flags&STBIR_FLAG_GAMMA_CORRECT_ALPHA)) + if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE)) decode_buffer[decode_pixel_index + alpha_channel] = (float)(((double)((const unsigned int*)input_data)[input_pixel_index + alpha_channel]) / 4294967295); } break; @@ -892,7 +971,7 @@ static void stbir__decode_scanline(stbir__info* stbir_info, int n) for (c = 0; c < channels; c++) decode_buffer[decode_pixel_index + c] = stbir__srgb_to_linear(((const float*)input_data)[input_pixel_index + c]); - if (!(stbir_info->flags&STBIR_FLAG_GAMMA_CORRECT_ALPHA)) + if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE)) decode_buffer[decode_pixel_index + alpha_channel] = ((const float*)input_data)[input_pixel_index + alpha_channel]; } @@ -903,7 +982,7 @@ static void stbir__decode_scanline(stbir__info* stbir_info, int n) break; } - if (stbir_info->flags & STBIR_FLAG_NONPREMUL_ALPHA) + if (!(stbir_info->flags & STBIR_FLAG_PREMULTIPLIED_ALPHA)) { for (x = -stbir__get_filter_pixel_margin_horizontal(stbir_info); x < max_x; x++) { @@ -918,6 +997,20 @@ static void stbir__decode_scanline(stbir__info* stbir_info, int n) } } } + + if (edge_horizontal == STBIR_EDGE_ZERO) + { + for (x = -stbir__get_filter_pixel_margin_horizontal(stbir_info); x < 0; x++) + { + for (c = 0; c < channels; c++) + decode_buffer[x*channels + c] = 0; + } + for (x = input_w; x < max_x; x++) + { + for (c = 0; c < channels; c++) + decode_buffer[x*channels + c] = 0; + } + } } static float* stbir__get_ring_buffer_entry(float* ring_buffer, int index, int ring_buffer_length) @@ -1067,75 +1160,131 @@ static float* stbir__get_ring_buffer_scanline(int get_scanline, float* ring_buff } -static stbir__inline void stbir__encode_pixel(stbir__info* stbir_info, void* output_buffer, int output_pixel_index, float* encode_buffer, int encode_pixel_index, int channels, int alpha_channel, int decode) +// @OPTIMIZE: embed stbir__encode_pixel and move switch out of per-pixel loop +static void stbir__encode_scanline(stbir__info* stbir_info, int num_pixels, void *output_buffer, float *encode_buffer, int channels, int alpha_channel, int decode) { + int x; int n; - float divide_alpha = 1; - if (stbir_info->flags&STBIR_FLAG_NONPREMUL_ALPHA) { - float alpha = encode_buffer[encode_pixel_index + alpha_channel]; - float reciprocal_alpha = alpha ? 1.0f / alpha : 0; - for (n = 0; n < channels; n++) - if (n != alpha_channel) - encode_buffer[encode_pixel_index + n] *= reciprocal_alpha; +// stbir__encode_pixel(stbir_info, output_buffer, x*channels, encode_buffer, x*channels, channels, alpha_channel, decode); + + if (!(stbir_info->flags&STBIR_FLAG_PREMULTIPLIED_ALPHA)) + { + for (x=0; x < num_pixels; ++x) + { + int output_pixel_index = x*channels; + int encode_pixel_index = x*channels; + float alpha = encode_buffer[encode_pixel_index + alpha_channel]; + float reciprocal_alpha = alpha ? 1.0f / alpha : 0; + // @TODO: if final alpha=0, we actually want to have ignored alpha... set alpha to sRGB_to_linear(1/255)/(2^24) so floats will discard it? + for (n = 0; n < channels; n++) + if (n != alpha_channel) + encode_buffer[encode_pixel_index + n] *= reciprocal_alpha; + } } switch (decode) { case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_LINEAR): - for (n = 0; n < channels; n++) - ((unsigned char*)output_buffer)[output_pixel_index + n] = (unsigned char)(stbir__saturate(encode_buffer[encode_pixel_index + n]) * 255); + for (x=0; x < num_pixels; ++x) + { + int output_pixel_index = x*channels; + int encode_pixel_index = x*channels; + + for (n = 0; n < channels; n++) + ((unsigned char*)output_buffer)[output_pixel_index + n] = (unsigned char)(stbir__saturate(encode_buffer[encode_pixel_index + n]) * 255); + } break; case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_SRGB): - for (n = 0; n < channels; n++) - ((unsigned char*)output_buffer)[output_pixel_index + n] = stbir__linear_uchar_to_srgb_uchar[(unsigned char)(stbir__saturate(encode_buffer[encode_pixel_index + n]) * 255)]; + for (x=0; x < num_pixels; ++x) + { + int output_pixel_index = x*channels; + int encode_pixel_index = x*channels; - if (!(stbir_info->flags&STBIR_FLAG_GAMMA_CORRECT_ALPHA)) - ((unsigned char*)output_buffer)[output_pixel_index + alpha_channel] = (unsigned char)(stbir__saturate(encode_buffer[encode_pixel_index + alpha_channel]) * 255); + for (n = 0; n < channels; n++) + ((unsigned char*)output_buffer)[output_pixel_index + n] = stbir__linear_to_srgb_uchar(encode_buffer[encode_pixel_index + n]); + + if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE)) + ((unsigned char*)output_buffer)[output_pixel_index + alpha_channel] = (unsigned char)(stbir__saturate(encode_buffer[encode_pixel_index + alpha_channel]) * 255); + } break; case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR): - for (n = 0; n < channels; n++) - ((unsigned short*)output_buffer)[output_pixel_index + n] = (unsigned short)(stbir__saturate(encode_buffer[encode_pixel_index + n]) * 65535); + for (x=0; x < num_pixels; ++x) + { + int output_pixel_index = x*channels; + int encode_pixel_index = x*channels; + + for (n = 0; n < channels; n++) + ((unsigned short*)output_buffer)[output_pixel_index + n] = (unsigned short)(stbir__saturate(encode_buffer[encode_pixel_index + n]) * 65535); + } break; case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB): - for (n = 0; n < channels; n++) - ((unsigned short*)output_buffer)[output_pixel_index + n] = (unsigned short)(stbir__linear_to_srgb(stbir__saturate(encode_buffer[encode_pixel_index + n])) * 65535); + for (x=0; x < num_pixels; ++x) + { + int output_pixel_index = x*channels; + int encode_pixel_index = x*channels; - if (!(stbir_info->flags&STBIR_FLAG_GAMMA_CORRECT_ALPHA)) - ((unsigned short*)output_buffer)[output_pixel_index + alpha_channel] = (unsigned char)(stbir__saturate(encode_buffer[encode_pixel_index + alpha_channel]) * 255); + for (n = 0; n < channels; n++) + ((unsigned short*)output_buffer)[output_pixel_index + n] = (unsigned short)(stbir__linear_to_srgb(stbir__saturate(encode_buffer[encode_pixel_index + n])) * 65535); + + if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE)) + ((unsigned short*)output_buffer)[output_pixel_index + alpha_channel] = (unsigned char)(stbir__saturate(encode_buffer[encode_pixel_index + alpha_channel]) * 255); + } break; case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR): - for (n = 0; n < channels; n++) - ((unsigned int*)output_buffer)[output_pixel_index + n] = (unsigned int)(((double)stbir__saturate(encode_buffer[encode_pixel_index + n])) * 4294967295); + for (x=0; x < num_pixels; ++x) + { + int output_pixel_index = x*channels; + int encode_pixel_index = x*channels; + + for (n = 0; n < channels; n++) + ((unsigned int*)output_buffer)[output_pixel_index + n] = (unsigned int)(((double)stbir__saturate(encode_buffer[encode_pixel_index + n])) * 4294967295); + } break; case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB): - for (n = 0; n < channels; n++) - ((unsigned int*)output_buffer)[output_pixel_index + n] = (unsigned int)(((double)stbir__linear_to_srgb(stbir__saturate(encode_buffer[encode_pixel_index + n]))) * 4294967295); + for (x=0; x < num_pixels; ++x) + { + int output_pixel_index = x*channels; + int encode_pixel_index = x*channels; - if (!(stbir_info->flags&STBIR_FLAG_GAMMA_CORRECT_ALPHA)) - ((unsigned int*)output_buffer)[output_pixel_index + alpha_channel] = (unsigned int)(((double)stbir__saturate(encode_buffer[encode_pixel_index + alpha_channel])) * 4294967295); + for (n = 0; n < channels; n++) + ((unsigned int*)output_buffer)[output_pixel_index + n] = (unsigned int)(((double)stbir__linear_to_srgb(stbir__saturate(encode_buffer[encode_pixel_index + n]))) * 4294967295); + if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE)) + ((unsigned int*)output_buffer)[output_pixel_index + alpha_channel] = (unsigned int)(((double)stbir__saturate(encode_buffer[encode_pixel_index + alpha_channel])) * 4294967295); + } break; case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR): - for (n = 0; n < channels; n++) - ((float*)output_buffer)[output_pixel_index + n] = encode_buffer[encode_pixel_index + n]; + for (x=0; x < num_pixels; ++x) + { + int output_pixel_index = x*channels; + int encode_pixel_index = x*channels; + + for (n = 0; n < channels; n++) + ((float*)output_buffer)[output_pixel_index + n] = encode_buffer[encode_pixel_index + n]; + } break; case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB): - for (n = 0; n < channels; n++) - ((float*)output_buffer)[output_pixel_index + n] = stbir__linear_to_srgb(encode_buffer[encode_pixel_index + n]); + for (x=0; x < num_pixels; ++x) + { + int output_pixel_index = x*channels; + int encode_pixel_index = x*channels; - if (!(stbir_info->flags&STBIR_FLAG_GAMMA_CORRECT_ALPHA)) - ((float*)output_buffer)[output_pixel_index + alpha_channel] = encode_buffer[encode_pixel_index + alpha_channel]; + for (n = 0; n < channels; n++) + ((float*)output_buffer)[output_pixel_index + n] = stbir__linear_to_srgb(encode_buffer[encode_pixel_index + n]); + if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE)) + ((float*)output_buffer)[output_pixel_index + alpha_channel] = encode_buffer[encode_pixel_index + alpha_channel]; + } break; default: @@ -1144,16 +1293,6 @@ static stbir__inline void stbir__encode_pixel(stbir__info* stbir_info, void* out } } -// @OPTIMIZE: embed stbir__encode_pixel and move switch out of per-pixel loop -static void stbir__encode_scanline(stbir__info* stbir_info, int num_pixels, void *output_buffer, float *encode_buffer, int channels, int alpha_channel, int decode) -{ - int x; - for (x=0; x < num_pixels; ++x) - { - stbir__encode_pixel(stbir_info, output_buffer, x*channels, encode_buffer, x*channels, channels, alpha_channel, decode); - } -} - static void stbir__resample_vertical_upsample(stbir__info* stbir_info, int n, int in_first_scanline, int in_last_scanline, float in_center_of_out) { int x, k; @@ -1177,7 +1316,7 @@ static void stbir__resample_vertical_upsample(stbir__info* stbir_info, int n, in int n0,n1, output_row_start; - stbir__calculate_coefficients_upsample(stbir_info, in_first_scanline, in_last_scanline, in_center_of_out, vertical_contributors, vertical_coefficients); + stbir__calculate_coefficients_upsample(stbir_info, stbir_info->vertical_filter, in_first_scanline, in_last_scanline, in_center_of_out, vertical_contributors, vertical_coefficients); n0 = vertical_contributors->n0; n1 = vertical_contributors->n1; @@ -1230,7 +1369,7 @@ static void stbir__resample_vertical_downsample(stbir__info* stbir_info, int n, int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float); int n0,n1,max_n; - stbir__calculate_coefficients_downsample(stbir_info, stbir_info->vertical_scale, in_first_scanline, in_last_scanline, in_center_of_out, vertical_contributors, vertical_coefficients); + stbir__calculate_coefficients_downsample(stbir_info, stbir_info->vertical_filter, stbir_info->vertical_scale, in_first_scanline, in_last_scanline, in_center_of_out, vertical_contributors, vertical_coefficients); n0 = vertical_contributors->n0; n1 = vertical_contributors->n1; @@ -1264,7 +1403,7 @@ static void stbir__buffer_loop_upsample(stbir__info* stbir_info) { int y; float scale_ratio = stbir_info->vertical_scale; - float out_scanlines_radius = stbir__filter_info_table[stbir_info->filter].support * scale_ratio; + float out_scanlines_radius = stbir__filter_info_table[stbir_info->vertical_filter].support * scale_ratio; STBIR__DEBUG_ASSERT(stbir__use_height_upsampling(stbir_info)); @@ -1360,7 +1499,7 @@ static void stbir__buffer_loop_downsample(stbir__info* stbir_info) int y; float scale_ratio = stbir_info->vertical_scale; int output_h = stbir_info->output_h; - float in_pixels_radius = stbir__filter_info_table[stbir_info->filter].support / scale_ratio; + float in_pixels_radius = stbir__filter_info_table[stbir_info->vertical_filter].support / scale_ratio; int max_y = stbir_info->input_h + stbir__get_filter_pixel_margin_vertical(stbir_info); STBIR__DEBUG_ASSERT(!stbir__use_height_upsampling(stbir_info)); @@ -1395,27 +1534,68 @@ static void stbir__buffer_loop_downsample(stbir__info* stbir_info) stbir__empty_ring_buffer(stbir_info, stbir_info->output_h); } -static stbir__inline stbir_size_t stbir__calculate_memory(int input_w, int input_h, int output_w, int output_h, float s0, float t0, float s1, float t1, int channels, stbir_filter filter) +static void stbir__setup(stbir__info *info, int input_w, int input_h, int output_w, int output_h, int channels) { - float horizontal_scale = ((float)output_w / input_w) / (s1 - s0); - float vertical_scale = ((float)output_h / input_h) / (t1 - t0); + info->input_w = input_w; + info->input_h = input_h; + info->output_w = output_w; + info->output_h = output_h; + info->channels = channels; +} - int pixel_margin = stbir__get_filter_pixel_margin(filter, input_w, output_w, horizontal_scale); - int filter_height = stbir__get_filter_pixel_width(filter, input_h, output_h, vertical_scale); +static void stbir__calculate_transform(stbir__info *info, float s0, float t0, float s1, float t1, float *transform) +{ + info->s0 = s0; + info->t0 = t0; + info->s1 = s1; + info->t1 = t1; - int info_size = sizeof(stbir__info); - int contributors_size = stbir__get_horizontal_contributors_noinfo(filter, input_w, output_w, horizontal_scale) * sizeof(stbir__contributors); - int horizontal_coefficients_size = stbir__get_total_coefficients_noinfo(filter, input_w, output_w, horizontal_scale) * sizeof(float); + if (transform) + { + info->horizontal_scale = transform[0]; + info->vertical_scale = transform[1]; + info->horizontal_shift = transform[2]; + info->vertical_shift = transform[3]; + } + else + { + info->horizontal_scale = ((float)info->output_w / info->input_w) / (s1 - s0); + info->vertical_scale = ((float)info->output_h / info->input_h) / (t1 - t0); + + info->horizontal_shift = s0 * info->input_w / (s1 - s0); + info->vertical_shift = t0 * info->input_h / (t1 - t0); + } +} + +static void stbir__choose_filter(stbir__info *info, stbir_filter h_filter, stbir_filter v_filter) +{ + if (h_filter == 0) + h_filter = stbir__use_upsampling(info->horizontal_scale) ? STBIR_DEFAULT_FILTER_UPSAMPLE : STBIR_DEFAULT_FILTER_DOWNSAMPLE; + if (v_filter == 0) + v_filter = stbir__use_upsampling(info->vertical_scale) ? STBIR_DEFAULT_FILTER_UPSAMPLE : STBIR_DEFAULT_FILTER_DOWNSAMPLE; + info->horizontal_filter = h_filter; + info->vertical_filter = v_filter; +} + +static stbir_uint32 stbir__calculate_memory(stbir__info *info) +{ + int pixel_margin = stbir__get_filter_pixel_margin(info->horizontal_filter, info->input_w, info->output_w, info->horizontal_scale); + int filter_height = stbir__get_filter_pixel_width(info->vertical_filter, info->input_h, info->output_h, info->vertical_scale); + + int contributors_size = stbir__get_horizontal_contributors(info) * sizeof(stbir__contributors); + int horizontal_coefficients_size = stbir__get_total_coefficients(info) * sizeof(float); int vertical_coefficients_size = filter_height * sizeof(float); - int decode_buffer_size = (input_w + pixel_margin*2) * channels * sizeof(float); - int horizontal_buffer_size = output_w * channels * sizeof(float); - int ring_buffer_size = output_w * channels * filter_height * sizeof(float); - int encode_buffer_size = output_w * channels * sizeof(float); + int decode_buffer_size = (info->input_w + pixel_margin*2) * info->channels * sizeof(float); + int horizontal_buffer_size = info->output_w * info->channels * sizeof(float); + int ring_buffer_size = info->output_w * info->channels * filter_height * sizeof(float); + int encode_buffer_size = info->output_w * info->channels * sizeof(float); - STBIR_ASSERT(filter != 0); - STBIR_ASSERT(filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late + STBIR_ASSERT(info->horizontal_filter != 0); + STBIR_ASSERT(info->horizontal_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late + STBIR_ASSERT(info->vertical_filter != 0); + STBIR_ASSERT(info->vertical_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late - if (stbir__use_upsampling(horizontal_scale)) + if (stbir__use_upsampling(info->horizontal_scale)) // The horizontal buffer is for when we're downsampling the height and we // can't output the result of sampling the decode buffer directly into the // ring buffers. @@ -1425,21 +1605,20 @@ static stbir__inline stbir_size_t stbir__calculate_memory(int input_w, int input // and isn't used when height downsampling. encode_buffer_size = 0; - return info_size + contributors_size + horizontal_coefficients_size + vertical_coefficients_size + decode_buffer_size + horizontal_buffer_size + ring_buffer_size + encode_buffer_size; + return contributors_size + horizontal_coefficients_size + vertical_coefficients_size + decode_buffer_size + horizontal_buffer_size + ring_buffer_size + encode_buffer_size; } -static int stbir__resize_allocated(const void* input_data, int input_w, int input_h, int input_stride_in_bytes, - void* output_data, int output_w, int output_h, int output_stride_in_bytes, - float s0, float t0, float s1, float t1, - int channels, int alpha_channel, stbir_uint32 flags, stbir_type type, stbir_filter filter, stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace, - void* tempmem, stbir_size_t tempmem_size_in_bytes) +static int stbir__resize_allocated(stbir__info *info, + const void* input_data, int input_stride_in_bytes, + void* output_data, int output_stride_in_bytes, + int alpha_channel, stbir_uint32 flags, stbir_datatype type, + stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace, + void* tempmem, size_t tempmem_size_in_bytes) { - stbir__info* stbir_info = (stbir__info*)tempmem; + size_t memory_required = stbir__calculate_memory(info); - stbir_size_t memory_required = stbir__calculate_memory(input_w, input_h, output_w, output_h, s0, t0, s1, t1, channels, filter); - - int width_stride_input = input_stride_in_bytes ? input_stride_in_bytes : channels * input_w * stbir__type_size[type]; - int width_stride_output = output_stride_in_bytes ? output_stride_in_bytes : channels * output_w * stbir__type_size[type]; + int width_stride_input = input_stride_in_bytes ? input_stride_in_bytes : info->channels * info->input_w * stbir__type_size[type]; + int width_stride_output = output_stride_in_bytes ? output_stride_in_bytes : info->channels * info->output_w * stbir__type_size[type]; #ifdef STBIR_DEBUG_OVERWRITE_TEST #define OVERWRITE_ARRAY_SIZE 8 @@ -1455,30 +1634,27 @@ static int stbir__resize_allocated(const void* input_data, int input_w, int inpu memcpy(overwrite_tempmem_after_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE); #endif - STBIR_ASSERT(filter != 0); - STBIR_ASSERT(filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); + STBIR_ASSERT(info->channels <= STBIR_MAX_CHANNELS); + STBIR_ASSERT(info->channels >= 0); - if (!filter || filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table)) + if (info->channels > STBIR_MAX_CHANNELS || info->channels < 0) return 0; - STBIR_ASSERT(s1 > s0); - STBIR_ASSERT(t1 > t0); + STBIR_ASSERT(info->horizontal_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); + STBIR_ASSERT(info->vertical_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); - if (s1 <= s0 || t1 <= t0) + if (info->horizontal_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table)) return 0; - - STBIR_ASSERT(s1 <= 1 && s0 >= 0 && t1 <= 1 && t0 >= 0); - - if (s1 > 1 || s0 < 0 || t1 > 1 || t0 < 0) + if (info->vertical_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table)) return 0; if (alpha_channel < 0) - flags = STBIR_FLAG_GAMMA_CORRECT_ALPHA; // this shouldn't be necessary in the long run, but safety for now + flags = STBIR_FLAG_ALPHA_USES_COLORSPACE | STBIR_FLAG_PREMULTIPLIED_ALPHA; - if (!(flags&STBIR_FLAG_GAMMA_CORRECT_ALPHA) || (flags&STBIR_FLAG_NONPREMUL_ALPHA)) - STBIR_ASSERT(alpha_channel >= 0 && alpha_channel < channels); + if (!(flags&STBIR_FLAG_ALPHA_USES_COLORSPACE) || !(flags&STBIR_FLAG_PREMULTIPLIED_ALPHA)) + STBIR_ASSERT(alpha_channel >= 0 && alpha_channel < info->channels); - if (alpha_channel >= channels) + if (alpha_channel >= info->channels) return 0; STBIR_ASSERT(tempmem); @@ -1493,74 +1669,57 @@ static int stbir__resize_allocated(const void* input_data, int input_w, int inpu memset(tempmem, 0, tempmem_size_in_bytes); - stbir_info->input_data = input_data; - stbir_info->input_w = input_w; - stbir_info->input_h = input_h; - stbir_info->input_stride_bytes = width_stride_input; + info->input_data = input_data; + info->input_stride_bytes = width_stride_input; - stbir_info->output_data = output_data; - stbir_info->output_w = output_w; - stbir_info->output_h = output_h; - stbir_info->output_stride_bytes = width_stride_output; + info->output_data = output_data; + info->output_stride_bytes = width_stride_output; - stbir_info->s0 = s0; - stbir_info->t0 = t0; - stbir_info->s1 = s1; - stbir_info->t1 = t1; + info->alpha_channel = alpha_channel; + info->flags = flags; + info->type = type; + info->edge_horizontal = edge_horizontal; + info->edge_vertical = edge_vertical; + info->colorspace = colorspace; - stbir_info->horizontal_scale = ((float)output_w / input_w) / (s1 - s0); - stbir_info->vertical_scale = ((float)output_h / input_h) / (t1 - t0); - - stbir_info->horizontal_shift = s0 * input_w / (s1 - s0); - stbir_info->vertical_shift = t0 * input_h / (t1 - t0); - - stbir_info->channels = channels; - stbir_info->alpha_channel = alpha_channel; - stbir_info->flags = flags; - stbir_info->type = type; - stbir_info->filter = filter; - stbir_info->edge_horizontal = edge_horizontal; - stbir_info->edge_vertical = edge_vertical; - stbir_info->colorspace = colorspace; - - stbir_info->ring_buffer_length_bytes = output_w * channels * sizeof(float); - stbir_info->decode_buffer_pixels = input_w + stbir__get_filter_pixel_margin_horizontal(stbir_info) * 2; + info->ring_buffer_length_bytes = info->output_w * info->channels * sizeof(float); + info->decode_buffer_pixels = info->input_w + stbir__get_filter_pixel_margin_horizontal(info) * 2; #define STBIR__NEXT_MEMPTR(current, old, newtype) (newtype*)(((unsigned char*)current) + old) - stbir_info->horizontal_contributors = STBIR__NEXT_MEMPTR(stbir_info, sizeof(stbir__info), stbir__contributors); - stbir_info->horizontal_coefficients = STBIR__NEXT_MEMPTR(stbir_info->horizontal_contributors, stbir__get_horizontal_contributors(stbir_info) * sizeof(stbir__contributors), float); - stbir_info->vertical_coefficients = STBIR__NEXT_MEMPTR(stbir_info->horizontal_coefficients, stbir__get_total_coefficients(stbir_info) * sizeof(float), float); - stbir_info->decode_buffer = STBIR__NEXT_MEMPTR(stbir_info->vertical_coefficients, stbir__get_filter_pixel_width_vertical(stbir_info) * sizeof(float), float); + info->horizontal_contributors = (stbir__contributors *) tempmem; + info->horizontal_coefficients = STBIR__NEXT_MEMPTR(info->horizontal_contributors, stbir__get_horizontal_contributors(info) * sizeof(stbir__contributors), float); + info->vertical_coefficients = STBIR__NEXT_MEMPTR(info->horizontal_coefficients, stbir__get_total_coefficients(info) * sizeof(float), float); + info->decode_buffer = STBIR__NEXT_MEMPTR(info->vertical_coefficients, stbir__get_filter_pixel_width_vertical(info) * sizeof(float), float); - if (stbir__use_height_upsampling(stbir_info)) + if (stbir__use_height_upsampling(info)) { - stbir_info->horizontal_buffer = NULL; - stbir_info->ring_buffer = STBIR__NEXT_MEMPTR(stbir_info->decode_buffer, stbir_info->decode_buffer_pixels * channels * sizeof(float), float); - stbir_info->encode_buffer = STBIR__NEXT_MEMPTR(stbir_info->ring_buffer, stbir_info->ring_buffer_length_bytes * stbir__get_filter_pixel_width_horizontal(stbir_info), float); + info->horizontal_buffer = NULL; + info->ring_buffer = STBIR__NEXT_MEMPTR(info->decode_buffer, info->decode_buffer_pixels * info->channels * sizeof(float), float); + info->encode_buffer = STBIR__NEXT_MEMPTR(info->ring_buffer, info->ring_buffer_length_bytes * stbir__get_filter_pixel_width_horizontal(info), float); - STBIR__DEBUG_ASSERT((size_t)STBIR__NEXT_MEMPTR(stbir_info->encode_buffer, stbir_info->channels * sizeof(float), unsigned char) == (size_t)tempmem + tempmem_size_in_bytes); + STBIR__DEBUG_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->encode_buffer, info->channels * sizeof(float), unsigned char) == (size_t)tempmem + tempmem_size_in_bytes); } else { - stbir_info->horizontal_buffer = STBIR__NEXT_MEMPTR(stbir_info->decode_buffer, stbir_info->decode_buffer_pixels * channels * sizeof(float), float); - stbir_info->ring_buffer = STBIR__NEXT_MEMPTR(stbir_info->horizontal_buffer, output_w * channels * sizeof(float), float); - stbir_info->encode_buffer = NULL; + info->horizontal_buffer = STBIR__NEXT_MEMPTR(info->decode_buffer, info->decode_buffer_pixels * info->channels * sizeof(float), float); + info->ring_buffer = STBIR__NEXT_MEMPTR(info->horizontal_buffer, info->output_w * info->channels * sizeof(float), float); + info->encode_buffer = NULL; - STBIR__DEBUG_ASSERT((size_t)STBIR__NEXT_MEMPTR(stbir_info->ring_buffer, stbir_info->ring_buffer_length_bytes * stbir__get_filter_pixel_width_vertical(stbir_info), unsigned char) == (size_t)tempmem + tempmem_size_in_bytes); + STBIR__DEBUG_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->ring_buffer, info->ring_buffer_length_bytes * stbir__get_filter_pixel_width_vertical(info), unsigned char) == (size_t)tempmem + tempmem_size_in_bytes); } #undef STBIR__NEXT_MEMPTR // This signals that the ring buffer is empty - stbir_info->ring_buffer_begin_index = -1; + info->ring_buffer_begin_index = -1; - stbir__calculate_horizontal_filters(stbir_info); + stbir__calculate_horizontal_filters(info); - if (stbir__use_height_upsampling(stbir_info)) - stbir__buffer_loop_upsample(stbir_info); + if (stbir__use_height_upsampling(info)) + stbir__buffer_loop_upsample(info); else - stbir__buffer_loop_downsample(stbir_info); + stbir__buffer_loop_downsample(info); #ifdef STBIR_DEBUG_OVERWRITE_TEST STBIR__DEBUG_ASSERT(memcmp(overwrite_output_before_pre, &((unsigned char*)output_data)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE) == 0); @@ -1573,117 +1732,168 @@ static int stbir__resize_allocated(const void* input_data, int input_w, int inpu } -STBRDEF int stbir_resize_arbitrary(const void* input_data, int input_w, int input_h, int input_stride_in_bytes, +static int stbir_resize_arbitrary( + void *alloc_context, + const void* input_data, int input_w, int input_h, int input_stride_in_bytes, void* output_data, int output_w, int output_h, int output_stride_in_bytes, - float s0, float t0, float s1, float t1, - int channels, int alpha_channel, stbir_uint32 flags, stbir_type type, stbir_filter filter, stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace) + float s0, float t0, float s1, float t1, float *transform, + int channels, int alpha_channel, stbir_uint32 flags, stbir_datatype type, + stbir_filter h_filter, stbir_filter v_filter, + stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace) { + stbir__info info; int result; - size_t memory_required = stbir__calculate_memory(input_w, input_h, output_w, output_h, 0, 0, 1, 1, channels, filter); - void* extra_memory = STBIR_MALLOC(memory_required); + size_t memory_required; + void* extra_memory; + + stbir__setup(&info, input_w, input_h, output_w, output_h, channels); + stbir__calculate_transform(&info, s0,t0,s1,t1,transform); + stbir__choose_filter(&info, h_filter, v_filter); + memory_required = stbir__calculate_memory(&info); + extra_memory = STBIR_MALLOC(NULL, memory_required); if (!extra_memory) return 0; - result = stbir__resize_allocated(input_data, input_w, input_h, input_stride_in_bytes, output_data, output_w, output_h, output_stride_in_bytes, s0, t0, s1, t1, channels, alpha_channel, flags, type, filter, edge_horizontal, edge_vertical, colorspace, extra_memory, memory_required); + result = stbir__resize_allocated(&info, input_data, input_stride_in_bytes, + output_data, output_stride_in_bytes, + alpha_channel, flags, type, + edge_horizontal, edge_vertical, + colorspace, extra_memory, memory_required); - STBIR_FREE(extra_memory); + STBIR_FREE(context, extra_memory); return result; } -STBRDEF int stbir_resize_uint8_srgb(const stbir_uint8* input_data, int input_w, int input_h, - stbir_uint8* output_data, int output_w, int output_h, - int channels, stbir_filter filter, stbir_edge edge) +STBIRDEF int stbir_resize_uint8( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + int num_channels) { - return stbir_resize_arbitrary(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, 0, 0, STBIR_TYPE_UINT8, filter, edge, edge, STBIR_COLORSPACE_SRGB); + return stbir_resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes, + output_pixels, output_w, output_h, output_stride_in_bytes, + 0,0,1,1,NULL,num_channels,-1,0, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT, + STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR); } -STBRDEF int stbir_resize_uint16_srgb(const stbir_uint16* input_data, int input_w, int input_h, - stbir_uint16* output_data, int output_w, int output_h, - int channels, stbir_filter filter, stbir_edge edge) +STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + float *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + int num_channels) { - return stbir_resize_arbitrary(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, 0, 0, STBIR_TYPE_UINT16, filter, edge, edge, STBIR_COLORSPACE_SRGB); + return stbir_resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes, + output_pixels, output_w, output_h, output_stride_in_bytes, + 0,0,1,1,NULL,num_channels,-1,0, STBIR_TYPE_FLOAT, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT, + STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR); } -STBRDEF int stbir_resize_uint32_srgb(const stbir_uint32* input_data, int input_w, int input_h, - stbir_uint32* output_data, int output_w, int output_h, - int channels, stbir_filter filter, stbir_edge edge) +STBIRDEF int stbir_resize_uint8_srgb(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + int num_channels, int alpha_channel, int flags) { - return stbir_resize_arbitrary(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, 0, 0, STBIR_TYPE_UINT32, filter, edge, edge, STBIR_COLORSPACE_SRGB); + return stbir_resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes, + output_pixels, output_w, output_h, output_stride_in_bytes, + 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT, + STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); } -STBRDEF int stbir_resize_float_srgb(const float* input_data, int input_w, int input_h, - float* output_data, int output_w, int output_h, - int channels, stbir_filter filter, stbir_edge edge) +STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + int num_channels, int alpha_channel, int flags, + stbir_edge edge_wrap_mode) { - return stbir_resize_arbitrary(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, 0, 0, STBIR_TYPE_FLOAT, filter, edge, edge, STBIR_COLORSPACE_SRGB); + return stbir_resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes, + output_pixels, output_w, output_h, output_stride_in_bytes, + 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT, + edge_wrap_mode, edge_wrap_mode, STBIR_COLORSPACE_SRGB); } -STBRDEF int stbir_resize_uint8_alphaweighted(const stbir_uint8* input_data, int input_w, int input_h, - stbir_uint8* output_data, int output_w, int output_h, - int channels, int alpha_channel, stbir_filter filter, stbir_edge edge, stbir_colorspace colorspace) +STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + int num_channels, int alpha_channel, int flags, + stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, + void *alloc_context) { - return stbir_resize_arbitrary(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, alpha_channel, STBIR_FLAG_NONPREMUL_ALPHA, STBIR_TYPE_UINT8, filter, edge, edge, colorspace); + return stbir_resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, + output_pixels, output_w, output_h, output_stride_in_bytes, + 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, filter, filter, + edge_wrap_mode, edge_wrap_mode, space); } -STBRDEF int stbir_resize_uint16_alphaweighted(const stbir_uint16* input_data, int input_w, int input_h, - stbir_uint16* output_data, int output_w, int output_h, - int channels, int alpha_channel, stbir_filter filter, stbir_edge edge, stbir_colorspace colorspace) +STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + stbir_uint16 *output_pixels , int output_w, int output_h, int output_stride_in_bytes, + int num_channels, int alpha_channel, int flags, + stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, + void *alloc_context) { - return stbir_resize_arbitrary(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, alpha_channel, STBIR_FLAG_NONPREMUL_ALPHA, STBIR_TYPE_UINT16, filter, edge, edge, colorspace); + return stbir_resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, + output_pixels, output_w, output_h, output_stride_in_bytes, + 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT16, filter, filter, + edge_wrap_mode, edge_wrap_mode, space); } -STBRDEF int stbir_resize_uint32_alphaweighted(const stbir_uint32* input_data, int input_w, int input_h, - stbir_uint32* output_data, int output_w, int output_h, - int channels, int alpha_channel, stbir_filter filter, stbir_edge edge, stbir_colorspace colorspace) + +STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + float *output_pixels , int output_w, int output_h, int output_stride_in_bytes, + int num_channels, int alpha_channel, int flags, + stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, + void *alloc_context) { - return stbir_resize_arbitrary(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, alpha_channel, STBIR_FLAG_NONPREMUL_ALPHA, STBIR_TYPE_UINT32, filter, edge, edge, colorspace); + return stbir_resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, + output_pixels, output_w, output_h, output_stride_in_bytes, + 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_FLOAT, filter, filter, + edge_wrap_mode, edge_wrap_mode, space); } -STBRDEF int stbir_resize_float_alphaweighted(const float* input_data, int input_w, int input_h, - float* output_data, int output_w, int output_h, - int channels, int alpha_channel, stbir_filter filter, stbir_edge edge, stbir_colorspace colorspace) + +STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + stbir_datatype datatype, + int num_channels, int alpha_channel, int flags, + stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, + stbir_filter filter_horizontal, stbir_filter filter_vertical, + stbir_colorspace space, void *alloc_context) { - return stbir_resize_arbitrary(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, alpha_channel, STBIR_FLAG_NONPREMUL_ALPHA, STBIR_TYPE_FLOAT, filter, edge, edge, colorspace); + return stbir_resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, + output_pixels, output_w, output_h, output_stride_in_bytes, + 0,0,1,1,NULL,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical, + edge_mode_horizontal, edge_mode_vertical, space); } -STBRDEF int stbir_resize_uint8_subpixel(const stbir_uint8* input_data, int input_w, int input_h, - stbir_uint8* output_data, int output_w, int output_h, - float s0, float t0, float s1, float t1, - int channels, stbir_filter filter, stbir_edge edge) + +STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + stbir_datatype datatype, + int num_channels, int alpha_channel, int flags, + stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, + stbir_filter filter_horizontal, stbir_filter filter_vertical, + stbir_colorspace space, void *alloc_context, + float x_scale, float y_scale, + float x_offset, float y_offset) { - return stbir_resize_arbitrary(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, s0, t0, s1, t1, channels, 0, 0, STBIR_TYPE_UINT8, filter, edge, edge, STBIR_COLORSPACE_SRGB); + float transform[4]; + transform[0] = x_scale; + transform[1] = y_scale; + transform[2] = x_offset; + transform[3] = y_offset; + return stbir_resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, + output_pixels, output_w, output_h, output_stride_in_bytes, + 0,0,1,1,transform,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical, + edge_mode_horizontal, edge_mode_vertical, space); } -STBRDEF int stbir_resize_uint16_subpixel(const stbir_uint16* input_data, int input_w, int input_h, - stbir_uint16* output_data, int output_w, int output_h, - float s0, float t0, float s1, float t1, - int channels, stbir_filter filter, stbir_edge edge) +STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes, + void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, + stbir_datatype datatype, + int num_channels, int alpha_channel, int flags, + stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, + stbir_filter filter_horizontal, stbir_filter filter_vertical, + stbir_colorspace space, void *alloc_context, + float s0, float t0, float s1, float t1) { - return stbir_resize_arbitrary(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, s0, t0, s1, t1, channels, 0, 0, STBIR_TYPE_UINT16, filter, edge, edge, STBIR_COLORSPACE_SRGB); -} - -STBRDEF int stbir_resize_uint32_subpixel(const stbir_uint32* input_data, int input_w, int input_h, - stbir_uint32* output_data, int output_w, int output_h, - float s0, float t0, float s1, float t1, - int channels, stbir_filter filter, stbir_edge edge) -{ - return stbir_resize_arbitrary(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, s0, t0, s1, t1, channels, 0, 0, STBIR_TYPE_UINT32, filter, edge, edge, STBIR_COLORSPACE_SRGB); -} - -STBRDEF int stbir_resize_float_subpixel(const float* input_data, int input_w, int input_h, - float* output_data, int output_w, int output_h, - float s0, float t0, float s1, float t1, - int channels, stbir_filter filter, stbir_edge edge) -{ - return stbir_resize_arbitrary(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, s0, t0, s1, t1, channels, 0, 0, STBIR_TYPE_FLOAT, filter, edge, edge, STBIR_COLORSPACE_SRGB); + return stbir_resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, + output_pixels, output_w, output_h, output_stride_in_bytes, + s0,t0,s1,t1,NULL,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical, + edge_mode_horizontal, edge_mode_vertical, space); } #endif // STB_IMAGE_RESIZE_IMPLEMENTATION - -/* -revision history: - 0.50 (2014-07-29) - first released version -*/ diff --git a/tests/resample_test.cpp b/tests/resample_test.cpp index 856351b..e3a1d6d 100644 --- a/tests/resample_test.cpp +++ b/tests/resample_test.cpp @@ -129,7 +129,7 @@ int main(int argc, char** argv) { ftime(&initial_time_millis); for (int i = 0; i < 100; i++) - stbir_resize_arbitrary(input_data + w * border * n + border * n, in_w, in_h, w*n, output_data, out_w, out_h, out_stride, 0, 0, 1, 1, n, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); + stbir_resize_arbitrary(NULL, input_data + w * border * n + border * n, in_w, in_h, w*n, output_data, out_w, out_h, out_stride, 0, 0, 1, 1, NULL, n, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); ftime(&final_time_millis); long lapsed_ms = (long)(final_time_millis.time - initial_time_millis.time) * 1000 + (final_time_millis.millitm - initial_time_millis.millitm); printf("Resample: %dms\n", lapsed_ms); @@ -141,7 +141,7 @@ int main(int argc, char** argv) printf("Average: %dms\n", average); #else - stbir_resize_arbitrary(input_data + w * border * n + border * n, in_w, in_h, w*n, output_data, out_w, out_h, out_stride, s0, t0, s1, t1, n, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); + stbir_resize_arbitrary(NULL, input_data + w * border * n + border * n, in_w, in_h, w*n, output_data, out_w, out_h, out_stride, s0, t0, s1, t1, NULL, n, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); #endif stbi_image_free(input_data); @@ -160,7 +160,7 @@ void resize_image(const char* filename, float width_percent, float height_percen unsigned char* input_data = stbi_load(filename, &w, &h, &n, 0); if (!input_data) { - printf("Input image could not be loaded"); + printf("Input image could not be loaded\n"); return; } @@ -169,7 +169,7 @@ void resize_image(const char* filename, float width_percent, float height_percen unsigned char* output_data = (unsigned char*)malloc(out_w * out_h * n); - stbir_resize_arbitrary(input_data, w, h, 0, output_data, out_w, out_h, 0, 0, 0, 1, 1, n, -1, 0, STBIR_TYPE_UINT8, filter, edge, edge, colorspace); + stbir_resize_arbitrary(NULL, input_data, w, h, 0, output_data, out_w, out_h, 0, 0, 0, 1, 1, NULL, n, -1, 0, STBIR_TYPE_UINT8, filter, filter, edge, edge, colorspace); stbi_image_free(input_data); @@ -187,11 +187,15 @@ void convert_image(const F* input, T* output, int length) } template -void test_format(const char* file, float width_percent, float height_percent, stbir_type type, stbir_colorspace colorspace) +void test_format(const char* file, float width_percent, float height_percent, stbir_datatype type, stbir_colorspace colorspace) { int w, h, n; unsigned char* input_data = stbi_load(file, &w, &h, &n, 0); + if (input_data == NULL) + return; + + int new_w = (int)(w * width_percent); int new_h = (int)(h * height_percent); @@ -200,7 +204,7 @@ void test_format(const char* file, float width_percent, float height_percent, st T* output_data = (T*)malloc(new_w * new_h * n * sizeof(T)); - stbir_resize_arbitrary(T_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, n, -1, 0, type, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, colorspace); + stbir_resize_arbitrary(NULL, T_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, NULL, n, -1, 0, type, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, colorspace); free(T_data); stbi_image_free(input_data); @@ -228,11 +232,14 @@ void convert_image_float(const float* input, unsigned char* output, int length) output[i] = (unsigned char)(input[i] * 255); } -void test_float(const char* file, float width_percent, float height_percent, stbir_type type, stbir_colorspace colorspace) +void test_float(const char* file, float width_percent, float height_percent, stbir_datatype type, stbir_colorspace colorspace) { int w, h, n; unsigned char* input_data = stbi_load(file, &w, &h, &n, 0); + if (input_data == NULL) + return; + int new_w = (int)(w * width_percent); int new_h = (int)(h * height_percent); @@ -241,7 +248,7 @@ void test_float(const char* file, float width_percent, float height_percent, stb float* output_data = (float*)malloc(new_w * new_h * n * sizeof(float)); - stbir_resize_arbitrary(T_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, n, -1, 0, type, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, colorspace); + stbir_resize_arbitrary(NULL, T_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, NULL, n, -1, 0, type, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, colorspace); free(T_data); stbi_image_free(input_data); @@ -262,6 +269,9 @@ void test_channels(const char* file, float width_percent, float height_percent, int w, h, n; unsigned char* input_data = stbi_load(file, &w, &h, &n, 0); + if (input_data == NULL) + return; + int new_w = (int)(w * width_percent); int new_h = (int)(h * height_percent); @@ -278,7 +288,7 @@ void test_channels(const char* file, float width_percent, float height_percent, unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * channels * sizeof(unsigned char)); - stbir_resize_uint8_srgb(channels_data, w, h, output_data, new_w, new_h, channels, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP); + stbir_resize_uint8_srgb(channels_data, w, h, 0, output_data, new_w, new_h, 0, channels, -1,0); free(channels_data); stbi_image_free(input_data); @@ -295,6 +305,9 @@ void test_subpixel(const char* file, float width_percent, float height_percent, int w, h, n; unsigned char* input_data = stbi_load(file, &w, &h, &n, 0); + if (input_data == NULL) + return; + s1 = ((float)w - 1 + s1)/w; t1 = ((float)h - 1 + t1)/h; @@ -303,7 +316,7 @@ void test_subpixel(const char* file, float width_percent, float height_percent, unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char)); - stbir_resize_arbitrary(input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, s1, t1, n, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); + stbir_resize_arbitrary(NULL, input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, s1, t1, NULL, n, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); stbi_image_free(input_data); @@ -320,6 +333,10 @@ void test_premul(const char* file) unsigned char* input_data = stbi_load(file, &w, &h, &n, 4); n = 4; + if (input_data == NULL) + return; + + // Set alpha for the top half. for (int x = 0; x < w; x++) { @@ -334,13 +351,13 @@ void test_premul(const char* file) unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char)); - stbir_resize_arbitrary(input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, n, 3, STBIR_FLAG_NONPREMUL_ALPHA, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); + stbir_resize_arbitrary(NULL, input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, NULL, n, 3, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); char output[200]; sprintf(output, "test-output/premul-%s", file); stbi_write_png(output, new_w, new_h, n, output_data, 0); - stbir_resize_arbitrary(input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, n, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); + stbir_resize_arbitrary(NULL, input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, NULL, n, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); sprintf(output, "test-output/nopremul-%s", file); stbi_write_png(output, new_w, new_h, n, output_data, 0); @@ -362,13 +379,13 @@ void test_subpixel_1() unsigned char output_data[16 * 16]; - stbir_resize_arbitrary(image, 8, 8, 0, output_data, 16, 16, 0, 0, 0, 1, 1, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); + stbir_resize_arbitrary(NULL, image, 8, 8, 0, output_data, 16, 16, 0, 0, 0, 1, 1, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); unsigned char output_left[8 * 16]; unsigned char output_right[8 * 16]; - stbir_resize_arbitrary(image, 8, 8, 0, output_left, 8, 16, 0, 0, 0, 0.5f, 1, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); - stbir_resize_arbitrary(image, 8, 8, 0, output_right, 8, 16, 0, 0.5f, 0, 1, 1, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); + stbir_resize_arbitrary(NULL, image, 8, 8, 0, output_left, 8, 16, 0, 0, 0, 0.5f, 1, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); + stbir_resize_arbitrary(NULL, image, 8, 8, 0, output_right, 8, 16, 0, 0.5f, 0, 1, 1, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); for (int x = 0; x < 8; x++) { @@ -407,8 +424,8 @@ void test_subpixel_2() unsigned char output_data_1[16 * 16]; unsigned char output_data_2[16 * 16]; - stbir_resize_arbitrary(image, 8, 8, 0, output_data_1, 16, 16, 0, 0, 0, 1, 1, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_EDGE_WRAP, STBIR_EDGE_WRAP, STBIR_COLORSPACE_SRGB); - stbir_resize_arbitrary(large_image, 32, 32, 0, output_data_2, 16, 16, 0, 0.25f, 0.25f, 0.5f, 0.5f, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); + stbir_resize_arbitrary(NULL, image, 8, 8, 0, output_data_1, 16, 16, 0, 0, 0, 1, 1, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_WRAP, STBIR_EDGE_WRAP, STBIR_COLORSPACE_SRGB); + stbir_resize_arbitrary(NULL, large_image, 32, 32, 0, output_data_2, 16, 16, 0, 0.25f, 0.25f, 0.5f, 0.5f, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); {for (int x = 0; x < 16; x++) { @@ -430,8 +447,8 @@ void test_subpixel_3() unsigned char output_data_1[32 * 32]; unsigned char output_data_2[32 * 32]; - stbir_resize_uint8_subpixel(image, 8, 8, output_data_1, 32, 32, 0, 0, 1, 1, 1, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP); - stbir_resize_uint8_srgb(image, 8, 8, output_data_2, 32, 32, 1, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP); + stbir_resize_region(image, 8, 8, 0, output_data_1, 32, 32, 0, STBIR_TYPE_UINT8, 1,0,-1, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_LINEAR, NULL, 0, 0, 1, 1); + stbir_resize_uint8(image, 8, 8, 0, output_data_2, 32, 32, 0, 1); for (int x = 0; x < 32; x++) { @@ -452,7 +469,7 @@ void test_subpixel_4() unsigned char output[8 * 8]; - stbir_resize_arbitrary(image, 8, 8, 0, output, 8, 8, 0, 0, 0, 1, 1, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_BILINEAR, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR); + stbir_resize_arbitrary(NULL, image, 8, 8, 0, output, 8, 8, 0, 0, 0, 1, 1, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_BILINEAR, STBIR_FILTER_BILINEAR, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR); STBIR_ASSERT(memcmp(image, output, 8 * 8) == 0); } @@ -460,6 +477,15 @@ void test_suite() { int i; + #if 0 // linear_to_srgb_uchar table + for (i=0; i < 256; ++i) { + float f = stbir__srgb_to_linear((i+0.5f)/256.0f); + printf("%9d, ", (int) ((f) * (1<<28))); + if ((i & 7) == 7) + printf("\n"); + } + #endif + test_subpixel_1(); test_subpixel_2(); test_subpixel_3(); diff --git a/tests/stb.dsp b/tests/stb.dsp index 40e3229..9fd5df3 100644 --- a/tests/stb.dsp +++ b/tests/stb.dsp @@ -106,10 +106,18 @@ SOURCE=..\stb_dxt.h # End Source File # Begin Source File +SOURCE=..\stb_herringbone_wang_tile.h +# End Source File +# Begin Source File + SOURCE=..\stb_image.h # End Source File # Begin Source File +SOURCE=..\stb_image_resize.h +# End Source File +# Begin Source File + SOURCE=..\stb_image_write.h # End Source File # Begin Source File