Solved

Code for lossless JPEG rotation wanted please

Posted on 2004-08-03
15
1,202 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Many programs have tried to outwit PowerPoint in terms of technology and skill. These programs, however, still lack several characteristics that PowerPoint has possessed from the start. Here's why PowerPoint replacements won't entirely work for desi…
In this tutorial viewers will learn how to create blended and gradiated shapes in Illustrator using the blend tool Draw two shapes, one of them in a different color: Select both and create a blend by going to Object > Blend > Make: Blends can also b…
Learn the basics of inputting and editing your text components in Prezi. We will cover how to set styles, position, and group your text components. In your Prezi editor, click anywhere on the canvas to add text: A flashing cursor informs you that yo…
Suggested Courses
Course of the Month10 days, 3 hours left to enroll

623 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