Browse Source

Normalize configuration usage and expose method.

af/octree-no-pixelmap
James Jackson-South 6 years ago
parent
commit
4f46b3e1a4
  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)
=> 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>
/// 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.
@ -161,14 +177,6 @@ namespace SixLabors.ImageSharp.Advanced
internal static MemoryAllocator GetMemoryAllocator(this IConfigurable source)
=> 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>
/// Returns a reference to the 0th element of the Pixel buffer.
/// 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.
namespace SixLabors.ImageSharp.Advanced
@ -9,8 +9,8 @@ namespace SixLabors.ImageSharp.Advanced
internal interface IConfigurable
{
/// <summary>
/// Gets the configuration.
/// Gets the configuration which allows altering default behaviour or extending the library.
/// </summary>
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.
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<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 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
{
private Size size;
private readonly Configuration configuration;
/// <summary>
/// Initializes a new instance of the <see cref="Image"/> class.
/// </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="metadata">The <see cref="ImageMetadata"/>.</param>
/// <param name="size">The <see cref="size"/>.</param>
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
{
}
/// <summary>
/// Gets the <see cref="Configuration"/>.
/// </summary>
protected Configuration Configuration { get; }
/// <summary>
/// Gets the <see cref="ImageFrameCollection"/> implementing the public <see cref="Frames"/> property.
/// </summary>
@ -75,10 +71,8 @@ namespace SixLabors.ImageSharp
/// </summary>
public ImageFrameCollection Frames => this.NonGenericFrameCollection;
/// <summary>
/// Gets the pixel buffer.
/// </summary>
Configuration IConfigurable.Configuration => this.Configuration;
/// <inheritdoc/>
Configuration IConfigurable.Configuration => this.configuration;
/// <inheritdoc />
public void Dispose()
@ -108,7 +102,7 @@ namespace SixLabors.ImageSharp
/// <typeparam name="TPixel2">The pixel format.</typeparam>
/// <returns>The <see cref="Image{TPixel2}"/></returns>
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>
/// 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.
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.
/// </summary>
public abstract partial class ImageFrame : IDisposable
public abstract partial class ImageFrame : IConfigurable, IDisposable
{
private readonly Configuration configuration;
/// <summary>
/// Initializes a new instance of the <see cref="ImageFrame"/> class.
/// </summary>
@ -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
/// </summary>
public MemoryAllocator MemoryAllocator { get; }
/// <summary>
/// Gets the <see cref="Configuration"/> instance associated with this <see cref="ImageFrame{TPixel}"/>.
/// </summary>
internal Configuration Configuration { get; }
/// <summary>
/// Gets the width.
/// </summary>
@ -59,6 +57,9 @@ namespace SixLabors.ImageSharp
/// </summary>
public ImageFrameMetadata Metadata { get; }
/// <inheritdoc/>
Configuration IConfigurable.Configuration => this.configuration;
/// <summary>
/// Gets the size of the frame.
/// </summary>

6
src/ImageSharp/ImageFrame{TPixel}.cs

@ -219,7 +219,7 @@ namespace SixLabors.ImageSharp
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/>
@ -229,7 +229,7 @@ namespace SixLabors.ImageSharp
/// Clones the current instance.
/// </summary>
/// <returns>The <see cref="ImageFrame{TPixel}"/></returns>
internal ImageFrame<TPixel> Clone() => this.Clone(this.Configuration);
internal ImageFrame<TPixel> Clone() => this.Clone(this.GetConfiguration());
/// <summary>
/// Clones the current instance.
@ -244,7 +244,7 @@ namespace SixLabors.ImageSharp
/// <typeparam name="TPixel2">The pixel format.</typeparam>
/// <returns>The <see cref="ImageFrame{TPixel2}"/></returns>
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>
/// 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
/// </summary>
/// <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>
/// 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;
var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY);
Configuration configuration = this.Configuration;
using (IMemoryOwner<TPixel> colors = source.MemoryAllocator.Allocate<TPixel>(width))
using (IMemoryOwner<float> amount = source.MemoryAllocator.Allocate<float>(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,

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

6
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<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(
image.Configuration,
configuration,
palette.Span,
this.paletteVector.Memory.Span,
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<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);
// And loop through each column

Loading…
Cancel
Save