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