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