more Shapes code

png-branch
an 2019-02-10 23:29:12 -05:00
parent 5a569acd7d
commit 627e9969a3
2 changed files with 199 additions and 118 deletions

View File

@ -55,13 +55,13 @@ pub trait Image
pub trait Color
{
type Output: PrimInt;
type Comp: PrimInt;
const MAX: u32;
fn r(&self) -> Self::Output;
fn g(&self) -> Self::Output;
fn b(&self) -> Self::Output;
fn a(&self) -> Self::Output;
fn r(&self) -> Self::Comp;
fn g(&self) -> Self::Comp;
fn b(&self) -> Self::Comp;
fn a(&self) -> Self::Comp;
}
impl Image16
@ -118,7 +118,7 @@ impl Color16
impl Color for Color16
{
type Output = u16;
type Comp = u16;
const MAX: u32 = u16::max_value() as u32;
fn r(&self) -> u16 {self.0}
@ -137,7 +137,7 @@ impl Color8
impl Color for Color8
{
type Output = u8;
type Comp = u8;
const MAX: u32 = u8::max_value() as u32;
fn r(&self) -> u8 {self.0}

View File

@ -1,49 +1,65 @@
//! Marathon Shapes format handling.
use crate::durandal::{bin::*, err::*, fx32::*, /*image::*,*/ text::*};
use crate::durandal::{bin::*, err::*, fx32::*, image::*, text::*};
use bitflags::bitflags;
fn color(b: &[u8]) -> ResultS<()>
fn color(b: &[u8]) -> ResultS<(usize, ColorShp)>
{
let lum = b[0];
let ind = b[1];
let r = b.c_u16b(2)?;
let g = b.c_u16b(4)?;
let b = b.c_u16b(6)?;
// col = Color::from_rgb16(r, g, b);
let lum = match lum {
128 => true,
0 => false,
_ => {
return Err(err_msg("invalid flag in color"));
}
};
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")),
}?;
dbg!(lum);
dbg!(ind);
dbg!(r);
dbg!(g);
dbg!(b);
Ok(())
Ok((i, ColorShp::Opaque{r, g, b, l}))
}
fn clut_collection(b: &[u8], clr_num: usize, clu_num: usize)
-> ResultS<Vec<Vec<ColorShp>>>
{
let mut tables = vec![vec![ColorShp::Translucent; clr_num]; clu_num];
let mut p = 0;
for i in 0..clu_num {
let clut = &mut tables[i];
for j in 0..clr_num {
let (i, cr) = color(c_data(b, p..)?)?;
if i >= clr_num {
bail!("invalid index");
}
clut[i] = cr;
p += 8;
}
}
Ok(tables)
}
/*
fn frame(b: &[u8]) -> ResultS<()>
{
let flags = b.c_u16b(0)?;
let minlight = b.c_u32b(2)?;
let bmp_ind = b.c_u16b(6)?;
// orig_x = b.c_i16b(8)?;
// orig_y = b.c_i16b(10)?;
// key_x = b.c_i16b(12)?;
// key_y = b.c_i16b(14)?;
let wrl_l = b.c_i16b(16)?;
let wrl_r = b.c_i16b(18)?;
let wrl_t = b.c_i16b(20)?;
let wrl_b = b.c_i16b(22)?;
let wrl_x = b.c_i16b(24)?;
let wrl_y = b.c_i16b(26)?;
let flags = FrameFlags::from_bits(flags).ok_or_else(bad_flag)?;
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")?;
let minlight = Fx32::from_bits(minlight);
dbg!(flags);
@ -61,20 +77,21 @@ fn frame(b: &[u8]) -> ResultS<()>
fn sequence(b: &[u8]) -> ResultS<()>
{
// sq_type = b.c_u16b(0)?;
// flags = b.c_u16b(2)?;
let name = mac_roman_conv(pascal_str(&b[4..38])?);
let v_type = b.c_u16b(38)?;
let frames = b.c_u16b(40)?;
let ticks = b.c_u16b(42)?;
let key = b.c_u16b(44)?;
let xfer = b.c_u16b(46)?;
let xfer_pd = b.c_u16b(48)?;
let snd_beg = b.c_u16b(50)?;
let snd_key = b.c_u16b(52)?;
let snd_end = b.c_u16b(54)?;
// xform = b.c_u16b(56)?;
let loop_f = b.c_u16b(58)?;
// 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")?);
let snd_beg = ObjID::from_repr(snd_beg);
let snd_key = ObjID::from_repr(snd_key);
let snd_end = ObjID::from_repr(snd_end);
@ -93,93 +110,157 @@ fn sequence(b: &[u8]) -> ResultS<()>
Ok(())
}
*/
fn collection(b: &[u8]) -> ResultS<()>
fn collection(b: &[u8]) -> ResultS<Collection>
{
let version = b.c_u16b(0)?;
let cl_type = b.c_u16b(2)?;
// flags = b.c_u16b(4)?;
let colors = b.c_u16b(6)? as usize;
let clu_num = b.c_u16b(8)? as usize;
let clu_ofs = b.c_u32b(10)? as usize;
let seq_num = b.c_u16b(14)? as usize;
let seq_ofs = b.c_u32b(16)? as usize;
let frm_num = b.c_u16b(20)? as usize;
let frm_ofs = b.c_u32b(22)? as usize;
let bmp_num = b.c_u16b(26)? as usize;
let bmp_ofs = b.c_u32b(28)? as usize;
// xform = b.c_i16b(30)?;
let size = b.c_u32b(32)? as usize;
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;
let clu_num = c_u16b(b, 8)? as usize;
let clu_ofs = c_u32b(b, 10)? as usize;
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)?;
let size = c_u32b(b, 32)? as usize;
let cl_type = CollectionType::from_repr(cl_type)?;
dbg!(version);
dbg!(cl_type);
dbg!(colors);
dbg!(clu_num);
dbg!(clu_ofs);
dbg!(seq_num);
dbg!(seq_ofs);
dbg!(frm_num);
dbg!(frm_ofs);
dbg!(bmp_num);
dbg!(bmp_ofs);
dbg!(size);
eprintln!("[end of collection]");
if version != 3 {
return Err(err_msg("invalid collection version number"));
}
for i in 0..clu_num {
for j in 0..colors {
let p = clu_ofs + 8 * j + i * 8 * colors;
color(&b[p..])?;
}
bail!("invalid collection version number");
}
/*
for i in 0..frm_num {
let p = b.c_u32b(frm_ofs + 4 * i)? as usize;
frame(&b[p..])?;
let p = c_u32b(b, frm_ofs + 4 * i)? as usize;
frame(c_data(b, p..)?)?;
}
for i in 0..seq_num {
let p = b.c_u32b(seq_ofs + 4 * i)? as usize;
sequence(&b[p..])?;
let p = c_u32b(b, seq_ofs + 4 * i)? as usize;
sequence(c_data(b, p..)?)?;
}
*/
Ok(())
let tables = clut_collection(&b[clu_ofs..], clr_num, clu_num)?;
Ok(Collection{clr_num, tables, bitmaps: Vec::new()})
}
pub fn testfn_replaceme(b: &[u8]) -> ResultS<()>
{
for i in 0..32 {
let p = 32 * i;
let status = b.c_u16b(p + 0)?;
let flags = b.c_u16b(p + 2)?;
let offset_lo = b.c_u32b(p + 4)? as usize;
let length_lo = b.c_u32b(p + 8)? as usize;
let offset_hi = b.c_u32b(p + 12)? as usize;
let length_hi = b.c_u32b(p + 16)? as usize;
// status = c_u16b(b, p + 0)?;
// flags = c_u16b(b, p + 2)?;
let offset_lo = c_u32b(b, p + 4)? as usize;
let length_lo = c_u32b(b, p + 8)? as usize;
let offset_hi = c_u32b(b, p + 12)? as usize;
let length_hi = c_u32b(b, p + 16)? as usize;
dbg!(i);
dbg!(status);
dbg!(flags);
dbg!(offset_lo);
dbg!(length_lo);
dbg!(offset_hi);
dbg!(length_hi);
let collections = (
if offset_lo != u32::max_value() as usize {
Some(collection(c_data(b, offset_lo..offset_lo + length_lo)?)?)
} else {
None
},
if offset_hi != u32::max_value() as usize {
Some(collection(c_data(b, offset_hi..offset_hi + length_hi)?)?)
} else {
None
}
);
if offset_lo != u32::max_value() as usize {
collection(&b[offset_lo..offset_lo + length_lo])?;
}
if offset_hi != u32::max_value() as usize {
collection(&b[offset_hi..offset_hi + length_hi])?;
}
dbg!(collections);
}
Ok(())
}
impl Image for ImageShp<'_>
{
type Output = ColorShp;
fn w(&self) -> usize {self.w}
fn h(&self) -> usize {self.h}
fn cr_at(&self, x: usize, y: usize) -> Option<&ColorShp>
{
self.clut.get(*self.cr.get(x + y * self.w)? as usize)
}
}
impl Color for ColorShp
{
type Comp = u16;
const MAX: u32 = u16::max_value() as u32;
fn r(&self) -> u16
{
match self {
&ColorShp::Translucent => 0,
&ColorShp::Opaque{r, ..} => r,
}
}
fn g(&self) -> u16
{
match self {
&ColorShp::Translucent => 0,
&ColorShp::Opaque{g, ..} => g,
}
}
fn b(&self) -> u16
{
match self {
&ColorShp::Translucent => 0,
&ColorShp::Opaque{b, ..} => b,
}
}
fn a(&self) -> u16
{
match self {
&ColorShp::Translucent => 0,
&ColorShp::Opaque{..} => 255,
}
}
}
#[derive(Clone, Debug)]
enum ColorShp
{
Translucent,
Opaque {
r: u16,
g: u16,
b: u16,
l: bool,
}
}
#[derive(Debug)]
struct ImageShp<'a>
{
w: usize,
h: usize,
cr: Vec<u8>,
clut: &'a [ColorShp],
}
#[derive(Debug)]
struct Collection<'a>
{
clr_num: usize,
tables: Vec<Vec<ColorShp>>,
bitmaps: Vec<ImageShp<'a>>,
}
bitflags! {
pub struct FrameFlags: u16
{