mirror of https://github.com/SixLabors/ImageSharp
2 changed files with 181 additions and 181 deletions
@ -1,105 +1,105 @@ |
|||||
// <copyright file="DrawImage.cs" company="James Jackson-South">
|
// <copyright file="DrawImage.cs" company="James Jackson-South">
|
||||
// Copyright (c) James Jackson-South and contributors.
|
// Copyright (c) James Jackson-South and contributors.
|
||||
// Licensed under the Apache License, Version 2.0.
|
// Licensed under the Apache License, Version 2.0.
|
||||
// </copyright>
|
// </copyright>
|
||||
|
|
||||
namespace ImageSharp |
namespace ImageSharp |
||||
{ |
{ |
||||
using System; |
using System; |
||||
using Drawing.Processors; |
using Drawing.Processors; |
||||
using ImageSharp.PixelFormats; |
using ImageSharp.PixelFormats; |
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Extension methods for the <see cref="Image"/> type.
|
/// Extension methods for the <see cref="Image"/> type.
|
||||
/// </summary>
|
/// </summary>
|
||||
public static partial class ImageExtensions |
public static partial class ImageExtensions |
||||
{ |
{ |
||||
/// <summary>
|
/// <summary>
|
||||
/// Draws the given image together with the current one by blending their pixels.
|
/// Draws the given image together with the current one by blending their pixels.
|
||||
/// </summary>
|
/// </summary>
|
||||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
||||
/// <param name="source">The image this method extends.</param>
|
/// <param name="source">The image this method extends.</param>
|
||||
/// <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="percent">The opacity of the image image to blend. Must be between 0 and 100.</param>
|
/// <param name="percent">The opacity of the image image to blend. Must be between 0 and 100.</param>
|
||||
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
|
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
|
||||
public static Image<TPixel> Blend<TPixel>(this Image<TPixel> source, Image<TPixel> image, int percent = 50) |
public static Image<TPixel> Blend<TPixel>(this Image<TPixel> source, Image<TPixel> image, int percent = 50) |
||||
where TPixel : struct, IPixel<TPixel> |
where TPixel : struct, IPixel<TPixel> |
||||
{ |
{ |
||||
return DrawImage(source, image, percent, default(Size), default(Point)); |
return DrawImage(source, image, percent, default(Size), default(Point)); |
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Draws the given image together with the current one by blending their pixels.
|
|
||||
/// </summary>
|
|
||||
/// <param name="source">The image this method extends.</param>
|
|
||||
/// <param name="image">The image to blend with the currently processing image.</param>
|
|
||||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
||||
/// <param name="percent">The opacity of the image image to blend. Must be between 0 and 100.</param>
|
|
||||
/// <param name="size">The size to draw the blended image.</param>
|
|
||||
/// <param name="location">The location to draw the blended image.</param>
|
|
||||
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
|
|
||||
public static Image<TPixel> DrawImage<TPixel>(this Image<TPixel> source, Image<TPixel> image, int percent, Size size, Point location) |
|
||||
where TPixel : struct, IPixel<TPixel> |
|
||||
{ |
|
||||
if (size == default(Size)) |
|
||||
{ |
|
||||
size = new Size(image.Width, image.Height); |
|
||||
} |
|
||||
|
|
||||
if (location == default(Point)) |
|
||||
{ |
|
||||
location = Point.Empty; |
|
||||
} |
|
||||
|
|
||||
source.ApplyProcessor(new DrawImageProcessor<TPixel>(image, size, location, percent), source.Bounds); |
|
||||
return source; |
|
||||
} |
} |
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Draws the given image together with the current one by blending their pixels.
|
/// Draws the given image together with the current one by blending their pixels.
|
||||
/// </summary>
|
/// </summary>
|
||||
/// <param name="source">The image this method extends.</param>
|
/// <param name="source">The image this method extends.</param>
|
||||
/// <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="mode">Pixel function effect to apply on every pixel</param>
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
||||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
/// <param name="percent">The opacity of the image image to blend. Must be between 0 and 100.</param>
|
||||
/// <param name="percent">The opacity of the image image to blend. Must be between 0 and 100.</param>
|
/// <param name="size">The size to draw the blended image.</param>
|
||||
/// <param name="size">The size to draw the blended image.</param>
|
/// <param name="location">The location to draw the blended image.</param>
|
||||
/// <param name="location">The location to draw the blended image.</param>
|
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
|
||||
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
|
public static Image<TPixel> DrawImage<TPixel>(this Image<TPixel> source, Image<TPixel> image, int percent, Size size, Point location) |
||||
public static Image<TPixel> DrawImage<TPixel>(this Image<TPixel> source, Image<TPixel> image, PixelTransformMode mode, int percent, Size size, Point location) |
where TPixel : struct, IPixel<TPixel> |
||||
where TPixel : struct, IPixel<TPixel> |
{ |
||||
{ |
if (size == default(Size)) |
||||
|
{ |
||||
|
size = new Size(image.Width, image.Height); |
||||
|
} |
||||
|
|
||||
|
if (location == default(Point)) |
||||
|
{ |
||||
|
location = Point.Empty; |
||||
|
} |
||||
|
|
||||
|
source.ApplyProcessor(new DrawImageProcessor<TPixel>(image, size, location, percent), source.Bounds); |
||||
|
return source; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Draws the given image together with the current one by blending their pixels.
|
||||
|
/// </summary>
|
||||
|
/// <param name="source">The image this method extends.</param>
|
||||
|
/// <param name="image">The image to blend with the currently processing image.</param>
|
||||
|
/// <param name="mode">Pixel function effect to apply on every pixel</param>
|
||||
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
||||
|
/// <param name="percent">The opacity of the image image to blend. Must be between 0 and 100.</param>
|
||||
|
/// <param name="size">The size to draw the blended image.</param>
|
||||
|
/// <param name="location">The location to draw the blended image.</param>
|
||||
|
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
|
||||
|
public static Image<TPixel> DrawImage<TPixel>(this Image<TPixel> source, Image<TPixel> image, PixelTransformMode mode, int percent, Size size, Point location) |
||||
|
where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
Func<TPixel, TPixel, float, TPixel> pixelFunc = mode.GetPixelFunction<TPixel>(); |
Func<TPixel, TPixel, float, TPixel> pixelFunc = mode.GetPixelFunction<TPixel>(); |
||||
|
|
||||
return DrawImage(source, image, pixelFunc, percent, size, location); |
return DrawImage(source, image, pixelFunc, percent, size, location); |
||||
} |
} |
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Draws the given image together with the current one by blending their pixels.
|
/// Draws the given image together with the current one by blending their pixels.
|
||||
/// </summary>
|
/// </summary>
|
||||
/// <param name="source">The image this method extends.</param>
|
/// <param name="source">The image this method extends.</param>
|
||||
/// <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="pixelFunc">Pixel function effect to apply on every pixel</param>
|
/// <param name="pixelFunc">Pixel function effect to apply on every pixel</param>
|
||||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
||||
/// <param name="percent">The opacity of the image image to blend. Must be between 0 and 100.</param>
|
/// <param name="percent">The opacity of the image image to blend. Must be between 0 and 100.</param>
|
||||
/// <param name="size">The size to draw the blended image.</param>
|
/// <param name="size">The size to draw the blended image.</param>
|
||||
/// <param name="location">The location to draw the blended image.</param>
|
/// <param name="location">The location to draw the blended image.</param>
|
||||
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
|
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
|
||||
public static Image<TPixel> DrawImage<TPixel>(this Image<TPixel> source, Image<TPixel> image, Func<TPixel, TPixel, float, TPixel> pixelFunc, int percent, Size size, Point location) |
public static Image<TPixel> DrawImage<TPixel>(this Image<TPixel> source, Image<TPixel> image, Func<TPixel, TPixel, float, TPixel> pixelFunc, int percent, Size size, Point location) |
||||
where TPixel : struct, IPixel<TPixel> |
where TPixel : struct, IPixel<TPixel> |
||||
{ |
{ |
||||
if (size == default(Size)) |
if (size == default(Size)) |
||||
{ |
{ |
||||
size = new Size(image.Width, image.Height); |
size = new Size(image.Width, image.Height); |
||||
} |
} |
||||
|
|
||||
if (location == default(Point)) |
if (location == default(Point)) |
||||
{ |
{ |
||||
location = Point.Empty; |
location = Point.Empty; |
||||
} |
} |
||||
|
|
||||
source.ApplyProcessor(new DrawImageEffectProcessor<TPixel>(image, size, location, pixelFunc, percent), source.Bounds); |
source.ApplyProcessor(new DrawImageEffectProcessor<TPixel>(image, size, location, pixelFunc, percent), source.Bounds); |
||||
return source; |
return source; |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
@ -1,102 +1,102 @@ |
|||||
// <copyright file="DrawImageProcessor.cs" company="James Jackson-South">
|
// <copyright file="DrawImageEffectProcessor.cs" company="James Jackson-South">
|
||||
// Copyright (c) James Jackson-South and contributors.
|
// Copyright (c) James Jackson-South and contributors.
|
||||
// Licensed under the Apache License, Version 2.0.
|
// Licensed under the Apache License, Version 2.0.
|
||||
// </copyright>
|
// </copyright>
|
||||
|
|
||||
namespace ImageSharp.Drawing.Processors |
namespace ImageSharp.Drawing.Processors |
||||
{ |
{ |
||||
using System; |
using System; |
||||
using System.Numerics; |
using System.Numerics; |
||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
using ImageSharp.PixelFormats; |
using ImageSharp.PixelFormats; |
||||
using ImageSharp.Processing; |
using ImageSharp.Processing; |
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Combines two images together by blending the pixels.
|
/// Combines two images together by blending the pixels.
|
||||
/// </summary>
|
/// </summary>
|
||||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
||||
internal class DrawImageEffectProcessor<TPixel> : ImageProcessor<TPixel> |
internal class DrawImageEffectProcessor<TPixel> : ImageProcessor<TPixel> |
||||
where TPixel : struct, IPixel<TPixel> |
where TPixel : struct, IPixel<TPixel> |
||||
{ |
{ |
||||
/// <summary>
|
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DrawImageEffectProcessor{TPixel}"/> class.
|
/// Initializes a new instance of the <see cref="DrawImageEffectProcessor{TPixel}"/> 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="size">The size to draw the blended image.</param>
|
/// <param name="size">The size to draw the blended 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="pixelFunction">Pixel function effect to apply on every pixel</param>
|
/// <param name="pixelFunction">Pixel function effect to apply on every pixel</param>
|
||||
/// <param name="alpha">The opacity of the image to blend. Between 0 and 100.</param>
|
/// <param name="alpha">The opacity of the image to blend. Between 0 and 100.</param>
|
||||
public DrawImageEffectProcessor(Image<TPixel> image, Size size, Point location, Func<TPixel, TPixel, float, TPixel> pixelFunction, int alpha = 100) |
public DrawImageEffectProcessor(Image<TPixel> image, Size size, Point location, Func<TPixel, TPixel, float, TPixel> pixelFunction, int alpha = 100) |
||||
{ |
{ |
||||
Guard.MustBeBetweenOrEqualTo(alpha, 0, 100, nameof(alpha)); |
Guard.MustBeBetweenOrEqualTo(alpha, 0, 100, nameof(alpha)); |
||||
this.Image = image; |
this.Image = image; |
||||
this.PixelFunction = pixelFunction; |
this.PixelFunction = pixelFunction; |
||||
this.Size = size; |
this.Size = size; |
||||
this.Location = location; |
this.Location = location; |
||||
this.Alpha = alpha; |
this.Alpha = alpha; |
||||
} |
} |
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Gets the image to blend.
|
/// Gets the image to blend.
|
||||
/// </summary>
|
/// </summary>
|
||||
public Image<TPixel> Image { get; private set; } |
public Image<TPixel> Image { get; private set; } |
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Gets The function effect to apply on a per pixel basis
|
/// Gets The function effect to apply on a per pixel basis
|
||||
/// </summary>
|
/// </summary>
|
||||
public Func<TPixel, TPixel, float, TPixel> PixelFunction { get; private set; } |
public Func<TPixel, TPixel, float, TPixel> PixelFunction { get; private set; } |
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Gets the alpha percentage value.
|
/// Gets the alpha percentage value.
|
||||
/// </summary>
|
/// </summary>
|
||||
public int Alpha { get; } |
public int Alpha { get; } |
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Gets the size to draw the blended image.
|
/// Gets the size to draw the blended image.
|
||||
/// </summary>
|
/// </summary>
|
||||
public Size Size { get; } |
public Size Size { 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 OnApply(ImageBase<TPixel> target, Rectangle sourceRectangle) |
protected override void OnApply(ImageBase<TPixel> target, Rectangle sourceRectangle) |
||||
{ |
{ |
||||
if (this.Image.Bounds.Size != this.Size) |
if (this.Image.Bounds.Size != this.Size) |
||||
{ |
{ |
||||
// should Resize be moved to core?
|
// should Resize be moved to core?
|
||||
this.Image = this.Image.Resize(this.Size.Width, this.Size.Height); |
this.Image = this.Image.Resize(this.Size.Width, this.Size.Height); |
||||
} |
} |
||||
|
|
||||
// Align start/end positions.
|
// Align start/end positions.
|
||||
Rectangle bounds = this.Image.Bounds; |
Rectangle bounds = this.Image.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 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); |
||||
|
|
||||
float alpha = this.Alpha / 100F; |
float alpha = this.Alpha / 100F; |
||||
|
|
||||
using (PixelAccessor<TPixel> sourcePixels = this.Image.Lock()) |
using (PixelAccessor<TPixel> sourcePixels = this.Image.Lock()) |
||||
using (PixelAccessor<TPixel> targetPixels = target.Lock()) |
using (PixelAccessor<TPixel> targetPixels = target.Lock()) |
||||
{ |
{ |
||||
Parallel.For( |
Parallel.For( |
||||
minY, |
minY, |
||||
maxY, |
maxY, |
||||
this.ParallelOptions, |
this.ParallelOptions, |
||||
y => |
y => |
||||
{ |
{ |
||||
for (int x = minX; x < maxX; x++) |
for (int x = minX; x < maxX; x++) |
||||
{ |
{ |
||||
TPixel targetColor = targetPixels[x, y]; |
TPixel targetColor = targetPixels[x, y]; |
||||
TPixel sourceColor = sourcePixels[x - minX, y - minY]; |
TPixel sourceColor = sourcePixels[x - minX, y - minY]; |
||||
|
|
||||
targetPixels[x, y] = this.PixelFunction(targetColor, sourceColor, alpha); |
targetPixels[x, y] = this.PixelFunction(targetColor, sourceColor, alpha); |
||||
} |
} |
||||
}); |
}); |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
Loading…
Reference in new issue