blob: df9454760086cd4f8853c4968acc318f26a61cdd [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"
Matt Sarett0e032be2017-03-15 17:50:08 -040016#include "SkColorSpacePriv.h"
msarette6dd0042015-10-09 11:07:34 -070017#include "SkData.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000018#include "SkFrontBufferedStream.h"
Hal Canary95e3c052017-01-11 12:44:43 -050019#include "SkImageEncoder.h"
Matt Sarett0e032be2017-03-15 17:50:08 -040020#include "SkImageEncoderPriv.h"
Mike Reedede7bac2017-07-23 15:30:02 -040021#include "SkMakeUnique.h"
halcanarya096d7a2015-03-27 12:16:53 -070022#include "SkMD5.h"
Leon Scroggins III0354c622017-02-24 15:33:24 -050023#include "SkOSPath.h"
Matt Sarett26b44df2017-05-02 16:04:56 -040024#include "SkJpegEncoder.h"
Hal Canary95e3c052017-01-11 12:44:43 -050025#include "SkPngChunkReader.h"
Matt Sarettc367d032017-05-05 11:13:26 -040026#include "SkPngEncoder.h"
scroggob636b452015-07-22 07:16:20 -070027#include "SkRandom.h"
scroggocf98fa92015-11-23 08:14:40 -080028#include "SkStream.h"
scroggob9a1e342015-11-30 06:25:31 -080029#include "SkStreamPriv.h"
Matt Sarett04c37312017-05-05 14:02:13 -040030#include "SkWebpEncoder.h"
halcanarya096d7a2015-03-27 12:16:53 -070031#include "Test.h"
32
scroggocf98fa92015-11-23 08:14:40 -080033#include "png.h"
34
Hal Canarydb683012016-11-23 08:55:18 -070035#include "sk_tool_utils.h"
36
scroggo8e6c7ad2016-09-16 08:20:38 -070037#if PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 5
38 // FIXME (scroggo): Google3 needs to be updated to use a newer version of libpng. In
39 // the meantime, we had to break some pieces of SkPngCodec in order to support Google3.
40 // The parts that are broken are likely not used by Google3.
41 #define SK_PNG_DISABLE_TESTS
42#endif
43
halcanarya096d7a2015-03-27 12:16:53 -070044static void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
halcanarya096d7a2015-03-27 12:16:53 -070045 SkASSERT(bm.getPixels());
46 SkMD5 md5;
47 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
48 for (int y = 0; y < bm.height(); ++y) {
halcanary1e903042016-04-25 10:29:36 -070049 md5.write(bm.getAddr(0, y), rowLen);
halcanarya096d7a2015-03-27 12:16:53 -070050 }
51 md5.finish(*digest);
52}
53
scroggo9b2cdbf42015-07-10 12:07:02 -070054/**
55 * Compute the digest for bm and compare it to a known good digest.
56 * @param r Reporter to assert that bm's digest matches goodDigest.
57 * @param goodDigest The known good digest to compare to.
58 * @param bm The bitmap to test.
59 */
60static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
61 const SkBitmap& bm) {
62 SkMD5::Digest digest;
63 md5(bm, &digest);
64 REPORTER_ASSERT(r, digest == goodDigest);
65}
66
scroggod1bc5742015-08-12 08:31:44 -070067/**
68 * Test decoding an SkCodec to a particular SkImageInfo.
69 *
halcanary96fcdcc2015-08-27 07:41:13 -070070 * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr,
scroggod1bc5742015-08-12 08:31:44 -070071 * the resulting decode should match.
72 */
scroggo7b5e5532016-02-04 06:14:24 -080073template<typename Codec>
74static void test_info(skiatest::Reporter* r, Codec* codec, const SkImageInfo& info,
scroggod1bc5742015-08-12 08:31:44 -070075 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
76 SkBitmap bm;
77 bm.allocPixels(info);
scroggod1bc5742015-08-12 08:31:44 -070078
79 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
80 REPORTER_ASSERT(r, result == expectedResult);
81
82 if (goodDigest) {
83 compare_to_good_digest(r, *goodDigest, bm);
84 }
85}
86
scroggob636b452015-07-22 07:16:20 -070087SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
88 SkIRect rect;
89 do {
90 rect.fLeft = rand->nextRangeU(0, w);
91 rect.fTop = rand->nextRangeU(0, h);
92 rect.fRight = rand->nextRangeU(0, w);
93 rect.fBottom = rand->nextRangeU(0, h);
94 rect.sort();
95 } while (rect.isEmpty());
96 return rect;
97}
98
scroggo8e6c7ad2016-09-16 08:20:38 -070099static void test_incremental_decode(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
100 const SkMD5::Digest& goodDigest) {
101 SkBitmap bm;
102 bm.allocPixels(info);
scroggo8e6c7ad2016-09-16 08:20:38 -0700103
104 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->startIncrementalDecode(info, bm.getPixels(),
105 bm.rowBytes()));
106
107 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->incrementalDecode());
108
109 compare_to_good_digest(r, goodDigest, bm);
110}
111
112// Test in stripes, similar to DM's kStripe_Mode
113static void test_in_stripes(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
114 const SkMD5::Digest& goodDigest) {
115 SkBitmap bm;
116 bm.allocPixels(info);
117 bm.eraseColor(SK_ColorYELLOW);
118
119 const int height = info.height();
120 // Note that if numStripes does not evenly divide height there will be an extra
121 // stripe.
122 const int numStripes = 4;
123
124 if (numStripes > height) {
125 // Image is too small.
126 return;
127 }
128
129 const int stripeHeight = height / numStripes;
130
131 // Iterate through the image twice. Once to decode odd stripes, and once for even.
132 for (int oddEven = 1; oddEven >= 0; oddEven--) {
133 for (int y = oddEven * stripeHeight; y < height; y += 2 * stripeHeight) {
134 SkIRect subset = SkIRect::MakeLTRB(0, y, info.width(),
135 SkTMin(y + stripeHeight, height));
136 SkCodec::Options options;
137 options.fSubset = &subset;
138 if (SkCodec::kSuccess != codec->startIncrementalDecode(info, bm.getAddr(0, y),
139 bm.rowBytes(), &options)) {
140 ERRORF(r, "failed to start incremental decode!\ttop: %i\tbottom%i\n",
141 subset.top(), subset.bottom());
142 return;
143 }
144 if (SkCodec::kSuccess != codec->incrementalDecode()) {
145 ERRORF(r, "failed incremental decode starting from line %i\n", y);
146 return;
147 }
148 }
149 }
150
151 compare_to_good_digest(r, goodDigest, bm);
152}
153
scroggo7b5e5532016-02-04 06:14:24 -0800154template<typename Codec>
155static void test_codec(skiatest::Reporter* r, Codec* codec, SkBitmap& bm, const SkImageInfo& info,
scroggo27c17282015-10-27 08:14:46 -0700156 const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest,
157 const SkMD5::Digest* goodDigest) {
msarette6dd0042015-10-09 11:07:34 -0700158
halcanarya096d7a2015-03-27 12:16:53 -0700159 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -0700160 bm.allocPixels(info);
msarettcc7f3052015-10-05 14:20:27 -0700161
162 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700163 REPORTER_ASSERT(r, result == expectedResult);
halcanarya096d7a2015-03-27 12:16:53 -0700164
msarettcc7f3052015-10-05 14:20:27 -0700165 md5(bm, digest);
166 if (goodDigest) {
167 REPORTER_ASSERT(r, *digest == *goodDigest);
168 }
halcanarya096d7a2015-03-27 12:16:53 -0700169
msarett8ff6ca62015-09-18 12:06:04 -0700170 {
171 // Test decoding to 565
172 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggoba584892016-05-20 13:56:13 -0700173 if (info.alphaType() == kOpaque_SkAlphaType) {
174 // Decoding to 565 should succeed.
175 SkBitmap bm565;
176 bm565.allocPixels(info565);
scroggoba584892016-05-20 13:56:13 -0700177
178 // This will allow comparison even if the image is incomplete.
179 bm565.eraseColor(SK_ColorBLACK);
180
181 REPORTER_ASSERT(r, expectedResult == codec->getPixels(info565,
182 bm565.getPixels(), bm565.rowBytes()));
183
184 SkMD5::Digest digest565;
185 md5(bm565, &digest565);
186
187 // A dumb client's request for non-opaque should also succeed.
188 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
189 info565 = info565.makeAlphaType(alpha);
190 test_info(r, codec, info565, expectedResult, &digest565);
191 }
192 } else {
193 test_info(r, codec, info565, SkCodec::kInvalidConversion, nullptr);
194 }
195 }
196
197 if (codec->getInfo().colorType() == kGray_8_SkColorType) {
198 SkImageInfo grayInfo = codec->getInfo();
199 SkBitmap grayBm;
200 grayBm.allocPixels(grayInfo);
scroggoba584892016-05-20 13:56:13 -0700201
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();
Mike Reedede7bac2017-07-23 15:30:02 -0400277 codec = SkCodec::MakeFromData(SkData::MakeFromStream(stream.get(), 2 * size / 3));
msarette6dd0042015-10-09 11:07:34 -0700278 } else {
Mike Reedede7bac2017-07-23 15:30:02 -0400279 codec = SkCodec::MakeFromStream(std::move(stream));
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
Leon Scroggins571b30f2017-07-11 17:35:31 +0000351 const auto partialStartResult = codec->startScanlineDecode(info, &options);
msarett80803ff2015-10-16 10:54:12 -0700352 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
353
354 for (int y = 0; y < height; y++) {
355 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
356 if (!isIncomplete) {
357 REPORTER_ASSERT(r, 1 == lines);
358 }
359 }
360 }
scroggo58421542015-04-01 11:25:20 -0700361 } else {
scroggo46c57472015-09-30 08:57:13 -0700362 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700363 }
scroggob636b452015-07-22 07:16:20 -0700364
365 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
366 // random subsets.
367 // Do not attempt to decode subsets of an image of only once pixel, since there is no
368 // meaningful subset.
369 if (size.width() * size.height() == 1) {
370 return;
371 }
372
373 SkRandom rand;
374 SkIRect subset;
375 SkCodec::Options opts;
376 opts.fSubset = &subset;
377 for (int i = 0; i < 5; i++) {
378 subset = generate_random_subset(&rand, size.width(), size.height());
379 SkASSERT(!subset.isEmpty());
380 const bool supported = codec->getValidSubset(&subset);
381 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
382
383 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
384 SkBitmap bm;
385 bm.allocPixels(subsetInfo);
Leon Scroggins571b30f2017-07-11 17:35:31 +0000386 const auto result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(), &opts);
scroggob636b452015-07-22 07:16:20 -0700387
388 if (supportsSubsetDecoding) {
Leon Scroggins III58f100c2016-12-20 09:49:25 -0500389 if (expectedResult == SkCodec::kSuccess) {
390 REPORTER_ASSERT(r, result == expectedResult);
Leon Scroggins III58f100c2016-12-20 09:49:25 -0500391 }
scroggob636b452015-07-22 07:16:20 -0700392 // Webp is the only codec that supports subsets, and it will have modified the subset
393 // to have even left/top.
394 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
395 } else {
396 // No subsets will work.
397 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
398 }
399 }
msarettcc7f3052015-10-05 14:20:27 -0700400
scroggobed1ed62016-02-11 10:24:55 -0800401 // SkAndroidCodec tests
scroggo8e6c7ad2016-09-16 08:20:38 -0700402 if (supportsScanlineDecoding || supportsSubsetDecoding || supportsNewScanlineDecoding) {
scroggo2c3b2182015-10-09 08:40:59 -0700403
Ben Wagner145dbcd2016-11-03 14:40:50 -0400404 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarettcc7f3052015-10-05 14:20:27 -0700405 if (!stream) {
msarettcc7f3052015-10-05 14:20:27 -0700406 return;
407 }
msarette6dd0042015-10-09 11:07:34 -0700408
Leon Scroggins III7397d7a2018-01-04 13:26:30 -0500409 auto androidCodec = SkAndroidCodec::MakeFromCodec(std::move(codec));
scroggo7b5e5532016-02-04 06:14:24 -0800410 if (!androidCodec) {
msarettcc7f3052015-10-05 14:20:27 -0700411 ERRORF(r, "Unable to decode '%s'", path);
412 return;
413 }
414
415 SkBitmap bm;
scroggobed1ed62016-02-11 10:24:55 -0800416 SkMD5::Digest androidCodecDigest;
417 test_codec(r, androidCodec.get(), bm, info, size, expectedResult, &androidCodecDigest,
scroggo7b5e5532016-02-04 06:14:24 -0800418 &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700419 }
420
msarettedd2dcf2016-01-14 13:12:26 -0800421 if (!isIncomplete) {
scroggoef0fed32016-02-18 05:59:25 -0800422 // Test SkCodecImageGenerator
Ben Wagner145dbcd2016-11-03 14:40:50 -0400423 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
424 sk_sp<SkData> fullData(SkData::MakeFromStream(stream.get(), stream->getLength()));
425 std::unique_ptr<SkImageGenerator> gen(
Mike Reed185130c2017-02-15 15:14:16 -0500426 SkCodecImageGenerator::MakeFromEncodedCodec(fullData));
msarettedd2dcf2016-01-14 13:12:26 -0800427 SkBitmap bm;
428 bm.allocPixels(info);
msarettedd2dcf2016-01-14 13:12:26 -0800429 REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes()));
430 compare_to_good_digest(r, codecDigest, bm);
scroggoef0fed32016-02-18 05:59:25 -0800431
scroggo8e6c7ad2016-09-16 08:20:38 -0700432#ifndef SK_PNG_DISABLE_TESTS
scroggod8d68552016-06-06 11:26:17 -0700433 // Test using SkFrontBufferedStream, as Android does
Mike Reed98c5d922017-09-15 21:39:47 -0400434 auto bufferedStream = SkFrontBufferedStream::Make(
435 SkMemoryStream::Make(std::move(fullData)), SkCodec::MinBufferedBytesNeeded());
scroggod8d68552016-06-06 11:26:17 -0700436 REPORTER_ASSERT(r, bufferedStream);
Mike Reed98c5d922017-09-15 21:39:47 -0400437 codec = SkCodec::MakeFromStream(std::move(bufferedStream));
scroggod8d68552016-06-06 11:26:17 -0700438 REPORTER_ASSERT(r, codec);
439 if (codec) {
440 test_info(r, codec.get(), info, SkCodec::kSuccess, &codecDigest);
scroggoef0fed32016-02-18 05:59:25 -0800441 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700442#endif
msarettedd2dcf2016-01-14 13:12:26 -0800443 }
444
msarette6dd0042015-10-09 11:07:34 -0700445 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
446 if (isIncomplete) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700447 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false,
448 supportsNewScanlineDecoding);
msarettcc7f3052015-10-05 14:20:27 -0700449 }
halcanarya096d7a2015-03-27 12:16:53 -0700450}
451
Leon Scroggins III83926342016-12-06 10:58:02 -0500452DEF_TEST(Codec_wbmp, r) {
Hal Canaryc465d132017-12-08 10:21:31 -0500453 check(r, "images/mandrill.wbmp", SkISize::Make(512, 512), true, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500454}
halcanarya096d7a2015-03-27 12:16:53 -0700455
Leon Scroggins III83926342016-12-06 10:58:02 -0500456DEF_TEST(Codec_webp, r) {
Hal Canaryc465d132017-12-08 10:21:31 -0500457 check(r, "images/baby_tux.webp", SkISize::Make(386, 395), false, true, true);
458 check(r, "images/color_wheel.webp", SkISize::Make(128, 128), false, true, true);
459 check(r, "images/yellow_rose.webp", SkISize::Make(400, 301), false, true, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500460}
scroggo6f5e6192015-06-18 12:53:43 -0700461
Leon Scroggins III83926342016-12-06 10:58:02 -0500462DEF_TEST(Codec_bmp, r) {
Hal Canaryc465d132017-12-08 10:21:31 -0500463 check(r, "images/randPixels.bmp", SkISize::Make(8, 8), true, false, true);
464 check(r, "images/rle.bmp", SkISize::Make(320, 240), true, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500465}
halcanarya096d7a2015-03-27 12:16:53 -0700466
Leon Scroggins III83926342016-12-06 10:58:02 -0500467DEF_TEST(Codec_ico, r) {
msarette6dd0042015-10-09 11:07:34 -0700468 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700469 // These two tests examine interestingly different behavior:
470 // Decodes an embedded BMP image
Hal Canaryc465d132017-12-08 10:21:31 -0500471 check(r, "images/color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700472 // Decodes an embedded PNG image
Hal Canaryc465d132017-12-08 10:21:31 -0500473 check(r, "images/google_chrome.ico", SkISize::Make(256, 256), false, false, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500474}
halcanarya096d7a2015-03-27 12:16:53 -0700475
Leon Scroggins III83926342016-12-06 10:58:02 -0500476DEF_TEST(Codec_gif, r) {
Hal Canaryc465d132017-12-08 10:21:31 -0500477 check(r, "images/box.gif", SkISize::Make(200, 55), false, false, true, true);
478 check(r, "images/color_wheel.gif", SkISize::Make(128, 128), false, false, true, true);
msarette6dd0042015-10-09 11:07:34 -0700479 // randPixels.gif is too small to test incomplete
Hal Canaryc465d132017-12-08 10:21:31 -0500480 check(r, "images/randPixels.gif", SkISize::Make(8, 8), false, false, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500481}
msarett438b2ad2015-04-09 12:43:10 -0700482
Leon Scroggins III83926342016-12-06 10:58:02 -0500483DEF_TEST(Codec_jpg, r) {
Hal Canaryc465d132017-12-08 10:21:31 -0500484 check(r, "images/CMYK.jpg", SkISize::Make(642, 516), true, false, true);
485 check(r, "images/color_wheel.jpg", SkISize::Make(128, 128), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700486 // grayscale.jpg is too small to test incomplete
Hal Canaryc465d132017-12-08 10:21:31 -0500487 check(r, "images/grayscale.jpg", SkISize::Make(128, 128), true, false, false);
488 check(r, "images/mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700489 // randPixels.jpg is too small to test incomplete
Hal Canaryc465d132017-12-08 10:21:31 -0500490 check(r, "images/randPixels.jpg", SkISize::Make(8, 8), true, false, false);
Leon Scroggins III83926342016-12-06 10:58:02 -0500491}
msarette16b04a2015-04-15 07:32:19 -0700492
Leon Scroggins III83926342016-12-06 10:58:02 -0500493DEF_TEST(Codec_png, r) {
Hal Canaryc465d132017-12-08 10:21:31 -0500494 check(r, "images/arrow.png", SkISize::Make(187, 312), false, false, true, true);
495 check(r, "images/baby_tux.png", SkISize::Make(240, 246), false, false, true, true);
496 check(r, "images/color_wheel.png", SkISize::Make(128, 128), false, false, true, true);
scroggo8e6c7ad2016-09-16 08:20:38 -0700497 // half-transparent-white-pixel.png is too small to test incomplete
Hal Canaryc465d132017-12-08 10:21:31 -0500498 check(r, "images/half-transparent-white-pixel.png", SkISize::Make(1, 1), false, false, false, true);
499 check(r, "images/mandrill_128.png", SkISize::Make(128, 128), false, false, true, true);
500 check(r, "images/mandrill_16.png", SkISize::Make(16, 16), false, false, true, true);
501 check(r, "images/mandrill_256.png", SkISize::Make(256, 256), false, false, true, true);
502 check(r, "images/mandrill_32.png", SkISize::Make(32, 32), false, false, true, true);
503 check(r, "images/mandrill_512.png", SkISize::Make(512, 512), false, false, true, true);
504 check(r, "images/mandrill_64.png", SkISize::Make(64, 64), false, false, true, true);
505 check(r, "images/plane.png", SkISize::Make(250, 126), false, false, true, true);
506 check(r, "images/plane_interlaced.png", SkISize::Make(250, 126), false, false, true, true);
507 check(r, "images/randPixels.png", SkISize::Make(8, 8), false, false, true, true);
508 check(r, "images/yellow_rose.png", SkISize::Make(400, 301), false, false, true, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500509}
yujieqin916de9f2016-01-25 08:26:16 -0800510
yujieqinf236ee42016-02-29 07:14:42 -0800511// Disable RAW tests for Win32.
512#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Leon Scroggins III83926342016-12-06 10:58:02 -0500513DEF_TEST(Codec_raw, r) {
Hal Canaryc465d132017-12-08 10:21:31 -0500514 check(r, "images/sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
515 check(r, "images/sample_1mp_rotated.dng", SkISize::Make(600, 338), false, false, false);
516 check(r, "images/dng_with_preview.dng", SkISize::Make(600, 338), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700517}
Leon Scroggins III83926342016-12-06 10:58:02 -0500518#endif
scroggo0a7e69c2015-04-03 07:22:22 -0700519
520static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700521 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
Mike Reedede7bac2017-07-23 15:30:02 -0400522 REPORTER_ASSERT(r, !SkCodec::MakeFromStream(
523 skstd::make_unique<SkMemoryStream>(stream, len, false)));
524 REPORTER_ASSERT(r, !SkAndroidCodec::MakeFromStream(
525 skstd::make_unique<SkMemoryStream>(stream, len, false)));
scroggo0a7e69c2015-04-03 07:22:22 -0700526}
527
528// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
529// even on failure. Test some bad streams.
530DEF_TEST(Codec_leaks, r) {
531 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
532 const char nonSupportedStream[] = "hello world";
533 // The other strings should look like the beginning of a file type, so we'll call some
534 // internal version of NewFromStream, which must also delete the stream on failure.
535 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
536 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
537 const char emptyWebp[] = "RIFF1234WEBPVP";
538 const char emptyBmp[] = { 'B', 'M' };
539 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
540 const char emptyGif[] = "GIFVER";
541
542 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
543 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
544 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
545 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
546 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
547 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
548 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
549}
msarette16b04a2015-04-15 07:32:19 -0700550
scroggo2c3b2182015-10-09 08:40:59 -0700551DEF_TEST(Codec_null, r) {
scroggobed1ed62016-02-11 10:24:55 -0800552 // Attempting to create an SkCodec or an SkAndroidCodec with null should not
scroggo2c3b2182015-10-09 08:40:59 -0700553 // crash.
Mike Reedede7bac2017-07-23 15:30:02 -0400554 REPORTER_ASSERT(r, !SkCodec::MakeFromStream(nullptr));
555 REPORTER_ASSERT(r, !SkAndroidCodec::MakeFromStream(nullptr));
scroggo2c3b2182015-10-09 08:40:59 -0700556}
557
msarette16b04a2015-04-15 07:32:19 -0700558static void test_dimensions(skiatest::Reporter* r, const char path[]) {
559 // Create the codec from the resource file
Ben Wagner145dbcd2016-11-03 14:40:50 -0400560 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarette16b04a2015-04-15 07:32:19 -0700561 if (!stream) {
msarette16b04a2015-04-15 07:32:19 -0700562 return;
563 }
Mike Reedede7bac2017-07-23 15:30:02 -0400564 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromStream(std::move(stream)));
msarette16b04a2015-04-15 07:32:19 -0700565 if (!codec) {
566 ERRORF(r, "Unable to create codec '%s'", path);
567 return;
568 }
569
570 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800571 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700572 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700573 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700574 SkImageInfo scaledInfo = codec->getInfo()
575 .makeWH(scaledDims.width(), scaledDims.height())
576 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700577
578 // Set up for the decode
579 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
Mike Reedf0ffb892017-10-03 14:47:21 -0400580 size_t totalBytes = scaledInfo.computeByteSize(rowBytes);
msarette16b04a2015-04-15 07:32:19 -0700581 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
582
msarett3d9d7a72015-10-21 10:27:10 -0700583 SkAndroidCodec::AndroidOptions options;
584 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700585 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700586 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700587 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700588 }
589}
590
591// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
592DEF_TEST(Codec_Dimensions, r) {
593 // JPG
Hal Canaryc465d132017-12-08 10:21:31 -0500594 test_dimensions(r, "images/CMYK.jpg");
595 test_dimensions(r, "images/color_wheel.jpg");
596 test_dimensions(r, "images/grayscale.jpg");
597 test_dimensions(r, "images/mandrill_512_q075.jpg");
598 test_dimensions(r, "images/randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700599
600 // Decoding small images with very large scaling factors is a potential
601 // source of bugs and crashes. We disable these tests in Gold because
602 // tiny images are not very useful to look at.
603 // Here we make sure that we do not crash or access illegal memory when
604 // performing scaled decodes on small images.
Hal Canaryc465d132017-12-08 10:21:31 -0500605 test_dimensions(r, "images/1x1.png");
606 test_dimensions(r, "images/2x2.png");
607 test_dimensions(r, "images/3x3.png");
608 test_dimensions(r, "images/3x1.png");
609 test_dimensions(r, "images/1x1.png");
610 test_dimensions(r, "images/16x1.png");
611 test_dimensions(r, "images/1x16.png");
612 test_dimensions(r, "images/mandrill_16.png");
msarettb32758a2015-08-18 13:22:46 -0700613
yujieqin916de9f2016-01-25 08:26:16 -0800614 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800615// Disable RAW tests for Win32.
616#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Hal Canaryc465d132017-12-08 10:21:31 -0500617 test_dimensions(r, "images/sample_1mp.dng");
618 test_dimensions(r, "images/sample_1mp_rotated.dng");
619 test_dimensions(r, "images/dng_with_preview.dng");
msarett8e49ca32016-01-25 13:10:58 -0800620#endif
msarette16b04a2015-04-15 07:32:19 -0700621}
622
msarettd0375bc2015-08-12 08:08:56 -0700623static void test_invalid(skiatest::Reporter* r, const char path[]) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400624 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett4b17fa32015-04-23 08:53:39 -0700625 if (!stream) {
msarett4b17fa32015-04-23 08:53:39 -0700626 return;
627 }
Mike Reedede7bac2017-07-23 15:30:02 -0400628 REPORTER_ASSERT(r, !SkCodec::MakeFromStream(std::move(stream)));
msarett4b17fa32015-04-23 08:53:39 -0700629}
msarette16b04a2015-04-15 07:32:19 -0700630
msarett4b17fa32015-04-23 08:53:39 -0700631DEF_TEST(Codec_Empty, r) {
632 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700633 test_invalid(r, "empty_images/zero-dims.gif");
634 test_invalid(r, "empty_images/zero-embedded.ico");
635 test_invalid(r, "empty_images/zero-width.bmp");
636 test_invalid(r, "empty_images/zero-height.bmp");
637 test_invalid(r, "empty_images/zero-width.jpg");
638 test_invalid(r, "empty_images/zero-height.jpg");
639 test_invalid(r, "empty_images/zero-width.png");
640 test_invalid(r, "empty_images/zero-height.png");
641 test_invalid(r, "empty_images/zero-width.wbmp");
642 test_invalid(r, "empty_images/zero-height.wbmp");
643 // This image is an ico with an embedded mask-bmp. This is illegal.
644 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
Matt Sarett5c496172017-02-07 17:01:16 -0500645 // It is illegal for a webp frame to not be fully contained by the canvas.
646 test_invalid(r, "invalid_images/invalid-offset.webp");
Leon Scroggins IIId87fbee2016-12-02 16:47:53 -0500647#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
648 test_invalid(r, "empty_images/zero_height.tiff");
649#endif
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400650 test_invalid(r, "invalid_images/b37623797.ico");
msarett4b17fa32015-04-23 08:53:39 -0700651}
msarett99f567e2015-08-05 12:58:26 -0700652
scroggo8e6c7ad2016-09-16 08:20:38 -0700653#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
654
655#ifndef SK_PNG_DISABLE_TESTS // reading chunks does not work properly with older versions.
656 // It does not appear that anyone in Google3 is reading chunks.
657
scroggocf98fa92015-11-23 08:14:40 -0800658static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
659 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
660 if (!sk_stream->write(data, len)) {
661 png_error(png_ptr, "sk_write_fn Error!");
662 }
663}
664
scroggocf98fa92015-11-23 08:14:40 -0800665DEF_TEST(Codec_pngChunkReader, r) {
666 // Create a dummy bitmap. Use unpremul RGBA for libpng.
667 SkBitmap bm;
668 const int w = 1;
669 const int h = 1;
670 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
671 kUnpremul_SkAlphaType);
672 bm.setInfo(bmInfo);
673 bm.allocPixels();
674 bm.eraseColor(SK_ColorBLUE);
675 SkMD5::Digest goodDigest;
676 md5(bm, &goodDigest);
677
678 // Write to a png file.
679 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
680 REPORTER_ASSERT(r, png);
681 if (!png) {
682 return;
683 }
684
685 png_infop info = png_create_info_struct(png);
686 REPORTER_ASSERT(r, info);
687 if (!info) {
688 png_destroy_write_struct(&png, nullptr);
689 return;
690 }
691
692 if (setjmp(png_jmpbuf(png))) {
693 ERRORF(r, "failed writing png");
694 png_destroy_write_struct(&png, &info);
695 return;
696 }
697
698 SkDynamicMemoryWStream wStream;
699 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
700
701 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
702 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
703 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
704
705 // Create some chunks that match the Android framework's use.
706 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800707 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
708 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
709 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800710 };
711
712 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
713 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
714#if PNG_LIBPNG_VER < 10600
715 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800716 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
717 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800718#endif
719
720 png_write_info(png, info);
721
722 for (int j = 0; j < h; j++) {
723 png_bytep row = (png_bytep)(bm.getAddr(0, j));
724 png_write_rows(png, &row, 1);
725 }
726 png_write_end(png, info);
727 png_destroy_write_struct(&png, &info);
728
729 class ChunkReader : public SkPngChunkReader {
730 public:
731 ChunkReader(skiatest::Reporter* r)
732 : fReporter(r)
733 {
734 this->reset();
735 }
736
737 bool readChunk(const char tag[], const void* data, size_t length) override {
738 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
739 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
740 // Tag matches. This should have been the first time we see it.
741 REPORTER_ASSERT(fReporter, !fSeen[i]);
742 fSeen[i] = true;
743
744 // Data and length should match
745 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
746 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
747 (const char*) gUnknowns[i].data));
748 return true;
749 }
750 }
751 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
752 return true;
753 }
754
755 bool allHaveBeenSeen() {
756 bool ret = true;
757 for (auto seen : fSeen) {
758 ret &= seen;
759 }
760 return ret;
761 }
762
763 void reset() {
764 sk_bzero(fSeen, sizeof(fSeen));
765 }
766
767 private:
768 skiatest::Reporter* fReporter; // Unowned
769 bool fSeen[3];
770 };
771
772 ChunkReader chunkReader(r);
773
774 // Now read the file with SkCodec.
Mike Reedede7bac2017-07-23 15:30:02 -0400775 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(wStream.detachAsData(), &chunkReader));
scroggocf98fa92015-11-23 08:14:40 -0800776 REPORTER_ASSERT(r, codec);
777 if (!codec) {
778 return;
779 }
780
781 // Now compare to the original.
782 SkBitmap decodedBm;
783 decodedBm.setInfo(codec->getInfo());
784 decodedBm.allocPixels();
785 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
786 decodedBm.rowBytes());
787 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
788
789 if (decodedBm.colorType() != bm.colorType()) {
790 SkBitmap tmp;
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400791 bool success = sk_tool_utils::copy_to(&tmp, bm.colorType(), decodedBm);
scroggocf98fa92015-11-23 08:14:40 -0800792 REPORTER_ASSERT(r, success);
793 if (!success) {
794 return;
795 }
796
797 tmp.swap(decodedBm);
798 }
799
800 compare_to_good_digest(r, goodDigest, decodedBm);
801 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
802
803 // Decoding again will read the chunks again.
804 chunkReader.reset();
805 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
806 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
807 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
808 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
809}
scroggo8e6c7ad2016-09-16 08:20:38 -0700810#endif // SK_PNG_DISABLE_TESTS
scroggocf98fa92015-11-23 08:14:40 -0800811#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800812
scroggodb30be22015-12-08 18:54:13 -0800813// Stream that can only peek up to a limit
814class LimitedPeekingMemStream : public SkStream {
815public:
reed42943c82016-09-12 12:01:44 -0700816 LimitedPeekingMemStream(sk_sp<SkData> data, size_t limit)
817 : fStream(std::move(data))
scroggodb30be22015-12-08 18:54:13 -0800818 , fLimit(limit) {}
819
820 size_t peek(void* buf, size_t bytes) const override {
821 return fStream.peek(buf, SkTMin(bytes, fLimit));
822 }
823 size_t read(void* buf, size_t bytes) override {
824 return fStream.read(buf, bytes);
825 }
826 bool rewind() override {
827 return fStream.rewind();
828 }
829 bool isAtEnd() const override {
msarettff2a6c82016-09-07 11:23:28 -0700830 return fStream.isAtEnd();
scroggodb30be22015-12-08 18:54:13 -0800831 }
832private:
833 SkMemoryStream fStream;
834 const size_t fLimit;
835};
836
yujieqinf236ee42016-02-29 07:14:42 -0800837// Disable RAW tests for Win32.
838#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin9c7a8a42016-02-05 08:21:19 -0800839// Test that the RawCodec works also for not asset stream. This will test the code path using
840// SkRawBufferedStream instead of SkRawAssetStream.
yujieqin9c7a8a42016-02-05 08:21:19 -0800841DEF_TEST(Codec_raw_notseekable, r) {
Mike Reed0933bc92017-12-09 01:27:41 +0000842 constexpr char path[] = "images/dng_with_preview.dng";
843 sk_sp<SkData> data(GetResourceAsData(path));
yujieqin9c7a8a42016-02-05 08:21:19 -0800844 if (!data) {
845 SkDebugf("Missing resource '%s'\n", path);
846 return;
847 }
848
Mike Reedede7bac2017-07-23 15:30:02 -0400849 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(
850 skstd::make_unique<NotAssetMemStream>(std::move(data))));
yujieqin9c7a8a42016-02-05 08:21:19 -0800851 REPORTER_ASSERT(r, codec);
852
853 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
854}
855#endif
856
scroggodb30be22015-12-08 18:54:13 -0800857// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
858// + rewind() and succeed.
859DEF_TEST(Codec_webp_peek, r) {
Mike Reed0933bc92017-12-09 01:27:41 +0000860 constexpr char path[] = "images/baby_tux.webp";
861 auto data = GetResourceAsData(path);
scroggodb30be22015-12-08 18:54:13 -0800862 if (!data) {
863 SkDebugf("Missing resource '%s'\n", path);
864 return;
865 }
866
867 // The limit is less than webp needs to peek or read.
Mike Reedede7bac2017-07-23 15:30:02 -0400868 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(
869 skstd::make_unique<LimitedPeekingMemStream>(data, 25)));
scroggodb30be22015-12-08 18:54:13 -0800870 REPORTER_ASSERT(r, codec);
871
scroggo7b5e5532016-02-04 06:14:24 -0800872 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800873
874 // Similarly, a stream which does not peek should still succeed.
Mike Reedede7bac2017-07-23 15:30:02 -0400875 codec = SkCodec::MakeFromStream(skstd::make_unique<LimitedPeekingMemStream>(data, 0));
scroggodb30be22015-12-08 18:54:13 -0800876 REPORTER_ASSERT(r, codec);
877
scroggo7b5e5532016-02-04 06:14:24 -0800878 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800879}
880
msarett7f7ec202016-03-01 12:12:27 -0800881// SkCodec's wbmp decoder was initially unnecessarily restrictive.
882// It required the second byte to be zero. The wbmp specification allows
883// a couple of bits to be 1 (so long as they do not overlap with 0x9F).
884// Test that SkCodec now supports an image with these bits set.
Leon Scroggins III83926342016-12-06 10:58:02 -0500885DEF_TEST(Codec_wbmp_restrictive, r) {
Hal Canaryc465d132017-12-08 10:21:31 -0500886 const char* path = "images/mandrill.wbmp";
Ben Wagner145dbcd2016-11-03 14:40:50 -0400887 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
scroggob9a1e342015-11-30 06:25:31 -0800888 if (!stream) {
scroggob9a1e342015-11-30 06:25:31 -0800889 return;
890 }
891
892 // Modify the stream to contain a second byte with some bits set.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400893 auto data = SkCopyStreamToData(stream.get());
scroggob9a1e342015-11-30 06:25:31 -0800894 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
895 writeableData[1] = static_cast<uint8_t>(~0x9F);
896
msarett7f7ec202016-03-01 12:12:27 -0800897 // SkCodec should support this.
Mike Reedede7bac2017-07-23 15:30:02 -0400898 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
scroggob9a1e342015-11-30 06:25:31 -0800899 REPORTER_ASSERT(r, codec);
900 if (!codec) {
901 return;
902 }
scroggo7b5e5532016-02-04 06:14:24 -0800903 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800904}
scroggodb30be22015-12-08 18:54:13 -0800905
906// wbmp images have a header that can be arbitrarily large, depending on the
907// size of the image. We cap the size at 65535, meaning we only need to look at
908// 8 bytes to determine whether we can read the image. This is important
Leon Scroggins III04be2b52017-08-17 15:13:20 -0400909// because SkCodec only passes a limited number of bytes to SkWbmpCodec to
910// determine whether the image is a wbmp.
scroggodb30be22015-12-08 18:54:13 -0800911DEF_TEST(Codec_wbmp_max_size, r) {
912 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
913 0x83, 0xFF, 0x7F, // W: 65535
914 0x83, 0xFF, 0x7F }; // H: 65535
Ben Wagner145dbcd2016-11-03 14:40:50 -0400915 std::unique_ptr<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
Mike Reedede7bac2017-07-23 15:30:02 -0400916 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
scroggodb30be22015-12-08 18:54:13 -0800917
918 REPORTER_ASSERT(r, codec);
919 if (!codec) return;
920
921 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
922 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
923
924 // Now test an image which is too big. Any image with a larger header (i.e.
925 // has bigger width/height) is also too big.
926 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
927 0x84, 0x80, 0x00, // W: 65536
928 0x84, 0x80, 0x00 }; // H: 65536
929 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
Mike Reedede7bac2017-07-23 15:30:02 -0400930 codec = SkCodec::MakeFromStream(std::move(stream));
scroggodb30be22015-12-08 18:54:13 -0800931
932 REPORTER_ASSERT(r, !codec);
933}
msarett2812f032016-07-18 15:56:08 -0700934
935DEF_TEST(Codec_jpeg_rewind, r) {
Hal Canaryc465d132017-12-08 10:21:31 -0500936 const char* path = "images/mandrill_512_q075.jpg";
Leon Scroggins III42886572017-01-27 13:16:28 -0500937 sk_sp<SkData> data(GetResourceAsData(path));
938 if (!data) {
msarett2812f032016-07-18 15:56:08 -0700939 return;
940 }
Leon Scroggins III42886572017-01-27 13:16:28 -0500941
942 data = SkData::MakeSubset(data.get(), 0, data->size() / 2);
Mike Reedede7bac2017-07-23 15:30:02 -0400943 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(data));
msarett2812f032016-07-18 15:56:08 -0700944 if (!codec) {
945 ERRORF(r, "Unable to create codec '%s'.", path);
946 return;
947 }
948
949 const int width = codec->getInfo().width();
950 const int height = codec->getInfo().height();
951 size_t rowBytes = sizeof(SkPMColor) * width;
952 SkAutoMalloc pixelStorage(height * rowBytes);
953
954 // Perform a sampled decode.
955 SkAndroidCodec::AndroidOptions opts;
956 opts.fSampleSize = 12;
Leon Scroggins III42886572017-01-27 13:16:28 -0500957 auto sampledInfo = codec->getInfo().makeWH(width / 12, height / 12);
958 auto result = codec->getAndroidPixels(sampledInfo, pixelStorage.get(), rowBytes, &opts);
959 REPORTER_ASSERT(r, SkCodec::kIncompleteInput == result);
msarett2812f032016-07-18 15:56:08 -0700960
961 // Rewind the codec and perform a full image decode.
Matt Sarett74b16ed2017-01-25 11:58:11 -0500962 result = codec->getPixels(codec->getInfo(), pixelStorage.get(), rowBytes);
Leon Scroggins III42886572017-01-27 13:16:28 -0500963 REPORTER_ASSERT(r, SkCodec::kIncompleteInput == result);
Matt Sarett74b16ed2017-01-25 11:58:11 -0500964
965 // Now perform a subset decode.
966 {
967 opts.fSampleSize = 1;
968 SkIRect subset = SkIRect::MakeWH(100, 100);
969 opts.fSubset = &subset;
970 result = codec->getAndroidPixels(codec->getInfo().makeWH(100, 100), pixelStorage.get(),
971 rowBytes, &opts);
Leon Scroggins III42886572017-01-27 13:16:28 -0500972 // Though we only have half the data, it is enough to decode this subset.
Matt Sarett74b16ed2017-01-25 11:58:11 -0500973 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
974 }
975
976 // Perform another full image decode. ASAN will detect if we look at the subset when it is
977 // out of scope. This would happen if we depend on the old state in the codec.
Leon Scroggins III42886572017-01-27 13:16:28 -0500978 // This tests two layers of bugs: both SkJpegCodec::readRows and SkCodec::fillIncompleteImage
979 // used to look at the old subset.
Matt Sarett74b16ed2017-01-25 11:58:11 -0500980 opts.fSubset = nullptr;
981 result = codec->getAndroidPixels(codec->getInfo(), pixelStorage.get(), rowBytes, &opts);
Leon Scroggins III42886572017-01-27 13:16:28 -0500982 REPORTER_ASSERT(r, SkCodec::kIncompleteInput == result);
msarett2812f032016-07-18 15:56:08 -0700983}
msarett549ca322016-08-17 08:54:08 -0700984
msarett35bb74b2016-08-22 07:41:28 -0700985static void check_color_xform(skiatest::Reporter* r, const char* path) {
Mike Reedede7bac2017-07-23 15:30:02 -0400986 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromStream(GetResourceAsStream(path)));
msarett35bb74b2016-08-22 07:41:28 -0700987
988 SkAndroidCodec::AndroidOptions opts;
989 opts.fSampleSize = 3;
990 const int subsetWidth = codec->getInfo().width() / 2;
991 const int subsetHeight = codec->getInfo().height() / 2;
992 SkIRect subset = SkIRect::MakeWH(subsetWidth, subsetHeight);
993 opts.fSubset = &subset;
994
995 const int dstWidth = subsetWidth / opts.fSampleSize;
996 const int dstHeight = subsetHeight / opts.fSampleSize;
Mike Reed0933bc92017-12-09 01:27:41 +0000997 sk_sp<SkData> data = GetResourceAsData("icc_profiles/HP_ZR30w.icc");
Brian Osman526972e2016-10-24 09:24:02 -0400998 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeICC(data->data(), data->size());
msarett35bb74b2016-08-22 07:41:28 -0700999 SkImageInfo dstInfo = codec->getInfo().makeWH(dstWidth, dstHeight)
1000 .makeColorType(kN32_SkColorType)
1001 .makeColorSpace(colorSpace);
1002
1003 size_t rowBytes = dstInfo.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -04001004 SkAutoMalloc pixelStorage(dstInfo.computeByteSize(rowBytes));
msarett35bb74b2016-08-22 07:41:28 -07001005 SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get(), rowBytes, &opts);
1006 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1007}
1008
1009DEF_TEST(Codec_ColorXform, r) {
Hal Canaryc465d132017-12-08 10:21:31 -05001010 check_color_xform(r, "images/mandrill_512_q075.jpg");
1011 check_color_xform(r, "images/mandrill_512.png");
msarett35bb74b2016-08-22 07:41:28 -07001012}
1013
msarettf17b71f2016-09-12 14:30:03 -07001014static bool color_type_match(SkColorType origColorType, SkColorType codecColorType) {
1015 switch (origColorType) {
1016 case kRGBA_8888_SkColorType:
1017 case kBGRA_8888_SkColorType:
1018 return kRGBA_8888_SkColorType == codecColorType ||
1019 kBGRA_8888_SkColorType == codecColorType;
1020 default:
1021 return origColorType == codecColorType;
1022 }
1023}
1024
1025static bool alpha_type_match(SkAlphaType origAlphaType, SkAlphaType codecAlphaType) {
1026 switch (origAlphaType) {
1027 case kUnpremul_SkAlphaType:
1028 case kPremul_SkAlphaType:
1029 return kUnpremul_SkAlphaType == codecAlphaType ||
1030 kPremul_SkAlphaType == codecAlphaType;
1031 default:
1032 return origAlphaType == codecAlphaType;
1033 }
1034}
1035
1036static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const SkImageInfo& info) {
1037 SkBitmap bm1;
Leon Scroggins571b30f2017-07-11 17:35:31 +00001038 bm1.allocPixels(info);
1039 SkCodec::Result result = origCodec->getPixels(info, bm1.getPixels(), bm1.rowBytes());
msarettf17b71f2016-09-12 14:30:03 -07001040 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarett9b09cd82016-08-29 14:47:49 -07001041
1042 // Encode the image to png.
1043 sk_sp<SkData> data =
Hal Canarydb683012016-11-23 08:55:18 -07001044 sk_sp<SkData>(sk_tool_utils::EncodeImageToData(bm1, SkEncodedImageFormat::kPNG, 100));
msarett9b09cd82016-08-29 14:47:49 -07001045
Mike Reedede7bac2017-07-23 15:30:02 -04001046 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
msarettf17b71f2016-09-12 14:30:03 -07001047 REPORTER_ASSERT(r, color_type_match(info.colorType(), codec->getInfo().colorType()));
1048 REPORTER_ASSERT(r, alpha_type_match(info.alphaType(), codec->getInfo().alphaType()));
msarett9b09cd82016-08-29 14:47:49 -07001049
1050 SkBitmap bm2;
Leon Scroggins571b30f2017-07-11 17:35:31 +00001051 bm2.allocPixels(info);
1052 result = codec->getPixels(info, bm2.getPixels(), bm2.rowBytes());
msarett9b09cd82016-08-29 14:47:49 -07001053 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1054
1055 SkMD5::Digest d1, d2;
1056 md5(bm1, &d1);
1057 md5(bm2, &d2);
1058 REPORTER_ASSERT(r, d1 == d2);
1059}
1060
1061DEF_TEST(Codec_PngRoundTrip, r) {
Hal Canaryc465d132017-12-08 10:21:31 -05001062 auto codec = SkCodec::MakeFromStream(GetResourceAsStream("images/mandrill_512_q075.jpg"));
msarett549ca322016-08-17 08:54:08 -07001063
msarettf17b71f2016-09-12 14:30:03 -07001064 SkColorType colorTypesOpaque[] = {
1065 kRGB_565_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1066 };
1067 for (SkColorType colorType : colorTypesOpaque) {
1068 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType);
1069 check_round_trip(r, codec.get(), newInfo);
1070 }
1071
Hal Canaryc465d132017-12-08 10:21:31 -05001072 codec = SkCodec::MakeFromStream(GetResourceAsStream("images/grayscale.jpg"));
msarettf17b71f2016-09-12 14:30:03 -07001073 check_round_trip(r, codec.get(), codec->getInfo());
1074
Hal Canaryc465d132017-12-08 10:21:31 -05001075 codec = SkCodec::MakeFromStream(GetResourceAsStream("images/yellow_rose.png"));
msarettf17b71f2016-09-12 14:30:03 -07001076
1077 SkColorType colorTypesWithAlpha[] = {
1078 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1079 };
1080 SkAlphaType alphaTypes[] = {
1081 kUnpremul_SkAlphaType, kPremul_SkAlphaType
1082 };
1083 for (SkColorType colorType : colorTypesWithAlpha) {
1084 for (SkAlphaType alphaType : alphaTypes) {
1085 // Set color space to nullptr because color correct premultiplies do not round trip.
1086 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType)
1087 .makeAlphaType(alphaType)
1088 .makeColorSpace(nullptr);
1089 check_round_trip(r, codec.get(), newInfo);
1090 }
1091 }
1092
Hal Canaryc465d132017-12-08 10:21:31 -05001093 codec = SkCodec::MakeFromStream(GetResourceAsStream("images/index8.png"));
msarettf17b71f2016-09-12 14:30:03 -07001094
1095 for (SkAlphaType alphaType : alphaTypes) {
1096 SkImageInfo newInfo = codec->getInfo().makeAlphaType(alphaType)
1097 .makeColorSpace(nullptr);
1098 check_round_trip(r, codec.get(), newInfo);
1099 }
msarett549ca322016-08-17 08:54:08 -07001100}
msarett2ecc35f2016-09-08 11:55:16 -07001101
1102static void test_conversion_possible(skiatest::Reporter* r, const char* path,
scroggo8e6c7ad2016-09-16 08:20:38 -07001103 bool supportsScanlineDecoder,
1104 bool supportsIncrementalDecoder) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001105 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
Leon Scroggins IIIc9942a12017-01-30 09:59:28 -05001106 if (!stream) {
1107 return;
1108 }
1109
Mike Reedede7bac2017-07-23 15:30:02 -04001110 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
Leon Scroggins IIIc9942a12017-01-30 09:59:28 -05001111 if (!codec) {
1112 ERRORF(r, "failed to create a codec for %s", path);
1113 return;
1114 }
1115
msarett2ecc35f2016-09-08 11:55:16 -07001116 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType);
1117
1118 SkBitmap bm;
1119 bm.allocPixels(infoF16);
1120 SkCodec::Result result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1121 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001122
1123 result = codec->startScanlineDecode(infoF16);
1124 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001125 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001126 } else {
Leon Scroggins III07418182017-08-15 12:24:02 -04001127 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result
1128 || SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001129 }
1130
1131 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1132 if (supportsIncrementalDecoder) {
1133 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
1134 } else {
Leon Scroggins III07418182017-08-15 12:24:02 -04001135 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result
1136 || SkCodec::kInvalidConversion == result);
msarett2ecc35f2016-09-08 11:55:16 -07001137 }
1138
Brian Osman36703d92017-12-12 14:09:31 -05001139 infoF16 = infoF16.makeColorSpace(infoF16.colorSpace()->makeLinearGamma());
msarett2ecc35f2016-09-08 11:55:16 -07001140 result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1141 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001142 result = codec->startScanlineDecode(infoF16);
1143 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001144 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001145 } else {
1146 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1147 }
1148
1149 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1150 if (supportsIncrementalDecoder) {
1151 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1152 } else {
1153 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001154 }
1155}
1156
1157DEF_TEST(Codec_F16ConversionPossible, r) {
Hal Canaryc465d132017-12-08 10:21:31 -05001158 test_conversion_possible(r, "images/color_wheel.webp", false, false);
1159 test_conversion_possible(r, "images/mandrill_512_q075.jpg", true, false);
1160 test_conversion_possible(r, "images/yellow_rose.png", false, true);
scroggo8e6c7ad2016-09-16 08:20:38 -07001161}
1162
scroggo19b91532016-10-24 09:03:26 -07001163static void decode_frame(skiatest::Reporter* r, SkCodec* codec, size_t frame) {
1164 SkBitmap bm;
1165 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1166 bm.allocPixels(info);
1167
1168 SkCodec::Options opts;
1169 opts.fFrameIndex = frame;
1170 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->getPixels(info,
Leon Scroggins571b30f2017-07-11 17:35:31 +00001171 bm.getPixels(), bm.rowBytes(), &opts));
scroggo19b91532016-10-24 09:03:26 -07001172}
1173
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -04001174// For an animated GIF, we should only read enough to decode frame 0 if the
1175// client never calls getFrameInfo and only decodes frame 0.
scroggo19b91532016-10-24 09:03:26 -07001176DEF_TEST(Codec_skipFullParse, r) {
Hal Canaryc465d132017-12-08 10:21:31 -05001177 auto path = "images/test640x479.gif";
Mike Reed71f867c2017-07-23 13:14:10 -04001178 auto streamObj = GetResourceAsStream(path);
1179 if (!streamObj) {
scroggo19b91532016-10-24 09:03:26 -07001180 return;
1181 }
Mike Reed71f867c2017-07-23 13:14:10 -04001182 SkStream* stream = streamObj.get();
scroggo19b91532016-10-24 09:03:26 -07001183
1184 // Note that we cheat and hold on to the stream pointer, but SkCodec will
1185 // take ownership. We will not refer to the stream after the SkCodec
1186 // deletes it.
Mike Reedede7bac2017-07-23 15:30:02 -04001187 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(streamObj)));
scroggo19b91532016-10-24 09:03:26 -07001188 if (!codec) {
1189 ERRORF(r, "Failed to create codec for %s", path);
1190 return;
1191 }
1192
1193 REPORTER_ASSERT(r, stream->hasPosition());
1194 const size_t sizePosition = stream->getPosition();
1195 REPORTER_ASSERT(r, stream->hasLength() && sizePosition < stream->getLength());
1196
1197 // This should read more of the stream, but not the whole stream.
1198 decode_frame(r, codec.get(), 0);
1199 const size_t positionAfterFirstFrame = stream->getPosition();
1200 REPORTER_ASSERT(r, positionAfterFirstFrame > sizePosition
1201 && positionAfterFirstFrame < stream->getLength());
1202
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -04001203 // There is more data in the stream.
scroggo19b91532016-10-24 09:03:26 -07001204 auto frameInfo = codec->getFrameInfo();
1205 REPORTER_ASSERT(r, frameInfo.size() == 4);
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -04001206 REPORTER_ASSERT(r, stream->getPosition() > positionAfterFirstFrame);
scroggo19b91532016-10-24 09:03:26 -07001207}
1208
scroggo8e6c7ad2016-09-16 08:20:38 -07001209// Only rewinds up to a limit.
1210class LimitedRewindingStream : public SkStream {
1211public:
Mike Reed71f867c2017-07-23 13:14:10 -04001212 static std::unique_ptr<SkStream> Make(const char path[], size_t limit) {
1213 auto stream = GetResourceAsStream(path);
scroggo8e6c7ad2016-09-16 08:20:38 -07001214 if (!stream) {
1215 return nullptr;
1216 }
Mike Reed71f867c2017-07-23 13:14:10 -04001217 return std::unique_ptr<SkStream>(new LimitedRewindingStream(std::move(stream), limit));
scroggo8e6c7ad2016-09-16 08:20:38 -07001218 }
1219
1220 size_t read(void* buffer, size_t size) override {
1221 const size_t bytes = fStream->read(buffer, size);
1222 fPosition += bytes;
1223 return bytes;
1224 }
1225
1226 bool isAtEnd() const override {
1227 return fStream->isAtEnd();
1228 }
1229
1230 bool rewind() override {
1231 if (fPosition <= fLimit && fStream->rewind()) {
1232 fPosition = 0;
1233 return true;
1234 }
1235
1236 return false;
1237 }
1238
1239private:
Ben Wagner145dbcd2016-11-03 14:40:50 -04001240 std::unique_ptr<SkStream> fStream;
1241 const size_t fLimit;
1242 size_t fPosition;
scroggo8e6c7ad2016-09-16 08:20:38 -07001243
Mike Reed71f867c2017-07-23 13:14:10 -04001244 LimitedRewindingStream(std::unique_ptr<SkStream> stream, size_t limit)
1245 : fStream(std::move(stream))
scroggo8e6c7ad2016-09-16 08:20:38 -07001246 , fLimit(limit)
1247 , fPosition(0)
1248 {
1249 SkASSERT(fStream);
1250 }
1251};
1252
1253DEF_TEST(Codec_fallBack, r) {
1254 // SkAndroidCodec needs to be able to fall back to scanline decoding
1255 // if incremental decoding does not work. Make sure this does not
1256 // require a rewind.
1257
1258 // Formats that currently do not support incremental decoding
1259 auto files = {
Hal Canaryc465d132017-12-08 10:21:31 -05001260 "images/CMYK.jpg",
1261 "images/color_wheel.ico",
1262 "images/mandrill.wbmp",
1263 "images/randPixels.bmp",
scroggo8e6c7ad2016-09-16 08:20:38 -07001264 };
1265 for (auto file : files) {
Leon Scroggins III04be2b52017-08-17 15:13:20 -04001266 auto stream = LimitedRewindingStream::Make(file, SkCodec::MinBufferedBytesNeeded());
scroggo8e6c7ad2016-09-16 08:20:38 -07001267 if (!stream) {
1268 SkDebugf("Missing resources (%s). Set --resourcePath.\n", file);
1269 return;
1270 }
1271
Mike Reedede7bac2017-07-23 15:30:02 -04001272 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
scroggo8e6c7ad2016-09-16 08:20:38 -07001273 if (!codec) {
1274 ERRORF(r, "Failed to create codec for %s,", file);
1275 continue;
1276 }
1277
1278 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
1279 SkBitmap bm;
1280 bm.allocPixels(info);
1281
1282 if (SkCodec::kUnimplemented != codec->startIncrementalDecode(info, bm.getPixels(),
1283 bm.rowBytes())) {
1284 ERRORF(r, "Is scanline decoding now implemented for %s?", file);
1285 continue;
1286 }
1287
1288 // Scanline decoding should not require a rewind.
1289 SkCodec::Result result = codec->startScanlineDecode(info);
1290 if (SkCodec::kSuccess != result) {
1291 ERRORF(r, "Scanline decoding failed for %s with %i", file, result);
1292 }
1293 }
msarett2ecc35f2016-09-08 11:55:16 -07001294}
scroggoc46cdd42016-10-10 06:45:32 -07001295
1296// This test verifies that we fixed an assert statement that fired when reusing a png codec
1297// after scaling.
1298DEF_TEST(Codec_reusePng, r) {
Hal Canaryc465d132017-12-08 10:21:31 -05001299 std::unique_ptr<SkStream> stream(GetResourceAsStream("images/plane.png"));
scroggoc46cdd42016-10-10 06:45:32 -07001300 if (!stream) {
1301 return;
1302 }
1303
Mike Reedede7bac2017-07-23 15:30:02 -04001304 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromStream(std::move(stream)));
scroggoc46cdd42016-10-10 06:45:32 -07001305 if (!codec) {
1306 ERRORF(r, "Failed to create codec\n");
1307 return;
1308 }
1309
1310 SkAndroidCodec::AndroidOptions opts;
1311 opts.fSampleSize = 5;
1312 auto size = codec->getSampledDimensions(opts.fSampleSize);
1313 auto info = codec->getInfo().makeWH(size.fWidth, size.fHeight).makeColorType(kN32_SkColorType);
1314 SkBitmap bm;
1315 bm.allocPixels(info);
1316 auto result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1317 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1318
1319 info = codec->getInfo().makeColorType(kN32_SkColorType);
1320 bm.allocPixels(info);
1321 opts.fSampleSize = 1;
1322 result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1323 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1324}
scroggoe61b3b42016-10-10 07:17:32 -07001325
1326DEF_TEST(Codec_rowsDecoded, r) {
Hal Canaryc465d132017-12-08 10:21:31 -05001327 auto file = "images/plane_interlaced.png";
scroggoe61b3b42016-10-10 07:17:32 -07001328 std::unique_ptr<SkStream> stream(GetResourceAsStream(file));
1329 if (!stream) {
1330 return;
1331 }
1332
1333 // This is enough to read the header etc, but no rows.
Mike Reedede7bac2017-07-23 15:30:02 -04001334 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(SkData::MakeFromStream(stream.get(), 99)));
scroggoe61b3b42016-10-10 07:17:32 -07001335 if (!codec) {
1336 ERRORF(r, "Failed to create codec\n");
1337 return;
1338 }
1339
1340 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1341 SkBitmap bm;
1342 bm.allocPixels(info);
1343 auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
1344 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1345
1346 // This is an arbitrary value. The important fact is that it is not zero, and rowsDecoded
1347 // should get set to zero by incrementalDecode.
1348 int rowsDecoded = 77;
1349 result = codec->incrementalDecode(&rowsDecoded);
1350 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput);
1351 REPORTER_ASSERT(r, rowsDecoded == 0);
1352}
Matt Sarett29121eb2016-10-17 14:32:46 -04001353
Matt Sarettd59948a2017-04-26 10:59:48 -04001354static void test_invalid_images(skiatest::Reporter* r, const char* path,
1355 SkCodec::Result expectedResult) {
Mike Reed71f867c2017-07-23 13:14:10 -04001356 auto stream = GetResourceAsStream(path);
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -05001357 if (!stream) {
1358 return;
1359 }
1360
Mike Reedede7bac2017-07-23 15:30:02 -04001361 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -05001362 REPORTER_ASSERT(r, codec);
1363
Matt Sarettd59948a2017-04-26 10:59:48 -04001364 test_info(r, codec.get(), codec->getInfo().makeColorType(kN32_SkColorType), expectedResult,
1365 nullptr);
1366}
1367
1368DEF_TEST(Codec_InvalidImages, r) {
1369 // ASAN will complain if there is an issue.
Leon Scroggins III674a1842017-07-06 12:26:09 -04001370 test_invalid_images(r, "invalid_images/skbug5887.gif", SkCodec::kErrorInInput);
Matt Sarettd59948a2017-04-26 10:59:48 -04001371 test_invalid_images(r, "invalid_images/many-progressive-scans.jpg", SkCodec::kInvalidInput);
1372 test_invalid_images(r, "invalid_images/b33251605.bmp", SkCodec::kIncompleteInput);
1373 test_invalid_images(r, "invalid_images/bad_palette.png", SkCodec::kInvalidInput);
1374}
1375
1376static void test_invalid_header(skiatest::Reporter* r, const char* path) {
Mike Reed0933bc92017-12-09 01:27:41 +00001377 auto data = GetResourceAsData(path);
1378 if (!data) {
1379 return;
1380 }
1381 std::unique_ptr<SkStreamAsset> stream(new SkMemoryStream(std::move(data)));
Mike Reed847068c2017-07-26 11:35:53 -04001382 if (!stream) {
Matt Sarettd59948a2017-04-26 10:59:48 -04001383 return;
1384 }
Mike Reedede7bac2017-07-23 15:30:02 -04001385 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
Matt Sarettd59948a2017-04-26 10:59:48 -04001386 REPORTER_ASSERT(r, !codec);
1387}
1388
1389DEF_TEST(Codec_InvalidHeader, r) {
1390 test_invalid_header(r, "invalid_images/int_overflow.ico");
1391
1392 // These files report values that have caused problems with SkFILEStreams.
1393 // They are invalid, and should not create SkCodecs.
1394 test_invalid_header(r, "invalid_images/b33651913.bmp");
1395 test_invalid_header(r, "invalid_images/b34778578.bmp");
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -05001396}
1397
Leon Scroggins III56e32092016-12-12 17:10:46 -05001398DEF_TEST(Codec_InvalidAnimated, r) {
1399 // ASAN will complain if there is an issue.
1400 auto path = "invalid_images/skbug6046.gif";
Mike Reed71f867c2017-07-23 13:14:10 -04001401 auto stream = GetResourceAsStream(path);
Leon Scroggins III56e32092016-12-12 17:10:46 -05001402 if (!stream) {
1403 return;
1404 }
1405
Mike Reedede7bac2017-07-23 15:30:02 -04001406 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
Leon Scroggins III56e32092016-12-12 17:10:46 -05001407 REPORTER_ASSERT(r, codec);
1408 if (!codec) {
1409 return;
1410 }
1411
1412 const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1413 SkBitmap bm;
1414 bm.allocPixels(info);
1415
1416 auto frameInfos = codec->getFrameInfo();
1417 SkCodec::Options opts;
Leon Scroggins III249b8e32017-04-17 12:46:33 -04001418 for (int i = 0; static_cast<size_t>(i) < frameInfos.size(); i++) {
Leon Scroggins III56e32092016-12-12 17:10:46 -05001419 opts.fFrameIndex = i;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -04001420 const auto reqFrame = frameInfos[i].fRequiredFrame;
1421 opts.fPriorFrame = reqFrame == i - 1 ? reqFrame : SkCodec::kNone;
Leon Scroggins III56e32092016-12-12 17:10:46 -05001422 auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes(), &opts);
1423 if (result != SkCodec::kSuccess) {
1424 ERRORF(r, "Failed to start decoding frame %i (out of %i) with error %i\n", i,
1425 frameInfos.size(), result);
1426 continue;
1427 }
1428
1429 codec->incrementalDecode();
1430 }
1431}
Matt Sarett0e032be2017-03-15 17:50:08 -04001432
Matt Sarett5df93de2017-03-22 21:52:47 +00001433static void encode_format(SkDynamicMemoryWStream* stream, const SkPixmap& pixmap,
Matt Sarettc367d032017-05-05 11:13:26 -04001434 SkTransferFunctionBehavior unpremulBehavior,
1435 SkEncodedImageFormat format) {
1436 SkPngEncoder::Options pngOptions;
Matt Sarett04c37312017-05-05 14:02:13 -04001437 SkWebpEncoder::Options webpOptions;
Matt Sarettc367d032017-05-05 11:13:26 -04001438 pngOptions.fUnpremulBehavior = unpremulBehavior;
Matt Sarett04c37312017-05-05 14:02:13 -04001439 webpOptions.fUnpremulBehavior = unpremulBehavior;
Matt Sarett5df93de2017-03-22 21:52:47 +00001440 switch (format) {
1441 case SkEncodedImageFormat::kPNG:
Matt Sarettc367d032017-05-05 11:13:26 -04001442 SkPngEncoder::Encode(stream, pixmap, pngOptions);
Matt Sarett5df93de2017-03-22 21:52:47 +00001443 break;
1444 case SkEncodedImageFormat::kJPEG:
Matt Sarett26b44df2017-05-02 16:04:56 -04001445 SkJpegEncoder::Encode(stream, pixmap, SkJpegEncoder::Options());
Matt Sarett5df93de2017-03-22 21:52:47 +00001446 break;
Matt Sarett3dbef9f2017-04-05 22:33:22 +00001447 case SkEncodedImageFormat::kWEBP:
Matt Sarett04c37312017-05-05 14:02:13 -04001448 SkWebpEncoder::Encode(stream, pixmap, webpOptions);
Matt Sarett3dbef9f2017-04-05 22:33:22 +00001449 break;
Matt Sarett5df93de2017-03-22 21:52:47 +00001450 default:
1451 SkASSERT(false);
1452 break;
1453 }
1454}
1455
Matt Sarett7abfb5e2017-04-05 17:36:04 -04001456static void test_encode_icc(skiatest::Reporter* r, SkEncodedImageFormat format,
1457 SkTransferFunctionBehavior unpremulBehavior) {
Matt Sarett0e032be2017-03-15 17:50:08 -04001458 // Test with sRGB color space.
1459 SkBitmap srgbBitmap;
1460 SkImageInfo srgbInfo = SkImageInfo::MakeS32(1, 1, kOpaque_SkAlphaType);
1461 srgbBitmap.allocPixels(srgbInfo);
1462 *srgbBitmap.getAddr32(0, 0) = 0;
1463 SkPixmap pixmap;
1464 srgbBitmap.peekPixels(&pixmap);
1465 SkDynamicMemoryWStream srgbBuf;
Matt Sarettc367d032017-05-05 11:13:26 -04001466 encode_format(&srgbBuf, pixmap, unpremulBehavior, format);
Matt Sarett0e032be2017-03-15 17:50:08 -04001467 sk_sp<SkData> srgbData = srgbBuf.detachAsData();
Mike Reedede7bac2017-07-23 15:30:02 -04001468 std::unique_ptr<SkCodec> srgbCodec(SkCodec::MakeFromData(srgbData));
Matt Sarett0e032be2017-03-15 17:50:08 -04001469 REPORTER_ASSERT(r, srgbCodec->getInfo().colorSpace() == SkColorSpace::MakeSRGB().get());
1470
1471 // Test with P3 color space.
1472 SkDynamicMemoryWStream p3Buf;
1473 sk_sp<SkColorSpace> p3 = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
1474 SkColorSpace::kDCIP3_D65_Gamut);
1475 pixmap.setColorSpace(p3);
Matt Sarettc367d032017-05-05 11:13:26 -04001476 encode_format(&p3Buf, pixmap, unpremulBehavior, format);
Matt Sarett0e032be2017-03-15 17:50:08 -04001477 sk_sp<SkData> p3Data = p3Buf.detachAsData();
Mike Reedede7bac2017-07-23 15:30:02 -04001478 std::unique_ptr<SkCodec> p3Codec(SkCodec::MakeFromData(p3Data));
Matt Sarett0e032be2017-03-15 17:50:08 -04001479 REPORTER_ASSERT(r, p3Codec->getInfo().colorSpace()->gammaCloseToSRGB());
1480 SkMatrix44 mat0(SkMatrix44::kUninitialized_Constructor);
1481 SkMatrix44 mat1(SkMatrix44::kUninitialized_Constructor);
1482 bool success = p3->toXYZD50(&mat0);
1483 REPORTER_ASSERT(r, success);
1484 success = p3Codec->getInfo().colorSpace()->toXYZD50(&mat1);
1485 REPORTER_ASSERT(r, success);
1486
1487 for (int i = 0; i < 4; i++) {
1488 for (int j = 0; j < 4; j++) {
Matt Sarett5df93de2017-03-22 21:52:47 +00001489 REPORTER_ASSERT(r, color_space_almost_equal(mat0.get(i, j), mat1.get(i, j)));
Matt Sarett0e032be2017-03-15 17:50:08 -04001490 }
1491 }
1492}
Matt Sarett5df93de2017-03-22 21:52:47 +00001493
1494DEF_TEST(Codec_EncodeICC, r) {
Matt Sarett7abfb5e2017-04-05 17:36:04 -04001495 test_encode_icc(r, SkEncodedImageFormat::kPNG, SkTransferFunctionBehavior::kRespect);
1496 test_encode_icc(r, SkEncodedImageFormat::kJPEG, SkTransferFunctionBehavior::kRespect);
Matt Sarett46a45ba2017-04-06 20:34:38 +00001497 test_encode_icc(r, SkEncodedImageFormat::kWEBP, SkTransferFunctionBehavior::kRespect);
Matt Sarett7abfb5e2017-04-05 17:36:04 -04001498 test_encode_icc(r, SkEncodedImageFormat::kPNG, SkTransferFunctionBehavior::kIgnore);
1499 test_encode_icc(r, SkEncodedImageFormat::kJPEG, SkTransferFunctionBehavior::kIgnore);
Matt Sarett46a45ba2017-04-06 20:34:38 +00001500 test_encode_icc(r, SkEncodedImageFormat::kWEBP, SkTransferFunctionBehavior::kIgnore);
Matt Sarett5df93de2017-03-22 21:52:47 +00001501}
Leon Scroggins IIIe5677462017-09-27 16:31:08 -04001502
1503DEF_TEST(Codec_webp_rowsDecoded, r) {
Hal Canaryc465d132017-12-08 10:21:31 -05001504 const char* path = "images/baby_tux.webp";
Leon Scroggins IIIe5677462017-09-27 16:31:08 -04001505 sk_sp<SkData> data(GetResourceAsData(path));
1506 if (!data) {
1507 return;
1508 }
1509
1510 // Truncate this file so that the header is available but no rows can be
1511 // decoded. This should create a codec but fail to decode.
1512 size_t truncatedSize = 5000;
1513 sk_sp<SkData> subset = SkData::MakeSubset(data.get(), 0, truncatedSize);
1514 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(std::move(subset));
1515 if (!codec) {
1516 ERRORF(r, "Failed to create a codec for %s truncated to only %lu bytes",
1517 path, truncatedSize);
1518 return;
1519 }
1520
1521 test_info(r, codec.get(), codec->getInfo(), SkCodec::kInvalidInput, nullptr);
1522}