1
+ //! Decoders for different TIFF compression methods.
2
+
1
3
use std:: collections:: HashMap ;
2
4
use std:: fmt:: Debug ;
3
5
use std:: io:: { Cursor , Read } ;
4
6
5
7
use bytes:: Bytes ;
6
8
use flate2:: bufread:: ZlibDecoder ;
7
9
8
- use crate :: error:: Result ;
10
+ use crate :: error:: AsyncTiffResult ;
9
11
use crate :: tiff:: tags:: { CompressionMethod , PhotometricInterpretation } ;
10
12
use crate :: tiff:: { TiffError , TiffUnsupportedError } ;
11
13
@@ -46,14 +48,16 @@ impl Default for DecoderRegistry {
46
48
47
49
/// A trait to decode a TIFF tile.
48
50
pub trait Decoder : Debug + Send + Sync {
51
+ /// Decode a TIFF tile.
49
52
fn decode_tile (
50
53
& self ,
51
54
buffer : Bytes ,
52
55
photometric_interpretation : PhotometricInterpretation ,
53
56
jpeg_tables : Option < & [ u8 ] > ,
54
- ) -> Result < Bytes > ;
57
+ ) -> AsyncTiffResult < Bytes > ;
55
58
}
56
59
60
+ /// A decoder for the Deflate compression method.
57
61
#[ derive( Debug , Clone ) ]
58
62
pub struct DeflateDecoder ;
59
63
@@ -63,14 +67,15 @@ impl Decoder for DeflateDecoder {
63
67
buffer : Bytes ,
64
68
_photometric_interpretation : PhotometricInterpretation ,
65
69
_jpeg_tables : Option < & [ u8 ] > ,
66
- ) -> Result < Bytes > {
70
+ ) -> AsyncTiffResult < Bytes > {
67
71
let mut decoder = ZlibDecoder :: new ( Cursor :: new ( buffer) ) ;
68
72
let mut buf = Vec :: new ( ) ;
69
73
decoder. read_to_end ( & mut buf) ?;
70
74
Ok ( buf. into ( ) )
71
75
}
72
76
}
73
77
78
+ /// A decoder for the JPEG compression method.
74
79
#[ derive( Debug , Clone ) ]
75
80
pub struct JPEGDecoder ;
76
81
@@ -80,11 +85,12 @@ impl Decoder for JPEGDecoder {
80
85
buffer : Bytes ,
81
86
photometric_interpretation : PhotometricInterpretation ,
82
87
jpeg_tables : Option < & [ u8 ] > ,
83
- ) -> Result < Bytes > {
88
+ ) -> AsyncTiffResult < Bytes > {
84
89
decode_modern_jpeg ( buffer, photometric_interpretation, jpeg_tables)
85
90
}
86
91
}
87
92
93
+ /// A decoder for the LZW compression method.
88
94
#[ derive( Debug , Clone ) ]
89
95
pub struct LZWDecoder ;
90
96
@@ -94,14 +100,15 @@ impl Decoder for LZWDecoder {
94
100
buffer : Bytes ,
95
101
_photometric_interpretation : PhotometricInterpretation ,
96
102
_jpeg_tables : Option < & [ u8 ] > ,
97
- ) -> Result < Bytes > {
103
+ ) -> AsyncTiffResult < Bytes > {
98
104
// https://github.com/image-rs/image-tiff/blob/90ae5b8e54356a35e266fb24e969aafbcb26e990/src/decoder/stream.rs#L147
99
105
let mut decoder = weezl:: decode:: Decoder :: with_tiff_size_switch ( weezl:: BitOrder :: Msb , 8 ) ;
100
106
let decoded = decoder. decode ( & buffer) . expect ( "failed to decode LZW data" ) ;
101
107
Ok ( decoded. into ( ) )
102
108
}
103
109
}
104
110
111
+ /// A decoder for uncompressed data.
105
112
#[ derive( Debug , Clone ) ]
106
113
pub struct UncompressedDecoder ;
107
114
@@ -111,7 +118,7 @@ impl Decoder for UncompressedDecoder {
111
118
buffer : Bytes ,
112
119
_photometric_interpretation : PhotometricInterpretation ,
113
120
_jpeg_tables : Option < & [ u8 ] > ,
114
- ) -> Result < Bytes > {
121
+ ) -> AsyncTiffResult < Bytes > {
115
122
Ok ( buffer)
116
123
}
117
124
}
@@ -121,7 +128,7 @@ fn decode_modern_jpeg(
121
128
buf : Bytes ,
122
129
photometric_interpretation : PhotometricInterpretation ,
123
130
jpeg_tables : Option < & [ u8 ] > ,
124
- ) -> Result < Bytes > {
131
+ ) -> AsyncTiffResult < Bytes > {
125
132
// Construct new jpeg_reader wrapping a SmartReader.
126
133
//
127
134
// JPEG compression in TIFF allows saving quantization and/or huffman tables in one central
0 commit comments