2 * Copyright (C) 2006 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.
25 #include "collect-table.h"
26 #include "color-man.h"
29 #include "history-list.h"
30 #include "image-load.h"
32 #include "layout-image.h"
33 #include "pixbuf-renderer.h"
34 #include "pixbuf-util.h"
35 #include "ui-fileops.h"
36 #include "filecache.h"
40 static GList *image_list = nullptr;
42 static void image_read_ahead_start(ImageWindow *imd);
43 static void image_cache_set(ImageWindow *imd, FileData *fd);
45 // For draw rectangle function
46 static gint pixbuf_start_x;
47 static gint pixbuf_start_y;
48 static gint image_start_x;
49 static gint image_start_y;
50 static gint rect_x1, rect_x2, rect_y1, rect_y2;
51 static gint rect_id = 0;
54 *-------------------------------------------------------------------
56 *-------------------------------------------------------------------
59 static void image_click_cb(PixbufRenderer *, GdkEventButton *event, gpointer data)
61 auto imd = static_cast<ImageWindow *>(data);
62 if (!options->image_lm_click_nav && event->button == MOUSE_BUTTON_MIDDLE)
64 imd->mouse_wheel_mode = !imd->mouse_wheel_mode;
69 imd->func_button(imd, event, imd->data_button);
73 static void switch_coords_orientation(ImageWindow *imd, gint x, gint y, gint width, gint height)
75 switch (imd->orientation)
77 case EXIF_ORIENTATION_TOP_LEFT:
78 /* normal -- nothing to do */
79 rect_x1 = image_start_x;
80 rect_y1 = image_start_y;
84 case EXIF_ORIENTATION_TOP_RIGHT:
87 rect_y1 = image_start_y;
88 rect_x2 = width - image_start_x;
91 case EXIF_ORIENTATION_BOTTOM_RIGHT:
95 rect_x2 = width - image_start_x;
96 rect_y2 = height - image_start_y;
98 case EXIF_ORIENTATION_BOTTOM_LEFT:
100 rect_x1 = image_start_x;
101 rect_y1 = height - y;
103 rect_y2 = height - image_start_y;
105 case EXIF_ORIENTATION_LEFT_TOP:
107 rect_x1 = image_start_y;
108 rect_y1 = image_start_x;
112 case EXIF_ORIENTATION_RIGHT_TOP:
113 /* rotated -90 (270) */
114 rect_x1 = image_start_y;
117 rect_y2 = width - image_start_x;
119 case EXIF_ORIENTATION_RIGHT_BOTTOM:
121 rect_x1 = height - y;
123 rect_x2 = height - image_start_y;
124 rect_y2 = width - image_start_x;
126 case EXIF_ORIENTATION_LEFT_BOTTOM:
128 rect_x1 = height - y;
129 rect_y1 = image_start_x;
130 rect_x2 = height - image_start_y;
134 /* The other values are out of range */
139 static void image_press_cb(PixbufRenderer *pr, GdkEventButton *event, gpointer data)
141 auto imd = static_cast<ImageWindow *>(data);
143 gint x_pixel, y_pixel;
145 if(options->draw_rectangle)
147 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
149 pixbuf_start_x = event->x;
150 pixbuf_start_y = event->y;
158 image_start_x = x_pixel;
167 image_start_y = y_pixel;
173 pixbuf_renderer_overlay_remove(reinterpret_cast<PixbufRenderer *>(imd->pr), rect_id);
176 lw = layout_find_by_image(imd);
182 if (lw && event->button == MOUSE_BUTTON_LEFT && event->type == GDK_2BUTTON_PRESS
183 && !options->image_lm_click_nav)
185 layout_image_full_screen_toggle(lw);
189 static void image_drag_cb(PixbufRenderer *pr, GdkEventMotion *event, gpointer data)
191 auto imd = static_cast<ImageWindow *>(data);
195 GdkPixbuf *rect_pixbuf;
196 gint x_pixel, y_pixel;
197 gint image_x_pixel, image_y_pixel;
199 if (options->draw_rectangle)
201 pixbuf_renderer_get_image_size(pr, &width, &height);
202 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
206 image_x_pixel = width;
210 image_x_pixel = x_pixel;
215 image_y_pixel = height;
219 image_y_pixel = y_pixel;
222 switch_coords_orientation(imd, image_x_pixel, image_y_pixel, width, height);
225 pixbuf_renderer_overlay_remove(reinterpret_cast<PixbufRenderer *>(imd->pr), rect_id);
228 rect_width = pr->drag_last_x - pixbuf_start_x;
233 rect_height = pr->drag_last_y - pixbuf_start_y;
234 if (rect_height <= 0)
239 rect_pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, rect_width, rect_height);
240 pixbuf_set_rect_fill(rect_pixbuf, 0, 0, rect_width, rect_height, 255, 255, 255, 0);
241 pixbuf_set_rect(rect_pixbuf, 1, 1, rect_width-2, rect_height - 2, 0, 0, 0, 255, 1, 1, 1, 1);
242 pixbuf_set_rect(rect_pixbuf, 2, 2, rect_width-4, rect_height - 4, 255, 255, 255, 255, 1, 1, 1, 1);
244 rect_id = pixbuf_renderer_overlay_add(reinterpret_cast<PixbufRenderer *>(imd->pr), rect_pixbuf, pixbuf_start_x, pixbuf_start_y, OVL_NORMAL);
247 pixbuf_renderer_get_scaled_size(pr, &width, &height);
251 imd->func_drag(imd, event,
252 static_cast<gfloat>(pr->drag_last_x - event->x) / width,
253 static_cast<gfloat>(pr->drag_last_y - event->y) / height,
258 static void image_scroll_notify_cb(PixbufRenderer *pr, gpointer data)
260 auto imd = static_cast<ImageWindow *>(data);
262 if (imd->func_scroll_notify && pr->scale)
264 imd->func_scroll_notify(imd,
265 static_cast<gint>(static_cast<gdouble>(pr->x_scroll) / pr->scale),
266 static_cast<gint>(static_cast<gdouble>(pr->y_scroll) / pr->scale),
267 static_cast<gint>(static_cast<gdouble>(pr->image_width) - pr->vis_width / pr->scale),
268 static_cast<gint>(static_cast<gdouble>(pr->image_height) - pr->vis_height / pr->scale),
269 imd->data_scroll_notify);
273 static void image_update_util(ImageWindow *imd)
275 if (imd->func_update) imd->func_update(imd, imd->data_update);
279 static void image_complete_util(ImageWindow *imd, gboolean preload)
281 if (imd->il && image_get_pixbuf(imd) != image_loader_get_pixbuf(imd->il)) return;
283 DEBUG_1("%s image load completed \"%s\" (%s)", get_exec_time(),
284 (preload) ? (imd->read_ahead_fd ? imd->read_ahead_fd->path : "null") :
285 (imd->image_fd ? imd->image_fd->path : "null"),
286 (preload) ? "preload" : "current");
288 if (!preload) imd->completed = TRUE;
289 if (imd->func_complete) imd->func_complete(imd, preload, imd->data_complete);
292 static void image_render_complete_cb(PixbufRenderer *, gpointer data)
294 auto imd = static_cast<ImageWindow *>(data);
296 image_complete_util(imd, FALSE);
299 static void image_state_set(ImageWindow *imd, ImageState state)
301 if (state == IMAGE_STATE_NONE)
307 imd->state = static_cast<ImageState>(imd->state | state);
309 if (imd->func_state) imd->func_state(imd, state, imd->data_state);
312 static void image_state_unset(ImageWindow *imd, ImageState state)
314 imd->state = static_cast<ImageState>(imd->state & ~state);
315 if (imd->func_state) imd->func_state(imd, state, imd->data_state);
318 static void image_zoom_cb(PixbufRenderer *, gdouble, gpointer data)
320 auto imd = static_cast<ImageWindow *>(data);
322 if (imd->title_show_zoom) image_update_title(imd);
323 image_state_set(imd, IMAGE_STATE_IMAGE);
324 image_update_util(imd);
328 *-------------------------------------------------------------------
330 *-------------------------------------------------------------------
333 void image_update_title(ImageWindow *imd)
335 gchar *title = nullptr;
336 gchar *zoom = nullptr;
337 gchar *collection = nullptr;
339 gchar *lw_ident = nullptr;
341 if (!imd->top_window) return;
343 if (imd->collection && collection_to_number(imd->collection) >= 0)
345 const gchar *name = imd->collection->name;
346 if (!name) name = _("Untitled");
347 collection = g_strdup_printf(_(" (Collection %s)"), name);
350 if (imd->title_show_zoom)
352 gchar *buf = image_zoom_get_as_text(imd);
353 zoom = g_strconcat(" [", buf, "]", NULL);
357 lw = layout_find_by_image(imd);
360 lw_ident = g_strconcat(" (", lw->options.id, ")", NULL);
363 title = g_strdup_printf("%s%s%s%s%s%s%s",
364 imd->title ? imd->title : "",
365 imd->image_fd ? imd->image_fd->name : "",
367 collection ? collection : "",
368 imd->image_fd ? " - " : "",
369 imd->title_right ? imd->title_right : "",
370 options->show_window_ids ? (lw_ident ? lw_ident : "") : ""
377 gtk_window_set_title(GTK_WINDOW(imd->top_window), title);
385 *-------------------------------------------------------------------
386 * rotation, flip, etc.
387 *-------------------------------------------------------------------
389 static gboolean image_get_x11_screen_profile(ImageWindow *imd, guchar **screen_profile, gint *screen_profile_len)
391 GdkScreen *screen = gtk_widget_get_screen(imd->widget);;
392 GdkAtom type = GDK_NONE;
395 return (gdk_property_get(gdk_screen_get_root_window(screen),
396 gdk_atom_intern ("_ICC_PROFILE", FALSE),
398 0, 64 * 1024 * 1024, FALSE,
399 &type, &format, screen_profile_len, screen_profile) && *screen_profile_len > 0);
402 static gboolean image_post_process_color(ImageWindow *imd, gint start_row, gboolean run_in_bg)
405 ColorManProfileType input_type;
406 ColorManProfileType screen_type;
407 const gchar *input_file = nullptr;
408 const gchar *screen_file = nullptr;
409 guchar *profile = nullptr;
411 guchar *screen_profile = nullptr;
412 gint screen_profile_len;
415 if (imd->cm) return FALSE;
417 if (imd->color_profile_input >= COLOR_PROFILE_FILE &&
418 imd->color_profile_input < COLOR_PROFILE_FILE + COLOR_PROFILE_INPUTS)
420 const gchar *file = options->color_profile.input_file[imd->color_profile_input - COLOR_PROFILE_FILE];
422 if (!is_readable_file(file)) return FALSE;
424 input_type = COLOR_PROFILE_FILE;
427 else if (imd->color_profile_input >= COLOR_PROFILE_SRGB &&
428 imd->color_profile_input < COLOR_PROFILE_FILE)
430 input_type = static_cast<ColorManProfileType>(imd->color_profile_input);
431 input_file = nullptr;
438 if (options->color_profile.use_x11_screen_profile &&
439 image_get_x11_screen_profile(imd, &screen_profile, &screen_profile_len))
441 screen_type = COLOR_PROFILE_MEM;
442 DEBUG_1("Using X11 screen profile, length: %d", screen_profile_len);
444 else if (options->color_profile.screen_file &&
445 is_readable_file(options->color_profile.screen_file))
447 screen_type = COLOR_PROFILE_FILE;
448 screen_file = options->color_profile.screen_file;
452 screen_type = COLOR_PROFILE_SRGB;
453 screen_file = nullptr;
457 imd->color_profile_from_image = COLOR_PROFILE_NONE;
459 exif = exif_read_fd(imd->image_fd);
463 if (g_strcmp0(imd->image_fd->format_name, "heif") == 0)
465 profile = heif_color_profile(imd->image_fd, &profile_len);
470 profile = exif_get_color_profile(exif, &profile_len);
475 if (!imd->color_profile_use_image)
480 DEBUG_1("Found embedded color profile");
481 imd->color_profile_from_image = COLOR_PROFILE_MEM;
485 gchar *interop_index = exif_get_data_as_text(exif, "Exif.Iop.InteroperabilityIndex");
489 /* Exif 2.21 specification */
490 if (!strcmp(interop_index, "R98"))
492 imd->color_profile_from_image = COLOR_PROFILE_SRGB;
493 DEBUG_1("Found EXIF 2.21 ColorSpace of sRGB");
495 else if (!strcmp(interop_index, "R03"))
497 imd->color_profile_from_image = COLOR_PROFILE_ADOBERGB;
498 DEBUG_1("Found EXIF 2.21 ColorSpace of AdobeRGB");
500 g_free(interop_index);
506 /* ColorSpace == 1 specifies sRGB per EXIF 2.2 */
507 if (!exif_get_integer(exif, "Exif.Photo.ColorSpace", &cs)) cs = 0;
510 imd->color_profile_from_image = COLOR_PROFILE_SRGB;
511 DEBUG_1("Found EXIF 2.2 ColorSpace of sRGB");
515 /* non-standard way of specifying AdobeRGB (used by some software) */
516 imd->color_profile_from_image = COLOR_PROFILE_ADOBERGB;
517 DEBUG_1("Found EXIF 2.2 ColorSpace of AdobeRGB");
521 if (imd->color_profile_use_image && imd->color_profile_from_image != COLOR_PROFILE_NONE)
523 input_type = static_cast<ColorManProfileType>(imd->color_profile_from_image);
524 input_file = nullptr;
528 exif_free_fd(imd->image_fd, exif);
534 cm = color_man_new_embedded(run_in_bg ? imd : nullptr, nullptr,
535 profile, profile_len,
536 screen_type, screen_file, screen_profile, screen_profile_len);
541 cm = color_man_new(run_in_bg ? imd : nullptr, nullptr,
542 input_type, input_file,
543 screen_type, screen_file, screen_profile, screen_profile_len);
551 cm->incremental_sync = TRUE;
557 image_update_util(imd);
561 g_free(screen_profile);
562 screen_profile = nullptr;
569 static void image_post_process_tile_color_cb(PixbufRenderer *, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h, gpointer data)
571 auto imd = static_cast<ImageWindow *>(data);
572 if (imd->cm) color_man_correct_region(static_cast<ColorMan *>(imd->cm), *pixbuf, x, y, w, h);
573 if (imd->desaturate) pixbuf_desaturate_rect(*pixbuf, x, y, w, h);
574 if (imd->overunderexposed) pixbuf_highlight_overunderexposed(*pixbuf, x, y, w, h);
577 void image_alter_orientation(ImageWindow *imd, FileData *fd_n, AlterType type)
579 static const gint rotate_90[] = {1, 6, 7, 8, 5, 2, 3, 4, 1};
580 static const gint rotate_90_cc[] = {1, 8, 5, 6, 7, 4, 1, 2, 3};
581 static const gint rotate_180[] = {1, 3, 4, 1, 2, 7, 8, 5, 6};
582 static const gint mirror[] = {1, 2, 1, 4, 3, 6, 5, 8, 7};
583 static const gint flip[] = {1, 4, 3, 2, 1, 8, 7, 6, 5};
587 if (!imd || !imd->pr || !imd->image_fd || !fd_n) return;
589 orientation = EXIF_ORIENTATION_TOP_LEFT;
591 if (fd_n->user_orientation)
593 orientation = fd_n->user_orientation;
596 if (options->metadata.write_orientation)
598 if (g_strcmp0(imd->image_fd->format_name, "heif") == 0)
600 orientation = EXIF_ORIENTATION_TOP_LEFT;
604 orientation = metadata_read_int(fd_n, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
611 case ALTER_ROTATE_90:
612 orientation = rotate_90[orientation];
614 case ALTER_ROTATE_90_CC:
615 orientation = rotate_90_cc[orientation];
617 case ALTER_ROTATE_180:
618 orientation = rotate_180[orientation];
621 orientation = mirror[orientation];
624 orientation = flip[orientation];
627 orientation = fd_n->exif_orientation ? fd_n->exif_orientation : 1;
634 if (orientation != (fd_n->exif_orientation ? fd_n->exif_orientation : 1))
636 if (g_strcmp0(fd_n->format_name, "heif") != 0)
638 if (!options->metadata.write_orientation)
640 /* user_orientation does not work together with options->metadata.write_orientation,
641 use either one or the other.
642 we must however handle switching metadata.write_orientation on and off, therefore
643 we just disable referencing new fd's, not unreferencing the old ones
645 if (fd_n->user_orientation == 0) file_data_ref(fd_n);
646 fd_n->user_orientation = orientation;
651 if (fd_n->user_orientation == 0) file_data_ref(fd_n);
652 fd_n->user_orientation = orientation;
657 if (fd_n->user_orientation != 0) file_data_unref(fd_n);
658 fd_n->user_orientation = 0;
661 if (g_strcmp0(fd_n->format_name, "heif") != 0)
663 if (options->metadata.write_orientation)
665 if (type == ALTER_NONE)
667 metadata_write_revert(fd_n, ORIENTATION_KEY);
671 metadata_write_int(fd_n, ORIENTATION_KEY, orientation);
676 if (imd->image_fd == fd_n && (!options->metadata.write_orientation || options->image.exif_rotate_enable))
678 imd->orientation = orientation;
679 pixbuf_renderer_set_orientation(reinterpret_cast<PixbufRenderer *>(imd->pr), orientation);
683 void image_set_desaturate(ImageWindow *imd, gboolean desaturate)
685 imd->desaturate = desaturate;
686 if (imd->cm || imd->desaturate || imd->overunderexposed)
687 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), image_post_process_tile_color_cb, imd, (imd->cm != nullptr) );
689 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), nullptr, nullptr, TRUE);
690 pixbuf_renderer_set_orientation(reinterpret_cast<PixbufRenderer *>(imd->pr), imd->orientation);
693 gboolean image_get_desaturate(ImageWindow *imd)
695 return imd->desaturate;
698 void image_set_overunderexposed(ImageWindow *imd, gboolean overunderexposed)
700 imd->overunderexposed = overunderexposed;
701 if (imd->cm || imd->desaturate || imd->overunderexposed)
702 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), image_post_process_tile_color_cb, imd, (imd->cm != nullptr) );
704 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), nullptr, nullptr, TRUE);
705 pixbuf_renderer_set_orientation(reinterpret_cast<PixbufRenderer *>(imd->pr), imd->orientation);
708 void image_set_ignore_alpha(ImageWindow *imd, gboolean ignore_alpha)
710 pixbuf_renderer_set_ignore_alpha(reinterpret_cast<PixbufRenderer *>(imd->pr), ignore_alpha);
714 *-------------------------------------------------------------------
715 * read ahead (prebuffer)
716 *-------------------------------------------------------------------
719 static void image_read_ahead_cancel(ImageWindow *imd)
721 DEBUG_1("%s read ahead cancelled for :%s", get_exec_time(), imd->read_ahead_fd ? imd->read_ahead_fd->path : "null");
723 image_loader_free(imd->read_ahead_il);
724 imd->read_ahead_il = nullptr;
726 file_data_unref(imd->read_ahead_fd);
727 imd->read_ahead_fd = nullptr;
730 static void image_read_ahead_done_cb(ImageLoader *, gpointer data)
732 auto imd = static_cast<ImageWindow *>(data);
734 if (!imd->read_ahead_fd || !imd->read_ahead_il) return;
736 DEBUG_1("%s read ahead done for :%s", get_exec_time(), imd->read_ahead_fd->path);
738 if (!imd->read_ahead_fd->pixbuf)
740 imd->read_ahead_fd->pixbuf = image_loader_get_pixbuf(imd->read_ahead_il);
741 if (imd->read_ahead_fd->pixbuf)
743 g_object_ref(imd->read_ahead_fd->pixbuf);
744 image_cache_set(imd, imd->read_ahead_fd);
747 image_loader_free(imd->read_ahead_il);
748 imd->read_ahead_il = nullptr;
750 image_complete_util(imd, TRUE);
753 static void image_read_ahead_error_cb(ImageLoader *il, gpointer data)
755 /* we even treat errors as success, maybe at least some of the file was ok */
756 image_read_ahead_done_cb(il, data);
759 static void image_read_ahead_start(ImageWindow *imd)
761 /* already started ? */
762 if (!imd->read_ahead_fd || imd->read_ahead_il || imd->read_ahead_fd->pixbuf) return;
764 /* still loading ?, do later */
765 if (imd->il /*|| imd->cm*/) return;
767 DEBUG_1("%s read ahead started for :%s", get_exec_time(), imd->read_ahead_fd->path);
769 imd->read_ahead_il = image_loader_new(imd->read_ahead_fd);
771 image_loader_delay_area_ready(imd->read_ahead_il, TRUE); /* we will need the area_ready signals later */
773 g_signal_connect(G_OBJECT(imd->read_ahead_il), "error", (GCallback)image_read_ahead_error_cb, imd);
774 g_signal_connect(G_OBJECT(imd->read_ahead_il), "done", (GCallback)image_read_ahead_done_cb, imd);
776 if (!image_loader_start(imd->read_ahead_il))
778 image_read_ahead_cancel(imd);
779 image_complete_util(imd, TRUE);
783 static void image_read_ahead_set(ImageWindow *imd, FileData *fd)
785 if (imd->read_ahead_fd && fd && imd->read_ahead_fd == fd) return;
787 image_read_ahead_cancel(imd);
789 imd->read_ahead_fd = file_data_ref(fd);
791 DEBUG_1("read ahead set to :%s", imd->read_ahead_fd->path);
793 image_read_ahead_start(imd);
797 *-------------------------------------------------------------------
799 *-------------------------------------------------------------------
802 static void image_cache_release_cb(FileData *fd)
804 g_object_unref(fd->pixbuf);
805 fd->pixbuf = nullptr;
808 static FileCacheData *image_get_cache()
810 static FileCacheData *cache = nullptr;
811 if (!cache) cache = file_cache_new(image_cache_release_cb, 1);
812 file_cache_set_max_size(cache, static_cast<gulong>(options->image.image_cache_max) * 1048576); /* update from options */
816 static void image_cache_set(ImageWindow *, FileData *fd)
818 g_assert(fd->pixbuf);
820 file_cache_put(image_get_cache(), fd, static_cast<gulong>(gdk_pixbuf_get_rowstride(fd->pixbuf)) * static_cast<gulong>(gdk_pixbuf_get_height(fd->pixbuf)));
821 file_data_send_notification(fd, NOTIFY_PIXBUF); /* to update histogram */
824 static gint image_cache_get(ImageWindow *imd)
828 success = file_cache_get(image_get_cache(), imd->image_fd);
831 g_assert(imd->image_fd->pixbuf);
832 image_change_pixbuf(imd, imd->image_fd->pixbuf, image_zoom_get(imd), FALSE);
839 *-------------------------------------------------------------------
841 *-------------------------------------------------------------------
844 static void image_load_pixbuf_ready(ImageWindow *imd)
846 if (image_get_pixbuf(imd) || !imd->il) return;
848 image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), FALSE);
851 static void image_load_area_cb(ImageLoader *il, guint x, guint y, guint w, guint h, gpointer data)
853 auto imd = static_cast<ImageWindow *>(data);
856 pr = reinterpret_cast<PixbufRenderer *>(imd->pr);
858 if (imd->delay_flip &&
859 pr->pixbuf != image_loader_get_pixbuf(il))
864 if (!pr->pixbuf) image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), TRUE);
866 pixbuf_renderer_area_changed(pr, x, y, w, h);
869 static void image_load_done_cb(ImageLoader *, gpointer data)
871 auto imd = static_cast<ImageWindow *>(data);
873 DEBUG_1("%s image done", get_exec_time());
875 if (options->image.enable_read_ahead && imd->image_fd && !imd->image_fd->pixbuf && image_loader_get_pixbuf(imd->il))
877 imd->image_fd->pixbuf = static_cast<GdkPixbuf*>(g_object_ref(image_loader_get_pixbuf(imd->il)));
878 image_cache_set(imd, imd->image_fd);
880 /* call the callback triggered by image_state after fd->pixbuf is set */
881 g_object_set(G_OBJECT(imd->pr), "loading", FALSE, NULL);
882 image_state_unset(imd, IMAGE_STATE_LOADING);
884 if (!image_loader_get_pixbuf(imd->il))
886 GdkPixbuf *pixbuf = pixbuf_fallback(imd->image_fd, 0, 0);
888 image_change_pixbuf(imd, pixbuf, image_zoom_get(imd), FALSE);
889 g_object_unref(pixbuf);
893 else if (imd->delay_flip &&
894 image_get_pixbuf(imd) != image_loader_get_pixbuf(imd->il))
896 g_object_set(G_OBJECT(imd->pr), "complete", FALSE, NULL);
897 image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), FALSE);
900 image_loader_free(imd->il);
903 image_read_ahead_start(imd);
906 static void image_load_size_cb(ImageLoader *, guint width, guint height, gpointer data)
908 auto imd = static_cast<ImageWindow *>(data);
910 DEBUG_1("image_load_size_cb: %dx%d", width, height);
911 pixbuf_renderer_set_size_early(reinterpret_cast<PixbufRenderer *>(imd->pr), width, height);
914 static void image_load_error_cb(ImageLoader *il, gpointer data)
916 DEBUG_1("%s image error", get_exec_time());
918 /* even on error handle it like it was done,
919 * since we have a pixbuf with _something_ */
921 image_load_done_cb(il, data);
924 static void image_load_set_signals(ImageWindow *imd, gboolean override_old_signals)
927 if (override_old_signals)
929 /* override the old signals */
930 g_signal_handlers_disconnect_matched(G_OBJECT(imd->il), G_SIGNAL_MATCH_DATA, 0, 0, nullptr, nullptr, imd);
933 g_signal_connect(G_OBJECT(imd->il), "area_ready", (GCallback)image_load_area_cb, imd);
934 g_signal_connect(G_OBJECT(imd->il), "error", (GCallback)image_load_error_cb, imd);
935 g_signal_connect(G_OBJECT(imd->il), "done", (GCallback)image_load_done_cb, imd);
936 g_signal_connect(G_OBJECT(imd->il), "size_prepared", (GCallback)image_load_size_cb, imd);
939 /* this read ahead is located here merely for the callbacks, above */
941 static gboolean image_read_ahead_check(ImageWindow *imd)
943 if (!imd->read_ahead_fd) return FALSE;
944 if (imd->il) return FALSE;
946 if (!imd->image_fd || imd->read_ahead_fd != imd->image_fd)
948 image_read_ahead_cancel(imd);
952 if (imd->read_ahead_il)
954 imd->il = imd->read_ahead_il;
955 imd->read_ahead_il = nullptr;
957 image_load_set_signals(imd, TRUE);
959 g_object_set(G_OBJECT(imd->pr), "loading", TRUE, NULL);
960 image_state_set(imd, IMAGE_STATE_LOADING);
962 if (!imd->delay_flip)
964 image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), TRUE);
967 image_loader_delay_area_ready(imd->il, FALSE); /* send the delayed area_ready signals */
969 file_data_unref(imd->read_ahead_fd);
970 imd->read_ahead_fd = nullptr;
973 else if (imd->read_ahead_fd->pixbuf)
975 image_change_pixbuf(imd, imd->read_ahead_fd->pixbuf, image_zoom_get(imd), FALSE);
977 file_data_unref(imd->read_ahead_fd);
978 imd->read_ahead_fd = nullptr;
983 image_read_ahead_cancel(imd);
987 static gboolean image_load_begin(ImageWindow *imd, FileData *fd)
989 DEBUG_1("%s image begin", get_exec_time());
991 if (imd->il) return FALSE;
993 imd->completed = FALSE;
994 g_object_set(G_OBJECT(imd->pr), "complete", FALSE, NULL);
996 if (image_cache_get(imd))
998 DEBUG_1("from cache: %s", imd->image_fd->path);
1002 if (image_read_ahead_check(imd))
1004 DEBUG_1("from read ahead buffer: %s", imd->image_fd->path);
1008 if (!imd->delay_flip && image_get_pixbuf(imd))
1012 pr = PIXBUF_RENDERER(imd->pr);
1013 if (pr->pixbuf) g_object_unref(pr->pixbuf);
1014 pr->pixbuf = nullptr;
1017 g_object_set(G_OBJECT(imd->pr), "loading", TRUE, NULL);
1019 imd->il = image_loader_new(fd);
1021 image_load_set_signals(imd, FALSE);
1023 if (!image_loader_start(imd->il))
1025 DEBUG_1("image start error");
1027 g_object_set(G_OBJECT(imd->pr), "loading", FALSE, NULL);
1029 image_loader_free(imd->il);
1032 image_complete_util(imd, FALSE);
1037 image_state_set(imd, IMAGE_STATE_LOADING);
1040 if (!imd->delay_flip && !image_get_pixbuf(imd) && image_loader_get_pixbuf(imd->il))
1042 image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), TRUE);
1048 static void image_reset(ImageWindow *imd)
1050 /* stops anything currently being done */
1052 DEBUG_1("%s image reset", get_exec_time());
1054 g_object_set(G_OBJECT(imd->pr), "loading", FALSE, NULL);
1056 image_loader_free(imd->il);
1059 color_man_free(static_cast<ColorMan *>(imd->cm));
1062 imd->delay_alter_type = ALTER_NONE;
1064 image_state_set(imd, IMAGE_STATE_NONE);
1068 *-------------------------------------------------------------------
1070 *-------------------------------------------------------------------
1073 static void image_change_complete(ImageWindow *imd, gdouble zoom)
1076 imd->unknown = TRUE;
1078 /** @FIXME Might be improved when the wepb animation changes happen */
1079 g_object_set(G_OBJECT(imd->pr), "zoom_2pass", options->image.zoom_2pass, NULL);
1080 g_object_set(G_OBJECT(imd->pr), "zoom_quality", options->image.zoom_quality, NULL);
1084 image_change_pixbuf(imd, nullptr, zoom, FALSE);
1089 if (is_readable_file(imd->image_fd->path))
1093 pr = PIXBUF_RENDERER(imd->pr);
1094 pr->zoom = zoom; /* store the zoom, needed by the loader */
1096 /* Disable 2-pass for GIFs. Animated GIFs can flicker when enabled
1097 * Reduce quality to worst but fastest to avoid dropped frames */
1098 if (g_ascii_strcasecmp(imd->image_fd->extension, ".GIF") == 0)
1100 g_object_set(G_OBJECT(imd->pr), "zoom_2pass", FALSE, NULL);
1101 g_object_set(G_OBJECT(imd->pr), "zoom_quality", GDK_INTERP_NEAREST, NULL);
1105 if (image_load_begin(imd, imd->image_fd))
1107 imd->unknown = FALSE;
1111 if (imd->unknown == TRUE)
1115 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
1116 image_change_pixbuf(imd, pixbuf, zoom, FALSE);
1117 g_object_unref(pixbuf);
1121 image_update_util(imd);
1124 static void image_change_real(ImageWindow *imd, FileData *fd,
1125 CollectionData *cd, CollectInfo *info, gdouble zoom)
1128 imd->collection = cd;
1129 imd->collection_info = info;
1131 if (imd->auto_refresh && imd->image_fd)
1132 file_data_unregister_real_time_monitor(imd->image_fd);
1134 file_data_unref(imd->image_fd);
1135 imd->image_fd = file_data_ref(fd);
1138 image_change_complete(imd, zoom);
1140 image_update_title(imd);
1141 image_state_set(imd, IMAGE_STATE_IMAGE);
1143 if (imd->auto_refresh && imd->image_fd)
1144 file_data_register_real_time_monitor(imd->image_fd);
1148 *-------------------------------------------------------------------
1150 *-------------------------------------------------------------------
1153 static gboolean image_focus_in_cb(GtkWidget *, GdkEventFocus *, gpointer data)
1155 auto imd = static_cast<ImageWindow *>(data);
1157 if (imd->func_focus_in)
1159 imd->func_focus_in(imd, imd->data_focus_in);
1165 static gboolean image_scroll_cb(GtkWidget *, GdkEventScroll *event, gpointer data)
1167 auto imd = static_cast<ImageWindow *>(data);
1168 gboolean in_lw = FALSE;
1170 LayoutWindow *lw = nullptr;
1172 if (imd->func_scroll && event && event->type == GDK_SCROLL)
1175 /* check if the image is in a layout window */
1176 for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1178 if (imd == lw->split_images[i])
1187 if (lw->options.split_pane_sync)
1189 for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1191 if (lw->split_images[i])
1193 layout_image_activate(lw, i, FALSE);
1194 imd->func_scroll(lw->split_images[i], event, lw->split_images[i]->data_scroll);
1200 imd->func_scroll(imd, event, imd->data_scroll);
1206 imd->func_scroll(imd, event, imd->data_scroll);
1215 *-------------------------------------------------------------------
1217 *-------------------------------------------------------------------
1220 void image_attach_window(ImageWindow *imd, GtkWidget *window,
1221 const gchar *title, const gchar *title_right, gboolean show_zoom)
1225 imd->top_window = window;
1227 imd->title = g_strdup(title);
1228 g_free(imd->title_right);
1229 imd->title_right = g_strdup(title_right);
1230 imd->title_show_zoom = show_zoom;
1232 lw = layout_find_by_image(imd);
1234 if (!(options->image.fit_window_to_image && lw && (lw->options.tools_float || lw->options.tools_hidden))) window = nullptr;
1236 pixbuf_renderer_set_parent(reinterpret_cast<PixbufRenderer *>(imd->pr), reinterpret_cast<GtkWindow *>(window));
1238 image_update_title(imd);
1241 void image_set_update_func(ImageWindow *imd,
1242 void (*func)(ImageWindow *imd, gpointer data),
1245 imd->func_update = func;
1246 imd->data_update = data;
1249 void image_set_complete_func(ImageWindow *imd,
1250 void (*func)(ImageWindow *imd, gboolean preload, gpointer data),
1253 imd->func_complete = func;
1254 imd->data_complete = data;
1257 void image_set_state_func(ImageWindow *imd,
1258 void (*func)(ImageWindow *imd, ImageState state, gpointer data),
1261 imd->func_state = func;
1262 imd->data_state = data;
1266 void image_set_button_func(ImageWindow *imd,
1267 void (*func)(ImageWindow *, GdkEventButton *event, gpointer),
1270 imd->func_button = func;
1271 imd->data_button = data;
1274 void image_set_drag_func(ImageWindow *imd,
1275 void (*func)(ImageWindow *, GdkEventMotion *event, gdouble dx, gdouble dy, gpointer),
1278 imd->func_drag = func;
1279 imd->data_drag = data;
1282 void image_set_scroll_func(ImageWindow *imd,
1283 void (*func)(ImageWindow *, GdkEventScroll *event, gpointer),
1286 imd->func_scroll = func;
1287 imd->data_scroll = data;
1290 #pragma GCC diagnostic push
1291 #pragma GCC diagnostic ignored "-Wunused-function"
1292 void image_set_scroll_notify_func_unused(ImageWindow *imd,
1293 void (*func)(ImageWindow *imd, gint x, gint y, gint width, gint height, gpointer data),
1296 imd->func_scroll_notify = func;
1297 imd->data_scroll_notify = data;
1299 #pragma GCC diagnostic pop
1301 void image_set_focus_in_func(ImageWindow *imd,
1302 void (*func)(ImageWindow *, gpointer),
1305 imd->func_focus_in = func;
1306 imd->data_focus_in = data;
1311 const gchar *image_get_path(ImageWindow *imd)
1313 if (imd->image_fd == nullptr) return nullptr;
1314 return imd->image_fd->path;
1317 const gchar *image_get_name(ImageWindow *imd)
1319 if (imd->image_fd == nullptr) return nullptr;
1320 return imd->image_fd->name;
1323 FileData *image_get_fd(ImageWindow *imd)
1325 return imd->image_fd;
1328 /* merely changes path string, does not change the image! */
1329 void image_set_fd(ImageWindow *imd, FileData *fd)
1331 if (imd->auto_refresh && imd->image_fd)
1332 file_data_unregister_real_time_monitor(imd->image_fd);
1334 file_data_unref(imd->image_fd);
1335 imd->image_fd = file_data_ref(fd);
1337 image_update_title(imd);
1338 image_state_set(imd, IMAGE_STATE_IMAGE);
1340 if (imd->auto_refresh && imd->image_fd)
1341 file_data_register_real_time_monitor(imd->image_fd);
1344 /* load a new image */
1346 void image_change_fd(ImageWindow *imd, FileData *fd, gdouble zoom)
1348 if (imd->image_fd == fd) return;
1350 image_change_real(imd, fd, nullptr, nullptr, zoom);
1353 gboolean image_get_image_size(ImageWindow *imd, gint *width, gint *height)
1355 return pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), width, height);
1358 GdkPixbuf *image_get_pixbuf(ImageWindow *imd)
1360 return pixbuf_renderer_get_pixbuf(reinterpret_cast<PixbufRenderer *>(imd->pr));
1363 void image_change_pixbuf(ImageWindow *imd, GdkPixbuf *pixbuf, gdouble zoom, gboolean lazy)
1366 StereoPixbufData stereo_data = STEREO_PIXBUF_DEFAULT;
1367 /* read_exif and similar functions can actually notice that the file has changed and trigger
1368 a notification that removes the pixbuf from cache and unrefs it. Therefore we must ref it
1369 here before it is taken over by the renderer. */
1370 if (pixbuf) g_object_ref(pixbuf);
1372 imd->orientation = EXIF_ORIENTATION_TOP_LEFT;
1375 if (imd->image_fd->user_orientation)
1377 imd->orientation = imd->image_fd->user_orientation;
1379 else if (options->image.exif_rotate_enable)
1381 if (g_strcmp0(imd->image_fd->format_name, "heif") == 0)
1383 imd->orientation = EXIF_ORIENTATION_TOP_LEFT;
1384 imd->image_fd->exif_orientation = imd->orientation;
1388 imd->orientation = metadata_read_int(imd->image_fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
1389 imd->image_fd->exif_orientation = imd->orientation;
1396 stereo_data = static_cast<StereoPixbufData>(imd->user_stereo);
1397 if (stereo_data == STEREO_PIXBUF_DEFAULT)
1399 stereo_data = static_cast<StereoPixbufData>(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(pixbuf), "stereo_data")));
1403 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), nullptr, nullptr, FALSE);
1406 color_man_free(static_cast<ColorMan *>(imd->cm));
1412 pixbuf_renderer_set_pixbuf_lazy(reinterpret_cast<PixbufRenderer *>(imd->pr), pixbuf, zoom, imd->orientation, stereo_data);
1416 pixbuf_renderer_set_pixbuf(reinterpret_cast<PixbufRenderer *>(imd->pr), pixbuf, zoom);
1417 pixbuf_renderer_set_orientation(reinterpret_cast<PixbufRenderer *>(imd->pr), imd->orientation);
1418 pixbuf_renderer_set_stereo_data(reinterpret_cast<PixbufRenderer *>(imd->pr), stereo_data);
1421 if (pixbuf) g_object_unref(pixbuf);
1423 /* Color correction takes too much time for an animated gif */
1424 lw = layout_find_by_image(imd);
1425 if (imd->color_profile_enable && lw && !lw->animation)
1427 image_post_process_color(imd, 0, FALSE); /** @todo error handling */
1430 if (imd->cm || imd->desaturate || imd->overunderexposed)
1431 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), image_post_process_tile_color_cb, imd, (imd->cm != nullptr) );
1433 image_state_set(imd, IMAGE_STATE_IMAGE);
1436 void image_change_from_collection(ImageWindow *imd, CollectionData *cd, CollectInfo *info, gdouble zoom)
1440 if (!cd || !info || !g_list_find(cd->list, info)) return;
1442 image_change_real(imd, info->fd, cd, info, zoom);
1443 cw = collection_window_find(cd);
1446 collection_table_set_focus(cw->table, info);
1447 collection_table_unselect_all(cw->table);
1448 collection_table_select(cw->table,info);
1453 image_chain_append_end(info->fd->path);
1457 CollectionData *image_get_collection(ImageWindow *imd, CollectInfo **info)
1459 if (collection_to_number(imd->collection) >= 0)
1461 if (g_list_find(imd->collection->list, imd->collection_info) != nullptr)
1463 if (info) *info = imd->collection_info;
1467 if (info) *info = nullptr;
1469 return imd->collection;
1472 if (info) *info = nullptr;
1476 static void image_loader_sync_read_ahead_data(ImageLoader *il, gpointer old_data, gpointer data)
1478 if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (gpointer)image_read_ahead_error_cb, old_data))
1479 g_signal_connect(G_OBJECT(il), "error", G_CALLBACK(image_read_ahead_error_cb), data);
1481 if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (gpointer)image_read_ahead_done_cb, old_data))
1482 g_signal_connect(G_OBJECT(il), "done", G_CALLBACK(image_read_ahead_done_cb), data);
1485 static void image_loader_sync_data(ImageLoader *il, gpointer old_data, gpointer data)
1487 if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (gpointer)image_load_area_cb, old_data))
1488 g_signal_connect(G_OBJECT(il), "area_ready", G_CALLBACK(image_load_area_cb), data);
1490 if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (gpointer)image_load_error_cb, old_data))
1491 g_signal_connect(G_OBJECT(il), "error", G_CALLBACK(image_load_error_cb), data);
1493 if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (gpointer)image_load_done_cb, old_data))
1494 g_signal_connect(G_OBJECT(il), "done", G_CALLBACK(image_load_done_cb), data);
1497 /* this is more like a move function
1498 * it moves most data from source to imd
1500 void image_move_from_image(ImageWindow *imd, ImageWindow *source)
1502 if (imd == source) return;
1504 imd->unknown = source->unknown;
1506 imd->collection = source->collection;
1507 imd->collection_info = source->collection_info;
1509 image_loader_free(imd->il);
1512 image_set_fd(imd, image_get_fd(source));
1517 imd->il = source->il;
1518 source->il = nullptr;
1520 image_loader_sync_data(imd->il, source, imd);
1522 imd->delay_alter_type = source->delay_alter_type;
1523 source->delay_alter_type = ALTER_NONE;
1526 imd->color_profile_enable = source->color_profile_enable;
1527 imd->color_profile_input = source->color_profile_input;
1528 imd->color_profile_use_image = source->color_profile_use_image;
1529 color_man_free(static_cast<ColorMan *>(imd->cm));
1535 imd->cm = source->cm;
1536 source->cm = nullptr;
1538 cm = static_cast<ColorMan *>(imd->cm);
1540 cm->func_done_data = imd;
1543 file_data_unref(imd->read_ahead_fd);
1544 source->read_ahead_fd = nullptr;
1546 imd->orientation = source->orientation;
1547 imd->desaturate = source->desaturate;
1549 imd->user_stereo = source->user_stereo;
1551 pixbuf_renderer_move(PIXBUF_RENDERER(imd->pr), PIXBUF_RENDERER(source->pr));
1553 if (imd->cm || imd->desaturate || imd->overunderexposed)
1554 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), image_post_process_tile_color_cb, imd, (imd->cm != nullptr) );
1556 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), nullptr, nullptr, TRUE);
1560 /* this is a copy function
1561 * source stays unchanged
1563 void image_copy_from_image(ImageWindow *imd, ImageWindow *source)
1565 if (imd == source) return;
1567 imd->unknown = source->unknown;
1569 imd->collection = source->collection;
1570 imd->collection_info = source->collection_info;
1572 image_loader_free(imd->il);
1575 image_set_fd(imd, image_get_fd(source));
1578 imd->color_profile_enable = source->color_profile_enable;
1579 imd->color_profile_input = source->color_profile_input;
1580 imd->color_profile_use_image = source->color_profile_use_image;
1581 color_man_free(static_cast<ColorMan *>(imd->cm));
1587 imd->cm = source->cm;
1588 source->cm = nullptr;
1590 cm = static_cast<ColorMan *>(imd->cm);
1592 cm->func_done_data = imd;
1595 image_loader_free(imd->read_ahead_il);
1596 imd->read_ahead_il = source->read_ahead_il;
1597 source->read_ahead_il = nullptr;
1598 if (imd->read_ahead_il) image_loader_sync_read_ahead_data(imd->read_ahead_il, source, imd);
1600 file_data_unref(imd->read_ahead_fd);
1601 imd->read_ahead_fd = source->read_ahead_fd;
1602 source->read_ahead_fd = nullptr;
1604 imd->completed = source->completed;
1605 imd->state = source->state;
1606 source->state = IMAGE_STATE_NONE;
1608 imd->orientation = source->orientation;
1609 imd->desaturate = source->desaturate;
1611 imd->user_stereo = source->user_stereo;
1613 pixbuf_renderer_copy(PIXBUF_RENDERER(imd->pr), PIXBUF_RENDERER(source->pr));
1615 if (imd->cm || imd->desaturate || imd->overunderexposed)
1616 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), image_post_process_tile_color_cb, imd, (imd->cm != nullptr) );
1618 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), nullptr, nullptr, TRUE);
1625 void image_area_changed(ImageWindow *imd, gint x, gint y, gint width, gint height)
1627 pixbuf_renderer_area_changed(reinterpret_cast<PixbufRenderer *>(imd->pr), x, y, width, height);
1630 void image_reload(ImageWindow *imd)
1632 if (pixbuf_renderer_get_tiles(reinterpret_cast<PixbufRenderer *>(imd->pr))) return;
1634 image_change_complete(imd, image_zoom_get(imd));
1637 void image_scroll(ImageWindow *imd, gint x, gint y)
1639 pixbuf_renderer_scroll(reinterpret_cast<PixbufRenderer *>(imd->pr), x, y);
1642 void image_scroll_to_point(ImageWindow *imd, gint x, gint y,
1643 gdouble x_align, gdouble y_align)
1645 pixbuf_renderer_scroll_to_point(reinterpret_cast<PixbufRenderer *>(imd->pr), x, y, x_align, y_align);
1648 void image_get_scroll_center(ImageWindow *imd, gdouble *x, gdouble *y)
1650 pixbuf_renderer_get_scroll_center(PIXBUF_RENDERER(imd->pr), x, y);
1653 void image_set_scroll_center(ImageWindow *imd, gdouble x, gdouble y)
1655 pixbuf_renderer_set_scroll_center(PIXBUF_RENDERER(imd->pr), x, y);
1658 void image_zoom_adjust(ImageWindow *imd, gdouble increment)
1660 pixbuf_renderer_zoom_adjust(reinterpret_cast<PixbufRenderer *>(imd->pr), increment);
1663 void image_zoom_adjust_at_point(ImageWindow *imd, gdouble increment, gint x, gint y)
1665 pixbuf_renderer_zoom_adjust_at_point(reinterpret_cast<PixbufRenderer *>(imd->pr), increment, x, y);
1668 void image_zoom_set_limits(ImageWindow *imd, gdouble min, gdouble max)
1670 pixbuf_renderer_zoom_set_limits(reinterpret_cast<PixbufRenderer *>(imd->pr), min, max);
1673 void image_zoom_set(ImageWindow *imd, gdouble zoom)
1675 pixbuf_renderer_zoom_set(reinterpret_cast<PixbufRenderer *>(imd->pr), zoom);
1678 void image_zoom_set_fill_geometry(ImageWindow *imd, gboolean vertical)
1684 pr = reinterpret_cast<PixbufRenderer *>(imd->pr);
1686 if (!pixbuf_renderer_get_pixbuf(pr) ||
1687 !pixbuf_renderer_get_image_size(pr, &width, &height)) return;
1691 zoom = static_cast<gdouble>(pr->viewport_height) / height;
1695 zoom = static_cast<gdouble>(pr->viewport_width) / width;
1700 zoom = 0.0 - 1.0 / zoom;
1703 pixbuf_renderer_zoom_set(pr, zoom);
1706 gdouble image_zoom_get(ImageWindow *imd)
1708 return pixbuf_renderer_zoom_get(reinterpret_cast<PixbufRenderer *>(imd->pr));
1711 gdouble image_zoom_get_real(ImageWindow *imd)
1713 return pixbuf_renderer_zoom_get_scale(reinterpret_cast<PixbufRenderer *>(imd->pr));
1716 gchar *image_zoom_get_as_text(ImageWindow *imd)
1724 const gchar *approx = " ";
1726 zoom = image_zoom_get(imd);
1727 scale = image_zoom_get_real(imd);
1733 else if (zoom < 0.0)
1737 else if (zoom == 0.0 && scale != 0.0)
1750 if (rint(l) != l) pl = 2;
1751 if (rint(r) != r) pr = 2;
1753 return g_strdup_printf("%.*f :%s%.*f", pl, l, approx, pr, r);
1756 gdouble image_zoom_get_default(ImageWindow *imd)
1760 switch (options->image.zoom_mode)
1762 case ZOOM_RESET_ORIGINAL:
1764 case ZOOM_RESET_FIT_WINDOW:
1767 case ZOOM_RESET_NONE:
1768 if (imd) zoom = image_zoom_get(imd);
1777 #pragma GCC diagnostic push
1778 #pragma GCC diagnostic ignored "-Wunused-function"
1779 gint image_stereo_get_unused(ImageWindow *imd)
1781 return pixbuf_renderer_stereo_get((PixbufRenderer *)imd->pr);
1783 #pragma GCC diagnostic pop
1785 void image_stereo_set(ImageWindow *imd, gint stereo_mode)
1787 DEBUG_1("Setting stereo mode %04x for imd %p", stereo_mode, (void *)imd);
1788 pixbuf_renderer_stereo_set(reinterpret_cast<PixbufRenderer *>(imd->pr), stereo_mode);
1791 #pragma GCC diagnostic push
1792 #pragma GCC diagnostic ignored "-Wunused-function"
1793 void image_stereo_swap_unused(ImageWindow *imd)
1795 gint stereo_mode = pixbuf_renderer_stereo_get((PixbufRenderer *)imd->pr);
1796 stereo_mode ^= PR_STEREO_SWAP;
1797 pixbuf_renderer_stereo_set((PixbufRenderer *)imd->pr, stereo_mode);
1799 #pragma GCC diagnostic pop
1801 StereoPixbufData image_stereo_pixbuf_get(ImageWindow *imd)
1803 return static_cast<StereoPixbufData>(imd->user_stereo);
1806 void image_stereo_pixbuf_set(ImageWindow *imd, StereoPixbufData stereo_mode)
1808 imd->user_stereo = stereo_mode;
1814 void image_prebuffer_set(ImageWindow *imd, FileData *fd)
1816 if (pixbuf_renderer_get_tiles(reinterpret_cast<PixbufRenderer *>(imd->pr))) return;
1820 if (!file_cache_get(image_get_cache(), fd))
1822 image_read_ahead_set(imd, fd);
1827 image_read_ahead_cancel(imd);
1831 static void image_notify_cb(FileData *fd, NotifyType type, gpointer data)
1833 auto imd = static_cast<ImageWindow *>(data);
1835 if (!imd || !image_get_pixbuf(imd) ||
1836 /* imd->il || */ /* loading in progress - do not check - it should start from the beginning anyway */
1837 !imd->image_fd || /* nothing to reload */
1838 imd->state == IMAGE_STATE_NONE /* loading not started, no need to reload */
1841 if ((type & NOTIFY_REREAD) && fd == imd->image_fd)
1843 /* there is no need to reload on NOTIFY_CHANGE,
1844 modified files should be detacted anyway and NOTIFY_REREAD should be received
1845 or they are removed from the filelist completely on "move" and "delete"
1847 DEBUG_1("Notify image: %s %04x", fd->path, type);
1852 void image_auto_refresh_enable(ImageWindow *imd, gboolean enable)
1854 if (!enable && imd->auto_refresh && imd->image_fd)
1855 file_data_unregister_real_time_monitor(imd->image_fd);
1857 if (enable && !imd->auto_refresh && imd->image_fd)
1858 file_data_register_real_time_monitor(imd->image_fd);
1860 imd->auto_refresh = enable;
1863 void image_top_window_set_sync(ImageWindow *imd, gboolean allow_sync)
1865 imd->top_window_sync = allow_sync;
1867 g_object_set(G_OBJECT(imd->pr), "window_fit", allow_sync, NULL);
1870 void image_background_set_color(ImageWindow *imd, GdkColor *color)
1872 pixbuf_renderer_set_color(reinterpret_cast<PixbufRenderer *>(imd->pr), color);
1875 void image_background_set_color_from_options(ImageWindow *imd, gboolean fullscreen)
1877 GdkColor *color = nullptr;
1878 GdkColor theme_color;
1880 GtkStyleContext *style_context;
1881 LayoutWindow *lw = nullptr;
1883 if ((options->image.use_custom_border_color && !fullscreen) ||
1884 (options->image.use_custom_border_color_in_fullscreen && fullscreen))
1886 color = &options->image.border_color;
1891 if (!layout_valid(&lw)) return;
1893 style_context = gtk_widget_get_style_context(lw->window);
1894 gtk_style_context_get_background_color(style_context, GTK_STATE_FLAG_NORMAL, &bg_color);
1896 theme_color.red = bg_color.red * 65535;
1897 theme_color.green = bg_color.green * 65535;
1898 theme_color.blue = bg_color.blue * 65535;
1900 color = &theme_color;
1903 image_background_set_color(imd, color);
1906 void image_color_profile_set(ImageWindow *imd,
1912 if (input_type < 0 || input_type >= COLOR_PROFILE_FILE + COLOR_PROFILE_INPUTS)
1917 imd->color_profile_input = input_type;
1918 imd->color_profile_use_image = use_image;
1921 gboolean image_color_profile_get(ImageWindow *imd,
1923 gboolean *use_image)
1925 if (!imd) return FALSE;
1927 if (input_type) *input_type = imd->color_profile_input;
1928 if (use_image) *use_image = imd->color_profile_use_image;
1933 void image_color_profile_set_use(ImageWindow *imd, gboolean enable)
1937 if (imd->color_profile_enable == enable) return;
1939 imd->color_profile_enable = enable;
1942 gboolean image_color_profile_get_use(ImageWindow *imd)
1944 if (!imd) return FALSE;
1946 return imd->color_profile_enable;
1949 gboolean image_color_profile_get_status(ImageWindow *imd, gchar **image_profile, gchar **screen_profile)
1952 if (!imd) return FALSE;
1954 cm = static_cast<ColorMan *>(imd->cm);
1955 if (!cm) return FALSE;
1956 return color_man_get_status(cm, image_profile, screen_profile);
1960 void image_set_delay_flip(ImageWindow *imd, gboolean delay)
1963 imd->delay_flip == delay) return;
1965 imd->delay_flip = delay;
1967 g_object_set(G_OBJECT(imd->pr), "delay_flip", delay, NULL);
1969 if (!imd->delay_flip && imd->il)
1973 pr = PIXBUF_RENDERER(imd->pr);
1974 if (pr->pixbuf) g_object_unref(pr->pixbuf);
1975 pr->pixbuf = nullptr;
1977 image_load_pixbuf_ready(imd);
1981 void image_to_root_window(ImageWindow *, gboolean)
1985 void image_select(ImageWindow *imd, gboolean select)
1987 if (!imd->has_frame) return;
1991 gtk_widget_set_state(imd->widget, GTK_STATE_SELECTED);
1992 gtk_widget_set_state(imd->pr, GTK_STATE_NORMAL); /* do not propagate */
1995 gtk_widget_set_state(imd->widget, GTK_STATE_NORMAL);
1998 void image_set_selectable(ImageWindow *imd, gboolean selectable)
2000 if (!imd->has_frame) return;
2002 gtk_frame_set_shadow_type(GTK_FRAME(imd->frame), GTK_SHADOW_NONE);
2003 gtk_container_set_border_width(GTK_CONTAINER(imd->frame), selectable ? 4 : 0);
2006 void image_grab_focus(ImageWindow *imd)
2010 gtk_widget_grab_focus(imd->frame);
2014 gtk_widget_grab_focus(imd->widget);
2020 *-------------------------------------------------------------------
2022 *-------------------------------------------------------------------
2025 static void image_options_set(ImageWindow *imd)
2027 g_object_set(G_OBJECT(imd->pr), "zoom_quality", options->image.zoom_quality,
2028 "zoom_2pass", options->image.zoom_2pass,
2029 "zoom_expand", options->image.zoom_to_fit_allow_expand,
2030 "scroll_reset", options->image.scroll_reset_method,
2031 "cache_display", options->image.tile_cache_max,
2032 "window_fit", (imd->top_window_sync && options->image.fit_window_to_image),
2033 "window_limit", options->image.limit_window_size,
2034 "window_limit_value", options->image.max_window_size,
2035 "autofit_limit", options->image.limit_autofit_size,
2036 "autofit_limit_value", options->image.max_autofit_size,
2037 "enlargement_limit_value", options->image.max_enlargement_size,
2041 pixbuf_renderer_set_parent(reinterpret_cast<PixbufRenderer *>(imd->pr), reinterpret_cast<GtkWindow *>(imd->top_window));
2043 image_stereo_set(imd, options->stereo.mode);
2044 pixbuf_renderer_stereo_fixed_set(reinterpret_cast<PixbufRenderer *>(imd->pr),
2045 options->stereo.fixed_w, options->stereo.fixed_h,
2046 options->stereo.fixed_x1, options->stereo.fixed_y1,
2047 options->stereo.fixed_x2, options->stereo.fixed_y2);
2050 void image_options_sync()
2059 imd = static_cast<ImageWindow *>(work->data);
2062 image_options_set(imd);
2067 *-------------------------------------------------------------------
2069 *-------------------------------------------------------------------
2072 static void image_free(ImageWindow *imd)
2074 image_list = g_list_remove(image_list, imd);
2076 if (imd->auto_refresh && imd->image_fd)
2077 file_data_unregister_real_time_monitor(imd->image_fd);
2079 file_data_unregister_notify_func(image_notify_cb, imd);
2083 image_read_ahead_cancel(imd);
2085 file_data_unref(imd->image_fd);
2087 g_free(imd->title_right);
2091 static void image_destroy_cb(GtkWidget *, gpointer data)
2093 auto imd = static_cast<ImageWindow *>(data);
2097 gboolean selectable_frame_draw_cb(GtkWidget *widget, cairo_t *cr, gpointer)
2099 GtkAllocation allocation;
2100 gtk_widget_get_allocation(widget, &allocation);
2101 gtk_paint_flat_box(gtk_widget_get_style(widget),
2103 gtk_widget_get_state(widget),
2104 gtk_frame_get_shadow_type(GTK_FRAME(widget)),
2107 allocation.x + 3, allocation.y + 3,
2108 allocation.width - 6, allocation.height - 6);
2110 if (gtk_widget_has_focus(widget))
2112 gtk_paint_focus(gtk_widget_get_style(widget), cr, GTK_STATE_ACTIVE,
2113 widget, "image_window",
2114 allocation.x, allocation.y,
2115 allocation.width - 1, allocation.height - 1);
2119 gtk_paint_shadow(gtk_widget_get_style(widget), cr, GTK_STATE_NORMAL, GTK_SHADOW_IN,
2120 widget, "image_window",
2121 allocation.x, allocation.y,
2122 allocation.width - 1, allocation.height - 1);
2127 void image_set_frame(ImageWindow *imd, gboolean frame)
2131 if (frame == imd->has_frame) return;
2133 gtk_widget_hide(imd->pr);
2137 imd->frame = gtk_frame_new(nullptr);
2138 DEBUG_NAME(imd->frame);
2139 g_object_ref(imd->pr);
2140 if (imd->has_frame != -1) gtk_container_remove(GTK_CONTAINER(imd->widget), imd->pr);
2141 gtk_container_add(GTK_CONTAINER(imd->frame), imd->pr);
2143 g_object_unref(imd->pr);
2144 gtk_widget_set_can_focus(imd->frame, TRUE);
2145 gtk_widget_set_app_paintable(imd->frame, TRUE);
2147 g_signal_connect(G_OBJECT(imd->frame), "draw",
2148 G_CALLBACK(selectable_frame_draw_cb), NULL);
2149 g_signal_connect(G_OBJECT(imd->frame), "focus_in_event",
2150 G_CALLBACK(image_focus_in_cb), imd);
2152 gtk_box_pack_start(GTK_BOX(imd->widget), imd->frame, TRUE, TRUE, 0);
2153 gtk_widget_show(imd->frame);
2157 g_object_ref(imd->pr);
2160 gtk_container_remove(GTK_CONTAINER(imd->frame), imd->pr);
2161 gtk_widget_destroy(imd->frame);
2162 imd->frame = nullptr;
2164 gtk_box_pack_start(GTK_BOX(imd->widget), imd->pr, TRUE, TRUE, 0);
2166 g_object_unref(imd->pr);
2169 gtk_widget_show(imd->pr);
2171 imd->has_frame = frame;
2174 ImageWindow *image_new(gboolean frame)
2178 imd = g_new0(ImageWindow, 1);
2180 imd->unknown = TRUE;
2181 imd->has_frame = -1; /* not initialized; for image_set_frame */
2182 imd->delay_alter_type = ALTER_NONE;
2183 imd->state = IMAGE_STATE_NONE;
2184 imd->color_profile_from_image = COLOR_PROFILE_NONE;
2185 imd->orientation = 1;
2187 imd->pr = GTK_WIDGET(pixbuf_renderer_new());
2188 DEBUG_NAME(imd->pr);
2190 image_options_set(imd);
2192 imd->widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2193 DEBUG_NAME(imd->widget);
2195 image_set_frame(imd, frame);
2197 image_set_selectable(imd, 0);
2199 g_signal_connect(G_OBJECT(imd->pr), "clicked",
2200 G_CALLBACK(image_click_cb), imd);
2201 g_signal_connect(G_OBJECT(imd->pr), "button_press_event",
2202 G_CALLBACK(image_press_cb), imd);
2203 g_signal_connect(G_OBJECT(imd->pr), "scroll_notify",
2204 G_CALLBACK(image_scroll_notify_cb), imd);
2206 g_signal_connect(G_OBJECT(imd->pr), "scroll_event",
2207 G_CALLBACK(image_scroll_cb), imd);
2209 g_signal_connect(G_OBJECT(imd->pr), "destroy",
2210 G_CALLBACK(image_destroy_cb), imd);
2212 g_signal_connect(G_OBJECT(imd->pr), "zoom",
2213 G_CALLBACK(image_zoom_cb), imd);
2214 g_signal_connect(G_OBJECT(imd->pr), "render_complete",
2215 G_CALLBACK(image_render_complete_cb), imd);
2216 g_signal_connect(G_OBJECT(imd->pr), "drag",
2217 G_CALLBACK(image_drag_cb), imd);
2219 file_data_register_notify_func(image_notify_cb, imd, NOTIFY_PRIORITY_LOW);
2221 image_list = g_list_append(image_list, imd);
2226 void image_get_rectangle(gint *x1, gint *y1, gint *x2, gint *y2)
2234 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */