From 02199fdd2b86ca09ca094cc1929b59bf8d7751c1 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 28 Jan 2020 00:32:22 +1100 Subject: [PATCH] Fix options calculation precedence --- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 2 +- .../Formats/Png/PngEncoderOptionsHelpers.cs | 14 ++++++++------ src/ImageSharp/Formats/Png/PngMetadata.cs | 4 ++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 7f4b5b93b..69a80e024 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -144,7 +144,7 @@ namespace SixLabors.ImageSharp.Formats.Png this.height = image.Height; ImageMetadata metadata = image.Metadata; - PngMetadata pngMetadata = metadata.GetFormatMetadata(PngFormat.Instance); + PngMetadata pngMetadata = metadata.GetPngMetadata(); PngEncoderOptionsHelpers.AdjustOptions(this.options, pngMetadata, out this.use16Bit, out this.bytesPerPixel); IQuantizedFrame quantized = PngEncoderOptionsHelpers.CreateQuantizedFrame(this.options, image); this.bitDepth = PngEncoderOptionsHelpers.CalculateBitDepth(this.options, image, quantized); diff --git a/src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs b/src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs index 619679248..b494c164f 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs @@ -30,8 +30,10 @@ namespace SixLabors.ImageSharp.Formats.Png // Always take the encoder options over the metadata values. options.Gamma ??= pngMetadata.Gamma; - options.ColorType ??= SuggestColorType() ?? pngMetadata.ColorType; - options.BitDepth ??= SuggestBitDepth() ?? pngMetadata.BitDepth; + // Use options, then check metadata, if nothing set there then we suggest + // a sensible default based upon the pixel format. + options.ColorType ??= pngMetadata.ColorType ?? SuggestColorType(); + options.BitDepth ??= pngMetadata.BitDepth ?? SuggestBitDepth(); options.InterlaceMethod ??= pngMetadata.InterlaceMethod; @@ -148,7 +150,7 @@ namespace SixLabors.ImageSharp.Formats.Png /// Returns a suggested for the given /// This is not exhaustive but covers many common pixel formats. /// - private static PngColorType? SuggestColorType() + private static PngColorType SuggestColorType() where TPixel : struct, IPixel { return typeof(TPixel) switch @@ -166,7 +168,7 @@ namespace SixLabors.ImageSharp.Formats.Png Type t when t == typeof(Rgb48) => PngColorType.Rgb, Type t when t == typeof(Rgba64) => PngColorType.RgbWithAlpha, Type t when t == typeof(RgbaVector) => PngColorType.RgbWithAlpha, - _ => default(PngColorType?) + _ => PngColorType.RgbWithAlpha }; } @@ -174,7 +176,7 @@ namespace SixLabors.ImageSharp.Formats.Png /// Returns a suggested for the given /// This is not exhaustive but covers many common pixel formats. /// - private static PngBitDepth? SuggestBitDepth() + private static PngBitDepth SuggestBitDepth() where TPixel : struct, IPixel { return typeof(TPixel) switch @@ -192,7 +194,7 @@ namespace SixLabors.ImageSharp.Formats.Png Type t when t == typeof(Rgb48) => PngBitDepth.Bit16, Type t when t == typeof(Rgba64) => PngBitDepth.Bit16, Type t when t == typeof(RgbaVector) => PngBitDepth.Bit16, - _ => default(PngBitDepth?) + _ => PngBitDepth.Bit8 }; } } diff --git a/src/ImageSharp/Formats/Png/PngMetadata.cs b/src/ImageSharp/Formats/Png/PngMetadata.cs index 87a2080f0..341fc53ed 100644 --- a/src/ImageSharp/Formats/Png/PngMetadata.cs +++ b/src/ImageSharp/Formats/Png/PngMetadata.cs @@ -44,12 +44,12 @@ namespace SixLabors.ImageSharp.Formats.Png /// Gets or sets the number of bits per sample or per palette index (not per pixel). /// Not all values are allowed for all values. /// - public PngBitDepth BitDepth { get; set; } = PngBitDepth.Bit8; + public PngBitDepth? BitDepth { get; set; } /// /// Gets or sets the color type. /// - public PngColorType ColorType { get; set; } = PngColorType.RgbWithAlpha; + public PngColorType? ColorType { get; set; } /// /// Gets or sets a value indicating whether this instance should write an Adam7 interlaced image.