Brian Popow
6 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with
73 additions and
46 deletions
src/ImageSharp/Common/Exceptions/InvalidImageContentException.cs
src/ImageSharp/Formats/Bmp/BmpDecoder.cs
src/ImageSharp/Formats/Gif/GifDecoder.cs
src/ImageSharp/Formats/Gif/GifThrowHelper.cs
src/ImageSharp/Formats/Jpeg/JpegDecoder.cs
src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs
src/ImageSharp/Formats/Png/PngDecoder.cs
src/ImageSharp/Formats/Png/PngMetadata.cs
src/ImageSharp/Formats/Png/PngThrowHelper.cs
src/ImageSharp/Formats/Tga/TgaDecoder.cs
src/ImageSharp/Formats/Tga/TgaThrowHelper.cs
tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs
tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs
tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs
@ -1,6 +1,8 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System ;
namespace SixLabors.ImageSharp
{
/// <summary>
@ -18,5 +20,17 @@ namespace SixLabors.ImageSharp
: base ( errorMessage )
{
}
/// <summary>
/// Initializes a new instance of the <see cref="InvalidImageContentException"/> 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>
/// <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 InvalidImageContentException ( string errorMessage , Exception innerException )
: base ( errorMessage , innerException )
{
}
}
}
@ -43,9 +43,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
{
Size dims = decoder . Dimensions ;
// TODO: use InvalidImageContentException here, if we decide to define it
// https://github.com/SixLabors/ImageSharp/issues/1110
throw new ImageFormatException ( $"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {dims.Width}x{dims.Height}. This error can happen for very large RLE bitmaps, which are not supported." , ex ) ;
throw new InvalidImageContentException ( $"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {dims.Width}x{dims.Height}. This error can happen for very large RLE bitmaps, which are not supported." , ex ) ;
}
}
@ -38,9 +38,10 @@ namespace SixLabors.ImageSharp.Formats.Gif
{
Size dims = decoder . Dimensions ;
// TODO: use InvalidImageContentException here, if we decide to define it
// https://github.com/SixLabors/ImageSharp/issues/1110
throw new ImageFormatException ( $"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {dims.Width}x{dims.Height}." , ex ) ;
GifThrowHelper . ThrowInvalidImageContentException ( $"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {dims.Width}x{dims.Height}." , ex ) ;
// Not reachable, as the previous statement will throw a exception.
return null ;
}
}
@ -1,6 +1,7 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System ;
using System.Runtime.CompilerServices ;
namespace SixLabors.ImageSharp.Formats.Gif
@ -11,8 +12,17 @@ namespace SixLabors.ImageSharp.Formats.Gif
/// Cold path optimization for throwing <see cref="InvalidImageContentException"/>'s
/// </summary>
/// <param name="errorMessage">The error message for the exception.</param>
[MethodImpl(MethodImplOptions.NoInlining )]
[MethodImpl(InliningOptions.ColdPath )]
public static void ThrowInvalidImageContentException ( string errorMessage )
= > throw new InvalidImageContentException ( errorMessage ) ;
/// <summary>
/// Cold path optimization for throwing <see cref="InvalidImageContentException"/>'s.
/// </summary>
/// <param name="errorMessage">The error message for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference
/// if no inner exception is specified.</param>
[MethodImpl(InliningOptions.ColdPath)]
public static void ThrowInvalidImageContentException ( string errorMessage , Exception innerException ) = > throw new InvalidImageContentException ( errorMessage , innerException ) ;
}
}
@ -32,9 +32,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
{
( int w , int h ) = ( decoder . ImageWidth , decoder . ImageHeight ) ;
// TODO: use InvalidImageContentException here, if we decide to define it
// https://github.com/SixLabors/ImageSharp/issues/1110
throw new ImageFormatException ( $"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {w}x{h}." , ex ) ;
JpegThrowHelper . ThrowInvalidImageContentException ( $"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {w}x{h}." , ex ) ;
// Not reachable, as the previous statement will throw a exception.
return null ;
}
}
@ -15,6 +15,15 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
[MethodImpl(InliningOptions.ColdPath)]
public static void ThrowInvalidImageContentException ( string errorMessage ) = > throw new InvalidImageContentException ( errorMessage ) ;
/// <summary>
/// Cold path optimization for throwing <see cref="InvalidImageContentException"/>'s.
/// </summary>
/// <param name="errorMessage">The error message for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference
/// if no inner exception is specified.</param>
[MethodImpl(InliningOptions.ColdPath)]
public static void ThrowInvalidImageContentException ( string errorMessage , Exception innerException ) = > throw new InvalidImageContentException ( errorMessage , innerException ) ;
/// <summary>
/// Cold path optimization for throwing <see cref="NotImplementedException"/>'s
/// </summary>
@ -54,9 +54,10 @@ namespace SixLabors.ImageSharp.Formats.Png
{
Size dims = decoder . Dimensions ;
// TODO: use InvalidImageContentException here, if we decide to define it
// https://github.com/SixLabors/ImageSharp/issues/1110
throw new ImageFormatException ( $"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {dims.Width}x{dims.Height}." , ex ) ;
PngThrowHelper . ThrowInvalidImageContentException ( $"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {dims.Width}x{dims.Height}." , ex ) ;
// Not reachable, as the previous statement will throw a exception.
return null ;
}
}
@ -91,34 +91,12 @@ namespace SixLabors.ImageSharp.Formats.Png
public bool HasTransparency { get ; set ; }
/// <summary>
/// Gets or sets the collection of text data stored within the iTXt, tEXt, and zTXt chunks.
/// Gets or sets the collection of text data stored within the iTXt, tEXt, and zTXt chunks.
/// Used for conveying textual information associated with the image.
/// </summary>
public IList < PngTextData > TextData { get ; set ; } = new List < PngTextData > ( ) ;
/// <summary>
/// Gets the list of png text properties for storing meta information about this image.
/// </summary>
public IList < PngTextData > PngTextProperties { get ; } = new List < PngTextData > ( ) ;
/// <inheritdoc/>
public IDeepCloneable DeepClone ( ) = > new PngMetadata ( this ) ;
internal bool TryGetPngTextProperty ( string keyword , out PngTextData result )
{
for ( int i = 0 ; i < this . TextData . Count ; i + + )
{
if ( this . TextData [ i ] . Keyword = = keyword )
{
result = this . TextData [ i ] ;
return true ;
}
}
result = default ;
return false ;
}
}
}
@ -11,6 +11,10 @@ namespace SixLabors.ImageSharp.Formats.Png
/// </summary>
internal static class PngThrowHelper
{
[MethodImpl(InliningOptions.ColdPath)]
public static void ThrowInvalidImageContentException ( string errorMessage , Exception innerException )
= > throw new InvalidImageContentException ( errorMessage , innerException ) ;
[MethodImpl(InliningOptions.ColdPath)]
public static void ThrowNoHeader ( ) = > throw new InvalidImageContentException ( "PNG Image does not contain a header chunk" ) ;
@ -28,9 +28,10 @@ namespace SixLabors.ImageSharp.Formats.Tga
{
Size dims = decoder . Dimensions ;
// TODO: use InvalidImageContentException here, if we decide to define it
// https://github.com/SixLabors/ImageSharp/issues/1110
throw new ImageFormatException ( $"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {dims.Width}x{dims.Height}." , ex ) ;
TgaThrowHelper . ThrowInvalidImageContentException ( $"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {dims.Width}x{dims.Height}." , ex ) ;
// Not reachable, as the previous statement will throw a exception.
return null ;
}
}
@ -12,15 +12,25 @@ namespace SixLabors.ImageSharp.Formats.Tga
/// Cold path optimization for throwing <see cref="ImageFormatException"/>'s
/// </summary>
/// <param name="errorMessage">The error message for the exception.</param>
[MethodImpl(MethodImplOptions.NoInlining )]
[MethodImpl(InliningOptions.ColdPath )]
public static void ThrowInvalidImageContentException ( string errorMessage )
= > throw new InvalidImageContentException ( errorMessage ) ;
/// <summary>
/// Cold path optimization for throwing <see cref="ImageFormatException"/>'s
/// </summary>
/// <param name="errorMessage">The error message for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference
/// if no inner exception is specified.</param>
[MethodImpl(InliningOptions.ColdPath)]
public static void ThrowInvalidImageContentException ( string errorMessage , Exception innerException )
= > throw new InvalidImageContentException ( errorMessage , innerException ) ;
/// <summary>
/// Cold path optimization for throwing <see cref="NotSupportedException"/>'s
/// </summary>
/// <param name="errorMessage">The error message for the exception.</param>
[MethodImpl(MethodImplOptions.NoInlining)]
[MethodImpl(InliningOptions.ColdPath )]
public static void ThrowNotSupportedException ( string errorMessage )
= > throw new NotSupportedException ( errorMessage ) ;
}
@ -77,7 +77,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
where TPixel : unmanaged , IPixel < TPixel >
{
provider . LimitAllocatorBufferCapacity ( ) . InPixelsSqrt ( 1 0 ) ;
ImageForma tException ex = Assert . Throws < ImageForma tException > ( ( ) = > provider . GetImage ( BmpDecoder ) ) ;
InvalidImageConten tException ex = Assert . Throws < InvalidImageConten tException > ( ( ) = > provider . GetImage ( BmpDecoder ) ) ;
Assert . IsType < InvalidMemoryOperationException > ( ex . InnerException ) ;
}
@ -191,7 +191,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
where TPixel : unmanaged , IPixel < TPixel >
{
provider . LimitAllocatorBufferCapacity ( ) . InPixelsSqrt ( 1 0 ) ;
ImageForma tException ex = Assert . Throws < ImageForma tException > ( ( ) = > provider . GetImage ( GifDecoder ) ) ;
InvalidImageConten tException ex = Assert . Throws < InvalidImageConten tException > ( ( ) = > provider . GetImage ( GifDecoder ) ) ;
Assert . IsType < InvalidMemoryOperationException > ( ex . InnerException ) ;
}
@ -107,7 +107,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
where TPixel : unmanaged , IPixel < TPixel >
{
provider . LimitAllocatorBufferCapacity ( ) . InBytesSqrt ( 1 0 ) ;
ImageForma tException ex = Assert . Throws < ImageForma tException > ( ( ) = > provider . GetImage ( JpegDecoder ) ) ;
InvalidImageConten tException ex = Assert . Throws < InvalidImageConten tException > ( ( ) = > provider . GetImage ( JpegDecoder ) ) ;
this . Output . WriteLine ( ex . Message ) ;
Assert . IsType < InvalidMemoryOperationException > ( ex . InnerException ) ;
}
@ -397,7 +397,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
where TPixel : unmanaged , IPixel < TPixel >
{
provider . LimitAllocatorBufferCapacity ( ) . InPixelsSqrt ( 1 0 ) ;
ImageForma tException ex = Assert . Throws < ImageForma tException > ( ( ) = > provider . GetImage ( PngDecoder ) ) ;
InvalidImageConten tException ex = Assert . Throws < InvalidImageConten tException > ( ( ) = > provider . GetImage ( PngDecoder ) ) ;
Assert . IsType < InvalidMemoryOperationException > ( ex . InnerException ) ;
}
@ -740,7 +740,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
where TPixel : unmanaged , IPixel < TPixel >
{
provider . LimitAllocatorBufferCapacity ( ) . InPixelsSqrt ( 1 0 ) ;
ImageForma tException ex = Assert . Throws < ImageForma tException > ( ( ) = > provider . GetImage ( TgaDecoder ) ) ;
InvalidImageConten tException ex = Assert . Throws < InvalidImageConten tException > ( ( ) = > provider . GetImage ( TgaDecoder ) ) ;
Assert . IsType < InvalidMemoryOperationException > ( ex . InnerException ) ;
}