epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2006 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkImageDecoder.h" |
| 11 | #include "SkColor.h" |
| 12 | #include "SkColorPriv.h" |
| 13 | #include "SkMath.h" |
| 14 | #include "SkStream.h" |
| 15 | #include "SkTemplates.h" |
| 16 | #include "SkUtils.h" |
| 17 | |
| 18 | class SkWBMPImageDecoder : public SkImageDecoder { |
| 19 | public: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 20 | Format getFormat() const SK_OVERRIDE { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | return kWBMP_Format; |
| 22 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 23 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | protected: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 25 | Result onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 26 | |
| 27 | private: |
| 28 | typedef SkImageDecoder INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | static bool read_byte(SkStream* stream, uint8_t* data) |
| 32 | { |
| 33 | return stream->read(data, 1) == 1; |
| 34 | } |
| 35 | |
| 36 | static bool read_mbf(SkStream* stream, int* value) |
| 37 | { |
| 38 | int n = 0; |
| 39 | uint8_t data; |
| 40 | do { |
| 41 | if (!read_byte(stream, &data)) { |
| 42 | return false; |
| 43 | } |
| 44 | n = (n << 7) | (data & 0x7F); |
| 45 | } while (data & 0x80); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 46 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | *value = n; |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | struct wbmp_head { |
| 52 | int fWidth; |
| 53 | int fHeight; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 54 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | bool init(SkStream* stream) |
| 56 | { |
| 57 | uint8_t data; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 58 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | if (!read_byte(stream, &data) || data != 0) { // unknown type |
| 60 | return false; |
| 61 | } |
| 62 | if (!read_byte(stream, &data) || (data & 0x9F)) { // skip fixed header |
| 63 | return false; |
| 64 | } |
| 65 | if (!read_mbf(stream, &fWidth) || (unsigned)fWidth > 0xFFFF) { |
| 66 | return false; |
| 67 | } |
| 68 | if (!read_mbf(stream, &fHeight) || (unsigned)fHeight > 0xFFFF) { |
| 69 | return false; |
| 70 | } |
| 71 | return fWidth != 0 && fHeight != 0; |
| 72 | } |
| 73 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 74 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | static void expand_bits_to_bytes(uint8_t dst[], const uint8_t src[], int bits) |
| 76 | { |
| 77 | int bytes = bits >> 3; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 78 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | for (int i = 0; i < bytes; i++) { |
| 80 | unsigned mask = *src++; |
| 81 | dst[0] = (mask >> 7) & 1; |
| 82 | dst[1] = (mask >> 6) & 1; |
| 83 | dst[2] = (mask >> 5) & 1; |
| 84 | dst[3] = (mask >> 4) & 1; |
| 85 | dst[4] = (mask >> 3) & 1; |
| 86 | dst[5] = (mask >> 2) & 1; |
| 87 | dst[6] = (mask >> 1) & 1; |
| 88 | dst[7] = (mask >> 0) & 1; |
| 89 | dst += 8; |
| 90 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 91 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | bits &= 7; |
| 93 | if (bits > 0) { |
| 94 | unsigned mask = *src; |
| 95 | do { |
| 96 | *dst++ = (mask >> 7) & 1;; |
| 97 | mask <<= 1; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 98 | } while (--bits != 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 102 | SkImageDecoder::Result SkWBMPImageDecoder::onDecode(SkStream* stream, SkBitmap* decodedBitmap, |
| 103 | Mode mode) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | { |
| 105 | wbmp_head head; |
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 | if (!head.init(stream)) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 108 | return kFailure; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 110 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | int width = head.fWidth; |
| 112 | int height = head.fHeight; |
skia.committer@gmail.com | c49cabf | 2013-03-15 07:05:19 +0000 | [diff] [blame] | 113 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 114 | decodedBitmap->setInfo(SkImageInfo::Make(width, height, |
| 115 | kIndex_8_SkColorType, kOpaque_SkAlphaType)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 116 | |
scroggo@google.com | bc69ce9 | 2013-07-09 15:45:14 +0000 | [diff] [blame] | 117 | if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 118 | return kSuccess; |
scroggo@google.com | bc69ce9 | 2013-07-09 15:45:14 +0000 | [diff] [blame] | 119 | } |
| 120 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | const SkPMColor colors[] = { SK_ColorBLACK, SK_ColorWHITE }; |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 122 | SkColorTable* ct = SkNEW_ARGS(SkColorTable, (colors, 2)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | SkAutoUnref aur(ct); |
| 124 | |
| 125 | if (!this->allocPixelRef(decodedBitmap, ct)) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 126 | return kFailure; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | SkAutoLockPixels alp(*decodedBitmap); |
| 130 | |
| 131 | uint8_t* dst = decodedBitmap->getAddr8(0, 0); |
| 132 | // store the 1-bit valuess at the end of our pixels, so we won't stomp |
| 133 | // on them before we're read them. Just trying to avoid a temp allocation |
| 134 | size_t srcRB = SkAlign8(width) >> 3; |
| 135 | size_t srcSize = height * srcRB; |
| 136 | uint8_t* src = dst + decodedBitmap->getSize() - srcSize; |
| 137 | if (stream->read(src, srcSize) != srcSize) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 138 | return kFailure; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | for (int y = 0; y < height; y++) |
| 142 | { |
| 143 | expand_bits_to_bytes(dst, src, width); |
| 144 | dst += decodedBitmap->rowBytes(); |
| 145 | src += srcRB; |
| 146 | } |
| 147 | |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 148 | return kSuccess; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | } |
| 150 | |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 151 | /////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | ec51cb8 | 2012-03-23 18:13:47 +0000 | [diff] [blame] | 152 | DEFINE_DECODER_CREATOR(WBMPImageDecoder); |
| 153 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 154 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 155 | static SkImageDecoder* sk_wbmp_dfactory(SkStreamRewindable* stream) { |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 156 | wbmp_head head; |
| 157 | |
| 158 | if (head.init(stream)) { |
| 159 | return SkNEW(SkWBMPImageDecoder); |
| 160 | } |
| 161 | return NULL; |
| 162 | } |
| 163 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 164 | static SkImageDecoder::Format get_format_wbmp(SkStreamRewindable* stream) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 165 | wbmp_head head; |
| 166 | if (head.init(stream)) { |
| 167 | return SkImageDecoder::kWBMP_Format; |
| 168 | } |
| 169 | return SkImageDecoder::kUnknown_Format; |
| 170 | } |
| 171 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 172 | static SkImageDecoder_DecodeReg gDReg(sk_wbmp_dfactory); |
| 173 | static SkImageDecoder_FormatReg gFormatReg(get_format_wbmp); |