diff --git a/src/ImageSharp/Common/Exceptions/InvalidImageContentException.cs b/src/ImageSharp/Common/Exceptions/InvalidImageContentException.cs index 7069e89823..8be13919f1 100644 --- a/src/ImageSharp/Common/Exceptions/InvalidImageContentException.cs +++ b/src/ImageSharp/Common/Exceptions/InvalidImageContentException.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System; + namespace SixLabors.ImageSharp { /// @@ -18,5 +20,17 @@ public InvalidImageContentException(string errorMessage) : base(errorMessage) { } + + /// + /// Initializes a new instance of the class with the name of the + /// parameter that causes this exception. + /// + /// The error message that explains the reason for this exception. + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) + /// if no inner exception is specified. + public InvalidImageContentException(string errorMessage, Exception innerException) + : base(errorMessage, innerException) + { + } } } diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoder.cs b/src/ImageSharp/Formats/Bmp/BmpDecoder.cs index a956f19c72..cafe101060 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoder.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoder.cs @@ -43,9 +43,7 @@ public Image Decode(Configuration configuration, Stream stream) { 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); } } diff --git a/src/ImageSharp/Formats/Gif/GifDecoder.cs b/src/ImageSharp/Formats/Gif/GifDecoder.cs index caa076553d..553163bd7e 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoder.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoder.cs @@ -38,9 +38,10 @@ public Image Decode(Configuration configuration, Stream stream) { 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; } } diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index de5aa78843..2e849ba1b7 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -101,7 +101,7 @@ public GifDecoderCore(Configuration configuration, IGifDecoderOptions options) /// Decodes the stream to the image. /// /// The pixel format. - /// The stream containing image data. + /// The stream containing image data. /// The decoded image public Image Decode(Stream stream) where TPixel : unmanaged, IPixel diff --git a/src/ImageSharp/Formats/Gif/GifThrowHelper.cs b/src/ImageSharp/Formats/Gif/GifThrowHelper.cs index 1d81008a01..1b20c9f64c 100644 --- a/src/ImageSharp/Formats/Gif/GifThrowHelper.cs +++ b/src/ImageSharp/Formats/Gif/GifThrowHelper.cs @@ -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 @@ internal static class GifThrowHelper /// Cold path optimization for throwing 's /// /// The error message for the exception. - [MethodImpl(MethodImplOptions.NoInlining)] + [MethodImpl(InliningOptions.ColdPath)] public static void ThrowInvalidImageContentException(string errorMessage) => throw new InvalidImageContentException(errorMessage); + + /// + /// Cold path optimization for throwing 's. + /// + /// The error message for the exception. + /// The exception that is the cause of the current exception, or a null reference + /// if no inner exception is specified. + [MethodImpl(InliningOptions.ColdPath)] + public static void ThrowInvalidImageContentException(string errorMessage, Exception innerException) => throw new InvalidImageContentException(errorMessage, innerException); } } diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs index b1144508ec..e60901d913 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs @@ -32,9 +32,10 @@ public Image Decode(Configuration configuration, Stream stream) { (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; } } diff --git a/src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs b/src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs index dd44cb2d19..da4c3f9ee0 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs @@ -15,6 +15,15 @@ internal static class JpegThrowHelper [MethodImpl(InliningOptions.ColdPath)] public static void ThrowInvalidImageContentException(string errorMessage) => throw new InvalidImageContentException(errorMessage); + /// + /// Cold path optimization for throwing 's. + /// + /// The error message for the exception. + /// The exception that is the cause of the current exception, or a null reference + /// if no inner exception is specified. + [MethodImpl(InliningOptions.ColdPath)] + public static void ThrowInvalidImageContentException(string errorMessage, Exception innerException) => throw new InvalidImageContentException(errorMessage, innerException); + /// /// Cold path optimization for throwing 's /// diff --git a/src/ImageSharp/Formats/Png/PngDecoder.cs b/src/ImageSharp/Formats/Png/PngDecoder.cs index d605577e77..eceb4e592f 100644 --- a/src/ImageSharp/Formats/Png/PngDecoder.cs +++ b/src/ImageSharp/Formats/Png/PngDecoder.cs @@ -54,9 +54,10 @@ public Image Decode(Configuration configuration, Stream stream) { 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; } } diff --git a/src/ImageSharp/Formats/Png/PngThrowHelper.cs b/src/ImageSharp/Formats/Png/PngThrowHelper.cs index b0a2571eae..a72a4a0d88 100644 --- a/src/ImageSharp/Formats/Png/PngThrowHelper.cs +++ b/src/ImageSharp/Formats/Png/PngThrowHelper.cs @@ -11,6 +11,10 @@ namespace SixLabors.ImageSharp.Formats.Png /// 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"); diff --git a/src/ImageSharp/Formats/Tga/TgaDecoder.cs b/src/ImageSharp/Formats/Tga/TgaDecoder.cs index c3b8526ceb..64dbdf58a9 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoder.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoder.cs @@ -28,9 +28,10 @@ public Image Decode(Configuration configuration, Stream stream) { 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; } } diff --git a/src/ImageSharp/Formats/Tga/TgaThrowHelper.cs b/src/ImageSharp/Formats/Tga/TgaThrowHelper.cs index 1714a2025e..fc158e781e 100644 --- a/src/ImageSharp/Formats/Tga/TgaThrowHelper.cs +++ b/src/ImageSharp/Formats/Tga/TgaThrowHelper.cs @@ -12,15 +12,25 @@ internal static class TgaThrowHelper /// Cold path optimization for throwing 's /// /// The error message for the exception. - [MethodImpl(MethodImplOptions.NoInlining)] + [MethodImpl(InliningOptions.ColdPath)] public static void ThrowInvalidImageContentException(string errorMessage) => throw new InvalidImageContentException(errorMessage); + /// + /// Cold path optimization for throwing 's + /// + /// The error message for the exception. + /// The exception that is the cause of the current exception, or a null reference + /// if no inner exception is specified. + [MethodImpl(InliningOptions.ColdPath)] + public static void ThrowInvalidImageContentException(string errorMessage, Exception innerException) + => throw new InvalidImageContentException(errorMessage, innerException); + /// /// Cold path optimization for throwing 's /// /// The error message for the exception. - [MethodImpl(MethodImplOptions.NoInlining)] + [MethodImpl(InliningOptions.ColdPath)] public static void ThrowNotSupportedException(string errorMessage) => throw new NotSupportedException(errorMessage); } diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs index 85cdf6d11a..f63fc0a16a 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs @@ -77,7 +77,7 @@ public void BmpDecoder_DegenerateMemoryRequest_ShouldTranslateTo_ImageFormatExce where TPixel : unmanaged, IPixel { provider.LimitAllocatorBufferCapacity().InPixelsSqrt(10); - ImageFormatException ex = Assert.Throws(() => provider.GetImage(BmpDecoder)); + InvalidImageContentException ex = Assert.Throws(() => provider.GetImage(BmpDecoder)); Assert.IsType(ex.InnerException); } diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs index b3a99aa1c0..80675a5eb5 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs @@ -179,7 +179,7 @@ public void GifDecoder_DegenerateMemoryRequest_ShouldTranslateTo_ImageFormatExce where TPixel : unmanaged, IPixel { provider.LimitAllocatorBufferCapacity().InPixelsSqrt(10); - ImageFormatException ex = Assert.Throws(() => provider.GetImage(GifDecoder)); + InvalidImageContentException ex = Assert.Throws(() => provider.GetImage(GifDecoder)); Assert.IsType(ex.InnerException); } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index 25cf5dd376..a35bb177ce 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -107,7 +107,7 @@ public void DegenerateMemoryRequest_ShouldTranslateTo_ImageFormatException { provider.LimitAllocatorBufferCapacity().InBytesSqrt(10); - ImageFormatException ex = Assert.Throws(() => provider.GetImage(JpegDecoder)); + InvalidImageContentException ex = Assert.Throws(() => provider.GetImage(JpegDecoder)); this.Output.WriteLine(ex.Message); Assert.IsType(ex.InnerException); } diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index 6eb6e93db8..72b27ec5d6 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -397,7 +397,7 @@ public void PngDecoder_DegenerateMemoryRequest_ShouldTranslateTo_ImageFormatExce where TPixel : unmanaged, IPixel { provider.LimitAllocatorBufferCapacity().InPixelsSqrt(10); - ImageFormatException ex = Assert.Throws(() => provider.GetImage(PngDecoder)); + InvalidImageContentException ex = Assert.Throws(() => provider.GetImage(PngDecoder)); Assert.IsType(ex.InnerException); } diff --git a/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs index 840bb55f20..6f886b73df 100644 --- a/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs @@ -740,7 +740,7 @@ public void TgaDecoder_DegenerateMemoryRequest_ShouldTranslateTo_ImageFormatExce where TPixel : unmanaged, IPixel { provider.LimitAllocatorBufferCapacity().InPixelsSqrt(10); - ImageFormatException ex = Assert.Throws(() => provider.GetImage(TgaDecoder)); + InvalidImageContentException ex = Assert.Throws(() => provider.GetImage(TgaDecoder)); Assert.IsType(ex.InnerException); }