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_dds.h"
28 #include "image_load_pdf.h"
29 #include "image_load_ffmpegthumbnailer.h"
30 #include "image_load_collection.h"
34 #include "ui_fileops.h"
35 #include "gq-marshal.h"
40 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
41 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
44 /**************************************************************************************/
45 /* image loader class */
49 SIGNAL_AREA_READY = 0,
57 static guint signals[SIGNAL_COUNT] = { 0 };
59 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
60 static void image_loader_class_init(ImageLoaderClass *class);
61 static void image_loader_finalize(GObject *object);
62 static void image_loader_stop(ImageLoader *il);
64 GType image_loader_get_type(void)
66 static GType type = 0;
69 static const GTypeInfo info = {
70 sizeof(ImageLoaderClass),
72 NULL, /* base_finalize */
73 (GClassInitFunc)image_loader_class_init, /* class_init */
74 NULL, /* class_finalize */
75 NULL, /* class_data */
78 (GInstanceInitFunc)image_loader_init, /* instance_init */
79 NULL /* value_table */
81 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
86 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
88 ImageLoader *il = (ImageLoader *)instance;
92 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
101 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
102 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
103 il->mapped_file = NULL;
105 il->requested_width = 0;
106 il->requested_height = 0;
107 il->actual_width = 0;
108 il->actual_height = 0;
111 il->can_destroy = TRUE;
114 #if GLIB_CHECK_VERSION(2,32,0)
115 il->data_mutex = g_new(GMutex, 1);
116 g_mutex_init(il->data_mutex);
117 il->can_destroy_cond = g_new(GCond, 1);
118 g_cond_init(il->can_destroy_cond);
120 il->data_mutex = g_mutex_new();
121 il->can_destroy_cond = g_cond_new();
124 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
127 static void image_loader_class_init(ImageLoaderClass *class)
129 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
131 // gobject_class->set_property = image_loader_set_property;
132 // gobject_class->get_property = image_loader_get_property;
134 gobject_class->finalize = image_loader_finalize;
137 signals[SIGNAL_AREA_READY] =
138 g_signal_new("area_ready",
139 G_OBJECT_CLASS_TYPE(gobject_class),
141 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
143 gq_marshal_VOID__INT_INT_INT_INT,
150 signals[SIGNAL_ERROR] =
151 g_signal_new("error",
152 G_OBJECT_CLASS_TYPE(gobject_class),
154 G_STRUCT_OFFSET(ImageLoaderClass, error),
156 g_cclosure_marshal_VOID__VOID,
159 signals[SIGNAL_DONE] =
161 G_OBJECT_CLASS_TYPE(gobject_class),
163 G_STRUCT_OFFSET(ImageLoaderClass, done),
165 g_cclosure_marshal_VOID__VOID,
168 signals[SIGNAL_PERCENT] =
169 g_signal_new("percent",
170 G_OBJECT_CLASS_TYPE(gobject_class),
172 G_STRUCT_OFFSET(ImageLoaderClass, percent),
174 g_cclosure_marshal_VOID__DOUBLE,
178 signals[SIGNAL_SIZE] =
179 g_signal_new("size_prepared",
180 G_OBJECT_CLASS_TYPE(gobject_class),
182 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
184 gq_marshal_VOID__INT_INT,
191 static void image_loader_finalize(GObject *object)
193 ImageLoader *il = (ImageLoader *)object;
195 image_loader_stop(il);
197 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
199 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
201 if (il->idle_done_id)
203 g_source_remove(il->idle_done_id);
204 il->idle_done_id = 0;
207 while (g_source_remove_by_user_data(il))
209 DEBUG_2("pending signals detected");
212 while (il->area_param_list)
214 DEBUG_1("pending area_ready signals detected");
215 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
216 g_free(il->area_param_list->data);
217 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
220 while (il->area_param_delayed_list)
222 g_free(il->area_param_delayed_list->data);
223 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
226 if (il->pixbuf) g_object_unref(il->pixbuf);
228 if (il->error) g_error_free(il->error);
230 file_data_unref(il->fd);
232 #if GLIB_CHECK_VERSION(2,32,0)
233 g_mutex_clear(il->data_mutex);
234 g_free(il->data_mutex);
235 g_cond_clear(il->can_destroy_cond);
236 g_free(il->can_destroy_cond);
238 g_mutex_free(il->data_mutex);
239 g_cond_free(il->can_destroy_cond);
244 void image_loader_free(ImageLoader *il)
247 g_object_unref(G_OBJECT(il));
251 ImageLoader *image_loader_new(FileData *fd)
255 if (!fd) return NULL;
257 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
259 il->fd = file_data_ref(fd);
264 /**************************************************************************************/
265 /* send signals via idle calbacks - the callback are executed in the main thread */
267 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
268 struct _ImageLoaderAreaParam {
277 static gboolean image_loader_emit_area_ready_cb(gpointer data)
279 ImageLoaderAreaParam *par = data;
280 ImageLoader *il = par->il;
282 g_mutex_lock(il->data_mutex);
283 il->area_param_list = g_list_remove(il->area_param_list, par);
289 g_mutex_unlock(il->data_mutex);
291 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
296 static gboolean image_loader_emit_done_cb(gpointer data)
298 ImageLoader *il = data;
299 g_signal_emit(il, signals[SIGNAL_DONE], 0);
303 static gboolean image_loader_emit_error_cb(gpointer data)
305 ImageLoader *il = data;
306 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
310 static gboolean image_loader_emit_percent_cb(gpointer data)
312 ImageLoader *il = data;
313 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
317 static gboolean image_loader_emit_size_cb(gpointer data)
320 ImageLoader *il = data;
321 g_mutex_lock(il->data_mutex);
322 width = il->actual_width;
323 height = il->actual_height;
324 g_mutex_unlock(il->data_mutex);
325 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
330 /* DONE and ERROR are emited only once, thus they can have normal priority
331 PERCENT and AREA_READY should be processed ASAP
334 static void image_loader_emit_done(ImageLoader *il)
336 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
339 static void image_loader_emit_error(ImageLoader *il)
341 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
344 static void image_loader_emit_percent(ImageLoader *il)
346 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
349 static void image_loader_emit_size(ImageLoader *il)
351 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
354 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
358 ImageLoaderAreaParam *prev_par = (*list)->data;
359 if (prev_par->x == x && prev_par->w == w &&
360 prev_par->y + prev_par->h == y)
362 /* we can merge the notifications */
366 if (prev_par->x == x && prev_par->w == w &&
367 y + h == prev_par->y)
369 /* we can merge the notifications */
374 if (prev_par->y == y && prev_par->h == h &&
375 prev_par->x + prev_par->w == x)
377 /* we can merge the notifications */
381 if (prev_par->y == y && prev_par->h == h &&
382 x + w == prev_par->x)
384 /* we can merge the notifications */
391 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
398 *list = g_list_prepend(*list, par);
402 /* this function expects that il->data_mutex is locked by caller */
403 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
405 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
409 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
413 /**************************************************************************************/
414 /* the following functions may be executed in separate thread */
416 /* this function expects that il->data_mutex is locked by caller */
417 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
419 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
424 static gboolean image_loader_get_stopping(ImageLoader *il)
427 if (!il) return FALSE;
429 g_mutex_lock(il->data_mutex);
431 g_mutex_unlock(il->data_mutex);
437 static void image_loader_sync_pixbuf(ImageLoader *il)
441 g_mutex_lock(il->data_mutex);
445 g_mutex_unlock(il->data_mutex);
449 pb = il->backend.get_pixbuf(il->loader);
451 if (pb == il->pixbuf)
453 g_mutex_unlock(il->data_mutex);
457 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
459 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
462 if (il->pixbuf) g_object_unref(il->pixbuf);
465 if (il->pixbuf) g_object_ref(il->pixbuf);
467 g_mutex_unlock(il->data_mutex);
470 static void image_loader_area_updated_cb(gpointer loader,
471 guint x, guint y, guint w, guint h,
474 ImageLoader *il = data;
476 if (!image_loader_get_pixbuf(il))
478 image_loader_sync_pixbuf(il);
479 if (!image_loader_get_pixbuf(il))
481 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
485 g_mutex_lock(il->data_mutex);
486 if (il->delay_area_ready)
487 image_loader_queue_delayed_area_ready(il, x, y, w, h);
489 image_loader_emit_area_ready(il, x, y, w, h);
491 if (il->stopping) il->backend.abort(il->loader);
493 g_mutex_unlock(il->data_mutex);
496 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
498 ImageLoader *il = data;
504 http://bugzilla.gnome.org/show_bug.cgi?id=547669
505 http://bugzilla.gnome.org/show_bug.cgi?id=589334
507 gchar *format = il->backend.get_format_name(loader);
508 if (strcmp(format, "svg") == 0 ||
509 strcmp(format, "xpm") == 0)
517 pb = il->backend.get_pixbuf(loader);
519 h = gdk_pixbuf_get_height(pb);
520 rs = gdk_pixbuf_get_rowstride(pb);
521 pix = gdk_pixbuf_get_pixels(pb);
523 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
527 static void image_loader_size_cb(gpointer loader,
528 gint width, gint height, gpointer data)
530 ImageLoader *il = data;
532 gboolean scale = FALSE;
535 g_mutex_lock(il->data_mutex);
536 il->actual_width = width;
537 il->actual_height = height;
538 if (il->requested_width < 1 || il->requested_height < 1)
540 g_mutex_unlock(il->data_mutex);
541 image_loader_emit_size(il);
544 g_mutex_unlock(il->data_mutex);
546 #ifdef HAVE_FFMPEGTHUMBNAILER
547 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
550 mime_types = il->backend.get_format_mime_types(loader);
552 while (mime_types[n] && !scale)
554 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
557 g_strfreev(mime_types);
561 image_loader_emit_size(il);
565 g_mutex_lock(il->data_mutex);
568 if (width > il->requested_width || height > il->requested_height)
571 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
573 nw = il->requested_width;
574 nh = (gdouble)nw / width * height;
579 nh = il->requested_height;
580 nw = (gdouble)nh / height * width;
584 il->actual_width = nw;
585 il->actual_height = nh;
586 il->backend.set_size(loader, nw, nh);
590 g_mutex_unlock(il->data_mutex);
591 image_loader_emit_size(il);
594 static void image_loader_stop_loader(ImageLoader *il)
600 /* some loaders do not have a pixbuf till close, order is important here */
601 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
602 image_loader_sync_pixbuf(il);
603 il->backend.free(il->loader);
606 g_mutex_lock(il->data_mutex);
608 g_mutex_unlock(il->data_mutex);
611 static void image_loader_setup_loader(ImageLoader *il)
613 g_mutex_lock(il->data_mutex);
614 #ifdef HAVE_FFMPEGTHUMBNAILER
615 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
617 DEBUG_1("Using custom ffmpegthumbnailer loader");
618 image_loader_backend_set_ft(&il->backend);
623 if (il->fd->format_class == FORMAT_CLASS_PDF)
625 DEBUG_1("Using custom pdf loader");
626 image_loader_backend_set_pdf(&il->backend);
631 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
633 DEBUG_1("Using custom jpeg loader");
634 image_loader_backend_set_jpeg(&il->backend);
639 if (il->bytes_total >= 10 &&
640 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
641 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
642 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
643 memcmp(il->mapped_file, "II*\0", 4) == 0))
645 DEBUG_1("Using custom tiff loader");
646 image_loader_backend_set_tiff(&il->backend);
650 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
652 DEBUG_1("Using dds loader");
653 image_loader_backend_set_dds(&il->backend);
656 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
658 DEBUG_1("Using custom collection loader");
659 image_loader_backend_set_collection(&il->backend);
662 image_loader_backend_set_default(&il->backend);
664 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
665 g_mutex_unlock(il->data_mutex);
669 static void image_loader_done(ImageLoader *il)
671 image_loader_stop_loader(il);
673 image_loader_emit_done(il);
676 static void image_loader_error(ImageLoader *il)
678 image_loader_stop_loader(il);
680 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
682 image_loader_emit_error(il);
685 static gboolean image_loader_continue(ImageLoader *il)
690 if (!il) return FALSE;
692 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
693 while (c > 0 && !image_loader_get_stopping(il))
695 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
699 image_loader_done(il);
703 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
705 image_loader_error(il);
714 if (il->bytes_total > 0)
716 image_loader_emit_percent(il);
722 static gboolean image_loader_begin(ImageLoader *il)
726 if (il->pixbuf) return FALSE;
728 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
729 if (b < 1) return FALSE;
731 image_loader_setup_loader(il);
733 g_assert(il->bytes_read == 0);
734 if (il->backend.load) {
736 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
738 image_loader_stop_loader(il);
742 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
744 image_loader_stop_loader(il);
750 /* read until size is known */
751 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
753 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
754 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
756 image_loader_stop_loader(il);
761 if (!il->pixbuf) image_loader_sync_pixbuf(il);
763 if (il->bytes_read == il->bytes_total || b < 1)
765 /* done, handle (broken) loaders that do not have pixbuf till close */
766 image_loader_stop_loader(il);
768 if (!il->pixbuf) return FALSE;
770 image_loader_done(il);
776 image_loader_stop_loader(il);
783 /**************************************************************************************/
784 /* the following functions are always executed in the main thread */
787 static gboolean image_loader_setup_source(ImageLoader *il)
792 if (!il || il->loader || il->mapped_file) return FALSE;
794 il->mapped_file = NULL;
798 ExifData *exif = exif_read_fd(il->fd);
800 if (options->thumbnails.use_exif)
801 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
803 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
808 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
810 exif_free_fd(il->fd, exif);
814 if (!il->mapped_file)
819 pathl = path_from_utf8(il->fd->path);
820 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
822 if (load_fd == -1) return FALSE;
824 if (fstat(load_fd, &st) == 0)
826 il->bytes_total = st.st_size;
834 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
836 if (il->mapped_file == MAP_FAILED)
847 static void image_loader_stop_source(ImageLoader *il)
855 exif_free_preview(il->mapped_file);
859 munmap(il->mapped_file, il->bytes_total);
861 il->mapped_file = NULL;
865 static void image_loader_stop(ImageLoader *il)
871 g_source_remove(il->idle_id);
877 /* stop loader in the other thread */
878 g_mutex_lock(il->data_mutex);
880 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
881 g_mutex_unlock(il->data_mutex);
884 image_loader_stop_loader(il);
885 image_loader_stop_source(il);
889 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
891 g_mutex_lock(il->data_mutex);
892 il->delay_area_ready = enable;
897 list = g_list_reverse(il->area_param_delayed_list);
898 il->area_param_delayed_list = NULL;
899 g_mutex_unlock(il->data_mutex);
905 ImageLoaderAreaParam *par = work->data;
908 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
916 g_mutex_unlock(il->data_mutex);
921 /**************************************************************************************/
922 /* execution via idle calls */
924 static gboolean image_loader_idle_cb(gpointer data)
926 gboolean ret = FALSE;
927 ImageLoader *il = data;
931 ret = image_loader_continue(il);
936 image_loader_stop_source(il);
943 static gboolean image_loader_start_idle(ImageLoader *il)
947 if (!il) return FALSE;
949 if (!il->fd) return FALSE;
951 if (!image_loader_setup_source(il)) return FALSE;
953 ret = image_loader_begin(il);
955 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
959 /**************************************************************************************/
960 /* execution via thread */
963 static GThreadPool *image_loader_thread_pool = NULL;
965 static GCond *image_loader_prio_cond = NULL;
966 static GMutex *image_loader_prio_mutex = NULL;
967 static gint image_loader_prio_num = 0;
970 static void image_loader_thread_enter_high(void)
972 g_mutex_lock(image_loader_prio_mutex);
973 image_loader_prio_num++;
974 g_mutex_unlock(image_loader_prio_mutex);
977 static void image_loader_thread_leave_high(void)
979 g_mutex_lock(image_loader_prio_mutex);
980 image_loader_prio_num--;
981 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
982 g_mutex_unlock(image_loader_prio_mutex);
985 static void image_loader_thread_wait_high(void)
987 g_mutex_lock(image_loader_prio_mutex);
988 while (image_loader_prio_num)
990 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
993 g_mutex_unlock(image_loader_prio_mutex);
997 static void image_loader_thread_run(gpointer data, gpointer user_data)
999 ImageLoader *il = data;
1003 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1005 /* low prio, wait untill high prio tasks finishes */
1006 image_loader_thread_wait_high();
1011 image_loader_thread_enter_high();
1014 err = !image_loader_begin(il);
1019 loader failed, we have to send signal
1020 (idle mode returns the image_loader_begin return value directly)
1021 (success is always reported indirectly from image_loader_begin)
1023 image_loader_emit_error(il);
1028 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1030 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1032 /* low prio, wait untill high prio tasks finishes */
1033 image_loader_thread_wait_high();
1035 cont = image_loader_continue(il);
1037 image_loader_stop_loader(il);
1039 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1042 image_loader_thread_leave_high();
1045 g_mutex_lock(il->data_mutex);
1046 il->can_destroy = TRUE;
1047 g_cond_signal(il->can_destroy_cond);
1048 g_mutex_unlock(il->data_mutex);
1053 static gboolean image_loader_start_thread(ImageLoader *il)
1055 if (!il) return FALSE;
1057 if (!il->fd) return FALSE;
1061 if (!image_loader_setup_source(il)) return FALSE;
1063 if (!image_loader_thread_pool)
1065 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1066 #if GLIB_CHECK_VERSION(2,32,0)
1067 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1068 g_cond_init(image_loader_prio_cond);
1069 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1070 g_mutex_init(image_loader_prio_mutex);
1072 image_loader_prio_cond = g_cond_new();
1073 image_loader_prio_mutex = g_mutex_new();
1077 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1079 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1080 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1084 #endif /* HAVE_GTHREAD */
1087 /**************************************************************************************/
1088 /* public interface */
1091 gboolean image_loader_start(ImageLoader *il)
1093 if (!il) return FALSE;
1095 if (!il->fd) return FALSE;
1098 return image_loader_start_thread(il);
1100 return image_loader_start_idle(il);
1105 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1106 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1109 if (!il) return NULL;
1111 g_mutex_lock(il->data_mutex);
1113 g_mutex_unlock(il->data_mutex);
1117 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1121 g_mutex_lock(il->data_mutex);
1122 il->requested_width = width;
1123 il->requested_height = height;
1124 g_mutex_unlock(il->data_mutex);
1127 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1131 g_mutex_lock(il->data_mutex);
1132 il->idle_read_loop_count = count ? count : 1;
1133 g_mutex_unlock(il->data_mutex);
1136 void image_loader_set_priority(ImageLoader *il, gint priority)
1140 if (il->thread) return; /* can't change prio if the thread already runs */
1141 il->idle_priority = priority;
1145 gdouble image_loader_get_percent(ImageLoader *il)
1148 if (!il) return 0.0;
1150 g_mutex_lock(il->data_mutex);
1151 if (il->bytes_total == 0)
1157 ret = (gdouble)il->bytes_read / il->bytes_total;
1159 g_mutex_unlock(il->data_mutex);
1163 gboolean image_loader_get_is_done(ImageLoader *il)
1166 if (!il) return FALSE;
1168 g_mutex_lock(il->data_mutex);
1170 g_mutex_unlock(il->data_mutex);
1175 FileData *image_loader_get_fd(ImageLoader *il)
1178 if (!il) return NULL;
1180 g_mutex_lock(il->data_mutex);
1182 g_mutex_unlock(il->data_mutex);
1187 gboolean image_loader_get_shrunk(ImageLoader *il)
1190 if (!il) return FALSE;
1192 g_mutex_lock(il->data_mutex);
1194 g_mutex_unlock(il->data_mutex);
1198 const gchar *image_loader_get_error(ImageLoader *il)
1200 const gchar *ret = NULL;
1201 if (!il) return NULL;
1202 g_mutex_lock(il->data_mutex);
1203 if (il->error) ret = il->error->message;
1204 g_mutex_unlock(il->data_mutex);
1209 /* FIXME - this can be rather slow and blocks until the size is known */
1210 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1215 il = image_loader_new(fd);
1217 success = image_loader_start_idle(il);
1219 if (success && il->pixbuf)
1221 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1222 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1226 if (width) *width = -1;
1227 if (height) *height = -1;
1230 image_loader_free(il);
1234 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */