aboutsummaryrefslogtreecommitdiff
path: root/evc/src/block.rs
blob: 29dd4ba6436abd80d3d6f52dc446c1a4823d6a31 (plain)
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
use crate::ser::{Ser, Sink, Source};

#[derive(Copy, Clone, Debug)]
pub struct Pixel {
    pub r: u8,
    pub g: u8,
    pub b: u8,
}

#[derive(Clone, Debug)]
pub struct Block {
    size: (usize, usize),
    inner: BlockInner,
}

#[derive(Clone, Debug)]
pub enum BlockInner {
    Literal(Vec<Pixel>),
    Split(Box<[Block; 2]>),
    Reference { translation: (usize, usize) },
}

impl Block {
    pub fn write(&self, sink: &mut impl std::io::Write) -> std::io::Result<()> {
        match &self.inner {
            BlockInner::Literal(pixels) => {
                sink.put(0u8)?;
                pixels.write(sink)?;
            }
            BlockInner::Split(box [a, b]) => {
                sink.put(1u8)?;
                a.write(sink)?;
                b.write(sink)?;
            }
            BlockInner::Reference { translation } => {
                sink.put(2u8)?;
            }
        }
        Ok(())
    }

    pub fn read(source: &mut impl std::io::Read, size: (usize, usize)) -> std::io::Result<Self> {
        let inner = match source.get::<u8>()? {
            0 => BlockInner::Literal(source.get()?),
            1 => BlockInner::Split(Box::new({
                let subsize = if size.0 > size.1 {
                    (size.0 / 2, size.1)
                } else {
                    (size.0, size.1 / 2)
                };
                [Block::read(source, subsize)?, Block::read(source, subsize)?]
            })),
            2 => todo!(),
            _ => panic!("file corrupt"),
        };

        Ok(Self { size, inner })
    }
}
impl Ser for Pixel {
    fn write(&self, sink: &mut impl std::io::Write) -> std::io::Result<()> {
        sink.put((self.r, self.g, self.b))
    }

    fn read(source: &mut impl std::io::Read) -> std::io::Result<Self> {
        let (r, g, b) = source.get()?;
        Ok(Self { r, g, b })
    }
}