Browse Source

IPixelAcessor<TPacked>

Former-commit-id: 5b04e85fc18ebe98bd122766c58379a1666d0cbb
Former-commit-id: 6e626d6a6e55aa75922a3c1aeeb4094390b4e185
Former-commit-id: f5293704eec3ece4aceaebff57218bce383d02e8
pull/1/head
James Jackson-South 10 years ago
parent
commit
b04b6298e0
  1. 4
      src/ImageProcessorCore/Bootstrapper.cs
  2. 9
      src/ImageProcessorCore/Formats/Bmp/BmpEncoderCore.cs
  3. 2
      src/ImageProcessorCore/IImageBase.cs
  4. 2
      src/ImageProcessorCore/Image.cs
  5. 2
      src/ImageProcessorCore/ImageBase.cs
  6. 2
      src/ImageProcessorCore/ImageFrame.cs
  7. 6
      src/ImageProcessorCore/PixelAccessor/Bgra32PixelAccessor.cs
  8. 28
      src/ImageProcessorCore/PixelAccessor/IPixelAccessor.cs
  9. 10
      src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs
  10. 2
      tests/ImageProcessorCore.Benchmarks/Image/GetSetPixel.cs

4
src/ImageProcessorCore/Bootstrapper.cs

@ -73,13 +73,13 @@ namespace ImageProcessorCore
/// <typeparam name="TPackedVector">The type of pixel data.</typeparam>
/// <param name="image">The image</param>
/// <returns>The <see cref="IPixelAccessor"/></returns>
public IPixelAccessor GetPixelAccessor<TPackedVector>(IImageBase image)
public IPixelAccessor<TPackedVector> GetPixelAccessor<TPackedVector>(IImageBase image)
where TPackedVector : IPackedVector, new()
{
Type packed = typeof(TPackedVector);
if (this.pixelAccessors.ContainsKey(packed))
{
return this.pixelAccessors[packed].Invoke(image);
return (IPixelAccessor<TPackedVector>)this.pixelAccessors[packed].Invoke(image);
}
throw new NotSupportedException($"PixelAccessor cannot be loaded for {packed}:");

9
src/ImageProcessorCore/Formats/Bmp/BmpEncoderCore.cs

@ -137,7 +137,7 @@ namespace ImageProcessorCore.Formats
amount = 4 - amount;
}
using (IPixelAccessor pixels = image.Lock())
using (IPixelAccessor<TPackedVector> pixels = image.Lock())
{
switch (this.bmpBitsPerPixel)
{
@ -155,10 +155,12 @@ namespace ImageProcessorCore.Formats
/// <summary>
/// Writes the 32bit color palette to the stream.
/// </summary>
/// <typeparam name="TPackedVector">The type of pixels contained within the image.</typeparam>
/// <param name="writer">The <see cref="EndianBinaryWriter"/> containing the stream to write to.</param>
/// <param name="pixels">The <see cref="IPixelAccessor"/> containing pixel data.</param>
/// <param name="amount">The amount to pad each row by.</param>
private void Write32bit(EndianBinaryWriter writer, IPixelAccessor pixels, int amount)
private void Write32bit<TPackedVector>(EndianBinaryWriter writer, IPixelAccessor<TPackedVector> pixels, int amount)
where TPackedVector : IPackedVector, new()
{
for (int y = pixels.Height - 1; y >= 0; y--)
{
@ -183,7 +185,8 @@ namespace ImageProcessorCore.Formats
/// <param name="writer">The <see cref="EndianBinaryWriter"/> containing the stream to write to.</param>
/// <param name="pixels">The <see cref="IPixelAccessor"/> containing pixel data.</param>
/// <param name="amount">The amount to pad each row by.</param>
private void Write24bit(EndianBinaryWriter writer, IPixelAccessor pixels, int amount)
private void Write24bit<TPackedVector>(EndianBinaryWriter writer, IPixelAccessor<TPackedVector> pixels, int amount)
where TPackedVector : IPackedVector, new()
{
for (int y = pixels.Height - 1; y >= 0; y--)
{

2
src/ImageProcessorCore/IImageBase.cs

@ -7,7 +7,7 @@ namespace ImageProcessorCore
{
TPackedVector[] Pixels { get; }
void ClonePixels(int width, int height, TPackedVector[] pixels);
IPixelAccessor Lock();
IPixelAccessor<TPackedVector> Lock();
void SetPixels(int width, int height, TPackedVector[] pixels);
}

2
src/ImageProcessorCore/Image.cs

@ -184,7 +184,7 @@ namespace ImageProcessorCore
public IImageFormat CurrentImageFormat { get; internal set; }
/// <inheritdoc/>
public override IPixelAccessor Lock()
public override IPixelAccessor<TPackedVector> Lock()
{
return Bootstrapper.Instance.GetPixelAccessor<TPackedVector>(this);
}

2
src/ImageProcessorCore/ImageBase.cs

@ -196,6 +196,6 @@ namespace ImageProcessorCore
/// </remarks>
/// </summary>
/// <returns>The <see cref="IPixelAccessor"/></returns>
public abstract IPixelAccessor Lock();
public abstract IPixelAccessor<TPackedVector> Lock();
}
}

2
src/ImageProcessorCore/ImageFrame.cs

@ -33,7 +33,7 @@ namespace ImageProcessorCore
}
/// <inheritdoc />
public override IPixelAccessor Lock()
public override IPixelAccessor<TPackedVector> Lock()
{
return Bootstrapper.Instance.GetPixelAccessor<TPackedVector>(this);
}

6
src/ImageProcessorCore/PixelAccessor/Bgra32PixelAccessor.cs

@ -15,7 +15,7 @@ namespace ImageProcessorCore
/// The image data is always stored in <see cref="Bgra32"/> format, where the blue, green, red, and
/// alpha values are 8 bit unsigned bytes.
/// </remarks>
public sealed unsafe class Bgra32PixelAccessor : IPixelAccessor
public sealed unsafe class Bgra32PixelAccessor : IPixelAccessor<Bgra32>
{
/// <summary>
@ -90,7 +90,7 @@ namespace ImageProcessorCore
/// than zero and smaller than the width of the pixel.
/// </param>
/// <returns>The <see cref="IPackedVector"/> at the specified position.</returns>
public IPackedVector this[int x, int y]
public Bgra32 this[int x, int y]
{
get
{
@ -121,7 +121,7 @@ namespace ImageProcessorCore
throw new ArgumentOutOfRangeException(nameof(y), "Value cannot be less than zero or greater than the bitmap height.");
}
#endif
*(this.pixelsBase + ((y * this.Width) + x)) = (Bgra32)value;
*(this.pixelsBase + ((y * this.Width) + x)) = value;
}
}

28
src/ImageProcessorCore/PixelAccessor/IPixelAccessor.cs

@ -10,18 +10,9 @@ namespace ImageProcessorCore
/// <summary>
/// Encapsulates properties to provides per-pixel access to an images pixels.
/// </summary>
public interface IPixelAccessor : IDisposable
public interface IPixelAccessor<TPackedVector> : IPixelAccessor
where TPackedVector : IPackedVector, new()
{
/// <summary>
/// Gets the width of the image in pixels.
/// </summary>
int Width { get; }
/// <summary>
/// Gets the height of the image in pixels.
/// </summary>
int Height { get; }
/// <summary>
/// Gets or sets the pixel at the specified position.
/// </summary>
@ -34,10 +25,23 @@ namespace ImageProcessorCore
/// than zero and smaller than the width of the pixel.
/// </param>
/// <returns>The <see cref="TPackedVector"/> at the specified position.</returns>
IPackedVector this[int x, int y]
TPackedVector this[int x, int y]
{
get;
set;
}
}
public interface IPixelAccessor : IDisposable
{
/// <summary>
/// Gets the width of the image in pixels.
/// </summary>
int Width { get; }
/// <summary>
/// Gets the height of the image in pixels.
/// </summary>
int Height { get; }
}
}

10
src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs

@ -78,8 +78,8 @@ namespace ImageProcessorCore.Processors
float widthFactor = sourceRectangle.Width / (float)targetRectangle.Width;
float heightFactor = sourceRectangle.Height / (float)targetRectangle.Height;
using (IPixelAccessor sourcePixels = source.Lock())
using (IPixelAccessor targetPixels = target.Lock())
using (IPixelAccessor<TPackedVector> sourcePixels = source.Lock())
using (IPixelAccessor<TPackedVector> targetPixels = target.Lock())
{
Parallel.For(
startY,
@ -115,9 +115,9 @@ namespace ImageProcessorCore.Processors
// First process the columns. Since we are not using multiple threads startY and endY
// are the upper and lower bounds of the source rectangle.
Image<TPackedVector> firstPass = new Image<TPackedVector>(target.Width, source.Height);
using (IPixelAccessor sourcePixels = source.Lock())
using (IPixelAccessor firstPassPixels = firstPass.Lock())
using (IPixelAccessor targetPixels = target.Lock())
using (IPixelAccessor<TPackedVector> sourcePixels = source.Lock())
using (IPixelAccessor<TPackedVector> firstPassPixels = firstPass.Lock())
using (IPixelAccessor<TPackedVector> targetPixels = target.Lock())
{
Parallel.For(
0,

2
tests/ImageProcessorCore.Benchmarks/Image/GetSetPixel.cs

@ -24,7 +24,7 @@
public Bgra32 ResizeCore()
{
Image<Bgra32> image = new Image<Bgra32>(400, 400);
using (IPixelAccessor imagePixels = image.Lock())
using (IPixelAccessor<Bgra32> imagePixels = image.Lock())
{
imagePixels[200, 200] = new Bgra32(1, 1, 1, 1);
return (Bgra32)imagePixels[200, 200];

Loading…
Cancel
Save