Try to keep integer precision by briefly casting to double while decoding and encoding.
This commit is contained in:
parent
11897fbf96
commit
6625259959
@ -715,12 +715,12 @@ static void stbr__decode_scanline(stbr__info* stbr_info, int n)
|
|||||||
|
|
||||||
case STBR__DECODE(STBR_TYPE_UINT32, STBR_COLORSPACE_LINEAR):
|
case STBR__DECODE(STBR_TYPE_UINT32, STBR_COLORSPACE_LINEAR):
|
||||||
for (int n = 0; n < channels; n++)
|
for (int n = 0; n < channels; n++)
|
||||||
decode_buffer[decode_texel_index + n] = ((float)((const unsigned int*)input_data)[input_texel_index + n]) / 4294967295;
|
decode_buffer[decode_texel_index + n] = (float)(((double)((const unsigned int*)input_data)[input_texel_index + n]) / 4294967295);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case STBR__DECODE(STBR_TYPE_UINT32, STBR_COLORSPACE_SRGB):
|
case STBR__DECODE(STBR_TYPE_UINT32, STBR_COLORSPACE_SRGB):
|
||||||
for (int n = 0; n < channels; n++)
|
for (int n = 0; n < channels; n++)
|
||||||
decode_buffer[decode_texel_index + n] = stbr__srgb_to_linear(((float)((const unsigned int*)input_data)[input_texel_index + n]) / 4294967295);
|
decode_buffer[decode_texel_index + n] = stbr__srgb_to_linear((float)(((double)((const unsigned int*)input_data)[input_texel_index + n]) / 4294967295));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case STBR__DECODE(STBR_TYPE_FLOAT, STBR_COLORSPACE_LINEAR):
|
case STBR__DECODE(STBR_TYPE_FLOAT, STBR_COLORSPACE_LINEAR):
|
||||||
@ -913,12 +913,12 @@ static stbr_inline void stbr__encode_scanline(void* output_buffer, int output_te
|
|||||||
|
|
||||||
case STBR__DECODE(STBR_TYPE_UINT32, STBR_COLORSPACE_LINEAR):
|
case STBR__DECODE(STBR_TYPE_UINT32, STBR_COLORSPACE_LINEAR):
|
||||||
for (int n = 0; n < channels; n++)
|
for (int n = 0; n < channels; n++)
|
||||||
((unsigned int*)output_buffer)[output_texel_index + n] = (unsigned int)(stbr__saturate(encode_buffer[encode_texel_index + n]) * 4294967295);
|
((unsigned int*)output_buffer)[output_texel_index + n] = (unsigned int)(((double)stbr__saturate(encode_buffer[encode_texel_index + n])) * 4294967295);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case STBR__DECODE(STBR_TYPE_UINT32, STBR_COLORSPACE_SRGB):
|
case STBR__DECODE(STBR_TYPE_UINT32, STBR_COLORSPACE_SRGB):
|
||||||
for (int n = 0; n < channels; n++)
|
for (int n = 0; n < channels; n++)
|
||||||
((unsigned int*)output_buffer)[output_texel_index + n] = (unsigned int)(stbr__linear_to_srgb(stbr__saturate(encode_buffer[encode_texel_index + n])) * 4294967295);
|
((unsigned int*)output_buffer)[output_texel_index + n] = (unsigned int)(((double)stbr__linear_to_srgb(stbr__saturate(encode_buffer[encode_texel_index + n]))) * 4294967295);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case STBR__DECODE(STBR_TYPE_FLOAT, STBR_COLORSPACE_LINEAR):
|
case STBR__DECODE(STBR_TYPE_FLOAT, STBR_COLORSPACE_LINEAR):
|
||||||
|
@ -128,9 +128,9 @@ void resize_image(const char* filename, float width_percent, float height_percen
|
|||||||
template <typename F, typename T>
|
template <typename F, typename T>
|
||||||
void convert_image(const F* input, T* output, int length)
|
void convert_image(const F* input, T* output, int length)
|
||||||
{
|
{
|
||||||
float f = (pow(2.0f, 8.0f * sizeof(T)) - 1) / (pow(2.0f, 8.0f * sizeof(F)) - 1);
|
double f = (pow(2.0, 8.0 * sizeof(T)) - 1) / (pow(2.0, 8.0 * sizeof(F)) - 1);
|
||||||
for (int i = 0; i < length; i++)
|
for (int i = 0; i < length; i++)
|
||||||
output[i] = (T)(((float)input[i]) * f);
|
output[i] = (T)(((double)input[i]) * f);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -155,8 +155,8 @@ void test_format(const char* file, float width_percent, float height_percent, st
|
|||||||
free(T_data);
|
free(T_data);
|
||||||
stbi_image_free(input_data);
|
stbi_image_free(input_data);
|
||||||
|
|
||||||
char* char_data = (char*)malloc(new_w * new_h * n * sizeof(char));
|
unsigned char* char_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(char));
|
||||||
convert_image<T, char>(output_data, char_data, new_w * new_h * n);
|
convert_image<T, unsigned char>(output_data, char_data, new_w * new_h * n);
|
||||||
|
|
||||||
char output[200];
|
char output[200];
|
||||||
sprintf(output, "test-output/type-%d-%d-%d-%d-%s", type, colorspace, new_w, new_h, file);
|
sprintf(output, "test-output/type-%d-%d-%d-%d-%s", type, colorspace, new_w, new_h, file);
|
||||||
|
Loading…
Reference in New Issue
Block a user