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 */
61 type = g_type_register_static (G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
66 static void image_loader_init (GTypeInstance *instance, gpointer g_class)
68 ImageLoader *il = (ImageLoader *)instance;
72 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
79 il->idle_done_id = -1;
81 il->idle_read_loop_count = options->image.idle_read_loop_count;
82 il->read_buffer_size = options->image.read_buffer_size;
83 il->mapped_file = NULL;
85 il->requested_width = 0;
86 il->requested_height = 0;
90 il->data_mutex = g_mutex_new();
92 DEBUG_1("new image loader %p, bufsize=%u idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
95 static void image_loader_class_init (ImageLoaderClass *class)
97 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
99 // gobject_class->set_property = image_loader_set_property;
100 // gobject_class->get_property = image_loader_get_property;
102 gobject_class->finalize = image_loader_finalize;
105 signals[SIGNAL_AREA_READY] =
106 g_signal_new("area_ready",
107 G_OBJECT_CLASS_TYPE(gobject_class),
109 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
111 gq_marshal_VOID__INT_INT_INT_INT,
118 signals[SIGNAL_ERROR] =
119 g_signal_new("error",
120 G_OBJECT_CLASS_TYPE(gobject_class),
122 G_STRUCT_OFFSET(ImageLoaderClass, error),
124 g_cclosure_marshal_VOID__VOID,
128 signals[SIGNAL_DONE] =
130 G_OBJECT_CLASS_TYPE(gobject_class),
132 G_STRUCT_OFFSET(ImageLoaderClass, done),
134 g_cclosure_marshal_VOID__VOID,
137 signals[SIGNAL_PERCENT] =
138 g_signal_new("percent",
139 G_OBJECT_CLASS_TYPE(gobject_class),
141 G_STRUCT_OFFSET(ImageLoaderClass, percent),
143 g_cclosure_marshal_VOID__DOUBLE,
149 static void image_loader_finalize(GObject *object)
151 ImageLoader *il = (ImageLoader *)object;
153 image_loader_stop(il);
155 DEBUG_1("freeing image loader %p bytes_read=%d", il, il->bytes_read);
157 if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
159 while (g_source_remove_by_user_data(il))
161 DEBUG_1("pending signals detected");
164 while (il->area_param_list)
166 DEBUG_1("pending area_ready signals detected");
167 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
168 g_free(il->area_param_list->data);
169 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
172 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
173 file_data_unref(il->fd);
175 g_mutex_free(il->data_mutex);
179 void image_loader_free(ImageLoader *il)
182 g_object_unref(G_OBJECT(il));
186 ImageLoader *image_loader_new(FileData *fd)
190 if (!fd) return NULL;
192 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
194 il->fd = file_data_ref(fd);
199 /**************************************************************************************/
200 /* send signals via idle calbacks - the callback are executed in the main thread */
202 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
203 struct _ImageLoaderAreaParam {
212 static gint image_loader_emit_area_ready_cb(gpointer data)
214 ImageLoaderAreaParam *par = data;
215 ImageLoader *il = par->il;
216 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
217 g_mutex_lock(il->data_mutex);
218 il->area_param_list = g_list_remove(il->area_param_list, par);
220 g_mutex_unlock(il->data_mutex);
225 static gint image_loader_emit_done_cb(gpointer data)
227 ImageLoader *il = data;
228 g_signal_emit(il, signals[SIGNAL_DONE], 0);
232 static gint image_loader_emit_error_cb(gpointer data)
234 ImageLoader *il = data;
235 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
239 static gint image_loader_emit_percent_cb(gpointer data)
241 ImageLoader *il = data;
242 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
246 /* DONE and ERROR are emited only once, thus they can have normal priority
247 PERCENT and AREA_READY should be processed ASAP
250 static void image_loader_emit_done(ImageLoader *il)
252 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
255 static void image_loader_emit_error(ImageLoader *il)
257 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
260 static void image_loader_emit_percent(ImageLoader *il)
262 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
265 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
267 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
274 g_mutex_lock(il->data_mutex);
275 il->area_param_list = g_list_prepend(il->area_param_list, par);
276 g_mutex_unlock(il->data_mutex);
278 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
281 /**************************************************************************************/
282 /* the following functions may be executed in separate thread */
284 static void image_loader_sync_pixbuf(ImageLoader *il)
288 g_mutex_lock(il->data_mutex);
292 g_mutex_unlock(il->data_mutex);
296 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
298 if (pb == il->pixbuf)
300 g_mutex_unlock(il->data_mutex);
304 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
306 if (il->pixbuf) gdk_pixbuf_ref(il->pixbuf);
307 g_mutex_unlock(il->data_mutex);
310 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
311 guint x, guint y, guint w, guint h,
314 ImageLoader *il = data;
316 if (!image_loader_get_pixbuf(il))
318 image_loader_sync_pixbuf(il);
319 if (!image_loader_get_pixbuf(il))
321 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
324 image_loader_emit_area_ready(il, x, y, w, h);
327 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
333 /* a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669 */
334 gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
335 if (strcmp(format, "svg") == 0)
343 pb = gdk_pixbuf_loader_get_pixbuf(loader);
345 h = gdk_pixbuf_get_height(pb);
346 rs = gdk_pixbuf_get_rowstride(pb);
347 pix = gdk_pixbuf_get_pixels(pb);
349 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
353 static void image_loader_size_cb(GdkPixbufLoader *loader,
354 gint width, gint height, gpointer data)
356 ImageLoader *il = data;
357 GdkPixbufFormat *format;
362 g_mutex_lock(il->data_mutex);
363 if (il->requested_width < 1 || il->requested_height < 1)
365 g_mutex_unlock(il->data_mutex);
368 g_mutex_unlock(il->data_mutex);
370 format = gdk_pixbuf_loader_get_format(loader);
373 mime_types = gdk_pixbuf_format_get_mime_types(format);
375 while (mime_types[n])
377 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
380 g_strfreev(mime_types);
384 g_mutex_lock(il->data_mutex);
386 if (width > il->requested_width || height > il->requested_height)
390 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
392 nw = il->requested_width;
393 nh = (gdouble)nw / width * height;
398 nh = il->requested_height;
399 nw = (gdouble)nh / height * width;
403 gdk_pixbuf_loader_set_size(loader, nw, nh);
406 g_mutex_unlock(il->data_mutex);
410 static void image_loader_stop_loader(ImageLoader *il)
416 /* some loaders do not have a pixbuf till close, order is important here */
417 gdk_pixbuf_loader_close(il->loader, NULL);
418 image_loader_sync_pixbuf(il);
419 g_object_unref(G_OBJECT(il->loader));
426 static void image_loader_setup_loader(ImageLoader *il)
428 g_mutex_lock(il->data_mutex);
429 il->loader = gdk_pixbuf_loader_new();
430 g_mutex_unlock(il->data_mutex);
432 g_signal_connect(G_OBJECT(il->loader), "area_updated",
433 G_CALLBACK(image_loader_area_updated_cb), il);
434 g_signal_connect(G_OBJECT(il->loader), "size_prepared",
435 G_CALLBACK(image_loader_size_cb), il);
436 g_signal_connect(G_OBJECT(il->loader), "area_prepared",
437 G_CALLBACK(image_loader_area_prepared_cb), il);
441 static void image_loader_done(ImageLoader *il)
443 image_loader_stop_loader(il);
445 image_loader_emit_done(il);
448 static void image_loader_error(ImageLoader *il)
450 image_loader_stop_loader(il);
452 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
454 image_loader_emit_error(il);
457 static gint image_loader_continue(ImageLoader *il)
462 if (!il) return FALSE;
464 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
467 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
471 image_loader_done(il);
475 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
477 image_loader_error(il);
486 if (il->bytes_total > 0)
488 image_loader_emit_percent(il);
494 static gint image_loader_begin(ImageLoader *il)
498 if (il->pixbuf) return FALSE;
500 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
501 if (b < 1) return FALSE;
503 image_loader_setup_loader(il);
505 if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL))
507 image_loader_stop_loader(il);
513 /* read until size is known */
514 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0)
516 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
517 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
519 image_loader_stop_loader(il);
524 if (!il->pixbuf) image_loader_sync_pixbuf(il);
526 if (il->bytes_read == il->bytes_total || b < 1)
528 /* done, handle (broken) loaders that do not have pixbuf till close */
529 image_loader_stop_loader(il);
531 if (!il->pixbuf) return FALSE;
533 image_loader_done(il);
539 image_loader_stop_loader(il);
546 /**************************************************************************************/
547 /* the following functions are always executed in the main thread */
550 static gint image_loader_setup_source(ImageLoader *il)
555 if (!il || il->loader || il->mapped_file) return FALSE;
557 il->mapped_file = NULL;
561 ExifData *exif = exif_read_fd(il->fd);
563 il->mapped_file = exif_get_preview(exif, &il->bytes_total);
568 DEBUG_1("Raw file %s contains embedded image", il->fd->path);
570 exif_free_fd(il->fd, exif);
574 if (!il->mapped_file)
579 pathl = path_from_utf8(il->fd->path);
580 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
582 if (load_fd == -1) return FALSE;
584 if (fstat(load_fd, &st) == 0)
586 il->bytes_total = st.st_size;
594 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
596 if (il->mapped_file == MAP_FAILED)
607 static void image_loader_stop_source(ImageLoader *il)
615 exif_free_preview(il->mapped_file);
619 munmap(il->mapped_file, il->bytes_total);
621 il->mapped_file = NULL;
627 static gint image_loader_setup(ImageLoader *il)
629 if (!image_loader_setup_source(il)) return FALSE;
631 return image_loader_begin(il);
635 static void image_loader_stop(ImageLoader *il)
639 if (il->idle_id != -1)
641 g_source_remove(il->idle_id);
648 g_thread_join(il->thread);
651 image_loader_stop_loader(il);
652 image_loader_stop_source(il);
656 /**************************************************************************************/
657 /* execution via idle calls */
659 static gint image_loader_idle_cb(gpointer data)
662 ImageLoader *il = data;
663 if (il->idle_id != -1)
665 ret = image_loader_continue(il);
669 image_loader_stop_source(il);
675 gint image_loader_start_idle(ImageLoader *il)
678 if (!il) return FALSE;
680 if (!il->fd) return FALSE;
682 if (!image_loader_setup_source(il)) return FALSE;
684 ret = image_loader_begin(il);
686 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
690 /**************************************************************************************/
691 /* execution via thread */
693 gpointer image_loader_thread_run(gpointer data)
695 ImageLoader *il = data;
696 gint cont = image_loader_begin(il);
698 while (cont && !il->done && !il->stopping)
700 cont = image_loader_continue(il);
705 gint image_loader_start_thread(ImageLoader *il)
707 if (!il) return FALSE;
709 if (!il->fd) return FALSE;
711 if (!image_loader_setup_source(il)) return FALSE;
713 il->thread = g_thread_create(image_loader_thread_run, il, TRUE, NULL);
714 if (!il->thread) return FALSE;
720 /**************************************************************************************/
721 /* public interface */
724 gint image_loader_start(ImageLoader *il)
726 if (!il) return FALSE;
728 if (!il->fd) return FALSE;
731 return image_loader_start_thread(il);
733 return image_loader_start_idle(il);
738 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
739 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
742 if (!il) return NULL;
744 g_mutex_lock(il->data_mutex);
746 g_mutex_unlock(il->data_mutex);
750 gchar *image_loader_get_format(ImageLoader *il)
752 GdkPixbufFormat *format;
756 if (!il || !il->loader) return NULL;
758 format = gdk_pixbuf_loader_get_format(il->loader);
759 if (!format) return NULL;
761 mimev = gdk_pixbuf_format_get_mime_types(format);
762 if (!mimev) return NULL;
764 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
765 mime = g_strdup(mimev[0]);
771 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
775 g_mutex_lock(il->data_mutex);
776 il->requested_width = width;
777 il->requested_height = height;
778 g_mutex_unlock(il->data_mutex);
781 void image_loader_set_buffer_size(ImageLoader *il, guint count)
785 g_mutex_lock(il->data_mutex);
786 il->idle_read_loop_count = count ? count : 1;
787 g_mutex_unlock(il->data_mutex);
790 void image_loader_set_priority(ImageLoader *il, gint priority)
794 g_mutex_lock(il->data_mutex);
795 il->idle_priority = priority;
796 g_mutex_unlock(il->data_mutex);
800 gdouble image_loader_get_percent(ImageLoader *il)
805 g_mutex_lock(il->data_mutex);
806 if (il->bytes_total == 0)
812 ret = (gdouble)il->bytes_read / il->bytes_total;
814 g_mutex_unlock(il->data_mutex);
818 gint image_loader_get_is_done(ImageLoader *il)
821 if (!il) return FALSE;
823 g_mutex_lock(il->data_mutex);
825 g_mutex_unlock(il->data_mutex);
830 FileData *image_loader_get_fd(ImageLoader *il)
833 if (!il) return NULL;
835 g_mutex_lock(il->data_mutex);
837 g_mutex_unlock(il->data_mutex);
842 gint image_loader_get_shrunk(ImageLoader *il)
845 if (!il) return FALSE;
847 g_mutex_lock(il->data_mutex);
849 g_mutex_unlock(il->data_mutex);
854 /* FIXME - this can be rather slow and blocks until the size is known */
855 gint image_load_dimensions(FileData *fd, gint *width, gint *height)
860 il = image_loader_new(fd);
862 success = image_loader_start_idle(il);
864 if (success && il->pixbuf)
866 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
867 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
871 if (width) *width = -1;
872 if (height) *height = -1;
875 image_loader_free(il);