?? jpegfileformat.java
字號:
int k = 1; while (k < 64) { int rs = decodeUsingTable(acTable); int r = rs >> 4; int s = rs & 0xF; if (s == 0) { if (r == 15) { k += 16; } else { break; } } else { k += r; int bits = receive(s); dataUnit[ZigZag8x8[k]] = extendBy(bits, s); k++; } }}void decodeACFirstCoefficients(int[] dataUnit, int iComp, int start, int end, int approxBit) { if (eobrun > 0) { eobrun--; return; } int[] sParams = scanHeader.componentParameters[componentIds[iComp]]; JPEGHuffmanTable acTable = acHuffmanTables[sParams[AC]]; int k = start; while (k <= end) { int rs = decodeUsingTable(acTable); int r = rs >> 4; int s = rs & 0xF; if (s == 0) { if (r == 15) { k += 16; } else { eobrun = (1 << r) + receive(r) - 1; break; } } else { k += r; int bits = receive(s); dataUnit[ZigZag8x8[k]] = extendBy(bits, s) << approxBit; k++; } }}void decodeACRefineCoefficients(int[] dataUnit, int iComp, int start, int end, int approxBit) { int[] sParams = scanHeader.componentParameters[componentIds[iComp]]; JPEGHuffmanTable acTable = acHuffmanTables[sParams[AC]]; int k = start; while (k <= end) { if (eobrun > 0) { while (k <= end) { int zzIndex = ZigZag8x8[k]; if (dataUnit[zzIndex] != 0) { dataUnit[zzIndex] = refineAC(dataUnit[zzIndex], approxBit); } k++; } eobrun--; } else { int rs = decodeUsingTable(acTable); int r = rs >> 4; int s = rs & 0xF; if (s == 0) { if (r == 15) { int zeros = 0; while (zeros < 16 && k <= end) { int zzIndex = ZigZag8x8[k]; if (dataUnit[zzIndex] != 0) { dataUnit[zzIndex] = refineAC(dataUnit[zzIndex], approxBit); } else { zeros++; } k++; } } else { eobrun = (1 << r) + receive(r); } } else { int bit = receive(s); int zeros = 0; int zzIndex = ZigZag8x8[k]; while ((zeros < r || dataUnit[zzIndex] != 0) && k <= end) { if (dataUnit[zzIndex] != 0) { dataUnit[zzIndex] = refineAC(dataUnit[zzIndex], approxBit); } else { zeros++; } k++; zzIndex = ZigZag8x8[k]; } if (bit != 0) { dataUnit[zzIndex] = 1 << approxBit; } else { dataUnit[zzIndex] = -1 << approxBit; } k++; } } }}int refineAC(int ac, int approxBit) { if (ac > 0) { int bit = nextBit(); if (bit != 0) { ac = ac + (1 << approxBit); } } else if (ac < 0) { int bit = nextBit(); if (bit != 0) { ac = ac + (-1 << approxBit); } } return ac;}void decodeDCCoefficient(int[] dataUnit, int iComp, boolean first, int approxBit) { int[] sParams = scanHeader.componentParameters[componentIds[iComp]]; JPEGHuffmanTable dcTable = dcHuffmanTables[sParams[DC]]; int lastDC = 0; if (progressive && !first) { int bit = nextBit(); lastDC = dataUnit[0] + (bit << approxBit); } else { lastDC = precedingDCs[iComp]; int nBits = decodeUsingTable(dcTable); if (nBits != 0) { int bits = receive(nBits); int diff = extendBy(bits, nBits); lastDC = lastDC + diff; precedingDCs[iComp] = lastDC; } if (progressive) { lastDC = lastDC << approxBit; } } dataUnit[0] = lastDC;}void dequantize(int[] dataUnit, int iComp) { int[] qTable = quantizationTables[frameComponents[componentIds[iComp]][TQI]]; for (int i = 0; i < dataUnit.length; i++) { int zzIndex = ZigZag8x8[i]; dataUnit[zzIndex] = dataUnit[zzIndex] * qTable[i]; }}byte[] decodeImageComponents() { int[] compIds = new int[nComponents]; int compIdsIndex = 0; for (int i = 0; i < nComponents; i++) { compIds[compIdsIndex] = i + 1; compIdsIndex++; } if ((compIds.length == 3) && (compIds[0] == 1) && (compIds[1] == 2) && (compIds[2] == 3)) { return convertYCbCrToRGB(); } if ((compIds.length == 3) && (compIds[0] == 1) && (compIds[1] == 4) && (compIds[2] == 5)) { return convertYIQToRGB(); } if (compIds.length == 4) { return convertCMYKToRGB(); } return convertYToRGB();}void decodeMCUAtXAndY(int xmcu, int ymcu, int nComponentsInScan, boolean first, int start, int end, int approxBit) { for (int iComp = 0; iComp < nComponentsInScan; iComp++) { int scanComponent = iComp; while (scanHeader.componentParameters[componentIds[scanComponent]] == null) { scanComponent++; } int[] frameComponent = frameComponents[componentIds[scanComponent]]; int hi = frameComponent[HI]; int vi = frameComponent[VI]; if (nComponentsInScan == 1) { hi = 1; vi = 1; } int compWidth = frameComponent[CW]; for (int ivi = 0; ivi < vi; ivi++) { for (int ihi = 0; ihi < hi; ihi++) { if (progressive) { // Progressive: First scan - create a new data unit. // Subsequent scans - refine the existing data unit. int index = (ymcu * vi + ivi) * compWidth + xmcu * hi + ihi; dataUnit = dataUnits[scanComponent][index]; if (dataUnit == null) { dataUnit = new int[64]; dataUnits[scanComponent][index] = dataUnit; } } else { // Sequential: Clear and reuse the data unit buffer. for (int i = 0; i < dataUnit.length; i++) { dataUnit[i] = 0; } } if (!progressive || scanHeader.isDCProgressiveScan()) { decodeDCCoefficient(dataUnit, scanComponent, first, approxBit); } if (!progressive) { decodeACCoefficients(dataUnit, scanComponent); } else { if (scanHeader.isACProgressiveScan()) { if (first) { decodeACFirstCoefficients(dataUnit, scanComponent, start, end, approxBit); } else { decodeACRefineCoefficients(dataUnit, scanComponent, start, end, approxBit); } } if (loader.hasListeners()) { // Dequantization, IDCT, up-sampling and color conversion // are done on a copy of the coefficient data in order to // display the image incrementally. int[] temp = dataUnit; dataUnit = new int[64]; System.arraycopy(temp, 0, dataUnit, 0, 64); } } if (!progressive || (progressive && loader.hasListeners())) { dequantize(dataUnit, scanComponent); inverseDCT(dataUnit); storeData(dataUnit, scanComponent, xmcu, ymcu, hi, ihi, vi, ivi); } } } }}void decodeScan() { if (progressive && !scanHeader.verifyProgressiveScan()) { SWT.error(SWT.ERROR_INVALID_IMAGE); } int nComponentsInScan = scanHeader.getNumberOfImageComponents(); int mcuRowsInScan = interleavedMcuRows; int mcusPerRow = interleavedMcuCols; if (nComponentsInScan == 1) { // Non-interleaved. int scanComponent = 0; while (scanHeader.componentParameters[componentIds[scanComponent]] == null) { scanComponent++; } int[] frameComponent = frameComponents[componentIds[scanComponent]]; int hi = frameComponent[HI]; int vi = frameComponent[VI]; int mcuWidth = DCTSIZE * maxH / hi; int mcuHeight = DCTSIZE * maxV / vi; mcusPerRow = (imageWidth + mcuWidth - 1) / mcuWidth; mcuRowsInScan = (imageHeight + mcuHeight - 1) / mcuHeight; } boolean first = scanHeader.isFirstScan(); int start = scanHeader.getStartOfSpectralSelection(); int end = scanHeader.getEndOfSpectralSelection(); int approxBit = scanHeader.getApproxBitPositionLow(); restartsToGo = restartInterval; nextRestartNumber = 0; for (int ymcu = 0; ymcu < mcuRowsInScan; ymcu++) { for (int xmcu = 0; xmcu < mcusPerRow; xmcu++) { if (restartInterval != 0) { if (restartsToGo == 0) processRestartInterval(); restartsToGo--; } decodeMCUAtXAndY(xmcu, ymcu, nComponentsInScan, first, start, end, approxBit); } }}int decodeUsingTable(JPEGHuffmanTable huffmanTable) { int i = 0; int[] maxCodes = huffmanTable.getDhMaxCodes(); int[] minCodes = huffmanTable.getDhMinCodes(); int[] valPtrs = huffmanTable.getDhValPtrs(); int[] huffVals = huffmanTable.getDhValues(); int code = nextBit(); while (code > maxCodes[i]) { code = code * 2 + nextBit(); i++; } int j = valPtrs[i]; j = j + code - minCodes[i]; return huffVals[j];}void emit(int huffCode, int nBits) { if (nBits == 0) { SWT.error(SWT.ERROR_INVALID_IMAGE); } int[] power2m1 = new int[] { 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, 131125 }; int code = (huffCode & power2m1[nBits - 1]) << (24 - nBits - currentBitCount); byte[] codeBuffer = new byte[4]; codeBuffer[0] = (byte)(code % 256); codeBuffer[1] = (byte)((code / 256) % 256); codeBuffer[2] = (byte)((code / 65536) % 256); codeBuffer[3] = (byte)((code / 16777216) % 256); int abs = nBits - (8 - currentBitCount); if (abs < 0) abs = -abs; if ((abs / 8) > 0) { currentByte += codeBuffer[2]; emitByte((byte)currentByte); emitByte((byte)codeBuffer[1]); currentByte = codeBuffer[0]; currentBitCount += nBits - 16; } else { currentBitCount += nBits; if (currentBitCount >= 8) { currentByte += codeBuffer[2]; emitByte((byte)currentByte); currentByte = codeBuffer[1]; currentBitCount -= 8; } else { currentByte += codeBuffer[2]; } }}void emitByte(byte byteValue) { if (bufferCurrentPosition >= 512) { resetOutputBuffer(); } dataBuffer[bufferCurrentPosition] = byteValue; bufferCurrentPosition++; if (byteValue == -1) { emitByte((byte)0); }}void encodeACCoefficients(int[] dataUnit, int iComp) { int[] sParams = scanHeader.componentParameters[iComp]; JPEGHuffmanTable acTable = acHuffmanTables[sParams[AC]]; int[] ehCodes = acTable.ehCodes; byte[] ehSizes = acTable.ehCodeLengths; int r = 0; int k = 1; while (k < 64) { k++; int acValue = dataUnit[ZigZag8x8[k - 1]]; if (acValue == 0) { if (k == 64) { emit(ehCodes[0], ehSizes[0] & 0xFF); } else { r++; } } else { while (r > 15) { emit(ehCodes[0xF0], ehSizes[0xF0] & 0xFF); r -= 16; } if (acValue < 0) { int absACValue = acValue; if (absACValue < 0) absACValue = -absACValue; int nBits = NBitsTable[absACValue]; int rs = r * 16 + nBits; emit(ehCodes[rs], ehSizes[rs] & 0xFF); emit(0xFFFFFF - absACValue, nBits); } else { int nBits = NBitsTable[acValue]; int rs = r * 16 + nBits; emit(ehCodes[rs], ehSizes[rs] & 0xFF); emit(acValue, nBits); } r = 0; } }}void encodeDCCoefficients(int[] dataUnit, int iComp) { int[] sParams = scanHeader.componentParameters[iComp]; JPEGHuffmanTable dcTable = dcHuffmanTables[sParams[DC]]; int lastDC = precedingDCs[iComp]; int dcValue = dataUnit[0]; int diff = dcValue - lastDC; precedingDCs[iComp] = dcValue; if (diff < 0) { int absDiff = 0 - diff; int nBits = NBitsTable[absDiff]; emit(dcTable.ehCodes[nBits], dcTable.ehCodeLengths[nBits]); emit(0xFFFFFF - absDiff, nBits); } else { int nBits = NBitsTable[diff]; emit(dcTable.ehCodes[nBits], dcTable.ehCodeLengths[nBits]); if (nBits != 0) { emit(diff, nBits); } }}void encodeMCUAtXAndY(int xmcu, int ymcu) { int nComponentsInScan = scanHeader.getNumberOfImageComponents(); dataUnit = new int[64]; for (int iComp = 0; iComp < nComponentsInScan; iComp++) { int[] frameComponent = frameComponents[componentIds[iComp]]; int hi = frameComponent[HI]; int vi = frameComponent[VI]; for (int ivi = 0; ivi < vi; ivi++) { for (int ihi = 0; ihi < hi; ihi++) { extractData(dataUnit, iComp, xmcu, ymcu, ihi, ivi); forwardDCT(dataUnit); quantizeData(dataUnit, iComp); encodeDCCoefficients(dataUnit, iComp); encodeACCoefficients(dataUnit, iComp); } } }}void encodeScan() { for (int ymcu = 0; ymcu < interleavedMcuRows; ymcu++) { for (int xmcu = 0; xmcu < interleavedMcuCols; xmcu++) { encodeMCUAtXAndY(xmcu, ymcu); } } if (currentBitCount != 0) { emitByte((byte)currentByte); } resetOutputBuffer();}void expandImageComponents() { for (int iComp = 0; iComp < nComponents; iComp++) { int[] frameComponent = frameComponents[componentIds[iComp]]; int hi = frameComponent[HI]; int vi = frameComponent[VI]; int upH = maxH / hi; int upV = maxV / vi; if ((upH * upV) > 1) { byte[] component = imageComponents[iComp]; int compWidth = frameComponent[CW]; int compHeight = frameComponent[CH]; int upCompWidth = compWidth * upH; int upCompHeight = compHeight * upV; RGB[] rgbs = new RGB[] { new RGB(0,0,0), new RGB(0x80,0,0), new RGB(0,0x80,0), new RGB(0x80,0x80,0), new RGB(0,0,0x80), new RGB(0x80,0,0x80), new RGB(0,0x80,0x80), new RGB(0xC0,0xC0,0xC0), new RGB(0x80,0x80,0x80), new RGB(0xFF,0,0), new RGB(0,0xFF,0), new RGB(0xFF,0xFF,0), new RGB(0,0,0xFF), new RGB(0xFF,0,0xFF), new RGB(0,0xFF,0xFF), new RGB(0xFF,0xFF,0xFF), }; ImageData src = new ImageData(compWidth, compHeight, 8, new PaletteData(rgbs), 4, component); ImageData dest = src.scaledTo(upCompWidth, upCompHeight); imageComponents[iComp] = dest.data; } }}int extendBy(int diff, int t) { if (diff < ExtendTest[t]) { return diff + ExtendOffset[t]; } else { return diff; }}void extractData(int[] dataUnit, int iComp, int xmcu, int ymcu, int ihi, int ivi) { byte[] compImage = imageComponents[iComp]; int[] frameComponent = frameComponents[componentIds[iComp]]; int hi = frameComponent[HI]; int vi = frameComponent[VI]; int compWidth = frameComponent[CW]; int srcIndex = ((ymcu * vi + ivi) * compWidth * DCTSIZE) + ((xmcu * hi + ihi) * DCTSIZE); int destIndex = 0; for (int i = 0; i < DCTSIZE; i++) { for (int col = 0; col < DCTSIZE; col++) { dataUnit[destIndex] = (compImage[srcIndex + col] & 0xFF) - 128; destIndex++; } srcIndex += compWidth; }}void forwardDCT(int[] dataUnit) {
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -