unpackEncTable static method

void unpackEncTable(
  1. InputBuffer p,
  2. int ni,
  3. int im,
  4. int iM,
  5. List<int> hcode,
)

Implementation

static void unpackEncTable(
    InputBuffer p, int ni, int im, int iM, List<int> hcode) {
  final pcode = p.offset;
  final c_lc = [0, 0];

  for (; im <= iM; im++) {
    if (p.offset - pcode > ni) {
      throw ImageException('Error in Huffman-encoded data '
          '(unexpected end of code table data).');
    }

    final l = hcode[im] = getBits(6, c_lc, p); // code length

    if (l == LONG_ZEROCODE_RUN) {
      if (p.offset - pcode > ni) {
        throw ImageException('Error in Huffman-encoded data '
            '(unexpected end of code table data).');
      }

      var zerun = getBits(8, c_lc, p) + SHORTEST_LONG_RUN;

      if (im + zerun > iM + 1) {
        throw ImageException('Error in Huffman-encoded data '
            '(code table is longer than expected).');
      }

      while (zerun-- != 0) {
        hcode[im++] = 0;
      }

      im--;
    } else if (l >= SHORT_ZEROCODE_RUN) {
      var zerun = l - SHORT_ZEROCODE_RUN + 2;

      if (im + zerun > iM + 1) {
        throw ImageException('Error in Huffman-encoded data '
            '(code table is longer than expected).');
      }

      while (zerun-- != 0) {
        hcode[im++] = 0;
      }

      im--;
    }
  }

  canonicalCodeTable(hcode);
}