halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "Resources.h" |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 9 | #include "SkAndroidCodec.h" |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 10 | #include "SkBitmap.h" |
| 11 | #include "SkCodec.h" |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 12 | #include "SkCodecImageGenerator.h" |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 13 | #include "SkData.h" |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 14 | #include "SkImageEncoder.h" |
scroggo | ef0fed3 | 2016-02-18 05:59:25 -0800 | [diff] [blame] | 15 | #include "SkFrontBufferedStream.h" |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 16 | #include "SkMD5.h" |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 17 | #include "SkRandom.h" |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 18 | #include "SkStream.h" |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 19 | #include "SkStreamPriv.h" |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 20 | #include "SkPngChunkReader.h" |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 21 | #include "Test.h" |
| 22 | |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 23 | #include "png.h" |
| 24 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 25 | #if PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 5 |
| 26 | // FIXME (scroggo): Google3 needs to be updated to use a newer version of libpng. In |
| 27 | // the meantime, we had to break some pieces of SkPngCodec in order to support Google3. |
| 28 | // The parts that are broken are likely not used by Google3. |
| 29 | #define SK_PNG_DISABLE_TESTS |
| 30 | #endif |
| 31 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 32 | static void md5(const SkBitmap& bm, SkMD5::Digest* digest) { |
| 33 | SkAutoLockPixels autoLockPixels(bm); |
| 34 | SkASSERT(bm.getPixels()); |
| 35 | SkMD5 md5; |
| 36 | size_t rowLen = bm.info().bytesPerPixel() * bm.width(); |
| 37 | for (int y = 0; y < bm.height(); ++y) { |
halcanary | 1e90304 | 2016-04-25 10:29:36 -0700 | [diff] [blame] | 38 | md5.write(bm.getAddr(0, y), rowLen); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 39 | } |
| 40 | md5.finish(*digest); |
| 41 | } |
| 42 | |
scroggo | 9b2cdbf | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Compute the digest for bm and compare it to a known good digest. |
| 45 | * @param r Reporter to assert that bm's digest matches goodDigest. |
| 46 | * @param goodDigest The known good digest to compare to. |
| 47 | * @param bm The bitmap to test. |
| 48 | */ |
| 49 | static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest, |
| 50 | const SkBitmap& bm) { |
| 51 | SkMD5::Digest digest; |
| 52 | md5(bm, &digest); |
| 53 | REPORTER_ASSERT(r, digest == goodDigest); |
| 54 | } |
| 55 | |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Test decoding an SkCodec to a particular SkImageInfo. |
| 58 | * |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 59 | * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr, |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 60 | * the resulting decode should match. |
| 61 | */ |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 62 | template<typename Codec> |
| 63 | static void test_info(skiatest::Reporter* r, Codec* codec, const SkImageInfo& info, |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 64 | SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) { |
| 65 | SkBitmap bm; |
| 66 | bm.allocPixels(info); |
| 67 | SkAutoLockPixels autoLockPixels(bm); |
| 68 | |
| 69 | SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes()); |
| 70 | REPORTER_ASSERT(r, result == expectedResult); |
| 71 | |
| 72 | if (goodDigest) { |
| 73 | compare_to_good_digest(r, *goodDigest, bm); |
| 74 | } |
| 75 | } |
| 76 | |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 77 | SkIRect generate_random_subset(SkRandom* rand, int w, int h) { |
| 78 | SkIRect rect; |
| 79 | do { |
| 80 | rect.fLeft = rand->nextRangeU(0, w); |
| 81 | rect.fTop = rand->nextRangeU(0, h); |
| 82 | rect.fRight = rand->nextRangeU(0, w); |
| 83 | rect.fBottom = rand->nextRangeU(0, h); |
| 84 | rect.sort(); |
| 85 | } while (rect.isEmpty()); |
| 86 | return rect; |
| 87 | } |
| 88 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 89 | static void test_incremental_decode(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info, |
| 90 | const SkMD5::Digest& goodDigest) { |
| 91 | SkBitmap bm; |
| 92 | bm.allocPixels(info); |
| 93 | SkAutoLockPixels autoLockPixels(bm); |
| 94 | |
| 95 | REPORTER_ASSERT(r, SkCodec::kSuccess == codec->startIncrementalDecode(info, bm.getPixels(), |
| 96 | bm.rowBytes())); |
| 97 | |
| 98 | REPORTER_ASSERT(r, SkCodec::kSuccess == codec->incrementalDecode()); |
| 99 | |
| 100 | compare_to_good_digest(r, goodDigest, bm); |
| 101 | } |
| 102 | |
| 103 | // Test in stripes, similar to DM's kStripe_Mode |
| 104 | static void test_in_stripes(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info, |
| 105 | const SkMD5::Digest& goodDigest) { |
| 106 | SkBitmap bm; |
| 107 | bm.allocPixels(info); |
| 108 | bm.eraseColor(SK_ColorYELLOW); |
| 109 | |
| 110 | const int height = info.height(); |
| 111 | // Note that if numStripes does not evenly divide height there will be an extra |
| 112 | // stripe. |
| 113 | const int numStripes = 4; |
| 114 | |
| 115 | if (numStripes > height) { |
| 116 | // Image is too small. |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | const int stripeHeight = height / numStripes; |
| 121 | |
| 122 | // Iterate through the image twice. Once to decode odd stripes, and once for even. |
| 123 | for (int oddEven = 1; oddEven >= 0; oddEven--) { |
| 124 | for (int y = oddEven * stripeHeight; y < height; y += 2 * stripeHeight) { |
| 125 | SkIRect subset = SkIRect::MakeLTRB(0, y, info.width(), |
| 126 | SkTMin(y + stripeHeight, height)); |
| 127 | SkCodec::Options options; |
| 128 | options.fSubset = ⊂ |
| 129 | if (SkCodec::kSuccess != codec->startIncrementalDecode(info, bm.getAddr(0, y), |
| 130 | bm.rowBytes(), &options)) { |
| 131 | ERRORF(r, "failed to start incremental decode!\ttop: %i\tbottom%i\n", |
| 132 | subset.top(), subset.bottom()); |
| 133 | return; |
| 134 | } |
| 135 | if (SkCodec::kSuccess != codec->incrementalDecode()) { |
| 136 | ERRORF(r, "failed incremental decode starting from line %i\n", y); |
| 137 | return; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | compare_to_good_digest(r, goodDigest, bm); |
| 143 | } |
| 144 | |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 145 | template<typename Codec> |
| 146 | static void test_codec(skiatest::Reporter* r, Codec* codec, SkBitmap& bm, const SkImageInfo& info, |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 147 | const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest, |
| 148 | const SkMD5::Digest* goodDigest) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 149 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 150 | REPORTER_ASSERT(r, info.dimensions() == size); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 151 | bm.allocPixels(info); |
| 152 | SkAutoLockPixels autoLockPixels(bm); |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 153 | |
| 154 | SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 155 | REPORTER_ASSERT(r, result == expectedResult); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 156 | |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 157 | md5(bm, digest); |
| 158 | if (goodDigest) { |
| 159 | REPORTER_ASSERT(r, *digest == *goodDigest); |
| 160 | } |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 161 | |
msarett | 8ff6ca6 | 2015-09-18 12:06:04 -0700 | [diff] [blame] | 162 | { |
| 163 | // Test decoding to 565 |
| 164 | SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType); |
scroggo | ba58489 | 2016-05-20 13:56:13 -0700 | [diff] [blame] | 165 | if (info.alphaType() == kOpaque_SkAlphaType) { |
| 166 | // Decoding to 565 should succeed. |
| 167 | SkBitmap bm565; |
| 168 | bm565.allocPixels(info565); |
| 169 | SkAutoLockPixels alp(bm565); |
| 170 | |
| 171 | // This will allow comparison even if the image is incomplete. |
| 172 | bm565.eraseColor(SK_ColorBLACK); |
| 173 | |
| 174 | REPORTER_ASSERT(r, expectedResult == codec->getPixels(info565, |
| 175 | bm565.getPixels(), bm565.rowBytes())); |
| 176 | |
| 177 | SkMD5::Digest digest565; |
| 178 | md5(bm565, &digest565); |
| 179 | |
| 180 | // A dumb client's request for non-opaque should also succeed. |
| 181 | for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) { |
| 182 | info565 = info565.makeAlphaType(alpha); |
| 183 | test_info(r, codec, info565, expectedResult, &digest565); |
| 184 | } |
| 185 | } else { |
| 186 | test_info(r, codec, info565, SkCodec::kInvalidConversion, nullptr); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (codec->getInfo().colorType() == kGray_8_SkColorType) { |
| 191 | SkImageInfo grayInfo = codec->getInfo(); |
| 192 | SkBitmap grayBm; |
| 193 | grayBm.allocPixels(grayInfo); |
| 194 | SkAutoLockPixels alp(grayBm); |
| 195 | |
| 196 | grayBm.eraseColor(SK_ColorBLACK); |
| 197 | |
| 198 | REPORTER_ASSERT(r, expectedResult == codec->getPixels(grayInfo, |
| 199 | grayBm.getPixels(), grayBm.rowBytes())); |
| 200 | |
| 201 | SkMD5::Digest grayDigest; |
| 202 | md5(grayBm, &grayDigest); |
| 203 | |
| 204 | for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) { |
| 205 | grayInfo = grayInfo.makeAlphaType(alpha); |
| 206 | test_info(r, codec, grayInfo, expectedResult, &grayDigest); |
| 207 | } |
msarett | 8ff6ca6 | 2015-09-18 12:06:04 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Verify that re-decoding gives the same result. It is interesting to check this after |
| 211 | // a decode to 565, since choosing to decode to 565 may result in some of the decode |
| 212 | // options being modified. These options should return to their defaults on another |
| 213 | // decode to kN32, so the new digest should match the old digest. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 214 | test_info(r, codec, info, expectedResult, digest); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 215 | |
| 216 | { |
| 217 | // Check alpha type conversions |
| 218 | if (info.alphaType() == kOpaque_SkAlphaType) { |
| 219 | test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType), |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 220 | expectedResult, digest); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 221 | test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType), |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 222 | expectedResult, digest); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 223 | } else { |
| 224 | // Decoding to opaque should fail |
| 225 | test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 226 | SkCodec::kInvalidConversion, nullptr); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 227 | SkAlphaType otherAt = info.alphaType(); |
| 228 | if (kPremul_SkAlphaType == otherAt) { |
| 229 | otherAt = kUnpremul_SkAlphaType; |
| 230 | } else { |
| 231 | otherAt = kPremul_SkAlphaType; |
| 232 | } |
| 233 | // The other non-opaque alpha type should always succeed, but not match. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 234 | test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 237 | } |
| 238 | |
scroggo | bed1ed6 | 2016-02-11 10:24:55 -0800 | [diff] [blame] | 239 | static bool supports_partial_scanlines(const char path[]) { |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 240 | static const char* const exts[] = { |
| 241 | "jpg", "jpeg", "png", "webp" |
| 242 | "JPG", "JPEG", "PNG", "WEBP" |
| 243 | }; |
| 244 | |
| 245 | for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) { |
| 246 | if (SkStrEndsWith(path, exts[i])) { |
| 247 | return true; |
| 248 | } |
| 249 | } |
| 250 | return false; |
| 251 | } |
| 252 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 253 | // FIXME: Break up this giant function |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 254 | static void check(skiatest::Reporter* r, |
| 255 | const char path[], |
| 256 | SkISize size, |
| 257 | bool supportsScanlineDecoding, |
| 258 | bool supportsSubsetDecoding, |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 259 | bool supportsIncomplete, |
| 260 | bool supportsNewScanlineDecoding = false) { |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 261 | |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 262 | SkAutoTDelete<SkStream> stream(GetResourceAsStream(path)); |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 263 | if (!stream) { |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 264 | return; |
| 265 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 266 | |
| 267 | SkAutoTDelete<SkCodec> codec(nullptr); |
| 268 | bool isIncomplete = supportsIncomplete; |
| 269 | if (isIncomplete) { |
| 270 | size_t size = stream->getLength(); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 271 | sk_sp<SkData> data((SkData::MakeFromStream(stream, 2 * size / 3))); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 272 | codec.reset(SkCodec::NewFromData(data)); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 273 | } else { |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 274 | codec.reset(SkCodec::NewFromStream(stream.release())); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 275 | } |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 276 | if (!codec) { |
| 277 | ERRORF(r, "Unable to decode '%s'", path); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | // Test full image decodes with SkCodec |
| 282 | SkMD5::Digest codecDigest; |
scroggo | ef0fed3 | 2016-02-18 05:59:25 -0800 | [diff] [blame] | 283 | const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType); |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 284 | SkBitmap bm; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 285 | SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess; |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 286 | test_codec(r, codec.get(), bm, info, size, expectedResult, &codecDigest, nullptr); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 287 | |
| 288 | // Scanline decoding follows. |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 289 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 290 | if (supportsNewScanlineDecoding && !isIncomplete) { |
| 291 | test_incremental_decode(r, codec, info, codecDigest); |
| 292 | test_in_stripes(r, codec, info, codecDigest); |
| 293 | } |
| 294 | |
| 295 | // Need to call startScanlineDecode() first. |
| 296 | REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) == 0); |
| 297 | REPORTER_ASSERT(r, !codec->skipScanlines(1)); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 298 | const SkCodec::Result startResult = codec->startScanlineDecode(info); |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 299 | if (supportsScanlineDecoding) { |
| 300 | bm.eraseColor(SK_ColorYELLOW); |
msarett | c0e80c1 | 2015-07-01 06:50:35 -0700 | [diff] [blame] | 301 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 302 | REPORTER_ASSERT(r, startResult == SkCodec::kSuccess); |
scroggo | 9b2cdbf | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 303 | |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 304 | for (int y = 0; y < info.height(); y++) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 305 | const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0); |
| 306 | if (!isIncomplete) { |
| 307 | REPORTER_ASSERT(r, 1 == lines); |
| 308 | } |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 309 | } |
| 310 | // verify that scanline decoding gives the same result. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 311 | if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) { |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 312 | compare_to_good_digest(r, codecDigest, bm); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 313 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 314 | |
| 315 | // Cannot continue to decode scanlines beyond the end |
| 316 | REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 317 | == 0); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 318 | |
| 319 | // Interrupting a scanline decode with a full decode starts from |
| 320 | // scratch |
| 321 | REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 322 | const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0); |
| 323 | if (!isIncomplete) { |
| 324 | REPORTER_ASSERT(r, lines == 1); |
| 325 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 326 | REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes()) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 327 | == expectedResult); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 328 | REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 329 | == 0); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 330 | REPORTER_ASSERT(r, codec->skipScanlines(1) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 331 | == 0); |
msarett | 80803ff | 2015-10-16 10:54:12 -0700 | [diff] [blame] | 332 | |
| 333 | // Test partial scanline decodes |
scroggo | bed1ed6 | 2016-02-11 10:24:55 -0800 | [diff] [blame] | 334 | if (supports_partial_scanlines(path) && info.width() >= 3) { |
msarett | 80803ff | 2015-10-16 10:54:12 -0700 | [diff] [blame] | 335 | SkCodec::Options options; |
| 336 | int width = info.width(); |
| 337 | int height = info.height(); |
| 338 | SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height); |
| 339 | options.fSubset = ⊂ |
| 340 | |
| 341 | const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options, |
| 342 | nullptr, nullptr); |
| 343 | REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess); |
| 344 | |
| 345 | for (int y = 0; y < height; y++) { |
| 346 | const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0); |
| 347 | if (!isIncomplete) { |
| 348 | REPORTER_ASSERT(r, 1 == lines); |
| 349 | } |
| 350 | } |
| 351 | } |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 352 | } else { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 353 | REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 354 | } |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 355 | |
| 356 | // The rest of this function tests decoding subsets, and will decode an arbitrary number of |
| 357 | // random subsets. |
| 358 | // Do not attempt to decode subsets of an image of only once pixel, since there is no |
| 359 | // meaningful subset. |
| 360 | if (size.width() * size.height() == 1) { |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | SkRandom rand; |
| 365 | SkIRect subset; |
| 366 | SkCodec::Options opts; |
| 367 | opts.fSubset = ⊂ |
| 368 | for (int i = 0; i < 5; i++) { |
| 369 | subset = generate_random_subset(&rand, size.width(), size.height()); |
| 370 | SkASSERT(!subset.isEmpty()); |
| 371 | const bool supported = codec->getValidSubset(&subset); |
| 372 | REPORTER_ASSERT(r, supported == supportsSubsetDecoding); |
| 373 | |
| 374 | SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height()); |
| 375 | SkBitmap bm; |
| 376 | bm.allocPixels(subsetInfo); |
| 377 | const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 378 | &opts, nullptr, nullptr); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 379 | |
| 380 | if (supportsSubsetDecoding) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 381 | REPORTER_ASSERT(r, result == expectedResult); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 382 | // Webp is the only codec that supports subsets, and it will have modified the subset |
| 383 | // to have even left/top. |
| 384 | REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop)); |
| 385 | } else { |
| 386 | // No subsets will work. |
| 387 | REPORTER_ASSERT(r, result == SkCodec::kUnimplemented); |
| 388 | } |
| 389 | } |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 390 | |
scroggo | bed1ed6 | 2016-02-11 10:24:55 -0800 | [diff] [blame] | 391 | // SkAndroidCodec tests |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 392 | if (supportsScanlineDecoding || supportsSubsetDecoding || supportsNewScanlineDecoding) { |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 393 | |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 394 | SkAutoTDelete<SkStream> stream(GetResourceAsStream(path)); |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 395 | if (!stream) { |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 396 | return; |
| 397 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 398 | |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 399 | SkAutoTDelete<SkAndroidCodec> androidCodec(nullptr); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 400 | if (isIncomplete) { |
| 401 | size_t size = stream->getLength(); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 402 | sk_sp<SkData> data((SkData::MakeFromStream(stream, 2 * size / 3))); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 403 | androidCodec.reset(SkAndroidCodec::NewFromData(data)); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 404 | } else { |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 405 | androidCodec.reset(SkAndroidCodec::NewFromStream(stream.release())); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 406 | } |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 407 | if (!androidCodec) { |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 408 | ERRORF(r, "Unable to decode '%s'", path); |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | SkBitmap bm; |
scroggo | bed1ed6 | 2016-02-11 10:24:55 -0800 | [diff] [blame] | 413 | SkMD5::Digest androidCodecDigest; |
| 414 | test_codec(r, androidCodec.get(), bm, info, size, expectedResult, &androidCodecDigest, |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 415 | &codecDigest); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 416 | } |
| 417 | |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 418 | if (!isIncomplete) { |
scroggo | ef0fed3 | 2016-02-18 05:59:25 -0800 | [diff] [blame] | 419 | // Test SkCodecImageGenerator |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 420 | SkAutoTDelete<SkStream> stream(GetResourceAsStream(path)); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 421 | sk_sp<SkData> fullData(SkData::MakeFromStream(stream, stream->getLength())); |
| 422 | SkAutoTDelete<SkImageGenerator> gen( |
| 423 | SkCodecImageGenerator::NewFromEncodedCodec(fullData.get())); |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 424 | SkBitmap bm; |
| 425 | bm.allocPixels(info); |
| 426 | SkAutoLockPixels autoLockPixels(bm); |
| 427 | REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes())); |
| 428 | compare_to_good_digest(r, codecDigest, bm); |
scroggo | ef0fed3 | 2016-02-18 05:59:25 -0800 | [diff] [blame] | 429 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 430 | #ifndef SK_PNG_DISABLE_TESTS |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 431 | // Test using SkFrontBufferedStream, as Android does |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 432 | SkStream* bufferedStream = SkFrontBufferedStream::Create( |
| 433 | new SkMemoryStream(std::move(fullData)), SkCodec::MinBufferedBytesNeeded()); |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 434 | REPORTER_ASSERT(r, bufferedStream); |
| 435 | codec.reset(SkCodec::NewFromStream(bufferedStream)); |
| 436 | REPORTER_ASSERT(r, codec); |
| 437 | if (codec) { |
| 438 | test_info(r, codec.get(), info, SkCodec::kSuccess, &codecDigest); |
scroggo | ef0fed3 | 2016-02-18 05:59:25 -0800 | [diff] [blame] | 439 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 440 | #endif |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 441 | } |
| 442 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 443 | // If we've just tested incomplete decodes, let's run the same test again on full decodes. |
| 444 | if (isIncomplete) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 445 | check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false, |
| 446 | supportsNewScanlineDecoding); |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 447 | } |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | DEF_TEST(Codec, r) { |
| 451 | // WBMP |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 452 | check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false, true); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 453 | |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 454 | // WEBP |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 455 | check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true, true); |
| 456 | check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true, true); |
| 457 | check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true, true); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 458 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 459 | // BMP |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 460 | check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false, true); |
| 461 | check(r, "rle.bmp", SkISize::Make(320, 240), true, false, true); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 462 | |
| 463 | // ICO |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 464 | // FIXME: We are not ready to test incomplete ICOs |
msarett | 68b204e | 2015-04-01 12:09:21 -0700 | [diff] [blame] | 465 | // These two tests examine interestingly different behavior: |
| 466 | // Decodes an embedded BMP image |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 467 | check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false); |
msarett | 68b204e | 2015-04-01 12:09:21 -0700 | [diff] [blame] | 468 | // Decodes an embedded PNG image |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 469 | check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false, false, true); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 470 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 471 | // GIF |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 472 | // FIXME: We are not ready to test incomplete GIFs |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 473 | check(r, "box.gif", SkISize::Make(200, 55), true, false, false); |
| 474 | check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, false); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 475 | // randPixels.gif is too small to test incomplete |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 476 | check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, false); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 477 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 478 | // JPG |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 479 | check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false, true); |
| 480 | check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false, true); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 481 | // grayscale.jpg is too small to test incomplete |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 482 | check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 483 | check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false, true); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 484 | // randPixels.jpg is too small to test incomplete |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 485 | check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 486 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 487 | // PNG |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 488 | check(r, "arrow.png", SkISize::Make(187, 312), false, false, true, true); |
| 489 | check(r, "baby_tux.png", SkISize::Make(240, 246), false, false, true, true); |
| 490 | check(r, "color_wheel.png", SkISize::Make(128, 128), false, false, true, true); |
| 491 | // half-transparent-white-pixel.png is too small to test incomplete |
| 492 | check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), false, false, false, true); |
| 493 | check(r, "mandrill_128.png", SkISize::Make(128, 128), false, false, true, true); |
| 494 | check(r, "mandrill_16.png", SkISize::Make(16, 16), false, false, true, true); |
| 495 | check(r, "mandrill_256.png", SkISize::Make(256, 256), false, false, true, true); |
| 496 | check(r, "mandrill_32.png", SkISize::Make(32, 32), false, false, true, true); |
| 497 | check(r, "mandrill_512.png", SkISize::Make(512, 512), false, false, true, true); |
| 498 | check(r, "mandrill_64.png", SkISize::Make(64, 64), false, false, true, true); |
| 499 | check(r, "plane.png", SkISize::Make(250, 126), false, false, true, true); |
| 500 | check(r, "plane_interlaced.png", SkISize::Make(250, 126), false, false, true, true); |
| 501 | check(r, "randPixels.png", SkISize::Make(8, 8), false, false, true, true); |
| 502 | check(r, "yellow_rose.png", SkISize::Make(400, 301), false, false, true, true); |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 503 | |
| 504 | // RAW |
yujieqin | f236ee4 | 2016-02-29 07:14:42 -0800 | [diff] [blame] | 505 | // Disable RAW tests for Win32. |
| 506 | #if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32)) |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 507 | check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false); |
ebrauer | 46d2aa8 | 2016-02-17 08:04:00 -0800 | [diff] [blame] | 508 | check(r, "sample_1mp_rotated.dng", SkISize::Make(600, 338), false, false, false); |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 509 | check(r, "dng_with_preview.dng", SkISize::Make(600, 338), true, false, false); |
msarett | 02cb4d4 | 2016-01-25 11:01:34 -0800 | [diff] [blame] | 510 | #endif |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 511 | } |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 512 | |
| 513 | static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) { |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 514 | // Neither of these calls should return a codec. Bots should catch us if we leaked anything. |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 515 | SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false)); |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 516 | REPORTER_ASSERT(r, !codec); |
| 517 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 518 | SkAndroidCodec* androidCodec = |
| 519 | SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false)); |
| 520 | REPORTER_ASSERT(r, !androidCodec); |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | // Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream, |
| 524 | // even on failure. Test some bad streams. |
| 525 | DEF_TEST(Codec_leaks, r) { |
| 526 | // No codec should claim this as their format, so this tests SkCodec::NewFromStream. |
| 527 | const char nonSupportedStream[] = "hello world"; |
| 528 | // The other strings should look like the beginning of a file type, so we'll call some |
| 529 | // internal version of NewFromStream, which must also delete the stream on failure. |
| 530 | const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }; |
| 531 | const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF }; |
| 532 | const char emptyWebp[] = "RIFF1234WEBPVP"; |
| 533 | const char emptyBmp[] = { 'B', 'M' }; |
| 534 | const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' }; |
| 535 | const char emptyGif[] = "GIFVER"; |
| 536 | |
| 537 | test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream)); |
| 538 | test_invalid_stream(r, emptyPng, sizeof(emptyPng)); |
| 539 | test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg)); |
| 540 | test_invalid_stream(r, emptyWebp, sizeof(emptyWebp)); |
| 541 | test_invalid_stream(r, emptyBmp, sizeof(emptyBmp)); |
| 542 | test_invalid_stream(r, emptyIco, sizeof(emptyIco)); |
| 543 | test_invalid_stream(r, emptyGif, sizeof(emptyGif)); |
| 544 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 545 | |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 546 | DEF_TEST(Codec_null, r) { |
scroggo | bed1ed6 | 2016-02-11 10:24:55 -0800 | [diff] [blame] | 547 | // Attempting to create an SkCodec or an SkAndroidCodec with null should not |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 548 | // crash. |
| 549 | SkCodec* codec = SkCodec::NewFromStream(nullptr); |
| 550 | REPORTER_ASSERT(r, !codec); |
| 551 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 552 | SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr); |
| 553 | REPORTER_ASSERT(r, !androidCodec); |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 554 | } |
| 555 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 556 | static void test_dimensions(skiatest::Reporter* r, const char path[]) { |
| 557 | // Create the codec from the resource file |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 558 | SkAutoTDelete<SkStream> stream(GetResourceAsStream(path)); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 559 | if (!stream) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 560 | return; |
| 561 | } |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 562 | SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release())); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 563 | if (!codec) { |
| 564 | ERRORF(r, "Unable to create codec '%s'", path); |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | // Check that the decode is successful for a variety of scales |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 569 | for (int sampleSize = 1; sampleSize < 32; sampleSize++) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 570 | // Scale the output dimensions |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 571 | SkISize scaledDims = codec->getSampledDimensions(sampleSize); |
msarett | b32758a | 2015-08-18 13:22:46 -0700 | [diff] [blame] | 572 | SkImageInfo scaledInfo = codec->getInfo() |
| 573 | .makeWH(scaledDims.width(), scaledDims.height()) |
| 574 | .makeColorType(kN32_SkColorType); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 575 | |
| 576 | // Set up for the decode |
| 577 | size_t rowBytes = scaledDims.width() * sizeof(SkPMColor); |
| 578 | size_t totalBytes = scaledInfo.getSafeSize(rowBytes); |
| 579 | SkAutoTMalloc<SkPMColor> pixels(totalBytes); |
| 580 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 581 | SkAndroidCodec::AndroidOptions options; |
| 582 | options.fSampleSize = sampleSize; |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 583 | SkCodec::Result result = |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 584 | codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 585 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
| 589 | // Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes |
| 590 | DEF_TEST(Codec_Dimensions, r) { |
| 591 | // JPG |
| 592 | test_dimensions(r, "CMYK.jpg"); |
| 593 | test_dimensions(r, "color_wheel.jpg"); |
| 594 | test_dimensions(r, "grayscale.jpg"); |
| 595 | test_dimensions(r, "mandrill_512_q075.jpg"); |
| 596 | test_dimensions(r, "randPixels.jpg"); |
msarett | b32758a | 2015-08-18 13:22:46 -0700 | [diff] [blame] | 597 | |
| 598 | // Decoding small images with very large scaling factors is a potential |
| 599 | // source of bugs and crashes. We disable these tests in Gold because |
| 600 | // tiny images are not very useful to look at. |
| 601 | // Here we make sure that we do not crash or access illegal memory when |
| 602 | // performing scaled decodes on small images. |
| 603 | test_dimensions(r, "1x1.png"); |
| 604 | test_dimensions(r, "2x2.png"); |
| 605 | test_dimensions(r, "3x3.png"); |
| 606 | test_dimensions(r, "3x1.png"); |
| 607 | test_dimensions(r, "1x1.png"); |
| 608 | test_dimensions(r, "16x1.png"); |
| 609 | test_dimensions(r, "1x16.png"); |
| 610 | test_dimensions(r, "mandrill_16.png"); |
| 611 | |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 612 | // RAW |
yujieqin | f236ee4 | 2016-02-29 07:14:42 -0800 | [diff] [blame] | 613 | // Disable RAW tests for Win32. |
| 614 | #if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32)) |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 615 | test_dimensions(r, "sample_1mp.dng"); |
ebrauer | 46d2aa8 | 2016-02-17 08:04:00 -0800 | [diff] [blame] | 616 | test_dimensions(r, "sample_1mp_rotated.dng"); |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 617 | test_dimensions(r, "dng_with_preview.dng"); |
msarett | 8e49ca3 | 2016-01-25 13:10:58 -0800 | [diff] [blame] | 618 | #endif |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 619 | } |
| 620 | |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 621 | static void test_invalid(skiatest::Reporter* r, const char path[]) { |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 622 | SkAutoTDelete<SkStream> stream(GetResourceAsStream(path)); |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 623 | if (!stream) { |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 624 | return; |
| 625 | } |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 626 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 627 | REPORTER_ASSERT(r, nullptr == codec); |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 628 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 629 | |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 630 | DEF_TEST(Codec_Empty, r) { |
| 631 | // Test images that should not be able to create a codec |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 632 | test_invalid(r, "empty_images/zero-dims.gif"); |
| 633 | test_invalid(r, "empty_images/zero-embedded.ico"); |
| 634 | test_invalid(r, "empty_images/zero-width.bmp"); |
| 635 | test_invalid(r, "empty_images/zero-height.bmp"); |
| 636 | test_invalid(r, "empty_images/zero-width.jpg"); |
| 637 | test_invalid(r, "empty_images/zero-height.jpg"); |
| 638 | test_invalid(r, "empty_images/zero-width.png"); |
| 639 | test_invalid(r, "empty_images/zero-height.png"); |
| 640 | test_invalid(r, "empty_images/zero-width.wbmp"); |
| 641 | test_invalid(r, "empty_images/zero-height.wbmp"); |
| 642 | // This image is an ico with an embedded mask-bmp. This is illegal. |
| 643 | test_invalid(r, "invalid_images/mask-bmp-ico.ico"); |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 644 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 645 | |
| 646 | static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) { |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 647 | SkAutoTDelete<SkStream> stream(GetResourceAsStream(path)); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 648 | if (!stream) { |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 649 | return; |
| 650 | } |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 651 | SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.release())); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 652 | if (!decoder) { |
| 653 | SkDebugf("Missing codec for %s\n", path); |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | const SkImageInfo info = decoder->getInfo().makeColorType(kIndex_8_SkColorType); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 658 | |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 659 | // This should return kSuccess because kIndex8 is supported. |
| 660 | SkPMColor colorStorage[256]; |
| 661 | int colorCount; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 662 | SkCodec::Result result = decoder->startScanlineDecode(info, nullptr, colorStorage, |
| 663 | &colorCount); |
| 664 | if (SkCodec::kSuccess == result) { |
| 665 | // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid |
| 666 | // colorPtr and a valid colorCountPtr. |
| 667 | result = decoder->startScanlineDecode(info, nullptr, nullptr, nullptr); |
| 668 | REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); |
| 669 | result = decoder->startScanlineDecode(info); |
| 670 | REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); |
| 671 | } else if (SkCodec::kUnimplemented == result) { |
| 672 | // New method should be supported: |
| 673 | SkBitmap bm; |
| 674 | sk_sp<SkColorTable> colorTable(new SkColorTable(colorStorage, 256)); |
| 675 | bm.allocPixels(info, nullptr, colorTable.get()); |
| 676 | result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes(), nullptr, |
| 677 | colorStorage, &colorCount); |
| 678 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 679 | result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes()); |
| 680 | REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); |
| 681 | } else { |
| 682 | // The test is uninteresting if kIndex8 is not supported |
| 683 | ERRORF(r, "Should not call test_invalid_parameters for non-Index8 file: %s\n", path); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 684 | return; |
| 685 | } |
| 686 | |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | DEF_TEST(Codec_Params, r) { |
| 690 | test_invalid_parameters(r, "index8.png"); |
| 691 | test_invalid_parameters(r, "mandrill.wbmp"); |
| 692 | } |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 693 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 694 | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
| 695 | |
| 696 | #ifndef SK_PNG_DISABLE_TESTS // reading chunks does not work properly with older versions. |
| 697 | // It does not appear that anyone in Google3 is reading chunks. |
| 698 | |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 699 | static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) { |
| 700 | SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr); |
| 701 | if (!sk_stream->write(data, len)) { |
| 702 | png_error(png_ptr, "sk_write_fn Error!"); |
| 703 | } |
| 704 | } |
| 705 | |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 706 | DEF_TEST(Codec_pngChunkReader, r) { |
| 707 | // Create a dummy bitmap. Use unpremul RGBA for libpng. |
| 708 | SkBitmap bm; |
| 709 | const int w = 1; |
| 710 | const int h = 1; |
| 711 | const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, |
| 712 | kUnpremul_SkAlphaType); |
| 713 | bm.setInfo(bmInfo); |
| 714 | bm.allocPixels(); |
| 715 | bm.eraseColor(SK_ColorBLUE); |
| 716 | SkMD5::Digest goodDigest; |
| 717 | md5(bm, &goodDigest); |
| 718 | |
| 719 | // Write to a png file. |
| 720 | png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
| 721 | REPORTER_ASSERT(r, png); |
| 722 | if (!png) { |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | png_infop info = png_create_info_struct(png); |
| 727 | REPORTER_ASSERT(r, info); |
| 728 | if (!info) { |
| 729 | png_destroy_write_struct(&png, nullptr); |
| 730 | return; |
| 731 | } |
| 732 | |
| 733 | if (setjmp(png_jmpbuf(png))) { |
| 734 | ERRORF(r, "failed writing png"); |
| 735 | png_destroy_write_struct(&png, &info); |
| 736 | return; |
| 737 | } |
| 738 | |
| 739 | SkDynamicMemoryWStream wStream; |
| 740 | png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr); |
| 741 | |
| 742 | png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8, |
| 743 | PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, |
| 744 | PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
| 745 | |
| 746 | // Create some chunks that match the Android framework's use. |
| 747 | static png_unknown_chunk gUnknowns[] = { |
msarett | 133eaaa | 2016-01-07 11:03:25 -0800 | [diff] [blame] | 748 | { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR }, |
| 749 | { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR }, |
| 750 | { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR }, |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 751 | }; |
| 752 | |
| 753 | png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3); |
| 754 | png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns)); |
| 755 | #if PNG_LIBPNG_VER < 10600 |
| 756 | /* Deal with unknown chunk location bug in 1.5.x and earlier */ |
msarett | 133eaaa | 2016-01-07 11:03:25 -0800 | [diff] [blame] | 757 | png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR); |
| 758 | png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR); |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 759 | #endif |
| 760 | |
| 761 | png_write_info(png, info); |
| 762 | |
| 763 | for (int j = 0; j < h; j++) { |
| 764 | png_bytep row = (png_bytep)(bm.getAddr(0, j)); |
| 765 | png_write_rows(png, &row, 1); |
| 766 | } |
| 767 | png_write_end(png, info); |
| 768 | png_destroy_write_struct(&png, &info); |
| 769 | |
| 770 | class ChunkReader : public SkPngChunkReader { |
| 771 | public: |
| 772 | ChunkReader(skiatest::Reporter* r) |
| 773 | : fReporter(r) |
| 774 | { |
| 775 | this->reset(); |
| 776 | } |
| 777 | |
| 778 | bool readChunk(const char tag[], const void* data, size_t length) override { |
| 779 | for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) { |
| 780 | if (!strcmp(tag, (const char*) gUnknowns[i].name)) { |
| 781 | // Tag matches. This should have been the first time we see it. |
| 782 | REPORTER_ASSERT(fReporter, !fSeen[i]); |
| 783 | fSeen[i] = true; |
| 784 | |
| 785 | // Data and length should match |
| 786 | REPORTER_ASSERT(fReporter, length == gUnknowns[i].size); |
| 787 | REPORTER_ASSERT(fReporter, !strcmp((const char*) data, |
| 788 | (const char*) gUnknowns[i].data)); |
| 789 | return true; |
| 790 | } |
| 791 | } |
| 792 | ERRORF(fReporter, "Saw an unexpected unknown chunk."); |
| 793 | return true; |
| 794 | } |
| 795 | |
| 796 | bool allHaveBeenSeen() { |
| 797 | bool ret = true; |
| 798 | for (auto seen : fSeen) { |
| 799 | ret &= seen; |
| 800 | } |
| 801 | return ret; |
| 802 | } |
| 803 | |
| 804 | void reset() { |
| 805 | sk_bzero(fSeen, sizeof(fSeen)); |
| 806 | } |
| 807 | |
| 808 | private: |
| 809 | skiatest::Reporter* fReporter; // Unowned |
| 810 | bool fSeen[3]; |
| 811 | }; |
| 812 | |
| 813 | ChunkReader chunkReader(r); |
| 814 | |
| 815 | // Now read the file with SkCodec. |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 816 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(wStream.detachAsData(), &chunkReader)); |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 817 | REPORTER_ASSERT(r, codec); |
| 818 | if (!codec) { |
| 819 | return; |
| 820 | } |
| 821 | |
| 822 | // Now compare to the original. |
| 823 | SkBitmap decodedBm; |
| 824 | decodedBm.setInfo(codec->getInfo()); |
| 825 | decodedBm.allocPixels(); |
| 826 | SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), |
| 827 | decodedBm.rowBytes()); |
| 828 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 829 | |
| 830 | if (decodedBm.colorType() != bm.colorType()) { |
| 831 | SkBitmap tmp; |
| 832 | bool success = decodedBm.copyTo(&tmp, bm.colorType()); |
| 833 | REPORTER_ASSERT(r, success); |
| 834 | if (!success) { |
| 835 | return; |
| 836 | } |
| 837 | |
| 838 | tmp.swap(decodedBm); |
| 839 | } |
| 840 | |
| 841 | compare_to_good_digest(r, goodDigest, decodedBm); |
| 842 | REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen()); |
| 843 | |
| 844 | // Decoding again will read the chunks again. |
| 845 | chunkReader.reset(); |
| 846 | REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen()); |
| 847 | result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes()); |
| 848 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 849 | REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen()); |
| 850 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 851 | #endif // SK_PNG_DISABLE_TESTS |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 852 | #endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 853 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 854 | // Stream that can only peek up to a limit |
| 855 | class LimitedPeekingMemStream : public SkStream { |
| 856 | public: |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 857 | LimitedPeekingMemStream(sk_sp<SkData> data, size_t limit) |
| 858 | : fStream(std::move(data)) |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 859 | , fLimit(limit) {} |
| 860 | |
| 861 | size_t peek(void* buf, size_t bytes) const override { |
| 862 | return fStream.peek(buf, SkTMin(bytes, fLimit)); |
| 863 | } |
| 864 | size_t read(void* buf, size_t bytes) override { |
| 865 | return fStream.read(buf, bytes); |
| 866 | } |
| 867 | bool rewind() override { |
| 868 | return fStream.rewind(); |
| 869 | } |
| 870 | bool isAtEnd() const override { |
msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 871 | return fStream.isAtEnd(); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 872 | } |
| 873 | private: |
| 874 | SkMemoryStream fStream; |
| 875 | const size_t fLimit; |
| 876 | }; |
| 877 | |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 878 | // Stream that is not an asset stream (!hasPosition() or !hasLength()) |
| 879 | class NotAssetMemStream : public SkStream { |
| 880 | public: |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 881 | NotAssetMemStream(sk_sp<SkData> data) : fStream(std::move(data)) {} |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 882 | |
| 883 | bool hasPosition() const override { |
| 884 | return false; |
| 885 | } |
| 886 | |
| 887 | bool hasLength() const override { |
| 888 | return false; |
| 889 | } |
| 890 | |
| 891 | size_t peek(void* buf, size_t bytes) const override { |
| 892 | return fStream.peek(buf, bytes); |
| 893 | } |
| 894 | size_t read(void* buf, size_t bytes) override { |
| 895 | return fStream.read(buf, bytes); |
| 896 | } |
| 897 | bool rewind() override { |
| 898 | return fStream.rewind(); |
| 899 | } |
| 900 | bool isAtEnd() const override { |
| 901 | return fStream.isAtEnd(); |
| 902 | } |
| 903 | private: |
| 904 | SkMemoryStream fStream; |
| 905 | }; |
| 906 | |
yujieqin | f236ee4 | 2016-02-29 07:14:42 -0800 | [diff] [blame] | 907 | // Disable RAW tests for Win32. |
| 908 | #if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32)) |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 909 | // Test that the RawCodec works also for not asset stream. This will test the code path using |
| 910 | // SkRawBufferedStream instead of SkRawAssetStream. |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 911 | DEF_TEST(Codec_raw_notseekable, r) { |
| 912 | const char* path = "dng_with_preview.dng"; |
| 913 | SkString fullPath(GetResourcePath(path)); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 914 | sk_sp<SkData> data(SkData::MakeFromFileName(fullPath.c_str())); |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 915 | if (!data) { |
| 916 | SkDebugf("Missing resource '%s'\n", path); |
| 917 | return; |
| 918 | } |
| 919 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 920 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(std::move(data)))); |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 921 | REPORTER_ASSERT(r, codec); |
| 922 | |
| 923 | test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr); |
| 924 | } |
| 925 | #endif |
| 926 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 927 | // Test that even if webp_parse_header fails to peek enough, it will fall back to read() |
| 928 | // + rewind() and succeed. |
| 929 | DEF_TEST(Codec_webp_peek, r) { |
| 930 | const char* path = "baby_tux.webp"; |
| 931 | SkString fullPath(GetResourcePath(path)); |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 932 | auto data = SkData::MakeFromFileName(fullPath.c_str()); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 933 | if (!data) { |
| 934 | SkDebugf("Missing resource '%s'\n", path); |
| 935 | return; |
| 936 | } |
| 937 | |
| 938 | // The limit is less than webp needs to peek or read. |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 939 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream( |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 940 | new LimitedPeekingMemStream(data, 25))); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 941 | REPORTER_ASSERT(r, codec); |
| 942 | |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 943 | test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 944 | |
| 945 | // Similarly, a stream which does not peek should still succeed. |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 946 | codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0))); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 947 | REPORTER_ASSERT(r, codec); |
| 948 | |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 949 | test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 950 | } |
| 951 | |
msarett | 7f7ec20 | 2016-03-01 12:12:27 -0800 | [diff] [blame] | 952 | // SkCodec's wbmp decoder was initially unnecessarily restrictive. |
| 953 | // It required the second byte to be zero. The wbmp specification allows |
| 954 | // a couple of bits to be 1 (so long as they do not overlap with 0x9F). |
| 955 | // Test that SkCodec now supports an image with these bits set. |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 956 | DEF_TEST(Codec_wbmp, r) { |
| 957 | const char* path = "mandrill.wbmp"; |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 958 | SkAutoTDelete<SkStream> stream(GetResourceAsStream(path)); |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 959 | if (!stream) { |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 960 | return; |
| 961 | } |
| 962 | |
| 963 | // Modify the stream to contain a second byte with some bits set. |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 964 | auto data = SkCopyStreamToData(stream); |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 965 | uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data()); |
| 966 | writeableData[1] = static_cast<uint8_t>(~0x9F); |
| 967 | |
msarett | 7f7ec20 | 2016-03-01 12:12:27 -0800 | [diff] [blame] | 968 | // SkCodec should support this. |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 969 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data)); |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 970 | REPORTER_ASSERT(r, codec); |
| 971 | if (!codec) { |
| 972 | return; |
| 973 | } |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 974 | test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr); |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 975 | } |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 976 | |
| 977 | // wbmp images have a header that can be arbitrarily large, depending on the |
| 978 | // size of the image. We cap the size at 65535, meaning we only need to look at |
| 979 | // 8 bytes to determine whether we can read the image. This is important |
| 980 | // because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the |
| 981 | // image is a wbmp. |
| 982 | DEF_TEST(Codec_wbmp_max_size, r) { |
| 983 | const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header |
| 984 | 0x83, 0xFF, 0x7F, // W: 65535 |
| 985 | 0x83, 0xFF, 0x7F }; // H: 65535 |
| 986 | SkAutoTDelete<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false)); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 987 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 988 | |
| 989 | REPORTER_ASSERT(r, codec); |
| 990 | if (!codec) return; |
| 991 | |
| 992 | REPORTER_ASSERT(r, codec->getInfo().width() == 65535); |
| 993 | REPORTER_ASSERT(r, codec->getInfo().height() == 65535); |
| 994 | |
| 995 | // Now test an image which is too big. Any image with a larger header (i.e. |
| 996 | // has bigger width/height) is also too big. |
| 997 | const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header |
| 998 | 0x84, 0x80, 0x00, // W: 65536 |
| 999 | 0x84, 0x80, 0x00 }; // H: 65536 |
| 1000 | stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false)); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 1001 | codec.reset(SkCodec::NewFromStream(stream.release())); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 1002 | |
| 1003 | REPORTER_ASSERT(r, !codec); |
| 1004 | } |
msarett | 2812f03 | 2016-07-18 15:56:08 -0700 | [diff] [blame] | 1005 | |
| 1006 | DEF_TEST(Codec_jpeg_rewind, r) { |
| 1007 | const char* path = "mandrill_512_q075.jpg"; |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1008 | SkAutoTDelete<SkStream> stream(GetResourceAsStream(path)); |
msarett | 2812f03 | 2016-07-18 15:56:08 -0700 | [diff] [blame] | 1009 | if (!stream) { |
msarett | 2812f03 | 2016-07-18 15:56:08 -0700 | [diff] [blame] | 1010 | return; |
| 1011 | } |
| 1012 | SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release())); |
| 1013 | if (!codec) { |
| 1014 | ERRORF(r, "Unable to create codec '%s'.", path); |
| 1015 | return; |
| 1016 | } |
| 1017 | |
| 1018 | const int width = codec->getInfo().width(); |
| 1019 | const int height = codec->getInfo().height(); |
| 1020 | size_t rowBytes = sizeof(SkPMColor) * width; |
| 1021 | SkAutoMalloc pixelStorage(height * rowBytes); |
| 1022 | |
| 1023 | // Perform a sampled decode. |
| 1024 | SkAndroidCodec::AndroidOptions opts; |
| 1025 | opts.fSampleSize = 12; |
| 1026 | codec->getAndroidPixels(codec->getInfo().makeWH(width / 12, height / 12), pixelStorage.get(), |
| 1027 | rowBytes, &opts); |
| 1028 | |
| 1029 | // Rewind the codec and perform a full image decode. |
| 1030 | SkCodec::Result result = codec->getPixels(codec->getInfo(), pixelStorage.get(), rowBytes); |
| 1031 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 1032 | } |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 1033 | |
msarett | 35bb74b | 2016-08-22 07:41:28 -0700 | [diff] [blame] | 1034 | static void check_color_xform(skiatest::Reporter* r, const char* path) { |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1035 | SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(GetResourceAsStream(path))); |
msarett | 35bb74b | 2016-08-22 07:41:28 -0700 | [diff] [blame] | 1036 | |
| 1037 | SkAndroidCodec::AndroidOptions opts; |
| 1038 | opts.fSampleSize = 3; |
| 1039 | const int subsetWidth = codec->getInfo().width() / 2; |
| 1040 | const int subsetHeight = codec->getInfo().height() / 2; |
| 1041 | SkIRect subset = SkIRect::MakeWH(subsetWidth, subsetHeight); |
| 1042 | opts.fSubset = ⊂ |
| 1043 | |
| 1044 | const int dstWidth = subsetWidth / opts.fSampleSize; |
| 1045 | const int dstHeight = subsetHeight / opts.fSampleSize; |
| 1046 | sk_sp<SkData> data = SkData::MakeFromFileName( |
| 1047 | GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str()); |
| 1048 | sk_sp<SkColorSpace> colorSpace = SkColorSpace::NewICC(data->data(), data->size()); |
| 1049 | SkImageInfo dstInfo = codec->getInfo().makeWH(dstWidth, dstHeight) |
| 1050 | .makeColorType(kN32_SkColorType) |
| 1051 | .makeColorSpace(colorSpace); |
| 1052 | |
| 1053 | size_t rowBytes = dstInfo.minRowBytes(); |
| 1054 | SkAutoMalloc pixelStorage(dstInfo.getSafeSize(rowBytes)); |
| 1055 | SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get(), rowBytes, &opts); |
| 1056 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 1057 | } |
| 1058 | |
| 1059 | DEF_TEST(Codec_ColorXform, r) { |
| 1060 | check_color_xform(r, "mandrill_512_q075.jpg"); |
| 1061 | check_color_xform(r, "mandrill_512.png"); |
| 1062 | } |
| 1063 | |
msarett | f17b71f | 2016-09-12 14:30:03 -0700 | [diff] [blame] | 1064 | static bool color_type_match(SkColorType origColorType, SkColorType codecColorType) { |
| 1065 | switch (origColorType) { |
| 1066 | case kRGBA_8888_SkColorType: |
| 1067 | case kBGRA_8888_SkColorType: |
| 1068 | return kRGBA_8888_SkColorType == codecColorType || |
| 1069 | kBGRA_8888_SkColorType == codecColorType; |
| 1070 | default: |
| 1071 | return origColorType == codecColorType; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | static bool alpha_type_match(SkAlphaType origAlphaType, SkAlphaType codecAlphaType) { |
| 1076 | switch (origAlphaType) { |
| 1077 | case kUnpremul_SkAlphaType: |
| 1078 | case kPremul_SkAlphaType: |
| 1079 | return kUnpremul_SkAlphaType == codecAlphaType || |
| 1080 | kPremul_SkAlphaType == codecAlphaType; |
| 1081 | default: |
| 1082 | return origAlphaType == codecAlphaType; |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const SkImageInfo& info) { |
| 1087 | SkBitmap bm1; |
| 1088 | SkPMColor colors[256]; |
| 1089 | SkAutoTUnref<SkColorTable> colorTable1(new SkColorTable(colors, 256)); |
| 1090 | bm1.allocPixels(info, nullptr, colorTable1.get()); |
| 1091 | int numColors; |
| 1092 | SkCodec::Result result = origCodec->getPixels(info, bm1.getPixels(), bm1.rowBytes(), nullptr, |
| 1093 | const_cast<SkPMColor*>(colorTable1->readColors()), |
| 1094 | &numColors); |
| 1095 | // This will fail to update colorTable1->count() but is fine for the purpose of this test. |
| 1096 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
msarett | 9b09cd8 | 2016-08-29 14:47:49 -0700 | [diff] [blame] | 1097 | |
| 1098 | // Encode the image to png. |
| 1099 | sk_sp<SkData> data = |
| 1100 | sk_sp<SkData>(SkImageEncoder::EncodeData(bm1, SkImageEncoder::kPNG_Type, 100)); |
| 1101 | |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 1102 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data)); |
msarett | f17b71f | 2016-09-12 14:30:03 -0700 | [diff] [blame] | 1103 | REPORTER_ASSERT(r, color_type_match(info.colorType(), codec->getInfo().colorType())); |
| 1104 | REPORTER_ASSERT(r, alpha_type_match(info.alphaType(), codec->getInfo().alphaType())); |
msarett | 9b09cd8 | 2016-08-29 14:47:49 -0700 | [diff] [blame] | 1105 | |
| 1106 | SkBitmap bm2; |
msarett | f17b71f | 2016-09-12 14:30:03 -0700 | [diff] [blame] | 1107 | SkAutoTUnref<SkColorTable> colorTable2(new SkColorTable(colors, 256)); |
| 1108 | bm2.allocPixels(info, nullptr, colorTable2.get()); |
| 1109 | result = codec->getPixels(info, bm2.getPixels(), bm2.rowBytes(), nullptr, |
| 1110 | const_cast<SkPMColor*>(colorTable2->readColors()), &numColors); |
msarett | 9b09cd8 | 2016-08-29 14:47:49 -0700 | [diff] [blame] | 1111 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 1112 | |
| 1113 | SkMD5::Digest d1, d2; |
| 1114 | md5(bm1, &d1); |
| 1115 | md5(bm2, &d2); |
| 1116 | REPORTER_ASSERT(r, d1 == d2); |
| 1117 | } |
| 1118 | |
| 1119 | DEF_TEST(Codec_PngRoundTrip, r) { |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 1120 | const char* path = "mandrill_512_q075.jpg"; |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1121 | SkAutoTDelete<SkStream> stream(GetResourceAsStream(path)); |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 1122 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 1123 | |
msarett | f17b71f | 2016-09-12 14:30:03 -0700 | [diff] [blame] | 1124 | SkColorType colorTypesOpaque[] = { |
| 1125 | kRGB_565_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType |
| 1126 | }; |
| 1127 | for (SkColorType colorType : colorTypesOpaque) { |
| 1128 | SkImageInfo newInfo = codec->getInfo().makeColorType(colorType); |
| 1129 | check_round_trip(r, codec.get(), newInfo); |
| 1130 | } |
| 1131 | |
msarett | 9b09cd8 | 2016-08-29 14:47:49 -0700 | [diff] [blame] | 1132 | path = "grayscale.jpg"; |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1133 | stream.reset(GetResourceAsStream(path)); |
msarett | 9b09cd8 | 2016-08-29 14:47:49 -0700 | [diff] [blame] | 1134 | codec.reset(SkCodec::NewFromStream(stream.release())); |
msarett | f17b71f | 2016-09-12 14:30:03 -0700 | [diff] [blame] | 1135 | check_round_trip(r, codec.get(), codec->getInfo()); |
| 1136 | |
| 1137 | path = "yellow_rose.png"; |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1138 | stream.reset(GetResourceAsStream(path)); |
msarett | f17b71f | 2016-09-12 14:30:03 -0700 | [diff] [blame] | 1139 | codec.reset(SkCodec::NewFromStream(stream.release())); |
| 1140 | |
| 1141 | SkColorType colorTypesWithAlpha[] = { |
| 1142 | kRGBA_8888_SkColorType, kBGRA_8888_SkColorType |
| 1143 | }; |
| 1144 | SkAlphaType alphaTypes[] = { |
| 1145 | kUnpremul_SkAlphaType, kPremul_SkAlphaType |
| 1146 | }; |
| 1147 | for (SkColorType colorType : colorTypesWithAlpha) { |
| 1148 | for (SkAlphaType alphaType : alphaTypes) { |
| 1149 | // Set color space to nullptr because color correct premultiplies do not round trip. |
| 1150 | SkImageInfo newInfo = codec->getInfo().makeColorType(colorType) |
| 1151 | .makeAlphaType(alphaType) |
| 1152 | .makeColorSpace(nullptr); |
| 1153 | check_round_trip(r, codec.get(), newInfo); |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | path = "index8.png"; |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1158 | stream.reset(GetResourceAsStream(path)); |
msarett | f17b71f | 2016-09-12 14:30:03 -0700 | [diff] [blame] | 1159 | codec.reset(SkCodec::NewFromStream(stream.release())); |
| 1160 | |
| 1161 | for (SkAlphaType alphaType : alphaTypes) { |
| 1162 | SkImageInfo newInfo = codec->getInfo().makeAlphaType(alphaType) |
| 1163 | .makeColorSpace(nullptr); |
| 1164 | check_round_trip(r, codec.get(), newInfo); |
| 1165 | } |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 1166 | } |
msarett | 2ecc35f | 2016-09-08 11:55:16 -0700 | [diff] [blame] | 1167 | |
| 1168 | static void test_conversion_possible(skiatest::Reporter* r, const char* path, |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 1169 | bool supportsScanlineDecoder, |
| 1170 | bool supportsIncrementalDecoder) { |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1171 | SkAutoTDelete<SkStream> stream(GetResourceAsStream(path)); |
msarett | 2ecc35f | 2016-09-08 11:55:16 -0700 | [diff] [blame] | 1172 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); |
| 1173 | SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType); |
| 1174 | |
| 1175 | SkBitmap bm; |
| 1176 | bm.allocPixels(infoF16); |
| 1177 | SkCodec::Result result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes()); |
| 1178 | REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 1179 | |
| 1180 | result = codec->startScanlineDecode(infoF16); |
| 1181 | if (supportsScanlineDecoder) { |
msarett | 2ecc35f | 2016-09-08 11:55:16 -0700 | [diff] [blame] | 1182 | REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 1183 | } else { |
| 1184 | REPORTER_ASSERT(r, SkCodec::kUnimplemented == result); |
| 1185 | } |
| 1186 | |
| 1187 | result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes()); |
| 1188 | if (supportsIncrementalDecoder) { |
| 1189 | REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result); |
| 1190 | } else { |
| 1191 | REPORTER_ASSERT(r, SkCodec::kUnimplemented == result); |
msarett | 2ecc35f | 2016-09-08 11:55:16 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | infoF16 = infoF16.makeColorSpace(infoF16.colorSpace()->makeLinearGamma()); |
| 1195 | result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes()); |
| 1196 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 1197 | result = codec->startScanlineDecode(infoF16); |
| 1198 | if (supportsScanlineDecoder) { |
msarett | 2ecc35f | 2016-09-08 11:55:16 -0700 | [diff] [blame] | 1199 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 1200 | } else { |
| 1201 | REPORTER_ASSERT(r, SkCodec::kUnimplemented == result); |
| 1202 | } |
| 1203 | |
| 1204 | result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes()); |
| 1205 | if (supportsIncrementalDecoder) { |
| 1206 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 1207 | } else { |
| 1208 | REPORTER_ASSERT(r, SkCodec::kUnimplemented == result); |
msarett | 2ecc35f | 2016-09-08 11:55:16 -0700 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | DEF_TEST(Codec_F16ConversionPossible, r) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 1213 | test_conversion_possible(r, "color_wheel.webp", false, false); |
| 1214 | test_conversion_possible(r, "mandrill_512_q075.jpg", true, false); |
| 1215 | test_conversion_possible(r, "yellow_rose.png", false, true); |
| 1216 | } |
| 1217 | |
| 1218 | // Only rewinds up to a limit. |
| 1219 | class LimitedRewindingStream : public SkStream { |
| 1220 | public: |
| 1221 | static SkStream* Make(const char path[], size_t limit) { |
| 1222 | SkStream* stream = GetResourceAsStream(path); |
| 1223 | if (!stream) { |
| 1224 | return nullptr; |
| 1225 | } |
| 1226 | return new LimitedRewindingStream(stream, limit); |
| 1227 | } |
| 1228 | |
| 1229 | size_t read(void* buffer, size_t size) override { |
| 1230 | const size_t bytes = fStream->read(buffer, size); |
| 1231 | fPosition += bytes; |
| 1232 | return bytes; |
| 1233 | } |
| 1234 | |
| 1235 | bool isAtEnd() const override { |
| 1236 | return fStream->isAtEnd(); |
| 1237 | } |
| 1238 | |
| 1239 | bool rewind() override { |
| 1240 | if (fPosition <= fLimit && fStream->rewind()) { |
| 1241 | fPosition = 0; |
| 1242 | return true; |
| 1243 | } |
| 1244 | |
| 1245 | return false; |
| 1246 | } |
| 1247 | |
| 1248 | private: |
| 1249 | SkAutoTDelete<SkStream> fStream; |
| 1250 | const size_t fLimit; |
| 1251 | size_t fPosition; |
| 1252 | |
| 1253 | LimitedRewindingStream(SkStream* stream, size_t limit) |
| 1254 | : fStream(stream) |
| 1255 | , fLimit(limit) |
| 1256 | , fPosition(0) |
| 1257 | { |
| 1258 | SkASSERT(fStream); |
| 1259 | } |
| 1260 | }; |
| 1261 | |
| 1262 | DEF_TEST(Codec_fallBack, r) { |
| 1263 | // SkAndroidCodec needs to be able to fall back to scanline decoding |
| 1264 | // if incremental decoding does not work. Make sure this does not |
| 1265 | // require a rewind. |
| 1266 | |
| 1267 | // Formats that currently do not support incremental decoding |
| 1268 | auto files = { |
| 1269 | "box.gif", |
| 1270 | "CMYK.jpg", |
| 1271 | "color_wheel.ico", |
| 1272 | "mandrill.wbmp", |
| 1273 | "randPixels.bmp", |
| 1274 | }; |
| 1275 | for (auto file : files) { |
| 1276 | SkStream* stream = LimitedRewindingStream::Make(file, 14); |
| 1277 | if (!stream) { |
| 1278 | SkDebugf("Missing resources (%s). Set --resourcePath.\n", file); |
| 1279 | return; |
| 1280 | } |
| 1281 | |
| 1282 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream)); |
| 1283 | if (!codec) { |
| 1284 | ERRORF(r, "Failed to create codec for %s,", file); |
| 1285 | continue; |
| 1286 | } |
| 1287 | |
| 1288 | SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType); |
| 1289 | SkBitmap bm; |
| 1290 | bm.allocPixels(info); |
| 1291 | |
| 1292 | if (SkCodec::kUnimplemented != codec->startIncrementalDecode(info, bm.getPixels(), |
| 1293 | bm.rowBytes())) { |
| 1294 | ERRORF(r, "Is scanline decoding now implemented for %s?", file); |
| 1295 | continue; |
| 1296 | } |
| 1297 | |
| 1298 | // Scanline decoding should not require a rewind. |
| 1299 | SkCodec::Result result = codec->startScanlineDecode(info); |
| 1300 | if (SkCodec::kSuccess != result) { |
| 1301 | ERRORF(r, "Scanline decoding failed for %s with %i", file, result); |
| 1302 | } |
| 1303 | } |
msarett | 2ecc35f | 2016-09-08 11:55:16 -0700 | [diff] [blame] | 1304 | } |