Skip to main content

ImageFormatDefinition

Struct ImageFormatDefinition 

Source
#[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
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§name: &'static str

Unique 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 str

Human-readable format name for display (e.g. "JPEG 2000", "DDS").

§preferred_extension: &'static str

Primary file extension without dot (e.g. "jp2").

§extensions: &'static [&'static str]

All recognized file extensions (must include preferred_extension).

§preferred_mime_type: &'static str

Primary MIME type (e.g. "image/jp2").

§mime_types: &'static [&'static str]

All recognized MIME types (must include preferred_mime_type).

§supports_alpha: bool

Whether this format supports alpha channel.

§supports_animation: bool

Whether this format supports animation.

§supports_lossless: bool

Whether this format supports lossless encoding.

§supports_lossy: bool

Whether this format supports lossy encoding.

§magic_bytes_needed: usize

Recommended bytes to fetch for reliable format probing.

The detect function must still handle shorter inputs safely (returning false for inconclusive data).

§detect: fn(&[u8]) -> bool

Magic 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

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for ImageFormatDefinition

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for ImageFormatDefinition

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for ImageFormatDefinition

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for ImageFormatDefinition

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.