Browse Source

drop unnecessary generic IImageProcessorContext<TPixel> usages

af/merge-core
Anton Firszov 7 years ago
parent
commit
9ea9f2df5d
  1. 14
      src/ImageSharp/Processing/Extensions/ProcessingExtensions.cs
  2. 10
      tests/ImageSharp.Benchmarks/Drawing/DrawText.cs
  3. 5
      tests/ImageSharp.Benchmarks/Drawing/DrawTextOutline.cs
  4. 10
      tests/ImageSharp.Benchmarks/Samplers/Resize.cs
  5. 39
      tests/ImageSharp.Tests/ImageOperationTests.cs
  6. 6
      tests/ImageSharp.Tests/Processing/Processors/Convolution/Basic1ParameterConvolutionTests.cs
  7. 4
      tests/ImageSharp.Tests/Processing/Processors/Convolution/BoxBlurTest.cs
  8. 4
      tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianBlurTest.cs
  9. 4
      tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianSharpenTest.cs
  10. 6
      tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs
  11. 2
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs
  12. 69
      tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs
  13. 14
      tests/ImageSharp.Tests/TestUtilities/TestUtils.cs

14
src/ImageSharp/Processing/Extensions/ProcessingExtensions.cs

@ -42,7 +42,7 @@ namespace SixLabors.ImageSharp.Processing
/// <typeparam name="TPixel">The pixel format.</typeparam> /// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image to mutate.</param> /// <param name="source">The image to mutate.</param>
/// <param name="operation">The operation to perform on the source.</param> /// <param name="operation">The operation to perform on the source.</param>
public static void Mutate<TPixel>(this Image<TPixel> source, Action<IImageProcessingContext<TPixel>> operation) public static void Mutate<TPixel>(this Image<TPixel> source, Action<IImageProcessingContext> operation)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
Guard.NotNull(operation, nameof(operation)); Guard.NotNull(operation, nameof(operation));
@ -59,7 +59,7 @@ namespace SixLabors.ImageSharp.Processing
/// <typeparam name="TPixel">The pixel format.</typeparam> /// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image to mutate.</param> /// <param name="source">The image to mutate.</param>
/// <param name="operations">The operations to perform on the source.</param> /// <param name="operations">The operations to perform on the source.</param>
public static void Mutate<TPixel>(this Image<TPixel> source, params IImageProcessor<TPixel>[] operations) public static void Mutate<TPixel>(this Image<TPixel> source, params IImageProcessor[] operations)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
Guard.NotNull(operations, nameof(operations)); Guard.NotNull(operations, nameof(operations));
@ -90,7 +90,7 @@ namespace SixLabors.ImageSharp.Processing
/// <param name="source">The image to clone.</param> /// <param name="source">The image to clone.</param>
/// <param name="operation">The operation to perform on the clone.</param> /// <param name="operation">The operation to perform on the clone.</param>
/// <returns>The new <see cref="SixLabors.ImageSharp.Image{TPixel}"/></returns> /// <returns>The new <see cref="SixLabors.ImageSharp.Image{TPixel}"/></returns>
public static Image<TPixel> Clone<TPixel>(this Image<TPixel> source, Action<IImageProcessingContext<TPixel>> operation) public static Image<TPixel> Clone<TPixel>(this Image<TPixel> source, Action<IImageProcessingContext> operation)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
Guard.NotNull(operation, nameof(operation)); Guard.NotNull(operation, nameof(operation));
@ -108,7 +108,7 @@ namespace SixLabors.ImageSharp.Processing
/// <param name="source">The image to clone.</param> /// <param name="source">The image to clone.</param>
/// <param name="operations">The operations to perform on the clone.</param> /// <param name="operations">The operations to perform on the clone.</param>
/// <returns>The new <see cref="SixLabors.ImageSharp.Image{TPixel}"/></returns> /// <returns>The new <see cref="SixLabors.ImageSharp.Image{TPixel}"/></returns>
public static Image<TPixel> Clone<TPixel>(this Image<TPixel> source, params IImageProcessor<TPixel>[] operations) public static Image<TPixel> Clone<TPixel>(this Image<TPixel> source, params IImageProcessor[] operations)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
Guard.NotNull(operations, nameof(operations)); Guard.NotNull(operations, nameof(operations));
@ -122,14 +122,12 @@ namespace SixLabors.ImageSharp.Processing
/// <summary> /// <summary>
/// Applies the given <see cref="IImageProcessor{TPixel}"/> collection against the context /// Applies the given <see cref="IImageProcessor{TPixel}"/> collection against the context
/// </summary> /// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image processing context.</param> /// <param name="source">The image processing context.</param>
/// <param name="operations">The operations to perform on the source.</param> /// <param name="operations">The operations to perform on the source.</param>
/// <returns>The <see cref="IImageProcessor{TPixel}"/> to allow chaining of operations.</returns> /// <returns>The <see cref="IImageProcessor{TPixel}"/> to allow chaining of operations.</returns>
public static IImageProcessingContext<TPixel> ApplyProcessors<TPixel>(this IImageProcessingContext<TPixel> source, params IImageProcessor<TPixel>[] operations) public static IImageProcessingContext ApplyProcessors(this IImageProcessingContext source, params IImageProcessor[] operations)
where TPixel : struct, IPixel<TPixel>
{ {
foreach (IImageProcessor<TPixel> p in operations) foreach (IImageProcessor p in operations)
{ {
source = source.ApplyProcessor(p); source = source.ApplyProcessor(p);
} }

10
tests/ImageSharp.Benchmarks/Drawing/DrawText.cs

@ -55,8 +55,14 @@ namespace SixLabors.ImageSharp.Benchmarks
image.Mutate(x => DrawTextOldVersion(x, new TextGraphicsOptions(true) { WrapTextWidth = 780 }, TextToRender, font, Processing.Brushes.Solid(Rgba32.HotPink), null, new SixLabors.Primitives.PointF(10, 10))); image.Mutate(x => DrawTextOldVersion(x, new TextGraphicsOptions(true) { WrapTextWidth = 780 }, TextToRender, font, Processing.Brushes.Solid(Rgba32.HotPink), null, new SixLabors.Primitives.PointF(10, 10)));
} }
IImageProcessingContext<TPixel> DrawTextOldVersion<TPixel>(IImageProcessingContext<TPixel> source, TextGraphicsOptions options, string text, SixLabors.Fonts.Font font, IBrush brush, IPen pen, SixLabors.Primitives.PointF location) IImageProcessingContext DrawTextOldVersion(
where TPixel : struct, IPixel<TPixel> IImageProcessingContext source,
TextGraphicsOptions options,
string text,
SixLabors.Fonts.Font font,
IBrush brush,
IPen pen,
SixLabors.Primitives.PointF location)
{ {
float dpiX = 72; float dpiX = 72;
float dpiY = 72; float dpiY = 72;

5
tests/ImageSharp.Benchmarks/Drawing/DrawTextOutline.cs

@ -64,15 +64,14 @@ namespace SixLabors.ImageSharp.Benchmarks
new SixLabors.Primitives.PointF(10, 10))); new SixLabors.Primitives.PointF(10, 10)));
} }
IImageProcessingContext<TPixel> DrawTextOldVersion<TPixel>( IImageProcessingContext DrawTextOldVersion(
IImageProcessingContext<TPixel> source, IImageProcessingContext source,
TextGraphicsOptions options, TextGraphicsOptions options,
string text, string text,
SixLabors.Fonts.Font font, SixLabors.Fonts.Font font,
IBrush brush, IBrush brush,
IPen pen, IPen pen,
SixLabors.Primitives.PointF location) SixLabors.Primitives.PointF location)
where TPixel : struct, IPixel<TPixel>
{ {
var style = new SixLabors.Fonts.RendererOptions(font, options.DpiX, options.DpiY, location) var style = new SixLabors.Fonts.RendererOptions(font, options.DpiX, options.DpiY, location)
{ {

10
tests/ImageSharp.Benchmarks/Samplers/Resize.cs

@ -91,12 +91,12 @@ namespace SixLabors.ImageSharp.Benchmarks
} }
} }
protected abstract void ExecuteResizeOperation(IImageProcessingContext<TPixel> ctx); protected abstract void ExecuteResizeOperation(IImageProcessingContext ctx);
} }
public class Resize_Bicubic_Rgba32 : ResizeBenchmarkBase<Rgba32> public class Resize_Bicubic_Rgba32 : ResizeBenchmarkBase<Rgba32>
{ {
protected override void ExecuteResizeOperation(IImageProcessingContext<Rgba32> ctx) protected override void ExecuteResizeOperation(IImageProcessingContext ctx)
{ {
ctx.Resize(this.DestSize, this.DestSize, KnownResamplers.Bicubic); ctx.Resize(this.DestSize, this.DestSize, KnownResamplers.Bicubic);
} }
@ -143,7 +143,7 @@ namespace SixLabors.ImageSharp.Benchmarks
public class Resize_Bicubic_Bgra32 : ResizeBenchmarkBase<Bgra32> public class Resize_Bicubic_Bgra32 : ResizeBenchmarkBase<Bgra32>
{ {
protected override void ExecuteResizeOperation(IImageProcessingContext<Bgra32> ctx) protected override void ExecuteResizeOperation(IImageProcessingContext ctx)
{ {
ctx.Resize(this.DestSize, this.DestSize, KnownResamplers.Bicubic); ctx.Resize(this.DestSize, this.DestSize, KnownResamplers.Bicubic);
} }
@ -171,7 +171,7 @@ namespace SixLabors.ImageSharp.Benchmarks
public class Resize_Bicubic_Rgb24 : ResizeBenchmarkBase<Rgb24> public class Resize_Bicubic_Rgb24 : ResizeBenchmarkBase<Rgb24>
{ {
protected override void ExecuteResizeOperation(IImageProcessingContext<Rgb24> ctx) protected override void ExecuteResizeOperation(IImageProcessingContext ctx)
{ {
ctx.Resize(this.DestSize, this.DestSize, KnownResamplers.Bicubic); ctx.Resize(this.DestSize, this.DestSize, KnownResamplers.Bicubic);
} }
@ -198,7 +198,7 @@ namespace SixLabors.ImageSharp.Benchmarks
public class Resize_BicubicCompand_Rgba32 : ResizeBenchmarkBase<Rgba32> public class Resize_BicubicCompand_Rgba32 : ResizeBenchmarkBase<Rgba32>
{ {
protected override void ExecuteResizeOperation(IImageProcessingContext<Rgba32> ctx) protected override void ExecuteResizeOperation(IImageProcessingContext ctx)
{ {
ctx.Resize(this.DestSize, this.DestSize, KnownResamplers.Bicubic, true); ctx.Resize(this.DestSize, this.DestSize, KnownResamplers.Bicubic, true);
} }

39
tests/ImageSharp.Tests/ImageOperationTests.cs

@ -21,14 +21,21 @@ namespace SixLabors.ImageSharp.Tests
{ {
private readonly Image<Rgba32> image; private readonly Image<Rgba32> image;
private readonly FakeImageOperationsProvider provider; private readonly FakeImageOperationsProvider provider;
private readonly IImageProcessor<Rgba32> processor; private readonly IImageProcessor<Rgba32> processorImplementation;
private readonly IImageProcessor processorDefinition;
public Configuration Configuration { get; private set; } public Configuration Configuration { get; private set; }
public ImageOperationTests() public ImageOperationTests()
{ {
this.provider = new FakeImageOperationsProvider(); this.provider = new FakeImageOperationsProvider();
this.processor = new Mock<IImageProcessor<Rgba32>>().Object; this.processorImplementation = new Mock<IImageProcessor<Rgba32>>().Object;
Mock<IImageProcessor> processorMock = new Mock<IImageProcessor>();
processorMock.Setup(p => p.CreatePixelSpecificProcessor<Rgba32>()).Returns(this.processorImplementation);
this.processorDefinition = processorMock.Object;
this.image = new Image<Rgba32>(new Configuration() this.image = new Image<Rgba32>(new Configuration()
{ {
ImageOperationsProvider = this.provider ImageOperationsProvider = this.provider
@ -38,61 +45,61 @@ namespace SixLabors.ImageSharp.Tests
[Fact] [Fact]
public void MutateCallsImageOperationsProvider_Func_OriginalImage() public void MutateCallsImageOperationsProvider_Func_OriginalImage()
{ {
this.image.Mutate(x => x.ApplyProcessor(this.processor)); this.image.Mutate(x => x.ApplyProcessor(this.processorDefinition));
Assert.True(this.provider.HasCreated(this.image)); Assert.True(this.provider.HasCreated(this.image));
Assert.Contains(this.processor, this.provider.AppliedOperations(this.image).Select(x => x.GenericProcessor)); Assert.Contains(this.processorImplementation, this.provider.AppliedOperations(this.image).Select(x => x.GenericProcessor));
} }
[Fact] [Fact]
public void MutateCallsImageOperationsProvider_ListOfProcessors_OriginalImage() public void MutateCallsImageOperationsProvider_ListOfProcessors_OriginalImage()
{ {
this.image.Mutate(this.processor); this.image.Mutate(this.processorDefinition);
Assert.True(this.provider.HasCreated(this.image)); Assert.True(this.provider.HasCreated(this.image));
Assert.Contains(this.processor, this.provider.AppliedOperations(this.image).Select(x => x.GenericProcessor)); Assert.Contains(this.processorImplementation, this.provider.AppliedOperations(this.image).Select(x => x.GenericProcessor));
} }
[Fact] [Fact]
public void CloneCallsImageOperationsProvider_Func_WithDuplicateImage() public void CloneCallsImageOperationsProvider_Func_WithDuplicateImage()
{ {
Image<Rgba32> returned = this.image.Clone(x => x.ApplyProcessor(this.processor)); Image<Rgba32> returned = this.image.Clone(x => x.ApplyProcessor(this.processorDefinition));
Assert.True(this.provider.HasCreated(returned)); Assert.True(this.provider.HasCreated(returned));
Assert.Contains(this.processor, this.provider.AppliedOperations(returned).Select(x => x.GenericProcessor)); Assert.Contains(this.processorImplementation, this.provider.AppliedOperations(returned).Select(x => x.GenericProcessor));
} }
[Fact] [Fact]
public void CloneCallsImageOperationsProvider_ListOfProcessors_WithDuplicateImage() public void CloneCallsImageOperationsProvider_ListOfProcessors_WithDuplicateImage()
{ {
Image<Rgba32> returned = this.image.Clone(this.processor); Image<Rgba32> returned = this.image.Clone(this.processorDefinition);
Assert.True(this.provider.HasCreated(returned)); Assert.True(this.provider.HasCreated(returned));
Assert.Contains(this.processor, this.provider.AppliedOperations(returned).Select(x => x.GenericProcessor)); Assert.Contains(this.processorImplementation, this.provider.AppliedOperations(returned).Select(x => x.GenericProcessor));
} }
[Fact] [Fact]
public void CloneCallsImageOperationsProvider_Func_NotOnOrigional() public void CloneCallsImageOperationsProvider_Func_NotOnOrigional()
{ {
Image<Rgba32> returned = this.image.Clone(x => x.ApplyProcessor(this.processor)); Image<Rgba32> returned = this.image.Clone(x => x.ApplyProcessor(this.processorDefinition));
Assert.False(this.provider.HasCreated(this.image)); Assert.False(this.provider.HasCreated(this.image));
Assert.DoesNotContain(this.processor, this.provider.AppliedOperations(this.image).Select(x => x.GenericProcessor)); Assert.DoesNotContain(this.processorImplementation, this.provider.AppliedOperations(this.image).Select(x => x.GenericProcessor));
} }
[Fact] [Fact]
public void CloneCallsImageOperationsProvider_ListOfProcessors_NotOnOrigional() public void CloneCallsImageOperationsProvider_ListOfProcessors_NotOnOrigional()
{ {
Image<Rgba32> returned = this.image.Clone(this.processor); Image<Rgba32> returned = this.image.Clone(this.processorDefinition);
Assert.False(this.provider.HasCreated(this.image)); Assert.False(this.provider.HasCreated(this.image));
Assert.DoesNotContain(this.processor, this.provider.AppliedOperations(this.image).Select(x => x.GenericProcessor)); Assert.DoesNotContain(this.processorImplementation, this.provider.AppliedOperations(this.image).Select(x => x.GenericProcessor));
} }
[Fact] [Fact]
public void ApplyProcessors_ListOfProcessors_AppliesAllProcessorsToOperation() public void ApplyProcessors_ListOfProcessors_AppliesAllProcessorsToOperation()
{ {
var operations = new FakeImageOperationsProvider.FakeImageOperations<Rgba32>(null, false); var operations = new FakeImageOperationsProvider.FakeImageOperations<Rgba32>(null, false);
operations.ApplyProcessors(this.processor); operations.ApplyProcessors(this.processorDefinition);
Assert.Contains(this.processor, operations.Applied.Select(x => x.GenericProcessor)); Assert.Contains(this.processorImplementation, operations.Applied.Select(x => x.GenericProcessor));
} }
public void Dispose() => this.image.Dispose(); public void Dispose() => this.image.Dispose();

6
tests/ImageSharp.Tests/Processing/Processors/Convolution/Basic1ParameterConvolutionTests.cs

@ -47,10 +47,8 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution
ValidatorComparer); ValidatorComparer);
} }
protected abstract void Apply<TPixel>(IImageProcessingContext<TPixel> ctx, int value) protected abstract void Apply(IImageProcessingContext ctx, int value);
where TPixel : struct, IPixel<TPixel>;
protected abstract void Apply<TPixel>(IImageProcessingContext<TPixel> ctx, int value, Rectangle bounds) protected abstract void Apply(IImageProcessingContext ctx, int value, Rectangle bounds);
where TPixel : struct, IPixel<TPixel>;
} }
} }

4
tests/ImageSharp.Tests/Processing/Processors/Convolution/BoxBlurTest.cs

@ -9,9 +9,9 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution
[GroupOutput("Convolution")] [GroupOutput("Convolution")]
public class BoxBlurTest : Basic1ParameterConvolutionTests public class BoxBlurTest : Basic1ParameterConvolutionTests
{ {
protected override void Apply<TPixel>(IImageProcessingContext<TPixel> ctx, int value) => ctx.BoxBlur(value); protected override void Apply(IImageProcessingContext ctx, int value) => ctx.BoxBlur(value);
protected override void Apply<TPixel>(IImageProcessingContext<TPixel> ctx, int value, Rectangle bounds) => protected override void Apply(IImageProcessingContext ctx, int value, Rectangle bounds) =>
ctx.BoxBlur(value, bounds); ctx.BoxBlur(value, bounds);
} }
} }

4
tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianBlurTest.cs

@ -13,9 +13,9 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution
[GroupOutput("Convolution")] [GroupOutput("Convolution")]
public class GaussianBlurTest : Basic1ParameterConvolutionTests public class GaussianBlurTest : Basic1ParameterConvolutionTests
{ {
protected override void Apply<TPixel>(IImageProcessingContext<TPixel> ctx, int value) => ctx.GaussianBlur(value); protected override void Apply(IImageProcessingContext ctx, int value) => ctx.GaussianBlur(value);
protected override void Apply<TPixel>(IImageProcessingContext<TPixel> ctx, int value, Rectangle bounds) => protected override void Apply(IImageProcessingContext ctx, int value, Rectangle bounds) =>
ctx.GaussianBlur(value, bounds); ctx.GaussianBlur(value, bounds);
} }
} }

4
tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianSharpenTest.cs

@ -12,9 +12,9 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution
[GroupOutput("Convolution")] [GroupOutput("Convolution")]
public class GaussianSharpenTest : Basic1ParameterConvolutionTests public class GaussianSharpenTest : Basic1ParameterConvolutionTests
{ {
protected override void Apply<TPixel>(IImageProcessingContext<TPixel> ctx, int value) => ctx.GaussianSharpen(value); protected override void Apply(IImageProcessingContext ctx, int value) => ctx.GaussianSharpen(value);
protected override void Apply<TPixel>(IImageProcessingContext<TPixel> ctx, int value, Rectangle bounds) => protected override void Apply(IImageProcessingContext ctx, int value, Rectangle bounds) =>
ctx.GaussianSharpen(value, bounds); ctx.GaussianSharpen(value, bounds);
} }
} }

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

@ -322,11 +322,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
} }
FormattableString testOutputDetails = $"{samplerName}-{destSizeInfo}"; FormattableString testOutputDetails = $"{samplerName}-{destSizeInfo}";
ctx.Apply(
img => img.DebugSave(
provider,
$"{testOutputDetails}-ORIGINAL",
appendPixelTypeToFileName: false));
ctx.Resize((Size)newSize, sampler, false); ctx.Resize((Size)newSize, sampler, false);
return testOutputDetails; return testOutputDetails;
}, },

