1 /*
2 * feImage filter primitive renderer
3 *
4 * Authors:
5 * Felipe CorrĂȘa da Silva Sanches <juca@members.fsf.org>
6 * Tavmjong Bah <tavmjong@free.fr>
7 * Abhishek Sharma
8 *
9 * Copyright (C) 2007 authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
13 #include "document.h"
14 #include "sp-item.h"
15 #include "display/nr-arena.h"
16 #include "display/nr-arena-item.h"
17 #include "display/nr-filter.h"
18 #include "display/nr-filter-image.h"
19 #include "display/nr-filter-units.h"
20 #include "libnr/nr-compose-transform.h"
21 #include "libnr/nr-rect-l.h"
22 #include "preferences.h"
24 namespace Inkscape {
25 namespace Filters {
27 FilterImage::FilterImage() :
28 SVGElem(0),
29 document(0),
30 feImageHref(0),
31 image_pixbuf(0)
32 { }
34 FilterPrimitive * FilterImage::create() {
35 return new FilterImage();
36 }
38 FilterImage::~FilterImage()
39 {
40 if (feImageHref) g_free(feImageHref);
41 }
43 int FilterImage::render(FilterSlot &slot, FilterUnits const &units) {
44 if (!feImageHref) return 0;
46 NRPixBlock* pb = NULL;
47 bool free_pb_on_exit = false;
49 if(from_element){
50 if (!SVGElem) return 0;
52 // prep the document
53 document->ensureUpToDate();
54 NRArena* arena = NRArena::create();
55 unsigned const key = SPItem::display_key_new(1);
56 NRArenaItem* ai = SVGElem->invoke_show(arena, key, SP_ITEM_SHOW_DISPLAY);
57 if (!ai) {
58 g_warning("feImage renderer: error creating NRArenaItem for SVG Element");
59 nr_object_unref((NRObject *) arena);
60 return 0;
61 }
63 pb = new NRPixBlock;
64 free_pb_on_exit = true;
66 Geom::OptRect area = SVGElem->getBounds(Geom::identity());
68 NRRectL rect;
69 rect.x0=area->min()[Geom::X];
70 rect.x1=area->max()[Geom::X];
71 rect.y0=area->min()[Geom::Y];
72 rect.y1=area->max()[Geom::Y];
74 width = (int)(rect.x1-rect.x0);
75 height = (int)(rect.y1-rect.y0);
76 rowstride = 4*width;
77 has_alpha = true;
79 if (image_pixbuf) g_free(image_pixbuf);
80 image_pixbuf = g_try_new(unsigned char, 4L * width * height);
81 if(image_pixbuf != NULL)
82 {
83 memset(image_pixbuf, 0x00, 4 * width * height);
85 NRGC gc(NULL);
86 /* Update to renderable state */
87 double sf = 1.0;
88 Geom::Matrix t(Geom::Scale(sf, sf));
89 nr_arena_item_set_transform(ai, &t);
90 gc.transform.setIdentity();
91 nr_arena_item_invoke_update( ai, NULL, &gc,
92 NR_ARENA_ITEM_STATE_ALL,
93 NR_ARENA_ITEM_STATE_NONE );
94 nr_pixblock_setup_extern(pb, NR_PIXBLOCK_MODE_R8G8B8A8N,
95 (int)rect.x0, (int)rect.y0, (int)rect.x1, (int)rect.y1,
96 image_pixbuf, 4 * width, FALSE, FALSE );
98 nr_arena_item_invoke_render(NULL, ai, &rect, pb, NR_ARENA_ITEM_RENDER_NO_CACHE);
99 }
100 else
101 {
102 g_warning("FilterImage::render: not enough memory to create pixel buffer. Need %ld.", 4L * width * height);
103 }
104 SVGElem->invoke_hide(key);
105 nr_object_unref((NRObject *) arena);
106 }
109 if (!image_pixbuf){
110 try {
111 /* TODO: If feImageHref is absolute, then use that (preferably handling the
112 * case that it's not a file URI). Otherwise, go up the tree looking
113 * for an xml:base attribute, and use that as the base URI for resolving
114 * the relative feImageHref URI. Otherwise, if document && document->base,
115 * then use that as the base URI. Otherwise, use feImageHref directly
116 * (i.e. interpreting it as relative to our current working directory).
117 * (See http://www.w3.org/TR/xmlbase/#resolution .) */
118 gchar *fullname = feImageHref;
119 if ( !g_file_test( fullname, G_FILE_TEST_EXISTS ) ) {
120 // Try to load from relative postion combined with document base
121 if( document ) {
122 fullname = g_build_filename( document->getBase(), feImageHref, NULL );
123 }
124 }
125 if ( !g_file_test( fullname, G_FILE_TEST_EXISTS ) ) {
126 // Should display Broken Image png.
127 g_warning("FilterImage::render: Can not find: %s", feImageHref );
128 }
129 image = Gdk::Pixbuf::create_from_file(fullname);
130 if( fullname != feImageHref ) g_free( fullname );
131 }
132 catch (const Glib::FileError & e)
133 {
134 g_warning("caught Glib::FileError in FilterImage::render %i", e.code() );
135 return 0;
136 }
137 catch (const Gdk::PixbufError & e)
138 {
139 g_warning("Gdk::PixbufError in FilterImage::render: %i", e.code() );
140 return 0;
141 }
142 if ( !image ) return 0;
144 // Native size of image
145 width = image->get_width();
146 height = image->get_height();
147 rowstride = image->get_rowstride();
148 image_pixbuf = image->get_pixels();
149 has_alpha = image->get_has_alpha();
150 }
151 int w,x,y;
152 NRPixBlock *in = slot.get(_input);
153 if (!in) {
154 g_warning("Missing source image for feImage (in=%d)", _input);
155 return 1;
156 }
158 // This section needs to be fully tested!!
160 // Region being drawn on screen
161 int x0 = in->area.x0, y0 = in->area.y0;
162 int x1 = in->area.x1, y1 = in->area.y1;
163 NRPixBlock *out = new NRPixBlock;
164 nr_pixblock_setup_fast(out, NR_PIXBLOCK_MODE_R8G8B8A8P, x0, y0, x1, y1, true);
165 w = x1 - x0;
167 // Get the object bounding box. Image is placed with respect to box.
168 // Array values: 0: width; 3: height; 4: -x; 5: -y.
169 Geom::Matrix object_bbox = units.get_matrix_user2filterunits().inverse();
171 // feImage is suppose to use the same parameters as a normal SVG image.
172 // If a width or height is set to zero, the image is not suppose to be displayed.
173 // This does not seem to be what Firefox or Opera does, nor does the W3C displacement
174 // filter test expect this behavior. If the width and/or height are zero, we use
175 // the width and height of the object bounding box.
176 if( feImageWidth == 0 ) feImageWidth = object_bbox[0];
177 if( feImageHeight == 0 ) feImageHeight = object_bbox[3];
179 double scaleX = width/feImageWidth;
180 double scaleY = height/feImageHeight;
182 int coordx,coordy;
183 unsigned char *out_data = NR_PIXBLOCK_PX(out);
184 Geom::Matrix unit_trans = units.get_matrix_primitiveunits2pb().inverse();
185 Geom::Matrix d2s = Geom::Translate(x0, y0) * unit_trans * Geom::Translate(object_bbox[4]-feImageX, object_bbox[5]-feImageY) * Geom::Scale(scaleX, scaleY);
187 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
188 int nr_arena_image_x_sample = prefs->getInt("/options/bitmapoversample/value", 1);
189 int nr_arena_image_y_sample = nr_arena_image_x_sample;
191 if (has_alpha) {
192 nr_R8G8B8A8_P_R8G8B8A8_P_R8G8B8A8_N_TRANSFORM(out_data, x1-x0, y1-y0, 4*w, image_pixbuf, width, height, rowstride, d2s, 255, nr_arena_image_x_sample, nr_arena_image_y_sample);
193 } else {
194 for (x=x0; x < x1; x++){
195 for (y=y0; y < y1; y++){
196 //TODO: use interpolation
197 // Temporarily add 0.5 so we sample center of "cell"
198 double indexX = scaleX * (((x+0.5) * unit_trans[0] + unit_trans[4]) - feImageX + object_bbox[4]);
199 double indexY = scaleY * (((y+0.5) * unit_trans[3] + unit_trans[5]) - feImageY + object_bbox[5]);
201 // coordx == 0 and coordy == 0 must be included, but we protect
202 // against negative numbers which round up to 0 with (int).
203 coordx = ( indexX >= 0 ? int( indexX ) : -1 );
204 coordy = ( indexY >= 0 ? int( indexY ) : -1 );
205 if (coordx >= 0 && coordx < width && coordy >= 0 && coordy < height){
206 out_data[4*((x - x0)+w*(y - y0)) ] = (unsigned char) image_pixbuf[3*coordx + rowstride*coordy ]; //Red
207 out_data[4*((x - x0)+w*(y - y0)) + 1] = (unsigned char) image_pixbuf[3*coordx + rowstride*coordy + 1]; //Green
208 out_data[4*((x - x0)+w*(y - y0)) + 2] = (unsigned char) image_pixbuf[3*coordx + rowstride*coordy + 2]; //Blue
209 out_data[4*((x - x0)+w*(y - y0)) + 3] = 255; //Alpha
210 }
211 }
212 }
213 }
214 if (free_pb_on_exit) {
215 nr_pixblock_release(pb);
216 delete pb;
217 }
219 out->empty = FALSE;
220 slot.set(_output, out);
221 return 0;
222 }
224 void FilterImage::set_href(const gchar *href){
225 if (feImageHref) g_free (feImageHref);
226 feImageHref = (href) ? g_strdup (href) : NULL;
227 }
229 void FilterImage::set_document(SPDocument *doc){
230 document = doc;
231 }
233 void FilterImage::set_region(SVGLength x, SVGLength y, SVGLength width, SVGLength height){
234 feImageX=x.computed;
235 feImageY=y.computed;
236 feImageWidth=width.computed;
237 feImageHeight=height.computed;
238 }
240 FilterTraits FilterImage::get_input_traits() {
241 return TRAIT_PARALLER;
242 }
244 } /* namespace Filters */
245 } /* namespace Inkscape */
247 /*
248 Local Variables:
249 mode:c++
250 c-file-style:"stroustrup"
251 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
252 indent-tabs-mode:nil
253 fill-column:99
254 End:
255 */
256 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :