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