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.
22 #include "image-load.h"
29 #include "gq-marshal.h"
30 #include "image-load-collection.h"
31 #include "image-load-dds.h"
32 #include "image-load-external.h"
33 #include "image-load-gdk.h"
34 #include "image-load-libraw.h"
35 #include "image-load-psd.h"
36 #include "image-load-svgz.h"
37 #include "image-load-webp.h"
38 #include "image-load-zxscr.h"
41 #include "ui-fileops.h"
44 #include "image-load-djvu.h"
46 #ifdef HAVE_FFMPEGTHUMBNAILER
47 #include "image-load-ffmpegthumbnailer.h"
50 #include "image-load-heif.h"
53 #include "image-load-j2k.h"
56 #include "image-load-cr3.h"
57 #include "image-load-jpeg.h"
60 #include "image-load-jpegxl.h"
63 #include "image-load-pdf.h"
66 #include "image-load-tiff.h"
73 IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT = 4096,
74 IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT = 1
77 /* image loader class */
81 SIGNAL_AREA_READY = 0,
89 static guint signals[SIGNAL_COUNT] = { 0 };
91 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
92 static void image_loader_class_init_wrapper(void *data, void *user_data);
93 static void image_loader_class_init(ImageLoaderClass *loader_class);
94 static void image_loader_finalize(GObject *object);
95 static void image_loader_stop(ImageLoader *il);
97 GType image_loader_get_type()
99 static GType type = 0;
102 static const GTypeInfo info = {
103 sizeof(ImageLoaderClass),
104 nullptr, /* base_init */
105 nullptr, /* base_finalize */
106 static_cast<GClassInitFunc>(image_loader_class_init_wrapper), /* class_init */
107 nullptr, /* class_finalize */
108 nullptr, /* class_data */
111 static_cast<GInstanceInitFunc>(image_loader_init), /* instance_init */
112 nullptr /* value_table */
114 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, static_cast<GTypeFlags>(0));
119 static void image_loader_init(GTypeInstance *instance, gpointer)
121 auto il = reinterpret_cast<ImageLoader *>(instance);
123 il->pixbuf = nullptr;
125 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
127 il->loader = nullptr;
132 il->idle_done_id = 0;
134 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
135 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
136 il->mapped_file = nullptr;
137 il->preview = IMAGE_LOADER_PREVIEW_NONE;
139 il->requested_width = 0;
140 il->requested_height = 0;
141 il->actual_width = 0;
142 il->actual_height = 0;
145 il->can_destroy = TRUE;
147 il->data_mutex = g_new(GMutex, 1);
148 g_mutex_init(il->data_mutex);
149 il->can_destroy_cond = g_new(GCond, 1);
150 g_cond_init(il->can_destroy_cond);
152 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", (void *)il, il->read_buffer_size, il->idle_read_loop_count);
155 static void image_loader_class_init_wrapper(void *data, void *)
157 image_loader_class_init(static_cast<ImageLoaderClass *>(data));
160 static void image_loader_class_init(ImageLoaderClass *loader_class)
162 GObjectClass *gobject_class = G_OBJECT_CLASS (loader_class);
164 gobject_class->finalize = image_loader_finalize;
167 signals[SIGNAL_AREA_READY] =
168 g_signal_new("area_ready",
169 G_OBJECT_CLASS_TYPE(gobject_class),
171 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
173 gq_marshal_VOID__INT_INT_INT_INT,
180 signals[SIGNAL_ERROR] =
181 g_signal_new("error",
182 G_OBJECT_CLASS_TYPE(gobject_class),
184 G_STRUCT_OFFSET(ImageLoaderClass, error),
186 g_cclosure_marshal_VOID__VOID,
189 signals[SIGNAL_DONE] =
191 G_OBJECT_CLASS_TYPE(gobject_class),
193 G_STRUCT_OFFSET(ImageLoaderClass, done),
195 g_cclosure_marshal_VOID__VOID,
198 signals[SIGNAL_PERCENT] =
199 g_signal_new("percent",
200 G_OBJECT_CLASS_TYPE(gobject_class),
202 G_STRUCT_OFFSET(ImageLoaderClass, percent),
204 g_cclosure_marshal_VOID__DOUBLE,
208 signals[SIGNAL_SIZE] =
209 g_signal_new("size_prepared",
210 G_OBJECT_CLASS_TYPE(gobject_class),
212 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
214 gq_marshal_VOID__INT_INT,
221 static void image_loader_finalize(GObject *object)
223 auto il = reinterpret_cast<ImageLoader *>(object);
225 image_loader_stop(il);
227 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
229 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, (void *)il, il->bytes_read);
231 if (il->idle_done_id)
233 g_source_remove(il->idle_done_id);
234 il->idle_done_id = 0;
237 while (g_source_remove_by_user_data(il))
239 DEBUG_2("pending signals detected");
242 while (il->area_param_list)
244 DEBUG_1("pending area_ready signals detected");
245 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
246 g_free(il->area_param_list->data);
247 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
250 while (il->area_param_delayed_list)
252 g_free(il->area_param_delayed_list->data);
253 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
256 if (il->pixbuf) g_object_unref(il->pixbuf);
258 if (il->error) g_error_free(il->error);
260 file_data_unref(il->fd);
262 g_mutex_clear(il->data_mutex);
263 g_free(il->data_mutex);
264 g_cond_clear(il->can_destroy_cond);
265 g_free(il->can_destroy_cond);
268 void image_loader_free(ImageLoader *il)
271 g_object_unref(G_OBJECT(il));
275 ImageLoader *image_loader_new(FileData *fd)
279 if (!fd) return nullptr;
281 il = static_cast<ImageLoader *>(g_object_new(TYPE_IMAGE_LOADER, nullptr));
283 il->fd = file_data_ref(fd);
288 /**************************************************************************************/
289 /* send signals via idle calbacks - the callback are executed in the main thread */
291 struct ImageLoaderAreaParam {
300 static gboolean image_loader_emit_area_ready_cb(gpointer data)
302 auto par = static_cast<ImageLoaderAreaParam *>(data);
303 ImageLoader *il = par->il;
308 g_mutex_lock(il->data_mutex);
309 il->area_param_list = g_list_remove(il->area_param_list, par);
315 g_mutex_unlock(il->data_mutex);
317 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
319 return G_SOURCE_REMOVE;
322 static gboolean image_loader_emit_done_cb(gpointer data)
324 auto il = static_cast<ImageLoader *>(data);
325 g_signal_emit(il, signals[SIGNAL_DONE], 0);
326 return G_SOURCE_REMOVE;
329 static gboolean image_loader_emit_error_cb(gpointer data)
331 auto il = static_cast<ImageLoader *>(data);
332 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
333 return G_SOURCE_REMOVE;
336 static gboolean image_loader_emit_percent_cb(gpointer data)
338 auto il = static_cast<ImageLoader *>(data);
339 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
340 return G_SOURCE_REMOVE;
343 static gboolean image_loader_emit_size_cb(gpointer data)
347 auto il = static_cast<ImageLoader *>(data);
348 g_mutex_lock(il->data_mutex);
349 width = il->actual_width;
350 height = il->actual_height;
351 g_mutex_unlock(il->data_mutex);
352 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
353 return G_SOURCE_REMOVE;
357 /* DONE and ERROR are emitted only once, thus they can have normal priority
358 PERCENT and AREA_READY should be processed ASAP
361 static void image_loader_emit_done(ImageLoader *il)
363 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, nullptr);
366 static void image_loader_emit_error(ImageLoader *il)
368 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, nullptr);
371 static void image_loader_emit_percent(ImageLoader *il)
373 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, nullptr);
376 static void image_loader_emit_size(ImageLoader *il)
378 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, nullptr);
381 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
385 auto prev_par = static_cast<ImageLoaderAreaParam *>((*list)->data);
386 if (prev_par->x == x && prev_par->w == w &&
387 prev_par->y + prev_par->h == y)
389 /* we can merge the notifications */
393 if (prev_par->x == x && prev_par->w == w &&
394 y + h == prev_par->y)
396 /* we can merge the notifications */
401 if (prev_par->y == y && prev_par->h == h &&
402 prev_par->x + prev_par->w == x)
404 /* we can merge the notifications */
408 if (prev_par->y == y && prev_par->h == h &&
409 x + w == prev_par->x)
411 /* we can merge the notifications */
418 auto par = g_new0(ImageLoaderAreaParam, 1);
425 *list = g_list_prepend(*list, par);
429 /* this function expects that il->data_mutex is locked by caller */
430 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
432 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
436 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, nullptr);
440 /**************************************************************************************/
441 /* the following functions may be executed in separate thread */
443 /* this function expects that il->data_mutex is locked by caller */
444 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
446 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
451 static gboolean image_loader_get_stopping(ImageLoader *il)
454 if (!il) return FALSE;
456 g_mutex_lock(il->data_mutex);
458 g_mutex_unlock(il->data_mutex);
464 static void image_loader_sync_pixbuf(ImageLoader *il)
468 g_mutex_lock(il->data_mutex);
472 g_mutex_unlock(il->data_mutex);
476 pb = il->backend.get_pixbuf(il->loader);
478 if (pb == il->pixbuf)
480 g_mutex_unlock(il->data_mutex);
484 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
486 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
489 if (il->pixbuf) g_object_unref(il->pixbuf);
492 if (il->pixbuf) g_object_ref(il->pixbuf);
494 g_mutex_unlock(il->data_mutex);
497 static void image_loader_area_updated_cb(gpointer,
498 guint x, guint y, guint w, guint h,
501 auto il = static_cast<ImageLoader *>(data);
503 if (!image_loader_get_pixbuf(il))
505 image_loader_sync_pixbuf(il);
506 if (!image_loader_get_pixbuf(il))
508 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
512 g_mutex_lock(il->data_mutex);
513 if (il->delay_area_ready)
514 image_loader_queue_delayed_area_ready(il, x, y, w, h);
516 image_loader_emit_area_ready(il, x, y, w, h);
518 if (il->stopping) il->backend.abort(il->loader);
520 g_mutex_unlock(il->data_mutex);
523 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
525 auto il = static_cast<ImageLoader *>(data);
532 https://bugzilla.gnome.org/show_bug.cgi?id=547669
533 https://bugzilla.gnome.org/show_bug.cgi?id=589334
535 gchar *format = il->backend.get_format_name(loader);
536 if (strcmp(format, "svg") == 0 ||
537 strcmp(format, "xpm") == 0)
545 pb = il->backend.get_pixbuf(loader);
547 h = gdk_pixbuf_get_height(pb);
548 rs = gdk_pixbuf_get_rowstride(pb);
549 pix = gdk_pixbuf_get_pixels(pb);
551 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
555 static void image_loader_size_cb(gpointer loader,
556 gint width, gint height, gpointer data)
558 auto il = static_cast<ImageLoader *>(data);
560 gboolean scale = FALSE;
563 g_mutex_lock(il->data_mutex);
564 il->actual_width = width;
565 il->actual_height = height;
566 if (il->requested_width < 1 || il->requested_height < 1)
568 g_mutex_unlock(il->data_mutex);
569 image_loader_emit_size(il);
572 g_mutex_unlock(il->data_mutex);
574 #ifdef HAVE_FFMPEGTHUMBNAILER
575 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
578 mime_types = il->backend.get_format_mime_types(loader);
580 while (mime_types[n] && !scale)
582 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
585 g_strfreev(mime_types);
589 image_loader_emit_size(il);
593 g_mutex_lock(il->data_mutex);
597 if (width > il->requested_width || height > il->requested_height)
600 if ((static_cast<gdouble>(il->requested_width) / width) < (static_cast<gdouble>(il->requested_height) / height))
602 nw = il->requested_width;
603 nh = static_cast<gdouble>(nw) / width * height;
608 nh = il->requested_height;
609 nw = static_cast<gdouble>(nh) / height * width;
613 il->actual_width = nw;
614 il->actual_height = nh;
615 il->backend.set_size(loader, nw, nh);
619 g_mutex_unlock(il->data_mutex);
620 image_loader_emit_size(il);
623 static void image_loader_stop_loader(ImageLoader *il)
629 /* some loaders do not have a pixbuf till close, order is important here */
630 il->backend.close(il->loader, il->error ? nullptr : &il->error); /* we are interested in the first error only */
631 image_loader_sync_pixbuf(il);
632 il->backend.free(il->loader);
633 il->loader = nullptr;
635 g_mutex_lock(il->data_mutex);
637 g_mutex_unlock(il->data_mutex);
640 static void image_loader_setup_loader(ImageLoader *il)
642 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
646 gint external_preview = 1;
648 g_mutex_lock(il->data_mutex);
650 if (options->external_preview.enable)
653 gchar *tilde_filename;
655 tilde_filename = expand_tilde(options->external_preview.select);
657 cmd_line = g_strdup_printf("\"%s\" \"%s\"" , tilde_filename, il->fd->path);
659 external_preview = runcmd(cmd_line);
661 g_free(tilde_filename);
664 if (external_preview == 0)
666 DEBUG_1("Using custom external loader");
667 image_loader_backend_set_external(&il->backend);
671 #ifdef HAVE_FFMPEGTHUMBNAILER
672 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
674 DEBUG_1("Using custom ffmpegthumbnailer loader");
675 image_loader_backend_set_ft(&il->backend);
680 if (il->bytes_total >= 4 &&
681 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
683 DEBUG_1("Using custom pdf loader");
684 image_loader_backend_set_pdf(&il->backend);
689 if (il->bytes_total >= 12 &&
690 ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
691 (memcmp(il->mapped_file + 4, "ftypheix", 8) == 0) ||
692 (memcmp(il->mapped_file + 4, "ftypmsf1", 8) == 0) ||
693 (memcmp(il->mapped_file + 4, "ftypmif1", 8) == 0) ||
694 (memcmp(il->mapped_file + 4, "ftypavif", 8) == 0)))
696 DEBUG_1("Using custom heif loader");
697 image_loader_backend_set_heif(&il->backend);
702 if (il->bytes_total >= 12 &&
703 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
704 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
706 DEBUG_1("Using custom webp loader");
707 image_loader_backend_set_webp(&il->backend);
712 if (il->bytes_total >= 16 &&
713 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
714 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
716 DEBUG_1("Using custom djvu loader");
717 image_loader_backend_set_djvu(&il->backend);
722 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
724 DEBUG_1("Using custom jpeg loader");
725 image_loader_backend_set_jpeg(&il->backend);
729 if (il->bytes_total >= 11 &&
730 (memcmp(il->mapped_file + 4, "ftypcrx", 7) == 0) &&
731 (memcmp(il->mapped_file + 64, "CanonCR3", 8) == 0))
733 DEBUG_1("Using custom cr3 loader");
734 image_loader_backend_set_cr3(&il->backend);
740 if (il->bytes_total >= 10 &&
741 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
742 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
743 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
744 memcmp(il->mapped_file, "II*\0", 4) == 0))
746 DEBUG_1("Using custom tiff loader");
747 image_loader_backend_set_tiff(&il->backend);
751 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
753 DEBUG_1("Using dds loader");
754 image_loader_backend_set_dds(&il->backend);
757 if (il->bytes_total >= 6 &&
758 (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
760 DEBUG_1("Using custom psd loader");
761 image_loader_backend_set_psd(&il->backend);
765 if (il->bytes_total >= 12 &&
766 (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
768 DEBUG_1("Using custom j2k loader");
769 image_loader_backend_set_j2k(&il->backend);
774 if (il->bytes_total >= 12 &&
775 (memcmp(il->mapped_file, "\0\0\0\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A", 12) == 0))
777 DEBUG_1("Using custom jpeg xl loader");
778 image_loader_backend_set_jpegxl(&il->backend);
781 if (il->bytes_total >= 2 &&
782 (memcmp(il->mapped_file, "\xFF\x0A", 2) == 0))
784 DEBUG_1("Using custom jpeg xl loader");
785 image_loader_backend_set_jpegxl(&il->backend);
789 if ((il->bytes_total == 6144 || il->bytes_total == 6912) &&
790 (file_extension_match(il->fd->path, ".scr")))
792 DEBUG_1("Using custom zxscr loader");
793 image_loader_backend_set_zxscr(&il->backend);
796 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
798 DEBUG_1("Using custom collection loader");
799 image_loader_backend_set_collection(&il->backend);
802 if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
804 DEBUG_1("Using custom svgz loader");
805 image_loader_backend_set_svgz(&il->backend);
808 image_loader_backend_set_default(&il->backend);
811 il->loader = static_cast<void **>(il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il));
814 format = il->backend.get_format_name(il->loader);
815 if (g_strcmp0(format, "tiff") == 0)
817 il->backend.set_page_num(il->loader, il->fd->page_num);
823 format = il->backend.get_format_name(il->loader);
824 if (g_strcmp0(format, "pdf") == 0)
826 il->backend.set_page_num(il->loader, il->fd->page_num);
832 format = il->backend.get_format_name(il->loader);
833 if (g_strcmp0(format, "heif") == 0)
835 il->backend.set_page_num(il->loader, il->fd->page_num);
841 format = il->backend.get_format_name(il->loader);
842 if (g_strcmp0(format, "djvu") == 0)
844 il->backend.set_page_num(il->loader, il->fd->page_num);
849 il->fd->format_name = il->backend.get_format_name(il->loader);
851 g_mutex_unlock(il->data_mutex);
855 static void image_loader_done(ImageLoader *il)
857 image_loader_stop_loader(il);
859 image_loader_emit_done(il);
862 static void image_loader_error(ImageLoader *il)
864 image_loader_stop_loader(il);
866 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
868 image_loader_emit_error(il);
871 static gboolean image_loader_continue(ImageLoader *il)
876 if (!il) return G_SOURCE_REMOVE;
878 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
879 while (c > 0 && !image_loader_get_stopping(il))
881 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
885 image_loader_done(il);
886 return G_SOURCE_REMOVE;
889 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
891 image_loader_error(il);
892 return G_SOURCE_REMOVE;
900 if (il->bytes_total > 0)
902 image_loader_emit_percent(il);
905 return G_SOURCE_CONTINUE;
908 static gboolean image_loader_begin(ImageLoader *il)
910 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
915 if (il->pixbuf) return FALSE;
917 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
918 if (b < 1) return FALSE;
920 image_loader_setup_loader(il);
922 g_assert(il->bytes_read == 0);
923 if (il->backend.load) {
925 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
927 image_loader_stop_loader(il);
931 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
933 image_loader_stop_loader(il);
938 format = il->backend.get_format_name(il->loader);
939 if (g_strcmp0(format, "pdf") == 0)
941 gint i = il->backend.get_page_total(il->loader);
942 file_data_set_page_total(il->fd, i);
947 format = il->backend.get_format_name(il->loader);
948 if (g_strcmp0(format, "heif") == 0)
950 gint i = il->backend.get_page_total(il->loader);
951 file_data_set_page_total(il->fd, i);
956 format = il->backend.get_format_name(il->loader);
957 if (g_strcmp0(format, "djvu") == 0)
959 gint i = il->backend.get_page_total(il->loader);
960 file_data_set_page_total(il->fd, i);
965 format = il->backend.get_format_name(il->loader);
966 if (g_strcmp0(format, "tiff") == 0)
968 gint i = il->backend.get_page_total(il->loader);
969 file_data_set_page_total(il->fd, i);
976 /* read until size is known */
977 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
979 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
980 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
982 image_loader_stop_loader(il);
987 if (!il->pixbuf) image_loader_sync_pixbuf(il);
989 if (il->bytes_read == il->bytes_total || b < 1)
991 /* done, handle (broken) loaders that do not have pixbuf till close */
992 image_loader_stop_loader(il);
994 if (!il->pixbuf) return FALSE;
996 image_loader_done(il);
1002 image_loader_stop_loader(il);
1009 /**************************************************************************************/
1010 /* the following functions are always executed in the main thread */
1013 static gboolean image_loader_setup_source(ImageLoader *il)
1018 if (!il || il->loader || il->mapped_file) return FALSE;
1020 il->mapped_file = nullptr;
1024 ExifData *exif = exif_read_fd(il->fd);
1026 if (options->thumbnails.use_exif)
1028 il->mapped_file = exif_get_preview(exif, reinterpret_cast<guint *>(&il->bytes_total), il->requested_width, il->requested_height);
1030 if (il->mapped_file)
1032 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1037 il->mapped_file = libraw_get_preview(il, reinterpret_cast<guint *>(&il->bytes_total));
1039 if (il->mapped_file)
1041 /* Both exiv2 and libraw sometimes return a pointer to a file
1042 * section that is not a jpeg */
1043 if (il->mapped_file[0] != 0xFF || il->mapped_file[1] != 0xD8)
1045 il->mapped_file = nullptr;
1049 il->preview = IMAGE_LOADER_PREVIEW_LIBRAW;
1054 /* If libraw does not find a thumbnail, try exiv2 */
1055 if (!il->mapped_file)
1057 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*/
1059 if (il->mapped_file)
1061 /* Both exiv2 and libraw sometimes return a pointer to a file
1062 * section that is not a jpeg */
1063 if (il->mapped_file[0] != 0xFF || il->mapped_file[1] != 0xD8)
1065 il->mapped_file = nullptr;
1069 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1074 if (il->mapped_file)
1076 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
1078 exif_free_fd(il->fd, exif);
1082 if (!il->mapped_file)
1087 pathl = path_from_utf8(il->fd->path);
1088 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
1090 if (load_fd == -1) return FALSE;
1092 if (fstat(load_fd, &st) == 0)
1094 il->bytes_total = st.st_size;
1102 il->mapped_file = static_cast<guchar *>(mmap(nullptr, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0));
1104 if (il->mapped_file == MAP_FAILED)
1106 il->mapped_file = nullptr;
1109 il->preview = IMAGE_LOADER_PREVIEW_NONE;
1115 static void image_loader_stop_source(ImageLoader *il)
1119 if (il->mapped_file)
1121 if (il->preview == IMAGE_LOADER_PREVIEW_EXIF)
1123 exif_free_preview(il->mapped_file);
1125 else if (il->preview == IMAGE_LOADER_PREVIEW_LIBRAW)
1127 libraw_free_preview(il->mapped_file);
1131 munmap(il->mapped_file, il->bytes_total);
1133 il->mapped_file = nullptr;
1137 static void image_loader_stop(ImageLoader *il)
1143 g_source_remove(il->idle_id);
1149 /* stop loader in the other thread */
1150 g_mutex_lock(il->data_mutex);
1151 il->stopping = TRUE;
1152 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
1153 g_mutex_unlock(il->data_mutex);
1156 image_loader_stop_loader(il);
1157 image_loader_stop_source(il);
1161 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
1163 g_mutex_lock(il->data_mutex);
1164 il->delay_area_ready = enable;
1170 list = g_list_reverse(il->area_param_delayed_list);
1171 il->area_param_delayed_list = nullptr;
1172 g_mutex_unlock(il->data_mutex);
1178 auto par = static_cast<ImageLoaderAreaParam *>(work->data);
1181 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1183 g_list_free_full(list, g_free);
1188 g_mutex_unlock(il->data_mutex);
1193 /**************************************************************************************/
1194 /* execution via idle calls */
1196 static gboolean image_loader_idle_cb(gpointer data)
1198 gboolean ret = G_SOURCE_REMOVE;
1199 auto il = static_cast<ImageLoader *>(data);
1203 ret = image_loader_continue(il);
1208 image_loader_stop_source(il);
1214 static gboolean image_loader_start_idle(ImageLoader *il)
1218 if (!il) return FALSE;
1220 if (!il->fd) return FALSE;
1222 if (!image_loader_setup_source(il)) return FALSE;
1224 ret = image_loader_begin(il);
1226 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, nullptr);
1230 /**************************************************************************************/
1231 /* execution via thread */
1233 static GThreadPool *image_loader_thread_pool = nullptr;
1235 static GCond *image_loader_prio_cond = nullptr;
1236 static GMutex *image_loader_prio_mutex = nullptr;
1237 static gint image_loader_prio_num = 0;
1240 static void image_loader_thread_enter_high()
1242 g_mutex_lock(image_loader_prio_mutex);
1243 image_loader_prio_num++;
1244 g_mutex_unlock(image_loader_prio_mutex);
1247 static void image_loader_thread_leave_high()
1249 g_mutex_lock(image_loader_prio_mutex);
1250 image_loader_prio_num--;
1251 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1252 g_mutex_unlock(image_loader_prio_mutex);
1255 static void image_loader_thread_wait_high()
1257 g_mutex_lock(image_loader_prio_mutex);
1258 while (image_loader_prio_num)
1260 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1263 g_mutex_unlock(image_loader_prio_mutex);
1267 static void image_loader_thread_run(gpointer data, gpointer)
1269 auto il = static_cast<ImageLoader *>(data);
1273 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1275 /* low prio, wait until high prio tasks finishes */
1276 image_loader_thread_wait_high();
1281 image_loader_thread_enter_high();
1284 err = !image_loader_begin(il);
1289 loader failed, we have to send signal
1290 (idle mode returns the image_loader_begin return value directly)
1291 (success is always reported indirectly from image_loader_begin)
1293 image_loader_emit_error(il);
1298 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1300 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1302 /* low prio, wait until high prio tasks finishes */
1303 image_loader_thread_wait_high();
1305 cont = image_loader_continue(il);
1307 image_loader_stop_loader(il);
1309 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1312 image_loader_thread_leave_high();
1315 g_mutex_lock(il->data_mutex);
1316 il->can_destroy = TRUE;
1317 g_cond_signal(il->can_destroy_cond);
1318 g_mutex_unlock(il->data_mutex);
1323 static gboolean image_loader_start_thread(ImageLoader *il)
1325 if (!il) return FALSE;
1327 if (!il->fd) return FALSE;
1331 if (!image_loader_setup_source(il)) return FALSE;
1333 if (!image_loader_thread_pool)
1335 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, nullptr, -1, FALSE, nullptr);
1336 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1337 g_cond_init(image_loader_prio_cond);
1338 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1339 g_mutex_init(image_loader_prio_mutex);
1342 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1344 g_thread_pool_push(image_loader_thread_pool, il, nullptr);
1345 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1351 /**************************************************************************************/
1352 /* public interface */
1355 gboolean image_loader_start(ImageLoader *il)
1357 if (!il) return FALSE;
1359 if (!il->fd) return FALSE;
1361 return image_loader_start_thread(il);
1365 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1366 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1369 if (!il) return nullptr;
1371 g_mutex_lock(il->data_mutex);
1373 g_mutex_unlock(il->data_mutex);
1377 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1381 g_mutex_lock(il->data_mutex);
1382 il->requested_width = width;
1383 il->requested_height = height;
1384 g_mutex_unlock(il->data_mutex);
1387 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1391 g_mutex_lock(il->data_mutex);
1392 il->idle_read_loop_count = count ? count : 1;
1393 g_mutex_unlock(il->data_mutex);
1396 void image_loader_set_priority(ImageLoader *il, gint priority)
1400 if (il->thread) return; /* can't change prio if the thread already runs */
1401 il->idle_priority = priority;
1405 gdouble image_loader_get_percent(ImageLoader *il)
1408 if (!il) return 0.0;
1410 g_mutex_lock(il->data_mutex);
1411 if (il->bytes_total == 0)
1417 ret = static_cast<gdouble>(il->bytes_read) / il->bytes_total;
1419 g_mutex_unlock(il->data_mutex);
1423 gboolean image_loader_get_is_done(ImageLoader *il)
1426 if (!il) return FALSE;
1428 g_mutex_lock(il->data_mutex);
1430 g_mutex_unlock(il->data_mutex);
1435 FileData *image_loader_get_fd(ImageLoader *il)
1438 if (!il) return nullptr;
1440 g_mutex_lock(il->data_mutex);
1442 g_mutex_unlock(il->data_mutex);
1447 gboolean image_loader_get_shrunk(ImageLoader *il)
1450 if (!il) return FALSE;
1452 g_mutex_lock(il->data_mutex);
1454 g_mutex_unlock(il->data_mutex);
1458 const gchar *image_loader_get_error(ImageLoader *il)
1460 const gchar *ret = nullptr;
1461 if (!il) return nullptr;
1462 g_mutex_lock(il->data_mutex);
1463 if (il->error) ret = il->error->message;
1464 g_mutex_unlock(il->data_mutex);
1470 * @FIXME this can be rather slow and blocks until the size is known
1472 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1477 il = image_loader_new(fd);
1479 success = image_loader_start_idle(il);
1481 if (success && il->pixbuf)
1483 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1484 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1488 if (width) *width = -1;
1489 if (height) *height = -1;
1492 image_loader_free(il);
1496 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */