Browse Source

Add ability to set custom Bootstrapper.

This allows the setting of custom formats and parallel processing rules.
pull/68/head
James Jackson-South 10 years ago
parent
commit
050b4c5f45
  1. 1
      ImageSharp.sln
  2. 2
      Settings.StyleCop
  3. 80
      src/ImageSharp/Bootstrapper.cs
  4. 1
      src/ImageSharp/Formats/IImageFormat.cs
  5. 6
      src/ImageSharp/Formats/Jpg/JpegDecoderCore.cs
  6. 27
      src/ImageSharp/Image.cs
  7. 20
      src/ImageSharp/Image/ImageBase{TColor}.cs
  8. 10
      src/ImageSharp/Image/ImageFrame{TColor}.cs
  9. 5
      src/ImageSharp/Image/ImageProcessingExtensions.cs
  10. 46
      src/ImageSharp/Image/Image{TColor}.cs
  11. 2
      src/ImageSharp/ImageProcessor.cs
  12. 6
      src/ImageSharp/Quantizers/QuantizedImage.cs
  13. 2
      src/ImageSharp/Quantizers/Wu/WuQuantizer.cs
  14. 2
      tests/ImageSharp.Benchmarks/Image/CopyPixels.cs
  15. 28
      tests/ImageSharp.Tests/BootstrapperTests.cs
  16. 24
      tests/ImageSharp.Tests/TestBase.cs
  17. 2
      tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs

1
ImageSharp.sln

@ -21,6 +21,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionIt
features.md = features.md features.md = features.md
global.json = global.json global.json = global.json
ImageSharp.ruleset = ImageSharp.ruleset ImageSharp.ruleset = ImageSharp.ruleset
ImageSharp.sln.DotSettings = ImageSharp.sln.DotSettings
NuGet.config = NuGet.config NuGet.config = NuGet.config
build\package.json = build\package.json build\package.json = build\package.json
README.md = README.md README.md = README.md

2
Settings.StyleCop

@ -39,6 +39,8 @@
<Value>desensitivity</Value> <Value>desensitivity</Value>
<Value>premultiplied</Value> <Value>premultiplied</Value>
<Value>endianness</Value> <Value>endianness</Value>
<Value>bootstrapper</Value>
<Value>thresholding</Value>
</CollectionProperty> </CollectionProperty>
</GlobalSettings> </GlobalSettings>
<Analyzers> <Analyzers>

80
src/ImageSharp/Bootstrapper.cs

@ -16,12 +16,12 @@ namespace ImageSharp
/// <summary> /// <summary>
/// Provides initialization code which allows extending the library. /// Provides initialization code which allows extending the library.
/// </summary> /// </summary>
public static class Bootstrapper public class Bootstrapper
{ {
/// <summary> /// <summary>
/// The list of supported <see cref="IImageFormat"/>. /// A lazily initialized bootstrapper default instance.
/// </summary> /// </summary>
private static readonly List<IImageFormat> ImageFormatsList; private static readonly Lazy<Bootstrapper> Lazy = new Lazy<Bootstrapper>(() => new Bootstrapper());
/// <summary> /// <summary>
/// An object that can be used to synchronize access to the <see cref="Bootstrapper"/>. /// An object that can be used to synchronize access to the <see cref="Bootstrapper"/>.
@ -29,41 +29,35 @@ namespace ImageSharp
private static readonly object SyncRoot = new object(); private static readonly object SyncRoot = new object();
/// <summary> /// <summary>
/// Initializes static members of the <see cref="Bootstrapper"/> class. /// The list of supported <see cref="IImageFormat"/>.
/// </summary> /// </summary>
static Bootstrapper() private readonly List<IImageFormat> imageFormatsList = new List<IImageFormat>();
{
ImageFormatsList = new List<IImageFormat> /// <summary>
{ /// Gets the default <see cref="Bootstrapper"/> instance.
new BmpFormat(), /// </summary>
new JpegFormat(), public static Bootstrapper Default { get; } = Lazy.Value;
new PngFormat(),
new GifFormat()
};
SetMaxHeaderSize();
ParallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount };
}
/// <summary> /// <summary>
/// Gets the collection of supported <see cref="IImageFormat"/> /// Gets the collection of supported <see cref="IImageFormat"/>
/// </summary> /// </summary>
public static IReadOnlyCollection<IImageFormat> ImageFormats => new ReadOnlyCollection<IImageFormat>(ImageFormatsList); public IReadOnlyCollection<IImageFormat> ImageFormats => new ReadOnlyCollection<IImageFormat>(this.imageFormatsList);
/// <summary> /// <summary>
/// Gets the global parallel options for processing tasks in parallel. /// Gets the global parallel options for processing tasks in parallel.
/// </summary> /// </summary>
public static ParallelOptions ParallelOptions { get; } public ParallelOptions ParallelOptions { get; } = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount };
/// <summary> /// <summary>
/// Gets the maximum header size of all formats. /// Gets the maximum header size of all formats.
/// </summary> /// </summary>
internal static int MaxHeaderSize { get; private set; } internal int MaxHeaderSize { get; private set; }
/// <summary> /// <summary>
/// Adds a new <see cref="IImageFormat"/> to the collection of supported image formats. /// Adds a new <see cref="IImageFormat"/> to the collection of supported image formats.
/// </summary> /// </summary>
/// <param name="format">The new format to add.</param> /// <param name="format">The new format to add.</param>
public static void AddImageFormat(IImageFormat format) public void AddImageFormat(IImageFormat format)
{ {
Guard.NotNull(format, nameof(format)); Guard.NotNull(format, nameof(format));
Guard.NotNull(format.Encoder, nameof(format), "The encoder should not be null."); Guard.NotNull(format.Encoder, nameof(format), "The encoder should not be null.");
@ -72,50 +66,72 @@ namespace ImageSharp
Guard.NotNullOrEmpty(format.Extension, nameof(format), "The extension should not be null or empty."); Guard.NotNullOrEmpty(format.Extension, nameof(format), "The extension should not be null or empty.");
Guard.NotNullOrEmpty(format.SupportedExtensions, nameof(format), "The supported extensions not be null or empty."); Guard.NotNullOrEmpty(format.SupportedExtensions, nameof(format), "The supported extensions not be null or empty.");
AddImageFormatLocked(format); this.AddImageFormatLocked(format);
} }
private static void AddImageFormatLocked(IImageFormat format) /// <summary>
/// Adds image format. The class is locked to make it thread safe.
/// </summary>
/// <param name="format">The image format.</param>
private void AddImageFormatLocked(IImageFormat format)
{ {
lock (SyncRoot) lock (SyncRoot)
{ {
GuardDuplicate(format); if (this.GuardDuplicate(format))
{
ImageFormatsList.Add(format); this.imageFormatsList.Add(format);
SetMaxHeaderSize(); this.SetMaxHeaderSize();
}
} }
} }
private static void GuardDuplicate(IImageFormat format) /// <summary>
/// Checks to ensure duplicate image formats are not added.
/// </summary>
/// <param name="format">The image format.</param>
/// <exception cref="ArgumentException">Thrown if a duplicate is added.</exception>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
private bool GuardDuplicate(IImageFormat format)
{ {
if (!format.SupportedExtensions.Contains(format.Extension, StringComparer.OrdinalIgnoreCase)) if (!format.SupportedExtensions.Contains(format.Extension, StringComparer.OrdinalIgnoreCase))
{ {
throw new ArgumentException("The supported extensions should contain the default extension.", nameof(format)); throw new ArgumentException("The supported extensions should contain the default extension.", nameof(format));
} }
// ReSharper disable once ConvertClosureToMethodGroup
// Prevents method group allocation
if (format.SupportedExtensions.Any(e => string.IsNullOrWhiteSpace(e))) if (format.SupportedExtensions.Any(e => string.IsNullOrWhiteSpace(e)))
{ {
throw new ArgumentException("The supported extensions should not contain empty values.", nameof(format)); throw new ArgumentException("The supported extensions should not contain empty values.", nameof(format));
} }
foreach (var imageFormat in ImageFormatsList) // If there is already a format with the same extension or a format that supports that
// extension return false.
foreach (IImageFormat imageFormat in this.imageFormatsList)
{ {
if (imageFormat.Extension.Equals(format.Extension, StringComparison.OrdinalIgnoreCase)) if (imageFormat.Extension.Equals(format.Extension, StringComparison.OrdinalIgnoreCase))
{ {
throw new ArgumentException("There is already a format with the same extension.", nameof(format)); return false;
} }
if (imageFormat.SupportedExtensions.Intersect(format.SupportedExtensions, StringComparer.OrdinalIgnoreCase).Any()) if (imageFormat.SupportedExtensions.Intersect(format.SupportedExtensions, StringComparer.OrdinalIgnoreCase).Any())
{ {
throw new ArgumentException("There is already a format that supports the same extension.", nameof(format)); return false;
} }
} }
return true;
} }
private static void SetMaxHeaderSize() /// <summary>
/// Sets max header size.
/// </summary>
private void SetMaxHeaderSize()
{ {
MaxHeaderSize = ImageFormatsList.Max(x => x.HeaderSize); this.MaxHeaderSize = this.imageFormatsList.Max(x => x.HeaderSize);
} }
} }
} }

