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