4 * Copyright (C) 2008 - 2009 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
15 #include "image-load.h"
19 #include "ui_fileops.h"
20 #include "gq-marshal.h"
25 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
26 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
29 /**************************************************************************************/
30 /* image loader class */
34 SIGNAL_AREA_READY = 0,
41 static guint signals[SIGNAL_COUNT] = { 0 };
43 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
44 static void image_loader_class_init(ImageLoaderClass *class);
45 static void image_loader_finalize(GObject *object);
46 static void image_loader_stop(ImageLoader *il);
48 GType image_loader_get_type(void)
50 static GType type = 0;
53 static const GTypeInfo info = {
54 sizeof(ImageLoaderClass),
56 NULL, /* base_finalize */
57 (GClassInitFunc)image_loader_class_init, /* class_init */
58 NULL, /* class_finalize */
59 NULL, /* class_data */
62 (GInstanceInitFunc)image_loader_init, /* instance_init */
63 NULL /* value_table */
65 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
70 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
72 ImageLoader *il = (ImageLoader *)instance;
76 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
83 il->idle_done_id = -1;
85 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
86 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
87 il->mapped_file = NULL;
89 il->requested_width = 0;
90 il->requested_height = 0;
93 il->can_destroy = TRUE;
96 il->data_mutex = g_mutex_new();
97 il->can_destroy_cond = g_cond_new();
99 DEBUG_1("new image loader %p, bufsize=%u idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
102 static void image_loader_class_init(ImageLoaderClass *class)
104 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
106 // gobject_class->set_property = image_loader_set_property;
107 // gobject_class->get_property = image_loader_get_property;
109 gobject_class->finalize = image_loader_finalize;
112 signals[SIGNAL_AREA_READY] =
113 g_signal_new("area_ready",
114 G_OBJECT_CLASS_TYPE(gobject_class),
116 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
118 gq_marshal_VOID__INT_INT_INT_INT,
125 signals[SIGNAL_ERROR] =
126 g_signal_new("error",
127 G_OBJECT_CLASS_TYPE(gobject_class),
129 G_STRUCT_OFFSET(ImageLoaderClass, error),
131 g_cclosure_marshal_VOID__VOID,
134 signals[SIGNAL_DONE] =
136 G_OBJECT_CLASS_TYPE(gobject_class),
138 G_STRUCT_OFFSET(ImageLoaderClass, done),
140 g_cclosure_marshal_VOID__VOID,
143 signals[SIGNAL_PERCENT] =
144 g_signal_new("percent",
145 G_OBJECT_CLASS_TYPE(gobject_class),
147 G_STRUCT_OFFSET(ImageLoaderClass, percent),
149 g_cclosure_marshal_VOID__DOUBLE,
155 static void image_loader_finalize(GObject *object)
157 ImageLoader *il = (ImageLoader *)object;
159 image_loader_stop(il);
161 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
163 DEBUG_1("freeing image loader %p bytes_read=%d", il, il->bytes_read);
165 if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
167 while (g_source_remove_by_user_data(il))
169 DEBUG_2("pending signals detected");
172 while (il->area_param_list)
174 DEBUG_1("pending area_ready signals detected");
175 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
176 g_free(il->area_param_list->data);
177 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
180 while (il->area_param_delayed_list)
182 g_free(il->area_param_delayed_list->data);
183 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
186 if (il->pixbuf) g_object_unref(il->pixbuf);
188 if (il->error) g_error_free(il->error);
190 file_data_unref(il->fd);
192 g_mutex_free(il->data_mutex);
193 g_cond_free(il->can_destroy_cond);
197 void image_loader_free(ImageLoader *il)
200 g_object_unref(G_OBJECT(il));
204 ImageLoader *image_loader_new(FileData *fd)
208 if (!fd) return NULL;
210 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
212 il->fd = file_data_ref(fd);
217 /**************************************************************************************/
218 /* send signals via idle calbacks - the callback are executed in the main thread */
220 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
221 struct _ImageLoaderAreaParam {
230 static gboolean image_loader_emit_area_ready_cb(gpointer data)
232 ImageLoaderAreaParam *par = data;
233 ImageLoader *il = par->il;
234 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
235 g_mutex_lock(il->data_mutex);
236 il->area_param_list = g_list_remove(il->area_param_list, par);
238 g_mutex_unlock(il->data_mutex);
243 static gboolean image_loader_emit_done_cb(gpointer data)
245 ImageLoader *il = data;
246 g_signal_emit(il, signals[SIGNAL_DONE], 0);
250 static gboolean image_loader_emit_error_cb(gpointer data)
252 ImageLoader *il = data;
253 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
257 static gboolean image_loader_emit_percent_cb(gpointer data)
259 ImageLoader *il = data;
260 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
264 /* DONE and ERROR are emited only once, thus they can have normal priority
265 PERCENT and AREA_READY should be processed ASAP
268 static void image_loader_emit_done(ImageLoader *il)
270 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
273 static void image_loader_emit_error(ImageLoader *il)
275 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
278 static void image_loader_emit_percent(ImageLoader *il)
280 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
283 /* this function expects that il->data_mutex is locked by caller */
284 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
286 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
293 il->area_param_list = g_list_prepend(il->area_param_list, par);
295 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
298 /**************************************************************************************/
299 /* the following functions may be executed in separate thread */
301 /* this function expects that il->data_mutex is locked by caller */
302 static void image_loader_queue_delayed_erea_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
304 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
311 il->area_param_delayed_list = g_list_prepend(il->area_param_delayed_list, par);
316 static gboolean image_loader_get_stopping(ImageLoader *il)
319 if (!il) return FALSE;
321 g_mutex_lock(il->data_mutex);
323 g_mutex_unlock(il->data_mutex);
329 static void image_loader_sync_pixbuf(ImageLoader *il)
333 g_mutex_lock(il->data_mutex);
337 g_mutex_unlock(il->data_mutex);
341 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
343 if (pb == il->pixbuf)
345 g_mutex_unlock(il->data_mutex);
349 if (il->pixbuf) g_object_unref(il->pixbuf);
352 if (il->pixbuf) g_object_ref(il->pixbuf);
354 g_mutex_unlock(il->data_mutex);
357 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
358 guint x, guint y, guint w, guint h,
361 ImageLoader *il = data;
363 if (!image_loader_get_pixbuf(il))
365 image_loader_sync_pixbuf(il);
366 if (!image_loader_get_pixbuf(il))
368 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
372 g_mutex_lock(il->data_mutex);
373 if (il->delay_area_ready)
374 image_loader_queue_delayed_erea_ready(il, x, y, w, h);
376 image_loader_emit_area_ready(il, x, y, w, h);
377 g_mutex_unlock(il->data_mutex);
380 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
386 /* a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669 */
387 gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
388 if (strcmp(format, "svg") == 0)
396 pb = gdk_pixbuf_loader_get_pixbuf(loader);
398 h = gdk_pixbuf_get_height(pb);
399 rs = gdk_pixbuf_get_rowstride(pb);
400 pix = gdk_pixbuf_get_pixels(pb);
402 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
406 static void image_loader_size_cb(GdkPixbufLoader *loader,
407 gint width, gint height, gpointer data)
409 ImageLoader *il = data;
410 GdkPixbufFormat *format;
412 gboolean scale = FALSE;
415 g_mutex_lock(il->data_mutex);
416 if (il->requested_width < 1 || il->requested_height < 1)
418 g_mutex_unlock(il->data_mutex);
421 g_mutex_unlock(il->data_mutex);
423 format = gdk_pixbuf_loader_get_format(loader);
426 mime_types = gdk_pixbuf_format_get_mime_types(format);
428 while (mime_types[n])
430 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
433 g_strfreev(mime_types);
437 g_mutex_lock(il->data_mutex);
439 if (width > il->requested_width || height > il->requested_height)
443 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
445 nw = il->requested_width;
446 nh = (gdouble)nw / width * height;
451 nh = il->requested_height;
452 nw = (gdouble)nh / height * width;
456 gdk_pixbuf_loader_set_size(loader, nw, nh);
459 g_mutex_unlock(il->data_mutex);
463 static void image_loader_stop_loader(ImageLoader *il)
469 /* some loaders do not have a pixbuf till close, order is important here */
470 gdk_pixbuf_loader_close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
471 image_loader_sync_pixbuf(il);
472 g_object_unref(G_OBJECT(il->loader));
475 g_mutex_lock(il->data_mutex);
477 g_mutex_unlock(il->data_mutex);
480 static void image_loader_setup_loader(ImageLoader *il)
482 g_mutex_lock(il->data_mutex);
483 il->loader = gdk_pixbuf_loader_new();
485 g_signal_connect(G_OBJECT(il->loader), "area_updated",
486 G_CALLBACK(image_loader_area_updated_cb), il);
487 g_signal_connect(G_OBJECT(il->loader), "size_prepared",
488 G_CALLBACK(image_loader_size_cb), il);
489 g_signal_connect(G_OBJECT(il->loader), "area_prepared",
490 G_CALLBACK(image_loader_area_prepared_cb), il);
491 g_mutex_unlock(il->data_mutex);
495 static void image_loader_done(ImageLoader *il)
497 image_loader_stop_loader(il);
499 image_loader_emit_done(il);
502 static void image_loader_error(ImageLoader *il)
504 image_loader_stop_loader(il);
506 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
508 image_loader_emit_error(il);
511 static gboolean image_loader_continue(ImageLoader *il)
516 if (!il) return FALSE;
518 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
519 while (c > 0 && !image_loader_get_stopping(il))
521 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
525 image_loader_done(il);
529 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
531 image_loader_error(il);
540 if (il->bytes_total > 0)
542 image_loader_emit_percent(il);
548 static gboolean image_loader_begin(ImageLoader *il)
552 if (il->pixbuf) return FALSE;
554 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
555 if (b < 1) return FALSE;
557 image_loader_setup_loader(il);
559 if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error))
561 image_loader_stop_loader(il);
567 /* read until size is known */
568 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
570 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
571 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
573 image_loader_stop_loader(il);
578 if (!il->pixbuf) image_loader_sync_pixbuf(il);
580 if (il->bytes_read == il->bytes_total || b < 1)
582 /* done, handle (broken) loaders that do not have pixbuf till close */
583 image_loader_stop_loader(il);
585 if (!il->pixbuf) return FALSE;
587 image_loader_done(il);
593 image_loader_stop_loader(il);
600 /**************************************************************************************/
601 /* the following functions are always executed in the main thread */
604 static gboolean image_loader_setup_source(ImageLoader *il)
609 if (!il || il->loader || il->mapped_file) return FALSE;
611 il->mapped_file = NULL;
615 ExifData *exif = exif_read_fd(il->fd);
617 if (options->thumbnails.use_exif)
618 il->mapped_file = exif_get_preview(exif, &il->bytes_total, il->requested_width, il->requested_height);
620 il->mapped_file = exif_get_preview(exif, &il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
625 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
627 exif_free_fd(il->fd, exif);
631 if (!il->mapped_file)
636 pathl = path_from_utf8(il->fd->path);
637 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
639 if (load_fd == -1) return FALSE;
641 if (fstat(load_fd, &st) == 0)
643 il->bytes_total = st.st_size;
651 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
653 if (il->mapped_file == MAP_FAILED)
664 static void image_loader_stop_source(ImageLoader *il)
672 exif_free_preview(il->mapped_file);
676 munmap(il->mapped_file, il->bytes_total);
678 il->mapped_file = NULL;
682 static void image_loader_stop(ImageLoader *il)
686 if (il->idle_id != -1)
688 g_source_remove(il->idle_id);
694 /* stop loader in the other thread */
695 g_mutex_lock(il->data_mutex);
697 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
698 g_mutex_unlock(il->data_mutex);
701 image_loader_stop_loader(il);
702 image_loader_stop_source(il);
706 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
708 g_mutex_lock(il->data_mutex);
709 il->delay_area_ready = enable;
714 list = g_list_reverse(il->area_param_delayed_list);
715 il->area_param_delayed_list = NULL;
716 g_mutex_unlock(il->data_mutex);
722 ImageLoaderAreaParam *par = work->data;
725 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
733 g_mutex_unlock(il->data_mutex);
738 /**************************************************************************************/
739 /* execution via idle calls */
741 static gboolean image_loader_idle_cb(gpointer data)
743 gboolean ret = FALSE;
744 ImageLoader *il = data;
746 if (il->idle_id != -1)
748 ret = image_loader_continue(il);
753 image_loader_stop_source(il);
760 static gboolean image_loader_start_idle(ImageLoader *il)
764 if (!il) return FALSE;
766 if (!il->fd) return FALSE;
768 if (!image_loader_setup_source(il)) return FALSE;
770 ret = image_loader_begin(il);
772 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
776 /**************************************************************************************/
777 /* execution via thread */
778 static GThreadPool *image_loader_thread_pool = NULL;
780 static GCond *image_loader_prio_cond = NULL;
781 static GMutex *image_loader_prio_mutex = NULL;
782 static gint image_loader_prio_num = 0;
785 static void image_loader_thread_enter_high(void)
787 g_mutex_lock(image_loader_prio_mutex);
788 image_loader_prio_num++;
789 g_mutex_unlock(image_loader_prio_mutex);
792 static void image_loader_thread_leave_high(void)
794 g_mutex_lock(image_loader_prio_mutex);
795 image_loader_prio_num--;
796 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
797 g_mutex_unlock(image_loader_prio_mutex);
800 static void image_loader_thread_wait_high(void)
802 g_mutex_lock(image_loader_prio_mutex);
803 while (image_loader_prio_num)
805 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
808 g_mutex_unlock(image_loader_prio_mutex);
812 static void image_loader_thread_run(gpointer data, gpointer user_data)
814 ImageLoader *il = data;
817 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
819 /* low prio, wait untill high prio tasks finishes */
820 image_loader_thread_wait_high();
825 image_loader_thread_enter_high();
828 cont = image_loader_begin(il);
830 if (!cont && !image_loader_get_pixbuf(il))
833 loader failed, we have to send signal
834 (idle mode returns the image_loader_begin return value directly)
835 (success is always reported indirectly from image_loader_begin)
837 image_loader_emit_error(il);
840 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
842 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
844 /* low prio, wait untill high prio tasks finishes */
845 image_loader_thread_wait_high();
847 cont = image_loader_continue(il);
849 image_loader_stop_loader(il);
851 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
854 image_loader_thread_leave_high();
857 g_mutex_lock(il->data_mutex);
858 il->can_destroy = TRUE;
859 g_cond_signal(il->can_destroy_cond);
860 g_mutex_unlock(il->data_mutex);
865 static gboolean image_loader_start_thread(ImageLoader *il)
867 if (!il) return FALSE;
869 if (!il->fd) return FALSE;
873 if (!image_loader_setup_source(il)) return FALSE;
875 if (!image_loader_thread_pool)
877 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
878 image_loader_prio_cond = g_cond_new();
879 image_loader_prio_mutex = g_mutex_new();
882 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
884 g_thread_pool_push(image_loader_thread_pool, il, NULL);
885 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
891 /**************************************************************************************/
892 /* public interface */
895 gboolean image_loader_start(ImageLoader *il)
897 if (!il) return FALSE;
899 if (!il->fd) return FALSE;
902 return image_loader_start_thread(il);
904 return image_loader_start_idle(il);
909 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
910 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
913 if (!il) return NULL;
915 g_mutex_lock(il->data_mutex);
917 g_mutex_unlock(il->data_mutex);
921 gchar *image_loader_get_format(ImageLoader *il)
923 GdkPixbufFormat *format;
927 if (!il || !il->loader) return NULL;
929 format = gdk_pixbuf_loader_get_format(il->loader);
930 if (!format) return NULL;
932 mimev = gdk_pixbuf_format_get_mime_types(format);
933 if (!mimev) return NULL;
935 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
936 mime = g_strdup(mimev[0]);
942 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
946 g_mutex_lock(il->data_mutex);
947 il->requested_width = width;
948 il->requested_height = height;
949 g_mutex_unlock(il->data_mutex);
952 void image_loader_set_buffer_size(ImageLoader *il, guint count)
956 g_mutex_lock(il->data_mutex);
957 il->idle_read_loop_count = count ? count : 1;
958 g_mutex_unlock(il->data_mutex);
961 void image_loader_set_priority(ImageLoader *il, gint priority)
965 if (il->thread) return; /* can't change prio if the thread already runs */
966 il->idle_priority = priority;
970 gdouble image_loader_get_percent(ImageLoader *il)
975 g_mutex_lock(il->data_mutex);
976 if (il->bytes_total == 0)
982 ret = (gdouble)il->bytes_read / il->bytes_total;
984 g_mutex_unlock(il->data_mutex);
988 gboolean image_loader_get_is_done(ImageLoader *il)
991 if (!il) return FALSE;
993 g_mutex_lock(il->data_mutex);
995 g_mutex_unlock(il->data_mutex);
1000 FileData *image_loader_get_fd(ImageLoader *il)
1003 if (!il) return NULL;
1005 g_mutex_lock(il->data_mutex);
1007 g_mutex_unlock(il->data_mutex);
1012 gboolean image_loader_get_shrunk(ImageLoader *il)
1015 if (!il) return FALSE;
1017 g_mutex_lock(il->data_mutex);
1019 g_mutex_unlock(il->data_mutex);
1023 const gchar *image_loader_get_error(ImageLoader *il)
1025 const gchar *ret = NULL;
1026 if (!il) return NULL;
1027 g_mutex_lock(il->data_mutex);
1028 if (il->error) ret = il->error->message;
1029 g_mutex_unlock(il->data_mutex);
1034 /* FIXME - this can be rather slow and blocks until the size is known */
1035 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1040 il = image_loader_new(fd);
1042 success = image_loader_start_idle(il);
1044 if (success && il->pixbuf)
1046 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1047 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1051 if (width) *width = -1;
1052 if (height) *height = -1;
1055 image_loader_free(il);
1059 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */