nr-filter-image.cpp revision 723b4d8bde8ce8503d1d01ee0f2e3548ec0dc88c
2035N/A/*
2035N/A * feImage filter primitive renderer
2035N/A *
2035N/A * Authors:
2035N/A * Felipe CorrĂȘa da Silva Sanches <felipe.sanches@gmail.com>
2035N/A *
2035N/A * Copyright (C) 2007 authors
2035N/A *
2035N/A * Released under GNU GPL, read the file 'COPYING' for more information
2035N/A */
2035N/A#include "display/nr-arena-item.h"
2035N/A#include "display/nr-filter.h"
2035N/A#include "display/nr-filter-image.h"
2035N/A#include "display/nr-filter-units.h"
2035N/A
2035N/Anamespace NR {
2035N/A
2035N/AFilterImage::FilterImage()
2035N/A{
2035N/A feImageHref=NULL;
5680N/A image_pixbuf=NULL;
5680N/A}
6408N/A
2035N/AFilterPrimitive * FilterImage::create() {
2035N/A return new FilterImage();
2035N/A}
2035N/A
6408N/AFilterImage::~FilterImage()
2035N/A{
2035N/A if (feImageHref) g_free(feImageHref);
2035N/A if (image_pixbuf) g_free(image_pixbuf);
6408N/A}
2035N/A
2035N/Aint FilterImage::render(FilterSlot &slot, FilterUnits const &/*units*/) {
2035N/A if (!feImageHref) return 0;
2035N/A
6408N/A if (!image_pixbuf){
2899N/A if ( (image = Gdk::Pixbuf::create_from_file(feImageHref)) < 0 ) return 0;
3817N/A width = image->get_width();
3817N/A height = image->get_height();
3817N/A rowstride = image->get_rowstride();
2035N/A image_pixbuf = image->get_pixels();
2035N/A }
2035N/A int w,x,y;
2035N/A NRPixBlock *in = slot.get(_input);
2035N/A NRPixBlock *out = new NRPixBlock;
2035N/A
2035N/A int x0 = in->area.x0, y0 = in->area.y0;
2035N/A int x1 = in->area.x1, y1 = in->area.y1;
6908N/A int bbox_x0 = (int) slot.get_arenaitem()->bbox.x0;
6908N/A int bbox_y0 = (int) slot.get_arenaitem()->bbox.y0;
2035N/A
2035N/A nr_pixblock_setup_fast(out, in->mode, x0, y0, x1, y1, true);
4337N/A
w = x1 - x0;
double scaleX = width/feImageWidth;
double scaleY = height/feImageHeight;
int coordx,coordy;
unsigned char *out_data = NR_PIXBLOCK_PX(out);
for (x=x0; x < x1; x++){
for (y=y0; y < y1; y++){
//TODO: use interpolation
coordx = int((x - feImageX - bbox_x0)*scaleX);
coordy = int((y - feImageY - bbox_y0)*scaleY);
if (coordx > 0 && coordx < width && coordy > 0 && coordy < height){
out_data[4*((x - x0)+w*(y - y0))] = (unsigned char) image_pixbuf[3*coordx + rowstride*coordy]; //Red
out_data[4*((x - x0)+w*(y - y0)) + 1] = (unsigned char) image_pixbuf[3*coordx + rowstride*coordy + 1]; //Green
out_data[4*((x - x0)+w*(y - y0)) + 2] = (unsigned char) image_pixbuf[3*coordx + rowstride*coordy + 2]; //Blue
out_data[4*((x - x0)+w*(y - y0)) + 3] = 255; //Alpha
}
}
}
out->empty = FALSE;
slot.set(_output, out);
return 0;
}
void FilterImage::set_href(const gchar *href){
if (feImageHref) g_free (feImageHref);
feImageHref = (href) ? g_strdup (href) : NULL;
}
void FilterImage::set_region(SVGLength x, SVGLength y, SVGLength width, SVGLength height){
feImageX=x.computed;
feImageY=y.computed;
feImageWidth=width.computed;
feImageHeight=height.computed;
}
FilterTraits FilterImage::get_input_traits() {
return TRAIT_PARALLER;
}
} /* namespace NR */
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :