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: ColorEncodingHow the pixel data is encoded on disk.
alpha_size: u8Bits 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: PixelFormatDXT sub-format, relevant only when
color_encoding == ColorEncoding::Dxt.
width: u32Width of the base (level-0) mipmap in pixels.
height: u32Height of the base (level-0) mipmap in pixels.
Implementations§
Source§impl BlpFile
impl BlpFile
Sourcepub fn open(path: impl AsRef<Path>) -> Result<Self, BlpError>
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.
Sourcepub fn from_bytes(data: Vec<u8>) -> Result<Self, BlpError>
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
| Error | Cause |
|---|---|
BlpError::Io | Buffer is too short to contain a valid header |
BlpError::InvalidMagic | First 4 bytes are not BLP0, BLP1, or BLP2 |
BlpError::InvalidFormatVersion | BLP2 format version field ≠ 1 |
BlpError::UnsupportedEncoding | Color encoding byte is unrecognised |
BlpError::DataTooShort | JPEG header size field exceeds 64 KiB |
Source§impl BlpFile
impl BlpFile
Sourcepub fn mipmap_count(&self) -> usize
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.
Sourcepub fn get_pixels(
&self,
mipmap_level: u32,
) -> Result<(Vec<u8>, u32, u32), BlpError>
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
| Error | Cause |
|---|---|
BlpError::NoMipmaps | mipmap_count() == 0 |
BlpError::ImageTooLarge | width × height × 4 overflows or exceeds 256 MiB |
BlpError::OutOfBounds | Mipmap offset/size points outside the file buffer |
BlpError::DataTooShort | Mipmap slice is too small for the declared dimensions |
BlpError::JpegDecode | JPEG data is invalid or corrupt |