Skip to content

Commit a3291b0

Browse files
committed
Remove registry
1 parent 6c9a8cf commit a3291b0

File tree

3 files changed

+45
-86
lines changed

3 files changed

+45
-86
lines changed

src/cog.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ mod test {
2323
use std::io::BufReader;
2424
use std::sync::Arc;
2525

26-
use crate::decoder::DecoderRegistry;
2726
use crate::metadata::{PrefetchBuffer, TiffMetadataReader};
28-
use crate::predictor::RevPredictorRegistry;
2927
use crate::reader::{AsyncFileReader, ObjectReader};
3028

3129
use super::*;
@@ -52,10 +50,8 @@ mod test {
5250
let tiff = TIFF::new(ifds);
5351

5452
let ifd = &tiff.ifds[1];
55-
let decoder_registry = DecoderRegistry::default();
56-
let predictor_registry = RevPredictorRegistry::default();
5753
let tile = ifd.fetch_tile(0, 0, reader.as_ref()).await.unwrap();
58-
let tile = tile.decode(&decoder_registry, &predictor_registry).unwrap();
54+
let tile = tile.decode(&Default::default()).unwrap();
5955
std::fs::write("img.buf", tile).unwrap();
6056
}
6157

src/predictor.rs

Lines changed: 17 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,14 @@
11
//! Predictors for no predictor, horizontal and floating-point
2-
use std::collections::HashMap;
32
use std::fmt::Debug;
43

54
use 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-
///
4910
pub 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
///
249208
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) {
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
///
270229
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) {
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)
297256
pub 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[..]

src/tile.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bytes::Bytes;
22

33
use crate::decoder::DecoderRegistry;
44
use crate::error::AsyncTiffResult;
5-
use crate::predictor::RevPredictorRegistry;
5+
use crate::predictor::{FloatingPointPredictor, HorizontalPredictor, NoPredictor, RevPredict};
66
use crate::reader::Endianness;
77
use crate::tiff::tags::{
88
CompressionMethod, PhotometricInterpretation, PlanarConfiguration, Predictor, SampleFormat,
@@ -221,36 +221,40 @@ impl Tile<'_> {
221221
///
222222
/// Decoding is separate from fetching so that sync and async operations do not block the same
223223
/// runtime.
224-
pub fn decode(
225-
self,
226-
decoder_registry: &DecoderRegistry,
227-
predictor_registry: &RevPredictorRegistry,
228-
) -> AsyncTiffResult<Bytes> {
224+
pub fn decode(self, decoder_registry: &DecoderRegistry) -> AsyncTiffResult<Bytes> {
229225
let decoder = decoder_registry
230226
.as_ref()
231227
.get(&self.compression_method)
232228
.ok_or(TiffError::UnsupportedError(
233229
TiffUnsupportedError::UnsupportedCompressionMethod(self.compression_method),
234230
))?;
235231

236-
let predictor =
237-
predictor_registry
238-
.as_ref()
239-
.get(&self.predictor)
240-
.ok_or(TiffError::UnsupportedError(
241-
TiffUnsupportedError::UnsupportedPredictor(self.predictor),
242-
))?;
232+
let decoded_tile = decoder.decode_tile(
233+
self.compressed_bytes.clone(),
234+
self.photometric_interpretation,
235+
self.jpeg_tables.as_deref(),
236+
)?;
243237

244-
predictor.rev_predict_fix_endianness(
245-
decoder.decode_tile(
246-
self.compressed_bytes.clone(),
247-
self.photometric_interpretation,
248-
self.jpeg_tables.as_deref(),
249-
)?,
250-
&self.predictor_info,
251-
self.x as _,
252-
self.y as _,
253-
)
238+
match self.predictor {
239+
Predictor::None => NoPredictor.rev_predict_fix_endianness(
240+
decoded_tile,
241+
&self.predictor_info,
242+
self.x as _,
243+
self.y as _,
244+
),
245+
Predictor::Horizontal => HorizontalPredictor.rev_predict_fix_endianness(
246+
decoded_tile,
247+
&self.predictor_info,
248+
self.x as _,
249+
self.y as _,
250+
),
251+
Predictor::FloatingPoint => FloatingPointPredictor.rev_predict_fix_endianness(
252+
decoded_tile,
253+
&self.predictor_info,
254+
self.x as _,
255+
self.y as _,
256+
),
257+
}
254258
}
255259
}
256260

0 commit comments

Comments
 (0)