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