Browse Source

Removed usePool overloads and added a Length property that can be used when the pool is rented.

pull/65/head
Dirk Lemstra 9 years ago
parent
commit
8f048cc965
  1. 4
      src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
  2. 4
      src/ImageSharp/Formats/Jpg/JpegEncoderCore.cs
  3. 71
      src/ImageSharp/Image/PixelArea{TColor}.cs

4
src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

@ -155,7 +155,7 @@ namespace ImageSharp.Formats
for (int y = pixels.Height - 1; y >= 0; y--)
{
pixels.CopyTo(row, y);
writer.Write(row.Bytes);
writer.Write(row.Bytes, 0, row.Length);
}
}
}
@ -174,7 +174,7 @@ namespace ImageSharp.Formats
for (int y = pixels.Height - 1; y >= 0; y--)
{
pixels.CopyTo(row, y);
writer.Write(row.Bytes);
writer.Write(row.Bytes, 0, row.Length);
}
}
}

4
src/ImageSharp/Formats/Jpg/JpegEncoderCore.cs

@ -436,7 +436,7 @@ namespace ImageSharp.Formats
// ReSharper disable once InconsistentNaming
float prevDCY = 0, prevDCCb = 0, prevDCCr = 0;
using (PixelArea<TColor> rgbBytes = new PixelArea<TColor>(8, 8, ComponentOrder.Xyz, true))
using (PixelArea<TColor> rgbBytes = new PixelArea<TColor>(8, 8, ComponentOrder.Xyz))
{
for (int y = 0; y < pixels.Height; y += 8)
{
@ -805,7 +805,7 @@ namespace ImageSharp.Formats
// ReSharper disable once InconsistentNaming
float prevDCY = 0, prevDCCb = 0, prevDCCr = 0;
using (PixelArea<TColor> rgbBytes = new PixelArea<TColor>(8, 8, ComponentOrder.Xyz, true))
using (PixelArea<TColor> rgbBytes = new PixelArea<TColor>(8, 8, ComponentOrder.Xyz))
{
for (int y = 0; y < pixels.Height; y += 16)
{

71
src/ImageSharp/Image/PixelArea{TColor}.cs

@ -77,6 +77,8 @@ namespace ImageSharp
this.ComponentOrder = componentOrder;
this.RowStride = width * GetComponentCount(componentOrder);
this.Bytes = bytes;
this.Length = bytes.Length;
this.isBufferRented = false;
this.pixelsHandle = GCHandle.Alloc(this.Bytes, GCHandleType.Pinned);
// TODO: Why is Resharper warning us about an impure method call?
@ -88,34 +90,31 @@ namespace ImageSharp
/// Initializes a new instance of the <see cref="PixelArea{TColor}"/> class.
/// </summary>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="componentOrder">The component order.</param>
/// <param name="usePool">True if the buffer should be rented from ArrayPool</param>
public PixelArea(int width, int height, ComponentOrder componentOrder, bool usePool = false)
: this(width, height, componentOrder, 0, usePool)
public PixelArea(int width, ComponentOrder componentOrder)
: this(width, 1, componentOrder, 0)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PixelArea{TColor}"/> class.
/// </summary>
/// <param name="width">The width.</param>
/// <param name="width">The width. </param>
/// <param name="componentOrder">The component order.</param>
/// <param name="usePool">True if the buffer should be rented from ArrayPool</param>
public PixelArea(int width, ComponentOrder componentOrder, bool usePool = false)
: this(width, 1, componentOrder, 0, usePool)
/// <param name="padding">The number of bytes to pad each row.</param>
public PixelArea(int width, ComponentOrder componentOrder, int padding)
: this(width, 1, componentOrder, padding)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PixelArea{TColor}"/> class.
/// </summary>
/// <param name="width">The width. </param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="componentOrder">The component order.</param>
/// <param name="padding">The number of bytes to pad each row.</param>
/// <param name="usePool">True if the buffer should be rented from ArrayPool</param>
public PixelArea(int width, ComponentOrder componentOrder, int padding, bool usePool = false)
: this(width, 1, componentOrder, padding, usePool)
public PixelArea(int width, int height, ComponentOrder componentOrder)
: this(width, height, componentOrder, 0)
{
}
@ -126,27 +125,15 @@ namespace ImageSharp
/// <param name="height">The height.</param>
/// <param name="componentOrder">The component order.</param>
/// <param name="padding">The number of bytes to pad each row.</param>
/// <param name="usePool">True if the buffer should be rented from ArrayPool</param>
public PixelArea(int width, int height, ComponentOrder componentOrder, int padding, bool usePool = false)
public PixelArea(int width, int height, ComponentOrder componentOrder, int padding)
{
this.Width = width;
this.Height = height;
this.ComponentOrder = componentOrder;
this.RowStride = (width * GetComponentCount(componentOrder)) + padding;
int bufferSize = this.RowStride * height;
if (usePool)
{
this.Bytes = BytesPool.Rent(bufferSize);
this.isBufferRented = true;
Array.Clear(this.Bytes, 0, bufferSize);
}
else
{
this.Bytes = new byte[bufferSize];
}
this.Length = this.RowStride * height;
this.Bytes = BytesPool.Rent(this.Length);
this.isBufferRented = true;
this.pixelsHandle = GCHandle.Alloc(this.Bytes, GCHandleType.Pinned);
// TODO: Why is Resharper warning us about an impure method call?
@ -167,6 +154,11 @@ namespace ImageSharp
/// </summary>
public byte[] Bytes { get; }
/// <summary>
/// Gets the length of the buffer.
/// </summary>
public int Length { get; }
/// <summary>
/// Gets the component order.
/// </summary>
@ -198,9 +190,8 @@ namespace ImageSharp
public int Width { get; }
/// <summary>
/// Gets the pool used to rent <see cref="Bytes"/>, when it's not coming from an external source
/// Gets the pool used to rent bytes, when it's not coming from an external source.
/// </summary>
// ReSharper disable once StaticMemberInGenericType
// TODO: Use own pool?
private static ArrayPool<byte> BytesPool => ArrayPool<byte>.Shared;
@ -210,6 +201,13 @@ namespace ImageSharp
public void Dispose()
{
this.Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SuppressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
/// <summary>
@ -218,7 +216,7 @@ namespace ImageSharp
/// <param name="stream">The stream.</param>
public void Read(Stream stream)
{
stream.Read(this.Bytes, 0, this.Bytes.Length);
stream.Read(this.Bytes, 0, this.Length);
}
/// <summary>
@ -227,7 +225,7 @@ namespace ImageSharp
/// <param name="stream">The stream.</param>
public void Write(Stream stream)
{
stream.Write(this.Bytes, 0, this.Bytes.Length);
stream.Write(this.Bytes, 0, this.Length);
}
/// <summary>
@ -315,13 +313,6 @@ namespace ImageSharp
this.PixelBase = null;
this.isDisposed = true;
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SuppressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
}
}
Loading…
Cancel
Save