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