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 27 #include "core/platform/graphics/filters/FEMorphology.h" 28 29 #include "core/platform/graphics/filters/Filter.h" 30 #include "core/platform/graphics/filters/SkiaImageFilterBuilder.h" 31 32 #include "core/platform/text/TextStream.h" 33 #include "core/rendering/RenderTreeAsText.h" 34 35 #include "wtf/ParallelJobs.h" 36 #include "wtf/Uint8ClampedArray.h" 37 #include "wtf/Vector.h" 38 39 #include "SkMorphologyImageFilter.h" 40 41 using std::min; 42 using std::max; 43 44 namespace WebCore { 45 46 FEMorphology::FEMorphology(Filter* filter, MorphologyOperatorType type, float radiusX, float radiusY) 47 : FilterEffect(filter) 48 , m_type(type) 49 , m_radiusX(radiusX) 50 , m_radiusY(radiusY) 51 { 52 } 53 54 PassRefPtr<FEMorphology> FEMorphology::create(Filter* filter, MorphologyOperatorType type, float radiusX, float radiusY) 55 { 56 return adoptRef(new FEMorphology(filter, type, radiusX, radiusY)); 57 } 58 59 MorphologyOperatorType FEMorphology::morphologyOperator() const 60 { 61 return m_type; 62 } 63 64 bool FEMorphology::setMorphologyOperator(MorphologyOperatorType type) 65 { 66 if (m_type == type) 67 return false; 68 m_type = type; 69 return true; 70 } 71 72 float FEMorphology::radiusX() const 73 { 74 return m_radiusX; 75 } 76 77 bool FEMorphology::setRadiusX(float radiusX) 78 { 79 if (m_radiusX == radiusX) 80 return false; 81 m_radiusX = radiusX; 82 return true; 83 } 84 85 float FEMorphology::radiusY() const 86 { 87 return m_radiusY; 88 } 89 90 void FEMorphology::determineAbsolutePaintRect() 91 { 92 FloatRect paintRect = mapRect(inputEffect(0)->absolutePaintRect()); 93 if (clipsToBounds()) 94 paintRect.intersect(maxEffectRect()); 95 else 96 paintRect.unite(maxEffectRect()); 97 setAbsolutePaintRect(enclosingIntRect(paintRect)); 98 } 99 100 FloatRect FEMorphology::mapRect(const FloatRect& rect, bool) 101 { 102 FloatRect result = rect; 103 result.inflateX(filter()->applyHorizontalScale(m_radiusX)); 104 result.inflateY(filter()->applyVerticalScale(m_radiusY)); 105 return result; 106 } 107 108 bool FEMorphology::setRadiusY(float radiusY) 109 { 110 if (m_radiusY == radiusY) 111 return false; 112 m_radiusY = radiusY; 113 return true; 114 } 115 116 void FEMorphology::platformApplyGeneric(PaintingData* paintingData, int yStart, int yEnd) 117 { 118 Uint8ClampedArray* srcPixelArray = paintingData->srcPixelArray; 119 Uint8ClampedArray* dstPixelArray = paintingData->dstPixelArray; 120 const int width = paintingData->width; 121 const int height = paintingData->height; 122 const int effectWidth = width * 4; 123 const int radiusX = paintingData->radiusX; 124 const int radiusY = paintingData->radiusY; 125 126 Vector<unsigned char> extrema; 127 for (int y = yStart; y < yEnd; ++y) { 128 int extremaStartY = max(0, y - radiusY); 129 int extremaEndY = min(height - 1, y + radiusY); 130 for (unsigned int clrChannel = 0; clrChannel < 4; ++clrChannel) { 131 extrema.clear(); 132 // Compute extremas for each columns 133 for (int x = 0; x <= radiusX; ++x) { 134 unsigned char columnExtrema = srcPixelArray->item(extremaStartY * effectWidth + 4 * x + clrChannel); 135 for (int eY = extremaStartY + 1; eY < extremaEndY; ++eY) { 136 unsigned char pixel = srcPixelArray->item(eY * effectWidth + 4 * x + clrChannel); 137 if ((m_type == FEMORPHOLOGY_OPERATOR_ERODE && pixel <= columnExtrema) 138 || (m_type == FEMORPHOLOGY_OPERATOR_DILATE && pixel >= columnExtrema)) { 139 columnExtrema = pixel; 140 } 141 } 142 143 extrema.append(columnExtrema); 144 } 145 146 // Kernel is filled, get extrema of next column 147 for (int x = 0; x < width; ++x) { 148 const int endX = min(x + radiusX, width - 1); 149 unsigned char columnExtrema = srcPixelArray->item(extremaStartY * effectWidth + endX * 4 + clrChannel); 150 for (int i = extremaStartY + 1; i <= extremaEndY; ++i) { 151 unsigned char pixel = srcPixelArray->item(i * effectWidth + endX * 4 + clrChannel); 152 if ((m_type == FEMORPHOLOGY_OPERATOR_ERODE && pixel <= columnExtrema) 153 || (m_type == FEMORPHOLOGY_OPERATOR_DILATE && pixel >= columnExtrema)) 154 columnExtrema = pixel; 155 } 156 if (x - radiusX >= 0) 157 extrema.remove(0); 158 if (x + radiusX <= width) 159 extrema.append(columnExtrema); 160 161 unsigned char entireExtrema = extrema[0]; 162 for (unsigned kernelIndex = 1; kernelIndex < extrema.size(); ++kernelIndex) { 163 if ((m_type == FEMORPHOLOGY_OPERATOR_ERODE && extrema[kernelIndex] <= entireExtrema) 164 || (m_type == FEMORPHOLOGY_OPERATOR_DILATE && extrema[kernelIndex] >= entireExtrema)) 165 entireExtrema = extrema[kernelIndex]; 166 } 167 dstPixelArray->set(y * effectWidth + 4 * x + clrChannel, entireExtrema); 168 } 169 } 170 } 171 } 172 173 void FEMorphology::platformApplyWorker(PlatformApplyParameters* param) 174 { 175 param->filter->platformApplyGeneric(param->paintingData, param->startY, param->endY); 176 } 177 178 void FEMorphology::platformApply(PaintingData* paintingData) 179 { 180 int optimalThreadNumber = (paintingData->width * paintingData->height) / s_minimalArea; 181 if (optimalThreadNumber > 1) { 182 ParallelJobs<PlatformApplyParameters> parallelJobs(&WebCore::FEMorphology::platformApplyWorker, optimalThreadNumber); 183 int numOfThreads = parallelJobs.numberOfJobs(); 184 if (numOfThreads > 1) { 185 // Split the job into "jobSize"-sized jobs but there a few jobs that need to be slightly larger since 186 // jobSize * jobs < total size. These extras are handled by the remainder "jobsWithExtra". 187 const int jobSize = paintingData->height / numOfThreads; 188 const int jobsWithExtra = paintingData->height % numOfThreads; 189 int currentY = 0; 190 for (int job = numOfThreads - 1; job >= 0; --job) { 191 PlatformApplyParameters& param = parallelJobs.parameter(job); 192 param.filter = this; 193 param.startY = currentY; 194 currentY += job < jobsWithExtra ? jobSize + 1 : jobSize; 195 param.endY = currentY; 196 param.paintingData = paintingData; 197 } 198 parallelJobs.execute(); 199 return; 200 } 201 // Fallback to single thread model 202 } 203 204 platformApplyGeneric(paintingData, 0, paintingData->height); 205 } 206 207 208 void FEMorphology::applySoftware() 209 { 210 FilterEffect* in = inputEffect(0); 211 212 Uint8ClampedArray* dstPixelArray = createPremultipliedImageResult(); 213 if (!dstPixelArray) 214 return; 215 216 setIsAlphaImage(in->isAlphaImage()); 217 if (m_radiusX <= 0 || m_radiusY <= 0) { 218 dstPixelArray->zeroFill(); 219 return; 220 } 221 222 Filter* filter = this->filter(); 223 int radiusX = static_cast<int>(floorf(filter->applyHorizontalScale(m_radiusX))); 224 int radiusY = static_cast<int>(floorf(filter->applyVerticalScale(m_radiusY))); 225 226 IntRect effectDrawingRect = requestedRegionOfInputImageData(in->absolutePaintRect()); 227 RefPtr<Uint8ClampedArray> srcPixelArray = in->asPremultipliedImage(effectDrawingRect); 228 229 PaintingData paintingData; 230 paintingData.srcPixelArray = srcPixelArray.get(); 231 paintingData.dstPixelArray = dstPixelArray; 232 paintingData.width = effectDrawingRect.width(); 233 paintingData.height = effectDrawingRect.height(); 234 paintingData.radiusX = min(effectDrawingRect.width() - 1, radiusX); 235 paintingData.radiusY = min(effectDrawingRect.height() - 1, radiusY); 236 237 platformApply(&paintingData); 238 } 239 240 bool FEMorphology::applySkia() 241 { 242 ImageBuffer* resultImage = createImageBufferResult(); 243 if (!resultImage) 244 return false; 245 246 FilterEffect* in = inputEffect(0); 247 248 IntRect drawingRegion = drawingRegionOfInputImage(in->absolutePaintRect()); 249 250 setIsAlphaImage(in->isAlphaImage()); 251 252 float radiusX = filter()->applyHorizontalScale(m_radiusX); 253 float radiusY = filter()->applyVerticalScale(m_radiusY); 254 255 RefPtr<Image> image = in->asImageBuffer()->copyImage(DontCopyBackingStore); 256 257 SkPaint paint; 258 GraphicsContext* dstContext = resultImage->context(); 259 if (m_type == FEMORPHOLOGY_OPERATOR_DILATE) 260 paint.setImageFilter(new SkDilateImageFilter(radiusX, radiusY))->unref(); 261 else if (m_type == FEMORPHOLOGY_OPERATOR_ERODE) 262 paint.setImageFilter(new SkErodeImageFilter(radiusX, radiusY))->unref(); 263 264 dstContext->saveLayer(0, &paint); 265 dstContext->drawImage(image.get(), drawingRegion.location(), CompositeCopy); 266 dstContext->restoreLayer(); 267 return true; 268 } 269 270 PassRefPtr<SkImageFilter> FEMorphology::createImageFilter(SkiaImageFilterBuilder* builder) 271 { 272 RefPtr<SkImageFilter> input(builder->build(inputEffect(0), operatingColorSpace())); 273 SkScalar radiusX = SkFloatToScalar(filter()->applyHorizontalScale(m_radiusX)); 274 SkScalar radiusY = SkFloatToScalar(filter()->applyVerticalScale(m_radiusY)); 275 if (m_type == FEMORPHOLOGY_OPERATOR_DILATE) 276 return adoptRef(new SkDilateImageFilter(radiusX, radiusY, input.get())); 277 return adoptRef(new SkErodeImageFilter(radiusX, radiusY, input.get())); 278 } 279 280 static TextStream& operator<<(TextStream& ts, const MorphologyOperatorType& type) 281 { 282 switch (type) { 283 case FEMORPHOLOGY_OPERATOR_UNKNOWN: 284 ts << "UNKNOWN"; 285 break; 286 case FEMORPHOLOGY_OPERATOR_ERODE: 287 ts << "ERODE"; 288 break; 289 case FEMORPHOLOGY_OPERATOR_DILATE: 290 ts << "DILATE"; 291 break; 292 } 293 return ts; 294 } 295 296 TextStream& FEMorphology::externalRepresentation(TextStream& ts, int indent) const 297 { 298 writeIndent(ts, indent); 299 ts << "[feMorphology"; 300 FilterEffect::externalRepresentation(ts); 301 ts << " operator=\"" << morphologyOperator() << "\" " 302 << "radius=\"" << radiusX() << ", " << radiusY() << "\"]\n"; 303 inputEffect(0)->externalRepresentation(ts, indent + 1); 304 return ts; 305 } 306 307 } // namespace WebCore 308