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();
92 il->can_destroy_cond = g_cond_new();
94 DEBUG_1("new image loader %p, bufsize=%u idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
97 static void image_loader_class_init (ImageLoaderClass *class)
99 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
101 // gobject_class->set_property = image_loader_set_property;
102 // gobject_class->get_property = image_loader_get_property;
104 gobject_class->finalize = image_loader_finalize;
107 signals[SIGNAL_AREA_READY] =
108 g_signal_new("area_ready",
109 G_OBJECT_CLASS_TYPE(gobject_class),
111 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
113 gq_marshal_VOID__INT_INT_INT_INT,
120 signals[SIGNAL_ERROR] =
121 g_signal_new("error",
122 G_OBJECT_CLASS_TYPE(gobject_class),
124 G_STRUCT_OFFSET(ImageLoaderClass, error),
126 g_cclosure_marshal_VOID__VOID,
130 signals[SIGNAL_DONE] =
132 G_OBJECT_CLASS_TYPE(gobject_class),
134 G_STRUCT_OFFSET(ImageLoaderClass, done),
136 g_cclosure_marshal_VOID__VOID,
139 signals[SIGNAL_PERCENT] =
140 g_signal_new("percent",
141 G_OBJECT_CLASS_TYPE(gobject_class),
143 G_STRUCT_OFFSET(ImageLoaderClass, percent),
145 g_cclosure_marshal_VOID__DOUBLE,
151 static void image_loader_finalize(GObject *object)
153 ImageLoader *il = (ImageLoader *)object;
155 image_loader_stop(il);
157 DEBUG_1("freeing image loader %p bytes_read=%d", il, il->bytes_read);
159 if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
161 while (g_source_remove_by_user_data(il))
163 DEBUG_1("pending signals detected");
166 while (il->area_param_list)
168 DEBUG_1("pending area_ready signals detected");
169 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
170 g_free(il->area_param_list->data);
171 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
174 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
175 file_data_unref(il->fd);
177 g_mutex_free(il->data_mutex);
178 g_cond_free(il->can_destroy_cond);
182 void image_loader_free(ImageLoader *il)
185 g_object_unref(G_OBJECT(il));
189 ImageLoader *image_loader_new(FileData *fd)
193 if (!fd) return NULL;
195 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
197 il->fd = file_data_ref(fd);
202 /**************************************************************************************/
203 /* send signals via idle calbacks - the callback are executed in the main thread */
205 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
206 struct _ImageLoaderAreaParam {
215 static gint image_loader_emit_area_ready_cb(gpointer data)
217 ImageLoaderAreaParam *par = data;
218 ImageLoader *il = par->il;
219 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
220 g_mutex_lock(il->data_mutex);
221 il->area_param_list = g_list_remove(il->area_param_list, par);
223 g_mutex_unlock(il->data_mutex);
228 static gint image_loader_emit_done_cb(gpointer data)
230 ImageLoader *il = data;
231 g_signal_emit(il, signals[SIGNAL_DONE], 0);
235 static gint image_loader_emit_error_cb(gpointer data)
237 ImageLoader *il = data;
238 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
242 static gint image_loader_emit_percent_cb(gpointer data)
244 ImageLoader *il = data;
245 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
249 /* DONE and ERROR are emited only once, thus they can have normal priority
250 PERCENT and AREA_READY should be processed ASAP
253 static void image_loader_emit_done(ImageLoader *il)
255 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
258 static void image_loader_emit_error(ImageLoader *il)
260 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
263 static void image_loader_emit_percent(ImageLoader *il)
265 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
268 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
270 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
277 g_mutex_lock(il->data_mutex);
278 il->area_param_list = g_list_prepend(il->area_param_list, par);
279 g_mutex_unlock(il->data_mutex);
281 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
284 /**************************************************************************************/
285 /* the following functions may be executed in separate thread */
287 static gint image_loader_get_stopping(ImageLoader *il)
290 if (!il) return FALSE;
292 g_mutex_lock(il->data_mutex);
294 g_mutex_unlock(il->data_mutex);
299 static void image_loader_sync_pixbuf(ImageLoader *il)
303 g_mutex_lock(il->data_mutex);
307 g_mutex_unlock(il->data_mutex);
311 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
313 if (pb == il->pixbuf)
315 g_mutex_unlock(il->data_mutex);
319 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
321 if (il->pixbuf) gdk_pixbuf_ref(il->pixbuf);
322 g_mutex_unlock(il->data_mutex);
325 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
326 guint x, guint y, guint w, guint h,
329 ImageLoader *il = data;
331 if (!image_loader_get_pixbuf(il))
333 image_loader_sync_pixbuf(il);
334 if (!image_loader_get_pixbuf(il))
336 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
339 image_loader_emit_area_ready(il, x, y, w, h);
342 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
348 /* a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669 */
349 gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
350 if (strcmp(format, "svg") == 0)
358 pb = gdk_pixbuf_loader_get_pixbuf(loader);
360 h = gdk_pixbuf_get_height(pb);
361 rs = gdk_pixbuf_get_rowstride(pb);
362 pix = gdk_pixbuf_get_pixels(pb);
364 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
368 static void image_loader_size_cb(GdkPixbufLoader *loader,
369 gint width, gint height, gpointer data)
371 ImageLoader *il = data;
372 GdkPixbufFormat *format;
377 g_mutex_lock(il->data_mutex);
378 if (il->requested_width < 1 || il->requested_height < 1)
380 g_mutex_unlock(il->data_mutex);
383 g_mutex_unlock(il->data_mutex);
385 format = gdk_pixbuf_loader_get_format(loader);
388 mime_types = gdk_pixbuf_format_get_mime_types(format);
390 while (mime_types[n])
392 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
395 g_strfreev(mime_types);
399 g_mutex_lock(il->data_mutex);
401 if (width > il->requested_width || height > il->requested_height)
405 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
407 nw = il->requested_width;
408 nh = (gdouble)nw / width * height;
413 nh = il->requested_height;
414 nw = (gdouble)nh / height * width;
418 gdk_pixbuf_loader_set_size(loader, nw, nh);
421 g_mutex_unlock(il->data_mutex);
425 static void image_loader_stop_loader(ImageLoader *il)
431 /* some loaders do not have a pixbuf till close, order is important here */
432 gdk_pixbuf_loader_close(il->loader, NULL);
433 image_loader_sync_pixbuf(il);
434 g_object_unref(G_OBJECT(il->loader));
437 g_mutex_lock(il->data_mutex);
439 g_mutex_unlock(il->data_mutex);
442 static void image_loader_setup_loader(ImageLoader *il)
444 g_mutex_lock(il->data_mutex);
445 il->loader = gdk_pixbuf_loader_new();
447 g_signal_connect(G_OBJECT(il->loader), "area_updated",
448 G_CALLBACK(image_loader_area_updated_cb), il);
449 g_signal_connect(G_OBJECT(il->loader), "size_prepared",
450 G_CALLBACK(image_loader_size_cb), il);
451 g_signal_connect(G_OBJECT(il->loader), "area_prepared",
452 G_CALLBACK(image_loader_area_prepared_cb), il);
453 g_mutex_unlock(il->data_mutex);
457 static void image_loader_done(ImageLoader *il)
459 image_loader_stop_loader(il);
461 image_loader_emit_done(il);
464 static void image_loader_error(ImageLoader *il)
466 image_loader_stop_loader(il);
468 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
470 image_loader_emit_error(il);
473 static gint image_loader_continue(ImageLoader *il)
478 if (!il) return FALSE;
480 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
481 while (c > 0 && !image_loader_get_stopping(il))
483 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
487 image_loader_done(il);
491 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
493 image_loader_error(il);
502 if (il->bytes_total > 0)
504 image_loader_emit_percent(il);
510 static gint image_loader_begin(ImageLoader *il)
514 if (il->pixbuf) return FALSE;
516 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
517 if (b < 1) return FALSE;
519 image_loader_setup_loader(il);
521 if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL))
523 image_loader_stop_loader(il);
529 /* read until size is known */
530 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
532 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
533 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
535 image_loader_stop_loader(il);
540 if (!il->pixbuf) image_loader_sync_pixbuf(il);
542 if (il->bytes_read == il->bytes_total || b < 1)
544 /* done, handle (broken) loaders that do not have pixbuf till close */
545 image_loader_stop_loader(il);
547 if (!il->pixbuf) return FALSE;
549 image_loader_done(il);
555 image_loader_stop_loader(il);
562 /**************************************************************************************/
563 /* the following functions are always executed in the main thread */
566 static gint image_loader_setup_source(ImageLoader *il)
571 if (!il || il->loader || il->mapped_file) return FALSE;
573 il->mapped_file = NULL;
577 ExifData *exif = exif_read_fd(il->fd);
579 il->mapped_file = exif_get_preview(exif, &il->bytes_total);
584 DEBUG_1("Raw file %s contains embedded image", il->fd->path);
586 exif_free_fd(il->fd, exif);
590 if (!il->mapped_file)
595 pathl = path_from_utf8(il->fd->path);
596 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
598 if (load_fd == -1) return FALSE;
600 if (fstat(load_fd, &st) == 0)
602 il->bytes_total = st.st_size;
610 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
612 if (il->mapped_file == MAP_FAILED)
623 static void image_loader_stop_source(ImageLoader *il)
631 exif_free_preview(il->mapped_file);
635 munmap(il->mapped_file, il->bytes_total);
637 il->mapped_file = NULL;
641 static void image_loader_stop(ImageLoader *il)
645 if (il->idle_id != -1)
647 g_source_remove(il->idle_id);
653 /* stop loader in the other thread */
654 g_mutex_lock(il->data_mutex);
656 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
657 g_mutex_unlock(il->data_mutex);
660 image_loader_stop_loader(il);
661 image_loader_stop_source(il);
665 /**************************************************************************************/
666 /* execution via idle calls */
668 static gint image_loader_idle_cb(gpointer data)
671 ImageLoader *il = data;
672 if (il->idle_id != -1)
674 ret = image_loader_continue(il);
678 image_loader_stop_source(il);
684 gint image_loader_start_idle(ImageLoader *il)
687 if (!il) return FALSE;
689 if (!il->fd) return FALSE;
691 if (!image_loader_setup_source(il)) return FALSE;
693 ret = image_loader_begin(il);
695 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
699 /**************************************************************************************/
700 /* execution via thread */
701 static GThreadPool *image_loader_thread_pool = NULL;
703 void image_loader_thread_run(gpointer data, gpointer user_data)
705 ImageLoader *il = data;
706 gint cont = image_loader_begin(il);
708 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
710 cont = image_loader_continue(il);
712 image_loader_stop_loader(il);
714 g_mutex_lock(il->data_mutex);
715 il->can_destroy = TRUE;
716 g_cond_signal(il->can_destroy_cond);
717 g_mutex_unlock(il->data_mutex);
722 gint image_loader_start_thread(ImageLoader *il)
724 if (!il) return FALSE;
726 if (!il->fd) return FALSE;
730 if (!image_loader_setup_source(il)) return FALSE;
732 if (!image_loader_thread_pool)
734 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
737 g_thread_pool_push(image_loader_thread_pool, il, NULL);
738 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
744 /**************************************************************************************/
745 /* public interface */
748 gint image_loader_start(ImageLoader *il)
750 if (!il) return FALSE;
752 if (!il->fd) return FALSE;
755 return image_loader_start_thread(il);
757 return image_loader_start_idle(il);
762 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
763 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
766 if (!il) return NULL;
768 g_mutex_lock(il->data_mutex);
770 g_mutex_unlock(il->data_mutex);
774 gchar *image_loader_get_format(ImageLoader *il)
776 GdkPixbufFormat *format;
780 if (!il || !il->loader) return NULL;
782 format = gdk_pixbuf_loader_get_format(il->loader);
783 if (!format) return NULL;
785 mimev = gdk_pixbuf_format_get_mime_types(format);
786 if (!mimev) return NULL;
788 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
789 mime = g_strdup(mimev[0]);
795 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
799 g_mutex_lock(il->data_mutex);
800 il->requested_width = width;
801 il->requested_height = height;
802 g_mutex_unlock(il->data_mutex);
805 void image_loader_set_buffer_size(ImageLoader *il, guint count)
809 g_mutex_lock(il->data_mutex);
810 il->idle_read_loop_count = count ? count : 1;
811 g_mutex_unlock(il->data_mutex);
814 void image_loader_set_priority(ImageLoader *il, gint priority)
818 g_mutex_lock(il->data_mutex);
819 il->idle_priority = priority;
820 g_mutex_unlock(il->data_mutex);
824 gdouble image_loader_get_percent(ImageLoader *il)
829 g_mutex_lock(il->data_mutex);
830 if (il->bytes_total == 0)
836 ret = (gdouble)il->bytes_read / il->bytes_total;
838 g_mutex_unlock(il->data_mutex);
842 gint image_loader_get_is_done(ImageLoader *il)
845 if (!il) return FALSE;
847 g_mutex_lock(il->data_mutex);
849 g_mutex_unlock(il->data_mutex);
854 FileData *image_loader_get_fd(ImageLoader *il)
857 if (!il) return NULL;
859 g_mutex_lock(il->data_mutex);
861 g_mutex_unlock(il->data_mutex);
866 gint image_loader_get_shrunk(ImageLoader *il)
869 if (!il) return FALSE;
871 g_mutex_lock(il->data_mutex);
873 g_mutex_unlock(il->data_mutex);
878 /* FIXME - this can be rather slow and blocks until the size is known */
879 gint image_load_dimensions(FileData *fd, gint *width, gint *height)
884 il = image_loader_new(fd);
886 success = image_loader_start_idle(il);
888 if (success && il->pixbuf)
890 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
891 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
895 if (width) *width = -1;
896 if (height) *height = -1;
899 image_loader_free(il);