blob: 265b51e8860ee9a791e3239612da9a11b05ac5f6 [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"
Hal Canary95e3c052017-01-11 12:44:43 -050011#include "SkAutoMalloc.h"
halcanarya096d7a2015-03-27 12:16:53 -070012#include "SkBitmap.h"
13#include "SkCodec.h"
msarettedd2dcf2016-01-14 13:12:26 -080014#include "SkCodecImageGenerator.h"
raftias94888332016-10-18 10:02:51 -070015#include "SkColorSpace_XYZ.h"
msarette6dd0042015-10-09 11:07:34 -070016#include "SkData.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000017#include "SkFrontBufferedStream.h"
Hal Canary95e3c052017-01-11 12:44:43 -050018#include "SkImageEncoder.h"
halcanarya096d7a2015-03-27 12:16:53 -070019#include "SkMD5.h"
Leon Scroggins III0354c622017-02-24 15:33:24 -050020#include "SkOSPath.h"
Hal Canary95e3c052017-01-11 12:44:43 -050021#include "SkPngChunkReader.h"
scroggob636b452015-07-22 07:16:20 -070022#include "SkRandom.h"
scroggocf98fa92015-11-23 08:14:40 -080023#include "SkStream.h"
scroggob9a1e342015-11-30 06:25:31 -080024#include "SkStreamPriv.h"
halcanarya096d7a2015-03-27 12:16:53 -070025#include "Test.h"
26
scroggocf98fa92015-11-23 08:14:40 -080027#include "png.h"
28
Hal Canarydb683012016-11-23 08:55:18 -070029#include "sk_tool_utils.h"
30
scroggo8e6c7ad2016-09-16 08:20:38 -070031#if PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 5
32 // FIXME (scroggo): Google3 needs to be updated to use a newer version of libpng. In
33 // the meantime, we had to break some pieces of SkPngCodec in order to support Google3.
34 // The parts that are broken are likely not used by Google3.
35 #define SK_PNG_DISABLE_TESTS
36#endif
37
halcanarya096d7a2015-03-27 12:16:53 -070038static void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
39 SkAutoLockPixels autoLockPixels(bm);
40 SkASSERT(bm.getPixels());
41 SkMD5 md5;
42 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
43 for (int y = 0; y < bm.height(); ++y) {
halcanary1e903042016-04-25 10:29:36 -070044 md5.write(bm.getAddr(0, y), rowLen);
halcanarya096d7a2015-03-27 12:16:53 -070045 }
46 md5.finish(*digest);
47}
48
scroggo9b2cdbf42015-07-10 12:07:02 -070049/**
50 * Compute the digest for bm and compare it to a known good digest.
51 * @param r Reporter to assert that bm's digest matches goodDigest.
52 * @param goodDigest The known good digest to compare to.
53 * @param bm The bitmap to test.
54 */
55static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
56 const SkBitmap& bm) {
57 SkMD5::Digest digest;
58 md5(bm, &digest);
59 REPORTER_ASSERT(r, digest == goodDigest);
60}
61
scroggod1bc5742015-08-12 08:31:44 -070062/**
63 * Test decoding an SkCodec to a particular SkImageInfo.
64 *
halcanary96fcdcc2015-08-27 07:41:13 -070065 * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr,
scroggod1bc5742015-08-12 08:31:44 -070066 * the resulting decode should match.
67 */
scroggo7b5e5532016-02-04 06:14:24 -080068template<typename Codec>
69static void test_info(skiatest::Reporter* r, Codec* codec, const SkImageInfo& info,
scroggod1bc5742015-08-12 08:31:44 -070070 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
71 SkBitmap bm;
72 bm.allocPixels(info);
73 SkAutoLockPixels autoLockPixels(bm);
74
75 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
76 REPORTER_ASSERT(r, result == expectedResult);
77
78 if (goodDigest) {
79 compare_to_good_digest(r, *goodDigest, bm);
80 }
81}
82
scroggob636b452015-07-22 07:16:20 -070083SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
84 SkIRect rect;
85 do {
86 rect.fLeft = rand->nextRangeU(0, w);
87 rect.fTop = rand->nextRangeU(0, h);
88 rect.fRight = rand->nextRangeU(0, w);
89 rect.fBottom = rand->nextRangeU(0, h);
90 rect.sort();
91 } while (rect.isEmpty());
92 return rect;
93}
94
scroggo8e6c7ad2016-09-16 08:20:38 -070095static void test_incremental_decode(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
96 const SkMD5::Digest& goodDigest) {
97 SkBitmap bm;
98 bm.allocPixels(info);
99 SkAutoLockPixels autoLockPixels(bm);
100
101 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->startIncrementalDecode(info, bm.getPixels(),
102 bm.rowBytes()));
103
104 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->incrementalDecode());
105
106 compare_to_good_digest(r, goodDigest, bm);
107}
108
109// Test in stripes, similar to DM's kStripe_Mode
110static void test_in_stripes(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
111 const SkMD5::Digest& goodDigest) {
112 SkBitmap bm;
113 bm.allocPixels(info);
114 bm.eraseColor(SK_ColorYELLOW);
115
116 const int height = info.height();
117 // Note that if numStripes does not evenly divide height there will be an extra
118 // stripe.
119 const int numStripes = 4;
120
121 if (numStripes > height) {
122 // Image is too small.
123 return;
124 }
125
126 const int stripeHeight = height / numStripes;
127
128 // Iterate through the image twice. Once to decode odd stripes, and once for even.
129 for (int oddEven = 1; oddEven >= 0; oddEven--) {
130 for (int y = oddEven * stripeHeight; y < height; y += 2 * stripeHeight) {
131 SkIRect subset = SkIRect::MakeLTRB(0, y, info.width(),
132 SkTMin(y + stripeHeight, height));
133 SkCodec::Options options;
134 options.fSubset = &subset;
135 if (SkCodec::kSuccess != codec->startIncrementalDecode(info, bm.getAddr(0, y),
136 bm.rowBytes(), &options)) {
137 ERRORF(r, "failed to start incremental decode!\ttop: %i\tbottom%i\n",
138 subset.top(), subset.bottom());
139 return;
140 }
141 if (SkCodec::kSuccess != codec->incrementalDecode()) {
142 ERRORF(r, "failed incremental decode starting from line %i\n", y);
143 return;
144 }
145 }
146 }
147
148 compare_to_good_digest(r, goodDigest, bm);
149}
150
scroggo7b5e5532016-02-04 06:14:24 -0800151template<typename Codec>
152static void test_codec(skiatest::Reporter* r, Codec* codec, SkBitmap& bm, const SkImageInfo& info,
scroggo27c17282015-10-27 08:14:46 -0700153 const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest,
154 const SkMD5::Digest* goodDigest) {
msarette6dd0042015-10-09 11:07:34 -0700155
halcanarya096d7a2015-03-27 12:16:53 -0700156 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -0700157 bm.allocPixels(info);
158 SkAutoLockPixels autoLockPixels(bm);
msarettcc7f3052015-10-05 14:20:27 -0700159
160 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700161 REPORTER_ASSERT(r, result == expectedResult);
halcanarya096d7a2015-03-27 12:16:53 -0700162
msarettcc7f3052015-10-05 14:20:27 -0700163 md5(bm, digest);
164 if (goodDigest) {
165 REPORTER_ASSERT(r, *digest == *goodDigest);
166 }
halcanarya096d7a2015-03-27 12:16:53 -0700167
msarett8ff6ca62015-09-18 12:06:04 -0700168 {
169 // Test decoding to 565
170 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggoba584892016-05-20 13:56:13 -0700171 if (info.alphaType() == kOpaque_SkAlphaType) {
172 // Decoding to 565 should succeed.
173 SkBitmap bm565;
174 bm565.allocPixels(info565);
175 SkAutoLockPixels alp(bm565);
176
177 // This will allow comparison even if the image is incomplete.
178 bm565.eraseColor(SK_ColorBLACK);
179
180 REPORTER_ASSERT(r, expectedResult == codec->getPixels(info565,
181 bm565.getPixels(), bm565.rowBytes()));
182
183 SkMD5::Digest digest565;
184 md5(bm565, &digest565);
185
186 // A dumb client's request for non-opaque should also succeed.
187 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
188 info565 = info565.makeAlphaType(alpha);
189 test_info(r, codec, info565, expectedResult, &digest565);
190 }
191 } else {
192 test_info(r, codec, info565, SkCodec::kInvalidConversion, nullptr);
193 }
194 }
195
196 if (codec->getInfo().colorType() == kGray_8_SkColorType) {
197 SkImageInfo grayInfo = codec->getInfo();
198 SkBitmap grayBm;
199 grayBm.allocPixels(grayInfo);
200 SkAutoLockPixels alp(grayBm);
201
202 grayBm.eraseColor(SK_ColorBLACK);
203
204 REPORTER_ASSERT(r, expectedResult == codec->getPixels(grayInfo,
205 grayBm.getPixels(), grayBm.rowBytes()));
206
207 SkMD5::Digest grayDigest;
208 md5(grayBm, &grayDigest);
209
210 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
211 grayInfo = grayInfo.makeAlphaType(alpha);
212 test_info(r, codec, grayInfo, expectedResult, &grayDigest);
213 }
msarett8ff6ca62015-09-18 12:06:04 -0700214 }
215
216 // Verify that re-decoding gives the same result. It is interesting to check this after
217 // a decode to 565, since choosing to decode to 565 may result in some of the decode
218 // options being modified. These options should return to their defaults on another
219 // decode to kN32, so the new digest should match the old digest.
msarette6dd0042015-10-09 11:07:34 -0700220 test_info(r, codec, info, expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700221
222 {
223 // Check alpha type conversions
224 if (info.alphaType() == kOpaque_SkAlphaType) {
225 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800226 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700227 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800228 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700229 } else {
230 // Decoding to opaque should fail
231 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700232 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700233 SkAlphaType otherAt = info.alphaType();
234 if (kPremul_SkAlphaType == otherAt) {
235 otherAt = kUnpremul_SkAlphaType;
236 } else {
237 otherAt = kPremul_SkAlphaType;
238 }
239 // The other non-opaque alpha type should always succeed, but not match.
msarette6dd0042015-10-09 11:07:34 -0700240 test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700241 }
242 }
msarettcc7f3052015-10-05 14:20:27 -0700243}
244
scroggobed1ed62016-02-11 10:24:55 -0800245static bool supports_partial_scanlines(const char path[]) {
scroggo2c3b2182015-10-09 08:40:59 -0700246 static const char* const exts[] = {
247 "jpg", "jpeg", "png", "webp"
248 "JPG", "JPEG", "PNG", "WEBP"
249 };
250
251 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
252 if (SkStrEndsWith(path, exts[i])) {
253 return true;
254 }
255 }
256 return false;
257}
258
scroggo8e6c7ad2016-09-16 08:20:38 -0700259// FIXME: Break up this giant function
msarettcc7f3052015-10-05 14:20:27 -0700260static void check(skiatest::Reporter* r,
261 const char path[],
262 SkISize size,
263 bool supportsScanlineDecoding,
264 bool supportsSubsetDecoding,
scroggo8e6c7ad2016-09-16 08:20:38 -0700265 bool supportsIncomplete,
266 bool supportsNewScanlineDecoding = false) {
msarettcc7f3052015-10-05 14:20:27 -0700267
Ben Wagner145dbcd2016-11-03 14:40:50 -0400268 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarettcc7f3052015-10-05 14:20:27 -0700269 if (!stream) {
msarettcc7f3052015-10-05 14:20:27 -0700270 return;
271 }
msarette6dd0042015-10-09 11:07:34 -0700272
Ben Wagner145dbcd2016-11-03 14:40:50 -0400273 std::unique_ptr<SkCodec> codec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700274 bool isIncomplete = supportsIncomplete;
275 if (isIncomplete) {
276 size_t size = stream->getLength();
Ben Wagner145dbcd2016-11-03 14:40:50 -0400277 sk_sp<SkData> data((SkData::MakeFromStream(stream.get(), 2 * size / 3)));
reed42943c82016-09-12 12:01:44 -0700278 codec.reset(SkCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700279 } else {
mtklein18300a32016-03-16 13:53:35 -0700280 codec.reset(SkCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700281 }
msarettcc7f3052015-10-05 14:20:27 -0700282 if (!codec) {
283 ERRORF(r, "Unable to decode '%s'", path);
284 return;
285 }
286
287 // Test full image decodes with SkCodec
288 SkMD5::Digest codecDigest;
scroggoef0fed32016-02-18 05:59:25 -0800289 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
msarettcc7f3052015-10-05 14:20:27 -0700290 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700291 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
scroggo7b5e5532016-02-04 06:14:24 -0800292 test_codec(r, codec.get(), bm, info, size, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700293
294 // Scanline decoding follows.
scroggod8d68552016-06-06 11:26:17 -0700295
scroggo8e6c7ad2016-09-16 08:20:38 -0700296 if (supportsNewScanlineDecoding && !isIncomplete) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400297 test_incremental_decode(r, codec.get(), info, codecDigest);
scroggo19b91532016-10-24 09:03:26 -0700298 // This is only supported by codecs that use incremental decoding to
299 // support subset decodes - png and jpeg (once SkJpegCodec is
300 // converted).
301 if (SkStrEndsWith(path, "png") || SkStrEndsWith(path, "PNG")) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400302 test_in_stripes(r, codec.get(), info, codecDigest);
scroggo19b91532016-10-24 09:03:26 -0700303 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700304 }
305
306 // Need to call startScanlineDecode() first.
307 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) == 0);
308 REPORTER_ASSERT(r, !codec->skipScanlines(1));
scroggo46c57472015-09-30 08:57:13 -0700309 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700310 if (supportsScanlineDecoding) {
311 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700312
scroggo46c57472015-09-30 08:57:13 -0700313 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700314
scroggo58421542015-04-01 11:25:20 -0700315 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700316 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
317 if (!isIncomplete) {
318 REPORTER_ASSERT(r, 1 == lines);
319 }
scroggo58421542015-04-01 11:25:20 -0700320 }
321 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700322 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700323 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700324 }
scroggo46c57472015-09-30 08:57:13 -0700325
326 // Cannot continue to decode scanlines beyond the end
327 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700328 == 0);
scroggo46c57472015-09-30 08:57:13 -0700329
330 // Interrupting a scanline decode with a full decode starts from
331 // scratch
332 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700333 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
334 if (!isIncomplete) {
335 REPORTER_ASSERT(r, lines == 1);
336 }
scroggo46c57472015-09-30 08:57:13 -0700337 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700338 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700339 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700340 == 0);
scroggo46c57472015-09-30 08:57:13 -0700341 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700342 == 0);
msarett80803ff2015-10-16 10:54:12 -0700343
344 // Test partial scanline decodes
scroggobed1ed62016-02-11 10:24:55 -0800345 if (supports_partial_scanlines(path) && info.width() >= 3) {
msarett80803ff2015-10-16 10:54:12 -0700346 SkCodec::Options options;
347 int width = info.width();
348 int height = info.height();
349 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
350 options.fSubset = &subset;
351
352 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
353 nullptr, nullptr);
354 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
355
356 for (int y = 0; y < height; y++) {
357 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
358 if (!isIncomplete) {
359 REPORTER_ASSERT(r, 1 == lines);
360 }
361 }
362 }
scroggo58421542015-04-01 11:25:20 -0700363 } else {
scroggo46c57472015-09-30 08:57:13 -0700364 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700365 }
scroggob636b452015-07-22 07:16:20 -0700366
367 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
368 // random subsets.
369 // Do not attempt to decode subsets of an image of only once pixel, since there is no
370 // meaningful subset.
371 if (size.width() * size.height() == 1) {
372 return;
373 }
374
375 SkRandom rand;
376 SkIRect subset;
377 SkCodec::Options opts;
378 opts.fSubset = &subset;
379 for (int i = 0; i < 5; i++) {
380 subset = generate_random_subset(&rand, size.width(), size.height());
381 SkASSERT(!subset.isEmpty());
382 const bool supported = codec->getValidSubset(&subset);
383 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
384
385 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
386 SkBitmap bm;
387 bm.allocPixels(subsetInfo);
388 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700389 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700390
391 if (supportsSubsetDecoding) {
Leon Scroggins III58f100c2016-12-20 09:49:25 -0500392 if (expectedResult == SkCodec::kSuccess) {
393 REPORTER_ASSERT(r, result == expectedResult);
394 } else {
395 SkASSERT(expectedResult == SkCodec::kIncompleteInput);
396 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput
397 || result == SkCodec::kSuccess);
398 }
scroggob636b452015-07-22 07:16:20 -0700399 // Webp is the only codec that supports subsets, and it will have modified the subset
400 // to have even left/top.
401 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
402 } else {
403 // No subsets will work.
404 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
405 }
406 }
msarettcc7f3052015-10-05 14:20:27 -0700407
scroggobed1ed62016-02-11 10:24:55 -0800408 // SkAndroidCodec tests
scroggo8e6c7ad2016-09-16 08:20:38 -0700409 if (supportsScanlineDecoding || supportsSubsetDecoding || supportsNewScanlineDecoding) {
scroggo2c3b2182015-10-09 08:40:59 -0700410
Ben Wagner145dbcd2016-11-03 14:40:50 -0400411 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarettcc7f3052015-10-05 14:20:27 -0700412 if (!stream) {
msarettcc7f3052015-10-05 14:20:27 -0700413 return;
414 }
msarette6dd0042015-10-09 11:07:34 -0700415
Ben Wagner145dbcd2016-11-03 14:40:50 -0400416 std::unique_ptr<SkAndroidCodec> androidCodec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700417 if (isIncomplete) {
418 size_t size = stream->getLength();
Ben Wagner145dbcd2016-11-03 14:40:50 -0400419 sk_sp<SkData> data((SkData::MakeFromStream(stream.get(), 2 * size / 3)));
reed42943c82016-09-12 12:01:44 -0700420 androidCodec.reset(SkAndroidCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700421 } else {
mtklein18300a32016-03-16 13:53:35 -0700422 androidCodec.reset(SkAndroidCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700423 }
scroggo7b5e5532016-02-04 06:14:24 -0800424 if (!androidCodec) {
msarettcc7f3052015-10-05 14:20:27 -0700425 ERRORF(r, "Unable to decode '%s'", path);
426 return;
427 }
428
429 SkBitmap bm;
scroggobed1ed62016-02-11 10:24:55 -0800430 SkMD5::Digest androidCodecDigest;
431 test_codec(r, androidCodec.get(), bm, info, size, expectedResult, &androidCodecDigest,
scroggo7b5e5532016-02-04 06:14:24 -0800432 &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700433 }
434
msarettedd2dcf2016-01-14 13:12:26 -0800435 if (!isIncomplete) {
scroggoef0fed32016-02-18 05:59:25 -0800436 // Test SkCodecImageGenerator
Ben Wagner145dbcd2016-11-03 14:40:50 -0400437 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
438 sk_sp<SkData> fullData(SkData::MakeFromStream(stream.get(), stream->getLength()));
439 std::unique_ptr<SkImageGenerator> gen(
Mike Reed185130c2017-02-15 15:14:16 -0500440 SkCodecImageGenerator::MakeFromEncodedCodec(fullData));
msarettedd2dcf2016-01-14 13:12:26 -0800441 SkBitmap bm;
442 bm.allocPixels(info);
443 SkAutoLockPixels autoLockPixels(bm);
444 REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes()));
445 compare_to_good_digest(r, codecDigest, bm);
scroggoef0fed32016-02-18 05:59:25 -0800446
scroggo8e6c7ad2016-09-16 08:20:38 -0700447#ifndef SK_PNG_DISABLE_TESTS
scroggod8d68552016-06-06 11:26:17 -0700448 // Test using SkFrontBufferedStream, as Android does
bungeman38d909e2016-08-02 14:40:46 -0700449 SkStream* bufferedStream = SkFrontBufferedStream::Create(
450 new SkMemoryStream(std::move(fullData)), SkCodec::MinBufferedBytesNeeded());
scroggod8d68552016-06-06 11:26:17 -0700451 REPORTER_ASSERT(r, bufferedStream);
452 codec.reset(SkCodec::NewFromStream(bufferedStream));
453 REPORTER_ASSERT(r, codec);
454 if (codec) {
455 test_info(r, codec.get(), info, SkCodec::kSuccess, &codecDigest);
scroggoef0fed32016-02-18 05:59:25 -0800456 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700457#endif
msarettedd2dcf2016-01-14 13:12:26 -0800458 }
459
msarette6dd0042015-10-09 11:07:34 -0700460 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
461 if (isIncomplete) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700462 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false,
463 supportsNewScanlineDecoding);
msarettcc7f3052015-10-05 14:20:27 -0700464 }
halcanarya096d7a2015-03-27 12:16:53 -0700465}
466
Leon Scroggins III83926342016-12-06 10:58:02 -0500467DEF_TEST(Codec_wbmp, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700468 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500469}
halcanarya096d7a2015-03-27 12:16:53 -0700470
Leon Scroggins III83926342016-12-06 10:58:02 -0500471DEF_TEST(Codec_webp, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700472 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true, true);
473 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true, true);
474 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500475}
scroggo6f5e6192015-06-18 12:53:43 -0700476
Leon Scroggins III83926342016-12-06 10:58:02 -0500477DEF_TEST(Codec_bmp, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700478 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false, true);
479 check(r, "rle.bmp", SkISize::Make(320, 240), true, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500480}
halcanarya096d7a2015-03-27 12:16:53 -0700481
Leon Scroggins III83926342016-12-06 10:58:02 -0500482DEF_TEST(Codec_ico, r) {
msarette6dd0042015-10-09 11:07:34 -0700483 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700484 // These two tests examine interestingly different behavior:
485 // Decodes an embedded BMP image
msarettbe8216a2015-12-04 08:00:50 -0800486 check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700487 // Decodes an embedded PNG image
scroggo8e6c7ad2016-09-16 08:20:38 -0700488 check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500489}
halcanarya096d7a2015-03-27 12:16:53 -0700490
Leon Scroggins III83926342016-12-06 10:58:02 -0500491DEF_TEST(Codec_gif, r) {
scroggo19b91532016-10-24 09:03:26 -0700492 check(r, "box.gif", SkISize::Make(200, 55), false, false, true, true);
493 check(r, "color_wheel.gif", SkISize::Make(128, 128), false, false, true, true);
msarette6dd0042015-10-09 11:07:34 -0700494 // randPixels.gif is too small to test incomplete
scroggo19b91532016-10-24 09:03:26 -0700495 check(r, "randPixels.gif", SkISize::Make(8, 8), false, false, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500496}
msarett438b2ad2015-04-09 12:43:10 -0700497
Leon Scroggins III83926342016-12-06 10:58:02 -0500498DEF_TEST(Codec_jpg, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700499 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false, true);
500 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700501 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700502 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggo8e6c7ad2016-09-16 08:20:38 -0700503 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700504 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700505 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
Leon Scroggins III83926342016-12-06 10:58:02 -0500506}
msarette16b04a2015-04-15 07:32:19 -0700507
Leon Scroggins III83926342016-12-06 10:58:02 -0500508DEF_TEST(Codec_png, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700509 check(r, "arrow.png", SkISize::Make(187, 312), false, false, true, true);
510 check(r, "baby_tux.png", SkISize::Make(240, 246), false, false, true, true);
511 check(r, "color_wheel.png", SkISize::Make(128, 128), false, false, true, true);
512 // half-transparent-white-pixel.png is too small to test incomplete
513 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), false, false, false, true);
514 check(r, "mandrill_128.png", SkISize::Make(128, 128), false, false, true, true);
515 check(r, "mandrill_16.png", SkISize::Make(16, 16), false, false, true, true);
516 check(r, "mandrill_256.png", SkISize::Make(256, 256), false, false, true, true);
517 check(r, "mandrill_32.png", SkISize::Make(32, 32), false, false, true, true);
518 check(r, "mandrill_512.png", SkISize::Make(512, 512), false, false, true, true);
519 check(r, "mandrill_64.png", SkISize::Make(64, 64), false, false, true, true);
520 check(r, "plane.png", SkISize::Make(250, 126), false, false, true, true);
521 check(r, "plane_interlaced.png", SkISize::Make(250, 126), false, false, true, true);
522 check(r, "randPixels.png", SkISize::Make(8, 8), false, false, true, true);
523 check(r, "yellow_rose.png", SkISize::Make(400, 301), false, false, true, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500524}
yujieqin916de9f2016-01-25 08:26:16 -0800525
yujieqinf236ee42016-02-29 07:14:42 -0800526// Disable RAW tests for Win32.
527#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Leon Scroggins III83926342016-12-06 10:58:02 -0500528DEF_TEST(Codec_raw, r) {
yujieqin916de9f2016-01-25 08:26:16 -0800529 check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
ebrauer46d2aa82016-02-17 08:04:00 -0800530 check(r, "sample_1mp_rotated.dng", SkISize::Make(600, 338), false, false, false);
yujieqin9c7a8a42016-02-05 08:21:19 -0800531 check(r, "dng_with_preview.dng", SkISize::Make(600, 338), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700532}
Leon Scroggins III83926342016-12-06 10:58:02 -0500533#endif
scroggo0a7e69c2015-04-03 07:22:22 -0700534
535static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700536 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700537 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700538 REPORTER_ASSERT(r, !codec);
539
msarett3d9d7a72015-10-21 10:27:10 -0700540 SkAndroidCodec* androidCodec =
541 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
542 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700543}
544
545// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
546// even on failure. Test some bad streams.
547DEF_TEST(Codec_leaks, r) {
548 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
549 const char nonSupportedStream[] = "hello world";
550 // The other strings should look like the beginning of a file type, so we'll call some
551 // internal version of NewFromStream, which must also delete the stream on failure.
552 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
553 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
554 const char emptyWebp[] = "RIFF1234WEBPVP";
555 const char emptyBmp[] = { 'B', 'M' };
556 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
557 const char emptyGif[] = "GIFVER";
558
559 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
560 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
561 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
562 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
563 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
564 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
565 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
566}
msarette16b04a2015-04-15 07:32:19 -0700567
scroggo2c3b2182015-10-09 08:40:59 -0700568DEF_TEST(Codec_null, r) {
scroggobed1ed62016-02-11 10:24:55 -0800569 // Attempting to create an SkCodec or an SkAndroidCodec with null should not
scroggo2c3b2182015-10-09 08:40:59 -0700570 // crash.
571 SkCodec* codec = SkCodec::NewFromStream(nullptr);
572 REPORTER_ASSERT(r, !codec);
573
msarett3d9d7a72015-10-21 10:27:10 -0700574 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
575 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700576}
577
msarette16b04a2015-04-15 07:32:19 -0700578static void test_dimensions(skiatest::Reporter* r, const char path[]) {
579 // Create the codec from the resource file
Ben Wagner145dbcd2016-11-03 14:40:50 -0400580 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarette16b04a2015-04-15 07:32:19 -0700581 if (!stream) {
msarette16b04a2015-04-15 07:32:19 -0700582 return;
583 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400584 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarette16b04a2015-04-15 07:32:19 -0700585 if (!codec) {
586 ERRORF(r, "Unable to create codec '%s'", path);
587 return;
588 }
589
590 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800591 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700592 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700593 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700594 SkImageInfo scaledInfo = codec->getInfo()
595 .makeWH(scaledDims.width(), scaledDims.height())
596 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700597
598 // Set up for the decode
599 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
600 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
601 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
602
msarett3d9d7a72015-10-21 10:27:10 -0700603 SkAndroidCodec::AndroidOptions options;
604 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700605 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700606 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700607 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700608 }
609}
610
611// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
612DEF_TEST(Codec_Dimensions, r) {
613 // JPG
614 test_dimensions(r, "CMYK.jpg");
615 test_dimensions(r, "color_wheel.jpg");
616 test_dimensions(r, "grayscale.jpg");
617 test_dimensions(r, "mandrill_512_q075.jpg");
618 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700619
620 // Decoding small images with very large scaling factors is a potential
621 // source of bugs and crashes. We disable these tests in Gold because
622 // tiny images are not very useful to look at.
623 // Here we make sure that we do not crash or access illegal memory when
624 // performing scaled decodes on small images.
625 test_dimensions(r, "1x1.png");
626 test_dimensions(r, "2x2.png");
627 test_dimensions(r, "3x3.png");
628 test_dimensions(r, "3x1.png");
629 test_dimensions(r, "1x1.png");
630 test_dimensions(r, "16x1.png");
631 test_dimensions(r, "1x16.png");
632 test_dimensions(r, "mandrill_16.png");
633
yujieqin916de9f2016-01-25 08:26:16 -0800634 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800635// Disable RAW tests for Win32.
636#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800637 test_dimensions(r, "sample_1mp.dng");
ebrauer46d2aa82016-02-17 08:04:00 -0800638 test_dimensions(r, "sample_1mp_rotated.dng");
yujieqin9c7a8a42016-02-05 08:21:19 -0800639 test_dimensions(r, "dng_with_preview.dng");
msarett8e49ca32016-01-25 13:10:58 -0800640#endif
msarette16b04a2015-04-15 07:32:19 -0700641}
642
msarettd0375bc2015-08-12 08:08:56 -0700643static void test_invalid(skiatest::Reporter* r, const char path[]) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400644 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett4b17fa32015-04-23 08:53:39 -0700645 if (!stream) {
msarett4b17fa32015-04-23 08:53:39 -0700646 return;
647 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400648 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
halcanary96fcdcc2015-08-27 07:41:13 -0700649 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700650}
msarette16b04a2015-04-15 07:32:19 -0700651
msarett4b17fa32015-04-23 08:53:39 -0700652DEF_TEST(Codec_Empty, r) {
653 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700654 test_invalid(r, "empty_images/zero-dims.gif");
655 test_invalid(r, "empty_images/zero-embedded.ico");
656 test_invalid(r, "empty_images/zero-width.bmp");
657 test_invalid(r, "empty_images/zero-height.bmp");
658 test_invalid(r, "empty_images/zero-width.jpg");
659 test_invalid(r, "empty_images/zero-height.jpg");
660 test_invalid(r, "empty_images/zero-width.png");
661 test_invalid(r, "empty_images/zero-height.png");
662 test_invalid(r, "empty_images/zero-width.wbmp");
663 test_invalid(r, "empty_images/zero-height.wbmp");
664 // This image is an ico with an embedded mask-bmp. This is illegal.
665 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
Matt Sarett5c496172017-02-07 17:01:16 -0500666 // It is illegal for a webp frame to not be fully contained by the canvas.
667 test_invalid(r, "invalid_images/invalid-offset.webp");
Leon Scroggins IIId87fbee2016-12-02 16:47:53 -0500668#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
669 test_invalid(r, "empty_images/zero_height.tiff");
670#endif
msarett4b17fa32015-04-23 08:53:39 -0700671}
msarett99f567e2015-08-05 12:58:26 -0700672
673static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400674 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett99f567e2015-08-05 12:58:26 -0700675 if (!stream) {
msarett99f567e2015-08-05 12:58:26 -0700676 return;
677 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400678 std::unique_ptr<SkCodec> decoder(SkCodec::NewFromStream(stream.release()));
scroggo8e6c7ad2016-09-16 08:20:38 -0700679 if (!decoder) {
680 SkDebugf("Missing codec for %s\n", path);
681 return;
682 }
683
684 const SkImageInfo info = decoder->getInfo().makeColorType(kIndex_8_SkColorType);
halcanary9d524f22016-03-29 09:03:52 -0700685
msarett99f567e2015-08-05 12:58:26 -0700686 // This should return kSuccess because kIndex8 is supported.
687 SkPMColor colorStorage[256];
688 int colorCount;
scroggo8e6c7ad2016-09-16 08:20:38 -0700689 SkCodec::Result result = decoder->startScanlineDecode(info, nullptr, colorStorage,
690 &colorCount);
691 if (SkCodec::kSuccess == result) {
692 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
693 // colorPtr and a valid colorCountPtr.
694 result = decoder->startScanlineDecode(info, nullptr, nullptr, nullptr);
695 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
696 result = decoder->startScanlineDecode(info);
697 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
698 } else if (SkCodec::kUnimplemented == result) {
699 // New method should be supported:
700 SkBitmap bm;
701 sk_sp<SkColorTable> colorTable(new SkColorTable(colorStorage, 256));
702 bm.allocPixels(info, nullptr, colorTable.get());
703 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes(), nullptr,
704 colorStorage, &colorCount);
705 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
706 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
707 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
708 } else {
709 // The test is uninteresting if kIndex8 is not supported
710 ERRORF(r, "Should not call test_invalid_parameters for non-Index8 file: %s\n", path);
msarett99f567e2015-08-05 12:58:26 -0700711 return;
712 }
713
msarett99f567e2015-08-05 12:58:26 -0700714}
715
716DEF_TEST(Codec_Params, r) {
717 test_invalid_parameters(r, "index8.png");
718 test_invalid_parameters(r, "mandrill.wbmp");
719}
scroggocf98fa92015-11-23 08:14:40 -0800720
scroggo8e6c7ad2016-09-16 08:20:38 -0700721#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
722
723#ifndef SK_PNG_DISABLE_TESTS // reading chunks does not work properly with older versions.
724 // It does not appear that anyone in Google3 is reading chunks.
725
scroggocf98fa92015-11-23 08:14:40 -0800726static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
727 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
728 if (!sk_stream->write(data, len)) {
729 png_error(png_ptr, "sk_write_fn Error!");
730 }
731}
732
scroggocf98fa92015-11-23 08:14:40 -0800733DEF_TEST(Codec_pngChunkReader, r) {
734 // Create a dummy bitmap. Use unpremul RGBA for libpng.
735 SkBitmap bm;
736 const int w = 1;
737 const int h = 1;
738 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
739 kUnpremul_SkAlphaType);
740 bm.setInfo(bmInfo);
741 bm.allocPixels();
742 bm.eraseColor(SK_ColorBLUE);
743 SkMD5::Digest goodDigest;
744 md5(bm, &goodDigest);
745
746 // Write to a png file.
747 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
748 REPORTER_ASSERT(r, png);
749 if (!png) {
750 return;
751 }
752
753 png_infop info = png_create_info_struct(png);
754 REPORTER_ASSERT(r, info);
755 if (!info) {
756 png_destroy_write_struct(&png, nullptr);
757 return;
758 }
759
760 if (setjmp(png_jmpbuf(png))) {
761 ERRORF(r, "failed writing png");
762 png_destroy_write_struct(&png, &info);
763 return;
764 }
765
766 SkDynamicMemoryWStream wStream;
767 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
768
769 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
770 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
771 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
772
773 // Create some chunks that match the Android framework's use.
774 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800775 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
776 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
777 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800778 };
779
780 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
781 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
782#if PNG_LIBPNG_VER < 10600
783 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800784 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
785 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800786#endif
787
788 png_write_info(png, info);
789
790 for (int j = 0; j < h; j++) {
791 png_bytep row = (png_bytep)(bm.getAddr(0, j));
792 png_write_rows(png, &row, 1);
793 }
794 png_write_end(png, info);
795 png_destroy_write_struct(&png, &info);
796
797 class ChunkReader : public SkPngChunkReader {
798 public:
799 ChunkReader(skiatest::Reporter* r)
800 : fReporter(r)
801 {
802 this->reset();
803 }
804
805 bool readChunk(const char tag[], const void* data, size_t length) override {
806 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
807 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
808 // Tag matches. This should have been the first time we see it.
809 REPORTER_ASSERT(fReporter, !fSeen[i]);
810 fSeen[i] = true;
811
812 // Data and length should match
813 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
814 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
815 (const char*) gUnknowns[i].data));
816 return true;
817 }
818 }
819 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
820 return true;
821 }
822
823 bool allHaveBeenSeen() {
824 bool ret = true;
825 for (auto seen : fSeen) {
826 ret &= seen;
827 }
828 return ret;
829 }
830
831 void reset() {
832 sk_bzero(fSeen, sizeof(fSeen));
833 }
834
835 private:
836 skiatest::Reporter* fReporter; // Unowned
837 bool fSeen[3];
838 };
839
840 ChunkReader chunkReader(r);
841
842 // Now read the file with SkCodec.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400843 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(wStream.detachAsData(), &chunkReader));
scroggocf98fa92015-11-23 08:14:40 -0800844 REPORTER_ASSERT(r, codec);
845 if (!codec) {
846 return;
847 }
848
849 // Now compare to the original.
850 SkBitmap decodedBm;
851 decodedBm.setInfo(codec->getInfo());
852 decodedBm.allocPixels();
853 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
854 decodedBm.rowBytes());
855 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
856
857 if (decodedBm.colorType() != bm.colorType()) {
858 SkBitmap tmp;
859 bool success = decodedBm.copyTo(&tmp, bm.colorType());
860 REPORTER_ASSERT(r, success);
861 if (!success) {
862 return;
863 }
864
865 tmp.swap(decodedBm);
866 }
867
868 compare_to_good_digest(r, goodDigest, decodedBm);
869 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
870
871 // Decoding again will read the chunks again.
872 chunkReader.reset();
873 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
874 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
875 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
876 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
877}
scroggo8e6c7ad2016-09-16 08:20:38 -0700878#endif // SK_PNG_DISABLE_TESTS
scroggocf98fa92015-11-23 08:14:40 -0800879#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800880
scroggodb30be22015-12-08 18:54:13 -0800881// Stream that can only peek up to a limit
882class LimitedPeekingMemStream : public SkStream {
883public:
reed42943c82016-09-12 12:01:44 -0700884 LimitedPeekingMemStream(sk_sp<SkData> data, size_t limit)
885 : fStream(std::move(data))
scroggodb30be22015-12-08 18:54:13 -0800886 , fLimit(limit) {}
887
888 size_t peek(void* buf, size_t bytes) const override {
889 return fStream.peek(buf, SkTMin(bytes, fLimit));
890 }
891 size_t read(void* buf, size_t bytes) override {
892 return fStream.read(buf, bytes);
893 }
894 bool rewind() override {
895 return fStream.rewind();
896 }
897 bool isAtEnd() const override {
msarettff2a6c82016-09-07 11:23:28 -0700898 return fStream.isAtEnd();
scroggodb30be22015-12-08 18:54:13 -0800899 }
900private:
901 SkMemoryStream fStream;
902 const size_t fLimit;
903};
904
yujieqinf236ee42016-02-29 07:14:42 -0800905// Disable RAW tests for Win32.
906#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin9c7a8a42016-02-05 08:21:19 -0800907// Test that the RawCodec works also for not asset stream. This will test the code path using
908// SkRawBufferedStream instead of SkRawAssetStream.
yujieqin9c7a8a42016-02-05 08:21:19 -0800909DEF_TEST(Codec_raw_notseekable, r) {
910 const char* path = "dng_with_preview.dng";
911 SkString fullPath(GetResourcePath(path));
bungeman38d909e2016-08-02 14:40:46 -0700912 sk_sp<SkData> data(SkData::MakeFromFileName(fullPath.c_str()));
yujieqin9c7a8a42016-02-05 08:21:19 -0800913 if (!data) {
914 SkDebugf("Missing resource '%s'\n", path);
915 return;
916 }
917
Ben Wagner145dbcd2016-11-03 14:40:50 -0400918 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(std::move(data))));
yujieqin9c7a8a42016-02-05 08:21:19 -0800919 REPORTER_ASSERT(r, codec);
920
921 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
922}
923#endif
924
scroggodb30be22015-12-08 18:54:13 -0800925// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
926// + rewind() and succeed.
927DEF_TEST(Codec_webp_peek, r) {
928 const char* path = "baby_tux.webp";
929 SkString fullPath(GetResourcePath(path));
reedfde05112016-03-11 13:02:28 -0800930 auto data = SkData::MakeFromFileName(fullPath.c_str());
scroggodb30be22015-12-08 18:54:13 -0800931 if (!data) {
932 SkDebugf("Missing resource '%s'\n", path);
933 return;
934 }
935
936 // The limit is less than webp needs to peek or read.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400937 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(
reed42943c82016-09-12 12:01:44 -0700938 new LimitedPeekingMemStream(data, 25)));
scroggodb30be22015-12-08 18:54:13 -0800939 REPORTER_ASSERT(r, codec);
940
scroggo7b5e5532016-02-04 06:14:24 -0800941 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800942
943 // Similarly, a stream which does not peek should still succeed.
reed42943c82016-09-12 12:01:44 -0700944 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0)));
scroggodb30be22015-12-08 18:54:13 -0800945 REPORTER_ASSERT(r, codec);
946
scroggo7b5e5532016-02-04 06:14:24 -0800947 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800948}
949
msarett7f7ec202016-03-01 12:12:27 -0800950// SkCodec's wbmp decoder was initially unnecessarily restrictive.
951// It required the second byte to be zero. The wbmp specification allows
952// a couple of bits to be 1 (so long as they do not overlap with 0x9F).
953// Test that SkCodec now supports an image with these bits set.
Leon Scroggins III83926342016-12-06 10:58:02 -0500954DEF_TEST(Codec_wbmp_restrictive, r) {
scroggob9a1e342015-11-30 06:25:31 -0800955 const char* path = "mandrill.wbmp";
Ben Wagner145dbcd2016-11-03 14:40:50 -0400956 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
scroggob9a1e342015-11-30 06:25:31 -0800957 if (!stream) {
scroggob9a1e342015-11-30 06:25:31 -0800958 return;
959 }
960
961 // Modify the stream to contain a second byte with some bits set.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400962 auto data = SkCopyStreamToData(stream.get());
scroggob9a1e342015-11-30 06:25:31 -0800963 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
964 writeableData[1] = static_cast<uint8_t>(~0x9F);
965
msarett7f7ec202016-03-01 12:12:27 -0800966 // SkCodec should support this.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400967 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
scroggob9a1e342015-11-30 06:25:31 -0800968 REPORTER_ASSERT(r, codec);
969 if (!codec) {
970 return;
971 }
scroggo7b5e5532016-02-04 06:14:24 -0800972 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800973}
scroggodb30be22015-12-08 18:54:13 -0800974
975// wbmp images have a header that can be arbitrarily large, depending on the
976// size of the image. We cap the size at 65535, meaning we only need to look at
977// 8 bytes to determine whether we can read the image. This is important
978// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
979// image is a wbmp.
980DEF_TEST(Codec_wbmp_max_size, r) {
981 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
982 0x83, 0xFF, 0x7F, // W: 65535
983 0x83, 0xFF, 0x7F }; // H: 65535
Ben Wagner145dbcd2016-11-03 14:40:50 -0400984 std::unique_ptr<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
985 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -0800986
987 REPORTER_ASSERT(r, codec);
988 if (!codec) return;
989
990 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
991 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
992
993 // Now test an image which is too big. Any image with a larger header (i.e.
994 // has bigger width/height) is also too big.
995 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
996 0x84, 0x80, 0x00, // W: 65536
997 0x84, 0x80, 0x00 }; // H: 65536
998 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
mtklein18300a32016-03-16 13:53:35 -0700999 codec.reset(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -08001000
1001 REPORTER_ASSERT(r, !codec);
1002}
msarett2812f032016-07-18 15:56:08 -07001003
1004DEF_TEST(Codec_jpeg_rewind, r) {
1005 const char* path = "mandrill_512_q075.jpg";
Leon Scroggins III42886572017-01-27 13:16:28 -05001006 sk_sp<SkData> data(GetResourceAsData(path));
1007 if (!data) {
msarett2812f032016-07-18 15:56:08 -07001008 return;
1009 }
Leon Scroggins III42886572017-01-27 13:16:28 -05001010
1011 data = SkData::MakeSubset(data.get(), 0, data->size() / 2);
1012 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromData(data));
msarett2812f032016-07-18 15:56:08 -07001013 if (!codec) {
1014 ERRORF(r, "Unable to create codec '%s'.", path);
1015 return;
1016 }
1017
1018 const int width = codec->getInfo().width();
1019 const int height = codec->getInfo().height();
1020 size_t rowBytes = sizeof(SkPMColor) * width;
1021 SkAutoMalloc pixelStorage(height * rowBytes);
1022
1023 // Perform a sampled decode.
1024 SkAndroidCodec::AndroidOptions opts;
1025 opts.fSampleSize = 12;
Leon Scroggins III42886572017-01-27 13:16:28 -05001026 auto sampledInfo = codec->getInfo().makeWH(width / 12, height / 12);
1027 auto result = codec->getAndroidPixels(sampledInfo, pixelStorage.get(), rowBytes, &opts);
1028 REPORTER_ASSERT(r, SkCodec::kIncompleteInput == result);
msarett2812f032016-07-18 15:56:08 -07001029
1030 // Rewind the codec and perform a full image decode.
Matt Sarett74b16ed2017-01-25 11:58:11 -05001031 result = codec->getPixels(codec->getInfo(), pixelStorage.get(), rowBytes);
Leon Scroggins III42886572017-01-27 13:16:28 -05001032 REPORTER_ASSERT(r, SkCodec::kIncompleteInput == result);
Matt Sarett74b16ed2017-01-25 11:58:11 -05001033
1034 // Now perform a subset decode.
1035 {
1036 opts.fSampleSize = 1;
1037 SkIRect subset = SkIRect::MakeWH(100, 100);
1038 opts.fSubset = &subset;
1039 result = codec->getAndroidPixels(codec->getInfo().makeWH(100, 100), pixelStorage.get(),
1040 rowBytes, &opts);
Leon Scroggins III42886572017-01-27 13:16:28 -05001041 // Though we only have half the data, it is enough to decode this subset.
Matt Sarett74b16ed2017-01-25 11:58:11 -05001042 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1043 }
1044
1045 // Perform another full image decode. ASAN will detect if we look at the subset when it is
1046 // out of scope. This would happen if we depend on the old state in the codec.
Leon Scroggins III42886572017-01-27 13:16:28 -05001047 // This tests two layers of bugs: both SkJpegCodec::readRows and SkCodec::fillIncompleteImage
1048 // used to look at the old subset.
Matt Sarett74b16ed2017-01-25 11:58:11 -05001049 opts.fSubset = nullptr;
1050 result = codec->getAndroidPixels(codec->getInfo(), pixelStorage.get(), rowBytes, &opts);
Leon Scroggins III42886572017-01-27 13:16:28 -05001051 REPORTER_ASSERT(r, SkCodec::kIncompleteInput == result);
msarett2812f032016-07-18 15:56:08 -07001052}
msarett549ca322016-08-17 08:54:08 -07001053
msarett35bb74b2016-08-22 07:41:28 -07001054static void check_color_xform(skiatest::Reporter* r, const char* path) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001055 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(GetResourceAsStream(path)));
msarett35bb74b2016-08-22 07:41:28 -07001056
1057 SkAndroidCodec::AndroidOptions opts;
1058 opts.fSampleSize = 3;
1059 const int subsetWidth = codec->getInfo().width() / 2;
1060 const int subsetHeight = codec->getInfo().height() / 2;
1061 SkIRect subset = SkIRect::MakeWH(subsetWidth, subsetHeight);
1062 opts.fSubset = &subset;
1063
1064 const int dstWidth = subsetWidth / opts.fSampleSize;
1065 const int dstHeight = subsetHeight / opts.fSampleSize;
1066 sk_sp<SkData> data = SkData::MakeFromFileName(
1067 GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str());
Brian Osman526972e2016-10-24 09:24:02 -04001068 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeICC(data->data(), data->size());
msarett35bb74b2016-08-22 07:41:28 -07001069 SkImageInfo dstInfo = codec->getInfo().makeWH(dstWidth, dstHeight)
1070 .makeColorType(kN32_SkColorType)
1071 .makeColorSpace(colorSpace);
1072
1073 size_t rowBytes = dstInfo.minRowBytes();
1074 SkAutoMalloc pixelStorage(dstInfo.getSafeSize(rowBytes));
1075 SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get(), rowBytes, &opts);
1076 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1077}
1078
1079DEF_TEST(Codec_ColorXform, r) {
1080 check_color_xform(r, "mandrill_512_q075.jpg");
1081 check_color_xform(r, "mandrill_512.png");
1082}
1083
msarettf17b71f2016-09-12 14:30:03 -07001084static bool color_type_match(SkColorType origColorType, SkColorType codecColorType) {
1085 switch (origColorType) {
1086 case kRGBA_8888_SkColorType:
1087 case kBGRA_8888_SkColorType:
1088 return kRGBA_8888_SkColorType == codecColorType ||
1089 kBGRA_8888_SkColorType == codecColorType;
1090 default:
1091 return origColorType == codecColorType;
1092 }
1093}
1094
1095static bool alpha_type_match(SkAlphaType origAlphaType, SkAlphaType codecAlphaType) {
1096 switch (origAlphaType) {
1097 case kUnpremul_SkAlphaType:
1098 case kPremul_SkAlphaType:
1099 return kUnpremul_SkAlphaType == codecAlphaType ||
1100 kPremul_SkAlphaType == codecAlphaType;
1101 default:
1102 return origAlphaType == codecAlphaType;
1103 }
1104}
1105
1106static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const SkImageInfo& info) {
1107 SkBitmap bm1;
1108 SkPMColor colors[256];
Hal Canary342b7ac2016-11-04 11:49:42 -04001109 sk_sp<SkColorTable> colorTable1(new SkColorTable(colors, 256));
msarettf17b71f2016-09-12 14:30:03 -07001110 bm1.allocPixels(info, nullptr, colorTable1.get());
1111 int numColors;
1112 SkCodec::Result result = origCodec->getPixels(info, bm1.getPixels(), bm1.rowBytes(), nullptr,
1113 const_cast<SkPMColor*>(colorTable1->readColors()),
1114 &numColors);
1115 // This will fail to update colorTable1->count() but is fine for the purpose of this test.
1116 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarett9b09cd82016-08-29 14:47:49 -07001117
1118 // Encode the image to png.
1119 sk_sp<SkData> data =
Hal Canarydb683012016-11-23 08:55:18 -07001120 sk_sp<SkData>(sk_tool_utils::EncodeImageToData(bm1, SkEncodedImageFormat::kPNG, 100));
msarett9b09cd82016-08-29 14:47:49 -07001121
Ben Wagner145dbcd2016-11-03 14:40:50 -04001122 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
msarettf17b71f2016-09-12 14:30:03 -07001123 REPORTER_ASSERT(r, color_type_match(info.colorType(), codec->getInfo().colorType()));
1124 REPORTER_ASSERT(r, alpha_type_match(info.alphaType(), codec->getInfo().alphaType()));
msarett9b09cd82016-08-29 14:47:49 -07001125
1126 SkBitmap bm2;
Hal Canary342b7ac2016-11-04 11:49:42 -04001127 sk_sp<SkColorTable> colorTable2(new SkColorTable(colors, 256));
msarettf17b71f2016-09-12 14:30:03 -07001128 bm2.allocPixels(info, nullptr, colorTable2.get());
1129 result = codec->getPixels(info, bm2.getPixels(), bm2.rowBytes(), nullptr,
1130 const_cast<SkPMColor*>(colorTable2->readColors()), &numColors);
msarett9b09cd82016-08-29 14:47:49 -07001131 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1132
1133 SkMD5::Digest d1, d2;
1134 md5(bm1, &d1);
1135 md5(bm2, &d2);
1136 REPORTER_ASSERT(r, d1 == d2);
1137}
1138
1139DEF_TEST(Codec_PngRoundTrip, r) {
msarett549ca322016-08-17 08:54:08 -07001140 const char* path = "mandrill_512_q075.jpg";
Ben Wagner145dbcd2016-11-03 14:40:50 -04001141 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
1142 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett549ca322016-08-17 08:54:08 -07001143
msarettf17b71f2016-09-12 14:30:03 -07001144 SkColorType colorTypesOpaque[] = {
1145 kRGB_565_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1146 };
1147 for (SkColorType colorType : colorTypesOpaque) {
1148 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType);
1149 check_round_trip(r, codec.get(), newInfo);
1150 }
1151
msarett9b09cd82016-08-29 14:47:49 -07001152 path = "grayscale.jpg";
bungemanf93d7112016-09-16 06:24:20 -07001153 stream.reset(GetResourceAsStream(path));
msarett9b09cd82016-08-29 14:47:49 -07001154 codec.reset(SkCodec::NewFromStream(stream.release()));
msarettf17b71f2016-09-12 14:30:03 -07001155 check_round_trip(r, codec.get(), codec->getInfo());
1156
1157 path = "yellow_rose.png";
bungemanf93d7112016-09-16 06:24:20 -07001158 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001159 codec.reset(SkCodec::NewFromStream(stream.release()));
1160
1161 SkColorType colorTypesWithAlpha[] = {
1162 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1163 };
1164 SkAlphaType alphaTypes[] = {
1165 kUnpremul_SkAlphaType, kPremul_SkAlphaType
1166 };
1167 for (SkColorType colorType : colorTypesWithAlpha) {
1168 for (SkAlphaType alphaType : alphaTypes) {
1169 // Set color space to nullptr because color correct premultiplies do not round trip.
1170 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType)
1171 .makeAlphaType(alphaType)
1172 .makeColorSpace(nullptr);
1173 check_round_trip(r, codec.get(), newInfo);
1174 }
1175 }
1176
1177 path = "index8.png";
bungemanf93d7112016-09-16 06:24:20 -07001178 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001179 codec.reset(SkCodec::NewFromStream(stream.release()));
1180
1181 for (SkAlphaType alphaType : alphaTypes) {
1182 SkImageInfo newInfo = codec->getInfo().makeAlphaType(alphaType)
1183 .makeColorSpace(nullptr);
1184 check_round_trip(r, codec.get(), newInfo);
1185 }
msarett549ca322016-08-17 08:54:08 -07001186}
msarett2ecc35f2016-09-08 11:55:16 -07001187
1188static void test_conversion_possible(skiatest::Reporter* r, const char* path,
scroggo8e6c7ad2016-09-16 08:20:38 -07001189 bool supportsScanlineDecoder,
1190 bool supportsIncrementalDecoder) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001191 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
Leon Scroggins IIIc9942a12017-01-30 09:59:28 -05001192 if (!stream) {
1193 return;
1194 }
1195
Ben Wagner145dbcd2016-11-03 14:40:50 -04001196 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
Leon Scroggins IIIc9942a12017-01-30 09:59:28 -05001197 if (!codec) {
1198 ERRORF(r, "failed to create a codec for %s", path);
1199 return;
1200 }
1201
msarett2ecc35f2016-09-08 11:55:16 -07001202 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType);
1203
1204 SkBitmap bm;
1205 bm.allocPixels(infoF16);
1206 SkCodec::Result result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1207 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001208
1209 result = codec->startScanlineDecode(infoF16);
1210 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001211 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == 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::kInvalidConversion == result);
1219 } else {
1220 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001221 }
1222
raftias94888332016-10-18 10:02:51 -07001223 SkASSERT(SkColorSpace_Base::Type::kXYZ == as_CSB(infoF16.colorSpace())->type());
1224 SkColorSpace_XYZ* csXYZ = static_cast<SkColorSpace_XYZ*>(infoF16.colorSpace());
1225 infoF16 = infoF16.makeColorSpace(csXYZ->makeLinearGamma());
msarett2ecc35f2016-09-08 11:55:16 -07001226 result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1227 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001228 result = codec->startScanlineDecode(infoF16);
1229 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001230 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001231 } else {
1232 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1233 }
1234
1235 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1236 if (supportsIncrementalDecoder) {
1237 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1238 } else {
1239 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001240 }
1241}
1242
1243DEF_TEST(Codec_F16ConversionPossible, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -07001244 test_conversion_possible(r, "color_wheel.webp", false, false);
1245 test_conversion_possible(r, "mandrill_512_q075.jpg", true, false);
1246 test_conversion_possible(r, "yellow_rose.png", false, true);
1247}
1248
scroggo19b91532016-10-24 09:03:26 -07001249static void decode_frame(skiatest::Reporter* r, SkCodec* codec, size_t frame) {
1250 SkBitmap bm;
1251 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1252 bm.allocPixels(info);
1253
1254 SkCodec::Options opts;
1255 opts.fFrameIndex = frame;
1256 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->getPixels(info,
1257 bm.getPixels(), bm.rowBytes(), &opts, nullptr, nullptr));
1258}
1259
1260// For an animated image, we should only read enough to decode the requested
1261// frame if the client never calls getFrameInfo.
1262DEF_TEST(Codec_skipFullParse, r) {
1263 auto path = "test640x479.gif";
1264 SkStream* stream(GetResourceAsStream(path));
1265 if (!stream) {
1266 return;
1267 }
1268
1269 // Note that we cheat and hold on to the stream pointer, but SkCodec will
1270 // take ownership. We will not refer to the stream after the SkCodec
1271 // deletes it.
1272 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
1273 if (!codec) {
1274 ERRORF(r, "Failed to create codec for %s", path);
1275 return;
1276 }
1277
1278 REPORTER_ASSERT(r, stream->hasPosition());
1279 const size_t sizePosition = stream->getPosition();
1280 REPORTER_ASSERT(r, stream->hasLength() && sizePosition < stream->getLength());
1281
1282 // This should read more of the stream, but not the whole stream.
1283 decode_frame(r, codec.get(), 0);
1284 const size_t positionAfterFirstFrame = stream->getPosition();
1285 REPORTER_ASSERT(r, positionAfterFirstFrame > sizePosition
1286 && positionAfterFirstFrame < stream->getLength());
1287
1288 // Again, this should read more of the stream.
1289 decode_frame(r, codec.get(), 2);
1290 const size_t positionAfterThirdFrame = stream->getPosition();
1291 REPORTER_ASSERT(r, positionAfterThirdFrame > positionAfterFirstFrame
1292 && positionAfterThirdFrame < stream->getLength());
1293
1294 // This does not need to read any more of the stream, since it has already
1295 // parsed the second frame.
1296 decode_frame(r, codec.get(), 1);
1297 REPORTER_ASSERT(r, stream->getPosition() == positionAfterThirdFrame);
1298
1299 // This should read the rest of the frames.
1300 decode_frame(r, codec.get(), 3);
1301 const size_t finalPosition = stream->getPosition();
1302 REPORTER_ASSERT(r, finalPosition > positionAfterThirdFrame);
1303
1304 // There may be more data in the stream.
1305 auto frameInfo = codec->getFrameInfo();
1306 REPORTER_ASSERT(r, frameInfo.size() == 4);
1307 REPORTER_ASSERT(r, stream->getPosition() >= finalPosition);
1308}
1309
scroggo8e6c7ad2016-09-16 08:20:38 -07001310// Only rewinds up to a limit.
1311class LimitedRewindingStream : public SkStream {
1312public:
1313 static SkStream* Make(const char path[], size_t limit) {
1314 SkStream* stream = GetResourceAsStream(path);
1315 if (!stream) {
1316 return nullptr;
1317 }
1318 return new LimitedRewindingStream(stream, limit);
1319 }
1320
1321 size_t read(void* buffer, size_t size) override {
1322 const size_t bytes = fStream->read(buffer, size);
1323 fPosition += bytes;
1324 return bytes;
1325 }
1326
1327 bool isAtEnd() const override {
1328 return fStream->isAtEnd();
1329 }
1330
1331 bool rewind() override {
1332 if (fPosition <= fLimit && fStream->rewind()) {
1333 fPosition = 0;
1334 return true;
1335 }
1336
1337 return false;
1338 }
1339
1340private:
Ben Wagner145dbcd2016-11-03 14:40:50 -04001341 std::unique_ptr<SkStream> fStream;
1342 const size_t fLimit;
1343 size_t fPosition;
scroggo8e6c7ad2016-09-16 08:20:38 -07001344
1345 LimitedRewindingStream(SkStream* stream, size_t limit)
1346 : fStream(stream)
1347 , fLimit(limit)
1348 , fPosition(0)
1349 {
1350 SkASSERT(fStream);
1351 }
1352};
1353
1354DEF_TEST(Codec_fallBack, r) {
1355 // SkAndroidCodec needs to be able to fall back to scanline decoding
1356 // if incremental decoding does not work. Make sure this does not
1357 // require a rewind.
1358
1359 // Formats that currently do not support incremental decoding
1360 auto files = {
scroggo8e6c7ad2016-09-16 08:20:38 -07001361 "CMYK.jpg",
1362 "color_wheel.ico",
1363 "mandrill.wbmp",
1364 "randPixels.bmp",
1365 };
1366 for (auto file : files) {
1367 SkStream* stream = LimitedRewindingStream::Make(file, 14);
1368 if (!stream) {
1369 SkDebugf("Missing resources (%s). Set --resourcePath.\n", file);
1370 return;
1371 }
1372
Ben Wagner145dbcd2016-11-03 14:40:50 -04001373 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
scroggo8e6c7ad2016-09-16 08:20:38 -07001374 if (!codec) {
1375 ERRORF(r, "Failed to create codec for %s,", file);
1376 continue;
1377 }
1378
1379 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
1380 SkBitmap bm;
1381 bm.allocPixels(info);
1382
1383 if (SkCodec::kUnimplemented != codec->startIncrementalDecode(info, bm.getPixels(),
1384 bm.rowBytes())) {
1385 ERRORF(r, "Is scanline decoding now implemented for %s?", file);
1386 continue;
1387 }
1388
1389 // Scanline decoding should not require a rewind.
1390 SkCodec::Result result = codec->startScanlineDecode(info);
1391 if (SkCodec::kSuccess != result) {
1392 ERRORF(r, "Scanline decoding failed for %s with %i", file, result);
1393 }
1394 }
msarett2ecc35f2016-09-08 11:55:16 -07001395}
scroggoc46cdd42016-10-10 06:45:32 -07001396
1397// This test verifies that we fixed an assert statement that fired when reusing a png codec
1398// after scaling.
1399DEF_TEST(Codec_reusePng, r) {
1400 std::unique_ptr<SkStream> stream(GetResourceAsStream("plane.png"));
1401 if (!stream) {
1402 return;
1403 }
1404
1405 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
1406 if (!codec) {
1407 ERRORF(r, "Failed to create codec\n");
1408 return;
1409 }
1410
1411 SkAndroidCodec::AndroidOptions opts;
1412 opts.fSampleSize = 5;
1413 auto size = codec->getSampledDimensions(opts.fSampleSize);
1414 auto info = codec->getInfo().makeWH(size.fWidth, size.fHeight).makeColorType(kN32_SkColorType);
1415 SkBitmap bm;
1416 bm.allocPixels(info);
1417 auto result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1418 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1419
1420 info = codec->getInfo().makeColorType(kN32_SkColorType);
1421 bm.allocPixels(info);
1422 opts.fSampleSize = 1;
1423 result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1424 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1425}
scroggoe61b3b42016-10-10 07:17:32 -07001426
1427DEF_TEST(Codec_rowsDecoded, r) {
1428 auto file = "plane_interlaced.png";
1429 std::unique_ptr<SkStream> stream(GetResourceAsStream(file));
1430 if (!stream) {
1431 return;
1432 }
1433
1434 // This is enough to read the header etc, but no rows.
1435 auto data = SkData::MakeFromStream(stream.get(), 99);
1436 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
1437 if (!codec) {
1438 ERRORF(r, "Failed to create codec\n");
1439 return;
1440 }
1441
1442 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1443 SkBitmap bm;
1444 bm.allocPixels(info);
1445 auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
1446 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1447
1448 // This is an arbitrary value. The important fact is that it is not zero, and rowsDecoded
1449 // should get set to zero by incrementalDecode.
1450 int rowsDecoded = 77;
1451 result = codec->incrementalDecode(&rowsDecoded);
1452 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput);
1453 REPORTER_ASSERT(r, rowsDecoded == 0);
1454}
Matt Sarett29121eb2016-10-17 14:32:46 -04001455
Matt Sarett8a4e9c52016-10-25 14:24:50 -04001456static void test_invalid_images(skiatest::Reporter* r, const char* path, bool shouldSucceed) {
Matt Sarett29121eb2016-10-17 14:32:46 -04001457 SkBitmap bitmap;
Matt Sarett8a4e9c52016-10-25 14:24:50 -04001458 const bool success = GetResourceAsBitmap(path, &bitmap);
1459 REPORTER_ASSERT(r, success == shouldSucceed);
1460}
1461
1462DEF_TEST(Codec_InvalidImages, r) {
1463 // ASAN will complain if there is an issue.
1464 test_invalid_images(r, "invalid_images/int_overflow.ico", false);
1465 test_invalid_images(r, "invalid_images/skbug5887.gif", true);
Matt Sarettdbdf6d22016-11-08 15:26:56 -05001466 test_invalid_images(r, "invalid_images/many-progressive-scans.jpg", false);
Matt Sarett29121eb2016-10-17 14:32:46 -04001467}
Leon Scroggins III56e32092016-12-12 17:10:46 -05001468
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -05001469DEF_TEST(Codec_InvalidBmp, r) {
Leon Scroggins III0354c622017-02-24 15:33:24 -05001470 // These files report values that have caused problems with SkFILEStreams.
1471 // They are invalid, and should not create SkCodecs.
1472 for (auto* bmp : { "b33651913.bmp", "b34778578.bmp" } ) {
1473 SkString path = SkOSPath::Join("invalid_images", bmp);
1474 path = GetResourcePath(path.c_str());
1475 std::unique_ptr<SkFILEStream> stream(new SkFILEStream(path.c_str()));
1476 if (!stream->isValid()) {
1477 return;
1478 }
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -05001479
Leon Scroggins III0354c622017-02-24 15:33:24 -05001480 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
1481 REPORTER_ASSERT(r, !codec);
1482 }
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -05001483}
1484
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -05001485DEF_TEST(Codec_InvalidRLEBmp, r) {
1486 auto* stream = GetResourceAsStream("invalid_images/b33251605.bmp");
1487 if (!stream) {
1488 return;
1489 }
1490
1491 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
1492 REPORTER_ASSERT(r, codec);
1493
1494 test_info(r, codec.get(), codec->getInfo(), SkCodec::kIncompleteInput, nullptr);
1495}
1496
Leon Scroggins III56e32092016-12-12 17:10:46 -05001497DEF_TEST(Codec_InvalidAnimated, r) {
1498 // ASAN will complain if there is an issue.
1499 auto path = "invalid_images/skbug6046.gif";
1500 auto* stream = GetResourceAsStream(path);
1501 if (!stream) {
1502 return;
1503 }
1504
1505 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
1506 REPORTER_ASSERT(r, codec);
1507 if (!codec) {
1508 return;
1509 }
1510
1511 const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1512 SkBitmap bm;
1513 bm.allocPixels(info);
1514
1515 auto frameInfos = codec->getFrameInfo();
1516 SkCodec::Options opts;
1517 for (size_t i = 0; i < frameInfos.size(); i++) {
1518 opts.fFrameIndex = i;
1519 opts.fHasPriorFrame = frameInfos[i].fRequiredFrame == i - 1;
1520 auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes(), &opts);
1521 if (result != SkCodec::kSuccess) {
1522 ERRORF(r, "Failed to start decoding frame %i (out of %i) with error %i\n", i,
1523 frameInfos.size(), result);
1524 continue;
1525 }
1526
1527 codec->incrementalDecode();
1528 }
1529}