Force lossless rotation of JPEGs

It must be understood that the dimension issue in JPEG lossless transformations is a core design flaw of the JPEG format.

It cannot be fixed without losing backwards compatibility.

I have proposed such fix, but there is little support for advancing JPEG in general, and so I must give priority to more important features. The effort to maintain such non-backwards-compatible correction would be too high in comparison to the benefits which only few people could appreciate.

It is a pity that we who appreciate the benefits of advanced JPEG have to deal with such curiosities, but what can we do?

So there is no automatic solution, and the best we can do is to apply manual adjustments for each individual case.
I have integrated several options for this purpose in my Jpegcrop utility:

See also the Jpegcrop Preferences and Options description.

A special trick which I use in some cases is to manipulate the image dimensions in the SOF (Start Of Frame) marker with a Hex editor so that it completes the block size. Other than trimming, this adds more or less appropriate content depending on the encoder (the IJG encoder simply replicates the right/bottom edge to fill the block, which is mostly acceptable when becoming visible by such patch).

This dimension patch method I also use regularly with Instagram images which sometimes deviate only by one or few pixels from a standard format (1080x1350 is very common, for example, and regularly you find 1080x1349 or so).
If the deviation is more than a block size, I combine this method with the reflecting crop extension feature of jpegtran which in most cases gives pretty results.
So a number of things can be done from case to case if you know the opportunities.

Regards
Guido
JPEG developer

2 Likes