halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 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 | |
| 8 | #include "SkCodec.h" |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 9 | #include "SkCodecPriv.h" |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 11 | #include "SkColorTable.h" |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 12 | #include "SkStream.h" |
| 13 | #include "SkCodec_wbmp.h" |
| 14 | |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 15 | // Each bit represents a pixel, so width is actually a number of bits. |
| 16 | // A row will always be stored in bytes, so we round width up to the |
| 17 | // nearest multiple of 8 to get the number of bits actually in the row. |
| 18 | // We then divide by 8 to convert to bytes. |
| 19 | static inline size_t get_src_row_bytes(int width) { |
| 20 | return SkAlign8(width) >> 3; |
| 21 | } |
| 22 | |
| 23 | static inline void setup_color_table(SkColorType colorType, |
| 24 | SkPMColor* colorPtr, int* colorCount) { |
| 25 | if (kIndex_8_SkColorType == colorType) { |
| 26 | colorPtr[0] = SK_ColorBLACK; |
| 27 | colorPtr[1] = SK_ColorWHITE; |
| 28 | *colorCount = 2; |
| 29 | } |
| 30 | } |
| 31 | |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame^] | 32 | static bool read_byte(SkStream* stream, uint8_t* data) |
| 33 | { |
| 34 | return stream->read(data, 1) == 1; |
| 35 | } |
| 36 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 37 | // http://en.wikipedia.org/wiki/Variable-length_quantity |
| 38 | static bool read_mbf(SkStream* stream, uint64_t* value) { |
| 39 | uint64_t n = 0; |
| 40 | uint8_t data; |
| 41 | const uint64_t kLimit = 0xFE00000000000000; |
| 42 | SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7)); |
| 43 | do { |
| 44 | if (n & kLimit) { // Will overflow on shift by 7. |
| 45 | return false; |
| 46 | } |
| 47 | if (stream->read(&data, 1) != 1) { |
| 48 | return false; |
| 49 | } |
| 50 | n = (n << 7) | (data & 0x7F); |
| 51 | } while (data & 0x80); |
| 52 | *value = n; |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | static bool read_header(SkStream* stream, SkISize* size) { |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame^] | 57 | { |
| 58 | uint8_t data; |
| 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 | } |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 65 | } |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame^] | 66 | |
| 67 | uint64_t width, height; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 68 | if (!read_mbf(stream, &width) || width > 0xFFFF || !width) { |
| 69 | return false; |
| 70 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 71 | if (!read_mbf(stream, &height) || height > 0xFFFF || !height) { |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | if (size) { |
| 75 | *size = SkISize::Make(SkToS32(width), SkToS32(height)); |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 80 | bool SkWbmpCodec::onRewind() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 81 | return read_header(this->stream(), nullptr); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 82 | } |
| 83 | |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 84 | SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, const SkPMColor* ctable, |
| 85 | const Options& opts) { |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 86 | // Create the swizzler based on the desired color type |
| 87 | switch (info.colorType()) { |
| 88 | case kIndex_8_SkColorType: |
| 89 | case kN32_SkColorType: |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 90 | case kRGB_565_SkColorType: |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 91 | case kGray_8_SkColorType: |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 92 | break; |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 93 | default: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 94 | return nullptr; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 95 | } |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 96 | return SkSwizzler::CreateSwizzler(SkSwizzler::kBit, ctable, info, opts); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 97 | } |
| 98 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 99 | bool SkWbmpCodec::readRow(uint8_t* row) { |
| 100 | return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes; |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 101 | } |
| 102 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 103 | SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream) |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 104 | : INHERITED(info, stream) |
| 105 | , fSrcRowBytes(get_src_row_bytes(this->getInfo().width())) |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 106 | , fSwizzler(nullptr) |
msarett | f724b99 | 2015-10-15 06:41:06 -0700 | [diff] [blame] | 107 | , fColorTable(nullptr) |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 108 | {} |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 109 | |
| 110 | SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const { |
| 111 | return kWBMP_SkEncodedFormat; |
| 112 | } |
| 113 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 114 | SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info, |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 115 | void* dst, |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 116 | size_t rowBytes, |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 117 | const Options& options, |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 118 | SkPMColor ctable[], |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 119 | int* ctableCount, |
| 120 | int* rowsDecoded) { |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 121 | if (options.fSubset) { |
| 122 | // Subsets are not supported. |
| 123 | return kUnimplemented; |
| 124 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 125 | |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 126 | if (!valid_alpha(info.alphaType(), this->getInfo().alphaType())) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 127 | return kInvalidConversion; |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 128 | } |
| 129 | |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 130 | // Prepare a color table if necessary |
| 131 | setup_color_table(info.colorType(), ctable, ctableCount); |
| 132 | |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 133 | // Initialize the swizzler |
| 134 | SkAutoTDelete<SkSwizzler> swizzler(this->initializeSwizzler(info, ctable, options)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 135 | if (nullptr == swizzler.get()) { |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 136 | return kInvalidConversion; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 137 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 138 | |
| 139 | // Perform the decode |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 140 | SkISize size = info.dimensions(); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 141 | SkAutoTMalloc<uint8_t> src(fSrcRowBytes); |
| 142 | void* dstRow = dst; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 143 | for (int y = 0; y < size.height(); ++y) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 144 | if (!this->readRow(src.get())) { |
| 145 | *rowsDecoded = y; |
| 146 | return kIncompleteInput; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 147 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 148 | swizzler->swizzle(dstRow, src.get()); |
| 149 | dstRow = SkTAddOffset<void>(dstRow, rowBytes); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 150 | } |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 151 | return kSuccess; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | bool SkWbmpCodec::IsWbmp(SkStream* stream) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 155 | return read_header(stream, nullptr); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) { |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 159 | SkAutoTDelete<SkStream> streamDeleter(stream); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 160 | SkISize size; |
| 161 | if (!read_header(stream, &size)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 162 | return nullptr; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 163 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 164 | SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), |
| 165 | kGray_8_SkColorType, kOpaque_SkAlphaType); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 166 | return new SkWbmpCodec(info, streamDeleter.detach()); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 167 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 168 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 169 | int SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 170 | void* dstRow = dst; |
| 171 | for (int y = 0; y < count; ++y) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 172 | if (!this->readRow(fSrcBuffer.get())) { |
| 173 | return y; |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 174 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 175 | fSwizzler->swizzle(dstRow, fSrcBuffer.get()); |
| 176 | dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 177 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 178 | return count; |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 179 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 180 | |
| 181 | SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, |
| 182 | const Options& options, SkPMColor inputColorTable[], int* inputColorCount) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 183 | if (options.fSubset) { |
| 184 | // Subsets are not supported. |
| 185 | return kUnimplemented; |
| 186 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 187 | |
| 188 | if (!valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType())) { |
| 189 | return kInvalidConversion; |
| 190 | } |
| 191 | |
| 192 | // Fill in the color table |
| 193 | setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount); |
| 194 | |
| 195 | // Copy the color table to a pointer that can be owned by the scanline decoder |
| 196 | if (kIndex_8_SkColorType == dstInfo.colorType()) { |
| 197 | fColorTable.reset(new SkColorTable(inputColorTable, 2)); |
| 198 | } |
| 199 | |
| 200 | // Initialize the swizzler |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 201 | fSwizzler.reset(this->initializeSwizzler(dstInfo, get_color_ptr(fColorTable.get()), options)); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 202 | if (nullptr == fSwizzler.get()) { |
| 203 | return kInvalidConversion; |
| 204 | } |
| 205 | |
| 206 | fSrcBuffer.reset(fSrcRowBytes); |
| 207 | |
| 208 | return kSuccess; |
| 209 | } |