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