Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add JPEG comments to an existing JPEG image file

Tags:

c++

c

libjpeg

Is there a way to add JPEG comments ("COM" markers) to an existing JPEG image file using libjpeg?

It is certainly possible to do so by first decompressing an existing image to an in-memory buffer and then compressing the raw image again with jpeg_write_marker( ... JPEG_COM ... ) to add comments, and saving to disk. Unless there is a need to decompress first, doing this seems to be an overkill.

like image 397
alexg Avatar asked Feb 01 '26 00:02

alexg


2 Answers

There's a tool called wrjpgcom, it's part of libjpeg. I think it does what you want. Perhaps you could look through its source to find out how it's done.

like image 68
detunized Avatar answered Feb 02 '26 15:02

detunized


You can use jpeg_write_marker() during the writing of the output file to write the comment after setting it up. Then, use jpeg_read_coefficients() and jpeg_write_coefficients() (in place of the ordinary jpeg_read_scanlines() and jpeg_write_scanlines()) to read and write the raw, compressed data without actually decompressing and recompressing it.

See the section "Really raw data: DCT coefficients" in the libjpeg documentation. Be sure to read all the caveats mentioned there.

like image 38
Adam Rosenfield Avatar answered Feb 02 '26 14:02

Adam Rosenfield



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!