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