blob: f9b04e39a7b3de3b247221a13426ef663c42c647 [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)
reedbe08ace2014-07-08 11:15:59 -070025 , fPreserveSrcDepth(false)
scroggo@google.com7e6fcee2013-05-03 20:14:28 +000026 , fDitherImage(true)
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);
reedbe08ace2014-07-08 11:15:59 -070050 other->setPreserveSrcDepth(fPreserveSrcDepth);
scroggo@google.com8d239242013-10-01 17:27:15 +000051 other->setDitherImage(fDitherImage);
52 other->setSkipWritingZeroes(fSkipWritingZeroes);
scroggo@google.com468142b2013-07-09 15:48:24 +000053 other->setPreferQualityOverSpeed(fPreferQualityOverSpeed);
54 other->setRequireUnpremultipliedColors(fRequireUnpremultipliedColors);
55}
56
reed@android.com8a1c16f2008-12-17 15:59:43 +000057SkImageDecoder::Format SkImageDecoder::getFormat() const {
58 return kUnknown_Format;
59}
60
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000061const char* SkImageDecoder::getFormatName() const {
scroggo@google.comf98118e2013-05-15 14:53:49 +000062 return GetFormatName(this->getFormat());
63}
64
65const char* SkImageDecoder::GetFormatName(Format format) {
66 switch (format) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +000067 case kUnknown_Format:
68 return "Unknown Format";
69 case kBMP_Format:
70 return "BMP";
71 case kGIF_Format:
72 return "GIF";
73 case kICO_Format:
74 return "ICO";
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000075 case kPKM_Format:
76 return "PKM";
krajcevski99ffe242014-06-03 13:04:35 -070077 case kKTX_Format:
78 return "KTX";
krajcevski95b1b3d2014-08-07 12:58:38 -070079 case kASTC_Format:
80 return "ASTC";
scroggo@google.com39edf4c2013-04-25 17:33:51 +000081 case kJPEG_Format:
82 return "JPEG";
83 case kPNG_Format:
84 return "PNG";
85 case kWBMP_Format:
86 return "WBMP";
87 case kWEBP_Format:
88 return "WEBP";
89 default:
mtklein@google.com330313a2013-08-22 15:37:26 +000090 SkDEBUGFAIL("Invalid format type!");
scroggo@google.com39edf4c2013-04-25 17:33:51 +000091 }
92 return "Unknown Format";
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000093}
94
reed@android.com8a1c16f2008-12-17 15:59:43 +000095SkImageDecoder::Peeker* SkImageDecoder::setPeeker(Peeker* peeker) {
96 SkRefCnt_SafeAssign(fPeeker, peeker);
97 return peeker;
98}
99
reed5926b862014-06-11 10:33:13 -0700100#ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101SkImageDecoder::Chooser* SkImageDecoder::setChooser(Chooser* chooser) {
102 SkRefCnt_SafeAssign(fChooser, chooser);
103 return chooser;
104}
reed5926b862014-06-11 10:33:13 -0700105#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106
107SkBitmap::Allocator* SkImageDecoder::setAllocator(SkBitmap::Allocator* alloc) {
108 SkRefCnt_SafeAssign(fAllocator, alloc);
109 return alloc;
110}
111
112void SkImageDecoder::setSampleSize(int size) {
113 if (size < 1) {
114 size = 1;
115 }
116 fSampleSize = size;
117}
118
reed5926b862014-06-11 10:33:13 -0700119#ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER
reed6c225732014-06-09 19:52:07 -0700120// TODO: change Chooser virtual to take colorType, so we can stop calling SkColorTypeToBitmapConfig
121//
122bool SkImageDecoder::chooseFromOneChoice(SkColorType colorType, int width, int height) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 Chooser* chooser = fChooser;
reed6c225732014-06-09 19:52:07 -0700124
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 if (NULL == chooser) { // no chooser, we just say YES to decoding :)
126 return true;
127 }
128 chooser->begin(1);
reed6c225732014-06-09 19:52:07 -0700129 chooser->inspect(0, SkColorTypeToBitmapConfig(colorType), width, height);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 return chooser->choose() == 0;
131}
reed5926b862014-06-11 10:33:13 -0700132#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133
134bool SkImageDecoder::allocPixelRef(SkBitmap* bitmap,
135 SkColorTable* ctable) const {
reed84825042014-09-02 12:50:45 -0700136 return bitmap->tryAllocPixels(fAllocator, ctable);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137}
138
139///////////////////////////////////////////////////////////////////////////////
reed@android.comb6137c32009-07-29 20:56:52 +0000140
reed6c225732014-06-09 19:52:07 -0700141SkColorType SkImageDecoder::getPrefColorType(SrcDepth srcDepth, bool srcHasAlpha) const {
reedbfefc7c2014-06-12 17:40:00 -0700142 SkColorType ct = fDefaultPref;
reedbe08ace2014-07-08 11:15:59 -0700143 if (fPreserveSrcDepth) {
144 switch (srcDepth) {
145 case kIndex_SrcDepth:
146 ct = kIndex_8_SkColorType;
147 break;
148 case k8BitGray_SrcDepth:
149 ct = kN32_SkColorType;
150 break;
151 case k32Bit_SrcDepth:
152 ct = kN32_SkColorType;
153 break;
154 }
155 }
reedbfefc7c2014-06-12 17:40:00 -0700156 return ct;
reed@android.com3f1f06a2010-03-03 21:04:12 +0000157}
158
reedbfefc7c2014-06-12 17:40:00 -0700159bool SkImageDecoder::decode(SkStream* stream, SkBitmap* bm, SkColorType pref, Mode mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 // we reset this to false before calling onDecode
161 fShouldCancelDecode = false;
reedbfefc7c2014-06-12 17:40:00 -0700162 // assign this, for use by getPrefColorType(), in case fUsePrefTable is false
reed@android.com3f1f06a2010-03-03 21:04:12 +0000163 fDefaultPref = pref;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000165 // pass a temporary bitmap, so that if we return false, we are assured of
166 // leaving the caller's bitmap untouched.
167 SkBitmap tmp;
reed@android.com3f1f06a2010-03-03 21:04:12 +0000168 if (!this->onDecode(stream, &tmp, mode)) {
reed@android.com62900b42009-02-11 15:07:19 +0000169 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 }
reed@android.com62900b42009-02-11 15:07:19 +0000171 bm->swap(tmp);
172 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173}
174
reedbfefc7c2014-06-12 17:40:00 -0700175bool SkImageDecoder::decodeSubset(SkBitmap* bm, const SkIRect& rect, SkColorType pref) {
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000176 // we reset this to false before calling onDecodeSubset
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000177 fShouldCancelDecode = false;
reedbfefc7c2014-06-12 17:40:00 -0700178 // assign this, for use by getPrefColorType(), in case fUsePrefTable is false
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000179 fDefaultPref = pref;
180
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000181 return this->onDecodeSubset(bm, rect);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000182}
183
reedbfefc7c2014-06-12 17:40:00 -0700184bool SkImageDecoder::buildTileIndex(SkStreamRewindable* stream, int *width, int *height) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000185 // we reset this to false before calling onBuildTileIndex
186 fShouldCancelDecode = false;
187
188 return this->onBuildTileIndex(stream, width, height);
189}
190
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000191bool SkImageDecoder::cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize,
192 int dstX, int dstY, int width, int height,
193 int srcX, int srcY) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000194 int w = width / sampleSize;
195 int h = height / sampleSize;
reed0689d7b2014-06-14 05:30:20 -0700196 if (src->colorType() == kIndex_8_SkColorType) {
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000197 // kIndex8 does not allow drawing via an SkCanvas, as is done below.
198 // Instead, use extractSubset. Note that this shares the SkPixelRef and
199 // SkColorTable.
200 // FIXME: Since src is discarded in practice, this holds on to more
201 // pixels than is strictly necessary. Switch to a copy if memory
202 // savings are more important than speed here. This also means
203 // that the pixels in dst can not be reused (though there is no
204 // allocation, which was already done on src).
205 int x = (dstX - srcX) / sampleSize;
206 int y = (dstY - srcY) / sampleSize;
207 SkIRect subset = SkIRect::MakeXYWH(x, y, w, h);
208 return src->extractSubset(dst, subset);
209 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000210 // if the destination has no pixels then we must allocate them.
211 if (dst->isNull()) {
reed6c225732014-06-09 19:52:07 -0700212 dst->setInfo(src->info().makeWH(w, h));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000213
214 if (!this->allocPixelRef(dst, NULL)) {
215 SkDEBUGF(("failed to allocate pixels needed to crop the bitmap"));
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000216 return false;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000217 }
218 }
219 // check to see if the destination is large enough to decode the desired
220 // region. If this assert fails we will just draw as much of the source
221 // into the destination that we can.
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000222 if (dst->width() < w || dst->height() < h) {
223 SkDEBUGF(("SkImageDecoder::cropBitmap does not have a large enough bitmap.\n"));
224 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000225
226 // Set the Src_Mode for the paint to prevent transparency issue in the
227 // dest in the event that the dest was being re-used.
228 SkPaint paint;
229 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
230
231 SkCanvas canvas(*dst);
232 canvas.drawSprite(*src, (srcX - dstX) / sampleSize,
233 (srcY - dstY) / sampleSize,
234 &paint);
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000235 return true;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000236}
237
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238///////////////////////////////////////////////////////////////////////////////
239
reedbfefc7c2014-06-12 17:40:00 -0700240bool SkImageDecoder::DecodeFile(const char file[], SkBitmap* bm, SkColorType pref, Mode mode,
241 Format* format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242 SkASSERT(file);
243 SkASSERT(bm);
244
scroggo@google.comb5571b32013-09-25 21:34:24 +0000245 SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(file));
mike@reedtribe.orgf3811622013-03-19 02:18:33 +0000246 if (stream.get()) {
247 if (SkImageDecoder::DecodeStream(stream, bm, pref, mode, format)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248 bm->pixelRef()->setURI(file);
tomhudson@google.com1a366212012-01-03 14:42:08 +0000249 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251 }
252 return false;
253}
254
reedbfefc7c2014-06-12 17:40:00 -0700255bool SkImageDecoder::DecodeMemory(const void* buffer, size_t size, SkBitmap* bm, SkColorType pref,
256 Mode mode, Format* format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000257 if (0 == size) {
258 return false;
259 }
260 SkASSERT(buffer);
261
262 SkMemoryStream stream(buffer, size);
reed@android.comb3ade9d2009-06-15 13:04:45 +0000263 return SkImageDecoder::DecodeStream(&stream, bm, pref, mode, format);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000264}
265
reedbfefc7c2014-06-12 17:40:00 -0700266bool SkImageDecoder::DecodeStream(SkStreamRewindable* stream, SkBitmap* bm, SkColorType pref,
267 Mode mode, Format* format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268 SkASSERT(stream);
269 SkASSERT(bm);
270
271 bool success = false;
272 SkImageDecoder* codec = SkImageDecoder::Factory(stream);
273
bsalomon49f085d2014-09-05 13:34:00 -0700274 if (codec) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275 success = codec->decode(stream, bm, pref, mode);
reed@android.comb3ade9d2009-06-15 13:04:45 +0000276 if (success && format) {
277 *format = codec->getFormat();
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000278 if (kUnknown_Format == *format) {
279 if (stream->rewind()) {
280 *format = GetStreamFormat(stream);
281 }
282 }
reed@android.comb3ade9d2009-06-15 13:04:45 +0000283 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284 delete codec;
285 }
286 return success;
287}
sugoib227e372014-10-16 13:10:57 -0700288
289bool SkImageDecoder::decodeYUV8Planes(SkStream* stream, SkISize componentSizes[3], void* planes[3],
290 size_t rowBytes[3], SkYUVColorSpace* colorSpace) {
291 // we reset this to false before calling onDecodeYUV8Planes
292 fShouldCancelDecode = false;
293
294 return this->onDecodeYUV8Planes(stream, componentSizes, planes, rowBytes, colorSpace);
295}