Fix banding from linear RGB color space conversion

This commit is contained in:
Surma
2020-12-13 16:27:59 +00:00
parent c3d05e0e2d
commit 405dd1cdfa
7 changed files with 176 additions and 188 deletions

View File

@@ -5,12 +5,12 @@ extern crate wasm_bindgen;
mod utils;
use cfg_if::cfg_if;
use resize::Pixel::RGBA;
use resize::Pixel;
use resize::Type;
use wasm_bindgen::prelude::*;
mod srgb;
use srgb::Clamp;
use srgb::{linear_to_srgb, srgb_to_linear, Clamp};
cfg_if! {
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
@@ -27,14 +27,17 @@ include!("./lut.inc");
// If `with_space_conversion` is true, this function returns 2 functions that
// convert from sRGB to linear RGB and vice versa. If `with_space_conversion` is
// false, the 2 functions returned do nothing.
fn converter_funcs(with_space_conversion: bool) -> ((fn(u8) -> f32), (fn(f32) -> u8)) {
fn srgb_converter_funcs(with_space_conversion: bool) -> (fn(u8) -> f32, fn(f32) -> u8) {
if with_space_conversion {
(
|v| SRGB_TO_LINEAR_LUT[v as usize] * 255.0,
|v| (LINEAR_TO_SRGB_LUT[v as usize] * 255.0) as u8,
|v| SRGB_TO_LINEAR_LUT[v as usize],
|v| (linear_to_srgb(v) * 255.0).clamp(0.0, 255.0) as u8,
)
} else {
(|v| v as f32, |v| v as u8)
(
|v| (v as f32) / 255.0,
|v| (v * 255.0).clamp(0.0, 255.0) as u8,
)
}
}
@@ -44,21 +47,24 @@ fn converter_funcs(with_space_conversion: bool) -> ((fn(u8) -> f32), (fn(f32) ->
// false, the functions just return the channel value.
fn alpha_multiplier_funcs(
with_alpha_premultiplication: bool,
) -> ((fn(f32, u8) -> u8), (fn(u8, u8) -> f32)) {
) -> (fn(f32, f32) -> f32, fn(f32, f32) -> f32) {
if with_alpha_premultiplication {
(
|v, a| (v * (a as f32) / 255.0) as u8,
|v, a| ((v as f32) * 255.0 / (a as f32)).clamp(0.0, 255.0),
)
(|v, a| v * a, |v, a| v / a)
} else {
(|v, _a| v as u8, |v, _a| v as f32)
(|v, _a| v, |v, _a| v)
}
}
fn vec_with_len<T: Clone>(len: usize, item: T) -> Vec<T> {
let mut v: Vec<T> = Vec::with_capacity(len);
v.resize(len, item);
return v;
}
#[wasm_bindgen]
#[no_mangle]
pub fn resize(
mut input_image: Vec<u8>,
input_image: Vec<u8>,
input_width: usize,
input_height: usize,
output_width: usize,
@@ -77,44 +83,64 @@ pub fn resize(
let num_input_pixels = input_width * input_height;
let num_output_pixels = output_width * output_height;
let (to_linear, to_color_space) = converter_funcs(color_space_conversion);
let (premultiplier, demultiplier) = alpha_multiplier_funcs(premultiply);
let mut output_image: Vec<u8> = vec_with_len(num_output_pixels * 4, 0);
// If both options are false, there is no preprocessing on the pixel valus
// and we can skip the loop.
if premultiply || color_space_conversion {
for i in 0..num_input_pixels {
for j in 0..3 {
input_image[4 * i + j] =
premultiplier(to_linear(input_image[4 * i + j]), input_image[4 * i + 3]);
}
if !premultiply && !color_space_conversion {
let mut resizer = resize::new(
input_width,
input_height,
output_width,
output_height,
Pixel::RGBA,
typ,
);
resizer.resize(input_image.as_slice(), output_image.as_mut_slice());
return output_image;
}
// Otherwise, we convert to f32 images so we dont introduce
// banding through the conversions.
let (to_linear, to_srgb) = srgb_converter_funcs(color_space_conversion);
let (premultiplier, demultiplier) = alpha_multiplier_funcs(premultiply);
let mut preprocessed_input_image: Vec<f32> = Vec::with_capacity(input_image.len());
preprocessed_input_image.resize(input_image.len(), 0.0f32);
for i in 0..num_input_pixels {
for j in 0..3 {
preprocessed_input_image[4 * i + j] = premultiplier(
to_linear(input_image[4 * i + j]),
(input_image[4 * i + 3] as f32) / 255.0,
);
preprocessed_input_image[4 * i + 3] = (input_image[4 * i + 3] as f32) / 255.0;
}
}
let mut unprocessed_output_image: Vec<f32> = vec_with_len(num_output_pixels * 4, 0.0f32);
let mut resizer = resize::new(
input_width,
input_height,
output_width,
output_height,
RGBA,
Pixel::RGBAF32,
typ,
);
let mut output_image = Vec::<u8>::with_capacity(num_output_pixels * 4);
output_image.resize(num_output_pixels * 4, 0);
resizer.resize(input_image.as_slice(), output_image.as_mut_slice());
resizer.resize(
preprocessed_input_image.as_slice(),
unprocessed_output_image.as_mut_slice(),
);
if premultiply || color_space_conversion {
for i in 0..num_output_pixels {
for j in 0..3 {
// We dont need to worry about division by zero, as division by zero
// is well-defined on floats to return ±Inf. ±Inf is converted to 0
// when casting to integers.
output_image[4 * i + j] = to_color_space(demultiplier(
output_image[4 * i + j],
output_image[4 * i + 3],
));
}
for i in 0..num_output_pixels {
for j in 0..3 {
output_image[4 * i + j] = to_srgb(demultiplier(
unprocessed_output_image[4 * i + j],
unprocessed_output_image[4 * i + 3],
));
}
output_image[4 * i + 3] =
(unprocessed_output_image[4 * i + 3] * 255.0).clamp(0.0, 255.0) as u8;
}
return output_image;