Skip to main content

BlpFile

Struct BlpFile 

Source
pub struct BlpFile {
    pub color_encoding: ColorEncoding,
    pub alpha_size: u8,
    pub preferred_format: PixelFormat,
    pub width: u32,
    pub height: u32,
    /* private fields */
}
Expand description

A parsed BLP texture file.

Load with BlpFile::open (from disk) or BlpFile::from_bytes (from an in-memory buffer), then call BlpFile::get_pixels to decode a mipmap level into raw RGBA bytes.

The entire file is kept in memory so that any mipmap level can be decoded on demand without re-opening the file.

§Example

use rustydemon_blp2::BlpFile;

let blp = BlpFile::open("icon.blp")?;

for level in 0..blp.mipmap_count() as u32 {
    let (pixels, w, h) = blp.get_pixels(level)?;
    println!("mip {level}: {w}×{h} ({} bytes)", pixels.len());
}

Fields§

§color_encoding: ColorEncoding

How the pixel data is encoded on disk.

§alpha_size: u8

Bits of alpha precision stored per pixel.

  • 0 — no alpha (all pixels are fully opaque)
  • 1 — 1-bit alpha (transparent or opaque)
  • 4 — 4-bit alpha (16 levels)
  • 8 — 8-bit alpha (256 levels)
§preferred_format: PixelFormat

DXT sub-format, relevant only when color_encoding == ColorEncoding::Dxt.

§width: u32

Width of the base (level-0) mipmap in pixels.

§height: u32

Height of the base (level-0) mipmap in pixels.

Implementations§

Source§

impl BlpFile

Source

pub fn open(path: impl AsRef<Path>) -> Result<Self, BlpError>

Load and parse a BLP file from disk.

The entire file is read into memory. Use from_bytes if you already have the data in a buffer (e.g. extracted from a CASC archive).

§Errors

Returns BlpError::Io if the file cannot be read, or any parse error documented on from_bytes.

Source

pub fn from_bytes(data: Vec<u8>) -> Result<Self, BlpError>

Parse a BLP file from an in-memory byte buffer.

The buffer is consumed and stored internally so that mipmap data can be decoded later without additional allocations.

§Errors
ErrorCause
BlpError::IoBuffer is too short to contain a valid header
BlpError::InvalidMagicFirst 4 bytes are not BLP0, BLP1, or BLP2
BlpError::InvalidFormatVersionBLP2 format version field ≠ 1
BlpError::UnsupportedEncodingColor encoding byte is unrecognised
BlpError::DataTooShortJPEG header size field exceeds 64 KiB
Source§

impl BlpFile

Source

pub fn mipmap_count(&self) -> usize

The number of mipmap levels stored in this file (0–16).

Counted as the number of leading non-zero entries in the mipmap offset table. A value of 0 means the file contains no usable image data.

Source

pub fn get_pixels( &self, mipmap_level: u32, ) -> Result<(Vec<u8>, u32, u32), BlpError>

Decode a mipmap level and return (pixels, width, height).

pixels is a Vec<u8> containing raw RGBA data (red first, alpha last), 4 bytes per pixel, in row-major left-to-right top-to-bottom order. Its length is always width * height * 4.

mipmap_level is clamped to the available range: requesting level 99 on a file with 3 mip levels returns level 2. Level 0 is always the largest (base) image.

§Errors
ErrorCause
BlpError::NoMipmapsmipmap_count() == 0
BlpError::ImageTooLargewidth × height × 4 overflows or exceeds 256 MiB
BlpError::OutOfBoundsMipmap offset/size points outside the file buffer
BlpError::DataTooShortMipmap slice is too small for the declared dimensions
BlpError::JpegDecodeJPEG data is invalid or corrupt

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.