Search code examples
javaimagecompressionjpegdct

Change Code from DCT to Inverse Discrete Cosine Transformation?


For an project I have to demonstrate JPEG compression and therefore the conversion with DCTII and IDCT. I have no idea how to implement this formula. But I found an website that provides the Java code and online IDE for testing. https://ide.geeksforgeeks.org/FnC3bRJEAr here you can see the code.

formula for DCT formula for IDCT

(formulas from Wikipedia/JPEG)

So, what changes have to be made to the code?

I tried switching the for-loops and the variables in the formula but the values I got were definitely wrong, other tries lead to error messages.


Solution

  • The only difference between the DCT and IDCT is where coefficient are taken into account.

    You should replace line 46 in your code by

    sum = sum + ck*cl*dct1;
    

    where ck and cl are computed as in lines 24-34, but for k and l

    And suppress ci*cj in line 49

    BTW, this java code is exceptionally inefficient. Precompute Math.sqrt(2), Math.sqrt(n) and put your cosine in a table and it will be at least 3 times faster.