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