mirror of https://github.com/SixLabors/ImageSharp
30 changed files with 652 additions and 508 deletions
@ -0,0 +1,101 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
using System.Buffers; |
|||
|
|||
using SixLabors.ImageSharp.Advanced; |
|||
using SixLabors.ImageSharp.Memory; |
|||
using SixLabors.ImageSharp.ParallelUtils; |
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
using SixLabors.Primitives; |
|||
|
|||
namespace SixLabors.ImageSharp.Processing.Processors.Overlays |
|||
{ |
|||
/// <summary>
|
|||
/// Sets the background color of the image.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|||
internal class BackgroundColorProcessor<TPixel> : ImageProcessor<TPixel> |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
private readonly BackgroundColorProcessor definition; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="BackgroundColorProcessor{TPixel}"/> class.
|
|||
/// </summary>
|
|||
public BackgroundColorProcessor(BackgroundColorProcessor definition) |
|||
{ |
|||
this.definition = definition; |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
protected override void OnFrameApply( |
|||
ImageFrame<TPixel> source, |
|||
Rectangle sourceRectangle, |
|||
Configuration configuration) |
|||
{ |
|||
TPixel color = this.definition.Color.ToPixel<TPixel>(); |
|||
GraphicsOptions graphicsOptions = this.definition.GraphicsOptions; |
|||
|
|||
int startY = sourceRectangle.Y; |
|||
int endY = sourceRectangle.Bottom; |
|||
int startX = sourceRectangle.X; |
|||
int endX = sourceRectangle.Right; |
|||
|
|||
// Align start/end positions.
|
|||
int minX = Math.Max(0, startX); |
|||
int maxX = Math.Min(source.Width, endX); |
|||
int minY = Math.Max(0, startY); |
|||
int maxY = Math.Min(source.Height, endY); |
|||
|
|||
// Reset offset if necessary.
|
|||
if (minX > 0) |
|||
{ |
|||
startX = 0; |
|||
} |
|||
|
|||
if (minY > 0) |
|||
{ |
|||
startY = 0; |
|||
} |
|||
|
|||
int width = maxX - minX; |
|||
|
|||
var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY); |
|||
|
|||
using (IMemoryOwner<TPixel> colors = source.MemoryAllocator.Allocate<TPixel>(width)) |
|||
using (IMemoryOwner<float> amount = source.MemoryAllocator.Allocate<float>(width)) |
|||
{ |
|||
// Be careful! Do not capture colorSpan & amountSpan in the lambda below!
|
|||
Span<TPixel> colorSpan = colors.GetSpan(); |
|||
Span<float> amountSpan = amount.GetSpan(); |
|||
|
|||
colorSpan.Fill(color); |
|||
amountSpan.Fill(graphicsOptions.BlendPercentage); |
|||
|
|||
PixelBlender<TPixel> blender = PixelOperations<TPixel>.Instance.GetPixelBlender(graphicsOptions); |
|||
|
|||
ParallelHelper.IterateRows( |
|||
workingRect, |
|||
configuration, |
|||
rows => |
|||
{ |
|||
for (int y = rows.Min; y < rows.Max; y++) |
|||
{ |
|||
Span<TPixel> destination = |
|||
source.GetPixelRowSpan(y - startY).Slice(minX - startX, width); |
|||
|
|||
// This switched color & destination in the 2nd and 3rd places because we are applying the target color under the current one
|
|||
blender.Blend( |
|||
source.Configuration, |
|||
destination, |
|||
colors.GetSpan(), |
|||
destination, |
|||
amount.GetSpan()); |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,112 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
using System.Buffers; |
|||
using System.Numerics; |
|||
|
|||
using SixLabors.ImageSharp.Advanced; |
|||
using SixLabors.ImageSharp.Memory; |
|||
using SixLabors.ImageSharp.ParallelUtils; |
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
using SixLabors.Primitives; |
|||
|
|||
namespace SixLabors.ImageSharp.Processing.Processors.Overlays |
|||
{ |
|||
/// <summary>
|
|||
/// An <see cref="IImageProcessor{TPixel}"/> that applies a radial glow effect an <see cref="Image{TPixel}"/>.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|||
internal class GlowProcessor<TPixel> : ImageProcessor<TPixel> |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
private readonly PixelBlender<TPixel> blender; |
|||
|
|||
private readonly GlowProcessor definition; |
|||
|
|||
public GlowProcessor(GlowProcessor definition) |
|||
{ |
|||
this.definition = definition; |
|||
this.blender = PixelOperations<TPixel>.Instance.GetPixelBlender(definition.GraphicsOptions); |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
protected override void OnFrameApply( |
|||
ImageFrame<TPixel> source, |
|||
Rectangle sourceRectangle, |
|||
Configuration configuration) |
|||
{ |
|||
// TODO: can we simplify the rectangle calculation?
|
|||
int startY = sourceRectangle.Y; |
|||
int endY = sourceRectangle.Bottom; |
|||
int startX = sourceRectangle.X; |
|||
int endX = sourceRectangle.Right; |
|||
TPixel glowColor = this.definition.GlowColor.ToPixel<TPixel>(); |
|||
Vector2 center = Rectangle.Center(sourceRectangle); |
|||
|
|||
float finalRadius = this.definition.Radius.Calculate(source.Size()); |
|||
|
|||
float maxDistance = finalRadius > 0 |
|||
? MathF.Min(finalRadius, sourceRectangle.Width * .5F) |
|||
: sourceRectangle.Width * .5F; |
|||
|
|||
// Align start/end positions.
|
|||
int minX = Math.Max(0, startX); |
|||
int maxX = Math.Min(source.Width, endX); |
|||
int minY = Math.Max(0, startY); |
|||
int maxY = Math.Min(source.Height, endY); |
|||
|
|||
// Reset offset if necessary.
|
|||
if (minX > 0) |
|||
{ |
|||
startX = 0; |
|||
} |
|||
|
|||
if (minY > 0) |
|||
{ |
|||
startY = 0; |
|||
} |
|||
|
|||
int width = maxX - minX; |
|||
int offsetX = minX - startX; |
|||
|
|||
var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY); |
|||
|
|||
float blendPercentage = this.definition.GraphicsOptions.BlendPercentage; |
|||
|
|||
using (IMemoryOwner<TPixel> rowColors = source.MemoryAllocator.Allocate<TPixel>(width)) |
|||
{ |
|||
rowColors.GetSpan().Fill(glowColor); |
|||
|
|||
ParallelHelper.IterateRowsWithTempBuffer<float>( |
|||
workingRect, |
|||
configuration, |
|||
(rows, amounts) => |
|||
{ |
|||
Span<float> amountsSpan = amounts.Span; |
|||
|
|||
for (int y = rows.Min; y < rows.Max; y++) |
|||
{ |
|||
int offsetY = y - startY; |
|||
|
|||
for (int i = 0; i < width; i++) |
|||
{ |
|||
float distance = Vector2.Distance(center, new Vector2(i + offsetX, offsetY)); |
|||
amountsSpan[i] = |
|||
(blendPercentage * (1 - (.95F * (distance / maxDistance)))).Clamp(0, 1); |
|||
} |
|||
|
|||
Span<TPixel> destination = source.GetPixelRowSpan(offsetY).Slice(offsetX, width); |
|||
|
|||
this.blender.Blend( |
|||
source.Configuration, |
|||
destination, |
|||
destination, |
|||
rowColors.GetSpan(), |
|||
amountsSpan); |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,114 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
using System.Buffers; |
|||
using System.Numerics; |
|||
|
|||
using SixLabors.ImageSharp.Advanced; |
|||
using SixLabors.ImageSharp.Memory; |
|||
using SixLabors.ImageSharp.ParallelUtils; |
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
using SixLabors.Primitives; |
|||
|
|||
namespace SixLabors.ImageSharp.Processing.Processors.Overlays |
|||
{ |
|||
/// <summary>
|
|||
/// An <see cref="IImageProcessor{TPixel}"/> that applies a radial vignette effect to an <see cref="Image{TPixel}"/>.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|||
internal class VignetteProcessor<TPixel> : ImageProcessor<TPixel> |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
private readonly PixelBlender<TPixel> blender; |
|||
|
|||
private readonly VignetteProcessor definition; |
|||
|
|||
public VignetteProcessor(VignetteProcessor definition) |
|||
{ |
|||
this.definition = definition; |
|||
this.blender = PixelOperations<TPixel>.Instance.GetPixelBlender(definition.GraphicsOptions); |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
protected override void OnFrameApply( |
|||
ImageFrame<TPixel> source, |
|||
Rectangle sourceRectangle, |
|||
Configuration configuration) |
|||
{ |
|||
int startY = sourceRectangle.Y; |
|||
int endY = sourceRectangle.Bottom; |
|||
int startX = sourceRectangle.X; |
|||
int endX = sourceRectangle.Right; |
|||
TPixel vignetteColor = this.definition.VignetteColor.ToPixel<TPixel>(); |
|||
Vector2 centre = Rectangle.Center(sourceRectangle); |
|||
|
|||
Size sourceSize = source.Size(); |
|||
float finalRadiusX = this.definition.RadiusX.Calculate(sourceSize); |
|||
float finalRadiusY = this.definition.RadiusY.Calculate(sourceSize); |
|||
float rX = finalRadiusX > 0 |
|||
? MathF.Min(finalRadiusX, sourceRectangle.Width * .5F) |
|||
: sourceRectangle.Width * .5F; |
|||
float rY = finalRadiusY > 0 |
|||
? MathF.Min(finalRadiusY, sourceRectangle.Height * .5F) |
|||
: sourceRectangle.Height * .5F; |
|||
float maxDistance = MathF.Sqrt((rX * rX) + (rY * rY)); |
|||
|
|||
// Align start/end positions.
|
|||
int minX = Math.Max(0, startX); |
|||
int maxX = Math.Min(source.Width, endX); |
|||
int minY = Math.Max(0, startY); |
|||
int maxY = Math.Min(source.Height, endY); |
|||
|
|||
// Reset offset if necessary.
|
|||
if (minX > 0) |
|||
{ |
|||
startX = 0; |
|||
} |
|||
|
|||
if (minY > 0) |
|||
{ |
|||
startY = 0; |
|||
} |
|||
|
|||
int width = maxX - minX; |
|||
int offsetX = minX - startX; |
|||
|
|||
var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY); |
|||
float blendPercentage = this.definition.GraphicsOptions.BlendPercentage; |
|||
|
|||
using (IMemoryOwner<TPixel> rowColors = source.MemoryAllocator.Allocate<TPixel>(width)) |
|||
{ |
|||
rowColors.GetSpan().Fill(vignetteColor); |
|||
|
|||
ParallelHelper.IterateRowsWithTempBuffer<float>( |
|||
workingRect, |
|||
configuration, |
|||
(rows, amounts) => |
|||
{ |
|||
Span<float> amountsSpan = amounts.Span; |
|||
|
|||
for (int y = rows.Min; y < rows.Max; y++) |
|||
{ |
|||
int offsetY = y - startY; |
|||
|
|||
for (int i = 0; i < width; i++) |
|||
{ |
|||
float distance = Vector2.Distance(centre, new Vector2(i + offsetX, offsetY)); |
|||
amountsSpan[i] = (blendPercentage * (.9F * (distance / maxDistance))).Clamp(0, 1); |
|||
} |
|||
|
|||
Span<TPixel> destination = source.GetPixelRowSpan(offsetY).Slice(offsetX, width); |
|||
|
|||
this.blender.Blend( |
|||
source.Configuration, |
|||
destination, |
|||
destination, |
|||
rowColors.GetSpan(), |
|||
amountsSpan); |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue