Browse Source

Use configuration to set ReadOrigin

af/merge-core
James Jackson-South 8 years ago
parent
commit
c5f7f27e19
  1. 5
      src/ImageSharp/Configuration.cs
  2. 101
      src/ImageSharp/Image/Image.FromStream.cs
  3. 11
      tests/ImageSharp.Tests/ConfigurationTests.cs

5
src/ImageSharp/Configuration.cs

@ -83,6 +83,11 @@ namespace SixLabors.ImageSharp
/// </summary>
public IEnumerable<IImageFormat> ImageFormats => this.imageFormats;
/// <summary>
/// Gets or sets the position in a stream to use for reading.
/// </summary>
public ReadOrigin ReadOrigin { get; set; } = ReadOrigin.Begin;
/// <summary>
/// Gets or sets the <see cref="MemoryManager"/> that is currently in use.
/// </summary>

101
src/ImageSharp/Image/Image.FromStream.cs

@ -20,15 +20,7 @@ namespace SixLabors.ImageSharp
/// </summary>
/// <param name="stream">The image stream to read the header from.</param>
/// <returns>The mime type or null if none found.</returns>
public static IImageFormat DetectFormat(Stream stream) => DetectFormat(null, stream);
/// <summary>
/// By reading the header on the provided stream this calculates the images mime type.
/// </summary>
/// <param name="stream">The image stream to read the header from.</param>
/// <param name="origin">The position in the stream to use for reading.</param>
/// <returns>The mime type or null if none found.</returns>
public static IImageFormat DetectFormat(Stream stream, ReadOrigin origin) => DetectFormat(null, stream, origin);
public static IImageFormat DetectFormat(Stream stream) => DetectFormat(Configuration.Default, stream);
/// <summary>
/// By reading the header on the provided stream this calculates the images mime type.
@ -36,17 +28,8 @@ namespace SixLabors.ImageSharp
/// <param name="config">The configuration.</param>
/// <param name="stream">The image stream to read the header from.</param>
/// <returns>The mime type or null if none found.</returns>
public static IImageFormat DetectFormat(Configuration config, Stream stream) => DetectFormat(config, stream, ReadOrigin.Begin);
/// <summary>
/// By reading the header on the provided stream this calculates the images mime type.
/// </summary>
/// <param name="config">The configuration.</param>
/// <param name="stream">The image stream to read the header from.</param>
/// <param name="origin">The position in the stream to use for reading.</param>
/// <returns>The mime type or null if none found.</returns>
public static IImageFormat DetectFormat(Configuration config, Stream stream, ReadOrigin origin)
=> WithSeekableStream(stream, origin, s => InternalDetectFormat(s, config ?? Configuration.Default));
public static IImageFormat DetectFormat(Configuration config, Stream stream)
=> WithSeekableStream(config, stream, s => InternalDetectFormat(s, config));
/// <summary>
/// By reading the header on the provided stream this reads the raw image information.
@ -58,33 +41,19 @@ namespace SixLabors.ImageSharp
/// <returns>
/// The <see cref="IImageInfo"/> or null if suitable info detector not found.
/// </returns>
public static IImageInfo Identify(Stream stream) => Identify(null, stream);
/// <summary>
/// Reads the raw image information from the specified stream without fully decoding it.
/// </summary>
/// <param name="config">The configuration.</param>
/// <param name="stream">The image stream to read the information from.</param>
/// <exception cref="NotSupportedException">
/// Thrown if the stream is not readable nor seekable.
/// </exception>
/// <returns>
/// The <see cref="IImageInfo"/> or null if suitable info detector is not found.
/// </returns>
public static IImageInfo Identify(Configuration config, Stream stream) => Identify(config, stream, ReadOrigin.Begin);
public static IImageInfo Identify(Stream stream) => Identify(Configuration.Default, stream);
/// <summary>
/// Reads the raw image information from the specified stream without fully decoding it.
/// </summary>
/// <param name="config">The configuration.</param>
/// <param name="stream">The image stream to read the information from.</param>
/// <param name="origin">The position in the stream to use for reading.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <returns>
/// The <see cref="IImageInfo"/> or null if suitable info detector is not found.
/// </returns>
public static IImageInfo Identify(Configuration config, Stream stream, ReadOrigin origin)
=> WithSeekableStream(stream, origin, s => InternalIdentity(s, config ?? Configuration.Default));
public static IImageInfo Identify(Configuration config, Stream stream)
=> WithSeekableStream(config, stream, s => InternalIdentity(s, config ?? Configuration.Default));
/// <summary>
/// Create a new instance of the <see cref="Image{Rgba32}"/> class from the given stream.
@ -181,22 +150,7 @@ namespace SixLabors.ImageSharp
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
public static Image<TPixel> Load<TPixel>(Stream stream, IImageDecoder decoder)
where TPixel : struct, IPixel<TPixel>
=> Load<TPixel>(stream, ReadOrigin.Begin, decoder);
/// <summary>
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
/// </summary>
/// <param name="stream">The stream containing image information.</param>
/// <param name="origin">The position in the stream to use for reading.</param>
/// <param name="decoder">The decoder.</param>
/// <exception cref="NotSupportedException">
/// Thrown if the stream is not readable nor seekable.
/// </exception>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
public static Image<TPixel> Load<TPixel>(Stream stream, ReadOrigin origin, IImageDecoder decoder)
where TPixel : struct, IPixel<TPixel>
=> WithSeekableStream(stream, origin, s => decoder.Decode<TPixel>(Configuration.Default, s));
=> WithSeekableStream(Configuration.Default, stream, s => decoder.Decode<TPixel>(Configuration.Default, s));
/// <summary>
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
@ -211,23 +165,7 @@ namespace SixLabors.ImageSharp
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
public static Image<TPixel> Load<TPixel>(Configuration config, Stream stream, IImageDecoder decoder)
where TPixel : struct, IPixel<TPixel>
=> Load<TPixel>(config, stream, ReadOrigin.Begin, decoder);
/// <summary>
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
/// </summary>
/// <param name="config">The Configuration.</param>
/// <param name="stream">The stream containing image information.</param>
/// <param name="origin">The position in the stream to use for reading.</param>
/// <param name="decoder">The decoder.</param>
/// <exception cref="NotSupportedException">
/// Thrown if the stream is not readable nor seekable.
/// </exception>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
public static Image<TPixel> Load<TPixel>(Configuration config, Stream stream, ReadOrigin origin, IImageDecoder decoder)
where TPixel : struct, IPixel<TPixel>
=> WithSeekableStream(stream, origin, s => decoder.Decode<TPixel>(config, s));
=> WithSeekableStream(config, stream, s => decoder.Decode<TPixel>(config, s));
/// <summary>
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
@ -243,21 +181,6 @@ namespace SixLabors.ImageSharp
where TPixel : struct, IPixel<TPixel>
=> Load<TPixel>(config, stream, out IImageFormat _);
/// <summary>
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
/// </summary>
/// <param name="config">The configuration options.</param>
/// <param name="stream">The stream containing image information.</param>
/// <param name="origin">The position in the stream to use for reading.</param>
/// <exception cref="NotSupportedException">
/// Thrown if the stream is not readable nor seekable.
/// </exception>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
public static Image<TPixel> Load<TPixel>(Configuration config, Stream stream, ReadOrigin origin)
where TPixel : struct, IPixel<TPixel>
=> Load<TPixel>(config, stream, origin, out IImageFormat _);
/// <summary>
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
/// </summary>
@ -289,7 +212,7 @@ namespace SixLabors.ImageSharp
where TPixel : struct, IPixel<TPixel>
{
config = config ?? Configuration.Default;
(Image<TPixel> img, IImageFormat format) data = WithSeekableStream(stream, origin, s => Decode<TPixel>(s, config));
(Image<TPixel> img, IImageFormat format) data = WithSeekableStream(config, stream, s => Decode<TPixel>(s, config));
format = data.format;
@ -309,7 +232,7 @@ namespace SixLabors.ImageSharp
throw new NotSupportedException(stringBuilder.ToString());
}
private static T WithSeekableStream<T>(Stream stream, ReadOrigin origin, Func<Stream, T> action)
private static T WithSeekableStream<T>(Configuration config, Stream stream, Func<Stream, T> action)
{
if (!stream.CanRead)
{
@ -318,7 +241,7 @@ namespace SixLabors.ImageSharp
if (stream.CanSeek)
{
if (origin == ReadOrigin.Begin)
if (config.ReadOrigin == ReadOrigin.Begin)
{
stream.Position = 0;
}
@ -326,7 +249,7 @@ namespace SixLabors.ImageSharp
return action(stream);
}
if (origin == ReadOrigin.Current)
if (config.ReadOrigin == ReadOrigin.Current)
{
throw new NotSupportedException("Cannot seek within the stream.");
}

11
tests/ImageSharp.Tests/ConfigurationTests.cs

@ -59,6 +59,15 @@ namespace SixLabors.ImageSharp.Tests
Assert.True(Configuration.Default.ParallelOptions != null);
}
/// <summary>
/// Test that the default configuration read origin options is set to begin.
/// </summary>
[Fact]
public void TestDefultConfigurationReadOriginIsBegin()
{
Assert.True(Configuration.Default.ReadOrigin == ReadOrigin.Begin);
}
/// <summary>
/// Test that the default configuration parallel options max degrees of parallelism matches the
/// environment processor count.
@ -83,7 +92,7 @@ namespace SixLabors.ImageSharp.Tests
{
Assert.Throws<ArgumentNullException>(() =>
{
this.DefaultConfiguration.SetEncoder(null, new Mock<IImageEncoder>().Object);
this.DefaultConfiguration.SetEncoder(null, new Mock<IImageEncoder>().Object);
});
Assert.Throws<ArgumentNullException>(() =>
{

Loading…
Cancel
Save