blob: 2768d57cfc392de03d58dbe07212a90e5f07a077 [file] [log] [blame]
halcanarya096d7a2015-03-27 12:16:53 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Resources.h"
msarett3d9d7a72015-10-21 10:27:10 -07009#include "SkAndroidCodec.h"
halcanarya096d7a2015-03-27 12:16:53 -070010#include "SkBitmap.h"
11#include "SkCodec.h"
msarettedd2dcf2016-01-14 13:12:26 -080012#include "SkCodecImageGenerator.h"
msarette6dd0042015-10-09 11:07:34 -070013#include "SkData.h"
scroggob9a1e342015-11-30 06:25:31 -080014#include "SkImageDecoder.h"
halcanarya096d7a2015-03-27 12:16:53 -070015#include "SkMD5.h"
scroggob636b452015-07-22 07:16:20 -070016#include "SkRandom.h"
scroggocf98fa92015-11-23 08:14:40 -080017#include "SkStream.h"
scroggob9a1e342015-11-30 06:25:31 -080018#include "SkStreamPriv.h"
scroggocf98fa92015-11-23 08:14:40 -080019#include "SkPngChunkReader.h"
halcanarya096d7a2015-03-27 12:16:53 -070020#include "Test.h"
21
scroggocf98fa92015-11-23 08:14:40 -080022#include "png.h"
23
halcanarya096d7a2015-03-27 12:16:53 -070024static SkStreamAsset* resource(const char path[]) {
25 SkString fullPath = GetResourcePath(path);
26 return SkStream::NewFromFile(fullPath.c_str());
27}
28
29static void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
30 SkAutoLockPixels autoLockPixels(bm);
31 SkASSERT(bm.getPixels());
32 SkMD5 md5;
33 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
34 for (int y = 0; y < bm.height(); ++y) {
35 md5.update(static_cast<uint8_t*>(bm.getAddr(0, y)), rowLen);
36 }
37 md5.finish(*digest);
38}
39
scroggo9b2cdbf42015-07-10 12:07:02 -070040/**
41 * Compute the digest for bm and compare it to a known good digest.
42 * @param r Reporter to assert that bm's digest matches goodDigest.
43 * @param goodDigest The known good digest to compare to.
44 * @param bm The bitmap to test.
45 */
46static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
47 const SkBitmap& bm) {
48 SkMD5::Digest digest;
49 md5(bm, &digest);
50 REPORTER_ASSERT(r, digest == goodDigest);
51}
52
scroggod1bc5742015-08-12 08:31:44 -070053/**
54 * Test decoding an SkCodec to a particular SkImageInfo.
55 *
halcanary96fcdcc2015-08-27 07:41:13 -070056 * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr,
scroggod1bc5742015-08-12 08:31:44 -070057 * the resulting decode should match.
58 */
59static void test_info(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
60 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
61 SkBitmap bm;
62 bm.allocPixels(info);
63 SkAutoLockPixels autoLockPixels(bm);
64
65 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
66 REPORTER_ASSERT(r, result == expectedResult);
67
68 if (goodDigest) {
69 compare_to_good_digest(r, *goodDigest, bm);
70 }
71}
72
msarett3d9d7a72015-10-21 10:27:10 -070073static void test_android_info(skiatest::Reporter* r, SkAndroidCodec* codec, const SkImageInfo& info,
74 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
75 SkBitmap bm;
76 bm.allocPixels(info);
77 SkAutoLockPixels autoLockPixels(bm);
78
79 SkCodec::Result result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes());
80 REPORTER_ASSERT(r, result == expectedResult);
81
82 if (goodDigest) {
83 compare_to_good_digest(r, *goodDigest, bm);
84 }
85}
86
scroggob636b452015-07-22 07:16:20 -070087SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
88 SkIRect rect;
89 do {
90 rect.fLeft = rand->nextRangeU(0, w);
91 rect.fTop = rand->nextRangeU(0, h);
92 rect.fRight = rand->nextRangeU(0, w);
93 rect.fBottom = rand->nextRangeU(0, h);
94 rect.sort();
95 } while (rect.isEmpty());
96 return rect;
97}
98
msarettcc7f3052015-10-05 14:20:27 -070099static void test_codec(skiatest::Reporter* r, SkCodec* codec, SkBitmap& bm, const SkImageInfo& info,
scroggo27c17282015-10-27 08:14:46 -0700100 const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest,
101 const SkMD5::Digest* goodDigest) {
msarette6dd0042015-10-09 11:07:34 -0700102
halcanarya096d7a2015-03-27 12:16:53 -0700103 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -0700104 bm.allocPixels(info);
105 SkAutoLockPixels autoLockPixels(bm);
msarettcc7f3052015-10-05 14:20:27 -0700106
107 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700108 REPORTER_ASSERT(r, result == expectedResult);
halcanarya096d7a2015-03-27 12:16:53 -0700109
msarettcc7f3052015-10-05 14:20:27 -0700110 md5(bm, digest);
111 if (goodDigest) {
112 REPORTER_ASSERT(r, *digest == *goodDigest);
113 }
halcanarya096d7a2015-03-27 12:16:53 -0700114
msarett8ff6ca62015-09-18 12:06:04 -0700115 {
116 // Test decoding to 565
117 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggo27c17282015-10-27 08:14:46 -0700118 SkCodec::Result expected565 = info.alphaType() == kOpaque_SkAlphaType ?
msarette6dd0042015-10-09 11:07:34 -0700119 expectedResult : SkCodec::kInvalidConversion;
120 test_info(r, codec, info565, expected565, nullptr);
msarett8ff6ca62015-09-18 12:06:04 -0700121 }
122
123 // Verify that re-decoding gives the same result. It is interesting to check this after
124 // a decode to 565, since choosing to decode to 565 may result in some of the decode
125 // options being modified. These options should return to their defaults on another
126 // decode to kN32, so the new digest should match the old digest.
msarette6dd0042015-10-09 11:07:34 -0700127 test_info(r, codec, info, expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700128
129 {
130 // Check alpha type conversions
131 if (info.alphaType() == kOpaque_SkAlphaType) {
132 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700133 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700134 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700135 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700136 } else {
137 // Decoding to opaque should fail
138 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700139 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700140 SkAlphaType otherAt = info.alphaType();
141 if (kPremul_SkAlphaType == otherAt) {
142 otherAt = kUnpremul_SkAlphaType;
143 } else {
144 otherAt = kPremul_SkAlphaType;
145 }
146 // The other non-opaque alpha type should always succeed, but not match.
msarette6dd0042015-10-09 11:07:34 -0700147 test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700148 }
149 }
msarettcc7f3052015-10-05 14:20:27 -0700150}
151
msarett3d9d7a72015-10-21 10:27:10 -0700152static void test_android_codec(skiatest::Reporter* r, SkAndroidCodec* codec, SkBitmap& bm,
scroggo27c17282015-10-27 08:14:46 -0700153 const SkImageInfo& info, const SkISize& size, SkCodec::Result expectedResult,
154 SkMD5::Digest* digest, const SkMD5::Digest* goodDigest) {
msarett3d9d7a72015-10-21 10:27:10 -0700155
156 REPORTER_ASSERT(r, info.dimensions() == size);
157 bm.allocPixels(info);
158 SkAutoLockPixels autoLockPixels(bm);
159
160 SkCodec::Result result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes());
161 REPORTER_ASSERT(r, result == expectedResult);
162
163 md5(bm, digest);
164 if (goodDigest) {
165 REPORTER_ASSERT(r, *digest == *goodDigest);
166 }
167
168 {
169 // Test decoding to 565
170 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggo27c17282015-10-27 08:14:46 -0700171 SkCodec::Result expected565 = info.alphaType() == kOpaque_SkAlphaType ?
msarett3d9d7a72015-10-21 10:27:10 -0700172 expectedResult : SkCodec::kInvalidConversion;
173 test_android_info(r, codec, info565, expected565, nullptr);
174 }
175
176 // Verify that re-decoding gives the same result. It is interesting to check this after
177 // a decode to 565, since choosing to decode to 565 may result in some of the decode
178 // options being modified. These options should return to their defaults on another
179 // decode to kN32, so the new digest should match the old digest.
180 test_android_info(r, codec, info, expectedResult, digest);
181
182 {
183 // Check alpha type conversions
184 if (info.alphaType() == kOpaque_SkAlphaType) {
185 test_android_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
186 SkCodec::kInvalidConversion, nullptr);
187 test_android_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
188 SkCodec::kInvalidConversion, nullptr);
189 } else {
190 // Decoding to opaque should fail
191 test_android_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
192 SkCodec::kInvalidConversion, nullptr);
193 SkAlphaType otherAt = info.alphaType();
194 if (kPremul_SkAlphaType == otherAt) {
195 otherAt = kUnpremul_SkAlphaType;
196 } else {
197 otherAt = kPremul_SkAlphaType;
198 }
199 // The other non-opaque alpha type should always succeed, but not match.
200 test_android_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
201 }
202 }
203}
204
scroggo2c3b2182015-10-09 08:40:59 -0700205// FIXME: SkScaledCodec is currently only supported for types used by BRD
halcanary6950de62015-11-07 05:29:00 -0800206// https://bug.skia.org/4428
scroggo2c3b2182015-10-09 08:40:59 -0700207static bool supports_scaled_codec(const char path[]) {
208 static const char* const exts[] = {
209 "jpg", "jpeg", "png", "webp"
210 "JPG", "JPEG", "PNG", "WEBP"
211 };
212
213 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
214 if (SkStrEndsWith(path, exts[i])) {
215 return true;
216 }
217 }
218 return false;
219}
220
msarettcc7f3052015-10-05 14:20:27 -0700221static void check(skiatest::Reporter* r,
222 const char path[],
223 SkISize size,
224 bool supportsScanlineDecoding,
225 bool supportsSubsetDecoding,
msarette6dd0042015-10-09 11:07:34 -0700226 bool supportsIncomplete = true) {
msarettcc7f3052015-10-05 14:20:27 -0700227
228 SkAutoTDelete<SkStream> stream(resource(path));
229 if (!stream) {
230 SkDebugf("Missing resource '%s'\n", path);
231 return;
232 }
msarette6dd0042015-10-09 11:07:34 -0700233
234 SkAutoTDelete<SkCodec> codec(nullptr);
235 bool isIncomplete = supportsIncomplete;
236 if (isIncomplete) {
237 size_t size = stream->getLength();
238 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
239 codec.reset(SkCodec::NewFromData(data));
240 } else {
241 codec.reset(SkCodec::NewFromStream(stream.detach()));
242 }
msarettcc7f3052015-10-05 14:20:27 -0700243 if (!codec) {
244 ERRORF(r, "Unable to decode '%s'", path);
245 return;
246 }
247
248 // Test full image decodes with SkCodec
249 SkMD5::Digest codecDigest;
250 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
251 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700252 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
scroggo27c17282015-10-27 08:14:46 -0700253 test_codec(r, codec, bm, info, size, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700254
255 // Scanline decoding follows.
msarettcc7f3052015-10-05 14:20:27 -0700256 // Need to call startScanlineDecode() first.
scroggo46c57472015-09-30 08:57:13 -0700257 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700258 == 0);
scroggo46c57472015-09-30 08:57:13 -0700259 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700260 == 0);
scroggo46c57472015-09-30 08:57:13 -0700261
262 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700263 if (supportsScanlineDecoding) {
264 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700265
scroggo46c57472015-09-30 08:57:13 -0700266 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700267
scroggo58421542015-04-01 11:25:20 -0700268 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700269 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
270 if (!isIncomplete) {
271 REPORTER_ASSERT(r, 1 == lines);
272 }
scroggo58421542015-04-01 11:25:20 -0700273 }
274 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700275 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700276 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700277 }
scroggo46c57472015-09-30 08:57:13 -0700278
279 // Cannot continue to decode scanlines beyond the end
280 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700281 == 0);
scroggo46c57472015-09-30 08:57:13 -0700282
283 // Interrupting a scanline decode with a full decode starts from
284 // scratch
285 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700286 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
287 if (!isIncomplete) {
288 REPORTER_ASSERT(r, lines == 1);
289 }
scroggo46c57472015-09-30 08:57:13 -0700290 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700291 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700292 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700293 == 0);
scroggo46c57472015-09-30 08:57:13 -0700294 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700295 == 0);
msarett80803ff2015-10-16 10:54:12 -0700296
297 // Test partial scanline decodes
298 if (supports_scaled_codec(path) && info.width() >= 3) {
299 SkCodec::Options options;
300 int width = info.width();
301 int height = info.height();
302 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
303 options.fSubset = &subset;
304
305 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
306 nullptr, nullptr);
307 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
308
309 for (int y = 0; y < height; y++) {
310 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
311 if (!isIncomplete) {
312 REPORTER_ASSERT(r, 1 == lines);
313 }
314 }
315 }
scroggo58421542015-04-01 11:25:20 -0700316 } else {
scroggo46c57472015-09-30 08:57:13 -0700317 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700318 }
scroggob636b452015-07-22 07:16:20 -0700319
320 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
321 // random subsets.
322 // Do not attempt to decode subsets of an image of only once pixel, since there is no
323 // meaningful subset.
324 if (size.width() * size.height() == 1) {
325 return;
326 }
327
328 SkRandom rand;
329 SkIRect subset;
330 SkCodec::Options opts;
331 opts.fSubset = &subset;
332 for (int i = 0; i < 5; i++) {
333 subset = generate_random_subset(&rand, size.width(), size.height());
334 SkASSERT(!subset.isEmpty());
335 const bool supported = codec->getValidSubset(&subset);
336 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
337
338 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
339 SkBitmap bm;
340 bm.allocPixels(subsetInfo);
341 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700342 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700343
344 if (supportsSubsetDecoding) {
msarette6dd0042015-10-09 11:07:34 -0700345 REPORTER_ASSERT(r, result == expectedResult);
scroggob636b452015-07-22 07:16:20 -0700346 // Webp is the only codec that supports subsets, and it will have modified the subset
347 // to have even left/top.
348 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
349 } else {
350 // No subsets will work.
351 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
352 }
353 }
msarettcc7f3052015-10-05 14:20:27 -0700354
355 // SkScaledCodec tests
scroggo2c3b2182015-10-09 08:40:59 -0700356 if ((supportsScanlineDecoding || supportsSubsetDecoding) && supports_scaled_codec(path)) {
357
msarettcc7f3052015-10-05 14:20:27 -0700358 SkAutoTDelete<SkStream> stream(resource(path));
359 if (!stream) {
360 SkDebugf("Missing resource '%s'\n", path);
361 return;
362 }
msarette6dd0042015-10-09 11:07:34 -0700363
msarett3d9d7a72015-10-21 10:27:10 -0700364 SkAutoTDelete<SkAndroidCodec> codec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700365 if (isIncomplete) {
366 size_t size = stream->getLength();
367 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
msarett3d9d7a72015-10-21 10:27:10 -0700368 codec.reset(SkAndroidCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700369 } else {
msarett3d9d7a72015-10-21 10:27:10 -0700370 codec.reset(SkAndroidCodec::NewFromStream(stream.detach()));
msarette6dd0042015-10-09 11:07:34 -0700371 }
msarettcc7f3052015-10-05 14:20:27 -0700372 if (!codec) {
373 ERRORF(r, "Unable to decode '%s'", path);
374 return;
375 }
376
377 SkBitmap bm;
378 SkMD5::Digest scaledCodecDigest;
scroggo27c17282015-10-27 08:14:46 -0700379 test_android_codec(r, codec, bm, info, size, expectedResult,
msarett3d9d7a72015-10-21 10:27:10 -0700380 &scaledCodecDigest, &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700381 }
382
msarettedd2dcf2016-01-14 13:12:26 -0800383 // Test SkCodecImageGenerator
384 if (!isIncomplete) {
385 SkAutoTDelete<SkStream> stream(resource(path));
386 SkAutoTUnref<SkData> fullData(SkData::NewFromStream(stream, stream->getLength()));
387 SkAutoTDelete<SkImageGenerator> gen(SkCodecImageGenerator::NewFromEncodedCodec(fullData));
388 SkBitmap bm;
389 bm.allocPixels(info);
390 SkAutoLockPixels autoLockPixels(bm);
391 REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes()));
392 compare_to_good_digest(r, codecDigest, bm);
393 }
394
msarette6dd0042015-10-09 11:07:34 -0700395 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
396 if (isIncomplete) {
scroggo27c17282015-10-27 08:14:46 -0700397 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false);
msarettcc7f3052015-10-05 14:20:27 -0700398 }
halcanarya096d7a2015-03-27 12:16:53 -0700399}
400
401DEF_TEST(Codec, r) {
402 // WBMP
msarett99f567e2015-08-05 12:58:26 -0700403 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700404
scroggo6f5e6192015-06-18 12:53:43 -0700405 // WEBP
scroggob636b452015-07-22 07:16:20 -0700406 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true);
407 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true);
408 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true);
scroggo6f5e6192015-06-18 12:53:43 -0700409
halcanarya096d7a2015-03-27 12:16:53 -0700410 // BMP
msarett5406d6f2015-08-31 06:55:13 -0700411 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700412
413 // ICO
msarette6dd0042015-10-09 11:07:34 -0700414 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700415 // These two tests examine interestingly different behavior:
416 // Decodes an embedded BMP image
msarettbe8216a2015-12-04 08:00:50 -0800417 check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700418 // Decodes an embedded PNG image
msarettbe8216a2015-12-04 08:00:50 -0800419 check(r, "google_chrome.ico", SkISize::Make(256, 256), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700420
msarett438b2ad2015-04-09 12:43:10 -0700421 // GIF
msarette6dd0042015-10-09 11:07:34 -0700422 // FIXME: We are not ready to test incomplete GIFs
scroggo27c17282015-10-27 08:14:46 -0700423 check(r, "box.gif", SkISize::Make(200, 55), true, false, false);
424 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700425 // randPixels.gif is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700426 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, false);
msarett438b2ad2015-04-09 12:43:10 -0700427
msarette16b04a2015-04-15 07:32:19 -0700428 // JPG
scroggo27c17282015-10-27 08:14:46 -0700429 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false);
scroggob636b452015-07-22 07:16:20 -0700430 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
msarette6dd0042015-10-09 11:07:34 -0700431 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700432 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggob636b452015-07-22 07:16:20 -0700433 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
msarette6dd0042015-10-09 11:07:34 -0700434 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700435 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
msarette16b04a2015-04-15 07:32:19 -0700436
halcanarya096d7a2015-03-27 12:16:53 -0700437 // PNG
scroggo27c17282015-10-27 08:14:46 -0700438 check(r, "arrow.png", SkISize::Make(187, 312), true, false, false);
439 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false, false);
440 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false, false);
441 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false, false);
442 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false, false);
443 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false, false);
444 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false, false);
445 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false, false);
446 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false, false);
447 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false, false);
448 check(r, "plane.png", SkISize::Make(250, 126), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700449 // FIXME: We are not ready to test incomplete interlaced pngs
scroggo27c17282015-10-27 08:14:46 -0700450 check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false, false);
451 check(r, "randPixels.png", SkISize::Make(8, 8), true, false, false);
452 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700453}
scroggo0a7e69c2015-04-03 07:22:22 -0700454
scroggo46c57472015-09-30 08:57:13 -0700455// Test interlaced PNG in stripes, similar to DM's kStripe_Mode
456DEF_TEST(Codec_stripes, r) {
457 const char * path = "plane_interlaced.png";
458 SkAutoTDelete<SkStream> stream(resource(path));
459 if (!stream) {
460 SkDebugf("Missing resource '%s'\n", path);
461 }
462
463 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
464 REPORTER_ASSERT(r, codec);
465
466 if (!codec) {
467 return;
468 }
469
470 switch (codec->getScanlineOrder()) {
471 case SkCodec::kBottomUp_SkScanlineOrder:
472 case SkCodec::kOutOfOrder_SkScanlineOrder:
473 ERRORF(r, "This scanline order will not match the original.");
474 return;
475 default:
476 break;
477 }
478
479 // Baseline for what the image should look like, using N32.
480 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
481
482 SkBitmap bm;
483 bm.allocPixels(info);
484 SkAutoLockPixels autoLockPixels(bm);
485 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
486 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
487
488 SkMD5::Digest digest;
489 md5(bm, &digest);
490
491 // Now decode in stripes
492 const int height = info.height();
493 const int numStripes = 4;
494 int stripeHeight;
495 int remainingLines;
496 SkTDivMod(height, numStripes, &stripeHeight, &remainingLines);
497
498 bm.eraseColor(SK_ColorYELLOW);
499
500 result = codec->startScanlineDecode(info);
501 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
502
503 // Odd stripes
504 for (int i = 1; i < numStripes; i += 2) {
505 // Skip the even stripes
msarette6dd0042015-10-09 11:07:34 -0700506 bool skipResult = codec->skipScanlines(stripeHeight);
507 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700508
msarette6dd0042015-10-09 11:07:34 -0700509 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700510 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700511 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700512 }
513
514 // Even stripes
515 result = codec->startScanlineDecode(info);
516 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
517
518 for (int i = 0; i < numStripes; i += 2) {
msarette6dd0042015-10-09 11:07:34 -0700519 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700520 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700521 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700522
523 // Skip the odd stripes
524 if (i + 1 < numStripes) {
msarette6dd0042015-10-09 11:07:34 -0700525 bool skipResult = codec->skipScanlines(stripeHeight);
526 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700527 }
528 }
529
530 // Remainder at the end
531 if (remainingLines > 0) {
532 result = codec->startScanlineDecode(info);
533 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
534
msarette6dd0042015-10-09 11:07:34 -0700535 bool skipResult = codec->skipScanlines(height - remainingLines);
536 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700537
msarette6dd0042015-10-09 11:07:34 -0700538 int linesDecoded = codec->getScanlines(bm.getAddr(0, height - remainingLines),
scroggo46c57472015-09-30 08:57:13 -0700539 remainingLines, bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700540 REPORTER_ASSERT(r, linesDecoded == remainingLines);
scroggo46c57472015-09-30 08:57:13 -0700541 }
542
543 compare_to_good_digest(r, digest, bm);
544}
545
scroggo0a7e69c2015-04-03 07:22:22 -0700546static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700547 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700548 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700549 REPORTER_ASSERT(r, !codec);
550
msarett3d9d7a72015-10-21 10:27:10 -0700551 SkAndroidCodec* androidCodec =
552 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
553 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700554}
555
556// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
557// even on failure. Test some bad streams.
558DEF_TEST(Codec_leaks, r) {
559 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
560 const char nonSupportedStream[] = "hello world";
561 // The other strings should look like the beginning of a file type, so we'll call some
562 // internal version of NewFromStream, which must also delete the stream on failure.
563 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
564 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
565 const char emptyWebp[] = "RIFF1234WEBPVP";
566 const char emptyBmp[] = { 'B', 'M' };
567 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
568 const char emptyGif[] = "GIFVER";
569
570 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
571 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
572 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
573 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
574 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
575 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
576 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
577}
msarette16b04a2015-04-15 07:32:19 -0700578
scroggo2c3b2182015-10-09 08:40:59 -0700579DEF_TEST(Codec_null, r) {
580 // Attempting to create an SkCodec or an SkScaledCodec with null should not
581 // crash.
582 SkCodec* codec = SkCodec::NewFromStream(nullptr);
583 REPORTER_ASSERT(r, !codec);
584
msarett3d9d7a72015-10-21 10:27:10 -0700585 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
586 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700587}
588
msarette16b04a2015-04-15 07:32:19 -0700589static void test_dimensions(skiatest::Reporter* r, const char path[]) {
590 // Create the codec from the resource file
591 SkAutoTDelete<SkStream> stream(resource(path));
592 if (!stream) {
593 SkDebugf("Missing resource '%s'\n", path);
594 return;
595 }
msarett3d9d7a72015-10-21 10:27:10 -0700596 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.detach()));
msarette16b04a2015-04-15 07:32:19 -0700597 if (!codec) {
598 ERRORF(r, "Unable to create codec '%s'", path);
599 return;
600 }
601
602 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800603 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700604 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700605 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700606 SkImageInfo scaledInfo = codec->getInfo()
607 .makeWH(scaledDims.width(), scaledDims.height())
608 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700609
610 // Set up for the decode
611 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
612 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
613 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
614
msarett3d9d7a72015-10-21 10:27:10 -0700615 SkAndroidCodec::AndroidOptions options;
616 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700617 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700618 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700619 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700620 }
621}
622
623// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
624DEF_TEST(Codec_Dimensions, r) {
625 // JPG
626 test_dimensions(r, "CMYK.jpg");
627 test_dimensions(r, "color_wheel.jpg");
628 test_dimensions(r, "grayscale.jpg");
629 test_dimensions(r, "mandrill_512_q075.jpg");
630 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700631
632 // Decoding small images with very large scaling factors is a potential
633 // source of bugs and crashes. We disable these tests in Gold because
634 // tiny images are not very useful to look at.
635 // Here we make sure that we do not crash or access illegal memory when
636 // performing scaled decodes on small images.
637 test_dimensions(r, "1x1.png");
638 test_dimensions(r, "2x2.png");
639 test_dimensions(r, "3x3.png");
640 test_dimensions(r, "3x1.png");
641 test_dimensions(r, "1x1.png");
642 test_dimensions(r, "16x1.png");
643 test_dimensions(r, "1x16.png");
644 test_dimensions(r, "mandrill_16.png");
645
msarette16b04a2015-04-15 07:32:19 -0700646}
647
msarettd0375bc2015-08-12 08:08:56 -0700648static void test_invalid(skiatest::Reporter* r, const char path[]) {
msarett4b17fa32015-04-23 08:53:39 -0700649 SkAutoTDelete<SkStream> stream(resource(path));
650 if (!stream) {
651 SkDebugf("Missing resource '%s'\n", path);
652 return;
653 }
654 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700655 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700656}
msarette16b04a2015-04-15 07:32:19 -0700657
msarett4b17fa32015-04-23 08:53:39 -0700658DEF_TEST(Codec_Empty, r) {
659 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700660 test_invalid(r, "empty_images/zero-dims.gif");
661 test_invalid(r, "empty_images/zero-embedded.ico");
662 test_invalid(r, "empty_images/zero-width.bmp");
663 test_invalid(r, "empty_images/zero-height.bmp");
664 test_invalid(r, "empty_images/zero-width.jpg");
665 test_invalid(r, "empty_images/zero-height.jpg");
666 test_invalid(r, "empty_images/zero-width.png");
667 test_invalid(r, "empty_images/zero-height.png");
668 test_invalid(r, "empty_images/zero-width.wbmp");
669 test_invalid(r, "empty_images/zero-height.wbmp");
670 // This image is an ico with an embedded mask-bmp. This is illegal.
671 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700672}
msarett99f567e2015-08-05 12:58:26 -0700673
674static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
675 SkAutoTDelete<SkStream> stream(resource(path));
676 if (!stream) {
677 SkDebugf("Missing resource '%s'\n", path);
678 return;
679 }
scroggo46c57472015-09-30 08:57:13 -0700680 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.detach()));
msarett99f567e2015-08-05 12:58:26 -0700681
682 // This should return kSuccess because kIndex8 is supported.
683 SkPMColor colorStorage[256];
684 int colorCount;
scroggo46c57472015-09-30 08:57:13 -0700685 SkCodec::Result result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700686 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
msarett99f567e2015-08-05 12:58:26 -0700687 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
688 // The rest of the test is uninteresting if kIndex8 is not supported
689 if (SkCodec::kSuccess != result) {
690 return;
691 }
692
693 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
694 // colorPtr and a valid colorCountPtr.
scroggo46c57472015-09-30 08:57:13 -0700695 result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700696 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
msarett99f567e2015-08-05 12:58:26 -0700697 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
scroggo46c57472015-09-30 08:57:13 -0700698 result = decoder->startScanlineDecode(
msarett99f567e2015-08-05 12:58:26 -0700699 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
700 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
701}
702
703DEF_TEST(Codec_Params, r) {
704 test_invalid_parameters(r, "index8.png");
705 test_invalid_parameters(r, "mandrill.wbmp");
706}
scroggocf98fa92015-11-23 08:14:40 -0800707
708static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
709 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
710 if (!sk_stream->write(data, len)) {
711 png_error(png_ptr, "sk_write_fn Error!");
712 }
713}
714
715#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
716DEF_TEST(Codec_pngChunkReader, r) {
717 // Create a dummy bitmap. Use unpremul RGBA for libpng.
718 SkBitmap bm;
719 const int w = 1;
720 const int h = 1;
721 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
722 kUnpremul_SkAlphaType);
723 bm.setInfo(bmInfo);
724 bm.allocPixels();
725 bm.eraseColor(SK_ColorBLUE);
726 SkMD5::Digest goodDigest;
727 md5(bm, &goodDigest);
728
729 // Write to a png file.
730 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
731 REPORTER_ASSERT(r, png);
732 if (!png) {
733 return;
734 }
735
736 png_infop info = png_create_info_struct(png);
737 REPORTER_ASSERT(r, info);
738 if (!info) {
739 png_destroy_write_struct(&png, nullptr);
740 return;
741 }
742
743 if (setjmp(png_jmpbuf(png))) {
744 ERRORF(r, "failed writing png");
745 png_destroy_write_struct(&png, &info);
746 return;
747 }
748
749 SkDynamicMemoryWStream wStream;
750 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
751
752 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
753 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
754 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
755
756 // Create some chunks that match the Android framework's use.
757 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800758 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
759 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
760 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800761 };
762
763 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
764 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
765#if PNG_LIBPNG_VER < 10600
766 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800767 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
768 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800769#endif
770
771 png_write_info(png, info);
772
773 for (int j = 0; j < h; j++) {
774 png_bytep row = (png_bytep)(bm.getAddr(0, j));
775 png_write_rows(png, &row, 1);
776 }
777 png_write_end(png, info);
778 png_destroy_write_struct(&png, &info);
779
780 class ChunkReader : public SkPngChunkReader {
781 public:
782 ChunkReader(skiatest::Reporter* r)
783 : fReporter(r)
784 {
785 this->reset();
786 }
787
788 bool readChunk(const char tag[], const void* data, size_t length) override {
789 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
790 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
791 // Tag matches. This should have been the first time we see it.
792 REPORTER_ASSERT(fReporter, !fSeen[i]);
793 fSeen[i] = true;
794
795 // Data and length should match
796 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
797 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
798 (const char*) gUnknowns[i].data));
799 return true;
800 }
801 }
802 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
803 return true;
804 }
805
806 bool allHaveBeenSeen() {
807 bool ret = true;
808 for (auto seen : fSeen) {
809 ret &= seen;
810 }
811 return ret;
812 }
813
814 void reset() {
815 sk_bzero(fSeen, sizeof(fSeen));
816 }
817
818 private:
819 skiatest::Reporter* fReporter; // Unowned
820 bool fSeen[3];
821 };
822
823 ChunkReader chunkReader(r);
824
825 // Now read the file with SkCodec.
826 SkAutoTUnref<SkData> data(wStream.copyToData());
827 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data, &chunkReader));
828 REPORTER_ASSERT(r, codec);
829 if (!codec) {
830 return;
831 }
832
833 // Now compare to the original.
834 SkBitmap decodedBm;
835 decodedBm.setInfo(codec->getInfo());
836 decodedBm.allocPixels();
837 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
838 decodedBm.rowBytes());
839 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
840
841 if (decodedBm.colorType() != bm.colorType()) {
842 SkBitmap tmp;
843 bool success = decodedBm.copyTo(&tmp, bm.colorType());
844 REPORTER_ASSERT(r, success);
845 if (!success) {
846 return;
847 }
848
849 tmp.swap(decodedBm);
850 }
851
852 compare_to_good_digest(r, goodDigest, decodedBm);
853 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
854
855 // Decoding again will read the chunks again.
856 chunkReader.reset();
857 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
858 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
859 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
860 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
861}
862#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800863
scroggodb30be22015-12-08 18:54:13 -0800864// Stream that can only peek up to a limit
865class LimitedPeekingMemStream : public SkStream {
866public:
867 LimitedPeekingMemStream(SkData* data, size_t limit)
868 : fStream(data)
869 , fLimit(limit) {}
870
871 size_t peek(void* buf, size_t bytes) const override {
872 return fStream.peek(buf, SkTMin(bytes, fLimit));
873 }
874 size_t read(void* buf, size_t bytes) override {
875 return fStream.read(buf, bytes);
876 }
877 bool rewind() override {
878 return fStream.rewind();
879 }
880 bool isAtEnd() const override {
881 return false;
882 }
883private:
884 SkMemoryStream fStream;
885 const size_t fLimit;
886};
887
888// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
889// + rewind() and succeed.
890DEF_TEST(Codec_webp_peek, r) {
891 const char* path = "baby_tux.webp";
892 SkString fullPath(GetResourcePath(path));
893 SkAutoTUnref<SkData> data(SkData::NewFromFileName(fullPath.c_str()));
894 if (!data) {
895 SkDebugf("Missing resource '%s'\n", path);
896 return;
897 }
898
899 // The limit is less than webp needs to peek or read.
900 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 25)));
901 REPORTER_ASSERT(r, codec);
902
903 test_info(r, codec, codec->getInfo(), SkCodec::kSuccess, nullptr);
904
905 // Similarly, a stream which does not peek should still succeed.
906 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0)));
907 REPORTER_ASSERT(r, codec);
908
909 test_info(r, codec, codec->getInfo(), SkCodec::kSuccess, nullptr);
910}
911
scroggob9a1e342015-11-30 06:25:31 -0800912// SkCodec's wbmp decoder was initially more restrictive than SkImageDecoder.
913// It required the second byte to be zero. But SkImageDecoder allowed a couple
914// of bits to be 1 (so long as they do not overlap with 0x9F). Test that
915// SkCodec now supports an image with these bits set.
916DEF_TEST(Codec_wbmp, r) {
917 const char* path = "mandrill.wbmp";
918 SkAutoTDelete<SkStream> stream(resource(path));
919 if (!stream) {
920 SkDebugf("Missing resource '%s'\n", path);
921 return;
922 }
923
924 // Modify the stream to contain a second byte with some bits set.
925 SkAutoTUnref<SkData> data(SkCopyStreamToData(stream));
926 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
927 writeableData[1] = static_cast<uint8_t>(~0x9F);
928
929 // SkImageDecoder supports this.
930 SkBitmap bitmap;
931 REPORTER_ASSERT(r, SkImageDecoder::DecodeMemory(data->data(), data->size(), &bitmap));
932
933 // So SkCodec should, too.
934 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
935 REPORTER_ASSERT(r, codec);
936 if (!codec) {
937 return;
938 }
939 test_info(r, codec, codec->getInfo(), SkCodec::kSuccess, nullptr);
940}
scroggodb30be22015-12-08 18:54:13 -0800941
942// wbmp images have a header that can be arbitrarily large, depending on the
943// size of the image. We cap the size at 65535, meaning we only need to look at
944// 8 bytes to determine whether we can read the image. This is important
945// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
946// image is a wbmp.
947DEF_TEST(Codec_wbmp_max_size, r) {
948 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
949 0x83, 0xFF, 0x7F, // W: 65535
950 0x83, 0xFF, 0x7F }; // H: 65535
951 SkAutoTDelete<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
952 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
953
954 REPORTER_ASSERT(r, codec);
955 if (!codec) return;
956
957 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
958 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
959
960 // Now test an image which is too big. Any image with a larger header (i.e.
961 // has bigger width/height) is also too big.
962 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
963 0x84, 0x80, 0x00, // W: 65536
964 0x84, 0x80, 0x00 }; // H: 65536
965 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
966 codec.reset(SkCodec::NewFromStream(stream.detach()));
967
968 REPORTER_ASSERT(r, !codec);
969}