From 7ece3dd84a68b75281d748ed60bce5efa4a144c5 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Tue, 10 May 2022 17:11:53 +0300 Subject: [PATCH] Quality property fix --- .../Formats/Jpeg/JpegEncoderCore.cs | 20 +--------- src/ImageSharp/Formats/Jpeg/JpegMetadata.cs | 39 ++++++++----------- 2 files changed, 18 insertions(+), 41 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs index cbcaae2fc3..4c44e04961 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs @@ -122,7 +122,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg this.WriteDefineHuffmanTables(this.scanConfig.HuffmanTables); // Write the quantization tables. - this.InitQuantizationTables(this.scanConfig.QuantizationTables, jpegMetadata); + this.WriteDefineQuantizationTables(this.scanConfig.QuantizationTables, jpegMetadata); // Write the scan header. this.WriteStartOfScan(this.frameConfig.Components.Length, this.frameConfig.Components); @@ -188,22 +188,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg || colorType == JpegEncodingColor.Luminance || colorType == JpegEncodingColor.Rgb; - /// - /// Writes data to "Define Quantization Tables" block for QuantIndex. - /// - /// The "Define Quantization Tables" block. - /// Offset in "Define Quantization Tables" block. - /// The quantization index. - /// The quantization table to copy data from. - private static void WriteDataToDqt(byte[] dqt, ref int offset, QuantIndex i, ref Block8x8F quant) - { - dqt[offset++] = (byte)i; - for (int j = 0; j < Block8x8F.Size; j++) - { - dqt[offset++] = (byte)quant[ZigZag.ZigZagOrder[j]]; - } - } - /// /// Write the start of image marker. /// @@ -707,7 +691,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg /// /// Quantization tables configs. /// Jpeg metadata instance. - private void InitQuantizationTables(JpegQuantizationTableConfig[] configs, JpegMetadata metadata) + private void WriteDefineQuantizationTables(JpegQuantizationTableConfig[] configs, JpegMetadata metadata) { int dataLen = configs.Length * (1 + Block8x8.Size); diff --git a/src/ImageSharp/Formats/Jpeg/JpegMetadata.cs b/src/ImageSharp/Formats/Jpeg/JpegMetadata.cs index bb4cbeeee9..6c34036793 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegMetadata.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegMetadata.cs @@ -67,41 +67,34 @@ namespace SixLabors.ImageSharp.Formats.Jpeg } /// - /// Gets or sets the encoded quality. + /// Gets the encoded quality. /// /// /// Note that jpeg image can have different quality for luminance and chrominance components. - /// This property returns maximum value of luma/chroma qualities. + /// This property returns maximum value of luma/chroma qualities if both are present. /// public int Quality { get { - // Jpeg always has a luminance table thus it must have a luminance quality derived from it - if (!this.luminanceQuality.HasValue) + if (this.luminanceQuality.HasValue) { - return Quantization.DefaultQualityFactor; - } - - int lumaQuality = this.luminanceQuality.Value; + if (this.chrominanceQuality.HasValue) + { + return Math.Max(this.luminanceQuality.Value, this.chrominanceQuality.Value); + } - // Jpeg might not have a chrominance table - return luminance quality (grayscale images) - if (!this.chrominanceQuality.HasValue) - { - return lumaQuality; + return this.luminanceQuality.Value; } + else + { + if (this.chrominanceQuality.HasValue) + { + return this.chrominanceQuality.Value; + } - int chromaQuality = this.chrominanceQuality.Value; - - // Theoretically, luma quality would always be greater or equal to chroma quality - // But we've already encountered images which can have higher quality of chroma components - return Math.Max(lumaQuality, chromaQuality); - } - - set - { - this.LuminanceQuality = value; - this.ChrominanceQuality = value; + return Quantization.DefaultQualityFactor; + } } }