diff --git a/src/ImageSharp/Advanced/AdvancedImageExtensions.cs b/src/ImageSharp/Advanced/AdvancedImageExtensions.cs index 9bf8943b7..d36240512 100644 --- a/src/ImageSharp/Advanced/AdvancedImageExtensions.cs +++ b/src/ImageSharp/Advanced/AdvancedImageExtensions.cs @@ -31,6 +31,22 @@ namespace SixLabors.ImageSharp.Advanced public static Configuration GetConfiguration(this Image source) => GetConfiguration((IConfigurable)source); + /// + /// Gets the configuration for the image frame. + /// + /// The source image. + /// Returns the configuration. + public static Configuration GetConfiguration(this ImageFrame source) + => GetConfiguration((IConfigurable)source); + + /// + /// Gets the configuration . + /// + /// The source image + /// Returns the bounds of the image + private static Configuration GetConfiguration(IConfigurable source) + => source?.Configuration ?? Configuration.Default; + /// /// Gets the representation of the pixels as a of contiguous memory in the source image's pixel format /// stored in row major order. @@ -161,14 +177,6 @@ namespace SixLabors.ImageSharp.Advanced internal static MemoryAllocator GetMemoryAllocator(this IConfigurable source) => GetConfiguration(source).MemoryAllocator; - /// - /// Gets the configuration. - /// - /// The source image - /// Returns the bounds of the image - private static Configuration GetConfiguration(IConfigurable source) - => source?.Configuration ?? Configuration.Default; - /// /// Returns a reference to the 0th element of the Pixel buffer. /// Such a reference can be used for pinning but must never be dereferenced. diff --git a/src/ImageSharp/Advanced/IConfigurable.cs b/src/ImageSharp/Advanced/IConfigurable.cs index 38fc83ae1..d36cde0ed 100644 --- a/src/ImageSharp/Advanced/IConfigurable.cs +++ b/src/ImageSharp/Advanced/IConfigurable.cs @@ -1,4 +1,4 @@ -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. namespace SixLabors.ImageSharp.Advanced @@ -9,8 +9,8 @@ namespace SixLabors.ImageSharp.Advanced internal interface IConfigurable { /// - /// Gets the configuration. + /// Gets the configuration which allows altering default behaviour or extending the library. /// Configuration Configuration { get; } } -} \ No newline at end of file +} diff --git a/src/ImageSharp/Formats/Jpeg/Components/Encoder/YCbCrForwardConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Encoder/YCbCrForwardConverter{TPixel}.cs index 883a085b5..92482de2a 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Encoder/YCbCrForwardConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Encoder/YCbCrForwardConverter{TPixel}.cs @@ -1,9 +1,9 @@ -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using System; using System.Runtime.CompilerServices; - +using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder @@ -60,7 +60,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder this.pixelBlock.LoadAndStretchEdges(frame, x, y); Span rgbSpan = this.rgbBlock.AsSpanUnsafe(); - PixelOperations.Instance.ToRgb24(frame.Configuration, this.pixelBlock.AsSpanUnsafe(), rgbSpan); + PixelOperations.Instance.ToRgb24(frame.GetConfiguration(), this.pixelBlock.AsSpanUnsafe(), rgbSpan); ref float yBlockStart = ref Unsafe.As(ref this.Y); ref float cbBlockStart = ref Unsafe.As(ref this.Cb); @@ -81,4 +81,4 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder } } } -} \ No newline at end of file +} diff --git a/src/ImageSharp/Image.cs b/src/ImageSharp/Image.cs index a62bfed1e..c0de4e04c 100644 --- a/src/ImageSharp/Image.cs +++ b/src/ImageSharp/Image.cs @@ -19,17 +19,18 @@ namespace SixLabors.ImageSharp public abstract partial class Image : IImage, IConfigurable { private Size size; + private readonly Configuration configuration; /// /// Initializes a new instance of the class. /// - /// The . + /// The . /// The . /// The . /// The . protected Image(Configuration configuration, PixelTypeInfo pixelType, ImageMetadata metadata, Size size) { - this.Configuration = configuration ?? Configuration.Default; + this.configuration = configuration ?? Configuration.Default; this.PixelType = pixelType; this.size = size; this.Metadata = metadata ?? new ImageMetadata(); @@ -48,11 +49,6 @@ namespace SixLabors.ImageSharp { } - /// - /// Gets the . - /// - protected Configuration Configuration { get; } - /// /// Gets the implementing the public property. /// @@ -75,10 +71,8 @@ namespace SixLabors.ImageSharp /// public ImageFrameCollection Frames => this.NonGenericFrameCollection; - /// - /// Gets the pixel buffer. - /// - Configuration IConfigurable.Configuration => this.Configuration; + /// + Configuration IConfigurable.Configuration => this.configuration; /// public void Dispose() @@ -108,7 +102,7 @@ namespace SixLabors.ImageSharp /// The pixel format. /// The public Image CloneAs() - where TPixel2 : struct, IPixel => this.CloneAs(this.Configuration); + where TPixel2 : struct, IPixel => this.CloneAs(this.GetConfiguration()); /// /// Returns a copy of the image in the given pixel format. diff --git a/src/ImageSharp/ImageFrame.cs b/src/ImageSharp/ImageFrame.cs index fe2a2b762..af7405c75 100644 --- a/src/ImageSharp/ImageFrame.cs +++ b/src/ImageSharp/ImageFrame.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; +using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.PixelFormats; @@ -13,8 +14,10 @@ namespace SixLabors.ImageSharp /// In case of animated formats like gif, it contains the single frame in a animation. /// In all other cases it is the only frame of the image. /// - public abstract partial class ImageFrame : IDisposable + public abstract partial class ImageFrame : IConfigurable, IDisposable { + private readonly Configuration configuration; + /// /// Initializes a new instance of the class. /// @@ -27,7 +30,7 @@ namespace SixLabors.ImageSharp Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(metadata, nameof(metadata)); - this.Configuration = configuration; + this.configuration = configuration ?? Configuration.Default; this.MemoryAllocator = configuration.MemoryAllocator; this.Width = width; this.Height = height; @@ -39,11 +42,6 @@ namespace SixLabors.ImageSharp /// public MemoryAllocator MemoryAllocator { get; } - /// - /// Gets the instance associated with this . - /// - internal Configuration Configuration { get; } - /// /// Gets the width. /// @@ -59,6 +57,9 @@ namespace SixLabors.ImageSharp /// public ImageFrameMetadata Metadata { get; } + /// + Configuration IConfigurable.Configuration => this.configuration; + /// /// Gets the size of the frame. /// diff --git a/src/ImageSharp/ImageFrame{TPixel}.cs b/src/ImageSharp/ImageFrame{TPixel}.cs index 85454e150..fef1730b9 100644 --- a/src/ImageSharp/ImageFrame{TPixel}.cs +++ b/src/ImageSharp/ImageFrame{TPixel}.cs @@ -219,7 +219,7 @@ namespace SixLabors.ImageSharp this.PixelBuffer.GetSpan().CopyTo(dest1); } - PixelOperations.Instance.To(this.Configuration, this.PixelBuffer.GetSpan(), destination); + PixelOperations.Instance.To(this.GetConfiguration(), this.PixelBuffer.GetSpan(), destination); } /// @@ -229,7 +229,7 @@ namespace SixLabors.ImageSharp /// Clones the current instance. /// /// The - internal ImageFrame Clone() => this.Clone(this.Configuration); + internal ImageFrame Clone() => this.Clone(this.GetConfiguration()); /// /// Clones the current instance. @@ -244,7 +244,7 @@ namespace SixLabors.ImageSharp /// The pixel format. /// The internal ImageFrame CloneAs() - where TPixel2 : struct, IPixel => this.CloneAs(this.Configuration); + where TPixel2 : struct, IPixel => this.CloneAs(this.GetConfiguration()); /// /// Returns a copy of the image frame in the given pixel format. diff --git a/src/ImageSharp/Image{TPixel}.cs b/src/ImageSharp/Image{TPixel}.cs index b7e63dc25..87bdf90a1 100644 --- a/src/ImageSharp/Image{TPixel}.cs +++ b/src/ImageSharp/Image{TPixel}.cs @@ -154,7 +154,7 @@ namespace SixLabors.ImageSharp /// Clones the current image /// /// Returns a new image with all the same metadata as the original. - public Image Clone() => this.Clone(this.Configuration); + public Image Clone() => this.Clone(this.GetConfiguration()); /// /// Clones the current image with the given configuration. diff --git a/src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor{TPixel}.cs index 1c974612e..53bd0f231 100644 --- a/src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor{TPixel}.cs @@ -64,6 +64,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays int width = maxX - minX; var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY); + Configuration configuration = this.Configuration; using (IMemoryOwner colors = source.MemoryAllocator.Allocate(width)) using (IMemoryOwner amount = source.MemoryAllocator.Allocate(width)) @@ -79,7 +80,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays ParallelHelper.IterateRows( workingRect, - this.Configuration, + configuration, rows => { for (int y = rows.Min; y < rows.Max; y++) @@ -89,7 +90,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays // This switched color & destination in the 2nd and 3rd places because we are applying the target color under the current one blender.Blend( - source.Configuration, + configuration, destination, colors.GetSpan(), destination, diff --git a/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor{TPixel}.cs index d6aa6f894..86071cdfd 100644 --- a/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor{TPixel}.cs @@ -76,6 +76,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY); float blendPercentage = this.definition.GraphicsOptions.BlendPercentage; + Configuration configuration = this.Configuration; using (IMemoryOwner rowColors = source.MemoryAllocator.Allocate(width)) { @@ -83,7 +84,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays ParallelHelper.IterateRowsWithTempBuffer( workingRect, - this.Configuration, + configuration, (rows, amounts) => { Span amountsSpan = amounts.Span; @@ -102,7 +103,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays Span destination = source.GetPixelRowSpan(offsetY).Slice(offsetX, width); this.blender.Blend( - source.Configuration, + configuration, destination, destination, rowColors.GetSpan(), diff --git a/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor{TPixel}.cs index fd782261b..402fb1776 100644 --- a/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor{TPixel}.cs @@ -80,6 +80,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY); float blendPercentage = this.definition.GraphicsOptions.BlendPercentage; + Configuration configuration = this.Configuration; using (IMemoryOwner rowColors = source.MemoryAllocator.Allocate(width)) { @@ -87,7 +88,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays ParallelHelper.IterateRowsWithTempBuffer( workingRect, - this.Configuration, + configuration, (rows, amounts) => { Span amountsSpan = amounts.Span; @@ -105,7 +106,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays Span destination = source.GetPixelRowSpan(offsetY).Slice(offsetX, width); this.blender.Blend( - source.Configuration, + configuration, destination, destination, rowColors.GetSpan(), diff --git a/src/ImageSharp/Processing/Processors/Quantization/FrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/FrameQuantizer{TPixel}.cs index 71013548b..84e783240 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/FrameQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/FrameQuantizer{TPixel}.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors.Dithering; @@ -108,9 +109,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization // Collect the palette. Required before the second pass runs. ReadOnlyMemory palette = this.GetPalette(); - this.paletteVector = image.Configuration.MemoryAllocator.Allocate(palette.Length); + Configuration configuration = image.GetConfiguration(); + this.paletteVector = configuration.MemoryAllocator.Allocate(palette.Length); PixelOperations.Instance.ToVector4( - image.Configuration, + configuration, palette.Span, this.paletteVector.Memory.Span, PixelConversionModifiers.Scale); diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs index 9b5e89427..e695dbd17 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs @@ -471,7 +471,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization { Span row = source.GetPixelRowSpan(y); Span rgbaSpan = rgbaBuffer.GetSpan(); - PixelOperations.Instance.ToRgba32(source.Configuration, row, rgbaSpan); + PixelOperations.Instance.ToRgba32(source.GetConfiguration(), row, rgbaSpan); ref Rgba32 scanBaseRef = ref MemoryMarshal.GetReference(rgbaSpan); // And loop through each column