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