From e59255d43fb8f144691426a891dbe689643ea1fe Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 5 Mar 2017 18:18:09 +0100 Subject: [PATCH] cleanup & stylecop --- .../BulkPixelOperations{TColor}.cs | 56 +++++++++---------- .../Colors/PackedPixel/HalfVector2.cs | 2 +- .../Colors/PackedPixel/NormalizedByte4.cs | 2 +- src/ImageSharp/Common/Memory/ArrayPointer.cs | 34 +++++++++++ .../Common/Memory/ArrayPointer{T}.cs | 4 +- .../{PinnedBuffer.cs => PinnedBuffer{T}.cs} | 16 +++++- .../Common/Memory/PixelDataPool{T}.cs | 2 +- src/ImageSharp/Image/ImageBase{TColor}.cs | 4 +- src/ImageSharp/Image/PixelAccessor{TColor}.cs | 41 +------------- src/ImageSharp/Image/PixelArea{TColor}.cs | 7 +-- .../Colors/BulkPixelOperationsTests.cs | 5 +- 11 files changed, 92 insertions(+), 81 deletions(-) rename src/ImageSharp/Common/Memory/{PinnedBuffer.cs => PinnedBuffer{T}.cs} (93%) diff --git a/src/ImageSharp/Colors/PackedPixel/BulkPixelOperations{TColor}.cs b/src/ImageSharp/Colors/PackedPixel/BulkPixelOperations{TColor}.cs index 557d59a16..31f872e42 100644 --- a/src/ImageSharp/Colors/PackedPixel/BulkPixelOperations{TColor}.cs +++ b/src/ImageSharp/Colors/PackedPixel/BulkPixelOperations{TColor}.cs @@ -78,9 +78,9 @@ namespace ImageSharp /// /// Bulk version of that converts data in . /// - /// - /// - /// + /// The to the source bytes. + /// The to the destination colors. + /// The number of pixels to convert. internal virtual void PackFromXyzBytes( ArrayPointer sourceBytes, ArrayPointer destColors, @@ -102,15 +102,15 @@ namespace ImageSharp /// /// Bulk version of . /// - /// - /// - /// + /// The to the source colors. + /// The to the destination bytes. + /// The number of pixels to convert. internal virtual void PackToXyzBytes(ArrayPointer sourceColors, ArrayPointer destBytes, int count) { byte* sp = (byte*)sourceColors; byte[] dest = destBytes.Array; - for (int i = destBytes.Offset; i < destBytes.Offset + count * 3; i += 3) + for (int i = destBytes.Offset; i < destBytes.Offset + (count * 3); i += 3) { TColor c = Unsafe.Read(sp); c.ToXyzBytes(dest, i); @@ -121,9 +121,9 @@ namespace ImageSharp /// /// Bulk version of that converts data in . /// - /// - /// - /// + /// The to the source bytes. + /// The to the destination colors. + /// The number of pixels to convert. internal virtual void PackFromXyzwBytes( ArrayPointer sourceBytes, ArrayPointer destColors, @@ -145,9 +145,9 @@ namespace ImageSharp /// /// Bulk version of . /// - /// - /// - /// + /// The to the source colors. + /// The to the destination bytes. + /// The number of pixels to convert. internal virtual void PackToXyzwBytes( ArrayPointer sourceColors, ArrayPointer destBytes, @@ -156,7 +156,7 @@ namespace ImageSharp byte* sp = (byte*)sourceColors; byte[] dest = destBytes.Array; - for (int i = destBytes.Offset; i < destBytes.Offset + count * 4; i += 4) + for (int i = destBytes.Offset; i < destBytes.Offset + (count * 4); i += 4) { TColor c = Unsafe.Read(sp); c.ToXyzwBytes(dest, i); @@ -167,9 +167,9 @@ namespace ImageSharp /// /// Bulk version of that converts data in . /// - /// - /// - /// + /// The to the source bytes. + /// The to the destination colors. + /// The number of pixels to convert. internal virtual void PackFromZyxBytes( ArrayPointer sourceBytes, ArrayPointer destColors, @@ -191,15 +191,15 @@ namespace ImageSharp /// /// Bulk version of . /// - /// - /// - /// + /// The to the source colors. + /// The to the destination bytes. + /// The number of pixels to convert. internal virtual void PackToZyxBytes(ArrayPointer sourceColors, ArrayPointer destBytes, int count) { byte* sp = (byte*)sourceColors; byte[] dest = destBytes.Array; - for (int i = destBytes.Offset; i < destBytes.Offset + count * 3; i += 3) + for (int i = destBytes.Offset; i < destBytes.Offset + (count * 3); i += 3) { TColor c = Unsafe.Read(sp); c.ToZyxBytes(dest, i); @@ -210,9 +210,9 @@ namespace ImageSharp /// /// Bulk version of that converts data in . /// - /// - /// - /// + /// The to the source bytes. + /// The to the destination colors. + /// The number of pixels to convert. internal virtual void PackFromZyxwBytes( ArrayPointer sourceBytes, ArrayPointer destColors, @@ -234,9 +234,9 @@ namespace ImageSharp /// /// Bulk version of . /// - /// - /// - /// + /// The to the source colors. + /// The to the destination bytes. + /// The number of pixels to convert. internal virtual void PackToZyxwBytes( ArrayPointer sourceColors, ArrayPointer destBytes, @@ -245,7 +245,7 @@ namespace ImageSharp byte* sp = (byte*)sourceColors; byte[] dest = destBytes.Array; - for (int i = destBytes.Offset; i < destBytes.Offset + count * 4; i += 4) + for (int i = destBytes.Offset; i < destBytes.Offset + (count * 4); i += 4) { TColor c = Unsafe.Read(sp); c.ToZyxwBytes(dest, i); diff --git a/src/ImageSharp/Colors/PackedPixel/HalfVector2.cs b/src/ImageSharp/Colors/PackedPixel/HalfVector2.cs index 778f86e0f..68cb42735 100644 --- a/src/ImageSharp/Colors/PackedPixel/HalfVector2.cs +++ b/src/ImageSharp/Colors/PackedPixel/HalfVector2.cs @@ -45,7 +45,7 @@ namespace ImageSharp /// public uint PackedValue { get; set; } - + /// public BulkPixelOperations BulkOperations => new BulkPixelOperations(); diff --git a/src/ImageSharp/Colors/PackedPixel/NormalizedByte4.cs b/src/ImageSharp/Colors/PackedPixel/NormalizedByte4.cs index cba3f0e86..4511060e4 100644 --- a/src/ImageSharp/Colors/PackedPixel/NormalizedByte4.cs +++ b/src/ImageSharp/Colors/PackedPixel/NormalizedByte4.cs @@ -52,7 +52,7 @@ namespace ImageSharp /// public uint PackedValue { get; set; } - + /// public BulkPixelOperations BulkOperations => new BulkPixelOperations(); diff --git a/src/ImageSharp/Common/Memory/ArrayPointer.cs b/src/ImageSharp/Common/Memory/ArrayPointer.cs index 56cae35a4..342eaa6c8 100644 --- a/src/ImageSharp/Common/Memory/ArrayPointer.cs +++ b/src/ImageSharp/Common/Memory/ArrayPointer.cs @@ -1,3 +1,8 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + namespace ImageSharp { using System.Runtime.CompilerServices; @@ -7,6 +12,12 @@ namespace ImageSharp /// internal static class ArrayPointer { + /// + /// Gets an to the beginning of the raw data in 'buffer'. + /// + /// The element type + /// The input + /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe ArrayPointer GetArrayPointer(this PinnedBuffer buffer) where T : struct @@ -14,6 +25,13 @@ namespace ImageSharp return new ArrayPointer(buffer.Array, (void*)buffer.Pointer); } + /// + /// Copy 'count' number of elements of the same type from 'source' to 'dest' + /// + /// The element type. + /// The input + /// The destination . + /// The number of elements to copy [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe void Copy(ArrayPointer source, ArrayPointer destination, int count) where T : struct @@ -21,6 +39,13 @@ namespace ImageSharp Unsafe.CopyBlock((void*)source.PointerAtOffset, (void*)destination.PointerAtOffset, USizeOf(count)); } + /// + /// Copy 'countInSource' elements of from 'source' into the raw byte buffer 'destination'. + /// + /// The element type. + /// The source buffer of elements to copy from. + /// The destination buffer. + /// The number of elements to copy from 'source' [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe void Copy(ArrayPointer source, ArrayPointer destination, int countInSource) where T : struct @@ -28,6 +53,13 @@ namespace ImageSharp Unsafe.CopyBlock((void*)source.PointerAtOffset, (void*)destination.PointerAtOffset, USizeOf(countInSource)); } + /// + /// Copy 'countInDest' number of elements into 'dest' from a raw byte buffer defined by 'source'. + /// + /// The element type. + /// The raw source buffer to copy from"/> + /// The destination buffer"/> + /// The number of elements to copy. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe void Copy(ArrayPointer source, ArrayPointer destination, int countInDest) where T : struct @@ -38,6 +70,7 @@ namespace ImageSharp /// /// Gets the size of `count` elements in bytes. /// + /// The element type. /// The count of the elements /// The size in bytes as int [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -47,6 +80,7 @@ namespace ImageSharp /// /// Gets the size of `count` elements in bytes as UInt32 /// + /// The element type. /// The count of the elements /// The size in bytes as UInt32 [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/ImageSharp/Common/Memory/ArrayPointer{T}.cs b/src/ImageSharp/Common/Memory/ArrayPointer{T}.cs index e0b728095..9cbbaf094 100644 --- a/src/ImageSharp/Common/Memory/ArrayPointer{T}.cs +++ b/src/ImageSharp/Common/Memory/ArrayPointer{T}.cs @@ -73,7 +73,7 @@ namespace ImageSharp /// /// The to convert [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static explicit operator void* (ArrayPointer arrayPointer) + public static explicit operator void*(ArrayPointer arrayPointer) { return (void*)arrayPointer.PointerAtOffset; } @@ -83,7 +83,7 @@ namespace ImageSharp /// /// The to convert [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static explicit operator byte* (ArrayPointer arrayPointer) + public static explicit operator byte*(ArrayPointer arrayPointer) { return (byte*)arrayPointer.PointerAtOffset; } diff --git a/src/ImageSharp/Common/Memory/PinnedBuffer.cs b/src/ImageSharp/Common/Memory/PinnedBuffer{T}.cs similarity index 93% rename from src/ImageSharp/Common/Memory/PinnedBuffer.cs rename to src/ImageSharp/Common/Memory/PinnedBuffer{T}.cs index 201d93b56..d95d67557 100644 --- a/src/ImageSharp/Common/Memory/PinnedBuffer.cs +++ b/src/ImageSharp/Common/Memory/PinnedBuffer{T}.cs @@ -1,3 +1,8 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + namespace ImageSharp { using System; @@ -21,7 +26,7 @@ namespace ImageSharp /// A value indicating wether this instance should return the array to the pool. /// private bool isPoolingOwner; - + /// /// Initializes a new instance of the class. /// @@ -56,11 +61,15 @@ namespace ImageSharp { throw new ArgumentException("Can't initialize a PinnedBuffer with array.Length < count", nameof(array)); } + this.Count = count; this.Array = array; this.Pin(); } + /// + /// Finalizes an instance of the class. + /// ~PinnedBuffer() { this.UnPin(); @@ -71,14 +80,13 @@ namespace ImageSharp /// public bool IsDisposedOrLostArrayOwnership { get; private set; } - /// /// Gets the count of "relevant" elements. Usually be smaller than 'Array.Length' when is pooled. /// public int Count { get; private set; } /// - /// The (pinned) array of elements. + /// Gets the backing pinned array. /// public T[] Array { get; private set; } @@ -96,6 +104,7 @@ namespace ImageSharp { return; } + this.IsDisposedOrLostArrayOwnership = true; this.UnPin(); @@ -147,6 +156,7 @@ namespace ImageSharp { return; } + this.handle.Free(); this.Pointer = IntPtr.Zero; } diff --git a/src/ImageSharp/Common/Memory/PixelDataPool{T}.cs b/src/ImageSharp/Common/Memory/PixelDataPool{T}.cs index f6f6a1042..74f0c1e8e 100644 --- a/src/ImageSharp/Common/Memory/PixelDataPool{T}.cs +++ b/src/ImageSharp/Common/Memory/PixelDataPool{T}.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // diff --git a/src/ImageSharp/Image/ImageBase{TColor}.cs b/src/ImageSharp/Image/ImageBase{TColor}.cs index 9bd760805..c2110eca4 100644 --- a/src/ImageSharp/Image/ImageBase{TColor}.cs +++ b/src/ImageSharp/Image/ImageBase{TColor}.cs @@ -163,9 +163,9 @@ namespace ImageSharp { Guard.NotNull(pixelSource, nameof(pixelSource)); - // TODO: This check was useful. We can introduce a bool PixelAccessor.IsBoundToImage to re-introduce it. - // Guard.IsTrue(pixelSource.PooledMemory, nameof(pixelSource.PooledMemory), "pixelSource must be using pooled memory"); + // TODO: Was this check really useful? If yes, we can define a bool PixelAccessor.IsBoundToImage to re-introduce it: + // Guard.IsTrue(pixelSource.PooledMemory, nameof(pixelSource.PooledMemory), "pixelSource must be using pooled memory"); int newWidth = pixelSource.Width; int newHeight = pixelSource.Height; diff --git a/src/ImageSharp/Image/PixelAccessor{TColor}.cs b/src/ImageSharp/Image/PixelAccessor{TColor}.cs index 3a3f0f5c7..9201270d9 100644 --- a/src/ImageSharp/Image/PixelAccessor{TColor}.cs +++ b/src/ImageSharp/Image/PixelAccessor{TColor}.cs @@ -22,7 +22,7 @@ namespace ImageSharp /// The position of the first pixel in the image. /// private byte* pixelsBase; - + /// /// A value indicating whether this instance of the given entity has been disposed. /// @@ -86,11 +86,6 @@ namespace ImageSharp Guard.MustBeGreaterThan(width, 0, nameof(width)); Guard.MustBeGreaterThan(height, 0, nameof(height)); - //if (!(pixels.Length >= width * height)) - //{ - // throw new ArgumentException($"Pixel array must have the length of at least {width * height}."); - //} - this.SetPixelBufferUnsafe(width, height, pixels); this.ParallelOptions = Configuration.Default.ParallelOptions; @@ -103,7 +98,7 @@ namespace ImageSharp { this.Dispose(); } - + /// /// Gets the pixel buffer array. /// @@ -230,7 +225,7 @@ namespace ImageSharp { return; } - + // Note disposing is done. this.isDisposed = true; @@ -513,36 +508,6 @@ namespace ImageSharp this.RowStride = this.Width * this.PixelSize; } - ///// - ///// Pins the pixels data. - ///// - //private void PinPixels() - //{ - // // unpin any old pixels just incase - // this.UnPinPixels(); - - // this.pixelsHandle = GCHandle.Alloc(this.pixelBuffer, GCHandleType.Pinned); - // this.dataPointer = this.pixelsHandle.AddrOfPinnedObject(); - // this.pixelsBase = (byte*)this.dataPointer.ToPointer(); - //} - - ///// - ///// Unpins pixels data. - ///// - //private void UnPinPixels() - //{ - // if (this.pixelsBase != null) - // { - // if (this.pixelsHandle.IsAllocated) - // { - // this.pixelsHandle.Free(); - // } - - // this.dataPointer = IntPtr.Zero; - // this.pixelsBase = null; - // } - //} - /// /// Copy an area of pixels to the image. /// diff --git a/src/ImageSharp/Image/PixelArea{TColor}.cs b/src/ImageSharp/Image/PixelArea{TColor}.cs index 25840167e..c54de12d6 100644 --- a/src/ImageSharp/Image/PixelArea{TColor}.cs +++ b/src/ImageSharp/Image/PixelArea{TColor}.cs @@ -69,7 +69,6 @@ namespace ImageSharp this.Length = bytes.Length; // TODO: Is this the right value for Length? this.byteBuffer = new PinnedBuffer(bytes); - this.PixelBase = (byte*)this.byteBuffer.Pointer; } @@ -123,7 +122,7 @@ namespace ImageSharp this.byteBuffer = new PinnedBuffer(this.Length); this.PixelBase = (byte*)this.byteBuffer.Pointer; } - + /// /// Gets the data in bytes. /// @@ -163,7 +162,7 @@ namespace ImageSharp /// Gets the width. /// public int Width { get; } - + /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// @@ -249,6 +248,6 @@ namespace ImageSharp nameof(bytes), $"Invalid byte array length. Length {bytes.Length}; Should be {requiredLength}."); } - } + } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Colors/BulkPixelOperationsTests.cs b/tests/ImageSharp.Tests/Colors/BulkPixelOperationsTests.cs index 3682aa78a..4e1083033 100644 --- a/tests/ImageSharp.Tests/Colors/BulkPixelOperationsTests.cs +++ b/tests/ImageSharp.Tests/Colors/BulkPixelOperationsTests.cs @@ -9,11 +9,13 @@ { public class Color : BulkPixelOperationsTests { + // MemberData does not work without redeclaring the public field in the derived test class: public static TheoryData ArraySizesData = new TheoryData { 7, 16, 1111 }; } public class Argb : BulkPixelOperationsTests { + // MemberData does not work without redeclaring the public field in the derived test class: public static TheoryData ArraySizesData = new TheoryData { 7, 16, 1111 }; } } @@ -21,7 +23,8 @@ public abstract class BulkPixelOperationsTests where TColor : struct, IPixel { - protected static TheoryData ArraySizesData = new TheoryData { 7, 16, 1111 }; + // The field is private so it can be safely redeclared in inherited non-abstract test classes. + private static TheoryData ArraySizesData = new TheoryData { 7, 16, 1111 }; [Theory] [MemberData(nameof(ArraySizesData))]