jsexton
asked on
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!
Thanks!
Jon
Thanks!
Jon
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
GL
Mike
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
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
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:TBitma p);
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.Bi tmap);
Hope it helped.
Dennis
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:TBitma
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.Bi
Hope it helped.
Dennis
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
GL
Mike
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.
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.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Thanks,
geobul
EE Cleanup Volunteer
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
Look at this page, it will help you
http://homepages.borland.com/efg2lab/ImageProcessing/RotateScanline.htm
Best regards
Mohammed Nasman