Browse Source

Make Trace public

Former-commit-id: 90a73f8bc6a47348572822b166a6706f88f381e6
Former-commit-id: 35bb8ae4f5f509e060fac398c31f1812c0c8ac98
pull/17/head
James South 11 years ago
parent
commit
628af708e6
  1. 3
      src/ImageProcessor/Imaging/Filters/Artistic/HalftoneFilter.cs
  2. 3
      src/ImageProcessor/Imaging/Filters/Artistic/OilPaintingFilter.cs
  3. 66
      src/ImageProcessor/Imaging/Filters/Photo/ComicMatrixFilter.cs
  4. 61
      src/ImageProcessor/Imaging/Helpers/Effects.cs
  5. 65
      src/ImageProcessor/Processors/Halftone.cs

3
src/ImageProcessor/Imaging/Filters/Artistic/HalftoneFilter.cs

@ -149,7 +149,7 @@ namespace ImageProcessor.Imaging.Filters.Artistic
}
/// <summary>
/// Applies the filter. TODO: Make this class implement an interface?
/// Applies the halftone filter.
/// </summary>
/// <param name="source">
/// The <see cref="Bitmap"/> to apply the filter to.
@ -159,6 +159,7 @@ namespace ImageProcessor.Imaging.Filters.Artistic
/// </returns>
public Bitmap ApplyFilter(Bitmap source)
{
// TODO: Make this class implement an interface?
Bitmap cyan = null;
Bitmap magenta = null;
Bitmap yellow = null;

3
src/ImageProcessor/Imaging/Filters/Artistic/OilPaintingFilter.cs

@ -85,7 +85,7 @@ namespace ImageProcessor.Imaging.Filters.Artistic
}
/// <summary>
/// Applies the filter. TODO: Make this class implement an interface?
/// Applies the oil painting filter.
/// </summary>
/// <param name="source">
/// The source.
@ -95,6 +95,7 @@ namespace ImageProcessor.Imaging.Filters.Artistic
/// </returns>
public Bitmap ApplyFilter(Bitmap source)
{
// TODO: Make this class implement an interface?
int width = source.Width;
int height = source.Height;

66
src/ImageProcessor/Imaging/Filters/Photo/ComicMatrixFilter.cs

@ -13,10 +13,8 @@ namespace ImageProcessor.Imaging.Filters.Photo
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Threading.Tasks;
using ImageProcessor.Imaging.Filters.Artistic;
using ImageProcessor.Imaging.Filters.EdgeDetection;
using ImageProcessor.Imaging.Helpers;
/// <summary>
@ -68,7 +66,7 @@ namespace ImageProcessor.Imaging.Filters.Photo
// Draw the edges.
edgeBitmap = new Bitmap(width, height);
edgeBitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
edgeBitmap = Trace(image, edgeBitmap, 120);
edgeBitmap = Effects.Trace(image, edgeBitmap, 120);
using (Graphics graphics = Graphics.FromImage(highBitmap))
{
@ -97,8 +95,7 @@ namespace ImageProcessor.Imaging.Filters.Photo
using (Graphics graphics = Graphics.FromImage(patternBitmap))
{
graphics.Clear(Color.Transparent);
graphics.SmoothingMode = SmoothingMode.HighQuality;
for (int y = 0; y < height; y += 8)
{
for (int x = 0; x < width; x += 4)
@ -170,64 +167,5 @@ namespace ImageProcessor.Imaging.Filters.Photo
return (Bitmap)image;
}
/// <summary>
/// Traces the edges of a given <see cref="Image"/>.
/// TODO: Move this to another class.
/// </summary>
/// <param name="source">
/// The source <see cref="Image"/>.
/// </param>
/// <param name="destination">
/// The destination <see cref="Image"/>.
/// </param>
/// <param name="threshold">
/// The threshold (between 0 and 255).
/// </param>
/// <returns>
/// The a new instance of <see cref="Bitmap"/> traced.
/// </returns>
private static Bitmap Trace(Image source, Image destination, byte threshold = 0)
{
int width = source.Width;
int height = source.Height;
// Grab the edges converting to greyscale, and invert the colors.
ConvolutionFilter filter = new ConvolutionFilter(new SobelEdgeFilter(), true);
using (Bitmap temp = filter.Process2DFilter(source))
{
destination = new InvertMatrixFilter().TransformImage(temp, destination);
// Darken it slightly to aid detection
destination = Adjustments.Brightness(destination, -5);
}
// Loop through and replace any colors more white than the threshold
// with a transparent one.
using (FastBitmap destinationBitmap = new FastBitmap(destination))
{
Parallel.For(
0,
height,
y =>
{
for (int x = 0; x < width; x++)
{
// ReSharper disable AccessToDisposedClosure
Color color = destinationBitmap.GetPixel(x, y);
if (color.B >= threshold)
{
destinationBitmap.SetPixel(x, y, Color.Transparent);
}
// ReSharper restore AccessToDisposedClosure
}
});
}
// Darken it again to average out the color.
destination = Adjustments.Brightness(destination, -5);
return (Bitmap)destination;
}
}
}

61
src/ImageProcessor/Imaging/Helpers/Effects.cs

@ -15,6 +15,9 @@ namespace ImageProcessor.Imaging.Helpers
using System.Drawing.Drawing2D;
using System.Threading.Tasks;
using ImageProcessor.Imaging.Filters.EdgeDetection;
using ImageProcessor.Imaging.Filters.Photo;
/// <summary>
/// Provides reusable effect methods to apply to images.
/// </summary>
@ -176,5 +179,63 @@ namespace ImageProcessor.Imaging.Helpers
toMask.Dispose();
return clear;
}
/// <summary>
/// Traces the edges of a given <see cref="Image"/>.
/// </summary>
/// <param name="source">
/// The source <see cref="Image"/>.
/// </param>
/// <param name="destination">
/// The destination <see cref="Image"/>.
/// </param>
/// <param name="threshold">
/// The threshold (between 0 and 255).
/// </param>
/// <returns>
/// The a new instance of <see cref="Bitmap"/> traced.
/// </returns>
public static Bitmap Trace(Image source, Image destination, byte threshold = 0)
{
int width = source.Width;
int height = source.Height;
// Grab the edges converting to greyscale, and invert the colors.
ConvolutionFilter filter = new ConvolutionFilter(new SobelEdgeFilter(), true);
using (Bitmap temp = filter.Process2DFilter(source))
{
destination = new InvertMatrixFilter().TransformImage(temp, destination);
// Darken it slightly to aid detection
destination = Adjustments.Brightness(destination, -5);
}
// Loop through and replace any colors more white than the threshold
// with a transparent one.
using (FastBitmap destinationBitmap = new FastBitmap(destination))
{
Parallel.For(
0,
height,
y =>
{
for (int x = 0; x < width; x++)
{
// ReSharper disable AccessToDisposedClosure
Color color = destinationBitmap.GetPixel(x, y);
if (color.B >= threshold)
{
destinationBitmap.SetPixel(x, y, Color.Transparent);
}
// ReSharper restore AccessToDisposedClosure
}
});
}
// Darken it again to average out the color.
destination = Adjustments.Brightness(destination, -5);
return (Bitmap)destination;
}
}
}

