Skip to content

Commit

Permalink
Merge pull request #254 from B-LechCode/add_image_scaling
Browse files Browse the repository at this point in the history
Adding gray value scaling
  • Loading branch information
woelper authored Dec 22, 2023
2 parents c1cd86f + 4598dd5 commit e00f246
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
51 changes: 49 additions & 2 deletions src/image_editing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub enum ImageOperation {
GradientMap(Vec<GradientStop>),
Exposure(i32),
Equalize((i32, i32)),
ScaleImageMinMax,
Mult([u8; 3]),
Add([u8; 3]),
Fill([u8; 4]),
Expand All @@ -120,7 +121,7 @@ pub enum ImageOperation {
Invert,
Blur(u8),
MMult,
MDiv,
MDiv,
Resize {
dimensions: (u32, u32),
aspect: bool,
Expand Down Expand Up @@ -158,6 +159,7 @@ impl fmt::Display for ImageOperation {
Self::GradientMap { .. } => write!(f, "🗠 Gradient Map"),
Self::Expression(_) => write!(f, "{FUNCTION} Expression"),
Self::MMult => write!(f, "✖ Multiply with alpha"),
Self::ScaleImageMinMax=> write!(f, "\u{2195} Scale image min max"),
Self::MDiv => write!(f, "➗ Divide by alpha"),
Self::LUT(_) => write!(f, "{FILM_STRIP} Apply Color LUT"),
// _ => write!(f, "Not implemented Display"),
Expand All @@ -176,6 +178,7 @@ impl ImageOperation {
Self::Flip(_) => false,
Self::ChromaticAberration(_) => false,
Self::LUT(_) => false,
Self::ScaleImageMinMax => false,
_ => true,
}
}
Expand Down Expand Up @@ -814,6 +817,46 @@ impl ImageOperation {
}
*img = image::imageops::flip_horizontal(img);
}
Self::ScaleImageMinMax =>
{
//Step 0: Get color channel min and max values
let mut min = 255u8;
let mut max = 0u8;

img.chunks_mut(4).for_each(|px| {
min = std::cmp::min(min, px[0]);
min = std::cmp::min(min, px[1]);
min = std::cmp::min(min, px[2]);

max = std::cmp::max(max, px[0]);
max = std::cmp::max(max, px[1]);
max = std::cmp::max(max, px[2]);
});
let min_f = min as f64;
let max_f = max as f64;


//Step 1: Don't do zero division
if min!=max {
//Step 2: Create 8-Bit LUT
let mut lut: [u8; 256] =[0u8;256];

for n in min as usize..=max as usize {
let g = n as f64;
lut[n] = (255.0*(g-min_f)/(max_f-min_f)) as u8;
}

//Step 3: Apply 8-Bit LUT
img

.par_chunks_mut(4)
.for_each(|px| {
px[0] = lut[px[0] as usize];
px[1] = lut[px[1] as usize];
px[2] = lut[px[2] as usize];
});
}
}
Self::ChromaticAberration(amt) => {
let center = (img.width() as i32 / 2, img.height() as i32 / 2);
let img_c = img.clone();
Expand Down Expand Up @@ -1019,6 +1062,8 @@ pub fn builtin_luts() -> HashMap<String, Vec<u8>> {
);
luts
}


pub fn process_pixels(buffer: &mut RgbaImage, operators: &Vec<ImageOperation>) {
// use pulp::Arch;
// let arch = Arch::new();
Expand All @@ -1028,7 +1073,9 @@ pub fn process_pixels(buffer: &mut RgbaImage, operators: &Vec<ImageOperation>) {
// *x = 12 as u8;
// }
// });




buffer
// .chunks_mut(4)
.par_chunks_mut(4)
Expand Down
3 changes: 2 additions & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ pub fn edit_ui(app: &mut App, ctx: &Context, state: &mut OculanteState, gfx: &mu
ImageOperation::Desaturate(0),
ImageOperation::LUT("Lomography Redscale 100".into()),
ImageOperation::Equalize((0, 255)),
ImageOperation::ScaleImageMinMax,
ImageOperation::Posterize(8),
ImageOperation::ChannelSwap((Channel::Red, Channel::Red)),
ImageOperation::Rotate(90),
Expand All @@ -708,7 +709,7 @@ pub fn edit_ui(app: &mut App, ctx: &Context, state: &mut OculanteState, gfx: &mu
ImageOperation::Fill([255, 255, 255, 255]),
ImageOperation::Blur(0),
ImageOperation::GradientMap(vec![GradientStop::new(0, [155,33,180]), GradientStop::new(128, [255,83,0]),GradientStop::new(255, [224,255,0])]),
ImageOperation::MMult,
ImageOperation::MMult,
ImageOperation::MDiv,
ImageOperation::Expression("r = 1.0".into()),
ImageOperation::Noise {
Expand Down

0 comments on commit e00f246

Please sign in to comment.