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