Skip to content

Commit

Permalink
Remove legacy ImageError::UnsupportedColor() constructors from DynIma…
Browse files Browse the repository at this point in the history
…ge, farbfeld, flat, and PNG

Ref: #1134
  • Loading branch information
nabijaczleweli committed Apr 18, 2020
1 parent d87ddd8 commit 2db6935
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
5 changes: 4 additions & 1 deletion src/dynimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,10 @@ fn decoder_to_image<'a, I: ImageDecoder<'a>>(decoder: I) -> ImageResult<DynamicI
let buf = image::decoder_to_vec(decoder)?;
ImageBuffer::from_raw(w, h, buf).map(DynamicImage::ImageLumaA16)
}
_ => 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),
Expand Down
7 changes: 5 additions & 2 deletions src/farbfeld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -261,7 +261,10 @@ impl<W: Write> ImageEncoder for FarbfeldEncoder<W> {
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)
Expand Down
6 changes: 4 additions & 2 deletions src/flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1379,9 +1379,11 @@ impl From<Error> 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()))),
}
}
}
Expand Down
40 changes: 25 additions & 15 deletions src/png.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -99,6 +99,13 @@ pub struct PngDecoder<R: Read> {
impl<R: Read> PngDecoder<R> {
/// Creates a new decoder that decodes from the stream ```r```
pub fn new(r: R) -> ImageResult<PngDecoder<R>> {
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(),
};
Expand All @@ -120,34 +127,34 @@ impl<R: Read> PngDecoder<R> {
(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 })
Expand Down Expand Up @@ -220,7 +227,10 @@ impl<W: Write> PNGEncoder<W> {
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);
Expand Down

0 comments on commit 2db6935

Please sign in to comment.