⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file_jpeg_to_image.java

📁 基于J2ME的JPEG decode和encode
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                }
                k++;
                x += 8;
                y -= 8;
            }
            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];
    JPGChunkData jpgChunk[];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    private void outputAndEncode() {
        try {
            int tx, ty;
            int temp;
            int k = 0;
            int wChunkNum = oriW / 8 + (oriW % 8 > 0 ? 1 : 0);
            int hChunkNum = oriH / 8 + (oriH % 8 > 0 ? 1 : 0);
            int allChunkNum = wChunkNum * hChunkNum;
            baos.reset();
            int chunkDataPosition = -1;
            int tempRGB = 0;
            int R, G, B;
            int currentRotateIndex = -1;
            int lastIndex, nextIndex;
            int tempIndexToRotate;
            for (int i = 0; i < Comp[1].samplingV; i++) {
                for (int j = 0; j < Comp[1].smaplingH; j++) {
                    ty = i * 32;
                    tx = temp = j * 4;

                    chunkDataPosition = (y / 8) * wChunkNum + x / 8;
                    Main_Canvas.rotateProcess = 10 + (chunkDataPosition * 60 / allChunkNum);
//                if(chunkDataPosition == 7499){
//                    System.out.println("chunkDataPosition: "+chunkDataPosition + " x: "+(x / 8) + " y: "+(y / 8));
//                }
                    for (int l = 0; l < 64; l++) {
                        Y_D[l] = 0;
                        U_D[l] = 0;
                        V_D[l] = 0;
                    }
                    for (int l = 0; l < 64; l++) {
                        tempRGB = getRGB_int(chunkData[0][k][l] + 128, chunkData[1][0][ty + tx], chunkData[2][0][ty + tx]);
                        R = (tempRGB >>> 16) & 0xFF;
                        G = (tempRGB >>> 8) & 0xFF;
                        B = (tempRGB & 0xFF);
//                    if(chunkDataPosition == 900 || chunkDataPosition == 975){
//                        System.out.print("R: "+R+" ");
//                        System.out.print("G: "+G+" ");
//                        System.out.print("B: "+B+" ");
//                        System.out.println("");
//                    }
                        tempIndexToRotate = indexToRotate(par.rotate, l, 8, 8);
                    Y_D[tempIndexToRotate]=((( 29900)*R+( 58700)*G+( 11400)*B))/100000-128;	//sure: -128
                    U_D[tempIndexToRotate]=(((-16874)*R+(-33126)*G+( 50000)*B))/100000;		//sure
                    V_D[tempIndexToRotate]=((( 50000)*R+(-41869)*G+(-8131)*B))/100000;		//sure
//                        Y_D[l] = (((29900) * R + (58700) * G + (11400) * B)) / 100000 - 128;	//sure: -128
//                        U_D[l] = (((-16874) * R + (-33126) * G + (50000) * B)) / 100000;		//sure
//                        V_D[l] = (((50000) * R + (-41869) * G + (-8131) * B)) / 100000;		//sure
                        x++;
                        if ((x % Comp[1].smaplingH) == 0) {
                            tx++;
                        }
                        if ((x % 8) == 0) {
                            y++;
                            x -= 8;
                            tx = temp;
                            if ((y % Comp[1].samplingV) == 0) {
                                ty += 8;
                            }
                        }
                    }
                    if (chunkDataPosition >= jpgChunk.length) {
                        continue;
                    }

                    currentRotateIndex = indexToRotate(par.rotate, chunkDataPosition, wChunkNum, hChunkNum);
//                    if(chunkDataPosition == 7499){
//                        System.out.println("currentRotateIndex: "+currentRotateIndex);
//                    }
//                    currentRotateIndex = chunkDataPosition;
//                int[] tYDU = Y_D;
//                int[] tUDU = U_D;
//                int[] tVDU = V_D;
//                jpgChunk[currentRotateIndex].Y_D = new int[64];
//                jpgChunk[currentRotateIndex].U_D = new int[64];
//                jpgChunk[currentRotateIndex].V_D = new int[64];
//                for(int a=0;a<64;a++){
//                    jpgChunk[currentRotateIndex].Y_D[a] = Y_D[a];
//                    jpgChunk[currentRotateIndex].U_D[a] = U_D[a];
//                    jpgChunk[currentRotateIndex].V_D[a] = V_D[a];
//                }
//                jpgChunk[currentRotateIndex].DC_YUV = par.fileImageToJPG.getDC_YUV(tYDU, tUDU, tVDU);


                    if (currentRotateIndex == 0) {
//                    jpgChunk[chunkDataPosition].DC_YUV = new int[3];
                        jpgChunk[chunkDataPosition].hasDCYUV = true;
//                    System.out.println("chunkDataPosition: "+chunkDataPosition);
//                    System.out.print("Y_D: ");
//                    for(int m=0;m<64;m++){
//                     System.out.print(Y_D[m]+" ");
//                    }
//                    System.out.println("");
//                    System.out.print("U_D: ");
//                    for(int m=0;m<64;m++){
//                     System.out.print(U_D[m]+" ");
//                    }
//                    System.out.println("");
//                    System.out.print("V_D: ");
//                    for(int m=0;m<64;m++){
//                     System.out.print(V_D[m]+" ");
//                    }
//                    System.out.println("");
                        par.fileImageToJPG.doEncode(baos, Y_D, U_D, V_D, jpgChunk[chunkDataPosition].DC_YUV);
                        if (currentRotateIndex < par.fileImageToJPG.chunkData.length) {
                            par.fileImageToJPG.chunkData[currentRotateIndex] = baos.toByteArray();
                            baos.reset();
                        }
//                    System.out.print("DC_YUV: ");
//                    for(int m=0;m<3;m++){
//                     System.out.print(jpgChunk[chunkDataPosition].DC_YUV[m]+" ");
//                    }
//                    System.out.println("");
                    } else {
                        lastIndex = indexFromRotate(par.rotate, currentRotateIndex - 1, hChunkNum, wChunkNum);
//                        lastIndex = currentRotateIndex - 1;
                        if (jpgChunk[lastIndex] != null && jpgChunk[lastIndex].hasDCYUV) {
//                        if(currentRotateIndex == 74){
//                            System.out.println("currentRotateIndex: "+currentRotateIndex+" chunkDataPosition: "+chunkDataPosition);
//                            System.out.print("Y_D: ");
//                            for(int m=0;m<64;m++){
//                             System.out.print(Y_D[m]+" ");
//                            }
//                            System.out.println("");
//                            System.out.print("U_D: ");
//                            for(int m=0;m<64;m++){
//                             System.out.print(U_D[m]+" ");
//                            }
//                            System.out.println("");
//                            System.out.print("V_D: ");
//                            for(int m=0;m<64;m++){
//                             System.out.print(V_D[m]+" ");
//                            }
//                            System.out.println("");
//                            System.out.print("last DC_YUV: ");
//                            for(int m=0;m<3;m++){
//                             System.out.print(jpgChunk[lastIndex].DC_YUV[m]+" ");
//                            }
//                            System.out.println("");
//                        }
                            par.fileImageToJPG.doEncode(baos, Y_D, U_D, V_D, jpgChunk[lastIndex].DC_YUV);
                            
//                        if(currentRotateIndex == 74){
//                            System.out.print("DC_YUV: ");
//                            for(int m=0;m<3;m++){
//                             System.out.print(jpgChunk[lastIndex].DC_YUV[m]+" ");
//                            }
//                            System.out.println("");
//                        }
                            if (currentRotateIndex < par.fileImageToJPG.chunkData.length) {
                                par.fileImageToJPG.chunkData[currentRotateIndex] = baos.toByteArray();
                                baos.reset();
                            }
//                        if(currentRotateIndex == 74){
//                            System.out.print("chunkData: ");
//                            for(int m=0;m<par.fileImageToJPG.chunkData[currentRotateIndex].length;m++){
//                             System.out.print(par.fileImageToJPG.chunkData[currentRotateIndex][m]+" ");
//                            }
//                            System.out.println("");
//                        }
//                        jpgChunk[chunkDataPosition].DC_YUV = new int[3];
                            jpgChunk[chunkDataPosition].hasDCYUV = true;
                            for (int a = 0; a < 3; a++) {
                                jpgChunk[chunkDataPosition].DC_YUV[a] = jpgChunk[lastIndex].DC_YUV[a];
                            }
                            jpgChunk[lastIndex].hasDCYUV = false;
                            jpgChunk[lastIndex].DC_YUV = null;
                        } else {
                            jpgChunk[chunkDataPosition].YDU = new int[64];
                            jpgChunk[chunkDataPosition].UDU = new int[64];
                            jpgChunk[chunkDataPosition].VDU = new int[64];
                            for (int a = 0; a < 64; a++) {
                                jpgChunk[chunkDataPosition].YDU[a] = Y_D[a];
                                jpgChunk[chunkDataPosition].UDU[a] = U_D[a];
                                jpgChunk[chunkDataPosition].VDU[a] = V_D[a];
                            }
                            jpgChunk[chunkDataPosition].hasDCYUV = true;
                            jpgChunk[chunkDataPosition].DC_YUV = par.fileImageToJPG.getDC_YUV(Y_D, U_D, V_D);
                        }
                    }
                    if (currentRotateIndex < allChunkNum - 1) {
                        nextIndex = indexFromRotate(par.rotate, currentRotateIndex + 1, hChunkNum, wChunkNum);
//                        nextIndex = currentRotateIndex + 1;
                        
                        if (jpgChunk[nextIndex] != null && jpgChunk[nextIndex].UDU != null) {
//                            if(currentRotateIndex + 1 == 75){
//                                System.out.println("currentRotateIndex+1: "+(currentRotateIndex+ 1)+" chunkDataPosition: "+chunkDataPosition);
//                                System.out.print("Y_D: ");
//                                for(int m=0;m<64;m++){
//                                 System.out.print(Y_D[m]+" ");
//                                }
//                                System.out.println("");
//                                System.out.print("U_D: ");
//                                for(int m=0;m<64;m++){
//                                 System.out.print(U_D[m]+" ");
//                                }
//                                System.out.println("");
//                                System.out.print("V_D: ");
//                                for(int m=0;m<64;m++){
//                                 System.out.print(V_D[m]+" ");
//                                }
//                                System.out.println("");
//                                System.out.print("last DC_YUV: ");
//                                for(int m=0;m<3;m++){
//                                 System.out.print(jpgChunk[chunkDataPosition].DC_YUV[m]+" ");
//                                }
//                                System.out.println("");
//                            }
                            par.fileImageToJPG.doEncode(baos, jpgChunk[nextIndex].YDU, jpgChunk[nextIndex].UDU, jpgChunk[nextIndex].VDU, jpgChunk[chunkDataPosition].DC_YUV);
                            if ((currentRotateIndex + 1) < par.fileImageToJPG.chunkData.length) {
                                par.fileImageToJPG.chunkData[(currentRotateIndex + 1)] = baos.toByteArray();
                                baos.reset();
                            }
                            jpgChunk[chunkDataPosition].hasDCYUV = false;
                            jpgChunk[chunkDataPosition].DC_YUV = null;
                            jpgChunk[nextIndex].clear();
                        }
                    }
                    k++;
                    x += 8;
                    y -= 8;
                }
                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;
            }
        } catch (Exception e) {
            System.out.println("outputAndEncode: " + e);
        }
    }

    public int indexToRotate(int ro, int index, int wid, int hei) {
        switch (ro) {
            case 6: // left
                return (wid - 1 - index % wid) * hei + index / wid;
            case 5: // right
                return ((index % wid) * hei + (hei - 1 - index / wid));
        }
        return -1;
    }

    public int indexFromRotate(int ro, int index, int wid, int hei) {
        switch (ro) {
            case 6:
                return ((index % wid) * hei + (hei - 1 - index / wid));
            case 5:
                return ((wid - 1 - index % wid) * hei + index / wid);
        }
        return -1;
    }
    
    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
    };
    private int getDUArray(byte[] in, int PrevDC[],
            int temp[], int index[]){
        try {
            int value;
            short actab[], dctab[];
            int qtab[], Cs;

            int wChunkNum = oriW / 8 + (oriW % 8 > 0 ? 1 : 0);
            int chunkDataPosition = 0;
            chunkDataPosition = (y / 8) * wChunkNum + x / 8;
            for (Cs = 0; Cs < scanNum; Cs++) {
                qtab = quTab[Cs];
                actab = acT[Cs];
                dctab = dcT[Cs];
                for (int i = 0; i < blockNum[Cs]; i++) {
                    for (int k = 0; k < DCTable.length; k++) {
                        DCTable[k] = 0;
                    }
                    value = countHuffman(dctab, temp, index, in);
                    
                    if (value >= 0xFF00) {
                        return value;
                    }
                    int tt = getBit(in, value, temp, index);
//                    if(m == 3749){
//                        System.out.println("");
//                        System.out.println("blockNum: "+i+" scanNum "+scanNum);
//                        System.out.println((readIndex-1)+"("+value+"): "+tt);
//                        System.out.println("Read 64 start:");
//                    }
                    PrevDC[Cs] = DCTable[0] = PrevDC[Cs] + tt;
                    DCTable[0] *= qtab[0];
                    
                    for (int j = 1; j < 64; j++) {
//                        if(m == 3749){
//                            System.out.println("i: "+i);
//                        }
                        value = countHuffman(actab, temp, index, in);
//                        if(m == 3749){
//                            System.out.println("value: "+value);
//                            System.out.println("value >> 4: "+(value >> 4));
//                        }
                        if (value >= 0xFF00) {
                            return value;
                        }
                        j += (value >> 4);

                        if ((value & 0x0F) == 0) {
                            if ((value >> 4) == 0) {
                                break;
                            }
                        } else {
//                            if(m == 3749)
//                                System.out.println("value & 0x0F: "+(value & 0x0F));
                            tt = getBit(in, value & 0x0F, temp, index);
//                            if(m == 3749)
//                                System.out.println((readIndex-1)+"("+value+"): "+tt);
                            DCTable[Init_DCTable[j]] = tt * qtab[j];
                        }
                    }
                    getDU(chunkData[Cs][i]);
                }
            }
        } catch (Exception e) {
//            System.out.println("getDUArray: "+e);
        }
        return 0;
    }

    boolean tempPrintFlag = false;
    private void getDU(int chunkData[]) {
        int tempA[][] = new int[8][8];
        int t0, t1, t2, t3;
        int s0, s1, s2, s3, s4, s5, s6, s7;
        int d0, d1, d2, d3, d4, d5, d6, d7;
        for (int i = 0; i < 8; i++) {
            s0 = DCTable[0 * 8 + i];
            s1 = DCTable[1 * 8 + i];
            s2 = DCTable[2 * 8 + i] - DCTable[3 * 8 + i];
            s3 = DCTable[3 * 8 + i] + DCTable[2 * 8 + i];
            s4 = DCTable[4 * 8 + i] - DCTable[7 * 8 + i];
            s6 = DCTable[5 * 8 + i] - DCTable[6 * 8 + i];
            t0 = DCTable[5 * 8 + i] + DCTable[6 * 8 + i];
            t1 = DCTable[4 * 8 + i] + DCTable[7 * 8 + i];
            s5 = t0 - t1;
            s7 = t0 + t1;
            
            d4 = -s4 * 480 - s6 * 192;
            d5 = s5 * 384;
            d6 = s6 * 480 - s4 * 192;
            d7 = s7 * 256;
            t0 = s0 * 256;
            t1 = s1 * 256;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -