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

156 Views Asked by At

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).

4

There are 4 best solutions below

7
John Bollinger On BEST ANSWER

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.

21
Tripp Kinetics On

You will not lose fidelity when converting to PPM. Why? It's a lossless format.

It's also an uncompressed format. I would highly recommend using something else, though, otherwise you're being extremely inefficient with your disk space.

Why is particularly inefficient? Because you've already lost your fidelity through the JPEG compression. You are not gaining any fidelity back by storing in an uncompressed format. You are just faithfully storing your image after the fidelity has already been lost.

7
Ted Lyngmo On

PPM has up to 65536 different values per channel (RGB) which makes it capable of representing everything that comes out of a JPEG (unless there is some type of super-JPEG around). Normal JPEGs have 256 values per channel.

None of the information in the uncompressed JPEG needs therefore to be lost when rendering the picture on a virtual PPM canvas.

PPM also stores this representation losslessly, so no degradation will happen in this process. It also stores it uncompressed, which will likely make the files huge. You could use some other lossless fileformat, like PNG to store your pictures instead. PNG compresses the image without loosing information.

Note 1: There's "true PPM" and variations. The sample values in PPM are nonlinear and some constants and a gamma transfer function is used. To not actually loose any information, you need to use a reader which uses the same constants and transfer function when reading the PPM file.

Note 2: A fileformat being uncompressed is not a guarantee for losslessness when doing the conversion from JPEG to this other format. There are uncompressed fileformats not even capable of representing everything that comes out of a JPEG, so there you would have a loss when rendering the picture on the virtual canvas. It would store it uncompressed, so no further loss would happen, but then it's already too late.

Note 3: If you have the option, store the original pictures in a lossless and compressed format instead of using JPEG. JPEG is inherently lossy so there you irreversibly loose quality.

0
user3344003 On

To begin, there is no such thing as "quality" that is inherent in JPEG. Some JPEG CODECs, have a "quality" setting that is just a simple means of selecting quantization tables. "Quality" settings do not have the same effect on different. Quality=90 is not necessarily the same as quality=90 in a different encoder. I've seen encoders use 1 to 5, 1 to 10, and 1 to 100 quality settings and encoders that do not use a quality setting at all.

The issue in JPEG is loss, not quality.

The loss in JPEG occurs going into JPEG and not (generally) going out of JPEG. The RGB=>YCbCR conversion can introduce loss. If used, subsampling introduces loss. The DCT can produce floating to integer conversion loss. Quantization is a big source of loss.

You can have a JPEG decoder that introduces loss but that is not strictly necessary.

  1. An image starts with some uncompressed bitmap format.

  2. That gets converted to a JPEG. In nearly all cases that will result in some loss.

  3. Now you are converting the JPEG to PPM, that will not necessarily create additional loss.