decode static method

void decode(
  1. List<int> hcode,
  2. List<ExrHufDec> hdecod,
  3. InputBuffer input,
  4. int ni,
  5. int rlc,
  6. int no,
  7. Uint16List? out,
)

Implementation

static void decode(List<int> hcode, List<ExrHufDec> hdecod, InputBuffer input,
    int ni, int rlc, int no, Uint16List? out) {
  final c_lc = [0, 0];
  final ie = input.offset + (ni + 7) ~/ 8; // input byte size
  var oi = 0;

  // Loop on input bytes

  while (input.offset < ie) {
    getChar(c_lc, input);

    // Access decoding table
    while (c_lc[1] >= HUF_DECBITS) {
      final pl = hdecod[(c_lc[0] >> (c_lc[1] - HUF_DECBITS)) & HUF_DECMASK];

      if (pl.len != 0) {
        // Get short code
        c_lc[1] -= pl.len;
        oi = getCode(pl.lit, rlc, c_lc, input, out, oi, no);
      } else {
        if (pl.p == null) {
          throw ImageException('Error in Huffman-encoded data '
              '(invalid code).');
        }

        // Search long code
        int j;
        for (j = 0; j < pl.lit; j++) {
          final l = hufLength(hcode[pl.p![j]]);

          while (c_lc[1] < l && input.offset < ie) {
            // get more bits
            getChar(c_lc, input);
          }

          if (c_lc[1] >= l) {
            if (hufCode(hcode[pl.p![j]]) ==
                ((c_lc[0] >> (c_lc[1] - l)) & ((1 << l) - 1))) {
              // Found : get long code
              c_lc[1] -= l;
              oi = getCode(pl.p![j], rlc, c_lc, input, out, oi, no);
              break;
            }
          }
        }

        if (j == pl.lit) {
          throw ImageException('Error in Huffman-encoded data '
              '(invalid code).');
        }
      }
    }
  }

  // Get remaining (short) codes
  final i = (8 - ni) & 7;
  c_lc[0] >>= i;
  c_lc[1] -= i;

  while (c_lc[1] > 0) {
    final pl = hdecod[(c_lc[0] << (HUF_DECBITS - c_lc[1])) & HUF_DECMASK];

    if (pl.len != 0) {
      c_lc[1] -= pl.len;
      oi = getCode(pl.lit, rlc, c_lc, input, out, oi, no);
    } else {
      throw ImageException('Error in Huffman-encoded data '
          '(invalid code).');
    }
  }

  if (oi != no) {
    throw ImageException('Error in Huffman-encoded data '
        '(decoded data are shorter than expected).');
  }
}