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) g_object_unref(il->pixbuf);
177 file_data_unref(il->fd);
179 g_mutex_free(il->data_mutex);
180 g_cond_free(il->can_destroy_cond);
184 void image_loader_free(ImageLoader *il)
187 g_object_unref(G_OBJECT(il));
191 ImageLoader *image_loader_new(FileData *fd)
195 if (!fd) return NULL;
197 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
199 il->fd = file_data_ref(fd);
204 /**************************************************************************************/
205 /* send signals via idle calbacks - the callback are executed in the main thread */
207 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
208 struct _ImageLoaderAreaParam {
217 static gint image_loader_emit_area_ready_cb(gpointer data)
219 ImageLoaderAreaParam *par = data;
220 ImageLoader *il = par->il;
221 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
222 g_mutex_lock(il->data_mutex);
223 il->area_param_list = g_list_remove(il->area_param_list, par);
225 g_mutex_unlock(il->data_mutex);
230 static gint image_loader_emit_done_cb(gpointer data)
232 ImageLoader *il = data;
233 g_signal_emit(il, signals[SIGNAL_DONE], 0);
237 static gint image_loader_emit_error_cb(gpointer data)
239 ImageLoader *il = data;
240 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
244 static gint image_loader_emit_percent_cb(gpointer data)
246 ImageLoader *il = data;
247 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
251 /* DONE and ERROR are emited only once, thus they can have normal priority
252 PERCENT and AREA_READY should be processed ASAP
255 static void image_loader_emit_done(ImageLoader *il)
257 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
260 static void image_loader_emit_error(ImageLoader *il)
262 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
265 static void image_loader_emit_percent(ImageLoader *il)
267 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
270 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
272 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
279 g_mutex_lock(il->data_mutex);
280 il->area_param_list = g_list_prepend(il->area_param_list, par);
281 g_mutex_unlock(il->data_mutex);
283 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
286 /**************************************************************************************/
287 /* the following functions may be executed in separate thread */
289 static gint image_loader_get_stopping(ImageLoader *il)
292 if (!il) return FALSE;
294 g_mutex_lock(il->data_mutex);
296 g_mutex_unlock(il->data_mutex);
301 static void image_loader_sync_pixbuf(ImageLoader *il)
305 g_mutex_lock(il->data_mutex);
309 g_mutex_unlock(il->data_mutex);
313 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
315 if (pb == il->pixbuf)
317 g_mutex_unlock(il->data_mutex);
321 if (il->pixbuf) g_object_unref(il->pixbuf);
324 if (il->pixbuf) g_object_ref(il->pixbuf);
326 g_mutex_unlock(il->data_mutex);
329 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
330 guint x, guint y, guint w, guint h,
333 ImageLoader *il = data;
335 if (!image_loader_get_pixbuf(il))
337 image_loader_sync_pixbuf(il);
338 if (!image_loader_get_pixbuf(il))
340 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
343 image_loader_emit_area_ready(il, x, y, w, h);
346 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
352 /* a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669 */
353 gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
354 if (strcmp(format, "svg") == 0)
362 pb = gdk_pixbuf_loader_get_pixbuf(loader);
364 h = gdk_pixbuf_get_height(pb);
365 rs = gdk_pixbuf_get_rowstride(pb);
366 pix = gdk_pixbuf_get_pixels(pb);
368 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
372 static void image_loader_size_cb(GdkPixbufLoader *loader,
373 gint width, gint height, gpointer data)
375 ImageLoader *il = data;
376 GdkPixbufFormat *format;
381 g_mutex_lock(il->data_mutex);
382 if (il->requested_width < 1 || il->requested_height < 1)
384 g_mutex_unlock(il->data_mutex);
387 g_mutex_unlock(il->data_mutex);
389 format = gdk_pixbuf_loader_get_format(loader);
392 mime_types = gdk_pixbuf_format_get_mime_types(format);
394 while (mime_types[n])
396 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
399 g_strfreev(mime_types);
403 g_mutex_lock(il->data_mutex);
405 if (width > il->requested_width || height > il->requested_height)
409 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
411 nw = il->requested_width;
412 nh = (gdouble)nw / width * height;
417 nh = il->requested_height;
418 nw = (gdouble)nh / height * width;
422 gdk_pixbuf_loader_set_size(loader, nw, nh);
425 g_mutex_unlock(il->data_mutex);
429 static void image_loader_stop_loader(ImageLoader *il)
435 /* some loaders do not have a pixbuf till close, order is important here */
436 gdk_pixbuf_loader_close(il->loader, NULL);
437 image_loader_sync_pixbuf(il);
438 g_object_unref(G_OBJECT(il->loader));
441 g_mutex_lock(il->data_mutex);
443 g_mutex_unlock(il->data_mutex);
446 static void image_loader_setup_loader(ImageLoader *il)
448 g_mutex_lock(il->data_mutex);
449 il->loader = gdk_pixbuf_loader_new();
451 g_signal_connect(G_OBJECT(il->loader), "area_updated",
452 G_CALLBACK(image_loader_area_updated_cb), il);
453 g_signal_connect(G_OBJECT(il->loader), "size_prepared",
454 G_CALLBACK(image_loader_size_cb), il);
455 g_signal_connect(G_OBJECT(il->loader), "area_prepared",
456 G_CALLBACK(image_loader_area_prepared_cb), il);
457 g_mutex_unlock(il->data_mutex);
461 static void image_loader_done(ImageLoader *il)
463 image_loader_stop_loader(il);
465 image_loader_emit_done(il);
468 static void image_loader_error(ImageLoader *il)
470 image_loader_stop_loader(il);
472 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
474 image_loader_emit_error(il);
477 static gint image_loader_continue(ImageLoader *il)
482 if (!il) return FALSE;
484 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
485 while (c > 0 && !image_loader_get_stopping(il))
487 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
491 image_loader_done(il);
495 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
497 image_loader_error(il);
506 if (il->bytes_total > 0)
508 image_loader_emit_percent(il);
514 static gint image_loader_begin(ImageLoader *il)
518 if (il->pixbuf) return FALSE;
520 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
521 if (b < 1) return FALSE;
523 image_loader_setup_loader(il);
525 if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL))
527 image_loader_stop_loader(il);
533 /* read until size is known */
534 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
536 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
537 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
539 image_loader_stop_loader(il);
544 if (!il->pixbuf) image_loader_sync_pixbuf(il);
546 if (il->bytes_read == il->bytes_total || b < 1)
548 /* done, handle (broken) loaders that do not have pixbuf till close */
549 image_loader_stop_loader(il);
551 if (!il->pixbuf) return FALSE;
553 image_loader_done(il);
559 image_loader_stop_loader(il);
566 /**************************************************************************************/
567 /* the following functions are always executed in the main thread */
570 static gint image_loader_setup_source(ImageLoader *il)
575 if (!il || il->loader || il->mapped_file) return FALSE;
577 il->mapped_file = NULL;
581 ExifData *exif = exif_read_fd(il->fd);
583 il->mapped_file = exif_get_preview(exif, &il->bytes_total);
588 DEBUG_1("Raw file %s contains embedded image", il->fd->path);
590 exif_free_fd(il->fd, exif);
594 if (!il->mapped_file)
599 pathl = path_from_utf8(il->fd->path);
600 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
602 if (load_fd == -1) return FALSE;
604 if (fstat(load_fd, &st) == 0)
606 il->bytes_total = st.st_size;
614 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
616 if (il->mapped_file == MAP_FAILED)
627 static void image_loader_stop_source(ImageLoader *il)
635 exif_free_preview(il->mapped_file);
639 munmap(il->mapped_file, il->bytes_total);
641 il->mapped_file = NULL;
645 static void image_loader_stop(ImageLoader *il)
649 if (il->idle_id != -1)
651 g_source_remove(il->idle_id);
657 /* stop loader in the other thread */
658 g_mutex_lock(il->data_mutex);
660 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
661 g_mutex_unlock(il->data_mutex);
664 image_loader_stop_loader(il);
665 image_loader_stop_source(il);
669 /**************************************************************************************/
670 /* execution via idle calls */
672 static gint image_loader_idle_cb(gpointer data)
675 ImageLoader *il = data;
676 if (il->idle_id != -1)
678 ret = image_loader_continue(il);
682 image_loader_stop_source(il);
688 gint image_loader_start_idle(ImageLoader *il)
691 if (!il) return FALSE;
693 if (!il->fd) return FALSE;
695 if (!image_loader_setup_source(il)) return FALSE;
697 ret = image_loader_begin(il);
699 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
703 /**************************************************************************************/
704 /* execution via thread */
705 static GThreadPool *image_loader_thread_pool = NULL;
707 static GCond *image_loader_prio_cond = NULL;
708 static GMutex *image_loader_prio_mutex = NULL;
709 static gint image_loader_prio_num = 0;
712 static void image_loader_thread_enter_high()
714 g_mutex_lock(image_loader_prio_mutex);
715 image_loader_prio_num++;
716 g_mutex_unlock(image_loader_prio_mutex);
719 static void image_loader_thread_leave_high()
721 g_mutex_lock(image_loader_prio_mutex);
722 image_loader_prio_num--;
723 if (image_loader_prio_num == 0) g_cond_signal(image_loader_prio_cond);
724 g_mutex_unlock(image_loader_prio_mutex);
727 static void image_loader_thread_wait_high()
729 g_mutex_lock(image_loader_prio_mutex);
730 while (image_loader_prio_num)
732 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
735 g_mutex_unlock(image_loader_prio_mutex);
739 void image_loader_thread_run(gpointer data, gpointer user_data)
741 ImageLoader *il = data;
744 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
746 /* low prio, wait untill high prio tasks finishes */
747 image_loader_thread_wait_high();
752 image_loader_thread_enter_high();
755 cont = image_loader_begin(il);
757 if (!cont && !image_loader_get_pixbuf(il))
760 loader failed, we have to send signal
761 (idle mode returns the image_loader_begin return value directly)
762 (success is always reported indirectly from image_loader_begin)
764 image_loader_emit_error(il);
767 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
769 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
771 /* low prio, wait untill high prio tasks finishes */
772 image_loader_thread_wait_high();
774 cont = image_loader_continue(il);
776 image_loader_stop_loader(il);
778 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
781 image_loader_thread_leave_high();
784 g_mutex_lock(il->data_mutex);
785 il->can_destroy = TRUE;
786 g_cond_signal(il->can_destroy_cond);
787 g_mutex_unlock(il->data_mutex);
792 gint image_loader_start_thread(ImageLoader *il)
794 if (!il) return FALSE;
796 if (!il->fd) return FALSE;
800 if (!image_loader_setup_source(il)) return FALSE;
802 if (!image_loader_thread_pool)
804 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
805 image_loader_prio_cond = g_cond_new();
806 image_loader_prio_mutex = g_mutex_new();
809 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
811 g_thread_pool_push(image_loader_thread_pool, il, NULL);
812 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
818 /**************************************************************************************/
819 /* public interface */
822 gint image_loader_start(ImageLoader *il)
824 if (!il) return FALSE;
826 if (!il->fd) return FALSE;
829 return image_loader_start_thread(il);
831 return image_loader_start_idle(il);
836 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
837 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
840 if (!il) return NULL;
842 g_mutex_lock(il->data_mutex);
844 g_mutex_unlock(il->data_mutex);
848 gchar *image_loader_get_format(ImageLoader *il)
850 GdkPixbufFormat *format;
854 if (!il || !il->loader) return NULL;
856 format = gdk_pixbuf_loader_get_format(il->loader);
857 if (!format) return NULL;
859 mimev = gdk_pixbuf_format_get_mime_types(format);
860 if (!mimev) return NULL;
862 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
863 mime = g_strdup(mimev[0]);
869 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
873 g_mutex_lock(il->data_mutex);
874 il->requested_width = width;
875 il->requested_height = height;
876 g_mutex_unlock(il->data_mutex);
879 void image_loader_set_buffer_size(ImageLoader *il, guint count)
883 g_mutex_lock(il->data_mutex);
884 il->idle_read_loop_count = count ? count : 1;
885 g_mutex_unlock(il->data_mutex);
888 void image_loader_set_priority(ImageLoader *il, gint priority)
892 if (il->thread) return; /* can't change prio if the thread already runs */
893 il->idle_priority = priority;
897 gdouble image_loader_get_percent(ImageLoader *il)
902 g_mutex_lock(il->data_mutex);
903 if (il->bytes_total == 0)
909 ret = (gdouble)il->bytes_read / il->bytes_total;
911 g_mutex_unlock(il->data_mutex);
915 gint image_loader_get_is_done(ImageLoader *il)
918 if (!il) return FALSE;
920 g_mutex_lock(il->data_mutex);
922 g_mutex_unlock(il->data_mutex);
927 FileData *image_loader_get_fd(ImageLoader *il)
930 if (!il) return NULL;
932 g_mutex_lock(il->data_mutex);
934 g_mutex_unlock(il->data_mutex);
939 gint image_loader_get_shrunk(ImageLoader *il)
942 if (!il) return FALSE;
944 g_mutex_lock(il->data_mutex);
946 g_mutex_unlock(il->data_mutex);
951 /* FIXME - this can be rather slow and blocks until the size is known */
952 gint image_load_dimensions(FileData *fd, gint *width, gint *height)
957 il = image_loader_new(fd);
959 success = image_loader_start_idle(il);
961 if (success && il->pixbuf)
963 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
964 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
968 if (width) *width = -1;
969 if (height) *height = -1;
972 image_loader_free(il);