Maraiah/src/marathon/shp.rs

395 lines
8.6 KiB
Rust
Raw Normal View History

2019-02-09 11:02:23 -08:00
//! Marathon Shapes format handling.
2019-02-10 20:29:12 -08:00
use crate::durandal::{bin::*, err::*, fx32::*, image::*, text::*};
2019-02-09 11:02:23 -08:00
use bitflags::bitflags;
2019-02-10 20:29:12 -08:00
fn color(b: &[u8]) -> ResultS<(usize, ColorShp)>
2019-02-09 11:02:23 -08:00
{
2019-02-10 20:29:12 -08:00
let l = c_byte(b, 0)?;
let i = c_byte(b, 1)? as usize;
let r = c_u16b(b, 2)?;
let g = c_u16b(b, 4)?;
let b = c_u16b(b, 6)?;
let l = match l {
128 => Ok(true),
0 => Ok(false),
_ => Err(err_msg("invalid flag in color")),
}?;
2019-02-09 11:02:23 -08:00
2019-02-10 20:29:12 -08:00
Ok((i, ColorShp::Opaque{r, g, b, l}))
}
2019-02-09 11:02:23 -08:00
2019-02-12 02:31:20 -08:00
fn tab_coll(b: &[u8],
tab_num: usize,
clr_num: usize)
-> ResultS<Vec<Vec<ColorShp>>>
2019-02-10 20:29:12 -08:00
{
2019-02-12 02:31:20 -08:00
let mut tabs = vec![vec![ColorShp::Translucent; clr_num]; tab_num];
let mut p = 0;
2019-02-10 20:29:12 -08:00
2019-02-12 02:31:20 -08:00
for i in 0..tab_num {
let clut = &mut tabs[i];
2019-02-10 20:29:12 -08:00
2019-02-12 02:31:20 -08:00
for _ in 0..clr_num {
2019-02-10 20:29:12 -08:00
let (i, cr) = color(c_data(b, p..)?)?;
2019-02-12 02:31:20 -08:00
*ok!(clut.get_mut(i), "invalid index")? = cr;
2019-02-10 20:29:12 -08:00
p += 8;
}
}
2019-02-12 02:31:20 -08:00
Ok(tabs)
2019-02-09 11:02:23 -08:00
}
2019-02-12 02:31:20 -08:00
fn bitmap(b: &[u8]) -> ResultS<Bitmap>
2019-02-11 03:29:01 -08:00
{
let width = c_u16b(b, 0)? as usize;
let height = c_u16b(b, 2)? as usize;
2019-02-12 02:31:20 -08:00
let compr = c_u16b(b, 4)? == u16::max_value();
2019-02-11 03:29:01 -08:00
let flags = c_u16b(b, 6)?;
2019-02-11 07:33:10 -08:00
let depth = c_u16b(b, 8)?;
2019-02-11 03:29:01 -08:00
let flags = ok!(BmpFlags::from_bits(flags), "bad BmpFlags")?;
let alpha = flags.contains(BmpFlags::Transparent);
2019-02-12 02:31:20 -08:00
let cmajr = flags.contains(BmpFlags::ColumnMajor);
2019-02-11 03:29:01 -08:00
2019-02-11 07:33:10 -08:00
if depth != 8 {
2019-02-11 03:29:01 -08:00
bail!("invalid bit depth (should always be 8)");
}
2019-02-12 02:31:20 -08:00
let mut bmp = Bitmap::new(width, height, alpha, cmajr);
let mut p = 30 + if cmajr {4 * width} else {4 * height};
2019-02-11 03:29:01 -08:00
2019-02-12 02:31:20 -08:00
let scanlines = if cmajr {width} else {height};
let pitch = if cmajr {height} else {width};
2019-02-11 03:29:01 -08:00
2019-02-12 02:31:20 -08:00
if compr {
// compressed scanlines (transparency RLE)
for _ in 0..scanlines {
let fst = c_u16b(b, p)? as usize;
let lst = c_u16b(b, p + 2)? as usize;
if lst < fst || fst > pitch || lst > pitch {
bail!("invalid compressed scanline");
}
p += 4;
for _ in 0..fst {
bmp.cr.push(0);
}
2019-02-11 03:29:01 -08:00
2019-02-12 02:31:20 -08:00
let end = lst - fst;
bmp.cr.extend_from_slice(c_data(b, p..p + end)?);
for _ in lst..pitch {
bmp.cr.push(0);
}
p += end;
2019-02-11 03:29:01 -08:00
}
2019-02-12 02:31:20 -08:00
} else {
// simple copy
bmp.cr.extend_from_slice(c_data(b, p..p + width * height)?);
2019-02-11 03:29:01 -08:00
}
2019-02-12 02:31:20 -08:00
Ok(bmp)
2019-02-11 03:29:01 -08:00
}
2019-02-12 02:31:20 -08:00
fn bmp_coll(b: &[u8], bmp_ofs: usize, bmp_num: usize) -> ResultS<Vec<Bitmap>>
2019-02-11 03:29:01 -08:00
{
2019-02-12 02:31:20 -08:00
let mut bmps = Vec::with_capacity(bmp_num);
let mut p = bmp_ofs;
2019-02-11 03:29:01 -08:00
for _ in 0..bmp_num {
let ofs = c_u32b(b, p)? as usize;
2019-02-12 02:31:20 -08:00
bmps.push(bitmap(c_data(b, ofs..)?)?);
2019-02-11 03:29:01 -08:00
p += 4;
}
2019-02-12 02:31:20 -08:00
Ok(bmps)
2019-02-11 03:29:01 -08:00
}
2019-02-10 20:29:12 -08:00
/*
2019-02-09 11:02:23 -08:00
fn frame(b: &[u8]) -> ResultS<()>
{
2019-02-10 20:29:12 -08:00
let flags = c_u16b(b, 0)?;
let minlight = c_u32b(b, 2)?;
let bmp_ind = c_u16b(b, 6)?;
// orig_x = c_i16b(b, 8)?;
// orig_y = c_i16b(b, 10)?;
// key_x = c_i16b(b, 12)?;
// key_y = c_i16b(b, 14)?;
let wrl_l = c_i16b(b, 16)?;
let wrl_r = c_i16b(b, 18)?;
let wrl_t = c_i16b(b, 20)?;
let wrl_b = c_i16b(b, 22)?;
let wrl_x = c_i16b(b, 24)?;
let wrl_y = c_i16b(b, 26)?;
let flags = ok!(FrameFlags::from_bits(flags), "bad flag")?;
2019-02-09 11:02:23 -08:00
let minlight = Fx32::from_bits(minlight);
dbg!(flags);
dbg!(minlight);
dbg!(bmp_ind);
dbg!(wrl_l);
dbg!(wrl_r);
dbg!(wrl_t);
dbg!(wrl_b);
dbg!(wrl_x);
dbg!(wrl_y);
Ok(())
}
fn sequence(b: &[u8]) -> ResultS<()>
{
2019-02-10 20:29:12 -08:00
// sq_type = c_u16b(b, 0)?;
// flags = c_u16b(b, 2)?;
let name = c_data(b, 4..38)?;
let v_type = c_u16b(b, 38)?;
let frames = c_u16b(b, 40)?;
let ticks = c_u16b(b, 42)?;
let key = c_u16b(b, 44)?;
let xfer = c_u16b(b, 46)?;
let xfer_pd = c_u16b(b, 48)?;
let snd_beg = c_u16b(b, 50)?;
let snd_key = c_u16b(b, 52)?;
let snd_end = c_u16b(b, 54)?;
// xform = c_u16b(b, 56)?;
let loop_f = c_u16b(b, 58)?;
let name = mac_roman_conv(ok!(pascal_str(name), "bad string")?);
2019-02-09 11:02:23 -08:00
let snd_beg = ObjID::from_repr(snd_beg);
let snd_key = ObjID::from_repr(snd_key);
let snd_end = ObjID::from_repr(snd_end);
dbg!(name);
dbg!(v_type);
dbg!(frames);
dbg!(ticks);
dbg!(key);
dbg!(xfer);
dbg!(xfer_pd);
dbg!(snd_beg);
dbg!(snd_key);
dbg!(snd_end);
dbg!(loop_f);
Ok(())
}
2019-02-10 20:29:12 -08:00
*/
2019-02-09 11:02:23 -08:00
2019-02-10 20:29:12 -08:00
fn collection(b: &[u8]) -> ResultS<Collection>
2019-02-09 11:02:23 -08:00
{
2019-02-10 20:29:12 -08:00
let version = c_u16b(b, 0)?;
let cl_type = c_u16b(b, 2)?;
// flags = c_u16b(b, 4)?;
let clr_num = c_u16b(b, 6)? as usize;
2019-02-12 02:31:20 -08:00
let tab_num = c_u16b(b, 8)? as usize;
let tab_ofs = c_u32b(b, 10)? as usize;
2019-02-10 20:29:12 -08:00
let seq_num = c_u16b(b, 14)? as usize;
let seq_ofs = c_u32b(b, 16)? as usize;
let frm_num = c_u16b(b, 20)? as usize;
let frm_ofs = c_u32b(b, 22)? as usize;
let bmp_num = c_u16b(b, 26)? as usize;
let bmp_ofs = c_u32b(b, 28)? as usize;
// xform = c_i16b(b, 30)?;
2019-02-12 02:31:20 -08:00
// size = c_u32b(b, 32)? as usize;
2019-02-09 11:02:23 -08:00
let cl_type = CollectionType::from_repr(cl_type)?;
if version != 3 {
2019-02-12 02:31:20 -08:00
bail!("invalid collection definition");
2019-02-09 11:02:23 -08:00
}
2019-02-12 02:31:20 -08:00
let tabs = tab_coll(c_data(b, tab_ofs..)?, tab_num, clr_num)?;
let bmps = bmp_coll(b, bmp_ofs, bmp_num)?;
// frms = frm_coll(c_data(b, frm_ofs..)?, frm_num)?;
// seqs = seq_coll(c_data(b, seq_ofs..)?, seq_num)?;
2019-02-09 11:02:23 -08:00
2019-02-12 02:31:20 -08:00
Ok(Collection{tabs, bmps /*, frms, seqs*/})
}
2019-02-09 11:02:23 -08:00
2019-02-12 02:31:20 -08:00
pub fn testfn_dump_bitmaps(c: &Collection, i: usize) -> ResultS<()>
{
use std::{fs, io};
2019-02-10 20:29:12 -08:00
2019-02-12 02:31:20 -08:00
// TODO: output with all CLUTs too
for (j, bmp) in c.bmps.iter().enumerate() {
let out = fs::File::create(&format!("out/shape{}_{}.tga", i, j))?;
let mut out = io::BufWriter::new(out);
write_tga(&mut out, &ImageShp{bmp: bmp.clone(), clut: &c.tabs[0]})?;
}
Ok(())
2019-02-09 11:02:23 -08:00
}
2019-02-12 02:31:20 -08:00
pub fn testfn_replaceme(b: &[u8]) -> ResultS<Vec<CollectionDef>>
2019-02-09 11:02:23 -08:00
{
2019-02-12 02:31:20 -08:00
let mut cl = Vec::with_capacity(32);
let mut p = 0;
for _ in 0..32 {
// status = c_u16b(b, p + 0)?;
// flags = c_u16b(b, p + 2)?;
let lo_ofs = c_u32b(b, p + 4)? as usize;
let lo_len = c_u32b(b, p + 8)? as usize;
let hi_ofs = c_u32b(b, p + 12)? as usize;
let hi_len = c_u32b(b, p + 16)? as usize;
let c_lo = if lo_ofs != u32::max_value() as usize {
Some(collection(c_data(b, lo_ofs..lo_ofs + lo_len)?)?)
} else {
None
};
let c_hi = if hi_ofs != u32::max_value() as usize {
Some(collection(c_data(b, hi_ofs..hi_ofs + hi_len)?)?)
} else {
None
};
cl.push((c_lo, c_hi));
p += 32;
2019-02-10 20:29:12 -08:00
}
2019-02-12 02:31:20 -08:00
Ok(cl)
2019-02-10 20:29:12 -08:00
}
2019-02-12 02:31:20 -08:00
impl Bitmap
2019-02-11 03:29:01 -08:00
{
2019-02-12 02:31:20 -08:00
fn new(w: usize, h: usize, alpha: bool, cmajr: bool) -> Self
2019-02-11 03:29:01 -08:00
{
2019-02-12 02:31:20 -08:00
Self{w, h, alpha, cmajr, cr: Vec::with_capacity(w * h)}
2019-02-11 03:29:01 -08:00
}
}
2019-02-10 20:29:12 -08:00
impl Image for ImageShp<'_>
{
type Output = ColorShp;
2019-02-12 02:31:20 -08:00
fn w(&self) -> usize {self.bmp.w}
fn h(&self) -> usize {self.bmp.h}
2019-02-10 20:29:12 -08:00
2019-02-11 05:44:20 -08:00
fn index(&self, x: usize, y: usize) -> &Self::Output
2019-02-10 20:29:12 -08:00
{
2019-02-11 03:29:01 -08:00
static TRANSLUCENT_COLOR: ColorShp = ColorShp::Translucent;
2019-02-12 02:31:20 -08:00
let cr = if self.bmp.cmajr {
self.bmp.cr[y + x * self.bmp.h] as usize
} else {
self.bmp.cr[x + y * self.bmp.w] as usize
};
2019-02-11 03:29:01 -08:00
2019-02-12 02:31:20 -08:00
if self.bmp.alpha && cr == 0 {
2019-02-11 03:29:01 -08:00
&TRANSLUCENT_COLOR
} else {
self.clut.get(cr).unwrap_or(&TRANSLUCENT_COLOR)
}
2019-02-10 20:29:12 -08:00
}
}
impl Color for ColorShp
{
fn r(&self) -> u16
{
2019-02-11 03:29:01 -08:00
match *self {
ColorShp::Translucent => 0,
ColorShp::Opaque{r, ..} => r,
2019-02-09 11:02:23 -08:00
}
2019-02-10 20:29:12 -08:00
}
fn g(&self) -> u16
{
2019-02-11 03:29:01 -08:00
match *self {
ColorShp::Translucent => 0,
ColorShp::Opaque{g, ..} => g,
2019-02-09 11:02:23 -08:00
}
}
2019-02-10 20:29:12 -08:00
fn b(&self) -> u16
{
2019-02-11 03:29:01 -08:00
match *self {
ColorShp::Translucent => 0,
ColorShp::Opaque{b, ..} => b,
2019-02-10 20:29:12 -08:00
}
}
fn a(&self) -> u16
{
2019-02-11 03:29:01 -08:00
match *self {
ColorShp::Translucent => 0,
ColorShp::Opaque{..} => u16::max_value(),
2019-02-10 20:29:12 -08:00
}
}
}
#[derive(Clone, Debug)]
2019-02-12 02:31:20 -08:00
pub enum ColorShp
2019-02-10 20:29:12 -08:00
{
Translucent,
2019-02-10 20:33:38 -08:00
Opaque
{
2019-02-10 20:29:12 -08:00
r: u16,
g: u16,
b: u16,
l: bool,
2019-02-10 20:33:38 -08:00
},
2019-02-10 20:29:12 -08:00
}
2019-02-12 02:31:20 -08:00
#[derive(Clone, Debug)]
pub struct Bitmap
2019-02-10 20:29:12 -08:00
{
2019-02-11 03:29:01 -08:00
w: usize,
h: usize,
cr: Vec<u8>,
alpha: bool,
2019-02-12 02:31:20 -08:00
cmajr: bool,
2019-02-10 20:29:12 -08:00
}
#[derive(Debug)]
2019-02-12 02:31:20 -08:00
pub struct ImageShp<'a>
2019-02-10 20:29:12 -08:00
{
2019-02-12 02:31:20 -08:00
bmp: Bitmap,
clut: &'a [ColorShp],
2019-02-09 11:02:23 -08:00
}
2019-02-12 02:31:20 -08:00
#[derive(Debug)]
pub struct Collection
{
pub tabs: Vec<Vec<ColorShp>>,
pub bmps: Vec<Bitmap>,
}
pub type CollectionDef = (Option<Collection>, Option<Collection>);
2019-02-11 03:29:01 -08:00
bitflags! {
pub struct BmpFlags: u16
{
const ColumnMajor = 0x80_00;
const Transparent = 0x40_00;
}
}
2019-02-09 11:02:23 -08:00
bitflags! {
pub struct FrameFlags: u16
{
const Obscure = 0x20_00;
const FlipY = 0x40_00;
const FlipX = 0x80_00;
}
}
c_enum! {
#[derive(Debug)]
pub enum CollectionType: u16
{
0 => Unused,
1 => Wall,
2 => Object,
3 => Interface,
4 => Scenery,
}
}
// EOF