Skip to content

Commit 8076ea9

Browse files
authored
fix draw text with alpha (#720)
* fix: correct text alpha blending
1 parent e7b1548 commit 8076ea9

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ text = ["dep:ab_glyph"]
2121
[dependencies]
2222
ab_glyph = { version = "0.2.23", default-features = false, features = ["std"], optional = true}
2323
approx = { version = "0.5", default-features = false }
24-
image = { version = "0.25.0", default-features = false }
24+
image = { version = "0.25.8", default-features = false }
2525
itertools = { version = "0.14.0", default-features = false, features = [
2626
"use_std",
2727
] }
@@ -31,7 +31,7 @@ rand = { version = "0.9.0", default-features = false, features = [
3131
"thread_rng"
3232
] }
3333
rand_distr = { version = "0.5.0", default-features = false }
34-
rayon = { version = "1.8.0", optional = true, default-features = false }
34+
rayon = { version = "1.10.0", optional = true, default-features = false }
3535
sdl2 = { version = "0.38.0", optional = true, default-features = false, features = [
3636
"bundled",
3737
] }

src/drawing/text.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,18 @@ pub fn draw_text_mut<C>(
9898
if (0..image_width).contains(&image_x) && (0..image_height).contains(&image_y) {
9999
let image_x = image_x as u32;
100100
let image_y = image_y as u32;
101-
let pixel = canvas.get_pixel(image_x, image_y);
101+
let mut pixel = canvas.get_pixel(image_x, image_y);
102102
let gv = gv.clamp(0.0, 1.0);
103-
let weighted_color = weighted_sum(pixel, color, 1.0 - gv, gv);
104-
canvas.draw_pixel(image_x, image_y, weighted_color);
103+
104+
if C::Pixel::HAS_ALPHA {
105+
let color = color.map_with_alpha(|f| f, |g| Clamp::clamp(g.into() * gv));
106+
107+
pixel.blend(&color);
108+
} else {
109+
pixel = weighted_sum(pixel, color, 1.0 - gv, gv);
110+
}
111+
112+
canvas.draw_pixel(image_x, image_y, pixel);
105113
}
106114
})
107115
});

tests/data/truth/text_alpha.png

105 KB
Loading

tests/regression.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,3 +1000,24 @@ fn test_draw_text() {
10001000
}
10011001
}
10021002
}
1003+
1004+
#[test]
1005+
#[cfg(feature = "text")]
1006+
fn test_draw_text_with_alpha() {
1007+
fn draw_text_with_alpha(img: &RgbaImage) -> RgbaImage {
1008+
let mut img = img.clone();
1009+
1010+
let font_bytes = include_bytes!("data/fonts/DejaVuSans.ttf");
1011+
let font = ab_glyph::FontRef::try_from_slice(font_bytes).unwrap();
1012+
1013+
let text = "Hello world!";
1014+
let scale = 30.0;
1015+
let (x, y) = (50, 100);
1016+
let color_text = Rgba([255, 255, 255, 85]);
1017+
imageproc::drawing::draw_text_mut(&mut img, color_text, x, y, scale, &font, text);
1018+
1019+
img
1020+
}
1021+
1022+
compare_to_truth("elephant.png", "text_alpha.png", draw_text_with_alpha);
1023+
}

0 commit comments

Comments
 (0)