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"
26 /**************************************************************************************/
27 /* image loader class */
31 SIGNAL_AREA_READY = 0,
38 static guint signals[SIGNAL_COUNT] = { 0 };
40 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
41 static void image_loader_class_init(ImageLoaderClass *class);
42 static void image_loader_finalize(GObject *object);
43 static void image_loader_stop(ImageLoader *il);
45 GType image_loader_get_type(void)
47 static GType type = 0;
50 static const GTypeInfo info = {
51 sizeof(ImageLoaderClass),
53 NULL, /* base_finalize */
54 (GClassInitFunc)image_loader_class_init, /* class_init */
55 NULL, /* class_finalize */
56 NULL, /* class_data */
59 (GInstanceInitFunc)image_loader_init, /* instance_init */
60 NULL /* value_table */
62 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
67 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
69 ImageLoader *il = (ImageLoader *)instance;
73 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
80 il->idle_done_id = -1;
82 il->idle_read_loop_count = options->image.idle_read_loop_count;
83 il->read_buffer_size = options->image.read_buffer_size;
84 il->mapped_file = NULL;
86 il->requested_width = 0;
87 il->requested_height = 0;
90 il->can_destroy = TRUE;
93 il->data_mutex = g_mutex_new();
94 il->can_destroy_cond = g_cond_new();
96 DEBUG_1("new image loader %p, bufsize=%u idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
99 static void image_loader_class_init(ImageLoaderClass *class)
101 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
103 // gobject_class->set_property = image_loader_set_property;
104 // gobject_class->get_property = image_loader_get_property;
106 gobject_class->finalize = image_loader_finalize;
109 signals[SIGNAL_AREA_READY] =
110 g_signal_new("area_ready",
111 G_OBJECT_CLASS_TYPE(gobject_class),
113 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
115 gq_marshal_VOID__INT_INT_INT_INT,
122 signals[SIGNAL_ERROR] =
123 g_signal_new("error",
124 G_OBJECT_CLASS_TYPE(gobject_class),
126 G_STRUCT_OFFSET(ImageLoaderClass, error),
128 g_cclosure_marshal_VOID__VOID,
131 signals[SIGNAL_DONE] =
133 G_OBJECT_CLASS_TYPE(gobject_class),
135 G_STRUCT_OFFSET(ImageLoaderClass, done),
137 g_cclosure_marshal_VOID__VOID,
140 signals[SIGNAL_PERCENT] =
141 g_signal_new("percent",
142 G_OBJECT_CLASS_TYPE(gobject_class),
144 G_STRUCT_OFFSET(ImageLoaderClass, percent),
146 g_cclosure_marshal_VOID__DOUBLE,
152 static void image_loader_finalize(GObject *object)
154 ImageLoader *il = (ImageLoader *)object;
156 image_loader_stop(il);
158 DEBUG_1("freeing image loader %p bytes_read=%d", il, il->bytes_read);
160 if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
162 while (g_source_remove_by_user_data(il))
164 DEBUG_1("pending signals detected");
167 while (il->area_param_list)
169 DEBUG_1("pending area_ready signals detected");
170 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
171 g_free(il->area_param_list->data);
172 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
175 while (il->area_param_delayed_list)
177 g_free(il->area_param_delayed_list->data);
178 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
181 if (il->pixbuf) g_object_unref(il->pixbuf);
183 file_data_unref(il->fd);
185 g_mutex_free(il->data_mutex);
186 g_cond_free(il->can_destroy_cond);
190 void image_loader_free(ImageLoader *il)
193 g_object_unref(G_OBJECT(il));
197 ImageLoader *image_loader_new(FileData *fd)
201 if (!fd) return NULL;
203 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
205 il->fd = file_data_ref(fd);
210 /**************************************************************************************/
211 /* send signals via idle calbacks - the callback are executed in the main thread */
213 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
214 struct _ImageLoaderAreaParam {
223 static gboolean image_loader_emit_area_ready_cb(gpointer data)
225 ImageLoaderAreaParam *par = data;
226 ImageLoader *il = par->il;
227 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
228 g_mutex_lock(il->data_mutex);
229 il->area_param_list = g_list_remove(il->area_param_list, par);
231 g_mutex_unlock(il->data_mutex);
236 static gboolean image_loader_emit_done_cb(gpointer data)
238 ImageLoader *il = data;
239 g_signal_emit(il, signals[SIGNAL_DONE], 0);
243 static gboolean image_loader_emit_error_cb(gpointer data)
245 ImageLoader *il = data;
246 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
250 static gboolean image_loader_emit_percent_cb(gpointer data)
252 ImageLoader *il = data;
253 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
257 /* DONE and ERROR are emited only once, thus they can have normal priority
258 PERCENT and AREA_READY should be processed ASAP
261 static void image_loader_emit_done(ImageLoader *il)
263 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
266 static void image_loader_emit_error(ImageLoader *il)
268 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
271 static void image_loader_emit_percent(ImageLoader *il)
273 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
276 /* this function expects that il->data_mutex is locked by caller */
277 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
279 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
286 il->area_param_list = g_list_prepend(il->area_param_list, par);
288 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
291 /**************************************************************************************/
292 /* the following functions may be executed in separate thread */
294 /* this function expects that il->data_mutex is locked by caller */
295 static void image_loader_queue_delayed_erea_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
297 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
304 il->area_param_delayed_list = g_list_prepend(il->area_param_delayed_list, par);
309 static gboolean image_loader_get_stopping(ImageLoader *il)
312 if (!il) return FALSE;
314 g_mutex_lock(il->data_mutex);
316 g_mutex_unlock(il->data_mutex);
322 static void image_loader_sync_pixbuf(ImageLoader *il)
326 g_mutex_lock(il->data_mutex);
330 g_mutex_unlock(il->data_mutex);
334 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
336 if (pb == il->pixbuf)
338 g_mutex_unlock(il->data_mutex);
342 if (il->pixbuf) g_object_unref(il->pixbuf);
345 if (il->pixbuf) g_object_ref(il->pixbuf);
347 g_mutex_unlock(il->data_mutex);
350 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
351 guint x, guint y, guint w, guint h,
354 ImageLoader *il = data;
356 if (!image_loader_get_pixbuf(il))
358 image_loader_sync_pixbuf(il);
359 if (!image_loader_get_pixbuf(il))
361 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
365 g_mutex_lock(il->data_mutex);
366 if (il->delay_area_ready)
367 image_loader_queue_delayed_erea_ready(il, x, y, w, h);
369 image_loader_emit_area_ready(il, x, y, w, h);
370 g_mutex_unlock(il->data_mutex);
373 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
379 /* a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669 */
380 gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
381 if (strcmp(format, "svg") == 0)
389 pb = gdk_pixbuf_loader_get_pixbuf(loader);
391 h = gdk_pixbuf_get_height(pb);
392 rs = gdk_pixbuf_get_rowstride(pb);
393 pix = gdk_pixbuf_get_pixels(pb);
395 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
399 static void image_loader_size_cb(GdkPixbufLoader *loader,
400 gint width, gint height, gpointer data)
402 ImageLoader *il = data;
403 GdkPixbufFormat *format;
405 gboolean scale = FALSE;
408 g_mutex_lock(il->data_mutex);
409 if (il->requested_width < 1 || il->requested_height < 1)
411 g_mutex_unlock(il->data_mutex);
414 g_mutex_unlock(il->data_mutex);
416 format = gdk_pixbuf_loader_get_format(loader);
419 mime_types = gdk_pixbuf_format_get_mime_types(format);
421 while (mime_types[n])
423 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
426 g_strfreev(mime_types);
430 g_mutex_lock(il->data_mutex);
432 if (width > il->requested_width || height > il->requested_height)
436 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
438 nw = il->requested_width;
439 nh = (gdouble)nw / width * height;
444 nh = il->requested_height;
445 nw = (gdouble)nh / height * width;
449 gdk_pixbuf_loader_set_size(loader, nw, nh);
452 g_mutex_unlock(il->data_mutex);
456 static void image_loader_stop_loader(ImageLoader *il)
462 /* some loaders do not have a pixbuf till close, order is important here */
463 gdk_pixbuf_loader_close(il->loader, NULL);
464 image_loader_sync_pixbuf(il);
465 g_object_unref(G_OBJECT(il->loader));
468 g_mutex_lock(il->data_mutex);
470 g_mutex_unlock(il->data_mutex);
473 static void image_loader_setup_loader(ImageLoader *il)
475 g_mutex_lock(il->data_mutex);
476 il->loader = gdk_pixbuf_loader_new();
478 g_signal_connect(G_OBJECT(il->loader), "area_updated",
479 G_CALLBACK(image_loader_area_updated_cb), il);
480 g_signal_connect(G_OBJECT(il->loader), "size_prepared",
481 G_CALLBACK(image_loader_size_cb), il);
482 g_signal_connect(G_OBJECT(il->loader), "area_prepared",
483 G_CALLBACK(image_loader_area_prepared_cb), il);
484 g_mutex_unlock(il->data_mutex);
488 static void image_loader_done(ImageLoader *il)
490 image_loader_stop_loader(il);
492 image_loader_emit_done(il);
495 static void image_loader_error(ImageLoader *il)
497 image_loader_stop_loader(il);
499 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
501 image_loader_emit_error(il);
504 static gboolean image_loader_continue(ImageLoader *il)
509 if (!il) return FALSE;
511 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
512 while (c > 0 && !image_loader_get_stopping(il))
514 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
518 image_loader_done(il);
522 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
524 image_loader_error(il);
533 if (il->bytes_total > 0)
535 image_loader_emit_percent(il);
541 static gboolean image_loader_begin(ImageLoader *il)
545 if (il->pixbuf) return FALSE;
547 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
548 if (b < 1) return FALSE;
550 image_loader_setup_loader(il);
552 if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL))
554 image_loader_stop_loader(il);
560 /* read until size is known */
561 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
563 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
564 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
566 image_loader_stop_loader(il);
571 if (!il->pixbuf) image_loader_sync_pixbuf(il);
573 if (il->bytes_read == il->bytes_total || b < 1)
575 /* done, handle (broken) loaders that do not have pixbuf till close */
576 image_loader_stop_loader(il);
578 if (!il->pixbuf) return FALSE;
580 image_loader_done(il);
586 image_loader_stop_loader(il);
593 /**************************************************************************************/
594 /* the following functions are always executed in the main thread */
597 static gboolean image_loader_setup_source(ImageLoader *il)
602 if (!il || il->loader || il->mapped_file) return FALSE;
604 il->mapped_file = NULL;
608 ExifData *exif = exif_read_fd(il->fd);
610 if (options->thumbnails.use_exif)
611 il->mapped_file = exif_get_preview(exif, &il->bytes_total, il->requested_width, il->requested_height);
613 il->mapped_file = exif_get_preview(exif, &il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
618 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
620 exif_free_fd(il->fd, exif);
624 if (!il->mapped_file)
629 pathl = path_from_utf8(il->fd->path);
630 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
632 if (load_fd == -1) return FALSE;
634 if (fstat(load_fd, &st) == 0)
636 il->bytes_total = st.st_size;
644 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
646 if (il->mapped_file == MAP_FAILED)
657 static void image_loader_stop_source(ImageLoader *il)
665 exif_free_preview(il->mapped_file);
669 munmap(il->mapped_file, il->bytes_total);
671 il->mapped_file = NULL;
675 static void image_loader_stop(ImageLoader *il)
679 if (il->idle_id != -1)
681 g_source_remove(il->idle_id);
687 /* stop loader in the other thread */
688 g_mutex_lock(il->data_mutex);
690 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
691 g_mutex_unlock(il->data_mutex);
694 image_loader_stop_loader(il);
695 image_loader_stop_source(il);
699 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
701 g_mutex_lock(il->data_mutex);
702 il->delay_area_ready = enable;
707 list = g_list_reverse(il->area_param_delayed_list);
708 il->area_param_delayed_list = NULL;
709 g_mutex_unlock(il->data_mutex);
715 ImageLoaderAreaParam *par = work->data;
718 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
726 g_mutex_unlock(il->data_mutex);
731 /**************************************************************************************/
732 /* execution via idle calls */
734 static gboolean image_loader_idle_cb(gpointer data)
736 gboolean ret = FALSE;
737 ImageLoader *il = data;
739 if (il->idle_id != -1)
741 ret = image_loader_continue(il);
746 image_loader_stop_source(il);
753 static gboolean image_loader_start_idle(ImageLoader *il)
757 if (!il) return FALSE;
759 if (!il->fd) return FALSE;
761 if (!image_loader_setup_source(il)) return FALSE;
763 ret = image_loader_begin(il);
765 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
769 /**************************************************************************************/
770 /* execution via thread */
771 static GThreadPool *image_loader_thread_pool = NULL;
773 static GCond *image_loader_prio_cond = NULL;
774 static GMutex *image_loader_prio_mutex = NULL;
775 static gint image_loader_prio_num = 0;
778 static void image_loader_thread_enter_high(void)
780 g_mutex_lock(image_loader_prio_mutex);
781 image_loader_prio_num++;
782 g_mutex_unlock(image_loader_prio_mutex);
785 static void image_loader_thread_leave_high(void)
787 g_mutex_lock(image_loader_prio_mutex);
788 image_loader_prio_num--;
789 if (image_loader_prio_num == 0) g_cond_signal(image_loader_prio_cond);
790 g_mutex_unlock(image_loader_prio_mutex);
793 static void image_loader_thread_wait_high(void)
795 g_mutex_lock(image_loader_prio_mutex);
796 while (image_loader_prio_num)
798 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
801 g_mutex_unlock(image_loader_prio_mutex);
805 static void image_loader_thread_run(gpointer data, gpointer user_data)
807 ImageLoader *il = data;
810 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
812 /* low prio, wait untill high prio tasks finishes */
813 image_loader_thread_wait_high();
818 image_loader_thread_enter_high();
821 cont = image_loader_begin(il);
823 if (!cont && !image_loader_get_pixbuf(il))
826 loader failed, we have to send signal
827 (idle mode returns the image_loader_begin return value directly)
828 (success is always reported indirectly from image_loader_begin)
830 image_loader_emit_error(il);
833 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
835 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
837 /* low prio, wait untill high prio tasks finishes */
838 image_loader_thread_wait_high();
840 cont = image_loader_continue(il);
842 image_loader_stop_loader(il);
844 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
847 image_loader_thread_leave_high();
850 g_mutex_lock(il->data_mutex);
851 il->can_destroy = TRUE;
852 g_cond_signal(il->can_destroy_cond);
853 g_mutex_unlock(il->data_mutex);
858 static gboolean image_loader_start_thread(ImageLoader *il)
860 if (!il) return FALSE;
862 if (!il->fd) return FALSE;
866 if (!image_loader_setup_source(il)) return FALSE;
868 if (!image_loader_thread_pool)
870 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
871 image_loader_prio_cond = g_cond_new();
872 image_loader_prio_mutex = g_mutex_new();
875 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
877 g_thread_pool_push(image_loader_thread_pool, il, NULL);
878 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
884 /**************************************************************************************/
885 /* public interface */
888 gboolean image_loader_start(ImageLoader *il)
890 if (!il) return FALSE;
892 if (!il->fd) return FALSE;
895 return image_loader_start_thread(il);
897 return image_loader_start_idle(il);
902 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
903 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
906 if (!il) return NULL;
908 g_mutex_lock(il->data_mutex);
910 g_mutex_unlock(il->data_mutex);
914 gchar *image_loader_get_format(ImageLoader *il)
916 GdkPixbufFormat *format;
920 if (!il || !il->loader) return NULL;
922 format = gdk_pixbuf_loader_get_format(il->loader);
923 if (!format) return NULL;
925 mimev = gdk_pixbuf_format_get_mime_types(format);
926 if (!mimev) return NULL;
928 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
929 mime = g_strdup(mimev[0]);
935 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
939 g_mutex_lock(il->data_mutex);
940 il->requested_width = width;
941 il->requested_height = height;
942 g_mutex_unlock(il->data_mutex);
945 void image_loader_set_buffer_size(ImageLoader *il, guint count)
949 g_mutex_lock(il->data_mutex);
950 il->idle_read_loop_count = count ? count : 1;
951 g_mutex_unlock(il->data_mutex);
954 void image_loader_set_priority(ImageLoader *il, gint priority)
958 if (il->thread) return; /* can't change prio if the thread already runs */
959 il->idle_priority = priority;
963 gdouble image_loader_get_percent(ImageLoader *il)
968 g_mutex_lock(il->data_mutex);
969 if (il->bytes_total == 0)
975 ret = (gdouble)il->bytes_read / il->bytes_total;
977 g_mutex_unlock(il->data_mutex);
981 gboolean image_loader_get_is_done(ImageLoader *il)
984 if (!il) return FALSE;
986 g_mutex_lock(il->data_mutex);
988 g_mutex_unlock(il->data_mutex);
993 FileData *image_loader_get_fd(ImageLoader *il)
996 if (!il) return NULL;
998 g_mutex_lock(il->data_mutex);
1000 g_mutex_unlock(il->data_mutex);
1005 gboolean image_loader_get_shrunk(ImageLoader *il)
1008 if (!il) return FALSE;
1010 g_mutex_lock(il->data_mutex);
1012 g_mutex_unlock(il->data_mutex);
1017 /* FIXME - this can be rather slow and blocks until the size is known */
1018 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1023 il = image_loader_new(fd);
1025 success = image_loader_start_idle(il);
1027 if (success && il->pixbuf)
1029 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1030 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1034 if (width) *width = -1;
1035 if (height) *height = -1;
1038 image_loader_free(il);
1042 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */