2018-09-06 09:01:52 -07:00
|
|
|
//! QuickDraw PICT format loader.
|
|
|
|
|
2018-12-11 00:08:23 -08:00
|
|
|
use crate::durandal::{bin::*, err::*, image::*};
|
2018-09-09 15:17:36 -07:00
|
|
|
|
2019-02-24 20:34:59 -08:00
|
|
|
/// Reads a `PixMap` header.
|
2019-02-13 21:19:36 -08:00
|
|
|
fn read_pm_header<'a>(b: &'a [u8],
|
|
|
|
pack: bool,
|
|
|
|
clip: bool,
|
|
|
|
im: &Image8)
|
|
|
|
-> ResultS<(&'a [u8], Header)>
|
2018-09-06 09:01:52 -07:00
|
|
|
{
|
2019-02-18 20:06:34 -08:00
|
|
|
read_data! {
|
|
|
|
36, BE in b =>
|
|
|
|
pt_fl = u16[0];
|
|
|
|
top = u16[2] as usize;
|
|
|
|
left = u16[4] as usize;
|
|
|
|
bottom = u16[6] as usize;
|
|
|
|
right = u16[8] as usize;
|
|
|
|
pack_t = u16[12];
|
|
|
|
depth = u16[28];
|
|
|
|
}
|
|
|
|
|
2019-02-13 21:19:36 -08:00
|
|
|
let pack_t = PackType::from_repr(pack_t)?;
|
|
|
|
|
|
|
|
if pt_fl & 0x8000 == 0 {
|
2019-02-10 02:31:57 -08:00
|
|
|
bail!("PICT1 not supported");
|
2019-02-08 21:53:27 -08:00
|
|
|
}
|
|
|
|
|
2019-02-13 21:19:36 -08:00
|
|
|
if right - left != im.w() || bottom - top != im.h() {
|
2019-02-10 02:31:57 -08:00
|
|
|
bail!("image bounds are incorrect");
|
2019-02-08 21:53:27 -08:00
|
|
|
}
|
2018-09-09 15:17:36 -07:00
|
|
|
|
2019-02-13 21:19:36 -08:00
|
|
|
let mut p = 46;
|
2018-09-09 15:17:36 -07:00
|
|
|
|
2018-12-11 15:50:23 -08:00
|
|
|
// get CLUT if packed
|
2019-02-13 21:19:36 -08:00
|
|
|
let clut = if pack {
|
2019-02-18 20:06:34 -08:00
|
|
|
let (clut, sz) = get_clut(&b[p..])?;
|
2018-12-11 15:50:23 -08:00
|
|
|
p += sz;
|
|
|
|
Some(clut)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2018-09-11 12:07:42 -07:00
|
|
|
|
2018-12-11 15:50:23 -08:00
|
|
|
p += 18; // srcRect, dstRect, mode
|
2018-09-09 15:17:36 -07:00
|
|
|
|
2019-02-08 21:53:27 -08:00
|
|
|
if clip {
|
2019-02-18 20:06:34 -08:00
|
|
|
p += u16b(&b[p..]) as usize; // maskRgn
|
2019-02-08 21:53:27 -08:00
|
|
|
}
|
2018-09-11 12:07:42 -07:00
|
|
|
|
2019-02-13 21:19:36 -08:00
|
|
|
let rle = pack_t == PackType::Default ||
|
|
|
|
pack_t == PackType::Rle16 && depth == 16 ||
|
|
|
|
pack_t == PackType::Rle32 && depth == 32;
|
2018-09-06 09:01:52 -07:00
|
|
|
|
2019-02-13 21:19:36 -08:00
|
|
|
let pitch = (pt_fl & 0x3FFF) as usize;
|
2018-09-06 09:01:52 -07:00
|
|
|
|
2019-02-13 21:19:36 -08:00
|
|
|
Ok((&b[p..], Header{pitch, pack_t, depth, clut, rle}))
|
|
|
|
}
|
2018-09-09 15:17:36 -07:00
|
|
|
|
2019-02-24 20:34:59 -08:00
|
|
|
/// Reads an indexed `PixMap`.
|
2019-02-13 21:19:36 -08:00
|
|
|
fn read_pm_ind(mut im: Image8, b: &[u8], hdr: Header) -> ResultS<Image8>
|
|
|
|
{
|
|
|
|
let clut = ok!(hdr.clut, "no CLUT in indexed mode")?;
|
|
|
|
let mut p = 0;
|
2018-09-06 09:01:52 -07:00
|
|
|
|
2019-02-13 21:19:36 -08:00
|
|
|
if hdr.pitch < 8 && hdr.depth == 8 {
|
|
|
|
// uncompressed 8-bit colormap indices
|
|
|
|
for _ in 0..im.h() {
|
|
|
|
for _ in 0..im.w() {
|
2019-02-18 20:06:34 -08:00
|
|
|
let idx = b[p] as usize;
|
2019-02-13 21:19:36 -08:00
|
|
|
im.cr.push(ok!(clut.get(idx), "invalid index")?.clone());
|
|
|
|
p += 1;
|
2019-02-08 21:53:27 -08:00
|
|
|
}
|
2018-09-06 09:01:52 -07:00
|
|
|
}
|
|
|
|
|
2019-02-13 21:19:36 -08:00
|
|
|
Ok(im)
|
|
|
|
} else if hdr.rle {
|
|
|
|
// RLE compressed 1, 2, 4 or 8 bit colormap indices
|
|
|
|
for _ in 0..im.h() {
|
2019-03-01 01:58:02 -08:00
|
|
|
let (d, pp) = read_rle::<u8>(&b[p..], hdr.pitch)?;
|
2019-02-20 18:33:57 -08:00
|
|
|
let d = if hdr.depth < 8 {
|
|
|
|
expand_data(d, hdr.depth)?
|
|
|
|
} else {
|
|
|
|
d
|
|
|
|
};
|
2018-09-09 15:17:36 -07:00
|
|
|
|
2019-02-13 21:19:36 -08:00
|
|
|
p += pp;
|
2018-09-09 15:17:36 -07:00
|
|
|
|
2019-02-20 18:10:00 -08:00
|
|
|
for &idx in &d {
|
2019-02-20 18:33:57 -08:00
|
|
|
im.cr
|
|
|
|
.push(ok!(clut.get(idx as usize), "invalid index")?.clone());
|
2019-02-13 21:19:36 -08:00
|
|
|
}
|
|
|
|
}
|
2019-02-08 21:53:27 -08:00
|
|
|
|
2019-02-13 21:19:36 -08:00
|
|
|
Ok(im)
|
|
|
|
} else {
|
|
|
|
bail!("invalid configuration")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-24 20:34:59 -08:00
|
|
|
/// Reads a R5G5B5 `PixMap`.
|
2019-02-13 21:19:36 -08:00
|
|
|
fn read_pm_16(mut im: Image8, b: &[u8], hdr: Header) -> ResultS<Image8>
|
|
|
|
{
|
|
|
|
let mut p = 0;
|
|
|
|
|
|
|
|
if hdr.pitch < 8 || hdr.pack_t == PackType::None {
|
|
|
|
// uncompressed R5G5B5
|
|
|
|
for _ in 0..im.h() {
|
|
|
|
for _ in 0..im.w() {
|
2019-02-18 20:06:34 -08:00
|
|
|
let cr = u16b(&b[p..]);
|
|
|
|
im.cr.push(r5g5b5_to_rgb8(cr));
|
2019-02-13 21:19:36 -08:00
|
|
|
p += 2;
|
2018-09-06 09:01:52 -07:00
|
|
|
}
|
2019-02-08 21:53:27 -08:00
|
|
|
}
|
2018-09-06 09:01:52 -07:00
|
|
|
|
2019-02-13 21:19:36 -08:00
|
|
|
Ok(im)
|
|
|
|
} else if hdr.rle {
|
|
|
|
// RLE compressed R5G5B5
|
|
|
|
for _ in 0..im.h() {
|
2019-03-01 01:58:02 -08:00
|
|
|
let (d, pp) = read_rle::<u16>(&b[p..], hdr.pitch)?;
|
2019-02-13 21:19:36 -08:00
|
|
|
|
|
|
|
p += pp;
|
|
|
|
|
2019-03-01 01:58:02 -08:00
|
|
|
for &cr in &d {
|
2019-02-18 20:06:34 -08:00
|
|
|
im.cr.push(r5g5b5_to_rgb8(cr));
|
2019-02-13 21:19:36 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(im)
|
|
|
|
} else {
|
|
|
|
bail!("invalid configuration")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-24 20:34:59 -08:00
|
|
|
/// Reads a RGB8 `PixMap`.
|
2019-02-13 21:19:36 -08:00
|
|
|
fn read_pm_32(mut im: Image8, b: &[u8], hdr: Header) -> ResultS<Image8>
|
|
|
|
{
|
|
|
|
let mut p = 0;
|
|
|
|
|
2019-02-20 18:33:57 -08:00
|
|
|
if hdr.pitch < 8 ||
|
|
|
|
hdr.pack_t == PackType::None ||
|
|
|
|
hdr.pack_t == PackType::NoPad
|
|
|
|
{
|
2019-02-13 21:19:36 -08:00
|
|
|
// uncompressed RGB8 or XRGB8
|
|
|
|
for _ in 0..im.h() {
|
|
|
|
for _ in 0..im.w() {
|
|
|
|
if hdr.pack_t != PackType::NoPad {
|
|
|
|
p += 1;
|
2018-09-06 09:01:52 -07:00
|
|
|
}
|
2019-02-18 20:06:34 -08:00
|
|
|
|
|
|
|
read_data! {
|
|
|
|
p + 3, BE in b =>
|
|
|
|
r = u8[p];
|
|
|
|
g = u8[p + 1];
|
|
|
|
b = u8[p + 2];
|
|
|
|
}
|
|
|
|
|
2019-02-13 21:19:36 -08:00
|
|
|
im.cr.push(Color8::new(r, g, b));
|
2019-02-18 20:06:34 -08:00
|
|
|
|
|
|
|
p += 3;
|
2019-02-13 21:19:36 -08:00
|
|
|
}
|
|
|
|
}
|
2018-09-06 09:01:52 -07:00
|
|
|
|
2019-02-13 21:19:36 -08:00
|
|
|
Ok(im)
|
|
|
|
} else if hdr.rle {
|
|
|
|
// RLE compressed RGB8
|
|
|
|
let pitch = hdr.pitch - im.w(); // remove padding byte from pitch
|
|
|
|
for _ in 0..im.h() {
|
2019-03-01 01:58:02 -08:00
|
|
|
let (d, pp) = read_rle::<u8>(&b[p..], pitch)?;
|
2019-02-13 21:19:36 -08:00
|
|
|
|
|
|
|
p += pp;
|
|
|
|
|
|
|
|
for x in 0..im.w() {
|
2019-02-18 20:06:34 -08:00
|
|
|
let r = d[x + im.w()];
|
|
|
|
let g = d[x + im.w() * 2];
|
|
|
|
let b = d[x + im.w() * 3];
|
2019-02-13 21:19:36 -08:00
|
|
|
im.cr.push(Color8::new(r, g, b));
|
2019-02-08 21:53:27 -08:00
|
|
|
}
|
|
|
|
}
|
2019-02-13 21:19:36 -08:00
|
|
|
|
|
|
|
Ok(im)
|
|
|
|
} else {
|
|
|
|
bail!("invalid configuration")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-24 20:34:59 -08:00
|
|
|
/// Process a `CopyBits` operation.
|
2019-02-20 18:33:57 -08:00
|
|
|
fn read_pm_area(im: Image8, b: &[u8], pack: bool, clip: bool)
|
2019-02-13 21:19:36 -08:00
|
|
|
-> ResultS<Image8>
|
|
|
|
{
|
|
|
|
let p = if pack {0} else {4};
|
|
|
|
let (b, hdr) = read_pm_header(&b[p..], pack, clip, &im)?;
|
|
|
|
|
|
|
|
match hdr.depth {
|
|
|
|
1 | 2 | 4 | 8 => read_pm_ind(im, b, hdr),
|
|
|
|
16 => read_pm_16(im, b, hdr),
|
|
|
|
32 => read_pm_32(im, b, hdr),
|
|
|
|
_ => bail!("invalid bit depth"),
|
2018-09-06 09:01:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-24 20:34:59 -08:00
|
|
|
/// Load a `PICT` image.
|
2019-02-09 11:04:27 -08:00
|
|
|
pub fn load_pict(b: &[u8]) -> ResultS<Image8>
|
2018-09-06 09:01:52 -07:00
|
|
|
{
|
2019-02-18 20:06:34 -08:00
|
|
|
read_data! {
|
|
|
|
10, BE in b =>
|
|
|
|
h = u16[6] as usize;
|
|
|
|
w = u16[8] as usize;
|
|
|
|
}
|
|
|
|
|
2019-02-12 03:44:54 -08:00
|
|
|
let im = Image8::new(w, h);
|
2018-12-10 23:32:59 -08:00
|
|
|
|
|
|
|
let mut p = 10; // size of header
|
|
|
|
|
|
|
|
while p < b.len() {
|
2019-02-18 20:06:34 -08:00
|
|
|
read_data! {
|
|
|
|
p + 2, BE in b =>
|
|
|
|
op = u16[p];
|
|
|
|
}
|
|
|
|
|
2019-02-13 21:19:36 -08:00
|
|
|
p += 2;
|
2018-09-09 15:17:36 -07:00
|
|
|
|
2018-09-06 09:01:52 -07:00
|
|
|
match op {
|
2019-02-08 21:53:27 -08:00
|
|
|
0x0098 => {
|
|
|
|
// PackBitsRect
|
2019-02-18 20:06:34 -08:00
|
|
|
return read_pm_area(im, &b[p..], true, false);
|
2019-02-08 21:53:27 -08:00
|
|
|
}
|
|
|
|
0x0099 => {
|
|
|
|
// PackBitsRgn
|
2019-02-18 20:06:34 -08:00
|
|
|
return read_pm_area(im, &b[p..], true, true);
|
2019-02-08 21:53:27 -08:00
|
|
|
}
|
2019-03-01 01:58:02 -08:00
|
|
|
0x009A => {
|
2019-02-08 21:53:27 -08:00
|
|
|
// DirectBitsRect
|
2019-02-18 20:06:34 -08:00
|
|
|
return read_pm_area(im, &b[p..], false, false);
|
2019-02-08 21:53:27 -08:00
|
|
|
}
|
2019-03-01 01:58:02 -08:00
|
|
|
0x009B => {
|
2019-02-08 21:53:27 -08:00
|
|
|
// DirectBitsRgn
|
2019-02-18 20:06:34 -08:00
|
|
|
return read_pm_area(im, &b[p..], false, true);
|
2019-02-08 21:53:27 -08:00
|
|
|
}
|
|
|
|
0x8200 => {
|
|
|
|
// CompressedQuickTime
|
2019-02-24 20:34:59 -08:00
|
|
|
unimplemented!();
|
2019-02-08 21:53:27 -08:00
|
|
|
}
|
2019-03-01 01:58:02 -08:00
|
|
|
0x00FF => {
|
2019-02-08 21:53:27 -08:00
|
|
|
// OpEndPic
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// help i'm trapped in an awful metafile format from the 80s
|
|
|
|
0x0000 | // NoOp
|
2019-03-01 01:58:02 -08:00
|
|
|
0x001C | // HiliteMode
|
|
|
|
0x001E | // DefHilite
|
2019-02-08 21:53:27 -08:00
|
|
|
0x0038 | // FrameSameRect
|
|
|
|
0x0039 | // PaintSameRect
|
2019-03-01 01:58:02 -08:00
|
|
|
0x003A | // EraseSameRect
|
|
|
|
0x003B | // InvertSameRect
|
|
|
|
0x003C | // FillSameRect
|
2019-02-08 21:53:27 -08:00
|
|
|
0x8000 | // Reserved
|
|
|
|
0x8100 => (), // Reserved
|
|
|
|
0x0003 | // TxFont
|
|
|
|
0x0004 | // TxFace
|
|
|
|
0x0005 | // TxMode
|
|
|
|
0x0008 | // PnMode
|
2019-03-01 01:58:02 -08:00
|
|
|
0x000D | // TxSize
|
2019-02-08 21:53:27 -08:00
|
|
|
0x0011 | // VersionOp
|
|
|
|
0x0015 | // PnLocHFrac
|
|
|
|
0x0016 | // ChExtra
|
|
|
|
0x0023 | // ShortLineFrom
|
2019-03-01 01:58:02 -08:00
|
|
|
0x00A0 => p += 2, // ShortComment
|
2019-02-08 21:53:27 -08:00
|
|
|
0x0006 | // SpExtra
|
|
|
|
0x0007 | // PnSize
|
2019-03-01 01:58:02 -08:00
|
|
|
0x000B | // OvSize
|
|
|
|
0x000C | // Origin
|
|
|
|
0x000E | // FgCol
|
|
|
|
0x000F | // BkCol
|
2019-02-08 21:53:27 -08:00
|
|
|
0x0021 => p += 4, // LineFrom
|
2019-03-01 01:58:02 -08:00
|
|
|
0x001A | // RGBFgCol
|
|
|
|
0x001B | // RGBBkCol
|
|
|
|
0x001D | // TxRatio
|
2019-02-08 21:53:27 -08:00
|
|
|
0x0022 => p += 6, // ShortLine
|
|
|
|
0x0002 | // BkPat
|
|
|
|
0x0009 | // PnPat
|
|
|
|
0x0010 | // TxRatio
|
|
|
|
0x0020 | // Line
|
2019-03-01 01:58:02 -08:00
|
|
|
0x002E | // GlyphState
|
2019-02-08 21:53:27 -08:00
|
|
|
0x0030 | // FrameRect
|
|
|
|
0x0031 | // PaintRect
|
|
|
|
0x0032 | // EraseRect
|
|
|
|
0x0033 | // InvertRect
|
|
|
|
0x0034 => p += 8, // FillRect
|
2019-03-01 01:58:02 -08:00
|
|
|
0x002D => p += 10, // LineJustify
|
2019-02-18 20:06:34 -08:00
|
|
|
0x0001 => p += (u16b(&b[p.. ]) & !1) as usize, // Clip
|
2019-03-01 01:58:02 -08:00
|
|
|
0x00A1 => p += (u16b(&b[p+2..]) & !1) as usize + 2, // LongComment
|
2019-02-08 21:53:27 -08:00
|
|
|
0x100..=
|
2019-03-01 01:58:02 -08:00
|
|
|
0x7FFF => p += (op >> 8) as usize * 2, // Reserved
|
2019-02-08 21:53:27 -08:00
|
|
|
_ => {
|
2019-02-10 02:31:57 -08:00
|
|
|
bail!("invalid op in PICT");
|
2019-02-08 21:53:27 -08:00
|
|
|
}
|
2018-09-06 09:01:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 14:51:49 -08:00
|
|
|
Err(err_msg("no image in data"))
|
2018-09-06 09:01:52 -07:00
|
|
|
}
|
2018-09-11 01:20:54 -07:00
|
|
|
|
2019-02-24 20:34:59 -08:00
|
|
|
/// Read a `ColorTable` structure.
|
2019-02-09 11:04:27 -08:00
|
|
|
pub fn get_clut(b: &[u8]) -> ResultS<(Vec<Color8>, usize)>
|
2018-12-11 15:50:23 -08:00
|
|
|
{
|
2019-02-18 20:06:34 -08:00
|
|
|
read_data! {
|
|
|
|
8, BE in b =>
|
|
|
|
dev = u16[4];
|
|
|
|
num = u16[6] as usize;
|
|
|
|
}
|
|
|
|
|
|
|
|
let dev = dev & 0x8000 != 0;
|
|
|
|
let num = num + 1;
|
2018-12-11 15:50:23 -08:00
|
|
|
|
|
|
|
let mut p = 8;
|
2019-02-09 11:04:27 -08:00
|
|
|
let mut clut = vec![Color8::new(0, 0, 0); num];
|
2018-12-11 15:50:23 -08:00
|
|
|
|
|
|
|
for i in 0..num {
|
2019-02-18 20:06:34 -08:00
|
|
|
read_data! {
|
|
|
|
p + 8, BE in b =>
|
|
|
|
n = u16[p] as usize;
|
|
|
|
r = u8[p + 2];
|
|
|
|
g = u8[p + 4];
|
|
|
|
b = u8[p + 6];
|
2019-02-08 21:53:27 -08:00
|
|
|
}
|
2018-12-11 15:50:23 -08:00
|
|
|
|
2019-02-18 20:06:34 -08:00
|
|
|
// with device mapping, we ignore the index entirely
|
2019-02-24 20:34:59 -08:00
|
|
|
let n = if dev {i} else {n};
|
2019-02-18 20:06:34 -08:00
|
|
|
|
2019-02-10 02:31:57 -08:00
|
|
|
*ok!(clut.get_mut(n), "invalid index")? = Color8::new(r, g, b);
|
2019-02-04 21:12:10 -08:00
|
|
|
|
2018-12-11 15:50:23 -08:00
|
|
|
p += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((clut, p))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read run-length encoded data.
|
2019-03-01 01:58:02 -08:00
|
|
|
fn read_rle<T>(b: &[u8], pitch: usize) -> ResultS<(Vec<T>, usize)>
|
|
|
|
where T: ReadRleData
|
2018-12-11 15:50:23 -08:00
|
|
|
{
|
|
|
|
let mut p = 0;
|
|
|
|
let mut o = Vec::with_capacity(pitch);
|
2019-02-08 21:53:27 -08:00
|
|
|
|
|
|
|
let sz = if pitch > 250 {
|
2019-02-18 20:06:34 -08:00
|
|
|
(u16b(b) as usize + 2, p += 2).0
|
2019-02-08 21:53:27 -08:00
|
|
|
} else {
|
2019-02-18 20:06:34 -08:00
|
|
|
(b[0] as usize + 1, p += 1).0
|
2019-02-08 21:53:27 -08:00
|
|
|
};
|
2018-12-11 15:50:23 -08:00
|
|
|
|
|
|
|
while p < sz {
|
2019-02-18 20:06:34 -08:00
|
|
|
let szf = b[p];
|
2018-12-11 15:50:23 -08:00
|
|
|
let cmp = szf & 0x80 != 0;
|
|
|
|
let len = if cmp {!szf + 2} else {szf + 1} as usize;
|
|
|
|
|
2019-02-13 21:19:36 -08:00
|
|
|
p += 1;
|
2018-12-11 15:50:23 -08:00
|
|
|
o.reserve(len);
|
|
|
|
|
2019-03-01 01:58:02 -08:00
|
|
|
T::read_rle_data(b, &mut p, cmp, len, &mut o);
|
2018-12-11 15:50:23 -08:00
|
|
|
}
|
|
|
|
|
2019-02-08 21:53:27 -08:00
|
|
|
if o.len() == pitch {
|
|
|
|
Ok((o, p))
|
|
|
|
} else {
|
|
|
|
Err(err_msg("incorrect size for compressed scanline"))
|
|
|
|
}
|
2018-12-11 15:50:23 -08:00
|
|
|
}
|
|
|
|
|
2019-03-01 01:58:02 -08:00
|
|
|
trait ReadRleData: Sized
|
2018-12-11 15:50:23 -08:00
|
|
|
{
|
2019-03-01 01:58:02 -08:00
|
|
|
/// Read a sequence of packed RLE data.
|
|
|
|
fn read_rle_data(b: &[u8],
|
|
|
|
p: &mut usize,
|
|
|
|
cmp: bool,
|
|
|
|
len: usize,
|
|
|
|
out: &mut Vec<Self>);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReadRleData for u16
|
|
|
|
{
|
|
|
|
fn read_rle_data(b: &[u8],
|
|
|
|
p: &mut usize,
|
|
|
|
cmp: bool,
|
|
|
|
len: usize,
|
2019-03-01 20:09:18 -08:00
|
|
|
out: &mut Vec<Self>)
|
2019-03-01 01:58:02 -08:00
|
|
|
{
|
|
|
|
if cmp {
|
|
|
|
let d = u16b(&b[*p..*p + 2]);
|
|
|
|
*p += 2;
|
|
|
|
for _ in 0..len {
|
|
|
|
out.push(d);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _ in 0..len {
|
|
|
|
let d = u16b(&b[*p..*p + 2]);
|
|
|
|
*p += 2;
|
|
|
|
out.push(d);
|
2019-02-08 21:53:27 -08:00
|
|
|
}
|
|
|
|
}
|
2019-03-01 01:58:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReadRleData for u8
|
|
|
|
{
|
|
|
|
fn read_rle_data(b: &[u8],
|
|
|
|
p: &mut usize,
|
|
|
|
cmp: bool,
|
|
|
|
len: usize,
|
2019-03-01 20:09:18 -08:00
|
|
|
out: &mut Vec<Self>)
|
2019-03-01 01:58:02 -08:00
|
|
|
{
|
|
|
|
if cmp {
|
|
|
|
let d = b[*p];
|
|
|
|
*p += 1;
|
|
|
|
for _ in 0..len {
|
|
|
|
out.push(d);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _ in 0..len {
|
|
|
|
let d = b[*p];
|
|
|
|
*p += 1;
|
|
|
|
out.push(d);
|
2019-02-08 21:53:27 -08:00
|
|
|
}
|
|
|
|
}
|
2018-12-11 15:50:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Expand packed pixel data based on bit depth.
|
2019-02-04 21:12:10 -08:00
|
|
|
pub fn expand_data(b: Vec<u8>, depth: u16) -> ResultS<Vec<u8>>
|
2018-12-11 15:50:23 -08:00
|
|
|
{
|
2019-02-10 20:33:38 -08:00
|
|
|
let mut o = Vec::with_capacity(match depth {
|
|
|
|
4 => b.len() * 2,
|
|
|
|
2 => b.len() * 4,
|
|
|
|
1 => b.len() * 8,
|
|
|
|
_ => bail!("invalid bit depth"),
|
|
|
|
});
|
2018-12-11 15:50:23 -08:00
|
|
|
|
|
|
|
for ch in b {
|
|
|
|
match depth {
|
2019-02-24 20:34:59 -08:00
|
|
|
4 => {for i in (0..=1).rev() {o.push(ch >> (i * 4) & 0xF_u8);}}
|
|
|
|
2 => {for i in (0..=3).rev() {o.push(ch >> (i * 2) & 0x3_u8);}}
|
|
|
|
1 => {for i in (0..=7).rev() {o.push(ch >> i & 0x1_u8);}}
|
2019-02-10 02:31:57 -08:00
|
|
|
_ => bail!("invalid bit depth"),
|
2018-12-11 15:50:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(o)
|
|
|
|
}
|
|
|
|
|
2019-02-13 21:19:36 -08:00
|
|
|
struct Header
|
|
|
|
{
|
|
|
|
pitch: usize,
|
|
|
|
pack_t: PackType,
|
|
|
|
depth: u16,
|
|
|
|
clut: Option<Vec<Color8>>,
|
|
|
|
rle: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
c_enum! {
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
enum PackType: u16
|
|
|
|
{
|
|
|
|
0 => Default,
|
|
|
|
1 => None,
|
|
|
|
2 => NoPad,
|
|
|
|
3 => Rle16,
|
|
|
|
4 => Rle32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 01:20:54 -07:00
|
|
|
// EOF
|