Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions crates/maple-render-core/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ impl Default for TextOptions {
#[derive(Clone)]
pub struct Input {
image: RgbaImage,
opaque: bool,
pub layer: u8,
pub xs: f64,
pub ys: f64,
Expand All @@ -44,6 +45,7 @@ impl Default for Input {
fn default() -> Self {
Input {
image: RgbaImage::new(1, 1),
opaque: false,
layer: 1,
xs: 1.0,
ys: 1.0,
Expand Down Expand Up @@ -75,17 +77,19 @@ impl Input {
}

let img = image::open(path)?;
let opaque = !img.color().has_alpha();
let rgba = img.to_rgba8();

let mut input = Input { image: rgba, layer, ..Default::default() };
let mut input = Input { image: rgba, opaque, layer, ..Default::default() };

input.compute_scale_params();

Ok(input)
}

pub fn from_image(image: RgbaImage, layer: u8) -> Self {
let mut input = Input { image, layer, ..Default::default() };
let opaque = image.pixels().all(|pixel| pixel[3] == 255);
let mut input = Input { image, opaque, layer, ..Default::default() };
input.compute_scale_params();
input
}
Expand Down Expand Up @@ -119,7 +123,11 @@ impl Input {

draw_text_mut(&mut image, options.color, x, y, scale, &font, text);

Ok(Input::from_image(image, layer))
let opaque = options.color[3] == 255 && options.background[3] == 255;
let mut input = Input { image, opaque, layer, ..Default::default() };
input.compute_scale_params();

Ok(input)
}

fn compute_scale_params(&mut self) {
Expand All @@ -146,9 +154,14 @@ impl Input {
}

pub fn get_mut(&mut self) -> &mut RgbaImage {
self.opaque = false;
&mut self.image
}

pub fn is_opaque(&self) -> bool {
self.opaque
}

pub fn width(&self) -> u32 {
self.image.width()
}
Expand Down Expand Up @@ -231,3 +244,38 @@ impl std::ops::IndexMut<usize> for Inputs {
&mut self.data[index]
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn opacity_is_detected_and_invalidated_by_mutable_access() {
let image = RgbaImage::from_pixel(2, 2, Rgba([10, 20, 30, 255]));
let mut input = Input::from_image(image, 1);
assert!(input.is_opaque());

input.get_mut().put_pixel(0, 0, Rgba([10, 20, 30, 255]));
assert!(!input.is_opaque());

let image = RgbaImage::from_pixel(2, 2, Rgba([10, 20, 30, 254]));
assert!(!Input::from_image(image, 1).is_opaque());
}

#[test]
fn text_opacity_follows_foreground_and_background_alpha() {
let font = include_bytes!("../../../fonts/DejaVuSans-Bold.ttf");
let opaque = Input::from_text("opaque", 1, &TextOptions::default(), font)
.expect("render opaque text");
assert!(opaque.is_opaque());

for options in [
TextOptions { color: Rgba([0, 0, 0, 254]), ..Default::default() },
TextOptions { background: Rgba([255, 255, 255, 254]), ..Default::default() },
] {
let transparent =
Input::from_text("transparent", 1, &options, font).expect("render text");
assert!(!transparent.is_opaque());
}
}
}
178 changes: 178 additions & 0 deletions crates/maple-render-core/src/pixer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,157 @@ pub fn sample_linear(img: &RgbaImage, x: f64, y: f64) -> Pixer {
}
}

/// Bilinear interpolation in premultiplied-alpha space.
///
/// The sampled compositor combines several neighboring samples before
/// converting back to straight alpha. Keeping RGB premultiplied here avoids a
/// divide followed immediately by a multiply for every tap.
#[inline(always)]
pub(crate) fn sample_linear_premultiplied(img: &RgbaImage, x: f64, y: f64) -> Pixer {
let xx = x.floor() as i32;
let yy = y.floor() as i32;
let fx = x - xx as f64;
let fy = y - yy as f64;

let w00 = ((1.0 - fx) * (1.0 - fy)) as f32;
let w10 = (fx * (1.0 - fy)) as f32;
let w01 = ((1.0 - fx) * fy) as f32;
let w11 = (fx * fy) as f32;

let w = img.width() as i32;
let h = img.height() as i32;

if xx >= 0 && yy >= 0 && xx + 1 < w && yy + 1 < h {
let raw = img.as_raw();
let row_stride = w as usize * 4;

let i00 = yy as usize * row_stride + xx as usize * 4;
let i10 = i00 + 4;
let i01 = i00 + row_stride;
let i11 = i01 + 4;

let a00 = raw[i00 + 3] as f32;
let a10 = raw[i10 + 3] as f32;
let a01 = raw[i01 + 3] as f32;
let a11 = raw[i11 + 3] as f32;

return Pixer {
r: w00 * raw[i00] as f32 * a00
+ w10 * raw[i10] as f32 * a10
+ w01 * raw[i01] as f32 * a01
+ w11 * raw[i11] as f32 * a11,
g: w00 * raw[i00 + 1] as f32 * a00
+ w10 * raw[i10 + 1] as f32 * a10
+ w01 * raw[i01 + 1] as f32 * a01
+ w11 * raw[i11 + 1] as f32 * a11,
b: w00 * raw[i00 + 2] as f32 * a00
+ w10 * raw[i10 + 2] as f32 * a10
+ w01 * raw[i01 + 2] as f32 * a01
+ w11 * raw[i11 + 2] as f32 * a11,
a: w00 * a00 + w10 * a10 + w01 * a01 + w11 * a11,
};
}

let p00 = safe_pixel(img, xx, yy);
let p10 = safe_pixel(img, xx + 1, yy);
let p01 = safe_pixel(img, xx, yy + 1);
let p11 = safe_pixel(img, xx + 1, yy + 1);

let a00 = p00[3] as f32;
let a10 = p10[3] as f32;
let a01 = p01[3] as f32;
let a11 = p11[3] as f32;

Pixer {
r: w00 * p00[0] as f32 * a00
+ w10 * p10[0] as f32 * a10
+ w01 * p01[0] as f32 * a01
+ w11 * p11[0] as f32 * a11,
g: w00 * p00[1] as f32 * a00
+ w10 * p10[1] as f32 * a10
+ w01 * p01[1] as f32 * a01
+ w11 * p11[1] as f32 * a11,
b: w00 * p00[2] as f32 * a00
+ w10 * p10[2] as f32 * a10
+ w01 * p01[2] as f32 * a01
+ w11 * p11[2] as f32 * a11,
a: w00 * a00 + w10 * a10 + w01 * a01 + w11 * a11,
}
}

/// Bilinear interpolation for an image known to contain only opaque pixels.
///
/// This preserves the general sampler's floating-point operation order while
/// replacing four alpha loads with the known value `255`.
#[inline(always)]
pub(crate) fn sample_linear_opaque(img: &RgbaImage, x: f64, y: f64) -> Pixer {
let xx = x.floor() as i32;
let yy = y.floor() as i32;
let fx = x - xx as f64;
let fy = y - yy as f64;

let w00 = ((1.0 - fx) * (1.0 - fy)) as f32;
let w10 = (fx * (1.0 - fy)) as f32;
let w01 = ((1.0 - fx) * fy) as f32;
let w11 = (fx * fy) as f32;

let w = img.width() as i32;
let h = img.height() as i32;

if xx >= 0 && yy >= 0 && xx + 1 < w && yy + 1 < h {
let raw = img.as_raw();
let row_stride = w as usize * 4;

let i00 = yy as usize * row_stride + xx as usize * 4;
let i10 = i00 + 4;
let i01 = i00 + row_stride;
let i11 = i01 + 4;

let alpha = 255.0;

return Pixer {
r: w00 * raw[i00] as f32 * alpha
+ w10 * raw[i10] as f32 * alpha
+ w01 * raw[i01] as f32 * alpha
+ w11 * raw[i11] as f32 * alpha,
g: w00 * raw[i00 + 1] as f32 * alpha
+ w10 * raw[i10 + 1] as f32 * alpha
+ w01 * raw[i01 + 1] as f32 * alpha
+ w11 * raw[i11 + 1] as f32 * alpha,
b: w00 * raw[i00 + 2] as f32 * alpha
+ w10 * raw[i10 + 2] as f32 * alpha
+ w01 * raw[i01 + 2] as f32 * alpha
+ w11 * raw[i11 + 2] as f32 * alpha,
a: w00 * alpha + w10 * alpha + w01 * alpha + w11 * alpha,
};
}

let p00 = safe_pixel(img, xx, yy);
let p10 = safe_pixel(img, xx + 1, yy);
let p01 = safe_pixel(img, xx, yy + 1);
let p11 = safe_pixel(img, xx + 1, yy + 1);
let a00 = p00[3] as f32;
let a10 = p10[3] as f32;
let a01 = p01[3] as f32;
let a11 = p11[3] as f32;

Pixer {
r: w00 * p00[0] as f32 * a00
+ w10 * p10[0] as f32 * a10
+ w01 * p01[0] as f32 * a01
+ w11 * p11[0] as f32 * a11,
g: w00 * p00[1] as f32 * a00
+ w10 * p10[1] as f32 * a10
+ w01 * p01[1] as f32 * a01
+ w11 * p11[1] as f32 * a11,
b: w00 * p00[2] as f32 * a00
+ w10 * p10[2] as f32 * a10
+ w01 * p01[2] as f32 * a01
+ w11 * p11[2] as f32 * a11,
a: w00 * a00 + w10 * a10 + w01 * a01 + w11 * a11,
}
}

pub fn sample_weakly(img: &RgbaImage, x: f64, y: f64) -> Pixer {
Pixer::from_rgba(&safe_pixel(img, x as i32, y as i32))
}
Expand Down Expand Up @@ -259,4 +410,31 @@ mod tests {
assert_eq!(clamp_u8(128.0), 128);
assert_eq!(clamp_u8(300.0), 255);
}

#[test]
fn opaque_sampler_matches_general_premultiplied_sampler() {
let mut image = RgbaImage::new(3, 2);
for (i, pixel) in image.pixels_mut().enumerate() {
let value = u8::try_from(i * 31).expect("test value fits in u8");
*pixel = Rgba([value, value.wrapping_add(17), value.wrapping_add(83), 255]);
}

for (x, y) in [
(-1.25, -0.5),
(-0.25, 0.25),
(0.0, 0.0),
(0.3, 0.7),
(1.5, 0.25),
(2.0, 1.0),
(2.75, 1.75),
(4.0, 4.0),
] {
let general = sample_linear_premultiplied(&image, x, y);
let opaque = sample_linear_opaque(&image, x, y);
assert_eq!(general.r.to_bits(), opaque.r.to_bits());
assert_eq!(general.g.to_bits(), opaque.g.to_bits());
assert_eq!(general.b.to_bits(), opaque.b.to_bits());
assert_eq!(general.a.to_bits(), opaque.a.to_bits());
}
}
}
Loading
Loading