diff options
| author | Jabier Arraiza Cenoz <jabier.arraiza@marker.es> | 2013-09-26 23:06:17 +0000 |
|---|---|---|
| committer | Jabiertxof <jtx@jtx.marker.es> | 2013-09-26 23:06:17 +0000 |
| commit | dd0b048ba951102b5363bde975f41807cbc6a503 (patch) | |
| tree | c5e1b3fdaba9bf160b78e30c23d8fc24b401658c /src/display | |
| parent | update to trunk (diff) | |
| parent | Use Cairo 1.10 blend operators to render feBlend (diff) | |
| download | inkscape-dd0b048ba951102b5363bde975f41807cbc6a503.tar.gz inkscape-dd0b048ba951102b5363bde975f41807cbc6a503.zip | |
update to trunk
(bzr r12588.1.9)
Diffstat (limited to 'src/display')
| -rw-r--r-- | src/display/drawing-image.cpp | 121 | ||||
| -rw-r--r-- | src/display/drawing-image.h | 3 | ||||
| -rw-r--r-- | src/display/nr-filter-blend.cpp | 164 |
3 files changed, 34 insertions, 254 deletions
diff --git a/src/display/drawing-image.cpp b/src/display/drawing-image.cpp index 0b661a450..a9c0499c2 100644 --- a/src/display/drawing-image.cpp +++ b/src/display/drawing-image.cpp @@ -23,7 +23,6 @@ DrawingImage::DrawingImage(Drawing &drawing) : DrawingItem(drawing) , _pixbuf(NULL) , _style(NULL) - , _new_surface(NULL) {} DrawingImage::~DrawingImage() @@ -123,123 +122,11 @@ unsigned DrawingImage::_renderItem(DrawingContext &ct, Geom::IntRect const &/*ar ct.rectangle(_clipbox); ct.clip(); - ///////////////////////////////////////////////////////////////////////////// - // BEGIN: Hack to avoid Cairo bug - // The total transform (which is RIGHT-multiplied with the item points to get display points) equals: - // scale*translate_origin*_ctm = scale*translate(origin)*expansion*expansionInv*_ctm - // = scale*expansion*translate(origin*expansion)*expansionInv*_ctm - // To avoid a Cairo bug, we handle the scale*expansion part ourselves. - // See https://bugs.launchpad.net/inkscape/+bug/804162 - - Geom::Scale expansion(_ctm.expansion()); - int orgwidth = _pixbuf->width(); - int orgheight = _pixbuf->height(); - - if (_scale[Geom::X]*expansion[Geom::X]*orgwidth*255.0<1.0 || _scale[Geom::Y]*expansion[Geom::Y]*orgheight*255.0<1.0) { - // Resized image too small to actually see anything - return RENDER_OK; - } - - _pixbuf->ensurePixelFormat(Inkscape::Pixbuf::PF_CAIRO); - - // Split scale*expansion in a part that is <= 1.0 and a part that is >= 1.0. We only take care of the part <= 1.0. - Geom::Scale scaleExpansionSmall(std::min<Geom::Coord>(fabs(_scale[Geom::X]*expansion[Geom::X]),1),std::min<Geom::Coord>(fabs(_scale[Geom::Y]*expansion[Geom::Y]),1)); - Geom::Scale scaleExpansionLarge(_scale[Geom::X]*expansion[Geom::X]/scaleExpansionSmall[Geom::X],_scale[Geom::Y]*expansion[Geom::Y]/scaleExpansionSmall[Geom::Y]); - - Geom::Point newSize(Geom::Point(orgwidth,orgheight)*scaleExpansionSmall); - if ((newSize-Geom::Point(orgwidth,orgheight)).length()<0.1) { - // Just use _surface directly. - ct.scale(expansion.inverse()); // This should not include scale (see derivation above) - ct.translate(_origin*expansion); - ct.scale(scaleExpansionLarge); - ct.setSource(_pixbuf->getSurfaceRaw(), 0, 0); - } else if (!_new_surface || (newSize-_rescaledSize).length()>0.1) { - // Rescaled image is sufficiently different from cached image to recompute - if (_new_surface) cairo_surface_destroy(_new_surface); - _rescaledSize = newSize; - - // This essentially considers an image to be composed of rectangular pixels (box kernel) and computes the least-squares approximation of the original. - // When the scale factor is really large or small this essentially results in using a box filter, while for scale factors approaching 1 it is more like a "tent" kernel. - // Although the quality of the result is not great, it is typically better than an ordinary box filter, and it is guaranteed to preserve the overall brightness of the image. - // The best improvement would probably be to do the same kind of thing based on a tent kernel, but that's quite a bit more complicated, and probably not worth the trouble for a hack like this. - int newwidth = static_cast<int>(floor(orgwidth*scaleExpansionSmall[Geom::X])+1); - int newheight = static_cast<int>(floor(orgheight*scaleExpansionSmall[Geom::Y])+1); - std::vector<int> xBegin(newwidth, -1), yBegin(newheight, -1); - std::vector< std::vector<float> > xCoefs(xBegin.size()), yCoefs(yBegin.size()); - for(int x=0; x<orgwidth; x++) { - double coordBegin = x*static_cast<double>(scaleExpansionSmall[Geom::X]); // x-coord in target coordinates where the current source pixel begins - double coordEnd = (x+1)*static_cast<double>(scaleExpansionSmall[Geom::X]); // x-coord in target coordinates where the current source pixel ends - int begin = static_cast<int>(floor(coordBegin)); // First pixel (x-coord) affected by the current source pixel - int end = static_cast<int>(ceil(coordEnd)); // First pixel (x-coord) NOT affected by the current source pixel (a zero contribution is counted as not affecting the pixel) - for(int nx=begin; nx<end; nx++) { - // Set xBegin if this is the first source pixel contributing to the target pixel. - if (xBegin[nx]==-1) xBegin[nx] = x; - // This computes the fraction of the current target pixel (at nx) that is covered by the source pixel (at x). - xCoefs[nx].push_back(static_cast<float>(std::min<double>(nx+1,coordEnd) - std::max<double>(nx,coordBegin))); - } - } - for(int y=0; y<orgheight; y++) { - double coordBegin = y*static_cast<double>(scaleExpansionSmall[Geom::Y]); // y-coord in target coordinates where the current source pixel begins - double coordEnd = (y+1)*static_cast<double>(scaleExpansionSmall[Geom::Y]); // y-coord in target coordinates where the current source pixel ends - int begin = static_cast<int>(floor(coordBegin)); // First pixel (y-coord) affected by the current source pixel - int end = static_cast<int>(ceil(coordEnd)); // First pixel (y-coord) NOT affected by the current source pixel (a zero contribution is counted as not affecting the pixel) - for(int ny=begin; ny<end; ny++) { - // Set yBegin if this is the first source pixel contributing to the target pixel. - if (yBegin[ny]==-1) yBegin[ny] = y; - // This computes the fraction of the current target pixel (at ny) that is covered by the source pixel (at y). - yCoefs[ny].push_back(static_cast<float>(std::min<double>(ny+1,coordEnd) - std::max<double>(ny,coordBegin))); - } - } - - cairo_surface_t *surface = _pixbuf->getSurfaceRaw(); - _new_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, newwidth,newheight); - unsigned char * orgdata = cairo_image_surface_get_data(surface); - unsigned char * newdata = cairo_image_surface_get_data(_new_surface); - int orgstride = cairo_image_surface_get_stride(surface); - int newstride = cairo_image_surface_get_stride(_new_surface); - - cairo_surface_flush(_new_surface); - - for(int y=0; y<newheight; y++) { - for(int x=0; x<newwidth; x++) { - float tempSum[4] = {0,0,0,0}; - for(int oy=0; oy<static_cast<int>(yCoefs[y].size()); oy++) { - for(int ox=0; ox<static_cast<int>(xCoefs[x].size()); ox++) { - for(int c=0; c<4; c++) { - tempSum[c] += xCoefs[x][ox]*yCoefs[y][oy]*orgdata[c+4*(xBegin[x]+ox)+orgstride*(yBegin[y]+oy)]; - } - } - } - for(int c=0; c<4; c++) { - newdata[c+4*x+newstride*y] = static_cast<unsigned char>(tempSum[c]); - } - } - } - - cairo_surface_mark_dirty(_new_surface); - - ct.scale(expansion.inverse()); // This should not include scale (see derivation above) - ct.translate(_origin*expansion); - ct.scale(scaleExpansionLarge); - ct.setSource(_new_surface, 0, 0); - } else { - // No need to regenerate, but we do draw from _new_surface. - ct.scale(expansion.inverse()); // This should not include scale (see derivation above) - ct.translate(_origin*expansion); - ct.scale(scaleExpansionLarge); - ct.setSource(_new_surface, 0, 0); - } - - // END: Hack to avoid Cairo bug - ///////////////////////////////////////////////////////////////////////////// - - // TODO: If Cairo's problems are gone, uncomment the following: - //ct.translate(_origin); - //ct.scale(_scale); - //ct.setSource(_pixbuf->getSurfaceRaw(), 0, 0); + ct.translate(_origin); + ct.scale(_scale); + ct.setSource(_pixbuf->getSurfaceRaw(), 0, 0); - //ct.paint(_opacity); - ct.paint(); + ct.paint(_opacity); } else { // outline; draw a rect instead Inkscape::Preferences *prefs = Inkscape::Preferences::get(); diff --git a/src/display/drawing-image.h b/src/display/drawing-image.h index 58e6de72e..cebaafc85 100644 --- a/src/display/drawing-image.h +++ b/src/display/drawing-image.h @@ -45,9 +45,6 @@ protected: Inkscape::Pixbuf *_pixbuf; SPStyle *_style; - cairo_surface_t *_new_surface; // Part of hack around Cairo bug - Geom::Point _rescaledSize; // Part of hack around Cairo bug - // TODO: the following three should probably be merged into a new Geom::Viewbox object Geom::Rect _clipbox; ///< for preserveAspectRatio Geom::Point _origin; diff --git a/src/display/nr-filter-blend.cpp b/src/display/nr-filter-blend.cpp index a08191f67..bff7405b7 100644 --- a/src/display/nr-filter-blend.cpp +++ b/src/display/nr-filter-blend.cpp @@ -1,6 +1,6 @@ -/* +/** @file * SVG feBlend renderer - * + *//* * "This filter composites two objects together using commonly used * imaging software blending modes. It performs a pixel-wise combination * of two input images." @@ -9,6 +9,7 @@ * Authors: * Niko Kiirala <niko@kiirala.com> * Jasper van de Gronde <th.v.d.gronde@hccnet.nl> + * Krzysztof KosiĆski <tweenk.pl@gmail.com> * * Copyright (C) 2007-2008 authors * @@ -30,20 +31,6 @@ namespace Inkscape { namespace Filters { -/* - * From http://www.w3.org/TR/SVG11/filters.html#feBlend - * - * For all feBlend modes, the result opacity is computed as follows: - * qr = 1 - (1-qa)*(1-qb) - * - * For the compositing formulas below, the following definitions apply: - * cr = Result color (RGB) - premultiplied - * qa = Opacity value at a given pixel for image A - * qb = Opacity value at a given pixel for image B - * ca = Color (RGB) at a given pixel for image A - premultiplied - * cb = Color (RGB) at a given pixel for image B - premultiplied - */ - FilterBlend::FilterBlend() : _blend_mode(BLEND_NORMAL), _input2(NR_FILTER_SLOT_NOT_SET) @@ -56,91 +43,6 @@ FilterPrimitive * FilterBlend::create() { FilterBlend::~FilterBlend() {} -// cr = (1-qa)*cb + (1-qb)*ca + ca*cb -struct BlendMultiply { - guint32 operator()(guint32 in1, guint32 in2) - { - EXTRACT_ARGB32(in1, aa, ra, ga, ba) - EXTRACT_ARGB32(in2, ab, rb, gb, bb) - - guint32 ao = 255*255 - (255-aa)*(255-ab); ao = (ao + 127) / 255; - guint32 ro = (255-aa)*rb + (255-ab)*ra + ra*rb; ro = (ro + 127) / 255; - guint32 go = (255-aa)*gb + (255-ab)*ga + ga*gb; go = (go + 127) / 255; - guint32 bo = (255-aa)*bb + (255-ab)*ba + ba*bb; bo = (bo + 127) / 255; - - ASSEMBLE_ARGB32(pxout, ao, ro, go, bo) - return pxout; - } -}; - -// cr = cb + ca - ca * cb -struct BlendScreen { - guint32 operator()(guint32 in1, guint32 in2) - { - EXTRACT_ARGB32(in1, aa, ra, ga, ba) - EXTRACT_ARGB32(in2, ab, rb, gb, bb) - - guint32 ao = 255*255 - (255-aa)*(255-ab); ao = (ao + 127) / 255; - guint32 ro = 255*(rb + ra) - ra * rb; ro = (ro + 127) / 255; - guint32 go = 255*(gb + ga) - ga * gb; go = (go + 127) / 255; - guint32 bo = 255*(bb + ba) - ba * bb; bo = (bo + 127) / 255; - - ASSEMBLE_ARGB32(pxout, ao, ro, go, bo) - return pxout; - } -}; - -// cr = Min ((1 - qa) * cb + ca, (1 - qb) * ca + cb) -struct BlendDarken { - guint32 operator()(guint32 in1, guint32 in2) - { - EXTRACT_ARGB32(in1, aa, ra, ga, ba) - EXTRACT_ARGB32(in2, ab, rb, gb, bb) - - guint32 ao = 255*255 - (255-aa)*(255-ab); ao = (ao + 127) / 255; - guint32 ro = std::min((255-aa)*rb + 255*ra, (255-ab)*ra + 255*rb); ro = (ro + 127) / 255; - guint32 go = std::min((255-aa)*gb + 255*ga, (255-ab)*ga + 255*gb); go = (go + 127) / 255; - guint32 bo = std::min((255-aa)*bb + 255*ba, (255-ab)*ba + 255*bb); bo = (bo + 127) / 255; - - ASSEMBLE_ARGB32(pxout, ao, ro, go, bo) - return pxout; - } -}; - -// cr = Max ((1 - qa) * cb + ca, (1 - qb) * ca + cb) -struct BlendLighten { - guint32 operator()(guint32 in1, guint32 in2) - { - EXTRACT_ARGB32(in1, aa, ra, ga, ba) - EXTRACT_ARGB32(in2, ab, rb, gb, bb) - - guint32 ao = 255*255 - (255-aa)*(255-ab); ao = (ao + 127) / 255; - guint32 ro = std::max((255-aa)*rb + 255*ra, (255-ab)*ra + 255*rb); ro = (ro + 127) / 255; - guint32 go = std::max((255-aa)*gb + 255*ga, (255-ab)*ga + 255*gb); go = (go + 127) / 255; - guint32 bo = std::max((255-aa)*bb + 255*ba, (255-ab)*ba + 255*bb); bo = (bo + 127) / 255; - - ASSEMBLE_ARGB32(pxout, ao, ro, go, bo) - return pxout; - } -}; - -/* -struct BlendAlpha -static inline void blend_alpha(guint32 in1, guint32 in2, guint32 *out) -{ - EXTRACT_ARGB32(in1, a1, a2, a3, a4); - EXTRACT_ARGB32(in2, b1, b2, b3, b4); - - guint32 o1 = 255*255 - (255-a1)*(255-b1); o1 = (o1+127) / 255; - guint32 o2 = 255*255 - (255-a2)*(255-b2); o2 = (o2+127) / 255; - guint32 o3 = 255*255 - (255-a3)*(255-b3); o3 = (o3+127) / 255; - guint32 o4 = 255*255 - (255-a4)*(255-b4); o4 = (o4+127) / 255; - - ASSEMBLE_ARGB32(pxout, o1, o2, o3, o4); - *out = pxout; -} -*/ - void FilterBlend::render_cairo(FilterSlot &slot) { cairo_surface_t *input1 = slot.getcairo(_input); @@ -161,41 +63,35 @@ void FilterBlend::render_cairo(FilterSlot &slot) cairo_surface_t *out = ink_cairo_surface_create_output(input1, input2); set_cairo_surface_ci( out, ci_fp ); - cairo_content_t ct1 = cairo_surface_get_content(input1); - cairo_content_t ct2 = cairo_surface_get_content(input2); - if ((ct1 == CAIRO_CONTENT_ALPHA && ct2 == CAIRO_CONTENT_ALPHA) - || _blend_mode == BLEND_NORMAL) - { - ink_cairo_surface_blit(input2, out); - cairo_t *out_ct = cairo_create(out); - cairo_set_source_surface(out_ct, input1, 0, 0); - cairo_paint(out_ct); - cairo_destroy(out_ct); - } else { - // blend mode != normal and at least 1 surface is not pure alpha - - // TODO: convert to Cairo blending operators once we start using the 1.10 series - switch (_blend_mode) { - case BLEND_MULTIPLY: - ink_cairo_surface_blend(input1, input2, out, BlendMultiply()); - break; - case BLEND_SCREEN: - ink_cairo_surface_blend(input1, input2, out, BlendScreen()); - break; - case BLEND_DARKEN: - ink_cairo_surface_blend(input1, input2, out, BlendDarken()); - break; - case BLEND_LIGHTEN: - ink_cairo_surface_blend(input1, input2, out, BlendLighten()); - break; - case BLEND_NORMAL: - default: - // this was handled before - g_assert_not_reached(); - break; - } + ink_cairo_surface_blit(input2, out); + cairo_t *out_ct = cairo_create(out); + cairo_set_source_surface(out_ct, input1, 0, 0); + + // All of the blend modes are implemented in Cairo as of 1.10. + // For a detailed description, see: + // http://cairographics.org/operators/ + switch (_blend_mode) { + case BLEND_MULTIPLY: + cairo_set_operator(out_ct, CAIRO_OPERATOR_MULTIPLY); + break; + case BLEND_SCREEN: + cairo_set_operator(out_ct, CAIRO_OPERATOR_SCREEN); + break; + case BLEND_DARKEN: + cairo_set_operator(out_ct, CAIRO_OPERATOR_DARKEN); + break; + case BLEND_LIGHTEN: + cairo_set_operator(out_ct, CAIRO_OPERATOR_LIGHTEN); + break; + case BLEND_NORMAL: + default: + cairo_set_operator(out_ct, CAIRO_OPERATOR_OVER); + break; } + cairo_paint(out_ct); + cairo_destroy(out_ct); + slot.set(_output, out); cairo_surface_destroy(out); } |
