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 | |
| 8 | #include "Resources.h" |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 9 | #include "SkAndroidCodec.h" |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 10 | #include "SkBitmap.h" |
| 11 | #include "SkCodec.h" |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 12 | #include "SkData.h" |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 13 | #include "SkImageDecoder.h" |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 14 | #include "SkMD5.h" |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 15 | #include "SkRandom.h" |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 16 | #include "SkStream.h" |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 17 | #include "SkStreamPriv.h" |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 18 | #include "SkPngChunkReader.h" |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 19 | #include "Test.h" |
| 20 | |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 21 | #include "png.h" |
| 22 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 23 | static SkStreamAsset* resource(const char path[]) { |
| 24 | SkString fullPath = GetResourcePath(path); |
| 25 | return SkStream::NewFromFile(fullPath.c_str()); |
| 26 | } |
| 27 | |
| 28 | static void md5(const SkBitmap& bm, SkMD5::Digest* digest) { |
| 29 | SkAutoLockPixels autoLockPixels(bm); |
| 30 | SkASSERT(bm.getPixels()); |
| 31 | SkMD5 md5; |
| 32 | size_t rowLen = bm.info().bytesPerPixel() * bm.width(); |
| 33 | for (int y = 0; y < bm.height(); ++y) { |
| 34 | md5.update(static_cast<uint8_t*>(bm.getAddr(0, y)), rowLen); |
| 35 | } |
| 36 | md5.finish(*digest); |
| 37 | } |
| 38 | |
scroggo | 9b2cdbf | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Compute the digest for bm and compare it to a known good digest. |
| 41 | * @param r Reporter to assert that bm's digest matches goodDigest. |
| 42 | * @param goodDigest The known good digest to compare to. |
| 43 | * @param bm The bitmap to test. |
| 44 | */ |
| 45 | static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest, |
| 46 | const SkBitmap& bm) { |
| 47 | SkMD5::Digest digest; |
| 48 | md5(bm, &digest); |
| 49 | REPORTER_ASSERT(r, digest == goodDigest); |
| 50 | } |
| 51 | |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Test decoding an SkCodec to a particular SkImageInfo. |
| 54 | * |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 55 | * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr, |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 56 | * the resulting decode should match. |
| 57 | */ |
| 58 | static void test_info(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info, |
| 59 | SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) { |
| 60 | SkBitmap bm; |
| 61 | bm.allocPixels(info); |
| 62 | SkAutoLockPixels autoLockPixels(bm); |
| 63 | |
| 64 | SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes()); |
| 65 | REPORTER_ASSERT(r, result == expectedResult); |
| 66 | |
| 67 | if (goodDigest) { |
| 68 | compare_to_good_digest(r, *goodDigest, bm); |
| 69 | } |
| 70 | } |
| 71 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 72 | static void test_android_info(skiatest::Reporter* r, SkAndroidCodec* codec, const SkImageInfo& info, |
| 73 | SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) { |
| 74 | SkBitmap bm; |
| 75 | bm.allocPixels(info); |
| 76 | SkAutoLockPixels autoLockPixels(bm); |
| 77 | |
| 78 | SkCodec::Result result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes()); |
| 79 | REPORTER_ASSERT(r, result == expectedResult); |
| 80 | |
| 81 | if (goodDigest) { |
| 82 | compare_to_good_digest(r, *goodDigest, bm); |
| 83 | } |
| 84 | } |
| 85 | |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 86 | SkIRect generate_random_subset(SkRandom* rand, int w, int h) { |
| 87 | SkIRect rect; |
| 88 | do { |
| 89 | rect.fLeft = rand->nextRangeU(0, w); |
| 90 | rect.fTop = rand->nextRangeU(0, h); |
| 91 | rect.fRight = rand->nextRangeU(0, w); |
| 92 | rect.fBottom = rand->nextRangeU(0, h); |
| 93 | rect.sort(); |
| 94 | } while (rect.isEmpty()); |
| 95 | return rect; |
| 96 | } |
| 97 | |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 98 | static void test_codec(skiatest::Reporter* r, SkCodec* codec, SkBitmap& bm, const SkImageInfo& info, |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 99 | const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest, |
| 100 | const SkMD5::Digest* goodDigest) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 101 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 102 | REPORTER_ASSERT(r, info.dimensions() == size); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 103 | bm.allocPixels(info); |
| 104 | SkAutoLockPixels autoLockPixels(bm); |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 105 | |
| 106 | SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 107 | REPORTER_ASSERT(r, result == expectedResult); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 108 | |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 109 | md5(bm, digest); |
| 110 | if (goodDigest) { |
| 111 | REPORTER_ASSERT(r, *digest == *goodDigest); |
| 112 | } |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 113 | |
msarett | 8ff6ca6 | 2015-09-18 12:06:04 -0700 | [diff] [blame] | 114 | { |
| 115 | // Test decoding to 565 |
| 116 | SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType); |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 117 | SkCodec::Result expected565 = info.alphaType() == kOpaque_SkAlphaType ? |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 118 | expectedResult : SkCodec::kInvalidConversion; |
| 119 | test_info(r, codec, info565, expected565, nullptr); |
msarett | 8ff6ca6 | 2015-09-18 12:06:04 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // Verify that re-decoding gives the same result. It is interesting to check this after |
| 123 | // a decode to 565, since choosing to decode to 565 may result in some of the decode |
| 124 | // options being modified. These options should return to their defaults on another |
| 125 | // decode to kN32, so the new digest should match the old digest. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 126 | test_info(r, codec, info, expectedResult, digest); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 127 | |
| 128 | { |
| 129 | // Check alpha type conversions |
| 130 | if (info.alphaType() == kOpaque_SkAlphaType) { |
| 131 | test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 132 | SkCodec::kInvalidConversion, nullptr); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 133 | test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 134 | SkCodec::kInvalidConversion, nullptr); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 135 | } else { |
| 136 | // Decoding to opaque should fail |
| 137 | test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 138 | SkCodec::kInvalidConversion, nullptr); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 139 | SkAlphaType otherAt = info.alphaType(); |
| 140 | if (kPremul_SkAlphaType == otherAt) { |
| 141 | otherAt = kUnpremul_SkAlphaType; |
| 142 | } else { |
| 143 | otherAt = kPremul_SkAlphaType; |
| 144 | } |
| 145 | // The other non-opaque alpha type should always succeed, but not match. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 146 | test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 149 | } |
| 150 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 151 | static void test_android_codec(skiatest::Reporter* r, SkAndroidCodec* codec, SkBitmap& bm, |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 152 | const SkImageInfo& info, const SkISize& size, SkCodec::Result expectedResult, |
| 153 | SkMD5::Digest* digest, const SkMD5::Digest* goodDigest) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 154 | |
| 155 | REPORTER_ASSERT(r, info.dimensions() == size); |
| 156 | bm.allocPixels(info); |
| 157 | SkAutoLockPixels autoLockPixels(bm); |
| 158 | |
| 159 | SkCodec::Result result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes()); |
| 160 | REPORTER_ASSERT(r, result == expectedResult); |
| 161 | |
| 162 | md5(bm, digest); |
| 163 | if (goodDigest) { |
| 164 | REPORTER_ASSERT(r, *digest == *goodDigest); |
| 165 | } |
| 166 | |
| 167 | { |
| 168 | // Test decoding to 565 |
| 169 | SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType); |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 170 | SkCodec::Result expected565 = info.alphaType() == kOpaque_SkAlphaType ? |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 171 | expectedResult : SkCodec::kInvalidConversion; |
| 172 | test_android_info(r, codec, info565, expected565, nullptr); |
| 173 | } |
| 174 | |
| 175 | // Verify that re-decoding gives the same result. It is interesting to check this after |
| 176 | // a decode to 565, since choosing to decode to 565 may result in some of the decode |
| 177 | // options being modified. These options should return to their defaults on another |
| 178 | // decode to kN32, so the new digest should match the old digest. |
| 179 | test_android_info(r, codec, info, expectedResult, digest); |
| 180 | |
| 181 | { |
| 182 | // Check alpha type conversions |
| 183 | if (info.alphaType() == kOpaque_SkAlphaType) { |
| 184 | test_android_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType), |
| 185 | SkCodec::kInvalidConversion, nullptr); |
| 186 | test_android_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType), |
| 187 | SkCodec::kInvalidConversion, nullptr); |
| 188 | } else { |
| 189 | // Decoding to opaque should fail |
| 190 | test_android_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType), |
| 191 | SkCodec::kInvalidConversion, nullptr); |
| 192 | SkAlphaType otherAt = info.alphaType(); |
| 193 | if (kPremul_SkAlphaType == otherAt) { |
| 194 | otherAt = kUnpremul_SkAlphaType; |
| 195 | } else { |
| 196 | otherAt = kPremul_SkAlphaType; |
| 197 | } |
| 198 | // The other non-opaque alpha type should always succeed, but not match. |
| 199 | test_android_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 204 | // FIXME: SkScaledCodec is currently only supported for types used by BRD |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 205 | // https://bug.skia.org/4428 |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 206 | static bool supports_scaled_codec(const char path[]) { |
| 207 | static const char* const exts[] = { |
| 208 | "jpg", "jpeg", "png", "webp" |
| 209 | "JPG", "JPEG", "PNG", "WEBP" |
| 210 | }; |
| 211 | |
| 212 | for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) { |
| 213 | if (SkStrEndsWith(path, exts[i])) { |
| 214 | return true; |
| 215 | } |
| 216 | } |
| 217 | return false; |
| 218 | } |
| 219 | |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 220 | static void check(skiatest::Reporter* r, |
| 221 | const char path[], |
| 222 | SkISize size, |
| 223 | bool supportsScanlineDecoding, |
| 224 | bool supportsSubsetDecoding, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 225 | bool supportsIncomplete = true) { |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 226 | |
| 227 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 228 | if (!stream) { |
| 229 | SkDebugf("Missing resource '%s'\n", path); |
| 230 | return; |
| 231 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 232 | |
| 233 | SkAutoTDelete<SkCodec> codec(nullptr); |
| 234 | bool isIncomplete = supportsIncomplete; |
| 235 | if (isIncomplete) { |
| 236 | size_t size = stream->getLength(); |
| 237 | SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3))); |
| 238 | codec.reset(SkCodec::NewFromData(data)); |
| 239 | } else { |
| 240 | codec.reset(SkCodec::NewFromStream(stream.detach())); |
| 241 | } |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 242 | if (!codec) { |
| 243 | ERRORF(r, "Unable to decode '%s'", path); |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | // Test full image decodes with SkCodec |
| 248 | SkMD5::Digest codecDigest; |
| 249 | SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType); |
| 250 | SkBitmap bm; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 251 | SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess; |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 252 | test_codec(r, codec, bm, info, size, expectedResult, &codecDigest, nullptr); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 253 | |
| 254 | // Scanline decoding follows. |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 255 | // Need to call startScanlineDecode() first. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 256 | REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 257 | == 0); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 258 | REPORTER_ASSERT(r, codec->skipScanlines(1) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 259 | == 0); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 260 | |
| 261 | const SkCodec::Result startResult = codec->startScanlineDecode(info); |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 262 | if (supportsScanlineDecoding) { |
| 263 | bm.eraseColor(SK_ColorYELLOW); |
msarett | c0e80c1 | 2015-07-01 06:50:35 -0700 | [diff] [blame] | 264 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 265 | REPORTER_ASSERT(r, startResult == SkCodec::kSuccess); |
scroggo | 9b2cdbf | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 266 | |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 267 | for (int y = 0; y < info.height(); y++) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 268 | const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0); |
| 269 | if (!isIncomplete) { |
| 270 | REPORTER_ASSERT(r, 1 == lines); |
| 271 | } |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 272 | } |
| 273 | // verify that scanline decoding gives the same result. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 274 | if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) { |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 275 | compare_to_good_digest(r, codecDigest, bm); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 276 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 277 | |
| 278 | // Cannot continue to decode scanlines beyond the end |
| 279 | REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 280 | == 0); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 281 | |
| 282 | // Interrupting a scanline decode with a full decode starts from |
| 283 | // scratch |
| 284 | REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 285 | const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0); |
| 286 | if (!isIncomplete) { |
| 287 | REPORTER_ASSERT(r, lines == 1); |
| 288 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 289 | REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes()) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 290 | == expectedResult); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 291 | REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 292 | == 0); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 293 | REPORTER_ASSERT(r, codec->skipScanlines(1) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 294 | == 0); |
msarett | 80803ff | 2015-10-16 10:54:12 -0700 | [diff] [blame] | 295 | |
| 296 | // Test partial scanline decodes |
| 297 | if (supports_scaled_codec(path) && info.width() >= 3) { |
| 298 | SkCodec::Options options; |
| 299 | int width = info.width(); |
| 300 | int height = info.height(); |
| 301 | SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height); |
| 302 | options.fSubset = ⊂ |
| 303 | |
| 304 | const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options, |
| 305 | nullptr, nullptr); |
| 306 | REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess); |
| 307 | |
| 308 | for (int y = 0; y < height; y++) { |
| 309 | const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0); |
| 310 | if (!isIncomplete) { |
| 311 | REPORTER_ASSERT(r, 1 == lines); |
| 312 | } |
| 313 | } |
| 314 | } |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 315 | } else { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 316 | REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 317 | } |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 318 | |
| 319 | // The rest of this function tests decoding subsets, and will decode an arbitrary number of |
| 320 | // random subsets. |
| 321 | // Do not attempt to decode subsets of an image of only once pixel, since there is no |
| 322 | // meaningful subset. |
| 323 | if (size.width() * size.height() == 1) { |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | SkRandom rand; |
| 328 | SkIRect subset; |
| 329 | SkCodec::Options opts; |
| 330 | opts.fSubset = ⊂ |
| 331 | for (int i = 0; i < 5; i++) { |
| 332 | subset = generate_random_subset(&rand, size.width(), size.height()); |
| 333 | SkASSERT(!subset.isEmpty()); |
| 334 | const bool supported = codec->getValidSubset(&subset); |
| 335 | REPORTER_ASSERT(r, supported == supportsSubsetDecoding); |
| 336 | |
| 337 | SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height()); |
| 338 | SkBitmap bm; |
| 339 | bm.allocPixels(subsetInfo); |
| 340 | const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 341 | &opts, nullptr, nullptr); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 342 | |
| 343 | if (supportsSubsetDecoding) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 344 | REPORTER_ASSERT(r, result == expectedResult); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 345 | // Webp is the only codec that supports subsets, and it will have modified the subset |
| 346 | // to have even left/top. |
| 347 | REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop)); |
| 348 | } else { |
| 349 | // No subsets will work. |
| 350 | REPORTER_ASSERT(r, result == SkCodec::kUnimplemented); |
| 351 | } |
| 352 | } |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 353 | |
| 354 | // SkScaledCodec tests |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 355 | if ((supportsScanlineDecoding || supportsSubsetDecoding) && supports_scaled_codec(path)) { |
| 356 | |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 357 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 358 | if (!stream) { |
| 359 | SkDebugf("Missing resource '%s'\n", path); |
| 360 | return; |
| 361 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 362 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 363 | SkAutoTDelete<SkAndroidCodec> codec(nullptr); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 364 | if (isIncomplete) { |
| 365 | size_t size = stream->getLength(); |
| 366 | SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3))); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 367 | codec.reset(SkAndroidCodec::NewFromData(data)); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 368 | } else { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 369 | codec.reset(SkAndroidCodec::NewFromStream(stream.detach())); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 370 | } |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 371 | if (!codec) { |
| 372 | ERRORF(r, "Unable to decode '%s'", path); |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | SkBitmap bm; |
| 377 | SkMD5::Digest scaledCodecDigest; |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 378 | test_android_codec(r, codec, bm, info, size, expectedResult, |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 379 | &scaledCodecDigest, &codecDigest); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | // If we've just tested incomplete decodes, let's run the same test again on full decodes. |
| 383 | if (isIncomplete) { |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 384 | check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false); |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 385 | } |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | DEF_TEST(Codec, r) { |
| 389 | // WBMP |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 390 | check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 391 | |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 392 | // WEBP |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 393 | check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true); |
| 394 | check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true); |
| 395 | check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 396 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 397 | // BMP |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 398 | check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 399 | |
| 400 | // ICO |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 401 | // FIXME: We are not ready to test incomplete ICOs |
msarett | 68b204e | 2015-04-01 12:09:21 -0700 | [diff] [blame] | 402 | // These two tests examine interestingly different behavior: |
| 403 | // Decodes an embedded BMP image |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 404 | check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false); |
msarett | 68b204e | 2015-04-01 12:09:21 -0700 | [diff] [blame] | 405 | // Decodes an embedded PNG image |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 406 | check(r, "google_chrome.ico", SkISize::Make(256, 256), true, false, false); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 407 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 408 | // GIF |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 409 | // FIXME: We are not ready to test incomplete GIFs |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 410 | check(r, "box.gif", SkISize::Make(200, 55), true, false, false); |
| 411 | check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, false); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 412 | // randPixels.gif is too small to test incomplete |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 413 | check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, false); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 414 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 415 | // JPG |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 416 | check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 417 | check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 418 | // grayscale.jpg is too small to test incomplete |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 419 | check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 420 | check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 421 | // randPixels.jpg is too small to test incomplete |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 422 | check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 423 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 424 | // PNG |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 425 | check(r, "arrow.png", SkISize::Make(187, 312), true, false, false); |
| 426 | check(r, "baby_tux.png", SkISize::Make(240, 246), true, false, false); |
| 427 | check(r, "color_wheel.png", SkISize::Make(128, 128), true, false, false); |
| 428 | check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false, false); |
| 429 | check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false, false); |
| 430 | check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false, false); |
| 431 | check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false, false); |
| 432 | check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false, false); |
| 433 | check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false, false); |
| 434 | check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false, false); |
| 435 | check(r, "plane.png", SkISize::Make(250, 126), true, false, false); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 436 | // FIXME: We are not ready to test incomplete interlaced pngs |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 437 | check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false, false); |
| 438 | check(r, "randPixels.png", SkISize::Make(8, 8), true, false, false); |
| 439 | check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false, false); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 440 | } |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 441 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 442 | // Test interlaced PNG in stripes, similar to DM's kStripe_Mode |
| 443 | DEF_TEST(Codec_stripes, r) { |
| 444 | const char * path = "plane_interlaced.png"; |
| 445 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 446 | if (!stream) { |
| 447 | SkDebugf("Missing resource '%s'\n", path); |
| 448 | } |
| 449 | |
| 450 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach())); |
| 451 | REPORTER_ASSERT(r, codec); |
| 452 | |
| 453 | if (!codec) { |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | switch (codec->getScanlineOrder()) { |
| 458 | case SkCodec::kBottomUp_SkScanlineOrder: |
| 459 | case SkCodec::kOutOfOrder_SkScanlineOrder: |
| 460 | ERRORF(r, "This scanline order will not match the original."); |
| 461 | return; |
| 462 | default: |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | // Baseline for what the image should look like, using N32. |
| 467 | const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType); |
| 468 | |
| 469 | SkBitmap bm; |
| 470 | bm.allocPixels(info); |
| 471 | SkAutoLockPixels autoLockPixels(bm); |
| 472 | SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes()); |
| 473 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 474 | |
| 475 | SkMD5::Digest digest; |
| 476 | md5(bm, &digest); |
| 477 | |
| 478 | // Now decode in stripes |
| 479 | const int height = info.height(); |
| 480 | const int numStripes = 4; |
| 481 | int stripeHeight; |
| 482 | int remainingLines; |
| 483 | SkTDivMod(height, numStripes, &stripeHeight, &remainingLines); |
| 484 | |
| 485 | bm.eraseColor(SK_ColorYELLOW); |
| 486 | |
| 487 | result = codec->startScanlineDecode(info); |
| 488 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 489 | |
| 490 | // Odd stripes |
| 491 | for (int i = 1; i < numStripes; i += 2) { |
| 492 | // Skip the even stripes |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 493 | bool skipResult = codec->skipScanlines(stripeHeight); |
| 494 | REPORTER_ASSERT(r, skipResult); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 495 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 496 | int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 497 | bm.rowBytes()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 498 | REPORTER_ASSERT(r, linesDecoded == stripeHeight); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | // Even stripes |
| 502 | result = codec->startScanlineDecode(info); |
| 503 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 504 | |
| 505 | for (int i = 0; i < numStripes; i += 2) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 506 | int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 507 | bm.rowBytes()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 508 | REPORTER_ASSERT(r, linesDecoded == stripeHeight); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 509 | |
| 510 | // Skip the odd stripes |
| 511 | if (i + 1 < numStripes) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 512 | bool skipResult = codec->skipScanlines(stripeHeight); |
| 513 | REPORTER_ASSERT(r, skipResult); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
| 517 | // Remainder at the end |
| 518 | if (remainingLines > 0) { |
| 519 | result = codec->startScanlineDecode(info); |
| 520 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 521 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 522 | bool skipResult = codec->skipScanlines(height - remainingLines); |
| 523 | REPORTER_ASSERT(r, skipResult); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 524 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 525 | int linesDecoded = codec->getScanlines(bm.getAddr(0, height - remainingLines), |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 526 | remainingLines, bm.rowBytes()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 527 | REPORTER_ASSERT(r, linesDecoded == remainingLines); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | compare_to_good_digest(r, digest, bm); |
| 531 | } |
| 532 | |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 533 | 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] | 534 | // Neither of these calls should return a codec. Bots should catch us if we leaked anything. |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 535 | SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false)); |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 536 | REPORTER_ASSERT(r, !codec); |
| 537 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 538 | SkAndroidCodec* androidCodec = |
| 539 | SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false)); |
| 540 | REPORTER_ASSERT(r, !androidCodec); |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | // Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream, |
| 544 | // even on failure. Test some bad streams. |
| 545 | DEF_TEST(Codec_leaks, r) { |
| 546 | // No codec should claim this as their format, so this tests SkCodec::NewFromStream. |
| 547 | const char nonSupportedStream[] = "hello world"; |
| 548 | // The other strings should look like the beginning of a file type, so we'll call some |
| 549 | // internal version of NewFromStream, which must also delete the stream on failure. |
| 550 | const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }; |
| 551 | const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF }; |
| 552 | const char emptyWebp[] = "RIFF1234WEBPVP"; |
| 553 | const char emptyBmp[] = { 'B', 'M' }; |
| 554 | const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' }; |
| 555 | const char emptyGif[] = "GIFVER"; |
| 556 | |
| 557 | test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream)); |
| 558 | test_invalid_stream(r, emptyPng, sizeof(emptyPng)); |
| 559 | test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg)); |
| 560 | test_invalid_stream(r, emptyWebp, sizeof(emptyWebp)); |
| 561 | test_invalid_stream(r, emptyBmp, sizeof(emptyBmp)); |
| 562 | test_invalid_stream(r, emptyIco, sizeof(emptyIco)); |
| 563 | test_invalid_stream(r, emptyGif, sizeof(emptyGif)); |
| 564 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 565 | |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 566 | DEF_TEST(Codec_null, r) { |
| 567 | // Attempting to create an SkCodec or an SkScaledCodec with null should not |
| 568 | // crash. |
| 569 | SkCodec* codec = SkCodec::NewFromStream(nullptr); |
| 570 | REPORTER_ASSERT(r, !codec); |
| 571 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 572 | SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr); |
| 573 | REPORTER_ASSERT(r, !androidCodec); |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 574 | } |
| 575 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 576 | static void test_dimensions(skiatest::Reporter* r, const char path[]) { |
| 577 | // Create the codec from the resource file |
| 578 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 579 | if (!stream) { |
| 580 | SkDebugf("Missing resource '%s'\n", path); |
| 581 | return; |
| 582 | } |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 583 | SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.detach())); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 584 | if (!codec) { |
| 585 | ERRORF(r, "Unable to create codec '%s'", path); |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | // Check that the decode is successful for a variety of scales |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 590 | for (int sampleSize = 1; sampleSize < 32; sampleSize++) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 591 | // Scale the output dimensions |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 592 | SkISize scaledDims = codec->getSampledDimensions(sampleSize); |
msarett | b32758a | 2015-08-18 13:22:46 -0700 | [diff] [blame] | 593 | SkImageInfo scaledInfo = codec->getInfo() |
| 594 | .makeWH(scaledDims.width(), scaledDims.height()) |
| 595 | .makeColorType(kN32_SkColorType); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 596 | |
| 597 | // Set up for the decode |
| 598 | size_t rowBytes = scaledDims.width() * sizeof(SkPMColor); |
| 599 | size_t totalBytes = scaledInfo.getSafeSize(rowBytes); |
| 600 | SkAutoTMalloc<SkPMColor> pixels(totalBytes); |
| 601 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 602 | SkAndroidCodec::AndroidOptions options; |
| 603 | options.fSampleSize = sampleSize; |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 604 | SkCodec::Result result = |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 605 | codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 606 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
| 610 | // Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes |
| 611 | DEF_TEST(Codec_Dimensions, r) { |
| 612 | // JPG |
| 613 | test_dimensions(r, "CMYK.jpg"); |
| 614 | test_dimensions(r, "color_wheel.jpg"); |
| 615 | test_dimensions(r, "grayscale.jpg"); |
| 616 | test_dimensions(r, "mandrill_512_q075.jpg"); |
| 617 | test_dimensions(r, "randPixels.jpg"); |
msarett | b32758a | 2015-08-18 13:22:46 -0700 | [diff] [blame] | 618 | |
| 619 | // Decoding small images with very large scaling factors is a potential |
| 620 | // source of bugs and crashes. We disable these tests in Gold because |
| 621 | // tiny images are not very useful to look at. |
| 622 | // Here we make sure that we do not crash or access illegal memory when |
| 623 | // performing scaled decodes on small images. |
| 624 | test_dimensions(r, "1x1.png"); |
| 625 | test_dimensions(r, "2x2.png"); |
| 626 | test_dimensions(r, "3x3.png"); |
| 627 | test_dimensions(r, "3x1.png"); |
| 628 | test_dimensions(r, "1x1.png"); |
| 629 | test_dimensions(r, "16x1.png"); |
| 630 | test_dimensions(r, "1x16.png"); |
| 631 | test_dimensions(r, "mandrill_16.png"); |
| 632 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 633 | } |
| 634 | |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 635 | static void test_invalid(skiatest::Reporter* r, const char path[]) { |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 636 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 637 | if (!stream) { |
| 638 | SkDebugf("Missing resource '%s'\n", path); |
| 639 | return; |
| 640 | } |
| 641 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 642 | REPORTER_ASSERT(r, nullptr == codec); |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 643 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 644 | |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 645 | DEF_TEST(Codec_Empty, r) { |
| 646 | // Test images that should not be able to create a codec |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 647 | test_invalid(r, "empty_images/zero-dims.gif"); |
| 648 | test_invalid(r, "empty_images/zero-embedded.ico"); |
| 649 | test_invalid(r, "empty_images/zero-width.bmp"); |
| 650 | test_invalid(r, "empty_images/zero-height.bmp"); |
| 651 | test_invalid(r, "empty_images/zero-width.jpg"); |
| 652 | test_invalid(r, "empty_images/zero-height.jpg"); |
| 653 | test_invalid(r, "empty_images/zero-width.png"); |
| 654 | test_invalid(r, "empty_images/zero-height.png"); |
| 655 | test_invalid(r, "empty_images/zero-width.wbmp"); |
| 656 | test_invalid(r, "empty_images/zero-height.wbmp"); |
| 657 | // This image is an ico with an embedded mask-bmp. This is illegal. |
| 658 | test_invalid(r, "invalid_images/mask-bmp-ico.ico"); |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 659 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 660 | |
| 661 | static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) { |
| 662 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 663 | if (!stream) { |
| 664 | SkDebugf("Missing resource '%s'\n", path); |
| 665 | return; |
| 666 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 667 | SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.detach())); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 668 | |
| 669 | // This should return kSuccess because kIndex8 is supported. |
| 670 | SkPMColor colorStorage[256]; |
| 671 | int colorCount; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 672 | SkCodec::Result result = decoder->startScanlineDecode( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 673 | decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 674 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 675 | // The rest of the test is uninteresting if kIndex8 is not supported |
| 676 | if (SkCodec::kSuccess != result) { |
| 677 | return; |
| 678 | } |
| 679 | |
| 680 | // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid |
| 681 | // colorPtr and a valid colorCountPtr. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 682 | result = decoder->startScanlineDecode( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 683 | decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 684 | REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 685 | result = decoder->startScanlineDecode( |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 686 | decoder->getInfo().makeColorType(kIndex_8_SkColorType)); |
| 687 | REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); |
| 688 | } |
| 689 | |
| 690 | DEF_TEST(Codec_Params, r) { |
| 691 | test_invalid_parameters(r, "index8.png"); |
| 692 | test_invalid_parameters(r, "mandrill.wbmp"); |
| 693 | } |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 694 | |
| 695 | static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) { |
| 696 | SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr); |
| 697 | if (!sk_stream->write(data, len)) { |
| 698 | png_error(png_ptr, "sk_write_fn Error!"); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
| 703 | DEF_TEST(Codec_pngChunkReader, r) { |
| 704 | // Create a dummy bitmap. Use unpremul RGBA for libpng. |
| 705 | SkBitmap bm; |
| 706 | const int w = 1; |
| 707 | const int h = 1; |
| 708 | const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, |
| 709 | kUnpremul_SkAlphaType); |
| 710 | bm.setInfo(bmInfo); |
| 711 | bm.allocPixels(); |
| 712 | bm.eraseColor(SK_ColorBLUE); |
| 713 | SkMD5::Digest goodDigest; |
| 714 | md5(bm, &goodDigest); |
| 715 | |
| 716 | // Write to a png file. |
| 717 | png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
| 718 | REPORTER_ASSERT(r, png); |
| 719 | if (!png) { |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | png_infop info = png_create_info_struct(png); |
| 724 | REPORTER_ASSERT(r, info); |
| 725 | if (!info) { |
| 726 | png_destroy_write_struct(&png, nullptr); |
| 727 | return; |
| 728 | } |
| 729 | |
| 730 | if (setjmp(png_jmpbuf(png))) { |
| 731 | ERRORF(r, "failed writing png"); |
| 732 | png_destroy_write_struct(&png, &info); |
| 733 | return; |
| 734 | } |
| 735 | |
| 736 | SkDynamicMemoryWStream wStream; |
| 737 | png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr); |
| 738 | |
| 739 | png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8, |
| 740 | PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, |
| 741 | PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
| 742 | |
| 743 | // Create some chunks that match the Android framework's use. |
| 744 | static png_unknown_chunk gUnknowns[] = { |
| 745 | { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_PLTE }, |
| 746 | { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_PLTE }, |
| 747 | { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_PLTE }, |
| 748 | }; |
| 749 | |
| 750 | png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3); |
| 751 | png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns)); |
| 752 | #if PNG_LIBPNG_VER < 10600 |
| 753 | /* Deal with unknown chunk location bug in 1.5.x and earlier */ |
| 754 | png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_PLTE); |
| 755 | png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_PLTE); |
| 756 | #endif |
| 757 | |
| 758 | png_write_info(png, info); |
| 759 | |
| 760 | for (int j = 0; j < h; j++) { |
| 761 | png_bytep row = (png_bytep)(bm.getAddr(0, j)); |
| 762 | png_write_rows(png, &row, 1); |
| 763 | } |
| 764 | png_write_end(png, info); |
| 765 | png_destroy_write_struct(&png, &info); |
| 766 | |
| 767 | class ChunkReader : public SkPngChunkReader { |
| 768 | public: |
| 769 | ChunkReader(skiatest::Reporter* r) |
| 770 | : fReporter(r) |
| 771 | { |
| 772 | this->reset(); |
| 773 | } |
| 774 | |
| 775 | bool readChunk(const char tag[], const void* data, size_t length) override { |
| 776 | for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) { |
| 777 | if (!strcmp(tag, (const char*) gUnknowns[i].name)) { |
| 778 | // Tag matches. This should have been the first time we see it. |
| 779 | REPORTER_ASSERT(fReporter, !fSeen[i]); |
| 780 | fSeen[i] = true; |
| 781 | |
| 782 | // Data and length should match |
| 783 | REPORTER_ASSERT(fReporter, length == gUnknowns[i].size); |
| 784 | REPORTER_ASSERT(fReporter, !strcmp((const char*) data, |
| 785 | (const char*) gUnknowns[i].data)); |
| 786 | return true; |
| 787 | } |
| 788 | } |
| 789 | ERRORF(fReporter, "Saw an unexpected unknown chunk."); |
| 790 | return true; |
| 791 | } |
| 792 | |
| 793 | bool allHaveBeenSeen() { |
| 794 | bool ret = true; |
| 795 | for (auto seen : fSeen) { |
| 796 | ret &= seen; |
| 797 | } |
| 798 | return ret; |
| 799 | } |
| 800 | |
| 801 | void reset() { |
| 802 | sk_bzero(fSeen, sizeof(fSeen)); |
| 803 | } |
| 804 | |
| 805 | private: |
| 806 | skiatest::Reporter* fReporter; // Unowned |
| 807 | bool fSeen[3]; |
| 808 | }; |
| 809 | |
| 810 | ChunkReader chunkReader(r); |
| 811 | |
| 812 | // Now read the file with SkCodec. |
| 813 | SkAutoTUnref<SkData> data(wStream.copyToData()); |
| 814 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data, &chunkReader)); |
| 815 | REPORTER_ASSERT(r, codec); |
| 816 | if (!codec) { |
| 817 | return; |
| 818 | } |
| 819 | |
| 820 | // Now compare to the original. |
| 821 | SkBitmap decodedBm; |
| 822 | decodedBm.setInfo(codec->getInfo()); |
| 823 | decodedBm.allocPixels(); |
| 824 | SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), |
| 825 | decodedBm.rowBytes()); |
| 826 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 827 | |
| 828 | if (decodedBm.colorType() != bm.colorType()) { |
| 829 | SkBitmap tmp; |
| 830 | bool success = decodedBm.copyTo(&tmp, bm.colorType()); |
| 831 | REPORTER_ASSERT(r, success); |
| 832 | if (!success) { |
| 833 | return; |
| 834 | } |
| 835 | |
| 836 | tmp.swap(decodedBm); |
| 837 | } |
| 838 | |
| 839 | compare_to_good_digest(r, goodDigest, decodedBm); |
| 840 | REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen()); |
| 841 | |
| 842 | // Decoding again will read the chunks again. |
| 843 | chunkReader.reset(); |
| 844 | REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen()); |
| 845 | result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes()); |
| 846 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 847 | REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen()); |
| 848 | } |
| 849 | #endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 850 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 851 | // Stream that can only peek up to a limit |
| 852 | class LimitedPeekingMemStream : public SkStream { |
| 853 | public: |
| 854 | LimitedPeekingMemStream(SkData* data, size_t limit) |
| 855 | : fStream(data) |
| 856 | , fLimit(limit) {} |
| 857 | |
| 858 | size_t peek(void* buf, size_t bytes) const override { |
| 859 | return fStream.peek(buf, SkTMin(bytes, fLimit)); |
| 860 | } |
| 861 | size_t read(void* buf, size_t bytes) override { |
| 862 | return fStream.read(buf, bytes); |
| 863 | } |
| 864 | bool rewind() override { |
| 865 | return fStream.rewind(); |
| 866 | } |
| 867 | bool isAtEnd() const override { |
| 868 | return false; |
| 869 | } |
| 870 | private: |
| 871 | SkMemoryStream fStream; |
| 872 | const size_t fLimit; |
| 873 | }; |
| 874 | |
| 875 | // Test that even if webp_parse_header fails to peek enough, it will fall back to read() |
| 876 | // + rewind() and succeed. |
| 877 | DEF_TEST(Codec_webp_peek, r) { |
| 878 | const char* path = "baby_tux.webp"; |
| 879 | SkString fullPath(GetResourcePath(path)); |
| 880 | SkAutoTUnref<SkData> data(SkData::NewFromFileName(fullPath.c_str())); |
| 881 | if (!data) { |
| 882 | SkDebugf("Missing resource '%s'\n", path); |
| 883 | return; |
| 884 | } |
| 885 | |
| 886 | // The limit is less than webp needs to peek or read. |
| 887 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 25))); |
| 888 | REPORTER_ASSERT(r, codec); |
| 889 | |
| 890 | test_info(r, codec, codec->getInfo(), SkCodec::kSuccess, nullptr); |
| 891 | |
| 892 | // Similarly, a stream which does not peek should still succeed. |
| 893 | codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0))); |
| 894 | REPORTER_ASSERT(r, codec); |
| 895 | |
| 896 | test_info(r, codec, codec->getInfo(), SkCodec::kSuccess, nullptr); |
| 897 | } |
| 898 | |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 899 | // SkCodec's wbmp decoder was initially more restrictive than SkImageDecoder. |
| 900 | // It required the second byte to be zero. But SkImageDecoder allowed a couple |
| 901 | // of bits to be 1 (so long as they do not overlap with 0x9F). Test that |
| 902 | // SkCodec now supports an image with these bits set. |
| 903 | DEF_TEST(Codec_wbmp, r) { |
| 904 | const char* path = "mandrill.wbmp"; |
| 905 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 906 | if (!stream) { |
| 907 | SkDebugf("Missing resource '%s'\n", path); |
| 908 | return; |
| 909 | } |
| 910 | |
| 911 | // Modify the stream to contain a second byte with some bits set. |
| 912 | SkAutoTUnref<SkData> data(SkCopyStreamToData(stream)); |
| 913 | uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data()); |
| 914 | writeableData[1] = static_cast<uint8_t>(~0x9F); |
| 915 | |
| 916 | // SkImageDecoder supports this. |
| 917 | SkBitmap bitmap; |
| 918 | REPORTER_ASSERT(r, SkImageDecoder::DecodeMemory(data->data(), data->size(), &bitmap)); |
| 919 | |
| 920 | // So SkCodec should, too. |
| 921 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data)); |
| 922 | REPORTER_ASSERT(r, codec); |
| 923 | if (!codec) { |
| 924 | return; |
| 925 | } |
| 926 | test_info(r, codec, codec->getInfo(), SkCodec::kSuccess, nullptr); |
| 927 | } |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 928 | |
| 929 | // wbmp images have a header that can be arbitrarily large, depending on the |
| 930 | // size of the image. We cap the size at 65535, meaning we only need to look at |
| 931 | // 8 bytes to determine whether we can read the image. This is important |
| 932 | // because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the |
| 933 | // image is a wbmp. |
| 934 | DEF_TEST(Codec_wbmp_max_size, r) { |
| 935 | const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header |
| 936 | 0x83, 0xFF, 0x7F, // W: 65535 |
| 937 | 0x83, 0xFF, 0x7F }; // H: 65535 |
| 938 | SkAutoTDelete<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false)); |
| 939 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach())); |
| 940 | |
| 941 | REPORTER_ASSERT(r, codec); |
| 942 | if (!codec) return; |
| 943 | |
| 944 | REPORTER_ASSERT(r, codec->getInfo().width() == 65535); |
| 945 | REPORTER_ASSERT(r, codec->getInfo().height() == 65535); |
| 946 | |
| 947 | // Now test an image which is too big. Any image with a larger header (i.e. |
| 948 | // has bigger width/height) is also too big. |
| 949 | const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header |
| 950 | 0x84, 0x80, 0x00, // W: 65536 |
| 951 | 0x84, 0x80, 0x00 }; // H: 65536 |
| 952 | stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false)); |
| 953 | codec.reset(SkCodec::NewFromStream(stream.detach())); |
| 954 | |
| 955 | REPORTER_ASSERT(r, !codec); |
| 956 | } |