void initTable(UInt32 huffSelect) { HuffmanTable* dctbl1 = &huff[0]; UInt32 acc = 0; for (UInt32 i = 0; i < 16; i++) { dctbl1.bits[i + 1] = nikon_tree[huffSelect][i]; acc += dctbl1.bits[i + 1]; } dctbl1.bits[0] = 0; for (UInt32 i = 0; i < acc; i++) { dctbl1.huffval[i] = nikon_tree[huffSelect][i + 16]; } createHuffmanTable(dctbl1); }
/* *-------------------------------------------------------------- * * HuffDecode -- * * Taken from Figure F.16: extract next coded symbol from * input stream. This should becode a macro. * * Results: * Next coded symbol * * Side effects: * Bitstream is parsed. * *-------------------------------------------------------------- */ int HuffDecodeNikon(BitPumpMSB bits) { int rv; int l, temp; int code, val; HuffmanTable* dctbl1 = &huff[0]; bits.fill(); code = bits.peekBitsNoFill(14); val = dctbl1.bigTable[code]; if ((val & 0xff) != 0xff) { bits.skipBitsNoFill(val & 0xff); return val >> 8; } rv = 0; code = bits.peekByteNoFill(); val = dctbl1.numbits[code]; l = val & 15; if (l) { bits.skipBitsNoFill(l); rv = val >> 4; } else { bits.skipBits(8); l = 8; while (code > dctbl1.maxcode[l]) { temp = bits.getBitNoFill(); code = (code << 1) | temp; l++; } if (l > 16) { throw new Exception("Corrupt JPEG data: bad Huffman code:%u\n", l); } else { rv = dctbl1.huffval[dctbl1.valptr[l] + ((int)(code - dctbl1.((code[l]))];
private static unsafe uint CreateDecodeTable(HuffmanTable *pTable, uint SymbolsCount, byte *pSymbolsLengths) { // --------- Sort by length the symbol table (counter sort) byte *LengthOccuranceCount = stackalloc byte[CodeMaxBits + 1]; //memset( LengthOccuranceCount, 0, sizeof ( LengthOccuranceCount ) ); // --------- CollectSymbolLengths byte *pSymbolLength = pSymbolsLengths; for (uint Counter = SymbolsCount; Counter > 0; --Counter) { ++LengthOccuranceCount[*pSymbolLength++]; } // --------- SortSymbolLengthsPositions HuffmanTableNode *SymbolLengthsList = stackalloc HuffmanTableNode[Literals]; // the symbols, sorted by length HuffmanTableNode *[] pSymbolLengthPosition = new HuffmanTableNode *[CodeMaxBits + 1]; // pointers to the first entry in to table above, // for each symbol length HuffmanTableNode *pLastPosition = SymbolLengthsList; pTable->SmallestLength = CodeMaxBits + 1; // set max value pTable->LargestLength = 0; for (uint Counter = 1; Counter <= CodeMaxBits; ++Counter) // NOTE: starts at 1 as there're no 0-length symbols { pSymbolLengthPosition[Counter] = pLastPosition; pLastPosition += LengthOccuranceCount[Counter]; // Also find the smallest and the largest codelength if (LengthOccuranceCount[Counter] != 0) { if (Counter < pTable->SmallestLength) { pTable->SmallestLength = Counter; } if (Counter > pTable->LargestLength) { pTable->LargestLength = Counter; } } } // ----------- CreateSortedSymbolList uint UsedSymbolsCount = 0; pSymbolLength = pSymbolsLengths; for (uint Counter = 0; Counter < SymbolsCount; ++Counter, ++pSymbolLength) { if (*pSymbolLength != 0) { HuffmanTableNode *pSymbolTableNode = pSymbolLengthPosition[*pSymbolLength]++; pSymbolTableNode->Value = (ushort)Counter; pSymbolTableNode->Length = *pSymbolLength; } } UsedSymbolsCount = (uint)(pSymbolLengthPosition[CodeMaxBits] - SymbolLengthsList); // ---------------------------------------------------------- // Two tables are needed to decode a symbol: // - The first one will lookup the symbol's first // PrimaryTableBits bits. Statistically, there are many // chances that the lookup will end here. // - If the symbol code is longer, a second table is needed. // Several secondary tables may be created. int CodeDecal = 1 << (int)(PrimaryTableBits - pTable->SmallestLength); HuffmanTableNode *pCurrentTableNode = pTable->pRoot; HuffmanTableNode *pCurrentSymbol = SymbolLengthsList; byte * pCurrentLengthOccurence = LengthOccuranceCount + pTable->SmallestLength; for (; CodeDecal != 0 && pCurrentLengthOccurence <= LengthOccuranceCount + pTable->LargestLength; CodeDecal >>= 1) { for (uint SymbolsLeft = *pCurrentLengthOccurence++; SymbolsLeft > 0; --SymbolsLeft, ++pCurrentSymbol) { // ------------- Fill table for (uint Counter = (uint)CodeDecal; Counter > 0; --Counter) { *pCurrentTableNode++ = *pCurrentSymbol; } } } if (pTable->LargestLength <= PrimaryTableBits) { return(1 << PrimaryTableBits); } else // -------------------------------------- Fill the secondary tables { HuffmanTableNode *pNextTableNode = pTable->pRoot + (1 << PrimaryTableBits); // uint RemainingBits = pTable->LargestLength - PrimaryTableBits; uint InitialRemainingBits = RemainingBits; pCurrentLengthOccurence = LengthOccuranceCount + pTable->LargestLength; uint CurrentSymbolCode = (1u << (int)pTable->LargestLength) - 1; CodeDecal = 0; pCurrentSymbol = SymbolLengthsList + UsedSymbolsCount - 1; pCurrentTableNode = pNextTableNode; for (; RemainingBits != 0; CurrentSymbolCode >>= 1, --RemainingBits, ++CodeDecal) { for (uint SymbolsLeft = *pCurrentLengthOccurence--; SymbolsLeft > 0; --SymbolsLeft, --pCurrentSymbol) { for (uint Counter = 1u << (int)CodeDecal; Counter > 0; --Counter) { *pNextTableNode++ = *pCurrentSymbol; } uint LastSymbolCode = CurrentSymbolCode >> (int)RemainingBits; --CurrentSymbolCode; if (((CurrentSymbolCode >> (int)RemainingBits) ^ LastSymbolCode) != 0)// top PrimaryTableBits bits changed? { --pCurrentTableNode; pCurrentTableNode->Length = (byte)(32 - InitialRemainingBits); // extra bits that need reading pCurrentTableNode->Value = (ushort)(pNextTableNode - pCurrentTableNode - 1); // offset to first entry in the secondary table CodeDecal = SymbolsLeft == 1 ? -1 : 0; InitialRemainingBits = SymbolsLeft == 1 ? RemainingBits - 1 : RemainingBits; } } } return((uint)(pNextTableNode - pTable->pRoot)); } }