4 * Copyright (C) 2009 by ProFUSION embedded systems
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library;
18 * if not, see <http://www.gnu.org/licenses/>.
20 * @author Rafael Antognolli <antognolli@profusion.mobi>
28 #elif defined __GNUC__
29 # define alloca __builtin_alloca
31 # define alloca __alloca
32 #elif defined _MSC_VER
34 # define alloca _alloca
40 void *alloca (size_t);
49 #include <sys/types.h>
56 # define PATH_MAX 4096
60 #include <eina_safety_checks.h>
63 #include <Ecore_Evas.h>
64 #include <Ecore_File.h>
68 #include "ethumb_private.h"
69 #include "Ethumb_Plugin.h"
73 #include <libexif/exif-data.h>
76 static int _log_dom = -1;
77 #define DBG(...) EINA_LOG_DOM_DBG(_log_dom, __VA_ARGS__)
78 #define INF(...) EINA_LOG_DOM_INFO(_log_dom, __VA_ARGS__)
79 #define WRN(...) EINA_LOG_DOM_WARN(_log_dom, __VA_ARGS__)
80 #define ERR(...) EINA_LOG_DOM_ERR(_log_dom, __VA_ARGS__)
82 static int initcount = 0;
83 static const char *_home_thumb_dir = NULL;
84 static const char *_thumb_category_normal = NULL;
85 static const char *_thumb_category_large = NULL;
87 static const int THUMB_SIZE_NORMAL = 128;
88 static const int THUMB_SIZE_LARGE = 256;
90 static Eina_Hash *_plugins_ext = NULL;
91 static Eina_Array *_plugins = NULL;
94 _ethumb_plugin_list_cb(Eina_Module *m, void *data __UNUSED__)
98 Ethumb_Plugin *plugin;
99 Ethumb_Plugin *(*plugin_get)(void);
101 file = eina_module_file_get(m);
102 if (!eina_module_load(m))
104 ERR("could not load module \"%s\": %s",
105 file, eina_error_msg_get(eina_error_get()));
109 plugin_get = eina_module_symbol_get(m, "ethumb_plugin_get");
112 ERR("could not find ethumb_plugin_get() in module \"%s\": %s",
113 file, eina_error_msg_get(eina_error_get()));
114 eina_module_unload(m);
118 plugin = plugin_get();
121 ERR("plugin \"%s\" failed to init.", file);
122 eina_module_unload(m);
126 DBG("loaded plugin \"%s\" (%p) with extensions:", file, plugin);
127 for (ext = plugin->extensions; *ext; ext++)
129 DBG(" extension \"%s\"", *ext);
130 eina_hash_add(_plugins_ext, *ext, plugin);
137 _ethumb_plugins_load(void)
139 _plugins_ext = eina_hash_string_small_new(NULL);
140 EINA_SAFETY_ON_NULL_RETURN(_plugins_ext);
142 _plugins = eina_module_list_get(_plugins, PLUGINSDIR, 1,
143 &_ethumb_plugin_list_cb, NULL);
147 _ethumb_plugins_unload(void)
149 eina_hash_free(_plugins_ext);
151 eina_module_list_unload(_plugins);
152 eina_module_list_free(_plugins);
153 eina_array_free(_plugins);
168 fprintf(stderr, "ERROR: Could not initialize eina.\n");
171 _log_dom = eina_log_domain_register("ethumb", EINA_COLOR_GREEN);
174 EINA_LOG_ERR("Could not register log domain: ethumb");
184 home = getenv("HOME");
185 snprintf(buf, sizeof(buf), "%s/.thumbnails", home);
187 _home_thumb_dir = eina_stringshare_add(buf);
188 _thumb_category_normal = eina_stringshare_add("normal");
189 _thumb_category_large = eina_stringshare_add("large");
191 _ethumb_plugins_load();
196 ethumb_shutdown(void)
201 _ethumb_plugins_unload();
202 eina_stringshare_del(_home_thumb_dir);
203 eina_stringshare_del(_thumb_category_normal);
204 eina_stringshare_del(_thumb_category_large);
207 ecore_evas_shutdown();
209 eina_log_domain_unregister(_log_dom);
221 Ecore_Evas *ee, *sub_ee;
223 Evas_Object *o, *img;
225 ethumb = calloc(1, sizeof(Ethumb));
226 EINA_SAFETY_ON_NULL_RETURN_VAL(ethumb, NULL);
228 /* IF CHANGED, UPDATE DOCS in (Ethumb.c, Ethumb_Client.c, python...)!!! */
229 ethumb->tw = THUMB_SIZE_NORMAL;
230 ethumb->th = THUMB_SIZE_NORMAL;
231 ethumb->orientation = ETHUMB_THUMB_ORIENT_ORIGINAL;
232 ethumb->crop_x = 0.5;
233 ethumb->crop_y = 0.5;
234 ethumb->quality = 80;
235 ethumb->compress = 9;
236 ethumb->video.start = 0.1;
237 ethumb->video.time = 3;
238 ethumb->video.interval = 0.05;
239 ethumb->video.ntimes = 3;
240 ethumb->video.fps = 10;
242 ee = ecore_evas_buffer_new(1, 1);
243 e = ecore_evas_get(ee);
246 ERR("could not create ecore evas buffer");
250 evas_image_cache_set(e, 0);
251 evas_font_cache_set(e, 0);
253 o = ecore_evas_object_image_new(ee);
256 ERR("could not create sub ecore evas buffer");
262 sub_ee = evas_object_data_get(o, "Ecore_Evas");
263 sub_e = ecore_evas_get(sub_ee);
265 evas_image_cache_set(sub_e, 0);
266 evas_font_cache_set(sub_e, 0);
268 img = evas_object_image_add(sub_e);
271 ERR("could not create source objects.");
279 ethumb->sub_ee = sub_ee;
280 ethumb->sub_e = sub_e;
284 DBG("ethumb=%p", ethumb);
290 _ethumb_frame_free(Ethumb_Frame *frame)
297 if (frame->swallow && frame->edje)
299 o = edje_object_part_swallow_get(frame->edje, frame->swallow);
301 edje_object_part_unswallow(frame->edje, o);
303 eina_stringshare_del(frame->file);
304 eina_stringshare_del(frame->group);
305 eina_stringshare_del(frame->swallow);
308 evas_object_del(frame->edje);
314 ethumb_free(Ethumb *ethumb)
316 EINA_SAFETY_ON_NULL_RETURN(ethumb);
318 DBG("ethumb=%p", ethumb);
321 _ethumb_frame_free(ethumb->frame);
322 ethumb_file_free(ethumb);
323 evas_object_del(ethumb->o);
324 ecore_evas_free(ethumb->ee);
325 eina_stringshare_del(ethumb->thumb_dir);
326 eina_stringshare_del(ethumb->category);
327 if (ethumb->finished_idler)
328 ecore_idler_del(ethumb->finished_idler);
333 ethumb_thumb_fdo_set(Ethumb *e, Ethumb_Thumb_FDO_Size s)
335 EINA_SAFETY_ON_NULL_RETURN(e);
336 EINA_SAFETY_ON_FALSE_RETURN(s == ETHUMB_THUMB_NORMAL ||
337 s == ETHUMB_THUMB_LARGE);
338 DBG("ethumb=%p, size=%d", e, s);
340 if (s == ETHUMB_THUMB_NORMAL)
342 e->tw = THUMB_SIZE_NORMAL;
343 e->th = THUMB_SIZE_NORMAL;
347 e->tw = THUMB_SIZE_LARGE;
348 e->th = THUMB_SIZE_LARGE;
351 e->format = ETHUMB_THUMB_FDO;
352 e->aspect = ETHUMB_THUMB_KEEP_ASPECT;
353 e->orientation = ETHUMB_THUMB_ORIENT_ORIGINAL;
354 _ethumb_frame_free(e->frame);
356 eina_stringshare_del(e->thumb_dir);
357 eina_stringshare_del(e->category);
363 ethumb_thumb_size_set(Ethumb *e, int tw, int th)
365 EINA_SAFETY_ON_NULL_RETURN(e);
366 EINA_SAFETY_ON_FALSE_RETURN(tw > 0);
367 EINA_SAFETY_ON_FALSE_RETURN(th > 0);
369 DBG("ethumb=%p, w=%d, h=%d", e, tw, th);
375 ethumb_thumb_size_get(const Ethumb *e, int *tw, int *th)
377 EINA_SAFETY_ON_NULL_RETURN(e);
384 ethumb_thumb_format_set(Ethumb *e, Ethumb_Thumb_Format f)
386 EINA_SAFETY_ON_NULL_RETURN(e);
387 EINA_SAFETY_ON_FALSE_RETURN(f == ETHUMB_THUMB_FDO ||
388 f == ETHUMB_THUMB_JPEG ||
389 f == ETHUMB_THUMB_EET);
391 DBG("ethumb=%p, format=%d", e, f);
395 EAPI Ethumb_Thumb_Format
396 ethumb_thumb_format_get(const Ethumb *e)
398 EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
403 ethumb_thumb_aspect_set(Ethumb *e, Ethumb_Thumb_Aspect a)
405 EINA_SAFETY_ON_NULL_RETURN(e);
406 EINA_SAFETY_ON_FALSE_RETURN(a == ETHUMB_THUMB_KEEP_ASPECT ||
407 a == ETHUMB_THUMB_IGNORE_ASPECT ||
408 a == ETHUMB_THUMB_CROP);
410 DBG("ethumb=%p, aspect=%d", e, a);
414 EAPI Ethumb_Thumb_Aspect
415 ethumb_thumb_aspect_get(const Ethumb *e)
417 EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
422 ethumb_thumb_orientation_set(Ethumb *e, Ethumb_Thumb_Orientation o)
424 EINA_SAFETY_ON_NULL_RETURN(e);
425 EINA_SAFETY_ON_FALSE_RETURN(o == ETHUMB_THUMB_ORIENT_NONE ||
426 o == ETHUMB_THUMB_ROTATE_90_CW ||
427 o == ETHUMB_THUMB_ROTATE_180 ||
428 o == ETHUMB_THUMB_ROTATE_90_CCW ||
429 o == ETHUMB_THUMB_FLIP_HORIZONTAL ||
430 o == ETHUMB_THUMB_FLIP_VERTICAL ||
431 o == ETHUMB_THUMB_FLIP_TRANSPOSE ||
432 o == ETHUMB_THUMB_FLIP_TRANSVERSE ||
433 o == ETHUMB_THUMB_ORIENT_ORIGINAL);
435 DBG("ethumb=%p, orientation=%d", e, o);
439 EAPI Ethumb_Thumb_Orientation
440 ethumb_thumb_orientation_get(const Ethumb *e)
442 EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
443 return e->orientation;
447 ethumb_thumb_crop_align_set(Ethumb *e, float x, float y)
449 EINA_SAFETY_ON_NULL_RETURN(e);
451 DBG("ethumb=%p, x=%f, y=%f", e, x, y);
457 ethumb_thumb_crop_align_get(const Ethumb *e, float *x, float *y)
459 EINA_SAFETY_ON_NULL_RETURN(e);
461 if (x) *x = e->crop_x;
462 if (y) *y = e->crop_y;
466 ethumb_thumb_quality_set(Ethumb *e, int quality)
468 EINA_SAFETY_ON_NULL_RETURN(e);
470 DBG("ethumb=%p, quality=%d", e, quality);
471 e->quality = quality;
475 ethumb_thumb_quality_get(const Ethumb *e)
477 EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
482 ethumb_thumb_compress_set(Ethumb *e, int compress)
484 EINA_SAFETY_ON_NULL_RETURN(e);
486 DBG("ethumb=%p, compress=%d", e, compress);
487 e->compress = compress;
491 ethumb_thumb_compress_get(const Ethumb *e)
493 EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
498 ethumb_frame_set(Ethumb *e, const char *theme_file, const char *group, const char *swallow)
500 EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
505 DBG("ethumb=%p, theme_file=%s, group=%s, swallow=%s",
506 e, theme_file ? theme_file : "", group ? group : "",
507 swallow ? swallow : "");
511 edje_object_part_unswallow(frame->edje, e->img);
513 _ethumb_frame_free(frame);
524 frame = calloc(1, sizeof(Ethumb_Frame));
527 ERR("could not allocate Ethumb_Frame structure.");
531 frame->edje = edje_object_add(e->sub_e);
534 ERR("could not create edje frame object.");
535 _ethumb_frame_free(frame);
541 if (!edje_object_file_set(frame->edje, theme_file, group))
543 ERR("could not load frame theme.");
544 _ethumb_frame_free(frame);
549 edje_object_part_swallow(frame->edje, swallow, e->img);
550 if (!edje_object_part_swallow_get(frame->edje, swallow))
552 ERR("could not swallow image to edje frame.");
553 _ethumb_frame_free(frame);
558 eina_stringshare_replace(&frame->file, theme_file);
559 eina_stringshare_replace(&frame->group, group);
560 eina_stringshare_replace(&frame->swallow, swallow);
568 ethumb_frame_get(const Ethumb *e, const char **theme_file, const char **group, const char **swallow)
570 EINA_SAFETY_ON_NULL_RETURN(e);
574 if (theme_file) *theme_file = e->frame->file;
575 if (group) *group = e->frame->group;
576 if (swallow) *swallow = e->frame->swallow;
580 if (theme_file) *theme_file = NULL;
581 if (group) *group = NULL;
582 if (swallow) *swallow = NULL;
587 _ethumb_build_absolute_path(const char *path, char buf[PATH_MAX])
599 else if (path[0] == '~')
601 const char *home = getenv("HOME");
613 if (!getcwd(p, PATH_MAX))
626 ethumb_thumb_dir_path_set(Ethumb *e, const char *path)
629 EINA_SAFETY_ON_NULL_RETURN(e);
631 DBG("ethumb=%p, path=%s", e, path ? path : "");
632 path = _ethumb_build_absolute_path(path, buf);
633 eina_stringshare_replace(&e->thumb_dir, path);
637 ethumb_thumb_dir_path_get(const Ethumb *e)
639 EINA_SAFETY_ON_NULL_RETURN_VAL(e, NULL);
645 ethumb_thumb_category_set(Ethumb *e, const char *category)
647 EINA_SAFETY_ON_NULL_RETURN(e);
649 DBG("ethumb=%p, category=%s", e, category ? category : "");
650 eina_stringshare_replace(&e->category, category);
654 ethumb_thumb_category_get(const Ethumb *e)
656 EINA_SAFETY_ON_NULL_RETURN_VAL(e, NULL);
662 ethumb_video_start_set(Ethumb *e, float start)
664 EINA_SAFETY_ON_NULL_RETURN(e);
665 EINA_SAFETY_ON_FALSE_RETURN(start >= 0.0);
666 EINA_SAFETY_ON_FALSE_RETURN(start <= 1.0);
668 DBG("ethumb=%p, video_start=%f", e, start);
669 e->video.start = start;
673 ethumb_video_start_get(const Ethumb *e)
675 EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
677 return e->video.start;
681 ethumb_video_time_set(Ethumb *e, float time)
683 EINA_SAFETY_ON_NULL_RETURN(e);
685 DBG("ethumb=%p, video_start=%f", e, time);
686 e->video.time = time;
690 ethumb_video_time_get(const Ethumb *e)
692 EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
694 return e->video.time;
698 ethumb_video_interval_set(Ethumb *e, float interval)
700 EINA_SAFETY_ON_NULL_RETURN(e);
702 DBG("ethumb=%p, video_interval=%f", e, interval);
703 e->video.interval = interval;
707 ethumb_video_interval_get(const Ethumb *e)
709 EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
711 return e->video.interval;
715 ethumb_video_ntimes_set(Ethumb *e, unsigned int ntimes)
717 EINA_SAFETY_ON_NULL_RETURN(e);
718 EINA_SAFETY_ON_FALSE_RETURN(ntimes > 0);
720 DBG("ethumb=%p, video_ntimes=%d", e, ntimes);
721 e->video.ntimes = ntimes;
725 ethumb_video_ntimes_get(const Ethumb *e)
727 EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
729 return e->video.ntimes;
733 ethumb_video_fps_set(Ethumb *e, unsigned int fps)
735 EINA_SAFETY_ON_NULL_RETURN(e);
736 EINA_SAFETY_ON_FALSE_RETURN(fps > 0);
738 DBG("ethumb=%p, video_fps=%d", e, fps);
743 ethumb_video_fps_get(const Ethumb *e)
745 EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
751 ethumb_document_page_set(Ethumb *e, unsigned int page)
753 EINA_SAFETY_ON_NULL_RETURN(e);
755 DBG("ethumb=%p, document_page=%d", e, page);
756 e->document.page = page;
760 ethumb_document_page_get(const Ethumb *e)
762 EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
764 return e->document.page;
768 ethumb_file_set(Ethumb *e, const char *path, const char *key)
771 EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
773 DBG("ethumb=%p, path=%s, key=%s", e, path ? path : "", key ? key : "");
774 if (path && access(path, R_OK))
776 ERR("couldn't access file \"%s\"", path);
780 path = _ethumb_build_absolute_path(path, buf);
781 eina_stringshare_replace(&e->src_path, path);
782 eina_stringshare_replace(&e->src_key, key);
783 eina_stringshare_replace(&e->thumb_path, NULL);
784 eina_stringshare_replace(&e->thumb_key, NULL);
790 ethumb_file_get(const Ethumb *e, const char **path, const char **key)
792 EINA_SAFETY_ON_NULL_RETURN(e);
794 if (path) *path = e->src_path;
795 if (key) *key = e->src_key;
798 static const char ACCEPTABLE_URI_CHARS[96] = {
799 /* ! " # $ % & ' ( ) * + , - . / */
800 0x00,0x3F,0x20,0x20,0x28,0x00,0x2C,0x3F,0x3F,0x3F,0x3F,0x2A,0x28,0x3F,0x3F,0x1C,
801 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
802 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x38,0x20,0x20,0x2C,0x20,0x20,
803 /* @ A B C D E F G H I J K L M N O */
804 0x38,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
805 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
806 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x20,0x3F,
807 /* ` a b c d e f g h i j k l m n o */
808 0x20,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
809 /* p q r s t u v w x y z { | } ~ DEL */
810 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x3F,0x20
814 _ethumb_generate_hash(const char *file)
818 char md5out[(2 * MD5_HASHBYTES) + 1];
819 unsigned char hash[MD5_HASHBYTES];
820 static const char hex[] = "0123456789abcdef";
824 const unsigned char *c;
826 #define _check_uri_char(c) \
827 ((c) >= 32 && (c) < 128 && (ACCEPTABLE_URI_CHARS[(c) - 32] & 0x08))
829 EINA_SAFETY_ON_NULL_RETURN_VAL(file, NULL);
831 uri = alloca(3 * strlen(file) + 9);
832 memcpy(uri, "file://", sizeof("file://") - 1);
833 t = uri + sizeof("file://") - 1;
835 for (c = (const unsigned char *)file; *c != '\0'; c++)
837 if (!_check_uri_char(*c))
848 #undef _check_uri_char
851 MD5Update (&ctx, (unsigned char const*)uri, (unsigned)strlen (uri));
852 MD5Final (hash, &ctx);
854 for (n = 0; n < MD5_HASHBYTES; n++)
856 md5out[2 * n] = hex[hash[n] >> 4];
857 md5out[2 * n + 1] = hex[hash[n] & 0x0f];
859 md5out[2 * n] = '\0';
861 DBG("md5=%s, file=%s", md5out, file);
862 return eina_stringshare_add(md5out);
866 _ethumb_file_check_fdo(Ethumb *e)
868 if (!((e->tw == THUMB_SIZE_NORMAL && e->th == THUMB_SIZE_NORMAL) ||
869 (e->tw == THUMB_SIZE_LARGE && e->th == THUMB_SIZE_LARGE)))
872 if (e->format != ETHUMB_THUMB_FDO)
875 if (e->aspect != ETHUMB_THUMB_KEEP_ASPECT)
885 _ethumb_file_generate_custom_category(Ethumb *e)
888 const char *aspect, *format;
891 if (e->aspect == ETHUMB_THUMB_KEEP_ASPECT)
892 aspect = "keep_aspect";
893 else if (e->aspect == ETHUMB_THUMB_IGNORE_ASPECT)
894 aspect = "ignore_aspect";
898 if (e->format == ETHUMB_THUMB_FDO)
900 else if (e->format == ETHUMB_THUMB_JPEG)
910 snprintf(buf, sizeof(buf), "%dx%d-%s%s-%s",
911 e->tw, e->th, aspect, frame, format);
913 return eina_stringshare_add(buf);
917 _ethumb_file_generate_path(Ethumb *e)
922 const char *thumb_dir, *category;
927 fdo_format = _ethumb_file_check_fdo(e);
930 thumb_dir = eina_stringshare_ref(e->thumb_dir);
932 thumb_dir = eina_stringshare_ref(_home_thumb_dir);
935 category = eina_stringshare_ref(e->category);
936 else if (!fdo_format)
937 category = _ethumb_file_generate_custom_category(e);
940 if (e->tw == THUMB_SIZE_NORMAL)
941 category = eina_stringshare_ref(_thumb_category_normal);
942 else if (e->tw == THUMB_SIZE_LARGE)
943 category = eina_stringshare_ref(_thumb_category_large);
946 ERR("fdo_format but size %d is not NORMAL (%d) or LARGE (%d)?",
947 e->tw, THUMB_SIZE_NORMAL, THUMB_SIZE_LARGE);
948 category = "unknown";
952 if (e->format == ETHUMB_THUMB_FDO)
954 else if (e->format == ETHUMB_THUMB_JPEG)
960 fullname = ecore_file_realpath(e->src_path);
961 hash = _ethumb_generate_hash(fullname);
962 snprintf(buf, sizeof(buf), "%s/%s/%s.%s", thumb_dir, category, hash, ext);
964 DBG("ethumb=%p, path=%s", e, buf);
965 eina_stringshare_replace(&e->thumb_path, buf);
966 if (e->format == ETHUMB_THUMB_EET)
967 eina_stringshare_replace(&e->thumb_key, "thumbnail");
970 eina_stringshare_del(e->thumb_key);
974 eina_stringshare_del(thumb_dir);
975 eina_stringshare_del(category);
976 eina_stringshare_del(hash);
980 ethumb_file_free(Ethumb *e)
982 EINA_SAFETY_ON_NULL_RETURN(e);
985 eina_stringshare_replace(&e->src_path, NULL);
986 eina_stringshare_replace(&e->src_key, NULL);
987 eina_stringshare_replace(&e->thumb_path, NULL);
988 eina_stringshare_replace(&e->thumb_key, NULL);
992 ethumb_thumb_path_set(Ethumb *e, const char *path, const char *key)
996 EINA_SAFETY_ON_NULL_RETURN(e);
997 DBG("ethumb=%p, path=%s, key=%s", e, path ? path : "", key ? key : "");
1001 eina_stringshare_replace(&e->thumb_path, NULL);
1002 eina_stringshare_replace(&e->thumb_key, NULL);
1006 path = _ethumb_build_absolute_path(path, buf);
1007 eina_stringshare_replace(&e->thumb_path, path);
1008 eina_stringshare_replace(&e->thumb_key, key);
1013 ethumb_thumb_path_get(Ethumb *e, const char **path, const char **key)
1015 EINA_SAFETY_ON_NULL_RETURN(e);
1017 _ethumb_file_generate_path(e);
1019 if (path) *path = e->thumb_path;
1020 if (key) *key = e->thumb_key;
1024 ethumb_calculate_aspect_from_ratio(Ethumb *e, float ia, int *w, int *h)
1034 a = e->tw / (float)e->th;
1036 if (e->aspect == ETHUMB_THUMB_KEEP_ASPECT)
1038 if ((ia > a && e->tw > 0) || e->th <= 0)
1046 ethumb_calculate_aspect(Ethumb *e, int iw, int ih, int *w, int *h)
1050 ia = iw / (float)ih;
1052 ethumb_calculate_aspect_from_ratio(e, ia, w, h);
1056 ethumb_calculate_fill_from_ratio(Ethumb *e, float ia, int *fx, int *fy, int *fw, int *fh)
1068 a = e->tw / (float)e->th;
1070 if (e->aspect == ETHUMB_THUMB_CROP)
1072 if ((ia > a && e->tw > 0) || e->th <= 0)
1077 *fx = - e->crop_x * (*fw - e->tw);
1078 *fy = - e->crop_y * (*fh - e->th);
1080 else if (e->aspect == ETHUMB_THUMB_KEEP_ASPECT)
1082 if ((ia > a && e->tw > 0) || e->th <= 0)
1090 ethumb_calculate_fill(Ethumb *e, int iw, int ih, int *fx, int *fy, int *fw, int *fh)
1093 ia = iw / (float)ih;
1095 ethumb_calculate_fill_from_ratio(e, ia, fx, fy, fw, fh);
1099 _ethumb_plugin_generate(Ethumb *e)
1103 Ethumb_Plugin *plugin;
1106 extp = strrchr(e->src_path, '.');
1109 ERR("could not get extension for file \"%s\"", e->src_path);
1113 for (i = 0; extp[i] != '\0'; i++)
1114 ext[i] = tolower(extp[i + 1]);
1116 plugin = eina_hash_find(_plugins_ext, ext);
1119 DBG("no plugin for extension: \"%s\"", ext);
1124 evas_object_hide(e->frame->edje);
1126 evas_object_hide(e->img);
1128 plugin->generate_thumb(e);
1134 ethumb_plugin_image_resize(Ethumb *e, int w, int h)
1142 edje_extern_object_min_size_set(img, w, h);
1143 edje_extern_object_max_size_set(img, w, h);
1144 edje_object_calc_force(e->frame->edje);
1145 evas_object_move(e->frame->edje, 0, 0);
1146 evas_object_resize(e->frame->edje, w, h);
1150 evas_object_move(img, 0, 0);
1151 evas_object_resize(img, w, h);
1154 evas_object_image_size_set(e->o, w, h);
1155 ecore_evas_resize(e->sub_ee, w, h);
1164 ethumb_image_save(Ethumb *e)
1170 evas_damage_rectangle_add(e->sub_e, 0, 0, e->rw, e->rh);
1171 evas_render(e->sub_e);
1174 _ethumb_file_generate_path(e);
1178 ERR("could not create file path...");
1182 dname = ecore_file_dir_get(e->thumb_path);
1183 r = ecore_file_mkpath(dname);
1187 ERR("could not create directory '%s'", dname);
1191 snprintf(flags, sizeof(flags), "quality=%d compress=%d",
1192 e->quality, e->compress);
1193 r = evas_object_image_save(e->o, e->thumb_path, e->thumb_key, flags);
1197 ERR("could not save image: path=%s, key=%s", e->thumb_path,
1206 _ethumb_image_orient(Ethumb *e, int orientation)
1208 Evas_Object *img = e->img, *tmp;
1209 unsigned int *data, *data2, *to, *from, *p1, *p2, pt;
1210 int x, y, w, hw, iw, ih, tw, th;
1211 const char *file, *key;
1213 evas_object_image_size_get(img, &iw, &ih);
1214 data = evas_object_image_data_get(img, 1);
1216 switch (orientation)
1218 case ETHUMB_THUMB_FLIP_HORIZONTAL:
1219 for (y = 0; y < ih; y++)
1221 p1 = data + (y * iw);
1222 p2 = data + ((y + 1) * iw) - 1;
1223 for (x = 0; x < (iw >> 1); x++)
1232 evas_object_image_data_set(img, data);
1233 evas_object_image_data_update_add(img, 0, 0, iw, ih);
1235 case ETHUMB_THUMB_FLIP_VERTICAL:
1236 for (y = 0; y < (ih >> 1); y++)
1238 p1 = data + (y * iw);
1239 p2 = data + ((ih - 1 - y) * iw);
1240 for (x = 0; x < iw; x++)
1249 evas_object_image_data_set(img, data);
1250 evas_object_image_data_update_add(img, 0, 0, iw, ih);
1252 case ETHUMB_THUMB_ROTATE_180:
1265 evas_object_image_data_set(img, data);
1266 evas_object_image_data_update_add(img, 0, 0, iw, ih);
1270 evas_object_image_load_size_get(img, &tw, &th);
1271 evas_object_image_file_get(img, &file, &key);
1272 tmp = evas_object_image_add(evas_object_evas_get(img));
1273 evas_object_image_load_size_set(tmp, tw, th);
1274 evas_object_image_file_set(tmp, file, key);
1275 data2 = evas_object_image_data_get(tmp, 0);
1282 evas_object_image_size_set(img, iw, ih);
1283 data = evas_object_image_data_get(img, 1);
1285 switch (orientation)
1287 case ETHUMB_THUMB_FLIP_TRANSPOSE:
1291 case ETHUMB_THUMB_FLIP_TRANSVERSE:
1296 case ETHUMB_THUMB_ROTATE_90_CW:
1300 case ETHUMB_THUMB_ROTATE_90_CCW:
1306 ERR("unknown orient %d", orientation);
1307 evas_object_del(tmp);
1308 evas_object_image_data_set(img, data); // give it back
1312 for (x = iw; --x >= 0;)
1314 for (y = ih; --y >= 0;)
1322 evas_object_del(tmp);
1323 evas_object_image_data_set(img, data);
1324 evas_object_image_data_update_add(img, 0, 0, iw, ih);
1328 _ethumb_image_load(Ethumb *e)
1331 Evas_Coord w, h, ww, hh, fx, fy, fw, fh;
1333 int orientation = ETHUMB_THUMB_ORIENT_NONE;
1338 evas_object_hide(e->frame->edje);
1340 evas_object_hide(img);
1341 evas_object_image_file_set(img, NULL, NULL);
1342 evas_object_image_load_size_set(img, e->tw, e->th);
1343 evas_object_image_file_set(img, e->src_path, e->src_key);
1346 evas_object_show(e->frame->edje);
1348 evas_object_show(img);
1350 error = evas_object_image_load_error_get(img);
1351 if (error != EVAS_LOAD_ERROR_NONE)
1353 ERR("could not load image '%s': %d", e->src_path, error);
1357 if (e->orientation == ETHUMB_THUMB_ORIENT_ORIGINAL)
1360 ExifData *exif = exif_data_new_from_file(e->src_path);
1361 ExifEntry *entry = NULL;
1367 entry = exif_data_get_entry(exif, EXIF_TAG_ORIENTATION);
1370 bo = exif_data_get_byte_order(exif);
1371 o = exif_get_short(entry->data, bo);
1373 exif_data_free(exif);
1377 orientation = ETHUMB_THUMB_FLIP_HORIZONTAL;
1380 orientation = ETHUMB_THUMB_ROTATE_180;
1383 orientation = ETHUMB_THUMB_FLIP_VERTICAL;
1386 orientation = ETHUMB_THUMB_FLIP_TRANSPOSE;
1389 orientation = ETHUMB_THUMB_ROTATE_90_CW;
1392 orientation = ETHUMB_THUMB_FLIP_TRANSVERSE;
1395 orientation = ETHUMB_THUMB_ROTATE_90_CCW;
1402 if (orientation != ETHUMB_THUMB_ORIENT_NONE)
1403 _ethumb_image_orient(e, orientation);
1405 evas_object_image_size_get(img, &w, &h);
1406 if ((w <= 0) || (h <= 0))
1409 ethumb_calculate_aspect(e, w, h, &ww, &hh);
1413 edje_extern_object_min_size_set(img, ww, hh);
1414 edje_extern_object_max_size_set(img, ww, hh);
1415 edje_object_calc_force(e->frame->edje);
1416 evas_object_move(e->frame->edje, 0, 0);
1417 evas_object_resize(e->frame->edje, ww, hh);
1421 evas_object_move(img, 0, 0);
1422 evas_object_resize(img, ww, hh);
1425 ethumb_calculate_fill(e, w, h, &fx, &fy, &fw, &fh);
1426 evas_object_image_fill_set(img, fx, fy, fw, fh);
1428 evas_object_image_size_set(e->o, ww, hh);
1429 ecore_evas_resize(e->sub_ee, ww, hh);
1438 _ethumb_finished_idler_cb(void *data)
1442 e->finished_cb(e->cb_data, e, e->cb_result);
1443 if (e->cb_data_free)
1444 e->cb_data_free(e->cb_data);
1445 e->finished_idler = NULL;
1446 e->finished_cb = NULL;
1448 e->cb_data_free = NULL;
1454 ethumb_finished_callback_call(Ethumb *e, int result)
1456 EINA_SAFETY_ON_NULL_RETURN(e);
1458 e->cb_result = result;
1459 if (e->finished_idler)
1460 ecore_idler_del(e->finished_idler);
1461 e->finished_idler = ecore_idler_add(_ethumb_finished_idler_cb, e);
1465 ethumb_generate(Ethumb *e, Ethumb_Generate_Cb finished_cb, const void *data, Eina_Free_Cb free_data)
1469 EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
1470 EINA_SAFETY_ON_NULL_RETURN_VAL(finished_cb, 0);
1471 DBG("ethumb=%p, finished_cb=%p, data=%p, free_data=%p, path=%s, key=%s",
1472 e, finished_cb, data, free_data,
1473 e->src_path ? e->src_path : "", e->src_key ? e->src_key : "");
1475 if (e->finished_idler)
1477 ERR("thumbnail generation already in progress.");
1480 e->finished_cb = finished_cb;
1481 e->cb_data = (void *)data;
1482 e->cb_data_free = free_data;
1486 ERR("no file set.");
1487 ethumb_finished_callback_call(e, 0);
1491 r = _ethumb_plugin_generate(e);
1495 if (!_ethumb_image_load(e))
1497 ERR("could not load input image.");
1498 ethumb_finished_callback_call(e, 0);
1502 r = ethumb_image_save(e);
1504 ethumb_finished_callback_call(e, r);
1510 ethumb_exists(Ethumb *e)
1512 struct stat thumb, src;
1514 Eina_Bool r = EINA_FALSE;
1516 EINA_SAFETY_ON_NULL_RETURN_VAL(e, 0);
1517 EINA_SAFETY_ON_NULL_RETURN_VAL(e->src_path, 0);
1518 DBG("ethumb=%p, path=%s", e, e->src_path ? e->src_path : "");
1521 _ethumb_file_generate_path(e);
1523 EINA_SAFETY_ON_NULL_RETURN_VAL(e->thumb_path, 0);
1525 r_thumb = stat(e->thumb_path, &thumb);
1526 r_src = stat(e->src_path, &src);
1528 EINA_SAFETY_ON_TRUE_RETURN_VAL(r_src, 0);
1530 if (r_thumb && errno != ENOENT)
1531 ERR("could not access file \"%s\": %s", e->thumb_path, strerror(errno));
1532 else if (!r_thumb && thumb.st_mtime > src.st_mtime)
1539 ethumb_evas_get(const Ethumb *e)
1541 EINA_SAFETY_ON_NULL_RETURN_VAL(e, NULL);
1547 ethumb_ecore_evas_get(const Ethumb *e)
1549 EINA_SAFETY_ON_NULL_RETURN_VAL(e, NULL);