1#![cfg(feature = "psd")]
14#![cfg_attr(feature = "docs", doc(cfg(feature = "psd")))]
15
16use zune_core::bytestream::ZByteReaderTrait;
17use zune_core::colorspace::ColorSpace;
18use zune_core::result::DecodingResult;
19pub use zune_psd::*;
20
21use crate::codecs::ImageFormat;
22use crate::errors::ImageErrors;
23use crate::image::Image;
24use crate::metadata::ImageMetadata;
25use crate::traits::DecoderTrait;
26
27impl<T> DecoderTrait for PSDDecoder<T>
28where
29 T: ZByteReaderTrait
30{
31 fn decode(&mut self) -> Result<Image, ImageErrors> {
32 let pixels = self.decode()?;
33
34 let depth = self.bit_depth().unwrap();
35 let (width, height) = self.dimensions().unwrap();
36 let colorspace = self.colorspace().unwrap();
37
38 let mut image = match pixels {
39 DecodingResult::U8(data) => Image::from_u8(&data, width, height, colorspace),
40 DecodingResult::U16(data) => Image::from_u16(&data, width, height, colorspace),
41 _ => unreachable!()
42 };
43 image.metadata.format = Some(ImageFormat::PSD);
45
46 Ok(image)
47 }
48
49 fn dimensions(&self) -> Option<(usize, usize)> {
50 self.dimensions()
51 }
52
53 fn out_colorspace(&self) -> ColorSpace {
54 self.colorspace().unwrap()
55 }
56
57 fn name(&self) -> &'static str {
58 "PSD Decoder"
59 }
60
61 fn is_experimental(&self) -> bool {
62 true
63 }
64
65 fn read_headers(&mut self) -> Result<Option<ImageMetadata>, crate::errors::ImageErrors> {
66 self.decode_headers()
67 .map_err(<errors::PSDDecodeErrors as Into<ImageErrors>>::into)?;
68
69 let (width, height) = self.dimensions().unwrap();
70 let depth = self.bit_depth().unwrap();
71
72 let metadata = ImageMetadata {
73 format: Some(ImageFormat::PSD),
74 colorspace: self.colorspace().unwrap(),
75 depth: depth,
76 width: width,
77 height: height,
78 ..Default::default()
79 };
80
81 Ok(Some(metadata))
82 }
83}
84
85impl From<zune_psd::errors::PSDDecodeErrors> for ImageErrors {
86 fn from(error: zune_psd::errors::PSDDecodeErrors) -> Self {
87 let err = format!("psd: {error:?}");
88
89 ImageErrors::ImageDecodeErrors(err)
90 }
91}