Index: src/codec/SkBmpMaskCodec.cpp |
diff --git a/src/codec/SkBmpMaskCodec.cpp b/src/codec/SkBmpMaskCodec.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ab4f4a33d31873966049b85e33383d11d9968dd7 |
--- /dev/null |
+++ b/src/codec/SkBmpMaskCodec.cpp |
@@ -0,0 +1,166 @@ |
+/* |
+ * Copyright 2015 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include "SkBmpMaskCodec.h" |
+#include "SkCodecPriv.h" |
+#include "SkColorPriv.h" |
+#include "SkScanlineDecoder.h" |
scroggo
2015/07/31 15:05:43
This is not needed, right?
msarett
2015/08/03 22:52:35
Done.
|
+#include "SkStream.h" |
+ |
+/* |
+ * Checks if the conversion between the input image and the requested output |
+ * image has been implemented |
+ */ |
+static bool conversion_possible(const SkImageInfo& dst, |
+ const SkImageInfo& src) { |
+ // Ensure that the profile type is unchanged |
+ if (dst.profileType() != src.profileType()) { |
+ return false; |
+ } |
+ |
+ // Ensure the alpha type is valid |
+ if (!valid_alpha(dst.alphaType(), src.alphaType())) { |
+ // In order to correct invalid transparent decodes, we will always |
+ // allow dst alpha type to be opaque. |
+ if (kOpaque_SkAlphaType != dst.alphaType()) { |
+ return false; |
+ } |
+ SkCodecPrintf("Warning: The client should not request an opaque " |
+ "decode if we indicate that the encoded data is not opaque. " |
+ "We should only hit this case on the automatic correction " |
+ "of invalid transparent decodes.\n"); |
+ } |
+ |
+ // Check for supported color types |
+ switch (dst.colorType()) { |
+ // Allow output to kN32 |
+ case kN32_SkColorType: |
+ return true; |
+ default: |
+ return false; |
+ } |
+} |
+ |
+ |
+/* |
+ * Creates an instance of the decoder |
+ */ |
+SkBmpMaskCodec::SkBmpMaskCodec(const SkImageInfo& info, SkStream* stream, |
+ uint16_t bitsPerPixel, SkMasks* masks, |
+ SkBmpCodec::RowOrder rowOrder) |
+ : INHERITED(info, stream) |
+ , fBitsPerPixel(bitsPerPixel) |
+ , fMasks(masks) |
+ , fRowOrder(rowOrder) |
+ , fMaskSwizzler(NULL) |
+ , fSrcBuffer(NULL) |
+ , fIsTransparent(true) |
+{} |
+ |
+/* |
+ * Initiates the bitmap decode |
+ */ |
+SkCodec::Result SkBmpMaskCodec::onGetPixels(const SkImageInfo& dstInfo, |
+ void* dst, size_t dstRowBytes, |
+ const Options& opts, |
+ SkPMColor* inputColorPtr, |
+ int* inputColorCount) { |
+ // Check for proper input and output formats |
+ SkCodec::RewindState rewindState = this->rewindIfNeeded(); |
+ if (rewindState == kCouldNotRewind_RewindState) { |
+ return kCouldNotRewind; |
+ } else if (rewindState == kRewound_RewindState) { |
+ if (!SkBmpCodec::ReadHeader(this->stream(), false, NULL)) { |
+ return kCouldNotRewind; |
+ } |
+ } |
+ if (opts.fSubset) { |
+ // Subsets are not supported. |
+ return kUnimplemented; |
+ } |
+ if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
+ SkCodecPrintf("Error: scaling not supported.\n"); |
+ return kInvalidScale; |
+ } |
+ |
+ if (!conversion_possible(dstInfo, this->getInfo())) { |
+ SkCodecPrintf("Error: cannot convert input type to output type.\n"); |
+ return kInvalidConversion; |
+ } |
+ |
+ // Initialize a the mask swizzler |
+ if (!this->initializeSwizzler(dstInfo)) { |
+ SkCodecPrintf("Error: cannot initialize swizzler.\n"); |
+ return kInvalidConversion; |
+ } |
+ |
+ // Perform the decode |
+ SkCodec::Result result = decode(dstInfo, dst, dstRowBytes, opts); |
+ |
+ // Fix the decode, if neceessary |
+ if (fIsTransparent) { |
+ result = SkBmpCodec::FixTransparentDecode(dst, dstRowBytes, |
+ inputColorPtr, inputColorCount, this->stream()->duplicate()); |
+ } |
+ |
+ return result; |
+} |
+ |
+bool SkBmpMaskCodec::initializeSwizzler(const SkImageInfo& dstInfo) { |
+ // Allocate space for a row buffer |
+ const size_t rowBytes = SkAlign4(compute_row_bytes(dstInfo.width(), fBitsPerPixel)); |
+ fSrcBuffer.reset(SkNEW_ARRAY(uint8_t, rowBytes)); |
+ |
+ // Create the swizzler |
+ fMaskSwizzler.reset(SkMaskSwizzler::CreateMaskSwizzler( |
+ dstInfo, fMasks, fBitsPerPixel)); |
+ |
+ if (NULL == fMaskSwizzler.get()) { |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+/* |
+ * Performs the decoding |
+ */ |
+SkCodec::Result SkBmpMaskCodec::decode(const SkImageInfo& dstInfo, |
+ void* dst, size_t dstRowBytes, |
+ const Options& opts) { |
+ // Set constant values |
+ const int width = dstInfo.width(); |
+ const int height = dstInfo.height(); |
+ const size_t rowBytes = SkAlign4(compute_row_bytes(width, fBitsPerPixel)); |
+ |
+ // Iterate over rows of the image |
+ uint8_t* srcRow = fSrcBuffer.get(); |
+ for (int y = 0; y < height; y++) { |
+ // Read a row of the input |
+ if (this->stream()->read(srcRow, rowBytes) != rowBytes) { |
+ SkCodecPrintf("Warning: incomplete input stream.\n"); |
+ // Fill the destination image on failure |
+ SkPMColor fillColor = dstInfo.alphaType() == kOpaque_SkAlphaType ? |
+ SK_ColorBLACK : SK_ColorTRANSPARENT; |
+ if (kNo_ZeroInitialized == opts.fZeroInitialized || 0 != fillColor) { |
+ void* dstStart = get_dst_start_row(dst, dstRowBytes, y, fRowOrder); |
+ SkSwizzler::Fill(dstStart, dstInfo, dstRowBytes, dstInfo.height() - y, fillColor, |
+ NULL); |
+ } |
+ return kIncompleteInput; |
+ } |
+ |
+ // Decode the row in destination format |
+ int row = SkBmpCodec::kBottomUp_RowOrder == fRowOrder ? height - 1 - y : y; |
+ void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes); |
+ SkSwizzler::ResultAlpha r = fMaskSwizzler->swizzle(dstRow, srcRow); |
+ fIsTransparent &= SkSwizzler::IsTransparent(r); |
+ } |
+ |
+ // Finished decoding the entire image |
+ return kSuccess; |
+} |