Browse Source

Add rectangular crop.

Also add overloads to Gaussian blur and sharpen.


Former-commit-id: ff18410aefbdb605d7177dfcc0e98dadf9d85e47
Former-commit-id: 71993046259e4fbc6734345cab961c2741ccb9d6
Former-commit-id: 5a3f8b071b8e4948544db58aa1280c0df451efe2
pull/17/head
James Jackson-South 10 years ago
parent
commit
1fc25ae5dd
  1. 2
      README.md
  2. 28
      src/ImageProcessor/Filters/Convolution/GuassianBlur.cs
  3. 7
      src/ImageProcessor/ImageExtensions.cs
  4. 1
      src/ImageProcessor/ImageProcessor.csproj
  5. 44
      src/ImageProcessor/Samplers/Crop.cs
  6. 46
      src/ImageProcessor/Samplers/ImageSampleExtensions.cs
  7. 24
      tests/ImageProcessor.Tests/Processors/Samplers/SamplerTests.cs

2
README.md

@ -48,7 +48,7 @@ Honestly... I don't know. I could be writing code that may be suddenly obsolete.
- [x] Triangle
- [x] Welch
- Cropping
- [ ] Rectangular Crop
- [x] Rectangular Crop
- [ ] Elliptical Crop
- [ ] Entropy Crop
- Rotation

28
src/ImageProcessor/Filters/Convolution/GuassianBlur.cs

@ -44,6 +44,34 @@ namespace ImageProcessor.Filters
this.sigma = sigma;
}
/// <summary>
/// Initializes a new instance of the <see cref="GuassianBlur"/> class.
/// </summary>
/// <param name="radius">
/// The 'radius' value representing the size of the area to sample.
/// </param>
public GuassianBlur(int radius)
{
this.kernelSize = (radius * 2) + 1;
this.sigma = radius;
}
/// <summary>
/// Initializes a new instance of the <see cref="GuassianBlur"/> class.
/// </summary>
/// <param name="sigma">
/// The 'sigma' value representing the weight of the blur.
/// </param>
/// <param name="radius">
/// The 'radius' value representing the size of the area to sample.
/// This should be at least twice the sigma value.
/// </param>
public GuassianBlur(float sigma, int radius)
{
this.kernelSize = (radius * 2) + 1;
this.sigma = sigma;
}
/// <inheritdoc/>
public override float[,] KernelX => this.kernelX;

7
src/ImageProcessor/ImageExtensions.cs

@ -1,4 +1,4 @@
// <copyright file="ImageFilterExtensions.cs" company="James South">
// <copyright file="ImageExtensions.cs" company="James South">
// Copyright (c) James South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
@ -62,6 +62,7 @@ namespace ImageProcessor
/// <summary>
/// Applies the collection of processors to the image.
/// <remarks>This method does not resize the target image.</remarks>
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="sourceRectangle">
@ -95,6 +96,10 @@ namespace ImageProcessor
/// <summary>
/// Applies the collection of processors to the image.
/// <remarks>
/// This method does will resize the target image if the source and target
/// rectangles are different.
/// </remarks>
/// </summary>
/// <param name="source">The source image. Cannot be null.</param>
/// <param name="width">The target image width.</param>

1
src/ImageProcessor/ImageProcessor.csproj

@ -235,6 +235,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Numerics\Size.cs" />
<Compile Include="Filters\Convolution\ConvolutionFilter.cs" />
<Compile Include="Samplers\Crop.cs" />
<Compile Include="Samplers\Resamplers\Lanczos5Resampler.cs" />
<Compile Include="Samplers\Resamplers\Lanczos8Resampler.cs" />
<Compile Include="Samplers\Resamplers\WelchResampler.cs" />

44
src/ImageProcessor/Samplers/Crop.cs

@ -0,0 +1,44 @@
// <copyright file="Crop.cs" company="James South">
// Copyright (c) James South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageProcessor.Samplers
{
using System.Threading.Tasks;
/// <summary>
/// Provides methods to allow the cropping of an image.
/// </summary>
public class Crop : ParallelImageProcessor
{
/// <inheritdoc/>
protected override void Apply(
ImageBase target,
ImageBase source,
Rectangle targetRectangle,
Rectangle sourceRectangle,
int startY,
int endY)
{
int targetY = targetRectangle.Y;
int targetBottom = targetRectangle.Bottom;
int startX = targetRectangle.X;
int endX = targetRectangle.Right;
Parallel.For(
startY,
endY,
y =>
{
if (y >= targetY && y < targetBottom)
{
for (int x = startX; x < endX; x++)
{
target[x, y] = source[x, y];
}
}
});
}
}
}

46
src/ImageProcessor/Samplers/ImageSampleExtensions.cs

@ -36,7 +36,8 @@ namespace ImageProcessor.Samplers
}
/// <summary>
/// Resizes an image to the given width and height with the given sampler.
/// Resizes an image to the given width and height with the given sampler,
/// source rectangle, and target rectangle.
/// </summary>
/// <param name="source">The image to resize.</param>
/// <param name="width">The target image width.</param>
@ -54,5 +55,48 @@ namespace ImageProcessor.Samplers
{
return source.Process(width, height, sourceRectangle, targetRectangle, new Resize(sampler));
}
/// <summary>
/// Crops an image to the given width and height.
/// </summary>
/// <param name="source">The image to resize.</param>
/// <param name="width">The target image width.</param>
/// <param name="height">The target image height.</param>
/// <returns>The <see cref="Image"/></returns>
public static Image Crop(this Image source, int width, int height)
{
return Crop(source, width, height, source.Bounds, new Rectangle(0, 0, width, height));
}
/// <summary>
/// Crops an image to the given width and height with the given source rectangle,
/// and target rectangle.
/// <remarks>
/// If the source rectangle is smaller than the target dimensions then the
/// area within the source is resized performing a zoomed crop.
/// </remarks>
/// </summary>
/// <param name="source">The image to resize.</param>
/// <param name="width">The target image width.</param>
/// <param name="height">The target image height.</param>
/// <param name="sourceRectangle">
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw.
/// </param>
/// <param name="targetRectangle">
/// The <see cref="Rectangle"/> structure that specifies the location and size of the drawn image.
/// The image is cropped to fit the rectangle.
/// </param>
/// <returns>The <see cref="Image"/></returns>
public static Image Crop(this Image source, int width, int height, Rectangle sourceRectangle, Rectangle targetRectangle)
{
if (sourceRectangle.Width < targetRectangle.Width || sourceRectangle.Height < targetRectangle.Height)
{
// If the source rectangle is smaller than the target perform a
// cropped zoom.
source = source.Resize(sourceRectangle.Width, sourceRectangle.Height);
}
return source.Process(width, height, sourceRectangle, targetRectangle, new Crop());
}
}
}

24
tests/ImageProcessor.Tests/Processors/Samplers/SamplerTests.cs

@ -1,7 +1,6 @@

namespace ImageProcessor.Tests
{
using System;
using System.Diagnostics;
using System.IO;
@ -47,7 +46,6 @@ namespace ImageProcessor.Tests
string filename = Path.GetFileNameWithoutExtension(file) + "-" + name + Path.GetExtension(file);
using (FileStream output = File.OpenWrite($"Resized/{filename}"))
{
//image.Resize(image.Width / 2, image.Height / 2, sampler).Save(output);
image.Resize(image.Width / 2, image.Height / 2, sampler).Save(output);
}
@ -56,6 +54,28 @@ namespace ImageProcessor.Tests
}
}
[Fact]
public void ImageShouldCrop()
{
if (!Directory.Exists("Cropped"))
{
Directory.CreateDirectory("Cropped");
}
foreach (string file in Files)
{
using (FileStream stream = File.OpenRead(file))
{
Image image = new Image(stream);
string filename = Path.GetFileNameWithoutExtension(file) + "-Cropped" + Path.GetExtension(file);
using (FileStream output = File.OpenWrite($"Cropped/{filename}"))
{
image.Crop(image.Width / 2, image.Height / 2).Save(output);
}
}
}
}
[Theory]
[InlineData(-2, 0)]
[InlineData(-1, 0)]

Loading…
Cancel
Save