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

#1994 - Support for iterating enabled image formats #2038

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Changes from 2 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
91 changes: 91 additions & 0 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,73 @@ 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<Item = ImageFormat> {
[
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.
Expand Down Expand Up @@ -1386,6 +1453,7 @@ where

#[cfg(test)]
mod tests {
use std::collections::HashSet;
use std::io;
use std::path::Path;

Expand Down Expand Up @@ -1910,4 +1978,27 @@ mod tests {
let v: ImageResult<Vec<u8>> = super::decoder_to_vec(D);
assert!(v.is_err());
}

#[test]
fn all() {
let all_formats: HashSet<ImageFormat> = 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());
}
}
Loading