Solved

Code for lossless JPEG rotation wanted please

Posted on 2004-08-03
15
1,191 Views
Last Modified: 2013-11-19
I would like Delphi code for lossless JPEG rotation so that I can open a JPEG file, rotate the image either  plus or minus 90 degrees and save the file again without losing image quality or increasing file size. I can handle restoring any EXIF data, it is the code for lossless JPEG rotation that is important to me.

Thanks in advance - Dan
0
Comment
Question by:seabear
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 
LVL 4

Expert Comment

by:Colin_Dawson
ID: 11708601
Rotating the image 90 defrees without changing the file size.  That highly unlikley to be able to do without changing the image quality.

JPEG's are compressed images so altering the file in any way will alter the results from the compression, and will change the filesize.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 11709470
What I'd do is convert to PNG, rotate and then convert back to JPG, no loss there...
0
 
LVL 34

Expert Comment

by:Slick812
ID: 11723128
???
seabear, can you give some information about why you expect a "lossless JPEG rotation" from a compression algoritem (the JFIF compression of jpg) that is NOT lossless, with any jpg compression you will have loss of image imformation, , , ,
and beside that, you can not manipulate (change or rotate) jpg file data, because it is compressed , any change will have to be made with a  PER PIXEL format which is a system bitmap,  what are you needing as a lossless factor
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:seabear
ID: 11724158
Hi Slick812,
Lossless JPEG rotation is rotation of the image stored in a JPEG without loosing any further data. I do not know how it is done but it is a common claim for many programs providing image manipulation. As far as the size of the resulting rotated image I assume it is aproximately the size of the original JPEG, not exactly the same size. I agree that it does not make sense to expect the rotation to occur without decompressing the JPEG and then recompressing it after rotation but there is still the claim that there is a lossless rotation process out there somewhere. By the way is it really the case that JPEG compression is always lossy? I thought that once the original compression had been done (and you have lost data) that there was no significant further degradation if later decompression and recompression was done using the lowest level of compression.
As t why I want it I am writing a photo browser and some of my digital camera shots are taken with the camera side on

regards - Dan
0
 
LVL 34

Expert Comment

by:Slick812
ID: 11727597
???
OK, some of what you said I can see (understand somewhat), but will a jpg, there is Bitmap image information that is already lost, I juess you want something to rotate it with out furthur loss of  bimap information, but I do not see how to call this lossless, as lossless to me means "NO LOSS", , ,.
whatever, don't need to do word definition here. . . . .

I can show you some code that will load a JPG from file, and then get the bitmap from it , then rotate that bitmap, 90 degrees, and then place that rotated bitmap into a jpg and save that jpg to file. . .
However, I have tried to get a way to determine a jpg file's jfif compression, by reading the jpg file header, but there are many factors in those headers, and the compression ratio was something I could not get from the jpg file, but I can tell wheater a jpg file is progressive compressed or not.
So I can not supply you with any way to know the original commpression ratio and then save it to that compression And the whole lossless thing is beyond me
0
 

Author Comment

by:seabear
ID: 11730270
Hi Slick812
    I am also struggling with this. However I have done a bit more searching and the word  lossless appears to mean no further loss after the first compression. If you try to get a rotation by decompressing and recompressing you get a further loss. However is seems that the JPEG format uses blocks which are related to matching 16 x 16 pixel blocks in the original image. It is apparently possible to work on the JPEG binary without decompressing it and process each of those 16x16 blocks to get a 90, 180, 270 rotation without losing any further info. This quote is typical of the tech stuff but does not get me closer to the code.

"A jpeg file is stored as blocks of data. Each block often contains 8 or 16 pixels. The blocks are encoded such that a simple matrix transform will change the location of the pixels without needing to uncompress the block. Because of this, a jpeg whose size if a multiple of the iMCU size (the 8 or 16 number I mentioned above) can be rotated losslessly." And apparently all digital camera image sizes are a multiple of the iMCU size.

regards - Dan
0
 
LVL 34

Expert Comment

by:Slick812
ID: 11732744
OK, but I doubt you will ever find any information at a Delphi Help place (like this) about the very very advanced jfif  methods of disasembling the pixel defintion data and then reassemling it to have it be rotated. . . I do know that the jfif compression will take a different size block of pixels (the higher the compression the larger the block of pixels) and then aveage the pixel data for that to get a single value and just srore that single value for a  2x2 or 3x3 or 4x4 or 5x5 or 6x6 block of pixels, but it is not that simple, there are also some adjacent block luminosity values in the formular

you might do a web search for that type of information, but as I remember, I did not find much about the decoding of the header for a jfif image file, and you may find very little for the actual pixel data, that you seem to think is your key to this,

you may not get anymore info here a EE
0
 

Author Comment

by:seabear
ID: 11738769
Hi Slick812
    Yes it seems like this one is outside the area where Ex-Ex does well but I have just had a person I know say that they think they have a piece of code that does what I want. If I get it I will post it here as an answer for future use - Dan
0
 
LVL 34

Expert Comment

by:Slick812
ID: 11740643
I for one, would appreciate any code for the jfif file bytes usage. . . and if you can ask them how to get the jpg compression ratio from the jfif file header segments bytes, , ,  I'll give 500 points for information about that, in pascal code , in C++ code or VB code
0
 

Author Comment

by:seabear
ID: 11756466
Hi Slick,
 if you want the actual ratio it is just the file size divided by (image.width x image.height) which is available from TJpegImage, but if you want the compression quality number that was used to create the JPEG from the original image it gets harder. The stuff below is what I found.

regards - Dan

Finding the compression quality used with a JPEG file

URL - http://support.microsoft.com/default.aspx?scid=kb;en-us;324788

Retrieve the Quality Factor from a JPEG File using VB.net
The quality factor is not stored directly in the JPEG file, so you cannot read the quality factor from the file. However, you can read the quantization tables from the JPEG file by using the PropertyItems property on the Image class. But even with the quantization tables, you cannot always determine a quality factor.

You might be able to determine the quality factor by comparing the quantization tables against the "standard" IJG-generated tables. However, because some applications may use custom tables, you will not always find a match. For more information about the quantization tables, see the "References" section.

0
 

Author Comment

by:seabear
ID: 11835961
Okay here is the answer.

Gabriel Corneanu has posted an enhanced JPEG unit on Borland's Code Central

I have downloaded it and tried it out and it works without hassles

The download URl is

http://codecentral.borland.com/codecentral/ccweb.exe/listing?id=19723

Thanks to everyone who posted ideas - Dan
0
 
LVL 34

Expert Comment

by:Slick812
ID: 11857527
Thanks for your link ! !
and the Info at the http://support.microsoft.com/default.aspx?scid=kb;en-us;324788 did not help me at all, since it uses the built in code of the ImageCodecInfo, which I have no reference to. . .

anyway, Here at EE, it is good to finish a question . . . if you did not get any help form this question, then you should go to the community support at -

http://www.experts-exchange.com/Community_Support/

and ask for a question "Close" , , "Delete" or othe way to finish this question
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 13404397
PAQed with points refunded (250)

modulo
Community Support Moderator
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Multiple image collision 13 105
update joined tables 2 71
Delphi TListView and TEdit (Select item and input into TEdit) 10 77
Broadcast a message using ICS 2 49
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Keep your audience engaged and get the most out of your next presentation with these quick Prezi tips.
Add visuals to your Prezi to keep your audience engaged. Learn how to embed images, edit them, and more in this video micro tutorial. Select "Insert" from the top menu in your Prezi editor: Select "Image": A toolbar will pop in from the right …
Learn how to download your full Prezi presentation for offline presenting. Prezi doesn’t have to be viewed and shared in a web browser, even with a free account you can download your full presentation to share with others. Be sure to download any vi…
Suggested Courses

737 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