Browse Source

Added options for the decoder.

af/merge-core
Dirk Lemstra 9 years ago
committed by Dirk Lemstra
parent
commit
395505ac66
  1. 2
      src/ImageSharp.Formats.Bmp/BmpDecoder.cs
  2. 2
      src/ImageSharp.Formats.Gif/GifDecoder.cs
  3. 2
      src/ImageSharp.Formats.Jpeg/JpegDecoder.cs
  4. 9
      src/ImageSharp.Formats.Png/PngDecoder.cs
  5. 18
      src/ImageSharp/Formats/DecoderOptions.cs
  6. 18
      src/ImageSharp/Formats/IDecoderOptions.cs
  7. 3
      src/ImageSharp/Formats/IImageDecoder.cs
  8. 23
      src/ImageSharp/Image.cs
  9. 33
      src/ImageSharp/Image/Image{TColor}.cs

2
src/ImageSharp.Formats.Bmp/BmpDecoder.cs

@ -26,7 +26,7 @@ namespace ImageSharp.Formats
public class BmpDecoder : IImageDecoder
{
/// <inheritdoc/>
public void Decode<TColor>(Image<TColor> image, Stream stream)
public void Decode<TColor>(Image<TColor> image, Stream stream, IDecoderOptions options)
where TColor : struct, IPixel<TColor>
{
Guard.NotNull(image, "image");

2
src/ImageSharp.Formats.Gif/GifDecoder.cs

@ -14,7 +14,7 @@ namespace ImageSharp.Formats
public class GifDecoder : IImageDecoder
{
/// <inheritdoc/>
public void Decode<TColor>(Image<TColor> image, Stream stream)
public void Decode<TColor>(Image<TColor> image, Stream stream, IDecoderOptions options)
where TColor : struct, IPixel<TColor>
{
new GifDecoderCore<TColor>().Decode(image, stream);

2
src/ImageSharp.Formats.Jpeg/JpegDecoder.cs

@ -14,7 +14,7 @@ namespace ImageSharp.Formats
public class JpegDecoder : IImageDecoder
{
/// <inheritdoc/>
public void Decode<TColor>(Image<TColor> image, Stream stream)
public void Decode<TColor>(Image<TColor> image, Stream stream, IDecoderOptions options)
where TColor : struct, IPixel<TColor>
{
Guard.NotNull(image, "image");

9
src/ImageSharp.Formats.Png/PngDecoder.cs

@ -30,13 +30,8 @@ namespace ImageSharp.Formats
/// </remarks>
public class PngDecoder : IImageDecoder
{
/// <summary>
/// Decodes the image from the specified stream to the <see cref="ImageBase{TColor}"/>.
/// </summary>
/// <typeparam name="TColor">The pixel format.</typeparam>
/// <param name="image">The <see cref="ImageBase{TColor}"/> to decode to.</param>
/// <param name="stream">The <see cref="Stream"/> containing image data.</param>
public void Decode<TColor>(Image<TColor> image, Stream stream)
/// <inheritdoc/>
public void Decode<TColor>(Image<TColor> image, Stream stream, IDecoderOptions options)
where TColor : struct, IPixel<TColor>
{
new PngDecoderCore().Decode(image, stream);

18
src/ImageSharp/Formats/DecoderOptions.cs

@ -0,0 +1,18 @@
// <copyright file="DecoderOptions.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Formats
{
/// <summary>
/// Encapsulates the shared decoder options.
/// </summary>
public class DecoderOptions : IDecoderOptions
{
/// <summary>
/// Gets or sets a value indicating whether the metadata should be ignored when the image is being decoded.
/// </summary>
public bool IgnoreMetadata { get; set; }
}
}

18
src/ImageSharp/Formats/IDecoderOptions.cs

@ -0,0 +1,18 @@
// <copyright file="IDecoderOptions.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Formats
{
/// <summary>
/// Encapsulates the shared decoder options.
/// </summary>
public interface IDecoderOptions
{
/// <summary>
/// Gets a value indicating whether the metadata should be ignored when the image is being decoded.
/// </summary>
bool IgnoreMetadata { get; }
}
}

3
src/ImageSharp/Formats/IImageDecoder.cs

@ -19,7 +19,8 @@ namespace ImageSharp.Formats
/// <typeparam name="TColor">The pixel format.</typeparam>
/// <param name="image">The <see cref="ImageBase{TColor}"/> to decode to.</param>
/// <param name="stream">The <see cref="Stream"/> containing image data.</param>
void Decode<TColor>(Image<TColor> image, Stream stream)
/// <param name="options">The options for the decoder.</param>
void Decode<TColor>(Image<TColor> image, Stream stream, IDecoderOptions options)
where TColor : struct, IPixel<TColor>;
}
}

23
src/ImageSharp/Image.cs

@ -8,6 +8,8 @@ namespace ImageSharp
using System.Diagnostics;
using System.IO;
using Formats;
/// <summary>
/// Represents an image. Each pixel is a made up four 8-bit components red, green, blue, and alpha
/// packed into a single unsigned integer value.
@ -35,12 +37,15 @@ namespace ImageSharp
/// <param name="stream">
/// The stream containing image information.
/// </param>
/// <param name="options">
/// The options for the decoder.
/// </param>
/// <param name="configuration">
/// The configuration providing initialization code which allows extending the library.
/// </param>
/// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="stream"/> is null.</exception>
public Image(Stream stream, Configuration configuration = null)
: base(stream, configuration)
public Image(Stream stream, IDecoderOptions options = null, Configuration configuration = null)
: base(stream, options, configuration)
{
}
@ -51,12 +56,15 @@ namespace ImageSharp
/// <param name="filePath">
/// A file path to read image information.
/// </param>
/// <param name="options">
/// The options for the decoder.
/// </param>
/// <param name="configuration">
/// The configuration providing initialization code which allows extending the library.
/// </param>
/// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="filePath"/> is null.</exception>
public Image(string filePath, Configuration configuration = null)
: base(filePath, configuration)
public Image(string filePath, IDecoderOptions options = null, Configuration configuration = null)
: base(filePath, options, configuration)
{
}
#endif
@ -67,12 +75,15 @@ namespace ImageSharp
/// <param name="bytes">
/// The byte array containing image information.
/// </param>
/// <param name="options">
/// The options for the decoder.
/// </param>
/// <param name="configuration">
/// The configuration providing initialization code which allows extending the library.
/// </param>
/// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="bytes"/> is null.</exception>
public Image(byte[] bytes, Configuration configuration = null)
: base(bytes, configuration)
public Image(byte[] bytes, IDecoderOptions options = null, Configuration configuration = null)
: base(bytes, options, configuration)
{
}

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

@ -52,15 +52,18 @@ namespace ImageSharp
/// <param name="stream">
/// The stream containing image information.
/// </param>
/// <param name="options">
/// The options for the decoder.
/// </param>
/// <param name="configuration">
/// The configuration providing initialization code which allows extending the library.
/// </param>
/// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="stream"/> is null.</exception>
public Image(Stream stream, Configuration configuration = null)
public Image(Stream stream, IDecoderOptions options = null, Configuration configuration = null)
: base(configuration)
{
Guard.NotNull(stream, nameof(stream));
this.Load(stream);
this.Load(stream, options);
}
#if !NETSTANDARD1_1
@ -70,17 +73,20 @@ namespace ImageSharp
/// <param name="filePath">
/// The file containing image information.
/// </param>
/// <param name="options">
/// The options for the decoder.
/// </param>
/// <param name="configuration">
/// The configuration providing initialization code which allows extending the library.
/// </param>
/// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="filePath"/> is null.</exception>
public Image(string filePath, Configuration configuration = null)
public Image(string filePath, IDecoderOptions options = null, Configuration configuration = null)
: base(configuration)
{
Guard.NotNull(filePath, nameof(filePath));
using (var fs = File.OpenRead(filePath))
{
this.Load(fs);
this.Load(fs, options);
}
}
#endif
@ -91,18 +97,21 @@ namespace ImageSharp
/// <param name="bytes">
/// The byte array containing image information.
/// </param>
/// <param name="options">
/// The options for the decoder.
/// </param>
/// <param name="configuration">
/// The configuration providing initialization code which allows extending the library.
/// </param>
/// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="bytes"/> is null.</exception>
public Image(byte[] bytes, Configuration configuration = null)
public Image(byte[] bytes, IDecoderOptions options = null, Configuration configuration = null)
: base(configuration)
{
Guard.NotNull(bytes, nameof(bytes));
using (MemoryStream stream = new MemoryStream(bytes, false))
{
this.Load(stream);
this.Load(stream, options);
}
}
@ -397,10 +406,11 @@ namespace ImageSharp
/// Loads the image from the given stream.
/// </summary>
/// <param name="stream">The stream containing image information.</param>
/// <param name="options">The options for the decoder.</param>
/// <exception cref="NotSupportedException">
/// Thrown if the stream is not readable nor seekable.
/// </exception>
private void Load(Stream stream)
private void Load(Stream stream, IDecoderOptions options)
{
if (!this.Configuration.ImageFormats.Any())
{
@ -414,7 +424,7 @@ namespace ImageSharp
if (stream.CanSeek)
{
if (this.Decode(stream))
if (this.Decode(stream, options))
{
return;
}
@ -427,7 +437,7 @@ namespace ImageSharp
stream.CopyTo(ms);
ms.Position = 0;
if (this.Decode(ms))
if (this.Decode(ms, options))
{
return;
}
@ -449,10 +459,11 @@ namespace ImageSharp
/// Decodes the image stream to the current image.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="options">The options for the decoder.</param>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
private bool Decode(Stream stream)
private bool Decode(Stream stream, IDecoderOptions options)
{
int maxHeaderSize = this.Configuration.MaxHeaderSize;
if (maxHeaderSize <= 0)
@ -479,7 +490,7 @@ namespace ImageSharp
return false;
}
format.Decoder.Decode(this, stream);
format.Decoder.Decode(this, stream, options);
this.CurrentImageFormat = format;
return true;
}

Loading…
Cancel
Save