📄 huffd.c
字号:
((-1) << 13) + 1, ((-1) << 14) + 1, ((-1) << 15) + 1};#define HuffExtend(x,s) { \ if ((x) < extendTest[s]) { \ (x) += extendOffset[s]; \ } \}/* *-------------------------------------------------------------- * * HuffDecoderInit -- * * Initialize for a Huffman-compressed scan. * This is invoked after reading the SOS marker. * * Results: * None * * Side effects: * None. * *-------------------------------------------------------------- */void HuffDecoderInit (dcPtr) DecompressInfo *dcPtr;{ short ci; JpegComponentInfo *compptr; /* * Initialize static variables */ bitsLeft = 0; for (ci = 0; ci < dcPtr->compsInScan; ci++) { compptr = dcPtr->curCompInfo[ci]; /* * Make sure requested tables are present */ if (dcPtr->dcHuffTblPtrs[compptr->dcTblNo] == NULL) { fprintf (stderr, "Error: Use of undefined Huffman table\n"); exit (1); } /* * Compute derived values for Huffman tables. * We may do this more than once for same table, but it's not a * big deal */ FixHuffTbl (dcPtr->dcHuffTblPtrs[compptr->dcTblNo]); } /* * Initialize restart stuff */ dcPtr->restartInRows = (dcPtr->restartInterval)/(dcPtr->imageWidth); dcPtr->restartRowsToGo = dcPtr->restartInRows; dcPtr->nextRestartNum = 0;}/* *-------------------------------------------------------------- * * ProcessRestart -- * * Check for a restart marker & resynchronize decoder. * * Results: * None. * * Side effects: * BitStream is parsed, bit buffer is reset, etc. * *-------------------------------------------------------------- */static void ProcessRestart (dcPtr) DecompressInfo *dcPtr;{ int c, nbytes; short ci; /* * Throw away any unused bits remaining in bit buffer */ nbytes = bitsLeft / 8; bitsLeft = 0; /* * Scan for next JPEG marker */ do { do { /* skip any non-FF bytes */ nbytes++; c = GetJpegChar (); } while (c != 0xFF); do { /* skip any duplicate FFs */ /* * we don't increment nbytes here since extra FFs are legal */ c = GetJpegChar (); } while (c == 0xFF); } while (c == 0); /* repeat if it was a stuffed FF/00 */ if (c != (RST0 + dcPtr->nextRestartNum)) { /* * Uh-oh, the restart markers have been messed up too. * Just bail out. */ fprintf (stderr, "Error: Corrupt JPEG data. Exiting...\n"); exit(-1); } /* * Update restart state */ dcPtr->restartRowsToGo = dcPtr->restartInRows; dcPtr->nextRestartNum = (dcPtr->nextRestartNum + 1) & 7;}/* *-------------------------------------------------------------- * * DecodeFirstRow -- * * Decode the first raster line of samples at the start of * the scan and at the beginning of each restart interval. * This includes modifying the component value so the real * value, not the difference is returned. * * Results: * None. * * Side effects: * Bitstream is parsed. * *-------------------------------------------------------------- */void DecodeFirstRow(dcPtr,curRowBuf) DecompressInfo *dcPtr; MCU *curRowBuf;{ register short curComp,ci; register int s,col,compsInScan,numCOL; register JpegComponentInfo *compptr; int Pr,Pt,d; HuffmanTable *dctbl; Pr=dcPtr->dataPrecision; Pt=dcPtr->Pt; compsInScan=dcPtr->compsInScan; numCOL=dcPtr->imageWidth; /* * the start of the scan or at the beginning of restart interval. */ for (curComp = 0; curComp < compsInScan; curComp++) { ci = dcPtr->MCUmembership[curComp]; compptr = dcPtr->curCompInfo[ci]; dctbl = dcPtr->dcHuffTblPtrs[compptr->dcTblNo]; /* * Section F.2.2.1: decode the difference */ HuffDecode (dctbl,s); if (s) { get_bits(s,d); HuffExtend(d,s); } else { d = 0; } /* * Add the predictor to the difference. */ curRowBuf[0][curComp]=d+(1<<(Pr-Pt-1)); } /* * the rest of the first row */ for (col=1; col<numCOL; col++) { for (curComp = 0; curComp < compsInScan; curComp++) { ci = dcPtr->MCUmembership[curComp]; compptr = dcPtr->curCompInfo[ci]; dctbl = dcPtr->dcHuffTblPtrs[compptr->dcTblNo]; /* * Section F.2.2.1: decode the difference */ HuffDecode (dctbl,s); if (s) { get_bits(s,d); HuffExtend(d,s); } else { d = 0; } /* * Add the predictor to the difference. */ curRowBuf[col][curComp]=d+curRowBuf[col-1][curComp]; } } if (dcPtr->restartInRows) { (dcPtr->restartRowsToGo)--; }}/* *-------------------------------------------------------------- * * DecodeImage -- * * Decode the input stream. This includes modifying * the component value so the real value, not the * difference is returned. * * Results: * None. * * Side effects: * Bitstream is parsed. * *-------------------------------------------------------------- */voidDecodeImage(dcPtr) DecompressInfo *dcPtr;{ register int s,d,col,row; register short curComp, ci; HuffmanTable *dctbl; JpegComponentInfo *compptr; int predictor; int numCOL,numROW,compsInScan; MCU *prevRowBuf,*curRowBuf; int imagewidth,Pt,psv; numCOL=imagewidth=dcPtr->imageWidth; numROW=dcPtr->imageHeight; compsInScan=dcPtr->compsInScan; Pt=dcPtr->Pt; psv=dcPtr->Ss; prevRowBuf=mcuROW2; curRowBuf=mcuROW1; /* * Decode the first row of image. Output the row and * turn this row into a previous row for later predictor * calculation. */ DecodeFirstRow(dcPtr,curRowBuf); PmPutRow(curRowBuf,compsInScan,numCOL,Pt); swap(MCU *,prevRowBuf,curRowBuf); for (row=1; row<numROW; row++) { /* * Account for restart interval, process restart marker if needed. */ if (dcPtr->restartInRows) { if (dcPtr->restartRowsToGo == 0) { ProcessRestart (dcPtr); /* * Reset predictors at restart. */ DecodeFirstRow(dcPtr,curRowBuf); PmPutRow(curRowBuf,compsInScan,numCOL,Pt); swap(MCU *,prevRowBuf,curRowBuf); continue; } dcPtr->restartRowsToGo--; } /* * The upper neighbors are predictors for the first column. */ for (curComp = 0; curComp < compsInScan; curComp++) { ci = dcPtr->MCUmembership[curComp]; compptr = dcPtr->curCompInfo[ci]; dctbl = dcPtr->dcHuffTblPtrs[compptr->dcTblNo]; /* * Section F.2.2.1: decode the difference */ HuffDecode (dctbl,s); if (s) { get_bits(s,d); HuffExtend(d,s); } else { d = 0; } curRowBuf[0][curComp]=d+prevRowBuf[0][curComp]; } /* * For the rest of the column on this row, predictor * calculations are base on PSV. */ for (col=1; col<numCOL; col++) { for (curComp = 0; curComp < compsInScan; curComp++) { ci = dcPtr->MCUmembership[curComp]; compptr = dcPtr->curCompInfo[ci]; dctbl = dcPtr->dcHuffTblPtrs[compptr->dcTblNo]; /* * Section F.2.2.1: decode the difference */ HuffDecode (dctbl,s); if (s) { get_bits(s,d); HuffExtend(d,s); } else { d = 0; } QuickPredict(col,curComp,curRowBuf,prevRowBuf, psv,&predictor); curRowBuf[col][curComp]=d+predictor; } } PmPutRow(curRowBuf,compsInScan,numCOL,Pt); swap(MCU *,prevRowBuf,curRowBuf); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -