jpegtran.exe不能正确旋转图像(jpegtran.exe not correctly ro

2019-10-17 15:52发布

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:

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?

Answer 1:

无损旋转工作与包含在JPEG文件中的整个DCT块。 这些块是8×8始终或16×16像素(取决于压缩下采样设置)。 该文件包含的宽度和高度,使多余的像素可以扔掉当图像被解码,但没有办法从右边/底部边缘左/顶边移动剪裁。 该软件是做得最好的它可以与一个不可能的问题。

正如你已经发现解决这个问题的方法是使宽度和高度由16整除你会发现,从摄像机例如图像将具有这种性质。



文章来源: jpegtran.exe not correctly rotating image
标签: c++ jpeg libjpeg