Code

* remove code duplication from pdf-cairo extension
[inkscape.git] / src / display / nr-filter-image.cpp
1 /*
2  * feImage filter primitive renderer
3  *
4  * Authors:
5  *   Felipe CorrĂȘa da Silva Sanches <felipe.sanches@gmail.com>
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-matrix.h"
20 #include "libnr/nr-rect-l.h"
22 namespace NR {
24 FilterImage::FilterImage()
25 {
26     feImageHref=NULL;
27     image_pixbuf=NULL;
28     document=NULL;
29 }
31 FilterPrimitive * FilterImage::create() {
32     return new FilterImage();
33 }
35 FilterImage::~FilterImage()
36 {
37         if (feImageHref) g_free(feImageHref);
38 }
40 int FilterImage::render(FilterSlot &slot, FilterUnits const &units) {
41     if (!feImageHref) return 0;
43     NRPixBlock* pb = NULL;
44     bool free_pb_on_exit = false;
46     if(from_element){
47         if (!SVGElem) return 0;
48         
49         // prep the document
50         sp_document_ensure_up_to_date(document);
51         NRArena* arena = NRArena::create();
52         unsigned const key = sp_item_display_key_new(1);
53         NRArenaItem* ai = sp_item_invoke_show(SVGElem, arena, key, SP_ITEM_SHOW_DISPLAY);
54         if (!ai) {
55             g_warning("feImage renderer: error creating NRArenaItem for SVG Element");
56             g_free(arena);
57             return 0;
58         }
59         pb = new NRPixBlock;
60         free_pb_on_exit = true;
61         
62         Matrix identity(1.0, 0.0,
63                    0.0, 1.0,
64                    0.0, 0.0);
65         NR::Maybe<NR::Rect> area = SVGElem->getBounds(identity);
66         
67         NRRectL rect;
68         rect.x0=area->min()[NR::X];
69         rect.x1=area->max()[NR::X];
70         rect.y0=area->min()[NR::Y];
71         rect.y1=area->max()[NR::Y];
73         width = (int)(rect.x1-rect.x0);
74         height = (int)(rect.y1-rect.y0);
76         if (image_pixbuf) g_free(image_pixbuf);
77         image_pixbuf = g_new(unsigned char, 4 * width * height);
78         memset(image_pixbuf, 0x00, 4 * width * height);
80         NRGC gc(NULL);
81         /* Update to renderable state */
82         double sf = 1.0;
83         NR::Matrix t(NR::scale(sf, sf));
84         nr_arena_item_set_transform(ai, &t);
85         gc.transform.set_identity();
86         nr_arena_item_invoke_update( ai, NULL, &gc,
87                                              NR_ARENA_ITEM_STATE_ALL,
88                                              NR_ARENA_ITEM_STATE_NONE );
89         nr_pixblock_setup_extern(pb, NR_PIXBLOCK_MODE_R8G8B8A8N,
90                               (int)rect.x0, (int)rect.y0, (int)rect.x1, (int)rect.y1,
91                               image_pixbuf, 4 * width, FALSE, FALSE );
93         nr_arena_item_invoke_render(NULL, ai, &rect, pb, NR_ARENA_ITEM_RENDER_NO_CACHE);
95         nr_arena_item_unref(ai);
96         nr_object_unref((NRObject *) arena);
97     }
100     if (!image_pixbuf){
101         try {
102             gchar *fullname = feImageHref;
103             if ( !g_file_test( fullname, G_FILE_TEST_EXISTS ) ) {
104                 // Try to load from relative postion combined with document base
105                 if( document ) {
106                     fullname = g_build_filename( document->base, feImageHref, NULL );
107                 }
108             }
109             if ( !g_file_test( fullname, G_FILE_TEST_EXISTS ) ) {
110                 // Should display Broken Image png.
111                 g_warning("FilterImage::render: Can not find: %s", feImageHref  );
112             }
113             image = Gdk::Pixbuf::create_from_file(fullname);
114             if( fullname != feImageHref ) g_free( fullname );
115         }
116         catch (const Glib::FileError & e)
117         {
118             g_warning("caught Glib::FileError in FilterImage::render %i", e.code() );
119             return 0;
120         }
121         catch (const Gdk::PixbufError & e)
122         {
123             g_warning("Gdk::PixbufError in FilterImage::render: %i", e.code() );
124             return 0;
125         }
126         if ( !image ) return 0;
128         // Native size of image
129         width = image->get_width();
130         height = image->get_height();
131         rowstride = image->get_rowstride();
132         image_pixbuf = image->get_pixels();
133     }
134     int w,x,y;
135     NRPixBlock *in = slot.get(_input);
136     if (!in) {
137         g_warning("Missing source image for feImage (in=%d)", _input);
138         return 1;
139     }
141     // This section needs to be fully tested!!
143     // Region being drawn on screen
144     int x0 = in->area.x0, y0 = in->area.y0;
145     int x1 = in->area.x1, y1 = in->area.y1;
146     NRPixBlock *out = new NRPixBlock;
147     nr_pixblock_setup_fast(out, in->mode, x0, y0, x1, y1, true);
148     w = x1 - x0;
150     // Get the object bounding box. Image is placed with respect to box.
151     // Array values:  0: width; 3: height; 4: -x; 5: -y.
152     Matrix object_bbox = units.get_matrix_user2filterunits().inverse();
154     // feImage is suppose to use the same parameters as a normal SVG image.
155     // If a width or height is set to zero, the image is not suppose to be displayed.
156     // This does not seem to be what Firefox or Opera does, nor does the W3C displacement
157     // filter test expect this behavior. If the width and/or height are zero, we use
158     // the width and height of the object bounding box.
159     if( feImageWidth  == 0 ) feImageWidth  = object_bbox[0];
160     if( feImageHeight == 0 ) feImageHeight = object_bbox[3];
162     double scaleX = width/feImageWidth;
163     double scaleY = height/feImageHeight;
165     int coordx,coordy;
166     unsigned char *out_data = NR_PIXBLOCK_PX(out);
167     Matrix unit_trans = units.get_matrix_primitiveunits2pb().inverse();
168     for (x=x0; x < x1; x++){
169         for (y=y0; y < y1; y++){
170             //TODO: use interpolation
171             // Temporarily add 0.5 so we sample center of "cell"
172             double indexX = scaleX * (((x+0.5) * unit_trans[0] + unit_trans[4]) - feImageX + object_bbox[4]);
173             double indexY = scaleY * (((y+0.5) * unit_trans[3] + unit_trans[5]) - feImageY + object_bbox[5]);
175             // coordx == 0 and coordy == 0 must be included, but we protect
176             // against negative numbers which round up to 0 with (int).
177             coordx = ( indexX >= 0 ? int( indexX ) : -1 );
178             coordy = ( indexY >= 0 ? int( indexY ) : -1 );
179             if (coordx >= 0 && coordx < width && coordy >= 0 && coordy < height){
180                 if (from_element){
181                     out_data[4*((x - x0)+w*(y - y0))] = (unsigned char) image_pixbuf[4*(coordx + width*coordy)]; //Red
182                     out_data[4*((x - x0)+w*(y - y0)) + 1] = (unsigned char) image_pixbuf[4*(coordx + width*coordy) + 1]; //Green
183                     out_data[4*((x - x0)+w*(y - y0)) + 2] = (unsigned char) image_pixbuf[4*(coordx + width*coordy) + 2]; //Blue
184                     out_data[4*((x - x0)+w*(y - y0)) + 3] = (unsigned char) image_pixbuf[4*(coordx + width*coordy) + 3]; //Alpha
185                 } else {
186                     out_data[4*((x - x0)+w*(y - y0))] = (unsigned char) image_pixbuf[3*coordx + rowstride*coordy]; //Red
187                     out_data[4*((x - x0)+w*(y - y0)) + 1] = (unsigned char) image_pixbuf[3*coordx + rowstride*coordy + 1]; //Green
188                     out_data[4*((x - x0)+w*(y - y0)) + 2] = (unsigned char) image_pixbuf[3*coordx + rowstride*coordy + 2]; //Blue
189                     out_data[4*((x - x0)+w*(y - y0)) + 3] = 255; //Alpha
190                 }
191             }
192         }
193     }
194     if (free_pb_on_exit) nr_pixblock_release(pb);
196     out->empty = FALSE;
197     slot.set(_output, out);
198     return 0;
201 void FilterImage::set_href(const gchar *href){
202     if (feImageHref) g_free (feImageHref);
203     feImageHref = (href) ? g_strdup (href) : NULL;
206 void FilterImage::set_document(SPDocument *doc){
207     document = doc;
210 void FilterImage::set_region(SVGLength x, SVGLength y, SVGLength width, SVGLength height){
211         feImageX=x.computed;
212         feImageY=y.computed;
213         feImageWidth=width.computed;
214         feImageHeight=height.computed;
217 FilterTraits FilterImage::get_input_traits() {
218     return TRAIT_PARALLER;
221 } /* namespace NR */
223 /*
224   Local Variables:
225   mode:c++
226   c-file-style:"stroustrup"
227   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
228   indent-tabs-mode:nil
229   fill-column:99
230   End:
231 */
232 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :