blob: 05e8c3f4e807cf6dc2d78ccaaa6ae0c508fcea81 [file] [log] [blame]
halcanarya096d7a2015-03-27 12:16:53 -07001/*
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"
msarett3d9d7a72015-10-21 10:27:10 -07009#include "SkAndroidCodec.h"
halcanarya096d7a2015-03-27 12:16:53 -070010#include "SkBitmap.h"
11#include "SkCodec.h"
msarettedd2dcf2016-01-14 13:12:26 -080012#include "SkCodecImageGenerator.h"
raftias94888332016-10-18 10:02:51 -070013#include "SkColorSpace_XYZ.h"
msarette6dd0042015-10-09 11:07:34 -070014#include "SkData.h"
msarett549ca322016-08-17 08:54:08 -070015#include "SkImageEncoder.h"
scroggoef0fed32016-02-18 05:59:25 -080016#include "SkFrontBufferedStream.h"
halcanarya096d7a2015-03-27 12:16:53 -070017#include "SkMD5.h"
scroggob636b452015-07-22 07:16:20 -070018#include "SkRandom.h"
scroggocf98fa92015-11-23 08:14:40 -080019#include "SkStream.h"
scroggob9a1e342015-11-30 06:25:31 -080020#include "SkStreamPriv.h"
scroggocf98fa92015-11-23 08:14:40 -080021#include "SkPngChunkReader.h"
halcanarya096d7a2015-03-27 12:16:53 -070022#include "Test.h"
23
scroggocf98fa92015-11-23 08:14:40 -080024#include "png.h"
25
Hal Canarydb683012016-11-23 08:55:18 -070026#include "sk_tool_utils.h"
27
scroggo8e6c7ad2016-09-16 08:20:38 -070028#if PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 5
29 // FIXME (scroggo): Google3 needs to be updated to use a newer version of libpng. In
30 // the meantime, we had to break some pieces of SkPngCodec in order to support Google3.
31 // The parts that are broken are likely not used by Google3.
32 #define SK_PNG_DISABLE_TESTS
33#endif
34
halcanarya096d7a2015-03-27 12:16:53 -070035static void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
36 SkAutoLockPixels autoLockPixels(bm);
37 SkASSERT(bm.getPixels());
38 SkMD5 md5;
39 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
40 for (int y = 0; y < bm.height(); ++y) {
halcanary1e903042016-04-25 10:29:36 -070041 md5.write(bm.getAddr(0, y), rowLen);
halcanarya096d7a2015-03-27 12:16:53 -070042 }
43 md5.finish(*digest);
44}
45
scroggo9b2cdbf42015-07-10 12:07:02 -070046/**
47 * Compute the digest for bm and compare it to a known good digest.
48 * @param r Reporter to assert that bm's digest matches goodDigest.
49 * @param goodDigest The known good digest to compare to.
50 * @param bm The bitmap to test.
51 */
52static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
53 const SkBitmap& bm) {
54 SkMD5::Digest digest;
55 md5(bm, &digest);
56 REPORTER_ASSERT(r, digest == goodDigest);
57}
58
scroggod1bc5742015-08-12 08:31:44 -070059/**
60 * Test decoding an SkCodec to a particular SkImageInfo.
61 *
halcanary96fcdcc2015-08-27 07:41:13 -070062 * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr,
scroggod1bc5742015-08-12 08:31:44 -070063 * the resulting decode should match.
64 */
scroggo7b5e5532016-02-04 06:14:24 -080065template<typename Codec>
66static void test_info(skiatest::Reporter* r, Codec* codec, const SkImageInfo& info,
scroggod1bc5742015-08-12 08:31:44 -070067 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
68 SkBitmap bm;
69 bm.allocPixels(info);
70 SkAutoLockPixels autoLockPixels(bm);
71
72 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
73 REPORTER_ASSERT(r, result == expectedResult);
74
75 if (goodDigest) {
76 compare_to_good_digest(r, *goodDigest, bm);
77 }
78}
79
scroggob636b452015-07-22 07:16:20 -070080SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
81 SkIRect rect;
82 do {
83 rect.fLeft = rand->nextRangeU(0, w);
84 rect.fTop = rand->nextRangeU(0, h);
85 rect.fRight = rand->nextRangeU(0, w);
86 rect.fBottom = rand->nextRangeU(0, h);
87 rect.sort();
88 } while (rect.isEmpty());
89 return rect;
90}
91
scroggo8e6c7ad2016-09-16 08:20:38 -070092static void test_incremental_decode(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
93 const SkMD5::Digest& goodDigest) {
94 SkBitmap bm;
95 bm.allocPixels(info);
96 SkAutoLockPixels autoLockPixels(bm);
97
98 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->startIncrementalDecode(info, bm.getPixels(),
99 bm.rowBytes()));
100
101 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->incrementalDecode());
102
103 compare_to_good_digest(r, goodDigest, bm);
104}
105
106// Test in stripes, similar to DM's kStripe_Mode
107static void test_in_stripes(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
108 const SkMD5::Digest& goodDigest) {
109 SkBitmap bm;
110 bm.allocPixels(info);
111 bm.eraseColor(SK_ColorYELLOW);
112
113 const int height = info.height();
114 // Note that if numStripes does not evenly divide height there will be an extra
115 // stripe.
116 const int numStripes = 4;
117
118 if (numStripes > height) {
119 // Image is too small.
120 return;
121 }
122
123 const int stripeHeight = height / numStripes;
124
125 // Iterate through the image twice. Once to decode odd stripes, and once for even.
126 for (int oddEven = 1; oddEven >= 0; oddEven--) {
127 for (int y = oddEven * stripeHeight; y < height; y += 2 * stripeHeight) {
128 SkIRect subset = SkIRect::MakeLTRB(0, y, info.width(),
129 SkTMin(y + stripeHeight, height));
130 SkCodec::Options options;
131 options.fSubset = &subset;
132 if (SkCodec::kSuccess != codec->startIncrementalDecode(info, bm.getAddr(0, y),
133 bm.rowBytes(), &options)) {
134 ERRORF(r, "failed to start incremental decode!\ttop: %i\tbottom%i\n",
135 subset.top(), subset.bottom());
136 return;
137 }
138 if (SkCodec::kSuccess != codec->incrementalDecode()) {
139 ERRORF(r, "failed incremental decode starting from line %i\n", y);
140 return;
141 }
142 }
143 }
144
145 compare_to_good_digest(r, goodDigest, bm);
146}
147
scroggo7b5e5532016-02-04 06:14:24 -0800148template<typename Codec>
149static void test_codec(skiatest::Reporter* r, Codec* codec, SkBitmap& bm, const SkImageInfo& info,
scroggo27c17282015-10-27 08:14:46 -0700150 const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest,
151 const SkMD5::Digest* goodDigest) {
msarette6dd0042015-10-09 11:07:34 -0700152
halcanarya096d7a2015-03-27 12:16:53 -0700153 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -0700154 bm.allocPixels(info);
155 SkAutoLockPixels autoLockPixels(bm);
msarettcc7f3052015-10-05 14:20:27 -0700156
157 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700158 REPORTER_ASSERT(r, result == expectedResult);
halcanarya096d7a2015-03-27 12:16:53 -0700159
msarettcc7f3052015-10-05 14:20:27 -0700160 md5(bm, digest);
161 if (goodDigest) {
162 REPORTER_ASSERT(r, *digest == *goodDigest);
163 }
halcanarya096d7a2015-03-27 12:16:53 -0700164
msarett8ff6ca62015-09-18 12:06:04 -0700165 {
166 // Test decoding to 565
167 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggoba584892016-05-20 13:56:13 -0700168 if (info.alphaType() == kOpaque_SkAlphaType) {
169 // Decoding to 565 should succeed.
170 SkBitmap bm565;
171 bm565.allocPixels(info565);
172 SkAutoLockPixels alp(bm565);
173
174 // This will allow comparison even if the image is incomplete.
175 bm565.eraseColor(SK_ColorBLACK);
176
177 REPORTER_ASSERT(r, expectedResult == codec->getPixels(info565,
178 bm565.getPixels(), bm565.rowBytes()));
179
180 SkMD5::Digest digest565;
181 md5(bm565, &digest565);
182
183 // A dumb client's request for non-opaque should also succeed.
184 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
185 info565 = info565.makeAlphaType(alpha);
186 test_info(r, codec, info565, expectedResult, &digest565);
187 }
188 } else {
189 test_info(r, codec, info565, SkCodec::kInvalidConversion, nullptr);
190 }
191 }
192
193 if (codec->getInfo().colorType() == kGray_8_SkColorType) {
194 SkImageInfo grayInfo = codec->getInfo();
195 SkBitmap grayBm;
196 grayBm.allocPixels(grayInfo);
197 SkAutoLockPixels alp(grayBm);
198
199 grayBm.eraseColor(SK_ColorBLACK);
200
201 REPORTER_ASSERT(r, expectedResult == codec->getPixels(grayInfo,
202 grayBm.getPixels(), grayBm.rowBytes()));
203
204 SkMD5::Digest grayDigest;
205 md5(grayBm, &grayDigest);
206
207 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
208 grayInfo = grayInfo.makeAlphaType(alpha);
209 test_info(r, codec, grayInfo, expectedResult, &grayDigest);
210 }
msarett8ff6ca62015-09-18 12:06:04 -0700211 }
212
213 // Verify that re-decoding gives the same result. It is interesting to check this after
214 // a decode to 565, since choosing to decode to 565 may result in some of the decode
215 // options being modified. These options should return to their defaults on another
216 // decode to kN32, so the new digest should match the old digest.
msarette6dd0042015-10-09 11:07:34 -0700217 test_info(r, codec, info, expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700218
219 {
220 // Check alpha type conversions
221 if (info.alphaType() == kOpaque_SkAlphaType) {
222 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800223 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700224 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800225 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700226 } else {
227 // Decoding to opaque should fail
228 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700229 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700230 SkAlphaType otherAt = info.alphaType();
231 if (kPremul_SkAlphaType == otherAt) {
232 otherAt = kUnpremul_SkAlphaType;
233 } else {
234 otherAt = kPremul_SkAlphaType;
235 }
236 // The other non-opaque alpha type should always succeed, but not match.
msarette6dd0042015-10-09 11:07:34 -0700237 test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700238 }
239 }
msarettcc7f3052015-10-05 14:20:27 -0700240}
241
scroggobed1ed62016-02-11 10:24:55 -0800242static bool supports_partial_scanlines(const char path[]) {
scroggo2c3b2182015-10-09 08:40:59 -0700243 static const char* const exts[] = {
244 "jpg", "jpeg", "png", "webp"
245 "JPG", "JPEG", "PNG", "WEBP"
246 };
247
248 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
249 if (SkStrEndsWith(path, exts[i])) {
250 return true;
251 }
252 }
253 return false;
254}
255
scroggo8e6c7ad2016-09-16 08:20:38 -0700256// FIXME: Break up this giant function
msarettcc7f3052015-10-05 14:20:27 -0700257static void check(skiatest::Reporter* r,
258 const char path[],
259 SkISize size,
260 bool supportsScanlineDecoding,
261 bool supportsSubsetDecoding,
scroggo8e6c7ad2016-09-16 08:20:38 -0700262 bool supportsIncomplete,
263 bool supportsNewScanlineDecoding = false) {
msarettcc7f3052015-10-05 14:20:27 -0700264
Ben Wagner145dbcd2016-11-03 14:40:50 -0400265 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarettcc7f3052015-10-05 14:20:27 -0700266 if (!stream) {
msarettcc7f3052015-10-05 14:20:27 -0700267 return;
268 }
msarette6dd0042015-10-09 11:07:34 -0700269
Ben Wagner145dbcd2016-11-03 14:40:50 -0400270 std::unique_ptr<SkCodec> codec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700271 bool isIncomplete = supportsIncomplete;
272 if (isIncomplete) {
273 size_t size = stream->getLength();
Ben Wagner145dbcd2016-11-03 14:40:50 -0400274 sk_sp<SkData> data((SkData::MakeFromStream(stream.get(), 2 * size / 3)));
reed42943c82016-09-12 12:01:44 -0700275 codec.reset(SkCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700276 } else {
mtklein18300a32016-03-16 13:53:35 -0700277 codec.reset(SkCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700278 }
msarettcc7f3052015-10-05 14:20:27 -0700279 if (!codec) {
280 ERRORF(r, "Unable to decode '%s'", path);
281 return;
282 }
283
284 // Test full image decodes with SkCodec
285 SkMD5::Digest codecDigest;
scroggoef0fed32016-02-18 05:59:25 -0800286 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
msarettcc7f3052015-10-05 14:20:27 -0700287 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700288 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
scroggo7b5e5532016-02-04 06:14:24 -0800289 test_codec(r, codec.get(), bm, info, size, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700290
291 // Scanline decoding follows.
scroggod8d68552016-06-06 11:26:17 -0700292
scroggo8e6c7ad2016-09-16 08:20:38 -0700293 if (supportsNewScanlineDecoding && !isIncomplete) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400294 test_incremental_decode(r, codec.get(), info, codecDigest);
scroggo19b91532016-10-24 09:03:26 -0700295 // This is only supported by codecs that use incremental decoding to
296 // support subset decodes - png and jpeg (once SkJpegCodec is
297 // converted).
298 if (SkStrEndsWith(path, "png") || SkStrEndsWith(path, "PNG")) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400299 test_in_stripes(r, codec.get(), info, codecDigest);
scroggo19b91532016-10-24 09:03:26 -0700300 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700301 }
302
303 // Need to call startScanlineDecode() first.
304 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) == 0);
305 REPORTER_ASSERT(r, !codec->skipScanlines(1));
scroggo46c57472015-09-30 08:57:13 -0700306 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700307 if (supportsScanlineDecoding) {
308 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700309
scroggo46c57472015-09-30 08:57:13 -0700310 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700311
scroggo58421542015-04-01 11:25:20 -0700312 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700313 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
314 if (!isIncomplete) {
315 REPORTER_ASSERT(r, 1 == lines);
316 }
scroggo58421542015-04-01 11:25:20 -0700317 }
318 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700319 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700320 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700321 }
scroggo46c57472015-09-30 08:57:13 -0700322
323 // Cannot continue to decode scanlines beyond the end
324 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700325 == 0);
scroggo46c57472015-09-30 08:57:13 -0700326
327 // Interrupting a scanline decode with a full decode starts from
328 // scratch
329 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700330 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
331 if (!isIncomplete) {
332 REPORTER_ASSERT(r, lines == 1);
333 }
scroggo46c57472015-09-30 08:57:13 -0700334 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700335 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700336 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700337 == 0);
scroggo46c57472015-09-30 08:57:13 -0700338 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700339 == 0);
msarett80803ff2015-10-16 10:54:12 -0700340
341 // Test partial scanline decodes
scroggobed1ed62016-02-11 10:24:55 -0800342 if (supports_partial_scanlines(path) && info.width() >= 3) {
msarett80803ff2015-10-16 10:54:12 -0700343 SkCodec::Options options;
344 int width = info.width();
345 int height = info.height();
346 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
347 options.fSubset = &subset;
348
349 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
350 nullptr, nullptr);
351 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
352
353 for (int y = 0; y < height; y++) {
354 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
355 if (!isIncomplete) {
356 REPORTER_ASSERT(r, 1 == lines);
357 }
358 }
359 }
scroggo58421542015-04-01 11:25:20 -0700360 } else {
scroggo46c57472015-09-30 08:57:13 -0700361 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700362 }
scroggob636b452015-07-22 07:16:20 -0700363
364 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
365 // random subsets.
366 // Do not attempt to decode subsets of an image of only once pixel, since there is no
367 // meaningful subset.
368 if (size.width() * size.height() == 1) {
369 return;
370 }
371
372 SkRandom rand;
373 SkIRect subset;
374 SkCodec::Options opts;
375 opts.fSubset = &subset;
376 for (int i = 0; i < 5; i++) {
377 subset = generate_random_subset(&rand, size.width(), size.height());
378 SkASSERT(!subset.isEmpty());
379 const bool supported = codec->getValidSubset(&subset);
380 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
381
382 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
383 SkBitmap bm;
384 bm.allocPixels(subsetInfo);
385 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700386 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700387
388 if (supportsSubsetDecoding) {
msarette6dd0042015-10-09 11:07:34 -0700389 REPORTER_ASSERT(r, result == expectedResult);
scroggob636b452015-07-22 07:16:20 -0700390 // Webp is the only codec that supports subsets, and it will have modified the subset
391 // to have even left/top.
392 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
393 } else {
394 // No subsets will work.
395 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
396 }
397 }
msarettcc7f3052015-10-05 14:20:27 -0700398
scroggobed1ed62016-02-11 10:24:55 -0800399 // SkAndroidCodec tests
scroggo8e6c7ad2016-09-16 08:20:38 -0700400 if (supportsScanlineDecoding || supportsSubsetDecoding || supportsNewScanlineDecoding) {
scroggo2c3b2182015-10-09 08:40:59 -0700401
Ben Wagner145dbcd2016-11-03 14:40:50 -0400402 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarettcc7f3052015-10-05 14:20:27 -0700403 if (!stream) {
msarettcc7f3052015-10-05 14:20:27 -0700404 return;
405 }
msarette6dd0042015-10-09 11:07:34 -0700406
Ben Wagner145dbcd2016-11-03 14:40:50 -0400407 std::unique_ptr<SkAndroidCodec> androidCodec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700408 if (isIncomplete) {
409 size_t size = stream->getLength();
Ben Wagner145dbcd2016-11-03 14:40:50 -0400410 sk_sp<SkData> data((SkData::MakeFromStream(stream.get(), 2 * size / 3)));
reed42943c82016-09-12 12:01:44 -0700411 androidCodec.reset(SkAndroidCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700412 } else {
mtklein18300a32016-03-16 13:53:35 -0700413 androidCodec.reset(SkAndroidCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700414 }
scroggo7b5e5532016-02-04 06:14:24 -0800415 if (!androidCodec) {
msarettcc7f3052015-10-05 14:20:27 -0700416 ERRORF(r, "Unable to decode '%s'", path);
417 return;
418 }
419
420 SkBitmap bm;
scroggobed1ed62016-02-11 10:24:55 -0800421 SkMD5::Digest androidCodecDigest;
422 test_codec(r, androidCodec.get(), bm, info, size, expectedResult, &androidCodecDigest,
scroggo7b5e5532016-02-04 06:14:24 -0800423 &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700424 }
425
msarettedd2dcf2016-01-14 13:12:26 -0800426 if (!isIncomplete) {
scroggoef0fed32016-02-18 05:59:25 -0800427 // Test SkCodecImageGenerator
Ben Wagner145dbcd2016-11-03 14:40:50 -0400428 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
429 sk_sp<SkData> fullData(SkData::MakeFromStream(stream.get(), stream->getLength()));
430 std::unique_ptr<SkImageGenerator> gen(
bungeman38d909e2016-08-02 14:40:46 -0700431 SkCodecImageGenerator::NewFromEncodedCodec(fullData.get()));
msarettedd2dcf2016-01-14 13:12:26 -0800432 SkBitmap bm;
433 bm.allocPixels(info);
434 SkAutoLockPixels autoLockPixels(bm);
435 REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes()));
436 compare_to_good_digest(r, codecDigest, bm);
scroggoef0fed32016-02-18 05:59:25 -0800437
scroggo8e6c7ad2016-09-16 08:20:38 -0700438#ifndef SK_PNG_DISABLE_TESTS
scroggod8d68552016-06-06 11:26:17 -0700439 // Test using SkFrontBufferedStream, as Android does
bungeman38d909e2016-08-02 14:40:46 -0700440 SkStream* bufferedStream = SkFrontBufferedStream::Create(
441 new SkMemoryStream(std::move(fullData)), SkCodec::MinBufferedBytesNeeded());
scroggod8d68552016-06-06 11:26:17 -0700442 REPORTER_ASSERT(r, bufferedStream);
443 codec.reset(SkCodec::NewFromStream(bufferedStream));
444 REPORTER_ASSERT(r, codec);
445 if (codec) {
446 test_info(r, codec.get(), info, SkCodec::kSuccess, &codecDigest);
scroggoef0fed32016-02-18 05:59:25 -0800447 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700448#endif
msarettedd2dcf2016-01-14 13:12:26 -0800449 }
450
msarette6dd0042015-10-09 11:07:34 -0700451 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
452 if (isIncomplete) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700453 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false,
454 supportsNewScanlineDecoding);
msarettcc7f3052015-10-05 14:20:27 -0700455 }
halcanarya096d7a2015-03-27 12:16:53 -0700456}
457
Leon Scroggins III83926342016-12-06 10:58:02 -0500458DEF_TEST(Codec_wbmp, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700459 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500460}
halcanarya096d7a2015-03-27 12:16:53 -0700461
Leon Scroggins III83926342016-12-06 10:58:02 -0500462DEF_TEST(Codec_webp, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700463 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true, true);
464 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true, true);
465 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500466}
scroggo6f5e6192015-06-18 12:53:43 -0700467
Leon Scroggins III83926342016-12-06 10:58:02 -0500468DEF_TEST(Codec_bmp, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700469 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false, true);
470 check(r, "rle.bmp", SkISize::Make(320, 240), true, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500471}
halcanarya096d7a2015-03-27 12:16:53 -0700472
Leon Scroggins III83926342016-12-06 10:58:02 -0500473DEF_TEST(Codec_ico, r) {
msarette6dd0042015-10-09 11:07:34 -0700474 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700475 // These two tests examine interestingly different behavior:
476 // Decodes an embedded BMP image
msarettbe8216a2015-12-04 08:00:50 -0800477 check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700478 // Decodes an embedded PNG image
scroggo8e6c7ad2016-09-16 08:20:38 -0700479 check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500480}
halcanarya096d7a2015-03-27 12:16:53 -0700481
Leon Scroggins III83926342016-12-06 10:58:02 -0500482DEF_TEST(Codec_gif, r) {
scroggo19b91532016-10-24 09:03:26 -0700483 check(r, "box.gif", SkISize::Make(200, 55), false, false, true, true);
484 check(r, "color_wheel.gif", SkISize::Make(128, 128), false, false, true, true);
msarette6dd0042015-10-09 11:07:34 -0700485 // randPixels.gif is too small to test incomplete
scroggo19b91532016-10-24 09:03:26 -0700486 check(r, "randPixels.gif", SkISize::Make(8, 8), false, false, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500487}
msarett438b2ad2015-04-09 12:43:10 -0700488
Leon Scroggins III83926342016-12-06 10:58:02 -0500489DEF_TEST(Codec_jpg, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700490 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false, true);
491 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700492 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700493 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggo8e6c7ad2016-09-16 08:20:38 -0700494 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700495 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700496 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
Leon Scroggins III83926342016-12-06 10:58:02 -0500497}
msarette16b04a2015-04-15 07:32:19 -0700498
Leon Scroggins III83926342016-12-06 10:58:02 -0500499DEF_TEST(Codec_png, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700500 check(r, "arrow.png", SkISize::Make(187, 312), false, false, true, true);
501 check(r, "baby_tux.png", SkISize::Make(240, 246), false, false, true, true);
502 check(r, "color_wheel.png", SkISize::Make(128, 128), false, false, true, true);
503 // half-transparent-white-pixel.png is too small to test incomplete
504 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), false, false, false, true);
505 check(r, "mandrill_128.png", SkISize::Make(128, 128), false, false, true, true);
506 check(r, "mandrill_16.png", SkISize::Make(16, 16), false, false, true, true);
507 check(r, "mandrill_256.png", SkISize::Make(256, 256), false, false, true, true);
508 check(r, "mandrill_32.png", SkISize::Make(32, 32), false, false, true, true);
509 check(r, "mandrill_512.png", SkISize::Make(512, 512), false, false, true, true);
510 check(r, "mandrill_64.png", SkISize::Make(64, 64), false, false, true, true);
511 check(r, "plane.png", SkISize::Make(250, 126), false, false, true, true);
512 check(r, "plane_interlaced.png", SkISize::Make(250, 126), false, false, true, true);
513 check(r, "randPixels.png", SkISize::Make(8, 8), false, false, true, true);
514 check(r, "yellow_rose.png", SkISize::Make(400, 301), false, false, true, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500515}
yujieqin916de9f2016-01-25 08:26:16 -0800516
yujieqinf236ee42016-02-29 07:14:42 -0800517// Disable RAW tests for Win32.
518#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Leon Scroggins III83926342016-12-06 10:58:02 -0500519DEF_TEST(Codec_raw, r) {
yujieqin916de9f2016-01-25 08:26:16 -0800520 check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
ebrauer46d2aa82016-02-17 08:04:00 -0800521 check(r, "sample_1mp_rotated.dng", SkISize::Make(600, 338), false, false, false);
yujieqin9c7a8a42016-02-05 08:21:19 -0800522 check(r, "dng_with_preview.dng", SkISize::Make(600, 338), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700523}
Leon Scroggins III83926342016-12-06 10:58:02 -0500524#endif
scroggo0a7e69c2015-04-03 07:22:22 -0700525
526static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700527 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700528 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700529 REPORTER_ASSERT(r, !codec);
530
msarett3d9d7a72015-10-21 10:27:10 -0700531 SkAndroidCodec* androidCodec =
532 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
533 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700534}
535
536// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
537// even on failure. Test some bad streams.
538DEF_TEST(Codec_leaks, r) {
539 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
540 const char nonSupportedStream[] = "hello world";
541 // The other strings should look like the beginning of a file type, so we'll call some
542 // internal version of NewFromStream, which must also delete the stream on failure.
543 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
544 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
545 const char emptyWebp[] = "RIFF1234WEBPVP";
546 const char emptyBmp[] = { 'B', 'M' };
547 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
548 const char emptyGif[] = "GIFVER";
549
550 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
551 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
552 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
553 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
554 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
555 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
556 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
557}
msarette16b04a2015-04-15 07:32:19 -0700558
scroggo2c3b2182015-10-09 08:40:59 -0700559DEF_TEST(Codec_null, r) {
scroggobed1ed62016-02-11 10:24:55 -0800560 // Attempting to create an SkCodec or an SkAndroidCodec with null should not
scroggo2c3b2182015-10-09 08:40:59 -0700561 // crash.
562 SkCodec* codec = SkCodec::NewFromStream(nullptr);
563 REPORTER_ASSERT(r, !codec);
564
msarett3d9d7a72015-10-21 10:27:10 -0700565 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
566 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700567}
568
msarette16b04a2015-04-15 07:32:19 -0700569static void test_dimensions(skiatest::Reporter* r, const char path[]) {
570 // Create the codec from the resource file
Ben Wagner145dbcd2016-11-03 14:40:50 -0400571 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarette16b04a2015-04-15 07:32:19 -0700572 if (!stream) {
msarette16b04a2015-04-15 07:32:19 -0700573 return;
574 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400575 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarette16b04a2015-04-15 07:32:19 -0700576 if (!codec) {
577 ERRORF(r, "Unable to create codec '%s'", path);
578 return;
579 }
580
581 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800582 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700583 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700584 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700585 SkImageInfo scaledInfo = codec->getInfo()
586 .makeWH(scaledDims.width(), scaledDims.height())
587 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700588
589 // Set up for the decode
590 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
591 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
592 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
593
msarett3d9d7a72015-10-21 10:27:10 -0700594 SkAndroidCodec::AndroidOptions options;
595 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700596 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700597 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700598 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700599 }
600}
601
602// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
603DEF_TEST(Codec_Dimensions, r) {
604 // JPG
605 test_dimensions(r, "CMYK.jpg");
606 test_dimensions(r, "color_wheel.jpg");
607 test_dimensions(r, "grayscale.jpg");
608 test_dimensions(r, "mandrill_512_q075.jpg");
609 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700610
611 // Decoding small images with very large scaling factors is a potential
612 // source of bugs and crashes. We disable these tests in Gold because
613 // tiny images are not very useful to look at.
614 // Here we make sure that we do not crash or access illegal memory when
615 // performing scaled decodes on small images.
616 test_dimensions(r, "1x1.png");
617 test_dimensions(r, "2x2.png");
618 test_dimensions(r, "3x3.png");
619 test_dimensions(r, "3x1.png");
620 test_dimensions(r, "1x1.png");
621 test_dimensions(r, "16x1.png");
622 test_dimensions(r, "1x16.png");
623 test_dimensions(r, "mandrill_16.png");
624
yujieqin916de9f2016-01-25 08:26:16 -0800625 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800626// Disable RAW tests for Win32.
627#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800628 test_dimensions(r, "sample_1mp.dng");
ebrauer46d2aa82016-02-17 08:04:00 -0800629 test_dimensions(r, "sample_1mp_rotated.dng");
yujieqin9c7a8a42016-02-05 08:21:19 -0800630 test_dimensions(r, "dng_with_preview.dng");
msarett8e49ca32016-01-25 13:10:58 -0800631#endif
msarette16b04a2015-04-15 07:32:19 -0700632}
633
msarettd0375bc2015-08-12 08:08:56 -0700634static void test_invalid(skiatest::Reporter* r, const char path[]) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400635 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett4b17fa32015-04-23 08:53:39 -0700636 if (!stream) {
msarett4b17fa32015-04-23 08:53:39 -0700637 return;
638 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400639 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
halcanary96fcdcc2015-08-27 07:41:13 -0700640 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700641}
msarette16b04a2015-04-15 07:32:19 -0700642
msarett4b17fa32015-04-23 08:53:39 -0700643DEF_TEST(Codec_Empty, r) {
644 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700645 test_invalid(r, "empty_images/zero-dims.gif");
646 test_invalid(r, "empty_images/zero-embedded.ico");
647 test_invalid(r, "empty_images/zero-width.bmp");
648 test_invalid(r, "empty_images/zero-height.bmp");
649 test_invalid(r, "empty_images/zero-width.jpg");
650 test_invalid(r, "empty_images/zero-height.jpg");
651 test_invalid(r, "empty_images/zero-width.png");
652 test_invalid(r, "empty_images/zero-height.png");
653 test_invalid(r, "empty_images/zero-width.wbmp");
654 test_invalid(r, "empty_images/zero-height.wbmp");
655 // This image is an ico with an embedded mask-bmp. This is illegal.
656 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
Leon Scroggins IIId87fbee2016-12-02 16:47:53 -0500657#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
658 test_invalid(r, "empty_images/zero_height.tiff");
659#endif
msarett4b17fa32015-04-23 08:53:39 -0700660}
msarett99f567e2015-08-05 12:58:26 -0700661
662static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400663 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett99f567e2015-08-05 12:58:26 -0700664 if (!stream) {
msarett99f567e2015-08-05 12:58:26 -0700665 return;
666 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400667 std::unique_ptr<SkCodec> decoder(SkCodec::NewFromStream(stream.release()));
scroggo8e6c7ad2016-09-16 08:20:38 -0700668 if (!decoder) {
669 SkDebugf("Missing codec for %s\n", path);
670 return;
671 }
672
673 const SkImageInfo info = decoder->getInfo().makeColorType(kIndex_8_SkColorType);
halcanary9d524f22016-03-29 09:03:52 -0700674
msarett99f567e2015-08-05 12:58:26 -0700675 // This should return kSuccess because kIndex8 is supported.
676 SkPMColor colorStorage[256];
677 int colorCount;
scroggo8e6c7ad2016-09-16 08:20:38 -0700678 SkCodec::Result result = decoder->startScanlineDecode(info, nullptr, colorStorage,
679 &colorCount);
680 if (SkCodec::kSuccess == result) {
681 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
682 // colorPtr and a valid colorCountPtr.
683 result = decoder->startScanlineDecode(info, nullptr, nullptr, nullptr);
684 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
685 result = decoder->startScanlineDecode(info);
686 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
687 } else if (SkCodec::kUnimplemented == result) {
688 // New method should be supported:
689 SkBitmap bm;
690 sk_sp<SkColorTable> colorTable(new SkColorTable(colorStorage, 256));
691 bm.allocPixels(info, nullptr, colorTable.get());
692 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes(), nullptr,
693 colorStorage, &colorCount);
694 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
695 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
696 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
697 } else {
698 // The test is uninteresting if kIndex8 is not supported
699 ERRORF(r, "Should not call test_invalid_parameters for non-Index8 file: %s\n", path);
msarett99f567e2015-08-05 12:58:26 -0700700 return;
701 }
702
msarett99f567e2015-08-05 12:58:26 -0700703}
704
705DEF_TEST(Codec_Params, r) {
706 test_invalid_parameters(r, "index8.png");
707 test_invalid_parameters(r, "mandrill.wbmp");
708}
scroggocf98fa92015-11-23 08:14:40 -0800709
scroggo8e6c7ad2016-09-16 08:20:38 -0700710#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
711
712#ifndef SK_PNG_DISABLE_TESTS // reading chunks does not work properly with older versions.
713 // It does not appear that anyone in Google3 is reading chunks.
714
scroggocf98fa92015-11-23 08:14:40 -0800715static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
716 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
717 if (!sk_stream->write(data, len)) {
718 png_error(png_ptr, "sk_write_fn Error!");
719 }
720}
721
scroggocf98fa92015-11-23 08:14:40 -0800722DEF_TEST(Codec_pngChunkReader, r) {
723 // Create a dummy bitmap. Use unpremul RGBA for libpng.
724 SkBitmap bm;
725 const int w = 1;
726 const int h = 1;
727 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
728 kUnpremul_SkAlphaType);
729 bm.setInfo(bmInfo);
730 bm.allocPixels();
731 bm.eraseColor(SK_ColorBLUE);
732 SkMD5::Digest goodDigest;
733 md5(bm, &goodDigest);
734
735 // Write to a png file.
736 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
737 REPORTER_ASSERT(r, png);
738 if (!png) {
739 return;
740 }
741
742 png_infop info = png_create_info_struct(png);
743 REPORTER_ASSERT(r, info);
744 if (!info) {
745 png_destroy_write_struct(&png, nullptr);
746 return;
747 }
748
749 if (setjmp(png_jmpbuf(png))) {
750 ERRORF(r, "failed writing png");
751 png_destroy_write_struct(&png, &info);
752 return;
753 }
754
755 SkDynamicMemoryWStream wStream;
756 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
757
758 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
759 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
760 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
761
762 // Create some chunks that match the Android framework's use.
763 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800764 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
765 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
766 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800767 };
768
769 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
770 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
771#if PNG_LIBPNG_VER < 10600
772 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800773 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
774 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800775#endif
776
777 png_write_info(png, info);
778
779 for (int j = 0; j < h; j++) {
780 png_bytep row = (png_bytep)(bm.getAddr(0, j));
781 png_write_rows(png, &row, 1);
782 }
783 png_write_end(png, info);
784 png_destroy_write_struct(&png, &info);
785
786 class ChunkReader : public SkPngChunkReader {
787 public:
788 ChunkReader(skiatest::Reporter* r)
789 : fReporter(r)
790 {
791 this->reset();
792 }
793
794 bool readChunk(const char tag[], const void* data, size_t length) override {
795 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
796 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
797 // Tag matches. This should have been the first time we see it.
798 REPORTER_ASSERT(fReporter, !fSeen[i]);
799 fSeen[i] = true;
800
801 // Data and length should match
802 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
803 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
804 (const char*) gUnknowns[i].data));
805 return true;
806 }
807 }
808 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
809 return true;
810 }
811
812 bool allHaveBeenSeen() {
813 bool ret = true;
814 for (auto seen : fSeen) {
815 ret &= seen;
816 }
817 return ret;
818 }
819
820 void reset() {
821 sk_bzero(fSeen, sizeof(fSeen));
822 }
823
824 private:
825 skiatest::Reporter* fReporter; // Unowned
826 bool fSeen[3];
827 };
828
829 ChunkReader chunkReader(r);
830
831 // Now read the file with SkCodec.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400832 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(wStream.detachAsData(), &chunkReader));
scroggocf98fa92015-11-23 08:14:40 -0800833 REPORTER_ASSERT(r, codec);
834 if (!codec) {
835 return;
836 }
837
838 // Now compare to the original.
839 SkBitmap decodedBm;
840 decodedBm.setInfo(codec->getInfo());
841 decodedBm.allocPixels();
842 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
843 decodedBm.rowBytes());
844 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
845
846 if (decodedBm.colorType() != bm.colorType()) {
847 SkBitmap tmp;
848 bool success = decodedBm.copyTo(&tmp, bm.colorType());
849 REPORTER_ASSERT(r, success);
850 if (!success) {
851 return;
852 }
853
854 tmp.swap(decodedBm);
855 }
856
857 compare_to_good_digest(r, goodDigest, decodedBm);
858 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
859
860 // Decoding again will read the chunks again.
861 chunkReader.reset();
862 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
863 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
864 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
865 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
866}
scroggo8e6c7ad2016-09-16 08:20:38 -0700867#endif // SK_PNG_DISABLE_TESTS
scroggocf98fa92015-11-23 08:14:40 -0800868#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800869
scroggodb30be22015-12-08 18:54:13 -0800870// Stream that can only peek up to a limit
871class LimitedPeekingMemStream : public SkStream {
872public:
reed42943c82016-09-12 12:01:44 -0700873 LimitedPeekingMemStream(sk_sp<SkData> data, size_t limit)
874 : fStream(std::move(data))
scroggodb30be22015-12-08 18:54:13 -0800875 , fLimit(limit) {}
876
877 size_t peek(void* buf, size_t bytes) const override {
878 return fStream.peek(buf, SkTMin(bytes, fLimit));
879 }
880 size_t read(void* buf, size_t bytes) override {
881 return fStream.read(buf, bytes);
882 }
883 bool rewind() override {
884 return fStream.rewind();
885 }
886 bool isAtEnd() const override {
msarettff2a6c82016-09-07 11:23:28 -0700887 return fStream.isAtEnd();
scroggodb30be22015-12-08 18:54:13 -0800888 }
889private:
890 SkMemoryStream fStream;
891 const size_t fLimit;
892};
893
yujieqin9c7a8a42016-02-05 08:21:19 -0800894// Stream that is not an asset stream (!hasPosition() or !hasLength())
895class NotAssetMemStream : public SkStream {
896public:
bungeman38d909e2016-08-02 14:40:46 -0700897 NotAssetMemStream(sk_sp<SkData> data) : fStream(std::move(data)) {}
yujieqin9c7a8a42016-02-05 08:21:19 -0800898
899 bool hasPosition() const override {
900 return false;
901 }
902
903 bool hasLength() const override {
904 return false;
905 }
906
907 size_t peek(void* buf, size_t bytes) const override {
908 return fStream.peek(buf, bytes);
909 }
910 size_t read(void* buf, size_t bytes) override {
911 return fStream.read(buf, bytes);
912 }
913 bool rewind() override {
914 return fStream.rewind();
915 }
916 bool isAtEnd() const override {
917 return fStream.isAtEnd();
918 }
919private:
920 SkMemoryStream fStream;
921};
922
yujieqinf236ee42016-02-29 07:14:42 -0800923// Disable RAW tests for Win32.
924#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin9c7a8a42016-02-05 08:21:19 -0800925// Test that the RawCodec works also for not asset stream. This will test the code path using
926// SkRawBufferedStream instead of SkRawAssetStream.
yujieqin9c7a8a42016-02-05 08:21:19 -0800927DEF_TEST(Codec_raw_notseekable, r) {
928 const char* path = "dng_with_preview.dng";
929 SkString fullPath(GetResourcePath(path));
bungeman38d909e2016-08-02 14:40:46 -0700930 sk_sp<SkData> data(SkData::MakeFromFileName(fullPath.c_str()));
yujieqin9c7a8a42016-02-05 08:21:19 -0800931 if (!data) {
932 SkDebugf("Missing resource '%s'\n", path);
933 return;
934 }
935
Ben Wagner145dbcd2016-11-03 14:40:50 -0400936 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(std::move(data))));
yujieqin9c7a8a42016-02-05 08:21:19 -0800937 REPORTER_ASSERT(r, codec);
938
939 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
940}
941#endif
942
scroggodb30be22015-12-08 18:54:13 -0800943// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
944// + rewind() and succeed.
945DEF_TEST(Codec_webp_peek, r) {
946 const char* path = "baby_tux.webp";
947 SkString fullPath(GetResourcePath(path));
reedfde05112016-03-11 13:02:28 -0800948 auto data = SkData::MakeFromFileName(fullPath.c_str());
scroggodb30be22015-12-08 18:54:13 -0800949 if (!data) {
950 SkDebugf("Missing resource '%s'\n", path);
951 return;
952 }
953
954 // The limit is less than webp needs to peek or read.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400955 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(
reed42943c82016-09-12 12:01:44 -0700956 new LimitedPeekingMemStream(data, 25)));
scroggodb30be22015-12-08 18:54:13 -0800957 REPORTER_ASSERT(r, codec);
958
scroggo7b5e5532016-02-04 06:14:24 -0800959 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800960
961 // Similarly, a stream which does not peek should still succeed.
reed42943c82016-09-12 12:01:44 -0700962 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0)));
scroggodb30be22015-12-08 18:54:13 -0800963 REPORTER_ASSERT(r, codec);
964
scroggo7b5e5532016-02-04 06:14:24 -0800965 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800966}
967
msarett7f7ec202016-03-01 12:12:27 -0800968// SkCodec's wbmp decoder was initially unnecessarily restrictive.
969// It required the second byte to be zero. The wbmp specification allows
970// a couple of bits to be 1 (so long as they do not overlap with 0x9F).
971// Test that SkCodec now supports an image with these bits set.
Leon Scroggins III83926342016-12-06 10:58:02 -0500972DEF_TEST(Codec_wbmp_restrictive, r) {
scroggob9a1e342015-11-30 06:25:31 -0800973 const char* path = "mandrill.wbmp";
Ben Wagner145dbcd2016-11-03 14:40:50 -0400974 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
scroggob9a1e342015-11-30 06:25:31 -0800975 if (!stream) {
scroggob9a1e342015-11-30 06:25:31 -0800976 return;
977 }
978
979 // Modify the stream to contain a second byte with some bits set.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400980 auto data = SkCopyStreamToData(stream.get());
scroggob9a1e342015-11-30 06:25:31 -0800981 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
982 writeableData[1] = static_cast<uint8_t>(~0x9F);
983
msarett7f7ec202016-03-01 12:12:27 -0800984 // SkCodec should support this.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400985 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
scroggob9a1e342015-11-30 06:25:31 -0800986 REPORTER_ASSERT(r, codec);
987 if (!codec) {
988 return;
989 }
scroggo7b5e5532016-02-04 06:14:24 -0800990 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800991}
scroggodb30be22015-12-08 18:54:13 -0800992
993// wbmp images have a header that can be arbitrarily large, depending on the
994// size of the image. We cap the size at 65535, meaning we only need to look at
995// 8 bytes to determine whether we can read the image. This is important
996// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
997// image is a wbmp.
998DEF_TEST(Codec_wbmp_max_size, r) {
999 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
1000 0x83, 0xFF, 0x7F, // W: 65535
1001 0x83, 0xFF, 0x7F }; // H: 65535
Ben Wagner145dbcd2016-11-03 14:40:50 -04001002 std::unique_ptr<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
1003 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -08001004
1005 REPORTER_ASSERT(r, codec);
1006 if (!codec) return;
1007
1008 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
1009 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
1010
1011 // Now test an image which is too big. Any image with a larger header (i.e.
1012 // has bigger width/height) is also too big.
1013 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
1014 0x84, 0x80, 0x00, // W: 65536
1015 0x84, 0x80, 0x00 }; // H: 65536
1016 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
mtklein18300a32016-03-16 13:53:35 -07001017 codec.reset(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -08001018
1019 REPORTER_ASSERT(r, !codec);
1020}
msarett2812f032016-07-18 15:56:08 -07001021
1022DEF_TEST(Codec_jpeg_rewind, r) {
1023 const char* path = "mandrill_512_q075.jpg";
Ben Wagner145dbcd2016-11-03 14:40:50 -04001024 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett2812f032016-07-18 15:56:08 -07001025 if (!stream) {
msarett2812f032016-07-18 15:56:08 -07001026 return;
1027 }
Ben Wagner145dbcd2016-11-03 14:40:50 -04001028 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarett2812f032016-07-18 15:56:08 -07001029 if (!codec) {
1030 ERRORF(r, "Unable to create codec '%s'.", path);
1031 return;
1032 }
1033
1034 const int width = codec->getInfo().width();
1035 const int height = codec->getInfo().height();
1036 size_t rowBytes = sizeof(SkPMColor) * width;
1037 SkAutoMalloc pixelStorage(height * rowBytes);
1038
1039 // Perform a sampled decode.
1040 SkAndroidCodec::AndroidOptions opts;
1041 opts.fSampleSize = 12;
1042 codec->getAndroidPixels(codec->getInfo().makeWH(width / 12, height / 12), pixelStorage.get(),
1043 rowBytes, &opts);
1044
1045 // Rewind the codec and perform a full image decode.
1046 SkCodec::Result result = codec->getPixels(codec->getInfo(), pixelStorage.get(), rowBytes);
1047 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1048}
msarett549ca322016-08-17 08:54:08 -07001049
msarett35bb74b2016-08-22 07:41:28 -07001050static void check_color_xform(skiatest::Reporter* r, const char* path) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001051 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(GetResourceAsStream(path)));
msarett35bb74b2016-08-22 07:41:28 -07001052
1053 SkAndroidCodec::AndroidOptions opts;
1054 opts.fSampleSize = 3;
1055 const int subsetWidth = codec->getInfo().width() / 2;
1056 const int subsetHeight = codec->getInfo().height() / 2;
1057 SkIRect subset = SkIRect::MakeWH(subsetWidth, subsetHeight);
1058 opts.fSubset = &subset;
1059
1060 const int dstWidth = subsetWidth / opts.fSampleSize;
1061 const int dstHeight = subsetHeight / opts.fSampleSize;
1062 sk_sp<SkData> data = SkData::MakeFromFileName(
1063 GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str());
Brian Osman526972e2016-10-24 09:24:02 -04001064 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeICC(data->data(), data->size());
msarett35bb74b2016-08-22 07:41:28 -07001065 SkImageInfo dstInfo = codec->getInfo().makeWH(dstWidth, dstHeight)
1066 .makeColorType(kN32_SkColorType)
1067 .makeColorSpace(colorSpace);
1068
1069 size_t rowBytes = dstInfo.minRowBytes();
1070 SkAutoMalloc pixelStorage(dstInfo.getSafeSize(rowBytes));
1071 SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get(), rowBytes, &opts);
1072 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1073}
1074
1075DEF_TEST(Codec_ColorXform, r) {
1076 check_color_xform(r, "mandrill_512_q075.jpg");
1077 check_color_xform(r, "mandrill_512.png");
1078}
1079
msarettf17b71f2016-09-12 14:30:03 -07001080static bool color_type_match(SkColorType origColorType, SkColorType codecColorType) {
1081 switch (origColorType) {
1082 case kRGBA_8888_SkColorType:
1083 case kBGRA_8888_SkColorType:
1084 return kRGBA_8888_SkColorType == codecColorType ||
1085 kBGRA_8888_SkColorType == codecColorType;
1086 default:
1087 return origColorType == codecColorType;
1088 }
1089}
1090
1091static bool alpha_type_match(SkAlphaType origAlphaType, SkAlphaType codecAlphaType) {
1092 switch (origAlphaType) {
1093 case kUnpremul_SkAlphaType:
1094 case kPremul_SkAlphaType:
1095 return kUnpremul_SkAlphaType == codecAlphaType ||
1096 kPremul_SkAlphaType == codecAlphaType;
1097 default:
1098 return origAlphaType == codecAlphaType;
1099 }
1100}
1101
1102static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const SkImageInfo& info) {
1103 SkBitmap bm1;
1104 SkPMColor colors[256];
Hal Canary342b7ac2016-11-04 11:49:42 -04001105 sk_sp<SkColorTable> colorTable1(new SkColorTable(colors, 256));
msarettf17b71f2016-09-12 14:30:03 -07001106 bm1.allocPixels(info, nullptr, colorTable1.get());
1107 int numColors;
1108 SkCodec::Result result = origCodec->getPixels(info, bm1.getPixels(), bm1.rowBytes(), nullptr,
1109 const_cast<SkPMColor*>(colorTable1->readColors()),
1110 &numColors);
1111 // This will fail to update colorTable1->count() but is fine for the purpose of this test.
1112 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarett9b09cd82016-08-29 14:47:49 -07001113
1114 // Encode the image to png.
1115 sk_sp<SkData> data =
Hal Canarydb683012016-11-23 08:55:18 -07001116 sk_sp<SkData>(sk_tool_utils::EncodeImageToData(bm1, SkEncodedImageFormat::kPNG, 100));
msarett9b09cd82016-08-29 14:47:49 -07001117
Ben Wagner145dbcd2016-11-03 14:40:50 -04001118 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
msarettf17b71f2016-09-12 14:30:03 -07001119 REPORTER_ASSERT(r, color_type_match(info.colorType(), codec->getInfo().colorType()));
1120 REPORTER_ASSERT(r, alpha_type_match(info.alphaType(), codec->getInfo().alphaType()));
msarett9b09cd82016-08-29 14:47:49 -07001121
1122 SkBitmap bm2;
Hal Canary342b7ac2016-11-04 11:49:42 -04001123 sk_sp<SkColorTable> colorTable2(new SkColorTable(colors, 256));
msarettf17b71f2016-09-12 14:30:03 -07001124 bm2.allocPixels(info, nullptr, colorTable2.get());
1125 result = codec->getPixels(info, bm2.getPixels(), bm2.rowBytes(), nullptr,
1126 const_cast<SkPMColor*>(colorTable2->readColors()), &numColors);
msarett9b09cd82016-08-29 14:47:49 -07001127 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1128
1129 SkMD5::Digest d1, d2;
1130 md5(bm1, &d1);
1131 md5(bm2, &d2);
1132 REPORTER_ASSERT(r, d1 == d2);
1133}
1134
1135DEF_TEST(Codec_PngRoundTrip, r) {
msarett549ca322016-08-17 08:54:08 -07001136 const char* path = "mandrill_512_q075.jpg";
Ben Wagner145dbcd2016-11-03 14:40:50 -04001137 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
1138 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett549ca322016-08-17 08:54:08 -07001139
msarettf17b71f2016-09-12 14:30:03 -07001140 SkColorType colorTypesOpaque[] = {
1141 kRGB_565_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1142 };
1143 for (SkColorType colorType : colorTypesOpaque) {
1144 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType);
1145 check_round_trip(r, codec.get(), newInfo);
1146 }
1147
msarett9b09cd82016-08-29 14:47:49 -07001148 path = "grayscale.jpg";
bungemanf93d7112016-09-16 06:24:20 -07001149 stream.reset(GetResourceAsStream(path));
msarett9b09cd82016-08-29 14:47:49 -07001150 codec.reset(SkCodec::NewFromStream(stream.release()));
msarettf17b71f2016-09-12 14:30:03 -07001151 check_round_trip(r, codec.get(), codec->getInfo());
1152
1153 path = "yellow_rose.png";
bungemanf93d7112016-09-16 06:24:20 -07001154 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001155 codec.reset(SkCodec::NewFromStream(stream.release()));
1156
1157 SkColorType colorTypesWithAlpha[] = {
1158 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1159 };
1160 SkAlphaType alphaTypes[] = {
1161 kUnpremul_SkAlphaType, kPremul_SkAlphaType
1162 };
1163 for (SkColorType colorType : colorTypesWithAlpha) {
1164 for (SkAlphaType alphaType : alphaTypes) {
1165 // Set color space to nullptr because color correct premultiplies do not round trip.
1166 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType)
1167 .makeAlphaType(alphaType)
1168 .makeColorSpace(nullptr);
1169 check_round_trip(r, codec.get(), newInfo);
1170 }
1171 }
1172
1173 path = "index8.png";
bungemanf93d7112016-09-16 06:24:20 -07001174 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001175 codec.reset(SkCodec::NewFromStream(stream.release()));
1176
1177 for (SkAlphaType alphaType : alphaTypes) {
1178 SkImageInfo newInfo = codec->getInfo().makeAlphaType(alphaType)
1179 .makeColorSpace(nullptr);
1180 check_round_trip(r, codec.get(), newInfo);
1181 }
msarett549ca322016-08-17 08:54:08 -07001182}
msarett2ecc35f2016-09-08 11:55:16 -07001183
1184static void test_conversion_possible(skiatest::Reporter* r, const char* path,
scroggo8e6c7ad2016-09-16 08:20:38 -07001185 bool supportsScanlineDecoder,
1186 bool supportsIncrementalDecoder) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001187 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
1188 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett2ecc35f2016-09-08 11:55:16 -07001189 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType);
1190
1191 SkBitmap bm;
1192 bm.allocPixels(infoF16);
1193 SkCodec::Result result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1194 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001195
1196 result = codec->startScanlineDecode(infoF16);
1197 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001198 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001199 } else {
1200 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1201 }
1202
1203 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1204 if (supportsIncrementalDecoder) {
1205 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
1206 } else {
1207 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001208 }
1209
raftias94888332016-10-18 10:02:51 -07001210 SkASSERT(SkColorSpace_Base::Type::kXYZ == as_CSB(infoF16.colorSpace())->type());
1211 SkColorSpace_XYZ* csXYZ = static_cast<SkColorSpace_XYZ*>(infoF16.colorSpace());
1212 infoF16 = infoF16.makeColorSpace(csXYZ->makeLinearGamma());
msarett2ecc35f2016-09-08 11:55:16 -07001213 result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1214 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001215 result = codec->startScanlineDecode(infoF16);
1216 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001217 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001218 } else {
1219 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1220 }
1221
1222 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1223 if (supportsIncrementalDecoder) {
1224 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1225 } else {
1226 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001227 }
1228}
1229
1230DEF_TEST(Codec_F16ConversionPossible, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -07001231 test_conversion_possible(r, "color_wheel.webp", false, false);
1232 test_conversion_possible(r, "mandrill_512_q075.jpg", true, false);
1233 test_conversion_possible(r, "yellow_rose.png", false, true);
1234}
1235
scroggo19b91532016-10-24 09:03:26 -07001236static void decode_frame(skiatest::Reporter* r, SkCodec* codec, size_t frame) {
1237 SkBitmap bm;
1238 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1239 bm.allocPixels(info);
1240
1241 SkCodec::Options opts;
1242 opts.fFrameIndex = frame;
1243 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->getPixels(info,
1244 bm.getPixels(), bm.rowBytes(), &opts, nullptr, nullptr));
1245}
1246
1247// For an animated image, we should only read enough to decode the requested
1248// frame if the client never calls getFrameInfo.
1249DEF_TEST(Codec_skipFullParse, r) {
1250 auto path = "test640x479.gif";
1251 SkStream* stream(GetResourceAsStream(path));
1252 if (!stream) {
1253 return;
1254 }
1255
1256 // Note that we cheat and hold on to the stream pointer, but SkCodec will
1257 // take ownership. We will not refer to the stream after the SkCodec
1258 // deletes it.
1259 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
1260 if (!codec) {
1261 ERRORF(r, "Failed to create codec for %s", path);
1262 return;
1263 }
1264
1265 REPORTER_ASSERT(r, stream->hasPosition());
1266 const size_t sizePosition = stream->getPosition();
1267 REPORTER_ASSERT(r, stream->hasLength() && sizePosition < stream->getLength());
1268
1269 // This should read more of the stream, but not the whole stream.
1270 decode_frame(r, codec.get(), 0);
1271 const size_t positionAfterFirstFrame = stream->getPosition();
1272 REPORTER_ASSERT(r, positionAfterFirstFrame > sizePosition
1273 && positionAfterFirstFrame < stream->getLength());
1274
1275 // Again, this should read more of the stream.
1276 decode_frame(r, codec.get(), 2);
1277 const size_t positionAfterThirdFrame = stream->getPosition();
1278 REPORTER_ASSERT(r, positionAfterThirdFrame > positionAfterFirstFrame
1279 && positionAfterThirdFrame < stream->getLength());
1280
1281 // This does not need to read any more of the stream, since it has already
1282 // parsed the second frame.
1283 decode_frame(r, codec.get(), 1);
1284 REPORTER_ASSERT(r, stream->getPosition() == positionAfterThirdFrame);
1285
1286 // This should read the rest of the frames.
1287 decode_frame(r, codec.get(), 3);
1288 const size_t finalPosition = stream->getPosition();
1289 REPORTER_ASSERT(r, finalPosition > positionAfterThirdFrame);
1290
1291 // There may be more data in the stream.
1292 auto frameInfo = codec->getFrameInfo();
1293 REPORTER_ASSERT(r, frameInfo.size() == 4);
1294 REPORTER_ASSERT(r, stream->getPosition() >= finalPosition);
1295}
1296
scroggo8e6c7ad2016-09-16 08:20:38 -07001297// Only rewinds up to a limit.
1298class LimitedRewindingStream : public SkStream {
1299public:
1300 static SkStream* Make(const char path[], size_t limit) {
1301 SkStream* stream = GetResourceAsStream(path);
1302 if (!stream) {
1303 return nullptr;
1304 }
1305 return new LimitedRewindingStream(stream, limit);
1306 }
1307
1308 size_t read(void* buffer, size_t size) override {
1309 const size_t bytes = fStream->read(buffer, size);
1310 fPosition += bytes;
1311 return bytes;
1312 }
1313
1314 bool isAtEnd() const override {
1315 return fStream->isAtEnd();
1316 }
1317
1318 bool rewind() override {
1319 if (fPosition <= fLimit && fStream->rewind()) {
1320 fPosition = 0;
1321 return true;
1322 }
1323
1324 return false;
1325 }
1326
1327private:
Ben Wagner145dbcd2016-11-03 14:40:50 -04001328 std::unique_ptr<SkStream> fStream;
1329 const size_t fLimit;
1330 size_t fPosition;
scroggo8e6c7ad2016-09-16 08:20:38 -07001331
1332 LimitedRewindingStream(SkStream* stream, size_t limit)
1333 : fStream(stream)
1334 , fLimit(limit)
1335 , fPosition(0)
1336 {
1337 SkASSERT(fStream);
1338 }
1339};
1340
1341DEF_TEST(Codec_fallBack, r) {
1342 // SkAndroidCodec needs to be able to fall back to scanline decoding
1343 // if incremental decoding does not work. Make sure this does not
1344 // require a rewind.
1345
1346 // Formats that currently do not support incremental decoding
1347 auto files = {
scroggo8e6c7ad2016-09-16 08:20:38 -07001348 "CMYK.jpg",
1349 "color_wheel.ico",
1350 "mandrill.wbmp",
1351 "randPixels.bmp",
1352 };
1353 for (auto file : files) {
1354 SkStream* stream = LimitedRewindingStream::Make(file, 14);
1355 if (!stream) {
1356 SkDebugf("Missing resources (%s). Set --resourcePath.\n", file);
1357 return;
1358 }
1359
Ben Wagner145dbcd2016-11-03 14:40:50 -04001360 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
scroggo8e6c7ad2016-09-16 08:20:38 -07001361 if (!codec) {
1362 ERRORF(r, "Failed to create codec for %s,", file);
1363 continue;
1364 }
1365
1366 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
1367 SkBitmap bm;
1368 bm.allocPixels(info);
1369
1370 if (SkCodec::kUnimplemented != codec->startIncrementalDecode(info, bm.getPixels(),
1371 bm.rowBytes())) {
1372 ERRORF(r, "Is scanline decoding now implemented for %s?", file);
1373 continue;
1374 }
1375
1376 // Scanline decoding should not require a rewind.
1377 SkCodec::Result result = codec->startScanlineDecode(info);
1378 if (SkCodec::kSuccess != result) {
1379 ERRORF(r, "Scanline decoding failed for %s with %i", file, result);
1380 }
1381 }
msarett2ecc35f2016-09-08 11:55:16 -07001382}
scroggoc46cdd42016-10-10 06:45:32 -07001383
1384// This test verifies that we fixed an assert statement that fired when reusing a png codec
1385// after scaling.
1386DEF_TEST(Codec_reusePng, r) {
1387 std::unique_ptr<SkStream> stream(GetResourceAsStream("plane.png"));
1388 if (!stream) {
1389 return;
1390 }
1391
1392 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
1393 if (!codec) {
1394 ERRORF(r, "Failed to create codec\n");
1395 return;
1396 }
1397
1398 SkAndroidCodec::AndroidOptions opts;
1399 opts.fSampleSize = 5;
1400 auto size = codec->getSampledDimensions(opts.fSampleSize);
1401 auto info = codec->getInfo().makeWH(size.fWidth, size.fHeight).makeColorType(kN32_SkColorType);
1402 SkBitmap bm;
1403 bm.allocPixels(info);
1404 auto result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1405 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1406
1407 info = codec->getInfo().makeColorType(kN32_SkColorType);
1408 bm.allocPixels(info);
1409 opts.fSampleSize = 1;
1410 result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1411 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1412}
scroggoe61b3b42016-10-10 07:17:32 -07001413
1414DEF_TEST(Codec_rowsDecoded, r) {
1415 auto file = "plane_interlaced.png";
1416 std::unique_ptr<SkStream> stream(GetResourceAsStream(file));
1417 if (!stream) {
1418 return;
1419 }
1420
1421 // This is enough to read the header etc, but no rows.
1422 auto data = SkData::MakeFromStream(stream.get(), 99);
1423 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
1424 if (!codec) {
1425 ERRORF(r, "Failed to create codec\n");
1426 return;
1427 }
1428
1429 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1430 SkBitmap bm;
1431 bm.allocPixels(info);
1432 auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
1433 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1434
1435 // This is an arbitrary value. The important fact is that it is not zero, and rowsDecoded
1436 // should get set to zero by incrementalDecode.
1437 int rowsDecoded = 77;
1438 result = codec->incrementalDecode(&rowsDecoded);
1439 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput);
1440 REPORTER_ASSERT(r, rowsDecoded == 0);
1441}
Matt Sarett29121eb2016-10-17 14:32:46 -04001442
Matt Sarett8a4e9c52016-10-25 14:24:50 -04001443static void test_invalid_images(skiatest::Reporter* r, const char* path, bool shouldSucceed) {
Matt Sarett29121eb2016-10-17 14:32:46 -04001444 SkBitmap bitmap;
Matt Sarett8a4e9c52016-10-25 14:24:50 -04001445 const bool success = GetResourceAsBitmap(path, &bitmap);
1446 REPORTER_ASSERT(r, success == shouldSucceed);
1447}
1448
1449DEF_TEST(Codec_InvalidImages, r) {
1450 // ASAN will complain if there is an issue.
1451 test_invalid_images(r, "invalid_images/int_overflow.ico", false);
1452 test_invalid_images(r, "invalid_images/skbug5887.gif", true);
Matt Sarettdbdf6d22016-11-08 15:26:56 -05001453 test_invalid_images(r, "invalid_images/many-progressive-scans.jpg", false);
Matt Sarett29121eb2016-10-17 14:32:46 -04001454}
Leon Scroggins III56e32092016-12-12 17:10:46 -05001455
1456DEF_TEST(Codec_InvalidAnimated, r) {
1457 // ASAN will complain if there is an issue.
1458 auto path = "invalid_images/skbug6046.gif";
1459 auto* stream = GetResourceAsStream(path);
1460 if (!stream) {
1461 return;
1462 }
1463
1464 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
1465 REPORTER_ASSERT(r, codec);
1466 if (!codec) {
1467 return;
1468 }
1469
1470 const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1471 SkBitmap bm;
1472 bm.allocPixels(info);
1473
1474 auto frameInfos = codec->getFrameInfo();
1475 SkCodec::Options opts;
1476 for (size_t i = 0; i < frameInfos.size(); i++) {
1477 opts.fFrameIndex = i;
1478 opts.fHasPriorFrame = frameInfos[i].fRequiredFrame == i - 1;
1479 auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes(), &opts);
1480 if (result != SkCodec::kSuccess) {
1481 ERRORF(r, "Failed to start decoding frame %i (out of %i) with error %i\n", i,
1482 frameInfos.size(), result);
1483 continue;
1484 }
1485
1486 codec->incrementalDecode();
1487 }
1488}