From 8f048cc9653cbb893b396e4a3032536331b0eb8f Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Wed, 28 Dec 2016 14:24:23 +0100 Subject: [PATCH] Removed usePool overloads and added a Length property that can be used when the pool is rented. --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 4 +- src/ImageSharp/Formats/Jpg/JpegEncoderCore.cs | 4 +- src/ImageSharp/Image/PixelArea{TColor}.cs | 71 ++++++++----------- 3 files changed, 35 insertions(+), 44 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 1d38b896b..20c899a18 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/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); } } } diff --git a/src/ImageSharp/Formats/Jpg/JpegEncoderCore.cs b/src/ImageSharp/Formats/Jpg/JpegEncoderCore.cs index 24f8bb84c..cd0ec0af4 100644 --- a/src/ImageSharp/Formats/Jpg/JpegEncoderCore.cs +++ b/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 rgbBytes = new PixelArea(8, 8, ComponentOrder.Xyz, true)) + using (PixelArea rgbBytes = new PixelArea(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 rgbBytes = new PixelArea(8, 8, ComponentOrder.Xyz, true)) + using (PixelArea rgbBytes = new PixelArea(8, 8, ComponentOrder.Xyz)) { for (int y = 0; y < pixels.Height; y += 16) { diff --git a/src/ImageSharp/Image/PixelArea{TColor}.cs b/src/ImageSharp/Image/PixelArea{TColor}.cs index c63d8b756..40da75c82 100644 --- a/src/ImageSharp/Image/PixelArea{TColor}.cs +++ b/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 class. /// /// The width. - /// The height. /// The component order. - /// True if the buffer should be rented from ArrayPool - 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) { } /// /// Initializes a new instance of the class. /// - /// The width. + /// The width. /// The component order. - /// True if the buffer should be rented from ArrayPool - public PixelArea(int width, ComponentOrder componentOrder, bool usePool = false) - : this(width, 1, componentOrder, 0, usePool) + /// The number of bytes to pad each row. + public PixelArea(int width, ComponentOrder componentOrder, int padding) + : this(width, 1, componentOrder, padding) { } /// /// Initializes a new instance of the class. /// - /// The width. + /// The width. + /// The height. /// The component order. - /// The number of bytes to pad each row. - /// True if the buffer should be rented from ArrayPool - 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 /// The height. /// The component order. /// The number of bytes to pad each row. - /// True if the buffer should be rented from ArrayPool - 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 /// public byte[] Bytes { get; } + /// + /// Gets the length of the buffer. + /// + public int Length { get; } + /// /// Gets the component order. /// @@ -198,9 +190,8 @@ namespace ImageSharp public int Width { get; } /// - /// Gets the pool used to rent , 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. /// - // ReSharper disable once StaticMemberInGenericType // TODO: Use own pool? private static ArrayPool BytesPool => ArrayPool.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); } /// @@ -218,7 +216,7 @@ namespace ImageSharp /// The stream. public void Read(Stream stream) { - stream.Read(this.Bytes, 0, this.Bytes.Length); + stream.Read(this.Bytes, 0, this.Length); } /// @@ -227,7 +225,7 @@ namespace ImageSharp /// The stream. public void Write(Stream stream) { - stream.Write(this.Bytes, 0, this.Bytes.Length); + stream.Write(this.Bytes, 0, this.Length); } /// @@ -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); } } } \ No newline at end of file