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