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_gdk.h"
25 #include "image_load_jpeg.h"
26 #include "image_load_tiff.h"
30 #include "ui_fileops.h"
31 #include "gq-marshal.h"
36 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
37 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
40 /**************************************************************************************/
41 /* image loader class */
45 SIGNAL_AREA_READY = 0,
53 static guint signals[SIGNAL_COUNT] = { 0 };
55 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
56 static void image_loader_class_init(ImageLoaderClass *class);
57 static void image_loader_finalize(GObject *object);
58 static void image_loader_stop(ImageLoader *il);
60 GType image_loader_get_type(void)
62 static GType type = 0;
65 static const GTypeInfo info = {
66 sizeof(ImageLoaderClass),
68 NULL, /* base_finalize */
69 (GClassInitFunc)image_loader_class_init, /* class_init */
70 NULL, /* class_finalize */
71 NULL, /* class_data */
74 (GInstanceInitFunc)image_loader_init, /* instance_init */
75 NULL /* value_table */
77 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
82 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
84 ImageLoader *il = (ImageLoader *)instance;
88 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
97 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
98 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
99 il->mapped_file = NULL;
101 il->requested_width = 0;
102 il->requested_height = 0;
103 il->actual_width = 0;
104 il->actual_height = 0;
107 il->can_destroy = TRUE;
110 #if GLIB_CHECK_VERSION(2,32,0)
111 il->data_mutex = g_new(GMutex, 1);
112 g_mutex_init(il->data_mutex);
113 il->can_destroy_cond = g_new(GCond, 1);
114 g_cond_init(il->can_destroy_cond);
116 il->data_mutex = g_mutex_new();
117 il->can_destroy_cond = g_cond_new();
120 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
123 static void image_loader_class_init(ImageLoaderClass *class)
125 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
127 // gobject_class->set_property = image_loader_set_property;
128 // gobject_class->get_property = image_loader_get_property;
130 gobject_class->finalize = image_loader_finalize;
133 signals[SIGNAL_AREA_READY] =
134 g_signal_new("area_ready",
135 G_OBJECT_CLASS_TYPE(gobject_class),
137 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
139 gq_marshal_VOID__INT_INT_INT_INT,
146 signals[SIGNAL_ERROR] =
147 g_signal_new("error",
148 G_OBJECT_CLASS_TYPE(gobject_class),
150 G_STRUCT_OFFSET(ImageLoaderClass, error),
152 g_cclosure_marshal_VOID__VOID,
155 signals[SIGNAL_DONE] =
157 G_OBJECT_CLASS_TYPE(gobject_class),
159 G_STRUCT_OFFSET(ImageLoaderClass, done),
161 g_cclosure_marshal_VOID__VOID,
164 signals[SIGNAL_PERCENT] =
165 g_signal_new("percent",
166 G_OBJECT_CLASS_TYPE(gobject_class),
168 G_STRUCT_OFFSET(ImageLoaderClass, percent),
170 g_cclosure_marshal_VOID__DOUBLE,
174 signals[SIGNAL_SIZE] =
175 g_signal_new("size_prepared",
176 G_OBJECT_CLASS_TYPE(gobject_class),
178 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
180 gq_marshal_VOID__INT_INT,
187 static void image_loader_finalize(GObject *object)
189 ImageLoader *il = (ImageLoader *)object;
191 image_loader_stop(il);
193 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
195 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
197 if (il->idle_done_id)
199 g_source_remove(il->idle_done_id);
200 il->idle_done_id = 0;
203 while (g_source_remove_by_user_data(il))
205 DEBUG_2("pending signals detected");
208 while (il->area_param_list)
210 DEBUG_1("pending area_ready signals detected");
211 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
212 g_free(il->area_param_list->data);
213 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
216 while (il->area_param_delayed_list)
218 g_free(il->area_param_delayed_list->data);
219 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
222 if (il->pixbuf) g_object_unref(il->pixbuf);
224 if (il->error) g_error_free(il->error);
226 file_data_unref(il->fd);
228 #if GLIB_CHECK_VERSION(2,32,0)
229 g_mutex_clear(il->data_mutex);
230 g_free(il->data_mutex);
231 g_cond_clear(il->can_destroy_cond);
232 g_free(il->can_destroy_cond);
234 g_mutex_free(il->data_mutex);
235 g_cond_free(il->can_destroy_cond);
240 void image_loader_free(ImageLoader *il)
243 g_object_unref(G_OBJECT(il));
247 ImageLoader *image_loader_new(FileData *fd)
251 if (!fd) return NULL;
253 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
255 il->fd = file_data_ref(fd);
260 /**************************************************************************************/
261 /* send signals via idle calbacks - the callback are executed in the main thread */
263 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
264 struct _ImageLoaderAreaParam {
273 static gboolean image_loader_emit_area_ready_cb(gpointer data)
275 ImageLoaderAreaParam *par = data;
276 ImageLoader *il = par->il;
278 g_mutex_lock(il->data_mutex);
279 il->area_param_list = g_list_remove(il->area_param_list, par);
285 g_mutex_unlock(il->data_mutex);
287 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
292 static gboolean image_loader_emit_done_cb(gpointer data)
294 ImageLoader *il = data;
295 g_signal_emit(il, signals[SIGNAL_DONE], 0);
299 static gboolean image_loader_emit_error_cb(gpointer data)
301 ImageLoader *il = data;
302 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
306 static gboolean image_loader_emit_percent_cb(gpointer data)
308 ImageLoader *il = data;
309 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
313 static gboolean image_loader_emit_size_cb(gpointer data)
316 ImageLoader *il = data;
317 g_mutex_lock(il->data_mutex);
318 width = il->actual_width;
319 height = il->actual_height;
320 g_mutex_unlock(il->data_mutex);
321 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
326 /* DONE and ERROR are emited only once, thus they can have normal priority
327 PERCENT and AREA_READY should be processed ASAP
330 static void image_loader_emit_done(ImageLoader *il)
332 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
335 static void image_loader_emit_error(ImageLoader *il)
337 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
340 static void image_loader_emit_percent(ImageLoader *il)
342 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
345 static void image_loader_emit_size(ImageLoader *il)
347 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
350 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
354 ImageLoaderAreaParam *prev_par = (*list)->data;
355 if (prev_par->x == x && prev_par->w == w &&
356 prev_par->y + prev_par->h == y)
358 /* we can merge the notifications */
362 if (prev_par->x == x && prev_par->w == w &&
363 y + h == prev_par->y)
365 /* we can merge the notifications */
370 if (prev_par->y == y && prev_par->h == h &&
371 prev_par->x + prev_par->w == x)
373 /* we can merge the notifications */
377 if (prev_par->y == y && prev_par->h == h &&
378 x + w == prev_par->x)
380 /* we can merge the notifications */
387 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
394 *list = g_list_prepend(*list, par);
398 /* this function expects that il->data_mutex is locked by caller */
399 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
401 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
405 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
409 /**************************************************************************************/
410 /* the following functions may be executed in separate thread */
412 /* this function expects that il->data_mutex is locked by caller */
413 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
415 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
420 static gboolean image_loader_get_stopping(ImageLoader *il)
423 if (!il) return FALSE;
425 g_mutex_lock(il->data_mutex);
427 g_mutex_unlock(il->data_mutex);
433 static void image_loader_sync_pixbuf(ImageLoader *il)
437 g_mutex_lock(il->data_mutex);
441 g_mutex_unlock(il->data_mutex);
445 pb = il->backend.get_pixbuf(il->loader);
447 if (pb == il->pixbuf)
449 g_mutex_unlock(il->data_mutex);
453 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
455 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
458 if (il->pixbuf) g_object_unref(il->pixbuf);
461 if (il->pixbuf) g_object_ref(il->pixbuf);
463 g_mutex_unlock(il->data_mutex);
466 static void image_loader_area_updated_cb(gpointer loader,
467 guint x, guint y, guint w, guint h,
470 ImageLoader *il = data;
472 if (!image_loader_get_pixbuf(il))
474 image_loader_sync_pixbuf(il);
475 if (!image_loader_get_pixbuf(il))
477 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
481 g_mutex_lock(il->data_mutex);
482 if (il->delay_area_ready)
483 image_loader_queue_delayed_area_ready(il, x, y, w, h);
485 image_loader_emit_area_ready(il, x, y, w, h);
487 if (il->stopping) il->backend.abort(il->loader);
489 g_mutex_unlock(il->data_mutex);
492 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
494 ImageLoader *il = data;
500 http://bugzilla.gnome.org/show_bug.cgi?id=547669
501 http://bugzilla.gnome.org/show_bug.cgi?id=589334
503 gchar *format = il->backend.get_format_name(loader);
504 if (strcmp(format, "svg") == 0 ||
505 strcmp(format, "xpm") == 0)
513 pb = il->backend.get_pixbuf(loader);
515 h = gdk_pixbuf_get_height(pb);
516 rs = gdk_pixbuf_get_rowstride(pb);
517 pix = gdk_pixbuf_get_pixels(pb);
519 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
523 static void image_loader_size_cb(gpointer loader,
524 gint width, gint height, gpointer data)
526 ImageLoader *il = data;
528 gboolean scale = FALSE;
531 g_mutex_lock(il->data_mutex);
532 il->actual_width = width;
533 il->actual_height = height;
534 if (il->requested_width < 1 || il->requested_height < 1)
536 g_mutex_unlock(il->data_mutex);
537 image_loader_emit_size(il);
540 g_mutex_unlock(il->data_mutex);
542 mime_types = il->backend.get_format_mime_types(loader);
544 while (mime_types[n])
546 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
549 g_strfreev(mime_types);
553 image_loader_emit_size(il);
557 g_mutex_lock(il->data_mutex);
560 if (width > il->requested_width || height > il->requested_height)
563 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
565 nw = il->requested_width;
566 nh = (gdouble)nw / width * height;
571 nh = il->requested_height;
572 nw = (gdouble)nh / height * width;
576 il->actual_width = nw;
577 il->actual_height = nh;
578 il->backend.set_size(loader, nw, nh);
582 g_mutex_unlock(il->data_mutex);
583 image_loader_emit_size(il);
586 static void image_loader_stop_loader(ImageLoader *il)
592 /* some loaders do not have a pixbuf till close, order is important here */
593 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
594 image_loader_sync_pixbuf(il);
595 il->backend.free(il->loader);
598 g_mutex_lock(il->data_mutex);
600 g_mutex_unlock(il->data_mutex);
603 static void image_loader_setup_loader(ImageLoader *il)
605 g_mutex_lock(il->data_mutex);
607 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
609 DEBUG_1("Using custom jpeg loader");
610 image_loader_backend_set_jpeg(&il->backend);
615 if (il->bytes_total >= 10 &&
616 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
617 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
618 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
619 memcmp(il->mapped_file, "II*\0", 4) == 0))
621 DEBUG_1("Using custom tiff loader");
622 image_loader_backend_set_tiff(&il->backend);
626 image_loader_backend_set_default(&il->backend);
628 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
629 g_mutex_unlock(il->data_mutex);
633 static void image_loader_done(ImageLoader *il)
635 image_loader_stop_loader(il);
637 image_loader_emit_done(il);
640 static void image_loader_error(ImageLoader *il)
642 image_loader_stop_loader(il);
644 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
646 image_loader_emit_error(il);
649 static gboolean image_loader_continue(ImageLoader *il)
654 if (!il) return FALSE;
656 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
657 while (c > 0 && !image_loader_get_stopping(il))
659 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
663 image_loader_done(il);
667 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
669 image_loader_error(il);
678 if (il->bytes_total > 0)
680 image_loader_emit_percent(il);
686 static gboolean image_loader_begin(ImageLoader *il)
690 if (il->pixbuf) return FALSE;
692 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
693 if (b < 1) return FALSE;
695 image_loader_setup_loader(il);
697 g_assert(il->bytes_read == 0);
698 if (il->backend.load) {
700 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
702 image_loader_stop_loader(il);
706 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
708 image_loader_stop_loader(il);
714 /* read until size is known */
715 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
717 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
718 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
720 image_loader_stop_loader(il);
725 if (!il->pixbuf) image_loader_sync_pixbuf(il);
727 if (il->bytes_read == il->bytes_total || b < 1)
729 /* done, handle (broken) loaders that do not have pixbuf till close */
730 image_loader_stop_loader(il);
732 if (!il->pixbuf) return FALSE;
734 image_loader_done(il);
740 image_loader_stop_loader(il);
747 /**************************************************************************************/
748 /* the following functions are always executed in the main thread */
751 static gboolean image_loader_setup_source(ImageLoader *il)
756 if (!il || il->loader || il->mapped_file) return FALSE;
758 il->mapped_file = NULL;
762 ExifData *exif = exif_read_fd(il->fd);
764 if (options->thumbnails.use_exif)
765 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
767 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
772 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
774 exif_free_fd(il->fd, exif);
778 if (!il->mapped_file)
783 pathl = path_from_utf8(il->fd->path);
784 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
786 if (load_fd == -1) return FALSE;
788 if (fstat(load_fd, &st) == 0)
790 il->bytes_total = st.st_size;
798 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
800 if (il->mapped_file == MAP_FAILED)
811 static void image_loader_stop_source(ImageLoader *il)
819 exif_free_preview(il->mapped_file);
823 munmap(il->mapped_file, il->bytes_total);
825 il->mapped_file = NULL;
829 static void image_loader_stop(ImageLoader *il)
835 g_source_remove(il->idle_id);
841 /* stop loader in the other thread */
842 g_mutex_lock(il->data_mutex);
844 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
845 g_mutex_unlock(il->data_mutex);
848 image_loader_stop_loader(il);
849 image_loader_stop_source(il);
853 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
855 g_mutex_lock(il->data_mutex);
856 il->delay_area_ready = enable;
861 list = g_list_reverse(il->area_param_delayed_list);
862 il->area_param_delayed_list = NULL;
863 g_mutex_unlock(il->data_mutex);
869 ImageLoaderAreaParam *par = work->data;
872 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
880 g_mutex_unlock(il->data_mutex);
885 /**************************************************************************************/
886 /* execution via idle calls */
888 static gboolean image_loader_idle_cb(gpointer data)
890 gboolean ret = FALSE;
891 ImageLoader *il = data;
895 ret = image_loader_continue(il);
900 image_loader_stop_source(il);
907 static gboolean image_loader_start_idle(ImageLoader *il)
911 if (!il) return FALSE;
913 if (!il->fd) return FALSE;
915 if (!image_loader_setup_source(il)) return FALSE;
917 ret = image_loader_begin(il);
919 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
923 /**************************************************************************************/
924 /* execution via thread */
927 static GThreadPool *image_loader_thread_pool = NULL;
929 static GCond *image_loader_prio_cond = NULL;
930 static GMutex *image_loader_prio_mutex = NULL;
931 static gint image_loader_prio_num = 0;
934 static void image_loader_thread_enter_high(void)
936 g_mutex_lock(image_loader_prio_mutex);
937 image_loader_prio_num++;
938 g_mutex_unlock(image_loader_prio_mutex);
941 static void image_loader_thread_leave_high(void)
943 g_mutex_lock(image_loader_prio_mutex);
944 image_loader_prio_num--;
945 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
946 g_mutex_unlock(image_loader_prio_mutex);
949 static void image_loader_thread_wait_high(void)
951 g_mutex_lock(image_loader_prio_mutex);
952 while (image_loader_prio_num)
954 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
957 g_mutex_unlock(image_loader_prio_mutex);
961 static void image_loader_thread_run(gpointer data, gpointer user_data)
963 ImageLoader *il = data;
967 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
969 /* low prio, wait untill high prio tasks finishes */
970 image_loader_thread_wait_high();
975 image_loader_thread_enter_high();
978 err = !image_loader_begin(il);
983 loader failed, we have to send signal
984 (idle mode returns the image_loader_begin return value directly)
985 (success is always reported indirectly from image_loader_begin)
987 image_loader_emit_error(il);
992 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
994 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
996 /* low prio, wait untill high prio tasks finishes */
997 image_loader_thread_wait_high();
999 cont = image_loader_continue(il);
1001 image_loader_stop_loader(il);
1003 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1006 image_loader_thread_leave_high();
1009 g_mutex_lock(il->data_mutex);
1010 il->can_destroy = TRUE;
1011 g_cond_signal(il->can_destroy_cond);
1012 g_mutex_unlock(il->data_mutex);
1017 static gboolean image_loader_start_thread(ImageLoader *il)
1019 if (!il) return FALSE;
1021 if (!il->fd) return FALSE;
1025 if (!image_loader_setup_source(il)) return FALSE;
1027 if (!image_loader_thread_pool)
1029 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1030 #if GLIB_CHECK_VERSION(2,32,0)
1031 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1032 g_cond_init(image_loader_prio_cond);
1033 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1034 g_mutex_init(image_loader_prio_mutex);
1036 image_loader_prio_cond = g_cond_new();
1037 image_loader_prio_mutex = g_mutex_new();
1041 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1043 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1044 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1048 #endif /* HAVE_GTHREAD */
1051 /**************************************************************************************/
1052 /* public interface */
1055 gboolean image_loader_start(ImageLoader *il)
1057 if (!il) return FALSE;
1059 if (!il->fd) return FALSE;
1062 return image_loader_start_thread(il);
1064 return image_loader_start_idle(il);
1069 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1070 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1073 if (!il) return NULL;
1075 g_mutex_lock(il->data_mutex);
1077 g_mutex_unlock(il->data_mutex);
1081 gchar *image_loader_get_format(ImageLoader *il)
1086 if (!il || !il->loader) return NULL;
1088 mimev = il->backend.get_format_mime_types(il->loader);
1089 if (!mimev) return NULL;
1091 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
1092 mime = g_strdup(mimev[0]);
1098 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1102 g_mutex_lock(il->data_mutex);
1103 il->requested_width = width;
1104 il->requested_height = height;
1105 g_mutex_unlock(il->data_mutex);
1108 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1112 g_mutex_lock(il->data_mutex);
1113 il->idle_read_loop_count = count ? count : 1;
1114 g_mutex_unlock(il->data_mutex);
1117 void image_loader_set_priority(ImageLoader *il, gint priority)
1121 if (il->thread) return; /* can't change prio if the thread already runs */
1122 il->idle_priority = priority;
1126 gdouble image_loader_get_percent(ImageLoader *il)
1129 if (!il) return 0.0;
1131 g_mutex_lock(il->data_mutex);
1132 if (il->bytes_total == 0)
1138 ret = (gdouble)il->bytes_read / il->bytes_total;
1140 g_mutex_unlock(il->data_mutex);
1144 gboolean image_loader_get_is_done(ImageLoader *il)
1147 if (!il) return FALSE;
1149 g_mutex_lock(il->data_mutex);
1151 g_mutex_unlock(il->data_mutex);
1156 FileData *image_loader_get_fd(ImageLoader *il)
1159 if (!il) return NULL;
1161 g_mutex_lock(il->data_mutex);
1163 g_mutex_unlock(il->data_mutex);
1168 gboolean image_loader_get_shrunk(ImageLoader *il)
1171 if (!il) return FALSE;
1173 g_mutex_lock(il->data_mutex);
1175 g_mutex_unlock(il->data_mutex);
1179 const gchar *image_loader_get_error(ImageLoader *il)
1181 const gchar *ret = NULL;
1182 if (!il) return NULL;
1183 g_mutex_lock(il->data_mutex);
1184 if (il->error) ret = il->error->message;
1185 g_mutex_unlock(il->data_mutex);
1190 /* FIXME - this can be rather slow and blocks until the size is known */
1191 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1196 il = image_loader_new(fd);
1198 success = image_loader_start_idle(il);
1200 if (success && il->pixbuf)
1202 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1203 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1207 if (width) *width = -1;
1208 if (height) *height = -1;
1211 image_loader_free(il);
1215 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */