initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / Image.cpp
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/MainThread.h>
38 #include <wtf/StdLibExtras.h>
39
40 #if USE(CG)
41 #include <CoreFoundation/CoreFoundation.h>
42 #endif
43
44 namespace WebCore {
45
46 Image::Image(ImageObserver* observer)
47     : m_imageObserver(observer)
48 {
49 }
50
51 Image::~Image()
52 {
53 }
54
55 Image* Image::nullImage()
56 {
57     ASSERT(isMainThread());
58     DEFINE_STATIC_LOCAL(RefPtr<Image>, nullImage, (BitmapImage::create()));;
59     return nullImage.get();
60 }
61
62 bool Image::supportsType(const String& type)
63 {
64     return MIMETypeRegistry::isSupportedImageResourceMIMEType(type); 
65
66
67 bool Image::setData(PassRefPtr<SharedBuffer> data, bool allDataReceived)
68 {
69     m_data = data;
70     if (!m_data.get())
71         return true;
72
73     int length = m_data->size();
74     if (!length)
75         return true;
76     
77     return dataChanged(allDataReceived);
78 }
79
80 void Image::fillWithSolidColor(GraphicsContext* ctxt, const FloatRect& dstRect, const Color& color, ColorSpace styleColorSpace, CompositeOperator op)
81 {
82     if (color.alpha() <= 0)
83         return;
84     
85     CompositeOperator previousOperator = ctxt->compositeOperation();
86     ctxt->setCompositeOperation(!color.hasAlpha() && op == CompositeSourceOver ? CompositeCopy : op);
87     ctxt->fillRect(dstRect, color, styleColorSpace);
88     ctxt->setCompositeOperation(previousOperator);
89 }
90
91 void Image::drawTiled(GraphicsContext* ctxt, const FloatRect& destRect, const FloatPoint& srcPoint, const FloatSize& scaledTileSize, ColorSpace styleColorSpace, CompositeOperator op)
92 {    
93     if (mayFillWithSolidColor()) {
94         fillWithSolidColor(ctxt, destRect, solidColor(), styleColorSpace, op);
95         return;
96     }
97
98     // See <https://webkit.org/b/59043>.
99 #if !PLATFORM(WX)
100     ASSERT(!isBitmapImage() || notSolidColor());
101 #endif
102
103     FloatSize intrinsicTileSize = size();
104     if (hasRelativeWidth())
105         intrinsicTileSize.setWidth(scaledTileSize.width());
106     if (hasRelativeHeight())
107         intrinsicTileSize.setHeight(scaledTileSize.height());
108
109     FloatSize scale(scaledTileSize.width() / intrinsicTileSize.width(),
110                     scaledTileSize.height() / intrinsicTileSize.height());
111
112     FloatRect oneTileRect;
113     oneTileRect.setX(destRect.x() + fmodf(fmodf(-srcPoint.x(), scaledTileSize.width()) - scaledTileSize.width(), scaledTileSize.width()));
114     oneTileRect.setY(destRect.y() + fmodf(fmodf(-srcPoint.y(), scaledTileSize.height()) - scaledTileSize.height(), scaledTileSize.height()));
115     oneTileRect.setSize(scaledTileSize);
116     
117     // Check and see if a single draw of the image can cover the entire area we are supposed to tile.    
118     if (oneTileRect.contains(destRect)) {
119         FloatRect visibleSrcRect;
120         visibleSrcRect.setX((destRect.x() - oneTileRect.x()) / scale.width());
121         visibleSrcRect.setY((destRect.y() - oneTileRect.y()) / scale.height());
122         visibleSrcRect.setWidth(destRect.width() / scale.width());
123         visibleSrcRect.setHeight(destRect.height() / scale.height());
124         draw(ctxt, destRect, visibleSrcRect, styleColorSpace, op);
125         return;
126     }
127
128     AffineTransform patternTransform = AffineTransform().scaleNonUniform(scale.width(), scale.height());
129     FloatRect tileRect(FloatPoint(), intrinsicTileSize);    
130     drawPattern(ctxt, tileRect, patternTransform, oneTileRect.location(), styleColorSpace, op, destRect);
131     
132     startAnimation();
133 }
134
135 // FIXME: Merge with the other drawTiled eventually, since we need a combination of both for some things.
136 void Image::drawTiled(GraphicsContext* ctxt, const FloatRect& dstRect, const FloatRect& srcRect,
137     const FloatSize& tileScaleFactor, TileRule hRule, TileRule vRule, ColorSpace styleColorSpace, CompositeOperator op)
138 {    
139     if (mayFillWithSolidColor()) {
140         fillWithSolidColor(ctxt, dstRect, solidColor(), styleColorSpace, op);
141         return;
142     }
143     
144     // FIXME: We do not support 'round' or 'space' yet. For now just map them to 'repeat'.
145     if (hRule == RoundTile || hRule == SpaceTile)
146         hRule = RepeatTile;
147     if (vRule == RoundTile || vRule == SpaceTile)
148         vRule = RepeatTile;
149
150     AffineTransform patternTransform = AffineTransform().scaleNonUniform(tileScaleFactor.width(), tileScaleFactor.height());
151
152     // We want to construct the phase such that the pattern is centered (when stretch is not
153     // set for a particular rule).
154     float hPhase = tileScaleFactor.width() * srcRect.x();
155     float vPhase = tileScaleFactor.height() * srcRect.y();
156     float scaledTileWidth = tileScaleFactor.width() * srcRect.width();
157     float scaledTileHeight = tileScaleFactor.height() * srcRect.height();
158     if (hRule == Image::RepeatTile)
159         hPhase -= (dstRect.width() - scaledTileWidth) / 2;
160     if (vRule == Image::RepeatTile)
161         vPhase -= (dstRect.height() - scaledTileHeight) / 2; 
162     FloatPoint patternPhase(dstRect.x() - hPhase, dstRect.y() - vPhase);
163     
164     drawPattern(ctxt, srcRect, patternTransform, patternPhase, styleColorSpace, op, dstRect);
165
166     startAnimation();
167 }
168
169
170 }