Is it possible to convert JPEG to a lossless format and convert it back without losing quality?

2

Say I have a lossless version of an Image that is previously compressed in JPEG format (like if you open a JPEG image in mspaint and save it as BMP, I know that some metadata would be lost in this process, but that's not my concern)

I'd like to know if it's possible and how to convert it back to JPEG format without losing image quality, meaning that every pixel is exactly the same?

EDIT: I fully understand that converting a JPEG image to a lossless format won't give me any extra quality. I'm asking this because: I have a PNG file extracted form a SWF file. Originally (in the SWF file) the object was JPEG image + alpha channal, which filesize is much smaller comparing to the PNG file I have. I want the original format (JPEG + alpha channel) yet I don't have any copies of the original SWF file anymore (and I failed to find one online either) so I can't redo the extract process.

RadarNyan

Posted 2017-06-22T08:15:06.833

Reputation: 23

Is this actually just about your understanding or do you want to do this (and if so, why)? – Dave – 2017-06-22T08:21:43.720

1I still don't get your problem - but as a rule of thumb, any (further) lossy encoding reduces the quality - it's only a best-case-scenario that can't be achieved without much effort (manually fine-tuning parameters, using "the best" tools,...) to get the same quality out of a lossy compression. And since JPEG doesn't offer much fine-tuning...

So the answer is: no. However, it's possible that the further loss of quality won't be visible - but that's something you have to try out because it really depends on the used formats (encoders), the picture in question and the use-case. – flolilo – 2017-06-22T11:43:12.030

Answers

3

What you are asking, essentially, is whether the decode process for a JPEG image can be reversed. This is a very different intent from that of a conventional JPEG encoder, and I am not aware of any software which accomplishes this. Mathematically this is possible* so long as the image has not had a transform or other modification applied after it was encoded, but programatically how difficult could it be?

The first problem is colour space - JPEG uses subsampled (the colour sampled over a group of pixels) yCbCr whereas a bitmap is RGB. Inevitable losses converting between these colour-spaces (and also in any gamma adjustment) introduce noise. The second problem to solve is JPEG records the image data as macroblocks (NxN sized sub-images), which must be inferred from the decoder's output image. A more in-depth explanation of the JPEG format and encoding process can be found here http://www.guillermito2.net/stegano/jsteg/

'un-decoding' a JPEG:

To create an approximation of the original JPEG data, we need to find the subsampling, size and alignment of macroblocks, and Discrete Cosine Transform (DCT) coefficients that were used to encode them. The rest is identical to a normal JPEG encoding process. One naive implementation that comes to mind is "educated guess and check": assume some common JPEG implementations were used to create the image, and try applying them until one is found that has 'JPEG-like' results from the DCT (typically zeros for high frequencies).

After that, fine-tune the coefficients for minimum error compared to the original (i.e. bitmap of the compressed image). This process will still result in tradeoff between error and filesize, but it should get close enough for practical purposes.

Source: Previous employment verifying DNx and ProRes video codecs, which apply JPEG-like coding to each individual frame (software reversing decoded frames would have been a very interesting and potentially useful side project).

* A set of input data exists that will produce identical output when passed through the original JPEG decoder, however it is highly improbable for any reconstruction to be a byte-wise copy (ignoring metadata) of the original file because information was lost during the decoding process.

Liam

Posted 2017-06-22T08:15:06.833

Reputation: 46

4

JPEG is a lossy format and there is no way around it. Even at compression quality of 100 the compression ratio is still 2.6:1 (Wikipedia JPEG).

There were some variants proposed for Lossless JPEG, such as JPEG 2000, but they have not caught on and their support is very poor.

You would be better off converting to a lossless format, such as PNG, which still compresses but without loss.

harrymc

Posted 2017-06-22T08:15:06.833

Reputation: 306 093

1Normally yes, though in the jpg -> png -> jpg process, it should be theoretically possible for the png -> jpg process to apply the same compression method as what produced the original jpg. However, in practice this is almost impossible to do on purpose. Also if there has been even slight editing then this almost always results in the 2nd jpg compression struggling to compress the jpg artifacts from the 1st compression that no longer line up properly. – BeowulfNode42 – 2017-06-22T12:32:41.993

0

If you open a JPEG image with any editor and save it as BMP, you're not getting any more quality than what's already available in your .jpg file. That'd be like converting a low-bitrate mp3 file (for example, 128kbps) to a much better format like flac, hoping that somehow the quality would magically be restored. It won't.

Information was lost when you first saved the image in jpg, like harrymc has said; there's no way to get that information back from this jpg file.

Lossy compression is "destructive", or also known as "irreversible". See https://en.wikipedia.org/wiki/Lossy_compression.

Marc.2377

Posted 2017-06-22T08:15:06.833

Reputation: 1 072

0

Information was almost certainly lost converting to a JPEG. Converting that file to a BMP will preserve the information stored in the JPEG, but converting the BMP back to a JPEG will probably cause you to lose even more informaion

PiedPiper

Posted 2017-06-22T08:15:06.833

Reputation: 206