blob: 5d94bb1c2db97efd618320213210ef91ee363329 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
reed@android.com8a1c16f2008-12-17 15:59:43 +00008
9#include "SkImageDecoder.h"
10#include "SkBitmap.h"
scroggo@google.comf8d7d272013-02-22 21:38:35 +000011#include "SkImagePriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkPixelRef.h"
13#include "SkStream.h"
14#include "SkTemplates.h"
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000015#include "SkCanvas.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000017SK_DEFINE_INST_COUNT(SkImageDecoder::Peeker)
18SK_DEFINE_INST_COUNT(SkImageDecoder::Chooser)
19SK_DEFINE_INST_COUNT(SkImageDecoderFactory)
20
reed@android.com8a1c16f2008-12-17 15:59:43 +000021static SkBitmap::Config gDeviceConfig = SkBitmap::kNo_Config;
22
23SkBitmap::Config SkImageDecoder::GetDeviceConfig()
24{
25 return gDeviceConfig;
26}
27
28void SkImageDecoder::SetDeviceConfig(SkBitmap::Config config)
29{
30 gDeviceConfig = config;
31}
32
33///////////////////////////////////////////////////////////////////////////////
34
35SkImageDecoder::SkImageDecoder()
scroggo@google.com7e6fcee2013-05-03 20:14:28 +000036 : fPeeker(NULL)
37 , fChooser(NULL)
38 , fAllocator(NULL)
39 , fSampleSize(1)
40 , fDefaultPref(SkBitmap::kNo_Config)
41 , fDitherImage(true)
42 , fUsePrefTable(false)
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000043 , fPreferQualityOverSpeed(false)
44 , fRequireUnpremultipliedColors(false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000045}
46
47SkImageDecoder::~SkImageDecoder() {
reed@google.com82065d62011-02-07 15:30:46 +000048 SkSafeUnref(fPeeker);
49 SkSafeUnref(fChooser);
50 SkSafeUnref(fAllocator);
reed@android.com8a1c16f2008-12-17 15:59:43 +000051}
52
scroggo@google.com468142b2013-07-09 15:48:24 +000053void SkImageDecoder::copyFieldsToOther(SkImageDecoder* other) {
54 if (NULL == other) {
55 return;
56 }
57 other->setPeeker(fPeeker);
58 other->setChooser(fChooser);
59 other->setAllocator(fAllocator);
60 other->setSampleSize(fSampleSize);
61 if (fUsePrefTable) {
62 other->setPrefConfigTable(fPrefTable);
63 } else {
64 other->fDefaultPref = fDefaultPref;
65 }
66 other->setPreferQualityOverSpeed(fPreferQualityOverSpeed);
67 other->setRequireUnpremultipliedColors(fRequireUnpremultipliedColors);
68}
69
reed@android.com8a1c16f2008-12-17 15:59:43 +000070SkImageDecoder::Format SkImageDecoder::getFormat() const {
71 return kUnknown_Format;
72}
73
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000074const char* SkImageDecoder::getFormatName() const {
scroggo@google.comf98118e2013-05-15 14:53:49 +000075 return GetFormatName(this->getFormat());
76}
77
78const char* SkImageDecoder::GetFormatName(Format format) {
79 switch (format) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +000080 case kUnknown_Format:
81 return "Unknown Format";
82 case kBMP_Format:
83 return "BMP";
84 case kGIF_Format:
85 return "GIF";
86 case kICO_Format:
87 return "ICO";
88 case kJPEG_Format:
89 return "JPEG";
90 case kPNG_Format:
91 return "PNG";
92 case kWBMP_Format:
93 return "WBMP";
94 case kWEBP_Format:
95 return "WEBP";
96 default:
97 SkASSERT(!"Invalid format type!");
98 }
99 return "Unknown Format";
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000100}
101
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102SkImageDecoder::Peeker* SkImageDecoder::setPeeker(Peeker* peeker) {
103 SkRefCnt_SafeAssign(fPeeker, peeker);
104 return peeker;
105}
106
107SkImageDecoder::Chooser* SkImageDecoder::setChooser(Chooser* chooser) {
108 SkRefCnt_SafeAssign(fChooser, chooser);
109 return chooser;
110}
111
112SkBitmap::Allocator* SkImageDecoder::setAllocator(SkBitmap::Allocator* alloc) {
113 SkRefCnt_SafeAssign(fAllocator, alloc);
114 return alloc;
115}
116
117void SkImageDecoder::setSampleSize(int size) {
118 if (size < 1) {
119 size = 1;
120 }
121 fSampleSize = size;
122}
123
124bool SkImageDecoder::chooseFromOneChoice(SkBitmap::Config config, int width,
125 int height) const {
126 Chooser* chooser = fChooser;
127
128 if (NULL == chooser) { // no chooser, we just say YES to decoding :)
129 return true;
130 }
131 chooser->begin(1);
132 chooser->inspect(0, config, width, height);
133 return chooser->choose() == 0;
134}
135
136bool SkImageDecoder::allocPixelRef(SkBitmap* bitmap,
137 SkColorTable* ctable) const {
138 return bitmap->allocPixels(fAllocator, ctable);
139}
140
141///////////////////////////////////////////////////////////////////////////////
reed@android.comb6137c32009-07-29 20:56:52 +0000142
reed@android.com3f1f06a2010-03-03 21:04:12 +0000143void SkImageDecoder::setPrefConfigTable(const SkBitmap::Config pref[6]) {
144 if (NULL == pref) {
145 fUsePrefTable = false;
146 } else {
147 fUsePrefTable = true;
148 memcpy(fPrefTable, pref, sizeof(fPrefTable));
149 }
150}
151
152SkBitmap::Config SkImageDecoder::getPrefConfig(SrcDepth srcDepth,
153 bool srcHasAlpha) const {
154 SkBitmap::Config config;
155
156 if (fUsePrefTable) {
157 int index = 0;
158 switch (srcDepth) {
159 case kIndex_SrcDepth:
160 index = 0;
161 break;
162 case k16Bit_SrcDepth:
163 index = 2;
164 break;
165 case k32Bit_SrcDepth:
166 index = 4;
167 break;
168 }
169 if (srcHasAlpha) {
170 index += 1;
171 }
172 config = fPrefTable[index];
173 } else {
174 config = fDefaultPref;
175 }
176
177 if (SkBitmap::kNo_Config == config) {
178 config = SkImageDecoder::GetDeviceConfig();
179 }
180 return config;
181}
182
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183bool SkImageDecoder::decode(SkStream* stream, SkBitmap* bm,
scroggo@google.combc69ce92013-07-09 15:45:14 +0000184 SkBitmap::Config pref, Mode mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185 // we reset this to false before calling onDecode
186 fShouldCancelDecode = false;
reed@android.com3f1f06a2010-03-03 21:04:12 +0000187 // assign this, for use by getPrefConfig(), in case fUsePrefTable is false
188 fDefaultPref = pref;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000190 // pass a temporary bitmap, so that if we return false, we are assured of
191 // leaving the caller's bitmap untouched.
192 SkBitmap tmp;
reed@android.com3f1f06a2010-03-03 21:04:12 +0000193 if (!this->onDecode(stream, &tmp, mode)) {
reed@android.com62900b42009-02-11 15:07:19 +0000194 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 }
reed@android.com62900b42009-02-11 15:07:19 +0000196 bm->swap(tmp);
197 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198}
199
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000200bool SkImageDecoder::decodeSubset(SkBitmap* bm, const SkIRect& rect,
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000201 SkBitmap::Config pref) {
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000202 // we reset this to false before calling onDecodeSubset
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000203 fShouldCancelDecode = false;
204 // assign this, for use by getPrefConfig(), in case fUsePrefTable is false
205 fDefaultPref = pref;
206
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000207 return this->onDecodeSubset(bm, rect);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000208}
209
210bool SkImageDecoder::buildTileIndex(SkStream* stream,
211 int *width, int *height) {
212 // we reset this to false before calling onBuildTileIndex
213 fShouldCancelDecode = false;
214
215 return this->onBuildTileIndex(stream, width, height);
216}
217
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000218bool SkImageDecoder::cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize,
219 int dstX, int dstY, int width, int height,
220 int srcX, int srcY) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000221 int w = width / sampleSize;
222 int h = height / sampleSize;
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000223 if (src->getConfig() == SkBitmap::kIndex8_Config) {
224 // kIndex8 does not allow drawing via an SkCanvas, as is done below.
225 // Instead, use extractSubset. Note that this shares the SkPixelRef and
226 // SkColorTable.
227 // FIXME: Since src is discarded in practice, this holds on to more
228 // pixels than is strictly necessary. Switch to a copy if memory
229 // savings are more important than speed here. This also means
230 // that the pixels in dst can not be reused (though there is no
231 // allocation, which was already done on src).
232 int x = (dstX - srcX) / sampleSize;
233 int y = (dstY - srcY) / sampleSize;
234 SkIRect subset = SkIRect::MakeXYWH(x, y, w, h);
235 return src->extractSubset(dst, subset);
236 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000237 // if the destination has no pixels then we must allocate them.
238 if (dst->isNull()) {
239 dst->setConfig(src->getConfig(), w, h);
240 dst->setIsOpaque(src->isOpaque());
241
242 if (!this->allocPixelRef(dst, NULL)) {
243 SkDEBUGF(("failed to allocate pixels needed to crop the bitmap"));
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000244 return false;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000245 }
246 }
247 // check to see if the destination is large enough to decode the desired
248 // region. If this assert fails we will just draw as much of the source
249 // into the destination that we can.
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000250 if (dst->width() < w || dst->height() < h) {
251 SkDEBUGF(("SkImageDecoder::cropBitmap does not have a large enough bitmap.\n"));
252 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000253
254 // Set the Src_Mode for the paint to prevent transparency issue in the
255 // dest in the event that the dest was being re-used.
256 SkPaint paint;
257 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
258
259 SkCanvas canvas(*dst);
260 canvas.drawSprite(*src, (srcX - dstX) / sampleSize,
261 (srcY - dstY) / sampleSize,
262 &paint);
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000263 return true;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000264}
265
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266///////////////////////////////////////////////////////////////////////////////
267
268bool SkImageDecoder::DecodeFile(const char file[], SkBitmap* bm,
reed@android.comb3ade9d2009-06-15 13:04:45 +0000269 SkBitmap::Config pref, Mode mode, Format* format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000270 SkASSERT(file);
271 SkASSERT(bm);
272
mike@reedtribe.orgf3811622013-03-19 02:18:33 +0000273 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(file));
274 if (stream.get()) {
275 if (SkImageDecoder::DecodeStream(stream, bm, pref, mode, format)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000276 bm->pixelRef()->setURI(file);
tomhudson@google.com1a366212012-01-03 14:42:08 +0000277 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000278 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 }
280 return false;
281}
282
283bool SkImageDecoder::DecodeMemory(const void* buffer, size_t size, SkBitmap* bm,
reed@android.comb3ade9d2009-06-15 13:04:45 +0000284 SkBitmap::Config pref, Mode mode, Format* format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 if (0 == size) {
286 return false;
287 }
288 SkASSERT(buffer);
289
290 SkMemoryStream stream(buffer, size);
reed@android.comb3ade9d2009-06-15 13:04:45 +0000291 return SkImageDecoder::DecodeStream(&stream, bm, pref, mode, format);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292}
293
scroggo@google.com67882dd2013-05-20 16:58:16 +0000294/**
295 * Special allocator used by DecodeMemoryToTarget. Uses preallocated memory
296 * provided if the bm is 8888. Otherwise, uses a heap allocator. The same
297 * allocator will be used again for a copy to 8888, when the preallocated
298 * memory will be used.
299 */
300class TargetAllocator : public SkBitmap::HeapAllocator {
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000301
302public:
303 TargetAllocator(void* target)
304 : fTarget(target) {}
305
306 virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) SK_OVERRIDE {
scroggo@google.com67882dd2013-05-20 16:58:16 +0000307 // If the config is not 8888, allocate a pixelref using the heap.
308 // fTarget will be used to store the final pixels when copied to
309 // 8888.
310 if (bm->config() != SkBitmap::kARGB_8888_Config) {
311 return INHERITED::allocPixelRef(bm, ct);
312 }
313 // In kARGB_8888_Config, there is no colortable.
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000314 SkASSERT(NULL == ct);
315 bm->setPixels(fTarget);
316 return true;
317 }
318
319private:
320 void* fTarget;
scroggo@google.com67882dd2013-05-20 16:58:16 +0000321 typedef SkBitmap::HeapAllocator INHERITED;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000322};
323
scroggo@google.com67882dd2013-05-20 16:58:16 +0000324/**
325 * Helper function for DecodeMemoryToTarget. DecodeMemoryToTarget wants
326 * 8888, so set the config to it. All parameters must not be null.
327 * @param decoder Decoder appropriate for this stream.
328 * @param stream Rewound stream to the encoded data.
329 * @param bitmap On success, will have its bounds set to the bounds of the
330 * encoded data, and its config set to 8888.
331 * @return True if the bounds were decoded and the bitmap is 8888 or can be
332 * copied to 8888.
333 */
334static bool decode_bounds_to_8888(SkImageDecoder* decoder, SkStream* stream,
335 SkBitmap* bitmap) {
336 SkASSERT(decoder != NULL);
337 SkASSERT(stream != NULL);
338 SkASSERT(bitmap != NULL);
339
340 if (!decoder->decode(stream, bitmap, SkImageDecoder::kDecodeBounds_Mode)) {
341 return false;
342 }
343
344 if (bitmap->config() == SkBitmap::kARGB_8888_Config) {
345 return true;
346 }
347
348 if (!bitmap->canCopyTo(SkBitmap::kARGB_8888_Config)) {
349 return false;
350 }
351
352 bitmap->setConfig(SkBitmap::kARGB_8888_Config, bitmap->width(), bitmap->height());
353 return true;
354}
355
356/**
357 * Helper function for DecodeMemoryToTarget. Decodes the stream into bitmap, and if
358 * the bitmap is not 8888, then it is copied to 8888. Either way, the end result has
359 * its pixels stored in target. All parameters must not be null.
360 * @param decoder Decoder appropriate for this stream.
361 * @param stream Rewound stream to the encoded data.
362 * @param bitmap On success, will contain the decoded image, with its pixels stored
363 * at target.
364 * @param target Preallocated memory for storing pixels.
365 * @return bool Whether the decode (and copy, if necessary) succeeded.
366 */
367static bool decode_pixels_to_8888(SkImageDecoder* decoder, SkStream* stream,
368 SkBitmap* bitmap, void* target) {
369 SkASSERT(decoder != NULL);
370 SkASSERT(stream != NULL);
371 SkASSERT(bitmap != NULL);
372 SkASSERT(target != NULL);
373
374 TargetAllocator allocator(target);
375 decoder->setAllocator(&allocator);
376
377 bool success = decoder->decode(stream, bitmap, SkImageDecoder::kDecodePixels_Mode);
378 decoder->setAllocator(NULL);
379
380 if (!success) {
381 return false;
382 }
383
384 if (bitmap->config() == SkBitmap::kARGB_8888_Config) {
385 return true;
386 }
387
388 SkBitmap bm8888;
389 if (!bitmap->copyTo(&bm8888, SkBitmap::kARGB_8888_Config, &allocator)) {
390 return false;
391 }
392
393 bitmap->swap(bm8888);
394 return true;
395}
396
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000397bool SkImageDecoder::DecodeMemoryToTarget(const void* buffer, size_t size,
398 SkImage::Info* info,
399 const SkBitmapFactory::Target* target) {
400 if (NULL == info) {
401 return false;
402 }
scroggo@google.com67882dd2013-05-20 16:58:16 +0000403
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000404 // FIXME: Just to get this working, implement in terms of existing
405 // ImageDecoder calls.
406 SkBitmap bm;
407 SkMemoryStream stream(buffer, size);
408 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream));
scroggo@google.com67882dd2013-05-20 16:58:16 +0000409 if (NULL == decoder.get()) {
410 return false;
411 }
412
413 if (!decode_bounds_to_8888(decoder.get(), &stream, &bm)) {
414 return false;
415 }
416
417 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
418
419 // Now set info properly.
420 // Since Config is SkBitmap::kARGB_8888_Config, SkBitmapToImageInfo
421 // will always succeed.
422 SkAssertResult(SkBitmapToImageInfo(bm, info));
423
424 if (NULL == target) {
425 return true;
426 }
427
428 if (target->fRowBytes != SkToU32(bm.rowBytes())) {
429 if (target->fRowBytes < SkImageMinRowBytes(*info)) {
430 SkASSERT(!"Desired row bytes is too small");
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000431 return false;
432 }
scroggo@google.com67882dd2013-05-20 16:58:16 +0000433 bm.setConfig(bm.config(), bm.width(), bm.height(), target->fRowBytes);
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000434 }
scroggo@google.com67882dd2013-05-20 16:58:16 +0000435
436 // SkMemoryStream.rewind() will always return true.
437 SkAssertResult(stream.rewind());
438 return decode_pixels_to_8888(decoder.get(), &stream, &bm, target->fAddr);
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000439}
440
441
reed@android.com8a1c16f2008-12-17 15:59:43 +0000442bool SkImageDecoder::DecodeStream(SkStream* stream, SkBitmap* bm,
reed@android.comb3ade9d2009-06-15 13:04:45 +0000443 SkBitmap::Config pref, Mode mode, Format* format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444 SkASSERT(stream);
445 SkASSERT(bm);
446
447 bool success = false;
448 SkImageDecoder* codec = SkImageDecoder::Factory(stream);
449
450 if (NULL != codec) {
451 success = codec->decode(stream, bm, pref, mode);
reed@android.comb3ade9d2009-06-15 13:04:45 +0000452 if (success && format) {
453 *format = codec->getFormat();
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000454 if (kUnknown_Format == *format) {
455 if (stream->rewind()) {
456 *format = GetStreamFormat(stream);
457 }
458 }
reed@android.comb3ade9d2009-06-15 13:04:45 +0000459 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000460 delete codec;
461 }
462 return success;
463}