mirror of https://github.com/SixLabors/ImageSharp
4 changed files with 379 additions and 312 deletions
@ -1,95 +1,101 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
// Copyright (c) Six Labors and contributors.
|
||||
// Licensed under the Apache License, Version 2.0.
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
using System; |
using System; |
||||
using System.Buffers; |
using System.Buffers; |
||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
using SixLabors.ImageSharp.Advanced; |
using SixLabors.ImageSharp.Advanced; |
||||
using SixLabors.ImageSharp.Memory; |
using SixLabors.ImageSharp.Memory; |
||||
using SixLabors.ImageSharp.PixelFormats; |
using SixLabors.ImageSharp.ParallelUtils; |
||||
using SixLabors.Memory; |
using SixLabors.ImageSharp.PixelFormats; |
||||
using SixLabors.Primitives; |
using SixLabors.Memory; |
||||
|
using SixLabors.Primitives; |
||||
namespace SixLabors.ImageSharp.Processing.Processors.Drawing |
|
||||
{ |
namespace SixLabors.ImageSharp.Processing.Processors.Drawing |
||||
/// <summary>
|
{ |
||||
/// Combines two images together by blending the pixels.
|
/// <summary>
|
||||
/// </summary>
|
/// Combines two images together by blending the pixels.
|
||||
|
/// </summary>
|
||||
/// <typeparam name="TPixelDst">The pixel format of destination image.</typeparam>
|
/// <typeparam name="TPixelDst">The pixel format of destination image.</typeparam>
|
||||
/// <typeparam name="TPixelSrc">The pixel format os source image.</typeparam>
|
/// <typeparam name="TPixelSrc">The pixel format os source image.</typeparam>
|
||||
internal class DrawImageProcessor<TPixelDst, TPixelSrc> : ImageProcessor<TPixelDst> |
internal class DrawImageProcessor<TPixelDst, TPixelSrc> : ImageProcessor<TPixelDst> |
||||
where TPixelDst : struct, IPixel<TPixelDst> |
where TPixelDst : struct, IPixel<TPixelDst> |
||||
where TPixelSrc : struct, IPixel<TPixelSrc> |
where TPixelSrc : struct, IPixel<TPixelSrc> |
||||
{ |
{ |
||||
/// <summary>
|
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DrawImageProcessor{TPixelDst, TPixelSrc}"/> class.
|
/// Initializes a new instance of the <see cref="DrawImageProcessor{TPixelDst, TPixelSrc}"/> class.
|
||||
/// </summary>
|
/// </summary>
|
||||
/// <param name="image">The image to blend with the currently processing image.</param>
|
/// <param name="image">The image to blend with the currently processing image.</param>
|
||||
/// <param name="location">The location to draw the blended image.</param>
|
/// <param name="location">The location to draw the blended image.</param>
|
||||
/// <param name="colorBlendingMode">The blending mode to use when drawing the image.</param>
|
/// <param name="colorBlendingMode">The blending mode to use when drawing the image.</param>
|
||||
/// <param name="alphaCompositionMode">The Alpha blending mode to use when drawing the image.</param>
|
/// <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>
|
/// <param name="opacity">The opacity of the image to blend. Must be between 0 and 1.</param>
|
||||
public DrawImageProcessor(Image<TPixelSrc> image, Point location, PixelColorBlendingMode colorBlendingMode, PixelAlphaCompositionMode alphaCompositionMode, float opacity) |
public DrawImageProcessor(Image<TPixelSrc> image, Point location, PixelColorBlendingMode colorBlendingMode, PixelAlphaCompositionMode alphaCompositionMode, float opacity) |
||||
{ |
{ |
||||
Guard.MustBeBetweenOrEqualTo(opacity, 0, 1, nameof(opacity)); |
Guard.MustBeBetweenOrEqualTo(opacity, 0, 1, nameof(opacity)); |
||||
|
|
||||
this.Image = image; |
this.Image = image; |
||||
this.Opacity = opacity; |
this.Opacity = opacity; |
||||
this.Blender = PixelOperations<TPixelDst>.Instance.GetPixelBlender(colorBlendingMode, alphaCompositionMode); |
this.Blender = PixelOperations<TPixelDst>.Instance.GetPixelBlender(colorBlendingMode, alphaCompositionMode); |
||||
this.Location = location; |
this.Location = location; |
||||
} |
} |
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Gets the image to blend
|
/// Gets the image to blend
|
||||
/// </summary>
|
/// </summary>
|
||||
public Image<TPixelSrc> Image { get; } |
public Image<TPixelSrc> Image { get; } |
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Gets the opacity of the image to blend
|
/// Gets the opacity of the image to blend
|
||||
/// </summary>
|
/// </summary>
|
||||
public float Opacity { get; } |
public float Opacity { get; } |
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Gets the pixel blender
|
/// Gets the pixel blender
|
||||
/// </summary>
|
/// </summary>
|
||||
public PixelBlender<TPixelDst> Blender { get; } |
public PixelBlender<TPixelDst> Blender { get; } |
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Gets the location to draw the blended image
|
/// Gets the location to draw the blended image
|
||||
/// </summary>
|
/// </summary>
|
||||
public Point Location { get; } |
public Point Location { get; } |
||||
|
|
||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||
protected override void OnFrameApply(ImageFrame<TPixelDst> source, Rectangle sourceRectangle, Configuration configuration) |
protected override void OnFrameApply(ImageFrame<TPixelDst> source, Rectangle sourceRectangle, Configuration configuration) |
||||
{ |
{ |
||||
Image<TPixelSrc> targetImage = this.Image; |
Image<TPixelSrc> targetImage = this.Image; |
||||
PixelBlender<TPixelDst> blender = this.Blender; |
PixelBlender<TPixelDst> blender = this.Blender; |
||||
int locationY = this.Location.Y; |
int locationY = this.Location.Y; |
||||
|
|
||||
// Align start/end positions.
|
// Align start/end positions.
|
||||
Rectangle bounds = targetImage.Bounds(); |
Rectangle bounds = targetImage.Bounds(); |
||||
|
|
||||
int minX = Math.Max(this.Location.X, sourceRectangle.X); |
int minX = Math.Max(this.Location.X, sourceRectangle.X); |
||||
int maxX = Math.Min(this.Location.X + bounds.Width, sourceRectangle.Width); |
int maxX = Math.Min(this.Location.X + bounds.Width, sourceRectangle.Width); |
||||
int targetX = minX - this.Location.X; |
int targetX = minX - this.Location.X; |
||||
|
|
||||
int minY = Math.Max(this.Location.Y, sourceRectangle.Y); |
int minY = Math.Max(this.Location.Y, sourceRectangle.Y); |
||||
int maxY = Math.Min(this.Location.Y + bounds.Height, sourceRectangle.Bottom); |
int maxY = Math.Min(this.Location.Y + bounds.Height, sourceRectangle.Bottom); |
||||
|
|
||||
int width = maxX - minX; |
int width = maxX - minX; |
||||
|
|
||||
MemoryAllocator memoryAllocator = this.Image.GetConfiguration().MemoryAllocator; |
MemoryAllocator memoryAllocator = this.Image.GetConfiguration().MemoryAllocator; |
||||
|
|
||||
ParallelFor.WithConfiguration( |
var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY); |
||||
minY, |
|
||||
maxY, |
ParallelHelper.IterateRows( |
||||
configuration, |
workingRect, |
||||
y => |
configuration, |
||||
{ |
rows => |
||||
Span<TPixelDst> background = source.GetPixelRowSpan(y).Slice(minX, width); |
{ |
||||
Span<TPixelSrc> foreground = targetImage.GetPixelRowSpan(y - locationY).Slice(targetX, width); |
for (int y = rows.Min; y < rows.Max; y++) |
||||
blender.Blend<TPixelSrc>(memoryAllocator, background, background, foreground, this.Opacity); |
{ |
||||
}); |
Span<TPixelDst> background = source.GetPixelRowSpan(y).Slice(minX, width); |
||||
} |
Span<TPixelSrc> foreground = |
||||
} |
targetImage.GetPixelRowSpan(y - locationY).Slice(targetX, width); |
||||
|
blender.Blend<TPixelSrc>(memoryAllocator, background, background, foreground, this.Opacity); |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
} |
} |
||||
Loading…
Reference in new issue