cimagejpegppmlibjpeg-turbo

Do I lose quality if I decompress a JPEG file into a ppm using libjpeg-turbo C library?


I was trying to turn a jpg photo into a pixel map. I discovered that I can do it with the C library libjpeg-turbo by transforming the jpg photo into ppm. With djpeg jpegfile.jpg imagefile.ppn.

Does the file imagefile.ppn lose quality compared to jpegfile.jpg? Why?

I'm more interested in a technical answer. I'm studying a bit of programming and image formats and I have this doubt, I read the official libjpeg-turbo website but I couldn't find any information about it. By doing the opposite I know you lose quality and the site explains it (from ppn to jpg).


Solution

  • The form of a digital image that is captured by a digital camera or can be displayed on a screen or printed on paper is a raster (regular 2D array) of digitized pixel colors. This is sometimes called a "bitmap".

    So,

    Does the file imagefile.ppn lose quality compared to jpegfile.jpg?

    It's a bit complicated, but the simple answer is no, not if the image converter does its job correctly.

    There is a "quality" factor associated with JPEG because it does not represent bitmaps directly. It employs a lossy representation that reduces the space required to represent images. That algorithm has an adjustable tradeoff between image size and lossiness that is characterized by what has come to be known as a quality factor: lower quality means smaller and lossier. That effect is incurred when the original image, whether on the image sensor of a digital camera, in computer memory, or in a file, is converted to JPEG format. It is then baked into that JPEG -- it represents a slightly different image than the original, because some information has been lost.

    To display a JPEG on a screen or convert it to a different format or otherwise edit it, it needs to be converted back to a bitmap. The result will not exactly match the original, but if the reconstruction is done correctly then a given JPEG should yield the same bitmap every time. There need not be any additional data loss at this step.

    PPM is different. It represents images directly in the form of bitmaps. PPM's bitmaps have three color channels supporting up to 16-bit depth each, which is more than your monitor does, very likely more than the images you are working with do, and more than your eyes can resolve. It can directly represent the bitmap produced by decompressing a JPEG without any loss of detail.

    It gets more complicated, though, when you bring color spaces, non-linearity, and similar considerations into it. These are not what JPEG's quality factor is about, but they do constitute another image-representation dimension that makes room for a PPM (or other image file) to be interpreted differently than the original image. JPEG's native color space is YCbCr, whereas PPM's is a calibrated, gamma-corrected RGB color space. Conversion is necessary from one to the other, and this can introduce slight color distortions, especially with narrow color channels.

    Note also that there are other image formats available. Many employ one or another form of lossless compression to reduce images' storage sizes, which can be a distinct advantage over PPM. GIF was at one time a major format, but it has somewhat fallen out of favor. PNG seems to be winning the bitmap wars at the moment. It is a more complex format than PPM, but very good, with built-in support for lossless compression.