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"
27 #include "image_load_ffmpegthumbnailer.h"
31 #include "ui_fileops.h"
32 #include "gq-marshal.h"
37 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
38 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
41 /**************************************************************************************/
42 /* image loader class */
46 SIGNAL_AREA_READY = 0,
54 static guint signals[SIGNAL_COUNT] = { 0 };
56 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
57 static void image_loader_class_init(ImageLoaderClass *class);
58 static void image_loader_finalize(GObject *object);
59 static void image_loader_stop(ImageLoader *il);
61 GType image_loader_get_type(void)
63 static GType type = 0;
66 static const GTypeInfo info = {
67 sizeof(ImageLoaderClass),
69 NULL, /* base_finalize */
70 (GClassInitFunc)image_loader_class_init, /* class_init */
71 NULL, /* class_finalize */
72 NULL, /* class_data */
75 (GInstanceInitFunc)image_loader_init, /* instance_init */
76 NULL /* value_table */
78 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
83 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
85 ImageLoader *il = (ImageLoader *)instance;
89 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
98 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
99 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
100 il->mapped_file = NULL;
102 il->requested_width = 0;
103 il->requested_height = 0;
104 il->actual_width = 0;
105 il->actual_height = 0;
108 il->can_destroy = TRUE;
111 #if GLIB_CHECK_VERSION(2,32,0)
112 il->data_mutex = g_new(GMutex, 1);
113 g_mutex_init(il->data_mutex);
114 il->can_destroy_cond = g_new(GCond, 1);
115 g_cond_init(il->can_destroy_cond);
117 il->data_mutex = g_mutex_new();
118 il->can_destroy_cond = g_cond_new();
121 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
124 static void image_loader_class_init(ImageLoaderClass *class)
126 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
128 // gobject_class->set_property = image_loader_set_property;
129 // gobject_class->get_property = image_loader_get_property;
131 gobject_class->finalize = image_loader_finalize;
134 signals[SIGNAL_AREA_READY] =
135 g_signal_new("area_ready",
136 G_OBJECT_CLASS_TYPE(gobject_class),
138 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
140 gq_marshal_VOID__INT_INT_INT_INT,
147 signals[SIGNAL_ERROR] =
148 g_signal_new("error",
149 G_OBJECT_CLASS_TYPE(gobject_class),
151 G_STRUCT_OFFSET(ImageLoaderClass, error),
153 g_cclosure_marshal_VOID__VOID,
156 signals[SIGNAL_DONE] =
158 G_OBJECT_CLASS_TYPE(gobject_class),
160 G_STRUCT_OFFSET(ImageLoaderClass, done),
162 g_cclosure_marshal_VOID__VOID,
165 signals[SIGNAL_PERCENT] =
166 g_signal_new("percent",
167 G_OBJECT_CLASS_TYPE(gobject_class),
169 G_STRUCT_OFFSET(ImageLoaderClass, percent),
171 g_cclosure_marshal_VOID__DOUBLE,
175 signals[SIGNAL_SIZE] =
176 g_signal_new("size_prepared",
177 G_OBJECT_CLASS_TYPE(gobject_class),
179 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
181 gq_marshal_VOID__INT_INT,
188 static void image_loader_finalize(GObject *object)
190 ImageLoader *il = (ImageLoader *)object;
192 image_loader_stop(il);
194 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
196 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
198 if (il->idle_done_id)
200 g_source_remove(il->idle_done_id);
201 il->idle_done_id = 0;
204 while (g_source_remove_by_user_data(il))
206 DEBUG_2("pending signals detected");
209 while (il->area_param_list)
211 DEBUG_1("pending area_ready signals detected");
212 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
213 g_free(il->area_param_list->data);
214 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
217 while (il->area_param_delayed_list)
219 g_free(il->area_param_delayed_list->data);
220 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
223 if (il->pixbuf) g_object_unref(il->pixbuf);
225 if (il->error) g_error_free(il->error);
227 file_data_unref(il->fd);
229 #if GLIB_CHECK_VERSION(2,32,0)
230 g_mutex_clear(il->data_mutex);
231 g_free(il->data_mutex);
232 g_cond_clear(il->can_destroy_cond);
233 g_free(il->can_destroy_cond);
235 g_mutex_free(il->data_mutex);
236 g_cond_free(il->can_destroy_cond);
241 void image_loader_free(ImageLoader *il)
244 g_object_unref(G_OBJECT(il));
248 ImageLoader *image_loader_new(FileData *fd)
252 if (!fd) return NULL;
254 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
256 il->fd = file_data_ref(fd);
261 /**************************************************************************************/
262 /* send signals via idle calbacks - the callback are executed in the main thread */
264 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
265 struct _ImageLoaderAreaParam {
274 static gboolean image_loader_emit_area_ready_cb(gpointer data)
276 ImageLoaderAreaParam *par = data;
277 ImageLoader *il = par->il;
279 g_mutex_lock(il->data_mutex);
280 il->area_param_list = g_list_remove(il->area_param_list, par);
286 g_mutex_unlock(il->data_mutex);
288 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
293 static gboolean image_loader_emit_done_cb(gpointer data)
295 ImageLoader *il = data;
296 g_signal_emit(il, signals[SIGNAL_DONE], 0);
300 static gboolean image_loader_emit_error_cb(gpointer data)
302 ImageLoader *il = data;
303 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
307 static gboolean image_loader_emit_percent_cb(gpointer data)
309 ImageLoader *il = data;
310 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
314 static gboolean image_loader_emit_size_cb(gpointer data)
317 ImageLoader *il = data;
318 g_mutex_lock(il->data_mutex);
319 width = il->actual_width;
320 height = il->actual_height;
321 g_mutex_unlock(il->data_mutex);
322 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
327 /* DONE and ERROR are emited only once, thus they can have normal priority
328 PERCENT and AREA_READY should be processed ASAP
331 static void image_loader_emit_done(ImageLoader *il)
333 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
336 static void image_loader_emit_error(ImageLoader *il)
338 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
341 static void image_loader_emit_percent(ImageLoader *il)
343 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
346 static void image_loader_emit_size(ImageLoader *il)
348 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
351 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
355 ImageLoaderAreaParam *prev_par = (*list)->data;
356 if (prev_par->x == x && prev_par->w == w &&
357 prev_par->y + prev_par->h == y)
359 /* we can merge the notifications */
363 if (prev_par->x == x && prev_par->w == w &&
364 y + h == prev_par->y)
366 /* we can merge the notifications */
371 if (prev_par->y == y && prev_par->h == h &&
372 prev_par->x + prev_par->w == x)
374 /* we can merge the notifications */
378 if (prev_par->y == y && prev_par->h == h &&
379 x + w == prev_par->x)
381 /* we can merge the notifications */
388 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
395 *list = g_list_prepend(*list, par);
399 /* this function expects that il->data_mutex is locked by caller */
400 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
402 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
406 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
410 /**************************************************************************************/
411 /* the following functions may be executed in separate thread */
413 /* this function expects that il->data_mutex is locked by caller */
414 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
416 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
421 static gboolean image_loader_get_stopping(ImageLoader *il)
424 if (!il) return FALSE;
426 g_mutex_lock(il->data_mutex);
428 g_mutex_unlock(il->data_mutex);
434 static void image_loader_sync_pixbuf(ImageLoader *il)
438 g_mutex_lock(il->data_mutex);
442 g_mutex_unlock(il->data_mutex);
446 pb = il->backend.get_pixbuf(il->loader);
448 if (pb == il->pixbuf)
450 g_mutex_unlock(il->data_mutex);
454 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
456 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
459 if (il->pixbuf) g_object_unref(il->pixbuf);
462 if (il->pixbuf) g_object_ref(il->pixbuf);
464 g_mutex_unlock(il->data_mutex);
467 static void image_loader_area_updated_cb(gpointer loader,
468 guint x, guint y, guint w, guint h,
471 ImageLoader *il = data;
473 if (!image_loader_get_pixbuf(il))
475 image_loader_sync_pixbuf(il);
476 if (!image_loader_get_pixbuf(il))
478 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
482 g_mutex_lock(il->data_mutex);
483 if (il->delay_area_ready)
484 image_loader_queue_delayed_area_ready(il, x, y, w, h);
486 image_loader_emit_area_ready(il, x, y, w, h);
488 if (il->stopping) il->backend.abort(il->loader);
490 g_mutex_unlock(il->data_mutex);
493 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
495 ImageLoader *il = data;
501 http://bugzilla.gnome.org/show_bug.cgi?id=547669
502 http://bugzilla.gnome.org/show_bug.cgi?id=589334
504 gchar *format = il->backend.get_format_name(loader);
505 if (strcmp(format, "svg") == 0 ||
506 strcmp(format, "xpm") == 0)
514 pb = il->backend.get_pixbuf(loader);
516 h = gdk_pixbuf_get_height(pb);
517 rs = gdk_pixbuf_get_rowstride(pb);
518 pix = gdk_pixbuf_get_pixels(pb);
520 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
524 static void image_loader_size_cb(gpointer loader,
525 gint width, gint height, gpointer data)
527 ImageLoader *il = data;
529 gboolean scale = FALSE;
532 g_mutex_lock(il->data_mutex);
533 il->actual_width = width;
534 il->actual_height = height;
535 if (il->requested_width < 1 || il->requested_height < 1)
537 g_mutex_unlock(il->data_mutex);
538 image_loader_emit_size(il);
541 g_mutex_unlock(il->data_mutex);
543 #ifdef HAVE_FFMPEGTHUMBNAILER
544 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
547 mime_types = il->backend.get_format_mime_types(loader);
549 while (mime_types[n] && !scale)
551 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
554 g_strfreev(mime_types);
558 image_loader_emit_size(il);
562 g_mutex_lock(il->data_mutex);
565 if (width > il->requested_width || height > il->requested_height)
568 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
570 nw = il->requested_width;
571 nh = (gdouble)nw / width * height;
576 nh = il->requested_height;
577 nw = (gdouble)nh / height * width;
581 il->actual_width = nw;
582 il->actual_height = nh;
583 il->backend.set_size(loader, nw, nh);
587 g_mutex_unlock(il->data_mutex);
588 image_loader_emit_size(il);
591 static void image_loader_stop_loader(ImageLoader *il)
597 /* some loaders do not have a pixbuf till close, order is important here */
598 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
599 image_loader_sync_pixbuf(il);
600 il->backend.free(il->loader);
603 g_mutex_lock(il->data_mutex);
605 g_mutex_unlock(il->data_mutex);
608 static void image_loader_setup_loader(ImageLoader *il)
610 g_mutex_lock(il->data_mutex);
611 #ifdef HAVE_FFMPEGTHUMBNAILER
612 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
614 DEBUG_1("Using custom ffmpegthumbnailer loader");
615 image_loader_backend_set_ft(&il->backend);
620 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
622 DEBUG_1("Using custom jpeg loader");
623 image_loader_backend_set_jpeg(&il->backend);
628 if (il->bytes_total >= 10 &&
629 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
630 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
631 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
632 memcmp(il->mapped_file, "II*\0", 4) == 0))
634 DEBUG_1("Using custom tiff loader");
635 image_loader_backend_set_tiff(&il->backend);
639 image_loader_backend_set_default(&il->backend);
641 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
642 g_mutex_unlock(il->data_mutex);
646 static void image_loader_done(ImageLoader *il)
648 image_loader_stop_loader(il);
650 image_loader_emit_done(il);
653 static void image_loader_error(ImageLoader *il)
655 image_loader_stop_loader(il);
657 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
659 image_loader_emit_error(il);
662 static gboolean image_loader_continue(ImageLoader *il)
667 if (!il) return FALSE;
669 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
670 while (c > 0 && !image_loader_get_stopping(il))
672 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
676 image_loader_done(il);
680 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
682 image_loader_error(il);
691 if (il->bytes_total > 0)
693 image_loader_emit_percent(il);
699 static gboolean image_loader_begin(ImageLoader *il)
703 if (il->pixbuf) return FALSE;
705 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
706 if (b < 1) return FALSE;
708 image_loader_setup_loader(il);
710 g_assert(il->bytes_read == 0);
711 if (il->backend.load) {
713 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
715 image_loader_stop_loader(il);
719 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
721 image_loader_stop_loader(il);
727 /* read until size is known */
728 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
730 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
731 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
733 image_loader_stop_loader(il);
738 if (!il->pixbuf) image_loader_sync_pixbuf(il);
740 if (il->bytes_read == il->bytes_total || b < 1)
742 /* done, handle (broken) loaders that do not have pixbuf till close */
743 image_loader_stop_loader(il);
745 if (!il->pixbuf) return FALSE;
747 image_loader_done(il);
753 image_loader_stop_loader(il);
760 /**************************************************************************************/
761 /* the following functions are always executed in the main thread */
764 static gboolean image_loader_setup_source(ImageLoader *il)
769 if (!il || il->loader || il->mapped_file) return FALSE;
771 il->mapped_file = NULL;
775 ExifData *exif = exif_read_fd(il->fd);
777 if (options->thumbnails.use_exif)
778 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
780 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
785 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
787 exif_free_fd(il->fd, exif);
791 if (!il->mapped_file)
796 pathl = path_from_utf8(il->fd->path);
797 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
799 if (load_fd == -1) return FALSE;
801 if (fstat(load_fd, &st) == 0)
803 il->bytes_total = st.st_size;
811 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
813 if (il->mapped_file == MAP_FAILED)
824 static void image_loader_stop_source(ImageLoader *il)
832 exif_free_preview(il->mapped_file);
836 munmap(il->mapped_file, il->bytes_total);
838 il->mapped_file = NULL;
842 static void image_loader_stop(ImageLoader *il)
848 g_source_remove(il->idle_id);
854 /* stop loader in the other thread */
855 g_mutex_lock(il->data_mutex);
857 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
858 g_mutex_unlock(il->data_mutex);
861 image_loader_stop_loader(il);
862 image_loader_stop_source(il);
866 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
868 g_mutex_lock(il->data_mutex);
869 il->delay_area_ready = enable;
874 list = g_list_reverse(il->area_param_delayed_list);
875 il->area_param_delayed_list = NULL;
876 g_mutex_unlock(il->data_mutex);
882 ImageLoaderAreaParam *par = work->data;
885 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
893 g_mutex_unlock(il->data_mutex);
898 /**************************************************************************************/
899 /* execution via idle calls */
901 static gboolean image_loader_idle_cb(gpointer data)
903 gboolean ret = FALSE;
904 ImageLoader *il = data;
908 ret = image_loader_continue(il);
913 image_loader_stop_source(il);
920 static gboolean image_loader_start_idle(ImageLoader *il)
924 if (!il) return FALSE;
926 if (!il->fd) return FALSE;
928 if (!image_loader_setup_source(il)) return FALSE;
930 ret = image_loader_begin(il);
932 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
936 /**************************************************************************************/
937 /* execution via thread */
940 static GThreadPool *image_loader_thread_pool = NULL;
942 static GCond *image_loader_prio_cond = NULL;
943 static GMutex *image_loader_prio_mutex = NULL;
944 static gint image_loader_prio_num = 0;
947 static void image_loader_thread_enter_high(void)
949 g_mutex_lock(image_loader_prio_mutex);
950 image_loader_prio_num++;
951 g_mutex_unlock(image_loader_prio_mutex);
954 static void image_loader_thread_leave_high(void)
956 g_mutex_lock(image_loader_prio_mutex);
957 image_loader_prio_num--;
958 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
959 g_mutex_unlock(image_loader_prio_mutex);
962 static void image_loader_thread_wait_high(void)
964 g_mutex_lock(image_loader_prio_mutex);
965 while (image_loader_prio_num)
967 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
970 g_mutex_unlock(image_loader_prio_mutex);
974 static void image_loader_thread_run(gpointer data, gpointer user_data)
976 ImageLoader *il = data;
980 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
982 /* low prio, wait untill high prio tasks finishes */
983 image_loader_thread_wait_high();
988 image_loader_thread_enter_high();
991 err = !image_loader_begin(il);
996 loader failed, we have to send signal
997 (idle mode returns the image_loader_begin return value directly)
998 (success is always reported indirectly from image_loader_begin)
1000 image_loader_emit_error(il);
1005 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1007 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1009 /* low prio, wait untill high prio tasks finishes */
1010 image_loader_thread_wait_high();
1012 cont = image_loader_continue(il);
1014 image_loader_stop_loader(il);
1016 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1019 image_loader_thread_leave_high();
1022 g_mutex_lock(il->data_mutex);
1023 il->can_destroy = TRUE;
1024 g_cond_signal(il->can_destroy_cond);
1025 g_mutex_unlock(il->data_mutex);
1030 static gboolean image_loader_start_thread(ImageLoader *il)
1032 if (!il) return FALSE;
1034 if (!il->fd) return FALSE;
1038 if (!image_loader_setup_source(il)) return FALSE;
1040 if (!image_loader_thread_pool)
1042 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1043 #if GLIB_CHECK_VERSION(2,32,0)
1044 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1045 g_cond_init(image_loader_prio_cond);
1046 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1047 g_mutex_init(image_loader_prio_mutex);
1049 image_loader_prio_cond = g_cond_new();
1050 image_loader_prio_mutex = g_mutex_new();
1054 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1056 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1057 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1061 #endif /* HAVE_GTHREAD */
1064 /**************************************************************************************/
1065 /* public interface */
1068 gboolean image_loader_start(ImageLoader *il)
1070 if (!il) return FALSE;
1072 if (!il->fd) return FALSE;
1075 return image_loader_start_thread(il);
1077 return image_loader_start_idle(il);
1082 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1083 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1086 if (!il) return NULL;
1088 g_mutex_lock(il->data_mutex);
1090 g_mutex_unlock(il->data_mutex);
1094 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1098 g_mutex_lock(il->data_mutex);
1099 il->requested_width = width;
1100 il->requested_height = height;
1101 g_mutex_unlock(il->data_mutex);
1104 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1108 g_mutex_lock(il->data_mutex);
1109 il->idle_read_loop_count = count ? count : 1;
1110 g_mutex_unlock(il->data_mutex);
1113 void image_loader_set_priority(ImageLoader *il, gint priority)
1117 if (il->thread) return; /* can't change prio if the thread already runs */
1118 il->idle_priority = priority;
1122 gdouble image_loader_get_percent(ImageLoader *il)
1125 if (!il) return 0.0;
1127 g_mutex_lock(il->data_mutex);
1128 if (il->bytes_total == 0)
1134 ret = (gdouble)il->bytes_read / il->bytes_total;
1136 g_mutex_unlock(il->data_mutex);
1140 gboolean image_loader_get_is_done(ImageLoader *il)
1143 if (!il) return FALSE;
1145 g_mutex_lock(il->data_mutex);
1147 g_mutex_unlock(il->data_mutex);
1152 FileData *image_loader_get_fd(ImageLoader *il)
1155 if (!il) return NULL;
1157 g_mutex_lock(il->data_mutex);
1159 g_mutex_unlock(il->data_mutex);
1164 gboolean image_loader_get_shrunk(ImageLoader *il)
1167 if (!il) return FALSE;
1169 g_mutex_lock(il->data_mutex);
1171 g_mutex_unlock(il->data_mutex);
1175 const gchar *image_loader_get_error(ImageLoader *il)
1177 const gchar *ret = NULL;
1178 if (!il) return NULL;
1179 g_mutex_lock(il->data_mutex);
1180 if (il->error) ret = il->error->message;
1181 g_mutex_unlock(il->data_mutex);
1186 /* FIXME - this can be rather slow and blocks until the size is known */
1187 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1192 il = image_loader_new(fd);
1194 success = image_loader_start_idle(il);
1196 if (success && il->pixbuf)
1198 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1199 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1203 if (width) *width = -1;
1204 if (height) *height = -1;
1207 image_loader_free(il);
1211 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */