diff --git a/crates/image-codecs/Cargo.toml b/crates/image-codecs/Cargo.toml index a8872c0d3..7cb8cb4a1 100644 --- a/crates/image-codecs/Cargo.toml +++ b/crates/image-codecs/Cargo.toml @@ -13,6 +13,7 @@ workspace = true [features] corpus = [] default = [] +sgi = [] avif = ["image/avif", "dep:ravif", "dep:rav1e", "dep:avif-serialize"] heif = ["dep:craft-heif"] diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index bf854ff3e..ef9ceb102 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -56,7 +56,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "< | Cineon | yes | yes | U16 (10-bit code values read and written exactly through U16) | Gray, RGB | no | no | no | no | no | no | no | built in | | DICOM | yes | **no** | U8, U16 | Gray, RGB | no | no | no | no | no | no | n/a | built in | | Amiga IFF (ILBM/PBM) | yes | yes (24-bit ILBM) | U8 | RGB, RGBA | yes (1-bit mask plane on write) | no | no | no | no | no | no | built in | -| SGI (`.sgi`, `.rgb`, `.bw`) | yes | yes (RLE) | U8, U16 | Gray, Gray+A, RGB, RGBA | yes | no | no | no | no | no | no | built in | +| SGI (`.sgi`, `.rgb`, `.bw`) | with feature `sgi` | with feature `sgi` (RLE) | U8, U16 | Gray, Gray+A, RGB, RGBA | yes | no | no | no | no | no | no | `sgi` (off by default) | | JPEG-LS (`.jls`) | yes (any NEAR; one scan per component) | yes (lossless) | U8, U16 | Gray, RGB | no | no | no | no | no | no | no | built in | | AVIF | **no** | only with feature `avif` | U8 | RGB, RGBA | yes | no | no | no | no | no | yes | `image` + `ravif` | | HEIF/HEIC | with feature `heif` | **no** | U8, U16 (10/12-bit decodes to U16) | RGB, RGBA | yes (auxiliary alpha) | yes (`colr` prof) | yes | yes | no | no | n/a | `heic-rs` | diff --git a/crates/image-codecs/src/codecs.rs b/crates/image-codecs/src/codecs.rs index bacbdfaaf..9ddda8311 100644 --- a/crates/image-codecs/src/codecs.rs +++ b/crates/image-codecs/src/codecs.rs @@ -18,6 +18,7 @@ pub(crate) mod jxl; pub(crate) mod pcx; pub(crate) mod png; pub(crate) mod pnm; +#[cfg(feature = "sgi")] pub(crate) mod sgi; pub(crate) mod tga; pub(crate) mod tiff; diff --git a/crates/image-codecs/src/format.rs b/crates/image-codecs/src/format.rs index eeb9f21c6..62cae8efe 100644 --- a/crates/image-codecs/src/format.rs +++ b/crates/image-codecs/src/format.rs @@ -322,7 +322,9 @@ pub fn caps(format: Format) -> FormatCaps { Format::Cineon => FormatCaps { depths: &[S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Dicom => FormatCaps { read: true, write: false, depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Iff => FormatCaps { depths: &[S::U8], layouts: &[L::Rgb, L::Rgba], alpha: true, ..base }, - Format::Sgi => FormatCaps { depths: &[S::U8, S::U16], layouts: RGB_GRAY, alpha: true, ..base }, + Format::Sgi => { + FormatCaps { read: cfg!(feature = "sgi"), write: cfg!(feature = "sgi"), depths: &[S::U8, S::U16], layouts: RGB_GRAY, alpha: true, ..base } + } Format::JpegLs => FormatCaps { depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Jp2 => FormatCaps { read: true, write: false, layouts: &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk, L::CmykA], icc: true, ..base }, } @@ -405,6 +407,7 @@ pub fn detect(bytes: &[u8]) -> Option { if b.starts_with(b"/* XPM */") || b.starts_with(b"! XPM2\n") || b.starts_with(b"! XPM2\r\n") { return Some(Format::Xpm); } + #[cfg(feature = "sgi")] if crate::codecs::sgi::looks_like_sgi(b) { return Some(Format::Sgi); } diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 3098e5068..f6399b5e8 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -64,7 +64,9 @@ pub use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_ pub use crate::source_settings::{WebpEncoding, jpeg_quality, webp_encoding}; pub use half::f16; -use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jpegls, jxl, pcx, png, pnm, sgi, tiff, via_image, webp, xpm}; +#[cfg(feature = "sgi")] +use crate::codecs::sgi; +use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jpegls, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; /// Detect the format and decode with default [`Limits`]. pub fn decode(bytes: &[u8]) -> Result { @@ -103,7 +105,16 @@ pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Res Format::Cineon => dpx::decode_cineon(bytes, l), Format::Dicom => dicom::decode(bytes, l), Format::Iff => iff::decode(bytes, l), - Format::Sgi => sgi::decode(bytes, l), + Format::Sgi => { + #[cfg(feature = "sgi")] + { + sgi::decode(bytes, l) + } + #[cfg(not(feature = "sgi"))] + { + Err(CodecError::unsupported(format, "SGI support requires the sgi feature")) + } + } Format::JpegLs => jpegls::decode(bytes, l), Format::OpenExr => exr::decode(bytes, l), Format::Heif => heif::decode(bytes, l, opts.keep_orientation), @@ -461,7 +472,16 @@ pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result dpx::encode_cineon(image, plan), Format::Dicom => Err(CodecError::unsupported(format, "DICOM is import-only; save as TIFF or PNG instead")), Format::Iff => iff::encode(image, plan), - Format::Sgi => sgi::encode(image, plan), + Format::Sgi => { + #[cfg(feature = "sgi")] + { + sgi::encode(image, plan) + } + #[cfg(not(feature = "sgi"))] + { + Err(CodecError::unsupported(format, "SGI support requires the sgi feature")) + } + } Format::JpegLs => jpegls::encode(image, plan), Format::OpenExr => exr::encode(image, plan, opts), Format::Heif => Err(CodecError::unsupported(format, "encoding is not available in this build")), diff --git a/crates/image-codecs/tests/it/detect_caps.rs b/crates/image-codecs/tests/it/detect_caps.rs index c8c7602db..d1055ae1f 100644 --- a/crates/image-codecs/tests/it/detect_caps.rs +++ b/crates/image-codecs/tests/it/detect_caps.rs @@ -62,6 +62,7 @@ fn default_build_formats_all_symmetric() { Format::Dpx, Format::Cineon, Format::Iff, + #[cfg(feature = "sgi")] Format::Sgi, Format::JpegLs, ]; @@ -220,6 +221,7 @@ fn detect_magic_iff() { assert_eq!(detect(b"FORM\0\0\0\x14AIFF"), None); assert!(decode_as(Format::Iff, b"FORM\0\0\0\x14ILBM").is_err()); } +#[cfg(feature = "sgi")] #[test] fn detect_magic_sgi() { use craft_image_codecs::{ChannelLayout, EncodeOptions, Image, encode}; @@ -340,3 +342,16 @@ fn extensions_unique_across_formats() { all.dedup(); assert_eq!(all.len(), n); } + +#[cfg(not(feature = "sgi"))] +#[test] +fn sgi_requires_explicit_feature() { + use craft_image_codecs::{ChannelLayout, EncodeOptions, Image, encode}; + assert!(!caps(Format::Sgi).read && !caps(Format::Sgi).write); + assert!(decode_as(Format::Sgi, &[]).is_err()); + let image = Image::from_u8(1, 1, ChannelLayout::Gray, vec![0]).unwrap(); + assert!(encode(&image, Format::Sgi, &EncodeOptions::default()).is_err()); + let mut header = vec![0; 512]; + header[..12].copy_from_slice(&[1, 218, 0, 1, 0, 2, 0, 1, 0, 1, 0, 1]); + assert_eq!(detect(&header), None); +}