Search code examples
javajpeg

JPEG how to skip user defined tags while decoding the file stream


public ArrayList DCTread(char[] im,int flag,int select, int DC0,int row,int col){
    //Input:im is the binary sequence of the host image. I wrote a "byte2char" function to convert that. flag serves as an outside pointer to locate the to-be-decoded chars. DC0 is the DC coeff for the last block.And row,col is simply for debug.
    //Main Output:An ArrayList that contains the DCT coeffs,pointer(an int showing how many bits are read in this function)
    String rev=new String();char[] DCcode;
    ArrayList res = new ArrayList(2);int[][] ac = null; int[][] dc = null;int[][] coeff = new int[8][8];int pointer = 0;
    int[] ans;int wordLen;int zeroLen;int diff;int ACnum = 1;int dct;
    switch(select){//determine using which two huffman trees.
    case(0):ac = a0;dc = d0;break;
    case(1):ac = a1;dc = d0;break;
    case(16):ac = a0;dc = d1;break;
    case(17):ac = a1;dc = d1;break;
    }
    //DC
    ans = T.huffmanDecoder(im,pointer+flag,dc,row,col);
    if(ans[0]==-1){
        int a1 = T.bin2dec_str(im,pointer+flag,8);int a2 = T.bin2dec_str(im,pointer+flag+8,8);
        pointer +=16;//I wish to skip the User Defined Tags by reading its length
        int autoLen = T.bin2dec_str(im,pointer+flag,8)*16+T.bin2dec_str(im,pointer+flag+8,8);
        pointer +=autoLen*8;
    }
    ans = T.huffmanDecoder(im,pointer+flag,dc,row,col);
    pointer += ans[0];wordLen = ans[1];
    diff = T.i_unsignDecoder(T.bin2dec_str(im,pointer+flag,wordLen),wordLen);   
    coeff[0][0]= DC0 + diff;
    pointer += wordLen;DCcode=Arrays.copyOfRange(im, flag, pointer+flag);
    //AC
    while(ACnum<=63){
        ans = T.huffmanDecoder(im,pointer+flag,ac,row,col);
        pointer += ans[0];
        if(ans[1]==0){//
            break;}
        zeroLen = (ans[1]&(0xF0))/16;wordLen = ans[1]&(0x0F);
        for(int j=0;j<zeroLen;j++){
            coeff[zigZag[ACnum][0]][zigZag[ACnum][1]] = 0;
                    ACnum ++;
        }
        dct = T.i_unsignDecoder(T.bin2dec_str(im,pointer+flag,wordLen),wordLen);
        pointer += wordLen;

        coeff[zigZag[ACnum][0]][zigZag[ACnum][1]] = dct;
        ACnum ++;
    }

    res.add(coeff);
    res.add(pointer);
    res.add(DCcode);
    return res;

}

Hi everyone, firstly I'm so glad to welcome you for seeing my tough problem that has bothered me for two days, and gratefully thank you for your time helping me solve this problem. I've been a watcher of StackOverflow for a long time yet it really is my first time posing a problem.

What I want is to read DCT of JPEG in Java without utilizing the libjpeg library (which is written in C++).However I encounter many user defined tags(UDTs) that I find hard to skip using the method I listed above in the algorithm. I'm quite not familiar with UDTs.

AREN'T THEY written with the beginning of "0xFFXX 0x...."(where 0x.... gives the length of this tag)? Your suggestions would be of great help to me. Thanks!


Solution

  • The markers that can be user defined are APPn's and COM. Those markers are followed by lengths in BIG ENDIAN format.

    However, I am surprised you are finding "many" such tags. Typically, there will only be one or two in a JPEG stream.