Browse Source

refactor FilterProcessor stuff

af/merge-core
Anton Firszov 7 years ago
parent
commit
ff045a1362
  1. 2
      src/ImageSharp/IImage.cs
  2. 12
      src/ImageSharp/Processing/BlackWhiteExtensions.cs
  3. 12
      src/ImageSharp/Processing/FilterExtensions.cs
  4. 12
      src/ImageSharp/Processing/OpacityExtensions.cs
  5. 3
      src/ImageSharp/Processing/Processors/Filters/BlackWhiteProcessor.cs
  6. 43
      src/ImageSharp/Processing/Processors/Filters/FilterProcessor.cs
  7. 55
      src/ImageSharp/Processing/Processors/Filters/FilterProcessorImplementation.cs
  8. 3
      src/ImageSharp/Processing/Processors/Filters/OpacityProcessor.cs
  9. 7
      src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor.cs
  10. 4
      src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessorImplementation.cs
  11. 1
      tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs

2
src/ImageSharp/IImage.cs

@ -4,7 +4,7 @@
using System;
namespace SixLabors.ImageSharp
{
{
/// <summary>
/// Encapsulates the properties and methods that describe an image.
/// </summary>

12
src/ImageSharp/Processing/BlackWhiteExtensions.cs

@ -15,24 +15,20 @@ namespace SixLabors.ImageSharp.Processing
/// <summary>
/// Applies black and white toning to the image.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image this method extends.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static IImageProcessingContext<TPixel> BlackWhite<TPixel>(this IImageProcessingContext<TPixel> source)
where TPixel : struct, IPixel<TPixel>
=> source.ApplyProcessor(new BlackWhiteProcessor<TPixel>());
public static IImageProcessingContext BlackWhite(this IImageProcessingContext source)
=> source.ApplyProcessor(new BlackWhiteProcessor());
/// <summary>
/// Applies black and white toning to the image.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image this method extends.</param>
/// <param name="rectangle">
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
/// </param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static IImageProcessingContext<TPixel> BlackWhite<TPixel>(this IImageProcessingContext<TPixel> source, Rectangle rectangle)
where TPixel : struct, IPixel<TPixel>
=> source.ApplyProcessor(new BlackWhiteProcessor<TPixel>(), rectangle);
public static IImageProcessingContext BlackWhite(this IImageProcessingContext source, Rectangle rectangle)
=> source.ApplyProcessor(new BlackWhiteProcessor(), rectangle);
}
}

12
src/ImageSharp/Processing/FilterExtensions.cs

@ -16,26 +16,22 @@ namespace SixLabors.ImageSharp.Processing
/// <summary>
/// Filters an image but the given color matrix
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image this method extends.</param>
/// <param name="matrix">The filter color matrix</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static IImageProcessingContext<TPixel> Filter<TPixel>(this IImageProcessingContext<TPixel> source, ColorMatrix matrix)
where TPixel : struct, IPixel<TPixel>
=> source.ApplyProcessor(new FilterProcessor<TPixel>(matrix));
public static IImageProcessingContext Filter(this IImageProcessingContext source, ColorMatrix matrix)
=> source.ApplyProcessor(new FilterProcessor(matrix));
/// <summary>
/// Filters an image but the given color matrix
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image this method extends.</param>
/// <param name="matrix">The filter color matrix</param>
/// <param name="rectangle">
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
/// </param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static IImageProcessingContext<TPixel> Filter<TPixel>(this IImageProcessingContext<TPixel> source, ColorMatrix matrix, Rectangle rectangle)
where TPixel : struct, IPixel<TPixel>
=> source.ApplyProcessor(new FilterProcessor<TPixel>(matrix), rectangle);
public static IImageProcessingContext Filter(this IImageProcessingContext source, ColorMatrix matrix, Rectangle rectangle)
=> source.ApplyProcessor(new FilterProcessor(matrix), rectangle);
}
}

12
src/ImageSharp/Processing/OpacityExtensions.cs

@ -15,26 +15,22 @@ namespace SixLabors.ImageSharp.Processing
/// <summary>
/// Multiplies the alpha component of the image.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image this method extends.</param>
/// <param name="amount">The proportion of the conversion. Must be between 0 and 1.</param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static IImageProcessingContext<TPixel> Opacity<TPixel>(this IImageProcessingContext<TPixel> source, float amount)
where TPixel : struct, IPixel<TPixel>
=> source.ApplyProcessor(new OpacityProcessor<TPixel>(amount));
public static IImageProcessingContext Opacity(this IImageProcessingContext source, float amount)
=> source.ApplyProcessor(new OpacityProcessor(amount));
/// <summary>
/// Multiplies the alpha component of the image.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image this method extends.</param>
/// <param name="amount">The proportion of the conversion. Must be between 0 and 1.</param>
/// <param name="rectangle">
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
/// </param>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static IImageProcessingContext<TPixel> Opacity<TPixel>(this IImageProcessingContext<TPixel> source, float amount, Rectangle rectangle)
where TPixel : struct, IPixel<TPixel>
=> source.ApplyProcessor(new OpacityProcessor<TPixel>(amount), rectangle);
public static IImageProcessingContext Opacity(this IImageProcessingContext source, float amount, Rectangle rectangle)
=> source.ApplyProcessor(new OpacityProcessor(amount), rectangle);
}
}

3
src/ImageSharp/Processing/Processors/Filters/BlackWhiteProcessor.cs

@ -9,8 +9,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters
/// Applies a black and white filter matrix to the image
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
internal class BlackWhiteProcessor<TPixel> : FilterProcessor<TPixel>
where TPixel : struct, IPixel<TPixel>
internal class BlackWhiteProcessor : FilterProcessor
{
/// <summary>
/// Initializes a new instance of the <see cref="BlackWhiteProcessor{TPixel}"/> class.

43
src/ImageSharp/Processing/Processors/Filters/FilterProcessor.cs

@ -1,25 +1,15 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Numerics;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.ParallelUtils;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Primitives;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Processors.Filters
{
/// <summary>
/// Provides methods that accept a <see cref="ColorMatrix"/> matrix to apply free-form filters to images.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
internal class FilterProcessor<TPixel> : ImageProcessor<TPixel>
where TPixel : struct, IPixel<TPixel>
public class FilterProcessor : IImageProcessor
{
/// <summary>
/// Initializes a new instance of the <see cref="FilterProcessor{TPixel}"/> class.
/// Initializes a new instance of the <see cref="FilterProcessor"/> class.
/// </summary>
/// <param name="matrix">The matrix used to apply the image filter</param>
public FilterProcessor(ColorMatrix matrix) => this.Matrix = matrix;
@ -29,31 +19,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters
/// </summary>
public ColorMatrix Matrix { get; }
/// <inheritdoc/>
protected override void OnFrameApply(ImageFrame<TPixel> source, Rectangle sourceRectangle, Configuration configuration)
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>()
where TPixel : struct, IPixel<TPixel>
{
var interest = Rectangle.Intersect(sourceRectangle, source.Bounds());
int startX = interest.X;
ColorMatrix matrix = this.Matrix;
ParallelHelper.IterateRowsWithTempBuffer<Vector4>(
interest,
configuration,
(rows, vectorBuffer) =>
{
for (int y = rows.Min; y < rows.Max; y++)
{
Span<Vector4> vectorSpan = vectorBuffer.Span;
int length = vectorSpan.Length;
Span<TPixel> rowSpan = source.GetPixelRowSpan(y).Slice(startX, length);
PixelOperations<TPixel>.Instance.ToVector4(configuration, rowSpan, vectorSpan);
Vector4Utils.Transform(vectorSpan, ref matrix);
PixelOperations<TPixel>.Instance.FromVector4Destructive(configuration, vectorSpan, rowSpan);
}
});
return new FilterProcessorImplementation<TPixel>(this);
}
}
}

55
src/ImageSharp/Processing/Processors/Filters/FilterProcessorImplementation.cs

@ -0,0 +1,55 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Numerics;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.ParallelUtils;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Primitives;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Processors.Filters
{
/// <summary>
/// Provides methods that accept a <see cref="ColorMatrix"/> matrix to apply free-form filters to images.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
internal class FilterProcessorImplementation<TPixel> : ImageProcessor<TPixel>
where TPixel : struct, IPixel<TPixel>
{
private readonly FilterProcessor paramterSource;
public FilterProcessorImplementation(FilterProcessor paramterSource)
{
this.paramterSource = paramterSource;
}
/// <inheritdoc/>
protected override void OnFrameApply(ImageFrame<TPixel> source, Rectangle sourceRectangle, Configuration configuration)
{
var interest = Rectangle.Intersect(sourceRectangle, source.Bounds());
int startX = interest.X;
ColorMatrix matrix = this.paramterSource.Matrix;
ParallelHelper.IterateRowsWithTempBuffer<Vector4>(
interest,
configuration,
(rows, vectorBuffer) =>
{
for (int y = rows.Min; y < rows.Max; y++)
{
Span<Vector4> vectorSpan = vectorBuffer.Span;
int length = vectorSpan.Length;
Span<TPixel> rowSpan = source.GetPixelRowSpan(y).Slice(startX, length);
PixelOperations<TPixel>.Instance.ToVector4(configuration, rowSpan, vectorSpan);
Vector4Utils.Transform(vectorSpan, ref matrix);
PixelOperations<TPixel>.Instance.FromVector4Destructive(configuration, vectorSpan, rowSpan);
}
});
}
}
}

3
src/ImageSharp/Processing/Processors/Filters/OpacityProcessor.cs

@ -9,8 +9,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters
/// Applies an opacity filter matrix using the given amount.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
internal class OpacityProcessor<TPixel> : FilterProcessor<TPixel>
where TPixel : struct, IPixel<TPixel>
internal class OpacityProcessor : FilterProcessor
{
/// <summary>
/// Initializes a new instance of the <see cref="OpacityProcessor{TPixel}"/> class.

7
src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor.cs

@ -1,5 +1,5 @@
// // Copyright (c) Six Labors and contributors.
// // Licensed under the Apache License, Version 2.0.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
@ -8,6 +8,9 @@ using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Processors.Transforms
{
// The non-generic processor is responsible for:
// - Encapsulating the parameters of the processor
// - Implementing a factory method to create the pixel-specific processor that contains the implementation
public class ResizeProcessor : IImageProcessor
{
/// <summary>

4
src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessorImplementation.cs

@ -18,10 +18,6 @@ using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Processors.Transforms
{
// The non-generic processor is responsible for:
// - Encapsulating the parameters of the processor
// - Implementing a factory method to create the pixel-specific processor that contains the implementation
/// <summary>
/// Provides methods that allow the resizing of images using various algorithms.
/// Adapted from <see href="http://www.realtimerendering.com/resources/GraphicsGems/gemsiii/filter_rcg.c"/>

1
tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs

@ -40,6 +40,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
nameof(KnownResamplers.Lanczos5),
};
private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.07F);
[Fact]

Loading…
Cancel
Save