@@ -110,7 +110,8 @@ pub struct Decoder<R> {
110
110
quantization_tables : [ Option < Arc < [ u16 ; 64 ] > > ; 4 ] ,
111
111
112
112
restart_interval : u16 ,
113
- color_transform : ColorTransform ,
113
+ fallback_color_transform : ColorTransform ,
114
+ color_transform : Option < ColorTransform > ,
114
115
is_jfif : bool ,
115
116
is_mjpeg : bool ,
116
117
@@ -137,8 +138,9 @@ impl<R: Read> Decoder<R> {
137
138
ac_huffman_tables : vec ! [ None , None , None , None ] ,
138
139
quantization_tables : [ None , None , None , None ] ,
139
140
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 ,
142
144
is_jfif : false ,
143
145
is_mjpeg : false ,
144
146
icc_markers : Vec :: new ( ) ,
@@ -149,9 +151,15 @@ impl<R: Read> Decoder<R> {
149
151
}
150
152
}
151
153
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.
153
161
pub fn set_color_transform ( & mut self , transform : ColorTransform ) {
154
- self . color_transform = transform;
162
+ self . color_transform = Some ( transform) ;
155
163
}
156
164
157
165
/// Set maximum buffer size allowed for decoded images
@@ -521,8 +529,12 @@ impl<R: Read> Decoder<R> {
521
529
if let Some ( data) = parse_app ( & mut self . reader , marker) ? {
522
530
match data {
523
531
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
+ }
526
538
}
527
539
AppData :: Jfif => {
528
540
// From the JFIF spec:
@@ -674,12 +686,18 @@ impl<R: Read> Decoder<R> {
674
686
if frame. coding_process == CodingProcess :: Lossless {
675
687
compute_image_lossless ( frame, planes_u16)
676
688
} 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
+
677
695
compute_image (
678
696
& frame. components ,
679
697
planes,
680
698
frame. output_size ,
681
699
self . is_jfif ,
682
- self . color_transform ,
700
+ color_transform,
683
701
)
684
702
}
685
703
}
0 commit comments