Simple JPG rotation?

Posted on 2001-09-08
Last Modified: 2013-11-19
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!


Question by:jsexton
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6468061

Look at this page, it will help you

Best regards
Mohammed Nasman

Expert Comment

ID: 6468125
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?

LVL 10

Expert Comment

ID: 6468218
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Expert Comment

ID: 6468299
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.

Expert Comment

ID: 6469439
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.  

Accepted Solution

alexstewart@beta earned 100 total points
ID: 6481289
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.


Expert Comment

ID: 6481300
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.


Expert Comment

ID: 6481339
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.

LVL 17

Expert Comment

ID: 9296165
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
In addition to being a great web-based presentation tool, Prezi also makes it easy to save your presentation as a PDF to share with others as well. Learn how in this tutorial. Select the share icon from the top menu in your Prezi editor: Select "D…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question