epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2007 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "bmpdecoderhelper.h" |
| 11 | #include "SkImageDecoder.h" |
| 12 | #include "SkScaledBitmapSampler.h" |
| 13 | #include "SkStream.h" |
| 14 | #include "SkColorPriv.h" |
| 15 | #include "SkTDArray.h" |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 16 | #include "SkTRegistry.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | |
| 18 | class SkBMPImageDecoder : public SkImageDecoder { |
| 19 | public: |
| 20 | SkBMPImageDecoder() {} |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 21 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | virtual Format getFormat() const { |
| 23 | return kBMP_Format; |
| 24 | } |
| 25 | |
| 26 | protected: |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 27 | virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | }; |
| 29 | |
robertphillips@google.com | ec51cb8 | 2012-03-23 18:13:47 +0000 | [diff] [blame] | 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | DEFINE_DECODER_CREATOR(BMPImageDecoder); |
| 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | |
caryclark@google.com | 2a2cc20 | 2012-06-06 12:04:36 +0000 | [diff] [blame] | 34 | static SkImageDecoder* sk_libbmp_dfactory(SkStream* stream) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | static const char kBmpMagic[] = { 'B', 'M' }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 36 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | size_t len = stream->getLength(); |
| 38 | char buffer[sizeof(kBmpMagic)]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 39 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | if (len > sizeof(kBmpMagic) && |
| 41 | stream->read(buffer, sizeof(kBmpMagic)) == sizeof(kBmpMagic) && |
| 42 | !memcmp(buffer, kBmpMagic, sizeof(kBmpMagic))) { |
| 43 | return SkNEW(SkBMPImageDecoder); |
| 44 | } |
| 45 | return NULL; |
| 46 | } |
| 47 | |
robertphillips@google.com | 8570b5c | 2012-03-20 17:40:58 +0000 | [diff] [blame] | 48 | static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libbmp_dfactory); |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 49 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | /////////////////////////////////////////////////////////////////////////////// |
| 51 | |
| 52 | class SkBmpDecoderCallback : public image_codec::BmpDecoderCallback { |
| 53 | public: |
| 54 | // we don't copy the bitmap, just remember the pointer |
| 55 | SkBmpDecoderCallback(bool justBounds) : fJustBounds(justBounds) {} |
| 56 | |
| 57 | // override from BmpDecoderCallback |
| 58 | virtual uint8* SetSize(int width, int height) { |
| 59 | fWidth = width; |
| 60 | fHeight = height; |
| 61 | if (fJustBounds) { |
| 62 | return NULL; |
| 63 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 64 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | fRGB.setCount(width * height * 3); // 3 == r, g, b |
| 66 | return fRGB.begin(); |
| 67 | } |
| 68 | |
| 69 | int width() const { return fWidth; } |
| 70 | int height() const { return fHeight; } |
| 71 | uint8_t* rgb() const { return fRGB.begin(); } |
| 72 | |
| 73 | private: |
| 74 | SkTDArray<uint8_t> fRGB; |
| 75 | int fWidth; |
| 76 | int fHeight; |
| 77 | bool fJustBounds; |
| 78 | }; |
| 79 | |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 80 | bool SkBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | |
| 82 | size_t length = stream->getLength(); |
| 83 | SkAutoMalloc storage(length); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 84 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | if (stream->read(storage.get(), length) != length) { |
| 86 | return false; |
| 87 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 88 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | const bool justBounds = SkImageDecoder::kDecodeBounds_Mode == mode; |
| 90 | SkBmpDecoderCallback callback(justBounds); |
| 91 | |
| 92 | // Now decode the BMP into callback's rgb() array [r,g,b, r,g,b, ...] |
| 93 | { |
| 94 | image_codec::BmpDecoderHelper helper; |
| 95 | const int max_pixels = 16383*16383; // max width*height |
| 96 | if (!helper.DecodeImage((const char*)storage.get(), length, |
| 97 | max_pixels, &callback)) { |
| 98 | return false; |
| 99 | } |
| 100 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 101 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | // we don't need this anymore, so free it now (before we try to allocate |
| 103 | // the bitmap's pixels) rather than waiting for its destructor |
| 104 | storage.free(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 105 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | int width = callback.width(); |
| 107 | int height = callback.height(); |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 108 | SkBitmap::Config config = this->getPrefConfig(k32Bit_SrcDepth, false); |
| 109 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 | // only accept prefConfig if it makes sense for us |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 111 | if (SkBitmap::kARGB_4444_Config != config && |
| 112 | SkBitmap::kRGB_565_Config != config) { |
| 113 | config = SkBitmap::kARGB_8888_Config; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | SkScaledBitmapSampler sampler(width, height, getSampleSize()); |
| 117 | |
| 118 | bm->setConfig(config, sampler.scaledWidth(), sampler.scaledHeight()); |
| 119 | bm->setIsOpaque(true); |
| 120 | if (justBounds) { |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | if (!this->allocPixelRef(bm, NULL)) { |
| 125 | return false; |
| 126 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 127 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | SkAutoLockPixels alp(*bm); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 129 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, getDitherImage())) { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | const int srcRowBytes = width * 3; |
| 135 | const int dstHeight = sampler.scaledHeight(); |
| 136 | const uint8_t* srcRow = callback.rgb(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 137 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 138 | srcRow += sampler.srcY0() * srcRowBytes; |
| 139 | for (int y = 0; y < dstHeight; y++) { |
| 140 | sampler.next(srcRow); |
| 141 | srcRow += sampler.srcDY() * srcRowBytes; |
| 142 | } |
| 143 | return true; |
| 144 | } |