From 990b490dc42b9761afd2269f08778912f64d48b2 Mon Sep 17 00:00:00 2001 From: Wesley King Date: Mon, 23 Oct 2023 20:05:41 -0400 Subject: [PATCH 1/3] #1994 - Support for iterating enabled image formats --- src/image.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/image.rs b/src/image.rs index 1fb8d74438..5924968a82 100644 --- a/src/image.rs +++ b/src/image.rs @@ -307,6 +307,58 @@ impl ImageFormat { ImageFormat::Qoi => &["qoi"], } } + + /// Return the ImageFormats which are enabled for reading. + #[inline] + pub fn reading_enabled(&self) -> bool { + match self { + ImageFormat::Png => cfg!(feature = "png"), + ImageFormat::Gif => cfg!(feature = "gif"), + ImageFormat::Jpeg => cfg!(feature = "jpeg"), + ImageFormat::WebP => cfg!(feature = "webp"), + ImageFormat::Tiff => cfg!(feature = "tiff"), + ImageFormat::Tga => cfg!(feature = "tga"), + ImageFormat::Bmp => cfg!(feature = "bmp"), + ImageFormat::Ico => cfg!(feature = "ico"), + ImageFormat::Hdr => cfg!(feature = "hdr"), + ImageFormat::OpenExr => cfg!(feature = "openexr"), + ImageFormat::Pnm => cfg!(feature = "pnm"), + ImageFormat::Farbfeld => cfg!(feature = "farbfeld"), + ImageFormat::Avif => cfg!(feature = "avif"), + ImageFormat::Qoi => cfg!(feature = "qoi"), + ImageFormat::Dds => false + } + } + + /// Return the ImageFormats which are enabled for writing. + #[inline] + pub fn writing_enabled(&self) -> bool { + match self { + ImageFormat::Gif => cfg!(feature = "gif"), + ImageFormat::Ico => cfg!(feature = "ico"), + ImageFormat::Jpeg => cfg!(feature = "jpeg"), + ImageFormat::Png => cfg!(feature = "png"), + ImageFormat::Bmp => cfg!(feature = "bmp"), + ImageFormat::Tiff => cfg!(feature = "tiff"), + ImageFormat::Tga => cfg!(feature = "tga"), + ImageFormat::Pnm => cfg!(feature = "pnm"), + ImageFormat::Farbfeld => cfg!(feature = "farbfeld"), + ImageFormat::Avif => cfg!(feature = "avif"), + ImageFormat::WebP => cfg!(feature = "webp"), + ImageFormat::OpenExr => cfg!(feature = "openexr"), + ImageFormat::Qoi => cfg!(feature = "qoi"), + ImageFormat::Dds => false, + ImageFormat::Hdr => false + } + } + + /// Return all ImageFormats + fn all() -> impl Iterator { + [ImageFormat::Gif, ImageFormat::Ico, ImageFormat::Jpeg, ImageFormat::Png, ImageFormat::Bmp, + ImageFormat::Tiff, ImageFormat::Tga, ImageFormat::Pnm, ImageFormat::Farbfeld, + ImageFormat::Avif, ImageFormat::WebP, ImageFormat::OpenExr, ImageFormat::Qoi, + ImageFormat::Dds, ImageFormat::Hdr].iter().copied() + } } /// An enumeration of supported image formats for encoding. @@ -1386,6 +1438,7 @@ where #[cfg(test)] mod tests { + use std::collections::HashSet; use std::io; use std::path::Path; @@ -1910,4 +1963,27 @@ mod tests { let v: ImageResult> = super::decoder_to_vec(D); assert!(v.is_err()); } + + #[test] + fn all() { + let all_formats: HashSet = HashSet::from_iter(ImageFormat::all()); + assert!(all_formats.contains(&ImageFormat::Avif)); + assert!(all_formats.contains(&ImageFormat::Gif)); + assert!(all_formats.contains(&ImageFormat::Bmp)); + assert!(all_formats.contains(&ImageFormat::Farbfeld)); + assert!(all_formats.contains(&ImageFormat::Jpeg)); + } + + #[test] + fn reading_enabled() { + assert_eq!(cfg!(feature = "jpeg"), ImageFormat::Jpeg.reading_enabled()); + assert_eq!(false, ImageFormat::Dds.reading_enabled()); + } + + #[test] + fn writing_enabled() { + assert_eq!(cfg!(feature = "jpeg"), ImageFormat::Jpeg.writing_enabled()); + assert_eq!(false, ImageFormat::Hdr.writing_enabled()); + assert_eq!(false, ImageFormat::Dds.writing_enabled()); + } } From e794c0db213cc9cc889900f33fa08e299812c887 Mon Sep 17 00:00:00 2001 From: Wesley King Date: Mon, 23 Oct 2023 20:24:21 -0400 Subject: [PATCH 2/3] Fix rust formatting --- src/image.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/image.rs b/src/image.rs index 5924968a82..c1c4968aaa 100644 --- a/src/image.rs +++ b/src/image.rs @@ -326,7 +326,7 @@ impl ImageFormat { ImageFormat::Farbfeld => cfg!(feature = "farbfeld"), ImageFormat::Avif => cfg!(feature = "avif"), ImageFormat::Qoi => cfg!(feature = "qoi"), - ImageFormat::Dds => false + ImageFormat::Dds => false, } } @@ -348,16 +348,31 @@ impl ImageFormat { ImageFormat::OpenExr => cfg!(feature = "openexr"), ImageFormat::Qoi => cfg!(feature = "qoi"), ImageFormat::Dds => false, - ImageFormat::Hdr => false + ImageFormat::Hdr => false, } } /// Return all ImageFormats - fn all() -> impl Iterator { - [ImageFormat::Gif, ImageFormat::Ico, ImageFormat::Jpeg, ImageFormat::Png, ImageFormat::Bmp, - ImageFormat::Tiff, ImageFormat::Tga, ImageFormat::Pnm, ImageFormat::Farbfeld, - ImageFormat::Avif, ImageFormat::WebP, ImageFormat::OpenExr, ImageFormat::Qoi, - ImageFormat::Dds, ImageFormat::Hdr].iter().copied() + fn all() -> impl Iterator { + [ + ImageFormat::Gif, + ImageFormat::Ico, + ImageFormat::Jpeg, + ImageFormat::Png, + ImageFormat::Bmp, + ImageFormat::Tiff, + ImageFormat::Tga, + ImageFormat::Pnm, + ImageFormat::Farbfeld, + ImageFormat::Avif, + ImageFormat::WebP, + ImageFormat::OpenExr, + ImageFormat::Qoi, + ImageFormat::Dds, + ImageFormat::Hdr, + ] + .iter() + .copied() } } From d5dda3d1cf5fa0374eec52d5e2cefb7595afff7f Mon Sep 17 00:00:00 2001 From: Wesley King Date: Mon, 23 Oct 2023 21:21:12 -0400 Subject: [PATCH 3/3] Fix cargo clippy warnings --- src/image.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/image.rs b/src/image.rs index c1c4968aaa..d12778a06b 100644 --- a/src/image.rs +++ b/src/image.rs @@ -353,7 +353,7 @@ impl ImageFormat { } /// Return all ImageFormats - fn all() -> impl Iterator { + pub fn all() -> impl Iterator { [ ImageFormat::Gif, ImageFormat::Ico, @@ -1992,13 +1992,13 @@ mod tests { #[test] fn reading_enabled() { assert_eq!(cfg!(feature = "jpeg"), ImageFormat::Jpeg.reading_enabled()); - assert_eq!(false, ImageFormat::Dds.reading_enabled()); + assert!(!ImageFormat::Dds.reading_enabled()); } #[test] fn writing_enabled() { assert_eq!(cfg!(feature = "jpeg"), ImageFormat::Jpeg.writing_enabled()); - assert_eq!(false, ImageFormat::Hdr.writing_enabled()); - assert_eq!(false, ImageFormat::Dds.writing_enabled()); + assert!(!ImageFormat::Hdr.writing_enabled()); + assert!(!ImageFormat::Dds.writing_enabled()); } }