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 gint image_loader_get_stopping(ImageLoader *il)
288 if (!il) return FALSE;
290 g_mutex_lock(il->data_mutex);
292 g_mutex_unlock(il->data_mutex);
297 static void image_loader_sync_pixbuf(ImageLoader *il)
301 g_mutex_lock(il->data_mutex);
305 g_mutex_unlock(il->data_mutex);
309 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
311 if (pb == il->pixbuf)
313 g_mutex_unlock(il->data_mutex);
317 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
319 if (il->pixbuf) gdk_pixbuf_ref(il->pixbuf);
320 g_mutex_unlock(il->data_mutex);
323 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
324 guint x, guint y, guint w, guint h,
327 ImageLoader *il = data;
329 if (!image_loader_get_pixbuf(il))
331 image_loader_sync_pixbuf(il);
332 if (!image_loader_get_pixbuf(il))
334 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
337 image_loader_emit_area_ready(il, x, y, w, h);
340 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
346 /* a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669 */
347 gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
348 if (strcmp(format, "svg") == 0)
356 pb = gdk_pixbuf_loader_get_pixbuf(loader);
358 h = gdk_pixbuf_get_height(pb);
359 rs = gdk_pixbuf_get_rowstride(pb);
360 pix = gdk_pixbuf_get_pixels(pb);
362 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
366 static void image_loader_size_cb(GdkPixbufLoader *loader,
367 gint width, gint height, gpointer data)
369 ImageLoader *il = data;
370 GdkPixbufFormat *format;
375 g_mutex_lock(il->data_mutex);
376 if (il->requested_width < 1 || il->requested_height < 1)
378 g_mutex_unlock(il->data_mutex);
381 g_mutex_unlock(il->data_mutex);
383 format = gdk_pixbuf_loader_get_format(loader);
386 mime_types = gdk_pixbuf_format_get_mime_types(format);
388 while (mime_types[n])
390 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
393 g_strfreev(mime_types);
397 g_mutex_lock(il->data_mutex);
399 if (width > il->requested_width || height > il->requested_height)
403 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
405 nw = il->requested_width;
406 nh = (gdouble)nw / width * height;
411 nh = il->requested_height;
412 nw = (gdouble)nh / height * width;
416 gdk_pixbuf_loader_set_size(loader, nw, nh);
419 g_mutex_unlock(il->data_mutex);
423 static void image_loader_stop_loader(ImageLoader *il)
429 /* some loaders do not have a pixbuf till close, order is important here */
430 gdk_pixbuf_loader_close(il->loader, NULL);
431 image_loader_sync_pixbuf(il);
432 g_object_unref(G_OBJECT(il->loader));
435 g_mutex_lock(il->data_mutex);
437 g_mutex_unlock(il->data_mutex);
440 static void image_loader_setup_loader(ImageLoader *il)
442 g_mutex_lock(il->data_mutex);
443 il->loader = gdk_pixbuf_loader_new();
445 g_signal_connect(G_OBJECT(il->loader), "area_updated",
446 G_CALLBACK(image_loader_area_updated_cb), il);
447 g_signal_connect(G_OBJECT(il->loader), "size_prepared",
448 G_CALLBACK(image_loader_size_cb), il);
449 g_signal_connect(G_OBJECT(il->loader), "area_prepared",
450 G_CALLBACK(image_loader_area_prepared_cb), il);
451 g_mutex_unlock(il->data_mutex);
455 static void image_loader_done(ImageLoader *il)
457 image_loader_stop_loader(il);
459 image_loader_emit_done(il);
462 static void image_loader_error(ImageLoader *il)
464 image_loader_stop_loader(il);
466 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
468 image_loader_emit_error(il);
471 static gint image_loader_continue(ImageLoader *il)
476 if (!il) return FALSE;
478 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
479 while (c > 0 && !image_loader_get_stopping(il))
481 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
485 image_loader_done(il);
489 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
491 image_loader_error(il);
500 if (il->bytes_total > 0)
502 image_loader_emit_percent(il);
508 static gint image_loader_begin(ImageLoader *il)
512 if (il->pixbuf) return FALSE;
514 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
515 if (b < 1) return FALSE;
517 image_loader_setup_loader(il);
519 if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL))
521 image_loader_stop_loader(il);
527 /* read until size is known */
528 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
530 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
531 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
533 image_loader_stop_loader(il);
538 if (!il->pixbuf) image_loader_sync_pixbuf(il);
540 if (il->bytes_read == il->bytes_total || b < 1)
542 /* done, handle (broken) loaders that do not have pixbuf till close */
543 image_loader_stop_loader(il);
545 if (!il->pixbuf) return FALSE;
547 image_loader_done(il);
553 image_loader_stop_loader(il);
560 /**************************************************************************************/
561 /* the following functions are always executed in the main thread */
564 static gint image_loader_setup_source(ImageLoader *il)
569 if (!il || il->loader || il->mapped_file) return FALSE;
571 il->mapped_file = NULL;
575 ExifData *exif = exif_read_fd(il->fd);
577 il->mapped_file = exif_get_preview(exif, &il->bytes_total);
582 DEBUG_1("Raw file %s contains embedded image", il->fd->path);
584 exif_free_fd(il->fd, exif);
588 if (!il->mapped_file)
593 pathl = path_from_utf8(il->fd->path);
594 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
596 if (load_fd == -1) return FALSE;
598 if (fstat(load_fd, &st) == 0)
600 il->bytes_total = st.st_size;
608 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
610 if (il->mapped_file == MAP_FAILED)
621 static void image_loader_stop_source(ImageLoader *il)
629 exif_free_preview(il->mapped_file);
633 munmap(il->mapped_file, il->bytes_total);
635 il->mapped_file = NULL;
639 static void image_loader_stop(ImageLoader *il)
643 if (il->idle_id != -1)
645 g_source_remove(il->idle_id);
651 g_mutex_lock(il->data_mutex);
653 g_mutex_unlock(il->data_mutex);
654 g_thread_join(il->thread);
657 image_loader_stop_loader(il);
658 image_loader_stop_source(il);
662 /**************************************************************************************/
663 /* execution via idle calls */
665 static gint image_loader_idle_cb(gpointer data)
668 ImageLoader *il = data;
669 if (il->idle_id != -1)
671 ret = image_loader_continue(il);
675 image_loader_stop_source(il);
681 gint image_loader_start_idle(ImageLoader *il)
684 if (!il) return FALSE;
686 if (!il->fd) return FALSE;
688 if (!image_loader_setup_source(il)) return FALSE;
690 ret = image_loader_begin(il);
692 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
696 /**************************************************************************************/
697 /* execution via thread */
699 gpointer image_loader_thread_run(gpointer data)
701 ImageLoader *il = data;
702 gint cont = image_loader_begin(il);
704 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
706 cont = image_loader_continue(il);
708 image_loader_stop_loader(il);
712 gint image_loader_start_thread(ImageLoader *il)
714 if (!il) return FALSE;
716 if (!il->fd) return FALSE;
718 if (!image_loader_setup_source(il)) return FALSE;
720 il->thread = g_thread_create(image_loader_thread_run, il, TRUE, NULL);
721 if (!il->thread) return FALSE;
727 /**************************************************************************************/
728 /* public interface */
731 gint image_loader_start(ImageLoader *il)
733 if (!il) return FALSE;
735 if (!il->fd) return FALSE;
738 return image_loader_start_thread(il);
740 return image_loader_start_idle(il);
745 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
746 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
749 if (!il) return NULL;
751 g_mutex_lock(il->data_mutex);
753 g_mutex_unlock(il->data_mutex);
757 gchar *image_loader_get_format(ImageLoader *il)
759 GdkPixbufFormat *format;
763 if (!il || !il->loader) return NULL;
765 format = gdk_pixbuf_loader_get_format(il->loader);
766 if (!format) return NULL;
768 mimev = gdk_pixbuf_format_get_mime_types(format);
769 if (!mimev) return NULL;
771 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
772 mime = g_strdup(mimev[0]);
778 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
782 g_mutex_lock(il->data_mutex);
783 il->requested_width = width;
784 il->requested_height = height;
785 g_mutex_unlock(il->data_mutex);
788 void image_loader_set_buffer_size(ImageLoader *il, guint count)
792 g_mutex_lock(il->data_mutex);
793 il->idle_read_loop_count = count ? count : 1;
794 g_mutex_unlock(il->data_mutex);
797 void image_loader_set_priority(ImageLoader *il, gint priority)
801 g_mutex_lock(il->data_mutex);
802 il->idle_priority = priority;
803 g_mutex_unlock(il->data_mutex);
807 gdouble image_loader_get_percent(ImageLoader *il)
812 g_mutex_lock(il->data_mutex);
813 if (il->bytes_total == 0)
819 ret = (gdouble)il->bytes_read / il->bytes_total;
821 g_mutex_unlock(il->data_mutex);
825 gint image_loader_get_is_done(ImageLoader *il)
828 if (!il) return FALSE;
830 g_mutex_lock(il->data_mutex);
832 g_mutex_unlock(il->data_mutex);
837 FileData *image_loader_get_fd(ImageLoader *il)
840 if (!il) return NULL;
842 g_mutex_lock(il->data_mutex);
844 g_mutex_unlock(il->data_mutex);
849 gint image_loader_get_shrunk(ImageLoader *il)
852 if (!il) return FALSE;
854 g_mutex_lock(il->data_mutex);
856 g_mutex_unlock(il->data_mutex);
861 /* FIXME - this can be rather slow and blocks until the size is known */
862 gint image_load_dimensions(FileData *fd, gint *width, gint *height)
867 il = image_loader_new(fd);
869 success = image_loader_start_idle(il);
871 if (success && il->pixbuf)
873 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
874 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
878 if (width) *width = -1;
879 if (height) *height = -1;
882 image_loader_free(il);