add write_tga
parent
5856d2c1c9
commit
b41525006e
|
@ -46,6 +46,30 @@ pub fn write_ppm(out: &mut impl io::Write, im: &impl Image) -> ResultS<()>
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Writes a TGA file from an image.
|
||||
pub fn write_tga(out: &mut impl io::Write, im: &impl Image) -> ResultS<()>
|
||||
{
|
||||
out.write(&[0, 0, 2])?; // id len, color map type, image type
|
||||
out.write(&[0, 0, 0, 0, 0])?; // color map spec
|
||||
out.write(&[0, 0])?; // x origin
|
||||
out.write(&[0, 0])?; // y origin
|
||||
out.write(&(im.w() as u16).to_le_bytes())?; // width
|
||||
out.write(&(im.h() as u16).to_le_bytes())?; // height
|
||||
out.write(&[32, 0])?; // depth, descriptor
|
||||
|
||||
for y in (0..im.h()).rev() {
|
||||
for x in 0..im.w() {
|
||||
let cr = im.index(x, y);
|
||||
out.write(&[(cr.b() >> 8) as u8,
|
||||
(cr.g() >> 8) as u8,
|
||||
(cr.r() >> 8) as u8,
|
||||
(cr.a() >> 8) as u8])?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub trait Image
|
||||
{
|
||||
type Output: Color;
|
||||
|
@ -72,16 +96,6 @@ pub trait Color
|
|||
fn a(&self) -> u16;
|
||||
}
|
||||
|
||||
impl<T: Color> std::ops::Index<(usize, usize)> for Image<Output = T>
|
||||
{
|
||||
type Output = T;
|
||||
|
||||
fn index(&self, (x, y): (usize, usize)) -> &Self::Output
|
||||
{
|
||||
<Self as Image>::index(self, x, y)
|
||||
}
|
||||
}
|
||||
|
||||
impl Image16
|
||||
{
|
||||
/// Creates a new Image16.
|
||||
|
|
|
@ -51,11 +51,11 @@ fn bitmap<'a>(b: &[u8], clut: &'a [ColorShp]) -> ResultS<ImageShp<'a>>
|
|||
let height = c_u16b(b, 2)? as usize;
|
||||
let pitch = c_u16b(b, 4)? as usize;
|
||||
let flags = c_u16b(b, 6)?;
|
||||
let bpp = c_u16b(b, 8)?;
|
||||
let depth = c_u16b(b, 8)?;
|
||||
let flags = ok!(BmpFlags::from_bits(flags), "bad BmpFlags")?;
|
||||
let alpha = flags.contains(BmpFlags::Transparent);
|
||||
|
||||
if bpp != 8 {
|
||||
if depth != 8 {
|
||||
bail!("invalid bit depth (should always be 8)");
|
||||
}
|
||||
|
||||
|
@ -68,12 +68,11 @@ fn bitmap<'a>(b: &[u8], clut: &'a [ColorShp]) -> ResultS<ImageShp<'a>>
|
|||
}
|
||||
|
||||
let mut im = ImageShp::new(width, height, clut, alpha);
|
||||
// let mut p = 30 + if flags.contains(BmpFlags::ColumnMajor) {
|
||||
// 4 * width
|
||||
// } else {
|
||||
// 4 * height
|
||||
// };
|
||||
let mut p = 30 + 4 * height;
|
||||
let mut p = 30 + if flags.contains(BmpFlags::ColumnMajor) {
|
||||
4 * width
|
||||
} else {
|
||||
4 * height
|
||||
};
|
||||
|
||||
for _ in 0..height {
|
||||
for _ in 0..width {
|
||||
|
@ -99,9 +98,9 @@ fn bitmap_collection<'a>(b: &[u8],
|
|||
let bmp = bitmap(c_data(b, ofs..)?, clut)?;
|
||||
|
||||
use std::{fs, io};
|
||||
let out = fs::File::create(&format!("out/shape{}.ppm", ofs))?;
|
||||
let out = fs::File::create(&format!("out/shape{}.tga", ofs))?;
|
||||
let mut out = io::BufWriter::new(out);
|
||||
write_ppm(&mut out, &bmp)?;
|
||||
write_tga(&mut out, &bmp)?;
|
||||
|
||||
p += 4;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue