OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkColor.h" | 10 #include "SkColor.h" |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 do { | 156 do { |
157 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), basen
ame.c_str()); | 157 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), basen
ame.c_str()); |
158 // SkDebugf("about to decode \"%s\"\n", filename.c_str()); | 158 // SkDebugf("about to decode \"%s\"\n", filename.c_str()); |
159 compare_unpremul(reporter, filename); | 159 compare_unpremul(reporter, filename); |
160 } while (iter.next(&basename)); | 160 } while (iter.next(&basename)); |
161 } else { | 161 } else { |
162 SkDebugf("Failed to find any files :(\n"); | 162 SkDebugf("Failed to find any files :(\n"); |
163 } | 163 } |
164 } | 164 } |
165 | 165 |
| 166 #if defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_UNIX) |
| 167 // Test that the alpha type is what we expect. |
| 168 static void test_alphaType(skiatest::Reporter* reporter, const SkString& filenam
e, |
| 169 bool requireUnpremul) { |
| 170 SkBitmap bm; |
| 171 SkFILEStream stream(filename.c_str()); |
| 172 |
| 173 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream)); |
| 174 if (NULL == decoder.get()) { |
| 175 return; |
| 176 } |
| 177 |
| 178 decoder->setRequireUnpremultipliedColors(requireUnpremul); |
| 179 |
| 180 // Decode just the bounds. This should always succeed. |
| 181 bool success = decoder->decode(&stream, &bm, SkBitmap::kARGB_8888_Config, |
| 182 SkImageDecoder::kDecodeBounds_Mode); |
| 183 REPORTER_ASSERT(reporter, success); |
| 184 if (!success) { |
| 185 return; |
| 186 } |
| 187 |
| 188 // Keep track of the alpha type for testing later. If the full decode |
| 189 // succeeds, the alpha type should be the same, unless the full decode |
| 190 // determined that the alpha type should actually be opaque, which may |
| 191 // not be known when only decoding the bounds. |
| 192 const SkAlphaType boundsAlphaType = bm.alphaType(); |
| 193 |
| 194 // rewind should always succeed on SkFILEStream. |
| 195 success = stream.rewind(); |
| 196 REPORTER_ASSERT(reporter, success); |
| 197 if (!success) { |
| 198 return; |
| 199 } |
| 200 |
| 201 success = decoder->decode(&stream, &bm, SkBitmap::kARGB_8888_Config, |
| 202 SkImageDecoder::kDecodePixels_Mode); |
| 203 |
| 204 if (!success) { |
| 205 // When the decoder is set to require unpremul, if it does not support |
| 206 // unpremul it will fail. This is the only reason the decode should |
| 207 // fail (since we know the files we are using to test can be decoded). |
| 208 REPORTER_ASSERT(reporter, requireUnpremul); |
| 209 return; |
| 210 } |
| 211 |
| 212 // The bounds decode should return with either the requested |
| 213 // premul/unpremul or opaque, if that value could be determined when only |
| 214 // decoding the bounds. |
| 215 if (requireUnpremul) { |
| 216 REPORTER_ASSERT(reporter, kUnpremul_SkAlphaType == boundsAlphaType |
| 217 || kOpaque_SkAlphaType == boundsAlphaType); |
| 218 } else { |
| 219 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == boundsAlphaType |
| 220 || kOpaque_SkAlphaType == boundsAlphaType); |
| 221 } |
| 222 |
| 223 // When decoding the full image, the alpha type should match the one |
| 224 // returned by the bounds decode, unless the full decode determined that |
| 225 // the alpha type is actually opaque. |
| 226 REPORTER_ASSERT(reporter, bm.alphaType() == boundsAlphaType |
| 227 || bm.alphaType() == kOpaque_SkAlphaType); |
| 228 } |
| 229 |
| 230 DEF_TEST(ImageDecoding_alphaType, reporter) { |
| 231 SkString resourcePath = skiatest::Test::GetResourcePath(); |
| 232 if (resourcePath.isEmpty()) { |
| 233 SkDebugf("Could not run alphaType test because resourcePath not specifie
d."); |
| 234 return; |
| 235 } |
| 236 |
| 237 SkOSFile::Iter iter(resourcePath.c_str()); |
| 238 SkString basename; |
| 239 if (iter.next(&basename)) { |
| 240 do { |
| 241 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), basen
ame.c_str()); |
| 242 for (int truth = 0; truth <= 1; ++truth) { |
| 243 test_alphaType(reporter, filename, SkToBool(truth)); |
| 244 } |
| 245 } while (iter.next(&basename)); |
| 246 } else { |
| 247 SkDebugf("Failed to find any files :(\n"); |
| 248 } |
| 249 |
| 250 } |
| 251 |
| 252 // Using known images, test that decoding into unpremul and premul behave as exp
ected. |
| 253 DEF_TEST(ImageDecoding_unpremul, reporter) { |
| 254 SkString resourcePath = skiatest::Test::GetResourcePath(); |
| 255 if (resourcePath.isEmpty()) { |
| 256 SkDebugf("Could not run unpremul test because resourcePath not specified
."); |
| 257 return; |
| 258 } |
| 259 const char* root = "half-transparent-white-pixel"; |
| 260 const char* suffixes[] = { ".png", ".webp" }; |
| 261 |
| 262 for (size_t i = 0; i < SK_ARRAY_COUNT(suffixes); ++i) { |
| 263 SkString basename = SkStringPrintf("%s%s", root, suffixes[i]); |
| 264 SkString fullName = SkOSPath::SkPathJoin(resourcePath.c_str(), basename.
c_str()); |
| 265 |
| 266 SkBitmap bm; |
| 267 SkFILEStream stream(fullName.c_str()); |
| 268 |
| 269 if (!stream.isValid()) { |
| 270 SkDebugf("file %s missing from resource directoy %s\n", |
| 271 basename.c_str(), resourcePath.c_str()); |
| 272 continue; |
| 273 } |
| 274 |
| 275 // This should never fail since we know the images we're decoding. |
| 276 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream)); |
| 277 REPORTER_ASSERT(reporter, NULL != decoder.get()); |
| 278 if (NULL == decoder.get()) { |
| 279 continue; |
| 280 } |
| 281 |
| 282 // Test unpremultiplied. We know what color this should result in. |
| 283 decoder->setRequireUnpremultipliedColors(true); |
| 284 bool success = decoder->decode(&stream, &bm, SkBitmap::kARGB_8888_Config
, |
| 285 SkImageDecoder::kDecodePixels_Mode); |
| 286 REPORTER_ASSERT(reporter, success); |
| 287 if (!success) { |
| 288 continue; |
| 289 } |
| 290 |
| 291 REPORTER_ASSERT(reporter, bm.width() == 1 && bm.height() == 1); |
| 292 { |
| 293 SkAutoLockPixels alp(bm); |
| 294 REPORTER_ASSERT(reporter, bm.getAddr32(0, 0)[0] == 0x7fffffff); |
| 295 } |
| 296 |
| 297 success = stream.rewind(); |
| 298 REPORTER_ASSERT(reporter, success); |
| 299 if (!success) { |
| 300 continue; |
| 301 } |
| 302 |
| 303 // Test premultiplied. Once again, we know which color this should |
| 304 // result in. |
| 305 decoder->setRequireUnpremultipliedColors(false); |
| 306 success = decoder->decode(&stream, &bm, SkBitmap::kARGB_8888_Config, |
| 307 SkImageDecoder::kDecodePixels_Mode); |
| 308 REPORTER_ASSERT(reporter, success); |
| 309 if (!success) { |
| 310 continue; |
| 311 } |
| 312 |
| 313 REPORTER_ASSERT(reporter, bm.width() == 1 && bm.height() == 1); |
| 314 { |
| 315 SkAutoLockPixels alp(bm); |
| 316 REPORTER_ASSERT(reporter, bm.getAddr32(0, 0)[0] == 0x7f7f7f7f); |
| 317 } |
| 318 } |
| 319 } |
| 320 #endif // SK_BUILD_FOR_UNIX/ANDROID skbug.com/2388 |
| 321 |
166 #ifdef SK_DEBUG | 322 #ifdef SK_DEBUG |
167 // Create a stream containing a bitmap encoded to Type type. | 323 // Create a stream containing a bitmap encoded to Type type. |
168 static SkMemoryStream* create_image_stream(SkImageEncoder::Type type) { | 324 static SkMemoryStream* create_image_stream(SkImageEncoder::Type type) { |
169 SkBitmap bm; | 325 SkBitmap bm; |
170 const int size = 50; | 326 const int size = 50; |
171 bm.allocN32Pixels(size, size); | 327 bm.allocN32Pixels(size, size); |
172 SkCanvas canvas(bm); | 328 SkCanvas canvas(bm); |
173 SkPoint points[2] = { | 329 SkPoint points[2] = { |
174 { SkIntToScalar(0), SkIntToScalar(0) }, | 330 { SkIntToScalar(0), SkIntToScalar(0) }, |
175 { SkIntToScalar(size), SkIntToScalar(size) } | 331 { SkIntToScalar(size), SkIntToScalar(size) } |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 } | 724 } |
569 SkDecodingImageGenerator::Options options(scaleList[i], | 725 SkDecodingImageGenerator::Options options(scaleList[i], |
570 ditherList[j]); | 726 ditherList[j]); |
571 test_options(reporter, options, encodedStream, encodedData, | 727 test_options(reporter, options, encodedStream, encodedData, |
572 useDataList[m], path); | 728 useDataList[m], path); |
573 } | 729 } |
574 } | 730 } |
575 } | 731 } |
576 } | 732 } |
577 } | 733 } |
OLD | NEW |