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 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
160 DEBUG_1("freeing image loader %p bytes_read=%d", il, il->bytes_read);
162 if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
164 while (g_source_remove_by_user_data(il))
166 DEBUG_2("pending signals detected");
169 while (il->area_param_list)
171 DEBUG_1("pending area_ready signals detected");
172 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
173 g_free(il->area_param_list->data);
174 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
177 while (il->area_param_delayed_list)
179 g_free(il->area_param_delayed_list->data);
180 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
183 if (il->pixbuf) g_object_unref(il->pixbuf);
185 if (il->error) g_error_free(il->error);
187 file_data_unref(il->fd);
189 g_mutex_free(il->data_mutex);
190 g_cond_free(il->can_destroy_cond);
194 void image_loader_free(ImageLoader *il)
197 g_object_unref(G_OBJECT(il));
201 ImageLoader *image_loader_new(FileData *fd)
205 if (!fd) return NULL;
207 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
209 il->fd = file_data_ref(fd);
214 /**************************************************************************************/
215 /* send signals via idle calbacks - the callback are executed in the main thread */
217 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
218 struct _ImageLoaderAreaParam {
227 static gboolean image_loader_emit_area_ready_cb(gpointer data)
229 ImageLoaderAreaParam *par = data;
230 ImageLoader *il = par->il;
231 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
232 g_mutex_lock(il->data_mutex);
233 il->area_param_list = g_list_remove(il->area_param_list, par);
235 g_mutex_unlock(il->data_mutex);
240 static gboolean image_loader_emit_done_cb(gpointer data)
242 ImageLoader *il = data;
243 g_signal_emit(il, signals[SIGNAL_DONE], 0);
247 static gboolean image_loader_emit_error_cb(gpointer data)
249 ImageLoader *il = data;
250 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
254 static gboolean image_loader_emit_percent_cb(gpointer data)
256 ImageLoader *il = data;
257 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
261 /* DONE and ERROR are emited only once, thus they can have normal priority
262 PERCENT and AREA_READY should be processed ASAP
265 static void image_loader_emit_done(ImageLoader *il)
267 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
270 static void image_loader_emit_error(ImageLoader *il)
272 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
275 static void image_loader_emit_percent(ImageLoader *il)
277 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
280 /* this function expects that il->data_mutex is locked by caller */
281 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
283 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
290 il->area_param_list = g_list_prepend(il->area_param_list, par);
292 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
295 /**************************************************************************************/
296 /* the following functions may be executed in separate thread */
298 /* this function expects that il->data_mutex is locked by caller */
299 static void image_loader_queue_delayed_erea_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
301 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
308 il->area_param_delayed_list = g_list_prepend(il->area_param_delayed_list, par);
313 static gboolean image_loader_get_stopping(ImageLoader *il)
316 if (!il) return FALSE;
318 g_mutex_lock(il->data_mutex);
320 g_mutex_unlock(il->data_mutex);
326 static void image_loader_sync_pixbuf(ImageLoader *il)
330 g_mutex_lock(il->data_mutex);
334 g_mutex_unlock(il->data_mutex);
338 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
340 if (pb == il->pixbuf)
342 g_mutex_unlock(il->data_mutex);
346 if (il->pixbuf) g_object_unref(il->pixbuf);
349 if (il->pixbuf) g_object_ref(il->pixbuf);
351 g_mutex_unlock(il->data_mutex);
354 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
355 guint x, guint y, guint w, guint h,
358 ImageLoader *il = data;
360 if (!image_loader_get_pixbuf(il))
362 image_loader_sync_pixbuf(il);
363 if (!image_loader_get_pixbuf(il))
365 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
369 g_mutex_lock(il->data_mutex);
370 if (il->delay_area_ready)
371 image_loader_queue_delayed_erea_ready(il, x, y, w, h);
373 image_loader_emit_area_ready(il, x, y, w, h);
374 g_mutex_unlock(il->data_mutex);
377 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
383 /* a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669 */
384 gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
385 if (strcmp(format, "svg") == 0)
393 pb = gdk_pixbuf_loader_get_pixbuf(loader);
395 h = gdk_pixbuf_get_height(pb);
396 rs = gdk_pixbuf_get_rowstride(pb);
397 pix = gdk_pixbuf_get_pixels(pb);
399 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
403 static void image_loader_size_cb(GdkPixbufLoader *loader,
404 gint width, gint height, gpointer data)
406 ImageLoader *il = data;
407 GdkPixbufFormat *format;
409 gboolean scale = FALSE;
412 g_mutex_lock(il->data_mutex);
413 if (il->requested_width < 1 || il->requested_height < 1)
415 g_mutex_unlock(il->data_mutex);
418 g_mutex_unlock(il->data_mutex);
420 format = gdk_pixbuf_loader_get_format(loader);
423 mime_types = gdk_pixbuf_format_get_mime_types(format);
425 while (mime_types[n])
427 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
430 g_strfreev(mime_types);
434 g_mutex_lock(il->data_mutex);
436 if (width > il->requested_width || height > il->requested_height)
440 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
442 nw = il->requested_width;
443 nh = (gdouble)nw / width * height;
448 nh = il->requested_height;
449 nw = (gdouble)nh / height * width;
453 gdk_pixbuf_loader_set_size(loader, nw, nh);
456 g_mutex_unlock(il->data_mutex);
460 static void image_loader_stop_loader(ImageLoader *il)
466 /* some loaders do not have a pixbuf till close, order is important here */
467 gdk_pixbuf_loader_close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
468 image_loader_sync_pixbuf(il);
469 g_object_unref(G_OBJECT(il->loader));
472 g_mutex_lock(il->data_mutex);
474 g_mutex_unlock(il->data_mutex);
477 static void image_loader_setup_loader(ImageLoader *il)
479 g_mutex_lock(il->data_mutex);
480 il->loader = gdk_pixbuf_loader_new();
482 g_signal_connect(G_OBJECT(il->loader), "area_updated",
483 G_CALLBACK(image_loader_area_updated_cb), il);
484 g_signal_connect(G_OBJECT(il->loader), "size_prepared",
485 G_CALLBACK(image_loader_size_cb), il);
486 g_signal_connect(G_OBJECT(il->loader), "area_prepared",
487 G_CALLBACK(image_loader_area_prepared_cb), il);
488 g_mutex_unlock(il->data_mutex);
492 static void image_loader_done(ImageLoader *il)
494 image_loader_stop_loader(il);
496 image_loader_emit_done(il);
499 static void image_loader_error(ImageLoader *il)
501 image_loader_stop_loader(il);
503 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
505 image_loader_emit_error(il);
508 static gboolean image_loader_continue(ImageLoader *il)
513 if (!il) return FALSE;
515 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
516 while (c > 0 && !image_loader_get_stopping(il))
518 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
522 image_loader_done(il);
526 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
528 image_loader_error(il);
537 if (il->bytes_total > 0)
539 image_loader_emit_percent(il);
545 static gboolean image_loader_begin(ImageLoader *il)
549 if (il->pixbuf) return FALSE;
551 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
552 if (b < 1) return FALSE;
554 image_loader_setup_loader(il);
556 if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error))
558 image_loader_stop_loader(il);
564 /* read until size is known */
565 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
567 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
568 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
570 image_loader_stop_loader(il);
575 if (!il->pixbuf) image_loader_sync_pixbuf(il);
577 if (il->bytes_read == il->bytes_total || b < 1)
579 /* done, handle (broken) loaders that do not have pixbuf till close */
580 image_loader_stop_loader(il);
582 if (!il->pixbuf) return FALSE;
584 image_loader_done(il);
590 image_loader_stop_loader(il);
597 /**************************************************************************************/
598 /* the following functions are always executed in the main thread */
601 static gboolean image_loader_setup_source(ImageLoader *il)
606 if (!il || il->loader || il->mapped_file) return FALSE;
608 il->mapped_file = NULL;
612 ExifData *exif = exif_read_fd(il->fd);
614 if (options->thumbnails.use_exif)
615 il->mapped_file = exif_get_preview(exif, &il->bytes_total, il->requested_width, il->requested_height);
617 il->mapped_file = exif_get_preview(exif, &il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
622 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
624 exif_free_fd(il->fd, exif);
628 if (!il->mapped_file)
633 pathl = path_from_utf8(il->fd->path);
634 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
636 if (load_fd == -1) return FALSE;
638 if (fstat(load_fd, &st) == 0)
640 il->bytes_total = st.st_size;
648 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
650 if (il->mapped_file == MAP_FAILED)
661 static void image_loader_stop_source(ImageLoader *il)
669 exif_free_preview(il->mapped_file);
673 munmap(il->mapped_file, il->bytes_total);
675 il->mapped_file = NULL;
679 static void image_loader_stop(ImageLoader *il)
683 if (il->idle_id != -1)
685 g_source_remove(il->idle_id);
691 /* stop loader in the other thread */
692 g_mutex_lock(il->data_mutex);
694 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
695 g_mutex_unlock(il->data_mutex);
698 image_loader_stop_loader(il);
699 image_loader_stop_source(il);
703 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
705 g_mutex_lock(il->data_mutex);
706 il->delay_area_ready = enable;
711 list = g_list_reverse(il->area_param_delayed_list);
712 il->area_param_delayed_list = NULL;
713 g_mutex_unlock(il->data_mutex);
719 ImageLoaderAreaParam *par = work->data;
722 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
730 g_mutex_unlock(il->data_mutex);
735 /**************************************************************************************/
736 /* execution via idle calls */
738 static gboolean image_loader_idle_cb(gpointer data)
740 gboolean ret = FALSE;
741 ImageLoader *il = data;
743 if (il->idle_id != -1)
745 ret = image_loader_continue(il);
750 image_loader_stop_source(il);
757 static gboolean image_loader_start_idle(ImageLoader *il)
761 if (!il) return FALSE;
763 if (!il->fd) return FALSE;
765 if (!image_loader_setup_source(il)) return FALSE;
767 ret = image_loader_begin(il);
769 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
773 /**************************************************************************************/
774 /* execution via thread */
775 static GThreadPool *image_loader_thread_pool = NULL;
777 static GCond *image_loader_prio_cond = NULL;
778 static GMutex *image_loader_prio_mutex = NULL;
779 static gint image_loader_prio_num = 0;
782 static void image_loader_thread_enter_high(void)
784 g_mutex_lock(image_loader_prio_mutex);
785 image_loader_prio_num++;
786 g_mutex_unlock(image_loader_prio_mutex);
789 static void image_loader_thread_leave_high(void)
791 g_mutex_lock(image_loader_prio_mutex);
792 image_loader_prio_num--;
793 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
794 g_mutex_unlock(image_loader_prio_mutex);
797 static void image_loader_thread_wait_high(void)
799 g_mutex_lock(image_loader_prio_mutex);
800 while (image_loader_prio_num)
802 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
805 g_mutex_unlock(image_loader_prio_mutex);
809 static void image_loader_thread_run(gpointer data, gpointer user_data)
811 ImageLoader *il = data;
814 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
816 /* low prio, wait untill high prio tasks finishes */
817 image_loader_thread_wait_high();
822 image_loader_thread_enter_high();
825 cont = image_loader_begin(il);
827 if (!cont && !image_loader_get_pixbuf(il))
830 loader failed, we have to send signal
831 (idle mode returns the image_loader_begin return value directly)
832 (success is always reported indirectly from image_loader_begin)
834 image_loader_emit_error(il);
837 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
839 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
841 /* low prio, wait untill high prio tasks finishes */
842 image_loader_thread_wait_high();
844 cont = image_loader_continue(il);
846 image_loader_stop_loader(il);
848 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
851 image_loader_thread_leave_high();
854 g_mutex_lock(il->data_mutex);
855 il->can_destroy = TRUE;
856 g_cond_signal(il->can_destroy_cond);
857 g_mutex_unlock(il->data_mutex);
862 static gboolean image_loader_start_thread(ImageLoader *il)
864 if (!il) return FALSE;
866 if (!il->fd) return FALSE;
870 if (!image_loader_setup_source(il)) return FALSE;
872 if (!image_loader_thread_pool)
874 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
875 image_loader_prio_cond = g_cond_new();
876 image_loader_prio_mutex = g_mutex_new();
879 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
881 g_thread_pool_push(image_loader_thread_pool, il, NULL);
882 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
888 /**************************************************************************************/
889 /* public interface */
892 gboolean image_loader_start(ImageLoader *il)
894 if (!il) return FALSE;
896 if (!il->fd) return FALSE;
899 return image_loader_start_thread(il);
901 return image_loader_start_idle(il);
906 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
907 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
910 if (!il) return NULL;
912 g_mutex_lock(il->data_mutex);
914 g_mutex_unlock(il->data_mutex);
918 gchar *image_loader_get_format(ImageLoader *il)
920 GdkPixbufFormat *format;
924 if (!il || !il->loader) return NULL;
926 format = gdk_pixbuf_loader_get_format(il->loader);
927 if (!format) return NULL;
929 mimev = gdk_pixbuf_format_get_mime_types(format);
930 if (!mimev) return NULL;
932 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
933 mime = g_strdup(mimev[0]);
939 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
943 g_mutex_lock(il->data_mutex);
944 il->requested_width = width;
945 il->requested_height = height;
946 g_mutex_unlock(il->data_mutex);
949 void image_loader_set_buffer_size(ImageLoader *il, guint count)
953 g_mutex_lock(il->data_mutex);
954 il->idle_read_loop_count = count ? count : 1;
955 g_mutex_unlock(il->data_mutex);
958 void image_loader_set_priority(ImageLoader *il, gint priority)
962 if (il->thread) return; /* can't change prio if the thread already runs */
963 il->idle_priority = priority;
967 gdouble image_loader_get_percent(ImageLoader *il)
972 g_mutex_lock(il->data_mutex);
973 if (il->bytes_total == 0)
979 ret = (gdouble)il->bytes_read / il->bytes_total;
981 g_mutex_unlock(il->data_mutex);
985 gboolean image_loader_get_is_done(ImageLoader *il)
988 if (!il) return FALSE;
990 g_mutex_lock(il->data_mutex);
992 g_mutex_unlock(il->data_mutex);
997 FileData *image_loader_get_fd(ImageLoader *il)
1000 if (!il) return NULL;
1002 g_mutex_lock(il->data_mutex);
1004 g_mutex_unlock(il->data_mutex);
1009 gboolean image_loader_get_shrunk(ImageLoader *il)
1012 if (!il) return FALSE;
1014 g_mutex_lock(il->data_mutex);
1016 g_mutex_unlock(il->data_mutex);
1020 const gchar *image_loader_get_error(ImageLoader *il)
1022 const gchar *ret = NULL;
1023 if (!il) return NULL;
1024 g_mutex_lock(il->data_mutex);
1025 if (il->error) ret = il->error->message;
1026 g_mutex_unlock(il->data_mutex);
1031 /* FIXME - this can be rather slow and blocks until the size is known */
1032 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1037 il = image_loader_new(fd);
1039 success = image_loader_start_idle(il);
1041 if (success && il->pixbuf)
1043 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1044 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1048 if (width) *width = -1;
1049 if (height) *height = -1;
1052 image_loader_free(il);
1056 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */