| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "Resources.h" | 8 #include "Resources.h" |
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkCodec.h" | 10 #include "SkCodec.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 * @param goodDigest The known good digest to compare to. | 35 * @param goodDigest The known good digest to compare to. |
| 36 * @param bm The bitmap to test. | 36 * @param bm The bitmap to test. |
| 37 */ | 37 */ |
| 38 static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& g
oodDigest, | 38 static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& g
oodDigest, |
| 39 const SkBitmap& bm) { | 39 const SkBitmap& bm) { |
| 40 SkMD5::Digest digest; | 40 SkMD5::Digest digest; |
| 41 md5(bm, &digest); | 41 md5(bm, &digest); |
| 42 REPORTER_ASSERT(r, digest == goodDigest); | 42 REPORTER_ASSERT(r, digest == goodDigest); |
| 43 } | 43 } |
| 44 | 44 |
| 45 /** |
| 46 * Test decoding an SkCodec to a particular SkImageInfo. |
| 47 * |
| 48 * Calling getPixels(info) should return expectedResult, and if goodDigest is n
on NULL, |
| 49 * the resulting decode should match. |
| 50 */ |
| 51 static void test_info(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo&
info, |
| 52 SkCodec::Result expectedResult, const SkMD5::Digest* goodD
igest) { |
| 53 SkBitmap bm; |
| 54 bm.allocPixels(info); |
| 55 SkAutoLockPixels autoLockPixels(bm); |
| 56 |
| 57 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes(
)); |
| 58 REPORTER_ASSERT(r, result == expectedResult); |
| 59 |
| 60 if (goodDigest) { |
| 61 compare_to_good_digest(r, *goodDigest, bm); |
| 62 } |
| 63 } |
| 64 |
| 45 SkIRect generate_random_subset(SkRandom* rand, int w, int h) { | 65 SkIRect generate_random_subset(SkRandom* rand, int w, int h) { |
| 46 SkIRect rect; | 66 SkIRect rect; |
| 47 do { | 67 do { |
| 48 rect.fLeft = rand->nextRangeU(0, w); | 68 rect.fLeft = rand->nextRangeU(0, w); |
| 49 rect.fTop = rand->nextRangeU(0, h); | 69 rect.fTop = rand->nextRangeU(0, h); |
| 50 rect.fRight = rand->nextRangeU(0, w); | 70 rect.fRight = rand->nextRangeU(0, w); |
| 51 rect.fBottom = rand->nextRangeU(0, h); | 71 rect.fBottom = rand->nextRangeU(0, h); |
| 52 rect.sort(); | 72 rect.sort(); |
| 53 } while (rect.isEmpty()); | 73 } while (rect.isEmpty()); |
| 54 return rect; | 74 return rect; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 79 SkBitmap bm; | 99 SkBitmap bm; |
| 80 bm.allocPixels(info); | 100 bm.allocPixels(info); |
| 81 SkAutoLockPixels autoLockPixels(bm); | 101 SkAutoLockPixels autoLockPixels(bm); |
| 82 SkCodec::Result result = | 102 SkCodec::Result result = |
| 83 codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL); | 103 codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL); |
| 84 REPORTER_ASSERT(r, result == SkCodec::kSuccess); | 104 REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 85 | 105 |
| 86 SkMD5::Digest digest; | 106 SkMD5::Digest digest; |
| 87 md5(bm, &digest); | 107 md5(bm, &digest); |
| 88 | 108 |
| 89 bm.eraseColor(SK_ColorYELLOW); | 109 // verify that re-decoding gives the same result. |
| 110 test_info(r, codec, info, SkCodec::kSuccess, &digest); |
| 90 | 111 |
| 91 result = | 112 { |
| 92 codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL); | 113 // Check alpha type conversions |
| 114 if (info.alphaType() == kOpaque_SkAlphaType) { |
| 115 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType), |
| 116 SkCodec::kInvalidConversion, NULL); |
| 117 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType), |
| 118 SkCodec::kInvalidConversion, NULL); |
| 119 } else { |
| 120 // Decoding to opaque should fail |
| 121 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType), |
| 122 SkCodec::kInvalidConversion, NULL); |
| 123 SkAlphaType otherAt = info.alphaType(); |
| 124 if (kPremul_SkAlphaType == otherAt) { |
| 125 otherAt = kUnpremul_SkAlphaType; |
| 126 } else { |
| 127 otherAt = kPremul_SkAlphaType; |
| 128 } |
| 129 // The other non-opaque alpha type should always succeed, but not ma
tch. |
| 130 test_info(r, codec, info.makeAlphaType(otherAt), SkCodec::kSuccess,
NULL); |
| 131 } |
| 132 } |
| 93 | 133 |
| 94 REPORTER_ASSERT(r, result == SkCodec::kSuccess); | 134 // Scanline decoding follows. |
| 95 // verify that re-decoding gives the same result. | |
| 96 compare_to_good_digest(r, digest, bm); | |
| 97 | 135 |
| 98 stream.reset(resource(path)); | 136 stream.reset(resource(path)); |
| 99 SkAutoTDelete<SkScanlineDecoder> scanlineDecoder( | 137 SkAutoTDelete<SkScanlineDecoder> scanlineDecoder( |
| 100 SkScanlineDecoder::NewFromStream(stream.detach())); | 138 SkScanlineDecoder::NewFromStream(stream.detach())); |
| 101 if (supportsScanlineDecoding) { | 139 if (supportsScanlineDecoding) { |
| 102 bm.eraseColor(SK_ColorYELLOW); | 140 bm.eraseColor(SK_ColorYELLOW); |
| 103 REPORTER_ASSERT(r, scanlineDecoder); | 141 REPORTER_ASSERT(r, scanlineDecoder); |
| 104 | 142 |
| 105 REPORTER_ASSERT(r, scanlineDecoder->start(info) == SkCodec::kSuccess); | 143 REPORTER_ASSERT(r, scanlineDecoder->start(info) == SkCodec::kSuccess); |
| 106 | 144 |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); | 355 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); |
| 318 result = decoder->start( | 356 result = decoder->start( |
| 319 decoder->getInfo().makeColorType(kIndex_8_SkColorType)); | 357 decoder->getInfo().makeColorType(kIndex_8_SkColorType)); |
| 320 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); | 358 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); |
| 321 } | 359 } |
| 322 | 360 |
| 323 DEF_TEST(Codec_Params, r) { | 361 DEF_TEST(Codec_Params, r) { |
| 324 test_invalid_parameters(r, "index8.png"); | 362 test_invalid_parameters(r, "index8.png"); |
| 325 test_invalid_parameters(r, "mandrill.wbmp"); | 363 test_invalid_parameters(r, "mandrill.wbmp"); |
| 326 } | 364 } |
| OLD | NEW |