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