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