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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use std::collections::HashMap;

pub type ColorMap = HashMap<usize, Rgb>;

#[derive(Debug)]
pub struct Gif {
    signature: String,
    screen_descriptor: ScreenDescriptor,
    global_color_map: Option<ColorMap>,
    frames: Vec<Frame>,
}

impl Gif {
    pub fn signature(&self) -> &str {
        &self.signature
    }
    pub fn screen_descriptor(&self) -> &ScreenDescriptor {
        &self.screen_descriptor
    }
    pub fn global_color_map(&self) -> &Option<ColorMap> {
        &self.global_color_map
    }
    pub fn frames(&self) -> &Vec<Frame> {
        &self.frames
    }
    pub fn new(
        signature: String,
        screen_descriptor: ScreenDescriptor,
        global_color_map: Option<ColorMap>,
        frames: Vec<Frame>,
    ) -> Self {
        Gif {
            signature,
            screen_descriptor,
            global_color_map,
            frames,
        }
    }
}

#[derive(Debug)]
pub struct ScreenDescriptor {
    width: u16,
    height: u16,
    m: bool,
    cr: u8,
    pixel: u8,
    background: u8,
}

impl ScreenDescriptor {
    pub fn width(&self) -> u16 {
        self.width
    }
    pub fn height(&self) -> u16 {
        self.height
    }
    pub fn m(&self) -> bool {
        self.m
    }
    pub fn cr(&self) -> u8 {
        self.cr
    }
    pub fn pixel(&self) -> u8 {
        self.pixel
    }

    pub fn background(&self) -> u8 {
        self.background
    }
    pub fn new(width: u16, height: u16, m: bool, cr: u8, pixel: u8, background: u8) -> Self {
        ScreenDescriptor {
            width,
            height,
            m,
            cr,
            pixel,
            background,
        }
    }
    pub fn set_m(&mut self, m: bool) {
        self.m = m;
    }
}

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

impl Rgb {
    pub fn r(&self) -> u8 {
        self.r
    }
    pub fn g(&self) -> u8 {
        self.g
    }
    pub fn b(&self) -> u8 {
        self.b
    }
    pub fn new(r: u8, g: u8, b: u8) -> Self {
        Rgb { r, g, b }
    }
}

#[derive(Debug)]
pub struct Frame {
    image_descriptor: ImageDescriptor,
    local_color_map: Option<ColorMap>,
    raster_data: Vec<u8>,
    rgba_raster_data: Option<Vec<u8>>,
    graphic_control_extension: Option<GraphicControlExtension>,
}

impl Frame {
    pub fn image_descriptor(&self) -> &ImageDescriptor {
        &self.image_descriptor
    }
    pub fn local_color_map(&self) -> &Option<ColorMap> {
        &self.local_color_map
    }
    /// Normal ColorMap index color mapping.
    /// Color maps / graphic control extension block usage is necessary to retrieve the pixel colors in rgba.
    pub fn raster_data(&self) -> &Vec<u8> {
        &self.raster_data
    }
    /// This is not a gif89a specification field, present only if requested in the decoding process.
    /// Every byte of the raster data expanded to 4 bytes (R G B A).
    /// Color maps / graphic control extension block has already been used internally to obtain this raster data representation.
    pub fn rgba_raster_data(&self) -> &Option<Vec<u8>> {
        &self.rgba_raster_data
    }
    pub fn graphic_control_extension(&self) -> &Option<GraphicControlExtension> {
        &self.graphic_control_extension
    }
    pub fn new(
        image_descriptor: ImageDescriptor,
        local_color_map: Option<ColorMap>,
        raster_data: Vec<u8>,
        rgba_raster_data: Option<Vec<u8>>,
        graphic_control_extension: Option<GraphicControlExtension>,
    ) -> Self {
        Frame {
            image_descriptor,
            local_color_map,
            raster_data,
            rgba_raster_data,
            graphic_control_extension,
        }
    }
}

#[derive(Debug)]
pub enum ExtensionBlock {
    GraphicControlExtension(GraphicControlExtension),
}

#[derive(Debug)]
pub struct GraphicControlExtension {
    disposal_method: u8,
    user_input: bool,
    transparent_color: bool,
    delay_time: u16,
    transparent_color_index: Option<u8>,
}

impl GraphicControlExtension {
    pub fn disposal_method(&self) -> u8 {
        self.disposal_method
    }
    pub fn user_input(&self) -> bool {
        self.user_input
    }
    pub fn transparent_color(&self) -> bool {
        self.transparent_color
    }
    pub fn delay_time(&self) -> u16 {
        self.delay_time
    }
    pub fn transparent_color_index(&self) -> Option<u8> {
        self.transparent_color_index
    }
    pub fn new(
        disposal_method: u8,
        user_input: bool,
        transparent_color: bool,
        delay_time: u16,
        transparent_color_index: Option<u8>,
    ) -> Self {
        GraphicControlExtension {
            disposal_method,
            user_input,
            transparent_color,
            delay_time,
            transparent_color_index,
        }
    }
}

#[derive(Debug)]
pub struct ImageDescriptor {
    image_left: u16,
    image_top: u16,
    image_width: u16,
    image_height: u16,
    m: bool,
    i: bool,
    pixel: u8,
}

impl ImageDescriptor {
    pub fn image_left(&self) -> u16 {
        self.image_left
    }
    pub fn image_top(&self) -> u16 {
        self.image_top
    }
    pub fn image_width(&self) -> u16 {
        self.image_width
    }
    pub fn image_height(&self) -> u16 {
        self.image_height
    }
    pub fn m(&self) -> bool {
        self.m
    }
    pub fn i(&self) -> bool {
        self.i
    }
    pub fn pixel(&self) -> u8 {
        self.pixel
    }

    pub fn new(
        image_left: u16,
        image_top: u16,
        image_width: u16,
        image_height: u16,
        m: bool,
        i: bool,
        pixel: u8,
    ) -> Self {
        ImageDescriptor {
            image_left,
            image_top,
            image_width,
            image_height,
            m,
            i,
            pixel,
        }
    }
}