I decided to use libjpeg as the main library working with jpeg files. I've read libjpg.txt file. And I was pleased that library allows DCT coefficients reading/writing in a convenient way. Since writing an own decoder will take a long time.
My work is related to the lossless embedding. Currently I need to read DCT coefficients from a file then modify some of them and write changed coefficients in the same file.
Well, I found jpeg_write_coefficients()
function. And I naively thought that I could apply it to a decompression object (struct jpeg_decompress_struct
). But it does not work and requires a compression object.
I can't believe that such the powerful library is not able to do this. I think that most likely I'm missing something. Although I tried to be attentive. Perhaps the writing coefficients can be done more sophisticated way. But I don't know how to.
I will be very glad if you propose your ideas.
You should really take a look at transupp.h and sources for jpegtran that comes with the library. Anyway, here is my dirty code with comments, assembled partially from jpegtran. It lets you manipulate DCT coefficients one by one.
And the function itself:
You can ue jpeg_write_coefficients to write your changed DCT.
The following information is avaliable in
libjpeg.txt
So to change a single dct, you can use the following simple code: To access any dct coeff, you need to change four index, cx, bx, by, bi. In my code, I used
blockptr_one[bi]++;
to increase one dct Coeff