| 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 if (skip_image_format(format)) { | 83 if (skip_image_format(format)) { |
| 84 return; | 84 return; |
| 85 } | 85 } |
| 86 | 86 |
| 87 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream)); | 87 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream)); |
| 88 if (NULL == decoder.get()) { | 88 if (NULL == decoder.get()) { |
| 89 SkDebugf("couldn't decode %s\n", filename.c_str()); | 89 SkDebugf("couldn't decode %s\n", filename.c_str()); |
| 90 return; | 90 return; |
| 91 } | 91 } |
| 92 | 92 |
| 93 bool success = decoder->decode(&stream, &bm8888, SkBitmap::kARGB_8888_Config
, | 93 bool success = decoder->decode(&stream, &bm8888, kN32_SkColorType, |
| 94 SkImageDecoder::kDecodePixels_Mode); | 94 SkImageDecoder::kDecodePixels_Mode); |
| 95 if (!success) { | 95 if (!success) { |
| 96 return; | 96 return; |
| 97 } | 97 } |
| 98 | 98 |
| 99 success = stream.rewind(); | 99 success = stream.rewind(); |
| 100 REPORTER_ASSERT(reporter, success); | 100 REPORTER_ASSERT(reporter, success); |
| 101 if (!success) { | 101 if (!success) { |
| 102 return; | 102 return; |
| 103 } | 103 } |
| 104 | 104 |
| 105 decoder->setRequireUnpremultipliedColors(true); | 105 decoder->setRequireUnpremultipliedColors(true); |
| 106 success = decoder->decode(&stream, &bm8888Unpremul, SkBitmap::kARGB_8888_Con
fig, | 106 success = decoder->decode(&stream, &bm8888Unpremul, kN32_SkColorType, |
| 107 SkImageDecoder::kDecodePixels_Mode); | 107 SkImageDecoder::kDecodePixels_Mode); |
| 108 if (!success) { | 108 if (!success) { |
| 109 return; | 109 return; |
| 110 } | 110 } |
| 111 | 111 |
| 112 bool dimensionsMatch = bm8888.width() == bm8888Unpremul.width() | 112 bool dimensionsMatch = bm8888.width() == bm8888Unpremul.width() |
| 113 && bm8888.height() == bm8888Unpremul.height(); | 113 && bm8888.height() == bm8888Unpremul.height(); |
| 114 REPORTER_ASSERT(reporter, dimensionsMatch); | 114 REPORTER_ASSERT(reporter, dimensionsMatch); |
| 115 if (!dimensionsMatch) { | 115 if (!dimensionsMatch) { |
| 116 return; | 116 return; |
| 117 } | 117 } |
| 118 | 118 |
| 119 // Only do the comparison if the two bitmaps are both 8888. | 119 // Only do the comparison if the two bitmaps are both 8888. |
| 120 if (bm8888.config() != SkBitmap::kARGB_8888_Config | 120 if (bm8888.colorType() != kN32_SkColorType || bm8888Unpremul.colorType() !=
kN32_SkColorType) { |
| 121 || bm8888Unpremul.config() != SkBitmap::kARGB_8888_Config) { | |
| 122 return; | 121 return; |
| 123 } | 122 } |
| 124 | 123 |
| 125 // Now compare the two bitmaps. | 124 // Now compare the two bitmaps. |
| 126 for (int i = 0; i < bm8888.width(); ++i) { | 125 for (int i = 0; i < bm8888.width(); ++i) { |
| 127 for (int j = 0; j < bm8888.height(); ++j) { | 126 for (int j = 0; j < bm8888.height(); ++j) { |
| 128 // "c0" is the color of the premultiplied bitmap at (i, j). | 127 // "c0" is the color of the premultiplied bitmap at (i, j). |
| 129 const SkPMColor c0 = *bm8888.getAddr32(i, j); | 128 const SkPMColor c0 = *bm8888.getAddr32(i, j); |
| 130 // "c1" is the result of premultiplying the color of the unpremultip
lied | 129 // "c1" is the result of premultiplying the color of the unpremultip
lied |
| 131 // bitmap at (i, j). | 130 // bitmap at (i, j). |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 SkFILEStream stream(filename.c_str()); | 174 SkFILEStream stream(filename.c_str()); |
| 176 | 175 |
| 177 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream)); | 176 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream)); |
| 178 if (NULL == decoder.get()) { | 177 if (NULL == decoder.get()) { |
| 179 return; | 178 return; |
| 180 } | 179 } |
| 181 | 180 |
| 182 decoder->setRequireUnpremultipliedColors(requireUnpremul); | 181 decoder->setRequireUnpremultipliedColors(requireUnpremul); |
| 183 | 182 |
| 184 // Decode just the bounds. This should always succeed. | 183 // Decode just the bounds. This should always succeed. |
| 185 bool success = decoder->decode(&stream, &bm, SkBitmap::kARGB_8888_Config, | 184 bool success = decoder->decode(&stream, &bm, kN32_SkColorType, |
| 186 SkImageDecoder::kDecodeBounds_Mode); | 185 SkImageDecoder::kDecodeBounds_Mode); |
| 187 REPORTER_ASSERT(reporter, success); | 186 REPORTER_ASSERT(reporter, success); |
| 188 if (!success) { | 187 if (!success) { |
| 189 return; | 188 return; |
| 190 } | 189 } |
| 191 | 190 |
| 192 // Keep track of the alpha type for testing later. If the full decode | 191 // Keep track of the alpha type for testing later. If the full decode |
| 193 // succeeds, the alpha type should be the same, unless the full decode | 192 // succeeds, the alpha type should be the same, unless the full decode |
| 194 // determined that the alpha type should actually be opaque, which may | 193 // determined that the alpha type should actually be opaque, which may |
| 195 // not be known when only decoding the bounds. | 194 // not be known when only decoding the bounds. |
| 196 const SkAlphaType boundsAlphaType = bm.alphaType(); | 195 const SkAlphaType boundsAlphaType = bm.alphaType(); |
| 197 | 196 |
| 198 // rewind should always succeed on SkFILEStream. | 197 // rewind should always succeed on SkFILEStream. |
| 199 success = stream.rewind(); | 198 success = stream.rewind(); |
| 200 REPORTER_ASSERT(reporter, success); | 199 REPORTER_ASSERT(reporter, success); |
| 201 if (!success) { | 200 if (!success) { |
| 202 return; | 201 return; |
| 203 } | 202 } |
| 204 | 203 |
| 205 success = decoder->decode(&stream, &bm, SkBitmap::kARGB_8888_Config, | 204 success = decoder->decode(&stream, &bm, kN32_SkColorType, SkImageDecoder::kD
ecodePixels_Mode); |
| 206 SkImageDecoder::kDecodePixels_Mode); | |
| 207 | 205 |
| 208 if (!success) { | 206 if (!success) { |
| 209 // When the decoder is set to require unpremul, if it does not support | 207 // When the decoder is set to require unpremul, if it does not support |
| 210 // unpremul it will fail. This is the only reason the decode should | 208 // unpremul it will fail. This is the only reason the decode should |
| 211 // fail (since we know the files we are using to test can be decoded). | 209 // fail (since we know the files we are using to test can be decoded). |
| 212 REPORTER_ASSERT(reporter, requireUnpremul); | 210 REPORTER_ASSERT(reporter, requireUnpremul); |
| 213 return; | 211 return; |
| 214 } | 212 } |
| 215 | 213 |
| 216 // The bounds decode should return with either the requested | 214 // The bounds decode should return with either the requested |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 | 276 |
| 279 // This should never fail since we know the images we're decoding. | 277 // This should never fail since we know the images we're decoding. |
| 280 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream)); | 278 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream)); |
| 281 REPORTER_ASSERT(reporter, NULL != decoder.get()); | 279 REPORTER_ASSERT(reporter, NULL != decoder.get()); |
| 282 if (NULL == decoder.get()) { | 280 if (NULL == decoder.get()) { |
| 283 continue; | 281 continue; |
| 284 } | 282 } |
| 285 | 283 |
| 286 // Test unpremultiplied. We know what color this should result in. | 284 // Test unpremultiplied. We know what color this should result in. |
| 287 decoder->setRequireUnpremultipliedColors(true); | 285 decoder->setRequireUnpremultipliedColors(true); |
| 288 bool success = decoder->decode(&stream, &bm, SkBitmap::kARGB_8888_Config
, | 286 bool success = decoder->decode(&stream, &bm, kN32_SkColorType, |
| 289 SkImageDecoder::kDecodePixels_Mode); | 287 SkImageDecoder::kDecodePixels_Mode); |
| 290 REPORTER_ASSERT(reporter, success); | 288 REPORTER_ASSERT(reporter, success); |
| 291 if (!success) { | 289 if (!success) { |
| 292 continue; | 290 continue; |
| 293 } | 291 } |
| 294 | 292 |
| 295 REPORTER_ASSERT(reporter, bm.width() == 1 && bm.height() == 1); | 293 REPORTER_ASSERT(reporter, bm.width() == 1 && bm.height() == 1); |
| 296 { | 294 { |
| 297 SkAutoLockPixels alp(bm); | 295 SkAutoLockPixels alp(bm); |
| 298 REPORTER_ASSERT(reporter, bm.getAddr32(0, 0)[0] == 0x7fffffff); | 296 REPORTER_ASSERT(reporter, bm.getAddr32(0, 0)[0] == 0x7fffffff); |
| 299 } | 297 } |
| 300 | 298 |
| 301 success = stream.rewind(); | 299 success = stream.rewind(); |
| 302 REPORTER_ASSERT(reporter, success); | 300 REPORTER_ASSERT(reporter, success); |
| 303 if (!success) { | 301 if (!success) { |
| 304 continue; | 302 continue; |
| 305 } | 303 } |
| 306 | 304 |
| 307 // Test premultiplied. Once again, we know which color this should | 305 // Test premultiplied. Once again, we know which color this should |
| 308 // result in. | 306 // result in. |
| 309 decoder->setRequireUnpremultipliedColors(false); | 307 decoder->setRequireUnpremultipliedColors(false); |
| 310 success = decoder->decode(&stream, &bm, SkBitmap::kARGB_8888_Config, | 308 success = decoder->decode(&stream, &bm, kN32_SkColorType, |
| 311 SkImageDecoder::kDecodePixels_Mode); | 309 SkImageDecoder::kDecodePixels_Mode); |
| 312 REPORTER_ASSERT(reporter, success); | 310 REPORTER_ASSERT(reporter, success); |
| 313 if (!success) { | 311 if (!success) { |
| 314 continue; | 312 continue; |
| 315 } | 313 } |
| 316 | 314 |
| 317 REPORTER_ASSERT(reporter, bm.width() == 1 && bm.height() == 1); | 315 REPORTER_ASSERT(reporter, bm.width() == 1 && bm.height() == 1); |
| 318 { | 316 { |
| 319 SkAutoLockPixels alp(bm); | 317 SkAutoLockPixels alp(bm); |
| 320 REPORTER_ASSERT(reporter, bm.getAddr32(0, 0)[0] == 0x7f7f7f7f); | 318 REPORTER_ASSERT(reporter, bm.getAddr32(0, 0)[0] == 0x7f7f7f7f); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 continue; | 372 continue; |
| 375 } | 373 } |
| 376 int width, height; | 374 int width, height; |
| 377 if (!decoder->buildTileIndex(stream.get(), &width, &height)) { | 375 if (!decoder->buildTileIndex(stream.get(), &width, &height)) { |
| 378 SkDebugf("could not build a tile index\n"); | 376 SkDebugf("could not build a tile index\n"); |
| 379 continue; | 377 continue; |
| 380 } | 378 } |
| 381 // Now unref the stream to make sure it survives | 379 // Now unref the stream to make sure it survives |
| 382 stream.reset(NULL); | 380 stream.reset(NULL); |
| 383 SkBitmap bm; | 381 SkBitmap bm; |
| 384 decoder->decodeSubset(&bm, SkIRect::MakeWH(width, height), | 382 decoder->decodeSubset(&bm, SkIRect::MakeWH(width, height), kN32_SkColorT
ype); |
| 385 SkBitmap::kARGB_8888_Config); | |
| 386 } | 383 } |
| 387 } | 384 } |
| 388 | 385 |
| 389 // Test inside SkScaledBitmapSampler.cpp | 386 // Test inside SkScaledBitmapSampler.cpp |
| 390 extern void test_row_proc_choice(); | 387 extern void test_row_proc_choice(); |
| 391 | 388 |
| 392 #endif // SK_DEBUG | 389 #endif // SK_DEBUG |
| 393 | 390 |
| 394 DEF_TEST(ImageDecoding, reporter) { | 391 DEF_TEST(ImageDecoding, reporter) { |
| 395 test_unpremul(reporter); | 392 test_unpremul(reporter); |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 622 // && out_size <= SkNextPow2(((in_size - 1) / sample_size) + 1)); | 619 // && out_size <= SkNextPow2(((in_size - 1) / sample_size) + 1)); |
| 623 #endif // SK_BUILD_FOR_ANDROID || SK_BUILD_FOR_UNIX | 620 #endif // SK_BUILD_FOR_ANDROID || SK_BUILD_FOR_UNIX |
| 624 SkAutoLockPixels alp(bm); | 621 SkAutoLockPixels alp(bm); |
| 625 if (bm.getPixels() == NULL) { | 622 if (bm.getPixels() == NULL) { |
| 626 ERRORF(reporter, "Pixel decode failed [sampleSize=%d dither=%s " | 623 ERRORF(reporter, "Pixel decode failed [sampleSize=%d dither=%s " |
| 627 "colorType=%s %s]", opts.fSampleSize, yn(opts.fDitherImage), | 624 "colorType=%s %s]", opts.fSampleSize, yn(opts.fDitherImage), |
| 628 options_colorType(opts), path.c_str()); | 625 options_colorType(opts), path.c_str()); |
| 629 return; | 626 return; |
| 630 } | 627 } |
| 631 | 628 |
| 632 SkBitmap::Config requestedConfig | 629 SkColorType requestedColorType = opts.fRequestedColorType; |
| 633 = SkColorTypeToBitmapConfig(opts.fRequestedColorType); | |
| 634 REPORTER_ASSERT(reporter, | 630 REPORTER_ASSERT(reporter, |
| 635 (!opts.fUseRequestedColorType) | 631 (!opts.fUseRequestedColorType) |
| 636 || (bm.config() == requestedConfig)); | 632 || (bm.colorType() == requestedColorType)); |
| 637 | 633 |
| 638 // Condition under which we should check the decoding results: | 634 // Condition under which we should check the decoding results: |
| 639 if ((SkBitmap::kARGB_8888_Config == bm.config()) | 635 if ((kN32_SkColorType == bm.colorType()) |
| 640 && (!path.endsWith(".jpg")) // lossy | 636 && (!path.endsWith(".jpg")) // lossy |
| 641 && (opts.fSampleSize == 1)) { // scaled | 637 && (opts.fSampleSize == 1)) { // scaled |
| 642 const SkColor* correctPixels = kExpectedPixels; | 638 const SkColor* correctPixels = kExpectedPixels; |
| 643 SkASSERT(bm.height() == kExpectedHeight); | 639 SkASSERT(bm.height() == kExpectedHeight); |
| 644 SkASSERT(bm.width() == kExpectedWidth); | 640 SkASSERT(bm.width() == kExpectedWidth); |
| 645 int pixelErrors = 0; | 641 int pixelErrors = 0; |
| 646 for (int y = 0; y < bm.height(); ++y) { | 642 for (int y = 0; y < bm.height(); ++y) { |
| 647 for (int x = 0; x < bm.width(); ++x) { | 643 for (int x = 0; x < bm.width(); ++x) { |
| 648 if (*correctPixels != bm.getColor(x, y)) { | 644 if (*correctPixels != bm.getColor(x, y)) { |
| 649 ++pixelErrors; | 645 ++pixelErrors; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 } | 718 } |
| 723 SkDecodingImageGenerator::Options options(scaleList[i], | 719 SkDecodingImageGenerator::Options options(scaleList[i], |
| 724 ditherList[j]); | 720 ditherList[j]); |
| 725 test_options(reporter, options, encodedStream, encodedData, | 721 test_options(reporter, options, encodedStream, encodedData, |
| 726 useDataList[m], path); | 722 useDataList[m], path); |
| 727 } | 723 } |
| 728 } | 724 } |
| 729 } | 725 } |
| 730 } | 726 } |
| 731 } | 727 } |
| OLD | NEW |