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