File: ImageBlackBerry.cpp

package info (click to toggle)
qtwebkit-opensource-src 5.7.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 291,692 kB
  • ctags: 268,122
  • sloc: cpp: 1,360,420; python: 70,286; ansic: 42,986; perl: 35,476; ruby: 12,236; objc: 9,465; xml: 8,396; asm: 3,873; yacc: 2,397; sh: 1,647; makefile: 650; lex: 644; java: 110
file content (215 lines) | stat: -rw-r--r-- 7,743 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 * Copyright (C) 2010, 2011, 2012, 2013 Research In Motion Limited. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"
#include "Image.h"

#include "AffineTransform.h"
#include "BitmapImage.h"
#include "FloatConversion.h"
#include "FloatRect.h"
#include "GraphicsContext.h"
#include "ImageBuffer.h"
#include "ImageDecoder.h"
#include "ImageObserver.h"
#include "IntSize.h"
#include "NotImplemented.h"
#include "SharedBuffer.h"

#include <BlackBerryPlatformGraphicsContext.h>
#include <BlackBerryPlatformResourceStore.h>
#include <wtf/MathExtras.h>

using BlackBerry::Platform::ResourceData;
using BlackBerry::Platform::ResourceStore;

namespace WebCore {

PassRefPtr<Image> Image::loadPlatformResource(const char *name)
{
    ResourceData data = ResourceStore::instance()->requestResource(BlackBerry::Platform::String::fromUtf8(name));
    if (!data.data())
        return BitmapImage::nullImage();

    RefPtr<SharedBuffer> buffer = SharedBuffer::create(data.data(), data.len());
    if (!buffer)
        return BitmapImage::nullImage();

    RefPtr<BitmapImage> img = BitmapImage::create();
    img->setData(buffer.release(), true /* allDataReceived */);
    return img.release();
}

PassNativeImagePtr ImageFrame::asNewNativeImage() const
{
    return new BlackBerry::Platform::Graphics::TiledImage(m_size, m_bytes);
}

bool FrameData::clear(bool clearMetadata)
{
    if (clearMetadata)
        m_haveMetadata = false;

    if (m_frame) {
        delete m_frame;
        m_frame = 0;
        return true;
    }
    return false;
}

BitmapImage::BitmapImage(PassNativeImagePtr nativeImage, ImageObserver* observer)
    : Image(observer)
    , m_currentFrame(0)
    , m_frames(0)
    , m_frameTimer(0)
    , m_repetitionCount(cAnimationNone)
    , m_repetitionCountStatus(Unknown)
    , m_repetitionsComplete(0)
    , m_decodedSize(0)
    , m_frameCount(1)
    , m_isSolidColor(false)
    , m_checkedForSolidColor(false)
    , m_animationFinished(true)
    , m_allDataReceived(true)
    , m_haveSize(true)
    , m_sizeAvailable(true)
    , m_haveFrameCount(true)
{
    int width = nativeImage->size().width();
    int height = nativeImage->size().height();
    m_decodedSize = width * height * 4;
    m_size = IntSize(width, height);

    m_frames.grow(1);
    m_frames[0].m_frame = nativeImage;
    m_frames[0].m_hasAlpha = nativeImage->hasAlpha();
    m_frames[0].m_haveMetadata = true;
    checkForSolidColor();
}

void BitmapImage::checkForSolidColor()
{
    m_isSolidColor = size().width() == 1 && size().height() == 1 && frameCount() == 1;
    m_checkedForSolidColor = true;
    if (m_isSolidColor) {
        unsigned buffer;
        NativeImagePtr image = nativeImageForCurrentFrame();
        image->readPixels(&buffer, 1);
        m_solidColor = Color(buffer);
    }
}

void BitmapImage::invalidatePlatformData()
{
}

void BitmapImage::draw(GraphicsContext* context, const FloatRect& dstRect, const FloatRect& srcRect, ColorSpace styleColorSpace, CompositeOperator op, BlendMode blendMode)
{
    draw(context, dstRect, srcRect, styleColorSpace, op, blendMode, DoNotRespectImageOrientation);
}

void BitmapImage::draw(GraphicsContext* context, const FloatRect& dstRect, const FloatRect& srcRect, ColorSpace, CompositeOperator op, BlendMode, RespectImageOrientationEnum shouldRespectImageOrientation)
{
    startAnimation();

    NativeImagePtr image = nativeImageForCurrentFrame();
    if (!image)
        return;

    FloatRect normDstRect = dstRect.normalized();
    FloatRect normSrcRect = srcRect.normalized();

    if (normSrcRect.isEmpty() || normDstRect.isEmpty())
        return; // Nothing to draw.

#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
    normSrcRect = adjustSourceRectForDownSampling(normSrcRect, image->size());
#endif

    // use similar orientation code as ImageSkia
    ImageOrientation orientation = DefaultImageOrientation;
    if (shouldRespectImageOrientation == RespectImageOrientation)
        orientation = frameOrientationAtIndex(m_currentFrame);

    GraphicsContextStateSaver saveContext(*context, false);
    if (orientation != DefaultImageOrientation) {
        saveContext.save();

        // ImageOrientation expects the origin to be at (0, 0)
        context->translate(normDstRect.x(), normDstRect.y());
        normDstRect.setLocation(FloatPoint());

        context->concatCTM(orientation.transformFromDefault(normDstRect.size()));
        if (orientation.usesWidthAsHeight()) {
            // The destination rect will have its width and height already reversed for the orientation of
            // the image, as it was needed for page layout, so we need to reverse it back here.
            normDstRect = FloatRect(normDstRect.x(), normDstRect.y(), normDstRect.height(), normDstRect.width());
        }
    }

    CompositeOperator oldOperator = context->compositeOperation();
    context->setCompositeOperation(op);
    context->platformContext()->addImage(normDstRect, normSrcRect, image);
    context->setCompositeOperation(oldOperator);

    if (ImageObserver* observer = imageObserver())
        observer->didDraw(this);
}

void Image::drawPattern(GraphicsContext* context, const FloatRect& srcRect, const AffineTransform& patternTransformation, const FloatPoint& phase, ColorSpace, CompositeOperator op, const FloatRect& dstRect, BlendMode)
{
    NativeImagePtr image = nativeImageForCurrentFrame();
    if (!image)
        return;

    // patternTransformation contains the tile scale factor relative to
    // document coordinates. phase is given in document coordinates and
    // corresponds to a texture drawn with src starting at (0, 0).
    // Thus, dstRect starts at phase + patternTransformation.mapPoint(srcRect.location()).
    AffineTransform srcTransform;
    srcTransform.translate(phase.x(), phase.y());
    srcTransform.multiply(patternTransformation);
#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
    FloatRect srcRectLocal(srcRect);
    const IntSize unscaledSize = size();
    const IntSize scaledSize = image->size();
    if (unscaledSize != scaledSize) {
        srcRectLocal = adjustSourceRectForDownSampling(srcRect, scaledSize);
        // If the image has been down sampled, the transformation should take the scale into account.
        float xscale = static_cast<float>(unscaledSize.width()) / scaledSize.width();
        float yscale = static_cast<float>(unscaledSize.height()) / scaledSize.height();
        srcTransform.scaleNonUniform(xscale, yscale);
    }
#else
    FloatRect srcRectLocal(srcRect);
#endif
    srcTransform.translate(srcRectLocal.x(), srcRectLocal.y());
    double srcTransformArray[6] = {
        srcTransform.a(), srcTransform.b(),
        srcTransform.c(), srcTransform.d(),
        srcTransform.e(), srcTransform.f()
    };

    CompositeOperator oldOperator = context->compositeOperation();
    context->setCompositeOperation(op);
    context->platformContext()->addPattern(dstRect, srcRectLocal, srcTransformArray, image);
    context->setCompositeOperation(oldOperator);
}

} // namespace WebCore