blob: 3c6ff69982bf903f470079240c567b91a464e340 [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
reed@android.com8a1c16f2008-12-17 15:59:43 +000017SkImageDecoder::SkImageDecoder()
scroggo@google.com7e6fcee2013-05-03 20:14:28 +000018 : fPeeker(NULL)
reed5926b862014-06-11 10:33:13 -070019#ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER
scroggo@google.com7e6fcee2013-05-03 20:14:28 +000020 , fChooser(NULL)
reed5926b862014-06-11 10:33:13 -070021#endif
scroggo@google.com7e6fcee2013-05-03 20:14:28 +000022 , fAllocator(NULL)
23 , fSampleSize(1)
reedbfefc7c2014-06-12 17:40:00 -070024 , fDefaultPref(kUnknown_SkColorType)
scroggo@google.com7e6fcee2013-05-03 20:14:28 +000025 , fDitherImage(true)
26 , fUsePrefTable(false)
scroggo@google.com8d239242013-10-01 17:27:15 +000027 , fSkipWritingZeroes(false)
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000028 , fPreferQualityOverSpeed(false)
29 , fRequireUnpremultipliedColors(false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000030}
31
32SkImageDecoder::~SkImageDecoder() {
reed@google.com82065d62011-02-07 15:30:46 +000033 SkSafeUnref(fPeeker);
reed5926b862014-06-11 10:33:13 -070034#ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER
reed@google.com82065d62011-02-07 15:30:46 +000035 SkSafeUnref(fChooser);
reed5926b862014-06-11 10:33:13 -070036#endif
reed@google.com82065d62011-02-07 15:30:46 +000037 SkSafeUnref(fAllocator);
reed@android.com8a1c16f2008-12-17 15:59:43 +000038}
39
scroggo@google.com468142b2013-07-09 15:48:24 +000040void SkImageDecoder::copyFieldsToOther(SkImageDecoder* other) {
41 if (NULL == other) {
42 return;
43 }
44 other->setPeeker(fPeeker);
reed5926b862014-06-11 10:33:13 -070045#ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER
scroggo@google.com468142b2013-07-09 15:48:24 +000046 other->setChooser(fChooser);
reed5926b862014-06-11 10:33:13 -070047#endif
scroggo@google.com468142b2013-07-09 15:48:24 +000048 other->setAllocator(fAllocator);
49 other->setSampleSize(fSampleSize);
50 if (fUsePrefTable) {
51 other->setPrefConfigTable(fPrefTable);
52 } else {
53 other->fDefaultPref = fDefaultPref;
54 }
scroggo@google.com8d239242013-10-01 17:27:15 +000055 other->setDitherImage(fDitherImage);
56 other->setSkipWritingZeroes(fSkipWritingZeroes);
scroggo@google.com468142b2013-07-09 15:48:24 +000057 other->setPreferQualityOverSpeed(fPreferQualityOverSpeed);
58 other->setRequireUnpremultipliedColors(fRequireUnpremultipliedColors);
59}
60
reed@android.com8a1c16f2008-12-17 15:59:43 +000061SkImageDecoder::Format SkImageDecoder::getFormat() const {
62 return kUnknown_Format;
63}
64
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000065const char* SkImageDecoder::getFormatName() const {
scroggo@google.comf98118e2013-05-15 14:53:49 +000066 return GetFormatName(this->getFormat());
67}
68
69const char* SkImageDecoder::GetFormatName(Format format) {
70 switch (format) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +000071 case kUnknown_Format:
72 return "Unknown Format";
73 case kBMP_Format:
74 return "BMP";
75 case kGIF_Format:
76 return "GIF";
77 case kICO_Format:
78 return "ICO";
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000079 case kPKM_Format:
80 return "PKM";
krajcevski99ffe242014-06-03 13:04:35 -070081 case kKTX_Format:
82 return "KTX";
scroggo@google.com39edf4c2013-04-25 17:33:51 +000083 case kJPEG_Format:
84 return "JPEG";
85 case kPNG_Format:
86 return "PNG";
87 case kWBMP_Format:
88 return "WBMP";
89 case kWEBP_Format:
90 return "WEBP";
91 default:
mtklein@google.com330313a2013-08-22 15:37:26 +000092 SkDEBUGFAIL("Invalid format type!");
scroggo@google.com39edf4c2013-04-25 17:33:51 +000093 }
94 return "Unknown Format";
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000095}
96
reed@android.com8a1c16f2008-12-17 15:59:43 +000097SkImageDecoder::Peeker* SkImageDecoder::setPeeker(Peeker* peeker) {
98 SkRefCnt_SafeAssign(fPeeker, peeker);
99 return peeker;
100}
101
reed5926b862014-06-11 10:33:13 -0700102#ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103SkImageDecoder::Chooser* SkImageDecoder::setChooser(Chooser* chooser) {
104 SkRefCnt_SafeAssign(fChooser, chooser);
105 return chooser;
106}
reed5926b862014-06-11 10:33:13 -0700107#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108
109SkBitmap::Allocator* SkImageDecoder::setAllocator(SkBitmap::Allocator* alloc) {
110 SkRefCnt_SafeAssign(fAllocator, alloc);
111 return alloc;
112}
113
114void SkImageDecoder::setSampleSize(int size) {
115 if (size < 1) {
116 size = 1;
117 }
118 fSampleSize = size;
119}
120
reed5926b862014-06-11 10:33:13 -0700121#ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER
reed6c225732014-06-09 19:52:07 -0700122// TODO: change Chooser virtual to take colorType, so we can stop calling SkColorTypeToBitmapConfig
123//
124bool SkImageDecoder::chooseFromOneChoice(SkColorType colorType, int width, int height) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 Chooser* chooser = fChooser;
reed6c225732014-06-09 19:52:07 -0700126
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 if (NULL == chooser) { // no chooser, we just say YES to decoding :)
128 return true;
129 }
130 chooser->begin(1);
reed6c225732014-06-09 19:52:07 -0700131 chooser->inspect(0, SkColorTypeToBitmapConfig(colorType), width, height);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 return chooser->choose() == 0;
133}
reed5926b862014-06-11 10:33:13 -0700134#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135
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
scroggo@google.comf698c822013-07-18 19:34:49 +0000143void SkImageDecoder::setPrefConfigTable(const PrefConfigTable& prefTable) {
144 fUsePrefTable = true;
145 fPrefTable = prefTable;
146}
147
reed2f785a22014-06-12 09:21:31 -0700148// TODO: use colortype in fPrefTable, fDefaultPref so we can stop using SkBitmapConfigToColorType()
reed6c225732014-06-09 19:52:07 -0700149//
150SkColorType SkImageDecoder::getPrefColorType(SrcDepth srcDepth, bool srcHasAlpha) const {
reedbfefc7c2014-06-12 17:40:00 -0700151 SkColorType ct = fDefaultPref;
reed@android.com3f1f06a2010-03-03 21:04:12 +0000152
153 if (fUsePrefTable) {
reedbfefc7c2014-06-12 17:40:00 -0700154 // Until we kill or change the PrefTable, we have to go into Config land for a moment.
155 SkBitmap::Config config = SkBitmap::kNo_Config;
reed@android.com3f1f06a2010-03-03 21:04:12 +0000156 switch (srcDepth) {
157 case kIndex_SrcDepth:
scroggo@google.comf698c822013-07-18 19:34:49 +0000158 config = srcHasAlpha ? fPrefTable.fPrefFor_8Index_YesAlpha_src
159 : fPrefTable.fPrefFor_8Index_NoAlpha_src;
reed@android.com3f1f06a2010-03-03 21:04:12 +0000160 break;
scroggo@google.comf698c822013-07-18 19:34:49 +0000161 case k8BitGray_SrcDepth:
162 config = fPrefTable.fPrefFor_8Gray_src;
reed@android.com3f1f06a2010-03-03 21:04:12 +0000163 break;
164 case k32Bit_SrcDepth:
scroggo@google.comf698c822013-07-18 19:34:49 +0000165 config = srcHasAlpha ? fPrefTable.fPrefFor_8bpc_YesAlpha_src
166 : fPrefTable.fPrefFor_8bpc_NoAlpha_src;
reed@android.com3f1f06a2010-03-03 21:04:12 +0000167 break;
168 }
reedbfefc7c2014-06-12 17:40:00 -0700169 // now return to SkColorType land
170 ct = SkBitmapConfigToColorType(config);
reed@android.com3f1f06a2010-03-03 21:04:12 +0000171 }
reedbfefc7c2014-06-12 17:40:00 -0700172 return ct;
reed@android.com3f1f06a2010-03-03 21:04:12 +0000173}
174
reedbfefc7c2014-06-12 17:40:00 -0700175bool SkImageDecoder::decode(SkStream* stream, SkBitmap* bm, SkColorType pref, Mode mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 // we reset this to false before calling onDecode
177 fShouldCancelDecode = false;
reedbfefc7c2014-06-12 17:40:00 -0700178 // assign this, for use by getPrefColorType(), in case fUsePrefTable is false
reed@android.com3f1f06a2010-03-03 21:04:12 +0000179 fDefaultPref = pref;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000181 // pass a temporary bitmap, so that if we return false, we are assured of
182 // leaving the caller's bitmap untouched.
183 SkBitmap tmp;
reed@android.com3f1f06a2010-03-03 21:04:12 +0000184 if (!this->onDecode(stream, &tmp, mode)) {
reed@android.com62900b42009-02-11 15:07:19 +0000185 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 }
reed@android.com62900b42009-02-11 15:07:19 +0000187 bm->swap(tmp);
188 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189}
190
reedbfefc7c2014-06-12 17:40:00 -0700191bool SkImageDecoder::decodeSubset(SkBitmap* bm, const SkIRect& rect, SkColorType pref) {
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000192 // we reset this to false before calling onDecodeSubset
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000193 fShouldCancelDecode = false;
reedbfefc7c2014-06-12 17:40:00 -0700194 // assign this, for use by getPrefColorType(), in case fUsePrefTable is false
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000195 fDefaultPref = pref;
196
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000197 return this->onDecodeSubset(bm, rect);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000198}
199
reedbfefc7c2014-06-12 17:40:00 -0700200bool SkImageDecoder::buildTileIndex(SkStreamRewindable* stream, int *width, int *height) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000201 // we reset this to false before calling onBuildTileIndex
202 fShouldCancelDecode = false;
203
204 return this->onBuildTileIndex(stream, width, height);
205}
206
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000207bool SkImageDecoder::cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize,
208 int dstX, int dstY, int width, int height,
209 int srcX, int srcY) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000210 int w = width / sampleSize;
211 int h = height / sampleSize;
reed@google.com44699382013-10-31 17:28:30 +0000212 if (src->config() == SkBitmap::kIndex8_Config) {
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000213 // kIndex8 does not allow drawing via an SkCanvas, as is done below.
214 // Instead, use extractSubset. Note that this shares the SkPixelRef and
215 // SkColorTable.
216 // FIXME: Since src is discarded in practice, this holds on to more
217 // pixels than is strictly necessary. Switch to a copy if memory
218 // savings are more important than speed here. This also means
219 // that the pixels in dst can not be reused (though there is no
220 // allocation, which was already done on src).
221 int x = (dstX - srcX) / sampleSize;
222 int y = (dstY - srcY) / sampleSize;
223 SkIRect subset = SkIRect::MakeXYWH(x, y, w, h);
224 return src->extractSubset(dst, subset);
225 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000226 // if the destination has no pixels then we must allocate them.
227 if (dst->isNull()) {
reed6c225732014-06-09 19:52:07 -0700228 dst->setInfo(src->info().makeWH(w, h));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000229
230 if (!this->allocPixelRef(dst, NULL)) {
231 SkDEBUGF(("failed to allocate pixels needed to crop the bitmap"));
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000232 return false;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000233 }
234 }
235 // check to see if the destination is large enough to decode the desired
236 // region. If this assert fails we will just draw as much of the source
237 // into the destination that we can.
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000238 if (dst->width() < w || dst->height() < h) {
239 SkDEBUGF(("SkImageDecoder::cropBitmap does not have a large enough bitmap.\n"));
240 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000241
242 // Set the Src_Mode for the paint to prevent transparency issue in the
243 // dest in the event that the dest was being re-used.
244 SkPaint paint;
245 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
246
247 SkCanvas canvas(*dst);
248 canvas.drawSprite(*src, (srcX - dstX) / sampleSize,
249 (srcY - dstY) / sampleSize,
250 &paint);
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000251 return true;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000252}
253
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254///////////////////////////////////////////////////////////////////////////////
255
reedbfefc7c2014-06-12 17:40:00 -0700256bool SkImageDecoder::DecodeFile(const char file[], SkBitmap* bm, SkColorType pref, Mode mode,
257 Format* format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000258 SkASSERT(file);
259 SkASSERT(bm);
260
scroggo@google.comb5571b32013-09-25 21:34:24 +0000261 SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(file));
mike@reedtribe.orgf3811622013-03-19 02:18:33 +0000262 if (stream.get()) {
263 if (SkImageDecoder::DecodeStream(stream, bm, pref, mode, format)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000264 bm->pixelRef()->setURI(file);
tomhudson@google.com1a366212012-01-03 14:42:08 +0000265 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000267 }
268 return false;
269}
270
reedbfefc7c2014-06-12 17:40:00 -0700271bool SkImageDecoder::DecodeMemory(const void* buffer, size_t size, SkBitmap* bm, SkColorType pref,
272 Mode mode, Format* format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273 if (0 == size) {
274 return false;
275 }
276 SkASSERT(buffer);
277
278 SkMemoryStream stream(buffer, size);
reed@android.comb3ade9d2009-06-15 13:04:45 +0000279 return SkImageDecoder::DecodeStream(&stream, bm, pref, mode, format);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280}
281
reedbfefc7c2014-06-12 17:40:00 -0700282bool SkImageDecoder::DecodeStream(SkStreamRewindable* stream, SkBitmap* bm, SkColorType pref,
283 Mode mode, Format* format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284 SkASSERT(stream);
285 SkASSERT(bm);
286
287 bool success = false;
288 SkImageDecoder* codec = SkImageDecoder::Factory(stream);
289
290 if (NULL != codec) {
291 success = codec->decode(stream, bm, pref, mode);
reed@android.comb3ade9d2009-06-15 13:04:45 +0000292 if (success && format) {
293 *format = codec->getFormat();
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000294 if (kUnknown_Format == *format) {
295 if (stream->rewind()) {
296 *format = GetStreamFormat(stream);
297 }
298 }
reed@android.comb3ade9d2009-06-15 13:04:45 +0000299 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000300 delete codec;
301 }
302 return success;
303}