@@ -42,7 +42,16 @@ public static bool VerifyAndParse(this TiffDecoderCore options, ExifProfile exif
4242 }
4343 }
4444
45- TiffFillOrder fillOrder = ( TiffFillOrder ? ) exifProfile . GetValue ( ExifTag . FillOrder ) ? . Value ?? TiffFillOrder . MostSignificantBitFirst ;
45+ TiffFillOrder fillOrder ;
46+ if ( exifProfile . TryGetValue ( ExifTag . FillOrder , out IExifValue < ushort > value ) )
47+ {
48+ fillOrder = ( TiffFillOrder ) value . Value ;
49+ }
50+ else
51+ {
52+ fillOrder = TiffFillOrder . MostSignificantBitFirst ;
53+ }
54+
4655 if ( fillOrder == TiffFillOrder . LeastSignificantBitFirst && frameMetadata . BitsPerPixel != TiffBitsPerPixel . Bit1 )
4756 {
4857 TiffThrowHelper . ThrowNotSupported ( "The lower-order bits of the byte FillOrder is only supported in combination with 1bit per pixel bicolor tiff's." ) ;
@@ -53,10 +62,10 @@ public static bool VerifyAndParse(this TiffDecoderCore options, ExifProfile exif
5362 TiffThrowHelper . ThrowNotSupported ( "TIFF images with FloatingPoint horizontal predictor are not supported." ) ;
5463 }
5564
56- TiffSampleFormat [ ] sampleFormats = exifProfile . GetValue ( ExifTag . SampleFormat ) ? . Value ? . Select ( a => ( TiffSampleFormat ) a ) . ToArray ( ) ;
5765 TiffSampleFormat ? sampleFormat = null ;
58- if ( sampleFormats != null )
66+ if ( exifProfile . TryGetValue ( ExifTag . SampleFormat , out var formatValue ) )
5967 {
68+ TiffSampleFormat [ ] sampleFormats = formatValue . Value . Select ( a => ( TiffSampleFormat ) a ) . ToArray ( ) ;
6069 sampleFormat = sampleFormats [ 0 ] ;
6170 foreach ( TiffSampleFormat format in sampleFormats )
6271 {
@@ -67,7 +76,12 @@ public static bool VerifyAndParse(this TiffDecoderCore options, ExifProfile exif
6776 }
6877 }
6978
70- ushort [ ] ycbcrSubSampling = exifProfile . GetValue ( ExifTag . YCbCrSubsampling ) ? . Value ;
79+ ushort [ ] ycbcrSubSampling = null ;
80+ if ( exifProfile . TryGetValue ( ExifTag . YCbCrSubsampling , out IExifValue < ushort [ ] > subSamplingValue ) )
81+ {
82+ ycbcrSubSampling = subSamplingValue . Value ;
83+ }
84+
7185 if ( ycbcrSubSampling != null && ycbcrSubSampling . Length != 2 )
7286 {
7387 TiffThrowHelper . ThrowImageFormatException ( "Invalid YCbCrSubsampling, expected 2 values." ) ;
@@ -78,23 +92,52 @@ public static bool VerifyAndParse(this TiffDecoderCore options, ExifProfile exif
7892 TiffThrowHelper . ThrowImageFormatException ( "ChromaSubsampleVert shall always be less than or equal to ChromaSubsampleHoriz." ) ;
7993 }
8094
81- if ( exifProfile . GetValue ( ExifTag . StripRowCounts ) ? . Value != null )
95+ if ( exifProfile . TryGetValue ( ExifTag . StripRowCounts , out _ ) )
8296 {
8397 TiffThrowHelper . ThrowNotSupported ( "Variable-sized strips are not supported." ) ;
8498 }
8599
86- options . PlanarConfiguration = ( TiffPlanarConfiguration ? ) exifProfile . GetValue ( ExifTag . PlanarConfiguration ) ? . Value ?? DefaultPlanarConfiguration ;
100+ if ( exifProfile . TryGetValue ( ExifTag . PlanarConfiguration , out IExifValue < ushort > planarValue ) )
101+ {
102+ options . PlanarConfiguration = ( TiffPlanarConfiguration ) planarValue . Value ;
103+ }
104+ else
105+ {
106+ options . PlanarConfiguration = DefaultPlanarConfiguration ;
107+ }
108+
87109 options . Predictor = frameMetadata . Predictor ?? TiffPredictor . None ;
88110 options . PhotometricInterpretation = frameMetadata . PhotometricInterpretation ?? TiffPhotometricInterpretation . Rgb ;
89111 options . SampleFormat = sampleFormat ?? TiffSampleFormat . UnsignedInteger ;
90112 options . BitsPerPixel = frameMetadata . BitsPerPixel != null ? ( int ) frameMetadata . BitsPerPixel . Value : ( int ) TiffBitsPerPixel . Bit24 ;
91113 options . BitsPerSample = frameMetadata . BitsPerSample ?? new TiffBitsPerSample ( 0 , 0 , 0 ) ;
92- options . ReferenceBlackAndWhite = exifProfile . GetValue ( ExifTag . ReferenceBlackWhite ) ? . Value ;
93- options . YcbcrCoefficients = exifProfile . GetValue ( ExifTag . YCbCrCoefficients ) ? . Value ;
94- options . YcbcrSubSampling = exifProfile . GetValue ( ExifTag . YCbCrSubsampling ) ? . Value ;
114+
115+ if ( exifProfile . TryGetValue ( ExifTag . ReferenceBlackWhite , out IExifValue < Rational [ ] > blackWhiteValue ) )
116+ {
117+ options . ReferenceBlackAndWhite = blackWhiteValue . Value ;
118+ }
119+
120+ if ( exifProfile . TryGetValue ( ExifTag . YCbCrCoefficients , out IExifValue < Rational [ ] > coefficientsValue ) )
121+ {
122+ options . YcbcrCoefficients = coefficientsValue . Value ;
123+ }
124+
125+ if ( exifProfile . TryGetValue ( ExifTag . YCbCrSubsampling , out IExifValue < ushort [ ] > ycbrSubSamplingValue ) )
126+ {
127+ options . YcbcrSubSampling = ycbrSubSamplingValue . Value ;
128+ }
129+
95130 options . FillOrder = fillOrder ;
96- options . JpegTables = exifProfile . GetValue ( ExifTag . JPEGTables ) ? . Value ;
97- options . OldJpegCompressionStartOfImageMarker = exifProfile . GetValue ( ExifTag . JPEGInterchangeFormat ) ? . Value ;
131+
132+ if ( exifProfile . TryGetValue ( ExifTag . JPEGTables , out IExifValue < byte [ ] > jpegTablesValue ) )
133+ {
134+ options . JpegTables = jpegTablesValue . Value ;
135+ }
136+
137+ if ( exifProfile . TryGetValue ( ExifTag . JPEGInterchangeFormat , out IExifValue < uint > jpegInterchangeFormatValue ) )
138+ {
139+ options . OldJpegCompressionStartOfImageMarker = jpegInterchangeFormatValue . Value ;
140+ }
98141
99142 options . ParseColorType ( exifProfile ) ;
100143 options . ParseCompression ( frameMetadata . Compression , exifProfile ) ;
@@ -394,9 +437,9 @@ private static void ParseColorType(this TiffDecoderCore options, ExifProfile exi
394437
395438 case TiffPhotometricInterpretation . PaletteColor :
396439 {
397- options . ColorMap = exifProfile . GetValue ( ExifTag . ColorMap ) ? . Value ;
398- if ( options . ColorMap != null )
440+ if ( exifProfile . TryGetValue ( ExifTag . ColorMap , out IExifValue < ushort [ ] > value ) )
399441 {
442+ options . ColorMap = value . Value ;
400443 if ( options . BitsPerSample . Channels != 1 )
401444 {
402445 TiffThrowHelper . ThrowNotSupported ( "The number of samples in the TIFF BitsPerSample entry is not supported." ) ;
@@ -414,7 +457,11 @@ private static void ParseColorType(this TiffDecoderCore options, ExifProfile exi
414457
415458 case TiffPhotometricInterpretation . YCbCr :
416459 {
417- options . ColorMap = exifProfile . GetValue ( ExifTag . ColorMap ) ? . Value ;
460+ if ( exifProfile . TryGetValue ( ExifTag . ColorMap , out IExifValue < ushort [ ] > value ) )
461+ {
462+ options . ColorMap = value . Value ;
463+ }
464+
418465 if ( options . BitsPerSample . Channels != 3 )
419466 {
420467 TiffThrowHelper . ThrowNotSupported ( "The number of samples in the TIFF BitsPerSample entry is not supported for YCbCr images." ) ;
@@ -508,15 +555,30 @@ private static void ParseCompression(this TiffDecoderCore options, TiffCompressi
508555 case TiffCompression . CcittGroup3Fax :
509556 {
510557 options . CompressionType = TiffDecoderCompressionType . T4 ;
511- options . FaxCompressionOptions = exifProfile . GetValue ( ExifTag . T4Options ) != null ? ( FaxCompressionOptions ) exifProfile . GetValue ( ExifTag . T4Options ) . Value : FaxCompressionOptions . None ;
558+
559+ if ( exifProfile . TryGetValue ( ExifTag . T4Options , out IExifValue < uint > t4OptionsValue ) )
560+ {
561+ options . FaxCompressionOptions = ( FaxCompressionOptions ) t4OptionsValue . Value ;
562+ }
563+ else
564+ {
565+ options . FaxCompressionOptions = FaxCompressionOptions . None ;
566+ }
512567
513568 break ;
514569 }
515570
516571 case TiffCompression . CcittGroup4Fax :
517572 {
518573 options . CompressionType = TiffDecoderCompressionType . T6 ;
519- options . FaxCompressionOptions = exifProfile . GetValue ( ExifTag . T4Options ) != null ? ( FaxCompressionOptions ) exifProfile . GetValue ( ExifTag . T4Options ) . Value : FaxCompressionOptions . None ;
574+ if ( exifProfile . TryGetValue ( ExifTag . T4Options , out IExifValue < uint > t4OptionsValue ) )
575+ {
576+ options . FaxCompressionOptions = ( FaxCompressionOptions ) t4OptionsValue . Value ;
577+ }
578+ else
579+ {
580+ options . FaxCompressionOptions = FaxCompressionOptions . None ;
581+ }
520582
521583 break ;
522584 }
0 commit comments