From e40b7c1a295e63984ec9802a5afafba3367eb0fb Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 14 Sep 2018 19:35:29 +0100 Subject: [PATCH] Fix 8bit to float to 8bit conversion --- src/gui/icon_fetch.cpp | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/gui/icon_fetch.cpp b/src/gui/icon_fetch.cpp index 6cb43a7..71528b7 100644 --- a/src/gui/icon_fetch.cpp +++ b/src/gui/icon_fetch.cpp @@ -49,6 +49,10 @@ namespace { [[gnu::pure]] float to_srgb (float l) { + if (l <= 0.00313066844250063f) + return l * 12.92f; + else + return 1.055f * std::pow(l, 1.0f / 2.4f) - 0.055f; } void srgb_to_linear (std::vector& data, int, int) { @@ -56,17 +60,16 @@ namespace { } void linear_to_srgb (std::vector& data, int w, int h) { - assert(false); + std::transform(data.begin(), data.end(), data.begin(), &to_srgb); } std::vector bilinear_resize (int in_w, int in_h, const std::vector& data, int out_w, int out_h) { - assert(false); - return std::vector(); + return data; } std::vector rgb4_to_float (const std::vector& data, const std::vector& palette, int w, int h) { assert(w * h); - std::vector retval(w * h); + std::vector retval(w * h * 3); const int scanl_sz = scanline_size(w, 4); std::vector float_palette; @@ -82,10 +85,10 @@ namespace { const int row = y * scanl_sz; for (int x = 0; x < w / 2; ++x) { const uint8_t curr = data[row + x]; - const int dst_index_a = y * scanl_sz + x * 2 * 3; + const int dst_index_a = y * (w * 3) + x * 2 * 3; const int dst_index_b = dst_index_a + 3; - const int src_index_a = (curr bitand 0x0f) * 4; - const int src_index_b = ((curr bitand 0xf0) >> 4) * 4; + const int src_index_b = (curr bitand 0x0f) * 4; + const int src_index_a = ((curr bitand 0xf0) >> 4) * 4; retval[dst_index_a + 0] = float_palette[src_index_a + 0]; retval[dst_index_a + 1] = float_palette[src_index_a + 1]; @@ -98,11 +101,17 @@ namespace { return retval; } - std::vector float_to_rgb24 (const std::vector& data) { - std::vector retval; - retval.reserve(data.size()); - for (auto f : data) { - retval.push_back(static_cast(static_cast(f * 255.0f))); + std::vector float_to_rgb24 (const std::vector& data, int w, int h) { + const int scanl_sz = scanline_size(w, 24); + std::vector retval(scanl_sz * h); + assert(retval.size() >= data.size()); + + for (int y = 0; y < h; ++y) { + for (int x = 0; x < w; ++x) { + for (int c = 0; c < 3; ++c) { + retval[y * scanl_sz + x * 3 + c] = static_cast(static_cast(data[y * scanl_sz + x * 3 + c] * 255.0f)); + } + } } return retval; } @@ -163,10 +172,11 @@ std::vector> icon_fetch (const ConstBlock& block, int width, i std::vector resized_rgb; if (scale) { std::vector float_data = rgb4_to_float(orig_rgb, palette, in_width, in_height); - srgb_to_linear(float_data, in_width, in_height); - auto float_resized_rgb = bilinear_resize(in_width, in_height, float_data, width, height); - linear_to_srgb(float_resized_rgb, width, height); - resized_rgb = float_to_rgb24(float_resized_rgb); + //srgb_to_linear(float_data, in_width, in_height); + //auto float_resized_rgb = bilinear_resize(in_width, in_height, float_data, width, height); + //linear_to_srgb(float_resized_rgb, width, height); + //resized_rgb = float_to_rgb24(float_resized_rgb, width, height); + resized_rgb = float_to_rgb24(float_data, width, height); } else { std::swap(resized_rgb, orig_rgb);