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_djvu.h"
29 #include "image_load_pdf.h"
30 #include "image_load_psd.h"
31 #include "image_load_heif.h"
32 #include "image_load_ffmpegthumbnailer.h"
33 #include "image_load_collection.h"
34 #include "image_load_webp.h"
35 #include "image_load_j2k.h"
39 #include "ui_fileops.h"
40 #include "gq-marshal.h"
45 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
46 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
49 /**************************************************************************************/
50 /* image loader class */
54 SIGNAL_AREA_READY = 0,
62 static guint signals[SIGNAL_COUNT] = { 0 };
64 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
65 static void image_loader_class_init(ImageLoaderClass *class);
66 static void image_loader_finalize(GObject *object);
67 static void image_loader_stop(ImageLoader *il);
69 GType image_loader_get_type(void)
71 static GType type = 0;
74 static const GTypeInfo info = {
75 sizeof(ImageLoaderClass),
77 NULL, /* base_finalize */
78 (GClassInitFunc)image_loader_class_init, /* class_init */
79 NULL, /* class_finalize */
80 NULL, /* class_data */
83 (GInstanceInitFunc)image_loader_init, /* instance_init */
84 NULL /* value_table */
86 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
91 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
93 ImageLoader *il = (ImageLoader *)instance;
97 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
104 il->idle_done_id = 0;
106 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
107 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
108 il->mapped_file = NULL;
110 il->requested_width = 0;
111 il->requested_height = 0;
112 il->actual_width = 0;
113 il->actual_height = 0;
116 il->can_destroy = TRUE;
119 #if GLIB_CHECK_VERSION(2,32,0)
120 il->data_mutex = g_new(GMutex, 1);
121 g_mutex_init(il->data_mutex);
122 il->can_destroy_cond = g_new(GCond, 1);
123 g_cond_init(il->can_destroy_cond);
125 il->data_mutex = g_mutex_new();
126 il->can_destroy_cond = g_cond_new();
129 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
132 static void image_loader_class_init(ImageLoaderClass *class)
134 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
136 // gobject_class->set_property = image_loader_set_property;
137 // gobject_class->get_property = image_loader_get_property;
139 gobject_class->finalize = image_loader_finalize;
142 signals[SIGNAL_AREA_READY] =
143 g_signal_new("area_ready",
144 G_OBJECT_CLASS_TYPE(gobject_class),
146 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
148 gq_marshal_VOID__INT_INT_INT_INT,
155 signals[SIGNAL_ERROR] =
156 g_signal_new("error",
157 G_OBJECT_CLASS_TYPE(gobject_class),
159 G_STRUCT_OFFSET(ImageLoaderClass, error),
161 g_cclosure_marshal_VOID__VOID,
164 signals[SIGNAL_DONE] =
166 G_OBJECT_CLASS_TYPE(gobject_class),
168 G_STRUCT_OFFSET(ImageLoaderClass, done),
170 g_cclosure_marshal_VOID__VOID,
173 signals[SIGNAL_PERCENT] =
174 g_signal_new("percent",
175 G_OBJECT_CLASS_TYPE(gobject_class),
177 G_STRUCT_OFFSET(ImageLoaderClass, percent),
179 g_cclosure_marshal_VOID__DOUBLE,
183 signals[SIGNAL_SIZE] =
184 g_signal_new("size_prepared",
185 G_OBJECT_CLASS_TYPE(gobject_class),
187 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
189 gq_marshal_VOID__INT_INT,
196 static void image_loader_finalize(GObject *object)
198 ImageLoader *il = (ImageLoader *)object;
200 image_loader_stop(il);
202 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
204 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
206 if (il->idle_done_id)
208 g_source_remove(il->idle_done_id);
209 il->idle_done_id = 0;
212 while (g_source_remove_by_user_data(il))
214 DEBUG_2("pending signals detected");
217 while (il->area_param_list)
219 DEBUG_1("pending area_ready signals detected");
220 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
221 g_free(il->area_param_list->data);
222 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
225 while (il->area_param_delayed_list)
227 g_free(il->area_param_delayed_list->data);
228 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
231 if (il->pixbuf) g_object_unref(il->pixbuf);
233 if (il->error) g_error_free(il->error);
235 file_data_unref(il->fd);
237 #if GLIB_CHECK_VERSION(2,32,0)
238 g_mutex_clear(il->data_mutex);
239 g_free(il->data_mutex);
240 g_cond_clear(il->can_destroy_cond);
241 g_free(il->can_destroy_cond);
243 g_mutex_free(il->data_mutex);
244 g_cond_free(il->can_destroy_cond);
249 void image_loader_free(ImageLoader *il)
252 g_object_unref(G_OBJECT(il));
256 ImageLoader *image_loader_new(FileData *fd)
260 if (!fd) return NULL;
262 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
264 il->fd = file_data_ref(fd);
269 /**************************************************************************************/
270 /* send signals via idle calbacks - the callback are executed in the main thread */
272 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
273 struct _ImageLoaderAreaParam {
282 static gboolean image_loader_emit_area_ready_cb(gpointer data)
284 ImageLoaderAreaParam *par = data;
285 ImageLoader *il = par->il;
287 g_mutex_lock(il->data_mutex);
288 il->area_param_list = g_list_remove(il->area_param_list, par);
294 g_mutex_unlock(il->data_mutex);
296 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
301 static gboolean image_loader_emit_done_cb(gpointer data)
303 ImageLoader *il = data;
304 g_signal_emit(il, signals[SIGNAL_DONE], 0);
308 static gboolean image_loader_emit_error_cb(gpointer data)
310 ImageLoader *il = data;
311 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
315 static gboolean image_loader_emit_percent_cb(gpointer data)
317 ImageLoader *il = data;
318 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
322 static gboolean image_loader_emit_size_cb(gpointer data)
325 ImageLoader *il = data;
326 g_mutex_lock(il->data_mutex);
327 width = il->actual_width;
328 height = il->actual_height;
329 g_mutex_unlock(il->data_mutex);
330 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
335 /* DONE and ERROR are emited only once, thus they can have normal priority
336 PERCENT and AREA_READY should be processed ASAP
339 static void image_loader_emit_done(ImageLoader *il)
341 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
344 static void image_loader_emit_error(ImageLoader *il)
346 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
349 static void image_loader_emit_percent(ImageLoader *il)
351 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
354 static void image_loader_emit_size(ImageLoader *il)
356 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
359 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
363 ImageLoaderAreaParam *prev_par = (*list)->data;
364 if (prev_par->x == x && prev_par->w == w &&
365 prev_par->y + prev_par->h == y)
367 /* we can merge the notifications */
371 if (prev_par->x == x && prev_par->w == w &&
372 y + h == prev_par->y)
374 /* we can merge the notifications */
379 if (prev_par->y == y && prev_par->h == h &&
380 prev_par->x + prev_par->w == x)
382 /* we can merge the notifications */
386 if (prev_par->y == y && prev_par->h == h &&
387 x + w == prev_par->x)
389 /* we can merge the notifications */
396 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
403 *list = g_list_prepend(*list, par);
407 /* this function expects that il->data_mutex is locked by caller */
408 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
410 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
414 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
418 /**************************************************************************************/
419 /* the following functions may be executed in separate thread */
421 /* this function expects that il->data_mutex is locked by caller */
422 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
424 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
429 static gboolean image_loader_get_stopping(ImageLoader *il)
432 if (!il) return FALSE;
434 g_mutex_lock(il->data_mutex);
436 g_mutex_unlock(il->data_mutex);
442 static void image_loader_sync_pixbuf(ImageLoader *il)
446 g_mutex_lock(il->data_mutex);
450 g_mutex_unlock(il->data_mutex);
454 pb = il->backend.get_pixbuf(il->loader);
456 if (pb == il->pixbuf)
458 g_mutex_unlock(il->data_mutex);
462 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
464 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
467 if (il->pixbuf) g_object_unref(il->pixbuf);
470 if (il->pixbuf) g_object_ref(il->pixbuf);
472 g_mutex_unlock(il->data_mutex);
475 static void image_loader_area_updated_cb(gpointer loader,
476 guint x, guint y, guint w, guint h,
479 ImageLoader *il = data;
481 if (!image_loader_get_pixbuf(il))
483 image_loader_sync_pixbuf(il);
484 if (!image_loader_get_pixbuf(il))
486 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
490 g_mutex_lock(il->data_mutex);
491 if (il->delay_area_ready)
492 image_loader_queue_delayed_area_ready(il, x, y, w, h);
494 image_loader_emit_area_ready(il, x, y, w, h);
496 if (il->stopping) il->backend.abort(il->loader);
498 g_mutex_unlock(il->data_mutex);
501 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
503 ImageLoader *il = data;
509 http://bugzilla.gnome.org/show_bug.cgi?id=547669
510 http://bugzilla.gnome.org/show_bug.cgi?id=589334
512 gchar *format = il->backend.get_format_name(loader);
513 if (strcmp(format, "svg") == 0 ||
514 strcmp(format, "xpm") == 0)
522 pb = il->backend.get_pixbuf(loader);
524 h = gdk_pixbuf_get_height(pb);
525 rs = gdk_pixbuf_get_rowstride(pb);
526 pix = gdk_pixbuf_get_pixels(pb);
528 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
532 static void image_loader_size_cb(gpointer loader,
533 gint width, gint height, gpointer data)
535 ImageLoader *il = data;
537 gboolean scale = FALSE;
540 g_mutex_lock(il->data_mutex);
541 il->actual_width = width;
542 il->actual_height = height;
543 if (il->requested_width < 1 || il->requested_height < 1)
545 g_mutex_unlock(il->data_mutex);
546 image_loader_emit_size(il);
549 g_mutex_unlock(il->data_mutex);
551 #ifdef HAVE_FFMPEGTHUMBNAILER
552 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
555 mime_types = il->backend.get_format_mime_types(loader);
557 while (mime_types[n] && !scale)
559 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
562 g_strfreev(mime_types);
566 image_loader_emit_size(il);
570 g_mutex_lock(il->data_mutex);
573 if (width > il->requested_width || height > il->requested_height)
576 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
578 nw = il->requested_width;
579 nh = (gdouble)nw / width * height;
584 nh = il->requested_height;
585 nw = (gdouble)nh / height * width;
589 il->actual_width = nw;
590 il->actual_height = nh;
591 il->backend.set_size(loader, nw, nh);
595 g_mutex_unlock(il->data_mutex);
596 image_loader_emit_size(il);
599 static void image_loader_stop_loader(ImageLoader *il)
605 /* some loaders do not have a pixbuf till close, order is important here */
606 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
607 image_loader_sync_pixbuf(il);
608 il->backend.free(il->loader);
611 g_mutex_lock(il->data_mutex);
613 g_mutex_unlock(il->data_mutex);
616 static void image_loader_setup_loader(ImageLoader *il)
620 g_mutex_lock(il->data_mutex);
621 #ifdef HAVE_FFMPEGTHUMBNAILER
622 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
624 DEBUG_1("Using custom ffmpegthumbnailer loader");
625 image_loader_backend_set_ft(&il->backend);
630 if (il->bytes_total >= 4 &&
631 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
633 DEBUG_1("Using custom pdf loader");
634 image_loader_backend_set_pdf(&il->backend);
639 if (il->bytes_total >= 10 &&
640 (memcmp(il->mapped_file + 4, "ftypheic", 8) == 0))
642 DEBUG_1("Using custom heif loader");
643 image_loader_backend_set_heif(&il->backend);
648 if (il->bytes_total >= 12 &&
649 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
650 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
652 DEBUG_1("Using custom webp loader");
653 image_loader_backend_set_webp(&il->backend);
658 if (il->bytes_total >= 16 &&
659 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
660 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
662 DEBUG_1("Using custom djvu loader");
663 image_loader_backend_set_djvu(&il->backend);
668 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
670 DEBUG_1("Using custom jpeg loader");
671 image_loader_backend_set_jpeg(&il->backend);
676 if (il->bytes_total >= 10 &&
677 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
678 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
679 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
680 memcmp(il->mapped_file, "II*\0", 4) == 0))
682 DEBUG_1("Using custom tiff loader");
683 image_loader_backend_set_tiff(&il->backend);
687 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
689 DEBUG_1("Using dds loader");
690 image_loader_backend_set_dds(&il->backend);
693 if (il->bytes_total >= 6 &&
694 (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
696 DEBUG_1("Using custom psd loader");
697 image_loader_backend_set_psd(&il->backend);
701 if (il->bytes_total >= 12 &&
702 (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
704 DEBUG_1("Using custom j2k loader");
705 image_loader_backend_set_j2k(&il->backend);
709 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
711 DEBUG_1("Using custom collection loader");
712 image_loader_backend_set_collection(&il->backend);
715 image_loader_backend_set_default(&il->backend);
717 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
720 format = il->backend.get_format_name(il->loader);
721 if (g_strcmp0(format, "tiff") == 0)
723 il->backend.set_page_num(il->loader, il->fd->page_num);
729 format = il->backend.get_format_name(il->loader);
730 if (g_strcmp0(format, "pdf") == 0)
732 il->backend.set_page_num(il->loader, il->fd->page_num);
738 format = il->backend.get_format_name(il->loader);
739 if (g_strcmp0(format, "djvu") == 0)
741 il->backend.set_page_num(il->loader, il->fd->page_num);
746 g_mutex_unlock(il->data_mutex);
750 static void image_loader_done(ImageLoader *il)
752 image_loader_stop_loader(il);
754 image_loader_emit_done(il);
757 static void image_loader_error(ImageLoader *il)
759 image_loader_stop_loader(il);
761 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
763 image_loader_emit_error(il);
766 static gboolean image_loader_continue(ImageLoader *il)
771 if (!il) return FALSE;
773 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
774 while (c > 0 && !image_loader_get_stopping(il))
776 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
780 image_loader_done(il);
784 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
786 image_loader_error(il);
795 if (il->bytes_total > 0)
797 image_loader_emit_percent(il);
803 static gboolean image_loader_begin(ImageLoader *il)
808 if (il->pixbuf) return FALSE;
810 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
811 if (b < 1) return FALSE;
813 image_loader_setup_loader(il);
815 g_assert(il->bytes_read == 0);
816 if (il->backend.load) {
818 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
820 image_loader_stop_loader(il);
824 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
826 image_loader_stop_loader(il);
831 format = il->backend.get_format_name(il->loader);
832 if (g_strcmp0(format, "pdf") == 0)
834 gint i = il->backend.get_page_total(il->loader);
835 file_data_set_page_total(il->fd, i);
840 format = il->backend.get_format_name(il->loader);
841 if (g_strcmp0(format, "djvu") == 0)
843 gint i = il->backend.get_page_total(il->loader);
844 file_data_set_page_total(il->fd, i);
849 format = il->backend.get_format_name(il->loader);
850 if (g_strcmp0(format, "tiff") == 0)
852 gint i = il->backend.get_page_total(il->loader);
853 file_data_set_page_total(il->fd, i);
860 /* read until size is known */
861 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
863 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
864 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
866 image_loader_stop_loader(il);
871 if (!il->pixbuf) image_loader_sync_pixbuf(il);
873 if (il->bytes_read == il->bytes_total || b < 1)
875 /* done, handle (broken) loaders that do not have pixbuf till close */
876 image_loader_stop_loader(il);
878 if (!il->pixbuf) return FALSE;
880 image_loader_done(il);
886 image_loader_stop_loader(il);
893 /**************************************************************************************/
894 /* the following functions are always executed in the main thread */
897 static gboolean image_loader_setup_source(ImageLoader *il)
902 if (!il || il->loader || il->mapped_file) return FALSE;
904 il->mapped_file = NULL;
908 ExifData *exif = exif_read_fd(il->fd);
910 if (options->thumbnails.use_exif)
911 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
913 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
918 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
920 exif_free_fd(il->fd, exif);
924 if (!il->mapped_file)
929 pathl = path_from_utf8(il->fd->path);
930 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
932 if (load_fd == -1) return FALSE;
934 if (fstat(load_fd, &st) == 0)
936 il->bytes_total = st.st_size;
944 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
946 if (il->mapped_file == MAP_FAILED)
957 static void image_loader_stop_source(ImageLoader *il)
965 exif_free_preview(il->mapped_file);
969 munmap(il->mapped_file, il->bytes_total);
971 il->mapped_file = NULL;
975 static void image_loader_stop(ImageLoader *il)
981 g_source_remove(il->idle_id);
987 /* stop loader in the other thread */
988 g_mutex_lock(il->data_mutex);
990 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
991 g_mutex_unlock(il->data_mutex);
994 image_loader_stop_loader(il);
995 image_loader_stop_source(il);
999 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
1001 g_mutex_lock(il->data_mutex);
1002 il->delay_area_ready = enable;
1007 list = g_list_reverse(il->area_param_delayed_list);
1008 il->area_param_delayed_list = NULL;
1009 g_mutex_unlock(il->data_mutex);
1015 ImageLoaderAreaParam *par = work->data;
1018 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1026 g_mutex_unlock(il->data_mutex);
1031 /**************************************************************************************/
1032 /* execution via idle calls */
1034 static gboolean image_loader_idle_cb(gpointer data)
1036 gboolean ret = FALSE;
1037 ImageLoader *il = data;
1041 ret = image_loader_continue(il);
1046 image_loader_stop_source(il);
1053 static gboolean image_loader_start_idle(ImageLoader *il)
1057 if (!il) return FALSE;
1059 if (!il->fd) return FALSE;
1061 if (!image_loader_setup_source(il)) return FALSE;
1063 ret = image_loader_begin(il);
1065 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
1069 /**************************************************************************************/
1070 /* execution via thread */
1073 static GThreadPool *image_loader_thread_pool = NULL;
1075 static GCond *image_loader_prio_cond = NULL;
1076 static GMutex *image_loader_prio_mutex = NULL;
1077 static gint image_loader_prio_num = 0;
1080 static void image_loader_thread_enter_high(void)
1082 g_mutex_lock(image_loader_prio_mutex);
1083 image_loader_prio_num++;
1084 g_mutex_unlock(image_loader_prio_mutex);
1087 static void image_loader_thread_leave_high(void)
1089 g_mutex_lock(image_loader_prio_mutex);
1090 image_loader_prio_num--;
1091 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1092 g_mutex_unlock(image_loader_prio_mutex);
1095 static void image_loader_thread_wait_high(void)
1097 g_mutex_lock(image_loader_prio_mutex);
1098 while (image_loader_prio_num)
1100 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1103 g_mutex_unlock(image_loader_prio_mutex);
1107 static void image_loader_thread_run(gpointer data, gpointer user_data)
1109 ImageLoader *il = data;
1113 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1115 /* low prio, wait untill high prio tasks finishes */
1116 image_loader_thread_wait_high();
1121 image_loader_thread_enter_high();
1124 err = !image_loader_begin(il);
1129 loader failed, we have to send signal
1130 (idle mode returns the image_loader_begin return value directly)
1131 (success is always reported indirectly from image_loader_begin)
1133 image_loader_emit_error(il);
1138 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1140 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1142 /* low prio, wait untill high prio tasks finishes */
1143 image_loader_thread_wait_high();
1145 cont = image_loader_continue(il);
1147 image_loader_stop_loader(il);
1149 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1152 image_loader_thread_leave_high();
1155 g_mutex_lock(il->data_mutex);
1156 il->can_destroy = TRUE;
1157 g_cond_signal(il->can_destroy_cond);
1158 g_mutex_unlock(il->data_mutex);
1163 static gboolean image_loader_start_thread(ImageLoader *il)
1165 if (!il) return FALSE;
1167 if (!il->fd) return FALSE;
1171 if (!image_loader_setup_source(il)) return FALSE;
1173 if (!image_loader_thread_pool)
1175 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1176 #if GLIB_CHECK_VERSION(2,32,0)
1177 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1178 g_cond_init(image_loader_prio_cond);
1179 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1180 g_mutex_init(image_loader_prio_mutex);
1182 image_loader_prio_cond = g_cond_new();
1183 image_loader_prio_mutex = g_mutex_new();
1187 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1189 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1190 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1194 #endif /* HAVE_GTHREAD */
1197 /**************************************************************************************/
1198 /* public interface */
1201 gboolean image_loader_start(ImageLoader *il)
1203 if (!il) return FALSE;
1205 if (!il->fd) return FALSE;
1208 return image_loader_start_thread(il);
1210 return image_loader_start_idle(il);
1215 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1216 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1219 if (!il) return NULL;
1221 g_mutex_lock(il->data_mutex);
1223 g_mutex_unlock(il->data_mutex);
1227 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1231 g_mutex_lock(il->data_mutex);
1232 il->requested_width = width;
1233 il->requested_height = height;
1234 g_mutex_unlock(il->data_mutex);
1237 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1241 g_mutex_lock(il->data_mutex);
1242 il->idle_read_loop_count = count ? count : 1;
1243 g_mutex_unlock(il->data_mutex);
1246 void image_loader_set_priority(ImageLoader *il, gint priority)
1250 if (il->thread) return; /* can't change prio if the thread already runs */
1251 il->idle_priority = priority;
1255 gdouble image_loader_get_percent(ImageLoader *il)
1258 if (!il) return 0.0;
1260 g_mutex_lock(il->data_mutex);
1261 if (il->bytes_total == 0)
1267 ret = (gdouble)il->bytes_read / il->bytes_total;
1269 g_mutex_unlock(il->data_mutex);
1273 gboolean image_loader_get_is_done(ImageLoader *il)
1276 if (!il) return FALSE;
1278 g_mutex_lock(il->data_mutex);
1280 g_mutex_unlock(il->data_mutex);
1285 FileData *image_loader_get_fd(ImageLoader *il)
1288 if (!il) return NULL;
1290 g_mutex_lock(il->data_mutex);
1292 g_mutex_unlock(il->data_mutex);
1297 gboolean image_loader_get_shrunk(ImageLoader *il)
1300 if (!il) return FALSE;
1302 g_mutex_lock(il->data_mutex);
1304 g_mutex_unlock(il->data_mutex);
1308 const gchar *image_loader_get_error(ImageLoader *il)
1310 const gchar *ret = NULL;
1311 if (!il) return NULL;
1312 g_mutex_lock(il->data_mutex);
1313 if (il->error) ret = il->error->message;
1314 g_mutex_unlock(il->data_mutex);
1319 /* FIXME - this can be rather slow and blocks until the size is known */
1320 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1325 il = image_loader_new(fd);
1327 success = image_loader_start_idle(il);
1329 if (success && il->pixbuf)
1331 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1332 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1336 if (width) *width = -1;
1337 if (height) *height = -1;
1340 image_loader_free(il);
1344 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */