📄 file_jpeg_to_image.java
字号:
// return result;// } private int getBit(byte[] in, int n, int temp[], int index[]) { if(n == 0)return 0; result = 0; input = 0; index[0] -= n; while(index[0] < 0){ temp[0] <<= 8; readStep(in); if (markNumI != 0) { markNumI = 0; return (0xFF00 | markNum) << 8; } temp[0] |= input; index[0] += 8; } result = temp[0] >> index[0]; temp[0] &= (mask >> (16 - index[0])); if (result < (one << (n - 1))) { result += (n_one << n) + 1; } return result; } int tempB, tempG, tempR; private short getRGB(int Y, int u, int v) { Y += 128; if (Y < 0) { Y = 0; } tempR = (Y * 100 + 114 * v) / 100; if (tempR < 0) { tempR = 0; } else if (tempR > 255) { tempR = 255; } tempG = (Y * 100 - (39 * u + 58 * v)) / 100; if (tempG < 0) { tempG = 0; } else if (tempG > 255) { tempG = 255; } tempB = (Y * 100 + (203 * u)) / 100; if (tempB < 0) { tempB = 0; } else if (tempB > 255) { tempB = 255; } return (short) ((tempR * 31 / 255) << 11 | (tempG * 63 / 255) << 5 | (tempB * 31 / 255)); } private int getRGB_int(int Y, int u, int v) { if (Y < 0) { Y = 0; } tempR = (Y * 100 + 114 * v) / 100; if (tempR < 0) { tempR = 0; } else if (tempR > 255) { tempR = 255; } tempG = (Y * 100 - (39 * u + 58 * v)) / 100; if (tempG < 0) { tempG = 0; } else if (tempG > 255) { tempG = 255; } tempB = (Y * 100 + (203 * u)) / 100; if (tempB < 0) { tempB = 0; } else if (tempB > 255) { tempB = 255; } return 0xff000000 | ((tempR << 16) + (tempG << 8) + tempB); } int uv_pos; int k = 0; short tc = 0; int i, j; private void outputPixel() { k = 0; for (i = 0; i < Comp[1].samplingV; i++) { for (j = 0; j < Comp[1].smaplingH; j++) {// uv_pos = i * 32 + j * 4; tc = getRGB(chunkData[0][k][0], chunkData[1][0][0], chunkData[2][0][0]); par.par.srcZoomBuf[x / 8 + (y / 8) * par.desW] = tc; x += 8; k++; } x -= Comp[1].smaplingH * 8; y += 8; } x += Comp[1].smaplingH * 8; y -= Comp[1].samplingV * 8; if (x >= oriW) { y += Comp[1].samplingV * 8; x = 0; } }// int Y_D[] = new int[64];// int U_D[] = new int[64];// int V_D[] = new int[64];// int Init_DCTable[] = {// 0, 5, 40, 16, 45, 2, 7, 42,// 21, 56, 8, 61, 18, 47, 1, 4,// 41, 23, 58, 13, 32, 24, 37, 10,// 63, 17, 44, 3, 6, 43, 20, 57,// 15, 34, 29, 48, 53, 26, 39, 9,// 60, 19, 46, 22, 59, 12, 33, 31,// 50, 55, 25, 36, 11, 62, 14, 35,// 28, 49, 52, 27, 38, 30, 51, 54// }; int Cs; static int value; int blockI, cH; long tTime; static int dctSum = 0; private int getDUArray(byte[] in, int PrevDC[], int temp[], int index[]) { try { for (Cs = 0; Cs < scanNum; Cs++) { for (blockI = 0; blockI < blockNum[Cs]; blockI++) { value = countHuffman(dcT[Cs], temp, index, in); if (value >= 0xFF00) { return value; } PrevDC[Cs] = PrevDC[Cs] + getBit(in, value, temp, index); dctSum = PrevDC[Cs] * quTab[Cs][0];//DCTable[0] = cH = 1; value = 1; while(cH < 64 && value != 0){ value = countHuffman(acT[Cs], temp, index, in); if (value >= 0xFF00) { return value; } cH += (value >> 4); dctSum += (getBit(in, value & 0x0F, temp, index) * quTab[Cs][cH]); cH++; } chunkData[Cs][blockI][0] = (((dctSum+128) >> 5) + 16) >> 5; } } } catch (Exception e) {// System.out.println("getDUArray: "+e); } return 0; }// static int tempA[][] = new int[8][8];// static int s0, s1, s2, s3, s4, s5, s6, s7;//// static int i = 0, a, b;//// private static void getDU_UV(int chunkData[]) {// for (i = 0; i < 8; i++) {// s0 = (DCTable[i] + DCTable[8 + i] + DCTable[16 + i] + DCTable[24 + i]);// s1 = DCTable[i] * 256 - DCTable[8 + i] * 256 + DCTable[16 + i] * 128 - DCTable[24 + i] * 640;// s2 = DCTable[i] * 256 - DCTable[8 + i] * 256 - DCTable[16 + i] * 128 + DCTable[24 + i] * 640;// s3 = (DCTable[i] + DCTable[8 + i] - DCTable[24 + i] - DCTable[16 + i]) * 256;// s4 = DCTable[32 + i] * 416 - DCTable[40 + i] * 352 - DCTable[48 + i] * 928 - DCTable[56 + i] * 160;// s5 = DCTable[32 + i] * 64 + DCTable[40 + i] * 160 + DCTable[48 + i] * 1120 + DCTable[56 + i] * 320;// s6 = DCTable[40 + i] * 224 - DCTable[48 + i] * 736 - DCTable[32 + i] * 448 - DCTable[56 + i] * 64;// s7 = (DCTable[32 + i] + DCTable[40 + i] + DCTable[48 + i] + DCTable[56 + i]);// tempA[0][i] = (s0 + s7 + (1 << 4)) >> 5;// tempA[1][i] = (s1 + s6 + (1 << 12)) >> 13;// tempA[2][i] = (s2 + s5 + (1 << 12)) >> 13;// tempA[3][i] = (s3 + s4 + (1 << 12)) >> 13;// tempA[4][i] = (s3 - s4 + (1 << 12)) >> 13;// tempA[5][i] = (s2 - s5 + (1 << 12)) >> 13;// tempA[6][i] = (s1 - s6 + (1 << 12)) >> 13;// tempA[7][i] = (s0 - s7 + (1 << 4)) >> 5;// DCTable[i] = DCTable[8 + i] = DCTable[16 + i] = DCTable[24 + i] = DCTable[32 + i] = DCTable[40 + i] = DCTable[48 + i] = DCTable[56 + i] = 0;// }//// for (a = 0; a < Comp[1].samplingV; a++) {// for (b = 0; b < Comp[1].smaplingH; b += 2) {// i = a * 4 + b / 2;// s0 = (tempA[i][0] + tempA[i][1] + tempA[i][2] + tempA[i][3]);// s7 = (tempA[i][4] + tempA[i][5] + tempA[i][6] + tempA[i][7]);////// s3 = (tempA[i][0] + tempA[i][1] - tempA[i][3] - tempA[i][2]) * 256;//// s4 = tempA[i][4] * 416 - tempA[i][5] * 352 - tempA[i][6] * 928 - tempA[i][7] * 160;// chunkData[i * 8 + 0] = (s0 + s7 + (1 << 4)) >> 5;// chunkData[i * 8 + 4] = chunkData[i * 8 + 0];//// chunkData[i * 8 + 4] = (s3 - s4 + (1 << 12)) >> 13;// }// }// } public void decode(byte[] in) { readIndex = 0; int current, /*m,*/ i, scan_num = 0, RST_num; int PRED[] = new int[3]; if (in == null) { return; } x = 0; y = 0; current = get2Byte(in); if (current != 0xFFD8) { return; } current = get2Byte(in); tTime = System.currentTimeMillis();// par.par.form.append("==================\nRead Start\n"); while (current == 0xFFC4 || current == 0xFFCC || current >> 4 != 0x0FFC) { //SOF 0~15 switch (current) { case 0xFFC4: readHuffmanTable(in); break; case 0xFFCC: return; case 0xFFDB: readQualityTable(in); break; case 0xFFDD: FFDDnum = getNum(in); break; case 0xFFE0: case 0xFFE1: case 0xFFE2: case 0xFFE3: case 0xFFE4: case 0xFFE5: case 0xFFE6: case 0xFFE7: case 0xFFE8: case 0xFFE9: case 0xFFEA: case 0xFFEB: case 0xFFEC: case 0xFFED: case 0xFFEE: case 0xFFEF: case 0xFFFE: skipChunk(in); break; default: if (current >> 8 != 0xFF) { return; } } current = get2Byte(in); } if (current < 0xFFC0 || current > 0xFFC7) { return; } //read frame header int ti, c; skip(in, 2); skip(in, 1); oriH = get2Byte(in); oriW = get2Byte(in); int Nf = getByte(in); Comp = new ComponentSpec[Nf + 1]; for (i = 0; i <= Nf; i++) { Comp[i] = new ComponentSpec(); } for (i = 1; i <= Nf; i++) { c = getByte(in); Comp[c].Cid = c; ti = getByte(in); Comp[c].smaplingH = ti >> 4; Comp[c].samplingV = ti & 0x0F; Comp[c].qTable = getByte(in); } //read frame header over current = get2Byte(in);// if(par.par.mode_main.vbia != null)par.par.mode_main.vbia.process = 15; par.getImageSize(oriW, oriH);// do {// par.par.form.append("Read Photo: " + par.width + " " + par.height + " " + (System.currentTimeMillis() - tTime) + " ms\n"); tTime = System.currentTimeMillis(); while (current != 0x0FFDA) { //SOS switch (current) { case 0xFFC4: //DHT readHuffmanTable(in); break; case 0xFFCC: //DAC return; case 0xFFDB: readQualityTable(in); break; case 0xFFDD: FFDDnum = getNum(in); break; case 0xFFE0: case 0xFFE1: case 0xFFE2: case 0xFFE3: case 0xFFE4: case 0xFFE5: case 0xFFE6: case 0xFFE7: case 0xFFE8: case 0xFFE9: case 0xFFEA: case 0xFFEB: case 0xFFEC: case 0xFFED: case 0xFFEE: case 0xFFEF: case 0xFFFE: skipChunk(in); break; default: if (current >> 8 != 0xFF) { return; } } current = get2Byte(in); } tTime = System.currentTimeMillis(); //read scan JPEG_table get2Byte(in); //len scanNum = getByte(in); int tb; for (i = 0; i < scanNum; i++) { int CompN = getByte(in); quTab[i] = Qtable[Comp[CompN].qTable]; blockNum[i] = Comp[CompN].samplingV * Comp[CompN].smaplingH; tb = getByte(in); dcT[i] = HuffmanData[tb >> 4][0]; acT[i] = HuffmanData[tb & 0x0F][1]; } try{ Qtable = null; JPEG_table = null; HuffmanData = null; System.gc(); Thread.sleep(1000); }catch(Exception e){} skip(in, 3); //read scan JPEG_table over scan_num++; int Mnum; int temp[] = new int[1]; // to store remainded bits int index[] = new int[1];// par.par.form.append("scan start: " + (System.currentTimeMillis() - tTime) + " ms\n"); tTime = System.currentTimeMillis(); for (RST_num = 0;; RST_num++) { //Decode one scan Mnum = 0; temp[0] = 0; index[0] = 0; for (i = 0; i < PRED.length; i++) { PRED[i] = 0; } if (FFDDnum == 0) { current = getDUArray(in, PRED, temp, index); while (current == 0) { outputPixel(); current = getDUArray(in, PRED, temp, index); } break; } for (Mnum = 0; Mnum < FFDDnum; Mnum++) { current = getDUArray(in, PRED, temp, index); outputPixel(); if (current != 0) { break; } } if (current == 0) { if (markNumI != 0) { current = (0xFF00 | markNum); markNumI = 0; } else { current = get2Byte(in); } } if (current >= 0xFFD0 && current <= 0xFFD7) { } else { break; } }// par.par.form.append("Read scan over: " + (System.currentTimeMillis() - tTime) + " ms\n"); }// int count = 0;// while(true){// try{// if(in.read() == -1){// count ++;// if(count == 10)// break;// }// }catch(Exception e){}// }}class ComponentSpec { public int Cid; public int smaplingH; public int samplingV; public int qTable;}class JPGChunkData { int YDU[]; int UDU[]; int VDU[]; int DC_YUV[] = new int[3]; boolean hasDCYUV = false; public void clear() { YDU = null; UDU = null; VDU = null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -