Browse Source

Much better Vignette

Former-commit-id: d70717994e390056d03b55f1c3f6b5ececd97bc0
Former-commit-id: 92ab616c4b25d03512c764da0bf54916dc0c4c6d
Former-commit-id: 8696473ce6f0867755004503b936e76953c63859
pull/17/head
James Jackson-South 10 years ago
parent
commit
041e9fee3d
  1. 3
      src/ImageProcessor/Colors/Color.cs
  2. 6
      src/ImageProcessor/Filters/ColorMatrix/Lomograph.cs
  3. 7
      src/ImageProcessor/Filters/ColorMatrix/Polaroid.cs
  4. 58
      src/ImageProcessor/Filters/Glow.cs
  5. 45
      src/ImageProcessor/Filters/Vignette.cs
  6. 2
      src/ImageProcessor/Numerics/Ellipse.cs

3
src/ImageProcessor/Colors/Color.cs

@ -353,7 +353,8 @@ namespace ImageProcessor
amount = amount.Clamp(0f, 1f);
// Premultiplied.
return (from * (1 - amount)) + to;
return from + (to - from) * amount;
//return (from * (1 - amount)) + to;
}
/// <summary>

6
src/ImageProcessor/Filters/ColorMatrix/Lomograph.cs

@ -22,5 +22,11 @@ namespace ImageProcessor.Filters
M42 = .0f,
M43 = -.08f
};
/// <inheritdoc/>
protected override void AfterApply(ImageBase source, ImageBase target, Rectangle targetRectangle, Rectangle sourceRectangle)
{
new Vignette { Color = new Color(0, 10 / 255f, 0) }.Apply(target, target, targetRectangle);
}
}
}

7
src/ImageProcessor/Filters/ColorMatrix/Polaroid.cs

@ -28,5 +28,12 @@ namespace ImageProcessor.Filters
M42 = -0.05f,
M43 = -0.05f
};
/// <inheritdoc/>
protected override void AfterApply(ImageBase source, ImageBase target, Rectangle targetRectangle, Rectangle sourceRectangle)
{
new Vignette { Color = new Color(102 / 255f, 34 / 255f, 0) }.Apply(target, target, targetRectangle);
new Glow { Color = new Color(1, 153 / 255f, 102 / 255f) }.Apply(target, target, targetRectangle);
}
}
}

58
src/ImageProcessor/Filters/Glow.cs

@ -0,0 +1,58 @@
// <copyright file="Glow.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageProcessor.Filters
{
using System;
using System.Numerics;
using System.Threading.Tasks;
/// <summary>
/// Creates a glow effect on the image
/// </summary>
public class Glow : ParallelImageProcessor
{
/// <summary>
/// Gets or sets the vignette color to apply.
/// </summary>
public Color Color { get; set; } = Color.White;
/// <summary>
/// Gets or sets the the x-radius.
/// </summary>
public float RadiusX { get; set; }
/// <summary>
/// Gets or sets the the y-radius.
/// </summary>
public float RadiusY { get; set; }
/// <inheritdoc/>
protected override void Apply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY)
{
int startX = sourceRectangle.X;
int endX = sourceRectangle.Right;
Color color = this.Color;
Vector2 centre = Rectangle.Center(targetRectangle);
float rX = this.RadiusX > 0 ? this.RadiusX : targetRectangle.Width / 2f;
float rY = this.RadiusY > 0 ? this.RadiusY : targetRectangle.Height / 2f;
float maxDistance = (float)Math.Sqrt(rX * rX + rY * rY);
Parallel.For(
startY,
endY,
y =>
{
for (int x = startX; x < endX; x++)
{
float distance = Vector2.Distance(centre, new Vector2(x, y));
Color sourceColor = target[x, y];
target[x, y] = Color.Lerp(color, sourceColor, 4f * distance / maxDistance);
}
});
}
}
}

45
src/ImageProcessor/Filters/Vignette.cs

@ -1,18 +1,19 @@
namespace ImageProcessor.Filters
// <copyright file="Vignette.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageProcessor.Filters
{
using System;
using System.Numerics;
using System.Threading.Tasks;
/// <summary>
/// Creates a vignette
/// Creates a vignette effect on the image
/// </summary>
public class Vignette : ParallelImageProcessor
{
/// <summary>
/// Used to hold a copy of the target image.
/// </summary>
private readonly Image targetCopy = new Image();
/// <summary>
/// Gets or sets the vignette color to apply.
/// </summary>
@ -28,26 +29,16 @@
/// </summary>
public float RadiusY { get; set; }
/// <inheritdoc/>
protected override void OnApply(ImageBase source, ImageBase target, Rectangle targetRectangle, Rectangle sourceRectangle)
{
this.targetCopy.SetPixels(target.Width, target.Height, target.Pixels);
target.SetPixels(target.Width, target.Height, new float[target.Pixels.Length]);
}
/// <inheritdoc/>
protected override void Apply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY)
{
int startX = sourceRectangle.X;
int endX = sourceRectangle.Right;
Color color = this.Color;
Vector2 centre = Rectangle.Center(targetRectangle);
int centerX = (int)centre.X;
int centerY = (int)centre.Y;
float rX = this.RadiusX > 0 ? this.RadiusX : targetRectangle.Width / 2f;
float rY = this.RadiusY > 0 ? this.RadiusY : targetRectangle.Height / 2f;
Ellipse ellipse = new Ellipse(new Point(centerX, centerY), rX - 1, rY - 1);
float maxDistance = (float)Math.Sqrt(rX * rX + rY * rY);
Parallel.For(
startY,
@ -56,22 +47,12 @@
{
for (int x = startX; x < endX; x++)
{
if (!ellipse.Contains(x, y))
{
target[x, y] = Color.Black;
}
float distance = Vector2.Distance(centre, new Vector2(x, y));
Color sourceColor = target[x, y];
target[x, y] = Color.Lerp(sourceColor, color, .9f * distance / maxDistance);
}
});
}
/// <inheritdoc/>
protected override void AfterApply(ImageBase source, ImageBase target, Rectangle targetRectangle, Rectangle sourceRectangle)
{
new GuassianBlur(30).Apply(target, target, targetRectangle);
Image temp = new Image(this.targetCopy);
new Blend(target, 40).Apply(this.targetCopy, temp, targetRectangle);
target.SetPixels(temp.Width, temp.Height, this.targetCopy.Pixels);
}
}
}

2
src/ImageProcessor/Numerics/Ellipse.cs

@ -97,7 +97,7 @@ namespace ImageProcessor
/// <summary>
/// Determines if the specfied point is contained within the rectangular region defined by
/// this <see cref="Rectangle"/>.
/// this <see cref="Ellipse"/>.
/// </summary>
/// <param name="x">The x-coordinate of the given point.</param>
/// <param name="y">The y-coordinate of the given point.</param>

Loading…
Cancel
Save