Skip to content

Commit f926822

Browse files
authored
Merge pull request #2643 from SixLabors/bp/Issue2629
TGA: Treat 32 bit True Color images always as transparent
2 parents 77cf848 + 10b27a3 commit f926822

File tree

9 files changed

+31
-19
lines changed

9 files changed

+31
-19
lines changed

src/ImageSharp/Formats/Tga/TgaDecoderCore.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,9 @@ private TgaImageOrigin ReadFileHeader(BufferedReadStream stream)
937937
this.tgaMetadata = this.metadata.GetTgaMetadata();
938938
this.tgaMetadata.BitsPerPixel = (TgaBitsPerPixel)this.fileHeader.PixelDepth;
939939

940-
int alphaBits = this.fileHeader.ImageDescriptor & 0xf;
940+
// TrueColor images with 32 bits per pixel are assumed to always have 8 bit alpha channel,
941+
// because some encoders do not set correctly the alpha bits in the image descriptor.
942+
int alphaBits = this.IsTrueColor32BitPerPixel(this.tgaMetadata.BitsPerPixel) ? 8 : this.fileHeader.ImageDescriptor & 0xf;
941943
if (alphaBits is not 0 and not 1 and not 8)
942944
{
943945
TgaThrowHelper.ThrowInvalidImageContentException("Invalid alpha channel bits");
@@ -949,4 +951,8 @@ private TgaImageOrigin ReadFileHeader(BufferedReadStream stream)
949951
// Bits 4 and 5 describe the image origin.
950952
return (TgaImageOrigin)((this.fileHeader.ImageDescriptor & 0x30) >> 4);
951953
}
954+
955+
private bool IsTrueColor32BitPerPixel(TgaBitsPerPixel bitsPerPixel) => bitsPerPixel == TgaBitsPerPixel.Pixel32 &&
956+
(this.fileHeader.ImageType == TgaImageType.TrueColor ||
957+
this.fileHeader.ImageType == TgaImageType.RleTrueColor);
952958
}

src/ImageSharp/Formats/Tga/TgaFileHeader.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,7 @@ public TgaFileHeader(
131131
/// </summary>
132132
public byte ImageDescriptor { get; }
133133

134-
public static TgaFileHeader Parse(Span<byte> data)
135-
{
136-
return MemoryMarshal.Cast<byte, TgaFileHeader>(data)[0];
137-
}
134+
public static TgaFileHeader Parse(Span<byte> data) => MemoryMarshal.Cast<byte, TgaFileHeader>(data)[0];
138135

139136
public void WriteTo(Span<byte> buffer)
140137
{

tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4-
using System.Runtime.InteropServices;
54
using System.Runtime.Intrinsics.X86;
65
using Microsoft.DotNet.RemoteExecutor;
76
using SixLabors.ImageSharp.Formats;
@@ -724,10 +723,8 @@ public void TgaDecoder_CanDecode_WhenAlphaBitsNotSet<TPixel>(TestImageProvider<T
724723
{
725724
using (Image<TPixel> image = provider.GetImage(TgaDecoder.Instance))
726725
{
727-
// Using here the reference output instead of the the reference decoder,
728-
// because the reference decoder does not ignore the alpha data here.
729726
image.DebugSave(provider);
730-
image.CompareToReferenceOutput(ImageComparer.Exact, provider);
727+
ImageComparingUtils.CompareWithReferenceDecoder(provider, image);
731728
}
732729
}
733730

@@ -771,6 +768,19 @@ public void TgaDecoder_Decode_Resize<TPixel>(TestImageProvider<TPixel> provider)
771768
appendPixelTypeToFileName: false);
772769
}
773770

771+
// https://github.com/SixLabors/ImageSharp/issues/2629
772+
[Theory]
773+
[WithFile(Issue2629, PixelTypes.Rgba32)]
774+
public void TgaDecoder_CanDecode_Issue2629<TPixel>(TestImageProvider<TPixel> provider)
775+
where TPixel : unmanaged, IPixel<TPixel>
776+
{
777+
using (Image<TPixel> image = provider.GetImage(TgaDecoder.Instance))
778+
{
779+
image.DebugSave(provider);
780+
ImageComparingUtils.CompareWithReferenceDecoder(provider, image);
781+
}
782+
}
783+
774784
[Theory]
775785
[WithFile(Bit16BottomLeft, PixelTypes.Rgba32)]
776786
[WithFile(Bit24BottomLeft, PixelTypes.Rgba32)]

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ public static class Tga
617617

618618
public const string Github_RLE_legacy = "Tga/Github_RLE_legacy.tga";
619619
public const string WhiteStripesPattern = "Tga/whitestripes.png";
620+
621+
public const string Issue2629 = "Tga/issues/Issue2629.tga";
620622
}
621623

622624
public static class Webp

tests/Images/External/ReferenceOutput/TgaDecoderTests/TgaDecoder_CanDecode_WhenAlphaBitsNotSet_Rgba32_32bit_no_alphabits.png

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/Images/External/ReferenceOutput/TgaDecoderTests/TgaDecoder_CanDecode_WhenAlphaBitsNotSet_Rgba32_32bit_rle_no_alphabits.png

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:0aea1128a1bd7477dfa0d007a1eba25907be24847284c48a5f9fbd61bcea3cf0
3-
size 61522
2+
oid sha256:019315f9dcbe4516ecb15426a45c210d437e9ad152c8e1a0e80abe9449177e12
3+
size 235218
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:98a198392bd527523f8649d6126af81e5a588ad7265dc3d48a1da7b5a6cb6ff7
3-
size 230578
2+
oid sha256:33954ae93b4c7d57f52965a9028e97119c546db1da255100c2903a2760c7479e
3+
size 76870
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:defc1396481f426a74e8af51ed57f65cbed932f932673ce5a87fa12ea9b460f8
3+
size 32786

0 commit comments

Comments
 (0)