简体   繁体   中英

jpegtran.exe not correctly rotating image

I have a freshly compiled libjpeg version 9 and tried running jpegtran.exe in command line with the arguments:

.\jpegtran.exe -rotate 180 -outfile test_output1.jpg testimg.jpg

testimg.jpg: 之前 test_output1.jpg: 后

As you can see it does rotate the image but it clips it and it's not put together correctly. The usage.txt file that comes with the package isn't totally up to date because I had to use the -outfile switch instead of what it says:

jpegtran uses a command line syntax similar to cjpeg or djpeg. On Unix-like systems, you say:

  • jpegtran [switches] [inputfile] >outputfile

On most non-Unix systems, you say:

  • jpegtran [switches] inputfile outputfile

where both the input and output files are JPEG files.

To specify the coded JPEG representation used in the output file, jpegtran accepts a subset of the switches recognized by cjpeg:

  • -optimize Perform optimization of entropy encoding parameters.
  • -progressive Create progressive JPEG file.
  • -arithmetic Use arithmetic coding.
  • -restart N Emit a JPEG restart marker every N MCU rows, or every N MCU blocks if "B" is attached to the number.
  • -scans file Use the scan script given in the specified text file.

See the previous discussion of cjpeg for more details about these switches. If you specify none of these switches, you get a plain baseline-JPEG output file. The quality setting and so forth are determined by the input file.

The image can be losslessly transformed by giving one of these switches:

  • -flip horizontal Mirror image horizontally (left-right).
  • -flip vertical Mirror image vertically (top-bottom).
  • -rotate 90 Rotate image 90 degrees clockwise.
  • -rotate 180 Rotate image 180 degrees.
  • -rotate 270 Rotate image 270 degrees clockwise (or 90 ccw).
  • -transpose Transpose image (across UL-to-LR axis).
  • -transverse Transverse transpose (across UR-to-LL axis).

Oddly enough (or maybe not), if I execute .\\jpegtran.exe -rotate 180 -outfile test_output2.jpg test_output1.jpg I get the original image back without any clipping issues. It's flipping the clipped parts but just not lining it up right with the rest of the image.

test_output2.jpg: 接连旋转180

I get the same result by executing jpegtran.exe -rotate 90 twice.

Also, I tried it on a larger .jpg file which resulted in the same issue but the file size was 18KB smaller for the output. I imagine the issue is related to this.


Edit - I also found this blurb which seems to describe the problem:

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.

The -trim switch works, if you can call it that, and trims out the disorganized data but the image is smaller and lost data.

test_output5.jpg: 调整后的输出

Adding the -perfect switch which supposedly stops the above from happening results in this: transformation is not perfect for output and no image.

So is it not possible to losslessly rotate a .jpg? I could, myself, go into paint and reconstruct the original image by simply moving the edge lines into their correct place. Is there a method to do this within libjpeg?

A lossless rotation works with whole DCT blocks contained within the JPEG file. These blocks are always 8x8 or 16x16 pixels (depending on the compression downsampling settings). The file contains a width and height so the extra pixels can be thrown away when the image is decoded, but there's no way to move the clipping from the right/bottom edge to the left/top edge. The software is doing the best it can with an impossible problem.

As you've discovered the way around this problem is to make the width and height evenly divisible by 16. You'll find that images from cameras for example will have this property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM