Skip to content

fix draw text with alpha #720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ jobs:
# https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability
strategy:
matrix:
msrv: ["1.80.1"] # Don't forget to update the `rust-version` in Cargo.toml as well
msrv: ["1.81.0"] # Don't forget to update the `rust-version` in Cargo.toml as well
name: ubuntu / ${{ matrix.msrv }}
steps:
- uses: actions/checkout@v4
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "imageproc"
version = "0.26.0"
version = "0.26.1"
authors = ["theotherphil"]
# note: when changed, also update `msrv` in `.github/workflows/check.yml`
rust-version = "1.80.1"
rust-version = "1.81.0"
edition = "2021"
license = "MIT"
description = "Image processing operations"
Expand All @@ -20,7 +20,7 @@ rayon = ["dep:rayon", "image/rayon"]
[dependencies]
ab_glyph = { version = "0.2.23", default-features = false, features = ["std"] }
approx = { version = "0.5", default-features = false }
image = { version = "0.25.0", default-features = false }
image = { version = "0.25.6", default-features = false }
itertools = { version = "0.13.0", default-features = false, features = [
"use_std",
] }
Expand All @@ -31,7 +31,7 @@ rand = { version = "0.8.5", default-features = false, features = [
"std_rng",
] }
rand_distr = { version = "0.4.3", 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.36", optional = true, default-features = false, features = [
"bundled",
] }
Expand Down
1 change: 1 addition & 0 deletions src/contrast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ fn histogram_lut(source_histc: &[u32; 256], target_histc: &[u32; 256]) -> [usize
lut
}

#[cfg(not(miri))]
#[cfg(test)]
mod tests {
use super::*;
Expand Down
17 changes: 14 additions & 3 deletions src/drawing/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ pub fn draw_text_mut<C>(
let image_width = canvas.width() as i32;
let image_height = canvas.height() as i32;

let has_alpha = matches!(C::Pixel::COLOR_MODEL, "RGBA" | "YA");

layout_glyphs(scale, font, text, |g, bb| {
let x_shift = x + bb.min.x.round() as i32;
let y_shift = y + bb.min.y.round() as i32;
Expand All @@ -98,10 +100,19 @@ 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);
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 has_alpha {
let mut color = color;
color.apply_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
2 changes: 1 addition & 1 deletion src/template_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ mod methods {
}

fn square_sum(input: &GrayImage) -> f32 {
input.iter().map(|&x| (x as f32 * x as f32)).sum()
input.iter().map(|&x| x as f32 * x as f32).sum()
}
fn mult_square_sum(a: &GrayImage, b: &GrayImage) -> f32 {
a.iter()
Expand Down
Loading