Skip to content

Commit 193dafe

Browse files
committed
Use ThrowHelper
1 parent 08de04b commit 193dafe

File tree

8 files changed

+52
-7
lines changed

8 files changed

+52
-7
lines changed

src/ImageSharp/Formats/Gif/GifDecoder.cs

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

41-
throw new InvalidImageContentException($"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;
4245
}
4346
}
4447

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
3232
{
3333
(int w, int h) = (decoder.ImageWidth, decoder.ImageHeight);
3434

35-
throw new InvalidImageContentException($"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;
3639
}
3740
}
3841

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
5454
{
5555
Size dims = decoder.Dimensions;
5656

57-
throw new InvalidImageContentException($"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;
5861
}
5962
}
6063

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
2828
{
2929
Size dims = decoder.Dimensions;
3030

31-
throw new InvalidImageContentException($"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;
3235
}
3336
}
3437

src/ImageSharp/Formats/Tga/TgaThrowHelper.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,25 @@ internal static class TgaThrowHelper
1212
/// Cold path optimization for throwing <see cref="ImageFormatException"/>'s
1313
/// </summary>
1414
/// <param name="errorMessage">The error message for the exception.</param>
15-
[MethodImpl(MethodImplOptions.NoInlining)]
15+
[MethodImpl(InliningOptions.ColdPath)]
1616
public static void ThrowInvalidImageContentException(string errorMessage)
1717
=> throw new InvalidImageContentException(errorMessage);
1818

19+
/// <summary>
20+
/// Cold path optimization for throwing <see cref="ImageFormatException"/>'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)
27+
=> throw new InvalidImageContentException(errorMessage, innerException);
28+
1929
/// <summary>
2030
/// Cold path optimization for throwing <see cref="NotSupportedException"/>'s
2131
/// </summary>
2232
/// <param name="errorMessage">The error message for the exception.</param>
23-
[MethodImpl(MethodImplOptions.NoInlining)]
33+
[MethodImpl(InliningOptions.ColdPath)]
2434
public static void ThrowNotSupportedException(string errorMessage)
2535
=> throw new NotSupportedException(errorMessage);
2636
}

0 commit comments

Comments
 (0)