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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use crate::sections::image_data_section::ChannelBytes;
use crate::sections::PsdCursor;
use thiserror::Error;
pub trait IntoRgba {
fn rgba_idx(&self, idx: usize) -> usize;
fn red(&self) -> &ChannelBytes;
fn green(&self) -> Option<&ChannelBytes>;
fn blue(&self) -> Option<&ChannelBytes>;
fn alpha(&self) -> Option<&ChannelBytes>;
fn psd_width(&self) -> u32;
fn psd_height(&self) -> u32;
fn generate_rgba(&self) -> Vec<u8> {
let rgba_len = (self.psd_width() * self.psd_height() * 4) as usize;
let red = self.red();
let green = self.green();
let blue = self.blue();
let alpha = self.alpha();
let mut rgba = vec![0; rgba_len];
use crate::psd_channel::PsdChannelKind::*;
self.insert_channel_bytes(&mut rgba, Red, red);
if let Some(green) = green {
self.insert_channel_bytes(&mut rgba, Green, green);
} else {
self.insert_channel_bytes(&mut rgba, Green, red);
}
if let Some(blue) = blue {
self.insert_channel_bytes(&mut rgba, Blue, blue);
} else {
self.insert_channel_bytes(&mut rgba, Blue, red);
}
if let Some(alpha_channel) = alpha {
self.insert_channel_bytes(&mut rgba, TransparencyMask, alpha_channel);
} else {
for idx in 0..rgba_len / 4 {
rgba[idx * 4 + 3] = 255;
}
}
rgba
}
fn generate_16_bit_grayscale_rgba(&self) -> Vec<u8> {
match self.red() {
ChannelBytes::RawData(red) => match self.green().unwrap() {
ChannelBytes::RawData(green) => sixteen_to_eight_rgba(red, green),
ChannelBytes::RleCompressed(green) => {
let green = &rle_decompress(green);
sixteen_to_eight_rgba(red, green)
}
},
ChannelBytes::RleCompressed(red) => {
let red = &rle_decompress(red);
match self.green().unwrap() {
ChannelBytes::RawData(green) => sixteen_to_eight_rgba(red, green),
ChannelBytes::RleCompressed(green) => {
let green = &rle_decompress(green);
sixteen_to_eight_rgba(red, green)
}
}
}
}
}
fn insert_channel_bytes(
&self,
rgba: &mut Vec<u8>,
channel_kind: PsdChannelKind,
channel_bytes: &ChannelBytes,
) {
match channel_bytes {
ChannelBytes::RawData(channel_bytes) => {
let offset = channel_kind.rgba_offset().unwrap();
for (idx, byte) in channel_bytes.iter().enumerate() {
let rgba_idx = self.rgba_idx(idx);
rgba[rgba_idx * 4 + offset] = *byte;
}
}
ChannelBytes::RleCompressed(channel_bytes) => {
self.insert_rle_channel(rgba, channel_kind, &channel_bytes);
}
}
}
fn insert_rle_channel(
&self,
rgba: &mut Vec<u8>,
channel_kind: PsdChannelKind,
channel_bytes: &[u8],
) {
let mut cursor = PsdCursor::new(&channel_bytes[..]);
let mut idx = 0;
let offset = channel_kind.rgba_offset().unwrap();
while cursor.position() != cursor.get_ref().len() as u64 {
let header = cursor.read_i8() as i16;
if header == -128 {
continue;
} else if header >= 0 {
let bytes_to_read = 1 + header;
for byte in cursor.read(bytes_to_read as u32) {
let rgba_idx = self.rgba_idx(idx);
rgba[rgba_idx * 4 + offset] = *byte;
idx += 1;
}
} else {
let repeat = 1 - header;
let byte = cursor.read_1()[0];
for _ in 0..repeat as usize {
let rgba_idx = self.rgba_idx(idx);
rgba[rgba_idx * 4 + offset] = byte;
idx += 1;
}
};
}
}
}
fn rle_decompress(bytes: &[u8]) -> Vec<u8> {
let mut cursor = PsdCursor::new(&bytes[..]);
let mut decompressed = vec![];
while cursor.position() != cursor.get_ref().len() as u64 {
let header = cursor.read_i8() as i16;
if header == -128 {
continue;
} else if header >= 0 {
let bytes_to_read = 1 + header;
for byte in cursor.read(bytes_to_read as u32) {
decompressed.push(*byte);
}
} else {
let repeat = 1 - header;
let byte = cursor.read_1()[0];
for _ in 0..repeat as usize {
decompressed.push(byte);
}
};
}
decompressed
}
fn sixteen_to_eight_rgba(channel1: &[u8], channel2: &[u8]) -> Vec<u8> {
let mut eight = Vec::with_capacity(channel1.len());
for idx in 0..channel1.len() {
if idx % 2 == 1 {
continue;
}
let sixteen_bit = [channel1[idx], channel1[idx + 1]];
let sixteen_bit = u16::from_be_bytes(sixteen_bit);
let eight_bit = (sixteen_bit / 256) as u8;
eight.push(eight_bit);
eight.push(eight_bit);
eight.push(eight_bit);
eight.push(255);
}
for idx in 0..channel2.len() {
if idx % 2 == 1 {
continue;
}
let sixteen_bit = [channel2[idx], channel2[idx + 1]];
let sixteen_bit = u16::from_be_bytes(sixteen_bit);
let eight_bit = (sixteen_bit / 256) as u8;
eight.push(eight_bit);
eight.push(eight_bit);
eight.push(eight_bit);
eight.push(255);
}
eight
}
#[derive(Debug, Eq, PartialEq)]
#[allow(missing_docs)]
pub enum PsdChannelCompression {
RawData = 0,
RleCompressed = 1,
ZipWithoutPrediction = 2,
ZipWithPrediction = 3,
}
impl PsdChannelCompression {
pub fn new(compression: u16) -> Option<PsdChannelCompression> {
match compression {
0 => Some(PsdChannelCompression::RawData),
1 => Some(PsdChannelCompression::RleCompressed),
2 => Some(PsdChannelCompression::ZipWithoutPrediction),
3 => Some(PsdChannelCompression::ZipWithPrediction),
_ => None,
}
}
}
#[derive(Debug, Hash, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
#[allow(missing_docs)]
pub enum PsdChannelKind {
Red = 0,
Green = 1,
Blue = 2,
TransparencyMask = -1,
UserSuppliedLayerMask = -2,
RealUserSuppliedLayerMask = -3,
}
#[derive(Debug, Error)]
pub enum PsdChannelError {
#[error("Channel {channel:#?} not present")]
ChannelNotFound { channel: PsdChannelKind },
}
impl PsdChannelKind {
pub fn new(channel_id: i16) -> Option<PsdChannelKind> {
match channel_id {
0 => Some(PsdChannelKind::Red),
1 => Some(PsdChannelKind::Green),
2 => Some(PsdChannelKind::Blue),
-1 => Some(PsdChannelKind::TransparencyMask),
-2 => Some(PsdChannelKind::UserSuppliedLayerMask),
-3 => Some(PsdChannelKind::RealUserSuppliedLayerMask),
_ => None,
}
}
pub fn rgba_offset(self) -> Result<usize, String> {
match self {
PsdChannelKind::Red => Ok(0),
PsdChannelKind::Green => Ok(1),
PsdChannelKind::Blue => Ok(2),
PsdChannelKind::TransparencyMask => Ok(3),
_ => Err(format!("{:#?} is not an RGBA channel", &self)),
}
}
}