diff --git a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs
index fea24111ca..bc3a5f1b56 100644
--- a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs
+++ b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs
@@ -23,11 +23,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
///
internal sealed unsafe class JpegEncoderCore : IImageEncoderInternals
{
- ///
- /// Default JPEG encoding quality for both luminance and chominance tables.
- ///
- private const int DefaultQualityValue = 75;
-
///
/// The number of quantization tables.
///
@@ -659,50 +654,29 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
/// Output chrominance quantization table.
private void InitQuantizationTables(int componentCount, JpegMetadata metadata, out Block8x8F luminanceQuantTable, out Block8x8F chrominanceQuantTable)
{
- // encoder quality
if (this.luminanceQuality.HasValue)
{
int lumaQuality = Numerics.Clamp(this.luminanceQuality.Value, 1, 100);
luminanceQuantTable = Quantization.ScaleLuminanceTable(lumaQuality);
}
-
- // non-standard table
- else if (metadata.LuminanceQuantizationTable.HasValue)
- {
- luminanceQuantTable = metadata.LuminanceQuantizationTable.Value;
- }
-
- // metadata or default quality
else
{
- int lumaQuality = Numerics.Clamp(metadata.LuminanceQuality ?? DefaultQualityValue, 1, 100);
- luminanceQuantTable = Quantization.ScaleLuminanceTable(lumaQuality);
+ luminanceQuantTable = metadata.LuminanceQuantizationTable;
}
chrominanceQuantTable = default;
if (componentCount > 1)
{
int chromaQuality;
-
- // encoder quality
if (this.chrominanceQuality.HasValue)
{
chromaQuality = Numerics.Clamp(this.chrominanceQuality.Value, 1, 100);
chrominanceQuantTable = Quantization.ScaleLuminanceTable(chromaQuality);
}
-
- // non-standard table
- else if (metadata.ChromaQuantizationTable.HasValue)
- {
- chromaQuality = metadata.ChrominanceQuality.Value;
- chrominanceQuantTable = metadata.ChromaQuantizationTable.Value;
- }
-
- // metadata or default quality
else
{
- chromaQuality = Numerics.Clamp(metadata.ChrominanceQuality ?? DefaultQualityValue, 1, 100);
- chrominanceQuantTable = Quantization.ScaleChrominanceTable(chromaQuality);
+ chromaQuality = metadata.ChrominanceQuality;
+ chrominanceQuantTable = metadata.ChromaQuantizationTable;
}
this.subsample = chromaQuality >= 91 ? JpegSubsample.Ratio444 : JpegSubsample.Ratio420;
diff --git a/src/ImageSharp/Formats/Jpeg/JpegMetadata.cs b/src/ImageSharp/Formats/Jpeg/JpegMetadata.cs
index 4a58c6946c..6b7f42bb27 100644
--- a/src/ImageSharp/Formats/Jpeg/JpegMetadata.cs
+++ b/src/ImageSharp/Formats/Jpeg/JpegMetadata.cs
@@ -11,6 +11,17 @@ 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.
///
@@ -33,20 +44,40 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
}
///
- /// Gets or sets luminance qunatization table derived from jpeg image.
+ /// Gets or sets luminance qunatization table for jpeg image.
///
- ///
- /// Would be null if jpeg was encoded using table from ITU spec
- ///
- internal Block8x8F? LuminanceQuantizationTable { get; set; }
+ internal Block8x8F LuminanceQuantizationTable
+ {
+ get
+ {
+ if (this.lumaQuantTable.HasValue)
+ {
+ return this.lumaQuantTable.Value;
+ }
+
+ return Quantization.ScaleLuminanceTable(this.LuminanceQuality);
+ }
+
+ set => this.lumaQuantTable = value;
+ }
///
- /// Gets or sets chrominance qunatization table derived from jpeg image.
+ /// Gets or sets chrominance qunatization table for jpeg image.
///
- ///
- /// Would be null if jpeg was encoded using table from ITU spec
- ///
- internal Block8x8F? ChromaQuantizationTable { get; set; }
+ internal Block8x8F ChromaQuantizationTable
+ {
+ get
+ {
+ if (this.chromaQuantTable.HasValue)
+ {
+ return this.chromaQuantTable.Value;
+ }
+
+ return Quantization.ScaleChrominanceTable(this.ChrominanceQuality);
+ }
+
+ set => this.chromaQuantTable = value;
+ }
///
/// Gets or sets the jpeg luminance quality.
@@ -55,7 +86,11 @@ 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; set; }
+ public int LuminanceQuality
+ {
+ get => this.lumaQuality ?? DefaultQualityValue;
+ set => this.lumaQuality = value;
+ }
///
/// Gets or sets the jpeg chrominance quality.
@@ -64,30 +99,21 @@ 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; set; }
-
- ///
- /// Gets a value indicating whether jpeg luminance data was encoded using ITU complient quantization table.
- ///
- public bool UsesStandardLuminanceTable => !this.LuminanceQuantizationTable.HasValue;
-
- ///
- /// Gets a value indicating whether jpeg luminance data was encoded using ITU complient quantization table.
- ///
- public bool UsesStandardChrominanceTable => !this.ChromaQuantizationTable.HasValue;
+ public int ChrominanceQuality
+ {
+ get => this.chromaQuality ?? DefaultQualityValue;
+ set => this.chromaQuality = value;
+ }
///
/// Gets or sets the encoded quality.
///
public int Quality
{
- [Obsolete("This accessor will soon be deprecated. Use LuminanceQuality and ChrominanceQuality getters instead.", error: false)]
get
{
- const int defaultQuality = 75;
-
- int lumaQuality = this.LuminanceQuality ?? defaultQuality;
- int chromaQuality = this.LuminanceQuality ?? lumaQuality;
+ int lumaQuality = this.lumaQuality ?? DefaultQualityValue;
+ int chromaQuality = this.chromaQuality ?? lumaQuality;
return (int)Math.Round((lumaQuality + chromaQuality) / 2f);
}