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;
91 il->data_mutex = g_mutex_new();
93 DEBUG_1("new image loader %p, bufsize=%u idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
96 static void image_loader_class_init (ImageLoaderClass *class)
98 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
100 // gobject_class->set_property = image_loader_set_property;
101 // gobject_class->get_property = image_loader_get_property;
103 gobject_class->finalize = image_loader_finalize;
106 signals[SIGNAL_AREA_READY] =
107 g_signal_new("area_ready",
108 G_OBJECT_CLASS_TYPE(gobject_class),
110 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
112 gq_marshal_VOID__INT_INT_INT_INT,
119 signals[SIGNAL_ERROR] =
120 g_signal_new("error",
121 G_OBJECT_CLASS_TYPE(gobject_class),
123 G_STRUCT_OFFSET(ImageLoaderClass, error),
125 g_cclosure_marshal_VOID__VOID,
129 signals[SIGNAL_DONE] =
131 G_OBJECT_CLASS_TYPE(gobject_class),
133 G_STRUCT_OFFSET(ImageLoaderClass, done),
135 g_cclosure_marshal_VOID__VOID,
138 signals[SIGNAL_PERCENT] =
139 g_signal_new("percent",
140 G_OBJECT_CLASS_TYPE(gobject_class),
142 G_STRUCT_OFFSET(ImageLoaderClass, percent),
144 g_cclosure_marshal_VOID__DOUBLE,
150 static void image_loader_finalize(GObject *object)
152 ImageLoader *il = (ImageLoader *)object;
154 image_loader_stop(il);
156 DEBUG_1("freeing image loader %p bytes_read=%d", il, il->bytes_read);
158 if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
160 while (g_source_remove_by_user_data(il))
162 DEBUG_1("pending signals detected");
165 while (il->area_param_list)
167 DEBUG_1("pending area_ready signals detected");
168 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
169 g_free(il->area_param_list->data);
170 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
173 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
174 file_data_unref(il->fd);
176 g_mutex_free(il->data_mutex);
180 void image_loader_free(ImageLoader *il)
183 g_object_unref(G_OBJECT(il));
187 ImageLoader *image_loader_new(FileData *fd)
191 if (!fd) return NULL;
193 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
195 il->fd = file_data_ref(fd);
200 /**************************************************************************************/
201 /* send signals via idle calbacks - the callback are executed in the main thread */
203 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
204 struct _ImageLoaderAreaParam {
213 static gint image_loader_emit_area_ready_cb(gpointer data)
215 ImageLoaderAreaParam *par = data;
216 ImageLoader *il = par->il;
217 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
218 g_mutex_lock(il->data_mutex);
219 il->area_param_list = g_list_remove(il->area_param_list, par);
221 g_mutex_unlock(il->data_mutex);
226 static gint image_loader_emit_done_cb(gpointer data)
228 ImageLoader *il = data;
229 g_signal_emit(il, signals[SIGNAL_DONE], 0);
233 static gint image_loader_emit_error_cb(gpointer data)
235 ImageLoader *il = data;
236 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
240 static gint image_loader_emit_percent_cb(gpointer data)
242 ImageLoader *il = data;
243 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
247 /* DONE and ERROR are emited only once, thus they can have normal priority
248 PERCENT and AREA_READY should be processed ASAP
251 static void image_loader_emit_done(ImageLoader *il)
253 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
256 static void image_loader_emit_error(ImageLoader *il)
258 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
261 static void image_loader_emit_percent(ImageLoader *il)
263 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
266 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
268 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
275 g_mutex_lock(il->data_mutex);
276 il->area_param_list = g_list_prepend(il->area_param_list, par);
277 g_mutex_unlock(il->data_mutex);
279 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
282 /**************************************************************************************/
283 /* the following functions may be executed in separate thread */
285 static void image_loader_sync_pixbuf(ImageLoader *il)
289 g_mutex_lock(il->data_mutex);
293 g_mutex_unlock(il->data_mutex);
297 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
299 if (pb == il->pixbuf)
301 g_mutex_unlock(il->data_mutex);
305 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
307 if (il->pixbuf) gdk_pixbuf_ref(il->pixbuf);
308 g_mutex_unlock(il->data_mutex);
311 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
312 guint x, guint y, guint w, guint h,
315 ImageLoader *il = data;
317 if (!image_loader_get_pixbuf(il))
319 image_loader_sync_pixbuf(il);
320 if (!image_loader_get_pixbuf(il))
322 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
325 image_loader_emit_area_ready(il, x, y, w, h);
328 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
334 /* a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669 */
335 gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
336 if (strcmp(format, "svg") == 0)
344 pb = gdk_pixbuf_loader_get_pixbuf(loader);
346 h = gdk_pixbuf_get_height(pb);
347 rs = gdk_pixbuf_get_rowstride(pb);
348 pix = gdk_pixbuf_get_pixels(pb);
350 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
354 static void image_loader_size_cb(GdkPixbufLoader *loader,
355 gint width, gint height, gpointer data)
357 ImageLoader *il = data;
358 GdkPixbufFormat *format;
363 g_mutex_lock(il->data_mutex);
364 if (il->requested_width < 1 || il->requested_height < 1)
366 g_mutex_unlock(il->data_mutex);
369 g_mutex_unlock(il->data_mutex);
371 format = gdk_pixbuf_loader_get_format(loader);
374 mime_types = gdk_pixbuf_format_get_mime_types(format);
376 while (mime_types[n])
378 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
381 g_strfreev(mime_types);
385 g_mutex_lock(il->data_mutex);
387 if (width > il->requested_width || height > il->requested_height)
391 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
393 nw = il->requested_width;
394 nh = (gdouble)nw / width * height;
399 nh = il->requested_height;
400 nw = (gdouble)nh / height * width;
404 gdk_pixbuf_loader_set_size(loader, nw, nh);
407 g_mutex_unlock(il->data_mutex);
411 static void image_loader_stop_loader(ImageLoader *il)
417 /* some loaders do not have a pixbuf till close, order is important here */
418 gdk_pixbuf_loader_close(il->loader, NULL);
419 image_loader_sync_pixbuf(il);
420 g_object_unref(G_OBJECT(il->loader));
427 static void image_loader_setup_loader(ImageLoader *il)
429 g_mutex_lock(il->data_mutex);
430 il->loader = gdk_pixbuf_loader_new();
431 g_mutex_unlock(il->data_mutex);
433 g_signal_connect(G_OBJECT(il->loader), "area_updated",
434 G_CALLBACK(image_loader_area_updated_cb), il);
435 g_signal_connect(G_OBJECT(il->loader), "size_prepared",
436 G_CALLBACK(image_loader_size_cb), il);
437 g_signal_connect(G_OBJECT(il->loader), "area_prepared",
438 G_CALLBACK(image_loader_area_prepared_cb), il);
442 static void image_loader_done(ImageLoader *il)
444 image_loader_stop_loader(il);
446 image_loader_emit_done(il);
449 static void image_loader_error(ImageLoader *il)
451 image_loader_stop_loader(il);
453 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
455 image_loader_emit_error(il);
458 static gint image_loader_continue(ImageLoader *il)
463 if (!il) return FALSE;
465 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
468 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
472 image_loader_done(il);
476 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
478 image_loader_error(il);
487 if (il->bytes_total > 0)
489 image_loader_emit_percent(il);
495 static gint image_loader_begin(ImageLoader *il)
499 if (il->pixbuf) return FALSE;
501 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
502 if (b < 1) return FALSE;
504 image_loader_setup_loader(il);
506 if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL))
508 image_loader_stop_loader(il);
514 /* read until size is known */
515 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0)
517 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
518 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
520 image_loader_stop_loader(il);
525 if (!il->pixbuf) image_loader_sync_pixbuf(il);
527 if (il->bytes_read == il->bytes_total || b < 1)
529 /* done, handle (broken) loaders that do not have pixbuf till close */
530 image_loader_stop_loader(il);
532 if (!il->pixbuf) return FALSE;
534 image_loader_done(il);
540 image_loader_stop_loader(il);
547 /**************************************************************************************/
548 /* the following functions are always executed in the main thread */
551 static gint image_loader_setup_source(ImageLoader *il)
556 if (!il || il->loader || il->mapped_file) return FALSE;
558 il->mapped_file = NULL;
562 ExifData *exif = exif_read_fd(il->fd);
564 il->mapped_file = exif_get_preview(exif, &il->bytes_total);
569 DEBUG_1("Raw file %s contains embedded image", il->fd->path);
571 exif_free_fd(il->fd, exif);
575 if (!il->mapped_file)
580 pathl = path_from_utf8(il->fd->path);
581 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
583 if (load_fd == -1) return FALSE;
585 if (fstat(load_fd, &st) == 0)
587 il->bytes_total = st.st_size;
595 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
597 if (il->mapped_file == MAP_FAILED)
608 static void image_loader_stop_source(ImageLoader *il)
616 exif_free_preview(il->mapped_file);
620 munmap(il->mapped_file, il->bytes_total);
622 il->mapped_file = NULL;
628 static gint image_loader_setup(ImageLoader *il)
630 if (!image_loader_setup_source(il)) return FALSE;
632 return image_loader_begin(il);
636 static void image_loader_stop(ImageLoader *il)
640 if (il->idle_id != -1)
642 g_source_remove(il->idle_id);
649 g_thread_join(il->thread);
652 image_loader_stop_loader(il);
653 image_loader_stop_source(il);
657 /**************************************************************************************/
658 /* execution via idle calls */
660 static gint image_loader_idle_cb(gpointer data)
663 ImageLoader *il = data;
664 if (il->idle_id != -1)
666 ret = image_loader_continue(il);
670 image_loader_stop_source(il);
676 gint image_loader_start_idle(ImageLoader *il)
679 if (!il) return FALSE;
681 if (!il->fd) return FALSE;
683 if (!image_loader_setup_source(il)) return FALSE;
685 ret = image_loader_begin(il);
687 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
691 /**************************************************************************************/
692 /* execution via thread */
694 gpointer image_loader_thread_run(gpointer data)
696 ImageLoader *il = data;
697 gint cont = image_loader_begin(il);
699 while (cont && !il->done && !il->stopping)
701 cont = image_loader_continue(il);
706 gint image_loader_start_thread(ImageLoader *il)
708 if (!il) return FALSE;
710 if (!il->fd) return FALSE;
712 if (!image_loader_setup_source(il)) return FALSE;
714 il->thread = g_thread_create(image_loader_thread_run, il, TRUE, NULL);
715 if (!il->thread) return FALSE;
721 /**************************************************************************************/
722 /* public interface */
725 gint image_loader_start(ImageLoader *il)
727 if (!il) return FALSE;
729 if (!il->fd) return FALSE;
732 return image_loader_start_thread(il);
734 return image_loader_start_idle(il);
739 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
740 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
743 if (!il) return NULL;
745 g_mutex_lock(il->data_mutex);
747 g_mutex_unlock(il->data_mutex);
751 gchar *image_loader_get_format(ImageLoader *il)
753 GdkPixbufFormat *format;
757 if (!il || !il->loader) return NULL;
759 format = gdk_pixbuf_loader_get_format(il->loader);
760 if (!format) return NULL;
762 mimev = gdk_pixbuf_format_get_mime_types(format);
763 if (!mimev) return NULL;
765 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
766 mime = g_strdup(mimev[0]);
772 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
776 g_mutex_lock(il->data_mutex);
777 il->requested_width = width;
778 il->requested_height = height;
779 g_mutex_unlock(il->data_mutex);
782 void image_loader_set_buffer_size(ImageLoader *il, guint count)
786 g_mutex_lock(il->data_mutex);
787 il->idle_read_loop_count = count ? count : 1;
788 g_mutex_unlock(il->data_mutex);
791 void image_loader_set_priority(ImageLoader *il, gint priority)
795 g_mutex_lock(il->data_mutex);
796 il->idle_priority = priority;
797 g_mutex_unlock(il->data_mutex);
801 gdouble image_loader_get_percent(ImageLoader *il)
806 g_mutex_lock(il->data_mutex);
807 if (il->bytes_total == 0)
813 ret = (gdouble)il->bytes_read / il->bytes_total;
815 g_mutex_unlock(il->data_mutex);
819 gint image_loader_get_is_done(ImageLoader *il)
822 if (!il) return FALSE;
824 g_mutex_lock(il->data_mutex);
826 g_mutex_unlock(il->data_mutex);
831 FileData *image_loader_get_fd(ImageLoader *il)
834 if (!il) return NULL;
836 g_mutex_lock(il->data_mutex);
838 g_mutex_unlock(il->data_mutex);
843 gint image_loader_get_shrunk(ImageLoader *il)
846 if (!il) return FALSE;
848 g_mutex_lock(il->data_mutex);
850 g_mutex_unlock(il->data_mutex);
855 /* FIXME - this can be rather slow and blocks until the size is known */
856 gint image_load_dimensions(FileData *fd, gint *width, gint *height)
861 il = image_loader_new(fd);
863 success = image_loader_start_idle(il);
865 if (success && il->pixbuf)
867 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
868 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
872 if (width) *width = -1;
873 if (height) *height = -1;
876 image_loader_free(il);