Skip to content

Commit

Permalink
Merge pull request #1070 from SixLabors/js/custom-configuration-fix-650
Browse files Browse the repository at this point in the history
Allow passing custom Configuration instances to processors
  • Loading branch information
JimBobSquarePants committed Jan 7, 2020
2 parents a41ba70 + a5ab02c commit 92f3ada
Show file tree
Hide file tree
Showing 111 changed files with 450 additions and 362 deletions.
Expand Up @@ -60,23 +60,25 @@ public class DrawImageProcessor : IImageProcessor
public float Opacity { get; }

/// <inheritdoc />
public IImageProcessor<TPixelBg> CreatePixelSpecificProcessor<TPixelBg>(Image<TPixelBg> source, Rectangle sourceRectangle)
public IImageProcessor<TPixelBg> CreatePixelSpecificProcessor<TPixelBg>(Configuration configuration, Image<TPixelBg> source, Rectangle sourceRectangle)
where TPixelBg : struct, IPixel<TPixelBg>
{
var visitor = new ProcessorFactoryVisitor<TPixelBg>(this, source, sourceRectangle);
var visitor = new ProcessorFactoryVisitor<TPixelBg>(configuration, this, source, sourceRectangle);
this.Image.AcceptVisitor(visitor);
return visitor.Result;
}

private class ProcessorFactoryVisitor<TPixelBg> : IImageVisitor
where TPixelBg : struct, IPixel<TPixelBg>
{
private readonly Configuration configuration;
private readonly DrawImageProcessor definition;
private readonly Image<TPixelBg> source;
private readonly Rectangle sourceRectangle;

public ProcessorFactoryVisitor(DrawImageProcessor definition, Image<TPixelBg> source, Rectangle sourceRectangle)
public ProcessorFactoryVisitor(Configuration configuration, DrawImageProcessor definition, Image<TPixelBg> source, Rectangle sourceRectangle)
{
this.configuration = configuration;
this.definition = definition;
this.source = source;
this.sourceRectangle = sourceRectangle;
Expand All @@ -88,6 +90,7 @@ public void Visit<TPixelFg>(Image<TPixelFg> image)
where TPixelFg : struct, IPixel<TPixelFg>
{
this.Result = new DrawImageProcessor<TPixelBg, TPixelFg>(
this.configuration,
image,
this.source,
this.sourceRectangle,
Expand Down
Expand Up @@ -22,6 +22,7 @@ internal class DrawImageProcessor<TPixelBg, TPixelFg> : ImageProcessor<TPixelBg>
/// <summary>
/// Initializes a new instance of the <see cref="DrawImageProcessor{TPixelBg, TPixelFg}"/> class.
/// </summary>
/// <param name="configuration">The configuration which allows altering default behaviour or extending the library.</param>
/// <param name="image">The foreground <see cref="Image{TPixelFg}"/> to blend with the currently processing image.</param>
/// <param name="source">The source <see cref="Image{TPixelBg}"/> for the current processor instance.</param>
/// <param name="sourceRectangle">The source area to process for the current processor instance.</param>
Expand All @@ -30,14 +31,15 @@ internal class DrawImageProcessor<TPixelBg, TPixelFg> : ImageProcessor<TPixelBg>
/// <param name="alphaCompositionMode">The Alpha blending mode to use when drawing the image.</param>
/// <param name="opacity">The opacity of the image to blend. Must be between 0 and 1.</param>
public DrawImageProcessor(
Configuration configuration,
Image<TPixelFg> image,
Image<TPixelBg> source,
Rectangle sourceRectangle,
Point location,
PixelColorBlendingMode colorBlendingMode,
PixelAlphaCompositionMode alphaCompositionMode,
float opacity)
: base(source, sourceRectangle)
: base(configuration, source, sourceRectangle)
{
Guard.MustBeBetweenOrEqualTo(opacity, 0, 1, nameof(opacity));

Expand Down
Expand Up @@ -34,10 +34,8 @@ public FillProcessor(GraphicsOptions options, IBrush brush)
public GraphicsOptions Options { get; }

/// <inheritdoc />
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Image<TPixel> source, Rectangle sourceRectangle)
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Configuration configuration, Image<TPixel> source, Rectangle sourceRectangle)
where TPixel : struct, IPixel<TPixel>
{
return new FillProcessor<TPixel>(this, source, sourceRectangle);
}
=> new FillProcessor<TPixel>(configuration, this, source, sourceRectangle);
}
}
Expand Up @@ -6,7 +6,6 @@

using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Advanced.ParallelUtils;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;

Expand All @@ -21,8 +20,8 @@ internal class FillProcessor<TPixel> : ImageProcessor<TPixel>
{
private readonly FillProcessor definition;

public FillProcessor(FillProcessor definition, Image<TPixel> source, Rectangle sourceRectangle)
: base(source, sourceRectangle)
public FillProcessor(Configuration configuration, FillProcessor definition, Image<TPixel> source, Rectangle sourceRectangle)
: base(configuration, source, sourceRectangle)
{
this.definition = definition;
}
Expand Down Expand Up @@ -112,7 +111,7 @@ private bool IsSolidBrushWithoutBlending(out SolidBrush solidBrush)
{
solidBrush = this.definition.Brush as SolidBrush;

if (solidBrush == null)
if (solidBrush is null)
{
return false;
}
Expand Down
Expand Up @@ -42,10 +42,8 @@ public FillRegionProcessor(GraphicsOptions options, IBrush brush, Region region)
public GraphicsOptions Options { get; }

/// <inheritdoc />
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Image<TPixel> source, Rectangle sourceRectangle)
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Configuration configuration, Image<TPixel> source, Rectangle sourceRectangle)
where TPixel : struct, IPixel<TPixel>
{
return new FillRegionProcessor<TPixel>(this, source, sourceRectangle);
}
=> new FillRegionProcessor<TPixel>(configuration, this, source, sourceRectangle);
}
}
Expand Up @@ -23,8 +23,8 @@ internal class FillRegionProcessor<TPixel> : ImageProcessor<TPixel>
{
private readonly FillRegionProcessor definition;

public FillRegionProcessor(FillRegionProcessor definition, Image<TPixel> source, Rectangle sourceRectangle)
: base(source, sourceRectangle)
public FillRegionProcessor(Configuration configuration, FillRegionProcessor definition, Image<TPixel> source, Rectangle sourceRectangle)
: base(configuration, source, sourceRectangle)
{
this.definition = definition;
}
Expand Down
Expand Up @@ -72,10 +72,8 @@ public DrawTextProcessor(TextGraphicsOptions options, string text, Font font, IB
public PointF Location { get; }

/// <inheritdoc />
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Image<TPixel> source, Rectangle sourceRectangle)
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Configuration configuration, Image<TPixel> source, Rectangle sourceRectangle)
where TPixel : struct, IPixel<TPixel>
{
return new DrawTextProcessor<TPixel>(this, source, sourceRectangle);
}
=> new DrawTextProcessor<TPixel>(configuration, this, source, sourceRectangle);
}
}
Expand Up @@ -27,8 +27,8 @@ internal class DrawTextProcessor<TPixel> : ImageProcessor<TPixel>

