blob: d15c7103250801107ee747213630e79d3e70f472 [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
458DEF_TEST(Codec, r) {
459 // WBMP
scroggo8e6c7ad2016-09-16 08:20:38 -0700460 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false, true);
halcanarya096d7a2015-03-27 12:16:53 -0700461
scroggo6f5e6192015-06-18 12:53:43 -0700462 // WEBP
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);
scroggo6f5e6192015-06-18 12:53:43 -0700466
halcanarya096d7a2015-03-27 12:16:53 -0700467 // BMP
scroggo8e6c7ad2016-09-16 08:20:38 -0700468 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false, true);
469 check(r, "rle.bmp", SkISize::Make(320, 240), true, false, true);
halcanarya096d7a2015-03-27 12:16:53 -0700470
471 // ICO
msarette6dd0042015-10-09 11:07:34 -0700472 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700473 // These two tests examine interestingly different behavior:
474 // Decodes an embedded BMP image
msarettbe8216a2015-12-04 08:00:50 -0800475 check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700476 // Decodes an embedded PNG image
scroggo8e6c7ad2016-09-16 08:20:38 -0700477 check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false, false, true);
halcanarya096d7a2015-03-27 12:16:53 -0700478
msarett438b2ad2015-04-09 12:43:10 -0700479 // GIF
scroggo19b91532016-10-24 09:03:26 -0700480 check(r, "box.gif", SkISize::Make(200, 55), false, false, true, true);
481 check(r, "color_wheel.gif", SkISize::Make(128, 128), false, false, true, true);
msarette6dd0042015-10-09 11:07:34 -0700482 // randPixels.gif is too small to test incomplete
scroggo19b91532016-10-24 09:03:26 -0700483 check(r, "randPixels.gif", SkISize::Make(8, 8), false, false, false, true);
msarett438b2ad2015-04-09 12:43:10 -0700484
msarette16b04a2015-04-15 07:32:19 -0700485 // JPG
scroggo8e6c7ad2016-09-16 08:20:38 -0700486 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false, true);
487 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700488 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700489 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggo8e6c7ad2016-09-16 08:20:38 -0700490 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700491 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700492 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
msarette16b04a2015-04-15 07:32:19 -0700493
halcanarya096d7a2015-03-27 12:16:53 -0700494 // PNG
scroggo8e6c7ad2016-09-16 08:20:38 -0700495 check(r, "arrow.png", SkISize::Make(187, 312), false, false, true, true);
496 check(r, "baby_tux.png", SkISize::Make(240, 246), false, false, true, true);
497 check(r, "color_wheel.png", SkISize::Make(128, 128), false, false, true, true);
498 // half-transparent-white-pixel.png is too small to test incomplete
499 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), false, false, false, true);
500 check(r, "mandrill_128.png", SkISize::Make(128, 128), false, false, true, true);
501 check(r, "mandrill_16.png", SkISize::Make(16, 16), false, false, true, true);
502 check(r, "mandrill_256.png", SkISize::Make(256, 256), false, false, true, true);
503 check(r, "mandrill_32.png", SkISize::Make(32, 32), false, false, true, true);
504 check(r, "mandrill_512.png", SkISize::Make(512, 512), false, false, true, true);
505 check(r, "mandrill_64.png", SkISize::Make(64, 64), false, false, true, true);
506 check(r, "plane.png", SkISize::Make(250, 126), false, false, true, true);
507 check(r, "plane_interlaced.png", SkISize::Make(250, 126), false, false, true, true);
508 check(r, "randPixels.png", SkISize::Make(8, 8), false, false, true, true);
509 check(r, "yellow_rose.png", SkISize::Make(400, 301), false, false, true, true);
yujieqin916de9f2016-01-25 08:26:16 -0800510
511 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800512// Disable RAW tests for Win32.
513#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800514 check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
ebrauer46d2aa82016-02-17 08:04:00 -0800515 check(r, "sample_1mp_rotated.dng", SkISize::Make(600, 338), false, false, false);
yujieqin9c7a8a42016-02-05 08:21:19 -0800516 check(r, "dng_with_preview.dng", SkISize::Make(600, 338), true, false, false);
msarett02cb4d42016-01-25 11:01:34 -0800517#endif
halcanarya096d7a2015-03-27 12:16:53 -0700518}
scroggo0a7e69c2015-04-03 07:22:22 -0700519
520static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700521 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700522 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700523 REPORTER_ASSERT(r, !codec);
524
msarett3d9d7a72015-10-21 10:27:10 -0700525 SkAndroidCodec* androidCodec =
526 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
527 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700528}
529
530// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
531// even on failure. Test some bad streams.
532DEF_TEST(Codec_leaks, r) {
533 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
534 const char nonSupportedStream[] = "hello world";
535 // The other strings should look like the beginning of a file type, so we'll call some
536 // internal version of NewFromStream, which must also delete the stream on failure.
537 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
538 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
539 const char emptyWebp[] = "RIFF1234WEBPVP";
540 const char emptyBmp[] = { 'B', 'M' };
541 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
542 const char emptyGif[] = "GIFVER";
543
544 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
545 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
546 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
547 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
548 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
549 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
550 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
551}
msarette16b04a2015-04-15 07:32:19 -0700552
scroggo2c3b2182015-10-09 08:40:59 -0700553DEF_TEST(Codec_null, r) {
scroggobed1ed62016-02-11 10:24:55 -0800554 // Attempting to create an SkCodec or an SkAndroidCodec with null should not
scroggo2c3b2182015-10-09 08:40:59 -0700555 // crash.
556 SkCodec* codec = SkCodec::NewFromStream(nullptr);
557 REPORTER_ASSERT(r, !codec);
558
msarett3d9d7a72015-10-21 10:27:10 -0700559 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
560 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700561}
562
msarette16b04a2015-04-15 07:32:19 -0700563static void test_dimensions(skiatest::Reporter* r, const char path[]) {
564 // Create the codec from the resource file
Ben Wagner145dbcd2016-11-03 14:40:50 -0400565 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarette16b04a2015-04-15 07:32:19 -0700566 if (!stream) {
msarette16b04a2015-04-15 07:32:19 -0700567 return;
568 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400569 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarette16b04a2015-04-15 07:32:19 -0700570 if (!codec) {
571 ERRORF(r, "Unable to create codec '%s'", path);
572 return;
573 }
574
575 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800576 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700577 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700578 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700579 SkImageInfo scaledInfo = codec->getInfo()
580 .makeWH(scaledDims.width(), scaledDims.height())
581 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700582
583 // Set up for the decode
584 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
585 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
586 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
587
msarett3d9d7a72015-10-21 10:27:10 -0700588 SkAndroidCodec::AndroidOptions options;
589 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700590 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700591 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700592 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700593 }
594}
595
596// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
597DEF_TEST(Codec_Dimensions, r) {
598 // JPG
599 test_dimensions(r, "CMYK.jpg");
600 test_dimensions(r, "color_wheel.jpg");
601 test_dimensions(r, "grayscale.jpg");
602 test_dimensions(r, "mandrill_512_q075.jpg");
603 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700604
605 // Decoding small images with very large scaling factors is a potential
606 // source of bugs and crashes. We disable these tests in Gold because
607 // tiny images are not very useful to look at.
608 // Here we make sure that we do not crash or access illegal memory when
609 // performing scaled decodes on small images.
610 test_dimensions(r, "1x1.png");
611 test_dimensions(r, "2x2.png");
612 test_dimensions(r, "3x3.png");
613 test_dimensions(r, "3x1.png");
614 test_dimensions(r, "1x1.png");
615 test_dimensions(r, "16x1.png");
616 test_dimensions(r, "1x16.png");
617 test_dimensions(r, "mandrill_16.png");
618
yujieqin916de9f2016-01-25 08:26:16 -0800619 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800620// Disable RAW tests for Win32.
621#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800622 test_dimensions(r, "sample_1mp.dng");
ebrauer46d2aa82016-02-17 08:04:00 -0800623 test_dimensions(r, "sample_1mp_rotated.dng");
yujieqin9c7a8a42016-02-05 08:21:19 -0800624 test_dimensions(r, "dng_with_preview.dng");
msarett8e49ca32016-01-25 13:10:58 -0800625#endif
msarette16b04a2015-04-15 07:32:19 -0700626}
627
msarettd0375bc2015-08-12 08:08:56 -0700628static void test_invalid(skiatest::Reporter* r, const char path[]) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400629 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett4b17fa32015-04-23 08:53:39 -0700630 if (!stream) {
msarett4b17fa32015-04-23 08:53:39 -0700631 return;
632 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400633 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
halcanary96fcdcc2015-08-27 07:41:13 -0700634 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700635}
msarette16b04a2015-04-15 07:32:19 -0700636
msarett4b17fa32015-04-23 08:53:39 -0700637DEF_TEST(Codec_Empty, r) {
638 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700639 test_invalid(r, "empty_images/zero-dims.gif");
640 test_invalid(r, "empty_images/zero-embedded.ico");
641 test_invalid(r, "empty_images/zero-width.bmp");
642 test_invalid(r, "empty_images/zero-height.bmp");
643 test_invalid(r, "empty_images/zero-width.jpg");
644 test_invalid(r, "empty_images/zero-height.jpg");
645 test_invalid(r, "empty_images/zero-width.png");
646 test_invalid(r, "empty_images/zero-height.png");
647 test_invalid(r, "empty_images/zero-width.wbmp");
648 test_invalid(r, "empty_images/zero-height.wbmp");
649 // This image is an ico with an embedded mask-bmp. This is illegal.
650 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700651}
msarett99f567e2015-08-05 12:58:26 -0700652
653static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400654 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett99f567e2015-08-05 12:58:26 -0700655 if (!stream) {
msarett99f567e2015-08-05 12:58:26 -0700656 return;
657 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400658 std::unique_ptr<SkCodec> decoder(SkCodec::NewFromStream(stream.release()));
scroggo8e6c7ad2016-09-16 08:20:38 -0700659 if (!decoder) {
660 SkDebugf("Missing codec for %s\n", path);
661 return;
662 }
663
664 const SkImageInfo info = decoder->getInfo().makeColorType(kIndex_8_SkColorType);
halcanary9d524f22016-03-29 09:03:52 -0700665
msarett99f567e2015-08-05 12:58:26 -0700666 // This should return kSuccess because kIndex8 is supported.
667 SkPMColor colorStorage[256];
668 int colorCount;
scroggo8e6c7ad2016-09-16 08:20:38 -0700669 SkCodec::Result result = decoder->startScanlineDecode(info, nullptr, colorStorage,
670 &colorCount);
671 if (SkCodec::kSuccess == result) {
672 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
673 // colorPtr and a valid colorCountPtr.
674 result = decoder->startScanlineDecode(info, nullptr, nullptr, nullptr);
675 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
676 result = decoder->startScanlineDecode(info);
677 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
678 } else if (SkCodec::kUnimplemented == result) {
679 // New method should be supported:
680 SkBitmap bm;
681 sk_sp<SkColorTable> colorTable(new SkColorTable(colorStorage, 256));
682 bm.allocPixels(info, nullptr, colorTable.get());
683 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes(), nullptr,
684 colorStorage, &colorCount);
685 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
686 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
687 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
688 } else {
689 // The test is uninteresting if kIndex8 is not supported
690 ERRORF(r, "Should not call test_invalid_parameters for non-Index8 file: %s\n", path);
msarett99f567e2015-08-05 12:58:26 -0700691 return;
692 }
693
msarett99f567e2015-08-05 12:58:26 -0700694}
695
696DEF_TEST(Codec_Params, r) {
697 test_invalid_parameters(r, "index8.png");
698 test_invalid_parameters(r, "mandrill.wbmp");
699}
scroggocf98fa92015-11-23 08:14:40 -0800700
scroggo8e6c7ad2016-09-16 08:20:38 -0700701#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
702
703#ifndef SK_PNG_DISABLE_TESTS // reading chunks does not work properly with older versions.
704 // It does not appear that anyone in Google3 is reading chunks.
705
scroggocf98fa92015-11-23 08:14:40 -0800706static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
707 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
708 if (!sk_stream->write(data, len)) {
709 png_error(png_ptr, "sk_write_fn Error!");
710 }
711}
712
scroggocf98fa92015-11-23 08:14:40 -0800713DEF_TEST(Codec_pngChunkReader, r) {
714 // Create a dummy bitmap. Use unpremul RGBA for libpng.
715 SkBitmap bm;
716 const int w = 1;
717 const int h = 1;
718 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
719 kUnpremul_SkAlphaType);
720 bm.setInfo(bmInfo);
721 bm.allocPixels();
722 bm.eraseColor(SK_ColorBLUE);
723 SkMD5::Digest goodDigest;
724 md5(bm, &goodDigest);
725
726 // Write to a png file.
727 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
728 REPORTER_ASSERT(r, png);
729 if (!png) {
730 return;
731 }
732
733 png_infop info = png_create_info_struct(png);
734 REPORTER_ASSERT(r, info);
735 if (!info) {
736 png_destroy_write_struct(&png, nullptr);
737 return;
738 }
739
740 if (setjmp(png_jmpbuf(png))) {
741 ERRORF(r, "failed writing png");
742 png_destroy_write_struct(&png, &info);
743 return;
744 }
745
746 SkDynamicMemoryWStream wStream;
747 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
748
749 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
750 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
751 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
752
753 // Create some chunks that match the Android framework's use.
754 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800755 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
756 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
757 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800758 };
759
760 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
761 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
762#if PNG_LIBPNG_VER < 10600
763 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800764 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
765 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800766#endif
767
768 png_write_info(png, info);
769
770 for (int j = 0; j < h; j++) {
771 png_bytep row = (png_bytep)(bm.getAddr(0, j));
772 png_write_rows(png, &row, 1);
773 }
774 png_write_end(png, info);
775 png_destroy_write_struct(&png, &info);
776
777 class ChunkReader : public SkPngChunkReader {
778 public:
779 ChunkReader(skiatest::Reporter* r)
780 : fReporter(r)
781 {
782 this->reset();
783 }
784
785 bool readChunk(const char tag[], const void* data, size_t length) override {
786 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
787 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
788 // Tag matches. This should have been the first time we see it.
789 REPORTER_ASSERT(fReporter, !fSeen[i]);
790 fSeen[i] = true;
791
792 // Data and length should match
793 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
794 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
795 (const char*) gUnknowns[i].data));
796 return true;
797 }
798 }
799 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
800 return true;
801 }
802
803 bool allHaveBeenSeen() {
804 bool ret = true;
805 for (auto seen : fSeen) {
806 ret &= seen;
807 }
808 return ret;
809 }
810
811 void reset() {
812 sk_bzero(fSeen, sizeof(fSeen));
813 }
814
815 private:
816 skiatest::Reporter* fReporter; // Unowned
817 bool fSeen[3];
818 };
819
820 ChunkReader chunkReader(r);
821
822 // Now read the file with SkCodec.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400823 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(wStream.detachAsData(), &chunkReader));
scroggocf98fa92015-11-23 08:14:40 -0800824 REPORTER_ASSERT(r, codec);
825 if (!codec) {
826 return;
827 }
828
829 // Now compare to the original.
830 SkBitmap decodedBm;
831 decodedBm.setInfo(codec->getInfo());
832 decodedBm.allocPixels();
833 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
834 decodedBm.rowBytes());
835 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
836
837 if (decodedBm.colorType() != bm.colorType()) {
838 SkBitmap tmp;
839 bool success = decodedBm.copyTo(&tmp, bm.colorType());
840 REPORTER_ASSERT(r, success);
841 if (!success) {
842 return;
843 }
844
845 tmp.swap(decodedBm);
846 }
847
848 compare_to_good_digest(r, goodDigest, decodedBm);
849 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
850
851 // Decoding again will read the chunks again.
852 chunkReader.reset();
853 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
854 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
855 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
856 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
857}
scroggo8e6c7ad2016-09-16 08:20:38 -0700858#endif // SK_PNG_DISABLE_TESTS
scroggocf98fa92015-11-23 08:14:40 -0800859#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800860
scroggodb30be22015-12-08 18:54:13 -0800861// Stream that can only peek up to a limit
862class LimitedPeekingMemStream : public SkStream {
863public:
reed42943c82016-09-12 12:01:44 -0700864 LimitedPeekingMemStream(sk_sp<SkData> data, size_t limit)
865 : fStream(std::move(data))
scroggodb30be22015-12-08 18:54:13 -0800866 , fLimit(limit) {}
867
868 size_t peek(void* buf, size_t bytes) const override {
869 return fStream.peek(buf, SkTMin(bytes, fLimit));
870 }
871 size_t read(void* buf, size_t bytes) override {
872 return fStream.read(buf, bytes);
873 }
874 bool rewind() override {
875 return fStream.rewind();
876 }
877 bool isAtEnd() const override {
msarettff2a6c82016-09-07 11:23:28 -0700878 return fStream.isAtEnd();
scroggodb30be22015-12-08 18:54:13 -0800879 }
880private:
881 SkMemoryStream fStream;
882 const size_t fLimit;
883};
884
yujieqin9c7a8a42016-02-05 08:21:19 -0800885// Stream that is not an asset stream (!hasPosition() or !hasLength())
886class NotAssetMemStream : public SkStream {
887public:
bungeman38d909e2016-08-02 14:40:46 -0700888 NotAssetMemStream(sk_sp<SkData> data) : fStream(std::move(data)) {}
yujieqin9c7a8a42016-02-05 08:21:19 -0800889
890 bool hasPosition() const override {
891 return false;
892 }
893
894 bool hasLength() const override {
895 return false;
896 }
897
898 size_t peek(void* buf, size_t bytes) const override {
899 return fStream.peek(buf, bytes);
900 }
901 size_t read(void* buf, size_t bytes) override {
902 return fStream.read(buf, bytes);
903 }
904 bool rewind() override {
905 return fStream.rewind();
906 }
907 bool isAtEnd() const override {
908 return fStream.isAtEnd();
909 }
910private:
911 SkMemoryStream fStream;
912};
913
yujieqinf236ee42016-02-29 07:14:42 -0800914// Disable RAW tests for Win32.
915#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin9c7a8a42016-02-05 08:21:19 -0800916// Test that the RawCodec works also for not asset stream. This will test the code path using
917// SkRawBufferedStream instead of SkRawAssetStream.
yujieqin9c7a8a42016-02-05 08:21:19 -0800918DEF_TEST(Codec_raw_notseekable, r) {
919 const char* path = "dng_with_preview.dng";
920 SkString fullPath(GetResourcePath(path));
bungeman38d909e2016-08-02 14:40:46 -0700921 sk_sp<SkData> data(SkData::MakeFromFileName(fullPath.c_str()));
yujieqin9c7a8a42016-02-05 08:21:19 -0800922 if (!data) {
923 SkDebugf("Missing resource '%s'\n", path);
924 return;
925 }
926
Ben Wagner145dbcd2016-11-03 14:40:50 -0400927 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(std::move(data))));
yujieqin9c7a8a42016-02-05 08:21:19 -0800928 REPORTER_ASSERT(r, codec);
929
930 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
931}
932#endif
933
scroggodb30be22015-12-08 18:54:13 -0800934// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
935// + rewind() and succeed.
936DEF_TEST(Codec_webp_peek, r) {
937 const char* path = "baby_tux.webp";
938 SkString fullPath(GetResourcePath(path));
reedfde05112016-03-11 13:02:28 -0800939 auto data = SkData::MakeFromFileName(fullPath.c_str());
scroggodb30be22015-12-08 18:54:13 -0800940 if (!data) {
941 SkDebugf("Missing resource '%s'\n", path);
942 return;
943 }
944
945 // The limit is less than webp needs to peek or read.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400946 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(
reed42943c82016-09-12 12:01:44 -0700947 new LimitedPeekingMemStream(data, 25)));
scroggodb30be22015-12-08 18:54:13 -0800948 REPORTER_ASSERT(r, codec);
949
scroggo7b5e5532016-02-04 06:14:24 -0800950 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800951
952 // Similarly, a stream which does not peek should still succeed.
reed42943c82016-09-12 12:01:44 -0700953 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0)));
scroggodb30be22015-12-08 18:54:13 -0800954 REPORTER_ASSERT(r, codec);
955
scroggo7b5e5532016-02-04 06:14:24 -0800956 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800957}
958
msarett7f7ec202016-03-01 12:12:27 -0800959// SkCodec's wbmp decoder was initially unnecessarily restrictive.
960// It required the second byte to be zero. The wbmp specification allows
961// a couple of bits to be 1 (so long as they do not overlap with 0x9F).
962// Test that SkCodec now supports an image with these bits set.
scroggob9a1e342015-11-30 06:25:31 -0800963DEF_TEST(Codec_wbmp, r) {
964 const char* path = "mandrill.wbmp";
Ben Wagner145dbcd2016-11-03 14:40:50 -0400965 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
scroggob9a1e342015-11-30 06:25:31 -0800966 if (!stream) {
scroggob9a1e342015-11-30 06:25:31 -0800967 return;
968 }
969
970 // Modify the stream to contain a second byte with some bits set.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400971 auto data = SkCopyStreamToData(stream.get());
scroggob9a1e342015-11-30 06:25:31 -0800972 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
973 writeableData[1] = static_cast<uint8_t>(~0x9F);
974
msarett7f7ec202016-03-01 12:12:27 -0800975 // SkCodec should support this.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400976 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
scroggob9a1e342015-11-30 06:25:31 -0800977 REPORTER_ASSERT(r, codec);
978 if (!codec) {
979 return;
980 }
scroggo7b5e5532016-02-04 06:14:24 -0800981 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800982}
scroggodb30be22015-12-08 18:54:13 -0800983
984// wbmp images have a header that can be arbitrarily large, depending on the
985// size of the image. We cap the size at 65535, meaning we only need to look at
986// 8 bytes to determine whether we can read the image. This is important
987// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
988// image is a wbmp.
989DEF_TEST(Codec_wbmp_max_size, r) {
990 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
991 0x83, 0xFF, 0x7F, // W: 65535
992 0x83, 0xFF, 0x7F }; // H: 65535
Ben Wagner145dbcd2016-11-03 14:40:50 -0400993 std::unique_ptr<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
994 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -0800995
996 REPORTER_ASSERT(r, codec);
997 if (!codec) return;
998
999 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
1000 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
1001
1002 // Now test an image which is too big. Any image with a larger header (i.e.
1003 // has bigger width/height) is also too big.
1004 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
1005 0x84, 0x80, 0x00, // W: 65536
1006 0x84, 0x80, 0x00 }; // H: 65536
1007 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
mtklein18300a32016-03-16 13:53:35 -07001008 codec.reset(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -08001009
1010 REPORTER_ASSERT(r, !codec);
1011}
msarett2812f032016-07-18 15:56:08 -07001012
1013DEF_TEST(Codec_jpeg_rewind, r) {
1014 const char* path = "mandrill_512_q075.jpg";
Ben Wagner145dbcd2016-11-03 14:40:50 -04001015 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett2812f032016-07-18 15:56:08 -07001016 if (!stream) {
msarett2812f032016-07-18 15:56:08 -07001017 return;
1018 }
Ben Wagner145dbcd2016-11-03 14:40:50 -04001019 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarett2812f032016-07-18 15:56:08 -07001020 if (!codec) {
1021 ERRORF(r, "Unable to create codec '%s'.", path);
1022 return;
1023 }
1024
1025 const int width = codec->getInfo().width();
1026 const int height = codec->getInfo().height();
1027 size_t rowBytes = sizeof(SkPMColor) * width;
1028 SkAutoMalloc pixelStorage(height * rowBytes);
1029
1030 // Perform a sampled decode.
1031 SkAndroidCodec::AndroidOptions opts;
1032 opts.fSampleSize = 12;
1033 codec->getAndroidPixels(codec->getInfo().makeWH(width / 12, height / 12), pixelStorage.get(),
1034 rowBytes, &opts);
1035
1036 // Rewind the codec and perform a full image decode.
1037 SkCodec::Result result = codec->getPixels(codec->getInfo(), pixelStorage.get(), rowBytes);
1038 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1039}
msarett549ca322016-08-17 08:54:08 -07001040
msarett35bb74b2016-08-22 07:41:28 -07001041static void check_color_xform(skiatest::Reporter* r, const char* path) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001042 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(GetResourceAsStream(path)));
msarett35bb74b2016-08-22 07:41:28 -07001043
1044 SkAndroidCodec::AndroidOptions opts;
1045 opts.fSampleSize = 3;
1046 const int subsetWidth = codec->getInfo().width() / 2;
1047 const int subsetHeight = codec->getInfo().height() / 2;
1048 SkIRect subset = SkIRect::MakeWH(subsetWidth, subsetHeight);
1049 opts.fSubset = &subset;
1050
1051 const int dstWidth = subsetWidth / opts.fSampleSize;
1052 const int dstHeight = subsetHeight / opts.fSampleSize;
1053 sk_sp<SkData> data = SkData::MakeFromFileName(
1054 GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str());
Brian Osman526972e2016-10-24 09:24:02 -04001055 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeICC(data->data(), data->size());
msarett35bb74b2016-08-22 07:41:28 -07001056 SkImageInfo dstInfo = codec->getInfo().makeWH(dstWidth, dstHeight)
1057 .makeColorType(kN32_SkColorType)
1058 .makeColorSpace(colorSpace);
1059
1060 size_t rowBytes = dstInfo.minRowBytes();
1061 SkAutoMalloc pixelStorage(dstInfo.getSafeSize(rowBytes));
1062 SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get(), rowBytes, &opts);
1063 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1064}
1065
1066DEF_TEST(Codec_ColorXform, r) {
1067 check_color_xform(r, "mandrill_512_q075.jpg");
1068 check_color_xform(r, "mandrill_512.png");
1069}
1070
msarettf17b71f2016-09-12 14:30:03 -07001071static bool color_type_match(SkColorType origColorType, SkColorType codecColorType) {
1072 switch (origColorType) {
1073 case kRGBA_8888_SkColorType:
1074 case kBGRA_8888_SkColorType:
1075 return kRGBA_8888_SkColorType == codecColorType ||
1076 kBGRA_8888_SkColorType == codecColorType;
1077 default:
1078 return origColorType == codecColorType;
1079 }
1080}
1081
1082static bool alpha_type_match(SkAlphaType origAlphaType, SkAlphaType codecAlphaType) {
1083 switch (origAlphaType) {
1084 case kUnpremul_SkAlphaType:
1085 case kPremul_SkAlphaType:
1086 return kUnpremul_SkAlphaType == codecAlphaType ||
1087 kPremul_SkAlphaType == codecAlphaType;
1088 default:
1089 return origAlphaType == codecAlphaType;
1090 }
1091}
1092
1093static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const SkImageInfo& info) {
1094 SkBitmap bm1;
1095 SkPMColor colors[256];
Hal Canary342b7ac2016-11-04 11:49:42 -04001096 sk_sp<SkColorTable> colorTable1(new SkColorTable(colors, 256));
msarettf17b71f2016-09-12 14:30:03 -07001097 bm1.allocPixels(info, nullptr, colorTable1.get());
1098 int numColors;
1099 SkCodec::Result result = origCodec->getPixels(info, bm1.getPixels(), bm1.rowBytes(), nullptr,
1100 const_cast<SkPMColor*>(colorTable1->readColors()),
1101 &numColors);
1102 // This will fail to update colorTable1->count() but is fine for the purpose of this test.
1103 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarett9b09cd82016-08-29 14:47:49 -07001104
1105 // Encode the image to png.
1106 sk_sp<SkData> data =
Hal Canarydb683012016-11-23 08:55:18 -07001107 sk_sp<SkData>(sk_tool_utils::EncodeImageToData(bm1, SkEncodedImageFormat::kPNG, 100));
msarett9b09cd82016-08-29 14:47:49 -07001108
Ben Wagner145dbcd2016-11-03 14:40:50 -04001109 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
msarettf17b71f2016-09-12 14:30:03 -07001110 REPORTER_ASSERT(r, color_type_match(info.colorType(), codec->getInfo().colorType()));
1111 REPORTER_ASSERT(r, alpha_type_match(info.alphaType(), codec->getInfo().alphaType()));
msarett9b09cd82016-08-29 14:47:49 -07001112
1113 SkBitmap bm2;
Hal Canary342b7ac2016-11-04 11:49:42 -04001114 sk_sp<SkColorTable> colorTable2(new SkColorTable(colors, 256));
msarettf17b71f2016-09-12 14:30:03 -07001115 bm2.allocPixels(info, nullptr, colorTable2.get());
1116 result = codec->getPixels(info, bm2.getPixels(), bm2.rowBytes(), nullptr,
1117 const_cast<SkPMColor*>(colorTable2->readColors()), &numColors);
msarett9b09cd82016-08-29 14:47:49 -07001118 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1119
1120 SkMD5::Digest d1, d2;
1121 md5(bm1, &d1);
1122 md5(bm2, &d2);
1123 REPORTER_ASSERT(r, d1 == d2);
1124}
1125
1126DEF_TEST(Codec_PngRoundTrip, r) {
msarett549ca322016-08-17 08:54:08 -07001127 const char* path = "mandrill_512_q075.jpg";
Ben Wagner145dbcd2016-11-03 14:40:50 -04001128 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
1129 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett549ca322016-08-17 08:54:08 -07001130
msarettf17b71f2016-09-12 14:30:03 -07001131 SkColorType colorTypesOpaque[] = {
1132 kRGB_565_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1133 };
1134 for (SkColorType colorType : colorTypesOpaque) {
1135 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType);
1136 check_round_trip(r, codec.get(), newInfo);
1137 }
1138
msarett9b09cd82016-08-29 14:47:49 -07001139 path = "grayscale.jpg";
bungemanf93d7112016-09-16 06:24:20 -07001140 stream.reset(GetResourceAsStream(path));
msarett9b09cd82016-08-29 14:47:49 -07001141 codec.reset(SkCodec::NewFromStream(stream.release()));
msarettf17b71f2016-09-12 14:30:03 -07001142 check_round_trip(r, codec.get(), codec->getInfo());
1143
1144 path = "yellow_rose.png";
bungemanf93d7112016-09-16 06:24:20 -07001145 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001146 codec.reset(SkCodec::NewFromStream(stream.release()));
1147
1148 SkColorType colorTypesWithAlpha[] = {
1149 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1150 };
1151 SkAlphaType alphaTypes[] = {
1152 kUnpremul_SkAlphaType, kPremul_SkAlphaType
1153 };
1154 for (SkColorType colorType : colorTypesWithAlpha) {
1155 for (SkAlphaType alphaType : alphaTypes) {
1156 // Set color space to nullptr because color correct premultiplies do not round trip.
1157 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType)
1158 .makeAlphaType(alphaType)
1159 .makeColorSpace(nullptr);
1160 check_round_trip(r, codec.get(), newInfo);
1161 }
1162 }
1163
1164 path = "index8.png";
bungemanf93d7112016-09-16 06:24:20 -07001165 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001166 codec.reset(SkCodec::NewFromStream(stream.release()));
1167
1168 for (SkAlphaType alphaType : alphaTypes) {
1169 SkImageInfo newInfo = codec->getInfo().makeAlphaType(alphaType)
1170 .makeColorSpace(nullptr);
1171 check_round_trip(r, codec.get(), newInfo);
1172 }
msarett549ca322016-08-17 08:54:08 -07001173}
msarett2ecc35f2016-09-08 11:55:16 -07001174
1175static void test_conversion_possible(skiatest::Reporter* r, const char* path,
scroggo8e6c7ad2016-09-16 08:20:38 -07001176 bool supportsScanlineDecoder,
1177 bool supportsIncrementalDecoder) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001178 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
1179 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett2ecc35f2016-09-08 11:55:16 -07001180 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType);
1181
1182 SkBitmap bm;
1183 bm.allocPixels(infoF16);
1184 SkCodec::Result result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1185 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001186
1187 result = codec->startScanlineDecode(infoF16);
1188 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001189 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001190 } else {
1191 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1192 }
1193
1194 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1195 if (supportsIncrementalDecoder) {
1196 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
1197 } else {
1198 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001199 }
1200
raftias94888332016-10-18 10:02:51 -07001201 SkASSERT(SkColorSpace_Base::Type::kXYZ == as_CSB(infoF16.colorSpace())->type());
1202 SkColorSpace_XYZ* csXYZ = static_cast<SkColorSpace_XYZ*>(infoF16.colorSpace());
1203 infoF16 = infoF16.makeColorSpace(csXYZ->makeLinearGamma());
msarett2ecc35f2016-09-08 11:55:16 -07001204 result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1205 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001206 result = codec->startScanlineDecode(infoF16);
1207 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001208 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001209 } else {
1210 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1211 }
1212
1213 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1214 if (supportsIncrementalDecoder) {
1215 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1216 } else {
1217 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001218 }
1219}
1220
1221DEF_TEST(Codec_F16ConversionPossible, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -07001222 test_conversion_possible(r, "color_wheel.webp", false, false);
1223 test_conversion_possible(r, "mandrill_512_q075.jpg", true, false);
1224 test_conversion_possible(r, "yellow_rose.png", false, true);
1225}
1226
scroggo19b91532016-10-24 09:03:26 -07001227static void decode_frame(skiatest::Reporter* r, SkCodec* codec, size_t frame) {
1228 SkBitmap bm;
1229 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1230 bm.allocPixels(info);
1231
1232 SkCodec::Options opts;
1233 opts.fFrameIndex = frame;
1234 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->getPixels(info,
1235 bm.getPixels(), bm.rowBytes(), &opts, nullptr, nullptr));
1236}
1237
1238// For an animated image, we should only read enough to decode the requested
1239// frame if the client never calls getFrameInfo.
1240DEF_TEST(Codec_skipFullParse, r) {
1241 auto path = "test640x479.gif";
1242 SkStream* stream(GetResourceAsStream(path));
1243 if (!stream) {
1244 return;
1245 }
1246
1247 // Note that we cheat and hold on to the stream pointer, but SkCodec will
1248 // take ownership. We will not refer to the stream after the SkCodec
1249 // deletes it.
1250 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
1251 if (!codec) {
1252 ERRORF(r, "Failed to create codec for %s", path);
1253 return;
1254 }
1255
1256 REPORTER_ASSERT(r, stream->hasPosition());
1257 const size_t sizePosition = stream->getPosition();
1258 REPORTER_ASSERT(r, stream->hasLength() && sizePosition < stream->getLength());
1259
1260 // This should read more of the stream, but not the whole stream.
1261 decode_frame(r, codec.get(), 0);
1262 const size_t positionAfterFirstFrame = stream->getPosition();
1263 REPORTER_ASSERT(r, positionAfterFirstFrame > sizePosition
1264 && positionAfterFirstFrame < stream->getLength());
1265
1266 // Again, this should read more of the stream.
1267 decode_frame(r, codec.get(), 2);
1268 const size_t positionAfterThirdFrame = stream->getPosition();
1269 REPORTER_ASSERT(r, positionAfterThirdFrame > positionAfterFirstFrame
1270 && positionAfterThirdFrame < stream->getLength());
1271
1272 // This does not need to read any more of the stream, since it has already
1273 // parsed the second frame.
1274 decode_frame(r, codec.get(), 1);
1275 REPORTER_ASSERT(r, stream->getPosition() == positionAfterThirdFrame);
1276
1277 // This should read the rest of the frames.
1278 decode_frame(r, codec.get(), 3);
1279 const size_t finalPosition = stream->getPosition();
1280 REPORTER_ASSERT(r, finalPosition > positionAfterThirdFrame);
1281
1282 // There may be more data in the stream.
1283 auto frameInfo = codec->getFrameInfo();
1284 REPORTER_ASSERT(r, frameInfo.size() == 4);
1285 REPORTER_ASSERT(r, stream->getPosition() >= finalPosition);
1286}
1287
scroggo8e6c7ad2016-09-16 08:20:38 -07001288// Only rewinds up to a limit.
1289class LimitedRewindingStream : public SkStream {
1290public:
1291 static SkStream* Make(const char path[], size_t limit) {
1292 SkStream* stream = GetResourceAsStream(path);
1293 if (!stream) {
1294 return nullptr;
1295 }
1296 return new LimitedRewindingStream(stream, limit);
1297 }
1298
1299 size_t read(void* buffer, size_t size) override {
1300 const size_t bytes = fStream->read(buffer, size);
1301 fPosition += bytes;
1302 return bytes;
1303 }
1304
1305 bool isAtEnd() const override {
1306 return fStream->isAtEnd();
1307 }
1308
1309 bool rewind() override {
1310 if (fPosition <= fLimit && fStream->rewind()) {
1311 fPosition = 0;
1312 return true;
1313 }
1314
1315 return false;
1316 }
1317
1318private:
Ben Wagner145dbcd2016-11-03 14:40:50 -04001319 std::unique_ptr<SkStream> fStream;
1320 const size_t fLimit;
1321 size_t fPosition;
scroggo8e6c7ad2016-09-16 08:20:38 -07001322
1323 LimitedRewindingStream(SkStream* stream, size_t limit)
1324 : fStream(stream)
1325 , fLimit(limit)
1326 , fPosition(0)
1327 {
1328 SkASSERT(fStream);
1329 }
1330};
1331
1332DEF_TEST(Codec_fallBack, r) {
1333 // SkAndroidCodec needs to be able to fall back to scanline decoding
1334 // if incremental decoding does not work. Make sure this does not
1335 // require a rewind.
1336
1337 // Formats that currently do not support incremental decoding
1338 auto files = {
scroggo8e6c7ad2016-09-16 08:20:38 -07001339 "CMYK.jpg",
1340 "color_wheel.ico",
1341 "mandrill.wbmp",
1342 "randPixels.bmp",
1343 };
1344 for (auto file : files) {
1345 SkStream* stream = LimitedRewindingStream::Make(file, 14);
1346 if (!stream) {
1347 SkDebugf("Missing resources (%s). Set --resourcePath.\n", file);
1348 return;
1349 }
1350
Ben Wagner145dbcd2016-11-03 14:40:50 -04001351 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
scroggo8e6c7ad2016-09-16 08:20:38 -07001352 if (!codec) {
1353 ERRORF(r, "Failed to create codec for %s,", file);
1354 continue;
1355 }
1356
1357 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
1358 SkBitmap bm;
1359 bm.allocPixels(info);
1360
1361 if (SkCodec::kUnimplemented != codec->startIncrementalDecode(info, bm.getPixels(),
1362 bm.rowBytes())) {
1363 ERRORF(r, "Is scanline decoding now implemented for %s?", file);
1364 continue;
1365 }
1366
1367 // Scanline decoding should not require a rewind.
1368 SkCodec::Result result = codec->startScanlineDecode(info);
1369 if (SkCodec::kSuccess != result) {
1370 ERRORF(r, "Scanline decoding failed for %s with %i", file, result);
1371 }
1372 }
msarett2ecc35f2016-09-08 11:55:16 -07001373}
scroggoc46cdd42016-10-10 06:45:32 -07001374
1375// This test verifies that we fixed an assert statement that fired when reusing a png codec
1376// after scaling.
1377DEF_TEST(Codec_reusePng, r) {
1378 std::unique_ptr<SkStream> stream(GetResourceAsStream("plane.png"));
1379 if (!stream) {
1380 return;
1381 }
1382
1383 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
1384 if (!codec) {
1385 ERRORF(r, "Failed to create codec\n");
1386 return;
1387 }
1388
1389 SkAndroidCodec::AndroidOptions opts;
1390 opts.fSampleSize = 5;
1391 auto size = codec->getSampledDimensions(opts.fSampleSize);
1392 auto info = codec->getInfo().makeWH(size.fWidth, size.fHeight).makeColorType(kN32_SkColorType);
1393 SkBitmap bm;
1394 bm.allocPixels(info);
1395 auto result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1396 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1397
1398 info = codec->getInfo().makeColorType(kN32_SkColorType);
1399 bm.allocPixels(info);
1400 opts.fSampleSize = 1;
1401 result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1402 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1403}
scroggoe61b3b42016-10-10 07:17:32 -07001404
1405DEF_TEST(Codec_rowsDecoded, r) {
1406 auto file = "plane_interlaced.png";
1407 std::unique_ptr<SkStream> stream(GetResourceAsStream(file));
1408 if (!stream) {
1409 return;
1410 }
1411
1412 // This is enough to read the header etc, but no rows.
1413 auto data = SkData::MakeFromStream(stream.get(), 99);
1414 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
1415 if (!codec) {
1416 ERRORF(r, "Failed to create codec\n");
1417 return;
1418 }
1419
1420 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1421 SkBitmap bm;
1422 bm.allocPixels(info);
1423 auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
1424 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1425
1426 // This is an arbitrary value. The important fact is that it is not zero, and rowsDecoded
1427 // should get set to zero by incrementalDecode.
1428 int rowsDecoded = 77;
1429 result = codec->incrementalDecode(&rowsDecoded);
1430 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput);
1431 REPORTER_ASSERT(r, rowsDecoded == 0);
1432}
Matt Sarett29121eb2016-10-17 14:32:46 -04001433
Matt Sarett8a4e9c52016-10-25 14:24:50 -04001434static void test_invalid_images(skiatest::Reporter* r, const char* path, bool shouldSucceed) {
Matt Sarett29121eb2016-10-17 14:32:46 -04001435 SkBitmap bitmap;
Matt Sarett8a4e9c52016-10-25 14:24:50 -04001436 const bool success = GetResourceAsBitmap(path, &bitmap);
1437 REPORTER_ASSERT(r, success == shouldSucceed);
1438}
1439
1440DEF_TEST(Codec_InvalidImages, r) {
1441 // ASAN will complain if there is an issue.
1442 test_invalid_images(r, "invalid_images/int_overflow.ico", false);
1443 test_invalid_images(r, "invalid_images/skbug5887.gif", true);
Matt Sarettdbdf6d22016-11-08 15:26:56 -05001444 test_invalid_images(r, "invalid_images/many-progressive-scans.jpg", false);
Matt Sarett29121eb2016-10-17 14:32:46 -04001445}