blob: a6058a9c35f75c5481ec5b49f0111aea0046ce00 [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"
raftias7c602de2016-10-13 10:45:44 -070013#include "SkColorSpace_Base.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
bungemanf93d7112016-09-16 06:24:20 -0700263 SkAutoTDelete<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
268 SkAutoTDelete<SkCodec> codec(nullptr);
269 bool isIncomplete = supportsIncomplete;
270 if (isIncomplete) {
271 size_t size = stream->getLength();
bungeman38d909e2016-08-02 14:40:46 -0700272 sk_sp<SkData> data((SkData::MakeFromStream(stream, 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) {
292 test_incremental_decode(r, codec, info, codecDigest);
293 test_in_stripes(r, codec, info, codecDigest);
294 }
295
296 // Need to call startScanlineDecode() first.
297 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) == 0);
298 REPORTER_ASSERT(r, !codec->skipScanlines(1));
scroggo46c57472015-09-30 08:57:13 -0700299 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700300 if (supportsScanlineDecoding) {
301 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700302
scroggo46c57472015-09-30 08:57:13 -0700303 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700304
scroggo58421542015-04-01 11:25:20 -0700305 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700306 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
307 if (!isIncomplete) {
308 REPORTER_ASSERT(r, 1 == lines);
309 }
scroggo58421542015-04-01 11:25:20 -0700310 }
311 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700312 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700313 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700314 }
scroggo46c57472015-09-30 08:57:13 -0700315
316 // Cannot continue to decode scanlines beyond the end
317 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700318 == 0);
scroggo46c57472015-09-30 08:57:13 -0700319
320 // Interrupting a scanline decode with a full decode starts from
321 // scratch
322 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700323 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
324 if (!isIncomplete) {
325 REPORTER_ASSERT(r, lines == 1);
326 }
scroggo46c57472015-09-30 08:57:13 -0700327 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700328 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700329 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700330 == 0);
scroggo46c57472015-09-30 08:57:13 -0700331 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700332 == 0);
msarett80803ff2015-10-16 10:54:12 -0700333
334 // Test partial scanline decodes
scroggobed1ed62016-02-11 10:24:55 -0800335 if (supports_partial_scanlines(path) && info.width() >= 3) {
msarett80803ff2015-10-16 10:54:12 -0700336 SkCodec::Options options;
337 int width = info.width();
338 int height = info.height();
339 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
340 options.fSubset = &subset;
341
342 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
343 nullptr, nullptr);
344 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
345
346 for (int y = 0; y < height; y++) {
347 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
348 if (!isIncomplete) {
349 REPORTER_ASSERT(r, 1 == lines);
350 }
351 }
352 }
scroggo58421542015-04-01 11:25:20 -0700353 } else {
scroggo46c57472015-09-30 08:57:13 -0700354 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700355 }
scroggob636b452015-07-22 07:16:20 -0700356
357 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
358 // random subsets.
359 // Do not attempt to decode subsets of an image of only once pixel, since there is no
360 // meaningful subset.
361 if (size.width() * size.height() == 1) {
362 return;
363 }
364
365 SkRandom rand;
366 SkIRect subset;
367 SkCodec::Options opts;
368 opts.fSubset = &subset;
369 for (int i = 0; i < 5; i++) {
370 subset = generate_random_subset(&rand, size.width(), size.height());
371 SkASSERT(!subset.isEmpty());
372 const bool supported = codec->getValidSubset(&subset);
373 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
374
375 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
376 SkBitmap bm;
377 bm.allocPixels(subsetInfo);
378 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700379 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700380
381 if (supportsSubsetDecoding) {
msarette6dd0042015-10-09 11:07:34 -0700382 REPORTER_ASSERT(r, result == expectedResult);
scroggob636b452015-07-22 07:16:20 -0700383 // Webp is the only codec that supports subsets, and it will have modified the subset
384 // to have even left/top.
385 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
386 } else {
387 // No subsets will work.
388 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
389 }
390 }
msarettcc7f3052015-10-05 14:20:27 -0700391
scroggobed1ed62016-02-11 10:24:55 -0800392 // SkAndroidCodec tests
scroggo8e6c7ad2016-09-16 08:20:38 -0700393 if (supportsScanlineDecoding || supportsSubsetDecoding || supportsNewScanlineDecoding) {
scroggo2c3b2182015-10-09 08:40:59 -0700394
bungemanf93d7112016-09-16 06:24:20 -0700395 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarettcc7f3052015-10-05 14:20:27 -0700396 if (!stream) {
msarettcc7f3052015-10-05 14:20:27 -0700397 return;
398 }
msarette6dd0042015-10-09 11:07:34 -0700399
scroggo7b5e5532016-02-04 06:14:24 -0800400 SkAutoTDelete<SkAndroidCodec> androidCodec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700401 if (isIncomplete) {
402 size_t size = stream->getLength();
bungeman38d909e2016-08-02 14:40:46 -0700403 sk_sp<SkData> data((SkData::MakeFromStream(stream, 2 * size / 3)));
reed42943c82016-09-12 12:01:44 -0700404 androidCodec.reset(SkAndroidCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700405 } else {
mtklein18300a32016-03-16 13:53:35 -0700406 androidCodec.reset(SkAndroidCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700407 }
scroggo7b5e5532016-02-04 06:14:24 -0800408 if (!androidCodec) {
msarettcc7f3052015-10-05 14:20:27 -0700409 ERRORF(r, "Unable to decode '%s'", path);
410 return;
411 }
412
413 SkBitmap bm;
scroggobed1ed62016-02-11 10:24:55 -0800414 SkMD5::Digest androidCodecDigest;
415 test_codec(r, androidCodec.get(), bm, info, size, expectedResult, &androidCodecDigest,
scroggo7b5e5532016-02-04 06:14:24 -0800416 &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700417 }
418
msarettedd2dcf2016-01-14 13:12:26 -0800419 if (!isIncomplete) {
scroggoef0fed32016-02-18 05:59:25 -0800420 // Test SkCodecImageGenerator
bungemanf93d7112016-09-16 06:24:20 -0700421 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
bungeman38d909e2016-08-02 14:40:46 -0700422 sk_sp<SkData> fullData(SkData::MakeFromStream(stream, stream->getLength()));
423 SkAutoTDelete<SkImageGenerator> gen(
424 SkCodecImageGenerator::NewFromEncodedCodec(fullData.get()));
msarettedd2dcf2016-01-14 13:12:26 -0800425 SkBitmap bm;
426 bm.allocPixels(info);
427 SkAutoLockPixels autoLockPixels(bm);
428 REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes()));
429 compare_to_good_digest(r, codecDigest, bm);
scroggoef0fed32016-02-18 05:59:25 -0800430
scroggo8e6c7ad2016-09-16 08:20:38 -0700431#ifndef SK_PNG_DISABLE_TESTS
scroggod8d68552016-06-06 11:26:17 -0700432 // Test using SkFrontBufferedStream, as Android does
bungeman38d909e2016-08-02 14:40:46 -0700433 SkStream* bufferedStream = SkFrontBufferedStream::Create(
434 new SkMemoryStream(std::move(fullData)), SkCodec::MinBufferedBytesNeeded());
scroggod8d68552016-06-06 11:26:17 -0700435 REPORTER_ASSERT(r, bufferedStream);
436 codec.reset(SkCodec::NewFromStream(bufferedStream));
437 REPORTER_ASSERT(r, codec);
438 if (codec) {
439 test_info(r, codec.get(), info, SkCodec::kSuccess, &codecDigest);
scroggoef0fed32016-02-18 05:59:25 -0800440 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700441#endif
msarettedd2dcf2016-01-14 13:12:26 -0800442 }
443
msarette6dd0042015-10-09 11:07:34 -0700444 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
445 if (isIncomplete) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700446 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false,
447 supportsNewScanlineDecoding);
msarettcc7f3052015-10-05 14:20:27 -0700448 }
halcanarya096d7a2015-03-27 12:16:53 -0700449}
450
451DEF_TEST(Codec, r) {
452 // WBMP
scroggo8e6c7ad2016-09-16 08:20:38 -0700453 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false, true);
halcanarya096d7a2015-03-27 12:16:53 -0700454
scroggo6f5e6192015-06-18 12:53:43 -0700455 // WEBP
scroggo8e6c7ad2016-09-16 08:20:38 -0700456 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true, true);
457 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true, true);
458 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true, true);
scroggo6f5e6192015-06-18 12:53:43 -0700459
halcanarya096d7a2015-03-27 12:16:53 -0700460 // BMP
scroggo8e6c7ad2016-09-16 08:20:38 -0700461 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false, true);
462 check(r, "rle.bmp", SkISize::Make(320, 240), true, false, true);
halcanarya096d7a2015-03-27 12:16:53 -0700463
464 // ICO
msarette6dd0042015-10-09 11:07:34 -0700465 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700466 // These two tests examine interestingly different behavior:
467 // Decodes an embedded BMP image
msarettbe8216a2015-12-04 08:00:50 -0800468 check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700469 // Decodes an embedded PNG image
scroggo8e6c7ad2016-09-16 08:20:38 -0700470 check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false, false, true);
halcanarya096d7a2015-03-27 12:16:53 -0700471
msarett438b2ad2015-04-09 12:43:10 -0700472 // GIF
msarette6dd0042015-10-09 11:07:34 -0700473 // FIXME: We are not ready to test incomplete GIFs
scroggo27c17282015-10-27 08:14:46 -0700474 check(r, "box.gif", SkISize::Make(200, 55), true, false, false);
475 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700476 // randPixels.gif is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700477 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, false);
msarett438b2ad2015-04-09 12:43:10 -0700478
msarette16b04a2015-04-15 07:32:19 -0700479 // JPG
scroggo8e6c7ad2016-09-16 08:20:38 -0700480 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false, true);
481 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700482 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700483 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggo8e6c7ad2016-09-16 08:20:38 -0700484 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700485 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700486 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
msarette16b04a2015-04-15 07:32:19 -0700487
halcanarya096d7a2015-03-27 12:16:53 -0700488 // PNG
scroggo8e6c7ad2016-09-16 08:20:38 -0700489 check(r, "arrow.png", SkISize::Make(187, 312), false, false, true, true);
490 check(r, "baby_tux.png", SkISize::Make(240, 246), false, false, true, true);
491 check(r, "color_wheel.png", SkISize::Make(128, 128), false, false, true, true);
492 // half-transparent-white-pixel.png is too small to test incomplete
493 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), false, false, false, true);
494 check(r, "mandrill_128.png", SkISize::Make(128, 128), false, false, true, true);
495 check(r, "mandrill_16.png", SkISize::Make(16, 16), false, false, true, true);
496 check(r, "mandrill_256.png", SkISize::Make(256, 256), false, false, true, true);
497 check(r, "mandrill_32.png", SkISize::Make(32, 32), false, false, true, true);
498 check(r, "mandrill_512.png", SkISize::Make(512, 512), false, false, true, true);
499 check(r, "mandrill_64.png", SkISize::Make(64, 64), false, false, true, true);
500 check(r, "plane.png", SkISize::Make(250, 126), false, false, true, true);
501 check(r, "plane_interlaced.png", SkISize::Make(250, 126), false, false, true, true);
502 check(r, "randPixels.png", SkISize::Make(8, 8), false, false, true, true);
503 check(r, "yellow_rose.png", SkISize::Make(400, 301), false, false, true, true);
yujieqin916de9f2016-01-25 08:26:16 -0800504
505 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800506// Disable RAW tests for Win32.
507#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800508 check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
ebrauer46d2aa82016-02-17 08:04:00 -0800509 check(r, "sample_1mp_rotated.dng", SkISize::Make(600, 338), false, false, false);
yujieqin9c7a8a42016-02-05 08:21:19 -0800510 check(r, "dng_with_preview.dng", SkISize::Make(600, 338), true, false, false);
msarett02cb4d42016-01-25 11:01:34 -0800511#endif
halcanarya096d7a2015-03-27 12:16:53 -0700512}
scroggo0a7e69c2015-04-03 07:22:22 -0700513
514static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700515 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700516 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700517 REPORTER_ASSERT(r, !codec);
518
msarett3d9d7a72015-10-21 10:27:10 -0700519 SkAndroidCodec* androidCodec =
520 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
521 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700522}
523
524// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
525// even on failure. Test some bad streams.
526DEF_TEST(Codec_leaks, r) {
527 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
528 const char nonSupportedStream[] = "hello world";
529 // The other strings should look like the beginning of a file type, so we'll call some
530 // internal version of NewFromStream, which must also delete the stream on failure.
531 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
532 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
533 const char emptyWebp[] = "RIFF1234WEBPVP";
534 const char emptyBmp[] = { 'B', 'M' };
535 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
536 const char emptyGif[] = "GIFVER";
537
538 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
539 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
540 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
541 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
542 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
543 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
544 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
545}
msarette16b04a2015-04-15 07:32:19 -0700546
scroggo2c3b2182015-10-09 08:40:59 -0700547DEF_TEST(Codec_null, r) {
scroggobed1ed62016-02-11 10:24:55 -0800548 // Attempting to create an SkCodec or an SkAndroidCodec with null should not
scroggo2c3b2182015-10-09 08:40:59 -0700549 // crash.
550 SkCodec* codec = SkCodec::NewFromStream(nullptr);
551 REPORTER_ASSERT(r, !codec);
552
msarett3d9d7a72015-10-21 10:27:10 -0700553 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
554 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700555}
556
msarette16b04a2015-04-15 07:32:19 -0700557static void test_dimensions(skiatest::Reporter* r, const char path[]) {
558 // Create the codec from the resource file
bungemanf93d7112016-09-16 06:24:20 -0700559 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarette16b04a2015-04-15 07:32:19 -0700560 if (!stream) {
msarette16b04a2015-04-15 07:32:19 -0700561 return;
562 }
mtklein18300a32016-03-16 13:53:35 -0700563 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarette16b04a2015-04-15 07:32:19 -0700564 if (!codec) {
565 ERRORF(r, "Unable to create codec '%s'", path);
566 return;
567 }
568
569 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800570 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700571 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700572 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700573 SkImageInfo scaledInfo = codec->getInfo()
574 .makeWH(scaledDims.width(), scaledDims.height())
575 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700576
577 // Set up for the decode
578 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
579 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
580 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
581
msarett3d9d7a72015-10-21 10:27:10 -0700582 SkAndroidCodec::AndroidOptions options;
583 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700584 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700585 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700586 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700587 }
588}
589
590// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
591DEF_TEST(Codec_Dimensions, r) {
592 // JPG
593 test_dimensions(r, "CMYK.jpg");
594 test_dimensions(r, "color_wheel.jpg");
595 test_dimensions(r, "grayscale.jpg");
596 test_dimensions(r, "mandrill_512_q075.jpg");
597 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700598
599 // Decoding small images with very large scaling factors is a potential
600 // source of bugs and crashes. We disable these tests in Gold because
601 // tiny images are not very useful to look at.
602 // Here we make sure that we do not crash or access illegal memory when
603 // performing scaled decodes on small images.
604 test_dimensions(r, "1x1.png");
605 test_dimensions(r, "2x2.png");
606 test_dimensions(r, "3x3.png");
607 test_dimensions(r, "3x1.png");
608 test_dimensions(r, "1x1.png");
609 test_dimensions(r, "16x1.png");
610 test_dimensions(r, "1x16.png");
611 test_dimensions(r, "mandrill_16.png");
612
yujieqin916de9f2016-01-25 08:26:16 -0800613 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800614// Disable RAW tests for Win32.
615#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800616 test_dimensions(r, "sample_1mp.dng");
ebrauer46d2aa82016-02-17 08:04:00 -0800617 test_dimensions(r, "sample_1mp_rotated.dng");
yujieqin9c7a8a42016-02-05 08:21:19 -0800618 test_dimensions(r, "dng_with_preview.dng");
msarett8e49ca32016-01-25 13:10:58 -0800619#endif
msarette16b04a2015-04-15 07:32:19 -0700620}
621
msarettd0375bc2015-08-12 08:08:56 -0700622static void test_invalid(skiatest::Reporter* r, const char path[]) {
bungemanf93d7112016-09-16 06:24:20 -0700623 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarett4b17fa32015-04-23 08:53:39 -0700624 if (!stream) {
msarett4b17fa32015-04-23 08:53:39 -0700625 return;
626 }
mtklein18300a32016-03-16 13:53:35 -0700627 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
halcanary96fcdcc2015-08-27 07:41:13 -0700628 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700629}
msarette16b04a2015-04-15 07:32:19 -0700630
msarett4b17fa32015-04-23 08:53:39 -0700631DEF_TEST(Codec_Empty, r) {
632 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700633 test_invalid(r, "empty_images/zero-dims.gif");
634 test_invalid(r, "empty_images/zero-embedded.ico");
635 test_invalid(r, "empty_images/zero-width.bmp");
636 test_invalid(r, "empty_images/zero-height.bmp");
637 test_invalid(r, "empty_images/zero-width.jpg");
638 test_invalid(r, "empty_images/zero-height.jpg");
639 test_invalid(r, "empty_images/zero-width.png");
640 test_invalid(r, "empty_images/zero-height.png");
641 test_invalid(r, "empty_images/zero-width.wbmp");
642 test_invalid(r, "empty_images/zero-height.wbmp");
643 // This image is an ico with an embedded mask-bmp. This is illegal.
644 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700645}
msarett99f567e2015-08-05 12:58:26 -0700646
647static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
bungemanf93d7112016-09-16 06:24:20 -0700648 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarett99f567e2015-08-05 12:58:26 -0700649 if (!stream) {
msarett99f567e2015-08-05 12:58:26 -0700650 return;
651 }
mtklein18300a32016-03-16 13:53:35 -0700652 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.release()));
scroggo8e6c7ad2016-09-16 08:20:38 -0700653 if (!decoder) {
654 SkDebugf("Missing codec for %s\n", path);
655 return;
656 }
657
658 const SkImageInfo info = decoder->getInfo().makeColorType(kIndex_8_SkColorType);
halcanary9d524f22016-03-29 09:03:52 -0700659
msarett99f567e2015-08-05 12:58:26 -0700660 // This should return kSuccess because kIndex8 is supported.
661 SkPMColor colorStorage[256];
662 int colorCount;
scroggo8e6c7ad2016-09-16 08:20:38 -0700663 SkCodec::Result result = decoder->startScanlineDecode(info, nullptr, colorStorage,
664 &colorCount);
665 if (SkCodec::kSuccess == result) {
666 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
667 // colorPtr and a valid colorCountPtr.
668 result = decoder->startScanlineDecode(info, nullptr, nullptr, nullptr);
669 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
670 result = decoder->startScanlineDecode(info);
671 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
672 } else if (SkCodec::kUnimplemented == result) {
673 // New method should be supported:
674 SkBitmap bm;
675 sk_sp<SkColorTable> colorTable(new SkColorTable(colorStorage, 256));
676 bm.allocPixels(info, nullptr, colorTable.get());
677 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes(), nullptr,
678 colorStorage, &colorCount);
679 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
680 result = decoder->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
681 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
682 } else {
683 // The test is uninteresting if kIndex8 is not supported
684 ERRORF(r, "Should not call test_invalid_parameters for non-Index8 file: %s\n", path);
msarett99f567e2015-08-05 12:58:26 -0700685 return;
686 }
687
msarett99f567e2015-08-05 12:58:26 -0700688}
689
690DEF_TEST(Codec_Params, r) {
691 test_invalid_parameters(r, "index8.png");
692 test_invalid_parameters(r, "mandrill.wbmp");
693}
scroggocf98fa92015-11-23 08:14:40 -0800694
scroggo8e6c7ad2016-09-16 08:20:38 -0700695#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
696
697#ifndef SK_PNG_DISABLE_TESTS // reading chunks does not work properly with older versions.
698 // It does not appear that anyone in Google3 is reading chunks.
699
scroggocf98fa92015-11-23 08:14:40 -0800700static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
701 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
702 if (!sk_stream->write(data, len)) {
703 png_error(png_ptr, "sk_write_fn Error!");
704 }
705}
706
scroggocf98fa92015-11-23 08:14:40 -0800707DEF_TEST(Codec_pngChunkReader, r) {
708 // Create a dummy bitmap. Use unpremul RGBA for libpng.
709 SkBitmap bm;
710 const int w = 1;
711 const int h = 1;
712 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
713 kUnpremul_SkAlphaType);
714 bm.setInfo(bmInfo);
715 bm.allocPixels();
716 bm.eraseColor(SK_ColorBLUE);
717 SkMD5::Digest goodDigest;
718 md5(bm, &goodDigest);
719
720 // Write to a png file.
721 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
722 REPORTER_ASSERT(r, png);
723 if (!png) {
724 return;
725 }
726
727 png_infop info = png_create_info_struct(png);
728 REPORTER_ASSERT(r, info);
729 if (!info) {
730 png_destroy_write_struct(&png, nullptr);
731 return;
732 }
733
734 if (setjmp(png_jmpbuf(png))) {
735 ERRORF(r, "failed writing png");
736 png_destroy_write_struct(&png, &info);
737 return;
738 }
739
740 SkDynamicMemoryWStream wStream;
741 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
742
743 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
744 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
745 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
746
747 // Create some chunks that match the Android framework's use.
748 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800749 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
750 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
751 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800752 };
753
754 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
755 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
756#if PNG_LIBPNG_VER < 10600
757 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800758 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
759 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800760#endif
761
762 png_write_info(png, info);
763
764 for (int j = 0; j < h; j++) {
765 png_bytep row = (png_bytep)(bm.getAddr(0, j));
766 png_write_rows(png, &row, 1);
767 }
768 png_write_end(png, info);
769 png_destroy_write_struct(&png, &info);
770
771 class ChunkReader : public SkPngChunkReader {
772 public:
773 ChunkReader(skiatest::Reporter* r)
774 : fReporter(r)
775 {
776 this->reset();
777 }
778
779 bool readChunk(const char tag[], const void* data, size_t length) override {
780 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
781 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
782 // Tag matches. This should have been the first time we see it.
783 REPORTER_ASSERT(fReporter, !fSeen[i]);
784 fSeen[i] = true;
785
786 // Data and length should match
787 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
788 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
789 (const char*) gUnknowns[i].data));
790 return true;
791 }
792 }
793 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
794 return true;
795 }
796
797 bool allHaveBeenSeen() {
798 bool ret = true;
799 for (auto seen : fSeen) {
800 ret &= seen;
801 }
802 return ret;
803 }
804
805 void reset() {
806 sk_bzero(fSeen, sizeof(fSeen));
807 }
808
809 private:
810 skiatest::Reporter* fReporter; // Unowned
811 bool fSeen[3];
812 };
813
814 ChunkReader chunkReader(r);
815
816 // Now read the file with SkCodec.
reed42943c82016-09-12 12:01:44 -0700817 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(wStream.detachAsData(), &chunkReader));
scroggocf98fa92015-11-23 08:14:40 -0800818 REPORTER_ASSERT(r, codec);
819 if (!codec) {
820 return;
821 }
822
823 // Now compare to the original.
824 SkBitmap decodedBm;
825 decodedBm.setInfo(codec->getInfo());
826 decodedBm.allocPixels();
827 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
828 decodedBm.rowBytes());
829 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
830
831 if (decodedBm.colorType() != bm.colorType()) {
832 SkBitmap tmp;
833 bool success = decodedBm.copyTo(&tmp, bm.colorType());
834 REPORTER_ASSERT(r, success);
835 if (!success) {
836 return;
837 }
838
839 tmp.swap(decodedBm);
840 }
841
842 compare_to_good_digest(r, goodDigest, decodedBm);
843 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
844
845 // Decoding again will read the chunks again.
846 chunkReader.reset();
847 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
848 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
849 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
850 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
851}
scroggo8e6c7ad2016-09-16 08:20:38 -0700852#endif // SK_PNG_DISABLE_TESTS
scroggocf98fa92015-11-23 08:14:40 -0800853#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800854
scroggodb30be22015-12-08 18:54:13 -0800855// Stream that can only peek up to a limit
856class LimitedPeekingMemStream : public SkStream {
857public:
reed42943c82016-09-12 12:01:44 -0700858 LimitedPeekingMemStream(sk_sp<SkData> data, size_t limit)
859 : fStream(std::move(data))
scroggodb30be22015-12-08 18:54:13 -0800860 , fLimit(limit) {}
861
862 size_t peek(void* buf, size_t bytes) const override {
863 return fStream.peek(buf, SkTMin(bytes, fLimit));
864 }
865 size_t read(void* buf, size_t bytes) override {
866 return fStream.read(buf, bytes);
867 }
868 bool rewind() override {
869 return fStream.rewind();
870 }
871 bool isAtEnd() const override {
msarettff2a6c82016-09-07 11:23:28 -0700872 return fStream.isAtEnd();
scroggodb30be22015-12-08 18:54:13 -0800873 }
874private:
875 SkMemoryStream fStream;
876 const size_t fLimit;
877};
878
yujieqin9c7a8a42016-02-05 08:21:19 -0800879// Stream that is not an asset stream (!hasPosition() or !hasLength())
880class NotAssetMemStream : public SkStream {
881public:
bungeman38d909e2016-08-02 14:40:46 -0700882 NotAssetMemStream(sk_sp<SkData> data) : fStream(std::move(data)) {}
yujieqin9c7a8a42016-02-05 08:21:19 -0800883
884 bool hasPosition() const override {
885 return false;
886 }
887
888 bool hasLength() const override {
889 return false;
890 }
891
892 size_t peek(void* buf, size_t bytes) const override {
893 return fStream.peek(buf, bytes);
894 }
895 size_t read(void* buf, size_t bytes) override {
896 return fStream.read(buf, bytes);
897 }
898 bool rewind() override {
899 return fStream.rewind();
900 }
901 bool isAtEnd() const override {
902 return fStream.isAtEnd();
903 }
904private:
905 SkMemoryStream fStream;
906};
907
yujieqinf236ee42016-02-29 07:14:42 -0800908// Disable RAW tests for Win32.
909#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin9c7a8a42016-02-05 08:21:19 -0800910// Test that the RawCodec works also for not asset stream. This will test the code path using
911// SkRawBufferedStream instead of SkRawAssetStream.
yujieqin9c7a8a42016-02-05 08:21:19 -0800912DEF_TEST(Codec_raw_notseekable, r) {
913 const char* path = "dng_with_preview.dng";
914 SkString fullPath(GetResourcePath(path));
bungeman38d909e2016-08-02 14:40:46 -0700915 sk_sp<SkData> data(SkData::MakeFromFileName(fullPath.c_str()));
yujieqin9c7a8a42016-02-05 08:21:19 -0800916 if (!data) {
917 SkDebugf("Missing resource '%s'\n", path);
918 return;
919 }
920
bungeman38d909e2016-08-02 14:40:46 -0700921 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(std::move(data))));
yujieqin9c7a8a42016-02-05 08:21:19 -0800922 REPORTER_ASSERT(r, codec);
923
924 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
925}
926#endif
927
scroggodb30be22015-12-08 18:54:13 -0800928// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
929// + rewind() and succeed.
930DEF_TEST(Codec_webp_peek, r) {
931 const char* path = "baby_tux.webp";
932 SkString fullPath(GetResourcePath(path));
reedfde05112016-03-11 13:02:28 -0800933 auto data = SkData::MakeFromFileName(fullPath.c_str());
scroggodb30be22015-12-08 18:54:13 -0800934 if (!data) {
935 SkDebugf("Missing resource '%s'\n", path);
936 return;
937 }
938
939 // The limit is less than webp needs to peek or read.
reedfde05112016-03-11 13:02:28 -0800940 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(
reed42943c82016-09-12 12:01:44 -0700941 new LimitedPeekingMemStream(data, 25)));
scroggodb30be22015-12-08 18:54:13 -0800942 REPORTER_ASSERT(r, codec);
943
scroggo7b5e5532016-02-04 06:14:24 -0800944 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800945
946 // Similarly, a stream which does not peek should still succeed.
reed42943c82016-09-12 12:01:44 -0700947 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0)));
scroggodb30be22015-12-08 18:54:13 -0800948 REPORTER_ASSERT(r, codec);
949
scroggo7b5e5532016-02-04 06:14:24 -0800950 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800951}
952
msarett7f7ec202016-03-01 12:12:27 -0800953// SkCodec's wbmp decoder was initially unnecessarily restrictive.
954// It required the second byte to be zero. The wbmp specification allows
955// a couple of bits to be 1 (so long as they do not overlap with 0x9F).
956// Test that SkCodec now supports an image with these bits set.
scroggob9a1e342015-11-30 06:25:31 -0800957DEF_TEST(Codec_wbmp, r) {
958 const char* path = "mandrill.wbmp";
bungemanf93d7112016-09-16 06:24:20 -0700959 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
scroggob9a1e342015-11-30 06:25:31 -0800960 if (!stream) {
scroggob9a1e342015-11-30 06:25:31 -0800961 return;
962 }
963
964 // Modify the stream to contain a second byte with some bits set.
reedfde05112016-03-11 13:02:28 -0800965 auto data = SkCopyStreamToData(stream);
scroggob9a1e342015-11-30 06:25:31 -0800966 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
967 writeableData[1] = static_cast<uint8_t>(~0x9F);
968
msarett7f7ec202016-03-01 12:12:27 -0800969 // SkCodec should support this.
reed42943c82016-09-12 12:01:44 -0700970 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
scroggob9a1e342015-11-30 06:25:31 -0800971 REPORTER_ASSERT(r, codec);
972 if (!codec) {
973 return;
974 }
scroggo7b5e5532016-02-04 06:14:24 -0800975 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800976}
scroggodb30be22015-12-08 18:54:13 -0800977
978// wbmp images have a header that can be arbitrarily large, depending on the
979// size of the image. We cap the size at 65535, meaning we only need to look at
980// 8 bytes to determine whether we can read the image. This is important
981// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
982// image is a wbmp.
983DEF_TEST(Codec_wbmp_max_size, r) {
984 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
985 0x83, 0xFF, 0x7F, // W: 65535
986 0x83, 0xFF, 0x7F }; // H: 65535
987 SkAutoTDelete<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
mtklein18300a32016-03-16 13:53:35 -0700988 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -0800989
990 REPORTER_ASSERT(r, codec);
991 if (!codec) return;
992
993 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
994 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
995
996 // Now test an image which is too big. Any image with a larger header (i.e.
997 // has bigger width/height) is also too big.
998 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
999 0x84, 0x80, 0x00, // W: 65536
1000 0x84, 0x80, 0x00 }; // H: 65536
1001 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
mtklein18300a32016-03-16 13:53:35 -07001002 codec.reset(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -08001003
1004 REPORTER_ASSERT(r, !codec);
1005}
msarett2812f032016-07-18 15:56:08 -07001006
1007DEF_TEST(Codec_jpeg_rewind, r) {
1008 const char* path = "mandrill_512_q075.jpg";
bungemanf93d7112016-09-16 06:24:20 -07001009 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarett2812f032016-07-18 15:56:08 -07001010 if (!stream) {
msarett2812f032016-07-18 15:56:08 -07001011 return;
1012 }
1013 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
1014 if (!codec) {
1015 ERRORF(r, "Unable to create codec '%s'.", path);
1016 return;
1017 }
1018
1019 const int width = codec->getInfo().width();
1020 const int height = codec->getInfo().height();
1021 size_t rowBytes = sizeof(SkPMColor) * width;
1022 SkAutoMalloc pixelStorage(height * rowBytes);
1023
1024 // Perform a sampled decode.
1025 SkAndroidCodec::AndroidOptions opts;
1026 opts.fSampleSize = 12;
1027 codec->getAndroidPixels(codec->getInfo().makeWH(width / 12, height / 12), pixelStorage.get(),
1028 rowBytes, &opts);
1029
1030 // Rewind the codec and perform a full image decode.
1031 SkCodec::Result result = codec->getPixels(codec->getInfo(), pixelStorage.get(), rowBytes);
1032 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1033}
msarett549ca322016-08-17 08:54:08 -07001034
msarett35bb74b2016-08-22 07:41:28 -07001035static void check_color_xform(skiatest::Reporter* r, const char* path) {
bungemanf93d7112016-09-16 06:24:20 -07001036 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(GetResourceAsStream(path)));
msarett35bb74b2016-08-22 07:41:28 -07001037
1038 SkAndroidCodec::AndroidOptions opts;
1039 opts.fSampleSize = 3;
1040 const int subsetWidth = codec->getInfo().width() / 2;
1041 const int subsetHeight = codec->getInfo().height() / 2;
1042 SkIRect subset = SkIRect::MakeWH(subsetWidth, subsetHeight);
1043 opts.fSubset = &subset;
1044
1045 const int dstWidth = subsetWidth / opts.fSampleSize;
1046 const int dstHeight = subsetHeight / opts.fSampleSize;
1047 sk_sp<SkData> data = SkData::MakeFromFileName(
1048 GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str());
1049 sk_sp<SkColorSpace> colorSpace = SkColorSpace::NewICC(data->data(), data->size());
1050 SkImageInfo dstInfo = codec->getInfo().makeWH(dstWidth, dstHeight)
1051 .makeColorType(kN32_SkColorType)
1052 .makeColorSpace(colorSpace);
1053
1054 size_t rowBytes = dstInfo.minRowBytes();
1055 SkAutoMalloc pixelStorage(dstInfo.getSafeSize(rowBytes));
1056 SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get(), rowBytes, &opts);
1057 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1058}
1059
1060DEF_TEST(Codec_ColorXform, r) {
1061 check_color_xform(r, "mandrill_512_q075.jpg");
1062 check_color_xform(r, "mandrill_512.png");
1063}
1064
msarettf17b71f2016-09-12 14:30:03 -07001065static bool color_type_match(SkColorType origColorType, SkColorType codecColorType) {
1066 switch (origColorType) {
1067 case kRGBA_8888_SkColorType:
1068 case kBGRA_8888_SkColorType:
1069 return kRGBA_8888_SkColorType == codecColorType ||
1070 kBGRA_8888_SkColorType == codecColorType;
1071 default:
1072 return origColorType == codecColorType;
1073 }
1074}
1075
1076static bool alpha_type_match(SkAlphaType origAlphaType, SkAlphaType codecAlphaType) {
1077 switch (origAlphaType) {
1078 case kUnpremul_SkAlphaType:
1079 case kPremul_SkAlphaType:
1080 return kUnpremul_SkAlphaType == codecAlphaType ||
1081 kPremul_SkAlphaType == codecAlphaType;
1082 default:
1083 return origAlphaType == codecAlphaType;
1084 }
1085}
1086
1087static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const SkImageInfo& info) {
1088 SkBitmap bm1;
1089 SkPMColor colors[256];
1090 SkAutoTUnref<SkColorTable> colorTable1(new SkColorTable(colors, 256));
1091 bm1.allocPixels(info, nullptr, colorTable1.get());
1092 int numColors;
1093 SkCodec::Result result = origCodec->getPixels(info, bm1.getPixels(), bm1.rowBytes(), nullptr,
1094 const_cast<SkPMColor*>(colorTable1->readColors()),
1095 &numColors);
1096 // This will fail to update colorTable1->count() but is fine for the purpose of this test.
1097 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarett9b09cd82016-08-29 14:47:49 -07001098
1099 // Encode the image to png.
1100 sk_sp<SkData> data =
1101 sk_sp<SkData>(SkImageEncoder::EncodeData(bm1, SkImageEncoder::kPNG_Type, 100));
1102
reed42943c82016-09-12 12:01:44 -07001103 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
msarettf17b71f2016-09-12 14:30:03 -07001104 REPORTER_ASSERT(r, color_type_match(info.colorType(), codec->getInfo().colorType()));
1105 REPORTER_ASSERT(r, alpha_type_match(info.alphaType(), codec->getInfo().alphaType()));
msarett9b09cd82016-08-29 14:47:49 -07001106
1107 SkBitmap bm2;
msarettf17b71f2016-09-12 14:30:03 -07001108 SkAutoTUnref<SkColorTable> colorTable2(new SkColorTable(colors, 256));
1109 bm2.allocPixels(info, nullptr, colorTable2.get());
1110 result = codec->getPixels(info, bm2.getPixels(), bm2.rowBytes(), nullptr,
1111 const_cast<SkPMColor*>(colorTable2->readColors()), &numColors);
msarett9b09cd82016-08-29 14:47:49 -07001112 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1113
1114 SkMD5::Digest d1, d2;
1115 md5(bm1, &d1);
1116 md5(bm2, &d2);
1117 REPORTER_ASSERT(r, d1 == d2);
1118}
1119
1120DEF_TEST(Codec_PngRoundTrip, r) {
msarett549ca322016-08-17 08:54:08 -07001121 const char* path = "mandrill_512_q075.jpg";
bungemanf93d7112016-09-16 06:24:20 -07001122 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarett549ca322016-08-17 08:54:08 -07001123 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett549ca322016-08-17 08:54:08 -07001124
msarettf17b71f2016-09-12 14:30:03 -07001125 SkColorType colorTypesOpaque[] = {
1126 kRGB_565_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1127 };
1128 for (SkColorType colorType : colorTypesOpaque) {
1129 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType);
1130 check_round_trip(r, codec.get(), newInfo);
1131 }
1132
msarett9b09cd82016-08-29 14:47:49 -07001133 path = "grayscale.jpg";
bungemanf93d7112016-09-16 06:24:20 -07001134 stream.reset(GetResourceAsStream(path));
msarett9b09cd82016-08-29 14:47:49 -07001135 codec.reset(SkCodec::NewFromStream(stream.release()));
msarettf17b71f2016-09-12 14:30:03 -07001136 check_round_trip(r, codec.get(), codec->getInfo());
1137
1138 path = "yellow_rose.png";
bungemanf93d7112016-09-16 06:24:20 -07001139 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001140 codec.reset(SkCodec::NewFromStream(stream.release()));
1141
1142 SkColorType colorTypesWithAlpha[] = {
1143 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1144 };
1145 SkAlphaType alphaTypes[] = {
1146 kUnpremul_SkAlphaType, kPremul_SkAlphaType
1147 };
1148 for (SkColorType colorType : colorTypesWithAlpha) {
1149 for (SkAlphaType alphaType : alphaTypes) {
1150 // Set color space to nullptr because color correct premultiplies do not round trip.
1151 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType)
1152 .makeAlphaType(alphaType)
1153 .makeColorSpace(nullptr);
1154 check_round_trip(r, codec.get(), newInfo);
1155 }
1156 }
1157
1158 path = "index8.png";
bungemanf93d7112016-09-16 06:24:20 -07001159 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001160 codec.reset(SkCodec::NewFromStream(stream.release()));
1161
1162 for (SkAlphaType alphaType : alphaTypes) {
1163 SkImageInfo newInfo = codec->getInfo().makeAlphaType(alphaType)
1164 .makeColorSpace(nullptr);
1165 check_round_trip(r, codec.get(), newInfo);
1166 }
msarett549ca322016-08-17 08:54:08 -07001167}
msarett2ecc35f2016-09-08 11:55:16 -07001168
1169static void test_conversion_possible(skiatest::Reporter* r, const char* path,
scroggo8e6c7ad2016-09-16 08:20:38 -07001170 bool supportsScanlineDecoder,
1171 bool supportsIncrementalDecoder) {
bungemanf93d7112016-09-16 06:24:20 -07001172 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarett2ecc35f2016-09-08 11:55:16 -07001173 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
1174 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType);
1175
1176 SkBitmap bm;
1177 bm.allocPixels(infoF16);
1178 SkCodec::Result result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1179 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001180
1181 result = codec->startScanlineDecode(infoF16);
1182 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001183 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001184 } else {
1185 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1186 }
1187
1188 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1189 if (supportsIncrementalDecoder) {
1190 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
1191 } else {
1192 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001193 }
1194
raftias7c602de2016-10-13 10:45:44 -07001195 infoF16 = infoF16.makeColorSpace(as_CSB(infoF16.colorSpace())->makeLinearGamma());
msarett2ecc35f2016-09-08 11:55:16 -07001196 result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1197 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001198 result = codec->startScanlineDecode(infoF16);
1199 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001200 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001201 } else {
1202 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1203 }
1204
1205 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1206 if (supportsIncrementalDecoder) {
1207 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1208 } else {
1209 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001210 }
1211}
1212
1213DEF_TEST(Codec_F16ConversionPossible, r) {
scroggo8e6c7ad2016-09-16 08:20:38 -07001214 test_conversion_possible(r, "color_wheel.webp", false, false);
1215 test_conversion_possible(r, "mandrill_512_q075.jpg", true, false);
1216 test_conversion_possible(r, "yellow_rose.png", false, true);
1217}
1218
1219// Only rewinds up to a limit.
1220class LimitedRewindingStream : public SkStream {
1221public:
1222 static SkStream* Make(const char path[], size_t limit) {
1223 SkStream* stream = GetResourceAsStream(path);
1224 if (!stream) {
1225 return nullptr;
1226 }
1227 return new LimitedRewindingStream(stream, limit);
1228 }
1229
1230 size_t read(void* buffer, size_t size) override {
1231 const size_t bytes = fStream->read(buffer, size);
1232 fPosition += bytes;
1233 return bytes;
1234 }
1235
1236 bool isAtEnd() const override {
1237 return fStream->isAtEnd();
1238 }
1239
1240 bool rewind() override {
1241 if (fPosition <= fLimit && fStream->rewind()) {
1242 fPosition = 0;
1243 return true;
1244 }
1245
1246 return false;
1247 }
1248
1249private:
1250 SkAutoTDelete<SkStream> fStream;
1251 const size_t fLimit;
1252 size_t fPosition;
1253
1254 LimitedRewindingStream(SkStream* stream, size_t limit)
1255 : fStream(stream)
1256 , fLimit(limit)
1257 , fPosition(0)
1258 {
1259 SkASSERT(fStream);
1260 }
1261};
1262
1263DEF_TEST(Codec_fallBack, r) {
1264 // SkAndroidCodec needs to be able to fall back to scanline decoding
1265 // if incremental decoding does not work. Make sure this does not
1266 // require a rewind.
1267
1268 // Formats that currently do not support incremental decoding
1269 auto files = {
1270 "box.gif",
1271 "CMYK.jpg",
1272 "color_wheel.ico",
1273 "mandrill.wbmp",
1274 "randPixels.bmp",
1275 };
1276 for (auto file : files) {
1277 SkStream* stream = LimitedRewindingStream::Make(file, 14);
1278 if (!stream) {
1279 SkDebugf("Missing resources (%s). Set --resourcePath.\n", file);
1280 return;
1281 }
1282
1283 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream));
1284 if (!codec) {
1285 ERRORF(r, "Failed to create codec for %s,", file);
1286 continue;
1287 }
1288
1289 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
1290 SkBitmap bm;
1291 bm.allocPixels(info);
1292
1293 if (SkCodec::kUnimplemented != codec->startIncrementalDecode(info, bm.getPixels(),
1294 bm.rowBytes())) {
1295 ERRORF(r, "Is scanline decoding now implemented for %s?", file);
1296 continue;
1297 }
1298
1299 // Scanline decoding should not require a rewind.
1300 SkCodec::Result result = codec->startScanlineDecode(info);
1301 if (SkCodec::kSuccess != result) {
1302 ERRORF(r, "Scanline decoding failed for %s with %i", file, result);
1303 }
1304 }
msarett2ecc35f2016-09-08 11:55:16 -07001305}
scroggoc46cdd42016-10-10 06:45:32 -07001306
1307// This test verifies that we fixed an assert statement that fired when reusing a png codec
1308// after scaling.
1309DEF_TEST(Codec_reusePng, r) {
1310 std::unique_ptr<SkStream> stream(GetResourceAsStream("plane.png"));
1311 if (!stream) {
1312 return;
1313 }
1314
1315 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
1316 if (!codec) {
1317 ERRORF(r, "Failed to create codec\n");
1318 return;
1319 }
1320
1321 SkAndroidCodec::AndroidOptions opts;
1322 opts.fSampleSize = 5;
1323 auto size = codec->getSampledDimensions(opts.fSampleSize);
1324 auto info = codec->getInfo().makeWH(size.fWidth, size.fHeight).makeColorType(kN32_SkColorType);
1325 SkBitmap bm;
1326 bm.allocPixels(info);
1327 auto result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1328 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1329
1330 info = codec->getInfo().makeColorType(kN32_SkColorType);
1331 bm.allocPixels(info);
1332 opts.fSampleSize = 1;
1333 result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1334 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1335}
scroggoe61b3b42016-10-10 07:17:32 -07001336
1337DEF_TEST(Codec_rowsDecoded, r) {
1338 auto file = "plane_interlaced.png";
1339 std::unique_ptr<SkStream> stream(GetResourceAsStream(file));
1340 if (!stream) {
1341 return;
1342 }
1343
1344 // This is enough to read the header etc, but no rows.
1345 auto data = SkData::MakeFromStream(stream.get(), 99);
1346 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(data));
1347 if (!codec) {
1348 ERRORF(r, "Failed to create codec\n");
1349 return;
1350 }
1351
1352 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1353 SkBitmap bm;
1354 bm.allocPixels(info);
1355 auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
1356 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1357
1358 // This is an arbitrary value. The important fact is that it is not zero, and rowsDecoded
1359 // should get set to zero by incrementalDecode.
1360 int rowsDecoded = 77;
1361 result = codec->incrementalDecode(&rowsDecoded);
1362 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput);
1363 REPORTER_ASSERT(r, rowsDecoded == 0);
1364}
Matt Sarett29121eb2016-10-17 14:32:46 -04001365
1366DEF_TEST(Codec_IcoIntOverflow, r) {
1367 // ASAN will complain if there is an issue.
1368 SkBitmap bitmap;
1369 const bool success = GetResourceAsBitmap("invalid_images/int_overflow.ico", &bitmap);
1370 REPORTER_ASSERT(r, !success);
1371}