Browse Source

Remove duplicate locking

pull/23/head
James Jackson-South 10 years ago
parent
commit
a187ca99f8
  1. 34
      src/ImageSharp/Formats/Png/PngEncoderCore.cs

34
src/ImageSharp/Formats/Png/PngEncoderCore.cs

@ -182,7 +182,11 @@ namespace ImageSharp.Formats
this.WritePhysicalChunk(stream, image); this.WritePhysicalChunk(stream, image);
this.WriteGammaChunk(stream); this.WriteGammaChunk(stream);
this.WriteDataChunks(image, stream); using (PixelAccessor<TColor, TPacked> pixels = image.Lock())
{
this.WriteDataChunks(pixels, stream);
}
this.WriteEndChunk(stream); this.WriteEndChunk(stream);
stream.Flush(); stream.Flush();
} }
@ -249,17 +253,15 @@ namespace ImageSharp.Formats
/// </summary> /// </summary>
/// <typeparam name="TColor">The pixel format.</typeparam> /// <typeparam name="TColor">The pixel format.</typeparam>
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam> /// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
/// <param name="image">The image to encode.</param> /// <param name="pixels">The image pixels accessor.</param>
/// <param name="row">The row index.</param> /// <param name="row">The row index.</param>
/// <param name="rawScanline">The raw scanline.</param> /// <param name="rawScanline">The raw scanline.</param>
private void CollectGrayscaleBytes<TColor, TPacked>(ImageBase<TColor, TPacked> image, int row, byte[] rawScanline) private void CollectGrayscaleBytes<TColor, TPacked>(PixelAccessor<TColor, TPacked> pixels, int row, byte[] rawScanline)
where TColor : struct, IPackedPixel<TPacked> where TColor : struct, IPackedPixel<TPacked>
where TPacked : struct where TPacked : struct
{ {
// Copy the pixels across from the image. // Copy the pixels across from the image.
// Reuse the chunk type buffer. // Reuse the chunk type buffer.
using (PixelAccessor<TColor, TPacked> 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 // Convert the color to YCbCr and store the luminance
@ -281,21 +283,18 @@ namespace ImageSharp.Formats
} }
} }
} }
}
/// <summary> /// <summary>
/// Collects a row of true color pixel data. /// Collects a row of true color pixel data.
/// </summary> /// </summary>
/// <typeparam name="TColor">The pixel format.</typeparam> /// <typeparam name="TColor">The pixel format.</typeparam>
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam> /// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
/// <param name="image">The image to encode.</param> /// <param name="pixels">The image pixel accessor.</param>
/// <param name="row">The row index.</param> /// <param name="row">The row index.</param>
/// <param name="rawScanline">The raw scanline.</param> /// <param name="rawScanline">The raw scanline.</param>
private void CollectColorBytes<TColor, TPacked>(ImageBase<TColor, TPacked> image, int row, byte[] rawScanline) private void CollectColorBytes<TColor, TPacked>(PixelAccessor<TColor, TPacked> pixels, int row, byte[] rawScanline)
where TColor : struct, IPackedPixel<TPacked> where TColor : struct, IPackedPixel<TPacked>
where TPacked : struct where TPacked : struct
{
using (PixelAccessor<TColor, TPacked> pixels = image.Lock())
{ {
int bpp = this.bytesPerPixel; int bpp = this.bytesPerPixel;
for (int x = 0; x < this.width; x++) for (int x = 0; x < this.width; x++)
@ -303,7 +302,6 @@ namespace ImageSharp.Formats
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);
} }
} }
}
/// <summary> /// <summary>
/// Encodes the pixel data line by line. /// Encodes the pixel data line by line.
@ -311,13 +309,13 @@ namespace ImageSharp.Formats
/// </summary> /// </summary>
/// <typeparam name="TColor">The pixel format.</typeparam> /// <typeparam name="TColor">The pixel format.</typeparam>
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam> /// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
/// <param name="image">The image to encode.</param> /// <param name="pixels">The image pixel accessor.</param>
/// <param name="row">The row.</param> /// <param name="row">The row.</param>
/// <param name="previousScanline">The previous scanline.</param> /// <param name="previousScanline">The previous scanline.</param>
/// <param name="rawScanline">The raw scanline.</param> /// <param name="rawScanline">The raw scanline.</param>
/// <param name="bytesPerScanline">The number of bytes per scanline.</param> /// <param name="bytesPerScanline">The number of bytes per scanline.</param>
/// <returns>The <see cref="T:byte[]"/></returns> /// <returns>The <see cref="T:byte[]"/></returns>
private byte[] EncodePixelRow<TColor, TPacked>(ImageBase<TColor, TPacked> image, int row, byte[] previousScanline, byte[] rawScanline, int bytesPerScanline) private byte[] EncodePixelRow<TColor, TPacked>(PixelAccessor<TColor, TPacked> pixels, int row, byte[] previousScanline, byte[] rawScanline, int bytesPerScanline)
where TColor : struct, IPackedPixel<TPacked> where TColor : struct, IPackedPixel<TPacked>
where TPacked : struct where TPacked : struct
{ {
@ -328,10 +326,10 @@ namespace ImageSharp.Formats
break; break;
case PngColorType.Grayscale: case PngColorType.Grayscale:
case PngColorType.GrayscaleWithAlpha: case PngColorType.GrayscaleWithAlpha:
this.CollectGrayscaleBytes(image, row, rawScanline); this.CollectGrayscaleBytes(pixels, row, rawScanline);
break; break;
default: default:
this.CollectColorBytes(image, row, rawScanline); this.CollectColorBytes(pixels, row, rawScanline);
break; break;
} }
@ -592,9 +590,9 @@ namespace ImageSharp.Formats
/// </summary> /// </summary>
/// <typeparam name="TColor">The pixel format.</typeparam> /// <typeparam name="TColor">The pixel format.</typeparam>
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam> /// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
/// <param name="image">The image to encode.</param> /// <param name="pixels">The pixel accessor.</param>
/// <param name="stream">The stream.</param> /// <param name="stream">The stream.</param>
private void WriteDataChunks<TColor, TPacked>(ImageBase<TColor, TPacked> image, Stream stream) private void WriteDataChunks<TColor, TPacked>(PixelAccessor<TColor, TPacked> pixels, Stream stream)
where TColor : struct, IPackedPixel<TPacked> where TColor : struct, IPackedPixel<TPacked>
where TPacked : struct where TPacked : struct
{ {
@ -612,7 +610,7 @@ namespace ImageSharp.Formats
{ {
for (int y = 0; y < this.height; y++) 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.Write(data, 0, data.Length);
deflateStream.Flush(); deflateStream.Flush();

Loading…
Cancel
Save