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