Get rid of magic numbers

png-branch
an 2018-09-11 04:20:54 -04:00
parent 2ff6b35af1
commit 7e8c28b6ca
3 changed files with 63 additions and 51 deletions

View File

@ -100,6 +100,8 @@ fn expand_data(b: Vec<u8>, depth: u16) -> Result<Vec<u8>, &'static str>
/// Process a CopyBits operation.
fn read_bitmap_area(mut im: Image, b: &[u8], packed: bool, clip: bool) -> Result<Image, &str>
{
const SIZE_HEAD: usize = 46;
let mut p = if !packed {4} else {0}; // baseAddr
let (w, h) = (im.w(), im.h());
@ -122,7 +124,7 @@ fn read_bitmap_area(mut im: Image, b: &[u8], packed: bool, clip: bool) -> Result
// = b_u32b(&b[p+38..p+42]); pmTable
// = b_u32b(&b[p+42..p+46]); pmReserved
p += 46;
p += SIZE_HEAD;
if pf & 0x8000 == 0 {return Err("PICT1 not supported")}
if xe - xb != w || ye - yb != h {return Err("image bounds are incorrect")}
@ -248,13 +250,15 @@ fn read_quicktime_c(_im: Image, _b: &[u8]) -> Result<Image, &str>
/// Load a PICT image.
pub fn load_pict(b: &[u8]) -> Result<Image, &str>
{
const SIZE_HEAD: usize = 10;
// size = b_u16b(&b[0.. 2]);
// top = b_u16b(&b[2.. 4]);
// left = b_u16b(&b[4.. 6]);
let h = b_u16b(&b[6.. 8]) as usize;
let w = b_u16b(&b[8..10]) as usize;
let im = Image::new(w, h);
let mut p = 10;
let mut p = SIZE_HEAD;
while p < b.len()
{
@ -322,3 +326,5 @@ pub fn load_pict(b: &[u8]) -> Result<Image, &str>
Err("no image in data")
}
// EOF

View File

@ -38,7 +38,22 @@ pub fn fuck_string(s: &[u8]) -> Vec<u8>
/// Converts input from Mac Roman to a Unicode string.
pub fn mac_roman_conv(s: &[u8]) -> String
{
let tr = [
let l = s.len();
let mut v = String::with_capacity(l);
for i in 0..l
{
if s[i] == 0 {break}
if s[i] & 0x80 != 0 {v.push(TR[s[i] as usize & 0x7f])}
else if s[i] == b'\r' {v.push('\n')}
else {v.push(s[i] as char)}
}
v
}
const TR: [char; 128] = [
'\u{00c4}', '\u{00c5}', '\u{00c7}', '\u{00c9}',
'\u{00d1}', '\u{00d6}', '\u{00dc}', '\u{00e1}',
'\u{00e0}', '\u{00e2}', '\u{00e4}', '\u{00e3}',
@ -73,19 +88,4 @@ pub fn mac_roman_conv(s: &[u8]) -> String
'\u{00b8}', '\u{02dd}', '\u{02db}', '\u{02c7}'
];
let l = s.len();
let mut v = String::with_capacity(l);
for i in 0..l
{
if s[i] == 0 {break}
if s[i] & 0x80 != 0 {v.push(tr[s[i] as usize & 0x7f])}
else if s[i] == b'\r' {v.push('\n')}
else {v.push(s[i] as char)}
}
v
}
// EOF

View File

@ -31,8 +31,8 @@ pub struct Wad<'a>
pub enum Ver
{
MI,
M2HasOvr,
M2HasDir,
M2,
M1Dir,
M1
}
@ -50,6 +50,9 @@ impl<'a> Wad<'a>
{
pub fn new(b: &[u8]) -> Wad
{
const SIZE_ENTRY_NEW: usize = 10;
const SIZE_ENTRY_OLD: usize = 8;
let b = &b[try_mac_header(b)..];
let ver = b_u16b(&b[ .. 2]);
let dvr = b_u16b(&b[ 2.. 4]);
@ -64,24 +67,24 @@ impl<'a> Wad<'a>
let ver = match ver {
4 => Ver::MI,
2 => Ver::M2HasOvr,
1 => Ver::M2HasDir,
2 => Ver::M2,
1 => Ver::M1Dir,
0 => Ver::M1,
_ => panic!("invalid wad version"),
_ => panic!("invalid wad version {}", ver),
};
let mut map = EntryMap::new();
let mut p = dir;
let n = match ver {Ver::M1 | Ver::M2HasDir => true, _ => false};
let h = if n {8} else {10};
let o = match ver {Ver::M1 | Ver::M1Dir => true, _ => false};
let h = if o {SIZE_ENTRY_OLD} else {SIZE_ENTRY_NEW};
for i in 0..num
{
let ofs = b_u32b(&b[p ..p+ 4]) as usize;
let len = b_u32b(&b[p+4..p+ 8]) as usize;
let ind = if n {i as u16}
let ind = if o {i as u16}
else {b_u16b(&b[p+8..p+10])};
let ent = Entry{map: get_chunks(&b[ofs..ofs+len], n),
let ent = Entry{map: get_chunks(&b[ofs..ofs+len], o),
ext: &b[p+h..p+h+ext]};
map.insert(ind, ent);
@ -95,11 +98,14 @@ impl<'a> Wad<'a>
}
}
fn get_chunks(b: &[u8], n: bool) -> ChunkMap
fn get_chunks(b: &[u8], o: bool) -> ChunkMap
{
let mut p = 0;
const SIZE_CHUNK_NEW: usize = 16;
const SIZE_CHUNK_OLD: usize = 12;
let mut map = ChunkMap::new();
let h = if n {12} else {16};
let mut p = 0;
let h = if o {SIZE_CHUNK_OLD} else {SIZE_CHUNK_NEW};
while p < b.len()
{