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 | |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 17 | SK_DEFINE_INST_COUNT(SkImageDecoder::Peeker) |
| 18 | SK_DEFINE_INST_COUNT(SkImageDecoder::Chooser) |
| 19 | SK_DEFINE_INST_COUNT(SkImageDecoderFactory) |
| 20 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | static SkBitmap::Config gDeviceConfig = SkBitmap::kNo_Config; |
| 22 | |
| 23 | SkBitmap::Config SkImageDecoder::GetDeviceConfig() |
| 24 | { |
| 25 | return gDeviceConfig; |
| 26 | } |
| 27 | |
| 28 | void SkImageDecoder::SetDeviceConfig(SkBitmap::Config config) |
| 29 | { |
| 30 | gDeviceConfig = config; |
| 31 | } |
| 32 | |
| 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | |
| 35 | SkImageDecoder::SkImageDecoder() |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 36 | : fPeeker(NULL) |
| 37 | , fChooser(NULL) |
| 38 | , fAllocator(NULL) |
| 39 | , fSampleSize(1) |
| 40 | , fDefaultPref(SkBitmap::kNo_Config) |
| 41 | , fDitherImage(true) |
| 42 | , fUsePrefTable(false) |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 43 | , fSkipWritingZeroes(false) |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 44 | , fPreferQualityOverSpeed(false) |
| 45 | , fRequireUnpremultipliedColors(false) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | SkImageDecoder::~SkImageDecoder() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 49 | SkSafeUnref(fPeeker); |
| 50 | SkSafeUnref(fChooser); |
| 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); |
| 59 | other->setChooser(fChooser); |
| 60 | other->setAllocator(fAllocator); |
| 61 | other->setSampleSize(fSampleSize); |
| 62 | if (fUsePrefTable) { |
| 63 | other->setPrefConfigTable(fPrefTable); |
| 64 | } else { |
| 65 | other->fDefaultPref = fDefaultPref; |
| 66 | } |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 67 | other->setDitherImage(fDitherImage); |
| 68 | other->setSkipWritingZeroes(fSkipWritingZeroes); |
scroggo@google.com | 468142b | 2013-07-09 15:48:24 +0000 | [diff] [blame] | 69 | other->setPreferQualityOverSpeed(fPreferQualityOverSpeed); |
| 70 | other->setRequireUnpremultipliedColors(fRequireUnpremultipliedColors); |
| 71 | } |
| 72 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | SkImageDecoder::Format SkImageDecoder::getFormat() const { |
| 74 | return kUnknown_Format; |
| 75 | } |
| 76 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 77 | const char* SkImageDecoder::getFormatName() const { |
scroggo@google.com | f98118e | 2013-05-15 14:53:49 +0000 | [diff] [blame] | 78 | return GetFormatName(this->getFormat()); |
| 79 | } |
| 80 | |
| 81 | const char* SkImageDecoder::GetFormatName(Format format) { |
| 82 | switch (format) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 83 | case kUnknown_Format: |
| 84 | return "Unknown Format"; |
| 85 | case kBMP_Format: |
| 86 | return "BMP"; |
| 87 | case kGIF_Format: |
| 88 | return "GIF"; |
| 89 | case kICO_Format: |
| 90 | return "ICO"; |
| 91 | case kJPEG_Format: |
| 92 | return "JPEG"; |
| 93 | case kPNG_Format: |
| 94 | return "PNG"; |
| 95 | case kWBMP_Format: |
| 96 | return "WBMP"; |
| 97 | case kWEBP_Format: |
| 98 | return "WEBP"; |
| 99 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 100 | SkDEBUGFAIL("Invalid format type!"); |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 101 | } |
| 102 | return "Unknown Format"; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 103 | } |
| 104 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | SkImageDecoder::Peeker* SkImageDecoder::setPeeker(Peeker* peeker) { |
| 106 | SkRefCnt_SafeAssign(fPeeker, peeker); |
| 107 | return peeker; |
| 108 | } |
| 109 | |
| 110 | SkImageDecoder::Chooser* SkImageDecoder::setChooser(Chooser* chooser) { |
| 111 | SkRefCnt_SafeAssign(fChooser, chooser); |
| 112 | return chooser; |
| 113 | } |
| 114 | |
| 115 | SkBitmap::Allocator* SkImageDecoder::setAllocator(SkBitmap::Allocator* alloc) { |
| 116 | SkRefCnt_SafeAssign(fAllocator, alloc); |
| 117 | return alloc; |
| 118 | } |
| 119 | |
| 120 | void SkImageDecoder::setSampleSize(int size) { |
| 121 | if (size < 1) { |
| 122 | size = 1; |
| 123 | } |
| 124 | fSampleSize = size; |
| 125 | } |
| 126 | |
| 127 | bool SkImageDecoder::chooseFromOneChoice(SkBitmap::Config config, int width, |
| 128 | int height) const { |
| 129 | Chooser* chooser = fChooser; |
| 130 | |
| 131 | if (NULL == chooser) { // no chooser, we just say YES to decoding :) |
| 132 | return true; |
| 133 | } |
| 134 | chooser->begin(1); |
| 135 | chooser->inspect(0, config, width, height); |
| 136 | return chooser->choose() == 0; |
| 137 | } |
| 138 | |
| 139 | bool SkImageDecoder::allocPixelRef(SkBitmap* bitmap, |
| 140 | SkColorTable* ctable) const { |
| 141 | return bitmap->allocPixels(fAllocator, ctable); |
| 142 | } |
| 143 | |
| 144 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | b6137c3 | 2009-07-29 20:56:52 +0000 | [diff] [blame] | 145 | |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 146 | void SkImageDecoder::setPrefConfigTable(const SkBitmap::Config pref[6]) { |
| 147 | if (NULL == pref) { |
| 148 | fUsePrefTable = false; |
| 149 | } else { |
| 150 | fUsePrefTable = true; |
scroggo@google.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 151 | fPrefTable.fPrefFor_8Index_NoAlpha_src = pref[0]; |
| 152 | fPrefTable.fPrefFor_8Index_YesAlpha_src = pref[1]; |
| 153 | fPrefTable.fPrefFor_8Gray_src = SkBitmap::kNo_Config; |
| 154 | fPrefTable.fPrefFor_8bpc_NoAlpha_src = pref[4]; |
| 155 | fPrefTable.fPrefFor_8bpc_YesAlpha_src = pref[5]; |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
scroggo@google.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 159 | void SkImageDecoder::setPrefConfigTable(const PrefConfigTable& prefTable) { |
| 160 | fUsePrefTable = true; |
| 161 | fPrefTable = prefTable; |
| 162 | } |
| 163 | |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 164 | SkBitmap::Config SkImageDecoder::getPrefConfig(SrcDepth srcDepth, |
| 165 | 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 | } |
| 189 | return config; |
| 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; |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 232 | if (src->getConfig() == SkBitmap::kIndex8_Config) { |
| 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@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 248 | dst->setConfig(src->getConfig(), w, h, 0, src->alphaType()); |
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 | 67882dd | 2013-05-20 16:58:16 +0000 | [diff] [blame] | 302 | /** |
| 303 | * Special allocator used by DecodeMemoryToTarget. Uses preallocated memory |
| 304 | * provided if the bm is 8888. Otherwise, uses a heap allocator. The same |
| 305 | * allocator will be used again for a copy to 8888, when the preallocated |
| 306 | * memory will be used. |
| 307 | */ |
| 308 | class TargetAllocator : public SkBitmap::HeapAllocator { |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 309 | |
| 310 | public: |
| 311 | TargetAllocator(void* target) |
| 312 | : fTarget(target) {} |
| 313 | |
| 314 | virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) SK_OVERRIDE { |
scroggo@google.com | 67882dd | 2013-05-20 16:58:16 +0000 | [diff] [blame] | 315 | // If the config is not 8888, allocate a pixelref using the heap. |
| 316 | // fTarget will be used to store the final pixels when copied to |
| 317 | // 8888. |
| 318 | if (bm->config() != SkBitmap::kARGB_8888_Config) { |
| 319 | return INHERITED::allocPixelRef(bm, ct); |
| 320 | } |
| 321 | // In kARGB_8888_Config, there is no colortable. |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 322 | SkASSERT(NULL == ct); |
| 323 | bm->setPixels(fTarget); |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | private: |
| 328 | void* fTarget; |
scroggo@google.com | 67882dd | 2013-05-20 16:58:16 +0000 | [diff] [blame] | 329 | typedef SkBitmap::HeapAllocator INHERITED; |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 330 | }; |
| 331 | |
scroggo@google.com | 67882dd | 2013-05-20 16:58:16 +0000 | [diff] [blame] | 332 | /** |
| 333 | * Helper function for DecodeMemoryToTarget. DecodeMemoryToTarget wants |
| 334 | * 8888, so set the config to it. All parameters must not be null. |
| 335 | * @param decoder Decoder appropriate for this stream. |
| 336 | * @param stream Rewound stream to the encoded data. |
| 337 | * @param bitmap On success, will have its bounds set to the bounds of the |
| 338 | * encoded data, and its config set to 8888. |
| 339 | * @return True if the bounds were decoded and the bitmap is 8888 or can be |
| 340 | * copied to 8888. |
| 341 | */ |
| 342 | static bool decode_bounds_to_8888(SkImageDecoder* decoder, SkStream* stream, |
| 343 | SkBitmap* bitmap) { |
| 344 | SkASSERT(decoder != NULL); |
| 345 | SkASSERT(stream != NULL); |
| 346 | SkASSERT(bitmap != NULL); |
| 347 | |
| 348 | if (!decoder->decode(stream, bitmap, SkImageDecoder::kDecodeBounds_Mode)) { |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | if (bitmap->config() == SkBitmap::kARGB_8888_Config) { |
| 353 | return true; |
| 354 | } |
| 355 | |
| 356 | if (!bitmap->canCopyTo(SkBitmap::kARGB_8888_Config)) { |
| 357 | return false; |
| 358 | } |
| 359 | |
| 360 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, bitmap->width(), bitmap->height()); |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Helper function for DecodeMemoryToTarget. Decodes the stream into bitmap, and if |
| 366 | * the bitmap is not 8888, then it is copied to 8888. Either way, the end result has |
| 367 | * its pixels stored in target. All parameters must not be null. |
| 368 | * @param decoder Decoder appropriate for this stream. |
| 369 | * @param stream Rewound stream to the encoded data. |
| 370 | * @param bitmap On success, will contain the decoded image, with its pixels stored |
| 371 | * at target. |
| 372 | * @param target Preallocated memory for storing pixels. |
| 373 | * @return bool Whether the decode (and copy, if necessary) succeeded. |
| 374 | */ |
| 375 | static bool decode_pixels_to_8888(SkImageDecoder* decoder, SkStream* stream, |
| 376 | SkBitmap* bitmap, void* target) { |
| 377 | SkASSERT(decoder != NULL); |
| 378 | SkASSERT(stream != NULL); |
| 379 | SkASSERT(bitmap != NULL); |
| 380 | SkASSERT(target != NULL); |
| 381 | |
| 382 | TargetAllocator allocator(target); |
| 383 | decoder->setAllocator(&allocator); |
| 384 | |
| 385 | bool success = decoder->decode(stream, bitmap, SkImageDecoder::kDecodePixels_Mode); |
| 386 | decoder->setAllocator(NULL); |
| 387 | |
| 388 | if (!success) { |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | if (bitmap->config() == SkBitmap::kARGB_8888_Config) { |
| 393 | return true; |
| 394 | } |
| 395 | |
| 396 | SkBitmap bm8888; |
| 397 | if (!bitmap->copyTo(&bm8888, SkBitmap::kARGB_8888_Config, &allocator)) { |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | bitmap->swap(bm8888); |
| 402 | return true; |
| 403 | } |
| 404 | |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 405 | bool SkImageDecoder::DecodeMemoryToTarget(const void* buffer, size_t size, |
| 406 | SkImage::Info* info, |
| 407 | const SkBitmapFactory::Target* target) { |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 408 | // FIXME: Just to get this working, implement in terms of existing |
| 409 | // ImageDecoder calls. |
| 410 | SkBitmap bm; |
| 411 | SkMemoryStream stream(buffer, size); |
| 412 | SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream)); |
scroggo@google.com | 67882dd | 2013-05-20 16:58:16 +0000 | [diff] [blame] | 413 | if (NULL == decoder.get()) { |
| 414 | return false; |
| 415 | } |
| 416 | |
| 417 | if (!decode_bounds_to_8888(decoder.get(), &stream, &bm)) { |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config); |
| 422 | |
| 423 | // Now set info properly. |
| 424 | // Since Config is SkBitmap::kARGB_8888_Config, SkBitmapToImageInfo |
| 425 | // will always succeed. |
reed@google.com | 114038a | 2013-10-21 20:13:42 +0000 | [diff] [blame] | 426 | if (info) { |
| 427 | SkAssertResult(SkBitmapToImageInfo(bm, info)); |
| 428 | } |
scroggo@google.com | 67882dd | 2013-05-20 16:58:16 +0000 | [diff] [blame] | 429 | |
| 430 | if (NULL == target) { |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | if (target->fRowBytes != SkToU32(bm.rowBytes())) { |
reed@google.com | 114038a | 2013-10-21 20:13:42 +0000 | [diff] [blame] | 435 | size_t minRB = SkBitmap::ComputeRowBytes(bm.config(), bm.width()); |
| 436 | if (target->fRowBytes < minRB) { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 437 | SkDEBUGFAIL("Desired row bytes is too small"); |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 438 | return false; |
| 439 | } |
scroggo@google.com | 67882dd | 2013-05-20 16:58:16 +0000 | [diff] [blame] | 440 | bm.setConfig(bm.config(), bm.width(), bm.height(), target->fRowBytes); |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 441 | } |
scroggo@google.com | 67882dd | 2013-05-20 16:58:16 +0000 | [diff] [blame] | 442 | |
| 443 | // SkMemoryStream.rewind() will always return true. |
| 444 | SkAssertResult(stream.rewind()); |
| 445 | return decode_pixels_to_8888(decoder.get(), &stream, &bm, target->fAddr); |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 449 | bool SkImageDecoder::DecodeStream(SkStreamRewindable* stream, SkBitmap* bm, |
| 450 | SkBitmap::Config pref, Mode mode, |
| 451 | Format* format) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 452 | SkASSERT(stream); |
| 453 | SkASSERT(bm); |
| 454 | |
| 455 | bool success = false; |
| 456 | SkImageDecoder* codec = SkImageDecoder::Factory(stream); |
| 457 | |
| 458 | if (NULL != codec) { |
| 459 | success = codec->decode(stream, bm, pref, mode); |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 460 | if (success && format) { |
| 461 | *format = codec->getFormat(); |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 462 | if (kUnknown_Format == *format) { |
| 463 | if (stream->rewind()) { |
| 464 | *format = GetStreamFormat(stream); |
| 465 | } |
| 466 | } |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 467 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 468 | delete codec; |
| 469 | } |
| 470 | return success; |
| 471 | } |