It is theoretically possible to losslessly encode a BMP image known from JPEG format in JPEG format. That is, you can do the following conversion:
JPEG A => BMP X -> JPEG B => BMP X
where => is the decompression operation, and -> is the compression operation.
Please note that this is not exactly what you are asking for. You are requesting the above plus that JPEG A == JPEG B I doubt (but don’t know for sure) that every JPEG representation is decoded into a unique image, so I assume that this guarantee cannot be fulfilled.
The reason why lossless coding can be done is because there are a finite number of JPEG images of a certain size and depth. This number is uncontrollably large, but nevertheless it is finite. The encoding algorithm may simply be to decode each of these representations until you find one that exactly matches your BMP image and then displays that representation. The algorithm is completely impractical, but shows that the problem is limited.
A practical algorithm may well exist. For example, you can imagine an iterative approach, for example:
- Select the initial compression settings (sampling and color quantization).
- Compress BMP image to JPEG candidate.
- Decompress JPEG candidate.
- Compare the compressed image to the original BMP.
- If this is the same, output the JPEG candidate and complete.
- If they differ, update the compression settings and return to step 2.
The trick will be in this update step - figuring out how image difference can help you improve settings.
You may be able to write code that will work in the vast majority of cases. There are basically only 3 color selection choices , and I assume that there are not many algorithms for generating quantization matrices. You can implement all of them, or alternatively scan the Internet for JPEG files and record unique compression settings. Then you expect that you will not work primarily on images from new compressors.
It is important to note that these approaches depend on using the same decompressor for => operations, since different decompressors may not create one image bit for a bit from a single JPEG file.
source share