📄 huffd.c
字号:
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); */ dcPtr->error = -1; return; } /* * 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 (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)--; }}/*endof DecodeFirstRow*//* *-------------------------------------------------------------- * * 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. * *-------------------------------------------------------------- */void DecodeImage (DecompressInfo *dcPtr, unsigned short **image, int depth){ 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; unsigned short *image16tmp; unsigned char *image8tmp, *image24tmp; numCOL = imagewidth=dcPtr->imageWidth; numROW = dcPtr->imageHeight; compsInScan = dcPtr->compsInScan; Pt = dcPtr->Pt; psv = dcPtr->Ss; prevRowBuf = mcuROW2; curRowBuf = mcuROW1; if (depth == 8) image8tmp = (unsigned char *) *image; else if (depth == 16) image16tmp = (unsigned short *) *image; else if (depth == 24) image24tmp = (unsigned char *) *image; else { fprintf(stderr,"Unsupported image depth %d\n",depth); dcPtr->error = -1; return; } /* * Decode the first row of image. Output the row and * turn this row into a previous row for later predictor * calculation. */ row = 0; DecodeFirstRow (dcPtr, curRowBuf); if (depth == 8) PmPutRow8 (curRowBuf, numCOL, &image8tmp); else if (depth == 16) PmPutRow16 (curRowBuf, numCOL, &image16tmp); else if (depth == 24) PmPutRow24 (curRowBuf, numCOL, &image24tmp); swap(MCU *, prevRowBuf, curRowBuf); /* optimal case : 8 bit image, one color component, no restartInRows */ if ((depth == 8) && (compsInScan == 1) && (dcPtr->restartInRows == 0)) { unsigned char *curPixelPtr; int left,upper,diag; /* initializations */ curComp = 0; ci = dcPtr->MCUmembership[curComp]; compptr = dcPtr->curCompInfo[ci]; dctbl = dcPtr->dcHuffTblPtrs[compptr->dcTblNo]; curPixelPtr = image8tmp; for (row=1; row<numROW; row++) { /* Upper neighbor is predictor for the first column */ /* Section F.2.2.1: decode the difference */ HuffDecode (dctbl,s); if (s) { get_bits(s,d); HuffExtend(d,s); } else d = 0; *curPixelPtr = (unsigned char) (d + *(curPixelPtr - numCOL)); curPixelPtr++; if (psv == 1) { for (col=1; col < numCOL; col++) { /* Section F.2.2.1: decode the difference */ HuffDecode (dctbl, s); if (s) { get_bits (s, d); HuffExtend (d, s); } else d = 0; *curPixelPtr = (unsigned char) (d + *(curPixelPtr - 1)); curPixelPtr++; }/*endfor col*/ }/*endif*/ else { for (col=1; col < numCOL; col++) { /* Section F.2.2.1: decode the difference */ HuffDecode (dctbl, s); if (s) { get_bits (s, d); HuffExtend (d, s); } else d = 0; /* Predict : All predictors are calculated according to psv */ switch (psv) { case 0: predictor = 0; break; case 2: predictor = *(curPixelPtr - numCOL); break; case 3: predictor = *(curPixelPtr - numCOL - 1); break; case 4: upper = *(curPixelPtr - numCOL); left = *(curPixelPtr - 1); diag = *(curPixelPtr - numCOL - 1); predictor = left + upper - diag; break; case 5: upper = *(curPixelPtr - numCOL); left = *(curPixelPtr - 1); diag = *(curPixelPtr - numCOL - 1); predictor = left+((upper-diag)>>1); break; case 6: upper = *(curPixelPtr - numCOL); left = *(curPixelPtr - 1); diag = *(curPixelPtr - numCOL - 1); predictor = upper+((left-diag)>>1); break; case 7: upper = *(curPixelPtr - numCOL); left = *(curPixelPtr - 1); predictor = (left+upper)>>1; break; default : predictor = 0; }/*endsandwich*/ *curPixelPtr = (unsigned char) (d + predictor); curPixelPtr++; }/*endfor col*/ }/*endelse*/ }/*endfor row*/ }/*endif fast case*/ else { /*normal case with 16 bits or color or ...*/ for (row=1; row<numROW; row++) { /* * Account for restart interval, process restart marker if needed. */ if (dcPtr->restartInRows) { if (dcPtr->restartRowsToGo == 0) { ProcessRestart (dcPtr); if (dcPtr->error) return; /* * Reset predictors at restart. */ DecodeFirstRow(dcPtr,curRowBuf); if (depth == 8) PmPutRow8 (curRowBuf, numCOL, &image8tmp); else if (depth == 16) PmPutRow16 (curRowBuf, numCOL, &image16tmp); else if (depth == 24) PmPutRow24 (curRowBuf, numCOL, &image24tmp); swap(MCU *,prevRowBuf,curRowBuf); continue; } dcPtr->restartRowsToGo--; }/*endif*/ /* * For the rest of the column on this row, predictor * calculations are base on PSV. */ /* several color components to decode (RGB colors)*/ /* 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]; }/*endfor curComp*/ 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; }/*endfor curComp*/ }/*endfor col*/ if (depth == 8) PmPutRow8 (curRowBuf, numCOL, &image8tmp); else if (depth == 16) PmPutRow16 (curRowBuf, numCOL, &image16tmp); else if (depth == 24) PmPutRow24 (curRowBuf, numCOL, &image24tmp); swap(MCU *, prevRowBuf, curRowBuf); }/*endfor row*/ }/*endelse*/}/*endofmethod DecodeImage*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -