Home | History | Annotate | Download | only in filters
      1 /*
      2  * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann (at) kde.org>
      3  * Copyright (C) 2004, 2005 Rob Buis <buis (at) kde.org>
      4  * Copyright (C) 2005 Eric Seidel <eric (at) webkit.org>
      5  * Copyright (C) 2009 Dirk Schulze <krit (at) webkit.org>
      6  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
      7  * Copyright (C) 2013 Google Inc. All rights reserved.
      8  *
      9  * This library is free software; you can redistribute it and/or
     10  * modify it under the terms of the GNU Library General Public
     11  * License as published by the Free Software Foundation; either
     12  * version 2 of the License, or (at your option) any later version.
     13  *
     14  * This library is distributed in the hope that it will be useful,
     15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     17  * Library General Public License for more details.
     18  *
     19  * You should have received a copy of the GNU Library General Public License
     20  * along with this library; see the file COPYING.LIB.  If not, write to
     21  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     22  * Boston, MA 02110-1301, USA.
     23  */
     24 
     25 #include "config.h"
     26 #include "platform/graphics/filters/FEMorphology.h"
     27 
     28 #include "SkMorphologyImageFilter.h"
     29 #include "platform/graphics/GraphicsContext.h"
     30 #include "platform/graphics/Image.h"
     31 #include "platform/graphics/filters/ParallelJobs.h"
     32 #include "platform/graphics/filters/SkiaImageFilterBuilder.h"
     33 #include "platform/text/TextStream.h"
     34 #include "wtf/Uint8ClampedArray.h"
     35 #include "wtf/Vector.h"
     36 
     37 using namespace std;
     38 
     39 namespace WebCore {
     40 
     41 FEMorphology::FEMorphology(Filter* filter, MorphologyOperatorType type, float radiusX, float radiusY)
     42     : FilterEffect(filter)
     43     , m_type(type)
     44     , m_radiusX(radiusX)
     45     , m_radiusY(radiusY)
     46 {
     47 }
     48 
     49 PassRefPtr<FEMorphology> FEMorphology::create(Filter* filter, MorphologyOperatorType type, float radiusX, float radiusY)
     50 {
     51     return adoptRef(new FEMorphology(filter, type, radiusX, radiusY));
     52 }
     53 
     54 MorphologyOperatorType FEMorphology::morphologyOperator() const
     55 {
     56     return m_type;
     57 }
     58 
     59 bool FEMorphology::setMorphologyOperator(MorphologyOperatorType type)
     60 {
     61     if (m_type == type)
     62         return false;
     63     m_type = type;
     64     return true;
     65 }
     66 
     67 float FEMorphology::radiusX() const
     68 {
     69     return m_radiusX;
     70 }
     71 
     72 bool FEMorphology::setRadiusX(float radiusX)
     73 {
     74     if (m_radiusX == radiusX)
     75         return false;
     76     m_radiusX = radiusX;
     77     return true;
     78 }
     79 
     80 float FEMorphology::radiusY() const
     81 {
     82     return m_radiusY;
     83 }
     84 
     85 FloatRect FEMorphology::mapRect(const FloatRect& rect, bool)
     86 {
     87     FloatRect result = rect;
     88     result.inflateX(filter()->applyHorizontalScale(m_radiusX));
     89     result.inflateY(filter()->applyVerticalScale(m_radiusY));
     90     return result;
     91 }
     92 
     93 bool FEMorphology::setRadiusY(float radiusY)
     94 {
     95     if (m_radiusY == radiusY)
     96         return false;
     97     m_radiusY = radiusY;
     98     return true;
     99 }
    100 
    101 void FEMorphology::applySoftware()
    102 {
    103     ImageBuffer* resultImage = createImageBufferResult();
    104     if (!resultImage)
    105         return;
    106 
    107     FilterEffect* in = inputEffect(0);
    108 
    109     IntRect drawingRegion = drawingRegionOfInputImage(in->absolutePaintRect());
    110 
    111     setIsAlphaImage(in->isAlphaImage());
    112 
    113     float radiusX = filter()->applyHorizontalScale(m_radiusX);
    114     float radiusY = filter()->applyVerticalScale(m_radiusY);
    115 
    116     RefPtr<Image> image = in->asImageBuffer()->copyImage(DontCopyBackingStore);
    117 
    118     SkPaint paint;
    119     GraphicsContext* dstContext = resultImage->context();
    120     if (m_type == FEMORPHOLOGY_OPERATOR_DILATE)
    121         paint.setImageFilter(SkDilateImageFilter::Create(radiusX, radiusY))->unref();
    122     else if (m_type == FEMORPHOLOGY_OPERATOR_ERODE)
    123         paint.setImageFilter(SkErodeImageFilter::Create(radiusX, radiusY))->unref();
    124 
    125     SkRect bounds = SkRect::MakeWH(absolutePaintRect().width(), absolutePaintRect().height());
    126     dstContext->saveLayer(&bounds, &paint);
    127     dstContext->drawImage(image.get(), drawingRegion.location(), CompositeCopy);
    128     dstContext->restoreLayer();
    129 }
    130 
    131 PassRefPtr<SkImageFilter> FEMorphology::createImageFilter(SkiaImageFilterBuilder* builder)
    132 {
    133     RefPtr<SkImageFilter> input(builder->build(inputEffect(0), operatingColorSpace()));
    134     SkScalar radiusX = SkFloatToScalar(filter()->applyHorizontalScale(m_radiusX));
    135     SkScalar radiusY = SkFloatToScalar(filter()->applyVerticalScale(m_radiusY));
    136     SkImageFilter::CropRect rect = getCropRect(builder->cropOffset());
    137     if (m_type == FEMORPHOLOGY_OPERATOR_DILATE)
    138         return adoptRef(SkDilateImageFilter::Create(radiusX, radiusY, input.get(), &rect));
    139     return adoptRef(SkErodeImageFilter::Create(radiusX, radiusY, input.get(), &rect));
    140 }
    141 
    142 static TextStream& operator<<(TextStream& ts, const MorphologyOperatorType& type)
    143 {
    144     switch (type) {
    145     case FEMORPHOLOGY_OPERATOR_UNKNOWN:
    146         ts << "UNKNOWN";
    147         break;
    148     case FEMORPHOLOGY_OPERATOR_ERODE:
    149         ts << "ERODE";
    150         break;
    151     case FEMORPHOLOGY_OPERATOR_DILATE:
    152         ts << "DILATE";
    153         break;
    154     }
    155     return ts;
    156 }
    157 
    158 TextStream& FEMorphology::externalRepresentation(TextStream& ts, int indent) const
    159 {
    160     writeIndent(ts, indent);
    161     ts << "[feMorphology";
    162     FilterEffect::externalRepresentation(ts);
    163     ts << " operator=\"" << morphologyOperator() << "\" "
    164         << "radius=\"" << radiusX() << ", " << radiusY() << "\"]\n";
    165     inputEffect(0)->externalRepresentation(ts, indent + 1);
    166     return ts;
    167 }
    168 
    169 } // namespace WebCore
    170