use std::io::Cursor;
use std::sync::Arc;
use image::{DynamicImage, ImageDecoder, ImageFormat, ImageReader, RgbaImage};
use crate::digest::Digest;
use crate::error::{Result, VisionError};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DecodeLimits {
pub max_bytes: u64,
pub max_pixels: u64,
}
impl Default for DecodeLimits {
fn default() -> Self {
Self { max_bytes: 50 * 1024 * 1024, max_pixels: 50_000_000 }
}
}
impl DecodeLimits {
pub fn with_max_bytes(mut self, max_bytes: u64) -> Self {
self.max_bytes = max_bytes;
self
}
pub fn with_max_pixels(mut self, max_pixels: u64) -> Self {
self.max_pixels = max_pixels;
self
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct DecodedImage {
pixels: RgbaImage,
}
impl std::fmt::Debug for DecodedImage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DecodedImage")
.field("width", &self.width())
.field("height", &self.height())
.finish_non_exhaustive()
}
}
impl DecodedImage {
pub fn from_rgba(width: u32, height: u32, rgba: Vec<u8>) -> Result<Self> {
if width == 0 || height == 0 {
return Err(VisionError::InvalidInput(format!(
"a {width}x{height} image has no pixels"
)));
}
let expected = width as usize * height as usize * 4;
let actual = rgba.len();
let pixels = RgbaImage::from_raw(width, height, rgba).ok_or_else(|| {
VisionError::InvalidInput(format!(
"a {width}x{height} RGBA image needs {expected} bytes, got {actual}"
))
})?;
Ok(Self { pixels })
}
pub fn filled(width: u32, height: u32, rgba: [u8; 4]) -> Self {
Self { pixels: RgbaImage::from_pixel(width.max(1), height.max(1), image::Rgba(rgba)) }
}
pub(crate) fn from_inner(pixels: RgbaImage) -> Self {
Self { pixels }
}
pub(crate) fn inner(&self) -> &RgbaImage {
&self.pixels
}
pub(crate) fn inner_mut(&mut self) -> &mut RgbaImage {
&mut self.pixels
}
pub fn width(&self) -> u32 {
self.pixels.width()
}
pub fn height(&self) -> u32 {
self.pixels.height()
}
pub fn size(&self) -> (u32, u32) {
self.pixels.dimensions()
}
pub fn rgba(&self) -> &[u8] {
self.pixels.as_raw()
}
pub fn pixel(&self, x: u32, y: u32) -> [u8; 4] {
self.pixels.get_pixel(x, y).0
}
pub fn encode_png(&self) -> Result<Vec<u8>> {
let mut out = Cursor::new(Vec::new());
self.pixels
.write_to(&mut out, ImageFormat::Png)
.map_err(|e| VisionError::Decode(format!("PNG encoding failed: {e}")))?;
Ok(out.into_inner())
}
pub fn decode(bytes: &[u8], limits: &DecodeLimits) -> Result<(Self, &'static str)> {
let size = bytes.len() as u64;
if size > limits.max_bytes {
return Err(VisionError::TooManyBytes { limit: limits.max_bytes, actual: size });
}
let reader = || {
ImageReader::new(Cursor::new(bytes))
.with_guessed_format()
.map_err(|e| VisionError::Decode(e.to_string()))
};
let format = reader()?.format();
let mime = match format {
Some(ImageFormat::Png) => "image/png",
Some(ImageFormat::Jpeg) => "image/jpeg",
Some(ImageFormat::WebP) => "image/webp",
Some(ImageFormat::Gif) => "image/gif",
other => {
let detected = other.map_or_else(|| "unknown".to_string(), |f| format!("{f:?}"));
return Err(VisionError::UnsupportedFormat { detected });
}
};
let (width, height) =
reader()?.into_dimensions().map_err(|e| VisionError::Decode(e.to_string()))?;
let pixels = u64::from(width) * u64::from(height);
if pixels > limits.max_pixels {
return Err(VisionError::TooManyPixels { limit: limits.max_pixels, actual: pixels });
}
if pixels == 0 {
return Err(VisionError::Decode("the image declares no pixels".into()));
}
let mut reader = reader()?;
let mut caps = image::Limits::default();
caps.max_image_width = Some(width);
caps.max_image_height = Some(height);
caps.max_alloc = Some(pixels.saturating_mul(8).saturating_add(1 << 20));
reader.limits(caps);
let mut decoder = reader.into_decoder().map_err(|e| VisionError::Decode(e.to_string()))?;
let orientation =
decoder.orientation().unwrap_or(image::metadata::Orientation::NoTransforms);
let mut decoded =
DynamicImage::from_decoder(decoder).map_err(|e| VisionError::Decode(e.to_string()))?;
decoded.apply_orientation(orientation);
Ok((Self { pixels: decoded.into_rgba8() }, mime))
}
}
#[derive(Clone, Debug)]
pub struct SourceImage {
digest: Digest,
mime_type: String,
pixels: Arc<DecodedImage>,
}
impl SourceImage {
pub fn decode(bytes: &[u8], limits: &DecodeLimits) -> Result<Self> {
let (pixels, mime) = DecodedImage::decode(bytes, limits)?;
Ok(Self {
digest: Digest::of(bytes),
mime_type: mime.to_string(),
pixels: Arc::new(pixels),
})
}
pub fn from_pixels(pixels: DecodedImage) -> Result<Self> {
let png = pixels.encode_png()?;
Ok(Self {
digest: Digest::of(&png),
mime_type: "image/png".into(),
pixels: Arc::new(pixels),
})
}
pub(crate) fn from_parts(digest: Digest, mime_type: String, pixels: Arc<DecodedImage>) -> Self {
Self { digest, mime_type, pixels }
}
pub fn digest(&self) -> Digest {
self.digest
}
pub fn mime_type(&self) -> &str {
&self.mime_type
}
pub fn pixels(&self) -> &DecodedImage {
&self.pixels
}
pub(crate) fn shared_pixels(&self) -> Arc<DecodedImage> {
Arc::clone(&self.pixels)
}
pub fn size(&self) -> (u32, u32) {
self.pixels.size()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn caps_refuse_before_decoding() {
let png = DecodedImage::filled(100, 100, [1, 2, 3, 255]).encode_png().unwrap();
let err = DecodedImage::decode(&png, &DecodeLimits::default().with_max_pixels(9_999))
.unwrap_err();
assert!(matches!(err, VisionError::TooManyPixels { limit: 9_999, actual: 10_000 }));
let err =
DecodedImage::decode(&png, &DecodeLimits::default().with_max_bytes(10)).unwrap_err();
assert!(matches!(err, VisionError::TooManyBytes { limit: 10, .. }));
}
#[test]
fn unknown_formats_are_refused() {
let err = DecodedImage::decode(b"GIF89 is not this", &DecodeLimits::default()).unwrap_err();
assert!(matches!(err, VisionError::UnsupportedFormat { .. } | VisionError::Decode(_)));
let mut bmp = b"BM".to_vec();
bmp.extend_from_slice(&[0; 60]);
let err = DecodedImage::decode(&bmp, &DecodeLimits::default()).unwrap_err();
assert!(matches!(err, VisionError::UnsupportedFormat { .. }), "{err}");
}
#[test]
fn png_round_trips_and_encoding_is_stable() {
let image = DecodedImage::filled(3, 2, [10, 20, 30, 255]);
let a = image.encode_png().unwrap();
let (decoded, mime) = DecodedImage::decode(&a, &DecodeLimits::default()).unwrap();
assert_eq!(mime, "image/png");
assert_eq!(decoded, image);
assert_eq!(decoded.encode_png().unwrap(), a);
}
}