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"
36 #include "image_load_svgz.h"
40 #include "ui_fileops.h"
41 #include "gq-marshal.h"
46 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
47 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
50 /**************************************************************************************/
51 /* image loader class */
55 SIGNAL_AREA_READY = 0,
63 static guint signals[SIGNAL_COUNT] = { 0 };
65 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
66 static void image_loader_class_init(ImageLoaderClass *class);
67 static void image_loader_finalize(GObject *object);
68 static void image_loader_stop(ImageLoader *il);
70 GType image_loader_get_type(void)
72 static GType type = 0;
75 static const GTypeInfo info = {
76 sizeof(ImageLoaderClass),
78 NULL, /* base_finalize */
79 (GClassInitFunc)image_loader_class_init, /* class_init */
80 NULL, /* class_finalize */
81 NULL, /* class_data */
84 (GInstanceInitFunc)image_loader_init, /* instance_init */
85 NULL /* value_table */
87 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
92 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
94 ImageLoader *il = (ImageLoader *)instance;
98 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
105 il->idle_done_id = 0;
107 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
108 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
109 il->mapped_file = NULL;
111 il->requested_width = 0;
112 il->requested_height = 0;
113 il->actual_width = 0;
114 il->actual_height = 0;
117 il->can_destroy = TRUE;
120 #if GLIB_CHECK_VERSION(2,32,0)
121 il->data_mutex = g_new(GMutex, 1);
122 g_mutex_init(il->data_mutex);
123 il->can_destroy_cond = g_new(GCond, 1);
124 g_cond_init(il->can_destroy_cond);
126 il->data_mutex = g_mutex_new();
127 il->can_destroy_cond = g_cond_new();
130 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
133 static void image_loader_class_init(ImageLoaderClass *class)
135 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
137 // gobject_class->set_property = image_loader_set_property;
138 // gobject_class->get_property = image_loader_get_property;
140 gobject_class->finalize = image_loader_finalize;
143 signals[SIGNAL_AREA_READY] =
144 g_signal_new("area_ready",
145 G_OBJECT_CLASS_TYPE(gobject_class),
147 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
149 gq_marshal_VOID__INT_INT_INT_INT,
156 signals[SIGNAL_ERROR] =
157 g_signal_new("error",
158 G_OBJECT_CLASS_TYPE(gobject_class),
160 G_STRUCT_OFFSET(ImageLoaderClass, error),
162 g_cclosure_marshal_VOID__VOID,
165 signals[SIGNAL_DONE] =
167 G_OBJECT_CLASS_TYPE(gobject_class),
169 G_STRUCT_OFFSET(ImageLoaderClass, done),
171 g_cclosure_marshal_VOID__VOID,
174 signals[SIGNAL_PERCENT] =
175 g_signal_new("percent",
176 G_OBJECT_CLASS_TYPE(gobject_class),
178 G_STRUCT_OFFSET(ImageLoaderClass, percent),
180 g_cclosure_marshal_VOID__DOUBLE,
184 signals[SIGNAL_SIZE] =
185 g_signal_new("size_prepared",
186 G_OBJECT_CLASS_TYPE(gobject_class),
188 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
190 gq_marshal_VOID__INT_INT,
197 static void image_loader_finalize(GObject *object)
199 ImageLoader *il = (ImageLoader *)object;
201 image_loader_stop(il);
203 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
205 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
207 if (il->idle_done_id)
209 g_source_remove(il->idle_done_id);
210 il->idle_done_id = 0;
213 while (g_source_remove_by_user_data(il))
215 DEBUG_2("pending signals detected");
218 while (il->area_param_list)
220 DEBUG_1("pending area_ready signals detected");
221 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
222 g_free(il->area_param_list->data);
223 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
226 while (il->area_param_delayed_list)
228 g_free(il->area_param_delayed_list->data);
229 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
232 if (il->pixbuf) g_object_unref(il->pixbuf);
234 if (il->error) g_error_free(il->error);
236 file_data_unref(il->fd);
238 #if GLIB_CHECK_VERSION(2,32,0)
239 g_mutex_clear(il->data_mutex);
240 g_free(il->data_mutex);
241 g_cond_clear(il->can_destroy_cond);
242 g_free(il->can_destroy_cond);
244 g_mutex_free(il->data_mutex);
245 g_cond_free(il->can_destroy_cond);
250 void image_loader_free(ImageLoader *il)
253 g_object_unref(G_OBJECT(il));
257 ImageLoader *image_loader_new(FileData *fd)
261 if (!fd) return NULL;
263 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
265 il->fd = file_data_ref(fd);
270 /**************************************************************************************/
271 /* send signals via idle calbacks - the callback are executed in the main thread */
273 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
274 struct _ImageLoaderAreaParam {
283 static gboolean image_loader_emit_area_ready_cb(gpointer data)
285 ImageLoaderAreaParam *par = data;
286 ImageLoader *il = par->il;
288 g_mutex_lock(il->data_mutex);
289 il->area_param_list = g_list_remove(il->area_param_list, par);
295 g_mutex_unlock(il->data_mutex);
297 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
302 static gboolean image_loader_emit_done_cb(gpointer data)
304 ImageLoader *il = data;
305 g_signal_emit(il, signals[SIGNAL_DONE], 0);
309 static gboolean image_loader_emit_error_cb(gpointer data)
311 ImageLoader *il = data;
312 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
316 static gboolean image_loader_emit_percent_cb(gpointer data)
318 ImageLoader *il = data;
319 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
323 static gboolean image_loader_emit_size_cb(gpointer data)
326 ImageLoader *il = data;
327 g_mutex_lock(il->data_mutex);
328 width = il->actual_width;
329 height = il->actual_height;
330 g_mutex_unlock(il->data_mutex);
331 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
336 /* DONE and ERROR are emited only once, thus they can have normal priority
337 PERCENT and AREA_READY should be processed ASAP
340 static void image_loader_emit_done(ImageLoader *il)
342 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
345 static void image_loader_emit_error(ImageLoader *il)
347 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
350 static void image_loader_emit_percent(ImageLoader *il)
352 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
355 static void image_loader_emit_size(ImageLoader *il)
357 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
360 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
364 ImageLoaderAreaParam *prev_par = (*list)->data;
365 if (prev_par->x == x && prev_par->w == w &&
366 prev_par->y + prev_par->h == y)
368 /* we can merge the notifications */
372 if (prev_par->x == x && prev_par->w == w &&
373 y + h == prev_par->y)
375 /* we can merge the notifications */
380 if (prev_par->y == y && prev_par->h == h &&
381 prev_par->x + prev_par->w == x)
383 /* we can merge the notifications */
387 if (prev_par->y == y && prev_par->h == h &&
388 x + w == prev_par->x)
390 /* we can merge the notifications */
397 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
404 *list = g_list_prepend(*list, par);
408 /* this function expects that il->data_mutex is locked by caller */
409 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
411 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
415 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
419 /**************************************************************************************/
420 /* the following functions may be executed in separate thread */
422 /* this function expects that il->data_mutex is locked by caller */
423 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
425 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
430 static gboolean image_loader_get_stopping(ImageLoader *il)
433 if (!il) return FALSE;
435 g_mutex_lock(il->data_mutex);
437 g_mutex_unlock(il->data_mutex);
443 static void image_loader_sync_pixbuf(ImageLoader *il)
447 g_mutex_lock(il->data_mutex);
451 g_mutex_unlock(il->data_mutex);
455 pb = il->backend.get_pixbuf(il->loader);
457 if (pb == il->pixbuf)
459 g_mutex_unlock(il->data_mutex);
463 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
465 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
468 if (il->pixbuf) g_object_unref(il->pixbuf);
471 if (il->pixbuf) g_object_ref(il->pixbuf);
473 g_mutex_unlock(il->data_mutex);
476 static void image_loader_area_updated_cb(gpointer loader,
477 guint x, guint y, guint w, guint h,
480 ImageLoader *il = data;
482 if (!image_loader_get_pixbuf(il))
484 image_loader_sync_pixbuf(il);
485 if (!image_loader_get_pixbuf(il))
487 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
491 g_mutex_lock(il->data_mutex);
492 if (il->delay_area_ready)
493 image_loader_queue_delayed_area_ready(il, x, y, w, h);
495 image_loader_emit_area_ready(il, x, y, w, h);
497 if (il->stopping) il->backend.abort(il->loader);
499 g_mutex_unlock(il->data_mutex);
502 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
504 ImageLoader *il = data;
510 http://bugzilla.gnome.org/show_bug.cgi?id=547669
511 http://bugzilla.gnome.org/show_bug.cgi?id=589334
513 gchar *format = il->backend.get_format_name(loader);
514 if (strcmp(format, "svg") == 0 ||
515 strcmp(format, "xpm") == 0)
523 pb = il->backend.get_pixbuf(loader);
525 h = gdk_pixbuf_get_height(pb);
526 rs = gdk_pixbuf_get_rowstride(pb);
527 pix = gdk_pixbuf_get_pixels(pb);
529 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
533 static void image_loader_size_cb(gpointer loader,
534 gint width, gint height, gpointer data)
536 ImageLoader *il = data;
538 gboolean scale = FALSE;
541 g_mutex_lock(il->data_mutex);
542 il->actual_width = width;
543 il->actual_height = height;
544 if (il->requested_width < 1 || il->requested_height < 1)
546 g_mutex_unlock(il->data_mutex);
547 image_loader_emit_size(il);
550 g_mutex_unlock(il->data_mutex);
552 #ifdef HAVE_FFMPEGTHUMBNAILER
553 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
556 mime_types = il->backend.get_format_mime_types(loader);
558 while (mime_types[n] && !scale)
560 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
563 g_strfreev(mime_types);
567 image_loader_emit_size(il);
571 g_mutex_lock(il->data_mutex);
574 if (width > il->requested_width || height > il->requested_height)
577 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
579 nw = il->requested_width;
580 nh = (gdouble)nw / width * height;
585 nh = il->requested_height;
586 nw = (gdouble)nh / height * width;
590 il->actual_width = nw;
591 il->actual_height = nh;
592 il->backend.set_size(loader, nw, nh);
596 g_mutex_unlock(il->data_mutex);
597 image_loader_emit_size(il);
600 static void image_loader_stop_loader(ImageLoader *il)
606 /* some loaders do not have a pixbuf till close, order is important here */
607 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
608 image_loader_sync_pixbuf(il);
609 il->backend.free(il->loader);
612 g_mutex_lock(il->data_mutex);
614 g_mutex_unlock(il->data_mutex);
617 static void image_loader_setup_loader(ImageLoader *il)
621 g_mutex_lock(il->data_mutex);
622 #ifdef HAVE_FFMPEGTHUMBNAILER
623 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
625 DEBUG_1("Using custom ffmpegthumbnailer loader");
626 image_loader_backend_set_ft(&il->backend);
631 if (il->bytes_total >= 4 &&
632 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
634 DEBUG_1("Using custom pdf loader");
635 image_loader_backend_set_pdf(&il->backend);
640 if (il->bytes_total >= 12 &&
641 ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
642 (memcmp(il->mapped_file + 4, "ftypmsf1", 8) == 0) ||
643 (memcmp(il->mapped_file + 4, "ftypmif1", 8) == 0)))
645 DEBUG_1("Using custom heif loader");
646 image_loader_backend_set_heif(&il->backend);
651 if (il->bytes_total >= 12 &&
652 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
653 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
655 DEBUG_1("Using custom webp loader");
656 image_loader_backend_set_webp(&il->backend);
661 if (il->bytes_total >= 16 &&
662 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
663 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
665 DEBUG_1("Using custom djvu loader");
666 image_loader_backend_set_djvu(&il->backend);
671 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
673 DEBUG_1("Using custom jpeg loader");
674 image_loader_backend_set_jpeg(&il->backend);
679 if (il->bytes_total >= 10 &&
680 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
681 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
682 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
683 memcmp(il->mapped_file, "II*\0", 4) == 0))
685 DEBUG_1("Using custom tiff loader");
686 image_loader_backend_set_tiff(&il->backend);
690 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
692 DEBUG_1("Using dds loader");
693 image_loader_backend_set_dds(&il->backend);
696 if (il->bytes_total >= 6 &&
697 (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
699 DEBUG_1("Using custom psd loader");
700 image_loader_backend_set_psd(&il->backend);
704 if (il->bytes_total >= 12 &&
705 (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
707 DEBUG_1("Using custom j2k loader");
708 image_loader_backend_set_j2k(&il->backend);
712 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
714 DEBUG_1("Using custom collection loader");
715 image_loader_backend_set_collection(&il->backend);
718 if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
720 DEBUG_1("Using custom svgz loader");
721 image_loader_backend_set_svgz(&il->backend);
724 image_loader_backend_set_default(&il->backend);
726 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
729 format = il->backend.get_format_name(il->loader);
730 if (g_strcmp0(format, "tiff") == 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, "pdf") == 0)
741 il->backend.set_page_num(il->loader, il->fd->page_num);
747 format = il->backend.get_format_name(il->loader);
748 if (g_strcmp0(format, "heif") == 0)
750 il->backend.set_page_num(il->loader, il->fd->page_num);
756 format = il->backend.get_format_name(il->loader);
757 if (g_strcmp0(format, "djvu") == 0)
759 il->backend.set_page_num(il->loader, il->fd->page_num);
764 g_mutex_unlock(il->data_mutex);
768 static void image_loader_done(ImageLoader *il)
770 image_loader_stop_loader(il);
772 image_loader_emit_done(il);
775 static void image_loader_error(ImageLoader *il)
777 image_loader_stop_loader(il);
779 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
781 image_loader_emit_error(il);
784 static gboolean image_loader_continue(ImageLoader *il)
789 if (!il) return FALSE;
791 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
792 while (c > 0 && !image_loader_get_stopping(il))
794 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
798 image_loader_done(il);
802 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
804 image_loader_error(il);
813 if (il->bytes_total > 0)
815 image_loader_emit_percent(il);
821 static gboolean image_loader_begin(ImageLoader *il)
826 if (il->pixbuf) return FALSE;
828 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
829 if (b < 1) return FALSE;
831 image_loader_setup_loader(il);
833 g_assert(il->bytes_read == 0);
834 if (il->backend.load) {
836 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
838 image_loader_stop_loader(il);
842 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
844 image_loader_stop_loader(il);
849 format = il->backend.get_format_name(il->loader);
850 if (g_strcmp0(format, "pdf") == 0)
852 gint i = il->backend.get_page_total(il->loader);
853 file_data_set_page_total(il->fd, i);
858 format = il->backend.get_format_name(il->loader);
859 if (g_strcmp0(format, "heif") == 0)
861 gint i = il->backend.get_page_total(il->loader);
862 file_data_set_page_total(il->fd, i);
867 format = il->backend.get_format_name(il->loader);
868 if (g_strcmp0(format, "djvu") == 0)
870 gint i = il->backend.get_page_total(il->loader);
871 file_data_set_page_total(il->fd, i);
876 format = il->backend.get_format_name(il->loader);
877 if (g_strcmp0(format, "tiff") == 0)
879 gint i = il->backend.get_page_total(il->loader);
880 file_data_set_page_total(il->fd, i);
887 /* read until size is known */
888 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
890 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
891 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
893 image_loader_stop_loader(il);
898 if (!il->pixbuf) image_loader_sync_pixbuf(il);
900 if (il->bytes_read == il->bytes_total || b < 1)
902 /* done, handle (broken) loaders that do not have pixbuf till close */
903 image_loader_stop_loader(il);
905 if (!il->pixbuf) return FALSE;
907 image_loader_done(il);
913 image_loader_stop_loader(il);
920 /**************************************************************************************/
921 /* the following functions are always executed in the main thread */
924 static gboolean image_loader_setup_source(ImageLoader *il)
929 if (!il || il->loader || il->mapped_file) return FALSE;
931 il->mapped_file = NULL;
935 ExifData *exif = exif_read_fd(il->fd);
937 if (options->thumbnails.use_exif)
938 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
940 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
945 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
947 exif_free_fd(il->fd, exif);
951 if (!il->mapped_file)
956 pathl = path_from_utf8(il->fd->path);
957 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
959 if (load_fd == -1) return FALSE;
961 if (fstat(load_fd, &st) == 0)
963 il->bytes_total = st.st_size;
971 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
973 if (il->mapped_file == MAP_FAILED)
984 static void image_loader_stop_source(ImageLoader *il)
992 exif_free_preview(il->mapped_file);
996 munmap(il->mapped_file, il->bytes_total);
998 il->mapped_file = NULL;
1002 static void image_loader_stop(ImageLoader *il)
1008 g_source_remove(il->idle_id);
1014 /* stop loader in the other thread */
1015 g_mutex_lock(il->data_mutex);
1016 il->stopping = TRUE;
1017 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
1018 g_mutex_unlock(il->data_mutex);
1021 image_loader_stop_loader(il);
1022 image_loader_stop_source(il);
1026 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
1028 g_mutex_lock(il->data_mutex);
1029 il->delay_area_ready = enable;
1034 list = g_list_reverse(il->area_param_delayed_list);
1035 il->area_param_delayed_list = NULL;
1036 g_mutex_unlock(il->data_mutex);
1042 ImageLoaderAreaParam *par = work->data;
1045 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1053 g_mutex_unlock(il->data_mutex);
1058 /**************************************************************************************/
1059 /* execution via idle calls */
1061 static gboolean image_loader_idle_cb(gpointer data)
1063 gboolean ret = FALSE;
1064 ImageLoader *il = data;
1068 ret = image_loader_continue(il);
1073 image_loader_stop_source(il);
1080 static gboolean image_loader_start_idle(ImageLoader *il)
1084 if (!il) return FALSE;
1086 if (!il->fd) return FALSE;
1088 if (!image_loader_setup_source(il)) return FALSE;
1090 ret = image_loader_begin(il);
1092 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
1096 /**************************************************************************************/
1097 /* execution via thread */
1100 static GThreadPool *image_loader_thread_pool = NULL;
1102 static GCond *image_loader_prio_cond = NULL;
1103 static GMutex *image_loader_prio_mutex = NULL;
1104 static gint image_loader_prio_num = 0;
1107 static void image_loader_thread_enter_high(void)
1109 g_mutex_lock(image_loader_prio_mutex);
1110 image_loader_prio_num++;
1111 g_mutex_unlock(image_loader_prio_mutex);
1114 static void image_loader_thread_leave_high(void)
1116 g_mutex_lock(image_loader_prio_mutex);
1117 image_loader_prio_num--;
1118 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1119 g_mutex_unlock(image_loader_prio_mutex);
1122 static void image_loader_thread_wait_high(void)
1124 g_mutex_lock(image_loader_prio_mutex);
1125 while (image_loader_prio_num)
1127 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1130 g_mutex_unlock(image_loader_prio_mutex);
1134 static void image_loader_thread_run(gpointer data, gpointer user_data)
1136 ImageLoader *il = data;
1140 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1142 /* low prio, wait untill high prio tasks finishes */
1143 image_loader_thread_wait_high();
1148 image_loader_thread_enter_high();
1151 err = !image_loader_begin(il);
1156 loader failed, we have to send signal
1157 (idle mode returns the image_loader_begin return value directly)
1158 (success is always reported indirectly from image_loader_begin)
1160 image_loader_emit_error(il);
1165 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1167 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1169 /* low prio, wait untill high prio tasks finishes */
1170 image_loader_thread_wait_high();
1172 cont = image_loader_continue(il);
1174 image_loader_stop_loader(il);
1176 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1179 image_loader_thread_leave_high();
1182 g_mutex_lock(il->data_mutex);
1183 il->can_destroy = TRUE;
1184 g_cond_signal(il->can_destroy_cond);
1185 g_mutex_unlock(il->data_mutex);
1190 static gboolean image_loader_start_thread(ImageLoader *il)
1192 if (!il) return FALSE;
1194 if (!il->fd) return FALSE;
1198 if (!image_loader_setup_source(il)) return FALSE;
1200 if (!image_loader_thread_pool)
1202 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1203 #if GLIB_CHECK_VERSION(2,32,0)
1204 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1205 g_cond_init(image_loader_prio_cond);
1206 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1207 g_mutex_init(image_loader_prio_mutex);
1209 image_loader_prio_cond = g_cond_new();
1210 image_loader_prio_mutex = g_mutex_new();
1214 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1216 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1217 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1221 #endif /* HAVE_GTHREAD */
1224 /**************************************************************************************/
1225 /* public interface */
1228 gboolean image_loader_start(ImageLoader *il)
1230 if (!il) return FALSE;
1232 if (!il->fd) return FALSE;
1235 return image_loader_start_thread(il);
1237 return image_loader_start_idle(il);
1242 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1243 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1246 if (!il) return NULL;
1248 g_mutex_lock(il->data_mutex);
1250 g_mutex_unlock(il->data_mutex);
1254 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1258 g_mutex_lock(il->data_mutex);
1259 il->requested_width = width;
1260 il->requested_height = height;
1261 g_mutex_unlock(il->data_mutex);
1264 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1268 g_mutex_lock(il->data_mutex);
1269 il->idle_read_loop_count = count ? count : 1;
1270 g_mutex_unlock(il->data_mutex);
1273 void image_loader_set_priority(ImageLoader *il, gint priority)
1277 if (il->thread) return; /* can't change prio if the thread already runs */
1278 il->idle_priority = priority;
1282 gdouble image_loader_get_percent(ImageLoader *il)
1285 if (!il) return 0.0;
1287 g_mutex_lock(il->data_mutex);
1288 if (il->bytes_total == 0)
1294 ret = (gdouble)il->bytes_read / il->bytes_total;
1296 g_mutex_unlock(il->data_mutex);
1300 gboolean image_loader_get_is_done(ImageLoader *il)
1303 if (!il) return FALSE;
1305 g_mutex_lock(il->data_mutex);
1307 g_mutex_unlock(il->data_mutex);
1312 FileData *image_loader_get_fd(ImageLoader *il)
1315 if (!il) return NULL;
1317 g_mutex_lock(il->data_mutex);
1319 g_mutex_unlock(il->data_mutex);
1324 gboolean image_loader_get_shrunk(ImageLoader *il)
1327 if (!il) return FALSE;
1329 g_mutex_lock(il->data_mutex);
1331 g_mutex_unlock(il->data_mutex);
1335 const gchar *image_loader_get_error(ImageLoader *il)
1337 const gchar *ret = NULL;
1338 if (!il) return NULL;
1339 g_mutex_lock(il->data_mutex);
1340 if (il->error) ret = il->error->message;
1341 g_mutex_unlock(il->data_mutex);
1346 /* FIXME - this can be rather slow and blocks until the size is known */
1347 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1352 il = image_loader_new(fd);
1354 success = image_loader_start_idle(il);
1356 if (success && il->pixbuf)
1358 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1359 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1363 if (width) *width = -1;
1364 if (height) *height = -1;
1367 image_loader_free(il);
1371 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */