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_heif.h"
30 #include "image_load_ffmpegthumbnailer.h"
31 #include "image_load_collection.h"
35 #include "ui_fileops.h"
36 #include "gq-marshal.h"
41 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
42 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
45 /**************************************************************************************/
46 /* image loader class */
50 SIGNAL_AREA_READY = 0,
58 static guint signals[SIGNAL_COUNT] = { 0 };
60 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
61 static void image_loader_class_init(ImageLoaderClass *class);
62 static void image_loader_finalize(GObject *object);
63 static void image_loader_stop(ImageLoader *il);
65 GType image_loader_get_type(void)
67 static GType type = 0;
70 static const GTypeInfo info = {
71 sizeof(ImageLoaderClass),
73 NULL, /* base_finalize */
74 (GClassInitFunc)image_loader_class_init, /* class_init */
75 NULL, /* class_finalize */
76 NULL, /* class_data */
79 (GInstanceInitFunc)image_loader_init, /* instance_init */
80 NULL /* value_table */
82 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
87 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
89 ImageLoader *il = (ImageLoader *)instance;
93 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
100 il->idle_done_id = 0;
102 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
103 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
104 il->mapped_file = NULL;
106 il->requested_width = 0;
107 il->requested_height = 0;
108 il->actual_width = 0;
109 il->actual_height = 0;
112 il->can_destroy = TRUE;
115 #if GLIB_CHECK_VERSION(2,32,0)
116 il->data_mutex = g_new(GMutex, 1);
117 g_mutex_init(il->data_mutex);
118 il->can_destroy_cond = g_new(GCond, 1);
119 g_cond_init(il->can_destroy_cond);
121 il->data_mutex = g_mutex_new();
122 il->can_destroy_cond = g_cond_new();
125 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
128 static void image_loader_class_init(ImageLoaderClass *class)
130 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
132 // gobject_class->set_property = image_loader_set_property;
133 // gobject_class->get_property = image_loader_get_property;
135 gobject_class->finalize = image_loader_finalize;
138 signals[SIGNAL_AREA_READY] =
139 g_signal_new("area_ready",
140 G_OBJECT_CLASS_TYPE(gobject_class),
142 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
144 gq_marshal_VOID__INT_INT_INT_INT,
151 signals[SIGNAL_ERROR] =
152 g_signal_new("error",
153 G_OBJECT_CLASS_TYPE(gobject_class),
155 G_STRUCT_OFFSET(ImageLoaderClass, error),
157 g_cclosure_marshal_VOID__VOID,
160 signals[SIGNAL_DONE] =
162 G_OBJECT_CLASS_TYPE(gobject_class),
164 G_STRUCT_OFFSET(ImageLoaderClass, done),
166 g_cclosure_marshal_VOID__VOID,
169 signals[SIGNAL_PERCENT] =
170 g_signal_new("percent",
171 G_OBJECT_CLASS_TYPE(gobject_class),
173 G_STRUCT_OFFSET(ImageLoaderClass, percent),
175 g_cclosure_marshal_VOID__DOUBLE,
179 signals[SIGNAL_SIZE] =
180 g_signal_new("size_prepared",
181 G_OBJECT_CLASS_TYPE(gobject_class),
183 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
185 gq_marshal_VOID__INT_INT,
192 static void image_loader_finalize(GObject *object)
194 ImageLoader *il = (ImageLoader *)object;
196 image_loader_stop(il);
198 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
200 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
202 if (il->idle_done_id)
204 g_source_remove(il->idle_done_id);
205 il->idle_done_id = 0;
208 while (g_source_remove_by_user_data(il))
210 DEBUG_2("pending signals detected");
213 while (il->area_param_list)
215 DEBUG_1("pending area_ready signals detected");
216 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
217 g_free(il->area_param_list->data);
218 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
221 while (il->area_param_delayed_list)
223 g_free(il->area_param_delayed_list->data);
224 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
227 if (il->pixbuf) g_object_unref(il->pixbuf);
229 if (il->error) g_error_free(il->error);
231 file_data_unref(il->fd);
233 #if GLIB_CHECK_VERSION(2,32,0)
234 g_mutex_clear(il->data_mutex);
235 g_free(il->data_mutex);
236 g_cond_clear(il->can_destroy_cond);
237 g_free(il->can_destroy_cond);
239 g_mutex_free(il->data_mutex);
240 g_cond_free(il->can_destroy_cond);
245 void image_loader_free(ImageLoader *il)
248 g_object_unref(G_OBJECT(il));
252 ImageLoader *image_loader_new(FileData *fd)
256 if (!fd) return NULL;
258 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
260 il->fd = file_data_ref(fd);
265 /**************************************************************************************/
266 /* send signals via idle calbacks - the callback are executed in the main thread */
268 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
269 struct _ImageLoaderAreaParam {
278 static gboolean image_loader_emit_area_ready_cb(gpointer data)
280 ImageLoaderAreaParam *par = data;
281 ImageLoader *il = par->il;
283 g_mutex_lock(il->data_mutex);
284 il->area_param_list = g_list_remove(il->area_param_list, par);
290 g_mutex_unlock(il->data_mutex);
292 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
297 static gboolean image_loader_emit_done_cb(gpointer data)
299 ImageLoader *il = data;
300 g_signal_emit(il, signals[SIGNAL_DONE], 0);
304 static gboolean image_loader_emit_error_cb(gpointer data)
306 ImageLoader *il = data;
307 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
311 static gboolean image_loader_emit_percent_cb(gpointer data)
313 ImageLoader *il = data;
314 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
318 static gboolean image_loader_emit_size_cb(gpointer data)
321 ImageLoader *il = data;
322 g_mutex_lock(il->data_mutex);
323 width = il->actual_width;
324 height = il->actual_height;
325 g_mutex_unlock(il->data_mutex);
326 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
331 /* DONE and ERROR are emited only once, thus they can have normal priority
332 PERCENT and AREA_READY should be processed ASAP
335 static void image_loader_emit_done(ImageLoader *il)
337 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
340 static void image_loader_emit_error(ImageLoader *il)
342 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
345 static void image_loader_emit_percent(ImageLoader *il)
347 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
350 static void image_loader_emit_size(ImageLoader *il)
352 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
355 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
359 ImageLoaderAreaParam *prev_par = (*list)->data;
360 if (prev_par->x == x && prev_par->w == w &&
361 prev_par->y + prev_par->h == y)
363 /* we can merge the notifications */
367 if (prev_par->x == x && prev_par->w == w &&
368 y + h == prev_par->y)
370 /* we can merge the notifications */
375 if (prev_par->y == y && prev_par->h == h &&
376 prev_par->x + prev_par->w == x)
378 /* we can merge the notifications */
382 if (prev_par->y == y && prev_par->h == h &&
383 x + w == prev_par->x)
385 /* we can merge the notifications */
392 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
399 *list = g_list_prepend(*list, par);
403 /* this function expects that il->data_mutex is locked by caller */
404 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
406 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
410 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
414 /**************************************************************************************/
415 /* the following functions may be executed in separate thread */
417 /* this function expects that il->data_mutex is locked by caller */
418 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
420 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
425 static gboolean image_loader_get_stopping(ImageLoader *il)
428 if (!il) return FALSE;
430 g_mutex_lock(il->data_mutex);
432 g_mutex_unlock(il->data_mutex);
438 static void image_loader_sync_pixbuf(ImageLoader *il)
442 g_mutex_lock(il->data_mutex);
446 g_mutex_unlock(il->data_mutex);
450 pb = il->backend.get_pixbuf(il->loader);
452 if (pb == il->pixbuf)
454 g_mutex_unlock(il->data_mutex);
458 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
460 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
463 if (il->pixbuf) g_object_unref(il->pixbuf);
466 if (il->pixbuf) g_object_ref(il->pixbuf);
468 g_mutex_unlock(il->data_mutex);
471 static void image_loader_area_updated_cb(gpointer loader,
472 guint x, guint y, guint w, guint h,
475 ImageLoader *il = data;
477 if (!image_loader_get_pixbuf(il))
479 image_loader_sync_pixbuf(il);
480 if (!image_loader_get_pixbuf(il))
482 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
486 g_mutex_lock(il->data_mutex);
487 if (il->delay_area_ready)
488 image_loader_queue_delayed_area_ready(il, x, y, w, h);
490 image_loader_emit_area_ready(il, x, y, w, h);
492 if (il->stopping) il->backend.abort(il->loader);
494 g_mutex_unlock(il->data_mutex);
497 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
499 ImageLoader *il = data;
505 http://bugzilla.gnome.org/show_bug.cgi?id=547669
506 http://bugzilla.gnome.org/show_bug.cgi?id=589334
508 gchar *format = il->backend.get_format_name(loader);
509 if (strcmp(format, "svg") == 0 ||
510 strcmp(format, "xpm") == 0)
518 pb = il->backend.get_pixbuf(loader);
520 h = gdk_pixbuf_get_height(pb);
521 rs = gdk_pixbuf_get_rowstride(pb);
522 pix = gdk_pixbuf_get_pixels(pb);
524 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
528 static void image_loader_size_cb(gpointer loader,
529 gint width, gint height, gpointer data)
531 ImageLoader *il = data;
533 gboolean scale = FALSE;
536 g_mutex_lock(il->data_mutex);
537 il->actual_width = width;
538 il->actual_height = height;
539 if (il->requested_width < 1 || il->requested_height < 1)
541 g_mutex_unlock(il->data_mutex);
542 image_loader_emit_size(il);
545 g_mutex_unlock(il->data_mutex);
547 #ifdef HAVE_FFMPEGTHUMBNAILER
548 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
551 mime_types = il->backend.get_format_mime_types(loader);
553 while (mime_types[n] && !scale)
555 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
558 g_strfreev(mime_types);
562 image_loader_emit_size(il);
566 g_mutex_lock(il->data_mutex);
569 if (width > il->requested_width || height > il->requested_height)
572 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
574 nw = il->requested_width;
575 nh = (gdouble)nw / width * height;
580 nh = il->requested_height;
581 nw = (gdouble)nh / height * width;
585 il->actual_width = nw;
586 il->actual_height = nh;
587 il->backend.set_size(loader, nw, nh);
591 g_mutex_unlock(il->data_mutex);
592 image_loader_emit_size(il);
595 static void image_loader_stop_loader(ImageLoader *il)
601 /* some loaders do not have a pixbuf till close, order is important here */
602 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
603 image_loader_sync_pixbuf(il);
604 il->backend.free(il->loader);
607 g_mutex_lock(il->data_mutex);
609 g_mutex_unlock(il->data_mutex);
612 static void image_loader_setup_loader(ImageLoader *il)
614 g_mutex_lock(il->data_mutex);
615 #ifdef HAVE_FFMPEGTHUMBNAILER
616 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
618 DEBUG_1("Using custom ffmpegthumbnailer loader");
619 image_loader_backend_set_ft(&il->backend);
624 if (il->fd->format_class == FORMAT_CLASS_PDF)
626 DEBUG_1("Using custom pdf loader");
627 image_loader_backend_set_pdf(&il->backend);
632 if (il->bytes_total >= 10 &&
633 (memcmp(il->mapped_file + 4, "ftypheic", 8) == 0))
635 DEBUG_1("Using custom heif loader");
636 image_loader_backend_set_heif(&il->backend);
641 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
643 DEBUG_1("Using custom jpeg loader");
644 image_loader_backend_set_jpeg(&il->backend);
649 if (il->bytes_total >= 10 &&
650 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
651 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
652 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
653 memcmp(il->mapped_file, "II*\0", 4) == 0))
655 DEBUG_1("Using custom tiff loader");
656 image_loader_backend_set_tiff(&il->backend);
660 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
662 DEBUG_1("Using dds loader");
663 image_loader_backend_set_dds(&il->backend);
666 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
668 DEBUG_1("Using custom collection loader");
669 image_loader_backend_set_collection(&il->backend);
672 image_loader_backend_set_default(&il->backend);
674 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
675 g_mutex_unlock(il->data_mutex);
679 static void image_loader_done(ImageLoader *il)
681 image_loader_stop_loader(il);
683 image_loader_emit_done(il);
686 static void image_loader_error(ImageLoader *il)
688 image_loader_stop_loader(il);
690 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
692 image_loader_emit_error(il);
695 static gboolean image_loader_continue(ImageLoader *il)
700 if (!il) return FALSE;
702 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
703 while (c > 0 && !image_loader_get_stopping(il))
705 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
709 image_loader_done(il);
713 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
715 image_loader_error(il);
724 if (il->bytes_total > 0)
726 image_loader_emit_percent(il);
732 static gboolean image_loader_begin(ImageLoader *il)
736 if (il->pixbuf) return FALSE;
738 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
739 if (b < 1) return FALSE;
741 image_loader_setup_loader(il);
743 g_assert(il->bytes_read == 0);
744 if (il->backend.load) {
746 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
748 image_loader_stop_loader(il);
752 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
754 image_loader_stop_loader(il);
760 /* read until size is known */
761 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
763 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
764 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
766 image_loader_stop_loader(il);
771 if (!il->pixbuf) image_loader_sync_pixbuf(il);
773 if (il->bytes_read == il->bytes_total || b < 1)
775 /* done, handle (broken) loaders that do not have pixbuf till close */
776 image_loader_stop_loader(il);
778 if (!il->pixbuf) return FALSE;
780 image_loader_done(il);
786 image_loader_stop_loader(il);
793 /**************************************************************************************/
794 /* the following functions are always executed in the main thread */
797 static gboolean image_loader_setup_source(ImageLoader *il)
802 if (!il || il->loader || il->mapped_file) return FALSE;
804 il->mapped_file = NULL;
808 ExifData *exif = exif_read_fd(il->fd);
810 if (options->thumbnails.use_exif)
811 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
813 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
818 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
820 exif_free_fd(il->fd, exif);
824 if (!il->mapped_file)
829 pathl = path_from_utf8(il->fd->path);
830 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
832 if (load_fd == -1) return FALSE;
834 if (fstat(load_fd, &st) == 0)
836 il->bytes_total = st.st_size;
844 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
846 if (il->mapped_file == MAP_FAILED)
857 static void image_loader_stop_source(ImageLoader *il)
865 exif_free_preview(il->mapped_file);
869 munmap(il->mapped_file, il->bytes_total);
871 il->mapped_file = NULL;
875 static void image_loader_stop(ImageLoader *il)
881 g_source_remove(il->idle_id);
887 /* stop loader in the other thread */
888 g_mutex_lock(il->data_mutex);
890 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
891 g_mutex_unlock(il->data_mutex);
894 image_loader_stop_loader(il);
895 image_loader_stop_source(il);
899 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
901 g_mutex_lock(il->data_mutex);
902 il->delay_area_ready = enable;
907 list = g_list_reverse(il->area_param_delayed_list);
908 il->area_param_delayed_list = NULL;
909 g_mutex_unlock(il->data_mutex);
915 ImageLoaderAreaParam *par = work->data;
918 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
926 g_mutex_unlock(il->data_mutex);
931 /**************************************************************************************/
932 /* execution via idle calls */
934 static gboolean image_loader_idle_cb(gpointer data)
936 gboolean ret = FALSE;
937 ImageLoader *il = data;
941 ret = image_loader_continue(il);
946 image_loader_stop_source(il);
953 static gboolean image_loader_start_idle(ImageLoader *il)
957 if (!il) return FALSE;
959 if (!il->fd) return FALSE;
961 if (!image_loader_setup_source(il)) return FALSE;
963 ret = image_loader_begin(il);
965 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
969 /**************************************************************************************/
970 /* execution via thread */
973 static GThreadPool *image_loader_thread_pool = NULL;
975 static GCond *image_loader_prio_cond = NULL;
976 static GMutex *image_loader_prio_mutex = NULL;
977 static gint image_loader_prio_num = 0;
980 static void image_loader_thread_enter_high(void)
982 g_mutex_lock(image_loader_prio_mutex);
983 image_loader_prio_num++;
984 g_mutex_unlock(image_loader_prio_mutex);
987 static void image_loader_thread_leave_high(void)
989 g_mutex_lock(image_loader_prio_mutex);
990 image_loader_prio_num--;
991 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
992 g_mutex_unlock(image_loader_prio_mutex);
995 static void image_loader_thread_wait_high(void)
997 g_mutex_lock(image_loader_prio_mutex);
998 while (image_loader_prio_num)
1000 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1003 g_mutex_unlock(image_loader_prio_mutex);
1007 static void image_loader_thread_run(gpointer data, gpointer user_data)
1009 ImageLoader *il = data;
1013 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1015 /* low prio, wait untill high prio tasks finishes */
1016 image_loader_thread_wait_high();
1021 image_loader_thread_enter_high();
1024 err = !image_loader_begin(il);
1029 loader failed, we have to send signal
1030 (idle mode returns the image_loader_begin return value directly)
1031 (success is always reported indirectly from image_loader_begin)
1033 image_loader_emit_error(il);
1038 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1040 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1042 /* low prio, wait untill high prio tasks finishes */
1043 image_loader_thread_wait_high();
1045 cont = image_loader_continue(il);
1047 image_loader_stop_loader(il);
1049 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1052 image_loader_thread_leave_high();
1055 g_mutex_lock(il->data_mutex);
1056 il->can_destroy = TRUE;
1057 g_cond_signal(il->can_destroy_cond);
1058 g_mutex_unlock(il->data_mutex);
1063 static gboolean image_loader_start_thread(ImageLoader *il)
1065 if (!il) return FALSE;
1067 if (!il->fd) return FALSE;
1071 if (!image_loader_setup_source(il)) return FALSE;
1073 if (!image_loader_thread_pool)
1075 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1076 #if GLIB_CHECK_VERSION(2,32,0)
1077 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1078 g_cond_init(image_loader_prio_cond);
1079 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1080 g_mutex_init(image_loader_prio_mutex);
1082 image_loader_prio_cond = g_cond_new();
1083 image_loader_prio_mutex = g_mutex_new();
1087 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1089 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1090 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1094 #endif /* HAVE_GTHREAD */
1097 /**************************************************************************************/
1098 /* public interface */
1101 gboolean image_loader_start(ImageLoader *il)
1103 if (!il) return FALSE;
1105 if (!il->fd) return FALSE;
1108 return image_loader_start_thread(il);
1110 return image_loader_start_idle(il);
1115 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1116 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1119 if (!il) return NULL;
1121 g_mutex_lock(il->data_mutex);
1123 g_mutex_unlock(il->data_mutex);
1127 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1131 g_mutex_lock(il->data_mutex);
1132 il->requested_width = width;
1133 il->requested_height = height;
1134 g_mutex_unlock(il->data_mutex);
1137 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1141 g_mutex_lock(il->data_mutex);
1142 il->idle_read_loop_count = count ? count : 1;
1143 g_mutex_unlock(il->data_mutex);
1146 void image_loader_set_priority(ImageLoader *il, gint priority)
1150 if (il->thread) return; /* can't change prio if the thread already runs */
1151 il->idle_priority = priority;
1155 gdouble image_loader_get_percent(ImageLoader *il)
1158 if (!il) return 0.0;
1160 g_mutex_lock(il->data_mutex);
1161 if (il->bytes_total == 0)
1167 ret = (gdouble)il->bytes_read / il->bytes_total;
1169 g_mutex_unlock(il->data_mutex);
1173 gboolean image_loader_get_is_done(ImageLoader *il)
1176 if (!il) return FALSE;
1178 g_mutex_lock(il->data_mutex);
1180 g_mutex_unlock(il->data_mutex);
1185 FileData *image_loader_get_fd(ImageLoader *il)
1188 if (!il) return NULL;
1190 g_mutex_lock(il->data_mutex);
1192 g_mutex_unlock(il->data_mutex);
1197 gboolean image_loader_get_shrunk(ImageLoader *il)
1200 if (!il) return FALSE;
1202 g_mutex_lock(il->data_mutex);
1204 g_mutex_unlock(il->data_mutex);
1208 const gchar *image_loader_get_error(ImageLoader *il)
1210 const gchar *ret = NULL;
1211 if (!il) return NULL;
1212 g_mutex_lock(il->data_mutex);
1213 if (il->error) ret = il->error->message;
1214 g_mutex_unlock(il->data_mutex);
1219 /* FIXME - this can be rather slow and blocks until the size is known */
1220 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1225 il = image_loader_new(fd);
1227 success = image_loader_start_idle(il);
1229 if (success && il->pixbuf)
1231 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1232 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1236 if (width) *width = -1;
1237 if (height) *height = -1;
1240 image_loader_free(il);
1244 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */