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"
27 #include "gq-marshal.h"
28 #include "image-load-collection.h"
29 #include "image-load-dds.h"
30 #include "image-load-external.h"
31 #include "image-load-gdk.h"
32 #include "image-load-libraw.h"
33 #include "image-load-psd.h"
34 #include "image-load-svgz.h"
35 #include "image-load-webp.h"
36 #include "image-load-zxscr.h"
38 #include "ui-fileops.h"
41 #include "image-load-djvu.h"
43 #ifdef HAVE_FFMPEGTHUMBNAILER
44 #include "image-load-ffmpegthumbnailer.h"
47 #include "image-load-heif.h"
50 #include "image-load-j2k.h"
53 #include "image-load-cr3.h"
54 #include "image-load-jpeg.h"
57 #include "image-load-jpegxl.h"
60 #include "image-load-pdf.h"
63 #include "image-load-tiff.h"
70 IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT = 4096,
71 IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT = 1
74 /* image loader class */
78 SIGNAL_AREA_READY = 0,
86 static guint signals[SIGNAL_COUNT] = { 0 };
88 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
89 static void image_loader_class_init_wrapper(void *data, void *user_data);
90 static void image_loader_class_init(ImageLoaderClass *loader_class);
91 static void image_loader_finalize(GObject *object);
92 static void image_loader_stop(ImageLoader *il);
94 GType image_loader_get_type()
96 static GType type = 0;
99 static const GTypeInfo info = {
100 sizeof(ImageLoaderClass),
101 nullptr, /* base_init */
102 nullptr, /* base_finalize */
103 static_cast<GClassInitFunc>(image_loader_class_init_wrapper), /* class_init */
104 nullptr, /* class_finalize */
105 nullptr, /* class_data */
108 static_cast<GInstanceInitFunc>(image_loader_init), /* instance_init */
109 nullptr /* value_table */
111 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, static_cast<GTypeFlags>(0));
116 static void image_loader_init(GTypeInstance *instance, gpointer)
118 auto il = reinterpret_cast<ImageLoader *>(instance);
120 il->pixbuf = nullptr;
122 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
124 il->loader = nullptr;
129 il->idle_done_id = 0;
131 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
132 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
133 il->mapped_file = nullptr;
134 il->preview = IMAGE_LOADER_PREVIEW_NONE;
136 il->requested_width = 0;
137 il->requested_height = 0;
138 il->actual_width = 0;
139 il->actual_height = 0;
142 il->can_destroy = TRUE;
144 il->data_mutex = g_new(GMutex, 1);
145 g_mutex_init(il->data_mutex);
146 il->can_destroy_cond = g_new(GCond, 1);
147 g_cond_init(il->can_destroy_cond);
149 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", (void *)il, il->read_buffer_size, il->idle_read_loop_count);
152 static void image_loader_class_init_wrapper(void *data, void *)
154 image_loader_class_init(static_cast<ImageLoaderClass *>(data));
157 static void image_loader_class_init(ImageLoaderClass *loader_class)
159 GObjectClass *gobject_class = G_OBJECT_CLASS (loader_class);
161 gobject_class->finalize = image_loader_finalize;
164 signals[SIGNAL_AREA_READY] =
165 g_signal_new("area_ready",
166 G_OBJECT_CLASS_TYPE(gobject_class),
168 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
170 gq_marshal_VOID__INT_INT_INT_INT,
177 signals[SIGNAL_ERROR] =
178 g_signal_new("error",
179 G_OBJECT_CLASS_TYPE(gobject_class),
181 G_STRUCT_OFFSET(ImageLoaderClass, error),
183 g_cclosure_marshal_VOID__VOID,
186 signals[SIGNAL_DONE] =
188 G_OBJECT_CLASS_TYPE(gobject_class),
190 G_STRUCT_OFFSET(ImageLoaderClass, done),
192 g_cclosure_marshal_VOID__VOID,
195 signals[SIGNAL_PERCENT] =
196 g_signal_new("percent",
197 G_OBJECT_CLASS_TYPE(gobject_class),
199 G_STRUCT_OFFSET(ImageLoaderClass, percent),
201 g_cclosure_marshal_VOID__DOUBLE,
205 signals[SIGNAL_SIZE] =
206 g_signal_new("size_prepared",
207 G_OBJECT_CLASS_TYPE(gobject_class),
209 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
211 gq_marshal_VOID__INT_INT,
218 static void image_loader_finalize(GObject *object)
220 auto il = reinterpret_cast<ImageLoader *>(object);
222 image_loader_stop(il);
224 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
226 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, (void *)il, il->bytes_read);
228 if (il->idle_done_id)
230 g_source_remove(il->idle_done_id);
231 il->idle_done_id = 0;
234 while (g_source_remove_by_user_data(il))
236 DEBUG_2("pending signals detected");
239 while (il->area_param_list)
241 DEBUG_1("pending area_ready signals detected");
242 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
243 g_free(il->area_param_list->data);
244 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
247 while (il->area_param_delayed_list)
249 g_free(il->area_param_delayed_list->data);
250 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
253 if (il->pixbuf) g_object_unref(il->pixbuf);
255 if (il->error) g_error_free(il->error);
257 file_data_unref(il->fd);
259 g_mutex_clear(il->data_mutex);
260 g_free(il->data_mutex);
261 g_cond_clear(il->can_destroy_cond);
262 g_free(il->can_destroy_cond);
265 void image_loader_free(ImageLoader *il)
268 g_object_unref(G_OBJECT(il));
272 ImageLoader *image_loader_new(FileData *fd)
276 if (!fd) return nullptr;
278 il = static_cast<ImageLoader *>(g_object_new(TYPE_IMAGE_LOADER, nullptr));
280 il->fd = file_data_ref(fd);
285 /**************************************************************************************/
286 /* send signals via idle calbacks - the callback are executed in the main thread */
288 struct ImageLoaderAreaParam {
297 static gboolean image_loader_emit_area_ready_cb(gpointer data)
299 auto par = static_cast<ImageLoaderAreaParam *>(data);
300 ImageLoader *il = par->il;
305 g_mutex_lock(il->data_mutex);
306 il->area_param_list = g_list_remove(il->area_param_list, par);
312 g_mutex_unlock(il->data_mutex);
314 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
316 return G_SOURCE_REMOVE;
319 static gboolean image_loader_emit_done_cb(gpointer data)
321 auto il = static_cast<ImageLoader *>(data);
322 g_signal_emit(il, signals[SIGNAL_DONE], 0);
323 return G_SOURCE_REMOVE;
326 static gboolean image_loader_emit_error_cb(gpointer data)
328 auto il = static_cast<ImageLoader *>(data);
329 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
330 return G_SOURCE_REMOVE;
333 static gboolean image_loader_emit_percent_cb(gpointer data)
335 auto il = static_cast<ImageLoader *>(data);
336 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
337 return G_SOURCE_REMOVE;
340 static gboolean image_loader_emit_size_cb(gpointer data)
344 auto il = static_cast<ImageLoader *>(data);
345 g_mutex_lock(il->data_mutex);
346 width = il->actual_width;
347 height = il->actual_height;
348 g_mutex_unlock(il->data_mutex);
349 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
350 return G_SOURCE_REMOVE;
354 /* DONE and ERROR are emitted only once, thus they can have normal priority
355 PERCENT and AREA_READY should be processed ASAP
358 static void image_loader_emit_done(ImageLoader *il)
360 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, nullptr);
363 static void image_loader_emit_error(ImageLoader *il)
365 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, nullptr);
368 static void image_loader_emit_percent(ImageLoader *il)
370 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, nullptr);
373 static void image_loader_emit_size(ImageLoader *il)
375 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, nullptr);
378 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
382 auto prev_par = static_cast<ImageLoaderAreaParam *>((*list)->data);
383 if (prev_par->x == x && prev_par->w == w &&
384 prev_par->y + prev_par->h == y)
386 /* we can merge the notifications */
390 if (prev_par->x == x && prev_par->w == w &&
391 y + h == prev_par->y)
393 /* we can merge the notifications */
398 if (prev_par->y == y && prev_par->h == h &&
399 prev_par->x + prev_par->w == x)
401 /* we can merge the notifications */
405 if (prev_par->y == y && prev_par->h == h &&
406 x + w == prev_par->x)
408 /* we can merge the notifications */
415 auto par = g_new0(ImageLoaderAreaParam, 1);
422 *list = g_list_prepend(*list, par);
426 /* this function expects that il->data_mutex is locked by caller */
427 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
429 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
433 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, nullptr);
437 /**************************************************************************************/
438 /* the following functions may be executed in separate thread */
440 /* this function expects that il->data_mutex is locked by caller */
441 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
443 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
448 static gboolean image_loader_get_stopping(ImageLoader *il)
451 if (!il) return FALSE;
453 g_mutex_lock(il->data_mutex);
455 g_mutex_unlock(il->data_mutex);
461 static void image_loader_sync_pixbuf(ImageLoader *il)
465 g_mutex_lock(il->data_mutex);
469 g_mutex_unlock(il->data_mutex);
473 pb = il->backend.get_pixbuf(il->loader);
475 if (pb == il->pixbuf)
477 g_mutex_unlock(il->data_mutex);
481 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
483 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
486 if (il->pixbuf) g_object_unref(il->pixbuf);
489 if (il->pixbuf) g_object_ref(il->pixbuf);
491 g_mutex_unlock(il->data_mutex);
494 static void image_loader_area_updated_cb(gpointer,
495 guint x, guint y, guint w, guint h,
498 auto il = static_cast<ImageLoader *>(data);
500 if (!image_loader_get_pixbuf(il))
502 image_loader_sync_pixbuf(il);
503 if (!image_loader_get_pixbuf(il))
505 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
509 g_mutex_lock(il->data_mutex);
510 if (il->delay_area_ready)
511 image_loader_queue_delayed_area_ready(il, x, y, w, h);
513 image_loader_emit_area_ready(il, x, y, w, h);
515 if (il->stopping) il->backend.abort(il->loader);
517 g_mutex_unlock(il->data_mutex);
520 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
522 auto il = static_cast<ImageLoader *>(data);
529 https://bugzilla.gnome.org/show_bug.cgi?id=547669
530 https://bugzilla.gnome.org/show_bug.cgi?id=589334
532 gchar *format = il->backend.get_format_name(loader);
533 if (strcmp(format, "svg") == 0 ||
534 strcmp(format, "xpm") == 0)
542 pb = il->backend.get_pixbuf(loader);
544 h = gdk_pixbuf_get_height(pb);
545 rs = gdk_pixbuf_get_rowstride(pb);
546 pix = gdk_pixbuf_get_pixels(pb);
548 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
552 static void image_loader_size_cb(gpointer loader,
553 gint width, gint height, gpointer data)
555 auto il = static_cast<ImageLoader *>(data);
557 gboolean scale = FALSE;
560 g_mutex_lock(il->data_mutex);
561 il->actual_width = width;
562 il->actual_height = height;
563 if (il->requested_width < 1 || il->requested_height < 1)
565 g_mutex_unlock(il->data_mutex);
566 image_loader_emit_size(il);
569 g_mutex_unlock(il->data_mutex);
571 #ifdef HAVE_FFMPEGTHUMBNAILER
572 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
575 mime_types = il->backend.get_format_mime_types(loader);
577 while (mime_types[n] && !scale)
579 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
582 g_strfreev(mime_types);
586 image_loader_emit_size(il);
590 g_mutex_lock(il->data_mutex);
594 if (width > il->requested_width || height > il->requested_height)
597 if ((static_cast<gdouble>(il->requested_width) / width) < (static_cast<gdouble>(il->requested_height) / height))
599 nw = il->requested_width;
600 nh = static_cast<gdouble>(nw) / width * height;
605 nh = il->requested_height;
606 nw = static_cast<gdouble>(nh) / height * width;
610 il->actual_width = nw;
611 il->actual_height = nh;
612 il->backend.set_size(loader, nw, nh);
616 g_mutex_unlock(il->data_mutex);
617 image_loader_emit_size(il);
620 static void image_loader_stop_loader(ImageLoader *il)
626 /* some loaders do not have a pixbuf till close, order is important here */
627 il->backend.close(il->loader, il->error ? nullptr : &il->error); /* we are interested in the first error only */
628 image_loader_sync_pixbuf(il);
629 il->backend.free(il->loader);
630 il->loader = nullptr;
632 g_mutex_lock(il->data_mutex);
634 g_mutex_unlock(il->data_mutex);
637 static void image_loader_setup_loader(ImageLoader *il)
639 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
643 gint external_preview = 1;
645 g_mutex_lock(il->data_mutex);
647 if (options->external_preview.enable)
650 gchar *tilde_filename;
652 tilde_filename = expand_tilde(options->external_preview.select);
654 cmd_line = g_strdup_printf("\"%s\" \"%s\"" , tilde_filename, il->fd->path);
656 external_preview = runcmd(cmd_line);
658 g_free(tilde_filename);
661 if (external_preview == 0)
663 DEBUG_1("Using custom external loader");
664 image_loader_backend_set_external(&il->backend);
668 #ifdef HAVE_FFMPEGTHUMBNAILER
669 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
671 DEBUG_1("Using custom ffmpegthumbnailer loader");
672 image_loader_backend_set_ft(&il->backend);
677 if (il->bytes_total >= 4 &&
678 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
680 DEBUG_1("Using custom pdf loader");
681 image_loader_backend_set_pdf(&il->backend);
686 if (il->bytes_total >= 12 &&
687 ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
688 (memcmp(il->mapped_file + 4, "ftypheix", 8) == 0) ||
689 (memcmp(il->mapped_file + 4, "ftypmsf1", 8) == 0) ||
690 (memcmp(il->mapped_file + 4, "ftypmif1", 8) == 0) ||
691 (memcmp(il->mapped_file + 4, "ftypavif", 8) == 0)))
693 DEBUG_1("Using custom heif loader");
694 image_loader_backend_set_heif(&il->backend);
699 if (il->bytes_total >= 12 &&
700 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
701 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
703 DEBUG_1("Using custom webp loader");
704 image_loader_backend_set_webp(&il->backend);
709 if (il->bytes_total >= 16 &&
710 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
711 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
713 DEBUG_1("Using custom djvu loader");
714 image_loader_backend_set_djvu(&il->backend);
719 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
721 DEBUG_1("Using custom jpeg loader");
722 image_loader_backend_set_jpeg(&il->backend);
726 if (il->bytes_total >= 11 &&
727 (memcmp(il->mapped_file + 4, "ftypcrx", 7) == 0) &&
728 (memcmp(il->mapped_file + 64, "CanonCR3", 8) == 0))
730 DEBUG_1("Using custom cr3 loader");
731 image_loader_backend_set_cr3(&il->backend);
737 if (il->bytes_total >= 10 &&
738 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
739 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
740 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
741 memcmp(il->mapped_file, "II*\0", 4) == 0))
743 DEBUG_1("Using custom tiff loader");
744 image_loader_backend_set_tiff(&il->backend);
748 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
750 DEBUG_1("Using dds loader");
751 image_loader_backend_set_dds(&il->backend);
754 if (il->bytes_total >= 6 &&
755 (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
757 DEBUG_1("Using custom psd loader");
758 image_loader_backend_set_psd(&il->backend);
762 if (il->bytes_total >= 12 &&
763 (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
765 DEBUG_1("Using custom j2k loader");
766 image_loader_backend_set_j2k(&il->backend);
771 if (il->bytes_total >= 12 &&
772 (memcmp(il->mapped_file, "\0\0\0\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A", 12) == 0))
774 DEBUG_1("Using custom jpeg xl loader");
775 image_loader_backend_set_jpegxl(&il->backend);
778 if (il->bytes_total >= 2 &&
779 (memcmp(il->mapped_file, "\xFF\x0A", 2) == 0))
781 DEBUG_1("Using custom jpeg xl loader");
782 image_loader_backend_set_jpegxl(&il->backend);
786 if ((il->bytes_total == 6144 || il->bytes_total == 6912) &&
787 (file_extension_match(il->fd->path, ".scr")))
789 DEBUG_1("Using custom zxscr loader");
790 image_loader_backend_set_zxscr(&il->backend);
793 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
795 DEBUG_1("Using custom collection loader");
796 image_loader_backend_set_collection(&il->backend);
799 if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
801 DEBUG_1("Using custom svgz loader");
802 image_loader_backend_set_svgz(&il->backend);
805 image_loader_backend_set_default(&il->backend);
808 il->loader = static_cast<void **>(il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il));
811 format = il->backend.get_format_name(il->loader);
812 if (g_strcmp0(format, "tiff") == 0)
814 il->backend.set_page_num(il->loader, il->fd->page_num);
820 format = il->backend.get_format_name(il->loader);
821 if (g_strcmp0(format, "pdf") == 0)
823 il->backend.set_page_num(il->loader, il->fd->page_num);
829 format = il->backend.get_format_name(il->loader);
830 if (g_strcmp0(format, "heif") == 0)
832 il->backend.set_page_num(il->loader, il->fd->page_num);
838 format = il->backend.get_format_name(il->loader);
839 if (g_strcmp0(format, "djvu") == 0)
841 il->backend.set_page_num(il->loader, il->fd->page_num);
846 il->fd->format_name = il->backend.get_format_name(il->loader);
848 g_mutex_unlock(il->data_mutex);
852 static void image_loader_done(ImageLoader *il)
854 image_loader_stop_loader(il);
856 image_loader_emit_done(il);
859 static void image_loader_error(ImageLoader *il)
861 image_loader_stop_loader(il);
863 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
865 image_loader_emit_error(il);
868 static gboolean image_loader_continue(ImageLoader *il)
873 if (!il) return G_SOURCE_REMOVE;
875 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
876 while (c > 0 && !image_loader_get_stopping(il))
878 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
882 image_loader_done(il);
883 return G_SOURCE_REMOVE;
886 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
888 image_loader_error(il);
889 return G_SOURCE_REMOVE;
897 if (il->bytes_total > 0)
899 image_loader_emit_percent(il);
902 return G_SOURCE_CONTINUE;
905 static gboolean image_loader_begin(ImageLoader *il)
907 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
912 if (il->pixbuf) return FALSE;
914 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
915 if (b < 1) return FALSE;
917 image_loader_setup_loader(il);
919 g_assert(il->bytes_read == 0);
920 if (il->backend.load) {
922 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
924 image_loader_stop_loader(il);
928 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
930 image_loader_stop_loader(il);
935 format = il->backend.get_format_name(il->loader);
936 if (g_strcmp0(format, "pdf") == 0)
938 gint i = il->backend.get_page_total(il->loader);
939 file_data_set_page_total(il->fd, i);
944 format = il->backend.get_format_name(il->loader);
945 if (g_strcmp0(format, "heif") == 0)
947 gint i = il->backend.get_page_total(il->loader);
948 file_data_set_page_total(il->fd, i);
953 format = il->backend.get_format_name(il->loader);
954 if (g_strcmp0(format, "djvu") == 0)
956 gint i = il->backend.get_page_total(il->loader);
957 file_data_set_page_total(il->fd, i);
962 format = il->backend.get_format_name(il->loader);
963 if (g_strcmp0(format, "tiff") == 0)
965 gint i = il->backend.get_page_total(il->loader);
966 file_data_set_page_total(il->fd, i);
973 /* read until size is known */
974 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
976 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
977 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
979 image_loader_stop_loader(il);
984 if (!il->pixbuf) image_loader_sync_pixbuf(il);
986 if (il->bytes_read == il->bytes_total || b < 1)
988 /* done, handle (broken) loaders that do not have pixbuf till close */
989 image_loader_stop_loader(il);
991 if (!il->pixbuf) return FALSE;
993 image_loader_done(il);
999 image_loader_stop_loader(il);
1006 /**************************************************************************************/
1007 /* the following functions are always executed in the main thread */
1010 static gboolean image_loader_setup_source(ImageLoader *il)
1015 if (!il || il->loader || il->mapped_file) return FALSE;
1017 il->mapped_file = nullptr;
1021 ExifData *exif = exif_read_fd(il->fd);
1023 if (options->thumbnails.use_exif)
1025 il->mapped_file = exif_get_preview(exif, reinterpret_cast<guint *>(&il->bytes_total), il->requested_width, il->requested_height);
1027 if (il->mapped_file)
1029 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1034 il->mapped_file = libraw_get_preview(il, reinterpret_cast<guint *>(&il->bytes_total));
1036 if (il->mapped_file)
1038 /* Both exiv2 and libraw sometimes return a pointer to a file
1039 * section that is not a jpeg */
1040 if (il->mapped_file[0] != 0xFF || il->mapped_file[1] != 0xD8)
1042 il->mapped_file = nullptr;
1046 il->preview = IMAGE_LOADER_PREVIEW_LIBRAW;
1051 /* If libraw does not find a thumbnail, try exiv2 */
1052 if (!il->mapped_file)
1054 il->mapped_file = exif_get_preview(exif, reinterpret_cast<guint *>(&il->bytes_total), 0, 0); /* get the largest available preview image or NULL for normal images*/
1056 if (il->mapped_file)
1058 /* Both exiv2 and libraw sometimes return a pointer to a file
1059 * section that is not a jpeg */
1060 if (il->mapped_file[0] != 0xFF || il->mapped_file[1] != 0xD8)
1062 il->mapped_file = nullptr;
1066 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1071 if (il->mapped_file)
1073 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
1075 exif_free_fd(il->fd, exif);
1079 if (!il->mapped_file)
1084 pathl = path_from_utf8(il->fd->path);
1085 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
1087 if (load_fd == -1) return FALSE;
1089 if (fstat(load_fd, &st) == 0)
1091 il->bytes_total = st.st_size;
1099 il->mapped_file = static_cast<guchar *>(mmap(nullptr, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0));
1101 if (il->mapped_file == MAP_FAILED)
1103 il->mapped_file = nullptr;
1106 il->preview = IMAGE_LOADER_PREVIEW_NONE;
1112 static void image_loader_stop_source(ImageLoader *il)
1116 if (il->mapped_file)
1118 if (il->preview == IMAGE_LOADER_PREVIEW_EXIF)
1120 exif_free_preview(il->mapped_file);
1122 else if (il->preview == IMAGE_LOADER_PREVIEW_LIBRAW)
1124 libraw_free_preview(il->mapped_file);
1128 munmap(il->mapped_file, il->bytes_total);
1130 il->mapped_file = nullptr;
1134 static void image_loader_stop(ImageLoader *il)
1140 g_source_remove(il->idle_id);
1146 /* stop loader in the other thread */
1147 g_mutex_lock(il->data_mutex);
1148 il->stopping = TRUE;
1149 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
1150 g_mutex_unlock(il->data_mutex);
1153 image_loader_stop_loader(il);
1154 image_loader_stop_source(il);
1158 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
1160 g_mutex_lock(il->data_mutex);
1161 il->delay_area_ready = enable;
1167 list = g_list_reverse(il->area_param_delayed_list);
1168 il->area_param_delayed_list = nullptr;
1169 g_mutex_unlock(il->data_mutex);
1175 auto par = static_cast<ImageLoaderAreaParam *>(work->data);
1178 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1180 g_list_free_full(list, g_free);
1185 g_mutex_unlock(il->data_mutex);
1190 /**************************************************************************************/
1191 /* execution via idle calls */
1193 static gboolean image_loader_idle_cb(gpointer data)
1195 gboolean ret = G_SOURCE_REMOVE;
1196 auto il = static_cast<ImageLoader *>(data);
1200 ret = image_loader_continue(il);
1205 image_loader_stop_source(il);
1211 static gboolean image_loader_start_idle(ImageLoader *il)
1215 if (!il) return FALSE;
1217 if (!il->fd) return FALSE;
1219 if (!image_loader_setup_source(il)) return FALSE;
1221 ret = image_loader_begin(il);
1223 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, nullptr);
1227 /**************************************************************************************/
1228 /* execution via thread */
1230 static GThreadPool *image_loader_thread_pool = nullptr;
1232 static GCond *image_loader_prio_cond = nullptr;
1233 static GMutex *image_loader_prio_mutex = nullptr;
1234 static gint image_loader_prio_num = 0;
1237 static void image_loader_thread_enter_high()
1239 g_mutex_lock(image_loader_prio_mutex);
1240 image_loader_prio_num++;
1241 g_mutex_unlock(image_loader_prio_mutex);
1244 static void image_loader_thread_leave_high()
1246 g_mutex_lock(image_loader_prio_mutex);
1247 image_loader_prio_num--;
1248 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1249 g_mutex_unlock(image_loader_prio_mutex);
1252 static void image_loader_thread_wait_high()
1254 g_mutex_lock(image_loader_prio_mutex);
1255 while (image_loader_prio_num)
1257 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1260 g_mutex_unlock(image_loader_prio_mutex);
1264 static void image_loader_thread_run(gpointer data, gpointer)
1266 auto il = static_cast<ImageLoader *>(data);
1270 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1272 /* low prio, wait until high prio tasks finishes */
1273 image_loader_thread_wait_high();
1278 image_loader_thread_enter_high();
1281 err = !image_loader_begin(il);
1286 loader failed, we have to send signal
1287 (idle mode returns the image_loader_begin return value directly)
1288 (success is always reported indirectly from image_loader_begin)
1290 image_loader_emit_error(il);
1295 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1297 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1299 /* low prio, wait until high prio tasks finishes */
1300 image_loader_thread_wait_high();
1302 cont = image_loader_continue(il);
1304 image_loader_stop_loader(il);
1306 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1309 image_loader_thread_leave_high();
1312 g_mutex_lock(il->data_mutex);
1313 il->can_destroy = TRUE;
1314 g_cond_signal(il->can_destroy_cond);
1315 g_mutex_unlock(il->data_mutex);
1320 static gboolean image_loader_start_thread(ImageLoader *il)
1322 if (!il) return FALSE;
1324 if (!il->fd) return FALSE;
1328 if (!image_loader_setup_source(il)) return FALSE;
1330 if (!image_loader_thread_pool)
1332 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, nullptr, -1, FALSE, nullptr);
1333 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1334 g_cond_init(image_loader_prio_cond);
1335 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1336 g_mutex_init(image_loader_prio_mutex);
1339 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1341 g_thread_pool_push(image_loader_thread_pool, il, nullptr);
1342 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1348 /**************************************************************************************/
1349 /* public interface */
1352 gboolean image_loader_start(ImageLoader *il)
1354 if (!il) return FALSE;
1356 if (!il->fd) return FALSE;
1358 return image_loader_start_thread(il);
1362 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1363 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1366 if (!il) return nullptr;
1368 g_mutex_lock(il->data_mutex);
1370 g_mutex_unlock(il->data_mutex);
1374 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1378 g_mutex_lock(il->data_mutex);
1379 il->requested_width = width;
1380 il->requested_height = height;
1381 g_mutex_unlock(il->data_mutex);
1384 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1388 g_mutex_lock(il->data_mutex);
1389 il->idle_read_loop_count = count ? count : 1;
1390 g_mutex_unlock(il->data_mutex);
1393 void image_loader_set_priority(ImageLoader *il, gint priority)
1397 if (il->thread) return; /* can't change prio if the thread already runs */
1398 il->idle_priority = priority;
1402 gdouble image_loader_get_percent(ImageLoader *il)
1405 if (!il) return 0.0;
1407 g_mutex_lock(il->data_mutex);
1408 if (il->bytes_total == 0)
1414 ret = static_cast<gdouble>(il->bytes_read) / il->bytes_total;
1416 g_mutex_unlock(il->data_mutex);
1420 gboolean image_loader_get_is_done(ImageLoader *il)
1423 if (!il) return FALSE;
1425 g_mutex_lock(il->data_mutex);
1427 g_mutex_unlock(il->data_mutex);
1432 FileData *image_loader_get_fd(ImageLoader *il)
1435 if (!il) return nullptr;
1437 g_mutex_lock(il->data_mutex);
1439 g_mutex_unlock(il->data_mutex);
1444 gboolean image_loader_get_shrunk(ImageLoader *il)
1447 if (!il) return FALSE;
1449 g_mutex_lock(il->data_mutex);
1451 g_mutex_unlock(il->data_mutex);
1455 const gchar *image_loader_get_error(ImageLoader *il)
1457 const gchar *ret = nullptr;
1458 if (!il) return nullptr;
1459 g_mutex_lock(il->data_mutex);
1460 if (il->error) ret = il->error->message;
1461 g_mutex_unlock(il->data_mutex);
1467 * @FIXME this can be rather slow and blocks until the size is known
1469 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1474 il = image_loader_new(fd);
1476 success = image_loader_start_idle(il);
1478 if (success && il->pixbuf)
1480 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1481 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1485 if (width) *width = -1;
1486 if (height) *height = -1;
1489 image_loader_free(il);
1493 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */