use crate::error::{Error, Result};
use crate::image::Image;
use crate::props::Transform;
use crate::props::simple::{Clap, Mirror, Rotation};
pub fn apply_all(mut image: Image, transforms: &[Transform]) -> Result<Image> {
for t in transforms {
image = match *t {
Transform::Crop(c) => crop(image, c)?,
Transform::Rotate(r) => rotate(image, r),
Transform::Mirror(m) => mirror(image, m),
};
}
Ok(image)
}
pub fn transformed_size(width: u32, height: u32, transforms: &[Transform]) -> Result<(u32, u32)> {
let (mut w, mut h) = (width, height);
for t in transforms {
match *t {
Transform::Crop(c) => {
let (cw, ch) = crop_size(w, h, c)?;
w = cw;
h = ch;
}
Transform::Rotate(r) if r.swaps_axes() => core::mem::swap(&mut w, &mut h),
_ => {}
}
}
Ok((w, h))
}
pub fn crop_rect(width: u32, height: u32, c: Clap) -> Result<(u32, u32, u32, u32)> {
let (cw, ch) = crop_size(width, height, c)?;
let left = corner(width, cw, c.horiz_off)?;
let top = corner(height, ch, c.vert_off)?;
if left + cw > width || top + ch > height {
return Err(Error::Malformed(
"clap selects a rectangle outside the image",
));
}
Ok((left, top, cw, ch))
}
fn crop_size(width: u32, height: u32, c: Clap) -> Result<(u32, u32)> {
let cw = ratio(c.width)?;
let ch = ratio(c.height)?;
if cw == 0 || ch == 0 || cw > width || ch > height {
return Err(Error::Malformed("clap declares an impossible cropped size"));
}
Ok((cw, ch))
}
fn ratio((n, d): (u32, u32)) -> Result<u32> {
if d == 0 {
return Err(Error::Malformed("clap has a zero denominator"));
}
Ok(n / d)
}
fn corner(full: u32, cropped: u32, off: (i32, u32)) -> Result<u32> {
if off.1 == 0 {
return Err(Error::Malformed("clap has a zero denominator"));
}
let off2 = 2 * i64::from(off.0) / i64::from(off.1);
let left2 = i64::from(full) - 1 + off2 - (i64::from(cropped) - 1);
if left2 < 0 {
return Err(Error::Malformed(
"clap selects a rectangle outside the image",
));
}
u32::try_from(left2 / 2).map_err(|_| Error::Malformed("clap offset is out of range"))
}
pub fn crop(image: Image, c: Clap) -> Result<Image> {
let (left, top, cw, ch) = crop_rect(image.width, image.height, c)?;
let bpp = image.layout.bytes_per_pixel();
let src_row = image.row_bytes();
let dst_row = cw as usize * bpp;
let mut data = alloc::vec![0u8; dst_row * ch as usize];
for y in 0..ch as usize {
let s = (top as usize + y) * src_row + left as usize * bpp;
let d = y * dst_row;
let (Some(srow), Some(drow)) =
(image.data.get(s..s + dst_row), data.get_mut(d..d + dst_row))
else {
return Err(Error::Malformed(
"clap selects a rectangle outside the image",
));
};
drow.copy_from_slice(srow);
}
Ok(Image {
data,
width: cw,
height: ch,
layout: image.layout,
})
}
pub fn rotate(image: Image, r: Rotation) -> Image {
if r == Rotation::None {
return image;
}
let bpp = image.layout.bytes_per_pixel();
let (w, h) = (image.width as usize, image.height as usize);
let (ow, oh) = if r.swaps_axes() { (h, w) } else { (w, h) };
let mut data = alloc::vec![0u8; w * h * bpp];
for y in 0..h {
for x in 0..w {
let (nx, ny) = match r {
Rotation::Ccw90 => (y, w - 1 - x),
Rotation::Ccw180 => (w - 1 - x, h - 1 - y),
_ => (h - 1 - y, x),
};
let s = (y * w + x) * bpp;
let d = (ny * ow + nx) * bpp;
data[d..d + bpp].copy_from_slice(&image.data[s..s + bpp]);
}
}
Image {
data,
width: ow as u32,
height: oh as u32,
layout: image.layout,
}
}
pub fn mirror(mut image: Image, m: Mirror) -> Image {
let bpp = image.layout.bytes_per_pixel();
let row = image.row_bytes();
let (w, h) = (image.width as usize, image.height as usize);
match m {
Mirror::LeftRight => {
for y in 0..h {
let base = y * row;
for x in 0..w / 2 {
let (a, b) = (base + x * bpp, base + (w - 1 - x) * bpp);
for k in 0..bpp {
image.data.swap(a + k, b + k);
}
}
}
}
Mirror::TopBottom => {
for y in 0..h / 2 {
let (a, b) = (y * row, (h - 1 - y) * row);
for k in 0..row {
image.data.swap(a + k, b + k);
}
}
}
}
image
}