diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index a06e306f5..c4b234147 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -182,7 +182,11 @@ namespace ImageSharp.Formats this.WritePhysicalChunk(stream, image); this.WriteGammaChunk(stream); - this.WriteDataChunks(image, stream); + using (PixelAccessor pixels = image.Lock()) + { + this.WriteDataChunks(pixels, stream); + } + this.WriteEndChunk(stream); stream.Flush(); } @@ -249,35 +253,32 @@ namespace ImageSharp.Formats /// /// The pixel format. /// The packed format. uint, long, float. - /// The image to encode. + /// The image pixels accessor. /// The row index. /// The raw scanline. - private void CollectGrayscaleBytes(ImageBase image, int row, byte[] rawScanline) + private void CollectGrayscaleBytes(PixelAccessor pixels, int row, byte[] rawScanline) where TColor : struct, IPackedPixel where TPacked : struct { // Copy the pixels across from the image. // Reuse the chunk type buffer. - using (PixelAccessor pixels = image.Lock()) + for (int x = 0; x < this.width; x++) { - for (int x = 0; x < this.width; x++) - { - // Convert the color to YCbCr and store the luminance - // Optionally store the original color alpha. - int offset = x * this.bytesPerPixel; - pixels[x, row].ToBytes(this.chunkTypeBuffer, 0, ComponentOrder.XYZW); - byte luminance = (byte)((0.299F * this.chunkTypeBuffer[0]) + (0.587F * this.chunkTypeBuffer[1]) + (0.114F * this.chunkTypeBuffer[2])); + // Convert the color to YCbCr and store the luminance + // Optionally store the original color alpha. + int offset = x * this.bytesPerPixel; + pixels[x, row].ToBytes(this.chunkTypeBuffer, 0, ComponentOrder.XYZW); + byte luminance = (byte)((0.299F * this.chunkTypeBuffer[0]) + (0.587F * this.chunkTypeBuffer[1]) + (0.114F * this.chunkTypeBuffer[2])); - for (int i = 0; i < this.bytesPerPixel; i++) + for (int i = 0; i < this.bytesPerPixel; i++) + { + if (i == 0) { - if (i == 0) - { - rawScanline[offset] = luminance; - } - else - { - rawScanline[offset + i] = this.chunkTypeBuffer[3]; - } + rawScanline[offset] = luminance; + } + else + { + rawScanline[offset + i] = this.chunkTypeBuffer[3]; } } } @@ -288,20 +289,17 @@ namespace ImageSharp.Formats /// /// The pixel format. /// The packed format. uint, long, float. - /// The image to encode. + /// The image pixel accessor. /// The row index. /// The raw scanline. - private void CollectColorBytes(ImageBase image, int row, byte[] rawScanline) + private void CollectColorBytes(PixelAccessor pixels, int row, byte[] rawScanline) where TColor : struct, IPackedPixel where TPacked : struct { - using (PixelAccessor pixels = image.Lock()) + int bpp = this.bytesPerPixel; + for (int x = 0; x < this.width; x++) { - int bpp = this.bytesPerPixel; - for (int x = 0; x < this.width; x++) - { - pixels[x, row].ToBytes(rawScanline, x * this.bytesPerPixel, bpp == 4 ? ComponentOrder.XYZW : ComponentOrder.XYZ); - } + pixels[x, row].ToBytes(rawScanline, x * this.bytesPerPixel, bpp == 4 ? ComponentOrder.XYZW : ComponentOrder.XYZ); } } @@ -311,13 +309,13 @@ namespace ImageSharp.Formats /// /// The pixel format. /// The packed format. uint, long, float. - /// The image to encode. + /// The image pixel accessor. /// The row. /// The previous scanline. /// The raw scanline. /// The number of bytes per scanline. /// The - private byte[] EncodePixelRow(ImageBase image, int row, byte[] previousScanline, byte[] rawScanline, int bytesPerScanline) + private byte[] EncodePixelRow(PixelAccessor pixels, int row, byte[] previousScanline, byte[] rawScanline, int bytesPerScanline) where TColor : struct, IPackedPixel where TPacked : struct { @@ -328,10 +326,10 @@ namespace ImageSharp.Formats break; case PngColorType.Grayscale: case PngColorType.GrayscaleWithAlpha: - this.CollectGrayscaleBytes(image, row, rawScanline); + this.CollectGrayscaleBytes(pixels, row, rawScanline); break; default: - this.CollectColorBytes(image, row, rawScanline); + this.CollectColorBytes(pixels, row, rawScanline); break; } @@ -592,9 +590,9 @@ namespace ImageSharp.Formats /// /// The pixel format. /// The packed format. uint, long, float. - /// The image to encode. + /// The pixel accessor. /// The stream. - private void WriteDataChunks(ImageBase image, Stream stream) + private void WriteDataChunks(PixelAccessor pixels, Stream stream) where TColor : struct, IPackedPixel where TPacked : struct { @@ -612,7 +610,7 @@ namespace ImageSharp.Formats { for (int y = 0; y < this.height; y++) { - byte[] data = this.EncodePixelRow(image, y, previousScanline, rawScanline, bytesPerScanline); + byte[] data = this.EncodePixelRow(pixels, y, previousScanline, rawScanline, bytesPerScanline); deflateStream.Write(data, 0, data.Length); deflateStream.Flush();