diff --git a/src/dynimage.rs b/src/dynimage.rs index a40cdd9c3b..e4edbe543c 100644 --- a/src/dynimage.rs +++ b/src/dynimage.rs @@ -922,7 +922,10 @@ fn decoder_to_image<'a, I: ImageDecoder<'a>>(decoder: I) -> ImageResult return Err(ImageError::UnsupportedColor(color_type.into())), + _ => return Err(ImageError::Unsupported(UnsupportedError::from_format_and_kind( + ImageFormatHint::Unknown, + UnsupportedErrorKind::Color(color_type.into()), + ))), }; match image { Some(image) => Ok(image), diff --git a/src/farbfeld.rs b/src/farbfeld.rs index ffc693d168..0e1e252d8e 100644 --- a/src/farbfeld.rs +++ b/src/farbfeld.rs @@ -23,7 +23,7 @@ use std::io::{self, Seek, SeekFrom, Read, Write, BufReader, BufWriter}; use byteorder::{BigEndian, ByteOrder, NativeEndian}; use crate::color::ColorType; -use crate::error::{EncodingError, DecodingError, ImageError, ImageResult}; +use crate::error::{EncodingError, DecodingError, ImageError, ImageResult, UnsupportedError, UnsupportedErrorKind}; use crate::image::{self, ImageDecoder, ImageDecoderExt, ImageEncoder, ImageFormat, Progress}; /// farbfeld Reader @@ -261,7 +261,10 @@ impl ImageEncoder for FarbfeldEncoder { color_type: ColorType, ) -> ImageResult<()> { if color_type != ColorType::Rgba16 { - return Err(ImageError::UnsupportedColor(color_type.into())); + return Err(ImageError::Unsupported(UnsupportedError::from_format_and_kind( + ImageFormat::Farbfeld.into(), + UnsupportedErrorKind::Color(color_type.into()), + ))); } self.encode(buf, width, height) diff --git a/src/flat.rs b/src/flat.rs index 581dbc5006..1be1dc9923 100644 --- a/src/flat.rs +++ b/src/flat.rs @@ -49,7 +49,7 @@ use num_traits::Zero; use crate::ImageBuffer; use crate::color::ColorType; -use crate::error::{ImageError, ParameterError, ParameterErrorKind}; +use crate::error::{ImageError, ImageFormatHint, ParameterError, ParameterErrorKind, UnsupportedError, UnsupportedErrorKind}; use crate::image::{GenericImage, GenericImageView}; use crate::traits::Pixel; @@ -1379,9 +1379,11 @@ impl From for ImageError { Error::TooLarge => ImageError::Parameter( ParameterError::from_kind(ParameterErrorKind::DimensionMismatch) ), - Error::WrongColor(color) => ImageError::UnsupportedColor(color.into()), Error::NormalFormRequired(form) => ImageError::FormatError( format!("Required sample buffer in normal form {:?}", form)), + Error::WrongColor(color) => ImageError::Unsupported(UnsupportedError::from_format_and_kind( + ImageFormatHint::Unknown, + UnsupportedErrorKind::Color(color.into()))), } } } diff --git a/src/png.rs b/src/png.rs index e3a3dd4d6a..dcb26dca20 100644 --- a/src/png.rs +++ b/src/png.rs @@ -11,7 +11,7 @@ use std::io::{self, Read, Write}; use crate::color::{ColorType, ExtendedColorType}; use crate::error::{ - DecodingError, ImageError, ImageResult, LimitError, LimitErrorKind, ParameterError, ParameterErrorKind + DecodingError, ImageError, ImageResult, LimitError, LimitErrorKind, ParameterError, ParameterErrorKind, UnsupportedError, UnsupportedErrorKind }; use crate::image::{ImageDecoder, ImageEncoder, ImageFormat}; @@ -99,6 +99,13 @@ pub struct PngDecoder { impl PngDecoder { /// Creates a new decoder that decodes from the stream ```r``` pub fn new(r: R) -> ImageResult> { + fn unsupported_color(ect: ExtendedColorType) -> ImageError { + ImageError::Unsupported(UnsupportedError::from_format_and_kind( + ImageFormat::Png.into(), + UnsupportedErrorKind::Color(ect), + )) + } + let limits = png::Limits { bytes: usize::max_value(), }; @@ -120,34 +127,34 @@ impl PngDecoder { (png::ColorType::RGBA, png::BitDepth::Sixteen) => ColorType::Rgba16, (png::ColorType::Grayscale, png::BitDepth::One) => - return Err(ImageError::UnsupportedColor(ExtendedColorType::L1)), + return Err(unsupported_color(ExtendedColorType::L1)), (png::ColorType::GrayscaleAlpha, png::BitDepth::One) => - return Err(ImageError::UnsupportedColor(ExtendedColorType::La1)), + return Err(unsupported_color(ExtendedColorType::La1)), (png::ColorType::RGB, png::BitDepth::One) => - return Err(ImageError::UnsupportedColor(ExtendedColorType::Rgb1)), + return Err(unsupported_color(ExtendedColorType::Rgb1)), (png::ColorType::RGBA, png::BitDepth::One) => - return Err(ImageError::UnsupportedColor(ExtendedColorType::Rgba1)), + return Err(unsupported_color(ExtendedColorType::Rgba1)), (png::ColorType::Grayscale, png::BitDepth::Two) => - return Err(ImageError::UnsupportedColor(ExtendedColorType::L2)), + return Err(unsupported_color(ExtendedColorType::L2)), (png::ColorType::GrayscaleAlpha, png::BitDepth::Two) => - return Err(ImageError::UnsupportedColor(ExtendedColorType::La2)), + return Err(unsupported_color(ExtendedColorType::La2)), (png::ColorType::RGB, png::BitDepth::Two) => - return Err(ImageError::UnsupportedColor(ExtendedColorType::Rgb2)), + return Err(unsupported_color(ExtendedColorType::Rgb2)), (png::ColorType::RGBA, png::BitDepth::Two) => - return Err(ImageError::UnsupportedColor(ExtendedColorType::Rgba2)), + return Err(unsupported_color(ExtendedColorType::Rgba2)), (png::ColorType::Grayscale, png::BitDepth::Four) => - return Err(ImageError::UnsupportedColor(ExtendedColorType::L4)), + return Err(unsupported_color(ExtendedColorType::L4)), (png::ColorType::GrayscaleAlpha, png::BitDepth::Four) => - return Err(ImageError::UnsupportedColor(ExtendedColorType::La4)), + return Err(unsupported_color(ExtendedColorType::La4)), (png::ColorType::RGB, png::BitDepth::Four) => - return Err(ImageError::UnsupportedColor(ExtendedColorType::Rgb4)), + return Err(unsupported_color(ExtendedColorType::Rgb4)), (png::ColorType::RGBA, png::BitDepth::Four) => - return Err(ImageError::UnsupportedColor(ExtendedColorType::Rgba4)), + return Err(unsupported_color(ExtendedColorType::Rgba4)), (png::ColorType::Indexed, bits) => - return Err(ImageError::UnsupportedColor(ExtendedColorType::Unknown(bits as u8))), + return Err(unsupported_color(ExtendedColorType::Unknown(bits as u8))), }; Ok(PngDecoder { color_type, reader }) @@ -220,7 +227,10 @@ impl PNGEncoder { ColorType::Rgb16 => (png::ColorType::RGB,png::BitDepth::Sixteen), ColorType::Rgba8 => (png::ColorType::RGBA, png::BitDepth::Eight), ColorType::Rgba16 => (png::ColorType::RGBA,png::BitDepth::Sixteen), - _ => return Err(ImageError::UnsupportedColor(color.into())), + _ => return Err(ImageError::Unsupported(UnsupportedError::from_format_and_kind( + ImageFormat::Png.into(), + UnsupportedErrorKind::Color(color.into()), + ))), }; let mut encoder = png::Encoder::new(self.w, width, height);