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_ffmpegthumbnailer.h"
29 #include "image_load_collection.h"
33 #include "ui_fileops.h"
34 #include "gq-marshal.h"
39 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
40 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
43 /**************************************************************************************/
44 /* image loader class */
48 SIGNAL_AREA_READY = 0,
56 static guint signals[SIGNAL_COUNT] = { 0 };
58 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
59 static void image_loader_class_init(ImageLoaderClass *class);
60 static void image_loader_finalize(GObject *object);
61 static void image_loader_stop(ImageLoader *il);
63 GType image_loader_get_type(void)
65 static GType type = 0;
68 static const GTypeInfo info = {
69 sizeof(ImageLoaderClass),
71 NULL, /* base_finalize */
72 (GClassInitFunc)image_loader_class_init, /* class_init */
73 NULL, /* class_finalize */
74 NULL, /* class_data */
77 (GInstanceInitFunc)image_loader_init, /* instance_init */
78 NULL /* value_table */
80 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
85 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
87 ImageLoader *il = (ImageLoader *)instance;
91 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
100 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
101 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
102 il->mapped_file = NULL;
104 il->requested_width = 0;
105 il->requested_height = 0;
106 il->actual_width = 0;
107 il->actual_height = 0;
110 il->can_destroy = TRUE;
113 #if GLIB_CHECK_VERSION(2,32,0)
114 il->data_mutex = g_new(GMutex, 1);
115 g_mutex_init(il->data_mutex);
116 il->can_destroy_cond = g_new(GCond, 1);
117 g_cond_init(il->can_destroy_cond);
119 il->data_mutex = g_mutex_new();
120 il->can_destroy_cond = g_cond_new();
123 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
126 static void image_loader_class_init(ImageLoaderClass *class)
128 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
130 // gobject_class->set_property = image_loader_set_property;
131 // gobject_class->get_property = image_loader_get_property;
133 gobject_class->finalize = image_loader_finalize;
136 signals[SIGNAL_AREA_READY] =
137 g_signal_new("area_ready",
138 G_OBJECT_CLASS_TYPE(gobject_class),
140 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
142 gq_marshal_VOID__INT_INT_INT_INT,
149 signals[SIGNAL_ERROR] =
150 g_signal_new("error",
151 G_OBJECT_CLASS_TYPE(gobject_class),
153 G_STRUCT_OFFSET(ImageLoaderClass, error),
155 g_cclosure_marshal_VOID__VOID,
158 signals[SIGNAL_DONE] =
160 G_OBJECT_CLASS_TYPE(gobject_class),
162 G_STRUCT_OFFSET(ImageLoaderClass, done),
164 g_cclosure_marshal_VOID__VOID,
167 signals[SIGNAL_PERCENT] =
168 g_signal_new("percent",
169 G_OBJECT_CLASS_TYPE(gobject_class),
171 G_STRUCT_OFFSET(ImageLoaderClass, percent),
173 g_cclosure_marshal_VOID__DOUBLE,
177 signals[SIGNAL_SIZE] =
178 g_signal_new("size_prepared",
179 G_OBJECT_CLASS_TYPE(gobject_class),
181 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
183 gq_marshal_VOID__INT_INT,
190 static void image_loader_finalize(GObject *object)
192 ImageLoader *il = (ImageLoader *)object;
194 image_loader_stop(il);
196 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
198 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
200 if (il->idle_done_id)
202 g_source_remove(il->idle_done_id);
203 il->idle_done_id = 0;
206 while (g_source_remove_by_user_data(il))
208 DEBUG_2("pending signals detected");
211 while (il->area_param_list)
213 DEBUG_1("pending area_ready signals detected");
214 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
215 g_free(il->area_param_list->data);
216 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
219 while (il->area_param_delayed_list)
221 g_free(il->area_param_delayed_list->data);
222 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
225 if (il->pixbuf) g_object_unref(il->pixbuf);
227 if (il->error) g_error_free(il->error);
229 file_data_unref(il->fd);
231 #if GLIB_CHECK_VERSION(2,32,0)
232 g_mutex_clear(il->data_mutex);
233 g_free(il->data_mutex);
234 g_cond_clear(il->can_destroy_cond);
235 g_free(il->can_destroy_cond);
237 g_mutex_free(il->data_mutex);
238 g_cond_free(il->can_destroy_cond);
243 void image_loader_free(ImageLoader *il)
246 g_object_unref(G_OBJECT(il));
250 ImageLoader *image_loader_new(FileData *fd)
254 if (!fd) return NULL;
256 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
258 il->fd = file_data_ref(fd);
263 /**************************************************************************************/
264 /* send signals via idle calbacks - the callback are executed in the main thread */
266 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
267 struct _ImageLoaderAreaParam {
276 static gboolean image_loader_emit_area_ready_cb(gpointer data)
278 ImageLoaderAreaParam *par = data;
279 ImageLoader *il = par->il;
281 g_mutex_lock(il->data_mutex);
282 il->area_param_list = g_list_remove(il->area_param_list, par);
288 g_mutex_unlock(il->data_mutex);
290 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
295 static gboolean image_loader_emit_done_cb(gpointer data)
297 ImageLoader *il = data;
298 g_signal_emit(il, signals[SIGNAL_DONE], 0);
302 static gboolean image_loader_emit_error_cb(gpointer data)
304 ImageLoader *il = data;
305 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
309 static gboolean image_loader_emit_percent_cb(gpointer data)
311 ImageLoader *il = data;
312 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
316 static gboolean image_loader_emit_size_cb(gpointer data)
319 ImageLoader *il = data;
320 g_mutex_lock(il->data_mutex);
321 width = il->actual_width;
322 height = il->actual_height;
323 g_mutex_unlock(il->data_mutex);
324 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
329 /* DONE and ERROR are emited only once, thus they can have normal priority
330 PERCENT and AREA_READY should be processed ASAP
333 static void image_loader_emit_done(ImageLoader *il)
335 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
338 static void image_loader_emit_error(ImageLoader *il)
340 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
343 static void image_loader_emit_percent(ImageLoader *il)
345 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
348 static void image_loader_emit_size(ImageLoader *il)
350 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
353 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
357 ImageLoaderAreaParam *prev_par = (*list)->data;
358 if (prev_par->x == x && prev_par->w == w &&
359 prev_par->y + prev_par->h == y)
361 /* we can merge the notifications */
365 if (prev_par->x == x && prev_par->w == w &&
366 y + h == prev_par->y)
368 /* we can merge the notifications */
373 if (prev_par->y == y && prev_par->h == h &&
374 prev_par->x + prev_par->w == x)
376 /* we can merge the notifications */
380 if (prev_par->y == y && prev_par->h == h &&
381 x + w == prev_par->x)
383 /* we can merge the notifications */
390 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
397 *list = g_list_prepend(*list, par);
401 /* this function expects that il->data_mutex is locked by caller */
402 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
404 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
408 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
412 /**************************************************************************************/
413 /* the following functions may be executed in separate thread */
415 /* this function expects that il->data_mutex is locked by caller */
416 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
418 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
423 static gboolean image_loader_get_stopping(ImageLoader *il)
426 if (!il) return FALSE;
428 g_mutex_lock(il->data_mutex);
430 g_mutex_unlock(il->data_mutex);
436 static void image_loader_sync_pixbuf(ImageLoader *il)
440 g_mutex_lock(il->data_mutex);
444 g_mutex_unlock(il->data_mutex);
448 pb = il->backend.get_pixbuf(il->loader);
450 if (pb == il->pixbuf)
452 g_mutex_unlock(il->data_mutex);
456 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
458 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
461 if (il->pixbuf) g_object_unref(il->pixbuf);
464 if (il->pixbuf) g_object_ref(il->pixbuf);
466 g_mutex_unlock(il->data_mutex);
469 static void image_loader_area_updated_cb(gpointer loader,
470 guint x, guint y, guint w, guint h,
473 ImageLoader *il = data;
475 if (!image_loader_get_pixbuf(il))
477 image_loader_sync_pixbuf(il);
478 if (!image_loader_get_pixbuf(il))
480 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
484 g_mutex_lock(il->data_mutex);
485 if (il->delay_area_ready)
486 image_loader_queue_delayed_area_ready(il, x, y, w, h);
488 image_loader_emit_area_ready(il, x, y, w, h);
490 if (il->stopping) il->backend.abort(il->loader);
492 g_mutex_unlock(il->data_mutex);
495 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
497 ImageLoader *il = data;
503 http://bugzilla.gnome.org/show_bug.cgi?id=547669
504 http://bugzilla.gnome.org/show_bug.cgi?id=589334
506 gchar *format = il->backend.get_format_name(loader);
507 if (strcmp(format, "svg") == 0 ||
508 strcmp(format, "xpm") == 0)
516 pb = il->backend.get_pixbuf(loader);
518 h = gdk_pixbuf_get_height(pb);
519 rs = gdk_pixbuf_get_rowstride(pb);
520 pix = gdk_pixbuf_get_pixels(pb);
522 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
526 static void image_loader_size_cb(gpointer loader,
527 gint width, gint height, gpointer data)
529 ImageLoader *il = data;
531 gboolean scale = FALSE;
534 g_mutex_lock(il->data_mutex);
535 il->actual_width = width;
536 il->actual_height = height;
537 if (il->requested_width < 1 || il->requested_height < 1)
539 g_mutex_unlock(il->data_mutex);
540 image_loader_emit_size(il);
543 g_mutex_unlock(il->data_mutex);
545 #ifdef HAVE_FFMPEGTHUMBNAILER
546 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
549 mime_types = il->backend.get_format_mime_types(loader);
551 while (mime_types[n] && !scale)
553 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
556 g_strfreev(mime_types);
560 image_loader_emit_size(il);
564 g_mutex_lock(il->data_mutex);
567 if (width > il->requested_width || height > il->requested_height)
570 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
572 nw = il->requested_width;
573 nh = (gdouble)nw / width * height;
578 nh = il->requested_height;
579 nw = (gdouble)nh / height * width;
583 il->actual_width = nw;
584 il->actual_height = nh;
585 il->backend.set_size(loader, nw, nh);
589 g_mutex_unlock(il->data_mutex);
590 image_loader_emit_size(il);
593 static void image_loader_stop_loader(ImageLoader *il)
599 /* some loaders do not have a pixbuf till close, order is important here */
600 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
601 image_loader_sync_pixbuf(il);
602 il->backend.free(il->loader);
605 g_mutex_lock(il->data_mutex);
607 g_mutex_unlock(il->data_mutex);
610 static void image_loader_setup_loader(ImageLoader *il)
612 g_mutex_lock(il->data_mutex);
613 #ifdef HAVE_FFMPEGTHUMBNAILER
614 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
616 DEBUG_1("Using custom ffmpegthumbnailer loader");
617 image_loader_backend_set_ft(&il->backend);
622 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
624 DEBUG_1("Using custom jpeg loader");
625 image_loader_backend_set_jpeg(&il->backend);
630 if (il->bytes_total >= 10 &&
631 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
632 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
633 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
634 memcmp(il->mapped_file, "II*\0", 4) == 0))
636 DEBUG_1("Using custom tiff loader");
637 image_loader_backend_set_tiff(&il->backend);
641 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
643 DEBUG_1("Using dds loader");
644 image_loader_backend_set_dds(&il->backend);
647 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
649 DEBUG_1("Using custom collection loader");
650 image_loader_backend_set_collection(&il->backend);
653 image_loader_backend_set_default(&il->backend);
655 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
656 g_mutex_unlock(il->data_mutex);
660 static void image_loader_done(ImageLoader *il)
662 image_loader_stop_loader(il);
664 image_loader_emit_done(il);
667 static void image_loader_error(ImageLoader *il)
669 image_loader_stop_loader(il);
671 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
673 image_loader_emit_error(il);
676 static gboolean image_loader_continue(ImageLoader *il)
681 if (!il) return FALSE;
683 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
684 while (c > 0 && !image_loader_get_stopping(il))
686 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
690 image_loader_done(il);
694 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
696 image_loader_error(il);
705 if (il->bytes_total > 0)
707 image_loader_emit_percent(il);
713 static gboolean image_loader_begin(ImageLoader *il)
717 if (il->pixbuf) return FALSE;
719 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
720 if (b < 1) return FALSE;
722 image_loader_setup_loader(il);
724 g_assert(il->bytes_read == 0);
725 if (il->backend.load) {
727 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
729 image_loader_stop_loader(il);
733 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
735 image_loader_stop_loader(il);
741 /* read until size is known */
742 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
744 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
745 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
747 image_loader_stop_loader(il);
752 if (!il->pixbuf) image_loader_sync_pixbuf(il);
754 if (il->bytes_read == il->bytes_total || b < 1)
756 /* done, handle (broken) loaders that do not have pixbuf till close */
757 image_loader_stop_loader(il);
759 if (!il->pixbuf) return FALSE;
761 image_loader_done(il);
767 image_loader_stop_loader(il);
774 /**************************************************************************************/
775 /* the following functions are always executed in the main thread */
778 static gboolean image_loader_setup_source(ImageLoader *il)
783 if (!il || il->loader || il->mapped_file) return FALSE;
785 il->mapped_file = NULL;
789 ExifData *exif = exif_read_fd(il->fd);
791 if (options->thumbnails.use_exif)
792 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
794 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
799 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
801 exif_free_fd(il->fd, exif);
805 if (!il->mapped_file)
810 pathl = path_from_utf8(il->fd->path);
811 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
813 if (load_fd == -1) return FALSE;
815 if (fstat(load_fd, &st) == 0)
817 il->bytes_total = st.st_size;
825 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
827 if (il->mapped_file == MAP_FAILED)
838 static void image_loader_stop_source(ImageLoader *il)
846 exif_free_preview(il->mapped_file);
850 munmap(il->mapped_file, il->bytes_total);
852 il->mapped_file = NULL;
856 static void image_loader_stop(ImageLoader *il)
862 g_source_remove(il->idle_id);
868 /* stop loader in the other thread */
869 g_mutex_lock(il->data_mutex);
871 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
872 g_mutex_unlock(il->data_mutex);
875 image_loader_stop_loader(il);
876 image_loader_stop_source(il);
880 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
882 g_mutex_lock(il->data_mutex);
883 il->delay_area_ready = enable;
888 list = g_list_reverse(il->area_param_delayed_list);
889 il->area_param_delayed_list = NULL;
890 g_mutex_unlock(il->data_mutex);
896 ImageLoaderAreaParam *par = work->data;
899 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
907 g_mutex_unlock(il->data_mutex);
912 /**************************************************************************************/
913 /* execution via idle calls */
915 static gboolean image_loader_idle_cb(gpointer data)
917 gboolean ret = FALSE;
918 ImageLoader *il = data;
922 ret = image_loader_continue(il);
927 image_loader_stop_source(il);
934 static gboolean image_loader_start_idle(ImageLoader *il)
938 if (!il) return FALSE;
940 if (!il->fd) return FALSE;
942 if (!image_loader_setup_source(il)) return FALSE;
944 ret = image_loader_begin(il);
946 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
950 /**************************************************************************************/
951 /* execution via thread */
954 static GThreadPool *image_loader_thread_pool = NULL;
956 static GCond *image_loader_prio_cond = NULL;
957 static GMutex *image_loader_prio_mutex = NULL;
958 static gint image_loader_prio_num = 0;
961 static void image_loader_thread_enter_high(void)
963 g_mutex_lock(image_loader_prio_mutex);
964 image_loader_prio_num++;
965 g_mutex_unlock(image_loader_prio_mutex);
968 static void image_loader_thread_leave_high(void)
970 g_mutex_lock(image_loader_prio_mutex);
971 image_loader_prio_num--;
972 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
973 g_mutex_unlock(image_loader_prio_mutex);
976 static void image_loader_thread_wait_high(void)
978 g_mutex_lock(image_loader_prio_mutex);
979 while (image_loader_prio_num)
981 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
984 g_mutex_unlock(image_loader_prio_mutex);
988 static void image_loader_thread_run(gpointer data, gpointer user_data)
990 ImageLoader *il = data;
994 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
996 /* low prio, wait untill high prio tasks finishes */
997 image_loader_thread_wait_high();
1002 image_loader_thread_enter_high();
1005 err = !image_loader_begin(il);
1010 loader failed, we have to send signal
1011 (idle mode returns the image_loader_begin return value directly)
1012 (success is always reported indirectly from image_loader_begin)
1014 image_loader_emit_error(il);
1019 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1021 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1023 /* low prio, wait untill high prio tasks finishes */
1024 image_loader_thread_wait_high();
1026 cont = image_loader_continue(il);
1028 image_loader_stop_loader(il);
1030 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1033 image_loader_thread_leave_high();
1036 g_mutex_lock(il->data_mutex);
1037 il->can_destroy = TRUE;
1038 g_cond_signal(il->can_destroy_cond);
1039 g_mutex_unlock(il->data_mutex);
1044 static gboolean image_loader_start_thread(ImageLoader *il)
1046 if (!il) return FALSE;
1048 if (!il->fd) return FALSE;
1052 if (!image_loader_setup_source(il)) return FALSE;
1054 if (!image_loader_thread_pool)
1056 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1057 #if GLIB_CHECK_VERSION(2,32,0)
1058 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1059 g_cond_init(image_loader_prio_cond);
1060 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1061 g_mutex_init(image_loader_prio_mutex);
1063 image_loader_prio_cond = g_cond_new();
1064 image_loader_prio_mutex = g_mutex_new();
1068 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1070 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1071 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1075 #endif /* HAVE_GTHREAD */
1078 /**************************************************************************************/
1079 /* public interface */
1082 gboolean image_loader_start(ImageLoader *il)
1084 if (!il) return FALSE;
1086 if (!il->fd) return FALSE;
1089 return image_loader_start_thread(il);
1091 return image_loader_start_idle(il);
1096 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1097 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1100 if (!il) return NULL;
1102 g_mutex_lock(il->data_mutex);
1104 g_mutex_unlock(il->data_mutex);
1108 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1112 g_mutex_lock(il->data_mutex);
1113 il->requested_width = width;
1114 il->requested_height = height;
1115 g_mutex_unlock(il->data_mutex);
1118 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1122 g_mutex_lock(il->data_mutex);
1123 il->idle_read_loop_count = count ? count : 1;
1124 g_mutex_unlock(il->data_mutex);
1127 void image_loader_set_priority(ImageLoader *il, gint priority)
1131 if (il->thread) return; /* can't change prio if the thread already runs */
1132 il->idle_priority = priority;
1136 gdouble image_loader_get_percent(ImageLoader *il)
1139 if (!il) return 0.0;
1141 g_mutex_lock(il->data_mutex);
1142 if (il->bytes_total == 0)
1148 ret = (gdouble)il->bytes_read / il->bytes_total;
1150 g_mutex_unlock(il->data_mutex);
1154 gboolean image_loader_get_is_done(ImageLoader *il)
1157 if (!il) return FALSE;
1159 g_mutex_lock(il->data_mutex);
1161 g_mutex_unlock(il->data_mutex);
1166 FileData *image_loader_get_fd(ImageLoader *il)
1169 if (!il) return NULL;
1171 g_mutex_lock(il->data_mutex);
1173 g_mutex_unlock(il->data_mutex);
1178 gboolean image_loader_get_shrunk(ImageLoader *il)
1181 if (!il) return FALSE;
1183 g_mutex_lock(il->data_mutex);
1185 g_mutex_unlock(il->data_mutex);
1189 const gchar *image_loader_get_error(ImageLoader *il)
1191 const gchar *ret = NULL;
1192 if (!il) return NULL;
1193 g_mutex_lock(il->data_mutex);
1194 if (il->error) ret = il->error->message;
1195 g_mutex_unlock(il->data_mutex);
1200 /* FIXME - this can be rather slow and blocks until the size is known */
1201 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1206 il = image_loader_new(fd);
1208 success = image_loader_start_idle(il);
1210 if (success && il->pixbuf)
1212 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1213 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1217 if (width) *width = -1;
1218 if (height) *height = -1;
1221 image_loader_free(il);
1225 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */