Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create an ImageFormat from a MIME type #1538

Merged
merged 3 commits into from
Aug 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,35 @@ impl ImageFormat {
inner(path.as_ref())
}

/// Return the image format specified by a MIME type.
///
/// # Example
///
/// ```
/// use image::ImageFormat;
///
/// let format = ImageFormat::from_mime_type("image/png").unwrap();
/// assert_eq!(format, ImageFormat::Png);
/// ```
pub fn from_mime_type<M>(mime_type: M) -> Option<Self> where M : AsRef<str> {
match mime_type.as_ref() {
"image/avif" => Some(ImageFormat::Avif),
"image/jpeg" => Some(ImageFormat::Jpeg),
"image/png" => Some(ImageFormat::Png),
"image/gif" => Some(ImageFormat::Gif),
"image/webp" => Some(ImageFormat::WebP),
"image/tiff" => Some(ImageFormat::Tiff),
"image/x-targa" | "image/x-tga" => Some(ImageFormat::Tga),
"image/vnd-ms.dds" => Some(ImageFormat::Dds),
"image/bmp" => Some(ImageFormat::Bmp),
"image/x-icon" => Some(ImageFormat::Ico),
"image/vnd.radiance" => Some(ImageFormat::Hdr),
"image/x-exr" => Some(ImageFormat::OpenExr),
"image/x-portable-bitmap" | "image/x-portable-graymap" | "image/x-portable-pixmap" | "image/x-portable-anymap" => Some(ImageFormat::Pnm),
_ => None
}
}

/// Return if the ImageFormat can be decoded by the lib.
#[inline]
pub fn can_read(&self) -> bool {
Expand Down