Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw UnkownFormatException on Image.Load #932

Merged
merged 6 commits into from
Jun 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/ImageSharp/Common/Exceptions/ImageFormatException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System;
Expand All @@ -9,14 +9,14 @@ namespace SixLabors.ImageSharp
/// The exception that is thrown when the library tries to load
/// an image, which has an invalid format.
/// </summary>
public sealed class ImageFormatException : Exception
public class ImageFormatException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="ImageFormatException"/> class with the name of the
/// parameter that causes this exception.
/// </summary>
/// <param name="errorMessage">The error message that explains the reason for this exception.</param>
public ImageFormatException(string errorMessage)
internal ImageFormatException(string errorMessage)
: base(errorMessage)
{
}
Expand All @@ -28,7 +28,7 @@ public ImageFormatException(string errorMessage)
/// <param name="errorMessage">The error message that explains the reason for this exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic)
/// if no inner exception is specified.</param>
public ImageFormatException(string errorMessage, Exception innerException)
internal ImageFormatException(string errorMessage, Exception innerException)
: base(errorMessage, innerException)
{
}
Expand Down
36 changes: 36 additions & 0 deletions src/ImageSharp/Common/Exceptions/UnknownImageFormatException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System;

namespace SixLabors.ImageSharp
{
/// <summary>
/// The exception that is thrown when the library tries to load
/// an image which has an unkown format.
/// </summary>
public sealed class UnknownImageFormatException : ImageFormatException
{
/// <summary>
/// Initializes a new instance of the <see cref="UnknownImageFormatException"/> class with the name of the
/// parameter that causes this exception.
/// </summary>
/// <param name="errorMessage">The error message that explains the reason for this exception.</param>
public UnknownImageFormatException(string errorMessage)
: base(errorMessage)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="UnknownImageFormatException"/> class with a specified
/// error message and the exception that is the cause of this exception.
/// </summary>
/// <param name="errorMessage">The error message that explains the reason for this exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic)
/// if no inner exception is specified.</param>
public UnknownImageFormatException(string errorMessage, Exception innerException)
: base(errorMessage, innerException)
{
}
}
}
20 changes: 16 additions & 4 deletions src/ImageSharp/Image.FromStream.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System;
Expand Down Expand Up @@ -62,6 +62,7 @@ public static IImageInfo Identify(Configuration config, Stream stream)
/// <param name="stream">The stream containing image information.</param>
/// <param name="format">the mime type of the decoded image.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception>
/// <returns>A new <see cref="Image"/>.</returns>>
public static Image Load(Stream stream, out IImageFormat format) => Load(Configuration.Default, stream, out format);

Expand All @@ -71,6 +72,7 @@ public static IImageInfo Identify(Configuration config, Stream stream)
/// </summary>
/// <param name="stream">The stream containing image information.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception>
/// <returns>A new <see cref="Image"/>.</returns>>
public static Image Load(Stream stream) => Load(Configuration.Default, stream);

Expand All @@ -81,6 +83,7 @@ public static IImageInfo Identify(Configuration config, Stream stream)
/// <param name="stream">The stream containing image information.</param>
/// <param name="decoder">The decoder.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception>
/// <returns>A new <see cref="Image"/>.</returns>>
public static Image Load(Stream stream, IImageDecoder decoder) => Load(Configuration.Default, stream, decoder);

Expand All @@ -92,6 +95,7 @@ public static IImageInfo Identify(Configuration config, Stream stream)
/// <param name="stream">The stream containing image information.</param>
/// <param name="decoder">The decoder.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception>
/// <returns>A new <see cref="Image"/>.</returns>>
public static Image Load(Configuration config, Stream stream, IImageDecoder decoder) =>
WithSeekableStream(config, stream, s => decoder.Decode(config, s));
Expand All @@ -102,6 +106,7 @@ public static Image Load(Configuration config, Stream stream, IImageDecoder deco
/// <param name="config">The config for the decoder.</param>
/// <param name="stream">The stream containing image information.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception>
/// <returns>A new <see cref="Image"/>.</returns>>
public static Image Load(Configuration config, Stream stream) => Load(config, stream, out _);

Expand All @@ -110,6 +115,7 @@ public static Image Load(Configuration config, Stream stream, IImageDecoder deco
/// </summary>
/// <param name="stream">The stream containing image information.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
public static Image<TPixel> Load<TPixel>(Stream stream)
Expand All @@ -122,6 +128,7 @@ public static Image<TPixel> Load<TPixel>(Stream stream)
/// <param name="stream">The stream containing image information.</param>
/// <param name="format">the mime type of the decoded image.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
public static Image<TPixel> Load<TPixel>(Stream stream, out IImageFormat format)
Expand All @@ -134,6 +141,7 @@ public static Image<TPixel> Load<TPixel>(Stream stream, out IImageFormat format)
/// <param name="stream">The stream containing image information.</param>
/// <param name="decoder">The decoder.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
public static Image<TPixel> Load<TPixel>(Stream stream, IImageDecoder decoder)
Expand All @@ -147,6 +155,7 @@ public static Image<TPixel> Load<TPixel>(Stream stream, IImageDecoder decoder)
/// <param name="stream">The stream containing image information.</param>
/// <param name="decoder">The decoder.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</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, IImageDecoder decoder)
Expand All @@ -159,6 +168,7 @@ public static Image<TPixel> Load<TPixel>(Configuration config, Stream stream, II
/// <param name="config">The configuration options.</param>
/// <param name="stream">The stream containing image information.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</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)
Expand All @@ -172,6 +182,7 @@ public static Image<TPixel> Load<TPixel>(Configuration config, Stream stream)
/// <param name="stream">The stream containing image information.</param>
/// <param name="format">the mime type of the decoded image.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</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, out IImageFormat format)
Expand All @@ -195,7 +206,7 @@ public static Image<TPixel> Load<TPixel>(Configuration config, Stream stream, ou
sb.AppendLine($" - {val.Key.Name} : {val.Value.GetType().Name}");
}

throw new NotSupportedException(sb.ToString());
throw new UnknownImageFormatException(sb.ToString());
}

/// <summary>
Expand All @@ -206,6 +217,7 @@ public static Image<TPixel> Load<TPixel>(Configuration config, Stream stream, ou
/// <param name="stream">The stream containing image information.</param>
/// <param name="format">the mime type of the decoded image.</param>
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
/// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception>
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
public static Image Load(Configuration config, Stream stream, out IImageFormat format)
{
Expand All @@ -227,7 +239,7 @@ public static Image Load(Configuration config, Stream stream, out IImageFormat f
sb.AppendLine($" - {val.Key.Name} : {val.Value.GetType().Name}");
}

throw new NotSupportedException(sb.ToString());
throw new UnknownImageFormatException(sb.ToString());
}

private static T WithSeekableStream<T>(Configuration config, Stream stream, Func<Stream, T> action)
Expand Down Expand Up @@ -257,4 +269,4 @@ private static T WithSeekableStream<T>(Configuration config, Stream stream, Func
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.IO;

using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;

using Xunit;

namespace SixLabors.ImageSharp.Tests
{
public partial class ImageTests
{
public class Load_FromStream_Throws : IDisposable
{
private static readonly byte[] Data = new byte[] { 0x01 };

private MemoryStream Stream { get; } = new MemoryStream(Data);

[Fact]
public void Image_Load_Throws_UknownImageFormatException()
{
Assert.Throws<UnknownImageFormatException>(() =>
{
using (var img = Image.Load(Configuration.Default, this.Stream, out IImageFormat format))
{
}
});
}

[Fact]
public void Image_Load_T_Throws_UknownImageFormatException()
{
Assert.Throws<UnknownImageFormatException>(() =>
{
using (var img = Image.Load<Rgba32>(Configuration.Default, this.Stream, out IImageFormat format))
{
}
});
}

public void Dispose()
{
this.Stream?.Dispose();
}
}
}
}