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