Skip to main content

vtf/
lib.rs

1pub mod header;
2pub mod image;
3pub mod resources;
4mod utils;
5pub mod vtf;
6
7pub use crate::image::ImageFormat;
8use crate::vtf::VTF;
9use ::image::DynamicImage;
10use num_enum::TryFromPrimitiveError;
11use thiserror::Error;
12
13#[derive(Debug, Error)]
14pub enum Error {
15    #[error("IO error: {0}")]
16    Io(#[from] std::io::Error),
17    #[error("File does not have a valid vtf signature")]
18    InvalidSignature,
19    #[error("File does not have a valid vtf image format: {0}")]
20    InvalidImageFormat(i16),
21    #[error("Error manipulating image data: {0}")]
22    Image(#[from] ::image::ImageError),
23    #[error("Decoding {0} images is not supported")]
24    UnsupportedImageFormat(ImageFormat),
25    #[error("Decoded image data does not have the expected size")]
26    InvalidImageData,
27    #[error("Image size needs to be a power of 2 and below 2^16")]
28    InvalidImageSize,
29    #[error("Encoding {0} images is not supported")]
30    UnsupportedEncodeImageFormat(ImageFormat),
31}
32
33impl From<TryFromPrimitiveError<image::ImageFormat>> for Error {
34    fn from(err: TryFromPrimitiveError<image::ImageFormat>) -> Self {
35        Error::InvalidImageFormat(err.number)
36    }
37}
38
39pub fn from_bytes(bytes: &Vec<u8>) -> Result<VTF, Error> {
40    VTF::read(bytes)
41}
42
43pub fn create(image: DynamicImage, image_format: ImageFormat) -> Result<Vec<u8>, Error> {
44    VTF::create(image, image_format)
45}