Browse Source

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

af/merge-core
Dirk Lemstra 9 years ago
parent
commit
f7c5e6fa29
  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--) for (int y = pixels.Height - 1; y >= 0; y--)
{ {
pixels.CopyTo(row, 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--) for (int y = pixels.Height - 1; y >= 0; y--)
{ {
pixels.CopyTo(row, 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 // ReSharper disable once InconsistentNaming
float prevDCY = 0, prevDCCb = 0, prevDCCr = 0; 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) for (int y = 0; y < pixels.Height; y += 8)
{ {
@ -805,7 +805,7 @@ namespace ImageSharp.Formats
// ReSharper disable once InconsistentNaming // ReSharper disable once InconsistentNaming
float prevDCY = 0, prevDCCb = 0, prevDCCr = 0; 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) 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.ComponentOrder = componentOrder;
this.RowStride = width * GetComponentCount(componentOrder); this.RowStride = width * GetComponentCount(componentOrder);
this.Bytes = bytes; this.Bytes = bytes;
this.Length = bytes.Length;
this.isBufferRented = false;
this.pixelsHandle = GCHandle.Alloc(this.Bytes, GCHandleType.Pinned); this.pixelsHandle = GCHandle.Alloc(this.Bytes, GCHandleType.Pinned);
// TODO: Why is Resharper warning us about an impure method call? // 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. /// Initializes a new instance of the <see cref="PixelArea{TColor}"/> class.
/// </summary> /// </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="componentOrder">The component order.</param>
/// <param name="usePool">True if the buffer should be rented from ArrayPool</param> public PixelArea(int width, ComponentOrder componentOrder)
public PixelArea(int width, int height, ComponentOrder componentOrder, bool usePool = false) : this(width, 1, componentOrder, 0)
: this(width, height, componentOrder, 0, usePool)
{ {
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="PixelArea{TColor}"/> class. /// Initializes a new instance of the <see cref="PixelArea{TColor}"/> class.
/// </summary> /// </summary>
/// <param name="width">The width.</param> /// <param name="width">The width. </param>
/// <param name="componentOrder">The component order.</param> /// <param name="componentOrder">The component order.</param>
/// <param name="usePool">True if the buffer should be rented from ArrayPool</param> /// <param name="padding">The number of bytes to pad each row.</param>
public PixelArea(int width, ComponentOrder componentOrder, bool usePool = false) public PixelArea(int width, ComponentOrder componentOrder, int padding)
: this(width, 1, componentOrder, 0, usePool) : this(width, 1, componentOrder, padding)
{ {
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="PixelArea{TColor}"/> class. /// Initializes a new instance of the <see cref="PixelArea{TColor}"/> class.
/// </summary> /// </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="componentOrder">The component order.</param>
/// <param name="padding">The number of bytes to pad each row.</param> public PixelArea(int width, int height, ComponentOrder componentOrder)
/// <param name="usePool">True if the buffer should be rented from ArrayPool</param> : this(width, height, componentOrder, 0)
public PixelArea(int width, ComponentOrder componentOrder, int padding, bool usePool = false)
: this(width, 1, componentOrder, padding, usePool)
{ {
} }
@ -126,27 +125,15 @@ namespace ImageSharp
/// <param name="height">The height.</param> /// <param name="height">The height.</param>
/// <param name="componentOrder">The component order.</param> /// <param name="componentOrder">The component order.</param>
/// <param name="padding">The number of bytes to pad each row.</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)
public PixelArea(int width, int height, ComponentOrder componentOrder, int padding, bool usePool = false)
{ {
this.Width = width; this.Width = width;
this.Height = height; this.Height = height;
this.ComponentOrder = componentOrder; this.ComponentOrder = componentOrder;
this.RowStride = (width * GetComponentCount(componentOrder)) + padding; this.RowStride = (width * GetComponentCount(componentOrder)) + padding;
this.Length = this.RowStride * height;
int bufferSize = this.RowStride * height; this.Bytes = BytesPool.Rent(this.Length);
this.isBufferRented = true;
if (usePool)
{
this.Bytes = BytesPool.Rent(bufferSize);
this.isBufferRented = true;
Array.Clear(this.Bytes, 0, bufferSize);
}
else
{
this.Bytes = new byte[bufferSize];
}
this.pixelsHandle = GCHandle.Alloc(this.Bytes, GCHandleType.Pinned); this.pixelsHandle = GCHandle.Alloc(this.Bytes, GCHandleType.Pinned);
// TODO: Why is Resharper warning us about an impure method call? // TODO: Why is Resharper warning us about an impure method call?
@ -167,6 +154,11 @@ namespace ImageSharp
/// </summary> /// </summary>
public byte[] Bytes { get; } public byte[] Bytes { get; }
/// <summary>
/// Gets the length of the buffer.
/// </summary>
public int Length { get; }
/// <summary> /// <summary>
/// Gets the component order. /// Gets the component order.
/// </summary> /// </summary>
@ -198,9 +190,8 @@ namespace ImageSharp
public int Width { get; } public int Width { get; }
/// <summary> /// <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> /// </summary>
// ReSharper disable once StaticMemberInGenericType
// TODO: Use own pool? // TODO: Use own pool?
private static ArrayPool<byte> BytesPool => ArrayPool<byte>.Shared; private static ArrayPool<byte> BytesPool => ArrayPool<byte>.Shared;
@ -210,6 +201,13 @@ namespace ImageSharp
public void Dispose() public void Dispose()
{ {
this.Dispose(true); 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> /// <summary>
@ -218,7 +216,7 @@ namespace ImageSharp
/// <param name="stream">The stream.</param> /// <param name="stream">The stream.</param>
public void Read(Stream stream) public void Read(Stream stream)
{ {
stream.Read(this.Bytes, 0, this.Bytes.Length); stream.Read(this.Bytes, 0, this.Length);
} }
/// <summary> /// <summary>
@ -227,7 +225,7 @@ namespace ImageSharp
/// <param name="stream">The stream.</param> /// <param name="stream">The stream.</param>
public void Write(Stream stream) public void Write(Stream stream)
{ {
stream.Write(this.Bytes, 0, this.Bytes.Length); stream.Write(this.Bytes, 0, this.Length);
} }
/// <summary> /// <summary>
@ -315,13 +313,6 @@ namespace ImageSharp
this.PixelBase = null; this.PixelBase = null;
this.isDisposed = true; 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