mirror of https://github.com/SixLabors/ImageSharp
committed by
GitHub
59 changed files with 698 additions and 487 deletions
@ -1,36 +0,0 @@ |
|||
// <copyright file="IImageFilteringProcessor.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Processors |
|||
{ |
|||
using System; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates methods to alter the pixels of an image. The processor operates on the original source pixels.
|
|||
/// </summary>
|
|||
/// <typeparam name="TColor">The pixel format.</typeparam>
|
|||
public interface IImageFilteringProcessor<TColor> : IImageProcessor |
|||
where TColor : struct, IPackedPixel, IEquatable<TColor> |
|||
{ |
|||
/// <summary>
|
|||
/// Applies the process to the specified portion of the specified <see cref="ImageBase{TColor}"/>.
|
|||
/// </summary>
|
|||
/// <param name="source">The source image. Cannot be null.</param>
|
|||
/// <param name="sourceRectangle">
|
|||
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw.
|
|||
/// </param>
|
|||
/// <remarks>
|
|||
/// The method keeps the source image unchanged and returns the
|
|||
/// the result of image processing filter as new image.
|
|||
/// </remarks>
|
|||
/// <exception cref="System.ArgumentNullException">
|
|||
/// <paramref name="source"/> is null.
|
|||
/// </exception>
|
|||
/// <exception cref="System.ArgumentException">
|
|||
/// <paramref name="sourceRectangle"/> doesnt fit the dimension of the image.
|
|||
/// </exception>
|
|||
void Apply(ImageBase<TColor> source, Rectangle sourceRectangle); |
|||
} |
|||
} |
|||
@ -1,66 +0,0 @@ |
|||
// <copyright file="ImageFilteringProcessor.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Processors |
|||
{ |
|||
using System; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates methods to alter the pixels of an image. The processor operates on the original source pixels.
|
|||
/// </summary>
|
|||
/// <typeparam name="TColor">The pixel format.</typeparam>
|
|||
public abstract class ImageFilteringProcessor<TColor> : ImageProcessor<TColor>, IImageFilteringProcessor<TColor> |
|||
where TColor : struct, IPackedPixel, IEquatable<TColor> |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public void Apply(ImageBase<TColor> source, Rectangle sourceRectangle) |
|||
{ |
|||
try |
|||
{ |
|||
this.BeforeApply(source, sourceRectangle); |
|||
|
|||
this.OnApply(source, sourceRectangle); |
|||
|
|||
this.AfterApply(source, sourceRectangle); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
throw new ImageProcessingException($"An error occured when processing the image using {this.GetType().Name}. See the inner exception for more detail.", ex); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// This method is called before the process is applied to prepare the processor.
|
|||
/// </summary>
|
|||
/// <param name="source">The source image. Cannot be null.</param>
|
|||
/// <param name="sourceRectangle">
|
|||
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw.
|
|||
/// </param>
|
|||
protected virtual void BeforeApply(ImageBase<TColor> source, Rectangle sourceRectangle) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Applies the process to the specified portion of the specified <see cref="ImageBase{TColor}"/> at the specified location
|
|||
/// and with the specified size.
|
|||
/// </summary>
|
|||
/// <param name="source">The source image. Cannot be null.</param>
|
|||
/// <param name="sourceRectangle">
|
|||
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw.
|
|||
/// </param>
|
|||
protected abstract void OnApply(ImageBase<TColor> source, Rectangle sourceRectangle); |
|||
|
|||
/// <summary>
|
|||
/// This method is called after the process is applied to prepare the processor.
|
|||
/// </summary>
|
|||
/// <param name="source">The source image. Cannot be null.</param>
|
|||
/// <param name="sourceRectangle">
|
|||
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw.
|
|||
/// </param>
|
|||
protected virtual void AfterApply(ImageBase<TColor> source, Rectangle sourceRectangle) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -1,164 +0,0 @@ |
|||
// <copyright file="BootstrapperTests.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Tests |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using ImageSharp.Formats; |
|||
using Xunit; |
|||
using System.Linq; |
|||
|
|||
public class BootstrapperTests |
|||
{ |
|||
private class TestFormat : IImageFormat |
|||
{ |
|||
private IImageDecoder decoder; |
|||
private IImageEncoder encoder; |
|||
private string mimeType; |
|||
private string extension; |
|||
private IEnumerable<string> supportedExtensions; |
|||
|
|||
public TestFormat() |
|||
{ |
|||
this.decoder = new JpegDecoder(); |
|||
this.encoder = new JpegEncoder(); |
|||
this.extension = "jpg"; |
|||
this.mimeType = "image/test"; |
|||
this.supportedExtensions = new string[] { "jpg" }; |
|||
} |
|||
|
|||
public IImageDecoder Decoder { get { return this.decoder; } set { this.decoder = value; } } |
|||
|
|||
public IImageEncoder Encoder { get { return this.encoder; } set { this.encoder = value; } } |
|||
|
|||
public string MimeType { get { return this.mimeType; } set { this.mimeType = value; } } |
|||
|
|||
public string Extension { get { return this.extension; } set { this.extension = value; } } |
|||
|
|||
public IEnumerable<string> SupportedExtensions { get { return this.supportedExtensions; } set { this.supportedExtensions = value; } } |
|||
|
|||
public int HeaderSize { get { throw new NotImplementedException(); } } |
|||
|
|||
public bool IsSupportedFileFormat(byte[] header) { throw new NotImplementedException(); } |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddImageFormatGuardNull() |
|||
{ |
|||
ArgumentException exception; |
|||
|
|||
exception = Assert.Throws<ArgumentNullException>(() => |
|||
{ |
|||
Bootstrapper.AddImageFormat(null); |
|||
}); |
|||
|
|||
var format = new TestFormat(); |
|||
format.Decoder = null; |
|||
|
|||
exception = Assert.Throws<ArgumentNullException>(() => |
|||
{ |
|||
Bootstrapper.AddImageFormat(format); |
|||
}); |
|||
Assert.Contains("decoder", exception.Message); |
|||
|
|||
format = new TestFormat(); |
|||
format.Encoder = null; |
|||
|
|||
exception = Assert.Throws<ArgumentNullException>(() => |
|||
{ |
|||
Bootstrapper.AddImageFormat(format); |
|||
}); |
|||
Assert.Contains("encoder", exception.Message); |
|||
|
|||
format = new TestFormat(); |
|||
format.MimeType = null; |
|||
|
|||
exception = Assert.Throws<ArgumentNullException>(() => |
|||
{ |
|||
Bootstrapper.AddImageFormat(format); |
|||
}); |
|||
Assert.Contains("mime type", exception.Message); |
|||
|
|||
format = new TestFormat(); |
|||
format.MimeType = ""; |
|||
|
|||
exception = Assert.Throws<ArgumentException>(() => |
|||
{ |
|||
Bootstrapper.AddImageFormat(format); |
|||
}); |
|||
Assert.Contains("mime type", exception.Message); |
|||
|
|||
format = new TestFormat(); |
|||
format.Extension = null; |
|||
|
|||
exception = Assert.Throws<ArgumentNullException>(() => |
|||
{ |
|||
Bootstrapper.AddImageFormat(format); |
|||
}); |
|||
Assert.Contains("extension", exception.Message); |
|||
|
|||
format = new TestFormat(); |
|||
format.Extension = ""; |
|||
|
|||
exception = Assert.Throws<ArgumentException>(() => |
|||
{ |
|||
Bootstrapper.AddImageFormat(format); |
|||
}); |
|||
Assert.Contains("extension", exception.Message); |
|||
|
|||
format = new TestFormat(); |
|||
format.SupportedExtensions = null; |
|||
|
|||
exception = Assert.Throws<ArgumentNullException>(() => |
|||
{ |
|||
Bootstrapper.AddImageFormat(format); |
|||
}); |
|||
Assert.Contains("supported extensions", exception.Message); |
|||
|
|||
format = new TestFormat(); |
|||
format.SupportedExtensions = Enumerable.Empty<string>(); |
|||
|
|||
exception = Assert.Throws<ArgumentException>(() => |
|||
{ |
|||
Bootstrapper.AddImageFormat(format); |
|||
}); |
|||
Assert.Contains("supported extensions", exception.Message); |
|||
} |
|||
|
|||
[Fact] |
|||
public void AddImageFormatChecks() |
|||
{ |
|||
var format = new TestFormat(); |
|||
|
|||
var exception = Assert.Throws<ArgumentException>(() => |
|||
{ |
|||
Bootstrapper.AddImageFormat(format); |
|||
}); |
|||
Assert.Contains("format with the same", exception.Message); |
|||
|
|||
format.Extension = "test"; |
|||
exception = Assert.Throws<ArgumentException>(() => |
|||
{ |
|||
Bootstrapper.AddImageFormat(format); |
|||
}); |
|||
Assert.Contains("should contain", exception.Message); |
|||
|
|||
format.SupportedExtensions = new string[] { "test", "jpg" }; |
|||
exception = Assert.Throws<ArgumentException>(() => |
|||
{ |
|||
Bootstrapper.AddImageFormat(format); |
|||
}); |
|||
Assert.Contains("supports the same", exception.Message); |
|||
|
|||
format.SupportedExtensions = new string[] { "test", "" }; |
|||
exception = Assert.Throws<ArgumentException>(() => |
|||
{ |
|||
Bootstrapper.AddImageFormat(format); |
|||
}); |
|||
Assert.Contains("empty values", exception.Message); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,288 @@ |
|||
// <copyright file="ConfigurationTests.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Tests |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
|
|||
using ImageSharp.Formats; |
|||
|
|||
using Xunit; |
|||
|
|||
/// <summary>
|
|||
/// Tests the configuration class.
|
|||
/// </summary>
|
|||
public class ConfigurationTests |
|||
{ |
|||
/// <summary>
|
|||
/// Test that the default configuration is not null.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void TestDefultConfigurationIsNotNull() |
|||
{ |
|||
Assert.True(Configuration.Default != null); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Test that the default configuration parallel options is not null.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void TestDefultConfigurationParallelOptionsIsNotNull() |
|||
{ |
|||
Assert.True(Configuration.Default.ParallelOptions != null); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Test that the default configuration parallel options max degrees of parallelism matches the
|
|||
/// environment processor count.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void TestDefultConfigurationMaxDegreeOfParallelism() |
|||
{ |
|||
Assert.True(Configuration.Default.ParallelOptions.MaxDegreeOfParallelism == Environment.ProcessorCount); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Test that the default configuration parallel options is not null.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void TestDefultConfigurationImageFormatsIsNotNull() |
|||
{ |
|||
Assert.True(Configuration.Default.ImageFormats != null); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests the <see cref="M:Configuration.AddImageFormat"/> method throws an exception
|
|||
/// when the format is null.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void TestAddImageFormatThrowsWithNullFormat() |
|||
{ |
|||
Assert.Throws<ArgumentNullException>(() => |
|||
{ |
|||
Configuration.Default.AddImageFormat(null); |
|||
}); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests the <see cref="M:Configuration.AddImageFormat"/> method throws an exception
|
|||
/// when the encoder is null.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void TestAddImageFormatThrowsWithNullEncoder() |
|||
{ |
|||
TestFormat format = new TestFormat { Encoder = null }; |
|||
|
|||
Assert.Throws<ArgumentNullException>(() => |
|||
{ |
|||
Configuration.Default.AddImageFormat(format); |
|||
}); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests the <see cref="M:Configuration.AddImageFormat"/> method throws an exception
|
|||
/// when the decoder is null.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void TestAddImageFormatThrowsWithNullDecoder() |
|||
{ |
|||
TestFormat format = new TestFormat { Decoder = null }; |
|||
|
|||
Assert.Throws<ArgumentNullException>(() => |
|||
{ |
|||
Configuration.Default.AddImageFormat(format); |
|||
}); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests the <see cref="M:Configuration.AddImageFormat"/> method throws an exception
|
|||
/// when the mime type is null or an empty string.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void TestAddImageFormatThrowsWithNullOrEmptyMimeType() |
|||
{ |
|||
TestFormat format = new TestFormat { MimeType = null }; |
|||
|
|||
Assert.Throws<ArgumentNullException>(() => |
|||
{ |
|||
Configuration.Default.AddImageFormat(format); |
|||
}); |
|||
|
|||
format = new TestFormat { MimeType = string.Empty }; |
|||
|
|||
Assert.Throws<ArgumentException>(() => |
|||
{ |
|||
Configuration.Default.AddImageFormat(format); |
|||
}); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests the <see cref="M:Configuration.AddImageFormat"/> method throws an exception
|
|||
/// when the extension is null or an empty string.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void TestAddImageFormatThrowsWithNullOrEmptyExtension() |
|||
{ |
|||
TestFormat format = new TestFormat { Extension = null }; |
|||
|
|||
Assert.Throws<ArgumentNullException>(() => |
|||
{ |
|||
Configuration.Default.AddImageFormat(format); |
|||
}); |
|||
|
|||
format = new TestFormat { Extension = string.Empty }; |
|||
|
|||
Assert.Throws<ArgumentException>(() => |
|||
{ |
|||
Configuration.Default.AddImageFormat(format); |
|||
}); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests the <see cref="M:Configuration.AddImageFormat"/> method throws an exception
|
|||
/// when the supported extensions list is null or empty.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void TestAddImageFormatThrowsWenSupportedExtensionsIsNullOrEmpty() |
|||
{ |
|||
TestFormat format = new TestFormat { SupportedExtensions = null }; |
|||
|
|||
Assert.Throws<ArgumentNullException>(() => |
|||
{ |
|||
Configuration.Default.AddImageFormat(format); |
|||
}); |
|||
|
|||
format = new TestFormat { SupportedExtensions = Enumerable.Empty<string>() }; |
|||
|
|||
Assert.Throws<ArgumentException>(() => |
|||
{ |
|||
Configuration.Default.AddImageFormat(format); |
|||
}); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests the <see cref="M:Configuration.AddImageFormat"/> method throws an exception
|
|||
/// when the supported extensions list does not contain the default extension.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void TestAddImageFormatThrowsWithoutDefaultExtension() |
|||
{ |
|||
TestFormat format = new TestFormat { Extension = "test" }; |
|||
|
|||
Assert.Throws<ArgumentException>(() => |
|||
{ |
|||
Configuration.Default.AddImageFormat(format); |
|||
}); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests the <see cref="M:Configuration.AddImageFormat"/> method throws an exception
|
|||
/// when the supported extensions list contains an empty string.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void TestAddImageFormatThrowsWithEmptySupportedExtension() |
|||
{ |
|||
TestFormat format = new TestFormat |
|||
{ |
|||
Extension = "test", |
|||
SupportedExtensions = new[] { "test", string.Empty } |
|||
}; |
|||
|
|||
Assert.Throws<ArgumentException>(() => |
|||
{ |
|||
Configuration.Default.AddImageFormat(format); |
|||
}); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Test that the <see cref="M:Configuration.AddImageFormat"/> method ignores adding duplicate image formats.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void TestConfigurationIgnoresDuplicateImageFormats() |
|||
{ |
|||
Configuration.Default.AddImageFormat(new PngFormat()); |
|||
Configuration.Default.AddImageFormat(new PngFormat()); |
|||
|
|||
Assert.True(Configuration.Default.ImageFormats.Count(i => i.GetType() == typeof(PngFormat)) == 1); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Test that the default image constructors use default configuration.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void TestImageUsesDefaultConfiguration() |
|||
{ |
|||
Configuration.Default.AddImageFormat(new PngFormat()); |
|||
|
|||
Image image = new Image(1, 1); |
|||
Assert.Equal(image.Configuration.ParallelOptions, Configuration.Default.ParallelOptions); |
|||
Assert.Equal(image.Configuration.ImageFormats, Configuration.Default.ImageFormats); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Test that the default image constructor copies the configuration.
|
|||
/// </summary>
|
|||
[Fact] |
|||
public void TestImageCopiesConfiguration() |
|||
{ |
|||
Configuration.Default.AddImageFormat(new PngFormat()); |
|||
|
|||
Image image = new Image(1, 1); |
|||
Image image2 = new Image(image); |
|||
Assert.Equal(image2.Configuration.ParallelOptions, image.Configuration.ParallelOptions); |
|||
Assert.True(image2.Configuration.ImageFormats.SequenceEqual(image.Configuration.ImageFormats)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// A test image format for testing the configuration.
|
|||
/// </summary>
|
|||
private class TestFormat : IImageFormat |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="TestFormat"/> class.
|
|||
/// </summary>
|
|||
public TestFormat() |
|||
{ |
|||
this.Decoder = new JpegDecoder(); |
|||
this.Encoder = new JpegEncoder(); |
|||
this.Extension = "jpg"; |
|||
this.MimeType = "image/test"; |
|||
this.SupportedExtensions = new[] { "jpg" }; |
|||
} |
|||
|
|||
/// <inheritdoc />
|
|||
public IImageDecoder Decoder { get; set; } |
|||
|
|||
/// <inheritdoc />
|
|||
public IImageEncoder Encoder { get; set; } |
|||
|
|||
/// <inheritdoc />
|
|||
public string MimeType { get; set; } |
|||
|
|||
/// <inheritdoc />
|
|||
public string Extension { get; set; } |
|||
|
|||
/// <inheritdoc />
|
|||
public IEnumerable<string> SupportedExtensions { get; set; } |
|||
|
|||
/// <inheritdoc />
|
|||
public int HeaderSize |
|||
{ |
|||
get |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
} |
|||
|
|||
/// <inheritdoc />
|
|||
public bool IsSupportedFileFormat(byte[] header) |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue