aseprite_loader/binary/chunks/mask.rs
1use nom::bytes::complete::take;
2
3use crate::binary::{
4 errors::ParseResult,
5 scalars::{parse_string, short, word, Short, Word},
6};
7
8#[derive(Debug)]
9pub struct MaskChunk<'a> {
10 /// X position
11 pub x: Short,
12 /// Y position
13 pub y: Short,
14 /// Width
15 pub width: Word,
16 /// Height
17 pub height: Word,
18 /// Mask name
19 pub name: &'a str,
20 /// Bit map data (size = height*((width+7)/8))
21 /// Each byte contains 8 pixels (the leftmost pixels are
22 /// packed into the high order bits)
23 ///
24 /// Important: The length of the data is NOT checked by
25 /// the parser. If you really need this you should make
26 /// sure that `data.len() == height * ((width + 7) / 8`
27 /// is true.
28 pub data: &'a [u8],
29}
30
31pub fn parse_mask_chunk(input: &[u8]) -> ParseResult<'_, MaskChunk<'_>> {
32 let (input, x) = short(input)?;
33 let (input, y) = short(input)?;
34 let (input, width) = word(input)?;
35 let (input, height) = word(input)?;
36 let (input, _) = take(8usize)(input)?;
37 let (input, name) = parse_string(input)?;
38 // The parser should ensure that there are enough bytes
39 // available to read. Since this is the only frame that
40 // needs the `width` and `height` from the `Header` and
41 // this frame is deprecated anyways this check was left
42 // to the users of this library.
43 //let size = height * ((width + 7) / 8);
44 //let (input, data) = take(size)(input)?;
45 Ok((
46 &input[input.len()..],
47 MaskChunk {
48 x,
49 y,
50 width,
51 height,
52 name,
53 data: input,
54 },
55 ))
56}