Code

optimize guide canvasupdating
[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     feImageHref=NULL;
21     image_pixbuf=NULL;
22 }
24 FilterPrimitive * FilterImage::create() {
25     return new FilterImage();
26 }
28 FilterImage::~FilterImage()
29 {
30         if (feImageHref) g_free(feImageHref);
31         if (image_pixbuf) g_free(image_pixbuf);
32 }
34 int FilterImage::render(FilterSlot &slot, FilterUnits const &/*units*/) {
35     if  (!feImageHref) return 0;
37     if (!image_pixbuf){
38             if ( (image = Gdk::Pixbuf::create_from_file(feImageHref)) < 0 ) return 0;
39             width = image->get_width();
40             height = image->get_height();
41             rowstride = image->get_rowstride();
42             image_pixbuf = image->get_pixels();
43     }
44     int w,x,y;
45     NRPixBlock *in = slot.get(_input);
46     NRPixBlock *out = new NRPixBlock;
48     int x0 = in->area.x0, y0 = in->area.y0;
49     int x1 = in->area.x1, y1 = in->area.y1;
50     int bbox_x0 = (int) slot.get_arenaitem()->bbox.x0;
51     int bbox_y0 = (int) slot.get_arenaitem()->bbox.y0;
53     nr_pixblock_setup_fast(out, in->mode, x0, y0, x1, y1, true);
55     w = x1 - x0;
56     double scaleX = width/feImageWidth;
57     double scaleY = height/feImageHeight;
58     int coordx,coordy;
59     unsigned char *out_data = NR_PIXBLOCK_PX(out);
60     for (x=x0; x < x1; x++){
61         for (y=y0; y < y1; y++){
62             //TODO: use interpolation
63             coordx = int((x - feImageX - bbox_x0)*scaleX);
64             coordy = int((y - feImageY - bbox_y0)*scaleY);
66             if (coordx > 0 && coordx < width && coordy > 0 && coordy < height){
67                 out_data[4*((x - x0)+w*(y - y0))] = (unsigned char) image_pixbuf[3*coordx + rowstride*coordy]; //Red
68                 out_data[4*((x - x0)+w*(y - y0)) + 1] = (unsigned char) image_pixbuf[3*coordx + rowstride*coordy + 1]; //Green
69                 out_data[4*((x - x0)+w*(y - y0)) + 2] = (unsigned char) image_pixbuf[3*coordx + rowstride*coordy + 2]; //Blue
70                 out_data[4*((x - x0)+w*(y - y0)) + 3] = 255; //Alpha
71             }
72         }
73     }
75     out->empty = FALSE;
76     slot.set(_output, out);
77     return 0;
78 }
80 void FilterImage::set_href(const gchar *href){
81     if (feImageHref) g_free (feImageHref);
82     feImageHref = (href) ? g_strdup (href) : NULL;
83 }
85 void FilterImage::set_region(SVGLength x, SVGLength y, SVGLength width, SVGLength height){
86         feImageX=x.computed;
87         feImageY=y.computed;
88         feImageWidth=width.computed;
89         feImageHeight=height.computed;
90 }
92 FilterTraits FilterImage::get_input_traits() {
93     return TRAIT_PARALLER;
94 }
96 } /* namespace NR */
98 /*
99   Local Variables:
100   mode:c++
101   c-file-style:"stroustrup"
102   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
103   indent-tabs-mode:nil
104   fill-column:99
105   End:
106 */
107 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :