Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to re-encode a BMP made of JPG back to JPG without loss of quality?

Sometimes I save JPG images as uncompressed bitmap (BMP/PNG), to keep the quality when I make changes to the image.

I was wondering, is it theoretically possible to re-encode the bitmap back to its original JPG format, without losing any quality, (except for the areas I edited) ?

Edit: I was thinking somehow to brute-force it to find the original JPG information setting for that block of BMP data, and thus generating JPG out of BMP (which was JPG before) without any difference to original JPG. I don't know enough about JPG format to say if it's even possible, but I can't think why not, at least in some finite time you could brute-force 8x8 block?

like image 401
Rookie Avatar asked Feb 17 '23 04:02

Rookie


1 Answers

JPEG compression is lossy, so you will lose some information in the .bmp when you re-encode it as a JPEG. If the image is trivial (for example 1 black pixel1 black all black for example, 1 pixel) you may be able to re-encode without loss.

You can see an example of JPEG being re-encoded multiple time here.

You can do some operations on a JPEG which are lossless, from wikipedia :

A number of alterations to a JPEG image can be performed losslessly (that is, without recompression and the associated quality loss) as long as the image size is a multiple of 1 MCU block (Minimum Coded Unit) (usually 16 pixels in both directions, for 4:2:0 chroma subsampling). Utilities that implement this include jpegtran, with user interface Jpegcrop, and the JPG_TRANSFORM plugin to IrfanView.

Blocks can be rotated in 90 degree increments, flipped in the horizontal, vertical and diagonal axes and moved about in the image. Not all blocks from the original image need to be used in the modified one.

The top and left edge of a JPEG image must lie on a 8 × 8 pixel block boundary, but the bottom and right edge need not do so. This limits the possible lossless crop operations, and also prevents flips and rotations of an image whose bottom or right edge does not lie on a block boundary for all channels (because the edge would end up on top or left, where – as aforementioned – a block boundary is obligatory).

When using lossless cropping, if the bottom or right side of the crop region is not on a block boundary then the rest of the data from the partially used blocks will still be present in the cropped file and can be recovered.

It is also possible to transform between baseline and progressive formats without any loss of quality, since the only difference is the order in which the coefficients are placed in the file.

Furthermore, several JPEG images can be losslessly joined together, as long as the edges coincide with block boundaries.

like image 58
sbridges Avatar answered Apr 28 '23 17:04

sbridges