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 }
33 int FilterImage::render(FilterSlot &slot, FilterUnits const &units) {
34 if (!feImageHref) return 0;
36 if (!image_pixbuf){
37 try {
38 image = Gdk::Pixbuf::create_from_file(feImageHref);
39 }
40 catch (const Glib::FileError & e)
41 {
42 g_warning("caught Glib::FileError in FilterImage::render");
43 return 0;
44 }
45 catch (const Gdk::PixbufError & e)
46 {
47 g_warning("Gdk::PixbufError in FilterImage::render");
48 return 0;
49 }
50 if ( !image ) return 0;
52 width = image->get_width();
53 height = image->get_height();
54 rowstride = image->get_rowstride();
55 image_pixbuf = image->get_pixels();
56 }
57 int w,x,y;
58 NRPixBlock *in = slot.get(_input);
59 if (!in) {
60 g_warning("Missing source image for feImage (in=%d)", _input);
61 return 1;
62 }
64 NRPixBlock *out = new NRPixBlock;
66 int x0 = in->area.x0, y0 = in->area.y0;
67 int x1 = in->area.x1, y1 = in->area.y1;
69 Matrix unit_trans = units.get_matrix_primitiveunits2pb().inverse();
71 nr_pixblock_setup_fast(out, in->mode, x0, y0, x1, y1, true);
73 w = x1 - x0;
74 double scaleX = width/feImageWidth;
75 double scaleY = height/feImageHeight;
76 int coordx,coordy;
77 unsigned char *out_data = NR_PIXBLOCK_PX(out);
78 for (x=x0; x < x1; x++){
79 for (y=y0; y < y1; y++){
80 //TODO: use interpolation
81 coordx = int(scaleX * ((x * unit_trans[0] + unit_trans[4]) - feImageX));
82 coordy = int(scaleY * ((y * unit_trans[3] + unit_trans[5]) - feImageY));
84 if (coordx > 0 && coordx < width && coordy > 0 && coordy < height){
85 out_data[4*((x - x0)+w*(y - y0))] = (unsigned char) image_pixbuf[3*coordx + rowstride*coordy]; //Red
86 out_data[4*((x - x0)+w*(y - y0)) + 1] = (unsigned char) image_pixbuf[3*coordx + rowstride*coordy + 1]; //Green
87 out_data[4*((x - x0)+w*(y - y0)) + 2] = (unsigned char) image_pixbuf[3*coordx + rowstride*coordy + 2]; //Blue
88 out_data[4*((x - x0)+w*(y - y0)) + 3] = 255; //Alpha
89 }
90 }
91 }
93 out->empty = FALSE;
94 slot.set(_output, out);
95 return 0;
96 }
98 void FilterImage::set_href(const gchar *href){
99 if (feImageHref) g_free (feImageHref);
100 feImageHref = (href) ? g_strdup (href) : NULL;
101 }
103 void FilterImage::set_region(SVGLength x, SVGLength y, SVGLength width, SVGLength height){
104 feImageX=x.computed;
105 feImageY=y.computed;
106 feImageWidth=width.computed;
107 feImageHeight=height.computed;
108 }
110 FilterTraits FilterImage::get_input_traits() {
111 return TRAIT_PARALLER;
112 }
114 } /* namespace NR */
116 /*
117 Local Variables:
118 mode:c++
119 c-file-style:"stroustrup"
120 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
121 indent-tabs-mode:nil
122 fill-column:99
123 End:
124 */
125 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :