mod draw;
mod editor;
pub(crate) mod ops;
use serde::{Deserialize, Serialize};
pub use editor::DeterministicEditor;
use crate::error::{Result, VisionError};
use crate::geometry::{BBox, Mask, Polygon, TransformChain};
use crate::media::DecodedImage;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Rgba(pub [u8; 4]);
impl Rgba {
pub const BLACK: Self = Self([0, 0, 0, 255]);
pub const WHITE: Self = Self([255, 255, 255, 255]);
pub const RED: Self = Self([230, 25, 75, 255]);
pub const GREEN: Self = Self([60, 180, 75, 255]);
pub const BLUE: Self = Self([0, 130, 200, 255]);
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "kind")]
pub enum Overlay {
Box {
bbox: BBox,
color: Rgba,
thickness: u32,
},
Polygon {
polygon: Polygon,
color: Rgba,
thickness: u32,
},
Mask {
mask: Mask,
color: Rgba,
},
Point {
at: [f32; 2],
color: Rgba,
radius: u32,
},
Mark {
at: [f32; 2],
number: u32,
color: Rgba,
#[serde(default = "unit_scale", skip_serializing_if = "is_unit_scale")]
scale: f32,
},
}
fn unit_scale() -> f32 {
1.0
}
fn is_unit_scale(scale: &f32) -> bool {
(*scale - 1.0).abs() < f32::EPSILON
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "op")]
pub enum EditOp {
Crop {
bbox: BBox,
},
Resize {
width: u32,
height: u32,
},
Redact {
regions: Vec<BBox>,
color: Rgba,
},
Blur {
regions: Vec<BBox>,
sigma: f32,
},
Pixelate {
regions: Vec<BBox>,
block: u32,
},
Annotate {
overlays: Vec<Overlay>,
},
Generative {
prompt: String,
},
}
impl EditOp {
pub fn digest(&self) -> crate::Digest {
crate::Digest::of_json(self)
}
}
pub fn apply_edit(
pixels: &DecodedImage,
chain: &TransformChain,
op: &EditOp,
) -> Result<DecodedImage> {
let to_target = |b: &BBox| chain.bbox_to_target(b);
match op {
EditOp::Crop { bbox } => {
let (w, h) = pixels.size();
let (x, y, cw, ch) = to_target(bbox).pixel_bounds(w, h);
if cw == 0 || ch == 0 {
return Err(VisionError::InvalidInput(format!(
"crop {bbox:?} leaves no pixels of the {w}x{h} image"
)));
}
Ok(ops::crop(pixels, x, y, cw, ch))
}
EditOp::Resize { width, height } => {
if *width == 0 || *height == 0 {
return Err(VisionError::InvalidInput(format!(
"resize to {width}x{height} leaves no pixels"
)));
}
Ok(ops::resize(pixels, *width, *height))
}
EditOp::Redact { regions, color } => {
let mut out = pixels.clone();
for region in regions {
ops::fill(&mut out, &to_target(region), *color);
}
Ok(out)
}
EditOp::Blur { regions, sigma } => {
let mut out = pixels.clone();
for region in regions {
ops::blur(&mut out, &to_target(region), *sigma);
}
Ok(out)
}
EditOp::Pixelate { regions, block } => {
let mut out = pixels.clone();
for region in regions {
ops::pixelate(&mut out, &to_target(region), (*block).max(1));
}
Ok(out)
}
EditOp::Annotate { overlays } => {
let mut out = pixels.clone();
for overlay in overlays {
draw::overlay(&mut out, chain, overlay)?;
}
Ok(out)
}
EditOp::Generative { .. } => Err(VisionError::Unsupported(
"generative edits need a model-backed editor; the deterministic editor crops, \
resizes, redacts, blurs, pixelates and annotates"
.into(),
)),
}
}