From b1bec4de820f8b6d32105117c7d1015e5093af8b Mon Sep 17 00:00:00 2001 From: jucablues Date: Wed, 25 Jul 2007 07:12:49 +0000 Subject: [PATCH] DisplacementMap filter boilerplate code + renderer draft implementation. General setting of attributes and default values. renderer method is still not correct. Needs some more effort. --- src/display/Makefile_insert | 2 + src/display/nr-filter-displacement-map.cpp | 115 +++++++++++++++++++++ src/display/nr-filter-displacement-map.h | 54 ++++++++++ src/display/nr-filter.cpp | 3 +- src/sp-fedisplacementmap.cpp | 102 +++++++++++++++++- src/sp-fedisplacementmap.h | 5 +- 6 files changed, 277 insertions(+), 4 deletions(-) create mode 100644 src/display/nr-filter-displacement-map.cpp create mode 100644 src/display/nr-filter-displacement-map.h diff --git a/src/display/Makefile_insert b/src/display/Makefile_insert index a72175b3f..f31236674 100644 --- a/src/display/Makefile_insert +++ b/src/display/Makefile_insert @@ -83,6 +83,8 @@ display_libspdisplay_a_SOURCES = \ display/nr-filter-composite.cpp \ display/nr-filter-convolve-matrix.cpp \ display/nr-filter-convolve-matrix.h \ + display/nr-filter-displacement-map.cpp \ + display/nr-filter-displacement-map.h \ display/nr-filter-slot.cpp \ display/nr-filter-slot.h \ display/nr-filter-getalpha.cpp \ diff --git a/src/display/nr-filter-displacement-map.cpp b/src/display/nr-filter-displacement-map.cpp new file mode 100644 index 000000000..ec8681ad1 --- /dev/null +++ b/src/display/nr-filter-displacement-map.cpp @@ -0,0 +1,115 @@ +/* + * feDisplacementMap filter primitive renderer + * + * Authors: + * Felipe Corrêa da Silva Sanches + * + * Copyright (C) 2007 authors + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#include "display/nr-filter-displacement-map.h" +#include "display/nr-filter-types.h" + +namespace NR { + +FilterDisplacementMap::FilterDisplacementMap() +: Xchannel(3), + Ychannel(3), + scale(0), + _input2(NR_FILTER_SLOT_NOT_SET) +{} + +FilterPrimitive * FilterDisplacementMap::create() { + return new FilterDisplacementMap(); +} + +FilterDisplacementMap::~FilterDisplacementMap() +{} + +int FilterDisplacementMap::render(FilterSlot &slot, Matrix const &trans) { + g_warning("FIX-ME: FilterDisplacementMap::render method is still a bit buggy. Needs Love."); + + NRPixBlock *texture = slot.get(_input); + NRPixBlock *map = slot.get(_input2); + NRPixBlock *out = new NRPixBlock; + + // Bail out if either one of source images is missing + if (!map || !texture) { + g_warning("Missing source image for feDisplacementMap (map=%d texture=%d)", _input, _input2); + return 1; + } + + nr_pixblock_setup_fast(out, map->mode, + map->area.x0, map->area.y0, map->area.x1, map->area.y1, + true); + + unsigned char *map_data = NR_PIXBLOCK_PX(map); + unsigned char *texture_data = NR_PIXBLOCK_PX(texture); + unsigned char *out_data = NR_PIXBLOCK_PX(out); + int x, y, x0, y0, x1, y1, width; + double coordx, coordy; + + x0 = out->area.x0; + y0 = out->area.y0; + x1 = out->area.x1; + y1 = out->area.y1; + width = x1 - x0; + + for (x=x0 + scale/2; x < x1 - scale/2; x++){ + for (y=y0 + scale/2; y < y1 - scale/2; y++){ + coordx = x-x0 + scale * ( ((double)map_data[4*((x-x0) + width*(y-y0)) + Xchannel])/255 - 0.5); + coordy = y-y0 + scale * ( ((double)map_data[4*((x-x0) + width*(y-y0)) + Ychannel])/255 - 0.5); + + out_data[4*((x-x0) + width*(y-y0))] = texture_data[4*(int)(coordx + coordy*width)]; + out_data[4*((x-x0) + width*(y-y0)) + 1] = texture_data[4*(int)(coordx + coordy*width) + 1]; + out_data[4*((x-x0) + width*(y-y0)) + 2] = texture_data[4*(int)(coordx + coordy*width) + 2]; + out_data[4*((x-x0) + width*(y-y0)) + 3] = texture_data[4*(int)(coordx + coordy*width) + 3]; + } + } + + out->empty = FALSE; + slot.set(_output, out); + return 0; +} + +void FilterDisplacementMap::set_input(int slot) { + _input = slot; +} + +void FilterDisplacementMap::set_scale(int s) { + scale = s; +} + +void FilterDisplacementMap::set_input(int input, int slot) { + if (input == 0) _input = slot; + if (input == 1) _input2 = slot; +} + +void FilterDisplacementMap::set_channel_selector(int s, int channel) { + if (s == 0) Xchannel = channel; + if (s == 1) Ychannel = channel; +} + +void FilterDisplacementMap::area_enlarge(NRRectL &area, Matrix const &trans) +{ + //I'm in doubt whether this affects all input buffers or only 'in' + area.x0 -= scale/2; + area.y0 -= scale/2; + area.x1 += scale/2; + area.y1 += scale/2; +} + +} /* 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 : diff --git a/src/display/nr-filter-displacement-map.h b/src/display/nr-filter-displacement-map.h new file mode 100644 index 000000000..4b5c6ada5 --- /dev/null +++ b/src/display/nr-filter-displacement-map.h @@ -0,0 +1,54 @@ +#ifndef __NR_FILTER_DISPLACEMENT_MAP_H__ +#define __NR_FILTER_DISPLACEMENT_MAP_H__ + +/* + * feDisplacementMap filter primitive renderer + * + * Authors: + * Felipe Corrêa da Silva Sanches + * + * Copyright (C) 2007 authors + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#include "display/nr-filter-primitive.h" +#include "display/nr-filter-slot.h" +#include "libnr/nr-matrix.h" +#include "libnr/nr-rect-l.h" + +namespace NR { + +class FilterDisplacementMap : public FilterPrimitive { +public: + FilterDisplacementMap(); + static FilterPrimitive *create(); + virtual ~FilterDisplacementMap(); + + virtual void set_input(int slot); + virtual void set_input(int input, int slot); + virtual void set_scale(int s); + virtual void set_channel_selector(int channel, int s); + virtual int render(FilterSlot &slot, Matrix const &trans); + virtual void area_enlarge(NRRectL &area, Matrix const &trans); + +private: + int scale; + int _input2; + int Xchannel; + int Ychannel; +}; + +} /* namespace NR */ + +#endif /* __NR_FILTER_DISPLACEMENT_MAP_H__ */ +/* + 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 : diff --git a/src/display/nr-filter.cpp b/src/display/nr-filter.cpp index 47b05d0b8..2491fba35 100644 --- a/src/display/nr-filter.cpp +++ b/src/display/nr-filter.cpp @@ -21,6 +21,7 @@ #include "display/pixblock-scaler.h" #include "display/pixblock-transform.h" +#include "display/nr-filter-displacement-map.h" #include "display/nr-filter-gaussian.h" #include "display/nr-filter-convolve-matrix.h" #include "display/nr-filter-blend.h" @@ -317,7 +318,7 @@ void Filter::_create_constructor_table() _constructor[NR_FILTER_COMPOSITE] = &FilterComposite::create; _constructor[NR_FILTER_CONVOLVEMATRIX] = &FilterConvolveMatrix::create; _constructor[NR_FILTER_DIFFUSELIGHTING] = &FilterDiffuseLighting::create; - _constructor[NR_FILTER_DISPLACEMENTMAP] = NULL; + _constructor[NR_FILTER_DISPLACEMENTMAP] = &FilterDisplacementMap::create; _constructor[NR_FILTER_FLOOD] = NULL; _constructor[NR_FILTER_GAUSSIANBLUR] = &FilterGaussian::create; _constructor[NR_FILTER_IMAGE] = NULL; diff --git a/src/sp-fedisplacementmap.cpp b/src/sp-fedisplacementmap.cpp index 8b861b85d..6c606f6bf 100644 --- a/src/sp-fedisplacementmap.cpp +++ b/src/sp-fedisplacementmap.cpp @@ -21,7 +21,7 @@ #include "svg/svg.h" #include "sp-fedisplacementmap.h" #include "xml/repr.h" - +#include "display/nr-filter-displacement-map.h" /* FeDisplacementMap base class */ @@ -32,6 +32,7 @@ static void sp_feDisplacementMap_build(SPObject *object, SPDocument *document, I static void sp_feDisplacementMap_release(SPObject *object); static void sp_feDisplacementMap_set(SPObject *object, unsigned int key, gchar const *value); static void sp_feDisplacementMap_update(SPObject *object, SPCtx *ctx, guint flags); +static void sp_feDisplacementMap_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter); static Inkscape::XML::Node *sp_feDisplacementMap_write(SPObject *object, Inkscape::XML::Node *repr, guint flags); static SPFilterPrimitiveClass *feDisplacementMap_parent_class; @@ -61,6 +62,7 @@ static void sp_feDisplacementMap_class_init(SPFeDisplacementMapClass *klass) { SPObjectClass *sp_object_class = (SPObjectClass *)klass; + SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass; feDisplacementMap_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass); @@ -69,11 +71,16 @@ sp_feDisplacementMap_class_init(SPFeDisplacementMapClass *klass) sp_object_class->write = sp_feDisplacementMap_write; sp_object_class->set = sp_feDisplacementMap_set; sp_object_class->update = sp_feDisplacementMap_update; + sp_primitive_class->build_renderer = sp_feDisplacementMap_build_renderer; } static void sp_feDisplacementMap_init(SPFeDisplacementMap *feDisplacementMap) { + feDisplacementMap->scale=0; + feDisplacementMap->xChannelSelector=3; + feDisplacementMap->yChannelSelector=3; + feDisplacementMap->in2 = NR::NR_FILTER_SLOT_NOT_SET; } /** @@ -89,6 +96,10 @@ sp_feDisplacementMap_build(SPObject *object, SPDocument *document, Inkscape::XML } /*LOAD ATTRIBUTES FROM REPR HERE*/ + sp_object_read_attr(object, "scale"); + sp_object_read_attr(object, "in2"); + sp_object_read_attr(object, "xChannelSelector"); + sp_object_read_attr(object, "yChannelSelector"); } /** @@ -101,6 +112,44 @@ sp_feDisplacementMap_release(SPObject *object) ((SPObjectClass *) feDisplacementMap_parent_class)->release(object); } +static int sp_feDisplacementMap_readChannelSelector(gchar const *value) +{ + if (!value) return 0; + switch (value[0]) { + case 'R': + return 0; + break; + case 'G': + return 1; + break; + case 'B': + return 2; + break; + case 'A': + return 3; + break; + default: + // error + g_warning("Invalid attribute for Channel Selector. Valid modes are 'R', 'G', 'B' or 'A'"); + break; + } + return 3; //default is Alpha Channel +} + +static double +sp_feDisplacementMap_read_number(gchar const *value) { + if (!value) return 0; + char *end; + double ret = g_ascii_strtod(value, &end); + if (*end) { + g_warning("Unable to convert \"%s\" to number", value); + // We could leave this out, too. If strtod can't convert + // anything, it will return zero. + ret = 0; + } + return ret; +} + /** * Sets a specific value in the SPFeDisplacementMap. */ @@ -109,9 +158,39 @@ sp_feDisplacementMap_set(SPObject *object, unsigned int key, gchar const *value) { SPFeDisplacementMap *feDisplacementMap = SP_FEDISPLACEMENTMAP(object); (void)feDisplacementMap; - + int input; + double read_num; + int read_selector; switch(key) { /*DEAL WITH SETTING ATTRIBUTES HERE*/ + case SP_ATTR_XCHANNELSELECTOR: + read_selector = sp_feDisplacementMap_readChannelSelector(value); + if (read_selector != feDisplacementMap->xChannelSelector){ + feDisplacementMap->xChannelSelector = read_selector; + object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG); + } + break; + case SP_ATTR_YCHANNELSELECTOR: + read_selector = sp_feDisplacementMap_readChannelSelector(value); + if (read_selector != feDisplacementMap->yChannelSelector){ + feDisplacementMap->yChannelSelector = read_selector; + object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG); + } + break; + case SP_ATTR_SCALE: + read_num = sp_feDisplacementMap_read_number(value); + if (read_num != feDisplacementMap->scale) { + feDisplacementMap->scale = read_num; + object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG); + } + break; + case SP_ATTR_IN2: + input = sp_filter_primitive_read_in(feDisplacementMap, value); + if (input != feDisplacementMap->in2) { + feDisplacementMap->in2 = input; + object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG); + } + break; default: if (((SPObjectClass *) feDisplacementMap_parent_class)->set) ((SPObjectClass *) feDisplacementMap_parent_class)->set(object, key, value); @@ -161,6 +240,25 @@ sp_feDisplacementMap_write(SPObject *object, Inkscape::XML::Node *repr, guint fl return repr; } +static void sp_feDisplacementMap_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter) { + g_assert(primitive != NULL); + g_assert(filter != NULL); + + SPFeDisplacementMap *sp_displacement_map = SP_FEDISPLACEMENTMAP(primitive); + + int primitive_n = filter->add_primitive(NR::NR_FILTER_DISPLACEMENTMAP); + NR::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n); + NR::FilterDisplacementMap *nr_displacement_map = dynamic_cast(nr_primitive); + g_assert(nr_displacement_map != NULL); + + sp_filter_primitive_renderer_common(primitive, nr_primitive); + + nr_displacement_map->set_input(1, sp_displacement_map->in2); + nr_displacement_map->set_scale(sp_displacement_map->scale); + nr_displacement_map->set_channel_selector(0, sp_displacement_map->xChannelSelector); + nr_displacement_map->set_channel_selector(1, sp_displacement_map->yChannelSelector); +} + /* Local Variables: diff --git a/src/sp-fedisplacementmap.h b/src/sp-fedisplacementmap.h index 1e4eb2845..427bce153 100644 --- a/src/sp-fedisplacementmap.h +++ b/src/sp-fedisplacementmap.h @@ -21,7 +21,10 @@ class SPFeDisplacementMapClass; struct SPFeDisplacementMap : public SPFilterPrimitive { /** DISPLACEMENTMAP ATTRIBUTES HERE */ - + int in2; + double scale; + int xChannelSelector; + int yChannelSelector; }; struct SPFeDisplacementMapClass { -- 2.30.2