1/*
2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2007-2009 Torch Mobile Inc.
4 * Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "Image.h"
29
30#include "BitmapImage.h"
31#include "GraphicsContext.h"
32#include "ImageDecoder.h"
33#include "NotImplemented.h"
34#include "PlatformString.h"
35#include "SharedBuffer.h"
36#include "TransformationMatrix.h"
37#include "WinceGraphicsExtras.h"
38#include <wtf/OwnPtr.h>
39
40#include <windows.h>
41
42namespace WebCore {
43
44NativeImagePtr RGBA32Buffer::asNewNativeImage() const
45{
46 NativeImagePtr ret = SharedBitmap::createInstance(false, width(), height(), false);
47
48 memcpy(ret->bytes(), m_bytes.data(), m_bytes.size() * sizeof(PixelData));
49
50 return ret;
51}
52
53bool FrameData::clear(bool clearMetaData)
54{
55 if (clearMetaData)
56 m_haveMetadata = false;
57
58 if (m_frame) {
59 m_frame = 0;
60 return true;
61 }
62
63 return false;
64}
65
66bool BitmapImage::getHBITMAPOfSize(HBITMAP bmp, LPSIZE size)
67{
68 if (!bmp)
69 return false;
70
71 BITMAP bmpInfo;
72 GetObject(bmp, sizeof(BITMAP), &bmpInfo);
73
74 ASSERT(bmpInfo.bmBitsPixel == 32);
75 int bufferSize = bmpInfo.bmWidthBytes * bmpInfo.bmHeight;
76
77 OwnPtr<HDC> hdc(CreateCompatibleDC(0));
78 HGDIOBJ hOldBmp = SelectObject(hdc.get(), bmp);
79
80 {
81 GraphicsContext gc(hdc.get());
82
83 IntSize imageSize = BitmapImage::size();
84 if (size)
85 drawFrameMatchingSourceSize(&gc, FloatRect(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight), IntSize(*size), DeviceColorSpace, CompositeCopy);
86 else
87 draw(&gc, FloatRect(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight), FloatRect(0, 0, imageSize.width(), imageSize.height()), DeviceColorSpace, CompositeCopy);
88 }
89
90 SelectObject(hdc.get(), hOldBmp);
91
92 return true;
93}
94
95void BitmapImage::drawFrameMatchingSourceSize(GraphicsContext* ctxt, const FloatRect& dstRect, const IntSize& srcSize, ColorSpace styleColorSpace, CompositeOperator compositeOp)
96{
97 int frames = frameCount();
98 for (int i = 0; i < frames; ++i) {
99 RefPtr<SharedBitmap> bmp = frameAtIndex(i);
100 if (bmp && bmp->height() == (size_t)srcSize.height() && bmp->width() == (size_t)srcSize.width()) {
101 size_t currentFrame = m_currentFrame;
102 m_currentFrame = i;
103 draw(ctxt, dstRect, FloatRect(0, 0, srcSize.width(), srcSize.height()), styleColorSpace, compositeOp);
104 m_currentFrame = currentFrame;
105 return;
106 }
107 }
108
109 // No image of the correct size was found, fallback to drawing the current frame
110 IntSize imageSize = BitmapImage::size();
111 draw(ctxt, dstRect, FloatRect(0, 0, imageSize.width(), imageSize.height()), styleColorSpace, compositeOp);
112}
113
114void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dstRect, const FloatRect& srcRectIn, ColorSpace styleColorSpace, CompositeOperator compositeOp)
115{
116 if (!m_source.initialized())
117 return;
118
119 RefPtr<SharedBitmap> bmp = frameAtIndex(m_currentFrame);
120 if (mayFillWithSolidColor())
121 fillWithSolidColor(ctxt, dstRect, solidColor(), styleColorSpace, compositeOp);
122 else {
123 IntRect intSrcRect(srcRectIn);
124
125 if (bmp->width() != m_source.size().width()) {
126 double rate = (double)bmp->width() / m_source.size().width();
127 int temp = stableRound(srcRectIn.right() * rate);
128 intSrcRect.setX(stableRound(srcRectIn.x() * rate));
129 intSrcRect.setWidth(temp - intSrcRect.x());
130 temp = stableRound(srcRectIn.bottom() * rate);
131 intSrcRect.setY(stableRound(srcRectIn.y() * rate));
132 intSrcRect.setHeight(temp - intSrcRect.y());
133 }
134 bmp->draw(ctxt, enclosingIntRect(dstRect), intSrcRect, styleColorSpace, compositeOp);
135 }
136
137 startAnimation();
138}
139
140void Image::drawPattern(GraphicsContext* ctxt, const FloatRect& tileRect, const AffineTransform& patternTransform,
141 const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator op, const FloatRect& destRect)
142{
143 notImplemented();
144}
145
146void BitmapImage::drawPattern(GraphicsContext* ctxt, const FloatRect& tileRectIn, const AffineTransform& patternTransform,
147 const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator op, const FloatRect& destRect)
148{
149 RefPtr<SharedBitmap> bmp = nativeImageForCurrentFrame();
150 if (bmp)
151 bmp->drawPattern(ctxt, tileRectIn, patternTransform, phase, styleColorSpace, op, destRect, m_source.size());
152}
153
154void BitmapImage::checkForSolidColor()
155{
156 if (m_checkedForSolidColor)
157 return;
158
159 if (frameCount() != 1) {
160 m_isSolidColor = false;
161 m_checkedForSolidColor = true;
162 return;
163 }
164
165 RefPtr<SharedBitmap> bmp = frameAtIndex(0);
166 if (!bmp || !bmp->validHeight()) {
167 m_isSolidColor = false;
168 return;
169 }
170
171 if (bmp->width() == 1 && bmp->validHeight() == 1) {
172 m_isSolidColor = true;
173
174 if (bmp->is16bit()) {
175 unsigned short c = ((unsigned short *)bmp->bytes())[0];
176 int r = (c >> 7) & 0xF8;
177 int g = (c >> 2) & 0xF8;
178 int b = (c << 3) & 0xF8;
179 if (bmp->usesTransparentColor() && bmp->transparentColor() == RGB(r, g, b))
180 m_solidColor = Color(r, g, b, 0);
181 else
182 m_solidColor = Color(r, g, b);
183 } else {
184 unsigned c = ((unsigned *)bmp->bytes())[0];
185 m_solidColor = Color(c);
186 }
187
188 if (bmp->validHeight() == bmp->height())
189 m_checkedForSolidColor = true;
190 } else {
191 m_isSolidColor = false;
192 m_checkedForSolidColor = true;
193 }
194}
195
196} // namespace WebCore