use alloc::vec::Vec;
use crate::error::{Error, Result};
pub const DEFAULT_MAX_PIXELS: u64 = 268_435_456;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PixelLayout {
#[default]
Rgb8,
Rgba8,
Bgr8,
Bgra8,
Gray8,
Rgb16,
Rgba16,
}
impl PixelLayout {
pub const fn channels(self) -> usize {
match self {
PixelLayout::Gray8 => 1,
PixelLayout::Rgb8 | PixelLayout::Bgr8 | PixelLayout::Rgb16 => 3,
PixelLayout::Rgba8 | PixelLayout::Bgra8 | PixelLayout::Rgba16 => 4,
}
}
pub const fn bytes_per_channel(self) -> usize {
match self {
PixelLayout::Rgb16 | PixelLayout::Rgba16 => 2,
_ => 1,
}
}
pub const fn bytes_per_pixel(self) -> usize {
self.channels() * self.bytes_per_channel()
}
pub const fn has_alpha(self) -> bool {
matches!(
self,
PixelLayout::Rgba8 | PixelLayout::Bgra8 | PixelLayout::Rgba16
)
}
pub const fn is_bgr(self) -> bool {
matches!(self, PixelLayout::Bgr8 | PixelLayout::Bgra8)
}
pub const fn is_gray(self) -> bool {
matches!(self, PixelLayout::Gray8)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Image {
pub data: Vec<u8>,
pub width: u32,
pub height: u32,
pub layout: PixelLayout,
}
impl Image {
pub fn zeroed(width: u32, height: u32, layout: PixelLayout, max_pixels: u64) -> Result<Image> {
let pixels = check_pixels(width, height, max_pixels)?;
let bytes = pixels
.checked_mul(layout.bytes_per_pixel() as u64)
.and_then(|b| usize::try_from(b).ok())
.ok_or(Error::PixelLimit { pixels, max_pixels })?;
Ok(Image {
data: alloc::vec![0u8; bytes],
width,
height,
layout,
})
}
pub const fn row_bytes(&self) -> usize {
self.width as usize * self.layout.bytes_per_pixel()
}
pub const fn pixel_count(&self) -> u64 {
self.width as u64 * self.height as u64
}
}
pub fn check_pixels(width: u32, height: u32, max_pixels: u64) -> Result<u64> {
if width == 0 || height == 0 {
return Err(Error::Malformed("image has a zero dimension"));
}
let pixels = u64::from(width) * u64::from(height);
if pixels > max_pixels {
return Err(Error::PixelLimit { pixels, max_pixels });
}
Ok(pixels)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DecodeOptions {
pub layout: PixelLayout,
pub max_pixels: Option<u64>,
pub apply_transforms: bool,
pub decode_alpha: bool,
pub strict: bool,
pub threads: Option<usize>,
}
impl Default for DecodeOptions {
fn default() -> Self {
DecodeOptions {
layout: PixelLayout::Rgb8,
max_pixels: Some(DEFAULT_MAX_PIXELS),
apply_transforms: true,
decode_alpha: true,
strict: false,
threads: None,
}
}
}
impl DecodeOptions {
pub const fn pixel_limit(&self) -> u64 {
match self.max_pixels {
Some(n) => n,
None => u64::MAX,
}
}
pub fn with_layout(mut self, layout: PixelLayout) -> Self {
self.layout = layout;
self
}
pub fn with_max_pixels(mut self, max_pixels: Option<u64>) -> Self {
self.max_pixels = max_pixels;
self
}
pub fn with_transforms(mut self, apply: bool) -> Self {
self.apply_transforms = apply;
self
}
pub fn with_alpha(mut self, decode: bool) -> Self {
self.decode_alpha = decode;
self
}
pub fn with_strict(mut self, strict: bool) -> Self {
self.strict = strict;
self
}
pub fn with_threads(mut self, threads: Option<usize>) -> Self {
self.threads = threads;
self
}
}