Browse Source

Added QoL throw helper method for jpeg w/h size check before encoding

pull/1632/head
Dmitry Pentin 5 years ago
parent
commit
5b05a0a1da
  1. 8
      src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs
  2. 3
      src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs

8
src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs

@ -118,14 +118,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
{ {
Guard.NotNull(image, nameof(image)); Guard.NotNull(image, nameof(image));
Guard.NotNull(stream, nameof(stream)); Guard.NotNull(stream, nameof(stream));
cancellationToken.ThrowIfCancellationRequested();
const ushort max = JpegConstants.MaxLength; if (image.Width >= JpegConstants.MaxLength || image.Height >= JpegConstants.MaxLength)
if (image.Width >= max || image.Height >= max)
{ {
throw new ImageFormatException($"Image is too large to encode at {image.Width}x{image.Height}."); JpegThrowHelper.ThrowDimensionsTooLarge(image.Width, image.Height);
} }
cancellationToken.ThrowIfCancellationRequested();
this.outputStream = stream; this.outputStream = stream;
ImageMetadata metadata = image.Metadata; ImageMetadata metadata = image.Metadata;

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

@ -46,5 +46,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
[MethodImpl(InliningOptions.ColdPath)] [MethodImpl(InliningOptions.ColdPath)]
public static void ThrowInvalidImageDimensions(int width, int height) => throw new InvalidImageContentException($"Invalid image dimensions: {width}x{height}."); public static void ThrowInvalidImageDimensions(int width, int height) => throw new InvalidImageContentException($"Invalid image dimensions: {width}x{height}.");
[MethodImpl(InliningOptions.ColdPath)]
public static void ThrowDimensionsTooLarge(int width, int height) => throw new ImageFormatException($"Image is too large to encode at {width}x{height} for JPEG format.");
} }
} }

Loading…
Cancel
Save