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

📄 file_jpeg_to_image.java

📁 基于J2ME的JPEG decode和encode
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                }
            }
        }
        for (int i = 1; tCount < 256; i++, tCount++) {
            tab[tCount] = (short) (i | MASK);
        }

        int curTable = 1;
        tCount = 0;
        for (int i = 8; i < 16; i++) {  // i+1 is Code length
            for (int j = 0; j < L[i]; j++) {
                for (int n = 0; n < (temp >> (i - 7)); n++) {
                    tab[curTable * 256 + tCount] = (short) (V[i][j] | ((i + 1) << 8));
                    tCount++;
                }
                if (tCount >= 256) {
                    if (tCount > 256) {
                        return;
                    }
                    tCount = 0;
                    curTable++;
                }
            }
        }
    }

    private int countHuffman(short table[], int temp[], int index[], byte[] in){
        int code = 0, input, mask = 0xFFFF;
        int MASK = (short) 0x8000;
        try {
            if (index[0] < 8) {
                temp[0] <<= 8;
                input = getByte(in);
//                if(m == 3750)
//                    System.out.println((readIndex-1)+"(a): "+input+" index[0] "+index[0]+" temp[] "+temp[0]);
                if (input == 0xFF) {
                    markNum = getByte(in);
//                    if(m == 3750)
//                        System.out.println((readIndex-1)+"(b): "+markNum+" index[0] "+index[0]+" temp[] "+temp[0]);
                    if (markNum != 0) {
                        markNumI = 9;
                    }
                }
                temp[0] |= input;
            } else {
                index[0] -= 8;
            }
            if (markNumI != 0 && index[0] > 0){
                markNumI = 0;
                readIndex -= 2;
//                tempPrintFlag = false;
                code = table[temp[0] >>(16 - index[0])];
                temp[0] = 0;
                index[0] = 0;
                code = code & 0x0000ffff;
                return code & 0xFF;
//                return 0xFF00 | markNum;
            }else {
                code = table[temp[0] >> index[0]];
            }
            code = code & 0x0000ffff;
            if ((code & MASK) != 0) {
                if (markNumI != 0) {
                    markNumI = 0;
                    tempPrintFlag = false;
                    return 0xFF00 | markNum;
                }
                temp[0] &= (mask >> (16 - index[0]));
                temp[0] <<= 8;
                input = getByte(in);
//                if(m == 3749)
//                        System.out.println((readIndex-1)+"(c): "+input+" index[0] "+index[0]+" temp[] "+temp[0]);
                if (input == 0xFF) {
                    markNum = getByte(in);
//                    if(m == 3749)
//                        System.out.println((readIndex-1)+"(d): "+markNum+" index[0] "+index[0]+" temp[] "+temp[0]);
                    if (markNum != 0) {
                        markNumI = 9;
                    }
                }
                temp[0] |= input;
                if (markNumI != 0 && index[0] > 0){
                    markNumI = 0;
                    readIndex -= 2;
                    code = table[(code & 0xFF) * 256 + (temp[0] >> (16-index[0]))];
                    index[0] = 0;
                    temp[0] = 0;
                    code = code & 0x0000ffff;
                    return code & 0xFF;
                }else {
                    code = table[(code & 0xFF) * 256 + (temp[0] >> index[0])];
                }
                code = code & 0x0000ffff;
                index[0] += 8;
            }
            index[0] += 8 - (code >> 8);
            if (index[0] < 0) {
                return 0;
            }
            if (index[0] < markNumI) {
                markNumI = 0;
                tempPrintFlag = false;
                return 0xFF00 | markNum;
            }
            temp[0] &= (mask >> (16 - index[0]));
        } catch (Exception e) {
//            System.out.println("countHuffman: "+e.toString());
        }
        tempPrintFlag = false;
        return code & 0xFF;
    }

    void readQualityTable(byte[] in){
        int len;
        int Pq[] = new int[4];
        int Tq[] = new int[4];
        
        int i, count = 0, temp, t;
        len = get2Byte(in);
        count += 2;
        while (count < len) {
            temp = getByte(in);
            count++;
            t = temp & 0x0F;
            if (t > 3) {
                return;
            }
            Pq[t] = temp >> 4;
            if (Pq[t] == 0) {
                Pq[t] = 8;
            } else if (Pq[t] == 1) {
                Pq[t] = 16;
            } else {
                return;
            }
            Tq[t] = 1;
            if (Pq[t] == 8) {
                for (i = 0; i < 64; i++) {
                    Qtable[t][i] = getByte(in);
                    count++;
                }
                countQuality(Qtable[t]);
            } else {
                for (i = 0; i < 64; i++) {
                    Qtable[t][i] = get2Byte(in);
                    count += 2;
                }
                countQuality(Qtable[t]);
            }
        }
    }
    
    void countQuality(int qt[]) {
        int i;
        for (i = 0; i < 8; i++) {
            qt[JPEG_table[0 * 8 + i]] *= 90;
            qt[JPEG_table[1 * 8 + i]] *= 126;
            qt[JPEG_table[2 * 8 + i]] *= 118;
            qt[JPEG_table[3 * 8 + i]] *= 106;
            qt[JPEG_table[4 * 8 + i]] *= 90;
            qt[JPEG_table[5 * 8 + i]] *= 71;
            qt[JPEG_table[6 * 8 + i]] *= 49;
            qt[JPEG_table[7 * 8 + i]] *= 25;
        }
        for (i = 0; i < 8; i++) {
            qt[JPEG_table[0 + 8 * i]] *= 90;
            qt[JPEG_table[1 + 8 * i]] *= 126;
            qt[JPEG_table[2 + 8 * i]] *= 118;
            qt[JPEG_table[3 + 8 * i]] *= 106;
            qt[JPEG_table[4 + 8 * i]] *= 90;
            qt[JPEG_table[5 + 8 * i]] *= 71;
            qt[JPEG_table[6 + 8 * i]] *= 49;
            qt[JPEG_table[7 + 8 * i]] *= 25;
        }
        for (i = 0; i < 64; i++) {
            qt[i] >>= 6;
        }
    }
    
    private int getBit(byte[] in, int n, int temp[], int index[]){
        int result, one = 1, n_one = -1;
        int mask = 0xFFFF, input;
        if (n == 0) {
            return 0;
        }
//        if(m == 3749)
//            System.out.println("index[0]: "+index[0]+" temp[] "+temp[0]);
        index[0] -= n;
        if (index[0] >= 0) {
            if (index[0] < markNumI) {
                markNumI = 0;
                tempPrintFlag = false;
                return (0xFF00 | markNum) << 8;
            }
            result = temp[0] >> index[0];
            temp[0] &= (mask >> (16 - index[0]));
        } else {
            temp[0] <<= 8;
            input = getByte(in);
//            if(m == 3749)
//                System.out.println((readIndex-1) +": "+ input+"index[0]: "+index[0]+" temp[] "+temp[0]);
            if (input == 0xFF) {
                markNum = getByte(in);
//                if(m == 3749)
//                    System.out.println((readIndex-1) +": "+ markNum+"index[0]: "+index[0]+" temp[] "+temp[0]);
                if (markNum != 0) {
                    markNumI = 9;
                }
            }
            temp[0] |= input;
            index[0] += 8;
            if (index[0] < 0) {
                if (markNumI != 0) {
                    markNumI = 0;
                    return (0xFF00 | markNum) << 8;
                }
                temp[0] <<= 8;
                input = getByte(in);
//                if(m == 3749)
//                    System.out.println((readIndex-1) +": "+ input+"index[0]: "+index[0]+" temp[] "+temp[0]);
                if (input == 0xFF) {
                    markNum = getByte(in);
//                    if(m == 3749)
//                        System.out.println((readIndex-1) +": "+ markNum+"index[0]: "+index[0]+" temp[] "+temp[0]);
                    if (markNum != 0) {
                        markNumI = 9;
                    }
                }
                temp[0] |= input;
                index[0] += 8;
            }
            tempPrintFlag = false;
            if (index[0] < markNumI) {
                markNumI = 0;
                return (0xFF00 | markNum) << 8;
            }
            result = temp[0] >> index[0];
            temp[0] &= (mask >> (16 - index[0]));
        }
        if (result < (one << (n - 1))) {
            result += (n_one << n) + 1;
        }
        return result;
    }

    private short getRGB(int Y, int u, int v) {
        Y += 128;
        if (Y < 0) {
            Y = 0;
        }
        int tempB, tempG, tempR;

        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;
        }
        int tempB, tempG, tempR;

        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);
    }
    
    private void outputPixel() {
        int tx, ty;
        int temp;
        int k = 0;

        int nowx = -1;
        int nowy = -1;

        int wChunkNum = oriW / 8 + (oriW % 8 > 0 ? 1 : 0);
        int hChunkNum = oriH / 8 + (oriH % 8 > 0 ? 1 : 0);
        int chunkDataPosition = 0;
        short tc = 0;

        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;
                for (int l = 0; l < 64; l++) {
                    if (x < oriW && y < oriH) {
                        if (par.needZoom) {
                            if (par.tabX[x] != -1 && par.tabY[y] != -1) {
                                nowx = par.tabX[x];
                                nowy = par.tabY[y];
                                int newPosition = 0;
                                switch (par.rotate) {
                                    case 0:
                                        newPosition = nowx + nowy * par.desW;
                                        break;
                                    case 6:
                                        newPosition = (par.desW - 1 - nowx) * par.desH + nowy;
                                        break;
                                    case 3:
                                        newPosition = (par.desH - 1 - nowy) * par.desW + (par.desW - 1 - nowx);
                                        break;
                                    case 5:
                                        newPosition = nowx * par.desH + (par.desH - 1 - nowy);
                                        break;
                                }
                                par.par.srcZoomBuf[newPosition] = getRGB(chunkData[0][k][l], chunkData[1][0][ty + tx], chunkData[2][0][ty + tx]);
                            }
                        } else {
                            if (!par.needZoom) {
                                tc = getRGB(chunkData[0][k][l], chunkData[1][0][ty + tx], chunkData[2][0][ty + tx]);
                                switch (par.rotate) {
                                    case 0:
                                        par.par.srcZoomBuf[x + y * par.width] = tc;
                                        break;
                                    case 5:
                                        par.par.srcZoomBuf[(par.width - 1 - x) * par.height + y] = tc;
                                        break;
                                    case 3:
                                        par.par.srcZoomBuf[(par.height - 1 - y) * par.width + (par.width - 1 - x)] = tc;
                                        break;
                                    case 6:
                                        par.par.srcZoomBuf[x * par.height + (par.height - 1 - y)] = tc;
                                        break;
                                    }
                            }
                        }
                    }
                    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;
                        }
                    }

⌨️ 快捷键说明

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