about summary refs log tree commit diff
path: root/src/writer/imagebuilder.rs
blob: d38687e377143b2084ffd59a5871f7bc5e9cecb0 (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
use crate::block::{ColorTable, ImageDescriptor, IndexedImage};

pub struct ImageBuilder {
    left_offset: u16,
    top_offset: u16,
    width: u16,
    height: u16,
    color_table: Option<ColorTable>,
    indicies: Vec<u8>,
}

impl ImageBuilder {
    pub fn new(width: u16, height: u16) -> Self {
        Self {
            left_offset: 0,
            top_offset: 0,
            width,
            height,
            color_table: None,
            indicies: vec![],
        }
    }

    pub fn offsets(mut self, left_offset: u16, top_offset: u16) -> Self {
        self.left_offset = left_offset;
        self.top_offset = top_offset;
        self
    }

    pub fn left_offset(mut self, offset: u16) -> Self {
        self.left_offset = offset;
        self
    }

    pub fn top_offset(mut self, offset: u16) -> Self {
        self.top_offset = offset;
        self
    }

    pub fn color_table(mut self, table: ColorTable) -> Self {
        self.color_table = Some(table);

        self
    }

    pub fn indicies(mut self, vec: Vec<u8>) -> Self {
        self.indicies = vec;
        self
    }

    pub fn build(self) -> IndexedImage {
        let mut imgdesc = ImageDescriptor {
            left: self.left_offset,
            top: self.top_offset,
            width: self.width,
            height: self.height,
            packed: 0, // Set later
        };

        if let Some(lct) = &self.color_table {
            imgdesc.set_color_table_present(true);
            imgdesc.set_color_table_size(lct.packed_len());
        }

        IndexedImage {
            image_descriptor: imgdesc,
            local_color_table: self.color_table,
            indicies: self.indicies,
        }
    }
}