Browse Source

wip - Add basic structure for the swizzler

js/color-alpha-handling
Evangelink 6 years ago
parent
commit
f3c3cb6413
  1. 24
      src/ImageSharp/Processing/Extensions/Transforms/SwizzleExtensions.cs
  2. 18
      src/ImageSharp/Processing/Processors/Transforms/ISwizzler.cs
  3. 33
      src/ImageSharp/Processing/Processors/Transforms/SwizzleProcessor{TSwizzler,TPixel}.cs
  4. 32
      src/ImageSharp/Processing/Processors/Transforms/SwizzleProcessor{TSwizzler}.cs
  5. 31
      tests/ImageSharp.Tests/Processing/Transforms/SwizzleTests.cs

24
src/ImageSharp/Processing/Extensions/Transforms/SwizzleExtensions.cs

@ -0,0 +1,24 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.Processing.Processors.Transforms;
namespace SixLabors.ImageSharp.Processing.Extensions.Transforms
{
/// <summary>
/// Defines extensions that allow the application of swizzle operations on an <see cref="Image"/>
/// </summary>
public static class SwizzleExtensions
{
/// <summary>
/// Swizzles an image.
/// </summary>
/// <param name="source">The image to swizzle.</param>
/// <param name="swizzler">The swizzler function.</param>
/// <typeparam name="TSwizzler">The swizzler function type.</typeparam>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Swizzle<TSwizzler>(this IImageProcessingContext source, TSwizzler swizzler)
where TSwizzler : struct, ISwizzler
=> source.ApplyProcessor(new SwizzleProcessor<TSwizzler>(swizzler));
}
}

18
src/ImageSharp/Processing/Processors/Transforms/ISwizzler.cs

@ -0,0 +1,18 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
namespace SixLabors.ImageSharp.Processing.Processors.Transforms
{
/// <summary>
/// Encapsulate an algorithm to swizzle pixels in an image.
/// </summary>
public interface ISwizzler
{
/// <summary>
/// Applies the swizzle transformation to a given point.
/// </summary>
/// <param name="point">Point to transform.</param>
/// <returns>Transformed point.</returns>
Point Transform(Point point);
}
}

33
src/ImageSharp/Processing/Processors/Transforms/SwizzleProcessor{TSwizzler,TPixel}.cs

@ -0,0 +1,33 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Processing.Processors.Transforms
{
internal class SwizzleProcessor<TSwizzler, TPixel> : ImageProcessor<TPixel>
where TSwizzler : struct, ISwizzler
where TPixel : unmanaged, IPixel<TPixel>
{
private readonly TSwizzler swizzler;
public SwizzleProcessor(Configuration configuration, TSwizzler swizzler, Image<TPixel> source, Rectangle sourceRectangle)
: base(configuration, source, sourceRectangle)
{
this.swizzler = swizzler;
}
/// <inheritdoc/>
protected override void OnFrameApply(ImageFrame<TPixel> source)
{
for (int y = 0; y < source.Height; y++)
{
var pixelRowSpan = source.GetPixelRowSpan(y);
for (int x = 0; x < source.Width; x++)
{
var newPoint = this.swizzler.Transform(new Point(x, y));
}
}
}
}
}

32
src/ImageSharp/Processing/Processors/Transforms/SwizzleProcessor{TSwizzler}.cs

@ -0,0 +1,32 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Processing.Processors.Transforms
{
/// <summary>
/// Defines a swizzle operation on an image.
/// </summary>
/// <typeparam name="TSwizzler">The swizzle function type.</typeparam>
public sealed class SwizzleProcessor<TSwizzler> : IImageProcessor
where TSwizzler : struct, ISwizzler
{
/// <summary>
/// Initializes a new instance of the <see cref="SwizzleProcessor{TSwizzler}"/> class.
/// </summary>
/// <param name="swizzler">The swizzler operation.</param>
public SwizzleProcessor(TSwizzler swizzler)
=> this.Swizzler = swizzler;
/// <summary>
/// Gets the swizzler operation.
/// </summary>
public TSwizzler Swizzler { get; }
/// <inheritdoc />
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Configuration configuration, Image<TPixel> source, Rectangle sourceRectangle)
where TPixel : unmanaged, IPixel<TPixel>
=> new SwizzleProcessor<TSwizzler, TPixel>(configuration, this, source, sourceRectangle);
}
}

31
tests/ImageSharp.Tests/Processing/Transforms/SwizzleTests.cs

@ -0,0 +1,31 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.Processing.Extensions.Transforms;
using SixLabors.ImageSharp.Processing.Processors.Transforms;
using Xunit;
namespace SixLabors.ImageSharp.Tests.Processing.Transforms
{
public class SwizzleTests : BaseImageOperationsExtensionTest
{
private struct InvertXAndYSwizzler : ISwizzler
{
public Point Transform(Point point) => new Point(point.Y, point.X);
}
[Fact]
public void RotateDegreesFloatRotateProcessorWithAnglesSet()
{
this.operations.Swizzle(default(InvertXAndYSwizzler));
SwizzleProcessor<InvertXAndYSwizzler> processor = this.Verify<SwizzleProcessor<InvertXAndYSwizzler>>();
// assert that pixels have been changed
this.operations.Swizzle(default(InvertXAndYSwizzler));
SwizzleProcessor<InvertXAndYSwizzler> processor2 = this.Verify<SwizzleProcessor<InvertXAndYSwizzler>>();
// assert that pixels have been changed (i.e. back to original)
}
}
}
Loading…
Cancel
Save