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