blob: bae8344bad631d5e6e4102ad561f2d911b455b03 [file] [log] [blame]
halcanarya096d7a2015-03-27 12:16:53 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Resources.h"
msarett3d9d7a72015-10-21 10:27:10 -07009#include "SkAndroidCodec.h"
halcanarya096d7a2015-03-27 12:16:53 -070010#include "SkBitmap.h"
11#include "SkCodec.h"
msarettedd2dcf2016-01-14 13:12:26 -080012#include "SkCodecImageGenerator.h"
msarette6dd0042015-10-09 11:07:34 -070013#include "SkData.h"
msarett549ca322016-08-17 08:54:08 -070014#include "SkImageEncoder.h"
scroggoef0fed32016-02-18 05:59:25 -080015#include "SkFrontBufferedStream.h"
halcanarya096d7a2015-03-27 12:16:53 -070016#include "SkMD5.h"
scroggob636b452015-07-22 07:16:20 -070017#include "SkRandom.h"
scroggocf98fa92015-11-23 08:14:40 -080018#include "SkStream.h"
scroggob9a1e342015-11-30 06:25:31 -080019#include "SkStreamPriv.h"
scroggocf98fa92015-11-23 08:14:40 -080020#include "SkPngChunkReader.h"
halcanarya096d7a2015-03-27 12:16:53 -070021#include "Test.h"
22
scroggocf98fa92015-11-23 08:14:40 -080023#include "png.h"
24
scroggo8e6c7ad2016-09-16 08:20:38 -070025#if PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 5
26 // FIXME (scroggo): Google3 needs to be updated to use a newer version of libpng. In
27 // the meantime, we had to break some pieces of SkPngCodec in order to support Google3.
28 // The parts that are broken are likely not used by Google3.
29 #define SK_PNG_DISABLE_TESTS
30#endif
31
halcanarya096d7a2015-03-27 12:16:53 -070032static void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
33 SkAutoLockPixels autoLockPixels(bm);
34 SkASSERT(bm.getPixels());
35 SkMD5 md5;
36 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
37 for (int y = 0; y < bm.height(); ++y) {
halcanary1e903042016-04-25 10:29:36 -070038 md5.write(bm.getAddr(0, y), rowLen);
halcanarya096d7a2015-03-27 12:16:53 -070039 }
40 md5.finish(*digest);
41}
42
scroggo9b2cdbf42015-07-10 12:07:02 -070043/**
44 * Compute the digest for bm and compare it to a known good digest.
45 * @param r Reporter to assert that bm's digest matches goodDigest.
46 * @param goodDigest The known good digest to compare to.
47 * @param bm The bitmap to test.
48 */
49static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
50 const SkBitmap& bm) {
51 SkMD5::Digest digest;
52 md5(bm, &digest);
53 REPORTER_ASSERT(r, digest == goodDigest);
54}
55
scroggod1bc5742015-08-12 08:31:44 -070056/**
57 * Test decoding an SkCodec to a particular SkImageInfo.
58 *
halcanary96fcdcc2015-08-27 07:41:13 -070059 * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr,
scroggod1bc5742015-08-12 08:31:44 -070060 * the resulting decode should match.
61 */
scroggo7b5e5532016-02-04 06:14:24 -080062template<typename Codec>
63static void test_info(skiatest::Reporter* r, Codec* codec, const SkImageInfo& info,
scroggod1bc5742015-08-12 08:31:44 -070064 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
65 SkBitmap bm;
66 bm.allocPixels(info);
67 SkAutoLockPixels autoLockPixels(bm);
68
69 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
70 REPORTER_ASSERT(r, result == expectedResult);
71
72 if (goodDigest) {
73 compare_to_good_digest(r, *goodDigest, bm);
74 }
75}
76
scroggob636b452015-07-22 07:16:20 -070077SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
78 SkIRect rect;
79 do {
80 rect.fLeft = rand->nextRangeU(0, w);
81 rect.fTop = rand->nextRangeU(0, h);
82 rect.fRight = rand->nextRangeU(0, w);
83 rect.fBottom = rand->nextRangeU(0, h);
84 rect.sort();
85 } while (rect.isEmpty());
86 return rect;
87}
88
scroggo8e6c7ad2016-09-16 08:20:38 -070089static void test_incremental_decode(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
90 const SkMD5::Digest& goodDigest) {
91 SkBitmap bm;
92 bm.allocPixels(info);
93 SkAutoLockPixels autoLockPixels(bm);
94
95 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->startIncrementalDecode(info, bm.getPixels(),
96 bm.rowBytes()));
97
98 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->incrementalDecode());
99
100 compare_to_good_digest(r, goodDigest, bm);
101}
102
103// Test in stripes, similar to DM's kStripe_Mode
104static void test_in_stripes(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
105 const SkMD5::Digest& goodDigest) {
106 SkBitmap bm;
107 bm.allocPixels(info);
108 bm.eraseColor(SK_ColorYELLOW);
109
110 const int height = info.height();
111 // Note that if numStripes does not evenly divide height there will be an extra
112 // stripe.
113 const int numStripes = 4;
114
115 if (numStripes > height) {
116 // Image is too small.
117 return;
118 }
119
120 const int stripeHeight = height / numStripes;
121
122 // Iterate through the image twice. Once to decode odd stripes, and once for even.
123 for (int oddEven = 1; oddEven >= 0; oddEven--) {
124 for (int y = oddEven * stripeHeight; y < height; y += 2 * stripeHeight) {
125 SkIRect subset = SkIRect::MakeLTRB(0, y, info.width(),
126 SkTMin(y + stripeHeight, height));
127 SkCodec::Options options;
128 options.fSubset = &subset;
129 if (SkCodec::kSuccess != codec->startIncrementalDecode(info, bm.getAddr(0, y),
130 bm.rowBytes(), &options)) {
131 ERRORF(r, "failed to start incremental decode!\ttop: %i\tbottom%i\n",
132 subset.top(), subset.bottom());
133 return;
134 }
135 if (SkCodec::kSuccess != codec->incrementalDecode()) {
136 ERRORF(r, "failed incremental decode starting from line %i\n", y);
137 return;
138 }
139 }
140 }
141
142 compare_to_good_digest(r, goodDigest, bm);
143}
144
scroggo7b5e5532016-02-04 06:14:24 -0800145template<typename Codec>
146static void test_codec(skiatest::Reporter* r, Codec* codec, SkBitmap& bm, const SkImageInfo& info,
scroggo27c17282015-10-27 08:14:46 -0700147 const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest,
148 const SkMD5::Digest* goodDigest) {
msarette6dd0042015-10-09 11:07:34 -0700149
halcanarya096d7a2015-03-27 12:16:53 -0700150 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -0700151 bm.allocPixels(info);
152 SkAutoLockPixels autoLockPixels(bm);
msarettcc7f3052015-10-05 14:20:27 -0700153
154 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700155 REPORTER_ASSERT(r, result == expectedResult);
halcanarya096d7a2015-03-27 12:16:53 -0700156
msarettcc7f3052015-10-05 14:20:27 -0700157 md5(bm, digest);
158 if (goodDigest) {
159 REPORTER_ASSERT(r, *digest == *goodDigest);
160 }
halcanarya096d7a2015-03-27 12:16:53 -0700161
msarett8ff6ca62015-09-18 12:06:04 -0700162 {
163 // Test decoding to 565
164 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggoba584892016-05-20 13:56:13 -0700165 if (info.alphaType() == kOpaque_SkAlphaType) {
166 // Decoding to 565 should succeed.
167 SkBitmap bm565;
168 bm565.allocPixels(info565);
169 SkAutoLockPixels alp(bm565);
170
171 // This will allow comparison even if the image is incomplete.
172 bm565.eraseColor(SK_ColorBLACK);
173
174 REPORTER_ASSERT(r, expectedResult == codec->getPixels(info565,
175 bm565.getPixels(), bm565.rowBytes()));
176
177 SkMD5::Digest digest565;
178 md5(bm565, &digest565);
179
180 // A dumb client's request for non-opaque should also succeed.
181 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
182 info565 = info565.makeAlphaType(alpha);
183 test_info(r, codec, info565, expectedResult, &digest565);
184 }
185 } else {
186 test_info(r, codec, info565, SkCodec::kInvalidConversion, nullptr);
187 }
188 }
189
190 if (codec->getInfo().colorType() == kGray_8_SkColorType) {
191 SkImageInfo grayInfo = codec->getInfo();
192 SkBitmap grayBm;
193 grayBm.allocPixels(grayInfo);
194 SkAutoLockPixels alp(grayBm);
195
196 grayBm.eraseColor(SK_ColorBLACK);
197
198 REPORTER_ASSERT(r, expectedResult == codec->getPixels(grayInfo,
199 grayBm.getPixels(), grayBm.rowBytes()));
200
201 SkMD5::Digest grayDigest;
202 md5(grayBm, &grayDigest);
203
204 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
205 grayInfo = grayInfo.makeAlphaType(alpha);
206 test_info(r, codec, grayInfo, expectedResult, &grayDigest);
207 }
msarett8ff6ca62015-09-18 12:06:04 -0700208 }
209
210 // Verify that re-decoding gives the same result. It is interesting to check this after
211 // a decode to 565, since choosing to decode to 565 may result in some of the decode
212 // options being modified. These options should return to their defaults on another
213 // decode to kN32, so the new digest should match the old digest.
msarette6dd0042015-10-09 11:07:34 -0700214 test_info(r, codec, info, expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700215
216 {
217 // Check alpha type conversions
218 if (info.alphaType() == kOpaque_SkAlphaType) {
219 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800220 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700221 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800222 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700223 } else {
224 // Decoding to opaque should fail
225 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700226 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700227 SkAlphaType otherAt = info.alphaType();
228 if (kPremul_SkAlphaType == otherAt) {
229 otherAt = kUnpremul_SkAlphaType;
230 } else {
231 otherAt = kPremul_SkAlphaType;
232 }
233 // The other non-opaque alpha type should always succeed, but not match.
msarette6dd0042015-10-09 11:07:34 -0700234 test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700235 }
236 }
msarettcc7f3052015-10-05 14:20:27 -0700237}
238
scroggobed1ed62016-02-11 10:24:55 -0800239static bool supports_partial_scanlines(const char path[]) {
scroggo2c3b2182015-10-09 08:40:59 -0700240 static const char* const exts[] = {
241 "jpg", "jpeg", "png", "webp"
242 "JPG", "JPEG", "PNG", "WEBP"
243 };
244
245 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
246 if (SkStrEndsWith(path, exts[i])) {
247 return true;
248 }
249 }
250 return false;
251}
252
scroggo8e6c7ad2016-09-16 08:20:38 -0700253// FIXME: Break up this giant function
msarettcc7f3052015-10-05 14:20:27 -0700254static void check(skiatest::Reporter* r,
255 const char path[],
256 SkISize size,
257 bool supportsScanlineDecoding,
258 bool supportsSubsetDecoding,
scroggo8e6c7ad2016-09-16 08:20:38 -0700259 bool supportsIncomplete,
260 bool supportsNewScanlineDecoding = false) {
msarettcc7f3052015-10-05 14:20:27 -0700261
bungemanf93d7112016-09-16 06:24:20 -0700262 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarettcc7f3052015-10-05 14:20:27 -0700263 if (!stream) {
msarettcc7f3052015-10-05 14:20:27 -0700264 return;
265 }
msarette6dd0042015-10-09 11:07:34 -0700266
267 SkAutoTDelete<SkCodec> codec(nullptr);
268 bool isIncomplete = supportsIncomplete;
269 if (isIncomplete) {
270 size_t size = stream->getLength();
bungeman38d909e2016-08-02 14:40:46 -0700271 sk_sp<SkData> data((SkData::MakeFromStream(stream, 2 * size / 3)));
reed42943c82016-09-12 12:01:44 -0700272 codec.reset(SkCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700273 } else {
mtklein18300a32016-03-16 13:53:35 -0700274 codec.reset(SkCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700275 }
msarettcc7f3052015-10-05 14:20:27 -0700276 if (!codec) {
277 ERRORF(r, "Unable to decode '%s'", path);
278 return;
279 }
280
281 // Test full image decodes with SkCodec
282 SkMD5::Digest codecDigest;
scroggoef0fed32016-02-18 05:59:25 -0800283 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
msarettcc7f3052015-10-05 14:20:27 -0700284 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700285 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
scroggo7b5e5532016-02-04 06:14:24 -0800286 test_codec(r, codec.get(), bm, info, size, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700287
288 // Scanline decoding follows.
scroggod8d68552016-06-06 11:26:17 -0700289
scroggo8e6c7ad2016-09-16 08:20:38 -0700290 if (supportsNewScanlineDecoding && !isIncomplete) {
291 test_incremental_decode(r, codec, info, codecDigest);
292 test_in_stripes(r, codec, info, codecDigest);
293 }
294
295 // Need to call startScanlineDecode() first.
296 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) == 0);
297 REPORTER_ASSERT(r, !codec->skipScanlines(1));
scroggo46c57472015-09-30 08:57:13 -0700298 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700299 if (supportsScanlineDecoding) {
300 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700301
scroggo46c57472015-09-30 08:57:13 -0700302 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700303
scroggo58421542015-04-01 11:25:20 -0700304 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700305 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
306 if (!isIncomplete) {
307 REPORTER_ASSERT(r, 1 == lines);
308 }
scroggo58421542015-04-01 11:25:20 -0700309 }
310 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700311 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700312 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700313 }
scroggo46c57472015-09-30 08:57:13 -0700314
315 // Cannot continue to decode scanlines beyond the end
316 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700317 == 0);
scroggo46c57472015-09-30 08:57:13 -0700318
319 // Interrupting a scanline decode with a full decode starts from
320 // scratch
321 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700322 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
323 if (!isIncomplete) {
324 REPORTER_ASSERT(r, lines == 1);
325 }
scroggo46c57472015-09-30 08:57:13 -0700326 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700327 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700328 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700329 == 0);
scroggo46c57472015-09-30 08:57:13 -0700330 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700331 == 0);
msarett80803ff2015-10-16 10:54:12 -0700332
333 // Test partial scanline decodes
scroggobed1ed62016-02-11 10:24:55 -0800334 if (supports_partial_scanlines(path) && info.width() >= 3) {
msarett80803ff2015-10-16 10:54:12 -0700335 SkCodec::Options options;
336 int width = info.width();
337 int height = info.height();
338 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
339 options.fSubset = &subset;
340
341 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
342 nullptr, nullptr);
343 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
344
345 for (int y = 0; y < height; y++) {
346 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
347 if (!isIncomplete) {
348 REPORTER_ASSERT(r, 1 == lines);
349 }
350 }
351 }
scroggo58421542015-04-01 11:25:20 -0700352 } else {
scroggo46c57472015-09-30 08:57:13 -0700353 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700354 }
scroggob636b452015-07-22 07:16:20 -0700355
356 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
357 // random subsets.
358 // Do not attempt to decode subsets of an image of only once pixel, since there is no
359 // meaningful subset.
360 if (size.width() * size.height() == 1) {
361 return;
362 }
363
364 SkRandom rand;
365 SkIRect subset;
366 SkCodec::Options opts;
367 opts.fSubset = &subset;
368 for (int i = 0; i < 5; i++) {
369 subset = generate_random_subset(&rand, size.width(), size.height());
370 SkASSERT(!subset.isEmpty());
371 const bool supported = codec->getValidSubset(&subset);
372 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
373
374 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
375 SkBitmap bm;
376 bm.allocPixels(subsetInfo);
377 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700378 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700379
380 if (supportsSubsetDecoding) {
msarette6dd0042015-10-09 11:07:34 -0700381 REPORTER_ASSERT(r, result == expectedResult);
scroggob636b452015-07-22 07:16:20 -0700382 // Webp is the only codec that supports subsets, and it will have modified the subset
383 // to have even left/top.
384 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
385 } else {
386 // No subsets will work.
387 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
388 }
389 }
msarettcc7f3052015-10-05 14:20:27 -0700390
scroggobed1ed62016-02-11 10:24:55 -0800391 // SkAndroidCodec tests
scroggo8e6c7ad2016-09-16 08:20:38 -0700392 if (supportsScanlineDecoding || supportsSubsetDecoding || supportsNewScanlineDecoding) {
scroggo2c3b2182015-10-09 08:40:59 -0700393
bungemanf93d7112016-09-16 06:24:20 -0700394 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarettcc7f3052015-10-05 14:20:27 -0700395 if (!stream) {
msarettcc7f3052015-10-05 14:20:27 -0700396 return;
397 }
msarette6dd0042015-10-09 11:07:34 -0700398
scroggo7b5e5532016-02-04 06:14:24 -0800399 SkAutoTDelete<SkAndroidCodec> androidCodec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700400 if (isIncomplete) {
401 size_t size = stream->getLength();
bungeman38d909e2016-08-02 14:40:46 -0700402 sk_sp<SkData> data((SkData::MakeFromStream(stream, 2 * size / 3)));
reed42943c82016-09-12 12:01:44 -0700403 androidCodec.reset(SkAndroidCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700404 } else {
mtklein18300a32016-03-16 13:53:35 -0700405 androidCodec.reset(SkAndroidCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700406 }
scroggo7b5e5532016-02-04 06:14:24 -0800407 if (!androidCodec) {
msarettcc7f3052015-10-05 14:20:27 -0700408 ERRORF(r, "Unable to decode '%s'", path);
409 return;
410 }
411
412 SkBitmap bm;
scroggobed1ed62016-02-11 10:24:55 -0800413 SkMD5::Digest androidCodecDigest;
414 test_codec(r, androidCodec.get(), bm, info, size, expectedResult, &androidCodecDigest,
scroggo7b5e5532016-02-04 06:14:24 -0800415 &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700416 }
417
msarettedd2dcf2016-01-14 13:12:26 -0800418 if (!isIncomplete) {
scroggoef0fed32016-02-18 05:59:25 -0800419 // Test SkCodecImageGenerator
bungemanf93d7112016-09-16 06:24:20 -0700420 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
bungeman38d909e2016-08-02 14:40:46 -0700421 sk_sp<SkData> fullData(SkData::MakeFromStream(stream, stream->getLength()));
422 SkAutoTDelete<SkImageGenerator> gen(
423 SkCodecImageGenerator::NewFromEncodedCodec(fullData.get()));
msarettedd2dcf2016-01-14 13:12:26 -0800424 SkBitmap bm;
425 bm.allocPixels(info);
426 SkAutoLockPixels autoLockPixels(bm);
427 REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes()));
428 compare_to_good_digest(r, codecDigest, bm);
scroggoef0fed32016-02-18 05:59:25 -0800429
scroggo8e6c7ad2016-09-16 08:20:38 -0700430#ifndef SK_PNG_DISABLE_TESTS
scroggod8d68552016-06-06 11:26:17 -0700431 // Test using SkFrontBufferedStream, as Android does
bungeman38d909e2016-08-02 14:40:46 -0700432 SkStream* bufferedStream = SkFrontBufferedStream::Create(
433 new SkMemoryStream(std::move(fullData)), SkCodec::MinBufferedBytesNeeded());
scroggod8d68552016-06-06 11:26:17 -0700434 REPORTER_ASSERT(r, bufferedStream);
435 codec.reset(SkCodec::NewFromStream(bufferedStream));
436 REPORTER_ASSERT(r, codec);
437 if (codec) {
438 test_info(r, codec.get(), info, SkCodec::kSuccess, &codecDigest);
scroggoef0fed32016-02-18 05:59:25 -0800439 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700440#endif
msarettedd2dcf2016-01-14 13:12:26 -0800441 }
442
msarette6dd0042015-10-09 11:07:34 -0700443 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
444 if (isIncomplete) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700445 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false,
446 supportsNewScanlineDecoding);
msarettcc7f3052015-10-05 14:20:27 -0700447 }
halcanarya096d7a2015-03-27 12:16:53 -0700448}
449
450DEF_TEST(Codec, r) {
451 // WBMP
scroggo8e6c7ad2016-09-16 08:20:38 -0700452 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false, true);
halcanarya096d7a2015-03-27 12:16:53 -0700453
scroggo6f5e6192015-06-18 12:53:43 -0700454 // WEBP
scroggo8e6c7ad2016-09-16 08:20:38 -0700455 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true, true);
456 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true, true);
457 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true, true);
scroggo6f5e6192015-06-18 12:53:43 -0700458
halcanarya096d7a2015-03-27 12:16:53 -0700459 // BMP
scroggo8e6c7ad2016-09-16 08:20:38 -0700460 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false, true);
461 check(r, "rle.bmp", SkISize::Make(320, 240), true, false, true);
halcanarya096d7a2015-03-27 12:16:53 -0700462
463 // ICO
msarette6dd0042015-10-09 11:07:34 -0700464 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700465 // These two tests examine interestingly different behavior:
466 // Decodes an embedded BMP image
msarettbe8216a2015-12-04 08:00:50 -0800467 check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700468 // Decodes an embedded PNG image
scroggo8e6c7ad2016-09-16 08:20:38 -0700469 check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false, false, true);
halcanarya096d7a2015-03-27 12:16:53 -0700470
msarett438b2ad2015-04-09 12:43:10 -0700471 // GIF
msarette6dd0042015-10-09 11:07:34 -0700472 // FIXME: We are not ready to test incomplete GIFs
scroggo27c17282015-10-27 08:14:46 -0700473 check(r, "box.gif", SkISize::Make(200, 55), true, false, false);
474 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700475 // randPixels.gif is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700476 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, false);
msarett438b2ad2015-04-09 12:43:10 -0700477
msarette16b04a2015-04-15 07:32:19 -0700478 // JPG
scroggo8e6c7ad2016-09-16 08:20:38 -0700479 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false, true);
480 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700481 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700482 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggo8e6c7ad2016-09-16 08:20:38 -0700483 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700484 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700485 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
msarette16b04a2015-04-15 07:32:19 -0700486
halcanarya096d7a2015-03-27 12:16:53 -0700487 // PNG
scroggo8e6c7ad2016-09-16 08:20:38 -0700488 check(r, "arrow.png", SkISize::Make(187, 312), false, false, true, true);
489 check(r, "baby_tux.png", SkISize::Make(240, 246), false, false, true, true);
490 check(r, "color_wheel.png", SkISize::Make(128, 128), false, false, true, true);
491 // half-transparent-white-pixel.png is too small to test incomplete
492 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), false, false, false, true);
493 check(r, "mandrill_128.png", SkISize::Make(128, 128), false, false, true, true);
494 check(r, "mandrill_16.png", SkISize::Make(16, 16), false, false, true, true);
495 check(r, "mandrill_256.png", SkISize::Make(256, 256), false, false, true, true);
496 check(r, "mandrill_32.png", SkISize::Make(32, 32), false, false, true, true);
497 check(r, "mandrill_512.png", SkISize::Make(512, 512), false, false, true, true);
498 check(r, "mandrill_64.png", SkISize::Make(64, 64), false, false, true, true);
499 check(r, "plane.png", SkISize::Make(250, 126), false, false, true, true);
500 check(r, "plane_interlaced.png", SkISize::Make(250, 126), false, false, true, true);
501 check(r, "randPixels.png", SkISize::Make(8, 8), false, false, true, true);
502 check(r, "yellow_rose.png", SkISize::Make(400, 301), false, false, true, true);
yujieqin916de9f2016-01-25 08:26:16 -0800503
504 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800505// Disable RAW tests for Win32.
506#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800507 check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
ebrauer46d2aa82016-02-17 08:04:00 -0800508 check(r, "sample_1mp_rotated.dng", SkISize::Make(600, 338), false, false, false);
yujieqin9c7a8a42016-02-05 08:21:19 -0800509 check(r, "dng_with_preview.dng", SkISize::Make(600, 338), true, false, false);
msarett02cb4d42016-01-25 11:01:34 -0800510#endif
halcanarya096d7a2015-03-27 12:16:53 -0700511}
scroggo0a7e69c2015-04-03 07:22:22 -0700512
513static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700514 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700515 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700516 REPORTER_ASSERT(r, !codec);
517
msarett3d9d7a72015-10-21 10:27:10 -0700518 SkAndroidCodec* androidCodec =
519 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
520 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700521}
522
523// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
524// even on failure. Test some bad streams.
525DEF_TEST(Codec_leaks, r) {
526 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
527 const char nonSupportedStream[] = "hello world";
528 // The other strings should look like the beginning of a file type, so we'll call some
529 // internal version of NewFromStream, which must also delete the stream on failure.
530 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
531 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
532 const char emptyWebp[] = "RIFF1234WEBPVP";
533 const char emptyBmp[] = { 'B', 'M' };
534 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
535 const char emptyGif[] = "GIFVER";
536
537 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
538 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
539 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
540 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
541 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
542 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
543 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
544}
msarette16b04a2015-04-15 07:32:19 -0700545
scroggo2c3b2182015-10-09 08:40:59 -0700546DEF_TEST(Codec_null, r) {
scroggobed1ed62016-02-11 10:24:55 -0800547 // Attempting to create an SkCodec or an SkAndroidCodec with null should not
scroggo2c3b2182015-10-09 08:40:59 -0700548 // crash.
549 SkCodec* codec = SkCodec::NewFromStream(nullptr);
550 REPORTER_ASSERT(r, !codec);
551
msarett3d9d7a72015-10-21 10:27:10 -0700552 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
553 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700554}
555
msarette16b04a2015-04-15 07:32:19 -0700556static void test_dimensions(skiatest::Reporter* r, const char path[]) {
557 // Create the codec from the resource file
bungemanf93d7112016-09-16 06:24:20 -0700558 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarette16b04a2015-04-15 07:32:19 -0700559 if (!stream) {
msarette16b04a2015-04-15 07:32:19 -0700560 return;
561 }
mtklein18300a32016-03-16 13:53:35 -0700562 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarette16b04a2015-04-15 07:32:19 -0700563 if (!codec) {
564 ERRORF(r, "Unable to create codec '%s'", path);
565 return;
566 }
567
568 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800569 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700570 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700571 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700572 SkImageInfo scaledInfo = codec->getInfo()
573 .makeWH(scaledDims.width(), scaledDims.height())
574 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700575
576 // Set up for the decode
577 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
578 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
579 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
580
msarett3d9d7a72015-10-21 10:27:10 -0700581 SkAndroidCodec::AndroidOptions options;
582 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700583 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700584 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700585 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700586 }
587}
588
589// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
590DEF_TEST(Codec_Dimensions, r) {
591 // JPG
592 test_dimensions(r, "CMYK.jpg");
593 test_dimensions(r, "color_wheel.jpg");
594 test_dimensions(r, "grayscale.jpg");
595 test_dimensions(r, "mandrill_512_q075.jpg");
596 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700597
598 // Decoding small images with very large scaling factors is a potential
599 // source of bugs and crashes. We disable these tests in Gold because
600 // tiny images are not very useful to look at.
601 // Here we make sure that we do not crash or access illegal memory when
602 // performing scaled decodes on small images.
603 test_dimensions(r, "1x1.png");
604 test_dimensions(r, "2x2.png");
605 test_dimensions(r, "3x3.png");
606 test_dimensions(r, "3x1.png");
607 test_dimensions(r, "1x1.png");
608 test_dimensions(r, "16x1.png");
609 test_dimensions(r, "1x16.png");
610 test_dimensions(r, "mandrill_16.png");
611
yujieqin916de9f2016-01-25 08:26:16 -0800612 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800613// Disable RAW tests for Win32.
614#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800615 test_dimensions(r, "sample_1mp.dng");
ebrauer46d2aa82016-02-17 08:04:00 -0800616 test_dimensions(r, "sample_1mp_rotated.dng");
yujieqin9c7a8a42016-02-05 08:21:19 -0800617 test_dimensions(r, "dng_with_preview.dng");
msarett8e49ca32016-01-25 13:10:58 -0800618#endif
msarette16b04a2015-04-15 07:32:19 -0700619}
620
msarettd0375bc2015-08-12 08:08:56 -0700621static void test_invalid(skiatest::Reporter* r, const char path[]) {
bungemanf93d7112016-09-16 06:24:20 -0700622 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarett4b17fa32015-04-23 08:53:39 -0700623 if (!stream) {
msarett4b17fa32015-04-23 08:53:39 -0700624 return;
625 }
mtklein18300a32016-03-16 13:53:35 -0700626 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
halcanary96fcdcc2015-08-27 07:41:13 -0700627 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700628}
msarette16b04a2015-04-15 07:32:19 -0700629
msarett4b17fa32015-04-23 08:53:39 -0700630DEF_TEST(Codec_Empty, r) {
631 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700632 test_invalid(r, "empty_images/zero-dims.gif");
633 test_invalid(r, "empty_images/zero-embedded.ico");
634 test_invalid(r, "empty_images/zero-width.bmp");
635 test_invalid(r, "empty_images/zero-height.bmp");
636 test_invalid(r, "empty_images/zero-width.jpg");
637 test_invalid(r, "empty_images/zero-height.jpg");
638 test_invalid(r, "empty_images/zero-width.png");
639 test_invalid(r, "empty_images/zero-height.png");
640 test_invalid(r, "empty_images/zero-width.wbmp");
641 test_invalid(r, "empty_images/zero-height.wbmp");
642 // This image is an ico with an embedded mask-bmp. This is illegal.
643 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700644}
msarett99f567e2015-08-05 12:58:26 -0700645
646static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
bungemanf93d7112016-09-16 06:24:20 -0700647 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarett99f567e2015-08-05 12:58:26 -0700648 if (!stream) {
msarett99f567e2015-08-05 12:58:26 -0700649 return;
650 }
mtklein18300a32016-03-16 13:53:35 -0700651 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.release()));
scroggo8e6c7ad2016-09-16 08:20:38 -0700652 if (!decoder) {
653 SkDebugf("Missing codec for %s\n", path);
654 return;
655 }
656
657 const SkImageInfo info = decoder->getInfo().makeColorType(kIndex_8_SkColorType);
halcanary9d524f22016-03-29 09:03:52 -0700658
msarett99f567e2015-08-05 12:58:26 -0700659 // This should return kSuccess because kIndex8 is supported.
660 SkPMColor colorStorage[256];
661 int colorCount;
scroggo8e6c7ad2016-09-16 08:20:38 -0700662 SkCodec::Result result = decoder->startScanlineDecode(info, nullptr, colorStorage,
663 &colorCount);
664 if (SkCodec::kSuccess == result) {
665 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
666 // colorPtr and a valid colorCountPtr.
667 result = decoder->startScanlineDecode(info, nullptr, nullptr, nullptr);
668 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
669 result = decoder->startScanlineDecode(info);
670 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
671 } else if (SkCodec::kUnimplemented == result) {
672 // New method should be supported:
673 SkBitmap bm;
674 sk_sp<SkColorTable> colorTable(new SkColorTable(colorStorage, 256));
675 bm.allocPixels(info, nullptr, colorTable.get());
676 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes(), nullptr,
677 colorStorage, &colorCount);
678 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
679 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
680 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
681 } else {
682 // The test is uninteresting if kIndex8 is not supported
683 ERRORF(r, "Should not call test_invalid_parameters for non-Index8 file: %s\n", path);
msarett99f567e2015-08-05 12:58:26 -0700684 return;
685 }
686
msarett99f567e2015-08-05 12:58:26 -0700687}
688
689DEF_TEST(Codec_Params, r) {
690 test_invalid_parameters(r, "index8.png");
691 test_invalid_parameters(r, "mandrill.wbmp");
692}
scroggocf98fa92015-11-23 08:14:40 -0800693
scroggo8e6c7ad2016-09-16 08:20:38 -0700694#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
695
696#ifndef SK_PNG_DISABLE_TESTS // reading chunks does not work properly with older versions.
697 // It does not appear that anyone in Google3 is reading chunks.
698
scroggocf98fa92015-11-23 08:14:40 -0800699static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
700 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
701 if (!sk_stream->write(data, len)) {
702 png_error(png_ptr, "sk_write_fn Error!");
703 }
704}
705
scroggocf98fa92015-11-23 08:14:40 -0800706DEF_TEST(Codec_pngChunkReader, r) {
707 // Create a dummy bitmap. Use unpremul RGBA for libpng.
708 SkBitmap bm;
709 const int w = 1;
710 const int h = 1;
711 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
712 kUnpremul_SkAlphaType);
713 bm.setInfo(bmInfo);
714 bm.allocPixels();
715 bm.eraseColor(SK_ColorBLUE);
716 SkMD5::Digest goodDigest;
717 md5(bm, &goodDigest);
718
719 // Write to a png file.
720 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
721 REPORTER_ASSERT(r, png);
722 if (!png) {
723 return;
724 }
725
726 png_infop info = png_create_info_struct(png);
727 REPORTER_ASSERT(r, info);
728 if (!info) {
729 png_destroy_write_struct(&png, nullptr);
730 return;
731 }
732
733 if (setjmp(png_jmpbuf(png))) {
734 ERRORF(r, "failed writing png");
735 png_destroy_write_struct(&png, &info);
736 return;
737 }
738
739 SkDynamicMemoryWStream wStream;
740 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
741
742 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
743 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
744 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
745
746 // Create some chunks that match the Android framework's use.
747 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800748 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
749 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
750 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800751 };
752
753 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
754 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
755#if PNG_LIBPNG_VER < 10600
756 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800757 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
758 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800759#endif
760
761 png_write_info(png, info);
762
763 for (int j = 0; j < h; j++) {
764 png_bytep row = (png_bytep)(bm.getAddr(0, j));
765 png_write_rows(png, &row, 1);
766 }
767 png_write_end(png, info);
768 png_destroy_write_struct(&png, &info);
769
770 class ChunkReader : public SkPngChunkReader {
771 public:
772 ChunkReader(skiatest::Reporter* r)
773 : fReporter(r)
774 {
775 this->reset();
776 }
777
778 bool readChunk(const char tag[], const void* data, size_t length) override {
779 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
780 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
781 // Tag matches. This should have been the first time we see it.
782 REPORTER_ASSERT(fReporter, !fSeen[i]);
783 fSeen[i] = true;
784
785 // Data and length should match
786 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
787 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
788 (const char*) gUnknowns[i].data));
789 return true;
790 }
791 }
792 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
793 return true;
794 }
795
796 bool allHaveBeenSeen() {
797 bool ret = true;
798 for (auto seen : fSeen) {
799 ret &= seen;
800 }
801 return ret;
802 }
803
804 void reset() {
805 sk_bzero(fSeen, sizeof(fSeen));
806 }
807
808 private:
809 skiatest::Reporter* fReporter; // Unowned
810 bool fSeen[3];
811 };
812
813 ChunkReader chunkReader(r);
814
815 // Now read the file with SkCodec.
reed42943c82016-09-12 12:01:44 -0700816 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(wStream.detachAsData(), &chunkReader));
scroggocf98fa92015-11-23 08:14:40 -0800817 REPORTER_ASSERT(r, codec);
818 if (!codec) {
819 return;
820 }
821
822 // Now compare to the original.
823 SkBitmap decodedBm;
824 decodedBm.setInfo(codec->getInfo());
825 decodedBm.allocPixels();
826 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
827 decodedBm.rowBytes());
828 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
829
830 if (decodedBm.colorType() != bm.colorType()) {
831 SkBitmap tmp;
832 bool success = decodedBm.copyTo(&tmp, bm.colorType());
833 REPORTER_ASSERT(r, success);
834 if (!success) {
835 return;
836 }
837
838 tmp.swap(decodedBm);
839 }
840
841 compare_to_good_digest(r, goodDigest, decodedBm);
842 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
843
844 // Decoding again will read the chunks again.
845 chunkReader.reset();
846 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
847 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
848 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
849 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
850}
scroggo8e6c7ad2016-09-16 08:20:38 -0700851#endif // SK_PNG_DISABLE_TESTS
scroggocf98fa92015-11-23 08:14:40 -0800852#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800853
scroggodb30be22015-12-08 18:54:13 -0800854// Stream that can only peek up to a limit
855class LimitedPeekingMemStream : public SkStream {
856public:
reed42943c82016-09-12 12:01:44 -0700857 LimitedPeekingMemStream(sk_sp<SkData> data, size_t limit)
858 : fStream(std::move(data))
scroggodb30be22015-12-08 18:54:13 -0800859 , fLimit(limit) {}
860
861 size_t peek(void* buf, size_t bytes) const override {
862 return fStream.peek(buf, SkTMin(bytes, fLimit));
863 }
864 size_t read(void* buf, size_t bytes) override {
865 return fStream.read(buf, bytes);
866 }
867 bool rewind() override {
868 return fStream.rewind();
869 }
870 bool isAtEnd() const override {
msarettff2a6c82016-09-07 11:23:28 -0700871 return fStream.isAtEnd();
scroggodb30be22015-12-08 18:54:13 -0800872 }
873private:
874 SkMemoryStream fStream;
875 const size_t fLimit;
876};
877
yujieqin9c7a8a42016-02-05 08:21:19 -0800878// Stream that is not an asset stream (!hasPosition() or !hasLength())
879class NotAssetMemStream : public SkStream {
880public:
bungeman38d909e2016-08-02 14:40:46 -0700881 NotAssetMemStream(sk_sp<SkData> data) : fStream(std::move(data)) {}
yujieqin9c7a8a42016-02-05 08:21:19 -0800882
883 bool hasPosition() const override {
884 return false;
885 }
886
887 bool hasLength() const override {
888 return false;
889 }
890
891 size_t peek(void* buf, size_t bytes) const override {
892 return fStream.peek(buf, bytes);
893 }
894 size_t read(void* buf, size_t bytes) override {
895 return fStream.read(buf, bytes);
896 }
897 bool rewind() override {
898 return fStream.rewind();
899 }
900 bool isAtEnd() const override {
901 return fStream.isAtEnd();
902 }
903private:
904 SkMemoryStream fStream;
905};
906
yujieqinf236ee42016-02-29 07:14:42 -0800907// Disable RAW tests for Win32.
908#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin9c7a8a42016-02-05 08:21:19 -0800909// Test that the RawCodec works also for not asset stream. This will test the code path using
910// SkRawBufferedStream instead of SkRawAssetStream.
yujieqin9c7a8a42016-02-05 08:21:19 -0800911DEF_TEST(Codec_raw_notseekable, r) {
912 const char* path = "dng_with_preview.dng";
913 SkString fullPath(GetResourcePath(path));
bungeman38d909e2016-08-02 14:40:46 -0700914 sk_sp<SkData> data(SkData::MakeFromFileName(fullPath.c_str()));
yujieqin9c7a8a42016-02-05 08:21:19 -0800915 if (!data) {
916 SkDebugf("Missing resource '%s'\n", path);
917 return;
918 }
919
bungeman38d909e2016-08-02 14:40:46 -0700920 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(std::move(data))));
yujieqin9c7a8a42016-02-05 08:21:19 -0800921 REPORTER_ASSERT(r, codec);
922
923 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
924}
925#endif
926
scroggodb30be22015-12-08 18:54:13 -0800927// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
928// + rewind() and succeed.
929DEF_TEST(Codec_webp_peek, r) {
930 const char* path = "baby_tux.webp";
931 SkString fullPath(GetResourcePath(path));
reedfde05112016-03-11 13:02:28 -0800932 auto data = SkData::MakeFromFileName(fullPath.c_str());
scroggodb30be22015-12-08 18:54:13 -0800933 if (!data) {
934 SkDebugf("Missing resource '%s'\n", path);
935 return;
936 }
937
938 // The limit is less than webp needs to peek or read.
reedfde05112016-03-11 13:02:28 -0800939 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(
reed42943c82016-09-12 12:01:44 -0700940 new LimitedPeekingMemStream(data, 25)));
scroggodb30be22015-12-08 18:54:13 -0800941 REPORTER_ASSERT(r, codec);
942
scroggo7b5e5532016-02-04 06:14:24 -0800943 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800944
945 // Similarly, a stream which does not peek should still succeed.
reed42943c82016-09-12 12:01:44 -0700946 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0)));
scroggodb30be22015-12-08 18:54:13 -0800947 REPORTER_ASSERT(r, codec);
948
scroggo7b5e5532016-02-04 06:14:24 -0800949 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800950}
951
msarett7f7ec202016-03-01 12:12:27 -0800952// SkCodec's wbmp decoder was initially unnecessarily restrictive.
953// It required the second byte to be zero. The wbmp specification allows
954// a couple of bits to be 1 (so long as they do not overlap with 0x9F).
955// Test that SkCodec now supports an image with these bits set.
scroggob9a1e342015-11-30 06:25:31 -0800956DEF_TEST(Codec_wbmp, r) {
957 const char* path = "mandrill.wbmp";
bungemanf93d7112016-09-16 06:24:20 -0700958 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
scroggob9a1e342015-11-30 06:25:31 -0800959 if (!stream) {
scroggob9a1e342015-11-30 06:25:31 -0800960 return;
961 }
962
963 // Modify the stream to contain a second byte with some bits set.
reedfde05112016-03-11 13:02:28 -0800964 auto data = SkCopyStreamToData(stream);
scroggob9a1e342015-11-30 06:25:31 -0800965 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
966 writeableData[1] = static_cast<uint8_t>(~0x9F);
967
msarett7f7ec202016-03-01 12:12:27 -0800968 // SkCodec should support this.
reed42943c82016-09-12 12:01:44 -0700969 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
scroggob9a1e342015-11-30 06:25:31 -0800970 REPORTER_ASSERT(r, codec);
971 if (!codec) {
972 return;
973 }
scroggo7b5e5532016-02-04 06:14:24 -0800974 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800975}
scroggodb30be22015-12-08 18:54:13 -0800976
977// wbmp images have a header that can be arbitrarily large, depending on the
978// size of the image. We cap the size at 65535, meaning we only need to look at
979// 8 bytes to determine whether we can read the image. This is important
980// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
981// image is a wbmp.
982DEF_TEST(Codec_wbmp_max_size, r) {
983 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
984 0x83, 0xFF, 0x7F, // W: 65535
985 0x83, 0xFF, 0x7F }; // H: 65535
986 SkAutoTDelete<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
mtklein18300a32016-03-16 13:53:35 -0700987 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -0800988
989 REPORTER_ASSERT(r, codec);
990 if (!codec) return;
991
992 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
993 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
994
995 // Now test an image which is too big. Any image with a larger header (i.e.
996 // has bigger width/height) is also too big.
997 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
998 0x84, 0x80, 0x00, // W: 65536
999 0x84, 0x80, 0x00 }; // H: 65536
1000 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
mtklein18300a32016-03-16 13:53:35 -07001001 codec.reset(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -08001002
1003 REPORTER_ASSERT(r, !codec);
1004}
msarett2812f032016-07-18 15:56:08 -07001005
1006DEF_TEST(Codec_jpeg_rewind, r) {
1007 const char* path = "mandrill_512_q075.jpg";
bungemanf93d7112016-09-16 06:24:20 -07001008 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarett2812f032016-07-18 15:56:08 -07001009 if (!stream) {
msarett2812f032016-07-18 15:56:08 -07001010 return;
1011 }
1012 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
1013 if (!codec) {
1014 ERRORF(r, "Unable to create codec '%s'.", path);
1015 return;
1016 }
1017
1018 const int width = codec->getInfo().width();
1019 const int height = codec->getInfo().height();
1020 size_t rowBytes = sizeof(SkPMColor) * width;
1021 SkAutoMalloc pixelStorage(height * rowBytes);
1022
1023 // Perform a sampled decode.
1024 SkAndroidCodec::AndroidOptions opts;
1025 opts.fSampleSize = 12;
1026 codec->getAndroidPixels(codec->getInfo().makeWH(width / 12, height / 12), pixelStorage.get(),
1027 rowBytes, &opts);
1028
1029 // Rewind the codec and perform a full image decode.
1030 SkCodec::Result result = codec->getPixels(codec->getInfo(), pixelStorage.get(), rowBytes);
1031 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1032}
msarett549ca322016-08-17 08:54:08 -07001033
msarett35bb74b2016-08-22 07:41:28 -07001034static void check_color_xform(skiatest::Reporter* r, const char* path) {
bungemanf93d7112016-09-16 06:24:20 -07001035 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(GetResourceAsStream(path)));
msarett35bb74b2016-08-22 07:41:28 -07001036
1037 SkAndroidCodec::AndroidOptions opts;
1038 opts.fSampleSize = 3;
1039 const int subsetWidth = codec->getInfo().width() / 2;
1040 const int subsetHeight = codec->getInfo().height() / 2;
1041 SkIRect subset = SkIRect::MakeWH(subsetWidth, subsetHeight);
1042 opts.fSubset = &subset;
1043
1044 const int dstWidth = subsetWidth / opts.fSampleSize;
1045 const int dstHeight = subsetHeight / opts.fSampleSize;
1046 sk_sp<SkData> data = SkData::MakeFromFileName(
1047 GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str());
1048 sk_sp<SkColorSpace> colorSpace = SkColorSpace::NewICC(data->data(), data->size());
1049 SkImageInfo dstInfo = codec->getInfo().makeWH(dstWidth, dstHeight)
1050 .makeColorType(kN32_SkColorType)
1051 .makeColorSpace(colorSpace);
1052
1053 size_t rowBytes = dstInfo.minRowBytes();
1054 SkAutoMalloc pixelStorage(dstInfo.getSafeSize(rowBytes));
1055 SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get(), rowBytes, &opts);
1056 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1057}
1058
1059DEF_TEST(Codec_ColorXform, r) {
1060 check_color_xform(r, "mandrill_512_q075.jpg");
1061 check_color_xform(r, "mandrill_512.png");
1062}
1063
msarettf17b71f2016-09-12 14:30:03 -07001064static bool color_type_match(SkColorType origColorType, SkColorType codecColorType) {
1065 switch (origColorType) {
1066 case kRGBA_8888_SkColorType:
1067 case kBGRA_8888_SkColorType:
1068 return kRGBA_8888_SkColorType == codecColorType ||
1069 kBGRA_8888_SkColorType == codecColorType;
1070 default:
1071 return origColorType == codecColorType;
1072 }
1073}
1074
1075static bool alpha_type_match(SkAlphaType origAlphaType, SkAlphaType codecAlphaType) {
1076 switch (origAlphaType) {
1077 case kUnpremul_SkAlphaType:
1078 case kPremul_SkAlphaType:
1079 return kUnpremul_SkAlphaType == codecAlphaType ||
1080 kPremul_SkAlphaType == codecAlphaType;
1081 default:
1082 return origAlphaType == codecAlphaType;
1083 }
1084}
1085
1086static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const SkImageInfo& info) {
1087 SkBitmap bm1;
1088 SkPMColor colors[256];
1089 SkAutoTUnref<SkColorTable> colorTable1(new SkColorTable(colors, 256));
1090 bm1.allocPixels(info, nullptr, colorTable1.get());
1091 int numColors;
1092 SkCodec::Result result = origCodec->getPixels(info, bm1.getPixels(), bm1.rowBytes(), nullptr,
1093 const_cast<SkPMColor*>(colorTable1->readColors()),
1094 &numColors);
1095 // This will fail to update colorTable1->count() but is fine for the purpose of this test.
1096 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarett9b09cd82016-08-29 14:47:49 -07001097
1098 // Encode the image to png.
1099 sk_sp<SkData> data =
1100 sk_sp<SkData>(SkImageEncoder::EncodeData(bm1, SkImageEncoder::kPNG_Type, 100));
1101
reed42943c82016-09-12 12:01:44 -07001102 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
msarettf17b71f2016-09-12 14:30:03 -07001103 REPORTER_ASSERT(r, color_type_match(info.colorType(), codec->getInfo().colorType()));
1104 REPORTER_ASSERT(r, alpha_type_match(info.alphaType(), codec->getInfo().alphaType()));
msarett9b09cd82016-08-29 14:47:49 -07001105
1106 SkBitmap bm2;
msarettf17b71f2016-09-12 14:30:03 -07001107 SkAutoTUnref<SkColorTable> colorTable2(new SkColorTable(colors, 256));
1108 bm2.allocPixels(info, nullptr, colorTable2.get());
1109 result = codec->getPixels(info, bm2.getPixels(), bm2.rowBytes(), nullptr,
1110 const_cast<SkPMColor*>(colorTable2->readColors()), &numColors);
msarett9b09cd82016-08-29 14:47:49 -07001111 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1112
1113 SkMD5::Digest d1, d2;
1114 md5(bm1, &d1);
1115 md5(bm2, &d2);
1116 REPORTER_ASSERT(r, d1 == d2);
1117}
1118
1119DEF_TEST(Codec_PngRoundTrip, r) {
msarett549ca322016-08-17 08:54:08 -07001120 const char* path = "mandrill_512_q075.jpg";
bungemanf93d7112016-09-16 06:24:20 -07001121 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarett549ca322016-08-17 08:54:08 -07001122 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett549ca322016-08-17 08:54:08 -07001123
msarettf17b71f2016-09-12 14:30:03 -07001124 SkColorType colorTypesOpaque[] = {
1125 kRGB_565_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1126 };
1127 for (SkColorType colorType : colorTypesOpaque) {
1128 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType);
1129 check_round_trip(r, codec.get(), newInfo);
1130 }
1131
msarett9b09cd82016-08-29 14:47:49 -07001132 path = "grayscale.jpg";
bungemanf93d7112016-09-16 06:24:20 -07001133 stream.reset(GetResourceAsStream(path));
msarett9b09cd82016-08-29 14:47:49 -07001134 codec.reset(SkCodec::NewFromStream(stream.release()));
msarettf17b71f2016-09-12 14:30:03 -07001135 check_round_trip(r, codec.get(), codec->getInfo());
1136
1137 path = "yellow_rose.png";
bungemanf93d7112016-09-16 06:24:20 -07001138 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001139 codec.reset(SkCodec::NewFromStream(stream.release()));
1140
1141 SkColorType colorTypesWithAlpha[] = {
1142 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1143 };
1144 SkAlphaType alphaTypes[] = {
1145 kUnpremul_SkAlphaType, kPremul_SkAlphaType
1146 };
1147 for (SkColorType colorType : colorTypesWithAlpha) {
1148 for (SkAlphaType alphaType : alphaTypes) {
1149 // Set color space to nullptr because color correct premultiplies do not round trip.
1150 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType)
1151 .makeAlphaType(alphaType)
1152 .makeColorSpace(nullptr);
1153 check_round_trip(r, codec.get(), newInfo);
1154 }
1155 }
1156
1157 path = "index8.png";
bungemanf93d7112016-09-16 06:24:20 -07001158 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001159 codec.reset(SkCodec::NewFromStream(stream.release()));
1160
1161 for (SkAlphaType alphaType : alphaTypes) {
1162 SkImageInfo newInfo = codec->getInfo().makeAlphaType(alphaType)
1163 .makeColorSpace(nullptr);
1164 check_round_trip(r, codec.get(), newInfo);
1165 }
msarett549ca322016-08-17 08:54:08 -07001166}
msarett2ecc35f2016-09-08 11:55:16 -07001167
1168static void test_conversion_possible(skiatest::Reporter* r, const char* path,
scroggo8e6c7ad2016-09-16 08:20:38 -07001169 bool supportsScanlineDecoder,
1170 bool supportsIncrementalDecoder) {
bungemanf93d7112016-09-16 06:24:20 -07001171 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarett2ecc35f2016-09-08 11:55:16 -07001172 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
1173 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType);
1174
1175 SkBitmap bm;
1176 bm.allocPixels(infoF16);
1177 SkCodec::Result result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1178 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001179
1180 result = codec->startScanlineDecode(infoF16);
1181 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001182 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001183 } else {
1184 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1185 }
1186
1187 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1188 if (supportsIncrementalDecoder) {
1189 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
1190 } else {
1191 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001192 }
1193
1194 infoF16 = infoF16.makeColorSpace(infoF16.colorSpace()->makeLinearGamma());
1195 result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1196 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001197 result = codec->startScanlineDecode(infoF16);
1198 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001199 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001200 } else {
1201 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1202 }
1203
1204 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1205 if (supportsIncrementalDecoder) {
1206 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1207 } else {
1208 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001209 }
1210}
1211
1212DEF_TEST(Codec_F16ConversionPossible, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -07001213 test_conversion_possible(r, "color_wheel.webp", false, false);
1214 test_conversion_possible(r, "mandrill_512_q075.jpg", true, false);
1215 test_conversion_possible(r, "yellow_rose.png", false, true);
1216}
1217
1218// Only rewinds up to a limit.
1219class LimitedRewindingStream : public SkStream {
1220public:
1221 static SkStream* Make(const char path[], size_t limit) {
1222 SkStream* stream = GetResourceAsStream(path);
1223 if (!stream) {
1224 return nullptr;
1225 }
1226 return new LimitedRewindingStream(stream, limit);
1227 }
1228
1229 size_t read(void* buffer, size_t size) override {
1230 const size_t bytes = fStream->read(buffer, size);
1231 fPosition += bytes;
1232 return bytes;
1233 }
1234
1235 bool isAtEnd() const override {
1236 return fStream->isAtEnd();
1237 }
1238
1239 bool rewind() override {
1240 if (fPosition <= fLimit && fStream->rewind()) {
1241 fPosition = 0;
1242 return true;
1243 }
1244
1245 return false;
1246 }
1247
1248private:
1249 SkAutoTDelete<SkStream> fStream;
1250 const size_t fLimit;
1251 size_t fPosition;
1252
1253 LimitedRewindingStream(SkStream* stream, size_t limit)
1254 : fStream(stream)
1255 , fLimit(limit)
1256 , fPosition(0)
1257 {
1258 SkASSERT(fStream);
1259 }
1260};
1261
1262DEF_TEST(Codec_fallBack, r) {
1263 // SkAndroidCodec needs to be able to fall back to scanline decoding
1264 // if incremental decoding does not work. Make sure this does not
1265 // require a rewind.
1266
1267 // Formats that currently do not support incremental decoding
1268 auto files = {
1269 "box.gif",
1270 "CMYK.jpg",
1271 "color_wheel.ico",
1272 "mandrill.wbmp",
1273 "randPixels.bmp",
1274 };
1275 for (auto file : files) {
1276 SkStream* stream = LimitedRewindingStream::Make(file, 14);
1277 if (!stream) {
1278 SkDebugf("Missing resources (%s). Set --resourcePath.\n", file);
1279 return;
1280 }
1281
1282 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream));
1283 if (!codec) {
1284 ERRORF(r, "Failed to create codec for %s,", file);
1285 continue;
1286 }
1287
1288 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
1289 SkBitmap bm;
1290 bm.allocPixels(info);
1291
1292 if (SkCodec::kUnimplemented != codec->startIncrementalDecode(info, bm.getPixels(),
1293 bm.rowBytes())) {
1294 ERRORF(r, "Is scanline decoding now implemented for %s?", file);
1295 continue;
1296 }
1297
1298 // Scanline decoding should not require a rewind.
1299 SkCodec::Result result = codec->startScanlineDecode(info);
1300 if (SkCodec::kSuccess != result) {
1301 ERRORF(r, "Scanline decoding failed for %s with %i", file, result);
1302 }
1303 }
msarett2ecc35f2016-09-08 11:55:16 -07001304}
scroggoc46cdd42016-10-10 06:45:32 -07001305
1306// This test verifies that we fixed an assert statement that fired when reusing a png codec
1307// after scaling.
1308DEF_TEST(Codec_reusePng, r) {
1309 std::unique_ptr<SkStream> stream(GetResourceAsStream("plane.png"));
1310 if (!stream) {
1311 return;
1312 }
1313
1314 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
1315 if (!codec) {
1316 ERRORF(r, "Failed to create codec\n");
1317 return;
1318 }
1319
1320 SkAndroidCodec::AndroidOptions opts;
1321 opts.fSampleSize = 5;
1322 auto size = codec->getSampledDimensions(opts.fSampleSize);
1323 auto info = codec->getInfo().makeWH(size.fWidth, size.fHeight).makeColorType(kN32_SkColorType);
1324 SkBitmap bm;
1325 bm.allocPixels(info);
1326 auto result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1327 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1328
1329 info = codec->getInfo().makeColorType(kN32_SkColorType);
1330 bm.allocPixels(info);
1331 opts.fSampleSize = 1;
1332 result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1333 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1334}
scroggoe61b3b42016-10-10 07:17:32 -07001335
1336DEF_TEST(Codec_rowsDecoded, r) {
1337 auto file = "plane_interlaced.png";
1338 std::unique_ptr<SkStream> stream(GetResourceAsStream(file));
1339 if (!stream) {
1340 return;
1341 }
1342
1343 // This is enough to read the header etc, but no rows.
1344 auto data = SkData::MakeFromStream(stream.get(), 99);
1345 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
1346 if (!codec) {
1347 ERRORF(r, "Failed to create codec\n");
1348 return;
1349 }
1350
1351 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1352 SkBitmap bm;
1353 bm.allocPixels(info);
1354 auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
1355 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1356
1357 // This is an arbitrary value. The important fact is that it is not zero, and rowsDecoded
1358 // should get set to zero by incrementalDecode.
1359 int rowsDecoded = 77;
1360 result = codec->incrementalDecode(&rowsDecoded);
1361 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput);
1362 REPORTER_ASSERT(r, rowsDecoded == 0);
1363}