Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix run-length encode compression implementation mistake in tga encoder #2172

Merged
merged 2 commits into from
Jul 30, 2022
Merged
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
43 changes: 20 additions & 23 deletions src/ImageSharp/Formats/Tga/TgaEncoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Metadata;
Expand Down Expand Up @@ -73,7 +72,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
this.configuration = image.GetConfiguration();
ImageMetadata metadata = image.Metadata;
TgaMetadata tgaMetadata = metadata.GetTgaMetadata();
this.bitsPerPixel = this.bitsPerPixel ?? tgaMetadata.BitsPerPixel;
this.bitsPerPixel ??= tgaMetadata.BitsPerPixel;

TgaImageType imageType = this.compression is TgaCompression.RunLength ? TgaImageType.RleTrueColor : TgaImageType.TrueColor;
if (this.bitsPerPixel == TgaBitsPerPixel.Pixel8)
Expand Down Expand Up @@ -160,6 +159,8 @@ private void WriteImage<TPixel>(Stream stream, ImageFrame<TPixel> image)
case TgaBitsPerPixel.Pixel32:
this.Write32Bit(stream, pixels);
break;
default:
break;
}
}

Expand Down Expand Up @@ -213,6 +214,8 @@ private void WriteRunLengthEncodedImage<TPixel>(Stream stream, ImageFrame<TPixel
stream.WriteByte(color.R);
stream.WriteByte(color.A);
break;
default:
break;
}

encodedPixels += equalPixelCount + 1;
Expand All @@ -225,35 +228,29 @@ private void WriteRunLengthEncodedImage<TPixel>(Stream stream, ImageFrame<TPixel
/// <typeparam name="TPixel">The pixel type.</typeparam>
/// <param name="pixels">The pixels of the image.</param>
/// <param name="xStart">X coordinate to start searching for the same pixels.</param>
/// <param name="yStart">Y coordinate to start searching for the same pixels.</param>
/// <param name="yPos">Y coordinate to searching for the same pixels in only one scan line.</param>
/// <returns>The number of equal pixels.</returns>
private byte FindEqualPixels<TPixel>(Buffer2D<TPixel> pixels, int xStart, int yStart)
private byte FindEqualPixels<TPixel>(Buffer2D<TPixel> pixels, int xStart, int yPos)
where TPixel : unmanaged, IPixel<TPixel>
{
byte equalPixelCount = 0;
bool firstRow = true;
TPixel startPixel = pixels[xStart, yStart];
for (int y = yStart; y < pixels.Height; y++)
TPixel startPixel = pixels[xStart, yPos];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using the indexer, you could use DangerousGetRowSpan, which should be faster. Like this:

Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(yPos).Slice(xStart);

for (int x = xStart + 1; x < pixels.Width; x++)
{
for (int x = firstRow ? xStart + 1 : 0; x < pixels.Width; x++)
TPixel nextPixel = pixels[x, yPos];
if (startPixel.Equals(nextPixel))
{
equalPixelCount++;
}
else
{
TPixel nextPixel = pixels[x, y];
if (startPixel.Equals(nextPixel))
{
equalPixelCount++;
}
else
{
return equalPixelCount;
}

if (equalPixelCount >= 127)
{
return equalPixelCount;
}
return equalPixelCount;
}

firstRow = false;
if (equalPixelCount >= 127)
brianpopow marked this conversation as resolved.
Show resolved Hide resolved
{
return equalPixelCount;
}
}

return equalPixelCount;
Expand Down