nr-filter-displacement-map.cpp revision 833c08758cda30e603c640ef35ba2da894caef77
0N/A/*
0N/A * feDisplacementMap filter primitive renderer
0N/A *
0N/A * Authors:
0N/A * Felipe CorrĂȘa da Silva Sanches <felipe.sanches@gmail.com>
0N/A *
0N/A * Copyright (C) 2007 authors
0N/A *
0N/A * Released under GNU GPL, read the file 'COPYING' for more information
0N/A */
0N/A
0N/A#include "display/nr-filter-displacement-map.h"
0N/A#include "display/nr-filter-types.h"
0N/A#include "display/nr-filter-units.h"
0N/A#include "libnr/nr-blit.h"
0N/A#include "libnr/nr-pixops.h"
0N/A
0N/Anamespace Inkscape {
0N/Anamespace Filters {
0N/A
0N/AFilterDisplacementMap::FilterDisplacementMap()
0N/A{}
0N/A
0N/AFilterPrimitive * FilterDisplacementMap::create() {
0N/A return new FilterDisplacementMap();
0N/A}
0N/A
0N/AFilterDisplacementMap::~FilterDisplacementMap()
0N/A{}
0N/A
0N/Astruct pixel_t {
0N/A unsigned char channels[4];
0N/A inline unsigned char operator[](int c) const { return channels[c]; }
0N/A inline unsigned char& operator[](int c) { return channels[c]; }
0N/A static inline pixel_t blank() {
0N/A pixel_t p;
0N/A for(unsigned int i=0; i<4; i++) {
0N/A p[i] = 0;
0N/A }
0N/A return p;
0N/A }
0N/A};
0N/A
0N/Astatic inline pixel_t pixelValue(NRPixBlock const* pb, int x, int y) {
0N/A if ( x < pb->area.x0 || x >= pb->area.x1 || y < pb->area.y0 || y >= pb->area.y1 ) return pixel_t::blank(); // This assumes anything outside the defined range is (0,0,0,0)
0N/A pixel_t const* data = reinterpret_cast<pixel_t const*>(NR_PIXBLOCK_PX(pb));
0N/A int offset = (x-pb->area.x0) + (pb->area.x1-pb->area.x0)*(y-pb->area.y0);
0N/A return data[offset];
0N/A}
0N/A
0N/Atemplate<bool PREMULTIPLIED>
0N/Astatic pixel_t interpolatePixels(NRPixBlock const* pb, double x, double y) {
0N/A unsigned int const sfl = 8u;
0N/A unsigned int const sf = 1u<<sfl;
0N/A unsigned int const sf2h = 1u<<(2u*sfl-1);
0N/A int xi = (int)floor(x), yi = (int)floor(y);
0N/A unsigned int xf = static_cast<unsigned int>(floor(sf*(x-xi)+.5)), yf = static_cast<unsigned int>(floor(sf*(y-yi)+.5));
0N/A pixel_t p00 = pixelValue(pb, xi+0, yi+0);
0N/A pixel_t p01 = pixelValue(pb, xi+1, yi+0);
0N/A pixel_t p10 = pixelValue(pb, xi+0, yi+1);
0N/A pixel_t p11 = pixelValue(pb, xi+1, yi+1);
0N/A
0N/A pixel_t r;
0N/A if (PREMULTIPLIED) {
0N/A unsigned int p0[4]; // range [0,a*sf]
0N/A unsigned int p1[4];
0N/A for(size_t i=0; i<4; i++) {
0N/A p0[i] = sf*p00[i] + xf*((unsigned int)p01[i]-(unsigned int)p00[i]);
0N/A p1[i] = sf*p10[i] + xf*((unsigned int)p11[i]-(unsigned int)p10[i]);
0N/A }
0N/A for(size_t i=0; i<4; i++) {
0N/A unsigned int ru = sf*p0[i] + yf*(p1[i]-p0[i]); // range [0,a*sf^2]
0N/A r[i] = (ru + sf2h)>>(2u*sfl); // range [0,a]
0N/A }
0N/A } else {
0N/A // It's a good idea to interpolate premultiplied colors
0N/A // Consider two pixels, one being rgb(255,0,0,0), which is fully transparent,
0N/A // and the other being rgb(0,255,0,255), or green (fully opaque).
0N/A // If these two colors are interpolated the expected result would be greenish pixels containing no red.
0N/A unsigned int p0[4];
0N/A unsigned int p1[4];
0N/A for(size_t i=0; i<3; i++) {
0N/A unsigned int c00 = p00[i]*p00[3], c01 = p01[i]*p01[3], c10 = p10[i]*p10[3], c11 = p11[i]*p11[3]; // range [0,255*a]
0N/A p0[i] = sf*c00 + xf*(c01-c00); // range [0,255*a*sf]
0N/A p1[i] = sf*c10 + xf*(c11-c10); // range [0,255*a*sf]
0N/A }
0N/A p0[3] = sf*p00[3] + xf*(p01[3]-p00[3]); // range [0,a*sf]
0N/A p1[3] = sf*p10[3] + xf*(p11[3]-p10[3]);
0N/A unsigned int ru = sf*p0[3] + yf*(p1[3]-p0[3]); // range [0,a*sf^2]
0N/A r[3] = (ru + sf2h)>>(2u*sfl); // range [0,a]
0N/A for(size_t i=0; i<3; i++) {
0N/A unsigned int ru = sf*p0[i] + yf*(p1[i]-p0[i]); // range [0,255*a*sf^2]
0N/A r[i] = (ru + ru/2u)/ru; // range [0,255]
0N/A }
0N/A }
0N/A
0N/A return r;
0N/A}
0N/A
0N/Atemplate<bool MAP_PREMULTIPLIED, bool DATA_PREMULTIPLIED>
0N/Astatic void performDisplacement(NRPixBlock const* texture, NRPixBlock const* map, int Xchannel, int Ychannel, NRPixBlock* out, double scalex, double scaley) {
0N/A pixel_t *out_data = reinterpret_cast<pixel_t*>(NR_PIXBLOCK_PX(out));
0N/A
0N/A bool Xneedsdemul = MAP_PREMULTIPLIED && Xchannel<3;
0N/A bool Yneedsdemul = MAP_PREMULTIPLIED && Ychannel<3;
0N/A if (!Xneedsdemul) scalex /= 255.0;
0N/A if (!Yneedsdemul) scaley /= 255.0;
0N/A
0N/A for (int xout=out->area.x0; xout < out->area.x1; xout++){
0N/A for (int yout=out->area.y0; yout < out->area.y1; yout++){
0N/A int xmap = xout;
0N/A int ymap = yout;
0N/A
0N/A pixel_t mapValue = pixelValue(map, xmap, ymap);
0N/A double xtex = xout + (Xneedsdemul ?
0N/A (mapValue[3]==0?0:(scalex * (mapValue[Xchannel] - mapValue[3]*0.5) / mapValue[3])) :
0N/A (scalex * (mapValue[Xchannel] - 127.5)));
0N/A double ytex = yout + (Yneedsdemul ?
0N/A (mapValue[3]==0?0:(scaley * (mapValue[Ychannel] - mapValue[3]*0.5) / mapValue[3])) :
0N/A (scaley * (mapValue[Ychannel] - 127.5)));
0N/A
0N/A out_data[(xout-out->area.x0) + (out->area.x1-out->area.x0)*(yout-out->area.y0)] = interpolatePixels<DATA_PREMULTIPLIED>(texture, xtex, ytex);
0N/A }
0N/A }
0N/A}
0N/A
0N/Aint FilterDisplacementMap::render(FilterSlot &slot, FilterUnits const &units) {
0N/A NRPixBlock *texture = slot.get(_input);
0N/A NRPixBlock *map = slot.get(_input2);
0N/A
0N/A // Bail out if either one of source images is missing
0N/A if (!map || !texture) {
0N/A g_warning("Missing source image for feDisplacementMap (map=%d texture=%d)", _input, _input2);
0N/A return 1;
0N/A }
0N/A
0N/A //TODO: check whether do we really need this check:
0N/A if (map->area.x1 <= map->area.x0 || map->area.y1 <= map->area.y0) return 0; //nothing to do!
0N/A
0N/A if (texture->mode != NR_PIXBLOCK_MODE_R8G8B8A8N && texture->mode != NR_PIXBLOCK_MODE_R8G8B8A8P) {
0N/A g_warning("Source images without an alpha channel are not supported by feDisplacementMap at the moment.");
0N/A return 1;
0N/A }
0N/A
0N/A NRPixBlock *out = new NRPixBlock;
0N/A
0N/A out->area.x0 = map->area.x0;
0N/A out->area.y0 = map->area.y0;
0N/A out->area.x1 = map->area.x1;
0N/A out->area.y1 = map->area.y1;
0N/A
0N/A nr_pixblock_setup_fast(out, texture->mode, out->area.x0, out->area.y0, out->area.x1, out->area.y1, true);
0N/A
0N/A // convert to a suitable format
0N/A bool free_map_on_exit = false;
0N/A if (map->mode != NR_PIXBLOCK_MODE_R8G8B8A8N && map->mode != NR_PIXBLOCK_MODE_R8G8B8A8P) {
0N/A NRPixBlock *original_map = map;
0N/A map = new NRPixBlock;
0N/A nr_pixblock_setup_fast(map, NR_PIXBLOCK_MODE_R8G8B8A8N,
0N/A original_map->area.x0, original_map->area.y0,
0N/A original_map->area.x1, original_map->area.y1,
0N/A false);
0N/A nr_blit_pixblock_pixblock(map, original_map);
0N/A free_map_on_exit = true;
0N/A }
0N/A bool map_premultiplied = (map->mode == NR_PIXBLOCK_MODE_R8G8B8A8P);
0N/A bool data_premultiplied = (out->mode == NR_PIXBLOCK_MODE_R8G8B8A8P);
0N/A
0N/A Geom::Matrix trans = units.get_matrix_primitiveunits2pb();
0N/A double scalex = scale * trans.expansionX();
0N/A double scaley = scale * trans.expansionY();
0N/A
0N/A if (map_premultiplied && data_premultiplied) {
0N/A performDisplacement<true,true>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
0N/A } else if (map_premultiplied && !data_premultiplied) {
0N/A performDisplacement<true,false>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
0N/A } else if (data_premultiplied) {
0N/A performDisplacement<false,true>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
0N/A } else {
0N/A performDisplacement<false,false>(texture, map, Xchannel, Ychannel, out, scalex, scaley);
0N/A }
0N/A
0N/A if (free_map_on_exit) {
0N/A nr_pixblock_release(map);
0N/A delete map;
0N/A }
0N/A
0N/A out->empty = FALSE;
0N/A slot.set(_output, out);
0N/A return 0;
0N/A}
0N/A
0N/Avoid FilterDisplacementMap::set_input(int slot) {
0N/A _input = slot;
0N/A}
0N/A
0N/Avoid FilterDisplacementMap::set_scale(double s) {
0N/A scale = s;
0N/A}
0N/A
0N/Avoid FilterDisplacementMap::set_input(int input, int slot) {
0N/A if (input == 0) _input = slot;
0N/A if (input == 1) _input2 = slot;
0N/A}
0N/A
0N/Avoid FilterDisplacementMap::set_channel_selector(int s, FilterDisplacementMapChannelSelector channel) {
0N/A if (channel > DISPLACEMENTMAP_CHANNEL_ALPHA || channel < DISPLACEMENTMAP_CHANNEL_RED) {
0N/A g_warning("Selected an invalid channel value. (%d)", channel);
0N/A return;
0N/A }
0N/A
0N/A if (s == 0) Xchannel = channel;
0N/A if (s == 1) Ychannel = channel;
0N/A}
0N/A
0N/Avoid FilterDisplacementMap::area_enlarge(NRRectL &area, Geom::Matrix const &trans)
0N/A{
0N/A //I assume scale is in user coordinates (?!?)
0N/A //FIXME: trans should be multiplied by some primitiveunits2user, shouldn't it?
0N/A
0N/A double scalex = scale/2.*(std::fabs(trans[0])+std::fabs(trans[1]));
0N/A double scaley = scale/2.*(std::fabs(trans[2])+std::fabs(trans[3]));
0N/A
0N/A //FIXME: no +2 should be there!... (noticable only for big scales at big zoom factor)
0N/A area.x0 -= (int)(scalex)+2;
0N/A area.x1 += (int)(scalex)+2;
0N/A area.y0 -= (int)(scaley)+2;
0N/A area.y1 += (int)(scaley)+2;
0N/A}
0N/A
0N/AFilterTraits FilterDisplacementMap::get_input_traits() {
0N/A return TRAIT_PARALLER;
0N/A}
0N/A
0N/A} /* namespace Filters */
0N/A} /* namespace Inkscape */
0N/A
0N/A/*
0N/A Local Variables:
0N/A mode:c++
0N/A c-file-style:"stroustrup"
0N/A c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
0N/A indent-tabs-mode:nil
0N/A fill-column:99
0N/A End:
0N/A*/
0N/A// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
0N/A