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" |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 9 | #include "SkAndroidCodec.h" |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 10 | #include "SkBitmap.h" |
| 11 | #include "SkCodec.h" |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 12 | #include "SkCodecImageGenerator.h" |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 13 | #include "SkData.h" |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 14 | #include "SkImageDecoder.h" |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 15 | #include "SkMD5.h" |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 16 | #include "SkRandom.h" |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 17 | #include "SkStream.h" |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 18 | #include "SkStreamPriv.h" |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 19 | #include "SkPngChunkReader.h" |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 20 | #include "Test.h" |
| 21 | |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 22 | #include "png.h" |
| 23 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 24 | static SkStreamAsset* resource(const char path[]) { |
| 25 | SkString fullPath = GetResourcePath(path); |
| 26 | return SkStream::NewFromFile(fullPath.c_str()); |
| 27 | } |
| 28 | |
| 29 | static void md5(const SkBitmap& bm, SkMD5::Digest* digest) { |
| 30 | SkAutoLockPixels autoLockPixels(bm); |
| 31 | SkASSERT(bm.getPixels()); |
| 32 | SkMD5 md5; |
| 33 | size_t rowLen = bm.info().bytesPerPixel() * bm.width(); |
| 34 | for (int y = 0; y < bm.height(); ++y) { |
| 35 | md5.update(static_cast<uint8_t*>(bm.getAddr(0, y)), rowLen); |
| 36 | } |
| 37 | md5.finish(*digest); |
| 38 | } |
| 39 | |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Compute the digest for bm and compare it to a known good digest. |
| 42 | * @param r Reporter to assert that bm's digest matches goodDigest. |
| 43 | * @param goodDigest The known good digest to compare to. |
| 44 | * @param bm The bitmap to test. |
| 45 | */ |
| 46 | static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest, |
| 47 | const SkBitmap& bm) { |
| 48 | SkMD5::Digest digest; |
| 49 | md5(bm, &digest); |
| 50 | REPORTER_ASSERT(r, digest == goodDigest); |
| 51 | } |
| 52 | |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Test decoding an SkCodec to a particular SkImageInfo. |
| 55 | * |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 56 | * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr, |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 57 | * the resulting decode should match. |
| 58 | */ |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 59 | template<typename Codec> |
| 60 | static void test_info(skiatest::Reporter* r, Codec* codec, const SkImageInfo& info, |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 61 | SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) { |
| 62 | SkBitmap bm; |
| 63 | bm.allocPixels(info); |
| 64 | SkAutoLockPixels autoLockPixels(bm); |
| 65 | |
| 66 | SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes()); |
| 67 | REPORTER_ASSERT(r, result == expectedResult); |
| 68 | |
| 69 | if (goodDigest) { |
| 70 | compare_to_good_digest(r, *goodDigest, bm); |
| 71 | } |
| 72 | } |
| 73 | |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 74 | SkIRect generate_random_subset(SkRandom* rand, int w, int h) { |
| 75 | SkIRect rect; |
| 76 | do { |
| 77 | rect.fLeft = rand->nextRangeU(0, w); |
| 78 | rect.fTop = rand->nextRangeU(0, h); |
| 79 | rect.fRight = rand->nextRangeU(0, w); |
| 80 | rect.fBottom = rand->nextRangeU(0, h); |
| 81 | rect.sort(); |
| 82 | } while (rect.isEmpty()); |
| 83 | return rect; |
| 84 | } |
| 85 | |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 86 | template<typename Codec> |
| 87 | static void test_codec(skiatest::Reporter* r, Codec* codec, SkBitmap& bm, const SkImageInfo& info, |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 88 | const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest, |
| 89 | const SkMD5::Digest* goodDigest) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 90 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 91 | REPORTER_ASSERT(r, info.dimensions() == size); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 92 | bm.allocPixels(info); |
| 93 | SkAutoLockPixels autoLockPixels(bm); |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 94 | |
| 95 | SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 96 | REPORTER_ASSERT(r, result == expectedResult); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 97 | |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 98 | md5(bm, digest); |
| 99 | if (goodDigest) { |
| 100 | REPORTER_ASSERT(r, *digest == *goodDigest); |
| 101 | } |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 102 | |
msarett | 8ff6ca6 | 2015-09-18 12:06:04 -0700 | [diff] [blame] | 103 | { |
| 104 | // Test decoding to 565 |
| 105 | SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType); |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 106 | SkCodec::Result expected565 = info.alphaType() == kOpaque_SkAlphaType ? |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 107 | expectedResult : SkCodec::kInvalidConversion; |
| 108 | test_info(r, codec, info565, expected565, nullptr); |
msarett | 8ff6ca6 | 2015-09-18 12:06:04 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Verify that re-decoding gives the same result. It is interesting to check this after |
| 112 | // a decode to 565, since choosing to decode to 565 may result in some of the decode |
| 113 | // options being modified. These options should return to their defaults on another |
| 114 | // decode to kN32, so the new digest should match the old digest. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 115 | test_info(r, codec, info, expectedResult, digest); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 116 | |
| 117 | { |
| 118 | // Check alpha type conversions |
| 119 | if (info.alphaType() == kOpaque_SkAlphaType) { |
| 120 | test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType), |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 121 | expectedResult, digest); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 122 | test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType), |
scroggo | c5560be | 2016-02-03 09:42:42 -0800 | [diff] [blame] | 123 | expectedResult, digest); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 124 | } else { |
| 125 | // Decoding to opaque should fail |
| 126 | test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 127 | SkCodec::kInvalidConversion, nullptr); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 128 | SkAlphaType otherAt = info.alphaType(); |
| 129 | if (kPremul_SkAlphaType == otherAt) { |
| 130 | otherAt = kUnpremul_SkAlphaType; |
| 131 | } else { |
| 132 | otherAt = kPremul_SkAlphaType; |
| 133 | } |
| 134 | // The other non-opaque alpha type should always succeed, but not match. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 135 | test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 138 | } |
| 139 | |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 140 | // FIXME: SkScaledCodec is currently only supported for types used by BRD |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 141 | // https://bug.skia.org/4428 |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 142 | static bool supports_scaled_codec(const char path[]) { |
| 143 | static const char* const exts[] = { |
| 144 | "jpg", "jpeg", "png", "webp" |
| 145 | "JPG", "JPEG", "PNG", "WEBP" |
| 146 | }; |
| 147 | |
| 148 | for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) { |
| 149 | if (SkStrEndsWith(path, exts[i])) { |
| 150 | return true; |
| 151 | } |
| 152 | } |
| 153 | return false; |
| 154 | } |
| 155 | |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 156 | static void check(skiatest::Reporter* r, |
| 157 | const char path[], |
| 158 | SkISize size, |
| 159 | bool supportsScanlineDecoding, |
| 160 | bool supportsSubsetDecoding, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 161 | bool supportsIncomplete = true) { |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 162 | |
| 163 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 164 | if (!stream) { |
| 165 | SkDebugf("Missing resource '%s'\n", path); |
| 166 | return; |
| 167 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 168 | |
| 169 | SkAutoTDelete<SkCodec> codec(nullptr); |
| 170 | bool isIncomplete = supportsIncomplete; |
| 171 | if (isIncomplete) { |
| 172 | size_t size = stream->getLength(); |
| 173 | SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3))); |
| 174 | codec.reset(SkCodec::NewFromData(data)); |
| 175 | } else { |
| 176 | codec.reset(SkCodec::NewFromStream(stream.detach())); |
| 177 | } |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 178 | if (!codec) { |
| 179 | ERRORF(r, "Unable to decode '%s'", path); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | // Test full image decodes with SkCodec |
| 184 | SkMD5::Digest codecDigest; |
| 185 | SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType); |
| 186 | SkBitmap bm; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 187 | SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess; |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 188 | test_codec(r, codec.get(), bm, info, size, expectedResult, &codecDigest, nullptr); |
scroggo | d1bc574 | 2015-08-12 08:31:44 -0700 | [diff] [blame] | 189 | |
| 190 | // Scanline decoding follows. |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 191 | // Need to call startScanlineDecode() first. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 192 | REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 193 | == 0); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 194 | REPORTER_ASSERT(r, codec->skipScanlines(1) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 195 | == 0); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 196 | |
| 197 | const SkCodec::Result startResult = codec->startScanlineDecode(info); |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 198 | if (supportsScanlineDecoding) { |
| 199 | bm.eraseColor(SK_ColorYELLOW); |
msarett | c0e80c1 | 2015-07-01 06:50:35 -0700 | [diff] [blame] | 200 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 201 | REPORTER_ASSERT(r, startResult == SkCodec::kSuccess); |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 202 | |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 203 | for (int y = 0; y < info.height(); y++) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 204 | const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0); |
| 205 | if (!isIncomplete) { |
| 206 | REPORTER_ASSERT(r, 1 == lines); |
| 207 | } |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 208 | } |
| 209 | // verify that scanline decoding gives the same result. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 210 | if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) { |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 211 | compare_to_good_digest(r, codecDigest, bm); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 212 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 213 | |
| 214 | // Cannot continue to decode scanlines beyond the end |
| 215 | REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 216 | == 0); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 217 | |
| 218 | // Interrupting a scanline decode with a full decode starts from |
| 219 | // scratch |
| 220 | REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 221 | const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0); |
| 222 | if (!isIncomplete) { |
| 223 | REPORTER_ASSERT(r, lines == 1); |
| 224 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 225 | REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes()) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 226 | == expectedResult); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 227 | REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 228 | == 0); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 229 | REPORTER_ASSERT(r, codec->skipScanlines(1) |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 230 | == 0); |
msarett | 80803ff | 2015-10-16 10:54:12 -0700 | [diff] [blame] | 231 | |
| 232 | // Test partial scanline decodes |
| 233 | if (supports_scaled_codec(path) && info.width() >= 3) { |
| 234 | SkCodec::Options options; |
| 235 | int width = info.width(); |
| 236 | int height = info.height(); |
| 237 | SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height); |
| 238 | options.fSubset = ⊂ |
| 239 | |
| 240 | const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options, |
| 241 | nullptr, nullptr); |
| 242 | REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess); |
| 243 | |
| 244 | for (int y = 0; y < height; y++) { |
| 245 | const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0); |
| 246 | if (!isIncomplete) { |
| 247 | REPORTER_ASSERT(r, 1 == lines); |
| 248 | } |
| 249 | } |
| 250 | } |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 251 | } else { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 252 | REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 253 | } |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 254 | |
| 255 | // The rest of this function tests decoding subsets, and will decode an arbitrary number of |
| 256 | // random subsets. |
| 257 | // Do not attempt to decode subsets of an image of only once pixel, since there is no |
| 258 | // meaningful subset. |
| 259 | if (size.width() * size.height() == 1) { |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | SkRandom rand; |
| 264 | SkIRect subset; |
| 265 | SkCodec::Options opts; |
| 266 | opts.fSubset = ⊂ |
| 267 | for (int i = 0; i < 5; i++) { |
| 268 | subset = generate_random_subset(&rand, size.width(), size.height()); |
| 269 | SkASSERT(!subset.isEmpty()); |
| 270 | const bool supported = codec->getValidSubset(&subset); |
| 271 | REPORTER_ASSERT(r, supported == supportsSubsetDecoding); |
| 272 | |
| 273 | SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height()); |
| 274 | SkBitmap bm; |
| 275 | bm.allocPixels(subsetInfo); |
| 276 | const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 277 | &opts, nullptr, nullptr); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 278 | |
| 279 | if (supportsSubsetDecoding) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 280 | REPORTER_ASSERT(r, result == expectedResult); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 281 | // Webp is the only codec that supports subsets, and it will have modified the subset |
| 282 | // to have even left/top. |
| 283 | REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop)); |
| 284 | } else { |
| 285 | // No subsets will work. |
| 286 | REPORTER_ASSERT(r, result == SkCodec::kUnimplemented); |
| 287 | } |
| 288 | } |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 289 | |
| 290 | // SkScaledCodec tests |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 291 | if ((supportsScanlineDecoding || supportsSubsetDecoding) && supports_scaled_codec(path)) { |
| 292 | |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 293 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 294 | if (!stream) { |
| 295 | SkDebugf("Missing resource '%s'\n", path); |
| 296 | return; |
| 297 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 298 | |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 299 | SkAutoTDelete<SkAndroidCodec> androidCodec(nullptr); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 300 | if (isIncomplete) { |
| 301 | size_t size = stream->getLength(); |
| 302 | SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3))); |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 303 | androidCodec.reset(SkAndroidCodec::NewFromData(data)); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 304 | } else { |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 305 | androidCodec.reset(SkAndroidCodec::NewFromStream(stream.detach())); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 306 | } |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 307 | if (!androidCodec) { |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 308 | ERRORF(r, "Unable to decode '%s'", path); |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | SkBitmap bm; |
| 313 | SkMD5::Digest scaledCodecDigest; |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 314 | test_codec(r, androidCodec.get(), bm, info, size, expectedResult, &scaledCodecDigest, |
| 315 | &codecDigest); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 316 | } |
| 317 | |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 318 | // Test SkCodecImageGenerator |
| 319 | if (!isIncomplete) { |
| 320 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 321 | SkAutoTUnref<SkData> fullData(SkData::NewFromStream(stream, stream->getLength())); |
| 322 | SkAutoTDelete<SkImageGenerator> gen(SkCodecImageGenerator::NewFromEncodedCodec(fullData)); |
| 323 | SkBitmap bm; |
| 324 | bm.allocPixels(info); |
| 325 | SkAutoLockPixels autoLockPixels(bm); |
| 326 | REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes())); |
| 327 | compare_to_good_digest(r, codecDigest, bm); |
| 328 | } |
| 329 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 330 | // If we've just tested incomplete decodes, let's run the same test again on full decodes. |
| 331 | if (isIncomplete) { |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 332 | check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false); |
msarett | cc7f305 | 2015-10-05 14:20:27 -0700 | [diff] [blame] | 333 | } |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | DEF_TEST(Codec, r) { |
| 337 | // WBMP |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 338 | check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 339 | |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 340 | // WEBP |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 341 | check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true); |
| 342 | check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true); |
| 343 | check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 344 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 345 | // BMP |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 346 | check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false); |
msarett | 4946b94 | 2016-02-11 08:41:01 -0800 | [diff] [blame] | 347 | check(r, "rle.bmp", SkISize::Make(320, 240), true, false); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 348 | |
| 349 | // ICO |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 350 | // FIXME: We are not ready to test incomplete ICOs |
msarett | 68b204e | 2015-04-01 12:09:21 -0700 | [diff] [blame] | 351 | // These two tests examine interestingly different behavior: |
| 352 | // Decodes an embedded BMP image |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 353 | check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false); |
msarett | 68b204e | 2015-04-01 12:09:21 -0700 | [diff] [blame] | 354 | // Decodes an embedded PNG image |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 355 | check(r, "google_chrome.ico", SkISize::Make(256, 256), true, false, false); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 356 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 357 | // GIF |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 358 | // FIXME: We are not ready to test incomplete GIFs |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 359 | check(r, "box.gif", SkISize::Make(200, 55), true, false, false); |
| 360 | check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, false); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 361 | // randPixels.gif is too small to test incomplete |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 362 | check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, false); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 363 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 364 | // JPG |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 365 | check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 366 | check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 367 | // grayscale.jpg is too small to test incomplete |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 368 | check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false); |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 369 | check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 370 | // randPixels.jpg is too small to test incomplete |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 371 | check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 372 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 373 | // PNG |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 374 | check(r, "arrow.png", SkISize::Make(187, 312), true, false, false); |
| 375 | check(r, "baby_tux.png", SkISize::Make(240, 246), true, false, false); |
| 376 | check(r, "color_wheel.png", SkISize::Make(128, 128), true, false, false); |
| 377 | check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false, false); |
| 378 | check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false, false); |
| 379 | check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false, false); |
| 380 | check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false, false); |
| 381 | check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false, false); |
| 382 | check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false, false); |
| 383 | check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false, false); |
| 384 | check(r, "plane.png", SkISize::Make(250, 126), true, false, false); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 385 | // FIXME: We are not ready to test incomplete interlaced pngs |
scroggo | 27c1728 | 2015-10-27 08:14:46 -0700 | [diff] [blame] | 386 | check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false, false); |
| 387 | check(r, "randPixels.png", SkISize::Make(8, 8), true, false, false); |
| 388 | check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false, false); |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 389 | |
| 390 | // RAW |
msarett | 02cb4d4 | 2016-01-25 11:01:34 -0800 | [diff] [blame] | 391 | #if defined(SK_CODEC_DECODES_RAW) |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 392 | check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false); |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 393 | check(r, "dng_with_preview.dng", SkISize::Make(600, 338), true, false, false); |
msarett | 02cb4d4 | 2016-01-25 11:01:34 -0800 | [diff] [blame] | 394 | #endif |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 395 | } |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 396 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 397 | // Test interlaced PNG in stripes, similar to DM's kStripe_Mode |
| 398 | DEF_TEST(Codec_stripes, r) { |
| 399 | const char * path = "plane_interlaced.png"; |
| 400 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 401 | if (!stream) { |
| 402 | SkDebugf("Missing resource '%s'\n", path); |
| 403 | } |
| 404 | |
| 405 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach())); |
| 406 | REPORTER_ASSERT(r, codec); |
| 407 | |
| 408 | if (!codec) { |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | switch (codec->getScanlineOrder()) { |
| 413 | case SkCodec::kBottomUp_SkScanlineOrder: |
| 414 | case SkCodec::kOutOfOrder_SkScanlineOrder: |
| 415 | ERRORF(r, "This scanline order will not match the original."); |
| 416 | return; |
| 417 | default: |
| 418 | break; |
| 419 | } |
| 420 | |
| 421 | // Baseline for what the image should look like, using N32. |
| 422 | const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType); |
| 423 | |
| 424 | SkBitmap bm; |
| 425 | bm.allocPixels(info); |
| 426 | SkAutoLockPixels autoLockPixels(bm); |
| 427 | SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes()); |
| 428 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 429 | |
| 430 | SkMD5::Digest digest; |
| 431 | md5(bm, &digest); |
| 432 | |
| 433 | // Now decode in stripes |
| 434 | const int height = info.height(); |
| 435 | const int numStripes = 4; |
| 436 | int stripeHeight; |
| 437 | int remainingLines; |
| 438 | SkTDivMod(height, numStripes, &stripeHeight, &remainingLines); |
| 439 | |
| 440 | bm.eraseColor(SK_ColorYELLOW); |
| 441 | |
| 442 | result = codec->startScanlineDecode(info); |
| 443 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 444 | |
| 445 | // Odd stripes |
| 446 | for (int i = 1; i < numStripes; i += 2) { |
| 447 | // Skip the even stripes |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 448 | bool skipResult = codec->skipScanlines(stripeHeight); |
| 449 | REPORTER_ASSERT(r, skipResult); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 450 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 451 | int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 452 | bm.rowBytes()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 453 | REPORTER_ASSERT(r, linesDecoded == stripeHeight); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | // Even stripes |
| 457 | result = codec->startScanlineDecode(info); |
| 458 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 459 | |
| 460 | for (int i = 0; i < numStripes; i += 2) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 461 | int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 462 | bm.rowBytes()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 463 | REPORTER_ASSERT(r, linesDecoded == stripeHeight); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 464 | |
| 465 | // Skip the odd stripes |
| 466 | if (i + 1 < numStripes) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 467 | bool skipResult = codec->skipScanlines(stripeHeight); |
| 468 | REPORTER_ASSERT(r, skipResult); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
| 472 | // Remainder at the end |
| 473 | if (remainingLines > 0) { |
| 474 | result = codec->startScanlineDecode(info); |
| 475 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 476 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 477 | bool skipResult = codec->skipScanlines(height - remainingLines); |
| 478 | REPORTER_ASSERT(r, skipResult); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 479 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 480 | int linesDecoded = codec->getScanlines(bm.getAddr(0, height - remainingLines), |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 481 | remainingLines, bm.rowBytes()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 482 | REPORTER_ASSERT(r, linesDecoded == remainingLines); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | compare_to_good_digest(r, digest, bm); |
| 486 | } |
| 487 | |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 488 | 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] | 489 | // 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] | 490 | SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false)); |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 491 | REPORTER_ASSERT(r, !codec); |
| 492 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 493 | SkAndroidCodec* androidCodec = |
| 494 | SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false)); |
| 495 | REPORTER_ASSERT(r, !androidCodec); |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | // Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream, |
| 499 | // even on failure. Test some bad streams. |
| 500 | DEF_TEST(Codec_leaks, r) { |
| 501 | // No codec should claim this as their format, so this tests SkCodec::NewFromStream. |
| 502 | const char nonSupportedStream[] = "hello world"; |
| 503 | // The other strings should look like the beginning of a file type, so we'll call some |
| 504 | // internal version of NewFromStream, which must also delete the stream on failure. |
| 505 | const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }; |
| 506 | const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF }; |
| 507 | const char emptyWebp[] = "RIFF1234WEBPVP"; |
| 508 | const char emptyBmp[] = { 'B', 'M' }; |
| 509 | const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' }; |
| 510 | const char emptyGif[] = "GIFVER"; |
| 511 | |
| 512 | test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream)); |
| 513 | test_invalid_stream(r, emptyPng, sizeof(emptyPng)); |
| 514 | test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg)); |
| 515 | test_invalid_stream(r, emptyWebp, sizeof(emptyWebp)); |
| 516 | test_invalid_stream(r, emptyBmp, sizeof(emptyBmp)); |
| 517 | test_invalid_stream(r, emptyIco, sizeof(emptyIco)); |
| 518 | test_invalid_stream(r, emptyGif, sizeof(emptyGif)); |
| 519 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 520 | |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 521 | DEF_TEST(Codec_null, r) { |
| 522 | // Attempting to create an SkCodec or an SkScaledCodec with null should not |
| 523 | // crash. |
| 524 | SkCodec* codec = SkCodec::NewFromStream(nullptr); |
| 525 | REPORTER_ASSERT(r, !codec); |
| 526 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 527 | SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr); |
| 528 | REPORTER_ASSERT(r, !androidCodec); |
scroggo | 2c3b218 | 2015-10-09 08:40:59 -0700 | [diff] [blame] | 529 | } |
| 530 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 531 | static void test_dimensions(skiatest::Reporter* r, const char path[]) { |
| 532 | // Create the codec from the resource file |
| 533 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 534 | if (!stream) { |
| 535 | SkDebugf("Missing resource '%s'\n", path); |
| 536 | return; |
| 537 | } |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 538 | SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.detach())); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 539 | if (!codec) { |
| 540 | ERRORF(r, "Unable to create codec '%s'", path); |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | // Check that the decode is successful for a variety of scales |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 545 | for (int sampleSize = 1; sampleSize < 32; sampleSize++) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 546 | // Scale the output dimensions |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 547 | SkISize scaledDims = codec->getSampledDimensions(sampleSize); |
msarett | b32758a | 2015-08-18 13:22:46 -0700 | [diff] [blame] | 548 | SkImageInfo scaledInfo = codec->getInfo() |
| 549 | .makeWH(scaledDims.width(), scaledDims.height()) |
| 550 | .makeColorType(kN32_SkColorType); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 551 | |
| 552 | // Set up for the decode |
| 553 | size_t rowBytes = scaledDims.width() * sizeof(SkPMColor); |
| 554 | size_t totalBytes = scaledInfo.getSafeSize(rowBytes); |
| 555 | SkAutoTMalloc<SkPMColor> pixels(totalBytes); |
| 556 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 557 | SkAndroidCodec::AndroidOptions options; |
| 558 | options.fSampleSize = sampleSize; |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 559 | SkCodec::Result result = |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 560 | codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 561 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
| 565 | // Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes |
| 566 | DEF_TEST(Codec_Dimensions, r) { |
| 567 | // JPG |
| 568 | test_dimensions(r, "CMYK.jpg"); |
| 569 | test_dimensions(r, "color_wheel.jpg"); |
| 570 | test_dimensions(r, "grayscale.jpg"); |
| 571 | test_dimensions(r, "mandrill_512_q075.jpg"); |
| 572 | test_dimensions(r, "randPixels.jpg"); |
msarett | b32758a | 2015-08-18 13:22:46 -0700 | [diff] [blame] | 573 | |
| 574 | // Decoding small images with very large scaling factors is a potential |
| 575 | // source of bugs and crashes. We disable these tests in Gold because |
| 576 | // tiny images are not very useful to look at. |
| 577 | // Here we make sure that we do not crash or access illegal memory when |
| 578 | // performing scaled decodes on small images. |
| 579 | test_dimensions(r, "1x1.png"); |
| 580 | test_dimensions(r, "2x2.png"); |
| 581 | test_dimensions(r, "3x3.png"); |
| 582 | test_dimensions(r, "3x1.png"); |
| 583 | test_dimensions(r, "1x1.png"); |
| 584 | test_dimensions(r, "16x1.png"); |
| 585 | test_dimensions(r, "1x16.png"); |
| 586 | test_dimensions(r, "mandrill_16.png"); |
| 587 | |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 588 | // RAW |
msarett | 8e49ca3 | 2016-01-25 13:10:58 -0800 | [diff] [blame] | 589 | #if defined(SK_CODEC_DECODES_RAW) |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 590 | test_dimensions(r, "sample_1mp.dng"); |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 591 | test_dimensions(r, "dng_with_preview.dng"); |
msarett | 8e49ca3 | 2016-01-25 13:10:58 -0800 | [diff] [blame] | 592 | #endif |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 593 | } |
| 594 | |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 595 | static void test_invalid(skiatest::Reporter* r, const char path[]) { |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 596 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 597 | if (!stream) { |
| 598 | SkDebugf("Missing resource '%s'\n", path); |
| 599 | return; |
| 600 | } |
| 601 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 602 | REPORTER_ASSERT(r, nullptr == codec); |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 603 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 604 | |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 605 | DEF_TEST(Codec_Empty, r) { |
| 606 | // Test images that should not be able to create a codec |
msarett | d0375bc | 2015-08-12 08:08:56 -0700 | [diff] [blame] | 607 | test_invalid(r, "empty_images/zero-dims.gif"); |
| 608 | test_invalid(r, "empty_images/zero-embedded.ico"); |
| 609 | test_invalid(r, "empty_images/zero-width.bmp"); |
| 610 | test_invalid(r, "empty_images/zero-height.bmp"); |
| 611 | test_invalid(r, "empty_images/zero-width.jpg"); |
| 612 | test_invalid(r, "empty_images/zero-height.jpg"); |
| 613 | test_invalid(r, "empty_images/zero-width.png"); |
| 614 | test_invalid(r, "empty_images/zero-height.png"); |
| 615 | test_invalid(r, "empty_images/zero-width.wbmp"); |
| 616 | test_invalid(r, "empty_images/zero-height.wbmp"); |
| 617 | // This image is an ico with an embedded mask-bmp. This is illegal. |
| 618 | test_invalid(r, "invalid_images/mask-bmp-ico.ico"); |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 619 | } |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 620 | |
| 621 | static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) { |
| 622 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 623 | if (!stream) { |
| 624 | SkDebugf("Missing resource '%s'\n", path); |
| 625 | return; |
| 626 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 627 | SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.detach())); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 628 | |
| 629 | // This should return kSuccess because kIndex8 is supported. |
| 630 | SkPMColor colorStorage[256]; |
| 631 | int colorCount; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 632 | SkCodec::Result result = decoder->startScanlineDecode( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 633 | decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 634 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 635 | // The rest of the test is uninteresting if kIndex8 is not supported |
| 636 | if (SkCodec::kSuccess != result) { |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid |
| 641 | // colorPtr and a valid colorCountPtr. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 642 | result = decoder->startScanlineDecode( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 643 | decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 644 | REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 645 | result = decoder->startScanlineDecode( |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 646 | decoder->getInfo().makeColorType(kIndex_8_SkColorType)); |
| 647 | REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); |
| 648 | } |
| 649 | |
| 650 | DEF_TEST(Codec_Params, r) { |
| 651 | test_invalid_parameters(r, "index8.png"); |
| 652 | test_invalid_parameters(r, "mandrill.wbmp"); |
| 653 | } |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 654 | |
| 655 | static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) { |
| 656 | SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr); |
| 657 | if (!sk_stream->write(data, len)) { |
| 658 | png_error(png_ptr, "sk_write_fn Error!"); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
| 663 | DEF_TEST(Codec_pngChunkReader, r) { |
| 664 | // Create a dummy bitmap. Use unpremul RGBA for libpng. |
| 665 | SkBitmap bm; |
| 666 | const int w = 1; |
| 667 | const int h = 1; |
| 668 | const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, |
| 669 | kUnpremul_SkAlphaType); |
| 670 | bm.setInfo(bmInfo); |
| 671 | bm.allocPixels(); |
| 672 | bm.eraseColor(SK_ColorBLUE); |
| 673 | SkMD5::Digest goodDigest; |
| 674 | md5(bm, &goodDigest); |
| 675 | |
| 676 | // Write to a png file. |
| 677 | png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
| 678 | REPORTER_ASSERT(r, png); |
| 679 | if (!png) { |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | png_infop info = png_create_info_struct(png); |
| 684 | REPORTER_ASSERT(r, info); |
| 685 | if (!info) { |
| 686 | png_destroy_write_struct(&png, nullptr); |
| 687 | return; |
| 688 | } |
| 689 | |
| 690 | if (setjmp(png_jmpbuf(png))) { |
| 691 | ERRORF(r, "failed writing png"); |
| 692 | png_destroy_write_struct(&png, &info); |
| 693 | return; |
| 694 | } |
| 695 | |
| 696 | SkDynamicMemoryWStream wStream; |
| 697 | png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr); |
| 698 | |
| 699 | png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8, |
| 700 | PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, |
| 701 | PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
| 702 | |
| 703 | // Create some chunks that match the Android framework's use. |
| 704 | static png_unknown_chunk gUnknowns[] = { |
msarett | 133eaaa | 2016-01-07 11:03:25 -0800 | [diff] [blame] | 705 | { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR }, |
| 706 | { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR }, |
| 707 | { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR }, |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 708 | }; |
| 709 | |
| 710 | png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3); |
| 711 | png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns)); |
| 712 | #if PNG_LIBPNG_VER < 10600 |
| 713 | /* Deal with unknown chunk location bug in 1.5.x and earlier */ |
msarett | 133eaaa | 2016-01-07 11:03:25 -0800 | [diff] [blame] | 714 | png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR); |
| 715 | png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR); |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 716 | #endif |
| 717 | |
| 718 | png_write_info(png, info); |
| 719 | |
| 720 | for (int j = 0; j < h; j++) { |
| 721 | png_bytep row = (png_bytep)(bm.getAddr(0, j)); |
| 722 | png_write_rows(png, &row, 1); |
| 723 | } |
| 724 | png_write_end(png, info); |
| 725 | png_destroy_write_struct(&png, &info); |
| 726 | |
| 727 | class ChunkReader : public SkPngChunkReader { |
| 728 | public: |
| 729 | ChunkReader(skiatest::Reporter* r) |
| 730 | : fReporter(r) |
| 731 | { |
| 732 | this->reset(); |
| 733 | } |
| 734 | |
| 735 | bool readChunk(const char tag[], const void* data, size_t length) override { |
| 736 | for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) { |
| 737 | if (!strcmp(tag, (const char*) gUnknowns[i].name)) { |
| 738 | // Tag matches. This should have been the first time we see it. |
| 739 | REPORTER_ASSERT(fReporter, !fSeen[i]); |
| 740 | fSeen[i] = true; |
| 741 | |
| 742 | // Data and length should match |
| 743 | REPORTER_ASSERT(fReporter, length == gUnknowns[i].size); |
| 744 | REPORTER_ASSERT(fReporter, !strcmp((const char*) data, |
| 745 | (const char*) gUnknowns[i].data)); |
| 746 | return true; |
| 747 | } |
| 748 | } |
| 749 | ERRORF(fReporter, "Saw an unexpected unknown chunk."); |
| 750 | return true; |
| 751 | } |
| 752 | |
| 753 | bool allHaveBeenSeen() { |
| 754 | bool ret = true; |
| 755 | for (auto seen : fSeen) { |
| 756 | ret &= seen; |
| 757 | } |
| 758 | return ret; |
| 759 | } |
| 760 | |
| 761 | void reset() { |
| 762 | sk_bzero(fSeen, sizeof(fSeen)); |
| 763 | } |
| 764 | |
| 765 | private: |
| 766 | skiatest::Reporter* fReporter; // Unowned |
| 767 | bool fSeen[3]; |
| 768 | }; |
| 769 | |
| 770 | ChunkReader chunkReader(r); |
| 771 | |
| 772 | // Now read the file with SkCodec. |
| 773 | SkAutoTUnref<SkData> data(wStream.copyToData()); |
| 774 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data, &chunkReader)); |
| 775 | REPORTER_ASSERT(r, codec); |
| 776 | if (!codec) { |
| 777 | return; |
| 778 | } |
| 779 | |
| 780 | // Now compare to the original. |
| 781 | SkBitmap decodedBm; |
| 782 | decodedBm.setInfo(codec->getInfo()); |
| 783 | decodedBm.allocPixels(); |
| 784 | SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), |
| 785 | decodedBm.rowBytes()); |
| 786 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 787 | |
| 788 | if (decodedBm.colorType() != bm.colorType()) { |
| 789 | SkBitmap tmp; |
| 790 | bool success = decodedBm.copyTo(&tmp, bm.colorType()); |
| 791 | REPORTER_ASSERT(r, success); |
| 792 | if (!success) { |
| 793 | return; |
| 794 | } |
| 795 | |
| 796 | tmp.swap(decodedBm); |
| 797 | } |
| 798 | |
| 799 | compare_to_good_digest(r, goodDigest, decodedBm); |
| 800 | REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen()); |
| 801 | |
| 802 | // Decoding again will read the chunks again. |
| 803 | chunkReader.reset(); |
| 804 | REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen()); |
| 805 | result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes()); |
| 806 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 807 | REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen()); |
| 808 | } |
| 809 | #endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 810 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 811 | // Stream that can only peek up to a limit |
| 812 | class LimitedPeekingMemStream : public SkStream { |
| 813 | public: |
| 814 | LimitedPeekingMemStream(SkData* data, size_t limit) |
| 815 | : fStream(data) |
| 816 | , fLimit(limit) {} |
| 817 | |
| 818 | size_t peek(void* buf, size_t bytes) const override { |
| 819 | return fStream.peek(buf, SkTMin(bytes, fLimit)); |
| 820 | } |
| 821 | size_t read(void* buf, size_t bytes) override { |
| 822 | return fStream.read(buf, bytes); |
| 823 | } |
| 824 | bool rewind() override { |
| 825 | return fStream.rewind(); |
| 826 | } |
| 827 | bool isAtEnd() const override { |
| 828 | return false; |
| 829 | } |
| 830 | private: |
| 831 | SkMemoryStream fStream; |
| 832 | const size_t fLimit; |
| 833 | }; |
| 834 | |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 835 | // Stream that is not an asset stream (!hasPosition() or !hasLength()) |
| 836 | class NotAssetMemStream : public SkStream { |
| 837 | public: |
| 838 | NotAssetMemStream(SkData* data) : fStream(data) {} |
| 839 | |
| 840 | bool hasPosition() const override { |
| 841 | return false; |
| 842 | } |
| 843 | |
| 844 | bool hasLength() const override { |
| 845 | return false; |
| 846 | } |
| 847 | |
| 848 | size_t peek(void* buf, size_t bytes) const override { |
| 849 | return fStream.peek(buf, bytes); |
| 850 | } |
| 851 | size_t read(void* buf, size_t bytes) override { |
| 852 | return fStream.read(buf, bytes); |
| 853 | } |
| 854 | bool rewind() override { |
| 855 | return fStream.rewind(); |
| 856 | } |
| 857 | bool isAtEnd() const override { |
| 858 | return fStream.isAtEnd(); |
| 859 | } |
| 860 | private: |
| 861 | SkMemoryStream fStream; |
| 862 | }; |
| 863 | |
| 864 | // Test that the RawCodec works also for not asset stream. This will test the code path using |
| 865 | // SkRawBufferedStream instead of SkRawAssetStream. |
| 866 | #if defined(SK_CODEC_DECODES_RAW) |
| 867 | DEF_TEST(Codec_raw_notseekable, r) { |
| 868 | const char* path = "dng_with_preview.dng"; |
| 869 | SkString fullPath(GetResourcePath(path)); |
| 870 | SkAutoTUnref<SkData> data(SkData::NewFromFileName(fullPath.c_str())); |
| 871 | if (!data) { |
| 872 | SkDebugf("Missing resource '%s'\n", path); |
| 873 | return; |
| 874 | } |
| 875 | |
| 876 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(data))); |
| 877 | REPORTER_ASSERT(r, codec); |
| 878 | |
| 879 | test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr); |
| 880 | } |
| 881 | #endif |
| 882 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 883 | // Test that even if webp_parse_header fails to peek enough, it will fall back to read() |
| 884 | // + rewind() and succeed. |
| 885 | DEF_TEST(Codec_webp_peek, r) { |
| 886 | const char* path = "baby_tux.webp"; |
| 887 | SkString fullPath(GetResourcePath(path)); |
| 888 | SkAutoTUnref<SkData> data(SkData::NewFromFileName(fullPath.c_str())); |
| 889 | if (!data) { |
| 890 | SkDebugf("Missing resource '%s'\n", path); |
| 891 | return; |
| 892 | } |
| 893 | |
| 894 | // The limit is less than webp needs to peek or read. |
| 895 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 25))); |
| 896 | REPORTER_ASSERT(r, codec); |
| 897 | |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 898 | test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 899 | |
| 900 | // Similarly, a stream which does not peek should still succeed. |
| 901 | codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0))); |
| 902 | REPORTER_ASSERT(r, codec); |
| 903 | |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 904 | test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 905 | } |
| 906 | |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 907 | // SkCodec's wbmp decoder was initially more restrictive than SkImageDecoder. |
| 908 | // It required the second byte to be zero. But SkImageDecoder allowed a couple |
| 909 | // of bits to be 1 (so long as they do not overlap with 0x9F). Test that |
| 910 | // SkCodec now supports an image with these bits set. |
| 911 | DEF_TEST(Codec_wbmp, r) { |
| 912 | const char* path = "mandrill.wbmp"; |
| 913 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 914 | if (!stream) { |
| 915 | SkDebugf("Missing resource '%s'\n", path); |
| 916 | return; |
| 917 | } |
| 918 | |
| 919 | // Modify the stream to contain a second byte with some bits set. |
| 920 | SkAutoTUnref<SkData> data(SkCopyStreamToData(stream)); |
| 921 | uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data()); |
| 922 | writeableData[1] = static_cast<uint8_t>(~0x9F); |
| 923 | |
| 924 | // SkImageDecoder supports this. |
| 925 | SkBitmap bitmap; |
| 926 | REPORTER_ASSERT(r, SkImageDecoder::DecodeMemory(data->data(), data->size(), &bitmap)); |
| 927 | |
| 928 | // So SkCodec should, too. |
| 929 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data)); |
| 930 | REPORTER_ASSERT(r, codec); |
| 931 | if (!codec) { |
| 932 | return; |
| 933 | } |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 934 | test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr); |
scroggo | b9a1e34 | 2015-11-30 06:25:31 -0800 | [diff] [blame] | 935 | } |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 936 | |
| 937 | // wbmp images have a header that can be arbitrarily large, depending on the |
| 938 | // size of the image. We cap the size at 65535, meaning we only need to look at |
| 939 | // 8 bytes to determine whether we can read the image. This is important |
| 940 | // because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the |
| 941 | // image is a wbmp. |
| 942 | DEF_TEST(Codec_wbmp_max_size, r) { |
| 943 | const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header |
| 944 | 0x83, 0xFF, 0x7F, // W: 65535 |
| 945 | 0x83, 0xFF, 0x7F }; // H: 65535 |
| 946 | SkAutoTDelete<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false)); |
| 947 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach())); |
| 948 | |
| 949 | REPORTER_ASSERT(r, codec); |
| 950 | if (!codec) return; |
| 951 | |
| 952 | REPORTER_ASSERT(r, codec->getInfo().width() == 65535); |
| 953 | REPORTER_ASSERT(r, codec->getInfo().height() == 65535); |
| 954 | |
| 955 | // Now test an image which is too big. Any image with a larger header (i.e. |
| 956 | // has bigger width/height) is also too big. |
| 957 | const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header |
| 958 | 0x84, 0x80, 0x00, // W: 65536 |
| 959 | 0x84, 0x80, 0x00 }; // H: 65536 |
| 960 | stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false)); |
| 961 | codec.reset(SkCodec::NewFromStream(stream.detach())); |
| 962 | |
| 963 | REPORTER_ASSERT(r, !codec); |
| 964 | } |