png-branch
an 2019-02-20 21:33:57 -05:00
parent 717e0c841a
commit b1d673faf3
14 changed files with 121 additions and 181 deletions

View File

@ -92,30 +92,11 @@ macro_rules! read_data {
};
}
pub fn ident(b: &[u8]) -> Ident
{
[b[0], b[1], b[2], b[3]]
}
pub fn u32b(b: &[u8]) -> u32
{
u32::from_be_bytes([b[0], b[1], b[2], b[3]])
}
pub fn u16b(b: &[u8]) -> u16
{
u16::from_be_bytes([b[0], b[1]])
}
pub fn i32b(b: &[u8]) -> i32
{
i32::from_be_bytes([b[0], b[1], b[2], b[3]])
}
pub fn i16b(b: &[u8]) -> i16
{
i16::from_be_bytes([b[0], b[1]])
}
pub fn ident(b: &[u8]) -> Ident {[b[0], b[1], b[2], b[3]]}
pub fn u32b(b: &[u8]) -> u32 {u32::from_be_bytes([b[0], b[1], b[2], b[3]])}
pub fn u16b(b: &[u8]) -> u16 {u16::from_be_bytes([b[0], b[1]])}
pub fn i32b(b: &[u8]) -> i32 {i32::from_be_bytes([b[0], b[1], b[2], b[3]])}
pub fn i16b(b: &[u8]) -> i16 {i16::from_be_bytes([b[0], b[1]])}
pub fn rd_array<T, F>(b: &[u8], read: F) -> ResultS<Vec<T>>
where T: Sized,
@ -134,10 +115,10 @@ pub fn rd_array<T, F>(b: &[u8], read: F) -> ResultS<Vec<T>>
}
pub fn rd_ofstable<T, F>(b: &[u8],
mut p: usize,
num: usize,
read: F)
-> ResultS<Vec<T>>
mut p: usize,
num: usize,
read: F)
-> ResultS<Vec<T>>
where T: Sized,
F: Fn(&[u8]) -> ResultS<T>
{

View File

@ -59,10 +59,7 @@ mod test
#[test]
#[should_panic]
fn c_enum_should_error()
{
TestEnum::from_repr(3).unwrap();
}
fn c_enum_should_error() {TestEnum::from_repr(3).unwrap();}
}
// EOF

View File

@ -19,10 +19,7 @@ macro_rules! bail {
};
}
pub fn err_msg(msg: &'static str) -> Error
{
Error::from(ErrMsg(msg))
}
pub fn err_msg(msg: &'static str) -> Error {Error::from(ErrMsg(msg))}
impl Fail for ReprError {}
impl Fail for ErrMsg {}
@ -45,10 +42,7 @@ impl fmt::Debug for ReprError
impl fmt::Display for ErrMsg
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
f.write_str(self.0)
}
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {f.write_str(self.0)}
}
impl fmt::Debug for ErrMsg

View File

@ -5,9 +5,7 @@ use std::{fmt::{self, Write},
ops};
macro_rules! define_fixed_type {
(
$Type:ident : $IT:ident, $UT:ident, $LT:ident, $FracBits:expr
) => {
($Type:ident : $IT:ident, $UT:ident, $LT:ident, $FracBits:expr) => {
#[derive(Clone, PartialEq, Serialize)]
pub struct $Type($IT);
@ -15,42 +13,15 @@ macro_rules! define_fixed_type {
{
const FRACBITS: $UT = $FracBits;
const FRACMASK: $UT = (1 << Self::FRACBITS) - 1;
const ONE: $IT = 1 << Self::FRACBITS;
const ONE: $IT = 1 << Self::FRACBITS;
pub fn to_bits(&self) -> $UT
{
self.0 as $UT
}
pub fn set_bits(&mut self, bits: $UT)
{
self.0 = bits as $IT
}
pub const fn from_bits(bits: $UT) -> Self
{
$Type(bits as $IT)
}
pub fn integ(&self) -> $LT
{
(self.0 >> Self::FRACBITS) as $LT
}
pub fn fract(&self) -> u16
{
(self.0 as $UT & Self::FRACMASK) as u16
}
pub fn mul_i(&self, n: $LT) -> Self
{
$Type(self.0 * $IT::from(n))
}
pub fn div_i(&self, n: $LT) -> Self
{
$Type(self.0 / $IT::from(n))
}
pub fn to_bits(&self) -> $UT {self.0 as $UT}
pub fn set_bits(&mut self, bits: $UT) {self.0 = bits as $IT}
pub const fn from_bits(bits: $UT) -> Self {$Type(bits as $IT)}
pub fn integ(&self) -> $LT {(self.0 >> Self::FRACBITS) as $LT}
pub fn fract(&self) -> u16 {(self.0 as $UT & Self::FRACMASK) as u16}
pub fn mul_i(&self, n: $LT) -> Self {$Type(self.0 * $IT::from(n))}
pub fn div_i(&self, n: $LT) -> Self {$Type(self.0 / $IT::from(n))}
#[inline]
fn fx_div(x: $IT, y: $IT) -> $IT
@ -67,70 +38,49 @@ macro_rules! define_fixed_type {
impl From<$LT> for $Type
{
fn from(n: $LT) -> Self
{
$Type($IT::from(n) << Self::FRACBITS)
}
fn from(n: $LT) -> Self {$Type($IT::from(n) << Self::FRACBITS)}
}
impl ops::Add for $Type
{
type Output = Self;
fn add(self, o: Self) -> Self
{
$Type(self.0 + o.0)
}
fn add(self, o: Self) -> Self {$Type(self.0 + o.0)}
}
impl ops::Sub for $Type
{
type Output = Self;
fn sub(self, o: Self) -> Self
{
$Type(self.0 - o.0)
}
fn sub(self, o: Self) -> Self {$Type(self.0 - o.0)}
}
impl ops::Mul for $Type
{
type Output = Self;
fn mul(self, o: Self) -> Self
{
$Type(Self::fx_mul(self.0, o.0))
}
fn mul(self, o: Self) -> Self {$Type(Self::fx_mul(self.0, o.0))}
}
impl ops::Div for $Type
{
type Output = Self;
fn div(self, o: Self) -> Self
{
$Type(Self::fx_div(self.0, o.0))
}
fn div(self, o: Self) -> Self {$Type(Self::fx_div(self.0, o.0))}
}
impl ops::Neg for $Type
{
type Output = Self;
fn neg(self) -> Self
{
$Type(-self.0)
}
fn neg(self) -> Self {$Type(-self.0)}
}
impl ops::Not for $Type
{
type Output = Self;
fn not(self) -> Self
{
$Type(!self.0)
}
fn not(self) -> Self {$Type(!self.0)}
}
impl fmt::Display for $Type
@ -183,10 +133,7 @@ fn fixed_basic_ops()
#[test]
#[should_panic]
#[allow(unused_must_use)]
fn fixed_overflow()
{
Fixed::from(i16::max_value()) + 1.into();
}
fn fixed_overflow() {Fixed::from(i16::max_value()) + 1.into();}
#[test]
fn fixed_printing()

View File

@ -142,10 +142,7 @@ impl Image for Image8
impl Color16
{
pub const fn new(r: u16, g: u16, b: u16) -> Color16
{
Color16(r, g, b)
}
pub const fn new(r: u16, g: u16, b: u16) -> Color16 {Color16(r, g, b)}
}
impl Color for Color16
@ -158,10 +155,7 @@ impl Color for Color16
impl Color8
{
pub const fn new(r: u8, g: u8, b: u8) -> Color8
{
Color8(r, g, b)
}
pub const fn new(r: u8, g: u8, b: u8) -> Color8 {Color8(r, g, b)}
}
impl Color for Color8

View File

@ -15,11 +15,11 @@ pub fn write_wav(out: &mut impl io::Write, snd: &impl Sound) -> ResultS<()>
out.write_all(b"WAVE")?;
out.write_all(b"fmt ")?;
out.write_all(&16u32.to_le_bytes())?;
out.write_all(&1u16.to_le_bytes())?; // PCM
out.write_all(&1u16.to_le_bytes())?; // mono
out.write_all(&rate.to_le_bytes())?; // rate
out.write_all(&bps.to_le_bytes())?; // bytes per second
out.write_all(&2u16.to_le_bytes())?; // block alignment
out.write_all(&1u16.to_le_bytes())?; // PCM
out.write_all(&1u16.to_le_bytes())?; // mono
out.write_all(&rate.to_le_bytes())?; // rate
out.write_all(&bps.to_le_bytes())?; // bytes per second
out.write_all(&2u16.to_le_bytes())?; // block alignment
out.write_all(&16u16.to_le_bytes())?; // bits per sample
out.write_all(b"data")?;
out.write_all(&ssize.to_le_bytes())?;
@ -44,10 +44,7 @@ pub trait Sound
fn lp_beg(&self) -> usize;
fn lp_end(&self) -> usize;
fn is_empty(&self) -> bool
{
self.len() == 0
}
fn is_empty(&self) -> bool {self.len() == 0}
fn get(&self, p: usize) -> Option<i16>
{
@ -68,7 +65,8 @@ impl Sound16
}
/// Creates a new Sound16 from an unsigned 8-bit stream.
pub fn new_from_8(rate: u16, lp_beg: usize, lp_end: usize, b: &[u8]) -> Self
pub fn new_from_8(rate: u16, lp_beg: usize, lp_end: usize, b: &[u8])
-> Self
{
let mut snd = Sound16::new(rate, lp_beg, lp_end, b.len());
@ -80,7 +78,8 @@ impl Sound16
}
/// Creates a new Sound16 from a signed 16-bit stream.
pub fn new_from_16(rate: u16, lp_beg: usize, lp_end: usize, b: &[u8]) -> Self
pub fn new_from_16(rate: u16, lp_beg: usize, lp_end: usize, b: &[u8])
-> Self
{
let mut snd = Sound16::new(rate, lp_beg, lp_end, b.len() / 2);
@ -92,10 +91,7 @@ impl Sound16
}
/// Creates a signed 16-bit sample from an unsigned 8-bit sample.
pub fn sample_from_8(sample: u8) -> i16
{
(i16::from(sample) - 0x80) << 8
}
pub fn sample_from_8(sample: u8) -> i16 {(i16::from(sample) - 0x80) << 8}
}
impl Sound for Sound16

View File

@ -201,43 +201,69 @@ fn main() -> ResultS<()>
ap.refer(&mut opt.inputs)
.add_argument("inputs", Collect, "Input files");
arg!("--shp-write-tab", opt.shp_tab, StoreTrue,
arg!("--shp-write-tab",
opt.shp_tab,
StoreTrue,
"shp: Dump all CLUTs as YAML to standard output");
arg!("--shp-dump-bitmaps", opt.shp_bmp, StoreTrue,
arg!("--shp-dump-bitmaps",
opt.shp_bmp,
StoreTrue,
"shp: Dump bitmaps into a folder");
arg!("--shp-dump-more-bitmaps", opt.shp_bmp_all, StoreTrue,
arg!("--shp-dump-more-bitmaps",
opt.shp_bmp_all,
StoreTrue,
"shp: Dump all color variations of each bitmap");
arg!("--shp-write-frm", opt.shp_frm, StoreTrue,
arg!("--shp-write-frm",
opt.shp_frm,
StoreTrue,
"shp: Dump all frames as YAML to standard output");
arg!("--shp-write-seq", opt.shp_seq, StoreTrue,
arg!("--shp-write-seq",
opt.shp_seq,
StoreTrue,
"shp: Dump all sequences as YAML to standard output");
arg!("--snd-write", opt.snd_write, StoreTrue,
arg!("--snd-write",
opt.snd_write,
StoreTrue,
"snd: Dump all sound headers as YAML to standard output");
arg!("--snd-dump", opt.snd_dump, StoreTrue,
arg!("--snd-dump",
opt.snd_dump,
StoreTrue,
"snd: Dump all sounds to WAVE files");
arg!("--wad-dump-all", opt.wad_all, StoreTrue,
arg!("--wad-dump-all",
opt.wad_all,
StoreTrue,
"wad: Dump all chunks into a folder");
arg!("--wad-dump-unknown", opt.wad_unknown, StoreTrue,
arg!("--wad-dump-unknown",
opt.wad_unknown,
StoreTrue,
"wad: Dump all unknown chunks into a folder");
arg!("--wad-write-header", opt.wad_header, StoreTrue,
arg!("--wad-write-header",
opt.wad_header,
StoreTrue,
"wad: Dump header info as YAML to standard output");
arg!("--wad-write-chunks", opt.wad_c_temp, Store,
arg!("--wad-write-chunks",
opt.wad_c_temp,
Store,
"wad: Dump specified chunks in various formats");
arg!("--out-dir", opt.out_dir, Store,
arg!("--out-dir",
opt.out_dir,
Store,
"Sets output directory for dump options");
arg!("--out-debug", opt.out_debug, StoreTrue,
arg!("--out-debug",
opt.out_debug,
StoreTrue,
"Writes debugging output rather than YAML");
ap.parse_args_or_exit();

View File

@ -1,5 +1,4 @@
use crate::{durandal::{bin::*, err::*, fixed::*,
text::mac_roman_conv},
use crate::{durandal::{bin::*, err::*, fixed::*, text::mac_roman_conv},
marathon::xfer::TransferMode};
use bitflags::bitflags;
use serde::Serialize;

View File

@ -78,12 +78,17 @@ fn read_pm_ind(mut im: Image8, b: &[u8], hdr: Header) -> ResultS<Image8>
// RLE compressed 1, 2, 4 or 8 bit colormap indices
for _ in 0..im.h() {
let (d, pp) = read_rle(&b[p..], hdr.pitch, false)?;
let d = if hdr.depth < 8 {expand_data(d, hdr.depth)?} else {d};
let d = if hdr.depth < 8 {
expand_data(d, hdr.depth)?
} else {
d
};
p += pp;
for &idx in &d {
im.cr.push(ok!(clut.get(idx as usize), "invalid index")?.clone());
im.cr
.push(ok!(clut.get(idx as usize), "invalid index")?.clone());
}
}
@ -133,7 +138,10 @@ fn read_pm_32(mut im: Image8, b: &[u8], hdr: Header) -> ResultS<Image8>
{
let mut p = 0;
if hdr.pitch < 8 || hdr.pack_t == PackType::None || hdr.pack_t == PackType::NoPad {
if hdr.pitch < 8 ||
hdr.pack_t == PackType::None ||
hdr.pack_t == PackType::NoPad
{
// uncompressed RGB8 or XRGB8
for _ in 0..im.h() {
for _ in 0..im.w() {
@ -178,10 +186,7 @@ fn read_pm_32(mut im: Image8, b: &[u8], hdr: Header) -> ResultS<Image8>
}
/// Process a CopyBits operation.
fn read_pm_area(im: Image8,
b: &[u8],
pack: bool,
clip: bool)
fn read_pm_area(im: Image8, b: &[u8], pack: bool, clip: bool)
-> ResultS<Image8>
{
let p = if pack {0} else {4};

View File

@ -61,10 +61,10 @@ fn bitmap(b: &[u8]) -> ResultS<Bitmap>
depth = u16[8];
}
let compr = compr == u16::max_value();
let flags = ok!(BmpFlags::from_bits(flags), "bad BmpFlags")?;
let alpha = flags.contains(BmpFlags::Transparent);
let cmajr = flags.contains(BmpFlags::ColumnMajor);
let compr = compr == u16::max_value();
let flags = ok!(BmpFlags::from_bits(flags), "bad BmpFlags")?;
let alpha = flags.contains(BmpFlags::Transparent);
let cmajr = flags.contains(BmpFlags::ColumnMajor);
if depth != 8 {
bail!("invalid bit depth (should always be 8)");
@ -131,14 +131,14 @@ fn frame(b: &[u8]) -> ResultS<Frame>
wrl_y = u16[26];
}
let flags = ok!(FrameFlags::from_bits(flags), "bad flag")?;
let min_lt = Fixed::from_bits(min_lt);
let wrl_l = Unit::from_bits(wrl_l);
let wrl_r = Unit::from_bits(wrl_r);
let wrl_t = Unit::from_bits(wrl_t);
let wrl_b = Unit::from_bits(wrl_b);
let wrl_x = Unit::from_bits(wrl_x);
let wrl_y = Unit::from_bits(wrl_y);
let flags = ok!(FrameFlags::from_bits(flags), "bad flag")?;
let min_lt = Fixed::from_bits(min_lt);
let wrl_l = Unit::from_bits(wrl_l);
let wrl_r = Unit::from_bits(wrl_r);
let wrl_t = Unit::from_bits(wrl_t);
let wrl_b = Unit::from_bits(wrl_b);
let wrl_x = Unit::from_bits(wrl_x);
let wrl_y = Unit::from_bits(wrl_y);
Ok(Frame{flags, min_lt, bmp_ind, wrl_l, wrl_r, wrl_t, wrl_b, wrl_x, wrl_y})
}

View File

@ -33,7 +33,7 @@ fn sound(b: &[u8]) -> ResultS<Sound16>
let stream = &b[63..63 + len * 2];
Ok(Sound16::new_from_16(rate, lp_beg, lp_end, stream))
}
8 => {
8 => {
let stream = &b[63..63 + len];
Ok(Sound16::new_from_8(rate, lp_beg, lp_end, stream))
}

View File

@ -77,8 +77,8 @@ fn get_chunks(b: &[u8], cnksize: usize) -> ResultS<ChunkMap>
size = u32[p + 8] as usize;
}
let beg = p + cnksize;
let end = beg + size;
let beg = p + cnksize;
let end = beg + size;
chunks.insert(iden, &b[beg..end]);

View File

@ -1,9 +1,11 @@
use maraiah::{durandal::{bin, fixed::*}, marathon::map};
use maraiah::{durandal::{bin, fixed::*},
marathon::map};
#[test]
fn read_epnt_must_process_this()
{
assert_eq!(bin::rd_array(INPUT, map::read_epnt).unwrap(), OUTPUT.to_vec());
assert_eq!(bin::rd_array(INPUT, map::read_epnt).unwrap(),
OUTPUT.to_vec());
}
const INPUT: &'static [u8] = include_bytes!("data/epnt.in");

View File

@ -3,15 +3,14 @@ use maraiah::marathon::map;
#[test]
fn read_minf_must_process_map0()
{
assert_eq!(map::read_minf(INPUT).unwrap(), map::Minf{
env_code: 0,
physi_id: 1,
music_id: 1,
msn_flag: map::MsnFlags::Repair,
env_flag: map::EnvFlags::empty(),
ent_flag: map::EntFlags::Solo | map::EntFlags::CoOp,
levelnam: "Waterloo Waterpark".to_string()
});
assert_eq!(map::read_minf(INPUT).unwrap(),
map::Minf{env_code: 0,
physi_id: 1,
music_id: 1,
msn_flag: map::MsnFlags::Repair,
env_flag: map::EnvFlags::empty(),
ent_flag: map::EntFlags::Solo | map::EntFlags::CoOp,
levelnam: "Waterloo Waterpark".to_string()});
}
const INPUT: &'static [u8] = include_bytes!("data/minf.in");