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