Browse Source

Remove Span member from ImageBase

pull/221/head
James Jackson-South 9 years ago
parent
commit
d87101ed7b
  1. 6
      src/ImageSharp/Image/IImageBase{TPixel}.cs
  2. 7
      src/ImageSharp/Image/Image.LoadPixelData.cs
  3. 41
      src/ImageSharp/Image/ImageBase{TPixel}.cs
  4. 2
      src/ImageSharp/Image/PixelAccessor{TPixel}.cs
  5. 2
      src/ImageSharp/Memory/Buffer2D.cs
  6. 4
      tests/ImageSharp.Tests/Processors/Filters/GrayscaleTest.cs

6
src/ImageSharp/Image/IImageBase{TPixel}.cs

@ -16,10 +16,8 @@ namespace ImageSharp
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
/// <summary> /// <summary>
/// Gets the pixels as an array of the given packed pixel format. /// Gets the representation of the pixels as an area of contiguous memory in the given pixel format.
/// Important. Due to the nature in the way this is constructed do not rely on the length
/// of the array for calculations. Use Width * Height.
/// </summary> /// </summary>
TPixel[] Pixels { get; } Span<TPixel> Pixels { get; }
} }
} }

7
src/ImageSharp/Image/Image.LoadPixelData.cs

@ -68,11 +68,10 @@ namespace ImageSharp
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
int count = width * height; int count = width * height;
Guard.MustBeGreaterThanOrEqualTo(data.Length, width * height, nameof(data)); Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data));
var image = new Image<TPixel>(config, width, height);
var dest = new Span<TPixel>(image.Pixels, 0, count);
SpanHelper.Copy(data, dest, count); var image = new Image<TPixel>(config, width, height);
SpanHelper.Copy(data, image.Pixels, count);
return image; return image;
} }

41
src/ImageSharp/Image/ImageBase{TPixel}.cs

@ -32,15 +32,12 @@ namespace ImageSharp
/// </summary> /// </summary>
public const int MaxHeight = int.MaxValue; public const int MaxHeight = int.MaxValue;
#pragma warning disable SA1401 // Fields must be private
/// <summary> /// <summary>
/// The image pixels /// The image pixels. Not private as Buffer2D requires an array in its constructor.
/// </summary> /// </summary>
private TPixel[] pixelBuffer; internal TPixel[] PixelBuffer;
#pragma warning restore SA1401 // Fields must be private
/// <summary>
/// The span representing the pixel buffer
/// </summary>
private Span<TPixel> span;
/// <summary> /// <summary>
/// A value indicating whether this instance of the given entity has been disposed. /// A value indicating whether this instance of the given entity has been disposed.
@ -116,7 +113,7 @@ namespace ImageSharp
} }
/// <inheritdoc/> /// <inheritdoc/>
public TPixel[] Pixels => this.pixelBuffer; public Span<TPixel> Pixels => new Span<TPixel>(this.PixelBuffer, 0, this.Width * this.Height);
/// <inheritdoc/> /// <inheritdoc/>
public int Width { get; private set; } public int Width { get; private set; }
@ -147,14 +144,14 @@ namespace ImageSharp
get get
{ {
this.CheckCoordinates(x, y); this.CheckCoordinates(x, y);
return this.pixelBuffer[(y * this.Width) + x]; return this.PixelBuffer[(y * this.Width) + x];
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
set set
{ {
this.CheckCoordinates(x, y); this.CheckCoordinates(x, y);
this.pixelBuffer[(y * this.Width) + x] = value; this.PixelBuffer[(y * this.Width) + x] = value;
} }
} }
@ -168,7 +165,7 @@ namespace ImageSharp
public ref TPixel GetPixelReference(int x, int y) public ref TPixel GetPixelReference(int x, int y)
{ {
this.CheckCoordinates(x, y); this.CheckCoordinates(x, y);
return ref this.pixelBuffer[(y * this.Width) + x]; return ref this.PixelBuffer[(y * this.Width) + x];
} }
/// <summary> /// <summary>
@ -180,7 +177,7 @@ namespace ImageSharp
public Span<TPixel> GetRowSpan(int y) public Span<TPixel> GetRowSpan(int y)
{ {
this.CheckCoordinates(y); this.CheckCoordinates(y);
return this.span.Slice(y * this.Width, this.Width); return this.Pixels.Slice(y * this.Width, this.Width);
} }
/// <summary> /// <summary>
@ -193,7 +190,7 @@ namespace ImageSharp
public Span<TPixel> GetRowSpan(int x, int y) public Span<TPixel> GetRowSpan(int x, int y)
{ {
this.CheckCoordinates(x, y); this.CheckCoordinates(x, y);
return this.span.Slice((y * this.Width) + x, this.Width - x); return this.Pixels.Slice((y * this.Width) + x, this.Width - x);
} }
/// <summary> /// <summary>
@ -237,7 +234,7 @@ namespace ImageSharp
/// <param name="target">The target pixel buffer accessor.</param> /// <param name="target">The target pixel buffer accessor.</param>
internal void CopyTo(PixelAccessor<TPixel> target) internal void CopyTo(PixelAccessor<TPixel> target)
{ {
SpanHelper.Copy(this.span, target.PixelBuffer.Span); SpanHelper.Copy(this.Pixels, target.PixelBuffer.Span);
} }
/// <summary> /// <summary>
@ -251,12 +248,11 @@ namespace ImageSharp
int newWidth = pixelSource.Width; int newWidth = pixelSource.Width;
int newHeight = pixelSource.Height; int newHeight = pixelSource.Height;
// Push my memory into the accessor (which in turn unpins the old puffer ready for the images use) // Push my memory into the accessor (which in turn unpins the old buffer ready for the images use)
TPixel[] newPixels = pixelSource.ReturnCurrentColorsAndReplaceThemInternally(this.Width, this.Height, this.pixelBuffer); TPixel[] newPixels = pixelSource.ReturnCurrentColorsAndReplaceThemInternally(this.Width, this.Height, this.PixelBuffer);
this.Width = newWidth; this.Width = newWidth;
this.Height = newHeight; this.Height = newHeight;
this.pixelBuffer = newPixels; this.PixelBuffer = newPixels;
this.span = new Span<TPixel>(this.pixelBuffer);
} }
/// <summary> /// <summary>
@ -307,8 +303,7 @@ namespace ImageSharp
/// </summary> /// </summary>
private void RentPixels() private void RentPixels()
{ {
this.pixelBuffer = PixelDataPool<TPixel>.Rent(this.Width * this.Height); this.PixelBuffer = PixelDataPool<TPixel>.Rent(this.Width * this.Height);
this.span = new Span<TPixel>(this.pixelBuffer);
} }
/// <summary> /// <summary>
@ -316,8 +311,8 @@ namespace ImageSharp
/// </summary> /// </summary>
private void ReturnPixels() private void ReturnPixels()
{ {
PixelDataPool<TPixel>.Return(this.pixelBuffer); PixelDataPool<TPixel>.Return(this.PixelBuffer);
this.pixelBuffer = null; this.PixelBuffer = null;
} }
/// <summary> /// <summary>
@ -325,7 +320,7 @@ namespace ImageSharp
/// </summary> /// </summary>
private void ClearPixels() private void ClearPixels()
{ {
Array.Clear(this.pixelBuffer, 0, this.Width * this.Height); Array.Clear(this.PixelBuffer, 0, this.Width * this.Height);
} }
/// <summary> /// <summary>

2
src/ImageSharp/Image/PixelAccessor{TPixel}.cs

@ -48,7 +48,7 @@ namespace ImageSharp
Guard.MustBeGreaterThan(image.Width, 0, "image width"); Guard.MustBeGreaterThan(image.Width, 0, "image width");
Guard.MustBeGreaterThan(image.Height, 0, "image height"); Guard.MustBeGreaterThan(image.Height, 0, "image height");
this.SetPixelBufferUnsafe(image.Width, image.Height, image.Pixels); this.SetPixelBufferUnsafe(image.Width, image.Height, image.PixelBuffer);
this.ParallelOptions = image.Configuration.ParallelOptions; this.ParallelOptions = image.Configuration.ParallelOptions;
} }

2
src/ImageSharp/Memory/Buffer2D.cs

@ -69,7 +69,7 @@ namespace ImageSharp.Memory
/// <returns>The <see cref="Buffer{T}"/> instance</returns> /// <returns>The <see cref="Buffer{T}"/> instance</returns>
public static Buffer2D<T> CreateClean(int width, int height) public static Buffer2D<T> CreateClean(int width, int height)
{ {
Buffer2D<T> buffer = new Buffer2D<T>(width, height); var buffer = new Buffer2D<T>(width, height);
buffer.Clear(); buffer.Clear();
return buffer; return buffer;
} }

4
tests/ImageSharp.Tests/Processors/Filters/GrayscaleTest.cs

@ -25,9 +25,9 @@ namespace ImageSharp.Tests
{ {
image.Grayscale(value); image.Grayscale(value);
byte[] data = new byte[3]; byte[] data = new byte[3];
foreach (TPixel p in image.Pixels) for (int i = 0; i < image.Pixels.Length; i++)
{ {
p.ToXyzBytes(data, 0); image.Pixels[i].ToXyzBytes(data, 0);
Assert.Equal(data[0], data[1]); Assert.Equal(data[0], data[1]);
Assert.Equal(data[1], data[2]); Assert.Equal(data[1], data[2]);
} }

Loading…
Cancel
Save