Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is theoretically possible to losslessly encode a BMP image known to come from a JPEG format back into a JPEG format. That is, you can do the following transformation:</p> <p><code>JPEG A =&gt; BMP X -&gt; JPEG B =&gt; BMP X</code></p> <p>where <code>=&gt;</code> is a decompression operation and <code>-&gt;</code> is an compression operation.</p> <p>Note that this is not <em>quite</em> what you are asking. You are asking the above <strong>plus</strong> that <code>JPEG A == JPEG B</code>. I doubt (but do not know for sure) that every JPEG representation decodes to a unique image, so my guess is that this guarantee cannot be made.</p> <p>The reason it is possible to do the lossless encoding is that there is a finite number of JPEG representations of an image of a specific size and depth. This number is unmanageably large but it is nevertheless finite. An encoding algorithm could simply be to decode each one of these representations until you find the one that matches your BMP image exactly and then output that representation. The algorithm is completely impractical but shows the problem is bounded.</p> <p>A practical algorithm may well exist. For example, one could imagine an iterative approach like:</p> <ol> <li>Choose initial compression parameters (color sampling and quantization).</li> <li>Compress BMP image to candidate JPEG.</li> <li>Decompress candidate JPEG.</li> <li>Compare decompressed image with original BMP. <ul> <li>If the same, output the candidate JPEG and terminate.</li> <li>If different, update the compression parameters and return to step 2.</li> </ul></li> </ol> <p>The trick would be in that update step - figuring out how the image diffs could guide you to improve the parameters.</p> <p>You might be able to write code that would work in the vast majority of cases. There are <a href="http://en.wikipedia.org/wiki/Minimum_coded_unit#Downsampling" rel="nofollow">basically only 3 color sampling choices</a> and my guess is there aren't <em>that</em> many algorithms to generate quantization matrices. You could implement them all, or alternatively scan the internet for JPEG files and record unique compression parameters. Then you would expect to fail primarily on images from newly written compressors.</p> <p>It is important to note that these approaches depend on using the same decompressor for the <code>=&gt;</code> operations, as different decompressors may not produce the same image bit for bit from one JPEG file.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload