From c1b876768e27990357da2bb0965eb2c4c903984f Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Sun, 31 Aug 2014 06:31:50 -0700 Subject: [PATCH 01/12] in progress new API --- stb_image_resize.h | 418 ++++++++++++++++++++++++++++----------------- 1 file changed, 257 insertions(+), 161 deletions(-) diff --git a/stb_image_resize.h b/stb_image_resize.h index d247ade..91e6b2a 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 @@ -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,176 @@ 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. +// 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_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( 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); +STBIRDEF int stbir_resize_uint8_srgb(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_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_float( 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_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); +typedef enum +{ + STBIR_EDGE_CLAMP = 1, + STBIR_EDGE_REFLECT = 2, + STBIR_EDGE_WRAP = 3, +} stbir_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(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, + stbir_edge edge_wrap_mode); + +////////////////////////////////////////////////////////////////////////////// +// +// 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 + +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; + +#define STBIR_FLAG_NONPREMUL_ALPHA (1 << 0) // The specified alpha channel will be multiplied into all other channels before resampling, then divided back out after. +#define STBIR_FLAG_GAMMA_CORRECT_ALPHA (1 << 1) // The specified alpha channel should be handled as gamma-corrected value even when doing sRGB operations. + +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( 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_colorspace space, stbir_filter filter, + void *context); + +STBIRDEF int stbir_resize_uint16_generic(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_colorspace space, stbir_filter filter, + void *context); + +STBIRDEF int stbir_resize_float_generic( 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_colorspace space, stbir_filter filter, + void *context); + +#define STBIR_ALPHA_CHANNEL_NONE -1 -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); +////////////////////////////////////////////////////////////////////////////// +// +// 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 -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); +typedef enum +{ + STBIR_TYPE_UINT8 , + STBIR_TYPE_UINT16, + STBIR_TYPE_UINT32, + STBIR_TYPE_FLOAT , -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); + STBIR_MAX_TYPES +} stbir_datatype; -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( 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_mode_horizontal, stbir_filter filter_horizontal, + stbir_edge edge_mode_vertical , stbir_filter filter_vertical, + stbir_colorspace space); +STBIRDEF int stbir_resize_subpixel(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_mode_horizontal, stbir_filter filter_horizontal, + stbir_edge edge_mode_vertical , stbir_filter filter_vertical, + stbir_colorspace space, + float x_scale, float x_offset, + float y_scale, float y_offset); -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_region( 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_mode_horizontal, stbir_filter filter_horizontal, + stbir_edge edge_mode_vertical , stbir_filter filter_vertical, + stbir_colorspace space, + 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. -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); - -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); - -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); - - -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); // // //// end header file ///////////////////////////////////////////////////// #endif // STBIR_INCLUDE_STB_IMAGE_RESIZE_H + + + + #ifdef STB_IMAGE_RESIZE_IMPLEMENTATION #ifndef STBIR_ASSERT @@ -229,11 +242,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(c,x) +#define STBIR_FREE(c,x) free(c,x) #endif - #ifndef _MSC_VER #ifdef __cplusplus #define stbir__inline inline @@ -256,7 +268,15 @@ 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 + +// must match stbir_datatype static unsigned char stbir__type_size[] = { 1, // STBIR_TYPE_UINT8 2, // STBIR_TYPE_UINT16 @@ -303,7 +323,7 @@ typedef struct int channels; int alpha_channel; stbir_uint32 flags; - stbir_type type; + stbir_datatype type; stbir_filter filter; stbir_edge edge_horizontal; stbir_edge edge_vertical; @@ -1395,7 +1415,7 @@ 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 stbir__inline stbir_uint32 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) { float horizontal_scale = ((float)output_w / input_w) / (s1 - s0); float vertical_scale = ((float)output_h / input_h) / (t1 - t0); @@ -1430,13 +1450,13 @@ static stbir__inline stbir_size_t stbir__calculate_memory(int input_w, int input 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) + float s0, float t0, float s1, float t1, float *transform, + int channels, int alpha_channel, stbir_uint32 flags, stbir_datatype type, stbir_filter x_filter, stbir_filter y_filter, 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; - stbir_size_t memory_required = stbir__calculate_memory(input_w, input_h, output_w, output_h, s0, t0, s1, t1, channels, filter); + 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]; @@ -1508,12 +1528,21 @@ static int stbir__resize_allocated(const void* input_data, int input_w, int inpu stbir_info->s1 = s1; stbir_info->t1 = t1; - 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); + if (transform) + { + stbir_info->horizontal_scale = transform[0]; + stbir_info->vertical_scale = transform[1]; + stbir_info->horizontal_shift = transform[2]; + stbir_info->vertical_shift = transform[3]; + } + else + { + 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; @@ -1573,10 +1602,10 @@ 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, +STBIRDEF 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) + int channels, int alpha_channel, stbir_uint32 flags, stbir_datatype type, stbir_filter filter, stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace) { int result; size_t memory_required = stbir__calculate_memory(input_w, input_h, output_w, output_h, 0, 0, 1, 1, channels, filter); @@ -1592,63 +1621,63 @@ STBRDEF int stbir_resize_arbitrary(const void* input_data, int input_w, int inpu return result; } -STBRDEF int stbir_resize_uint8_srgb(const stbir_uint8* input_data, int input_w, int input_h, +STBIRDEF 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) { 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); } -STBRDEF int stbir_resize_uint16_srgb(const stbir_uint16* input_data, int input_w, int input_h, +STBIRDEF 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) { 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); } -STBRDEF int stbir_resize_uint32_srgb(const stbir_uint32* input_data, int input_w, int input_h, +STBIRDEF 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) { 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); } -STBRDEF int stbir_resize_float_srgb(const float* input_data, int input_w, int input_h, +STBIRDEF 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) { 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); } -STBRDEF int stbir_resize_uint8_alphaweighted(const stbir_uint8* input_data, int input_w, int input_h, +STBIRDEF 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) { 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); } -STBRDEF int stbir_resize_uint16_alphaweighted(const stbir_uint16* input_data, int input_w, int input_h, +STBIRDEF 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) { 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); } -STBRDEF int stbir_resize_uint32_alphaweighted(const stbir_uint32* input_data, int input_w, int input_h, +STBIRDEF 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) { 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); } -STBRDEF int stbir_resize_float_alphaweighted(const float* input_data, int input_w, int input_h, +STBIRDEF 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) { 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); } -STBRDEF int stbir_resize_uint8_subpixel(const stbir_uint8* input_data, int input_w, int input_h, +STBIRDEF 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) @@ -1656,7 +1685,7 @@ STBRDEF int stbir_resize_uint8_subpixel(const stbir_uint8* input_data, int input 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); } -STBRDEF int stbir_resize_uint16_subpixel(const stbir_uint16* input_data, int input_w, int input_h, +STBIRDEF 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) @@ -1664,7 +1693,7 @@ STBRDEF int stbir_resize_uint16_subpixel(const stbir_uint16* input_data, int inp 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, +STBIRDEF 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) @@ -1672,7 +1701,7 @@ STBRDEF int stbir_resize_uint32_subpixel(const stbir_uint32* input_data, int inp 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, +STBIRDEF 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) @@ -1680,6 +1709,73 @@ STBRDEF int stbir_resize_float_subpixel(const float* input_data, int input_w, in 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); } +STBIRDEF int stbir_resize_uint8( 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); + +STBIRDEF int stbir_resize_uint8_srgb(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); + +STBIRDEF int stbir_resize_float( 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); + +typedef enum +{ + STBIR_EDGE_CLAMP = 1, + STBIR_EDGE_REFLECT = 2, + STBIR_EDGE_WRAP = 3, +} stbir_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(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, + stbir_edge edge_wrap_mode); + +STBIRDEF int stbir_resize_uint8_generic( 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_colorspace space, stbir_filter filter, + void *context); + +STBIRDEF int stbir_resize_uint16_generic(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_colorspace space, stbir_filter filter, + void *context); + +STBIRDEF int stbir_resize_float_generic( 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_colorspace space, stbir_filter filter, + void *context); + +STBIRDEF int stbir_resize( 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_mode_horizontal, stbir_filter filter_horizontal, + stbir_edge edge_mode_vertical , stbir_filter filter_vertical, + stbir_colorspace space); + +STBIRDEF int stbir_resize_subpixel(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_mode_horizontal, stbir_filter filter_horizontal, + stbir_edge edge_mode_vertical , stbir_filter filter_vertical, + stbir_colorspace space, + float x_scale, float x_offset, + float y_scale, float y_offset); + +STBIRDEF int stbir_resize_region( 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_mode_horizontal, stbir_filter filter_horizontal, + stbir_edge edge_mode_vertical , stbir_filter filter_vertical, + stbir_colorspace space, + float s0, float t0, float s1, float t1); + #endif // STB_IMAGE_RESIZE_IMPLEMENTATION /* From 1bd9770e75e666155d9759e27db7a3ad25bfb8c2 Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Sun, 31 Aug 2014 06:47:45 -0700 Subject: [PATCH 02/12] separate filter for horizontal and vertical --- stb_image_resize.h | 82 +++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 37 deletions(-) diff --git a/stb_image_resize.h b/stb_image_resize.h index d247ade..ae4f17d 100644 --- a/stb_image_resize.h +++ b/stb_image_resize.h @@ -304,7 +304,8 @@ typedef struct int alpha_channel; stbir_uint32 flags; stbir_type type; - stbir_filter filter; + stbir_filter horizontal_filter; + stbir_filter vertical_filter; stbir_edge edge_horizontal; stbir_edge edge_vertical; stbir_colorspace colorspace; @@ -504,12 +505,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,12 +522,12 @@ 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) @@ -539,17 +540,18 @@ stbir__inline static int stbir__get_horizontal_contributors_noinfo(stbir_filter stbir__inline static int stbir__get_horizontal_contributors(stbir__info* stbir_info) { - return stbir__get_horizontal_contributors_noinfo(stbir_info->filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale); + return stbir__get_horizontal_contributors_noinfo(stbir_info->horizontal_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) +stbir__inline static int stbir__get_total_coefficients_noinfo(stbir_filter h_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); + return stbir__get_horizontal_contributors_noinfo(h_filter, input_w, output_w, horizontal_scale) + * stbir__get_filter_pixel_width (h_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_total_coefficients_noinfo(stbir_info->horizontal_filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale); } stbir__inline static stbir__contributors* stbir__get_contributor(stbir__info* stbir_info, int n) @@ -560,7 +562,7 @@ 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) @@ -655,12 +657,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 +686,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 +740,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 +750,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 +766,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 @@ -1177,7 +1177,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 +1230,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 +1264,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 +1360,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,25 +1395,27 @@ 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 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 h_filter, stbir_filter v_filter) { float horizontal_scale = ((float)output_w / input_w) / (s1 - s0); float vertical_scale = ((float)output_h / input_h) / (t1 - t0); - 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); + int pixel_margin = stbir__get_filter_pixel_margin(h_filter, input_w, output_w, horizontal_scale); + int filter_height = stbir__get_filter_pixel_width(v_filter, input_h, output_h, vertical_scale); 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); + int contributors_size = stbir__get_horizontal_contributors_noinfo(h_filter, input_w, output_w, horizontal_scale) * sizeof(stbir__contributors); + int horizontal_coefficients_size = stbir__get_total_coefficients_noinfo(h_filter, input_w, output_w, horizontal_scale) * 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); - STBIR_ASSERT(filter != 0); - STBIR_ASSERT(filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late + STBIR_ASSERT(h_filter != 0); + STBIR_ASSERT(h_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late + STBIR_ASSERT(v_filter != 0); + STBIR_ASSERT(v_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late if (stbir__use_upsampling(horizontal_scale)) // The horizontal buffer is for when we're downsampling the height and we @@ -1431,12 +1433,13 @@ static stbir__inline stbir_size_t stbir__calculate_memory(int input_w, int input 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, + int channels, int alpha_channel, stbir_uint32 flags, stbir_type type, stbir_filter h_filter, stbir_filter v_filter, + stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace, void* tempmem, stbir_size_t tempmem_size_in_bytes) { stbir__info* stbir_info = (stbir__info*)tempmem; - stbir_size_t memory_required = stbir__calculate_memory(input_w, input_h, output_w, output_h, s0, t0, s1, t1, channels, filter); + stbir_size_t memory_required = stbir__calculate_memory(input_w, input_h, output_w, output_h, s0, t0, s1, t1, channels, h_filter, v_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]; @@ -1455,10 +1458,14 @@ 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(h_filter != 0); + STBIR_ASSERT(h_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); + STBIR_ASSERT(v_filter != 0); + STBIR_ASSERT(v_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); - if (!filter || filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table)) + if (!h_filter || h_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table)) + return 0; + if (!v_filter || v_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table)) return 0; STBIR_ASSERT(s1 > s0); @@ -1518,7 +1525,8 @@ static int stbir__resize_allocated(const void* input_data, int input_w, int inpu stbir_info->alpha_channel = alpha_channel; stbir_info->flags = flags; stbir_info->type = type; - stbir_info->filter = filter; + stbir_info->horizontal_filter = h_filter; + stbir_info->vertical_filter = v_filter; stbir_info->edge_horizontal = edge_horizontal; stbir_info->edge_vertical = edge_vertical; stbir_info->colorspace = colorspace; @@ -1579,13 +1587,13 @@ STBRDEF int stbir_resize_arbitrary(const void* input_data, int input_w, int inpu 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) { int result; - size_t memory_required = stbir__calculate_memory(input_w, input_h, output_w, output_h, 0, 0, 1, 1, channels, filter); + size_t memory_required = stbir__calculate_memory(input_w, input_h, output_w, output_h, 0, 0, 1, 1, channels, filter, filter); void* extra_memory = STBIR_MALLOC(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(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, filter, edge_horizontal, edge_vertical, colorspace, extra_memory, memory_required); STBIR_FREE(extra_memory); From 2549d8156e8e38977dbbe4f8ac06db5327d72ad9 Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Sun, 31 Aug 2014 07:23:34 -0700 Subject: [PATCH 03/12] in-progress refactoring --- tests/resample_test.cpp | 4 ++-- tests/stb.dsp | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/resample_test.cpp b/tests/resample_test.cpp index 856351b..b906d15 100644 --- a/tests/resample_test.cpp +++ b/tests/resample_test.cpp @@ -187,7 +187,7 @@ 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); @@ -228,7 +228,7 @@ 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); 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 From bbd4e2ee9ac48d51fec23141b268d076059011b7 Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Sun, 31 Aug 2014 07:32:10 -0700 Subject: [PATCH 04/12] new API "finished" but untested --- stb_image_resize.h | 102 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 86 insertions(+), 16 deletions(-) diff --git a/stb_image_resize.h b/stb_image_resize.h index 6678f6f..9ed04be 100644 --- a/stb_image_resize.h +++ b/stb_image_resize.h @@ -119,6 +119,7 @@ STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels typedef enum { + STBIR_FILTER_DEFAULT = 0, STBIR_FILTER_NEAREST = 1, STBIR_FILTER_BILINEAR = 2, STBIR_FILTER_BICUBIC = 3, // A cubic b spline @@ -189,14 +190,14 @@ STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int int num_channels, int alpha_channel, int flags, stbir_edge edge_mode_horizontal, stbir_filter filter_horizontal, stbir_edge edge_mode_vertical , stbir_filter filter_vertical, - stbir_colorspace space); + 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, int num_channels, int alpha_channel, int flags, stbir_edge edge_mode_horizontal, stbir_filter filter_horizontal, stbir_edge edge_mode_vertical , stbir_filter filter_vertical, - stbir_colorspace space, + stbir_colorspace space, void *alloc_context, float x_scale, float x_offset, float y_scale, float y_offset); @@ -205,7 +206,7 @@ STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int int num_channels, int alpha_channel, int flags, stbir_edge edge_mode_horizontal, stbir_filter filter_horizontal, stbir_edge edge_mode_vertical , stbir_filter filter_vertical, - stbir_colorspace space, + 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. @@ -1765,39 +1766,82 @@ STBIRDEF int stbir_resize_float_subpixel(const float* input_data, int input_w, i 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); + int num_channels) +{ + return stbir_resize_arbitrary2(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); +} 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 num_channels) +{ + return stbir_resize_arbitrary2(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_SRGB); +} 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); + int num_channels) +{ + return stbir_resize_arbitrary2(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); +} -// 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, - stbir_edge edge_wrap_mode); + stbir_edge edge_wrap_mode) +{ + return stbir_resize_arbitrary2(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, + edge_wrap_mode, edge_wrap_mode, STBIR_COLORSPACE_SRGB); +} 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_colorspace space, stbir_filter filter, - void *alloc_context); + void *alloc_context) +{ + return stbir_resize_arbitrary2(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); +} 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_colorspace space, stbir_filter filter, - void *alloc_context); + void *alloc_context) +{ + return stbir_resize_arbitrary2(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); +} + 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_colorspace space, stbir_filter filter, - void *alloc_context); + void *alloc_context) +{ + return stbir_resize_arbitrary2(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); +} + 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, @@ -1805,24 +1849,50 @@ STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int int num_channels, int alpha_channel, int flags, stbir_edge edge_mode_horizontal, stbir_filter filter_horizontal, stbir_edge edge_mode_vertical , stbir_filter filter_vertical, - stbir_colorspace space); + stbir_colorspace space, void *alloc_context) +{ + return stbir_resize_arbitrary2(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); +} + 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_filter filter_horizontal, stbir_edge edge_mode_vertical , stbir_filter filter_vertical, - stbir_colorspace space, + stbir_colorspace space, void *alloc_context, float x_scale, float x_offset, - float y_scale, float y_offset); + float y_scale, float y_offset) +{ + float transform[4]; + transform[0] = x_scale; + transform[1] = y_scale; + transform[2] = x_offset; + transform[3] = y_offset; + return stbir_resize_arbitrary2(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); +} 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_filter filter_horizontal, stbir_edge edge_mode_vertical , stbir_filter filter_vertical, - stbir_colorspace space, - float s0, float t0, float s1, float t1); + stbir_colorspace space, void *alloc_context, + float s0, float t0, float s1, float t1) +{ + return stbir_resize_arbitrary2(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 From 664d8961f54d490f877d996495150bd2fba4fc06 Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Sun, 31 Aug 2014 08:34:05 -0700 Subject: [PATCH 05/12] new API partially in-use --- stb_image_resize.h | 51 +++++++++++++---------------------------- tests/resample_test.cpp | 4 ++-- 2 files changed, 18 insertions(+), 37 deletions(-) diff --git a/stb_image_resize.h b/stb_image_resize.h index 9ed04be..803ed45 100644 --- a/stb_image_resize.h +++ b/stb_image_resize.h @@ -119,7 +119,7 @@ STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels typedef enum { - STBIR_FILTER_DEFAULT = 0, + 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 @@ -194,6 +194,7 @@ STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int 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_filter filter_horizontal, stbir_edge edge_mode_vertical , stbir_filter filter_vertical, @@ -203,6 +204,7 @@ STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int 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_filter filter_horizontal, stbir_edge edge_mode_vertical , stbir_filter filter_vertical, @@ -1564,8 +1566,8 @@ static int stbir__resize_allocated(stbir__info *stbir_info, stbir_info->alpha_channel = alpha_channel; stbir_info->flags = flags; stbir_info->type = type; - stbir_info->horizontal_filter = h_filter; - stbir_info->vertical_filter = v_filter; + //stbir_info->horizontal_filter = h_filter; + //stbir_info->vertical_filter = v_filter; stbir_info->edge_horizontal = edge_horizontal; stbir_info->edge_vertical = edge_vertical; stbir_info->colorspace = colorspace; @@ -1575,7 +1577,7 @@ static int stbir__resize_allocated(stbir__info *stbir_info, #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_contributors = (stbir__contributors *) tempmem; 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); @@ -1620,32 +1622,6 @@ static int stbir__resize_allocated(stbir__info *stbir_info, } -STBIRDEF 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_datatype type, - stbir_filter filter, stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace) -{ - stbir__info info; - int result; - size_t memory_required; - void* extra_memory; - stbir__calculate_transform(&info, input_w, input_h, output_w, output_h, s0,t0,s1,t1,NULL); - stbir__choose_filter(&info, filter, filter); - memory_required = stbir__calculate_memory(&info, input_w, input_h, output_w, output_h, 0, 0, 1, 1, channels); - extra_memory = STBIR_MALLOC(NULL, memory_required); - - if (!extra_memory) - return 0; - - result = stbir__resize_allocated(&info, input_data, input_w, input_h, input_stride_in_bytes, output_data, output_w, output_h, output_stride_in_bytes, s0, t0, s1, t1, NULL, - channels, alpha_channel, flags, type, filter, filter, edge_horizontal, edge_vertical, colorspace, extra_memory, memory_required); - - STBIR_FREE(NULL, extra_memory); - - return result; -} - STBIRDEF int stbir_resize_arbitrary2( void *alloc_context, const void* input_data, int input_w, int input_h, int input_stride_in_bytes, @@ -1675,14 +1651,19 @@ STBIRDEF int stbir_resize_arbitrary2( return result; } - -STBIRDEF 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_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_datatype type, + stbir_filter filter, stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace) { - 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_arbitrary2(NULL, input_data, input_w, input_h, input_stride_in_bytes, + output_data, output_w, output_h, output_stride_in_bytes, + s0,t0,s1,t1,NULL, channels, alpha_channel, flags, type, + filter,filter, edge_horizontal,edge_vertical, colorspace); } + STBIRDEF 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) diff --git a/tests/resample_test.cpp b/tests/resample_test.cpp index b906d15..b06b966 100644 --- a/tests/resample_test.cpp +++ b/tests/resample_test.cpp @@ -278,7 +278,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); free(channels_data); stbi_image_free(input_data); @@ -431,7 +431,7 @@ void test_subpixel_3() 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_uint8(image, 8, 8, 0, output_data_2, 32, 32, 0, 1); for (int x = 0; x < 32; x++) { From 732fec68ee8e4e85251a37f2881d9bae0c003ac1 Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Sun, 31 Aug 2014 08:55:41 -0700 Subject: [PATCH 06/12] tweak new API, get it partly working --- stb_image_resize.h | 38 +++++++++++++++++--------------- tests/resample_test.cpp | 49 +++++++++++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 34 deletions(-) diff --git a/stb_image_resize.h b/stb_image_resize.h index 803ed45..b497f35 100644 --- a/stb_image_resize.h +++ b/stb_image_resize.h @@ -143,19 +143,19 @@ typedef enum 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_colorspace space, stbir_filter filter, + 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_colorspace space, stbir_filter filter, + 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_colorspace space, stbir_filter filter, + stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, void *alloc_context); #define STBIR_ALPHA_CHANNEL_NONE -1 @@ -188,16 +188,16 @@ STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int 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_filter filter_horizontal, - stbir_edge edge_mode_vertical , stbir_filter filter_vertical, + 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_filter filter_horizontal, - stbir_edge edge_mode_vertical , stbir_filter filter_vertical, + 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 x_offset, float y_scale, float y_offset); @@ -206,8 +206,8 @@ STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int 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_filter filter_horizontal, - stbir_edge edge_mode_vertical , stbir_filter filter_vertical, + 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. @@ -1651,6 +1651,7 @@ STBIRDEF int stbir_resize_arbitrary2( return result; } +#if 0 STBIRDEF 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, @@ -1744,6 +1745,7 @@ STBIRDEF int stbir_resize_float_subpixel(const float* input_data, int input_w, i { 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); } +#endif 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, @@ -1789,7 +1791,7 @@ STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels 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_colorspace space, stbir_filter filter, + stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, void *alloc_context) { return stbir_resize_arbitrary2(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, @@ -1801,7 +1803,7 @@ STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int 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_colorspace space, stbir_filter filter, + stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, void *alloc_context) { return stbir_resize_arbitrary2(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, @@ -1814,7 +1816,7 @@ STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int 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_colorspace space, stbir_filter filter, + stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, void *alloc_context) { return stbir_resize_arbitrary2(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, @@ -1828,8 +1830,8 @@ STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int 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_filter filter_horizontal, - stbir_edge edge_mode_vertical , stbir_filter filter_vertical, + 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_arbitrary2(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, @@ -1843,8 +1845,8 @@ STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int 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_filter filter_horizontal, - stbir_edge edge_mode_vertical , stbir_filter filter_vertical, + 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 x_offset, float y_scale, float y_offset) @@ -1864,8 +1866,8 @@ STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int 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_filter filter_horizontal, - stbir_edge edge_mode_vertical , stbir_filter filter_vertical, + 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) { diff --git a/tests/resample_test.cpp b/tests/resample_test.cpp index b06b966..50e1cde 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_arbitrary2(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_arbitrary2(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_arbitrary2(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); @@ -192,6 +192,10 @@ void test_format(const char* file, float width_percent, float height_percent, st 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_arbitrary2(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); @@ -233,6 +237,9 @@ void test_float(const char* file, float width_percent, float height_percent, stb 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_arbitrary2(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); @@ -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_arbitrary2(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_arbitrary2(NULL, input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, NULL, n, 3, STBIR_FLAG_NONPREMUL_ALPHA, 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_arbitrary2(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_arbitrary2(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_arbitrary2(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_arbitrary2(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_arbitrary2(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_arbitrary2(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,7 +447,7 @@ 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_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_arbitrary2(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); } From 9a1d34843e524b80fad39d50267b07b064415e23 Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Sun, 31 Aug 2014 09:10:49 -0700 Subject: [PATCH 07/12] STBIR_EDGE_ZERO --- stb_image_resize.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/stb_image_resize.h b/stb_image_resize.h index b497f35..0c6a5d7 100644 --- a/stb_image_resize.h +++ b/stb_image_resize.h @@ -94,6 +94,7 @@ typedef enum STBIR_EDGE_CLAMP = 1, STBIR_EDGE_REFLECT = 2, STBIR_EDGE_WRAP = 3, + STBIR_EDGE_ZERO = 4, } stbir_edge; // This function adds the ability to specify how requests to sample off the edge of the image are handled. @@ -593,6 +594,9 @@ static int stbir__edge_wrap_slow(stbir_edge edge, int n, int max) { switch (edge) { + case STBIR_EDGE_ZERO: + return 0; + case STBIR_EDGE_CLAMP: if (n < 0) return 0; @@ -600,7 +604,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: { @@ -621,7 +625,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: @@ -636,6 +640,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"); From 84520de6c47b13b9faa8e4f6b566375d36f22954 Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Sun, 31 Aug 2014 09:32:17 -0700 Subject: [PATCH 08/12] finish STBI_EDGE_ZERO, untested --- stb_image_resize.h | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/stb_image_resize.h b/stb_image_resize.h index 0c6a5d7..b1b0670 100644 --- a/stb_image_resize.h +++ b/stb_image_resize.h @@ -595,7 +595,7 @@ static int stbir__edge_wrap_slow(stbir_edge edge, int n, int max) switch (edge) { case STBIR_EDGE_ZERO: - return 0; + return 0; // we'll decode the wrong pixel here, and then overwrite with 0s later case STBIR_EDGE_CLAMP: if (n < 0) @@ -832,6 +832,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): @@ -947,6 +957,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) From 07c35180f79380c8341d91755242456c7f146e7b Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Sun, 31 Aug 2014 09:45:29 -0700 Subject: [PATCH 09/12] tweak new API --- stb_image_resize.h | 58 ++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/stb_image_resize.h b/stb_image_resize.h index b1b0670..80dff68 100644 --- a/stb_image_resize.h +++ b/stb_image_resize.h @@ -81,14 +81,26 @@ STBIRDEF int stbir_resize_uint8( const unsigned char *input_pixels , int inp unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, int num_channels); -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); - 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); + +// +// 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. By default, +// alpha channel is linear even if colors are sRGB. + +#define STBIR_ALPHA_CHANNEL_NONE -1 +#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_GAMMA_CORRECT_ALPHA (1 << 1) // The specified alpha channel should be handled as gamma-corrected value even when doing sRGB operations. + +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); + + typedef enum { STBIR_EDGE_CLAMP = 1, @@ -100,7 +112,7 @@ typedef enum // 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 num_channels, int alpha_channel, int flags, stbir_edge edge_wrap_mode); ////////////////////////////////////////////////////////////////////////////// @@ -109,7 +121,7 @@ STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels // // This extends the easy-to-use API as follows: // -// * Alpha-channel can be processed separately +// * 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) @@ -128,9 +140,6 @@ typedef enum STBIR_FILTER_MITCHELL = 5, } stbir_filter; -#define STBIR_FLAG_NONPREMUL_ALPHA (1 << 0) // The specified alpha channel will be multiplied into all other channels before resampling, then divided back out after. -#define STBIR_FLAG_GAMMA_CORRECT_ALPHA (1 << 1) // The specified alpha channel should be handled as gamma-corrected value even when doing sRGB operations. - typedef enum { STBIR_COLORSPACE_LINEAR, @@ -159,7 +168,6 @@ STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, void *alloc_context); -#define STBIR_ALPHA_CHANNEL_NONE -1 ////////////////////////////////////////////////////////////////////////////// @@ -942,7 +950,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++) { @@ -1125,7 +1133,7 @@ static stbir__inline void stbir__encode_pixel(stbir__info* stbir_info, void* out int n; float divide_alpha = 1; - if (stbir_info->flags&STBIR_FLAG_NONPREMUL_ALPHA) { + if (!(stbir_info->flags&STBIR_FLAG_PREMULTIPLIED_ALPHA)) { float alpha = encode_buffer[encode_pixel_index + alpha_channel]; float reciprocal_alpha = alpha ? 1.0f / alpha : 0; for (n = 0; n < channels; n++) @@ -1558,7 +1566,7 @@ static int stbir__resize_allocated(stbir__info *stbir_info, if (alpha_channel < 0) flags = STBIR_FLAG_GAMMA_CORRECT_ALPHA; // this shouldn't be necessary in the long run, but safety for now - if (!(flags&STBIR_FLAG_GAMMA_CORRECT_ALPHA) || (flags&STBIR_FLAG_NONPREMUL_ALPHA)) + if (!(flags&STBIR_FLAG_GAMMA_CORRECT_ALPHA) || !(flags&STBIR_FLAG_PREMULTIPLIED_ALPHA)) STBIR_ASSERT(alpha_channel >= 0 && alpha_channel < channels); if (alpha_channel >= channels) @@ -1786,16 +1794,6 @@ STBIRDEF int stbir_resize_uint8( const unsigned char *input_pixels , int inp STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR); } -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) -{ - return stbir_resize_arbitrary2(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_SRGB); -} - 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) @@ -1806,14 +1804,24 @@ STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , i STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR); } +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_arbitrary2(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); +} + 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 num_channels, int alpha_channel, int flags, stbir_edge edge_wrap_mode) { return stbir_resize_arbitrary2(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, + 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); } From 24c540e1b0bddcfe0753640df365b8a1a564491a Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Sun, 31 Aug 2014 10:00:54 -0700 Subject: [PATCH 10/12] rename alpha gamma flag --- stb_image_resize.h | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/stb_image_resize.h b/stb_image_resize.h index 80dff68..6ec2919 100644 --- a/stb_image_resize.h +++ b/stb_image_resize.h @@ -70,7 +70,7 @@ typedef uint32_t stbir_uint32; // * 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. -// In the case of an error an assert with be triggered, #define STBIR_ASSERT() to see it. +// #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, @@ -86,15 +86,16 @@ STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , i int num_channels); -// // 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. By default, -// alpha channel is linear even if colors are sRGB. +// 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. -#define STBIR_ALPHA_CHANNEL_NONE -1 -#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_GAMMA_CORRECT_ALPHA (1 << 1) // The specified alpha channel should be handled as gamma-corrected value even when doing sRGB operations. +#define STBIR_ALPHA_CHANNEL_NONE -1 + +#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. 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, @@ -870,7 +871,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; @@ -893,7 +894,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; @@ -916,7 +917,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; @@ -939,7 +940,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]; } @@ -1152,7 +1153,7 @@ static stbir__inline void stbir__encode_pixel(stbir__info* stbir_info, void* out 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)]; - if (!(stbir_info->flags&STBIR_FLAG_GAMMA_CORRECT_ALPHA)) + 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; @@ -1166,7 +1167,7 @@ static stbir__inline void stbir__encode_pixel(stbir__info* stbir_info, void* out 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_GAMMA_CORRECT_ALPHA)) + 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; @@ -1180,7 +1181,7 @@ static stbir__inline void stbir__encode_pixel(stbir__info* stbir_info, void* out 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_GAMMA_CORRECT_ALPHA)) + 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; @@ -1194,7 +1195,7 @@ static stbir__inline void stbir__encode_pixel(stbir__info* stbir_info, void* out 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_GAMMA_CORRECT_ALPHA)) + 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; @@ -1564,9 +1565,9 @@ static int stbir__resize_allocated(stbir__info *stbir_info, 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; // this shouldn't be necessary in the long run, but safety for now - if (!(flags&STBIR_FLAG_GAMMA_CORRECT_ALPHA) || !(flags&STBIR_FLAG_PREMULTIPLIED_ALPHA)) + if (!(flags&STBIR_FLAG_ALPHA_USES_COLORSPACE) || !(flags&STBIR_FLAG_PREMULTIPLIED_ALPHA)) STBIR_ASSERT(alpha_channel >= 0 && alpha_channel < channels); if (alpha_channel >= channels) From aee30095c717eae57b9d559538062d8451a4dd0b Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Mon, 1 Sep 2014 16:52:04 -0700 Subject: [PATCH 11/12] refactor internal interfaces to avoid passing things multiple times; finish prepping 'stbir__info' even before calculate_memory; get rid of 'noinfo' functions since now calculate_memory doesn't need 'em; add new binary-searched sRGB function (untested) --- stb_image_resize.h | 423 +++++++++++++++++----------------------- tests/resample_test.cpp | 39 ++-- 2 files changed, 200 insertions(+), 262 deletions(-) diff --git a/stb_image_resize.h b/stb_image_resize.h index 6ec2919..e46cebc 100644 --- a/stb_image_resize.h +++ b/stb_image_resize.h @@ -19,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 @@ -209,8 +209,8 @@ STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int 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 x_offset, - float y_scale, float y_offset); + 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, @@ -290,6 +290,10 @@ typedef unsigned char stbir__validate_uint32[sizeof(stbir_uint32) == 4 ? 1 : -1] #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 @@ -412,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; @@ -434,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; @@ -442,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) { @@ -502,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 }, @@ -564,28 +605,18 @@ stbir__inline static int stbir__get_filter_pixel_margin_vertical(stbir__info* st 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->horizontal_filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale); -} - -stbir__inline static int stbir__get_total_coefficients_noinfo(stbir_filter h_filter, int input_w, int output_w, float horizontal_scale) -{ - return stbir__get_horizontal_contributors_noinfo(h_filter, input_w, output_w, horizontal_scale) - * stbir__get_filter_pixel_width (h_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->horizontal_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) @@ -1151,7 +1182,7 @@ static stbir__inline void stbir__encode_pixel(stbir__info* stbir_info, void* out 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)]; + ((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); @@ -1457,22 +1488,36 @@ static void stbir__buffer_loop_downsample(stbir__info* stbir_info) stbir__empty_ring_buffer(stbir_info, stbir_info->output_h); } -static void stbir__calculate_transform(stbir__info *stbir_info, int input_w, int input_h, int output_w, int output_h, float s0, float t0, float s1, float t1, float *transform) +static void stbir__setup(stbir__info *info, int input_w, int input_h, int output_w, int output_h, int channels) { + info->input_w = input_w; + info->input_h = input_h; + info->output_w = output_w; + info->output_h = output_h; + info->channels = channels; +} + +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; + if (transform) { - stbir_info->horizontal_scale = transform[0]; - stbir_info->vertical_scale = transform[1]; - stbir_info->horizontal_shift = transform[2]; - stbir_info->vertical_shift = transform[3]; + info->horizontal_scale = transform[0]; + info->vertical_scale = transform[1]; + info->horizontal_shift = transform[2]; + info->vertical_shift = transform[3]; } else { - stbir_info->horizontal_scale = ((float)output_w / input_w) / (s1 - s0); - stbir_info->vertical_scale = ((float)output_h / input_h) / (t1 - t0); + info->horizontal_scale = ((float)info->output_w / info->input_w) / (s1 - s0); + info->vertical_scale = ((float)info->output_h / info->input_h) / (t1 - t0); - stbir_info->horizontal_shift = s0 * input_w / (s1 - s0); - stbir_info->vertical_shift = t0 * input_h / (t1 - t0); + info->horizontal_shift = s0 * info->input_w / (s1 - s0); + info->vertical_shift = t0 * info->input_h / (t1 - t0); } } @@ -1486,26 +1531,25 @@ static void stbir__choose_filter(stbir__info *info, stbir_filter h_filter, stbir info->vertical_filter = v_filter; } -static stbir_uint32 stbir__calculate_memory(stbir__info *stbir_info, int input_w, int input_h, int output_w, int output_h, float s0, float t0, float s1, float t1, int channels) +static stbir_uint32 stbir__calculate_memory(stbir__info *info) { - int pixel_margin = stbir__get_filter_pixel_margin(stbir_info->horizontal_filter, input_w, output_w, stbir_info->horizontal_scale); - int filter_height = stbir__get_filter_pixel_width(stbir_info->vertical_filter, input_h, output_h, stbir_info->vertical_scale); + 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 info_size = sizeof(stbir__info); - int contributors_size = stbir__get_horizontal_contributors_noinfo(stbir_info->horizontal_filter, input_w, output_w, stbir_info->horizontal_scale) * sizeof(stbir__contributors); - int horizontal_coefficients_size = stbir__get_total_coefficients_noinfo(stbir_info->horizontal_filter, input_w, output_w, stbir_info->horizontal_scale) * sizeof(float); + 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(stbir_info->horizontal_filter != 0); - STBIR_ASSERT(stbir_info->horizontal_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late - STBIR_ASSERT(stbir_info->vertical_filter != 0); - STBIR_ASSERT(stbir_info->vertical_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(stbir_info->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. @@ -1515,21 +1559,20 @@ static stbir_uint32 stbir__calculate_memory(stbir__info *stbir_info, int input_w // 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(stbir__info *stbir_info, - 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, float *transform, - int channels, int alpha_channel, stbir_uint32 flags, stbir_datatype type, stbir_filter h_filter, stbir_filter v_filter, +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) { - size_t memory_required = stbir__calculate_memory(stbir_info, input_w, input_h, output_w, output_h, s0, t0, s1, t1, channels); + size_t memory_required = stbir__calculate_memory(info); - 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 @@ -1545,32 +1588,27 @@ static int stbir__resize_allocated(stbir__info *stbir_info, memcpy(overwrite_tempmem_after_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE); #endif - STBIR_ASSERT(h_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); - STBIR_ASSERT(v_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); + STBIR_ASSERT(info->channels <= STBIR_MAX_CHANNELS); + STBIR_ASSERT(info->channels >= 0); - if (h_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table)) - return 0; - if (v_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_ALPHA_USES_COLORSPACE; // 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_ALPHA_USES_COLORSPACE) || !(flags&STBIR_FLAG_PREMULTIPLIED_ALPHA)) - STBIR_ASSERT(alpha_channel >= 0 && alpha_channel < channels); + STBIR_ASSERT(alpha_channel >= 0 && alpha_channel < info->channels); - if (alpha_channel >= channels) + if (alpha_channel >= info->channels) return 0; STBIR_ASSERT(tempmem); @@ -1585,69 +1623,57 @@ static int stbir__resize_allocated(stbir__info *stbir_info, 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->channels = channels; - stbir_info->alpha_channel = alpha_channel; - stbir_info->flags = flags; - stbir_info->type = type; - //stbir_info->horizontal_filter = h_filter; - //stbir_info->vertical_filter = v_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__contributors *) tempmem; - 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); @@ -1660,7 +1686,7 @@ static int stbir__resize_allocated(stbir__info *stbir_info, } -STBIRDEF int stbir_resize_arbitrary2( +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, @@ -1673,123 +1699,32 @@ STBIRDEF int stbir_resize_arbitrary2( int result; size_t memory_required; void* extra_memory; - stbir__calculate_transform(&info, input_w, input_h, output_w, output_h, s0,t0,s1,t1,transform); + + 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, input_w, input_h, output_w, output_h, 0, 0, 1, 1, channels); + memory_required = stbir__calculate_memory(&info); extra_memory = STBIR_MALLOC(NULL, memory_required); if (!extra_memory) return 0; - result = stbir__resize_allocated(&info, input_data, input_w, input_h, input_stride_in_bytes, output_data, output_w, output_h, output_stride_in_bytes, s0, t0, s1, t1, NULL, - channels, alpha_channel, flags, type, h_filter, v_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(context, extra_memory); return result; } -#if 0 -STBIRDEF 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_datatype type, - stbir_filter filter, stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace) -{ - return stbir_resize_arbitrary2(NULL, input_data, input_w, input_h, input_stride_in_bytes, - output_data, output_w, output_h, output_stride_in_bytes, - s0,t0,s1,t1,NULL, channels, alpha_channel, flags, type, - filter,filter, edge_horizontal,edge_vertical, colorspace); -} - - -STBIRDEF 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) -{ - 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); -} - -STBIRDEF 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) -{ - 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); -} - -STBIRDEF 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) -{ - 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); -} - -STBIRDEF 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) -{ - 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); -} - -STBIRDEF 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) -{ - 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); -} - -STBIRDEF 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) -{ - 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); -} - -STBIRDEF 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) -{ - 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); -} - -STBIRDEF 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) -{ - 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); -} - -STBIRDEF 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) -{ - 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); -} - -STBIRDEF 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); -} - -STBIRDEF 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); -} -#endif - 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_arbitrary2(NULL, input_pixels, input_w, input_h, input_stride_in_bytes, + 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); @@ -1799,7 +1734,7 @@ STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , i float *output_pixels, int output_w, int output_h, int output_stride_in_bytes, int num_channels) { - return stbir_resize_arbitrary2(NULL, input_pixels, input_w, input_h, input_stride_in_bytes, + 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); @@ -1809,7 +1744,7 @@ STBIRDEF int stbir_resize_uint8_srgb(const unsigned char *input_pixels , int inp 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_arbitrary2(NULL, input_pixels, input_w, input_h, input_stride_in_bytes, + 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); @@ -1820,7 +1755,7 @@ STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels int num_channels, int alpha_channel, int flags, stbir_edge edge_wrap_mode) { - return stbir_resize_arbitrary2(NULL, input_pixels, input_w, input_h, input_stride_in_bytes, + 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); @@ -1832,7 +1767,7 @@ STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, void *alloc_context) { - return stbir_resize_arbitrary2(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, + 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); @@ -1844,7 +1779,7 @@ STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, void *alloc_context) { - return stbir_resize_arbitrary2(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, + 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); @@ -1857,7 +1792,7 @@ STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, void *alloc_context) { - return stbir_resize_arbitrary2(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, + 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); @@ -1872,7 +1807,7 @@ STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int stbir_filter filter_horizontal, stbir_filter filter_vertical, stbir_colorspace space, void *alloc_context) { - return stbir_resize_arbitrary2(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, + 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); @@ -1886,15 +1821,15 @@ STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int 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 x_offset, - float y_scale, float y_offset) + float x_scale, float y_scale, + float x_offset, float y_offset) { float transform[4]; transform[0] = x_scale; transform[1] = y_scale; transform[2] = x_offset; transform[3] = y_offset; - return stbir_resize_arbitrary2(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, + 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); @@ -1909,16 +1844,10 @@ STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int stbir_colorspace space, void *alloc_context, float s0, float t0, float s1, float t1) { - return stbir_resize_arbitrary2(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, + 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 50e1cde..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_arbitrary2(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); + 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_arbitrary2(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); + 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); @@ -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_arbitrary2(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); + 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); @@ -204,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_arbitrary2(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); + 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); @@ -248,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_arbitrary2(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); + 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); @@ -288,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, 0, output_data, new_w, new_h, 0, channels); + 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); @@ -316,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_arbitrary2(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); + 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); @@ -351,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_arbitrary2(NULL, input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, NULL, n, 3, STBIR_FLAG_NONPREMUL_ALPHA, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, 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_arbitrary2(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); + 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); @@ -379,13 +379,13 @@ void test_subpixel_1() unsigned char output_data[16 * 16]; - stbir_resize_arbitrary2(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); + 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_arbitrary2(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_arbitrary2(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); + 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++) { @@ -424,8 +424,8 @@ void test_subpixel_2() unsigned char output_data_1[16 * 16]; unsigned char output_data_2[16 * 16]; - stbir_resize_arbitrary2(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_arbitrary2(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); + 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++) { @@ -469,7 +469,7 @@ void test_subpixel_4() unsigned char output[8 * 8]; - stbir_resize_arbitrary2(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_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); } @@ -477,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(); From 952c26e626144aef43ae052503fcd1c5d979230c Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Mon, 1 Sep 2014 19:29:28 -0700 Subject: [PATCH 12/12] inline stbir__encode_pixel into stbir__encode_scanline --- stb_image_resize.h | 130 ++++++++++++++++++++++++++++++--------------- 1 file changed, 88 insertions(+), 42 deletions(-) diff --git a/stb_image_resize.h b/stb_image_resize.h index e46cebc..c68a778 100644 --- a/stb_image_resize.h +++ b/stb_image_resize.h @@ -1160,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_PREMULTIPLIED_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_to_srgb_uchar(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_ALPHA_USES_COLORSPACE)) - ((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_ALPHA_USES_COLORSPACE)) - ((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_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); + 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_ALPHA_USES_COLORSPACE)) - ((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: @@ -1237,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;