blob: 01e9cc4d45308cd5a96bdd0a73ca406baea868c6 [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
Leon Scroggins III932efed2016-12-16 11:39:51 -05008#include "FakeStreams.h"
halcanarya096d7a2015-03-27 12:16:53 -07009#include "Resources.h"
msarett3d9d7a72015-10-21 10:27:10 -070010#include "SkAndroidCodec.h"
halcanarya096d7a2015-03-27 12:16:53 -070011#include "SkBitmap.h"
12#include "SkCodec.h"
msarettedd2dcf2016-01-14 13:12:26 -080013#include "SkCodecImageGenerator.h"
raftias94888332016-10-18 10:02:51 -070014#include "SkColorSpace_XYZ.h"
msarette6dd0042015-10-09 11:07:34 -070015#include "SkData.h"
Hal Canarya5494f12017-01-10 15:02:26 -050016#include "SkImageEncoder.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000017#include "SkFrontBufferedStream.h"
halcanarya096d7a2015-03-27 12:16:53 -070018#include "SkMD5.h"
scroggob636b452015-07-22 07:16:20 -070019#include "SkRandom.h"
scroggocf98fa92015-11-23 08:14:40 -080020#include "SkStream.h"
scroggob9a1e342015-11-30 06:25:31 -080021#include "SkStreamPriv.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000022#include "SkPngChunkReader.h"
halcanarya096d7a2015-03-27 12:16:53 -070023#include "Test.h"
24
scroggocf98fa92015-11-23 08:14:40 -080025#include "png.h"
26
Hal Canarydb683012016-11-23 08:55:18 -070027#include "sk_tool_utils.h"
28
scroggo8e6c7ad2016-09-16 08:20:38 -070029#if PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 5
30 // FIXME (scroggo): Google3 needs to be updated to use a newer version of libpng. In
31 // the meantime, we had to break some pieces of SkPngCodec in order to support Google3.
32 // The parts that are broken are likely not used by Google3.
33 #define SK_PNG_DISABLE_TESTS
34#endif
35
halcanarya096d7a2015-03-27 12:16:53 -070036static void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
37 SkAutoLockPixels autoLockPixels(bm);
38 SkASSERT(bm.getPixels());
39 SkMD5 md5;
40 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
41 for (int y = 0; y < bm.height(); ++y) {
halcanary1e903042016-04-25 10:29:36 -070042 md5.write(bm.getAddr(0, y), rowLen);
halcanarya096d7a2015-03-27 12:16:53 -070043 }
44 md5.finish(*digest);
45}
46
scroggo9b2cdbf42015-07-10 12:07:02 -070047/**
48 * Compute the digest for bm and compare it to a known good digest.
49 * @param r Reporter to assert that bm's digest matches goodDigest.
50 * @param goodDigest The known good digest to compare to.
51 * @param bm The bitmap to test.
52 */
53static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
54 const SkBitmap& bm) {
55 SkMD5::Digest digest;
56 md5(bm, &digest);
57 REPORTER_ASSERT(r, digest == goodDigest);
58}
59
scroggod1bc5742015-08-12 08:31:44 -070060/**
61 * Test decoding an SkCodec to a particular SkImageInfo.
62 *
halcanary96fcdcc2015-08-27 07:41:13 -070063 * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr,
scroggod1bc5742015-08-12 08:31:44 -070064 * the resulting decode should match.
65 */
scroggo7b5e5532016-02-04 06:14:24 -080066template<typename Codec>
67static void test_info(skiatest::Reporter* r, Codec* codec, const SkImageInfo& info,
scroggod1bc5742015-08-12 08:31:44 -070068 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
69 SkBitmap bm;
70 bm.allocPixels(info);
71 SkAutoLockPixels autoLockPixels(bm);
72
73 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
74 REPORTER_ASSERT(r, result == expectedResult);
75
76 if (goodDigest) {
77 compare_to_good_digest(r, *goodDigest, bm);
78 }
79}
80
scroggob636b452015-07-22 07:16:20 -070081SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
82 SkIRect rect;
83 do {
84 rect.fLeft = rand->nextRangeU(0, w);
85 rect.fTop = rand->nextRangeU(0, h);
86 rect.fRight = rand->nextRangeU(0, w);
87 rect.fBottom = rand->nextRangeU(0, h);
88 rect.sort();
89 } while (rect.isEmpty());
90 return rect;
91}
92
scroggo8e6c7ad2016-09-16 08:20:38 -070093static void test_incremental_decode(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
94 const SkMD5::Digest& goodDigest) {
95 SkBitmap bm;
96 bm.allocPixels(info);
97 SkAutoLockPixels autoLockPixels(bm);
98
99 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->startIncrementalDecode(info, bm.getPixels(),
100 bm.rowBytes()));
101
102 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->incrementalDecode());
103
104 compare_to_good_digest(r, goodDigest, bm);
105}
106
107// Test in stripes, similar to DM's kStripe_Mode
108static void test_in_stripes(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
109 const SkMD5::Digest& goodDigest) {
110 SkBitmap bm;
111 bm.allocPixels(info);
112 bm.eraseColor(SK_ColorYELLOW);
113
114 const int height = info.height();
115 // Note that if numStripes does not evenly divide height there will be an extra
116 // stripe.
117 const int numStripes = 4;
118
119 if (numStripes > height) {
120 // Image is too small.
121 return;
122 }
123
124 const int stripeHeight = height / numStripes;
125
126 // Iterate through the image twice. Once to decode odd stripes, and once for even.
127 for (int oddEven = 1; oddEven >= 0; oddEven--) {
128 for (int y = oddEven * stripeHeight; y < height; y += 2 * stripeHeight) {
129 SkIRect subset = SkIRect::MakeLTRB(0, y, info.width(),
130 SkTMin(y + stripeHeight, height));
131 SkCodec::Options options;
132 options.fSubset = &subset;
133 if (SkCodec::kSuccess != codec->startIncrementalDecode(info, bm.getAddr(0, y),
134 bm.rowBytes(), &options)) {
135 ERRORF(r, "failed to start incremental decode!\ttop: %i\tbottom%i\n",
136 subset.top(), subset.bottom());
137 return;
138 }
139 if (SkCodec::kSuccess != codec->incrementalDecode()) {
140 ERRORF(r, "failed incremental decode starting from line %i\n", y);
141 return;
142 }
143 }
144 }
145
146 compare_to_good_digest(r, goodDigest, bm);
147}
148
scroggo7b5e5532016-02-04 06:14:24 -0800149template<typename Codec>
150static void test_codec(skiatest::Reporter* r, Codec* codec, SkBitmap& bm, const SkImageInfo& info,
scroggo27c17282015-10-27 08:14:46 -0700151 const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest,
152 const SkMD5::Digest* goodDigest) {
msarette6dd0042015-10-09 11:07:34 -0700153
halcanarya096d7a2015-03-27 12:16:53 -0700154 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -0700155 bm.allocPixels(info);
156 SkAutoLockPixels autoLockPixels(bm);
msarettcc7f3052015-10-05 14:20:27 -0700157
158 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700159 REPORTER_ASSERT(r, result == expectedResult);
halcanarya096d7a2015-03-27 12:16:53 -0700160
msarettcc7f3052015-10-05 14:20:27 -0700161 md5(bm, digest);
162 if (goodDigest) {
163 REPORTER_ASSERT(r, *digest == *goodDigest);
164 }
halcanarya096d7a2015-03-27 12:16:53 -0700165
msarett8ff6ca62015-09-18 12:06:04 -0700166 {
167 // Test decoding to 565
168 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggoba584892016-05-20 13:56:13 -0700169 if (info.alphaType() == kOpaque_SkAlphaType) {
170 // Decoding to 565 should succeed.
171 SkBitmap bm565;
172 bm565.allocPixels(info565);
173 SkAutoLockPixels alp(bm565);
174
175 // This will allow comparison even if the image is incomplete.
176 bm565.eraseColor(SK_ColorBLACK);
177
178 REPORTER_ASSERT(r, expectedResult == codec->getPixels(info565,
179 bm565.getPixels(), bm565.rowBytes()));
180
181 SkMD5::Digest digest565;
182 md5(bm565, &digest565);
183
184 // A dumb client's request for non-opaque should also succeed.
185 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
186 info565 = info565.makeAlphaType(alpha);
187 test_info(r, codec, info565, expectedResult, &digest565);
188 }
189 } else {
190 test_info(r, codec, info565, SkCodec::kInvalidConversion, nullptr);
191 }
192 }
193
194 if (codec->getInfo().colorType() == kGray_8_SkColorType) {
195 SkImageInfo grayInfo = codec->getInfo();
196 SkBitmap grayBm;
197 grayBm.allocPixels(grayInfo);
198 SkAutoLockPixels alp(grayBm);
199
200 grayBm.eraseColor(SK_ColorBLACK);
201
202 REPORTER_ASSERT(r, expectedResult == codec->getPixels(grayInfo,
203 grayBm.getPixels(), grayBm.rowBytes()));
204
205 SkMD5::Digest grayDigest;
206 md5(grayBm, &grayDigest);
207
208 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
209 grayInfo = grayInfo.makeAlphaType(alpha);
210 test_info(r, codec, grayInfo, expectedResult, &grayDigest);
211 }
msarett8ff6ca62015-09-18 12:06:04 -0700212 }
213
214 // Verify that re-decoding gives the same result. It is interesting to check this after
215 // a decode to 565, since choosing to decode to 565 may result in some of the decode
216 // options being modified. These options should return to their defaults on another
217 // decode to kN32, so the new digest should match the old digest.
msarette6dd0042015-10-09 11:07:34 -0700218 test_info(r, codec, info, expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700219
220 {
221 // Check alpha type conversions
222 if (info.alphaType() == kOpaque_SkAlphaType) {
223 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800224 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700225 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800226 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700227 } else {
228 // Decoding to opaque should fail
229 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700230 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700231 SkAlphaType otherAt = info.alphaType();
232 if (kPremul_SkAlphaType == otherAt) {
233 otherAt = kUnpremul_SkAlphaType;
234 } else {
235 otherAt = kPremul_SkAlphaType;
236 }
237 // The other non-opaque alpha type should always succeed, but not match.
msarette6dd0042015-10-09 11:07:34 -0700238 test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700239 }
240 }
msarettcc7f3052015-10-05 14:20:27 -0700241}
242
scroggobed1ed62016-02-11 10:24:55 -0800243static bool supports_partial_scanlines(const char path[]) {
scroggo2c3b2182015-10-09 08:40:59 -0700244 static const char* const exts[] = {
245 "jpg", "jpeg", "png", "webp"
246 "JPG", "JPEG", "PNG", "WEBP"
247 };
248
249 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
250 if (SkStrEndsWith(path, exts[i])) {
251 return true;
252 }
253 }
254 return false;
255}
256
scroggo8e6c7ad2016-09-16 08:20:38 -0700257// FIXME: Break up this giant function
msarettcc7f3052015-10-05 14:20:27 -0700258static void check(skiatest::Reporter* r,
259 const char path[],
260 SkISize size,
261 bool supportsScanlineDecoding,
262 bool supportsSubsetDecoding,
scroggo8e6c7ad2016-09-16 08:20:38 -0700263 bool supportsIncomplete,
264 bool supportsNewScanlineDecoding = false) {
msarettcc7f3052015-10-05 14:20:27 -0700265
Ben Wagner145dbcd2016-11-03 14:40:50 -0400266 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarettcc7f3052015-10-05 14:20:27 -0700267 if (!stream) {
msarettcc7f3052015-10-05 14:20:27 -0700268 return;
269 }
msarette6dd0042015-10-09 11:07:34 -0700270
Ben Wagner145dbcd2016-11-03 14:40:50 -0400271 std::unique_ptr<SkCodec> codec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700272 bool isIncomplete = supportsIncomplete;
273 if (isIncomplete) {
274 size_t size = stream->getLength();
Ben Wagner145dbcd2016-11-03 14:40:50 -0400275 sk_sp<SkData> data((SkData::MakeFromStream(stream.get(), 2 * size / 3)));
reed42943c82016-09-12 12:01:44 -0700276 codec.reset(SkCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700277 } else {
mtklein18300a32016-03-16 13:53:35 -0700278 codec.reset(SkCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700279 }
msarettcc7f3052015-10-05 14:20:27 -0700280 if (!codec) {
281 ERRORF(r, "Unable to decode '%s'", path);
282 return;
283 }
284
285 // Test full image decodes with SkCodec
286 SkMD5::Digest codecDigest;
scroggoef0fed32016-02-18 05:59:25 -0800287 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
msarettcc7f3052015-10-05 14:20:27 -0700288 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700289 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
scroggo7b5e5532016-02-04 06:14:24 -0800290 test_codec(r, codec.get(), bm, info, size, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700291
292 // Scanline decoding follows.
scroggod8d68552016-06-06 11:26:17 -0700293
scroggo8e6c7ad2016-09-16 08:20:38 -0700294 if (supportsNewScanlineDecoding && !isIncomplete) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400295 test_incremental_decode(r, codec.get(), info, codecDigest);
scroggo19b91532016-10-24 09:03:26 -0700296 // This is only supported by codecs that use incremental decoding to
297 // support subset decodes - png and jpeg (once SkJpegCodec is
298 // converted).
299 if (SkStrEndsWith(path, "png") || SkStrEndsWith(path, "PNG")) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400300 test_in_stripes(r, codec.get(), info, codecDigest);
scroggo19b91532016-10-24 09:03:26 -0700301 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700302 }
303
304 // Need to call startScanlineDecode() first.
305 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) == 0);
306 REPORTER_ASSERT(r, !codec->skipScanlines(1));
scroggo46c57472015-09-30 08:57:13 -0700307 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700308 if (supportsScanlineDecoding) {
309 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700310
scroggo46c57472015-09-30 08:57:13 -0700311 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700312
scroggo58421542015-04-01 11:25:20 -0700313 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700314 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
315 if (!isIncomplete) {
316 REPORTER_ASSERT(r, 1 == lines);
317 }
scroggo58421542015-04-01 11:25:20 -0700318 }
319 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700320 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700321 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700322 }
scroggo46c57472015-09-30 08:57:13 -0700323
324 // Cannot continue to decode scanlines beyond the end
325 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700326 == 0);
scroggo46c57472015-09-30 08:57:13 -0700327
328 // Interrupting a scanline decode with a full decode starts from
329 // scratch
330 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700331 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
332 if (!isIncomplete) {
333 REPORTER_ASSERT(r, lines == 1);
334 }
scroggo46c57472015-09-30 08:57:13 -0700335 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700336 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700337 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700338 == 0);
scroggo46c57472015-09-30 08:57:13 -0700339 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700340 == 0);
msarett80803ff2015-10-16 10:54:12 -0700341
342 // Test partial scanline decodes
scroggobed1ed62016-02-11 10:24:55 -0800343 if (supports_partial_scanlines(path) && info.width() >= 3) {
msarett80803ff2015-10-16 10:54:12 -0700344 SkCodec::Options options;
345 int width = info.width();
346 int height = info.height();
347 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
348 options.fSubset = &subset;
349
350 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
351 nullptr, nullptr);
352 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
353
354 for (int y = 0; y < height; y++) {
355 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
356 if (!isIncomplete) {
357 REPORTER_ASSERT(r, 1 == lines);
358 }
359 }
360 }
scroggo58421542015-04-01 11:25:20 -0700361 } else {
scroggo46c57472015-09-30 08:57:13 -0700362 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700363 }
scroggob636b452015-07-22 07:16:20 -0700364
365 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
366 // random subsets.
367 // Do not attempt to decode subsets of an image of only once pixel, since there is no
368 // meaningful subset.
369 if (size.width() * size.height() == 1) {
370 return;
371 }
372
373 SkRandom rand;
374 SkIRect subset;
375 SkCodec::Options opts;
376 opts.fSubset = &subset;
377 for (int i = 0; i < 5; i++) {
378 subset = generate_random_subset(&rand, size.width(), size.height());
379 SkASSERT(!subset.isEmpty());
380 const bool supported = codec->getValidSubset(&subset);
381 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
382
383 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
384 SkBitmap bm;
385 bm.allocPixels(subsetInfo);
386 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700387 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700388
389 if (supportsSubsetDecoding) {
Leon Scroggins III58f100c2016-12-20 09:49:25 -0500390 if (expectedResult == SkCodec::kSuccess) {
391 REPORTER_ASSERT(r, result == expectedResult);
392 } else {
393 SkASSERT(expectedResult == SkCodec::kIncompleteInput);
394 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput
395 || result == SkCodec::kSuccess);
396 }
scroggob636b452015-07-22 07:16:20 -0700397 // Webp is the only codec that supports subsets, and it will have modified the subset
398 // to have even left/top.
399 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
400 } else {
401 // No subsets will work.
402 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
403 }
404 }
msarettcc7f3052015-10-05 14:20:27 -0700405
scroggobed1ed62016-02-11 10:24:55 -0800406 // SkAndroidCodec tests
scroggo8e6c7ad2016-09-16 08:20:38 -0700407 if (supportsScanlineDecoding || supportsSubsetDecoding || supportsNewScanlineDecoding) {
scroggo2c3b2182015-10-09 08:40:59 -0700408
Ben Wagner145dbcd2016-11-03 14:40:50 -0400409 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarettcc7f3052015-10-05 14:20:27 -0700410 if (!stream) {
msarettcc7f3052015-10-05 14:20:27 -0700411 return;
412 }
msarette6dd0042015-10-09 11:07:34 -0700413
Ben Wagner145dbcd2016-11-03 14:40:50 -0400414 std::unique_ptr<SkAndroidCodec> androidCodec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700415 if (isIncomplete) {
416 size_t size = stream->getLength();
Ben Wagner145dbcd2016-11-03 14:40:50 -0400417 sk_sp<SkData> data((SkData::MakeFromStream(stream.get(), 2 * size / 3)));
reed42943c82016-09-12 12:01:44 -0700418 androidCodec.reset(SkAndroidCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700419 } else {
mtklein18300a32016-03-16 13:53:35 -0700420 androidCodec.reset(SkAndroidCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700421 }
scroggo7b5e5532016-02-04 06:14:24 -0800422 if (!androidCodec) {
msarettcc7f3052015-10-05 14:20:27 -0700423 ERRORF(r, "Unable to decode '%s'", path);
424 return;
425 }
426
427 SkBitmap bm;
scroggobed1ed62016-02-11 10:24:55 -0800428 SkMD5::Digest androidCodecDigest;
429 test_codec(r, androidCodec.get(), bm, info, size, expectedResult, &androidCodecDigest,
scroggo7b5e5532016-02-04 06:14:24 -0800430 &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700431 }
432
msarettedd2dcf2016-01-14 13:12:26 -0800433 if (!isIncomplete) {
scroggoef0fed32016-02-18 05:59:25 -0800434 // Test SkCodecImageGenerator
Ben Wagner145dbcd2016-11-03 14:40:50 -0400435 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
436 sk_sp<SkData> fullData(SkData::MakeFromStream(stream.get(), stream->getLength()));
437 std::unique_ptr<SkImageGenerator> gen(
bungeman38d909e2016-08-02 14:40:46 -0700438 SkCodecImageGenerator::NewFromEncodedCodec(fullData.get()));
msarettedd2dcf2016-01-14 13:12:26 -0800439 SkBitmap bm;
440 bm.allocPixels(info);
441 SkAutoLockPixels autoLockPixels(bm);
442 REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes()));
443 compare_to_good_digest(r, codecDigest, bm);
scroggoef0fed32016-02-18 05:59:25 -0800444
scroggo8e6c7ad2016-09-16 08:20:38 -0700445#ifndef SK_PNG_DISABLE_TESTS
scroggod8d68552016-06-06 11:26:17 -0700446 // Test using SkFrontBufferedStream, as Android does
bungeman38d909e2016-08-02 14:40:46 -0700447 SkStream* bufferedStream = SkFrontBufferedStream::Create(
448 new SkMemoryStream(std::move(fullData)), SkCodec::MinBufferedBytesNeeded());
scroggod8d68552016-06-06 11:26:17 -0700449 REPORTER_ASSERT(r, bufferedStream);
450 codec.reset(SkCodec::NewFromStream(bufferedStream));
451 REPORTER_ASSERT(r, codec);
452 if (codec) {
453 test_info(r, codec.get(), info, SkCodec::kSuccess, &codecDigest);
scroggoef0fed32016-02-18 05:59:25 -0800454 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700455#endif
msarettedd2dcf2016-01-14 13:12:26 -0800456 }
457
msarette6dd0042015-10-09 11:07:34 -0700458 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
459 if (isIncomplete) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700460 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false,
461 supportsNewScanlineDecoding);
msarettcc7f3052015-10-05 14:20:27 -0700462 }
halcanarya096d7a2015-03-27 12:16:53 -0700463}
464
Leon Scroggins III83926342016-12-06 10:58:02 -0500465DEF_TEST(Codec_wbmp, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700466 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500467}
halcanarya096d7a2015-03-27 12:16:53 -0700468
Leon Scroggins III83926342016-12-06 10:58:02 -0500469DEF_TEST(Codec_webp, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700470 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true, true);
471 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true, true);
472 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500473}
scroggo6f5e6192015-06-18 12:53:43 -0700474
Leon Scroggins III83926342016-12-06 10:58:02 -0500475DEF_TEST(Codec_bmp, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700476 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false, true);
477 check(r, "rle.bmp", SkISize::Make(320, 240), true, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500478}
halcanarya096d7a2015-03-27 12:16:53 -0700479
Leon Scroggins III83926342016-12-06 10:58:02 -0500480DEF_TEST(Codec_ico, r) {
msarette6dd0042015-10-09 11:07:34 -0700481 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700482 // These two tests examine interestingly different behavior:
483 // Decodes an embedded BMP image
msarettbe8216a2015-12-04 08:00:50 -0800484 check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700485 // Decodes an embedded PNG image
scroggo8e6c7ad2016-09-16 08:20:38 -0700486 check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500487}
halcanarya096d7a2015-03-27 12:16:53 -0700488
Leon Scroggins III83926342016-12-06 10:58:02 -0500489DEF_TEST(Codec_gif, r) {
scroggo19b91532016-10-24 09:03:26 -0700490 check(r, "box.gif", SkISize::Make(200, 55), false, false, true, true);
491 check(r, "color_wheel.gif", SkISize::Make(128, 128), false, false, true, true);
msarette6dd0042015-10-09 11:07:34 -0700492 // randPixels.gif is too small to test incomplete
scroggo19b91532016-10-24 09:03:26 -0700493 check(r, "randPixels.gif", SkISize::Make(8, 8), false, false, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500494}
msarett438b2ad2015-04-09 12:43:10 -0700495
Leon Scroggins III83926342016-12-06 10:58:02 -0500496DEF_TEST(Codec_jpg, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700497 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false, true);
498 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700499 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700500 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggo8e6c7ad2016-09-16 08:20:38 -0700501 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700502 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700503 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
Leon Scroggins III83926342016-12-06 10:58:02 -0500504}
msarette16b04a2015-04-15 07:32:19 -0700505
Leon Scroggins III83926342016-12-06 10:58:02 -0500506DEF_TEST(Codec_png, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700507 check(r, "arrow.png", SkISize::Make(187, 312), false, false, true, true);
508 check(r, "baby_tux.png", SkISize::Make(240, 246), false, false, true, true);
509 check(r, "color_wheel.png", SkISize::Make(128, 128), false, false, true, true);
510 // half-transparent-white-pixel.png is too small to test incomplete
511 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), false, false, false, true);
512 check(r, "mandrill_128.png", SkISize::Make(128, 128), false, false, true, true);
513 check(r, "mandrill_16.png", SkISize::Make(16, 16), false, false, true, true);
514 check(r, "mandrill_256.png", SkISize::Make(256, 256), false, false, true, true);
515 check(r, "mandrill_32.png", SkISize::Make(32, 32), false, false, true, true);
516 check(r, "mandrill_512.png", SkISize::Make(512, 512), false, false, true, true);
517 check(r, "mandrill_64.png", SkISize::Make(64, 64), false, false, true, true);
518 check(r, "plane.png", SkISize::Make(250, 126), false, false, true, true);
519 check(r, "plane_interlaced.png", SkISize::Make(250, 126), false, false, true, true);
520 check(r, "randPixels.png", SkISize::Make(8, 8), false, false, true, true);
521 check(r, "yellow_rose.png", SkISize::Make(400, 301), false, false, true, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500522}
yujieqin916de9f2016-01-25 08:26:16 -0800523
yujieqinf236ee42016-02-29 07:14:42 -0800524// Disable RAW tests for Win32.
525#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Leon Scroggins III83926342016-12-06 10:58:02 -0500526DEF_TEST(Codec_raw, r) {
yujieqin916de9f2016-01-25 08:26:16 -0800527 check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
ebrauer46d2aa82016-02-17 08:04:00 -0800528 check(r, "sample_1mp_rotated.dng", SkISize::Make(600, 338), false, false, false);
yujieqin9c7a8a42016-02-05 08:21:19 -0800529 check(r, "dng_with_preview.dng", SkISize::Make(600, 338), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700530}
Leon Scroggins III83926342016-12-06 10:58:02 -0500531#endif
scroggo0a7e69c2015-04-03 07:22:22 -0700532
533static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700534 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700535 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700536 REPORTER_ASSERT(r, !codec);
537
msarett3d9d7a72015-10-21 10:27:10 -0700538 SkAndroidCodec* androidCodec =
539 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
540 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700541}
542
543// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
544// even on failure. Test some bad streams.
545DEF_TEST(Codec_leaks, r) {
546 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
547 const char nonSupportedStream[] = "hello world";
548 // The other strings should look like the beginning of a file type, so we'll call some
549 // internal version of NewFromStream, which must also delete the stream on failure.
550 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
551 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
552 const char emptyWebp[] = "RIFF1234WEBPVP";
553 const char emptyBmp[] = { 'B', 'M' };
554 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
555 const char emptyGif[] = "GIFVER";
556
557 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
558 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
559 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
560 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
561 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
562 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
563 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
564}
msarette16b04a2015-04-15 07:32:19 -0700565
scroggo2c3b2182015-10-09 08:40:59 -0700566DEF_TEST(Codec_null, r) {
scroggobed1ed62016-02-11 10:24:55 -0800567 // Attempting to create an SkCodec or an SkAndroidCodec with null should not
scroggo2c3b2182015-10-09 08:40:59 -0700568 // crash.
569 SkCodec* codec = SkCodec::NewFromStream(nullptr);
570 REPORTER_ASSERT(r, !codec);
571
msarett3d9d7a72015-10-21 10:27:10 -0700572 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
573 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700574}
575
msarette16b04a2015-04-15 07:32:19 -0700576static void test_dimensions(skiatest::Reporter* r, const char path[]) {
577 // Create the codec from the resource file
Ben Wagner145dbcd2016-11-03 14:40:50 -0400578 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarette16b04a2015-04-15 07:32:19 -0700579 if (!stream) {
msarette16b04a2015-04-15 07:32:19 -0700580 return;
581 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400582 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarette16b04a2015-04-15 07:32:19 -0700583 if (!codec) {
584 ERRORF(r, "Unable to create codec '%s'", path);
585 return;
586 }
587
588 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800589 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700590 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700591 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700592 SkImageInfo scaledInfo = codec->getInfo()
593 .makeWH(scaledDims.width(), scaledDims.height())
594 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700595
596 // Set up for the decode
597 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
598 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
599 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
600
msarett3d9d7a72015-10-21 10:27:10 -0700601 SkAndroidCodec::AndroidOptions options;
602 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700603 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700604 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700605 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700606 }
607}
608
609// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
610DEF_TEST(Codec_Dimensions, r) {
611 // JPG
612 test_dimensions(r, "CMYK.jpg");
613 test_dimensions(r, "color_wheel.jpg");
614 test_dimensions(r, "grayscale.jpg");
615 test_dimensions(r, "mandrill_512_q075.jpg");
616 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700617
618 // Decoding small images with very large scaling factors is a potential
619 // source of bugs and crashes. We disable these tests in Gold because
620 // tiny images are not very useful to look at.
621 // Here we make sure that we do not crash or access illegal memory when
622 // performing scaled decodes on small images.
623 test_dimensions(r, "1x1.png");
624 test_dimensions(r, "2x2.png");
625 test_dimensions(r, "3x3.png");
626 test_dimensions(r, "3x1.png");
627 test_dimensions(r, "1x1.png");
628 test_dimensions(r, "16x1.png");
629 test_dimensions(r, "1x16.png");
630 test_dimensions(r, "mandrill_16.png");
631
yujieqin916de9f2016-01-25 08:26:16 -0800632 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800633// Disable RAW tests for Win32.
634#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800635 test_dimensions(r, "sample_1mp.dng");
ebrauer46d2aa82016-02-17 08:04:00 -0800636 test_dimensions(r, "sample_1mp_rotated.dng");
yujieqin9c7a8a42016-02-05 08:21:19 -0800637 test_dimensions(r, "dng_with_preview.dng");
msarett8e49ca32016-01-25 13:10:58 -0800638#endif
msarette16b04a2015-04-15 07:32:19 -0700639}
640
msarettd0375bc2015-08-12 08:08:56 -0700641static void test_invalid(skiatest::Reporter* r, const char path[]) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400642 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett4b17fa32015-04-23 08:53:39 -0700643 if (!stream) {
msarett4b17fa32015-04-23 08:53:39 -0700644 return;
645 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400646 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
halcanary96fcdcc2015-08-27 07:41:13 -0700647 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700648}
msarette16b04a2015-04-15 07:32:19 -0700649
msarett4b17fa32015-04-23 08:53:39 -0700650DEF_TEST(Codec_Empty, r) {
651 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700652 test_invalid(r, "empty_images/zero-dims.gif");
653 test_invalid(r, "empty_images/zero-embedded.ico");
654 test_invalid(r, "empty_images/zero-width.bmp");
655 test_invalid(r, "empty_images/zero-height.bmp");
656 test_invalid(r, "empty_images/zero-width.jpg");
657 test_invalid(r, "empty_images/zero-height.jpg");
658 test_invalid(r, "empty_images/zero-width.png");
659 test_invalid(r, "empty_images/zero-height.png");
660 test_invalid(r, "empty_images/zero-width.wbmp");
661 test_invalid(r, "empty_images/zero-height.wbmp");
662 // This image is an ico with an embedded mask-bmp. This is illegal.
663 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
Leon Scroggins IIId87fbee2016-12-02 16:47:53 -0500664#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
665 test_invalid(r, "empty_images/zero_height.tiff");
666#endif
msarett4b17fa32015-04-23 08:53:39 -0700667}
msarett99f567e2015-08-05 12:58:26 -0700668
669static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400670 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett99f567e2015-08-05 12:58:26 -0700671 if (!stream) {
msarett99f567e2015-08-05 12:58:26 -0700672 return;
673 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400674 std::unique_ptr<SkCodec> decoder(SkCodec::NewFromStream(stream.release()));
scroggo8e6c7ad2016-09-16 08:20:38 -0700675 if (!decoder) {
676 SkDebugf("Missing codec for %s\n", path);
677 return;
678 }
679
680 const SkImageInfo info = decoder->getInfo().makeColorType(kIndex_8_SkColorType);
halcanary9d524f22016-03-29 09:03:52 -0700681
msarett99f567e2015-08-05 12:58:26 -0700682 // This should return kSuccess because kIndex8 is supported.
683 SkPMColor colorStorage[256];
684 int colorCount;
scroggo8e6c7ad2016-09-16 08:20:38 -0700685 SkCodec::Result result = decoder->startScanlineDecode(info, nullptr, colorStorage,
686 &colorCount);
687 if (SkCodec::kSuccess == result) {
688 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
689 // colorPtr and a valid colorCountPtr.
690 result = decoder->startScanlineDecode(info, nullptr, nullptr, nullptr);
691 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
692 result = decoder->startScanlineDecode(info);
693 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
694 } else if (SkCodec::kUnimplemented == result) {
695 // New method should be supported:
696 SkBitmap bm;
697 sk_sp<SkColorTable> colorTable(new SkColorTable(colorStorage, 256));
698 bm.allocPixels(info, nullptr, colorTable.get());
699 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes(), nullptr,
700 colorStorage, &colorCount);
701 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
702 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
703 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
704 } else {
705 // The test is uninteresting if kIndex8 is not supported
706 ERRORF(r, "Should not call test_invalid_parameters for non-Index8 file: %s\n", path);
msarett99f567e2015-08-05 12:58:26 -0700707 return;
708 }
709
msarett99f567e2015-08-05 12:58:26 -0700710}
711
712DEF_TEST(Codec_Params, r) {
713 test_invalid_parameters(r, "index8.png");
714 test_invalid_parameters(r, "mandrill.wbmp");
715}
scroggocf98fa92015-11-23 08:14:40 -0800716
scroggo8e6c7ad2016-09-16 08:20:38 -0700717#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
718
719#ifndef SK_PNG_DISABLE_TESTS // reading chunks does not work properly with older versions.
720 // It does not appear that anyone in Google3 is reading chunks.
721
scroggocf98fa92015-11-23 08:14:40 -0800722static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
723 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
724 if (!sk_stream->write(data, len)) {
725 png_error(png_ptr, "sk_write_fn Error!");
726 }
727}
728
scroggocf98fa92015-11-23 08:14:40 -0800729DEF_TEST(Codec_pngChunkReader, r) {
730 // Create a dummy bitmap. Use unpremul RGBA for libpng.
731 SkBitmap bm;
732 const int w = 1;
733 const int h = 1;
734 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
735 kUnpremul_SkAlphaType);
736 bm.setInfo(bmInfo);
737 bm.allocPixels();
738 bm.eraseColor(SK_ColorBLUE);
739 SkMD5::Digest goodDigest;
740 md5(bm, &goodDigest);
741
742 // Write to a png file.
743 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
744 REPORTER_ASSERT(r, png);
745 if (!png) {
746 return;
747 }
748
749 png_infop info = png_create_info_struct(png);
750 REPORTER_ASSERT(r, info);
751 if (!info) {
752 png_destroy_write_struct(&png, nullptr);
753 return;
754 }
755
756 if (setjmp(png_jmpbuf(png))) {
757 ERRORF(r, "failed writing png");
758 png_destroy_write_struct(&png, &info);
759 return;
760 }
761
762 SkDynamicMemoryWStream wStream;
763 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
764
765 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
766 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
767 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
768
769 // Create some chunks that match the Android framework's use.
770 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800771 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
772 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
773 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800774 };
775
776 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
777 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
778#if PNG_LIBPNG_VER < 10600
779 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800780 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
781 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800782#endif
783
784 png_write_info(png, info);
785
786 for (int j = 0; j < h; j++) {
787 png_bytep row = (png_bytep)(bm.getAddr(0, j));
788 png_write_rows(png, &row, 1);
789 }
790 png_write_end(png, info);
791 png_destroy_write_struct(&png, &info);
792
793 class ChunkReader : public SkPngChunkReader {
794 public:
795 ChunkReader(skiatest::Reporter* r)
796 : fReporter(r)
797 {
798 this->reset();
799 }
800
801 bool readChunk(const char tag[], const void* data, size_t length) override {
802 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
803 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
804 // Tag matches. This should have been the first time we see it.
805 REPORTER_ASSERT(fReporter, !fSeen[i]);
806 fSeen[i] = true;
807
808 // Data and length should match
809 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
810 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
811 (const char*) gUnknowns[i].data));
812 return true;
813 }
814 }
815 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
816 return true;
817 }
818
819 bool allHaveBeenSeen() {
820 bool ret = true;
821 for (auto seen : fSeen) {
822 ret &= seen;
823 }
824 return ret;
825 }
826
827 void reset() {
828 sk_bzero(fSeen, sizeof(fSeen));
829 }
830
831 private:
832 skiatest::Reporter* fReporter; // Unowned
833 bool fSeen[3];
834 };
835
836 ChunkReader chunkReader(r);
837
838 // Now read the file with SkCodec.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400839 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(wStream.detachAsData(), &chunkReader));
scroggocf98fa92015-11-23 08:14:40 -0800840 REPORTER_ASSERT(r, codec);
841 if (!codec) {
842 return;
843 }
844
845 // Now compare to the original.
846 SkBitmap decodedBm;
847 decodedBm.setInfo(codec->getInfo());
848 decodedBm.allocPixels();
849 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
850 decodedBm.rowBytes());
851 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
852
853 if (decodedBm.colorType() != bm.colorType()) {
854 SkBitmap tmp;
855 bool success = decodedBm.copyTo(&tmp, bm.colorType());
856 REPORTER_ASSERT(r, success);
857 if (!success) {
858 return;
859 }
860
861 tmp.swap(decodedBm);
862 }
863
864 compare_to_good_digest(r, goodDigest, decodedBm);
865 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
866
867 // Decoding again will read the chunks again.
868 chunkReader.reset();
869 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
870 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
871 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
872 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
873}
scroggo8e6c7ad2016-09-16 08:20:38 -0700874#endif // SK_PNG_DISABLE_TESTS
scroggocf98fa92015-11-23 08:14:40 -0800875#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800876
scroggodb30be22015-12-08 18:54:13 -0800877// Stream that can only peek up to a limit
878class LimitedPeekingMemStream : public SkStream {
879public:
reed42943c82016-09-12 12:01:44 -0700880 LimitedPeekingMemStream(sk_sp<SkData> data, size_t limit)
881 : fStream(std::move(data))
scroggodb30be22015-12-08 18:54:13 -0800882 , fLimit(limit) {}
883
884 size_t peek(void* buf, size_t bytes) const override {
885 return fStream.peek(buf, SkTMin(bytes, fLimit));
886 }
887 size_t read(void* buf, size_t bytes) override {
888 return fStream.read(buf, bytes);
889 }
890 bool rewind() override {
891 return fStream.rewind();
892 }
893 bool isAtEnd() const override {
msarettff2a6c82016-09-07 11:23:28 -0700894 return fStream.isAtEnd();
scroggodb30be22015-12-08 18:54:13 -0800895 }
896private:
897 SkMemoryStream fStream;
898 const size_t fLimit;
899};
900
yujieqinf236ee42016-02-29 07:14:42 -0800901// Disable RAW tests for Win32.
902#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin9c7a8a42016-02-05 08:21:19 -0800903// Test that the RawCodec works also for not asset stream. This will test the code path using
904// SkRawBufferedStream instead of SkRawAssetStream.
yujieqin9c7a8a42016-02-05 08:21:19 -0800905DEF_TEST(Codec_raw_notseekable, r) {
906 const char* path = "dng_with_preview.dng";
907 SkString fullPath(GetResourcePath(path));
bungeman38d909e2016-08-02 14:40:46 -0700908 sk_sp<SkData> data(SkData::MakeFromFileName(fullPath.c_str()));
yujieqin9c7a8a42016-02-05 08:21:19 -0800909 if (!data) {
910 SkDebugf("Missing resource '%s'\n", path);
911 return;
912 }
913
Ben Wagner145dbcd2016-11-03 14:40:50 -0400914 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(std::move(data))));
yujieqin9c7a8a42016-02-05 08:21:19 -0800915 REPORTER_ASSERT(r, codec);
916
917 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
918}
919#endif
920
scroggodb30be22015-12-08 18:54:13 -0800921// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
922// + rewind() and succeed.
923DEF_TEST(Codec_webp_peek, r) {
924 const char* path = "baby_tux.webp";
925 SkString fullPath(GetResourcePath(path));
reedfde05112016-03-11 13:02:28 -0800926 auto data = SkData::MakeFromFileName(fullPath.c_str());
scroggodb30be22015-12-08 18:54:13 -0800927 if (!data) {
928 SkDebugf("Missing resource '%s'\n", path);
929 return;
930 }
931
932 // The limit is less than webp needs to peek or read.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400933 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(
reed42943c82016-09-12 12:01:44 -0700934 new LimitedPeekingMemStream(data, 25)));
scroggodb30be22015-12-08 18:54:13 -0800935 REPORTER_ASSERT(r, codec);
936
scroggo7b5e5532016-02-04 06:14:24 -0800937 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800938
939 // Similarly, a stream which does not peek should still succeed.
reed42943c82016-09-12 12:01:44 -0700940 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0)));
scroggodb30be22015-12-08 18:54:13 -0800941 REPORTER_ASSERT(r, codec);
942
scroggo7b5e5532016-02-04 06:14:24 -0800943 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800944}
945
msarett7f7ec202016-03-01 12:12:27 -0800946// SkCodec's wbmp decoder was initially unnecessarily restrictive.
947// It required the second byte to be zero. The wbmp specification allows
948// a couple of bits to be 1 (so long as they do not overlap with 0x9F).
949// Test that SkCodec now supports an image with these bits set.
Leon Scroggins III83926342016-12-06 10:58:02 -0500950DEF_TEST(Codec_wbmp_restrictive, r) {
scroggob9a1e342015-11-30 06:25:31 -0800951 const char* path = "mandrill.wbmp";
Ben Wagner145dbcd2016-11-03 14:40:50 -0400952 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
scroggob9a1e342015-11-30 06:25:31 -0800953 if (!stream) {
scroggob9a1e342015-11-30 06:25:31 -0800954 return;
955 }
956
957 // Modify the stream to contain a second byte with some bits set.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400958 auto data = SkCopyStreamToData(stream.get());
scroggob9a1e342015-11-30 06:25:31 -0800959 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
960 writeableData[1] = static_cast<uint8_t>(~0x9F);
961
msarett7f7ec202016-03-01 12:12:27 -0800962 // SkCodec should support this.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400963 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
scroggob9a1e342015-11-30 06:25:31 -0800964 REPORTER_ASSERT(r, codec);
965 if (!codec) {
966 return;
967 }
scroggo7b5e5532016-02-04 06:14:24 -0800968 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800969}
scroggodb30be22015-12-08 18:54:13 -0800970
971// wbmp images have a header that can be arbitrarily large, depending on the
972// size of the image. We cap the size at 65535, meaning we only need to look at
973// 8 bytes to determine whether we can read the image. This is important
974// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
975// image is a wbmp.
976DEF_TEST(Codec_wbmp_max_size, r) {
977 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
978 0x83, 0xFF, 0x7F, // W: 65535
979 0x83, 0xFF, 0x7F }; // H: 65535
Ben Wagner145dbcd2016-11-03 14:40:50 -0400980 std::unique_ptr<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
981 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -0800982
983 REPORTER_ASSERT(r, codec);
984 if (!codec) return;
985
986 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
987 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
988
989 // Now test an image which is too big. Any image with a larger header (i.e.
990 // has bigger width/height) is also too big.
991 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
992 0x84, 0x80, 0x00, // W: 65536
993 0x84, 0x80, 0x00 }; // H: 65536
994 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
mtklein18300a32016-03-16 13:53:35 -0700995 codec.reset(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -0800996
997 REPORTER_ASSERT(r, !codec);
998}
msarett2812f032016-07-18 15:56:08 -0700999
1000DEF_TEST(Codec_jpeg_rewind, r) {
1001 const char* path = "mandrill_512_q075.jpg";
Ben Wagner145dbcd2016-11-03 14:40:50 -04001002 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett2812f032016-07-18 15:56:08 -07001003 if (!stream) {
msarett2812f032016-07-18 15:56:08 -07001004 return;
1005 }
Ben Wagner145dbcd2016-11-03 14:40:50 -04001006 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarett2812f032016-07-18 15:56:08 -07001007 if (!codec) {
1008 ERRORF(r, "Unable to create codec '%s'.", path);
1009 return;
1010 }
1011
1012 const int width = codec->getInfo().width();
1013 const int height = codec->getInfo().height();
1014 size_t rowBytes = sizeof(SkPMColor) * width;
1015 SkAutoMalloc pixelStorage(height * rowBytes);
1016
1017 // Perform a sampled decode.
1018 SkAndroidCodec::AndroidOptions opts;
1019 opts.fSampleSize = 12;
1020 codec->getAndroidPixels(codec->getInfo().makeWH(width / 12, height / 12), pixelStorage.get(),
1021 rowBytes, &opts);
1022
1023 // Rewind the codec and perform a full image decode.
1024 SkCodec::Result result = codec->getPixels(codec->getInfo(), pixelStorage.get(), rowBytes);
1025 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1026}
msarett549ca322016-08-17 08:54:08 -07001027
msarett35bb74b2016-08-22 07:41:28 -07001028static void check_color_xform(skiatest::Reporter* r, const char* path) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001029 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(GetResourceAsStream(path)));
msarett35bb74b2016-08-22 07:41:28 -07001030
1031 SkAndroidCodec::AndroidOptions opts;
1032 opts.fSampleSize = 3;
1033 const int subsetWidth = codec->getInfo().width() / 2;
1034 const int subsetHeight = codec->getInfo().height() / 2;
1035 SkIRect subset = SkIRect::MakeWH(subsetWidth, subsetHeight);
1036 opts.fSubset = &subset;
1037
1038 const int dstWidth = subsetWidth / opts.fSampleSize;
1039 const int dstHeight = subsetHeight / opts.fSampleSize;
1040 sk_sp<SkData> data = SkData::MakeFromFileName(
1041 GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str());
Brian Osman526972e2016-10-24 09:24:02 -04001042 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeICC(data->data(), data->size());
msarett35bb74b2016-08-22 07:41:28 -07001043 SkImageInfo dstInfo = codec->getInfo().makeWH(dstWidth, dstHeight)
1044 .makeColorType(kN32_SkColorType)
1045 .makeColorSpace(colorSpace);
1046
1047 size_t rowBytes = dstInfo.minRowBytes();
1048 SkAutoMalloc pixelStorage(dstInfo.getSafeSize(rowBytes));
1049 SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get(), rowBytes, &opts);
1050 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1051}
1052
1053DEF_TEST(Codec_ColorXform, r) {
1054 check_color_xform(r, "mandrill_512_q075.jpg");
1055 check_color_xform(r, "mandrill_512.png");
1056}
1057
msarettf17b71f2016-09-12 14:30:03 -07001058static bool color_type_match(SkColorType origColorType, SkColorType codecColorType) {
1059 switch (origColorType) {
1060 case kRGBA_8888_SkColorType:
1061 case kBGRA_8888_SkColorType:
1062 return kRGBA_8888_SkColorType == codecColorType ||
1063 kBGRA_8888_SkColorType == codecColorType;
1064 default:
1065 return origColorType == codecColorType;
1066 }
1067}
1068
1069static bool alpha_type_match(SkAlphaType origAlphaType, SkAlphaType codecAlphaType) {
1070 switch (origAlphaType) {
1071 case kUnpremul_SkAlphaType:
1072 case kPremul_SkAlphaType:
1073 return kUnpremul_SkAlphaType == codecAlphaType ||
1074 kPremul_SkAlphaType == codecAlphaType;
1075 default:
1076 return origAlphaType == codecAlphaType;
1077 }
1078}
1079
1080static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const SkImageInfo& info) {
1081 SkBitmap bm1;
1082 SkPMColor colors[256];
Hal Canary342b7ac2016-11-04 11:49:42 -04001083 sk_sp<SkColorTable> colorTable1(new SkColorTable(colors, 256));
msarettf17b71f2016-09-12 14:30:03 -07001084 bm1.allocPixels(info, nullptr, colorTable1.get());
1085 int numColors;
1086 SkCodec::Result result = origCodec->getPixels(info, bm1.getPixels(), bm1.rowBytes(), nullptr,
1087 const_cast<SkPMColor*>(colorTable1->readColors()),
1088 &numColors);
1089 // This will fail to update colorTable1->count() but is fine for the purpose of this test.
1090 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarett9b09cd82016-08-29 14:47:49 -07001091
1092 // Encode the image to png.
1093 sk_sp<SkData> data =
Hal Canarydb683012016-11-23 08:55:18 -07001094 sk_sp<SkData>(sk_tool_utils::EncodeImageToData(bm1, SkEncodedImageFormat::kPNG, 100));
msarett9b09cd82016-08-29 14:47:49 -07001095
Ben Wagner145dbcd2016-11-03 14:40:50 -04001096 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
msarettf17b71f2016-09-12 14:30:03 -07001097 REPORTER_ASSERT(r, color_type_match(info.colorType(), codec->getInfo().colorType()));
1098 REPORTER_ASSERT(r, alpha_type_match(info.alphaType(), codec->getInfo().alphaType()));
msarett9b09cd82016-08-29 14:47:49 -07001099
1100 SkBitmap bm2;
Hal Canary342b7ac2016-11-04 11:49:42 -04001101 sk_sp<SkColorTable> colorTable2(new SkColorTable(colors, 256));
msarettf17b71f2016-09-12 14:30:03 -07001102 bm2.allocPixels(info, nullptr, colorTable2.get());
1103 result = codec->getPixels(info, bm2.getPixels(), bm2.rowBytes(), nullptr,
1104 const_cast<SkPMColor*>(colorTable2->readColors()), &numColors);
msarett9b09cd82016-08-29 14:47:49 -07001105 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1106
1107 SkMD5::Digest d1, d2;
1108 md5(bm1, &d1);
1109 md5(bm2, &d2);
1110 REPORTER_ASSERT(r, d1 == d2);
1111}
1112
1113DEF_TEST(Codec_PngRoundTrip, r) {
msarett549ca322016-08-17 08:54:08 -07001114 const char* path = "mandrill_512_q075.jpg";
Ben Wagner145dbcd2016-11-03 14:40:50 -04001115 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
1116 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett549ca322016-08-17 08:54:08 -07001117
msarettf17b71f2016-09-12 14:30:03 -07001118 SkColorType colorTypesOpaque[] = {
1119 kRGB_565_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1120 };
1121 for (SkColorType colorType : colorTypesOpaque) {
1122 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType);
1123 check_round_trip(r, codec.get(), newInfo);
1124 }
1125
msarett9b09cd82016-08-29 14:47:49 -07001126 path = "grayscale.jpg";
bungemanf93d7112016-09-16 06:24:20 -07001127 stream.reset(GetResourceAsStream(path));
msarett9b09cd82016-08-29 14:47:49 -07001128 codec.reset(SkCodec::NewFromStream(stream.release()));
msarettf17b71f2016-09-12 14:30:03 -07001129 check_round_trip(r, codec.get(), codec->getInfo());
1130
1131 path = "yellow_rose.png";
bungemanf93d7112016-09-16 06:24:20 -07001132 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001133 codec.reset(SkCodec::NewFromStream(stream.release()));
1134
1135 SkColorType colorTypesWithAlpha[] = {
1136 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1137 };
1138 SkAlphaType alphaTypes[] = {
1139 kUnpremul_SkAlphaType, kPremul_SkAlphaType
1140 };
1141 for (SkColorType colorType : colorTypesWithAlpha) {
1142 for (SkAlphaType alphaType : alphaTypes) {
1143 // Set color space to nullptr because color correct premultiplies do not round trip.
1144 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType)
1145 .makeAlphaType(alphaType)
1146 .makeColorSpace(nullptr);
1147 check_round_trip(r, codec.get(), newInfo);
1148 }
1149 }
1150
1151 path = "index8.png";
bungemanf93d7112016-09-16 06:24:20 -07001152 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001153 codec.reset(SkCodec::NewFromStream(stream.release()));
1154
1155 for (SkAlphaType alphaType : alphaTypes) {
1156 SkImageInfo newInfo = codec->getInfo().makeAlphaType(alphaType)
1157 .makeColorSpace(nullptr);
1158 check_round_trip(r, codec.get(), newInfo);
1159 }
msarett549ca322016-08-17 08:54:08 -07001160}
msarett2ecc35f2016-09-08 11:55:16 -07001161
1162static void test_conversion_possible(skiatest::Reporter* r, const char* path,
scroggo8e6c7ad2016-09-16 08:20:38 -07001163 bool supportsScanlineDecoder,
1164 bool supportsIncrementalDecoder) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001165 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
1166 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett2ecc35f2016-09-08 11:55:16 -07001167 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType);
1168
1169 SkBitmap bm;
1170 bm.allocPixels(infoF16);
1171 SkCodec::Result result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1172 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001173
1174 result = codec->startScanlineDecode(infoF16);
1175 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001176 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001177 } else {
1178 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1179 }
1180
1181 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1182 if (supportsIncrementalDecoder) {
1183 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
1184 } else {
1185 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001186 }
1187
raftias94888332016-10-18 10:02:51 -07001188 SkASSERT(SkColorSpace_Base::Type::kXYZ == as_CSB(infoF16.colorSpace())->type());
1189 SkColorSpace_XYZ* csXYZ = static_cast<SkColorSpace_XYZ*>(infoF16.colorSpace());
1190 infoF16 = infoF16.makeColorSpace(csXYZ->makeLinearGamma());
msarett2ecc35f2016-09-08 11:55:16 -07001191 result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1192 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001193 result = codec->startScanlineDecode(infoF16);
1194 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001195 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001196 } else {
1197 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1198 }
1199
1200 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1201 if (supportsIncrementalDecoder) {
1202 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1203 } else {
1204 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001205 }
1206}
1207
1208DEF_TEST(Codec_F16ConversionPossible, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -07001209 test_conversion_possible(r, "color_wheel.webp", false, false);
1210 test_conversion_possible(r, "mandrill_512_q075.jpg", true, false);
1211 test_conversion_possible(r, "yellow_rose.png", false, true);
1212}
1213
scroggo19b91532016-10-24 09:03:26 -07001214static void decode_frame(skiatest::Reporter* r, SkCodec* codec, size_t frame) {
1215 SkBitmap bm;
1216 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1217 bm.allocPixels(info);
1218
1219 SkCodec::Options opts;
1220 opts.fFrameIndex = frame;
1221 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->getPixels(info,
1222 bm.getPixels(), bm.rowBytes(), &opts, nullptr, nullptr));
1223}
1224
1225// For an animated image, we should only read enough to decode the requested
1226// frame if the client never calls getFrameInfo.
1227DEF_TEST(Codec_skipFullParse, r) {
1228 auto path = "test640x479.gif";
1229 SkStream* stream(GetResourceAsStream(path));
1230 if (!stream) {
1231 return;
1232 }
1233
1234 // Note that we cheat and hold on to the stream pointer, but SkCodec will
1235 // take ownership. We will not refer to the stream after the SkCodec
1236 // deletes it.
1237 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
1238 if (!codec) {
1239 ERRORF(r, "Failed to create codec for %s", path);
1240 return;
1241 }
1242
1243 REPORTER_ASSERT(r, stream->hasPosition());
1244 const size_t sizePosition = stream->getPosition();
1245 REPORTER_ASSERT(r, stream->hasLength() && sizePosition < stream->getLength());
1246
1247 // This should read more of the stream, but not the whole stream.
1248 decode_frame(r, codec.get(), 0);
1249 const size_t positionAfterFirstFrame = stream->getPosition();
1250 REPORTER_ASSERT(r, positionAfterFirstFrame > sizePosition
1251 && positionAfterFirstFrame < stream->getLength());
1252
1253 // Again, this should read more of the stream.
1254 decode_frame(r, codec.get(), 2);
1255 const size_t positionAfterThirdFrame = stream->getPosition();
1256 REPORTER_ASSERT(r, positionAfterThirdFrame > positionAfterFirstFrame
1257 && positionAfterThirdFrame < stream->getLength());
1258
1259 // This does not need to read any more of the stream, since it has already
1260 // parsed the second frame.
1261 decode_frame(r, codec.get(), 1);
1262 REPORTER_ASSERT(r, stream->getPosition() == positionAfterThirdFrame);
1263
1264 // This should read the rest of the frames.
1265 decode_frame(r, codec.get(), 3);
1266 const size_t finalPosition = stream->getPosition();
1267 REPORTER_ASSERT(r, finalPosition > positionAfterThirdFrame);
1268
1269 // There may be more data in the stream.
1270 auto frameInfo = codec->getFrameInfo();
1271 REPORTER_ASSERT(r, frameInfo.size() == 4);
1272 REPORTER_ASSERT(r, stream->getPosition() >= finalPosition);
1273}
1274
scroggo8e6c7ad2016-09-16 08:20:38 -07001275// Only rewinds up to a limit.
1276class LimitedRewindingStream : public SkStream {
1277public:
1278 static SkStream* Make(const char path[], size_t limit) {
1279 SkStream* stream = GetResourceAsStream(path);
1280 if (!stream) {
1281 return nullptr;
1282 }
1283 return new LimitedRewindingStream(stream, limit);
1284 }
1285
1286 size_t read(void* buffer, size_t size) override {
1287 const size_t bytes = fStream->read(buffer, size);
1288 fPosition += bytes;
1289 return bytes;
1290 }
1291
1292 bool isAtEnd() const override {
1293 return fStream->isAtEnd();
1294 }
1295
1296 bool rewind() override {
1297 if (fPosition <= fLimit && fStream->rewind()) {
1298 fPosition = 0;
1299 return true;
1300 }
1301
1302 return false;
1303 }
1304
1305private:
Ben Wagner145dbcd2016-11-03 14:40:50 -04001306 std::unique_ptr<SkStream> fStream;
1307 const size_t fLimit;
1308 size_t fPosition;
scroggo8e6c7ad2016-09-16 08:20:38 -07001309
1310 LimitedRewindingStream(SkStream* stream, size_t limit)
1311 : fStream(stream)
1312 , fLimit(limit)
1313 , fPosition(0)
1314 {
1315 SkASSERT(fStream);
1316 }
1317};
1318
1319DEF_TEST(Codec_fallBack, r) {
1320 // SkAndroidCodec needs to be able to fall back to scanline decoding
1321 // if incremental decoding does not work. Make sure this does not
1322 // require a rewind.
1323
1324 // Formats that currently do not support incremental decoding
1325 auto files = {
scroggo8e6c7ad2016-09-16 08:20:38 -07001326 "CMYK.jpg",
1327 "color_wheel.ico",
1328 "mandrill.wbmp",
1329 "randPixels.bmp",
1330 };
1331 for (auto file : files) {
1332 SkStream* stream = LimitedRewindingStream::Make(file, 14);
1333 if (!stream) {
1334 SkDebugf("Missing resources (%s). Set --resourcePath.\n", file);
1335 return;
1336 }
1337
Ben Wagner145dbcd2016-11-03 14:40:50 -04001338 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
scroggo8e6c7ad2016-09-16 08:20:38 -07001339 if (!codec) {
1340 ERRORF(r, "Failed to create codec for %s,", file);
1341 continue;
1342 }
1343
1344 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
1345 SkBitmap bm;
1346 bm.allocPixels(info);
1347
1348 if (SkCodec::kUnimplemented != codec->startIncrementalDecode(info, bm.getPixels(),
1349 bm.rowBytes())) {
1350 ERRORF(r, "Is scanline decoding now implemented for %s?", file);
1351 continue;
1352 }
1353
1354 // Scanline decoding should not require a rewind.
1355 SkCodec::Result result = codec->startScanlineDecode(info);
1356 if (SkCodec::kSuccess != result) {
1357 ERRORF(r, "Scanline decoding failed for %s with %i", file, result);
1358 }
1359 }
msarett2ecc35f2016-09-08 11:55:16 -07001360}
scroggoc46cdd42016-10-10 06:45:32 -07001361
1362// This test verifies that we fixed an assert statement that fired when reusing a png codec
1363// after scaling.
1364DEF_TEST(Codec_reusePng, r) {
1365 std::unique_ptr<SkStream> stream(GetResourceAsStream("plane.png"));
1366 if (!stream) {
1367 return;
1368 }
1369
1370 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
1371 if (!codec) {
1372 ERRORF(r, "Failed to create codec\n");
1373 return;
1374 }
1375
1376 SkAndroidCodec::AndroidOptions opts;
1377 opts.fSampleSize = 5;
1378 auto size = codec->getSampledDimensions(opts.fSampleSize);
1379 auto info = codec->getInfo().makeWH(size.fWidth, size.fHeight).makeColorType(kN32_SkColorType);
1380 SkBitmap bm;
1381 bm.allocPixels(info);
1382 auto result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1383 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1384
1385 info = codec->getInfo().makeColorType(kN32_SkColorType);
1386 bm.allocPixels(info);
1387 opts.fSampleSize = 1;
1388 result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1389 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1390}
scroggoe61b3b42016-10-10 07:17:32 -07001391
1392DEF_TEST(Codec_rowsDecoded, r) {
1393 auto file = "plane_interlaced.png";
1394 std::unique_ptr<SkStream> stream(GetResourceAsStream(file));
1395 if (!stream) {
1396 return;
1397 }
1398
1399 // This is enough to read the header etc, but no rows.
1400 auto data = SkData::MakeFromStream(stream.get(), 99);
1401 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
1402 if (!codec) {
1403 ERRORF(r, "Failed to create codec\n");
1404 return;
1405 }
1406
1407 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1408 SkBitmap bm;
1409 bm.allocPixels(info);
1410 auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
1411 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1412
1413 // This is an arbitrary value. The important fact is that it is not zero, and rowsDecoded
1414 // should get set to zero by incrementalDecode.
1415 int rowsDecoded = 77;
1416 result = codec->incrementalDecode(&rowsDecoded);
1417 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput);
1418 REPORTER_ASSERT(r, rowsDecoded == 0);
1419}
Matt Sarett29121eb2016-10-17 14:32:46 -04001420
Matt Sarett8a4e9c52016-10-25 14:24:50 -04001421static void test_invalid_images(skiatest::Reporter* r, const char* path, bool shouldSucceed) {
Matt Sarett29121eb2016-10-17 14:32:46 -04001422 SkBitmap bitmap;
Matt Sarett8a4e9c52016-10-25 14:24:50 -04001423 const bool success = GetResourceAsBitmap(path, &bitmap);
1424 REPORTER_ASSERT(r, success == shouldSucceed);
1425}
1426
1427DEF_TEST(Codec_InvalidImages, r) {
1428 // ASAN will complain if there is an issue.
1429 test_invalid_images(r, "invalid_images/int_overflow.ico", false);
1430 test_invalid_images(r, "invalid_images/skbug5887.gif", true);
Matt Sarettdbdf6d22016-11-08 15:26:56 -05001431 test_invalid_images(r, "invalid_images/many-progressive-scans.jpg", false);
Matt Sarett29121eb2016-10-17 14:32:46 -04001432}
Leon Scroggins III56e32092016-12-12 17:10:46 -05001433
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -05001434DEF_TEST(Codec_InvalidBmp, r) {
1435 // This file reports a header size that crashes when we try to read this
1436 // much directly from a file using SkFILEStream.
1437 SkString path = GetResourcePath("invalid_images/b33651913.bmp");
1438 std::unique_ptr<SkFILEStream> stream(new SkFILEStream(path.c_str()));
1439 if (!stream->isValid()) {
1440 ERRORF(r, "no stream");
1441 return;
1442 }
1443
1444 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
1445 // This file is invalid, but more importantly, we did not crash before
1446 // reaching here.
1447 REPORTER_ASSERT(r, !codec);
1448}
1449
Leon Scroggins III56e32092016-12-12 17:10:46 -05001450DEF_TEST(Codec_InvalidAnimated, r) {
1451 // ASAN will complain if there is an issue.
1452 auto path = "invalid_images/skbug6046.gif";
1453 auto* stream = GetResourceAsStream(path);
1454 if (!stream) {
1455 return;
1456 }
1457
1458 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
1459 REPORTER_ASSERT(r, codec);
1460 if (!codec) {
1461 return;
1462 }
1463
1464 const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1465 SkBitmap bm;
1466 bm.allocPixels(info);
1467
1468 auto frameInfos = codec->getFrameInfo();
1469 SkCodec::Options opts;
1470 for (size_t i = 0; i < frameInfos.size(); i++) {
1471 opts.fFrameIndex = i;
1472 opts.fHasPriorFrame = frameInfos[i].fRequiredFrame == i - 1;
1473 auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes(), &opts);
1474 if (result != SkCodec::kSuccess) {
1475 ERRORF(r, "Failed to start decoding frame %i (out of %i) with error %i\n", i,
1476 frameInfos.size(), result);
1477 continue;
1478 }
1479
1480 codec->incrementalDecode();
1481 }
1482}