#[non_exhaustive]pub struct ImageFormatDefinition {Show 13 fields
pub name: &'static str,
pub image_format: Option<ImageFormat>,
pub display_name: &'static str,
pub preferred_extension: &'static str,
pub extensions: &'static [&'static str],
pub preferred_mime_type: &'static str,
pub mime_types: &'static [&'static str],
pub supports_alpha: bool,
pub supports_animation: bool,
pub supports_lossless: bool,
pub supports_lossy: bool,
pub magic_bytes_needed: usize,
pub detect: fn(&[u8]) -> bool,
}Expand description
Describes an image format’s metadata, capabilities, and detection logic.
Used both for built-in formats (via ImageFormatRegistry::common()) and
for custom formats defined by downstream crates. Define as a static and
reference via ImageFormat::Custom.
Identity is based on name — two definitions with the same name are
considered equal.
§Example
use zencodec::{ImageFormatDefinition, ImageFormat, ImageFormatRegistry};
fn detect_jpeg2000(data: &[u8]) -> bool {
data.len() >= 12 && data[..4] == [0x00, 0x00, 0x00, 0x0C]
&& &data[4..8] == b"jP "
}
static JPEG2000: ImageFormatDefinition = ImageFormatDefinition::new(
"jpeg2000",
None,
"JPEG 2000",
"jp2",
&["jp2", "j2k", "jpx"],
"image/jp2",
&["image/jp2", "image/jpx"],
true, // alpha
false, // animation
true, // lossless
true, // lossy
12,
detect_jpeg2000,
);
// Build a registry with custom + common formats
let registry = ImageFormatRegistry::from_vec(vec![&JPEG2000]);
let fmt = registry.detect(data);Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.name: &'static strUnique lowercase format identifier (e.g. "jpeg2000", "dds").
Used for equality comparison and hashing. Must be unique across all format definitions in use.
image_format: Option<ImageFormat>The corresponding built-in ImageFormat variant, if any.
Set to Some(ImageFormat::Jpeg) etc. for definitions that describe
built-in formats. Set to None for custom formats — the registry
wraps them as ImageFormat::Custom.
display_name: &'static strHuman-readable format name for display (e.g. "JPEG 2000", "DDS").
preferred_extension: &'static strPrimary file extension without dot (e.g. "jp2").
extensions: &'static [&'static str]All recognized file extensions (must include preferred_extension).
preferred_mime_type: &'static strPrimary MIME type (e.g. "image/jp2").
mime_types: &'static [&'static str]All recognized MIME types (must include preferred_mime_type).
supports_alpha: boolWhether this format supports alpha channel.
supports_animation: boolWhether this format supports animation.
supports_lossless: boolWhether this format supports lossless encoding.
supports_lossy: boolWhether this format supports lossy encoding.
magic_bytes_needed: usizeRecommended bytes to fetch for reliable format probing.
The detect function must still handle shorter inputs safely
(returning false for inconclusive data).
detect: fn(&[u8]) -> boolMagic byte detection function.
Returns true if the data appears to be this format.
Must handle any data length safely (including empty slices).
Implementations§
Source§impl ImageFormatDefinition
impl ImageFormatDefinition
Sourcepub const fn new(
name: &'static str,
image_format: Option<ImageFormat>,
display_name: &'static str,
preferred_extension: &'static str,
extensions: &'static [&'static str],
preferred_mime_type: &'static str,
mime_types: &'static [&'static str],
supports_alpha: bool,
supports_animation: bool,
supports_lossless: bool,
supports_lossy: bool,
magic_bytes_needed: usize,
detect: fn(&[u8]) -> bool,
) -> Self
pub const fn new( name: &'static str, image_format: Option<ImageFormat>, display_name: &'static str, preferred_extension: &'static str, extensions: &'static [&'static str], preferred_mime_type: &'static str, mime_types: &'static [&'static str], supports_alpha: bool, supports_animation: bool, supports_lossless: bool, supports_lossy: bool, magic_bytes_needed: usize, detect: fn(&[u8]) -> bool, ) -> Self
Create a new format definition.
All fields are required. For built-in formats, set image_format to the
corresponding ImageFormat variant. For custom formats, set it to None.
Sourcepub fn to_image_format(&'static self) -> ImageFormat
pub fn to_image_format(&'static self) -> ImageFormat
Convert this definition to the corresponding ImageFormat.
Returns the built-in variant if image_format is Some, otherwise
wraps as ImageFormat::Custom.