65
src/ImageProcessor/Processors/Halftone.cs

@ -13,13 +13,9 @@ namespace ImageProcessor.Processors
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using ImageProcessor.Common.Exceptions;
using ImageProcessor.Imaging;
using ImageProcessor.Imaging.Filters.Artistic;
using ImageProcessor.Imaging.Filters.EdgeDetection;
using ImageProcessor.Imaging.Filters.Photo;
using ImageProcessor.Imaging.Helpers;
/// <summary>
@ -83,7 +79,7 @@ namespace ImageProcessor.Processors
// Draw the edges.
edgeBitmap = new Bitmap(width, height);
edgeBitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
edgeBitmap = Trace(image, edgeBitmap, 120);
edgeBitmap = Effects.Trace(image, edgeBitmap, 120);
using (Graphics graphics = Graphics.FromImage(newImage))
{
@ -122,64 +118,5 @@ namespace ImageProcessor.Processors
return image;
}
/// <summary>
/// Traces the edges of a given <see cref="Image"/>.
/// TODO: Move this to another class.
/// </summary>
/// <param name="source">
/// The source <see cref="Image"/>.
/// </param>
/// <param name="destination">
/// The destination <see cref="Image"/>.
/// </param>
/// <param name="threshold">
/// The threshold (between 0 and 255).
/// </param>
/// <returns>
/// The a new instance of <see cref="Bitmap"/> traced.
/// </returns>
private static Bitmap Trace(Image source, Image destination, byte threshold = 0)
{
int width = source.Width;
int height = source.Height;
// Grab the edges converting to greyscale, and invert the colors.
ConvolutionFilter filter = new ConvolutionFilter(new SobelEdgeFilter(), true);
using (Bitmap temp = filter.Process2DFilter(source))
{
destination = new InvertMatrixFilter().TransformImage(temp, destination);
// Darken it slightly to aid detection
destination = Adjustments.Brightness(destination, -5);
}
// Loop through and replace any colors more white than the threshold
// with a transparent one.
using (FastBitmap destinationBitmap = new FastBitmap(destination))
{
Parallel.For(
0,
height,
y =>
{
for (int x = 0; x < width; x++)
{
// ReSharper disable AccessToDisposedClosure
Color color = destinationBitmap.GetPixel(x, y);
if (color.B >= threshold)
{
destinationBitmap.SetPixel(x, y, Color.Transparent);
}
// ReSharper restore AccessToDisposedClosure
}
});
}
// Darken it again to average out the color.
destination = Adjustments.Brightness(destination, -5);
return (Bitmap)destination;
}
}
}

Loading…
Cancel
Save