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