Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion 8 src/ImageSharp/Formats/Tga/TgaDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,9 @@ private TgaImageOrigin ReadFileHeader(BufferedReadStream stream)
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");
Expand All @@ -949,4 +951,8 @@ private TgaImageOrigin ReadFileHeader(BufferedReadStream stream)
// 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 changes: 1 addition & 4 deletions 5 src/ImageSharp/Formats/Tga/TgaFileHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ public 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)
{
Expand Down
18 changes: 14 additions & 4 deletions 18 tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -724,10 +723,8 @@ public void TgaDecoder_CanDecode_WhenAlphaBitsNotSet<TPixel>(TestImageProvider<T
{
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);
}
}

Expand Down Expand Up @@ -771,6 +768,19 @@ public void TgaDecoder_Decode_Resize<TPixel>(TestImageProvider<TPixel> provider)
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)]
Expand Down
2 changes: 2 additions & 0 deletions 2 tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,8 @@ public static class Tga

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
Expand Down

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions 4 tests/Images/Input/Tga/32bit_no_alphabits.tga
Git LFS file not shown
4 changes: 2 additions & 2 deletions 4 tests/Images/Input/Tga/32bit_rle_no_alphabits.tga
Git LFS file not shown
3 changes: 3 additions & 0 deletions 3 tests/Images/Input/Tga/issues/Issue2629.tga
Git LFS file not shown
Morty Proxy This is a proxified and sanitized view of the page, visit original site.