Skip to content

Commit 6df81b3

Browse files
Merge pull request #1179 from SixLabors/js/fix-1110
Add exception type and update xml docs.
2 parents 9c25d6c + c32cc6e commit 6df81b3

23 files changed

+528
-238
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
namespace SixLabors.ImageSharp
5+
{
6+
/// <summary>
7+
/// The exception that is thrown when the library tries to load
8+
/// an image which contains invalid content.
9+
/// </summary>
10+
public sealed class InvalidImageContentException : ImageFormatException
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="InvalidImageContentException"/> class with the name of the
14+
/// parameter that causes this exception.
15+
/// </summary>
16+
/// <param name="errorMessage">The error message that explains the reason for this exception.</param>
17+
public InvalidImageContentException(string errorMessage)
18+
: base(errorMessage)
19+
{
20+
}
21+
}
22+
}

src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ private void UncompressRle4(int w, Span<byte> buffer, Span<bool> undefinedPixels
462462
{
463463
if (this.stream.Read(cmd, 0, cmd.Length) != 2)
464464
{
465-
BmpThrowHelper.ThrowImageFormatException("Failed to read 2 bytes from the stream while uncompressing RLE4 bitmap.");
465+
BmpThrowHelper.ThrowInvalidImageContentException("Failed to read 2 bytes from the stream while uncompressing RLE4 bitmap.");
466466
}
467467

468468
if (cmd[0] == RleCommand)
@@ -569,7 +569,7 @@ private void UncompressRle8(int w, Span<byte> buffer, Span<bool> undefinedPixels
569569
{
570570
if (this.stream.Read(cmd, 0, cmd.Length) != 2)
571571
{
572-
BmpThrowHelper.ThrowImageFormatException("Failed to read 2 bytes from stream while uncompressing RLE8 bitmap.");
572+
BmpThrowHelper.ThrowInvalidImageContentException("Failed to read 2 bytes from stream while uncompressing RLE8 bitmap.");
573573
}
574574

575575
if (cmd[0] == RleCommand)
@@ -648,7 +648,7 @@ private void UncompressRle24(int w, Span<byte> buffer, Span<bool> undefinedPixel
648648
{
649649
if (this.stream.Read(cmd, 0, cmd.Length) != 2)
650650
{
651-
BmpThrowHelper.ThrowImageFormatException("Failed to read 2 bytes from stream while uncompressing RLE24 bitmap.");
651+
BmpThrowHelper.ThrowInvalidImageContentException("Failed to read 2 bytes from stream while uncompressing RLE24 bitmap.");
652652
}
653653

654654
if (cmd[0] == RleCommand)
@@ -1431,7 +1431,7 @@ private int ReadImageHeaders(Stream stream, out bool inverted, out byte[] palett
14311431
// Make sure, that we will not read pass the bitmap offset (starting position of image data).
14321432
if ((this.stream.Position + colorMapSizeBytes) > this.fileHeader.Offset)
14331433
{
1434-
BmpThrowHelper.ThrowImageFormatException(
1434+
BmpThrowHelper.ThrowInvalidImageContentException(
14351435
$"Reading the color map would read beyond the bitmap offset. Either the color map size of '{colorMapSizeBytes}' is invalid or the bitmap offset.");
14361436
}
14371437

@@ -1445,7 +1445,7 @@ private int ReadImageHeaders(Stream stream, out bool inverted, out byte[] palett
14451445
int skipAmount = this.fileHeader.Offset - (int)this.stream.Position;
14461446
if ((skipAmount + (int)this.stream.Position) > this.stream.Length)
14471447
{
1448-
BmpThrowHelper.ThrowImageFormatException("Invalid fileheader offset found. Offset is greater than the stream length.");
1448+
BmpThrowHelper.ThrowInvalidImageContentException("Invalid fileheader offset found. Offset is greater than the stream length.");
14491449
}
14501450

14511451
if (skipAmount > 0)

src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ public static BmpInfoHeader ParseOs2Version2(ReadOnlySpan<byte> data)
393393
break;
394394
default:
395395
// Compression type 3 (1DHuffman) is not supported.
396-
BmpThrowHelper.ThrowImageFormatException("Compression type is not supported. ImageSharp only supports uncompressed, RLE4, RLE8 and RLE24.");
396+
BmpThrowHelper.ThrowInvalidImageContentException("Compression type is not supported. ImageSharp only supports uncompressed, RLE4, RLE8 and RLE24.");
397397
break;
398398
}
399399

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

44
using System;
@@ -9,23 +9,19 @@ namespace SixLabors.ImageSharp.Formats.Bmp
99
internal static class BmpThrowHelper
1010
{
1111
/// <summary>
12-
/// Cold path optimization for throwing <see cref="ImageFormatException"/>-s
12+
/// Cold path optimization for throwing <see cref="InvalidImageContentException"/>'s
1313
/// </summary>
1414
/// <param name="errorMessage">The error message for the exception.</param>
1515
[MethodImpl(MethodImplOptions.NoInlining)]
16-
public static void ThrowImageFormatException(string errorMessage)
17-
{
18-
throw new ImageFormatException(errorMessage);
19-
}
16+
public static void ThrowInvalidImageContentException(string errorMessage)
17+
=> throw new InvalidImageContentException(errorMessage);
2018

2119
/// <summary>
22-
/// Cold path optimization for throwing <see cref="NotSupportedException"/>-s
20+
/// Cold path optimization for throwing <see cref="NotSupportedException"/>'s
2321
/// </summary>
2422
/// <param name="errorMessage">The error message for the exception.</param>
2523
[MethodImpl(MethodImplOptions.NoInlining)]
2624
public static void ThrowNotSupportedException(string errorMessage)
27-
{
28-
throw new NotSupportedException(errorMessage);
29-
}
25+
=> throw new NotSupportedException(errorMessage);
3026
}
3127
}

src/ImageSharp/Formats/Gif/GifDecoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ private void ReadComments()
318318
{
319319
if (length > GifConstants.MaxCommentSubBlockLength)
320320
{
321-
throw new ImageFormatException($"Gif comment length '{length}' exceeds max '{GifConstants.MaxCommentSubBlockLength}' of a comment data block");
321+
GifThrowHelper.ThrowInvalidImageContentException($"Gif comment length '{length}' exceeds max '{GifConstants.MaxCommentSubBlockLength}' of a comment data block");
322322
}
323323

324324
if (this.IgnoreMetadata)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.Runtime.CompilerServices;
5+
6+
namespace SixLabors.ImageSharp.Formats.Gif
7+
{
8+
internal static class GifThrowHelper
9+
{
10+
/// <summary>
11+
/// Cold path optimization for throwing <see cref="InvalidImageContentException"/>'s
12+
/// </summary>
13+
/// <param name="errorMessage">The error message for the exception.</param>
14+
[MethodImpl(MethodImplOptions.NoInlining)]
15+
public static void ThrowInvalidImageContentException(string errorMessage)
16+
=> throw new InvalidImageContentException(errorMessage);
17+
}
18+
}

src/ImageSharp/Formats/Jpeg/Components/Decoder/ColorConverters/JpegColorConverter.FromYCbCrSimd.cs

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

44
using System;
@@ -104,7 +104,7 @@ internal static void ConvertCore(in ComponentValues values, Span<Vector4> result
104104
{
105105
// TODO: Run fallback scalar code here
106106
// However, no issues expected before someone implements this: https://github.com/dotnet/coreclr/issues/12007
107-
throw new NotImplementedException("Your CPU architecture is too modern!");
107+
JpegThrowHelper.ThrowNotImplementedException("Your CPU architecture is too modern!");
108108
}
109109

110110
// Collect (r0,r1...r8) (g0,g1...g8) (b0,b1...b8) vector values in the expected (r0,g0,g1,1), (r1,g1,g2,1) ... order:

src/ImageSharp/Formats/Jpeg/Components/Decoder/JFifMarker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ private JFifMarker(byte majorVersion, byte minorVersion, byte densityUnits, shor
2929
{
3030
if (xDensity <= 0)
3131
{
32-
JpegThrowHelper.ThrowImageFormatException($"X-Density {xDensity} must be greater than 0.");
32+
JpegThrowHelper.ThrowInvalidImageContentException($"X-Density {xDensity} must be greater than 0.");
3333
}
3434

3535
if (yDensity <= 0)
3636
{
37-
JpegThrowHelper.ThrowImageFormatException($"Y-Density {yDensity} must be greater than 0.");
37+
JpegThrowHelper.ThrowInvalidImageContentException($"Y-Density {yDensity} must be greater than 0.");
3838
}
3939

4040
this.MajorVersion = majorVersion;

src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public void ParseStream(Stream stream, bool metadataOnly = false)
259259
var fileMarker = new JpegFileMarker(this.markerBuffer[1], 0);
260260
if (fileMarker.Marker != JpegConstants.Markers.SOI)
261261
{
262-
JpegThrowHelper.ThrowImageFormatException("Missing SOI marker.");
262+
JpegThrowHelper.ThrowInvalidImageContentException("Missing SOI marker.");
263263
}
264264

265265
this.InputStream.Read(this.markerBuffer, 0, 2);
@@ -423,7 +423,7 @@ private JpegColorSpace DeduceJpegColorSpace()
423423
: JpegColorSpace.Cmyk;
424424
}
425425

426-
JpegThrowHelper.ThrowImageFormatException($"Unsupported color mode. Supported component counts 1, 3, and 4; found {this.ComponentCount}");
426+
JpegThrowHelper.ThrowInvalidImageContentException($"Unsupported color mode. Supported component counts 1, 3, and 4; found {this.ComponentCount}");
427427
return default;
428428
}
429429

@@ -821,7 +821,7 @@ private void ProcessStartOfFrameMarker(int remaining, in JpegFileMarker frameMar
821821
{
822822
if (this.Frame != null)
823823
{
824-
JpegThrowHelper.ThrowImageFormatException("Multiple SOF markers. Only single frame jpegs supported.");
824+
JpegThrowHelper.ThrowInvalidImageContentException("Multiple SOF markers. Only single frame jpegs supported.");
825825
}
826826

827827
// Read initial marker definitions.
@@ -831,7 +831,7 @@ private void ProcessStartOfFrameMarker(int remaining, in JpegFileMarker frameMar
831831
// We only support 8-bit and 12-bit precision.
832832
if (Array.IndexOf(this.supportedPrecisions, this.temp[0]) == -1)
833833
{
834-
JpegThrowHelper.ThrowImageFormatException("Only 8-Bit and 12-Bit precision supported.");
834+
JpegThrowHelper.ThrowInvalidImageContentException("Only 8-Bit and 12-Bit precision supported.");
835835
}
836836

837837
this.Precision = this.temp[0];
@@ -928,13 +928,13 @@ private void ProcessDefineHuffmanTablesMarker(int remaining)
928928
// Types 0..1 DC..AC
929929
if (tableType > 1)
930930
{
931-
JpegThrowHelper.ThrowImageFormatException("Bad Huffman Table type.");
931+
JpegThrowHelper.ThrowInvalidImageContentException("Bad Huffman Table type.");
932932
}
933933

934934
// Max tables of each type
935935
if (tableIndex > 3)
936936
{
937-
JpegThrowHelper.ThrowImageFormatException("Bad Huffman Table index.");
937+
JpegThrowHelper.ThrowInvalidImageContentException("Bad Huffman Table index.");
938938
}
939939

940940
this.InputStream.Read(huffmanData.Array, 0, 16);
@@ -953,7 +953,7 @@ private void ProcessDefineHuffmanTablesMarker(int remaining)
953953

954954
if (codeLengthSum > 256 || codeLengthSum > length)
955955
{
956-
JpegThrowHelper.ThrowImageFormatException("Huffman table has excessive length.");
956+
JpegThrowHelper.ThrowInvalidImageContentException("Huffman table has excessive length.");
957957
}
958958

959959
using (IManagedByteBuffer huffmanValues = this.configuration.MemoryAllocator.AllocateManagedByteBuffer(256, AllocationOptions.Clean))
@@ -995,7 +995,7 @@ private void ProcessStartOfScanMarker()
995995
{
996996
if (this.Frame is null)
997997
{
998-
JpegThrowHelper.ThrowImageFormatException("No readable SOFn (Start Of Frame) marker found.");
998+
JpegThrowHelper.ThrowInvalidImageContentException("No readable SOFn (Start Of Frame) marker found.");
999999
}
10001000

10011001
int selectorsCount = this.InputStream.ReadByte();
@@ -1016,7 +1016,7 @@ private void ProcessStartOfScanMarker()
10161016

10171017
if (componentIndex < 0)
10181018
{
1019-
JpegThrowHelper.ThrowImageFormatException($"Unknown component selector {componentIndex}.");
1019+
JpegThrowHelper.ThrowInvalidImageContentException($"Unknown component selector {componentIndex}.");
10201020
}
10211021

10221022
ref JpegComponent component = ref this.Frame.Components[componentIndex];
Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
1-
// Copyright (c) Six Labors and contributors.
1+
// 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.Jpeg
78
{
89
internal static class JpegThrowHelper
910
{
1011
/// <summary>
11-
/// Cold path optimization for throwing <see cref="ImageFormatException"/>'s.
12+
/// Cold path optimization for throwing <see cref="InvalidImageContentException"/>'s.
1213
/// </summary>
1314
/// <param name="errorMessage">The error message for the exception.</param>
1415
[MethodImpl(InliningOptions.ColdPath)]
15-
public static void ThrowImageFormatException(string errorMessage) => throw new ImageFormatException(errorMessage);
16+
public static void ThrowInvalidImageContentException(string errorMessage) => throw new InvalidImageContentException(errorMessage);
17+
18+
/// <summary>
19+
/// Cold path optimization for throwing <see cref="NotImplementedException"/>'s
20+
/// </summary>
21+
/// <param name="errorMessage">The error message for the exception.</param>
22+
[MethodImpl(InliningOptions.ColdPath)]
23+
public static void ThrowNotImplementedException(string errorMessage)
24+
=> throw new NotImplementedException(errorMessage);
1625

1726
[MethodImpl(InliningOptions.ColdPath)]
18-
public static void ThrowBadMarker(string marker, int length) => throw new ImageFormatException($"Marker {marker} has bad length {length}.");
27+
public static void ThrowBadMarker(string marker, int length) => throw new InvalidImageContentException($"Marker {marker} has bad length {length}.");
1928

2029
[MethodImpl(InliningOptions.ColdPath)]
21-
public static void ThrowBadQuantizationTable() => throw new ImageFormatException("Bad Quantization Table index.");
30+
public static void ThrowBadQuantizationTable() => throw new InvalidImageContentException("Bad Quantization Table index.");
2231

2332
[MethodImpl(InliningOptions.ColdPath)]
24-
public static void ThrowBadSampling() => throw new ImageFormatException("Bad sampling factor.");
33+
public static void ThrowBadSampling() => throw new InvalidImageContentException("Bad sampling factor.");
2534

2635
[MethodImpl(InliningOptions.ColdPath)]
27-
public static void ThrowBadProgressiveScan(int ss, int se, int ah, int al) => throw new ImageFormatException($"Invalid progressive parameters Ss={ss} Se={se} Ah={ah} Al={al}.");
36+
public static void ThrowBadProgressiveScan(int ss, int se, int ah, int al) => throw new InvalidImageContentException($"Invalid progressive parameters Ss={ss} Se={se} Ah={ah} Al={al}.");
2837

2938
[MethodImpl(InliningOptions.ColdPath)]
30-
public static void ThrowInvalidImageDimensions(int width, int height) => throw new ImageFormatException($"Invalid image dimensions: {width}x{height}.");
39+
public static void ThrowInvalidImageDimensions(int width, int height) => throw new InvalidImageContentException($"Invalid image dimensions: {width}x{height}.");
3140
}
32-
}
41+
}

0 commit comments

Comments
 (0)