Fix 8bit to float to 8bit conversion

This commit is contained in:
King_DuckZ 2018-09-14 19:35:29 +01:00
parent 5b20c5df29
commit e40b7c1a29

View file

@ -49,6 +49,10 @@ namespace {
[[gnu::pure]] [[gnu::pure]]
float to_srgb (float l) { 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<float>& data, int, int) { void srgb_to_linear (std::vector<float>& data, int, int) {
@ -56,17 +60,16 @@ namespace {
} }
void linear_to_srgb (std::vector<float>& data, int w, int h) { void linear_to_srgb (std::vector<float>& data, int w, int h) {
assert(false); std::transform(data.begin(), data.end(), data.begin(), &to_srgb);
} }
std::vector<float> bilinear_resize (int in_w, int in_h, const std::vector<float>& data, int out_w, int out_h) { std::vector<float> bilinear_resize (int in_w, int in_h, const std::vector<float>& data, int out_w, int out_h) {
assert(false); return data;
return std::vector<float>();
} }
std::vector<float> rgb4_to_float (const std::vector<char>& data, const std::vector<uint8_t>& palette, int w, int h) { std::vector<float> rgb4_to_float (const std::vector<char>& data, const std::vector<uint8_t>& palette, int w, int h) {
assert(w * h); assert(w * h);
std::vector<float> retval(w * h); std::vector<float> retval(w * h * 3);
const int scanl_sz = scanline_size(w, 4); const int scanl_sz = scanline_size(w, 4);
std::vector<float> float_palette; std::vector<float> float_palette;
@ -82,10 +85,10 @@ namespace {
const int row = y * scanl_sz; const int row = y * scanl_sz;
for (int x = 0; x < w / 2; ++x) { for (int x = 0; x < w / 2; ++x) {
const uint8_t curr = data[row + 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 dst_index_b = dst_index_a + 3;
const int src_index_a = (curr bitand 0x0f) * 4; const int src_index_b = (curr bitand 0x0f) * 4;
const int src_index_b = ((curr bitand 0xf0) >> 4) * 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 + 0] = float_palette[src_index_a + 0];
retval[dst_index_a + 1] = float_palette[src_index_a + 1]; retval[dst_index_a + 1] = float_palette[src_index_a + 1];
@ -98,11 +101,17 @@ namespace {
return retval; return retval;
} }
std::vector<char> float_to_rgb24 (const std::vector<float>& data) { std::vector<char> float_to_rgb24 (const std::vector<float>& data, int w, int h) {
std::vector<char> retval; const int scanl_sz = scanline_size(w, 24);
retval.reserve(data.size()); std::vector<char> retval(scanl_sz * h);
for (auto f : data) { assert(retval.size() >= data.size());
retval.push_back(static_cast<char>(static_cast<uint8_t>(f * 255.0f)));
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<char>(static_cast<uint8_t>(data[y * scanl_sz + x * 3 + c] * 255.0f));
}
}
} }
return retval; return retval;
} }
@ -163,10 +172,11 @@ std::vector<std::vector<char>> icon_fetch (const ConstBlock& block, int width, i
std::vector<char> resized_rgb; std::vector<char> resized_rgb;
if (scale) { if (scale) {
std::vector<float> float_data = rgb4_to_float(orig_rgb, palette, in_width, in_height); std::vector<float> float_data = rgb4_to_float(orig_rgb, palette, in_width, in_height);
srgb_to_linear(float_data, 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); //auto float_resized_rgb = bilinear_resize(in_width, in_height, float_data, width, height);
linear_to_srgb(float_resized_rgb, width, height); //linear_to_srgb(float_resized_rgb, width, height);
resized_rgb = float_to_rgb24(float_resized_rgb); //resized_rgb = float_to_rgb24(float_resized_rgb, width, height);
resized_rgb = float_to_rgb24(float_data, width, height);
} }
else { else {
std::swap(resized_rgb, orig_rgb); std::swap(resized_rgb, orig_rgb);