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"
37 /* size to use when breaking up image pane for rendering */
38 #define PR_TILE_SIZE 128
40 /* default size of tile cache (mb) */
41 #define PR_CACHE_SIZE_DEFAULT 8
43 /* default min and max zoom */
44 #define PR_ZOOM_MIN -32.0
45 #define PR_ZOOM_MAX 32.0
47 /* distance to drag mouse to disable image flip */
48 #define PR_DRAG_SCROLL_THRESHHOLD 4
50 /* increase pan rate when holding down shift */
51 #define PR_PAN_SHIFT_MULTIPLIER 6
54 #define PR_SCROLLER_UPDATES_PER_SEC 30
55 #define PR_SCROLLER_DEAD_ZONE 6
57 /* alpha channel checkerboard background (same as gimp) */
58 #define PR_ALPHA_CHECK1 0x00999999
59 #define PR_ALPHA_CHECK2 0x00666666
60 #define PR_ALPHA_CHECK_SIZE 16
62 /* when scaling image to below this size, use nearest pixel for scaling
63 * (below about 4, the other scale types become slow generating their conversion tables)
65 #define PR_MIN_SCALE_SIZE 8
68 EXIF_ORIENTATION_UNKNOWN = 0,
69 EXIF_ORIENTATION_TOP_LEFT = 1,
70 EXIF_ORIENTATION_TOP_RIGHT = 2,
71 EXIF_ORIENTATION_BOTTOM_RIGHT = 3,
72 EXIF_ORIENTATION_BOTTOM_LEFT = 4,
73 EXIF_ORIENTATION_LEFT_TOP = 5,
74 EXIF_ORIENTATION_RIGHT_TOP = 6,
75 EXIF_ORIENTATION_RIGHT_BOTTOM = 7,
76 EXIF_ORIENTATION_LEFT_BOTTOM = 8
77 } ExifOrientationType;
81 TILE_RENDER_NONE = 0, /* do nothing */
82 TILE_RENDER_AREA, /* render an area of the tile */
83 TILE_RENDER_ALL /* render the whole tile */
84 } ImageTileRenderType;
86 typedef struct _ImageTile ImageTile;
87 typedef struct _QueueData QueueData;
91 GdkPixmap *pixmap; /* off screen buffer */
92 GdkPixbuf *pixbuf; /* pixbuf area for zooming */
93 gint x; /* x offset into image */
94 gint y; /* y offset into image */
95 gint w; /* width that is visible (may be less if at edge of image) */
96 gint h; /* height '' */
100 /* render_todo: (explanation)
102 AREA render area of tile, usually only used when loading an image
103 note: will jump to an ALL if render_done is not ALL.
104 ALL render entire tile, if never done before w/ ALL, for expose events *only*
107 ImageTileRenderType render_todo; /* what to do (see above) */
108 ImageTileRenderType render_done; /* highest that has been done before on tile */
113 guint size; /* est. memory used by pixmap and pixbuf */
126 typedef struct _SourceTile SourceTile;
135 typedef struct _OverlayData OverlayData;
145 gint relative; /* x,y coordinates are relative, negative values start bottom right */
147 gint always; /* hide temporarily when scrolling (not yet implemented) */
153 SIGNAL_SCROLL_NOTIFY,
154 SIGNAL_RENDER_COMPLETE,
171 PROP_CACHE_SIZE_DISPLAY,
172 PROP_CACHE_SIZE_TILES,
175 PROP_WINDOW_LIMIT_VALUE,
177 PROP_AUTOFIT_LIMIT_VALUE
182 static guint signals[SIGNAL_COUNT] = { 0 };
183 static GtkEventBoxClass *parent_class = NULL;
187 static void pixbuf_renderer_class_init(PixbufRendererClass *class);
188 static void pixbuf_renderer_init(PixbufRenderer *pr);
189 static void pixbuf_renderer_finalize(GObject *object);
190 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
191 const GValue *value, GParamSpec *pspec);
192 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
193 GValue *value, GParamSpec *pspec);
194 static gint pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event);
196 static void pr_render_complete_signal(PixbufRenderer *pr);
198 static void pr_overlay_list_clear(PixbufRenderer *pr);
199 static void pr_scroller_timer_set(PixbufRenderer *pr, gint start);
200 static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h);
203 static void pr_source_tile_free_all(PixbufRenderer *pr);
204 static void pr_tile_free_all(PixbufRenderer *pr);
205 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h);
206 static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it);
207 static void pr_queue_clear(PixbufRenderer *pr);
208 static void pr_queue_merge(QueueData *parent, QueueData *qd);
209 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
210 gint clamp, ImageTileRenderType render, gint new_data, gint only_existing);
212 static void pr_redraw(PixbufRenderer *pr, gint new_data);
214 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
215 gint force, gint new,
216 gint center_point, gint px, gint py);
218 static void pr_signals_connect(PixbufRenderer *pr);
219 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data);
220 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area);
224 *-------------------------------------------------------------------
225 * Pixbuf Renderer object
226 *-------------------------------------------------------------------
229 GType pixbuf_renderer_get_type(void)
231 static GType pixbuf_renderer_type = 0;
233 if (!pixbuf_renderer_type)
235 static const GTypeInfo pixbuf_renderer_info =
237 sizeof(PixbufRendererClass),
238 NULL, /* base_init */
239 NULL, /* base_finalize */
240 (GClassInitFunc)pixbuf_renderer_class_init,
241 NULL, /* class_finalize */
242 NULL, /* class_data */
243 sizeof(PixbufRenderer),
245 (GInstanceInitFunc)pixbuf_renderer_init,
248 pixbuf_renderer_type = g_type_register_static(GTK_TYPE_EVENT_BOX, "PixbufRenderer",
249 &pixbuf_renderer_info, 0);
252 return pixbuf_renderer_type;
255 static void pixbuf_renderer_class_init(PixbufRendererClass *class)
257 GObjectClass *gobject_class = G_OBJECT_CLASS(class);
258 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);
260 parent_class = g_type_class_peek_parent(class);
262 gobject_class->set_property = pixbuf_renderer_set_property;
263 gobject_class->get_property = pixbuf_renderer_get_property;
265 gobject_class->finalize = pixbuf_renderer_finalize;
267 widget_class->expose_event = pixbuf_renderer_expose;
269 g_object_class_install_property(gobject_class,
271 g_param_spec_double("zoom_min",
277 G_PARAM_READABLE | G_PARAM_WRITABLE));
279 g_object_class_install_property(gobject_class,
281 g_param_spec_double("zoom_max",
287 G_PARAM_READABLE | G_PARAM_WRITABLE));
289 g_object_class_install_property(gobject_class,
291 g_param_spec_uint("zoom_quality",
297 G_PARAM_READABLE | G_PARAM_WRITABLE));
299 g_object_class_install_property(gobject_class,
301 g_param_spec_boolean("zoom_2pass",
305 G_PARAM_READABLE | G_PARAM_WRITABLE));
307 g_object_class_install_property(gobject_class,
309 g_param_spec_boolean("zoom_expand",
310 "Expand image in autozoom.",
313 G_PARAM_READABLE | G_PARAM_WRITABLE));
315 g_object_class_install_property(gobject_class,
317 g_param_spec_uint("dither_quality",
322 GDK_RGB_DITHER_NORMAL,
323 G_PARAM_READABLE | G_PARAM_WRITABLE));
325 g_object_class_install_property(gobject_class,
327 g_param_spec_uint("scroll_reset",
328 "New image scroll reset",
330 PR_SCROLL_RESET_TOPLEFT,
331 PR_SCROLL_RESET_NOCHANGE,
332 PR_SCROLL_RESET_TOPLEFT,
333 G_PARAM_READABLE | G_PARAM_WRITABLE));
335 g_object_class_install_property(gobject_class,
337 g_param_spec_boolean("delay_flip",
338 "Delay image update",
341 G_PARAM_READABLE | G_PARAM_WRITABLE));
343 g_object_class_install_property(gobject_class,
345 g_param_spec_boolean("loading",
346 "Image actively loading",
349 G_PARAM_READABLE | G_PARAM_WRITABLE));
351 g_object_class_install_property(gobject_class,
353 g_param_spec_boolean("complete",
354 "Image rendering complete",
357 G_PARAM_READABLE | G_PARAM_WRITABLE));
359 g_object_class_install_property(gobject_class,
360 PROP_CACHE_SIZE_DISPLAY,
361 g_param_spec_uint("cache_display",
362 "Display cache size MB",
366 PR_CACHE_SIZE_DEFAULT,
367 G_PARAM_READABLE | G_PARAM_WRITABLE));
369 g_object_class_install_property(gobject_class,
370 PROP_CACHE_SIZE_TILES,
371 g_param_spec_uint("cache_tiles",
373 "Number of tiles to retain in memory at any one time.",
376 PR_CACHE_SIZE_DEFAULT,
377 G_PARAM_READABLE | G_PARAM_WRITABLE));
379 g_object_class_install_property(gobject_class,
381 g_param_spec_boolean("window_fit",
382 "Fit window to image size",
385 G_PARAM_READABLE | G_PARAM_WRITABLE));
387 g_object_class_install_property(gobject_class,
389 g_param_spec_boolean("window_limit",
390 "Limit size of parent window",
393 G_PARAM_READABLE | G_PARAM_WRITABLE));
395 g_object_class_install_property(gobject_class,
396 PROP_WINDOW_LIMIT_VALUE,
397 g_param_spec_uint("window_limit_value",
398 "Size limit of parent window",
403 G_PARAM_READABLE | G_PARAM_WRITABLE));
405 g_object_class_install_property(gobject_class,
407 g_param_spec_boolean("autofit_limit",
408 "Limit size of image when autofitting",
411 G_PARAM_READABLE | G_PARAM_WRITABLE));
413 g_object_class_install_property(gobject_class,
414 PROP_AUTOFIT_LIMIT_VALUE,
415 g_param_spec_uint("autofit_limit_value",
416 "Size limit of image when autofitting",
421 G_PARAM_READABLE | G_PARAM_WRITABLE));
424 signals[SIGNAL_ZOOM] =
426 G_OBJECT_CLASS_TYPE(gobject_class),
428 G_STRUCT_OFFSET(PixbufRendererClass, zoom),
430 g_cclosure_marshal_VOID__DOUBLE,
434 signals[SIGNAL_CLICKED] =
435 g_signal_new("clicked",
436 G_OBJECT_CLASS_TYPE(gobject_class),
438 G_STRUCT_OFFSET(PixbufRendererClass, clicked),
440 g_cclosure_marshal_VOID__BOXED,
444 signals[SIGNAL_SCROLL_NOTIFY] =
445 g_signal_new("scroll-notify",
446 G_OBJECT_CLASS_TYPE(gobject_class),
448 G_STRUCT_OFFSET(PixbufRendererClass, scroll_notify),
450 g_cclosure_marshal_VOID__VOID,
453 signals[SIGNAL_RENDER_COMPLETE] =
454 g_signal_new("render-complete",
455 G_OBJECT_CLASS_TYPE(gobject_class),
457 G_STRUCT_OFFSET(PixbufRendererClass, render_complete),
459 g_cclosure_marshal_VOID__VOID,
462 signals[SIGNAL_DRAG] =
464 G_OBJECT_CLASS_TYPE(gobject_class),
466 G_STRUCT_OFFSET(PixbufRendererClass, drag),
468 g_cclosure_marshal_VOID__BOXED,
473 static void pixbuf_renderer_init(PixbufRenderer *pr)
477 box = GTK_WIDGET(pr);
479 pr->zoom_min = PR_ZOOM_MIN;
480 pr->zoom_max = PR_ZOOM_MAX;
481 pr->zoom_quality = GDK_INTERP_BILINEAR;
482 pr->zoom_2pass = FALSE;
487 pr->dither_quality = GDK_RGB_DITHER_NORMAL;
489 pr->scroll_reset = PR_SCROLL_RESET_TOPLEFT;
491 pr->draw_idle_id = -1;
493 pr->tile_width = PR_TILE_SIZE;
494 pr->tile_height = PR_TILE_SIZE;
497 pr->tile_cache_size = 0;
499 pr->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
501 pr->scroller_id = -1;
502 pr->scroller_overlay = -1;
504 pr->source_tiles_enabled = FALSE;
505 pr->source_tiles = NULL;
509 gtk_widget_set_double_buffered(box, FALSE);
510 g_signal_connect_after(G_OBJECT(box), "size_allocate",
511 G_CALLBACK(pr_size_cb), pr);
513 pr_signals_connect(pr);
516 static void pixbuf_renderer_finalize(GObject *object)
520 pr = PIXBUF_RENDERER(object);
523 pr_tile_free_all(pr);
525 if (pr->pixbuf) g_object_unref(pr->pixbuf);
526 if (pr->spare_tile) gdk_pixbuf_unref(pr->spare_tile);
528 pr_scroller_timer_set(pr, FALSE);
529 pr_overlay_list_clear(pr);
531 pr_source_tile_free_all(pr);
534 PixbufRenderer* pixbuf_renderer_new(void)
536 return g_object_new(TYPE_PIXBUF_RENDERER, NULL);
539 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
540 const GValue *value, GParamSpec *pspec)
544 pr = PIXBUF_RENDERER(object);
549 pr->zoom_min = g_value_get_double(value);
552 pr->zoom_max = g_value_get_double(value);
554 case PROP_ZOOM_QUALITY:
555 pr->zoom_quality = g_value_get_uint(value);
557 case PROP_ZOOM_2PASS:
558 pr->zoom_2pass = g_value_get_boolean(value);
560 case PROP_ZOOM_EXPAND:
561 pr->zoom_expand = g_value_get_boolean(value);
563 case PROP_DITHER_QUALITY:
564 pr->dither_quality = g_value_get_uint(value);
566 case PROP_SCROLL_RESET:
567 pr->scroll_reset = g_value_get_uint(value);
569 case PROP_DELAY_FLIP:
570 pr->delay_flip = g_value_get_boolean(value);
573 pr->loading = g_value_get_boolean(value);
576 pr->complete = g_value_get_boolean(value);
578 case PROP_CACHE_SIZE_DISPLAY:
579 pr->tile_cache_max = g_value_get_uint(value);
581 case PROP_CACHE_SIZE_TILES:
582 pr->source_tiles_cache_size = g_value_get_uint(value);
584 case PROP_WINDOW_FIT:
585 pr->window_fit = g_value_get_boolean(value);
587 case PROP_WINDOW_LIMIT:
588 pr->window_limit = g_value_get_boolean(value);
590 case PROP_WINDOW_LIMIT_VALUE:
591 pr->window_limit_size = g_value_get_uint(value);
593 case PROP_AUTOFIT_LIMIT:
594 pr->autofit_limit = g_value_get_boolean(value);
596 case PROP_AUTOFIT_LIMIT_VALUE:
597 pr->autofit_limit_size = g_value_get_uint(value);
600 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
605 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
606 GValue *value, GParamSpec *pspec)
610 pr = PIXBUF_RENDERER(object);
615 g_value_set_double(value, pr->zoom_min);
618 g_value_set_double(value, pr->zoom_max);
620 case PROP_ZOOM_QUALITY:
621 g_value_set_uint(value, pr->zoom_quality);
623 case PROP_ZOOM_2PASS:
624 g_value_set_boolean(value, pr->zoom_2pass);
626 case PROP_ZOOM_EXPAND:
627 g_value_set_boolean(value, pr->zoom_expand);
629 case PROP_DITHER_QUALITY:
630 g_value_set_uint(value, pr->dither_quality);
632 case PROP_SCROLL_RESET:
633 g_value_set_uint(value, pr->scroll_reset);
635 case PROP_DELAY_FLIP:
636 g_value_set_boolean(value, pr->delay_flip);
639 g_value_set_boolean(value, pr->loading);
642 g_value_set_boolean(value, pr->complete);
644 case PROP_CACHE_SIZE_DISPLAY:
645 g_value_set_uint(value, pr->tile_cache_max);
647 case PROP_CACHE_SIZE_TILES:
648 g_value_set_uint(value, pr->source_tiles_cache_size);
650 case PROP_WINDOW_FIT:
651 g_value_set_boolean(value, pr->window_fit);
653 case PROP_WINDOW_LIMIT:
654 g_value_set_boolean(value, pr->window_limit);
656 case PROP_WINDOW_LIMIT_VALUE:
657 g_value_set_uint(value, pr->window_limit_size);
659 case PROP_AUTOFIT_LIMIT:
660 g_value_set_boolean(value, pr->autofit_limit);
662 case PROP_AUTOFIT_LIMIT_VALUE:
663 g_value_set_uint(value, pr->autofit_limit_size);
666 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
671 static gint pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event)
673 if (GTK_WIDGET_DRAWABLE(widget))
675 if (!GTK_WIDGET_NO_WINDOW(widget))
677 if (event->window != widget->window)
681 gdk_window_get_position(event->window, &area.x, &area.y);
683 area.x += event->area.x;
684 area.y += event->area.y;
685 area.width = event->area.width;
686 area.height = event->area.height;
687 pixbuf_renderer_paint(PIXBUF_RENDERER(widget), &area);
691 pixbuf_renderer_paint(PIXBUF_RENDERER(widget), &event->area);
700 *-------------------------------------------------------------------
702 *-------------------------------------------------------------------
705 static void widget_set_cursor(GtkWidget *widget, gint icon)
709 if (!widget->window) return;
717 cursor = gdk_cursor_new(icon);
720 gdk_window_set_cursor(widget->window, cursor);
722 if (cursor) gdk_cursor_unref(cursor);
725 static gint pixmap_calc_size(GdkPixmap *pixmap)
729 d = gdk_drawable_get_depth(pixmap);
730 gdk_drawable_get_size(pixmap, &w, &h);
731 return w * h * (d / 8);
734 static gint pr_clip_region(gint x, gint y, gint w, gint h,
735 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
736 gint *rx, gint *ry, gint *rw, gint *rh)
738 if (clip_x + clip_w <= x ||
740 clip_y + clip_h <= y ||
746 *rx = MAX(x, clip_x);
747 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
749 *ry = MAX(y, clip_y);
750 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
755 static gint pr_parent_window_sizable(PixbufRenderer *pr)
757 GdkWindowState state;
759 if (!pr->parent_window) return FALSE;
760 if (!pr->window_fit) return FALSE;
761 if (!GTK_WIDGET(pr)->window) return FALSE;
763 if (!pr->parent_window->window) return FALSE;
764 state = gdk_window_get_state(pr->parent_window->window);
765 if (state & GDK_WINDOW_STATE_MAXIMIZED) return FALSE;
770 static gint pr_parent_window_resize(PixbufRenderer *pr, gint w, gint h)
776 if (!pr_parent_window_sizable(pr)) return FALSE;
778 if (pr->window_limit)
780 gint sw = gdk_screen_width() * pr->window_limit_size / 100;
781 gint sh = gdk_screen_height() * pr->window_limit_size / 100;
787 widget = GTK_WIDGET(pr);
788 parent = GTK_WIDGET(pr->parent_window);
790 w += (parent->allocation.width - widget->allocation.width);
791 h += (parent->allocation.height - widget->allocation.height);
793 gdk_drawable_get_size(parent->window, &ww, &wh);
794 if (w == ww && h == wh) return FALSE;
796 gdk_window_resize(parent->window, w, h);
801 void pixbuf_renderer_set_parent(PixbufRenderer *pr, GtkWindow *window)
803 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
804 g_return_if_fail(window == NULL || GTK_IS_WINDOW(window));
806 pr->parent_window = GTK_WIDGET(window);
809 GtkWindow *pixbuf_renderer_get_parent(PixbufRenderer *pr)
811 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
813 return GTK_WINDOW(pr->parent_window);
818 *-------------------------------------------------------------------
820 *-------------------------------------------------------------------
823 static void pr_overlay_get_position(PixbufRenderer *pr, OverlayData *od,
824 gint *x, gint *y, gint *w, gint *h)
828 pw = gdk_pixbuf_get_width(od->pixbuf);
829 ph = gdk_pixbuf_get_height(od->pixbuf);
835 if (px < 0) px = pr->window_width - pw + px;
836 if (py < 0) py = pr->window_height - ph + py;
845 static void pr_overlay_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
851 box = GTK_WIDGET(pr);
853 work = pr->overlay_list;
863 pr_overlay_get_position(pr, od, &px, &py, &pw, &ph);
864 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
866 if (!pr->overlay_buffer)
868 pr->overlay_buffer = gdk_pixmap_new(((GtkWidget *)pr)->window, pr->tile_width, pr->tile_height, -1);
873 gdk_draw_drawable(pr->overlay_buffer, box->style->fg_gc[GTK_WIDGET_STATE(box)],
875 rx - (pr->x_offset + (it->x - pr->x_scroll)),
876 ry - (pr->y_offset + (it->y - pr->y_scroll)),
878 gdk_draw_pixbuf(pr->overlay_buffer,
879 box->style->fg_gc[GTK_WIDGET_STATE(box)],
883 pr->dither_quality, rx, ry);
884 gdk_draw_drawable(od->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
887 rx - px, ry - py, rw, rh);
891 /* no ImageTile means region may be larger than our scratch buffer */
894 for (sx = rx; sx < rx + rw; sx += pr->tile_width)
895 for (sy = ry; sy < ry + rh; sy += pr->tile_height)
899 sw = MIN(rx + rw - sx, pr->tile_width);
900 sh = MIN(ry + rh - sy, pr->tile_height);
902 gdk_draw_rectangle(pr->overlay_buffer,
903 box->style->bg_gc[GTK_WIDGET_STATE(box)], TRUE,
905 gdk_draw_pixbuf(pr->overlay_buffer,
906 box->style->fg_gc[GTK_WIDGET_STATE(box)],
910 pr->dither_quality, sx, sy);
911 gdk_draw_drawable(od->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
914 sx - px, sy - py, sw, sh);
921 static void pr_overlay_queue_draw(PixbufRenderer *pr, OverlayData *od)
925 pr_overlay_get_position(pr, od, &x, &y, &w, &h);
926 pr_queue(pr, pr->x_scroll - pr->x_offset + x,
927 pr->y_scroll - pr->y_offset + y,
929 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
931 pr_border_draw(pr, x, y, w, h);
934 static void pr_overlay_queue_all(PixbufRenderer *pr)
938 work = pr->overlay_list;
941 OverlayData *od = work->data;
944 pr_overlay_queue_draw(pr, od);
948 static void pr_overlay_update_sizes(PixbufRenderer *pr)
952 work = pr->overlay_list;
955 OverlayData *od = work->data;
958 if (od->relative && od->window)
962 pr_overlay_get_position(pr, od, &x, &y, &w, &h);
963 gdk_window_move_resize(od->window, x, y, w, h);
968 static OverlayData *pr_overlay_find(PixbufRenderer *pr, gint id)
972 work = pr->overlay_list;
975 OverlayData *od = work->data;
978 if (od->id == id) return od;
984 gint pixbuf_renderer_overlay_add(PixbufRenderer *pr, GdkPixbuf *pixbuf, gint x, gint y,
985 gint relative, gint always)
990 GdkWindowAttr attributes;
991 gint attributes_mask;
993 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
994 g_return_val_if_fail(pixbuf != NULL, -1);
997 while (pr_overlay_find(pr, id)) id++;
999 od = g_new0(OverlayData, 1);
1001 od->pixbuf = pixbuf;
1002 g_object_ref(G_OBJECT(od->pixbuf));
1005 od->relative = relative;
1006 od->always = always;
1008 pr_overlay_get_position(pr, od, &px, &py, &pw, &ph);
1010 attributes.window_type = GDK_WINDOW_CHILD;
1011 attributes.wclass = GDK_INPUT_OUTPUT;
1012 attributes.width = pw;
1013 attributes.height = ph;
1014 attributes.event_mask = GDK_EXPOSURE_MASK;
1015 attributes_mask = 0;
1017 od->window = gdk_window_new(GTK_WIDGET(pr)->window, &attributes, attributes_mask);
1018 gdk_window_set_user_data(od->window, pr);
1019 gdk_window_move(od->window, px, py);
1020 gdk_window_show(od->window);
1022 pr->overlay_list = g_list_append(pr->overlay_list, od);
1024 pr_overlay_queue_draw(pr, od);
1029 static void pr_overlay_free(PixbufRenderer *pr, OverlayData *od)
1031 pr->overlay_list = g_list_remove(pr->overlay_list, od);
1033 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
1034 if (od->window) gdk_window_destroy(od->window);
1037 if (!pr->overlay_list && pr->overlay_buffer)
1039 g_object_unref(pr->overlay_buffer);
1040 pr->overlay_buffer = NULL;
1044 static void pr_overlay_list_clear(PixbufRenderer *pr)
1046 while (pr->overlay_list)
1050 od = pr->overlay_list->data;
1051 pr_overlay_free(pr, od);
1055 void pixbuf_renderer_overlay_set(PixbufRenderer *pr, gint id, GdkPixbuf *pixbuf, gint x, gint y)
1059 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1061 od = pr_overlay_find(pr, id);
1066 gint px, py, pw, ph;
1068 g_object_ref(G_OBJECT(pixbuf));
1069 g_object_unref(G_OBJECT(od->pixbuf));
1070 od->pixbuf = pixbuf;
1075 pr_overlay_queue_draw(pr, od);
1076 pr_overlay_get_position(pr, od, &px, &py, &pw, &ph);
1077 gdk_window_move_resize(od->window, px, py, pw, ph);
1081 pr_overlay_queue_draw(pr, od);
1082 pr_overlay_free(pr, od);
1086 gint pixbuf_renderer_overlay_get(PixbufRenderer *pr, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
1090 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
1092 od = pr_overlay_find(pr, id);
1093 if (!od) return FALSE;
1095 if (pixbuf) *pixbuf = od->pixbuf;
1102 void pixbuf_renderer_overlay_remove(PixbufRenderer *pr, gint id)
1104 pixbuf_renderer_overlay_set(pr, id, NULL, 0, 0);
1108 *-------------------------------------------------------------------
1110 *-------------------------------------------------------------------
1114 static gboolean pr_scroller_update_cb(gpointer data)
1116 PixbufRenderer *pr = data;
1120 /* this was a simple scroll by difference between scroller and mouse position,
1121 * but all this math results in a smoother result and accounts for a dead zone.
1124 if (abs(pr->scroller_xpos - pr->scroller_x) < PR_SCROLLER_DEAD_ZONE)
1130 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1131 x = (pr->scroller_xpos - pr->scroller_x) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1132 x += (x > 0) ? -shift : shift;
1135 if (abs(pr->scroller_ypos - pr->scroller_y) < PR_SCROLLER_DEAD_ZONE)
1141 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1142 y = (pr->scroller_ypos - pr->scroller_y) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1143 y += (y > 0) ? -shift : shift;
1146 if (abs(x) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
1152 xinc = pr->scroller_xinc;
1156 if (xinc < 0) xinc = 0;
1157 if (x < xinc) xinc = x;
1158 if (x > xinc) xinc = MIN(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
1162 if (xinc > 0) xinc = 0;
1163 if (x > xinc) xinc = x;
1164 if (x < xinc) xinc = MAX(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
1168 if (abs(y) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
1174 yinc = pr->scroller_yinc;
1178 if (yinc < 0) yinc = 0;
1179 if (y < yinc) yinc = y;
1180 if (y > yinc) yinc = MIN(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
1184 if (yinc > 0) yinc = 0;
1185 if (y > yinc) yinc = y;
1186 if (y < yinc) yinc = MAX(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
1190 pr->scroller_xinc = xinc;
1191 pr->scroller_yinc = yinc;
1193 xinc = xinc / PR_SCROLLER_UPDATES_PER_SEC;
1194 yinc = yinc / PR_SCROLLER_UPDATES_PER_SEC;
1196 pixbuf_renderer_scroll(pr, xinc, yinc);
1201 static void pr_scroller_timer_set(PixbufRenderer *pr, gint start)
1203 if (pr->scroller_id != -1)
1205 g_source_remove(pr->scroller_id);
1206 pr->scroller_id = -1;
1211 pr->scroller_id = g_timeout_add(1000 / PR_SCROLLER_UPDATES_PER_SEC,
1212 pr_scroller_update_cb, pr);
1216 static void pr_scroller_start(PixbufRenderer *pr, gint x, gint y)
1218 if (pr->scroller_overlay == -1)
1224 pixbuf = pixbuf_inline(PIXBUF_INLINE_SCROLLER);
1226 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 32, 32);
1227 gdk_pixbuf_fill(pixbuf, 0x000000ff);
1229 w = gdk_pixbuf_get_width(pixbuf);
1230 h = gdk_pixbuf_get_height(pixbuf);
1232 pr->scroller_overlay = pixbuf_renderer_overlay_add(pr, pixbuf, x - w / 2, y - h / 2, FALSE, TRUE);
1233 g_object_unref(pixbuf);
1238 pr->scroller_xpos = x;
1239 pr->scroller_ypos = y;
1241 pr_scroller_timer_set(pr, TRUE);
1244 static void pr_scroller_stop(PixbufRenderer *pr)
1246 if (pr->scroller_id == -1) return;
1248 pixbuf_renderer_overlay_remove(pr, pr->scroller_overlay);
1249 pr->scroller_overlay = -1;
1251 pr_scroller_timer_set(pr, FALSE);
1255 *-------------------------------------------------------------------
1257 *-------------------------------------------------------------------
1260 static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
1263 gint rx, ry, rw, rh;
1265 box = GTK_WIDGET(pr);
1267 if (!box->window) return;
1269 if (!pr->pixbuf && !pr->source_tiles_enabled)
1271 if (pr_clip_region(x, y, w, h,
1273 pr->window_width, pr->window_height,
1274 &rx, &ry, &rw, &rh))
1276 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1277 pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1282 if (pr->vis_width < pr->window_width)
1284 if (pr->x_offset > 0 &&
1285 pr_clip_region(x, y, w, h,
1287 pr->x_offset, pr->window_height,
1288 &rx, &ry, &rw, &rh))
1290 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1291 pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1293 if (pr->window_width - pr->vis_width - pr->x_offset > 0 &&
1294 pr_clip_region(x, y, w, h,
1295 pr->x_offset + pr->vis_width, 0,
1296 pr->window_width - pr->vis_width - pr->x_offset, pr->window_height,
1297 &rx, &ry, &rw, &rh))
1299 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1300 pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1303 if (pr->vis_height < pr->window_height)
1305 if (pr->y_offset > 0 &&
1306 pr_clip_region(x, y, w, h,
1308 pr->vis_width, pr->y_offset,
1309 &rx, &ry, &rw, &rh))
1311 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1312 pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1314 if (pr->window_height - pr->vis_height - pr->y_offset > 0 &&
1315 pr_clip_region(x, y, w, h,
1316 pr->x_offset, pr->y_offset + pr->vis_height,
1317 pr->vis_width, pr->window_height - pr->vis_height - pr->y_offset,
1318 &rx, &ry, &rw, &rh))
1320 gdk_window_clear_area(box->window, rx, ry, rw, rh);
1321 pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1326 static void pr_border_clear(PixbufRenderer *pr)
1328 pr_border_draw(pr, 0, 0, pr->window_width, pr->window_height);
1331 void pixbuf_renderer_set_color(PixbufRenderer *pr, GdkColor *color)
1336 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1338 widget = GTK_WIDGET(pr);
1343 style = gtk_style_copy(gtk_widget_get_style(widget));
1344 slot = &style->bg[GTK_STATE_NORMAL];
1346 slot->red = color->red;
1347 slot->green = color->green;
1348 slot->blue = color->blue;
1351 style = gtk_style_copy(gtk_widget_get_default_style());
1354 gtk_widget_set_style(widget, style);
1356 if (GTK_WIDGET_VISIBLE(widget)) pr_border_clear(pr);
1361 *-------------------------------------------------------------------
1363 *-------------------------------------------------------------------
1366 static void pr_source_tile_free(SourceTile *st)
1370 if (st->pixbuf) g_object_unref(st->pixbuf);
1374 static void pr_source_tile_free_all(PixbufRenderer *pr)
1378 work = pr->source_tiles;
1386 pr_source_tile_free(st);
1389 g_list_free(pr->source_tiles);
1390 pr->source_tiles = NULL;
1393 static void pr_source_tile_unset(PixbufRenderer *pr)
1395 pr_source_tile_free_all(pr);
1396 pr->source_tiles_enabled = FALSE;
1399 static gint pr_source_tile_visible(PixbufRenderer *pr, SourceTile *st)
1401 gint x1, y1, x2, y2;
1403 if (!st) return FALSE;
1405 x1 = (pr->x_scroll / pr->tile_width) * pr->tile_width;
1406 y1 = (pr->y_scroll / pr->tile_height) * pr->tile_height;
1407 x2 = ((pr->x_scroll + pr->vis_width) / pr->tile_width) * pr->tile_width + pr->tile_width;
1408 y2 = ((pr->y_scroll + pr->vis_height) / pr->tile_height) * pr->tile_height + pr->tile_height;
1410 return !((double)st->x * pr->scale > (double)x2 ||
1411 (double)(st->x + pr->source_tile_width) * pr->scale < (double)x1 ||
1412 (double)st->y * pr->scale > (double)y2 ||
1413 (double)(st->y + pr->source_tile_height) * pr->scale < (double)y1);
1416 static SourceTile *pr_source_tile_new(PixbufRenderer *pr, gint x, gint y)
1418 SourceTile *st = NULL;
1421 g_return_val_if_fail(pr->source_tile_width >= 1 && pr->source_tile_height >= 1, NULL);
1423 if (pr->source_tiles_cache_size < 4) pr->source_tiles_cache_size = 4;
1425 count = g_list_length(pr->source_tiles);
1426 if (count >= pr->source_tiles_cache_size)
1430 work = g_list_last(pr->source_tiles);
1431 while (work && count >= pr->source_tiles_cache_size)
1435 needle = work->data;
1438 if (!pr_source_tile_visible(pr, needle))
1440 pr->source_tiles = g_list_remove(pr->source_tiles, needle);
1442 if (pr->func_tile_dispose)
1444 pr->func_tile_dispose(pr, needle->x, needle->y,
1445 pr->source_tile_width, pr->source_tile_height,
1446 needle->pixbuf, pr->func_tile_data);
1455 pr_source_tile_free(needle);
1465 st = g_new0(SourceTile, 1);
1466 st->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
1467 pr->source_tile_width, pr->source_tile_height);
1470 st->x = (x / pr->source_tile_width) * pr->source_tile_width;
1471 st->y = (y / pr->source_tile_height) * pr->source_tile_height;
1474 pr->source_tiles = g_list_prepend(pr->source_tiles, st);
1479 static SourceTile *pr_source_tile_request(PixbufRenderer *pr, gint x, gint y)
1483 st = pr_source_tile_new(pr, x, y);
1484 if (!st) return NULL;
1486 if (pr->func_tile_request &&
1487 pr->func_tile_request(pr, st->x, st->y,
1488 pr->source_tile_width, pr->source_tile_height, st->pixbuf, pr->func_tile_data))
1493 pr_tile_invalidate_region(pr, st->x * pr->scale, st->y * pr->scale,
1494 pr->source_tile_width * pr->scale, pr->source_tile_height * pr->scale);
1499 static SourceTile *pr_source_tile_find(PixbufRenderer *pr, gint x, gint y)
1503 work = pr->source_tiles;
1506 SourceTile *st = work->data;
1508 if (x >= st->x && x < st->x + pr->source_tile_width &&
1509 y >= st->y && y < st->y + pr->source_tile_height)
1511 if (work != pr->source_tiles)
1513 pr->source_tiles = g_list_remove_link(pr->source_tiles, work);
1514 pr->source_tiles = g_list_concat(work, pr->source_tiles);
1525 static GList *pr_source_tile_compute_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h, gint request)
1533 if (w > pr->image_width) w = pr->image_width;
1534 if (h > pr->image_height) h = pr->image_height;
1536 sx = (x / pr->source_tile_width) * pr->source_tile_width;
1537 sy = (y / pr->source_tile_height) * pr->source_tile_height;
1539 for (x1 = sx; x1 < x + w; x1+= pr->source_tile_width)
1541 for (y1 = sy; y1 < y + h; y1 += pr->source_tile_height)
1545 st = pr_source_tile_find(pr, x1, y1);
1546 if (!st && request) st = pr_source_tile_request(pr, x1, y1);
1548 if (st) list = g_list_prepend(list, st);
1552 return g_list_reverse(list);
1555 static void pr_source_tile_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height)
1559 if (width < 1 || height < 1) return;
1561 work = pr->source_tiles;
1565 gint rx, ry, rw, rh;
1570 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1571 x, y, width, height,
1572 &rx, &ry, &rw, &rh))
1576 pixbuf = gdk_pixbuf_new_subpixbuf(st->pixbuf, rx - st->x, ry - st->y, rw, rh);
1577 if (pr->func_tile_request &&
1578 pr->func_tile_request(pr, rx, ry, rw, rh, pixbuf, pr->func_tile_data))
1580 pr_tile_invalidate_region(pr, rx * pr->scale, ry * pr->scale,
1581 rw * pr->scale, rh * pr->scale);
1583 g_object_unref(pixbuf);
1588 static gint pr_source_tile_render(PixbufRenderer *pr, ImageTile *it,
1589 gint x, gint y, gint w, gint h,
1590 gint new_data, gint fast)
1597 box = GTK_WIDGET(pr);
1599 if (pr->zoom == 1.0 || pr->scale == 1.0)
1601 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1606 gint rx, ry, rw, rh;
1611 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1612 it->x + x, it->y + y, w, h,
1613 &rx, &ry, &rw, &rh))
1617 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1618 rx - st->x, ry - st->y, rw, rh);
1620 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1622 gdk_draw_pixbuf(it->pixmap,
1623 box->style->fg_gc[GTK_WIDGET_STATE(box)],
1625 rx - st->x, ry - st->y,
1626 rx - it->x, ry - it->y,
1628 pr->dither_quality, rx, ry);
1635 double scale_x, scale_y;
1636 gint sx, sy, sw, sh;
1638 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1639 scale_x = (double)pr->width / pr->image_width;
1640 scale_y = (double)pr->height / pr->image_height;
1642 sx = (double)(it->x + x) / scale_x;
1643 sy = (double)(it->y + y) / scale_y;
1644 sw = (double)w / scale_x;
1645 sh = (double)h / scale_y;
1647 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1650 /* draws red over draw region, to check for leaks (regions not filled) */
1651 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1654 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1659 gint rx, ry, rw, rh;
1660 gint stx, sty, stw, sth;
1665 stx = floor((double)st->x * scale_x);
1666 sty = floor((double)st->y * scale_y);
1667 stw = ceil((double)(st->x + pr->source_tile_width) * scale_x) - stx;
1668 sth = ceil((double)(st->y + pr->source_tile_height) * scale_y) - sty;
1670 if (pr_clip_region(stx, sty, stw, sth,
1671 it->x + x, it->y + y, w, h,
1672 &rx, &ry, &rw, &rh))
1676 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1677 rx - st->x, ry - st->y, rw, rh);
1684 /* may need to use unfloored stx,sty values here */
1685 offset_x = (double)(stx - it->x);
1686 offset_y = (double)(sty - it->y);
1688 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1689 (double) 0.0 + offset_x,
1690 (double) 0.0 + offset_y,
1692 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1704 void pixbuf_renderer_set_tiles(PixbufRenderer *pr, gint width, gint height,
1705 gint tile_width, gint tile_height, gint cache_size,
1706 PixbufRendererTileRequestFunc func_request,
1707 PixbufRendererTileDisposeFunc func_dispose,
1711 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1712 g_return_if_fail(tile_width >= 32 && tile_width >= 32);
1713 g_return_if_fail(width >= 32 && height > 32);
1714 g_return_if_fail(func_request != NULL);
1716 if (pr->pixbuf) g_object_unref(pr->pixbuf);
1719 pr_source_tile_unset(pr);
1721 if (cache_size < 4) cache_size = 4;
1723 pr->source_tiles_enabled = TRUE;
1724 pr->source_tiles_cache_size = cache_size;
1725 pr->source_tile_width = tile_width;
1726 pr->source_tile_height = tile_height;
1728 pr->image_width = width;
1729 pr->image_height = height;
1731 pr->func_tile_request = func_request;
1732 pr->func_tile_dispose = func_dispose;
1733 pr->func_tile_data = user_data;
1735 pr_zoom_sync(pr, zoom, TRUE, TRUE, FALSE, 0, 0);
1736 pr_redraw(pr, TRUE);
1739 void pixbuf_renderer_set_tiles_size(PixbufRenderer *pr, gint width, gint height)
1741 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1742 g_return_if_fail(width >= 32 && height > 32);
1744 if (!pr->source_tiles_enabled) return;
1745 if (pr->image_width == width && pr->image_height == height) return;
1747 pr->image_width = width;
1748 pr->image_height = height;
1750 pr_zoom_sync(pr, pr->zoom, TRUE, FALSE, FALSE, 0, 0);
1753 gint pixbuf_renderer_get_tiles(PixbufRenderer *pr)
1755 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
1757 return pr->source_tiles_enabled;
1760 static void pr_zoom_adjust_real(PixbufRenderer *pr, gdouble increment,
1761 gint center_point, gint x, gint y)
1763 gdouble zoom = pr->zoom;
1765 if (increment == 0.0) return;
1769 if (pr->scale < 1.0)
1771 zoom = 0.0 - 1.0 / pr->scale;
1779 if (increment < 0.0)
1781 if (zoom >= 1.0 && zoom + increment < 1.0)
1783 zoom = zoom + increment - 2.0;
1787 zoom = zoom + increment;
1792 if (zoom <= -1.0 && zoom + increment > -1.0)
1794 zoom = zoom + increment + 2.0;
1798 zoom = zoom + increment;
1802 pr_zoom_sync(pr, zoom, FALSE, FALSE, center_point, x, y);
1806 *-------------------------------------------------------------------
1808 *-------------------------------------------------------------------
1811 static ImageTile *pr_tile_new(gint x, gint y, gint width, gint height)
1815 it = g_new0(ImageTile, 1);
1822 it->render_done = TILE_RENDER_NONE;
1827 static void pr_tile_free(ImageTile *it)
1831 if (it->pixbuf) gdk_pixbuf_unref(it->pixbuf);
1832 if (it->pixmap) g_object_unref(it->pixmap);
1837 static void pr_tile_free_all(PixbufRenderer *pr)
1852 g_list_free(pr->tiles);
1854 pr->tile_cache_size = 0;
1857 static ImageTile *pr_tile_add(PixbufRenderer *pr, gint x, gint y)
1861 it = pr_tile_new(x, y, pr->tile_width, pr->tile_height);
1863 if (it->x + it->w > pr->width) it->w = pr->width - it->x;
1864 if (it->y + it->h > pr->height) it->h = pr->height - it->y;
1866 pr->tiles = g_list_prepend(pr->tiles, it);
1867 pr->tile_cache_size += it->size;
1872 static void pr_tile_remove(PixbufRenderer *pr, ImageTile *it)
1876 QueueData *qd = it->qd;
1879 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
1885 QueueData *qd = it->qd2;
1888 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
1892 pr->tiles = g_list_remove(pr->tiles, it);
1893 pr->tile_cache_size -= it->size;
1898 static void pr_tile_free_space(PixbufRenderer *pr, guint space, ImageTile *it)
1903 work = g_list_last(pr->tiles);
1905 if (pr->source_tiles_enabled && pr->scale < 1.0)
1909 tiles = (pr->vis_width / pr->tile_width + 1) * (pr->vis_height / pr->tile_height + 1);
1910 tile_max = MAX(tiles * pr->tile_width * pr->tile_height * 3,
1911 (gint)((double)pr->tile_cache_max * 1048576.0 * pr->scale));
1915 tile_max = pr->tile_cache_max * 1048576;
1918 while (work && pr->tile_cache_size + space > tile_max)
1922 needle = work->data;
1925 ((!needle->qd && !needle->qd2) || !pr_tile_is_visible(pr, needle))) pr_tile_remove(pr, needle);
1929 static void pr_tile_invalidate_all(PixbufRenderer *pr)
1941 it->render_done = TILE_RENDER_NONE;
1942 it->render_todo = TILE_RENDER_ALL;
1945 it->w = MIN(pr->tile_width, pr->width - it->x);
1946 it->h = MIN(pr->tile_height, pr->height - it->y);
1950 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
1956 x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
1957 x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
1959 y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
1960 y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
1970 if (it->x < x2 && it->x + it->w > x1 &&
1971 it->y < y2 && it->y + it->h > y1)
1973 it->render_done = TILE_RENDER_NONE;
1974 it->render_todo = TILE_RENDER_ALL;
1979 static ImageTile *pr_tile_get(PixbufRenderer *pr, gint x, gint y, gint only_existing)
1989 if (it->x == x && it->y == y)
1991 pr->tiles = g_list_delete_link(pr->tiles, work);
1992 pr->tiles = g_list_prepend(pr->tiles, it);
1999 if (only_existing) return NULL;
2001 return pr_tile_add(pr, x, y);
2004 static void pr_tile_prepare(PixbufRenderer *pr, ImageTile *it)
2011 pixmap = gdk_pixmap_new(((GtkWidget *)pr)->window, pr->tile_width, pr->tile_height, -1);
2013 size = pixmap_calc_size(pixmap);
2014 pr_tile_free_space(pr, size, it);
2016 it->pixmap = pixmap;
2018 pr->tile_cache_size += size;
2021 if ((pr->zoom != 1.0 || pr->source_tiles_enabled || (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf)) ||
2022 pr->orientation != EXIF_ORIENTATION_TOP_LEFT || pr->func_post_process) && !it->pixbuf)
2027 /* I don't think that we need a pixbuf with alpha channel here */
2030 pixbuf = gdk_pixbuf_new(gdk_pixbuf_get_colorspace(pr->pixbuf),
2031 gdk_pixbuf_get_has_alpha(pr->pixbuf),
2032 gdk_pixbuf_get_bits_per_sample(pr->pixbuf),
2033 pr->tile_width, pr->tile_height);
2038 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, pr->tile_width, pr->tile_height);
2041 size = gdk_pixbuf_get_rowstride(pixbuf) * pr->tile_height;
2042 pr_tile_free_space(pr, size, it);
2044 it->pixbuf = pixbuf;
2046 pr->tile_cache_size += size;
2051 *-------------------------------------------------------------------
2053 *-------------------------------------------------------------------
2057 static void pr_tile_coords_map_orientation(PixbufRenderer *pr,
2058 double tile_x, double tile_y, /* coordinates of the tile */
2059 gint image_w, gint image_h,
2060 double tile_w, double tile_h,
2061 double *res_x, double *res_y)
2065 switch (pr->orientation)
2067 case EXIF_ORIENTATION_TOP_LEFT:
2068 /* normal -- nothing to do */
2070 case EXIF_ORIENTATION_TOP_RIGHT:
2072 *res_x = image_w - tile_x - tile_w;
2074 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2076 *res_x = image_w - tile_x - tile_w;
2077 *res_y = image_h - tile_y - tile_h;
2079 case EXIF_ORIENTATION_BOTTOM_LEFT:
2081 *res_y = image_h - tile_y - tile_h;
2083 case EXIF_ORIENTATION_LEFT_TOP:
2087 case EXIF_ORIENTATION_RIGHT_TOP:
2088 /* rotated -90 (270) */
2090 *res_y = image_w - tile_x - tile_w;
2092 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2093 *res_x = image_h - tile_y - tile_h;
2094 *res_y = image_w - tile_x - tile_w;
2096 case EXIF_ORIENTATION_LEFT_BOTTOM:
2098 *res_x = image_h - tile_y - tile_h;
2102 /* The other values are out of range */
2105 // printf("tile coord y:%f, ih:%d, th:%f ry:%f\n", tile_y, image_h, tile_h, *res_x);
2108 static void pr_tile_region_map_orientation(PixbufRenderer *pr,
2109 gint area_x, gint area_y, /* coordinates of the area inside tile */
2110 gint tile_w, gint tile_h,
2111 gint area_w, gint area_h,
2112 gint *res_x, gint *res_y,
2113 gint *res_w, gint *res_h)
2120 switch (pr->orientation)
2122 case EXIF_ORIENTATION_TOP_LEFT:
2123 /* normal -- nothing to do */
2125 case EXIF_ORIENTATION_TOP_RIGHT:
2127 *res_x = tile_w - area_x - area_w;
2129 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2131 *res_x = tile_w - area_x - area_w;
2132 *res_y = tile_h - area_y - area_h;
2134 case EXIF_ORIENTATION_BOTTOM_LEFT:
2136 *res_y = tile_h - area_y - area_h;
2138 case EXIF_ORIENTATION_LEFT_TOP:
2144 case EXIF_ORIENTATION_RIGHT_TOP:
2145 /* rotated -90 (270) */
2147 *res_y = tile_w - area_x - area_w;
2151 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2152 *res_x = tile_h - area_y - area_h;
2153 *res_y = tile_w - area_x - area_w;
2157 case EXIF_ORIENTATION_LEFT_BOTTOM:
2159 *res_x = tile_h - area_y - area_h;
2165 /* The other values are out of range */
2168 // printf("inside y:%d, th:%d, ah:%d ry:%d\n", area_y, tile_h, area_h, *res_x);
2171 static void pr_coords_map_orientation_reverse(PixbufRenderer *pr,
2172 gint area_x, gint area_y,
2173 gint tile_w, gint tile_h,
2174 gint area_w, gint area_h,
2175 gint *res_x, gint *res_y,
2176 gint *res_w, gint *res_h)
2183 switch (pr->orientation)
2185 case EXIF_ORIENTATION_TOP_LEFT:
2186 /* normal -- nothing to do */
2188 case EXIF_ORIENTATION_TOP_RIGHT:
2190 *res_x = tile_w - area_x - area_w;
2192 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2194 *res_x = tile_w - area_x - area_w;
2195 *res_y = tile_h - area_y - area_h;
2197 case EXIF_ORIENTATION_BOTTOM_LEFT:
2199 *res_y = tile_h - area_y - area_h;
2201 case EXIF_ORIENTATION_LEFT_TOP:
2207 case EXIF_ORIENTATION_RIGHT_TOP:
2208 /* rotated -90 (270) */
2209 *res_x = tile_w - area_y - area_h;
2214 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2215 *res_x = tile_w - area_y - area_h;
2216 *res_y = tile_h - area_x - area_w;
2220 case EXIF_ORIENTATION_LEFT_BOTTOM:
2223 *res_y = tile_h - area_x - area_w;
2228 /* The other values are out of range */
2234 static GdkPixbuf *pr_get_spare_tile(PixbufRenderer *pr)
2236 if (!pr->spare_tile) pr->spare_tile = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, pr->tile_width, pr->tile_height);
2237 return pr->spare_tile;
2240 #define COLOR_BYTES 3 /* rgb */
2242 static void pr_tile_rotate_90_clockwise(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2244 GdkPixbuf *src = *tile;
2247 guchar *s_pix, *d_pix;
2249 guchar *ip, *spi, *dpi;
2251 gint tw = pr->tile_width;
2253 srs = gdk_pixbuf_get_rowstride(src);
2254 s_pix = gdk_pixbuf_get_pixels(src);
2255 spi = s_pix + (x * COLOR_BYTES);
2257 dest = pr_get_spare_tile(pr);
2258 drs = gdk_pixbuf_get_rowstride(dest);
2259 d_pix = gdk_pixbuf_get_pixels(dest);
2260 dpi = d_pix + (tw - 1) * COLOR_BYTES;
2262 for (i = y; i < y + h; i++)
2264 sp = spi + (i * srs);
2265 ip = dpi - (i * COLOR_BYTES);
2266 for (j = x; j < x + w; j++)
2268 dp = ip + (j * drs);
2269 memcpy(dp, sp, COLOR_BYTES);
2274 pr->spare_tile = src;
2278 static void pr_tile_rotate_90_counter_clockwise(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2280 GdkPixbuf *src = *tile;
2283 guchar *s_pix, *d_pix;
2285 guchar *ip, *spi, *dpi;
2287 gint th = pr->tile_height;
2289 srs = gdk_pixbuf_get_rowstride(src);
2290 s_pix = gdk_pixbuf_get_pixels(src);
2291 spi = s_pix + (x * COLOR_BYTES);
2293 dest = pr_get_spare_tile(pr);
2294 drs = gdk_pixbuf_get_rowstride(dest);
2295 d_pix = gdk_pixbuf_get_pixels(dest);
2296 dpi = d_pix + (th - 1) * drs;
2298 for (i = y; i < y + h; i++)
2300 sp = spi + (i * srs);
2301 ip = dpi + (i * COLOR_BYTES);
2302 for (j = x; j < x + w; j++)
2304 dp = ip - (j * drs);
2305 memcpy(dp, sp, COLOR_BYTES);
2310 pr->spare_tile = src;
2314 static void pr_tile_mirror_only(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2316 GdkPixbuf *src = *tile;
2319 guchar *s_pix, *d_pix;
2324 gint tw = pr->tile_width;
2326 srs = gdk_pixbuf_get_rowstride(src);
2327 s_pix = gdk_pixbuf_get_pixels(src);
2328 spi = s_pix + (x * COLOR_BYTES);
2330 dest = pr_get_spare_tile(pr);
2331 drs = gdk_pixbuf_get_rowstride(dest);
2332 d_pix = gdk_pixbuf_get_pixels(dest);
2333 dpi = d_pix + (tw - x - 1) * COLOR_BYTES;
2335 for (i = y; i < y + h; i++)
2337 sp = spi + (i * srs);
2338 dp = dpi + (i * drs);
2339 for (j = 0; j < w; j++)
2341 memcpy(dp, sp, COLOR_BYTES);
2347 pr->spare_tile = src;
2351 static void pr_tile_mirror_and_flip(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2353 GdkPixbuf *src = *tile;
2356 guchar *s_pix, *d_pix;
2360 gint tw = pr->tile_width;
2361 gint th = pr->tile_height;
2363 srs = gdk_pixbuf_get_rowstride(src);
2364 s_pix = gdk_pixbuf_get_pixels(src);
2365 spi = s_pix + (x * COLOR_BYTES);
2367 dest = pr_get_spare_tile(pr);
2368 drs = gdk_pixbuf_get_rowstride(dest);
2369 d_pix = gdk_pixbuf_get_pixels(dest);
2370 dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
2372 for (i = y; i < y + h; i++)
2374 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
2375 dp = dpi - (i * drs) - (x * COLOR_BYTES);
2376 for (j = 0; j < w; j++)
2378 memcpy(dp, sp, COLOR_BYTES);
2384 pr->spare_tile = src;
2388 static void pr_tile_flip_only(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2390 GdkPixbuf *src = *tile;
2393 guchar *s_pix, *d_pix;
2397 gint th = pr->tile_height;
2399 srs = gdk_pixbuf_get_rowstride(src);
2400 s_pix = gdk_pixbuf_get_pixels(src);
2401 spi = s_pix + (x * COLOR_BYTES);
2403 dest = pr_get_spare_tile(pr);
2404 drs = gdk_pixbuf_get_rowstride(dest);
2405 d_pix = gdk_pixbuf_get_pixels(dest);
2406 dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
2408 for (i = y; i < y + h; i++)
2410 sp = spi + (i * srs);
2411 dp = dpi - (i * drs);
2412 memcpy(dp, sp, w * COLOR_BYTES);
2415 pr->spare_tile = src;
2419 static void pr_tile_apply_orientation(PixbufRenderer *pr, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
2421 switch (pr->orientation)
2423 case EXIF_ORIENTATION_TOP_LEFT:
2424 /* normal -- nothing to do */
2426 case EXIF_ORIENTATION_TOP_RIGHT:
2429 pr_tile_mirror_only(pr, pixbuf, x, y, w, h);
2432 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2435 pr_tile_mirror_and_flip(pr, pixbuf, x, y, w, h);
2438 case EXIF_ORIENTATION_BOTTOM_LEFT:
2441 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2444 case EXIF_ORIENTATION_LEFT_TOP:
2446 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2447 pr_tile_rotate_90_clockwise(pr, pixbuf, x, pr->tile_height - y - h, w, h);
2450 case EXIF_ORIENTATION_RIGHT_TOP:
2451 /* rotated -90 (270) */
2453 pr_tile_rotate_90_clockwise(pr, pixbuf, x, y, w, h);
2456 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2458 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2459 pr_tile_rotate_90_counter_clockwise(pr, pixbuf, x, pr->tile_height - y - h, w, h);
2462 case EXIF_ORIENTATION_LEFT_BOTTOM:
2465 pr_tile_rotate_90_counter_clockwise(pr, pixbuf, x, y, w, h);
2469 /* The other values are out of range */
2475 static void pr_tile_render(PixbufRenderer *pr, ImageTile *it,
2476 gint x, gint y, gint w, gint h,
2477 gint new_data, gint fast)
2483 if (it->render_todo == TILE_RENDER_NONE && it->pixmap && !new_data) return;
2485 if (it->render_done != TILE_RENDER_ALL)
2491 if (!fast) it->render_done = TILE_RENDER_ALL;
2493 else if (it->render_todo != TILE_RENDER_AREA)
2495 if (!fast) it->render_todo = TILE_RENDER_NONE;
2499 if (!fast) it->render_todo = TILE_RENDER_NONE;
2501 if (new_data) it->blank = FALSE;
2503 pr_tile_prepare(pr, it);
2504 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
2506 box = GTK_WIDGET(pr);
2508 /* FIXME checker colors for alpha should be configurable,
2509 * also should be drawn for blank = TRUE
2514 /* no data, do fast rect fill */
2515 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
2516 0, 0, it->w, it->h);
2518 else if (pr->source_tiles_enabled)
2520 draw = pr_source_tile_render(pr, it, x, y, w, h, new_data, fast);
2522 else if (pr->zoom == 1.0 || pr->scale == 1.0)
2525 double src_x, src_y;
2528 pr_tile_coords_map_orientation(pr, it->x, it->y,
2529 pr->image_width, pr->image_height,
2530 pr->tile_width, pr->tile_height,
2532 pr_tile_region_map_orientation(pr, x, y,
2533 pr->tile_width, pr->tile_height,
2540 gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2541 (double) 0.0 - src_x,
2542 (double) 0.0 - src_y,
2543 1.0, 1.0, GDK_INTERP_NEAREST,
2544 255, it->x + pb_x, it->y + pb_y,
2545 PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
2546 pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2553 if (pr->orientation == EXIF_ORIENTATION_TOP_LEFT && !(pr->func_post_process && !(pr->post_process_slow && fast)))
2555 /* faster, simple, base orientation, no postprocessing */
2556 gdk_draw_pixbuf(it->pixmap,
2557 box->style->fg_gc[GTK_WIDGET_STATE(box)],
2559 it->x + x, it->y + y,
2562 pr->dither_quality, it->x + x, it->y + y);
2566 gdk_pixbuf_copy_area(pr->pixbuf,
2567 src_x + pb_x, src_y + pb_y,
2571 pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2578 double scale_x, scale_y;
2579 double src_x, src_y;
2583 if (pr->image_width == 0 || pr->image_height == 0) return;
2585 scale_x = (double)pr->width / pr->image_width;
2586 scale_y = (double)pr->height / pr->image_height;
2588 pr_tile_coords_map_orientation(pr, it->x / scale_x, it->y /scale_y ,
2589 pr->image_width, pr->image_height,
2590 pr->tile_width / scale_x , pr->tile_height / scale_y,
2592 pr_tile_region_map_orientation(pr, x, y,
2593 pr->tile_width, pr->tile_height,
2598 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
2599 * small sizes for anything but GDK_INTERP_NEAREST
2601 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
2605 gdk_pixbuf_scale(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2606 (double) 0.0 - src_x * scale_x,
2607 (double) 0.0 - src_y * scale_y,
2609 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
2613 gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2614 (double) 0.0 - src_x * scale_x,
2615 (double) 0.0 - src_y * scale_y,
2617 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
2618 255, it->x + pb_x, it->y + pb_y,
2619 PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
2621 pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2625 if (draw && it->pixbuf && !it->blank)
2628 if (pr->func_post_process && !(pr->post_process_slow && fast))
2629 pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
2631 gdk_draw_pixbuf(it->pixmap,
2632 box->style->fg_gc[GTK_WIDGET_STATE(box)],
2637 pr->dither_quality, it->x + x, it->y + y);
2641 /* enable this line for debugging the edges of tiles */
2642 gdk_draw_rectangle(it->pixmap, box->style->white_gc,
2643 FALSE, 0, 0, it->w, it->h);
2644 gdk_draw_rectangle(it->pixmap, box->style->white_gc,
2650 static void pr_tile_expose(PixbufRenderer *pr, ImageTile *it,
2651 gint x, gint y, gint w, gint h,
2652 gint new_data, gint fast)
2656 pr_tile_render(pr, it, x, y, w, h, new_data, fast);
2658 box = GTK_WIDGET(pr);
2660 gdk_draw_drawable(box->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
2662 pr->x_offset + (it->x - pr->x_scroll) + x, pr->y_offset + (it->y - pr->y_scroll) + y, w, h);
2664 if (pr->overlay_list)
2666 pr_overlay_draw(pr, pr->x_offset + (it->x - pr->x_scroll) + x,
2667 pr->y_offset + (it->y - pr->y_scroll) + y,
2674 static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it)
2676 return (it->x + it->w >= pr->x_scroll && it->x < pr->x_scroll + pr->vis_width &&
2677 it->y + it->h >= pr->y_scroll && it->y < pr->y_scroll + pr->vis_height);
2681 *-------------------------------------------------------------------
2683 *-------------------------------------------------------------------
2686 static gint pr_queue_draw_idle_cb(gpointer data)
2688 PixbufRenderer *pr = data;
2692 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
2693 (!pr->draw_queue && !pr->draw_queue_2pass) ||
2694 pr->draw_idle_id == -1)
2696 pr_render_complete_signal(pr);
2698 pr->draw_idle_id = -1;
2704 qd = pr->draw_queue->data;
2705 fast = ((pr->zoom_2pass && pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow);
2711 /* still loading, wait till done (also drops the higher priority) */
2713 pr->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
2714 pr_queue_draw_idle_cb, pr, NULL);
2715 pr->draw_idle_high = FALSE;
2719 qd = pr->draw_queue_2pass->data;
2723 if (GTK_WIDGET_REALIZED(pr))
2725 if (pr_tile_is_visible(pr, qd->it))
2727 pr_tile_expose(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2729 else if (qd->new_data)
2731 /* if new pixel data, and we already have a pixmap, update the tile */
2732 qd->it->blank = FALSE;
2733 if (qd->it->pixmap && qd->it->render_done == TILE_RENDER_ALL)
2735 pr_tile_render(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2743 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
2748 pr_queue_merge(qd->it->qd2, qd);
2754 pr->draw_queue_2pass = g_list_append(pr->draw_queue_2pass, qd);
2765 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
2769 if (!pr->draw_queue && !pr->draw_queue_2pass)
2771 pr_render_complete_signal(pr);
2773 pr->draw_idle_id = -1;
2780 static void pr_queue_list_free(GList *list)
2800 static void pr_queue_clear(PixbufRenderer *pr)
2802 pr_queue_list_free(pr->draw_queue);
2803 pr->draw_queue = NULL;
2805 pr_queue_list_free(pr->draw_queue_2pass);
2806 pr->draw_queue_2pass = NULL;
2808 if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2809 pr->draw_idle_id = -1;
2812 static void pr_queue_merge(QueueData *parent, QueueData *qd)
2814 if (parent->x + parent->w < qd->x + qd->w)
2816 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
2818 if (parent->x > qd->x)
2820 parent->w += parent->x - qd->x;
2824 if (parent->y + parent->h < qd->y + qd->h)
2826 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
2828 if (parent->y > qd->y)
2830 parent->h += parent->y - qd->y;
2834 parent->new_data |= qd->new_data;
2837 static gint pr_clamp_to_visible(PixbufRenderer *pr, gint *x, gint *y, gint *w, gint *h)
2845 vh = pr->vis_height;
2850 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
2853 nx = CLAMP(*x, vx, vx + vw);
2854 nw = CLAMP(*w - (nx - *x), 1, vw);
2856 ny = CLAMP(*y, vy, vy + vh);
2857 nh = CLAMP(*h - (ny - *y), 1, vh);
2867 static gint pr_queue_to_tiles(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2868 gint clamp, ImageTileRenderType render, gint new_data, gint only_existing)
2874 if (clamp && !pr_clamp_to_visible(pr, &x, &y, &w, &h)) return FALSE;
2876 x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
2877 x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
2879 y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
2880 y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
2882 for (j = y1; j <= y2; j += pr->tile_height)
2884 for (i = x1; i <= x2; i += pr->tile_width)
2888 it = pr_tile_get(pr, i, j,
2890 (i + pr->tile_width < pr->x_scroll ||
2891 i > pr->x_scroll + pr->vis_width ||
2892 j + pr->tile_height < pr->y_scroll ||
2893 j > pr->y_scroll + pr->vis_height)));
2898 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
2899 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
2901 it->render_todo = render;
2904 qd = g_new(QueueData, 1);
2906 qd->new_data = new_data;
2916 qd->w = x + w - i - qd->x;
2917 if (qd->x + qd->w > pr->tile_width) qd->w = pr->tile_width - qd->x;
2927 qd->h = y + h - j - qd->y;
2928 if (qd->y + qd->h > pr->tile_height) qd->h = pr->tile_height - qd->y;
2930 if (qd->w < 1 || qd->h < 1)
2936 pr_queue_merge(it->qd, qd);
2942 pr->draw_queue = g_list_append(pr->draw_queue, qd);
2951 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2952 gint clamp, ImageTileRenderType render, gint new_data, gint only_existing)
2956 nx = CLAMP(x, 0, pr->width - 1);
2957 ny = CLAMP(y, 0, pr->height - 1);
2960 w = CLAMP(w, 0, pr->width - nx);
2961 h = CLAMP(h, 0, pr->height - ny);
2962 if (w < 1 || h < 1) return;
2964 if (pr_queue_to_tiles(pr, nx, ny, w, h, clamp, render, new_data, only_existing) &&
2965 ((!pr->draw_queue && !pr->draw_queue_2pass) || pr->draw_idle_id == -1 || !pr->draw_idle_high))
2967 if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2968 pr->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW,
2969 pr_queue_draw_idle_cb, pr, NULL);
2970 pr->draw_idle_high = TRUE;
2974 static void pr_redraw(PixbufRenderer *pr, gint new_data)
2977 pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, new_data, FALSE);
2981 *-------------------------------------------------------------------
2983 *-------------------------------------------------------------------
2986 static void pr_update_signal(PixbufRenderer *pr)
2989 printf("FIXME: send updated signal\n");
2991 DEBUG_1("%s pixbuf renderer updated - started drawing %p", get_exec_time(), pr);
2992 pr->debug_updated = TRUE;
2995 static void pr_zoom_signal(PixbufRenderer *pr)
2997 g_signal_emit(pr, signals[SIGNAL_ZOOM], 0, pr->zoom);
3000 static void pr_clicked_signal(PixbufRenderer *pr, GdkEventButton *bevent)
3002 g_signal_emit(pr, signals[SIGNAL_CLICKED], 0, bevent);
3005 static void pr_scroll_notify_signal(PixbufRenderer *pr)
3007 g_signal_emit(pr, signals[SIGNAL_SCROLL_NOTIFY], 0);
3010 static void pr_render_complete_signal(PixbufRenderer *pr)
3014 g_signal_emit(pr, signals[SIGNAL_RENDER_COMPLETE], 0);
3015 g_object_set(G_OBJECT(pr), "complete", TRUE, NULL);
3017 if (pr->debug_updated)
3019 DEBUG_1("%s pixbuf renderer done %p", get_exec_time(), pr);
3020 pr->debug_updated = FALSE;
3024 static void pr_drag_signal(PixbufRenderer *pr, GdkEventButton *bevent)
3026 g_signal_emit(pr, signals[SIGNAL_DRAG], 0, bevent);
3030 *-------------------------------------------------------------------
3032 *-------------------------------------------------------------------
3035 static gint pr_scroll_clamp(PixbufRenderer *pr)
3040 if (pr->zoom == 0.0)
3048 old_xs = pr->x_scroll;
3049 old_ys = pr->y_scroll;
3051 if (pr->x_offset > 0)
3057 pr->x_scroll = CLAMP(pr->x_scroll, 0, pr->width - pr->vis_width);
3060 if (pr->y_offset > 0)
3066 pr->y_scroll = CLAMP(pr->y_scroll, 0, pr->height - pr->vis_height);
3069 return (old_xs != pr->x_scroll || old_ys != pr->y_scroll);
3072 static gint pr_size_clamp(PixbufRenderer *pr)
3074 gint old_vw, old_vh;
3076 old_vw = pr->vis_width;
3077 old_vh = pr->vis_height;
3079 if (pr->width < pr->window_width)
3081 pr->vis_width = pr->width;
3082 pr->x_offset = (pr->window_width - pr->width) / 2;
3086 pr->vis_width = pr->window_width;
3090 if (pr->height < pr->window_height)
3092 pr->vis_height = pr->height;
3093 pr->y_offset = (pr->window_height - pr->height) / 2;
3097 pr->vis_height = pr->window_height;
3101 return (old_vw != pr->vis_width || old_vh != pr->vis_height);
3104 static gint pr_zoom_clamp(PixbufRenderer *pr, gdouble zoom,
3105 gint force, gint new, gint invalidate,
3112 zoom = CLAMP(zoom, pr->zoom_min, pr->zoom_max);
3114 if (pr->zoom == zoom && !force) return FALSE;
3116 w = pr->image_width;
3117 h = pr->image_height;
3119 if (zoom == 0.0 && !pr->pixbuf)
3123 else if (zoom == 0.0)
3129 sizeable = (new && pr_parent_window_sizable(pr));
3133 max_w = gdk_screen_width();
3134 max_h = gdk_screen_height();
3136 if (pr->window_limit)
3138 max_w = max_w * pr->window_limit_size / 100;
3139 max_h = max_h * pr->window_limit_size / 100;
3144 max_w = pr->window_width;
3145 max_h = pr->window_height;
3148 if ((pr->zoom_expand && !sizeable) || w > max_w || h > max_h)
3150 if ((gdouble)max_w / w > (gdouble)max_h / h)
3152 scale = (gdouble)max_h / h;
3154 w = w * scale + 0.5;
3155 if (w > max_w) w = max_w;
3159 scale = (gdouble)max_w / w;
3161 h = h * scale + 0.5;
3162 if (h > max_h) h = max_h;
3165 if (pr->autofit_limit)
3167 gdouble factor = (gdouble)pr->autofit_limit_size / 100;
3168 w = w * factor + 0.5;
3169 h = h * factor + 0.5;
3170 scale = scale * factor;
3181 else if (zoom > 0.0) /* zoom orig, in */
3189 scale = 1.0 / (0.0 - zoom);
3194 invalid = (pr->width != w || pr->height != h);
3201 if (invalidate || invalid)
3203 pr_tile_invalidate_all(pr);
3204 pr_redraw(pr, TRUE);
3206 if (redrawn) *redrawn = (invalidate || invalid);
3211 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
3212 gint force, gint new,
3213 gint center_point, gint px, gint py)
3216 gint old_cx, old_cy;
3219 gint redrawn = FALSE;
3221 old_scale = pr->scale;
3224 px = CLAMP(px, 0, pr->width);
3225 py = CLAMP(py, 0, pr->height);
3226 old_cx = pr->x_scroll + (px - pr->x_offset);
3227 old_cy = pr->y_scroll + (py - pr->y_offset);
3232 old_cx = pr->x_scroll + pr->vis_width / 2;
3233 old_cy = pr->y_scroll + pr->vis_height / 2;
3236 if (!pr_zoom_clamp(pr, zoom, force, new, force, &redrawn)) return;
3238 clamped = pr_size_clamp(pr);
3239 sized = pr_parent_window_resize(pr, pr->width, pr->height);
3243 switch (pr->scroll_reset)
3245 case PR_SCROLL_RESET_NOCHANGE:
3246 /* maintain old scroll position, do nothing */
3248 case PR_SCROLL_RESET_CENTER:
3249 /* center new image */
3250 pr->x_scroll = ((double)pr->image_width / 2.0 * pr->scale) - pr->vis_width / 2;
3251 pr->y_scroll = ((double)pr->image_height / 2.0 * pr->scale) - pr->vis_height / 2;
3253 case PR_SCROLL_RESET_TOPLEFT:
3255 /* reset to upper left */
3263 /* user zoom does not force, so keep visible center point */
3266 pr->x_scroll = old_cx / old_scale * pr->scale - (px - pr->x_offset);
3267 pr->y_scroll = old_cy / old_scale * pr->scale - (py - pr->y_offset);
3271 pr->x_scroll = old_cx / old_scale * pr->scale - (pr->vis_width / 2);
3272 pr->y_scroll = old_cy / old_scale * pr->scale - (pr->vis_height / 2);
3276 pr_scroll_clamp(pr);
3278 /* If the window was not sized, redraw the image - we know there will be no size/expose signal.
3279 * But even if a size is claimed, there is no guarantee that the window manager will allow it,
3280 * so redraw the window anyway :/
3282 if (sized || clamped) pr_border_clear(pr);
3283 pr_redraw(pr, redrawn);
3285 pr_scroll_notify_signal(pr);
3287 pr_update_signal(pr);
3290 static void pr_size_sync(PixbufRenderer *pr, gint new_width, gint new_height)
3292 gint zoom_changed = FALSE;
3294 if (pr->window_width == new_width && pr->window_height == new_height) return;
3296 pr->window_width = new_width;
3297 pr->window_height = new_height;
3299 if (pr->zoom == 0.0)
3301 gdouble old_scale = pr->scale;
3302 pr_zoom_clamp(pr, 0.0, TRUE, FALSE, FALSE, NULL);
3303 zoom_changed = (old_scale != pr->scale);
3307 pr_scroll_clamp(pr);
3309 pr_overlay_update_sizes(pr);
3311 /* ensure scroller remains visible */
3312 if (pr->scroller_overlay != -1)
3314 gint update = FALSE;
3316 if (pr->scroller_x > new_width)
3318 pr->scroller_x = new_width;
3319 pr->scroller_xpos = new_width;
3322 if (pr->scroller_y > new_height)
3324 pr->scroller_y = new_height;
3325 pr->scroller_ypos = new_height;
3333 if (pixbuf_renderer_overlay_get(pr, pr->scroller_overlay, &pixbuf, NULL, NULL))
3337 w = gdk_pixbuf_get_width(pixbuf);
3338 h = gdk_pixbuf_get_height(pixbuf);
3339 pixbuf_renderer_overlay_set(pr, pr->scroller_overlay, pixbuf,
3340 pr->scroller_x - w / 2, pr->scroller_y - h / 2);
3345 pr_border_clear(pr);
3347 pr_scroll_notify_signal(pr);
3348 if (zoom_changed) pr_zoom_signal(pr);
3349 pr_update_signal(pr);
3352 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
3354 PixbufRenderer *pr = data;
3356 pr_size_sync(pr, allocation->width, allocation->height);
3359 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area)
3363 pr_border_draw(pr, area->x, area->y, area->width, area->height);
3365 x = MAX(0, (gint)area->x - pr->x_offset + pr->x_scroll);
3366 y = MAX(0, (gint)area->y - pr->y_offset + pr->y_scroll);
3369 MIN((gint)area->width, pr->width - x),
3370 MIN((gint)area->height, pr->height - y),
3371 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
3375 *-------------------------------------------------------------------
3377 *-------------------------------------------------------------------
3380 void pixbuf_renderer_scroll(PixbufRenderer *pr, gint x, gint y)
3386 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3388 if (!pr->pixbuf && !pr->source_tiles_enabled) return;
3390 old_x = pr->x_scroll;
3391 old_y = pr->y_scroll;
3396 pr_scroll_clamp(pr);
3397 if (pr->x_scroll == old_x && pr->y_scroll == old_y) return;
3399 pr_scroll_notify_signal(pr);
3401 x_off = pr->x_scroll - old_x;
3402 y_off = pr->y_scroll - old_y;
3404 w = pr->vis_width - abs(x_off);
3405 h = pr->vis_height - abs(y_off);
3409 /* scrolled completely to new material */
3410 pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3443 box = GTK_WIDGET(pr);
3445 gc = gdk_gc_new(box->window);
3446 gdk_gc_set_exposures(gc, TRUE);
3447 gdk_draw_drawable(box->window, gc,
3449 x2 + pr->x_offset, y2 + pr->y_offset,
3450 x1 + pr->x_offset, y1 + pr->y_offset, w, h);
3453 if (pr->overlay_list)
3455 pr_overlay_queue_all(pr);
3458 w = pr->vis_width - w;
3459 h = pr->vis_height - h;
3464 x_off > 0 ? pr->x_scroll + (pr->vis_width - w) : pr->x_scroll, pr->y_scroll,
3465 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3469 /* FIXME, to optimize this, remove overlap */
3471 pr->x_scroll, y_off > 0 ? pr->y_scroll + (pr->vis_height - h) : pr->y_scroll,
3472 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3475 /* process exposures here, "expose_event" seems to miss a few with obstructed windows */
3476 while ((event = gdk_event_get_graphics_expose(box->window)) != NULL)
3478 pixbuf_renderer_paint(pr, &event->expose.area);
3480 if (event->expose.count == 0)
3482 gdk_event_free(event);
3485 gdk_event_free(event);
3490 void pixbuf_renderer_scroll_to_point(PixbufRenderer *pr, gint x, gint y,
3491 gdouble x_align, gdouble y_align)
3496 x_align = CLAMP(x_align, 0.0, 1.0);
3497 y_align = CLAMP(y_align, 0.0, 1.0);
3499 ax = (gdouble)pr->vis_width * x_align;
3500 ay = (gdouble)pr->vis_height * y_align;
3502 px = (gdouble)x * pr->scale - (pr->x_scroll + ax);
3503 py = (gdouble)y * pr->scale - (pr->y_scroll + ay);
3505 pixbuf_renderer_scroll(pr, px, py);
3508 /* get or set coordinates of viewport center in the image, in range 0.0 - 1.0 */
3510 void pixbuf_renderer_get_scroll_center(PixbufRenderer *pr, gdouble *x, gdouble *y)
3514 src_x = pr->x_scroll + pr->vis_width / 2;
3515 src_y = pr->y_scroll + pr->vis_height / 2;
3517 *x = (gdouble)src_x / pr->width;
3518 *y = (gdouble)src_y / pr->height;
3521 void pixbuf_renderer_set_scroll_center(PixbufRenderer *pr, gdouble x, gdouble y)
3523 gdouble dst_x, dst_y;
3525 dst_x = x * pr->width - pr->vis_width / 2 - pr->x_scroll + CLAMP(pr->subpixel_x_scroll, -1.0, 1.0);
3526 dst_y = y * pr->height - pr->vis_height / 2 - pr->y_scroll + CLAMP(pr->subpixel_y_scroll, -1.0, 1.0);
3528 pr->subpixel_x_scroll = dst_x - (int)dst_x;
3529 pr->subpixel_y_scroll = dst_y - (int)dst_y;
3531 pixbuf_renderer_scroll(pr, (int)dst_x, (int)dst_y);
3536 *-------------------------------------------------------------------
3538 *-------------------------------------------------------------------
3541 static gint pr_mouse_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3546 pr = PIXBUF_RENDERER(widget);
3548 if (pr->scroller_id != -1)
3550 pr->scroller_xpos = bevent->x;
3551 pr->scroller_ypos = bevent->y;
3554 if (!pr->in_drag || !gdk_pointer_is_grabbed()) return FALSE;
3556 if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
3562 widget_set_cursor(widget, GDK_FLEUR);
3565 if (bevent->state & GDK_SHIFT_MASK)
3567 accel = PR_PAN_SHIFT_MULTIPLIER;
3575 pixbuf_renderer_scroll(pr, (pr->drag_last_x - bevent->x) * accel,
3576 (pr->drag_last_y - bevent->y) * accel);
3578 pr_drag_signal(pr, bevent);
3580 pr->drag_last_x = bevent->x;
3581 pr->drag_last_y = bevent->y;
3586 static gint pr_mouse_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3591 pr = PIXBUF_RENDERER(widget);
3593 if (pr->scroller_id != -1) return TRUE;
3595 switch (bevent->button)
3597 case MOUSE_BUTTON_LEFT:
3599 pr->drag_last_x = bevent->x;
3600 pr->drag_last_y = bevent->y;
3602 gdk_pointer_grab(widget->window, FALSE,
3603 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
3604 NULL, NULL, bevent->time);
3605 gtk_grab_add(widget);
3607 case MOUSE_BUTTON_MIDDLE:
3610 case MOUSE_BUTTON_RIGHT:
3611 pr_clicked_signal(pr, bevent);
3617 parent = gtk_widget_get_parent(widget);
3618 if (widget && GTK_WIDGET_CAN_FOCUS(parent))
3620 gtk_widget_grab_focus(parent);
3626 static gint pr_mouse_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3630 pr = PIXBUF_RENDERER(widget);
3632 if (pr->scroller_id != -1)
3634 pr_scroller_stop(pr);
3638 if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB(pr))
3640 gtk_grab_remove(widget);
3641 gdk_pointer_ungrab(bevent->time);
3642 widget_set_cursor(widget, -1);
3645 if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
3647 if (bevent->button == MOUSE_BUTTON_LEFT && (bevent->state & GDK_SHIFT_MASK))
3649 pr_scroller_start(pr, bevent->x, bevent->y);
3651 else if (bevent->button == MOUSE_BUTTON_LEFT || bevent->button == MOUSE_BUTTON_MIDDLE)
3653 pr_clicked_signal(pr, bevent);
3657 pr->in_drag = FALSE;
3662 static gint pr_mouse_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
3666 pr = PIXBUF_RENDERER(widget);
3668 if (pr->scroller_id != -1)
3670 pr->scroller_xpos = pr->scroller_x;
3671 pr->scroller_ypos = pr->scroller_y;
3672 pr->scroller_xinc = 0;
3673 pr->scroller_yinc = 0;
3679 static void pr_mouse_drag_cb(GtkWidget *widget, GdkDragContext *context, gpointer data)
3683 pr = PIXBUF_RENDERER(widget);
3685 pr->drag_moved = PR_DRAG_SCROLL_THRESHHOLD;
3688 static void pr_signals_connect(PixbufRenderer *pr)
3690 g_signal_connect(G_OBJECT(pr), "motion_notify_event",
3691 G_CALLBACK(pr_mouse_motion_cb), pr);
3692 g_signal_connect(G_OBJECT(pr), "button_press_event",
3693 G_CALLBACK(pr_mouse_press_cb), pr);
3694 g_signal_connect(G_OBJECT(pr), "button_release_event",
3695 G_CALLBACK(pr_mouse_release_cb), pr);
3696 g_signal_connect(G_OBJECT(pr), "leave_notify_event",
3697 G_CALLBACK(pr_mouse_leave_cb), pr);
3699 gtk_widget_set_events(GTK_WIDGET(pr), GDK_POINTER_MOTION_MASK |
3700 GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
3701 GDK_LEAVE_NOTIFY_MASK);
3703 g_signal_connect(G_OBJECT(pr), "drag_begin",
3704 G_CALLBACK(pr_mouse_drag_cb), pr);
3709 *-------------------------------------------------------------------
3711 *-------------------------------------------------------------------
3713 static void pr_pixbuf_size_sync(PixbufRenderer *pr)
3715 if (!pr->pixbuf) return;
3716 switch (pr->orientation)
3718 case EXIF_ORIENTATION_LEFT_TOP:
3719 case EXIF_ORIENTATION_RIGHT_TOP:
3720 case EXIF_ORIENTATION_RIGHT_BOTTOM:
3721 case EXIF_ORIENTATION_LEFT_BOTTOM:
3722 pr->image_width = gdk_pixbuf_get_height(pr->pixbuf);
3723 pr->image_height = gdk_pixbuf_get_width(pr->pixbuf);
3726 pr->image_width = gdk_pixbuf_get_width(pr->pixbuf);
3727 pr->image_height = gdk_pixbuf_get_height(pr->pixbuf);
3731 static void pr_pixbuf_sync(PixbufRenderer *pr, gdouble zoom)
3737 /* no pixbuf so just clear the window */
3738 pr->image_width = 0;
3739 pr->image_height = 0;
3742 box = GTK_WIDGET(pr);
3744 if (GTK_WIDGET_REALIZED(box))
3746 gdk_window_clear(box->window);
3747 pr_overlay_draw(pr, 0, 0, pr->window_width, pr->window_height, NULL);
3750 pr_update_signal(pr);
3755 pr_pixbuf_size_sync(pr);
3756 pr_zoom_sync(pr, zoom, TRUE, TRUE, FALSE, 0, 0);
3759 static void pr_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
3761 if (pixbuf) g_object_ref(pixbuf);
3762 if (pr->pixbuf) g_object_unref(pr->pixbuf);
3763 pr->pixbuf = pixbuf;
3765 pr_pixbuf_sync(pr, zoom);
3768 void pixbuf_renderer_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
3770 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3772 pr_source_tile_unset(pr);
3774 pr_set_pixbuf(pr, pixbuf, zoom);
3776 pr_update_signal(pr);
3779 GdkPixbuf *pixbuf_renderer_get_pixbuf(PixbufRenderer *pr)
3781 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
3786 void pixbuf_renderer_set_orientation(PixbufRenderer *pr, gint orientation)
3788 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3790 pr->orientation = orientation;
3792 pr_pixbuf_size_sync(pr);
3793 pr_zoom_sync(pr, pr->zoom, TRUE, FALSE, FALSE, 0, 0);
3796 gint pixbuf_renderer_get_orientation(PixbufRenderer *pr)
3799 return pr->orientation;
3802 void pixbuf_renderer_set_post_process_func(PixbufRenderer *pr, PixbufRendererPostProcessFunc func, gpointer user_data, gint slow)
3804 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3806 pr->func_post_process = func;
3807 pr->post_process_user_data = user_data;
3808 pr->post_process_slow = func && slow;
3813 void pixbuf_renderer_move(PixbufRenderer *pr, PixbufRenderer *source)
3816 PixbufRendererScrollResetType scroll_reset;
3818 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3819 g_return_if_fail(IS_PIXBUF_RENDERER(source));
3821 if (pr == source) return;
3823 object = G_OBJECT(pr);
3825 g_object_set(object, "zoom_min", source->zoom_min, NULL);
3826 g_object_set(object, "zoom_max", source->zoom_max, NULL);
3827 g_object_set(object, "loading", source->loading, NULL);
3829 pr->complete = source->complete;
3831 pr->x_scroll = source->x_scroll;
3832 pr->y_scroll = source->y_scroll;
3834 scroll_reset = pr->scroll_reset;
3835 pr->scroll_reset = PR_SCROLL_RESET_NOCHANGE;
3837 pr->func_post_process = source->func_post_process;
3838 pr->post_process_user_data = source->post_process_user_data;
3839 pr->post_process_slow = source->post_process_slow;
3840 pr->orientation = source->orientation;
3842 if (source->source_tiles_enabled)
3844 pr_source_tile_unset(pr);
3846 pr->source_tiles_enabled = source->source_tiles_enabled;
3847 pr->source_tiles_cache_size = source->source_tiles_cache_size;
3848 pr->source_tile_width = source->source_tile_width;
3849 pr->source_tile_height = source->source_tile_height;
3850 pr->image_width = source->image_width;
3851 pr->image_height = source->image_height;
3853 pr->func_tile_request = source->func_tile_request;
3854 pr->func_tile_dispose = source->func_tile_dispose;
3855 pr->func_tile_data = source->func_tile_data;
3857 pr->source_tiles = source->source_tiles;
3858 source->source_tiles = NULL;
3860 pr_zoom_sync(pr, source->zoom, TRUE, TRUE, FALSE, 0, 0);
3861 pr_redraw(pr, TRUE);
3865 pixbuf_renderer_set_pixbuf(pr, source->pixbuf, source->zoom);
3868 pr->scroll_reset = scroll_reset;
3870 pixbuf_renderer_set_pixbuf(source, NULL, source->zoom);
3871 pr_queue_clear(source);
3872 pr_tile_free_all(source);
3875 void pixbuf_renderer_area_changed(PixbufRenderer *pr, gint src_x, gint src_y, gint src_w, gint src_h)
3877 gint x, y, width, height, x1, y1, x2, y2;
3879 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3881 pr_coords_map_orientation_reverse(pr,
3883 pr->image_width, pr->image_height,
3888 if (pr->source_tiles_enabled)
3890 pr_source_tile_changed(pr, x, y, width, height);
3893 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
3895 /* increase region when using a zoom quality that may access surrounding pixels */
3900 x1 = (gint)floor((double)x * pr->scale);
3901 y1 = (gint)floor((double)y * pr->scale);
3902 x2 = (gint)ceil((double)(x + width) * pr->scale);
3903 y2 = (gint)ceil((double)(y + height) * pr->scale);
3905 pr_queue(pr, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
3908 void pixbuf_renderer_zoom_adjust(PixbufRenderer *pr, gdouble increment)
3910 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3912 pr_zoom_adjust_real(pr, increment, FALSE, 0, 0);
3915 void pixbuf_renderer_zoom_adjust_at_point(PixbufRenderer *pr, gdouble increment, gint x, gint y)
3917 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3919 pr_zoom_adjust_real(pr, increment, TRUE, x, y);
3922 void pixbuf_renderer_zoom_set(PixbufRenderer *pr, gdouble zoom)
3924 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3926 pr_zoom_sync(pr, zoom, FALSE, FALSE, FALSE, 0, 0);
3929 gdouble pixbuf_renderer_zoom_get(PixbufRenderer *pr)
3931 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3936 gdouble pixbuf_renderer_zoom_get_scale(PixbufRenderer *pr)
3938 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3943 void pixbuf_renderer_zoom_set_limits(PixbufRenderer *pr, gdouble min, gdouble max)
3945 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3947 if (min > 1.0 || max < 1.0) return;
3948 if (min < 1.0 && min > -1.0) return;
3949 if (min < -200.0 || max > 200.0) return;
3951 if (pr->zoom_min != min)
3954 g_object_notify(G_OBJECT(pr), "zoom_min");
3956 if (pr->zoom_max != max)
3959 g_object_notify(G_OBJECT(pr), "zoom_max");
3963 gint pixbuf_renderer_get_image_size(PixbufRenderer *pr, gint *width, gint *height)
3965 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3966 g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3968 if (!pr->pixbuf && !pr->source_tiles_enabled)
3975 *width = pr->image_width;
3976 *height = pr->image_height;
3980 gint pixbuf_renderer_get_scaled_size(PixbufRenderer *pr, gint *width, gint *height)
3982 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3983 g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3985 if (!pr->pixbuf && !pr->source_tiles_enabled)
3993 *height = pr->height;
3997 gint pixbuf_renderer_get_visible_rect(PixbufRenderer *pr, GdkRectangle *rect)
3999 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
4000 g_return_val_if_fail(rect != NULL, FALSE);
4002 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
4012 rect->x = (gint)((gdouble)pr->x_scroll / pr->scale);
4013 rect->y = (gint)((gdouble)pr->y_scroll / pr->scale);
4014 rect->width = (gint)((gdouble)pr->vis_width / pr->scale);
4015 rect->height = (gint)((gdouble)pr->vis_height / pr->scale);
4019 gint pixbuf_renderer_get_virtual_rect(PixbufRenderer *pr, GdkRectangle *rect)
4021 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
4022 g_return_val_if_fail(rect != NULL, FALSE);
4024 if ((!pr->pixbuf && !pr->source_tiles_enabled))
4033 rect->x = pr->x_scroll;
4034 rect->y = pr->y_scroll;
4035 rect->width = pr->vis_width;
4036 rect->height = pr->vis_height;