Browse Source

cleanup & stylecop

af/merge-core
Anton Firszov 9 years ago
parent
commit
e59255d43f
  1. 56
      src/ImageSharp/Colors/PackedPixel/BulkPixelOperations{TColor}.cs
  2. 2
      src/ImageSharp/Colors/PackedPixel/HalfVector2.cs
  3. 2
      src/ImageSharp/Colors/PackedPixel/NormalizedByte4.cs
  4. 34
      src/ImageSharp/Common/Memory/ArrayPointer.cs
  5. 4
      src/ImageSharp/Common/Memory/ArrayPointer{T}.cs
  6. 16
      src/ImageSharp/Common/Memory/PinnedBuffer{T}.cs
  7. 2
      src/ImageSharp/Common/Memory/PixelDataPool{T}.cs
  8. 4
      src/ImageSharp/Image/ImageBase{TColor}.cs
  9. 41
      src/ImageSharp/Image/PixelAccessor{TColor}.cs
  10. 7
      src/ImageSharp/Image/PixelArea{TColor}.cs
  11. 5
      tests/ImageSharp.Tests/Colors/BulkPixelOperationsTests.cs

56
src/ImageSharp/Colors/PackedPixel/BulkPixelOperations{TColor}.cs

@ -78,9 +78,9 @@ namespace ImageSharp
/// <summary>
/// Bulk version of <see cref="IPixel.PackFromBytes(byte, byte, byte, byte)"/> that converts data in <see cref="ComponentOrder.Xyz"/>.
/// </summary>
/// <param name="sourceBytes"></param>
/// <param name="destColors"></param>
/// <param name="count"></param>
/// <param name="sourceBytes">The <see cref="ArrayPointer{Byte}"/> to the source bytes.</param>
/// <param name="destColors">The <see cref="ArrayPointer{TColor}"/> to the destination colors.</param>
/// <param name="count">The number of pixels to convert.</param>
internal virtual void PackFromXyzBytes(
ArrayPointer<byte> sourceBytes,
ArrayPointer<TColor> destColors,
@ -102,15 +102,15 @@ namespace ImageSharp
/// <summary>
/// Bulk version of <see cref="IPixel.ToXyzBytes(byte[], int)"/>.
/// </summary>
/// <param name="sourceColors"></param>
/// <param name="destBytes"></param>
/// <param name="count"></param>
/// <param name="sourceColors">The <see cref="ArrayPointer{TColor}"/> to the source colors.</param>
/// <param name="destBytes">The <see cref="ArrayPointer{Byte}"/> to the destination bytes.</param>
/// <param name="count">The number of pixels to convert.</param>
internal virtual void PackToXyzBytes(ArrayPointer<TColor> sourceColors, ArrayPointer<byte> 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<TColor>(sp);
c.ToXyzBytes(dest, i);
@ -121,9 +121,9 @@ namespace ImageSharp
/// <summary>
/// Bulk version of <see cref="IPixel.PackFromBytes(byte, byte, byte, byte)"/> that converts data in <see cref="ComponentOrder.Xyzw"/>.
/// </summary>
/// <param name="sourceBytes"></param>
/// <param name="destColors"></param>
/// <param name="count"></param>
/// <param name="sourceBytes">The <see cref="ArrayPointer{Byte}"/> to the source bytes.</param>
/// <param name="destColors">The <see cref="ArrayPointer{TColor}"/> to the destination colors.</param>
/// <param name="count">The number of pixels to convert.</param>
internal virtual void PackFromXyzwBytes(
ArrayPointer<byte> sourceBytes,
ArrayPointer<TColor> destColors,
@ -145,9 +145,9 @@ namespace ImageSharp
/// <summary>
/// Bulk version of <see cref="IPixel.ToXyzwBytes(byte[], int)"/>.
/// </summary>
/// <param name="sourceColors"></param>
/// <param name="destBytes"></param>
/// <param name="count"></param>
/// <param name="sourceColors">The <see cref="ArrayPointer{TColor}"/> to the source colors.</param>
/// <param name="destBytes">The <see cref="ArrayPointer{Byte}"/> to the destination bytes.</param>
/// <param name="count">The number of pixels to convert.</param>
internal virtual void PackToXyzwBytes(
ArrayPointer<TColor> sourceColors,
ArrayPointer<byte> 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<TColor>(sp);
c.ToXyzwBytes(dest, i);
@ -167,9 +167,9 @@ namespace ImageSharp
/// <summary>
/// Bulk version of <see cref="IPixel.PackFromBytes(byte, byte, byte, byte)"/> that converts data in <see cref="ComponentOrder.Zyx"/>.
/// </summary>
/// <param name="sourceBytes"></param>
/// <param name="destColors"></param>
/// <param name="count"></param>
/// <param name="sourceBytes">The <see cref="ArrayPointer{Byte}"/> to the source bytes.</param>
/// <param name="destColors">The <see cref="ArrayPointer{TColor}"/> to the destination colors.</param>
/// <param name="count">The number of pixels to convert.</param>
internal virtual void PackFromZyxBytes(
ArrayPointer<byte> sourceBytes,
ArrayPointer<TColor> destColors,
@ -191,15 +191,15 @@ namespace ImageSharp
/// <summary>
/// Bulk version of <see cref="IPixel.ToZyxBytes(byte[], int)"/>.
/// </summary>
/// <param name="sourceColors"></param>
/// <param name="destBytes"></param>
/// <param name="count"></param>
/// <param name="sourceColors">The <see cref="ArrayPointer{TColor}"/> to the source colors.</param>
/// <param name="destBytes">The <see cref="ArrayPointer{Byte}"/> to the destination bytes.</param>
/// <param name="count">The number of pixels to convert.</param>
internal virtual void PackToZyxBytes(ArrayPointer<TColor> sourceColors, ArrayPointer<byte> 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<TColor>(sp);
c.ToZyxBytes(dest, i);
@ -210,9 +210,9 @@ namespace ImageSharp
/// <summary>
/// Bulk version of <see cref="IPixel.PackFromBytes(byte, byte, byte, byte)"/> that converts data in <see cref="ComponentOrder.Zyxw"/>.
/// </summary>
/// <param name="sourceBytes"></param>
/// <param name="destColors"></param>
/// <param name="count"></param>
/// <param name="sourceBytes">The <see cref="ArrayPointer{Byte}"/> to the source bytes.</param>
/// <param name="destColors">The <see cref="ArrayPointer{TColor}"/> to the destination colors.</param>
/// <param name="count">The number of pixels to convert.</param>
internal virtual void PackFromZyxwBytes(
ArrayPointer<byte> sourceBytes,
ArrayPointer<TColor> destColors,
@ -234,9 +234,9 @@ namespace ImageSharp
/// <summary>
/// Bulk version of <see cref="IPixel.ToZyxwBytes(byte[], int)"/>.
/// </summary>
/// <param name="sourceColors"></param>
/// <param name="destBytes"></param>
/// <param name="count"></param>
/// <param name="sourceColors">The <see cref="ArrayPointer{TColor}"/> to the source colors.</param>
/// <param name="destBytes">The <see cref="ArrayPointer{Byte}"/> to the destination bytes.</param>
/// <param name="count">The number of pixels to convert.</param>
internal virtual void PackToZyxwBytes(
ArrayPointer<TColor> sourceColors,
ArrayPointer<byte> 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<TColor>(sp);
c.ToZyxwBytes(dest, i);

2
src/ImageSharp/Colors/PackedPixel/HalfVector2.cs

@ -45,7 +45,7 @@ namespace ImageSharp
/// <inheritdoc/>
public uint PackedValue { get; set; }
/// <inheritdoc />
public BulkPixelOperations<HalfVector2> BulkOperations => new BulkPixelOperations<HalfVector2>();

2
src/ImageSharp/Colors/PackedPixel/NormalizedByte4.cs

@ -52,7 +52,7 @@ namespace ImageSharp
/// <inheritdoc/>
public uint PackedValue { get; set; }
/// <inheritdoc />
public BulkPixelOperations<NormalizedByte4> BulkOperations => new BulkPixelOperations<NormalizedByte4>();

34
src/ImageSharp/Common/Memory/ArrayPointer.cs

@ -1,3 +1,8 @@
// <copyright file="ArrayPointer.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp
{
using System.Runtime.CompilerServices;
@ -7,6 +12,12 @@ namespace ImageSharp
/// </summary>
internal static class ArrayPointer
{
/// <summary>
/// Gets an <see cref="ArrayPointer{T}"/> to the beginning of the raw data in 'buffer'.
/// </summary>
/// <typeparam name="T">The element type</typeparam>
/// <param name="buffer">The input <see cref="PinnedBuffer{T}"/></param>
/// <returns>The <see cref="ArrayPointer{T}"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe ArrayPointer<T> GetArrayPointer<T>(this PinnedBuffer<T> buffer)
where T : struct
@ -14,6 +25,13 @@ namespace ImageSharp
return new ArrayPointer<T>(buffer.Array, (void*)buffer.Pointer);
}
/// <summary>
/// Copy 'count' number of elements of the same type from 'source' to 'dest'
/// </summary>
/// <typeparam name="T">The element type.</typeparam>
/// <param name="source">The input <see cref="ArrayPointer{T}"/></param>
/// <param name="destination">The destination <see cref="ArrayPointer{T}"/>.</param>
/// <param name="count">The number of elements to copy</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Copy<T>(ArrayPointer<T> source, ArrayPointer<T> destination, int count)
where T : struct
@ -21,6 +39,13 @@ namespace ImageSharp
Unsafe.CopyBlock((void*)source.PointerAtOffset, (void*)destination.PointerAtOffset, USizeOf<T>(count));
}
/// <summary>
/// Copy 'countInSource' elements of <typeparamref name="T"/> from 'source' into the raw byte buffer 'destination'.
/// </summary>
/// <typeparam name="T">The element type.</typeparam>
/// <param name="source">The source buffer of <typeparamref name="T"/> elements to copy from.</param>
/// <param name="destination">The destination buffer.</param>
/// <param name="countInSource">The number of elements to copy from 'source'</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Copy<T>(ArrayPointer<T> source, ArrayPointer<byte> destination, int countInSource)
where T : struct
@ -28,6 +53,13 @@ namespace ImageSharp
Unsafe.CopyBlock((void*)source.PointerAtOffset, (void*)destination.PointerAtOffset, USizeOf<T>(countInSource));
}
/// <summary>
/// Copy 'countInDest' number of <typeparamref name="T"/> elements into 'dest' from a raw byte buffer defined by 'source'.
/// </summary>
/// <typeparam name="T">The element type.</typeparam>
/// <param name="source">The raw source buffer to copy from"/></param>
/// <param name="destination">The destination buffer"/></param>
/// <param name="countInDest">The number of <typeparamref name="T"/> elements to copy.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Copy<T>(ArrayPointer<byte> source, ArrayPointer<T> destination, int countInDest)
where T : struct
@ -38,6 +70,7 @@ namespace ImageSharp
/// <summary>
/// Gets the size of `count` elements in bytes.
/// </summary>
/// <typeparam name="T">The element type.</typeparam>
/// <param name="count">The count of the elements</param>
/// <returns>The size in bytes as int</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -47,6 +80,7 @@ namespace ImageSharp
/// <summary>
/// Gets the size of `count` elements in bytes as UInt32
/// </summary>
/// <typeparam name="T">The element type.</typeparam>
/// <param name="count">The count of the elements</param>
/// <returns>The size in bytes as UInt32</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]

4
src/ImageSharp/Common/Memory/ArrayPointer{T}.cs

@ -73,7 +73,7 @@ namespace ImageSharp
/// </summary>
/// <param name="arrayPointer">The <see cref="ArrayPointer{T}"/> to convert</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator void* (ArrayPointer<T> arrayPointer)
public static explicit operator void*(ArrayPointer<T> arrayPointer)
{
return (void*)arrayPointer.PointerAtOffset;
}
@ -83,7 +83,7 @@ namespace ImageSharp
/// </summary>
/// <param name="arrayPointer">The <see cref="ArrayPointer{T}"/> to convert</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator byte* (ArrayPointer<T> arrayPointer)
public static explicit operator byte*(ArrayPointer<T> arrayPointer)
{
return (byte*)arrayPointer.PointerAtOffset;
}

16
src/ImageSharp/Common/Memory/PinnedBuffer.cs → src/ImageSharp/Common/Memory/PinnedBuffer{T}.cs

@ -1,3 +1,8 @@
// <copyright file="PinnedBuffer{T}.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp
{
using System;
@ -21,7 +26,7 @@ namespace ImageSharp
/// A value indicating wether this <see cref="PinnedBuffer{T}"/> instance should return the array to the pool.
/// </summary>
private bool isPoolingOwner;
/// <summary>
/// Initializes a new instance of the <see cref="PinnedBuffer{T}"/> class.
/// </summary>
@ -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();
}
/// <summary>
/// Finalizes an instance of the <see cref="PinnedBuffer{T}"/> class.
/// </summary>
~PinnedBuffer()
{
this.UnPin();
@ -71,14 +80,13 @@ namespace ImageSharp
/// </summary>
public bool IsDisposedOrLostArrayOwnership { get; private set; }
/// <summary>
/// Gets the count of "relevant" elements. Usually be smaller than 'Array.Length' when <see cref="Array"/> is pooled.
/// </summary>
public int Count { get; private set; }
/// <summary>
/// The (pinned) array of elements.
/// Gets the backing pinned array.
/// </summary>
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;
}

2
src/ImageSharp/Common/Memory/PixelDataPool{T}.cs

@ -1,4 +1,4 @@
// <copyright file="PixelDataPool{TColor}.cs" company="James Jackson-South">
// <copyright file="PixelDataPool{T}.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>

4
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<TColor>.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<TColor>.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;

41
src/ImageSharp/Image/PixelAccessor{TColor}.cs

@ -22,7 +22,7 @@ namespace ImageSharp
/// The position of the first pixel in the image.
/// </summary>
private byte* pixelsBase;
/// <summary>
/// A value indicating whether this instance of the given entity has been disposed.
/// </summary>
@ -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();
}
/// <summary>
/// Gets the pixel buffer array.
/// </summary>
@ -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;
}
///// <summary>
///// Pins the pixels data.
///// </summary>
//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();
//}
///// <summary>
///// Unpins pixels data.
///// </summary>
//private void UnPinPixels()
//{
// if (this.pixelsBase != null)
// {
// if (this.pixelsHandle.IsAllocated)
// {
// this.pixelsHandle.Free();
// }
// this.dataPointer = IntPtr.Zero;
// this.pixelsBase = null;
// }
//}
/// <summary>
/// Copy an area of pixels to the image.
/// </summary>

7
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<byte>(bytes);
this.PixelBase = (byte*)this.byteBuffer.Pointer;
}
@ -123,7 +122,7 @@ namespace ImageSharp
this.byteBuffer = new PinnedBuffer<byte>(this.Length);
this.PixelBase = (byte*)this.byteBuffer.Pointer;
}
/// <summary>
/// Gets the data in bytes.
/// </summary>
@ -163,7 +162,7 @@ namespace ImageSharp
/// Gets the width.
/// </summary>
public int Width { get; }
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
@ -249,6 +248,6 @@ namespace ImageSharp
nameof(bytes),
$"Invalid byte array length. Length {bytes.Length}; Should be {requiredLength}.");
}
}
}
}
}

5
tests/ImageSharp.Tests/Colors/BulkPixelOperationsTests.cs

@ -9,11 +9,13 @@
{
public class Color : BulkPixelOperationsTests<ImageSharp.Color>
{
// MemberData does not work without redeclaring the public field in the derived test class:
public static TheoryData<int> ArraySizesData = new TheoryData<int> { 7, 16, 1111 };
}
public class Argb : BulkPixelOperationsTests<ImageSharp.Argb>
{
// MemberData does not work without redeclaring the public field in the derived test class:
public static TheoryData<int> ArraySizesData = new TheoryData<int> { 7, 16, 1111 };
}
}
@ -21,7 +23,8 @@
public abstract class BulkPixelOperationsTests<TColor>
where TColor : struct, IPixel<TColor>
{
protected static TheoryData<int> ArraySizesData = new TheoryData<int> { 7, 16, 1111 };
// The field is private so it can be safely redeclared in inherited non-abstract test classes.
private static TheoryData<int> ArraySizesData = new TheoryData<int> { 7, 16, 1111 };
[Theory]
[MemberData(nameof(ArraySizesData))]

Loading…
Cancel
Save