java - Issue related to implementing Inverse DCT on Images -


i trying implement dct on images part of understanding whole jpeg compression pipeline (in java). can implement forward dct. however, facing issues in inverse dct. highly appreciated.

public bufferedimage inverse_dct (bufferedimage img) {     int width = img.getwidth();     int height = img.getheight();     bufferedimage outputimage = new bufferedimage(width,height,bufferedimage.type_int_rgb);      // ------------------- idct implementation ------------------------------     double [][]cos_basis = new double [8][8]; // 8x8 cosine basis implementation     double [] coeff = new double [8];     // ------------------ pre compute kernel ---------------------------     (int i=0;i<8;i++)     {            (int j=0;j<8;j++)         {             cos_basis[i][j] =  math.cos((2*i+1)*j*3.14159f/16.0f); // cosine kernel         }         if (i==0)             coeff[i]=1/(math.sqrt(2));         else             coeff[i]=1;                  }     // -------------------- idct code --------------------------------------     (int row=0;row<(height/8);row++)     {         (int col=0;col<(width/8);col++)         {             (int i=0;i<8;i++) // block row             {                 (int j=0;j<8;j++) // block col                 {                    double sumr = 0;double sumg = 0;double sumb = 0;                    (int x=0;x<8;x++)                    {                        (int y=0;y<8;y++)                        {                            sumr += (coeff[x])*(coeff[y])*((((img.getrgb((col*8+y),(row*8+x))>>16) & 0xff))*(cos_basis[i][x])*(cos_basis[j][y]));                            sumg += (coeff[x])*(coeff[y])*((((img.getrgb((col*8+y),(row*8+x))>>8) & 0xff))*(cos_basis[i][x])*(cos_basis[j][y]));                            sumb += (coeff[x])*(coeff[y])*((((img.getrgb((col*8+y),(row*8+x))>>0) & 0xff))*(cos_basis[i][x])*(cos_basis[j][y]));                        }                    }                     sumr *= 0.25f; sumr += 128;                    sumg *= 0.25f; sumg += 128;                    sumb *= 0.25f; sumb += 128;                    if (sumr<0) sumr=0; if (sumg<0) sumg=0;if (sumb<0) sumb=0;                    if (sumr>255) sumr=255; if (sumg>255) sumg=255;if (sumb>255) sumb=255;                    //system.out.println("sumr : "+sumr+"  sumg:"+sumg+" sumb:"+sumb);                    // assign output image                    int pix = 0x00000000 | (((int)(sumr) & 0xff) << 16) | (((int)(sumg) & 0xff) << 8) | ((int)(sumb) & 0xff);                    //system.out.println("pixel : "+pix);                    outputimage.setrgb((col*8+j),(row*8+i),pix);                 }             }         }     }     return outputimage;  } 

i can confirm forward dct correct (used 8x8 exmple paper , got matching values). however, idct giving me problems. presume have implemented formula correctly cant seem point out place going wrong. can please out ?

the problem solved me hours after posted, fixed following ; works fine now.

public bufferedimage inverse_dct (bufferedimage img){ int width = img.getwidth(); int height = img.getheight(); bufferedimage outputimage = new bufferedimage(width,height,bufferedimage.type_int_rgb);  // ------------------- idct implementation ------------------------------ double [][]cos_basis = new double [8][8]; // 8x8 cosine basis implementation //double [] coeff = new double [8]; double cu,cv; // ------------------ pre compute kernel --------------------------- (int i=0;i<8;i++) {        (int j=0;j<8;j++)     {         cos_basis[i][j] =  math.cos((2*i+1)*j*3.14159f/16.0f); // cosine kernel     }     //if (i==0)     //    coeff[i]=1/(math.sqrt(2));     //else     //    coeff[i]=1;              } // -------------------- idct code -------------------------------------- (int row=0;row<(height);row+=8) {     (int col=0;col<(width);col+=8)     {         (int i=0;i<8;i++) // block row         {             (int j=0;j<8;j++) // block col             {                double sumr = 0;double sumg = 0;double sumb = 0;                (int x=0;x<8;x++)                {                    (int y=0;y<8;y++)                    {                          if (x==0) {cu = (1/(math.sqrt(2)));}                        else { cu = 1;}                         if (y==0) {cv = (1/(math.sqrt(2)));}                        else { cv = 1;}                          sumr += (cu)*(cv)*((((img.getrgb((col+y),(row+x))>>16) & 0xff))*(cos_basis[i][x])*(cos_basis[j][y]));                        sumg += (cu)*(cv)*((((img.getrgb((col+y),(row+x))>>8) & 0xff))*(cos_basis[i][x])*(cos_basis[j][y]));                        sumb += (cu)*(cv)*((((img.getrgb((col+y),(row+x))>>0) & 0xff))*(cos_basis[i][x])*(cos_basis[j][y]));                    }                }                 sumr *= 0.25f;                 sumg *= 0.25f;                 sumb *= 0.25f;                 if (sumr<0) sumr=0; if (sumg<0) sumg=0;if (sumb<0) sumb=0;                if (sumr>255) sumr=255; if (sumg>255) sumg=255;if (sumb>255) sumb=255;                //system.out.println("sumr : "+sumr+"  sumg:"+sumg+" sumb:"+sumb);                // assign output image                int pix = 0xff000000 | (((int)(sumr) & 0xff) << 16) | (((int)(sumg) & 0xff) << 8) | ((int)(sumb) & 0xff);                //system.out.println("pixel : "+pix);                outputimage.setrgb((col+j),(row+i),pix);             }         }     } } return outputimage; } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -