/* *Very* simple de/compression utility, based on simple_c and simple_d from * rngcod13.zip at http://www.compressconsult.com/rangecoder/ * Really just a demonstration/test of the rangecoder. */ if (typeof define !== 'function') { var define = require('amdefine')(module); } define(['./RangeCoder','./Stream','./Util'],function(RangeCoder,Stream,Util){ var MAX_BLOCK_SIZE = 1<<17; var Simple = Object.create(null); Simple.MAGIC = 'smpl'; Simple.compressFile = Util.compressFileHelper(Simple.MAGIC, function(input, output, size, props, finalByte) { var encoder = new RangeCoder(output); encoder.encodeStart(finalByte, 1); // read a block var block = Util.makeU8Buffer(MAX_BLOCK_SIZE); var counts = []; var blockLength = 0, sawEOF = false; var readBlock = function() { var pos = 0; // initialize counts for (pos=0; pos < 256; pos++) { counts[pos] = 0; } if (sawEOF) { blockLength = 0; return; } for (pos=0; pos < MAX_BLOCK_SIZE; ) { var c = input.readByte(); if (c===Stream.EOF) { sawEOF = true; break; } block[pos++] = c; counts[c]++; // bail if some count reaches maximum if (counts[c]===0xFFFF) { break; } } blockLength = pos; }; while (true) { var i; readBlock(); if (sawEOF && blockLength===0) { break; } // indicate that there's another block comin' encoder.encodeBit(true); // write all the statistics for (i=0; i<256; i++) { encoder.encodeShort(counts[i]); } // convert counts to cumulative counts counts[256] = blockLength; for (i=256; i; i--) { counts[i-1] = counts[i] - counts[i-1]; } // encode the symbols using the probability table. for (i=0; i