From ca241daefaa9eace0053843bb28e0ae806837db1 Mon Sep 17 00:00:00 2001 From: Jorge Rodriguez Date: Tue, 9 Sep 2014 12:16:23 -0700 Subject: [PATCH] Use rounding to try to preserve the original value. Fix test case. --- stb_image_resize.h | 16 ++++++++-------- tests/resample_test.cpp | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/stb_image_resize.h b/stb_image_resize.h index 2a6013a..f0892cd 100644 --- a/stb_image_resize.h +++ b/stb_image_resize.h @@ -1210,7 +1210,7 @@ static void stbir__encode_scanline(stbir__info* stbir_info, int num_pixels, void 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); + ((unsigned char*)output_buffer)[output_pixel_index + n] = (unsigned char)(round(stbir__saturate(encode_buffer[encode_pixel_index + n]) * 255)); } break; @@ -1225,7 +1225,7 @@ static void stbir__encode_scanline(stbir__info* stbir_info, int num_pixels, void ((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); + ((unsigned char*)output_buffer)[output_pixel_index + alpha_channel] = (unsigned char)(round(stbir__saturate(encode_buffer[encode_pixel_index + alpha_channel]) * 255)); } break; @@ -1236,7 +1236,7 @@ static void stbir__encode_scanline(stbir__info* stbir_info, int num_pixels, void 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); + ((unsigned short*)output_buffer)[output_pixel_index + n] = (unsigned short)(round(stbir__saturate(encode_buffer[encode_pixel_index + n]) * 65535)); } break; @@ -1247,10 +1247,10 @@ static void stbir__encode_scanline(stbir__info* stbir_info, int num_pixels, void int encode_pixel_index = x*channels; 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); + ((unsigned short*)output_buffer)[output_pixel_index + n] = (unsigned short)(round(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); + ((unsigned short*)output_buffer)[output_pixel_index + alpha_channel] = (unsigned short)(round(stbir__saturate(encode_buffer[encode_pixel_index + alpha_channel]) * 65535)); } break; @@ -1262,7 +1262,7 @@ static void stbir__encode_scanline(stbir__info* stbir_info, int num_pixels, void 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); + ((unsigned int*)output_buffer)[output_pixel_index + n] = (unsigned int)(round(((double)stbir__saturate(encode_buffer[encode_pixel_index + n])) * 4294967295)); } break; @@ -1273,10 +1273,10 @@ static void stbir__encode_scanline(stbir__info* stbir_info, int num_pixels, void int encode_pixel_index = x*channels; 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); + ((unsigned int*)output_buffer)[output_pixel_index + n] = (unsigned int)(round(((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); + ((unsigned int*)output_buffer)[output_pixel_index + alpha_channel] = (unsigned int)(round(((double)stbir__saturate(encode_buffer[encode_pixel_index + alpha_channel])) * 4294967295)); } break; diff --git a/tests/resample_test.cpp b/tests/resample_test.cpp index 9fb6593..469fa05 100644 --- a/tests/resample_test.cpp +++ b/tests/resample_test.cpp @@ -563,7 +563,7 @@ void verify_box(void) STBIR_ASSERT(output44[j][i] == ((n+2)>>2)); t += n; } - STBIR_ASSERT(output11[j][i] == ((t+32)>>6)); + STBIR_ASSERT(output11[0][0] == ((t+32)>>6)); } void verify_filter_normalized(stbir_filter filter, unsigned char* output, int output_size)