#71 JPEGTransform doesn't transform thumbnail

open
nobody
None
5
2012-10-31
2006-08-20
MrGriffon
No

When a JPEG has an EXIF thumbnail (which seems to be
the case of virtually every picture out of a digital
camera), using the FreeImage_JPEGTransform function
will only transform the picture and not the associated
thumbnail, making it incoherent.

As the embedded EXIF thumbnail is widely used by
imaging programs, this can be quite a problem.

Discussion

  • I note that this hasn't been fixed in about 4 years... so here's the code I am using to work around this problem:

    void RotateThumbnail( fipImage &thumb, fipImage &parent )
    {
    // Source: http://sylvana.net/jpegcrop/exif_orientation.html
    // "here is what the letter F would look like if it were tagged correctly and displayed by a program that ignores the orientation tag"
    // 1 2 3 4 5 6 7 8
    //
    // 888888 888888 88 88 8888888888 88 88 8888888888
    // 88 88 88 88 88 88 88 88 88 88 88 88
    // 8888 8888 8888 8888 88 8888888888 8888888888 88
    // 88 88 88 88
    // 88 88 888888 888888
    // These are the transforms which would make it look correct (like orientation 1):
    // [ident] [flip H] [rot 180] [rot 180] [rot 270] [rot 270] [rot 90] [rot 90]
    // [flip H ] [flip H ] [flip H]

    fipTag tag;
    if ( parent.getMetadata( FIMD_EXIF_MAIN, "Orientation", tag ) )
    {
        struct orient { double rotate; bool hFlip; };
        static struct orient LUT[8] = { {   0, false }, // Rotations are CCW
                                        {   0,  true },
                                        { 180, false },
                                        {   0, false },
                                        { 270,  true },
                                        { 270, false },
                                        {  90,  true },
                                        {  90, false } };
        FREE_IMAGE_MDTYPE type = tag.getType();
        Assert( type == FIDT_SHORT );
        unsigned short orientation = *(unsigned short *)tag.getValue();
        orient &transform = LUT[orientation - 1];
        if ( transform.rotate ) thumb.rotate( transform.rotate );
        if ( transform.hFlip  ) thumb.flipHorizontal();
    }
    

    }

     
  • Ugh, sorry about the formatting... the SourceForge comment field truncates and strips whitespace. The code will work, though, and you can see the original formatting of the rotation comments on the linked page: http://sylvana.net/jpegcrop/exif_orientation.html

     
  • I realize that the bug is not fully described. Here are additional details:
    - if a JPG is loaded using the flag the JPEG_EXIFROTATE flag, then any JPEG with an orientation value other than 1 (i.e. identity) will load without a thumbnail (it's either not loaded or is discarded when the main image is rotated)
    - if FIF_LOAD_NOPIXELS is used to load the JPG, then the thumbnail is loaded, but the JPEG_EXIFROTATE flag is ignored, so the thumbnail is not oriented correctly