huffarray.js
/**
     * Initialize the decoder with the 'BITS' array containing the number of huffman codes
     * of each length.
     * 
     * @param {Array} bits List of the number of Huffman codes of each length (0-NUM_HUFFMAN_LENGTHS)
     */
Initialize the decoder with the 'BITS' array containing the number of huffman codes
of each length.

@param {Array} bits List of the number of Huffman codes of each length (0-NUM_HUFFMAN_LENGTHS)
Initialize the decoder with the ' BITS ' array containing the number of huffman codes of each length . @param { Array } bits List of the number of Huffman codes of each length ( 0 - NUM_HUFFMAN_LENGTHS )
initDecoder(bits) {
        this.bits = bits;
        let k = 0;
        let lastk = 0;
        for (let i = 1; i <= NUM_HUFFMAN_LENGTHS; i++) {
            for (let j = 1; j <= this.bits[i]; j++) {
                this.huffsize[k++] = i;
            }
        }
        this.huffsize[k] = 0;
        lastk = k;
        console.log("HUFFSIZE: " + this.huffsize);
        k = 0;
        let code = 0;
        let si = this.huffsize[0];
        while (this.huffsize[k] != 0) {
            while (this.huffsize[k] === si) {
                this.huffcode[k++] = code++;
            }
            code = code << 1;
            si++;
        }
        console.log("HUFFCODE: " + this.huffcode.map(x => x.toString(2)));
    }
initDecoder ( bits ) { this . bits = bits ; let k = 0 ; let lastk = 0 ; for ( let i = 1 ; i <= NUM_HUFFMAN_LENGTHS ; i ++ ) { for ( let j = 1 ; j <= this . bits [ i ] ; j ++ ) { this . huffsize [ k ++ ] = i ; } } this . huffsize [ k ] = 0 ; lastk = k ; console . log ( "HUFFSIZE: " + this . huffsize ) ; k = 0 ; let code = 0 ; let si = this . huffsize [ 0 ] ; while ( this . huffsize [ k ] != 0 ) { while ( this . huffsize [ k ] === si ) { this . huffcode [ k ++ ] = code ++ ; } code = code << 1 ; si ++ ; } console . log ( "HUFFCODE: " + this . huffcode . map ( x => x . toString ( 2 ) ) ) ; }
Initialize the decoder with the 'BITS' array containing the number of huffman codes
 of each length.
Initialize the decoder with the ' BITS ' array containing the number of huffman codes of each length .
// Generate code sizes (JPEG Spec Fig. C.1) // Generate codes (JPEG Spec Fig. C.2) // Note: this incorrectly drops leading zeros, for example the huffcode '001' is printed as '1'