@@ -937,13 +937,19 @@ fn ycbcr_to_rgb(y: u8, cb: u8, cr: u8) -> (u8, u8, u8) {
937937 let g = y - 0.34414 * cb - 0.71414 * cr;
938938 let b = y + 1.77200 * cb;
939939
940- ( clamp ( ( r + 0.5 ) as i32 , 0 , 255 ) as u8 ,
941- clamp ( ( g + 0.5 ) as i32 , 0 , 255 ) as u8 ,
942- clamp ( ( b + 0.5 ) as i32 , 0 , 255 ) as u8 )
940+ // TODO: Rust has defined float-to-int conversion as saturating,
941+ // which is exactly what we need here. However, as of this writing
942+ // it still hasn't reached the stable channel.
943+ // This can be simplified to `(r + 0.5) as u8` without any clamping
944+ // as soon as our MSRV reaches the version that has saturating casts.
945+ // The version without explicit clamping is also noticeably faster.
946+ ( clamp_to_u8 ( ( r + 0.5 ) as i32 ) as u8 ,
947+ clamp_to_u8 ( ( g + 0.5 ) as i32 ) as u8 ,
948+ clamp_to_u8 ( ( b + 0.5 ) as i32 ) as u8 )
943949}
944950
945- fn clamp < T : PartialOrd > ( value : T , min : T , max : T ) -> T {
946- if value < min { return min ; }
947- if value > max { return max ; }
951+ fn clamp_to_u8 ( value : i32 ) -> i32 {
952+ let value = std :: cmp :: max ( value , 0 ) ;
953+ let value = std :: cmp :: min ( value , 255 ) ;
948954 value
949955}
0 commit comments