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