Browse Source

Merge pull request #2643 from SixLabors/bp/Issue2629

TGA: Treat 32 bit True Color images always as transparent
pull/2648/head
Brian Popow 2 years ago
committed by GitHub
parent
commit
f926822425
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      src/ImageSharp/Formats/Tga/TgaDecoderCore.cs
  2. 5
      src/ImageSharp/Formats/Tga/TgaFileHeader.cs
  3. 18
      tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs
  4. 2
      tests/ImageSharp.Tests/TestImages.cs
  5. 3
      tests/Images/External/ReferenceOutput/TgaDecoderTests/TgaDecoder_CanDecode_WhenAlphaBitsNotSet_Rgba32_32bit_no_alphabits.png
  6. 3
      tests/Images/External/ReferenceOutput/TgaDecoderTests/TgaDecoder_CanDecode_WhenAlphaBitsNotSet_Rgba32_32bit_rle_no_alphabits.png
  7. 4
      tests/Images/Input/Tga/32bit_no_alphabits.tga
  8. 4
      tests/Images/Input/Tga/32bit_rle_no_alphabits.tga
  9. 3
      tests/Images/Input/Tga/issues/Issue2629.tga

8
src/ImageSharp/Formats/Tga/TgaDecoderCore.cs

@ -937,7 +937,9 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
this.tgaMetadata = this.metadata.GetTgaMetadata();
this.tgaMetadata.BitsPerPixel = (TgaBitsPerPixel)this.fileHeader.PixelDepth;
int alphaBits = this.fileHeader.ImageDescriptor & 0xf;
// TrueColor images with 32 bits per pixel are assumed to always have 8 bit alpha channel,
// because some encoders do not set correctly the alpha bits in the image descriptor.
int alphaBits = this.IsTrueColor32BitPerPixel(this.tgaMetadata.BitsPerPixel) ? 8 : this.fileHeader.ImageDescriptor & 0xf;
if (alphaBits is not 0 and not 1 and not 8)
{
TgaThrowHelper.ThrowInvalidImageContentException("Invalid alpha channel bits");
@ -949,4 +951,8 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
// Bits 4 and 5 describe the image origin.
return (TgaImageOrigin)((this.fileHeader.ImageDescriptor & 0x30) >> 4);
}
private bool IsTrueColor32BitPerPixel(TgaBitsPerPixel bitsPerPixel) => bitsPerPixel == TgaBitsPerPixel.Pixel32 &&
(this.fileHeader.ImageType == TgaImageType.TrueColor ||
this.fileHeader.ImageType == TgaImageType.RleTrueColor);
}

5
src/ImageSharp/Formats/Tga/TgaFileHeader.cs

@ -131,10 +131,7 @@ internal readonly struct TgaFileHeader
/// </summary>
public byte ImageDescriptor { get; }
public static TgaFileHeader Parse(Span<byte> data)
{
return MemoryMarshal.Cast<byte, TgaFileHeader>(data)[0];
}
public static TgaFileHeader Parse(Span<byte> data) => MemoryMarshal.Cast<byte, TgaFileHeader>(data)[0];
public void WriteTo(Span<byte> buffer)
{

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

@ -1,7 +1,6 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics.X86;
using Microsoft.DotNet.RemoteExecutor;
using SixLabors.ImageSharp.Formats;
@ -724,10 +723,8 @@ public class TgaDecoderTests
{
using (Image<TPixel> image = provider.GetImage(TgaDecoder.Instance))
{
// Using here the reference output instead of the the reference decoder,
// because the reference decoder does not ignore the alpha data here.
image.DebugSave(provider);
image.CompareToReferenceOutput(ImageComparer.Exact, provider);
ImageComparingUtils.CompareWithReferenceDecoder(provider, image);
}
}
@ -771,6 +768,19 @@ public class TgaDecoderTests
appendPixelTypeToFileName: false);
}
// https://github.com/SixLabors/ImageSharp/issues/2629
[Theory]
[WithFile(Issue2629, PixelTypes.Rgba32)]
public void TgaDecoder_CanDecode_Issue2629<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage(TgaDecoder.Instance))
{
image.DebugSave(provider);
ImageComparingUtils.CompareWithReferenceDecoder(provider, image);
}
}
[Theory]
[WithFile(Bit16BottomLeft, PixelTypes.Rgba32)]
[WithFile(Bit24BottomLeft, PixelTypes.Rgba32)]

2
tests/ImageSharp.Tests/TestImages.cs

@ -617,6 +617,8 @@ public static class TestImages
public const string Github_RLE_legacy = "Tga/Github_RLE_legacy.tga";
public const string WhiteStripesPattern = "Tga/whitestripes.png";
public const string Issue2629 = "Tga/issues/Issue2629.tga";
}
public static class Webp

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

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:d3dc0516f656c14b5ffcc40f88d3912f2a8fb310dfbda5836e15847e205919b5
size 1012

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

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:cad24c7e4657f2bc8d8a60ea76397eac0adf8dee5fc81f60bc5bc02dd7eeed8f
size 90589

4
tests/Images/Input/Tga/32bit_no_alphabits.tga

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:0aea1128a1bd7477dfa0d007a1eba25907be24847284c48a5f9fbd61bcea3cf0
size 61522
oid sha256:019315f9dcbe4516ecb15426a45c210d437e9ad152c8e1a0e80abe9449177e12
size 235218

4
tests/Images/Input/Tga/32bit_rle_no_alphabits.tga

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:98a198392bd527523f8649d6126af81e5a588ad7265dc3d48a1da7b5a6cb6ff7
size 230578
oid sha256:33954ae93b4c7d57f52965a9028e97119c546db1da255100c2903a2760c7479e
size 76870

3
tests/Images/Input/Tga/issues/Issue2629.tga

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:defc1396481f426a74e8af51ed57f65cbed932f932673ce5a87fa12ea9b460f8
size 32786
Loading…
Cancel
Save