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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ text = ["dep:ab_glyph"]
[dependencies]
ab_glyph = { version = "0.2.23", default-features = false, features = ["std"], optional = true}
approx = { version = "0.5", default-features = false }
image = { version = "0.25.0", default-features = false }
image = { version = "0.25.8", default-features = false }
itertools = { version = "0.14.0", default-features = false, features = [
"use_std",
] }
Expand All @@ -31,7 +31,7 @@ rand = { version = "0.9.0", default-features = false, features = [
"thread_rng"
] }
rand_distr = { version = "0.5.0", default-features = false }
rayon = { version = "1.8.0", optional = true, default-features = false }
rayon = { version = "1.10.0", optional = true, default-features = false }
sdl2 = { version = "0.38.0", optional = true, default-features = false, features = [
"bundled",
] }
Expand Down
14 changes: 11 additions & 3 deletions src/drawing/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,18 @@ pub fn draw_text_mut<C>(
if (0..image_width).contains(&image_x) && (0..image_height).contains(&image_y) {
let image_x = image_x as u32;
let image_y = image_y as u32;
let pixel = canvas.get_pixel(image_x, image_y);
let mut pixel = canvas.get_pixel(image_x, image_y);
Comment thread
cospectrum marked this conversation as resolved.
let gv = gv.clamp(0.0, 1.0);
let weighted_color = weighted_sum(pixel, color, 1.0 - gv, gv);
canvas.draw_pixel(image_x, image_y, weighted_color);

if C::Pixel::HAS_ALPHA {
let color = color.map_with_alpha(|f| f, |g| Clamp::clamp(g.into() * gv));

pixel.blend(&color);
} else {
pixel = weighted_sum(pixel, color, 1.0 - gv, gv);
}

canvas.draw_pixel(image_x, image_y, pixel);
}
})
});
Expand Down
Binary file added tests/data/truth/text_alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,3 +1000,24 @@ fn test_draw_text() {
}
}
}

#[test]
#[cfg(feature = "text")]
fn test_draw_text_with_alpha() {
fn draw_text_with_alpha(img: &RgbaImage) -> RgbaImage {
let mut img = img.clone();

let font_bytes = include_bytes!("data/fonts/DejaVuSans.ttf");
let font = ab_glyph::FontRef::try_from_slice(font_bytes).unwrap();

let text = "Hello world!";
let scale = 30.0;
let (x, y) = (50, 100);
let color_text = Rgba([255, 255, 255, 85]);
imageproc::drawing::draw_text_mut(&mut img, color_text, x, y, scale, &font, text);

img
}

compare_to_truth("elephant.png", "text_alpha.png", draw_text_with_alpha);
}