Skip to content

Commit

Permalink
refactor: move line drawing to Line struct
Browse files Browse the repository at this point in the history
  • Loading branch information
loiccoyle committed Aug 18, 2024
1 parent be370a7 commit 66c5e6b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
21 changes: 21 additions & 0 deletions src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,25 @@ impl Line {
.fold(0.0, |acc, &pixel| acc + (pixel.0[0] as f64))
/ (255. * self.len() as f64)
}

/// Draw the [`Line`] on the image, with alpha compositing.
///
/// # Arguments:
///
/// * `image`: the [`image::ImageBuffer`] to draw the line on, should be single channel.
/// * `line_opacity`: the opacity of the line.
/// * `line_color`: the grey scale color of the line.
pub fn draw(
&self,
image: &mut image::ImageBuffer<image::Luma<u8>, Vec<u8>>,
line_opacity: f64,
line_color: f64,
) {
self.zip().for_each(|(x, y)| {
let pixel = image.get_pixel_mut(*x, *y);
pixel.0[0] = ((1. - line_opacity) * pixel.0[0] as f64 + line_color)
.round()
.min(255.0) as u8;
});
}
}
20 changes: 4 additions & 16 deletions src/pather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ impl Pather {
.with_message("Computing blueprint");

let line_color = 255. * self.config.yarn.opacity;
let opacity_factor = 1. - self.config.yarn.opacity;

let mut last_peg = start_peg;
let mut last_last_peg = last_peg;
Expand Down Expand Up @@ -287,12 +286,7 @@ impl Pather {
last_last_peg = last_peg;
last_peg = min_peg;

min_line.zip().for_each(|(x, y)| {
let pixel = work_img.get_pixel_mut(*x, *y);
pixel.0[0] = ((opacity_factor) * pixel.0[0] as f64 + line_color)
.round()
.min(255.0) as u8;
});
min_line.draw(&mut work_img, self.config.yarn.opacity, line_color);
}
});

Expand All @@ -318,7 +312,6 @@ impl Pather {
.with_message("Computing blueprint");

let line_color = 255. * self.config.yarn.opacity;
let opacity_factor = 1. - self.config.yarn.opacity;

let mut beam = vec![BeamState {
peg_order: vec![start_peg],
Expand Down Expand Up @@ -382,13 +375,8 @@ impl Pather {
.into_iter()
.take(self.config.beam_width)
.map(|(loss, peg_i, line, beam_state)| {
let mut new_image = beam_state.image.clone();
line.zip().for_each(|(x, y)| {
let pixel = new_image.get_pixel_mut(*x, *y);
pixel.0[0] = ((opacity_factor) * pixel.0[0] as f64 + line_color)
.round()
.min(255.0) as u8;
});
let mut new_img = beam_state.image.clone();
line.draw(&mut new_img, self.config.yarn.opacity, line_color);

BeamState {
peg_order: beam_state
Expand All @@ -398,7 +386,7 @@ impl Pather {
.chain(std::iter::once(peg_i))
.collect(),
loss: beam_state.loss + loss,
image: new_image,
image: new_img,
}
})
.collect();
Expand Down

0 comments on commit 66c5e6b

Please sign in to comment.