Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for VTF #28

Merged
merged 2 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ imagesize = "0.12"
* QOI
* TGA
* TIFF
* VTF
* WEBP

If you have a format you think should be added, feel free to create an issue.
Expand Down
5 changes: 5 additions & 0 deletions src/formats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod psd;
pub mod qoi;
pub mod tga;
pub mod tiff;
pub mod vtf;
pub mod webp;

use crate::{ImageError, ImageResult, ImageType};
Expand Down Expand Up @@ -106,6 +107,10 @@ pub fn image_type<R: BufRead + Seek>(reader: &mut R) -> ImageResult<ImageType> {
return Ok(ImageType::Farbfeld);
}

if vtf::matches(&header) {
return Ok(ImageType::Vtf);
}

// Keep TGA last because it has the highest probability of false positives
if tga::matches(&header, reader) {
return Ok(ImageType::Tga);
Expand Down
17 changes: 17 additions & 0 deletions src/formats/vtf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::util::*;
use crate::{ImageResult, ImageSize};

use std::io::{BufRead, Seek, SeekFrom};

pub fn size<R: BufRead + Seek>(reader: &mut R) -> ImageResult<ImageSize> {
reader.seek(SeekFrom::Start(16))?;

Ok(ImageSize {
width: read_u16(reader, &Endian::Little)? as usize,
height: read_u16(reader, &Endian::Little)? as usize,
})
}

pub fn matches(header: &[u8]) -> bool {
header.starts_with(b"VTF\0")
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub enum ImageType {
Qoi,
Tga,
Tiff,
Vtf,
Webp,
}

Expand Down Expand Up @@ -239,6 +240,7 @@ fn dispatch_header<R: BufRead + Seek>(reader: &mut R) -> ImageResult<ImageSize>
ImageType::Qoi => qoi::size(reader),
ImageType::Tga => tga::size(reader),
ImageType::Tiff => tiff::size(reader),
ImageType::Vtf => vtf::size(reader),
ImageType::Webp => webp::size(reader),
}
}
Binary file added tests/images/vtf/test.vtf
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/vtf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[cfg(test)]
use imagesize::{size, ImageSize};

#[test]
fn vtf_test() {
let dim = size("tests/images/vtf/test.vtf").unwrap();
assert_eq!(dim, ImageSize { width: 512, height: 256 });
}