help with jpg compression

I am trying to learn how to do jpg compression. Currently I am taking in a ppm image, doing a DCT on the image, then doing quantization, then doing reverse quantization, then doing an IDCT, and then outputting a new ppm image. However while it is somewhat working I sometimes get glitches on the output image.

here is the code

https://pastebin.com/ez3ztRBN
Help us help you. There's 100s of lines of code there with mathematical formulas that I would need to look closely at to see if they're right.

Show us the simplest set of pixels that produces "glitches" in the output image.

In general, I would break your issue into self-contained tests. You have given no hint as to which component might be at fault.
The issue could be any of the following:
1. Reading in the image
2. Doing a DCT on given input
3. Doing quantization on input
4. Doing reverse quantization on input
5. Doing IDCT
6. Writing out the image

I suggest writing self-contained tests for each component, and testing what you think the output should be at each step.

Also, don't manually call destructors. The whole point of destructors is that they are automatically called to deallocate resources.
Last edited on
Here is an image that produces glitches and also the resulting glitchy image
https://imgur.com/a/hBdCsew
I would suggest to work with grayscale first, easier to interpret the "glitches"
when looked at each channels, it seems that you have issues in white zones, perhaps it's because of overflow
A few more things,
1. Your program is not portable, you should be #including <cmath> and <cstdlib>.
2. You have a dozen unused variables. Is it possibly a mistake that one or more of them aren't being used?
3. Your program expects .ppm file as input, but you have provided .png files. You're making us do unnecessary work in converting the test image to help you.

Bonus:
4. After converting your png to a ppm and running your program, the program crashes on me.
But this is only because of the manual destructor calls I mentioned earlier. However, after I removed those calls, the crash no longer happens, but the output.ppm is still severely misaligned, with incorrect dimensions.
standard output of your program:
input.ppm is a PPM file (raw version)!
done
done
done
450  823


Those dimensions seem backwards to me?
Your jpg* functions expect {height, width} as input, but you are inputting {width, height}. Your readppm function is also mixing up width and height.

Maybe that helps. Perhaps you're consistently switching the two, canceling out errors. But the image that I produced by running your code definitely has wrong dimensions.
Last edited on
another totally different, non-code take:
wavelet compression is known to make artifacts and the higher the compression level (or threshold in the code where you zero out some of the data) the worse that gets. These artifacts are known to look better in 'natural' images (like a photo) and are more visually offending in drawn / generated images.
So what you are seeing could be normal, if you have too much compression or unfriendly test images. Or they could be errors from your code. If you think they may be normal, what I would do is grab a few images from the web and feed them to a image program where you can save-as a new jpg with compression turned way up, and get familiar with what you can expect to see when an image is damaged, and from there you can decide if its a bug or 'setting' maybe?

a quick summary:
radical blobs of a totally off-looking color are bugs.
blurry blobs, usually sort of rectangularish shape, of similar colors to what is around are normal.
Last edited on
Ok I figured what was causing the glitches, and it was the quantization. However, I still don't know why the quantization is causing the issues, and I don't know how to fix it.

here is the revised code

https://pastebin.com/4nUJnZeL
so ¿what part in those 600 lines of code is the quantization?
the quantization is the function called qu and the de quantization is the function called qu2
1
2
3
4
//quantization
round( value / factor )
//de quantization
value * factor

if you do
y = dequantization( quantization(x) )
you may end up with y > x


¿are you clamping your pixel values?
Ok clamping the output worked, and it is now fixed

here is the code
https://pastebin.com/6hFFcbJb
Topic archived. No new replies allowed.