@@ -35,6 +35,15 @@ public static void AdjustOptions<TPixel>(
3535 options . ColorType ??= pngMetadata . ColorType ?? SuggestColorType < TPixel > ( ) ;
3636 options . BitDepth ??= pngMetadata . BitDepth ?? SuggestBitDepth < TPixel > ( ) ;
3737
38+ // Ensure bit depth and color type are a supported combination.
39+ // Bit8 is the only bit depth supported by all color types.
40+ byte bits = ( byte ) options . BitDepth ;
41+ byte [ ] validBitDepths = PngConstants . ColorTypes [ options . ColorType . Value ] ;
42+ if ( Array . IndexOf ( validBitDepths , bits ) == - 1 )
43+ {
44+ options . BitDepth = PngBitDepth . Bit8 ;
45+ }
46+
3847 options . InterlaceMethod ??= pngMetadata . InterlaceMethod ;
3948
4049 use16Bit = options . BitDepth == PngBitDepth . Bit16 ;
@@ -44,12 +53,6 @@ public static void AdjustOptions<TPixel>(
4453 {
4554 options . ChunkFilter = PngChunkFilter . ExcludeAll ;
4655 }
47-
48- // Ensure we are not allowing impossible combinations.
49- if ( ! PngConstants . ColorTypes . ContainsKey ( options . ColorType . Value ) )
50- {
51- throw new NotSupportedException ( "Color type is not supported or not valid." ) ;
52- }
5356 }
5457
5558 /// <summary>
@@ -68,15 +71,10 @@ public static IndexedImageFrame<TPixel> CreateQuantizedFrame<TPixel>(
6871 return null ;
6972 }
7073
71- byte bits = ( byte ) options . BitDepth ;
72- if ( Array . IndexOf ( PngConstants . ColorTypes [ options . ColorType . Value ] , bits ) == - 1 )
73- {
74- throw new NotSupportedException ( "Bit depth is not supported or not valid." ) ;
75- }
76-
7774 // Use the metadata to determine what quantization depth to use if no quantizer has been set.
7875 if ( options . Quantizer is null )
7976 {
77+ byte bits = ( byte ) options . BitDepth ;
8078 var maxColors = ImageMaths . GetColorCountForBitDepth ( bits ) ;
8179 options . Quantizer = new WuQuantizer ( new QuantizerOptions { MaxColors = maxColors } ) ;
8280 }
0 commit comments