Skip to content

Commit

Permalink
Merge pull request #1145 from lo48576/feature/tga-detailed-error
Browse files Browse the repository at this point in the history
Use detailed errors for TGA decoder
  • Loading branch information
HeroicKatora authored Apr 19, 2020
2 parents a304885 + 81cf3af commit 947fda9
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/tga/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::convert::TryFrom;
use std::io;
use std::io::{Read, Seek};

use crate::color::ColorType;
use crate::error::{ImageError, ImageResult};
use crate::image::{ImageDecoder, ImageReadBuffer};
use crate::color::{ColorType, ExtendedColorType};
use crate::error::{ImageError, ImageResult, UnsupportedError, UnsupportedErrorKind};
use crate::image::{ImageDecoder, ImageFormat, ImageReadBuffer};

enum ImageType {
NoImageData = 0,
Expand Down Expand Up @@ -221,16 +221,18 @@ impl<R: Read + Seek> TgaDecoder<R> {
/// Loads the color information for the decoder
///
/// To keep things simple, we won't handle bit depths that aren't divisible
/// by 8 and are less than 32.
/// by 8 and are larger than 32.
fn read_color_information(&mut self) -> ImageResult<()> {
if self.header.pixel_depth % 8 != 0 {
return Err(ImageError::UnsupportedError(
"Bit depth must be divisible by 8".to_string(),
));
}
if self.header.pixel_depth > 32 {
return Err(ImageError::UnsupportedError(
"Bit depth must be less than 32".to_string(),
if self.header.pixel_depth % 8 != 0 || self.header.pixel_depth > 32 {
// Bit depth must be divisible by 8, and must be less than or equal
// to 32.
return Err(ImageError::Unsupported(
UnsupportedError::from_format_and_kind(
ImageFormat::Tga.into(),
UnsupportedErrorKind::Color(ExtendedColorType::Unknown(
self.header.pixel_depth,
)),
),
));
}

Expand All @@ -240,8 +242,13 @@ impl<R: Read + Seek> TgaDecoder<R> {
self.header.map_entry_size
} else {
if num_alpha_bits > self.header.pixel_depth {
return Err(ImageError::UnsupportedError(
format!("Color format not supported. Alpha bits: {}", num_alpha_bits),
return Err(ImageError::Unsupported(
UnsupportedError::from_format_and_kind(
ImageFormat::Tga.into(),
UnsupportedErrorKind::Color(ExtendedColorType::Unknown(
self.header.pixel_depth,
)),
),
));
}

Expand All @@ -258,10 +265,14 @@ impl<R: Read + Seek> TgaDecoder<R> {
(8, 8, false) => self.color_type = ColorType::La8,
(0, 8, false) => self.color_type = ColorType::L8,
_ => {
return Err(ImageError::UnsupportedError(format!(
"Color format not supported. Bit depth: {}, Alpha bits: {}",
other_channel_bits, num_alpha_bits
)))
return Err(ImageError::Unsupported(
UnsupportedError::from_format_and_kind(
ImageFormat::Tga.into(),
UnsupportedErrorKind::Color(ExtendedColorType::Unknown(
self.header.pixel_depth,
)),
),
))
}
}
Ok(())
Expand Down

0 comments on commit 947fda9

Please sign in to comment.