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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#![deny(missing_docs)]
use std::collections::HashMap;
use std::ops::Deref;
use thiserror::Error;
use sections::file_header_section::FileHeaderSectionError;
use sections::image_data_section::ImageDataSectionError;
use sections::image_resources_section::ImageResourcesSectionError;
use sections::layer_and_mask_information_section::layer::PsdLayerError;
use crate::psd_channel::IntoRgba;
pub use crate::psd_channel::{PsdChannelCompression, PsdChannelKind};
pub use crate::sections::file_header_section::{ColorMode, PsdDepth};
use crate::sections::image_data_section::ChannelBytes;
use crate::sections::image_data_section::ImageDataSection;
pub use crate::sections::image_resources_section::ImageResource;
use crate::sections::image_resources_section::ImageResourcesSection;
pub use crate::sections::image_resources_section::{DescriptorField, UnitFloatStructure};
pub use crate::sections::layer_and_mask_information_section::layer::PsdGroup;
pub use crate::sections::layer_and_mask_information_section::layer::PsdLayer;
use crate::sections::layer_and_mask_information_section::LayerAndMaskInformationSection;
use crate::sections::MajorSections;
use self::sections::file_header_section::FileHeaderSection;
mod blend;
mod psd_channel;
mod render;
mod sections;
#[derive(PartialEq, Debug, Error)]
#[non_exhaustive]
pub enum PsdError {
#[error("Failed to parse PSD header: '{0}'.")]
HeaderError(FileHeaderSectionError),
#[error("Failed to parse PSD layer: '{0}'.")]
LayerError(PsdLayerError),
#[error("Failed to parse PSD data section: '{0}'.")]
ImageError(ImageDataSectionError),
#[error("Failed to parse PSD resource section: '{0}'.")]
ResourceError(ImageResourcesSectionError),
}
#[derive(Debug)]
pub struct Psd {
file_header_section: FileHeaderSection,
image_resources_section: ImageResourcesSection,
layer_and_mask_information_section: LayerAndMaskInformationSection,
image_data_section: ImageDataSection,
}
impl Psd {
pub fn from_bytes(bytes: &[u8]) -> Result<Psd, PsdError> {
let major_sections = MajorSections::from_bytes(bytes).map_err(PsdError::HeaderError)?;
let file_header_section = FileHeaderSection::from_bytes(major_sections.file_header)
.map_err(PsdError::HeaderError)?;
let psd_width = file_header_section.width.0;
let psd_height = file_header_section.height.0;
let channel_count = file_header_section.channel_count.count();
let layer_and_mask_information_section = LayerAndMaskInformationSection::from_bytes(
major_sections.layer_and_mask,
psd_width,
psd_height,
)
.map_err(PsdError::LayerError)?;
let image_data_section = ImageDataSection::from_bytes(
major_sections.image_data,
file_header_section.depth,
psd_height,
channel_count,
)
.map_err(PsdError::ImageError)?;
let image_resources_section =
ImageResourcesSection::from_bytes(major_sections.image_resources)
.map_err(PsdError::ResourceError)?;
Ok(Psd {
file_header_section,
image_resources_section,
layer_and_mask_information_section,
image_data_section,
})
}
}
impl Psd {
pub fn width(&self) -> u32 {
self.file_header_section.width.0
}
pub fn height(&self) -> u32 {
self.file_header_section.height.0
}
pub fn depth(&self) -> PsdDepth {
self.file_header_section.depth
}
pub fn color_mode(&self) -> ColorMode {
self.file_header_section.color_mode
}
}
impl Psd {
pub fn layers(&self) -> &Vec<PsdLayer> {
&self.layer_and_mask_information_section.layers
}
pub fn layer_by_name(&self, name: &str) -> Option<&PsdLayer> {
self.layer_and_mask_information_section
.layers
.item_by_name(name)
}
pub fn layer_by_idx(&self, idx: usize) -> &PsdLayer {
self.layer_and_mask_information_section
.layers
.get(idx)
.unwrap()
}
pub fn groups(&self) -> &HashMap<u32, PsdGroup> {
&self.layer_and_mask_information_section.groups
}
pub fn group_ids_in_order(&self) -> &Vec<u32> {
self.layer_and_mask_information_section
.groups
.group_ids_in_order()
}
pub fn get_group_sub_layers(&self, id: &u32) -> Option<&[PsdLayer]> {
match self.groups().get(id) {
Some(group) => Some(
&self.layer_and_mask_information_section.layers.deref()
[group.contained_layers.clone()],
),
None => None,
}
}
pub fn flatten_layers_rgba(
&self,
filter: &dyn Fn((usize, &PsdLayer)) -> bool,
) -> Result<Vec<u8>, PsdError> {
if self.layers().is_empty() {
return Ok(self.rgba());
}
let layers_to_flatten_top_down: Vec<&PsdLayer> = self
.layers()
.iter()
.enumerate()
.filter(|(_, layer)| (layer.opacity > 0 && layer.visible) || layer.clipping_mask)
.filter(|(idx, layer)| filter((*idx, layer)))
.map(|(_, layer)| layer)
.collect();
let pixel_count = self.width() * self.height();
if layers_to_flatten_top_down.is_empty() {
return Ok(vec![0; pixel_count as usize * 4]);
}
let renderer = render::Renderer::new(&layers_to_flatten_top_down, self.width() as usize);
let mut flattened_pixels = Vec::with_capacity((pixel_count * 4) as usize);
for pixel_idx in 0..pixel_count as usize {
let left = pixel_idx % self.width() as usize;
let top = pixel_idx / self.width() as usize;
let pixel_coord = (left, top);
let blended_pixel = renderer.flattened_pixel(pixel_coord);
flattened_pixels.push(blended_pixel[0]);
flattened_pixels.push(blended_pixel[1]);
flattened_pixels.push(blended_pixel[2]);
flattened_pixels.push(blended_pixel[3]);
}
Ok(flattened_pixels)
}
}
impl Psd {
pub fn rgba(&self) -> Vec<u8> {
self.generate_rgba()
}
pub fn compression(&self) -> &PsdChannelCompression {
&self.image_data_section.compression
}
}
impl Psd {
pub fn resources(&self) -> &Vec<ImageResource> {
&self.image_resources_section.resources
}
}
impl IntoRgba for Psd {
fn rgba_idx(&self, idx: usize) -> usize {
idx
}
fn red(&self) -> &ChannelBytes {
&self.image_data_section.red
}
fn green(&self) -> Option<&ChannelBytes> {
match self.color_mode() {
ColorMode::Grayscale => None,
_ => self.image_data_section.green.as_ref(),
}
}
fn blue(&self) -> Option<&ChannelBytes> {
self.image_data_section.blue.as_ref()
}
fn alpha(&self) -> Option<&ChannelBytes> {
self.image_data_section.alpha.as_ref()
}
fn psd_width(&self) -> u32 {
self.width()
}
fn psd_height(&self) -> u32 {
self.height()
}
}
#[cfg(test)]
mod tests {
use crate::sections::file_header_section::FileHeaderSectionError;
use super::*;
#[test]
fn psd_signature_fail() {
let psd = include_bytes!("../tests/fixtures/green-1x1.png");
let err = Psd::from_bytes(psd).expect_err("Psd::from_bytes() didn't catch the PNG file");
assert_eq!(
err,
PsdError::HeaderError(FileHeaderSectionError::InvalidSignature {})
);
}
}