Browse Source

Adding pixelate processor

Former-commit-id: 89965b4a0b6f41159b90655139045dd2bcfcea44
af/merge-core
James South 12 years ago
parent
commit
207c1fa4e7
  1. 22
      src/ImageProcessor/ImageFactory.cs
  2. 1
      src/ImageProcessor/ImageProcessor.csproj
  3. 134
      src/ImageProcessor/Processors/Pixelate.cs
  4. 9
      src/ImageProcessorConsole/Program.cs

22
src/ImageProcessor/ImageFactory.cs

@ -650,6 +650,28 @@ namespace ImageProcessor
return this;
}
/// <summary>
/// Pixelates an image with the given size.
/// </summary>
/// <param name="pixelSize">
/// The size of the pixels to create.</param>
/// <param name="rectangle">
/// The area in which to pixelate the image. If not set, the whole image is pixelated.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Pixelate(int pixelSize, Rectangle? rectangle = null)
{
if (this.ShouldProcess && pixelSize > 0)
{
Pixelate pixelate = new Pixelate { DynamicParameter = new Tuple<int, Rectangle?>(pixelSize, rectangle) };
this.CurrentImageFormat.ApplyProcessor(pixelate.ProcessImage, this);
}
return this;
}
/// <summary>
/// Alters the output quality of the current image.
/// <remarks>

1
src/ImageProcessor/ImageProcessor.csproj

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

134
src/ImageProcessor/Processors/Pixelate.cs

@ -0,0 +1,134 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Pixelate.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates methods to pixelate an image.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Processors
{
using System;
using System.Collections.Generic;
using System.Drawing;
using ImageProcessor.Common.Exceptions;
using ImageProcessor.Imaging;
/// <summary>
/// Encapsulates methods to pixelate an image.
/// </summary>
public class Pixelate : IGraphicsProcessor
{
/// <summary>
/// Initializes a new instance of the <see cref="Pixelate"/> class.
/// </summary>
public Pixelate()
{
this.Settings = new Dictionary<string, string>();
}
/// <summary>
/// Gets or sets the dynamic parameter.
/// </summary>
public dynamic DynamicParameter
{
get;
set;
}
/// <summary>
/// Gets or sets any additional settings required by the processor.
/// </summary>
public Dictionary<string, string> Settings
{
get;
set;
}
/// <summary>
/// Processes the image.
/// </summary>
/// <param name="factory">
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
/// the image to process.
/// </param>
/// <returns>
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public Image ProcessImage(ImageFactory factory)
{
Bitmap newImage = null;
Image image = factory.Image;
try
{
Tuple<int, Rectangle?> parameters = this.DynamicParameter;
int size = parameters.Item1;
Rectangle rectangle = parameters.Item2.HasValue
? parameters.Item2.Value
: new Rectangle(0, 0, image.Width, image.Height);
int x = rectangle.X;
int y = rectangle.Y;
int offset = size / 2;
int width = rectangle.Width;
int height = rectangle.Height;
int maxWidth = image.Width;
int maxHeight = image.Height;
newImage = new Bitmap(image);
using (FastBitmap fastBitmap = new FastBitmap(newImage))
{
for (int i = x; i < x + width && i < maxWidth; i += size)
{
for (int j = y; j < y + height && j < maxHeight; j += size)
{
int offsetX = offset;
int offsetY = offset;
// Make sure that the offset is within the boundary of the image.
while (i + offsetX >= maxWidth)
{
offsetX--;
}
while (j + offsetY >= maxHeight)
{
offsetY--;
}
// Get the pixel color in the centre of the soon to be pixelated area.
Color pixel = fastBitmap.GetPixel(i + offsetX, j + offsetY);
// For each pixel in the pixelate size, set it to the centre color.
for (int k = i; k < i + size && k < maxWidth; k++)
{
for (int l = j; l < j + size && l < maxHeight; l++)
{
fastBitmap.SetPixel(k, l, pixel);
}
}
}
}
}
image.Dispose();
image = newImage;
}
catch (Exception ex)
{
if (newImage != null)
{
newImage.Dispose();
}
throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
}
return image;
}
}
}

9
src/ImageProcessorConsole/Program.cs

@ -18,6 +18,7 @@ namespace ImageProcessorConsole
using System.Linq;
using ImageProcessor;
using ImageProcessor.Imaging;
using ImageProcessor.Imaging.Filters;
using ImageProcessor.Plugins.Cair;
using ImageProcessor.Plugins.Cair.Imaging;
@ -60,7 +61,7 @@ namespace ImageProcessorConsole
{
using (ImageFactory imageFactory = new ImageFactory(true))
{
Size size = new Size(448, 0);
Size size = new Size(800, 0);
//ContentAwareResizeLayer layer = new ContentAwareResizeLayer(size)
//{
@ -72,9 +73,9 @@ namespace ImageProcessorConsole
//.BackgroundColor(Color.White)
//.Resize(new Size((int)(size.Width * 1.1), 0))
//.ContentAwareResize(layer)
.Constrain(size)
.Hue(180)
.Quality(100)
//.Constrain(size)
//.Filter(MatrixFilters.HiSatch)
.Pixelate(8)
.Save(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(path), @"..\..\images\output", fileInfo.Name)));
stopwatch.Stop();

Loading…
Cancel
Save