Merge merge-requests/513 - build paths
[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                 switch (imd->image_fd->format_class)
675                         {
676                         case FORMAT_CLASS_UNKNOWN:
677                                 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
678                                 break;
679                         case FORMAT_CLASS_META:
680                                 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
681                                 break;
682                         case FORMAT_CLASS_VIDEO:
683                                 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
684                                 break;
685                         default:
686                                 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
687                         }
688
689                 image_change_pixbuf(imd, pixbuf, image_zoom_get(imd), FALSE);
690                 g_object_unref(pixbuf);
691
692                 imd->unknown = TRUE;
693                 }
694         else if (imd->delay_flip &&
695             image_get_pixbuf(imd) != image_loader_get_pixbuf(imd->il))
696                 {
697                 g_object_set(G_OBJECT(imd->pr), "complete", FALSE, NULL);
698                 image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), FALSE);
699                 }
700
701         image_loader_free(imd->il);
702         imd->il = NULL;
703
704 //      image_post_process(imd, TRUE);
705
706         image_read_ahead_start(imd);
707 }
708
709 static void image_load_size_cb(ImageLoader *il, guint width, guint height, gpointer data)
710 {
711         ImageWindow *imd = data;
712
713         DEBUG_1("image_load_size_cb: %dx%d", width, height);
714         pixbuf_renderer_set_size_early((PixbufRenderer *)imd->pr, width, height);
715 }
716
717 static void image_load_error_cb(ImageLoader *il, gpointer data)
718 {
719         DEBUG_1("%s image error", get_exec_time());
720
721         /* even on error handle it like it was done,
722          * since we have a pixbuf with _something_ */
723
724         image_load_done_cb(il, data);
725 }
726
727 static void image_load_set_signals(ImageWindow *imd, gboolean override_old_signals)
728 {
729         g_assert(imd->il);
730         if (override_old_signals)
731                 {
732                 /* override the old signals */
733                 g_signal_handlers_disconnect_matched(G_OBJECT(imd->il), G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, imd);
734                 }
735
736         g_signal_connect(G_OBJECT(imd->il), "area_ready", (GCallback)image_load_area_cb, imd);
737         g_signal_connect(G_OBJECT(imd->il), "error", (GCallback)image_load_error_cb, imd);
738         g_signal_connect(G_OBJECT(imd->il), "done", (GCallback)image_load_done_cb, imd);
739         g_signal_connect(G_OBJECT(imd->il), "size_prepared", (GCallback)image_load_size_cb, imd);
740 }
741
742 /* this read ahead is located here merely for the callbacks, above */
743
744 static gboolean image_read_ahead_check(ImageWindow *imd)
745 {
746         if (!imd->read_ahead_fd) return FALSE;
747         if (imd->il) return FALSE;
748
749         if (!imd->image_fd || imd->read_ahead_fd != imd->image_fd)
750                 {
751                 image_read_ahead_cancel(imd);
752                 return FALSE;
753                 }
754
755         if (imd->read_ahead_il)
756                 {
757                 imd->il = imd->read_ahead_il;
758                 imd->read_ahead_il = NULL;
759
760                 image_load_set_signals(imd, TRUE);
761
762                 g_object_set(G_OBJECT(imd->pr), "loading", TRUE, NULL);
763                 image_state_set(imd, IMAGE_STATE_LOADING);
764
765                 if (!imd->delay_flip)
766                         {
767                         image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), TRUE);
768                         }
769
770                 image_loader_delay_area_ready(imd->il, FALSE); /* send the delayed area_ready signals */
771
772                 file_data_unref(imd->read_ahead_fd);
773                 imd->read_ahead_fd = NULL;
774                 return TRUE;
775                 }
776         else if (imd->read_ahead_fd->pixbuf)
777                 {
778                 image_change_pixbuf(imd, imd->read_ahead_fd->pixbuf, image_zoom_get(imd), FALSE);
779
780                 file_data_unref(imd->read_ahead_fd);
781                 imd->read_ahead_fd = NULL;
782
783 //              image_post_process(imd, FALSE);
784                 return TRUE;
785                 }
786
787         image_read_ahead_cancel(imd);
788         return FALSE;
789 }
790
791 static gboolean image_load_begin(ImageWindow *imd, FileData *fd)
792 {
793         DEBUG_1("%s image begin", get_exec_time());
794
795         if (imd->il) return FALSE;
796
797         imd->completed = FALSE;
798         g_object_set(G_OBJECT(imd->pr), "complete", FALSE, NULL);
799
800         if (image_cache_get(imd))
801                 {
802                 DEBUG_1("from cache: %s", imd->image_fd->path);
803                 return TRUE;
804                 }
805
806         if (image_read_ahead_check(imd))
807                 {
808                 DEBUG_1("from read ahead buffer: %s", imd->image_fd->path);
809                 return TRUE;
810                 }
811
812         if (!imd->delay_flip && image_get_pixbuf(imd))
813                 {
814                 PixbufRenderer *pr;
815
816                 pr = PIXBUF_RENDERER(imd->pr);
817                 if (pr->pixbuf) g_object_unref(pr->pixbuf);
818                 pr->pixbuf = NULL;
819                 }
820
821         g_object_set(G_OBJECT(imd->pr), "loading", TRUE, NULL);
822
823         imd->il = image_loader_new(fd);
824
825         image_load_set_signals(imd, FALSE);
826
827         if (!image_loader_start(imd->il))
828                 {
829                 DEBUG_1("image start error");
830
831                 g_object_set(G_OBJECT(imd->pr), "loading", FALSE, NULL);
832
833                 image_loader_free(imd->il);
834                 imd->il = NULL;
835
836                 image_complete_util(imd, FALSE);
837
838                 return FALSE;
839                 }
840
841         image_state_set(imd, IMAGE_STATE_LOADING);
842
843 /*
844         if (!imd->delay_flip && !image_get_pixbuf(imd) && image_loader_get_pixbuf(imd->il))
845                 {
846                 image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), TRUE);
847                 }
848 */
849         return TRUE;
850 }
851
852 static void image_reset(ImageWindow *imd)
853 {
854         /* stops anything currently being done */
855
856         DEBUG_1("%s image reset", get_exec_time());
857
858         g_object_set(G_OBJECT(imd->pr), "loading", FALSE, NULL);
859
860         image_loader_free(imd->il);
861         imd->il = NULL;
862
863         color_man_free((ColorMan *)imd->cm);
864         imd->cm = NULL;
865
866         imd->delay_alter_type = ALTER_NONE;
867
868         image_state_set(imd, IMAGE_STATE_NONE);
869 }
870
871 /*
872  *-------------------------------------------------------------------
873  * image changer
874  *-------------------------------------------------------------------
875  */
876
877 static void image_change_complete(ImageWindow *imd, gdouble zoom)
878 {
879         image_reset(imd);
880         imd->unknown = TRUE;
881
882         if (!imd->image_fd)
883                 {
884                 image_change_pixbuf(imd, NULL, zoom, FALSE);
885                 }
886         else
887                 {
888
889                 if (is_readable_file(imd->image_fd->path))
890                         {
891                         PixbufRenderer *pr;
892
893                         pr = PIXBUF_RENDERER(imd->pr);
894                         pr->zoom = zoom;        /* store the zoom, needed by the loader */
895
896                         if (image_load_begin(imd, imd->image_fd))
897                                 {
898                                 imd->unknown = FALSE;
899                                 }
900                         }
901
902                 if (imd->unknown == TRUE)
903                         {
904                         GdkPixbuf *pixbuf;
905
906                         pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
907                         image_change_pixbuf(imd, pixbuf, zoom, FALSE);
908                         g_object_unref(pixbuf);
909                         }
910                 }
911
912         image_update_util(imd);
913 }
914
915 static void image_change_real(ImageWindow *imd, FileData *fd,
916                               CollectionData *cd, CollectInfo *info, gdouble zoom)
917 {
918
919         imd->collection = cd;
920         imd->collection_info = info;
921
922         if (imd->auto_refresh && imd->image_fd)
923                 file_data_unregister_real_time_monitor(imd->image_fd);
924
925         file_data_unref(imd->image_fd);
926         imd->image_fd = file_data_ref(fd);
927
928
929         image_change_complete(imd, zoom);
930
931         image_update_title(imd);
932         image_state_set(imd, IMAGE_STATE_IMAGE);
933
934         if (imd->auto_refresh && imd->image_fd)
935                 file_data_register_real_time_monitor(imd->image_fd);
936 }
937
938 /*
939  *-------------------------------------------------------------------
940  * focus stuff
941  *-------------------------------------------------------------------
942  */
943
944 static gboolean image_focus_in_cb(GtkWidget *widget, GdkEventFocus *event, gpointer data)
945 {
946         ImageWindow *imd = data;
947
948         if (imd->func_focus_in)
949                 {
950                 imd->func_focus_in(imd, imd->data_focus_in);
951                 }
952
953         return TRUE;
954 }
955
956 static gboolean image_scroll_cb(GtkWidget *widget, GdkEventScroll *event, gpointer data)
957 {
958         ImageWindow *imd = data;
959
960         if (imd->func_scroll &&
961             event && event->type == GDK_SCROLL)
962                 {
963                 imd->func_scroll(imd, event, imd->data_scroll);
964                 return TRUE;
965                 }
966
967         return FALSE;
968 }
969
970 /*
971  *-------------------------------------------------------------------
972  * public interface
973  *-------------------------------------------------------------------
974  */
975
976 void image_attach_window(ImageWindow *imd, GtkWidget *window,
977                          const gchar *title, const gchar *title_right, gboolean show_zoom)
978 {
979         imd->top_window = window;
980         g_free(imd->title);
981         imd->title = g_strdup(title);
982         g_free(imd->title_right);
983         imd->title_right = g_strdup(title_right);
984         imd->title_show_zoom = show_zoom;
985
986         if (!options->image.fit_window_to_image) window = NULL;
987
988         pixbuf_renderer_set_parent((PixbufRenderer *)imd->pr, (GtkWindow *)window);
989
990         image_update_title(imd);
991 }
992
993 void image_set_update_func(ImageWindow *imd,
994                            void (*func)(ImageWindow *imd, gpointer data),
995                            gpointer data)
996 {
997         imd->func_update = func;
998         imd->data_update = data;
999 }
1000
1001 void image_set_complete_func(ImageWindow *imd,
1002                              void (*func)(ImageWindow *imd, gboolean preload, gpointer data),
1003                              gpointer data)
1004 {
1005         imd->func_complete = func;
1006         imd->data_complete = data;
1007 }
1008
1009 void image_set_state_func(ImageWindow *imd,
1010                         void (*func)(ImageWindow *imd, ImageState state, gpointer data),
1011                         gpointer data)
1012 {
1013         imd->func_state = func;
1014         imd->data_state = data;
1015 }
1016
1017
1018 void image_set_button_func(ImageWindow *imd,
1019                            void (*func)(ImageWindow *, GdkEventButton *event, gpointer),
1020                            gpointer data)
1021 {
1022         imd->func_button = func;
1023         imd->data_button = data;
1024 }
1025
1026 void image_set_drag_func(ImageWindow *imd,
1027                            void (*func)(ImageWindow *, GdkEventMotion *event, gdouble dx, gdouble dy, gpointer),
1028                            gpointer data)
1029 {
1030         imd->func_drag = func;
1031         imd->data_drag = data;
1032 }
1033
1034 void image_set_scroll_func(ImageWindow *imd,
1035                            void (*func)(ImageWindow *, GdkEventScroll *event, gpointer),
1036                            gpointer data)
1037 {
1038         imd->func_scroll = func;
1039         imd->data_scroll = data;
1040 }
1041
1042 void image_set_scroll_notify_func(ImageWindow *imd,
1043                                   void (*func)(ImageWindow *imd, gint x, gint y, gint width, gint height, gpointer data),
1044                                   gpointer data)
1045 {
1046         imd->func_scroll_notify = func;
1047         imd->data_scroll_notify = data;
1048 }
1049
1050 void image_set_focus_in_func(ImageWindow *imd,
1051                            void (*func)(ImageWindow *, gpointer),
1052                            gpointer data)
1053 {
1054         imd->func_focus_in = func;
1055         imd->data_focus_in = data;
1056 }
1057
1058 /* path, name */
1059
1060 const gchar *image_get_path(ImageWindow *imd)
1061 {
1062         if (imd->image_fd == NULL) return NULL;
1063         return imd->image_fd->path;
1064 }
1065
1066 const gchar *image_get_name(ImageWindow *imd)
1067 {
1068         if (imd->image_fd == NULL) return NULL;
1069         return imd->image_fd->name;
1070 }
1071
1072 FileData *image_get_fd(ImageWindow *imd)
1073 {
1074         return imd->image_fd;
1075 }
1076
1077 /* merely changes path string, does not change the image! */
1078 void image_set_fd(ImageWindow *imd, FileData *fd)
1079 {
1080         if (imd->auto_refresh && imd->image_fd)
1081                 file_data_unregister_real_time_monitor(imd->image_fd);
1082
1083         file_data_unref(imd->image_fd);
1084         imd->image_fd = file_data_ref(fd);
1085
1086         image_update_title(imd);
1087         image_state_set(imd, IMAGE_STATE_IMAGE);
1088
1089         if (imd->auto_refresh && imd->image_fd)
1090                 file_data_register_real_time_monitor(imd->image_fd);
1091 }
1092
1093 /* load a new image */
1094
1095 void image_change_fd(ImageWindow *imd, FileData *fd, gdouble zoom)
1096 {
1097         if (imd->image_fd == fd) return;
1098
1099         image_change_real(imd, fd, NULL, NULL, zoom);
1100 }
1101
1102 gboolean image_get_image_size(ImageWindow *imd, gint *width, gint *height)
1103 {
1104         return pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), width, height);
1105 }
1106
1107 GdkPixbuf *image_get_pixbuf(ImageWindow *imd)
1108 {
1109         return pixbuf_renderer_get_pixbuf((PixbufRenderer *)imd->pr);
1110 }
1111
1112 void image_change_pixbuf(ImageWindow *imd, GdkPixbuf *pixbuf, gdouble zoom, gboolean lazy)
1113 {
1114         StereoPixbufData stereo_data = STEREO_PIXBUF_DEFAULT;
1115         /* read_exif and similar functions can actually notice that the file has changed and trigger
1116            a notification that removes the pixbuf from cache and unrefs it. Therefore we must ref it
1117            here before it is taken over by the renderer. */
1118         if (pixbuf) g_object_ref(pixbuf);
1119
1120         imd->orientation = EXIF_ORIENTATION_TOP_LEFT;
1121         if (imd->image_fd)
1122                 {
1123                 if (imd->image_fd->user_orientation)
1124                         {
1125                         imd->orientation = imd->image_fd->user_orientation;
1126                         }
1127                 else if (options->image.exif_rotate_enable)
1128                         {
1129                         imd->orientation = metadata_read_int(imd->image_fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
1130                         imd->image_fd->exif_orientation = imd->orientation;
1131                         }
1132                 }
1133
1134         if (pixbuf)
1135                 {
1136                 stereo_data = imd->user_stereo;
1137                 if (stereo_data == STEREO_PIXBUF_DEFAULT)
1138                         {
1139                         stereo_data = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(pixbuf), "stereo_data"));
1140                         }
1141                 }
1142
1143         pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, NULL, NULL, FALSE);
1144         if (imd->cm)
1145                 {
1146                 color_man_free(imd->cm);
1147                 imd->cm = NULL;
1148                 }
1149
1150         if (lazy)
1151                 {
1152                 pixbuf_renderer_set_pixbuf_lazy((PixbufRenderer *)imd->pr, pixbuf, zoom, imd->orientation, stereo_data);
1153                 }
1154         else
1155                 {
1156                 pixbuf_renderer_set_pixbuf((PixbufRenderer *)imd->pr, pixbuf, zoom);
1157                 pixbuf_renderer_set_orientation((PixbufRenderer *)imd->pr, imd->orientation);
1158                 pixbuf_renderer_set_stereo_data((PixbufRenderer *)imd->pr, stereo_data);
1159                 }
1160
1161         if (pixbuf) g_object_unref(pixbuf);
1162
1163         if (imd->color_profile_enable)
1164                 {
1165                 image_post_process_color(imd, 0, FALSE); /* TODO: error handling */
1166                 }
1167
1168         if (imd->cm || imd->desaturate)
1169                 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, image_post_process_tile_color_cb, (gpointer) imd, (imd->cm != NULL) );
1170
1171         image_state_set(imd, IMAGE_STATE_IMAGE);
1172 }
1173
1174 void image_change_from_collection(ImageWindow *imd, CollectionData *cd, CollectInfo *info, gdouble zoom)
1175 {
1176         if (!cd || !info || !g_list_find(cd->list, info)) return;
1177
1178         image_change_real(imd, info->fd, cd, info, zoom);
1179 }
1180
1181 CollectionData *image_get_collection(ImageWindow *imd, CollectInfo **info)
1182 {
1183         if (collection_to_number(imd->collection) >= 0)
1184                 {
1185                 if (g_list_find(imd->collection->list, imd->collection_info) != NULL)
1186                         {
1187                         if (info) *info = imd->collection_info;
1188                         }
1189                 else
1190                         {
1191                         if (info) *info = NULL;
1192                         }
1193                 return imd->collection;
1194                 }
1195
1196         if (info) *info = NULL;
1197         return NULL;
1198 }
1199
1200 static void image_loader_sync_read_ahead_data(ImageLoader *il, gpointer old_data, gpointer data)
1201 {
1202         if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (GCallback)image_read_ahead_error_cb, old_data))
1203                 g_signal_connect(G_OBJECT(il), "error", (GCallback)image_read_ahead_error_cb, data);
1204
1205         if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (GCallback)image_read_ahead_done_cb, old_data))
1206                 g_signal_connect(G_OBJECT(il), "done", (GCallback)image_read_ahead_done_cb, data);
1207 }
1208
1209 static void image_loader_sync_data(ImageLoader *il, gpointer old_data, gpointer data)
1210 {
1211         if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (GCallback)image_load_area_cb, old_data))
1212                 g_signal_connect(G_OBJECT(il), "area_ready", (GCallback)image_load_area_cb, data);
1213
1214         if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (GCallback)image_load_error_cb, old_data))
1215                 g_signal_connect(G_OBJECT(il), "error", (GCallback)image_load_error_cb, data);
1216
1217         if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (GCallback)image_load_done_cb, old_data))
1218                 g_signal_connect(G_OBJECT(il), "done", (GCallback)image_load_done_cb, data);
1219 }
1220
1221 /* this is more like a move function
1222  * it moves most data from source to imd
1223  */
1224 void image_move_from_image(ImageWindow *imd, ImageWindow *source)
1225 {
1226         if (imd == source) return;
1227
1228         imd->unknown = source->unknown;
1229
1230         imd->collection = source->collection;
1231         imd->collection_info = source->collection_info;
1232
1233         image_loader_free(imd->il);
1234         imd->il = NULL;
1235
1236         image_set_fd(imd, image_get_fd(source));
1237
1238
1239         if (source->il)
1240                 {
1241                 imd->il = source->il;
1242                 source->il = NULL;
1243
1244                 image_loader_sync_data(imd->il, source, imd);
1245
1246                 imd->delay_alter_type = source->delay_alter_type;
1247                 source->delay_alter_type = ALTER_NONE;
1248                 }
1249
1250         imd->color_profile_enable = source->color_profile_enable;
1251         imd->color_profile_input = source->color_profile_input;
1252         imd->color_profile_use_image = source->color_profile_use_image;
1253         color_man_free((ColorMan *)imd->cm);
1254         imd->cm = NULL;
1255         if (source->cm)
1256                 {
1257                 ColorMan *cm;
1258
1259                 imd->cm = source->cm;
1260                 source->cm = NULL;
1261
1262                 cm = (ColorMan *)imd->cm;
1263                 cm->imd = imd;
1264                 cm->func_done_data = imd;
1265                 }
1266
1267         file_data_unref(imd->read_ahead_fd);
1268         source->read_ahead_fd = NULL;
1269
1270         imd->orientation = source->orientation;
1271         imd->desaturate = source->desaturate;
1272
1273         imd->user_stereo = source->user_stereo;
1274
1275         pixbuf_renderer_move(PIXBUF_RENDERER(imd->pr), PIXBUF_RENDERER(source->pr));
1276
1277         if (imd->cm || imd->desaturate)
1278                 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, image_post_process_tile_color_cb, (gpointer) imd, (imd->cm != NULL) );
1279         else
1280                 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, NULL, NULL, TRUE);
1281
1282 }
1283
1284 /* this is  a copy function
1285  * source stays unchanged
1286  */
1287 void image_copy_from_image(ImageWindow *imd, ImageWindow *source)
1288 {
1289         if (imd == source) return;
1290
1291         imd->unknown = source->unknown;
1292
1293         imd->collection = source->collection;
1294         imd->collection_info = source->collection_info;
1295
1296         image_loader_free(imd->il);
1297         imd->il = NULL;
1298
1299         image_set_fd(imd, image_get_fd(source));
1300
1301
1302         imd->color_profile_enable = source->color_profile_enable;
1303         imd->color_profile_input = source->color_profile_input;
1304         imd->color_profile_use_image = source->color_profile_use_image;
1305         color_man_free((ColorMan *)imd->cm);
1306         imd->cm = NULL;
1307         if (source->cm)
1308                 {
1309                 ColorMan *cm;
1310
1311                 imd->cm = source->cm;
1312                 source->cm = NULL;
1313
1314                 cm = (ColorMan *)imd->cm;
1315                 cm->imd = imd;
1316                 cm->func_done_data = imd;
1317                 }
1318
1319         image_loader_free(imd->read_ahead_il);
1320         imd->read_ahead_il = source->read_ahead_il;
1321         source->read_ahead_il = NULL;
1322         if (imd->read_ahead_il) image_loader_sync_read_ahead_data(imd->read_ahead_il, source, imd);
1323
1324         file_data_unref(imd->read_ahead_fd);
1325         imd->read_ahead_fd = source->read_ahead_fd;
1326         source->read_ahead_fd = NULL;
1327
1328         imd->completed = source->completed;
1329         imd->state = source->state;
1330         source->state = IMAGE_STATE_NONE;
1331
1332         imd->orientation = source->orientation;
1333         imd->desaturate = source->desaturate;
1334
1335         imd->user_stereo = source->user_stereo;
1336
1337         pixbuf_renderer_copy(PIXBUF_RENDERER(imd->pr), PIXBUF_RENDERER(source->pr));
1338
1339         if (imd->cm || imd->desaturate)
1340                 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, image_post_process_tile_color_cb, (gpointer) imd, (imd->cm != NULL) );
1341         else
1342                 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, NULL, NULL, TRUE);
1343
1344 }
1345
1346
1347 /* manipulation */
1348
1349 void image_area_changed(ImageWindow *imd, gint x, gint y, gint width, gint height)
1350 {
1351         pixbuf_renderer_area_changed((PixbufRenderer *)imd->pr, x, y, width, height);
1352 }
1353
1354 void image_reload(ImageWindow *imd)
1355 {
1356         if (pixbuf_renderer_get_tiles((PixbufRenderer *)imd->pr)) return;
1357
1358         image_change_complete(imd, image_zoom_get(imd));
1359 }
1360
1361 void image_scroll(ImageWindow *imd, gint x, gint y)
1362 {
1363         pixbuf_renderer_scroll((PixbufRenderer *)imd->pr, x, y);
1364 }
1365
1366 void image_scroll_to_point(ImageWindow *imd, gint x, gint y,
1367                            gdouble x_align, gdouble y_align)
1368 {
1369         pixbuf_renderer_scroll_to_point((PixbufRenderer *)imd->pr, x, y, x_align, y_align);
1370 }
1371
1372 void image_get_scroll_center(ImageWindow *imd, gdouble *x, gdouble *y)
1373 {
1374         pixbuf_renderer_get_scroll_center(PIXBUF_RENDERER(imd->pr), x, y);
1375 }
1376
1377 void image_set_scroll_center(ImageWindow *imd, gdouble x, gdouble y)
1378 {
1379         pixbuf_renderer_set_scroll_center(PIXBUF_RENDERER(imd->pr), x, y);
1380 }
1381
1382 void image_zoom_adjust(ImageWindow *imd, gdouble increment)
1383 {
1384         pixbuf_renderer_zoom_adjust((PixbufRenderer *)imd->pr, increment);
1385 }
1386
1387 void image_zoom_adjust_at_point(ImageWindow *imd, gdouble increment, gint x, gint y)
1388 {
1389         pixbuf_renderer_zoom_adjust_at_point((PixbufRenderer *)imd->pr, increment, x, y);
1390 }
1391
1392 void image_zoom_set_limits(ImageWindow *imd, gdouble min, gdouble max)
1393 {
1394         pixbuf_renderer_zoom_set_limits((PixbufRenderer *)imd->pr, min, max);
1395 }
1396
1397 void image_zoom_set(ImageWindow *imd, gdouble zoom)
1398 {
1399         pixbuf_renderer_zoom_set((PixbufRenderer *)imd->pr, zoom);
1400 }
1401
1402 void image_zoom_set_fill_geometry(ImageWindow *imd, gboolean vertical)
1403 {
1404         PixbufRenderer *pr;
1405         gdouble zoom;
1406         gint width, height;
1407
1408         pr = (PixbufRenderer *)imd->pr;
1409
1410         if (!pixbuf_renderer_get_pixbuf(pr) ||
1411             !pixbuf_renderer_get_image_size(pr, &width, &height)) return;
1412
1413         if (vertical)
1414                 {
1415                 zoom = (gdouble)pr->viewport_height / height;
1416                 }
1417         else
1418                 {
1419                 zoom = (gdouble)pr->viewport_width / width;
1420                 }
1421
1422         if (zoom < 1.0)
1423                 {
1424                 zoom = 0.0 - 1.0 / zoom;
1425                 }
1426
1427         pixbuf_renderer_zoom_set(pr, zoom);
1428 }
1429
1430 gdouble image_zoom_get(ImageWindow *imd)
1431 {
1432         return pixbuf_renderer_zoom_get((PixbufRenderer *)imd->pr);
1433 }
1434
1435 gdouble image_zoom_get_real(ImageWindow *imd)
1436 {
1437         return pixbuf_renderer_zoom_get_scale((PixbufRenderer *)imd->pr);
1438 }
1439
1440 gchar *image_zoom_get_as_text(ImageWindow *imd)
1441 {
1442         gdouble zoom;
1443         gdouble scale;
1444         gdouble l = 1.0;
1445         gdouble r = 1.0;
1446         gint pl = 0;
1447         gint pr = 0;
1448         gchar *approx = " ";
1449
1450         zoom = image_zoom_get(imd);
1451         scale = image_zoom_get_real(imd);
1452
1453         if (zoom > 0.0)
1454                 {
1455                 l = zoom;
1456                 }
1457         else if (zoom < 0.0)
1458                 {
1459                 r = 0.0 - zoom;
1460                 }
1461         else if (zoom == 0.0 && scale != 0.0)
1462                 {
1463                 if (scale >= 1.0)
1464                         {
1465                         l = scale;
1466                         }
1467                 else
1468                         {
1469                         r = 1.0 / scale;
1470                         }
1471                 approx = "~";
1472                 }
1473
1474         if (rint(l) != l) pl = 2;
1475         if (rint(r) != r) pr = 2;
1476
1477         return g_strdup_printf("%.*f :%s%.*f", pl, l, approx, pr, r);
1478 }
1479
1480 gdouble image_zoom_get_default(ImageWindow *imd)
1481 {
1482         gdouble zoom = 1.0;
1483
1484         switch (options->image.zoom_mode)
1485         {
1486         case ZOOM_RESET_ORIGINAL:
1487                 break;
1488         case ZOOM_RESET_FIT_WINDOW:
1489                 zoom = 0.0;
1490                 break;
1491         case ZOOM_RESET_NONE:
1492                 if (imd) zoom = image_zoom_get(imd);
1493                 break;
1494         }
1495
1496         return zoom;
1497 }
1498
1499 /* stereo */
1500 gint image_stereo_get(ImageWindow *imd)
1501 {
1502         return pixbuf_renderer_stereo_get((PixbufRenderer *)imd->pr);
1503 }
1504
1505 void image_stereo_set(ImageWindow *imd, gint stereo_mode)
1506 {
1507         DEBUG_1("Setting stereo mode %04x for imd %p", stereo_mode, imd);
1508         pixbuf_renderer_stereo_set((PixbufRenderer *)imd->pr, stereo_mode);
1509 }
1510
1511 void image_stereo_swap(ImageWindow *imd)
1512 {
1513         gint stereo_mode = pixbuf_renderer_stereo_get((PixbufRenderer *)imd->pr);
1514         stereo_mode ^= PR_STEREO_SWAP;
1515         pixbuf_renderer_stereo_set((PixbufRenderer *)imd->pr, stereo_mode);
1516 }
1517
1518 StereoPixbufData image_stereo_pixbuf_get(ImageWindow *imd)
1519 {
1520         return imd->user_stereo;
1521 }
1522
1523 void image_stereo_pixbuf_set(ImageWindow *imd, StereoPixbufData stereo_mode)
1524 {
1525         imd->user_stereo = stereo_mode;
1526         image_reload(imd);
1527 }
1528
1529 /* read ahead */
1530
1531 void image_prebuffer_set(ImageWindow *imd, FileData *fd)
1532 {
1533         if (pixbuf_renderer_get_tiles((PixbufRenderer *)imd->pr)) return;
1534
1535         if (fd)
1536                 {
1537                 if (!file_cache_get(image_get_cache(), fd))
1538                         {
1539                         image_read_ahead_set(imd, fd);
1540                         }
1541                 }
1542         else
1543                 {
1544                 image_read_ahead_cancel(imd);
1545                 }
1546 }
1547
1548 static void image_notify_cb(FileData *fd, NotifyType type, gpointer data)
1549 {
1550         ImageWindow *imd = data;
1551
1552         if (!imd || !image_get_pixbuf(imd) ||
1553             /* imd->il || */ /* loading in progress - do not check - it should start from the beginning anyway */
1554             !imd->image_fd || /* nothing to reload */
1555             imd->state == IMAGE_STATE_NONE /* loading not started, no need to reload */
1556             ) return;
1557
1558         if ((type & NOTIFY_REREAD) && fd == imd->image_fd)
1559                 {
1560                 /* there is no need to reload on NOTIFY_CHANGE,
1561                    modified files should be detacted anyway and NOTIFY_REREAD should be recieved
1562                    or they are removed from the filelist completely on "move" and "delete"
1563                 */
1564                 DEBUG_1("Notify image: %s %04x", fd->path, type);
1565                 image_reload(imd);
1566                 }
1567 }
1568
1569 void image_auto_refresh_enable(ImageWindow *imd, gboolean enable)
1570 {
1571         if (!enable && imd->auto_refresh && imd->image_fd)
1572                 file_data_unregister_real_time_monitor(imd->image_fd);
1573
1574         if (enable && !imd->auto_refresh && imd->image_fd)
1575                 file_data_register_real_time_monitor(imd->image_fd);
1576
1577         imd->auto_refresh = enable;
1578 }
1579
1580 void image_top_window_set_sync(ImageWindow *imd, gboolean allow_sync)
1581 {
1582         imd->top_window_sync = allow_sync;
1583
1584         g_object_set(G_OBJECT(imd->pr), "window_fit", allow_sync, NULL);
1585 }
1586
1587 void image_background_set_color(ImageWindow *imd, GdkColor *color)
1588 {
1589         pixbuf_renderer_set_color((PixbufRenderer *)imd->pr, color);
1590 }
1591
1592 void image_background_set_color_from_options(ImageWindow *imd, gboolean fullscreen)
1593 {
1594         GdkColor *color = NULL;
1595
1596         if ((options->image.use_custom_border_color && !fullscreen) ||
1597             (options->image.use_custom_border_color_in_fullscreen && fullscreen))
1598                 {
1599                 color = &options->image.border_color;
1600                 }
1601
1602         image_background_set_color(imd, color);
1603 }
1604
1605 void image_color_profile_set(ImageWindow *imd,
1606                              gint input_type,
1607                              gboolean use_image)
1608 {
1609         if (!imd) return;
1610
1611         if (input_type < 0 || input_type >= COLOR_PROFILE_FILE + COLOR_PROFILE_INPUTS)
1612                 {
1613                 return;
1614                 }
1615
1616         imd->color_profile_input = input_type;
1617         imd->color_profile_use_image = use_image;
1618 }
1619
1620 gboolean image_color_profile_get(ImageWindow *imd,
1621                                  gint *input_type,
1622                                  gboolean *use_image)
1623 {
1624         if (!imd) return FALSE;
1625
1626         if (input_type) *input_type = imd->color_profile_input;
1627         if (use_image) *use_image = imd->color_profile_use_image;
1628
1629         return TRUE;
1630 }
1631
1632 void image_color_profile_set_use(ImageWindow *imd, gboolean enable)
1633 {
1634         if (!imd) return;
1635
1636         if (imd->color_profile_enable == enable) return;
1637
1638         imd->color_profile_enable = enable;
1639 }
1640
1641 gboolean image_color_profile_get_use(ImageWindow *imd)
1642 {
1643         if (!imd) return FALSE;
1644
1645         return imd->color_profile_enable;
1646 }
1647
1648 gboolean image_color_profile_get_status(ImageWindow *imd, gchar **image_profile, gchar **screen_profile)
1649 {
1650         ColorMan *cm;
1651         if (!imd) return FALSE;
1652
1653         cm = imd->cm;
1654         if (!cm) return FALSE;
1655         return color_man_get_status(cm, image_profile, screen_profile);
1656
1657 }
1658
1659 void image_set_delay_flip(ImageWindow *imd, gboolean delay)
1660 {
1661         if (!imd ||
1662             imd->delay_flip == delay) return;
1663
1664         imd->delay_flip = delay;
1665
1666         g_object_set(G_OBJECT(imd->pr), "delay_flip", delay, NULL);
1667
1668         if (!imd->delay_flip && imd->il)
1669                 {
1670                 PixbufRenderer *pr;
1671
1672                 pr = PIXBUF_RENDERER(imd->pr);
1673                 if (pr->pixbuf) g_object_unref(pr->pixbuf);
1674                 pr->pixbuf = NULL;
1675
1676                 image_load_pixbuf_ready(imd);
1677                 }
1678 }
1679
1680 void image_to_root_window(ImageWindow *imd, gboolean scaled)
1681 {
1682 #if !GTK_CHECK_VERSION(3,0,0)
1683         GdkScreen *screen;
1684         GdkWindow *rootwindow;
1685         GdkPixmap *pixmap;
1686         GdkPixbuf *pixbuf;
1687         GdkPixbuf *pb;
1688         gint width, height;
1689
1690         if (!imd) return;
1691
1692         pixbuf = image_get_pixbuf(imd);
1693         if (!pixbuf) return;
1694
1695         screen = gtk_widget_get_screen(imd->widget);
1696         rootwindow = gdk_screen_get_root_window(screen);
1697         if (gdk_window_get_visual(rootwindow) != gdk_visual_get_system()) return;
1698
1699         if (scaled)
1700                 {
1701                 width = gdk_screen_width();
1702                 height = gdk_screen_height();
1703                 }
1704         else
1705                 {
1706                 pixbuf_renderer_get_scaled_size((PixbufRenderer *)imd->pr, &width, &height);
1707                 }
1708
1709         pb = gdk_pixbuf_scale_simple(pixbuf, width, height, (GdkInterpType)options->image.zoom_quality);
1710
1711         gdk_pixbuf_render_pixmap_and_mask(pb, &pixmap, NULL, 128);
1712         gdk_window_set_back_pixmap(rootwindow, pixmap, FALSE);
1713         gdk_window_clear(rootwindow);
1714         g_object_unref(pb);
1715         g_object_unref(pixmap);
1716
1717         gdk_flush();
1718 #endif
1719 }
1720
1721 void image_select(ImageWindow *imd, gboolean select)
1722 {
1723         if (!imd->has_frame) return;
1724
1725         if (select)
1726                 {
1727                 gtk_widget_set_state(imd->widget, GTK_STATE_SELECTED);
1728                 gtk_widget_set_state(imd->pr, GTK_STATE_NORMAL); /* do not propagate */
1729                 }
1730         else
1731                 gtk_widget_set_state(imd->widget, GTK_STATE_NORMAL);
1732 }
1733
1734 void image_set_selectable(ImageWindow *imd, gboolean selectable)
1735 {
1736         if (!imd->has_frame) return;
1737
1738         gtk_frame_set_shadow_type(GTK_FRAME(imd->frame), GTK_SHADOW_NONE);
1739         gtk_container_set_border_width(GTK_CONTAINER(imd->frame), selectable ? 4 : 0);
1740 }
1741
1742 void image_grab_focus(ImageWindow *imd)
1743 {
1744         if (imd->has_frame)
1745                 {
1746                 gtk_widget_grab_focus(imd->frame);
1747                 }
1748         else
1749                 {
1750                 gtk_widget_grab_focus(imd->widget);
1751                 }
1752 }
1753
1754
1755 /*
1756  *-------------------------------------------------------------------
1757  * prefs sync
1758  *-------------------------------------------------------------------
1759  */
1760
1761 static void image_options_set(ImageWindow *imd)
1762 {
1763         g_object_set(G_OBJECT(imd->pr), "zoom_quality", options->image.zoom_quality,
1764                                         "zoom_2pass", options->image.zoom_2pass,
1765                                         "zoom_expand", options->image.zoom_to_fit_allow_expand,
1766                                         "scroll_reset", options->image.scroll_reset_method,
1767                                         "cache_display", options->image.tile_cache_max,
1768                                         "window_fit", (imd->top_window_sync && options->image.fit_window_to_image),
1769                                         "window_limit", options->image.limit_window_size,
1770                                         "window_limit_value", options->image.max_window_size,
1771                                         "autofit_limit", options->image.limit_autofit_size,
1772                                         "autofit_limit_value", options->image.max_autofit_size,
1773                                         "enlargement_limit_value", options->image.max_enlargement_size,
1774
1775                                         NULL);
1776
1777         pixbuf_renderer_set_parent((PixbufRenderer *)imd->pr, (GtkWindow *)imd->top_window);
1778
1779         image_stereo_set(imd, options->stereo.mode);
1780         pixbuf_renderer_stereo_fixed_set((PixbufRenderer *)imd->pr,
1781                                         options->stereo.fixed_w, options->stereo.fixed_h,
1782                                         options->stereo.fixed_x1, options->stereo.fixed_y1,
1783                                         options->stereo.fixed_x2, options->stereo.fixed_y2);
1784 }
1785
1786 void image_options_sync(void)
1787 {
1788         GList *work;
1789
1790         work = image_list;
1791         while (work)
1792                 {
1793                 ImageWindow *imd;
1794
1795                 imd = work->data;
1796                 work = work->next;
1797
1798                 image_options_set(imd);
1799                 }
1800 }
1801
1802 /*
1803  *-------------------------------------------------------------------
1804  * init / destroy
1805  *-------------------------------------------------------------------
1806  */
1807
1808 static void image_free(ImageWindow *imd)
1809 {
1810         image_list = g_list_remove(image_list, imd);
1811
1812         if (imd->auto_refresh && imd->image_fd)
1813                 file_data_unregister_real_time_monitor(imd->image_fd);
1814
1815         file_data_unregister_notify_func(image_notify_cb, imd);
1816
1817         image_reset(imd);
1818
1819         image_read_ahead_cancel(imd);
1820
1821         file_data_unref(imd->image_fd);
1822         g_free(imd->title);
1823         g_free(imd->title_right);
1824         g_free(imd);
1825 }
1826
1827 static void image_destroy_cb(GtkWidget *widget, gpointer data)
1828 {
1829         ImageWindow *imd = data;
1830         image_free(imd);
1831 }
1832 #if GTK_CHECK_VERSION(3,0,0)
1833 gboolean selectable_frame_draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
1834 {
1835         GtkAllocation allocation;
1836         gtk_widget_get_allocation(widget, &allocation);
1837         gtk_paint_flat_box(gtk_widget_get_style(widget),
1838                            cr,
1839                            gtk_widget_get_state(widget),
1840                            gtk_frame_get_shadow_type(GTK_FRAME(widget)),
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), cr, GTK_STATE_ACTIVE,
1849                                 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), cr, GTK_STATE_NORMAL, GTK_SHADOW_IN,
1856                                  widget, "image_window",
1857                                  allocation.x, allocation.y,
1858                                  allocation.width - 1, allocation.height - 1);
1859                 }
1860         return FALSE;
1861 }
1862
1863 #else
1864 gboolean selectable_frame_expose_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
1865 {
1866         GtkAllocation allocation;
1867         gtk_widget_get_allocation(widget, &allocation);
1868         gtk_paint_flat_box(gtk_widget_get_style(widget),
1869                            gtk_widget_get_window(widget),
1870                            gtk_widget_get_state(widget),
1871                            gtk_frame_get_shadow_type(GTK_FRAME(widget)),
1872                            NULL,
1873                            widget,
1874                            NULL,
1875                            allocation.x + 3, allocation.y + 3,
1876                            allocation.width - 6, allocation.height - 6);
1877
1878         if (gtk_widget_has_focus(widget))
1879                 {
1880                 gtk_paint_focus(gtk_widget_get_style(widget), gtk_widget_get_window(widget), GTK_STATE_ACTIVE,
1881                                 &event->area, widget, "image_window",
1882                                 allocation.x, allocation.y,
1883                                 allocation.width - 1, allocation.height - 1);
1884                 }
1885         else
1886                 {
1887                 gtk_paint_shadow(gtk_widget_get_style(widget), gtk_widget_get_window(widget), GTK_STATE_NORMAL, GTK_SHADOW_IN,
1888                                  &event->area, widget, "image_window",
1889                                  allocation.x, allocation.y,
1890                                  allocation.width - 1, allocation.height - 1);
1891                 }
1892         return FALSE;
1893 }
1894 #endif
1895
1896
1897 void image_set_frame(ImageWindow *imd, gboolean frame)
1898 {
1899         frame = !!frame;
1900
1901         if (frame == imd->has_frame) return;
1902
1903         gtk_widget_hide(imd->pr);
1904
1905         if (frame)
1906                 {
1907                 imd->frame = gtk_frame_new(NULL);
1908                 g_object_ref(imd->pr);
1909                 if (imd->has_frame != -1) gtk_container_remove(GTK_CONTAINER(imd->widget), imd->pr);
1910                 gtk_container_add(GTK_CONTAINER(imd->frame), imd->pr);
1911
1912                 g_object_unref(imd->pr);
1913                 gtk_widget_set_can_focus(imd->frame, TRUE);
1914                 gtk_widget_set_app_paintable(imd->frame, TRUE);
1915
1916 #if GTK_CHECK_VERSION(3,0,0)
1917                 g_signal_connect(G_OBJECT(imd->frame), "draw",
1918                                  G_CALLBACK(selectable_frame_draw_cb), NULL);
1919 #else
1920                 g_signal_connect(G_OBJECT(imd->frame), "expose_event",
1921                                  G_CALLBACK(selectable_frame_expose_cb), NULL);
1922 #endif
1923                 g_signal_connect(G_OBJECT(imd->frame), "focus_in_event",
1924                                  G_CALLBACK(image_focus_in_cb), imd);
1925
1926                 gtk_box_pack_start(GTK_BOX(imd->widget), imd->frame, TRUE, TRUE, 0);
1927                 gtk_widget_show(imd->frame);
1928                 }
1929         else
1930                 {
1931                 g_object_ref(imd->pr);
1932                 if (imd->frame)
1933                         {
1934                         gtk_container_remove(GTK_CONTAINER(imd->frame), imd->pr);
1935                         gtk_widget_destroy(imd->frame);
1936                         imd->frame = NULL;
1937                         }
1938                 gtk_box_pack_start(GTK_BOX(imd->widget), imd->pr, TRUE, TRUE, 0);
1939
1940                 g_object_unref(imd->pr);
1941                 }
1942
1943         gtk_widget_show(imd->pr);
1944
1945         imd->has_frame = frame;
1946 }
1947
1948 ImageWindow *image_new(gboolean frame)
1949 {
1950         ImageWindow *imd;
1951
1952         imd = g_new0(ImageWindow, 1);
1953
1954         imd->unknown = TRUE;
1955         imd->has_frame = -1; /* not initialized; for image_set_frame */
1956         imd->delay_alter_type = ALTER_NONE;
1957         imd->state = IMAGE_STATE_NONE;
1958         imd->color_profile_from_image = COLOR_PROFILE_NONE;
1959         imd->orientation = 1;
1960
1961         imd->pr = GTK_WIDGET(pixbuf_renderer_new());
1962
1963         image_options_set(imd);
1964
1965         imd->widget = gtk_vbox_new(0, 0);
1966
1967         image_set_frame(imd, frame);
1968
1969         image_set_selectable(imd, 0);
1970
1971         g_signal_connect(G_OBJECT(imd->pr), "clicked",
1972                          G_CALLBACK(image_click_cb), imd);
1973         g_signal_connect(G_OBJECT(imd->pr), "button_press_event",
1974                          G_CALLBACK(image_press_cb), imd);
1975         g_signal_connect(G_OBJECT(imd->pr), "scroll_notify",
1976                          G_CALLBACK(image_scroll_notify_cb), imd);
1977
1978         g_signal_connect(G_OBJECT(imd->pr), "scroll_event",
1979                          G_CALLBACK(image_scroll_cb), imd);
1980
1981         g_signal_connect(G_OBJECT(imd->pr), "destroy",
1982                          G_CALLBACK(image_destroy_cb), imd);
1983
1984         g_signal_connect(G_OBJECT(imd->pr), "zoom",
1985                          G_CALLBACK(image_zoom_cb), imd);
1986         g_signal_connect(G_OBJECT(imd->pr), "render_complete",
1987                          G_CALLBACK(image_render_complete_cb), imd);
1988         g_signal_connect(G_OBJECT(imd->pr), "drag",
1989                          G_CALLBACK(image_drag_cb), imd);
1990
1991         file_data_register_notify_func(image_notify_cb, imd, NOTIFY_PRIORITY_LOW);
1992
1993         image_list = g_list_append(image_list, imd);
1994
1995         return imd;
1996 }
1997 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */