From bbc340d4816b3091494050d23e6ba895745308ce Mon Sep 17 00:00:00 2001 From: Jorge Rodriguez Date: Tue, 12 Aug 2014 12:22:38 -0700 Subject: [PATCH] Support different edge behavior on vertical and horizontal axis. --- stb_resample.h | 45 ++++++++++++++++++++++------------------- tests/resample_test.cpp | 26 ++++++++++++------------ 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/stb_resample.h b/stb_resample.h index d9b3166..fe9e133 100644 --- a/stb_resample.h +++ b/stb_resample.h @@ -198,7 +198,7 @@ extern "C" { STBRDEF int stbr_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 nonpremultiplied_alpha_channel, stbr_type type, stbr_filter filter, stbr_edge edge, stbr_colorspace colorspace); + int channels, int nonpremultiplied_alpha_channel, stbr_type type, stbr_filter filter, stbr_edge edge_horizontal, stbr_edge edge_vertical, stbr_colorspace colorspace); #ifdef __cplusplus } @@ -303,7 +303,8 @@ typedef struct int alpha_channel; stbr_type type; stbr_filter filter; - stbr_edge edge; + stbr_edge edge_horizontal; + stbr_edge edge_vertical; stbr_colorspace colorspace; stbr__contributors* horizontal_contributors; @@ -784,15 +785,16 @@ static void stbr__decode_scanline(stbr__info* stbr_info, int n) int input_stride = stbr_info->input_stride_bytes / stbr__type_size[stbr_info->type]; const void* input_data = stbr_info->input_data; float* decode_buffer = stbr__get_decode_buffer(stbr_info); - stbr_edge edge = stbr_info->edge; - int in_buffer_row_index = stbr__edge_wrap(edge, n, stbr_info->input_h) * input_stride; + stbr_edge edge_horizontal = stbr_info->edge_horizontal; + stbr_edge edge_vertical = stbr_info->edge_vertical; + int in_buffer_row_index = stbr__edge_wrap(edge_vertical, n, stbr_info->input_h) * input_stride; int max_x = input_w + stbr__get_filter_pixel_margin_horizontal(stbr_info); int decode = STBR__DECODE(type, colorspace); for (x = -stbr__get_filter_pixel_margin_horizontal(stbr_info); x < max_x; x++) { int decode_pixel_index = x * channels; - int input_pixel_index = in_buffer_row_index + stbr__edge_wrap(edge, x, input_w) * channels; + int input_pixel_index = in_buffer_row_index + stbr__edge_wrap(edge_horizontal, x, input_w) * channels; switch (decode) { @@ -1336,7 +1338,7 @@ static stbr_size_t stbr__calculate_memory(int input_w, int input_h, int output_w static int stbr__resize_advanced(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, stbr_type type, stbr_filter filter, stbr_edge edge, stbr_colorspace colorspace, + int channels, int alpha_channel, stbr_type type, stbr_filter filter, stbr_edge edge_horizontal, stbr_edge edge_vertical, stbr_colorspace colorspace, void* tempmem, stbr_size_t tempmem_size_in_bytes) { stbr__info* stbr_info = (stbr__info*)tempmem; @@ -1419,7 +1421,8 @@ static int stbr__resize_advanced(const void* input_data, int input_w, int input_ stbr_info->alpha_channel = alpha_channel; stbr_info->type = type; stbr_info->filter = filter; - stbr_info->edge = edge; + stbr_info->edge_horizontal = edge_horizontal; + stbr_info->edge_vertical = edge_vertical; stbr_info->colorspace = colorspace; stbr_info->ring_buffer_length_bytes = output_w * channels * sizeof(float); @@ -1516,7 +1519,7 @@ STBRDEF stbr_inline int stbr_resize_uint8_srgb(const stbr_uint8* input_data, int if (!extra_memory) return 0; - result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, 0, STBR_TYPE_UINT8, filter, edge, STBR_COLORSPACE_SRGB, extra_memory, memory_required); + result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, 0, STBR_TYPE_UINT8, filter, edge, edge, STBR_COLORSPACE_SRGB, extra_memory, memory_required); STBR_FREE(extra_memory); @@ -1534,7 +1537,7 @@ STBRDEF stbr_inline int stbr_resize_uint16_srgb(const stbr_uint16* input_data, i if (!extra_memory) return 0; - result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, 0, STBR_TYPE_UINT16, filter, edge, STBR_COLORSPACE_SRGB, extra_memory, memory_required); + result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, 0, STBR_TYPE_UINT16, filter, edge, edge, STBR_COLORSPACE_SRGB, extra_memory, memory_required); STBR_FREE(extra_memory); @@ -1552,7 +1555,7 @@ STBRDEF stbr_inline int stbr_resize_uint32_srgb(const stbr_uint32* input_data, i if (!extra_memory) return 0; - result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, 0, STBR_TYPE_UINT32, filter, edge, STBR_COLORSPACE_SRGB, extra_memory, memory_required); + result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, 0, STBR_TYPE_UINT32, filter, edge, edge, STBR_COLORSPACE_SRGB, extra_memory, memory_required); STBR_FREE(extra_memory); @@ -1570,7 +1573,7 @@ STBRDEF stbr_inline int stbr_resize_float_srgb(const float* input_data, int inpu if (!extra_memory) return 0; - result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, 0, STBR_TYPE_FLOAT, filter, edge, STBR_COLORSPACE_SRGB, extra_memory, memory_required); + result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, 0, STBR_TYPE_FLOAT, filter, edge, edge, STBR_COLORSPACE_SRGB, extra_memory, memory_required); STBR_FREE(extra_memory); @@ -1588,7 +1591,7 @@ STBRDEF stbr_inline int stbr_resize_uint8_alphaweighted(const stbr_uint8* input_ if (!extra_memory) return 0; - result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, nonpremultiplied_alpha_channel, STBR_TYPE_UINT8, filter, edge, colorspace, extra_memory, memory_required); + result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, nonpremultiplied_alpha_channel, STBR_TYPE_UINT8, filter, edge, edge, colorspace, extra_memory, memory_required); STBR_FREE(extra_memory); @@ -1606,7 +1609,7 @@ STBRDEF stbr_inline int stbr_resize_uint16_alphaweighted(const stbr_uint16* inpu if (!extra_memory) return 0; - result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, nonpremultiplied_alpha_channel, STBR_TYPE_UINT16, filter, edge, colorspace, extra_memory, memory_required); + result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, nonpremultiplied_alpha_channel, STBR_TYPE_UINT16, filter, edge, edge, colorspace, extra_memory, memory_required); STBR_FREE(extra_memory); @@ -1624,7 +1627,7 @@ STBRDEF stbr_inline int stbr_resize_uint32_alphaweighted(const stbr_uint32* inpu if (!extra_memory) return 0; - result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, nonpremultiplied_alpha_channel, STBR_TYPE_UINT32, filter, edge, colorspace, extra_memory, memory_required); + result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, nonpremultiplied_alpha_channel, STBR_TYPE_UINT32, filter, edge, edge, colorspace, extra_memory, memory_required); STBR_FREE(extra_memory); @@ -1642,7 +1645,7 @@ STBRDEF stbr_inline int stbr_resize_float_alphaweighted(const float* input_data, if (!extra_memory) return 0; - result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, nonpremultiplied_alpha_channel, STBR_TYPE_FLOAT, filter, edge, colorspace, extra_memory, memory_required); + result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, 0, 0, 1, 1, channels, nonpremultiplied_alpha_channel, STBR_TYPE_FLOAT, filter, edge, edge, colorspace, extra_memory, memory_required); STBR_FREE(extra_memory); @@ -1661,7 +1664,7 @@ STBRDEF stbr_inline int stbr_resize_uint8_subpixel(const stbr_uint8* input_data, if (!extra_memory) return 0; - result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, s0, t0, s1, t1, channels, 0, STBR_TYPE_UINT8, filter, edge, STBR_COLORSPACE_SRGB, extra_memory, memory_required); + result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, s0, t0, s1, t1, channels, 0, STBR_TYPE_UINT8, filter, edge, edge, STBR_COLORSPACE_SRGB, extra_memory, memory_required); STBR_FREE(extra_memory); @@ -1680,7 +1683,7 @@ STBRDEF stbr_inline int stbr_resize_uint16_subpixel(const stbr_uint16* input_dat if (!extra_memory) return 0; - result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, s0, t0, s1, t1, channels, 0, STBR_TYPE_UINT16, filter, edge, STBR_COLORSPACE_SRGB, extra_memory, memory_required); + result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, s0, t0, s1, t1, channels, 0, STBR_TYPE_UINT16, filter, edge, edge, STBR_COLORSPACE_SRGB, extra_memory, memory_required); STBR_FREE(extra_memory); @@ -1699,7 +1702,7 @@ STBRDEF stbr_inline int stbr_resize_uint32_subpixel(const stbr_uint32* input_dat if (!extra_memory) return 0; - result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, s0, t0, s1, t1, channels, 0, STBR_TYPE_UINT32, filter, edge, STBR_COLORSPACE_SRGB, extra_memory, memory_required); + result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, s0, t0, s1, t1, channels, 0, STBR_TYPE_UINT32, filter, edge, edge, STBR_COLORSPACE_SRGB, extra_memory, memory_required); STBR_FREE(extra_memory); @@ -1718,7 +1721,7 @@ STBRDEF stbr_inline int stbr_resize_float_subpixel(const float* input_data, int if (!extra_memory) return 0; - result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, s0, t0, s1, t1, channels, 0, STBR_TYPE_FLOAT, filter, edge, STBR_COLORSPACE_SRGB, extra_memory, memory_required); + result = stbr__resize_advanced(input_data, input_w, input_h, 0, output_data, output_w, output_h, 0, s0, t0, s1, t1, channels, 0, STBR_TYPE_FLOAT, filter, edge, edge, STBR_COLORSPACE_SRGB, extra_memory, memory_required); STBR_FREE(extra_memory); @@ -1728,7 +1731,7 @@ STBRDEF stbr_inline int stbr_resize_float_subpixel(const float* input_data, int STBRDEF int stbr_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 nonpremultiplied_alpha_channel, stbr_type type, stbr_filter filter, stbr_edge edge, stbr_colorspace colorspace) + int channels, int nonpremultiplied_alpha_channel, stbr_type type, stbr_filter filter, stbr_edge edge_horizontal, stbr_edge edge_vertical, stbr_colorspace colorspace) { int result; size_t memory_required = stbr__calculate_memory(input_w, input_h, output_w, output_h, s0, t0, s1, t1, channels, filter); @@ -1737,7 +1740,7 @@ STBRDEF int stbr_resize_arbitrary(const void* input_data, int input_w, int input if (!extra_memory) return 0; - result = stbr__resize_advanced(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, nonpremultiplied_alpha_channel, type, filter, edge, colorspace, extra_memory, memory_required); + result = stbr__resize_advanced(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, nonpremultiplied_alpha_channel, type, filter, edge_horizontal, edge_vertical, colorspace, extra_memory, memory_required); STBR_FREE(extra_memory); diff --git a/tests/resample_test.cpp b/tests/resample_test.cpp index 653dfe9..ff19d13 100644 --- a/tests/resample_test.cpp +++ b/tests/resample_test.cpp @@ -140,7 +140,7 @@ int main(int argc, char** argv) printf("Average: %dms\n", average); #else - stbr_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, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_COLORSPACE_SRGB); + stbr_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, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_EDGE_CLAMP, STBR_COLORSPACE_SRGB); #endif stbi_image_free(input_data); @@ -168,7 +168,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); - stbr_resize_arbitrary(input_data, w, h, 0, output_data, out_w, out_h, 0, 0, 0, 1, 1, n, -1, STBR_TYPE_UINT8, filter, edge, colorspace); + stbr_resize_arbitrary(input_data, w, h, 0, output_data, out_w, out_h, 0, 0, 0, 1, 1, n, -1, STBR_TYPE_UINT8, filter, edge, edge, colorspace); stbi_image_free(input_data); @@ -199,7 +199,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)); - stbr_resize_arbitrary(T_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, n, -1, type, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, colorspace); + stbr_resize_arbitrary(T_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, n, -1, type, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_EDGE_CLAMP, colorspace); free(T_data); stbi_image_free(input_data); @@ -240,7 +240,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)); - stbr_resize_arbitrary(T_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, n, -1, type, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, colorspace); + stbr_resize_arbitrary(T_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, n, -1, type, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_EDGE_CLAMP, colorspace); free(T_data); stbi_image_free(input_data); @@ -302,7 +302,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)); - stbr_resize_arbitrary(input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, s1, t1, n, -1, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_COLORSPACE_SRGB); + stbr_resize_arbitrary(input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, s1, t1, n, -1, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_EDGE_CLAMP, STBR_COLORSPACE_SRGB); stbi_image_free(input_data); @@ -333,13 +333,13 @@ void test_premul(const char* file) unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char)); - stbr_resize_arbitrary(input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, n, 3, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_COLORSPACE_SRGB); + stbr_resize_arbitrary(input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, n, 3, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_EDGE_CLAMP, STBR_COLORSPACE_SRGB); char output[200]; sprintf(output, "test-output/premul-%s", file); stbi_write_png(output, new_w, new_h, n, output_data, 0); - stbr_resize_arbitrary(input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, n, -1, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_COLORSPACE_SRGB); + stbr_resize_arbitrary(input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, n, -1, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_EDGE_CLAMP, STBR_COLORSPACE_SRGB); sprintf(output, "test-output/nopremul-%s", file); stbi_write_png(output, new_w, new_h, n, output_data, 0); @@ -360,13 +360,13 @@ void test_subpixel_1() unsigned char output_data[16 * 16]; - stbr_resize_arbitrary(image, 8, 8, 0, output_data, 16, 16, 0, 0, 0, 1, 1, 1, -1, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_COLORSPACE_SRGB); + stbr_resize_arbitrary(image, 8, 8, 0, output_data, 16, 16, 0, 0, 0, 1, 1, 1, -1, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_EDGE_CLAMP, STBR_COLORSPACE_SRGB); unsigned char output_left[8 * 16]; unsigned char output_right[8 * 16]; - stbr_resize_arbitrary(image, 8, 8, 0, output_left, 8, 16, 0, 0, 0, 0.5f, 1, 1, -1, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_COLORSPACE_SRGB); - stbr_resize_arbitrary(image, 8, 8, 0, output_right, 8, 16, 0, 0.5f, 0, 1, 1, 1, -1, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_COLORSPACE_SRGB); + stbr_resize_arbitrary(image, 8, 8, 0, output_left, 8, 16, 0, 0, 0, 0.5f, 1, 1, -1, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_EDGE_CLAMP, STBR_COLORSPACE_SRGB); + stbr_resize_arbitrary(image, 8, 8, 0, output_right, 8, 16, 0, 0.5f, 0, 1, 1, 1, -1, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_EDGE_CLAMP, STBR_COLORSPACE_SRGB); for (int x = 0; x < 8; x++) { @@ -404,8 +404,8 @@ void test_subpixel_2() unsigned char output_data_1[16 * 16]; unsigned char output_data_2[16 * 16]; - stbr_resize_arbitrary(image, 8, 8, 0, output_data_1, 16, 16, 0, 0, 0, 1, 1, 1, -1, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_WRAP, STBR_COLORSPACE_SRGB); - stbr_resize_arbitrary(large_image, 32, 32, 0, output_data_2, 16, 16, 0, 0.25f, 0.25f, 0.5f, 0.5f, 1, -1, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_COLORSPACE_SRGB); + stbr_resize_arbitrary(image, 8, 8, 0, output_data_1, 16, 16, 0, 0, 0, 1, 1, 1, -1, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_WRAP, STBR_EDGE_WRAP, STBR_COLORSPACE_SRGB); + stbr_resize_arbitrary(large_image, 32, 32, 0, output_data_2, 16, 16, 0, 0.25f, 0.25f, 0.5f, 0.5f, 1, -1, STBR_TYPE_UINT8, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, STBR_EDGE_CLAMP, STBR_COLORSPACE_SRGB); for (int x = 0; x < 16; x++) { @@ -447,7 +447,7 @@ void test_subpixel_4() unsigned char output[8 * 8]; - stbr_resize_arbitrary(image, 8, 8, 0, output, 8, 8, 0, 0, 0, 1, 1, 1, -1, STBR_TYPE_UINT8, STBR_FILTER_BILINEAR, STBR_EDGE_CLAMP, STBR_COLORSPACE_LINEAR); + stbr_resize_arbitrary(image, 8, 8, 0, output, 8, 8, 0, 0, 0, 1, 1, 1, -1, STBR_TYPE_UINT8, STBR_FILTER_BILINEAR, STBR_EDGE_CLAMP, STBR_EDGE_CLAMP, STBR_COLORSPACE_LINEAR); STBR_ASSERT(memcmp(image, output, 8 * 8) == 0); }