From 11897fbf9698ad0c992a401f161b0f08bd967790 Mon Sep 17 00:00:00 2001 From: Jorge Rodriguez Date: Wed, 30 Jul 2014 00:34:25 -0700 Subject: [PATCH] More fixing shorts. --- stb_resample.h | 7 ++---- tests/resample_test.cpp | 65 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/stb_resample.h b/stb_resample.h index ac2a0c8..be08db1 100644 --- a/stb_resample.h +++ b/stb_resample.h @@ -610,9 +610,8 @@ static void stbr__check_downsample_coefficients(stbr__info* stbr_info) break; } - STBR_DEBUG_ASSERT(stbr_info->type == STBR_TYPE_UINT8); // Assert below should be 1 + 1/(2^n-1) where n is bits per int. STBR_DEBUG_ASSERT(total > 0.9f); - STBR_DEBUG_ASSERT(total <= 1.0f + 1.0f / 255); + STBR_DEBUG_ASSERT(total <= 1.0f + 1.0f / (pow(2.0f, 8.0f * stbr__type_size[stbr_info->type]) - 1)); } } #endif @@ -958,14 +957,12 @@ static void stbr__resample_vertical_upsample(stbr__info* stbr_info, int n, int i int ring_buffer_last_scanline = stbr_info->ring_buffer_last_scanline; int ring_buffer_length = stbr_info->ring_buffer_length_bytes/sizeof(float); - STBR_UNIMPLEMENTED(stbr_info->type != STBR_TYPE_UINT8); - stbr__calculate_coefficients_upsample(stbr_info, in_first_scanline, in_last_scanline, in_center_of_out, vertical_contributors, vertical_coefficients); int n0 = vertical_contributors->n0; int n1 = vertical_contributors->n1; - int output_row_index = n * stbr_info->output_stride_bytes; + int output_row_index = n * stbr_info->output_stride_bytes / stbr__type_size[type]; STBR_DEBUG_ASSERT(stbr__use_height_upsampling(stbr_info)); STBR_DEBUG_ASSERT(n0 >= in_first_scanline); diff --git a/tests/resample_test.cpp b/tests/resample_test.cpp index c4c457d..8a19de8 100644 --- a/tests/resample_test.cpp +++ b/tests/resample_test.cpp @@ -133,6 +133,39 @@ void convert_image(const F* input, T* output, int length) output[i] = (T)(((float)input[i]) * f); } +template +void test_format(const char* file, float width_percent, float height_percent, stbr_type type, stbr_colorspace colorspace) +{ + int w, h, n; + unsigned char* input_data = stbi_load(file, &w, &h, &n, 0); + + int new_w = (int)(w * width_percent); + int new_h = (int)(h * height_percent); + + T* T_data = (T*)malloc(w * h * n * sizeof(T)); + convert_image(input_data, T_data, w * h * n); + + T* output_data = (T*)malloc(new_w * new_h * n * sizeof(T)); + + size_t required = stbr_calculate_memory(w, h, new_w, new_h, n, STBR_FILTER_CATMULLROM); + void* extra_memory = malloc(required); + stbr_resize_arbitrary(T_data, w, h, 0, output_data, new_w, new_h, 0, n, type, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, colorspace, extra_memory, required); + free(extra_memory); + + free(T_data); + stbi_image_free(input_data); + + char* char_data = (char*)malloc(new_w * new_h * n * sizeof(char)); + convert_image(output_data, char_data, new_w * new_h * n); + + char output[200]; + sprintf(output, "test-output/type-%d-%d-%d-%d-%s", type, colorspace, new_w, new_h, file); + stbi_write_png(output, new_w, new_h, n, char_data, 0); + + free(char_data); + free(output_data); +} + void test_suite() { // sRGB tests @@ -175,28 +208,20 @@ void test_suite() resize_image("barbara.png", 100 / (float)i, (float)i / 100, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP, outname); } - { - int w, h, n; - unsigned char* input_data = stbi_load("barbara.png", &w, &h, &n, 0); + test_format("barbara.png", 0.5, 2.0, STBR_TYPE_UINT16, STBR_COLORSPACE_SRGB); + test_format("barbara.png", 0.5, 2.0, STBR_TYPE_UINT16, STBR_COLORSPACE_LINEAR); + test_format("barbara.png", 2.0, 0.5, STBR_TYPE_UINT16, STBR_COLORSPACE_SRGB); + test_format("barbara.png", 2.0, 0.5, STBR_TYPE_UINT16, STBR_COLORSPACE_LINEAR); - unsigned short* short_data = (unsigned short*)malloc(w * h * n * sizeof(unsigned short)); - convert_image(input_data, short_data, w * h * n); + test_format("barbara.png", 0.5, 2.0, STBR_TYPE_UINT32, STBR_COLORSPACE_SRGB); + test_format("barbara.png", 0.5, 2.0, STBR_TYPE_UINT32, STBR_COLORSPACE_LINEAR); + test_format("barbara.png", 2.0, 0.5, STBR_TYPE_UINT32, STBR_COLORSPACE_SRGB); + test_format("barbara.png", 2.0, 0.5, STBR_TYPE_UINT32, STBR_COLORSPACE_LINEAR); - unsigned short* output_data = (unsigned short*)malloc(w * h * n * sizeof(unsigned short)); - - stbr_resize_srgb_uint16(short_data, w, h, output_data, w * 2, h / 2, n, STBR_FILTER_CATMULLROM, STBR_EDGE_CLAMP); - - free(short_data); - stbi_image_free(input_data); - - char* char_data = (char*)malloc(w * h * n * sizeof(char)); - convert_image(output_data, char_data, w * h * n); - - stbi_write_png("test-output/barbara-short.png", w * 2, h / 2, n, char_data, 0); - - free(char_data); - free(output_data); - } + test_format("barbara.png", 0.5, 2.0, STBR_TYPE_FLOAT, STBR_COLORSPACE_SRGB); + test_format("barbara.png", 0.5, 2.0, STBR_TYPE_FLOAT, STBR_COLORSPACE_LINEAR); + test_format("barbara.png", 2.0, 0.5, STBR_TYPE_FLOAT, STBR_COLORSPACE_SRGB); + test_format("barbara.png", 2.0, 0.5, STBR_TYPE_FLOAT, STBR_COLORSPACE_LINEAR); }