From abe16e5016aa518c5aeaeaf8bcb6be07331d6a2e Mon Sep 17 00:00:00 2001 From: kiirala Date: Mon, 10 Jul 2006 08:03:16 +0000 Subject: [PATCH] Bicubic scaler added to filtering, allowing changes in filter resolution --- src/display/Makefile_insert | 4 +- src/display/nr-filter-gaussian.cpp | 6 +- src/display/nr-filter-primitive.cpp | 4 +- src/display/nr-filter.cpp | 47 ++++++++-- src/display/pixblock-scaler.cpp | 141 ++++++++++++++++++++++++++++ src/display/pixblock-scaler.h | 22 +++++ 6 files changed, 211 insertions(+), 13 deletions(-) create mode 100644 src/display/pixblock-scaler.cpp create mode 100644 src/display/pixblock-scaler.h diff --git a/src/display/Makefile_insert b/src/display/Makefile_insert index f19ced220..6d38c1593 100644 --- a/src/display/Makefile_insert +++ b/src/display/Makefile_insert @@ -69,7 +69,9 @@ display_libspdisplay_a_SOURCES = \ display/nr-filter-gaussian.h \ display/nr-filter-slot.cpp \ display/nr-filter-slot.h \ - display/nr-filter-types.h + display/nr-filter-types.h \ + display/pixblock-scaler.cpp \ + display/pixblock-scaler.h display_bezier_utils_test_SOURCES = display/bezier-utils-test.cpp display_bezier_utils_test_LDADD = libnr/libnr.a -lglib-2.0 diff --git a/src/display/nr-filter-gaussian.cpp b/src/display/nr-filter-gaussian.cpp index ee1fd7d99..ad94bd0da 100644 --- a/src/display/nr-filter-gaussian.cpp +++ b/src/display/nr-filter-gaussian.cpp @@ -362,11 +362,11 @@ int FilterGaussian::render(FilterSlot &slot, Matrix const &trans) NR_PIXBLOCK_PX(out)[((y - yd0) * out->rs) + NR_PIXBLOCK_BPP(out) * (x - xd0) + byte] = a00; continue; } - _check_index(bufy, ((y - yd0) * bufy->rs) + NR_PIXBLOCK_BPP(bufy) + (x + 1 - xd0) + byte, __LINE__); + _check_index(bufy, ((y - yd0) * bufy->rs) + NR_PIXBLOCK_BPP(bufy) * (x + 1 - xd0) + byte, __LINE__); unsigned char a10 = NR_PIXBLOCK_PX(bufy)[((y - yd0) * bufy->rs) + NR_PIXBLOCK_BPP(bufy) * (x + 1 - xd0) + byte]; - _check_index(bufy, ((y + 1 - yd0) * bufy->rs) + NR_PIXBLOCK_BPP(bufy) + (x - xd0) + byte, __LINE__); + _check_index(bufy, ((y + 1 - yd0) * bufy->rs) + NR_PIXBLOCK_BPP(bufy) * (x - xd0) + byte, __LINE__); unsigned char a01 = NR_PIXBLOCK_PX(bufy)[((y + 1 - yd0) * bufy->rs) + NR_PIXBLOCK_BPP(bufy) * (x - xd0) + byte]; - _check_index(bufy, ((y + 1 - yd0) * bufy->rs) + NR_PIXBLOCK_BPP(bufy) + (x + 1 - xd0) + byte, __LINE__); + _check_index(bufy, ((y + 1 - yd0) * bufy->rs) + NR_PIXBLOCK_BPP(bufy) * (x + 1 - xd0) + byte, __LINE__); unsigned char a11 = NR_PIXBLOCK_PX(bufy)[((y + 1 - yd0) * bufy->rs) + NR_PIXBLOCK_BPP(bufy) * (x + 1 - xd0) + byte]; // iterate over the rectangle to be interpolated diff --git a/src/display/nr-filter-primitive.cpp b/src/display/nr-filter-primitive.cpp index f45f909f6..556812db5 100644 --- a/src/display/nr-filter-primitive.cpp +++ b/src/display/nr-filter-primitive.cpp @@ -25,8 +25,8 @@ FilterPrimitive::FilterPrimitive() _region_x.set(SVGLength::PERCENT, 0, 0); _region_y.set(SVGLength::PERCENT, 0, 0); - _region_width.set(SVGLength::PERCENT, 100, 0); - _region_height.set(SVGLength::PERCENT, 100, 0); + _region_width.set(SVGLength::PERCENT, 1, 0); + _region_height.set(SVGLength::PERCENT, 1, 0); } FilterPrimitive::~FilterPrimitive() diff --git a/src/display/nr-filter.cpp b/src/display/nr-filter.cpp index 026aa4ee0..6c57f3200 100644 --- a/src/display/nr-filter.cpp +++ b/src/display/nr-filter.cpp @@ -18,10 +18,13 @@ #include "display/nr-filter-gaussian.h" #include "display/nr-filter-slot.h" #include "display/nr-filter-types.h" +#include "display/pixblock-scaler.h" #include "display/nr-arena-item.h" #include "libnr/nr-pixblock.h" #include "libnr/nr-blit.h" +#include "libnr/nr-matrix.h" +#include "libnr/nr-scale.h" #include "svg/svg-length.h" #include "sp-filter-units.h" @@ -74,16 +77,42 @@ Filter::~Filter() int Filter::render(NRArenaItem const *item, NRPixBlock *pb) { + Matrix trans = *item->ctm; FilterSlot slot(_slot_count); NRPixBlock *in = new NRPixBlock; - nr_pixblock_setup_fast(in, pb->mode, - pb->area.x0, pb->area.y0, - pb->area.x1, pb->area.y1, true); - nr_blit_pixblock_pixblock(in, pb); + if (_x_pixels >= 0) { + /* If filter resolution is zero, the object should not be rendered */ + if (_x_pixels == 0 || _y_pixels == 0) { + int size = (pb->area.x1 - pb->area.x0) + * (pb->area.y1 - pb->area.y0) + * NR_PIXBLOCK_BPP(pb); + memset(NR_PIXBLOCK_PX(pb), 0, size); + return 0; + } + int x_len = (int)round(((pb->area.x1 - pb->area.x0) * _x_pixels) / (item->bbox.x1 - item->bbox.x0)); + if (x_len < 1) x_len = 1; + int y_len; + if (_y_pixels > 0) { + y_len = (int)round(((pb->area.y1 - pb->area.y0) * _y_pixels) / (item->bbox.y1 - item->bbox.y0)); + } else { + y_len = (int)round((x_len * (pb->area.y1 - pb->area.y0)) / (double)(pb->area.x1 - pb->area.x0)); + } + if (y_len < 1) y_len = 1; + nr_pixblock_setup_fast(in, pb->mode, 0, 0, x_len, y_len, true); + scale_bicubic(in, pb); + scale res_scaling(x_len / (double)(pb->area.x1 - pb->area.x0), + y_len / (double)(pb->area.y1 - pb->area.y0)); + trans *= res_scaling; + } else { + nr_pixblock_setup_fast(in, pb->mode, + pb->area.x0, pb->area.y0, + pb->area.x1, pb->area.y1, true); + nr_blit_pixblock_pixblock(in, pb); + } slot.set(NR_FILTER_SOURCEGRAPHIC, in); in = NULL; // in is now handled by FilterSlot, we should not touch it - _primitive[0]->render(slot, *item->ctm); + _primitive[0]->render(slot, trans); NRPixBlock *out = slot.get(_output_slot); @@ -91,8 +120,12 @@ int Filter::render(NRArenaItem const *item, NRPixBlock *pb) * (pb->area.y1 - pb->area.y0) * NR_PIXBLOCK_BPP(pb); memset(NR_PIXBLOCK_PX(pb), 0, size); - - nr_blit_pixblock_pixblock(pb, out); + + if (_x_pixels < 0) { + nr_blit_pixblock_pixblock(pb, out); + } else { + scale_bicubic(pb, out); + } _slot_count = slot.get_slot_count(); return 0; diff --git a/src/display/pixblock-scaler.cpp b/src/display/pixblock-scaler.cpp new file mode 100644 index 000000000..3b8461383 --- /dev/null +++ b/src/display/pixblock-scaler.cpp @@ -0,0 +1,141 @@ +#define __NR_PIXBLOCK_SCALER_CPP__ + +#include +#include +using std::floor; + +#include "libnr/nr-pixblock.h" + +namespace NR { + +struct RGBA { + int r, g, b, a; +}; + +inline int sampley(unsigned const char a, unsigned const char b, unsigned const char c, unsigned const char d, const double len) { + double lenf = len - floor(len); + int sum = 0; + sum += (int)((((-1.0 / 3.0) * lenf + 4.0 / 5.0) * lenf - 7.0 / 15.0) + * lenf * 256 * a); + sum += (int)((((lenf - 9.0 / 5.0) * lenf - 1.0 / 5.0) * lenf + 1.0) + * 256 * b); + sum += (int)(((((1 - lenf) - 9.0 / 5.0) * (1 - lenf) - 1.0 / 5.0) + * (1 - lenf) + 1.0) * 256 * c); + sum += (int)((((-1.0 / 3.0) * (1 - lenf) + 4.0 / 5.0) * (1 - lenf) + - 7.0 / 15.0) * (1 - lenf) * 256 * d); + return sum; +} + +inline unsigned char samplex(const int a, const int b, const int c, const int d, const double len) { + double lenf = len - floor(len); + int sum = 0; + sum += (int)(a * (((-1.0 / 3.0) * lenf + 4.0 / 5.0) * lenf - 7.0 / 15.0) * lenf); + sum += (int)(b * (((lenf - 9.0 / 5.0) * lenf - 1.0 / 5.0) * lenf + 1.0)); + sum += (int)(c * ((((1 - lenf) - 9.0 / 5.0) * (1 - lenf) - 1.0 / 5.0) * (1 - lenf) + 1.0)); + sum += (int)(d * (((-1.0 / 3.0) * (1 - lenf) + 4.0 / 5.0) * (1 - lenf) - 7.0 / 15.0) * (1 - lenf)); + if (sum < 0) sum = 0; + if (sum > 255*256) sum = 255 * 256; + return (unsigned char)(sum / 256); +} + +/** + * Sanity check function for indexing pixblocks. + * Catches reading and writing outside the pixblock area. + * When enabled, decreases filter rendering speed massively. + */ +inline void _check_index(NRPixBlock const * const pb, int const location, int const line) +{ + if(true) { + int max_loc = pb->rs * (pb->area.y1 - pb->area.y0); + if (location < 0 || (location + 4) >= max_loc) + g_warning("Location %d out of bounds (0 ... %d) at line %d", location, max_loc, line); + } +} + +void scale_bicubic(NRPixBlock *to, NRPixBlock *from) +{ + int from_width = from->area.x1 - from->area.x0; + int from_height = from->area.y1 - from->area.y0; + int to_width = to->area.x1 - to->area.x0; + int to_height = to->area.y1 - to->area.y0; + + double from_stepx = (double)from_width / (double)to_width; + double from_stepy = (double)from_height / (double)to_height; + + for (int to_y = 0 ; to_y < to_height ; to_y++) { + double from_y = to_y * from_stepy + from_stepy / 2; + int from_line[4]; + for (int i = 0 ; i < 4 ; i++) { + if ((int)floor(from_y) + i - 1 >= 0) { + if ((int)floor(from_y) + i - 1 < from_height) { + from_line[i] = ((int)floor(from_y) + i - 1) * from->rs; + } else { + from_line[i] = (from_height - 1) * from->rs; + } + } else { + from_line[i] = 0; + } + } + for (int to_x = 0 ; to_x < to_width ; to_x++) { + double from_x = to_x * from_stepx + from_stepx / 2; + RGBA line[4]; + for (int i = 0 ; i < 4 ; i++) { + int k = (int)floor(from_x) + i - 1; + if (k < 0) k = 0; + if (k >= from_width) k = from_width - 1; + k *= 4; + _check_index(from, from_line[0] + k, __LINE__); + _check_index(from, from_line[1] + k, __LINE__); + _check_index(from, from_line[2] + k, __LINE__); + _check_index(from, from_line[3] + k, __LINE__); + line[i].r = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k], + NR_PIXBLOCK_PX(from)[from_line[1] + k], + NR_PIXBLOCK_PX(from)[from_line[2] + k], + NR_PIXBLOCK_PX(from)[from_line[3] + k], + from_y); + line[i].g = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k + 1], + NR_PIXBLOCK_PX(from)[from_line[1] + k + 1], + NR_PIXBLOCK_PX(from)[from_line[2] + k + 1], + NR_PIXBLOCK_PX(from)[from_line[3] + k + 1], + from_y); + line[i].b = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k + 2], + NR_PIXBLOCK_PX(from)[from_line[1] + k + 2], + NR_PIXBLOCK_PX(from)[from_line[2] + k + 2], + NR_PIXBLOCK_PX(from)[from_line[3] + k + 2], + from_y); + line[i].a = sampley(NR_PIXBLOCK_PX(from)[from_line[0] + k + 3], + NR_PIXBLOCK_PX(from)[from_line[1] + k + 3], + NR_PIXBLOCK_PX(from)[from_line[2] + k + 3], + NR_PIXBLOCK_PX(from)[from_line[3] + k + 3], + from_y); + } + RGBA result; + result.r = samplex(line[0].r, line[1].r, line[2].r, line[3].r, + from_x); + result.g = samplex(line[0].g, line[1].g, line[2].g, line[3].g, + from_x); + result.b = samplex(line[0].b, line[1].b, line[2].b, line[3].b, + from_x); + result.a = samplex(line[0].a, line[1].a, line[2].a, line[3].a, + from_x); + + _check_index(to, to_y * to->rs + to_x * 4, __LINE__); + NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4] = result.r; + NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 1] = result.g; + NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 2] = result.b; + NR_PIXBLOCK_PX(to)[to_y * to->rs + to_x * 4 + 3] = result.a; + } + } +} + +} /* 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/pixblock-scaler.h b/src/display/pixblock-scaler.h new file mode 100644 index 000000000..1e30d0a34 --- /dev/null +++ b/src/display/pixblock-scaler.h @@ -0,0 +1,22 @@ +#ifndef __NR_PIXBLOCK_SCALER_H__ +#define __NR_PIXBLOCK_SCALER_H__ + +#include "libnr/nr-pixblock.h" + +namespace NR { + +void scale_bicubic(NRPixBlock *to, NRPixBlock *from); + +} /* namespace NR */ + +#endif // __NR_PIXBLOCK_SCALER_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 : -- 2.30.2