formatting

png-branch
an 2019-02-10 23:33:38 -05:00
parent 627e9969a3
commit b7a2e2cfde
4 changed files with 30 additions and 33 deletions

View File

@ -1,9 +1,7 @@
//! Binary data conversion utilities. //! Binary data conversion utilities.
use crate::durandal::err::*; use crate::durandal::err::*;
use std::{fmt, use std::{fmt, num::NonZeroU16, slice::SliceIndex};
num::NonZeroU16,
slice::SliceIndex};
/// Returns a byte array from `b` at `i`. /// Returns a byte array from `b` at `i`.
pub fn c_data<I>(b: &[u8], i: I) -> ResultS<&<I as SliceIndex<[u8]>>::Output> pub fn c_data<I>(b: &[u8], i: I) -> ResultS<&<I as SliceIndex<[u8]>>::Output>

View File

@ -5,21 +5,19 @@ pub use failure::{Error, Fail};
use crate::durandal::traits::PrimInt; use crate::durandal::traits::PrimInt;
use std::fmt; use std::fmt;
macro_rules! ok macro_rules! ok {
{
($v:expr, $msg:expr) => { ($v:expr, $msg:expr) => {
match $v { match $v {
Some(v) => Ok(v), Some(v) => Ok(v),
None => Err(err_msg($msg)), None => Err(err_msg($msg)),
} }
} };
} }
macro_rules! bail macro_rules! bail {
{
($e:expr) => { ($e:expr) => {
return Err(err_msg($e)) return Err(err_msg($e));
} };
} }
pub fn err_msg(msg: &'static str) -> Error pub fn err_msg(msg: &'static str) -> Error

View File

@ -360,13 +360,12 @@ fn read_rle_data<F, N>(cmp: bool, len: usize, out: &mut Vec<u8>, mut read: F)
/// Expand packed pixel data based on bit depth. /// Expand packed pixel data based on bit depth.
pub fn expand_data(b: Vec<u8>, depth: u16) -> ResultS<Vec<u8>> pub fn expand_data(b: Vec<u8>, depth: u16) -> ResultS<Vec<u8>>
{ {
let mut o = let mut o = Vec::with_capacity(match depth {
Vec::with_capacity(match depth { 4 => b.len() * 2,
4 => b.len() * 2, 2 => b.len() * 4,
2 => b.len() * 4, 1 => b.len() * 8,
1 => b.len() * 8, _ => bail!("invalid bit depth"),
_ => bail!("invalid bit depth"), });
});
for ch in b { for ch in b {
match depth { match depth {

View File

@ -19,8 +19,10 @@ fn color(b: &[u8]) -> ResultS<(usize, ColorShp)>
Ok((i, ColorShp::Opaque{r, g, b, l})) Ok((i, ColorShp::Opaque{r, g, b, l}))
} }
fn clut_collection(b: &[u8], clr_num: usize, clu_num: usize) fn clut_collection(b: &[u8],
-> ResultS<Vec<Vec<ColorShp>>> clr_num: usize,
clu_num: usize)
-> ResultS<Vec<Vec<ColorShp>>>
{ {
let mut tables = vec![vec![ColorShp::Translucent; clr_num]; clu_num]; let mut tables = vec![vec![ColorShp::Translucent; clr_num]; clu_num];
let mut p = 0; let mut p = 0;
@ -162,18 +164,17 @@ pub fn testfn_replaceme(b: &[u8]) -> ResultS<()>
let offset_hi = c_u32b(b, p + 12)? as usize; let offset_hi = c_u32b(b, p + 12)? as usize;
let length_hi = c_u32b(b, p + 16)? as usize; let length_hi = c_u32b(b, p + 16)? as usize;
let collections = ( let collections =
if offset_lo != u32::max_value() as usize { (if offset_lo != u32::max_value() as usize {
Some(collection(c_data(b, offset_lo..offset_lo + length_lo)?)?) Some(collection(c_data(b, offset_lo..offset_lo + length_lo)?)?)
} else { } else {
None None
}, },
if offset_hi != u32::max_value() as usize { if offset_hi != u32::max_value() as usize {
Some(collection(c_data(b, offset_hi..offset_hi + length_hi)?)?) Some(collection(c_data(b, offset_hi..offset_hi + length_hi)?)?)
} else { } else {
None None
} });
);
dbg!(collections); dbg!(collections);
} }
@ -236,12 +237,13 @@ impl Color for ColorShp
enum ColorShp enum ColorShp
{ {
Translucent, Translucent,
Opaque { Opaque
{
r: u16, r: u16,
g: u16, g: u16,
b: u16, b: u16,
l: bool, l: bool,
} },
} }
#[derive(Debug)] #[derive(Debug)]