Code

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