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 | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 21 | {} |
| 22 | |
| 23 | /* |
| 24 | * Initiates the bitmap decode |
| 25 | */ |
| 26 | SkCodec::Result SkBmpMaskCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 27 | void* dst, size_t dstRowBytes, |
| 28 | const Options& opts, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 29 | int* rowsDecoded) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 30 | if (opts.fSubset) { |
| 31 | // Subsets are not supported. |
| 32 | return kUnimplemented; |
| 33 | } |
| 34 | if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| 35 | SkCodecPrintf("Error: scaling not supported.\n"); |
| 36 | return kInvalidScale; |
| 37 | } |
| 38 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame^] | 39 | Result result = this->prepareToDecode(dstInfo, opts); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 40 | if (kSuccess != result) { |
| 41 | return result; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 42 | } |
| 43 | |
msarett | f724b99 | 2015-10-15 06:41:06 -0700 | [diff] [blame] | 44 | int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 45 | if (rows != dstInfo.height()) { |
| 46 | *rowsDecoded = rows; |
| 47 | return kIncompleteInput; |
| 48 | } |
| 49 | return kSuccess; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 52 | SkCodec::Result SkBmpMaskCodec::onPrepareToDecode(const SkImageInfo& dstInfo, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame^] | 53 | const SkCodec::Options& options) { |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 54 | if (this->colorXform()) { |
| 55 | this->resetXformBuffer(dstInfo.width()); |
| 56 | } |
| 57 | |
| 58 | SkImageInfo swizzlerInfo = dstInfo; |
| 59 | if (this->colorXform()) { |
Matt Sarett | 562e681 | 2016-11-08 16:13:43 -0500 | [diff] [blame] | 60 | swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType); |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 61 | if (kPremul_SkAlphaType == dstInfo.alphaType()) { |
| 62 | swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType); |
| 63 | } |
| 64 | } |
| 65 | |
msarett | b30d698 | 2016-02-15 10:18:45 -0800 | [diff] [blame] | 66 | // Initialize the mask swizzler |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 67 | fMaskSwizzler.reset(SkMaskSwizzler::CreateMaskSwizzler(swizzlerInfo, this->getInfo(), |
| 68 | fMasks.get(), this->bitsPerPixel(), options)); |
msarett | b30d698 | 2016-02-15 10:18:45 -0800 | [diff] [blame] | 69 | SkASSERT(fMaskSwizzler); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 70 | |
| 71 | return SkCodec::kSuccess; |
| 72 | } |
| 73 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 74 | /* |
| 75 | * Performs the decoding |
| 76 | */ |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 77 | int SkBmpMaskCodec::decodeRows(const SkImageInfo& dstInfo, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 78 | void* dst, size_t dstRowBytes, |
| 79 | const Options& opts) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 80 | // Iterate over rows of the image |
Leon Scroggins III | d81fed9 | 2017-06-01 13:42:28 -0400 | [diff] [blame] | 81 | uint8_t* srcRow = this->srcBuffer(); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 82 | const int height = dstInfo.height(); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 83 | for (int y = 0; y < height; y++) { |
| 84 | // Read a row of the input |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 85 | if (this->stream()->read(srcRow, this->srcRowBytes()) != this->srcRowBytes()) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 86 | SkCodecPrintf("Warning: incomplete input stream.\n"); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 87 | return y; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // Decode the row in destination format |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 91 | uint32_t row = this->getDstRow(y, height); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 92 | void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes); |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 93 | |
| 94 | if (this->colorXform()) { |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 95 | fMaskSwizzler->swizzle(this->xformBuffer(), srcRow); |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 96 | this->applyColorXform(dstRow, this->xformBuffer(), fMaskSwizzler->swizzleWidth()); |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 97 | } else { |
| 98 | fMaskSwizzler->swizzle(dstRow, srcRow); |
| 99 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Finished decoding the entire image |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 103 | return height; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 104 | } |