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