2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "include/private/SkColorData.h"
9 #include "src/codec/SkBmpMaskCodec.h"
10 #include "src/codec/SkCodecPriv.h"
13 * Creates an instance of the decoder
15 SkBmpMaskCodec::SkBmpMaskCodec(SkEncodedInfo&& info,
16 std::unique_ptr<SkStream> stream,
17 uint16_t bitsPerPixel, SkMasks* masks,
18 SkCodec::SkScanlineOrder rowOrder)
19 : INHERITED(std::move(info), std::move(stream), bitsPerPixel, rowOrder)
21 , fMaskSwizzler(nullptr)
25 * Initiates the bitmap decode
27 SkCodec::Result SkBmpMaskCodec::onGetPixels(const SkImageInfo& dstInfo,
28 void* dst, size_t dstRowBytes,
32 // Subsets are not supported.
33 return kUnimplemented;
35 if (dstInfo.dimensions() != this->dimensions()) {
36 SkCodecPrintf("Error: scaling not supported.\n");
40 Result result = this->prepareToDecode(dstInfo, opts);
41 if (kSuccess != result) {
45 int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts);
46 if (rows != dstInfo.height()) {
48 return kIncompleteInput;
53 SkCodec::Result SkBmpMaskCodec::onPrepareToDecode(const SkImageInfo& dstInfo,
54 const SkCodec::Options& options) {
55 if (this->colorXform()) {
56 this->resetXformBuffer(dstInfo.width());
59 SkImageInfo swizzlerInfo = dstInfo;
60 if (this->colorXform()) {
61 swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
62 if (kPremul_SkAlphaType == dstInfo.alphaType()) {
63 swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
67 bool srcIsOpaque = this->getEncodedInfo().opaque();
68 fMaskSwizzler.reset(SkMaskSwizzler::CreateMaskSwizzler(swizzlerInfo, srcIsOpaque,
69 fMasks.get(), this->bitsPerPixel(), options));
70 SkASSERT(fMaskSwizzler);
72 return SkCodec::kSuccess;
76 * Performs the decoding
78 int SkBmpMaskCodec::decodeRows(const SkImageInfo& dstInfo,
79 void* dst, size_t dstRowBytes,
80 const Options& opts) {
81 // Iterate over rows of the image
82 uint8_t* srcRow = this->srcBuffer();
83 const int height = dstInfo.height();
84 for (int y = 0; y < height; y++) {
85 // Read a row of the input
86 if (this->stream()->read(srcRow, this->srcRowBytes()) != this->srcRowBytes()) {
87 SkCodecPrintf("Warning: incomplete input stream.\n");
91 // Decode the row in destination format
92 uint32_t row = this->getDstRow(y, height);
93 void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes);
95 if (this->colorXform()) {
96 fMaskSwizzler->swizzle(this->xformBuffer(), srcRow);
97 this->applyColorXform(dstRow, this->xformBuffer(), fMaskSwizzler->swizzleWidth());
99 fMaskSwizzler->swizzle(dstRow, srcRow);
103 // Finished decoding the entire image