1 /*
2 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27
28 #if ENABLE(SVG)
29 #include "SVGResourceMasker.h"
30
31 #include "CanvasPixelArray.h"
32 #include "Image.h"
33 #include "ImageBuffer.h"
34 #include "ImageData.h"
35 #include "GraphicsContext.h"
36 #include "SVGMaskElement.h"
37 #include "SVGRenderSupport.h"
38 #include "SVGRenderStyle.h"
39 #include "TextStream.h"
40
41 #include <wtf/ByteArray.h>
42
43 using namespace std;
44
45 namespace WebCore {
46
SVGResourceMasker(const SVGMaskElement * ownerElement)47 SVGResourceMasker::SVGResourceMasker(const SVGMaskElement* ownerElement)
48 : SVGResource()
49 , m_ownerElement(ownerElement)
50 {
51 }
52
~SVGResourceMasker()53 SVGResourceMasker::~SVGResourceMasker()
54 {
55 }
56
invalidate()57 void SVGResourceMasker::invalidate()
58 {
59 SVGResource::invalidate();
60 m_mask.clear();
61 }
62
applyMask(GraphicsContext * context,const FloatRect & boundingBox)63 void SVGResourceMasker::applyMask(GraphicsContext* context, const FloatRect& boundingBox)
64 {
65 if (!m_mask)
66 m_mask = m_ownerElement->drawMaskerContent(boundingBox, m_maskRect);
67
68 if (!m_mask)
69 return;
70
71 IntSize imageSize(m_mask->size());
72 IntRect intImageRect(0, 0, imageSize.width(), imageSize.height());
73
74 // Create new ImageBuffer to apply luminance
75 OwnPtr<ImageBuffer> luminancedImage = ImageBuffer::create(imageSize);
76 if (!luminancedImage)
77 return;
78
79 PassRefPtr<CanvasPixelArray> srcPixelArray(m_mask->getImageData(intImageRect)->data());
80 PassRefPtr<ImageData> destImageData(luminancedImage->getImageData(intImageRect));
81
82 for (unsigned pixelOffset = 0; pixelOffset < srcPixelArray->length(); pixelOffset++) {
83 unsigned pixelByteOffset = pixelOffset * 4;
84
85 unsigned char r = 0, g = 0, b = 0, a = 0;
86 srcPixelArray->get(pixelByteOffset, r);
87 srcPixelArray->get(pixelByteOffset + 1, g);
88 srcPixelArray->get(pixelByteOffset + 2, b);
89 srcPixelArray->get(pixelByteOffset + 3, a);
90
91 double luma = (r * 0.2125 + g * 0.7154 + b * 0.0721) * ((double)a / 255.0);
92
93 destImageData->data()->set(pixelByteOffset + 3, luma);
94 }
95
96 luminancedImage->putImageData(destImageData.get(), intImageRect, IntPoint(0, 0));
97
98 context->clipToImageBuffer(m_maskRect, luminancedImage.get());
99 }
100
externalRepresentation(TextStream & ts) const101 TextStream& SVGResourceMasker::externalRepresentation(TextStream& ts) const
102 {
103 ts << "[type=MASKER]";
104 return ts;
105 }
106
getMaskerById(Document * document,const AtomicString & id)107 SVGResourceMasker* getMaskerById(Document* document, const AtomicString& id)
108 {
109 SVGResource* resource = getResourceById(document, id);
110 if (resource && resource->isMasker())
111 return static_cast<SVGResourceMasker*>(resource);
112
113 return 0;
114 }
115
116 } // namespace WebCore
117
118 #endif
119