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