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