I have a freshly compiled version of libjpeg 9 and tried to run jpegtran.exe on the command line with arguments:
.\jpegtran.exe -rotate 180 -outfile test_output1.jpg testimg.jpg
testimg.jpg:
test_output1.jpg: 
As you can see, it rotates the image, but it pinches it and it does not fold correctly. The usage.txt file that comes with the package is not completely updated because I had to use the -outfile switch instead of what it says:
jpegtran uses 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] outputfile outputfile
where both the input and output files are jpeg files.
To specify the encoded JPEG representation used in the output file, jpegtran accepts a subset of the switches recognized by cjpeg:
- -optimize Optimize the entropy encoding options.
- -progressive Create a progressive JPEG file.
- -arithmetic Using arithmetic coding.
- -restart N Run a JPEG restart marker every N lines of MCU or every N blocks of MCU if "B" is connected to the number.
- -scans file Use the scan script specified in the specified text file.
See the previous cjpeg discussion for more details on these switches. If you do not specify any of these switches, you will get a simple JPEG source file. Quality setting, etc. defined by the input file.
An image can be converted without loss by providing one of these switches:
- -flip horizontal Mirror image horizontally (left-right).
- -flip vertical mirror image vertically (upper bottom).
- -rotate 90 Rotate the image 90 degrees clockwise.
- -rotate 180 Rotate the image 180 degrees.
- -rotate 270 Rotate the image 270 degrees clockwise (or 90 ccw).
- - transport the transpose image (via the UL-LR axis).
- -transverse Transverse translation (via the UR-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 without any clipping problems. It flips the cropped parts, but just does not align them directly with the rest of the image.
test_output2.jpg: 
I get the same result by running jpegtran.exe -rotate 90 twice.
In addition, I tried it on a larger .jpg file, which led to the same problem, but the file size was less than 18 Kbytes for output. I assume the problem is related to this.
Edit - I also found this ad unit, which seems to describe the problem:
jpegtran default behavior when converting an odd-sized image designed to maintain accurate reversibility and mathematical consistency of many transformations. As indicated, transposition can flip the entire image area. Horizontal mirroring leaves any partial iMCU column on the right edge untouched, but it can flip all rows of an image. Similarly, vertical mirroring leaves any partial iMCU row at the bottom edge intact, but you can flip all columns. other transformations can be created as sequences of transposing and flipping operations; for consistency, their actions on the extreme pixels are determined to be the same as the final result of the corresponding transposed and flip sequence.
The -trim switch works if you can name it, and truncates unorganized data, but the image is smaller and lost data.
test_output5.jpg: 
Adding an -perfect switch that supposedly stops the above leads to this: transformation is not perfect for output and without an image.
So it is impossible to rotate .jpg without loss? I could go into the paint myself and restore the original image by simply moving the boundary lines to their correct place. Is there a way to do this in libjpeg?