Skip to main content

ImageType

Enum ImageType 

Source
#[non_exhaustive]
pub enum ImageType {
Show 26 variants Aseprite, Astc, Atc(AtcCompression), Bmp, Dds(DdsCompression), Eac(PkmCompression), Etc2(PkmCompression), Exr, Farbfeld, Gif, Hdr, Heif(Compression), Ico, Ilbm, Jpeg, Jxl, Ktx2, Png, Pnm, Pvrtc(PvrtcCompression), Psd, Qoi, Tga, Tiff, Vtf, Webp,
}
Expand description

Types of image formats that this crate can identify.

Many container formats support multiple inner compression formats. For these formats, the enum contains the inner compression type to provide more detailed information:

  • Dds(DdsCompression) - DirectDraw Surface with various BC compression formats
  • Etc2(PkmCompression) - ETC/PKM container with ETC1, ETC2, EAC variants
  • Eac(PkmCompression) - EAC formats (unified with ETC2 detection)
  • Atc(AtcCompression) - Adaptive Texture Compression variants
  • Pvrtc(PvrtcCompression) - PowerVR texture compression with 2bpp/4bpp variants

§Helper Methods

The ImageType provides several helper methods to query compression information across different container formats:

§Examples

§Basic Format Detection

use ai_imagesize::{image_type, ImageType, PkmCompression};

// Create a PKM header for ETC2 format
let mut header = vec![b'P', b'K', b'M', b' ', b'2', b'0'];
header.extend_from_slice(&0x0001u16.to_be_bytes()); // ETC2 RGB
header.extend_from_slice(&[0x00, 0x40, 0x00, 0x40]); // Extended dimensions
header.extend_from_slice(&[0x00, 0x40, 0x00, 0x40]); // Original dimensions

match image_type(&header).unwrap() {
    ImageType::Etc2(PkmCompression::Etc2) => println!("This is ETC2 RGB format"),
    ImageType::Etc2(compression) => println!("This is ETC2 format: {:?}", compression),
    other => println!("Other format: {:?}", other),
}

§Using Helper Methods for Cross-Container Queries

use ai_imagesize::{ImageType, CompressionFamily, DdsCompression, PvrtcCompression};

// Query compression families across different containers
let dds_bc1 = ImageType::Dds(DdsCompression::Bc1);
let pvr_etc2 = ImageType::Pvrtc(PvrtcCompression::Etc2Rgb);
let png = ImageType::Png;

// Group related compression algorithms
assert_eq!(dds_bc1.compression_family(), Some(CompressionFamily::BlockCompression));
assert_eq!(pvr_etc2.compression_family(), Some(CompressionFamily::Etc));
assert_eq!(png.compression_family(), None); // Simple formats don't have compression

// Check for specific compression types
assert!(dds_bc1.is_block_compressed());
assert!(!pvr_etc2.is_block_compressed());

// Identify container formats
assert_eq!(dds_bc1.container_format(), Some("DDS"));
assert_eq!(pvr_etc2.container_format(), Some("PowerVR"));
assert_eq!(png.container_format(), None);

// Check multi-compression support
assert!(dds_bc1.is_multi_compression_container()); // DDS supports BC1-7, RGBA, etc.
assert!(pvr_etc2.is_multi_compression_container()); // PowerVR supports PVRTC, ETC2, EAC

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Aseprite

Animated sprite image format https://github.com/aseprite/aseprite

§

Astc

Adaptive Scalable Texture Compression

§

Atc(AtcCompression)

Adaptive Texture Compression

§

Bmp

Standard Bitmap

§

Dds(DdsCompression)

DirectDraw Surface

§

Eac(PkmCompression)

Ericsson Texture Compression - Alpha Channel (now unified with ETC2)

§

Etc2(PkmCompression)

Ericsson Texture Compression 2 (includes ETC1, ETC2 variants)

§

Exr

OpenEXR

§

Farbfeld

§

Gif

Standard GIF

§

Hdr

Radiance HDR

§

