diff --git a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs
index 5a022e9258..9ad5a047eb 100644
--- a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs
+++ b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs
@@ -201,7 +201,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
}
///
- /// Finds consecutive pixels, which have the same value starting from the pixel span offset 0.
+ /// Finds consecutive pixels which have the same value.
///
/// The pixel type.
/// The pixels of the image.
@@ -212,13 +212,14 @@ namespace SixLabors.ImageSharp.Formats.Tga
where TPixel : struct, IPixel
{
byte equalPixelCount = 0;
+ bool firstRow = true;
+ TPixel startPixel = pixels[xStart, yStart];
for (int y = yStart; y < pixels.Height; y++)
{
- for (int x = xStart; x < pixels.Width - 1; x++)
+ for (int x = firstRow ? xStart + 1 : 0; x < pixels.Width; x++)
{
- TPixel currentPixel = pixels[x, y];
- TPixel nextPixel = pixels[x + 1, y];
- if (currentPixel.Equals(nextPixel))
+ TPixel nextPixel = pixels[x, y];
+ if (startPixel.Equals(nextPixel))
{
equalPixelCount++;
}
@@ -229,9 +230,11 @@ namespace SixLabors.ImageSharp.Formats.Tga
if (equalPixelCount >= 127)
{
- break;
+ return equalPixelCount;
}
}
+
+ firstRow = false;
}
return equalPixelCount;