@@ -563,9 +563,6 @@ impl<'a, W: 'a + Write + Seek, T: ColorType, K: TiffKind> ImageEncoder<'a, W, T,
563563 encoder. write_tag ( Tag :: Compression , compression. tag ( ) . to_u16 ( ) ) ?;
564564 encoder. write_tag ( Tag :: Predictor , predictor. to_u16 ( ) ) ?;
565565
566- encoder. write_tag ( Tag :: BitsPerSample , <T >:: BITS_PER_SAMPLE ) ?;
567- let sample_format: Vec < _ > = <T >:: SAMPLE_FORMAT . iter ( ) . map ( |s| s. to_u16 ( ) ) . collect ( ) ;
568- encoder. write_tag ( Tag :: SampleFormat , & sample_format[ ..] ) ?;
569566 encoder. write_tag ( Tag :: PhotometricInterpretation , <T >:: TIFF_VALUE . to_u16 ( ) ) ?;
570567
571568 encoder. write_tag ( Tag :: RowsPerStrip , u32:: try_from ( rows_per_strip) ?) ?;
@@ -754,6 +751,39 @@ impl<'a, W: 'a + Write + Seek, T: ColorType, K: TiffKind> ImageEncoder<'a, W, T,
754751 }
755752
756753 fn finish_internal ( & mut self ) -> TiffResult < DirectoryOffset < K > > {
754+ if self . extra_samples . is_empty ( ) {
755+ self . encoder
756+ . write_tag ( Tag :: BitsPerSample , <T >:: BITS_PER_SAMPLE ) ?;
757+ } else {
758+ let mut sample_format: Vec < _ > = <T >:: BITS_PER_SAMPLE . to_vec ( ) ;
759+ let replicated =
760+ core:: iter:: repeat ( <T >:: BITS_PER_SAMPLE [ 0 ] ) . take ( self . extra_samples . len ( ) ) ;
761+ sample_format. extend ( replicated) ;
762+
763+ self . encoder
764+ . write_tag ( Tag :: BitsPerSample , & sample_format[ ..] ) ?;
765+
766+ self . encoder . write_tag (
767+ Tag :: SamplesPerPixel ,
768+ u16:: try_from ( <T >:: BITS_PER_SAMPLE . len ( ) + self . extra_samples . len ( ) ) ?,
769+ ) ?;
770+ }
771+
772+ let mut sample_format: Vec < _ > = <T >:: SAMPLE_FORMAT . iter ( ) . map ( |s| s. to_u16 ( ) ) . collect ( ) ;
773+ let extra_format = sample_format
774+ . first ( )
775+ . copied ( )
776+ // Frankly should not occur, we have no sample format without at least one entry. We
777+ // would need an interface upgrade to handle this however. Either decide to support
778+ // heterogeneous sample formats or a way to provide fallback that is used for purely
779+ // extra samples and not provided via a slice used for samples themselves.
780+ . unwrap_or ( SampleFormat :: Void . to_u16 ( ) ) ;
781+
782+ sample_format. extend ( core:: iter:: repeat ( extra_format) . take ( self . extra_samples . len ( ) ) ) ;
783+
784+ self . encoder
785+ . write_tag ( Tag :: SampleFormat , & sample_format[ ..] ) ?;
786+
757787 self . encoder
758788 . write_tag ( Tag :: StripOffsets , K :: convert_slice ( & self . strip_offsets ) ) ?;
759789 self . encoder . write_tag (
0 commit comments