4 * Copyright (C) 2008 - 2012 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"
16 #include "image_load_gdk.h"
17 #include "image_load_jpeg.h"
18 #include "image_load_tiff.h"
22 #include "ui_fileops.h"
23 #include "gq-marshal.h"
28 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
29 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
32 /**************************************************************************************/
33 /* image loader class */
37 SIGNAL_AREA_READY = 0,
45 static guint signals[SIGNAL_COUNT] = { 0 };
47 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
48 static void image_loader_class_init(ImageLoaderClass *class);
49 static void image_loader_finalize(GObject *object);
50 static void image_loader_stop(ImageLoader *il);
52 GType image_loader_get_type(void)
54 static GType type = 0;
57 static const GTypeInfo info = {
58 sizeof(ImageLoaderClass),
60 NULL, /* base_finalize */
61 (GClassInitFunc)image_loader_class_init, /* class_init */
62 NULL, /* class_finalize */
63 NULL, /* class_data */
66 (GInstanceInitFunc)image_loader_init, /* instance_init */
67 NULL /* value_table */
69 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
74 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
76 ImageLoader *il = (ImageLoader *)instance;
80 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
89 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
90 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
91 il->mapped_file = NULL;
93 il->requested_width = 0;
94 il->requested_height = 0;
96 il->actual_height = 0;
99 il->can_destroy = TRUE;
102 #if GLIB_CHECK_VERSION(2,32,0)
103 il->data_mutex = g_new(GMutex, 1);
104 g_mutex_init(il->data_mutex);
105 il->can_destroy_cond = g_new(GCond, 1);
106 g_cond_init(il->can_destroy_cond);
108 il->data_mutex = g_mutex_new();
109 il->can_destroy_cond = g_cond_new();
112 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
115 static void image_loader_class_init(ImageLoaderClass *class)
117 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
119 // gobject_class->set_property = image_loader_set_property;
120 // gobject_class->get_property = image_loader_get_property;
122 gobject_class->finalize = image_loader_finalize;
125 signals[SIGNAL_AREA_READY] =
126 g_signal_new("area_ready",
127 G_OBJECT_CLASS_TYPE(gobject_class),
129 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
131 gq_marshal_VOID__INT_INT_INT_INT,
138 signals[SIGNAL_ERROR] =
139 g_signal_new("error",
140 G_OBJECT_CLASS_TYPE(gobject_class),
142 G_STRUCT_OFFSET(ImageLoaderClass, error),
144 g_cclosure_marshal_VOID__VOID,
147 signals[SIGNAL_DONE] =
149 G_OBJECT_CLASS_TYPE(gobject_class),
151 G_STRUCT_OFFSET(ImageLoaderClass, done),
153 g_cclosure_marshal_VOID__VOID,
156 signals[SIGNAL_PERCENT] =
157 g_signal_new("percent",
158 G_OBJECT_CLASS_TYPE(gobject_class),
160 G_STRUCT_OFFSET(ImageLoaderClass, percent),
162 g_cclosure_marshal_VOID__DOUBLE,
166 signals[SIGNAL_SIZE] =
167 g_signal_new("size_prepared",
168 G_OBJECT_CLASS_TYPE(gobject_class),
170 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
172 gq_marshal_VOID__INT_INT,
179 static void image_loader_finalize(GObject *object)
181 ImageLoader *il = (ImageLoader *)object;
183 image_loader_stop(il);
185 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
187 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
189 if (il->idle_done_id)
191 g_source_remove(il->idle_done_id);
192 il->idle_done_id = 0;
195 while (g_source_remove_by_user_data(il))
197 DEBUG_2("pending signals detected");
200 while (il->area_param_list)
202 DEBUG_1("pending area_ready signals detected");
203 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
204 g_free(il->area_param_list->data);
205 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
208 while (il->area_param_delayed_list)
210 g_free(il->area_param_delayed_list->data);
211 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
214 if (il->pixbuf) g_object_unref(il->pixbuf);
216 if (il->error) g_error_free(il->error);
218 file_data_unref(il->fd);
220 #if GLIB_CHECK_VERSION(2,32,0)
221 g_mutex_clear(il->data_mutex);
222 g_free(il->data_mutex);
223 g_cond_clear(il->can_destroy_cond);
224 g_free(il->can_destroy_cond);
226 g_mutex_free(il->data_mutex);
227 g_cond_free(il->can_destroy_cond);
232 void image_loader_free(ImageLoader *il)
235 g_object_unref(G_OBJECT(il));
239 ImageLoader *image_loader_new(FileData *fd)
243 if (!fd) return NULL;
245 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
247 il->fd = file_data_ref(fd);
252 /**************************************************************************************/
253 /* send signals via idle calbacks - the callback are executed in the main thread */
255 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
256 struct _ImageLoaderAreaParam {
265 static gboolean image_loader_emit_area_ready_cb(gpointer data)
267 ImageLoaderAreaParam *par = data;
268 ImageLoader *il = par->il;
270 g_mutex_lock(il->data_mutex);
271 il->area_param_list = g_list_remove(il->area_param_list, par);
277 g_mutex_unlock(il->data_mutex);
279 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
284 static gboolean image_loader_emit_done_cb(gpointer data)
286 ImageLoader *il = data;
287 g_signal_emit(il, signals[SIGNAL_DONE], 0);
291 static gboolean image_loader_emit_error_cb(gpointer data)
293 ImageLoader *il = data;
294 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
298 static gboolean image_loader_emit_percent_cb(gpointer data)
300 ImageLoader *il = data;
301 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
305 static gboolean image_loader_emit_size_cb(gpointer data)
308 ImageLoader *il = data;
309 g_mutex_lock(il->data_mutex);
310 width = il->actual_width;
311 height = il->actual_height;
312 g_mutex_unlock(il->data_mutex);
313 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
318 /* DONE and ERROR are emited only once, thus they can have normal priority
319 PERCENT and AREA_READY should be processed ASAP
322 static void image_loader_emit_done(ImageLoader *il)
324 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
327 static void image_loader_emit_error(ImageLoader *il)
329 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
332 static void image_loader_emit_percent(ImageLoader *il)
334 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
337 static void image_loader_emit_size(ImageLoader *il)
339 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
342 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
346 ImageLoaderAreaParam *prev_par = (*list)->data;
347 if (prev_par->x == x && prev_par->w == w &&
348 prev_par->y + prev_par->h == y)
350 /* we can merge the notifications */
354 if (prev_par->x == x && prev_par->w == w &&
355 y + h == prev_par->y)
357 /* we can merge the notifications */
362 if (prev_par->y == y && prev_par->h == h &&
363 prev_par->x + prev_par->w == x)
365 /* we can merge the notifications */
369 if (prev_par->y == y && prev_par->h == h &&
370 x + w == prev_par->x)
372 /* we can merge the notifications */
379 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
386 *list = g_list_prepend(*list, par);
390 /* this function expects that il->data_mutex is locked by caller */
391 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
393 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
397 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
401 /**************************************************************************************/
402 /* the following functions may be executed in separate thread */
404 /* this function expects that il->data_mutex is locked by caller */
405 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
407 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
412 static gboolean image_loader_get_stopping(ImageLoader *il)
415 if (!il) return FALSE;
417 g_mutex_lock(il->data_mutex);
419 g_mutex_unlock(il->data_mutex);
425 static void image_loader_sync_pixbuf(ImageLoader *il)
429 g_mutex_lock(il->data_mutex);
433 g_mutex_unlock(il->data_mutex);
437 pb = il->backend.get_pixbuf(il->loader);
439 if (pb == il->pixbuf)
441 g_mutex_unlock(il->data_mutex);
445 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
447 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
450 if (il->pixbuf) g_object_unref(il->pixbuf);
453 if (il->pixbuf) g_object_ref(il->pixbuf);
455 g_mutex_unlock(il->data_mutex);
458 static void image_loader_area_updated_cb(gpointer loader,
459 guint x, guint y, guint w, guint h,
462 ImageLoader *il = data;
464 if (!image_loader_get_pixbuf(il))
466 image_loader_sync_pixbuf(il);
467 if (!image_loader_get_pixbuf(il))
469 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
473 g_mutex_lock(il->data_mutex);
474 if (il->delay_area_ready)
475 image_loader_queue_delayed_area_ready(il, x, y, w, h);
477 image_loader_emit_area_ready(il, x, y, w, h);
479 if (il->stopping) il->backend.abort(il->loader);
481 g_mutex_unlock(il->data_mutex);
484 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
486 ImageLoader *il = data;
492 http://bugzilla.gnome.org/show_bug.cgi?id=547669
493 http://bugzilla.gnome.org/show_bug.cgi?id=589334
495 gchar *format = il->backend.get_format_name(loader);
496 if (strcmp(format, "svg") == 0 ||
497 strcmp(format, "xpm") == 0)
505 pb = il->backend.get_pixbuf(loader);
507 h = gdk_pixbuf_get_height(pb);
508 rs = gdk_pixbuf_get_rowstride(pb);
509 pix = gdk_pixbuf_get_pixels(pb);
511 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
515 static void image_loader_size_cb(gpointer loader,
516 gint width, gint height, gpointer data)
518 ImageLoader *il = data;
520 gboolean scale = FALSE;
523 g_mutex_lock(il->data_mutex);
524 il->actual_width = width;
525 il->actual_height = height;
526 if (il->requested_width < 1 || il->requested_height < 1)
528 g_mutex_unlock(il->data_mutex);
529 image_loader_emit_size(il);
532 g_mutex_unlock(il->data_mutex);
534 mime_types = il->backend.get_format_mime_types(loader);
536 while (mime_types[n])
538 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
541 g_strfreev(mime_types);
545 image_loader_emit_size(il);
549 g_mutex_lock(il->data_mutex);
552 if (width > il->requested_width || height > il->requested_height)
555 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
557 nw = il->requested_width;
558 nh = (gdouble)nw / width * height;
563 nh = il->requested_height;
564 nw = (gdouble)nh / height * width;
568 il->actual_width = nw;
569 il->actual_height = nh;
570 il->backend.set_size(loader, nw, nh);
574 g_mutex_unlock(il->data_mutex);
575 image_loader_emit_size(il);
578 static void image_loader_stop_loader(ImageLoader *il)
584 /* some loaders do not have a pixbuf till close, order is important here */
585 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
586 image_loader_sync_pixbuf(il);
587 il->backend.free(il->loader);
590 g_mutex_lock(il->data_mutex);
592 g_mutex_unlock(il->data_mutex);
595 static void image_loader_setup_loader(ImageLoader *il)
597 g_mutex_lock(il->data_mutex);
599 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
601 DEBUG_1("Using custom jpeg loader");
602 image_loader_backend_set_jpeg(&il->backend);
607 if (il->bytes_total >= 10 &&
608 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
609 memcmp(il->mapped_file, "II*\0", 4) == 0))
611 DEBUG_1("Using custom tiff loader");
612 image_loader_backend_set_tiff(&il->backend);
616 image_loader_backend_set_default(&il->backend);
618 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
619 g_mutex_unlock(il->data_mutex);
623 static void image_loader_done(ImageLoader *il)
625 image_loader_stop_loader(il);
627 image_loader_emit_done(il);
630 static void image_loader_error(ImageLoader *il)
632 image_loader_stop_loader(il);
634 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
636 image_loader_emit_error(il);
639 static gboolean image_loader_continue(ImageLoader *il)
644 if (!il) return FALSE;
646 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
647 while (c > 0 && !image_loader_get_stopping(il))
649 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
653 image_loader_done(il);
657 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
659 image_loader_error(il);
668 if (il->bytes_total > 0)
670 image_loader_emit_percent(il);
676 static gboolean image_loader_begin(ImageLoader *il)
680 if (il->pixbuf) return FALSE;
682 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
683 if (b < 1) return FALSE;
685 image_loader_setup_loader(il);
687 g_assert(il->bytes_read == 0);
688 if (il->backend.load) {
690 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
692 image_loader_stop_loader(il);
696 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
698 image_loader_stop_loader(il);
704 /* read until size is known */
705 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
707 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
708 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
710 image_loader_stop_loader(il);
715 if (!il->pixbuf) image_loader_sync_pixbuf(il);
717 if (il->bytes_read == il->bytes_total || b < 1)
719 /* done, handle (broken) loaders that do not have pixbuf till close */
720 image_loader_stop_loader(il);
722 if (!il->pixbuf) return FALSE;
724 image_loader_done(il);
730 image_loader_stop_loader(il);
737 /**************************************************************************************/
738 /* the following functions are always executed in the main thread */
741 static gboolean image_loader_setup_source(ImageLoader *il)
746 if (!il || il->loader || il->mapped_file) return FALSE;
748 il->mapped_file = NULL;
752 ExifData *exif = exif_read_fd(il->fd);
754 if (options->thumbnails.use_exif)
755 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
757 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
762 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
764 exif_free_fd(il->fd, exif);
768 if (!il->mapped_file)
773 pathl = path_from_utf8(il->fd->path);
774 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
776 if (load_fd == -1) return FALSE;
778 if (fstat(load_fd, &st) == 0)
780 il->bytes_total = st.st_size;
788 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
790 if (il->mapped_file == MAP_FAILED)
801 static void image_loader_stop_source(ImageLoader *il)
809 exif_free_preview(il->mapped_file);
813 munmap(il->mapped_file, il->bytes_total);
815 il->mapped_file = NULL;
819 static void image_loader_stop(ImageLoader *il)
825 g_source_remove(il->idle_id);
831 /* stop loader in the other thread */
832 g_mutex_lock(il->data_mutex);
834 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
835 g_mutex_unlock(il->data_mutex);
838 image_loader_stop_loader(il);
839 image_loader_stop_source(il);
843 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
845 g_mutex_lock(il->data_mutex);
846 il->delay_area_ready = enable;
851 list = g_list_reverse(il->area_param_delayed_list);
852 il->area_param_delayed_list = NULL;
853 g_mutex_unlock(il->data_mutex);
859 ImageLoaderAreaParam *par = work->data;
862 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
870 g_mutex_unlock(il->data_mutex);
875 /**************************************************************************************/
876 /* execution via idle calls */
878 static gboolean image_loader_idle_cb(gpointer data)
880 gboolean ret = FALSE;
881 ImageLoader *il = data;
885 ret = image_loader_continue(il);
890 image_loader_stop_source(il);
897 static gboolean image_loader_start_idle(ImageLoader *il)
901 if (!il) return FALSE;
903 if (!il->fd) return FALSE;
905 if (!image_loader_setup_source(il)) return FALSE;
907 ret = image_loader_begin(il);
909 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
913 /**************************************************************************************/
914 /* execution via thread */
917 static GThreadPool *image_loader_thread_pool = NULL;
919 static GCond *image_loader_prio_cond = NULL;
920 static GMutex *image_loader_prio_mutex = NULL;
921 static gint image_loader_prio_num = 0;
924 static void image_loader_thread_enter_high(void)
926 g_mutex_lock(image_loader_prio_mutex);
927 image_loader_prio_num++;
928 g_mutex_unlock(image_loader_prio_mutex);
931 static void image_loader_thread_leave_high(void)
933 g_mutex_lock(image_loader_prio_mutex);
934 image_loader_prio_num--;
935 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
936 g_mutex_unlock(image_loader_prio_mutex);
939 static void image_loader_thread_wait_high(void)
941 g_mutex_lock(image_loader_prio_mutex);
942 while (image_loader_prio_num)
944 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
947 g_mutex_unlock(image_loader_prio_mutex);
951 static void image_loader_thread_run(gpointer data, gpointer user_data)
953 ImageLoader *il = data;
957 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
959 /* low prio, wait untill high prio tasks finishes */
960 image_loader_thread_wait_high();
965 image_loader_thread_enter_high();
968 err = !image_loader_begin(il);
973 loader failed, we have to send signal
974 (idle mode returns the image_loader_begin return value directly)
975 (success is always reported indirectly from image_loader_begin)
977 image_loader_emit_error(il);
982 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
984 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
986 /* low prio, wait untill high prio tasks finishes */
987 image_loader_thread_wait_high();
989 cont = image_loader_continue(il);
991 image_loader_stop_loader(il);
993 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
996 image_loader_thread_leave_high();
999 g_mutex_lock(il->data_mutex);
1000 il->can_destroy = TRUE;
1001 g_cond_signal(il->can_destroy_cond);
1002 g_mutex_unlock(il->data_mutex);
1007 static gboolean image_loader_start_thread(ImageLoader *il)
1009 if (!il) return FALSE;
1011 if (!il->fd) return FALSE;
1015 if (!image_loader_setup_source(il)) return FALSE;
1017 if (!image_loader_thread_pool)
1019 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1020 #if GLIB_CHECK_VERSION(2,32,0)
1021 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1022 g_cond_init(image_loader_prio_cond);
1023 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1024 g_mutex_init(image_loader_prio_mutex);
1026 image_loader_prio_cond = g_cond_new();
1027 image_loader_prio_mutex = g_mutex_new();
1031 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1033 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1034 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1038 #endif /* HAVE_GTHREAD */
1041 /**************************************************************************************/
1042 /* public interface */
1045 gboolean image_loader_start(ImageLoader *il)
1047 if (!il) return FALSE;
1049 if (!il->fd) return FALSE;
1052 return image_loader_start_thread(il);
1054 return image_loader_start_idle(il);
1059 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1060 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1063 if (!il) return NULL;
1065 g_mutex_lock(il->data_mutex);
1067 g_mutex_unlock(il->data_mutex);
1071 gchar *image_loader_get_format(ImageLoader *il)
1076 if (!il || !il->loader) return NULL;
1078 mimev = il->backend.get_format_mime_types(il->loader);
1079 if (!mimev) return NULL;
1081 /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
1082 mime = g_strdup(mimev[0]);
1088 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1092 g_mutex_lock(il->data_mutex);
1093 il->requested_width = width;
1094 il->requested_height = height;
1095 g_mutex_unlock(il->data_mutex);
1098 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1102 g_mutex_lock(il->data_mutex);
1103 il->idle_read_loop_count = count ? count : 1;
1104 g_mutex_unlock(il->data_mutex);
1107 void image_loader_set_priority(ImageLoader *il, gint priority)
1111 if (il->thread) return; /* can't change prio if the thread already runs */
1112 il->idle_priority = priority;
1116 gdouble image_loader_get_percent(ImageLoader *il)
1119 if (!il) return 0.0;
1121 g_mutex_lock(il->data_mutex);
1122 if (il->bytes_total == 0)
1128 ret = (gdouble)il->bytes_read / il->bytes_total;
1130 g_mutex_unlock(il->data_mutex);
1134 gboolean image_loader_get_is_done(ImageLoader *il)
1137 if (!il) return FALSE;
1139 g_mutex_lock(il->data_mutex);
1141 g_mutex_unlock(il->data_mutex);
1146 FileData *image_loader_get_fd(ImageLoader *il)
1149 if (!il) return NULL;
1151 g_mutex_lock(il->data_mutex);
1153 g_mutex_unlock(il->data_mutex);
1158 gboolean image_loader_get_shrunk(ImageLoader *il)
1161 if (!il) return FALSE;
1163 g_mutex_lock(il->data_mutex);
1165 g_mutex_unlock(il->data_mutex);
1169 const gchar *image_loader_get_error(ImageLoader *il)
1171 const gchar *ret = NULL;
1172 if (!il) return NULL;
1173 g_mutex_lock(il->data_mutex);
1174 if (il->error) ret = il->error->message;
1175 g_mutex_unlock(il->data_mutex);
1180 /* FIXME - this can be rather slow and blocks until the size is known */
1181 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1186 il = image_loader_new(fd);
1188 success = image_loader_start_idle(il);
1190 if (success && il->pixbuf)
1192 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1193 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1197 if (width) *width = -1;
1198 if (height) *height = -1;
1201 image_loader_free(il);
1205 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */