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