aboutsummaryrefslogtreecommitdiff
path: root/evc/src/block.rs
blob: 24691e809b2ee1234e21fa369dd4ffad40040d29 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use anyhow::bail;

use crate::{
    format::ser::{Ser, Sink, Small, Source},
    helpers::vector::Vec2,
    helpers::{matrix::Mat2, pixel::Pixel},
};

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

#[derive(Clone, Debug)]
pub struct AdvancedReference {
    pub translation: Vec2<i8>,
    pub transform: Mat2<i8>,
    pub value_scale: i8,
}

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

    pub fn read(source: &mut impl std::io::Read, size: Vec2<isize>) -> anyhow::Result<Self> {
        Ok(match source.get::<u8>()? {
            0 => Block::Literal(source.get()?),
            1 => Block::Split(Box::new({
                let vert = size.x > size.y;
                let asize = if vert {
                    (size.x / 2, size.y).into()
                } else {
                    (size.x, size.y / 2).into()
                };
                let bsize = if vert {
                    (size.x - size.x / 2, size.y).into()
                } else {
                    (size.x, size.y - size.y / 2).into()
                };

                let a = Block::read(source, asize)?;
                let b = Block::read(source, bsize)?;
                [a, b]
            })),
            2 => Block::Reference {
                translation: source.get::<Small<Vec2<isize>>>()?.0,
            },
            3 => Block::AdvancedReference(AdvancedReference {
                translation: source.get()?,
                transform: source.get()?,
                value_scale: source.get()?,
            }),
            x => bail!("corrupt block type ({})", x),
        })
    }
}

impl Block {
    pub fn is_literal(&self) -> bool {
        matches!(self, Block::Literal(..))
    }
}

impl Default for AdvancedReference {
    fn default() -> Self {
        Self {
            translation: Vec2 { x: 0, y: 0 },
            transform: Mat2 {
                a: 4,
                b: 0,
                c: 0,
                d: 4,
            },
            value_scale: 4, // so 1
        }
    }
}