1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use crate::{parse_error::ParseError, Bpp, TgaHeader};
use nom::bytes::complete::take;

/// Color map.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct ColorMap<'a> {
    /// First color index.
    start_index: u16,
    /// Number of entries.
    length: u16,
    /// Entry bit depth.
    entry_bpp: Bpp,
    /// Color map data.
    data: &'a [u8],
}

impl<'a> ColorMap<'a> {
    pub(crate) fn parse(
        input: &'a [u8],
        header: &TgaHeader,
    ) -> Result<(&'a [u8], Option<Self>), ParseError> {
        if !header.has_color_map {
            return Ok((input, None));
        }

        let entry_bpp = header.color_map_depth.ok_or(ParseError::ColorMap)?;

        let length = usize::from(header.color_map_len) * usize::from(entry_bpp.bytes());

        let (input, color_map_data) =
            take(length)(input).map_err(|_: nom::Err<()>| ParseError::ColorMap)?;

        Ok((
            input,
            Some(Self {
                start_index: header.color_map_start,
                length: header.color_map_len,
                entry_bpp,
                data: color_map_data,
            }),
        ))
    }

    /// Returns the bit depth for the entries in the color map.
    pub fn entry_bpp(&self) -> Bpp {
        self.entry_bpp
    }

    /// Returns the raw color value for a color map entry.
    pub fn get_raw(&self, index: usize) -> Option<u32> {
        //TODO: use start_index
        if index >= usize::from(self.length) {
            return None;
        }

        let start = index * usize::from(self.entry_bpp.bytes());

        Some(match self.entry_bpp {
            Bpp::Bits8 => self.data[start] as u32,
            Bpp::Bits16 => u32::from_le_bytes([self.data[start], self.data[start + 1], 0, 0]),
            Bpp::Bits24 => u32::from_le_bytes([
                self.data[start],
                self.data[start + 1],
                self.data[start + 2],
                0,
            ]),
            Bpp::Bits32 => u32::from_le_bytes([
                self.data[start],
                self.data[start + 1],
                self.data[start + 2],
                self.data[start + 3],
            ]),
        })
    }
}