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(SkBitmap::kARGB_8888_Config, width, height)) { | 66 if (!this->chooseFromOneChoice(SkBitmap::kARGB_8888_Config, 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 bool bSrcIsPremul = ktxFile.getValueForKey(premulKey) == SkString("True"); | |
scroggo
2014/06/10 13:50:23
const
krajcevski
2014/06/10 14:47:29
Done.
| |
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. | |
scroggo
2014/06/10 13:50:23
FWIW, we *could* do a second pass and unpremultipl
krajcevski
2014/06/10 14:47:29
I agree, although I believe the use case for that
| |
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->setConfig(SkBitmap::kARGB_8888_Config, | 93 bm->setConfig(SkBitmap::kARGB_8888_Config, |
75 sampler.scaledWidth(), sampler.scaledHeight(), | 94 sampler.scaledWidth(), sampler.scaledHeight(), |
76 0, | 95 0, alphaType); |
77 ktxFile.isRGBA8()? kPremul_SkAlphaType : kOpaque_SkAlphaType); | |
78 if (SkImageDecoder::kDecodeBounds_Mode == mode) { | 96 if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
79 return true; | 97 return true; |
80 } | 98 } |
81 | 99 |
82 // If we've made it this far, then we know how to grok the data. | 100 // If we've made it this far, then we know how to grok the data. |
83 if (!this->allocPixelRef(bm, NULL)) { | 101 if (!this->allocPixelRef(bm, NULL)) { |
84 return false; | 102 return false; |
85 } | 103 } |
86 | 104 |
87 // Lock the pixels, since we're about to write to them... | 105 // Lock the pixels, since we're about to write to them... |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 srcRow += sampler.srcY0() * srcRowBytes; | 147 srcRow += sampler.srcY0() * srcRowBytes; |
130 for (int y = 0; y < dstHeight; ++y) { | 148 for (int y = 0; y < dstHeight; ++y) { |
131 sampler.next(srcRow); | 149 sampler.next(srcRow); |
132 srcRow += sampler.srcDY() * srcRowBytes; | 150 srcRow += sampler.srcDY() * srcRowBytes; |
133 } | 151 } |
134 | 152 |
135 return true; | 153 return true; |
136 | 154 |
137 } else if (ktxFile.isRGBA8()) { | 155 } else if (ktxFile.isRGBA8()) { |
138 | 156 |
157 // Uncompressed RGBA data | |
158 | |
139 // If we know that the image contains premultiplied alpha, then | 159 // If we know that the image contains premultiplied alpha, then |
140 // don't premultiply it upon decoding. | 160 // we need to turn off the premultiplier |
141 bool setRequireUnpremul = false; | 161 if (bSrcIsPremul) { |
142 const SkString premulKey("KTXPremultipliedAlpha"); | 162 SkASSERT(bm->alphaType() == kPremul_SkAlphaType); |
143 if (ktxFile.getValueForKey(premulKey) == SkString("True")) { | 163 SkASSERT(!this->getRequireUnpremultipliedColors()); |
144 this->setRequireUnpremultipliedColors(true); | |
145 setRequireUnpremul = true; | |
146 } | |
147 | 164 |
148 // Uncompressed RGBA data | 165 SkScaledBitmapSampler::Options opts (*this); |
scroggo
2014/06/10 13:50:23
It might be simpler to move this outside of the if
krajcevski
2014/06/10 14:47:29
Done.
| |
149 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGBA, *this)) { | 166 opts.fPremultiplyAlpha = false; |
150 return false; | 167 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGBA, opts)) { |
168 return false; | |
169 } | |
170 } else { | |
171 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGBA, *this)) { | |
172 return false; | |
173 } | |
151 } | 174 } |
152 | 175 |
153 // Just need to read RGBA pixels | 176 // Just need to read RGBA pixels |
154 const int srcRowBytes = width * 4; | 177 const int srcRowBytes = width * 4; |
155 const int dstHeight = sampler.scaledHeight(); | 178 const int dstHeight = sampler.scaledHeight(); |
156 const uint8_t *srcRow = reinterpret_cast<const uint8_t *>(ktxFile.pixelD ata()); | 179 const uint8_t *srcRow = reinterpret_cast<const uint8_t *>(ktxFile.pixelD ata()); |
157 srcRow += sampler.srcY0() * srcRowBytes; | 180 srcRow += sampler.srcY0() * srcRowBytes; |
158 for (int y = 0; y < dstHeight; ++y) { | 181 for (int y = 0; y < dstHeight; ++y) { |
159 sampler.next(srcRow); | 182 sampler.next(srcRow); |
160 srcRow += sampler.srcDY() * srcRowBytes; | 183 srcRow += sampler.srcDY() * srcRowBytes; |
161 } | 184 } |
162 | 185 |
163 // Reset this in case the decoder needs to be used again. | |
164 if (setRequireUnpremul) { | |
165 this->setRequireUnpremultipliedColors(false); | |
166 } | |
167 | |
168 return true; | 186 return true; |
169 } | 187 } |
170 | 188 |
171 return false; | 189 return false; |
172 } | 190 } |
173 | 191 |
174 /////////////////////////////////////////////////////////////////////////////// | 192 /////////////////////////////////////////////////////////////////////////////// |
175 | 193 |
176 // KTX Image Encoder | 194 // KTX Image Encoder |
177 // | 195 // |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
253 return SkImageDecoder::kUnknown_Format; | 271 return SkImageDecoder::kUnknown_Format; |
254 } | 272 } |
255 | 273 |
256 SkImageEncoder* sk_libktx_efactory(SkImageEncoder::Type t) { | 274 SkImageEncoder* sk_libktx_efactory(SkImageEncoder::Type t) { |
257 return (SkImageEncoder::kKTX_Type == t) ? SkNEW(SkKTXImageEncoder) : NULL; | 275 return (SkImageEncoder::kKTX_Type == t) ? SkNEW(SkKTXImageEncoder) : NULL; |
258 } | 276 } |
259 | 277 |
260 static SkImageDecoder_DecodeReg gReg(sk_libktx_dfactory); | 278 static SkImageDecoder_DecodeReg gReg(sk_libktx_dfactory); |
261 static SkImageDecoder_FormatReg gFormatReg(get_format_ktx); | 279 static SkImageDecoder_FormatReg gFormatReg(get_format_ktx); |
262 static SkImageEncoder_EncodeReg gEReg(sk_libktx_efactory); | 280 static SkImageEncoder_EncodeReg gEReg(sk_libktx_efactory); |
OLD | NEW |