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