aseprite_loader/binary/chunks/
old_palette.rs

1use nom::multi::count;
2
3use crate::binary::{
4    errors::ParseResult,
5    scalars::{byte, parse_rgb, word, Byte, RGB},
6};
7
8#[derive(Debug)]
9/// Ignore this chunk if you find the new palette chunk (0x2019)
10/// Aseprite v1.1 saves both chunks 0x0004 and 0x2019 just for
11/// backward compatibility.
12///
13/// Both old palette chunks are stored in this structure as their
14/// only difference is the color range. Colors in the 0x0004 version
15/// of the chunk range from 0-255 while colors in the 0x0011 version
16/// range from 0-63.
17pub struct OldPaletteChunk {
18    pub packets: Vec<Packet>,
19}
20
21#[derive(Debug)]
22pub struct Packet {
23    pub entries_to_skip: Byte,
24    pub colors: Vec<RGB>,
25}
26
27pub fn parse_old_palette_chunk(input: &[u8]) -> ParseResult<'_, OldPaletteChunk> {
28    let (input, number_of_packets) = word(input)?;
29    let (input, packets) = count(parse_packet, number_of_packets.into())(input)?;
30    Ok((input, OldPaletteChunk { packets }))
31}
32
33pub fn parse_packet(input: &[u8]) -> ParseResult<'_, Packet> {
34    let (input, entries_to_skip) = byte(input)?;
35    let (input, number_of_colors) = byte(input)?;
36    let number_of_colors = if number_of_colors == 0 {
37        256usize
38    } else {
39        number_of_colors.into()
40    };
41    let (input, colors) = count(parse_rgb, number_of_colors)(input)?;
42    Ok((
43        input,
44        Packet {
45            entries_to_skip,
46            colors,
47        },
48    ))
49}