diff --git a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs
index 744f82bda..b7459bdc7 100644
--- a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs
+++ b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs
@@ -86,9 +86,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
private readonly int? quality;
///
- /// Component count.
+ /// Gets or sets the subsampling method to use.
///
- private readonly int componentCount;
+ private readonly JpegColorType? colorType;
///
/// The output stream. All attempted writes after the first error become no-ops.
@@ -103,7 +103,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
{
this.quality = options.Quality;
this.subsample = options.Subsample;
- this.componentCount = (options.ColorType == JpegColorType.Luminance) ? 1 : 3;
+ this.colorType = options.ColorType;
}
///
@@ -129,6 +129,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
this.outputStream = stream;
ImageMetadata metadata = image.Metadata;
+ // Compute number of components based on color type in options.
+ int componentCount = (this.colorType == JpegColorType.Luminance) ? 1 : 3;
+
// System.Drawing produces identical output for jpegs with a quality parameter of 0 and 1.
int qlty = Numerics.Clamp(this.quality ?? metadata.GetJpegMetadata().Quality, 1, 100);
this.subsample ??= qlty >= 91 ? JpegSubsample.Ratio444 : JpegSubsample.Ratio420;
@@ -150,7 +153,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
Block8x8F luminanceQuantTable = default;
Block8x8F chrominanceQuantTable = default;
InitQuantizationTable(0, scale, ref luminanceQuantTable);
- if (this.componentCount > 1)
+ if (componentCount > 1)
{
InitQuantizationTable(1, scale, ref chrominanceQuantTable);
}
@@ -175,7 +178,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
// Write the scan compressed data.
var scanEncoder = new HuffmanScanEncoder(stream);
- if (this.componentCount == 1)
+ if (this.colorType == JpegColorType.Luminance)
{
scanEncoder.EncodeGrayscale(image, ref luminanceQuantTable, cancellationToken);
}
@@ -586,7 +589,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
0x01
};
- if (this.componentCount == 1)
+ if (this.colorType == JpegColorType.Luminance)
{
subsamples = stackalloc byte[]
{