private readonly DrawTextProcessor definition;

public DrawTextProcessor(DrawTextProcessor definition, Image<TPixel> source, Rectangle sourceRectangle)
: base(source, sourceRectangle)
public DrawTextProcessor(Configuration configuration, DrawTextProcessor definition, Image<TPixel> source, Rectangle sourceRectangle)
: base(configuration, source, sourceRectangle)
{
this.definition = definition;
}
Expand Down
@@ -1,10 +1,8 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Processors;
using SixLabors.Memory;
using SixLabors.Primitives;

namespace SixLabors.ImageSharp.Processing
Expand All @@ -23,10 +21,12 @@ internal class DefaultImageProcessorContext<TPixel> : IInternalImageProcessingCo
/// <summary>
/// Initializes a new instance of the <see cref="DefaultImageProcessorContext{TPixel}"/> class.
/// </summary>
/// <param name="source">The image.</param>
/// <param name="mutate">The mutate.</param>
public DefaultImageProcessorContext(Image<TPixel> source, bool mutate)
/// <param name="configuration">The configuration which allows altering default behaviour or extending the library.</param>
/// <param name="source">The source image.</param>
/// <param name="mutate">Whether to mutate the image.</param>
public DefaultImageProcessorContext(Configuration configuration, Image<TPixel> source, bool mutate)
{
this.Configuration = configuration;
this.mutate = mutate;
this.source = source;

Expand All @@ -38,7 +38,7 @@ public DefaultImageProcessorContext(Image<TPixel> source, bool mutate)
}

/// <inheritdoc/>
public MemoryAllocator MemoryAllocator => this.source.GetConfiguration().MemoryAllocator;
public Configuration Configuration { get; }

/// <inheritdoc/>
public Image<TPixel> GetResultImage()
Expand Down Expand Up @@ -71,7 +71,7 @@ public IImageProcessingContext ApplyProcessor(IImageProcessor processor, Rectang
// interim clone if the first processor in the pipeline is a cloning processor.
if (processor is ICloningImageProcessor cloningImageProcessor)
{
using (ICloningImageProcessor<TPixel> pixelProcessor = cloningImageProcessor.CreatePixelSpecificCloningProcessor(this.source, rectangle))
using (ICloningImageProcessor<TPixel> pixelProcessor = cloningImageProcessor.CreatePixelSpecificCloningProcessor(this.Configuration, this.source, rectangle))
{
this.destination = pixelProcessor.CloneAndExecute();
return this;
Expand All @@ -83,7 +83,7 @@ public IImageProcessingContext ApplyProcessor(IImageProcessor processor, Rectang
}

// Standard processing pipeline.
using (IImageProcessor<TPixel> specificProcessor = processor.CreatePixelSpecificProcessor(this.destination, rectangle))
using (IImageProcessor<TPixel> specificProcessor = processor.CreatePixelSpecificProcessor(this.Configuration, this.destination, rectangle))
{
specificProcessor.Execute();
}
Expand Down

0 comments on commit 92f3ada

Please sign in to comment.