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