Skip to content

Commit a9f368a

Browse files
committed
Added fallback colour transform and ability to set colour transform
1 parent 9e4ffd5 commit a9f368a

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

src/decoder.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ pub struct Decoder<R> {
110110
quantization_tables: [Option<Arc<[u16; 64]>>; 4],
111111

112112
restart_interval: u16,
113-
color_transform: ColorTransform,
113+
fallback_color_transform: ColorTransform,
114+
color_transform: Option<ColorTransform>,
114115
is_jfif: bool,
115116
is_mjpeg: bool,
116117

@@ -137,8 +138,9 @@ impl<R: Read> Decoder<R> {
137138
ac_huffman_tables: vec![None, None, None, None],
138139
quantization_tables: [None, None, None, None],
139140
restart_interval: 0,
140-
// Set the default transform as unknown. This can be overriden using `set_color_transform`
141-
color_transform: ColorTransform::default(),
141+
// Set the fallback transform as unknown. This can be overriden using `set_color_transform`
142+
fallback_color_transform: ColorTransform::default(),
143+
color_transform: None,
142144
is_jfif: false,
143145
is_mjpeg: false,
144146
icc_markers: Vec::new(),
@@ -149,9 +151,15 @@ impl<R: Read> Decoder<R> {
149151
}
150152
}
151153

152-
/// Sets the colour transform to be applied before returning the data.
154+
/// Colour transform to use if one is not set using `set_color_transform` or found in app segments.
155+
pub fn set_fallback_color_transform(&mut self, transform: ColorTransform) {
156+
self.fallback_color_transform = transform;
157+
}
158+
159+
/// Colour transform to use when decoding the image. App segments relating to colour transforms
160+
/// will be ignored.
153161
pub fn set_color_transform(&mut self, transform: ColorTransform) {
154-
self.color_transform = transform;
162+
self.color_transform = Some(transform);
155163
}
156164

157165
/// Set maximum buffer size allowed for decoded images
@@ -521,8 +529,12 @@ impl<R: Read> Decoder<R> {
521529
if let Some(data) = parse_app(&mut self.reader, marker)? {
522530
match data {
523531
AppData::Adobe(color_transform) => {
524-
self.color_transform =
525-
ColorTransform::AdobeColorTransform(color_transform)
532+
// Set the colour transform if it has not already been set by the user
533+
// calling `set_color_transform`
534+
if self.color_transform.is_none() {
535+
self.color_transform =
536+
Some(ColorTransform::AdobeColorTransform(color_transform))
537+
}
526538
}
527539
AppData::Jfif => {
528540
// From the JFIF spec:
@@ -674,12 +686,18 @@ impl<R: Read> Decoder<R> {
674686
if frame.coding_process == CodingProcess::Lossless {
675687
compute_image_lossless(frame, planes_u16)
676688
} else {
689+
// Check whether a colour transform has been set - if not use the fallback
690+
let color_transform = match self.color_transform {
691+
Some(color_transform) => color_transform,
692+
None => self.fallback_color_transform,
693+
};
694+
677695
compute_image(
678696
&frame.components,
679697
planes,
680698
frame.output_size,
681699
self.is_jfif,
682-
self.color_transform,
700+
color_transform,
683701
)
684702
}
685703
}

0 commit comments

Comments
 (0)