Simple JPG rotation?

I need to rotate a JPEG image 90, 180, or 270 degrees WITHOUT CHANGING THE IMAGE QUALITY!  I have a working piece of code that converts a JPG to BMP, rotates it, then changes it back to JPG; however, this results in quality loss (or at least a bigger file size than necessary).  I want a way to rotate the JPG without converting it to a BMP first!


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mohammed NasmanSoftware DeveloperCommented:

Look at this page, it will help you

Best regards
Mohammed Nasman
I'm pretty sure that you'd have to (re/de)compress it somewhere along the way.  Perhaps you might get the results you want if you ensure that you're re-encoding with the very best quality settings?

Hi Jon,

Mike is right. You need to decompress and recompress. JPEG is a very complex compression method so the task will not be simple. If you have a pas library for jpeg (de)compression that you understand, the task will be quit easy:

You do not have to go to BMP format. You can than reuse the quantization table and huffmann tables in the file. You only have to rotate every decompressed MCU (minimum coding unit) and reorder them. After that you compress them again reusing the specified tables.

This is not a simple task though. The only pascal library I know of is jpegpas and the code of it is not very understandable.

Regards Jacco
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

I don't think there is a way, where not the QUALITY will change. I will suggest u just call this code, to rotate it 90 degrees with.

procedure TForm1.Rotate90(bmp:TBitmap);
  x, y : cardinal;
  BitMap2 : TBitMap;
  BitMap2 := TBitMap.Create;
  with bmp do begin
    BitMap2.Width := Height;
    BitMap2.Height := Width;
    for y := 0 to Height - 1 do
      for x := 0 to Width - 1 do
        BitMap2.Canvas.Pixels[y, Width - x - 1] := Canvas.Pixels[x, y];
  bmp.Assign (BitMap2);

//Sample call

Hope it helped.
You will definitely have to partially decompress it.  JPEGs are both Huffman encoded and run-length encoded, so it is impossible to rotate because each pixel is represented by an unknown, variable number of bits.

Before compression, JPEGs are transformed from bitmaps using the Discrete Cosine Transform, and then parts of the frequency spectrum are culled by multiplying by a corresponding value in a table (quantization).  This results in a bunch of zeroes, which can be easily crunched using RLE, which also leads to the generally crappy quality of JPEGs.

You can probably rotate the image when it is in a transformed state;  you probably shouldn't have to inverse-transform it since each pixel is now represented by the same number of bits.

Then again, rotating it might corrupt the inverse transformation.  If this is the case (I am sure it is), it will be impossible to reproduce exactly the same image as if it were rotated.

After rotating it, you can probably recompress it using RLE and Huffman.

I only know the general procedure for decoding and encoding JPEGs, I don't know too many specifics.  However, I know it is impossible to rotate a JPEG without at least decompressing it.  
You do not have to invert the transform. You do have to undo the (lossless) huffman coding. The transform dimensions are orthogonal, you just move the coeficients in each 8x8 block to rotate to flip diagonaly, flips make this a rot90. The process does not degrade the image quality.

I use the intel jpeg library, I think it does rot90 for you, also does resampling and is fast. Huffman coding is not complex, so it wouldnt be that hard to do from scratch, there are complications like zig-zag ordering, but these are well documented. About as hard as a Gif encoder, if you have ever tried that.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I think the point was that the effort required to do this, without signal loss, is rather exorbitant.  If one was contimplating this sort of project they'd be _far_ better off to choose a more approriate, loss less, file format.

The intel jpeg library will provide the coeficients to you, you can then feed them back again.

Also there is some free code called jpegtran. (from the ijg)

But yes, its alot more trouble than using uncompressed Tiff and one of them big cheap drives.

from the jpeg tran man page..

jpegtran  performs  various useful transformations of JPEG
       files.  It can translate the coded representation from one
       variant of JPEG to another, for example from baseline JPEG
       to progressive JPEG or vice versa.  It  can  also  perform
       some rearrangements of the image data, for example turning
       an image from landscape to portrait format by rotation.

       jpegtran works by rearranging  the  compressed  data  (DCT
       coefficients),  without  ever  fully  decoding  the image.
       Therefore, its transformations are lossless: there  is  no
       image  degradation  at all, which would not be true if you
       used djpeg followed by cjpeg to accomplish the  same  con-
       version.   But  by the same token, jpegtran cannot perform
       lossy operations such as changing the image quality.

       jpegtran reads the named JPEG/JFIF file, or  the  standard
       input  if  no file is named, and produces a JPEG/JFIF file
       on the standard output.

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

accept alexstewart@beta's comment as answer

Please leave any comments here within the next seven days.



EE Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Graphics Software

From novice to tech pro — start learning today.