# lossless rotate of 4mp image- impossible?

Discussion in 'Digital Photography' started by B Young, Aug 26, 2003.

1. ### B YoungGuest

The popular jpegtran library from the IJG is in use by many major
commerical applications. The general info that circulates says that a
JPEG can be losslessly rotated if both dimensions are divisible by 16.
No problem until the 4 megapixel cams came out. Mine produces
2274x1704. 1704 is NOT divisible by 16.

What does this imply? Lossless rotation is lost for 4mp images? Or,
are these apps shaving a few pixels (8, to be exact) off the width to
make it happen?

Thanks,
Brian

B Young, Aug 26, 2003

2. ### Guido VollbedingGuest

Guido Vollbeding, Aug 26, 2003

3. ### B YoungGuest

Excellent information! Thanks. I had originally coded around the
jpegtran library with the same "extra safe" restriction as Microsoft-
16x16. Now I think the safest thing for a person to do would be to
grab the subsampling of the image and make a determination if a
lossless rotation is possible or not instead of trying to use
primative calculations.

One issue, for example: If you rotate a 2274x1704 90 degrees, your
dimensions are obviously flipped. Simple logic that just checks if
the height is divisible by 8 will fail, prohibiting a user from
rotating the image again. In reality the image *can* be rotated
again, without loss.

Also, there is the issue of what jpegtran might do if you passed it an
image that wasn't in a 2x1 configuration. I haven't tested enough to

Thanks again
Brian

Guido Vollbeding <> wrote in message news:<>...
> The condition for complete lossless rotation is that the dimension
> must be divisible by the *MCU* size (MCU = Minimal Coded Unit)!
> The MCU size is determined by the subsampling parameters of the
> JPEG image. Almost all digital cameras today use a subsampling
> configuration of 2x1 (horizontal only subsampling), giving an
> MCU size of 2x1 DCT blocks, or 16x8 pixels.

B Young, Aug 26, 2003
4. ### Guido VollbedingGuest

B Young wrote:
>
> Excellent information! Thanks.

Well, I should know it since I wrote the original lossless rotation code .

> I had originally coded around the
> jpegtran library with the same "extra safe" restriction as Microsoft-
> 16x16. Now I think the safest thing for a person to do would be to
> grab the subsampling of the image and make a determination if a
> lossless rotation is possible or not instead of trying to use
> primative calculations.

I have a patch for jpegtran in the pipeline which provides exactly that.
It adds a "-perfect" option to the program which uses following new

/* jtransform_perfect_transform
*
* Determine whether lossless transformation is perfectly
* possible for a specified image and transformation.
*
* Inputs:
* image_width, image_height: source image dimensions.
* MCU_width, MCU_height: pixel dimensions of MCU.
* transform: transformation identifier.
* Parameter sources from initialized jpeg_struct
* image_width = cinfo.image_width
* image_height = cinfo.image_height
* MCU_width = cinfo.max_h_samp_factor * DCTSIZE
* MCU_height = cinfo.max_v_samp_factor * DCTSIZE
* Result:
* TRUE = perfect transformation possible
* FALSE = perfect transformation not possible
* (may use custom action then)
*/

GLOBAL(boolean)
jtransform_perfect_transform(JDIMENSION image_width, JDIMENSION image_height,
int MCU_width, int MCU_height,
JXFORM_CODE transform)
{
boolean result = TRUE; /* initialize TRUE */

switch (transform) {
case JXFORM_FLIP_H:
case JXFORM_ROT_270:
if (image_width % (JDIMENSION) MCU_width)
result = FALSE;
break;
case JXFORM_FLIP_V:
case JXFORM_ROT_90:
if (image_height % (JDIMENSION) MCU_height)
result = FALSE;
break;
case JXFORM_TRANSVERSE:
case JXFORM_ROT_180:
if (image_width % (JDIMENSION) MCU_width)
result = FALSE;
if (image_height % (JDIMENSION) MCU_height)
result = FALSE;
break;
}

return result;
}

> One issue, for example: If you rotate a 2274x1704 90 degrees, your
> dimensions are obviously flipped. Simple logic that just checks if
> the height is divisible by 8 will fail, prohibiting a user from
> rotating the image again. In reality the image *can* be rotated
> again, without loss.

The sampling setting then also flips, so all is well.
The above code handles all cases properly.

> Also, there is the issue of what jpegtran might do if you passed it an
> image that wasn't in a 2x1 configuration. I haven't tested enough to
> know the answer to that.

jpegtran works with *any* subsampling configuration. This feature was
the main challenge while writing the core transformation functions.

Regards
Guido

Guido Vollbeding, Aug 26, 2003
5. ### Guido VollbedingGuest

Mick Ruthven wrote:
>
> I'm confused by the following two statements. 2274 is not divisible by 16,
> so how can a 2274x1704 be losslessly rotated? I have a feeling I'm missing
> something.

Oops, a typo in the original post which propagated.
The 4MP image size is 2272x1704.

Regards
Guido

Guido Vollbeding, Aug 26, 2003
6. ### Mick RuthvenGuest

Thanks. And I have a 4mp G2 and should have remembered 2272...

"Guido Vollbeding" <> wrote in message
news:...
> Mick Ruthven wrote:
> >
> > I'm confused by the following two statements. 2274 is not divisible by

16,
> > so how can a 2274x1704 be losslessly rotated? I have a feeling I'm

missing
> > something.

>
> Oops, a typo in the original post which propagated.
> The 4MP image size is 2272x1704.
>
> Regards
> Guido

Mick Ruthven, Aug 26, 2003
7. ### Robert CarterGuest

You have got me. Why not just save the original as a tif or psd and then rotate it or am I missing something here?

Guido Vollbeding <> wrote in message news:...
"Paul D. Sullivan" wrote:
>
> Doesn't Irfanview do that?

There are some more:

http://jpegclub.org/losslessapps.html

Regards
Guido

Robert Carter, Aug 27, 2003
8. ### gsumGuest

I'm also confused. Rotation by 90 degrees or a
multiple of 90 simply results in a rearrangement
of data in the jpg (or tif etc.) file. There is no division
by 16 as the data itself is not altered. That is why
90 degree rotation is lossless and is much faster than
arbitrary rotation.

Graham

"Robert Carter" <> wrote in message news:3f4c1a6b@news1...
> You have got me. Why not just save the original as a tif or psd and then

rotate it or am I missing something here?
>
> Guido Vollbeding <> wrote in message

news:...
> "Paul D. Sullivan" wrote:
> >
> > Doesn't Irfanview do that?

>
> There are some more:
>
> http://jpegclub.org/losslessapps.html
>
> Regards
> Guido
>
>

gsum, Aug 27, 2003
9. ### Ed RufGuest

On Wed, 27 Aug 2003 12:38:28 +1000, in rec.photo.digital "Robert
Carter" <> wrote:

>You have got me. Why not just save the original as a tif or psd and then rotate it or am I missing something here?

The size of the file is much larger this way. Lossless rotation
provides a means of rotating the image and keeping it in jpg format
without the loss of any information contained in the original jpg in
the first place.
________________________________________________________
Ed Ruf Lifetime AMA# 344007 ()
http://members.cox.net/egruf
See images taken with my CP-990 and 5700 at
http://members.cox.net/egruf-digicam

Ed Ruf, Aug 27, 2003

I pondered over the same problem until a friend (mathematician, not
photographer) offered a solution:
first, resize your jpg picture to as large a format as your computer will
allow. save as bmp or tiff, rotate and do all your digital processing on
this huge file; what with modern pentium4s and fast ram, this is not nearly
as daunting as it sounds.
only when you're done and have the final picture, resize and reconvert to
I followed this advice although it made me work with a 220Mb file, and was
amazed at the results.
question remains: can you afford the time and computing power involved?

--

11. ### Bob & AnniGuest

We've always done a similar thing, except for the up-sizing.
First thing we do when we download the images from the camera is to rename them using a re-naming utility.
Then we burn a copy of these files to CD without opening them (I'm only talking jpegs here). This represents the most efficient
storage and provides a route back if disaster happens.
We then, if required, open them one by one, rotate, crop adjust etc and save as uncompressed tiffs, these then become our working
copies, which can be opened and modified as often as we like without deterioration, as long as they remain as tiffs.
Then, for those we want to use on our websites, we resize down to screen size and save copies as jpegs
hope that helps any newcomers, Bob
http://www.feel-the-noise.com
http://www.surepix.co.uk/
--

"Yehuda Paradise" <> wrote in message news:biiect\$8cp\$...
> I pondered over the same problem until a friend (mathematician, not
> photographer) offered a solution:
> first, resize your jpg picture to as large a format as your computer will
> allow. save as bmp or tiff, rotate and do all your digital processing on
> this huge file; what with modern pentium4s and fast ram, this is not nearly
> as daunting as it sounds.
> only when you're done and have the final picture, resize and reconvert to