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