diff --git a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs index 247bdd49b3..16910040c5 100644 --- a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs @@ -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; @@ -73,7 +72,7 @@ public void Encode(Image 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) @@ -160,6 +159,8 @@ private void WriteImage(Stream stream, ImageFrame image) case TgaBitsPerPixel.Pixel32: this.Write32Bit(stream, pixels); break; + default: + break; } } @@ -213,6 +214,8 @@ private void WriteRunLengthEncodedImage(Stream stream, ImageFrame(Stream stream, ImageFrameThe pixel type. /// The pixels of the image. /// X coordinate to start searching for the same pixels. - /// Y coordinate to start searching for the same pixels. + /// Y coordinate to searching for the same pixels in only one scan line. /// The number of equal pixels. - private byte FindEqualPixels(Buffer2D pixels, int xStart, int yStart) + private byte FindEqualPixels(Buffer2D pixels, int xStart, int yPos) where TPixel : unmanaged, IPixel { byte equalPixelCount = 0; - bool firstRow = true; - TPixel startPixel = pixels[xStart, yStart]; - for (int y = yStart; y < pixels.Height; y++) + TPixel startPixel = pixels[xStart, yPos]; + 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) + { + return equalPixelCount; + } } return equalPixelCount;