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