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
use crate::{reader::AseReader, AsepriteParseError, Result};
use nohash::IntMap;
/// The color palette embedded in the file.
#[derive(Debug)]
pub struct ColorPalette {
//entries: Vec<ColorPaletteEntry>,
pub(crate) entries: IntMap<u32, ColorPaletteEntry>,
}
/// A single entry in a [ColorPalette].
#[derive(Debug)]
pub struct ColorPaletteEntry {
id: u32,
rgba8: [u8; 4],
name: Option<String>,
}
impl ColorPalette {
/// Total number of colors in the palette.
pub fn num_colors(&self) -> u32 {
self.entries.len() as u32
}
/// Look up entry at given index.
///
/// The Aseprite file format spec does not guarantee the color indices to
/// go from `0..num_colors()` but there doesn't seem to be a way to violate
/// this constraint using the Aseprite GUI.
pub fn color(&self, index: u32) -> Option<&ColorPaletteEntry> {
self.entries.get(&index)
}
pub(crate) fn validate_indexed_pixels(&self, indexed_pixels: &[u8]) -> Result<()> {
// TODO: Make way more efficient at least for the common case where
// the palette goes from `0..num_colors`. Just search for a value >=
// num_colors. Maybe make palette an enum and discover dense format
// after parsing.
for pixel in indexed_pixels {
let color = self.color(*pixel as u32);
color.ok_or_else(|| {
AsepriteParseError::InvalidInput(format!("Palette index invalid: {}", pixel,))
})?;
}
Ok(())
}
}
impl ColorPaletteEntry {
/// The id of this entry is the same as its index in the palette.
pub fn id(&self) -> u32 {
self.id
}
/// Get the RGBA components as an array. Most color libraries allow you to
/// build an instance of their color type from such an array.
pub fn raw_rgba8(&self) -> [u8; 4] {
self.rgba8
}
/// The red channel of the color.
pub fn red(&self) -> u8 {
self.rgba8[0]
}
/// The green channel of the color.
pub fn green(&self) -> u8 {
self.rgba8[1]
}
/// The blue channel of the color.
pub fn blue(&self) -> u8 {
self.rgba8[2]
}
/// Alpha value of this color (0 = fully transparent, 255 = fully opaque).
pub fn alpha(&self) -> u8 {
self.rgba8[3]
}
/// The color name. Seems to be usually empty in practice.
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
}
pub(crate) fn parse_chunk(data: &[u8]) -> Result<ColorPalette> {
let mut reader = AseReader::new(data);
let _num_total_entries = reader.dword()?;
let first_color_index = reader.dword()?;
let last_color_index = reader.dword()?;
reader.skip_reserved(8)?;
if last_color_index < first_color_index {
return Err(AsepriteParseError::InvalidInput(format!(
"Bad palette color indices: first={} last={}",
first_color_index, last_color_index,
)));
}
let count = last_color_index - first_color_index + 1;
//let mut entries = Vec::with_capacity(count as usize);
let mut entries = IntMap::default();
for id in 0..count {
let flags = reader.word()?;
let red = reader.byte()?;
let green = reader.byte()?;
let blue = reader.byte()?;
let alpha = reader.byte()?;
let name = if flags & 1 == 1 {
let s = reader.string()?;
Some(s)
} else {
None
};
let id = id + first_color_index;
entries.insert(
id,
ColorPaletteEntry {
id,
rgba8: [red, green, blue, alpha],
name,
},
);
}
Ok(ColorPalette { entries })
}
// Note: we want to map `0 -> 0` and `63 -> 255` and evenly for the in-between
// points so we can't simply multiply by 4.
fn scale_6bit_to_8bit(color: u8) -> Result<u8> {
if color >= 64 {
return Err(AsepriteParseError::InvalidInput(format!(
"6-bit color outside range: {}",
color
)));
}
// Duplicate the top two bits of the 6-bit input into the lower 2 bits after
// multiplying by 4. This "leans" the number towards 0 or towards 255, based
// on how close we are to either of them. Examples:
//
// 000010 -> 00001000 (2 -> 8) 2/63 = 0.032, 8/255 = 0.031
// 011111 -> 01111101 (31 -> 125) 31/63 = 0.492, 125/255 = 0.490
// 100000 -> 10000010 (32 -> 130) 32/63 = 0.508, 130/255 = 0.510
// 111111 -> 11111111 (63 -> 255)
//
Ok(color << 2 | color >> 4)
}
pub(crate) fn parse_old_chunk_04(data: &[u8]) -> Result<ColorPalette> {
let mut reader = AseReader::new(data);
let packet_count = reader.word()?;
let mut entries = IntMap::default();
let mut skip = 0;
for _ in 0..packet_count {
skip += reader.byte()? as u32;
let mut count = reader.byte()? as u32;
if count == 0 {
count = 256;
}
count += skip;
for id in skip..count {
let red = reader.byte()?;
let green = reader.byte()?;
let blue = reader.byte()?;
entries.insert(
id,
ColorPaletteEntry {
id,
rgba8: [red, green, blue, 255],
name: None,
},
);
}
}
Ok(ColorPalette { entries })
}
pub(crate) fn parse_old_chunk_11(data: &[u8]) -> Result<ColorPalette> {
let mut reader = AseReader::new(data);
let packet_count = reader.word()?;
let mut entries = IntMap::default();
let mut skip = 0;
for _ in 0..packet_count {
skip += reader.byte()? as u32;
let mut count = reader.byte()? as u32;
if count == 0 {
count = 256;
}
count += skip;
for id in skip..count {
let red = scale_6bit_to_8bit(reader.byte()?)?;
let green = scale_6bit_to_8bit(reader.byte()?)?;
let blue = scale_6bit_to_8bit(reader.byte()?)?;
entries.insert(
id,
ColorPaletteEntry {
id,
rgba8: [red, green, blue, 255],
name: None,
},
);
}
}
Ok(ColorPalette { entries })
}