From 88bb74f39ff9c79a72e1befb5e89e9f6674449ce Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 10 Oct 2021 18:21:47 +1100 Subject: [PATCH] Fix #1712 --- src/ImageSharp/Formats/Jpeg/JpegEncoder.cs | 25 ----------- .../Formats/Jpeg/JpegEncoderCore.cs | 42 +++++++++++++++++-- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegEncoder.cs b/src/ImageSharp/Formats/Jpeg/JpegEncoder.cs index 6f116f4fb..fc6e3189f 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegEncoder.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegEncoder.cs @@ -29,7 +29,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg where TPixel : unmanaged, IPixel { var encoder = new JpegEncoderCore(this); - this.InitializeColorType(image); encoder.Encode(image, stream); } @@ -45,31 +44,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg where TPixel : unmanaged, IPixel { var encoder = new JpegEncoderCore(this); - this.InitializeColorType(image); return encoder.EncodeAsync(image, stream, cancellationToken); } - - /// - /// If ColorType was not set, set it based on the given image. - /// - private void InitializeColorType(Image image) - where TPixel : unmanaged, IPixel - { - // First inspect the image metadata. - if (this.ColorType == null) - { - JpegMetadata metadata = image.Metadata.GetJpegMetadata(); - this.ColorType = metadata.ColorType; - } - - // Secondly, inspect the pixel type. - if (this.ColorType == null) - { - bool isGrayscale = - typeof(TPixel) == typeof(L8) || typeof(TPixel) == typeof(L16) || - typeof(TPixel) == typeof(La16) || typeof(TPixel) == typeof(La32); - this.ColorType = isGrayscale ? JpegColorType.Luminance : JpegColorType.YCbCrRatio420; - } - } } } diff --git a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs index 6ff887667..b39dc1315 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs @@ -86,10 +86,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg ImageMetadata metadata = image.Metadata; JpegMetadata jpegMetadata = metadata.GetJpegMetadata(); - // If the color type was not specified by the user, preserve the color type of the input image, if it's a supported color type. - if (!this.colorType.HasValue && IsSupportedColorType(jpegMetadata.ColorType)) + // If the color type was not specified by the user, preserve the color type of the input image. + if (!this.colorType.HasValue) { - this.colorType = jpegMetadata.ColorType; + this.colorType = SetFallbackColorType(image); } // Compute number of components based on color type in options. @@ -156,6 +156,42 @@ namespace SixLabors.ImageSharp.Formats.Jpeg stream.Flush(); } + /// + /// If color type was not set, set it based on the given image. + /// Note, if there is no metadata and the image has multiple components this method + /// returns defering the field assignment + /// to . + /// + private static JpegColorType? SetFallbackColorType(Image image) + where TPixel : unmanaged, IPixel + { + // First inspect the image metadata. + JpegColorType? colorType = null; + JpegMetadata metadata = image.Metadata.GetJpegMetadata(); + if (IsSupportedColorType(metadata.ColorType)) + { + colorType = metadata.ColorType; + } + + // Secondly, inspect the pixel type. + // TODO: PixelTypeInfo should contain a component count! + if (colorType is null) + { + bool isGrayscale = + typeof(TPixel) == typeof(L8) || typeof(TPixel) == typeof(L16) || + typeof(TPixel) == typeof(La16) || typeof(TPixel) == typeof(La32); + + // We don't set multi-component color types here since we can set it based upon + // the quality in InitQuantizationTables. + if (isGrayscale) + { + colorType = JpegColorType.Luminance; + } + } + + return colorType; + } + /// /// Returns true, if the color type is supported by the encoder. ///