msarett | 4ab9d5f | 2015-08-06 15:34:42 -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 "SkBmpMaskCodec.h" |
| 9 | #include "SkCodecPriv.h" |
| 10 | #include "SkColorPriv.h" |
| 11 | |
| 12 | /* |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 13 | * Creates an instance of the decoder |
| 14 | */ |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 15 | SkBmpMaskCodec::SkBmpMaskCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream, |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 16 | uint16_t bitsPerPixel, SkMasks* masks, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 17 | SkCodec::SkScanlineOrder rowOrder) |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 18 | : INHERITED(width, height, info, stream, bitsPerPixel, rowOrder) |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 19 | , fMasks(masks) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 20 | , fMaskSwizzler(nullptr) |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 21 | , fSrcBuffer(new uint8_t [this->srcRowBytes()]) |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 22 | {} |
| 23 | |
| 24 | /* |
| 25 | * Initiates the bitmap decode |
| 26 | */ |
| 27 | SkCodec::Result SkBmpMaskCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 28 | void* dst, size_t dstRowBytes, |
| 29 | const Options& opts, |
| 30 | SkPMColor* inputColorPtr, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 31 | int* inputColorCount, |
| 32 | int* rowsDecoded) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 33 | if (opts.fSubset) { |
| 34 | // Subsets are not supported. |
| 35 | return kUnimplemented; |
| 36 | } |
| 37 | if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| 38 | SkCodecPrintf("Error: scaling not supported.\n"); |
| 39 | return kInvalidScale; |
| 40 | } |
| 41 | |
| 42 | if (!conversion_possible(dstInfo, this->getInfo())) { |
| 43 | SkCodecPrintf("Error: cannot convert input type to output type.\n"); |
| 44 | return kInvalidConversion; |
| 45 | } |
| 46 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 47 | Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount); |
| 48 | if (kSuccess != result) { |
| 49 | return result; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 50 | } |
| 51 | |
msarett | f724b99 | 2015-10-15 06:41:06 -0700 | [diff] [blame] | 52 | int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 53 | if (rows != dstInfo.height()) { |
| 54 | *rowsDecoded = rows; |
| 55 | return kIncompleteInput; |
| 56 | } |
| 57 | return kSuccess; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 58 | } |
| 59 | |
msarett | deabdb5 | 2016-02-12 15:00:10 -0800 | [diff] [blame] | 60 | SkCodec::Result SkBmpMaskCodec::prepareToDecode(const SkImageInfo& dstInfo, |
| 61 | const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) { |
msarett | b30d698 | 2016-02-15 10:18:45 -0800 | [diff] [blame] | 62 | // Initialize the mask swizzler |
| 63 | fMaskSwizzler.reset(SkMaskSwizzler::CreateMaskSwizzler(dstInfo, this->getInfo(), fMasks, |
| 64 | this->bitsPerPixel(), options)); |
| 65 | SkASSERT(fMaskSwizzler); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 66 | |
| 67 | return SkCodec::kSuccess; |
| 68 | } |
| 69 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 70 | /* |
| 71 | * Performs the decoding |
| 72 | */ |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 73 | int SkBmpMaskCodec::decodeRows(const SkImageInfo& dstInfo, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 74 | void* dst, size_t dstRowBytes, |
| 75 | const Options& opts) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 76 | // Iterate over rows of the image |
| 77 | uint8_t* srcRow = fSrcBuffer.get(); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 78 | const int height = dstInfo.height(); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 79 | for (int y = 0; y < height; y++) { |
| 80 | // Read a row of the input |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 81 | if (this->stream()->read(srcRow, this->srcRowBytes()) != this->srcRowBytes()) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 82 | SkCodecPrintf("Warning: incomplete input stream.\n"); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 83 | return y; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // Decode the row in destination format |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 87 | uint32_t row = this->getDstRow(y, height); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 88 | void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes); |
| 89 | fMaskSwizzler->swizzle(dstRow, srcRow); |
| 90 | } |
| 91 | |
| 92 | // Finished decoding the entire image |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 93 | return height; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 94 | } |