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