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