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" |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 13 | #include "SkMD5.h" |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 14 | #include "SkRandom.h" |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 15 | #include "Test.h" |
| 16 | |
| 17 | static SkStreamAsset* resource(const char path[]) { |
| 18 | SkString fullPath = GetResourcePath(path); |
| 19 | return SkStream::NewFromFile(fullPath.c_str()); |
| 20 | } |
| 21 | |
| 22 | static void md5(const SkBitmap& bm, SkMD5::Digest* digest) { |
| 23 | SkAutoLockPixels autoLockPixels(bm); |
| 24 | SkASSERT(bm.getPixels()); |
| 25 | SkMD5 md5; |
| 26 | size_t rowLen = bm.info().bytesPerPixel() * bm.width(); |
| 27 | for (int y = 0; y < bm.height(); ++y) { |
| 28 | md5.update(static_cast<uint8_t*>(bm.getAddr(0, y)), rowLen); |
| 29 | } |
| 30 | md5.finish(*digest); |
| 31 | } |
| 32 | |
scroggo | 9b2cdbf | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Compute the digest for bm and compare it to a known good digest. |
| 35 | * @param r Reporter to assert that bm's digest matches goodDigest. |
| 36 | * @param goodDigest The known good digest to compare to. |
| 37 | * @param bm The bitmap to test. |
| 38 | */ |
| 39 | static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest, |
| 40 | const SkBitmap& bm) { |
| 41 | SkMD5::Digest digest; |
| 42 | md5(bm, &digest); |
| 43 | REPORTER_ASSERT(r, digest == goodDigest); |
| 44 | } |
| 45 | |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Test decoding an SkCodec to a particular SkImageInfo. |
| 48 | * |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 49 | * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr, |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 50 | * the resulting decode should match. |
| 51 | */ |
| 52 | static void test_info(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info, |
| 53 | SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) { |
| 54 | SkBitmap bm; |
| 55 | bm.allocPixels(info); |
| 56 | SkAutoLockPixels autoLockPixels(bm); |
| 57 | |
| 58 | SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes()); |
| 59 | REPORTER_ASSERT(r, result == expectedResult); |
| 60 | |
| 61 | if (goodDigest) { |
| 62 | compare_to_good_digest(r, *goodDigest, bm); |
| 63 | } |
| 64 | } |
| 65 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame^] | 66 | static void test_android_info(skiatest::Reporter* r, SkAndroidCodec* codec, const SkImageInfo& info, |
| 67 | SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) { |
| 68 | SkBitmap bm; |
| 69 | bm.allocPixels(info); |
| 70 | SkAutoLockPixels autoLockPixels(bm); |
| 71 | |
| 72 | SkCodec::Result result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes()); |
| 73 | REPORTER_ASSERT(r, result == expectedResult); |
| 74 | |
| 75 | if (goodDigest) { |
| 76 | compare_to_good_digest(r, *goodDigest, bm); |
| 77 | } |
| 78 | } |
| 79 | |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 80 | SkIRect generate_random_subset(SkRandom* rand, int w, int h) { |
| 81 | SkIRect rect; |
| 82 | do { |
| 83 | rect.fLeft = rand->nextRangeU(0, w); |
| 84 | rect.fTop = rand->nextRangeU(0, h); |
| 85 | rect.fRight = rand->nextRangeU(0, w); |
| 86 | rect.fBottom = rand->nextRangeU(0, h); |
| 87 | rect.sort(); |
| 88 | } while (rect.isEmpty()); |
| 89 | return rect; |
| 90 | } |
| 91 | |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 92 | static void test_codec(skiatest::Reporter* r, SkCodec* codec, SkBitmap& bm, const SkImageInfo& info, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 93 | const SkISize& size, bool supports565, SkCodec::Result expectedResult, |
| 94 | SkMD5::Digest* digest, const SkMD5::Digest* goodDigest) { |
| 95 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 96 | REPORTER_ASSERT(r, info.dimensions() == size); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 97 | bm.allocPixels(info); |
| 98 | SkAutoLockPixels autoLockPixels(bm); |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 99 | |
| 100 | SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 101 | REPORTER_ASSERT(r, result == expectedResult); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 102 | |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 103 | md5(bm, digest); |
| 104 | if (goodDigest) { |
| 105 | REPORTER_ASSERT(r, *digest == *goodDigest); |
| 106 | } |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 107 | |
msarett | 8ff6ca6 | 2015-09-18 12:06:04 -0700 | [diff] [blame] | 108 | { |
| 109 | // Test decoding to 565 |
| 110 | SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 111 | SkCodec::Result expected565 = (supports565 && info.alphaType() == kOpaque_SkAlphaType) ? |
| 112 | expectedResult : SkCodec::kInvalidConversion; |
| 113 | test_info(r, codec, info565, expected565, nullptr); |
msarett | 8ff6ca6 | 2015-09-18 12:06:04 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // Verify that re-decoding gives the same result. It is interesting to check this after |
| 117 | // a decode to 565, since choosing to decode to 565 may result in some of the decode |
| 118 | // options being modified. These options should return to their defaults on another |
| 119 | // decode to kN32, so the new digest should match the old digest. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 120 | test_info(r, codec, info, expectedResult, digest); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 121 | |
| 122 | { |
| 123 | // Check alpha type conversions |
| 124 | if (info.alphaType() == kOpaque_SkAlphaType) { |
| 125 | test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 126 | SkCodec::kInvalidConversion, nullptr); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 127 | test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 128 | SkCodec::kInvalidConversion, nullptr); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 129 | } else { |
| 130 | // Decoding to opaque should fail |
| 131 | test_info(r, codec, info.makeAlphaType(kOpaque_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 | SkAlphaType otherAt = info.alphaType(); |
| 134 | if (kPremul_SkAlphaType == otherAt) { |
| 135 | otherAt = kUnpremul_SkAlphaType; |
| 136 | } else { |
| 137 | otherAt = kPremul_SkAlphaType; |
| 138 | } |
| 139 | // The other non-opaque alpha type should always succeed, but not match. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 140 | test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 143 | } |
| 144 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame^] | 145 | static void test_android_codec(skiatest::Reporter* r, SkAndroidCodec* codec, SkBitmap& bm, |
| 146 | const SkImageInfo& info, const SkISize& size, bool supports565, |
| 147 | SkCodec::Result expectedResult, SkMD5::Digest* digest, const SkMD5::Digest* goodDigest) { |
| 148 | |
| 149 | REPORTER_ASSERT(r, info.dimensions() == size); |
| 150 | bm.allocPixels(info); |
| 151 | SkAutoLockPixels autoLockPixels(bm); |
| 152 | |
| 153 | SkCodec::Result result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes()); |
| 154 | REPORTER_ASSERT(r, result == expectedResult); |
| 155 | |
| 156 | md5(bm, digest); |
| 157 | if (goodDigest) { |
| 158 | REPORTER_ASSERT(r, *digest == *goodDigest); |
| 159 | } |
| 160 | |
| 161 | { |
| 162 | // Test decoding to 565 |
| 163 | SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType); |
| 164 | SkCodec::Result expected565 = (supports565 && info.alphaType() == kOpaque_SkAlphaType) ? |
| 165 | expectedResult : SkCodec::kInvalidConversion; |
| 166 | test_android_info(r, codec, info565, expected565, nullptr); |
| 167 | } |
| 168 | |
| 169 | // Verify that re-decoding gives the same result. It is interesting to check this after |
| 170 | // a decode to 565, since choosing to decode to 565 may result in some of the decode |
| 171 | // options being modified. These options should return to their defaults on another |
| 172 | // decode to kN32, so the new digest should match the old digest. |
| 173 | test_android_info(r, codec, info, expectedResult, digest); |
| 174 | |
| 175 | { |
| 176 | // Check alpha type conversions |
| 177 | if (info.alphaType() == kOpaque_SkAlphaType) { |
| 178 | test_android_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType), |
| 179 | SkCodec::kInvalidConversion, nullptr); |
| 180 | test_android_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType), |
| 181 | SkCodec::kInvalidConversion, nullptr); |
| 182 | } else { |
| 183 | // Decoding to opaque should fail |
| 184 | test_android_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType), |
| 185 | SkCodec::kInvalidConversion, nullptr); |
| 186 | SkAlphaType otherAt = info.alphaType(); |
| 187 | if (kPremul_SkAlphaType == otherAt) { |
| 188 | otherAt = kUnpremul_SkAlphaType; |
| 189 | } else { |
| 190 | otherAt = kPremul_SkAlphaType; |
| 191 | } |
| 192 | // The other non-opaque alpha type should always succeed, but not match. |
| 193 | test_android_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 198 | // FIXME: SkScaledCodec is currently only supported for types used by BRD |
| 199 | // skbug.com/4428 |
| 200 | static bool supports_scaled_codec(const char path[]) { |
| 201 | static const char* const exts[] = { |
| 202 | "jpg", "jpeg", "png", "webp" |
| 203 | "JPG", "JPEG", "PNG", "WEBP" |
| 204 | }; |
| 205 | |
| 206 | for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) { |
| 207 | if (SkStrEndsWith(path, exts[i])) { |
| 208 | return true; |
| 209 | } |
| 210 | } |
| 211 | return false; |
| 212 | } |
| 213 | |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 214 | static void check(skiatest::Reporter* r, |
| 215 | const char path[], |
| 216 | SkISize size, |
| 217 | bool supportsScanlineDecoding, |
| 218 | bool supportsSubsetDecoding, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 219 | bool supports565 = true, |
| 220 | bool supportsIncomplete = true) { |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 221 | |
| 222 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 223 | if (!stream) { |
| 224 | SkDebugf("Missing resource '%s'\n", path); |
| 225 | return; |
| 226 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 227 | |
| 228 | SkAutoTDelete<SkCodec> codec(nullptr); |
| 229 | bool isIncomplete = supportsIncomplete; |
| 230 | if (isIncomplete) { |
| 231 | size_t size = stream->getLength(); |
| 232 | SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3))); |
| 233 | codec.reset(SkCodec::NewFromData(data)); |
| 234 | } else { |
| 235 | codec.reset(SkCodec::NewFromStream(stream.detach())); |
| 236 | } |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 237 | if (!codec) { |
| 238 | ERRORF(r, "Unable to decode '%s'", path); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | // Test full image decodes with SkCodec |
| 243 | SkMD5::Digest codecDigest; |
| 244 | SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType); |
| 245 | SkBitmap bm; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 246 | SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess; |
| 247 | test_codec(r, codec, bm, info, size, supports565, expectedResult, &codecDigest, nullptr); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 248 | |
| 249 | // Scanline decoding follows. |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 250 | // Need to call startScanlineDecode() first. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 251 | REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 252 | == 0); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 253 | REPORTER_ASSERT(r, codec->skipScanlines(1) |
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 | const SkCodec::Result startResult = codec->startScanlineDecode(info); |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 257 | if (supportsScanlineDecoding) { |
| 258 | bm.eraseColor(SK_ColorYELLOW); |
msarett | c0e80c1 | 2015-07-01 06:50:35 -0700 | [diff] [blame] | 259 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 260 | REPORTER_ASSERT(r, startResult == SkCodec::kSuccess); |
scroggo | 9b2cdbf | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 261 | |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 262 | for (int y = 0; y < info.height(); y++) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 263 | const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0); |
| 264 | if (!isIncomplete) { |
| 265 | REPORTER_ASSERT(r, 1 == lines); |
| 266 | } |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 267 | } |
| 268 | // verify that scanline decoding gives the same result. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 269 | if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) { |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 270 | compare_to_good_digest(r, codecDigest, bm); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 271 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 272 | |
| 273 | // Cannot continue to decode scanlines beyond the end |
| 274 | REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 275 | == 0); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 276 | |
| 277 | // Interrupting a scanline decode with a full decode starts from |
| 278 | // scratch |
| 279 | REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 280 | const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0); |
| 281 | if (!isIncomplete) { |
| 282 | REPORTER_ASSERT(r, lines == 1); |
| 283 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 284 | REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes()) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 285 | == expectedResult); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 286 | REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 287 | == 0); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 288 | REPORTER_ASSERT(r, codec->skipScanlines(1) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 289 | == 0); |
msarett | 80803ff | 2015-10-16 10:54:12 -0700 | [diff] [blame] | 290 | |
| 291 | // Test partial scanline decodes |
| 292 | if (supports_scaled_codec(path) && info.width() >= 3) { |
| 293 | SkCodec::Options options; |
| 294 | int width = info.width(); |
| 295 | int height = info.height(); |
| 296 | SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height); |
| 297 | options.fSubset = ⊂ |
| 298 | |
| 299 | const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options, |
| 300 | nullptr, nullptr); |
| 301 | REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess); |
| 302 | |
| 303 | for (int y = 0; y < height; y++) { |
| 304 | const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0); |
| 305 | if (!isIncomplete) { |
| 306 | REPORTER_ASSERT(r, 1 == lines); |
| 307 | } |
| 308 | } |
| 309 | } |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 310 | } else { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 311 | REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 312 | } |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 313 | |
| 314 | // The rest of this function tests decoding subsets, and will decode an arbitrary number of |
| 315 | // random subsets. |
| 316 | // Do not attempt to decode subsets of an image of only once pixel, since there is no |
| 317 | // meaningful subset. |
| 318 | if (size.width() * size.height() == 1) { |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | SkRandom rand; |
| 323 | SkIRect subset; |
| 324 | SkCodec::Options opts; |
| 325 | opts.fSubset = ⊂ |
| 326 | for (int i = 0; i < 5; i++) { |
| 327 | subset = generate_random_subset(&rand, size.width(), size.height()); |
| 328 | SkASSERT(!subset.isEmpty()); |
| 329 | const bool supported = codec->getValidSubset(&subset); |
| 330 | REPORTER_ASSERT(r, supported == supportsSubsetDecoding); |
| 331 | |
| 332 | SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height()); |
| 333 | SkBitmap bm; |
| 334 | bm.allocPixels(subsetInfo); |
| 335 | const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 336 | &opts, nullptr, nullptr); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 337 | |
| 338 | if (supportsSubsetDecoding) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 339 | REPORTER_ASSERT(r, result == expectedResult); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 340 | // Webp is the only codec that supports subsets, and it will have modified the subset |
| 341 | // to have even left/top. |
| 342 | REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop)); |
| 343 | } else { |
| 344 | // No subsets will work. |
| 345 | REPORTER_ASSERT(r, result == SkCodec::kUnimplemented); |
| 346 | } |
| 347 | } |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 348 | |
| 349 | // SkScaledCodec tests |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 350 | if ((supportsScanlineDecoding || supportsSubsetDecoding) && supports_scaled_codec(path)) { |
| 351 | |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 352 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 353 | if (!stream) { |
| 354 | SkDebugf("Missing resource '%s'\n", path); |
| 355 | return; |
| 356 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 357 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame^] | 358 | SkAutoTDelete<SkAndroidCodec> codec(nullptr); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 359 | if (isIncomplete) { |
| 360 | size_t size = stream->getLength(); |
| 361 | SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3))); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame^] | 362 | codec.reset(SkAndroidCodec::NewFromData(data)); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 363 | } else { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame^] | 364 | codec.reset(SkAndroidCodec::NewFromStream(stream.detach())); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 365 | } |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 366 | if (!codec) { |
| 367 | ERRORF(r, "Unable to decode '%s'", path); |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | SkBitmap bm; |
| 372 | SkMD5::Digest scaledCodecDigest; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame^] | 373 | test_android_codec(r, codec, bm, info, size, supports565, expectedResult, |
| 374 | &scaledCodecDigest, &codecDigest); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // If we've just tested incomplete decodes, let's run the same test again on full decodes. |
| 378 | if (isIncomplete) { |
| 379 | check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, supports565, false); |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 380 | } |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | DEF_TEST(Codec, r) { |
| 384 | // WBMP |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 385 | check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 386 | |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 387 | // WEBP |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 388 | check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true); |
| 389 | check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true); |
| 390 | check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 391 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 392 | // BMP |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 393 | check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 394 | |
| 395 | // ICO |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 396 | // FIXME: We are not ready to test incomplete ICOs |
msarett | 68b204e | 2015-04-01 12:09:21 -0700 | [diff] [blame] | 397 | // These two tests examine interestingly different behavior: |
| 398 | // Decodes an embedded BMP image |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 399 | check(r, "color_wheel.ico", SkISize::Make(128, 128), false, false, true, false); |
msarett | 68b204e | 2015-04-01 12:09:21 -0700 | [diff] [blame] | 400 | // Decodes an embedded PNG image |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 401 | check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false, true, false); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 402 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 403 | // GIF |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 404 | // FIXME: We are not ready to test incomplete GIFs |
| 405 | check(r, "box.gif", SkISize::Make(200, 55), true, false, true, false); |
| 406 | check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, true, false); |
| 407 | // randPixels.gif is too small to test incomplete |
| 408 | check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, true, false); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 409 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 410 | // JPG |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 411 | check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false, false); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 412 | check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 413 | // grayscale.jpg is too small to test incomplete |
| 414 | check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, true, false); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 415 | check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 416 | // randPixels.jpg is too small to test incomplete |
| 417 | check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, true, false); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 418 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 419 | // PNG |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 420 | check(r, "arrow.png", SkISize::Make(187, 312), true, false, true, false); |
| 421 | check(r, "baby_tux.png", SkISize::Make(240, 246), true, false, true, false); |
| 422 | check(r, "color_wheel.png", SkISize::Make(128, 128), true, false, true, false); |
| 423 | check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false, true, false); |
| 424 | check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false, true, false); |
| 425 | check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false, true, false); |
| 426 | check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false, true, false); |
| 427 | check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false, true, false); |
| 428 | check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false, true, false); |
| 429 | check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false, true, false); |
| 430 | check(r, "plane.png", SkISize::Make(250, 126), true, false, true, false); |
| 431 | // FIXME: We are not ready to test incomplete interlaced pngs |
| 432 | check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false, true, false); |
| 433 | check(r, "randPixels.png", SkISize::Make(8, 8), true, false, true, false); |
| 434 | check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false, true, false); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 435 | } |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 436 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 437 | // Test interlaced PNG in stripes, similar to DM's kStripe_Mode |
| 438 | DEF_TEST(Codec_stripes, r) { |
| 439 | const char * path = "plane_interlaced.png"; |
| 440 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 441 | if (!stream) { |
| 442 | SkDebugf("Missing resource '%s'\n", path); |
| 443 | } |
| 444 | |
| 445 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach())); |
| 446 | REPORTER_ASSERT(r, codec); |
| 447 | |
| 448 | if (!codec) { |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | switch (codec->getScanlineOrder()) { |
| 453 | case SkCodec::kBottomUp_SkScanlineOrder: |
| 454 | case SkCodec::kOutOfOrder_SkScanlineOrder: |
| 455 | ERRORF(r, "This scanline order will not match the original."); |
| 456 | return; |
| 457 | default: |
| 458 | break; |
| 459 | } |
| 460 | |
| 461 | // Baseline for what the image should look like, using N32. |
| 462 | const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType); |
| 463 | |
| 464 | SkBitmap bm; |
| 465 | bm.allocPixels(info); |
| 466 | SkAutoLockPixels autoLockPixels(bm); |
| 467 | SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes()); |
| 468 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 469 | |
| 470 | SkMD5::Digest digest; |
| 471 | md5(bm, &digest); |
| 472 | |
| 473 | // Now decode in stripes |
| 474 | const int height = info.height(); |
| 475 | const int numStripes = 4; |
| 476 | int stripeHeight; |
| 477 | int remainingLines; |
| 478 | SkTDivMod(height, numStripes, &stripeHeight, &remainingLines); |
| 479 | |
| 480 | bm.eraseColor(SK_ColorYELLOW); |
| 481 | |
| 482 | result = codec->startScanlineDecode(info); |
| 483 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 484 | |
| 485 | // Odd stripes |
| 486 | for (int i = 1; i < numStripes; i += 2) { |
| 487 | // Skip the even stripes |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 488 | bool skipResult = codec->skipScanlines(stripeHeight); |
| 489 | REPORTER_ASSERT(r, skipResult); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 490 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 491 | int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 492 | bm.rowBytes()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 493 | REPORTER_ASSERT(r, linesDecoded == stripeHeight); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // Even stripes |
| 497 | result = codec->startScanlineDecode(info); |
| 498 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 499 | |
| 500 | for (int i = 0; i < numStripes; i += 2) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 501 | int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 502 | bm.rowBytes()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 503 | REPORTER_ASSERT(r, linesDecoded == stripeHeight); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 504 | |
| 505 | // Skip the odd stripes |
| 506 | if (i + 1 < numStripes) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 507 | bool skipResult = codec->skipScanlines(stripeHeight); |
| 508 | REPORTER_ASSERT(r, skipResult); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
| 512 | // Remainder at the end |
| 513 | if (remainingLines > 0) { |
| 514 | result = codec->startScanlineDecode(info); |
| 515 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 516 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 517 | bool skipResult = codec->skipScanlines(height - remainingLines); |
| 518 | REPORTER_ASSERT(r, skipResult); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 519 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 520 | int linesDecoded = codec->getScanlines(bm.getAddr(0, height - remainingLines), |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 521 | remainingLines, bm.rowBytes()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 522 | REPORTER_ASSERT(r, linesDecoded == remainingLines); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | compare_to_good_digest(r, digest, bm); |
| 526 | } |
| 527 | |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 528 | 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] | 529 | // 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] | 530 | SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false)); |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 531 | REPORTER_ASSERT(r, !codec); |
| 532 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame^] | 533 | SkAndroidCodec* androidCodec = |
| 534 | SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false)); |
| 535 | REPORTER_ASSERT(r, !androidCodec); |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | // Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream, |
| 539 | // even on failure. Test some bad streams. |
| 540 | DEF_TEST(Codec_leaks, r) { |
| 541 | // No codec should claim this as their format, so this tests SkCodec::NewFromStream. |
| 542 | const char nonSupportedStream[] = "hello world"; |
| 543 | // The other strings should look like the beginning of a file type, so we'll call some |
| 544 | // internal version of NewFromStream, which must also delete the stream on failure. |
| 545 | const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }; |
| 546 | const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF }; |
| 547 | const char emptyWebp[] = "RIFF1234WEBPVP"; |
| 548 | const char emptyBmp[] = { 'B', 'M' }; |
| 549 | const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' }; |
| 550 | const char emptyGif[] = "GIFVER"; |
| 551 | |
| 552 | test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream)); |
| 553 | test_invalid_stream(r, emptyPng, sizeof(emptyPng)); |
| 554 | test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg)); |
| 555 | test_invalid_stream(r, emptyWebp, sizeof(emptyWebp)); |
| 556 | test_invalid_stream(r, emptyBmp, sizeof(emptyBmp)); |
| 557 | test_invalid_stream(r, emptyIco, sizeof(emptyIco)); |
| 558 | test_invalid_stream(r, emptyGif, sizeof(emptyGif)); |
| 559 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 560 | |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 561 | DEF_TEST(Codec_null, r) { |
| 562 | // Attempting to create an SkCodec or an SkScaledCodec with null should not |
| 563 | // crash. |
| 564 | SkCodec* codec = SkCodec::NewFromStream(nullptr); |
| 565 | REPORTER_ASSERT(r, !codec); |
| 566 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame^] | 567 | SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr); |
| 568 | REPORTER_ASSERT(r, !androidCodec); |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 569 | } |
| 570 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 571 | static void test_dimensions(skiatest::Reporter* r, const char path[]) { |
| 572 | // Create the codec from the resource file |
| 573 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 574 | if (!stream) { |
| 575 | SkDebugf("Missing resource '%s'\n", path); |
| 576 | return; |
| 577 | } |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame^] | 578 | SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.detach())); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 579 | if (!codec) { |
| 580 | ERRORF(r, "Unable to create codec '%s'", path); |
| 581 | return; |
| 582 | } |
| 583 | |
| 584 | // Check that the decode is successful for a variety of scales |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame^] | 585 | for (int sampleSize = 1; sampleSize < 10; sampleSize++) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 586 | // Scale the output dimensions |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame^] | 587 | SkISize scaledDims = codec->getSampledDimensions(sampleSize); |
msarett | b32758a | 2015-08-18 13:22:46 -0700 | [diff] [blame] | 588 | SkImageInfo scaledInfo = codec->getInfo() |
| 589 | .makeWH(scaledDims.width(), scaledDims.height()) |
| 590 | .makeColorType(kN32_SkColorType); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 591 | |
| 592 | // Set up for the decode |
| 593 | size_t rowBytes = scaledDims.width() * sizeof(SkPMColor); |
| 594 | size_t totalBytes = scaledInfo.getSafeSize(rowBytes); |
| 595 | SkAutoTMalloc<SkPMColor> pixels(totalBytes); |
| 596 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame^] | 597 | SkAndroidCodec::AndroidOptions options; |
| 598 | options.fSampleSize = sampleSize; |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 599 | SkCodec::Result result = |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame^] | 600 | codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 601 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
| 605 | // Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes |
| 606 | DEF_TEST(Codec_Dimensions, r) { |
| 607 | // JPG |
| 608 | test_dimensions(r, "CMYK.jpg"); |
| 609 | test_dimensions(r, "color_wheel.jpg"); |
| 610 | test_dimensions(r, "grayscale.jpg"); |
| 611 | test_dimensions(r, "mandrill_512_q075.jpg"); |
| 612 | test_dimensions(r, "randPixels.jpg"); |
msarett | b32758a | 2015-08-18 13:22:46 -0700 | [diff] [blame] | 613 | |
| 614 | // Decoding small images with very large scaling factors is a potential |
| 615 | // source of bugs and crashes. We disable these tests in Gold because |
| 616 | // tiny images are not very useful to look at. |
| 617 | // Here we make sure that we do not crash or access illegal memory when |
| 618 | // performing scaled decodes on small images. |
| 619 | test_dimensions(r, "1x1.png"); |
| 620 | test_dimensions(r, "2x2.png"); |
| 621 | test_dimensions(r, "3x3.png"); |
| 622 | test_dimensions(r, "3x1.png"); |
| 623 | test_dimensions(r, "1x1.png"); |
| 624 | test_dimensions(r, "16x1.png"); |
| 625 | test_dimensions(r, "1x16.png"); |
| 626 | test_dimensions(r, "mandrill_16.png"); |
| 627 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 628 | } |
| 629 | |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 630 | static void test_invalid(skiatest::Reporter* r, const char path[]) { |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 631 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 632 | if (!stream) { |
| 633 | SkDebugf("Missing resource '%s'\n", path); |
| 634 | return; |
| 635 | } |
| 636 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 637 | REPORTER_ASSERT(r, nullptr == codec); |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 638 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 639 | |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 640 | DEF_TEST(Codec_Empty, r) { |
| 641 | // Test images that should not be able to create a codec |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 642 | test_invalid(r, "empty_images/zero-dims.gif"); |
| 643 | test_invalid(r, "empty_images/zero-embedded.ico"); |
| 644 | test_invalid(r, "empty_images/zero-width.bmp"); |
| 645 | test_invalid(r, "empty_images/zero-height.bmp"); |
| 646 | test_invalid(r, "empty_images/zero-width.jpg"); |
| 647 | test_invalid(r, "empty_images/zero-height.jpg"); |
| 648 | test_invalid(r, "empty_images/zero-width.png"); |
| 649 | test_invalid(r, "empty_images/zero-height.png"); |
| 650 | test_invalid(r, "empty_images/zero-width.wbmp"); |
| 651 | test_invalid(r, "empty_images/zero-height.wbmp"); |
| 652 | // This image is an ico with an embedded mask-bmp. This is illegal. |
| 653 | test_invalid(r, "invalid_images/mask-bmp-ico.ico"); |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 654 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 655 | |
| 656 | static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) { |
| 657 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 658 | if (!stream) { |
| 659 | SkDebugf("Missing resource '%s'\n", path); |
| 660 | return; |
| 661 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 662 | SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.detach())); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 663 | |
| 664 | // This should return kSuccess because kIndex8 is supported. |
| 665 | SkPMColor colorStorage[256]; |
| 666 | int colorCount; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 667 | SkCodec::Result result = decoder->startScanlineDecode( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 668 | decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 669 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 670 | // The rest of the test is uninteresting if kIndex8 is not supported |
| 671 | if (SkCodec::kSuccess != result) { |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid |
| 676 | // colorPtr and a valid colorCountPtr. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 677 | result = decoder->startScanlineDecode( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 678 | decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 679 | REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 680 | result = decoder->startScanlineDecode( |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 681 | decoder->getInfo().makeColorType(kIndex_8_SkColorType)); |
| 682 | REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); |
| 683 | } |
| 684 | |
| 685 | DEF_TEST(Codec_Params, r) { |
| 686 | test_invalid_parameters(r, "index8.png"); |
| 687 | test_invalid_parameters(r, "mandrill.wbmp"); |
| 688 | } |