diff --git a/src/ImageSharp/ImageFrameCollection.cs b/src/ImageSharp/ImageFrameCollection.cs index a9225eec4..1c00d9e63 100644 --- a/src/ImageSharp/ImageFrameCollection.cs +++ b/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(parent.GetConfiguration().MemoryManager, width, height)); + if (parent.ClearColor.HasValue) + { + this.frames.Add(new ImageFrame(parent.GetConfiguration(), width, height, parent.ClearColor.Value)); + } + else + { + this.frames.Add(new ImageFrame(parent.GetConfiguration().MemoryManager, width, height)); + } } internal ImageFrameCollection(Image parent, IEnumerable> frames) @@ -143,7 +150,16 @@ namespace SixLabors.ImageSharp /// public ImageFrame CreateFrame() { - var frame = new ImageFrame(this.parent.GetConfiguration().MemoryManager, this.RootFrame.Width, this.RootFrame.Height); + ImageFrame frame; + if (this.parent.ClearColor.HasValue) + { + frame = new ImageFrame(this.parent.GetConfiguration(), this.RootFrame.Width, this.RootFrame.Height, this.parent.ClearColor.Value); + } + else + { + frame = new ImageFrame(this.parent.GetConfiguration().MemoryManager, this.RootFrame.Width, this.RootFrame.Height); + } + this.frames.Add(frame); return frame; } diff --git a/src/ImageSharp/Image{TPixel}.cs b/src/ImageSharp/Image{TPixel}.cs index 78a091e41..2aa503844 100644 --- a/src/ImageSharp/Image{TPixel}.cs +++ b/src/ImageSharp/Image{TPixel}.cs @@ -22,6 +22,7 @@ namespace SixLabors.ImageSharp { private readonly Configuration configuration; private readonly ImageFrameCollection frames; + private readonly TPixel? clearColor; /// /// Initializes a new instance of the class @@ -37,6 +38,21 @@ namespace SixLabors.ImageSharp { } + /// + /// Initializes a new instance of the class + /// with the height and the width of the image. + /// + /// + /// The configuration providing initialization code which allows extending the library. + /// + /// The width of the image in pixels. + /// The height of the image in pixels. + /// The color to initialize the pixels with. + public Image(Configuration configuration, int width, int height, TPixel clearColor) + : this(configuration, width, height, clearColor, new ImageMetaData()) + { + } + /// /// Initializes a new instance of the class /// with the height and the width of the image. @@ -66,6 +82,25 @@ namespace SixLabors.ImageSharp this.frames = new ImageFrameCollection(this, width, height); } + /// + /// Initializes a new instance of the class + /// with the height and the width of the image. + /// + /// + /// The configuration providing initialization code which allows extending the library. + /// + /// The width of the image in pixels. + /// The height of the image in pixels. + /// The clear color. + /// The images metadata. + internal Image(Configuration configuration, int width, int height, TPixel clearColor, ImageMetaData metadata) { + this.configuration = configuration ?? Configuration.Default; + this.PixelType = new PixelTypeInfo(Unsafe.SizeOf() * 8); + this.MetaData = metadata ?? new ImageMetaData(); + this.clearColor = clearColor; + this.frames = new ImageFrameCollection(this, width, height); + } + /// /// Initializes a new instance of the class /// with the height and the width of the image. @@ -104,6 +139,11 @@ namespace SixLabors.ImageSharp /// public IImageFrameCollection Frames => this.frames; + /// + /// Gets the clear color to initialize the image frame pixels with. + /// + internal TPixel? ClearColor => this.clearColor; + /// /// Gets the root frame. /// diff --git a/src/ImageSharp/Memory/BufferExtensions.cs b/src/ImageSharp/Memory/BufferExtensions.cs index dd3114c21..1347f2882 100644 --- a/src/ImageSharp/Memory/BufferExtensions.cs +++ b/src/ImageSharp/Memory/BufferExtensions.cs @@ -52,6 +52,18 @@ namespace SixLabors.ImageSharp.Memory buffer.Span.Clear(); } + /// + /// Fills the contents of this buffer. + /// + /// The buffer + /// The value to fill the buffer with. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Fill(this IBuffer buffer, T value) + where T : struct + { + buffer.Span.Fill(value); + } + public static ref T DangerousGetPinnableReference(this IBuffer buffer) where T : struct => ref MemoryMarshal.GetReference(buffer.Span);