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