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;
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=%" G_GSIZE_FORMAT " 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=%" G_GSIZE_FORMAT, il, il->bytes_read);
165 if (il->idle_done_id)
167 g_source_remove(il->idle_done_id);
168 il->idle_done_id = 0;
171 while (g_source_remove_by_user_data(il))
173 DEBUG_2("pending signals detected");
176 while (il->area_param_list)
178 DEBUG_1("pending area_ready signals detected");
179 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
180 g_free(il->area_param_list->data);
181 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
184 while (il->area_param_delayed_list)
186 g_free(il->area_param_delayed_list->data);
187 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
190 if (il->pixbuf) g_object_unref(il->pixbuf);
192 if (il->error) g_error_free(il->error);
194 file_data_unref(il->fd);
196 g_mutex_free(il->data_mutex);
197 g_cond_free(il->can_destroy_cond);
201 void image_loader_free(ImageLoader *il)
204 g_object_unref(G_OBJECT(il));
208 ImageLoader *image_loader_new(FileData *fd)
212 if (!fd) return NULL;
214 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
216 il->fd = file_data_ref(fd);
221 /**************************************************************************************/
222 /* send signals via idle calbacks - the callback are executed in the main thread */
224 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
225 struct _ImageLoaderAreaParam {
234 static gboolean image_loader_emit_area_ready_cb(gpointer data)
236 ImageLoaderAreaParam *par = data;
237 ImageLoader *il = par->il;
238 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
239 g_mutex_lock(il->data_mutex);
240 il->area_param_list = g_list_remove(il->area_param_list, par);
242 g_mutex_unlock(il->data_mutex);
247 static gboolean image_loader_emit_done_cb(gpointer data)
249 ImageLoader *il = data;
250 g_signal_emit(il, signals[SIGNAL_DONE], 0);
254 static gboolean image_loader_emit_error_cb(gpointer data)
256 ImageLoader *il = data;
257 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
261 static gboolean image_loader_emit_percent_cb(gpointer data)
263 ImageLoader *il = data;
264 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
268 /* DONE and ERROR are emited only once, thus they can have normal priority
269 PERCENT and AREA_READY should be processed ASAP
272 static void image_loader_emit_done(ImageLoader *il)
274 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
277 static void image_loader_emit_error(ImageLoader *il)
279 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
282 static void image_loader_emit_percent(ImageLoader *il)
284 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
287 /* this function expects that il->data_mutex is locked by caller */
288 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
290 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
297 il->area_param_list = g_list_prepend(il->area_param_list, par);
299 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
302 /**************************************************************************************/
303 /* the following functions may be executed in separate thread */
305 /* this function expects that il->data_mutex is locked by caller */
306 static void image_loader_queue_delayed_erea_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
308 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
315 il->area_param_delayed_list = g_list_prepend(il->area_param_delayed_list, par);
320 static gboolean image_loader_get_stopping(ImageLoader *il)
323 if (!il) return FALSE;
325 g_mutex_lock(il->data_mutex);
327 g_mutex_unlock(il->data_mutex);
333 static void image_loader_sync_pixbuf(ImageLoader *il)
337 g_mutex_lock(il->data_mutex);
341 g_mutex_unlock(il->data_mutex);
345 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
347 if (pb == il->pixbuf)
349 g_mutex_unlock(il->data_mutex);
353 if (il->pixbuf) g_object_unref(il->pixbuf);
356 if (il->pixbuf) g_object_ref(il->pixbuf);
358 g_mutex_unlock(il->data_mutex);
361 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
362 guint x, guint y, guint w, guint h,
365 ImageLoader *il = data;
367 if (!image_loader_get_pixbuf(il))
369 image_loader_sync_pixbuf(il);
370 if (!image_loader_get_pixbuf(il))
372 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
376 g_mutex_lock(il->data_mutex);
377 if (il->delay_area_ready)
378 image_loader_queue_delayed_erea_ready(il, x, y, w, h);
380 image_loader_emit_area_ready(il, x, y, w, h);
381 g_mutex_unlock(il->data_mutex);
384 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
391 http://bugzilla.gnome.org/show_bug.cgi?id=547669
392 http://bugzilla.gnome.org/show_bug.cgi?id=589334
394 gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
395 if (strcmp(format, "svg") == 0 ||
396 strcmp(format, "xpm") == 0)
404 pb = gdk_pixbuf_loader_get_pixbuf(loader);
406 h = gdk_pixbuf_get_height(pb);
407 rs = gdk_pixbuf_get_rowstride(pb);
408 pix = gdk_pixbuf_get_pixels(pb);
410 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
414 static void image_loader_size_cb(GdkPixbufLoader *loader,
415 gint width, gint height, gpointer data)
417 ImageLoader *il = data;
418 GdkPixbufFormat *format;
420 gboolean scale = FALSE;
423 g_mutex_lock(il->data_mutex);
424 if (il->requested_width < 1 || il->requested_height < 1)
426 g_mutex_unlock(il->data_mutex);
429 g_mutex_unlock(il->data_mutex);
431 format = gdk_pixbuf_loader_get_format(loader);
434 mime_types = gdk_pixbuf_format_get_mime_types(format);
436 while (mime_types[n])
438 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
441 g_strfreev(mime_types);
445 g_mutex_lock(il->data_mutex);
447 if (width > il->requested_width || height > il->requested_height)
451 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
453 nw = il->requested_width;
454 nh = (gdouble)nw / width * height;
459 nh = il->requested_height;
460 nw = (gdouble)nh / height * width;
464 gdk_pixbuf_loader_set_size(loader, nw, nh);
467 g_mutex_unlock(il->data_mutex);
471 static void image_loader_stop_loader(ImageLoader *il)
477 /* some loaders do not have a pixbuf till close, order is important here */
478 gdk_pixbuf_loader_close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
479 image_loader_sync_pixbuf(il);
480 g_object_unref(G_OBJECT(il->loader));
483 g_mutex_lock(il->data_mutex);
485 g_mutex_unlock(il->data_mutex);
488 static void image_loader_setup_loader(ImageLoader *il)
490 g_mutex_lock(il->data_mutex);
491 il->loader = gdk_pixbuf_loader_new();
493 g_signal_connect(G_OBJECT(il->loader), "area_updated",
494 G_CALLBACK(image_loader_area_updated_cb), il);
495 g_signal_connect(G_OBJECT(il->loader), "size_prepared",
496 G_CALLBACK(image_loader_size_cb), il);
497 g_signal_connect(G_OBJECT(il->loader), "area_prepared",
498 G_CALLBACK(image_loader_area_prepared_cb), il);
499 g_mutex_unlock(il->data_mutex);
503 static void image_loader_done(ImageLoader *il)
505 image_loader_stop_loader(il);
507 image_loader_emit_done(il);
510 static void image_loader_error(ImageLoader *il)
512 image_loader_stop_loader(il);
514 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
516 image_loader_emit_error(il);
519 static gboolean image_loader_continue(ImageLoader *il)
524 if (!il) return FALSE;
526 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
527 while (c > 0 && !image_loader_get_stopping(il))
529 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
533 image_loader_done(il);
537 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
539 image_loader_error(il);
548 if (il->bytes_total > 0)
550 image_loader_emit_percent(il);
556 static gboolean image_loader_begin(ImageLoader *il)
560 if (il->pixbuf) return FALSE;
562 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
563 if (b < 1) return FALSE;
565 image_loader_setup_loader(il);
567 if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error))
569 image_loader_stop_loader(il);
575 /* read until size is known */
576 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
578 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
579 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
581 image_loader_stop_loader(il);
586 if (!il->pixbuf) image_loader_sync_pixbuf(il);
588 if (il->bytes_read == il->bytes_total || b < 1)
590 /* done, handle (broken) loaders that do not have pixbuf till close */
591 image_loader_stop_loader(il);
593 if (!il->pixbuf) return FALSE;
595 image_loader_done(il);
601 image_loader_stop_loader(il);
608 /**************************************************************************************/
609 /* the following functions are always executed in the main thread */
612 static gboolean image_loader_setup_source(ImageLoader *il)
617 if (!il || il->loader || il->mapped_file) return FALSE;
619 il->mapped_file = NULL;
623 ExifData *exif = exif_read_fd(il->fd);
625 if (options->thumbnails.use_exif)
626 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
628 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
633 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
635 exif_free_fd(il->fd, exif);
639 if (!il->mapped_file)
644 pathl = path_from_utf8(il->fd->path);
645 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
647 if (load_fd == -1) return FALSE;
649 if (fstat(load_fd, &st) == 0)
651 il->bytes_total = st.st_size;
659 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
661 if (il->mapped_file == MAP_FAILED)
672 static void image_loader_stop_source(ImageLoader *il)
680 exif_free_preview(il->mapped_file);
684 munmap(il->mapped_file, il->bytes_total);
686 il->mapped_file = NULL;
690 static void image_loader_stop(ImageLoader *il)
696 g_source_remove(il->idle_id);
702 /* stop loader in the other thread */
703 g_mutex_lock(il->data_mutex);
705 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
706 g_mutex_unlock(il->data_mutex);
709 image_loader_stop_loader(il);
710 image_loader_stop_source(il);
714 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
716 g_mutex_lock(il->data_mutex);
717 il->delay_area_ready = enable;
722 list = g_list_reverse(il->area_param_delayed_list);
723 il->area_param_delayed_list = NULL;
724 g_mutex_unlock(il->data_mutex);
730 ImageLoaderAreaParam *par = work->data;
733 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
741 g_mutex_unlock(il->data_mutex);
746 /**************************************************************************************/
747 /* execution via idle calls */
749 static gboolean image_loader_idle_cb(gpointer data)
751 gboolean ret = FALSE;
752 ImageLoader *il = data;
756 ret = image_loader_continue(il);
761 image_loader_stop_source(il);
768 static gboolean image_loader_start_idle(ImageLoader *il)
772 if (!il) return FALSE;
774 if (!il->fd) return FALSE;
776 if (!image_loader_setup_source(il)) return FALSE;
778 ret = image_loader_begin(il);
780 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
784 /**************************************************************************************/
785 /* execution via thread */
788 static GThreadPool *image_loader_thread_pool = NULL;
790 static GCond *image_loader_prio_cond = NULL;
791 static GMutex *image_loader_prio_mutex = NULL;
792 static gint image_loader_prio_num = 0;
795 static void image_loader_thread_enter_high(void)
797 g_mutex_lock(image_loader_prio_mutex);
798 image_loader_prio_num++;
799 g_mutex_unlock(image_loader_prio_mutex);
802 static void image_loader_thread_leave_high(void)
804 g_mutex_lock(image_loader_prio_mutex);
805 image_loader_prio_num--;
806 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
807 g_mutex_unlock(image_loader_prio_mutex);
810 static void image_loader_thread_wait_high(void)
812 g_mutex_lock(image_loader_prio_mutex);
813 while (image_loader_prio_num)
815 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
818 g_mutex_unlock(image_loader_prio_mutex);
822 static void image_loader_thread_run(gpointer data, gpointer user_data)
824 ImageLoader *il = data;
828 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
830 /* low prio, wait untill high prio tasks finishes */
831 image_loader_thread_wait_high();
836 image_loader_thread_enter_high();
839 err = !image_loader_begin(il);
844 loader failed, we have to send signal
845 (idle mode returns the image_loader_begin return value directly)
846 (success is always reported indirectly from image_loader_begin)
848 image_loader_emit_error(il);
853 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
855 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
857 /* low prio, wait untill high prio tasks finishes */
858 image_loader_thread_wait_high();
860 cont = image_loader_continue(il);
862 image_loader_stop_loader(il);
864 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
867 image_loader_thread_leave_high();
870 g_mutex_lock(il->data_mutex);
871 il->can_destroy = TRUE;
872 g_cond_signal(il->can_destroy_cond);
873 g_mutex_unlock(il->data_mutex);
878 static gboolean image_loader_start_thread(ImageLoader *il)
880 if (!il) return FALSE;
882 if (!il->fd) return FALSE;
886 if (!image_loader_setup_source(il)) return FALSE;
888 if (!image_loader_thread_pool)
890 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
891 image_loader_prio_cond = g_cond_new();
892 image_loader_prio_mutex = g_mutex_new();
895 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
897 g_thread_pool_push(image_loader_thread_pool, il, NULL);
898 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
902 #endif /* HAVE_GTHREAD */
905 /**************************************************************************************/
906 /* public interface */
909 gboolean image_loader_start(ImageLoader *il)
911 if (!il) return FALSE;
913 if (!il->fd) return FALSE;
916 return image_loader_start_thread(il);
918 return image_loader_start_idle(il);
923 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
924 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
927 if (!il) return NULL;
929 g_mutex_lock(il->data_mutex);
931 g_mutex_unlock(il->data_mutex);
935 gchar *image_loader_get_format(ImageLoader *il)
937 GdkPixbufFormat *format;
941 if (!il || !il->loader) return NULL;
943 format = gdk_pixbuf_loader_get_format(il->loader);
944 if (!format) return NULL;
946 mimev = gdk_pixbuf_format_get_mime_types(format);
947 if (!mimev) return NULL;
949 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
950 mime = g_strdup(mimev[0]);
956 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
960 g_mutex_lock(il->data_mutex);
961 il->requested_width = width;
962 il->requested_height = height;
963 g_mutex_unlock(il->data_mutex);
966 void image_loader_set_buffer_size(ImageLoader *il, guint count)
970 g_mutex_lock(il->data_mutex);
971 il->idle_read_loop_count = count ? count : 1;
972 g_mutex_unlock(il->data_mutex);
975 void image_loader_set_priority(ImageLoader *il, gint priority)
979 if (il->thread) return; /* can't change prio if the thread already runs */
980 il->idle_priority = priority;
984 gdouble image_loader_get_percent(ImageLoader *il)
989 g_mutex_lock(il->data_mutex);
990 if (il->bytes_total == 0)
996 ret = (gdouble)il->bytes_read / il->bytes_total;
998 g_mutex_unlock(il->data_mutex);
1002 gboolean image_loader_get_is_done(ImageLoader *il)
1005 if (!il) return FALSE;
1007 g_mutex_lock(il->data_mutex);
1009 g_mutex_unlock(il->data_mutex);
1014 FileData *image_loader_get_fd(ImageLoader *il)
1017 if (!il) return NULL;
1019 g_mutex_lock(il->data_mutex);
1021 g_mutex_unlock(il->data_mutex);
1026 gboolean image_loader_get_shrunk(ImageLoader *il)
1029 if (!il) return FALSE;
1031 g_mutex_lock(il->data_mutex);
1033 g_mutex_unlock(il->data_mutex);
1037 const gchar *image_loader_get_error(ImageLoader *il)
1039 const gchar *ret = NULL;
1040 if (!il) return NULL;
1041 g_mutex_lock(il->data_mutex);
1042 if (il->error) ret = il->error->message;
1043 g_mutex_unlock(il->data_mutex);
1048 /* FIXME - this can be rather slow and blocks until the size is known */
1049 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1054 il = image_loader_new(fd);
1056 success = image_loader_start_idle(il);
1058 if (success && il->pixbuf)
1060 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1061 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1065 if (width) *width = -1;
1066 if (height) *height = -1;
1069 image_loader_free(il);
1073 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */