all files / core/compression/ huffman-tree.js

15.65% Statements 18/115
0% Branches 0/28
16.67% Functions 2/12
15.65% Lines 18/115
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162                                                                                                                                                                                                                                                                                                
define(["require", "exports", "./../utils"], function (require, exports, utils_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var _HuffmanTree = (function () {
        function _HuffmanTree() {
        }
        _HuffmanTree.prototype._load = function (code) {
            this._clArray = code;
            this._initialize();
        };
        _HuffmanTree.prototype._loadTree = function (isLengthTree) {
            this._clArray = isLengthTree ? this._getLengthTree() : this._getDepthTree();
            this._initialize();
        };
        _HuffmanTree.prototype._initialize = function () {
            if (this._clArray.length === _HuffmanTree._maxLengthTree) {
                this._tBits = 9;
            }
            else {
                this._tBits = 7;
            }
            this._tMask = (1 << this._tBits) - 1;
            this._createTable();
        };
        _HuffmanTree.prototype._getLengthTree = function () {
            var lTree = Array(_HuffmanTree._maxLengthTree).fill(0);
            for (var i = 0; i <= 143; i++) {
                lTree[i] = utils_1._toUnsigned(8, 8);
            }
            for (var i = 144; i <= 255; i++) {
                lTree[i] = utils_1._toUnsigned(9, 8);
            }
            for (var i = 256; i <= 279; i++) {
                lTree[i] = utils_1._toUnsigned(7, 8);
            }
            for (var i = 280; i <= 287; i++) {
                lTree[i] = utils_1._toUnsigned(8, 8);
            }
            return lTree;
        };
        _HuffmanTree.prototype._getDepthTree = function () {
            return Array(_HuffmanTree._maxDepthTree).fill(5);
        };
        _HuffmanTree.prototype._calculateHashCode = function () {
            var bit = Array(17).fill(0);
            for (var i = 0; i < this._clArray.length; i++) {
                bit[this._clArray[i]]++;
            }
            bit[0] = 0;
            var next = Array(17).fill(0);
            var temp = 0;
            for (var bits = 1; bits <= 16; bits++) {
                temp = (temp + bit[bits - 1]) << 1;
                next[bits] = temp;
            }
            var code = Array(_HuffmanTree._maxLengthTree).fill(0);
            for (var i = 0; i < this._clArray.length; i++) {
                var len = this._clArray[i];
                if (len > 0) {
                    code[i] = this._bitReverse(next[len], len);
                    next[len]++;
                }
            }
            return code;
        };
        _HuffmanTree.prototype._bitReverse = function (code, length) {
            var newcode = 0;
            do {
                newcode |= code & 1;
                newcode <<= 1;
                code >>= 1;
            } while (--length > 0);
            return newcode >> 1;
        };
        _HuffmanTree.prototype._createTable = function () {
            var codeArray = this._calculateHashCode();
            this._table = Array(1 << this._tBits).fill(0);
            this._left = Array(2 * this._clArray.length).fill(0);
            this._right = Array(2 * this._clArray.length).fill(0);
            var avail = utils_1._toSigned16(this._clArray.length);
            for (var ch = 0; ch < this._clArray.length; ch++) {
                var len = this._clArray[ch];
                if (len > 0) {
                    var start = codeArray[ch];
                    if (len <= this._tBits) {
                        var i = 1 << len;
                        if (start >= i) {
                            throw new Error('Invalid Data.');
                        }
                        var l = 1 << (this._tBits - len);
                        for (var j = 0; j < l; j++) {
                            this._table[start] = utils_1._toSigned16(ch);
                            start += i;
                        }
                    }
                    else {
                        var ofBits = len - this._tBits;
                        var bitMask = 1 << this._tBits;
                        var index = start & ((1 << this._tBits) - 1);
                        var array = this._table;
                        do {
                            var value = utils_1._toSigned16(array[index]);
                            if (value === 0) {
                                array[index] = utils_1._toSigned16(-avail);
                                value = utils_1._toSigned16(-avail);
                                avail++;
                            }
                            if (value > 0) {
                                throw new Error('Invalid Data.');
                            }
                            if ((start & bitMask) === 0) {
                                array = this._left;
                            }
                            else {
                                array = this._right;
                            }
                            index = -value;
                            bitMask <<= 1;
                            ofBits--;
                        } while (ofBits !== 0);
                        array[index] = utils_1._toSigned16(ch);
                    }
                }
            }
        };
        _HuffmanTree.prototype._getNextSymbol = function (input) {
            var bitBuffer = input._load16Bits();
            if (input._bInBuffer === 0) {
                return -1;
            }
            var symbol = this._table[bitBuffer & this._tMask];
            if (symbol < 0) {
                var mask = utils_1._toUnsigned((1 << this._tBits), 32);
                do {
                    symbol = -symbol;
                    if ((bitBuffer & mask) === 0) {
                        symbol = this._left[symbol];
                    }
                    else {
                        symbol = this._right[symbol];
                    }
                    mask <<= 1;
                } while (symbol < 0);
            }
            var codeLength = this._clArray[symbol];
            if (codeLength <= 0) {
                throw new Error('Invalid Data.');
            }
            if (codeLength > input._bInBuffer) {
                return -1;
            }
            input._skipBits(codeLength);
            return symbol;
        };
        _HuffmanTree._maxLengthTree = 288;
        _HuffmanTree._maxDepthTree = 32;
        _HuffmanTree._nCLength = 19;
        return _HuffmanTree;
    }());
    exports._HuffmanTree = _HuffmanTree;
});