1
src/ImageSharp/Formats/IImageFormat.cs

@ -9,6 +9,7 @@ namespace ImageSharp.Formats
/// <summary> /// <summary>
/// Encapsulates a supported image format, providing means to encode and decode an image. /// Encapsulates a supported image format, providing means to encode and decode an image.
/// Individual formats implements in this interface must be registered in the <see cref="Bootstrapper"/>
/// </summary> /// </summary>
public interface IImageFormat public interface IImageFormat
{ {

6
src/ImageSharp/Formats/Jpg/JpegDecoderCore.cs

@ -687,7 +687,7 @@ namespace ImageSharp.Formats
Parallel.For( Parallel.For(
0, 0,
height, height,
Bootstrapper.ParallelOptions, image.Bootstrapper.ParallelOptions,
y => y =>
{ {
int yoff = this.grayImage.GetRowOffset(y); int yoff = this.grayImage.GetRowOffset(y);
@ -723,7 +723,7 @@ namespace ImageSharp.Formats
Parallel.For( Parallel.For(
0, 0,
height, height,
Bootstrapper.ParallelOptions, image.Bootstrapper.ParallelOptions,
y => y =>
{ {
// TODO: Simplify + optimize + share duplicate code across converter methods // TODO: Simplify + optimize + share duplicate code across converter methods
@ -764,7 +764,7 @@ namespace ImageSharp.Formats
Parallel.For( Parallel.For(
0, 0,
height, height,
Bootstrapper.ParallelOptions, image.Bootstrapper.ParallelOptions,
y => y =>
{ {
// TODO: Simplify + optimize + share duplicate code across converter methods // TODO: Simplify + optimize + share duplicate code across converter methods

27
src/ImageSharp/Image.cs

@ -18,7 +18,11 @@ namespace ImageSharp
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Image"/> class. /// Initializes a new instance of the <see cref="Image"/> class.
/// </summary> /// </summary>
public Image() /// <param name="bootstrapper">
/// The bootstrapper providing initialization code which allows extending the library.
/// </param>
public Image(Bootstrapper bootstrapper = null)
: base(bootstrapper)
{ {
} }
@ -28,8 +32,11 @@ namespace ImageSharp
/// </summary> /// </summary>
/// <param name="width">The width of the image in pixels.</param> /// <param name="width">The width of the image in pixels.</param>
/// <param name="height">The height of the image in pixels.</param> /// <param name="height">The height of the image in pixels.</param>
public Image(int width, int height) /// <param name="bootstrapper">
: base(width, height) /// The bootstrapper providing initialization code which allows extending the library.
/// </param>
public Image(int width, int height, Bootstrapper bootstrapper = null)
: base(width, height, bootstrapper)
{ {
} }
@ -39,9 +46,12 @@ namespace ImageSharp
/// <param name="stream"> /// <param name="stream">
/// The stream containing image information. /// The stream containing image information.
/// </param> /// </param>
/// <param name="bootstrapper">
/// The bootstrapper providing initialization code which allows extending the library.
/// </param>
/// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="stream"/> is null.</exception> /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="stream"/> is null.</exception>
public Image(Stream stream) public Image(Stream stream, Bootstrapper bootstrapper = null)
: base(stream) : base(stream, bootstrapper)
{ {
} }
@ -51,9 +61,12 @@ namespace ImageSharp
/// <param name="bytes"> /// <param name="bytes">
/// The byte array containing image information. /// The byte array containing image information.
/// </param> /// </param>
/// <param name="bootstrapper">
/// The bootstrapper providing initialization code which allows extending the library.
/// </param>
/// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="bytes"/> is null.</exception> /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="bytes"/> is null.</exception>
public Image(byte[] bytes) public Image(byte[] bytes, Bootstrapper bootstrapper = null)
: base(bytes) : base(bytes, bootstrapper)
{ {
} }

20
src/ImageSharp/Image/ImageBase{TColor}.cs

@ -25,8 +25,12 @@ namespace ImageSharp
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ImageBase{TColor}"/> class. /// Initializes a new instance of the <see cref="ImageBase{TColor}"/> class.
/// </summary> /// </summary>
protected ImageBase() /// <param name="bootstrapper">
/// The bootstrapper providing initialization code which allows extending the library.
/// </param>
protected ImageBase(Bootstrapper bootstrapper = null)
{ {
this.Bootstrapper = bootstrapper ?? Bootstrapper.Default;
} }
/// <summary> /// <summary>
@ -34,11 +38,15 @@ namespace ImageSharp
/// </summary> /// </summary>
/// <param name="width">The width of the image in pixels.</param> /// <param name="width">The width of the image in pixels.</param>
/// <param name="height">The height of the image in pixels.</param> /// <param name="height">The height of the image in pixels.</param>
/// <param name="bootstrapper">
/// The bootstrapper providing initialization code which allows extending the library.
/// </param>
/// <exception cref="System.ArgumentOutOfRangeException"> /// <exception cref="System.ArgumentOutOfRangeException">
/// Thrown if either <paramref name="width"/> or <paramref name="height"/> are less than or equal to 0. /// Thrown if either <paramref name="width"/> or <paramref name="height"/> are less than or equal to 0.
/// </exception> /// </exception>
protected ImageBase(int width, int height) protected ImageBase(int width, int height, Bootstrapper bootstrapper = null)
{ {
this.Bootstrapper = bootstrapper ?? Bootstrapper.Default;
this.InitPixels(width, height); this.InitPixels(width, height);
} }
@ -95,6 +103,11 @@ namespace ImageSharp
/// <inheritdoc/> /// <inheritdoc/>
public int FrameDelay { get; set; } public int FrameDelay { get; set; }
/// <summary>
/// Gets the bootstrapper providing initialization code which allows extending the library.
/// </summary>
public Bootstrapper Bootstrapper { get; private set; }
/// <inheritdoc/> /// <inheritdoc/>
public void InitPixels(int width, int height) public void InitPixels(int width, int height)
{ {
@ -157,8 +170,9 @@ namespace ImageSharp
/// </param> /// </param>
protected void CopyProperties(ImageBase<TColor> other) protected void CopyProperties(ImageBase<TColor> other)
{ {
this.Bootstrapper = other.Bootstrapper;
this.Quality = other.Quality; this.Quality = other.Quality;
this.FrameDelay = other.FrameDelay; this.FrameDelay = other.FrameDelay;
} }
} }
} }

10
src/ImageSharp/Image/ImageFrame{TColor}.cs

@ -19,7 +19,11 @@ namespace ImageSharp
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ImageFrame{TColor}"/> class. /// Initializes a new instance of the <see cref="ImageFrame{TColor}"/> class.
/// </summary> /// </summary>
public ImageFrame() /// <param name="bootstrapper">
/// The bootstrapper providing initialization code which allows extending the library.
/// </param>
public ImageFrame(Bootstrapper bootstrapper = null)
: base(bootstrapper)
{ {
} }
@ -49,7 +53,7 @@ namespace ImageSharp
{ {
scaleFunc = PackedPixelConverterHelper.ComputeScaleFunction<TColor, TColor2>(scaleFunc); scaleFunc = PackedPixelConverterHelper.ComputeScaleFunction<TColor, TColor2>(scaleFunc);
ImageFrame<TColor2> target = new ImageFrame<TColor2> ImageFrame<TColor2> target = new ImageFrame<TColor2>(this.Bootstrapper)
{ {
Quality = this.Quality, Quality = this.Quality,
FrameDelay = this.FrameDelay FrameDelay = this.FrameDelay
@ -63,7 +67,7 @@ namespace ImageSharp
Parallel.For( Parallel.For(
0, 0,
target.Height, target.Height,
Bootstrapper.ParallelOptions, this.Bootstrapper.ParallelOptions,
y => y =>
{ {
for (int x = 0; x < target.Width; x++) for (int x = 0; x < target.Width; x++)

5
src/ImageSharp/Image/ImageProcessingExtensions.cs

@ -41,6 +41,11 @@ namespace ImageSharp
internal static Image<TColor> Process<TColor>(this Image<TColor> source, Rectangle sourceRectangle, IImageFilteringProcessor<TColor> processor) internal static Image<TColor> Process<TColor>(this Image<TColor> source, Rectangle sourceRectangle, IImageFilteringProcessor<TColor> processor)
where TColor : struct, IPackedPixel, IEquatable<TColor> where TColor : struct, IPackedPixel, IEquatable<TColor>
{ {
if (processor.ParallelOptions == null)
{
processor.ParallelOptions = source.Bootstrapper.ParallelOptions;
}
return PerformAction(source, (sourceImage) => processor.Apply(sourceImage, sourceRectangle)); return PerformAction(source, (sourceImage) => processor.Apply(sourceImage, sourceRectangle));
} }

46
src/ImageSharp/Image/Image{TColor}.cs

@ -40,9 +40,14 @@ namespace ImageSharp
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Image{TColor}"/> class. /// Initializes a new instance of the <see cref="Image{TColor}"/> class.
/// </summary> /// </summary>
public Image() /// <param name="bootstrapper">
/// The bootstrapper providing initialization code which allows extending the library.
/// </param>
public Image(Bootstrapper bootstrapper = null)
: base(bootstrapper)
{ {
this.CurrentImageFormat = Bootstrapper.ImageFormats.First(f => f.GetType() == typeof(PngFormat)); // We want to throw here.
this.CurrentImageFormat = this.Bootstrapper.ImageFormats.First();
} }
/// <summary> /// <summary>
@ -51,10 +56,13 @@ namespace ImageSharp
/// </summary> /// </summary>
/// <param name="width">The width of the image in pixels.</param> /// <param name="width">The width of the image in pixels.</param>
/// <param name="height">The height of the image in pixels.</param> /// <param name="height">The height of the image in pixels.</param>
public Image(int width, int height) /// <param name="bootstrapper">
: base(width, height) /// The bootstrapper providing initialization code which allows extending the library.
/// </param>
public Image(int width, int height, Bootstrapper bootstrapper = null)
: base(width, height, bootstrapper)
{ {
this.CurrentImageFormat = Bootstrapper.ImageFormats.First(f => f.GetType() == typeof(PngFormat)); this.CurrentImageFormat = this.Bootstrapper.ImageFormats.First();
} }
/// <summary> /// <summary>
@ -63,8 +71,12 @@ namespace ImageSharp
/// <param name="stream"> /// <param name="stream">
/// The stream containing image information. /// The stream containing image information.
/// </param> /// </param>
/// <param name="bootstrapper">
/// The bootstrapper providing initialization code which allows extending the library.
/// </param>
/// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="stream"/> is null.</exception> /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="stream"/> is null.</exception>
public Image(Stream stream) public Image(Stream stream, Bootstrapper bootstrapper = null)
: base(bootstrapper)
{ {
Guard.NotNull(stream, nameof(stream)); Guard.NotNull(stream, nameof(stream));
this.Load(stream); this.Load(stream);
@ -76,8 +88,12 @@ namespace ImageSharp
/// <param name="bytes"> /// <param name="bytes">
/// The byte array containing image information. /// The byte array containing image information.
/// </param> /// </param>
/// <param name="bootstrapper">
/// The bootstrapper providing initialization code which allows extending the library.
/// </param>
/// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="bytes"/> is null.</exception> /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="bytes"/> is null.</exception>
public Image(byte[] bytes) public Image(byte[] bytes, Bootstrapper bootstrapper = null)
: base(bootstrapper)
{ {
Guard.NotNull(bytes, nameof(bytes)); Guard.NotNull(bytes, nameof(bytes));
@ -293,7 +309,7 @@ namespace ImageSharp
{ {
scaleFunc = PackedPixelConverterHelper.ComputeScaleFunction<TColor, TColor2>(scaleFunc); scaleFunc = PackedPixelConverterHelper.ComputeScaleFunction<TColor, TColor2>(scaleFunc);
Image<TColor2> target = new Image<TColor2>(this.Width, this.Height) Image<TColor2> target = new Image<TColor2>(this.Width, this.Height, this.Bootstrapper)
{ {
Quality = this.Quality, Quality = this.Quality,
FrameDelay = this.FrameDelay, FrameDelay = this.FrameDelay,
@ -309,7 +325,7 @@ namespace ImageSharp
Parallel.For( Parallel.For(
0, 0,
target.Height, target.Height,
Bootstrapper.ParallelOptions, this.Bootstrapper.ParallelOptions,
y => y =>
{ {
for (int x = 0; x < target.Width; x++) for (int x = 0; x < target.Width; x++)
@ -373,7 +389,7 @@ namespace ImageSharp
/// </exception> /// </exception>
private void Load(Stream stream) private void Load(Stream stream)
{ {
if (!Bootstrapper.ImageFormats.Any()) if (!this.Bootstrapper.ImageFormats.Any())
{ {
return; return;
} }
@ -408,7 +424,7 @@ namespace ImageSharp
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("Image cannot be loaded. Available formats:"); stringBuilder.AppendLine("Image cannot be loaded. Available formats:");
foreach (IImageFormat format in Bootstrapper.ImageFormats) foreach (IImageFormat format in this.Bootstrapper.ImageFormats)
{ {
stringBuilder.AppendLine("-" + format); stringBuilder.AppendLine("-" + format);
} }
@ -425,20 +441,20 @@ namespace ImageSharp
/// </returns> /// </returns>
private bool Decode(Stream stream) private bool Decode(Stream stream)
{ {
int maxHeaderSize = Bootstrapper.MaxHeaderSize; int maxHeaderSize = this.Bootstrapper.MaxHeaderSize;
if (maxHeaderSize <= 0) if (maxHeaderSize <= 0)
{ {
return false; return false;
} }
IImageFormat format = null; IImageFormat format;
byte[] header = ArrayPool<byte>.Shared.Rent(maxHeaderSize); byte[] header = ArrayPool<byte>.Shared.Rent(maxHeaderSize);
try try
{ {
long startPosition = stream.Position; long startPosition = stream.Position;
stream.Read(header, 0, maxHeaderSize); stream.Read(header, 0, maxHeaderSize);
stream.Position = startPosition; stream.Position = startPosition;
format = Bootstrapper.ImageFormats.FirstOrDefault(x => x.IsSupportedFileFormat(header)); format = this.Bootstrapper.ImageFormats.FirstOrDefault(x => x.IsSupportedFileFormat(header));
} }
finally finally
{ {
@ -455,4 +471,4 @@ namespace ImageSharp
return true; return true;
} }
} }
} }

2
src/ImageSharp/ImageProcessor.cs

@ -16,7 +16,7 @@ namespace ImageSharp.Processors
where TColor : struct, IPackedPixel, IEquatable<TColor> where TColor : struct, IPackedPixel, IEquatable<TColor>
{ {
/// <inheritdoc/> /// <inheritdoc/>
public virtual ParallelOptions ParallelOptions { get; set; } = Bootstrapper.ParallelOptions; public virtual ParallelOptions ParallelOptions { get; set; }
/// <inheritdoc/> /// <inheritdoc/>
public virtual bool Compand { get; set; } = false; public virtual bool Compand { get; set; } = false;

6
src/ImageSharp/Quantizers/QuantizedImage.cs

@ -71,16 +71,16 @@ namespace ImageSharp.Quantizers
Image<TColor> image = new Image<TColor>(); Image<TColor> image = new Image<TColor>();
int pixelCount = this.Pixels.Length; int pixelCount = this.Pixels.Length;
int palletCount = this.Palette.Length - 1; int palleteCount = this.Palette.Length - 1;
TColor[] pixels = new TColor[pixelCount]; TColor[] pixels = new TColor[pixelCount];
Parallel.For( Parallel.For(
0, 0,
pixelCount, pixelCount,
Bootstrapper.ParallelOptions, image.Bootstrapper.ParallelOptions,
i => i =>
{ {
TColor color = this.Palette[Math.Min(palletCount, this.Pixels[i])]; TColor color = this.Palette[Math.Min(palleteCount, this.Pixels[i])];
pixels[i] = color; pixels[i] = color;
}); });

2
src/ImageSharp/Quantizers/Wu/WuQuantizer.cs

@ -773,7 +773,7 @@ namespace ImageSharp.Quantizers
Parallel.For( Parallel.For(
0, 0,
height, height,
Bootstrapper.ParallelOptions, Bootstrapper.Default.ParallelOptions,
y => y =>
{ {
byte[] rgba = ArrayPool<byte>.Shared.Rent(4); byte[] rgba = ArrayPool<byte>.Shared.Rent(4);

2
tests/ImageSharp.Benchmarks/Image/CopyPixels.cs

@ -25,7 +25,7 @@ namespace ImageSharp.Benchmarks.Image
Parallel.For( Parallel.For(
0, 0,
source.Height, source.Height,
Bootstrapper.ParallelOptions, Bootstrapper.Default.ParallelOptions,
y => y =>
{ {
for (int x = 0; x < source.Width; x++) for (int x = 0; x < source.Width; x++)

28
tests/ImageSharp.Tests/BootstrapperTests.cs

@ -52,7 +52,7 @@ namespace ImageSharp.Tests
exception = Assert.Throws<ArgumentNullException>(() => exception = Assert.Throws<ArgumentNullException>(() =>
{ {
Bootstrapper.AddImageFormat(null); Bootstrapper.Default.AddImageFormat(null);
}); });
var format = new TestFormat(); var format = new TestFormat();
@ -60,7 +60,7 @@ namespace ImageSharp.Tests
exception = Assert.Throws<ArgumentNullException>(() => exception = Assert.Throws<ArgumentNullException>(() =>
{ {
Bootstrapper.AddImageFormat(format); Bootstrapper.Default.AddImageFormat(format);
}); });
Assert.Contains("decoder", exception.Message); Assert.Contains("decoder", exception.Message);
@ -69,7 +69,7 @@ namespace ImageSharp.Tests
exception = Assert.Throws<ArgumentNullException>(() => exception = Assert.Throws<ArgumentNullException>(() =>
{ {
Bootstrapper.AddImageFormat(format); Bootstrapper.Default.AddImageFormat(format);
}); });
Assert.Contains("encoder", exception.Message); Assert.Contains("encoder", exception.Message);
@ -78,7 +78,7 @@ namespace ImageSharp.Tests
exception = Assert.Throws<ArgumentNullException>(() => exception = Assert.Throws<ArgumentNullException>(() =>
{ {
Bootstrapper.AddImageFormat(format); Bootstrapper.Default.AddImageFormat(format);
}); });
Assert.Contains("mime type", exception.Message); Assert.Contains("mime type", exception.Message);
@ -87,7 +87,7 @@ namespace ImageSharp.Tests
exception = Assert.Throws<ArgumentException>(() => exception = Assert.Throws<ArgumentException>(() =>
{ {
Bootstrapper.AddImageFormat(format); Bootstrapper.Default.AddImageFormat(format);
}); });
Assert.Contains("mime type", exception.Message); Assert.Contains("mime type", exception.Message);
@ -96,7 +96,7 @@ namespace ImageSharp.Tests
exception = Assert.Throws<ArgumentNullException>(() => exception = Assert.Throws<ArgumentNullException>(() =>
{ {
Bootstrapper.AddImageFormat(format); Bootstrapper.Default.AddImageFormat(format);
}); });
Assert.Contains("extension", exception.Message); Assert.Contains("extension", exception.Message);
@ -105,7 +105,7 @@ namespace ImageSharp.Tests
exception = Assert.Throws<ArgumentException>(() => exception = Assert.Throws<ArgumentException>(() =>
{ {
Bootstrapper.AddImageFormat(format); Bootstrapper.Default.AddImageFormat(format);
}); });
Assert.Contains("extension", exception.Message); Assert.Contains("extension", exception.Message);
@ -114,7 +114,7 @@ namespace ImageSharp.Tests
exception = Assert.Throws<ArgumentNullException>(() => exception = Assert.Throws<ArgumentNullException>(() =>
{ {
Bootstrapper.AddImageFormat(format); Bootstrapper.Default.AddImageFormat(format);
}); });
Assert.Contains("supported extensions", exception.Message); Assert.Contains("supported extensions", exception.Message);
@ -123,7 +123,7 @@ namespace ImageSharp.Tests
exception = Assert.Throws<ArgumentException>(() => exception = Assert.Throws<ArgumentException>(() =>
{ {
Bootstrapper.AddImageFormat(format); Bootstrapper.Default.AddImageFormat(format);
}); });
Assert.Contains("supported extensions", exception.Message); Assert.Contains("supported extensions", exception.Message);
} }
@ -131,32 +131,32 @@ namespace ImageSharp.Tests
[Fact] [Fact]
public void AddImageFormatChecks() public void AddImageFormatChecks()
{ {
var format = new TestFormat(); TestFormat format = new TestFormat();
var exception = Assert.Throws<ArgumentException>(() => var exception = Assert.Throws<ArgumentException>(() =>
{ {
Bootstrapper.AddImageFormat(format); Bootstrapper.Default.AddImageFormat(format);
}); });
Assert.Contains("format with the same", exception.Message); Assert.Contains("format with the same", exception.Message);
format.Extension = "test"; format.Extension = "test";
exception = Assert.Throws<ArgumentException>(() => exception = Assert.Throws<ArgumentException>(() =>
{ {
Bootstrapper.AddImageFormat(format); Bootstrapper.Default.AddImageFormat(format);
}); });
Assert.Contains("should contain", exception.Message); Assert.Contains("should contain", exception.Message);
format.SupportedExtensions = new string[] { "test", "jpg" }; format.SupportedExtensions = new string[] { "test", "jpg" };
exception = Assert.Throws<ArgumentException>(() => exception = Assert.Throws<ArgumentException>(() =>
{ {
Bootstrapper.AddImageFormat(format); Bootstrapper.Default.AddImageFormat(format);
}); });
Assert.Contains("supports the same", exception.Message); Assert.Contains("supports the same", exception.Message);
format.SupportedExtensions = new string[] { "test", "" }; format.SupportedExtensions = new string[] { "test", "" };
exception = Assert.Throws<ArgumentException>(() => exception = Assert.Throws<ArgumentException>(() =>
{ {
Bootstrapper.AddImageFormat(format); Bootstrapper.Default.AddImageFormat(format);
}); });
Assert.Contains("empty values", exception.Message); Assert.Contains("empty values", exception.Message);
} }

24
tests/ImageSharp.Tests/TestBase.cs

@ -7,11 +7,33 @@ namespace ImageSharp.Tests
{ {
using System.IO; using System.IO;
using ImageSharp.Formats;
/// <summary> /// <summary>
/// The test base class. /// The test base class. Inherit from this class for any image manipulation tests.
/// </summary> /// </summary>
public abstract class TestBase public abstract class TestBase
{ {
/// <summary>
/// Initializes a new instance of the <see cref="TestBase"/> class.
/// </summary>
protected TestBase()
{
// Register the individual image formats.
Bootstrapper.Default.AddImageFormat(new PngFormat());
Bootstrapper.Default.AddImageFormat(new JpegFormat());
Bootstrapper.Default.AddImageFormat(new BmpFormat());
Bootstrapper.Default.AddImageFormat(new GifFormat());
}
/// <summary>
/// Creates the image output directory.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="pathParts">The path parts.</param>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
protected string CreateOutputDirectory(string path, params string[] pathParts) protected string CreateOutputDirectory(string path, params string[] pathParts)
{ {
path = Path.Combine("TestOutput", path); path = Path.Combine("TestOutput", path);

2
tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs

@ -96,7 +96,7 @@ namespace ImageSharp.Tests
private static IImageFormat GetImageFormatByExtension(string extension) private static IImageFormat GetImageFormatByExtension(string extension)
{ {
extension = extension.ToLower(); extension = extension.ToLower();
return Bootstrapper.ImageFormats.First(f => f.SupportedExtensions.Contains(extension)); return Bootstrapper.Default.ImageFormats.First(f => f.SupportedExtensions.Contains(extension));
} }
private string GetTestOutputDir() private string GetTestOutputDir()

Loading…
Cancel
Save