@@ -40,6 +40,19 @@ impl ImageFormatEnum {
4040 ImageFormatEnum :: Tiff => ImageFormat :: Tiff ,
4141 }
4242 }
43+
44+ pub fn from_image_format ( format : ImageFormat ) -> Option < Self > {
45+ match format {
46+ ImageFormat :: Png => Some ( ImageFormatEnum :: Png ) ,
47+ ImageFormat :: Jpeg => Some ( ImageFormatEnum :: Jpeg ) ,
48+ ImageFormat :: Gif => Some ( ImageFormatEnum :: Gif ) ,
49+ ImageFormat :: WebP => Some ( ImageFormatEnum :: WebP ) ,
50+ ImageFormat :: Bmp => Some ( ImageFormatEnum :: Bmp ) ,
51+ ImageFormat :: Ico => Some ( ImageFormatEnum :: Ico ) ,
52+ ImageFormat :: Tiff => Some ( ImageFormatEnum :: Tiff ) ,
53+ _ => None ,
54+ }
55+ }
4356}
4457
4558#[ allow( dead_code) ]
@@ -216,6 +229,18 @@ pub fn get_metadata(img: &DynamicImage) -> ImageMetadata {
216229 }
217230}
218231
232+ /// Guess image format from byte data
233+ pub fn guess_image_format ( data : & [ u8 ] ) -> Result < ImageFormatEnum , ImageError > {
234+ let format = image:: guess_format ( data) ?;
235+ ImageFormatEnum :: from_image_format ( format)
236+ . ok_or_else ( || {
237+ ImageError :: Unsupported ( image:: error:: UnsupportedError :: from_format_and_kind (
238+ image:: error:: ImageFormatHint :: Unknown ,
239+ image:: error:: UnsupportedErrorKind :: Format ( image:: error:: ImageFormatHint :: Unknown ) ,
240+ ) )
241+ } )
242+ }
243+
219244/// Convert ImageError to error code
220245pub fn error_to_code ( err : & ImageError ) -> ImageErrorCode {
221246 match err {
0 commit comments