From f35a8c30b941673e672d23f3e0660595b8c9728f Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 11 Aug 2016 10:38:47 +1000 Subject: [PATCH] Fix Glow Former-commit-id: dbea7d85b184eecdbac2eb1d3330469885ef9cdd Former-commit-id: 0e6fb4eb1dd1ce575a04100eef819cb80a72834a Former-commit-id: 728c0fe6fae89ac1d551af02a030a6bdf78d18a3 --- src/ImageProcessorCore/Filters/Glow.cs | 20 ++++++------ .../ColorMatrix/LomographProcessor.cs | 2 +- .../ColorMatrix/PolaroidProcessor.cs | 12 ++----- .../Filters/Processors/GlowProcessor.cs | 32 ++++++++----------- .../Processors/Filters/GlowTest.cs | 2 +- 5 files changed, 28 insertions(+), 40 deletions(-) diff --git a/src/ImageProcessorCore/Filters/Glow.cs b/src/ImageProcessorCore/Filters/Glow.cs index 3a7f487d36..49acf33425 100644 --- a/src/ImageProcessorCore/Filters/Glow.cs +++ b/src/ImageProcessorCore/Filters/Glow.cs @@ -24,7 +24,7 @@ namespace ImageProcessorCore where T : IPackedVector where TP : struct { - return Glow(source, default(T), source.Bounds.Width * .5F, source.Bounds.Height * .5F, source.Bounds, progressHandler); + return Glow(source, default(T), source.Bounds.Width * .5F, source.Bounds, progressHandler); } /// @@ -40,7 +40,7 @@ namespace ImageProcessorCore where T : IPackedVector where TP : struct { - return Glow(source, color, source.Bounds.Width * .5F, source.Bounds.Height * .5F, source.Bounds, progressHandler); + return Glow(source, color, source.Bounds.Width * .5F, source.Bounds, progressHandler); } /// @@ -49,15 +49,14 @@ namespace ImageProcessorCore /// The pixel format. /// The packed format. long, float. /// The image this method extends. - /// The the x-radius. - /// The the y-radius. + /// The the radius. /// A delegate which is called as progress is made processing the image. /// The . - public static Image Glow(this Image source, float radiusX, float radiusY, ProgressEventHandler progressHandler = null) + public static Image Glow(this Image source, float radius, ProgressEventHandler progressHandler = null) where T : IPackedVector where TP : struct { - return Glow(source, default(T), radiusX, radiusY, source.Bounds, progressHandler); + return Glow(source, default(T), radius, source.Bounds, progressHandler); } /// @@ -75,7 +74,7 @@ namespace ImageProcessorCore where T : IPackedVector where TP : struct { - return Glow(source, default(T), 0, 0, rectangle, progressHandler); + return Glow(source, default(T), 0, rectangle, progressHandler); } /// @@ -85,18 +84,17 @@ namespace ImageProcessorCore /// The packed format. long, float. /// The image this method extends. /// The color to set as the glow. - /// The the x-radius. - /// The the y-radius. + /// The the radius. /// /// The structure that specifies the portion of the image object to alter. /// /// A delegate which is called as progress is made processing the image. /// The . - public static Image Glow(this Image source, T color, float radiusX, float radiusY, Rectangle rectangle, ProgressEventHandler progressHandler = null) + public static Image Glow(this Image source, T color, float radius, Rectangle rectangle, ProgressEventHandler progressHandler = null) where T : IPackedVector where TP : struct { - GlowProcessor processor = new GlowProcessor { RadiusX = radiusX, RadiusY = radiusY }; + GlowProcessor processor = new GlowProcessor { Radius = radius, }; if (!color.Equals(default(T))) { diff --git a/src/ImageProcessorCore/Filters/Processors/ColorMatrix/LomographProcessor.cs b/src/ImageProcessorCore/Filters/Processors/ColorMatrix/LomographProcessor.cs index f701d4d55a..173ec1a81c 100644 --- a/src/ImageProcessorCore/Filters/Processors/ColorMatrix/LomographProcessor.cs +++ b/src/ImageProcessorCore/Filters/Processors/ColorMatrix/LomographProcessor.cs @@ -31,7 +31,7 @@ namespace ImageProcessorCore.Processors protected override void AfterApply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle) { T packed = default(T); - packed.PackFromBytes(0, 10, 0, 255); + packed.PackFromBytes(0, 10, 0, 255); // Very dark (mostly black) lime green. new VignetteProcessor { VignetteColor = packed }.Apply(target, target, targetRectangle); } } diff --git a/src/ImageProcessorCore/Filters/Processors/ColorMatrix/PolaroidProcessor.cs b/src/ImageProcessorCore/Filters/Processors/ColorMatrix/PolaroidProcessor.cs index de56a9e00c..689861f73a 100644 --- a/src/ImageProcessorCore/Filters/Processors/ColorMatrix/PolaroidProcessor.cs +++ b/src/ImageProcessorCore/Filters/Processors/ColorMatrix/PolaroidProcessor.cs @@ -37,18 +37,12 @@ namespace ImageProcessorCore.Processors protected override void AfterApply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle) { T packedV = default(T); - packedV.PackFromBytes(102, 34, 0, 255); + packedV.PackFromBytes(102, 34, 0, 255); // Very dark orange [Brown tone] new VignetteProcessor { VignetteColor = packedV }.Apply(target, target, targetRectangle); T packedG = default(T); - packedG.PackFromBytes(255, 153, 102, 178); - new GlowProcessor - { - GlowColor = packedG, - RadiusX = target.Width / 4f, - RadiusY = target.Width / 4f - } - .Apply(target, target, targetRectangle); + packedG.PackFromBytes(255, 153, 102, 178); // Light orange + new GlowProcessor { GlowColor = packedG, Radius = target.Width / 4F }.Apply(target, target, targetRectangle); } } } diff --git a/src/ImageProcessorCore/Filters/Processors/GlowProcessor.cs b/src/ImageProcessorCore/Filters/Processors/GlowProcessor.cs index 69c2b05f7c..e854f798cf 100644 --- a/src/ImageProcessorCore/Filters/Processors/GlowProcessor.cs +++ b/src/ImageProcessorCore/Filters/Processors/GlowProcessor.cs @@ -24,7 +24,7 @@ namespace ImageProcessorCore.Processors public GlowProcessor() { T color = default(T); - color.PackFromVector4(Color.White.ToVector4()); + color.PackFromVector4(Color.Black.ToVector4()); this.GlowColor = color; } @@ -34,14 +34,9 @@ namespace ImageProcessorCore.Processors public T GlowColor { get; set; } /// - /// Gets or sets the the x-radius. + /// Gets or sets the the radius. /// - public float RadiusX { get; set; } - - /// - /// Gets or sets the the y-radius. - /// - public float RadiusY { get; set; } + public float Radius { get; set; } /// protected override void Apply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY) @@ -50,9 +45,8 @@ namespace ImageProcessorCore.Processors int endX = sourceRectangle.Right; T glowColor = this.GlowColor; Vector2 centre = Rectangle.Center(sourceRectangle).ToVector2(); - float rX = this.RadiusX > 0 ? Math.Min(this.RadiusX, sourceRectangle.Width * .5F) : sourceRectangle.Width * .5F; - float rY = this.RadiusY > 0 ? Math.Min(this.RadiusY, sourceRectangle.Height * .5F) : sourceRectangle.Height * .5F; - float maxDistance = (float)Math.Sqrt((rX * rX) + (rY * rY)); + float maxDistance = this.Radius > 0 ? Math.Min(this.Radius, sourceRectangle.Width * .5F) : sourceRectangle.Width * .5F; + Ellipse ellipse = new Ellipse(new Point(centre), maxDistance, maxDistance); // Align start/end positions. int minX = Math.Max(0, startX); @@ -84,13 +78,15 @@ namespace ImageProcessorCore.Processors for (int x = minX; x < maxX; x++) { int offsetX = x - startX; - - // TODO: Premultiply? - float distance = Vector2.Distance(centre, new Vector2(offsetX, offsetY)); - Vector4 sourceColor = sourcePixels[offsetX, offsetY].ToVector4(); - T packed = default(T); - packed.PackFromVector4(Vector4.Lerp(glowColor.ToVector4(), sourceColor, .5F * (distance / maxDistance))); - targetPixels[offsetX, offsetY] = packed; + if (ellipse.Contains(offsetX, offsetY)) + { + // TODO: Premultiply? + float distance = Vector2.Distance(centre, new Vector2(offsetX, offsetY)); + Vector4 sourceColor = sourcePixels[offsetX, offsetY].ToVector4(); + T packed = default(T); + packed.PackFromVector4(Vector4.Lerp(glowColor.ToVector4(), sourceColor, distance / maxDistance)); + targetPixels[offsetX, offsetY] = packed; + } } this.OnRowProcessed(); diff --git a/tests/ImageProcessorCore.Tests/Processors/Filters/GlowTest.cs b/tests/ImageProcessorCore.Tests/Processors/Filters/GlowTest.cs index 9666c31c20..0237cab6b3 100644 --- a/tests/ImageProcessorCore.Tests/Processors/Filters/GlowTest.cs +++ b/tests/ImageProcessorCore.Tests/Processors/Filters/GlowTest.cs @@ -76,7 +76,7 @@ namespace ImageProcessorCore.Tests Image image = new Image(stream); using (FileStream output = File.OpenWrite($"{path}/{filename}")) { - image.Glow(image.Width / 4, image.Height / 4) + image.Glow(image.Width / 4) .Save(output); } }