Skip to content

Commit 39dd158

Browse files
authored
Merge pull request #1190 from SixLabors/invalidContentException
Change ImageFormatException to InvalidImageContentException
2 parents 5639bab + 193dafe commit 39dd158

File tree

16 files changed

+73
-24
lines changed

16 files changed

+73
-24
lines changed

src/ImageSharp/Common/Exceptions/InvalidImageContentException.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Six Labors and contributors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
5+
46
namespace SixLabors.ImageSharp
57
{
68
/// <summary>
@@ -18,5 +20,17 @@ public InvalidImageContentException(string errorMessage)
1820
: base(errorMessage)
1921
{
2022
}
23+
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="InvalidImageContentException"/> class with the name of the
26+
/// parameter that causes this exception.
27+
/// </summary>
28+
/// <param name="errorMessage">The error message that explains the reason for this exception.</param>
29+
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic)
30+
/// if no inner exception is specified.</param>
31+
public InvalidImageContentException(string errorMessage, Exception innerException)
32+
: base(errorMessage, innerException)
33+
{
34+
}
2135
}
2236
}

src/ImageSharp/Formats/Bmp/BmpDecoder.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
4343
{
4444
Size dims = decoder.Dimensions;
4545

46-
// TODO: use InvalidImageContentException here, if we decide to define it
47-
// https://github.com/SixLabors/ImageSharp/issues/1110
48-
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);
46+
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);
4947
}
5048
}
5149

src/ImageSharp/Formats/Gif/GifDecoder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
3838
{
3939
Size dims = decoder.Dimensions;
4040

41-
// TODO: use InvalidImageContentException here, if we decide to define it
42-
// https://github.com/SixLabors/ImageSharp/issues/1110
43-
throw new ImageFormatException($"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {dims.Width}x{dims.Height}.", ex);
41+
GifThrowHelper.ThrowInvalidImageContentException($"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {dims.Width}x{dims.Height}.", ex);
42+
43+
// Not reachable, as the previous statement will throw a exception.
44+
return null;
4445
}
4546
}
4647

src/ImageSharp/Formats/Gif/GifDecoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public GifDecoderCore(Configuration configuration, IGifDecoderOptions options)
101101
/// Decodes the stream to the image.
102102
/// </summary>
103103
/// <typeparam name="TPixel">The pixel format.</typeparam>
104-
/// <param name="stream">The stream containing image data. </param>
104+
/// <param name="stream">The stream containing image data.</param>
105105
/// <returns>The decoded image</returns>
106106
public Image<TPixel> Decode<TPixel>(Stream stream)
107107
where TPixel : unmanaged, IPixel<TPixel>

src/ImageSharp/Formats/Gif/GifThrowHelper.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors and contributors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
45
using System.Runtime.CompilerServices;
56

67
namespace SixLabors.ImageSharp.Formats.Gif
@@ -11,8 +12,17 @@ internal static class GifThrowHelper
1112
/// Cold path optimization for throwing <see cref="InvalidImageContentException"/>'s
1213
/// </summary>
1314
/// <param name="errorMessage">The error message for the exception.</param>
14-
[MethodImpl(MethodImplOptions.NoInlining)]
15+
[MethodImpl(InliningOptions.ColdPath)]
1516
public static void ThrowInvalidImageContentException(string errorMessage)
1617
=> throw new InvalidImageContentException(errorMessage);
18+
19+
/// <summary>
20+
/// Cold path optimization for throwing <see cref="InvalidImageContentException"/>'s.
21+
/// </summary>
22+
/// <param name="errorMessage">The error message for the exception.</param>
23+
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference
24+
/// if no inner exception is specified.</param>
25+
[MethodImpl(InliningOptions.ColdPath)]
26+
public static void ThrowInvalidImageContentException(string errorMessage, Exception innerException) => throw new InvalidImageContentException(errorMessage, innerException);
1727
}
1828
}

src/ImageSharp/Formats/Jpeg/JpegDecoder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
3232
{
3333
(int w, int h) = (decoder.ImageWidth, decoder.ImageHeight);
3434

35-
// TODO: use InvalidImageContentException here, if we decide to define it
36-
// https://github.com/SixLabors/ImageSharp/issues/1110
37-
throw new ImageFormatException($"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {w}x{h}.", ex);
35+
JpegThrowHelper.ThrowInvalidImageContentException($"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {w}x{h}.", ex);
36+
37+
// Not reachable, as the previous statement will throw a exception.
38+
return null;
3839
}
3940
}
4041

src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ internal static class JpegThrowHelper
1515
[MethodImpl(InliningOptions.ColdPath)]
1616
public static void ThrowInvalidImageContentException(string errorMessage) => throw new InvalidImageContentException(errorMessage);
1717

18+
/// <summary>
19+
/// Cold path optimization for throwing <see cref="InvalidImageContentException"/>'s.
20+
/// </summary>
21+
/// <param name="errorMessage">The error message for the exception.</param>
22+
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference
23+
/// if no inner exception is specified.</param>
24+
[MethodImpl(InliningOptions.ColdPath)]
25+
public static void ThrowInvalidImageContentException(string errorMessage, Exception innerException) => throw new InvalidImageContentException(errorMessage, innerException);
26+
1827
/// <summary>
1928
/// Cold path optimization for throwing <see cref="NotImplementedException"/>'s
2029
/// </summary>

src/ImageSharp/Formats/Png/PngDecoder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
5454
{
5555
Size dims = decoder.Dimensions;
5656

57-
// TODO: use InvalidImageContentException here, if we decide to define it
58-
// https://github.com/SixLabors/ImageSharp/issues/1110
59-
throw new ImageFormatException($"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {dims.Width}x{dims.Height}.", ex);
57+
PngThrowHelper.ThrowInvalidImageContentException($"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {dims.Width}x{dims.Height}.", ex);
58+
59+
// Not reachable, as the previous statement will throw a exception.
60+
return null;
6061
}
6162
}
6263

src/ImageSharp/Formats/Png/PngThrowHelper.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ namespace SixLabors.ImageSharp.Formats.Png
1111
/// </summary>
1212
internal static class PngThrowHelper
1313
{
14+
[MethodImpl(InliningOptions.ColdPath)]
15+
public static void ThrowInvalidImageContentException(string errorMessage, Exception innerException)
16+
=> throw new InvalidImageContentException(errorMessage, innerException);
17+
1418
[MethodImpl(InliningOptions.ColdPath)]
1519
public static void ThrowNoHeader() => throw new InvalidImageContentException("PNG Image does not contain a header chunk");
1620

src/ImageSharp/Formats/Tga/TgaDecoder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
2828
{
2929
Size dims = decoder.Dimensions;
3030

31-
// TODO: use InvalidImageContentException here, if we decide to define it
32-
// https://github.com/SixLabors/ImageSharp/issues/1110
33-
throw new ImageFormatException($"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {dims.Width}x{dims.Height}.", ex);
31+
TgaThrowHelper.ThrowInvalidImageContentException($"Can not decode image. Failed to allocate buffers for possibly degenerate dimensions: {dims.Width}x{dims.Height}.", ex);
32+
33+
// Not reachable, as the previous statement will throw a exception.
34+
return null;
3435
}
3536
}
3637

0 commit comments

Comments
 (0)