Browse Source

Added new extension method for IConfigurable to get the configuration and remove the one from Image<TPixel>.

af/merge-core
Dirk Lemstra 9 years ago
parent
commit
da4ad7c129
  1. 8
      src/ImageSharp/Advanced/IConfigurable.cs
  2. 19
      src/ImageSharp/Advanced/IConfigurableExtensions.cs
  3. 18
      src/ImageSharp/Advanced/ImageExtensions.cs
  4. 8
      src/ImageSharp/ApplyProcessors.cs
  5. 2
      src/ImageSharp/Formats/Bmp/ImageExtensions.cs
  6. 2
      src/ImageSharp/Formats/Gif/ImageExtensions.cs
  7. 2
      src/ImageSharp/Formats/Jpeg/ImageExtensions.cs
  8. 2
      src/ImageSharp/Formats/Png/ImageExtensions.cs
  9. 14
      src/ImageSharp/Image/ImageExtensions.cs
  10. 2
      src/ImageSharp/Processing/Processors/CloningImageProcessor.cs
  11. 2
      src/ImageSharp/Processing/Processors/ImageProcessor.cs
  12. 2
      src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs
  13. 2
      src/ImageSharp/Quantizers/Quantize.cs
  14. 2
      tests/ImageSharp.Tests/TestFile.cs

8
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
{
/// <summary>
@ -15,7 +9,7 @@ namespace SixLabors.ImageSharp.Advanced
internal interface IConfigurable
{
/// <summary>
/// Gets the pixel buffer.
/// Gets the configuration.
/// </summary>
Configuration Configuration { get; }
}

19
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
{
/// <summary>
/// Extension methods over <see cref="IConfigurable"/>
/// </summary>
internal static partial class IConfigurableExtensions
{
/// <summary>
/// Gets the configuration.
/// </summary>
/// <param name="self">The configurable</param>
/// <returns>Returns the configuration.</returns>
public static Configuration GetConfiguration(this IConfigurable self)
=> self?.Configuration ?? Configuration.Default;
}
}

18
src/ImageSharp/Advanced/ImageExtensions.cs

@ -73,23 +73,5 @@ namespace SixLabors.ImageSharp.Advanced
private static Span<TPixel> GetSpan<TPixel>(Buffer2D<TPixel> source, int row)
where TPixel : struct, IPixel<TPixel>
=> source.Span.Slice(row * source.Width, source.Width);
/// <summary>
/// Gets the bounds of the image.
/// </summary>
/// <typeparam name="TPixel">The Pixel format.</typeparam>
/// <param name="source">The source image</param>
/// <returns>Returns the bounds of the image</returns>
public static Configuration Configuration<TPixel>(this Image<TPixel> source)
where TPixel : struct, IPixel<TPixel>
=> GetConfiguration(source);
/// <summary>
/// Gets the bounds of the image.
/// </summary>
/// <param name="source">The source image</param>
/// <returns>Returns the bounds of the image</returns>
private static Configuration GetConfiguration(IConfigurable source)
=> source?.Configuration ?? SixLabors.ImageSharp.Configuration.Default;
}
}

8
src/ImageSharp/ApplyProcessors.cs

@ -25,7 +25,7 @@ namespace SixLabors.ImageSharp
Guard.NotNull(operation, nameof(operation));
Guard.NotNull(source, nameof(source));
IInternalImageProcessingContext<TPixel> operationsRunner = source.Configuration().ImageOperationsProvider.CreateImageProcessingContext(source, true);
IInternalImageProcessingContext<TPixel> 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<TPixel> operationsRunner = source.Configuration().ImageOperationsProvider.CreateImageProcessingContext(source, true);
IInternalImageProcessingContext<TPixel> 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<TPixel> operationsRunner = source.Configuration().ImageOperationsProvider.CreateImageProcessingContext(source, false);
IInternalImageProcessingContext<TPixel> 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<TPixel> operationsRunner = source.Configuration().ImageOperationsProvider.CreateImageProcessingContext(source, false);
IInternalImageProcessingContext<TPixel> operationsRunner = source.GetConfiguration().ImageOperationsProvider.CreateImageProcessingContext(source, false);
operationsRunner.ApplyProcessors(operations);
return operationsRunner.Apply();
}

