Code

Super duper mega (fun!) commit: replaced encoding=utf-8 with fileencoding=utf-8 in...
[inkscape.git] / src / display / nr-arena-image.cpp
1 #define __NR_ARENA_IMAGE_C__
3 /*
4  * RGBA display list system for inkscape
5  *
6  * Author:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *
9  * Copyright (C) 2001-2002 Lauris Kaplinski
10  * Copyright (C) 2001 Ximian, Inc.
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #include <libnr/nr-compose-transform.h>
16 #include <2geom/transforms.h>
17 #include <libnr/nr-blit.h>
18 #include "../preferences.h"
19 #include "nr-arena-image.h"
20 #include "style.h"
21 #include "display/nr-arena.h"
22 #include "display/nr-filter.h"
23 #include "display/nr-filter-gaussian.h"
24 #include "sp-filter.h"
25 #include "sp-filter-reference.h"
26 #include "sp-gaussian-blur.h"
27 #include "filters/blend.h"
28 #include "display/nr-filter-blend.h"
30 int nr_arena_image_x_sample = 1;
31 int nr_arena_image_y_sample = 1;
33 /*
34  * NRArenaCanvasImage
35  *
36  */
38 static void nr_arena_image_class_init (NRArenaImageClass *klass);
39 static void nr_arena_image_init (NRArenaImage *image);
40 static void nr_arena_image_finalize (NRObject *object);
42 static unsigned int nr_arena_image_update (NRArenaItem *item, NRRectL *area, NRGC *gc, unsigned int state, unsigned int reset);
43 static unsigned int nr_arena_image_render (cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
44 static NRArenaItem *nr_arena_image_pick (NRArenaItem *item, Geom::Point p, double delta, unsigned int sticky);
46 static NRArenaItemClass *parent_class;
48 NRType
49 nr_arena_image_get_type (void)
50 {
51     static NRType type = 0;
52     if (!type) {
53         type = nr_object_register_type (NR_TYPE_ARENA_ITEM,
54                                         "NRArenaImage",
55                                         sizeof (NRArenaImageClass),
56                                         sizeof (NRArenaImage),
57                                         (void (*) (NRObjectClass *)) nr_arena_image_class_init,
58                                         (void (*) (NRObject *)) nr_arena_image_init);
59     }
60     return type;
61 }
63 static void
64 nr_arena_image_class_init (NRArenaImageClass *klass)
65 {
66     NRObjectClass *object_class;
67     NRArenaItemClass *item_class;
69     object_class = (NRObjectClass *) klass;
70     item_class = (NRArenaItemClass *) klass;
72     parent_class = (NRArenaItemClass *) ((NRObjectClass *) klass)->parent;
74     object_class->finalize = nr_arena_image_finalize;
75     object_class->cpp_ctor = NRObject::invoke_ctor<NRArenaImage>;
77     item_class->update = nr_arena_image_update;
78     item_class->render = nr_arena_image_render;
79     item_class->pick = nr_arena_image_pick;
80 }
82 static void
83 nr_arena_image_init (NRArenaImage *image)
84 {
85     image->px = NULL;
87     image->pxw = image->pxh = image->pxrs = 0;
88     image->x = image->y = 0.0;
89     image->width = 256.0;
90     image->height = 256.0;
92     image->grid2px.setIdentity();
94     image->style = 0;
95     image->render_opacity = TRUE;
96 }
98 static void
99 nr_arena_image_finalize (NRObject *object)
101     NRArenaImage *image = NR_ARENA_IMAGE (object);
103     image->px = NULL;
105     ((NRObjectClass *) parent_class)->finalize (object);
108 static unsigned int
109 nr_arena_image_update( NRArenaItem *item, NRRectL */*area*/, NRGC *gc, unsigned int /*state*/, unsigned int /*reset*/ )
111     Geom::Matrix grid2px;
113     // clear old bbox
114     nr_arena_item_request_render(item);
116     NRArenaImage *image = NR_ARENA_IMAGE (item);
118     /* Copy affine */
119     grid2px = gc->transform.inverse();
120     double hscale, vscale; // todo: replace with Geom::Scale
121     if (image->px) {
122         hscale = image->pxw / image->width;
123         vscale = image->pxh / image->height;
124     } else {
125         hscale = 1.0;
126         vscale = 1.0;
127     }
129     image->grid2px[0] = grid2px[0] * hscale;
130     image->grid2px[2] = grid2px[2] * hscale;
131     image->grid2px[4] = grid2px[4] * hscale;
132     image->grid2px[1] = grid2px[1] * vscale;
133     image->grid2px[3] = grid2px[3] * vscale;
134     image->grid2px[5] = grid2px[5] * vscale;
136     image->grid2px[4] -= image->x * hscale;
137     image->grid2px[5] -= image->y * vscale;
139     /* Calculate bbox */
140     if (image->px) {
141         NRRect bbox;
143         bbox.x0 = image->x;
144         bbox.y0 = image->y;
145         bbox.x1 = image->x + image->width;
146         bbox.y1 = image->y + image->height;
148         image->c00 = (Geom::Point(bbox.x0, bbox.y0) * gc->transform);
149         image->c01 = (Geom::Point(bbox.x0, bbox.y1) * gc->transform);
150         image->c10 = (Geom::Point(bbox.x1, bbox.y0) * gc->transform);
151         image->c11 = (Geom::Point(bbox.x1, bbox.y1) * gc->transform);
153         nr_rect_d_matrix_transform (&bbox, &bbox, gc->transform);
155         item->bbox.x0 = static_cast<NR::ICoord>(floor(bbox.x0)); // Floor gives the coordinate in which the point resides
156         item->bbox.y0 = static_cast<NR::ICoord>(floor(bbox.y0));
157         item->bbox.x1 = static_cast<NR::ICoord>(ceil (bbox.x1)); // Ceil gives the first coordinate beyond the point
158         item->bbox.y1 = static_cast<NR::ICoord>(ceil (bbox.y1));
159     } else {
160         item->bbox.x0 = (int) gc->transform[4];
161         item->bbox.y0 = (int) gc->transform[5];
162         item->bbox.x1 = item->bbox.x0 - 1;
163         item->bbox.y1 = item->bbox.y0 - 1;
164     }
166     return NR_ARENA_ITEM_STATE_ALL;
169 #define FBITS 12
170 #define b2i (image->grid2px)
172 static unsigned int
173 nr_arena_image_render( cairo_t *ct, NRArenaItem *item, NRRectL */*area*/, NRPixBlock *pb, unsigned int /*flags*/ )
175     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
176     nr_arena_image_x_sample = prefs->getInt("/options/bitmapoversample/value", 1);
177     nr_arena_image_y_sample = nr_arena_image_x_sample;
179     bool outline = (item->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
181     NRArenaImage *image = NR_ARENA_IMAGE (item);
183     Geom::Matrix d2s;
185     d2s[0] = b2i[0];
186     d2s[1] = b2i[1];
187     d2s[2] = b2i[2];
188     d2s[3] = b2i[3];
189     d2s[4] = b2i[0] * pb->area.x0 + b2i[2] * pb->area.y0 + b2i[4];
190     d2s[5] = b2i[1] * pb->area.x0 + b2i[3] * pb->area.y0 + b2i[5];
192     if (!outline) {
194         if (!image->px) return item->state;
196         guint32 Falpha = item->opacity;
197         if (Falpha < 1) return item->state;
199         unsigned char * dpx = NR_PIXBLOCK_PX (pb);
200         int const drs = pb->rs;
201         int const dw = pb->area.x1 - pb->area.x0;
202         int const dh = pb->area.y1 - pb->area.y0;
204         unsigned char * spx = image->px;
205         int const srs = image->pxrs;
206         int const sw = image->pxw;
207         int const sh = image->pxh;
209         if (pb->mode == NR_PIXBLOCK_MODE_R8G8B8) {
210             /* fixme: This is not implemented yet (Lauris) */
211             /* nr_R8G8B8_R8G8B8_R8G8B8A8_N_TRANSFORM (dpx, dw, dh, drs, spx, sw, sh, srs, d2s, Falpha, nr_arena_image_x_sample, nr_arena_image_y_sample); */
212         } else if (pb->mode == NR_PIXBLOCK_MODE_R8G8B8A8P) {
213             nr_R8G8B8A8_P_R8G8B8A8_P_R8G8B8A8_N_TRANSFORM (dpx, dw, dh, drs, spx, sw, sh, srs, d2s, Falpha, nr_arena_image_x_sample, nr_arena_image_y_sample);
214         } else if (pb->mode == NR_PIXBLOCK_MODE_R8G8B8A8N) {
215             nr_R8G8B8A8_N_R8G8B8A8_N_R8G8B8A8_N_TRANSFORM (dpx, dw, dh, drs, spx, sw, sh, srs, d2s, Falpha, nr_arena_image_x_sample, nr_arena_image_y_sample);
216         }
218         pb->empty = FALSE;
220     } else { // outline; draw a rect instead
222         if (!ct)
223             return item->state;
225         guint32 rgba = prefs->getInt("/options/wireframecolors/images", 0xff0000ff);
226         // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat
227         // by setting color channels in the "wrong" order
228         cairo_set_source_rgba(ct, SP_RGBA32_B_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_R_F(rgba), SP_RGBA32_A_F(rgba));
230         cairo_set_line_width(ct, 0.5);
231         cairo_new_path(ct);
233         Geom::Point shift(pb->area.x0, pb->area.y0);
234         Geom::Point c00 = image->c00 - shift;
235         Geom::Point c01 = image->c01 - shift;
236         Geom::Point c11 = image->c11 - shift;
237         Geom::Point c10 = image->c10 - shift;
239         cairo_move_to (ct, c00[Geom::X], c00[Geom::Y]);
241         // the box
242         cairo_line_to (ct, c10[Geom::X], c10[Geom::Y]);
243         cairo_line_to (ct, c11[Geom::X], c11[Geom::Y]);
244         cairo_line_to (ct, c01[Geom::X], c01[Geom::Y]);
245         cairo_line_to (ct, c00[Geom::X], c00[Geom::Y]);
246         // the diagonals
247         cairo_line_to (ct, c11[Geom::X], c11[Geom::Y]);
248         cairo_move_to (ct, c10[Geom::X], c10[Geom::Y]);
249         cairo_line_to (ct, c01[Geom::X], c01[Geom::Y]);
251         cairo_stroke(ct);
253         pb->empty = FALSE;
254     }
256     return item->state;
259 /** Calculates the closest distance from p to the segment a1-a2*/
260 double
261 distance_to_segment (Geom::Point p, Geom::Point a1, Geom::Point a2)
263     // calculate sides of the triangle and their squares
264     double d1 = Geom::L2(p - a1);
265     double d1_2 = d1 * d1;
266     double d2 = Geom::L2(p - a2);
267     double d2_2 = d2 * d2;
268     double a = Geom::L2(a1 - a2);
269     double a_2 = a * a;
271     // if one of the angles at the base is > 90, return the corresponding side
272     if (d1_2 + a_2 <= d2_2) return d1;
273     if (d2_2 + a_2 <= d1_2) return d2;
275     // otherwise calculate the height to the base
276     double peri = (a + d1 + d2)/2;
277     return (2*sqrt(peri * (peri - a) * (peri - d1) * (peri - d2))/a);
280 static NRArenaItem *
281 nr_arena_image_pick( NRArenaItem *item, Geom::Point p, double delta, unsigned int /*sticky*/ )
283     NRArenaImage *image = NR_ARENA_IMAGE (item);
285     if (!image->px) return NULL;
287     bool outline = (item->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
289     if (outline) {
291         // frame
292         if (distance_to_segment (p, image->c00, image->c10) < delta) return item;
293         if (distance_to_segment (p, image->c10, image->c11) < delta) return item;
294         if (distance_to_segment (p, image->c11, image->c01) < delta) return item;
295         if (distance_to_segment (p, image->c01, image->c00) < delta) return item;
297         // diagonals
298         if (distance_to_segment (p, image->c00, image->c11) < delta) return item;
299         if (distance_to_segment (p, image->c10, image->c01) < delta) return item;
301         return NULL;
303     } else {
305         unsigned char *const pixels = image->px;
306         int const width = image->pxw;
307         int const height = image->pxh;
308         int const rowstride = image->pxrs;
309         Geom::Point tp = p * image->grid2px;
310         int const ix = (int)(tp[Geom::X]);
311         int const iy = (int)(tp[Geom::Y]);
313         if ((ix < 0) || (iy < 0) || (ix >= width) || (iy >= height))
314             return NULL;
316         unsigned char *pix_ptr = pixels + iy * rowstride + ix * 4;
317         // is the alpha not transparent?
318         return (pix_ptr[3] > 0) ? item : NULL;
319     }
322 /* Utility */
324 void
325 nr_arena_image_set_pixels (NRArenaImage *image, unsigned char const *px, unsigned int pxw, unsigned int pxh, unsigned int pxrs)
327     nr_return_if_fail (image != NULL);
328     nr_return_if_fail (NR_IS_ARENA_IMAGE (image));
330     image->px = (unsigned char *) px;
331     image->pxw = pxw;
332     image->pxh = pxh;
333     image->pxrs = pxrs;
335     nr_arena_item_request_update (NR_ARENA_ITEM (image), NR_ARENA_ITEM_STATE_ALL, FALSE);
338 void
339 nr_arena_image_set_geometry (NRArenaImage *image, double x, double y, double width, double height)
341     nr_return_if_fail (image != NULL);
342     nr_return_if_fail (NR_IS_ARENA_IMAGE (image));
344     image->x = x;
345     image->y = y;
346     image->width = width;
347     image->height = height;
349     nr_arena_item_request_update (NR_ARENA_ITEM (image), NR_ARENA_ITEM_STATE_ALL, FALSE);
352 void nr_arena_image_set_style (NRArenaImage *image, SPStyle *style)
354     g_return_if_fail(image != NULL);
355     g_return_if_fail(NR_IS_ARENA_IMAGE(image));
357     if (style) sp_style_ref(style);
358     if (image->style) sp_style_unref(image->style);
359     image->style = style;
361     //if image has a filter
362     if (style->filter.set && style->getFilter()) {
363         if (!image->filter) {
364             int primitives = sp_filter_primitive_count(SP_FILTER(style->getFilter()));
365             image->filter = new Inkscape::Filters::Filter(primitives);
366         }
367         sp_filter_build_renderer(SP_FILTER(style->getFilter()), image->filter);
368     } else {
369         //no filter set for this image
370         delete image->filter;
371         image->filter = NULL;
372     }
374     if (style && style->enable_background.set
375         && style->enable_background.value == SP_CSS_BACKGROUND_NEW) {
376         image->background_new = true;
377     }
379     nr_arena_item_request_update(image, NR_ARENA_ITEM_STATE_ALL, FALSE);
383 /*
384   Local Variables:
385   mode:c++
386   c-file-style:"stroustrup"
387   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
388   indent-tabs-mode:nil
389   fill-column:99
390   End:
391 */
392 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :