Skip to content

Commit 819b2fa

Browse files
Fix options calculation precedence
1 parent b485452 commit 819b2fa

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/ImageSharp/Formats/Png/PngEncoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
144144
this.height = image.Height;
145145

146146
ImageMetadata metadata = image.Metadata;
147-
PngMetadata pngMetadata = metadata.GetFormatMetadata(PngFormat.Instance);
147+
PngMetadata pngMetadata = metadata.GetPngMetadata();
148148
PngEncoderOptionsHelpers.AdjustOptions<TPixel>(this.options, pngMetadata, out this.use16Bit, out this.bytesPerPixel);
149149
IQuantizedFrame<TPixel> quantized = PngEncoderOptionsHelpers.CreateQuantizedFrame(this.options, image);
150150
this.bitDepth = PngEncoderOptionsHelpers.CalculateBitDepth(this.options, image, quantized);

src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ public static void AdjustOptions<TPixel>(
3030
// Always take the encoder options over the metadata values.
3131
options.Gamma ??= pngMetadata.Gamma;
3232

33-
options.ColorType ??= SuggestColorType<TPixel>() ?? pngMetadata.ColorType;
34-
options.BitDepth ??= SuggestBitDepth<TPixel>() ?? pngMetadata.BitDepth;
33+
// Use options, then check metadata, if nothing set there then we suggest
34+
// a sensible default based upon the pixel format.
35+
options.ColorType ??= pngMetadata.ColorType ?? SuggestColorType<TPixel>();
36+
options.BitDepth ??= pngMetadata.BitDepth ?? SuggestBitDepth<TPixel>();
3537

3638
options.InterlaceMethod ??= pngMetadata.InterlaceMethod;
3739

@@ -148,7 +150,7 @@ private static int CalculateBytesPerPixel(PngColorType? pngColorType, bool use16
148150
/// Returns a suggested <see cref="PngColorType"/> for the given <typeparamref name="TPixel"/>
149151
/// This is not exhaustive but covers many common pixel formats.
150152
/// </summary>
151-
private static PngColorType? SuggestColorType<TPixel>()
153+
private static PngColorType SuggestColorType<TPixel>()
152154
where TPixel : struct, IPixel<TPixel>
153155
{
154156
return typeof(TPixel) switch
@@ -166,15 +168,15 @@ private static int CalculateBytesPerPixel(PngColorType? pngColorType, bool use16
166168
Type t when t == typeof(Rgb48) => PngColorType.Rgb,
167169
Type t when t == typeof(Rgba64) => PngColorType.RgbWithAlpha,
168170
Type t when t == typeof(RgbaVector) => PngColorType.RgbWithAlpha,
169-
_ => default(PngColorType?)
171+
_ => PngColorType.RgbWithAlpha
170172
};
171173
}
172174

173175
/// <summary>
174176
/// Returns a suggested <see cref="PngBitDepth"/> for the given <typeparamref name="TPixel"/>
175177
/// This is not exhaustive but covers many common pixel formats.
176178
/// </summary>
177-
private static PngBitDepth? SuggestBitDepth<TPixel>()
179+
private static PngBitDepth SuggestBitDepth<TPixel>()
178180
where TPixel : struct, IPixel<TPixel>
179181
{
180182
return typeof(TPixel) switch
@@ -192,7 +194,7 @@ private static int CalculateBytesPerPixel(PngColorType? pngColorType, bool use16
192194
Type t when t == typeof(Rgb48) => PngBitDepth.Bit16,
193195
Type t when t == typeof(Rgba64) => PngBitDepth.Bit16,
194196
Type t when t == typeof(RgbaVector) => PngBitDepth.Bit16,
195-
_ => default(PngBitDepth?)
197+
_ => PngBitDepth.Bit8
196198
};
197199
}
198200
}

src/ImageSharp/Formats/Png/PngMetadata.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ private PngMetadata(PngMetadata other)
4444
/// Gets or sets the number of bits per sample or per palette index (not per pixel).
4545
/// Not all values are allowed for all <see cref="ColorType"/> values.
4646
/// </summary>
47-
public PngBitDepth BitDepth { get; set; } = PngBitDepth.Bit8;
47+
public PngBitDepth? BitDepth { get; set; }
4848

4949
/// <summary>
5050
/// Gets or sets the color type.
5151
/// </summary>
52-
public PngColorType ColorType { get; set; } = PngColorType.RgbWithAlpha;
52+
public PngColorType? ColorType { get; set; }
5353

5454
/// <summary>
5555
/// Gets or sets a value indicating whether this instance should write an Adam7 interlaced image.

0 commit comments

Comments
 (0)