blob: c8b573a6680e03b598008146ca155ad154d7923e [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"
raftias94888332016-10-18 10:02:51 -070013#include "SkColorSpace_XYZ.h"
msarette6dd0042015-10-09 11:07:34 -070014#include "SkData.h"
msarett549ca322016-08-17 08:54:08 -070015#include "SkImageEncoder.h"
scroggoef0fed32016-02-18 05:59:25 -080016#include "SkFrontBufferedStream.h"
halcanarya096d7a2015-03-27 12:16:53 -070017#include "SkMD5.h"
scroggob636b452015-07-22 07:16:20 -070018#include "SkRandom.h"
scroggocf98fa92015-11-23 08:14:40 -080019#include "SkStream.h"
scroggob9a1e342015-11-30 06:25:31 -080020#include "SkStreamPriv.h"
scroggocf98fa92015-11-23 08:14:40 -080021#include "SkPngChunkReader.h"
halcanarya096d7a2015-03-27 12:16:53 -070022#include "Test.h"
23
scroggocf98fa92015-11-23 08:14:40 -080024#include "png.h"
25
scroggo8e6c7ad2016-09-16 08:20:38 -070026#if PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 5
27 // FIXME (scroggo): Google3 needs to be updated to use a newer version of libpng. In
28 // the meantime, we had to break some pieces of SkPngCodec in order to support Google3.
29 // The parts that are broken are likely not used by Google3.
30 #define SK_PNG_DISABLE_TESTS
31#endif
32
halcanarya096d7a2015-03-27 12:16:53 -070033static void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
34 SkAutoLockPixels autoLockPixels(bm);
35 SkASSERT(bm.getPixels());
36 SkMD5 md5;
37 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
38 for (int y = 0; y < bm.height(); ++y) {
halcanary1e903042016-04-25 10:29:36 -070039 md5.write(bm.getAddr(0, y), rowLen);
halcanarya096d7a2015-03-27 12:16:53 -070040 }
41 md5.finish(*digest);
42}
43
scroggo9b2cdbf42015-07-10 12:07:02 -070044/**
45 * Compute the digest for bm and compare it to a known good digest.
46 * @param r Reporter to assert that bm's digest matches goodDigest.
47 * @param goodDigest The known good digest to compare to.
48 * @param bm The bitmap to test.
49 */
50static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
51 const SkBitmap& bm) {
52 SkMD5::Digest digest;
53 md5(bm, &digest);
54 REPORTER_ASSERT(r, digest == goodDigest);
55}
56
scroggod1bc5742015-08-12 08:31:44 -070057/**
58 * Test decoding an SkCodec to a particular SkImageInfo.
59 *
halcanary96fcdcc2015-08-27 07:41:13 -070060 * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr,
scroggod1bc5742015-08-12 08:31:44 -070061 * the resulting decode should match.
62 */
scroggo7b5e5532016-02-04 06:14:24 -080063template<typename Codec>
64static void test_info(skiatest::Reporter* r, Codec* codec, const SkImageInfo& info,
scroggod1bc5742015-08-12 08:31:44 -070065 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
66 SkBitmap bm;
67 bm.allocPixels(info);
68 SkAutoLockPixels autoLockPixels(bm);
69
70 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
71 REPORTER_ASSERT(r, result == expectedResult);
72
73 if (goodDigest) {
74 compare_to_good_digest(r, *goodDigest, bm);
75 }
76}
77
scroggob636b452015-07-22 07:16:20 -070078SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
79 SkIRect rect;
80 do {
81 rect.fLeft = rand->nextRangeU(0, w);
82 rect.fTop = rand->nextRangeU(0, h);
83 rect.fRight = rand->nextRangeU(0, w);
84 rect.fBottom = rand->nextRangeU(0, h);
85 rect.sort();
86 } while (rect.isEmpty());
87 return rect;
88}
89
scroggo8e6c7ad2016-09-16 08:20:38 -070090static void test_incremental_decode(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
91 const SkMD5::Digest& goodDigest) {
92 SkBitmap bm;
93 bm.allocPixels(info);
94 SkAutoLockPixels autoLockPixels(bm);
95
96 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->startIncrementalDecode(info, bm.getPixels(),
97 bm.rowBytes()));
98
99 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->incrementalDecode());
100
101 compare_to_good_digest(r, goodDigest, bm);
102}
103
104// Test in stripes, similar to DM's kStripe_Mode
105static void test_in_stripes(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
106 const SkMD5::Digest& goodDigest) {
107 SkBitmap bm;
108 bm.allocPixels(info);
109 bm.eraseColor(SK_ColorYELLOW);
110
111 const int height = info.height();
112 // Note that if numStripes does not evenly divide height there will be an extra
113 // stripe.
114 const int numStripes = 4;
115
116 if (numStripes > height) {
117 // Image is too small.
118 return;
119 }
120
121 const int stripeHeight = height / numStripes;
122
123 // Iterate through the image twice. Once to decode odd stripes, and once for even.
124 for (int oddEven = 1; oddEven >= 0; oddEven--) {
125 for (int y = oddEven * stripeHeight; y < height; y += 2 * stripeHeight) {
126 SkIRect subset = SkIRect::MakeLTRB(0, y, info.width(),
127 SkTMin(y + stripeHeight, height));
128 SkCodec::Options options;
129 options.fSubset = &subset;
130 if (SkCodec::kSuccess != codec->startIncrementalDecode(info, bm.getAddr(0, y),
131 bm.rowBytes(), &options)) {
132 ERRORF(r, "failed to start incremental decode!\ttop: %i\tbottom%i\n",
133 subset.top(), subset.bottom());
134 return;
135 }
136 if (SkCodec::kSuccess != codec->incrementalDecode()) {
137 ERRORF(r, "failed incremental decode starting from line %i\n", y);
138 return;
139 }
140 }
141 }
142
143 compare_to_good_digest(r, goodDigest, bm);
144}
145
scroggo7b5e5532016-02-04 06:14:24 -0800146template<typename Codec>
147static void test_codec(skiatest::Reporter* r, Codec* codec, SkBitmap& bm, const SkImageInfo& info,
scroggo27c17282015-10-27 08:14:46 -0700148 const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest,
149 const SkMD5::Digest* goodDigest) {
msarette6dd0042015-10-09 11:07:34 -0700150
halcanarya096d7a2015-03-27 12:16:53 -0700151 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -0700152 bm.allocPixels(info);
153 SkAutoLockPixels autoLockPixels(bm);
msarettcc7f3052015-10-05 14:20:27 -0700154
155 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700156 REPORTER_ASSERT(r, result == expectedResult);
halcanarya096d7a2015-03-27 12:16:53 -0700157
msarettcc7f3052015-10-05 14:20:27 -0700158 md5(bm, digest);
159 if (goodDigest) {
160 REPORTER_ASSERT(r, *digest == *goodDigest);
161 }
halcanarya096d7a2015-03-27 12:16:53 -0700162
msarett8ff6ca62015-09-18 12:06:04 -0700163 {
164 // Test decoding to 565
165 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggoba584892016-05-20 13:56:13 -0700166 if (info.alphaType() == kOpaque_SkAlphaType) {
167 // Decoding to 565 should succeed.
168 SkBitmap bm565;
169 bm565.allocPixels(info565);
170 SkAutoLockPixels alp(bm565);
171
172 // This will allow comparison even if the image is incomplete.
173 bm565.eraseColor(SK_ColorBLACK);
174
175 REPORTER_ASSERT(r, expectedResult == codec->getPixels(info565,
176 bm565.getPixels(), bm565.rowBytes()));
177
178 SkMD5::Digest digest565;
179 md5(bm565, &digest565);
180
181 // A dumb client's request for non-opaque should also succeed.
182 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
183 info565 = info565.makeAlphaType(alpha);
184 test_info(r, codec, info565, expectedResult, &digest565);
185 }
186 } else {
187 test_info(r, codec, info565, SkCodec::kInvalidConversion, nullptr);
188 }
189 }
190
191 if (codec->getInfo().colorType() == kGray_8_SkColorType) {
192 SkImageInfo grayInfo = codec->getInfo();
193 SkBitmap grayBm;
194 grayBm.allocPixels(grayInfo);
195 SkAutoLockPixels alp(grayBm);
196
197 grayBm.eraseColor(SK_ColorBLACK);
198
199 REPORTER_ASSERT(r, expectedResult == codec->getPixels(grayInfo,
200 grayBm.getPixels(), grayBm.rowBytes()));
201
202 SkMD5::Digest grayDigest;
203 md5(grayBm, &grayDigest);
204
205 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
206 grayInfo = grayInfo.makeAlphaType(alpha);
207 test_info(r, codec, grayInfo, expectedResult, &grayDigest);
208 }
msarett8ff6ca62015-09-18 12:06:04 -0700209 }
210
211 // Verify that re-decoding gives the same result. It is interesting to check this after
212 // a decode to 565, since choosing to decode to 565 may result in some of the decode
213 // options being modified. These options should return to their defaults on another
214 // decode to kN32, so the new digest should match the old digest.
msarette6dd0042015-10-09 11:07:34 -0700215 test_info(r, codec, info, expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700216
217 {
218 // Check alpha type conversions
219 if (info.alphaType() == kOpaque_SkAlphaType) {
220 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800221 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700222 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800223 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700224 } else {
225 // Decoding to opaque should fail
226 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700227 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700228 SkAlphaType otherAt = info.alphaType();
229 if (kPremul_SkAlphaType == otherAt) {
230 otherAt = kUnpremul_SkAlphaType;
231 } else {
232 otherAt = kPremul_SkAlphaType;
233 }
234 // The other non-opaque alpha type should always succeed, but not match.
msarette6dd0042015-10-09 11:07:34 -0700235 test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700236 }
237 }
msarettcc7f3052015-10-05 14:20:27 -0700238}
239
scroggobed1ed62016-02-11 10:24:55 -0800240static bool supports_partial_scanlines(const char path[]) {
scroggo2c3b2182015-10-09 08:40:59 -0700241 static const char* const exts[] = {
242 "jpg", "jpeg", "png", "webp"
243 "JPG", "JPEG", "PNG", "WEBP"
244 };
245
246 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
247 if (SkStrEndsWith(path, exts[i])) {
248 return true;
249 }
250 }
251 return false;
252}
253
scroggo8e6c7ad2016-09-16 08:20:38 -0700254// FIXME: Break up this giant function
msarettcc7f3052015-10-05 14:20:27 -0700255static void check(skiatest::Reporter* r,
256 const char path[],
257 SkISize size,
258 bool supportsScanlineDecoding,
259 bool supportsSubsetDecoding,
scroggo8e6c7ad2016-09-16 08:20:38 -0700260 bool supportsIncomplete,
261 bool supportsNewScanlineDecoding = false) {
msarettcc7f3052015-10-05 14:20:27 -0700262
Ben Wagner145dbcd2016-11-03 14:40:50 -0400263 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarettcc7f3052015-10-05 14:20:27 -0700264 if (!stream) {
msarettcc7f3052015-10-05 14:20:27 -0700265 return;
266 }
msarette6dd0042015-10-09 11:07:34 -0700267
Ben Wagner145dbcd2016-11-03 14:40:50 -0400268 std::unique_ptr<SkCodec> codec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700269 bool isIncomplete = supportsIncomplete;
270 if (isIncomplete) {
271 size_t size = stream->getLength();
Ben Wagner145dbcd2016-11-03 14:40:50 -0400272 sk_sp<SkData> data((SkData::MakeFromStream(stream.get(), 2 * size / 3)));
reed42943c82016-09-12 12:01:44 -0700273 codec.reset(SkCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700274 } else {
mtklein18300a32016-03-16 13:53:35 -0700275 codec.reset(SkCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700276 }
msarettcc7f3052015-10-05 14:20:27 -0700277 if (!codec) {
278 ERRORF(r, "Unable to decode '%s'", path);
279 return;
280 }
281
282 // Test full image decodes with SkCodec
283 SkMD5::Digest codecDigest;
scroggoef0fed32016-02-18 05:59:25 -0800284 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
msarettcc7f3052015-10-05 14:20:27 -0700285 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700286 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
scroggo7b5e5532016-02-04 06:14:24 -0800287 test_codec(r, codec.get(), bm, info, size, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700288
289 // Scanline decoding follows.
scroggod8d68552016-06-06 11:26:17 -0700290
scroggo8e6c7ad2016-09-16 08:20:38 -0700291 if (supportsNewScanlineDecoding && !isIncomplete) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400292 test_incremental_decode(r, codec.get(), info, codecDigest);
scroggo19b91532016-10-24 09:03:26 -0700293 // This is only supported by codecs that use incremental decoding to
294 // support subset decodes - png and jpeg (once SkJpegCodec is
295 // converted).
296 if (SkStrEndsWith(path, "png") || SkStrEndsWith(path, "PNG")) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400297 test_in_stripes(r, codec.get(), info, codecDigest);
scroggo19b91532016-10-24 09:03:26 -0700298 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700299 }
300
301 // Need to call startScanlineDecode() first.
302 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) == 0);
303 REPORTER_ASSERT(r, !codec->skipScanlines(1));
scroggo46c57472015-09-30 08:57:13 -0700304 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700305 if (supportsScanlineDecoding) {
306 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700307
scroggo46c57472015-09-30 08:57:13 -0700308 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700309
scroggo58421542015-04-01 11:25:20 -0700310 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700311 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
312 if (!isIncomplete) {
313 REPORTER_ASSERT(r, 1 == lines);
314 }
scroggo58421542015-04-01 11:25:20 -0700315 }
316 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700317 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700318 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700319 }
scroggo46c57472015-09-30 08:57:13 -0700320
321 // Cannot continue to decode scanlines beyond the end
322 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700323 == 0);
scroggo46c57472015-09-30 08:57:13 -0700324
325 // Interrupting a scanline decode with a full decode starts from
326 // scratch
327 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700328 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
329 if (!isIncomplete) {
330 REPORTER_ASSERT(r, lines == 1);
331 }
scroggo46c57472015-09-30 08:57:13 -0700332 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700333 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700334 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700335 == 0);
scroggo46c57472015-09-30 08:57:13 -0700336 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700337 == 0);
msarett80803ff2015-10-16 10:54:12 -0700338
339 // Test partial scanline decodes
scroggobed1ed62016-02-11 10:24:55 -0800340 if (supports_partial_scanlines(path) && info.width() >= 3) {
msarett80803ff2015-10-16 10:54:12 -0700341 SkCodec::Options options;
342 int width = info.width();
343 int height = info.height();
344 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
345 options.fSubset = &subset;
346
347 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
348 nullptr, nullptr);
349 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
350
351 for (int y = 0; y < height; y++) {
352 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
353 if (!isIncomplete) {
354 REPORTER_ASSERT(r, 1 == lines);
355 }
356 }
357 }
scroggo58421542015-04-01 11:25:20 -0700358 } else {
scroggo46c57472015-09-30 08:57:13 -0700359 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700360 }
scroggob636b452015-07-22 07:16:20 -0700361
362 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
363 // random subsets.
364 // Do not attempt to decode subsets of an image of only once pixel, since there is no
365 // meaningful subset.
366 if (size.width() * size.height() == 1) {
367 return;
368 }
369
370 SkRandom rand;
371 SkIRect subset;
372 SkCodec::Options opts;
373 opts.fSubset = &subset;
374 for (int i = 0; i < 5; i++) {
375 subset = generate_random_subset(&rand, size.width(), size.height());
376 SkASSERT(!subset.isEmpty());
377 const bool supported = codec->getValidSubset(&subset);
378 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
379
380 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
381 SkBitmap bm;
382 bm.allocPixels(subsetInfo);
383 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700384 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700385
386 if (supportsSubsetDecoding) {
msarette6dd0042015-10-09 11:07:34 -0700387 REPORTER_ASSERT(r, result == expectedResult);
scroggob636b452015-07-22 07:16:20 -0700388 // Webp is the only codec that supports subsets, and it will have modified the subset
389 // to have even left/top.
390 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
391 } else {
392 // No subsets will work.
393 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
394 }
395 }
msarettcc7f3052015-10-05 14:20:27 -0700396
scroggobed1ed62016-02-11 10:24:55 -0800397 // SkAndroidCodec tests
scroggo8e6c7ad2016-09-16 08:20:38 -0700398 if (supportsScanlineDecoding || supportsSubsetDecoding || supportsNewScanlineDecoding) {
scroggo2c3b2182015-10-09 08:40:59 -0700399
Ben Wagner145dbcd2016-11-03 14:40:50 -0400400 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarettcc7f3052015-10-05 14:20:27 -0700401 if (!stream) {
msarettcc7f3052015-10-05 14:20:27 -0700402 return;
403 }
msarette6dd0042015-10-09 11:07:34 -0700404
Ben Wagner145dbcd2016-11-03 14:40:50 -0400405 std::unique_ptr<SkAndroidCodec> androidCodec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700406 if (isIncomplete) {
407 size_t size = stream->getLength();
Ben Wagner145dbcd2016-11-03 14:40:50 -0400408 sk_sp<SkData> data((SkData::MakeFromStream(stream.get(), 2 * size / 3)));
reed42943c82016-09-12 12:01:44 -0700409 androidCodec.reset(SkAndroidCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700410 } else {
mtklein18300a32016-03-16 13:53:35 -0700411 androidCodec.reset(SkAndroidCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700412 }
scroggo7b5e5532016-02-04 06:14:24 -0800413 if (!androidCodec) {
msarettcc7f3052015-10-05 14:20:27 -0700414 ERRORF(r, "Unable to decode '%s'", path);
415 return;
416 }
417
418 SkBitmap bm;
scroggobed1ed62016-02-11 10:24:55 -0800419 SkMD5::Digest androidCodecDigest;
420 test_codec(r, androidCodec.get(), bm, info, size, expectedResult, &androidCodecDigest,
scroggo7b5e5532016-02-04 06:14:24 -0800421 &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700422 }
423
msarettedd2dcf2016-01-14 13:12:26 -0800424 if (!isIncomplete) {
scroggoef0fed32016-02-18 05:59:25 -0800425 // Test SkCodecImageGenerator
Ben Wagner145dbcd2016-11-03 14:40:50 -0400426 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
427 sk_sp<SkData> fullData(SkData::MakeFromStream(stream.get(), stream->getLength()));
428 std::unique_ptr<SkImageGenerator> gen(
bungeman38d909e2016-08-02 14:40:46 -0700429 SkCodecImageGenerator::NewFromEncodedCodec(fullData.get()));
msarettedd2dcf2016-01-14 13:12:26 -0800430 SkBitmap bm;
431 bm.allocPixels(info);
432 SkAutoLockPixels autoLockPixels(bm);
433 REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes()));
434 compare_to_good_digest(r, codecDigest, bm);
scroggoef0fed32016-02-18 05:59:25 -0800435
scroggo8e6c7ad2016-09-16 08:20:38 -0700436#ifndef SK_PNG_DISABLE_TESTS
scroggod8d68552016-06-06 11:26:17 -0700437 // Test using SkFrontBufferedStream, as Android does
bungeman38d909e2016-08-02 14:40:46 -0700438 SkStream* bufferedStream = SkFrontBufferedStream::Create(
439 new SkMemoryStream(std::move(fullData)), SkCodec::MinBufferedBytesNeeded());
scroggod8d68552016-06-06 11:26:17 -0700440 REPORTER_ASSERT(r, bufferedStream);
441 codec.reset(SkCodec::NewFromStream(bufferedStream));
442 REPORTER_ASSERT(r, codec);
443 if (codec) {
444 test_info(r, codec.get(), info, SkCodec::kSuccess, &codecDigest);
scroggoef0fed32016-02-18 05:59:25 -0800445 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700446#endif
msarettedd2dcf2016-01-14 13:12:26 -0800447 }
448
msarette6dd0042015-10-09 11:07:34 -0700449 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
450 if (isIncomplete) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700451 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false,
452 supportsNewScanlineDecoding);
msarettcc7f3052015-10-05 14:20:27 -0700453 }
halcanarya096d7a2015-03-27 12:16:53 -0700454}
455
456DEF_TEST(Codec, r) {
457 // WBMP
scroggo8e6c7ad2016-09-16 08:20:38 -0700458 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false, true);
halcanarya096d7a2015-03-27 12:16:53 -0700459
scroggo6f5e6192015-06-18 12:53:43 -0700460 // WEBP
scroggo8e6c7ad2016-09-16 08:20:38 -0700461 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true, true);
462 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true, true);
463 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true, true);
scroggo6f5e6192015-06-18 12:53:43 -0700464
halcanarya096d7a2015-03-27 12:16:53 -0700465 // BMP
scroggo8e6c7ad2016-09-16 08:20:38 -0700466 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false, true);
467 check(r, "rle.bmp", SkISize::Make(320, 240), true, false, true);
halcanarya096d7a2015-03-27 12:16:53 -0700468
469 // ICO
msarette6dd0042015-10-09 11:07:34 -0700470 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700471 // These two tests examine interestingly different behavior:
472 // Decodes an embedded BMP image
msarettbe8216a2015-12-04 08:00:50 -0800473 check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700474 // Decodes an embedded PNG image
scroggo8e6c7ad2016-09-16 08:20:38 -0700475 check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false, false, true);
halcanarya096d7a2015-03-27 12:16:53 -0700476
msarett438b2ad2015-04-09 12:43:10 -0700477 // GIF
scroggo19b91532016-10-24 09:03:26 -0700478 check(r, "box.gif", SkISize::Make(200, 55), false, false, true, true);
479 check(r, "color_wheel.gif", SkISize::Make(128, 128), false, false, true, true);
msarette6dd0042015-10-09 11:07:34 -0700480 // randPixels.gif is too small to test incomplete
scroggo19b91532016-10-24 09:03:26 -0700481 check(r, "randPixels.gif", SkISize::Make(8, 8), false, false, false, true);
msarett438b2ad2015-04-09 12:43:10 -0700482
msarette16b04a2015-04-15 07:32:19 -0700483 // JPG
scroggo8e6c7ad2016-09-16 08:20:38 -0700484 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false, true);
485 check(r, "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
scroggo27c17282015-10-27 08:14:46 -0700487 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggo8e6c7ad2016-09-16 08:20:38 -0700488 check(r, "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
scroggo27c17282015-10-27 08:14:46 -0700490 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
msarette16b04a2015-04-15 07:32:19 -0700491
halcanarya096d7a2015-03-27 12:16:53 -0700492 // PNG
scroggo8e6c7ad2016-09-16 08:20:38 -0700493 check(r, "arrow.png", SkISize::Make(187, 312), false, false, true, true);
494 check(r, "baby_tux.png", SkISize::Make(240, 246), false, false, true, true);
495 check(r, "color_wheel.png", SkISize::Make(128, 128), false, false, true, true);
496 // half-transparent-white-pixel.png is too small to test incomplete
497 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), false, false, false, true);
498 check(r, "mandrill_128.png", SkISize::Make(128, 128), false, false, true, true);
499 check(r, "mandrill_16.png", SkISize::Make(16, 16), false, false, true, true);
500 check(r, "mandrill_256.png", SkISize::Make(256, 256), false, false, true, true);
501 check(r, "mandrill_32.png", SkISize::Make(32, 32), false, false, true, true);
502 check(r, "mandrill_512.png", SkISize::Make(512, 512), false, false, true, true);
503 check(r, "mandrill_64.png", SkISize::Make(64, 64), false, false, true, true);
504 check(r, "plane.png", SkISize::Make(250, 126), false, false, true, true);
505 check(r, "plane_interlaced.png", SkISize::Make(250, 126), false, false, true, true);
506 check(r, "randPixels.png", SkISize::Make(8, 8), false, false, true, true);
507 check(r, "yellow_rose.png", SkISize::Make(400, 301), false, false, true, true);
yujieqin916de9f2016-01-25 08:26:16 -0800508
509 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800510// Disable RAW tests for Win32.
511#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800512 check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
ebrauer46d2aa82016-02-17 08:04:00 -0800513 check(r, "sample_1mp_rotated.dng", SkISize::Make(600, 338), false, false, false);
yujieqin9c7a8a42016-02-05 08:21:19 -0800514 check(r, "dng_with_preview.dng", SkISize::Make(600, 338), true, false, false);
msarett02cb4d42016-01-25 11:01:34 -0800515#endif
halcanarya096d7a2015-03-27 12:16:53 -0700516}
scroggo0a7e69c2015-04-03 07:22:22 -0700517
518static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700519 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700520 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700521 REPORTER_ASSERT(r, !codec);
522
msarett3d9d7a72015-10-21 10:27:10 -0700523 SkAndroidCodec* androidCodec =
524 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
525 REPORTER_ASSERT(r, !androidCodec);
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.
554 SkCodec* codec = SkCodec::NewFromStream(nullptr);
555 REPORTER_ASSERT(r, !codec);
556
msarett3d9d7a72015-10-21 10:27:10 -0700557 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
558 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700559}
560
msarette16b04a2015-04-15 07:32:19 -0700561static void test_dimensions(skiatest::Reporter* r, const char path[]) {
562 // Create the codec from the resource file
Ben Wagner145dbcd2016-11-03 14:40:50 -0400563 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarette16b04a2015-04-15 07:32:19 -0700564 if (!stream) {
msarette16b04a2015-04-15 07:32:19 -0700565 return;
566 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400567 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarette16b04a2015-04-15 07:32:19 -0700568 if (!codec) {
569 ERRORF(r, "Unable to create codec '%s'", path);
570 return;
571 }
572
573 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800574 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700575 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700576 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700577 SkImageInfo scaledInfo = codec->getInfo()
578 .makeWH(scaledDims.width(), scaledDims.height())
579 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700580
581 // Set up for the decode
582 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
583 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
584 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
585
msarett3d9d7a72015-10-21 10:27:10 -0700586 SkAndroidCodec::AndroidOptions options;
587 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700588 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700589 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700590 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700591 }
592}
593
594// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
595DEF_TEST(Codec_Dimensions, r) {
596 // JPG
597 test_dimensions(r, "CMYK.jpg");
598 test_dimensions(r, "color_wheel.jpg");
599 test_dimensions(r, "grayscale.jpg");
600 test_dimensions(r, "mandrill_512_q075.jpg");
601 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700602
603 // Decoding small images with very large scaling factors is a potential
604 // source of bugs and crashes. We disable these tests in Gold because
605 // tiny images are not very useful to look at.
606 // Here we make sure that we do not crash or access illegal memory when
607 // performing scaled decodes on small images.
608 test_dimensions(r, "1x1.png");
609 test_dimensions(r, "2x2.png");
610 test_dimensions(r, "3x3.png");
611 test_dimensions(r, "3x1.png");
612 test_dimensions(r, "1x1.png");
613 test_dimensions(r, "16x1.png");
614 test_dimensions(r, "1x16.png");
615 test_dimensions(r, "mandrill_16.png");
616
yujieqin916de9f2016-01-25 08:26:16 -0800617 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800618// Disable RAW tests for Win32.
619#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800620 test_dimensions(r, "sample_1mp.dng");
ebrauer46d2aa82016-02-17 08:04:00 -0800621 test_dimensions(r, "sample_1mp_rotated.dng");
yujieqin9c7a8a42016-02-05 08:21:19 -0800622 test_dimensions(r, "dng_with_preview.dng");
msarett8e49ca32016-01-25 13:10:58 -0800623#endif
msarette16b04a2015-04-15 07:32:19 -0700624}
625
msarettd0375bc2015-08-12 08:08:56 -0700626static void test_invalid(skiatest::Reporter* r, const char path[]) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400627 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett4b17fa32015-04-23 08:53:39 -0700628 if (!stream) {
msarett4b17fa32015-04-23 08:53:39 -0700629 return;
630 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400631 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
halcanary96fcdcc2015-08-27 07:41:13 -0700632 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700633}
msarette16b04a2015-04-15 07:32:19 -0700634
msarett4b17fa32015-04-23 08:53:39 -0700635DEF_TEST(Codec_Empty, r) {
636 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700637 test_invalid(r, "empty_images/zero-dims.gif");
638 test_invalid(r, "empty_images/zero-embedded.ico");
639 test_invalid(r, "empty_images/zero-width.bmp");
640 test_invalid(r, "empty_images/zero-height.bmp");
641 test_invalid(r, "empty_images/zero-width.jpg");
642 test_invalid(r, "empty_images/zero-height.jpg");
643 test_invalid(r, "empty_images/zero-width.png");
644 test_invalid(r, "empty_images/zero-height.png");
645 test_invalid(r, "empty_images/zero-width.wbmp");
646 test_invalid(r, "empty_images/zero-height.wbmp");
647 // This image is an ico with an embedded mask-bmp. This is illegal.
648 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700649}
msarett99f567e2015-08-05 12:58:26 -0700650
651static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400652 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett99f567e2015-08-05 12:58:26 -0700653 if (!stream) {
msarett99f567e2015-08-05 12:58:26 -0700654 return;
655 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400656 std::unique_ptr<SkCodec> decoder(SkCodec::NewFromStream(stream.release()));
scroggo8e6c7ad2016-09-16 08:20:38 -0700657 if (!decoder) {
658 SkDebugf("Missing codec for %s\n", path);
659 return;
660 }
661
662 const SkImageInfo info = decoder->getInfo().makeColorType(kIndex_8_SkColorType);
halcanary9d524f22016-03-29 09:03:52 -0700663
msarett99f567e2015-08-05 12:58:26 -0700664 // This should return kSuccess because kIndex8 is supported.
665 SkPMColor colorStorage[256];
666 int colorCount;
scroggo8e6c7ad2016-09-16 08:20:38 -0700667 SkCodec::Result result = decoder->startScanlineDecode(info, nullptr, colorStorage,
668 &colorCount);
669 if (SkCodec::kSuccess == result) {
670 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
671 // colorPtr and a valid colorCountPtr.
672 result = decoder->startScanlineDecode(info, nullptr, nullptr, nullptr);
673 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
674 result = decoder->startScanlineDecode(info);
675 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
676 } else if (SkCodec::kUnimplemented == result) {
677 // New method should be supported:
678 SkBitmap bm;
679 sk_sp<SkColorTable> colorTable(new SkColorTable(colorStorage, 256));
680 bm.allocPixels(info, nullptr, colorTable.get());
681 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes(), nullptr,
682 colorStorage, &colorCount);
683 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
684 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
685 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
686 } else {
687 // The test is uninteresting if kIndex8 is not supported
688 ERRORF(r, "Should not call test_invalid_parameters for non-Index8 file: %s\n", path);
msarett99f567e2015-08-05 12:58:26 -0700689 return;
690 }
691
msarett99f567e2015-08-05 12:58:26 -0700692}
693
694DEF_TEST(Codec_Params, r) {
695 test_invalid_parameters(r, "index8.png");
696 test_invalid_parameters(r, "mandrill.wbmp");
697}
scroggocf98fa92015-11-23 08:14:40 -0800698
scroggo8e6c7ad2016-09-16 08:20:38 -0700699#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
700
701#ifndef SK_PNG_DISABLE_TESTS // reading chunks does not work properly with older versions.
702 // It does not appear that anyone in Google3 is reading chunks.
703
scroggocf98fa92015-11-23 08:14:40 -0800704static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
705 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
706 if (!sk_stream->write(data, len)) {
707 png_error(png_ptr, "sk_write_fn Error!");
708 }
709}
710
scroggocf98fa92015-11-23 08:14:40 -0800711DEF_TEST(Codec_pngChunkReader, r) {
712 // Create a dummy bitmap. Use unpremul RGBA for libpng.
713 SkBitmap bm;
714 const int w = 1;
715 const int h = 1;
716 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
717 kUnpremul_SkAlphaType);
718 bm.setInfo(bmInfo);
719 bm.allocPixels();
720 bm.eraseColor(SK_ColorBLUE);
721 SkMD5::Digest goodDigest;
722 md5(bm, &goodDigest);
723
724 // Write to a png file.
725 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
726 REPORTER_ASSERT(r, png);
727 if (!png) {
728 return;
729 }
730
731 png_infop info = png_create_info_struct(png);
732 REPORTER_ASSERT(r, info);
733 if (!info) {
734 png_destroy_write_struct(&png, nullptr);
735 return;
736 }
737
738 if (setjmp(png_jmpbuf(png))) {
739 ERRORF(r, "failed writing png");
740 png_destroy_write_struct(&png, &info);
741 return;
742 }
743
744 SkDynamicMemoryWStream wStream;
745 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
746
747 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
748 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
749 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
750
751 // Create some chunks that match the Android framework's use.
752 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800753 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
754 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
755 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800756 };
757
758 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
759 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
760#if PNG_LIBPNG_VER < 10600
761 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800762 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
763 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800764#endif
765
766 png_write_info(png, info);
767
768 for (int j = 0; j < h; j++) {
769 png_bytep row = (png_bytep)(bm.getAddr(0, j));
770 png_write_rows(png, &row, 1);
771 }
772 png_write_end(png, info);
773 png_destroy_write_struct(&png, &info);
774
775 class ChunkReader : public SkPngChunkReader {
776 public:
777 ChunkReader(skiatest::Reporter* r)
778 : fReporter(r)
779 {
780 this->reset();
781 }
782
783 bool readChunk(const char tag[], const void* data, size_t length) override {
784 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
785 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
786 // Tag matches. This should have been the first time we see it.
787 REPORTER_ASSERT(fReporter, !fSeen[i]);
788 fSeen[i] = true;
789
790 // Data and length should match
791 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
792 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
793 (const char*) gUnknowns[i].data));
794 return true;
795 }
796 }
797 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
798 return true;
799 }
800
801 bool allHaveBeenSeen() {
802 bool ret = true;
803 for (auto seen : fSeen) {
804 ret &= seen;
805 }
806 return ret;
807 }
808
809 void reset() {
810 sk_bzero(fSeen, sizeof(fSeen));
811 }
812
813 private:
814 skiatest::Reporter* fReporter; // Unowned
815 bool fSeen[3];
816 };
817
818 ChunkReader chunkReader(r);
819
820 // Now read the file with SkCodec.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400821 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(wStream.detachAsData(), &chunkReader));
scroggocf98fa92015-11-23 08:14:40 -0800822 REPORTER_ASSERT(r, codec);
823 if (!codec) {
824 return;
825 }
826
827 // Now compare to the original.
828 SkBitmap decodedBm;
829 decodedBm.setInfo(codec->getInfo());
830 decodedBm.allocPixels();
831 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
832 decodedBm.rowBytes());
833 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
834
835 if (decodedBm.colorType() != bm.colorType()) {
836 SkBitmap tmp;
837 bool success = decodedBm.copyTo(&tmp, bm.colorType());
838 REPORTER_ASSERT(r, success);
839 if (!success) {
840 return;
841 }
842
843 tmp.swap(decodedBm);
844 }
845
846 compare_to_good_digest(r, goodDigest, decodedBm);
847 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
848
849 // Decoding again will read the chunks again.
850 chunkReader.reset();
851 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
852 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
853 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
854 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
855}
scroggo8e6c7ad2016-09-16 08:20:38 -0700856#endif // SK_PNG_DISABLE_TESTS
scroggocf98fa92015-11-23 08:14:40 -0800857#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800858
scroggodb30be22015-12-08 18:54:13 -0800859// Stream that can only peek up to a limit
860class LimitedPeekingMemStream : public SkStream {
861public:
reed42943c82016-09-12 12:01:44 -0700862 LimitedPeekingMemStream(sk_sp<SkData> data, size_t limit)
863 : fStream(std::move(data))
scroggodb30be22015-12-08 18:54:13 -0800864 , fLimit(limit) {}
865
866 size_t peek(void* buf, size_t bytes) const override {
867 return fStream.peek(buf, SkTMin(bytes, fLimit));
868 }
869 size_t read(void* buf, size_t bytes) override {
870 return fStream.read(buf, bytes);
871 }
872 bool rewind() override {
873 return fStream.rewind();
874 }
875 bool isAtEnd() const override {
msarettff2a6c82016-09-07 11:23:28 -0700876 return fStream.isAtEnd();
scroggodb30be22015-12-08 18:54:13 -0800877 }
878private:
879 SkMemoryStream fStream;
880 const size_t fLimit;
881};
882
yujieqin9c7a8a42016-02-05 08:21:19 -0800883// Stream that is not an asset stream (!hasPosition() or !hasLength())
884class NotAssetMemStream : public SkStream {
885public:
bungeman38d909e2016-08-02 14:40:46 -0700886 NotAssetMemStream(sk_sp<SkData> data) : fStream(std::move(data)) {}
yujieqin9c7a8a42016-02-05 08:21:19 -0800887
888 bool hasPosition() const override {
889 return false;
890 }
891
892 bool hasLength() const override {
893 return false;
894 }
895
896 size_t peek(void* buf, size_t bytes) const override {
897 return fStream.peek(buf, bytes);
898 }
899 size_t read(void* buf, size_t bytes) override {
900 return fStream.read(buf, bytes);
901 }
902 bool rewind() override {
903 return fStream.rewind();
904 }
905 bool isAtEnd() const override {
906 return fStream.isAtEnd();
907 }
908private:
909 SkMemoryStream fStream;
910};
911
yujieqinf236ee42016-02-29 07:14:42 -0800912// Disable RAW tests for Win32.
913#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin9c7a8a42016-02-05 08:21:19 -0800914// Test that the RawCodec works also for not asset stream. This will test the code path using
915// SkRawBufferedStream instead of SkRawAssetStream.
yujieqin9c7a8a42016-02-05 08:21:19 -0800916DEF_TEST(Codec_raw_notseekable, r) {
917 const char* path = "dng_with_preview.dng";
918 SkString fullPath(GetResourcePath(path));
bungeman38d909e2016-08-02 14:40:46 -0700919 sk_sp<SkData> data(SkData::MakeFromFileName(fullPath.c_str()));
yujieqin9c7a8a42016-02-05 08:21:19 -0800920 if (!data) {
921 SkDebugf("Missing resource '%s'\n", path);
922 return;
923 }
924
Ben Wagner145dbcd2016-11-03 14:40:50 -0400925 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(std::move(data))));
yujieqin9c7a8a42016-02-05 08:21:19 -0800926 REPORTER_ASSERT(r, codec);
927
928 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
929}
930#endif
931
scroggodb30be22015-12-08 18:54:13 -0800932// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
933// + rewind() and succeed.
934DEF_TEST(Codec_webp_peek, r) {
935 const char* path = "baby_tux.webp";
936 SkString fullPath(GetResourcePath(path));
reedfde05112016-03-11 13:02:28 -0800937 auto data = SkData::MakeFromFileName(fullPath.c_str());
scroggodb30be22015-12-08 18:54:13 -0800938 if (!data) {
939 SkDebugf("Missing resource '%s'\n", path);
940 return;
941 }
942
943 // The limit is less than webp needs to peek or read.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400944 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(
reed42943c82016-09-12 12:01:44 -0700945 new LimitedPeekingMemStream(data, 25)));
scroggodb30be22015-12-08 18:54:13 -0800946 REPORTER_ASSERT(r, codec);
947
scroggo7b5e5532016-02-04 06:14:24 -0800948 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800949
950 // Similarly, a stream which does not peek should still succeed.
reed42943c82016-09-12 12:01:44 -0700951 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0)));
scroggodb30be22015-12-08 18:54:13 -0800952 REPORTER_ASSERT(r, codec);
953
scroggo7b5e5532016-02-04 06:14:24 -0800954 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800955}
956
msarett7f7ec202016-03-01 12:12:27 -0800957// SkCodec's wbmp decoder was initially unnecessarily restrictive.
958// It required the second byte to be zero. The wbmp specification allows
959// a couple of bits to be 1 (so long as they do not overlap with 0x9F).
960// Test that SkCodec now supports an image with these bits set.
scroggob9a1e342015-11-30 06:25:31 -0800961DEF_TEST(Codec_wbmp, r) {
962 const char* path = "mandrill.wbmp";
Ben Wagner145dbcd2016-11-03 14:40:50 -0400963 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
scroggob9a1e342015-11-30 06:25:31 -0800964 if (!stream) {
scroggob9a1e342015-11-30 06:25:31 -0800965 return;
966 }
967
968 // Modify the stream to contain a second byte with some bits set.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400969 auto data = SkCopyStreamToData(stream.get());
scroggob9a1e342015-11-30 06:25:31 -0800970 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
971 writeableData[1] = static_cast<uint8_t>(~0x9F);
972
msarett7f7ec202016-03-01 12:12:27 -0800973 // SkCodec should support this.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400974 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
scroggob9a1e342015-11-30 06:25:31 -0800975 REPORTER_ASSERT(r, codec);
976 if (!codec) {
977 return;
978 }
scroggo7b5e5532016-02-04 06:14:24 -0800979 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800980}
scroggodb30be22015-12-08 18:54:13 -0800981
982// wbmp images have a header that can be arbitrarily large, depending on the
983// size of the image. We cap the size at 65535, meaning we only need to look at
984// 8 bytes to determine whether we can read the image. This is important
985// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
986// image is a wbmp.
987DEF_TEST(Codec_wbmp_max_size, r) {
988 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
989 0x83, 0xFF, 0x7F, // W: 65535
990 0x83, 0xFF, 0x7F }; // H: 65535
Ben Wagner145dbcd2016-11-03 14:40:50 -0400991 std::unique_ptr<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
992 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -0800993
994 REPORTER_ASSERT(r, codec);
995 if (!codec) return;
996
997 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
998 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
999
1000 // Now test an image which is too big. Any image with a larger header (i.e.
1001 // has bigger width/height) is also too big.
1002 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
1003 0x84, 0x80, 0x00, // W: 65536
1004 0x84, 0x80, 0x00 }; // H: 65536
1005 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
mtklein18300a32016-03-16 13:53:35 -07001006 codec.reset(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -08001007
1008 REPORTER_ASSERT(r, !codec);
1009}
msarett2812f032016-07-18 15:56:08 -07001010
1011DEF_TEST(Codec_jpeg_rewind, r) {
1012 const char* path = "mandrill_512_q075.jpg";
Ben Wagner145dbcd2016-11-03 14:40:50 -04001013 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarett2812f032016-07-18 15:56:08 -07001014 if (!stream) {
msarett2812f032016-07-18 15:56:08 -07001015 return;
1016 }
Ben Wagner145dbcd2016-11-03 14:40:50 -04001017 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarett2812f032016-07-18 15:56:08 -07001018 if (!codec) {
1019 ERRORF(r, "Unable to create codec '%s'.", path);
1020 return;
1021 }
1022
1023 const int width = codec->getInfo().width();
1024 const int height = codec->getInfo().height();
1025 size_t rowBytes = sizeof(SkPMColor) * width;
1026 SkAutoMalloc pixelStorage(height * rowBytes);
1027
1028 // Perform a sampled decode.
1029 SkAndroidCodec::AndroidOptions opts;
1030 opts.fSampleSize = 12;
1031 codec->getAndroidPixels(codec->getInfo().makeWH(width / 12, height / 12), pixelStorage.get(),
1032 rowBytes, &opts);
1033
1034 // Rewind the codec and perform a full image decode.
1035 SkCodec::Result result = codec->getPixels(codec->getInfo(), pixelStorage.get(), rowBytes);
1036 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1037}
msarett549ca322016-08-17 08:54:08 -07001038
msarett35bb74b2016-08-22 07:41:28 -07001039static void check_color_xform(skiatest::Reporter* r, const char* path) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001040 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(GetResourceAsStream(path)));
msarett35bb74b2016-08-22 07:41:28 -07001041
1042 SkAndroidCodec::AndroidOptions opts;
1043 opts.fSampleSize = 3;
1044 const int subsetWidth = codec->getInfo().width() / 2;
1045 const int subsetHeight = codec->getInfo().height() / 2;
1046 SkIRect subset = SkIRect::MakeWH(subsetWidth, subsetHeight);
1047 opts.fSubset = &subset;
1048
1049 const int dstWidth = subsetWidth / opts.fSampleSize;
1050 const int dstHeight = subsetHeight / opts.fSampleSize;
1051 sk_sp<SkData> data = SkData::MakeFromFileName(
1052 GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str());
Brian Osman526972e2016-10-24 09:24:02 -04001053 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeICC(data->data(), data->size());
msarett35bb74b2016-08-22 07:41:28 -07001054 SkImageInfo dstInfo = codec->getInfo().makeWH(dstWidth, dstHeight)
1055 .makeColorType(kN32_SkColorType)
1056 .makeColorSpace(colorSpace);
1057
1058 size_t rowBytes = dstInfo.minRowBytes();
1059 SkAutoMalloc pixelStorage(dstInfo.getSafeSize(rowBytes));
1060 SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get(), rowBytes, &opts);
1061 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1062}
1063
1064DEF_TEST(Codec_ColorXform, r) {
1065 check_color_xform(r, "mandrill_512_q075.jpg");
1066 check_color_xform(r, "mandrill_512.png");
1067}
1068
msarettf17b71f2016-09-12 14:30:03 -07001069static bool color_type_match(SkColorType origColorType, SkColorType codecColorType) {
1070 switch (origColorType) {
1071 case kRGBA_8888_SkColorType:
1072 case kBGRA_8888_SkColorType:
1073 return kRGBA_8888_SkColorType == codecColorType ||
1074 kBGRA_8888_SkColorType == codecColorType;
1075 default:
1076 return origColorType == codecColorType;
1077 }
1078}
1079
1080static bool alpha_type_match(SkAlphaType origAlphaType, SkAlphaType codecAlphaType) {
1081 switch (origAlphaType) {
1082 case kUnpremul_SkAlphaType:
1083 case kPremul_SkAlphaType:
1084 return kUnpremul_SkAlphaType == codecAlphaType ||
1085 kPremul_SkAlphaType == codecAlphaType;
1086 default:
1087 return origAlphaType == codecAlphaType;
1088 }
1089}
1090
1091static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const SkImageInfo& info) {
1092 SkBitmap bm1;
1093 SkPMColor colors[256];
1094 SkAutoTUnref<SkColorTable> colorTable1(new SkColorTable(colors, 256));
1095 bm1.allocPixels(info, nullptr, colorTable1.get());
1096 int numColors;
1097 SkCodec::Result result = origCodec->getPixels(info, bm1.getPixels(), bm1.rowBytes(), nullptr,
1098 const_cast<SkPMColor*>(colorTable1->readColors()),
1099 &numColors);
1100 // This will fail to update colorTable1->count() but is fine for the purpose of this test.
1101 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarett9b09cd82016-08-29 14:47:49 -07001102
1103 // Encode the image to png.
1104 sk_sp<SkData> data =
1105 sk_sp<SkData>(SkImageEncoder::EncodeData(bm1, SkImageEncoder::kPNG_Type, 100));
1106
Ben Wagner145dbcd2016-11-03 14:40:50 -04001107 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
msarettf17b71f2016-09-12 14:30:03 -07001108 REPORTER_ASSERT(r, color_type_match(info.colorType(), codec->getInfo().colorType()));
1109 REPORTER_ASSERT(r, alpha_type_match(info.alphaType(), codec->getInfo().alphaType()));
msarett9b09cd82016-08-29 14:47:49 -07001110
1111 SkBitmap bm2;
msarettf17b71f2016-09-12 14:30:03 -07001112 SkAutoTUnref<SkColorTable> colorTable2(new SkColorTable(colors, 256));
1113 bm2.allocPixels(info, nullptr, colorTable2.get());
1114 result = codec->getPixels(info, bm2.getPixels(), bm2.rowBytes(), nullptr,
1115 const_cast<SkPMColor*>(colorTable2->readColors()), &numColors);
msarett9b09cd82016-08-29 14:47:49 -07001116 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1117
1118 SkMD5::Digest d1, d2;
1119 md5(bm1, &d1);
1120 md5(bm2, &d2);
1121 REPORTER_ASSERT(r, d1 == d2);
1122}
1123
1124DEF_TEST(Codec_PngRoundTrip, r) {
msarett549ca322016-08-17 08:54:08 -07001125 const char* path = "mandrill_512_q075.jpg";
Ben Wagner145dbcd2016-11-03 14:40:50 -04001126 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
1127 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett549ca322016-08-17 08:54:08 -07001128
msarettf17b71f2016-09-12 14:30:03 -07001129 SkColorType colorTypesOpaque[] = {
1130 kRGB_565_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1131 };
1132 for (SkColorType colorType : colorTypesOpaque) {
1133 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType);
1134 check_round_trip(r, codec.get(), newInfo);
1135 }
1136
msarett9b09cd82016-08-29 14:47:49 -07001137 path = "grayscale.jpg";
bungemanf93d7112016-09-16 06:24:20 -07001138 stream.reset(GetResourceAsStream(path));
msarett9b09cd82016-08-29 14:47:49 -07001139 codec.reset(SkCodec::NewFromStream(stream.release()));
msarettf17b71f2016-09-12 14:30:03 -07001140 check_round_trip(r, codec.get(), codec->getInfo());
1141
1142 path = "yellow_rose.png";
bungemanf93d7112016-09-16 06:24:20 -07001143 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001144 codec.reset(SkCodec::NewFromStream(stream.release()));
1145
1146 SkColorType colorTypesWithAlpha[] = {
1147 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1148 };
1149 SkAlphaType alphaTypes[] = {
1150 kUnpremul_SkAlphaType, kPremul_SkAlphaType
1151 };
1152 for (SkColorType colorType : colorTypesWithAlpha) {
1153 for (SkAlphaType alphaType : alphaTypes) {
1154 // Set color space to nullptr because color correct premultiplies do not round trip.
1155 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType)
1156 .makeAlphaType(alphaType)
1157 .makeColorSpace(nullptr);
1158 check_round_trip(r, codec.get(), newInfo);
1159 }
1160 }
1161
1162 path = "index8.png";
bungemanf93d7112016-09-16 06:24:20 -07001163 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001164 codec.reset(SkCodec::NewFromStream(stream.release()));
1165
1166 for (SkAlphaType alphaType : alphaTypes) {
1167 SkImageInfo newInfo = codec->getInfo().makeAlphaType(alphaType)
1168 .makeColorSpace(nullptr);
1169 check_round_trip(r, codec.get(), newInfo);
1170 }
msarett549ca322016-08-17 08:54:08 -07001171}
msarett2ecc35f2016-09-08 11:55:16 -07001172
1173static void test_conversion_possible(skiatest::Reporter* r, const char* path,
scroggo8e6c7ad2016-09-16 08:20:38 -07001174 bool supportsScanlineDecoder,
1175 bool supportsIncrementalDecoder) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001176 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
1177 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett2ecc35f2016-09-08 11:55:16 -07001178 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType);
1179
1180 SkBitmap bm;
1181 bm.allocPixels(infoF16);
1182 SkCodec::Result result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1183 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001184
1185 result = codec->startScanlineDecode(infoF16);
1186 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001187 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001188 } else {
1189 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1190 }
1191
1192 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1193 if (supportsIncrementalDecoder) {
1194 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
1195 } else {
1196 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001197 }
1198
raftias94888332016-10-18 10:02:51 -07001199 SkASSERT(SkColorSpace_Base::Type::kXYZ == as_CSB(infoF16.colorSpace())->type());
1200 SkColorSpace_XYZ* csXYZ = static_cast<SkColorSpace_XYZ*>(infoF16.colorSpace());
1201 infoF16 = infoF16.makeColorSpace(csXYZ->makeLinearGamma());
msarett2ecc35f2016-09-08 11:55:16 -07001202 result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1203 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001204 result = codec->startScanlineDecode(infoF16);
1205 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001206 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001207 } else {
1208 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1209 }
1210
1211 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1212 if (supportsIncrementalDecoder) {
1213 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1214 } else {
1215 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001216 }
1217}
1218
1219DEF_TEST(Codec_F16ConversionPossible, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -07001220 test_conversion_possible(r, "color_wheel.webp", false, false);
1221 test_conversion_possible(r, "mandrill_512_q075.jpg", true, false);
1222 test_conversion_possible(r, "yellow_rose.png", false, true);
1223}
1224
scroggo19b91532016-10-24 09:03:26 -07001225static void decode_frame(skiatest::Reporter* r, SkCodec* codec, size_t frame) {
1226 SkBitmap bm;
1227 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1228 bm.allocPixels(info);
1229
1230 SkCodec::Options opts;
1231 opts.fFrameIndex = frame;
1232 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->getPixels(info,
1233 bm.getPixels(), bm.rowBytes(), &opts, nullptr, nullptr));
1234}
1235
1236// For an animated image, we should only read enough to decode the requested
1237// frame if the client never calls getFrameInfo.
1238DEF_TEST(Codec_skipFullParse, r) {
1239 auto path = "test640x479.gif";
1240 SkStream* stream(GetResourceAsStream(path));
1241 if (!stream) {
1242 return;
1243 }
1244
1245 // Note that we cheat and hold on to the stream pointer, but SkCodec will
1246 // take ownership. We will not refer to the stream after the SkCodec
1247 // deletes it.
1248 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
1249 if (!codec) {
1250 ERRORF(r, "Failed to create codec for %s", path);
1251 return;
1252 }
1253
1254 REPORTER_ASSERT(r, stream->hasPosition());
1255 const size_t sizePosition = stream->getPosition();
1256 REPORTER_ASSERT(r, stream->hasLength() && sizePosition < stream->getLength());
1257
1258 // This should read more of the stream, but not the whole stream.
1259 decode_frame(r, codec.get(), 0);
1260 const size_t positionAfterFirstFrame = stream->getPosition();
1261 REPORTER_ASSERT(r, positionAfterFirstFrame > sizePosition
1262 && positionAfterFirstFrame < stream->getLength());
1263
1264 // Again, this should read more of the stream.
1265 decode_frame(r, codec.get(), 2);
1266 const size_t positionAfterThirdFrame = stream->getPosition();
1267 REPORTER_ASSERT(r, positionAfterThirdFrame > positionAfterFirstFrame
1268 && positionAfterThirdFrame < stream->getLength());
1269
1270 // This does not need to read any more of the stream, since it has already
1271 // parsed the second frame.
1272 decode_frame(r, codec.get(), 1);
1273 REPORTER_ASSERT(r, stream->getPosition() == positionAfterThirdFrame);
1274
1275 // This should read the rest of the frames.
1276 decode_frame(r, codec.get(), 3);
1277 const size_t finalPosition = stream->getPosition();
1278 REPORTER_ASSERT(r, finalPosition > positionAfterThirdFrame);
1279
1280 // There may be more data in the stream.
1281 auto frameInfo = codec->getFrameInfo();
1282 REPORTER_ASSERT(r, frameInfo.size() == 4);
1283 REPORTER_ASSERT(r, stream->getPosition() >= finalPosition);
1284}
1285
scroggo8e6c7ad2016-09-16 08:20:38 -07001286// Only rewinds up to a limit.
1287class LimitedRewindingStream : public SkStream {
1288public:
1289 static SkStream* Make(const char path[], size_t limit) {
1290 SkStream* stream = GetResourceAsStream(path);
1291 if (!stream) {
1292 return nullptr;
1293 }
1294 return new LimitedRewindingStream(stream, limit);
1295 }
1296
1297 size_t read(void* buffer, size_t size) override {
1298 const size_t bytes = fStream->read(buffer, size);
1299 fPosition += bytes;
1300 return bytes;
1301 }
1302
1303 bool isAtEnd() const override {
1304 return fStream->isAtEnd();
1305 }
1306
1307 bool rewind() override {
1308 if (fPosition <= fLimit && fStream->rewind()) {
1309 fPosition = 0;
1310 return true;
1311 }
1312
1313 return false;
1314 }
1315
1316private:
Ben Wagner145dbcd2016-11-03 14:40:50 -04001317 std::unique_ptr<SkStream> fStream;
1318 const size_t fLimit;
1319 size_t fPosition;
scroggo8e6c7ad2016-09-16 08:20:38 -07001320
1321 LimitedRewindingStream(SkStream* stream, size_t limit)
1322 : fStream(stream)
1323 , fLimit(limit)
1324 , fPosition(0)
1325 {
1326 SkASSERT(fStream);
1327 }
1328};
1329
1330DEF_TEST(Codec_fallBack, r) {
1331 // SkAndroidCodec needs to be able to fall back to scanline decoding
1332 // if incremental decoding does not work. Make sure this does not
1333 // require a rewind.
1334
1335 // Formats that currently do not support incremental decoding
1336 auto files = {
scroggo8e6c7ad2016-09-16 08:20:38 -07001337 "CMYK.jpg",
1338 "color_wheel.ico",
1339 "mandrill.wbmp",
1340 "randPixels.bmp",
1341 };
1342 for (auto file : files) {
1343 SkStream* stream = LimitedRewindingStream::Make(file, 14);
1344 if (!stream) {
1345 SkDebugf("Missing resources (%s). Set --resourcePath.\n", file);
1346 return;
1347 }
1348
Ben Wagner145dbcd2016-11-03 14:40:50 -04001349 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream));
scroggo8e6c7ad2016-09-16 08:20:38 -07001350 if (!codec) {
1351 ERRORF(r, "Failed to create codec for %s,", file);
1352 continue;
1353 }
1354
1355 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
1356 SkBitmap bm;
1357 bm.allocPixels(info);
1358
1359 if (SkCodec::kUnimplemented != codec->startIncrementalDecode(info, bm.getPixels(),
1360 bm.rowBytes())) {
1361 ERRORF(r, "Is scanline decoding now implemented for %s?", file);
1362 continue;
1363 }
1364
1365 // Scanline decoding should not require a rewind.
1366 SkCodec::Result result = codec->startScanlineDecode(info);
1367 if (SkCodec::kSuccess != result) {
1368 ERRORF(r, "Scanline decoding failed for %s with %i", file, result);
1369 }
1370 }
msarett2ecc35f2016-09-08 11:55:16 -07001371}
scroggoc46cdd42016-10-10 06:45:32 -07001372
1373// This test verifies that we fixed an assert statement that fired when reusing a png codec
1374// after scaling.
1375DEF_TEST(Codec_reusePng, r) {
1376 std::unique_ptr<SkStream> stream(GetResourceAsStream("plane.png"));
1377 if (!stream) {
1378 return;
1379 }
1380
1381 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
1382 if (!codec) {
1383 ERRORF(r, "Failed to create codec\n");
1384 return;
1385 }
1386
1387 SkAndroidCodec::AndroidOptions opts;
1388 opts.fSampleSize = 5;
1389 auto size = codec->getSampledDimensions(opts.fSampleSize);
1390 auto info = codec->getInfo().makeWH(size.fWidth, size.fHeight).makeColorType(kN32_SkColorType);
1391 SkBitmap bm;
1392 bm.allocPixels(info);
1393 auto result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1394 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1395
1396 info = codec->getInfo().makeColorType(kN32_SkColorType);
1397 bm.allocPixels(info);
1398 opts.fSampleSize = 1;
1399 result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1400 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1401}
scroggoe61b3b42016-10-10 07:17:32 -07001402
1403DEF_TEST(Codec_rowsDecoded, r) {
1404 auto file = "plane_interlaced.png";
1405 std::unique_ptr<SkStream> stream(GetResourceAsStream(file));
1406 if (!stream) {
1407 return;
1408 }
1409
1410 // This is enough to read the header etc, but no rows.
1411 auto data = SkData::MakeFromStream(stream.get(), 99);
1412 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
1413 if (!codec) {
1414 ERRORF(r, "Failed to create codec\n");
1415 return;
1416 }
1417
1418 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1419 SkBitmap bm;
1420 bm.allocPixels(info);
1421 auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
1422 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1423
1424 // This is an arbitrary value. The important fact is that it is not zero, and rowsDecoded
1425 // should get set to zero by incrementalDecode.
1426 int rowsDecoded = 77;
1427 result = codec->incrementalDecode(&rowsDecoded);
1428 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput);
1429 REPORTER_ASSERT(r, rowsDecoded == 0);
1430}
Matt Sarett29121eb2016-10-17 14:32:46 -04001431
Matt Sarett8a4e9c52016-10-25 14:24:50 -04001432static void test_invalid_images(skiatest::Reporter* r, const char* path, bool shouldSucceed) {
Matt Sarett29121eb2016-10-17 14:32:46 -04001433 SkBitmap bitmap;
Matt Sarett8a4e9c52016-10-25 14:24:50 -04001434 const bool success = GetResourceAsBitmap(path, &bitmap);
1435 REPORTER_ASSERT(r, success == shouldSucceed);
1436}
1437
1438DEF_TEST(Codec_InvalidImages, r) {
1439 // ASAN will complain if there is an issue.
1440 test_invalid_images(r, "invalid_images/int_overflow.ico", false);
1441 test_invalid_images(r, "invalid_images/skbug5887.gif", true);
Matt Sarett29121eb2016-10-17 14:32:46 -04001442}