Browse Source

Normalize configuration usage and expose method.

pull/1095/head
James Jackson-South 7 years ago
parent
commit
bfc97afd88
  1. 24
      src/ImageSharp/Advanced/AdvancedImageExtensions.cs
  2. 6
      src/ImageSharp/Advanced/IConfigurable.cs
  3. 8
      src/ImageSharp/Formats/Jpeg/Components/Encoder/YCbCrForwardConverter{TPixel}.cs
  4. 18
      src/ImageSharp/Image.cs
  5. 15
      src/ImageSharp/ImageFrame.cs
  6. 6
      src/ImageSharp/ImageFrame{TPixel}.cs
  7. 2
      src/ImageSharp/Image{TPixel}.cs
  8. 5
      src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor{TPixel}.cs
  9. 5
      src/ImageSharp/Processing/Processors/Overlays/GlowProcessor{TPixel}.cs
  10. 5
      src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor{TPixel}.cs
  11. 6
      src/ImageSharp/Processing/Processors/Quantization/FrameQuantizer{TPixel}.cs
  12. 2
      src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs

24
src/ImageSharp/Advanced/AdvancedImageExtensions.cs

@ -31,6 +31,22 @@ namespace SixLabors.ImageSharp.Advanced
public static Configuration GetConfiguration(this Image source) public static Configuration GetConfiguration(this Image source)
=> GetConfiguration((IConfigurable)source); => GetConfiguration((IConfigurable)source);
/// <summary>
/// Gets the configuration for the image frame.
/// </summary>
/// <param name="source">The source image.</param>
/// <returns>Returns the configuration.</returns>
public static Configuration GetConfiguration(this ImageFrame source)
=> GetConfiguration((IConfigurable)source);
/// <summary>
/// Gets the configuration .
/// </summary>
/// <param name="source">The source image</param>
/// <returns>Returns the bounds of the image</returns>
private static Configuration GetConfiguration(IConfigurable source)
=> source?.Configuration ?? Configuration.Default;
/// <summary> /// <summary>
/// Gets the representation of the pixels as a <see cref="Span{T}"/> of contiguous memory in the source image's pixel format /// Gets the representation of the pixels as a <see cref="Span{T}"/> of contiguous memory in the source image's pixel format
/// stored in row major order. /// stored in row major order.
@ -161,14 +177,6 @@ namespace SixLabors.ImageSharp.Advanced
internal static MemoryAllocator GetMemoryAllocator(this IConfigurable source) internal static MemoryAllocator GetMemoryAllocator(this IConfigurable source)
=> GetConfiguration(source).MemoryAllocator; => GetConfiguration(source).MemoryAllocator;
/// <summary>
/// Gets the configuration.
/// </summary>
/// <param name="source">The source image</param>
/// <returns>Returns the bounds of the image</returns>
private static Configuration GetConfiguration(IConfigurable source)
=> source?.Configuration ?? Configuration.Default;
/// <summary> /// <summary>
/// Returns a reference to the 0th element of the Pixel buffer. /// Returns a reference to the 0th element of the Pixel buffer.
/// Such a reference can be used for pinning but must never be dereferenced. /// Such a reference can be used for pinning but must never be dereferenced.

6
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. // Licensed under the Apache License, Version 2.0.
namespace SixLabors.ImageSharp.Advanced namespace SixLabors.ImageSharp.Advanced
@ -9,8 +9,8 @@ namespace SixLabors.ImageSharp.Advanced
internal interface IConfigurable internal interface IConfigurable
{ {
/// <summary> /// <summary>
/// Gets the configuration. /// Gets the configuration which allows altering default behaviour or extending the library.
/// </summary> /// </summary>
Configuration Configuration { get; } Configuration Configuration { get; }
} }
} }

8
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. // Licensed under the Apache License, Version 2.0.
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
@ -60,7 +60,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
this.pixelBlock.LoadAndStretchEdges(frame, x, y); this.pixelBlock.LoadAndStretchEdges(frame, x, y);
Span<Rgb24> rgbSpan = this.rgbBlock.AsSpanUnsafe(); Span<Rgb24> rgbSpan = this.rgbBlock.AsSpanUnsafe();
PixelOperations<TPixel>.Instance.ToRgb24(frame.Configuration, this.pixelBlock.AsSpanUnsafe(), rgbSpan); PixelOperations<TPixel>.Instance.ToRgb24(frame.GetConfiguration(), this.pixelBlock.AsSpanUnsafe(), rgbSpan);
ref float yBlockStart = ref Unsafe.As<Block8x8F, float>(ref this.Y); ref float yBlockStart = ref Unsafe.As<Block8x8F, float>(ref this.Y);
ref float cbBlockStart = ref Unsafe.As<Block8x8F, float>(ref this.Cb); ref float cbBlockStart = ref Unsafe.As<Block8x8F, float>(ref this.Cb);
@ -81,4 +81,4 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
} }
} }
} }
} }

18
src/ImageSharp/Image.cs

@ -19,17 +19,18 @@ namespace SixLabors.ImageSharp
public abstract partial class Image : IImage, IConfigurable public abstract partial class Image : IImage, IConfigurable
{ {
private Size size; private Size size;
private readonly Configuration configuration;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Image"/> class. /// Initializes a new instance of the <see cref="Image"/> class.
/// </summary> /// </summary>
/// <param name="configuration">The <see cref="Configuration"/>.</param> /// <param name="configuration">The <see cref="IConfigurable.Configuration"/>.</param>
/// <param name="pixelType">The <see cref="PixelTypeInfo"/>.</param> /// <param name="pixelType">The <see cref="PixelTypeInfo"/>.</param>
/// <param name="metadata">The <see cref="ImageMetadata"/>.</param> /// <param name="metadata">The <see cref="ImageMetadata"/>.</param>
/// <param name="size">The <see cref="size"/>.</param> /// <param name="size">The <see cref="size"/>.</param>
protected Image(Configuration configuration, PixelTypeInfo pixelType, ImageMetadata metadata, Size size) protected Image(Configuration configuration, PixelTypeInfo pixelType, ImageMetadata metadata, Size size)
{ {
this.Configuration = configuration ?? Configuration.Default; this.configuration = configuration ?? Configuration.Default;
this.PixelType = pixelType; this.PixelType = pixelType;
this.size = size; this.size = size;
this.Metadata = metadata ?? new ImageMetadata(); this.Metadata = metadata ?? new ImageMetadata();
@ -48,11 +49,6 @@ namespace SixLabors.ImageSharp
{ {
} }
/// <summary>
/// Gets the <see cref="Configuration"/>.
/// </summary>
protected Configuration Configuration { get; }
/// <summary> /// <summary>
/// Gets the <see cref="ImageFrameCollection"/> implementing the public <see cref="Frames"/> property. /// Gets the <see cref="ImageFrameCollection"/> implementing the public <see cref="Frames"/> property.
/// </summary> /// </summary>
@ -75,10 +71,8 @@ namespace SixLabors.ImageSharp
/// </summary> /// </summary>
public ImageFrameCollection Frames => this.NonGenericFrameCollection; public ImageFrameCollection Frames => this.NonGenericFrameCollection;
/// <summary> /// <inheritdoc/>
/// Gets the pixel buffer. Configuration IConfigurable.Configuration => this.configuration;
/// </summary>
Configuration IConfigurable.Configuration => this.Configuration;
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
@ -108,7 +102,7 @@ namespace SixLabors.ImageSharp
/// <typeparam name="TPixel2">The pixel format.</typeparam> /// <typeparam name="TPixel2">The pixel format.</typeparam>
/// <returns>The <see cref="Image{TPixel2}"/></returns> /// <returns>The <see cref="Image{TPixel2}"/></returns>
public Image<TPixel2> CloneAs<TPixel2>() public Image<TPixel2> CloneAs<TPixel2>()
where TPixel2 : struct, IPixel<TPixel2> => this.CloneAs<TPixel2>(this.Configuration); where TPixel2 : struct, IPixel<TPixel2> => this.CloneAs<TPixel2>(this.GetConfiguration());
/// <summary> /// <summary>
/// Returns a copy of the image in the given pixel format. /// Returns a copy of the image in the given pixel format.

15
src/ImageSharp/ImageFrame.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.PixelFormats; 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 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. /// In all other cases it is the only frame of the image.
/// </summary> /// </summary>
public abstract partial class ImageFrame : IDisposable public abstract partial class ImageFrame : IConfigurable, IDisposable
{ {
private readonly Configuration configuration;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ImageFrame"/> class. /// Initializes a new instance of the <see cref="ImageFrame"/> class.
/// </summary> /// </summary>
@ -27,7 +30,7 @@ namespace SixLabors.ImageSharp
Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(configuration, nameof(configuration));
Guard.NotNull(metadata, nameof(metadata)); Guard.NotNull(metadata, nameof(metadata));
this.Configuration = configuration; this.configuration = configuration ?? Configuration.Default;
this.MemoryAllocator = configuration.MemoryAllocator; this.MemoryAllocator = configuration.MemoryAllocator;
this.Width = width; this.Width = width;
this.Height = height; this.Height = height;
@ -39,11 +42,6 @@ namespace SixLabors.ImageSharp
/// </summary> /// </summary>
public MemoryAllocator MemoryAllocator { get; } public MemoryAllocator MemoryAllocator { get; }
/// <summary>
/// Gets the <see cref="Configuration"/> instance associated with this <see cref="ImageFrame{TPixel}"/>.
/// </summary>
internal Configuration Configuration { get; }
/// <summary> /// <summary>
/// Gets the width. /// Gets the width.
/// </summary> /// </summary>
@ -59,6 +57,9 @@ namespace SixLabors.ImageSharp
/// </summary> /// </summary>
public ImageFrameMetadata Metadata { get; } public ImageFrameMetadata Metadata { get; }
/// <inheritdoc/>
Configuration IConfigurable.Configuration => this.configuration;
/// <summary> /// <summary>
/// Gets the size of the frame. /// Gets the size of the frame.
/// </summary> /// </summary>

6
src/ImageSharp/ImageFrame{TPixel}.cs

@ -219,7 +219,7 @@ namespace SixLabors.ImageSharp
this.PixelBuffer.GetSpan().CopyTo(dest1); this.PixelBuffer.GetSpan().CopyTo(dest1);
} }
PixelOperations<TPixel>.Instance.To(this.Configuration, this.PixelBuffer.GetSpan(), destination); PixelOperations<TPixel>.Instance.To(this.GetConfiguration(), this.PixelBuffer.GetSpan(), destination);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -229,7 +229,7 @@ namespace SixLabors.ImageSharp
/// Clones the current instance. /// Clones the current instance.
/// </summary> /// </summary>
/// <returns>The <see cref="ImageFrame{TPixel}"/></returns> /// <returns>The <see cref="ImageFrame{TPixel}"/></returns>
internal ImageFrame<TPixel> Clone() => this.Clone(this.Configuration); internal ImageFrame<TPixel> Clone() => this.Clone(this.GetConfiguration());
/// <summary> /// <summary>
/// Clones the current instance. /// Clones the current instance.
@ -244,7 +244,7 @@ namespace SixLabors.ImageSharp
/// <typeparam name="TPixel2">The pixel format.</typeparam> /// <typeparam name="TPixel2">The pixel format.</typeparam>
/// <returns>The <see cref="ImageFrame{TPixel2}"/></returns> /// <returns>The <see cref="ImageFrame{TPixel2}"/></returns>
internal ImageFrame<TPixel2> CloneAs<TPixel2>() internal ImageFrame<TPixel2> CloneAs<TPixel2>()
where TPixel2 : struct, IPixel<TPixel2> => this.CloneAs<TPixel2>(this.Configuration); where TPixel2 : struct, IPixel<TPixel2> => this.CloneAs<TPixel2>(this.GetConfiguration());
/// <summary> /// <summary>
/// Returns a copy of the image frame in the given pixel format. /// Returns a copy of the image frame in the given pixel format.

2
src/ImageSharp/Image{TPixel}.cs

@ -154,7 +154,7 @@ namespace SixLabors.ImageSharp
/// Clones the current image /// Clones the current image
/// </summary> /// </summary>
/// <returns>Returns a new image with all the same metadata as the original.</returns> /// <returns>Returns a new image with all the same metadata as the original.</returns>
public Image<TPixel> Clone() => this.Clone(this.Configuration); public Image<TPixel> Clone() => this.Clone(this.GetConfiguration());
/// <summary> /// <summary>
/// Clones the current image with the given configuration. /// Clones the current image with the given configuration.

5
src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor{TPixel}.cs

@ -64,6 +64,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
int width = maxX - minX; int width = maxX - minX;
var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY); var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY);
Configuration configuration = this.Configuration;
using (IMemoryOwner<TPixel> colors = source.MemoryAllocator.Allocate<TPixel>(width)) using (IMemoryOwner<TPixel> colors = source.MemoryAllocator.Allocate<TPixel>(width))
using (IMemoryOwner<float> amount = source.MemoryAllocator.Allocate<float>(width)) using (IMemoryOwner<float> amount = source.MemoryAllocator.Allocate<float>(width))
@ -79,7 +80,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
ParallelHelper.IterateRows( ParallelHelper.IterateRows(
workingRect, workingRect,
this.Configuration, configuration,
rows => rows =>
{ {
for (int y = rows.Min; y < rows.Max; y++) 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 // This switched color & destination in the 2nd and 3rd places because we are applying the target color under the current one
blender.Blend( blender.Blend(
source.Configuration, configuration,
destination, destination,
colors.GetSpan(), colors.GetSpan(),
destination, destination,

5
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); var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY);
float blendPercentage = this.definition.GraphicsOptions.BlendPercentage; float blendPercentage = this.definition.GraphicsOptions.BlendPercentage;
Configuration configuration = this.Configuration;
using (IMemoryOwner<TPixel> rowColors = source.MemoryAllocator.Allocate<TPixel>(width)) using (IMemoryOwner<TPixel> rowColors = source.MemoryAllocator.Allocate<TPixel>(width))
{ {
@ -83,7 +84,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
ParallelHelper.IterateRowsWithTempBuffer<float>( ParallelHelper.IterateRowsWithTempBuffer<float>(
workingRect, workingRect,
this.Configuration, configuration,
(rows, amounts) => (rows, amounts) =>
{ {
Span<float> amountsSpan = amounts.Span; Span<float> amountsSpan = amounts.Span;
@ -102,7 +103,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
Span<TPixel> destination = source.GetPixelRowSpan(offsetY).Slice(offsetX, width); Span<TPixel> destination = source.GetPixelRowSpan(offsetY).Slice(offsetX, width);
this.blender.Blend( this.blender.Blend(
source.Configuration, configuration,
destination, destination,
destination, destination,
rowColors.GetSpan(), rowColors.GetSpan(),

5
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); var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY);
float blendPercentage = this.definition.GraphicsOptions.BlendPercentage; float blendPercentage = this.definition.GraphicsOptions.BlendPercentage;
Configuration configuration = this.Configuration;
using (IMemoryOwner<TPixel> rowColors = source.MemoryAllocator.Allocate<TPixel>(width)) using (IMemoryOwner<TPixel> rowColors = source.MemoryAllocator.Allocate<TPixel>(width))
{ {
@ -87,7 +88,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
ParallelHelper.IterateRowsWithTempBuffer<float>( ParallelHelper.IterateRowsWithTempBuffer<float>(
workingRect, workingRect,
this.Configuration, configuration,
(rows, amounts) => (rows, amounts) =>
{ {
Span<float> amountsSpan = amounts.Span; Span<float> amountsSpan = amounts.Span;
@ -105,7 +106,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
Span<TPixel> destination = source.GetPixelRowSpan(offsetY).Slice(offsetX, width); Span<TPixel> destination = source.GetPixelRowSpan(offsetY).Slice(offsetX, width);
this.blender.Blend( this.blender.Blend(
source.Configuration, configuration,
destination, destination,
destination, destination,
rowColors.GetSpan(), rowColors.GetSpan(),

6
src/ImageSharp/Processing/Processors/Quantization/FrameQuantizer{TPixel}.cs

@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Processors.Dithering; 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. // Collect the palette. Required before the second pass runs.
ReadOnlyMemory<TPixel> palette = this.GetPalette(); ReadOnlyMemory<TPixel> palette = this.GetPalette();
this.paletteVector = image.Configuration.MemoryAllocator.Allocate<Vector4>(palette.Length); Configuration configuration = image.GetConfiguration();
this.paletteVector = configuration.MemoryAllocator.Allocate<Vector4>(palette.Length);
PixelOperations<TPixel>.Instance.ToVector4( PixelOperations<TPixel>.Instance.ToVector4(
image.Configuration, configuration,
palette.Span, palette.Span,
this.paletteVector.Memory.Span, this.paletteVector.Memory.Span,
PixelConversionModifiers.Scale); PixelConversionModifiers.Scale);

2
src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs

@ -471,7 +471,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
{ {
Span<TPixel> row = source.GetPixelRowSpan(y); Span<TPixel> row = source.GetPixelRowSpan(y);
Span<Rgba32> rgbaSpan = rgbaBuffer.GetSpan(); Span<Rgba32> rgbaSpan = rgbaBuffer.GetSpan();
PixelOperations<TPixel>.Instance.ToRgba32(source.Configuration, row, rgbaSpan); PixelOperations<TPixel>.Instance.ToRgba32(source.GetConfiguration(), row, rgbaSpan);
ref Rgba32 scanBaseRef = ref MemoryMarshal.GetReference(rgbaSpan); ref Rgba32 scanBaseRef = ref MemoryMarshal.GetReference(rgbaSpan);
// And loop through each column // And loop through each column

Loading…
Cancel
Save