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