Browse Source

started implementing operations

af/merge-core
Anton Firszov 9 years ago
parent
commit
bf0d03fe89
  1. 58
      src/ImageSharp/Colors/PackedPixel/BulkPixelOperations.cs
  2. 21
      src/ImageSharp/Common/Memory/ArrayPointer{T}.cs
  3. 6
      tests/ImageSharp.Tests/Colors/BulkPixelOperationsTests.cs
  4. 8
      tests/ImageSharp.Tests/Formats/Jpg/JpegProfilingBenchmarks.cs

58
src/ImageSharp/Colors/PackedPixel/BulkPixelOperations.cs

@ -1,17 +1,34 @@
namespace ImageSharp
{
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
public unsafe class BulkPixelOperations<TColor>
where TColor : struct, IPixel<TColor>
{
public static BulkPixelOperations<TColor> Instance { get; } = default(TColor).BulkOperations;
private static readonly int ColorSize = Unsafe.SizeOf<TColor>();
internal virtual void PackFromVector4(
ArrayPointer<Vector4> sourceVectors,
ArrayPointer<TColor> destColors,
int count)
{
Vector4* sp = (Vector4*)sourceVectors.PointerAtOffset;
byte* dp = (byte*)destColors;
for (int i = 0; i < count; i++)
{
Vector4 v = Unsafe.Read<Vector4>(sp);
TColor c = default(TColor);
c.PackFromVector4(v);
Unsafe.Write(dp, c);
sp++;
dp += ColorSize;
}
}
internal virtual void PackToVector4(
@ -19,16 +36,51 @@
ArrayPointer<Vector4> destVectors,
int count)
{
byte* sp = (byte*)sourceColors;
Vector4* dp = (Vector4*)destVectors.PointerAtOffset;
for (int i = 0; i < count; i++)
{
TColor c = Unsafe.Read<TColor>(sp);
*dp = c.ToVector4();
sp += ColorSize;
dp++;
}
}
internal virtual void PackToXyzBytes(ArrayPointer<TColor> sourceColors, ArrayPointer<byte> destBytes, int count)
internal virtual void PackFromXyzBytes(
ArrayPointer<byte> sourceBytes,
ArrayPointer<TColor> destColors,
int count)
{
byte* sp = (byte*)sourceBytes;
byte* dp = (byte*)destColors.PointerAtOffset;
for (int i = 0; i < count; i++)
{
TColor c = default(TColor);
c.PackFromBytes(sp[0], sp[1], sp[2], 255);
Unsafe.Write(dp, c);
sp += 3;
dp += ColorSize;
}
}
internal virtual void PackFromXyzBytes(ArrayPointer<byte> sourceBytes, ArrayPointer<TColor> destColors, int count)
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)
{
TColor c = Unsafe.Read<TColor>(sp);
c.ToXyzBytes(dest, i);
}
}
internal virtual void PackToXyzwBytes(ArrayPointer<TColor> sourceColors, ArrayPointer<byte> destBytes, int count)
{
}

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

@ -73,6 +73,7 @@ namespace ImageSharp
/// </summary>
/// <param name="offset">The offset in number of elements</param>
/// <returns>The offseted (sliced) ArrayPointer</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ArrayPointer<T> Slice(int offset)
{
ArrayPointer<T> result = default(ArrayPointer<T>);
@ -81,5 +82,25 @@ namespace ImageSharp
result.PointerAtOffset = this.PointerAtOffset + (Unsafe.SizeOf<T>() * offset);
return result;
}
/// <summary>
/// Convertes <see cref="ArrayPointer{T}"/> instance to a raw 'void*' pointer
/// </summary>
/// <param name="arrayPointer">The <see cref="ArrayPointer{T}"/> to convert</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator void*(ArrayPointer<T> arrayPointer)
{
return (void*)arrayPointer.PointerAtOffset;
}
/// <summary>
/// Convertes <see cref="ArrayPointer{T}"/> instance to a raw 'byte*' pointer
/// </summary>
/// <param name="arrayPointer">The <see cref="ArrayPointer{T}"/> to convert</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator byte* (ArrayPointer<T> arrayPointer)
{
return (byte*)arrayPointer.PointerAtOffset;
}
}
}

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

@ -7,12 +7,14 @@
public abstract class BulkPixelOperationsTests
{
public class ColorPixels : BulkPixelOperationsTests<Color>
public class Color : BulkPixelOperationsTests<ImageSharp.Color>
{
public static TheoryData<int> ArraySizesData = new TheoryData<int> { 7, 16, 1111 };
}
public class ArgbPixels : BulkPixelOperationsTests<Argb>
public class Argb : BulkPixelOperationsTests<ImageSharp.Argb>
{
public static TheoryData<int> ArraySizesData = new TheoryData<int> { 7, 16, 1111 };
}
}

8
tests/ImageSharp.Tests/Formats/Jpg/JpegProfilingBenchmarks.cs

@ -38,10 +38,10 @@ namespace ImageSharp.Tests
{
const int ExecutionCount = 30;
if (!Vector.IsHardwareAccelerated)
{
throw new Exception("Vector.IsHardwareAccelerated == false! ('prefer32 bit' enabled?)");
}
//if (!Vector.IsHardwareAccelerated)
//{
// throw new Exception("Vector.IsHardwareAccelerated == false! ('prefer32 bit' enabled?)");
//}
string path = TestFile.GetPath(fileName);
byte[] bytes = File.ReadAllBytes(path);

Loading…
Cancel
Save