Skip to content

Commit 450787d

Browse files
authored
Merge pull request #1419 from kayru/allow_tga_a8_as_l8
Allow loading alpha-only TGAs as L8
2 parents 3c583a1 + 46997a7 commit 450787d

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/codecs/tga/decoder.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub struct TgaDecoder<R> {
5454

5555
image_type: ImageType,
5656
color_type: ColorType,
57+
original_color_type: Option<ExtendedColorType>,
5758

5859
header: Header,
5960
color_map: Option<ColorMap>,
@@ -76,6 +77,7 @@ impl<R: Read + Seek> TgaDecoder<R> {
7677

7778
image_type: ImageType::Unknown,
7879
color_type: ColorType::L8,
80+
original_color_type: None,
7981

8082
header: Header::default(),
8183
color_map: None,
@@ -153,6 +155,11 @@ impl<R: Read + Seek> TgaDecoder<R> {
153155
(0, 24, true) => self.color_type = ColorType::Rgb8,
154156
(8, 8, false) => self.color_type = ColorType::La8,
155157
(0, 8, false) => self.color_type = ColorType::L8,
158+
(8, 0, false) => {
159+
// alpha-only image is treated as L8
160+
self.color_type = ColorType::L8;
161+
self.original_color_type = Some(ExtendedColorType::A8);
162+
},
156163
_ => {
157164
return Err(ImageError::Unsupported(
158165
UnsupportedError::from_format_and_kind(
@@ -376,6 +383,10 @@ impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for TgaDecoder<R> {
376383
self.color_type
377384
}
378385

386+
fn original_color_type(&self) -> ExtendedColorType {
387+
self.original_color_type.unwrap_or_else(|| self.color_type().into())
388+
}
389+
379390
fn scanline_bytes(&self) -> u64 {
380391
// This cannot overflow because TGA has a maximum width of u16::MAX_VALUE and
381392
// `bytes_per_pixel` is a u8.

src/color.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ impl ColorType {
9191
/// decoding from and encoding to such an image format.
9292
#[derive(Copy, PartialEq, Eq, Debug, Clone, Hash)]
9393
pub enum ExtendedColorType {
94+
/// Pixel is 8-bit alpha
95+
A8,
9496
/// Pixel is 1-bit luminance
9597
L1,
9698
/// Pixel is 1-bit luminance with an alpha channel
@@ -152,6 +154,7 @@ impl ExtendedColorType {
152154
/// an opaque datum by the library.
153155
pub fn channel_count(self) -> u8 {
154156
match self {
157+
ExtendedColorType::A8 |
155158
ExtendedColorType::L1 |
156159
ExtendedColorType::L2 |
157160
ExtendedColorType::L4 |

0 commit comments

Comments
 (0)