Code

Modified filter primitives' render method to use FilterUnits instead of Matrix
[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  *
7  * Copyright (C) 2007 authors
8  *
9  * Released under GNU GPL, read the file 'COPYING' for more information
10  */
11 #include "display/nr-arena-item.h"
12 #include "display/nr-filter.h"
13 #include "display/nr-filter-image.h"
14 #include "display/nr-filter-units.h"
16 namespace NR {
18 FilterImage::FilterImage()
19 {
20 // Testing with hardcoded xlink:href :  
21     image = Gdk::Pixbuf::create_from_file("../images/image1.jpg");
22     //TODO: handle errors
23     width = image->get_width()+1;
24     height = image->get_height()+1;
25     image_pixbuf = image->get_pixels();
26 }
28 FilterPrimitive * FilterImage::create() {
29     return new FilterImage();
30 }
32 FilterImage::~FilterImage()
33 {}
35 int FilterImage::render(FilterSlot &slot, FilterUnits const &units) {
36     int w,x,y;
37     NRPixBlock *in = slot.get(_input);
38     NRPixBlock *out = new NRPixBlock;
40     int x0 = in->area.x0, y0 = in->area.y0;
41     int x1 = in->area.x1, y1 = in->area.y1;
42     int bbox_x0 = (int) slot.get_arenaitem()->bbox.x0;
43     int bbox_y0 = (int) slot.get_arenaitem()->bbox.y0;
44     
45     nr_pixblock_setup_fast(out, in->mode, x0, y0, x1, y1, true);
47     w = x1 - x0;
48     double scaleX = width/feImageWidth;
49     double scaleY = height/feImageHeight;
50     int coordx,coordy;
51     unsigned char *out_data = NR_PIXBLOCK_PX(out);
52     for (x=x0; x < x1; x++){
53         for (y=y0; y < y1; y++){
54             //TODO: use interpolation
55             coordx = int((x - feImageX - bbox_x0)*scaleX);
56             coordy = int((y - feImageY - bbox_y0)*scaleY);
58             if (coordx > 0 && coordx < width && coordy > 0 && coordy < height){
59                 out_data[4*((x - x0)+w*(y - y0))] = (unsigned char) image_pixbuf[3*(coordx + width*coordy)]; //Red
60                 out_data[4*((x - x0)+w*(y - y0)) + 1] = (unsigned char) image_pixbuf[3*(coordx + width*coordy) + 1]; //Green
61                 out_data[4*((x - x0)+w*(y - y0)) + 2] = (unsigned char) image_pixbuf[3*(coordx + width*coordy) + 2]; //Blue
62                 out_data[4*((x - x0)+w*(y - y0)) + 3] = 255; //Alpha
63             }
64         }
65     }
67     out->empty = FALSE;
68     slot.set(_output, out);
69     return 0;
70 }
71 void FilterImage::set_region(SVGLength x, SVGLength y, SVGLength width, SVGLength height){
72         feImageX=x.computed;
73         feImageY=y.computed;
74         feImageWidth=width.computed;
75         feImageHeight=height.computed;
76 }
78 FilterTraits FilterImage::get_input_traits() {
79     return TRAIT_PARALLER;
80 }
81     
82 } /* namespace NR */
84 /*
85   Local Variables:
86   mode:c++
87   c-file-style:"stroustrup"
88   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
89   indent-tabs-mode:nil
90   fill-column:99
91   End:
92 */
93 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :