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