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
use nom::bytes::complete::take;

use crate::binary::{
    errors::ParseResult,
    scalars::{parse_string, short, word, Short, Word},
};

#[derive(Debug)]
pub struct MaskChunk<'a> {
    /// X position
    pub x: Short,
    /// Y position
    pub y: Short,
    /// Width
    pub width: Word,
    /// Height
    pub height: Word,
    /// Mask name
    pub name: &'a str,
    /// Bit map data (size = height*((width+7)/8))
    /// Each byte contains 8 pixels (the leftmost pixels are
    /// packed into the high order bits)
    ///
    /// Important: The length of the data is NOT checked by
    /// the parser. If you really need this you should make
    /// sure that `data.len() == height * ((width + 7) / 8`
    /// is true.
    pub data: &'a [u8],
}

pub fn parse_mask_chunk(input: &[u8]) -> ParseResult<'_, MaskChunk<'_>> {
    let (input, x) = short(input)?;
    let (input, y) = short(input)?;
    let (input, width) = word(input)?;
    let (input, height) = word(input)?;
    let (input, _) = take(8usize)(input)?;
    let (input, name) = parse_string(input)?;
    // The parser should ensure that there are enough bytes
    // available to read. Since this is the only frame that
    // needs the `width` and `height` from the `Header` and
    // this frame is deprecated anyways this check was left
    // to the users of this library.
    //let size = height * ((width + 7) / 8);
    //let (input, data) = take(size)(input)?;
    Ok((
        &input[input.len()..],
        MaskChunk {
            x,
            y,
            width,
            height,
            name,
            data: input,
        },
    ))
}