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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/codec/SkWbmpCodec.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/codec/SkCodec.h" |
| 11 | #include "include/core/SkData.h" |
| 12 | #include "include/core/SkStream.h" |
| 13 | #include "include/private/SkColorData.h" |
| 14 | #include "include/private/SkTo.h" |
| 15 | #include "src/codec/SkCodecPriv.h" |
| 16 | #include "src/codec/SkColorTable.h" |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 17 | |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 18 | // Each bit represents a pixel, so width is actually a number of bits. |
| 19 | // A row will always be stored in bytes, so we round width up to the |
| 20 | // nearest multiple of 8 to get the number of bits actually in the row. |
| 21 | // We then divide by 8 to convert to bytes. |
| 22 | static inline size_t get_src_row_bytes(int width) { |
| 23 | return SkAlign8(width) >> 3; |
| 24 | } |
| 25 | |
Matt Sarett | a225e9b | 2016-11-07 10:26:17 -0500 | [diff] [blame] | 26 | static inline bool valid_color_type(const SkImageInfo& dstInfo) { |
| 27 | switch (dstInfo.colorType()) { |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 28 | case kRGBA_8888_SkColorType: |
| 29 | case kBGRA_8888_SkColorType: |
msarett | b30d698 | 2016-02-15 10:18:45 -0800 | [diff] [blame] | 30 | case kGray_8_SkColorType: |
| 31 | case kRGB_565_SkColorType: |
scroggo | ba58489 | 2016-05-20 13:56:13 -0700 | [diff] [blame] | 32 | return true; |
Matt Sarett | a225e9b | 2016-11-07 10:26:17 -0500 | [diff] [blame] | 33 | case kRGBA_F16_SkColorType: |
Mike Klein | ce4cf72 | 2018-05-10 11:29:15 -0400 | [diff] [blame] | 34 | return dstInfo.colorSpace(); |
msarett | b30d698 | 2016-02-15 10:18:45 -0800 | [diff] [blame] | 35 | default: |
| 36 | return false; |
| 37 | } |
| 38 | } |
| 39 | |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 40 | static bool read_byte(SkStream* stream, uint8_t* data) |
| 41 | { |
| 42 | return stream->read(data, 1) == 1; |
| 43 | } |
| 44 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 45 | // http://en.wikipedia.org/wiki/Variable-length_quantity |
| 46 | static bool read_mbf(SkStream* stream, uint64_t* value) { |
| 47 | uint64_t n = 0; |
| 48 | uint8_t data; |
| 49 | const uint64_t kLimit = 0xFE00000000000000; |
| 50 | SkASSERT(kLimit == ~((~static_cast<uint64_t>(0)) >> 7)); |
| 51 | do { |
| 52 | if (n & kLimit) { // Will overflow on shift by 7. |
| 53 | return false; |
| 54 | } |
| 55 | if (stream->read(&data, 1) != 1) { |
| 56 | return false; |
| 57 | } |
| 58 | n = (n << 7) | (data & 0x7F); |
| 59 | } while (data & 0x80); |
| 60 | *value = n; |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | static bool read_header(SkStream* stream, SkISize* size) { |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 65 | { |
| 66 | uint8_t data; |
| 67 | if (!read_byte(stream, &data) || data != 0) { // unknown type |
| 68 | return false; |
| 69 | } |
| 70 | if (!read_byte(stream, &data) || (data & 0x9F)) { // skip fixed header |
| 71 | return false; |
| 72 | } |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 73 | } |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 74 | |
| 75 | uint64_t width, height; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 76 | if (!read_mbf(stream, &width) || width > 0xFFFF || !width) { |
| 77 | return false; |
| 78 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 79 | if (!read_mbf(stream, &height) || height > 0xFFFF || !height) { |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 80 | return false; |
| 81 | } |
| 82 | if (size) { |
| 83 | *size = SkISize::Make(SkToS32(width), SkToS32(height)); |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 88 | bool SkWbmpCodec::onRewind() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 89 | return read_header(this->stream(), nullptr); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 90 | } |
| 91 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 92 | bool SkWbmpCodec::readRow(uint8_t* row) { |
| 93 | return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes; |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 96 | SkWbmpCodec::SkWbmpCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream) |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 97 | // Wbmp does not need a colorXform, so choose an arbitrary srcFormat. |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 98 | : INHERITED(std::move(info), skcms_PixelFormat(), |
| 99 | std::move(stream)) |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 100 | , fSrcRowBytes(get_src_row_bytes(this->dimensions().width())) |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 101 | , fSwizzler(nullptr) |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 102 | {} |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 103 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 104 | SkEncodedImageFormat SkWbmpCodec::onGetEncodedFormat() const { |
| 105 | return SkEncodedImageFormat::kWBMP; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 108 | bool SkWbmpCodec::conversionSupported(const SkImageInfo& dst, bool srcIsOpaque, |
| 109 | bool /*needsColorXform*/) { |
Leon Scroggins III | 0741818 | 2017-08-15 12:24:02 -0400 | [diff] [blame] | 110 | return valid_color_type(dst) && valid_alpha(dst.alphaType(), srcIsOpaque); |
| 111 | } |
| 112 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 113 | SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info, |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 114 | void* dst, |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 115 | size_t rowBytes, |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 116 | const Options& options, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 117 | int* rowsDecoded) { |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 118 | if (options.fSubset) { |
| 119 | // Subsets are not supported. |
| 120 | return kUnimplemented; |
| 121 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 122 | |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 123 | // Initialize the swizzler |
Leon Scroggins III | 65f4aea | 2018-10-24 12:17:22 -0400 | [diff] [blame] | 124 | std::unique_ptr<SkSwizzler> swizzler = SkSwizzler::Make(this->getEncodedInfo(), nullptr, info, |
| 125 | options); |
msarett | b30d698 | 2016-02-15 10:18:45 -0800 | [diff] [blame] | 126 | SkASSERT(swizzler); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 127 | |
| 128 | // Perform the decode |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 129 | SkISize size = info.dimensions(); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 130 | SkAutoTMalloc<uint8_t> src(fSrcRowBytes); |
| 131 | void* dstRow = dst; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 132 | for (int y = 0; y < size.height(); ++y) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 133 | if (!this->readRow(src.get())) { |
| 134 | *rowsDecoded = y; |
| 135 | return kIncompleteInput; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 136 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 137 | swizzler->swizzle(dstRow, src.get()); |
| 138 | dstRow = SkTAddOffset<void>(dstRow, rowBytes); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 139 | } |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 140 | return kSuccess; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 141 | } |
| 142 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 143 | bool SkWbmpCodec::IsWbmp(const void* buffer, size_t bytesRead) { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 144 | SkMemoryStream stream(buffer, bytesRead, false); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 145 | return read_header(&stream, nullptr); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 148 | std::unique_ptr<SkCodec> SkWbmpCodec::MakeFromStream(std::unique_ptr<SkStream> stream, |
| 149 | Result* result) { |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 150 | SkISize size; |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 151 | if (!read_header(stream.get(), &size)) { |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 152 | // This already succeeded in IsWbmp, so this stream was corrupted in/ |
| 153 | // after rewind. |
| 154 | *result = kCouldNotRewind; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 155 | return nullptr; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 156 | } |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 157 | *result = kSuccess; |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 158 | auto info = SkEncodedInfo::Make(size.width(), size.height(), SkEncodedInfo::kGray_Color, |
| 159 | SkEncodedInfo::kOpaque_Alpha, 1); |
| 160 | return std::unique_ptr<SkCodec>(new SkWbmpCodec(std::move(info), std::move(stream))); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 161 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 162 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 163 | int SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 164 | void* dstRow = dst; |
| 165 | for (int y = 0; y < count; ++y) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 166 | if (!this->readRow(fSrcBuffer.get())) { |
| 167 | return y; |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 168 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 169 | fSwizzler->swizzle(dstRow, fSrcBuffer.get()); |
| 170 | dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 171 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 172 | return count; |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 173 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 174 | |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 175 | bool SkWbmpCodec::onSkipScanlines(int count) { |
| 176 | const size_t bytesToSkip = count * fSrcRowBytes; |
| 177 | return this->stream()->skip(bytesToSkip) == bytesToSkip; |
| 178 | } |
| 179 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 180 | SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 181 | const Options& options) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 182 | if (options.fSubset) { |
| 183 | // Subsets are not supported. |
| 184 | return kUnimplemented; |
| 185 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 186 | |
Leon Scroggins III | 65f4aea | 2018-10-24 12:17:22 -0400 | [diff] [blame] | 187 | fSwizzler = SkSwizzler::Make(this->getEncodedInfo(), nullptr, dstInfo, options); |
msarett | b30d698 | 2016-02-15 10:18:45 -0800 | [diff] [blame] | 188 | SkASSERT(fSwizzler); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 189 | |
| 190 | fSrcBuffer.reset(fSrcRowBytes); |
| 191 | |
| 192 | return kSuccess; |
| 193 | } |