nsxivbfab4dc
Added EXIF auto-orientation
+56 −1
2 files changed
| @@ -16,11 +16,12 @@ | |||
| 16 | 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
| 17 | 17 | */ | |
| 18 | 18 | ||
| 19 | + | #include <string.h> | |
| 19 | 20 | #include <unistd.h> | |
| 21 | + | #include <libexif/exif-data.h> | |
| 20 | 22 | ||
| 21 | 23 | #ifdef GIF_SUPPORT | |
| 22 | 24 | #include <stdlib.h> | |
| 23 | − | #include <string.h> | |
| 24 | 25 | #include <sys/types.h> | |
| 25 | 26 | #include <gif_lib.h> | |
| 26 | 27 | #endif | |
| @@ -37,6 +38,49 @@ enum { MIN_GIF_DELAY = 50 }; | |||
| 37 | 38 | float zoom_min; | |
| 38 | 39 | float zoom_max; | |
| 39 | 40 | ||
| 41 | + | void exif_auto_orientate(const fileinfo_t *file) { | |
| 42 | + | ExifData *ed; | |
| 43 | + | ExifEntry *entry; | |
| 44 | + | int byte_order, orientation; | |
| 45 | + | ||
| 46 | + | if (!(ed = exif_data_new_from_file(file->path))) | |
| 47 | + | return; | |
| 48 | + | entry = exif_content_get_entry(ed->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION); | |
| 49 | + | if (entry) { | |
| 50 | + | byte_order = exif_data_get_byte_order(ed); | |
| 51 | + | orientation = exif_get_short(entry->data, byte_order); | |
| 52 | + | } | |
| 53 | + | exif_data_unref(ed); | |
| 54 | + | if (!entry) | |
| 55 | + | return; | |
| 56 | + | ||
| 57 | + | switch (orientation) { | |
| 58 | + | case 5: | |
| 59 | + | imlib_image_orientate(1); | |
| 60 | + | case 2: | |
| 61 | + | imlib_image_flip_vertical(); | |
| 62 | + | break; | |
| 63 | + | ||
| 64 | + | case 3: | |
| 65 | + | imlib_image_orientate(2); | |
| 66 | + | break; | |
| 67 | + | ||
| 68 | + | case 7: | |
| 69 | + | imlib_image_orientate(1); | |
| 70 | + | case 4: | |
| 71 | + | imlib_image_flip_horizontal(); | |
| 72 | + | break; | |
| 73 | + | ||
| 74 | + | case 6: | |
| 75 | + | imlib_image_orientate(1); | |
| 76 | + | break; | |
| 77 | + | ||
| 78 | + | case 8: | |
| 79 | + | imlib_image_orientate(270); | |
| 80 | + | break; | |
| 81 | + | } | |
| 82 | + | } | |
| 83 | + | ||
| 40 | 84 | void img_init(img_t *img, win_t *win) { | |
| 41 | 85 | zoom_min = zoom_levels[0] / 100.0; | |
| 42 | 86 | zoom_max = zoom_levels[ARRLEN(zoom_levels) - 1] / 100.0; | |
| @@ -241,6 +285,8 @@ int img_load(img_t *img, const fileinfo_t *file) { | |||
| 241 | 285 | imlib_context_set_anti_alias(img->aa); | |
| 242 | 286 | ||
| 243 | 287 | fmt = imlib_image_format(); | |
| 288 | + | if (!strcmp(fmt, "jpeg")) | |
| 289 | + | exif_auto_orientate(file); | |
| 244 | 290 | #ifdef GIF_SUPPORT | |
| 245 | 291 | if (!strcmp(fmt, "gif")) | |
| 246 | 292 | img_load_gif(img, file); | |
| @@ -34,6 +34,8 @@ | |||
| 34 | 34 | #define st_atim st_atimespec | |
| 35 | 35 | #endif | |
| 36 | 36 | ||
| 37 | + | void exif_auto_orientate(const fileinfo_t*); | |
| 38 | + | ||
| 37 | 39 | const int thumb_dim = THUMB_SIZE + 10; | |
| 38 | 40 | char *cache_dir = NULL; | |
| 39 | 41 | ||
| @@ -221,6 +223,7 @@ int tns_load(tns_t *tns, int n, const fileinfo_t *file, | |||
| 221 | 223 | float z, zw, zh; | |
| 222 | 224 | thumb_t *t; | |
| 223 | 225 | Imlib_Image *im; | |
| 226 | + | const char *fmt; | |
| 224 | 227 | ||
| 225 | 228 | if (!tns || !tns->thumbs || !file || !file->name || !file->path) | |
| 226 | 229 | return 0; | |
| @@ -252,6 +255,12 @@ int tns_load(tns_t *tns, int n, const fileinfo_t *file, | |||
| 252 | 255 | imlib_context_set_image(im); | |
| 253 | 256 | imlib_context_set_anti_alias(1); | |
| 254 | 257 | ||
| 258 | + | if (!cache_hit) { | |
| 259 | + | fmt = imlib_image_format(); | |
| 260 | + | if (!strcmp(fmt, "jpeg")) | |
| 261 | + | exif_auto_orientate(file); | |
| 262 | + | } | |
| 263 | + | ||
| 255 | 264 | w = imlib_image_get_width(); | |
| 256 | 265 | h = imlib_image_get_height(); | |
| 257 | 266 | zw = (float) THUMB_SIZE / (float) w; | |