aseprite_loader/binary/
color_depth.rs

1use strum::FromRepr;
2
3use super::{
4    errors::ParseResult,
5    scalars::{word, Word},
6};
7
8/// Color depth (bits per pixel)
9/// 16 bpp = Grayscale
10/// 8 bpp = Indexed
11#[derive(Copy, Clone, Debug, Eq, PartialEq, FromRepr)]
12pub enum ColorDepth {
13    Rgba,
14    Grayscale,
15    Indexed,
16    Unknown(Word),
17}
18
19impl ColorDepth {
20    pub fn bpp(&self) -> Word {
21        match self {
22            Self::Rgba => 32,
23            Self::Grayscale => 16,
24            Self::Indexed => 8,
25            Self::Unknown(bpp) => *bpp,
26        }
27    }
28    pub fn pixel_size(&self) -> Option<usize> {
29        match self {
30            Self::Rgba => Some(4),
31            Self::Grayscale => Some(2),
32            Self::Indexed => Some(1),
33            Self::Unknown(_) => None,
34        }
35    }
36}
37
38impl From<Word> for ColorDepth {
39    fn from(bpp: Word) -> Self {
40        match bpp {
41            32 => ColorDepth::Rgba,
42            16 => ColorDepth::Grayscale,
43            8 => ColorDepth::Indexed,
44            bpp => ColorDepth::Unknown(bpp),
45        }
46    }
47}
48
49pub fn parse_color_depth(input: &[u8]) -> ParseResult<'_, ColorDepth> {
50    let (input, bpp) = word(input)?;
51    Ok((input, bpp.into()))
52}
53
54#[test]
55fn test_bpp() {
56    assert_eq!(ColorDepth::Rgba.bpp(), 32);
57    assert_eq!(ColorDepth::Grayscale.bpp(), 16);
58    assert_eq!(ColorDepth::Indexed.bpp(), 8);
59}