OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkColorPriv.h" | 8 #include "SkColorPriv.h" |
9 #include "SkImageDecoder.h" | 9 #include "SkImageDecoder.h" |
10 #include "SkPixelRef.h" | 10 #include "SkPixelRef.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 } | 60 } |
61 | 61 |
62 const unsigned short width = ktxFile.width(); | 62 const unsigned short width = ktxFile.width(); |
63 const unsigned short height = ktxFile.height(); | 63 const unsigned short height = ktxFile.height(); |
64 | 64 |
65 // should we allow the Chooser (if present) to pick a config for us??? | 65 // should we allow the Chooser (if present) to pick a config for us??? |
66 if (!this->chooseFromOneChoice(kN32_SkColorType, width, height)) { | 66 if (!this->chooseFromOneChoice(kN32_SkColorType, width, height)) { |
67 return false; | 67 return false; |
68 } | 68 } |
69 | 69 |
| 70 // Set a flag if our source is premultiplied alpha |
| 71 const SkString premulKey("KTXPremultipliedAlpha"); |
| 72 const bool bSrcIsPremul = ktxFile.getValueForKey(premulKey) == SkString("Tru
e"); |
| 73 |
70 // Setup the sampler... | 74 // Setup the sampler... |
71 SkScaledBitmapSampler sampler(width, height, this->getSampleSize()); | 75 SkScaledBitmapSampler sampler(width, height, this->getSampleSize()); |
72 | 76 |
| 77 // Determine the alpha of the bitmap... |
| 78 SkAlphaType alphaType = kOpaque_SkAlphaType; |
| 79 if (ktxFile.isRGBA8()) { |
| 80 if (this->getRequireUnpremultipliedColors()) { |
| 81 alphaType = kUnpremul_SkAlphaType; |
| 82 // If the client wants unpremul colors and we only have |
| 83 // premul, then we cannot honor their wish. |
| 84 if (bSrcIsPremul) { |
| 85 return false; |
| 86 } |
| 87 } else { |
| 88 alphaType = kPremul_SkAlphaType; |
| 89 } |
| 90 } |
| 91 |
73 // Set the config... | 92 // Set the config... |
74 bm->setInfo(SkImageInfo::MakeN32(sampler.scaledWidth(), sampler.scaledHeight
(), | 93 bm->setInfo(SkImageInfo::MakeN32(sampler.scaledWidth(), sampler.scaledHeight
(), alphaType)); |
75 ktxFile.isRGBA8()? kPremul_SkAlphaType : kO
paque_SkAlphaType)); | |
76 if (SkImageDecoder::kDecodeBounds_Mode == mode) { | 94 if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
77 return true; | 95 return true; |
78 } | 96 } |
79 | 97 |
80 // If we've made it this far, then we know how to grok the data. | 98 // If we've made it this far, then we know how to grok the data. |
81 if (!this->allocPixelRef(bm, NULL)) { | 99 if (!this->allocPixelRef(bm, NULL)) { |
82 return false; | 100 return false; |
83 } | 101 } |
84 | 102 |
85 // Lock the pixels, since we're about to write to them... | 103 // Lock the pixels, since we're about to write to them... |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 srcRow += sampler.srcY0() * srcRowBytes; | 145 srcRow += sampler.srcY0() * srcRowBytes; |
128 for (int y = 0; y < dstHeight; ++y) { | 146 for (int y = 0; y < dstHeight; ++y) { |
129 sampler.next(srcRow); | 147 sampler.next(srcRow); |
130 srcRow += sampler.srcDY() * srcRowBytes; | 148 srcRow += sampler.srcDY() * srcRowBytes; |
131 } | 149 } |
132 | 150 |
133 return true; | 151 return true; |
134 | 152 |
135 } else if (ktxFile.isRGBA8()) { | 153 } else if (ktxFile.isRGBA8()) { |
136 | 154 |
| 155 // Uncompressed RGBA data |
| 156 |
137 // If we know that the image contains premultiplied alpha, then | 157 // If we know that the image contains premultiplied alpha, then |
138 // don't premultiply it upon decoding. | 158 // we need to turn off the premultiplier |
139 bool setRequireUnpremul = false; | 159 SkScaledBitmapSampler::Options opts (*this); |
140 const SkString premulKey("KTXPremultipliedAlpha"); | 160 if (bSrcIsPremul) { |
141 if (ktxFile.getValueForKey(premulKey) == SkString("True")) { | 161 SkASSERT(bm->alphaType() == kPremul_SkAlphaType); |
142 this->setRequireUnpremultipliedColors(true); | 162 SkASSERT(!this->getRequireUnpremultipliedColors()); |
143 setRequireUnpremul = true; | |
144 } | |
145 | 163 |
146 // Uncompressed RGBA data | 164 opts.fPremultiplyAlpha = false; |
147 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGBA, *this)) { | 165 } |
| 166 |
| 167 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGBA, opts)) { |
148 return false; | 168 return false; |
149 } | 169 } |
150 | 170 |
151 // Just need to read RGBA pixels | 171 // Just need to read RGBA pixels |
152 const int srcRowBytes = width * 4; | 172 const int srcRowBytes = width * 4; |
153 const int dstHeight = sampler.scaledHeight(); | 173 const int dstHeight = sampler.scaledHeight(); |
154 const uint8_t *srcRow = reinterpret_cast<const uint8_t *>(ktxFile.pixelD
ata()); | 174 const uint8_t *srcRow = reinterpret_cast<const uint8_t *>(ktxFile.pixelD
ata()); |
155 srcRow += sampler.srcY0() * srcRowBytes; | 175 srcRow += sampler.srcY0() * srcRowBytes; |
156 for (int y = 0; y < dstHeight; ++y) { | 176 for (int y = 0; y < dstHeight; ++y) { |
157 sampler.next(srcRow); | 177 sampler.next(srcRow); |
158 srcRow += sampler.srcDY() * srcRowBytes; | 178 srcRow += sampler.srcDY() * srcRowBytes; |
159 } | 179 } |
160 | 180 |
161 // Reset this in case the decoder needs to be used again. | |
162 if (setRequireUnpremul) { | |
163 this->setRequireUnpremultipliedColors(false); | |
164 } | |
165 | |
166 return true; | 181 return true; |
167 } | 182 } |
168 | 183 |
169 return false; | 184 return false; |
170 } | 185 } |
171 | 186 |
172 /////////////////////////////////////////////////////////////////////////////// | 187 /////////////////////////////////////////////////////////////////////////////// |
173 | 188 |
174 // KTX Image Encoder | 189 // KTX Image Encoder |
175 // | 190 // |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 return SkImageDecoder::kUnknown_Format; | 266 return SkImageDecoder::kUnknown_Format; |
252 } | 267 } |
253 | 268 |
254 SkImageEncoder* sk_libktx_efactory(SkImageEncoder::Type t) { | 269 SkImageEncoder* sk_libktx_efactory(SkImageEncoder::Type t) { |
255 return (SkImageEncoder::kKTX_Type == t) ? SkNEW(SkKTXImageEncoder) : NULL; | 270 return (SkImageEncoder::kKTX_Type == t) ? SkNEW(SkKTXImageEncoder) : NULL; |
256 } | 271 } |
257 | 272 |
258 static SkImageDecoder_DecodeReg gReg(sk_libktx_dfactory); | 273 static SkImageDecoder_DecodeReg gReg(sk_libktx_dfactory); |
259 static SkImageDecoder_FormatReg gFormatReg(get_format_ktx); | 274 static SkImageDecoder_FormatReg gFormatReg(get_format_ktx); |
260 static SkImageEncoder_EncodeReg gEReg(sk_libktx_efactory); | 275 static SkImageEncoder_EncodeReg gEReg(sk_libktx_efactory); |
OLD | NEW |