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