|
|
|
@ -35,6 +35,15 @@ namespace SixLabors.ImageSharp.Formats.Png |
|
|
|
options.ColorType ??= pngMetadata.ColorType ?? SuggestColorType<TPixel>(); |
|
|
|
options.BitDepth ??= pngMetadata.BitDepth ?? SuggestBitDepth<TPixel>(); |
|
|
|
|
|
|
|
// Ensure bit depth and color type are a supported combination.
|
|
|
|
// Bit8 is the only bit depth supported by all color types.
|
|
|
|
byte bits = (byte)options.BitDepth; |
|
|
|
byte[] validBitDepths = PngConstants.ColorTypes[options.ColorType.Value]; |
|
|
|
if (Array.IndexOf(validBitDepths, bits) == -1) |
|
|
|
{ |
|
|
|
options.BitDepth = PngBitDepth.Bit8; |
|
|
|
} |
|
|
|
|
|
|
|
options.InterlaceMethod ??= pngMetadata.InterlaceMethod; |
|
|
|
|
|
|
|
use16Bit = options.BitDepth == PngBitDepth.Bit16; |
|
|
|
@ -44,12 +53,6 @@ namespace SixLabors.ImageSharp.Formats.Png |
|
|
|
{ |
|
|
|
options.ChunkFilter = PngChunkFilter.ExcludeAll; |
|
|
|
} |
|
|
|
|
|
|
|
// Ensure we are not allowing impossible combinations.
|
|
|
|
if (!PngConstants.ColorTypes.ContainsKey(options.ColorType.Value)) |
|
|
|
{ |
|
|
|
throw new NotSupportedException("Color type is not supported or not valid."); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -68,15 +71,10 @@ namespace SixLabors.ImageSharp.Formats.Png |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
byte bits = (byte)options.BitDepth; |
|
|
|
if (Array.IndexOf(PngConstants.ColorTypes[options.ColorType.Value], bits) == -1) |
|
|
|
{ |
|
|
|
throw new NotSupportedException("Bit depth is not supported or not valid."); |
|
|
|
} |
|
|
|
|
|
|
|
// Use the metadata to determine what quantization depth to use if no quantizer has been set.
|
|
|
|
if (options.Quantizer is null) |
|
|
|
{ |
|
|
|
byte bits = (byte)options.BitDepth; |
|
|
|
var maxColors = ImageMaths.GetColorCountForBitDepth(bits); |
|
|
|
options.Quantizer = new WuQuantizer(new QuantizerOptions { MaxColors = maxColors }); |
|
|
|
} |
|
|
|
|