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