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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
/*
* Copyright (C) 2008-2009 Torch Mobile, Inc.
* Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "ImageDecoder.h"
#include <algorithm>
#include <cmath>
#include "BMPImageDecoder.h"
#include "GIFImageDecoder.h"
#include "ICOImageDecoder.h"
#include "JPEGImageDecoder.h"
#include "PNGImageDecoder.h"
#include "WEBPImageDecoder.h"
#include "SharedBuffer.h"
using namespace std;
namespace WebCore {
namespace {
unsigned copyFromSharedBuffer(char* buffer, unsigned bufferLength, const SharedBuffer& sharedBuffer, unsigned offset)
{
unsigned bytesExtracted = 0;
const char* moreData;
while (unsigned moreDataLength = sharedBuffer.getSomeData(moreData, offset)) {
unsigned bytesToCopy = min(bufferLength - bytesExtracted, moreDataLength);
memcpy(buffer + bytesExtracted, moreData, bytesToCopy);
bytesExtracted += bytesToCopy;
if (bytesExtracted == bufferLength)
break;
offset += bytesToCopy;
}
return bytesExtracted;
}
bool matchesGIFSignature(char* contents)
{
return !memcmp(contents, "GIF8", 4);
}
bool matchesPNGSignature(char* contents)
{
return !memcmp(contents, "\x89\x50\x4E\x47", 4);
}
bool matchesJPEGSignature(char* contents)
{
return !memcmp(contents, "\xFF\xD8\xFF", 3);
}
#if USE(WEBP)
bool matchesWebPSignature(char* contents)
{
return !memcmp(contents, "RIFF", 4) && !memcmp(contents + 8, "WEBPVP", 6);
}
#endif
bool matchesBMPSignature(char* contents)
{
return !memcmp(contents, "BM", 2);
}
bool matchesICOSignature(char* contents)
{
return !memcmp(contents, "\x00\x00\x01\x00", 4);
}
bool matchesCURSignature(char* contents)
{
return !memcmp(contents, "\x00\x00\x02\x00", 4);
}
}
#if !OS(ANDROID)
// This method requires BMPImageDecoder, PNGImageDecoder, ICOImageDecoder and
// JPEGDecoder, which aren't used on Android, and which don't all compile.
// TODO: Find a better fix.
ImageDecoder* ImageDecoder::create(const SharedBuffer& data, ImageSource::AlphaOption alphaOption, ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption)
{
static const unsigned lengthOfLongestSignature = 14; // To wit: "RIFF????WEBPVP"
char contents[lengthOfLongestSignature];
unsigned length = copyFromSharedBuffer(contents, lengthOfLongestSignature, data, 0);
if (length < lengthOfLongestSignature)
return 0;
if (matchesGIFSignature(contents))
return new GIFImageDecoder(alphaOption, gammaAndColorProfileOption);
if (matchesPNGSignature(contents))
return new PNGImageDecoder(alphaOption, gammaAndColorProfileOption);
if (matchesJPEGSignature(contents))
return new JPEGImageDecoder(alphaOption, gammaAndColorProfileOption);
#if USE(WEBP)
if (matchesWebPSignature(contents))
return new WEBPImageDecoder(alphaOption, gammaAndColorProfileOption);
#endif
if (matchesBMPSignature(contents))
return new BMPImageDecoder(alphaOption, gammaAndColorProfileOption);
if (matchesICOSignature(contents) || matchesCURSignature(contents))
return new ICOImageDecoder(alphaOption, gammaAndColorProfileOption);
return 0;
}
#endif // !OS(ANDROID)
#if !USE(SKIA)
ImageFrame::ImageFrame()
: m_hasAlpha(false)
, m_status(FrameEmpty)
, m_duration(0)
, m_disposalMethod(DisposeNotSpecified)
, m_premultiplyAlpha(true)
{
}
ImageFrame& ImageFrame::operator=(const ImageFrame& other)
{
if (this == &other)
return *this;
copyReferenceToBitmapData(other);
setOriginalFrameRect(other.originalFrameRect());
setStatus(other.status());
setDuration(other.duration());
setDisposalMethod(other.disposalMethod());
setPremultiplyAlpha(other.premultiplyAlpha());
return *this;
}
void ImageFrame::clearPixelData()
{
m_backingStore.clear();
m_bytes = 0;
m_status = FrameEmpty;
// NOTE: Do not reset other members here; clearFrameBufferCache() calls this
// to free the bitmap data, but other functions like initFrameBuffer() and
// frameComplete() may still need to read other metadata out of this frame
// later.
}
void ImageFrame::zeroFillPixelData()
{
memset(m_bytes, 0, m_size.width() * m_size.height() * sizeof(PixelData));
m_hasAlpha = true;
}
#if !USE(CG)
void ImageFrame::copyReferenceToBitmapData(const ImageFrame& other)
{
ASSERT(this != &other);
copyBitmapData(other);
}
bool ImageFrame::copyBitmapData(const ImageFrame& other)
{
if (this == &other)
return true;
m_backingStore = other.m_backingStore;
m_bytes = m_backingStore.data();
m_size = other.m_size;
setHasAlpha(other.m_hasAlpha);
return true;
}
bool ImageFrame::setSize(int newWidth, int newHeight)
{
// NOTE: This has no way to check for allocation failure if the requested
// size was too big...
m_backingStore.resize(newWidth * newHeight);
m_bytes = m_backingStore.data();
m_size = IntSize(newWidth, newHeight);
zeroFillPixelData();
return true;
}
#endif
bool ImageFrame::hasAlpha() const
{
return m_hasAlpha;
}
void ImageFrame::setHasAlpha(bool alpha)
{
m_hasAlpha = alpha;
}
void ImageFrame::setColorProfile(const ColorProfile& colorProfile)
{
m_colorProfile = colorProfile;
}
void ImageFrame::setStatus(FrameStatus status)
{
m_status = status;
}
int ImageFrame::width() const
{
return m_size.width();
}
int ImageFrame::height() const
{
return m_size.height();
}
#endif
namespace {
enum MatchType {
Exact,
UpperBound,
LowerBound
};
inline void fillScaledValues(Vector<int>& scaledValues, double scaleRate, int length)
{
double inflateRate = 1. / scaleRate;
scaledValues.reserveCapacity(static_cast<int>(length * scaleRate + 0.5));
for (int scaledIndex = 0; ; ++scaledIndex) {
int index = static_cast<int>(scaledIndex * inflateRate + 0.5);
if (index >= length)
break;
scaledValues.append(index);
}
}
template <MatchType type> int getScaledValue(const Vector<int>& scaledValues, int valueToMatch, int searchStart)
{
if (scaledValues.isEmpty())
return valueToMatch;
const int* dataStart = scaledValues.data();
const int* dataEnd = dataStart + scaledValues.size();
const int* matched = std::lower_bound(dataStart + searchStart, dataEnd, valueToMatch);
switch (type) {
case Exact:
return matched != dataEnd && *matched == valueToMatch ? matched - dataStart : -1;
case LowerBound:
return matched != dataEnd && *matched == valueToMatch ? matched - dataStart : matched - dataStart - 1;
case UpperBound:
default:
return matched != dataEnd ? matched - dataStart : -1;
}
}
}
void ImageDecoder::prepareScaleDataIfNecessary()
{
m_scaled = false;
m_scaledColumns.clear();
m_scaledRows.clear();
int width = size().width();
int height = size().height();
int numPixels = height * width;
if (m_maxNumPixels <= 0 || numPixels <= m_maxNumPixels)
return;
m_scaled = true;
double scale = sqrt(m_maxNumPixels / (double)numPixels);
fillScaledValues(m_scaledColumns, scale, width);
fillScaledValues(m_scaledRows, scale, height);
}
int ImageDecoder::upperBoundScaledX(int origX, int searchStart)
{
return getScaledValue<UpperBound>(m_scaledColumns, origX, searchStart);
}
int ImageDecoder::lowerBoundScaledX(int origX, int searchStart)
{
return getScaledValue<LowerBound>(m_scaledColumns, origX, searchStart);
}
int ImageDecoder::upperBoundScaledY(int origY, int searchStart)
{
return getScaledValue<UpperBound>(m_scaledRows, origY, searchStart);
}
int ImageDecoder::lowerBoundScaledY(int origY, int searchStart)
{
return getScaledValue<LowerBound>(m_scaledRows, origY, searchStart);
}
int ImageDecoder::scaledY(int origY, int searchStart)
{
return getScaledValue<Exact>(m_scaledRows, origY, searchStart);
}
}
|