blob: dd969960dc883c059fa20204494037d177dab8d8 [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");
Leon Scroggins IIId87fbee2016-12-02 16:47:53 -0500651#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
652 test_invalid(r, "empty_images/zero_height.tiff");
653#endif
msarett4b17fa32015-04-23 08:53:39 -0700654}
msarett99f567e2015-08-05 12:58:26 -0700655
656static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400657 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett99f567e2015-08-05 12:58:26 -0700658 if (!stream) {
msarett99f567e2015-08-05 12:58:26 -0700659 return;
660 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400661 std::unique_ptr<SkCodec> decoder(SkCodec::NewFromStream(stream.release()));
scroggo8e6c7ad2016-09-16 08:20:38 -0700662 if (!decoder) {
663 SkDebugf("Missing codec for %s\n", path);
664 return;
665 }
666
667 const SkImageInfo info = decoder->getInfo().makeColorType(kIndex_8_SkColorType);
halcanary9d524f22016-03-29 09:03:52 -0700668
msarett99f567e2015-08-05 12:58:26 -0700669 // This should return kSuccess because kIndex8 is supported.
670 SkPMColor colorStorage[256];
671 int colorCount;
scroggo8e6c7ad2016-09-16 08:20:38 -0700672 SkCodec::Result result = decoder->startScanlineDecode(info, nullptr, colorStorage,
673 &colorCount);
674 if (SkCodec::kSuccess == result) {
675 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
676 // colorPtr and a valid colorCountPtr.
677 result = decoder->startScanlineDecode(info, nullptr, nullptr, nullptr);
678 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
679 result = decoder->startScanlineDecode(info);
680 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
681 } else if (SkCodec::kUnimplemented == result) {
682 // New method should be supported:
683 SkBitmap bm;
684 sk_sp<SkColorTable> colorTable(new SkColorTable(colorStorage, 256));
685 bm.allocPixels(info, nullptr, colorTable.get());
686 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes(), nullptr,
687 colorStorage, &colorCount);
688 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
689 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
690 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
691 } else {
692 // The test is uninteresting if kIndex8 is not supported
693 ERRORF(r, "Should not call test_invalid_parameters for non-Index8 file: %s\n", path);
msarett99f567e2015-08-05 12:58:26 -0700694 return;
695 }
696
msarett99f567e2015-08-05 12:58:26 -0700697}
698
699DEF_TEST(Codec_Params, r) {
700 test_invalid_parameters(r, "index8.png");
701 test_invalid_parameters(r, "mandrill.wbmp");
702}
scroggocf98fa92015-11-23 08:14:40 -0800703
scroggo8e6c7ad2016-09-16 08:20:38 -0700704#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
705
706#ifndef SK_PNG_DISABLE_TESTS // reading chunks does not work properly with older versions.
707 // It does not appear that anyone in Google3 is reading chunks.
708
scroggocf98fa92015-11-23 08:14:40 -0800709static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
710 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
711 if (!sk_stream->write(data, len)) {
712 png_error(png_ptr, "sk_write_fn Error!");
713 }
714}
715
scroggocf98fa92015-11-23 08:14:40 -0800716DEF_TEST(Codec_pngChunkReader, r) {
717 // Create a dummy bitmap. Use unpremul RGBA for libpng.
718 SkBitmap bm;
719 const int w = 1;
720 const int h = 1;
721 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
722 kUnpremul_SkAlphaType);
723 bm.setInfo(bmInfo);
724 bm.allocPixels();
725 bm.eraseColor(SK_ColorBLUE);
726 SkMD5::Digest goodDigest;
727 md5(bm, &goodDigest);
728
729 // Write to a png file.
730 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
731 REPORTER_ASSERT(r, png);
732 if (!png) {
733 return;
734 }
735
736 png_infop info = png_create_info_struct(png);
737 REPORTER_ASSERT(r, info);
738 if (!info) {
739 png_destroy_write_struct(&png, nullptr);
740 return;
741 }
742
743 if (setjmp(png_jmpbuf(png))) {
744 ERRORF(r, "failed writing png");
745 png_destroy_write_struct(&png, &info);
746 return;
747 }
748
749 SkDynamicMemoryWStream wStream;
750 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
751
752 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
753 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
754 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
755
756 // Create some chunks that match the Android framework's use.
757 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800758 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
759 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
760 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800761 };
762
763 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
764 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
765#if PNG_LIBPNG_VER < 10600
766 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800767 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
768 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800769#endif
770
771 png_write_info(png, info);
772
773 for (int j = 0; j < h; j++) {
774 png_bytep row = (png_bytep)(bm.getAddr(0, j));
775 png_write_rows(png, &row, 1);
776 }
777 png_write_end(png, info);
778 png_destroy_write_struct(&png, &info);
779
780 class ChunkReader : public SkPngChunkReader {
781 public:
782 ChunkReader(skiatest::Reporter* r)
783 : fReporter(r)
784 {
785 this->reset();
786 }
787
788 bool readChunk(const char tag[], const void* data, size_t length) override {
789 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
790 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
791 // Tag matches. This should have been the first time we see it.
792 REPORTER_ASSERT(fReporter, !fSeen[i]);
793 fSeen[i] = true;
794
795 // Data and length should match
796 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
797 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
798 (const char*) gUnknowns[i].data));
799 return true;
800 }
801 }
802 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
803 return true;
804 }
805
806 bool allHaveBeenSeen() {
807 bool ret = true;
808 for (auto seen : fSeen) {
809 ret &= seen;
810 }
811 return ret;
812 }
813
814 void reset() {
815 sk_bzero(fSeen, sizeof(fSeen));
816 }
817
818 private:
819 skiatest::Reporter* fReporter; // Unowned
820 bool fSeen[3];
821 };
822
823 ChunkReader chunkReader(r);
824
825 // Now read the file with SkCodec.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400826 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(wStream.detachAsData(), &chunkReader));
scroggocf98fa92015-11-23 08:14:40 -0800827 REPORTER_ASSERT(r, codec);
828 if (!codec) {
829 return;
830 }
831
832 // Now compare to the original.
833 SkBitmap decodedBm;
834 decodedBm.setInfo(codec->getInfo());
835 decodedBm.allocPixels();
836 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
837 decodedBm.rowBytes());
838 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
839
840 if (decodedBm.colorType() != bm.colorType()) {
841 SkBitmap tmp;
842 bool success = decodedBm.copyTo(&tmp, bm.colorType());
843 REPORTER_ASSERT(r, success);
844 if (!success) {
845 return;
846 }
847
848 tmp.swap(decodedBm);
849 }
850
851 compare_to_good_digest(r, goodDigest, decodedBm);
852 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
853
854 // Decoding again will read the chunks again.
855 chunkReader.reset();
856 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
857 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
858 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
859 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
860}
scroggo8e6c7ad2016-09-16 08:20:38 -0700861#endif // SK_PNG_DISABLE_TESTS
scroggocf98fa92015-11-23 08:14:40 -0800862#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800863
scroggodb30be22015-12-08 18:54:13 -0800864// Stream that can only peek up to a limit
865class LimitedPeekingMemStream : public SkStream {
866public:
reed42943c82016-09-12 12:01:44 -0700867 LimitedPeekingMemStream(sk_sp<SkData> data, size_t limit)
868 : fStream(std::move(data))
scroggodb30be22015-12-08 18:54:13 -0800869 , fLimit(limit) {}
870
871 size_t peek(void* buf, size_t bytes) const override {
872 return fStream.peek(buf, SkTMin(bytes, fLimit));
873 }
874 size_t read(void* buf, size_t bytes) override {
875 return fStream.read(buf, bytes);
876 }
877 bool rewind() override {
878 return fStream.rewind();
879 }
880 bool isAtEnd() const override {
msarettff2a6c82016-09-07 11:23:28 -0700881 return fStream.isAtEnd();
scroggodb30be22015-12-08 18:54:13 -0800882 }
883private:
884 SkMemoryStream fStream;
885 const size_t fLimit;
886};
887
yujieqin9c7a8a42016-02-05 08:21:19 -0800888// Stream that is not an asset stream (!hasPosition() or !hasLength())
889class NotAssetMemStream : public SkStream {
890public:
bungeman38d909e2016-08-02 14:40:46 -0700891 NotAssetMemStream(sk_sp<SkData> data) : fStream(std::move(data)) {}
yujieqin9c7a8a42016-02-05 08:21:19 -0800892
893 bool hasPosition() const override {
894 return false;
895 }
896
897 bool hasLength() const override {
898 return false;
899 }
900
901 size_t peek(void* buf, size_t bytes) const override {
902 return fStream.peek(buf, bytes);
903 }
904 size_t read(void* buf, size_t bytes) override {
905 return fStream.read(buf, bytes);
906 }
907 bool rewind() override {
908 return fStream.rewind();
909 }
910 bool isAtEnd() const override {
911 return fStream.isAtEnd();
912 }
913private:
914 SkMemoryStream fStream;
915};
916
yujieqinf236ee42016-02-29 07:14:42 -0800917// Disable RAW tests for Win32.
918#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin9c7a8a42016-02-05 08:21:19 -0800919// Test that the RawCodec works also for not asset stream. This will test the code path using
920// SkRawBufferedStream instead of SkRawAssetStream.
yujieqin9c7a8a42016-02-05 08:21:19 -0800921DEF_TEST(Codec_raw_notseekable, r) {
922 const char* path = "dng_with_preview.dng";
923 SkString fullPath(GetResourcePath(path));
bungeman38d909e2016-08-02 14:40:46 -0700924 sk_sp<SkData> data(SkData::MakeFromFileName(fullPath.c_str()));
yujieqin9c7a8a42016-02-05 08:21:19 -0800925 if (!data) {
926 SkDebugf("Missing resource '%s'\n", path);
927 return;
928 }
929
Ben Wagner145dbcd2016-11-03 14:40:50 -0400930 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(std::move(data))));
yujieqin9c7a8a42016-02-05 08:21:19 -0800931 REPORTER_ASSERT(r, codec);
932
933 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
934}
935#endif
936
scroggodb30be22015-12-08 18:54:13 -0800937// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
938// + rewind() and succeed.
939DEF_TEST(Codec_webp_peek, r) {
940 const char* path = "baby_tux.webp";
941 SkString fullPath(GetResourcePath(path));
reedfde05112016-03-11 13:02:28 -0800942 auto data = SkData::MakeFromFileName(fullPath.c_str());
scroggodb30be22015-12-08 18:54:13 -0800943 if (!data) {
944 SkDebugf("Missing resource '%s'\n", path);
945 return;
946 }
947
948 // The limit is less than webp needs to peek or read.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400949 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(
reed42943c82016-09-12 12:01:44 -0700950 new LimitedPeekingMemStream(data, 25)));
scroggodb30be22015-12-08 18:54:13 -0800951 REPORTER_ASSERT(r, codec);
952
scroggo7b5e5532016-02-04 06:14:24 -0800953 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800954
955 // Similarly, a stream which does not peek should still succeed.
reed42943c82016-09-12 12:01:44 -0700956 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0)));
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
msarett7f7ec202016-03-01 12:12:27 -0800962// SkCodec's wbmp decoder was initially unnecessarily restrictive.
963// It required the second byte to be zero. The wbmp specification allows
964// a couple of bits to be 1 (so long as they do not overlap with 0x9F).
965// Test that SkCodec now supports an image with these bits set.
scroggob9a1e342015-11-30 06:25:31 -0800966DEF_TEST(Codec_wbmp, r) {
967 const char* path = "mandrill.wbmp";
Ben Wagner145dbcd2016-11-03 14:40:50 -0400968 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
scroggob9a1e342015-11-30 06:25:31 -0800969 if (!stream) {
scroggob9a1e342015-11-30 06:25:31 -0800970 return;
971 }
972
973 // Modify the stream to contain a second byte with some bits set.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400974 auto data = SkCopyStreamToData(stream.get());
scroggob9a1e342015-11-30 06:25:31 -0800975 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
976 writeableData[1] = static_cast<uint8_t>(~0x9F);
977
msarett7f7ec202016-03-01 12:12:27 -0800978 // SkCodec should support this.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400979 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
scroggob9a1e342015-11-30 06:25:31 -0800980 REPORTER_ASSERT(r, codec);
981 if (!codec) {
982 return;
983 }
scroggo7b5e5532016-02-04 06:14:24 -0800984 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800985}
scroggodb30be22015-12-08 18:54:13 -0800986
987// wbmp images have a header that can be arbitrarily large, depending on the
988// size of the image. We cap the size at 65535, meaning we only need to look at
989// 8 bytes to determine whether we can read the image. This is important
990// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
991// image is a wbmp.
992DEF_TEST(Codec_wbmp_max_size, r) {
993 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
994 0x83, 0xFF, 0x7F, // W: 65535
995 0x83, 0xFF, 0x7F }; // H: 65535
Ben Wagner145dbcd2016-11-03 14:40:50 -0400996 std::unique_ptr<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
997 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -0800998
999 REPORTER_ASSERT(r, codec);
1000 if (!codec) return;
1001
1002 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
1003 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
1004
1005 // Now test an image which is too big. Any image with a larger header (i.e.
1006 // has bigger width/height) is also too big.
1007 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
1008 0x84, 0x80, 0x00, // W: 65536
1009 0x84, 0x80, 0x00 }; // H: 65536
1010 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
mtklein18300a32016-03-16 13:53:35 -07001011 codec.reset(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -08001012
1013 REPORTER_ASSERT(r, !codec);
1014}
msarett2812f032016-07-18 15:56:08 -07001015
1016DEF_TEST(Codec_jpeg_rewind, r) {
1017 const char* path = "mandrill_512_q075.jpg";
Ben Wagner145dbcd2016-11-03 14:40:50 -04001018 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett2812f032016-07-18 15:56:08 -07001019 if (!stream) {
msarett2812f032016-07-18 15:56:08 -07001020 return;
1021 }
Ben Wagner145dbcd2016-11-03 14:40:50 -04001022 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarett2812f032016-07-18 15:56:08 -07001023 if (!codec) {
1024 ERRORF(r, "Unable to create codec '%s'.", path);
1025 return;
1026 }
1027
1028 const int width = codec->getInfo().width();
1029 const int height = codec->getInfo().height();
1030 size_t rowBytes = sizeof(SkPMColor) * width;
1031 SkAutoMalloc pixelStorage(height * rowBytes);
1032
1033 // Perform a sampled decode.
1034 SkAndroidCodec::AndroidOptions opts;
1035 opts.fSampleSize = 12;
1036 codec->getAndroidPixels(codec->getInfo().makeWH(width / 12, height / 12), pixelStorage.get(),
1037 rowBytes, &opts);
1038
1039 // Rewind the codec and perform a full image decode.
1040 SkCodec::Result result = codec->getPixels(codec->getInfo(), pixelStorage.get(), rowBytes);
1041 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1042}
msarett549ca322016-08-17 08:54:08 -07001043
msarett35bb74b2016-08-22 07:41:28 -07001044static void check_color_xform(skiatest::Reporter* r, const char* path) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001045 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(GetResourceAsStream(path)));
msarett35bb74b2016-08-22 07:41:28 -07001046
1047 SkAndroidCodec::AndroidOptions opts;
1048 opts.fSampleSize = 3;
1049 const int subsetWidth = codec->getInfo().width() / 2;
1050 const int subsetHeight = codec->getInfo().height() / 2;
1051 SkIRect subset = SkIRect::MakeWH(subsetWidth, subsetHeight);
1052 opts.fSubset = &subset;
1053
1054 const int dstWidth = subsetWidth / opts.fSampleSize;
1055 const int dstHeight = subsetHeight / opts.fSampleSize;
1056 sk_sp<SkData> data = SkData::MakeFromFileName(
1057 GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str());
Brian Osman526972e2016-10-24 09:24:02 -04001058 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeICC(data->data(), data->size());
msarett35bb74b2016-08-22 07:41:28 -07001059 SkImageInfo dstInfo = codec->getInfo().makeWH(dstWidth, dstHeight)
1060 .makeColorType(kN32_SkColorType)
1061 .makeColorSpace(colorSpace);
1062
1063 size_t rowBytes = dstInfo.minRowBytes();
1064 SkAutoMalloc pixelStorage(dstInfo.getSafeSize(rowBytes));
1065 SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get(), rowBytes, &opts);
1066 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1067}
1068
1069DEF_TEST(Codec_ColorXform, r) {
1070 check_color_xform(r, "mandrill_512_q075.jpg");
1071 check_color_xform(r, "mandrill_512.png");
1072}
1073
msarettf17b71f2016-09-12 14:30:03 -07001074static bool color_type_match(SkColorType origColorType, SkColorType codecColorType) {
1075 switch (origColorType) {
1076 case kRGBA_8888_SkColorType:
1077 case kBGRA_8888_SkColorType:
1078 return kRGBA_8888_SkColorType == codecColorType ||
1079 kBGRA_8888_SkColorType == codecColorType;
1080 default:
1081 return origColorType == codecColorType;
1082 }
1083}
1084
1085static bool alpha_type_match(SkAlphaType origAlphaType, SkAlphaType codecAlphaType) {
1086 switch (origAlphaType) {
1087 case kUnpremul_SkAlphaType:
1088 case kPremul_SkAlphaType:
1089 return kUnpremul_SkAlphaType == codecAlphaType ||
1090 kPremul_SkAlphaType == codecAlphaType;
1091 default:
1092 return origAlphaType == codecAlphaType;
1093 }
1094}
1095
1096static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const SkImageInfo& info) {
1097 SkBitmap bm1;
1098 SkPMColor colors[256];
Hal Canary342b7ac2016-11-04 11:49:42 -04001099 sk_sp<SkColorTable> colorTable1(new SkColorTable(colors, 256));
msarettf17b71f2016-09-12 14:30:03 -07001100 bm1.allocPixels(info, nullptr, colorTable1.get());
1101 int numColors;
1102 SkCodec::Result result = origCodec->getPixels(info, bm1.getPixels(), bm1.rowBytes(), nullptr,
1103 const_cast<SkPMColor*>(colorTable1->readColors()),
1104 &numColors);
1105 // This will fail to update colorTable1->count() but is fine for the purpose of this test.
1106 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarett9b09cd82016-08-29 14:47:49 -07001107
1108 // Encode the image to png.
1109 sk_sp<SkData> data =
Hal Canarydb683012016-11-23 08:55:18 -07001110 sk_sp<SkData>(sk_tool_utils::EncodeImageToData(bm1, SkEncodedImageFormat::kPNG, 100));
msarett9b09cd82016-08-29 14:47:49 -07001111
Ben Wagner145dbcd2016-11-03 14:40:50 -04001112 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
msarettf17b71f2016-09-12 14:30:03 -07001113 REPORTER_ASSERT(r, color_type_match(info.colorType(), codec->getInfo().colorType()));
1114 REPORTER_ASSERT(r, alpha_type_match(info.alphaType(), codec->getInfo().alphaType()));
msarett9b09cd82016-08-29 14:47:49 -07001115
1116 SkBitmap bm2;
Hal Canary342b7ac2016-11-04 11:49:42 -04001117 sk_sp<SkColorTable> colorTable2(new SkColorTable(colors, 256));
msarettf17b71f2016-09-12 14:30:03 -07001118 bm2.allocPixels(info, nullptr, colorTable2.get());
1119 result = codec->getPixels(info, bm2.getPixels(), bm2.rowBytes(), nullptr,
1120 const_cast<SkPMColor*>(colorTable2->readColors()), &numColors);
msarett9b09cd82016-08-29 14:47:49 -07001121 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1122
1123 SkMD5::Digest d1, d2;
1124 md5(bm1, &d1);
1125 md5(bm2, &d2);
1126 REPORTER_ASSERT(r, d1 == d2);
1127}
1128
1129DEF_TEST(Codec_PngRoundTrip, r) {
msarett549ca322016-08-17 08:54:08 -07001130 const char* path = "mandrill_512_q075.jpg";
Ben Wagner145dbcd2016-11-03 14:40:50 -04001131 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
1132 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett549ca322016-08-17 08:54:08 -07001133
msarettf17b71f2016-09-12 14:30:03 -07001134 SkColorType colorTypesOpaque[] = {
1135 kRGB_565_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1136 };
1137 for (SkColorType colorType : colorTypesOpaque) {
1138 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType);
1139 check_round_trip(r, codec.get(), newInfo);
1140 }
1141
msarett9b09cd82016-08-29 14:47:49 -07001142 path = "grayscale.jpg";
bungemanf93d7112016-09-16 06:24:20 -07001143 stream.reset(GetResourceAsStream(path));
msarett9b09cd82016-08-29 14:47:49 -07001144 codec.reset(SkCodec::NewFromStream(stream.release()));
msarettf17b71f2016-09-12 14:30:03 -07001145 check_round_trip(r, codec.get(), codec->getInfo());
1146
1147 path = "yellow_rose.png";
bungemanf93d7112016-09-16 06:24:20 -07001148 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001149 codec.reset(SkCodec::NewFromStream(stream.release()));
1150
1151 SkColorType colorTypesWithAlpha[] = {
1152 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1153 };
1154 SkAlphaType alphaTypes[] = {
1155 kUnpremul_SkAlphaType, kPremul_SkAlphaType
1156 };
1157 for (SkColorType colorType : colorTypesWithAlpha) {
1158 for (SkAlphaType alphaType : alphaTypes) {
1159 // Set color space to nullptr because color correct premultiplies do not round trip.
1160 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType)
1161 .makeAlphaType(alphaType)
1162 .makeColorSpace(nullptr);
1163 check_round_trip(r, codec.get(), newInfo);
1164 }
1165 }
1166
1167 path = "index8.png";
bungemanf93d7112016-09-16 06:24:20 -07001168 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001169 codec.reset(SkCodec::NewFromStream(stream.release()));
1170
1171 for (SkAlphaType alphaType : alphaTypes) {
1172 SkImageInfo newInfo = codec->getInfo().makeAlphaType(alphaType)
1173 .makeColorSpace(nullptr);
1174 check_round_trip(r, codec.get(), newInfo);
1175 }
msarett549ca322016-08-17 08:54:08 -07001176}
msarett2ecc35f2016-09-08 11:55:16 -07001177
1178static void test_conversion_possible(skiatest::Reporter* r, const char* path,
scroggo8e6c7ad2016-09-16 08:20:38 -07001179 bool supportsScanlineDecoder,
1180 bool supportsIncrementalDecoder) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001181 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
1182 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett2ecc35f2016-09-08 11:55:16 -07001183 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType);
1184
1185 SkBitmap bm;
1186 bm.allocPixels(infoF16);
1187 SkCodec::Result result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1188 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001189
1190 result = codec->startScanlineDecode(infoF16);
1191 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001192 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001193 } else {
1194 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1195 }
1196
1197 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1198 if (supportsIncrementalDecoder) {
1199 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
1200 } else {
1201 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001202 }
1203
raftias94888332016-10-18 10:02:51 -07001204 SkASSERT(SkColorSpace_Base::Type::kXYZ == as_CSB(infoF16.colorSpace())->type());
1205 SkColorSpace_XYZ* csXYZ = static_cast<SkColorSpace_XYZ*>(infoF16.colorSpace());
1206 infoF16 = infoF16.makeColorSpace(csXYZ->makeLinearGamma());
msarett2ecc35f2016-09-08 11:55:16 -07001207 result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1208 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001209 result = codec->startScanlineDecode(infoF16);
1210 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001211 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001212 } else {
1213 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1214 }
1215
1216 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1217 if (supportsIncrementalDecoder) {
1218 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1219 } else {
1220 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001221 }
1222}
1223
1224DEF_TEST(Codec_F16ConversionPossible, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -07001225 test_conversion_possible(r, "color_wheel.webp", false, false);
1226 test_conversion_possible(r, "mandrill_512_q075.jpg", true, false);
1227 test_conversion_possible(r, "yellow_rose.png", false, true);
1228}
1229
scroggo19b91532016-10-24 09:03:26 -07001230static void decode_frame(skiatest::Reporter* r, SkCodec* codec, size_t frame) {
1231 SkBitmap bm;
1232 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1233 bm.allocPixels(info);
1234
1235 SkCodec::Options opts;
1236 opts.fFrameIndex = frame;
1237 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->getPixels(info,
1238 bm.getPixels(), bm.rowBytes(), &opts, nullptr, nullptr));
1239}
1240
1241// For an animated image, we should only read enough to decode the requested
1242// frame if the client never calls getFrameInfo.
1243DEF_TEST(Codec_skipFullParse, r) {
1244 auto path = "test640x479.gif";
1245 SkStream* stream(GetResourceAsStream(path));
1246 if (!stream) {
1247 return;
1248 }
1249
1250 // Note that we cheat and hold on to the stream pointer, but SkCodec will
1251 // take ownership. We will not refer to the stream after the SkCodec
1252 // deletes it.
1253 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
1254 if (!codec) {
1255 ERRORF(r, "Failed to create codec for %s", path);
1256 return;
1257 }
1258
1259 REPORTER_ASSERT(r, stream->hasPosition());
1260 const size_t sizePosition = stream->getPosition();
1261 REPORTER_ASSERT(r, stream->hasLength() && sizePosition < stream->getLength());
1262
1263 // This should read more of the stream, but not the whole stream.
1264 decode_frame(r, codec.get(), 0);
1265 const size_t positionAfterFirstFrame = stream->getPosition();
1266 REPORTER_ASSERT(r, positionAfterFirstFrame > sizePosition
1267 && positionAfterFirstFrame < stream->getLength());
1268
1269 // Again, this should read more of the stream.
1270 decode_frame(r, codec.get(), 2);
1271 const size_t positionAfterThirdFrame = stream->getPosition();
1272 REPORTER_ASSERT(r, positionAfterThirdFrame > positionAfterFirstFrame
1273 && positionAfterThirdFrame < stream->getLength());
1274
1275 // This does not need to read any more of the stream, since it has already
1276 // parsed the second frame.
1277 decode_frame(r, codec.get(), 1);
1278 REPORTER_ASSERT(r, stream->getPosition() == positionAfterThirdFrame);
1279
1280 // This should read the rest of the frames.
1281 decode_frame(r, codec.get(), 3);
1282 const size_t finalPosition = stream->getPosition();
1283 REPORTER_ASSERT(r, finalPosition > positionAfterThirdFrame);
1284
1285 // There may be more data in the stream.
1286 auto frameInfo = codec->getFrameInfo();
1287 REPORTER_ASSERT(r, frameInfo.size() == 4);
1288 REPORTER_ASSERT(r, stream->getPosition() >= finalPosition);
1289}
1290
scroggo8e6c7ad2016-09-16 08:20:38 -07001291// Only rewinds up to a limit.
1292class LimitedRewindingStream : public SkStream {
1293public:
1294 static SkStream* Make(const char path[], size_t limit) {
1295 SkStream* stream = GetResourceAsStream(path);
1296 if (!stream) {
1297 return nullptr;
1298 }
1299 return new LimitedRewindingStream(stream, limit);
1300 }
1301
1302 size_t read(void* buffer, size_t size) override {
1303 const size_t bytes = fStream->read(buffer, size);
1304 fPosition += bytes;
1305 return bytes;
1306 }
1307
1308 bool isAtEnd() const override {
1309 return fStream->isAtEnd();
1310 }
1311
1312 bool rewind() override {
1313 if (fPosition <= fLimit && fStream->rewind()) {
1314 fPosition = 0;
1315 return true;
1316 }
1317
1318 return false;
1319 }
1320
1321private:
Ben Wagner145dbcd2016-11-03 14:40:50 -04001322 std::unique_ptr<SkStream> fStream;
1323 const size_t fLimit;
1324 size_t fPosition;
scroggo8e6c7ad2016-09-16 08:20:38 -07001325
1326 LimitedRewindingStream(SkStream* stream, size_t limit)
1327 : fStream(stream)
1328 , fLimit(limit)
1329 , fPosition(0)
1330 {
1331 SkASSERT(fStream);
1332 }
1333};
1334
1335DEF_TEST(Codec_fallBack, r) {
1336 // SkAndroidCodec needs to be able to fall back to scanline decoding
1337 // if incremental decoding does not work. Make sure this does not
1338 // require a rewind.
1339
1340 // Formats that currently do not support incremental decoding
1341 auto files = {
scroggo8e6c7ad2016-09-16 08:20:38 -07001342 "CMYK.jpg",
1343 "color_wheel.ico",
1344 "mandrill.wbmp",
1345 "randPixels.bmp",
1346 };
1347 for (auto file : files) {
1348 SkStream* stream = LimitedRewindingStream::Make(file, 14);
1349 if (!stream) {
1350 SkDebugf("Missing resources (%s). Set --resourcePath.\n", file);
1351 return;
1352 }
1353
Ben Wagner145dbcd2016-11-03 14:40:50 -04001354 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
scroggo8e6c7ad2016-09-16 08:20:38 -07001355 if (!codec) {
1356 ERRORF(r, "Failed to create codec for %s,", file);
1357 continue;
1358 }
1359
1360 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
1361 SkBitmap bm;
1362 bm.allocPixels(info);
1363
1364 if (SkCodec::kUnimplemented != codec->startIncrementalDecode(info, bm.getPixels(),
1365 bm.rowBytes())) {
1366 ERRORF(r, "Is scanline decoding now implemented for %s?", file);
1367 continue;
1368 }
1369
1370 // Scanline decoding should not require a rewind.
1371 SkCodec::Result result = codec->startScanlineDecode(info);
1372 if (SkCodec::kSuccess != result) {
1373 ERRORF(r, "Scanline decoding failed for %s with %i", file, result);
1374 }
1375 }
msarett2ecc35f2016-09-08 11:55:16 -07001376}
scroggoc46cdd42016-10-10 06:45:32 -07001377
1378// This test verifies that we fixed an assert statement that fired when reusing a png codec
1379// after scaling.
1380DEF_TEST(Codec_reusePng, r) {
1381 std::unique_ptr<SkStream> stream(GetResourceAsStream("plane.png"));
1382 if (!stream) {
1383 return;
1384 }
1385
1386 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
1387 if (!codec) {
1388 ERRORF(r, "Failed to create codec\n");
1389 return;
1390 }
1391
1392 SkAndroidCodec::AndroidOptions opts;
1393 opts.fSampleSize = 5;
1394 auto size = codec->getSampledDimensions(opts.fSampleSize);
1395 auto info = codec->getInfo().makeWH(size.fWidth, size.fHeight).makeColorType(kN32_SkColorType);
1396 SkBitmap bm;
1397 bm.allocPixels(info);
1398 auto result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1399 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1400
1401 info = codec->getInfo().makeColorType(kN32_SkColorType);
1402 bm.allocPixels(info);
1403 opts.fSampleSize = 1;
1404 result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1405 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1406}
scroggoe61b3b42016-10-10 07:17:32 -07001407
1408DEF_TEST(Codec_rowsDecoded, r) {
1409 auto file = "plane_interlaced.png";
1410 std::unique_ptr<SkStream> stream(GetResourceAsStream(file));
1411 if (!stream) {
1412 return;
1413 }
1414
1415 // This is enough to read the header etc, but no rows.
1416 auto data = SkData::MakeFromStream(stream.get(), 99);
1417 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
1418 if (!codec) {
1419 ERRORF(r, "Failed to create codec\n");
1420 return;
1421 }
1422
1423 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1424 SkBitmap bm;
1425 bm.allocPixels(info);
1426 auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
1427 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1428
1429 // This is an arbitrary value. The important fact is that it is not zero, and rowsDecoded
1430 // should get set to zero by incrementalDecode.
1431 int rowsDecoded = 77;
1432 result = codec->incrementalDecode(&rowsDecoded);
1433 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput);
1434 REPORTER_ASSERT(r, rowsDecoded == 0);
1435}
Matt Sarett29121eb2016-10-17 14:32:46 -04001436
Matt Sarett8a4e9c52016-10-25 14:24:50 -04001437static void test_invalid_images(skiatest::Reporter* r, const char* path, bool shouldSucceed) {
Matt Sarett29121eb2016-10-17 14:32:46 -04001438 SkBitmap bitmap;
Matt Sarett8a4e9c52016-10-25 14:24:50 -04001439 const bool success = GetResourceAsBitmap(path, &bitmap);
1440 REPORTER_ASSERT(r, success == shouldSucceed);
1441}
1442
1443DEF_TEST(Codec_InvalidImages, r) {
1444 // ASAN will complain if there is an issue.
1445 test_invalid_images(r, "invalid_images/int_overflow.ico", false);
1446 test_invalid_images(r, "invalid_images/skbug5887.gif", true);
Matt Sarettdbdf6d22016-11-08 15:26:56 -05001447 test_invalid_images(r, "invalid_images/many-progressive-scans.jpg", false);
Matt Sarett29121eb2016-10-17 14:32:46 -04001448}