@@ -2,7 +2,10 @@ use std::ops::{Index, IndexMut};
2
2
3
3
use num_traits:: { NumCast , ToPrimitive , Zero } ;
4
4
5
- use crate :: traits:: { Enlargeable , Pixel , Primitive } ;
5
+ use crate :: {
6
+ error:: TryFromExtendedColorError ,
7
+ traits:: { Enlargeable , Pixel , Primitive } ,
8
+ } ;
6
9
7
10
/// An enumeration over supported color types and bit depths
8
11
#[ derive( Copy , PartialEq , Eq , Debug , Clone , Hash ) ]
@@ -230,13 +233,53 @@ impl ExtendedColorType {
230
233
}
231
234
}
232
235
236
+ /// Returns the ColorType that is equivalent to this ExtendedColorType.
237
+ ///
238
+ /// # Example
239
+ ///
240
+ /// ```
241
+ /// use image::{ColorType, ExtendedColorType};
242
+ ///
243
+ /// assert_eq!(Some(ColorType::L8), ExtendedColorType::L8.color_type());
244
+ /// assert_eq!(None, ExtendedColorType::L1.color_type());
245
+ /// ```
246
+ ///
247
+ /// The method is equivalent to converting via the `TryFrom`/`TryInto` traits except for the
248
+ /// error path. Choose the more ergonomic option in your usage.
249
+ ///
250
+ /// ```
251
+ /// use image::{ColorType, ExtendedColorType, ImageError};
252
+ ///
253
+ /// fn handle_errors() -> Result<(), ImageError> {
254
+ /// let color: ColorType = ExtendedColorType::L8.try_into()?;
255
+ /// assert_eq!(color, ColorType::L8);
256
+ /// # Ok(())
257
+ /// }
258
+ /// ```
259
+ pub fn color_type ( & self ) -> Option < ColorType > {
260
+ match * self {
261
+ ExtendedColorType :: L8 => Some ( ColorType :: L8 ) ,
262
+ ExtendedColorType :: La8 => Some ( ColorType :: La8 ) ,
263
+ ExtendedColorType :: Rgb8 => Some ( ColorType :: Rgb8 ) ,
264
+ ExtendedColorType :: Rgba8 => Some ( ColorType :: Rgba8 ) ,
265
+ ExtendedColorType :: L16 => Some ( ColorType :: L16 ) ,
266
+ ExtendedColorType :: La16 => Some ( ColorType :: La16 ) ,
267
+ ExtendedColorType :: Rgb16 => Some ( ColorType :: Rgb16 ) ,
268
+ ExtendedColorType :: Rgba16 => Some ( ColorType :: Rgba16 ) ,
269
+ ExtendedColorType :: Rgb32F => Some ( ColorType :: Rgb32F ) ,
270
+ ExtendedColorType :: Rgba32F => Some ( ColorType :: Rgba32F ) ,
271
+ _ => None ,
272
+ }
273
+ }
274
+
233
275
/// Returns the number of bytes required to hold a width x height image of this color type.
234
276
pub ( crate ) fn buffer_size ( self , width : u32 , height : u32 ) -> u64 {
235
277
let bpp = self . bits_per_pixel ( ) as u64 ;
236
278
let row_pitch = ( width as u64 * bpp) . div_ceil ( 8 ) ;
237
279
row_pitch. saturating_mul ( height as u64 )
238
280
}
239
281
}
282
+
240
283
impl From < ColorType > for ExtendedColorType {
241
284
fn from ( c : ColorType ) -> Self {
242
285
match c {
@@ -254,6 +297,16 @@ impl From<ColorType> for ExtendedColorType {
254
297
}
255
298
}
256
299
300
+ impl TryFrom < ExtendedColorType > for ColorType {
301
+ type Error = TryFromExtendedColorError ;
302
+
303
+ fn try_from ( value : ExtendedColorType ) -> Result < ColorType , Self :: Error > {
304
+ value
305
+ . color_type ( )
306
+ . ok_or ( TryFromExtendedColorError { was : value } )
307
+ }
308
+ }
309
+
257
310
macro_rules! define_colors {
258
311
{ $(
259
312
$( #[ $doc: meta] ) *
0 commit comments