Code

Patch from codedread. Prevents rendering of title/desc/metadata elements in text...
[inkscape.git] / src / display / nr-filter-displacement-map.cpp
index 5d454e6cdd5fd87cbb1500f923a27f236d9a9dff..886ee118e12f7cc0ef9c1c80493cdede17752342 100644 (file)
@@ -12,6 +12,8 @@
 #include "display/nr-filter-displacement-map.h"
 #include "display/nr-filter-types.h"
 #include "display/nr-filter-units.h"
+#include "libnr/nr-blit.h"
+#include "libnr/nr-matrix-fns.h"
 #include "libnr/nr-pixops.h"
 
 namespace NR {
@@ -40,25 +42,32 @@ int FilterDisplacementMap::render(FilterSlot &slot, FilterUnits const &units) {
     if (map->area.x1 <= map->area.x0 || map->area.y1 <=  map->area.y0) return 0; //nothing to do!
 
     NRPixBlock *out = new NRPixBlock;
-
-    //are these checks really necessary?
-    if (out_w > map->area.x1 - out_x0) out_w = map->area.x1 - out_x0;
-    if (out_h > map->area.y1 - out_y0) out_h = map->area.y1 - out_y0;
-    if (out_x0 < map->area.x0){
-        out_x0 = map->area.x0;
-        out_w -= (map->area.x0 - out_x0);
-    }
-    if (out_y0 < map->area.y0){
-        out_y0 = map->area.y0;
-        out_h -= (map->area.y0 - out_y0);
-    }
+    
+    out_x0 = map->area.x0;
+    out_y0 = map->area.y0;
+    out_w  = map->area.x1 - map->area.x0;
+    out_h  = map->area.y1 - map->area.y0;
 
     out->area.x0 = out_x0;
     out->area.y0 = out_y0;
     out->area.x1 = out_x0 + out_w;
     out->area.y1 = out_y0 + out_h;
 
-    nr_pixblock_setup_fast(out, map->mode, out->area.x0, out->area.y0, out->area.x1, out->area.y1, true);
+    nr_pixblock_setup_fast(out, texture->mode, out->area.x0, out->area.y0, out->area.x1, out->area.y1, true);
+
+    // this primitive is defined for non-premultiplied RGBA values,
+    // thus convert them to that format
+    bool free_map_on_exit = false;
+    if (map->mode != NR_PIXBLOCK_MODE_R8G8B8A8N) {
+        NRPixBlock *original_map = map;
+        map = new NRPixBlock;
+        nr_pixblock_setup_fast(map, NR_PIXBLOCK_MODE_R8G8B8A8N,
+                               original_map->area.x0, original_map->area.y0,
+                               original_map->area.x1, original_map->area.y1,
+                               false);
+        nr_blit_pixblock_pixblock(map, original_map);
+        free_map_on_exit = true;
+    }
 
     unsigned char *map_data = NR_PIXBLOCK_PX(map);
     unsigned char *texture_data = NR_PIXBLOCK_PX(texture);
@@ -69,34 +78,41 @@ int FilterDisplacementMap::render(FilterSlot &slot, FilterUnits const &units) {
     double coordx, coordy;
     
     Matrix trans = units.get_matrix_primitiveunits2pb();
-    double scalex = scale*trans.expansionX();
-    double scaley = scale*trans.expansionY();
+    double scalex = scale*NR::expansionX(trans);
+    double scaley = scale*NR::expansionY(trans);
     
     for (x=0; x < out_w; x++){
         for (y=0; y < out_h; y++){
-            if (x+out_x0-map->area.x0 >= 0 &&
-                x+out_x0-map->area.x0 < in_w &&
-                y+out_y0-map->area.y0 >= 0 &&
-                y+out_y0-map->area.y0 < in_h){
-
-                    coordx = out_x0 - map->area.x0 + x + scalex * ( double(map_data[4*((x+out_x0-map->area.x0) + in_w*(y+out_y0-map->area.y0)) + Xchannel])/255 - 0.5);
-                    coordy = out_y0 - map->area.y0 + y + scaley * ( double(map_data[4*((x+out_x0-map->area.x0) + in_w*(y+out_y0-map->area.y0)) + Ychannel])/255 - 0.5);
-
-                    if (coordx>=0 && coordx<in_w && coordy>=0 && coordy<in_h){
-                            out_data[4*(x + out_w*y)] = texture_data[4*(int(coordx) + int(coordy)*in_w)];
-                            out_data[4*(x + out_w*y) + 1] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 1];
-                            out_data[4*(x + out_w*y) + 2] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 2];
-                            out_data[4*(x + out_w*y) + 3] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 3];
-                    } else {
-                            out_data[4*(x + out_w*y)] = 255;
-                            out_data[4*(x + out_w*y) + 1] = 255;
-                            out_data[4*(x + out_w*y) + 2] = 255;
-                            out_data[4*(x + out_w*y) + 3] = 0;
-                    }
+            int xmap = x+out_x0-map->area.x0;
+            int ymap = y+out_y0-map->area.y0;
+            if (xmap >= 0 &&
+                xmap < in_w &&
+                ymap >= 0 &&
+                ymap < in_h){
+
+                coordx = xmap + scalex * ( double(map_data[4*(xmap + in_w*ymap) + Xchannel]-128.)/256);
+                coordy = ymap + scaley * ( double(map_data[4*(xmap + in_w*ymap) + Ychannel]-128.)/256);
+
+                if (coordx>=0 && coordx<in_w && coordy>=0 && coordy<in_h){
+                    out_data[4*(x + out_w*y)    ] = texture_data[4*(int(coordx) + int(coordy)*in_w)    ];
+                    out_data[4*(x + out_w*y) + 1] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 1];
+                    out_data[4*(x + out_w*y) + 2] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 2];
+                    out_data[4*(x + out_w*y) + 3] = texture_data[4*(int(coordx) + int(coordy)*in_w) + 3];
+                } else {
+                    out_data[4*(x + out_w*y)    ] = 255;
+                    out_data[4*(x + out_w*y) + 1] = 255;
+                    out_data[4*(x + out_w*y) + 2] = 255;
+                    out_data[4*(x + out_w*y) + 3] = 0;
+                }
             }
         }
     }
 
