Fix warnings in exiv2
[geeqie.git] / src / image.cc
1 /*
2  * Copyright (C) 2006 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "image.h"
23
24 #include <cmath>
25 #include <cstring>
26
27 #include <cairo.h>
28 #include <glib-object.h>
29
30 #include "collect-table.h"
31 #include "collect.h"
32 #include "color-man.h"
33 #include "compat.h"
34 #include "debug.h"
35 #include "exif.h"
36 #include "filecache.h"
37 #include "filedata.h"
38 #include "history-list.h"
39 #include "image-load.h"
40 #include "intl.h"
41 #include "layout-image.h"
42 #include "layout.h"
43 #include "metadata.h"
44 #include "options.h"
45 #include "pixbuf-renderer.h"
46 #include "pixbuf-util.h"
47 #include "ui-fileops.h"
48 #include "ui-misc.h"
49
50 struct ExifData;
51 struct FileCacheData;
52
53 static GList *image_list = nullptr;
54
55 static void image_read_ahead_start(ImageWindow *imd);
56 static void image_cache_set(ImageWindow *imd, FileData *fd);
57
58 // For draw rectangle function
59 static gint pixbuf_start_x;
60 static gint pixbuf_start_y;
61 static gint image_start_x;
62 static gint image_start_y;
63 static gint rect_x1, rect_x2, rect_y1, rect_y2;
64 static gint rect_id = 0;
65
66 /*
67  *-------------------------------------------------------------------
68  * 'signals'
69  *-------------------------------------------------------------------
70  */
71
72 static void image_click_cb(PixbufRenderer *, GdkEventButton *event, gpointer data)
73 {
74         auto imd = static_cast<ImageWindow *>(data);
75         if (!options->image_lm_click_nav && event->button == MOUSE_BUTTON_MIDDLE)
76                 {
77                 imd->mouse_wheel_mode = !imd->mouse_wheel_mode;
78                 }
79
80         if (imd->func_button)
81                 {
82                 imd->func_button(imd, event, imd->data_button);
83                 }
84 }
85
86 static void switch_coords_orientation(ImageWindow *imd, gint x, gint y, gint width, gint height)
87 {
88         switch (imd->orientation)
89                 {
90                 case EXIF_ORIENTATION_TOP_LEFT:
91                         /* normal -- nothing to do */
92                         rect_x1 = image_start_x;
93                         rect_y1 = image_start_y;
94                         rect_x2 = x;
95                         rect_y2 = y;
96                         break;
97                 case EXIF_ORIENTATION_TOP_RIGHT:
98                         /* mirrored */
99                         rect_x1 = width - x;
100                         rect_y1 = image_start_y;
101                         rect_x2 = width - image_start_x;
102                         rect_y2 = y;
103                         break;
104                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
105                         /* upside down */
106                         rect_x1 = width - x;
107                         rect_y1 = height - y;
108                         rect_x2 = width - image_start_x;
109                         rect_y2 = height - image_start_y;
110                         break;
111                 case EXIF_ORIENTATION_BOTTOM_LEFT:
112                         /* flipped */
113                         rect_x1 = image_start_x;
114                         rect_y1 = height - y;
115                         rect_x2 = x;
116                         rect_y2 = height - image_start_y;
117                         break;
118                 case EXIF_ORIENTATION_LEFT_TOP:
119                         /* left mirrored */
120                         rect_x1 = image_start_y;
121                         rect_y1 = image_start_x;
122                         rect_x2 = y;
123                         rect_y2 = x;
124                         break;
125                 case EXIF_ORIENTATION_RIGHT_TOP:
126                         /* rotated -90 (270) */
127                         rect_x1 = image_start_y;
128                         rect_y1 = width - x;
129                         rect_x2 = y;
130                         rect_y2 = width - image_start_x;
131                         break;
132                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
133                         /* right mirrored */
134                         rect_x1 = height - y;
135                         rect_y1 = width - x;
136                         rect_x2 = height - image_start_y;
137                         rect_y2 = width - image_start_x;
138                         break;
139                 case EXIF_ORIENTATION_LEFT_BOTTOM:
140                         /* rotated 90 */
141                         rect_x1 = height - y;
142                         rect_y1 = image_start_x;
143                         rect_x2 = height - image_start_y;
144                         rect_y2 = x;
145                         break;
146                 default:
147                         /* The other values are out of range */
148                         break;
149                 }
150 }
151
152 static void image_press_cb(PixbufRenderer *pr, GdkEventButton *event, gpointer data)
153 {
154         auto imd = static_cast<ImageWindow *>(data);
155         LayoutWindow *lw;
156         gint x_pixel;
157         gint y_pixel;
158
159         if(options->draw_rectangle)
160                 {
161                 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
162
163                 pixbuf_start_x = event->x;
164                 pixbuf_start_y = event->y;
165
166                 if (x_pixel == -1)
167                         {
168                         image_start_x = 0;
169                         }
170                 else
171                         {
172                         image_start_x = x_pixel;
173                         }
174
175                 if (y_pixel == -1)
176                         {
177                         image_start_y = 0;
178                         }
179                 else
180                         {
181                         image_start_y = y_pixel;
182                         }
183                 }
184
185         if (rect_id)
186                 {
187                 pixbuf_renderer_overlay_remove(reinterpret_cast<PixbufRenderer *>(imd->pr), rect_id);
188                 }
189
190         lw = layout_find_by_image(imd);
191         if (!lw)
192                 {
193                 layout_valid(&lw);
194                 }
195
196         if (lw && event->button == MOUSE_BUTTON_LEFT && event->type == GDK_2BUTTON_PRESS
197                                                                                                 && !options->image_lm_click_nav)
198                 {
199                 layout_image_full_screen_toggle(lw);
200                 }
201 }
202
203 static void image_release_cb(PixbufRenderer *, GdkEventButton *event, gpointer data)
204 {
205         auto imd = static_cast<ImageWindow *>(data);
206         LayoutWindow *lw;
207
208         lw = layout_find_by_image(imd);
209         if (!lw)
210                 {
211                 layout_valid(&lw);
212                 }
213
214         defined_mouse_buttons(nullptr, event, lw);
215 }
216
217 static void image_drag_cb(PixbufRenderer *pr, GdkEventMotion *event, gpointer data)
218 {
219         auto imd = static_cast<ImageWindow *>(data);
220         gint width;
221         gint height;
222         gint rect_width;
223         gint rect_height;
224         GdkPixbuf *rect_pixbuf;
225         gint x_pixel;
226         gint y_pixel;
227         gint image_x_pixel;
228         gint image_y_pixel;
229
230         if (options->draw_rectangle)
231                 {
232                 pixbuf_renderer_get_image_size(pr, &width, &height);
233                 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
234
235                 if (x_pixel == -1)
236                         {
237                         image_x_pixel = width;
238                         }
239                 else
240                         {
241                         image_x_pixel = x_pixel;
242                         }
243
244                 if (y_pixel == -1)
245                         {
246                         image_y_pixel = height;
247                         }
248                 else
249                         {
250                         image_y_pixel = y_pixel;
251                         }
252
253                 switch_coords_orientation(imd, image_x_pixel, image_y_pixel, width, height);
254                 if (rect_id)
255                         {
256                         pixbuf_renderer_overlay_remove(reinterpret_cast<PixbufRenderer *>(imd->pr), rect_id);
257                         }
258
259                 rect_width = pr->drag_last_x - pixbuf_start_x;
260                 if (rect_width <= 0)
261                         {
262                         rect_width = 1;
263                         }
264                 rect_height = pr->drag_last_y - pixbuf_start_y;
265                 if (rect_height <= 0)
266                         {
267                         rect_height = 1;
268                         }
269
270                 rect_pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, rect_width, rect_height);
271                 pixbuf_set_rect_fill(rect_pixbuf, 0, 0, rect_width, rect_height, 255, 255, 255, 0);
272                 pixbuf_set_rect(rect_pixbuf, 1, 1, rect_width-2, rect_height - 2, 0, 0, 0, 255, 1, 1, 1, 1);
273                 pixbuf_set_rect(rect_pixbuf, 2, 2, rect_width-4, rect_height - 4, 255, 255, 255, 255, 1, 1, 1, 1);
274
275                 rect_id = pixbuf_renderer_overlay_add(reinterpret_cast<PixbufRenderer *>(imd->pr), rect_pixbuf, pixbuf_start_x, pixbuf_start_y, OVL_NORMAL);
276                 }
277
278         pixbuf_renderer_get_scaled_size(pr, &width, &height);
279
280         if (imd->func_drag)
281                 {
282                 imd->func_drag(imd, event,
283                                static_cast<gfloat>(pr->drag_last_x - event->x) / width,
284                                static_cast<gfloat>(pr->drag_last_y - event->y) / height,
285                                imd->data_button);
286                 }
287 }
288
289 static void image_scroll_notify_cb(PixbufRenderer *pr, gpointer data)
290 {
291         auto imd = static_cast<ImageWindow *>(data);
292
293         if (imd->func_scroll_notify && pr->scale)
294                 {
295                 imd->func_scroll_notify(imd,
296                                         static_cast<gint>(static_cast<gdouble>(pr->x_scroll) / pr->scale),
297                                         static_cast<gint>(static_cast<gdouble>(pr->y_scroll) / pr->scale),
298                                         static_cast<gint>(static_cast<gdouble>(pr->image_width) - pr->vis_width / pr->scale),
299                                         static_cast<gint>(static_cast<gdouble>(pr->image_height) - pr->vis_height / pr->scale),
300                                         imd->data_scroll_notify);
301                 }
302 }
303
304 static void image_update_util(ImageWindow *imd)
305 {
306         if (imd->func_update) imd->func_update(imd, imd->data_update);
307 }
308
309
310 static void image_complete_util(ImageWindow *imd, gboolean preload)
311 {
312         if (imd->il && image_get_pixbuf(imd) != image_loader_get_pixbuf(imd->il)) return;
313
314         DEBUG_1("%s image load completed \"%s\" (%s)", get_exec_time(),
315                           (preload) ? (imd->read_ahead_fd ? imd->read_ahead_fd->path : "null") :
316                                       (imd->image_fd ? imd->image_fd->path : "null"),
317                           (preload) ? "preload" : "current");
318
319         if (!preload) imd->completed = TRUE;
320         if (imd->func_complete) imd->func_complete(imd, preload, imd->data_complete);
321 }
322
323 static void image_render_complete_cb(PixbufRenderer *, gpointer data)
324 {
325         auto imd = static_cast<ImageWindow *>(data);
326
327         image_complete_util(imd, FALSE);
328 }
329
330 static void image_state_set(ImageWindow *imd, ImageState state)
331 {
332         if (state == IMAGE_STATE_NONE)
333                 {
334                 imd->state = state;
335                 }
336         else
337                 {
338                 imd->state = static_cast<ImageState>(imd->state | state);
339                 }
340         if (imd->func_state) imd->func_state(imd, state, imd->data_state);
341 }
342
343 static void image_state_unset(ImageWindow *imd, ImageState state)
344 {
345         imd->state = static_cast<ImageState>(imd->state & ~state);
346         if (imd->func_state) imd->func_state(imd, state, imd->data_state);
347 }
348
349 static void image_zoom_cb(PixbufRenderer *, gdouble, gpointer data)
350 {
351         auto imd = static_cast<ImageWindow *>(data);
352
353         if (imd->title_show_zoom) image_update_title(imd);
354         image_state_set(imd, IMAGE_STATE_IMAGE);
355         image_update_util(imd);
356 }
357
358 /*
359  *-------------------------------------------------------------------
360  * misc
361  *-------------------------------------------------------------------
362  */
363
364 void image_update_title(ImageWindow *imd)
365 {
366         gchar *title = nullptr;
367         gchar *zoom = nullptr;
368         gchar *collection = nullptr;
369         LayoutWindow *lw;
370         gchar *lw_ident = nullptr;
371
372         if (!imd->top_window) return;
373
374         if (imd->collection && collection_to_number(imd->collection) >= 0)
375                 {
376                 const gchar *name = imd->collection->name;
377                 if (!name) name = _("Untitled");
378                 collection = g_strdup_printf(_(" (Collection %s)"), name);
379                 }
380
381         if (imd->title_show_zoom)
382                 {
383                 gchar *buf = image_zoom_get_as_text(imd);
384                 zoom = g_strconcat(" [", buf, "]", NULL);
385                 g_free(buf);
386                 }
387
388         lw = layout_find_by_image(imd);
389         if (lw)
390                 {
391                 lw_ident = g_strconcat(" (", lw->options.id, ")", NULL);
392                 }
393
394         title = g_strdup_printf("%s%s%s%s%s%s%s",
395                 imd->title ? imd->title : "",
396                 imd->image_fd ? imd->image_fd->name : "",
397                 zoom ? zoom : "",
398                 collection ? collection : "",
399                 imd->image_fd ? " - " : "",
400                 imd->title_right ? imd->title_right : "",
401                 options->show_window_ids ? (lw_ident ? lw_ident : "") : ""
402                 );
403         if (lw_ident)
404                 {
405                 g_free(lw_ident);
406                 }
407
408         gtk_window_set_title(GTK_WINDOW(imd->top_window), title);
409
410         g_free(title);
411         g_free(zoom);
412         g_free(collection);
413 }
414
415 /*
416  *-------------------------------------------------------------------
417  * rotation, flip, etc.
418  *-------------------------------------------------------------------
419  */
420 static gboolean image_get_x11_screen_profile(ImageWindow *imd, guchar **screen_profile, gint *screen_profile_len)
421 {
422         GdkScreen *screen = gtk_widget_get_screen(imd->widget);;
423         GdkAtom    type   = GDK_NONE;
424         gint       format = 0;
425
426         return (gdk_property_get(gdk_screen_get_root_window(screen),
427                                  gdk_atom_intern ("_ICC_PROFILE", FALSE),
428                                  GDK_NONE,
429                                  0, 64 * 1024 * 1024, FALSE,
430                                  &type, &format, screen_profile_len, screen_profile) && *screen_profile_len > 0);
431 }
432
433 static gboolean image_post_process_color(ImageWindow *imd, gint start_row, gboolean run_in_bg)
434 {
435         ColorMan *cm;
436         ColorManProfileType input_type;
437         ColorManProfileType screen_type;
438         const gchar *input_file = nullptr;
439         const gchar *screen_file = nullptr;
440         guchar *profile = nullptr;
441         guint profile_len;
442         guchar *screen_profile = nullptr;
443         gint screen_profile_len;
444         ExifData *exif;
445
446         if (imd->cm) return FALSE;
447
448         if (imd->color_profile_input >= COLOR_PROFILE_FILE &&
449             imd->color_profile_input <  COLOR_PROFILE_FILE + COLOR_PROFILE_INPUTS)
450                 {
451                 const gchar *file = options->color_profile.input_file[imd->color_profile_input - COLOR_PROFILE_FILE];
452
453                 if (!is_readable_file(file)) return FALSE;
454
455                 input_type = COLOR_PROFILE_FILE;
456                 input_file = file;
457                 }
458         else if (imd->color_profile_input >= COLOR_PROFILE_SRGB &&
459                  imd->color_profile_input <  COLOR_PROFILE_FILE)
460                 {
461                 input_type = static_cast<ColorManProfileType>(imd->color_profile_input);
462                 input_file = nullptr;
463                 }
464         else
465                 {
466                 return FALSE;
467                 }
468
469         if (options->color_profile.use_x11_screen_profile &&
470             image_get_x11_screen_profile(imd, &screen_profile, &screen_profile_len))
471                 {
472                 screen_type = COLOR_PROFILE_MEM;
473                 DEBUG_1("Using X11 screen profile, length: %d", screen_profile_len);
474                 }
475         else if (options->color_profile.screen_file &&
476             is_readable_file(options->color_profile.screen_file))
477                 {
478                 screen_type = COLOR_PROFILE_FILE;
479                 screen_file = options->color_profile.screen_file;
480                 }
481         else
482                 {
483                 screen_type = COLOR_PROFILE_SRGB;
484                 screen_file = nullptr;
485                 }
486
487
488         imd->color_profile_from_image = COLOR_PROFILE_NONE;
489
490         exif = exif_read_fd(imd->image_fd);
491
492         if (exif)
493                 {
494                 if (g_strcmp0(imd->image_fd->format_name, "heif") == 0)
495                         {
496                         profile = heif_color_profile(imd->image_fd, &profile_len);
497                         }
498
499                 if (!profile)
500                         {
501                         profile = exif_get_color_profile(exif, &profile_len);
502                         }
503
504                 if (profile)
505                         {
506                         if (!imd->color_profile_use_image)
507                                 {
508                                 g_free(profile);
509                                 profile = nullptr;
510                                 }
511                         DEBUG_1("Found embedded color profile");
512                         imd->color_profile_from_image = COLOR_PROFILE_MEM;
513                         }
514                 else
515                         {
516                         gchar *interop_index = exif_get_data_as_text(exif, "Exif.Iop.InteroperabilityIndex");
517
518                         if (interop_index)
519                                 {
520                                 /* Exif 2.21 specification */
521                                 if (!strcmp(interop_index, "R98"))
522                                         {
523                                         imd->color_profile_from_image = COLOR_PROFILE_SRGB;
524                                         DEBUG_1("Found EXIF 2.21 ColorSpace of sRGB");
525                                         }
526                                 else if (!strcmp(interop_index, "R03"))
527                                         {
528                                         imd->color_profile_from_image = COLOR_PROFILE_ADOBERGB;
529                                         DEBUG_1("Found EXIF 2.21 ColorSpace of AdobeRGB");
530                                         }
531                                 g_free(interop_index);
532                                 }
533                         else
534                                 {
535                                 gint cs;
536
537                                 /* ColorSpace == 1 specifies sRGB per EXIF 2.2 */
538                                 if (!exif_get_integer(exif, "Exif.Photo.ColorSpace", &cs)) cs = 0;
539                                 if (cs == 1)
540                                         {
541                                         imd->color_profile_from_image = COLOR_PROFILE_SRGB;
542                                         DEBUG_1("Found EXIF 2.2 ColorSpace of sRGB");
543                                         }
544                                 else if (cs == 2)
545                                         {
546                                         /* non-standard way of specifying AdobeRGB (used by some software) */
547                                         imd->color_profile_from_image = COLOR_PROFILE_ADOBERGB;
548                                         DEBUG_1("Found EXIF 2.2 ColorSpace of AdobeRGB");
549                                         }
550                                 }
551
552                         if (imd->color_profile_use_image && imd->color_profile_from_image != COLOR_PROFILE_NONE)
553                                {
554                                input_type = static_cast<ColorManProfileType>(imd->color_profile_from_image);
555                                input_file = nullptr;
556                                }
557                         }
558
559                 exif_free_fd(imd->image_fd, exif);
560                 }
561
562
563         if (profile)
564                 {
565                 cm = color_man_new_embedded(run_in_bg ? imd : nullptr, nullptr,
566                                             profile, profile_len,
567                                             screen_type, screen_file, screen_profile, screen_profile_len);
568                 g_free(profile);
569                 }
570         else
571                 {
572                 cm = color_man_new(run_in_bg ? imd : nullptr, nullptr,
573                                    input_type, input_file,
574                                    screen_type, screen_file, screen_profile, screen_profile_len);
575                 }
576
577         if (cm)
578                 {
579                 if (start_row > 0)
580                         {
581                         cm->row = start_row;
582                         cm->incremental_sync = TRUE;
583                         }
584
585                 imd->cm = cm;
586                 }
587
588         image_update_util(imd);
589
590         if (screen_profile)
591                 {
592                 g_free(screen_profile);
593                 screen_profile = nullptr;
594                 }
595
596         return !!cm;
597 }
598
599
600 static void image_post_process_tile_color_cb(PixbufRenderer *, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h, gpointer data)
601 {
602         auto imd = static_cast<ImageWindow *>(data);
603         if (imd->cm) color_man_correct_region(static_cast<ColorMan *>(imd->cm), *pixbuf, x, y, w, h);
604         if (imd->desaturate) pixbuf_desaturate_rect(*pixbuf, x, y, w, h);
605         if (imd->overunderexposed) pixbuf_highlight_overunderexposed(*pixbuf, x, y, w, h);
606 }
607
608 void image_alter_orientation(ImageWindow *imd, FileData *fd_n, AlterType type)
609 {
610         static const gint rotate_90[]    = {1,   6, 7, 8, 5, 2, 3, 4, 1};
611         static const gint rotate_90_cc[] = {1,   8, 5, 6, 7, 4, 1, 2, 3};
612         static const gint rotate_180[]   = {1,   3, 4, 1, 2, 7, 8, 5, 6};
613         static const gint mirror[]       = {1,   2, 1, 4, 3, 6, 5, 8, 7};
614         static const gint flip[]         = {1,   4, 3, 2, 1, 8, 7, 6, 5};
615
616         gint orientation;
617
618         if (!imd || !imd->pr || !imd->image_fd || !fd_n) return;
619
620         orientation = EXIF_ORIENTATION_TOP_LEFT;
621         {
622         if (fd_n->user_orientation)
623                 {
624                 orientation = fd_n->user_orientation;
625                 }
626         else
627                 if (options->metadata.write_orientation)
628                         {
629                         if (g_strcmp0(imd->image_fd->format_name, "heif") == 0)
630                                 {
631                                 orientation = EXIF_ORIENTATION_TOP_LEFT;
632                                 }
633                         else
634                                 {
635                                 orientation = metadata_read_int(fd_n, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
636                                 }
637                         }
638         }
639
640         switch (type)
641                 {
642                 case ALTER_ROTATE_90:
643                         orientation = rotate_90[orientation];
644                         break;
645                 case ALTER_ROTATE_90_CC:
646                         orientation = rotate_90_cc[orientation];
647                         break;
648                 case ALTER_ROTATE_180:
649                         orientation = rotate_180[orientation];
650                         break;
651                 case ALTER_MIRROR:
652                         orientation = mirror[orientation];
653                         break;
654                 case ALTER_FLIP:
655                         orientation = flip[orientation];
656                         break;
657                 case ALTER_NONE:
658                         orientation = fd_n->exif_orientation ? fd_n->exif_orientation : 1;
659                         break;
660                 default:
661                         return;
662                         break;
663                 }
664
665         if (orientation != (fd_n->exif_orientation ? fd_n->exif_orientation : 1))
666                 {
667                 if (g_strcmp0(fd_n->format_name, "heif") != 0)
668                         {
669                         if (!options->metadata.write_orientation)
670                                 {
671                                 /* user_orientation does not work together with options->metadata.write_orientation,
672                                    use either one or the other.
673                                    we must however handle switching metadata.write_orientation on and off, therefore
674                                    we just disable referencing new fd's, not unreferencing the old ones
675                                 */
676                                 if (fd_n->user_orientation == 0) file_data_ref(fd_n);
677                                 fd_n->user_orientation = orientation;
678                                 }
679                         }
680                 else
681                         {
682                         if (fd_n->user_orientation == 0) file_data_ref(fd_n);
683                         fd_n->user_orientation = orientation;
684                         }
685                 }
686         else
687                 {
688                 if (fd_n->user_orientation != 0) file_data_unref(fd_n);
689                 fd_n->user_orientation = 0;
690                 }
691
692         if (g_strcmp0(fd_n->format_name, "heif") != 0)
693                 {
694                 if (options->metadata.write_orientation)
695                         {
696                         if (type == ALTER_NONE)
697                                 {
698                                 metadata_write_revert(fd_n, ORIENTATION_KEY);
699                                 }
700                         else
701                                 {
702                                 metadata_write_int(fd_n, ORIENTATION_KEY, orientation);
703                                 }
704                         }
705                 }
706
707         if (imd->image_fd == fd_n && (!options->metadata.write_orientation || options->image.exif_rotate_enable))
708                 {
709                 imd->orientation = orientation;
710                 pixbuf_renderer_set_orientation(reinterpret_cast<PixbufRenderer *>(imd->pr), orientation);
711                 }
712 }
713
714 void image_set_desaturate(ImageWindow *imd, gboolean desaturate)
715 {
716         imd->desaturate = desaturate;
717         if (imd->cm || imd->desaturate || imd->overunderexposed)
718                 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), image_post_process_tile_color_cb, imd, (imd->cm != nullptr) );
719         else
720                 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), nullptr, nullptr, TRUE);
721         pixbuf_renderer_set_orientation(reinterpret_cast<PixbufRenderer *>(imd->pr), imd->orientation);
722 }
723
724 gboolean image_get_desaturate(ImageWindow *imd)
725 {
726         return imd->desaturate;
727 }
728
729 void image_set_overunderexposed(ImageWindow *imd, gboolean overunderexposed)
730 {
731         imd->overunderexposed = overunderexposed;
732         if (imd->cm || imd->desaturate || imd->overunderexposed)
733                 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), image_post_process_tile_color_cb, imd, (imd->cm != nullptr) );
734         else
735                 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), nullptr, nullptr, TRUE);
736         pixbuf_renderer_set_orientation(reinterpret_cast<PixbufRenderer *>(imd->pr), imd->orientation);
737 }
738
739 void image_set_ignore_alpha(ImageWindow *imd, gboolean ignore_alpha)
740 {
741    pixbuf_renderer_set_ignore_alpha(reinterpret_cast<PixbufRenderer *>(imd->pr), ignore_alpha);
742 }
743
744 /*
745  *-------------------------------------------------------------------
746  * read ahead (prebuffer)
747  *-------------------------------------------------------------------
748  */
749
750 static void image_read_ahead_cancel(ImageWindow *imd)
751 {
752         DEBUG_1("%s read ahead cancelled for :%s", get_exec_time(), imd->read_ahead_fd ? imd->read_ahead_fd->path : "null");
753
754         image_loader_free(imd->read_ahead_il);
755         imd->read_ahead_il = nullptr;
756
757         file_data_unref(imd->read_ahead_fd);
758         imd->read_ahead_fd = nullptr;
759 }
760
761 static void image_read_ahead_done_cb(ImageLoader *, gpointer data)
762 {
763         auto imd = static_cast<ImageWindow *>(data);
764
765         if (!imd->read_ahead_fd || !imd->read_ahead_il) return;
766
767         DEBUG_1("%s read ahead done for :%s", get_exec_time(), imd->read_ahead_fd->path);
768
769         if (!imd->read_ahead_fd->pixbuf)
770                 {
771                 imd->read_ahead_fd->pixbuf = image_loader_get_pixbuf(imd->read_ahead_il);
772                 if (imd->read_ahead_fd->pixbuf)
773                         {
774                         g_object_ref(imd->read_ahead_fd->pixbuf);
775                         image_cache_set(imd, imd->read_ahead_fd);
776                         }
777                 }
778         image_loader_free(imd->read_ahead_il);
779         imd->read_ahead_il = nullptr;
780
781         image_complete_util(imd, TRUE);
782 }
783
784 static void image_read_ahead_error_cb(ImageLoader *il, gpointer data)
785 {
786         /* we even treat errors as success, maybe at least some of the file was ok */
787         image_read_ahead_done_cb(il, data);
788 }
789
790 static void image_read_ahead_start(ImageWindow *imd)
791 {
792         /* already started ? */
793         if (!imd->read_ahead_fd || imd->read_ahead_il || imd->read_ahead_fd->pixbuf) return;
794
795         /* still loading ?, do later */
796         if (imd->il /*|| imd->cm*/) return;
797
798         DEBUG_1("%s read ahead started for :%s", get_exec_time(), imd->read_ahead_fd->path);
799
800         imd->read_ahead_il = image_loader_new(imd->read_ahead_fd);
801
802         image_loader_delay_area_ready(imd->read_ahead_il, TRUE); /* we will need the area_ready signals later */
803
804         g_signal_connect(G_OBJECT(imd->read_ahead_il), "error", (GCallback)image_read_ahead_error_cb, imd);
805         g_signal_connect(G_OBJECT(imd->read_ahead_il), "done", (GCallback)image_read_ahead_done_cb, imd);
806
807         if (!image_loader_start(imd->read_ahead_il))
808                 {
809                 image_read_ahead_cancel(imd);
810                 image_complete_util(imd, TRUE);
811                 }
812 }
813
814 static void image_read_ahead_set(ImageWindow *imd, FileData *fd)
815 {
816         if (imd->read_ahead_fd && fd && imd->read_ahead_fd == fd) return;
817
818         image_read_ahead_cancel(imd);
819
820         imd->read_ahead_fd = file_data_ref(fd);
821
822         DEBUG_1("read ahead set to :%s", imd->read_ahead_fd->path);
823
824         image_read_ahead_start(imd);
825 }
826
827 /*
828  *-------------------------------------------------------------------
829  * post buffering
830  *-------------------------------------------------------------------
831  */
832
833 static void image_cache_release_cb(FileData *fd)
834 {
835         g_object_unref(fd->pixbuf);
836         fd->pixbuf = nullptr;
837 }
838
839 static FileCacheData *image_get_cache()
840 {
841         static FileCacheData *cache = nullptr;
842         if (!cache) cache = file_cache_new(image_cache_release_cb, 1);
843         file_cache_set_max_size(cache, static_cast<gulong>(options->image.image_cache_max) * 1048576); /* update from options */
844         return cache;
845 }
846
847 static void image_cache_set(ImageWindow *, FileData *fd)
848 {
849         g_assert(fd->pixbuf);
850
851         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)));
852         file_data_send_notification(fd, NOTIFY_PIXBUF); /* to update histogram */
853 }
854
855 static gint image_cache_get(ImageWindow *imd)
856 {
857         gint success;
858
859         success = file_cache_get(image_get_cache(), imd->image_fd);
860         if (success)
861                 {
862                 g_assert(imd->image_fd->pixbuf);
863                 image_change_pixbuf(imd, imd->image_fd->pixbuf, image_zoom_get(imd), FALSE);
864                 }
865
866         return success;
867 }
868
869 /*
870  *-------------------------------------------------------------------
871  * loading
872  *-------------------------------------------------------------------
873  */
874
875 static void image_load_pixbuf_ready(ImageWindow *imd)
876 {
877         if (image_get_pixbuf(imd) || !imd->il) return;
878
879         image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), FALSE);
880 }
881
882 static void image_load_area_cb(ImageLoader *il, guint x, guint y, guint w, guint h, gpointer data)
883 {
884         auto imd = static_cast<ImageWindow *>(data);
885         PixbufRenderer *pr;
886
887         pr = reinterpret_cast<PixbufRenderer *>(imd->pr);
888
889         if (imd->delay_flip &&
890             pr->pixbuf != image_loader_get_pixbuf(il))
891                 {
892                 return;
893                 }
894
895         if (!pr->pixbuf) image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), TRUE);
896
897         pixbuf_renderer_area_changed(pr, x, y, w, h);
898 }
899
900 static void image_load_done_cb(ImageLoader *, gpointer data)
901 {
902         auto imd = static_cast<ImageWindow *>(data);
903
904         DEBUG_1("%s image done", get_exec_time());
905
906         if (options->image.enable_read_ahead && imd->image_fd && !imd->image_fd->pixbuf && image_loader_get_pixbuf(imd->il))
907                 {
908                 imd->image_fd->pixbuf = static_cast<GdkPixbuf*>(g_object_ref(image_loader_get_pixbuf(imd->il)));
909                 image_cache_set(imd, imd->image_fd);
910                 }
911         /* call the callback triggered by image_state after fd->pixbuf is set */
912         g_object_set(G_OBJECT(imd->pr), "loading", FALSE, NULL);
913         image_state_unset(imd, IMAGE_STATE_LOADING);
914
915         if (!image_loader_get_pixbuf(imd->il))
916                 {
917                 GdkPixbuf *pixbuf = pixbuf_fallback(imd->image_fd, 0, 0);
918
919                 image_change_pixbuf(imd, pixbuf, image_zoom_get(imd), FALSE);
920                 g_object_unref(pixbuf);
921
922                 imd->unknown = TRUE;
923                 }
924         else if (imd->delay_flip &&
925             image_get_pixbuf(imd) != image_loader_get_pixbuf(imd->il))
926                 {
927                 g_object_set(G_OBJECT(imd->pr), "complete", FALSE, NULL);
928                 image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), FALSE);
929                 }
930
931         image_loader_free(imd->il);
932         imd->il = nullptr;
933
934         image_read_ahead_start(imd);
935 }
936
937 static void image_load_size_cb(ImageLoader *, guint width, guint height, gpointer data)
938 {
939         auto imd = static_cast<ImageWindow *>(data);
940
941         DEBUG_1("image_load_size_cb: %dx%d", width, height);
942         pixbuf_renderer_set_size_early(reinterpret_cast<PixbufRenderer *>(imd->pr), width, height);
943 }
944
945 static void image_load_error_cb(ImageLoader *il, gpointer data)
946 {
947         DEBUG_1("%s image error", get_exec_time());
948
949         /* even on error handle it like it was done,
950          * since we have a pixbuf with _something_ */
951
952         image_load_done_cb(il, data);
953 }
954
955 static void image_load_set_signals(ImageWindow *imd, gboolean override_old_signals)
956 {
957         g_assert(imd->il);
958         if (override_old_signals)
959                 {
960                 /* override the old signals */
961                 g_signal_handlers_disconnect_matched(G_OBJECT(imd->il), G_SIGNAL_MATCH_DATA, 0, 0, nullptr, nullptr, imd);
962                 }
963
964         g_signal_connect(G_OBJECT(imd->il), "area_ready", (GCallback)image_load_area_cb, imd);
965         g_signal_connect(G_OBJECT(imd->il), "error", (GCallback)image_load_error_cb, imd);
966         g_signal_connect(G_OBJECT(imd->il), "done", (GCallback)image_load_done_cb, imd);
967         g_signal_connect(G_OBJECT(imd->il), "size_prepared", (GCallback)image_load_size_cb, imd);
968 }
969
970 /* this read ahead is located here merely for the callbacks, above */
971
972 static gboolean image_read_ahead_check(ImageWindow *imd)
973 {
974         if (!imd->read_ahead_fd) return FALSE;
975         if (imd->il) return FALSE;
976
977         if (!imd->image_fd || imd->read_ahead_fd != imd->image_fd)
978                 {
979                 image_read_ahead_cancel(imd);
980                 return FALSE;
981                 }
982
983         if (imd->read_ahead_il)
984                 {
985                 imd->il = imd->read_ahead_il;
986                 imd->read_ahead_il = nullptr;
987
988                 image_load_set_signals(imd, TRUE);
989
990                 g_object_set(G_OBJECT(imd->pr), "loading", TRUE, NULL);
991                 image_state_set(imd, IMAGE_STATE_LOADING);
992
993                 if (!imd->delay_flip)
994                         {
995                         image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), TRUE);
996                         }
997
998                 image_loader_delay_area_ready(imd->il, FALSE); /* send the delayed area_ready signals */
999
1000                 file_data_unref(imd->read_ahead_fd);
1001                 imd->read_ahead_fd = nullptr;
1002                 return TRUE;
1003                 }
1004         if (imd->read_ahead_fd->pixbuf)
1005                 {
1006                 image_change_pixbuf(imd, imd->read_ahead_fd->pixbuf, image_zoom_get(imd), FALSE);
1007
1008                 file_data_unref(imd->read_ahead_fd);
1009                 imd->read_ahead_fd = nullptr;
1010
1011                 return TRUE;
1012                 }
1013
1014         image_read_ahead_cancel(imd);
1015         return FALSE;
1016 }
1017
1018 static gboolean image_load_begin(ImageWindow *imd, FileData *fd)
1019 {
1020         DEBUG_1("%s image begin", get_exec_time());
1021
1022         if (imd->il) return FALSE;
1023
1024         imd->completed = FALSE;
1025         g_object_set(G_OBJECT(imd->pr), "complete", FALSE, NULL);
1026
1027         if (image_cache_get(imd))
1028                 {
1029                 DEBUG_1("from cache: %s", imd->image_fd->path);
1030                 return TRUE;
1031                 }
1032
1033         if (image_read_ahead_check(imd))
1034                 {
1035                 DEBUG_1("from read ahead buffer: %s", imd->image_fd->path);
1036                 return TRUE;
1037                 }
1038
1039         if (!imd->delay_flip && image_get_pixbuf(imd))
1040                 {
1041                 PixbufRenderer *pr;
1042
1043                 pr = PIXBUF_RENDERER(imd->pr);
1044                 if (pr->pixbuf) g_object_unref(pr->pixbuf);
1045                 pr->pixbuf = nullptr;
1046                 }
1047
1048         g_object_set(G_OBJECT(imd->pr), "loading", TRUE, NULL);
1049
1050         imd->il = image_loader_new(fd);
1051
1052         image_load_set_signals(imd, FALSE);
1053
1054         if (!image_loader_start(imd->il))
1055                 {
1056                 DEBUG_1("image start error");
1057
1058                 g_object_set(G_OBJECT(imd->pr), "loading", FALSE, NULL);
1059
1060                 image_loader_free(imd->il);
1061                 imd->il = nullptr;
1062
1063                 image_complete_util(imd, FALSE);
1064
1065                 return FALSE;
1066                 }
1067
1068         image_state_set(imd, IMAGE_STATE_LOADING);
1069
1070 /*
1071         if (!imd->delay_flip && !image_get_pixbuf(imd) && image_loader_get_pixbuf(imd->il))
1072                 {
1073                 image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), TRUE);
1074                 }
1075 */
1076         return TRUE;
1077 }
1078
1079 static void image_reset(ImageWindow *imd)
1080 {
1081         /* stops anything currently being done */
1082
1083         DEBUG_1("%s image reset", get_exec_time());
1084
1085         g_object_set(G_OBJECT(imd->pr), "loading", FALSE, NULL);
1086
1087         image_loader_free(imd->il);
1088         imd->il = nullptr;
1089
1090         color_man_free(static_cast<ColorMan *>(imd->cm));
1091         imd->cm = nullptr;
1092
1093         imd->delay_alter_type = ALTER_NONE;
1094
1095         image_state_set(imd, IMAGE_STATE_NONE);
1096 }
1097
1098 /*
1099  *-------------------------------------------------------------------
1100  * image changer
1101  *-------------------------------------------------------------------
1102  */
1103
1104 static void image_change_complete(ImageWindow *imd, gdouble zoom)
1105 {
1106         image_reset(imd);
1107         imd->unknown = TRUE;
1108
1109         /** @FIXME Might be improved when the wepb animation changes happen */
1110         g_object_set(G_OBJECT(imd->pr), "zoom_2pass", options->image.zoom_2pass, NULL);
1111         g_object_set(G_OBJECT(imd->pr), "zoom_quality", options->image.zoom_quality, NULL);
1112
1113         if (!imd->image_fd)
1114                 {
1115                 image_change_pixbuf(imd, nullptr, zoom, FALSE);
1116                 }
1117         else
1118                 {
1119
1120                 if (is_readable_file(imd->image_fd->path))
1121                         {
1122                         PixbufRenderer *pr;
1123
1124                         pr = PIXBUF_RENDERER(imd->pr);
1125                         pr->zoom = zoom;        /* store the zoom, needed by the loader */
1126
1127                         /* Disable 2-pass for GIFs. Animated GIFs can flicker when enabled
1128                          * Reduce quality to worst but fastest to avoid dropped frames */
1129                         if (g_ascii_strcasecmp(imd->image_fd->extension, ".GIF") == 0)
1130                                 {
1131                                 g_object_set(G_OBJECT(imd->pr), "zoom_2pass", FALSE, NULL);
1132                                 g_object_set(G_OBJECT(imd->pr), "zoom_quality", GDK_INTERP_NEAREST, NULL);
1133                                 }
1134
1135
1136                         if (image_load_begin(imd, imd->image_fd))
1137                                 {
1138                                 imd->unknown = FALSE;
1139                                 }
1140                         }
1141
1142                 if (imd->unknown == TRUE)
1143                         {
1144                         GdkPixbuf *pixbuf;
1145
1146                         pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
1147                         image_change_pixbuf(imd, pixbuf, zoom, FALSE);
1148                         g_object_unref(pixbuf);
1149                         }
1150                 }
1151
1152         image_update_util(imd);
1153 }
1154
1155 static void image_change_real(ImageWindow *imd, FileData *fd,
1156                               CollectionData *cd, CollectInfo *info, gdouble zoom)
1157 {
1158
1159         imd->collection = cd;
1160         imd->collection_info = info;
1161
1162         if (imd->auto_refresh && imd->image_fd)
1163                 file_data_unregister_real_time_monitor(imd->image_fd);
1164
1165         file_data_unref(imd->image_fd);
1166         imd->image_fd = file_data_ref(fd);
1167
1168
1169         image_change_complete(imd, zoom);
1170
1171         image_update_title(imd);
1172         image_state_set(imd, IMAGE_STATE_IMAGE);
1173
1174         if (imd->auto_refresh && imd->image_fd)
1175                 file_data_register_real_time_monitor(imd->image_fd);
1176 }
1177
1178 /*
1179  *-------------------------------------------------------------------
1180  * focus stuff
1181  *-------------------------------------------------------------------
1182  */
1183
1184 static gboolean image_focus_in_cb(GtkWidget *, GdkEventFocus *, gpointer data)
1185 {
1186         auto imd = static_cast<ImageWindow *>(data);
1187
1188         if (imd->func_focus_in)
1189                 {
1190                 imd->func_focus_in(imd, imd->data_focus_in);
1191                 }
1192
1193         return TRUE;
1194 }
1195
1196 static gboolean image_scroll_cb(GtkWidget *, GdkEventScroll *event, gpointer data)
1197 {
1198         auto imd = static_cast<ImageWindow *>(data);
1199         gboolean in_lw = FALSE;
1200         gint i = 0;
1201         LayoutWindow *lw = nullptr;
1202
1203         if (imd->func_scroll && event && event->type == GDK_SCROLL)
1204                 {
1205                 layout_valid(&lw);
1206                 /* check if the image is in a layout window */
1207                 for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1208                         {
1209                         if (imd == lw->split_images[i])
1210                                 {
1211                                 in_lw = TRUE;
1212                                 break;
1213                                 }
1214                         }
1215
1216                 if (in_lw)
1217                         {
1218                         if (lw->options.split_pane_sync)
1219                                 {
1220                                 for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1221                                         {
1222                                         if (lw->split_images[i])
1223                                                 {
1224                                                 layout_image_activate(lw, i, FALSE);
1225                                                 imd->func_scroll(lw->split_images[i], event, lw->split_images[i]->data_scroll);
1226                                                 }
1227                                         }
1228                                 }
1229                         else
1230                                 {
1231                                 imd->func_scroll(imd, event, imd->data_scroll);
1232                                 }
1233                         return TRUE;
1234                         }
1235
1236                 imd->func_scroll(imd, event, imd->data_scroll);
1237                 return TRUE;
1238                 }
1239
1240         return FALSE;
1241 }
1242
1243 /*
1244  *-------------------------------------------------------------------
1245  * public interface
1246  *-------------------------------------------------------------------
1247  */
1248
1249 void image_attach_window(ImageWindow *imd, GtkWidget *window,
1250                          const gchar *title, const gchar *title_right, gboolean show_zoom)
1251 {
1252         LayoutWindow *lw;
1253
1254         imd->top_window = window;
1255         g_free(imd->title);
1256         imd->title = g_strdup(title);
1257         g_free(imd->title_right);
1258         imd->title_right = g_strdup(title_right);
1259         imd->title_show_zoom = show_zoom;
1260
1261         lw = layout_find_by_image(imd);
1262
1263         if (!(options->image.fit_window_to_image && lw && (lw->options.tools_float || lw->options.tools_hidden))) window = nullptr;
1264
1265         pixbuf_renderer_set_parent(reinterpret_cast<PixbufRenderer *>(imd->pr), reinterpret_cast<GtkWindow *>(window));
1266
1267         image_update_title(imd);
1268 }
1269
1270 void image_set_update_func(ImageWindow *imd,
1271                            void (*func)(ImageWindow *imd, gpointer data),
1272                            gpointer data)
1273 {
1274         imd->func_update = func;
1275         imd->data_update = data;
1276 }
1277
1278 void image_set_complete_func(ImageWindow *imd,
1279                              void (*func)(ImageWindow *imd, gboolean preload, gpointer data),
1280                              gpointer data)
1281 {
1282         imd->func_complete = func;
1283         imd->data_complete = data;
1284 }
1285
1286 void image_set_state_func(ImageWindow *imd,
1287                         void (*func)(ImageWindow *imd, ImageState state, gpointer data),
1288                         gpointer data)
1289 {
1290         imd->func_state = func;
1291         imd->data_state = data;
1292 }
1293
1294
1295 void image_set_button_func(ImageWindow *imd,
1296                            void (*func)(ImageWindow *, GdkEventButton *event, gpointer),
1297                            gpointer data)
1298 {
1299         imd->func_button = func;
1300         imd->data_button = data;
1301 }
1302
1303 void image_set_drag_func(ImageWindow *imd,
1304                            void (*func)(ImageWindow *, GdkEventMotion *event, gdouble dx, gdouble dy, gpointer),
1305                            gpointer data)
1306 {
1307         imd->func_drag = func;
1308         imd->data_drag = data;
1309 }
1310
1311 void image_set_scroll_func(ImageWindow *imd,
1312                            void (*func)(ImageWindow *, GdkEventScroll *event, gpointer),
1313                            gpointer data)
1314 {
1315         imd->func_scroll = func;
1316         imd->data_scroll = data;
1317 }
1318
1319 #pragma GCC diagnostic push
1320 #pragma GCC diagnostic ignored "-Wunused-function"
1321 void image_set_scroll_notify_func_unused(ImageWindow *imd,
1322                                   void (*func)(ImageWindow *imd, gint x, gint y, gint width, gint height, gpointer data),
1323                                   gpointer data)
1324 {
1325         imd->func_scroll_notify = func;
1326         imd->data_scroll_notify = data;
1327 }
1328 #pragma GCC diagnostic pop
1329
1330 void image_set_focus_in_func(ImageWindow *imd,
1331                            void (*func)(ImageWindow *, gpointer),
1332                            gpointer data)
1333 {
1334         imd->func_focus_in = func;
1335         imd->data_focus_in = data;
1336 }
1337
1338 /* path, name */
1339
1340 const gchar *image_get_path(ImageWindow *imd)
1341 {
1342         if (imd->image_fd == nullptr) return nullptr;
1343         return imd->image_fd->path;
1344 }
1345
1346 const gchar *image_get_name(ImageWindow *imd)
1347 {
1348         if (imd->image_fd == nullptr) return nullptr;
1349         return imd->image_fd->name;
1350 }
1351
1352 FileData *image_get_fd(ImageWindow *imd)
1353 {
1354         return imd->image_fd;
1355 }
1356
1357 /* merely changes path string, does not change the image! */
1358 void image_set_fd(ImageWindow *imd, FileData *fd)
1359 {
1360         if (imd->auto_refresh && imd->image_fd)
1361                 file_data_unregister_real_time_monitor(imd->image_fd);
1362
1363         file_data_unref(imd->image_fd);
1364         imd->image_fd = file_data_ref(fd);
1365
1366         image_update_title(imd);
1367         image_state_set(imd, IMAGE_STATE_IMAGE);
1368
1369         if (imd->auto_refresh && imd->image_fd)
1370                 file_data_register_real_time_monitor(imd->image_fd);
1371 }
1372
1373 /* load a new image */
1374
1375 void image_change_fd(ImageWindow *imd, FileData *fd, gdouble zoom)
1376 {
1377         if (imd->image_fd == fd) return;
1378
1379         image_change_real(imd, fd, nullptr, nullptr, zoom);
1380 }
1381
1382 gboolean image_get_image_size(ImageWindow *imd, gint *width, gint *height)
1383 {
1384         return pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), width, height);
1385 }
1386
1387 GdkPixbuf *image_get_pixbuf(ImageWindow *imd)
1388 {
1389         return pixbuf_renderer_get_pixbuf(reinterpret_cast<PixbufRenderer *>(imd->pr));
1390 }
1391
1392 void image_change_pixbuf(ImageWindow *imd, GdkPixbuf *pixbuf, gdouble zoom, gboolean lazy)
1393 {
1394         LayoutWindow *lw;
1395         StereoPixbufData stereo_data = STEREO_PIXBUF_DEFAULT;
1396         /* read_exif and similar functions can actually notice that the file has changed and trigger
1397            a notification that removes the pixbuf from cache and unrefs it. Therefore we must ref it
1398            here before it is taken over by the renderer. */
1399         if (pixbuf) g_object_ref(pixbuf);
1400
1401         imd->orientation = EXIF_ORIENTATION_TOP_LEFT;
1402         if (imd->image_fd)
1403                 {
1404                 if (imd->image_fd->user_orientation)
1405                         {
1406                         imd->orientation = imd->image_fd->user_orientation;
1407                         }
1408                 else if (options->image.exif_rotate_enable)
1409                         {
1410                         if (g_strcmp0(imd->image_fd->format_name, "heif") == 0)
1411                                 {
1412                                 imd->orientation = EXIF_ORIENTATION_TOP_LEFT;
1413                                 imd->image_fd->exif_orientation = imd->orientation;
1414                                 }
1415                         else
1416                                 {
1417                                 imd->orientation = metadata_read_int(imd->image_fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
1418                                 imd->image_fd->exif_orientation = imd->orientation;
1419                                 }
1420                         }
1421                 }
1422
1423         if (pixbuf)
1424                 {
1425                 stereo_data = static_cast<StereoPixbufData>(imd->user_stereo);
1426                 if (stereo_data == STEREO_PIXBUF_DEFAULT)
1427                         {
1428                         stereo_data = static_cast<StereoPixbufData>(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(pixbuf), "stereo_data")));
1429                         }
1430                 }
1431
1432         pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), nullptr, nullptr, FALSE);
1433         if (imd->cm)
1434                 {
1435                 color_man_free(static_cast<ColorMan *>(imd->cm));
1436                 imd->cm = nullptr;
1437                 }
1438
1439         if (lazy)
1440                 {
1441                 pixbuf_renderer_set_pixbuf_lazy(reinterpret_cast<PixbufRenderer *>(imd->pr), pixbuf, zoom, imd->orientation, stereo_data);
1442                 }
1443         else
1444                 {
1445                 pixbuf_renderer_set_pixbuf(reinterpret_cast<PixbufRenderer *>(imd->pr), pixbuf, zoom);
1446                 pixbuf_renderer_set_orientation(reinterpret_cast<PixbufRenderer *>(imd->pr), imd->orientation);
1447                 pixbuf_renderer_set_stereo_data(reinterpret_cast<PixbufRenderer *>(imd->pr), stereo_data);
1448                 }
1449
1450         if (pixbuf) g_object_unref(pixbuf);
1451
1452         /* Color correction takes too much time for an animated gif */
1453         lw = layout_find_by_image(imd);
1454         if (imd->color_profile_enable && lw && !lw->animation)
1455                 {
1456                 image_post_process_color(imd, 0, FALSE); /** @todo error handling */
1457                 }
1458
1459         if (imd->cm || imd->desaturate || imd->overunderexposed)
1460                 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), image_post_process_tile_color_cb, imd, (imd->cm != nullptr) );
1461
1462         image_state_set(imd, IMAGE_STATE_IMAGE);
1463 }
1464
1465 void image_change_from_collection(ImageWindow *imd, CollectionData *cd, CollectInfo *info, gdouble zoom)
1466 {
1467         CollectWindow *cw;
1468
1469         if (!cd || !info || !g_list_find(cd->list, info)) return;
1470
1471         image_change_real(imd, info->fd, cd, info, zoom);
1472         cw = collection_window_find(cd);
1473         if (cw)
1474                 {
1475                 collection_table_set_focus(cw->table, info);
1476                 collection_table_unselect_all(cw->table);
1477                 collection_table_select(cw->table,info);
1478                 }
1479
1480         if (info->fd)
1481                 {
1482                 image_chain_append_end(info->fd->path);
1483                 }
1484 }
1485
1486 CollectionData *image_get_collection(ImageWindow *imd, CollectInfo **info)
1487 {
1488         if (collection_to_number(imd->collection) >= 0)
1489                 {
1490                 if (g_list_find(imd->collection->list, imd->collection_info) != nullptr)
1491                         {
1492                         if (info) *info = imd->collection_info;
1493                         }
1494                 else
1495                         {
1496                         if (info) *info = nullptr;
1497                         }
1498                 return imd->collection;
1499                 }
1500
1501         if (info) *info = nullptr;
1502         return nullptr;
1503 }
1504
1505 static void image_loader_sync_read_ahead_data(ImageLoader *il, gpointer old_data, gpointer data)
1506 {
1507         if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (gpointer)image_read_ahead_error_cb, old_data))
1508                 g_signal_connect(G_OBJECT(il), "error", G_CALLBACK(image_read_ahead_error_cb), data);
1509
1510         if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (gpointer)image_read_ahead_done_cb, old_data))
1511                 g_signal_connect(G_OBJECT(il), "done", G_CALLBACK(image_read_ahead_done_cb), data);
1512 }
1513
1514 static void image_loader_sync_data(ImageLoader *il, gpointer old_data, gpointer data)
1515 {
1516         if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (gpointer)image_load_area_cb, old_data))
1517                 g_signal_connect(G_OBJECT(il), "area_ready", G_CALLBACK(image_load_area_cb), data);
1518
1519         if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (gpointer)image_load_error_cb, old_data))
1520                 g_signal_connect(G_OBJECT(il), "error", G_CALLBACK(image_load_error_cb), data);
1521
1522         if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (gpointer)image_load_done_cb, old_data))
1523                 g_signal_connect(G_OBJECT(il), "done", G_CALLBACK(image_load_done_cb), data);
1524 }
1525
1526 /* this is more like a move function
1527  * it moves most data from source to imd
1528  */
1529 void image_move_from_image(ImageWindow *imd, ImageWindow *source)
1530 {
1531         if (imd == source) return;
1532
1533         imd->unknown = source->unknown;
1534
1535         imd->collection = source->collection;
1536         imd->collection_info = source->collection_info;
1537
1538         image_loader_free(imd->il);
1539         imd->il = nullptr;
1540
1541         image_set_fd(imd, image_get_fd(source));
1542
1543
1544         if (source->il)
1545                 {
1546                 imd->il = source->il;
1547                 source->il = nullptr;
1548
1549                 image_loader_sync_data(imd->il, source, imd);
1550
1551                 imd->delay_alter_type = source->delay_alter_type;
1552                 source->delay_alter_type = ALTER_NONE;
1553                 }
1554
1555         imd->color_profile_enable = source->color_profile_enable;
1556         imd->color_profile_input = source->color_profile_input;
1557         imd->color_profile_use_image = source->color_profile_use_image;
1558         color_man_free(static_cast<ColorMan *>(imd->cm));
1559         imd->cm = nullptr;
1560         if (source->cm)
1561                 {
1562                 ColorMan *cm;
1563
1564                 imd->cm = source->cm;
1565                 source->cm = nullptr;
1566
1567                 cm = static_cast<ColorMan *>(imd->cm);
1568                 cm->imd = imd;
1569                 cm->func_done_data = imd;
1570                 }
1571
1572         file_data_unref(imd->read_ahead_fd);
1573         source->read_ahead_fd = nullptr;
1574
1575         imd->orientation = source->orientation;
1576         imd->desaturate = source->desaturate;
1577
1578         imd->user_stereo = source->user_stereo;
1579
1580         pixbuf_renderer_move(PIXBUF_RENDERER(imd->pr), PIXBUF_RENDERER(source->pr));
1581
1582         if (imd->cm || imd->desaturate || imd->overunderexposed)
1583                 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), image_post_process_tile_color_cb, imd, (imd->cm != nullptr) );
1584         else
1585                 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), nullptr, nullptr, TRUE);
1586
1587 }
1588
1589 /* this is  a copy function
1590  * source stays unchanged
1591  */
1592 void image_copy_from_image(ImageWindow *imd, ImageWindow *source)
1593 {
1594         if (imd == source) return;
1595
1596         imd->unknown = source->unknown;
1597
1598         imd->collection = source->collection;
1599         imd->collection_info = source->collection_info;
1600
1601         image_loader_free(imd->il);
1602         imd->il = nullptr;
1603
1604         image_set_fd(imd, image_get_fd(source));
1605
1606
1607         imd->color_profile_enable = source->color_profile_enable;
1608         imd->color_profile_input = source->color_profile_input;
1609         imd->color_profile_use_image = source->color_profile_use_image;
1610         color_man_free(static_cast<ColorMan *>(imd->cm));
1611         imd->cm = nullptr;
1612         if (source->cm)
1613                 {
1614                 ColorMan *cm;
1615
1616                 imd->cm = source->cm;
1617                 source->cm = nullptr;
1618
1619                 cm = static_cast<ColorMan *>(imd->cm);
1620                 cm->imd = imd;
1621                 cm->func_done_data = imd;
1622                 }
1623
1624         image_loader_free(imd->read_ahead_il);
1625         imd->read_ahead_il = source->read_ahead_il;
1626         source->read_ahead_il = nullptr;
1627         if (imd->read_ahead_il) image_loader_sync_read_ahead_data(imd->read_ahead_il, source, imd);
1628
1629         file_data_unref(imd->read_ahead_fd);
1630         imd->read_ahead_fd = source->read_ahead_fd;
1631         source->read_ahead_fd = nullptr;
1632
1633         imd->completed = source->completed;
1634         imd->state = source->state;
1635         source->state = IMAGE_STATE_NONE;
1636
1637         imd->orientation = source->orientation;
1638         imd->desaturate = source->desaturate;
1639
1640         imd->user_stereo = source->user_stereo;
1641
1642         pixbuf_renderer_copy(PIXBUF_RENDERER(imd->pr), PIXBUF_RENDERER(source->pr));
1643
1644         if (imd->cm || imd->desaturate || imd->overunderexposed)
1645                 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), image_post_process_tile_color_cb, imd, (imd->cm != nullptr) );
1646         else
1647                 pixbuf_renderer_set_post_process_func(reinterpret_cast<PixbufRenderer *>(imd->pr), nullptr, nullptr, TRUE);
1648
1649 }
1650
1651
1652 /* manipulation */
1653
1654 void image_area_changed(ImageWindow *imd, gint x, gint y, gint width, gint height)
1655 {
1656         pixbuf_renderer_area_changed(reinterpret_cast<PixbufRenderer *>(imd->pr), x, y, width, height);
1657 }
1658
1659 void image_reload(ImageWindow *imd)
1660 {
1661         if (pixbuf_renderer_get_tiles(reinterpret_cast<PixbufRenderer *>(imd->pr))) return;
1662
1663         image_change_complete(imd, image_zoom_get(imd));
1664 }
1665
1666 void image_scroll(ImageWindow *imd, gint x, gint y)
1667 {
1668         pixbuf_renderer_scroll(reinterpret_cast<PixbufRenderer *>(imd->pr), x, y);
1669 }
1670
1671 void image_scroll_to_point(ImageWindow *imd, gint x, gint y,
1672                            gdouble x_align, gdouble y_align)
1673 {
1674         pixbuf_renderer_scroll_to_point(reinterpret_cast<PixbufRenderer *>(imd->pr), x, y, x_align, y_align);
1675 }
1676
1677 void image_get_scroll_center(ImageWindow *imd, gdouble *x, gdouble *y)
1678 {
1679         pixbuf_renderer_get_scroll_center(PIXBUF_RENDERER(imd->pr), x, y);
1680 }
1681
1682 void image_set_scroll_center(ImageWindow *imd, gdouble x, gdouble y)
1683 {
1684         pixbuf_renderer_set_scroll_center(PIXBUF_RENDERER(imd->pr), x, y);
1685 }
1686
1687 void image_zoom_adjust(ImageWindow *imd, gdouble increment)
1688 {
1689         pixbuf_renderer_zoom_adjust(reinterpret_cast<PixbufRenderer *>(imd->pr), increment);
1690 }
1691
1692 void image_zoom_adjust_at_point(ImageWindow *imd, gdouble increment, gint x, gint y)
1693 {
1694         pixbuf_renderer_zoom_adjust_at_point(reinterpret_cast<PixbufRenderer *>(imd->pr), increment, x, y);
1695 }
1696
1697 void image_zoom_set_limits(ImageWindow *imd, gdouble min, gdouble max)
1698 {
1699         pixbuf_renderer_zoom_set_limits(reinterpret_cast<PixbufRenderer *>(imd->pr), min, max);
1700 }
1701
1702 void image_zoom_set(ImageWindow *imd, gdouble zoom)
1703 {
1704         pixbuf_renderer_zoom_set(reinterpret_cast<PixbufRenderer *>(imd->pr), zoom);
1705 }
1706
1707 void image_zoom_set_fill_geometry(ImageWindow *imd, gboolean vertical)
1708 {
1709         PixbufRenderer *pr;
1710         gdouble zoom;
1711         gint width;
1712         gint height;
1713
1714         pr = reinterpret_cast<PixbufRenderer *>(imd->pr);
1715
1716         if (!pixbuf_renderer_get_pixbuf(pr) ||
1717             !pixbuf_renderer_get_image_size(pr, &width, &height)) return;
1718
1719         if (vertical)
1720                 {
1721                 zoom = static_cast<gdouble>(pr->viewport_height) / height;
1722                 }
1723         else
1724                 {
1725                 zoom = static_cast<gdouble>(pr->viewport_width) / width;
1726                 }
1727
1728         if (zoom < 1.0)
1729                 {
1730                 zoom = 0.0 - 1.0 / zoom;
1731                 }
1732
1733         pixbuf_renderer_zoom_set(pr, zoom);
1734 }
1735
1736 gdouble image_zoom_get(ImageWindow *imd)
1737 {
1738         return pixbuf_renderer_zoom_get(reinterpret_cast<PixbufRenderer *>(imd->pr));
1739 }
1740
1741 gdouble image_zoom_get_real(ImageWindow *imd)
1742 {
1743         return pixbuf_renderer_zoom_get_scale(reinterpret_cast<PixbufRenderer *>(imd->pr));
1744 }
1745
1746 gchar *image_zoom_get_as_text(ImageWindow *imd)
1747 {
1748         gdouble zoom;
1749         gdouble scale;
1750         gdouble l = 1.0;
1751         gdouble r = 1.0;
1752         gint pl = 0;
1753         gint pr = 0;
1754         const gchar *approx = " ";
1755
1756         zoom = image_zoom_get(imd);
1757         scale = image_zoom_get_real(imd);
1758
1759         if (zoom > 0.0)
1760                 {
1761                 l = zoom;
1762                 }
1763         else if (zoom < 0.0)
1764                 {
1765                 r = 0.0 - zoom;
1766                 }
1767         else if (zoom == 0.0 && scale != 0.0)
1768                 {
1769                 if (scale >= 1.0)
1770                         {
1771                         l = scale;
1772                         }
1773                 else
1774                         {
1775                         r = 1.0 / scale;
1776                         }
1777                 approx = "~";
1778                 }
1779
1780         if (rint(l) != l) pl = 2;
1781         if (rint(r) != r) pr = 2;
1782
1783         return g_strdup_printf("%.*f :%s%.*f", pl, l, approx, pr, r);
1784 }
1785
1786 gdouble image_zoom_get_default(ImageWindow *imd)
1787 {
1788         gdouble zoom = 1.0;
1789
1790         switch (options->image.zoom_mode)
1791         {
1792         case ZOOM_RESET_ORIGINAL:
1793                 break;
1794         case ZOOM_RESET_FIT_WINDOW:
1795                 zoom = 0.0;
1796                 break;
1797         case ZOOM_RESET_NONE:
1798                 if (imd) zoom = image_zoom_get(imd);
1799                 break;
1800         }
1801
1802         return zoom;
1803 }
1804
1805 /* stereo */
1806
1807 #pragma GCC diagnostic push
1808 #pragma GCC diagnostic ignored "-Wunused-function"
1809 gint image_stereo_get_unused(ImageWindow *imd)
1810 {
1811         return pixbuf_renderer_stereo_get((PixbufRenderer *)imd->pr);
1812 }
1813 #pragma GCC diagnostic pop
1814
1815 void image_stereo_set(ImageWindow *imd, gint stereo_mode)
1816 {
1817         DEBUG_1("Setting stereo mode %04x for imd %p", stereo_mode, (void *)imd);
1818         pixbuf_renderer_stereo_set(reinterpret_cast<PixbufRenderer *>(imd->pr), stereo_mode);
1819 }
1820
1821 #pragma GCC diagnostic push
1822 #pragma GCC diagnostic ignored "-Wunused-function"
1823 void image_stereo_swap_unused(ImageWindow *imd)
1824 {
1825         gint stereo_mode = pixbuf_renderer_stereo_get((PixbufRenderer *)imd->pr);
1826         stereo_mode ^= PR_STEREO_SWAP;
1827         pixbuf_renderer_stereo_set((PixbufRenderer *)imd->pr, stereo_mode);
1828 }
1829 #pragma GCC diagnostic pop
1830
1831 StereoPixbufData image_stereo_pixbuf_get(ImageWindow *imd)
1832 {
1833         return static_cast<StereoPixbufData>(imd->user_stereo);
1834 }
1835
1836 void image_stereo_pixbuf_set(ImageWindow *imd, StereoPixbufData stereo_mode)
1837 {
1838         imd->user_stereo = stereo_mode;
1839         image_reload(imd);
1840 }
1841
1842 /* read ahead */
1843
1844 void image_prebuffer_set(ImageWindow *imd, FileData *fd)
1845 {
1846         if (pixbuf_renderer_get_tiles(reinterpret_cast<PixbufRenderer *>(imd->pr))) return;
1847
1848         if (fd)
1849                 {
1850                 if (!file_cache_get(image_get_cache(), fd))
1851                         {
1852                         image_read_ahead_set(imd, fd);
1853                         }
1854                 }
1855         else
1856                 {
1857                 image_read_ahead_cancel(imd);
1858                 }
1859 }
1860
1861 static void image_notify_cb(FileData *fd, NotifyType type, gpointer data)
1862 {
1863         auto imd = static_cast<ImageWindow *>(data);
1864
1865         if (!imd || !image_get_pixbuf(imd) ||
1866             /* imd->il || */ /* loading in progress - do not check - it should start from the beginning anyway */
1867             !imd->image_fd || /* nothing to reload */
1868             imd->state == IMAGE_STATE_NONE /* loading not started, no need to reload */
1869             ) return;
1870
1871         if ((type & NOTIFY_REREAD) && fd == imd->image_fd)
1872                 {
1873                 /* there is no need to reload on NOTIFY_CHANGE,
1874                    modified files should be detacted anyway and NOTIFY_REREAD should be received
1875                    or they are removed from the filelist completely on "move" and "delete"
1876                 */
1877                 DEBUG_1("Notify image: %s %04x", fd->path, type);
1878                 image_reload(imd);
1879                 }
1880 }
1881
1882 void image_auto_refresh_enable(ImageWindow *imd, gboolean enable)
1883 {
1884         if (!enable && imd->auto_refresh && imd->image_fd)
1885                 file_data_unregister_real_time_monitor(imd->image_fd);
1886
1887         if (enable && !imd->auto_refresh && imd->image_fd)
1888                 file_data_register_real_time_monitor(imd->image_fd);
1889
1890         imd->auto_refresh = enable;
1891 }
1892
1893 void image_top_window_set_sync(ImageWindow *imd, gboolean allow_sync)
1894 {
1895         imd->top_window_sync = allow_sync;
1896
1897         g_object_set(G_OBJECT(imd->pr), "window_fit", allow_sync, NULL);
1898 }
1899
1900 void image_background_set_color(ImageWindow *imd, GdkRGBA *color)
1901 {
1902         pixbuf_renderer_set_color(reinterpret_cast<PixbufRenderer *>(imd->pr), color);
1903 }
1904
1905 void image_background_set_color_from_options(ImageWindow *imd, gboolean fullscreen)
1906 {
1907         GdkRGBA *color = nullptr;
1908         GdkRGBA theme_color;
1909         GdkRGBA bg_color;
1910         GtkStyleContext *style_context;
1911         LayoutWindow *lw = nullptr;
1912
1913         if ((options->image.use_custom_border_color && !fullscreen) ||
1914             (options->image.use_custom_border_color_in_fullscreen && fullscreen))
1915                 {
1916                 color = &options->image.border_color;
1917                 }
1918
1919         else
1920                 {
1921                 if (!layout_valid(&lw)) return;
1922
1923                 style_context = gtk_widget_get_style_context(lw->window);
1924                 gtk_style_context_get_background_color(style_context, GTK_STATE_FLAG_NORMAL, &bg_color);
1925
1926                 theme_color.red = bg_color.red * 1;
1927                 theme_color.green = bg_color.green * 1;
1928                 theme_color.blue = bg_color.blue * 1;
1929
1930                 color = &theme_color;
1931                 }
1932
1933         image_background_set_color(imd, color);
1934 }
1935
1936 void image_color_profile_set(ImageWindow *imd,
1937                              gint input_type,
1938                              gboolean use_image)
1939 {
1940         if (!imd) return;
1941
1942         if (input_type < 0 || input_type >= COLOR_PROFILE_FILE + COLOR_PROFILE_INPUTS)
1943                 {
1944                 return;
1945                 }
1946
1947         imd->color_profile_input = input_type;
1948         imd->color_profile_use_image = use_image;
1949 }
1950
1951 gboolean image_color_profile_get(ImageWindow *imd,
1952                                  gint *input_type,
1953                                  gboolean *use_image)
1954 {
1955         if (!imd) return FALSE;
1956
1957         if (input_type) *input_type = imd->color_profile_input;
1958         if (use_image) *use_image = imd->color_profile_use_image;
1959
1960         return TRUE;
1961 }
1962
1963 void image_color_profile_set_use(ImageWindow *imd, gboolean enable)
1964 {
1965         if (!imd) return;
1966
1967         if (imd->color_profile_enable == enable) return;
1968
1969         imd->color_profile_enable = enable;
1970 }
1971
1972 gboolean image_color_profile_get_use(ImageWindow *imd)
1973 {
1974         if (!imd) return FALSE;
1975
1976         return imd->color_profile_enable;
1977 }
1978
1979 gboolean image_color_profile_get_status(ImageWindow *imd, gchar **image_profile, gchar **screen_profile)
1980 {
1981         ColorMan *cm;
1982         if (!imd) return FALSE;
1983
1984         cm = static_cast<ColorMan *>(imd->cm);
1985         if (!cm) return FALSE;
1986         return color_man_get_status(cm, image_profile, screen_profile);
1987
1988 }
1989
1990 void image_set_delay_flip(ImageWindow *imd, gboolean delay)
1991 {
1992         if (!imd ||
1993             imd->delay_flip == delay) return;
1994
1995         imd->delay_flip = delay;
1996
1997         g_object_set(G_OBJECT(imd->pr), "delay_flip", delay, NULL);
1998
1999         if (!imd->delay_flip && imd->il)
2000                 {
2001                 PixbufRenderer *pr;
2002
2003                 pr = PIXBUF_RENDERER(imd->pr);
2004                 if (pr->pixbuf) g_object_unref(pr->pixbuf);
2005                 pr->pixbuf = nullptr;
2006
2007                 image_load_pixbuf_ready(imd);
2008                 }
2009 }
2010
2011 void image_to_root_window(ImageWindow *, gboolean)
2012 {
2013 }
2014
2015 void image_select(ImageWindow *imd, gboolean select)
2016 {
2017         if (!imd->has_frame) return;
2018
2019         if (select)
2020                 {
2021                 gtk_widget_set_state(imd->widget, GTK_STATE_SELECTED);
2022                 gtk_widget_set_state(imd->pr, GTK_STATE_NORMAL); /* do not propagate */
2023                 }
2024         else
2025                 gtk_widget_set_state(imd->widget, GTK_STATE_NORMAL);
2026 }
2027
2028 void image_set_selectable(ImageWindow *imd, gboolean selectable)
2029 {
2030         if (!imd->has_frame) return;
2031
2032         gq_gtk_frame_set_shadow_type(GTK_FRAME(imd->frame), GTK_SHADOW_NONE);
2033         gtk_container_set_border_width(GTK_CONTAINER(imd->frame), selectable ? 4 : 0);
2034 }
2035
2036 void image_grab_focus(ImageWindow *imd)
2037 {
2038         if (imd->has_frame)
2039                 {
2040                 gtk_widget_grab_focus(imd->frame);
2041                 }
2042         else
2043                 {
2044                 gtk_widget_grab_focus(imd->widget);
2045                 }
2046 }
2047
2048
2049 /*
2050  *-------------------------------------------------------------------
2051  * prefs sync
2052  *-------------------------------------------------------------------
2053  */
2054
2055 static void image_options_set(ImageWindow *imd)
2056 {
2057         g_object_set(G_OBJECT(imd->pr), "zoom_quality", options->image.zoom_quality,
2058                                         "zoom_2pass", options->image.zoom_2pass,
2059                                         "zoom_expand", options->image.zoom_to_fit_allow_expand,
2060                                         "scroll_reset", options->image.scroll_reset_method,
2061                                         "cache_display", options->image.tile_cache_max,
2062                                         "window_fit", (imd->top_window_sync && options->image.fit_window_to_image),
2063                                         "window_limit", options->image.limit_window_size,
2064                                         "window_limit_value", options->image.max_window_size,
2065                                         "autofit_limit", options->image.limit_autofit_size,
2066                                         "autofit_limit_value", options->image.max_autofit_size,
2067                                         "enlargement_limit_value", options->image.max_enlargement_size,
2068
2069                                         NULL);
2070
2071         pixbuf_renderer_set_parent(reinterpret_cast<PixbufRenderer *>(imd->pr), reinterpret_cast<GtkWindow *>(imd->top_window));
2072
2073         image_stereo_set(imd, options->stereo.mode);
2074         pixbuf_renderer_stereo_fixed_set(reinterpret_cast<PixbufRenderer *>(imd->pr),
2075                                         options->stereo.fixed_w, options->stereo.fixed_h,
2076                                         options->stereo.fixed_x1, options->stereo.fixed_y1,
2077                                         options->stereo.fixed_x2, options->stereo.fixed_y2);
2078 }
2079
2080 void image_options_sync()
2081 {
2082         GList *work;
2083
2084         work = image_list;
2085         while (work)
2086                 {
2087                 ImageWindow *imd;
2088
2089                 imd = static_cast<ImageWindow *>(work->data);
2090                 work = work->next;
2091
2092                 image_options_set(imd);
2093                 }
2094 }
2095
2096 /*
2097  *-------------------------------------------------------------------
2098  * init / destroy
2099  *-------------------------------------------------------------------
2100  */
2101
2102 static void image_free(ImageWindow *imd)
2103 {
2104         image_list = g_list_remove(image_list, imd);
2105
2106         if (imd->auto_refresh && imd->image_fd)
2107                 file_data_unregister_real_time_monitor(imd->image_fd);
2108
2109         file_data_unregister_notify_func(image_notify_cb, imd);
2110
2111         image_reset(imd);
2112
2113         image_read_ahead_cancel(imd);
2114
2115         file_data_unref(imd->image_fd);
2116         g_free(imd->title);
2117         g_free(imd->title_right);
2118         g_free(imd);
2119 }
2120
2121 static void image_destroy_cb(GtkWidget *, gpointer data)
2122 {
2123         auto imd = static_cast<ImageWindow *>(data);
2124         image_free(imd);
2125 }
2126
2127 gboolean selectable_frame_draw_cb(GtkWidget *widget, cairo_t *cr, gpointer)
2128 {
2129         GtkAllocation allocation;
2130         gtk_widget_get_allocation(widget, &allocation);
2131
2132         gtk_render_frame(gtk_widget_get_style_context(widget), cr, allocation.x + 3, allocation.y + 3, allocation.width - 6, allocation.height - 6);
2133         gtk_render_background(gtk_widget_get_style_context(widget), cr, allocation.x + 3, allocation.y + 3, allocation.width - 6, allocation.height - 6);
2134
2135         if (gtk_widget_has_focus(widget))
2136                 {
2137                 gtk_render_focus(gtk_widget_get_style_context(widget), cr, allocation.x, allocation.y, allocation.width - 1, allocation.height - 1);
2138                 }
2139         else
2140                 {
2141                 gtk_render_frame(gtk_widget_get_style_context(widget), cr, allocation.x, allocation.y, allocation.width - 1, allocation.height - 1);
2142                 }
2143         return FALSE;
2144 }
2145
2146 void image_set_frame(ImageWindow *imd, gboolean frame)
2147 {
2148         frame = !!frame;
2149
2150         if (frame == imd->has_frame) return;
2151
2152         gtk_widget_hide(imd->pr);
2153
2154         if (frame)
2155                 {
2156                 imd->frame = gtk_frame_new(nullptr);
2157                 DEBUG_NAME(imd->frame);
2158                 g_object_ref(imd->pr);
2159                 if (imd->has_frame != -1) gtk_container_remove(GTK_CONTAINER(imd->widget), imd->pr);
2160                 gq_gtk_container_add(GTK_WIDGET(imd->frame), imd->pr);
2161
2162                 g_object_unref(imd->pr);
2163                 gtk_widget_set_can_focus(imd->frame, TRUE);
2164                 gtk_widget_set_app_paintable(imd->frame, TRUE);
2165
2166                 g_signal_connect(G_OBJECT(imd->frame), "draw",
2167                                  G_CALLBACK(selectable_frame_draw_cb), NULL);
2168                 g_signal_connect(G_OBJECT(imd->frame), "focus_in_event",
2169                                  G_CALLBACK(image_focus_in_cb), imd);
2170
2171                 gq_gtk_box_pack_start(GTK_BOX(imd->widget), imd->frame, TRUE, TRUE, 0);
2172                 gtk_widget_show(imd->frame);
2173                 }
2174         else
2175                 {
2176                 g_object_ref(imd->pr);
2177                 if (imd->frame)
2178                         {
2179                         gtk_container_remove(GTK_CONTAINER(imd->frame), imd->pr);
2180                         gtk_container_remove(GTK_CONTAINER(imd->widget), imd->frame);
2181                         imd->frame = nullptr;
2182                         }
2183                 gq_gtk_box_pack_start(GTK_BOX(imd->widget), imd->pr, TRUE, TRUE, 0);
2184
2185                 g_object_unref(imd->pr);
2186                 }
2187
2188         gtk_widget_show(imd->pr);
2189
2190         imd->has_frame = frame;
2191 }
2192
2193 ImageWindow *image_new(gboolean frame)
2194 {
2195         ImageWindow *imd;
2196
2197         imd = g_new0(ImageWindow, 1);
2198
2199         imd->unknown = TRUE;
2200         imd->has_frame = -1; /* not initialized; for image_set_frame */
2201         imd->delay_alter_type = ALTER_NONE;
2202         imd->state = IMAGE_STATE_NONE;
2203         imd->color_profile_from_image = COLOR_PROFILE_NONE;
2204         imd->orientation = 1;
2205
2206         imd->pr = GTK_WIDGET(pixbuf_renderer_new());
2207         DEBUG_NAME(imd->pr);
2208
2209         image_options_set(imd);
2210
2211         imd->widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2212         DEBUG_NAME(imd->widget);
2213
2214         image_set_frame(imd, frame);
2215
2216         image_set_selectable(imd, 0);
2217
2218         g_signal_connect(G_OBJECT(imd->pr), "clicked",
2219                          G_CALLBACK(image_click_cb), imd);
2220         g_signal_connect(G_OBJECT(imd->pr), "button_press_event",
2221                          G_CALLBACK(image_press_cb), imd);
2222         g_signal_connect(G_OBJECT(imd->pr), "button_release_event",
2223                          G_CALLBACK(image_release_cb), imd);
2224         g_signal_connect(G_OBJECT(imd->pr), "scroll_notify",
2225                          G_CALLBACK(image_scroll_notify_cb), imd);
2226
2227         g_signal_connect(G_OBJECT(imd->pr), "scroll_event",
2228                          G_CALLBACK(image_scroll_cb), imd);
2229
2230         g_signal_connect(G_OBJECT(imd->pr), "destroy",
2231                          G_CALLBACK(image_destroy_cb), imd);
2232
2233         g_signal_connect(G_OBJECT(imd->pr), "zoom",
2234                          G_CALLBACK(image_zoom_cb), imd);
2235         g_signal_connect(G_OBJECT(imd->pr), "render_complete",
2236                          G_CALLBACK(image_render_complete_cb), imd);
2237         g_signal_connect(G_OBJECT(imd->pr), "drag",
2238                          G_CALLBACK(image_drag_cb), imd);
2239
2240         file_data_register_notify_func(image_notify_cb, imd, NOTIFY_PRIORITY_LOW);
2241
2242         image_list = g_list_append(image_list, imd);
2243
2244         return imd;
2245 }
2246
2247 void image_get_rectangle(gint *x1, gint *y1, gint *x2, gint *y2)
2248 {
2249         *x1 = rect_x1;
2250         *y1 = rect_y1;
2251         *x2 = rect_x2;
2252         *y2 = rect_y2;
2253 }
2254
2255 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */