Lossless Transformation

Ask for help and post your question on how to use XnView Classic.

Moderators: XnTriq, xnview

Post Reply
Posts: 2
Joined: Fri Nov 06, 2009 11:49 am

Lossless Transformation

Post by Dolmen »

First I just want to thank the XnView developers for a very good program!

Then the question:
I'm trying to find out why I shouldn't always use the "JPEG Lossless transformation" when I'm rotating JPEG pictures.
Is there some downside to use this vs. the standard rotation technique? Have tried to search around but haven't found any clear answer...

Thanks in advance!
User avatar
Posts: 2010
Joined: Wed Mar 17, 2004 8:33 pm
Location: Sarasota Florida

Re: Lossless Transformation

Post by JohnFredC »

The issue (as I understand it) is that jpg compression uses a lossy algorithm. The jpeg compression method works in part by permanently discarding some of the data that constitutes the image (such as color and/or detail).

Read on to find out how this affects rotating a jpg image.

About JPG

Every time you save a jpg, the image is "compressed" to make it smaller (ever notice how much smaller jpgs are than other image file types?) This is great, but for the following:

The jpeg compression algorithm "averages" several pieces of the image data into one piece of data, thus reducing the amount of data that needs to be saved (and making the file smaller).

The more often one saves a jpg, the more data gets discarded. Eventually, the appearance of the image will suffer.

The amount of data that gets thrown away varies based upon the "quality" setting in most image editors' jpg options. For instance, in XnView go to Options->Read/Write, select the Write tab, and then select jpg from the list of extensions. Several controls will appear in the panel on the right. One is the "Quality" slider.

The higher the value of the "Quality" setting, the fewer bits (of image data) that get thrown away when the file is saved. So: higher quality values lead to larger file sizes. On the other hand, lower quality settings lead to smaller file sizes.

You can see the results of repeated recompression as you zoom into jpg files that have been opened and resaved many times, or saved only once but with very low quality. Notice that many of the pixels get averaged together so that the image looks almost like a checkboard? Or perhaps you can see a little "fuzz" around the text in the image, or near a straight line. Or banding of the colors in a blue sky.

It is also possible you won't see these artifacts until you have saved the jpg a few times... modern digital cameras take very high-resolution images, the recompression artifacts may not be apparent to the casual eye. Initially, one may need to zoom to 100% in the viewer to see the jpg artifacts caused by the compression. After many jpg saves, however, the disruption of the image will become readily apparent.

You can experiment with jpg compression using the XnView File->Export dialog. It previews the artifacts associated with any quality level before the file gets saved and is a very useful tool for optimizing the relationship between file size and image quality.

JPG Lossless Rotation

When a computer rotates an image, it goes through the image and reassigns the mapping (into the image display) of each pixel in such a way as to cause the image to appear to be rotated.

For a jpg file, you should now understand that if you save the jpg after a so-called "standard" rotation, you will be throwing away some of the data in the image and introducing the visual "artifacts" mentioned above. In many circumstances this is undesirable.

Note that, for other image types that do not use the jpeg lossy compression algorithm (PNG, TIF, BMP, etc), the "standard" rotation method does not lead to image degradation.

Fortunately, the manner in which the jpeg algorithm "averages" the image data also allows it (under certain circumstances) to "rotate" that data by increments of 90° in such a way that the file does not have to be recompressed upon saving it afterwards. There are a lot of mathematics involved, but essentially, if the number of pixels in each dimension of the image meet certain criteria, the image data can be manipulated to effect rotation and save the results into the file without recompressing the data.

Note also that "in between" rotation angles (37°, for instance) cannot be performed losslessly.

Hence the introduction of "Lossless jpeg transformations" into XnView and other image editors. Lossless rotation rearranges the compressed jpg image data "in place", without decompressing/recompressing the images. For images that meet the appropriate pixel dimension criteria, one can "losslessly" rotate a jpg image (by a multiple of 90°) as many times as desired, with no further effect on image quality. A very good thing, IMO.

But what if the pixel dimensions of the image data do not "allow" the lossless rotation to be "lossless"? In those circumstances, most graphics tools (including XnView, I believe) will slice tiny portions off the sides of the image to force it to meet the criteria necessary for "lossless" rotation, then the lossless rotation is performed on the "corrected" image.

So, in such cases, the image dimensions may not be exactly the same after lossless rotation occurs, but the quality of the remaining image will not be affected.

To summarize: for jpgs, lossless rotation is a good thing and should always be used if possible.
User avatar
Moderator & Librarian
Posts: 5660
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Lossless Transformation

Post by XnTriq »

You sure got a way with words, John! XnTriq vewwy vewwy jellious :P

This post belongs in the User Guide and the FAQs.
JohnFredC ([url=http://newsgroup.xnview.com/viewtopic.php?p=31192#p31192]Let use jpeg loseless rotate every time it is possible[/url]) wrote:So, my own preferred rotation logic for a single rotation command would be:

Code: Select all

If image is jpeg then 
   Evaluate jpeg lossless rotation
   If jpeg lossless rotation causes lost pixels then
      Explain and prompt user to choose lossless or lossy, etc.
      Perform user choice
      Perform jpeg lossless rotation
   Perform "standard" rotation
User avatar
Posts: 146
Joined: Thu Feb 23, 2006 3:18 am
Location: San Luis Obispo, California

Re: Lossless Transformation

Post by Ty »

Outstanding reply, JohnFredC. I agree with XnTriq, this needs to be in the permanent documentation. My understanding is that the permanent help doc is now a Wiki that Pierre has set up. Your treatise needs to go there.
Posts: 2
Joined: Fri Nov 06, 2009 11:49 am

Re: Lossless Transformation

Post by Dolmen »

Thanks for this very informative answer, JohnFredC! (Don't think I even could get a better one ;))
Then I know what to choose from now on.

Lets just hope that this comes as a "always use" setting in a future release.

Have a nice weekend everyone!
Post Reply