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