Skip to content

Commit

Permalink
Clamp JPEG quality estimation results.
Browse files Browse the repository at this point in the history
Signed-off-by: antonfirsov <antonfir@gmail.com>
  • Loading branch information
JimBobSquarePants authored and antonfirsov committed Jul 11, 2024
1 parent 4b910e7 commit 28c20de
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ImageSharp/Formats/Jpeg/Components/Quantization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static int EstimateQuality(ref Block8x8F table, ReadOnlySpan<byte> target
quality = (int)Math.Round(5000.0 / sumPercent);
}

return quality;
return Numerics.Clamp(quality, MinQualityFactor, MaxQualityFactor);
}

/// <summary>
Expand Down
27 changes: 27 additions & 0 deletions tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using SixLabors.ImageSharp.Metadata.Profiles.Icc;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

using Xunit;

Expand Down Expand Up @@ -361,6 +362,32 @@ public void EncodedStringTags_Read()
{
ExifProfile exif = image.Metadata.ExifProfile;
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());
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ public static class Issues
public const string ValidExifArgumentNullExceptionOnEncode = "Jpg/issues/Issue2087-exif-null-reference-on-encode.jpg";
public const string Issue2133DeduceColorSpace = "Jpg/issues/Issue2133.jpg";
public const string HangBadScan = "Jpg/issues/Hang_C438A851.jpg";
public const string Issue2758 = "Jpg/issues/issue-2758.jpg";

public static class Fuzz
{
Expand Down
3 changes: 3 additions & 0 deletions tests/Images/Input/Jpg/issues/issue-2758.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 28c20de

Please sign in to comment.