Browse Source

Fix rle compression mistake in tga encoder.

pull/2172/head
Yu 4 years ago
parent
commit
4b28eb6bdc
  1. 43
      src/ImageSharp/Formats/Tga/TgaEncoderCore.cs

43
src/ImageSharp/Formats/Tga/TgaEncoderCore.cs

@ -7,7 +7,6 @@ using System.Buffers.Binary;
using System.IO; using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.Metadata;
@ -73,7 +72,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
this.configuration = image.GetConfiguration(); this.configuration = image.GetConfiguration();
ImageMetadata metadata = image.Metadata; ImageMetadata metadata = image.Metadata;
TgaMetadata tgaMetadata = metadata.GetTgaMetadata(); 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; TgaImageType imageType = this.compression is TgaCompression.RunLength ? TgaImageType.RleTrueColor : TgaImageType.TrueColor;
if (this.bitsPerPixel == TgaBitsPerPixel.Pixel8) if (this.bitsPerPixel == TgaBitsPerPixel.Pixel8)
@ -160,6 +159,8 @@ namespace SixLabors.ImageSharp.Formats.Tga
case TgaBitsPerPixel.Pixel32: case TgaBitsPerPixel.Pixel32:
this.Write32Bit(stream, pixels); this.Write32Bit(stream, pixels);
break; break;
default:
break;
} }
} }
@ -213,6 +214,8 @@ namespace SixLabors.ImageSharp.Formats.Tga
stream.WriteByte(color.R); stream.WriteByte(color.R);
stream.WriteByte(color.A); stream.WriteByte(color.A);
break; break;
default:
break;
} }
encodedPixels += equalPixelCount + 1; encodedPixels += equalPixelCount + 1;
@ -225,35 +228,29 @@ namespace SixLabors.ImageSharp.Formats.Tga
/// <typeparam name="TPixel">The pixel type.</typeparam> /// <typeparam name="TPixel">The pixel type.</typeparam>
/// <param name="pixels">The pixels of the image.</param> /// <param name="pixels">The pixels of the image.</param>
/// <param name="xStart">X coordinate to start searching for the same pixels.</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> /// <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> where TPixel : unmanaged, IPixel<TPixel>
{ {
byte equalPixelCount = 0; byte equalPixelCount = 0;
bool firstRow = true; TPixel startPixel = pixels[xStart, yPos];
TPixel startPixel = pixels[xStart, yStart]; for (int x = xStart + 1; x < pixels.Width; x++)
for (int y = yStart; y < pixels.Height; y++)
{ {
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]; return equalPixelCount;
if (startPixel.Equals(nextPixel))
{
equalPixelCount++;
}
else
{
return equalPixelCount;
}
if (equalPixelCount >= 127)
{
return equalPixelCount;
}
} }
firstRow = false; if (equalPixelCount >= 127)
{
return equalPixelCount;
}
} }
return equalPixelCount; return equalPixelCount;

Loading…
Cancel
Save