@@ -745,6 +745,14 @@ impl DynamicImage {
745745 dynamic_map ! ( self , ref p, p. color_space( ) )
746746 }
747747
748+ /// Set primaries and transfer characteristics from a Cicp color space.
749+ ///
750+ /// Returns an error if `cicp` uses features that are not support with an RGB color space, e.g.
751+ /// a matrix or narrow range (studio encoding) channels.
752+ pub fn set_color_space ( & mut self , cicp : Cicp ) -> ImageResult < ( ) > {
753+ dynamic_map ! ( self , ref mut p, p. set_color_space( cicp) )
754+ }
755+
748756 /// Whether the image contains an alpha channel
749757 ///
750758 /// This is a convenience wrapper around `self.color().has_alpha()`.
@@ -1189,20 +1197,12 @@ impl DynamicImage {
11891197 return Ok ( ( ) ) ;
11901198 }
11911199
1192- // Forward compatibility: make sure we do not drop any details here.
1193- if Cicp :: from ( cicp. into_rgb ( ) ) != cicp {
1194- return Err ( ImageError :: Parameter ( ParameterError :: from_kind (
1195- ParameterErrorKind :: Generic ( "Not an RGB like CICP color space" . to_string ( ) ) ,
1196- ) ) ) ;
1197- }
1198-
11991200 // We could conceivably do this in-place faster but to handle the Luma conversion as we
12001201 // want this requires the full machinery as `CicpTransform::transform_dynamic` which is
12011202 // quite the replication. Let's just see if it is fast enough. Feel free to PR something if
12021203 // it is easy enough to review.
12031204 let mut target = self . clone ( ) ;
1204- target. set_rgb_primaries ( cicp. primaries ) ;
1205- target. set_transfer_function ( cicp. transfer ) ;
1205+ target. set_color_space ( cicp) ?;
12061206 target. copy_from_color_space ( self , options) ?;
12071207
12081208 * self = target;
@@ -1233,16 +1233,9 @@ impl DynamicImage {
12331233 }
12341234
12351235 // Forward compatibility: make sure we do not drop any details here.
1236- if Cicp :: from ( cicp. into_rgb ( ) ) != cicp {
1237- return Err ( ImageError :: Parameter ( ParameterError :: from_kind (
1238- ParameterErrorKind :: Generic ( "Not an RGB like CICP color space" . to_string ( ) ) ,
1239- ) ) ) ;
1240- }
1241-
1236+ let rgb = cicp. try_into_rgb ( ) ?;
12421237 let mut target = DynamicImage :: new ( self . width ( ) , self . height ( ) , color) ;
1243-
1244- target. set_rgb_primaries ( cicp. primaries ) ;
1245- target. set_transfer_function ( cicp. transfer ) ;
1238+ dynamic_map ! ( target, ref mut p, p. set_rgb_color_space( rgb) ) ;
12461239 target. copy_from_color_space ( self , options) ?;
12471240
12481241 * self = target;
0 commit comments