Skip to main content

wow_sharedmedia/converter/
blp.rs

1//! BLP texture format reading (BLP2 → DynamicImage).
2
3use crate::Error;
4
5/// Read a BLP2 file and convert to a DynamicImage.
6pub fn read_blp(path: &std::path::Path) -> Result<image::DynamicImage, Error> {
7	let data = std::fs::read(path).map_err(|e| Error::Io {
8		source: e,
9		path: path.to_path_buf(),
10	})?;
11
12	let blp_image =
13		wow_blp::parser::parse_blp(&data).map_err(|e| Error::ImageConversion(format!("Failed to parse BLP: {e}")))?;
14
15	let decoded = wow_blp::convert::blp_to_image(&blp_image, 0)
16		.map_err(|e| Error::ImageConversion(format!("Failed to decode BLP: {e}")))?;
17
18	Ok(decoded)
19}
20
21#[cfg(test)]
22mod tests {
23	use super::*;
24	use tempfile::TempDir;
25
26	#[test]
27	fn test_read_blp_missing_file() {
28		let dir = TempDir::new().unwrap();
29		let path = dir.path().join("missing.blp");
30
31		let result = read_blp(&path);
32		assert!(result.is_err());
33		match result.unwrap_err() {
34			Error::Io { path: err_path, .. } => assert_eq!(err_path, path),
35			other => panic!("Expected Io error, got: {other}"),
36		}
37	}
38
39	#[test]
40	fn test_read_blp_invalid_payload_maps_to_image_conversion() {
41		let dir = TempDir::new().unwrap();
42		let path = dir.path().join("invalid.blp");
43		std::fs::write(&path, b"definitely not a real blp file").unwrap();
44
45		let result = read_blp(&path);
46		assert!(result.is_err());
47		match result.unwrap_err() {
48			Error::ImageConversion(msg) => assert!(msg.contains("Failed to parse BLP")),
49			other => panic!("Expected ImageConversion, got: {other}"),
50		}
51	}
52}