Browse Source

Add Image and ImageFrame constructors that take a clear color as parameter.

This is good for performance because this avoids first needing to clear the buffer with zeroes.
pull/552/head
woutware 8 years ago
parent
commit
58384de0c7
  1. 20
      src/ImageSharp/ImageFrameCollection.cs
  2. 40
      src/ImageSharp/Image{TPixel}.cs
  3. 12
      src/ImageSharp/Memory/BufferExtensions.cs

20
src/ImageSharp/ImageFrameCollection.cs

@ -23,7 +23,14 @@ namespace SixLabors.ImageSharp
this.parent = parent;
// Frames are already cloned within the caller
this.frames.Add(new ImageFrame<TPixel>(parent.GetConfiguration().MemoryManager, width, height));
if (parent.ClearColor.HasValue)
{
this.frames.Add(new ImageFrame<TPixel>(parent.GetConfiguration(), width, height, parent.ClearColor.Value));
}
else
{
this.frames.Add(new ImageFrame<TPixel>(parent.GetConfiguration().MemoryManager, width, height));
}
}
internal ImageFrameCollection(Image<TPixel> parent, IEnumerable<ImageFrame<TPixel>> frames)
@ -143,7 +150,16 @@ namespace SixLabors.ImageSharp
/// <inheritdoc/>
public ImageFrame<TPixel> CreateFrame()
{
var frame = new ImageFrame<TPixel>(this.parent.GetConfiguration().MemoryManager, this.RootFrame.Width, this.RootFrame.Height);
ImageFrame<TPixel> frame;
if (this.parent.ClearColor.HasValue)
{
frame = new ImageFrame<TPixel>(this.parent.GetConfiguration(), this.RootFrame.Width, this.RootFrame.Height, this.parent.ClearColor.Value);
}
else
{
frame = new ImageFrame<TPixel>(this.parent.GetConfiguration().MemoryManager, this.RootFrame.Width, this.RootFrame.Height);
}
this.frames.Add(frame);
return frame;
}

40
src/ImageSharp/Image{TPixel}.cs

@ -22,6 +22,7 @@ namespace SixLabors.ImageSharp
{
private readonly Configuration configuration;
private readonly ImageFrameCollection<TPixel> frames;
private readonly TPixel? clearColor;
/// <summary>
/// Initializes a new instance of the <see cref="Image{TPixel}"/> class
@ -37,6 +38,21 @@ namespace SixLabors.ImageSharp
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Image{TPixel}"/> class
/// with the height and the width of the image.
/// </summary>
/// <param name="configuration">
/// The configuration providing initialization code which allows extending the library.
/// </param>
/// <param name="width">The width of the image in pixels.</param>
/// <param name="height">The height of the image in pixels.</param>
/// <param name="clearColor">The color to initialize the pixels with.</param>
public Image(Configuration configuration, int width, int height, TPixel clearColor)
: this(configuration, width, height, clearColor, new ImageMetaData())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Image{TPixel}"/> class
/// with the height and the width of the image.
@ -66,6 +82,25 @@ namespace SixLabors.ImageSharp
this.frames = new ImageFrameCollection<TPixel>(this, width, height);
}
/// <summary>
/// Initializes a new instance of the <see cref="Image{TPixel}"/> class
/// with the height and the width of the image.
/// </summary>
/// <param name="configuration">
/// The configuration providing initialization code which allows extending the library.
/// </param>
/// <param name="width">The width of the image in pixels.</param>
/// <param name="height">The height of the image in pixels.</param>
/// <param name="clearColor">The clear color.</param>
/// <param name="metadata">The images metadata.</param>
internal Image(Configuration configuration, int width, int height, TPixel clearColor, ImageMetaData metadata) {
this.configuration = configuration ?? Configuration.Default;
this.PixelType = new PixelTypeInfo(Unsafe.SizeOf<TPixel>() * 8);
this.MetaData = metadata ?? new ImageMetaData();
this.clearColor = clearColor;
this.frames = new ImageFrameCollection<TPixel>(this, width, height);
}
/// <summary>
/// Initializes a new instance of the <see cref="Image{TPixel}" /> class
/// with the height and the width of the image.
@ -104,6 +139,11 @@ namespace SixLabors.ImageSharp
/// </summary>
public IImageFrameCollection<TPixel> Frames => this.frames;
/// <summary>
/// Gets the clear color to initialize the image frame pixels with.
/// </summary>
internal TPixel? ClearColor => this.clearColor;
/// <summary>
/// Gets the root frame.
/// </summary>

12
src/ImageSharp/Memory/BufferExtensions.cs

@ -52,6 +52,18 @@ namespace SixLabors.ImageSharp.Memory
buffer.Span.Clear();
}
/// <summary>
/// Fills the contents of this buffer.
/// </summary>
/// <param name="buffer">The buffer</param>
/// <param name="value">The value to fill the buffer with.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Fill<T>(this IBuffer<T> buffer, T value)
where T : struct
{
buffer.Span.Fill(value);
}
public static ref T DangerousGetPinnableReference<T>(this IBuffer<T> buffer)
where T : struct =>
ref MemoryMarshal.GetReference(buffer.Span);

Loading…
Cancel
Save