2
tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs

@ -104,7 +104,7 @@ namespace SixLabors.ImageSharp.Tests
/// <summary> /// <summary>
/// Returns an <see cref="Image{TPixel}"/> instance to the test case with the necessary traits. /// Returns an <see cref="Image{TPixel}"/> instance to the test case with the necessary traits.
/// </summary> /// </summary>
public Image<TPixel> GetImage(Action<IImageProcessingContext<TPixel>> operationsToApply) public Image<TPixel> GetImage(Action<IImageProcessingContext> operationsToApply)
{ {
Image<TPixel> img = this.GetImage(); Image<TPixel> img = this.GetImage();
img.Mutate(operationsToApply); img.Mutate(operationsToApply);

69
tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

@ -9,10 +9,13 @@ using System.Numerics;
using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.ParallelUtils;
using SixLabors.Memory; using SixLabors.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
using SixLabors.Primitives;
using Xunit; using Xunit;
@ -21,39 +24,11 @@ namespace SixLabors.ImageSharp.Tests
public static class TestImageExtensions public static class TestImageExtensions
{ {
/// <summary> /// <summary>
/// TODO: This should be a common processing method! The image.Opacity(val) multiplies the alpha channel! /// TODO: Consider adding this private processor to the library
/// </summary> /// </summary>
/// <typeparam name="TPixel"></typeparam>
/// <param name="ctx"></param> /// <param name="ctx"></param>
public static void MakeOpaque<TPixel>(this IImageProcessingContext<TPixel> ctx) public static void MakeOpaque(this IImageProcessingContext ctx) =>
where TPixel : struct, IPixel<TPixel> ctx.ApplyProcessor(new MakeOpaqueProcessor());
{
MemoryAllocator memoryAllocator = ctx.MemoryAllocator;
ctx.Apply(
img =>
{
Configuration configuration = img.GetConfiguration();
using (Buffer2D<Vector4> temp = memoryAllocator.Allocate2D<Vector4>(img.Width, img.Height))
{
Span<Vector4> tempSpan = temp.GetSpan();
foreach (ImageFrame<TPixel> frame in img.Frames)
{
Span<TPixel> pixelSpan = frame.GetPixelSpan();
PixelOperations<TPixel>.Instance.ToVector4(configuration, pixelSpan, tempSpan, PixelConversionModifiers.Scale);
for (int i = 0; i < tempSpan.Length; i++)
{
ref Vector4 v = ref tempSpan[i];
v.W = 1F;
}
PixelOperations<TPixel>.Instance.FromVector4Destructive(configuration, tempSpan, pixelSpan, PixelConversionModifiers.Scale);
}
}
});
}
public static void DebugSave( public static void DebugSave(
this Image image, this Image image,
@ -702,5 +677,37 @@ namespace SixLabors.ImageSharp.Tests
return image; return image;
} }
private class MakeOpaqueProcessor : IImageProcessor
{
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>()
where TPixel : struct, IPixel<TPixel>
{
return new MakeOpaqueProcessor<TPixel>();
}
}
private class MakeOpaqueProcessor<TPixel> : ImageProcessor<TPixel>
where TPixel : struct, IPixel<TPixel>
{
protected override void OnFrameApply(ImageFrame<TPixel> source, Rectangle sourceRectangle, Configuration configuration)
{
ParallelHelper.IterateRowsWithTempBuffer<Vector4>(sourceRectangle, configuration,
(rows, temp) =>
{
Span<Vector4> tempSpan = temp.Span;
for (int y = rows.Min; y < rows.Max; y++)
{
var rowSpan = source.GetPixelRowSpan(y).Slice(sourceRectangle.Left, sourceRectangle.Width);
PixelOperations<TPixel>.Instance.ToVector4(configuration, rowSpan, tempSpan, PixelConversionModifiers.Scale);
for (int i = 0; i < tempSpan.Length; i++)
{
ref Vector4 v = ref tempSpan[i];
v.W = 1F;
}
PixelOperations<TPixel>.Instance.FromVector4Destructive(configuration, tempSpan, rowSpan, PixelConversionModifiers.Scale);
}
});
}
}
} }
} }

