Browse Source

started the refactor

af/merge-core
Anton Firszov 7 years ago
parent
commit
90c0772d96
  1. 19
      src/ImageSharp.Drawing/Processing/DrawingHelpers.cs
  2. 9
      src/ImageSharp.Drawing/Processing/IBrush.cs
  3. 16
      src/ImageSharp.Drawing/Processing/IPen.cs
  4. 30
      src/ImageSharp.Drawing/Processing/PatternBrush{TPixel}.cs
  5. 18
      src/ImageSharp.Drawing/Processing/SolidBrush{TPixel}.cs
  6. 3
      src/ImageSharp/Color/Color.Conversions.cs

19
src/ImageSharp.Drawing/Processing/DrawingHelpers.cs

@ -0,0 +1,19 @@
// // Copyright (c) Six Labors and contributors.
// // Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Primitives;
namespace SixLabors.ImageSharp.Processing
{
internal static class DrawingHelpers
{
public static DenseMatrix<TPixel> ToPixelMatrix<TPixel>(this DenseMatrix<Color> colorMatrix, Configuration configuration)
where TPixel : struct, IPixel<TPixel>
{
DenseMatrix<TPixel> result = new DenseMatrix<TPixel>(colorMatrix.Columns, colorMatrix.Rows);
Color.ToPixel(configuration, colorMatrix.Span, result.Span);
return result;
}
}
}

9
src/ImageSharp.Drawing/Processing/IBrush.cs

@ -14,8 +14,7 @@ namespace SixLabors.ImageSharp.Processing
/// A brush is a simple class that will return an <see cref="BrushApplicator{TPixel}" /> that will perform the
/// logic for converting a pixel location to a <typeparamref name="TPixel"/>.
/// </remarks>
public interface IBrush<TPixel>
where TPixel : struct, IPixel<TPixel>
public interface IBrush
{
/// <summary>
/// Creates the applicator for this brush.
@ -30,6 +29,10 @@ namespace SixLabors.ImageSharp.Processing
/// The <paramref name="region" /> when being applied to things like shapes would usually be the
/// bounding box of the shape not necessarily the bounds of the whole image
/// </remarks>
BrushApplicator<TPixel> CreateApplicator(ImageFrame<TPixel> source, RectangleF region, GraphicsOptions options);
BrushApplicator<TPixel> CreateApplicator<TPixel>(
ImageFrame<TPixel> source,
RectangleF region,
GraphicsOptions options)
where TPixel : struct, IPixel<TPixel>;
}
}

16
src/ImageSharp.Drawing/Processing/IPen.cs

