diff options
| author | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2011-06-04 20:10:34 +0200 |
|---|---|---|
| committer | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2011-06-04 20:10:34 +0200 |
| commit | 67735dc85fc4a25dcc3e0b30420b3ad5a6ac1c9f (patch) | |
| tree | 91407fb189dd2a97283c844297d7b6dab0c19152 /src/jpeg-utils.cpp | |
| parent | ee24863518448604b5600ab1e426d818975a76bb (diff) | |
| download | cataract-67735dc85fc4a25dcc3e0b30420b3ad5a6ac1c9f.tar.xz | |
Autorotate images according to EXIF Orientation info
This comes with a cost of decoding full image when only getting image size.
Diffstat (limited to 'src/jpeg-utils.cpp')
| -rw-r--r-- | src/jpeg-utils.cpp | 80 |
1 files changed, 77 insertions, 3 deletions
diff --git a/src/jpeg-utils.cpp b/src/jpeg-utils.cpp index 55e3442..663e97a 100644 --- a/src/jpeg-utils.cpp +++ b/src/jpeg-utils.cpp @@ -225,6 +225,66 @@ free_exif_data (TExifData *data) } +static void +autorotate_image (MagickWand *magick_wand) +{ + MagickBooleanType b; + PixelWand *pixel_wand; + ExceptionType severity; + gchar *description; + + pixel_wand = NewPixelWand (); + b = PixelSetColor (pixel_wand, "#000000"); + if (b == MagickFalse) { + description = MagickGetException (magick_wand, &severity); + log_error ("autorotate_image: Error creating pixel wand: %s %s %ld %s\n", GetMagickModule(), description); + MagickRelinquishMemory (description); + } + + b = MagickTrue; + switch (MagickGetImageOrientation (magick_wand)) + { + case TopRightOrientation: + b = MagickFlopImage (magick_wand); + break; + case BottomRightOrientation: + b = MagickRotateImage (magick_wand, pixel_wand, 180.0); + break; + case BottomLeftOrientation: + b = MagickFlipImage (magick_wand); + break; + case LeftTopOrientation: + b = MagickTransposeImage (magick_wand); + break; + case RightTopOrientation: + b = MagickRotateImage (magick_wand, pixel_wand, 90.0); + break; + case RightBottomOrientation: + b = MagickTransverseImage (magick_wand); + break; + case LeftBottomOrientation: + b = MagickRotateImage (magick_wand, pixel_wand, 270.0); + break; + default: + break; + } + + if (b == MagickFalse) { + description = MagickGetException (magick_wand, &severity); + log_error ("autorotate_image: Error rotating image: %s %s %ld %s\n", GetMagickModule(), description); + MagickRelinquishMemory (description); + } + + b = MagickSetImageOrientation (magick_wand, TopLeftOrientation); + if (b == MagickFalse) { + description = MagickGetException (magick_wand, &severity); + log_error ("autorotate_image: Error saving orientation: %s %s %ld %s\n", GetMagickModule(), description); + MagickRelinquishMemory (description); + } + + DestroyPixelWand (pixel_wand); +} + /* * resize_image: resize image pointed by src and save result to dst */ @@ -233,7 +293,8 @@ resize_image (const gchar *src, const gchar *dst, unsigned long size_x, unsigned long size_y, int quality, gboolean thumbnail, - ThumbnailSquareType squared_thumbnail_type) + ThumbnailSquareType squared_thumbnail_type, + gboolean autorotate) { MagickWand *magick_wand; ExceptionType severity; @@ -251,6 +312,9 @@ resize_image (const gchar *src, const gchar *dst, return FALSE; } + if (autorotate) + autorotate_image (magick_wand); + /* Don't resize if smaller than desired size */ if (MagickGetImageWidth (magick_wand) > size_x || MagickGetImageHeight (magick_wand) > size_y) @@ -295,9 +359,11 @@ resize_image (const gchar *src, const gchar *dst, */ void get_image_sizes (const gchar *img, - unsigned long *width, unsigned long *height) + unsigned long *width, unsigned long *height, + gboolean autorotate) { MagickWand *magick_wand; + MagickBooleanType b; ExceptionType severity; gchar *description; @@ -306,12 +372,20 @@ get_image_sizes (const gchar *img, /* Read an image. */ magick_wand = NewMagickWand(); - if (MagickPingImage (magick_wand, img) == MagickFalse) { + if (autorotate) + b = MagickReadImage (magick_wand, img); + else + b = MagickPingImage (magick_wand, img); + if (b == MagickFalse) { description = MagickGetException (magick_wand, &severity); log_error ("Error reading image info: %s %s %ld %s\n", GetMagickModule(), description); MagickRelinquishMemory(description); return; } + + if (autorotate) + autorotate_image (magick_wand); + *width = MagickGetImageWidth (magick_wand); *height = MagickGetImageHeight (magick_wand); |
