diff --git a/src/ImageSharp/Advanced/IConfigurable.cs b/src/ImageSharp/Advanced/IConfigurable.cs index 347bc9253..fd97ae921 100644 --- a/src/ImageSharp/Advanced/IConfigurable.cs +++ b/src/ImageSharp/Advanced/IConfigurable.cs @@ -1,12 +1,6 @@ // Copyright (c) Six Labors and contributors. // 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; - namespace SixLabors.ImageSharp.Advanced { /// @@ -15,7 +9,7 @@ namespace SixLabors.ImageSharp.Advanced internal interface IConfigurable { /// - /// Gets the pixel buffer. + /// Gets the configuration. /// Configuration Configuration { get; } } diff --git a/src/ImageSharp/Advanced/IConfigurableExtensions.cs b/src/ImageSharp/Advanced/IConfigurableExtensions.cs new file mode 100644 index 000000000..e4257ccc2 --- /dev/null +++ b/src/ImageSharp/Advanced/IConfigurableExtensions.cs @@ -0,0 +1,19 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +namespace SixLabors.ImageSharp.Advanced +{ + /// + /// Extension methods over + /// + internal static partial class IConfigurableExtensions + { + /// + /// Gets the configuration. + /// + /// The configurable + /// Returns the configuration. + public static Configuration GetConfiguration(this IConfigurable self) + => self?.Configuration ?? Configuration.Default; + } +} \ No newline at end of file diff --git a/src/ImageSharp/Advanced/ImageExtensions.cs b/src/ImageSharp/Advanced/ImageExtensions.cs index 135acefa4..ea7bb5349 100644 --- a/src/ImageSharp/Advanced/ImageExtensions.cs +++ b/src/ImageSharp/Advanced/ImageExtensions.cs @@ -73,23 +73,5 @@ namespace SixLabors.ImageSharp.Advanced private static Span GetSpan(Buffer2D source, int row) where TPixel : struct, IPixel => source.Span.Slice(row * source.Width, source.Width); - - /// - /// Gets the bounds of the image. - /// - /// The Pixel format. - /// The source image - /// Returns the bounds of the image - public static Configuration Configuration(this Image source) - where TPixel : struct, IPixel - => GetConfiguration(source); - - /// - /// Gets the bounds of the image. - /// - /// The source image - /// Returns the bounds of the image - private static Configuration GetConfiguration(IConfigurable source) - => source?.Configuration ?? SixLabors.ImageSharp.Configuration.Default; } } diff --git a/src/ImageSharp/ApplyProcessors.cs b/src/ImageSharp/ApplyProcessors.cs index 3184bc592..58a952c40 100644 --- a/src/ImageSharp/ApplyProcessors.cs +++ b/src/ImageSharp/ApplyProcessors.cs @@ -25,7 +25,7 @@ namespace SixLabors.ImageSharp Guard.NotNull(operation, nameof(operation)); Guard.NotNull(source, nameof(source)); - IInternalImageProcessingContext operationsRunner = source.Configuration().ImageOperationsProvider.CreateImageProcessingContext(source, true); + IInternalImageProcessingContext operationsRunner = source.GetConfiguration().ImageOperationsProvider.CreateImageProcessingContext(source, true); operation(operationsRunner); operationsRunner.Apply(); } @@ -42,7 +42,7 @@ namespace SixLabors.ImageSharp Guard.NotNull(operations, nameof(operations)); Guard.NotNull(source, nameof(source)); - IInternalImageProcessingContext operationsRunner = source.Configuration().ImageOperationsProvider.CreateImageProcessingContext(source, true); + IInternalImageProcessingContext operationsRunner = source.GetConfiguration().ImageOperationsProvider.CreateImageProcessingContext(source, true); operationsRunner.ApplyProcessors(operations); operationsRunner.Apply(); } @@ -60,7 +60,7 @@ namespace SixLabors.ImageSharp Guard.NotNull(operation, nameof(operation)); Guard.NotNull(source, nameof(source)); - IInternalImageProcessingContext operationsRunner = source.Configuration().ImageOperationsProvider.CreateImageProcessingContext(source, false); + IInternalImageProcessingContext operationsRunner = source.GetConfiguration().ImageOperationsProvider.CreateImageProcessingContext(source, false); operation(operationsRunner); return operationsRunner.Apply(); } @@ -78,7 +78,7 @@ namespace SixLabors.ImageSharp Guard.NotNull(operations, nameof(operations)); Guard.NotNull(source, nameof(source)); - IInternalImageProcessingContext operationsRunner = source.Configuration().ImageOperationsProvider.CreateImageProcessingContext(source, false); + IInternalImageProcessingContext operationsRunner = source.GetConfiguration().ImageOperationsProvider.CreateImageProcessingContext(source, false); operationsRunner.ApplyProcessors(operations); return operationsRunner.Apply(); } diff --git a/src/ImageSharp/Formats/Bmp/ImageExtensions.cs b/src/ImageSharp/Formats/Bmp/ImageExtensions.cs index 3bcbed49a..935ce8f4a 100644 --- a/src/ImageSharp/Formats/Bmp/ImageExtensions.cs +++ b/src/ImageSharp/Formats/Bmp/ImageExtensions.cs @@ -36,6 +36,6 @@ namespace SixLabors.ImageSharp /// Thrown if the stream is null. public static void SaveAsBmp(this Image source, Stream stream, BmpEncoder encoder) where TPixel : struct, IPixel - => source.Save(stream, encoder ?? source.Configuration().FindEncoder(ImageFormats.Bmp)); + => source.Save(stream, encoder ?? source.GetConfiguration().FindEncoder(ImageFormats.Bmp)); } } diff --git a/src/ImageSharp/Formats/Gif/ImageExtensions.cs b/src/ImageSharp/Formats/Gif/ImageExtensions.cs index 8174ec6f4..939eb456e 100644 --- a/src/ImageSharp/Formats/Gif/ImageExtensions.cs +++ b/src/ImageSharp/Formats/Gif/ImageExtensions.cs @@ -36,6 +36,6 @@ namespace SixLabors.ImageSharp /// Thrown if the stream is null. public static void SaveAsGif(this Image source, Stream stream, GifEncoder encoder) where TPixel : struct, IPixel - => source.Save(stream, encoder ?? source.Configuration().FindEncoder(ImageFormats.Gif)); + => source.Save(stream, encoder ?? source.GetConfiguration().FindEncoder(ImageFormats.Gif)); } } diff --git a/src/ImageSharp/Formats/Jpeg/ImageExtensions.cs b/src/ImageSharp/Formats/Jpeg/ImageExtensions.cs index bc87b3a1b..9cd7b3a8b 100644 --- a/src/ImageSharp/Formats/Jpeg/ImageExtensions.cs +++ b/src/ImageSharp/Formats/Jpeg/ImageExtensions.cs @@ -36,6 +36,6 @@ namespace SixLabors.ImageSharp /// Thrown if the stream is null. public static void SaveAsJpeg(this Image source, Stream stream, JpegEncoder encoder) where TPixel : struct, IPixel - => source.Save(stream, encoder ?? source.Configuration().FindEncoder(ImageFormats.Jpeg)); + => source.Save(stream, encoder ?? source.GetConfiguration().FindEncoder(ImageFormats.Jpeg)); } } diff --git a/src/ImageSharp/Formats/Png/ImageExtensions.cs b/src/ImageSharp/Formats/Png/ImageExtensions.cs index e237d6a4e..10970fc16 100644 --- a/src/ImageSharp/Formats/Png/ImageExtensions.cs +++ b/src/ImageSharp/Formats/Png/ImageExtensions.cs @@ -35,6 +35,6 @@ namespace SixLabors.ImageSharp /// Thrown if the stream is null. public static void SaveAsPng(this Image source, Stream stream, PngEncoder encoder) where TPixel : struct, IPixel - => source.Save(stream, encoder ?? source.Configuration().FindEncoder(ImageFormats.Png)); + => source.Save(stream, encoder ?? source.GetConfiguration().FindEncoder(ImageFormats.Png)); } } diff --git a/src/ImageSharp/Image/ImageExtensions.cs b/src/ImageSharp/Image/ImageExtensions.cs index d1798abf3..377dc2580 100644 --- a/src/ImageSharp/Image/ImageExtensions.cs +++ b/src/ImageSharp/Image/ImageExtensions.cs @@ -32,12 +32,12 @@ namespace SixLabors.ImageSharp Guard.NotNullOrEmpty(filePath, nameof(filePath)); string ext = Path.GetExtension(filePath).Trim('.'); - IImageFormat format = source.Configuration().FindFormatByFileExtension(ext); + IImageFormat format = source.GetConfiguration().FindFormatByFileExtension(ext); if (format == null) { var stringBuilder = new StringBuilder(); stringBuilder.AppendLine($"Can't find a format that is associated with the file extention '{ext}'. Registered formats with there extensions include:"); - foreach (IImageFormat fmt in source.Configuration().ImageFormats) + foreach (IImageFormat fmt in source.GetConfiguration().ImageFormats) { stringBuilder.AppendLine($" - {fmt.Name} : {string.Join(", ", fmt.FileExtensions)}"); } @@ -45,13 +45,13 @@ namespace SixLabors.ImageSharp throw new NotSupportedException(stringBuilder.ToString()); } - IImageEncoder encoder = source.Configuration().FindEncoder(format); + IImageEncoder encoder = source.GetConfiguration().FindEncoder(format); if (encoder == null) { var stringBuilder = new StringBuilder(); stringBuilder.AppendLine($"Can't find encoder for file extention '{ext}' using image format '{format.Name}'. Registered encoders include:"); - foreach (KeyValuePair enc in source.Configuration().ImageEncoders) + foreach (KeyValuePair enc in source.GetConfiguration().ImageEncoders) { stringBuilder.AppendLine($" - {enc.Key} : {enc.Value.GetType().Name}"); } @@ -74,7 +74,7 @@ namespace SixLabors.ImageSharp where TPixel : struct, IPixel { Guard.NotNull(encoder, nameof(encoder)); - using (Stream fs = source.Configuration().FileSystem.Create(filePath)) + using (Stream fs = source.GetConfiguration().FileSystem.Create(filePath)) { source.Save(fs, encoder); } @@ -93,14 +93,14 @@ namespace SixLabors.ImageSharp where TPixel : struct, IPixel { Guard.NotNull(format, nameof(format)); - IImageEncoder encoder = source.Configuration().FindEncoder(format); + IImageEncoder encoder = source.GetConfiguration().FindEncoder(format); if (encoder == null) { var stringBuilder = new StringBuilder(); stringBuilder.AppendLine("Can't find encoder for provided mime type. Available encoded:"); - foreach (KeyValuePair val in source.Configuration().ImageEncoders) + foreach (KeyValuePair val in source.GetConfiguration().ImageEncoders) { stringBuilder.AppendLine($" - {val.Key.Name} : {val.Value.GetType().Name}"); } diff --git a/src/ImageSharp/Processing/Processors/CloningImageProcessor.cs b/src/ImageSharp/Processing/Processors/CloningImageProcessor.cs index 176266ca6..fdd7ae17c 100644 --- a/src/ImageSharp/Processing/Processors/CloningImageProcessor.cs +++ b/src/ImageSharp/Processing/Processors/CloningImageProcessor.cs @@ -27,7 +27,7 @@ namespace SixLabors.ImageSharp.Processing throw new ImageProcessingException($"An error occured when processing the image using {this.GetType().Name}. The processor changed the number of frames."); } - var configuration = source.Configuration(); + var configuration = source.GetConfiguration(); this.BeforeImageApply(source, clone, sourceRectangle); for (int i = 0; i < source.Frames.Count; i++) diff --git a/src/ImageSharp/Processing/Processors/ImageProcessor.cs b/src/ImageSharp/Processing/Processors/ImageProcessor.cs index 4799b0995..993bbe54b 100644 --- a/src/ImageSharp/Processing/Processors/ImageProcessor.cs +++ b/src/ImageSharp/Processing/Processors/ImageProcessor.cs @@ -20,7 +20,7 @@ namespace SixLabors.ImageSharp.Processing { try { - var config = source.Configuration(); + var config = source.GetConfiguration(); this.BeforeImageApply(source, sourceRectangle); foreach (ImageFrame sourceFrame in source.Frames) diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs index 5dfa343ca..05c70b75e 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs @@ -53,7 +53,7 @@ namespace SixLabors.ImageSharp.Processing.Processors /// protected override Image CreateDestination(Image source, Rectangle sourceRectangle) { - Configuration config = source.Configuration(); + Configuration config = source.GetConfiguration(); // We will always be creating the clone even for mutate because thats the way this base processor works // ------------ diff --git a/src/ImageSharp/Quantizers/Quantize.cs b/src/ImageSharp/Quantizers/Quantize.cs index d9c3c84cd..049090f43 100644 --- a/src/ImageSharp/Quantizers/Quantize.cs +++ b/src/ImageSharp/Quantizers/Quantize.cs @@ -66,7 +66,7 @@ namespace SixLabors.ImageSharp Parallel.For( 0, pixels.Height, - img.Configuration().ParallelOptions, + img.GetConfiguration().ParallelOptions, y => { for (int x = 0; x < pixels.Width; x++) diff --git a/tests/ImageSharp.Tests/TestFile.cs b/tests/ImageSharp.Tests/TestFile.cs index e77b95f9e..b736dce20 100644 --- a/tests/ImageSharp.Tests/TestFile.cs +++ b/tests/ImageSharp.Tests/TestFile.cs @@ -146,7 +146,7 @@ namespace SixLabors.ImageSharp.Tests /// public Image CreateImage(IImageDecoder decoder) { - return ImageSharp.Image.Load(this.Image.Configuration(), this.Bytes, decoder); + return ImageSharp.Image.Load(this.Image.GetConfiguration(), this.Bytes, decoder); } } }