4 * Copyright (C) 2008 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 looader 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 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
176 file_data_unref(il->fd);
178 g_mutex_free(il->data_mutex);
179 g_cond_free(il->can_destroy_cond);
183 void image_loader_free(ImageLoader *il)
186 g_object_unref(G_OBJECT(il));
190 ImageLoader *image_loader_new(FileData *fd)
194 if (!fd) return NULL;
196 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
198 il->fd = file_data_ref(fd);
203 /**************************************************************************************/
204 /* send signals via idle calbacks - the callback are executed in the main thread */
206 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
207 struct _ImageLoaderAreaParam {
216 static gint image_loader_emit_area_ready_cb(gpointer data)
218 ImageLoaderAreaParam *par = data;
219 ImageLoader *il = par->il;
220 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
221 g_mutex_lock(il->data_mutex);
222 il->area_param_list = g_list_remove(il->area_param_list, par);
224 g_mutex_unlock(il->data_mutex);
229 static gint image_loader_emit_done_cb(gpointer data)
231 ImageLoader *il = data;
232 g_signal_emit(il, signals[SIGNAL_DONE], 0);
236 static gint image_loader_emit_error_cb(gpointer data)
238 ImageLoader *il = data;
239 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
243 static gint image_loader_emit_percent_cb(gpointer data)
245 ImageLoader *il = data;
246 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
250 /* DONE and ERROR are emited only once, thus they can have normal priority
251 PERCENT and AREA_READY should be processed ASAP
254 static void image_loader_emit_done(ImageLoader *il)
256 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
259 static void image_loader_emit_error(ImageLoader *il)
261 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
264 static void image_loader_emit_percent(ImageLoader *il)
266 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
269 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
271 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
278 g_mutex_lock(il->data_mutex);
279 il->area_param_list = g_list_prepend(il->area_param_list, par);
280 g_mutex_unlock(il->data_mutex);
282 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
285 /**************************************************************************************/
286 /* the following functions may be executed in separate thread */
288 static gint image_loader_get_stopping(ImageLoader *il)
291 if (!il) return FALSE;
293 g_mutex_lock(il->data_mutex);
295 g_mutex_unlock(il->data_mutex);
300 static void image_loader_sync_pixbuf(ImageLoader *il)
304 g_mutex_lock(il->data_mutex);
308 g_mutex_unlock(il->data_mutex);
312 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
314 if (pb == il->pixbuf)
316 g_mutex_unlock(il->data_mutex);
320 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
322 if (il->pixbuf) gdk_pixbuf_ref(il->pixbuf);
323 g_mutex_unlock(il->data_mutex);
326 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
327 guint x, guint y, guint w, guint h,
330 ImageLoader *il = data;
332 if (!image_loader_get_pixbuf(il))
334 image_loader_sync_pixbuf(il);
335 if (!image_loader_get_pixbuf(il))
337 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
340 image_loader_emit_area_ready(il, x, y, w, h);
343 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
349 /* a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669 */
350 gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
351 if (strcmp(format, "svg") == 0)
359 pb = gdk_pixbuf_loader_get_pixbuf(loader);
361 h = gdk_pixbuf_get_height(pb);
362 rs = gdk_pixbuf_get_rowstride(pb);
363 pix = gdk_pixbuf_get_pixels(pb);
365 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
369 static void image_loader_size_cb(GdkPixbufLoader *loader,
370 gint width, gint height, gpointer data)
372 ImageLoader *il = data;
373 GdkPixbufFormat *format;
378 g_mutex_lock(il->data_mutex);
379 if (il->requested_width < 1 || il->requested_height < 1)
381 g_mutex_unlock(il->data_mutex);
384 g_mutex_unlock(il->data_mutex);
386 format = gdk_pixbuf_loader_get_format(loader);
389 mime_types = gdk_pixbuf_format_get_mime_types(format);
391 while (mime_types[n])
393 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
396 g_strfreev(mime_types);
400 g_mutex_lock(il->data_mutex);
402 if (width > il->requested_width || height > il->requested_height)
406 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
408 nw = il->requested_width;
409 nh = (gdouble)nw / width * height;
414 nh = il->requested_height;
415 nw = (gdouble)nh / height * width;
419 gdk_pixbuf_loader_set_size(loader, nw, nh);
422 g_mutex_unlock(il->data_mutex);
426 static void image_loader_stop_loader(ImageLoader *il)
432 /* some loaders do not have a pixbuf till close, order is important here */
433 gdk_pixbuf_loader_close(il->loader, NULL);
434 image_loader_sync_pixbuf(il);
435 g_object_unref(G_OBJECT(il->loader));
438 g_mutex_lock(il->data_mutex);
440 g_mutex_unlock(il->data_mutex);
443 static void image_loader_setup_loader(ImageLoader *il)
445 g_mutex_lock(il->data_mutex);
446 il->loader = gdk_pixbuf_loader_new();
448 g_signal_connect(G_OBJECT(il->loader), "area_updated",
449 G_CALLBACK(image_loader_area_updated_cb), il);
450 g_signal_connect(G_OBJECT(il->loader), "size_prepared",
451 G_CALLBACK(image_loader_size_cb), il);
452 g_signal_connect(G_OBJECT(il->loader), "area_prepared",
453 G_CALLBACK(image_loader_area_prepared_cb), il);
454 g_mutex_unlock(il->data_mutex);
458 static void image_loader_done(ImageLoader *il)
460 image_loader_stop_loader(il);
462 image_loader_emit_done(il);
465 static void image_loader_error(ImageLoader *il)
467 image_loader_stop_loader(il);
469 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
471 image_loader_emit_error(il);
474 static gint image_loader_continue(ImageLoader *il)
479 if (!il) return FALSE;
481 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
482 while (c > 0 && !image_loader_get_stopping(il))
484 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
488 image_loader_done(il);
492 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
494 image_loader_error(il);
503 if (il->bytes_total > 0)
505 image_loader_emit_percent(il);
511 static gint image_loader_begin(ImageLoader *il)
515 if (il->pixbuf) return FALSE;
517 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
518 if (b < 1) return FALSE;
520 image_loader_setup_loader(il);
522 if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL))
524 image_loader_stop_loader(il);
530 /* read until size is known */
531 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
533 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
534 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
536 image_loader_stop_loader(il);
541 if (!il->pixbuf) image_loader_sync_pixbuf(il);
543 if (il->bytes_read == il->bytes_total || b < 1)
545 /* done, handle (broken) loaders that do not have pixbuf till close */
546 image_loader_stop_loader(il);
548 if (!il->pixbuf) return FALSE;
550 image_loader_done(il);
556 image_loader_stop_loader(il);
563 /**************************************************************************************/
564 /* the following functions are always executed in the main thread */
567 static gint image_loader_setup_source(ImageLoader *il)
572 if (!il || il->loader || il->mapped_file) return FALSE;
574 il->mapped_file = NULL;
578 ExifData *exif = exif_read_fd(il->fd);
580 il->mapped_file = exif_get_preview(exif, &il->bytes_total);
585 DEBUG_1("Raw file %s contains embedded image", il->fd->path);
587 exif_free_fd(il->fd, exif);
591 if (!il->mapped_file)
596 pathl = path_from_utf8(il->fd->path);
597 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
599 if (load_fd == -1) return FALSE;
601 if (fstat(load_fd, &st) == 0)
603 il->bytes_total = st.st_size;
611 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
613 if (il->mapped_file == MAP_FAILED)
624 static void image_loader_stop_source(ImageLoader *il)
632 exif_free_preview(il->mapped_file);
636 munmap(il->mapped_file, il->bytes_total);
638 il->mapped_file = NULL;
642 static void image_loader_stop(ImageLoader *il)
646 if (il->idle_id != -1)
648 g_source_remove(il->idle_id);
654 /* stop loader in the other thread */
655 g_mutex_lock(il->data_mutex);
657 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
658 g_mutex_unlock(il->data_mutex);
661 image_loader_stop_loader(il);
662 image_loader_stop_source(il);
666 /**************************************************************************************/
667 /* execution via idle calls */
669 static gint image_loader_idle_cb(gpointer data)
672 ImageLoader *il = data;
673 if (il->idle_id != -1)
675 ret = image_loader_continue(il);
679 image_loader_stop_source(il);
685 gint image_loader_start_idle(ImageLoader *il)
688 if (!il) return FALSE;
690 if (!il->fd) return FALSE;
692 if (!image_loader_setup_source(il)) return FALSE;
694 ret = image_loader_begin(il);
696 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
700 /**************************************************************************************/
701 /* execution via thread */
702 static GThreadPool *image_loader_thread_pool = NULL;
704 void image_loader_thread_run(gpointer data, gpointer user_data)
706 ImageLoader *il = data;
707 gint cont = image_loader_begin(il);
709 if (!cont && !image_loader_get_pixbuf(il))
712 loader failed, we have to send signal
713 (idle mode returns the image_loader_begin return value directly)
714 (success is always reported indirectly from image_loader_begin)
716 image_loader_emit_error(il);
719 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
721 cont = image_loader_continue(il);
723 image_loader_stop_loader(il);
725 g_mutex_lock(il->data_mutex);
726 il->can_destroy = TRUE;
727 g_cond_signal(il->can_destroy_cond);
728 g_mutex_unlock(il->data_mutex);
733 gint image_loader_start_thread(ImageLoader *il)
735 if (!il) return FALSE;
737 if (!il->fd) return FALSE;
741 if (!image_loader_setup_source(il)) return FALSE;
743 if (!image_loader_thread_pool)
745 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
748 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
750 g_thread_pool_push(image_loader_thread_pool, il, NULL);
751 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
757 /**************************************************************************************/
758 /* public interface */
761 gint image_loader_start(ImageLoader *il)
763 if (!il) return FALSE;
765 if (!il->fd) return FALSE;
768 return image_loader_start_thread(il);
770 return image_loader_start_idle(il);
775 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
776 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
779 if (!il) return NULL;
781 g_mutex_lock(il->data_mutex);
783 g_mutex_unlock(il->data_mutex);
787 gchar *image_loader_get_format(ImageLoader *il)
789 GdkPixbufFormat *format;
793 if (!il || !il->loader) return NULL;
795 format = gdk_pixbuf_loader_get_format(il->loader);
796 if (!format) return NULL;
798 mimev = gdk_pixbuf_format_get_mime_types(format);
799 if (!mimev) return NULL;
801 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
802 mime = g_strdup(mimev[0]);
808 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
812 g_mutex_lock(il->data_mutex);
813 il->requested_width = width;
814 il->requested_height = height;
815 g_mutex_unlock(il->data_mutex);
818 void image_loader_set_buffer_size(ImageLoader *il, guint count)
822 g_mutex_lock(il->data_mutex);
823 il->idle_read_loop_count = count ? count : 1;
824 g_mutex_unlock(il->data_mutex);
827 void image_loader_set_priority(ImageLoader *il, gint priority)
831 g_mutex_lock(il->data_mutex);
832 il->idle_priority = priority;
833 g_mutex_unlock(il->data_mutex);
837 gdouble image_loader_get_percent(ImageLoader *il)
842 g_mutex_lock(il->data_mutex);
843 if (il->bytes_total == 0)
849 ret = (gdouble)il->bytes_read / il->bytes_total;
851 g_mutex_unlock(il->data_mutex);
855 gint image_loader_get_is_done(ImageLoader *il)
858 if (!il) return FALSE;
860 g_mutex_lock(il->data_mutex);
862 g_mutex_unlock(il->data_mutex);
867 FileData *image_loader_get_fd(ImageLoader *il)
870 if (!il) return NULL;
872 g_mutex_lock(il->data_mutex);
874 g_mutex_unlock(il->data_mutex);
879 gint image_loader_get_shrunk(ImageLoader *il)
882 if (!il) return FALSE;
884 g_mutex_lock(il->data_mutex);
886 g_mutex_unlock(il->data_mutex);
891 /* FIXME - this can be rather slow and blocks until the size is known */
892 gint image_load_dimensions(FileData *fd, gint *width, gint *height)
897 il = image_loader_new(fd);
899 success = image_loader_start_idle(il);
901 if (success && il->pixbuf)
903 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
904 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
908 if (width) *width = -1;
909 if (height) *height = -1;
912 image_loader_free(il);