Browse Source

Add restriction to samplers.

Former-commit-id: a1f5890e16408916332a4aa3e6cb0454554a9964
Former-commit-id: 01019e4c20cb33666ac5407482ee77d77a9632a2
Former-commit-id: d6f42c3a126e0817a6840d3eccf9f62faf5696ba
pull/1/head
James Jackson-South 10 years ago
parent
commit
86e367b4db
  1. 10
      src/ImageProcessorCore/ImageExtensions.cs
  2. 2
      src/ImageProcessorCore/Samplers/Crop.cs
  3. 2
      src/ImageProcessorCore/Samplers/EntropyCrop.cs
  4. 14
      src/ImageProcessorCore/Samplers/IImageSampler.cs
  5. 15
      src/ImageProcessorCore/Samplers/ImageSampler.cs
  6. 4
      src/ImageProcessorCore/Samplers/ImageSamplerExtensions.cs
  7. 2
      src/ImageProcessorCore/Samplers/Resampler.cs
  8. 5
      tests/ImageProcessorCore.Tests/Processors/Filters/FilterTests.cs
  9. 7
      tests/ImageProcessorCore.Tests/Processors/ProcessorTestBase.cs
  10. 12
      tests/ImageProcessorCore.Tests/Processors/Samplers/SamplerTests.cs

10
src/ImageProcessorCore/ImageExtensions.cs

@ -10,6 +10,8 @@ namespace ImageProcessorCore
using Formats;
using ImageProcessorCore.Samplers;
/// <summary>
/// Exstension methods for the <see cref="Image"/> type.
/// </summary>
@ -89,7 +91,7 @@ namespace ImageProcessorCore
/// <param name="height">The target image height.</param>
/// <param name="processors">Any processors to apply to the image.</param>
/// <returns>The <see cref="Image"/>.</returns>
public static Image Process(this Image source, int width, int height, params IImageProcessor[] processors)
public static Image Process(this Image source, int width, int height, params IImageSampler[] processors)
{
return Process(source, width, height, source.Bounds, default(Rectangle), processors);
}
@ -113,12 +115,12 @@ namespace ImageProcessorCore
/// </param>
/// <param name="processors">Any processors to apply to the image.</param>
/// <returns>The <see cref="Image"/>.</returns>
public static Image Process(this Image source, int width, int height, Rectangle sourceRectangle, Rectangle targetRectangle, params IImageProcessor[] processors)
public static Image Process(this Image source, int width, int height, Rectangle sourceRectangle, Rectangle targetRectangle, params IImageSampler[] processors)
{
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (IImageProcessor filter in processors)
foreach (IImageSampler sampler in processors)
{
source = PerformAction(source, false, (sourceImage, targetImage) => filter.Apply(targetImage, sourceImage, width, height, targetRectangle, sourceRectangle));
source = PerformAction(source, false, (sourceImage, targetImage) => sampler.Apply(targetImage, sourceImage, width, height, targetRectangle, sourceRectangle));
}
return source;

2
src/ImageProcessorCore/Samplers/Crop.cs

@ -10,7 +10,7 @@ namespace ImageProcessorCore.Samplers
/// <summary>
/// Provides methods to allow the cropping of an image.
/// </summary>
public class Crop : ParallelImageProcessor
public class Crop : ImageSampler
{
/// <inheritdoc/>
protected override void Apply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY)

2
src/ImageProcessorCore/Samplers/EntropyCrop.cs

@ -14,7 +14,7 @@ namespace ImageProcessorCore.Samplers
/// Provides methods to allow the cropping of an image to preserve areas of highest
/// entropy.
/// </summary>
public class EntropyCrop : ParallelImageProcessor
public class EntropyCrop : ImageSampler
{
/// <summary>
/// The rectangle for cropping

14
src/ImageProcessorCore/Samplers/IImageSampler.cs

@ -0,0 +1,14 @@
// <copyright file="IImageSampler.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageProcessorCore.Samplers
{
/// <summary>
/// Acts as a marker for generic parameters that require an image sampler.
/// </summary>
public interface IImageSampler : IImageProcessor
{
}
}

15
src/ImageProcessorCore/Samplers/ImageSampler.cs

@ -0,0 +1,15 @@
// <copyright file="ImageSampler.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageProcessorCore.Samplers
{
/// <summary>
/// Applies sampling methods to an image.
/// All processors requiring resampling or resizing should inherit from this.
/// </summary>
public abstract class ImageSampler : ParallelImageProcessor, IImageSampler
{
}
}

4
src/ImageProcessorCore/Samplers/ImageSampleExtensions.cs → src/ImageProcessorCore/Samplers/ImageSamplerExtensions.cs

@ -1,4 +1,4 @@
// <copyright file="ImageSampleExtensions.cs" company="James Jackson-South">
// <copyright file="ImageSamplerExtensions.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
@ -8,7 +8,7 @@ namespace ImageProcessorCore.Samplers
/// <summary>
/// Extensions methods for <see cref="Image"/> to apply samplers to the image.
/// </summary>
public static class ImageSampleExtensions
public static class ImageSamplerExtensions
{
/// <summary>
/// Crops an image to the given width and height.

2
src/ImageProcessorCore/Samplers/Resampler.cs

@ -12,7 +12,7 @@ namespace ImageProcessorCore.Samplers
/// <summary>
/// Provides methods that allow the resampling of images using various algorithms.
/// </summary>
public abstract class Resampler : ParallelImageProcessor
public abstract class Resampler : ImageSampler
{
/// <summary>
/// Initializes a new instance of the <see cref="Resampler"/> class.

5
tests/ImageProcessorCore.Tests/Processors/Filters/FilterTests.cs

@ -74,10 +74,5 @@ namespace ImageProcessorCore.Tests
}
}
}
private void ProgressUpdate(object sender, ProgressEventArgs e)
{
Assert.InRange(e.RowsProcessed, 1, e.TotalRows);
}
}
}

7
tests/ImageProcessorCore.Tests/Processors/ProcessorTestBase.cs

@ -9,6 +9,8 @@ namespace ImageProcessorCore.Tests
{
using System.Collections.Generic;
using Xunit;
/// <summary>
/// The processor test base.
/// </summary>
@ -29,5 +31,10 @@ namespace ImageProcessorCore.Tests
"TestImages/Formats/Gif/rings.gif",
//"TestImages/Formats/Gif/giphy.gif" // Perf: Enable for local testing only
};
protected void ProgressUpdate(object sender, ProgressEventArgs e)
{
Assert.InRange(e.RowsProcessed, 1, e.TotalRows);
}
}
}

12
tests/ImageProcessorCore.Tests/Processors/Samplers/SamplerTests.cs

@ -30,7 +30,7 @@
//{ "Welch", new WelchResampler() }
};
public static readonly TheoryData<string, IImageProcessor> Samplers = new TheoryData<string, IImageProcessor>
public static readonly TheoryData<string, IImageSampler> Samplers = new TheoryData<string, IImageSampler>
{
{ "Resize", new Resize(new BicubicResampler()) },
{ "Crop", new Crop() }
@ -47,7 +47,7 @@
[Theory]
[MemberData("Samplers")]
public void SampleImage(string name, IImageProcessor processor)
public void SampleImage(string name, IImageSampler processor)
{
if (!Directory.Exists("TestOutput/Sample"))
{
@ -64,7 +64,8 @@
using (FileStream output = File.OpenWrite($"TestOutput/Sample/{ Path.GetFileName(filename) }"))
{
processor.OnProgress += this.ProgressUpdate;
image.Process(image.Width / 2, image.Height / 2, processor).Save(output);
image = image.Process(image.Width / 2, image.Height / 2, processor);
image.Save(output);
processor.OnProgress -= this.ProgressUpdate;
}
@ -269,10 +270,5 @@
Assert.Equal(result, expected);
}
private void ProgressUpdate(object sender, ProgressEventArgs e)
{
Assert.InRange(e.RowsProcessed, 1, e.TotalRows);
}
}
}

Loading…
Cancel
Save