mp4_atom/meta/properties/
pixi.rs

1use crate::*;
2
3// PixelInformationProperty, ISO/IEC 23008-12 Section 6.5.6
4// Number and bit depth of the colour components in the reconstructed image
5// for the image item this property is associated with.
6
7#[derive(Debug, Clone, PartialEq, Eq, Default)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct Pixi {
10    pub bits_per_channel: Vec<u8>,
11}
12
13impl AtomExt for Pixi {
14    type Ext = ();
15
16    const KIND_EXT: FourCC = FourCC::new(b"pixi");
17
18    fn decode_body_ext<B: Buf>(buf: &mut B, _ext: ()) -> Result<Self> {
19        let num_channels = u8::decode(buf)?;
20        let mut bits_per_channel = Vec::with_capacity(num_channels as usize);
21        for _ in 0..num_channels {
22            bits_per_channel.push(u8::decode(buf)?);
23        }
24        Ok(Pixi { bits_per_channel })
25    }
26
27    fn encode_body_ext<B: BufMut>(&self, buf: &mut B) -> Result<()> {
28        let num_channels = self.bits_per_channel.len() as u8;
29        num_channels.encode(buf)?;
30        for bpc in &self.bits_per_channel {
31            bpc.encode(buf)?;
32        }
33        Ok(())
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn test_pixi() {
43        let expected = Pixi {
44            bits_per_channel: vec![8, 7, 6],
45        };
46        let mut buf = Vec::new();
47        expected.encode(&mut buf).unwrap();
48
49        let mut buf = buf.as_ref();
50        assert_eq!(
51            buf,
52            [0, 0, 0, 16, b'p', b'i', b'x', b'i', 0, 0, 0, 0, 3, 8, 7, 6]
53        );
54        let decoded = Pixi::decode(&mut buf).unwrap();
55        assert_eq!(decoded, expected);
56    }
57}