Browse Source

Fix options calculation precedence

af/octree-no-pixelmap
James Jackson-South 6 years ago
parent
commit
02199fdd2b
  1. 2
      src/ImageSharp/Formats/Png/PngEncoderCore.cs
  2. 14
      src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs
  3. 4
      src/ImageSharp/Formats/Png/PngMetadata.cs

2
src/ImageSharp/Formats/Png/PngEncoderCore.cs

@ -144,7 +144,7 @@ namespace SixLabors.ImageSharp.Formats.Png
this.height = image.Height; this.height = image.Height;
ImageMetadata metadata = image.Metadata; ImageMetadata metadata = image.Metadata;
PngMetadata pngMetadata = metadata.GetFormatMetadata(PngFormat.Instance); PngMetadata pngMetadata = metadata.GetPngMetadata();
PngEncoderOptionsHelpers.AdjustOptions<TPixel>(this.options, pngMetadata, out this.use16Bit, out this.bytesPerPixel); PngEncoderOptionsHelpers.AdjustOptions<TPixel>(this.options, pngMetadata, out this.use16Bit, out this.bytesPerPixel);
IQuantizedFrame<TPixel> quantized = PngEncoderOptionsHelpers.CreateQuantizedFrame(this.options, image); IQuantizedFrame<TPixel> quantized = PngEncoderOptionsHelpers.CreateQuantizedFrame(this.options, image);
this.bitDepth = PngEncoderOptionsHelpers.CalculateBitDepth(this.options, image, quantized); this.bitDepth = PngEncoderOptionsHelpers.CalculateBitDepth(this.options, image, quantized);

14
src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs

@ -30,8 +30,10 @@ namespace SixLabors.ImageSharp.Formats.Png
// Always take the encoder options over the metadata values. // Always take the encoder options over the metadata values.
options.Gamma ??= pngMetadata.Gamma; options.Gamma ??= pngMetadata.Gamma;
options.ColorType ??= SuggestColorType<TPixel>() ?? pngMetadata.ColorType; // Use options, then check metadata, if nothing set there then we suggest
options.BitDepth ??= SuggestBitDepth<TPixel>() ?? pngMetadata.BitDepth; // a sensible default based upon the pixel format.
options.ColorType ??= pngMetadata.ColorType ?? SuggestColorType<TPixel>();
options.BitDepth ??= pngMetadata.BitDepth ?? SuggestBitDepth<TPixel>();
options.InterlaceMethod ??= pngMetadata.InterlaceMethod; options.InterlaceMethod ??= pngMetadata.InterlaceMethod;
@ -148,7 +150,7 @@ namespace SixLabors.ImageSharp.Formats.Png
/// Returns a suggested <see cref="PngColorType"/> for the given <typeparamref name="TPixel"/> /// Returns a suggested <see cref="PngColorType"/> for the given <typeparamref name="TPixel"/>
/// This is not exhaustive but covers many common pixel formats. /// This is not exhaustive but covers many common pixel formats.
/// </summary> /// </summary>
private static PngColorType? SuggestColorType<TPixel>() private static PngColorType SuggestColorType<TPixel>()
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
return typeof(TPixel) switch 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(Rgb48) => PngColorType.Rgb,
Type t when t == typeof(Rgba64) => PngColorType.RgbWithAlpha, Type t when t == typeof(Rgba64) => PngColorType.RgbWithAlpha,
Type t when t == typeof(RgbaVector) => 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 <see cref="PngBitDepth"/> for the given <typeparamref name="TPixel"/> /// Returns a suggested <see cref="PngBitDepth"/> for the given <typeparamref name="TPixel"/>
/// This is not exhaustive but covers many common pixel formats. /// This is not exhaustive but covers many common pixel formats.
/// </summary> /// </summary>
private static PngBitDepth? SuggestBitDepth<TPixel>() private static PngBitDepth SuggestBitDepth<TPixel>()
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
return typeof(TPixel) switch 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(Rgb48) => PngBitDepth.Bit16,
Type t when t == typeof(Rgba64) => PngBitDepth.Bit16, Type t when t == typeof(Rgba64) => PngBitDepth.Bit16,
Type t when t == typeof(RgbaVector) => PngBitDepth.Bit16, Type t when t == typeof(RgbaVector) => PngBitDepth.Bit16,
_ => default(PngBitDepth?) _ => PngBitDepth.Bit8
}; };
} }
} }

4
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). /// Gets or sets the number of bits per sample or per palette index (not per pixel).
/// Not all values are allowed for all <see cref="ColorType"/> values. /// Not all values are allowed for all <see cref="ColorType"/> values.
/// </summary> /// </summary>
public PngBitDepth BitDepth { get; set; } = PngBitDepth.Bit8; public PngBitDepth? BitDepth { get; set; }
/// <summary> /// <summary>
/// Gets or sets the color type. /// Gets or sets the color type.
/// </summary> /// </summary>
public PngColorType ColorType { get; set; } = PngColorType.RgbWithAlpha; public PngColorType? ColorType { get; set; }
/// <summary> /// <summary>
/// Gets or sets a value indicating whether this instance should write an Adam7 interlaced image. /// Gets or sets a value indicating whether this instance should write an Adam7 interlaced image.

Loading…
Cancel
Save