|
|
@ -102,18 +102,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg |
|
|
// Compute number of components based on color type in options.
|
|
|
// Compute number of components based on color type in options.
|
|
|
int componentCount = (this.colorType == JpegColorType.Luminance) ? 1 : 3; |
|
|
int componentCount = (this.colorType == JpegColorType.Luminance) ? 1 : 3; |
|
|
|
|
|
|
|
|
// Initialize the quantization tables.
|
|
|
|
|
|
// TODO: Right now encoder writes both quantization tables for grayscale images - we shouldn't do that
|
|
|
// TODO: Right now encoder writes both quantization tables for grayscale images - we shouldn't do that
|
|
|
int lumaQuality = Numerics.Clamp(this.luminanceQuality ?? jpegMetadata.LuminanceQuality, 1, 100); |
|
|
// Initialize the quantization tables.
|
|
|
Block8x8F luminanceQuantTable = Quantization.ScaleLuminanceTable(lumaQuality); |
|
|
this.InitQuantizationTables(componentCount, jpegMetadata, out Block8x8F luminanceQuantTable, out Block8x8F chrominanceQuantTable); |
|
|
Block8x8F chrominanceQuantTable = default; |
|
|
|
|
|
if (componentCount > 1) |
|
|
|
|
|
{ |
|
|
|
|
|
int chromaQuality = Numerics.Clamp(this.chrominanceQuality ?? jpegMetadata.ChrominanceQuality, 1, 100); |
|
|
|
|
|
this.subsample ??= chromaQuality >= 91 ? JpegSubsample.Ratio444 : JpegSubsample.Ratio420; |
|
|
|
|
|
|
|
|
|
|
|
chrominanceQuantTable = Quantization.ScaleChrominanceTable(chromaQuality); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Write the Start Of Image marker.
|
|
|
// Write the Start Of Image marker.
|
|
|
this.WriteApplicationHeader(metadata); |
|
|
this.WriteApplicationHeader(metadata); |
|
|
@ -651,5 +642,39 @@ namespace SixLabors.ImageSharp.Formats.Jpeg |
|
|
this.buffer[3] = (byte)(length & 0xff); |
|
|
this.buffer[3] = (byte)(length & 0xff); |
|
|
this.outputStream.Write(this.buffer, 0, 4); |
|
|
this.outputStream.Write(this.buffer, 0, 4); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes quntization tables.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="componentCount">Color components count.</param>
|
|
|
|
|
|
/// <param name="metadata">Jpeg metadata instance.</param>
|
|
|
|
|
|
/// <param name="luminanceQuantTable">Output luminance quantization table.</param>
|
|
|
|
|
|
/// <param name="chrominanceQuantTable">Output chrominance quantization table.</param>
|
|
|
|
|
|
private void InitQuantizationTables(int componentCount, JpegMetadata metadata, out Block8x8F luminanceQuantTable, out Block8x8F chrominanceQuantTable) |
|
|
|
|
|
{ |
|
|
|
|
|
// We take quality values in a hierarchical order:
|
|
|
|
|
|
// 1. Check if encoder has set quality
|
|
|
|
|
|
// 2. Check if metadata has special table for encoding
|
|
|
|
|
|
// 3. Check if metadata has set quality
|
|
|
|
|
|
// 4. Take default quality value - 75
|
|
|
|
|
|
int lumaQuality = Numerics.Clamp( |
|
|
|
|
|
this.luminanceQuality ?? metadata.LuminanceQuality ?? DefaultQualityValue, |
|
|
|
|
|
min: 1, |
|
|
|
|
|
max: 100); |
|
|
|
|
|
|
|
|
|
|
|
luminanceQuantTable = Quantization.ScaleLuminanceTable(lumaQuality); |
|
|
|
|
|
chrominanceQuantTable = default; |
|
|
|
|
|
if (componentCount > 1) |
|
|
|
|
|
{ |
|
|
|
|
|
int chromaQuality = Numerics.Clamp( |
|
|
|
|
|
this.chrominanceQuality ?? metadata.ChrominanceQuality ?? DefaultQualityValue, |
|
|
|
|
|
min: 1, |
|
|
|
|
|
max: 100); |
|
|
|
|
|
|
|
|
|
|
|
this.subsample ??= chromaQuality >= 91 ? JpegSubsample.Ratio444 : JpegSubsample.Ratio420; |
|
|
|
|
|
|
|
|
|
|
|
chrominanceQuantTable = Quantization.ScaleChrominanceTable(chromaQuality); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|