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 | */ |
| 15 | SkBmpMaskCodec::SkBmpMaskCodec(const SkImageInfo& info, SkStream* stream, |
| 16 | uint16_t bitsPerPixel, SkMasks* masks, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame^] | 17 | SkScanlineDecoder::SkScanlineOrder rowOrder) |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 18 | : INHERITED(info, stream, bitsPerPixel, rowOrder) |
| 19 | , fMasks(masks) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 20 | , fMaskSwizzler(nullptr) |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame^] | 21 | , fSrcRowBytes(SkAlign4(compute_row_bytes(this->getInfo().width(), this->bitsPerPixel()))) |
| 22 | , fSrcBuffer(new uint8_t [fSrcRowBytes]) |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 23 | {} |
| 24 | |
| 25 | /* |
| 26 | * Initiates the bitmap decode |
| 27 | */ |
| 28 | SkCodec::Result SkBmpMaskCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 29 | void* dst, size_t dstRowBytes, |
| 30 | const Options& opts, |
| 31 | SkPMColor* inputColorPtr, |
| 32 | int* inputColorCount) { |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 33 | if (!this->rewindIfNeeded()) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 34 | return kCouldNotRewind; |
| 35 | } |
| 36 | if (opts.fSubset) { |
| 37 | // Subsets are not supported. |
| 38 | return kUnimplemented; |
| 39 | } |
| 40 | if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| 41 | SkCodecPrintf("Error: scaling not supported.\n"); |
| 42 | return kInvalidScale; |
| 43 | } |
| 44 | |
| 45 | if (!conversion_possible(dstInfo, this->getInfo())) { |
| 46 | SkCodecPrintf("Error: cannot convert input type to output type.\n"); |
| 47 | return kInvalidConversion; |
| 48 | } |
| 49 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame^] | 50 | Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount); |
| 51 | if (kSuccess != result) { |
| 52 | return result; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 53 | } |
| 54 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame^] | 55 | return this->decodeRows(dstInfo, dst, dstRowBytes, opts); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | bool SkBmpMaskCodec::initializeSwizzler(const SkImageInfo& dstInfo) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 59 | // Create the swizzler |
| 60 | fMaskSwizzler.reset(SkMaskSwizzler::CreateMaskSwizzler( |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame^] | 61 | dstInfo, this->getInfo(), fMasks, this->bitsPerPixel())); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 62 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 63 | if (nullptr == fMaskSwizzler.get()) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 64 | return false; |
| 65 | } |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame^] | 70 | SkCodec::Result SkBmpMaskCodec::prepareToDecode(const SkImageInfo& dstInfo, |
| 71 | const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) { |
| 72 | // Initialize a the mask swizzler |
| 73 | if (!this->initializeSwizzler(dstInfo)) { |
| 74 | SkCodecPrintf("Error: cannot initialize swizzler.\n"); |
| 75 | return SkCodec::kInvalidConversion; |
| 76 | } |
| 77 | |
| 78 | return SkCodec::kSuccess; |
| 79 | } |
| 80 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 81 | /* |
| 82 | * Performs the decoding |
| 83 | */ |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame^] | 84 | SkCodec::Result SkBmpMaskCodec::decodeRows(const SkImageInfo& dstInfo, |
| 85 | void* dst, size_t dstRowBytes, |
| 86 | const Options& opts) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 87 | // Iterate over rows of the image |
| 88 | uint8_t* srcRow = fSrcBuffer.get(); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame^] | 89 | const int height = dstInfo.height(); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 90 | for (int y = 0; y < height; y++) { |
| 91 | // Read a row of the input |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame^] | 92 | if (this->stream()->read(srcRow, fSrcRowBytes) != fSrcRowBytes) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 93 | SkCodecPrintf("Warning: incomplete input stream.\n"); |
| 94 | // Fill the destination image on failure |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame^] | 95 | void* dstStart = this->getDstStartRow(dst, dstRowBytes, y); |
| 96 | uint32_t fillColor = get_fill_color_or_index(dstInfo.alphaType()); |
| 97 | SkSwizzler::Fill(dstStart, dstInfo, dstRowBytes, height - y, |
| 98 | fillColor, nullptr, opts.fZeroInitialized); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 99 | return kIncompleteInput; |
| 100 | } |
| 101 | |
| 102 | // Decode the row in destination format |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame^] | 103 | uint32_t row = this->getDstRow(y, height); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 104 | void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes); |
| 105 | fMaskSwizzler->swizzle(dstRow, srcRow); |
| 106 | } |
| 107 | |
| 108 | // Finished decoding the entire image |
| 109 | return kSuccess; |
| 110 | } |