4 * Copyright (C) 2008 - 2010 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"
16 #include "image_load_gdk.h"
20 #include "ui_fileops.h"
21 #include "gq-marshal.h"
26 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
27 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
30 /**************************************************************************************/
31 /* image loader class */
35 SIGNAL_AREA_READY = 0,
43 static guint signals[SIGNAL_COUNT] = { 0 };
45 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
46 static void image_loader_class_init(ImageLoaderClass *class);
47 static void image_loader_finalize(GObject *object);
48 static void image_loader_stop(ImageLoader *il);
50 GType image_loader_get_type(void)
52 static GType type = 0;
55 static const GTypeInfo info = {
56 sizeof(ImageLoaderClass),
58 NULL, /* base_finalize */
59 (GClassInitFunc)image_loader_class_init, /* class_init */
60 NULL, /* class_finalize */
61 NULL, /* class_data */
64 (GInstanceInitFunc)image_loader_init, /* instance_init */
65 NULL /* value_table */
67 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
72 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
74 ImageLoader *il = (ImageLoader *)instance;
78 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
87 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
88 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
89 il->mapped_file = NULL;
91 il->requested_width = 0;
92 il->requested_height = 0;
95 il->can_destroy = TRUE;
98 il->data_mutex = g_mutex_new();
99 il->can_destroy_cond = g_cond_new();
101 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
104 static void image_loader_class_init(ImageLoaderClass *class)
106 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
108 // gobject_class->set_property = image_loader_set_property;
109 // gobject_class->get_property = image_loader_get_property;
111 gobject_class->finalize = image_loader_finalize;
114 signals[SIGNAL_AREA_READY] =
115 g_signal_new("area_ready",
116 G_OBJECT_CLASS_TYPE(gobject_class),
118 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
120 gq_marshal_VOID__INT_INT_INT_INT,
127 signals[SIGNAL_ERROR] =
128 g_signal_new("error",
129 G_OBJECT_CLASS_TYPE(gobject_class),
131 G_STRUCT_OFFSET(ImageLoaderClass, error),
133 g_cclosure_marshal_VOID__VOID,
136 signals[SIGNAL_DONE] =
138 G_OBJECT_CLASS_TYPE(gobject_class),
140 G_STRUCT_OFFSET(ImageLoaderClass, done),
142 g_cclosure_marshal_VOID__VOID,
145 signals[SIGNAL_PERCENT] =
146 g_signal_new("percent",
147 G_OBJECT_CLASS_TYPE(gobject_class),
149 G_STRUCT_OFFSET(ImageLoaderClass, percent),
151 g_cclosure_marshal_VOID__DOUBLE,
155 signals[SIGNAL_SIZE] =
156 g_signal_new("size_prepared",
157 G_OBJECT_CLASS_TYPE(gobject_class),
159 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
161 gq_marshal_VOID__INT_INT,
168 static void image_loader_finalize(GObject *object)
170 ImageLoader *il = (ImageLoader *)object;
172 image_loader_stop(il);
174 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
176 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
178 if (il->idle_done_id)
180 g_source_remove(il->idle_done_id);
181 il->idle_done_id = 0;
184 while (g_source_remove_by_user_data(il))
186 DEBUG_2("pending signals detected");
189 while (il->area_param_list)
191 DEBUG_1("pending area_ready signals detected");
192 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
193 g_free(il->area_param_list->data);
194 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
197 while (il->area_param_delayed_list)
199 g_free(il->area_param_delayed_list->data);
200 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
203 if (il->pixbuf) g_object_unref(il->pixbuf);
205 if (il->error) g_error_free(il->error);
207 file_data_unref(il->fd);
209 g_mutex_free(il->data_mutex);
210 g_cond_free(il->can_destroy_cond);
214 void image_loader_free(ImageLoader *il)
217 g_object_unref(G_OBJECT(il));
221 ImageLoader *image_loader_new(FileData *fd)
225 if (!fd) return NULL;
227 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
229 il->fd = file_data_ref(fd);
234 /**************************************************************************************/
235 /* send signals via idle calbacks - the callback are executed in the main thread */
237 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
238 struct _ImageLoaderAreaParam {
247 static gboolean image_loader_emit_area_ready_cb(gpointer data)
249 ImageLoaderAreaParam *par = data;
250 ImageLoader *il = par->il;
251 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
252 g_mutex_lock(il->data_mutex);
253 il->area_param_list = g_list_remove(il->area_param_list, par);
255 g_mutex_unlock(il->data_mutex);
260 static gboolean image_loader_emit_done_cb(gpointer data)
262 ImageLoader *il = data;
263 g_signal_emit(il, signals[SIGNAL_DONE], 0);
267 static gboolean image_loader_emit_error_cb(gpointer data)
269 ImageLoader *il = data;
270 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
274 static gboolean image_loader_emit_percent_cb(gpointer data)
276 ImageLoader *il = data;
277 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
281 /* DONE and ERROR are emited only once, thus they can have normal priority
282 PERCENT and AREA_READY should be processed ASAP
285 static void image_loader_emit_done(ImageLoader *il)
287 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
290 static void image_loader_emit_error(ImageLoader *il)
292 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
295 static void image_loader_emit_percent(ImageLoader *il)
297 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
300 /* this function expects that il->data_mutex is locked by caller */
301 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
303 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
310 il->area_param_list = g_list_prepend(il->area_param_list, par);
312 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
315 /**************************************************************************************/
316 /* the following functions may be executed in separate thread */
318 /* this function expects that il->data_mutex is locked by caller */
319 static void image_loader_queue_delayed_erea_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
321 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
328 il->area_param_delayed_list = g_list_prepend(il->area_param_delayed_list, par);
333 static gboolean image_loader_get_stopping(ImageLoader *il)
336 if (!il) return FALSE;
338 g_mutex_lock(il->data_mutex);
340 g_mutex_unlock(il->data_mutex);
346 static void image_loader_sync_pixbuf(ImageLoader *il)
350 g_mutex_lock(il->data_mutex);
354 g_mutex_unlock(il->data_mutex);
358 pb = il->backend.get_pixbuf(il->loader);
360 if (pb == il->pixbuf)
362 g_mutex_unlock(il->data_mutex);
366 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
368 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
371 if (il->pixbuf) g_object_unref(il->pixbuf);
374 if (il->pixbuf) g_object_ref(il->pixbuf);
376 g_mutex_unlock(il->data_mutex);
379 static void image_loader_area_updated_cb(gpointer loader,
380 guint x, guint y, guint w, guint h,
383 ImageLoader *il = data;
385 if (!image_loader_get_pixbuf(il))
387 image_loader_sync_pixbuf(il);
388 if (!image_loader_get_pixbuf(il))
390 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
394 g_mutex_lock(il->data_mutex);
395 if (il->delay_area_ready)
396 image_loader_queue_delayed_erea_ready(il, x, y, w, h);
398 image_loader_emit_area_ready(il, x, y, w, h);
399 g_mutex_unlock(il->data_mutex);
402 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
404 ImageLoader *il = data;
410 http://bugzilla.gnome.org/show_bug.cgi?id=547669
411 http://bugzilla.gnome.org/show_bug.cgi?id=589334
413 gchar *format = il->backend.get_format_name(loader);
414 if (strcmp(format, "svg") == 0 ||
415 strcmp(format, "xpm") == 0)
423 pb = il->backend.get_pixbuf(loader);
425 h = gdk_pixbuf_get_height(pb);
426 rs = gdk_pixbuf_get_rowstride(pb);
427 pix = gdk_pixbuf_get_pixels(pb);
429 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
433 static void image_loader_size_cb(gpointer loader,
434 gint width, gint height, gpointer data)
436 ImageLoader *il = data;
438 gboolean scale = FALSE;
441 g_mutex_lock(il->data_mutex);
442 if (il->requested_width < 1 || il->requested_height < 1)
444 g_mutex_unlock(il->data_mutex);
445 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
448 g_mutex_unlock(il->data_mutex);
450 mime_types = il->backend.get_format_mime_types(loader);
452 while (mime_types[n])
454 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
457 g_strfreev(mime_types);
461 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
465 g_mutex_lock(il->data_mutex);
468 if (width > il->requested_width || height > il->requested_height)
471 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
473 nw = il->requested_width;
474 nh = (gdouble)nw / width * height;
479 nh = il->requested_height;
480 nw = (gdouble)nh / height * width;
484 il->backend.set_size(loader, nw, nh);
487 g_mutex_unlock(il->data_mutex);
489 g_signal_emit(il, signals[SIGNAL_SIZE], 0, nw, nh);
492 static void image_loader_stop_loader(ImageLoader *il)
498 /* some loaders do not have a pixbuf till close, order is important here */
499 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
500 image_loader_sync_pixbuf(il);
501 g_object_unref(G_OBJECT(il->loader));
504 g_mutex_lock(il->data_mutex);
506 g_mutex_unlock(il->data_mutex);
509 static void image_loader_setup_loader(ImageLoader *il)
511 g_mutex_lock(il->data_mutex);
512 image_loader_backend_set_default(&il->backend);
513 il->loader = il->backend.loader_new(G_CALLBACK(image_loader_area_updated_cb), G_CALLBACK(image_loader_size_cb), G_CALLBACK(image_loader_area_prepared_cb), il);
514 g_mutex_unlock(il->data_mutex);
518 static void image_loader_done(ImageLoader *il)
520 image_loader_stop_loader(il);
522 image_loader_emit_done(il);
525 static void image_loader_error(ImageLoader *il)
527 image_loader_stop_loader(il);
529 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
531 image_loader_emit_error(il);
534 static gboolean image_loader_continue(ImageLoader *il)
539 if (!il) return FALSE;
541 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
542 while (c > 0 && !image_loader_get_stopping(il))
544 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
548 image_loader_done(il);
552 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
554 image_loader_error(il);
563 if (il->bytes_total > 0)
565 image_loader_emit_percent(il);
571 static gboolean image_loader_begin(ImageLoader *il)
575 if (il->pixbuf) return FALSE;
577 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
578 if (b < 1) return FALSE;
580 image_loader_setup_loader(il);
582 if (!il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error))
584 image_loader_stop_loader(il);
590 /* read until size is known */
591 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
593 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
594 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
596 image_loader_stop_loader(il);
601 if (!il->pixbuf) image_loader_sync_pixbuf(il);
603 if (il->bytes_read == il->bytes_total || b < 1)
605 /* done, handle (broken) loaders that do not have pixbuf till close */
606 image_loader_stop_loader(il);
608 if (!il->pixbuf) return FALSE;
610 image_loader_done(il);
616 image_loader_stop_loader(il);
623 /**************************************************************************************/
624 /* the following functions are always executed in the main thread */
627 static gboolean image_loader_setup_source(ImageLoader *il)
632 if (!il || il->loader || il->mapped_file) return FALSE;
634 il->mapped_file = NULL;
638 ExifData *exif = exif_read_fd(il->fd);
640 if (options->thumbnails.use_exif)
641 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
643 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
648 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
650 exif_free_fd(il->fd, exif);
654 if (!il->mapped_file)
659 pathl = path_from_utf8(il->fd->path);
660 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
662 if (load_fd == -1) return FALSE;
664 if (fstat(load_fd, &st) == 0)
666 il->bytes_total = st.st_size;
674 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
676 if (il->mapped_file == MAP_FAILED)
687 static void image_loader_stop_source(ImageLoader *il)
695 exif_free_preview(il->mapped_file);
699 munmap(il->mapped_file, il->bytes_total);
701 il->mapped_file = NULL;
705 static void image_loader_stop(ImageLoader *il)
711 g_source_remove(il->idle_id);
717 /* stop loader in the other thread */
718 g_mutex_lock(il->data_mutex);
720 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
721 g_mutex_unlock(il->data_mutex);
724 image_loader_stop_loader(il);
725 image_loader_stop_source(il);
729 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
731 g_mutex_lock(il->data_mutex);
732 il->delay_area_ready = enable;
737 list = g_list_reverse(il->area_param_delayed_list);
738 il->area_param_delayed_list = NULL;
739 g_mutex_unlock(il->data_mutex);
745 ImageLoaderAreaParam *par = work->data;
748 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
756 g_mutex_unlock(il->data_mutex);
761 /**************************************************************************************/
762 /* execution via idle calls */
764 static gboolean image_loader_idle_cb(gpointer data)
766 gboolean ret = FALSE;
767 ImageLoader *il = data;
771 ret = image_loader_continue(il);
776 image_loader_stop_source(il);
783 static gboolean image_loader_start_idle(ImageLoader *il)
787 if (!il) return FALSE;
789 if (!il->fd) return FALSE;
791 if (!image_loader_setup_source(il)) return FALSE;
793 ret = image_loader_begin(il);
795 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
799 /**************************************************************************************/
800 /* execution via thread */
803 static GThreadPool *image_loader_thread_pool = NULL;
805 static GCond *image_loader_prio_cond = NULL;
806 static GMutex *image_loader_prio_mutex = NULL;
807 static gint image_loader_prio_num = 0;
810 static void image_loader_thread_enter_high(void)
812 g_mutex_lock(image_loader_prio_mutex);
813 image_loader_prio_num++;
814 g_mutex_unlock(image_loader_prio_mutex);
817 static void image_loader_thread_leave_high(void)
819 g_mutex_lock(image_loader_prio_mutex);
820 image_loader_prio_num--;
821 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
822 g_mutex_unlock(image_loader_prio_mutex);
825 static void image_loader_thread_wait_high(void)
827 g_mutex_lock(image_loader_prio_mutex);
828 while (image_loader_prio_num)
830 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
833 g_mutex_unlock(image_loader_prio_mutex);
837 static void image_loader_thread_run(gpointer data, gpointer user_data)
839 ImageLoader *il = data;
843 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
845 /* low prio, wait untill high prio tasks finishes */
846 image_loader_thread_wait_high();
851 image_loader_thread_enter_high();
854 err = !image_loader_begin(il);
859 loader failed, we have to send signal
860 (idle mode returns the image_loader_begin return value directly)
861 (success is always reported indirectly from image_loader_begin)
863 image_loader_emit_error(il);
868 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
870 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
872 /* low prio, wait untill high prio tasks finishes */
873 image_loader_thread_wait_high();
875 cont = image_loader_continue(il);
877 image_loader_stop_loader(il);
879 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
882 image_loader_thread_leave_high();
885 g_mutex_lock(il->data_mutex);
886 il->can_destroy = TRUE;
887 g_cond_signal(il->can_destroy_cond);
888 g_mutex_unlock(il->data_mutex);
893 static gboolean image_loader_start_thread(ImageLoader *il)
895 if (!il) return FALSE;
897 if (!il->fd) return FALSE;
901 if (!image_loader_setup_source(il)) return FALSE;
903 if (!image_loader_thread_pool)
905 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
906 image_loader_prio_cond = g_cond_new();
907 image_loader_prio_mutex = g_mutex_new();
910 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
912 g_thread_pool_push(image_loader_thread_pool, il, NULL);
913 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
917 #endif /* HAVE_GTHREAD */
920 /**************************************************************************************/
921 /* public interface */
924 gboolean image_loader_start(ImageLoader *il)
926 if (!il) return FALSE;
928 if (!il->fd) return FALSE;
931 return image_loader_start_thread(il);
933 return image_loader_start_idle(il);
938 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
939 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
942 if (!il) return NULL;
944 g_mutex_lock(il->data_mutex);
946 g_mutex_unlock(il->data_mutex);
950 gchar *image_loader_get_format(ImageLoader *il)
955 if (!il || !il->loader) return NULL;
957 mimev = il->backend.get_format_mime_types(il->loader);
958 if (!mimev) return NULL;
960 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
961 mime = g_strdup(mimev[0]);
967 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
971 g_mutex_lock(il->data_mutex);
972 il->requested_width = width;
973 il->requested_height = height;
974 g_mutex_unlock(il->data_mutex);
977 void image_loader_set_buffer_size(ImageLoader *il, guint count)
981 g_mutex_lock(il->data_mutex);
982 il->idle_read_loop_count = count ? count : 1;
983 g_mutex_unlock(il->data_mutex);
986 void image_loader_set_priority(ImageLoader *il, gint priority)
990 if (il->thread) return; /* can't change prio if the thread already runs */
991 il->idle_priority = priority;
995 gdouble image_loader_get_percent(ImageLoader *il)
1000 g_mutex_lock(il->data_mutex);
1001 if (il->bytes_total == 0)
1007 ret = (gdouble)il->bytes_read / il->bytes_total;
1009 g_mutex_unlock(il->data_mutex);
1013 gboolean image_loader_get_is_done(ImageLoader *il)
1016 if (!il) return FALSE;
1018 g_mutex_lock(il->data_mutex);
1020 g_mutex_unlock(il->data_mutex);
1025 FileData *image_loader_get_fd(ImageLoader *il)
1028 if (!il) return NULL;
1030 g_mutex_lock(il->data_mutex);
1032 g_mutex_unlock(il->data_mutex);
1037 gboolean image_loader_get_shrunk(ImageLoader *il)
1040 if (!il) return FALSE;
1042 g_mutex_lock(il->data_mutex);
1044 g_mutex_unlock(il->data_mutex);
1048 const gchar *image_loader_get_error(ImageLoader *il)
1050 const gchar *ret = NULL;
1051 if (!il) return NULL;
1052 g_mutex_lock(il->data_mutex);
1053 if (il->error) ret = il->error->message;
1054 g_mutex_unlock(il->data_mutex);
1059 /* FIXME - this can be rather slow and blocks until the size is known */
1060 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1065 il = image_loader_new(fd);
1067 success = image_loader_start_idle(il);
1069 if (success && il->pixbuf)
1071 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1072 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1076 if (width) *width = -1;
1077 if (height) *height = -1;
1080 image_loader_free(il);
1084 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */