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