2
src/ImageSharp/Formats/Bmp/ImageExtensions.cs

@ -36,6 +36,6 @@ namespace SixLabors.ImageSharp
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SaveAsBmp<TPixel>(this Image<TPixel> source, Stream stream, BmpEncoder encoder)
where TPixel : struct, IPixel<TPixel>
=> source.Save(stream, encoder ?? source.Configuration().FindEncoder(ImageFormats.Bmp));
=> source.Save(stream, encoder ?? source.GetConfiguration().FindEncoder(ImageFormats.Bmp));
}
}

2
src/ImageSharp/Formats/Gif/ImageExtensions.cs

@ -36,6 +36,6 @@ namespace SixLabors.ImageSharp
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SaveAsGif<TPixel>(this Image<TPixel> source, Stream stream, GifEncoder encoder)
where TPixel : struct, IPixel<TPixel>
=> source.Save(stream, encoder ?? source.Configuration().FindEncoder(ImageFormats.Gif));
=> source.Save(stream, encoder ?? source.GetConfiguration().FindEncoder(ImageFormats.Gif));
}
}

2
src/ImageSharp/Formats/Jpeg/ImageExtensions.cs

@ -36,6 +36,6 @@ namespace SixLabors.ImageSharp
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SaveAsJpeg<TPixel>(this Image<TPixel> source, Stream stream, JpegEncoder encoder)
where TPixel : struct, IPixel<TPixel>
=> source.Save(stream, encoder ?? source.Configuration().FindEncoder(ImageFormats.Jpeg));
=> source.Save(stream, encoder ?? source.GetConfiguration().FindEncoder(ImageFormats.Jpeg));
}
}

2
src/ImageSharp/Formats/Png/ImageExtensions.cs

@ -35,6 +35,6 @@ namespace SixLabors.ImageSharp
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SaveAsPng<TPixel>(this Image<TPixel> source, Stream stream, PngEncoder encoder)
where TPixel : struct, IPixel<TPixel>
=> source.Save(stream, encoder ?? source.Configuration().FindEncoder(ImageFormats.Png));
=> source.Save(stream, encoder ?? source.GetConfiguration().FindEncoder(ImageFormats.Png));
}
}

14
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<IImageFormat, IImageEncoder> enc in source.Configuration().ImageEncoders)
foreach (KeyValuePair<IImageFormat, IImageEncoder> enc in source.GetConfiguration().ImageEncoders)
{
stringBuilder.AppendLine($" - {enc.Key} : {enc.Value.GetType().Name}");
}
@ -74,7 +74,7 @@ namespace SixLabors.ImageSharp
where TPixel : struct, IPixel<TPixel>
{
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<TPixel>
{
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<IImageFormat, IImageEncoder> val in source.Configuration().ImageEncoders)
foreach (KeyValuePair<IImageFormat, IImageEncoder> val in source.GetConfiguration().ImageEncoders)
{
stringBuilder.AppendLine($" - {val.Key.Name} : {val.Value.GetType().Name}");
}

2
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++)

2
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<TPixel> sourceFrame in source.Frames)

2
src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs

@ -53,7 +53,7 @@ namespace SixLabors.ImageSharp.Processing.Processors
/// <inheritdoc/>
protected override Image<TPixel> CreateDestination(Image<TPixel> 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
// ------------

2
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++)

2
tests/ImageSharp.Tests/TestFile.cs

@ -146,7 +146,7 @@ namespace SixLabors.ImageSharp.Tests
/// </returns>
public Image<Rgba32> CreateImage(IImageDecoder decoder)
{
return ImageSharp.Image.Load(this.Image.Configuration(), this.Bytes, decoder);
return ImageSharp.Image.Load(this.Image.GetConfiguration(), this.Bytes, decoder);
}
}
}

Loading…
Cancel
Save