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