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