zune_psd/
errors.rs

1/*
2 * Copyright (c) 2023.
3 *
4 * This software is free software;
5 *
6 * You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license
7 */
8
9use core::fmt::{Debug, Formatter};
10
11use zune_core::bytestream::ZByteIoError;
12
13use crate::constants::{ColorModes, PSD_IDENTIFIER_BE};
14
15/// PSDDecodeErrors that can occur during PSD decoding
16pub enum PSDDecodeErrors {
17    WrongMagicBytes(u32),
18    UnsupportedFileType(u16),
19    UnsupportedChannelCount(u16),
20    UnsupportedBitDepth(u16),
21    UnsupportedColorFormat(Option<ColorModes>),
22    LargeDimensions(usize, usize),
23    ZeroDimensions,
24    UnknownCompression,
25    Generic(&'static str),
26    IoErrors(ZByteIoError),
27    BadRLE
28}
29
30impl Debug for PSDDecodeErrors {
31    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
32        match self {
33            PSDDecodeErrors::Generic(reason) => {
34                writeln!(f, "{reason}")
35            }
36            PSDDecodeErrors::WrongMagicBytes(bytes) => {
37                writeln!(
38                    f,
39                    "Expected {:?} but found  {:?}, not a PSD image",
40                    PSD_IDENTIFIER_BE.to_be_bytes(),
41                    bytes.to_be_bytes()
42                )
43            }
44            PSDDecodeErrors::UnsupportedFileType(version) => {
45                writeln!(
46                    f,
47                    "Unsupported file version {version:?}, known versions are 1",
48                )
49            }
50            PSDDecodeErrors::UnsupportedChannelCount(channels) => {
51                writeln!(f, "Unsupported channel count {channels:?}")
52            }
53            PSDDecodeErrors::UnsupportedBitDepth(depth) => {
54                writeln!(
55                    f,
56                    "Unsupported bit depth {depth:?}, supported depths are 8 and 16",
57                )
58            }
59            PSDDecodeErrors::UnsupportedColorFormat(color) => {
60                if let Some(color) = color {
61                    writeln!(
62                        f,
63                        "Unsupported color format  {color:?}, supported formats RGB,CMYK and Grayscale currently",
64                    )
65                } else {
66                    writeln!(f, "Unknown color format")
67                }
68            }
69            PSDDecodeErrors::UnknownCompression => {
70                writeln!(f, "Unknown compression format")
71            }
72            PSDDecodeErrors::BadRLE => {
73                writeln!(f, "Bad RLE")
74            }
75            PSDDecodeErrors::LargeDimensions(supported, found) => {
76                writeln!(
77                    f,
78                    "Too large dimensions, supported {supported} but found {found}",
79                )
80            }
81            PSDDecodeErrors::ZeroDimensions => {
82                writeln!(f, "Zero found where not expected")
83            }
84            PSDDecodeErrors::IoErrors(e) => {
85                writeln!(f, "I/O error :{:?}", e)
86            }
87        }
88    }
89}
90
91impl From<&'static str> for PSDDecodeErrors {
92    fn from(r: &'static str) -> Self {
93        Self::Generic(r)
94    }
95}
96
97impl From<ZByteIoError> for PSDDecodeErrors {
98    fn from(r: ZByteIoError) -> Self {
99        Self::IoErrors(r)
100    }
101}