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 return false; |
| 26 } |
| 27 |
| 28 // Check for supported color types |
| 29 switch (dst.colorType()) { |
| 30 // Allow output to kN32 |
| 31 case kN32_SkColorType: |
| 32 return true; |
| 33 default: |
| 34 return false; |
| 35 } |
| 36 } |
| 37 |
| 38 |
| 39 /* |
| 40 * Creates an instance of the decoder |
| 41 */ |
| 42 SkBmpMaskCodec::SkBmpMaskCodec(const SkImageInfo& info, SkStream* stream, |
| 43 uint16_t bitsPerPixel, SkMasks* masks, |
| 44 SkBmpCodec::RowOrder rowOrder) |
| 45 : INHERITED(info, stream, bitsPerPixel, rowOrder) |
| 46 , fMasks(masks) |
| 47 , fMaskSwizzler(NULL) |
| 48 , fSrcBuffer(NULL) |
| 49 {} |
| 50 |
| 51 /* |
| 52 * Initiates the bitmap decode |
| 53 */ |
| 54 SkCodec::Result SkBmpMaskCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 55 void* dst, size_t dstRowBytes, |
| 56 const Options& opts, |
| 57 SkPMColor* inputColorPtr, |
| 58 int* inputColorCount) { |
| 59 if (!this->handleRewind(false)) { |
| 60 return kCouldNotRewind; |
| 61 } |
| 62 if (opts.fSubset) { |
| 63 // Subsets are not supported. |
| 64 return kUnimplemented; |
| 65 } |
| 66 if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| 67 SkCodecPrintf("Error: scaling not supported.\n"); |
| 68 return kInvalidScale; |
| 69 } |
| 70 |
| 71 if (!conversion_possible(dstInfo, this->getInfo())) { |
| 72 SkCodecPrintf("Error: cannot convert input type to output type.\n"); |
| 73 return kInvalidConversion; |
| 74 } |
| 75 |
| 76 // Initialize a the mask swizzler |
| 77 if (!this->initializeSwizzler(dstInfo)) { |
| 78 SkCodecPrintf("Error: cannot initialize swizzler.\n"); |
| 79 return kInvalidConversion; |
| 80 } |
| 81 |
| 82 return this->decode(dstInfo, dst, dstRowBytes, opts); |
| 83 } |
| 84 |
| 85 bool SkBmpMaskCodec::initializeSwizzler(const SkImageInfo& dstInfo) { |
| 86 // Allocate space for a row buffer |
| 87 const size_t rowBytes = SkAlign4(compute_row_bytes(dstInfo.width(), this->bi
tsPerPixel())); |
| 88 fSrcBuffer.reset(SkNEW_ARRAY(uint8_t, rowBytes)); |
| 89 |
| 90 // Create the swizzler |
| 91 fMaskSwizzler.reset(SkMaskSwizzler::CreateMaskSwizzler( |
| 92 dstInfo, fMasks, this->bitsPerPixel())); |
| 93 |
| 94 if (NULL == fMaskSwizzler.get()) { |
| 95 return false; |
| 96 } |
| 97 |
| 98 return true; |
| 99 } |
| 100 |
| 101 /* |
| 102 * Performs the decoding |
| 103 */ |
| 104 SkCodec::Result SkBmpMaskCodec::decode(const SkImageInfo& dstInfo, |
| 105 void* dst, size_t dstRowBytes, |
| 106 const Options& opts) { |
| 107 // Set constant values |
| 108 const int width = dstInfo.width(); |
| 109 const int height = dstInfo.height(); |
| 110 const size_t rowBytes = SkAlign4(compute_row_bytes(width, this->bitsPerPixel
())); |
| 111 |
| 112 // Iterate over rows of the image |
| 113 uint8_t* srcRow = fSrcBuffer.get(); |
| 114 for (int y = 0; y < height; y++) { |
| 115 // Read a row of the input |
| 116 if (this->stream()->read(srcRow, rowBytes) != rowBytes) { |
| 117 SkCodecPrintf("Warning: incomplete input stream.\n"); |
| 118 // Fill the destination image on failure |
| 119 SkPMColor fillColor = dstInfo.alphaType() == kOpaque_SkAlphaType ? |
| 120 SK_ColorBLACK : SK_ColorTRANSPARENT; |
| 121 if (kNo_ZeroInitialized == opts.fZeroInitialized || 0 != fillColor)
{ |
| 122 void* dstStart = this->getDstStartRow(dst, dstRowBytes, y); |
| 123 SkSwizzler::Fill(dstStart, dstInfo, dstRowBytes, dstInfo.height(
) - y, fillColor, |
| 124 NULL); |
| 125 } |
| 126 return kIncompleteInput; |
| 127 } |
| 128 |
| 129 // Decode the row in destination format |
| 130 int row = SkBmpCodec::kBottomUp_RowOrder == this->rowOrder() ? height -
1 - y : y; |
| 131 void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes); |
| 132 fMaskSwizzler->swizzle(dstRow, srcRow); |
| 133 } |
| 134 |
| 135 // Finished decoding the entire image |
| 136 return kSuccess; |
| 137 } |
OLD | NEW |