epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | |
| 9 | #include "SkImageDecoder.h" |
| 10 | #include "SkBitmap.h" |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 11 | #include "SkImagePriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkPixelRef.h" |
| 13 | #include "SkStream.h" |
| 14 | #include "SkTemplates.h" |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 15 | #include "SkCanvas.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | |
| 17 | static SkBitmap::Config gDeviceConfig = SkBitmap::kNo_Config; |
| 18 | |
| 19 | SkBitmap::Config SkImageDecoder::GetDeviceConfig() |
| 20 | { |
| 21 | return gDeviceConfig; |
| 22 | } |
| 23 | |
| 24 | void SkImageDecoder::SetDeviceConfig(SkBitmap::Config config) |
| 25 | { |
| 26 | gDeviceConfig = config; |
| 27 | } |
| 28 | |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | |
| 31 | SkImageDecoder::SkImageDecoder() |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 32 | : fPeeker(NULL) |
reed | 5926b86 | 2014-06-11 10:33:13 -0700 | [diff] [blame^] | 33 | #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 34 | , fChooser(NULL) |
reed | 5926b86 | 2014-06-11 10:33:13 -0700 | [diff] [blame^] | 35 | #endif |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 36 | , fAllocator(NULL) |
| 37 | , fSampleSize(1) |
| 38 | , fDefaultPref(SkBitmap::kNo_Config) |
| 39 | , fDitherImage(true) |
| 40 | , fUsePrefTable(false) |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 41 | , fSkipWritingZeroes(false) |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 42 | , fPreferQualityOverSpeed(false) |
| 43 | , fRequireUnpremultipliedColors(false) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | SkImageDecoder::~SkImageDecoder() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 47 | SkSafeUnref(fPeeker); |
reed | 5926b86 | 2014-06-11 10:33:13 -0700 | [diff] [blame^] | 48 | #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 49 | SkSafeUnref(fChooser); |
reed | 5926b86 | 2014-06-11 10:33:13 -0700 | [diff] [blame^] | 50 | #endif |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 51 | SkSafeUnref(fAllocator); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | } |
| 53 | |
scroggo@google.com | 468142b | 2013-07-09 15:48:24 +0000 | [diff] [blame] | 54 | void SkImageDecoder::copyFieldsToOther(SkImageDecoder* other) { |
| 55 | if (NULL == other) { |
| 56 | return; |
| 57 | } |
| 58 | other->setPeeker(fPeeker); |
reed | 5926b86 | 2014-06-11 10:33:13 -0700 | [diff] [blame^] | 59 | #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER |
scroggo@google.com | 468142b | 2013-07-09 15:48:24 +0000 | [diff] [blame] | 60 | other->setChooser(fChooser); |
reed | 5926b86 | 2014-06-11 10:33:13 -0700 | [diff] [blame^] | 61 | #endif |
scroggo@google.com | 468142b | 2013-07-09 15:48:24 +0000 | [diff] [blame] | 62 | other->setAllocator(fAllocator); |
| 63 | other->setSampleSize(fSampleSize); |
| 64 | if (fUsePrefTable) { |
| 65 | other->setPrefConfigTable(fPrefTable); |
| 66 | } else { |
| 67 | other->fDefaultPref = fDefaultPref; |
| 68 | } |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 69 | other->setDitherImage(fDitherImage); |
| 70 | other->setSkipWritingZeroes(fSkipWritingZeroes); |
scroggo@google.com | 468142b | 2013-07-09 15:48:24 +0000 | [diff] [blame] | 71 | other->setPreferQualityOverSpeed(fPreferQualityOverSpeed); |
| 72 | other->setRequireUnpremultipliedColors(fRequireUnpremultipliedColors); |
| 73 | } |
| 74 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | SkImageDecoder::Format SkImageDecoder::getFormat() const { |
| 76 | return kUnknown_Format; |
| 77 | } |
| 78 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 79 | const char* SkImageDecoder::getFormatName() const { |
scroggo@google.com | f98118e | 2013-05-15 14:53:49 +0000 | [diff] [blame] | 80 | return GetFormatName(this->getFormat()); |
| 81 | } |
| 82 | |
| 83 | const char* SkImageDecoder::GetFormatName(Format format) { |
| 84 | switch (format) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 85 | case kUnknown_Format: |
| 86 | return "Unknown Format"; |
| 87 | case kBMP_Format: |
| 88 | return "BMP"; |
| 89 | case kGIF_Format: |
| 90 | return "GIF"; |
| 91 | case kICO_Format: |
| 92 | return "ICO"; |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 93 | case kPKM_Format: |
| 94 | return "PKM"; |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 95 | case kKTX_Format: |
| 96 | return "KTX"; |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 97 | case kJPEG_Format: |
| 98 | return "JPEG"; |
| 99 | case kPNG_Format: |
| 100 | return "PNG"; |
| 101 | case kWBMP_Format: |
| 102 | return "WBMP"; |
| 103 | case kWEBP_Format: |
| 104 | return "WEBP"; |
| 105 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 106 | SkDEBUGFAIL("Invalid format type!"); |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 107 | } |
| 108 | return "Unknown Format"; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 109 | } |
| 110 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | SkImageDecoder::Peeker* SkImageDecoder::setPeeker(Peeker* peeker) { |
| 112 | SkRefCnt_SafeAssign(fPeeker, peeker); |
| 113 | return peeker; |
| 114 | } |
| 115 | |
reed | 5926b86 | 2014-06-11 10:33:13 -0700 | [diff] [blame^] | 116 | #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | SkImageDecoder::Chooser* SkImageDecoder::setChooser(Chooser* chooser) { |
| 118 | SkRefCnt_SafeAssign(fChooser, chooser); |
| 119 | return chooser; |
| 120 | } |
reed | 5926b86 | 2014-06-11 10:33:13 -0700 | [diff] [blame^] | 121 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | |
| 123 | SkBitmap::Allocator* SkImageDecoder::setAllocator(SkBitmap::Allocator* alloc) { |
| 124 | SkRefCnt_SafeAssign(fAllocator, alloc); |
| 125 | return alloc; |
| 126 | } |
| 127 | |
| 128 | void SkImageDecoder::setSampleSize(int size) { |
| 129 | if (size < 1) { |
| 130 | size = 1; |
| 131 | } |
| 132 | fSampleSize = size; |
| 133 | } |
| 134 | |
reed | 5926b86 | 2014-06-11 10:33:13 -0700 | [diff] [blame^] | 135 | #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 136 | // TODO: change Chooser virtual to take colorType, so we can stop calling SkColorTypeToBitmapConfig |
| 137 | // |
| 138 | bool SkImageDecoder::chooseFromOneChoice(SkColorType colorType, int width, int height) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | Chooser* chooser = fChooser; |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 140 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | if (NULL == chooser) { // no chooser, we just say YES to decoding :) |
| 142 | return true; |
| 143 | } |
| 144 | chooser->begin(1); |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 145 | chooser->inspect(0, SkColorTypeToBitmapConfig(colorType), width, height); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | return chooser->choose() == 0; |
| 147 | } |
reed | 5926b86 | 2014-06-11 10:33:13 -0700 | [diff] [blame^] | 148 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | |
| 150 | bool SkImageDecoder::allocPixelRef(SkBitmap* bitmap, |
| 151 | SkColorTable* ctable) const { |
| 152 | return bitmap->allocPixels(fAllocator, ctable); |
| 153 | } |
| 154 | |
| 155 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | b6137c3 | 2009-07-29 20:56:52 +0000 | [diff] [blame] | 156 | |
scroggo@google.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 157 | void SkImageDecoder::setPrefConfigTable(const PrefConfigTable& prefTable) { |
| 158 | fUsePrefTable = true; |
| 159 | fPrefTable = prefTable; |
| 160 | } |
| 161 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 162 | // TODO: use colortype in fPrefTable, fDefaultPref and GetDeviceConfig() |
| 163 | // so we can stop using SkBitmapConfigToColorType() |
| 164 | // |
| 165 | SkColorType SkImageDecoder::getPrefColorType(SrcDepth srcDepth, bool srcHasAlpha) const { |
scroggo@google.com | 12d0642 | 2013-07-18 19:42:35 +0000 | [diff] [blame] | 166 | SkBitmap::Config config = SkBitmap::kNo_Config; |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 167 | |
| 168 | if (fUsePrefTable) { |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 169 | switch (srcDepth) { |
| 170 | case kIndex_SrcDepth: |
scroggo@google.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 171 | config = srcHasAlpha ? fPrefTable.fPrefFor_8Index_YesAlpha_src |
| 172 | : fPrefTable.fPrefFor_8Index_NoAlpha_src; |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 173 | break; |
scroggo@google.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 174 | case k8BitGray_SrcDepth: |
| 175 | config = fPrefTable.fPrefFor_8Gray_src; |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 176 | break; |
| 177 | case k32Bit_SrcDepth: |
scroggo@google.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 178 | config = srcHasAlpha ? fPrefTable.fPrefFor_8bpc_YesAlpha_src |
| 179 | : fPrefTable.fPrefFor_8bpc_NoAlpha_src; |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 180 | break; |
| 181 | } |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 182 | } else { |
| 183 | config = fDefaultPref; |
| 184 | } |
| 185 | |
| 186 | if (SkBitmap::kNo_Config == config) { |
| 187 | config = SkImageDecoder::GetDeviceConfig(); |
| 188 | } |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 189 | return SkBitmapConfigToColorType(config); |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 190 | } |
| 191 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 192 | bool SkImageDecoder::decode(SkStream* stream, SkBitmap* bm, |
scroggo@google.com | bc69ce9 | 2013-07-09 15:45:14 +0000 | [diff] [blame] | 193 | SkBitmap::Config pref, Mode mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | // we reset this to false before calling onDecode |
| 195 | fShouldCancelDecode = false; |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 196 | // assign this, for use by getPrefConfig(), in case fUsePrefTable is false |
| 197 | fDefaultPref = pref; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 198 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 199 | // pass a temporary bitmap, so that if we return false, we are assured of |
| 200 | // leaving the caller's bitmap untouched. |
| 201 | SkBitmap tmp; |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 202 | if (!this->onDecode(stream, &tmp, mode)) { |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 203 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | } |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 205 | bm->swap(tmp); |
| 206 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | } |
| 208 | |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 209 | bool SkImageDecoder::decodeSubset(SkBitmap* bm, const SkIRect& rect, |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 210 | SkBitmap::Config pref) { |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 211 | // we reset this to false before calling onDecodeSubset |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 212 | fShouldCancelDecode = false; |
| 213 | // assign this, for use by getPrefConfig(), in case fUsePrefTable is false |
| 214 | fDefaultPref = pref; |
| 215 | |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 216 | return this->onDecodeSubset(bm, rect); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 217 | } |
| 218 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 219 | bool SkImageDecoder::buildTileIndex(SkStreamRewindable* stream, |
| 220 | int *width, int *height) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 221 | // we reset this to false before calling onBuildTileIndex |
| 222 | fShouldCancelDecode = false; |
| 223 | |
| 224 | return this->onBuildTileIndex(stream, width, height); |
| 225 | } |
| 226 | |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 227 | bool SkImageDecoder::cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize, |
| 228 | int dstX, int dstY, int width, int height, |
| 229 | int srcX, int srcY) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 230 | int w = width / sampleSize; |
| 231 | int h = height / sampleSize; |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 232 | if (src->config() == SkBitmap::kIndex8_Config) { |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 233 | // kIndex8 does not allow drawing via an SkCanvas, as is done below. |
| 234 | // Instead, use extractSubset. Note that this shares the SkPixelRef and |
| 235 | // SkColorTable. |
| 236 | // FIXME: Since src is discarded in practice, this holds on to more |
| 237 | // pixels than is strictly necessary. Switch to a copy if memory |
| 238 | // savings are more important than speed here. This also means |
| 239 | // that the pixels in dst can not be reused (though there is no |
| 240 | // allocation, which was already done on src). |
| 241 | int x = (dstX - srcX) / sampleSize; |
| 242 | int y = (dstY - srcY) / sampleSize; |
| 243 | SkIRect subset = SkIRect::MakeXYWH(x, y, w, h); |
| 244 | return src->extractSubset(dst, subset); |
| 245 | } |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 246 | // if the destination has no pixels then we must allocate them. |
| 247 | if (dst->isNull()) { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 248 | dst->setInfo(src->info().makeWH(w, h)); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 249 | |
| 250 | if (!this->allocPixelRef(dst, NULL)) { |
| 251 | SkDEBUGF(("failed to allocate pixels needed to crop the bitmap")); |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 252 | return false; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | // check to see if the destination is large enough to decode the desired |
| 256 | // region. If this assert fails we will just draw as much of the source |
| 257 | // into the destination that we can. |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 258 | if (dst->width() < w || dst->height() < h) { |
| 259 | SkDEBUGF(("SkImageDecoder::cropBitmap does not have a large enough bitmap.\n")); |
| 260 | } |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 261 | |
| 262 | // Set the Src_Mode for the paint to prevent transparency issue in the |
| 263 | // dest in the event that the dest was being re-used. |
| 264 | SkPaint paint; |
| 265 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 266 | |
| 267 | SkCanvas canvas(*dst); |
| 268 | canvas.drawSprite(*src, (srcX - dstX) / sampleSize, |
| 269 | (srcY - dstY) / sampleSize, |
| 270 | &paint); |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 271 | return true; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 272 | } |
| 273 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 274 | /////////////////////////////////////////////////////////////////////////////// |
| 275 | |
| 276 | bool SkImageDecoder::DecodeFile(const char file[], SkBitmap* bm, |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 277 | SkBitmap::Config pref, Mode mode, Format* format) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 278 | SkASSERT(file); |
| 279 | SkASSERT(bm); |
| 280 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 281 | SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(file)); |
mike@reedtribe.org | f381162 | 2013-03-19 02:18:33 +0000 | [diff] [blame] | 282 | if (stream.get()) { |
| 283 | if (SkImageDecoder::DecodeStream(stream, bm, pref, mode, format)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 284 | bm->pixelRef()->setURI(file); |
tomhudson@google.com | 1a36621 | 2012-01-03 14:42:08 +0000 | [diff] [blame] | 285 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 286 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 287 | } |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | bool SkImageDecoder::DecodeMemory(const void* buffer, size_t size, SkBitmap* bm, |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 292 | SkBitmap::Config pref, Mode mode, Format* format) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 293 | if (0 == size) { |
| 294 | return false; |
| 295 | } |
| 296 | SkASSERT(buffer); |
| 297 | |
| 298 | SkMemoryStream stream(buffer, size); |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 299 | return SkImageDecoder::DecodeStream(&stream, bm, pref, mode, format); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 300 | } |
| 301 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 302 | bool SkImageDecoder::DecodeStream(SkStreamRewindable* stream, SkBitmap* bm, |
| 303 | SkBitmap::Config pref, Mode mode, |
| 304 | Format* format) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 305 | SkASSERT(stream); |
| 306 | SkASSERT(bm); |
| 307 | |
| 308 | bool success = false; |
| 309 | SkImageDecoder* codec = SkImageDecoder::Factory(stream); |
| 310 | |
| 311 | if (NULL != codec) { |
| 312 | success = codec->decode(stream, bm, pref, mode); |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 313 | if (success && format) { |
| 314 | *format = codec->getFormat(); |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 315 | if (kUnknown_Format == *format) { |
| 316 | if (stream->rewind()) { |
| 317 | *format = GetStreamFormat(stream); |
| 318 | } |
| 319 | } |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 320 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 321 | delete codec; |
| 322 | } |
| 323 | return success; |
| 324 | } |