fe262beb34bb57d2fd7667425a974f07b45fa93e
[geeqie.git] / src / pixbuf-renderer.cc
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 <cmath>
23 #include <cstdlib>
24 #include <cstring>
25
26 #include "main.h"
27 #include "pixbuf-renderer.h"
28
29 #include "renderer-tiles.h"
30
31 /* comment this out if not using this from within Geeqie
32  * defining GQ_BUILD does these things:
33  *   - Sets the shift-click scroller pixbuf to a nice icon instead of a black box
34  */
35 #define GQ_BUILD 1
36
37 #ifdef GQ_BUILD
38 #include "main.h"
39 #include "pixbuf-util.h"
40 #include "exif.h"
41 #else
42 enum ExifOrientationType {
43         EXIF_ORIENTATION_UNKNOWN        = 0,
44         EXIF_ORIENTATION_TOP_LEFT       = 1,
45         EXIF_ORIENTATION_TOP_RIGHT      = 2,
46         EXIF_ORIENTATION_BOTTOM_RIGHT   = 3,
47         EXIF_ORIENTATION_BOTTOM_LEFT    = 4,
48         EXIF_ORIENTATION_LEFT_TOP       = 5,
49         EXIF_ORIENTATION_RIGHT_TOP      = 6,
50         EXIF_ORIENTATION_RIGHT_BOTTOM   = 7,
51         EXIF_ORIENTATION_LEFT_BOTTOM    = 8
52 };
53 #endif
54
55
56 /* default min and max zoom */
57 #define PR_ZOOM_MIN (-32.0)
58 #define PR_ZOOM_MAX 32.0
59
60 /* distance to drag mouse to disable image flip */
61 enum {
62         PR_DRAG_SCROLL_THRESHHOLD = 4
63 };
64
65 /* increase pan rate when holding down shift */
66 enum {
67         PR_PAN_SHIFT_MULTIPLIER = 6
68 };
69
70 /* scroller config */
71 enum {
72         PR_SCROLLER_UPDATES_PER_SEC = 30,
73         PR_SCROLLER_DEAD_ZONE = 6
74 };
75
76 /* when scaling image to below this size, use nearest pixel for scaling
77  * (below about 4, the other scale types become slow generating their conversion tables)
78  */
79 //~ enum {
80 //~ PR_MIN_SCALE_SIZE = 8
81 //~ };
82
83 enum {
84         SIGNAL_ZOOM = 0,
85         SIGNAL_CLICKED,
86         SIGNAL_SCROLL_NOTIFY,
87         SIGNAL_RENDER_COMPLETE,
88         SIGNAL_DRAG,
89         SIGNAL_UPDATE_PIXEL,
90         SIGNAL_COUNT
91 };
92
93 enum {
94         PROP_0,
95         PROP_ZOOM_MIN,
96         PROP_ZOOM_MAX,
97         PROP_ZOOM_QUALITY,
98         PROP_ZOOM_2PASS,
99         PROP_ZOOM_EXPAND,
100         PROP_SCROLL_RESET,
101         PROP_DELAY_FLIP,
102         PROP_LOADING,
103         PROP_COMPLETE,
104         PROP_CACHE_SIZE_DISPLAY,
105         PROP_CACHE_SIZE_TILES,
106         PROP_WINDOW_FIT,
107         PROP_WINDOW_LIMIT,
108         PROP_WINDOW_LIMIT_VALUE,
109         PROP_AUTOFIT_LIMIT,
110         PROP_AUTOFIT_LIMIT_VALUE,
111         PROP_ENLARGEMENT_LIMIT_VALUE
112 };
113
114 enum PrZoomFlags {
115         PR_ZOOM_NONE            = 0,
116         PR_ZOOM_FORCE           = 1 << 0,
117         PR_ZOOM_NEW             = 1 << 1,
118         PR_ZOOM_CENTER          = 1 << 2,
119         PR_ZOOM_INVALIDATE      = 1 << 3,
120         PR_ZOOM_LAZY            = 1 << 4  /* wait with redraw for pixbuf_renderer_area_changed */
121 };
122
123 static guint signals[SIGNAL_COUNT] = { 0 };
124 static GtkEventBoxClass *parent_class = nullptr;
125
126
127
128 static void pixbuf_renderer_class_init(PixbufRendererClass *renderer_class);
129 static void pixbuf_renderer_init(PixbufRenderer *pr);
130 static void pixbuf_renderer_finalize(GObject *object);
131 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
132                                          const GValue *value, GParamSpec *pspec);
133 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
134                                          GValue *value, GParamSpec *pspec);
135 static void pr_scroller_timer_set(PixbufRenderer *pr, gboolean start);
136
137
138 static void pr_source_tile_free_all(PixbufRenderer *pr);
139
140 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
141                          PrZoomFlags flags, gint px, gint py);
142
143 static void pr_signals_connect(PixbufRenderer *pr);
144 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data);
145 static void pr_stereo_temp_disable(PixbufRenderer *pr, gboolean disable);
146
147
148 /*
149  *-------------------------------------------------------------------
150  * Pixbuf Renderer object
151  *-------------------------------------------------------------------
152  */
153
154 static void pixbuf_renderer_class_init_wrapper(void *g_class, void *)
155 {
156         pixbuf_renderer_class_init(static_cast<PixbufRendererClass *>(g_class));
157 }
158
159 static void pixbuf_renderer_init_wrapper(PixbufRenderer *pr, void *)
160 {
161         pixbuf_renderer_init(pr);
162 }
163
164 GType pixbuf_renderer_get_type()
165 {
166         static GType pixbuf_renderer_type = 0;
167
168         if (!pixbuf_renderer_type)
169                 {
170                 static const GTypeInfo pixbuf_renderer_info =
171                         {
172                         sizeof(PixbufRendererClass), /* class_size */
173                         nullptr,                /* base_init */
174                         nullptr,                /* base_finalize */
175                         static_cast<GClassInitFunc>(pixbuf_renderer_class_init_wrapper),
176                         nullptr,                /* class_finalize */
177                         nullptr,                /* class_data */
178                         sizeof(PixbufRenderer), /* instance_size */
179                         0,              /* n_preallocs */
180                         reinterpret_cast<GInstanceInitFunc>(pixbuf_renderer_init_wrapper), /* instance_init */
181                         nullptr,                /* value_table */
182                         };
183
184                 pixbuf_renderer_type = g_type_register_static(GTK_TYPE_EVENT_BOX, "PixbufRenderer",
185                                                               &pixbuf_renderer_info, static_cast<GTypeFlags>(0));
186                 }
187
188         return pixbuf_renderer_type;
189 }
190
191 static void pixbuf_renderer_class_init(PixbufRendererClass *renderer_class)
192 {
193         GObjectClass *gobject_class = G_OBJECT_CLASS(renderer_class);
194
195         parent_class = static_cast<GtkEventBoxClass *>(g_type_class_peek_parent(renderer_class));
196
197         gobject_class->set_property = pixbuf_renderer_set_property;
198         gobject_class->get_property = pixbuf_renderer_get_property;
199
200         gobject_class->finalize = pixbuf_renderer_finalize;
201
202         g_object_class_install_property(gobject_class,
203                                         PROP_ZOOM_MIN,
204                                         g_param_spec_double("zoom_min",
205                                                             "Zoom minimum",
206                                                             nullptr,
207                                                             -1000.0,
208                                                             1000.0,
209                                                             PR_ZOOM_MIN,
210                                                             static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_WRITABLE)));
211
212         g_object_class_install_property(gobject_class,
213                                         PROP_ZOOM_MAX,
214                                         g_param_spec_double("zoom_max",
215                                                             "Zoom maximum",
216                                                             nullptr,
217                                                             -1000.0,
218                                                             1000.0,
219                                                             PR_ZOOM_MIN,
220                                                             static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_WRITABLE)));
221
222         g_object_class_install_property(gobject_class,
223                                         PROP_ZOOM_QUALITY,
224                                         g_param_spec_uint("zoom_quality",
225                                                           "Zoom quality",
226                                                           nullptr,
227                                                           GDK_INTERP_NEAREST,
228                                                           GDK_INTERP_BILINEAR,
229                                                           GDK_INTERP_BILINEAR,
230                                                           static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_WRITABLE)));
231
232         g_object_class_install_property(gobject_class,
233                                         PROP_ZOOM_2PASS,
234                                         g_param_spec_boolean("zoom_2pass",
235                                                              "2 pass zoom",
236                                                              nullptr,
237                                                              TRUE,
238                                                              static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_WRITABLE)));
239
240         g_object_class_install_property(gobject_class,
241                                         PROP_ZOOM_EXPAND,
242                                         g_param_spec_boolean("zoom_expand",
243                                                              "Expand image in autozoom.",
244                                                              nullptr,
245                                                              FALSE,
246                                                              static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_WRITABLE)));
247         g_object_class_install_property(gobject_class,
248                                         PROP_SCROLL_RESET,
249                                         g_param_spec_uint("scroll_reset",
250                                                           "New image scroll reset",
251                                                           nullptr,
252                                                           PR_SCROLL_RESET_TOPLEFT,
253                                                           PR_SCROLL_RESET_NOCHANGE,
254                                                           PR_SCROLL_RESET_TOPLEFT,
255                                                           static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_WRITABLE)));
256
257         g_object_class_install_property(gobject_class,
258                                         PROP_DELAY_FLIP,
259                                         g_param_spec_boolean("delay_flip",
260                                                              "Delay image update",
261                                                              nullptr,
262                                                              FALSE,
263                                                              static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_WRITABLE)));
264
265         g_object_class_install_property(gobject_class,
266                                         PROP_LOADING,
267                                         g_param_spec_boolean("loading",
268                                                              "Image actively loading",
269                                                              nullptr,
270                                                              FALSE,
271                                                              static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_WRITABLE)));
272
273         g_object_class_install_property(gobject_class,
274                                         PROP_COMPLETE,
275                                         g_param_spec_boolean("complete",
276                                                              "Image rendering complete",
277                                                              nullptr,
278                                                              FALSE,
279                                                              static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_WRITABLE)));
280
281         g_object_class_install_property(gobject_class,
282                                         PROP_CACHE_SIZE_DISPLAY,
283                                         g_param_spec_uint("cache_display",
284                                                           "Display cache size MiB",
285                                                           nullptr,
286                                                           0,
287                                                           128,
288                                                           PR_CACHE_SIZE_DEFAULT,
289                                                           static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_WRITABLE)));
290
291         g_object_class_install_property(gobject_class,
292                                         PROP_CACHE_SIZE_TILES,
293                                         g_param_spec_uint("cache_tiles",
294                                                           "Tile cache count",
295                                                           "Number of tiles to retain in memory at any one time.",
296                                                           0,
297                                                           256,
298                                                           PR_CACHE_SIZE_DEFAULT,
299                                                           static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_WRITABLE)));
300
301         g_object_class_install_property(gobject_class,
302                                         PROP_WINDOW_FIT,
303                                         g_param_spec_boolean("window_fit",
304                                                              "Fit window to image size",
305                                                              nullptr,
306                                                              FALSE,
307                                                              static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_WRITABLE)));
308
309         g_object_class_install_property(gobject_class,
310                                         PROP_WINDOW_LIMIT,
311                                         g_param_spec_boolean("window_limit",
312                                                              "Limit size of parent window",
313                                                              nullptr,
314                                                              FALSE,
315                                                              static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_WRITABLE)));
316
317         g_object_class_install_property(gobject_class,
318                                         PROP_WINDOW_LIMIT_VALUE,
319                                         g_param_spec_uint("window_limit_value",
320                                                           "Size limit of parent window",
321                                                           nullptr,
322                                                           10,
323                                                           150,
324                                                           100,
325                                                           static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_WRITABLE)));
326
327         g_object_class_install_property(gobject_class,
328                                         PROP_AUTOFIT_LIMIT,
329                                         g_param_spec_boolean("autofit_limit",
330                                                              "Limit size of image when autofitting",
331                                                              nullptr,
332                                                              FALSE,
333                                                              static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_WRITABLE)));
334
335         g_object_class_install_property(gobject_class,
336                                         PROP_AUTOFIT_LIMIT_VALUE,
337                                         g_param_spec_uint("autofit_limit_value",
338                                                           "Size limit of image when autofitting",
339                                                           nullptr,
340                                                           10,
341                                                           150,
342                                                           100,
343                                                           static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_WRITABLE)));
344
345         g_object_class_install_property(gobject_class,
346                                         PROP_ENLARGEMENT_LIMIT_VALUE,
347                                         g_param_spec_uint("enlargement_limit_value",
348                                                           "Size increase limit of image when autofitting",
349                                                           nullptr,
350                                                           100,
351                                                           999,
352                                                           500,
353                                                           static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_WRITABLE)));
354
355
356         signals[SIGNAL_ZOOM] =
357                 g_signal_new("zoom",
358                              G_OBJECT_CLASS_TYPE(gobject_class),
359                              G_SIGNAL_RUN_LAST,
360                              G_STRUCT_OFFSET(PixbufRendererClass, zoom),
361                              nullptr, nullptr,
362                              g_cclosure_marshal_VOID__DOUBLE,
363                              G_TYPE_NONE, 1,
364                              G_TYPE_DOUBLE);
365
366         signals[SIGNAL_CLICKED] =
367                 g_signal_new("clicked",
368                              G_OBJECT_CLASS_TYPE(gobject_class),
369                              G_SIGNAL_RUN_LAST,
370                              G_STRUCT_OFFSET(PixbufRendererClass, clicked),
371                              nullptr, nullptr,
372                              g_cclosure_marshal_VOID__BOXED,
373                              G_TYPE_NONE, 1,
374                              GDK_TYPE_EVENT);
375
376         signals[SIGNAL_SCROLL_NOTIFY] =
377                 g_signal_new("scroll-notify",
378                              G_OBJECT_CLASS_TYPE(gobject_class),
379                              G_SIGNAL_RUN_LAST,
380                              G_STRUCT_OFFSET(PixbufRendererClass, scroll_notify),
381                              nullptr, nullptr,
382                              g_cclosure_marshal_VOID__VOID,
383                              G_TYPE_NONE, 0);
384
385         signals[SIGNAL_RENDER_COMPLETE] =
386                 g_signal_new("render-complete",
387                              G_OBJECT_CLASS_TYPE(gobject_class),
388                              G_SIGNAL_RUN_LAST,
389                              G_STRUCT_OFFSET(PixbufRendererClass, render_complete),
390                              nullptr, nullptr,
391                              g_cclosure_marshal_VOID__VOID,
392                              G_TYPE_NONE, 0);
393
394         signals[SIGNAL_DRAG] =
395                 g_signal_new("drag",
396                              G_OBJECT_CLASS_TYPE(gobject_class),
397                              G_SIGNAL_RUN_LAST,
398                              G_STRUCT_OFFSET(PixbufRendererClass, drag),
399                              nullptr, nullptr,
400                              g_cclosure_marshal_VOID__BOXED,
401                              G_TYPE_NONE, 1,
402                              GDK_TYPE_EVENT);
403
404         signals[SIGNAL_UPDATE_PIXEL] =
405                 g_signal_new("update-pixel",
406                              G_OBJECT_CLASS_TYPE(gobject_class),
407                              G_SIGNAL_RUN_LAST,
408                              G_STRUCT_OFFSET(PixbufRendererClass, update_pixel),
409                              nullptr, nullptr,
410                              g_cclosure_marshal_VOID__VOID,
411                              G_TYPE_NONE, 0);
412 }
413
414 static RendererFuncs *pr_backend_renderer_new(PixbufRenderer *pr)
415 {
416         return renderer_tiles_new(pr);
417 }
418
419
420 static void pixbuf_renderer_init(PixbufRenderer *pr)
421 {
422         GtkWidget *box;
423
424         box = GTK_WIDGET(pr);
425
426         pr->zoom_min = PR_ZOOM_MIN;
427         pr->zoom_max = PR_ZOOM_MAX;
428         pr->zoom_quality = GDK_INTERP_BILINEAR;
429         pr->zoom_2pass = FALSE;
430
431         pr->zoom = 1.0;
432         pr->scale = 1.0;
433         pr->aspect_ratio = 1.0;
434
435         pr->scroll_reset = PR_SCROLL_RESET_TOPLEFT;
436
437         pr->scroller_id = 0;
438         pr->scroller_overlay = -1;
439
440         pr->x_mouse = -1;
441         pr->y_mouse = -1;
442
443         pr->source_tiles_enabled = FALSE;
444         pr->source_tiles = nullptr;
445
446         pr->orientation = 1;
447
448         pr->norm_center_x = 0.5;
449         pr->norm_center_y = 0.5;
450
451         pr->stereo_mode = PR_STEREO_NONE;
452
453         pr->color.red =0;
454         pr->color.green =0;
455         pr->color.blue =0;
456
457         pr->renderer = pr_backend_renderer_new(pr);
458
459         pr->renderer2 = nullptr;
460
461         gtk_widget_set_double_buffered(box, FALSE);
462         gtk_widget_set_app_paintable(box, TRUE);
463         g_signal_connect_after(G_OBJECT(box), "size_allocate",
464                                G_CALLBACK(pr_size_cb), pr);
465
466         pr_signals_connect(pr);
467 }
468
469 static void pixbuf_renderer_finalize(GObject *object)
470 {
471         PixbufRenderer *pr;
472
473         pr = PIXBUF_RENDERER(object);
474
475         pr->renderer->free(pr->renderer);
476         if (pr->renderer2) pr->renderer2->free(pr->renderer2);
477
478
479         if (pr->pixbuf) g_object_unref(pr->pixbuf);
480
481         pr_scroller_timer_set(pr, FALSE);
482
483         pr_source_tile_free_all(pr);
484 }
485
486 PixbufRenderer *pixbuf_renderer_new()
487 {
488         return static_cast<PixbufRenderer *>(g_object_new(TYPE_PIXBUF_RENDERER, nullptr));
489 }
490
491 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
492                                          const GValue *value, GParamSpec *pspec)
493 {
494         PixbufRenderer *pr;
495
496         pr = PIXBUF_RENDERER(object);
497
498         switch (prop_id)
499                 {
500                 case PROP_ZOOM_MIN:
501                         pr->zoom_min = g_value_get_double(value);
502                         break;
503                 case PROP_ZOOM_MAX:
504                         pr->zoom_max = g_value_get_double(value);
505                         break;
506                 case PROP_ZOOM_QUALITY:
507                         pr->zoom_quality = static_cast<GdkInterpType>(g_value_get_uint(value));
508                         break;
509                 case PROP_ZOOM_2PASS:
510                         pr->zoom_2pass = g_value_get_boolean(value);
511                         break;
512                 case PROP_ZOOM_EXPAND:
513                         pr->zoom_expand = g_value_get_boolean(value);
514                         break;
515                 case PROP_SCROLL_RESET:
516                         pr->scroll_reset = static_cast<PixbufRendererScrollResetType>(g_value_get_uint(value));
517                         break;
518                 case PROP_DELAY_FLIP:
519                         pr->delay_flip = g_value_get_boolean(value);
520                         break;
521                 case PROP_LOADING:
522                         pr->loading = g_value_get_boolean(value);
523                         break;
524                 case PROP_COMPLETE:
525                         pr->complete = g_value_get_boolean(value);
526                         break;
527                 case PROP_CACHE_SIZE_DISPLAY:
528                         break;
529                 case PROP_CACHE_SIZE_TILES:
530                         pr->source_tiles_cache_size = g_value_get_uint(value);
531                         break;
532                 case PROP_WINDOW_FIT:
533                         pr->window_fit = g_value_get_boolean(value);
534                         break;
535                 case PROP_WINDOW_LIMIT:
536                         pr->window_limit = g_value_get_boolean(value);
537                         break;
538                 case PROP_WINDOW_LIMIT_VALUE:
539                         pr->window_limit_size = g_value_get_uint(value);
540                         break;
541                 case PROP_AUTOFIT_LIMIT:
542                         pr->autofit_limit = g_value_get_boolean(value);
543                         break;
544                 case PROP_AUTOFIT_LIMIT_VALUE:
545                         pr->autofit_limit_size = g_value_get_uint(value);
546                         break;
547                 case PROP_ENLARGEMENT_LIMIT_VALUE:
548                         pr->enlargement_limit_size = g_value_get_uint(value);
549                         break;
550                 default:
551                         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
552                         break;
553                 }
554 }
555
556 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
557                                          GValue *value, GParamSpec *pspec)
558 {
559         PixbufRenderer *pr;
560
561         pr = PIXBUF_RENDERER(object);
562
563         switch (prop_id)
564                 {
565                 case PROP_ZOOM_MIN:
566                         g_value_set_double(value, pr->zoom_min);
567                         break;
568                 case PROP_ZOOM_MAX:
569                         g_value_set_double(value, pr->zoom_max);
570                         break;
571                 case PROP_ZOOM_QUALITY:
572                         g_value_set_uint(value, pr->zoom_quality);
573                         break;
574                 case PROP_ZOOM_2PASS:
575                         g_value_set_boolean(value, pr->zoom_2pass);
576                         break;
577                 case PROP_ZOOM_EXPAND:
578                         g_value_set_boolean(value, pr->zoom_expand);
579                         break;
580                 case PROP_SCROLL_RESET:
581                         g_value_set_uint(value, pr->scroll_reset);
582                         break;
583                 case PROP_DELAY_FLIP:
584                         g_value_set_boolean(value, pr->delay_flip);
585                         break;
586                 case PROP_LOADING:
587                         g_value_set_boolean(value, pr->loading);
588                         break;
589                 case PROP_COMPLETE:
590                         g_value_set_boolean(value, pr->complete);
591                         break;
592                 case PROP_CACHE_SIZE_DISPLAY:
593                         break;
594                 case PROP_CACHE_SIZE_TILES:
595                         g_value_set_uint(value, pr->source_tiles_cache_size);
596                         break;
597                 case PROP_WINDOW_FIT:
598                         g_value_set_boolean(value, pr->window_fit);
599                         break;
600                 case PROP_WINDOW_LIMIT:
601                         g_value_set_boolean(value, pr->window_limit);
602                         break;
603                 case PROP_WINDOW_LIMIT_VALUE:
604                         g_value_set_uint(value, pr->window_limit_size);
605                         break;
606                 case PROP_AUTOFIT_LIMIT:
607                         g_value_set_boolean(value, pr->autofit_limit);
608                         break;
609                 case PROP_AUTOFIT_LIMIT_VALUE:
610                         g_value_set_uint(value, pr->autofit_limit_size);
611                         break;
612                 case PROP_ENLARGEMENT_LIMIT_VALUE:
613                         g_value_set_uint(value, pr->enlargement_limit_size);
614                         break;
615                 default:
616                         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
617                         break;
618                 }
619 }
620
621
622 /*
623  *-------------------------------------------------------------------
624  * misc utilities
625  *-------------------------------------------------------------------
626  */
627
628 static void widget_set_cursor(GtkWidget *widget, gint icon)
629 {
630         GdkCursor *cursor;
631         GdkDisplay *display;
632
633         if (!gtk_widget_get_window(widget)) return;
634
635         display = gdk_display_get_default();
636
637         if (icon == -1)
638                 {
639                 cursor = nullptr;
640                 }
641         else
642                 {
643                 cursor = gdk_cursor_new_for_display(display, static_cast<GdkCursorType>(icon));
644                 }
645
646         gdk_window_set_cursor(gtk_widget_get_window(widget), cursor);
647
648         if (cursor) g_object_unref(G_OBJECT(cursor));
649 }
650
651 gboolean pr_clip_region(gint x, gint y, gint w, gint h,
652                                gint clip_x, gint clip_y, gint clip_w, gint clip_h,
653                                gint *rx, gint *ry, gint *rw, gint *rh)
654 {
655         if (clip_x + clip_w <= x ||
656             clip_x >= x + w ||
657             clip_y + clip_h <= y ||
658             clip_y >= y + h)
659                 {
660                 return FALSE;
661                 }
662
663         *rx = MAX(x, clip_x);
664         *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
665
666         *ry = MAX(y, clip_y);
667         *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
668
669         return TRUE;
670 }
671
672 static gboolean pr_parent_window_sizable(PixbufRenderer *pr)
673 {
674         GdkWindowState state;
675
676         if (!pr->parent_window) return FALSE;
677         if (!pr->window_fit) return FALSE;
678         if (!gtk_widget_get_window(GTK_WIDGET(pr))) return FALSE;
679
680         if (!gtk_widget_get_window(pr->parent_window)) return FALSE;
681         state = gdk_window_get_state(gtk_widget_get_window(pr->parent_window));
682         if (state & GDK_WINDOW_STATE_MAXIMIZED) return FALSE;
683
684         return TRUE;
685 }
686
687 static gboolean pr_parent_window_resize(PixbufRenderer *pr, gint w, gint h)
688 {
689         GtkWidget *widget;
690         GtkWidget *parent;
691         gint ww;
692         gint wh;
693         GtkAllocation widget_allocation;
694         GtkAllocation parent_allocation;
695
696         if (!pr_parent_window_sizable(pr)) return FALSE;
697
698         if (pr->window_limit)
699                 {
700                 gint sw = gdk_screen_width() * pr->window_limit_size / 100;
701                 gint sh = gdk_screen_height() * pr->window_limit_size / 100;
702
703                 if (w > sw) w = sw;
704                 if (h > sh) h = sh;
705                 }
706
707         widget = GTK_WIDGET(pr);
708         parent = GTK_WIDGET(pr->parent_window);
709
710         gtk_widget_get_allocation(widget, &widget_allocation);
711         gtk_widget_get_allocation(parent, &parent_allocation);
712
713         w += (parent_allocation.width - widget_allocation.width);
714         h += (parent_allocation.height - widget_allocation.height);
715
716         ww = gdk_window_get_width(gtk_widget_get_window(parent));
717         wh = gdk_window_get_height(gtk_widget_get_window(parent));
718         if (w == ww && h == wh) return FALSE;
719
720         gdk_window_resize(gtk_widget_get_window(parent), w, h);
721
722         return TRUE;
723 }
724
725 void pixbuf_renderer_set_parent(PixbufRenderer *pr, GtkWindow *window)
726 {
727         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
728         g_return_if_fail(window == nullptr || GTK_IS_WINDOW(window));
729
730         pr->parent_window = GTK_WIDGET(window);
731 }
732
733 #pragma GCC diagnostic push
734 #pragma GCC diagnostic ignored "-Wunused-function"
735 GtkWindow *pixbuf_renderer_get_parent_unused(PixbufRenderer *pr)
736 {
737         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
738
739         return GTK_WINDOW(pr->parent_window);
740 }
741 #pragma GCC diagnostic pop
742
743
744 /*
745  *-------------------------------------------------------------------
746  * overlays
747  *-------------------------------------------------------------------
748  */
749
750
751 gint pixbuf_renderer_overlay_add(PixbufRenderer *pr, GdkPixbuf *pixbuf, gint x, gint y,
752                                  OverlayRendererFlags flags)
753 {
754         /* let's assume both renderers returns the same value */
755         if (pr->renderer2) pr->renderer2->overlay_add(pr->renderer2, pixbuf, x, y, flags);
756         return pr->renderer->overlay_add(pr->renderer, pixbuf, x, y, flags);
757 }
758
759 void pixbuf_renderer_overlay_set(PixbufRenderer *pr, gint id, GdkPixbuf *pixbuf, gint x, gint y)
760 {
761         pr->renderer->overlay_set(pr->renderer, id, pixbuf, x, y);
762         if (pr->renderer2) pr->renderer2->overlay_set(pr->renderer2, id, pixbuf, x, y);
763 }
764
765 gboolean pixbuf_renderer_overlay_get(PixbufRenderer *pr, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
766 {
767         if (pr->renderer2) pr->renderer2->overlay_get(pr->renderer2, id, pixbuf, x, y);
768         return pr->renderer->overlay_get(pr->renderer, id, pixbuf, x, y);
769 }
770
771 void pixbuf_renderer_overlay_remove(PixbufRenderer *pr, gint id)
772 {
773         pr->renderer->overlay_set(pr->renderer, id, nullptr, 0, 0);
774         if (pr->renderer2) pr->renderer2->overlay_set(pr->renderer2, id, nullptr, 0, 0);
775 }
776
777 /*
778  *-------------------------------------------------------------------
779  * scroller overlay
780  *-------------------------------------------------------------------
781  */
782
783
784 static gboolean pr_scroller_update_cb(gpointer data)
785 {
786         auto pr = static_cast<PixbufRenderer *>(data);
787         gint x;
788         gint y;
789         gint xinc;
790         gint yinc;
791
792         /* this was a simple scroll by difference between scroller and mouse position,
793          * but all this math results in a smoother result and accounts for a dead zone.
794          */
795
796         if (abs(pr->scroller_xpos - pr->scroller_x) < PR_SCROLLER_DEAD_ZONE)
797                 {
798                 x = 0;
799                 }
800         else
801                 {
802                 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
803                 x = (pr->scroller_xpos - pr->scroller_x) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
804                 x += (x > 0) ? -shift : shift;
805                 }
806
807         if (abs(pr->scroller_ypos - pr->scroller_y) < PR_SCROLLER_DEAD_ZONE)
808                 {
809                 y = 0;
810                 }
811         else
812                 {
813                 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
814                 y = (pr->scroller_ypos - pr->scroller_y) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
815                 y += (y > 0) ? -shift : shift;
816                 }
817
818         if (abs(x) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
819                 {
820                 xinc = x;
821                 }
822         else
823                 {
824                 xinc = pr->scroller_xinc;
825
826                 if (x >= 0)
827                         {
828                         if (xinc < 0) xinc = 0;
829                         if (x < xinc) xinc = x;
830                         if (x > xinc) xinc = MIN(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
831                         }
832                 else
833                         {
834                         if (xinc > 0) xinc = 0;
835                         if (x > xinc) xinc = x;
836                         if (x < xinc) xinc = MAX(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
837                         }
838                 }
839
840         if (abs(y) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
841                 {
842                 yinc = y;
843                 }
844         else
845                 {
846                 yinc = pr->scroller_yinc;
847
848                 if (y >= 0)
849                         {
850                         if (yinc < 0) yinc = 0;
851                         if (y < yinc) yinc = y;
852                         if (y > yinc) yinc = MIN(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
853                         }
854                 else
855                         {
856                         if (yinc > 0) yinc = 0;
857                         if (y > yinc) yinc = y;
858                         if (y < yinc) yinc = MAX(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
859                         }
860                 }
861
862         pr->scroller_xinc = xinc;
863         pr->scroller_yinc = yinc;
864
865         xinc = xinc / PR_SCROLLER_UPDATES_PER_SEC;
866         yinc = yinc / PR_SCROLLER_UPDATES_PER_SEC;
867
868         pixbuf_renderer_scroll(pr, xinc, yinc);
869
870         return TRUE;
871 }
872
873 static void pr_scroller_timer_set(PixbufRenderer *pr, gboolean start)
874 {
875         if (pr->scroller_id)
876                 {
877                 g_source_remove(pr->scroller_id);
878                 pr->scroller_id = 0;
879                 }
880
881         if (start)
882                 {
883                 pr->scroller_id = g_timeout_add(1000 / PR_SCROLLER_UPDATES_PER_SEC,
884                                                 pr_scroller_update_cb, pr);
885                 }
886 }
887
888 static void pr_scroller_start(PixbufRenderer *pr, gint x, gint y)
889 {
890         if (pr->scroller_overlay == -1)
891                 {
892                 GdkPixbuf *pixbuf;
893                 gint w;
894                 gint h;
895
896 #ifdef GQ_BUILD
897                 pixbuf = gdk_pixbuf_new_from_resource(GQ_RESOURCE_PATH_ICONS "/" PIXBUF_INLINE_SCROLLER ".png", nullptr);
898 #else
899                 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 32, 32);
900                 gdk_pixbuf_fill(pixbuf, 0x000000ff);
901 #endif
902                 w = gdk_pixbuf_get_width(pixbuf);
903                 h = gdk_pixbuf_get_height(pixbuf);
904
905                 pr->scroller_overlay = pixbuf_renderer_overlay_add(pr, pixbuf, x - w / 2, y - h / 2, OVL_NORMAL);
906                 g_object_unref(pixbuf);
907                 }
908
909         pr->scroller_x = x;
910         pr->scroller_y = y;
911         pr->scroller_xpos = x;
912         pr->scroller_ypos = y;
913
914         pr_scroller_timer_set(pr, TRUE);
915 }
916
917 static void pr_scroller_stop(PixbufRenderer *pr)
918 {
919         if (!pr->scroller_id) return;
920
921         pixbuf_renderer_overlay_remove(pr, pr->scroller_overlay);
922         pr->scroller_overlay = -1;
923
924         pr_scroller_timer_set(pr, FALSE);
925 }
926
927 /*
928  *-------------------------------------------------------------------
929  * borders
930  *-------------------------------------------------------------------
931  */
932
933 void pixbuf_renderer_set_color(PixbufRenderer *pr, GdkRGBA *color)
934 {
935         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
936
937         if (color)
938                 {
939                 pr->color.red = color->red;
940                 pr->color.green = color->green;
941                 pr->color.blue = color->blue;
942                 }
943         else
944                 {
945                 pr->color.red = 0;
946                 pr->color.green = 0;
947                 pr->color.blue = 0;
948                 }
949
950         pr->renderer->update_viewport(pr->renderer);
951         if (pr->renderer2) pr->renderer2->update_viewport(pr->renderer2);
952 }
953
954 /*
955  *-------------------------------------------------------------------
956  * source tiles
957  *-------------------------------------------------------------------
958  */
959
960 static void pr_source_tile_free(SourceTile *st)
961 {
962         if (!st) return;
963
964         if (st->pixbuf) g_object_unref(st->pixbuf);
965         g_free(st);
966 }
967
968 static void pr_source_tile_free_all(PixbufRenderer *pr)
969 {
970         g_list_free_full(pr->source_tiles, reinterpret_cast<GDestroyNotify>(pr_source_tile_free));
971         pr->source_tiles = nullptr;
972 }
973
974 static void pr_source_tile_unset(PixbufRenderer *pr)
975 {
976         pr_source_tile_free_all(pr);
977         pr->source_tiles_enabled = FALSE;
978 }
979
980 static gboolean pr_source_tile_visible(PixbufRenderer *pr, SourceTile *st)
981 {
982         gint x1;
983         gint y1;
984         gint x2;
985         gint y2;
986
987         if (!st) return FALSE;
988
989         x1 = pr->x_scroll;
990         y1 = pr->y_scroll;
991         x2 = pr->x_scroll + pr->vis_width;
992         y2 = pr->y_scroll + pr->vis_height;
993
994         return static_cast<gdouble>(st->x) * pr->scale <= static_cast<gdouble>(x2) &&
995                  static_cast<gdouble>(st->x + pr->source_tile_width) * pr->scale >= static_cast<gdouble>(x1) &&
996                  static_cast<gdouble>(st->y) * pr->scale <= static_cast<gdouble>(y2) &&
997                  static_cast<gdouble>(st->y + pr->source_tile_height) * pr->scale >= static_cast<gdouble>(y1);
998 }
999
1000 static SourceTile *pr_source_tile_new(PixbufRenderer *pr, gint x, gint y)
1001 {
1002         SourceTile *st = nullptr;
1003         gint count;
1004
1005         g_return_val_if_fail(pr->source_tile_width >= 1 && pr->source_tile_height >= 1, NULL);
1006
1007         if (pr->source_tiles_cache_size < 4) pr->source_tiles_cache_size = 4;
1008
1009         count = g_list_length(pr->source_tiles);
1010         if (count >= pr->source_tiles_cache_size)
1011                 {
1012                 GList *work;
1013
1014                 work = g_list_last(pr->source_tiles);
1015                 while (work && count >= pr->source_tiles_cache_size)
1016                         {
1017                         SourceTile *needle;
1018
1019                         needle = static_cast<SourceTile *>(work->data);
1020                         work = work->prev;
1021
1022                         if (!pr_source_tile_visible(pr, needle))
1023                                 {
1024                                 pr->source_tiles = g_list_remove(pr->source_tiles, needle);
1025
1026                                 if (pr->func_tile_dispose)
1027                                         {
1028                                         pr->func_tile_dispose(pr, needle->x, needle->y,
1029                                                               pr->source_tile_width, pr->source_tile_height,
1030                                                               needle->pixbuf, pr->func_tile_data);
1031                                         }
1032
1033                                 if (!st)
1034                                         {
1035                                         st = needle;
1036                                         }
1037                                 else
1038                                         {
1039                                         pr_source_tile_free(needle);
1040                                         }
1041
1042                                 count--;
1043                                 }
1044                         }
1045                 }
1046
1047         if (!st)
1048                 {
1049                 st = g_new0(SourceTile, 1);
1050                 st->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
1051                                             pr->source_tile_width, pr->source_tile_height);
1052                 }
1053
1054         st->x = ROUND_DOWN(x, pr->source_tile_width);
1055         st->y = ROUND_DOWN(y, pr->source_tile_height);
1056         st->blank = TRUE;
1057
1058         pr->source_tiles = g_list_prepend(pr->source_tiles, st);
1059
1060         return st;
1061 }
1062
1063 static SourceTile *pr_source_tile_request(PixbufRenderer *pr, gint x, gint y)
1064 {
1065         SourceTile *st;
1066
1067         st = pr_source_tile_new(pr, x, y);
1068         if (!st) return nullptr;
1069
1070         if (pr->func_tile_request &&
1071             pr->func_tile_request(pr, st->x, st->y,
1072                                    pr->source_tile_width, pr->source_tile_height, st->pixbuf, pr->func_tile_data))
1073                 {
1074                 st->blank = FALSE;
1075                 }
1076
1077         pr->renderer->invalidate_region(pr->renderer, st->x * pr->scale, st->y * pr->scale,
1078                                   pr->source_tile_width * pr->scale, pr->source_tile_height * pr->scale);
1079         if (pr->renderer2) pr->renderer2->invalidate_region(pr->renderer2, st->x * pr->scale, st->y * pr->scale,
1080                                   pr->source_tile_width * pr->scale, pr->source_tile_height * pr->scale);
1081         return st;
1082 }
1083
1084 static SourceTile *pr_source_tile_find(PixbufRenderer *pr, gint x, gint y)
1085 {
1086         GList *work;
1087
1088         work = pr->source_tiles;
1089         while (work)
1090                 {
1091                 auto st = static_cast<SourceTile *>(work->data);
1092
1093                 if (x >= st->x && x < st->x + pr->source_tile_width &&
1094                     y >= st->y && y < st->y + pr->source_tile_height)
1095                         {
1096                         if (work != pr->source_tiles)
1097                                 {
1098                                 pr->source_tiles = g_list_remove_link(pr->source_tiles, work);
1099                                 pr->source_tiles = g_list_concat(work, pr->source_tiles);
1100                                 }
1101                         return st;
1102                         }
1103
1104                 work = work->next;
1105                 }
1106
1107         return nullptr;
1108 }
1109
1110 GList *pr_source_tile_compute_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h, gboolean request)
1111 {
1112         gint x1;
1113         gint y1;
1114         GList *list = nullptr;
1115         gint sx;
1116         gint sy;
1117
1118         if (x < 0) x = 0;
1119         if (y < 0) y = 0;
1120         if (w > pr->image_width) w = pr->image_width;
1121         if (h > pr->image_height) h = pr->image_height;
1122
1123         sx = ROUND_DOWN(x, pr->source_tile_width);
1124         sy = ROUND_DOWN(y, pr->source_tile_height);
1125
1126         for (x1 = sx; x1 < x + w; x1+= pr->source_tile_width)
1127                 {
1128                 for (y1 = sy; y1 < y + h; y1 += pr->source_tile_height)
1129                         {
1130                         SourceTile *st;
1131
1132                         st = pr_source_tile_find(pr, x1, y1);
1133                         if (!st && request) st = pr_source_tile_request(pr, x1, y1);
1134
1135                         if (st) list = g_list_prepend(list, st);
1136                         }
1137                 }
1138
1139         return g_list_reverse(list);
1140 }
1141
1142 static void pr_source_tile_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height)
1143 {
1144         GList *work;
1145
1146         if (width < 1 || height < 1) return;
1147
1148         work = pr->source_tiles;
1149         while (work)
1150                 {
1151                 SourceTile *st;
1152                 gint rx;
1153                 gint ry;
1154                 gint rw;
1155                 gint rh;
1156
1157                 st = static_cast<SourceTile *>(work->data);
1158                 work = work->next;
1159
1160                 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1161                                    x, y, width, height,
1162                                    &rx, &ry, &rw, &rh))
1163                         {
1164                         GdkPixbuf *pixbuf;
1165
1166                         pixbuf = gdk_pixbuf_new_subpixbuf(st->pixbuf, rx - st->x, ry - st->y, rw, rh);
1167                         if (pr->func_tile_request &&
1168                             pr->func_tile_request(pr, rx, ry, rw, rh, pixbuf, pr->func_tile_data))
1169                                 {
1170                                         pr->renderer->invalidate_region(pr->renderer, rx * pr->scale, ry * pr->scale,
1171                                                               rw * pr->scale, rh * pr->scale);
1172                                         if (pr->renderer2) pr->renderer2->invalidate_region(pr->renderer2, rx * pr->scale, ry * pr->scale,
1173                                                                 rw * pr->scale, rh * pr->scale);
1174                                 }
1175                         g_object_unref(pixbuf);
1176                         }
1177                 }
1178 }
1179
1180 void pixbuf_renderer_set_tiles(PixbufRenderer *pr, gint width, gint height,
1181                                gint tile_width, gint tile_height, gint cache_size,
1182                                PixbufRendererTileRequestFunc func_request,
1183                                PixbufRendererTileDisposeFunc func_dispose,
1184                                gpointer user_data,
1185                                gdouble zoom)
1186 {
1187         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1188         g_return_if_fail(tile_width >= 32 && tile_height >= 32);
1189         g_return_if_fail(width >= 32 && height > 32);
1190         g_return_if_fail(func_request != nullptr);
1191
1192         if (pr->pixbuf) g_object_unref(pr->pixbuf);
1193         pr->pixbuf = nullptr;
1194
1195         pr_source_tile_unset(pr);
1196
1197         if (cache_size < 4) cache_size = 4;
1198
1199         pr->source_tiles_enabled = TRUE;
1200         pr->source_tiles_cache_size = cache_size;
1201         pr->source_tile_width = tile_width;
1202         pr->source_tile_height = tile_height;
1203
1204         pr->image_width = width;
1205         pr->image_height = height;
1206
1207         pr->func_tile_request = func_request;
1208         pr->func_tile_dispose = func_dispose;
1209         pr->func_tile_data = user_data;
1210
1211         pr_stereo_temp_disable(pr, TRUE);
1212         pr_zoom_sync(pr, zoom, static_cast<PrZoomFlags>(PR_ZOOM_FORCE | PR_ZOOM_NEW), 0, 0);
1213 }
1214
1215 void pixbuf_renderer_set_tiles_size(PixbufRenderer *pr, gint width, gint height)
1216 {
1217         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1218         g_return_if_fail(width >= 32 && height > 32);
1219
1220         if (!pr->source_tiles_enabled) return;
1221         if (pr->image_width == width && pr->image_height == height) return;
1222
1223         pr->image_width = width;
1224         pr->image_height = height;
1225
1226         pr_zoom_sync(pr, pr->zoom, PR_ZOOM_FORCE, 0, 0);
1227 }
1228
1229 gint pixbuf_renderer_get_tiles(PixbufRenderer *pr)
1230 {
1231         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
1232
1233         return pr->source_tiles_enabled;
1234 }
1235
1236 static void pr_zoom_adjust_real(PixbufRenderer *pr, gdouble increment,
1237                                 PrZoomFlags flags, gint x, gint y)
1238 {
1239         gdouble zoom = pr->zoom;
1240
1241         if (increment == 0.0) return;
1242
1243         if (zoom == 0.0)
1244                 {
1245                 if (pr->scale < 1.0)
1246                         {
1247                         zoom = 0.0 - 1.0 / pr->scale;
1248                         }
1249                 else
1250                         {
1251                         zoom = pr->scale;
1252                         }
1253                 }
1254
1255         if (options->image.zoom_style == ZOOM_GEOMETRIC)
1256                 {
1257                 if (increment < 0.0)
1258                         {
1259                         if (zoom >= 1.0)
1260                                 {
1261                                 if (zoom / -(increment - 1.0) < 1.0)
1262                                         {
1263                                         zoom = 1.0 / (zoom / (increment - 1.0));
1264                                         }
1265                                 else
1266                                         {
1267                                         zoom = zoom / -(increment - 1.0) ;
1268                                         }
1269                                 }
1270                         else
1271                                 {
1272                                 zoom = zoom * -(increment - 1.0);
1273                                 }
1274                         }
1275                 else
1276                         {
1277                         if (zoom <= -1.0 )
1278                                 {
1279                                 if (zoom / (increment + 1.0) > -1.0)
1280                                         {
1281                                         zoom = -(1.0 / (zoom / (increment + 1.0)));
1282                                         }
1283                                 else
1284                                         {
1285                                         zoom = zoom / (increment + 1.0) ;
1286                                         }
1287                                 }
1288                         else
1289                                 {
1290                                 zoom = zoom * (increment + 1.0);
1291                                 }
1292                         }
1293                 }
1294         else
1295                 {
1296                 if (increment < 0.0)
1297                         {
1298                         if (zoom >= 1.0 && zoom + increment < 1.0)
1299                                 {
1300                                 zoom = zoom + increment - 2.0;
1301                                 }
1302                         else
1303                                 {
1304                                 zoom = zoom + increment;
1305                                 }
1306                         }
1307                 else
1308                         {
1309                         if (zoom <= -1.0 && zoom + increment > -1.0)
1310                                 {
1311                                 zoom = zoom + increment + 2.0;
1312                                 }
1313                         else
1314                                 {
1315                                 zoom = zoom + increment;
1316                                 }
1317                         }
1318                 }
1319
1320         pr_zoom_sync(pr, zoom, flags, x, y);
1321 }
1322
1323
1324 /*
1325  *-------------------------------------------------------------------
1326  * signal emission
1327  *-------------------------------------------------------------------
1328  */
1329
1330 static void pr_update_signal(PixbufRenderer *pr)
1331 {
1332         DEBUG_1("%s pixbuf renderer updated - started drawing %p, img: %dx%d", get_exec_time(), (void *)pr, pr->image_width, pr->image_height);
1333         pr->debug_updated = TRUE;
1334 }
1335
1336 static void pr_zoom_signal(PixbufRenderer *pr)
1337 {
1338         g_signal_emit(pr, signals[SIGNAL_ZOOM], 0, pr->zoom);
1339 }
1340
1341 static void pr_clicked_signal(PixbufRenderer *pr, GdkEventButton *bevent)
1342 {
1343         g_signal_emit(pr, signals[SIGNAL_CLICKED], 0, bevent);
1344 }
1345
1346 static void pr_scroll_notify_signal(PixbufRenderer *pr)
1347 {
1348         g_signal_emit(pr, signals[SIGNAL_SCROLL_NOTIFY], 0);
1349 }
1350
1351 void pr_render_complete_signal(PixbufRenderer *pr)
1352 {
1353         if (!pr->complete)
1354                 {
1355                 g_signal_emit(pr, signals[SIGNAL_RENDER_COMPLETE], 0);
1356                 g_object_set(G_OBJECT(pr), "complete", TRUE, NULL);
1357                 }
1358         if (pr->debug_updated)
1359                 {
1360                 DEBUG_1("%s pixbuf renderer done %p", get_exec_time(), (void *)pr);
1361                 pr->debug_updated = FALSE;
1362                 }
1363 }
1364
1365 static void pr_drag_signal(PixbufRenderer *pr, GdkEventMotion *event)
1366 {
1367         g_signal_emit(pr, signals[SIGNAL_DRAG], 0, event);
1368 }
1369
1370 static void pr_update_pixel_signal(PixbufRenderer *pr)
1371 {
1372         g_signal_emit(pr, signals[SIGNAL_UPDATE_PIXEL], 0);
1373 }
1374
1375 /*
1376  *-------------------------------------------------------------------
1377  * sync and clamp
1378  *-------------------------------------------------------------------
1379  */
1380
1381
1382 void pr_tile_coords_map_orientation(gint orientation,
1383                                      gdouble tile_x, gdouble tile_y, /* coordinates of the tile */
1384                                      gdouble image_w, gdouble image_h,
1385                                      gdouble tile_w, gdouble tile_h,
1386                                      gdouble *res_x, gdouble *res_y)
1387 {
1388         *res_x = tile_x;
1389         *res_y = tile_y;
1390         switch (orientation)
1391                 {
1392                 case EXIF_ORIENTATION_TOP_LEFT:
1393                         /* normal -- nothing to do */
1394                         break;
1395                 case EXIF_ORIENTATION_TOP_RIGHT:
1396                         /* mirrored */
1397                         *res_x = image_w - tile_x - tile_w;
1398                         break;
1399                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1400                         /* upside down */
1401                         *res_x = image_w - tile_x - tile_w;
1402                         *res_y = image_h - tile_y - tile_h;
1403                         break;
1404                 case EXIF_ORIENTATION_BOTTOM_LEFT:
1405                         /* flipped */
1406                         *res_y = image_h - tile_y - tile_h;
1407                         break;
1408                 case EXIF_ORIENTATION_LEFT_TOP:
1409                         *res_x = tile_y;
1410                         *res_y = tile_x;
1411                         break;
1412                 case EXIF_ORIENTATION_RIGHT_TOP:
1413                         /* rotated -90 (270) */
1414                         *res_x = tile_y;
1415                         *res_y = image_w - tile_x - tile_w;
1416                         break;
1417                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1418                         *res_x = image_h - tile_y - tile_h;
1419                         *res_y = image_w - tile_x - tile_w;
1420                         break;
1421                 case EXIF_ORIENTATION_LEFT_BOTTOM:
1422                         /* rotated 90 */
1423                         *res_x = image_h - tile_y - tile_h;
1424                         *res_y = tile_x;
1425                         break;
1426                 default:
1427                         /* The other values are out of range */
1428                         break;
1429                 }
1430 }
1431
1432 void pr_tile_region_map_orientation(gint orientation,
1433                                      gint area_x, gint area_y, /* coordinates of the area inside tile */
1434                                      gint tile_w, gint tile_h,
1435                                      gint area_w, gint area_h,
1436                                      gint *res_x, gint *res_y,
1437                                      gint *res_w, gint *res_h)
1438 {
1439         *res_x = area_x;
1440         *res_y = area_y;
1441         *res_w = area_w;
1442         *res_h = area_h;
1443
1444         switch (orientation)
1445                 {
1446                 case EXIF_ORIENTATION_TOP_LEFT:
1447                         /* normal -- nothing to do */
1448                         break;
1449                 case EXIF_ORIENTATION_TOP_RIGHT:
1450                         /* mirrored */
1451                         *res_x = tile_w - area_x - area_w;
1452                         break;
1453                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1454                         /* upside down */
1455                         *res_x = tile_w - area_x - area_w;
1456                         *res_y = tile_h - area_y - area_h;
1457                         break;
1458                 case EXIF_ORIENTATION_BOTTOM_LEFT:
1459                         /* flipped */
1460                         *res_y = tile_h - area_y - area_h;
1461                         break;
1462                 case EXIF_ORIENTATION_LEFT_TOP:
1463                         *res_x = area_y;
1464                         *res_y = area_x;
1465                         *res_w = area_h;
1466                         *res_h = area_w;
1467                         break;
1468                 case EXIF_ORIENTATION_RIGHT_TOP:
1469                         /* rotated -90 (270) */
1470                         *res_x = area_y;
1471                         *res_y = tile_w - area_x - area_w;
1472                         *res_w = area_h;
1473                         *res_h = area_w;
1474                         break;
1475                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1476                         *res_x = tile_h - area_y - area_h;
1477                         *res_y = tile_w - area_x - area_w;
1478                         *res_w = area_h;
1479                         *res_h = area_w;
1480                         break;
1481                 case EXIF_ORIENTATION_LEFT_BOTTOM:
1482                         /* rotated 90 */
1483                         *res_x = tile_h - area_y - area_h;
1484                         *res_y = area_x;
1485                         *res_w = area_h;
1486                         *res_h = area_w;
1487                         break;
1488                 default:
1489                         /* The other values are out of range */
1490                         break;
1491                 }
1492 }
1493
1494 void pr_coords_map_orientation_reverse(gint orientation,
1495                                      gint area_x, gint area_y,
1496                                      gint tile_w, gint tile_h,
1497                                      gint area_w, gint area_h,
1498                                      gint *res_x, gint *res_y,
1499                                      gint *res_w, gint *res_h)
1500 {
1501         *res_x = area_x;
1502         *res_y = area_y;
1503         *res_w = area_w;
1504         *res_h = area_h;
1505
1506         switch (orientation)
1507                 {
1508                 case EXIF_ORIENTATION_TOP_LEFT:
1509                         /* normal -- nothing to do */
1510                         break;
1511                 case EXIF_ORIENTATION_TOP_RIGHT:
1512                         /* mirrored */
1513                         *res_x = tile_w - area_x - area_w;
1514                         break;
1515                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1516                         /* upside down */
1517                         *res_x = tile_w - area_x - area_w;
1518                         *res_y = tile_h - area_y - area_h;
1519                         break;
1520                 case EXIF_ORIENTATION_BOTTOM_LEFT:
1521                         /* flipped */
1522                         *res_y = tile_h - area_y - area_h;
1523                         break;
1524                 case EXIF_ORIENTATION_LEFT_TOP:
1525                         *res_x = area_y;
1526                         *res_y = area_x;
1527                         *res_w = area_h;
1528                         *res_h = area_w;
1529                         break;
1530                 case EXIF_ORIENTATION_RIGHT_TOP:
1531                         /* rotated -90 (270) */
1532                         *res_x = tile_w - area_y - area_h;
1533                         *res_y = area_x;
1534                         *res_w = area_h;
1535                         *res_h = area_w;
1536                         break;
1537                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1538                         *res_x = tile_w - area_y - area_h;
1539                         *res_y = tile_h - area_x - area_w;
1540                         *res_w = area_h;
1541                         *res_h = area_w;
1542                         break;
1543                 case EXIF_ORIENTATION_LEFT_BOTTOM:
1544                         /* rotated 90 */
1545                         *res_x = area_y;
1546                         *res_y = tile_h - area_x - area_w;
1547                         *res_w = area_h;
1548                         *res_h = area_w;
1549                         break;
1550                 default:
1551                         /* The other values are out of range */
1552                         break;
1553                 }
1554 }
1555
1556
1557
1558 static void pixbuf_renderer_sync_scroll_center(PixbufRenderer *pr)
1559 {
1560         gint src_x;
1561         gint src_y;
1562         if (!pr->width || !pr->height) return;
1563
1564         /*
1565          * Update norm_center only if the image is bigger than the window.
1566          * With this condition the stored center survives also a temporary display
1567          * of the "broken image" icon.
1568         */
1569
1570         if (pr->width > pr->viewport_width)
1571                 {
1572                 src_x = pr->x_scroll + pr->vis_width / 2;
1573                 pr->norm_center_x = static_cast<gdouble>(src_x) / pr->width;
1574                 }
1575
1576         if (pr->height > pr->viewport_height)
1577                 {
1578                 src_y = pr->y_scroll + pr->vis_height / 2;
1579                 pr->norm_center_y = static_cast<gdouble>(src_y) / pr->height;
1580                 }
1581 }
1582
1583
1584 static gboolean pr_scroll_clamp(PixbufRenderer *pr)
1585 {
1586         gint old_xs;
1587         gint old_ys;
1588
1589         if (pr->zoom == 0.0)
1590                 {
1591                 pr->x_scroll = 0;
1592                 pr->y_scroll = 0;
1593
1594                 return FALSE;
1595                 }
1596
1597         old_xs = pr->x_scroll;
1598         old_ys = pr->y_scroll;
1599
1600         if (pr->x_offset > 0)
1601                 {
1602                 pr->x_scroll = 0;
1603                 }
1604         else
1605                 {
1606                 pr->x_scroll = CLAMP(pr->x_scroll, 0, pr->width - pr->vis_width);
1607                 }
1608
1609         if (pr->y_offset > 0)
1610                 {
1611                 pr->y_scroll = 0;
1612                 }
1613         else
1614                 {
1615                 pr->y_scroll = CLAMP(pr->y_scroll, 0, pr->height - pr->vis_height);
1616                 }
1617
1618         pixbuf_renderer_sync_scroll_center(pr);
1619
1620         return (old_xs != pr->x_scroll || old_ys != pr->y_scroll);
1621 }
1622
1623 static gboolean pr_size_clamp(PixbufRenderer *pr)
1624 {
1625         gint old_vw;
1626         gint old_vh;
1627
1628         old_vw = pr->vis_width;
1629         old_vh = pr->vis_height;
1630
1631         if (pr->width < pr->viewport_width)
1632                 {
1633                 pr->vis_width = pr->width;
1634                 pr->x_offset = (pr->viewport_width - pr->width) / 2;
1635                 }
1636         else
1637                 {
1638                 pr->vis_width = pr->viewport_width;
1639                 pr->x_offset = 0;
1640                 }
1641
1642         if (pr->height < pr->viewport_height)
1643                 {
1644                 pr->vis_height = pr->height;
1645                 pr->y_offset = (pr->viewport_height - pr->height) / 2;
1646                 }
1647         else
1648                 {
1649                 pr->vis_height = pr->viewport_height;
1650                 pr->y_offset = 0;
1651                 }
1652
1653         pixbuf_renderer_sync_scroll_center(pr);
1654
1655         return (old_vw != pr->vis_width || old_vh != pr->vis_height);
1656 }
1657
1658 static gboolean pr_zoom_clamp(PixbufRenderer *pr, gdouble zoom,
1659                               PrZoomFlags flags)
1660 {
1661         gint w;
1662         gint h;
1663         gdouble scale;
1664         gboolean force = !!(flags & PR_ZOOM_FORCE);
1665         gboolean new_z = !!(flags & PR_ZOOM_NEW);
1666
1667         zoom = CLAMP(zoom, pr->zoom_min, pr->zoom_max);
1668
1669         if (pr->zoom == zoom && !force) return FALSE;
1670
1671         w = pr->image_width;
1672         h = pr->image_height;
1673
1674         if (zoom == 0.0 && !pr->pixbuf)
1675                 {
1676                 scale = 1.0;
1677                 }
1678         else if (zoom == 0.0)
1679                 {
1680                 gint max_w;
1681                 gint max_h;
1682                 gboolean sizeable;
1683
1684                 sizeable = (new_z && pr_parent_window_sizable(pr));
1685
1686                 if (sizeable)
1687                         {
1688                         max_w = gdk_screen_width();
1689                         max_h = gdk_screen_height();
1690
1691                         if (pr->window_limit)
1692                                 {
1693                                 max_w = max_w * pr->window_limit_size / 100;
1694                                 max_h = max_h * pr->window_limit_size / 100;
1695                                 }
1696                         }
1697                 else
1698                         {
1699                         max_w = pr->viewport_width;
1700                         max_h = pr->viewport_height;
1701                         }
1702
1703                 if ((pr->zoom_expand && !sizeable) || w > max_w || h > max_h)
1704                         {
1705                         if (static_cast<gdouble>(max_w) / w > static_cast<gdouble>(max_h) / h / pr->aspect_ratio)
1706                                 {
1707                                 scale = static_cast<gdouble>(max_h) / h / pr->aspect_ratio;
1708                                 h = max_h;
1709                                 w = w * scale + 0.5;
1710                                 if (w > max_w) w = max_w;
1711                                 }
1712                         else
1713                                 {
1714                                 scale = static_cast<gdouble>(max_w) / w;
1715                                 w = max_w;
1716                                 h = h * scale * pr->aspect_ratio + 0.5;
1717                                 if (h > max_h) h = max_h;
1718                                 }
1719
1720                         if (pr->autofit_limit)
1721                                 {
1722                                 gdouble factor = static_cast<gdouble>(pr->autofit_limit_size) / 100;
1723                                 w = w * factor + 0.5;
1724                                 h = h * factor + 0.5;
1725                                 scale = scale * factor;
1726                                 }
1727
1728                         if (pr->zoom_expand)
1729                                 {
1730                                 gdouble factor = static_cast<gdouble>(pr->enlargement_limit_size) / 100;
1731                                 if (scale > factor)
1732                                         {
1733                                         w = w * factor / scale;
1734                                         h = h * factor / scale;
1735                                         scale = factor;
1736                                         }
1737                                 }
1738
1739                         if (w < 1) w = 1;
1740                         if (h < 1) h = 1;
1741                         }
1742                 else
1743                         {
1744                         scale = 1.0;
1745                         }
1746                 }
1747         else if (zoom > 0.0) /* zoom orig, in */
1748                 {
1749                 scale = zoom;
1750                 w = w * scale;
1751                 h = h * scale * pr->aspect_ratio;
1752                 }
1753         else /* zoom out */
1754                 {
1755                 scale = 1.0 / (0.0 - zoom);
1756                 w = w * scale;
1757                 h = h * scale * pr->aspect_ratio;
1758                 }
1759
1760         pr->zoom = zoom;
1761         pr->width = w;
1762         pr->height = h;
1763         pr->scale = scale;
1764
1765         return TRUE;
1766 }
1767
1768 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
1769                          PrZoomFlags flags, gint px, gint py)
1770 {
1771         gdouble old_scale;
1772         gint old_cx;
1773         gint old_cy;
1774         gboolean center_point = !!(flags & PR_ZOOM_CENTER);
1775         gboolean force = !!(flags & PR_ZOOM_FORCE);
1776         gboolean new_z = !!(flags & PR_ZOOM_NEW);
1777         gboolean lazy = !!(flags & PR_ZOOM_LAZY);
1778         PrZoomFlags clamp_flags = flags;
1779         gdouble old_center_x = pr->norm_center_x;
1780         gdouble old_center_y = pr->norm_center_y;
1781
1782         old_scale = pr->scale;
1783         if (center_point)
1784                 {
1785                 px = CLAMP(px, 0, pr->width);
1786                 py = CLAMP(py, 0, pr->height);
1787                 old_cx = pr->x_scroll + (px - pr->x_offset);
1788                 old_cy = pr->y_scroll + (py - pr->y_offset);
1789                 }
1790         else
1791                 {
1792                 px = py = 0;
1793                 old_cx = pr->x_scroll + pr->vis_width / 2;
1794                 old_cy = pr->y_scroll + pr->vis_height / 2;
1795                 }
1796
1797         if (force) clamp_flags = static_cast<PrZoomFlags>(clamp_flags | PR_ZOOM_INVALIDATE);
1798         if (!pr_zoom_clamp(pr, zoom, clamp_flags)) return;
1799
1800         (void) pr_size_clamp(pr);
1801         (void) pr_parent_window_resize(pr, pr->width, pr->height);
1802
1803         if (force && new_z)
1804                 {
1805                 switch (pr->scroll_reset)
1806                         {
1807                         case PR_SCROLL_RESET_NOCHANGE:
1808                                 /* maintain old scroll position */
1809                                 pr->x_scroll = (static_cast<gdouble>(pr->image_width) * old_center_x * pr->scale) - pr->vis_width / 2.0;
1810                                 pr->y_scroll = (static_cast<gdouble>(pr->image_height) * old_center_y * pr->scale * pr->aspect_ratio) - pr->vis_height / 2.0;
1811                                 break;
1812                         case PR_SCROLL_RESET_CENTER:
1813                                 /* center new image */
1814                                 pr->x_scroll = (static_cast<gdouble>(pr->image_width) / 2.0 * pr->scale) - pr->vis_width / 2.0;
1815                                 pr->y_scroll = (static_cast<gdouble>(pr->image_height) / 2.0 * pr->scale * pr->aspect_ratio) - pr->vis_height / 2.0;
1816                                 break;
1817                         case PR_SCROLL_RESET_TOPLEFT:
1818                         default:
1819                                 /* reset to upper left */
1820                                 pr->x_scroll = 0;
1821                                 pr->y_scroll = 0;
1822                                 break;
1823                         }
1824                 }
1825         else
1826                 {
1827                 /* user zoom does not force, so keep visible center point */
1828                 if (center_point)
1829                         {
1830                         pr->x_scroll = old_cx / old_scale * pr->scale - (px - pr->x_offset);
1831                         pr->y_scroll = old_cy / old_scale * pr->scale * pr->aspect_ratio - (py - pr->y_offset);
1832                         }
1833                 else
1834                         {
1835                         pr->x_scroll = old_cx / old_scale * pr->scale - (pr->vis_width / 2.0);
1836                         pr->y_scroll = old_cy / old_scale * pr->scale * pr->aspect_ratio - (pr->vis_height / 2.0);
1837                         }
1838                 }
1839
1840         pr_scroll_clamp(pr);
1841
1842         pr->renderer->update_zoom(pr->renderer, lazy);
1843         if (pr->renderer2) pr->renderer2->update_zoom(pr->renderer2, lazy);
1844
1845         pr_scroll_notify_signal(pr);
1846         pr_zoom_signal(pr);
1847         pr_update_signal(pr);
1848 }
1849
1850 static void pr_size_sync(PixbufRenderer *pr, gint new_width, gint new_height)
1851 {
1852         gboolean zoom_changed = FALSE;
1853
1854         gint new_viewport_width = new_width;
1855         gint new_viewport_height = new_height;
1856
1857         if (!pr->stereo_temp_disable)
1858                 {
1859                 if (pr->stereo_mode & PR_STEREO_HORIZ)
1860                         {
1861                         new_viewport_width = new_width / 2;
1862                         }
1863                 else if (pr->stereo_mode & PR_STEREO_VERT)
1864                         {
1865                         new_viewport_height = new_height / 2;
1866                         }
1867                 else if (pr->stereo_mode & PR_STEREO_FIXED)
1868                         {
1869                         new_viewport_width = pr->stereo_fixed_width;
1870                         new_viewport_height = pr->stereo_fixed_height;
1871                         }
1872                 }
1873
1874         if (pr->window_width == new_width && pr->window_height == new_height &&
1875             pr->viewport_width == new_viewport_width && pr->viewport_height == new_viewport_height) return;
1876
1877         pr->window_width = new_width;
1878         pr->window_height = new_height;
1879         pr->viewport_width = new_viewport_width;
1880         pr->viewport_height = new_viewport_height;
1881
1882         if (pr->zoom == 0.0)
1883                 {
1884                 gdouble old_scale = pr->scale;
1885                 pr_zoom_clamp(pr, 0.0, PR_ZOOM_FORCE);
1886                 zoom_changed = (old_scale != pr->scale);
1887                 }
1888
1889         pr_size_clamp(pr);
1890         pr_scroll_clamp(pr);
1891
1892         if (zoom_changed)
1893                 {
1894                 pr->renderer->update_zoom(pr->renderer, FALSE);
1895                 if (pr->renderer2) pr->renderer2->update_zoom(pr->renderer2, FALSE);
1896                 }
1897
1898         pr->renderer->update_viewport(pr->renderer);
1899         if (pr->renderer2) pr->renderer2->update_viewport(pr->renderer2);
1900
1901
1902         /* ensure scroller remains visible */
1903         if (pr->scroller_overlay != -1)
1904                 {
1905                 gboolean update = FALSE;
1906
1907                 if (pr->scroller_x > new_width)
1908                         {
1909                         pr->scroller_x = new_width;
1910                         pr->scroller_xpos = new_width;
1911                         update = TRUE;
1912                         }
1913                 if (pr->scroller_y > new_height)
1914                         {
1915                         pr->scroller_y = new_height;
1916                         pr->scroller_ypos = new_height;
1917                         update = TRUE;
1918                         }
1919
1920                 if (update)
1921                         {
1922                         GdkPixbuf *pixbuf;
1923
1924                         if (pixbuf_renderer_overlay_get(pr, pr->scroller_overlay, &pixbuf, nullptr, nullptr))
1925                                 {
1926                                 gint w;
1927                                 gint h;
1928
1929                                 w = gdk_pixbuf_get_width(pixbuf);
1930                                 h = gdk_pixbuf_get_height(pixbuf);
1931                                 pixbuf_renderer_overlay_set(pr, pr->scroller_overlay, pixbuf,
1932                                                             pr->scroller_x - w / 2, pr->scroller_y - h / 2);
1933                                 }
1934                         }
1935                 }
1936
1937         pr_scroll_notify_signal(pr);
1938         if (zoom_changed) pr_zoom_signal(pr);
1939         pr_update_signal(pr);
1940 }
1941
1942 static void pr_size_cb(GtkWidget *, GtkAllocation *allocation, gpointer data)
1943 {
1944         auto pr = static_cast<PixbufRenderer *>(data);
1945
1946         pr_size_sync(pr, allocation->width, allocation->height);
1947 }
1948
1949 /*
1950  *-------------------------------------------------------------------
1951  * scrolling
1952  *-------------------------------------------------------------------
1953  */
1954
1955 void pixbuf_renderer_scroll(PixbufRenderer *pr, gint x, gint y)
1956 {
1957         gint old_x;
1958         gint old_y;
1959         gint x_off;
1960         gint y_off;
1961
1962         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1963
1964         if (!pr->pixbuf && !pr->source_tiles_enabled) return;
1965
1966         old_x = pr->x_scroll;
1967         old_y = pr->y_scroll;
1968
1969         pr->x_scroll += x;
1970         pr->y_scroll += y;
1971
1972         pr_scroll_clamp(pr);
1973
1974         pixbuf_renderer_sync_scroll_center(pr);
1975
1976         if (pr->x_scroll == old_x && pr->y_scroll == old_y) return;
1977
1978         pr_scroll_notify_signal(pr);
1979
1980         x_off = pr->x_scroll - old_x;
1981         y_off = pr->y_scroll - old_y;
1982
1983         pr->renderer->scroll(pr->renderer, x_off, y_off);
1984         if (pr->renderer2) pr->renderer2->scroll(pr->renderer2, x_off, y_off);
1985 }
1986
1987 void pixbuf_renderer_scroll_to_point(PixbufRenderer *pr, gint x, gint y,
1988                                      gdouble x_align, gdouble y_align)
1989 {
1990         gint px;
1991         gint py;
1992         gint ax;
1993         gint ay;
1994
1995         x_align = CLAMP(x_align, 0.0, 1.0);
1996         y_align = CLAMP(y_align, 0.0, 1.0);
1997
1998         ax = static_cast<gdouble>(pr->vis_width) * x_align;
1999         ay = static_cast<gdouble>(pr->vis_height) * y_align;
2000
2001         px = static_cast<gdouble>(x) * pr->scale - (pr->x_scroll + ax);
2002         py = static_cast<gdouble>(y) * pr->scale * pr->aspect_ratio - (pr->y_scroll + ay);
2003
2004         pixbuf_renderer_scroll(pr, px, py);
2005 }
2006
2007 /* get or set coordinates of viewport center in the image, in range 0.0 - 1.0 */
2008
2009 void pixbuf_renderer_get_scroll_center(PixbufRenderer *pr, gdouble *x, gdouble *y)
2010 {
2011         *x = pr->norm_center_x;
2012         *y = pr->norm_center_y;
2013 }
2014
2015 void pixbuf_renderer_set_scroll_center(PixbufRenderer *pr, gdouble x, gdouble y)
2016 {
2017         gdouble dst_x;
2018         gdouble dst_y;
2019
2020         dst_x = x * pr->width  - pr->vis_width  / 2.0 - pr->x_scroll + CLAMP(pr->subpixel_x_scroll, -1.0, 1.0);
2021         dst_y = y * pr->height - pr->vis_height / 2.0 - pr->y_scroll + CLAMP(pr->subpixel_y_scroll, -1.0, 1.0);
2022
2023         pr->subpixel_x_scroll = dst_x - static_cast<gint>(dst_x);
2024         pr->subpixel_y_scroll = dst_y - static_cast<gint>(dst_y);
2025
2026         pixbuf_renderer_scroll(pr, static_cast<gint>(dst_x), static_cast<gint>(dst_y));
2027 }
2028
2029 /*
2030  *-------------------------------------------------------------------
2031  * mouse
2032  *-------------------------------------------------------------------
2033  */
2034
2035 static gboolean pr_mouse_motion_cb(GtkWidget *widget, GdkEventMotion *event, gpointer)
2036 {
2037         PixbufRenderer *pr;
2038         gint accel;
2039         GdkSeat *seat;
2040         GdkDevice *device;
2041
2042         /* This is a hack, but work far the best, at least for single pointer systems.
2043          * See https://bugzilla.gnome.org/show_bug.cgi?id=587714 for more. */
2044         gint x;
2045         gint y;
2046         seat = gdk_display_get_default_seat(gdk_window_get_display(event->window));
2047         device = gdk_seat_get_pointer(seat);
2048
2049         gdk_window_get_device_position(event->window, device, &x, &y, nullptr);
2050
2051         event->x = x;
2052         event->y = y;
2053
2054         pr = PIXBUF_RENDERER(widget);
2055
2056         if (pr->scroller_id)
2057                 {
2058                 pr->scroller_xpos = event->x;
2059                 pr->scroller_ypos = event->y;
2060                 }
2061
2062         pr->x_mouse = event->x;
2063         pr->y_mouse = event->y;
2064         pr_update_pixel_signal(pr);
2065
2066         if (!pr->in_drag || !gdk_pointer_is_grabbed()) return FALSE;
2067
2068         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
2069                 {
2070                 pr->drag_moved++;
2071                 }
2072         else
2073                 {
2074                 widget_set_cursor(widget, GDK_FLEUR);
2075                 }
2076
2077         if (event->state & GDK_CONTROL_MASK)
2078                 {
2079                 accel = PR_PAN_SHIFT_MULTIPLIER;
2080                 }
2081         else
2082                 {
2083                 accel = 1;
2084                 }
2085
2086         /* do the scroll - not when drawing rectangle*/
2087         if (!options->draw_rectangle)
2088                 {
2089                 pixbuf_renderer_scroll(pr, (pr->drag_last_x - event->x) * accel,
2090                                         (pr->drag_last_y - event->y) * accel);
2091                 }
2092         pr_drag_signal(pr, event);
2093
2094         pr->drag_last_x = event->x;
2095         pr->drag_last_y = event->y;
2096
2097         /* This is recommended by the GTK+ documentation, but does not work properly.
2098          * Use deprecated way until GTK+ gets a solution for correct motion hint handling:
2099          * https://bugzilla.gnome.org/show_bug.cgi?id=587714
2100          */
2101         /* gdk_event_request_motions (event); */
2102         return FALSE;
2103 }
2104
2105 static gboolean pr_leave_notify_cb(GtkWidget *widget, GdkEventCrossing *, gpointer)
2106 {
2107         PixbufRenderer *pr;
2108
2109         pr = PIXBUF_RENDERER(widget);
2110         pr->x_mouse = -1;
2111         pr->y_mouse = -1;
2112
2113         pr_update_pixel_signal(pr);
2114         return FALSE;
2115 }
2116
2117 static gboolean pr_mouse_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer)
2118 {
2119         PixbufRenderer *pr;
2120         GtkWidget *parent;
2121
2122         pr = PIXBUF_RENDERER(widget);
2123
2124         if (pr->scroller_id) return TRUE;
2125
2126         switch (bevent->button)
2127                 {
2128                 case MOUSE_BUTTON_LEFT:
2129                         pr->in_drag = TRUE;
2130                         pr->drag_last_x = bevent->x;
2131                         pr->drag_last_y = bevent->y;
2132                         pr->drag_moved = 0;
2133                         gdk_pointer_grab(gtk_widget_get_window(widget), FALSE,
2134                                          static_cast<GdkEventMask>(GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_RELEASE_MASK),
2135                                          nullptr, nullptr, bevent->time);
2136                         gtk_grab_add(widget);
2137                         break;
2138                 case MOUSE_BUTTON_MIDDLE:
2139                         pr->drag_moved = 0;
2140                         break;
2141                 case MOUSE_BUTTON_RIGHT:
2142                         pr_clicked_signal(pr, bevent);
2143                         break;
2144                 default:
2145                         break;
2146                 }
2147
2148         parent = gtk_widget_get_parent(widget);
2149         if (widget && gtk_widget_get_can_focus(parent))
2150                 {
2151                 gtk_widget_grab_focus(parent);
2152                 }
2153
2154         return FALSE;
2155 }
2156
2157 static gboolean pr_mouse_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer)
2158 {
2159         PixbufRenderer *pr;
2160
2161         pr = PIXBUF_RENDERER(widget);
2162
2163         if (pr->scroller_id)
2164                 {
2165                 pr_scroller_stop(pr);
2166                 return TRUE;
2167                 }
2168
2169         if (gdk_pointer_is_grabbed() && gtk_widget_has_grab(GTK_WIDGET(pr)))
2170                 {
2171                 gtk_grab_remove(widget);
2172                 gdk_pointer_ungrab(bevent->time);
2173                 widget_set_cursor(widget, -1);
2174                 }
2175
2176         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
2177                 {
2178                 if (bevent->button == MOUSE_BUTTON_LEFT && (bevent->state & GDK_CONTROL_MASK))
2179                         {
2180                         pr_scroller_start(pr, bevent->x, bevent->y);
2181                         }
2182                 else if (bevent->button == MOUSE_BUTTON_LEFT || bevent->button == MOUSE_BUTTON_MIDDLE)
2183                         {
2184                         pr_clicked_signal(pr, bevent);
2185                         }
2186                 }
2187
2188         pr->in_drag = FALSE;
2189
2190         return FALSE;
2191 }
2192
2193 static gboolean pr_mouse_leave_cb(GtkWidget *widget, GdkEventCrossing *, gpointer)
2194 {
2195         PixbufRenderer *pr;
2196
2197         pr = PIXBUF_RENDERER(widget);
2198
2199         if (pr->scroller_id)
2200                 {
2201                 pr->scroller_xpos = pr->scroller_x;
2202                 pr->scroller_ypos = pr->scroller_y;
2203                 pr->scroller_xinc = 0;
2204                 pr->scroller_yinc = 0;
2205                 }
2206
2207         return FALSE;
2208 }
2209
2210 static void pr_mouse_drag_cb(GtkWidget *widget, GdkDragContext *, gpointer)
2211 {
2212         PixbufRenderer *pr;
2213
2214         pr = PIXBUF_RENDERER(widget);
2215
2216         pr->drag_moved = PR_DRAG_SCROLL_THRESHHOLD;
2217 }
2218
2219 static void pr_signals_connect(PixbufRenderer *pr)
2220 {
2221         g_signal_connect(G_OBJECT(pr), "motion_notify_event",
2222                          G_CALLBACK(pr_mouse_motion_cb), pr);
2223         g_signal_connect(G_OBJECT(pr), "button_press_event",
2224                          G_CALLBACK(pr_mouse_press_cb), pr);
2225         g_signal_connect(G_OBJECT(pr), "button_release_event",
2226                          G_CALLBACK(pr_mouse_release_cb), pr);
2227         g_signal_connect(G_OBJECT(pr), "leave_notify_event",
2228                          G_CALLBACK(pr_mouse_leave_cb), pr);
2229         g_signal_connect(G_OBJECT(pr), "leave_notify_event",
2230                          G_CALLBACK(pr_leave_notify_cb), pr);
2231
2232         gtk_widget_set_events(GTK_WIDGET(pr), GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK |
2233                                               static_cast<GdkEventMask>(GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK | GDK_SCROLL_MASK |
2234                                               GDK_LEAVE_NOTIFY_MASK));
2235
2236         g_signal_connect(G_OBJECT(pr), "drag_begin",
2237                          G_CALLBACK(pr_mouse_drag_cb), pr);
2238
2239 }
2240
2241 /*
2242  *-------------------------------------------------------------------
2243  * stereo support
2244  *-------------------------------------------------------------------
2245  */
2246
2247 enum {
2248         COLOR_BYTES = 3,   /* rgb */
2249         RC = 0,            /* Red-Cyan */
2250         GM = 1,            /* Green-Magenta */
2251         YB = 2            /* Yellow-Blue */
2252 };
2253
2254 static void pr_create_anaglyph_color(GdkPixbuf *pixbuf, GdkPixbuf *right, gint x, gint y, gint w, gint h, guint mode)
2255 {
2256         gint srs;
2257         gint drs;
2258         guchar *s_pix;
2259         guchar *d_pix;
2260         guchar *sp;
2261         guchar *dp;
2262         guchar *spi;
2263         guchar *dpi;
2264         gint i;
2265         gint j;
2266
2267         srs = gdk_pixbuf_get_rowstride(right);
2268         s_pix = gdk_pixbuf_get_pixels(right);
2269         spi = s_pix + (x * COLOR_BYTES);
2270
2271         drs = gdk_pixbuf_get_rowstride(pixbuf);
2272         d_pix = gdk_pixbuf_get_pixels(pixbuf);
2273         dpi =  d_pix + x * COLOR_BYTES;
2274
2275         for (i = y; i < y + h; i++)
2276                 {
2277                 sp = spi + (i * srs);
2278                 dp = dpi + (i * drs);
2279                 for (j = 0; j < w; j++)
2280                         {
2281                         switch(mode)
2282                                 {
2283                                 case RC:
2284                                         dp[0] = sp[0]; /* copy red channel */
2285                                         break;
2286                                 case GM:
2287                                         dp[1] = sp[1];
2288                                         break;
2289                                 case YB:
2290                                         dp[0] = sp[0];
2291                                         dp[1] = sp[1];
2292                                         break;
2293                                 }
2294                         sp += COLOR_BYTES;
2295                         dp += COLOR_BYTES;
2296                         }
2297                 }
2298 }
2299
2300 static void pr_create_anaglyph_gray(GdkPixbuf *pixbuf, GdkPixbuf *right, gint x, gint y, gint w, gint h, guint mode)
2301 {
2302         gint srs;
2303         gint drs;
2304         guchar *s_pix;
2305         guchar *d_pix;
2306         guchar *sp;
2307         guchar *dp;
2308         guchar *spi;
2309         guchar *dpi;
2310         gint i;
2311         gint j;
2312         const double gc[3] = {0.299, 0.587, 0.114};
2313
2314         srs = gdk_pixbuf_get_rowstride(right);
2315         s_pix = gdk_pixbuf_get_pixels(right);
2316         spi = s_pix + (x * COLOR_BYTES);
2317
2318         drs = gdk_pixbuf_get_rowstride(pixbuf);
2319         d_pix = gdk_pixbuf_get_pixels(pixbuf);
2320         dpi =  d_pix + x * COLOR_BYTES;
2321
2322         for (i = y; i < y + h; i++)
2323                 {
2324                 sp = spi + (i * srs);
2325                 dp = dpi + (i * drs);
2326                 for (j = 0; j < w; j++)
2327                         {
2328                         guchar g1 = dp[0] * gc[0] + dp[1] * gc[1] + dp[2] * gc[2];
2329                         guchar g2 = sp[0] * gc[0] + sp[1] * gc[1] + sp[2] * gc[2];
2330                         switch(mode)
2331                                 {
2332                                 case RC:
2333                                         dp[0] = g2; /* red channel from sp */
2334                                         dp[1] = g1; /* green and blue from dp */
2335                                         dp[2] = g1;
2336                                         break;
2337                                 case GM:
2338                                         dp[0] = g1;
2339                                         dp[1] = g2;
2340                                         dp[2] = g1;
2341                                         break;
2342                                 case YB:
2343                                         dp[0] = g2;
2344                                         dp[1] = g2;
2345                                         dp[2] = g1;
2346                                         break;
2347                                 }
2348                         sp += COLOR_BYTES;
2349                         dp += COLOR_BYTES;
2350                         }
2351                 }
2352 }
2353
2354 static void pr_create_anaglyph_dubois(GdkPixbuf *pixbuf, GdkPixbuf *right, gint x, gint y, gint w, gint h, guint mode)
2355 {
2356         gint srs;
2357         gint drs;
2358         guchar *s_pix;
2359         guchar *d_pix;
2360         guchar *sp;
2361         guchar *dp;
2362         guchar *spi;
2363         guchar *dpi;
2364         gint i;
2365         gint j;
2366         gint k;
2367         double pr_dubois_matrix[3][6];
2368         static const double pr_dubois_matrix_RC[3][6] = {
2369                 { 0.456,  0.500,  0.176, -0.043, -0.088, -0.002},
2370                 {-0.040, -0.038, -0.016,  0.378,  0.734, -0.018},
2371                 {-0.015, -0.021, -0.005, -0.072, -0.113,  1.226}};
2372         static const double pr_dubois_matrix_GM[3][6] = {
2373                 {-0.062, -0.158, -0.039,  0.529,  0.705,  0.024},
2374                 { 0.284,  0.668,  0.143, -0.016, -0.015, -0.065},
2375                 {-0.015, -0.027,  0.021,  0.009,  0.075,  0.937}};
2376         static const double pr_dubois_matrix_YB[3][6] = {
2377                 { 1.000, -0.193,  0.282, -0.015, -0.116, -0.016},
2378                 {-0.024,  0.855,  0.064,  0.006,  0.058, -0.016},
2379                 {-0.036, -0.163,  0.021,  0.089,  0.174,  0.858}};
2380
2381         switch(mode)
2382                 {
2383                 case RC:
2384                         memcpy(pr_dubois_matrix, pr_dubois_matrix_RC, sizeof pr_dubois_matrix);
2385                         break;
2386                 case GM:
2387                         memcpy(pr_dubois_matrix, pr_dubois_matrix_GM, sizeof pr_dubois_matrix);
2388                         break;
2389                 case YB:
2390                         memcpy(pr_dubois_matrix, pr_dubois_matrix_YB, sizeof pr_dubois_matrix);
2391                         break;
2392                 }
2393
2394         srs = gdk_pixbuf_get_rowstride(right);
2395         s_pix = gdk_pixbuf_get_pixels(right);
2396         spi = s_pix + (x * COLOR_BYTES);
2397
2398         drs = gdk_pixbuf_get_rowstride(pixbuf);
2399         d_pix = gdk_pixbuf_get_pixels(pixbuf);
2400         dpi =  d_pix + x * COLOR_BYTES;
2401
2402         for (i = y; i < y + h; i++)
2403                 {
2404                 sp = spi + (i * srs);
2405                 dp = dpi + (i * drs);
2406                 for (j = 0; j < w; j++)
2407                         {
2408                         double res[3];
2409                         for (k = 0; k < 3; k++)
2410                                 {
2411                                 const double *m = pr_dubois_matrix[k];
2412                                 res[k] = sp[0] * m[0] + sp[1] * m[1] + sp[2] * m[2] + dp[0] * m[3] + dp[1] * m[4] + dp[2] * m[5];
2413                                 if (res[k] < 0.0) res[k] = 0;
2414                                 if (res[k] > 255.0) res[k] = 255.0;
2415                                 }
2416                         dp[0] = res[0];
2417                         dp[1] = res[1];
2418                         dp[2] = res[2];
2419                         sp += COLOR_BYTES;
2420                         dp += COLOR_BYTES;
2421                         }
2422                 }
2423 }
2424
2425 void pr_create_anaglyph(guint mode, GdkPixbuf *pixbuf, GdkPixbuf *right, gint x, gint y, gint w, gint h)
2426 {
2427         if (mode & PR_STEREO_ANAGLYPH_RC)
2428                 pr_create_anaglyph_color(pixbuf, right, x, y, w, h, RC);
2429         else if (mode & PR_STEREO_ANAGLYPH_GM)
2430                 pr_create_anaglyph_color(pixbuf, right, x, y, w, h, GM);
2431         else if (mode & PR_STEREO_ANAGLYPH_YB)
2432                 pr_create_anaglyph_color(pixbuf, right, x, y, w, h, YB);
2433         else if (mode & PR_STEREO_ANAGLYPH_GRAY_RC)
2434                 pr_create_anaglyph_gray(pixbuf, right, x, y, w, h, RC);
2435         else if (mode & PR_STEREO_ANAGLYPH_GRAY_GM)
2436                 pr_create_anaglyph_gray(pixbuf, right, x, y, w, h, GM);
2437         else if (mode & PR_STEREO_ANAGLYPH_GRAY_YB)
2438                 pr_create_anaglyph_gray(pixbuf, right, x, y, w, h, YB);
2439         else if (mode & PR_STEREO_ANAGLYPH_DB_RC)
2440                 pr_create_anaglyph_dubois(pixbuf, right, x, y, w, h, RC);
2441         else if (mode & PR_STEREO_ANAGLYPH_DB_GM)
2442                 pr_create_anaglyph_dubois(pixbuf, right, x, y, w, h, GM);
2443         else if (mode & PR_STEREO_ANAGLYPH_DB_YB)
2444                 pr_create_anaglyph_dubois(pixbuf, right, x, y, w, h, YB);
2445 }
2446
2447 /*
2448  *-------------------------------------------------------------------
2449  * public
2450  *-------------------------------------------------------------------
2451  */
2452 static void pr_pixbuf_size_sync(PixbufRenderer *pr)
2453 {
2454         pr->stereo_pixbuf_offset_left = 0;
2455         pr->stereo_pixbuf_offset_right = 0;
2456         if (!pr->pixbuf) return;
2457         switch (pr->orientation)
2458                 {
2459                 case EXIF_ORIENTATION_LEFT_TOP:
2460                 case EXIF_ORIENTATION_RIGHT_TOP:
2461                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2462                 case EXIF_ORIENTATION_LEFT_BOTTOM:
2463                         pr->image_width = gdk_pixbuf_get_height(pr->pixbuf);
2464                         pr->image_height = gdk_pixbuf_get_width(pr->pixbuf);
2465                         if (pr->stereo_data == STEREO_PIXBUF_SBS)
2466                                 {
2467                                 pr->image_height /= 2;
2468                                 pr->stereo_pixbuf_offset_right = pr->image_height;
2469                                 }
2470                         else if (pr->stereo_data == STEREO_PIXBUF_CROSS)
2471                                 {
2472                                 pr->image_height /= 2;
2473                                 pr->stereo_pixbuf_offset_left = pr->image_height;
2474                                 }
2475
2476                         break;
2477                 default:
2478                         pr->image_width = gdk_pixbuf_get_width(pr->pixbuf);
2479                         pr->image_height = gdk_pixbuf_get_height(pr->pixbuf);
2480                         if (pr->stereo_data == STEREO_PIXBUF_SBS)
2481                                 {
2482                                 pr->image_width /= 2;
2483                                 pr->stereo_pixbuf_offset_right = pr->image_width;
2484                                 }
2485                         else if (pr->stereo_data == STEREO_PIXBUF_CROSS)
2486                                 {
2487                                 pr->image_width /= 2;
2488                                 pr->stereo_pixbuf_offset_left = pr->image_width;
2489                                 }
2490                 }
2491 }
2492
2493 static void pr_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom, PrZoomFlags flags)
2494 {
2495         if (pixbuf) g_object_ref(pixbuf);
2496         if (pr->pixbuf) g_object_unref(pr->pixbuf);
2497         pr->pixbuf = pixbuf;
2498
2499         if (!pr->pixbuf)
2500                 {
2501                 /* no pixbuf so just clear the window */
2502                 pr->image_width = 0;
2503                 pr->image_height = 0;
2504                 pr->scale = 1.0;
2505                 pr->zoom = zoom; /* don't throw away the zoom value, it is set by pixbuf_renderer_move, among others,
2506                                     and used for pixbuf_renderer_zoom_get */
2507
2508                 pr->renderer->update_pixbuf(pr->renderer, flags & PR_ZOOM_LAZY);
2509                 if (pr->renderer2) pr->renderer2->update_pixbuf(pr->renderer2, flags & PR_ZOOM_LAZY);
2510
2511                 pr_update_signal(pr);
2512
2513                 return;
2514                 }
2515
2516         if (pr->stereo_mode & PR_STEREO_TEMP_DISABLE)
2517                 {
2518                 gint disable = !pr->pixbuf || ! pr->stereo_data;
2519                 pr_stereo_temp_disable(pr, disable);
2520                 }
2521
2522         pr_pixbuf_size_sync(pr);
2523         pr->renderer->update_pixbuf(pr->renderer, flags & PR_ZOOM_LAZY);
2524         if (pr->renderer2) pr->renderer2->update_pixbuf(pr->renderer2, flags & PR_ZOOM_LAZY);
2525         pr_zoom_sync(pr, zoom, static_cast<PrZoomFlags>(flags | PR_ZOOM_FORCE | PR_ZOOM_NEW), 0, 0);
2526 }
2527
2528 void pixbuf_renderer_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
2529 {
2530         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2531
2532         pr_source_tile_unset(pr);
2533
2534         pr_set_pixbuf(pr, pixbuf, zoom, PR_ZOOM_NONE);
2535
2536         pr_update_signal(pr);
2537 }
2538
2539 void pixbuf_renderer_set_pixbuf_lazy(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom, gint orientation, StereoPixbufData stereo_data)
2540 {
2541         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2542
2543         pr_source_tile_unset(pr);
2544
2545         pr->orientation = orientation;
2546         pr->stereo_data = stereo_data;
2547         pr_set_pixbuf(pr, pixbuf, zoom, PR_ZOOM_LAZY);
2548
2549         pr_update_signal(pr);
2550 }
2551
2552 GdkPixbuf *pixbuf_renderer_get_pixbuf(PixbufRenderer *pr)
2553 {
2554         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
2555
2556         return pr->pixbuf;
2557 }
2558
2559 void pixbuf_renderer_set_orientation(PixbufRenderer *pr, gint orientation)
2560 {
2561         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2562
2563         pr->orientation = orientation;
2564
2565         pr_pixbuf_size_sync(pr);
2566         pr_zoom_sync(pr, pr->zoom, PR_ZOOM_FORCE, 0, 0);
2567 }
2568
2569 #pragma GCC diagnostic push
2570 #pragma GCC diagnostic ignored "-Wunused-function"
2571 gint pixbuf_renderer_get_orientation_unused(PixbufRenderer *pr)
2572 {
2573         if (!pr) return 1;
2574         return pr->orientation;
2575 }
2576 #pragma GCC diagnostic pop
2577
2578 void pixbuf_renderer_set_stereo_data(PixbufRenderer *pr, StereoPixbufData stereo_data)
2579 {
2580         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2581         if (pr->stereo_data == stereo_data) return;
2582
2583
2584         pr->stereo_data = stereo_data;
2585
2586         if (pr->stereo_mode & PR_STEREO_TEMP_DISABLE)
2587                 {
2588                 gint disable = !pr->pixbuf || ! pr->stereo_data;
2589                 pr_stereo_temp_disable(pr, disable);
2590                 }
2591         pr_pixbuf_size_sync(pr);
2592         pr->renderer->update_pixbuf(pr->renderer, FALSE);
2593         if (pr->renderer2) pr->renderer2->update_pixbuf(pr->renderer2, FALSE);
2594         pr_zoom_sync(pr, pr->zoom, PR_ZOOM_FORCE, 0, 0);
2595 }
2596
2597 void pixbuf_renderer_set_post_process_func(PixbufRenderer *pr, PixbufRendererPostProcessFunc func, gpointer user_data, gboolean slow)
2598 {
2599         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2600
2601         pr->func_post_process = func;
2602         pr->post_process_user_data = user_data;
2603         pr->post_process_slow = func && slow;
2604
2605 }
2606
2607
2608 void pixbuf_renderer_move(PixbufRenderer *pr, PixbufRenderer *source)
2609 {
2610         GObject *object;
2611         PixbufRendererScrollResetType scroll_reset;
2612
2613         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2614         g_return_if_fail(IS_PIXBUF_RENDERER(source));
2615
2616         if (pr == source) return;
2617
2618         object = G_OBJECT(pr);
2619
2620         g_object_set(object, "zoom_min", source->zoom_min, NULL);
2621         g_object_set(object, "zoom_max", source->zoom_max, NULL);
2622         g_object_set(object, "loading", source->loading, NULL);
2623
2624         pr->complete = source->complete;
2625
2626         pr->x_scroll = source->x_scroll;
2627         pr->y_scroll = source->y_scroll;
2628         pr->x_mouse  = source->x_mouse;
2629         pr->y_mouse  = source->y_mouse;
2630
2631         scroll_reset = pr->scroll_reset;
2632         pr->scroll_reset = PR_SCROLL_RESET_NOCHANGE;
2633
2634         pr->func_post_process = source->func_post_process;
2635         pr->post_process_user_data = source->post_process_user_data;
2636         pr->post_process_slow = source->post_process_slow;
2637         pr->orientation = source->orientation;
2638         pr->stereo_data = source->stereo_data;
2639
2640         if (source->source_tiles_enabled)
2641                 {
2642                 pr_source_tile_unset(pr);
2643
2644                 pr->source_tiles_enabled = source->source_tiles_enabled;
2645                 pr->source_tiles_cache_size = source->source_tiles_cache_size;
2646                 pr->source_tile_width = source->source_tile_width;
2647                 pr->source_tile_height = source->source_tile_height;
2648                 pr->image_width = source->image_width;
2649                 pr->image_height = source->image_height;
2650
2651                 pr->func_tile_request = source->func_tile_request;
2652                 pr->func_tile_dispose = source->func_tile_dispose;
2653                 pr->func_tile_data = source->func_tile_data;
2654
2655                 pr->source_tiles = source->source_tiles;
2656                 source->source_tiles = nullptr;
2657
2658                 pr_zoom_sync(pr, source->zoom, static_cast<PrZoomFlags>(PR_ZOOM_FORCE | PR_ZOOM_NEW), 0, 0);
2659                 }
2660         else
2661                 {
2662                 pixbuf_renderer_set_pixbuf(pr, source->pixbuf, source->zoom);
2663                 }
2664
2665         pr->scroll_reset = scroll_reset;
2666
2667         pixbuf_renderer_set_pixbuf(source, nullptr, source->zoom);
2668 }
2669
2670 void pixbuf_renderer_copy(PixbufRenderer *pr, PixbufRenderer *source)
2671 {
2672         GObject *object;
2673         PixbufRendererScrollResetType scroll_reset;
2674
2675         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2676         g_return_if_fail(IS_PIXBUF_RENDERER(source));
2677
2678         if (pr == source) return;
2679
2680         object = G_OBJECT(pr);
2681
2682         g_object_set(object, "zoom_min", source->zoom_min, NULL);
2683         g_object_set(object, "zoom_max", source->zoom_max, NULL);
2684         g_object_set(object, "loading", source->loading, NULL);
2685
2686         pr->complete = source->complete;
2687
2688         pr->x_scroll = source->x_scroll;
2689         pr->y_scroll = source->y_scroll;
2690         pr->x_mouse  = source->x_mouse;
2691         pr->y_mouse  = source->y_mouse;
2692
2693         scroll_reset = pr->scroll_reset;
2694         pr->scroll_reset = PR_SCROLL_RESET_NOCHANGE;
2695
2696         pr->orientation = source->orientation;
2697         pr->stereo_data = source->stereo_data;
2698
2699         if (source->source_tiles_enabled)
2700                 {
2701                 pr->source_tiles_enabled = source->source_tiles_enabled;
2702                 pr->source_tiles_cache_size = source->source_tiles_cache_size;
2703                 pr->source_tile_width = source->source_tile_width;
2704                 pr->source_tile_height = source->source_tile_height;
2705                 pr->image_width = source->image_width;
2706                 pr->image_height = source->image_height;
2707
2708                 pr->func_tile_request = source->func_tile_request;
2709                 pr->func_tile_dispose = source->func_tile_dispose;
2710                 pr->func_tile_data = source->func_tile_data;
2711
2712                 pr->source_tiles = source->source_tiles;
2713                 source->source_tiles = nullptr;
2714
2715                 pr_zoom_sync(pr, source->zoom, static_cast<PrZoomFlags>(PR_ZOOM_FORCE | PR_ZOOM_NEW), 0, 0);
2716                 }
2717         else
2718                 {
2719                 pixbuf_renderer_set_pixbuf(pr, source->pixbuf, source->zoom);
2720                 }
2721
2722         pr->scroll_reset = scroll_reset;
2723 }
2724
2725 void pixbuf_renderer_area_changed(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
2726 {
2727         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2728
2729         if (pr->source_tiles_enabled)
2730                 {
2731                 pr_source_tile_changed(pr, x, y, w, h);
2732                 }
2733
2734         pr->renderer->area_changed(pr->renderer, x, y, w, h);
2735         if (pr->renderer2) pr->renderer2->area_changed(pr->renderer2, x, y, w, h);
2736 }
2737
2738 void pixbuf_renderer_zoom_adjust(PixbufRenderer *pr, gdouble increment)
2739 {
2740         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2741
2742         pr_zoom_adjust_real(pr, increment, PR_ZOOM_NONE, 0, 0);
2743 }
2744
2745 void pixbuf_renderer_zoom_adjust_at_point(PixbufRenderer *pr, gdouble increment, gint x, gint y)
2746 {
2747         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2748
2749         pr_zoom_adjust_real(pr, increment, PR_ZOOM_CENTER, x, y);
2750 }
2751
2752 void pixbuf_renderer_zoom_set(PixbufRenderer *pr, gdouble zoom)
2753 {
2754         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2755
2756         pr_zoom_sync(pr, zoom, PR_ZOOM_NONE, 0, 0);
2757 }
2758
2759 gdouble pixbuf_renderer_zoom_get(PixbufRenderer *pr)
2760 {
2761         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
2762
2763         return pr->zoom;
2764 }
2765
2766 gdouble pixbuf_renderer_zoom_get_scale(PixbufRenderer *pr)
2767 {
2768         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
2769
2770         return pr->scale;
2771 }
2772
2773 void pixbuf_renderer_zoom_set_limits(PixbufRenderer *pr, gdouble min, gdouble max)
2774 {
2775         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2776
2777         if (min > 1.0 || max < 1.0) return;
2778         if (min < 1.0 && min > -1.0) return;
2779         if (min < -200.0 || max > 200.0) return;
2780
2781         if (pr->zoom_min != min)
2782                 {
2783                 pr->zoom_min = min;
2784                 g_object_notify(G_OBJECT(pr), "zoom_min");
2785                 }
2786         if (pr->zoom_max != max)
2787                 {
2788                 pr->zoom_max = max;
2789                 g_object_notify(G_OBJECT(pr), "zoom_max");
2790                 }
2791 }
2792
2793 static void pr_stereo_set(PixbufRenderer *pr)
2794 {
2795         if (!pr->renderer) pr->renderer = pr_backend_renderer_new(pr);
2796
2797         pr->renderer->stereo_set(pr->renderer, pr->stereo_mode & ~PR_STEREO_MIRROR_RIGHT & ~PR_STEREO_FLIP_RIGHT);
2798
2799         if (pr->stereo_mode & (PR_STEREO_HORIZ | PR_STEREO_VERT | PR_STEREO_FIXED))
2800                 {
2801                 if (!pr->renderer2) pr->renderer2 = pr_backend_renderer_new(pr);
2802                 pr->renderer2->stereo_set(pr->renderer2, (pr->stereo_mode & ~PR_STEREO_MIRROR_LEFT & ~PR_STEREO_FLIP_LEFT) | PR_STEREO_RIGHT);
2803                 }
2804         else
2805                 {
2806                 if (pr->renderer2) pr->renderer2->free(pr->renderer2);
2807                 pr->renderer2 = nullptr;
2808                 }
2809         if (pr->stereo_mode & PR_STEREO_HALF)
2810                 {
2811                 if (pr->stereo_mode & PR_STEREO_HORIZ) pr->aspect_ratio = 2.0;
2812                 else if (pr->stereo_mode & PR_STEREO_VERT) pr->aspect_ratio = 0.5;
2813                 else pr->aspect_ratio = 1.0;
2814                 }
2815         else
2816                 {
2817                 pr->aspect_ratio = 1.0;
2818                 }
2819 }
2820
2821 void pixbuf_renderer_stereo_set(PixbufRenderer *pr, gint stereo_mode)
2822 {
2823         gboolean redraw = !(pr->stereo_mode == stereo_mode) || pr->stereo_temp_disable;
2824         pr->stereo_mode = stereo_mode;
2825         if ((stereo_mode & PR_STEREO_TEMP_DISABLE) && pr->stereo_temp_disable) return;
2826
2827         pr->stereo_temp_disable = FALSE;
2828
2829         pr_stereo_set(pr);
2830
2831         if (redraw)
2832                 {
2833                 pr_size_sync(pr, pr->window_width, pr->window_height); /* recalculate new viewport */
2834                 pr_zoom_sync(pr, pr->zoom, static_cast<PrZoomFlags>(PR_ZOOM_FORCE | PR_ZOOM_NEW), 0, 0);
2835                 }
2836 }
2837
2838 void pixbuf_renderer_stereo_fixed_set(PixbufRenderer *pr, gint width, gint height, gint x1, gint y1, gint x2, gint y2)
2839 {
2840         pr->stereo_fixed_width = width;
2841         pr->stereo_fixed_height = height;
2842         pr->stereo_fixed_x_left = x1;
2843         pr->stereo_fixed_y_left = y1;
2844         pr->stereo_fixed_x_right = x2;
2845         pr->stereo_fixed_y_right = y2;
2846 }
2847
2848 gint pixbuf_renderer_stereo_get(PixbufRenderer *pr)
2849 {
2850         return pr->stereo_mode;
2851 }
2852
2853 static void pr_stereo_temp_disable(PixbufRenderer *pr, gboolean disable)
2854 {
2855         if (pr->stereo_temp_disable == disable) return;
2856         pr->stereo_temp_disable = disable;
2857         if (disable)
2858                 {
2859                 if (!pr->renderer) pr->renderer = pr_backend_renderer_new(pr);
2860                 pr->renderer->stereo_set(pr->renderer, PR_STEREO_NONE);
2861                 if (pr->renderer2) pr->renderer2->free(pr->renderer2);
2862                 pr->renderer2 = nullptr;
2863                 pr->aspect_ratio = 1.0;
2864                 }
2865         else
2866                 {
2867                 pr_stereo_set(pr);
2868                 }
2869         pr_size_sync(pr, pr->window_width, pr->window_height); /* recalculate new viewport */
2870 }
2871
2872 gboolean pixbuf_renderer_get_pixel_colors(PixbufRenderer *pr, gint x_pixel, gint y_pixel,
2873                                           gint *r_mouse, gint *g_mouse, gint *b_mouse)
2874 {
2875         GdkPixbuf *pb = pr->pixbuf;
2876         gint p_alpha;
2877         gint prs;
2878         guchar *p_pix;
2879         guchar *pp;
2880         gint map_x;
2881         gint map_y;
2882         gint map_w;
2883         gint map_h;
2884         size_t xoff;
2885         size_t yoff;
2886
2887         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
2888         g_return_val_if_fail(r_mouse != nullptr && g_mouse != nullptr && b_mouse != nullptr, FALSE);
2889
2890         if (!pr->pixbuf && !pr->source_tiles_enabled)
2891                 {
2892                 *r_mouse = -1;
2893                 *g_mouse = -1;
2894                 *b_mouse = -1;
2895                 return FALSE;
2896                 }
2897
2898         if (!pb) return FALSE;
2899
2900         pr_tile_region_map_orientation(pr->orientation,
2901                                         x_pixel, y_pixel,
2902                                         pr->image_width, pr->image_height,
2903                                         1, 1, /*single pixel */
2904                                         &map_x, &map_y,
2905                                         &map_w, &map_h);
2906
2907         if (map_x < 0 || map_x > gdk_pixbuf_get_width(pr->pixbuf) - 1) return  FALSE;
2908         if (map_y < 0 || map_y > gdk_pixbuf_get_height(pr->pixbuf) - 1) return  FALSE;
2909
2910         p_alpha = gdk_pixbuf_get_has_alpha(pb);
2911         prs = gdk_pixbuf_get_rowstride(pb);
2912         p_pix = gdk_pixbuf_get_pixels(pb);
2913
2914         xoff = static_cast<size_t>(map_x) * (p_alpha ? 4 : 3);
2915         yoff = static_cast<size_t>(map_y) * prs;
2916         pp = p_pix + yoff + xoff;
2917         *r_mouse = *pp;
2918         pp++;
2919         *g_mouse = *pp;
2920         pp++;
2921         *b_mouse = *pp;
2922
2923         return TRUE;
2924 }
2925
2926 gboolean pixbuf_renderer_get_mouse_position(PixbufRenderer *pr, gint *x_pixel_return, gint *y_pixel_return)
2927 {
2928         gint x_pixel;
2929         gint y_pixel;
2930         gint x_pixel_clamped;
2931         gint y_pixel_clamped;
2932
2933         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
2934         g_return_val_if_fail(x_pixel_return != nullptr && y_pixel_return != nullptr, FALSE);
2935
2936         if (!pr->pixbuf && !pr->source_tiles_enabled)
2937                 {
2938                 *x_pixel_return = -1;
2939                 *y_pixel_return = -1;
2940                 return FALSE;
2941                 }
2942
2943         x_pixel = floor(static_cast<gdouble>(pr->x_mouse - pr->x_offset + pr->x_scroll) / pr->scale);
2944         y_pixel = floor(static_cast<gdouble>(pr->y_mouse - pr->y_offset + pr->y_scroll) / pr->scale / pr->aspect_ratio);
2945         x_pixel_clamped = CLAMP(x_pixel, 0, pr->image_width - 1);
2946         y_pixel_clamped = CLAMP(y_pixel, 0, pr->image_height - 1);
2947
2948         if (x_pixel != x_pixel_clamped)
2949                 {
2950                 /* mouse is not on pr */
2951                 x_pixel = -1;
2952                 }
2953         if (y_pixel != y_pixel_clamped)
2954                 {
2955                 /* mouse is not on pr */
2956                 y_pixel = -1;
2957                 }
2958
2959         *x_pixel_return = x_pixel;
2960         *y_pixel_return = y_pixel;
2961
2962         return TRUE;
2963 }
2964
2965 gboolean pixbuf_renderer_get_image_size(PixbufRenderer *pr, gint *width, gint *height)
2966 {
2967         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
2968         g_return_val_if_fail(width != nullptr && height != nullptr, FALSE);
2969
2970         if (!pr->pixbuf && !pr->source_tiles_enabled && (!pr->image_width || !pr->image_height))
2971                 {
2972                 *width = 0;
2973                 *height = 0;
2974                 return FALSE;
2975                 }
2976
2977         *width = pr->image_width;
2978         *height = pr->image_height;
2979         return TRUE;
2980 }
2981
2982 gboolean pixbuf_renderer_get_scaled_size(PixbufRenderer *pr, gint *width, gint *height)
2983 {
2984         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
2985         g_return_val_if_fail(width != nullptr && height != nullptr, FALSE);
2986
2987         if (!pr->pixbuf && !pr->source_tiles_enabled && (!pr->image_width || !pr->image_height))
2988                 {
2989                 *width = 0;
2990                 *height = 0;
2991                 return FALSE;
2992                 }
2993
2994         *width = pr->width;
2995         *height = pr->height;
2996         return TRUE;
2997 }
2998
2999 gboolean pixbuf_renderer_get_visible_rect(PixbufRenderer *pr, GdkRectangle *rect)
3000 {
3001         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3002         g_return_val_if_fail(rect != nullptr, FALSE);
3003
3004         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
3005             !pr->scale)
3006                 {
3007                 rect->x = 0;
3008                 rect->y = 0;
3009                 rect->width = 0;
3010                 rect->height = 0;
3011                 return FALSE;
3012                 }
3013
3014         rect->x = static_cast<gint>(static_cast<gdouble>(pr->x_scroll) / pr->scale);
3015         rect->y = static_cast<gint>(static_cast<gdouble>(pr->y_scroll) / pr->scale / pr->aspect_ratio);
3016         rect->width = static_cast<gint>(static_cast<gdouble>(pr->vis_width) / pr->scale);
3017         rect->height = static_cast<gint>(static_cast<gdouble>(pr->vis_height) / pr->scale / pr->aspect_ratio);
3018         return TRUE;
3019 }
3020
3021 #pragma GCC diagnostic push
3022 #pragma GCC diagnostic ignored "-Wunused-function"
3023 gboolean pixbuf_renderer_get_virtual_rect_unused(PixbufRenderer *pr, GdkRectangle *rect)
3024 {
3025         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3026         g_return_val_if_fail(rect != nullptr, FALSE);
3027
3028         if ((!pr->pixbuf && !pr->source_tiles_enabled))
3029                 {
3030                 rect->x = 0;
3031                 rect->y = 0;
3032                 rect->width = 0;
3033                 rect->height = 0;
3034                 return FALSE;
3035                 }
3036
3037         rect->x = pr->x_scroll;
3038         rect->y = pr->y_scroll;
3039         rect->width = pr->vis_width;
3040         rect->height = pr->vis_height;
3041         return TRUE;
3042 }
3043 #pragma GCC diagnostic pop
3044
3045 void pixbuf_renderer_set_size_early(PixbufRenderer *, guint, guint)
3046 {
3047 #if 0
3048         /** @FIXME this function does not consider the image orientation,
3049         so it probably only breaks something */
3050         gdouble zoom;
3051         gint w, h;
3052
3053         zoom = pixbuf_renderer_zoom_get(pr);
3054         pr->image_width = width;
3055         pr->image_height = height;
3056
3057         pr_zoom_clamp(pr, zoom, PR_ZOOM_FORCE, NULL);
3058 #endif
3059 }
3060
3061 void pixbuf_renderer_set_ignore_alpha(PixbufRenderer *pr, gint ignore_alpha)
3062 {
3063    g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3064
3065    pr->ignore_alpha = ignore_alpha;
3066    pr_pixbuf_size_sync(pr);
3067    pr_zoom_sync(pr, pr->zoom, PR_ZOOM_FORCE, 0, 0);
3068 }
3069
3070 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */