コード例 #1
0
  int getNextValue(PngDecodingDataStream stream) throws IOException {
    int code = stream.getNextIdatBit();
    int codelength = 0;

    // Here we are taking advantage of the fact that 1 bits are used as
    // a prefix to the longer codeValues.
    while (codelength < MAX_CODE_LENGTH && code > codeLengthInfo[codelength].max) {
      code = ((code << 1) | stream.getNextIdatBit());
      codelength++;
    }
    if (codelength >= MAX_CODE_LENGTH) stream.error();

    // Now we have a Huffman code of length (codelength + 1) that
    // is somewhere in the range
    // minCodesByLength[codelength]..maxCodesByLength[codelength].
    // This code is the (offset + 1)'th code of (codelength + 1);
    int offset = code - codeLengthInfo[codelength].min;

    // indexesByLength[codelength] is the first code of length (codelength + 1)
    // so now we can look up the value for the Huffman code in the table.
    int index = codeLengthInfo[codelength].baseIndex + offset;
    return codeValues[index];
  }