2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "image-load.h"
24 #include "image_load_cr3.h"
25 #include "image_load_gdk.h"
26 #include "image_load_jpeg.h"
27 #include "image_load_tiff.h"
28 #include "image_load_dds.h"
29 #include "image_load_djvu.h"
30 #include "image_load_pdf.h"
31 #include "image_load_psd.h"
32 #include "image_load_heif.h"
33 #include "image_load_ffmpegthumbnailer.h"
34 #include "image_load_collection.h"
35 #include "image_load_webp.h"
36 #include "image_load_j2k.h"
37 #include "image_load_libraw.h"
38 #include "image_load_svgz.h"
42 #include "ui_fileops.h"
43 #include "gq-marshal.h"
48 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
49 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
52 /**************************************************************************************/
53 /* image loader class */
57 SIGNAL_AREA_READY = 0,
65 static guint signals[SIGNAL_COUNT] = { 0 };
67 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
68 static void image_loader_class_init_wrapper(void *data, void *user_data);
69 static void image_loader_class_init(ImageLoaderClass *class);
70 static void image_loader_finalize(GObject *object);
71 static void image_loader_stop(ImageLoader *il);
73 GType image_loader_get_type(void)
75 static GType type = 0;
78 static const GTypeInfo info = {
79 sizeof(ImageLoaderClass),
81 NULL, /* base_finalize */
82 (GClassInitFunc)image_loader_class_init_wrapper, /* class_init */
83 NULL, /* class_finalize */
84 NULL, /* class_data */
87 (GInstanceInitFunc)image_loader_init, /* instance_init */
88 NULL /* value_table */
90 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
95 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
97 ImageLoader *il = (ImageLoader *)instance;
101 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
108 il->idle_done_id = 0;
110 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
111 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
112 il->mapped_file = NULL;
113 il->preview = IMAGE_LOADER_PREVIEW_NONE;
115 il->requested_width = 0;
116 il->requested_height = 0;
117 il->actual_width = 0;
118 il->actual_height = 0;
121 il->can_destroy = TRUE;
124 #if GLIB_CHECK_VERSION(2,32,0)
125 il->data_mutex = g_new(GMutex, 1);
126 g_mutex_init(il->data_mutex);
127 il->can_destroy_cond = g_new(GCond, 1);
128 g_cond_init(il->can_destroy_cond);
130 il->data_mutex = g_mutex_new();
131 il->can_destroy_cond = g_cond_new();
134 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
137 static void image_loader_class_init_wrapper(void *data, void *user_data)
139 image_loader_class_init(data);
142 static void image_loader_class_init(ImageLoaderClass *class)
144 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
146 // gobject_class->set_property = image_loader_set_property;
147 // gobject_class->get_property = image_loader_get_property;
149 gobject_class->finalize = image_loader_finalize;
152 signals[SIGNAL_AREA_READY] =
153 g_signal_new("area_ready",
154 G_OBJECT_CLASS_TYPE(gobject_class),
156 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
158 gq_marshal_VOID__INT_INT_INT_INT,
165 signals[SIGNAL_ERROR] =
166 g_signal_new("error",
167 G_OBJECT_CLASS_TYPE(gobject_class),
169 G_STRUCT_OFFSET(ImageLoaderClass, error),
171 g_cclosure_marshal_VOID__VOID,
174 signals[SIGNAL_DONE] =
176 G_OBJECT_CLASS_TYPE(gobject_class),
178 G_STRUCT_OFFSET(ImageLoaderClass, done),
180 g_cclosure_marshal_VOID__VOID,
183 signals[SIGNAL_PERCENT] =
184 g_signal_new("percent",
185 G_OBJECT_CLASS_TYPE(gobject_class),
187 G_STRUCT_OFFSET(ImageLoaderClass, percent),
189 g_cclosure_marshal_VOID__DOUBLE,
193 signals[SIGNAL_SIZE] =
194 g_signal_new("size_prepared",
195 G_OBJECT_CLASS_TYPE(gobject_class),
197 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
199 gq_marshal_VOID__INT_INT,
206 static void image_loader_finalize(GObject *object)
208 ImageLoader *il = (ImageLoader *)object;
210 image_loader_stop(il);
212 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
214 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
216 if (il->idle_done_id)
218 g_source_remove(il->idle_done_id);
219 il->idle_done_id = 0;
222 while (g_source_remove_by_user_data(il))
224 DEBUG_2("pending signals detected");
227 while (il->area_param_list)
229 DEBUG_1("pending area_ready signals detected");
230 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
231 g_free(il->area_param_list->data);
232 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
235 while (il->area_param_delayed_list)
237 g_free(il->area_param_delayed_list->data);
238 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
241 if (il->pixbuf) g_object_unref(il->pixbuf);
243 if (il->error) g_error_free(il->error);
245 file_data_unref(il->fd);
247 #if GLIB_CHECK_VERSION(2,32,0)
248 g_mutex_clear(il->data_mutex);
249 g_free(il->data_mutex);
250 g_cond_clear(il->can_destroy_cond);
251 g_free(il->can_destroy_cond);
253 g_mutex_free(il->data_mutex);
254 g_cond_free(il->can_destroy_cond);
259 void image_loader_free(ImageLoader *il)
262 g_object_unref(G_OBJECT(il));
266 ImageLoader *image_loader_new(FileData *fd)
270 if (!fd) return NULL;
272 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
274 il->fd = file_data_ref(fd);
279 /**************************************************************************************/
280 /* send signals via idle calbacks - the callback are executed in the main thread */
282 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
283 struct _ImageLoaderAreaParam {
292 static gboolean image_loader_emit_area_ready_cb(gpointer data)
294 ImageLoaderAreaParam *par = data;
295 ImageLoader *il = par->il;
297 g_mutex_lock(il->data_mutex);
298 il->area_param_list = g_list_remove(il->area_param_list, par);
304 g_mutex_unlock(il->data_mutex);
306 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
311 static gboolean image_loader_emit_done_cb(gpointer data)
313 ImageLoader *il = data;
314 g_signal_emit(il, signals[SIGNAL_DONE], 0);
318 static gboolean image_loader_emit_error_cb(gpointer data)
320 ImageLoader *il = data;
321 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
325 static gboolean image_loader_emit_percent_cb(gpointer data)
327 ImageLoader *il = data;
328 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
332 static gboolean image_loader_emit_size_cb(gpointer data)
335 ImageLoader *il = data;
336 g_mutex_lock(il->data_mutex);
337 width = il->actual_width;
338 height = il->actual_height;
339 g_mutex_unlock(il->data_mutex);
340 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
345 /* DONE and ERROR are emitted only once, thus they can have normal priority
346 PERCENT and AREA_READY should be processed ASAP
349 static void image_loader_emit_done(ImageLoader *il)
351 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
354 static void image_loader_emit_error(ImageLoader *il)
356 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
359 static void image_loader_emit_percent(ImageLoader *il)
361 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
364 static void image_loader_emit_size(ImageLoader *il)
366 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
369 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
373 ImageLoaderAreaParam *prev_par = (*list)->data;
374 if (prev_par->x == x && prev_par->w == w &&
375 prev_par->y + prev_par->h == y)
377 /* we can merge the notifications */
381 if (prev_par->x == x && prev_par->w == w &&
382 y + h == prev_par->y)
384 /* we can merge the notifications */
389 if (prev_par->y == y && prev_par->h == h &&
390 prev_par->x + prev_par->w == x)
392 /* we can merge the notifications */
396 if (prev_par->y == y && prev_par->h == h &&
397 x + w == prev_par->x)
399 /* we can merge the notifications */
406 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
413 *list = g_list_prepend(*list, par);
417 /* this function expects that il->data_mutex is locked by caller */
418 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
420 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
424 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
428 /**************************************************************************************/
429 /* the following functions may be executed in separate thread */
431 /* this function expects that il->data_mutex is locked by caller */
432 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
434 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
439 static gboolean image_loader_get_stopping(ImageLoader *il)
442 if (!il) return FALSE;
444 g_mutex_lock(il->data_mutex);
446 g_mutex_unlock(il->data_mutex);
452 static void image_loader_sync_pixbuf(ImageLoader *il)
456 g_mutex_lock(il->data_mutex);
460 g_mutex_unlock(il->data_mutex);
464 pb = il->backend.get_pixbuf(il->loader);
466 if (pb == il->pixbuf)
468 g_mutex_unlock(il->data_mutex);
472 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
474 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
477 if (il->pixbuf) g_object_unref(il->pixbuf);
480 if (il->pixbuf) g_object_ref(il->pixbuf);
482 g_mutex_unlock(il->data_mutex);
485 static void image_loader_area_updated_cb(gpointer loader,
486 guint x, guint y, guint w, guint h,
489 ImageLoader *il = data;
491 if (!image_loader_get_pixbuf(il))
493 image_loader_sync_pixbuf(il);
494 if (!image_loader_get_pixbuf(il))
496 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
500 g_mutex_lock(il->data_mutex);
501 if (il->delay_area_ready)
502 image_loader_queue_delayed_area_ready(il, x, y, w, h);
504 image_loader_emit_area_ready(il, x, y, w, h);
506 if (il->stopping) il->backend.abort(il->loader);
508 g_mutex_unlock(il->data_mutex);
511 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
513 ImageLoader *il = data;
519 http://bugzilla.gnome.org/show_bug.cgi?id=547669
520 http://bugzilla.gnome.org/show_bug.cgi?id=589334
522 gchar *format = il->backend.get_format_name(loader);
523 if (strcmp(format, "svg") == 0 ||
524 strcmp(format, "xpm") == 0)
532 pb = il->backend.get_pixbuf(loader);
534 h = gdk_pixbuf_get_height(pb);
535 rs = gdk_pixbuf_get_rowstride(pb);
536 pix = gdk_pixbuf_get_pixels(pb);
538 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
542 static void image_loader_size_cb(gpointer loader,
543 gint width, gint height, gpointer data)
545 ImageLoader *il = data;
547 gboolean scale = FALSE;
550 g_mutex_lock(il->data_mutex);
551 il->actual_width = width;
552 il->actual_height = height;
553 if (il->requested_width < 1 || il->requested_height < 1)
555 g_mutex_unlock(il->data_mutex);
556 image_loader_emit_size(il);
559 g_mutex_unlock(il->data_mutex);
561 #ifdef HAVE_FFMPEGTHUMBNAILER
562 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
565 mime_types = il->backend.get_format_mime_types(loader);
567 while (mime_types[n] && !scale)
569 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
572 g_strfreev(mime_types);
576 image_loader_emit_size(il);
580 g_mutex_lock(il->data_mutex);
583 if (width > il->requested_width || height > il->requested_height)
586 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
588 nw = il->requested_width;
589 nh = (gdouble)nw / width * height;
594 nh = il->requested_height;
595 nw = (gdouble)nh / height * width;
599 il->actual_width = nw;
600 il->actual_height = nh;
601 il->backend.set_size(loader, nw, nh);
605 g_mutex_unlock(il->data_mutex);
606 image_loader_emit_size(il);
609 static void image_loader_stop_loader(ImageLoader *il)
615 /* some loaders do not have a pixbuf till close, order is important here */
616 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
617 image_loader_sync_pixbuf(il);
618 il->backend.free(il->loader);
621 g_mutex_lock(il->data_mutex);
623 g_mutex_unlock(il->data_mutex);
626 static void image_loader_setup_loader(ImageLoader *il)
628 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
632 g_mutex_lock(il->data_mutex);
633 #ifdef HAVE_FFMPEGTHUMBNAILER
634 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
636 DEBUG_1("Using custom ffmpegthumbnailer loader");
637 image_loader_backend_set_ft(&il->backend);
642 if (il->bytes_total >= 4 &&
643 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
645 DEBUG_1("Using custom pdf loader");
646 image_loader_backend_set_pdf(&il->backend);
651 if (il->bytes_total >= 12 &&
652 ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
653 (memcmp(il->mapped_file + 4, "ftypmsf1", 8) == 0) ||
654 (memcmp(il->mapped_file + 4, "ftypmif1", 8) == 0)))
656 DEBUG_1("Using custom heif loader");
657 image_loader_backend_set_heif(&il->backend);
662 if (il->bytes_total >= 12 &&
663 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
664 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
666 DEBUG_1("Using custom webp loader");
667 image_loader_backend_set_webp(&il->backend);
672 if (il->bytes_total >= 16 &&
673 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
674 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
676 DEBUG_1("Using custom djvu loader");
677 image_loader_backend_set_djvu(&il->backend);
682 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
684 DEBUG_1("Using custom jpeg loader");
685 image_loader_backend_set_jpeg(&il->backend);
689 if (il->bytes_total >= 11 &&
690 (memcmp(il->mapped_file + 4, "ftypcrx", 7) == 0) &&
691 (memcmp(il->mapped_file + 64, "CanonCR3", 8) == 0))
693 DEBUG_1("Using custom cr3 loader");
694 image_loader_backend_set_cr3(&il->backend);
700 if (il->bytes_total >= 10 &&
701 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
702 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
703 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
704 memcmp(il->mapped_file, "II*\0", 4) == 0))
706 DEBUG_1("Using custom tiff loader");
707 image_loader_backend_set_tiff(&il->backend);
711 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
713 DEBUG_1("Using dds loader");
714 image_loader_backend_set_dds(&il->backend);
717 if (il->bytes_total >= 6 &&
718 (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
720 DEBUG_1("Using custom psd loader");
721 image_loader_backend_set_psd(&il->backend);
725 if (il->bytes_total >= 12 &&
726 (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
728 DEBUG_1("Using custom j2k loader");
729 image_loader_backend_set_j2k(&il->backend);
733 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
735 DEBUG_1("Using custom collection loader");
736 image_loader_backend_set_collection(&il->backend);
739 if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
741 DEBUG_1("Using custom svgz loader");
742 image_loader_backend_set_svgz(&il->backend);
745 image_loader_backend_set_default(&il->backend);
747 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
750 format = il->backend.get_format_name(il->loader);
751 if (g_strcmp0(format, "tiff") == 0)
753 il->backend.set_page_num(il->loader, il->fd->page_num);
759 format = il->backend.get_format_name(il->loader);
760 if (g_strcmp0(format, "pdf") == 0)
762 il->backend.set_page_num(il->loader, il->fd->page_num);
768 format = il->backend.get_format_name(il->loader);
769 if (g_strcmp0(format, "heif") == 0)
771 il->backend.set_page_num(il->loader, il->fd->page_num);
777 format = il->backend.get_format_name(il->loader);
778 if (g_strcmp0(format, "djvu") == 0)
780 il->backend.set_page_num(il->loader, il->fd->page_num);
785 g_mutex_unlock(il->data_mutex);
789 static void image_loader_done(ImageLoader *il)
791 image_loader_stop_loader(il);
793 image_loader_emit_done(il);
796 static void image_loader_error(ImageLoader *il)
798 image_loader_stop_loader(il);
800 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
802 image_loader_emit_error(il);
805 static gboolean image_loader_continue(ImageLoader *il)
810 if (!il) return FALSE;
812 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
813 while (c > 0 && !image_loader_get_stopping(il))
815 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
819 image_loader_done(il);
823 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
825 image_loader_error(il);
834 if (il->bytes_total > 0)
836 image_loader_emit_percent(il);
842 static gboolean image_loader_begin(ImageLoader *il)
844 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
849 if (il->pixbuf) return FALSE;
851 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
852 if (b < 1) return FALSE;
854 image_loader_setup_loader(il);
856 g_assert(il->bytes_read == 0);
857 if (il->backend.load) {
859 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
861 image_loader_stop_loader(il);
865 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
867 image_loader_stop_loader(il);
872 format = il->backend.get_format_name(il->loader);
873 if (g_strcmp0(format, "pdf") == 0)
875 gint i = il->backend.get_page_total(il->loader);
876 file_data_set_page_total(il->fd, i);
881 format = il->backend.get_format_name(il->loader);
882 if (g_strcmp0(format, "heif") == 0)
884 gint i = il->backend.get_page_total(il->loader);
885 file_data_set_page_total(il->fd, i);
890 format = il->backend.get_format_name(il->loader);
891 if (g_strcmp0(format, "djvu") == 0)
893 gint i = il->backend.get_page_total(il->loader);
894 file_data_set_page_total(il->fd, i);
899 format = il->backend.get_format_name(il->loader);
900 if (g_strcmp0(format, "tiff") == 0)
902 gint i = il->backend.get_page_total(il->loader);
903 file_data_set_page_total(il->fd, i);
910 /* read until size is known */
911 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
913 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
914 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
916 image_loader_stop_loader(il);
921 if (!il->pixbuf) image_loader_sync_pixbuf(il);
923 if (il->bytes_read == il->bytes_total || b < 1)
925 /* done, handle (broken) loaders that do not have pixbuf till close */
926 image_loader_stop_loader(il);
928 if (!il->pixbuf) return FALSE;
930 image_loader_done(il);
936 image_loader_stop_loader(il);
943 /**************************************************************************************/
944 /* the following functions are always executed in the main thread */
947 static gboolean image_loader_setup_source(ImageLoader *il)
952 if (!il || il->loader || il->mapped_file) return FALSE;
954 il->mapped_file = NULL;
958 ExifData *exif = exif_read_fd(il->fd);
960 if (options->thumbnails.use_exif)
962 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
966 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
971 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
975 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
979 /* If exiv2 does not find a thumbnail, try libraw (which may be slower) */
980 if (!il->mapped_file)
982 il->mapped_file = libraw_get_preview(il, (guint *)&il->bytes_total);
986 il->preview = IMAGE_LOADER_PREVIEW_LIBRAW;
992 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
994 exif_free_fd(il->fd, exif);
998 if (!il->mapped_file)
1003 pathl = path_from_utf8(il->fd->path);
1004 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
1006 if (load_fd == -1) return FALSE;
1008 if (fstat(load_fd, &st) == 0)
1010 il->bytes_total = st.st_size;
1018 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
1020 if (il->mapped_file == MAP_FAILED)
1022 il->mapped_file = 0;
1025 il->preview = IMAGE_LOADER_PREVIEW_NONE;
1031 static void image_loader_stop_source(ImageLoader *il)
1035 if (il->mapped_file)
1037 if (il->preview == IMAGE_LOADER_PREVIEW_EXIF)
1039 exif_free_preview(il->mapped_file);
1041 else if (il->preview == IMAGE_LOADER_PREVIEW_LIBRAW)
1043 libraw_free_preview(il->mapped_file);
1047 munmap(il->mapped_file, il->bytes_total);
1049 il->mapped_file = NULL;
1053 static void image_loader_stop(ImageLoader *il)
1059 g_source_remove(il->idle_id);
1065 /* stop loader in the other thread */
1066 g_mutex_lock(il->data_mutex);
1067 il->stopping = TRUE;
1068 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
1069 g_mutex_unlock(il->data_mutex);
1072 image_loader_stop_loader(il);
1073 image_loader_stop_source(il);
1077 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
1079 g_mutex_lock(il->data_mutex);
1080 il->delay_area_ready = enable;
1085 list = g_list_reverse(il->area_param_delayed_list);
1086 il->area_param_delayed_list = NULL;
1087 g_mutex_unlock(il->data_mutex);
1093 ImageLoaderAreaParam *par = work->data;
1096 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1104 g_mutex_unlock(il->data_mutex);
1109 /**************************************************************************************/
1110 /* execution via idle calls */
1112 static gboolean image_loader_idle_cb(gpointer data)
1114 gboolean ret = FALSE;
1115 ImageLoader *il = data;
1119 ret = image_loader_continue(il);
1124 image_loader_stop_source(il);
1131 static gboolean image_loader_start_idle(ImageLoader *il)
1135 if (!il) return FALSE;
1137 if (!il->fd) return FALSE;
1139 if (!image_loader_setup_source(il)) return FALSE;
1141 ret = image_loader_begin(il);
1143 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
1147 /**************************************************************************************/
1148 /* execution via thread */
1151 static GThreadPool *image_loader_thread_pool = NULL;
1153 static GCond *image_loader_prio_cond = NULL;
1154 static GMutex *image_loader_prio_mutex = NULL;
1155 static gint image_loader_prio_num = 0;
1158 static void image_loader_thread_enter_high(void)
1160 g_mutex_lock(image_loader_prio_mutex);
1161 image_loader_prio_num++;
1162 g_mutex_unlock(image_loader_prio_mutex);
1165 static void image_loader_thread_leave_high(void)
1167 g_mutex_lock(image_loader_prio_mutex);
1168 image_loader_prio_num--;
1169 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1170 g_mutex_unlock(image_loader_prio_mutex);
1173 static void image_loader_thread_wait_high(void)
1175 g_mutex_lock(image_loader_prio_mutex);
1176 while (image_loader_prio_num)
1178 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1181 g_mutex_unlock(image_loader_prio_mutex);
1185 static void image_loader_thread_run(gpointer data, gpointer user_data)
1187 ImageLoader *il = data;
1191 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1193 /* low prio, wait until high prio tasks finishes */
1194 image_loader_thread_wait_high();
1199 image_loader_thread_enter_high();
1202 err = !image_loader_begin(il);
1207 loader failed, we have to send signal
1208 (idle mode returns the image_loader_begin return value directly)
1209 (success is always reported indirectly from image_loader_begin)
1211 image_loader_emit_error(il);
1216 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1218 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1220 /* low prio, wait until high prio tasks finishes */
1221 image_loader_thread_wait_high();
1223 cont = image_loader_continue(il);
1225 image_loader_stop_loader(il);
1227 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1230 image_loader_thread_leave_high();
1233 g_mutex_lock(il->data_mutex);
1234 il->can_destroy = TRUE;
1235 g_cond_signal(il->can_destroy_cond);
1236 g_mutex_unlock(il->data_mutex);
1241 static gboolean image_loader_start_thread(ImageLoader *il)
1243 if (!il) return FALSE;
1245 if (!il->fd) return FALSE;
1249 if (!image_loader_setup_source(il)) return FALSE;
1251 if (!image_loader_thread_pool)
1253 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1254 #if GLIB_CHECK_VERSION(2,32,0)
1255 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1256 g_cond_init(image_loader_prio_cond);
1257 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1258 g_mutex_init(image_loader_prio_mutex);
1260 image_loader_prio_cond = g_cond_new();
1261 image_loader_prio_mutex = g_mutex_new();
1265 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1267 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1268 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1272 #endif /* HAVE_GTHREAD */
1275 /**************************************************************************************/
1276 /* public interface */
1279 gboolean image_loader_start(ImageLoader *il)
1281 if (!il) return FALSE;
1283 if (!il->fd) return FALSE;
1286 return image_loader_start_thread(il);
1288 return image_loader_start_idle(il);
1293 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1294 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1297 if (!il) return NULL;
1299 g_mutex_lock(il->data_mutex);
1301 g_mutex_unlock(il->data_mutex);
1305 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1309 g_mutex_lock(il->data_mutex);
1310 il->requested_width = width;
1311 il->requested_height = height;
1312 g_mutex_unlock(il->data_mutex);
1315 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1319 g_mutex_lock(il->data_mutex);
1320 il->idle_read_loop_count = count ? count : 1;
1321 g_mutex_unlock(il->data_mutex);
1324 void image_loader_set_priority(ImageLoader *il, gint priority)
1328 if (il->thread) return; /* can't change prio if the thread already runs */
1329 il->idle_priority = priority;
1333 gdouble image_loader_get_percent(ImageLoader *il)
1336 if (!il) return 0.0;
1338 g_mutex_lock(il->data_mutex);
1339 if (il->bytes_total == 0)
1345 ret = (gdouble)il->bytes_read / il->bytes_total;
1347 g_mutex_unlock(il->data_mutex);
1351 gboolean image_loader_get_is_done(ImageLoader *il)
1354 if (!il) return FALSE;
1356 g_mutex_lock(il->data_mutex);
1358 g_mutex_unlock(il->data_mutex);
1363 FileData *image_loader_get_fd(ImageLoader *il)
1366 if (!il) return NULL;
1368 g_mutex_lock(il->data_mutex);
1370 g_mutex_unlock(il->data_mutex);
1375 gboolean image_loader_get_shrunk(ImageLoader *il)
1378 if (!il) return FALSE;
1380 g_mutex_lock(il->data_mutex);
1382 g_mutex_unlock(il->data_mutex);
1386 const gchar *image_loader_get_error(ImageLoader *il)
1388 const gchar *ret = NULL;
1389 if (!il) return NULL;
1390 g_mutex_lock(il->data_mutex);
1391 if (il->error) ret = il->error->message;
1392 g_mutex_unlock(il->data_mutex);
1398 * @FIXME this can be rather slow and blocks until the size is known
1400 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1405 il = image_loader_new(fd);
1407 success = image_loader_start_idle(il);
1409 if (success && il->pixbuf)
1411 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1412 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1416 if (width) *width = -1;
1417 if (height) *height = -1;
1420 image_loader_free(il);
1424 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */