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 | 0944100 | 2015-08-13 11:26:57 -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() { |
| 71 | return read_header(this->stream(), NULL); |
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) { |
| 76 | // TODO (msarett): Reenable support for 565 if it is desired |
| 77 | // skbug.com/3683 |
| 78 | |
| 79 | // Create the swizzler based on the desired color type |
| 80 | switch (info.colorType()) { |
| 81 | case kIndex_8_SkColorType: |
| 82 | case kN32_SkColorType: |
| 83 | case kGray_8_SkColorType: |
emmaleer | 0944100 | 2015-08-13 11:26:57 -0700 | [diff] [blame^] | 84 | return SkSwizzler::CreateSwizzler(SkSwizzler::kBit, ctable, info, opts.fZeroInitialized, |
| 85 | this->getInfo()); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 86 | default: |
| 87 | return NULL; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 91 | SkCodec::Result SkWbmpCodec::readRow(uint8_t* row) { |
| 92 | if (this->stream()->read(row, fSrcRowBytes) != fSrcRowBytes) { |
| 93 | return kIncompleteInput; |
| 94 | } |
| 95 | return kSuccess; |
| 96 | } |
| 97 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 98 | SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream) |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 99 | : INHERITED(info, stream) |
| 100 | , fSrcRowBytes(get_src_row_bytes(this->getInfo().width())) |
| 101 | {} |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 102 | |
| 103 | SkEncodedFormat SkWbmpCodec::onGetEncodedFormat() const { |
| 104 | return kWBMP_SkEncodedFormat; |
| 105 | } |
| 106 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 107 | SkCodec::Result SkWbmpCodec::onGetPixels(const SkImageInfo& info, |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 108 | void* dst, |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 109 | size_t rowBytes, |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 110 | const Options& options, |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 111 | SkPMColor ctable[], |
| 112 | int* ctableCount) { |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 113 | if (!this->rewindIfNeeded()) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 114 | return kCouldNotRewind; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 115 | } |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 116 | if (options.fSubset) { |
| 117 | // Subsets are not supported. |
| 118 | return kUnimplemented; |
| 119 | } |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 120 | if (info.dimensions() != this->getInfo().dimensions()) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 121 | return kInvalidScale; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 122 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 123 | |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 124 | if (!valid_alpha(info.alphaType(), this->getInfo().alphaType())) { |
| 125 | return SkCodec::kInvalidConversion; |
| 126 | } |
| 127 | |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 128 | // Prepare a color table if necessary |
| 129 | setup_color_table(info.colorType(), ctable, ctableCount); |
| 130 | |
| 131 | |
| 132 | // Initialize the swizzler |
| 133 | SkAutoTDelete<SkSwizzler> swizzler(this->initializeSwizzler(info, ctable, options)); |
| 134 | if (NULL == swizzler.get()) { |
| 135 | return kInvalidConversion; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 136 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 137 | |
| 138 | // Perform the decode |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 139 | SkISize size = info.dimensions(); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 140 | SkAutoTMalloc<uint8_t> src(fSrcRowBytes); |
| 141 | void* dstRow = dst; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 142 | for (int y = 0; y < size.height(); ++y) { |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 143 | Result rowResult = this->readRow(src.get()); |
| 144 | if (kSuccess != rowResult) { |
| 145 | return rowResult; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 146 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 147 | swizzler->swizzle(dstRow, src.get()); |
| 148 | dstRow = SkTAddOffset<void>(dstRow, rowBytes); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 149 | } |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 150 | return kSuccess; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | bool SkWbmpCodec::IsWbmp(SkStream* stream) { |
| 154 | return read_header(stream, NULL); |
| 155 | } |
| 156 | |
| 157 | SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) { |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 158 | SkAutoTDelete<SkStream> streamDeleter(stream); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 159 | SkISize size; |
| 160 | if (!read_header(stream, &size)) { |
| 161 | return NULL; |
| 162 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 163 | SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), |
| 164 | kGray_8_SkColorType, kOpaque_SkAlphaType); |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 165 | return SkNEW_ARGS(SkWbmpCodec, (info, streamDeleter.detach())); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 166 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 167 | |
| 168 | class SkWbmpScanlineDecoder : public SkScanlineDecoder { |
| 169 | public: |
| 170 | /* |
| 171 | * Takes ownership of all pointer paramters. |
| 172 | */ |
| 173 | SkWbmpScanlineDecoder(SkWbmpCodec* codec) |
| 174 | : INHERITED(codec->getInfo()) |
| 175 | , fCodec(codec) |
| 176 | , fColorTable(NULL) |
| 177 | , fSwizzler(NULL) |
| 178 | , fSrcBuffer(codec->fSrcRowBytes) |
| 179 | {} |
| 180 | |
| 181 | SkCodec::Result onGetScanlines(void* dst, int count, size_t dstRowBytes) override { |
| 182 | void* dstRow = dst; |
| 183 | for (int y = 0; y < count; ++y) { |
| 184 | SkCodec::Result rowResult = fCodec->readRow(fSrcBuffer.get()); |
| 185 | if (SkCodec::kSuccess != rowResult) { |
| 186 | return rowResult; |
| 187 | } |
| 188 | fSwizzler->swizzle(dstRow, fSrcBuffer.get()); |
| 189 | dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); |
| 190 | } |
| 191 | return SkCodec::kSuccess; |
| 192 | } |
| 193 | |
| 194 | SkCodec::Result onStart(const SkImageInfo& dstInfo, |
| 195 | const SkCodec::Options& options, SkPMColor inputColorTable[], |
| 196 | int* inputColorCount) { |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 197 | if (!fCodec->rewindIfNeeded()) { |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 198 | return SkCodec::kCouldNotRewind; |
| 199 | } |
| 200 | if (options.fSubset) { |
| 201 | // Subsets are not supported. |
| 202 | return SkCodec::kUnimplemented; |
| 203 | } |
| 204 | if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
emmaleer | 0944100 | 2015-08-13 11:26:57 -0700 | [diff] [blame^] | 205 | if (!SkScaledCodec::DimensionsSupportedForSampling(this->getInfo(), dstInfo)) { |
| 206 | return SkCodec::kInvalidScale; |
| 207 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 208 | } |
| 209 | |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 210 | if (!valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType())) { |
| 211 | return SkCodec::kInvalidConversion; |
| 212 | } |
| 213 | |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 214 | // Fill in the color table |
| 215 | setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount); |
| 216 | |
| 217 | // Copy the color table to a pointer that can be owned by the scanline decoder |
| 218 | if (kIndex_8_SkColorType == dstInfo.colorType()) { |
| 219 | fColorTable.reset(SkNEW_ARGS(SkColorTable, (inputColorTable, 2))); |
| 220 | } |
| 221 | |
| 222 | // Initialize the swizzler |
| 223 | fSwizzler.reset(fCodec->initializeSwizzler(dstInfo, |
| 224 | get_color_ptr(fColorTable.get()), options)); |
| 225 | if (NULL == fSwizzler.get()) { |
emmaleer | 0944100 | 2015-08-13 11:26:57 -0700 | [diff] [blame^] | 226 | return SkCodec::kInvalidConversion; |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | return SkCodec::kSuccess; |
| 230 | } |
| 231 | |
emmaleer | 0944100 | 2015-08-13 11:26:57 -0700 | [diff] [blame^] | 232 | SkEncodedFormat onGetEncodedFormat() const { |
| 233 | return kWBMP_SkEncodedFormat; |
| 234 | } |
| 235 | |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 236 | private: |
| 237 | SkAutoTDelete<SkWbmpCodec> fCodec; |
| 238 | SkAutoTUnref<SkColorTable> fColorTable; |
| 239 | SkAutoTDelete<SkSwizzler> fSwizzler; |
| 240 | SkAutoTMalloc<uint8_t> fSrcBuffer; |
| 241 | |
| 242 | typedef SkScanlineDecoder INHERITED; |
| 243 | }; |
| 244 | |
| 245 | SkScanlineDecoder* SkWbmpCodec::NewSDFromStream(SkStream* stream) { |
| 246 | SkAutoTDelete<SkWbmpCodec> codec(static_cast<SkWbmpCodec*>( |
| 247 | SkWbmpCodec::NewFromStream(stream))); |
| 248 | if (!codec) { |
| 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | // Return the new scanline decoder |
| 253 | return SkNEW_ARGS(SkWbmpScanlineDecoder, (codec.detach())); |
| 254 | } |