11//! Predictors for no predictor, horizontal and floating-point
2- use std:: collections:: HashMap ;
32use std:: fmt:: Debug ;
43
54use bytes:: { Bytes , BytesMut } ;
65// use tiff::decoder::DecodingResult;
76
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 } ;
458
469/// Trait for reverse predictors to implement
47- ///
48- ///
4910pub trait RevPredict : Debug + Send + Sync {
5011 /// reverse predict the decompressed bytes and fix endianness on the output
51- ///
52- ///
5312 fn rev_predict_fix_endianness (
5413 & self ,
5514 buffer : Bytes ,
@@ -83,9 +42,9 @@ impl RevPredict for NoPredictor {
8342
8443/// reverse horizontal predictor
8544#[ derive( Debug ) ]
86- pub struct RevHorizontalPredictor ;
45+ pub struct HorizontalPredictor ;
8746
88- impl RevPredict for RevHorizontalPredictor {
47+ impl RevPredict for HorizontalPredictor {
8948 fn rev_predict_fix_endianness (
9049 & self ,
9150 buffer : Bytes ,
@@ -179,9 +138,9 @@ pub fn fix_endianness(buf: &mut [u8], byte_order: Endianness, bit_depth: u16) {
179138
180139/// Floating point predictor
181140#[ derive( Debug ) ]
182- pub struct RevFloatingPointPredictor ;
141+ pub struct FloatingPointPredictor ;
183142
184- impl RevPredict for RevFloatingPointPredictor {
143+ impl RevPredict for FloatingPointPredictor {
185144 fn rev_predict_fix_endianness (
186145 & self ,
187146 buffer : Bytes ,
@@ -243,7 +202,7 @@ impl RevPredict for RevFloatingPointPredictor {
243202/// Reverse floating point prediction
244203///
245204/// floating point prediction first shuffles the bytes and then uses horizontal
246- /// differencing
205+ /// differencing
247206/// also performs byte-order conversion if needed.
248207///
249208pub 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) {
264223/// Reverse floating point prediction
265224///
266225/// floating point prediction first shuffles the bytes and then uses horizontal
267- /// differencing
226+ /// differencing
268227/// also performs byte-order conversion if needed.
269228///
270229pub 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) {
292251/// Reverse floating point prediction
293252///
294253/// floating point prediction first shuffles the bytes and then uses horizontal
295- /// differencing
254+ /// differencing
296255/// Also fixes byte order if needed (tiff's->native)
297256pub fn rev_predict_f64 ( input : & mut [ u8 ] , output : & mut [ u8 ] , samples : usize ) {
298257 for i in samples..input. len ( ) {
@@ -326,13 +285,13 @@ mod test {
326285 use bytes:: Bytes ;
327286
328287 use crate :: {
329- predictor:: RevFloatingPointPredictor ,
288+ predictor:: FloatingPointPredictor ,
330289 reader:: Endianness ,
331290 tiff:: tags:: { PlanarConfiguration , SampleFormat } ,
332291 tile:: PredictorInfo ,
333292 } ;
334293
335- use super :: { NoPredictor , RevHorizontalPredictor , RevPredict } ;
294+ use super :: { HorizontalPredictor , NoPredictor , RevPredict } ;
336295
337296 const PRED_INFO : PredictorInfo = PredictorInfo {
338297 endianness : Endianness :: LittleEndian ,
@@ -394,7 +353,7 @@ mod test {
394353 #[ rustfmt:: skip]
395354 #[ test]
396355 fn test_hpredict ( ) {
397- let p = RevHorizontalPredictor ;
356+ let p = HorizontalPredictor ;
398357 let mut predictor_info = PRED_INFO ;
399358 let cases = [
400359 ( 0 , 0 , vec ! [
@@ -541,7 +500,7 @@ mod test {
541500 // 0 1
542501 // 0 1
543502 // 0 1
544- let _shuffled = [ 0 , 2 , 4 , 6 , 1 , 3 , 5 , 7u8 ] ;
503+ let _shuffled = [ 0 , 2 , 4 , 6 , 1 , 3 , 5 , 7u8 ] ;
545504 let diffed = [ 0 , 2 , 2 , 2 , 251 , 2 , 2 , 2 ] ;
546505 let info = PredictorInfo {
547506 endianness : Endianness :: LittleEndian ,
@@ -556,7 +515,7 @@ mod test {
556515 } ;
557516 let input = Bytes :: from_owner ( diffed) ;
558517 assert_eq ! (
559- & RevFloatingPointPredictor . rev_predict_fix_endianness( input, & info, 1 , 1 ) . unwrap( ) [ ..] ,
518+ & FloatingPointPredictor . rev_predict_fix_endianness( input, & info, 1 , 1 ) . unwrap( ) [ ..] ,
560519 & expect_le[ ..]
561520 )
562521 }
@@ -586,7 +545,7 @@ mod test {
586545 } ;
587546 let input = Bytes :: from_owner ( diffed) ;
588547 assert_eq ! (
589- & RevFloatingPointPredictor . rev_predict_fix_endianness( input, & info, 1 , 1 ) . unwrap( ) [ ..] ,
548+ & FloatingPointPredictor . rev_predict_fix_endianness( input, & info, 1 , 1 ) . unwrap( ) [ ..] ,
590549 & expect_le[ ..]
591550 )
592551 }
@@ -615,13 +574,13 @@ mod test {
615574 } ;
616575 let input = Bytes :: from_owner ( diffed) ;
617576 assert_eq ! (
618- & RevFloatingPointPredictor
577+ & FloatingPointPredictor
619578 . rev_predict_fix_endianness( input. clone( ) , & info, 0 , 1 ) . unwrap( ) [ ..] ,
620579 & expect_le
621580 ) ;
622581 info. endianness = Endianness :: BigEndian ;
623582 assert_eq ! (
624- & RevFloatingPointPredictor . rev_predict_fix_endianness( input, & info, 0 , 1 ) . unwrap( ) [ ..] ,
583+ & FloatingPointPredictor . rev_predict_fix_endianness( input, & info, 0 , 1 ) . unwrap( ) [ ..] ,
625584 & expect_le
626585 )
627586 }
@@ -650,7 +609,7 @@ mod test {
650609 } ;
651610 let input = Bytes :: from_owner ( diffed) ;
652611 assert_eq ! (
653- & RevFloatingPointPredictor
612+ & FloatingPointPredictor
654613 . rev_predict_fix_endianness( input, & info, 0 , 1 )
655614 . unwrap( ) [ ..] ,
656615 & expect_be[ ..]
0 commit comments