OLD | NEW |
(Empty) | |
| 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 /* |
| 13 * Checks if the conversion between the input image and the requested output |
| 14 * image has been implemented |
| 15 */ |
| 16 static bool conversion_possible(const SkImageInfo& dst, |
| 17 const SkImageInfo& src) { |
| 18 // Ensure that the profile type is unchanged |
| 19 if (dst.profileType() != src.profileType()) { |
| 20 return false; |
| 21 } |
| 22 |
| 23 // Ensure the alpha type is valid |
| 24 if (!valid_alpha(dst.alphaType(), src.alphaType())) { |
| 25 // In order to correct invalid transparent decodes, we will always |
| 26 // allow dst alpha type to be opaque. |
| 27 if (kOpaque_SkAlphaType != dst.alphaType()) { |
| 28 return false; |
| 29 } |
| 30 SkCodecPrintf("Warning: The client should not request an opaque " |
| 31 "decode if we indicate that the encoded data is not opaque. " |
| 32 "We should only hit this case on the automatic correction " |
| 33 "of invalid transparent decodes.\n"); |
| 34 } |
| 35 |
| 36 // Check for supported color types |
| 37 switch (dst.colorType()) { |
| 38 // Allow output to kN32 |
| 39 case kN32_SkColorType: |
| 40 return true; |
| 41 default: |
| 42 return false; |
| 43 } |
| 44 } |
| 45 |
| 46 |
| 47 /* |
| 48 * Creates an instance of the decoder |
| 49 */ |
| 50 SkBmpMaskCodec::SkBmpMaskCodec(const SkImageInfo& info, SkStream* stream, |
| 51 uint16_t bitsPerPixel, SkMasks* masks, |
| 52 SkBmpCodec::RowOrder rowOrder) |
| 53 : INHERITED(info, stream, bitsPerPixel, rowOrder) |
| 54 , fMasks(masks) |
| 55 , fMaskSwizzler(NULL) |
| 56 , fSrcBuffer(NULL) |
| 57 , fIsTransparent(true) |
| 58 {} |
| 59 |
| 60 /* |
| 61 * Initiates the bitmap decode |
| 62 */ |
| 63 SkCodec::Result SkBmpMaskCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 64 void* dst, size_t dstRowBytes, |
| 65 const Options& opts, |
| 66 SkPMColor* inputColorPtr, |
| 67 int* inputColorCount) { |
| 68 if (!this->handleRewind(false)) { |
| 69 SkCodecPrintf("Error: could not rewind image stream.\n"); |
| 70 return kCouldNotRewind; |
| 71 } |
| 72 if (opts.fSubset) { |
| 73 // Subsets are not supported. |
| 74 return kUnimplemented; |
| 75 } |
| 76 if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| 77 SkCodecPrintf("Error: scaling not supported.\n"); |
| 78 return kInvalidScale; |
| 79 } |
| 80 |
| 81 if (!conversion_possible(dstInfo, this->getInfo())) { |
| 82 SkCodecPrintf("Error: cannot convert input type to output type.\n"); |
| 83 return kInvalidConversion; |
| 84 } |
| 85 |
| 86 // Initialize a the mask swizzler |
| 87 if (!this->initializeSwizzler(dstInfo)) { |
| 88 SkCodecPrintf("Error: cannot initialize swizzler.\n"); |
| 89 return kInvalidConversion; |
| 90 } |
| 91 |
| 92 // Perform the decode |
| 93 SkCodec::Result result = decode(dstInfo, dst, dstRowBytes, opts); |
| 94 |
| 95 // Fix the decode, if neceessary |
| 96 if (fIsTransparent) { |
| 97 result = SkBmpCodec::FixTransparentDecode(dst, dstRowBytes, |
| 98 inputColorPtr, inputColorCount, this->stream()->duplicate()); |
| 99 } |
| 100 |
| 101 return result; |
| 102 } |
| 103 |
| 104 bool SkBmpMaskCodec::initializeSwizzler(const SkImageInfo& dstInfo) { |
| 105 // Allocate space for a row buffer |
| 106 const size_t rowBytes = SkAlign4(compute_row_bytes(dstInfo.width(), this->bi
tsPerPixel())); |
| 107 fSrcBuffer.reset(SkNEW_ARRAY(uint8_t, rowBytes)); |
| 108 |
| 109 // Create the swizzler |
| 110 fMaskSwizzler.reset(SkMaskSwizzler::CreateMaskSwizzler( |
| 111 dstInfo, fMasks, this->bitsPerPixel())); |
| 112 |
| 113 if (NULL == fMaskSwizzler.get()) { |
| 114 return false; |
| 115 } |
| 116 |
| 117 return true; |
| 118 } |
| 119 |
| 120 /* |
| 121 * Performs the decoding |
| 122 */ |
| 123 SkCodec::Result SkBmpMaskCodec::decode(const SkImageInfo& dstInfo, |
| 124 void* dst, size_t dstRowBytes, |
| 125 const Options& opts) { |
| 126 // Set constant values |
| 127 const int width = dstInfo.width(); |
| 128 const int height = dstInfo.height(); |
| 129 const size_t rowBytes = SkAlign4(compute_row_bytes(width, this->bitsPerPixel
())); |
| 130 |
| 131 // Iterate over rows of the image |
| 132 uint8_t* srcRow = fSrcBuffer.get(); |
| 133 for (int y = 0; y < height; y++) { |
| 134 // Read a row of the input |
| 135 if (this->stream()->read(srcRow, rowBytes) != rowBytes) { |
| 136 SkCodecPrintf("Warning: incomplete input stream.\n"); |
| 137 // Fill the destination image on failure |
| 138 SkPMColor fillColor = dstInfo.alphaType() == kOpaque_SkAlphaType ? |
| 139 SK_ColorBLACK : SK_ColorTRANSPARENT; |
| 140 if (kNo_ZeroInitialized == opts.fZeroInitialized || 0 != fillColor)
{ |
| 141 void* dstStart = this->getDstStartRow(dst, dstRowBytes, y); |
| 142 SkSwizzler::Fill(dstStart, dstInfo, dstRowBytes, dstInfo.height(
) - y, fillColor, |
| 143 NULL); |
| 144 } |
| 145 return kIncompleteInput; |
| 146 } |
| 147 |
| 148 // Decode the row in destination format |
| 149 int row = SkBmpCodec::kBottomUp_RowOrder == this->rowOrder() ? height -
1 - y : y; |
| 150 void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes); |
| 151 SkSwizzler::ResultAlpha r = fMaskSwizzler->swizzle(dstRow, srcRow); |
| 152 fIsTransparent &= SkSwizzler::IsTransparent(r); |
| 153 } |
| 154 |
| 155 // Finished decoding the entire image |
| 156 return kSuccess; |
| 157 } |
OLD | NEW |