@@ -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) ]
@@ -224,6 +237,18 @@ pub fn get_metadata(img: &DynamicImage) -> ImageMetadata {
224237 }
225238}
226239
240+ /// Guess image format from byte data
241+ pub fn guess_image_format ( data : & [ u8 ] ) -> Result < ImageFormatEnum , ImageError > {
242+ let format = image:: guess_format ( data) ?;
243+ ImageFormatEnum :: from_image_format ( format)
244+ . ok_or_else ( || {
245+ ImageError :: Unsupported ( image:: error:: UnsupportedError :: from_format_and_kind (
246+ image:: error:: ImageFormatHint :: Unknown ,
247+ image:: error:: UnsupportedErrorKind :: Format ( image:: error:: ImageFormatHint :: Unknown ) ,
248+ ) )
249+ } )
250+ }
251+
227252/// Convert ImageError to error code
228253pub fn error_to_code ( err : & ImageError ) -> ImageErrorCode {
229254 match err {
0 commit comments