4 * Copyright (C) 2008 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
18 #include "pixbuf-renderer.h"
24 /* comment this out if not using this from within Geeqie
25 * defining GQ_BUILD does these things:
26 * - Sets the shift-click scroller pixbuf to a nice icon instead of a black box
32 #include "pixbuf_util.h"
38 /* size to use when breaking up image pane for rendering */
39 #define PR_TILE_SIZE 128
41 /* default size of tile cache (mb) */
42 #define PR_CACHE_SIZE_DEFAULT 8
44 /* default min and max zoom */
45 #define PR_ZOOM_MIN -32.0
46 #define PR_ZOOM_MAX 32.0
48 /* distance to drag mouse to disable image flip */
49 #define PR_DRAG_SCROLL_THRESHHOLD 4
51 /* increase pan rate when holding down shift */
52 #define PR_PAN_SHIFT_MULTIPLIER 6
55 #define PR_SCROLLER_UPDATES_PER_SEC 30
56 #define PR_SCROLLER_DEAD_ZONE 6
58 /* alpha channel checkerboard background (same as gimp) */
59 #define PR_ALPHA_CHECK1 0x00999999
60 #define PR_ALPHA_CHECK2 0x00666666
61 #define PR_ALPHA_CHECK_SIZE 16
63 /* when scaling image to below this size, use nearest pixel for scaling
64 * (below about 4, the other scale types become slow generating their conversion tables)
66 #define PR_MIN_SCALE_SIZE 8
69 EXIF_ORIENTATION_UNKNOWN = 0,
70 EXIF_ORIENTATION_TOP_LEFT = 1,
71 EXIF_ORIENTATION_TOP_RIGHT = 2,
72 EXIF_ORIENTATION_BOTTOM_RIGHT = 3,
73 EXIF_ORIENTATION_BOTTOM_LEFT = 4,
74 EXIF_ORIENTATION_LEFT_TOP = 5,
75 EXIF_ORIENTATION_RIGHT_TOP = 6,
76 EXIF_ORIENTATION_RIGHT_BOTTOM = 7,
77 EXIF_ORIENTATION_LEFT_BOTTOM = 8
78 } ExifOrientationType;
82 TILE_RENDER_NONE = 0, /* do nothing */
83 TILE_RENDER_AREA, /* render an area of the tile */
84 TILE_RENDER_ALL /* render the whole tile */
85 } ImageTileRenderType;
87 typedef struct _ImageTile ImageTile;
88 typedef struct _QueueData QueueData;
92 GdkPixmap *pixmap; /* off screen buffer */
93 GdkPixbuf *pixbuf; /* pixbuf area for zooming */
94 gint x; /* x offset into image */
95 gint y; /* y offset into image */
96 gint w; /* width that is visible (may be less if at edge of image) */
97 gint h; /* height '' */
101 /* render_todo: (explanation)
103 AREA render area of tile, usually only used when loading an image
104 note: will jump to an ALL if render_done is not ALL.
105 ALL render entire tile, if never done before w/ ALL, for expose events *only*
108 ImageTileRenderType render_todo; /* what to do (see above) */
109 ImageTileRenderType render_done; /* highest that has been done before on tile */
114 guint size; /* est. memory used by pixmap and pixbuf */
127 typedef struct _SourceTile SourceTile;
136 typedef struct _OverlayData OverlayData;
146 gint relative; /* x,y coordinates are relative, negative values start bottom right */
148 gint always; /* hide temporarily when scrolling (not yet implemented) */
154 SIGNAL_SCROLL_NOTIFY,
155 SIGNAL_RENDER_COMPLETE,
172 PROP_CACHE_SIZE_DISPLAY,
173 PROP_CACHE_SIZE_TILES,
176 PROP_WINDOW_LIMIT_VALUE,
178 PROP_AUTOFIT_LIMIT_VALUE
183 static guint signals[SIGNAL_COUNT] = { 0 };
184 static GtkEventBoxClass *parent_class = NULL;
188 static void pixbuf_renderer_class_init(PixbufRendererClass *class);
189 static void pixbuf_renderer_init(PixbufRenderer *pr);
190 static void pixbuf_renderer_finalize(GObject *object);
191 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
192 const GValue *value, GParamSpec *pspec);
193 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
194 GValue *value, GParamSpec *pspec);
195 static gint pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event);
197 static void pr_render_complete_signal(PixbufRenderer *pr);
199 static void pr_overlay_list_clear(PixbufRenderer *pr);
200 static void pr_scroller_timer_set(PixbufRenderer *pr, gint start);
201 static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h);
204 static void pr_source_tile_free_all(PixbufRenderer *pr);
205 static void pr_tile_free_all(PixbufRenderer *pr);
206 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h);
207 static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it);
208 static void pr_queue_clear(PixbufRenderer *pr);
209 static void pr_queue_merge(QueueData *parent, QueueData *qd);
210 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
211 gint clamp, ImageTileRenderType render, gint new_data, gint only_existing);
213 static void pr_redraw(PixbufRenderer *pr, gint new_data);
215 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
216 gint force, gint new,
217 gint center_point, gint px, gint py);
219 static void pr_signals_connect(PixbufRenderer *pr);
220 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data);
221 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area);
225 *-------------------------------------------------------------------
226 * Pixbuf Renderer object
227 *-------------------------------------------------------------------
230 GType pixbuf_renderer_get_type(void)
232 static GType pixbuf_renderer_type = 0;
234 if (!pixbuf_renderer_type)
236 static const GTypeInfo pixbuf_renderer_info =
238 sizeof(PixbufRendererClass),
239 NULL, /* base_init */
240 NULL, /* base_finalize */
241 (GClassInitFunc)pixbuf_renderer_class_init,
242 NULL, /* class_finalize */
243 NULL, /* class_data */
244 sizeof(PixbufRenderer),
246 (GInstanceInitFunc)pixbuf_renderer_init,
249 pixbuf_renderer_type = g_type_register_static(GTK_TYPE_EVENT_BOX, "PixbufRenderer",
250 &pixbuf_renderer_info, 0);
253 return pixbuf_renderer_type;
256 static void pixbuf_renderer_class_init(PixbufRendererClass *class)
258 GObjectClass *gobject_class = G_OBJECT_CLASS(class);
259 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);
261 parent_class = g_type_class_peek_parent(class);
263 gobject_class->set_property = pixbuf_renderer_set_property;
264 gobject_class->get_property = pixbuf_renderer_get_property;
266 gobject_class->finalize = pixbuf_renderer_finalize;
268 widget_class->expose_event = pixbuf_renderer_expose;
270 g_object_class_install_property(gobject_class,
272 g_param_spec_double("zoom_min",
278 G_PARAM_READABLE | G_PARAM_WRITABLE));
280 g_object_class_install_property(gobject_class,
282 g_param_spec_double("zoom_max",
288 G_PARAM_READABLE | G_PARAM_WRITABLE));
290 g_object_class_install_property(gobject_class,
292 g_param_spec_uint("zoom_quality",
298 G_PARAM_READABLE | G_PARAM_WRITABLE));
300 g_object_class_install_property(gobject_class,
302 g_param_spec_boolean("zoom_2pass",
306 G_PARAM_READABLE | G_PARAM_WRITABLE));
308 g_object_class_install_property(gobject_class,
310 g_param_spec_boolean("zoom_expand",
311 "Expand image in autozoom.",
314 G_PARAM_READABLE | G_PARAM_WRITABLE));
316 g_object_class_install_property(gobject_class,
318 g_param_spec_uint("dither_quality",
323 GDK_RGB_DITHER_NORMAL,
324 G_PARAM_READABLE | G_PARAM_WRITABLE));
326 g_object_class_install_property(gobject_class,
328 g_param_spec_uint("scroll_reset",
329 "New image scroll reset",
331 PR_SCROLL_RESET_TOPLEFT,
332 PR_SCROLL_RESET_NOCHANGE,
333 PR_SCROLL_RESET_TOPLEFT,
334 G_PARAM_READABLE | G_PARAM_WRITABLE));
336 g_object_class_install_property(gobject_class,
338 g_param_spec_boolean("delay_flip",
339 "Delay image update",
342 G_PARAM_READABLE | G_PARAM_WRITABLE));
344 g_object_class_install_property(gobject_class,
346 g_param_spec_boolean("loading",
347 "Image actively loading",
350 G_PARAM_READABLE | G_PARAM_WRITABLE));
352 g_object_class_install_property(gobject_class,
354 g_param_spec_boolean("complete",
355 "Image rendering complete",
358 G_PARAM_READABLE | G_PARAM_WRITABLE));
360 g_object_class_install_property(gobject_class,
361 PROP_CACHE_SIZE_DISPLAY,
362 g_param_spec_uint("cache_display",
363 "Display cache size MB",
367 PR_CACHE_SIZE_DEFAULT,
368 G_PARAM_READABLE | G_PARAM_WRITABLE));
370 g_object_class_install_property(gobject_class,
371 PROP_CACHE_SIZE_TILES,
372 g_param_spec_uint("cache_tiles",
374 "Number of tiles to retain in memory at any one time.",
377 PR_CACHE_SIZE_DEFAULT,
378 G_PARAM_READABLE | G_PARAM_WRITABLE));
380 g_object_class_install_property(gobject_class,
382 g_param_spec_boolean("window_fit",
383 "Fit window to image size",
386 G_PARAM_READABLE | G_PARAM_WRITABLE));
388 g_object_class_install_property(gobject_class,
390 g_param_spec_boolean("window_limit",
391 "Limit size of parent window",
394 G_PARAM_READABLE | G_PARAM_WRITABLE));
396 g_object_class_install_property(gobject_class,
397 PROP_WINDOW_LIMIT_VALUE,
398 g_param_spec_uint("window_limit_value",
399 "Size limit of parent window",
404 G_PARAM_READABLE | G_PARAM_WRITABLE));
406 g_object_class_install_property(gobject_class,
408 g_param_spec_boolean("autofit_limit",
409 "Limit size of image when autofitting",
412 G_PARAM_READABLE | G_PARAM_WRITABLE));
414 g_object_class_install_property(gobject_class,
415 PROP_AUTOFIT_LIMIT_VALUE,
416 g_param_spec_uint("autofit_limit_value",
417 "Size limit of image when autofitting",
422 G_PARAM_READABLE | G_PARAM_WRITABLE));
425 signals[SIGNAL_ZOOM] =
427 G_OBJECT_CLASS_TYPE(gobject_class),
429 G_STRUCT_OFFSET(PixbufRendererClass, zoom),
431 g_cclosure_marshal_VOID__DOUBLE,
435 signals[SIGNAL_CLICKED] =
436 g_signal_new("clicked",
437 G_OBJECT_CLASS_TYPE(gobject_class),
439 G_STRUCT_OFFSET(PixbufRendererClass, clicked),
441 g_cclosure_marshal_VOID__BOXED,
445 signals[SIGNAL_SCROLL_NOTIFY] =
446 g_signal_new("scroll-notify",
447 G_OBJECT_CLASS_TYPE(gobject_class),
449 G_STRUCT_OFFSET(PixbufRendererClass, scroll_notify),
451 g_cclosure_marshal_VOID__VOID,
454 signals[SIGNAL_RENDER_COMPLETE] =
455 g_signal_new("render-complete",
456 G_OBJECT_CLASS_TYPE(gobject_class),
458 G_STRUCT_OFFSET(PixbufRendererClass, render_complete),
460 g_cclosure_marshal_VOID__VOID,
463 signals[SIGNAL_DRAG] =
465 G_OBJECT_CLASS_TYPE(gobject_class),
467 G_STRUCT_OFFSET(PixbufRendererClass, drag),
469 g_cclosure_marshal_VOID__BOXED,
474 static void pixbuf_renderer_init(PixbufRenderer *pr)
478 box = GTK_WIDGET(pr);
480 pr->zoom_min = PR_ZOOM_MIN;
481 pr->zoom_max = PR_ZOOM_MAX;
482 pr->zoom_quality = GDK_INTERP_BILINEAR;
483 pr->zoom_2pass = FALSE;
488 pr->dither_quality = GDK_RGB_DITHER_NORMAL;
490 pr->scroll_reset = PR_SCROLL_RESET_TOPLEFT;
492 pr->draw_idle_id = -1;
494 pr->tile_width = PR_TILE_SIZE;
495 pr->tile_height = PR_TILE_SIZE;
498 pr->tile_cache_size = 0;
500 pr->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
502 pr->scroller_id = -1;
503 pr->scroller_overlay = -1;
505 pr->source_tiles_enabled = FALSE;
506 pr->source_tiles = NULL;
510 gtk_widget_set_double_buffered(box, FALSE);
511 g_signal_connect_after(G_OBJECT(box), "size_allocate",
512 G_CALLBACK(pr_size_cb), pr);
514 pr_signals_connect(pr);
517 static void pixbuf_renderer_finalize(GObject *object)
521 pr = PIXBUF_RENDERER(object);
524 pr_tile_free_all(pr);
526 if (pr->pixbuf) g_object_unref(pr->pixbuf);
527 if (pr->spare_tile) gdk_pixbuf_unref(pr->spare_tile);
529 pr_scroller_timer_set(pr, FALSE);
530 pr_overlay_list_clear(pr);
532 pr_source_tile_free_all(pr);
535 PixbufRenderer* pixbuf_renderer_new(void)
537 return g_object_new(TYPE_PIXBUF_RENDERER, NULL);
540 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
541 const GValue *value, GParamSpec *pspec)
545 pr = PIXBUF_RENDERER(object);
550 pr->zoom_min = g_value_get_double(value);
553 pr->zoom_max = g_value_get_double(value);
555 case PROP_ZOOM_QUALITY:
556 pr->zoom_quality = g_value_get_uint(value);
558 case PROP_ZOOM_2PASS:
559 pr->zoom_2pass = g_value_get_boolean(value);
561 case PROP_ZOOM_EXPAND:
562 pr->zoom_expand = g_value_get_boolean(value);
564 case PROP_DITHER_QUALITY:
565 pr->dither_quality = g_value_get_uint(value);
567 case PROP_SCROLL_RESET:
568 pr->scroll_reset = g_value_get_uint(value);
570 case PROP_DELAY_FLIP:
571 pr->delay_flip = g_value_get_boolean(value);
574 pr->loading = g_value_get_boolean(value);
577 pr->complete = g_value_get_boolean(value);
579 case PROP_CACHE_SIZE_DISPLAY:
580 pr->tile_cache_max = g_value_get_uint(value);
582 case PROP_CACHE_SIZE_TILES:
583 pr->source_tiles_cache_size = g_value_get_uint(value);
585 case PROP_WINDOW_FIT:
586 pr->window_fit = g_value_get_boolean(value);
588 case PROP_WINDOW_LIMIT:
589 pr->window_limit = g_value_get_boolean(value);
591 case PROP_WINDOW_LIMIT_VALUE:
592 pr->window_limit_size = g_value_get_uint(value);
594 case PROP_AUTOFIT_LIMIT:
595 pr->autofit_limit = g_value_get_boolean(value);
597 case PROP_AUTOFIT_LIMIT_VALUE:
598 pr->autofit_limit_size = g_value_get_uint(value);
601 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
606 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
607 GValue *value, GParamSpec *pspec)
611 pr = PIXBUF_RENDERER(object);
616 g_value_set_double(value, pr->zoom_min);
619 g_value_set_double(value, pr->zoom_max);
621 case PROP_ZOOM_QUALITY:
622 g_value_set_uint(value, pr->zoom_quality);
624 case PROP_ZOOM_2PASS:
625 g_value_set_boolean(value, pr->zoom_2pass);
627 case PROP_ZOOM_EXPAND:
628 g_value_set_boolean(value, pr->zoom_expand);
630 case PROP_DITHER_QUALITY:
631 g_value_set_uint(value, pr->dither_quality);
633 case PROP_SCROLL_RESET:
634 g_value_set_uint(value, pr->scroll_reset);
636 case PROP_DELAY_FLIP:
637 g_value_set_boolean(value, pr->delay_flip);
640 g_value_set_boolean(value, pr->loading);
643 g_value_set_boolean(value, pr->complete);
645 case PROP_CACHE_SIZE_DISPLAY:
646 g_value_set_uint(value, pr->tile_cache_max);
648 case PROP_CACHE_SIZE_TILES:
649 g_value_set_uint(value, pr->source_tiles_cache_size);
651 case PROP_WINDOW_FIT:
652 g_value_set_boolean(value, pr->window_fit);
654 case PROP_WINDOW_LIMIT:
655 g_value_set_boolean(value, pr->window_limit);
657 case PROP_WINDOW_LIMIT_VALUE:
658 g_value_set_uint(value, pr->window_limit_size);
660 case PROP_AUTOFIT_LIMIT:
661 g_value_set_boolean(value, pr->autofit_limit);
663 case PROP_AUTOFIT_LIMIT_VALUE:
664 g_value_set_uint(value, pr->autofit_limit_size);
667 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
672 static gint pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event)
674 if (GTK_WIDGET_DRAWABLE(widget))
676 if (!GTK_WIDGET_NO_WINDOW(widget))
678 if (event->window != widget->window)
682 gdk_window_get_position(event->window, &area.x, &area.y);
684 area.x += event->area.x;
685 area.y += event->area.y;
686 area.width = event->area.width;
687 area.height = event->area.height;
688 pixbuf_renderer_paint(PIXBUF_RENDERER(widget), &area);
692 pixbuf_renderer_paint(PIXBUF_RENDERER(widget), &event->area);
701 *-------------------------------------------------------------------
703 *-------------------------------------------------------------------
706 static void widget_set_cursor(GtkWidget *widget, gint icon)
710 if (!widget->window) return;
718 cursor = gdk_cursor_new (icon);
721 gdk_window_set_cursor(widget->window, cursor);
723 if (cursor) gdk_cursor_unref(cursor);
726 static gint pixmap_calc_size(GdkPixmap *pixmap)
730 d = gdk_drawable_get_depth(pixmap);
731 gdk_drawable_get_size(pixmap, &w, &h);
732 return w * h * (d / 8);
735 static gint pr_clip_region(gint x, gint y, gint w, gint h,
736 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
737 gint *rx, gint *ry, gint *rw, gint *rh)
739 if (clip_x + clip_w <= x ||
741 clip_y + clip_h <= y ||
747 *rx = MAX(x, clip_x);
748 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
750 *ry = MAX(y, clip_y);
751 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
756 static gint pr_parent_window_sizable(PixbufRenderer *pr)
758 GdkWindowState state;
760 if (!pr->parent_window) return FALSE;
761 if (!pr->window_fit) return FALSE;
762 if (!GTK_WIDGET(pr)->window) return FALSE;
764 if (!pr->parent_window->window) return FALSE;
765 state = gdk_window_get_state(pr->parent_window->window);
766 if (state & GDK_WINDOW_STATE_MAXIMIZED) return FALSE;
771 static gint pr_parent_window_resize(PixbufRenderer *pr, gint w, gint h)
777 if (!pr_parent_window_sizable(pr)) return FALSE;
779 if (pr->window_limit)
781 gint sw = gdk_screen_width() * pr->window_limit_size / 100;
782 gint sh = gdk_screen_height() * pr->window_limit_size / 100;
788 widget = GTK_WIDGET(pr);
789 parent = GTK_WIDGET(pr->parent_window);
791 w += (parent->allocation.width - widget->allocation.width);
792 h += (parent->allocation.height - widget->allocation.height);
794 gdk_drawable_get_size(parent->window, &ww, &wh);
795 if (w == ww && h == wh) return FALSE;
797 gdk_window_resize(parent->window, w, h);
802 void pixbuf_renderer_set_parent(PixbufRenderer *pr, GtkWindow *window)
804 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
805 g_return_if_fail(window == NULL || GTK_IS_WINDOW(window));
807 pr->parent_window = GTK_WIDGET(window);
810 GtkWindow *pixbuf_renderer_get_parent(PixbufRenderer *pr)
812 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
814 return GTK_WINDOW(pr->parent_window);
819 *-------------------------------------------------------------------
821 *-------------------------------------------------------------------
824 static void pr_overlay_get_position(PixbufRenderer *pr, OverlayData *od,
825 gint *x, gint *y, gint *w, gint *h)
829 pw = gdk_pixbuf_get_width(od->pixbuf);
830 ph = gdk_pixbuf_get_height(od->pixbuf);
836 if (px < 0) px = pr->window_width - pw + px;
837 if (py < 0) py = pr->window_height - ph + py;
846 static void pr_overlay_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
852 box = GTK_WIDGET(pr);
854 work = pr->overlay_list;
864 pr_overlay_get_position(pr, od, &px, &py, &pw, &ph);
865 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
867 if (!pr->overlay_buffer)
869 pr->overlay_buffer = gdk_pixmap_new(((GtkWidget *)pr)->window, pr->tile_width, pr->tile_height, -1);
874 gdk_draw_drawable(pr->overlay_buffer, box->style->fg_gc[GTK_WIDGET_STATE(box)],
876 rx - (pr->x_offset + (it->x - pr->x_scroll)),
877 ry - (pr->y_offset + (it->y - pr->y_scroll)),
879 gdk_draw_pixbuf(pr->overlay_buffer,
880 box->style->fg_gc[GTK_WIDGET_STATE(box)],
884 pr->dither_quality, rx, ry);
885 gdk_draw_drawable(od->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
888 rx - px, ry - py, rw, rh);
892 /* no ImageTile means region may be larger than our scratch buffer */
895 for (sx = rx; sx < rx + rw; sx += pr->tile_width)
896 for(sy = ry; sy < ry + rh; sy += pr->tile_height)
900 sw = MIN(rx + rw - sx, pr->tile_width);
901 sh = MIN(ry + rh - sy, pr->tile_height);
903 gdk_draw_rectangle(pr->overlay_buffer,
904 box->style->bg_gc[GTK_WIDGET_STATE(box)], TRUE,
906 gdk_draw_pixbuf(pr->overlay_buffer,
907 box->style->fg_gc[GTK_WIDGET_STATE(box)],
911 pr->dither_quality, sx, sy);
912 gdk_draw_drawable(od->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
915 sx - px, sy - py, sw, sh);
922 static void pr_overlay_queue_draw(PixbufRenderer *pr, OverlayData *od)
926 pr_overlay_get_position(pr, od, &x, &y, &w, &h);
927 pr_queue(pr, pr->x_scroll - pr->x_offset + x,
928 pr->y_scroll - pr->y_offset + y,
930 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
932 pr_border_draw(pr, x, y, w, h);
935 static void pr_overlay_queue_all(PixbufRenderer *pr)
939 work = pr->overlay_list;
942 OverlayData *od = work->data;
945 pr_overlay_queue_draw(pr, od);
949 static void pr_overlay_update_sizes(PixbufRenderer *pr)
953 work = pr->overlay_list;
956 OverlayData *od = work->data;
959 if (od->relative && od->window)
963 pr_overlay_get_position(pr, od, &x, &y, &w, &h);
964 gdk_window_move_resize(od->window, x, y, w, h);
969 static OverlayData *pr_overlay_find(PixbufRenderer *pr, gint id)
973 work = pr->overlay_list;
976 OverlayData *od = work->data;
979 if (od->id == id) return od;
985 gint pixbuf_renderer_overlay_add(PixbufRenderer *pr, GdkPixbuf *pixbuf, gint x, gint y,
986 gint relative, gint always)
991 GdkWindowAttr attributes;
992 gint attributes_mask;
994 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
995 g_return_val_if_fail(pixbuf != NULL, -1);
998 while (pr_overlay_find(pr, id)) id++;
1000 od = g_new0(OverlayData, 1);
1002 od->pixbuf = pixbuf;
1003 g_object_ref(G_OBJECT(od->pixbuf));
1006 od->relative = relative;
1007 od->always = always;
1009 pr_overlay_get_position(pr, od, &px, &py, &pw, &ph);
1011 attributes.window_type = GDK_WINDOW_CHILD;
1012 attributes.wclass = GDK_INPUT_OUTPUT;
1013 attributes.width = pw;
1014 attributes.height = ph;
1015 attributes.event_mask = GDK_EXPOSURE_MASK;
1016 attributes_mask = 0;
1018 od->window = gdk_window_new(GTK_WIDGET(pr)->window, &attributes, attributes_mask);
1019 gdk_window_set_user_data(od->window, pr);
1020 gdk_window_move(od->window, px, py);
1021 gdk_window_show(od->window);
1023 pr->overlay_list = g_list_append(pr->overlay_list, od);
1025 pr_overlay_queue_draw(pr, od);
1030 static void pr_overlay_free(PixbufRenderer *pr, OverlayData *od)
1032 pr->overlay_list = g_list_remove(pr->overlay_list, od);
1034 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
1035 if (od->window) gdk_window_destroy(od->window);
1038 if (!pr->overlay_list && pr->overlay_buffer)
1040 g_object_unref(pr->overlay_buffer);
1041 pr->overlay_buffer = NULL;
1045 static void pr_overlay_list_clear(PixbufRenderer *pr)
1047 while (pr->overlay_list)
1051 od = pr->overlay_list->data;
1052 pr_overlay_free(pr, od);
1056 void pixbuf_renderer_overlay_set(PixbufRenderer *pr, gint id, GdkPixbuf *pixbuf, gint x, gint y)
1060 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1062 od = pr_overlay_find(pr, id);
1067 gint px, py, pw, ph;
1069 g_object_ref(G_OBJECT(pixbuf));
1070 g_object_unref(G_OBJECT(od->pixbuf));
1071 od->pixbuf = pixbuf;
1076 pr_overlay_queue_draw(pr, od);
1077 pr_overlay_get_position(pr, od, &px, &py, &pw, &ph);
1078 gdk_window_move_resize(od->window, px, py, pw, ph);
1082 pr_overlay_queue_draw(pr, od);
1083 pr_overlay_free(pr, od);
1087 gint pixbuf_renderer_overlay_get(PixbufRenderer *pr, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
1091 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
1093 od = pr_overlay_find(pr, id);
1094 if (!od) return FALSE;
1096 if (pixbuf) *pixbuf = od->pixbuf;
1103 void pixbuf_renderer_overlay_remove(PixbufRenderer *pr, gint id)
1105 pixbuf_renderer_overlay_set(pr, id, NULL, 0, 0);
1109 *-------------------------------------------------------------------
1111 *-------------------------------------------------------------------
1115 static gboolean pr_scroller_update_cb(gpointer data)
1117 PixbufRenderer *pr = data;
1121 /* this was a simple scroll by difference between scroller and mouse position,
1122 * but all this math results in a smoother result and accounts for a dead zone.
1125 if (abs(pr->scroller_xpos - pr->scroller_x) < PR_SCROLLER_DEAD_ZONE)
1131 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1132 x = (pr->scroller_xpos - pr->scroller_x) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1133 x += (x > 0) ? -shift : shift;
1136 if (abs(pr->scroller_ypos - pr->scroller_y) < PR_SCROLLER_DEAD_ZONE)
1142 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1143 y = (pr->scroller_ypos - pr->scroller_y) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1144 y += (y > 0) ? -shift : shift;
1147 if (abs(x) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
1153 xinc = pr->scroller_xinc;
1157 if (xinc < 0) xinc = 0;
1158 if (x < xinc) xinc = x;
1159 if (x > xinc) xinc = MIN(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
1163 if (xinc > 0) xinc = 0;
1164 if (x > xinc) xinc = x;
1165 if (x < xinc) xinc = MAX(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
1169 if (abs(y) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
1175 yinc = pr->scroller_yinc;
1179 if (yinc < 0) yinc = 0;
1180 if (y < yinc) yinc = y;
1181 if (y > yinc) yinc = MIN(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
1185 if (yinc > 0) yinc = 0;
1186 if (y > yinc) yinc = y;
1187 if (y < yinc) yinc = MAX(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
1191 pr->scroller_xinc = xinc;
1192 pr->scroller_yinc = yinc;
1194 xinc = xinc / PR_SCROLLER_UPDATES_PER_SEC;
1195 yinc = yinc / PR_SCROLLER_UPDATES_PER_SEC;
1197 pixbuf_renderer_scroll(pr, xinc, yinc);
1202 static void pr_scroller_timer_set(PixbufRenderer *pr, gint start)
1204 if (pr->scroller_id != -1)
1206 g_source_remove(pr->scroller_id);
1207 pr->scroller_id = -1;
1212 pr->scroller_id = g_timeout_add(1000 / PR_SCROLLER_UPDATES_PER_SEC,
1213 pr_scroller_update_cb, pr);
1217 static void pr_scroller_start(PixbufRenderer *pr, gint x, gint y)
1219 if (pr->scroller_overlay == -1)
1225 pixbuf = pixbuf_inline(PIXBUF_INLINE_SCROLLER);
1227 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 32, 32);
1228 gdk_pixbuf_fill(pixbuf, 0x000000ff);
1230 w = gdk_pixbuf_get_width(pixbuf);
1231 h = gdk_pixbuf_get_height(pixbuf);
1233 pr->scroller_overlay = pixbuf_renderer_overlay_add(pr, pixbuf, x - w / 2, y - h / 2, FALSE, TRUE);
1234 g_object_unref(pixbuf);
1239 pr->scroller_xpos = x;
1240 pr->scroller_ypos = y;
1242 pr_scroller_timer_set(pr, TRUE);
1245 static void pr_scroller_stop(PixbufRenderer *pr)
1247 if (pr->scroller_id == -1) return;
1249 pixbuf_renderer_overlay_remove(pr, pr->scroller_overlay);
1250 pr->scroller_overlay = -1;
1252 pr_scroller_timer_set(pr, FALSE);
1256 *-------------------------------------------------------------------
1258 *-------------------------------------------------------------------
1261 static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
1264 gint rx, ry, rw, rh;
1266 box = GTK_WIDGET(pr);
1268 if (!box->window) return;
1270 if (!pr->pixbuf && !pr->source_tiles_enabled)
1272 if (pr_clip_region(x, y, w, h,
1274 pr->window_width, pr->window_height,
1275 &rx, &ry, &rw, &rh))
1277 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1278 pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1283 if (pr->vis_width < pr->window_width)
1285 if (pr->x_offset > 0 &&
1286 pr_clip_region(x, y, w, h,
1288 pr->x_offset, pr->window_height,
1289 &rx, &ry, &rw, &rh))
1291 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1292 pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1294 if (pr->window_width - pr->vis_width - pr->x_offset > 0 &&
1295 pr_clip_region(x, y, w, h,
1296 pr->x_offset + pr->vis_width, 0,
1297 pr->window_width - pr->vis_width - pr->x_offset, pr->window_height,
1298 &rx, &ry, &rw, &rh))
1300 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1301 pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1304 if (pr->vis_height < pr->window_height)
1306 if (pr->y_offset > 0 &&
1307 pr_clip_region(x, y, w, h,
1309 pr->vis_width, pr->y_offset,
1310 &rx, &ry, &rw, &rh))
1312 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1313 pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1315 if (pr->window_height - pr->vis_height - pr->y_offset > 0 &&
1316 pr_clip_region(x, y, w, h,
1317 pr->x_offset, pr->y_offset + pr->vis_height,
1318 pr->vis_width, pr->window_height - pr->vis_height - pr->y_offset,
1319 &rx, &ry, &rw, &rh))
1321 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1322 pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1327 static void pr_border_clear(PixbufRenderer *pr)
1329 pr_border_draw(pr, 0, 0, pr->window_width, pr->window_height);
1332 void pixbuf_renderer_set_color(PixbufRenderer *pr, GdkColor *color)
1337 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1339 widget = GTK_WIDGET(pr);
1344 style = gtk_style_copy(gtk_widget_get_style(widget));
1345 slot = &style->bg[GTK_STATE_NORMAL];
1347 slot->red = color->red;
1348 slot->green = color->green;
1349 slot->blue = color->blue;
1352 style = gtk_style_copy(gtk_widget_get_default_style());
1355 gtk_widget_set_style(widget, style);
1357 if (GTK_WIDGET_VISIBLE(widget)) pr_border_clear(pr);
1362 *-------------------------------------------------------------------
1364 *-------------------------------------------------------------------
1367 static void pr_source_tile_free(SourceTile *st)
1371 if (st->pixbuf) g_object_unref(st->pixbuf);
1375 static void pr_source_tile_free_all(PixbufRenderer *pr)
1379 work = pr->source_tiles;
1387 pr_source_tile_free(st);
1390 g_list_free(pr->source_tiles);
1391 pr->source_tiles = NULL;
1394 static void pr_source_tile_unset(PixbufRenderer *pr)
1396 pr_source_tile_free_all(pr);
1397 pr->source_tiles_enabled = FALSE;
1400 static gint pr_source_tile_visible(PixbufRenderer *pr, SourceTile *st)
1402 gint x1, y1, x2, y2;
1404 if (!st) return FALSE;
1406 x1 = (pr->x_scroll / pr->tile_width) * pr->tile_width;
1407 y1 = (pr->y_scroll / pr->tile_height) * pr->tile_height;
1408 x2 = ((pr->x_scroll + pr->vis_width) / pr->tile_width) * pr->tile_width + pr->tile_width;
1409 y2 = ((pr->y_scroll + pr->vis_height) / pr->tile_height) * pr->tile_height + pr->tile_height;
1411 return !((double)st->x * pr->scale > (double)x2 ||
1412 (double)(st->x + pr->source_tile_width) * pr->scale < (double)x1 ||
1413 (double)st->y * pr->scale > (double)y2 ||
1414 (double)(st->y + pr->source_tile_height) * pr->scale < (double)y1);
1417 static SourceTile *pr_source_tile_new(PixbufRenderer *pr, gint x, gint y)
1419 SourceTile *st = NULL;
1422 g_return_val_if_fail(pr->source_tile_width >= 1 && pr->source_tile_height >= 1, NULL);
1424 if (pr->source_tiles_cache_size < 4) pr->source_tiles_cache_size = 4;
1426 count = g_list_length(pr->source_tiles);
1427 if (count >= pr->source_tiles_cache_size)
1431 work = g_list_last(pr->source_tiles);
1432 while (work && count >= pr->source_tiles_cache_size)
1436 needle = work->data;
1439 if (!pr_source_tile_visible(pr, needle))
1441 pr->source_tiles = g_list_remove(pr->source_tiles, needle);
1443 if (pr->func_tile_dispose)
1445 pr->func_tile_dispose(pr, needle->x, needle->y,
1446 pr->source_tile_width, pr->source_tile_height,
1447 needle->pixbuf, pr->func_tile_data);
1456 pr_source_tile_free(needle);
1466 st = g_new0(SourceTile, 1);
1467 st->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
1468 pr->source_tile_width, pr->source_tile_height);
1471 st->x = (x / pr->source_tile_width) * pr->source_tile_width;
1472 st->y = (y / pr->source_tile_height) * pr->source_tile_height;
1475 pr->source_tiles = g_list_prepend(pr->source_tiles, st);
1480 static SourceTile *pr_source_tile_request(PixbufRenderer *pr, gint x, gint y)
1484 st = pr_source_tile_new(pr, x, y);
1485 if (!st) return NULL;
1487 if (pr->func_tile_request &&
1488 pr->func_tile_request(pr, st->x, st->y,
1489 pr->source_tile_width, pr->source_tile_height, st->pixbuf, pr->func_tile_data))
1494 pr_tile_invalidate_region(pr, st->x * pr->scale, st->y * pr->scale,
1495 pr->source_tile_width * pr->scale, pr->source_tile_height * pr->scale);
1500 static SourceTile *pr_source_tile_find(PixbufRenderer *pr, gint x, gint y)
1504 work = pr->source_tiles;
1507 SourceTile *st = work->data;
1509 if (x >= st->x && x < st->x + pr->source_tile_width &&
1510 y >= st->y && y < st->y + pr->source_tile_height)
1512 if (work != pr->source_tiles)
1514 pr->source_tiles = g_list_remove_link(pr->source_tiles, work);
1515 pr->source_tiles = g_list_concat(work, pr->source_tiles);
1526 static GList *pr_source_tile_compute_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h, gint request)
1534 if (w > pr->image_width) w = pr->image_width;
1535 if (h > pr->image_height) h = pr->image_height;
1537 sx = (x / pr->source_tile_width) * pr->source_tile_width;
1538 sy = (y / pr->source_tile_height) * pr->source_tile_height;
1540 for (x1 = sx; x1 < x + w; x1+= pr->source_tile_width)
1542 for (y1 = sy; y1 < y + h; y1 += pr->source_tile_height)
1546 st = pr_source_tile_find(pr, x1, y1);
1547 if (!st && request) st = pr_source_tile_request(pr, x1, y1);
1549 if (st) list = g_list_prepend(list, st);
1553 return g_list_reverse(list);
1556 static void pr_source_tile_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height)
1560 if (width < 1 || height < 1) return;
1562 work = pr->source_tiles;
1566 gint rx, ry, rw, rh;
1571 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1572 x, y, width, height,
1573 &rx, &ry, &rw, &rh))
1577 pixbuf = gdk_pixbuf_new_subpixbuf(st->pixbuf, rx - st->x, ry - st->y, rw, rh);
1578 if (pr->func_tile_request &&
1579 pr->func_tile_request(pr, rx, ry, rw, rh, pixbuf, pr->func_tile_data))
1581 pr_tile_invalidate_region(pr, rx * pr->scale, ry * pr->scale,
1582 rw * pr->scale, rh * pr->scale);
1584 g_object_unref(pixbuf);
1589 static gint pr_source_tile_render(PixbufRenderer *pr, ImageTile *it,
1590 gint x, gint y, gint w, gint h,
1591 gint new_data, gint fast)
1598 box = GTK_WIDGET(pr);
1600 if (pr->zoom == 1.0 || pr->scale == 1.0)
1602 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1607 gint rx, ry, rw, rh;
1612 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1613 it->x + x, it->y + y, w, h,
1614 &rx, &ry, &rw, &rh))
1618 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1619 rx - st->x, ry - st->y, rw, rh);
1621 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1623 gdk_draw_pixbuf(it->pixmap,
1624 box->style->fg_gc[GTK_WIDGET_STATE(box)],
1626 rx - st->x, ry - st->y,
1627 rx - it->x, ry - it->y,
1629 pr->dither_quality, rx, ry);
1636 double scale_x, scale_y;
1637 gint sx, sy, sw, sh;
1639 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1640 scale_x = (double)pr->width / pr->image_width;
1641 scale_y = (double)pr->height / pr->image_height;
1643 sx = (double)(it->x + x) / scale_x;
1644 sy = (double)(it->y + y) / scale_y;
1645 sw = (double)w / scale_x;
1646 sh = (double)h / scale_y;
1648 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1651 /* draws red over draw region, to check for leaks (regions not filled) */
1652 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1655 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1660 gint rx, ry, rw, rh;
1661 gint stx, sty, stw, sth;
1666 stx = floor((double)st->x * scale_x);
1667 sty = floor((double)st->y * scale_y);
1668 stw = ceil ((double)(st->x + pr->source_tile_width) * scale_x) - stx;
1669 sth = ceil ((double)(st->y + pr->source_tile_height) * scale_y) - sty;
1671 if (pr_clip_region(stx, sty, stw, sth,
1672 it->x + x, it->y + y, w, h,
1673 &rx, &ry, &rw, &rh))
1677 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1678 rx - st->x, ry - st->y, rw, rh);
1685 /* may need to use unfloored stx,sty values here */
1686 offset_x = (double)(stx - it->x);
1687 offset_y = (double)(sty - it->y);
1689 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1690 (double) 0.0 + offset_x,
1691 (double) 0.0 + offset_y,
1693 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1705 void pixbuf_renderer_set_tiles(PixbufRenderer *pr, gint width, gint height,
1706 gint tile_width, gint tile_height, gint cache_size,
1707 PixbufRendererTileRequestFunc func_request,
1708 PixbufRendererTileDisposeFunc func_dispose,
1712 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1713 g_return_if_fail(tile_width >= 32 && tile_width >= 32);
1714 g_return_if_fail(width >= 32 && height > 32);
1715 g_return_if_fail(func_request != NULL);
1717 if (pr->pixbuf) g_object_unref(pr->pixbuf);
1720 pr_source_tile_unset(pr);
1722 if (cache_size < 4) cache_size = 4;
1724 pr->source_tiles_enabled = TRUE;
1725 pr->source_tiles_cache_size = cache_size;
1726 pr->source_tile_width = tile_width;
1727 pr->source_tile_height = tile_height;
1729 pr->image_width = width;
1730 pr->image_height = height;
1732 pr->func_tile_request = func_request;
1733 pr->func_tile_dispose = func_dispose;
1734 pr->func_tile_data = user_data;
1736 pr_zoom_sync(pr, zoom, TRUE, TRUE, FALSE, 0, 0);
1737 pr_redraw(pr, TRUE);
1740 void pixbuf_renderer_set_tiles_size(PixbufRenderer *pr, gint width, gint height)
1742 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1743 g_return_if_fail(width >= 32 && height > 32);
1745 if (!pr->source_tiles_enabled) return;
1746 if (pr->image_width == width && pr->image_height == height) return;
1748 pr->image_width = width;
1749 pr->image_height = height;
1751 pr_zoom_sync(pr, pr->zoom, TRUE, FALSE, FALSE, 0, 0);
1754 gint pixbuf_renderer_get_tiles(PixbufRenderer *pr)
1756 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
1758 return pr->source_tiles_enabled;
1761 static void pr_zoom_adjust_real(PixbufRenderer *pr, gdouble increment,
1762 gint center_point, gint x, gint y)
1764 gdouble zoom = pr->zoom;
1766 if (increment == 0.0) return;
1770 if (pr->scale < 1.0)
1772 zoom = 0.0 - 1.0 / pr->scale;
1780 if (increment < 0.0)
1782 if (zoom >= 1.0 && zoom + increment < 1.0)
1784 zoom = zoom + increment - 2.0;
1788 zoom = zoom + increment;
1793 if (zoom <= -1.0 && zoom + increment > -1.0)
1795 zoom = zoom + increment + 2.0;
1799 zoom = zoom + increment;
1803 pr_zoom_sync(pr, zoom, FALSE, FALSE, center_point, x, y);
1807 *-------------------------------------------------------------------
1809 *-------------------------------------------------------------------
1812 static ImageTile *pr_tile_new(gint x, gint y, gint width, gint height)
1816 it = g_new0(ImageTile, 1);
1823 it->render_done = TILE_RENDER_NONE;
1828 static void pr_tile_free(ImageTile *it)
1832 if (it->pixbuf) gdk_pixbuf_unref(it->pixbuf);
1833 if (it->pixmap) g_object_unref(it->pixmap);
1838 static void pr_tile_free_all(PixbufRenderer *pr)
1853 g_list_free(pr->tiles);
1855 pr->tile_cache_size = 0;
1858 static ImageTile *pr_tile_add(PixbufRenderer *pr, gint x, gint y)
1862 it = pr_tile_new(x, y, pr->tile_width, pr->tile_height);
1864 if (it->x + it->w > pr->width) it->w = pr->width - it->x;
1865 if (it->y + it->h > pr->height) it->h = pr->height - it->y;
1867 pr->tiles = g_list_prepend(pr->tiles, it);
1868 pr->tile_cache_size += it->size;
1873 static void pr_tile_remove(PixbufRenderer *pr, ImageTile *it)
1877 QueueData *qd = it->qd;
1880 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
1886 QueueData *qd = it->qd2;
1889 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
1893 pr->tiles = g_list_remove(pr->tiles, it);
1894 pr->tile_cache_size -= it->size;
1899 static void pr_tile_free_space(PixbufRenderer *pr, guint space, ImageTile *it)
1904 work = g_list_last(pr->tiles);
1906 if (pr->source_tiles_enabled && pr->scale < 1.0)
1910 tiles = (pr->vis_width / pr->tile_width + 1) * (pr->vis_height / pr->tile_height + 1);
1911 tile_max = MAX(tiles * pr->tile_width * pr->tile_height * 3,
1912 (gint)((double)pr->tile_cache_max * 1048576.0 * pr->scale));
1916 tile_max = pr->tile_cache_max * 1048576;
1919 while (work && pr->tile_cache_size + space > tile_max)
1923 needle = work->data;
1926 ((!needle->qd && !needle->qd2) || !pr_tile_is_visible(pr, needle))) pr_tile_remove(pr, needle);
1930 static void pr_tile_invalidate_all(PixbufRenderer *pr)
1942 it->render_done = TILE_RENDER_NONE;
1943 it->render_todo = TILE_RENDER_ALL;
1946 it->w = MIN(pr->tile_width, pr->width - it->x);
1947 it->h = MIN(pr->tile_height, pr->height - it->y);
1951 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
1957 x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
1958 x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
1960 y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
1961 y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
1971 if (it->x < x2 && it->x + it->w > x1 &&
1972 it->y < y2 && it->y + it->h > y1)
1974 it->render_done = TILE_RENDER_NONE;
1975 it->render_todo = TILE_RENDER_ALL;
1980 static ImageTile *pr_tile_get(PixbufRenderer *pr, gint x, gint y, gint only_existing)
1990 if (it->x == x && it->y == y)
1992 pr->tiles = g_list_delete_link(pr->tiles, work);
1993 pr->tiles = g_list_prepend(pr->tiles, it);
2000 if (only_existing) return NULL;
2002 return pr_tile_add(pr, x, y);
2005 static void pr_tile_prepare(PixbufRenderer *pr, ImageTile *it)
2012 pixmap = gdk_pixmap_new(((GtkWidget *)pr)->window, pr->tile_width, pr->tile_height, -1);
2014 size = pixmap_calc_size(pixmap);
2015 pr_tile_free_space(pr, size, it);
2017 it->pixmap = pixmap;
2019 pr->tile_cache_size += size;
2022 if ((pr->zoom != 1.0 || pr->source_tiles_enabled || (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf)) ||
2023 pr->orientation != EXIF_ORIENTATION_TOP_LEFT || pr->func_post_process) && !it->pixbuf)
2028 /* I don't think that we need a pixbuf with alpha channel here */
2031 pixbuf = gdk_pixbuf_new(gdk_pixbuf_get_colorspace(pr->pixbuf),
2032 gdk_pixbuf_get_has_alpha(pr->pixbuf),
2033 gdk_pixbuf_get_bits_per_sample(pr->pixbuf),
2034 pr->tile_width, pr->tile_height);
2039 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, pr->tile_width, pr->tile_height);
2042 size = gdk_pixbuf_get_rowstride(pixbuf) * pr->tile_height;
2043 pr_tile_free_space(pr, size, it);
2045 it->pixbuf = pixbuf;
2047 pr->tile_cache_size += size;
2052 *-------------------------------------------------------------------
2054 *-------------------------------------------------------------------
2058 static void pr_tile_coords_map_orientation(PixbufRenderer *pr,
2059 double tile_x, double tile_y, /* coordinates of the tile */
2060 gint image_w, gint image_h,
2061 double tile_w, double tile_h,
2062 double *res_x, double *res_y)
2066 switch (pr->orientation)
2068 case EXIF_ORIENTATION_TOP_LEFT:
2069 /* normal -- nothing to do */
2071 case EXIF_ORIENTATION_TOP_RIGHT:
2073 *res_x = image_w - tile_x - tile_w;
2075 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2077 *res_x = image_w - tile_x - tile_w;
2078 *res_y = image_h - tile_y - tile_h;
2080 case EXIF_ORIENTATION_BOTTOM_LEFT:
2082 *res_y = image_h - tile_y - tile_h;
2084 case EXIF_ORIENTATION_LEFT_TOP:
2088 case EXIF_ORIENTATION_RIGHT_TOP:
2089 /* rotated -90 (270) */
2091 *res_y = image_w - tile_x - tile_w;
2093 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2094 *res_x = image_h - tile_y - tile_h;
2095 *res_y = image_w - tile_x - tile_w;
2097 case EXIF_ORIENTATION_LEFT_BOTTOM:
2099 *res_x = image_h - tile_y - tile_h;
2103 /* The other values are out of range */
2106 // printf("tile coord y:%f, ih:%d, th:%f ry:%f\n", tile_y, image_h, tile_h, *res_x);
2109 static void pr_tile_region_map_orientation(PixbufRenderer *pr,
2110 gint area_x, gint area_y, /* coordinates of the area inside tile */
2111 gint tile_w, gint tile_h,
2112 gint area_w, gint area_h,
2113 gint *res_x, gint *res_y,
2114 gint *res_w, gint *res_h)
2121 switch (pr->orientation)
2123 case EXIF_ORIENTATION_TOP_LEFT:
2124 /* normal -- nothing to do */
2126 case EXIF_ORIENTATION_TOP_RIGHT:
2128 *res_x = tile_w - area_x - area_w;
2130 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2132 *res_x = tile_w - area_x - area_w;
2133 *res_y = tile_h - area_y - area_h;
2135 case EXIF_ORIENTATION_BOTTOM_LEFT:
2137 *res_y = tile_h - area_y - area_h;
2139 case EXIF_ORIENTATION_LEFT_TOP:
2145 case EXIF_ORIENTATION_RIGHT_TOP:
2146 /* rotated -90 (270) */
2148 *res_y = tile_w - area_x - area_w;
2152 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2153 *res_x = tile_h - area_y - area_h;
2154 *res_y = tile_w - area_x - area_w;
2158 case EXIF_ORIENTATION_LEFT_BOTTOM:
2160 *res_x = tile_h - area_y - area_h;
2166 /* The other values are out of range */
2169 // printf("inside y:%d, th:%d, ah:%d ry:%d\n", area_y, tile_h, area_h, *res_x);
2172 static void pr_coords_map_orientation_reverse(PixbufRenderer *pr,
2173 gint area_x, gint area_y,
2174 gint tile_w, gint tile_h,
2175 gint area_w, gint area_h,
2176 gint *res_x, gint *res_y,
2177 gint *res_w, gint *res_h)
2184 switch (pr->orientation)
2186 case EXIF_ORIENTATION_TOP_LEFT:
2187 /* normal -- nothing to do */
2189 case EXIF_ORIENTATION_TOP_RIGHT:
2191 *res_x = tile_w - area_x - area_w;
2193 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2195 *res_x = tile_w - area_x - area_w;
2196 *res_y = tile_h - area_y - area_h;
2198 case EXIF_ORIENTATION_BOTTOM_LEFT:
2200 *res_y = tile_h - area_y - area_h;
2202 case EXIF_ORIENTATION_LEFT_TOP:
2208 case EXIF_ORIENTATION_RIGHT_TOP:
2209 /* rotated -90 (270) */
2210 *res_x = tile_w - area_y - area_h;
2215 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2216 *res_x = tile_w - area_y - area_h;
2217 *res_y = tile_h - area_x - area_w;
2221 case EXIF_ORIENTATION_LEFT_BOTTOM:
2224 *res_y = tile_h - area_x - area_w;
2229 /* The other values are out of range */
2235 static GdkPixbuf *pr_get_spare_tile(PixbufRenderer *pr)
2237 if (!pr->spare_tile) pr->spare_tile = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, pr->tile_width, pr->tile_height);
2238 return pr->spare_tile;
2241 #define COLOR_BYTES 3 /* rgb */
2243 static void pr_tile_rotate_90_clockwise(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2245 GdkPixbuf *src = *tile;
2248 guchar *s_pix, *d_pix;
2250 guchar *ip, *spi, *dpi;
2252 gint tw = pr->tile_width;
2254 srs = gdk_pixbuf_get_rowstride(src);
2255 s_pix = gdk_pixbuf_get_pixels(src);
2256 spi = s_pix + (x * COLOR_BYTES);
2258 dest = pr_get_spare_tile(pr);
2259 drs = gdk_pixbuf_get_rowstride(dest);
2260 d_pix = gdk_pixbuf_get_pixels(dest);
2261 dpi = d_pix + (tw - 1) * COLOR_BYTES;
2263 for (i = y; i < y + h; i++)
2265 sp = spi + (i * srs);
2266 ip = dpi - (i * COLOR_BYTES);
2267 for (j = x; j < x + w; j++)
2269 dp = ip + (j * drs);
2270 memcpy(dp, sp, COLOR_BYTES);
2275 pr->spare_tile = src;
2279 static void pr_tile_rotate_90_counter_clockwise(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2281 GdkPixbuf *src = *tile;
2284 guchar *s_pix, *d_pix;
2286 guchar *ip, *spi, *dpi;
2288 gint th = pr->tile_height;
2290 srs = gdk_pixbuf_get_rowstride(src);
2291 s_pix = gdk_pixbuf_get_pixels(src);
2292 spi = s_pix + (x * COLOR_BYTES);
2294 dest = pr_get_spare_tile(pr);
2295 drs = gdk_pixbuf_get_rowstride(dest);
2296 d_pix = gdk_pixbuf_get_pixels(dest);
2297 dpi = d_pix + (th - 1) * drs;
2299 for (i = y; i < y + h; i++)
2301 sp = spi + (i * srs);
2302 ip = dpi + (i * COLOR_BYTES);
2303 for (j = x; j < x + w; j++)
2305 dp = ip - (j * drs);
2306 memcpy(dp, sp, COLOR_BYTES);
2311 pr->spare_tile = src;
2315 static void pr_tile_mirror_only(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2317 GdkPixbuf *src = *tile;
2320 guchar *s_pix, *d_pix;
2325 gint tw = pr->tile_width;
2327 srs = gdk_pixbuf_get_rowstride(src);
2328 s_pix = gdk_pixbuf_get_pixels(src);
2329 spi = s_pix + (x * COLOR_BYTES);
2331 dest = pr_get_spare_tile(pr);
2332 drs = gdk_pixbuf_get_rowstride(dest);
2333 d_pix = gdk_pixbuf_get_pixels(dest);
2334 dpi = d_pix + (tw - x - 1) * COLOR_BYTES;
2336 for (i = y; i < y + h; i++)
2338 sp = spi + (i * srs);
2339 dp = dpi + (i * drs);
2340 for (j = 0; j < w; j++)
2342 memcpy(dp, sp, COLOR_BYTES);
2348 pr->spare_tile = src;
2352 static void pr_tile_mirror_and_flip(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2354 GdkPixbuf *src = *tile;
2357 guchar *s_pix, *d_pix;
2361 gint tw = pr->tile_width;
2362 gint th = pr->tile_height;
2364 srs = gdk_pixbuf_get_rowstride(src);
2365 s_pix = gdk_pixbuf_get_pixels(src);
2366 spi = s_pix + (x * COLOR_BYTES);
2368 dest = pr_get_spare_tile(pr);
2369 drs = gdk_pixbuf_get_rowstride(dest);
2370 d_pix = gdk_pixbuf_get_pixels(dest);
2371 dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
2373 for (i = y; i < y + h; i++)
2375 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
2376 dp = dpi - (i * drs) - (x * COLOR_BYTES);
2377 for (j = 0; j < w; j++)
2379 memcpy(dp, sp, COLOR_BYTES);
2385 pr->spare_tile = src;
2389 static void pr_tile_flip_only(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2391 GdkPixbuf *src = *tile;
2394 guchar *s_pix, *d_pix;
2398 gint th = pr->tile_height;
2400 srs = gdk_pixbuf_get_rowstride(src);
2401 s_pix = gdk_pixbuf_get_pixels(src);
2402 spi = s_pix + (x * COLOR_BYTES);
2404 dest = pr_get_spare_tile(pr);
2405 drs = gdk_pixbuf_get_rowstride(dest);
2406 d_pix = gdk_pixbuf_get_pixels(dest);
2407 dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
2409 for (i = y; i < y + h; i++)
2411 sp = spi + (i * srs);
2412 dp = dpi - (i * drs);
2413 memcpy(dp, sp, w * COLOR_BYTES);
2416 pr->spare_tile = src;
2420 static void pr_tile_apply_orientation(PixbufRenderer *pr, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
2422 switch (pr->orientation)
2424 case EXIF_ORIENTATION_TOP_LEFT:
2425 /* normal -- nothing to do */
2427 case EXIF_ORIENTATION_TOP_RIGHT:
2430 pr_tile_mirror_only(pr, pixbuf, x, y, w, h);
2433 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2436 pr_tile_mirror_and_flip(pr, pixbuf, x, y, w, h);
2439 case EXIF_ORIENTATION_BOTTOM_LEFT:
2442 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2445 case EXIF_ORIENTATION_LEFT_TOP:
2447 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2448 pr_tile_rotate_90_clockwise(pr, pixbuf, x, pr->tile_height - y - h, w, h);
2451 case EXIF_ORIENTATION_RIGHT_TOP:
2452 /* rotated -90 (270) */
2454 pr_tile_rotate_90_clockwise(pr, pixbuf, x, y, w, h);
2457 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2459 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2460 pr_tile_rotate_90_counter_clockwise(pr, pixbuf, x, pr->tile_height - y - h, w, h);
2463 case EXIF_ORIENTATION_LEFT_BOTTOM:
2466 pr_tile_rotate_90_counter_clockwise(pr, pixbuf, x, y, w, h);
2470 /* The other values are out of range */
2476 static void pr_tile_render(PixbufRenderer *pr, ImageTile *it,
2477 gint x, gint y, gint w, gint h,
2478 gint new_data, gint fast)
2484 if (it->render_todo == TILE_RENDER_NONE && it->pixmap && !new_data) return;
2486 if (it->render_done != TILE_RENDER_ALL)
2492 if (!fast) it->render_done = TILE_RENDER_ALL;
2494 else if (it->render_todo != TILE_RENDER_AREA)
2496 if (!fast) it->render_todo = TILE_RENDER_NONE;
2500 if (!fast) it->render_todo = TILE_RENDER_NONE;
2502 if (new_data) it->blank = FALSE;
2504 pr_tile_prepare(pr, it);
2505 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
2507 box = GTK_WIDGET(pr);
2509 /* FIXME checker colors for alpha should be configurable,
2510 * also should be drawn for blank = TRUE
2515 /* no data, do fast rect fill */
2516 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
2517 0, 0, it->w, it->h);
2519 else if (pr->source_tiles_enabled)
2521 draw = pr_source_tile_render(pr, it, x, y, w, h, new_data, fast);
2523 else if (pr->zoom == 1.0 || pr->scale == 1.0)
2526 double src_x, src_y;
2529 pr_tile_coords_map_orientation(pr, it->x, it->y,
2530 pr->image_width, pr->image_height,
2531 pr->tile_width, pr->tile_height,
2533 pr_tile_region_map_orientation(pr, x, y,
2534 pr->tile_width, pr->tile_height,
2541 gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2542 (double) 0.0 - src_x,
2543 (double) 0.0 - src_y,
2544 1.0, 1.0, GDK_INTERP_NEAREST,
2545 255, it->x + pb_x, it->y + pb_y,
2546 PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
2547 pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2554 if (pr->orientation == EXIF_ORIENTATION_TOP_LEFT && !(pr->func_post_process && !(pr->post_process_slow && fast)))
2556 /* faster, simple, base orientation, no postprocessing */
2557 gdk_draw_pixbuf(it->pixmap,
2558 box->style->fg_gc[GTK_WIDGET_STATE(box)],
2560 it->x + x, it->y + y,
2563 pr->dither_quality, it->x + x, it->y + y);
2567 gdk_pixbuf_copy_area(pr->pixbuf,
2568 src_x + pb_x, src_y + pb_y,
2572 pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2579 double scale_x, scale_y;
2580 double src_x, src_y;
2584 if (pr->image_width == 0 || pr->image_height == 0) return;
2586 scale_x = (double)pr->width / pr->image_width;
2587 scale_y = (double)pr->height / pr->image_height;
2589 pr_tile_coords_map_orientation(pr, it->x / scale_x, it->y /scale_y ,
2590 pr->image_width, pr->image_height,
2591 pr->tile_width / scale_x , pr->tile_height / scale_y,
2593 pr_tile_region_map_orientation(pr, x, y,
2594 pr->tile_width, pr->tile_height,
2599 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
2600 * small sizes for anything but GDK_INTERP_NEAREST
2602 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
2606 gdk_pixbuf_scale(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2607 (double) 0.0 - src_x * scale_x,
2608 (double) 0.0 - src_y * scale_y,
2610 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
2614 gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2615 (double) 0.0 - src_x * scale_x,
2616 (double) 0.0 - src_y * scale_y,
2618 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
2619 255, it->x + pb_x, it->y + pb_y,
2620 PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
2622 pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2626 if (draw && it->pixbuf && !it->blank)
2629 if (pr->func_post_process && !(pr->post_process_slow && fast))
2630 pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
2632 gdk_draw_pixbuf(it->pixmap,
2633 box->style->fg_gc[GTK_WIDGET_STATE(box)],
2638 pr->dither_quality, it->x + x, it->y + y);
2642 /* enable this line for debugging the edges of tiles */
2643 gdk_draw_rectangle(it->pixmap, box->style->white_gc,
2644 FALSE, 0, 0, it->w, it->h);
2645 gdk_draw_rectangle(it->pixmap, box->style->white_gc,
2651 static void pr_tile_expose(PixbufRenderer *pr, ImageTile *it,
2652 gint x, gint y, gint w, gint h,
2653 gint new_data, gint fast)
2657 pr_tile_render(pr, it, x, y, w, h, new_data, fast);
2659 box = GTK_WIDGET(pr);
2661 gdk_draw_drawable(box->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
2663 pr->x_offset + (it->x - pr->x_scroll) + x, pr->y_offset + (it->y - pr->y_scroll) + y, w, h);
2665 if (pr->overlay_list)
2667 pr_overlay_draw(pr, pr->x_offset + (it->x - pr->x_scroll) + x,
2668 pr->y_offset + (it->y - pr->y_scroll) + y,
2675 static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it)
2677 return (it->x + it->w >= pr->x_scroll && it->x < pr->x_scroll + pr->vis_width &&
2678 it->y + it->h >= pr->y_scroll && it->y < pr->y_scroll + pr->vis_height);
2682 *-------------------------------------------------------------------
2684 *-------------------------------------------------------------------
2687 static gint pr_queue_draw_idle_cb(gpointer data)
2689 PixbufRenderer *pr = data;
2693 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
2694 (!pr->draw_queue && !pr->draw_queue_2pass) ||
2695 pr->draw_idle_id == -1)
2697 pr_render_complete_signal(pr);
2699 pr->draw_idle_id = -1;
2705 qd = pr->draw_queue->data;
2706 fast = ((pr->zoom_2pass && pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow);
2712 /* still loading, wait till done (also drops the higher priority) */
2714 pr->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
2715 pr_queue_draw_idle_cb, pr, NULL);
2716 pr->draw_idle_high = FALSE;
2720 qd = pr->draw_queue_2pass->data;
2724 if (GTK_WIDGET_REALIZED(pr))
2726 if (pr_tile_is_visible(pr, qd->it))
2728 pr_tile_expose(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2730 else if (qd->new_data)
2732 /* if new pixel data, and we already have a pixmap, update the tile */
2733 qd->it->blank = FALSE;
2734 if (qd->it->pixmap && qd->it->render_done == TILE_RENDER_ALL)
2736 pr_tile_render(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2744 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
2749 pr_queue_merge(qd->it->qd2, qd);
2755 pr->draw_queue_2pass = g_list_append(pr->draw_queue_2pass, qd);
2766 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
2770 if (!pr->draw_queue && !pr->draw_queue_2pass)
2772 pr_render_complete_signal(pr);
2774 pr->draw_idle_id = -1;
2781 static void pr_queue_list_free(GList *list)
2801 static void pr_queue_clear(PixbufRenderer *pr)
2803 pr_queue_list_free(pr->draw_queue);
2804 pr->draw_queue = NULL;
2806 pr_queue_list_free(pr->draw_queue_2pass);
2807 pr->draw_queue_2pass = NULL;
2809 if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2810 pr->draw_idle_id = -1;
2813 static void pr_queue_merge(QueueData *parent, QueueData *qd)
2815 if (parent->x + parent->w < qd->x + qd->w)
2817 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
2819 if (parent->x > qd->x)
2821 parent->w += parent->x - qd->x;
2825 if (parent->y + parent->h < qd->y + qd->h)
2827 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
2829 if (parent->y > qd->y)
2831 parent->h += parent->y - qd->y;
2835 parent->new_data |= qd->new_data;
2838 static gint pr_clamp_to_visible(PixbufRenderer *pr, gint *x, gint *y, gint *w, gint *h)
2846 vh = pr->vis_height;
2851 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
2854 nx = CLAMP(*x, vx, vx + vw);
2855 nw = CLAMP(*w - (nx - *x), 1, vw);
2857 ny = CLAMP(*y, vy, vy + vh);
2858 nh = CLAMP(*h - (ny - *y), 1, vh);
2868 static gint pr_queue_to_tiles(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2869 gint clamp, ImageTileRenderType render, gint new_data, gint only_existing)
2875 if (clamp && !pr_clamp_to_visible(pr, &x, &y, &w, &h)) return FALSE;
2877 x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
2878 x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
2880 y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
2881 y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
2883 for (j = y1; j <= y2; j += pr->tile_height)
2885 for (i = x1; i <= x2; i += pr->tile_width)
2889 it = pr_tile_get(pr, i, j,
2891 (i + pr->tile_width < pr->x_scroll ||
2892 i > pr->x_scroll + pr->vis_width ||
2893 j + pr->tile_height < pr->y_scroll ||
2894 j > pr->y_scroll + pr->vis_height)));
2899 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
2900 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
2902 it->render_todo = render;
2905 qd = g_new(QueueData, 1);
2907 qd->new_data = new_data;
2917 qd->w = x + w - i - qd->x;
2918 if (qd->x + qd->w > pr->tile_width) qd->w = pr->tile_width - qd->x;
2928 qd->h = y + h - j - qd->y;
2929 if (qd->y + qd->h > pr->tile_height) qd->h = pr->tile_height - qd->y;
2931 if (qd->w < 1 || qd->h < 1)
2937 pr_queue_merge(it->qd, qd);
2943 pr->draw_queue = g_list_append(pr->draw_queue, qd);
2952 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2953 gint clamp, ImageTileRenderType render, gint new_data, gint only_existing)
2957 nx = CLAMP(x, 0, pr->width - 1);
2958 ny = CLAMP(y, 0, pr->height - 1);
2961 w = CLAMP(w, 0, pr->width - nx);
2962 h = CLAMP(h, 0, pr->height - ny);
2963 if (w < 1 || h < 1) return;
2965 if (pr_queue_to_tiles(pr, nx, ny, w, h, clamp, render, new_data, only_existing) &&
2966 ((!pr->draw_queue && !pr->draw_queue_2pass) || pr->draw_idle_id == -1 || !pr->draw_idle_high))
2968 if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2969 pr->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW,
2970 pr_queue_draw_idle_cb, pr, NULL);
2971 pr->draw_idle_high = TRUE;
2975 static void pr_redraw(PixbufRenderer *pr, gint new_data)
2978 pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, new_data, FALSE);
2982 *-------------------------------------------------------------------
2984 *-------------------------------------------------------------------
2987 static void pr_update_signal(PixbufRenderer *pr)
2990 printf("FIXME: send updated signal\n");
2992 DEBUG_1("%s pixbuf renderer updated - started drawing %p", get_exec_time(), pr);
2993 pr->debug_updated = TRUE;
2996 static void pr_zoom_signal(PixbufRenderer *pr)
2998 g_signal_emit(pr, signals[SIGNAL_ZOOM], 0, pr->zoom);
3001 static void pr_clicked_signal(PixbufRenderer *pr, GdkEventButton *bevent)
3003 g_signal_emit(pr, signals[SIGNAL_CLICKED], 0, bevent);
3006 static void pr_scroll_notify_signal(PixbufRenderer *pr)
3008 g_signal_emit(pr, signals[SIGNAL_SCROLL_NOTIFY], 0);
3011 static void pr_render_complete_signal(PixbufRenderer *pr)
3015 g_signal_emit(pr, signals[SIGNAL_RENDER_COMPLETE], 0);
3016 g_object_set(G_OBJECT(pr), "complete", TRUE, NULL);
3018 if (pr->debug_updated)
3020 DEBUG_1("%s pixbuf renderer done %p", get_exec_time(), pr);
3021 pr->debug_updated = FALSE;
3025 static void pr_drag_signal(PixbufRenderer *pr, GdkEventButton *bevent)
3027 g_signal_emit(pr, signals[SIGNAL_DRAG], 0, bevent);
3031 *-------------------------------------------------------------------
3033 *-------------------------------------------------------------------
3036 static gint pr_scroll_clamp(PixbufRenderer *pr)
3041 if (pr->zoom == 0.0)
3049 old_xs = pr->x_scroll;
3050 old_ys = pr->y_scroll;
3052 if (pr->x_offset > 0)
3058 pr->x_scroll = CLAMP(pr->x_scroll, 0, pr->width - pr->vis_width);
3061 if (pr->y_offset > 0)
3067 pr->y_scroll = CLAMP(pr->y_scroll, 0, pr->height - pr->vis_height);
3070 return (old_xs != pr->x_scroll || old_ys != pr->y_scroll);
3073 static gint pr_size_clamp(PixbufRenderer *pr)
3075 gint old_vw, old_vh;
3077 old_vw = pr->vis_width;
3078 old_vh = pr->vis_height;
3080 if (pr->width < pr->window_width)
3082 pr->vis_width = pr->width;
3083 pr->x_offset = (pr->window_width - pr->width) / 2;
3087 pr->vis_width = pr->window_width;
3091 if (pr->height < pr->window_height)
3093 pr->vis_height = pr->height;
3094 pr->y_offset = (pr->window_height - pr->height) / 2;
3098 pr->vis_height = pr->window_height;
3102 return (old_vw != pr->vis_width || old_vh != pr->vis_height);
3105 static gint pr_zoom_clamp(PixbufRenderer *pr, gdouble zoom,
3106 gint force, gint new, gint invalidate,
3113 zoom = CLAMP(zoom, pr->zoom_min, pr->zoom_max);
3115 if (pr->zoom == zoom && !force) return FALSE;
3117 w = pr->image_width;
3118 h = pr->image_height;
3120 if (zoom == 0.0 && !pr->pixbuf)
3124 else if (zoom == 0.0)
3130 sizeable = (new && pr_parent_window_sizable(pr));
3134 max_w = gdk_screen_width();
3135 max_h = gdk_screen_height();
3137 if (pr->window_limit)
3139 max_w = max_w * pr->window_limit_size / 100;
3140 max_h = max_h * pr->window_limit_size / 100;
3145 max_w = pr->window_width;
3146 max_h = pr->window_height;
3149 if ((pr->zoom_expand && !sizeable) || w > max_w || h > max_h)
3151 if ((gdouble)max_w / w > (gdouble)max_h / h)
3153 scale = (gdouble)max_h / h;
3155 w = w * scale + 0.5;
3156 if (w > max_w) w = max_w;
3160 scale = (gdouble)max_w / w;
3162 h = h * scale + 0.5;
3163 if (h > max_h) h = max_h;
3166 if (pr->autofit_limit)
3168 gdouble factor = (gdouble)pr->autofit_limit_size / 100;
3169 w = w * factor + 0.5;
3170 h = h * factor + 0.5;
3171 scale = scale * factor;
3182 else if (zoom > 0.0) /* zoom orig, in */
3190 scale = 1.0 / (0.0 - zoom);
3195 invalid = (pr->width != w || pr->height != h);
3202 if (invalidate || invalid)
3204 pr_tile_invalidate_all(pr);
3205 pr_redraw(pr, TRUE);
3207 if (redrawn) *redrawn = (invalidate || invalid);
3212 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
3213 gint force, gint new,
3214 gint center_point, gint px, gint py)
3217 gint old_cx, old_cy;
3220 gint redrawn = FALSE;
3222 old_scale = pr->scale;
3225 px = CLAMP(px, 0, pr->width);
3226 py = CLAMP(py, 0, pr->height);
3227 old_cx = pr->x_scroll + (px - pr->x_offset);
3228 old_cy = pr->y_scroll + (py - pr->y_offset);
3233 old_cx = pr->x_scroll + pr->vis_width / 2;
3234 old_cy = pr->y_scroll + pr->vis_height / 2;
3237 if (!pr_zoom_clamp(pr, zoom, force, new, force, &redrawn)) return;
3239 clamped = pr_size_clamp(pr);
3240 sized = pr_parent_window_resize(pr, pr->width, pr->height);
3244 switch (pr->scroll_reset)
3246 case PR_SCROLL_RESET_NOCHANGE:
3247 /* maintain old scroll position, do nothing */
3249 case PR_SCROLL_RESET_CENTER:
3250 /* center new image */
3251 pr->x_scroll = ((double)pr->image_width / 2.0 * pr->scale) - pr->vis_width / 2;
3252 pr->y_scroll = ((double)pr->image_height / 2.0 * pr->scale) - pr->vis_height / 2;
3254 case PR_SCROLL_RESET_TOPLEFT:
3256 /* reset to upper left */
3264 /* user zoom does not force, so keep visible center point */
3267 pr->x_scroll = old_cx / old_scale * pr->scale - (px - pr->x_offset);
3268 pr->y_scroll = old_cy / old_scale * pr->scale - (py - pr->y_offset);
3272 pr->x_scroll = old_cx / old_scale * pr->scale - (pr->vis_width / 2);
3273 pr->y_scroll = old_cy / old_scale * pr->scale - (pr->vis_height / 2);
3277 pr_scroll_clamp(pr);
3279 /* If the window was not sized, redraw the image - we know there will be no size/expose signal.
3280 * But even if a size is claimed, there is no guarantee that the window manager will allow it,
3281 * so redraw the window anyway :/
3283 if (sized || clamped) pr_border_clear(pr);
3284 pr_redraw(pr, redrawn);
3286 pr_scroll_notify_signal(pr);
3288 pr_update_signal(pr);
3291 static void pr_size_sync(PixbufRenderer *pr, gint new_width, gint new_height)
3293 gint zoom_changed = FALSE;
3295 if (pr->window_width == new_width && pr->window_height == new_height) return;
3297 pr->window_width = new_width;
3298 pr->window_height = new_height;
3300 if (pr->zoom == 0.0)
3302 gdouble old_scale = pr->scale;
3303 pr_zoom_clamp(pr, 0.0, TRUE, FALSE, FALSE, NULL);
3304 zoom_changed = (old_scale != pr->scale);
3308 pr_scroll_clamp(pr);
3310 pr_overlay_update_sizes(pr);
3312 /* ensure scroller remains visible */
3313 if (pr->scroller_overlay != -1)
3315 gint update = FALSE;
3317 if (pr->scroller_x > new_width)
3319 pr->scroller_x = new_width;
3320 pr->scroller_xpos = new_width;
3323 if (pr->scroller_y > new_height)
3325 pr->scroller_y = new_height;
3326 pr->scroller_ypos = new_height;
3334 if (pixbuf_renderer_overlay_get(pr, pr->scroller_overlay, &pixbuf, NULL, NULL))
3338 w = gdk_pixbuf_get_width(pixbuf);
3339 h = gdk_pixbuf_get_height(pixbuf);
3340 pixbuf_renderer_overlay_set(pr, pr->scroller_overlay, pixbuf,
3341 pr->scroller_x - w / 2, pr->scroller_y - h / 2);
3346 pr_border_clear(pr);
3348 pr_scroll_notify_signal(pr);
3349 if (zoom_changed) pr_zoom_signal(pr);
3350 pr_update_signal(pr);
3353 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
3355 PixbufRenderer *pr = data;
3357 pr_size_sync(pr, allocation->width, allocation->height);
3360 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area)
3364 pr_border_draw(pr, area->x, area->y, area->width, area->height);
3366 x = MAX(0, (gint)area->x - pr->x_offset + pr->x_scroll);
3367 y = MAX(0, (gint)area->y - pr->y_offset + pr->y_scroll);
3370 MIN((gint)area->width, pr->width - x),
3371 MIN((gint)area->height, pr->height - y),
3372 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
3376 *-------------------------------------------------------------------
3378 *-------------------------------------------------------------------
3381 void pixbuf_renderer_scroll(PixbufRenderer *pr, gint x, gint y)
3387 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3389 if (!pr->pixbuf && !pr->source_tiles_enabled) return;
3391 old_x = pr->x_scroll;
3392 old_y = pr->y_scroll;
3397 pr_scroll_clamp(pr);
3398 if (pr->x_scroll == old_x && pr->y_scroll == old_y) return;
3400 pr_scroll_notify_signal(pr);
3402 x_off = pr->x_scroll - old_x;
3403 y_off = pr->y_scroll - old_y;
3405 w = pr->vis_width - abs(x_off);
3406 h = pr->vis_height - abs(y_off);
3410 /* scrolled completely to new material */
3411 pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3444 box = GTK_WIDGET(pr);
3446 gc = gdk_gc_new(box->window);
3447 gdk_gc_set_exposures(gc, TRUE);
3448 gdk_draw_drawable(box->window, gc,
3450 x2 + pr->x_offset, y2 + pr->y_offset,
3451 x1 + pr->x_offset, y1 + pr->y_offset, w, h);
3454 if (pr->overlay_list)
3456 pr_overlay_queue_all(pr);
3459 w = pr->vis_width - w;
3460 h = pr->vis_height - h;
3465 x_off > 0 ? pr->x_scroll + (pr->vis_width - w) : pr->x_scroll, pr->y_scroll,
3466 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3470 /* FIXME, to optimize this, remove overlap */
3472 pr->x_scroll, y_off > 0 ? pr->y_scroll + (pr->vis_height - h) : pr->y_scroll,
3473 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3476 /* process exposures here, "expose_event" seems to miss a few with obstructed windows */
3477 while ((event = gdk_event_get_graphics_expose(box->window)) != NULL)
3479 pixbuf_renderer_paint(pr, &event->expose.area);
3481 if (event->expose.count == 0)
3483 gdk_event_free(event);
3486 gdk_event_free(event);
3491 void pixbuf_renderer_scroll_to_point(PixbufRenderer *pr, gint x, gint y,
3492 gdouble x_align, gdouble y_align)
3497 x_align = CLAMP(x_align, 0.0, 1.0);
3498 y_align = CLAMP(y_align, 0.0, 1.0);
3500 ax = (gdouble)pr->vis_width * x_align;
3501 ay = (gdouble)pr->vis_height * y_align;
3503 px = (gdouble)x * pr->scale - (pr->x_scroll + ax);
3504 py = (gdouble)y * pr->scale - (pr->y_scroll + ay);
3506 pixbuf_renderer_scroll(pr, px, py);
3509 /* get or set coordinates of viewport center in the image, in range 0.0 - 1.0 */
3511 void pixbuf_renderer_get_scroll_center(PixbufRenderer *pr, gdouble *x, gdouble *y)
3515 src_x = pr->x_scroll + pr->vis_width / 2;
3516 src_y = pr->y_scroll + pr->vis_height / 2;
3518 *x = (gdouble)src_x / pr->width;
3519 *y = (gdouble)src_y / pr->height;
3522 void pixbuf_renderer_set_scroll_center(PixbufRenderer *pr, gdouble x, gdouble y)
3524 gdouble dst_x, dst_y;
3526 dst_x = x * pr->width - pr->vis_width / 2 - pr->x_scroll + CLAMP(pr->subpixel_x_scroll, -1.0, 1.0);
3527 dst_y = y * pr->height - pr->vis_height / 2 - pr->y_scroll + CLAMP(pr->subpixel_y_scroll, -1.0, 1.0);
3529 pr->subpixel_x_scroll = dst_x - (int)dst_x;
3530 pr->subpixel_y_scroll = dst_y - (int)dst_y;
3532 pixbuf_renderer_scroll(pr, (int)dst_x, (int)dst_y);
3537 *-------------------------------------------------------------------
3539 *-------------------------------------------------------------------
3542 static gint pr_mouse_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3547 pr = PIXBUF_RENDERER(widget);
3549 if (pr->scroller_id != -1)
3551 pr->scroller_xpos = bevent->x;
3552 pr->scroller_ypos = bevent->y;
3555 if (!pr->in_drag || !gdk_pointer_is_grabbed()) return FALSE;
3557 if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
3563 widget_set_cursor(widget, GDK_FLEUR);
3566 if (bevent->state & GDK_SHIFT_MASK)
3568 accel = PR_PAN_SHIFT_MULTIPLIER;
3576 pixbuf_renderer_scroll(pr, (pr->drag_last_x - bevent->x) * accel,
3577 (pr->drag_last_y - bevent->y) * accel);
3579 pr_drag_signal(pr, bevent);
3581 pr->drag_last_x = bevent->x;
3582 pr->drag_last_y = bevent->y;
3587 static gint pr_mouse_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3592 pr = PIXBUF_RENDERER(widget);
3594 if (pr->scroller_id != -1) return TRUE;
3596 switch (bevent->button)
3598 case MOUSE_BUTTON_LEFT:
3600 pr->drag_last_x = bevent->x;
3601 pr->drag_last_y = bevent->y;
3603 gdk_pointer_grab(widget->window, FALSE,
3604 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
3605 NULL, NULL, bevent->time);
3606 gtk_grab_add(widget);
3608 case MOUSE_BUTTON_MIDDLE:
3611 case MOUSE_BUTTON_RIGHT:
3612 pr_clicked_signal(pr, bevent);
3618 parent = gtk_widget_get_parent(widget);
3619 if (widget && GTK_WIDGET_CAN_FOCUS(parent))
3621 gtk_widget_grab_focus(parent);
3627 static gint pr_mouse_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3631 pr = PIXBUF_RENDERER(widget);
3633 if (pr->scroller_id != -1)
3635 pr_scroller_stop(pr);
3639 if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB(pr))
3641 gtk_grab_remove(widget);
3642 gdk_pointer_ungrab(bevent->time);
3643 widget_set_cursor(widget, -1);
3646 if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
3648 if (bevent->button == MOUSE_BUTTON_LEFT && (bevent->state & GDK_SHIFT_MASK))
3650 pr_scroller_start(pr, bevent->x, bevent->y);
3652 else if (bevent->button == MOUSE_BUTTON_LEFT || bevent->button == MOUSE_BUTTON_MIDDLE)
3654 pr_clicked_signal(pr, bevent);
3658 pr->in_drag = FALSE;
3663 static gint pr_mouse_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
3667 pr = PIXBUF_RENDERER(widget);
3669 if (pr->scroller_id != -1)
3671 pr->scroller_xpos = pr->scroller_x;
3672 pr->scroller_ypos = pr->scroller_y;
3673 pr->scroller_xinc = 0;
3674 pr->scroller_yinc = 0;
3680 static void pr_mouse_drag_cb(GtkWidget *widget, GdkDragContext *context, gpointer data)
3684 pr = PIXBUF_RENDERER(widget);
3686 pr->drag_moved = PR_DRAG_SCROLL_THRESHHOLD;
3689 static void pr_signals_connect(PixbufRenderer *pr)
3691 g_signal_connect(G_OBJECT(pr), "motion_notify_event",
3692 G_CALLBACK(pr_mouse_motion_cb), pr);
3693 g_signal_connect(G_OBJECT(pr), "button_press_event",
3694 G_CALLBACK(pr_mouse_press_cb), pr);
3695 g_signal_connect(G_OBJECT(pr), "button_release_event",
3696 G_CALLBACK(pr_mouse_release_cb), pr);
3697 g_signal_connect(G_OBJECT(pr), "leave_notify_event",
3698 G_CALLBACK(pr_mouse_leave_cb), pr);
3700 gtk_widget_set_events(GTK_WIDGET(pr), GDK_POINTER_MOTION_MASK |
3701 GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
3702 GDK_LEAVE_NOTIFY_MASK);
3704 g_signal_connect(G_OBJECT(pr), "drag_begin",
3705 G_CALLBACK(pr_mouse_drag_cb), pr);
3710 *-------------------------------------------------------------------
3712 *-------------------------------------------------------------------
3714 static void pr_pixbuf_size_sync(PixbufRenderer *pr)
3716 if (!pr->pixbuf) return;
3717 switch (pr->orientation)
3719 case EXIF_ORIENTATION_LEFT_TOP:
3720 case EXIF_ORIENTATION_RIGHT_TOP:
3721 case EXIF_ORIENTATION_RIGHT_BOTTOM:
3722 case EXIF_ORIENTATION_LEFT_BOTTOM:
3723 pr->image_width = gdk_pixbuf_get_height(pr->pixbuf);
3724 pr->image_height = gdk_pixbuf_get_width(pr->pixbuf);
3727 pr->image_width = gdk_pixbuf_get_width(pr->pixbuf);
3728 pr->image_height = gdk_pixbuf_get_height(pr->pixbuf);
3732 static void pr_pixbuf_sync(PixbufRenderer *pr, gdouble zoom)
3738 /* no pixbuf so just clear the window */
3739 pr->image_width = 0;
3740 pr->image_height = 0;
3743 box = GTK_WIDGET(pr);
3745 if (GTK_WIDGET_REALIZED(box))
3747 gdk_window_clear(box->window);
3748 pr_overlay_draw(pr, 0, 0, pr->window_width, pr->window_height, NULL);
3751 pr_update_signal(pr);
3756 pr_pixbuf_size_sync(pr);
3757 pr_zoom_sync(pr, zoom, TRUE, TRUE, FALSE, 0, 0);
3760 static void pr_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
3762 if (pixbuf) g_object_ref(pixbuf);
3763 if (pr->pixbuf) g_object_unref(pr->pixbuf);
3764 pr->pixbuf = pixbuf;
3766 pr_pixbuf_sync(pr, zoom);
3769 void pixbuf_renderer_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
3771 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3773 pr_source_tile_unset(pr);
3775 pr_set_pixbuf(pr, pixbuf, zoom);
3777 pr_update_signal(pr);
3780 GdkPixbuf *pixbuf_renderer_get_pixbuf(PixbufRenderer *pr)
3782 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
3787 void pixbuf_renderer_set_orientation(PixbufRenderer *pr, gint orientation)
3789 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3791 pr->orientation = orientation;
3793 pr_pixbuf_size_sync(pr);
3794 pr_zoom_sync(pr, pr->zoom, TRUE, FALSE, FALSE, 0, 0);
3797 gint pixbuf_renderer_get_orientation(PixbufRenderer *pr)
3800 return pr->orientation;
3803 void pixbuf_renderer_set_post_process_func(PixbufRenderer *pr, PixbufRendererPostProcessFunc func, gpointer user_data, gint slow)
3805 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3807 pr->func_post_process = func;
3808 pr->post_process_user_data = user_data;
3809 pr->post_process_slow = func && slow;
3814 void pixbuf_renderer_move(PixbufRenderer *pr, PixbufRenderer *source)
3817 PixbufRendererScrollResetType scroll_reset;
3819 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3820 g_return_if_fail(IS_PIXBUF_RENDERER(source));
3822 if (pr == source) return;
3824 object = G_OBJECT(pr);
3826 g_object_set(object, "zoom_min", source->zoom_min, NULL);
3827 g_object_set(object, "zoom_max", source->zoom_max, NULL);
3828 g_object_set(object, "loading", source->loading, NULL);
3830 pr->complete = source->complete;
3832 pr->x_scroll = source->x_scroll;
3833 pr->y_scroll = source->y_scroll;
3835 scroll_reset = pr->scroll_reset;
3836 pr->scroll_reset = PR_SCROLL_RESET_NOCHANGE;
3838 pr->func_post_process = source->func_post_process;
3839 pr->post_process_user_data = source->post_process_user_data;
3840 pr->post_process_slow = source->post_process_slow;
3841 pr->orientation = source->orientation;
3843 if (source->source_tiles_enabled)
3845 pr_source_tile_unset(pr);
3847 pr->source_tiles_enabled = source->source_tiles_enabled;
3848 pr->source_tiles_cache_size = source->source_tiles_cache_size;
3849 pr->source_tile_width = source->source_tile_width;
3850 pr->source_tile_height = source->source_tile_height;
3851 pr->image_width = source->image_width;
3852 pr->image_height = source->image_height;
3854 pr->func_tile_request = source->func_tile_request;
3855 pr->func_tile_dispose = source->func_tile_dispose;
3856 pr->func_tile_data = source->func_tile_data;
3858 pr->source_tiles = source->source_tiles;
3859 source->source_tiles = NULL;
3861 pr_zoom_sync(pr, source->zoom, TRUE, TRUE, FALSE, 0, 0);
3862 pr_redraw(pr, TRUE);
3866 pixbuf_renderer_set_pixbuf(pr, source->pixbuf, source->zoom);
3869 pr->scroll_reset = scroll_reset;
3871 pixbuf_renderer_set_pixbuf(source, NULL, source->zoom);
3872 pr_queue_clear(source);
3873 pr_tile_free_all(source);
3876 void pixbuf_renderer_area_changed(PixbufRenderer *pr, gint src_x, gint src_y, gint src_w, gint src_h)
3878 gint x, y, width, height, x1, y1, x2, y2;
3880 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3882 pr_coords_map_orientation_reverse(pr,
3884 pr->image_width, pr->image_height,
3889 if (pr->source_tiles_enabled)
3891 pr_source_tile_changed(pr, x, y, width, height);
3894 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
3896 /* increase region when using a zoom quality that may access surrounding pixels */
3901 x1 = (gint)floor((double)x * pr->scale);
3902 y1 = (gint)floor((double)y * pr->scale);
3903 x2 = (gint)ceil((double)(x + width) * pr->scale);
3904 y2 = (gint)ceil((double)(y + height) * pr->scale);
3906 pr_queue(pr, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
3909 void pixbuf_renderer_zoom_adjust(PixbufRenderer *pr, gdouble increment)
3911 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3913 pr_zoom_adjust_real(pr, increment, FALSE, 0, 0);
3916 void pixbuf_renderer_zoom_adjust_at_point(PixbufRenderer *pr, gdouble increment, gint x, gint y)
3918 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3920 pr_zoom_adjust_real(pr, increment, TRUE, x, y);
3923 void pixbuf_renderer_zoom_set(PixbufRenderer *pr, gdouble zoom)
3925 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3927 pr_zoom_sync(pr, zoom, FALSE, FALSE, FALSE, 0, 0);
3930 gdouble pixbuf_renderer_zoom_get(PixbufRenderer *pr)
3932 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3937 gdouble pixbuf_renderer_zoom_get_scale(PixbufRenderer *pr)
3939 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3944 void pixbuf_renderer_zoom_set_limits(PixbufRenderer *pr, gdouble min, gdouble max)
3946 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3948 if (min > 1.0 || max < 1.0) return;
3949 if (min < 1.0 && min > -1.0) return;
3950 if (min < -200.0 || max > 200.0) return;
3952 if (pr->zoom_min != min)
3955 g_object_notify(G_OBJECT(pr), "zoom_min");
3957 if (pr->zoom_max != max)
3960 g_object_notify(G_OBJECT(pr), "zoom_max");
3964 gint pixbuf_renderer_get_image_size(PixbufRenderer *pr, gint *width, gint *height)
3966 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3967 g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3969 if (!pr->pixbuf && !pr->source_tiles_enabled)
3976 *width = pr->image_width;
3977 *height = pr->image_height;
3981 gint pixbuf_renderer_get_scaled_size(PixbufRenderer *pr, gint *width, gint *height)
3983 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3984 g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3986 if (!pr->pixbuf && !pr->source_tiles_enabled)
3994 *height = pr->height;
3998 gint pixbuf_renderer_get_visible_rect(PixbufRenderer *pr, GdkRectangle *rect)
4000 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
4001 g_return_val_if_fail(rect != NULL, FALSE);
4003 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
4013 rect->x = (gint)((gdouble)pr->x_scroll / pr->scale);
4014 rect->y = (gint)((gdouble)pr->y_scroll / pr->scale);
4015 rect->width = (gint)((gdouble)pr->vis_width / pr->scale);
4016 rect->height = (gint)((gdouble)pr->vis_height / pr->scale);
4020 gint pixbuf_renderer_get_virtual_rect(PixbufRenderer *pr, GdkRectangle *rect)
4022 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
4023 g_return_val_if_fail(rect != NULL, FALSE);
4025 if ((!pr->pixbuf && !pr->source_tiles_enabled))
4034 rect->x = pr->x_scroll;
4035 rect->y = pr->y_scroll;
4036 rect->width = pr->vis_width;
4037 rect->height = pr->vis_height;