7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
15 #include "pixbuf-renderer.h"
21 /* comment this out if not using this from within Geeqie
22 * defining GQ_BUILD does these things:
23 * - Sets the shift-click scroller pixbuf to a nice icon instead of a black box
29 #include "pixbuf_util.h"
36 /* size to use when breaking up image pane for rendering */
37 #define PR_TILE_SIZE 128
39 /* default size of tile cache (mb) */
40 #define PR_CACHE_SIZE_DEFAULT 8
42 /* default min and max zoom */
43 #define PR_ZOOM_MIN -32.0
44 #define PR_ZOOM_MAX 32.0
46 /* distance to drag mouse to disable image flip */
47 #define PR_DRAG_SCROLL_THRESHHOLD 4
49 /* increase pan rate when holding down shift */
50 #define PR_PAN_SHIFT_MULTIPLIER 6
53 #define PR_SCROLLER_UPDATES_PER_SEC 30
54 #define PR_SCROLLER_DEAD_ZONE 6
56 /* alpha channel checkerboard background (same as gimp) */
57 #define PR_ALPHA_CHECK1 0x00999999
58 #define PR_ALPHA_CHECK2 0x00666666
59 #define PR_ALPHA_CHECK_SIZE 16
61 /* when scaling image to below this size, use nearest pixel for scaling
62 * (below about 4, the other scale types become slow generating their conversion tables)
64 #define PR_MIN_SCALE_SIZE 8
67 EXIF_ORIENTATION_UNKNOWN = 0,
68 EXIF_ORIENTATION_TOP_LEFT = 1,
69 EXIF_ORIENTATION_TOP_RIGHT = 2,
70 EXIF_ORIENTATION_BOTTOM_RIGHT = 3,
71 EXIF_ORIENTATION_BOTTOM_LEFT = 4,
72 EXIF_ORIENTATION_LEFT_TOP = 5,
73 EXIF_ORIENTATION_RIGHT_TOP = 6,
74 EXIF_ORIENTATION_RIGHT_BOTTOM = 7,
75 EXIF_ORIENTATION_LEFT_BOTTOM = 8
76 } ExifOrientationType;
80 TILE_RENDER_NONE = 0, /* do nothing */
81 TILE_RENDER_AREA, /* render an area of the tile */
82 TILE_RENDER_ALL /* render the whole tile */
83 } ImageTileRenderType;
85 typedef struct _ImageTile ImageTile;
86 typedef struct _QueueData QueueData;
90 GdkPixmap *pixmap; /* off screen buffer */
91 GdkPixbuf *pixbuf; /* pixbuf area for zooming */
92 gint x; /* x offset into image */
93 gint y; /* y offset into image */
94 gint w; /* width that is visible (may be less if at edge of image) */
95 gint h; /* height '' */
99 /* render_todo: (explanation)
101 AREA render area of tile, usually only used when loading an image
102 note: will jump to an ALL if render_done is not ALL.
103 ALL render entire tile, if never done before w/ ALL, for expose events *only*
106 ImageTileRenderType render_todo; /* what to do (see above) */
107 ImageTileRenderType render_done; /* highest that has been done before on tile */
112 guint size; /* est. memory used by pixmap and pixbuf */
125 typedef struct _SourceTile SourceTile;
134 typedef struct _OverlayData OverlayData;
144 gint relative; /* x,y coordinates are relative, negative values start bottom right */
146 gint always; /* hide temporarily when scrolling (not yet implemented) */
152 SIGNAL_SCROLL_NOTIFY,
153 SIGNAL_RENDER_COMPLETE,
170 PROP_CACHE_SIZE_DISPLAY,
171 PROP_CACHE_SIZE_TILES,
174 PROP_WINDOW_LIMIT_VALUE,
176 PROP_AUTOFIT_LIMIT_VALUE
181 static guint signals[SIGNAL_COUNT] = { 0 };
182 static GtkEventBoxClass *parent_class = NULL;
186 static void pixbuf_renderer_class_init(PixbufRendererClass *class);
187 static void pixbuf_renderer_init(PixbufRenderer *pr);
188 static void pixbuf_renderer_finalize(GObject *object);
189 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
190 const GValue *value, GParamSpec *pspec);
191 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
192 GValue *value, GParamSpec *pspec);
193 static gint pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event);
195 static void pr_render_complete_signal(PixbufRenderer *pr);
197 static void pr_overlay_list_clear(PixbufRenderer *pr);
198 static void pr_scroller_timer_set(PixbufRenderer *pr, gint start);
199 static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h);
202 static void pr_source_tile_free_all(PixbufRenderer *pr);
203 static void pr_tile_free_all(PixbufRenderer *pr);
204 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h);
205 static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it);
206 static void pr_queue_clear(PixbufRenderer *pr);
207 static void pr_queue_merge(QueueData *parent, QueueData *qd);
208 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
209 gint clamp, ImageTileRenderType render, gint new_data, gint only_existing);
211 static void pr_redraw(PixbufRenderer *pr, gint new_data);
213 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
214 gint force, gint new,
215 gint center_point, gint px, gint py);
217 static void pr_signals_connect(PixbufRenderer *pr);
218 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data);
219 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area);
223 *-------------------------------------------------------------------
224 * Pixbuf Renderer object
225 *-------------------------------------------------------------------
228 GType pixbuf_renderer_get_type(void)
230 static GType pixbuf_renderer_type = 0;
232 if (!pixbuf_renderer_type)
234 static const GTypeInfo pixbuf_renderer_info =
236 sizeof(PixbufRendererClass),
237 NULL, /* base_init */
238 NULL, /* base_finalize */
239 (GClassInitFunc)pixbuf_renderer_class_init,
240 NULL, /* class_finalize */
241 NULL, /* class_data */
242 sizeof(PixbufRenderer),
244 (GInstanceInitFunc)pixbuf_renderer_init,
247 pixbuf_renderer_type = g_type_register_static(GTK_TYPE_EVENT_BOX, "PixbufRenderer",
248 &pixbuf_renderer_info, 0);
251 return pixbuf_renderer_type;
254 static void pixbuf_renderer_class_init(PixbufRendererClass *class)
256 GObjectClass *gobject_class = G_OBJECT_CLASS(class);
257 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);
259 parent_class = g_type_class_peek_parent(class);
261 gobject_class->set_property = pixbuf_renderer_set_property;
262 gobject_class->get_property = pixbuf_renderer_get_property;
264 gobject_class->finalize = pixbuf_renderer_finalize;
266 widget_class->expose_event = pixbuf_renderer_expose;
268 g_object_class_install_property(gobject_class,
270 g_param_spec_double("zoom_min",
276 G_PARAM_READABLE | G_PARAM_WRITABLE));
278 g_object_class_install_property(gobject_class,
280 g_param_spec_double("zoom_max",
286 G_PARAM_READABLE | G_PARAM_WRITABLE));
288 g_object_class_install_property(gobject_class,
290 g_param_spec_uint("zoom_quality",
296 G_PARAM_READABLE | G_PARAM_WRITABLE));
298 g_object_class_install_property(gobject_class,
300 g_param_spec_boolean("zoom_2pass",
304 G_PARAM_READABLE | G_PARAM_WRITABLE));
306 g_object_class_install_property(gobject_class,
308 g_param_spec_boolean("zoom_expand",
309 "Expand image in autozoom.",
312 G_PARAM_READABLE | G_PARAM_WRITABLE));
314 g_object_class_install_property(gobject_class,
316 g_param_spec_uint("dither_quality",
321 GDK_RGB_DITHER_NORMAL,
322 G_PARAM_READABLE | G_PARAM_WRITABLE));
324 g_object_class_install_property(gobject_class,
326 g_param_spec_uint("scroll_reset",
327 "New image scroll reset",
329 PR_SCROLL_RESET_TOPLEFT,
330 PR_SCROLL_RESET_NOCHANGE,
331 PR_SCROLL_RESET_TOPLEFT,
332 G_PARAM_READABLE | G_PARAM_WRITABLE));
334 g_object_class_install_property(gobject_class,
336 g_param_spec_boolean("delay_flip",
337 "Delay image update",
340 G_PARAM_READABLE | G_PARAM_WRITABLE));
342 g_object_class_install_property(gobject_class,
344 g_param_spec_boolean("loading",
345 "Image actively loading",
348 G_PARAM_READABLE | G_PARAM_WRITABLE));
350 g_object_class_install_property(gobject_class,
352 g_param_spec_boolean("complete",
353 "Image rendering complete",
356 G_PARAM_READABLE | G_PARAM_WRITABLE));
358 g_object_class_install_property(gobject_class,
359 PROP_CACHE_SIZE_DISPLAY,
360 g_param_spec_uint("cache_display",
361 "Display cache size MB",
365 PR_CACHE_SIZE_DEFAULT,
366 G_PARAM_READABLE | G_PARAM_WRITABLE));
368 g_object_class_install_property(gobject_class,
369 PROP_CACHE_SIZE_TILES,
370 g_param_spec_uint("cache_tiles",
372 "Number of tiles to retain in memory at any one time.",
375 PR_CACHE_SIZE_DEFAULT,
376 G_PARAM_READABLE | G_PARAM_WRITABLE));
378 g_object_class_install_property(gobject_class,
380 g_param_spec_boolean("window_fit",
381 "Fit window to image size",
384 G_PARAM_READABLE | G_PARAM_WRITABLE));
386 g_object_class_install_property(gobject_class,
388 g_param_spec_boolean("window_limit",
389 "Limit size of parent window",
392 G_PARAM_READABLE | G_PARAM_WRITABLE));
394 g_object_class_install_property(gobject_class,
395 PROP_WINDOW_LIMIT_VALUE,
396 g_param_spec_uint("window_limit_value",
397 "Size limit of parent window",
402 G_PARAM_READABLE | G_PARAM_WRITABLE));
404 g_object_class_install_property(gobject_class,
406 g_param_spec_boolean("autofit_limit",
407 "Limit size of image when autofitting",
410 G_PARAM_READABLE | G_PARAM_WRITABLE));
412 g_object_class_install_property(gobject_class,
413 PROP_AUTOFIT_LIMIT_VALUE,
414 g_param_spec_uint("autofit_limit_value",
415 "Size limit of image when autofitting",
420 G_PARAM_READABLE | G_PARAM_WRITABLE));
423 signals[SIGNAL_ZOOM] =
425 G_OBJECT_CLASS_TYPE(gobject_class),
427 G_STRUCT_OFFSET(PixbufRendererClass, zoom),
429 g_cclosure_marshal_VOID__DOUBLE,
433 signals[SIGNAL_CLICKED] =
434 g_signal_new("clicked",
435 G_OBJECT_CLASS_TYPE(gobject_class),
437 G_STRUCT_OFFSET(PixbufRendererClass, clicked),
439 g_cclosure_marshal_VOID__BOXED,
443 signals[SIGNAL_SCROLL_NOTIFY] =
444 g_signal_new("scroll-notify",
445 G_OBJECT_CLASS_TYPE(gobject_class),
447 G_STRUCT_OFFSET(PixbufRendererClass, scroll_notify),
449 g_cclosure_marshal_VOID__VOID,
452 signals[SIGNAL_RENDER_COMPLETE] =
453 g_signal_new("render-complete",
454 G_OBJECT_CLASS_TYPE(gobject_class),
456 G_STRUCT_OFFSET(PixbufRendererClass, render_complete),
458 g_cclosure_marshal_VOID__VOID,
461 signals[SIGNAL_DRAG] =
463 G_OBJECT_CLASS_TYPE(gobject_class),
465 G_STRUCT_OFFSET(PixbufRendererClass, drag),
467 g_cclosure_marshal_VOID__BOXED,
472 static void pixbuf_renderer_init(PixbufRenderer *pr)
476 box = GTK_WIDGET(pr);
478 pr->zoom_min = PR_ZOOM_MIN;
479 pr->zoom_max = PR_ZOOM_MAX;
480 pr->zoom_quality = GDK_INTERP_BILINEAR;
481 pr->zoom_2pass = FALSE;
486 pr->dither_quality = GDK_RGB_DITHER_NORMAL;
488 pr->scroll_reset = PR_SCROLL_RESET_TOPLEFT;
490 pr->draw_idle_id = -1;
492 pr->tile_width = PR_TILE_SIZE;
493 pr->tile_height = PR_TILE_SIZE;
496 pr->tile_cache_size = 0;
498 pr->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
500 pr->scroller_id = -1;
501 pr->scroller_overlay = -1;
503 pr->source_tiles_enabled = FALSE;
504 pr->source_tiles = NULL;
508 gtk_widget_set_double_buffered(box, FALSE);
509 g_signal_connect_after(G_OBJECT(box), "size_allocate",
510 G_CALLBACK(pr_size_cb), pr);
512 pr_signals_connect(pr);
515 static void pixbuf_renderer_finalize(GObject *object)
519 pr = PIXBUF_RENDERER(object);
522 pr_tile_free_all(pr);
524 if (pr->pixbuf) g_object_unref(pr->pixbuf);
525 if (pr->spare_tile) gdk_pixbuf_unref(pr->spare_tile);
527 pr_scroller_timer_set(pr, FALSE);
528 pr_overlay_list_clear(pr);
530 pr_source_tile_free_all(pr);
533 PixbufRenderer* pixbuf_renderer_new(void)
535 return g_object_new(TYPE_PIXBUF_RENDERER, NULL);
538 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
539 const GValue *value, GParamSpec *pspec)
543 pr = PIXBUF_RENDERER(object);
548 pr->zoom_min = g_value_get_double(value);
551 pr->zoom_max = g_value_get_double(value);
553 case PROP_ZOOM_QUALITY:
554 pr->zoom_quality = g_value_get_uint(value);
556 case PROP_ZOOM_2PASS:
557 pr->zoom_2pass = g_value_get_boolean(value);
559 case PROP_ZOOM_EXPAND:
560 pr->zoom_expand = g_value_get_boolean(value);
562 case PROP_DITHER_QUALITY:
563 pr->dither_quality = g_value_get_uint(value);
565 case PROP_SCROLL_RESET:
566 pr->scroll_reset = g_value_get_uint(value);
568 case PROP_DELAY_FLIP:
569 pr->delay_flip = g_value_get_boolean(value);
572 pr->loading = g_value_get_boolean(value);
575 pr->complete = g_value_get_boolean(value);
577 case PROP_CACHE_SIZE_DISPLAY:
578 pr->tile_cache_max = g_value_get_uint(value);
580 case PROP_CACHE_SIZE_TILES:
581 pr->source_tiles_cache_size = g_value_get_uint(value);
583 case PROP_WINDOW_FIT:
584 pr->window_fit = g_value_get_boolean(value);
586 case PROP_WINDOW_LIMIT:
587 pr->window_limit = g_value_get_boolean(value);
589 case PROP_WINDOW_LIMIT_VALUE:
590 pr->window_limit_size = g_value_get_uint(value);
592 case PROP_AUTOFIT_LIMIT:
593 pr->autofit_limit = g_value_get_boolean(value);
595 case PROP_AUTOFIT_LIMIT_VALUE:
596 pr->autofit_limit_size = g_value_get_uint(value);
599 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
604 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
605 GValue *value, GParamSpec *pspec)
609 pr = PIXBUF_RENDERER(object);
614 g_value_set_double(value, pr->zoom_min);
617 g_value_set_double(value, pr->zoom_max);
619 case PROP_ZOOM_QUALITY:
620 g_value_set_uint(value, pr->zoom_quality);
622 case PROP_ZOOM_2PASS:
623 g_value_set_boolean(value, pr->zoom_2pass);
625 case PROP_ZOOM_EXPAND:
626 g_value_set_boolean(value, pr->zoom_expand);
628 case PROP_DITHER_QUALITY:
629 g_value_set_uint(value, pr->dither_quality);
631 case PROP_SCROLL_RESET:
632 g_value_set_uint(value, pr->scroll_reset);
634 case PROP_DELAY_FLIP:
635 g_value_set_boolean(value, pr->delay_flip);
638 g_value_set_boolean(value, pr->loading);
641 g_value_set_boolean(value, pr->complete);
643 case PROP_CACHE_SIZE_DISPLAY:
644 g_value_set_uint(value, pr->tile_cache_max);
646 case PROP_CACHE_SIZE_TILES:
647 g_value_set_uint(value, pr->source_tiles_cache_size);
649 case PROP_WINDOW_FIT:
650 g_value_set_boolean(value, pr->window_fit);
652 case PROP_WINDOW_LIMIT:
653 g_value_set_boolean(value, pr->window_limit);
655 case PROP_WINDOW_LIMIT_VALUE:
656 g_value_set_uint(value, pr->window_limit_size);
658 case PROP_AUTOFIT_LIMIT:
659 g_value_set_boolean(value, pr->autofit_limit);
661 case PROP_AUTOFIT_LIMIT_VALUE:
662 g_value_set_uint(value, pr->autofit_limit_size);
665 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
670 static gint pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event)
672 if (GTK_WIDGET_DRAWABLE(widget))
674 if (!GTK_WIDGET_NO_WINDOW(widget))
676 if (event->window != widget->window)
680 gdk_window_get_position(event->window, &area.x, &area.y);
682 area.x += event->area.x;
683 area.y += event->area.y;
684 area.width = event->area.width;
685 area.height = event->area.height;
686 pixbuf_renderer_paint(PIXBUF_RENDERER(widget), &area);
690 pixbuf_renderer_paint(PIXBUF_RENDERER(widget), &event->area);
699 *-------------------------------------------------------------------
701 *-------------------------------------------------------------------
704 static void widget_set_cursor(GtkWidget *widget, gint icon)
708 if (!widget->window) return;
716 cursor = gdk_cursor_new (icon);
719 gdk_window_set_cursor(widget->window, cursor);
721 if (cursor) gdk_cursor_unref(cursor);
724 static gint pixmap_calc_size(GdkPixmap *pixmap)
728 d = gdk_drawable_get_depth(pixmap);
729 gdk_drawable_get_size(pixmap, &w, &h);
730 return w * h * (d / 8);
733 static gint pr_clip_region(gint x, gint y, gint w, gint h,
734 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
735 gint *rx, gint *ry, gint *rw, gint *rh)
737 if (clip_x + clip_w <= x ||
739 clip_y + clip_h <= y ||
745 *rx = MAX(x, clip_x);
746 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
748 *ry = MAX(y, clip_y);
749 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
754 static gint pr_parent_window_sizable(PixbufRenderer *pr)
756 GdkWindowState state;
758 if (!pr->parent_window) return FALSE;
759 if (!pr->window_fit) return FALSE;
760 if (!GTK_WIDGET(pr)->window) return FALSE;
762 if (!pr->parent_window->window) return FALSE;
763 state = gdk_window_get_state(pr->parent_window->window);
764 if (state & GDK_WINDOW_STATE_MAXIMIZED) return FALSE;
769 static gint pr_parent_window_resize(PixbufRenderer *pr, gint w, gint h)
775 if (!pr_parent_window_sizable(pr)) return FALSE;
777 if (pr->window_limit)
779 gint sw = gdk_screen_width() * pr->window_limit_size / 100;
780 gint sh = gdk_screen_height() * pr->window_limit_size / 100;
786 widget = GTK_WIDGET(pr);
787 parent = GTK_WIDGET(pr->parent_window);
789 w += (parent->allocation.width - widget->allocation.width);
790 h += (parent->allocation.height - widget->allocation.height);
792 gdk_drawable_get_size(parent->window, &ww, &wh);
793 if (w == ww && h == wh) return FALSE;
795 gdk_window_resize(parent->window, w, h);
800 void pixbuf_renderer_set_parent(PixbufRenderer *pr, GtkWindow *window)
802 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
803 g_return_if_fail(window == NULL || GTK_IS_WINDOW(window));
805 pr->parent_window = GTK_WIDGET(window);
808 GtkWindow *pixbuf_renderer_get_parent(PixbufRenderer *pr)
810 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
812 return GTK_WINDOW(pr->parent_window);
817 *-------------------------------------------------------------------
819 *-------------------------------------------------------------------
822 static void pr_overlay_get_position(PixbufRenderer *pr, OverlayData *od,
823 gint *x, gint *y, gint *w, gint *h)
827 pw = gdk_pixbuf_get_width(od->pixbuf);
828 ph = gdk_pixbuf_get_height(od->pixbuf);
834 if (px < 0) px = pr->window_width - pw + px;
835 if (py < 0) py = pr->window_height - ph + py;
844 static void pr_overlay_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
850 box = GTK_WIDGET(pr);
852 work = pr->overlay_list;
862 pr_overlay_get_position(pr, od, &px, &py, &pw, &ph);
863 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
865 if (!pr->overlay_buffer)
867 pr->overlay_buffer = gdk_pixmap_new(((GtkWidget *)pr)->window, pr->tile_width, pr->tile_height, -1);
872 gdk_draw_drawable(pr->overlay_buffer, box->style->fg_gc[GTK_WIDGET_STATE(box)],
874 rx - (pr->x_offset + (it->x - pr->x_scroll)),
875 ry - (pr->y_offset + (it->y - pr->y_scroll)),
877 gdk_draw_pixbuf(pr->overlay_buffer,
878 box->style->fg_gc[GTK_WIDGET_STATE(box)],
882 pr->dither_quality, rx, ry);
883 gdk_draw_drawable(od->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
886 rx - px, ry - py, rw, rh);
890 /* no ImageTile means region may be larger than our scratch buffer */
893 for (sx = rx; sx < rx + rw; sx += pr->tile_width)
894 for(sy = ry; sy < ry + rh; sy += pr->tile_height)
898 sw = MIN(rx + rw - sx, pr->tile_width);
899 sh = MIN(ry + rh - sy, pr->tile_height);
901 gdk_draw_rectangle(pr->overlay_buffer,
902 box->style->bg_gc[GTK_WIDGET_STATE(box)], TRUE,
904 gdk_draw_pixbuf(pr->overlay_buffer,
905 box->style->fg_gc[GTK_WIDGET_STATE(box)],
909 pr->dither_quality, sx, sy);
910 gdk_draw_drawable(od->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
913 sx - px, sy - py, sw, sh);
920 static void pr_overlay_queue_draw(PixbufRenderer *pr, OverlayData *od)
924 pr_overlay_get_position(pr, od, &x, &y, &w, &h);
925 pr_queue(pr, pr->x_scroll - pr->x_offset + x,
926 pr->y_scroll - pr->y_offset + y,
928 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
930 pr_border_draw(pr, x, y, w, h);
933 static void pr_overlay_queue_all(PixbufRenderer *pr)
937 work = pr->overlay_list;
940 OverlayData *od = work->data;
943 pr_overlay_queue_draw(pr, od);
947 static void pr_overlay_update_sizes(PixbufRenderer *pr)
951 work = pr->overlay_list;
954 OverlayData *od = work->data;
957 if (od->relative && od->window)
961 pr_overlay_get_position(pr, od, &x, &y, &w, &h);
962 gdk_window_move_resize(od->window, x, y, w, h);
967 static OverlayData *pr_overlay_find(PixbufRenderer *pr, gint id)
971 work = pr->overlay_list;
974 OverlayData *od = work->data;
977 if (od->id == id) return od;
983 gint pixbuf_renderer_overlay_add(PixbufRenderer *pr, GdkPixbuf *pixbuf, gint x, gint y,
984 gint relative, gint always)
989 GdkWindowAttr attributes;
990 gint attributes_mask;
992 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
993 g_return_val_if_fail(pixbuf != NULL, -1);
996 while (pr_overlay_find(pr, id)) id++;
998 od = g_new0(OverlayData, 1);
1000 od->pixbuf = pixbuf;
1001 g_object_ref(G_OBJECT(od->pixbuf));
1004 od->relative = relative;
1005 od->always = always;
1007 pr_overlay_get_position(pr, od, &px, &py, &pw, &ph);
1009 attributes.window_type = GDK_WINDOW_CHILD;
1010 attributes.wclass = GDK_INPUT_OUTPUT;
1011 attributes.width = pw;
1012 attributes.height = ph;
1013 attributes.event_mask = GDK_EXPOSURE_MASK;
1014 attributes_mask = 0;
1016 od->window = gdk_window_new(GTK_WIDGET(pr)->window, &attributes, attributes_mask);
1017 gdk_window_set_user_data (od->window, pr);
1018 gdk_window_move(od->window, px, py);
1019 gdk_window_show(od->window);
1021 pr->overlay_list = g_list_append(pr->overlay_list, od);
1023 pr_overlay_queue_draw(pr, od);
1028 static void pr_overlay_free(PixbufRenderer *pr, OverlayData *od)
1030 pr->overlay_list = g_list_remove(pr->overlay_list, od);
1032 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
1033 if (od->window) gdk_window_destroy(od->window);
1036 if (!pr->overlay_list && pr->overlay_buffer)
1038 g_object_unref(pr->overlay_buffer);
1039 pr->overlay_buffer = NULL;
1043 static void pr_overlay_list_clear(PixbufRenderer *pr)
1045 while (pr->overlay_list)
1049 od = pr->overlay_list->data;
1050 pr_overlay_free(pr, od);
1054 void pixbuf_renderer_overlay_set(PixbufRenderer *pr, gint id, GdkPixbuf *pixbuf, gint x, gint y)
1058 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1060 od = pr_overlay_find(pr, id);
1065 gint px, py, pw, ph;
1067 g_object_ref(G_OBJECT(pixbuf));
1068 g_object_unref(G_OBJECT(od->pixbuf));
1069 od->pixbuf = pixbuf;
1074 pr_overlay_queue_draw(pr, od);
1075 pr_overlay_get_position(pr, od, &px, &py, &pw, &ph);
1076 gdk_window_move_resize(od->window, px, py, pw, ph);
1080 pr_overlay_queue_draw(pr, od);
1081 pr_overlay_free(pr, od);
1085 gint pixbuf_renderer_overlay_get(PixbufRenderer *pr, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
1089 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
1091 od = pr_overlay_find(pr, id);
1092 if (!od) return FALSE;
1094 if (pixbuf) *pixbuf = od->pixbuf;
1101 void pixbuf_renderer_overlay_remove(PixbufRenderer *pr, gint id)
1103 pixbuf_renderer_overlay_set(pr, id, NULL, 0, 0);
1107 *-------------------------------------------------------------------
1109 *-------------------------------------------------------------------
1113 static gboolean pr_scroller_update_cb(gpointer data)
1115 PixbufRenderer *pr = data;
1119 /* this was a simple scroll by difference between scroller and mouse position,
1120 * but all this math results in a smoother result and accounts for a dead zone.
1123 if (abs(pr->scroller_xpos - pr->scroller_x) < PR_SCROLLER_DEAD_ZONE)
1129 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1130 x = (pr->scroller_xpos - pr->scroller_x) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1131 x += (x > 0) ? -shift : shift;
1134 if (abs(pr->scroller_ypos - pr->scroller_y) < PR_SCROLLER_DEAD_ZONE)
1140 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1141 y = (pr->scroller_ypos - pr->scroller_y) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1142 y += (y > 0) ? -shift : shift;
1145 if (abs(x) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
1151 xinc = pr->scroller_xinc;
1155 if (xinc < 0) xinc = 0;
1156 if (x < xinc) xinc = x;
1157 if (x > xinc) xinc = MIN(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
1161 if (xinc > 0) xinc = 0;
1162 if (x > xinc) xinc = x;
1163 if (x < xinc) xinc = MAX(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
1167 if (abs(y) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
1173 yinc = pr->scroller_yinc;
1177 if (yinc < 0) yinc = 0;
1178 if (y < yinc) yinc = y;
1179 if (y > yinc) yinc = MIN(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
1183 if (yinc > 0) yinc = 0;
1184 if (y > yinc) yinc = y;
1185 if (y < yinc) yinc = MAX(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
1189 pr->scroller_xinc = xinc;
1190 pr->scroller_yinc = yinc;
1192 xinc = xinc / PR_SCROLLER_UPDATES_PER_SEC;
1193 yinc = yinc / PR_SCROLLER_UPDATES_PER_SEC;
1195 pixbuf_renderer_scroll(pr, xinc, yinc);
1200 static void pr_scroller_timer_set(PixbufRenderer *pr, gint start)
1202 if (pr->scroller_id != -1)
1204 g_source_remove(pr->scroller_id);
1205 pr->scroller_id = -1;
1210 pr->scroller_id = g_timeout_add(1000 / PR_SCROLLER_UPDATES_PER_SEC,
1211 pr_scroller_update_cb, pr);
1215 static void pr_scroller_start(PixbufRenderer *pr, gint x, gint y)
1217 if (pr->scroller_overlay == -1)
1223 pixbuf = pixbuf_inline(PIXBUF_INLINE_SCROLLER);
1225 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 32, 32);
1226 gdk_pixbuf_fill(pixbuf, 0x000000ff);
1228 w = gdk_pixbuf_get_width(pixbuf);
1229 h = gdk_pixbuf_get_height(pixbuf);
1231 pr->scroller_overlay = pixbuf_renderer_overlay_add(pr, pixbuf, x - w / 2, y - h / 2, FALSE, TRUE);
1232 g_object_unref(pixbuf);
1237 pr->scroller_xpos = x;
1238 pr->scroller_ypos = y;
1240 pr_scroller_timer_set(pr, TRUE);
1243 static void pr_scroller_stop(PixbufRenderer *pr)
1245 if (pr->scroller_id == -1) return;
1247 pixbuf_renderer_overlay_remove(pr, pr->scroller_overlay);
1248 pr->scroller_overlay = -1;
1250 pr_scroller_timer_set(pr, FALSE);
1254 *-------------------------------------------------------------------
1256 *-------------------------------------------------------------------
1259 static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
1262 gint rx, ry, rw, rh;
1264 box = GTK_WIDGET(pr);
1266 if (!box->window) return;
1268 if (!pr->pixbuf && !pr->source_tiles_enabled)
1270 if (pr_clip_region(x, y, w, h,
1272 pr->window_width, pr->window_height,
1273 &rx, &ry, &rw, &rh))
1275 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1276 pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1281 if (pr->vis_width < pr->window_width)
1283 if (pr->x_offset > 0 &&
1284 pr_clip_region(x, y, w, h,
1286 pr->x_offset, pr->window_height,
1287 &rx, &ry, &rw, &rh))
1289 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1290 pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1292 if (pr->window_width - pr->vis_width - pr->x_offset > 0 &&
1293 pr_clip_region(x, y, w, h,
1294 pr->x_offset + pr->vis_width, 0,
1295 pr->window_width - pr->vis_width - pr->x_offset, pr->window_height,
1296 &rx, &ry, &rw, &rh))
1298 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1299 pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1302 if (pr->vis_height < pr->window_height)
1304 if (pr->y_offset > 0 &&
1305 pr_clip_region(x, y, w, h,
1307 pr->vis_width, pr->y_offset,
1308 &rx, &ry, &rw, &rh))
1310 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1311 pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1313 if (pr->window_height - pr->vis_height - pr->y_offset > 0 &&
1314 pr_clip_region(x, y, w, h,
1315 pr->x_offset, pr->y_offset + pr->vis_height,
1316 pr->vis_width, pr->window_height - pr->vis_height - pr->y_offset,
1317 &rx, &ry, &rw, &rh))
1319 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1320 pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1325 static void pr_border_clear(PixbufRenderer *pr)
1327 pr_border_draw(pr, 0, 0, pr->window_width, pr->window_height);
1330 void pixbuf_renderer_set_color(PixbufRenderer *pr, GdkColor *color)
1335 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1337 widget = GTK_WIDGET(pr);
1342 style = gtk_style_copy(gtk_widget_get_style(widget));
1343 slot = &style->bg[GTK_STATE_NORMAL];
1345 slot->red = color->red;
1346 slot->green = color->green;
1347 slot->blue = color->blue;
1350 style = gtk_style_copy(gtk_widget_get_default_style());
1353 gtk_widget_set_style(widget, style);
1355 if (GTK_WIDGET_VISIBLE(widget)) pr_border_clear(pr);
1360 *-------------------------------------------------------------------
1362 *-------------------------------------------------------------------
1365 static void pr_source_tile_free(SourceTile *st)
1369 if (st->pixbuf) g_object_unref(st->pixbuf);
1373 static void pr_source_tile_free_all(PixbufRenderer *pr)
1377 work = pr->source_tiles;
1385 pr_source_tile_free(st);
1388 g_list_free(pr->source_tiles);
1389 pr->source_tiles = NULL;
1392 static void pr_source_tile_unset(PixbufRenderer *pr)
1394 pr_source_tile_free_all(pr);
1395 pr->source_tiles_enabled = FALSE;
1398 static gint pr_source_tile_visible(PixbufRenderer *pr, SourceTile *st)
1400 gint x1, y1, x2, y2;
1402 if (!st) return FALSE;
1404 x1 = (pr->x_scroll / pr->tile_width) * pr->tile_width;
1405 y1 = (pr->y_scroll / pr->tile_height) * pr->tile_height;
1406 x2 = ((pr->x_scroll + pr->vis_width) / pr->tile_width) * pr->tile_width + pr->tile_width;
1407 y2 = ((pr->y_scroll + pr->vis_height) / pr->tile_height) * pr->tile_height + pr->tile_height;
1409 return !((double)st->x * pr->scale > (double)x2 ||
1410 (double)(st->x + pr->source_tile_width) * pr->scale < (double)x1 ||
1411 (double)st->y * pr->scale > (double)y2 ||
1412 (double)(st->y + pr->source_tile_height) * pr->scale < (double)y1);
1415 static SourceTile *pr_source_tile_new(PixbufRenderer *pr, gint x, gint y)
1417 SourceTile *st = NULL;
1420 g_return_val_if_fail(pr->source_tile_width >= 1 && pr->source_tile_height >= 1, NULL);
1422 if (pr->source_tiles_cache_size < 4) pr->source_tiles_cache_size = 4;
1424 count = g_list_length(pr->source_tiles);
1425 if (count >= pr->source_tiles_cache_size)
1429 work = g_list_last(pr->source_tiles);
1430 while (work && count >= pr->source_tiles_cache_size)
1434 needle = work->data;
1437 if (!pr_source_tile_visible(pr, needle))
1439 pr->source_tiles = g_list_remove(pr->source_tiles, needle);
1441 if (pr->func_tile_dispose)
1443 pr->func_tile_dispose(pr, needle->x, needle->y,
1444 pr->source_tile_width, pr->source_tile_height,
1445 needle->pixbuf, pr->func_tile_data);
1454 pr_source_tile_free(needle);
1464 st = g_new0(SourceTile, 1);
1465 st->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
1466 pr->source_tile_width, pr->source_tile_height);
1469 st->x = (x / pr->source_tile_width) * pr->source_tile_width;
1470 st->y = (y / pr->source_tile_height) * pr->source_tile_height;
1473 pr->source_tiles = g_list_prepend(pr->source_tiles, st);
1478 static SourceTile *pr_source_tile_request(PixbufRenderer *pr, gint x, gint y)
1482 st = pr_source_tile_new(pr, x, y);
1483 if (!st) return NULL;
1485 if (pr->func_tile_request &&
1486 pr->func_tile_request(pr, st->x, st->y,
1487 pr->source_tile_width, pr->source_tile_height, st->pixbuf, pr->func_tile_data))
1492 pr_tile_invalidate_region(pr, st->x * pr->scale, st->y * pr->scale,
1493 pr->source_tile_width * pr->scale, pr->source_tile_height * pr->scale);
1498 static SourceTile *pr_source_tile_find(PixbufRenderer *pr, gint x, gint y)
1502 work = pr->source_tiles;
1505 SourceTile *st = work->data;
1507 if (x >= st->x && x < st->x + pr->source_tile_width &&
1508 y >= st->y && y < st->y + pr->source_tile_height)
1510 if (work != pr->source_tiles)
1512 pr->source_tiles = g_list_remove_link(pr->source_tiles, work);
1513 pr->source_tiles = g_list_concat(work, pr->source_tiles);
1524 static GList *pr_source_tile_compute_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h, gint request)
1532 if (w > pr->image_width) w = pr->image_width;
1533 if (h > pr->image_height) h = pr->image_height;
1535 sx = (x / pr->source_tile_width) * pr->source_tile_width;
1536 sy = (y / pr->source_tile_height) * pr->source_tile_height;
1538 for (x1 = sx; x1 < x + w; x1+= pr->source_tile_width)
1540 for (y1 = sy; y1 < y + h; y1 += pr->source_tile_height)
1544 st = pr_source_tile_find(pr, x1, y1);
1545 if (!st && request) st = pr_source_tile_request(pr, x1, y1);
1547 if (st) list = g_list_prepend(list, st);
1551 return g_list_reverse(list);
1554 static void pr_source_tile_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height)
1558 if (width < 1 || height < 1) return;
1560 work = pr->source_tiles;
1564 gint rx, ry, rw, rh;
1569 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1570 x, y, width, height,
1571 &rx, &ry, &rw, &rh))
1575 pixbuf = gdk_pixbuf_new_subpixbuf(st->pixbuf, rx - st->x, ry - st->y, rw, rh);
1576 if (pr->func_tile_request &&
1577 pr->func_tile_request(pr, rx, ry, rw, rh, pixbuf, pr->func_tile_data))
1579 pr_tile_invalidate_region(pr, rx * pr->scale, ry * pr->scale,
1580 rw * pr->scale, rh * pr->scale);
1582 g_object_unref(pixbuf);
1587 static gint pr_source_tile_render(PixbufRenderer *pr, ImageTile *it,
1588 gint x, gint y, gint w, gint h,
1589 gint new_data, gint fast)
1596 box = GTK_WIDGET(pr);
1598 if (pr->zoom == 1.0 || pr->scale == 1.0)
1600 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1605 gint rx, ry, rw, rh;
1610 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1611 it->x + x, it->y + y, w, h,
1612 &rx, &ry, &rw, &rh))
1616 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1617 rx - st->x, ry - st->y, rw, rh);
1619 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1621 gdk_draw_pixbuf(it->pixmap,
1622 box->style->fg_gc[GTK_WIDGET_STATE(box)],
1624 rx - st->x, ry - st->y,
1625 rx - it->x, ry - it->y,
1627 pr->dither_quality, rx, ry);
1634 double scale_x, scale_y;
1635 gint sx, sy, sw, sh;
1637 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1638 scale_x = (double)pr->width / pr->image_width;
1639 scale_y = (double)pr->height / pr->image_height;
1641 sx = (double)(it->x + x) / scale_x;
1642 sy = (double)(it->y + y) / scale_y;
1643 sw = (double)w / scale_x;
1644 sh = (double)h / scale_y;
1646 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1649 /* draws red over draw region, to check for leaks (regions not filled) */
1650 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1653 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1658 gint rx, ry, rw, rh;
1659 gint stx, sty, stw, sth;
1664 stx = floor((double)st->x * scale_x);
1665 sty = floor((double)st->y * scale_y);
1666 stw = ceil ((double)(st->x + pr->source_tile_width) * scale_x) - stx;
1667 sth = ceil ((double)(st->y + pr->source_tile_height) * scale_y) - sty;
1669 if (pr_clip_region(stx, sty, stw, sth,
1670 it->x + x, it->y + y, w, h,
1671 &rx, &ry, &rw, &rh))
1675 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1676 rx - st->x, ry - st->y, rw, rh);
1683 /* may need to use unfloored stx,sty values here */
1684 offset_x = (double)(stx - it->x);
1685 offset_y = (double)(sty - it->y);
1687 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1688 (double) 0.0 + offset_x,
1689 (double) 0.0 + offset_y,
1691 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1703 void pixbuf_renderer_set_tiles(PixbufRenderer *pr, gint width, gint height,
1704 gint tile_width, gint tile_height, gint cache_size,
1705 PixbufRendererTileRequestFunc func_request,
1706 PixbufRendererTileDisposeFunc func_dispose,
1710 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1711 g_return_if_fail(tile_width >= 32 && tile_width >= 32);
1712 g_return_if_fail(width >= 32 && height > 32);
1713 g_return_if_fail(func_request != NULL);
1715 if (pr->pixbuf) g_object_unref(pr->pixbuf);
1718 pr_source_tile_unset(pr);
1720 if (cache_size < 4) cache_size = 4;
1722 pr->source_tiles_enabled = TRUE;
1723 pr->source_tiles_cache_size = cache_size;
1724 pr->source_tile_width = tile_width;
1725 pr->source_tile_height = tile_height;
1727 pr->image_width = width;
1728 pr->image_height = height;
1730 pr->func_tile_request = func_request;
1731 pr->func_tile_dispose = func_dispose;
1732 pr->func_tile_data = user_data;
1734 pr_zoom_sync(pr, zoom, TRUE, TRUE, FALSE, 0, 0);
1735 pr_redraw(pr, TRUE);
1738 void pixbuf_renderer_set_tiles_size(PixbufRenderer *pr, gint width, gint height)
1740 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1741 g_return_if_fail(width >= 32 && height > 32);
1743 if (!pr->source_tiles_enabled) return;
1744 if (pr->image_width == width && pr->image_height == height) return;
1746 pr->image_width = width;
1747 pr->image_height = height;
1749 pr_zoom_sync(pr, pr->zoom, TRUE, FALSE, FALSE, 0, 0);
1752 gint pixbuf_renderer_get_tiles(PixbufRenderer *pr)
1754 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
1756 return pr->source_tiles_enabled;
1759 static void pr_zoom_adjust_real(PixbufRenderer *pr, gdouble increment,
1760 gint center_point, gint x, gint y)
1762 gdouble zoom = pr->zoom;
1764 if (increment == 0.0) return;
1768 if (pr->scale < 1.0)
1770 zoom = 0.0 - 1.0 / pr->scale;
1778 if (increment < 0.0)
1780 if (zoom >= 1.0 && zoom + increment < 1.0)
1782 zoom = zoom + increment - 2.0;
1786 zoom = zoom + increment;
1791 if (zoom <= -1.0 && zoom + increment > -1.0)
1793 zoom = zoom + increment + 2.0;
1797 zoom = zoom + increment;
1801 pr_zoom_sync(pr, zoom, FALSE, FALSE, center_point, x, y);
1805 *-------------------------------------------------------------------
1807 *-------------------------------------------------------------------
1810 static ImageTile *pr_tile_new(gint x, gint y, gint width, gint height)
1814 it = g_new0(ImageTile, 1);
1821 it->render_done = TILE_RENDER_NONE;
1826 static void pr_tile_free(ImageTile *it)
1830 if (it->pixbuf) gdk_pixbuf_unref(it->pixbuf);
1831 if (it->pixmap) g_object_unref(it->pixmap);
1836 static void pr_tile_free_all(PixbufRenderer *pr)
1851 g_list_free(pr->tiles);
1853 pr->tile_cache_size = 0;
1856 static ImageTile *pr_tile_add(PixbufRenderer *pr, gint x, gint y)
1860 it = pr_tile_new(x, y, pr->tile_width, pr->tile_height);
1862 if (it->x + it->w > pr->width) it->w = pr->width - it->x;
1863 if (it->y + it->h > pr->height) it->h = pr->height - it->y;
1865 pr->tiles = g_list_prepend(pr->tiles, it);
1866 pr->tile_cache_size += it->size;
1871 static void pr_tile_remove(PixbufRenderer *pr, ImageTile *it)
1875 QueueData *qd = it->qd;
1878 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
1884 QueueData *qd = it->qd2;
1887 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
1891 pr->tiles = g_list_remove(pr->tiles, it);
1892 pr->tile_cache_size -= it->size;
1897 static void pr_tile_free_space(PixbufRenderer *pr, guint space, ImageTile *it)
1902 work = g_list_last(pr->tiles);
1904 if (pr->source_tiles_enabled && pr->scale < 1.0)
1908 tiles = (pr->vis_width / pr->tile_width + 1) * (pr->vis_height / pr->tile_height + 1);
1909 tile_max = MAX(tiles * pr->tile_width * pr->tile_height * 3,
1910 (gint)((double)pr->tile_cache_max * 1048576.0 * pr->scale));
1914 tile_max = pr->tile_cache_max * 1048576;
1917 while (work && pr->tile_cache_size + space > tile_max)
1921 needle = work->data;
1924 ((!needle->qd && !needle->qd2) || !pr_tile_is_visible(pr, needle))) pr_tile_remove(pr, needle);
1928 static void pr_tile_invalidate_all(PixbufRenderer *pr)
1940 it->render_done = TILE_RENDER_NONE;
1941 it->render_todo = TILE_RENDER_ALL;
1944 it->w = MIN(pr->tile_width, pr->width - it->x);
1945 it->h = MIN(pr->tile_height, pr->height - it->y);
1949 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
1955 x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
1956 x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
1958 y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
1959 y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
1969 if (it->x < x2 && it->x + it->w > x1 &&
1970 it->y < y2 && it->y + it->h > y1)
1972 it->render_done = TILE_RENDER_NONE;
1973 it->render_todo = TILE_RENDER_ALL;
1978 static ImageTile *pr_tile_get(PixbufRenderer *pr, gint x, gint y, gint only_existing)
1988 if (it->x == x && it->y == y)
1990 pr->tiles = g_list_delete_link(pr->tiles, work);
1991 pr->tiles = g_list_prepend(pr->tiles, it);
1998 if (only_existing) return NULL;
2000 return pr_tile_add(pr, x, y);
2003 static void pr_tile_prepare(PixbufRenderer *pr, ImageTile *it)
2010 pixmap = gdk_pixmap_new(((GtkWidget *)pr)->window, pr->tile_width, pr->tile_height, -1);
2012 size = pixmap_calc_size(pixmap);
2013 pr_tile_free_space(pr, size, it);
2015 it->pixmap = pixmap;
2017 pr->tile_cache_size += size;
2020 if ((pr->zoom != 1.0 || pr->source_tiles_enabled || (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf)) ||
2021 pr->orientation != EXIF_ORIENTATION_TOP_LEFT || pr->func_post_process) && !it->pixbuf)
2026 /* I don't think that we need a pixbuf with alpha channel here */
2029 pixbuf = gdk_pixbuf_new(gdk_pixbuf_get_colorspace(pr->pixbuf),
2030 gdk_pixbuf_get_has_alpha(pr->pixbuf),
2031 gdk_pixbuf_get_bits_per_sample(pr->pixbuf),
2032 pr->tile_width, pr->tile_height);
2037 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, pr->tile_width, pr->tile_height);
2040 size = gdk_pixbuf_get_rowstride(pixbuf) * pr->tile_height;
2041 pr_tile_free_space(pr, size, it);
2043 it->pixbuf = pixbuf;
2045 pr->tile_cache_size += size;
2050 *-------------------------------------------------------------------
2052 *-------------------------------------------------------------------
2056 static void pr_tile_coords_map_orientation(PixbufRenderer *pr,
2057 double tile_x, double tile_y, /* coordinates of the tile */
2058 gint image_w, gint image_h,
2059 double tile_w, double tile_h,
2060 double *res_x, double *res_y)
2064 switch (pr->orientation)
2066 case EXIF_ORIENTATION_TOP_LEFT:
2067 /* normal -- nothing to do */
2069 case EXIF_ORIENTATION_TOP_RIGHT:
2071 *res_x = image_w - tile_x - tile_w;
2073 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2075 *res_x = image_w - tile_x - tile_w;
2076 *res_y = image_h - tile_y - tile_h;
2078 case EXIF_ORIENTATION_BOTTOM_LEFT:
2080 *res_y = image_h - tile_y - tile_h;
2082 case EXIF_ORIENTATION_LEFT_TOP:
2086 case EXIF_ORIENTATION_RIGHT_TOP:
2087 /* rotated -90 (270) */
2089 *res_y = image_w - tile_x - tile_w;
2091 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2092 *res_x = image_h - tile_y - tile_h;
2093 *res_y = image_w - tile_x - tile_w;
2095 case EXIF_ORIENTATION_LEFT_BOTTOM:
2097 *res_x = image_h - tile_y - tile_h;
2101 /* The other values are out of range */
2104 // printf("tile coord y:%f, ih:%d, th:%f ry:%f\n", tile_y, image_h, tile_h, *res_x);
2107 static void pr_tile_region_map_orientation(PixbufRenderer *pr,
2108 gint area_x, gint area_y, /* coordinates of the area inside tile */
2109 gint tile_w, gint tile_h,
2110 gint area_w, gint area_h,
2111 gint *res_x, gint *res_y,
2112 gint *res_w, gint *res_h)
2119 switch (pr->orientation)
2121 case EXIF_ORIENTATION_TOP_LEFT:
2122 /* normal -- nothing to do */
2124 case EXIF_ORIENTATION_TOP_RIGHT:
2126 *res_x = tile_w - area_x - area_w;
2128 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2130 *res_x = tile_w - area_x - area_w;
2131 *res_y = tile_h - area_y - area_h;
2133 case EXIF_ORIENTATION_BOTTOM_LEFT:
2135 *res_y = tile_h - area_y - area_h;
2137 case EXIF_ORIENTATION_LEFT_TOP:
2143 case EXIF_ORIENTATION_RIGHT_TOP:
2144 /* rotated -90 (270) */
2146 *res_y = tile_w - area_x - area_w;
2150 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2151 *res_x = tile_h - area_y - area_h;
2152 *res_y = tile_w - area_x - area_w;
2156 case EXIF_ORIENTATION_LEFT_BOTTOM:
2158 *res_x = tile_h - area_y - area_h;
2164 /* The other values are out of range */
2167 // printf("inside y:%d, th:%d, ah:%d ry:%d\n", area_y, tile_h, area_h, *res_x);
2170 static void pr_coords_map_orientation_reverse(PixbufRenderer *pr,
2171 gint area_x, gint area_y,
2172 gint tile_w, gint tile_h,
2173 gint area_w, gint area_h,
2174 gint *res_x, gint *res_y,
2175 gint *res_w, gint *res_h)
2182 switch (pr->orientation)
2184 case EXIF_ORIENTATION_TOP_LEFT:
2185 /* normal -- nothing to do */
2187 case EXIF_ORIENTATION_TOP_RIGHT:
2189 *res_x = tile_w - area_x - area_w;
2191 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2193 *res_x = tile_w - area_x - area_w;
2194 *res_y = tile_h - area_y - area_h;
2196 case EXIF_ORIENTATION_BOTTOM_LEFT:
2198 *res_y = tile_h - area_y - area_h;
2200 case EXIF_ORIENTATION_LEFT_TOP:
2206 case EXIF_ORIENTATION_RIGHT_TOP:
2207 /* rotated -90 (270) */
2208 *res_x = tile_w - area_y - area_h;
2213 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2214 *res_x = tile_w - area_y - area_h;
2215 *res_y = tile_h - area_x - area_w;
2219 case EXIF_ORIENTATION_LEFT_BOTTOM:
2222 *res_y = tile_h - area_x - area_w;
2227 /* The other values are out of range */
2233 static GdkPixbuf *pr_get_spare_tile(PixbufRenderer *pr)
2235 if (!pr->spare_tile) pr->spare_tile = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, pr->tile_width, pr->tile_height);
2236 return pr->spare_tile;
2239 #define COLOR_BYTES 3 /* rgb */
2241 static void pr_tile_rotate_90_clockwise(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2243 GdkPixbuf *src = *tile;
2246 guchar *s_pix, *d_pix;
2248 guchar *ip, *spi, *dpi;
2250 gint tw = pr->tile_width;
2252 srs = gdk_pixbuf_get_rowstride(src);
2253 s_pix = gdk_pixbuf_get_pixels(src);
2254 spi = s_pix + (x * COLOR_BYTES);
2256 dest = pr_get_spare_tile(pr);
2257 drs = gdk_pixbuf_get_rowstride(dest);
2258 d_pix = gdk_pixbuf_get_pixels(dest);
2259 dpi = d_pix + (tw - 1) * COLOR_BYTES;
2261 for (i = y; i < y + h; i++)
2263 sp = spi + (i * srs);
2264 ip = dpi - (i * COLOR_BYTES);
2265 for (j = x; j < x + w; j++)
2267 dp = ip + (j * drs);
2268 memcpy(dp, sp, COLOR_BYTES);
2273 pr->spare_tile = src;
2277 static void pr_tile_rotate_90_counter_clockwise(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2279 GdkPixbuf *src = *tile;
2282 guchar *s_pix, *d_pix;
2284 guchar *ip, *spi, *dpi;
2286 gint th = pr->tile_height;
2288 srs = gdk_pixbuf_get_rowstride(src);
2289 s_pix = gdk_pixbuf_get_pixels(src);
2290 spi = s_pix + (x * COLOR_BYTES);
2292 dest = pr_get_spare_tile(pr);
2293 drs = gdk_pixbuf_get_rowstride(dest);
2294 d_pix = gdk_pixbuf_get_pixels(dest);
2295 dpi = d_pix + (th - 1) * drs;
2297 for (i = y; i < y + h; i++)
2299 sp = spi + (i * srs);
2300 ip = dpi + (i * COLOR_BYTES);
2301 for (j = x; j < x + w; j++)
2303 dp = ip - (j * drs);
2304 memcpy(dp, sp, COLOR_BYTES);
2309 pr->spare_tile = src;
2313 static void pr_tile_mirror_only(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2315 GdkPixbuf *src = *tile;
2318 guchar *s_pix, *d_pix;
2323 gint tw = pr->tile_width;
2325 srs = gdk_pixbuf_get_rowstride(src);
2326 s_pix = gdk_pixbuf_get_pixels(src);
2327 spi = s_pix + (x * COLOR_BYTES);
2329 dest = pr_get_spare_tile(pr);
2330 drs = gdk_pixbuf_get_rowstride(dest);
2331 d_pix = gdk_pixbuf_get_pixels(dest);
2332 dpi = d_pix + (tw - x - 1) * COLOR_BYTES;
2334 for (i = y; i < y + h; i++)
2336 sp = spi + (i * srs);
2337 dp = dpi + (i * drs);
2338 for (j = 0; j < w; j++)
2340 memcpy(dp, sp, COLOR_BYTES);
2346 pr->spare_tile = src;
2350 static void pr_tile_mirror_and_flip(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2352 GdkPixbuf *src = *tile;
2355 guchar *s_pix, *d_pix;
2359 gint tw = pr->tile_width;
2360 gint th = pr->tile_height;
2362 srs = gdk_pixbuf_get_rowstride(src);
2363 s_pix = gdk_pixbuf_get_pixels(src);
2364 spi = s_pix + (x * COLOR_BYTES);
2366 dest = pr_get_spare_tile(pr);
2367 drs = gdk_pixbuf_get_rowstride(dest);
2368 d_pix = gdk_pixbuf_get_pixels(dest);
2369 dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
2371 for (i = y; i < y + h; i++)
2373 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
2374 dp = dpi - (i * drs) - (x * COLOR_BYTES);
2375 for (j = 0; j < w; j++)
2377 memcpy(dp, sp, COLOR_BYTES);
2383 pr->spare_tile = src;
2387 static void pr_tile_flip_only(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2389 GdkPixbuf *src = *tile;
2392 guchar *s_pix, *d_pix;
2396 gint th = pr->tile_height;
2398 srs = gdk_pixbuf_get_rowstride(src);
2399 s_pix = gdk_pixbuf_get_pixels(src);
2400 spi = s_pix + (x * COLOR_BYTES);
2402 dest = pr_get_spare_tile(pr);
2403 drs = gdk_pixbuf_get_rowstride(dest);
2404 d_pix = gdk_pixbuf_get_pixels(dest);
2405 dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
2407 for (i = y; i < y + h; i++)
2409 sp = spi + (i * srs);
2410 dp = dpi - (i * drs);
2411 memcpy(dp, sp, w * COLOR_BYTES);
2414 pr->spare_tile = src;
2418 static void pr_tile_apply_orientation(PixbufRenderer *pr, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
2420 switch (pr->orientation)
2422 case EXIF_ORIENTATION_TOP_LEFT:
2423 /* normal -- nothing to do */
2425 case EXIF_ORIENTATION_TOP_RIGHT:
2428 pr_tile_mirror_only(pr, pixbuf, x, y, w, h);
2431 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2434 pr_tile_mirror_and_flip(pr, pixbuf, x, y, w, h);
2437 case EXIF_ORIENTATION_BOTTOM_LEFT:
2440 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2443 case EXIF_ORIENTATION_LEFT_TOP:
2445 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2446 pr_tile_rotate_90_clockwise(pr, pixbuf, x, pr->tile_height - y - h, w, h);
2449 case EXIF_ORIENTATION_RIGHT_TOP:
2450 /* rotated -90 (270) */
2452 pr_tile_rotate_90_clockwise(pr, pixbuf, x, y, w, h);
2455 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2457 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2458 pr_tile_rotate_90_counter_clockwise(pr, pixbuf, x, pr->tile_height - y - h, w, h);
2461 case EXIF_ORIENTATION_LEFT_BOTTOM:
2464 pr_tile_rotate_90_counter_clockwise(pr, pixbuf, x, y, w, h);
2468 /* The other values are out of range */
2474 static void pr_tile_render(PixbufRenderer *pr, ImageTile *it,
2475 gint x, gint y, gint w, gint h,
2476 gint new_data, gint fast)
2482 if (it->render_todo == TILE_RENDER_NONE && it->pixmap && !new_data) return;
2484 if (it->render_done != TILE_RENDER_ALL)
2490 if (!fast) it->render_done = TILE_RENDER_ALL;
2492 else if (it->render_todo != TILE_RENDER_AREA)
2494 if (!fast) it->render_todo = TILE_RENDER_NONE;
2498 if (!fast) it->render_todo = TILE_RENDER_NONE;
2500 if (new_data) it->blank = FALSE;
2502 pr_tile_prepare(pr, it);
2503 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
2505 box = GTK_WIDGET(pr);
2507 /* FIXME checker colors for alpha should be configurable,
2508 * also should be drawn for blank = TRUE
2513 /* no data, do fast rect fill */
2514 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
2515 0, 0, it->w, it->h);
2517 else if (pr->source_tiles_enabled)
2519 draw = pr_source_tile_render(pr, it, x, y, w, h, new_data, fast);
2521 else if (pr->zoom == 1.0 || pr->scale == 1.0)
2524 double src_x, src_y;
2527 pr_tile_coords_map_orientation(pr, it->x, it->y,
2528 pr->image_width, pr->image_height,
2529 pr->tile_width, pr->tile_height,
2531 pr_tile_region_map_orientation(pr, x, y,
2532 pr->tile_width, pr->tile_height,
2539 gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2540 (double) 0.0 - src_x,
2541 (double) 0.0 - src_y,
2542 1.0, 1.0, GDK_INTERP_NEAREST,
2543 255, it->x + pb_x, it->y + pb_y,
2544 PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
2545 pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2552 if (pr->orientation == EXIF_ORIENTATION_TOP_LEFT && !(pr->func_post_process && !(pr->post_process_slow && fast)))
2554 /* faster, simple, base orientation, no postprocessing */
2555 gdk_draw_pixbuf(it->pixmap,
2556 box->style->fg_gc[GTK_WIDGET_STATE(box)],
2558 it->x + x, it->y + y,
2561 pr->dither_quality, it->x + x, it->y + y);
2565 gdk_pixbuf_copy_area(pr->pixbuf,
2566 src_x + pb_x, src_y + pb_y,
2570 pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2577 double scale_x, scale_y;
2578 double src_x, src_y;
2582 if (pr->image_width == 0 || pr->image_height == 0) return;
2584 scale_x = (double)pr->width / pr->image_width;
2585 scale_y = (double)pr->height / pr->image_height;
2587 pr_tile_coords_map_orientation(pr, it->x / scale_x, it->y /scale_y ,
2588 pr->image_width, pr->image_height,
2589 pr->tile_width / scale_x , pr->tile_height / scale_y,
2591 pr_tile_region_map_orientation(pr, x, y,
2592 pr->tile_width, pr->tile_height,
2597 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
2598 * small sizes for anything but GDK_INTERP_NEAREST
2600 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
2604 gdk_pixbuf_scale(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2605 (double) 0.0 - src_x * scale_x,
2606 (double) 0.0 - src_y * scale_y,
2608 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
2612 gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2613 (double) 0.0 - src_x * scale_x,
2614 (double) 0.0 - src_y * scale_y,
2616 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
2617 255, it->x + pb_x, it->y + pb_y,
2618 PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
2620 pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2624 if (draw && it->pixbuf && !it->blank)
2627 if (pr->func_post_process && !(pr->post_process_slow && fast))
2628 pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
2630 gdk_draw_pixbuf(it->pixmap,
2631 box->style->fg_gc[GTK_WIDGET_STATE(box)],
2636 pr->dither_quality, it->x + x, it->y + y);
2640 /* enable this line for debugging the edges of tiles */
2641 gdk_draw_rectangle(it->pixmap, box->style->white_gc,
2642 FALSE, 0, 0, it->w, it->h);
2643 gdk_draw_rectangle(it->pixmap, box->style->white_gc,
2649 static void pr_tile_expose(PixbufRenderer *pr, ImageTile *it,
2650 gint x, gint y, gint w, gint h,
2651 gint new_data, gint fast)
2655 pr_tile_render(pr, it, x, y, w, h, new_data, fast);
2657 box = GTK_WIDGET(pr);
2659 gdk_draw_drawable(box->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
2661 pr->x_offset + (it->x - pr->x_scroll) + x, pr->y_offset + (it->y - pr->y_scroll) + y, w, h);
2663 if (pr->overlay_list)
2665 pr_overlay_draw(pr, pr->x_offset + (it->x - pr->x_scroll) + x,
2666 pr->y_offset + (it->y - pr->y_scroll) + y,
2673 static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it)
2675 return (it->x + it->w >= pr->x_scroll && it->x < pr->x_scroll + pr->vis_width &&
2676 it->y + it->h >= pr->y_scroll && it->y < pr->y_scroll + pr->vis_height);
2680 *-------------------------------------------------------------------
2682 *-------------------------------------------------------------------
2685 static gint pr_queue_draw_idle_cb(gpointer data)
2687 PixbufRenderer *pr = data;
2691 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
2692 (!pr->draw_queue && !pr->draw_queue_2pass) ||
2693 pr->draw_idle_id == -1)
2695 pr_render_complete_signal(pr);
2697 pr->draw_idle_id = -1;
2703 qd = pr->draw_queue->data;
2704 fast = ((pr->zoom_2pass && pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow);
2710 /* still loading, wait till done (also drops the higher priority) */
2712 pr->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
2713 pr_queue_draw_idle_cb, pr, NULL);
2714 pr->draw_idle_high = FALSE;
2718 qd = pr->draw_queue_2pass->data;
2722 if (GTK_WIDGET_REALIZED(pr))
2724 if (pr_tile_is_visible(pr, qd->it))
2726 pr_tile_expose(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2728 else if (qd->new_data)
2730 /* if new pixel data, and we already have a pixmap, update the tile */
2731 qd->it->blank = FALSE;
2732 if (qd->it->pixmap && qd->it->render_done == TILE_RENDER_ALL)
2734 pr_tile_render(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2742 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
2747 pr_queue_merge(qd->it->qd2, qd);
2753 pr->draw_queue_2pass = g_list_append(pr->draw_queue_2pass, qd);
2764 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
2768 if (!pr->draw_queue && !pr->draw_queue_2pass)
2770 pr_render_complete_signal(pr);
2772 pr->draw_idle_id = -1;
2779 static void pr_queue_list_free(GList *list)
2799 static void pr_queue_clear(PixbufRenderer *pr)
2801 pr_queue_list_free(pr->draw_queue);
2802 pr->draw_queue = NULL;
2804 pr_queue_list_free(pr->draw_queue_2pass);
2805 pr->draw_queue_2pass = NULL;
2807 if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2808 pr->draw_idle_id = -1;
2811 static void pr_queue_merge(QueueData *parent, QueueData *qd)
2813 if (parent->x + parent->w < qd->x + qd->w)
2815 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
2817 if (parent->x > qd->x)
2819 parent->w += parent->x - qd->x;
2823 if (parent->y + parent->h < qd->y + qd->h)
2825 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
2827 if (parent->y > qd->y)
2829 parent->h += parent->y - qd->y;
2833 parent->new_data |= qd->new_data;
2836 static gint pr_clamp_to_visible(PixbufRenderer *pr, gint *x, gint *y, gint *w, gint *h)
2844 vh = pr->vis_height;
2849 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
2852 nx = CLAMP(*x, vx, vx + vw);
2853 nw = CLAMP(*w - (nx - *x), 1, vw);
2855 ny = CLAMP(*y, vy, vy + vh);
2856 nh = CLAMP(*h - (ny - *y), 1, vh);
2866 static gint pr_queue_to_tiles(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2867 gint clamp, ImageTileRenderType render, gint new_data, gint only_existing)
2873 if (clamp && !pr_clamp_to_visible(pr, &x, &y, &w, &h)) return FALSE;
2875 x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
2876 x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
2878 y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
2879 y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
2881 for (j = y1; j <= y2; j += pr->tile_height)
2883 for (i = x1; i <= x2; i += pr->tile_width)
2887 it = pr_tile_get(pr, i, j,
2889 (i + pr->tile_width < pr->x_scroll ||
2890 i > pr->x_scroll + pr->vis_width ||
2891 j + pr->tile_height < pr->y_scroll ||
2892 j > pr->y_scroll + pr->vis_height)));
2897 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
2898 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
2900 it->render_todo = render;
2903 qd = g_new(QueueData, 1);
2905 qd->new_data = new_data;
2915 qd->w = x + w - i - qd->x;
2916 if (qd->x + qd->w > pr->tile_width) qd->w = pr->tile_width - qd->x;
2926 qd->h = y + h - j - qd->y;
2927 if (qd->y + qd->h > pr->tile_height) qd->h = pr->tile_height - qd->y;
2929 if (qd->w < 1 || qd->h < 1)
2935 pr_queue_merge(it->qd, qd);
2941 pr->draw_queue = g_list_append(pr->draw_queue, qd);
2950 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2951 gint clamp, ImageTileRenderType render, gint new_data, gint only_existing)
2955 nx = CLAMP(x, 0, pr->width - 1);
2956 ny = CLAMP(y, 0, pr->height - 1);
2959 w = CLAMP(w, 0, pr->width - nx);
2960 h = CLAMP(h, 0, pr->height - ny);
2961 if (w < 1 || h < 1) return;
2963 if (pr_queue_to_tiles(pr, nx, ny, w, h, clamp, render, new_data, only_existing) &&
2964 ((!pr->draw_queue && !pr->draw_queue_2pass) || pr->draw_idle_id == -1 || !pr->draw_idle_high))
2966 if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2967 pr->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW,
2968 pr_queue_draw_idle_cb, pr, NULL);
2969 pr->draw_idle_high = TRUE;
2973 static void pr_redraw(PixbufRenderer *pr, gint new_data)
2976 pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, new_data, FALSE);
2980 *-------------------------------------------------------------------
2982 *-------------------------------------------------------------------
2985 static void pr_update_signal(PixbufRenderer *pr)
2988 printf("FIXME: send updated signal\n");
2990 if (debug) printf("%s pixbuf renderer updated - started drawing %p\n", get_exec_time(), pr);
2991 pr->debug_updated = TRUE;
2994 static void pr_zoom_signal(PixbufRenderer *pr)
2996 g_signal_emit(pr, signals[SIGNAL_ZOOM], 0, pr->zoom);
2999 static void pr_clicked_signal(PixbufRenderer *pr, GdkEventButton *bevent)
3001 g_signal_emit(pr, signals[SIGNAL_CLICKED], 0, bevent);
3004 static void pr_scroll_notify_signal(PixbufRenderer *pr)
3006 g_signal_emit(pr, signals[SIGNAL_SCROLL_NOTIFY], 0);
3009 static void pr_render_complete_signal(PixbufRenderer *pr)
3013 g_signal_emit(pr, signals[SIGNAL_RENDER_COMPLETE], 0);
3014 g_object_set(G_OBJECT(pr), "complete", TRUE, NULL);
3016 if (pr->debug_updated)
3018 if (debug) printf("%s pixbuf renderer done %p\n", get_exec_time(), pr);
3019 pr->debug_updated = FALSE;
3023 static void pr_drag_signal(PixbufRenderer *pr, GdkEventButton *bevent)
3025 g_signal_emit(pr, signals[SIGNAL_DRAG], 0, bevent);
3029 *-------------------------------------------------------------------
3031 *-------------------------------------------------------------------
3034 static gint pr_scroll_clamp(PixbufRenderer *pr)
3039 if (pr->zoom == 0.0)
3047 old_xs = pr->x_scroll;
3048 old_ys = pr->y_scroll;
3050 if (pr->x_offset > 0)
3056 pr->x_scroll = CLAMP(pr->x_scroll, 0, pr->width - pr->vis_width);
3059 if (pr->y_offset > 0)
3065 pr->y_scroll = CLAMP(pr->y_scroll, 0, pr->height - pr->vis_height);
3068 return (old_xs != pr->x_scroll || old_ys != pr->y_scroll);
3071 static gint pr_size_clamp(PixbufRenderer *pr)
3073 gint old_vw, old_vh;
3075 old_vw = pr->vis_width;
3076 old_vh = pr->vis_height;
3078 if (pr->width < pr->window_width)
3080 pr->vis_width = pr->width;
3081 pr->x_offset = (pr->window_width - pr->width) / 2;
3085 pr->vis_width = pr->window_width;
3089 if (pr->height < pr->window_height)
3091 pr->vis_height = pr->height;
3092 pr->y_offset = (pr->window_height - pr->height) / 2;
3096 pr->vis_height = pr->window_height;
3100 return (old_vw != pr->vis_width || old_vh != pr->vis_height);
3103 static gint pr_zoom_clamp(PixbufRenderer *pr, gdouble zoom,
3104 gint force, gint new, gint invalidate,
3111 zoom = CLAMP(zoom, pr->zoom_min, pr->zoom_max);
3113 if (pr->zoom == zoom && !force) return FALSE;
3115 w = pr->image_width;
3116 h = pr->image_height;
3118 if (zoom == 0.0 && !pr->pixbuf)
3122 else if (zoom == 0.0)
3128 sizeable = (new && pr_parent_window_sizable(pr));
3132 max_w = gdk_screen_width();
3133 max_h = gdk_screen_height();
3135 if (pr->window_limit)
3137 max_w = max_w * pr->window_limit_size / 100;
3138 max_h = max_h * pr->window_limit_size / 100;
3143 max_w = pr->window_width;
3144 max_h = pr->window_height;
3147 if ((pr->zoom_expand && !sizeable) || w > max_w || h > max_h)
3149 if ((gdouble)max_w / w > (gdouble)max_h / h)
3151 scale = (gdouble)max_h / h;
3153 w = w * scale + 0.5;
3154 if (w > max_w) w = max_w;
3158 scale = (gdouble)max_w / w;
3160 h = h * scale + 0.5;
3161 if (h > max_h) h = max_h;
3164 if (pr->autofit_limit)
3166 gdouble factor = (gdouble)pr->autofit_limit_size / 100;
3167 w = w * factor + 0.5;
3168 h = h * factor + 0.5;
3169 scale = scale * factor;
3180 else if (zoom > 0.0) /* zoom orig, in */
3188 scale = 1.0 / (0.0 - zoom);
3193 invalid = (pr->width != w || pr->height != h);
3200 if (invalidate || invalid)
3202 pr_tile_invalidate_all(pr);
3203 pr_redraw(pr, TRUE);
3205 if (redrawn) *redrawn = (invalidate || invalid);
3210 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
3211 gint force, gint new,
3212 gint center_point, gint px, gint py)
3215 gint old_cx, old_cy;
3218 gint redrawn = FALSE;
3220 old_scale = pr->scale;
3223 px = CLAMP(px, 0, pr->width);
3224 py = CLAMP(py, 0, pr->height);
3225 old_cx = pr->x_scroll + (px - pr->x_offset);
3226 old_cy = pr->y_scroll + (py - pr->y_offset);
3231 old_cx = pr->x_scroll + pr->vis_width / 2;
3232 old_cy = pr->y_scroll + pr->vis_height / 2;
3235 if (!pr_zoom_clamp(pr, zoom, force, new, force, &redrawn)) return;
3237 clamped = pr_size_clamp(pr);
3238 sized = pr_parent_window_resize(pr, pr->width, pr->height);
3242 switch (pr->scroll_reset)
3244 case PR_SCROLL_RESET_NOCHANGE:
3245 /* maintain old scroll position, do nothing */
3247 case PR_SCROLL_RESET_CENTER:
3248 /* center new image */
3249 pr->x_scroll = ((double)pr->image_width / 2.0 * pr->scale) - pr->vis_width / 2;
3250 pr->y_scroll = ((double)pr->image_height / 2.0 * pr->scale) - pr->vis_height / 2;
3252 case PR_SCROLL_RESET_TOPLEFT:
3254 /* reset to upper left */
3262 /* user zoom does not force, so keep visible center point */
3265 pr->x_scroll = old_cx / old_scale * pr->scale - (px - pr->x_offset);
3266 pr->y_scroll = old_cy / old_scale * pr->scale - (py - pr->y_offset);
3270 pr->x_scroll = old_cx / old_scale * pr->scale - (pr->vis_width / 2);
3271 pr->y_scroll = old_cy / old_scale * pr->scale - (pr->vis_height / 2);
3275 pr_scroll_clamp(pr);
3277 /* If the window was not sized, redraw the image - we know there will be no size/expose signal.
3278 * But even if a size is claimed, there is no guarantee that the window manager will allow it,
3279 * so redraw the window anyway :/
3281 if (sized || clamped) pr_border_clear(pr);
3282 pr_redraw(pr, redrawn);
3284 pr_scroll_notify_signal(pr);
3286 pr_update_signal(pr);
3289 static void pr_size_sync(PixbufRenderer *pr, gint new_width, gint new_height)
3291 gint zoom_changed = FALSE;
3293 if (pr->window_width == new_width && pr->window_height == new_height) return;
3295 pr->window_width = new_width;
3296 pr->window_height = new_height;
3298 if (pr->zoom == 0.0)
3300 gdouble old_scale = pr->scale;
3301 pr_zoom_clamp(pr, 0.0, TRUE, FALSE, FALSE, NULL);
3302 zoom_changed = (old_scale != pr->scale);
3306 pr_scroll_clamp(pr);
3308 pr_overlay_update_sizes(pr);
3310 /* ensure scroller remains visible */
3311 if (pr->scroller_overlay != -1)
3313 gint update = FALSE;
3315 if (pr->scroller_x > new_width)
3317 pr->scroller_x = new_width;
3318 pr->scroller_xpos = new_width;
3321 if (pr->scroller_y > new_height)
3323 pr->scroller_y = new_height;
3324 pr->scroller_ypos = new_height;
3332 if (pixbuf_renderer_overlay_get(pr, pr->scroller_overlay, &pixbuf, NULL, NULL))
3336 w = gdk_pixbuf_get_width(pixbuf);
3337 h = gdk_pixbuf_get_height(pixbuf);
3338 pixbuf_renderer_overlay_set(pr, pr->scroller_overlay, pixbuf,
3339 pr->scroller_x - w / 2, pr->scroller_y - h / 2);
3344 pr_border_clear(pr);
3346 pr_scroll_notify_signal(pr);
3347 if (zoom_changed) pr_zoom_signal(pr);
3348 pr_update_signal(pr);
3351 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
3353 PixbufRenderer *pr = data;
3355 pr_size_sync(pr, allocation->width, allocation->height);
3358 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area)
3362 pr_border_draw(pr, area->x, area->y, area->width, area->height);
3364 x = MAX(0, (gint)area->x - pr->x_offset + pr->x_scroll);
3365 y = MAX(0, (gint)area->y - pr->y_offset + pr->y_scroll);
3368 MIN((gint)area->width, pr->width - x),
3369 MIN((gint)area->height, pr->height - y),
3370 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
3374 *-------------------------------------------------------------------
3376 *-------------------------------------------------------------------
3379 void pixbuf_renderer_scroll(PixbufRenderer *pr, gint x, gint y)
3385 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3387 if (!pr->pixbuf && !pr->source_tiles_enabled) return;
3389 old_x = pr->x_scroll;
3390 old_y = pr->y_scroll;
3395 pr_scroll_clamp(pr);
3396 if (pr->x_scroll == old_x && pr->y_scroll == old_y) return;
3398 pr_scroll_notify_signal(pr);
3400 x_off = pr->x_scroll - old_x;
3401 y_off = pr->y_scroll - old_y;
3403 w = pr->vis_width - abs(x_off);
3404 h = pr->vis_height - abs(y_off);
3408 /* scrolled completely to new material */
3409 pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3442 box = GTK_WIDGET(pr);
3444 gc = gdk_gc_new(box->window);
3445 gdk_gc_set_exposures(gc, TRUE);
3446 gdk_draw_drawable(box->window, gc,
3448 x2 + pr->x_offset, y2 + pr->y_offset,
3449 x1 + pr->x_offset, y1 + pr->y_offset, w, h);
3452 if (pr->overlay_list)
3454 pr_overlay_queue_all(pr);
3457 w = pr->vis_width - w;
3458 h = pr->vis_height - h;
3463 x_off > 0 ? pr->x_scroll + (pr->vis_width - w) : pr->x_scroll, pr->y_scroll,
3464 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3468 /* FIXME, to optimize this, remove overlap */
3470 pr->x_scroll, y_off > 0 ? pr->y_scroll + (pr->vis_height - h) : pr->y_scroll,
3471 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3474 /* process exposures here, "expose_event" seems to miss a few with obstructed windows */
3475 while ((event = gdk_event_get_graphics_expose(box->window)) != NULL)
3477 pixbuf_renderer_paint(pr, &event->expose.area);
3479 if (event->expose.count == 0)
3481 gdk_event_free(event);
3484 gdk_event_free(event);
3489 void pixbuf_renderer_scroll_to_point(PixbufRenderer *pr, gint x, gint y,
3490 gdouble x_align, gdouble y_align)
3495 x_align = CLAMP(x_align, 0.0, 1.0);
3496 y_align = CLAMP(y_align, 0.0, 1.0);
3498 ax = (gdouble)pr->vis_width * x_align;
3499 ay = (gdouble)pr->vis_height * y_align;
3501 px = (gdouble)x * pr->scale - (pr->x_scroll + ax);
3502 py = (gdouble)y * pr->scale - (pr->y_scroll + ay);
3504 pixbuf_renderer_scroll(pr, px, py);
3507 /* get or set coordinates of viewport center in the image, in range 0.0 - 1.0 */
3509 void pixbuf_renderer_get_scroll_center(PixbufRenderer *pr, gdouble *x, gdouble *y)
3513 src_x = pr->x_scroll + pr->vis_width / 2;
3514 src_y = pr->y_scroll + pr->vis_height / 2;
3516 *x = (gdouble)src_x / pr->width;
3517 *y = (gdouble)src_y / pr->height;
3520 void pixbuf_renderer_set_scroll_center(PixbufRenderer *pr, gdouble x, gdouble y)
3522 gdouble dst_x, dst_y;
3524 dst_x = x * pr->width - pr->vis_width / 2 - pr->x_scroll + CLAMP(pr->subpixel_x_scroll, -1.0, 1.0);
3525 dst_y = y * pr->height - pr->vis_height / 2 - pr->y_scroll + CLAMP(pr->subpixel_y_scroll, -1.0, 1.0);
3527 pr->subpixel_x_scroll = dst_x - (int)dst_x;
3528 pr->subpixel_y_scroll = dst_y - (int)dst_y;
3530 pixbuf_renderer_scroll(pr, (int)dst_x, (int)dst_y);
3535 *-------------------------------------------------------------------
3537 *-------------------------------------------------------------------
3540 static gint pr_mouse_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3545 pr = PIXBUF_RENDERER(widget);
3547 if (pr->scroller_id != -1)
3549 pr->scroller_xpos = bevent->x;
3550 pr->scroller_ypos = bevent->y;
3553 if (!pr->in_drag || !gdk_pointer_is_grabbed()) return FALSE;
3555 if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
3561 widget_set_cursor(widget, GDK_FLEUR);
3564 if (bevent->state & GDK_SHIFT_MASK)
3566 accel = PR_PAN_SHIFT_MULTIPLIER;
3574 pixbuf_renderer_scroll(pr, (pr->drag_last_x - bevent->x) * accel,
3575 (pr->drag_last_y - bevent->y) * accel);
3577 pr_drag_signal(pr, bevent);
3579 pr->drag_last_x = bevent->x;
3580 pr->drag_last_y = bevent->y;
3585 static gint pr_mouse_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3590 pr = PIXBUF_RENDERER(widget);
3592 if (pr->scroller_id != -1) return TRUE;
3594 switch (bevent->button)
3596 case MOUSE_BUTTON_LEFT:
3598 pr->drag_last_x = bevent->x;
3599 pr->drag_last_y = bevent->y;
3601 gdk_pointer_grab(widget->window, FALSE,
3602 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
3603 NULL, NULL, bevent->time);
3604 gtk_grab_add(widget);
3606 case MOUSE_BUTTON_MIDDLE:
3609 case MOUSE_BUTTON_RIGHT:
3610 pr_clicked_signal(pr, bevent);
3616 parent = gtk_widget_get_parent(widget);
3617 if (widget && GTK_WIDGET_CAN_FOCUS(parent))
3619 gtk_widget_grab_focus(parent);
3625 static gint pr_mouse_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3629 pr = PIXBUF_RENDERER(widget);
3631 if (pr->scroller_id != -1)
3633 pr_scroller_stop(pr);
3637 if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB(pr))
3639 gtk_grab_remove(widget);
3640 gdk_pointer_ungrab(bevent->time);
3641 widget_set_cursor(widget, -1);
3644 if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
3646 if (bevent->button == MOUSE_BUTTON_LEFT && (bevent->state & GDK_SHIFT_MASK))
3648 pr_scroller_start(pr, bevent->x, bevent->y);
3650 else if (bevent->button == MOUSE_BUTTON_LEFT || bevent->button == MOUSE_BUTTON_MIDDLE)
3652 pr_clicked_signal(pr, bevent);
3656 pr->in_drag = FALSE;
3661 static gint pr_mouse_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
3665 pr = PIXBUF_RENDERER(widget);
3667 if (pr->scroller_id != -1)
3669 pr->scroller_xpos = pr->scroller_x;
3670 pr->scroller_ypos = pr->scroller_y;
3671 pr->scroller_xinc = 0;
3672 pr->scroller_yinc = 0;
3678 static void pr_mouse_drag_cb(GtkWidget *widget, GdkDragContext *context, gpointer data)
3682 pr = PIXBUF_RENDERER(widget);
3684 pr->drag_moved = PR_DRAG_SCROLL_THRESHHOLD;
3687 static void pr_signals_connect(PixbufRenderer *pr)
3689 g_signal_connect(G_OBJECT(pr), "motion_notify_event",
3690 G_CALLBACK(pr_mouse_motion_cb), pr);
3691 g_signal_connect(G_OBJECT(pr), "button_press_event",
3692 G_CALLBACK(pr_mouse_press_cb), pr);
3693 g_signal_connect(G_OBJECT(pr), "button_release_event",
3694 G_CALLBACK(pr_mouse_release_cb), pr);
3695 g_signal_connect(G_OBJECT(pr), "leave_notify_event",
3696 G_CALLBACK(pr_mouse_leave_cb), pr);
3698 gtk_widget_set_events(GTK_WIDGET(pr), GDK_POINTER_MOTION_MASK |
3699 GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
3700 GDK_LEAVE_NOTIFY_MASK);
3702 g_signal_connect(G_OBJECT(pr), "drag_begin",
3703 G_CALLBACK(pr_mouse_drag_cb), pr);
3708 *-------------------------------------------------------------------
3710 *-------------------------------------------------------------------
3712 static void pr_pixbuf_size_sync(PixbufRenderer *pr)
3714 if (!pr->pixbuf) return;
3715 switch (pr->orientation)
3717 case EXIF_ORIENTATION_LEFT_TOP:
3718 case EXIF_ORIENTATION_RIGHT_TOP:
3719 case EXIF_ORIENTATION_RIGHT_BOTTOM:
3720 case EXIF_ORIENTATION_LEFT_BOTTOM:
3721 pr->image_width = gdk_pixbuf_get_height(pr->pixbuf);
3722 pr->image_height = gdk_pixbuf_get_width(pr->pixbuf);
3725 pr->image_width = gdk_pixbuf_get_width(pr->pixbuf);
3726 pr->image_height = gdk_pixbuf_get_height(pr->pixbuf);
3730 static void pr_pixbuf_sync(PixbufRenderer *pr, gdouble zoom)
3736 /* no pixbuf so just clear the window */
3737 pr->image_width = 0;
3738 pr->image_height = 0;
3741 box = GTK_WIDGET(pr);
3743 if (GTK_WIDGET_REALIZED(box))
3745 gdk_window_clear(box->window);
3746 pr_overlay_draw(pr, 0, 0, pr->window_width, pr->window_height, NULL);
3749 pr_update_signal(pr);
3754 pr_pixbuf_size_sync(pr);
3755 pr_zoom_sync(pr, zoom, TRUE, TRUE, FALSE, 0, 0);
3758 static void pr_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
3760 if (pixbuf) g_object_ref(pixbuf);
3761 if (pr->pixbuf) g_object_unref(pr->pixbuf);
3762 pr->pixbuf = pixbuf;
3764 pr_pixbuf_sync(pr, zoom);
3767 void pixbuf_renderer_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
3769 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3771 pr_source_tile_unset(pr);
3773 pr_set_pixbuf(pr, pixbuf, zoom);
3775 pr_update_signal(pr);
3778 GdkPixbuf *pixbuf_renderer_get_pixbuf(PixbufRenderer *pr)
3780 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
3785 void pixbuf_renderer_set_orientation(PixbufRenderer *pr, gint orientation)
3787 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3789 pr->orientation = orientation;
3791 pr_pixbuf_size_sync(pr);
3792 pr_zoom_sync(pr, pr->zoom, TRUE, FALSE, FALSE, 0, 0);
3795 gint pixbuf_renderer_get_orientation(PixbufRenderer *pr)
3798 return pr->orientation;
3801 void pixbuf_renderer_set_post_process_func(PixbufRenderer *pr, PixbufRendererPostProcessFunc func, gpointer user_data, gint slow)
3803 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3805 pr->func_post_process = func;
3806 pr->post_process_user_data = user_data;
3807 pr->post_process_slow = func && slow;
3812 void pixbuf_renderer_move(PixbufRenderer *pr, PixbufRenderer *source)
3815 PixbufRendererScrollResetType scroll_reset;
3817 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3818 g_return_if_fail(IS_PIXBUF_RENDERER(source));
3820 if (pr == source) return;
3822 object = G_OBJECT(pr);
3824 g_object_set(object, "zoom_min", source->zoom_min, NULL);
3825 g_object_set(object, "zoom_max", source->zoom_max, NULL);
3826 g_object_set(object, "loading", source->loading, NULL);
3828 pr->complete = source->complete;
3830 pr->x_scroll = source->x_scroll;
3831 pr->y_scroll = source->y_scroll;
3833 scroll_reset = pr->scroll_reset;
3834 pr->scroll_reset = PR_SCROLL_RESET_NOCHANGE;
3836 pr->func_post_process = source->func_post_process;
3837 pr->post_process_user_data = source->post_process_user_data;
3838 pr->post_process_slow = source->post_process_slow;
3839 pr->orientation = source->orientation;
3841 if (source->source_tiles_enabled)
3843 pr_source_tile_unset(pr);
3845 pr->source_tiles_enabled = source->source_tiles_enabled;
3846 pr->source_tiles_cache_size = source->source_tiles_cache_size;
3847 pr->source_tile_width = source->source_tile_width;
3848 pr->source_tile_height = source->source_tile_height;
3849 pr->image_width = source->image_width;
3850 pr->image_height = source->image_height;
3852 pr->func_tile_request = source->func_tile_request;
3853 pr->func_tile_dispose = source->func_tile_dispose;
3854 pr->func_tile_data = source->func_tile_data;
3856 pr->source_tiles = source->source_tiles;
3857 source->source_tiles = NULL;
3859 pr_zoom_sync(pr, source->zoom, TRUE, TRUE, FALSE, 0, 0);
3860 pr_redraw(pr, TRUE);
3864 pixbuf_renderer_set_pixbuf(pr, source->pixbuf, source->zoom);
3867 pr->scroll_reset = scroll_reset;
3869 pixbuf_renderer_set_pixbuf(source, NULL, source->zoom);
3870 pr_queue_clear(source);
3871 pr_tile_free_all(source);
3874 void pixbuf_renderer_area_changed(PixbufRenderer *pr, gint src_x, gint src_y, gint src_w, gint src_h)
3876 gint x, y, width, height, x1, y1, x2, y2;
3878 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3880 pr_coords_map_orientation_reverse(pr,
3882 pr->image_width, pr->image_height,
3887 if (pr->source_tiles_enabled)
3889 pr_source_tile_changed(pr, x, y, width, height);
3892 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
3894 /* increase region when using a zoom quality that may access surrounding pixels */
3899 x1 = (gint)floor((double)x * pr->scale);
3900 y1 = (gint)floor((double)y * pr->scale);
3901 x2 = (gint)ceil((double)(x + width) * pr->scale);
3902 y2 = (gint)ceil((double)(y + height) * pr->scale);
3904 pr_queue(pr, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
3907 void pixbuf_renderer_zoom_adjust(PixbufRenderer *pr, gdouble increment)
3909 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3911 pr_zoom_adjust_real(pr, increment, FALSE, 0, 0);
3914 void pixbuf_renderer_zoom_adjust_at_point(PixbufRenderer *pr, gdouble increment, gint x, gint y)
3916 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3918 pr_zoom_adjust_real(pr, increment, TRUE, x, y);
3921 void pixbuf_renderer_zoom_set(PixbufRenderer *pr, gdouble zoom)
3923 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3925 pr_zoom_sync(pr, zoom, FALSE, FALSE, FALSE, 0, 0);
3928 gdouble pixbuf_renderer_zoom_get(PixbufRenderer *pr)
3930 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3935 gdouble pixbuf_renderer_zoom_get_scale(PixbufRenderer *pr)
3937 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3942 void pixbuf_renderer_zoom_set_limits(PixbufRenderer *pr, gdouble min, gdouble max)
3944 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3946 if (min > 1.0 || max < 1.0) return;
3947 if (min < 1.0 && min > -1.0) return;
3948 if (min < -200.0 || max > 200.0) return;
3950 if (pr->zoom_min != min)
3953 g_object_notify(G_OBJECT(pr), "zoom_min");
3955 if (pr->zoom_max != max)
3958 g_object_notify(G_OBJECT(pr), "zoom_max");
3962 gint pixbuf_renderer_get_image_size(PixbufRenderer *pr, gint *width, gint *height)
3964 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3965 g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3967 if (!pr->pixbuf && !pr->source_tiles_enabled)
3974 *width = pr->image_width;
3975 *height = pr->image_height;
3979 gint pixbuf_renderer_get_scaled_size(PixbufRenderer *pr, gint *width, gint *height)
3981 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3982 g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3984 if (!pr->pixbuf && !pr->source_tiles_enabled)
3992 *height = pr->height;
3996 gint pixbuf_renderer_get_visible_rect(PixbufRenderer *pr, GdkRectangle *rect)
3998 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3999 g_return_val_if_fail(rect != NULL, FALSE);
4001 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
4011 rect->x = (gint)((gdouble)pr->x_scroll / pr->scale);
4012 rect->y = (gint)((gdouble)pr->y_scroll / pr->scale);
4013 rect->width = (gint)((gdouble)pr->vis_width / pr->scale);
4014 rect->height = (gint)((gdouble)pr->vis_height / pr->scale);
4018 gint pixbuf_renderer_get_virtual_rect(PixbufRenderer *pr, GdkRectangle *rect)
4020 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
4021 g_return_val_if_fail(rect != NULL, FALSE);
4023 if ((!pr->pixbuf && !pr->source_tiles_enabled))
4032 rect->x = pr->x_scroll;
4033 rect->y = pr->y_scroll;
4034 rect->width = pr->vis_width;
4035 rect->height = pr->vis_height;