Add PNG decompressor in SIM

This commit is contained in:
Michal Moskal 2017-10-30 14:42:08 +00:00
parent dcb398d3d5
commit 9cdb4081fd
2 changed files with 527 additions and 23 deletions

375
sim/inflate.ts Normal file
View File

@ -0,0 +1,375 @@
// adapted from https://github.com/devongovett/tiny-inflate
// License: MIT
namespace tinf {
var TINF_OK = 0;
var TINF_DATA_ERROR = -3;
class Tree {
table = new Uint16Array(16); /* table of code length counts */
trans = new Uint16Array(288); /* code -> symbol translation table */
}
class Data {
sourceIndex = 0;
tag = 0;
bitcount = 0;
destLen = 0;
ltree = new Tree(); /* dynamic length/symbol tree */
dtree = new Tree(); /* dynamic distance tree */
constructor(public source: Uint8Array, public dest: Uint8Array) {
}
}
/* --------------------------------------------------- *
* -- uninitialized global data (static structures) -- *
* --------------------------------------------------- */
var sltree = new Tree();
var sdtree = new Tree();
/* extra bits and base tables for length codes */
var length_bits = new Uint8Array(30);
var length_base = new Uint16Array(30);
/* extra bits and base tables for distance codes */
var dist_bits = new Uint8Array(30);
var dist_base = new Uint16Array(30);
/* special ordering of code length codes */
var clcidx = new Uint8Array([
16, 17, 18, 0, 8, 7, 9, 6,
10, 5, 11, 4, 12, 3, 13, 2,
14, 1, 15
]);
/* used by tinf_decode_trees, avoids allocations every call */
var code_tree = new Tree();
var lengths = new Uint8Array(288 + 32);
/* ----------------------- *
* -- utility functions -- *
* ----------------------- */
/* build extra bits and base tables */
function tinf_build_bits_base(bits: Uint8Array, base: Uint16Array, delta: number, first: number) {
/* build bits table */
for (let i = 0; i < delta; ++i) bits[i] = 0;
for (let i = 0; i < 30 - delta; ++i) bits[i + delta] = i / delta | 0;
/* build base table */
for (let sum = first, i = 0; i < 30; ++i) {
base[i] = sum;
sum += 1 << bits[i];
}
}
/* build the fixed huffman trees */
function tinf_build_fixed_trees(lt: Tree, dt: Tree) {
let i = 0;
/* build fixed length tree */
for (i = 0; i < 7; ++i) lt.table[i] = 0;
lt.table[7] = 24;
lt.table[8] = 152;
lt.table[9] = 112;
for (i = 0; i < 24; ++i) lt.trans[i] = 256 + i;
for (i = 0; i < 144; ++i) lt.trans[24 + i] = i;
for (i = 0; i < 8; ++i) lt.trans[24 + 144 + i] = 280 + i;
for (i = 0; i < 112; ++i) lt.trans[24 + 144 + 8 + i] = 144 + i;
/* build fixed distance tree */
for (i = 0; i < 5; ++i) dt.table[i] = 0;
dt.table[5] = 32;
for (i = 0; i < 32; ++i) dt.trans[i] = i;
}
/* given an array of code lengths, build a tree */
var offs = new Uint16Array(16);
function tinf_build_tree(t: Tree, lengths: Uint8Array, off: number, num: number) {
var i = 0, sum = 0;
/* clear code length count table */
for (i = 0; i < 16; ++i) t.table[i] = 0;
/* scan symbol lengths, and sum code length counts */
for (i = 0; i < num; ++i) t.table[lengths[off + i]]++;
t.table[0] = 0;
/* compute offset table for distribution sort */
for (sum = 0, i = 0; i < 16; ++i) {
offs[i] = sum;
sum += t.table[i];
}
/* create code->symbol translation table (symbols sorted by code) */
for (i = 0; i < num; ++i) {
if (lengths[off + i]) t.trans[offs[lengths[off + i]]++] = i;
}
}
/* ---------------------- *
* -- decode functions -- *
* ---------------------- */
/* get one bit from source stream */
function tinf_getbit(d: Data) {
/* check if tag is empty */
if (!d.bitcount--) {
/* load next tag */
d.tag = d.source[d.sourceIndex++];
d.bitcount = 7;
}
/* shift bit out of tag */
var bit = d.tag & 1;
d.tag >>>= 1;
return bit;
}
/* read a num bit value from a stream and add base */
function tinf_read_bits(d: Data, num: number, base: number) {
if (!num)
return base;
while (d.bitcount < 24) {
d.tag |= d.source[d.sourceIndex++] << d.bitcount;
d.bitcount += 8;
}
var val = d.tag & (0xffff >>> (16 - num));
d.tag >>>= num;
d.bitcount -= num;
return val + base;
}
/* given a data stream and a tree, decode a symbol */
function tinf_decode_symbol(d: Data, t: Tree) {
while (d.bitcount < 24) {
d.tag |= d.source[d.sourceIndex++] << d.bitcount;
d.bitcount += 8;
}
var sum = 0, cur = 0, len = 0;
var tag = d.tag;
/* get more bits while code value is above sum */
do {
cur = 2 * cur + (tag & 1);
tag >>>= 1;
++len;
sum += t.table[len];
cur -= t.table[len];
} while (cur >= 0);
d.tag = tag;
d.bitcount -= len;
return t.trans[sum + cur];
}
/* given a data stream, decode dynamic trees from it */
function tinf_decode_trees(d: Data, lt: Tree, dt: Tree) {
var i = 0, num = 0, length = 0;
/* get 5 bits HLIT (257-286) */
let hlit = tinf_read_bits(d, 5, 257);
/* get 5 bits HDIST (1-32) */
let hdist = tinf_read_bits(d, 5, 1);
/* get 4 bits HCLEN (4-19) */
let hclen = tinf_read_bits(d, 4, 4);
for (i = 0; i < 19; ++i) lengths[i] = 0;
/* read code lengths for code length alphabet */
for (i = 0; i < hclen; ++i) {
/* get 3 bits code length (0-7) */
var clen = tinf_read_bits(d, 3, 0);
lengths[clcidx[i]] = clen;
}
/* build code length tree */
tinf_build_tree(code_tree, lengths, 0, 19);
/* decode code lengths for the dynamic trees */
for (num = 0; num < hlit + hdist;) {
var sym = tinf_decode_symbol(d, code_tree);
switch (sym) {
case 16:
/* copy previous code length 3-6 times (read 2 bits) */
var prev = lengths[num - 1];
for (length = tinf_read_bits(d, 2, 3); length; --length) {
lengths[num++] = prev;
}
break;
case 17:
/* repeat code length 0 for 3-10 times (read 3 bits) */
for (length = tinf_read_bits(d, 3, 3); length; --length) {
lengths[num++] = 0;
}
break;
case 18:
/* repeat code length 0 for 11-138 times (read 7 bits) */
for (length = tinf_read_bits(d, 7, 11); length; --length) {
lengths[num++] = 0;
}
break;
default:
/* values 0-15 represent the actual code lengths */
lengths[num++] = sym;
break;
}
}
/* build dynamic trees */
tinf_build_tree(lt, lengths, 0, hlit);
tinf_build_tree(dt, lengths, hlit, hdist);
}
/* ----------------------------- *
* -- block inflate functions -- *
* ----------------------------- */
/* given a stream and two trees, inflate a block of data */
function tinf_inflate_block_data(d: Data, lt: Tree, dt: Tree) {
while (true) {
var sym = tinf_decode_symbol(d, lt);
/* check for end of block */
if (sym === 256) {
return TINF_OK;
}
if (sym < 256) {
d.dest[d.destLen++] = sym;
} else {
sym -= 257;
/* possibly get more bits from length code */
let length = tinf_read_bits(d, length_bits[sym], length_base[sym]);
let dist = tinf_decode_symbol(d, dt);
/* possibly get more bits from distance code */
let offs = d.destLen - tinf_read_bits(d, dist_bits[dist], dist_base[dist]);
/* copy match */
for (let i = offs; i < offs + length; ++i) {
d.dest[d.destLen++] = d.dest[i];
}
}
}
}
/* inflate an uncompressed block of data */
function tinf_inflate_uncompressed_block(d: Data) {
/* unread from bitbuffer */
while (d.bitcount > 8) {
d.sourceIndex--;
d.bitcount -= 8;
}
/* get length */
let length = d.source[d.sourceIndex + 1];
length = 256 * length + d.source[d.sourceIndex];
/* get one's complement of length */
let invlength = d.source[d.sourceIndex + 3];
invlength = 256 * invlength + d.source[d.sourceIndex + 2];
/* check length */
if (length !== (~invlength & 0x0000ffff))
return TINF_DATA_ERROR;
d.sourceIndex += 4;
/* copy block */
for (let i = length; i; --i)
d.dest[d.destLen++] = d.source[d.sourceIndex++];
/* make sure we start next block on a byte boundary */
d.bitcount = 0;
return TINF_OK;
}
/* inflate stream from source to dest */
export function uncompress(source: Uint8Array, dest: Uint8Array) {
var d = new Data(source, dest);
var bfinal = 0, res = 0;
do {
/* read final block flag */
bfinal = tinf_getbit(d);
/* read block type (2 bits) */
let btype = tinf_read_bits(d, 2, 0);
/* decompress block */
switch (btype) {
case 0:
/* decompress uncompressed block */
res = tinf_inflate_uncompressed_block(d);
break;
case 1:
/* decompress block with fixed huffman trees */
res = tinf_inflate_block_data(d, sltree, sdtree);
break;
case 2:
/* decompress block with dynamic huffman trees */
tinf_decode_trees(d, d.ltree, d.dtree);
res = tinf_inflate_block_data(d, d.ltree, d.dtree);
break;
default:
res = TINF_DATA_ERROR;
}
if (res !== TINF_OK)
throw new Error('Data error');
} while (!bfinal);
if (d.destLen >= d.dest.length)
return null
if (d.destLen < d.dest.length) {
if (typeof d.dest.slice === 'function')
return d.dest.slice(0, d.destLen);
else
return d.dest.subarray(0, d.destLen);
}
return d.dest;
}
/* -------------------- *
* -- initialization -- *
* -------------------- */
/* build fixed huffman trees */
tinf_build_fixed_trees(sltree, sdtree);
/* build extra bits and base tables */
tinf_build_bits_base(length_bits, length_base, 4, 3);
tinf_build_bits_base(dist_bits, dist_base, 2, 1);
/* fix a special case */
length_bits[28] = 0;
length_base[28] = 258;
}

View File

@ -90,36 +90,172 @@ namespace pxsim.screen {
screenState.blitLineCore(XX(xw), y, YY(xw), buf, mode)
}
export function isValidImage(buf: RefBuffer) {
return buf.data.length >= 3 && buf.data[0] == 0xf0;
}
export function PIX2BYTES(x: number) {
return ((x + 7) >> 3)
}
export function clear(): void {
const screenState = (board() as DalBoard).screenState;
screenState.clear()
}
export function dump() {
// No need for this one.
}
export function imageOf(buf: RefBuffer) {
return incr(buf)
}
}
namespace pxsim.screen {
function DMESG(msg: string) {
control.dmesg(msg)
}
const NULL: RefBuffer = null;
function revbits(v: number) {
v = (v & 0xf0) >> 4 | (v & 0x0f) << 4;
v = (v & 0xcc) >> 2 | (v & 0x33) << 2;
v = (v & 0xaa) >> 1 | (v & 0x55) << 1;
return v;
}
export function unpackPNG(png: RefBuffer) {
function memcmp(off: number, mark: string) {
for (let i = 0; i < mark.length; ++i) {
if (mark.charCodeAt(i) != png.data[off + i])
return 1
}
return 0
}
function readInt(off: number) {
return ((png.data[off] << 24) | (png.data[off + 1] << 16) |
(png.data[off + 2] << 8) | (png.data[off + 3])) >>> 0
}
if (!png) {
DMESG("PNG: Missing image");
return NULL;
}
if (png.data.length < 45) {
DMESG("PNG: File too small");
return NULL;
}
if (memcmp(0, "\x89PNG\r\n\x1A\n") != 0) {
DMESG("PNG: Invalid header");
return NULL;
}
if (memcmp(12, "IHDR") != 0) {
DMESG("PNG: missing IHDR");
return NULL;
}
const lenIHDR = readInt(8);
const width = readInt(16);
const height = readInt(20);
const lenIDAT = readInt(33);
const sizeOfHD = 41;
if (lenIHDR != 13) {
DMESG("PNG: bad IHDR len");
return NULL;
}
if (memcmp(24, "\x01\x00\x00\x00\x00") != 0) {
DMESG("PNG: not 1-bit grayscale");
return NULL;
}
if (memcmp(37, "IDAT") != 0) {
DMESG("PNG: missing IDAT");
return NULL;
}
if (lenIDAT + sizeOfHD >= png.data.length) {
DMESG("PNG: buffer too short");
return NULL;
}
if (width > 300 || height > 300) {
DMESG("PNG: too big");
return NULL;
}
const byteW = (width + 7) >> 3;
const sz = (byteW + 1) * height;
const tmp = new Uint8Array(sz + 1);
// uncompress doesn't take the zlib header, hence + 2
const two = tinf.uncompress(png.data.slice(sizeOfHD + 2, sizeOfHD + lenIDAT), tmp);
if (two.length != sz) {
DMESG("PNG: invalid compressed size");
return NULL;
}
const res = output.createBuffer(2 + byteW * height);
res.data[0] = 0xf0;
res.data[1] = width;
let dst = 2
let src = 0
let lastMask = (1 << (width & 7)) - 1;
if (lastMask == 0)
lastMask = 0xff;
for (let i = 0; i < height; ++i) {
if (two[src++] != 0) {
DMESG("PNG: unsupported filter");
decr(res);
return NULL;
}
for (let j = 0; j < byteW; ++j) {
res.data[dst] = ~revbits(two[src++]);
if (j == byteW - 1) {
res.data[dst] &= lastMask;
}
dst++;
}
}
return res;
}
}
namespace pxsim.ImageMethods {
const bitdouble = [
0x00, 0x03, 0x0c, 0x0f, 0x30, 0x33, 0x3c, 0x3f, 0xc0, 0xc3, 0xcc, 0xcf, 0xf0, 0xf3, 0xfc, 0xff,
]
export function isValidIcon(buf: RefBuffer) {
return buf.data.length >= 3 && buf.data[0] == 0xf0;
export function buffer(buf: RefBuffer) {
return incr(buf)
}
function PIX2BYTES(x: number) {
return ((x + 7) >> 3)
export function width(buf: RefBuffer) {
if (!screen.isValidImage(buf)) return 0
return buf.data[1]
}
export function drawIcon(x: number, y: number, buf: RefBuffer, mode: Draw): void {
export function height(buf: RefBuffer) {
if (!screen.isValidImage(buf)) return 0
const bw = screen.PIX2BYTES(buf.data[1]);
const h = ((buf.data.length - 2) / bw) | 0;
return h
}
export function draw(buf: RefBuffer, x: number, y: number, mode: Draw): void {
const screenState = (board() as DalBoard).screenState;
if (!isValidIcon(buf))
if (!screen.isValidImage(buf))
return;
if (mode & (Draw.Double | Draw.Quad)) {
buf = doubleIcon(buf);
buf = doubled(buf);
if (mode & Draw.Quad) {
let pbuf = buf;
buf = doubleIcon(buf);
buf = doubled(buf);
decr(pbuf);
}
}
let pixwidth = buf.data[1];
let ptr = 2;
const bytewidth = PIX2BYTES(pixwidth);
const bytewidth = screen.PIX2BYTES(pixwidth);
pixwidth = Math.min(pixwidth, visuals.SCREEN_WIDTH);
while (ptr + bytewidth <= buf.data.length) {
if (mode & (Draw.Clear | Draw.Xor | Draw.Transparent)) {
@ -132,24 +268,19 @@ namespace pxsim.screen {
ptr += bytewidth;
}
if (mode & Draw.Double)
if (mode & (Draw.Double | Draw.Quad))
decr(buf);
}
export function clear(): void {
const screenState = (board() as DalBoard).screenState;
screenState.clear()
}
export function doubleIcon(buf: RefBuffer): RefBuffer {
if (!isValidIcon(buf))
export function doubled(buf: RefBuffer): RefBuffer {
if (!screen.isValidImage(buf))
return null;
const w = buf.data[1];
if (w > 126)
return null;
const bw = PIX2BYTES(w);
const bw = screen.PIX2BYTES(w);
const h = ((buf.data.length - 2) / bw) | 0;
const bw2 = PIX2BYTES(w * 2);
const bw2 = screen.PIX2BYTES(w * 2);
const out = pins.createBuffer(2 + bw2 * h * 2)
out.data[0] = 0xf0;
out.data[1] = w * 2;
@ -169,7 +300,5 @@ namespace pxsim.screen {
return out;
}
export function dump() {
// do we need it?
}
}
}