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 // Perform the decode | |
83 SkCodec::Result result = decode(dstInfo, dst, dstRowBytes, opts); | |
scroggo
2015/08/06 21:12:41
nit: This can be simplified (also it should have a
msarett
2015/08/06 21:41:42
Done.
| |
84 | |
85 return result; | |
86 } | |
87 | |
88 bool SkBmpMaskCodec::initializeSwizzler(const SkImageInfo& dstInfo) { | |
89 // Allocate space for a row buffer | |
90 const size_t rowBytes = SkAlign4(compute_row_bytes(dstInfo.width(), this->bi tsPerPixel())); | |
91 fSrcBuffer.reset(SkNEW_ARRAY(uint8_t, rowBytes)); | |
92 | |
93 // Create the swizzler | |
94 fMaskSwizzler.reset(SkMaskSwizzler::CreateMaskSwizzler( | |
95 dstInfo, fMasks, this->bitsPerPixel())); | |
96 | |
97 if (NULL == fMaskSwizzler.get()) { | |
98 return false; | |
99 } | |
100 | |
101 return true; | |
102 } | |
103 | |
104 /* | |
105 * Performs the decoding | |
106 */ | |
107 SkCodec::Result SkBmpMaskCodec::decode(const SkImageInfo& dstInfo, | |
108 void* dst, size_t dstRowBytes, | |
109 const Options& opts) { | |
110 // Set constant values | |
111 const int width = dstInfo.width(); | |
112 const int height = dstInfo.height(); | |
113 const size_t rowBytes = SkAlign4(compute_row_bytes(width, this->bitsPerPixel ())); | |
114 | |
115 // Iterate over rows of the image | |
116 uint8_t* srcRow = fSrcBuffer.get(); | |
117 for (int y = 0; y < height; y++) { | |
118 // Read a row of the input | |
119 if (this->stream()->read(srcRow, rowBytes) != rowBytes) { | |
scroggo
2015/08/06 21:12:41
Doesn't need to be a part of this patch set, but I
msarett
2015/08/06 21:41:42
Acknowledged.
| |
120 SkCodecPrintf("Warning: incomplete input stream.\n"); | |
121 // Fill the destination image on failure | |
122 SkPMColor fillColor = dstInfo.alphaType() == kOpaque_SkAlphaType ? | |
123 SK_ColorBLACK : SK_ColorTRANSPARENT; | |
124 if (kNo_ZeroInitialized == opts.fZeroInitialized || 0 != fillColor) { | |
125 void* dstStart = this->getDstStartRow(dst, dstRowBytes, y); | |
126 SkSwizzler::Fill(dstStart, dstInfo, dstRowBytes, dstInfo.height( ) - y, fillColor, | |
127 NULL); | |
128 } | |
129 return kIncompleteInput; | |
130 } | |
131 | |
132 // Decode the row in destination format | |
133 int row = SkBmpCodec::kBottomUp_RowOrder == this->rowOrder() ? height - 1 - y : y; | |
134 void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes); | |
135 fMaskSwizzler->swizzle(dstRow, srcRow); | |
136 } | |
137 | |
138 // Finished decoding the entire image | |
139 return kSuccess; | |
140 } | |
OLD | NEW |