Heif(Compression)

Image Container Format

§

Ico

Icon file

§

Ilbm

Interleaved Bitmap

§

Jpeg

Standard JPEG

§

Jxl

JPEG XL

§

Ktx2

Khronos Texture Container

§

Png

Standard PNG

§

Pnm

Portable Any Map

§

Pvrtc(PvrtcCompression)

PowerVR Texture Compression

§

Psd

Photoshop Document

§

Qoi

Quite OK Image Format https://qoiformat.org/

§

Tga

Truevision Graphics Adapter

§

Tiff

Standard TIFF

§

Vtf

Valve Texture Format

§

Webp

Standard Webp

Implementations§

Source§

impl ImageType

Source

pub fn compression_family(&self) -> Option<CompressionFamily>

Returns the compression family for texture formats

Groups related compression algorithms regardless of their container format. Returns None for simple image formats like PNG, JPEG, etc.

§Examples
use ai_imagesize::{ImageType, CompressionFamily, DdsCompression, PvrtcCompression};

let dds_type = ImageType::Dds(DdsCompression::Bc1);
assert_eq!(dds_type.compression_family(), Some(CompressionFamily::BlockCompression));

let pvrtc_etc2_type = ImageType::Pvrtc(PvrtcCompression::Etc2Rgb);
assert_eq!(pvrtc_etc2_type.compression_family(), Some(CompressionFamily::Etc));

let png_type = ImageType::Png;
assert_eq!(png_type.compression_family(), None);
Source

pub fn is_block_compressed(&self) -> bool

Returns true if the image uses block compression (BC/DXT family)

Block compression includes BC1-7 formats (also known as DXT1-5, ATI1-2).

§Examples
use ai_imagesize::{ImageType, DdsCompression};

let bc1_type = ImageType::Dds(DdsCompression::Bc1);
assert!(bc1_type.is_block_compressed());

let png_type = ImageType::Png;
assert!(!png_type.is_block_compressed());
Source

pub fn container_format(&self) -> Option<&'static str>

Returns the container format name for texture formats

Returns a human-readable string identifying the container format. Returns None for simple image formats.

§Examples
use ai_imagesize::{ImageType, DdsCompression, PvrtcCompression};

let dds_type = ImageType::Dds(DdsCompression::Bc1);
assert_eq!(dds_type.container_format(), Some("DDS"));

let pvr_type = ImageType::Pvrtc(PvrtcCompression::Pvrtc2BppRgb);
assert_eq!(pvr_type.container_format(), Some("PowerVR"));

let png_type = ImageType::Png;
assert_eq!(png_type.container_format(), None);
Source

pub fn is_multi_compression_container(&self) -> bool

Returns true if the image format supports multiple compression types within the same container

Some container formats like PowerVR can store different compression algorithms.

§Examples
use ai_imagesize::{ImageType, PvrtcCompression, DdsCompression};

let pvr_type = ImageType::Pvrtc(PvrtcCompression::Etc2Rgb);
assert!(pvr_type.is_multi_compression_container());

let dds_type = ImageType::Dds(DdsCompression::Bc1);
assert!(dds_type.is_multi_compression_container());

let png_type = ImageType::Png;
assert!(!png_type.is_multi_compression_container());
Source

pub fn reader_size<R: BufRead + Seek>( &self, reader: &mut R, ) -> ImageResult<ImageSize>

Calls the correct image size method based on the image type

§Arguments
  • reader - A reader for the data

Trait Implementations§

Source§

impl Clone for ImageType

Source§

fn clone(&self) -> ImageType

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ImageType

Source§

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

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

impl Hash for ImageType

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 Ord for ImageType

Source§

fn cmp(&self, other: &ImageType) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for ImageType

Source§

fn eq(&self, other: &ImageType) -> 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 PartialOrd for ImageType

Source§

fn partial_cmp(&self, other: &ImageType) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for ImageType

Source§

impl Eq for ImageType

Source§

impl StructuralPartialEq for ImageType

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.