Solved

Simple JPG rotation?

Posted on 2001-09-08
9
441 Views
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!

Thanks!

Jon
0
Comment
Question by:jsexton
9 Comments
 
LVL 22

Expert Comment

by:mnasman
ID: 6468061
Hello

Look at this page, it will help you
http://homepages.borland.com/efg2lab/ImageProcessing/RotateScanline.htm

Best regards
Mohammed Nasman
0
 
LVL 6

Expert Comment

by:edey
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?

GL
Mike
0
 
LVL 10

Expert Comment

by:Jacco
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
0
 
LVL 1

Expert Comment

by:Dennis9
ID: 6468299
Hi.
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);
var
  x, y : cardinal;
  BitMap2 : TBitMap;
begin
  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];
  end;
  bmp.Assign (BitMap2);
end;

//Sample call
Rotate90(Image1.Picture.Bitmap);



Hope it helped.
Dennis
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 5

Expert Comment

by:scrapdog
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.  
0
 
LVL 2

Accepted Solution

by:
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.

as
0
 
LVL 6

Expert Comment

by:edey
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.

GL
Mike
0
 
LVL 2

Expert Comment

by:alexstewart@beta
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.


0
 
LVL 17

Expert Comment

by:geobul
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thanks,

geobul
EE Cleanup Volunteer
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
In this Micro Tutorial viewers will learn how to remove an unwanted object using Photoshop’s feature known as content-aware fill.
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now