Browse Source

Adding replace color functionality

Former-commit-id: 47ef8ddfa95092974213c3312125f5b171f7a2f3
pull/17/head
James South 12 years ago
parent
commit
22d661d168
  1. 37
      src/ImageProcessor/ImageFactory.cs
  2. 1
      src/ImageProcessor/ImageProcessor.csproj
  3. 55
      src/ImageProcessor/Processors/ReplaceColor.cs
  4. 3
      src/ImageProcessorConsole/Program.cs

37
src/ImageProcessor/ImageFactory.cs

@ -660,7 +660,7 @@ namespace ImageProcessor
/// </returns>
public ImageFactory Quality(int percentage)
{
if (this.ShouldProcess)
if (percentage <= 100 && percentage >= 0 && this.ShouldProcess)
{
this.CurrentImageFormat.Quality = percentage;
}
@ -668,6 +668,41 @@ namespace ImageProcessor
return this;
}
/// <summary>
/// Replaces a color within the current image.
/// </summary>
/// <param name="target">
/// The target <see cref="System.Drawing.Color"/>.
/// </param>
/// <param name="replacement">
/// The replacement <see cref="System.Drawing.Color"/>.
/// </param>
/// <param name="fuzziness">
/// A value between 0 and 100 with which to alter the target detection accuracy.
/// </param>
/// <returns>
/// The <see cref="ImageFactory"/>.
/// </returns>
public ImageFactory ReplaceColor(Color target, Color replacement, int fuzziness = 0)
{
// Sanitize the input.
if (fuzziness < 0 || fuzziness > 100)
{
fuzziness = 0;
}
if (this.ShouldProcess && target != Color.Empty && replacement != Color.Empty)
{
ReplaceColor replaceColor = new ReplaceColor
{
DynamicParameter = new Tuple<Color, Color, int>(target, replacement, fuzziness)
};
this.CurrentImageFormat.ApplyProcessor(replaceColor.ProcessImage, this);
}
return this;
}
/// <summary>
/// Resizes the current image to the given dimensions.
/// </summary>

1
src/ImageProcessor/ImageProcessor.csproj

@ -128,6 +128,7 @@
<Compile Include="Processors\Hue.cs" />
<Compile Include="Processors\Meta.cs" />
<Compile Include="Processors\Pixelate.cs" />
<Compile Include="Processors\ReplaceColor.cs" />
<Compile Include="Processors\RoundedCorners.cs" />
<Compile Include="Processors\Saturation.cs" />
<Compile Include="Processors\Flip.cs" />

55
src/ImageProcessor/Processors/ReplaceColor.cs

@ -1,14 +1,24 @@
namespace ImageProcessor.Processors
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ReplaceColor.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates methods allowing the replacement of a color within an image.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Processors
{
using System;
using System.Collections.Generic;
using System.Drawing;
using ImageProcessor.Common.Exceptions;
using ImageProcessor.Common.Extensions;
using ImageProcessor.Imaging;
/// <summary>
/// Encapsulates methods allowing the replacement of a color within an image.
/// <see href="http://softwarebydefault.com/2013/03/16/bitmap-color-substitution/"/>
/// </summary>
public class ReplaceColor : IGraphicsProcessor
{
@ -57,20 +67,55 @@
{
Tuple<Color, Color, int> parameters = this.DynamicParameter;
Color original = parameters.Item1;
byte originalR = original.R;
byte originalG = original.G;
byte originalB = original.B;
Color replacement = parameters.Item2;
int threshold = parameters.Item3;
byte replacementR = original.R;
byte replacementG = replacement.G;
byte replacementB = replacement.B;
int fuzziness = parameters.Item3;
newImage = new Bitmap(image);
int width = image.Width;
int height = image.Height;
using (FastBitmap fastBitmap = new FastBitmap(newImage))
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
// Get the pixel color.
Color currentColor = fastBitmap.GetPixel(x, y);
byte currentR = currentColor.R;
byte currentG = currentColor.B;
byte currentB = currentColor.B;
// Test whether it is in the expected range.
if (currentR <= originalR + fuzziness && currentR >= originalR - fuzziness)
{
if (currentG <= originalG + fuzziness && currentG >= originalG - fuzziness)
{
if (currentB <= originalB + fuzziness && currentB >= originalB - fuzziness)
{
// Ensure the values are withing an acceptable byte range
// and set the new value.
byte r = (originalR - currentR + replacementR).ToByte();
byte g = (originalG - currentG + replacementG).ToByte();
byte b = (originalB - currentB + replacementB).ToByte();
fastBitmap.SetPixel(x, y, Color.FromArgb(currentColor.A, r, g, b));
}
}
}
}
}
}
image.Dispose();
image = newImage;
}
catch (Exception ex)
{

3
src/ImageProcessorConsole/Program.cs

@ -74,7 +74,8 @@ namespace ImageProcessorConsole
//.Resize(new Size((int)(size.Width * 1.1), 0))
//.ContentAwareResize(layer)
.Constrain(size)
.Filter(MatrixFilters.Comic)
.ReplaceColor(Color.FromArgb(255, 1, 107, 165), Color.FromArgb(255, 1, 165, 13), 80)
//.Filter(MatrixFilters.Comic)
//.Filter(MatrixFilters.HiSatch)
//.Pixelate(8)
//.GaussianSharpen(10)

Loading…
Cancel
Save