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>
{
/// <summary>
/// Gets the pixels as an array of the given packed 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.
/// Gets the representation of the pixels as an area of contiguous memory in the given pixel format.
/// </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>
{
int count = width * height;
Guard.MustBeGreaterThanOrEqualTo(data.Length, width * height, nameof(data));
var image = new Image<TPixel>(config, width, height);
var dest = new Span<TPixel>(image.Pixels, 0, count);
Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data));
SpanHelper.Copy(data, dest, count);
var image = new Image<TPixel>(config, width, height);
SpanHelper.Copy(data, image.Pixels, count);
return image;
}

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

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

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

@ -48,7 +48,7 @@ namespace ImageSharp
Guard.MustBeGreaterThan(image.Width, 0, "image width");
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;
}

2
src/ImageSharp/Memory/Buffer2D.cs

@ -69,7 +69,7 @@ namespace ImageSharp.Memory
/// <returns>The <see cref="Buffer{T}"/> instance</returns>
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();
return buffer;
}

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

@ -25,9 +25,9 @@ namespace ImageSharp.Tests
{
image.Grayscale(value);
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[1], data[2]);
}

Loading…
Cancel
Save