+    if (free_map_on_exit) {
+        nr_pixblock_release(map);
+        delete map;
+    }
+
     out->empty = FALSE;
     slot.set(_output, out);
             return 0;
@@ -127,18 +143,17 @@ void FilterDisplacementMap::set_channel_selector(int s, FilterDisplacementMapCha
 
 void FilterDisplacementMap::area_enlarge(NRRectL &area, Matrix const &trans)
 {
-    out_x0 = area.x0;
-    out_y0 = area.y0;
-    out_w = area.x1 - area.x0;
-    out_h = area.y1 - area.y0;
+    //I assume scale is in user coordinates (?!?)
+    //FIXME: trans should be multiplied by some primitiveunits2user, shouldn't it?
     
-    double scalex = scale*trans.expansionX();
-    double scaley = scale*trans.expansionY();
-
-    area.x0 -= (int)(scalex/2);
-    area.x1 += (int)(scalex/2);
-    area.y0 -= (int)(scaley/2);
-    area.y1 += (int)(scaley/2);
+    double scalex = scale/2.*(std::fabs(trans[0])+std::fabs(trans[1]));
+    double scaley = scale/2.*(std::fabs(trans[2])+std::fabs(trans[3]));
+
+    //FIXME: no +2 should be there!... (noticable only for big scales at big zoom factor)
+    area.x0 -= (int)(scalex)+2;
+    area.x1 += (int)(scalex)+2;
+    area.y0 -= (int)(scaley)+2;
+    area.y1 += (int)(scaley)+2;
 }
 
 FilterTraits FilterDisplacementMap::get_input_traits() {