~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/platform/graphics/Image.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

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