• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <wtf/StdLibExtras.h>
36 
37 #include <math.h>
38 
39 #if PLATFORM(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     DEFINE_STATIC_LOCAL(RefPtr<Image>, nullImage, (BitmapImage::create()));;
57     return nullImage.get();
58 }
59 
supportsType(const String & type)60 bool Image::supportsType(const String& type)
61 {
62     return MIMETypeRegistry::isSupportedImageResourceMIMEType(type);
63 }
64 
setData(PassRefPtr<SharedBuffer> data,bool allDataReceived)65 bool Image::setData(PassRefPtr<SharedBuffer> data, bool allDataReceived)
66 {
67     m_data = data;
68     if (!m_data.get())
69         return true;
70 
71     int length = m_data->size();
72     if (!length)
73         return true;
74 
75     return dataChanged(allDataReceived);
76 }
77 
fillWithSolidColor(GraphicsContext * ctxt,const FloatRect & dstRect,const Color & color,ColorSpace styleColorSpace,CompositeOperator op)78 void Image::fillWithSolidColor(GraphicsContext* ctxt, const FloatRect& dstRect, const Color& color, ColorSpace styleColorSpace, CompositeOperator op)
79 {
80     if (color.alpha() <= 0)
81         return;
82 
83     ctxt->save();
84     ctxt->setCompositeOperation(!color.hasAlpha() && op == CompositeSourceOver ? CompositeCopy : op);
85     ctxt->fillRect(dstRect, color, styleColorSpace);
86     ctxt->restore();
87 }
88 
calculatePatternScale(const FloatRect & dstRect,const FloatRect & srcRect,Image::TileRule hRule,Image::TileRule vRule)89 static inline FloatSize calculatePatternScale(const FloatRect& dstRect, const FloatRect& srcRect, Image::TileRule hRule, Image::TileRule vRule)
90 {
91     float scaleX = 1.0f, scaleY = 1.0f;
92 
93     if (hRule == Image::StretchTile)
94         scaleX = dstRect.width() / srcRect.width();
95     if (vRule == Image::StretchTile)
96         scaleY = dstRect.height() / srcRect.height();
97 
98     if (hRule == Image::RepeatTile)
99         scaleX = scaleY;
100     if (vRule == Image::RepeatTile)
101         scaleY = scaleX;
102 
103     return FloatSize(scaleX, scaleY);
104 }
105 
106 
drawTiled(GraphicsContext * ctxt,const FloatRect & destRect,const FloatPoint & srcPoint,const FloatSize & scaledTileSize,ColorSpace styleColorSpace,CompositeOperator op)107 void Image::drawTiled(GraphicsContext* ctxt, const FloatRect& destRect, const FloatPoint& srcPoint, const FloatSize& scaledTileSize, ColorSpace styleColorSpace, CompositeOperator op)
108 {
109     if (mayFillWithSolidColor()) {
110         fillWithSolidColor(ctxt, destRect, solidColor(), styleColorSpace, op);
111         return;
112     }
113 
114     FloatSize intrinsicTileSize = size();
115     if (hasRelativeWidth())
116         intrinsicTileSize.setWidth(scaledTileSize.width());
117     if (hasRelativeHeight())
118         intrinsicTileSize.setHeight(scaledTileSize.height());
119 
120     FloatSize scale(scaledTileSize.width() / intrinsicTileSize.width(),
121                     scaledTileSize.height() / intrinsicTileSize.height());
122 
123     FloatRect oneTileRect;
124     oneTileRect.setX(destRect.x() + fmodf(fmodf(-srcPoint.x(), scaledTileSize.width()) - scaledTileSize.width(), scaledTileSize.width()));
125     oneTileRect.setY(destRect.y() + fmodf(fmodf(-srcPoint.y(), scaledTileSize.height()) - scaledTileSize.height(), scaledTileSize.height()));
126     oneTileRect.setSize(scaledTileSize);
127 
128     // Check and see if a single draw of the image can cover the entire area we are supposed to tile.
129     if (oneTileRect.contains(destRect)) {
130         FloatRect visibleSrcRect;
131         visibleSrcRect.setX((destRect.x() - oneTileRect.x()) / scale.width());
132         visibleSrcRect.setY((destRect.y() - oneTileRect.y()) / scale.height());
133         visibleSrcRect.setWidth(destRect.width() / scale.width());
134         visibleSrcRect.setHeight(destRect.height() / scale.height());
135         draw(ctxt, destRect, visibleSrcRect, styleColorSpace, op);
136         return;
137     }
138 
139     AffineTransform patternTransform = AffineTransform().scaleNonUniform(scale.width(), scale.height());
140     FloatRect tileRect(FloatPoint(), intrinsicTileSize);
141     drawPattern(ctxt, tileRect, patternTransform, oneTileRect.location(), styleColorSpace, op, destRect);
142 
143     startAnimation();
144 }
145 
146 // 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)147 void Image::drawTiled(GraphicsContext* ctxt, const FloatRect& dstRect, const FloatRect& srcRect, TileRule hRule, TileRule vRule, ColorSpace styleColorSpace, CompositeOperator op)
148 {
149     if (mayFillWithSolidColor()) {
150         fillWithSolidColor(ctxt, dstRect, solidColor(), styleColorSpace, op);
151         return;
152     }
153 
154     // FIXME: We do not support 'round' yet.  For now just map it to 'repeat'.
155     if (hRule == RoundTile)
156         hRule = RepeatTile;
157     if (vRule == RoundTile)
158         vRule = RepeatTile;
159 
160     FloatSize scale = calculatePatternScale(dstRect, srcRect, hRule, vRule);
161     AffineTransform patternTransform = AffineTransform().scaleNonUniform(scale.width(), scale.height());
162 
163     // We want to construct the phase such that the pattern is centered (when stretch is not
164     // set for a particular rule).
165     float hPhase = scale.width() * srcRect.x();
166     float vPhase = scale.height() * srcRect.y();
167     if (hRule == Image::RepeatTile)
168         hPhase -= fmodf(dstRect.width(), scale.width() * srcRect.width()) / 2.0f;
169     if (vRule == Image::RepeatTile)
170         vPhase -= fmodf(dstRect.height(), scale.height() * srcRect.height()) / 2.0f;
171     FloatPoint patternPhase(dstRect.x() - hPhase, dstRect.y() - vPhase);
172 
173     drawPattern(ctxt, srcRect, patternTransform, patternPhase, styleColorSpace, op, dstRect);
174 
175     startAnimation();
176 }
177 
178 
179 }
180