1 /*
2  * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
3  * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "config.h"
28 #include "Image.h"
29 
30 #include "AffineTransform.h"
31 #include "BitmapImage.h"
32 #include "GraphicsContext.h"
33 #include "IntRect.h"
34 #include "MIMETypeRegistry.h"
35 #include "SharedBuffer.h"
36 #include <math.h>
37 #include <wtf/StdLibExtras.h>
38 
39 #if USE(CG)
40 #include <CoreFoundation/CoreFoundation.h>
41 #endif
42 
43 namespace WebCore {
44 
Image(ImageObserver * observer)45 Image::Image(ImageObserver* observer)
46     : m_imageObserver(observer)
47 {
48 }
49 
~Image()50 Image::~Image()
51 {
52 }
53 
nullImage()54 Image* Image::nullImage()
55 {
56     ASSERT(isMainThread());
57     DEFINE_STATIC_LOCAL(RefPtr<Image>, nullImage, (BitmapImage::create()));;
58     return nullImage.get();
59 }
60 
supportsType(const String & type)61 bool Image::supportsType(const String& type)
62 {
63     return MIMETypeRegistry::isSupportedImageResourceMIMEType(type);
64 }
65 
setData(PassRefPtr<SharedBuffer> data,bool allDataReceived)66 bool Image::setData(PassRefPtr<SharedBuffer> data, bool allDataReceived)
67 {
68     m_data = data;
69     if (!m_data.get())
70         return true;
71 
72     int length = m_data->size();
73     if (!length)
74         return true;
75 
76     return dataChanged(allDataReceived);
77 }
78 
fillWithSolidColor(GraphicsContext * ctxt,const FloatRect & dstRect,const Color & color,ColorSpace styleColorSpace,CompositeOperator op)79 void Image::fillWithSolidColor(GraphicsContext* ctxt, const FloatRect& dstRect, const Color& color, ColorSpace styleColorSpace, CompositeOperator op)
80 {
81     if (color.alpha() <= 0)
82         return;
83 
84     CompositeOperator previousOperator = ctxt->compositeOperation();
85     ctxt->setCompositeOperation(!color.hasAlpha() && op == CompositeSourceOver ? CompositeCopy : op);
86     ctxt->fillRect(dstRect, color, styleColorSpace);
87     ctxt->setCompositeOperation(previousOperator);
88 }
89 
calculatePatternScale(const FloatRect & dstRect,const FloatRect & srcRect,Image::TileRule hRule,Image::TileRule vRule)90 static inline FloatSize calculatePatternScale(const FloatRect& dstRect, const FloatRect& srcRect, Image::TileRule hRule, Image::TileRule vRule)
91 {
92     float scaleX = 1.0f, scaleY = 1.0f;
93 
94     if (hRule == Image::StretchTile)
95         scaleX = dstRect.width() / srcRect.width();
96     if (vRule == Image::StretchTile)
97         scaleY = dstRect.height() / srcRect.height();
98 
99     if (hRule == Image::RepeatTile)
100         scaleX = scaleY;
101     if (vRule == Image::RepeatTile)
102         scaleY = scaleX;
103 
104     return FloatSize(scaleX, scaleY);
105 }
106 
107 
drawTiled(GraphicsContext * ctxt,const FloatRect & destRect,const FloatPoint & srcPoint,const FloatSize & scaledTileSize,ColorSpace styleColorSpace,CompositeOperator op)108 void Image::drawTiled(GraphicsContext* ctxt, const FloatRect& destRect, const FloatPoint& srcPoint, const FloatSize& scaledTileSize, ColorSpace styleColorSpace, CompositeOperator op)
109 {
110     if (mayFillWithSolidColor()) {
111         fillWithSolidColor(ctxt, destRect, solidColor(), styleColorSpace, op);
112         return;
113     }
114 
115     // See <https://webkit.org/b/59043>.
116 #if !PLATFORM(WX)
117     ASSERT(!isBitmapImage() || static_cast<BitmapImage*>(this)->notSolidColor());
118 #endif
119 
120     FloatSize intrinsicTileSize = size();
121     if (hasRelativeWidth())
122         intrinsicTileSize.setWidth(scaledTileSize.width());
123     if (hasRelativeHeight())
124         intrinsicTileSize.setHeight(scaledTileSize.height());
125 
126     FloatSize scale(scaledTileSize.width() / intrinsicTileSize.width(),
127                     scaledTileSize.height() / intrinsicTileSize.height());
128 
129     FloatRect oneTileRect;
130     oneTileRect.setX(destRect.x() + fmodf(fmodf(-srcPoint.x(), scaledTileSize.width()) - scaledTileSize.width(), scaledTileSize.width()));
131     oneTileRect.setY(destRect.y() + fmodf(fmodf(-srcPoint.y(), scaledTileSize.height()) - scaledTileSize.height(), scaledTileSize.height()));
132     oneTileRect.setSize(scaledTileSize);
133 
134     // Check and see if a single draw of the image can cover the entire area we are supposed to tile.
135     if (oneTileRect.contains(destRect)) {
136         FloatRect visibleSrcRect;
137         visibleSrcRect.setX((destRect.x() - oneTileRect.x()) / scale.width());
138         visibleSrcRect.setY((destRect.y() - oneTileRect.y()) / scale.height());
139         visibleSrcRect.setWidth(destRect.width() / scale.width());
140         visibleSrcRect.setHeight(destRect.height() / scale.height());
141         draw(ctxt, destRect, visibleSrcRect, styleColorSpace, op);
142         return;
143     }
144 
145     AffineTransform patternTransform = AffineTransform().scaleNonUniform(scale.width(), scale.height());
146     FloatRect tileRect(FloatPoint(), intrinsicTileSize);
147     drawPattern(ctxt, tileRect, patternTransform, oneTileRect.location(), styleColorSpace, op, destRect);
148 
149     startAnimation();
150 }
151 
152 // FIXME: Merge with the other drawTiled eventually, since we need a combination of both for some things.
drawTiled(GraphicsContext * ctxt,const FloatRect & dstRect,const FloatRect & srcRect,TileRule hRule,TileRule vRule,ColorSpace styleColorSpace,CompositeOperator op)153 void Image::drawTiled(GraphicsContext* ctxt, const FloatRect& dstRect, const FloatRect& srcRect, TileRule hRule, TileRule vRule, ColorSpace styleColorSpace, CompositeOperator op)
154 {
155     if (mayFillWithSolidColor()) {
156         fillWithSolidColor(ctxt, dstRect, solidColor(), styleColorSpace, op);
157         return;
158     }
159 
160     // FIXME: We do not support 'round' yet.  For now just map it to 'repeat'.
161     if (hRule == RoundTile)
162         hRule = RepeatTile;
163     if (vRule == RoundTile)
164         vRule = RepeatTile;
165 
166     FloatSize scale = calculatePatternScale(dstRect, srcRect, hRule, vRule);
167     AffineTransform patternTransform = AffineTransform().scaleNonUniform(scale.width(), scale.height());
168 
169     // We want to construct the phase such that the pattern is centered (when stretch is not
170     // set for a particular rule).
171     float hPhase = scale.width() * srcRect.x();
172     float vPhase = scale.height() * srcRect.y();
173     if (hRule == Image::RepeatTile)
174         hPhase -= fmodf(dstRect.width(), scale.width() * srcRect.width()) / 2.0f;
175     if (vRule == Image::RepeatTile)
176         vPhase -= fmodf(dstRect.height(), scale.height() * srcRect.height()) / 2.0f;
177     FloatPoint patternPhase(dstRect.x() - hPhase, dstRect.y() - vPhase);
178 
179     drawPattern(ctxt, srcRect, patternTransform, patternPhase, styleColorSpace, op, dstRect);
180 
181     startAnimation();
182 }
183 
184 
185 }
186