Files
vectorcraft/crates/render/src/encode/tests_tga.rs
T
bflatasticandClaude Opus 5.5 b578c32f4d M4.69: Targa export at 16, 24 or 32 bits per pixel
document.export {format: "tga"} writes uncompressed true-colour Targa files
through the raster export path (resolution, background, anti-aliasing,
artboard ranges): depth 16 (5-5-5 with a one-bit alpha), 24 (flattened on
white) or 32 (8-bit alpha), rows bottom-up, with the version 2 footer.
Export As offers it with the tgaOptions dialog (Depth).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 15:24:51 +07:00

31 lines
1.4 KiB
Rust

//! Targa export: depths, alpha and the footer.
use super::on_white;
use super::tests_bmp::{art, decode, u16_at};
use super::tga::{self, TgaOptions};
#[test]
fn targa_depths_alpha_and_footer() {
let (w, h) = (7, 3);
let px = art(w, h);
for (depth, alpha_bits) in [(16, 1), (24, 0), (32, 8)] {
let file = tga::encode(&px, w, h, &TgaOptions { depth }).unwrap();
assert_eq!((file[2], file[16], file[17]), (2, depth, alpha_bits), "true colour, the depth byte, alpha bits");
assert_eq!((u16_at(&file, 12), u16_at(&file, 14)), (7, 3));
assert_eq!(file.len(), 18 + (w * h) as usize * usize::from(depth / 8) + 26);
assert!(file.ends_with(b"TRUEVISION-XFILE.\0"));
let img = decode(&file, image::ImageFormat::Tga);
assert_eq!(img.dimensions(), (w, h));
let want = on_white(px[(2 * w as usize + 3) * 4..][..4].try_into().unwrap());
let got = img.get_pixel(3, 2).0;
assert!(got[..3].iter().zip(want).all(|(a, b)| a.abs_diff(b) <= 8), "depth {depth}: {got:?} vs {want:?}");
match depth {
32 => assert_eq!((img.get_pixel(0, 0).0[3], img.get_pixel(1, 0).0[3]), (0, 128), "8-bit alpha"),
24 => assert_eq!(img.get_pixel(0, 0).0, [255; 4], "flattened on white"),
_ => {}
}
}
assert!(tga::encode(&px, w, h, &TgaOptions { depth: 8 }).is_err());
assert!(tga::encode(&px, 70_000, 1, &TgaOptions::default()).is_err(), "too wide");
}