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