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
use std::mem;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ColorType {
Grayscale = 0,
RGB = 2,
Indexed = 3,
GrayscaleAlpha = 4,
RGBA = 6
}
impl ColorType {
pub fn samples(&self) -> usize {
use self::ColorType::*;
match *self {
Grayscale => 1,
RGB => 3,
Indexed => 1,
GrayscaleAlpha => 2,
RGBA => 4
}
}
pub fn from_u8(n: u8) -> Option<ColorType> {
match n {
0 | 2 | 3 | 4 | 6 => Some(unsafe { mem::transmute(n) }),
_ => None
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum BitDepth {
One = 1,
Two = 2,
Four = 4,
Eight = 8,
Sixteen = 16,
}
impl BitDepth {
pub fn from_u8(n: u8) -> Option<BitDepth> {
match n {
1 | 2 | 4 | 8 | 16 => Some(unsafe { mem::transmute(n) }),
_ => None
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct PixelDimensions {
pub xppu: u32,
pub yppu: u32,
pub unit: Unit,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum Unit {
Unspecified = 0,
Meter = 1,
}
impl Unit {
pub fn from_u8(n: u8) -> Option<Unit> {
match n {
0 | 1 => Some(unsafe { mem::transmute(n) }),
_ => None
}
}
}
#[derive(Debug)]
pub struct FrameControl {
pub sequence_number: u32,
pub width: u32,
pub height: u32,
pub x_offset: u32,
pub y_offset: u32,
pub delay_num: u16,
pub delay_den: u16,
pub dispose_op: u8,
pub blend_op: u8,
}
#[derive(Clone, Copy, Debug)]
pub struct AnimationControl {
pub num_frames: u32,
pub num_plays: u32,
}
#[derive(Debug)]
pub struct Info {
pub width: u32,
pub height: u32,
pub bit_depth: BitDepth,
pub color_type: ColorType,
pub interlaced: bool,
pub trns: Option<Vec<u8>>,
pub pixel_dims: Option<PixelDimensions>,
pub palette: Option<Vec<u8>>,
pub frame_control: Option<FrameControl>,
pub animation_control: Option<AnimationControl>
}
impl Default for Info {
fn default() -> Info {
Info {
width: 0,
height: 0,
bit_depth: BitDepth::Eight,
color_type: ColorType::Grayscale,
interlaced: false,
palette: None,
trns: None,
pixel_dims: None,
frame_control: None,
animation_control: None
}
}
}
impl Info {
pub fn size(&self) -> (u32, u32) {
(self.width, self.height)
}
pub fn is_animated(&self) -> bool {
self.frame_control.is_some() && self.animation_control.is_some()
}
pub fn animation_control(&self) -> Option<&AnimationControl> {
self.animation_control.as_ref()
}
pub fn frame_control(&self) -> Option<&FrameControl> {
self.frame_control.as_ref()
}
pub fn bits_per_pixel(&self) -> usize {
self.color_type.samples() * self.bit_depth as usize
}
pub fn bytes_per_pixel(&self) -> usize {
self.color_type.samples() * ((self.bit_depth as usize + 7) >> 3)
}
pub fn raw_bytes(&self) -> usize {
self.height as usize * self.raw_row_length()
}
pub fn raw_row_length(&self) -> usize {
let bits = self.width as usize * self.color_type.samples() * self.bit_depth as usize;
let extra = bits % 8;
bits/8
+ match extra { 0 => 0, _ => 1 }
+ 1
}
pub fn raw_row_length_from_width(&self, width: u32) -> usize {
let bits = width as usize * self.color_type.samples() * self.bit_depth as usize;
let extra = bits % 8;
bits/8
+ match extra { 0 => 0, _ => 1 }
+ 1
}
}
bitflags! {
pub struct Transformations: u32 {
const TRANSFORM_IDENTITY = 0x0000;
const TRANSFORM_STRIP_16 = 0x0001;
const TRANSFORM_STRIP_ALPHA = 0x0002;
const TRANSFORM_PACKING = 0x0004;
const TRANSFORM_PACKSWAP = 0x0008;
const TRANSFORM_EXPAND = 0x0010;
const TRANSFORM_INVERT_MONO = 0x0020;
const TRANSFORM_SHIFT = 0x0040;
const TRANSFORM_BGR = 0x0080;
const TRANSFORM_SWAP_ALPHA = 0x0100;
const TRANSFORM_SWAP_ENDIAN = 0x0200;
const TRANSFORM_INVERT_ALPHA = 0x0400;
const TRANSFORM_STRIP_FILLER = 0x0800;
const TRANSFORM_STRIP_FILLER_BEFORE = 0x0800;
const TRANSFORM_STRIP_FILLER_AFTER = 0x1000;
const TRANSFORM_GRAY_TO_RGB = 0x2000;
const TRANSFORM_EXPAND_16 = 0x4000;
const TRANSFORM_SCALE_16 = 0x8000;
}
}