about summary refs log tree commit diff
path: root/src/writer/gifbuilder.rs
blob: ec3c304302ec9e0a25d788d09266270d57eb3a41 (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
use std::convert::TryInto;

use crate::block::{extension::Extension, Block, ColorTable, ScreenDescriptor, Version};
use crate::writer::ImageBuilder;
use crate::{EncodingError, Gif};

pub struct GifBuilder {
    version: Version,
    width: u16,
    height: u16,
    background_color_index: u8,
    global_color_table: Option<ColorTable>,
    blocks: Vec<Block>,
}

impl GifBuilder {
    pub fn new(width: u16, height: u16) -> Self {
        Self {
            version: Version::Gif87a,
            width,
            height,
            background_color_index: 0,
            global_color_table: None,
            blocks: vec![],
        }
    }

    pub fn palette(mut self, palette: ColorTable) -> Self {
        self.global_color_table = Some(palette);
        self
    }

    pub fn background_index(mut self, ind: u8) -> Result<Self, EncodingError> {
        if self.global_color_table.is_none() {
            Err(EncodingError::NoColorTable)
        } else {
            self.background_color_index = ind;
            Ok(self)
        }
    }

    pub fn image(mut self, ib: ImageBuilder) -> Result<Self, EncodingError> {
        if ib.required_version() == Version::Gif89a {
            self.version = Version::Gif89a;
        }

        if let Some(gce) = ib.get_graphic_control() {
            self.blocks.push(Block::Extension(gce.into()));
        }

        self.blocks.push(Block::IndexedImage(ib.build()?));
        Ok(self)
    }

    /*pub fn extension(mut self, ext: Extension) -> Self {
        self.blocks.push(Block::Extension(ext));
        self
    }*/

    pub fn repeat(&mut self, count: u16) {
        self.blocks.push(Block::Extension(Extension::Looping(count)))
    }

    pub fn build(self) -> Gif {
        let mut lsd = ScreenDescriptor {
            width: self.width,
            height: self.height,
            packed: 0, // Set later
            background_color_index: self.background_color_index,
            pixel_aspect_ratio: 0, //TODO: Allow configuring
        };

        if let Some(gct) = &self.global_color_table {
            println!("build {}", gct.len());
            lsd.set_color_table_present(true);
            lsd.set_color_table_size((gct.len() - 1) as u8);
        }

        Gif {
            header: self.version,
            screen_descriptor: lsd,
            global_color_table: self.global_color_table,
            blocks: self.blocks,
        }
    }
}