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" |
scroggo@google.com | dbf9f88 | 2013-08-21 15:01:48 +0000 | [diff] [blame] | 11 | #include "SkColorPriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkImageDecoder.h" |
| 13 | #include "SkScaledBitmapSampler.h" |
| 14 | #include "SkStream.h" |
halcanary | 67ec1f8 | 2014-06-27 11:36:20 -0700 | [diff] [blame] | 15 | #include "SkStreamPriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "SkTDArray.h" |
| 17 | |
| 18 | class SkBMPImageDecoder : public SkImageDecoder { |
| 19 | public: |
| 20 | SkBMPImageDecoder() {} |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 21 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 22 | Format getFormat() const override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | return kBMP_Format; |
| 24 | } |
| 25 | |
| 26 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 27 | Result onDecode(SkStream* stream, SkBitmap* bm, Mode mode) override; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 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 | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 37 | static bool is_bmp(SkStreamRewindable* 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 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 47 | static SkImageDecoder* sk_libbmp_dfactory(SkStreamRewindable* stream) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 48 | if (is_bmp(stream)) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 49 | return new SkBMPImageDecoder; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 51 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | } |
| 53 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 54 | static SkImageDecoder_DecodeReg gReg(sk_libbmp_dfactory); |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 55 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 56 | static SkImageDecoder::Format get_format_bmp(SkStreamRewindable* stream) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 57 | if (is_bmp(stream)) { |
| 58 | return SkImageDecoder::kBMP_Format; |
| 59 | } |
| 60 | return SkImageDecoder::kUnknown_Format; |
| 61 | } |
| 62 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 63 | static SkImageDecoder_FormatReg gFormatReg(get_format_bmp); |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 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) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 77 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 95 | SkImageDecoder::Result SkBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { |
scroggo@google.com | 2fd740f | 2013-08-21 14:59:03 +0000 | [diff] [blame] | 96 | // First read the entire stream, so that all of the data can be passed to |
| 97 | // the BmpDecoderHelper. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | |
scroggo@google.com | 2fd740f | 2013-08-21 14:59:03 +0000 | [diff] [blame] | 99 | // Allocated space used to hold the data. |
| 100 | SkAutoMalloc storage; |
scroggo@google.com | dbf9f88 | 2013-08-21 15:01:48 +0000 | [diff] [blame] | 101 | // Byte length of all of the data. |
halcanary | 67ec1f8 | 2014-06-27 11:36:20 -0700 | [diff] [blame] | 102 | const size_t length = SkCopyStreamToStorage(&storage, stream); |
scroggo@google.com | dbf9f88 | 2013-08-21 15:01:48 +0000 | [diff] [blame] | 103 | if (0 == length) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 104 | return kFailure; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 106 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | const bool justBounds = SkImageDecoder::kDecodeBounds_Mode == mode; |
| 108 | SkBmpDecoderCallback callback(justBounds); |
| 109 | |
| 110 | // Now decode the BMP into callback's rgb() array [r,g,b, r,g,b, ...] |
| 111 | { |
| 112 | image_codec::BmpDecoderHelper helper; |
| 113 | const int max_pixels = 16383*16383; // max width*height |
| 114 | if (!helper.DecodeImage((const char*)storage.get(), length, |
| 115 | max_pixels, &callback)) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 116 | return kFailure; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 119 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | // we don't need this anymore, so free it now (before we try to allocate |
| 121 | // the bitmap's pixels) rather than waiting for its destructor |
| 122 | storage.free(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 123 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | int width = callback.width(); |
| 125 | int height = callback.height(); |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 126 | SkColorType colorType = this->getPrefColorType(k32Bit_SrcDepth, false); |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 127 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | // only accept prefConfig if it makes sense for us |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 129 | if (kARGB_4444_SkColorType != colorType && kRGB_565_SkColorType != colorType) { |
| 130 | colorType = kN32_SkColorType; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | SkScaledBitmapSampler sampler(width, height, getSampleSize()); |
| 134 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 135 | bm->setInfo(SkImageInfo::Make(sampler.scaledWidth(), sampler.scaledHeight(), |
| 136 | colorType, kOpaque_SkAlphaType)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | |
scroggo@google.com | bc69ce9 | 2013-07-09 15:45:14 +0000 | [diff] [blame] | 138 | if (justBounds) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 139 | return kSuccess; |
scroggo@google.com | bc69ce9 | 2013-07-09 15:45:14 +0000 | [diff] [blame] | 140 | } |
| 141 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 142 | if (!this->allocPixelRef(bm, nullptr)) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 143 | return kFailure; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 145 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | SkAutoLockPixels alp(*bm); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 147 | |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 148 | if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 149 | return kFailure; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | const int srcRowBytes = width * 3; |
| 153 | const int dstHeight = sampler.scaledHeight(); |
| 154 | const uint8_t* srcRow = callback.rgb(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 155 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | srcRow += sampler.srcY0() * srcRowBytes; |
| 157 | for (int y = 0; y < dstHeight; y++) { |
| 158 | sampler.next(srcRow); |
| 159 | srcRow += sampler.srcDY() * srcRowBytes; |
| 160 | } |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 161 | return kSuccess; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | } |