Is it possible to transcode BMP from JPG back to JPG without quality loss?

Sometimes I save JPG images as an uncompressed bitmap image (BMP / PNG) to maintain quality when making changes to the image.

I was wondering if it is theoretically possible to transcode a bitmap back to its original JPG format without loss of quality (except for the areas that I edited)?

Edit: I somehow thought about this in order to find the original JPG info parameter for this BMP data block and thus pulled the JPG from BMP (which was JPG before) without any difference in the original JPG. I don’t know enough about the JPG format to say if it’s possible, but I can’t understand why not, at least at some point in time, could you copy the 8x8 block?

+4
source share
4 answers

JPEG compression is lost, so you will lose some information in .bmp when re-encoding as JPEG. If the image is trivial (for example, 1 black pixel, 1 black, all black, for example, 1 pixel), you can recode without loss.

You can see an example of multiple JPEG encoding here .

You can perform some lossless JPEG operations, wikipedia :

Several changes to a JPEG image can be done without loss (that is, without recompression and the associated loss of quality), because if the image size is a multiple of 1 MCU (minimum Unit encoding) (usually 16 pixels in both directions, for a 4: 2: 0 chroma subsample ) Utilities that implement this include jpegtran, with the Jpegcrop user interface and the JPG_TRANSFORM plugin for IrfanView.

Blocks can be rotated in 90-degree increments, flipped in the horizontal, vertical and diagonal axes and moved around the image. Not all blocks from the original image should be used in a modified one.

The top and left edges of the JPEG image should lie on a 8 × 8 pixel border block, but the bottom and right edges should not. These restrictions are possible lossless operations, and also prevents flips and rotations of the image, the lower or right edge of which does not lie on the block border for all channels (because the edge will be on the top or left, where - as already mentioned - the block border is required).

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

It is also possible to convert between basic and progressive formats without losing quality, since the only difference is the order in which the coefficients are placed in the file.

In addition, multiple JPEG images can be losslessly combined together, because if the edges coincide with the boundaries of the blocks.

+4
source

Based on sbridges' detailed answer and the specific part of your question, I would say Yes .

(I was wondering, it is theoretically possible to recode the bitmap back to the original JPG format, without losing any quality, (except for the areas that I edited)

I think the main problem here is that you need to edit JPEG directly. You lose data when editing a block. So you can change it in BMP to edit it, but then you will need to track (or later compare and identify) the pixels that you are changing. Then you will need to edit these specific blocks in JPEG and leave all the unedited blocks intact. Thus, all unedited blocks will not have a loss of quality, but the blocks you changed will have a loss of decoding / changing / recoding quality.

Due to the complexity of the actual implementation, this is possible, but it may not be worth the amount of work.

+2
source

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.

+2
source

Basically, we are dealing with image format and compression here. First, let me focus on the process that allows you to convert a JPEG image to BMP. BMP is formed with display of pixels with RGB values. Thus, in terms of the visual aspect and in terms of edges, you should not observe the difference between the original JPEG image and the conversion of this picture to BMP (resolution problems are highlighted).

On the contrary, as other participants say here ( http://en.wikipedia.org/wiki/JPEG ), the JPEG format uses a compression matrix (DCT), which will somehow change the gradient of your picture.

In most cases, I would say that this will not work, an exception is made from simplified images (uniform shape on a uniform background color). It will not work on video or video.

+1
source

Source: https://habr.com/ru/post/1486851/


All Articles