Lossless flip/rotate

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

Moderators: XnTriq, xnview

kiwichick
Posts: 24
Joined: Mon May 21, 2007 10:29 pm

Lossless flip/rotate

Post by kiwichick »

Hi everyone, I apologise if these questions have been asked already but I couldn't find anything using search. I installed XnView MP yesterday and I was absolutely loving it until I ran into lossless transformation. So I'm wondering: Why can lossless transformation be turned off for rotate but not for flip? Why do lossless rotate and flip change the size of an image (eg: 1000x1000 becomes 992x1000)? Why is only the width affected? Not a lossless question, and I haven't tried all image filetypes, but why doesn't flip work for png or webp files? Thanks in advance for any help given :D
User avatar
XnTriq
Moderator & Librarian
Posts: 5922
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Lossless flip/rotate

Post by XnTriq »

Hi kiwichick (-:
kiwichick wrote: Sun Oct 03, 2021 9:10 pmWhy do lossless rotate and flip change the size of an image (eg: 1000x1000 becomes 992x1000)? Why is only the width affected?
Could you please upload a sample?

kiwichick wrote: Sun Oct 03, 2021 9:10 pmNot a lossless question, and I haven't tried all image filetypes, but why doesn't flip work for png or webp files?
You mean ToolsJPEG lossless transformationsFlip horizontal | Flip vertical?

Available commands in ToolsSettings…InterfaceToolBar:
  • Browse mode
    • cmd_lhflip = Flip horizontal (JPEG lossless transformation)
    • cmd_lvflip = Flip vertical (JPEG lossless transformation)
  • View mode
    • cmd_FlipM = Mirror [menu]
    • cmd_FlipH = Horizontal
    • cmd_FlipV = Vertical
    • cmd_lhflip = Flip horizontal (JPEG lossless transformation)
    • cmd_lvflip = Flip vertical (JPEG lossless transformation)
kiwichick
Posts: 24
Joined: Mon May 21, 2007 10:29 pm

Re: Lossless flip/rotate

Post by kiwichick »

Thanks for your reply, XnTriq.
XnTriq wrote: Mon Oct 04, 2021 5:45 pmCould you please upload a sample?
* Here you can see the difference before and after rotate. The same thing happens for flip.
2021-10-08_102605.png

XnTriq wrote: Mon Oct 04, 2021 5:45 pmYou mean ToolsJPEG lossless transformationsFlip horizontal | Flip vertical?
* No, I don't because that option is greyed out for non-jpg files.


XnTriq wrote: Mon Oct 04, 2021 5:45 pmAvailable commands in ToolsSettings…InterfaceToolBar:
* In Browse, I have added the Flip horizontal and Flip vertical buttons to the toolbar but they don't appear to work for anything other than jpg files. I've tried png, webp, bmp, and ico. Here you can see for this png file the flip buttons are "active" but they don't work. (The rotate buttons do.)
2021-10-08_105830.png
* In View, I didn't realise there were two different sets of flip buttons (as you indicated above, one for Browse and one for View) but both are available in the Interface / ToolBar / View mode options (is that a bug?). Here you can see both horizontal buttons.
2021-10-08_111112.png
Initially, I had just added the same buttons as for Browse but they didn't work. I've now changed those to the other ones (Horizontal and Vertical) and they work.
* I was going to try adding custom buttons in Browse using the cmd_FlipH/cmd_FlipV commands but is that possible? When I add -- COMMAND -- and click Set command, all I get is a browse dialog to select a file. I can't see how to enter a command.
You do not have the required permissions to view the files attached to this post.
Last edited by kiwichick on Fri Oct 15, 2021 5:37 am, edited 1 time in total.
User avatar
XnTriq
Moderator & Librarian
Posts: 5922
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Lossless flip/rotate

Post by XnTriq »

kiwichick wrote: Thu Oct 07, 2021 10:22 pm
XnTriq wrote: Mon Oct 04, 2021 5:45 pmCould you please upload a sample?
* Here you can see the difference before and after rotate. The same thing happens for flip.
Is it possible to attach colorful-floral-pattern-seamless-background-vector-1815061.jpg (1000×1000 pixels) so I can do some tests?

kiwichick wrote: Thu Oct 07, 2021 10:22 pm* In View, I didn't realise there were two different sets of flip buttons (as you indicated above, one for Browse and one for View) but both are available in the Interface / ToolBar / View mode options.
You're right. I've since edited my previous post accordingly.

kiwichick wrote: Thu Oct 07, 2021 10:22 pm* In Browse, I have added the Flip horizontal and Flip vertical buttons to the toolbar but they don't appear to work for anything other than jpg files. I've tried png, webp, bmp, and ico. Here you can see for this png file the flip buttons are "active" but they don't work. (The rotate buttons do.)
As you probably know, JPEG compression is lossy, and each time a JPEG image is re-saved, it is re-encoded. This process introduces further quality degradation known as “generation loss”. However…
https://en.wikipedia.org/wiki/JPEG#Lossless_editing wrote: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)
https://www.impulseadventure.com/photo/jpeg-minimum-coded-unit.html wrote:Every JPEG image is actually stored as a series of compressed image tiles that are usually only 8x8 pixels in size. The proper name for one of these tiles is the MCU or Minimum Coded Unit. When one refers to image blocking artifacts, they are really talking about visual discontinuities observed between one or more of these tiles. You can see the edges or boundaries of these tiles in JPEG images that have been compressed at a very low quality.
[…]
The Minimum Coded Unit size for JPEG images is usually 8x8, 16x8 or 16x16 pixels in size. The variations here (more specifically 16x8 and 16x16) are due to an optimization called chroma subsampling.
XnView MP's commands for “JPEG lossless transformations”:
  • cmd_lhflip = Flip horizontal
  • cmd_lvflip = Flip horizontal
  • cmd_lrotate90 = Rotate clockwise
  • cmd_lrotate180 = Rotate 180°
  • cmd_lrotate270 = Rotate counter clockwise
  • cmd_lrotateExif = Rotate based on EXIF orientation field
  • cmd_lresetExif = Reset EXIF orientation field
So It's not a bug that they don't work on other image formats. (Come to think of it, there should be an error message, though.)

Activating Use lossless rotation (if possible) in ToolsSettings…BrowserMisc.Rotation should make the following commands lossless when used on JPEGs:
  • Browse mode
    • cmd_rotate180 = Rotate 180°
  • View mode
    • cmd_FlipH = Horizontal
    • cmd_FlipV = Vertical

Related settings:
  • ToolsSettings…BrowserMisc.Rotation
    • Change EXIF orientation ONLY (if possible)
  • ToolsSettings…GeneralFile operations
    • Make backup for lossless flip/rotation
    • Make backup for lossless crop
    • Make backup for all other lossless operations
    • Keep original date/timeLossless transformations
kiwichick
Posts: 24
Joined: Mon May 21, 2007 10:29 pm

Re: Lossless flip/rotate

Post by kiwichick »

Thanks again, sorry for the delay in replying.
Is it possible to attach colorful-floral-pattern-seamless-background-vector-1815061.jpg (1000×1000 pixels) so I can do some tests?
I've attached that image but I don't see how it will help. I've tried several images and the change in size has happened for every one of them. And I've just noticed the size change is from cropping the file not shrinking the file.
seamless-colorful-floral-pattern-seamless-background-vector-1815061.jpg
As you probably know, JPEG compression is lossy
Yes, I understand JPEG compression is lossy. I've had to turn off "use lossless rotation (if possible)" to stop the size change happening. But it doesn't help the size change which still happens for flip. Why can't we turn off lossless flip?
So It's not a bug that they don't work on other image formats.
I realise it's not a bug that they don't work on other image formats but why are the buttons active for other image formats? And that wasn't what I was referrring to when I asked if it was a bug. I was referrring to the fact that Flip Vertical/Flip Horizontal and Vertical/Horizontal were all available in View options. You've confirmed it's not a bug but it does have me asking why, then, aren't the Vertical/Horizontal buttons available in Browse options? How do I get Vertical/Horizontal flip for non-jpg files in Browse?

And could you please answer my question about if it's possible to use custom buttons with XnView commands? And if it is, how do I do that?
You do not have the required permissions to view the files attached to this post.
User avatar
XnTriq
Moderator & Librarian
Posts: 5922
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Lossless flip/rotate

Post by XnTriq »

kiwichick wrote: Mon Oct 11, 2021 8:47 pmI realise it's not a bug that they don't work on other image formats but why are the buttons active for other image formats? And that wasn't what I was referrring to when I asked if it was a bug. I was referrring to the fact that Flip Vertical/Flip Horizontal and Vertical/Horizontal were all available in View options. You've confirmed it's not a bug but it does have me asking why, then, aren't the Vertical/Horizontal buttons available in Browse options? How do I get Vertical/Horizontal flip for non-jpg files in Browse?
These questions have to be answered by Pierre, who is the (sole) developer of XnView. (I'm just a user.)

kiwichick wrote: Mon Oct 11, 2021 8:47 pmAnd could you please answer my question about if it's possible to use custom buttons with XnView commands? And if it is, how do I do that?
Do these instructions work for you?
XnTriq wrote: Tue Oct 14, 2014 2:45 am
  1. Click somewhere on the toolbar with the right mouse button and select Customize toolbar... in the context menu.
  2. Select a mode (Browse mode or View mode).
  3. Select -- COMMAND -- in the list on the left and add it to the list on the right by clicking on the button.
  4. Click on Set command... to specify an EXE/BAT file or an XBS script.
  5. Click on Set label... to define a tooltip.
  6. Click on the blank button to select an icon.
Mrakobes666 wrote: Tue Aug 18, 2020 8:48 amyou can set an internal command, for example

cmd_googleMap

Image

kiwichick wrote: Mon Oct 11, 2021 8:47 pmI've attached that image but I don't see how it will help. I've tried several images and the change in size has happened for every one of them. And I've just noticed the size change is from cropping the file not shrinking the file.
Chroma sub-sampling of seamless-colorful-floral-pattern-seamless-background-vector-1815061.jpg is 2x2,1x1,1x1, which means that the MCU size is 16×16 pixels. Because its dimensions (1000×1000 pixels) are not a multiple of 16, partial MCUs (i.e. non-transformable edge blocks) are discarded/trimmed/dropped by XnView when a lossless flip/mirror or rotate operation/transformation is executed (62×16=992):

showAgain.png
  • cmd_lhflip → 992×1000 pixels
  • cmd_lvflip → 1000×992 pixels
jpegcrop.png
https://www.jpegclub.org/jpegcrop/index.html wrote:Transform Trim Edges

Drop non-transformable edge blocks.
Here is what the IJG v9 usage.txt file says about this switch:
The transpose transformation has no restrictions regarding image dimensions. The other transformations operate rather oddly if the image dimensions are not a multiple of the iMCU size (usually 8 or 16 pixels), because they can only transform complete blocks of DCT coefficient data in the desired way.

jpegtran's default behavior when transforming an odd-size image is designed to preserve exact reversibility and mathematical consistency of the transformation set. As stated, transpose is able to flip the entire image area. Horizontal mirroring leaves any partial iMCU column at the right edge untouched, but is able to flip all rows of the image. Similarly, vertical mirroring leaves any partial iMCU row at the bottom edge untouched, but is able to flip all columns. The other transforms can be built up as sequences of transpose and flip operations; for consistency, their actions on edge pixels are defined to be the same as the end result of the corresponding transpose-and-flip sequence.

For practical use, you may prefer to discard any untransformable edge pixels rather than having a strange-looking strip along the right and/or bottom edges of a transformed image. To do this, add the -trim switch.

Obviously, a transformation with -trim is not reversible, so strictly speaking jpegtran with this switch is not lossless. Also, the expected mathematical equivalences between the transformations no longer hold. For example, "-rot 270 -trim" trims only the bottom edge, but "-rot 90 -trim" followed by "-rot 180 -trim" trims both edges.
You do not have the required permissions to view the files attached to this post.
kiwichick
Posts: 24
Joined: Mon May 21, 2007 10:29 pm

Re: Lossless flip/rotate

Post by kiwichick »

XnTriq wrote: Tue Oct 12, 2021 3:00 amDo these instructions work for you?
Yes they do but as they say, and as I've already pointed out, I only get to select an EXE/BAT file or XBS script. How do I add an XnView command (eg: cmd_FlipH)?
XnTriq wrote: Tue Oct 12, 2021 3:00 amBecause its dimensions (1000×1000 pixels) are not a multiple of 16
The popup says it must be a multiple of 8, which 1000x1000 is. Thanks for the time you've taken to try and explain it all to me but I am now completely confused. I've never come across this with other image viewer I've used. They flip and rotate without any size change. I guess I'll just have to make sure I don't do any flipping or rotating if I'm going to continue using XnView. Your help is much appreciated.
Mrakobes666 wrote: Tue Aug 18, 2020 8:48 amyou can set an internal command
But how? As I've already said I only get the option to select a file.
User avatar
XnTriq
Moderator & Librarian
Posts: 5922
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Lossless flip/rotate

Post by XnTriq »

kiwichick wrote: Tue Oct 12, 2021 9:26 pmThanks for the time you've taken to try and explain it all to me but I am now completely confused.
Causing confusion is the opposite of what I intended to achieve. Unfortunately, JPEG compression is a very complex subject :-|

kiwichick wrote: Tue Oct 12, 2021 9:26 pmI've never come across this with other image viewer I've used. They flip and rotate without any size change.
I've yet to see an ideal solution.
https://www.raymond.cc/blog/rotating-photos-or-pictures-in-windows-photo-viewer-causes-quality-loss/ wrote:If you’ve been a user of Windows XP, you might have encountered a scary warning informing you that “Because of the dimensions of this picture, rotating it might permanently reduce its quality. Rotating a picture automatically saves it using the original name”.

Basically it is telling you that rotating the picture will reduce the quality and overwrites the original file without creating a backup copy. Fortunately this warning message will not appear on all images except for some with certain dimensions. Rotating a JPG image without any quality loss is called lossless rotation and this feature is actually already supported since Windows XP, although there are many claims stating otherwise just because of the file size difference.

If you rotate an image with a dimension that can be perfectly divided by 8, then every pixel on the image will be preserved. However, rotating an image that is not an exact multiple of 8 will cause quality loss and this is when the Windows Fax and Picture Viewer will show the warning.

kiwichick wrote: Tue Oct 12, 2021 9:26 pmThe popup says it must be a multiple of 8, which 1000x1000 is.
The correct popup message would be: “width and height must me a multiple of the MCU size”. The MCU size depends on the JPEG's chroma sub-sampling factor (ToolsSettings…FormatsWriteJPEG):
  • 8×8 pixels = 1x1,1x1,1x1 (best quality) = 4:4:4 = full horizontal resolution, full vertical resolution = no sub-sampling
  • 16×8 pixels = 2x1,1x1,1x1 = 4:2:2 = ½ horizontal resolution, full vertical resolution
  • 16×16 pixels = 2x2,1x1,1x1 (default) = 4:2:0 = ½ horizontal resolution, ½ vertical resolution

kiwichick wrote: Tue Oct 12, 2021 9:26 pmI only get to select an EXE/BAT file or XBS script. How do I add an XnView command (eg: cmd_FlipH)?
As far as I can tell, there's no way to add buttons for cmd_FlipH and/or cmd_FlipV to the toolbar of browse mode. I even tried to add these commands by creating a toolbar preset (foo.bar) with a text editor…

Code: Select all

[Browser]
cmd_flipM
cmd_flipH
cmd_flipV
cmd_lhflip
cmd_lvflip

[View]
cmd_flipM
cmd_flipH
cmd_flipV
cmd_lhflip
cmd_lvflip
… and importing it via ToolsSettings…InterfaceToolBarLoad… – to no avail.
Have you considered making a request in the “Suggestions” sub-forum?

kiwichick wrote: Tue Oct 12, 2021 9:26 pmI guess I'll just have to make sure I don't do any flipping or rotating if I'm going to continue using XnView.
If you turn off Use lossless rotation (if possible) in ToolsSettings…BrowserMisc.Rotation, images of any format – including “odd-sized” JPEGs – can be rotated mirrored with cmd_FlipH and cmd_FlipV in view mode without being trimmed.
kiwichick
Posts: 24
Joined: Mon May 21, 2007 10:29 pm

Re: Lossless flip/rotate

Post by kiwichick »

Thanks yet again, XnTriq
"Causing confusion is the opposite of what I intended to achieve."
Thanks, I realise that :) It's purely down to my lack of lossless knowledge.
"Have you considered making a request in the “Suggestions” sub-forum?"
I'm definitely going to do that. I just don't understand why it's not possible to use XnView commands for custom buttons. XnViewMP is supposed to be an improvement on XnView Classic so why would this awesome feature be removed?
"If you turn off Use lossless rotation"
Yes I've previously mentioned I had done that. But it's disappointing that it's only for rotation and can't be turned off for flip as well.
"rotated with cmd_FlipH and cmd_FlipV"
Sorry, confusion again. Rotate using Flip commands??? :?
User avatar
XnTriq
Moderator & Librarian
Posts: 5922
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Lossless flip/rotate

Post by XnTriq »

kiwichick wrote: Thu Oct 14, 2021 11:36 pmSorry, confusion again. Rotate using Flip commands??? :?
My bad! Corrected!

kiwichick wrote: Thu Oct 14, 2021 11:36 pm
XnTriq wrote: Wed Oct 13, 2021 2:00 amHave you considered making a request in the “Suggestions” sub-forum?
I'm definitely going to do that.
Thank you! We'll see what Pierre says.

kiwichick wrote: Thu Oct 14, 2021 11:36 pm
XnTriq wrote: Wed Oct 13, 2021 2:00 amIf you turn off Use lossless rotation (if possible)
Yes I've previously mentioned I had done that. But it's disappointing that it's only for rotation and can't be turned off for flip as well.
I'm sure Pierre will consider adding an option if you post another suggestion. Just remember: One request per topic helps him keep track.

kiwichick wrote: Thu Oct 14, 2021 11:36 pmI just don't understand why it's not possible to use XnView commands for custom buttons. XnViewMP is supposed to be an improvement on XnView Classic so why would this awesome feature be removed?

From “Custom Themes” (XnView Classic) for reference:
  • Commands available in browse mode
    • cmd_rotate90
    • cmd_rotate180
    • cmd_rotate270
    • cmd_losslessM
    • cmd_lrotate90
    • cmd_lrotate180
    • cmd_lrotate270
    • cmd_lhflip
    • cmd_lvflip
    • cmd_lrotateExif
    • cmd_lresetExif
  • Commands missing in browse mode
    • cmd_flipM
    • cmd_flipH
    • cmd_flipV