1+ //! Decoders for different TIFF compression methods.
2+
13use std:: collections:: HashMap ;
24use std:: fmt:: Debug ;
35use std:: io:: { Cursor , Read } ;
46
57use bytes:: Bytes ;
68use flate2:: bufread:: ZlibDecoder ;
79
8- use crate :: error:: Result ;
10+ use crate :: error:: AsyncTiffResult ;
911use crate :: tiff:: tags:: { CompressionMethod , PhotometricInterpretation } ;
1012use crate :: tiff:: { TiffError , TiffUnsupportedError } ;
1113
@@ -46,14 +48,16 @@ impl Default for DecoderRegistry {
4648
4749/// A trait to decode a TIFF tile.
4850pub trait Decoder : Debug + Send + Sync {
51+ /// Decode a TIFF tile.
4952 fn decode_tile (
5053 & self ,
5154 buffer : Bytes ,
5255 photometric_interpretation : PhotometricInterpretation ,
5356 jpeg_tables : Option < & [ u8 ] > ,
54- ) -> Result < Bytes > ;
57+ ) -> AsyncTiffResult < Bytes > ;
5558}
5659
60+ /// A decoder for the Deflate compression method.
5761#[ derive( Debug , Clone ) ]
5862pub struct DeflateDecoder ;
5963
@@ -63,14 +67,15 @@ impl Decoder for DeflateDecoder {
6367 buffer : Bytes ,
6468 _photometric_interpretation : PhotometricInterpretation ,
6569 _jpeg_tables : Option < & [ u8 ] > ,
66- ) -> Result < Bytes > {
70+ ) -> AsyncTiffResult < Bytes > {
6771 let mut decoder = ZlibDecoder :: new ( Cursor :: new ( buffer) ) ;
6872 let mut buf = Vec :: new ( ) ;
6973 decoder. read_to_end ( & mut buf) ?;
7074 Ok ( buf. into ( ) )
7175 }
7276}
7377
78+ /// A decoder for the JPEG compression method.
7479#[ derive( Debug , Clone ) ]
7580pub struct JPEGDecoder ;
7681
@@ -80,11 +85,12 @@ impl Decoder for JPEGDecoder {
8085 buffer : Bytes ,
8186 photometric_interpretation : PhotometricInterpretation ,
8287 jpeg_tables : Option < & [ u8 ] > ,
83- ) -> Result < Bytes > {
88+ ) -> AsyncTiffResult < Bytes > {
8489 decode_modern_jpeg ( buffer, photometric_interpretation, jpeg_tables)
8590 }
8691}
8792
93+ /// A decoder for the LZW compression method.
8894#[ derive( Debug , Clone ) ]
8995pub struct LZWDecoder ;
9096
@@ -94,14 +100,15 @@ impl Decoder for LZWDecoder {
94100 buffer : Bytes ,
95101 _photometric_interpretation : PhotometricInterpretation ,
96102 _jpeg_tables : Option < & [ u8 ] > ,
97- ) -> Result < Bytes > {
103+ ) -> AsyncTiffResult < Bytes > {
98104 // https://github.com/image-rs/image-tiff/blob/90ae5b8e54356a35e266fb24e969aafbcb26e990/src/decoder/stream.rs#L147
99105 let mut decoder = weezl:: decode:: Decoder :: with_tiff_size_switch ( weezl:: BitOrder :: Msb , 8 ) ;
100106 let decoded = decoder. decode ( & buffer) . expect ( "failed to decode LZW data" ) ;
101107 Ok ( decoded. into ( ) )
102108 }
103109}
104110
111+ /// A decoder for uncompressed data.
105112#[ derive( Debug , Clone ) ]
106113pub struct UncompressedDecoder ;
107114
@@ -111,7 +118,7 @@ impl Decoder for UncompressedDecoder {
111118 buffer : Bytes ,
112119 _photometric_interpretation : PhotometricInterpretation ,
113120 _jpeg_tables : Option < & [ u8 ] > ,
114- ) -> Result < Bytes > {
121+ ) -> AsyncTiffResult < Bytes > {
115122 Ok ( buffer)
116123 }
117124}
@@ -121,7 +128,7 @@ fn decode_modern_jpeg(
121128 buf : Bytes ,
122129 photometric_interpretation : PhotometricInterpretation ,
123130 jpeg_tables : Option < & [ u8 ] > ,
124- ) -> Result < Bytes > {
131+ ) -> AsyncTiffResult < Bytes > {
125132 // Construct new jpeg_reader wrapping a SmartReader.
126133 //
127134 // JPEG compression in TIFF allows saving quantization and/or huffman tables in one central
0 commit comments