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 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 22 | virtual Format getFormat() const SK_OVERRIDE { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | return kBMP_Format; |
| 24 | } |
| 25 | |
| 26 | protected: |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 27 | virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode mode) SK_OVERRIDE; |
| 28 | |
| 29 | private: |
| 30 | typedef SkImageDecoder INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
robertphillips@google.com | ec51cb8 | 2012-03-23 18:13:47 +0000 | [diff] [blame] | 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | DEFINE_DECODER_CREATOR(BMPImageDecoder); |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame^] | 37 | static bool is_bmp(SkStream* stream) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | static const char kBmpMagic[] = { 'B', 'M' }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 39 | |
djsollen@google.com | 5dd4502 | 2013-03-21 13:30:54 +0000 | [diff] [blame] | 40 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | char buffer[sizeof(kBmpMagic)]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 42 | |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame^] | 43 | return stream->read(buffer, sizeof(kBmpMagic)) == sizeof(kBmpMagic) && |
| 44 | !memcmp(buffer, kBmpMagic, sizeof(kBmpMagic)); |
| 45 | } |
| 46 | |
| 47 | static SkImageDecoder* sk_libbmp_dfactory(SkStream* stream) { |
| 48 | if (is_bmp(stream)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | return SkNEW(SkBMPImageDecoder); |
| 50 | } |
| 51 | return NULL; |
| 52 | } |
| 53 | |
robertphillips@google.com | 8570b5c | 2012-03-20 17:40:58 +0000 | [diff] [blame] | 54 | static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libbmp_dfactory); |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 55 | |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame^] | 56 | static SkImageDecoder::Format get_format_bmp(SkStream* stream) { |
| 57 | if (is_bmp(stream)) { |
| 58 | return SkImageDecoder::kBMP_Format; |
| 59 | } |
| 60 | return SkImageDecoder::kUnknown_Format; |
| 61 | } |
| 62 | |
| 63 | static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_bmp); |
| 64 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | /////////////////////////////////////////////////////////////////////////////// |
| 66 | |
| 67 | class SkBmpDecoderCallback : public image_codec::BmpDecoderCallback { |
| 68 | public: |
| 69 | // we don't copy the bitmap, just remember the pointer |
| 70 | SkBmpDecoderCallback(bool justBounds) : fJustBounds(justBounds) {} |
| 71 | |
| 72 | // override from BmpDecoderCallback |
| 73 | virtual uint8* SetSize(int width, int height) { |
| 74 | fWidth = width; |
| 75 | fHeight = height; |
| 76 | if (fJustBounds) { |
| 77 | return NULL; |
| 78 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 79 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | fRGB.setCount(width * height * 3); // 3 == r, g, b |
| 81 | return fRGB.begin(); |
| 82 | } |
| 83 | |
| 84 | int width() const { return fWidth; } |
| 85 | int height() const { return fHeight; } |
commit-bot@chromium.org | aa537d4 | 2013-02-28 19:03:13 +0000 | [diff] [blame] | 86 | const uint8_t* rgb() const { return fRGB.begin(); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | |
| 88 | private: |
| 89 | SkTDArray<uint8_t> fRGB; |
| 90 | int fWidth; |
| 91 | int fHeight; |
| 92 | bool fJustBounds; |
| 93 | }; |
| 94 | |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 95 | bool SkBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | |
| 97 | size_t length = stream->getLength(); |
| 98 | SkAutoMalloc storage(length); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 99 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | if (stream->read(storage.get(), length) != length) { |
| 101 | return false; |
| 102 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 103 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | const bool justBounds = SkImageDecoder::kDecodeBounds_Mode == mode; |
| 105 | SkBmpDecoderCallback callback(justBounds); |
| 106 | |
| 107 | // Now decode the BMP into callback's rgb() array [r,g,b, r,g,b, ...] |
| 108 | { |
| 109 | image_codec::BmpDecoderHelper helper; |
| 110 | const int max_pixels = 16383*16383; // max width*height |
| 111 | if (!helper.DecodeImage((const char*)storage.get(), length, |
| 112 | max_pixels, &callback)) { |
| 113 | return false; |
| 114 | } |
| 115 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 116 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | // we don't need this anymore, so free it now (before we try to allocate |
| 118 | // the bitmap's pixels) rather than waiting for its destructor |
| 119 | storage.free(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 120 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | int width = callback.width(); |
| 122 | int height = callback.height(); |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 123 | SkBitmap::Config config = this->getPrefConfig(k32Bit_SrcDepth, false); |
| 124 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | // only accept prefConfig if it makes sense for us |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 126 | if (SkBitmap::kARGB_4444_Config != config && |
| 127 | SkBitmap::kRGB_565_Config != config) { |
| 128 | config = SkBitmap::kARGB_8888_Config; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | SkScaledBitmapSampler sampler(width, height, getSampleSize()); |
| 132 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | if (justBounds) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 134 | bm->setConfig(config, sampler.scaledWidth(), sampler.scaledHeight()); |
| 135 | bm->setIsOpaque(true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 136 | return true; |
| 137 | } |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 138 | // No Bitmap reuse supported for this format |
| 139 | if (!bm->isNull()) { |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | bm->setConfig(config, sampler.scaledWidth(), sampler.scaledHeight()); |
| 144 | bm->setIsOpaque(true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | |
| 146 | if (!this->allocPixelRef(bm, NULL)) { |
| 147 | return false; |
| 148 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 149 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | SkAutoLockPixels alp(*bm); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 151 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, getDitherImage())) { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | const int srcRowBytes = width * 3; |
| 157 | const int dstHeight = sampler.scaledHeight(); |
| 158 | const uint8_t* srcRow = callback.rgb(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 159 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | srcRow += sampler.srcY0() * srcRowBytes; |
| 161 | for (int y = 0; y < dstHeight; y++) { |
| 162 | sampler.next(srcRow); |
| 163 | srcRow += sampler.srcDY() * srcRowBytes; |
| 164 | } |
| 165 | return true; |
| 166 | } |