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.
27 #include "collect-table.h"
28 #include "color-man.h"
31 #include "histogram.h"
32 #include "history_list.h"
33 #include "image-load.h"
34 #include "image-overlay.h"
36 #include "layout_image.h"
37 #include "pixbuf-renderer.h"
38 #include "pixbuf_util.h"
39 #include "ui_fileops.h"
42 #include "filecache.h"
46 static GList *image_list = NULL;
48 void image_update_title(ImageWindow *imd);
49 static void image_read_ahead_start(ImageWindow *imd);
50 static void image_cache_set(ImageWindow *imd, FileData *fd);
52 // For draw rectangle function
53 static gint pixbuf_start_x;
54 static gint pixbuf_start_y;
55 static gint image_start_x;
56 static gint image_start_y;
57 static gint rect_x1, rect_x2, rect_y1, rect_y2;
58 static gint rect_id = 0;
61 *-------------------------------------------------------------------
63 *-------------------------------------------------------------------
66 static void image_click_cb(PixbufRenderer *pr, GdkEventButton *event, gpointer data)
68 ImageWindow *imd = data;
69 if (!options->image_lm_click_nav && event->button == MOUSE_BUTTON_MIDDLE)
71 imd->mouse_wheel_mode = !imd->mouse_wheel_mode;
76 imd->func_button(imd, event, imd->data_button);
80 static void switch_coords_orientation(ImageWindow *imd, gint x, gint y, gint width, gint height)
82 switch (imd->orientation)
84 case EXIF_ORIENTATION_TOP_LEFT:
85 /* normal -- nothing to do */
86 rect_x1 = image_start_x;
87 rect_y1 = image_start_y;
91 case EXIF_ORIENTATION_TOP_RIGHT:
94 rect_y1 = image_start_y;
95 rect_x2 = width - image_start_x;
98 case EXIF_ORIENTATION_BOTTOM_RIGHT:
101 rect_y1 = height - y;
102 rect_x2 = width - image_start_x;
103 rect_y2 = height - image_start_y;
105 case EXIF_ORIENTATION_BOTTOM_LEFT:
107 rect_x1 = image_start_x;
108 rect_y1 = height - y;
110 rect_y2 = height - image_start_y;
112 case EXIF_ORIENTATION_LEFT_TOP:
114 rect_x1 = image_start_y;
115 rect_y1 = image_start_x;
119 case EXIF_ORIENTATION_RIGHT_TOP:
120 /* rotated -90 (270) */
121 rect_x1 = image_start_y;
124 rect_y2 = width - image_start_x;
126 case EXIF_ORIENTATION_RIGHT_BOTTOM:
128 rect_x1 = height - y;
130 rect_x2 = height - image_start_y;
131 rect_y2 = width - image_start_x;
133 case EXIF_ORIENTATION_LEFT_BOTTOM:
135 rect_x1 = height - y;
136 rect_y1 = image_start_x;
137 rect_x2 = height - image_start_y;
141 /* The other values are out of range */
146 static void image_press_cb(PixbufRenderer *pr, GdkEventButton *event, gpointer data)
148 ImageWindow *imd = data;
150 gint x_pixel, y_pixel;
152 if(options->draw_rectangle)
154 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
156 pixbuf_start_x = event->x;
157 pixbuf_start_y = event->y;
165 image_start_x = x_pixel;
174 image_start_y = y_pixel;
180 pixbuf_renderer_overlay_remove((PixbufRenderer *)imd->pr, rect_id);
183 lw = layout_find_by_image(imd);
184 if (lw && event->button == MOUSE_BUTTON_LEFT && event->type == GDK_2BUTTON_PRESS
185 && !options->image_lm_click_nav)
188 layout_image_full_screen_stop(lw);
192 static void image_drag_cb(PixbufRenderer *pr, GdkEventMotion *event, gpointer data)
194 ImageWindow *imd = data;
198 GdkPixbuf *rect_pixbuf;
199 gint x_pixel, y_pixel;
200 gint image_x_pixel, image_y_pixel;
202 if (options->draw_rectangle)
204 pixbuf_renderer_get_image_size(pr, &width, &height);
205 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
209 image_x_pixel = width;
213 image_x_pixel = x_pixel;
218 image_y_pixel = height;
222 image_y_pixel = y_pixel;
225 switch_coords_orientation(imd, image_x_pixel, image_y_pixel, width, height);
228 pixbuf_renderer_overlay_remove((PixbufRenderer *)imd->pr, rect_id);
231 rect_width = pr->drag_last_x - pixbuf_start_x;
236 rect_height = pr->drag_last_y - pixbuf_start_y;
237 if (rect_height <= 0)
242 rect_pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, rect_width, rect_height);
243 pixbuf_set_rect_fill(rect_pixbuf, 0, 0, rect_width, rect_height, 255, 255, 255, 0);
244 pixbuf_set_rect(rect_pixbuf, 1, 1, rect_width-2, rect_height - 2, 0, 0, 0, 255, 1, 1, 1, 1);
245 pixbuf_set_rect(rect_pixbuf, 2, 2, rect_width-4, rect_height - 4, 255, 255, 255, 255, 1, 1, 1, 1);
247 rect_id = pixbuf_renderer_overlay_add((PixbufRenderer *)imd->pr, rect_pixbuf, pixbuf_start_x, pixbuf_start_y, OVL_NORMAL);
250 pixbuf_renderer_get_scaled_size(pr, &width, &height);
254 imd->func_drag(imd, event,
255 (gfloat)(pr->drag_last_x - event->x) / width,
256 (gfloat)(pr->drag_last_y - event->y) / height,
261 static void image_scroll_notify_cb(PixbufRenderer *pr, gpointer data)
263 ImageWindow *imd = data;
265 if (imd->func_scroll_notify && pr->scale)
267 imd->func_scroll_notify(imd,
268 (gint)((gdouble)pr->x_scroll / pr->scale),
269 (gint)((gdouble)pr->y_scroll / pr->scale),
270 (gint)((gdouble)pr->image_width - pr->vis_width / pr->scale),
271 (gint)((gdouble)pr->image_height - pr->vis_height / pr->scale),
272 imd->data_scroll_notify);
276 static void image_update_util(ImageWindow *imd)
278 if (imd->func_update) imd->func_update(imd, imd->data_update);
282 static void image_complete_util(ImageWindow *imd, gboolean preload)
284 if (imd->il && image_get_pixbuf(imd) != image_loader_get_pixbuf(imd->il)) return;
286 DEBUG_1("%s image load completed \"%s\" (%s)", get_exec_time(),
287 (preload) ? (imd->read_ahead_fd ? imd->read_ahead_fd->path : "null") :
288 (imd->image_fd ? imd->image_fd->path : "null"),
289 (preload) ? "preload" : "current");
291 if (!preload) imd->completed = TRUE;
292 if (imd->func_complete) imd->func_complete(imd, preload, imd->data_complete);
295 static void image_render_complete_cb(PixbufRenderer *pr, gpointer data)
297 ImageWindow *imd = data;
299 image_complete_util(imd, FALSE);
302 static void image_state_set(ImageWindow *imd, ImageState state)
304 if (state == IMAGE_STATE_NONE)
312 if (imd->func_state) imd->func_state(imd, state, imd->data_state);
315 static void image_state_unset(ImageWindow *imd, ImageState state)
317 imd->state &= ~state;
318 if (imd->func_state) imd->func_state(imd, state, imd->data_state);
321 static void image_zoom_cb(PixbufRenderer *pr, gdouble zoom, gpointer data)
323 ImageWindow *imd = data;
325 if (imd->title_show_zoom) image_update_title(imd);
326 image_state_set(imd, IMAGE_STATE_IMAGE);
327 image_update_util(imd);
331 *-------------------------------------------------------------------
333 *-------------------------------------------------------------------
336 void image_update_title(ImageWindow *imd)
340 gchar *collection = NULL;
342 gchar *lw_ident = NULL;
344 if (!imd->top_window) return;
346 if (imd->collection && collection_to_number(imd->collection) >= 0)
348 const gchar *name = imd->collection->name;
349 if (!name) name = _("Untitled");
350 collection = g_strdup_printf(_(" (Collection %s)"), name);
353 if (imd->title_show_zoom)
355 gchar *buf = image_zoom_get_as_text(imd);
356 zoom = g_strconcat(" [", buf, "]", NULL);
360 lw = layout_find_by_image(imd);
363 lw_ident = g_strconcat(" (", lw->options.id, ")", NULL);
366 title = g_strdup_printf("%s%s%s%s%s%s%s",
367 imd->title ? imd->title : "",
368 imd->image_fd ? imd->image_fd->name : "",
370 collection ? collection : "",
371 imd->image_fd ? " - " : "",
372 imd->title_right ? imd->title_right : "",
373 options->show_window_ids ? (lw_ident ? lw_ident : "") : ""
380 gtk_window_set_title(GTK_WINDOW(imd->top_window), title);
388 *-------------------------------------------------------------------
389 * rotation, flip, etc.
390 *-------------------------------------------------------------------
392 static gboolean image_get_x11_screen_profile(ImageWindow *imd, guchar **screen_profile, gint *screen_profile_len)
394 GdkScreen *screen = gtk_widget_get_screen(imd->widget);;
395 GdkAtom type = GDK_NONE;
398 return (gdk_property_get(gdk_screen_get_root_window(screen),
399 gdk_atom_intern ("_ICC_PROFILE", FALSE),
401 0, 64 * 1024 * 1024, FALSE,
402 &type, &format, screen_profile_len, screen_profile) && *screen_profile_len > 0);
405 static gboolean image_post_process_color(ImageWindow *imd, gint start_row, gboolean run_in_bg)
408 ColorManProfileType input_type;
409 ColorManProfileType screen_type;
410 const gchar *input_file = NULL;
411 const gchar *screen_file = NULL;
412 guchar *profile = NULL;
414 guchar *screen_profile = NULL;
415 gint screen_profile_len;
418 if (imd->cm) return FALSE;
420 if (imd->color_profile_input >= COLOR_PROFILE_FILE &&
421 imd->color_profile_input < COLOR_PROFILE_FILE + COLOR_PROFILE_INPUTS)
423 const gchar *file = options->color_profile.input_file[imd->color_profile_input - COLOR_PROFILE_FILE];
425 if (!is_readable_file(file)) return FALSE;
427 input_type = COLOR_PROFILE_FILE;
430 else if (imd->color_profile_input >= COLOR_PROFILE_SRGB &&
431 imd->color_profile_input < COLOR_PROFILE_FILE)
433 input_type = imd->color_profile_input;
441 if (options->color_profile.use_x11_screen_profile &&
442 image_get_x11_screen_profile(imd, &screen_profile, &screen_profile_len))
444 screen_type = COLOR_PROFILE_MEM;
445 DEBUG_1("Using X11 screen profile, length: %d", screen_profile_len);
447 else if (options->color_profile.screen_file &&
448 is_readable_file(options->color_profile.screen_file))
450 screen_type = COLOR_PROFILE_FILE;
451 screen_file = options->color_profile.screen_file;
455 screen_type = COLOR_PROFILE_SRGB;
460 imd->color_profile_from_image = COLOR_PROFILE_NONE;
462 exif = exif_read_fd(imd->image_fd);
466 profile = exif_get_color_profile(exif, &profile_len);
469 if (!imd->color_profile_use_image)
474 DEBUG_1("Found embedded color profile");
475 imd->color_profile_from_image = COLOR_PROFILE_MEM;
479 gchar *interop_index = exif_get_data_as_text(exif, "Exif.Iop.InteroperabilityIndex");
483 /* Exif 2.21 specification */
484 if (!strcmp(interop_index, "R98"))
486 imd->color_profile_from_image = COLOR_PROFILE_SRGB;
487 DEBUG_1("Found EXIF 2.21 ColorSpace of sRGB");
489 else if (!strcmp(interop_index, "R03"))
491 imd->color_profile_from_image = COLOR_PROFILE_ADOBERGB;
492 DEBUG_1("Found EXIF 2.21 ColorSpace of AdobeRGB");
494 g_free(interop_index);
500 /* ColorSpace == 1 specifies sRGB per EXIF 2.2 */
501 if (!exif_get_integer(exif, "Exif.Photo.ColorSpace", &cs)) cs = 0;
504 imd->color_profile_from_image = COLOR_PROFILE_SRGB;
505 DEBUG_1("Found EXIF 2.2 ColorSpace of sRGB");
509 /* non-standard way of specifying AdobeRGB (used by some software) */
510 imd->color_profile_from_image = COLOR_PROFILE_ADOBERGB;
511 DEBUG_1("Found EXIF 2.2 ColorSpace of AdobeRGB");
515 if (imd->color_profile_use_image && imd->color_profile_from_image != COLOR_PROFILE_NONE)
517 input_type = imd->color_profile_from_image;
522 exif_free_fd(imd->image_fd, exif);
528 cm = color_man_new_embedded(run_in_bg ? imd : NULL, NULL,
529 profile, profile_len,
530 screen_type, screen_file, screen_profile, screen_profile_len);
535 cm = color_man_new(run_in_bg ? imd : NULL, NULL,
536 input_type, input_file,
537 screen_type, screen_file, screen_profile, screen_profile_len);
545 cm->incremental_sync = TRUE;
548 imd->cm = (gpointer)cm;
551 image_update_util(imd);
555 g_free(screen_profile);
556 screen_profile = NULL;
563 static void image_post_process_tile_color_cb(PixbufRenderer *pr, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h, gpointer data)
565 ImageWindow *imd = (ImageWindow *)data;
566 if (imd->cm) color_man_correct_region(imd->cm, *pixbuf, x, y, w, h);
567 if (imd->desaturate) pixbuf_desaturate_rect(*pixbuf, x, y, w, h);
568 if (imd->overunderexposed) pixbuf_highlight_overunderexposed(*pixbuf, x, y, w, h);
571 void image_alter_orientation(ImageWindow *imd, FileData *fd_n, AlterType type)
573 static const gint rotate_90[] = {1, 6, 7, 8, 5, 2, 3, 4, 1};
574 static const gint rotate_90_cc[] = {1, 8, 5, 6, 7, 4, 1, 2, 3};
575 static const gint rotate_180[] = {1, 3, 4, 1, 2, 7, 8, 5, 6};
576 static const gint mirror[] = {1, 2, 1, 4, 3, 6, 5, 8, 7};
577 static const gint flip[] = {1, 4, 3, 2, 1, 8, 7, 6, 5};
581 if (!imd || !imd->pr || !imd->image_fd || !fd_n) return;
583 orientation = EXIF_ORIENTATION_TOP_LEFT;
585 if (fd_n->user_orientation)
587 orientation = fd_n->user_orientation;
590 if (options->metadata.write_orientation)
592 if (g_strcmp0(imd->image_fd->format_name, "heif") == 0)
594 orientation = EXIF_ORIENTATION_TOP_LEFT;
598 orientation = metadata_read_int(fd_n, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
605 case ALTER_ROTATE_90:
606 orientation = rotate_90[orientation];
608 case ALTER_ROTATE_90_CC:
609 orientation = rotate_90_cc[orientation];
611 case ALTER_ROTATE_180:
612 orientation = rotate_180[orientation];
615 orientation = mirror[orientation];
618 orientation = flip[orientation];
621 orientation = fd_n->exif_orientation ? fd_n->exif_orientation : 1;
628 if (orientation != (fd_n->exif_orientation ? fd_n->exif_orientation : 1))
630 if (g_strcmp0(fd_n->format_name, "heif") != 0)
632 if (!options->metadata.write_orientation)
634 /* user_orientation does not work together with options->metadata.write_orientation,
635 use either one or the other.
636 we must however handle switching metadata.write_orientation on and off, therefore
637 we just disable referencing new fd's, not unreferencing the old ones
639 if (fd_n->user_orientation == 0) file_data_ref(fd_n);
640 fd_n->user_orientation = orientation;
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_unref(fd_n);
652 fd_n->user_orientation = 0;
655 if (g_strcmp0(fd_n->format_name, "heif") != 0)
657 if (options->metadata.write_orientation)
659 if (type == ALTER_NONE)
661 metadata_write_revert(fd_n, ORIENTATION_KEY);
665 metadata_write_int(fd_n, ORIENTATION_KEY, orientation);
670 if (imd->image_fd == fd_n && !(options->metadata.write_orientation && !options->image.exif_rotate_enable))
672 imd->orientation = orientation;
673 pixbuf_renderer_set_orientation((PixbufRenderer *)imd->pr, orientation);
677 void image_set_desaturate(ImageWindow *imd, gboolean desaturate)
679 imd->desaturate = desaturate;
680 if (imd->cm || imd->desaturate || imd->overunderexposed)
681 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, image_post_process_tile_color_cb, (gpointer) imd, (imd->cm != NULL) );
683 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, NULL, NULL, TRUE);
684 pixbuf_renderer_set_orientation((PixbufRenderer *)imd->pr, imd->orientation);
687 gboolean image_get_desaturate(ImageWindow *imd)
689 return imd->desaturate;
692 void image_set_overunderexposed(ImageWindow *imd, gboolean overunderexposed)
694 imd->overunderexposed = overunderexposed;
695 if (imd->cm || imd->desaturate || imd->overunderexposed)
696 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, image_post_process_tile_color_cb, (gpointer) imd, (imd->cm != NULL) );
698 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, NULL, NULL, TRUE);
699 pixbuf_renderer_set_orientation((PixbufRenderer *)imd->pr, imd->orientation);
702 gboolean image_get_overunderexposed(ImageWindow *imd)
704 return imd->overunderexposed;
707 void image_set_ignore_alpha(ImageWindow *imd, gboolean ignore_alpha)
709 pixbuf_renderer_set_ignore_alpha((PixbufRenderer *)imd->pr, ignore_alpha);
713 *-------------------------------------------------------------------
714 * read ahead (prebuffer)
715 *-------------------------------------------------------------------
718 static void image_read_ahead_cancel(ImageWindow *imd)
720 DEBUG_1("%s read ahead cancelled for :%s", get_exec_time(), imd->read_ahead_fd ? imd->read_ahead_fd->path : "null");
722 image_loader_free(imd->read_ahead_il);
723 imd->read_ahead_il = NULL;
725 file_data_unref(imd->read_ahead_fd);
726 imd->read_ahead_fd = NULL;
729 static void image_read_ahead_done_cb(ImageLoader *il, gpointer data)
731 ImageWindow *imd = data;
733 if (!imd->read_ahead_fd || !imd->read_ahead_il) return;
735 DEBUG_1("%s read ahead done for :%s", get_exec_time(), imd->read_ahead_fd->path);
737 if (!imd->read_ahead_fd->pixbuf)
739 imd->read_ahead_fd->pixbuf = image_loader_get_pixbuf(imd->read_ahead_il);
740 if (imd->read_ahead_fd->pixbuf)
742 g_object_ref(imd->read_ahead_fd->pixbuf);
743 image_cache_set(imd, imd->read_ahead_fd);
746 image_loader_free(imd->read_ahead_il);
747 imd->read_ahead_il = NULL;
749 image_complete_util(imd, TRUE);
752 static void image_read_ahead_error_cb(ImageLoader *il, gpointer data)
754 /* we even treat errors as success, maybe at least some of the file was ok */
755 image_read_ahead_done_cb(il, data);
758 static void image_read_ahead_start(ImageWindow *imd)
760 /* already started ? */
761 if (!imd->read_ahead_fd || imd->read_ahead_il || imd->read_ahead_fd->pixbuf) return;
763 /* still loading ?, do later */
764 if (imd->il /*|| imd->cm*/) return;
766 DEBUG_1("%s read ahead started for :%s", get_exec_time(), imd->read_ahead_fd->path);
768 imd->read_ahead_il = image_loader_new(imd->read_ahead_fd);
770 image_loader_delay_area_ready(imd->read_ahead_il, TRUE); /* we will need the area_ready signals later */
772 g_signal_connect(G_OBJECT(imd->read_ahead_il), "error", (GCallback)image_read_ahead_error_cb, imd);
773 g_signal_connect(G_OBJECT(imd->read_ahead_il), "done", (GCallback)image_read_ahead_done_cb, imd);
775 if (!image_loader_start(imd->read_ahead_il))
777 image_read_ahead_cancel(imd);
778 image_complete_util(imd, TRUE);
782 static void image_read_ahead_set(ImageWindow *imd, FileData *fd)
784 if (imd->read_ahead_fd && fd && imd->read_ahead_fd == fd) return;
786 image_read_ahead_cancel(imd);
788 imd->read_ahead_fd = file_data_ref(fd);
790 DEBUG_1("read ahead set to :%s", imd->read_ahead_fd->path);
792 image_read_ahead_start(imd);
796 *-------------------------------------------------------------------
798 *-------------------------------------------------------------------
801 static void image_cache_release_cb(FileData *fd)
803 g_object_unref(fd->pixbuf);
807 static FileCacheData *image_get_cache(void)
809 static FileCacheData *cache = NULL;
810 if (!cache) cache = file_cache_new(image_cache_release_cb, 1);
811 file_cache_set_max_size(cache, (gulong)options->image.image_cache_max * 1048576); /* update from options */
815 static void image_cache_set(ImageWindow *imd, FileData *fd)
817 g_assert(fd->pixbuf);
819 file_cache_put(image_get_cache(), fd, (gulong)gdk_pixbuf_get_rowstride(fd->pixbuf) * (gulong)gdk_pixbuf_get_height(fd->pixbuf));
820 file_data_send_notification(fd, NOTIFY_PIXBUF); /* to update histogram */
823 static gint image_cache_get(ImageWindow *imd)
827 success = file_cache_get(image_get_cache(), imd->image_fd);
830 g_assert(imd->image_fd->pixbuf);
831 image_change_pixbuf(imd, imd->image_fd->pixbuf, image_zoom_get(imd), FALSE);
834 // file_cache_dump(image_get_cache());
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 ImageWindow *imd = data;
856 pr = (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 *il, gpointer data)
871 ImageWindow *imd = 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 = 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))
888 switch (imd->image_fd->format_class)
890 case FORMAT_CLASS_UNKNOWN:
891 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
893 case FORMAT_CLASS_META:
894 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
896 case FORMAT_CLASS_VIDEO:
897 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
899 case FORMAT_CLASS_COLLECTION:
900 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
902 case FORMAT_CLASS_DOCUMENT:
903 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
906 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
909 image_change_pixbuf(imd, pixbuf, image_zoom_get(imd), FALSE);
910 g_object_unref(pixbuf);
914 else if (imd->delay_flip &&
915 image_get_pixbuf(imd) != image_loader_get_pixbuf(imd->il))
917 g_object_set(G_OBJECT(imd->pr), "complete", FALSE, NULL);
918 image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), FALSE);
921 image_loader_free(imd->il);
924 // image_post_process(imd, TRUE);
926 image_read_ahead_start(imd);
929 static void image_load_size_cb(ImageLoader *il, guint width, guint height, gpointer data)
931 ImageWindow *imd = data;
933 DEBUG_1("image_load_size_cb: %dx%d", width, height);
934 pixbuf_renderer_set_size_early((PixbufRenderer *)imd->pr, width, height);
937 static void image_load_error_cb(ImageLoader *il, gpointer data)
939 DEBUG_1("%s image error", get_exec_time());
941 /* even on error handle it like it was done,
942 * since we have a pixbuf with _something_ */
944 image_load_done_cb(il, data);
947 static void image_load_set_signals(ImageWindow *imd, gboolean override_old_signals)
950 if (override_old_signals)
952 /* override the old signals */
953 g_signal_handlers_disconnect_matched(G_OBJECT(imd->il), G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, imd);
956 g_signal_connect(G_OBJECT(imd->il), "area_ready", (GCallback)image_load_area_cb, imd);
957 g_signal_connect(G_OBJECT(imd->il), "error", (GCallback)image_load_error_cb, imd);
958 g_signal_connect(G_OBJECT(imd->il), "done", (GCallback)image_load_done_cb, imd);
959 g_signal_connect(G_OBJECT(imd->il), "size_prepared", (GCallback)image_load_size_cb, imd);
962 /* this read ahead is located here merely for the callbacks, above */
964 static gboolean image_read_ahead_check(ImageWindow *imd)
966 if (!imd->read_ahead_fd) return FALSE;
967 if (imd->il) return FALSE;
969 if (!imd->image_fd || imd->read_ahead_fd != imd->image_fd)
971 image_read_ahead_cancel(imd);
975 if (imd->read_ahead_il)
977 imd->il = imd->read_ahead_il;
978 imd->read_ahead_il = NULL;
980 image_load_set_signals(imd, TRUE);
982 g_object_set(G_OBJECT(imd->pr), "loading", TRUE, NULL);
983 image_state_set(imd, IMAGE_STATE_LOADING);
985 if (!imd->delay_flip)
987 image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), TRUE);
990 image_loader_delay_area_ready(imd->il, FALSE); /* send the delayed area_ready signals */
992 file_data_unref(imd->read_ahead_fd);
993 imd->read_ahead_fd = NULL;
996 else if (imd->read_ahead_fd->pixbuf)
998 image_change_pixbuf(imd, imd->read_ahead_fd->pixbuf, image_zoom_get(imd), FALSE);
1000 file_data_unref(imd->read_ahead_fd);
1001 imd->read_ahead_fd = NULL;
1003 // image_post_process(imd, FALSE);
1007 image_read_ahead_cancel(imd);
1011 static gboolean image_load_begin(ImageWindow *imd, FileData *fd)
1013 DEBUG_1("%s image begin", get_exec_time());
1015 if (imd->il) return FALSE;
1017 imd->completed = FALSE;
1018 g_object_set(G_OBJECT(imd->pr), "complete", FALSE, NULL);
1020 if (image_cache_get(imd))
1022 DEBUG_1("from cache: %s", imd->image_fd->path);
1026 if (image_read_ahead_check(imd))
1028 DEBUG_1("from read ahead buffer: %s", imd->image_fd->path);
1032 if (!imd->delay_flip && image_get_pixbuf(imd))
1036 pr = PIXBUF_RENDERER(imd->pr);
1037 if (pr->pixbuf) g_object_unref(pr->pixbuf);
1041 g_object_set(G_OBJECT(imd->pr), "loading", TRUE, NULL);
1043 imd->il = image_loader_new(fd);
1045 image_load_set_signals(imd, FALSE);
1047 if (!image_loader_start(imd->il))
1049 DEBUG_1("image start error");
1051 g_object_set(G_OBJECT(imd->pr), "loading", FALSE, NULL);
1053 image_loader_free(imd->il);
1056 image_complete_util(imd, FALSE);
1061 image_state_set(imd, IMAGE_STATE_LOADING);
1064 if (!imd->delay_flip && !image_get_pixbuf(imd) && image_loader_get_pixbuf(imd->il))
1066 image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), TRUE);
1072 static void image_reset(ImageWindow *imd)
1074 /* stops anything currently being done */
1076 DEBUG_1("%s image reset", get_exec_time());
1078 g_object_set(G_OBJECT(imd->pr), "loading", FALSE, NULL);
1080 image_loader_free(imd->il);
1083 color_man_free((ColorMan *)imd->cm);
1086 imd->delay_alter_type = ALTER_NONE;
1088 image_state_set(imd, IMAGE_STATE_NONE);
1092 *-------------------------------------------------------------------
1094 *-------------------------------------------------------------------
1097 static void image_change_complete(ImageWindow *imd, gdouble zoom)
1100 imd->unknown = TRUE;
1104 image_change_pixbuf(imd, NULL, zoom, FALSE);
1109 if (is_readable_file(imd->image_fd->path))
1113 pr = PIXBUF_RENDERER(imd->pr);
1114 pr->zoom = zoom; /* store the zoom, needed by the loader */
1116 /* Disable 2-pass for GIFs. Animated GIFs can flicker when enabled
1117 * Reduce quality to worst but fastest to avoid dropped frames */
1118 if (g_ascii_strcasecmp(imd->image_fd->extension, ".GIF") == 0)
1120 g_object_set(G_OBJECT(imd->pr), "zoom_2pass", FALSE, NULL);
1121 g_object_set(G_OBJECT(imd->pr), "zoom_quality", GDK_INTERP_NEAREST, NULL);
1125 if (image_load_begin(imd, imd->image_fd))
1127 imd->unknown = FALSE;
1131 if (imd->unknown == TRUE)
1135 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
1136 image_change_pixbuf(imd, pixbuf, zoom, FALSE);
1137 g_object_unref(pixbuf);
1141 image_update_util(imd);
1144 static void image_change_real(ImageWindow *imd, FileData *fd,
1145 CollectionData *cd, CollectInfo *info, gdouble zoom)
1148 imd->collection = cd;
1149 imd->collection_info = info;
1151 if (imd->auto_refresh && imd->image_fd)
1152 file_data_unregister_real_time_monitor(imd->image_fd);
1154 file_data_unref(imd->image_fd);
1155 imd->image_fd = file_data_ref(fd);
1158 image_change_complete(imd, zoom);
1160 image_update_title(imd);
1161 image_state_set(imd, IMAGE_STATE_IMAGE);
1163 if (imd->auto_refresh && imd->image_fd)
1164 file_data_register_real_time_monitor(imd->image_fd);
1168 *-------------------------------------------------------------------
1170 *-------------------------------------------------------------------
1173 static gboolean image_focus_in_cb(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1175 ImageWindow *imd = data;
1177 if (imd->func_focus_in)
1179 imd->func_focus_in(imd, imd->data_focus_in);
1185 static gboolean image_scroll_cb(GtkWidget *widget, GdkEventScroll *event, gpointer data)
1187 ImageWindow *imd = data;
1188 gboolean in_lw = FALSE;
1190 LayoutWindow *lw = NULL;
1192 if (imd->func_scroll && event && event->type == GDK_SCROLL)
1195 /* check if the image is in a layout window */
1196 for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1198 if (imd == lw->split_images[i])
1207 if (lw->options.split_pane_sync)
1209 for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1211 if (lw->split_images[i])
1213 layout_image_activate(lw, i, FALSE);
1214 imd->func_scroll(lw->split_images[i], event, lw->split_images[i]->data_scroll);
1220 imd->func_scroll(imd, event, imd->data_scroll);
1226 imd->func_scroll(imd, event, imd->data_scroll);
1235 *-------------------------------------------------------------------
1237 *-------------------------------------------------------------------
1240 void image_attach_window(ImageWindow *imd, GtkWidget *window,
1241 const gchar *title, const gchar *title_right, gboolean show_zoom)
1245 imd->top_window = window;
1247 imd->title = g_strdup(title);
1248 g_free(imd->title_right);
1249 imd->title_right = g_strdup(title_right);
1250 imd->title_show_zoom = show_zoom;
1252 lw = layout_find_by_image(imd);
1254 if (!(options->image.fit_window_to_image && lw && (lw->options.tools_float || lw->options.tools_hidden))) window = NULL;
1256 pixbuf_renderer_set_parent((PixbufRenderer *)imd->pr, (GtkWindow *)window);
1258 image_update_title(imd);
1261 void image_set_update_func(ImageWindow *imd,
1262 void (*func)(ImageWindow *imd, gpointer data),
1265 imd->func_update = func;
1266 imd->data_update = data;
1269 void image_set_complete_func(ImageWindow *imd,
1270 void (*func)(ImageWindow *imd, gboolean preload, gpointer data),
1273 imd->func_complete = func;
1274 imd->data_complete = data;
1277 void image_set_state_func(ImageWindow *imd,
1278 void (*func)(ImageWindow *imd, ImageState state, gpointer data),
1281 imd->func_state = func;
1282 imd->data_state = data;
1286 void image_set_button_func(ImageWindow *imd,
1287 void (*func)(ImageWindow *, GdkEventButton *event, gpointer),
1290 imd->func_button = func;
1291 imd->data_button = data;
1294 void image_set_drag_func(ImageWindow *imd,
1295 void (*func)(ImageWindow *, GdkEventMotion *event, gdouble dx, gdouble dy, gpointer),
1298 imd->func_drag = func;
1299 imd->data_drag = data;
1302 void image_set_scroll_func(ImageWindow *imd,
1303 void (*func)(ImageWindow *, GdkEventScroll *event, gpointer),
1306 imd->func_scroll = func;
1307 imd->data_scroll = data;
1310 void image_set_scroll_notify_func(ImageWindow *imd,
1311 void (*func)(ImageWindow *imd, gint x, gint y, gint width, gint height, gpointer data),
1314 imd->func_scroll_notify = func;
1315 imd->data_scroll_notify = data;
1318 void image_set_focus_in_func(ImageWindow *imd,
1319 void (*func)(ImageWindow *, gpointer),
1322 imd->func_focus_in = func;
1323 imd->data_focus_in = data;
1328 const gchar *image_get_path(ImageWindow *imd)
1330 if (imd->image_fd == NULL) return NULL;
1331 return imd->image_fd->path;
1334 const gchar *image_get_name(ImageWindow *imd)
1336 if (imd->image_fd == NULL) return NULL;
1337 return imd->image_fd->name;
1340 FileData *image_get_fd(ImageWindow *imd)
1342 return imd->image_fd;
1345 /* merely changes path string, does not change the image! */
1346 void image_set_fd(ImageWindow *imd, FileData *fd)
1348 if (imd->auto_refresh && imd->image_fd)
1349 file_data_unregister_real_time_monitor(imd->image_fd);
1351 file_data_unref(imd->image_fd);
1352 imd->image_fd = file_data_ref(fd);
1354 image_update_title(imd);
1355 image_state_set(imd, IMAGE_STATE_IMAGE);
1357 if (imd->auto_refresh && imd->image_fd)
1358 file_data_register_real_time_monitor(imd->image_fd);
1361 /* load a new image */
1363 void image_change_fd(ImageWindow *imd, FileData *fd, gdouble zoom)
1365 if (imd->image_fd == fd) return;
1367 image_change_real(imd, fd, NULL, NULL, zoom);
1370 gboolean image_get_image_size(ImageWindow *imd, gint *width, gint *height)
1372 return pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), width, height);
1375 GdkPixbuf *image_get_pixbuf(ImageWindow *imd)
1377 return pixbuf_renderer_get_pixbuf((PixbufRenderer *)imd->pr);
1380 void image_change_pixbuf(ImageWindow *imd, GdkPixbuf *pixbuf, gdouble zoom, gboolean lazy)
1383 StereoPixbufData stereo_data = STEREO_PIXBUF_DEFAULT;
1384 /* read_exif and similar functions can actually notice that the file has changed and trigger
1385 a notification that removes the pixbuf from cache and unrefs it. Therefore we must ref it
1386 here before it is taken over by the renderer. */
1387 if (pixbuf) g_object_ref(pixbuf);
1389 imd->orientation = EXIF_ORIENTATION_TOP_LEFT;
1392 if (imd->image_fd->user_orientation)
1394 imd->orientation = imd->image_fd->user_orientation;
1396 else if (options->image.exif_rotate_enable)
1398 if (g_strcmp0(imd->image_fd->format_name, "heif") == 0)
1400 imd->orientation = EXIF_ORIENTATION_TOP_LEFT;
1401 imd->image_fd->exif_orientation = imd->orientation;
1405 imd->orientation = metadata_read_int(imd->image_fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
1406 imd->image_fd->exif_orientation = imd->orientation;
1413 stereo_data = imd->user_stereo;
1414 if (stereo_data == STEREO_PIXBUF_DEFAULT)
1416 stereo_data = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(pixbuf), "stereo_data"));
1420 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, NULL, NULL, FALSE);
1423 color_man_free(imd->cm);
1429 pixbuf_renderer_set_pixbuf_lazy((PixbufRenderer *)imd->pr, pixbuf, zoom, imd->orientation, stereo_data);
1433 pixbuf_renderer_set_pixbuf((PixbufRenderer *)imd->pr, pixbuf, zoom);
1434 pixbuf_renderer_set_orientation((PixbufRenderer *)imd->pr, imd->orientation);
1435 pixbuf_renderer_set_stereo_data((PixbufRenderer *)imd->pr, stereo_data);
1438 if (pixbuf) g_object_unref(pixbuf);
1440 /* Color correction takes too much time for an animated gif */
1441 lw = layout_find_by_image(imd);
1442 if (imd->color_profile_enable && lw && !lw->animation)
1444 image_post_process_color(imd, 0, FALSE); /** @todo error handling */
1447 if (imd->cm || imd->desaturate || imd->overunderexposed)
1448 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, image_post_process_tile_color_cb, (gpointer) imd, (imd->cm != NULL) );
1450 image_state_set(imd, IMAGE_STATE_IMAGE);
1453 void image_change_from_collection(ImageWindow *imd, CollectionData *cd, CollectInfo *info, gdouble zoom)
1457 if (!cd || !info || !g_list_find(cd->list, info)) return;
1459 image_change_real(imd, info->fd, cd, info, zoom);
1460 cw = collection_window_find(cd);
1463 collection_table_set_focus(cw->table, info);
1464 collection_table_unselect_all(cw->table);
1465 collection_table_select(cw->table,info);
1470 image_chain_append_end(info->fd->path);
1474 CollectionData *image_get_collection(ImageWindow *imd, CollectInfo **info)
1476 if (collection_to_number(imd->collection) >= 0)
1478 if (g_list_find(imd->collection->list, imd->collection_info) != NULL)
1480 if (info) *info = imd->collection_info;
1484 if (info) *info = NULL;
1486 return imd->collection;
1489 if (info) *info = NULL;
1493 static void image_loader_sync_read_ahead_data(ImageLoader *il, gpointer old_data, gpointer data)
1495 if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (GCallback)image_read_ahead_error_cb, old_data))
1496 g_signal_connect(G_OBJECT(il), "error", (GCallback)image_read_ahead_error_cb, data);
1498 if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (GCallback)image_read_ahead_done_cb, old_data))
1499 g_signal_connect(G_OBJECT(il), "done", (GCallback)image_read_ahead_done_cb, data);
1502 static void image_loader_sync_data(ImageLoader *il, gpointer old_data, gpointer data)
1504 if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (GCallback)image_load_area_cb, old_data))
1505 g_signal_connect(G_OBJECT(il), "area_ready", (GCallback)image_load_area_cb, data);
1507 if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (GCallback)image_load_error_cb, old_data))
1508 g_signal_connect(G_OBJECT(il), "error", (GCallback)image_load_error_cb, data);
1510 if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (GCallback)image_load_done_cb, old_data))
1511 g_signal_connect(G_OBJECT(il), "done", (GCallback)image_load_done_cb, data);
1514 /* this is more like a move function
1515 * it moves most data from source to imd
1517 void image_move_from_image(ImageWindow *imd, ImageWindow *source)
1519 if (imd == source) return;
1521 imd->unknown = source->unknown;
1523 imd->collection = source->collection;
1524 imd->collection_info = source->collection_info;
1526 image_loader_free(imd->il);
1529 image_set_fd(imd, image_get_fd(source));
1534 imd->il = source->il;
1537 image_loader_sync_data(imd->il, source, imd);
1539 imd->delay_alter_type = source->delay_alter_type;
1540 source->delay_alter_type = ALTER_NONE;
1543 imd->color_profile_enable = source->color_profile_enable;
1544 imd->color_profile_input = source->color_profile_input;
1545 imd->color_profile_use_image = source->color_profile_use_image;
1546 color_man_free((ColorMan *)imd->cm);
1552 imd->cm = source->cm;
1555 cm = (ColorMan *)imd->cm;
1557 cm->func_done_data = imd;
1560 file_data_unref(imd->read_ahead_fd);
1561 source->read_ahead_fd = NULL;
1563 imd->orientation = source->orientation;
1564 imd->desaturate = source->desaturate;
1566 imd->user_stereo = source->user_stereo;
1568 pixbuf_renderer_move(PIXBUF_RENDERER(imd->pr), PIXBUF_RENDERER(source->pr));
1570 if (imd->cm || imd->desaturate || imd->overunderexposed)
1571 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, image_post_process_tile_color_cb, (gpointer) imd, (imd->cm != NULL) );
1573 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, NULL, NULL, TRUE);
1577 /* this is a copy function
1578 * source stays unchanged
1580 void image_copy_from_image(ImageWindow *imd, ImageWindow *source)
1582 if (imd == source) return;
1584 imd->unknown = source->unknown;
1586 imd->collection = source->collection;
1587 imd->collection_info = source->collection_info;
1589 image_loader_free(imd->il);
1592 image_set_fd(imd, image_get_fd(source));
1595 imd->color_profile_enable = source->color_profile_enable;
1596 imd->color_profile_input = source->color_profile_input;
1597 imd->color_profile_use_image = source->color_profile_use_image;
1598 color_man_free((ColorMan *)imd->cm);
1604 imd->cm = source->cm;
1607 cm = (ColorMan *)imd->cm;
1609 cm->func_done_data = imd;
1612 image_loader_free(imd->read_ahead_il);
1613 imd->read_ahead_il = source->read_ahead_il;
1614 source->read_ahead_il = NULL;
1615 if (imd->read_ahead_il) image_loader_sync_read_ahead_data(imd->read_ahead_il, source, imd);
1617 file_data_unref(imd->read_ahead_fd);
1618 imd->read_ahead_fd = source->read_ahead_fd;
1619 source->read_ahead_fd = NULL;
1621 imd->completed = source->completed;
1622 imd->state = source->state;
1623 source->state = IMAGE_STATE_NONE;
1625 imd->orientation = source->orientation;
1626 imd->desaturate = source->desaturate;
1628 imd->user_stereo = source->user_stereo;
1630 pixbuf_renderer_copy(PIXBUF_RENDERER(imd->pr), PIXBUF_RENDERER(source->pr));
1632 if (imd->cm || imd->desaturate || imd->overunderexposed)
1633 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, image_post_process_tile_color_cb, (gpointer) imd, (imd->cm != NULL) );
1635 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, NULL, NULL, TRUE);
1642 void image_area_changed(ImageWindow *imd, gint x, gint y, gint width, gint height)
1644 pixbuf_renderer_area_changed((PixbufRenderer *)imd->pr, x, y, width, height);
1647 void image_reload(ImageWindow *imd)
1649 if (pixbuf_renderer_get_tiles((PixbufRenderer *)imd->pr)) return;
1651 image_change_complete(imd, image_zoom_get(imd));
1654 void image_scroll(ImageWindow *imd, gint x, gint y)
1656 pixbuf_renderer_scroll((PixbufRenderer *)imd->pr, x, y);
1659 void image_scroll_to_point(ImageWindow *imd, gint x, gint y,
1660 gdouble x_align, gdouble y_align)
1662 pixbuf_renderer_scroll_to_point((PixbufRenderer *)imd->pr, x, y, x_align, y_align);
1665 void image_get_scroll_center(ImageWindow *imd, gdouble *x, gdouble *y)
1667 pixbuf_renderer_get_scroll_center(PIXBUF_RENDERER(imd->pr), x, y);
1670 void image_set_scroll_center(ImageWindow *imd, gdouble x, gdouble y)
1672 pixbuf_renderer_set_scroll_center(PIXBUF_RENDERER(imd->pr), x, y);
1675 void image_zoom_adjust(ImageWindow *imd, gdouble increment)
1677 pixbuf_renderer_zoom_adjust((PixbufRenderer *)imd->pr, increment);
1680 void image_zoom_adjust_at_point(ImageWindow *imd, gdouble increment, gint x, gint y)
1682 pixbuf_renderer_zoom_adjust_at_point((PixbufRenderer *)imd->pr, increment, x, y);
1685 void image_zoom_set_limits(ImageWindow *imd, gdouble min, gdouble max)
1687 pixbuf_renderer_zoom_set_limits((PixbufRenderer *)imd->pr, min, max);
1690 void image_zoom_set(ImageWindow *imd, gdouble zoom)
1692 pixbuf_renderer_zoom_set((PixbufRenderer *)imd->pr, zoom);
1695 void image_zoom_set_fill_geometry(ImageWindow *imd, gboolean vertical)
1701 pr = (PixbufRenderer *)imd->pr;
1703 if (!pixbuf_renderer_get_pixbuf(pr) ||
1704 !pixbuf_renderer_get_image_size(pr, &width, &height)) return;
1708 zoom = (gdouble)pr->viewport_height / height;
1712 zoom = (gdouble)pr->viewport_width / width;
1717 zoom = 0.0 - 1.0 / zoom;
1720 pixbuf_renderer_zoom_set(pr, zoom);
1723 gdouble image_zoom_get(ImageWindow *imd)
1725 return pixbuf_renderer_zoom_get((PixbufRenderer *)imd->pr);
1728 gdouble image_zoom_get_real(ImageWindow *imd)
1730 return pixbuf_renderer_zoom_get_scale((PixbufRenderer *)imd->pr);
1733 gchar *image_zoom_get_as_text(ImageWindow *imd)
1741 gchar *approx = " ";
1743 zoom = image_zoom_get(imd);
1744 scale = image_zoom_get_real(imd);
1750 else if (zoom < 0.0)
1754 else if (zoom == 0.0 && scale != 0.0)
1767 if (rint(l) != l) pl = 2;
1768 if (rint(r) != r) pr = 2;
1770 return g_strdup_printf("%.*f :%s%.*f", pl, l, approx, pr, r);
1773 gdouble image_zoom_get_default(ImageWindow *imd)
1777 switch (options->image.zoom_mode)
1779 case ZOOM_RESET_ORIGINAL:
1781 case ZOOM_RESET_FIT_WINDOW:
1784 case ZOOM_RESET_NONE:
1785 if (imd) zoom = image_zoom_get(imd);
1793 gint image_stereo_get(ImageWindow *imd)
1795 return pixbuf_renderer_stereo_get((PixbufRenderer *)imd->pr);
1798 void image_stereo_set(ImageWindow *imd, gint stereo_mode)
1800 DEBUG_1("Setting stereo mode %04x for imd %p", stereo_mode, imd);
1801 pixbuf_renderer_stereo_set((PixbufRenderer *)imd->pr, stereo_mode);
1804 void image_stereo_swap(ImageWindow *imd)
1806 gint stereo_mode = pixbuf_renderer_stereo_get((PixbufRenderer *)imd->pr);
1807 stereo_mode ^= PR_STEREO_SWAP;
1808 pixbuf_renderer_stereo_set((PixbufRenderer *)imd->pr, stereo_mode);
1811 StereoPixbufData image_stereo_pixbuf_get(ImageWindow *imd)
1813 return imd->user_stereo;
1816 void image_stereo_pixbuf_set(ImageWindow *imd, StereoPixbufData stereo_mode)
1818 imd->user_stereo = stereo_mode;
1824 void image_prebuffer_set(ImageWindow *imd, FileData *fd)
1826 if (pixbuf_renderer_get_tiles((PixbufRenderer *)imd->pr)) return;
1830 if (!file_cache_get(image_get_cache(), fd))
1832 image_read_ahead_set(imd, fd);
1837 image_read_ahead_cancel(imd);
1841 static void image_notify_cb(FileData *fd, NotifyType type, gpointer data)
1843 ImageWindow *imd = data;
1845 if (!imd || !image_get_pixbuf(imd) ||
1846 /* imd->il || */ /* loading in progress - do not check - it should start from the beginning anyway */
1847 !imd->image_fd || /* nothing to reload */
1848 imd->state == IMAGE_STATE_NONE /* loading not started, no need to reload */
1851 if ((type & NOTIFY_REREAD) && fd == imd->image_fd)
1853 /* there is no need to reload on NOTIFY_CHANGE,
1854 modified files should be detacted anyway and NOTIFY_REREAD should be received
1855 or they are removed from the filelist completely on "move" and "delete"
1857 DEBUG_1("Notify image: %s %04x", fd->path, type);
1862 void image_auto_refresh_enable(ImageWindow *imd, gboolean enable)
1864 if (!enable && imd->auto_refresh && imd->image_fd)
1865 file_data_unregister_real_time_monitor(imd->image_fd);
1867 if (enable && !imd->auto_refresh && imd->image_fd)
1868 file_data_register_real_time_monitor(imd->image_fd);
1870 imd->auto_refresh = enable;
1873 void image_top_window_set_sync(ImageWindow *imd, gboolean allow_sync)
1875 imd->top_window_sync = allow_sync;
1877 g_object_set(G_OBJECT(imd->pr), "window_fit", allow_sync, NULL);
1880 void image_background_set_color(ImageWindow *imd, GdkColor *color)
1882 pixbuf_renderer_set_color((PixbufRenderer *)imd->pr, color);
1885 void image_background_set_color_from_options(ImageWindow *imd, gboolean fullscreen)
1887 GdkColor *color = NULL;
1889 if ((options->image.use_custom_border_color && !fullscreen) ||
1890 (options->image.use_custom_border_color_in_fullscreen && fullscreen))
1892 color = &options->image.border_color;
1895 image_background_set_color(imd, color);
1898 void image_color_profile_set(ImageWindow *imd,
1904 if (input_type < 0 || input_type >= COLOR_PROFILE_FILE + COLOR_PROFILE_INPUTS)
1909 imd->color_profile_input = input_type;
1910 imd->color_profile_use_image = use_image;
1913 gboolean image_color_profile_get(ImageWindow *imd,
1915 gboolean *use_image)
1917 if (!imd) return FALSE;
1919 if (input_type) *input_type = imd->color_profile_input;
1920 if (use_image) *use_image = imd->color_profile_use_image;
1925 void image_color_profile_set_use(ImageWindow *imd, gboolean enable)
1929 if (imd->color_profile_enable == enable) return;
1931 imd->color_profile_enable = enable;
1934 gboolean image_color_profile_get_use(ImageWindow *imd)
1936 if (!imd) return FALSE;
1938 return imd->color_profile_enable;
1941 gboolean image_color_profile_get_status(ImageWindow *imd, gchar **image_profile, gchar **screen_profile)
1944 if (!imd) return FALSE;
1947 if (!cm) return FALSE;
1948 return color_man_get_status(cm, image_profile, screen_profile);
1952 void image_set_delay_flip(ImageWindow *imd, gboolean delay)
1955 imd->delay_flip == delay) return;
1957 imd->delay_flip = delay;
1959 g_object_set(G_OBJECT(imd->pr), "delay_flip", delay, NULL);
1961 if (!imd->delay_flip && imd->il)
1965 pr = PIXBUF_RENDERER(imd->pr);
1966 if (pr->pixbuf) g_object_unref(pr->pixbuf);
1969 image_load_pixbuf_ready(imd);
1973 void image_to_root_window(ImageWindow *imd, gboolean scaled)
1975 #if !GTK_CHECK_VERSION(3,0,0)
1977 GdkWindow *rootwindow;
1985 pixbuf = image_get_pixbuf(imd);
1986 if (!pixbuf) return;
1988 screen = gtk_widget_get_screen(imd->widget);
1989 rootwindow = gdk_screen_get_root_window(screen);
1990 if (gdk_window_get_visual(rootwindow) != gdk_visual_get_system()) return;
1994 width = gdk_screen_width();
1995 height = gdk_screen_height();
1999 pixbuf_renderer_get_scaled_size((PixbufRenderer *)imd->pr, &width, &height);
2002 pb = gdk_pixbuf_scale_simple(pixbuf, width, height, (GdkInterpType)options->image.zoom_quality);
2004 gdk_pixbuf_render_pixmap_and_mask(pb, &pixmap, NULL, 128);
2005 gdk_window_set_back_pixmap(rootwindow, pixmap, FALSE);
2006 gdk_window_clear(rootwindow);
2008 g_object_unref(pixmap);
2014 void image_select(ImageWindow *imd, gboolean select)
2016 if (!imd->has_frame) return;
2020 gtk_widget_set_state(imd->widget, GTK_STATE_SELECTED);
2021 gtk_widget_set_state(imd->pr, GTK_STATE_NORMAL); /* do not propagate */
2024 gtk_widget_set_state(imd->widget, GTK_STATE_NORMAL);
2027 void image_set_selectable(ImageWindow *imd, gboolean selectable)
2029 if (!imd->has_frame) return;
2031 gtk_frame_set_shadow_type(GTK_FRAME(imd->frame), GTK_SHADOW_NONE);
2032 gtk_container_set_border_width(GTK_CONTAINER(imd->frame), selectable ? 4 : 0);
2035 void image_grab_focus(ImageWindow *imd)
2039 gtk_widget_grab_focus(imd->frame);
2043 gtk_widget_grab_focus(imd->widget);
2049 *-------------------------------------------------------------------
2051 *-------------------------------------------------------------------
2054 static void image_options_set(ImageWindow *imd)
2056 g_object_set(G_OBJECT(imd->pr), "zoom_quality", options->image.zoom_quality,
2057 "zoom_2pass", options->image.zoom_2pass,
2058 "zoom_expand", options->image.zoom_to_fit_allow_expand,
2059 "scroll_reset", options->image.scroll_reset_method,
2060 "cache_display", options->image.tile_cache_max,
2061 "window_fit", (imd->top_window_sync && options->image.fit_window_to_image),
2062 "window_limit", options->image.limit_window_size,
2063 "window_limit_value", options->image.max_window_size,
2064 "autofit_limit", options->image.limit_autofit_size,
2065 "autofit_limit_value", options->image.max_autofit_size,
2066 "enlargement_limit_value", options->image.max_enlargement_size,
2070 pixbuf_renderer_set_parent((PixbufRenderer *)imd->pr, (GtkWindow *)imd->top_window);
2072 image_stereo_set(imd, options->stereo.mode);
2073 pixbuf_renderer_stereo_fixed_set((PixbufRenderer *)imd->pr,
2074 options->stereo.fixed_w, options->stereo.fixed_h,
2075 options->stereo.fixed_x1, options->stereo.fixed_y1,
2076 options->stereo.fixed_x2, options->stereo.fixed_y2);
2079 void image_options_sync(void)
2091 image_options_set(imd);
2096 *-------------------------------------------------------------------
2098 *-------------------------------------------------------------------
2101 static void image_free(ImageWindow *imd)
2103 image_list = g_list_remove(image_list, imd);
2105 if (imd->auto_refresh && imd->image_fd)
2106 file_data_unregister_real_time_monitor(imd->image_fd);
2108 file_data_unregister_notify_func(image_notify_cb, imd);
2112 image_read_ahead_cancel(imd);
2114 file_data_unref(imd->image_fd);
2116 g_free(imd->title_right);
2120 static void image_destroy_cb(GtkWidget *widget, gpointer data)
2122 ImageWindow *imd = data;
2125 #if GTK_CHECK_VERSION(3,0,0)
2126 gboolean selectable_frame_draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
2128 GtkAllocation allocation;
2129 gtk_widget_get_allocation(widget, &allocation);
2130 gtk_paint_flat_box(gtk_widget_get_style(widget),
2132 gtk_widget_get_state(widget),
2133 gtk_frame_get_shadow_type(GTK_FRAME(widget)),
2136 allocation.x + 3, allocation.y + 3,
2137 allocation.width - 6, allocation.height - 6);
2139 if (gtk_widget_has_focus(widget))
2141 gtk_paint_focus(gtk_widget_get_style(widget), cr, GTK_STATE_ACTIVE,
2142 widget, "image_window",
2143 allocation.x, allocation.y,
2144 allocation.width - 1, allocation.height - 1);
2148 gtk_paint_shadow(gtk_widget_get_style(widget), cr, GTK_STATE_NORMAL, GTK_SHADOW_IN,
2149 widget, "image_window",
2150 allocation.x, allocation.y,
2151 allocation.width - 1, allocation.height - 1);
2157 gboolean selectable_frame_expose_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
2159 GtkAllocation allocation;
2160 gtk_widget_get_allocation(widget, &allocation);
2161 gtk_paint_flat_box(gtk_widget_get_style(widget),
2162 gtk_widget_get_window(widget),
2163 gtk_widget_get_state(widget),
2164 gtk_frame_get_shadow_type(GTK_FRAME(widget)),
2168 allocation.x + 3, allocation.y + 3,
2169 allocation.width - 6, allocation.height - 6);
2171 if (gtk_widget_has_focus(widget))
2173 gtk_paint_focus(gtk_widget_get_style(widget), gtk_widget_get_window(widget), GTK_STATE_ACTIVE,
2174 &event->area, widget, "image_window",
2175 allocation.x, allocation.y,
2176 allocation.width - 1, allocation.height - 1);
2180 gtk_paint_shadow(gtk_widget_get_style(widget), gtk_widget_get_window(widget), GTK_STATE_NORMAL, GTK_SHADOW_IN,
2181 &event->area, widget, "image_window",
2182 allocation.x, allocation.y,
2183 allocation.width - 1, allocation.height - 1);
2190 void image_set_frame(ImageWindow *imd, gboolean frame)
2194 if (frame == imd->has_frame) return;
2196 gtk_widget_hide(imd->pr);
2200 imd->frame = gtk_frame_new(NULL);
2201 DEBUG_NAME(imd->frame);
2202 g_object_ref(imd->pr);
2203 if (imd->has_frame != -1) gtk_container_remove(GTK_CONTAINER(imd->widget), imd->pr);
2204 gtk_container_add(GTK_CONTAINER(imd->frame), imd->pr);
2206 g_object_unref(imd->pr);
2207 gtk_widget_set_can_focus(imd->frame, TRUE);
2208 gtk_widget_set_app_paintable(imd->frame, TRUE);
2210 #if GTK_CHECK_VERSION(3,0,0)
2211 g_signal_connect(G_OBJECT(imd->frame), "draw",
2212 G_CALLBACK(selectable_frame_draw_cb), NULL);
2214 g_signal_connect(G_OBJECT(imd->frame), "expose_event",
2215 G_CALLBACK(selectable_frame_expose_cb), NULL);
2217 g_signal_connect(G_OBJECT(imd->frame), "focus_in_event",
2218 G_CALLBACK(image_focus_in_cb), imd);
2220 gtk_box_pack_start(GTK_BOX(imd->widget), imd->frame, TRUE, TRUE, 0);
2221 gtk_widget_show(imd->frame);
2225 g_object_ref(imd->pr);
2228 gtk_container_remove(GTK_CONTAINER(imd->frame), imd->pr);
2229 gtk_widget_destroy(imd->frame);
2232 gtk_box_pack_start(GTK_BOX(imd->widget), imd->pr, TRUE, TRUE, 0);
2234 g_object_unref(imd->pr);
2237 gtk_widget_show(imd->pr);
2239 imd->has_frame = frame;
2242 ImageWindow *image_new(gboolean frame)
2246 imd = g_new0(ImageWindow, 1);
2248 imd->unknown = TRUE;
2249 imd->has_frame = -1; /* not initialized; for image_set_frame */
2250 imd->delay_alter_type = ALTER_NONE;
2251 imd->state = IMAGE_STATE_NONE;
2252 imd->color_profile_from_image = COLOR_PROFILE_NONE;
2253 imd->orientation = 1;
2255 imd->pr = GTK_WIDGET(pixbuf_renderer_new());
2256 DEBUG_NAME(imd->pr);
2258 image_options_set(imd);
2260 imd->widget = gtk_vbox_new(0, 0);
2261 DEBUG_NAME(imd->widget);
2263 image_set_frame(imd, frame);
2265 image_set_selectable(imd, 0);
2267 g_signal_connect(G_OBJECT(imd->pr), "clicked",
2268 G_CALLBACK(image_click_cb), imd);
2269 g_signal_connect(G_OBJECT(imd->pr), "button_press_event",
2270 G_CALLBACK(image_press_cb), imd);
2271 g_signal_connect(G_OBJECT(imd->pr), "scroll_notify",
2272 G_CALLBACK(image_scroll_notify_cb), imd);
2274 g_signal_connect(G_OBJECT(imd->pr), "scroll_event",
2275 G_CALLBACK(image_scroll_cb), imd);
2277 g_signal_connect(G_OBJECT(imd->pr), "destroy",
2278 G_CALLBACK(image_destroy_cb), imd);
2280 g_signal_connect(G_OBJECT(imd->pr), "zoom",
2281 G_CALLBACK(image_zoom_cb), imd);
2282 g_signal_connect(G_OBJECT(imd->pr), "render_complete",
2283 G_CALLBACK(image_render_complete_cb), imd);
2284 g_signal_connect(G_OBJECT(imd->pr), "drag",
2285 G_CALLBACK(image_drag_cb), imd);
2287 file_data_register_notify_func(image_notify_cb, imd, NOTIFY_PRIORITY_LOW);
2289 image_list = g_list_append(image_list, imd);
2294 void image_get_rectangle(gint *x1, gint *y1, gint *x2, gint *y2)
2302 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */