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