14
tests/ImageSharp.Tests/TestUtilities/TestUtils.cs

@ -165,7 +165,7 @@ namespace SixLabors.ImageSharp.Tests
/// <param name="appendSourceFileOrDescription"></param> /// <param name="appendSourceFileOrDescription"></param>
internal static void RunValidatingProcessorTest<TPixel>( internal static void RunValidatingProcessorTest<TPixel>(
this TestImageProvider<TPixel> provider, this TestImageProvider<TPixel> provider,
Action<IImageProcessingContext<TPixel>> process, Action<IImageProcessingContext> process,
object testOutputDetails = null, object testOutputDetails = null,
ImageComparer comparer = null, ImageComparer comparer = null,
bool appendPixelTypeToFileName = true, bool appendPixelTypeToFileName = true,
@ -202,7 +202,7 @@ namespace SixLabors.ImageSharp.Tests
internal static void RunValidatingProcessorTest<TPixel>( internal static void RunValidatingProcessorTest<TPixel>(
this TestImageProvider<TPixel> provider, this TestImageProvider<TPixel> provider,
Func<IImageProcessingContext<TPixel>, FormattableString> processAndGetTestOutputDetails, Func<IImageProcessingContext, FormattableString> processAndGetTestOutputDetails,
ImageComparer comparer = null, ImageComparer comparer = null,
bool appendPixelTypeToFileName = true, bool appendPixelTypeToFileName = true,
bool appendSourceFileOrDescription = true) bool appendSourceFileOrDescription = true)
@ -241,7 +241,7 @@ namespace SixLabors.ImageSharp.Tests
public static void RunValidatingProcessorTestOnWrappedMemoryImage<TPixel>( public static void RunValidatingProcessorTestOnWrappedMemoryImage<TPixel>(
this TestImageProvider<TPixel> provider, this TestImageProvider<TPixel> provider,
Action<IImageProcessingContext<TPixel>> process, Action<IImageProcessingContext> process,
object testOutputDetails = null, object testOutputDetails = null,
ImageComparer comparer = null, ImageComparer comparer = null,
string useReferenceOutputFrom = null, string useReferenceOutputFrom = null,
@ -291,11 +291,11 @@ namespace SixLabors.ImageSharp.Tests
} }
/// <summary> /// <summary>
/// Same as <see cref="RunValidatingProcessorTest{TPixel}"/> but with an additional <see cref="Rectangle"/> parameter passed to 'process' /// Same as 'RunValidatingProcessorTest{TPixel}' but with an additional <see cref="Rectangle"/> parameter passed to 'process'
/// </summary> /// </summary>
internal static void RunRectangleConstrainedValidatingProcessorTest<TPixel>( internal static void RunRectangleConstrainedValidatingProcessorTest<TPixel>(
this TestImageProvider<TPixel> provider, this TestImageProvider<TPixel> provider,
Action<IImageProcessingContext<TPixel>, Rectangle> process, Action<IImageProcessingContext, Rectangle> process,
object testOutputDetails = null, object testOutputDetails = null,
ImageComparer comparer = null) ImageComparer comparer = null)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
@ -315,11 +315,11 @@ namespace SixLabors.ImageSharp.Tests
} }
/// <summary> /// <summary>
/// Same as <see cref="RunValidatingProcessorTest{TPixel}"/> but without the 'CompareToReferenceOutput()' step. /// Same as 'RunValidatingProcessorTest{TPixel}' but without the 'CompareToReferenceOutput()' step.
/// </summary> /// </summary>
internal static void RunProcessorTest<TPixel>( internal static void RunProcessorTest<TPixel>(
this TestImageProvider<TPixel> provider, this TestImageProvider<TPixel> provider,
Action<IImageProcessingContext<TPixel>> process, Action<IImageProcessingContext> process,
object testOutputDetails = null) object testOutputDetails = null)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {

Loading…
Cancel
Save