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;
88 DEBUG_1("new image loader %p, bufsize=%u idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
91 static void image_loader_class_init (ImageLoaderClass *class)
93 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
95 // gobject_class->set_property = image_loader_set_property;
96 // gobject_class->get_property = image_loader_get_property;
98 gobject_class->finalize = image_loader_finalize;
101 signals[SIGNAL_AREA_READY] =
102 g_signal_new("area_ready",
103 G_OBJECT_CLASS_TYPE(gobject_class),
105 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
107 gq_marshal_VOID__INT_INT_INT_INT,
114 signals[SIGNAL_ERROR] =
115 g_signal_new("error",
116 G_OBJECT_CLASS_TYPE(gobject_class),
118 G_STRUCT_OFFSET(ImageLoaderClass, error),
120 g_cclosure_marshal_VOID__VOID,
124 signals[SIGNAL_DONE] =
126 G_OBJECT_CLASS_TYPE(gobject_class),
128 G_STRUCT_OFFSET(ImageLoaderClass, done),
130 g_cclosure_marshal_VOID__VOID,
133 signals[SIGNAL_PERCENT] =
134 g_signal_new("percent",
135 G_OBJECT_CLASS_TYPE(gobject_class),
137 G_STRUCT_OFFSET(ImageLoaderClass, percent),
139 g_cclosure_marshal_VOID__DOUBLE,
145 static void image_loader_finalize(GObject *object)
147 ImageLoader *il = (ImageLoader *)object;
149 image_loader_stop(il);
151 DEBUG_1("freeing image loader %p bytes_read=%d", il, il->bytes_read);
153 if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
155 while (g_source_remove_by_user_data(il))
157 DEBUG_1("pending signals detected");
160 while (il->area_param_list)
162 DEBUG_1("pending area_ready signals detected");
163 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
164 g_free(il->area_param_list->data);
165 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
168 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
169 file_data_unref(il->fd);
172 void image_loader_free(ImageLoader *il)
175 g_object_unref(G_OBJECT(il));
179 ImageLoader *image_loader_new(FileData *fd)
183 if (!fd) return NULL;
185 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
187 il->fd = file_data_ref(fd);
192 /**************************************************************************************/
193 /* send signals via idle calbacks - the callback are executed in the main thread */
195 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
196 struct _ImageLoaderAreaParam {
205 static gint image_loader_emit_area_ready_cb(gpointer data)
207 ImageLoaderAreaParam *par = data;
208 ImageLoader *il = par->il;
209 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
210 il->area_param_list = g_list_remove(il->area_param_list, par);
216 static gint image_loader_emit_done_cb(gpointer data)
218 ImageLoader *il = data;
219 g_signal_emit(il, signals[SIGNAL_DONE], 0);
223 static gint image_loader_emit_error_cb(gpointer data)
225 ImageLoader *il = data;
226 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
230 static gint image_loader_emit_percent_cb(gpointer data)
232 ImageLoader *il = data;
233 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, (gdouble)il->bytes_read / il->bytes_total);
237 /* DONE and ERROR are emited only once, thus they can have normal priority
238 PERCENT and AREA_READY should be processed ASAP
241 static void image_loader_emit_done(ImageLoader *il)
243 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
246 static void image_loader_emit_error(ImageLoader *il)
248 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
251 static void image_loader_emit_percent(ImageLoader *il)
253 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
256 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
258 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
265 il->area_param_list = g_list_prepend(il->area_param_list, par);
266 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
269 /**************************************************************************************/
270 /* the following functions may be executed in separate thread */
272 static void image_loader_sync_pixbuf(ImageLoader *il)
276 if (!il->loader) return;
278 pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
280 if (pb == il->pixbuf) return;
282 if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
284 if (il->pixbuf) gdk_pixbuf_ref(il->pixbuf);
287 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
288 guint x, guint y, guint w, guint h,
291 ImageLoader *il = data;
295 image_loader_sync_pixbuf(il);
298 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
301 image_loader_emit_area_ready(il, x, y, w, h);
304 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
310 /* a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669 */
311 gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
312 if (strcmp(format, "svg") == 0)
320 pb = gdk_pixbuf_loader_get_pixbuf(loader);
322 h = gdk_pixbuf_get_height(pb);
323 rs = gdk_pixbuf_get_rowstride(pb);
324 pix = gdk_pixbuf_get_pixels(pb);
326 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
330 static void image_loader_size_cb(GdkPixbufLoader *loader,
331 gint width, gint height, gpointer data)
333 ImageLoader *il = data;
334 GdkPixbufFormat *format;
339 if (il->requested_width < 1 || il->requested_height < 1) return;
341 format = gdk_pixbuf_loader_get_format(loader);
344 mime_types = gdk_pixbuf_format_get_mime_types(format);
346 while (mime_types[n])
348 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
351 g_strfreev(mime_types);
355 if (width > il->requested_width || height > il->requested_height)
359 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
361 nw = il->requested_width;
362 nh = (gdouble)nw / width * height;
367 nh = il->requested_height;
368 nw = (gdouble)nh / height * width;
372 gdk_pixbuf_loader_set_size(loader, nw, nh);
377 static void image_loader_stop_loader(ImageLoader *il)
383 /* some loaders do not have a pixbuf till close, order is important here */
384 gdk_pixbuf_loader_close(il->loader, NULL);
385 image_loader_sync_pixbuf(il);
386 g_object_unref(G_OBJECT(il->loader));
393 static void image_loader_setup_loader(ImageLoader *il)
395 il->loader = gdk_pixbuf_loader_new();
396 g_signal_connect(G_OBJECT(il->loader), "area_updated",
397 G_CALLBACK(image_loader_area_updated_cb), il);
398 g_signal_connect(G_OBJECT(il->loader), "size_prepared",
399 G_CALLBACK(image_loader_size_cb), il);
400 g_signal_connect(G_OBJECT(il->loader), "area_prepared",
401 G_CALLBACK(image_loader_area_prepared_cb), il);
405 static void image_loader_done(ImageLoader *il)
407 image_loader_stop_loader(il);
409 image_loader_emit_done(il);
412 static void image_loader_error(ImageLoader *il)
414 image_loader_stop_loader(il);
416 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
418 image_loader_emit_error(il);
421 static gint image_loader_continue(ImageLoader *il)
426 if (!il) return FALSE;
428 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
431 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
435 image_loader_done(il);
439 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
441 image_loader_error(il);
450 if (il->bytes_total > 0)
452 image_loader_emit_percent(il);
458 static gint image_loader_begin(ImageLoader *il)
462 if (il->pixbuf) return FALSE;
464 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
465 if (b < 1) return FALSE;
467 image_loader_setup_loader(il);
469 if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL))
471 image_loader_stop_loader(il);
477 /* read until size is known */
478 while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0)
480 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
481 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
483 image_loader_stop_loader(il);
488 if (!il->pixbuf) image_loader_sync_pixbuf(il);
490 if (il->bytes_read == il->bytes_total || b < 1)
492 /* done, handle (broken) loaders that do not have pixbuf till close */
493 image_loader_stop_loader(il);
495 if (!il->pixbuf) return FALSE;
497 image_loader_done(il);
503 image_loader_stop_loader(il);
510 /**************************************************************************************/
511 /* the following functions are always executed in the main thread */
514 static gint image_loader_setup_source(ImageLoader *il)
519 if (!il || il->loader || il->mapped_file) return FALSE;
521 il->mapped_file = NULL;
525 ExifData *exif = exif_read_fd(il->fd);
527 il->mapped_file = exif_get_preview(exif, &il->bytes_total);
532 DEBUG_1("Raw file %s contains embedded image", il->fd->path);
534 exif_free_fd(il->fd, exif);
538 if (!il->mapped_file)
543 pathl = path_from_utf8(il->fd->path);
544 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
546 if (load_fd == -1) return FALSE;
548 if (fstat(load_fd, &st) == 0)
550 il->bytes_total = st.st_size;
558 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
560 if (il->mapped_file == MAP_FAILED)
571 static void image_loader_stop_source(ImageLoader *il)
579 exif_free_preview(il->mapped_file);
583 munmap(il->mapped_file, il->bytes_total);
585 il->mapped_file = NULL;
591 static gint image_loader_setup(ImageLoader *il)
593 if (!image_loader_setup_source(il)) return FALSE;
595 return image_loader_begin(il);
599 static void image_loader_stop(ImageLoader *il)
603 if (il->idle_id != -1)
605 g_source_remove(il->idle_id);
610 image_loader_stop_loader(il);
611 image_loader_stop_source(il);
615 /**************************************************************************************/
616 /* execution via idle calls */
618 static gint image_loader_idle_cb(gpointer data)
621 ImageLoader *il = data;
622 if (il->idle_id != -1)
624 ret = image_loader_continue(il);
628 image_loader_stop_source(il);
634 gint image_loader_start_idle(ImageLoader *il)
637 if (!il) return FALSE;
639 if (!il->fd) return FALSE;
641 if (!image_loader_setup_source(il)) return FALSE;
643 ret = image_loader_begin(il);
645 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
649 /**************************************************************************************/
650 /* public interface */
653 gint image_loader_start(ImageLoader *il)
655 if (!il) return FALSE;
657 if (!il->fd) return FALSE;
659 return image_loader_start_idle(il);
663 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
664 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
666 if (!il) return NULL;
671 gchar *image_loader_get_format(ImageLoader *il)
673 GdkPixbufFormat *format;
677 if (!il || !il->loader) return NULL;
679 format = gdk_pixbuf_loader_get_format(il->loader);
680 if (!format) return NULL;
682 mimev = gdk_pixbuf_format_get_mime_types(format);
683 if (!mimev) return NULL;
685 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
686 mime = g_strdup(mimev[0]);
692 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
696 il->requested_width = width;
697 il->requested_height = height;
700 void image_loader_set_buffer_size(ImageLoader *il, guint count)
704 il->idle_read_loop_count = count ? count : 1;
707 void image_loader_set_priority(ImageLoader *il, gint priority)
711 il->idle_priority = priority;
715 gdouble image_loader_get_percent(ImageLoader *il)
717 if (!il || il->bytes_total == 0) return 0.0;
719 return (gdouble)il->bytes_read / il->bytes_total;
722 gint image_loader_get_is_done(ImageLoader *il)
724 if (!il) return FALSE;
729 FileData *image_loader_get_fd(ImageLoader *il)
731 if (!il) return NULL;
737 gint image_loader_get_shrunk(ImageLoader *il)
739 if (!il) return FALSE;
746 /* FIXME - this can be rather slow and blocks until the size is known */
747 gint image_load_dimensions(FileData *fd, gint *width, gint *height)
752 il = image_loader_new(fd);
754 success = image_loader_start_idle(il);
756 if (success && il->pixbuf)
758 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
759 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
763 if (width) *width = -1;
764 if (height) *height = -1;
767 image_loader_free(il);