use super::convert::Pixels;
use super::header::Header;
use super::metadata::Metadata;
use super::types::{BitDepth, ColorType};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ColorKey {
Gray(u16),
Rgb([u16; 3]),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DecodedImage {
header: Header,
data: Vec<u8>,
palette: Option<Vec<[u8; 4]>>,
color_key: Option<ColorKey>,
metadata: Metadata,
}
impl DecodedImage {
pub(crate) fn new(
header: Header,
data: Vec<u8>,
palette: Option<Vec<[u8; 4]>>,
color_key: Option<ColorKey>,
metadata: Metadata,
) -> Self {
Self {
header,
data,
palette,
color_key,
metadata,
}
}
pub const fn width(&self) -> u32 {
self.header.width
}
pub const fn height(&self) -> u32 {
self.header.height
}
pub const fn color_type(&self) -> ColorType {
self.header.format.color
}
pub const fn bit_depth(&self) -> BitDepth {
self.header.format.depth
}
pub const fn is_interlaced(&self) -> bool {
self.header.interlaced
}
pub fn row_bytes(&self) -> usize {
self.data.len() / self.header.height as usize
}
pub fn data(&self) -> &[u8] {
&self.data
}
pub fn into_data(self) -> Vec<u8> {
self.data
}
pub fn palette(&self) -> Option<&[[u8; 4]]> {
self.palette.as_deref()
}
pub const fn color_key(&self) -> Option<ColorKey> {
self.color_key
}
pub const fn metadata(&self) -> &Metadata {
&self.metadata
}
pub fn to_rgba8(&self) -> Vec<u8> {
self.pixels().to_rgba8()
}
pub fn to_rgba16(&self) -> Vec<u16> {
self.pixels().to_rgba16()
}
fn pixels(&self) -> Pixels<'_> {
Pixels {
width: self.header.width as usize,
format: self.header.format,
row_bytes: self.row_bytes(),
data: &self.data,
palette: self.palette.as_deref().unwrap_or_default(),
color_key: self.color_key,
}
}
}