| 1 |
use std::io; |
| 2 |
use std::str; |
| 3 |
use std::string; |
| 4 |
|
| 5 |
use quick_error::quick_error; |
| 6 |
|
| 7 |
use crate::decoder::ChunkType; |
| 8 |
use crate::tags::{ |
| 9 |
CompressionMethod, PhotometricInterpretation, PlanarConfiguration, SampleFormat, Tag, |
| 10 |
}; |
| 11 |
use crate::ColorType; |
| 12 |
|
| 13 |
quick_error! { |
| 14 |
/// Tiff error kinds. |
| 15 |
#[derive(Debug)] |
| 16 |
pub enum TiffError { |
| 17 |
/// The Image is not formatted properly. |
| 18 |
FormatError(err: TiffFormatError) { |
| 19 |
display("format error: {err}") |
| 20 |
from() |
| 21 |
source(err) |
| 22 |
} |
| 23 |
/// The Decoder does not support features required by the image. |
| 24 |
UnsupportedError(err: TiffUnsupportedError) { |
| 25 |
display("unsupported error: {err}") |
| 26 |
from() |
| 27 |
source(err) |
| 28 |
} |
| 29 |
/// An I/O Error occurred while decoding the image. |
| 30 |
IoError(err: io::Error) { |
| 31 |
display("{err}") |
| 32 |
from() |
| 33 |
source(err) |
| 34 |
} |
| 35 |
/// The Limits of the Decoder is exceeded. |
| 36 |
LimitsExceeded { |
| 37 |
display("decoder limits exceeded") |
| 38 |
} |
| 39 |
/// An integer conversion to or from a platform size failed. |
| 40 |
IntSizeError { |
| 41 |
display("platform or format size limits exceeded") |
| 42 |
} |
| 43 |
/// The image does not support the requested operation |
| 44 |
UsageError(err: UsageError) { |
| 45 |
display("usage error: {err}") |
| 46 |
from() |
| 47 |
source(err) |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
quick_error! { |
| 53 |
/// The image is not formatted properly. |
| 54 |
/// |
| 55 |
/// This indicates that the encoder producing the image might behave incorrectly or that the |
| 56 |
/// input file has been corrupted. |
| 57 |
#[derive(Debug, Clone, PartialEq)] |
| 58 |
#[non_exhaustive] |
| 59 |
pub enum TiffFormatError { |
| 60 |
TiffSignatureNotFound { |
| 61 |
display("TIFF signature not found") |
| 62 |
} |
| 63 |
TiffSignatureInvalid { |
| 64 |
display("TIFF signature invalid") |
| 65 |
} |
| 66 |
ImageFileDirectoryNotFound { |
| 67 |
display("image file directory not found") |
| 68 |
} |
| 69 |
InconsistentSizesEncountered { |
| 70 |
display("inconsistent sizes encountered") |
| 71 |
} |
| 72 |
InvalidDimensions(width: u32, height: u32) { |
| 73 |
display("invalid dimensions: {width}x{height}") |
| 74 |
} |
| 75 |
InvalidTag { |
| 76 |
display("image contains invalid tag") |
| 77 |
} |
| 78 |
InvalidTagValueType(tag: Tag) { |
| 79 |
display("tag `{tag:?}` did not have the expected value type") |
| 80 |
} |
| 81 |
RequiredTagNotFound(tag: Tag) { |
| 82 |
display("required tag `{tag:?}` not found") |
| 83 |
} |
| 84 |
UnknownPredictor(pred: u16) { |
| 85 |
display("unknown predictor “{pred}” encountered") |
| 86 |
} |
| 87 |
UnknownPlanarConfiguration(cfg: u16) { |
| 88 |
display("unknown planar configuration “{cfg}”") |
| 89 |
} |
| 90 |
InvalidTypeForTag { |
| 91 |
display("tag has invalid type") |
| 92 |
} |
| 93 |
StripTileTagConflict { |
| 94 |
display("file should contain either (StripByteCounts and StripOffsets) or (TileByteCounts and TileOffsets), other combination was found") |
| 95 |
} |
| 96 |
CycleInOffsets { |
| 97 |
display("file contained a cycle in the list of IFDs") |
| 98 |
} |
| 99 |
SamplesPerPixelIsZero { |
| 100 |
display("samples per pixel is zero") |
| 101 |
} |
| 102 |
CompressedDataCorrupt(message: String) { |
| 103 |
display("compressed data is corrupt: {message}") |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
quick_error! { |
| 109 |
/// The Decoder does not support features required by the image. |
| 110 |
/// |
| 111 |
/// This only captures known failures for which the standard either does not require support or an |
| 112 |
/// implementation has been planned but not yet completed. Some variants may become unused over |
| 113 |
/// time and will then get deprecated before being removed. |
| 114 |
#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 115 |
#[non_exhaustive] |
| 116 |
pub enum TiffUnsupportedError { |
| 117 |
FloatingPointPredictor(color_type: ColorType) { |
| 118 |
display("floating point predictor for {color_type:?} is unsupported") |
| 119 |
} |
| 120 |
HorizontalPredictor(color_type: ColorType) { |
| 121 |
display("horizontal predictor for {color_type:?} is unsupported") |
| 122 |
} |
| 123 |
InconsistentBitsPerSample(bits_per_sample: Vec<u8>) { |
| 124 |
display("inconsistent bits per sample: {bits_per_sample:?}") |
| 125 |
} |
| 126 |
InterpretationWithBits(interpretation: PhotometricInterpretation, bits_per_sample: Vec<u8>) { |
| 127 |
display("Photometric interpretation {interpretation:?} with bits per sample {bits_per_sample:?} is unsupported") |
| 128 |
} |
| 129 |
UnknownInterpretation { |
| 130 |
display("unknown photometric interpretation") |
| 131 |
} |
| 132 |
UnknownCompressionMethod { |
| 133 |
display("unknown compression method") |
| 134 |
} |
| 135 |
UnsupportedCompressionMethod(method: CompressionMethod) { |
| 136 |
display("compression method {method:?} is unsupported") |
| 137 |
} |
| 138 |
UnsupportedSampleDepth(depth: u8) { |
| 139 |
display("{depth} samples per pixel is unsupported") |
| 140 |
} |
| 141 |
UnsupportedSampleFormat(sample_format: Vec<SampleFormat>) { |
| 142 |
display("sample format {sample_format:?} is unsupported") |
| 143 |
} |
| 144 |
UnsupportedColorType(color_type: ColorType) { |
| 145 |
display("color type {color_type:?} is unsupported") |
| 146 |
} |
| 147 |
UnsupportedBitsPerChannel(bits_per_channel: u8) { |
| 148 |
display("{bits_per_channel} bits per channel not supported") |
| 149 |
} |
| 150 |
UnsupportedPlanarConfig(planar: Option<PlanarConfiguration>) { |
| 151 |
display("unsupported planar configuration “{planar:?}”") |
| 152 |
} |
| 153 |
UnsupportedDataType { |
| 154 |
display("unsupported data type.") |
| 155 |
} |
| 156 |
UnsupportedInterpretation(interpretation: PhotometricInterpretation) { |
| 157 |
display("unsupported photometric interpretation \"{interpretation:?}\"") |
| 158 |
} |
| 159 |
MisalignedTileBoundaries { |
| 160 |
display("tile rows are not aligned to byte boundaries") |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
quick_error! { |
| 166 |
/// User attempted to use the Decoder in a way that is incompatible with a specific image. |
| 167 |
/// |
| 168 |
/// For example: attempting to read a tile from a stripped image. |
| 169 |
#[derive(Debug)] |
| 170 |
#[non_exhaustive] |
| 171 |
pub enum UsageError { |
| 172 |
InvalidChunkType(expected: ChunkType, actual: ChunkType) { |
| 173 |
display("requested operation is only valid for images with chunk encoding of type {expected:?} but got {actual:?}") |
| 174 |
} |
| 175 |
InvalidChunkIndex(index: u32) { |
| 176 |
display("invalid chunk index ({index}) requested") |
| 177 |
} |
| 178 |
PredictorCompressionMismatch { |
| 179 |
display("requested predictor is not compatible with the requested compression") |
| 180 |
} |
| 181 |
PredictorIncompatible { |
| 182 |
display("the requested predictor is not compatible with the image's format") |
| 183 |
} |
| 184 |
PredictorUnavailable { |
| 185 |
display("the requested predictor is not available") |
| 186 |
} |
| 187 |
InsufficientOutputBufferSize { needed: usize, provided: usize } { |
| 188 |
display("the borrowed output buffer is not large enough for the decoded data, needed {needed} but have {provided}") |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
impl From<str::Utf8Error> for TiffError { |
| 194 |
fn from(_err: str::Utf8Error) -> TiffError { |
| 195 |
TiffError::FormatError(TiffFormatError::InvalidTag) |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
impl From<string::FromUtf8Error> for TiffError { |
| 200 |
fn from(_err: string::FromUtf8Error) -> TiffError { |
| 201 |
TiffError::FormatError(TiffFormatError::InvalidTag) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
impl From<std::num::TryFromIntError> for TiffError { |
| 206 |
fn from(_err: std::num::TryFromIntError) -> TiffError { |
| 207 |
TiffError::IntSizeError |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
#[cfg(feature = "lzw")] |
| 212 |
impl From<weezl::LzwError> for TiffError { |
| 213 |
fn from(err: weezl::LzwError) -> TiffError { |
| 214 |
TiffError::FormatError(TiffFormatError::CompressedDataCorrupt(err.to_string())) |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
#[cfg(feature = "jpeg")] |
| 219 |
impl From<zune_jpeg::errors::DecodeErrors> for TiffError { |
| 220 |
fn from(err: zune_jpeg::errors::DecodeErrors) -> Self { |
| 221 |
TiffError::FormatError(TiffFormatError::CompressedDataCorrupt(err.to_string())) |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
/// Result of an image decoding/encoding process |
| 226 |
pub type TiffResult<T> = Result<T, TiffError>; |