Browse Source

Clamp JPEG quality estimation results.

pull/2759/head
James Jackson-South 2 years ago
parent
commit
fae857fb1c
  1. 2
      src/ImageSharp/Formats/Jpeg/Components/Quantization.cs
  2. 27
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs
  3. 1
      tests/ImageSharp.Tests/TestImages.cs
  4. 3
      tests/Images/Input/Jpg/issues/issue-2758.jpg

2
src/ImageSharp/Formats/Jpeg/Components/Quantization.cs

@ -146,7 +146,7 @@ internal static class Quantization
quality = (int)Math.Round(5000.0 / sumPercent);
}
return quality;
return Numerics.Clamp(quality, MinQualityFactor, MaxQualityFactor);
}
/// <summary>

27
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs

@ -8,6 +8,7 @@ using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using SixLabors.ImageSharp.Metadata.Profiles.Icc;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Tests.TestUtilities;
// ReSharper disable InconsistentNaming
@ -425,6 +426,32 @@ public partial class JpegDecoderTests
VerifyEncodedStrings(exif);
}
// https://github.com/SixLabors/ImageSharp/issues/2758
[Theory]
[WithFile(TestImages.Jpeg.Issues.Issue2758, PixelTypes.L8)]
public void Issue2758_DecodeWorks<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage(JpegDecoder.Instance);
Assert.Equal(59787, image.Width);
Assert.Equal(511, image.Height);
JpegMetadata meta = image.Metadata.GetJpegMetadata();
// Quality determination should be between 1-100.
Assert.Equal(15, meta.LuminanceQuality);
Assert.Equal(1, meta.ChrominanceQuality);
// We want to test the encoder to ensure the determined values can be encoded but not by encoding
// the full size image as it would be too slow.
// We will crop the image to a smaller size and then encode it.
image.Mutate(x => x.Crop(new(0, 0, 100, 100)));
using MemoryStream ms = new();
image.Save(ms, new JpegEncoder());
}
private static void VerifyEncodedStrings(ExifProfile exif)
{
Assert.NotNull(exif);

1
tests/ImageSharp.Tests/TestImages.cs

@ -320,6 +320,7 @@ public static class TestImages
public const string HangBadScan = "Jpg/issues/Hang_C438A851.jpg";
public const string Issue2517 = "Jpg/issues/issue2517-bad-d7.jpg";
public const string Issue2638 = "Jpg/issues/Issue2638.jpg";
public const string Issue2758 = "Jpg/issues/issue-2758.jpg";
public static class Fuzz
{

3
tests/Images/Input/Jpg/issues/issue-2758.jpg

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:f32a238b57b7073f7442f8ae7efd6ba3ae4cda30d57e6666fb8a1eaa27108558
size 1412
Loading…
Cancel
Save