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