Files
craft-libs/crates/raster-codecs/src/pnm.rs
Dylan M. Taylor 5c0b0a3632 Deduplicate raster codecs via shared image implementations (successor to #1198) (#1331)
* Share small raster format implementations

(cherry picked from commit 7aedc1de73)

* Preserve raster facade encode contracts

(cherry picked from commit 2db7c82705)

* Allow alpha in IFF compatibility encoder

(cherry picked from commit 80b55dee5a)

* Test shared raster facade contracts and refresh lockfile
2026-11-14 20:46:22 -05:00

19 lines
920 B
Rust

//! Netpbm compatibility facade; implementation and bounds live in craft-image-codecs.
use crate::{CodecError, Image, Limits, bridge};
use craft_image_codecs::Format;
pub fn looks_like_pnm(bytes: &[u8]) -> bool {
craft_image_codecs::detect(bytes) == Some(Format::Pnm)
}
pub fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
bridge::format(Format::Pnm, bytes, limits)
}
pub fn encode(image: &Image) -> Result<Vec<u8>, CodecError> {
// PFM has only gray and RGB tuples. Preserve this facade's contract that
// unsupported layouts are refused instead of silently dropping alpha.
if image.sample_type() == crate::SampleType::F32 && !matches!(image.layout(), crate::ChannelLayout::Gray | crate::ChannelLayout::Rgb) {
return Err(CodecError::encode("PNM", format!("PFM cannot store {:?}", image.layout())));
}
bridge::encode(image, Format::Pnm, &Default::default())
}