@ -7,23 +7,15 @@ using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Processing
{
/// <summary>
/// Interface representing a Pen
/// Interface representing the pattern and size of the stroke to apply with a Pen.
/// </summary>
/// <typeparam name="TPixel">The type of the color.</typeparam>
public interface IPen<TPixel> : IPen
where TPixel : struct, IPixel<TPixel>
public interface IPen
{
/// <summary>
/// Gets the stroke fill.
/// </summary>
IBrush<TPixel> StrokeFill { get; }
}
/// <summary>
/// Interface representing the pattern and size of the stroke to apply with a Pen.
/// </summary>
public interface IPen
{
IBrush StrokeFill { get; }
/// <summary>
/// Gets the width to apply to the stroke
/// </summary>

30
src/ImageSharp.Drawing/Processing/PatternBrush{TPixel}.cs

@ -35,13 +35,12 @@ namespace SixLabors.ImageSharp.Processing
/// </para>
/// </remarks>
/// <typeparam name="TPixel">The pixel format.</typeparam>
public class PatternBrush<TPixel> : IBrush<TPixel>
where TPixel : struct, IPixel<TPixel>
public class PatternBrush : IBrush
{
/// <summary>
/// The pattern.
/// </summary>
private readonly DenseMatrix<TPixel> pattern;
private readonly DenseMatrix<Color> pattern;
private readonly DenseMatrix<Vector4> patternVector;
/// <summary>
@ -50,7 +49,7 @@ namespace SixLabors.ImageSharp.Processing
/// <param name="foreColor">Color of the fore.</param>
/// <param name="backColor">Color of the back.</param>
/// <param name="pattern">The pattern.</param>
public PatternBrush(TPixel foreColor, TPixel backColor, bool[,] pattern)
public PatternBrush(Color foreColor, Color backColor, bool[,] pattern)
: this(foreColor, backColor, new DenseMatrix<bool>(pattern))
{
}
@ -61,11 +60,11 @@ namespace SixLabors.ImageSharp.Processing
/// <param name="foreColor">Color of the fore.</param>
/// <param name="backColor">Color of the back.</param>
/// <param name="pattern">The pattern.</param>
internal PatternBrush(TPixel foreColor, TPixel backColor, in DenseMatrix<bool> pattern)
internal PatternBrush(Color foreColor, Color backColor, in DenseMatrix<bool> pattern)
{
var foreColorVector = foreColor.ToVector4();
var backColorVector = backColor.ToVector4();
this.pattern = new DenseMatrix<TPixel>(pattern.Columns, pattern.Rows);
this.pattern = new DenseMatrix<Color>(pattern.Columns, pattern.Rows);
this.patternVector = new DenseMatrix<Vector4>(pattern.Columns, pattern.Rows);
for (int i = 0; i < pattern.Data.Length; i++)
{
@ -86,19 +85,25 @@ namespace SixLabors.ImageSharp.Processing
/// Initializes a new instance of the <see cref="PatternBrush{TPixel}"/> class.
/// </summary>
/// <param name="brush">The brush.</param>
internal PatternBrush(PatternBrush<TPixel> brush)
internal PatternBrush(PatternBrush<Color> brush)
{
this.pattern = brush.pattern;
this.patternVector = brush.patternVector;
}
/// <inheritdoc />
public BrushApplicator<TPixel> CreateApplicator(ImageFrame<TPixel> source, RectangleF region, GraphicsOptions options) => new PatternBrushApplicator(source, this.pattern, this.patternVector, options);
public BrushApplicator<TPixel> CreateApplicator<TPixel>(
ImageFrame<TPixel> source,
RectangleF region,
GraphicsOptions options)
where TPixel : struct, IPixel<TPixel> =>
new PatternBrushApplicator<TPixel>(source, this.pattern, this.patternVector, options);
/// <summary>
/// The pattern brush applicator.
/// </summary>
private class PatternBrushApplicator : BrushApplicator<TPixel>
private class PatternBrushApplicator<TPixel> : BrushApplicator<TPixel>
where TPixel : struct, IPixel<TPixel>
{
/// <summary>
/// The pattern.
@ -107,16 +112,17 @@ namespace SixLabors.ImageSharp.Processing
private readonly DenseMatrix<Vector4> patternVector;
/// <summary>
/// Initializes a new instance of the <see cref="PatternBrushApplicator" /> class.
/// Initializes a new instance of the <see cref="PatternBrushApplicator{TPixel}" /> class.
/// </summary>
/// <param name="source">The source image.</param>
/// <param name="pattern">The pattern.</param>
/// <param name="patternVector">The patternVector.</param>
/// <param name="options">The options</param>
public PatternBrushApplicator(ImageFrame<TPixel> source, in DenseMatrix<TPixel> pattern, DenseMatrix<Vector4> patternVector, GraphicsOptions options)
public PatternBrushApplicator(ImageFrame<TPixel> source, in DenseMatrix<Color> pattern, DenseMatrix<Vector4> patternVector, GraphicsOptions options)
: base(source, options)
{
this.pattern = pattern;
this.pattern = new DenseMatrix<TPixel>(pattern.Columns, pattern.Rows);
Color.ToPixel<TPixel>(source.Configuration, pattern.Data, this.pattern.Data);
this.patternVector = patternVector;
}

18
src/ImageSharp.Drawing/Processing/SolidBrush{TPixel}.cs

@ -15,20 +15,18 @@ namespace SixLabors.ImageSharp.Processing
/// <summary>
/// Provides an implementation of a solid brush for painting solid color areas.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
public class SolidBrush<TPixel> : IBrush<TPixel>
where TPixel : struct, IPixel<TPixel>
public class SolidBrush : IBrush
{
/// <summary>
/// The color to paint.
/// </summary>
private readonly TPixel color;
private readonly Color color;
/// <summary>
/// Initializes a new instance of the <see cref="SolidBrush{TPixel}"/> class.
/// </summary>
/// <param name="color">The color.</param>
public SolidBrush(TPixel color)
public SolidBrush(Color color)
{
this.color = color;
}
@ -39,18 +37,20 @@ namespace SixLabors.ImageSharp.Processing
/// <value>
/// The color.
/// </value>
public TPixel Color => this.color;
public Color Color => this.color;
/// <inheritdoc />
public BrushApplicator<TPixel> CreateApplicator(ImageFrame<TPixel> source, RectangleF region, GraphicsOptions options)
public BrushApplicator<TPixel> CreateApplicator<TPixel>(ImageFrame<TPixel> source, RectangleF region, GraphicsOptions options)
where TPixel : struct, IPixel<TPixel>
{
return new SolidBrushApplicator(source, this.color, options);
return new SolidBrushApplicator<TPixel>(source, this.color.ToPixel<TPixel>(), options);
}
/// <summary>
/// The solid brush applicator.
/// </summary>
private class SolidBrushApplicator : BrushApplicator<TPixel>
private class SolidBrushApplicator<TPixel> : BrushApplicator<TPixel>
where TPixel : struct, IPixel<TPixel>
{
/// <summary>
/// Initializes a new instance of the <see cref="SolidBrushApplicator"/> class.

3
src/ImageSharp/Color/Color.Conversions.cs

@ -91,5 +91,8 @@ namespace SixLabors.ImageSharp
[MethodImpl(InliningOptions.ShortMethod)]
internal Bgr24 ToBgr24() => this.data.ToBgr24();
[MethodImpl(InliningOptions.ShortMethod)]
internal Vector4 ToVector4() => this.data.ToVector4();
}
}
Loading…
Cancel
Save