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"
69 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
70 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
72 /* image loader class */
76 SIGNAL_AREA_READY = 0,
84 static guint signals[SIGNAL_COUNT] = { 0 };
86 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
87 static void image_loader_class_init_wrapper(void *data, void *user_data);
88 static void image_loader_class_init(ImageLoaderClass *loader_class);
89 static void image_loader_finalize(GObject *object);
90 static void image_loader_stop(ImageLoader *il);
92 GType image_loader_get_type()
94 static GType type = 0;
97 static const GTypeInfo info = {
98 sizeof(ImageLoaderClass),
99 nullptr, /* base_init */
100 nullptr, /* base_finalize */
101 static_cast<GClassInitFunc>(image_loader_class_init_wrapper), /* class_init */
102 nullptr, /* class_finalize */
103 nullptr, /* class_data */
106 static_cast<GInstanceInitFunc>(image_loader_init), /* instance_init */
107 nullptr /* value_table */
109 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, static_cast<GTypeFlags>(0));
114 static void image_loader_init(GTypeInstance *instance, gpointer)
116 auto il = reinterpret_cast<ImageLoader *>(instance);
118 il->pixbuf = nullptr;
120 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
122 il->loader = nullptr;
127 il->idle_done_id = 0;
129 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
130 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
131 il->mapped_file = nullptr;
132 il->preview = IMAGE_LOADER_PREVIEW_NONE;
134 il->requested_width = 0;
135 il->requested_height = 0;
136 il->actual_width = 0;
137 il->actual_height = 0;
140 il->can_destroy = TRUE;
142 il->data_mutex = g_new(GMutex, 1);
143 g_mutex_init(il->data_mutex);
144 il->can_destroy_cond = g_new(GCond, 1);
145 g_cond_init(il->can_destroy_cond);
147 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", (void *)il, il->read_buffer_size, il->idle_read_loop_count);
150 static void image_loader_class_init_wrapper(void *data, void *)
152 image_loader_class_init(static_cast<ImageLoaderClass *>(data));
155 static void image_loader_class_init(ImageLoaderClass *loader_class)
157 GObjectClass *gobject_class = G_OBJECT_CLASS (loader_class);
159 gobject_class->finalize = image_loader_finalize;
162 signals[SIGNAL_AREA_READY] =
163 g_signal_new("area_ready",
164 G_OBJECT_CLASS_TYPE(gobject_class),
166 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
168 gq_marshal_VOID__INT_INT_INT_INT,
175 signals[SIGNAL_ERROR] =
176 g_signal_new("error",
177 G_OBJECT_CLASS_TYPE(gobject_class),
179 G_STRUCT_OFFSET(ImageLoaderClass, error),
181 g_cclosure_marshal_VOID__VOID,
184 signals[SIGNAL_DONE] =
186 G_OBJECT_CLASS_TYPE(gobject_class),
188 G_STRUCT_OFFSET(ImageLoaderClass, done),
190 g_cclosure_marshal_VOID__VOID,
193 signals[SIGNAL_PERCENT] =
194 g_signal_new("percent",
195 G_OBJECT_CLASS_TYPE(gobject_class),
197 G_STRUCT_OFFSET(ImageLoaderClass, percent),
199 g_cclosure_marshal_VOID__DOUBLE,
203 signals[SIGNAL_SIZE] =
204 g_signal_new("size_prepared",
205 G_OBJECT_CLASS_TYPE(gobject_class),
207 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
209 gq_marshal_VOID__INT_INT,
216 static void image_loader_finalize(GObject *object)
218 auto il = reinterpret_cast<ImageLoader *>(object);
220 image_loader_stop(il);
222 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
224 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, (void *)il, il->bytes_read);
226 if (il->idle_done_id)
228 g_source_remove(il->idle_done_id);
229 il->idle_done_id = 0;
232 while (g_source_remove_by_user_data(il))
234 DEBUG_2("pending signals detected");
237 while (il->area_param_list)
239 DEBUG_1("pending area_ready signals detected");
240 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
241 g_free(il->area_param_list->data);
242 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
245 while (il->area_param_delayed_list)
247 g_free(il->area_param_delayed_list->data);
248 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
251 if (il->pixbuf) g_object_unref(il->pixbuf);
253 if (il->error) g_error_free(il->error);
255 file_data_unref(il->fd);
257 g_mutex_clear(il->data_mutex);
258 g_free(il->data_mutex);
259 g_cond_clear(il->can_destroy_cond);
260 g_free(il->can_destroy_cond);
263 void image_loader_free(ImageLoader *il)
266 g_object_unref(G_OBJECT(il));
270 ImageLoader *image_loader_new(FileData *fd)
274 if (!fd) return nullptr;
276 il = static_cast<ImageLoader *>(g_object_new(TYPE_IMAGE_LOADER, nullptr));
278 il->fd = file_data_ref(fd);
283 /**************************************************************************************/
284 /* send signals via idle calbacks - the callback are executed in the main thread */
286 struct ImageLoaderAreaParam {
295 static gboolean image_loader_emit_area_ready_cb(gpointer data)
297 auto par = static_cast<ImageLoaderAreaParam *>(data);
298 ImageLoader *il = par->il;
300 g_mutex_lock(il->data_mutex);
301 il->area_param_list = g_list_remove(il->area_param_list, par);
307 g_mutex_unlock(il->data_mutex);
309 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
311 return G_SOURCE_REMOVE;
314 static gboolean image_loader_emit_done_cb(gpointer data)
316 auto il = static_cast<ImageLoader *>(data);
317 g_signal_emit(il, signals[SIGNAL_DONE], 0);
318 return G_SOURCE_REMOVE;
321 static gboolean image_loader_emit_error_cb(gpointer data)
323 auto il = static_cast<ImageLoader *>(data);
324 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
325 return G_SOURCE_REMOVE;
328 static gboolean image_loader_emit_percent_cb(gpointer data)
330 auto il = static_cast<ImageLoader *>(data);
331 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
332 return G_SOURCE_REMOVE;
335 static gboolean image_loader_emit_size_cb(gpointer data)
338 auto il = static_cast<ImageLoader *>(data);
339 g_mutex_lock(il->data_mutex);
340 width = il->actual_width;
341 height = il->actual_height;
342 g_mutex_unlock(il->data_mutex);
343 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
344 return G_SOURCE_REMOVE;
348 /* DONE and ERROR are emitted only once, thus they can have normal priority
349 PERCENT and AREA_READY should be processed ASAP
352 static void image_loader_emit_done(ImageLoader *il)
354 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, nullptr);
357 static void image_loader_emit_error(ImageLoader *il)
359 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, nullptr);
362 static void image_loader_emit_percent(ImageLoader *il)
364 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, nullptr);
367 static void image_loader_emit_size(ImageLoader *il)
369 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, nullptr);
372 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
376 auto prev_par = static_cast<ImageLoaderAreaParam *>((*list)->data);
377 if (prev_par->x == x && prev_par->w == w &&
378 prev_par->y + prev_par->h == y)
380 /* we can merge the notifications */
384 if (prev_par->x == x && prev_par->w == w &&
385 y + h == prev_par->y)
387 /* we can merge the notifications */
392 if (prev_par->y == y && prev_par->h == h &&
393 prev_par->x + prev_par->w == x)
395 /* we can merge the notifications */
399 if (prev_par->y == y && prev_par->h == h &&
400 x + w == prev_par->x)
402 /* we can merge the notifications */
409 auto par = g_new0(ImageLoaderAreaParam, 1);
416 *list = g_list_prepend(*list, par);
420 /* this function expects that il->data_mutex is locked by caller */
421 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
423 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
427 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, nullptr);
431 /**************************************************************************************/
432 /* the following functions may be executed in separate thread */
434 /* this function expects that il->data_mutex is locked by caller */
435 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
437 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
442 static gboolean image_loader_get_stopping(ImageLoader *il)
445 if (!il) return FALSE;
447 g_mutex_lock(il->data_mutex);
449 g_mutex_unlock(il->data_mutex);
455 static void image_loader_sync_pixbuf(ImageLoader *il)
459 g_mutex_lock(il->data_mutex);
463 g_mutex_unlock(il->data_mutex);
467 pb = il->backend.get_pixbuf(il->loader);
469 if (pb == il->pixbuf)
471 g_mutex_unlock(il->data_mutex);
475 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
477 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
480 if (il->pixbuf) g_object_unref(il->pixbuf);
483 if (il->pixbuf) g_object_ref(il->pixbuf);
485 g_mutex_unlock(il->data_mutex);
488 static void image_loader_area_updated_cb(gpointer,
489 guint x, guint y, guint w, guint h,
492 auto il = static_cast<ImageLoader *>(data);
494 if (!image_loader_get_pixbuf(il))
496 image_loader_sync_pixbuf(il);
497 if (!image_loader_get_pixbuf(il))
499 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
503 g_mutex_lock(il->data_mutex);
504 if (il->delay_area_ready)
505 image_loader_queue_delayed_area_ready(il, x, y, w, h);
507 image_loader_emit_area_ready(il, x, y, w, h);
509 if (il->stopping) il->backend.abort(il->loader);
511 g_mutex_unlock(il->data_mutex);
514 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
516 auto il = static_cast<ImageLoader *>(data);
522 https://bugzilla.gnome.org/show_bug.cgi?id=547669
523 https://bugzilla.gnome.org/show_bug.cgi?id=589334
525 gchar *format = il->backend.get_format_name(loader);
526 if (strcmp(format, "svg") == 0 ||
527 strcmp(format, "xpm") == 0)
535 pb = il->backend.get_pixbuf(loader);
537 h = gdk_pixbuf_get_height(pb);
538 rs = gdk_pixbuf_get_rowstride(pb);
539 pix = gdk_pixbuf_get_pixels(pb);
541 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
545 static void image_loader_size_cb(gpointer loader,
546 gint width, gint height, gpointer data)
548 auto il = static_cast<ImageLoader *>(data);
550 gboolean scale = FALSE;
553 g_mutex_lock(il->data_mutex);
554 il->actual_width = width;
555 il->actual_height = height;
556 if (il->requested_width < 1 || il->requested_height < 1)
558 g_mutex_unlock(il->data_mutex);
559 image_loader_emit_size(il);
562 g_mutex_unlock(il->data_mutex);
564 #ifdef HAVE_FFMPEGTHUMBNAILER
565 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
568 mime_types = il->backend.get_format_mime_types(loader);
570 while (mime_types[n] && !scale)
572 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
575 g_strfreev(mime_types);
579 image_loader_emit_size(il);
583 g_mutex_lock(il->data_mutex);
586 if (width > il->requested_width || height > il->requested_height)
589 if ((static_cast<gdouble>(il->requested_width) / width) < (static_cast<gdouble>(il->requested_height) / height))
591 nw = il->requested_width;
592 nh = static_cast<gdouble>(nw) / width * height;
597 nh = il->requested_height;
598 nw = static_cast<gdouble>(nh) / height * width;
602 il->actual_width = nw;
603 il->actual_height = nh;
604 il->backend.set_size(loader, nw, nh);
608 g_mutex_unlock(il->data_mutex);
609 image_loader_emit_size(il);
612 static void image_loader_stop_loader(ImageLoader *il)
618 /* some loaders do not have a pixbuf till close, order is important here */
619 il->backend.close(il->loader, il->error ? nullptr : &il->error); /* we are interested in the first error only */
620 image_loader_sync_pixbuf(il);
621 il->backend.free(il->loader);
622 il->loader = nullptr;
624 g_mutex_lock(il->data_mutex);
626 g_mutex_unlock(il->data_mutex);
629 static void image_loader_setup_loader(ImageLoader *il)
631 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
635 gint external_preview = 1;
637 g_mutex_lock(il->data_mutex);
639 if (options->external_preview.enable)
642 gchar *tilde_filename;
644 tilde_filename = expand_tilde(options->external_preview.select);
646 cmd_line = g_strdup_printf("\"%s\" \"%s\"" , tilde_filename, il->fd->path);
648 external_preview = runcmd(cmd_line);
650 g_free(tilde_filename);
653 if (external_preview == 0)
655 DEBUG_1("Using custom external loader");
656 image_loader_backend_set_external(&il->backend);
660 #ifdef HAVE_FFMPEGTHUMBNAILER
661 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
663 DEBUG_1("Using custom ffmpegthumbnailer loader");
664 image_loader_backend_set_ft(&il->backend);
669 if (il->bytes_total >= 4 &&
670 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
672 DEBUG_1("Using custom pdf loader");
673 image_loader_backend_set_pdf(&il->backend);
678 if (il->bytes_total >= 12 &&
679 ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
680 (memcmp(il->mapped_file + 4, "ftypheix", 8) == 0) ||
681 (memcmp(il->mapped_file + 4, "ftypmsf1", 8) == 0) ||
682 (memcmp(il->mapped_file + 4, "ftypmif1", 8) == 0) ||
683 (memcmp(il->mapped_file + 4, "ftypavif", 8) == 0)))
685 DEBUG_1("Using custom heif loader");
686 image_loader_backend_set_heif(&il->backend);
691 if (il->bytes_total >= 12 &&
692 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
693 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
695 DEBUG_1("Using custom webp loader");
696 image_loader_backend_set_webp(&il->backend);
701 if (il->bytes_total >= 16 &&
702 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
703 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
705 DEBUG_1("Using custom djvu loader");
706 image_loader_backend_set_djvu(&il->backend);
711 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
713 DEBUG_1("Using custom jpeg loader");
714 image_loader_backend_set_jpeg(&il->backend);
718 if (il->bytes_total >= 11 &&
719 (memcmp(il->mapped_file + 4, "ftypcrx", 7) == 0) &&
720 (memcmp(il->mapped_file + 64, "CanonCR3", 8) == 0))
722 DEBUG_1("Using custom cr3 loader");
723 image_loader_backend_set_cr3(&il->backend);
729 if (il->bytes_total >= 10 &&
730 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
731 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
732 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
733 memcmp(il->mapped_file, "II*\0", 4) == 0))
735 DEBUG_1("Using custom tiff loader");
736 image_loader_backend_set_tiff(&il->backend);
740 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
742 DEBUG_1("Using dds loader");
743 image_loader_backend_set_dds(&il->backend);
746 if (il->bytes_total >= 6 &&
747 (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
749 DEBUG_1("Using custom psd loader");
750 image_loader_backend_set_psd(&il->backend);
754 if (il->bytes_total >= 12 &&
755 (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
757 DEBUG_1("Using custom j2k loader");
758 image_loader_backend_set_j2k(&il->backend);
763 if (il->bytes_total >= 12 &&
764 (memcmp(il->mapped_file, "\0\0\0\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A", 12) == 0))
766 DEBUG_1("Using custom jpeg xl loader");
767 image_loader_backend_set_jpegxl(&il->backend);
770 if (il->bytes_total >= 2 &&
771 (memcmp(il->mapped_file, "\xFF\x0A", 2) == 0))
773 DEBUG_1("Using custom jpeg xl loader");
774 image_loader_backend_set_jpegxl(&il->backend);
778 if ((il->bytes_total == 6144 || il->bytes_total == 6912) &&
779 (file_extension_match(il->fd->path, ".scr")))
781 DEBUG_1("Using custom zxscr loader");
782 image_loader_backend_set_zxscr(&il->backend);
785 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
787 DEBUG_1("Using custom collection loader");
788 image_loader_backend_set_collection(&il->backend);
791 if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
793 DEBUG_1("Using custom svgz loader");
794 image_loader_backend_set_svgz(&il->backend);
797 image_loader_backend_set_default(&il->backend);
800 il->loader = static_cast<void **>(il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il));
803 format = il->backend.get_format_name(il->loader);
804 if (g_strcmp0(format, "tiff") == 0)
806 il->backend.set_page_num(il->loader, il->fd->page_num);
812 format = il->backend.get_format_name(il->loader);
813 if (g_strcmp0(format, "pdf") == 0)
815 il->backend.set_page_num(il->loader, il->fd->page_num);
821 format = il->backend.get_format_name(il->loader);
822 if (g_strcmp0(format, "heif") == 0)
824 il->backend.set_page_num(il->loader, il->fd->page_num);
830 format = il->backend.get_format_name(il->loader);
831 if (g_strcmp0(format, "djvu") == 0)
833 il->backend.set_page_num(il->loader, il->fd->page_num);
838 il->fd->format_name = il->backend.get_format_name(il->loader);
840 g_mutex_unlock(il->data_mutex);
844 static void image_loader_done(ImageLoader *il)
846 image_loader_stop_loader(il);
848 image_loader_emit_done(il);
851 static void image_loader_error(ImageLoader *il)
853 image_loader_stop_loader(il);
855 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
857 image_loader_emit_error(il);
860 static gboolean image_loader_continue(ImageLoader *il)
865 if (!il) return G_SOURCE_REMOVE;
867 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
868 while (c > 0 && !image_loader_get_stopping(il))
870 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
874 image_loader_done(il);
875 return G_SOURCE_REMOVE;
878 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
880 image_loader_error(il);
881 return G_SOURCE_REMOVE;
889 if (il->bytes_total > 0)
891 image_loader_emit_percent(il);
894 return G_SOURCE_CONTINUE;
897 static gboolean image_loader_begin(ImageLoader *il)
899 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
904 if (il->pixbuf) return FALSE;
906 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
907 if (b < 1) return FALSE;
909 image_loader_setup_loader(il);
911 g_assert(il->bytes_read == 0);
912 if (il->backend.load) {
914 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
916 image_loader_stop_loader(il);
920 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
922 image_loader_stop_loader(il);
927 format = il->backend.get_format_name(il->loader);
928 if (g_strcmp0(format, "pdf") == 0)
930 gint i = il->backend.get_page_total(il->loader);
931 file_data_set_page_total(il->fd, i);
936 format = il->backend.get_format_name(il->loader);
937 if (g_strcmp0(format, "heif") == 0)
939 gint i = il->backend.get_page_total(il->loader);
940 file_data_set_page_total(il->fd, i);
945 format = il->backend.get_format_name(il->loader);
946 if (g_strcmp0(format, "djvu") == 0)
948 gint i = il->backend.get_page_total(il->loader);
949 file_data_set_page_total(il->fd, i);
954 format = il->backend.get_format_name(il->loader);
955 if (g_strcmp0(format, "tiff") == 0)
957 gint i = il->backend.get_page_total(il->loader);
958 file_data_set_page_total(il->fd, i);
965 /* read until size is known */
966 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
968 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
969 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
971 image_loader_stop_loader(il);
976 if (!il->pixbuf) image_loader_sync_pixbuf(il);
978 if (il->bytes_read == il->bytes_total || b < 1)
980 /* done, handle (broken) loaders that do not have pixbuf till close */
981 image_loader_stop_loader(il);
983 if (!il->pixbuf) return FALSE;
985 image_loader_done(il);
991 image_loader_stop_loader(il);
998 /**************************************************************************************/
999 /* the following functions are always executed in the main thread */
1002 static gboolean image_loader_setup_source(ImageLoader *il)
1007 if (!il || il->loader || il->mapped_file) return FALSE;
1009 il->mapped_file = nullptr;
1013 ExifData *exif = exif_read_fd(il->fd);
1015 if (options->thumbnails.use_exif)
1017 il->mapped_file = exif_get_preview(exif, reinterpret_cast<guint *>(&il->bytes_total), il->requested_width, il->requested_height);
1019 if (il->mapped_file)
1021 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1026 il->mapped_file = libraw_get_preview(il, reinterpret_cast<guint *>(&il->bytes_total));
1028 if (il->mapped_file)
1030 /* Both exiv2 and libraw sometimes return a pointer to a file
1031 * section that is not a jpeg */
1032 if (il->mapped_file[0] != 0xFF || il->mapped_file[1] != 0xD8)
1034 il->mapped_file = nullptr;
1038 il->preview = IMAGE_LOADER_PREVIEW_LIBRAW;
1043 /* If libraw does not find a thumbnail, try exiv2 */
1044 if (!il->mapped_file)
1046 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*/
1048 if (il->mapped_file)
1050 /* Both exiv2 and libraw sometimes return a pointer to a file
1051 * section that is not a jpeg */
1052 if (il->mapped_file[0] != 0xFF || il->mapped_file[1] != 0xD8)
1054 il->mapped_file = nullptr;
1058 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1063 if (il->mapped_file)
1065 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
1067 exif_free_fd(il->fd, exif);
1071 if (!il->mapped_file)
1076 pathl = path_from_utf8(il->fd->path);
1077 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
1079 if (load_fd == -1) return FALSE;
1081 if (fstat(load_fd, &st) == 0)
1083 il->bytes_total = st.st_size;
1091 il->mapped_file = static_cast<guchar *>(mmap(nullptr, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0));
1093 if (il->mapped_file == MAP_FAILED)
1095 il->mapped_file = nullptr;
1098 il->preview = IMAGE_LOADER_PREVIEW_NONE;
1104 static void image_loader_stop_source(ImageLoader *il)
1108 if (il->mapped_file)
1110 if (il->preview == IMAGE_LOADER_PREVIEW_EXIF)
1112 exif_free_preview(il->mapped_file);
1114 else if (il->preview == IMAGE_LOADER_PREVIEW_LIBRAW)
1116 libraw_free_preview(il->mapped_file);
1120 munmap(il->mapped_file, il->bytes_total);
1122 il->mapped_file = nullptr;
1126 static void image_loader_stop(ImageLoader *il)
1132 g_source_remove(il->idle_id);
1138 /* stop loader in the other thread */
1139 g_mutex_lock(il->data_mutex);
1140 il->stopping = TRUE;
1141 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
1142 g_mutex_unlock(il->data_mutex);
1145 image_loader_stop_loader(il);
1146 image_loader_stop_source(il);
1150 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
1152 g_mutex_lock(il->data_mutex);
1153 il->delay_area_ready = enable;
1158 list = g_list_reverse(il->area_param_delayed_list);
1159 il->area_param_delayed_list = nullptr;
1160 g_mutex_unlock(il->data_mutex);
1166 auto par = static_cast<ImageLoaderAreaParam *>(work->data);
1169 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1171 g_list_free_full(list, g_free);
1176 g_mutex_unlock(il->data_mutex);
1181 /**************************************************************************************/
1182 /* execution via idle calls */
1184 static gboolean image_loader_idle_cb(gpointer data)
1186 gboolean ret = G_SOURCE_REMOVE;
1187 auto il = static_cast<ImageLoader *>(data);
1191 ret = image_loader_continue(il);
1196 image_loader_stop_source(il);
1202 static gboolean image_loader_start_idle(ImageLoader *il)
1206 if (!il) return FALSE;
1208 if (!il->fd) return FALSE;
1210 if (!image_loader_setup_source(il)) return FALSE;
1212 ret = image_loader_begin(il);
1214 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, nullptr);
1218 /**************************************************************************************/
1219 /* execution via thread */
1221 static GThreadPool *image_loader_thread_pool = nullptr;
1223 static GCond *image_loader_prio_cond = nullptr;
1224 static GMutex *image_loader_prio_mutex = nullptr;
1225 static gint image_loader_prio_num = 0;
1228 static void image_loader_thread_enter_high()
1230 g_mutex_lock(image_loader_prio_mutex);
1231 image_loader_prio_num++;
1232 g_mutex_unlock(image_loader_prio_mutex);
1235 static void image_loader_thread_leave_high()
1237 g_mutex_lock(image_loader_prio_mutex);
1238 image_loader_prio_num--;
1239 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1240 g_mutex_unlock(image_loader_prio_mutex);
1243 static void image_loader_thread_wait_high()
1245 g_mutex_lock(image_loader_prio_mutex);
1246 while (image_loader_prio_num)
1248 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1251 g_mutex_unlock(image_loader_prio_mutex);
1255 static void image_loader_thread_run(gpointer data, gpointer)
1257 auto il = static_cast<ImageLoader *>(data);
1261 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1263 /* low prio, wait until high prio tasks finishes */
1264 image_loader_thread_wait_high();
1269 image_loader_thread_enter_high();
1272 err = !image_loader_begin(il);
1277 loader failed, we have to send signal
1278 (idle mode returns the image_loader_begin return value directly)
1279 (success is always reported indirectly from image_loader_begin)
1281 image_loader_emit_error(il);
1286 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1288 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1290 /* low prio, wait until high prio tasks finishes */
1291 image_loader_thread_wait_high();
1293 cont = image_loader_continue(il);
1295 image_loader_stop_loader(il);
1297 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1300 image_loader_thread_leave_high();
1303 g_mutex_lock(il->data_mutex);
1304 il->can_destroy = TRUE;
1305 g_cond_signal(il->can_destroy_cond);
1306 g_mutex_unlock(il->data_mutex);
1311 static gboolean image_loader_start_thread(ImageLoader *il)
1313 if (!il) return FALSE;
1315 if (!il->fd) return FALSE;
1319 if (!image_loader_setup_source(il)) return FALSE;
1321 if (!image_loader_thread_pool)
1323 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, nullptr, -1, FALSE, nullptr);
1324 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1325 g_cond_init(image_loader_prio_cond);
1326 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1327 g_mutex_init(image_loader_prio_mutex);
1330 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1332 g_thread_pool_push(image_loader_thread_pool, il, nullptr);
1333 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1339 /**************************************************************************************/
1340 /* public interface */
1343 gboolean image_loader_start(ImageLoader *il)
1345 if (!il) return FALSE;
1347 if (!il->fd) return FALSE;
1349 return image_loader_start_thread(il);
1353 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1354 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1357 if (!il) return nullptr;
1359 g_mutex_lock(il->data_mutex);
1361 g_mutex_unlock(il->data_mutex);
1365 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1369 g_mutex_lock(il->data_mutex);
1370 il->requested_width = width;
1371 il->requested_height = height;
1372 g_mutex_unlock(il->data_mutex);
1375 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1379 g_mutex_lock(il->data_mutex);
1380 il->idle_read_loop_count = count ? count : 1;
1381 g_mutex_unlock(il->data_mutex);
1384 void image_loader_set_priority(ImageLoader *il, gint priority)
1388 if (il->thread) return; /* can't change prio if the thread already runs */
1389 il->idle_priority = priority;
1393 gdouble image_loader_get_percent(ImageLoader *il)
1396 if (!il) return 0.0;
1398 g_mutex_lock(il->data_mutex);
1399 if (il->bytes_total == 0)
1405 ret = static_cast<gdouble>(il->bytes_read) / il->bytes_total;
1407 g_mutex_unlock(il->data_mutex);
1411 gboolean image_loader_get_is_done(ImageLoader *il)
1414 if (!il) return FALSE;
1416 g_mutex_lock(il->data_mutex);
1418 g_mutex_unlock(il->data_mutex);
1423 FileData *image_loader_get_fd(ImageLoader *il)
1426 if (!il) return nullptr;
1428 g_mutex_lock(il->data_mutex);
1430 g_mutex_unlock(il->data_mutex);
1435 gboolean image_loader_get_shrunk(ImageLoader *il)
1438 if (!il) return FALSE;
1440 g_mutex_lock(il->data_mutex);
1442 g_mutex_unlock(il->data_mutex);
1446 const gchar *image_loader_get_error(ImageLoader *il)
1448 const gchar *ret = nullptr;
1449 if (!il) return nullptr;
1450 g_mutex_lock(il->data_mutex);
1451 if (il->error) ret = il->error->message;
1452 g_mutex_unlock(il->data_mutex);
1458 * @FIXME this can be rather slow and blocks until the size is known
1460 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1465 il = image_loader_new(fd);
1467 success = image_loader_start_idle(il);
1469 if (success && il->pixbuf)
1471 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1472 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1476 if (width) *width = -1;
1477 if (height) *height = -1;
1480 image_loader_free(il);
1484 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */