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
|
/*
* Copyright 2006 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkImageDecoder.h"
#include "SkBitmap.h"
#include "SkPixelRef.h"
#include "SkStream.h"
#include "SkTemplates.h"
#include "SkCanvas.h"
SkVMMemoryReporter::~SkVMMemoryReporter() {
}
const char *SkImageDecoder::kFormatName[] = {
"Unknown Format",
"BMP",
"GIF",
"ICO",
"JPEG",
"PNG",
"WBMP",
"WEBP",
};
static SkBitmap::Config gDeviceConfig = SkBitmap::kNo_Config;
SkBitmap::Config SkImageDecoder::GetDeviceConfig()
{
return gDeviceConfig;
}
void SkImageDecoder::SetDeviceConfig(SkBitmap::Config config)
{
gDeviceConfig = config;
}
///////////////////////////////////////////////////////////////////////////////
SkImageDecoder::SkImageDecoder()
: fReporter(NULL), fPeeker(NULL), fChooser(NULL), fAllocator(NULL),
fSampleSize(1), fDefaultPref(SkBitmap::kNo_Config), fDitherImage(true),
fUsePrefTable(false),fPreferQualityOverSpeed(false) {
}
SkImageDecoder::~SkImageDecoder() {
SkSafeUnref(fPeeker);
SkSafeUnref(fChooser);
SkSafeUnref(fAllocator);
SkSafeUnref(fReporter);
}
SkImageDecoder::Format SkImageDecoder::getFormat() const {
return kUnknown_Format;
}
SkImageDecoder::Peeker* SkImageDecoder::setPeeker(Peeker* peeker) {
SkRefCnt_SafeAssign(fPeeker, peeker);
return peeker;
}
SkImageDecoder::Chooser* SkImageDecoder::setChooser(Chooser* chooser) {
SkRefCnt_SafeAssign(fChooser, chooser);
return chooser;
}
SkBitmap::Allocator* SkImageDecoder::setAllocator(SkBitmap::Allocator* alloc) {
SkRefCnt_SafeAssign(fAllocator, alloc);
return alloc;
}
SkVMMemoryReporter* SkImageDecoder::setReporter(SkVMMemoryReporter* reporter) {
SkRefCnt_SafeAssign(fReporter, reporter);
return reporter;
}
void SkImageDecoder::setSampleSize(int size) {
if (size < 1) {
size = 1;
}
fSampleSize = size;
}
bool SkImageDecoder::chooseFromOneChoice(SkBitmap::Config config, int width,
int height) const {
Chooser* chooser = fChooser;
if (NULL == chooser) { // no chooser, we just say YES to decoding :)
return true;
}
chooser->begin(1);
chooser->inspect(0, config, width, height);
return chooser->choose() == 0;
}
bool SkImageDecoder::allocPixelRef(SkBitmap* bitmap,
SkColorTable* ctable) const {
return bitmap->allocPixels(fAllocator, ctable);
}
///////////////////////////////////////////////////////////////////////////////
void SkImageDecoder::setPrefConfigTable(const SkBitmap::Config pref[6]) {
if (NULL == pref) {
fUsePrefTable = false;
} else {
fUsePrefTable = true;
memcpy(fPrefTable, pref, sizeof(fPrefTable));
}
}
SkBitmap::Config SkImageDecoder::getPrefConfig(SrcDepth srcDepth,
bool srcHasAlpha) const {
SkBitmap::Config config;
if (fUsePrefTable) {
int index = 0;
switch (srcDepth) {
case kIndex_SrcDepth:
index = 0;
break;
case k16Bit_SrcDepth:
index = 2;
break;
case k32Bit_SrcDepth:
index = 4;
break;
}
if (srcHasAlpha) {
index += 1;
}
config = fPrefTable[index];
} else {
config = fDefaultPref;
}
if (SkBitmap::kNo_Config == config) {
config = SkImageDecoder::GetDeviceConfig();
}
return config;
}
bool SkImageDecoder::decode(SkStream* stream, SkBitmap* bm,
SkBitmap::Config pref, Mode mode, bool reuseBitmap) {
// pass a temporary bitmap, so that if we return false, we are assured of
// leaving the caller's bitmap untouched.
SkBitmap tmp;
// we reset this to false before calling onDecode
fShouldCancelDecode = false;
// assign this, for use by getPrefConfig(), in case fUsePrefTable is false
fDefaultPref = pref;
if (reuseBitmap) {
SkAutoLockPixels alp(*bm);
if (bm->getPixels() != NULL) {
return this->onDecode(stream, bm, mode);
}
}
if (!this->onDecode(stream, &tmp, mode)) {
return false;
}
bm->swap(tmp);
return true;
}
bool SkImageDecoder::decodeRegion(SkBitmap* bm, SkIRect rect,
SkBitmap::Config pref) {
// we reset this to false before calling onDecodeRegion
fShouldCancelDecode = false;
// assign this, for use by getPrefConfig(), in case fUsePrefTable is false
fDefaultPref = pref;
if (!this->onDecodeRegion(bm, rect)) {
return false;
}
return true;
}
bool SkImageDecoder::buildTileIndex(SkStream* stream,
int *width, int *height) {
// we reset this to false before calling onBuildTileIndex
fShouldCancelDecode = false;
return this->onBuildTileIndex(stream, width, height);
}
void SkImageDecoder::cropBitmap(SkBitmap *dest, SkBitmap *src,
int sampleSize, int destX, int destY,
int width, int height, int srcX, int srcY) {
int w = width / sampleSize;
int h = height / sampleSize;
if (w == src->width() && h == src->height() &&
(srcX - destX) / sampleSize == 0 &&
(srcY - destY) / sampleSize == 0 &&
dest->isNull()
) {
// The output rect is the same as the decode result
dest->swap(*src);
return;
}
// if the destination has no pixels then we must allocate them.
if (dest->isNull()) {
// The output rect is smaller than the decode result
dest->setConfig(src->getConfig(), w, h);
dest->setIsOpaque(src->isOpaque());
if (!this->allocPixelRef(dest, NULL)) {
SkDEBUGF(("failed to allocate pixels needed to crop the bitmap"));
return;
}
}
// check to see if the destination is large enough to decode the desired
// region. If this assert fails we will just draw as much of the source
// into the destination that we can.
SkASSERT(dest->width() >= w && dest->height() >= h);
// Set the Src_Mode for the paint to prevent transparency issue in the
// dest in the event that the dest was being re-used.
SkPaint paint;
paint.setXfermodeMode(SkXfermode::kSrc_Mode);
SkCanvas canvas(*dest);
canvas.drawSprite(*src, (srcX - destX) / sampleSize,
(srcY - destY) / sampleSize,
&paint);
}
///////////////////////////////////////////////////////////////////////////////
bool SkImageDecoder::DecodeFile(const char file[], SkBitmap* bm,
SkBitmap::Config pref, Mode mode, Format* format) {
SkASSERT(file);
SkASSERT(bm);
SkFILEStream stream(file);
if (stream.isValid()) {
if (SkImageDecoder::DecodeStream(&stream, bm, pref, mode, format)) {
bm->pixelRef()->setURI(file);
return true;
}
}
return false;
}
bool SkImageDecoder::DecodeMemory(const void* buffer, size_t size, SkBitmap* bm,
SkBitmap::Config pref, Mode mode, Format* format) {
if (0 == size) {
return false;
}
SkASSERT(buffer);
SkMemoryStream stream(buffer, size);
return SkImageDecoder::DecodeStream(&stream, bm, pref, mode, format);
}
bool SkImageDecoder::DecodeStream(SkStream* stream, SkBitmap* bm,
SkBitmap::Config pref, Mode mode, Format* format) {
SkASSERT(stream);
SkASSERT(bm);
bool success = false;
SkImageDecoder* codec = SkImageDecoder::Factory(stream);
if (NULL != codec) {
success = codec->decode(stream, bm, pref, mode);
if (success && format) {
*format = codec->getFormat();
}
delete codec;
}
return success;
}
|