blob: 8294c7a565b63fc69585fd1a36c6179b6e76f1d7 [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"
Hal Canary95e3c052017-01-11 12:44:43 -050020#include "SkPngChunkReader.h"
scroggob636b452015-07-22 07:16:20 -070021#include "SkRandom.h"
scroggocf98fa92015-11-23 08:14:40 -080022#include "SkStream.h"
scroggob9a1e342015-11-30 06:25:31 -080023#include "SkStreamPriv.h"
halcanarya096d7a2015-03-27 12:16:53 -070024#include "Test.h"
25
scroggocf98fa92015-11-23 08:14:40 -080026#include "png.h"
27
Hal Canarydb683012016-11-23 08:55:18 -070028#include "sk_tool_utils.h"
29
scroggo8e6c7ad2016-09-16 08:20:38 -070030#if PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 5
31 // FIXME (scroggo): Google3 needs to be updated to use a newer version of libpng. In
32 // the meantime, we had to break some pieces of SkPngCodec in order to support Google3.
33 // The parts that are broken are likely not used by Google3.
34 #define SK_PNG_DISABLE_TESTS
35#endif
36
halcanarya096d7a2015-03-27 12:16:53 -070037static void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
38 SkAutoLockPixels autoLockPixels(bm);
39 SkASSERT(bm.getPixels());
40 SkMD5 md5;
41 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
42 for (int y = 0; y < bm.height(); ++y) {
halcanary1e903042016-04-25 10:29:36 -070043 md5.write(bm.getAddr(0, y), rowLen);
halcanarya096d7a2015-03-27 12:16:53 -070044 }
45 md5.finish(*digest);
46}
47
scroggo9b2cdbf42015-07-10 12:07:02 -070048/**
49 * Compute the digest for bm and compare it to a known good digest.
50 * @param r Reporter to assert that bm's digest matches goodDigest.
51 * @param goodDigest The known good digest to compare to.
52 * @param bm The bitmap to test.
53 */
54static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
55 const SkBitmap& bm) {
56 SkMD5::Digest digest;
57 md5(bm, &digest);
58 REPORTER_ASSERT(r, digest == goodDigest);
59}
60
scroggod1bc5742015-08-12 08:31:44 -070061/**
62 * Test decoding an SkCodec to a particular SkImageInfo.
63 *
halcanary96fcdcc2015-08-27 07:41:13 -070064 * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr,
scroggod1bc5742015-08-12 08:31:44 -070065 * the resulting decode should match.
66 */
scroggo7b5e5532016-02-04 06:14:24 -080067template<typename Codec>
68static void test_info(skiatest::Reporter* r, Codec* codec, const SkImageInfo& info,
scroggod1bc5742015-08-12 08:31:44 -070069 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
70 SkBitmap bm;
71 bm.allocPixels(info);
72 SkAutoLockPixels autoLockPixels(bm);
73
74 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
75 REPORTER_ASSERT(r, result == expectedResult);
76
77 if (goodDigest) {
78 compare_to_good_digest(r, *goodDigest, bm);
79 }
80}
81
scroggob636b452015-07-22 07:16:20 -070082SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
83 SkIRect rect;
84 do {
85 rect.fLeft = rand->nextRangeU(0, w);
86 rect.fTop = rand->nextRangeU(0, h);
87 rect.fRight = rand->nextRangeU(0, w);
88 rect.fBottom = rand->nextRangeU(0, h);
89 rect.sort();
90 } while (rect.isEmpty());
91 return rect;
92}
93
scroggo8e6c7ad2016-09-16 08:20:38 -070094static void test_incremental_decode(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
95 const SkMD5::Digest& goodDigest) {
96 SkBitmap bm;
97 bm.allocPixels(info);
98 SkAutoLockPixels autoLockPixels(bm);
99
100 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->startIncrementalDecode(info, bm.getPixels(),
101 bm.rowBytes()));
102
103 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->incrementalDecode());
104
105 compare_to_good_digest(r, goodDigest, bm);
106}
107
108// Test in stripes, similar to DM's kStripe_Mode
109static void test_in_stripes(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
110 const SkMD5::Digest& goodDigest) {
111 SkBitmap bm;
112 bm.allocPixels(info);
113 bm.eraseColor(SK_ColorYELLOW);
114
115 const int height = info.height();
116 // Note that if numStripes does not evenly divide height there will be an extra
117 // stripe.
118 const int numStripes = 4;
119
120 if (numStripes > height) {
121 // Image is too small.
122 return;
123 }
124
125 const int stripeHeight = height / numStripes;
126
127 // Iterate through the image twice. Once to decode odd stripes, and once for even.
128 for (int oddEven = 1; oddEven >= 0; oddEven--) {
129 for (int y = oddEven * stripeHeight; y < height; y += 2 * stripeHeight) {
130 SkIRect subset = SkIRect::MakeLTRB(0, y, info.width(),
131 SkTMin(y + stripeHeight, height));
132 SkCodec::Options options;
133 options.fSubset = &subset;
134 if (SkCodec::kSuccess != codec->startIncrementalDecode(info, bm.getAddr(0, y),
135 bm.rowBytes(), &options)) {
136 ERRORF(r, "failed to start incremental decode!\ttop: %i\tbottom%i\n",
137 subset.top(), subset.bottom());
138 return;
139 }
140 if (SkCodec::kSuccess != codec->incrementalDecode()) {
141 ERRORF(r, "failed incremental decode starting from line %i\n", y);
142 return;
143 }
144 }
145 }
146
147 compare_to_good_digest(r, goodDigest, bm);
148}
149
scroggo7b5e5532016-02-04 06:14:24 -0800150template<typename Codec>
151static void test_codec(skiatest::Reporter* r, Codec* codec, SkBitmap& bm, const SkImageInfo& info,
scroggo27c17282015-10-27 08:14:46 -0700152 const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest,
153 const SkMD5::Digest* goodDigest) {
msarette6dd0042015-10-09 11:07:34 -0700154
halcanarya096d7a2015-03-27 12:16:53 -0700155 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -0700156 bm.allocPixels(info);
157 SkAutoLockPixels autoLockPixels(bm);
msarettcc7f3052015-10-05 14:20:27 -0700158
159 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700160 REPORTER_ASSERT(r, result == expectedResult);
halcanarya096d7a2015-03-27 12:16:53 -0700161
msarettcc7f3052015-10-05 14:20:27 -0700162 md5(bm, digest);
163 if (goodDigest) {
164 REPORTER_ASSERT(r, *digest == *goodDigest);
165 }
halcanarya096d7a2015-03-27 12:16:53 -0700166
msarett8ff6ca62015-09-18 12:06:04 -0700167 {
168 // Test decoding to 565
169 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggoba584892016-05-20 13:56:13 -0700170 if (info.alphaType() == kOpaque_SkAlphaType) {
171 // Decoding to 565 should succeed.
172 SkBitmap bm565;
173 bm565.allocPixels(info565);
174 SkAutoLockPixels alp(bm565);
175
176 // This will allow comparison even if the image is incomplete.
177 bm565.eraseColor(SK_ColorBLACK);
178
179 REPORTER_ASSERT(r, expectedResult == codec->getPixels(info565,
180 bm565.getPixels(), bm565.rowBytes()));
181
182 SkMD5::Digest digest565;
183 md5(bm565, &digest565);
184
185 // A dumb client's request for non-opaque should also succeed.
186 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
187 info565 = info565.makeAlphaType(alpha);
188 test_info(r, codec, info565, expectedResult, &digest565);
189 }
190 } else {
191 test_info(r, codec, info565, SkCodec::kInvalidConversion, nullptr);
192 }
193 }
194
195 if (codec->getInfo().colorType() == kGray_8_SkColorType) {
196 SkImageInfo grayInfo = codec->getInfo();
197 SkBitmap grayBm;
198 grayBm.allocPixels(grayInfo);
199 SkAutoLockPixels alp(grayBm);
200
201 grayBm.eraseColor(SK_ColorBLACK);
202
203 REPORTER_ASSERT(r, expectedResult == codec->getPixels(grayInfo,
204 grayBm.getPixels(), grayBm.rowBytes()));
205
206 SkMD5::Digest grayDigest;
207 md5(grayBm, &grayDigest);
208
209 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
210 grayInfo = grayInfo.makeAlphaType(alpha);
211 test_info(r, codec, grayInfo, expectedResult, &grayDigest);
212 }
msarett8ff6ca62015-09-18 12:06:04 -0700213 }
214
215 // Verify that re-decoding gives the same result. It is interesting to check this after
216 // a decode to 565, since choosing to decode to 565 may result in some of the decode
217 // options being modified. These options should return to their defaults on another
218 // decode to kN32, so the new digest should match the old digest.
msarette6dd0042015-10-09 11:07:34 -0700219 test_info(r, codec, info, expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700220
221 {
222 // Check alpha type conversions
223 if (info.alphaType() == kOpaque_SkAlphaType) {
224 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800225 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700226 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800227 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700228 } else {
229 // Decoding to opaque should fail
230 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700231 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700232 SkAlphaType otherAt = info.alphaType();
233 if (kPremul_SkAlphaType == otherAt) {
234 otherAt = kUnpremul_SkAlphaType;
235 } else {
236 otherAt = kPremul_SkAlphaType;
237 }
238 // The other non-opaque alpha type should always succeed, but not match.
msarette6dd0042015-10-09 11:07:34 -0700239 test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700240 }
241 }
msarettcc7f3052015-10-05 14:20:27 -0700242}
243
scroggobed1ed62016-02-11 10:24:55 -0800244static bool supports_partial_scanlines(const char path[]) {
scroggo2c3b2182015-10-09 08:40:59 -0700245 static const char* const exts[] = {
246 "jpg", "jpeg", "png", "webp"
247 "JPG", "JPEG", "PNG", "WEBP"
248 };
249
250 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
251 if (SkStrEndsWith(path, exts[i])) {
252 return true;
253 }
254 }
255 return false;
256}
257
scroggo8e6c7ad2016-09-16 08:20:38 -0700258// FIXME: Break up this giant function
msarettcc7f3052015-10-05 14:20:27 -0700259static void check(skiatest::Reporter* r,
260 const char path[],
261 SkISize size,
262 bool supportsScanlineDecoding,
263 bool supportsSubsetDecoding,
scroggo8e6c7ad2016-09-16 08:20:38 -0700264 bool supportsIncomplete,
265 bool supportsNewScanlineDecoding = false) {
msarettcc7f3052015-10-05 14:20:27 -0700266
Ben Wagner145dbcd2016-11-03 14:40:50 -0400267 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarettcc7f3052015-10-05 14:20:27 -0700268 if (!stream) {
msarettcc7f3052015-10-05 14:20:27 -0700269 return;
270 }
msarette6dd0042015-10-09 11:07:34 -0700271
Ben Wagner145dbcd2016-11-03 14:40:50 -0400272 std::unique_ptr<SkCodec> codec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700273 bool isIncomplete = supportsIncomplete;
274 if (isIncomplete) {
275 size_t size = stream->getLength();
Ben Wagner145dbcd2016-11-03 14:40:50 -0400276 sk_sp<SkData> data((SkData::MakeFromStream(stream.get(), 2 * size / 3)));
reed42943c82016-09-12 12:01:44 -0700277 codec.reset(SkCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700278 } else {
mtklein18300a32016-03-16 13:53:35 -0700279 codec.reset(SkCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700280 }
msarettcc7f3052015-10-05 14:20:27 -0700281 if (!codec) {
282 ERRORF(r, "Unable to decode '%s'", path);
283 return;
284 }
285
286 // Test full image decodes with SkCodec
287 SkMD5::Digest codecDigest;
scroggoef0fed32016-02-18 05:59:25 -0800288 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
msarettcc7f3052015-10-05 14:20:27 -0700289 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700290 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
scroggo7b5e5532016-02-04 06:14:24 -0800291 test_codec(r, codec.get(), bm, info, size, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700292
293 // Scanline decoding follows.
scroggod8d68552016-06-06 11:26:17 -0700294
scroggo8e6c7ad2016-09-16 08:20:38 -0700295 if (supportsNewScanlineDecoding && !isIncomplete) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400296 test_incremental_decode(r, codec.get(), info, codecDigest);
scroggo19b91532016-10-24 09:03:26 -0700297 // This is only supported by codecs that use incremental decoding to
298 // support subset decodes - png and jpeg (once SkJpegCodec is
299 // converted).
300 if (SkStrEndsWith(path, "png") || SkStrEndsWith(path, "PNG")) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400301 test_in_stripes(r, codec.get(), info, codecDigest);
scroggo19b91532016-10-24 09:03:26 -0700302 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700303 }
304
305 // Need to call startScanlineDecode() first.
306 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) == 0);
307 REPORTER_ASSERT(r, !codec->skipScanlines(1));
scroggo46c57472015-09-30 08:57:13 -0700308 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700309 if (supportsScanlineDecoding) {
310 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700311
scroggo46c57472015-09-30 08:57:13 -0700312 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700313
scroggo58421542015-04-01 11:25:20 -0700314 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700315 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
316 if (!isIncomplete) {
317 REPORTER_ASSERT(r, 1 == lines);
318 }
scroggo58421542015-04-01 11:25:20 -0700319 }
320 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700321 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700322 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700323 }
scroggo46c57472015-09-30 08:57:13 -0700324
325 // Cannot continue to decode scanlines beyond the end
326 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700327 == 0);
scroggo46c57472015-09-30 08:57:13 -0700328
329 // Interrupting a scanline decode with a full decode starts from
330 // scratch
331 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700332 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
333 if (!isIncomplete) {
334 REPORTER_ASSERT(r, lines == 1);
335 }
scroggo46c57472015-09-30 08:57:13 -0700336 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700337 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700338 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700339 == 0);
scroggo46c57472015-09-30 08:57:13 -0700340 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700341 == 0);
msarett80803ff2015-10-16 10:54:12 -0700342
343 // Test partial scanline decodes
scroggobed1ed62016-02-11 10:24:55 -0800344 if (supports_partial_scanlines(path) && info.width() >= 3) {
msarett80803ff2015-10-16 10:54:12 -0700345 SkCodec::Options options;
346 int width = info.width();
347 int height = info.height();
348 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
349 options.fSubset = &subset;
350
351 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
352 nullptr, nullptr);
353 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
354
355 for (int y = 0; y < height; y++) {
356 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
357 if (!isIncomplete) {
358 REPORTER_ASSERT(r, 1 == lines);
359 }
360 }
361 }
scroggo58421542015-04-01 11:25:20 -0700362 } else {
scroggo46c57472015-09-30 08:57:13 -0700363 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700364 }
scroggob636b452015-07-22 07:16:20 -0700365
366 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
367 // random subsets.
368 // Do not attempt to decode subsets of an image of only once pixel, since there is no
369 // meaningful subset.
370 if (size.width() * size.height() == 1) {
371 return;
372 }
373
374 SkRandom rand;
375 SkIRect subset;
376 SkCodec::Options opts;
377 opts.fSubset = &subset;
378 for (int i = 0; i < 5; i++) {
379 subset = generate_random_subset(&rand, size.width(), size.height());
380 SkASSERT(!subset.isEmpty());
381 const bool supported = codec->getValidSubset(&subset);
382 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
383
384 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
385 SkBitmap bm;
386 bm.allocPixels(subsetInfo);
387 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700388 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700389
390 if (supportsSubsetDecoding) {
Leon Scroggins III58f100c2016-12-20 09:49:25 -0500391 if (expectedResult == SkCodec::kSuccess) {
392 REPORTER_ASSERT(r, result == expectedResult);
393 } else {
394 SkASSERT(expectedResult == SkCodec::kIncompleteInput);
395 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput
396 || result == SkCodec::kSuccess);
397 }
scroggob636b452015-07-22 07:16:20 -0700398 // Webp is the only codec that supports subsets, and it will have modified the subset
399 // to have even left/top.
400 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
401 } else {
402 // No subsets will work.
403 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
404 }
405 }
msarettcc7f3052015-10-05 14:20:27 -0700406
scroggobed1ed62016-02-11 10:24:55 -0800407 // SkAndroidCodec tests
scroggo8e6c7ad2016-09-16 08:20:38 -0700408 if (supportsScanlineDecoding || supportsSubsetDecoding || supportsNewScanlineDecoding) {
scroggo2c3b2182015-10-09 08:40:59 -0700409
Ben Wagner145dbcd2016-11-03 14:40:50 -0400410 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarettcc7f3052015-10-05 14:20:27 -0700411 if (!stream) {
msarettcc7f3052015-10-05 14:20:27 -0700412 return;
413 }
msarette6dd0042015-10-09 11:07:34 -0700414
Ben Wagner145dbcd2016-11-03 14:40:50 -0400415 std::unique_ptr<SkAndroidCodec> androidCodec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700416 if (isIncomplete) {
417 size_t size = stream->getLength();
Ben Wagner145dbcd2016-11-03 14:40:50 -0400418 sk_sp<SkData> data((SkData::MakeFromStream(stream.get(), 2 * size / 3)));
reed42943c82016-09-12 12:01:44 -0700419 androidCodec.reset(SkAndroidCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700420 } else {
mtklein18300a32016-03-16 13:53:35 -0700421 androidCodec.reset(SkAndroidCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700422 }
scroggo7b5e5532016-02-04 06:14:24 -0800423 if (!androidCodec) {
msarettcc7f3052015-10-05 14:20:27 -0700424 ERRORF(r, "Unable to decode '%s'", path);
425 return;
426 }
427
428 SkBitmap bm;
scroggobed1ed62016-02-11 10:24:55 -0800429 SkMD5::Digest androidCodecDigest;
430 test_codec(r, androidCodec.get(), bm, info, size, expectedResult, &androidCodecDigest,
scroggo7b5e5532016-02-04 06:14:24 -0800431 &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700432 }
433
msarettedd2dcf2016-01-14 13:12:26 -0800434 if (!isIncomplete) {
scroggoef0fed32016-02-18 05:59:25 -0800435 // Test SkCodecImageGenerator
Ben Wagner145dbcd2016-11-03 14:40:50 -0400436 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
437 sk_sp<SkData> fullData(SkData::MakeFromStream(stream.get(), stream->getLength()));
438 std::unique_ptr<SkImageGenerator> gen(
bungeman38d909e2016-08-02 14:40:46 -0700439 SkCodecImageGenerator::NewFromEncodedCodec(fullData.get()));
msarettedd2dcf2016-01-14 13:12:26 -0800440 SkBitmap bm;
441 bm.allocPixels(info);
442 SkAutoLockPixels autoLockPixels(bm);
443 REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes()));
444 compare_to_good_digest(r, codecDigest, bm);
scroggoef0fed32016-02-18 05:59:25 -0800445
scroggo8e6c7ad2016-09-16 08:20:38 -0700446#ifndef SK_PNG_DISABLE_TESTS
scroggod8d68552016-06-06 11:26:17 -0700447 // Test using SkFrontBufferedStream, as Android does
bungeman38d909e2016-08-02 14:40:46 -0700448 SkStream* bufferedStream = SkFrontBufferedStream::Create(
449 new SkMemoryStream(std::move(fullData)), SkCodec::MinBufferedBytesNeeded());
scroggod8d68552016-06-06 11:26:17 -0700450 REPORTER_ASSERT(r, bufferedStream);
451 codec.reset(SkCodec::NewFromStream(bufferedStream));
452 REPORTER_ASSERT(r, codec);
453 if (codec) {
454 test_info(r, codec.get(), info, SkCodec::kSuccess, &codecDigest);
scroggoef0fed32016-02-18 05:59:25 -0800455 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700456#endif
msarettedd2dcf2016-01-14 13:12:26 -0800457 }
458
msarette6dd0042015-10-09 11:07:34 -0700459 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
460 if (isIncomplete) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700461 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false,
462 supportsNewScanlineDecoding);
msarettcc7f3052015-10-05 14:20:27 -0700463 }
halcanarya096d7a2015-03-27 12:16:53 -0700464}
465
Leon Scroggins III83926342016-12-06 10:58:02 -0500466DEF_TEST(Codec_wbmp, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700467 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500468}
halcanarya096d7a2015-03-27 12:16:53 -0700469
Leon Scroggins III83926342016-12-06 10:58:02 -0500470DEF_TEST(Codec_webp, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700471 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true, true);
472 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true, true);
473 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500474}
scroggo6f5e6192015-06-18 12:53:43 -0700475
Leon Scroggins III83926342016-12-06 10:58:02 -0500476DEF_TEST(Codec_bmp, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700477 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false, true);
478 check(r, "rle.bmp", SkISize::Make(320, 240), true, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500479}
halcanarya096d7a2015-03-27 12:16:53 -0700480
Leon Scroggins III83926342016-12-06 10:58:02 -0500481DEF_TEST(Codec_ico, r) {
msarette6dd0042015-10-09 11:07:34 -0700482 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700483 // These two tests examine interestingly different behavior:
484 // Decodes an embedded BMP image
msarettbe8216a2015-12-04 08:00:50 -0800485 check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700486 // Decodes an embedded PNG image
scroggo8e6c7ad2016-09-16 08:20:38 -0700487 check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500488}
halcanarya096d7a2015-03-27 12:16:53 -0700489
Leon Scroggins III83926342016-12-06 10:58:02 -0500490DEF_TEST(Codec_gif, r) {
scroggo19b91532016-10-24 09:03:26 -0700491 check(r, "box.gif", SkISize::Make(200, 55), false, false, true, true);
492 check(r, "color_wheel.gif", SkISize::Make(128, 128), false, false, true, true);
msarette6dd0042015-10-09 11:07:34 -0700493 // randPixels.gif is too small to test incomplete
scroggo19b91532016-10-24 09:03:26 -0700494 check(r, "randPixels.gif", SkISize::Make(8, 8), false, false, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500495}
msarett438b2ad2015-04-09 12:43:10 -0700496
Leon Scroggins III83926342016-12-06 10:58:02 -0500497DEF_TEST(Codec_jpg, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700498 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false, true);
499 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700500 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700501 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggo8e6c7ad2016-09-16 08:20:38 -0700502 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700503 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700504 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
Leon Scroggins III83926342016-12-06 10:58:02 -0500505}
msarette16b04a2015-04-15 07:32:19 -0700506
Leon Scroggins III83926342016-12-06 10:58:02 -0500507DEF_TEST(Codec_png, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700508 check(r, "arrow.png", SkISize::Make(187, 312), false, false, true, true);
509 check(r, "baby_tux.png", SkISize::Make(240, 246), false, false, true, true);
510 check(r, "color_wheel.png", SkISize::Make(128, 128), false, false, true, true);
511 // half-transparent-white-pixel.png is too small to test incomplete
512 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), false, false, false, true);
513 check(r, "mandrill_128.png", SkISize::Make(128, 128), false, false, true, true);
514 check(r, "mandrill_16.png", SkISize::Make(16, 16), false, false, true, true);
515 check(r, "mandrill_256.png", SkISize::Make(256, 256), false, false, true, true);
516 check(r, "mandrill_32.png", SkISize::Make(32, 32), false, false, true, true);
517 check(r, "mandrill_512.png", SkISize::Make(512, 512), false, false, true, true);
518 check(r, "mandrill_64.png", SkISize::Make(64, 64), false, false, true, true);
519 check(r, "plane.png", SkISize::Make(250, 126), false, false, true, true);
520 check(r, "plane_interlaced.png", SkISize::Make(250, 126), false, false, true, true);
521 check(r, "randPixels.png", SkISize::Make(8, 8), false, false, true, true);
522 check(r, "yellow_rose.png", SkISize::Make(400, 301), false, false, true, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500523}
yujieqin916de9f2016-01-25 08:26:16 -0800524
yujieqinf236ee42016-02-29 07:14:42 -0800525// Disable RAW tests for Win32.
526#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Leon Scroggins III83926342016-12-06 10:58:02 -0500527DEF_TEST(Codec_raw, r) {
yujieqin916de9f2016-01-25 08:26:16 -0800528 check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
ebrauer46d2aa82016-02-17 08:04:00 -0800529 check(r, "sample_1mp_rotated.dng", SkISize::Make(600, 338), false, false, false);
yujieqin9c7a8a42016-02-05 08:21:19 -0800530 check(r, "dng_with_preview.dng", SkISize::Make(600, 338), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700531}
Leon Scroggins III83926342016-12-06 10:58:02 -0500532#endif
scroggo0a7e69c2015-04-03 07:22:22 -0700533
534static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700535 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700536 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700537 REPORTER_ASSERT(r, !codec);
538
msarett3d9d7a72015-10-21 10:27:10 -0700539 SkAndroidCodec* androidCodec =
540 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
541 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700542}
543
544// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
545// even on failure. Test some bad streams.
546DEF_TEST(Codec_leaks, r) {
547 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
548 const char nonSupportedStream[] = "hello world";
549 // The other strings should look like the beginning of a file type, so we'll call some
550 // internal version of NewFromStream, which must also delete the stream on failure.
551 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
552 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
553 const char emptyWebp[] = "RIFF1234WEBPVP";
554 const char emptyBmp[] = { 'B', 'M' };
555 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
556 const char emptyGif[] = "GIFVER";
557
558 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
559 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
560 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
561 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
562 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
563 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
564 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
565}
msarette16b04a2015-04-15 07:32:19 -0700566
scroggo2c3b2182015-10-09 08:40:59 -0700567DEF_TEST(Codec_null, r) {
scroggobed1ed62016-02-11 10:24:55 -0800568 // Attempting to create an SkCodec or an SkAndroidCodec with null should not
scroggo2c3b2182015-10-09 08:40:59 -0700569 // crash.
570 SkCodec* codec = SkCodec::NewFromStream(nullptr);
571 REPORTER_ASSERT(r, !codec);
572
msarett3d9d7a72015-10-21 10:27:10 -0700573 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
574 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700575}
576
msarette16b04a2015-04-15 07:32:19 -0700577static void test_dimensions(skiatest::Reporter* r, const char path[]) {
578 // Create the codec from the resource file
Ben Wagner145dbcd2016-11-03 14:40:50 -0400579 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarette16b04a2015-04-15 07:32:19 -0700580 if (!stream) {
msarette16b04a2015-04-15 07:32:19 -0700581 return;
582 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400583 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarette16b04a2015-04-15 07:32:19 -0700584 if (!codec) {
585 ERRORF(r, "Unable to create codec '%s'", path);
586 return;
587 }
588
589 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800590 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700591 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700592 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700593 SkImageInfo scaledInfo = codec->getInfo()
594 .makeWH(scaledDims.width(), scaledDims.height())
595 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700596
597 // Set up for the decode
598 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
599 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
600 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
601
msarett3d9d7a72015-10-21 10:27:10 -0700602 SkAndroidCodec::AndroidOptions options;
603 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700604 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700605 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700606 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700607 }
608}
609
610// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
611DEF_TEST(Codec_Dimensions, r) {
612 // JPG
613 test_dimensions(r, "CMYK.jpg");
614 test_dimensions(r, "color_wheel.jpg");
615 test_dimensions(r, "grayscale.jpg");
616 test_dimensions(r, "mandrill_512_q075.jpg");
617 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700618
619 // Decoding small images with very large scaling factors is a potential
620 // source of bugs and crashes. We disable these tests in Gold because
621 // tiny images are not very useful to look at.
622 // Here we make sure that we do not crash or access illegal memory when
623 // performing scaled decodes on small images.
624 test_dimensions(r, "1x1.png");
625 test_dimensions(r, "2x2.png");
626 test_dimensions(r, "3x3.png");
627 test_dimensions(r, "3x1.png");
628 test_dimensions(r, "1x1.png");
629 test_dimensions(r, "16x1.png");
630 test_dimensions(r, "1x16.png");
631 test_dimensions(r, "mandrill_16.png");
632
yujieqin916de9f2016-01-25 08:26:16 -0800633 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800634// Disable RAW tests for Win32.
635#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800636 test_dimensions(r, "sample_1mp.dng");
ebrauer46d2aa82016-02-17 08:04:00 -0800637 test_dimensions(r, "sample_1mp_rotated.dng");
yujieqin9c7a8a42016-02-05 08:21:19 -0800638 test_dimensions(r, "dng_with_preview.dng");
msarett8e49ca32016-01-25 13:10:58 -0800639#endif
msarette16b04a2015-04-15 07:32:19 -0700640}
641
msarettd0375bc2015-08-12 08:08:56 -0700642static void test_invalid(skiatest::Reporter* r, const char path[]) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400643 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett4b17fa32015-04-23 08:53:39 -0700644 if (!stream) {
msarett4b17fa32015-04-23 08:53:39 -0700645 return;
646 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400647 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
halcanary96fcdcc2015-08-27 07:41:13 -0700648 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700649}
msarette16b04a2015-04-15 07:32:19 -0700650
msarett4b17fa32015-04-23 08:53:39 -0700651DEF_TEST(Codec_Empty, r) {
652 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700653 test_invalid(r, "empty_images/zero-dims.gif");
654 test_invalid(r, "empty_images/zero-embedded.ico");
655 test_invalid(r, "empty_images/zero-width.bmp");
656 test_invalid(r, "empty_images/zero-height.bmp");
657 test_invalid(r, "empty_images/zero-width.jpg");
658 test_invalid(r, "empty_images/zero-height.jpg");
659 test_invalid(r, "empty_images/zero-width.png");
660 test_invalid(r, "empty_images/zero-height.png");
661 test_invalid(r, "empty_images/zero-width.wbmp");
662 test_invalid(r, "empty_images/zero-height.wbmp");
663 // This image is an ico with an embedded mask-bmp. This is illegal.
664 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
Leon Scroggins IIId87fbee2016-12-02 16:47:53 -0500665#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
666 test_invalid(r, "empty_images/zero_height.tiff");
667#endif
msarett4b17fa32015-04-23 08:53:39 -0700668}
msarett99f567e2015-08-05 12:58:26 -0700669
670static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400671 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett99f567e2015-08-05 12:58:26 -0700672 if (!stream) {
msarett99f567e2015-08-05 12:58:26 -0700673 return;
674 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400675 std::unique_ptr<SkCodec> decoder(SkCodec::NewFromStream(stream.release()));
scroggo8e6c7ad2016-09-16 08:20:38 -0700676 if (!decoder) {
677 SkDebugf("Missing codec for %s\n", path);
678 return;
679 }
680
681 const SkImageInfo info = decoder->getInfo().makeColorType(kIndex_8_SkColorType);
halcanary9d524f22016-03-29 09:03:52 -0700682
msarett99f567e2015-08-05 12:58:26 -0700683 // This should return kSuccess because kIndex8 is supported.
684 SkPMColor colorStorage[256];
685 int colorCount;
scroggo8e6c7ad2016-09-16 08:20:38 -0700686 SkCodec::Result result = decoder->startScanlineDecode(info, nullptr, colorStorage,
687 &colorCount);
688 if (SkCodec::kSuccess == result) {
689 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
690 // colorPtr and a valid colorCountPtr.
691 result = decoder->startScanlineDecode(info, nullptr, nullptr, nullptr);
692 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
693 result = decoder->startScanlineDecode(info);
694 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
695 } else if (SkCodec::kUnimplemented == result) {
696 // New method should be supported:
697 SkBitmap bm;
698 sk_sp<SkColorTable> colorTable(new SkColorTable(colorStorage, 256));
699 bm.allocPixels(info, nullptr, colorTable.get());
700 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes(), nullptr,
701 colorStorage, &colorCount);
702 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
703 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
704 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
705 } else {
706 // The test is uninteresting if kIndex8 is not supported
707 ERRORF(r, "Should not call test_invalid_parameters for non-Index8 file: %s\n", path);
msarett99f567e2015-08-05 12:58:26 -0700708 return;
709 }
710
msarett99f567e2015-08-05 12:58:26 -0700711}
712
713DEF_TEST(Codec_Params, r) {
714 test_invalid_parameters(r, "index8.png");
715 test_invalid_parameters(r, "mandrill.wbmp");
716}
scroggocf98fa92015-11-23 08:14:40 -0800717
scroggo8e6c7ad2016-09-16 08:20:38 -0700718#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
719
720#ifndef SK_PNG_DISABLE_TESTS // reading chunks does not work properly with older versions.
721 // It does not appear that anyone in Google3 is reading chunks.
722
scroggocf98fa92015-11-23 08:14:40 -0800723static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
724 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
725 if (!sk_stream->write(data, len)) {
726 png_error(png_ptr, "sk_write_fn Error!");
727 }
728}
729
scroggocf98fa92015-11-23 08:14:40 -0800730DEF_TEST(Codec_pngChunkReader, r) {
731 // Create a dummy bitmap. Use unpremul RGBA for libpng.
732 SkBitmap bm;
733 const int w = 1;
734 const int h = 1;
735 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
736 kUnpremul_SkAlphaType);
737 bm.setInfo(bmInfo);
738 bm.allocPixels();
739 bm.eraseColor(SK_ColorBLUE);
740 SkMD5::Digest goodDigest;
741 md5(bm, &goodDigest);
742
743 // Write to a png file.
744 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
745 REPORTER_ASSERT(r, png);
746 if (!png) {
747 return;
748 }
749
750 png_infop info = png_create_info_struct(png);
751 REPORTER_ASSERT(r, info);
752 if (!info) {
753 png_destroy_write_struct(&png, nullptr);
754 return;
755 }
756
757 if (setjmp(png_jmpbuf(png))) {
758 ERRORF(r, "failed writing png");
759 png_destroy_write_struct(&png, &info);
760 return;
761 }
762
763 SkDynamicMemoryWStream wStream;
764 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
765
766 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
767 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
768 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
769
770 // Create some chunks that match the Android framework's use.
771 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800772 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
773 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
774 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800775 };
776
777 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
778 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
779#if PNG_LIBPNG_VER < 10600
780 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800781 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
782 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800783#endif
784
785 png_write_info(png, info);
786
787 for (int j = 0; j < h; j++) {
788 png_bytep row = (png_bytep)(bm.getAddr(0, j));
789 png_write_rows(png, &row, 1);
790 }
791 png_write_end(png, info);
792 png_destroy_write_struct(&png, &info);
793
794 class ChunkReader : public SkPngChunkReader {
795 public:
796 ChunkReader(skiatest::Reporter* r)
797 : fReporter(r)
798 {
799 this->reset();
800 }
801
802 bool readChunk(const char tag[], const void* data, size_t length) override {
803 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
804 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
805 // Tag matches. This should have been the first time we see it.
806 REPORTER_ASSERT(fReporter, !fSeen[i]);
807 fSeen[i] = true;
808
809 // Data and length should match
810 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
811 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
812 (const char*) gUnknowns[i].data));
813 return true;
814 }
815 }
816 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
817 return true;
818 }
819
820 bool allHaveBeenSeen() {
821 bool ret = true;
822 for (auto seen : fSeen) {
823 ret &= seen;
824 }
825 return ret;
826 }
827
828 void reset() {
829 sk_bzero(fSeen, sizeof(fSeen));
830 }
831
832 private:
833 skiatest::Reporter* fReporter; // Unowned
834 bool fSeen[3];
835 };
836
837 ChunkReader chunkReader(r);
838
839 // Now read the file with SkCodec.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400840 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(wStream.detachAsData(), &chunkReader));
scroggocf98fa92015-11-23 08:14:40 -0800841 REPORTER_ASSERT(r, codec);
842 if (!codec) {
843 return;
844 }
845
846 // Now compare to the original.
847 SkBitmap decodedBm;
848 decodedBm.setInfo(codec->getInfo());
849 decodedBm.allocPixels();
850 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
851 decodedBm.rowBytes());
852 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
853
854 if (decodedBm.colorType() != bm.colorType()) {
855 SkBitmap tmp;
856 bool success = decodedBm.copyTo(&tmp, bm.colorType());
857 REPORTER_ASSERT(r, success);
858 if (!success) {
859 return;
860 }
861
862 tmp.swap(decodedBm);
863 }
864
865 compare_to_good_digest(r, goodDigest, decodedBm);
866 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
867
868 // Decoding again will read the chunks again.
869 chunkReader.reset();
870 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
871 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
872 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
873 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
874}
scroggo8e6c7ad2016-09-16 08:20:38 -0700875#endif // SK_PNG_DISABLE_TESTS
scroggocf98fa92015-11-23 08:14:40 -0800876#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800877
scroggodb30be22015-12-08 18:54:13 -0800878// Stream that can only peek up to a limit
879class LimitedPeekingMemStream : public SkStream {
880public:
reed42943c82016-09-12 12:01:44 -0700881 LimitedPeekingMemStream(sk_sp<SkData> data, size_t limit)
882 : fStream(std::move(data))
scroggodb30be22015-12-08 18:54:13 -0800883 , fLimit(limit) {}
884
885 size_t peek(void* buf, size_t bytes) const override {
886 return fStream.peek(buf, SkTMin(bytes, fLimit));
887 }
888 size_t read(void* buf, size_t bytes) override {
889 return fStream.read(buf, bytes);
890 }
891 bool rewind() override {
892 return fStream.rewind();
893 }
894 bool isAtEnd() const override {
msarettff2a6c82016-09-07 11:23:28 -0700895 return fStream.isAtEnd();
scroggodb30be22015-12-08 18:54:13 -0800896 }
897private:
898 SkMemoryStream fStream;
899 const size_t fLimit;
900};
901
yujieqinf236ee42016-02-29 07:14:42 -0800902// Disable RAW tests for Win32.
903#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin9c7a8a42016-02-05 08:21:19 -0800904// Test that the RawCodec works also for not asset stream. This will test the code path using
905// SkRawBufferedStream instead of SkRawAssetStream.
yujieqin9c7a8a42016-02-05 08:21:19 -0800906DEF_TEST(Codec_raw_notseekable, r) {
907 const char* path = "dng_with_preview.dng";
908 SkString fullPath(GetResourcePath(path));
bungeman38d909e2016-08-02 14:40:46 -0700909 sk_sp<SkData> data(SkData::MakeFromFileName(fullPath.c_str()));
yujieqin9c7a8a42016-02-05 08:21:19 -0800910 if (!data) {
911 SkDebugf("Missing resource '%s'\n", path);
912 return;
913 }
914
Ben Wagner145dbcd2016-11-03 14:40:50 -0400915 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(std::move(data))));
yujieqin9c7a8a42016-02-05 08:21:19 -0800916 REPORTER_ASSERT(r, codec);
917
918 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
919}
920#endif
921
scroggodb30be22015-12-08 18:54:13 -0800922// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
923// + rewind() and succeed.
924DEF_TEST(Codec_webp_peek, r) {
925 const char* path = "baby_tux.webp";
926 SkString fullPath(GetResourcePath(path));
reedfde05112016-03-11 13:02:28 -0800927 auto data = SkData::MakeFromFileName(fullPath.c_str());
scroggodb30be22015-12-08 18:54:13 -0800928 if (!data) {
929 SkDebugf("Missing resource '%s'\n", path);
930 return;
931 }
932
933 // The limit is less than webp needs to peek or read.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400934 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(
reed42943c82016-09-12 12:01:44 -0700935 new LimitedPeekingMemStream(data, 25)));
scroggodb30be22015-12-08 18:54:13 -0800936 REPORTER_ASSERT(r, codec);
937
scroggo7b5e5532016-02-04 06:14:24 -0800938 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800939
940 // Similarly, a stream which does not peek should still succeed.
reed42943c82016-09-12 12:01:44 -0700941 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0)));
scroggodb30be22015-12-08 18:54:13 -0800942 REPORTER_ASSERT(r, codec);
943
scroggo7b5e5532016-02-04 06:14:24 -0800944 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800945}
946
msarett7f7ec202016-03-01 12:12:27 -0800947// SkCodec's wbmp decoder was initially unnecessarily restrictive.
948// It required the second byte to be zero. The wbmp specification allows
949// a couple of bits to be 1 (so long as they do not overlap with 0x9F).
950// Test that SkCodec now supports an image with these bits set.
Leon Scroggins III83926342016-12-06 10:58:02 -0500951DEF_TEST(Codec_wbmp_restrictive, r) {
scroggob9a1e342015-11-30 06:25:31 -0800952 const char* path = "mandrill.wbmp";
Ben Wagner145dbcd2016-11-03 14:40:50 -0400953 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
scroggob9a1e342015-11-30 06:25:31 -0800954 if (!stream) {
scroggob9a1e342015-11-30 06:25:31 -0800955 return;
956 }
957
958 // Modify the stream to contain a second byte with some bits set.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400959 auto data = SkCopyStreamToData(stream.get());
scroggob9a1e342015-11-30 06:25:31 -0800960 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
961 writeableData[1] = static_cast<uint8_t>(~0x9F);
962
msarett7f7ec202016-03-01 12:12:27 -0800963 // SkCodec should support this.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400964 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
scroggob9a1e342015-11-30 06:25:31 -0800965 REPORTER_ASSERT(r, codec);
966 if (!codec) {
967 return;
968 }
scroggo7b5e5532016-02-04 06:14:24 -0800969 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800970}
scroggodb30be22015-12-08 18:54:13 -0800971
972// wbmp images have a header that can be arbitrarily large, depending on the
973// size of the image. We cap the size at 65535, meaning we only need to look at
974// 8 bytes to determine whether we can read the image. This is important
975// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
976// image is a wbmp.
977DEF_TEST(Codec_wbmp_max_size, r) {
978 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
979 0x83, 0xFF, 0x7F, // W: 65535
980 0x83, 0xFF, 0x7F }; // H: 65535
Ben Wagner145dbcd2016-11-03 14:40:50 -0400981 std::unique_ptr<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
982 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -0800983
984 REPORTER_ASSERT(r, codec);
985 if (!codec) return;
986
987 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
988 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
989
990 // Now test an image which is too big. Any image with a larger header (i.e.
991 // has bigger width/height) is also too big.
992 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
993 0x84, 0x80, 0x00, // W: 65536
994 0x84, 0x80, 0x00 }; // H: 65536
995 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
mtklein18300a32016-03-16 13:53:35 -0700996 codec.reset(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -0800997
998 REPORTER_ASSERT(r, !codec);
999}
msarett2812f032016-07-18 15:56:08 -07001000
1001DEF_TEST(Codec_jpeg_rewind, r) {
1002 const char* path = "mandrill_512_q075.jpg";
Ben Wagner145dbcd2016-11-03 14:40:50 -04001003 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett2812f032016-07-18 15:56:08 -07001004 if (!stream) {
msarett2812f032016-07-18 15:56:08 -07001005 return;
1006 }
Ben Wagner145dbcd2016-11-03 14:40:50 -04001007 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarett2812f032016-07-18 15:56:08 -07001008 if (!codec) {
1009 ERRORF(r, "Unable to create codec '%s'.", path);
1010 return;
1011 }
1012
1013 const int width = codec->getInfo().width();
1014 const int height = codec->getInfo().height();
1015 size_t rowBytes = sizeof(SkPMColor) * width;
1016 SkAutoMalloc pixelStorage(height * rowBytes);
1017
1018 // Perform a sampled decode.
1019 SkAndroidCodec::AndroidOptions opts;
1020 opts.fSampleSize = 12;
1021 codec->getAndroidPixels(codec->getInfo().makeWH(width / 12, height / 12), pixelStorage.get(),
1022 rowBytes, &opts);
1023
1024 // Rewind the codec and perform a full image decode.
1025 SkCodec::Result result = codec->getPixels(codec->getInfo(), pixelStorage.get(), rowBytes);
1026 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1027}
msarett549ca322016-08-17 08:54:08 -07001028
msarett35bb74b2016-08-22 07:41:28 -07001029static void check_color_xform(skiatest::Reporter* r, const char* path) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001030 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(GetResourceAsStream(path)));
msarett35bb74b2016-08-22 07:41:28 -07001031
1032 SkAndroidCodec::AndroidOptions opts;
1033 opts.fSampleSize = 3;
1034 const int subsetWidth = codec->getInfo().width() / 2;
1035 const int subsetHeight = codec->getInfo().height() / 2;
1036 SkIRect subset = SkIRect::MakeWH(subsetWidth, subsetHeight);
1037 opts.fSubset = &subset;
1038
1039 const int dstWidth = subsetWidth / opts.fSampleSize;
1040 const int dstHeight = subsetHeight / opts.fSampleSize;
1041 sk_sp<SkData> data = SkData::MakeFromFileName(
1042 GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str());
Brian Osman526972e2016-10-24 09:24:02 -04001043 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeICC(data->data(), data->size());
msarett35bb74b2016-08-22 07:41:28 -07001044 SkImageInfo dstInfo = codec->getInfo().makeWH(dstWidth, dstHeight)
1045 .makeColorType(kN32_SkColorType)
1046 .makeColorSpace(colorSpace);
1047
1048 size_t rowBytes = dstInfo.minRowBytes();
1049 SkAutoMalloc pixelStorage(dstInfo.getSafeSize(rowBytes));
1050 SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get(), rowBytes, &opts);
1051 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1052}
1053
1054DEF_TEST(Codec_ColorXform, r) {
1055 check_color_xform(r, "mandrill_512_q075.jpg");
1056 check_color_xform(r, "mandrill_512.png");
1057}
1058
msarettf17b71f2016-09-12 14:30:03 -07001059static bool color_type_match(SkColorType origColorType, SkColorType codecColorType) {
1060 switch (origColorType) {
1061 case kRGBA_8888_SkColorType:
1062 case kBGRA_8888_SkColorType:
1063 return kRGBA_8888_SkColorType == codecColorType ||
1064 kBGRA_8888_SkColorType == codecColorType;
1065 default:
1066 return origColorType == codecColorType;
1067 }
1068}
1069
1070static bool alpha_type_match(SkAlphaType origAlphaType, SkAlphaType codecAlphaType) {
1071 switch (origAlphaType) {
1072 case kUnpremul_SkAlphaType:
1073 case kPremul_SkAlphaType:
1074 return kUnpremul_SkAlphaType == codecAlphaType ||
1075 kPremul_SkAlphaType == codecAlphaType;
1076 default:
1077 return origAlphaType == codecAlphaType;
1078 }
1079}
1080
1081static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const SkImageInfo& info) {
1082 SkBitmap bm1;
1083 SkPMColor colors[256];
Hal Canary342b7ac2016-11-04 11:49:42 -04001084 sk_sp<SkColorTable> colorTable1(new SkColorTable(colors, 256));
msarettf17b71f2016-09-12 14:30:03 -07001085 bm1.allocPixels(info, nullptr, colorTable1.get());
1086 int numColors;
1087 SkCodec::Result result = origCodec->getPixels(info, bm1.getPixels(), bm1.rowBytes(), nullptr,
1088 const_cast<SkPMColor*>(colorTable1->readColors()),
1089 &numColors);
1090 // This will fail to update colorTable1->count() but is fine for the purpose of this test.
1091 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarett9b09cd82016-08-29 14:47:49 -07001092
1093 // Encode the image to png.
1094 sk_sp<SkData> data =
Hal Canarydb683012016-11-23 08:55:18 -07001095 sk_sp<SkData>(sk_tool_utils::EncodeImageToData(bm1, SkEncodedImageFormat::kPNG, 100));
msarett9b09cd82016-08-29 14:47:49 -07001096
Ben Wagner145dbcd2016-11-03 14:40:50 -04001097 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
msarettf17b71f2016-09-12 14:30:03 -07001098 REPORTER_ASSERT(r, color_type_match(info.colorType(), codec->getInfo().colorType()));
1099 REPORTER_ASSERT(r, alpha_type_match(info.alphaType(), codec->getInfo().alphaType()));
msarett9b09cd82016-08-29 14:47:49 -07001100
1101 SkBitmap bm2;
Hal Canary342b7ac2016-11-04 11:49:42 -04001102 sk_sp<SkColorTable> colorTable2(new SkColorTable(colors, 256));
msarettf17b71f2016-09-12 14:30:03 -07001103 bm2.allocPixels(info, nullptr, colorTable2.get());
1104 result = codec->getPixels(info, bm2.getPixels(), bm2.rowBytes(), nullptr,
1105 const_cast<SkPMColor*>(colorTable2->readColors()), &numColors);
msarett9b09cd82016-08-29 14:47:49 -07001106 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1107
1108 SkMD5::Digest d1, d2;
1109 md5(bm1, &d1);
1110 md5(bm2, &d2);
1111 REPORTER_ASSERT(r, d1 == d2);
1112}
1113
1114DEF_TEST(Codec_PngRoundTrip, r) {
msarett549ca322016-08-17 08:54:08 -07001115 const char* path = "mandrill_512_q075.jpg";
Ben Wagner145dbcd2016-11-03 14:40:50 -04001116 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
1117 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett549ca322016-08-17 08:54:08 -07001118
msarettf17b71f2016-09-12 14:30:03 -07001119 SkColorType colorTypesOpaque[] = {
1120 kRGB_565_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1121 };
1122 for (SkColorType colorType : colorTypesOpaque) {
1123 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType);
1124 check_round_trip(r, codec.get(), newInfo);
1125 }
1126
msarett9b09cd82016-08-29 14:47:49 -07001127 path = "grayscale.jpg";
bungemanf93d7112016-09-16 06:24:20 -07001128 stream.reset(GetResourceAsStream(path));
msarett9b09cd82016-08-29 14:47:49 -07001129 codec.reset(SkCodec::NewFromStream(stream.release()));
msarettf17b71f2016-09-12 14:30:03 -07001130 check_round_trip(r, codec.get(), codec->getInfo());
1131
1132 path = "yellow_rose.png";
bungemanf93d7112016-09-16 06:24:20 -07001133 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001134 codec.reset(SkCodec::NewFromStream(stream.release()));
1135
1136 SkColorType colorTypesWithAlpha[] = {
1137 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1138 };
1139 SkAlphaType alphaTypes[] = {
1140 kUnpremul_SkAlphaType, kPremul_SkAlphaType
1141 };
1142 for (SkColorType colorType : colorTypesWithAlpha) {
1143 for (SkAlphaType alphaType : alphaTypes) {
1144 // Set color space to nullptr because color correct premultiplies do not round trip.
1145 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType)
1146 .makeAlphaType(alphaType)
1147 .makeColorSpace(nullptr);
1148 check_round_trip(r, codec.get(), newInfo);
1149 }
1150 }
1151
1152 path = "index8.png";
bungemanf93d7112016-09-16 06:24:20 -07001153 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001154 codec.reset(SkCodec::NewFromStream(stream.release()));
1155
1156 for (SkAlphaType alphaType : alphaTypes) {
1157 SkImageInfo newInfo = codec->getInfo().makeAlphaType(alphaType)
1158 .makeColorSpace(nullptr);
1159 check_round_trip(r, codec.get(), newInfo);
1160 }
msarett549ca322016-08-17 08:54:08 -07001161}
msarett2ecc35f2016-09-08 11:55:16 -07001162
1163static void test_conversion_possible(skiatest::Reporter* r, const char* path,
scroggo8e6c7ad2016-09-16 08:20:38 -07001164 bool supportsScanlineDecoder,
1165 bool supportsIncrementalDecoder) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001166 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
1167 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett2ecc35f2016-09-08 11:55:16 -07001168 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType);
1169
1170 SkBitmap bm;
1171 bm.allocPixels(infoF16);
1172 SkCodec::Result result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1173 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001174
1175 result = codec->startScanlineDecode(infoF16);
1176 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001177 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001178 } else {
1179 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1180 }
1181
1182 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1183 if (supportsIncrementalDecoder) {
1184 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
1185 } else {
1186 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001187 }
1188
raftias94888332016-10-18 10:02:51 -07001189 SkASSERT(SkColorSpace_Base::Type::kXYZ == as_CSB(infoF16.colorSpace())->type());
1190 SkColorSpace_XYZ* csXYZ = static_cast<SkColorSpace_XYZ*>(infoF16.colorSpace());
1191 infoF16 = infoF16.makeColorSpace(csXYZ->makeLinearGamma());
msarett2ecc35f2016-09-08 11:55:16 -07001192 result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1193 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001194 result = codec->startScanlineDecode(infoF16);
1195 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001196 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001197 } else {
1198 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1199 }
1200
1201 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1202 if (supportsIncrementalDecoder) {
1203 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1204 } else {
1205 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001206 }
1207}
1208
1209DEF_TEST(Codec_F16ConversionPossible, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -07001210 test_conversion_possible(r, "color_wheel.webp", false, false);
1211 test_conversion_possible(r, "mandrill_512_q075.jpg", true, false);
1212 test_conversion_possible(r, "yellow_rose.png", false, true);
1213}
1214
scroggo19b91532016-10-24 09:03:26 -07001215static void decode_frame(skiatest::Reporter* r, SkCodec* codec, size_t frame) {
1216 SkBitmap bm;
1217 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1218 bm.allocPixels(info);
1219
1220 SkCodec::Options opts;
1221 opts.fFrameIndex = frame;
1222 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->getPixels(info,
1223 bm.getPixels(), bm.rowBytes(), &opts, nullptr, nullptr));
1224}
1225
1226// For an animated image, we should only read enough to decode the requested
1227// frame if the client never calls getFrameInfo.
1228DEF_TEST(Codec_skipFullParse, r) {
1229 auto path = "test640x479.gif";
1230 SkStream* stream(GetResourceAsStream(path));
1231 if (!stream) {
1232 return;
1233 }
1234
1235 // Note that we cheat and hold on to the stream pointer, but SkCodec will
1236 // take ownership. We will not refer to the stream after the SkCodec
1237 // deletes it.
1238 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
1239 if (!codec) {
1240 ERRORF(r, "Failed to create codec for %s", path);
1241 return;
1242 }
1243
1244 REPORTER_ASSERT(r, stream->hasPosition());
1245 const size_t sizePosition = stream->getPosition();
1246 REPORTER_ASSERT(r, stream->hasLength() && sizePosition < stream->getLength());
1247
1248 // This should read more of the stream, but not the whole stream.
1249 decode_frame(r, codec.get(), 0);
1250 const size_t positionAfterFirstFrame = stream->getPosition();
1251 REPORTER_ASSERT(r, positionAfterFirstFrame > sizePosition
1252 && positionAfterFirstFrame < stream->getLength());
1253
1254 // Again, this should read more of the stream.
1255 decode_frame(r, codec.get(), 2);
1256 const size_t positionAfterThirdFrame = stream->getPosition();
1257 REPORTER_ASSERT(r, positionAfterThirdFrame > positionAfterFirstFrame
1258 && positionAfterThirdFrame < stream->getLength());
1259
1260 // This does not need to read any more of the stream, since it has already
1261 // parsed the second frame.
1262 decode_frame(r, codec.get(), 1);
1263 REPORTER_ASSERT(r, stream->getPosition() == positionAfterThirdFrame);
1264
1265 // This should read the rest of the frames.
1266 decode_frame(r, codec.get(), 3);
1267 const size_t finalPosition = stream->getPosition();
1268 REPORTER_ASSERT(r, finalPosition > positionAfterThirdFrame);
1269
1270 // There may be more data in the stream.
1271 auto frameInfo = codec->getFrameInfo();
1272 REPORTER_ASSERT(r, frameInfo.size() == 4);
1273 REPORTER_ASSERT(r, stream->getPosition() >= finalPosition);
1274}
1275
scroggo8e6c7ad2016-09-16 08:20:38 -07001276// Only rewinds up to a limit.
1277class LimitedRewindingStream : public SkStream {
1278public:
1279 static SkStream* Make(const char path[], size_t limit) {
1280 SkStream* stream = GetResourceAsStream(path);
1281 if (!stream) {
1282 return nullptr;
1283 }
1284 return new LimitedRewindingStream(stream, limit);
1285 }
1286
1287 size_t read(void* buffer, size_t size) override {
1288 const size_t bytes = fStream->read(buffer, size);
1289 fPosition += bytes;
1290 return bytes;
1291 }
1292
1293 bool isAtEnd() const override {
1294 return fStream->isAtEnd();
1295 }
1296
1297 bool rewind() override {
1298 if (fPosition <= fLimit && fStream->rewind()) {
1299 fPosition = 0;
1300 return true;
1301 }
1302
1303 return false;
1304 }
1305
1306private:
Ben Wagner145dbcd2016-11-03 14:40:50 -04001307 std::unique_ptr<SkStream> fStream;
1308 const size_t fLimit;
1309 size_t fPosition;
scroggo8e6c7ad2016-09-16 08:20:38 -07001310
1311 LimitedRewindingStream(SkStream* stream, size_t limit)
1312 : fStream(stream)
1313 , fLimit(limit)
1314 , fPosition(0)
1315 {
1316 SkASSERT(fStream);
1317 }
1318};
1319
1320DEF_TEST(Codec_fallBack, r) {
1321 // SkAndroidCodec needs to be able to fall back to scanline decoding
1322 // if incremental decoding does not work. Make sure this does not
1323 // require a rewind.
1324
1325 // Formats that currently do not support incremental decoding
1326 auto files = {
scroggo8e6c7ad2016-09-16 08:20:38 -07001327 "CMYK.jpg",
1328 "color_wheel.ico",
1329 "mandrill.wbmp",
1330 "randPixels.bmp",
1331 };
1332 for (auto file : files) {
1333 SkStream* stream = LimitedRewindingStream::Make(file, 14);
1334 if (!stream) {
1335 SkDebugf("Missing resources (%s). Set --resourcePath.\n", file);
1336 return;
1337 }
1338
Ben Wagner145dbcd2016-11-03 14:40:50 -04001339 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
scroggo8e6c7ad2016-09-16 08:20:38 -07001340 if (!codec) {
1341 ERRORF(r, "Failed to create codec for %s,", file);
1342 continue;
1343 }
1344
1345 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
1346 SkBitmap bm;
1347 bm.allocPixels(info);
1348
1349 if (SkCodec::kUnimplemented != codec->startIncrementalDecode(info, bm.getPixels(),
1350 bm.rowBytes())) {
1351 ERRORF(r, "Is scanline decoding now implemented for %s?", file);
1352 continue;
1353 }
1354
1355 // Scanline decoding should not require a rewind.
1356 SkCodec::Result result = codec->startScanlineDecode(info);
1357 if (SkCodec::kSuccess != result) {
1358 ERRORF(r, "Scanline decoding failed for %s with %i", file, result);
1359 }
1360 }
msarett2ecc35f2016-09-08 11:55:16 -07001361}
scroggoc46cdd42016-10-10 06:45:32 -07001362
1363// This test verifies that we fixed an assert statement that fired when reusing a png codec
1364// after scaling.
1365DEF_TEST(Codec_reusePng, r) {
1366 std::unique_ptr<SkStream> stream(GetResourceAsStream("plane.png"));
1367 if (!stream) {
1368 return;
1369 }
1370
1371 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
1372 if (!codec) {
1373 ERRORF(r, "Failed to create codec\n");
1374 return;
1375 }
1376
1377 SkAndroidCodec::AndroidOptions opts;
1378 opts.fSampleSize = 5;
1379 auto size = codec->getSampledDimensions(opts.fSampleSize);
1380 auto info = codec->getInfo().makeWH(size.fWidth, size.fHeight).makeColorType(kN32_SkColorType);
1381 SkBitmap bm;
1382 bm.allocPixels(info);
1383 auto result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1384 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1385
1386 info = codec->getInfo().makeColorType(kN32_SkColorType);
1387 bm.allocPixels(info);
1388 opts.fSampleSize = 1;
1389 result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1390 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1391}
scroggoe61b3b42016-10-10 07:17:32 -07001392
1393DEF_TEST(Codec_rowsDecoded, r) {
1394 auto file = "plane_interlaced.png";
1395 std::unique_ptr<SkStream> stream(GetResourceAsStream(file));
1396 if (!stream) {
1397 return;
1398 }
1399
1400 // This is enough to read the header etc, but no rows.
1401 auto data = SkData::MakeFromStream(stream.get(), 99);
1402 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
1403 if (!codec) {
1404 ERRORF(r, "Failed to create codec\n");
1405 return;
1406 }
1407
1408 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1409 SkBitmap bm;
1410 bm.allocPixels(info);
1411 auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
1412 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1413
1414 // This is an arbitrary value. The important fact is that it is not zero, and rowsDecoded
1415 // should get set to zero by incrementalDecode.
1416 int rowsDecoded = 77;
1417 result = codec->incrementalDecode(&rowsDecoded);
1418 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput);
1419 REPORTER_ASSERT(r, rowsDecoded == 0);
1420}
Matt Sarett29121eb2016-10-17 14:32:46 -04001421
Matt Sarett8a4e9c52016-10-25 14:24:50 -04001422static void test_invalid_images(skiatest::Reporter* r, const char* path, bool shouldSucceed) {
Matt Sarett29121eb2016-10-17 14:32:46 -04001423 SkBitmap bitmap;
Matt Sarett8a4e9c52016-10-25 14:24:50 -04001424 const bool success = GetResourceAsBitmap(path, &bitmap);
1425 REPORTER_ASSERT(r, success == shouldSucceed);
1426}
1427
1428DEF_TEST(Codec_InvalidImages, r) {
1429 // ASAN will complain if there is an issue.
1430 test_invalid_images(r, "invalid_images/int_overflow.ico", false);
1431 test_invalid_images(r, "invalid_images/skbug5887.gif", true);
Matt Sarettdbdf6d22016-11-08 15:26:56 -05001432 test_invalid_images(r, "invalid_images/many-progressive-scans.jpg", false);
Matt Sarett29121eb2016-10-17 14:32:46 -04001433}
Leon Scroggins III56e32092016-12-12 17:10:46 -05001434
Leon Scroggins III0b24cbd2016-12-15 15:58:22 -05001435DEF_TEST(Codec_InvalidBmp, r) {
1436 // This file reports a header size that crashes when we try to read this
1437 // much directly from a file using SkFILEStream.
1438 SkString path = GetResourcePath("invalid_images/b33651913.bmp");
1439 std::unique_ptr<SkFILEStream> stream(new SkFILEStream(path.c_str()));
1440 if (!stream->isValid()) {
1441 ERRORF(r, "no stream");
1442 return;
1443 }
1444
1445 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
1446 // This file is invalid, but more importantly, we did not crash before
1447 // reaching here.
1448 REPORTER_ASSERT(r, !codec);
1449}
1450
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -05001451DEF_TEST(Codec_InvalidRLEBmp, r) {
1452 auto* stream = GetResourceAsStream("invalid_images/b33251605.bmp");
1453 if (!stream) {
1454 return;
1455 }
1456
1457 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
1458 REPORTER_ASSERT(r, codec);
1459
1460 test_info(r, codec.get(), codec->getInfo(), SkCodec::kIncompleteInput, nullptr);
1461}
1462
Leon Scroggins III56e32092016-12-12 17:10:46 -05001463DEF_TEST(Codec_InvalidAnimated, r) {
1464 // ASAN will complain if there is an issue.
1465 auto path = "invalid_images/skbug6046.gif";
1466 auto* stream = GetResourceAsStream(path);
1467 if (!stream) {
1468 return;
1469 }
1470
1471 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
1472 REPORTER_ASSERT(r, codec);
1473 if (!codec) {
1474 return;
1475 }
1476
1477 const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1478 SkBitmap bm;
1479 bm.allocPixels(info);
1480
1481 auto frameInfos = codec->getFrameInfo();
1482 SkCodec::Options opts;
1483 for (size_t i = 0; i < frameInfos.size(); i++) {
1484 opts.fFrameIndex = i;
1485 opts.fHasPriorFrame = frameInfos[i].fRequiredFrame == i - 1;
1486 auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes(), &opts);
1487 if (result != SkCodec::kSuccess) {
1488 ERRORF(r, "Failed to start decoding frame %i (out of %i) with error %i\n", i,
1489 frameInfos.size(), result);
1490 continue;
1491 }
1492
1493 codec->incrementalDecode();
1494 }
1495}