diff --git a/src/ImageSharp/Formats/Jpeg/Components/Quantization.cs b/src/ImageSharp/Formats/Jpeg/Components/Quantization.cs index fb477dda85..fc602b7f85 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Quantization.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Quantization.cs @@ -23,6 +23,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components /// public const int MinQualityFactor = 1; + /// + /// Default JPEG quality for both luminance and chominance tables. + /// + public const int DefaultQualityFactor = 75; + /// /// Represents lowest quality setting which can be estimated with enough confidence. /// Any quality below it results in a highly compressed jpeg image diff --git a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs index 69ac5de714..828e03de7f 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs @@ -675,7 +675,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg } else { - chromaQuality = Numerics.Clamp(metadata.ChrominanceQuality, 1, 100); + chromaQuality = Numerics.Clamp(metadata.ChrominanceQuality ?? Quantization.DefaultQualityFactor, 1, 100); chrominanceQuantTable = metadata.ChromaQuantizationTable; } diff --git a/src/ImageSharp/Formats/Jpeg/JpegMetadata.cs b/src/ImageSharp/Formats/Jpeg/JpegMetadata.cs index a4f968d7c1..0d95599e27 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegMetadata.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegMetadata.cs @@ -11,17 +11,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg /// public class JpegMetadata : IDeepCloneable { - /// - /// Default JPEG quality for both luminance and chominance tables. - /// - private const int DefaultQualityValue = 75; - private Block8x8F? lumaQuantTable; private Block8x8F? chromaQuantTable; - private int? lumaQuality; - private int? chromaQuality; - /// /// Initializes a new instance of the class. /// @@ -55,7 +47,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg return this.lumaQuantTable.Value; } - return Quantization.ScaleLuminanceTable(this.LuminanceQuality); + return Quantization.ScaleLuminanceTable(this.LuminanceQuality ?? Quantization.DefaultQualityFactor); } set => this.lumaQuantTable = value; @@ -73,7 +65,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg return this.chromaQuantTable.Value; } - return Quantization.ScaleChrominanceTable(this.ChrominanceQuality); + return Quantization.ScaleChrominanceTable(this.ChrominanceQuality ?? Quantization.DefaultQualityFactor); } set => this.chromaQuantTable = value; @@ -86,11 +78,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg /// This value might not be accurate if it was calculated during jpeg decoding /// with non-complient ITU quantization tables. /// - public int LuminanceQuality - { - get => this.lumaQuality ?? DefaultQualityValue; - set => this.lumaQuality = value; - } + public int? LuminanceQuality { get; set; } /// /// Gets or sets the jpeg chrominance quality. @@ -99,11 +87,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg /// This value might not be accurate if it was calculated during jpeg decoding /// with non-complient ITU quantization tables. /// - public int ChrominanceQuality - { - get => this.chromaQuality ?? DefaultQualityValue; - set => this.chromaQuality = value; - } + public int? ChrominanceQuality { get; set; } /// /// Gets or sets the encoded quality. @@ -116,8 +100,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg { get { - int lumaQuality = this.lumaQuality ?? DefaultQualityValue; - int chromaQuality = this.chromaQuality ?? lumaQuality; + int lumaQuality = this.LuminanceQuality ?? Quantization.DefaultQualityFactor; + int chromaQuality = this.ChrominanceQuality ?? lumaQuality; return (int)Math.Round((lumaQuality + chromaQuality) / 2f); }