1
1
//! Predictors for no predictor, horizontal and floating-point
2
- use std:: collections:: HashMap ;
3
2
use std:: fmt:: Debug ;
4
3
5
4
use bytes:: { Bytes , BytesMut } ;
6
5
// use tiff::decoder::DecodingResult;
7
6
8
- use crate :: {
9
- error:: AsyncTiffResult , reader:: Endianness , tiff:: tags:: Predictor , tile:: PredictorInfo ,
10
- } ;
11
-
12
- /// A registry for reverse predictors
13
- ///
14
- /// Reverse predictors, because they perform the inverse (decoding) operation of prediction
15
- ///
16
- ///
17
- ///
18
- pub struct RevPredictorRegistry ( HashMap < Predictor , Box < dyn RevPredict > > ) ;
19
-
20
- impl RevPredictorRegistry {
21
- /// create a new predictor registry with no predictors registered
22
- pub fn new ( ) -> Self {
23
- Self ( HashMap :: new ( ) )
24
- }
25
- }
26
-
27
- impl AsRef < HashMap < Predictor , Box < dyn RevPredict > > > for RevPredictorRegistry {
28
- fn as_ref ( & self ) -> & HashMap < Predictor , Box < dyn RevPredict > > {
29
- & self . 0
30
- }
31
- }
32
-
33
- impl Default for RevPredictorRegistry {
34
- fn default ( ) -> Self {
35
- let mut hmap = HashMap :: new ( ) ;
36
- hmap. insert ( Predictor :: None , Box :: new ( NoPredictor ) as _ ) ;
37
- hmap. insert ( Predictor :: Horizontal , Box :: new ( RevHorizontalPredictor ) as _ ) ;
38
- hmap. insert (
39
- Predictor :: FloatingPoint ,
40
- Box :: new ( RevFloatingPointPredictor ) as _ ,
41
- ) ;
42
- Self ( hmap)
43
- }
44
- }
7
+ use crate :: { error:: AsyncTiffResult , reader:: Endianness , tile:: PredictorInfo } ;
45
8
46
9
/// Trait for reverse predictors to implement
47
- ///
48
- ///
49
10
pub trait RevPredict : Debug + Send + Sync {
50
11
/// reverse predict the decompressed bytes and fix endianness on the output
51
- ///
52
- ///
53
12
fn rev_predict_fix_endianness (
54
13
& self ,
55
14
buffer : Bytes ,
@@ -83,9 +42,9 @@ impl RevPredict for NoPredictor {
83
42
84
43
/// reverse horizontal predictor
85
44
#[ derive( Debug ) ]
86
- pub struct RevHorizontalPredictor ;
45
+ pub struct HorizontalPredictor ;
87
46
88
- impl RevPredict for RevHorizontalPredictor {
47
+ impl RevPredict for HorizontalPredictor {
89
48
fn rev_predict_fix_endianness (
90
49
& self ,
91
50
buffer : Bytes ,
@@ -179,9 +138,9 @@ pub fn fix_endianness(buf: &mut [u8], byte_order: Endianness, bit_depth: u16) {
179
138
180
139
/// Floating point predictor
181
140
#[ derive( Debug ) ]
182
- pub struct RevFloatingPointPredictor ;
141
+ pub struct FloatingPointPredictor ;
183
142
184
- impl RevPredict for RevFloatingPointPredictor {
143
+ impl RevPredict for FloatingPointPredictor {
185
144
fn rev_predict_fix_endianness (
186
145
& self ,
187
146
buffer : Bytes ,
@@ -243,7 +202,7 @@ impl RevPredict for RevFloatingPointPredictor {
243
202
/// Reverse floating point prediction
244
203
///
245
204
/// floating point prediction first shuffles the bytes and then uses horizontal
246
- /// differencing
205
+ /// differencing
247
206
/// also performs byte-order conversion if needed.
248
207
///
249
208
pub fn rev_predict_f16 ( input : & mut [ u8 ] , output : & mut [ u8 ] , samples : usize ) {
@@ -264,7 +223,7 @@ pub fn rev_predict_f16(input: &mut [u8], output: &mut [u8], samples: usize) {
264
223
/// Reverse floating point prediction
265
224
///
266
225
/// floating point prediction first shuffles the bytes and then uses horizontal
267
- /// differencing
226
+ /// differencing
268
227
/// also performs byte-order conversion if needed.
269
228
///
270
229
pub fn rev_predict_f32 ( input : & mut [ u8 ] , output : & mut [ u8 ] , samples : usize ) {
@@ -292,7 +251,7 @@ pub fn rev_predict_f32(input: &mut [u8], output: &mut [u8], samples: usize) {
292
251
/// Reverse floating point prediction
293
252
///
294
253
/// floating point prediction first shuffles the bytes and then uses horizontal
295
- /// differencing
254
+ /// differencing
296
255
/// Also fixes byte order if needed (tiff's->native)
297
256
pub fn rev_predict_f64 ( input : & mut [ u8 ] , output : & mut [ u8 ] , samples : usize ) {
298
257
for i in samples..input. len ( ) {
@@ -326,13 +285,13 @@ mod test {
326
285
use bytes:: Bytes ;
327
286
328
287
use crate :: {
329
- predictor:: RevFloatingPointPredictor ,
288
+ predictor:: FloatingPointPredictor ,
330
289
reader:: Endianness ,
331
290
tiff:: tags:: { PlanarConfiguration , SampleFormat } ,
332
291
tile:: PredictorInfo ,
333
292
} ;
334
293
335
- use super :: { NoPredictor , RevHorizontalPredictor , RevPredict } ;
294
+ use super :: { HorizontalPredictor , NoPredictor , RevPredict } ;
336
295
337
296
const PRED_INFO : PredictorInfo = PredictorInfo {
338
297
endianness : Endianness :: LittleEndian ,
@@ -394,7 +353,7 @@ mod test {
394
353
#[ rustfmt:: skip]
395
354
#[ test]
396
355
fn test_hpredict ( ) {
397
- let p = RevHorizontalPredictor ;
356
+ let p = HorizontalPredictor ;
398
357
let mut predictor_info = PRED_INFO ;
399
358
let cases = [
400
359
( 0 , 0 , vec ! [
@@ -541,7 +500,7 @@ mod test {
541
500
// 0 1
542
501
// 0 1
543
502
// 0 1
544
- let _shuffled = [ 0 , 2 , 4 , 6 , 1 , 3 , 5 , 7u8 ] ;
503
+ let _shuffled = [ 0 , 2 , 4 , 6 , 1 , 3 , 5 , 7u8 ] ;
545
504
let diffed = [ 0 , 2 , 2 , 2 , 251 , 2 , 2 , 2 ] ;
546
505
let info = PredictorInfo {
547
506
endianness : Endianness :: LittleEndian ,
@@ -556,7 +515,7 @@ mod test {
556
515
} ;
557
516
let input = Bytes :: from_owner ( diffed) ;
558
517
assert_eq ! (
559
- & RevFloatingPointPredictor . rev_predict_fix_endianness( input, & info, 1 , 1 ) . unwrap( ) [ ..] ,
518
+ & FloatingPointPredictor . rev_predict_fix_endianness( input, & info, 1 , 1 ) . unwrap( ) [ ..] ,
560
519
& expect_le[ ..]
561
520
)
562
521
}
@@ -586,7 +545,7 @@ mod test {
586
545
} ;
587
546
let input = Bytes :: from_owner ( diffed) ;
588
547
assert_eq ! (
589
- & RevFloatingPointPredictor . rev_predict_fix_endianness( input, & info, 1 , 1 ) . unwrap( ) [ ..] ,
548
+ & FloatingPointPredictor . rev_predict_fix_endianness( input, & info, 1 , 1 ) . unwrap( ) [ ..] ,
590
549
& expect_le[ ..]
591
550
)
592
551
}
@@ -615,13 +574,13 @@ mod test {
615
574
} ;
616
575
let input = Bytes :: from_owner ( diffed) ;
617
576
assert_eq ! (
618
- & RevFloatingPointPredictor
577
+ & FloatingPointPredictor
619
578
. rev_predict_fix_endianness( input. clone( ) , & info, 0 , 1 ) . unwrap( ) [ ..] ,
620
579
& expect_le
621
580
) ;
622
581
info. endianness = Endianness :: BigEndian ;
623
582
assert_eq ! (
624
- & RevFloatingPointPredictor . rev_predict_fix_endianness( input, & info, 0 , 1 ) . unwrap( ) [ ..] ,
583
+ & FloatingPointPredictor . rev_predict_fix_endianness( input, & info, 0 , 1 ) . unwrap( ) [ ..] ,
625
584
& expect_le
626
585
)
627
586
}
@@ -650,7 +609,7 @@ mod test {
650
609
} ;
651
610
let input = Bytes :: from_owner ( diffed) ;
652
611
assert_eq ! (
653
- & RevFloatingPointPredictor
612
+ & FloatingPointPredictor
654
613
. rev_predict_fix_endianness( input, & info, 0 , 1 )
655
614
. unwrap( ) [ ..] ,
656
615
& expect_be[ ..]
0 commit comments