Browse Source

Merge branch 'master' into gifTests

pull/1574/head
Brian Popow 6 years ago
committed by GitHub
parent
commit
f4539b93d2
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      src/ImageSharp/Common/Exceptions/InvalidImageContentException.cs
  2. 4
      src/ImageSharp/Formats/Bmp/BmpDecoder.cs
  3. 7
      src/ImageSharp/Formats/Gif/GifDecoder.cs
  4. 12
      src/ImageSharp/Formats/Gif/GifThrowHelper.cs
  5. 7
      src/ImageSharp/Formats/Jpeg/JpegDecoder.cs
  6. 9
      src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs
  7. 7
      src/ImageSharp/Formats/Png/PngDecoder.cs
  8. 24
      src/ImageSharp/Formats/Png/PngMetadata.cs
  9. 4
      src/ImageSharp/Formats/Png/PngThrowHelper.cs
  10. 7
      src/ImageSharp/Formats/Tga/TgaDecoder.cs
  11. 14
      src/ImageSharp/Formats/Tga/TgaThrowHelper.cs
  12. 2
      tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs
  13. 2
      tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs
  14. 2
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
  15. 2
      tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs
  16. 2
      tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs

14
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
{
/// <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)
{
}
}
}

4
src/ImageSharp/Formats/Bmp/BmpDecoder.cs

@ -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);
}
}

7
src/ImageSharp/Formats/Gif/GifDecoder.cs

@ -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;
}
}

12
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 @@ 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);
}
}

7
src/ImageSharp/Formats/Jpeg/JpegDecoder.cs

@ -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;
}
}

9
src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs

@ -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>

7
src/ImageSharp/Formats/Png/PngDecoder.cs

@ -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;
}
}

24
src/ImageSharp/Formats/Png/PngMetadata.cs

@ -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;
}
}
}

4
src/ImageSharp/Formats/Png/PngThrowHelper.cs

@ -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");

7
src/ImageSharp/Formats/Tga/TgaDecoder.cs

@ -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;
}
}

14
src/ImageSharp/Formats/Tga/TgaThrowHelper.cs

@ -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);
}

2
tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs

@ -77,7 +77,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
where TPixel : unmanaged, IPixel<TPixel>
{
provider.LimitAllocatorBufferCapacity().InPixelsSqrt(10);
ImageFormatException ex = Assert.Throws<ImageFormatException>(() => provider.GetImage(BmpDecoder));
InvalidImageContentException ex = Assert.Throws<InvalidImageContentException>(() => provider.GetImage(BmpDecoder));
Assert.IsType<InvalidMemoryOperationException>(ex.InnerException);
}

2
tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs

@ -191,7 +191,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Gif
where TPixel : unmanaged, IPixel<TPixel>
{
provider.LimitAllocatorBufferCapacity().InPixelsSqrt(10);
ImageFormatException ex = Assert.Throws<ImageFormatException>(() => provider.GetImage(GifDecoder));
InvalidImageContentException ex = Assert.Throws<InvalidImageContentException>(() => provider.GetImage(GifDecoder));
Assert.IsType<InvalidMemoryOperationException>(ex.InnerException);
}

2
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

@ -107,7 +107,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
where TPixel : unmanaged, IPixel<TPixel>
{
provider.LimitAllocatorBufferCapacity().InBytesSqrt(10);
ImageFormatException ex = Assert.Throws<ImageFormatException>(() => provider.GetImage(JpegDecoder));
InvalidImageContentException ex = Assert.Throws<InvalidImageContentException>(() => provider.GetImage(JpegDecoder));
this.Output.WriteLine(ex.Message);
Assert.IsType<InvalidMemoryOperationException>(ex.InnerException);
}

2
tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs

@ -397,7 +397,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
where TPixel : unmanaged, IPixel<TPixel>
{
provider.LimitAllocatorBufferCapacity().InPixelsSqrt(10);
ImageFormatException ex = Assert.Throws<ImageFormatException>(() => provider.GetImage(PngDecoder));
InvalidImageContentException ex = Assert.Throws<InvalidImageContentException>(() => provider.GetImage(PngDecoder));
Assert.IsType<InvalidMemoryOperationException>(ex.InnerException);
}

2
tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs

@ -740,7 +740,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
where TPixel : unmanaged, IPixel<TPixel>
{
provider.LimitAllocatorBufferCapacity().InPixelsSqrt(10);
ImageFormatException ex = Assert.Throws<ImageFormatException>(() => provider.GetImage(TgaDecoder));
InvalidImageContentException ex = Assert.Throws<InvalidImageContentException>(() => provider.GetImage(TgaDecoder));
Assert.IsType<InvalidMemoryOperationException>(ex.InnerException);
}

Loading…
Cancel
Save