blob: 1f6b7ac4c13fe068be168b8f51f1fc595228f201 [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),
scroggoc5560be2016-02-03 09:42:42 -0800133 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700134 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800135 expectedResult, digest);
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),
scroggoc5560be2016-02-03 09:42:42 -0800186 expectedResult, digest);
msarett3d9d7a72015-10-21 10:27:10 -0700187 test_android_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800188 expectedResult, digest);
msarett3d9d7a72015-10-21 10:27:10 -0700189 } 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);
yujieqin916de9f2016-01-25 08:26:16 -0800453
454 // RAW
msarett02cb4d42016-01-25 11:01:34 -0800455#if defined(SK_CODEC_DECODES_RAW)
yujieqin916de9f2016-01-25 08:26:16 -0800456 check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
msarett02cb4d42016-01-25 11:01:34 -0800457#endif
halcanarya096d7a2015-03-27 12:16:53 -0700458}
scroggo0a7e69c2015-04-03 07:22:22 -0700459
scroggo46c57472015-09-30 08:57:13 -0700460// Test interlaced PNG in stripes, similar to DM's kStripe_Mode
461DEF_TEST(Codec_stripes, r) {
462 const char * path = "plane_interlaced.png";
463 SkAutoTDelete<SkStream> stream(resource(path));
464 if (!stream) {
465 SkDebugf("Missing resource '%s'\n", path);
466 }
467
468 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
469 REPORTER_ASSERT(r, codec);
470
471 if (!codec) {
472 return;
473 }
474
475 switch (codec->getScanlineOrder()) {
476 case SkCodec::kBottomUp_SkScanlineOrder:
477 case SkCodec::kOutOfOrder_SkScanlineOrder:
478 ERRORF(r, "This scanline order will not match the original.");
479 return;
480 default:
481 break;
482 }
483
484 // Baseline for what the image should look like, using N32.
485 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
486
487 SkBitmap bm;
488 bm.allocPixels(info);
489 SkAutoLockPixels autoLockPixels(bm);
490 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
491 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
492
493 SkMD5::Digest digest;
494 md5(bm, &digest);
495
496 // Now decode in stripes
497 const int height = info.height();
498 const int numStripes = 4;
499 int stripeHeight;
500 int remainingLines;
501 SkTDivMod(height, numStripes, &stripeHeight, &remainingLines);
502
503 bm.eraseColor(SK_ColorYELLOW);
504
505 result = codec->startScanlineDecode(info);
506 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
507
508 // Odd stripes
509 for (int i = 1; i < numStripes; i += 2) {
510 // Skip the even stripes
msarette6dd0042015-10-09 11:07:34 -0700511 bool skipResult = codec->skipScanlines(stripeHeight);
512 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700513
msarette6dd0042015-10-09 11:07:34 -0700514 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700515 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700516 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700517 }
518
519 // Even stripes
520 result = codec->startScanlineDecode(info);
521 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
522
523 for (int i = 0; i < numStripes; i += 2) {
msarette6dd0042015-10-09 11:07:34 -0700524 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700525 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700526 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700527
528 // Skip the odd stripes
529 if (i + 1 < numStripes) {
msarette6dd0042015-10-09 11:07:34 -0700530 bool skipResult = codec->skipScanlines(stripeHeight);
531 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700532 }
533 }
534
535 // Remainder at the end
536 if (remainingLines > 0) {
537 result = codec->startScanlineDecode(info);
538 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
539
msarette6dd0042015-10-09 11:07:34 -0700540 bool skipResult = codec->skipScanlines(height - remainingLines);
541 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700542
msarette6dd0042015-10-09 11:07:34 -0700543 int linesDecoded = codec->getScanlines(bm.getAddr(0, height - remainingLines),
scroggo46c57472015-09-30 08:57:13 -0700544 remainingLines, bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700545 REPORTER_ASSERT(r, linesDecoded == remainingLines);
scroggo46c57472015-09-30 08:57:13 -0700546 }
547
548 compare_to_good_digest(r, digest, bm);
549}
550
scroggo0a7e69c2015-04-03 07:22:22 -0700551static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700552 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700553 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700554 REPORTER_ASSERT(r, !codec);
555
msarett3d9d7a72015-10-21 10:27:10 -0700556 SkAndroidCodec* androidCodec =
557 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
558 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700559}
560
561// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
562// even on failure. Test some bad streams.
563DEF_TEST(Codec_leaks, r) {
564 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
565 const char nonSupportedStream[] = "hello world";
566 // The other strings should look like the beginning of a file type, so we'll call some
567 // internal version of NewFromStream, which must also delete the stream on failure.
568 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
569 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
570 const char emptyWebp[] = "RIFF1234WEBPVP";
571 const char emptyBmp[] = { 'B', 'M' };
572 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
573 const char emptyGif[] = "GIFVER";
574
575 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
576 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
577 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
578 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
579 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
580 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
581 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
582}
msarette16b04a2015-04-15 07:32:19 -0700583
scroggo2c3b2182015-10-09 08:40:59 -0700584DEF_TEST(Codec_null, r) {
585 // Attempting to create an SkCodec or an SkScaledCodec with null should not
586 // crash.
587 SkCodec* codec = SkCodec::NewFromStream(nullptr);
588 REPORTER_ASSERT(r, !codec);
589
msarett3d9d7a72015-10-21 10:27:10 -0700590 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
591 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700592}
593
msarette16b04a2015-04-15 07:32:19 -0700594static void test_dimensions(skiatest::Reporter* r, const char path[]) {
595 // Create the codec from the resource file
596 SkAutoTDelete<SkStream> stream(resource(path));
597 if (!stream) {
598 SkDebugf("Missing resource '%s'\n", path);
599 return;
600 }
msarett3d9d7a72015-10-21 10:27:10 -0700601 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.detach()));
msarette16b04a2015-04-15 07:32:19 -0700602 if (!codec) {
603 ERRORF(r, "Unable to create codec '%s'", path);
604 return;
605 }
606
607 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800608 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700609 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700610 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700611 SkImageInfo scaledInfo = codec->getInfo()
612 .makeWH(scaledDims.width(), scaledDims.height())
613 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700614
615 // Set up for the decode
616 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
617 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
618 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
619
msarett3d9d7a72015-10-21 10:27:10 -0700620 SkAndroidCodec::AndroidOptions options;
621 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700622 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700623 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700624 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700625 }
626}
627
628// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
629DEF_TEST(Codec_Dimensions, r) {
630 // JPG
631 test_dimensions(r, "CMYK.jpg");
632 test_dimensions(r, "color_wheel.jpg");
633 test_dimensions(r, "grayscale.jpg");
634 test_dimensions(r, "mandrill_512_q075.jpg");
635 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700636
637 // Decoding small images with very large scaling factors is a potential
638 // source of bugs and crashes. We disable these tests in Gold because
639 // tiny images are not very useful to look at.
640 // Here we make sure that we do not crash or access illegal memory when
641 // performing scaled decodes on small images.
642 test_dimensions(r, "1x1.png");
643 test_dimensions(r, "2x2.png");
644 test_dimensions(r, "3x3.png");
645 test_dimensions(r, "3x1.png");
646 test_dimensions(r, "1x1.png");
647 test_dimensions(r, "16x1.png");
648 test_dimensions(r, "1x16.png");
649 test_dimensions(r, "mandrill_16.png");
650
yujieqin916de9f2016-01-25 08:26:16 -0800651 // RAW
msarett8e49ca32016-01-25 13:10:58 -0800652#if defined(SK_CODEC_DECODES_RAW)
yujieqin916de9f2016-01-25 08:26:16 -0800653 test_dimensions(r, "sample_1mp.dng");
msarett8e49ca32016-01-25 13:10:58 -0800654#endif
msarette16b04a2015-04-15 07:32:19 -0700655}
656
msarettd0375bc2015-08-12 08:08:56 -0700657static void test_invalid(skiatest::Reporter* r, const char path[]) {
msarett4b17fa32015-04-23 08:53:39 -0700658 SkAutoTDelete<SkStream> stream(resource(path));
659 if (!stream) {
660 SkDebugf("Missing resource '%s'\n", path);
661 return;
662 }
663 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700664 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700665}
msarette16b04a2015-04-15 07:32:19 -0700666
msarett4b17fa32015-04-23 08:53:39 -0700667DEF_TEST(Codec_Empty, r) {
668 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700669 test_invalid(r, "empty_images/zero-dims.gif");
670 test_invalid(r, "empty_images/zero-embedded.ico");
671 test_invalid(r, "empty_images/zero-width.bmp");
672 test_invalid(r, "empty_images/zero-height.bmp");
673 test_invalid(r, "empty_images/zero-width.jpg");
674 test_invalid(r, "empty_images/zero-height.jpg");
675 test_invalid(r, "empty_images/zero-width.png");
676 test_invalid(r, "empty_images/zero-height.png");
677 test_invalid(r, "empty_images/zero-width.wbmp");
678 test_invalid(r, "empty_images/zero-height.wbmp");
679 // This image is an ico with an embedded mask-bmp. This is illegal.
680 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700681}
msarett99f567e2015-08-05 12:58:26 -0700682
683static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
684 SkAutoTDelete<SkStream> stream(resource(path));
685 if (!stream) {
686 SkDebugf("Missing resource '%s'\n", path);
687 return;
688 }
scroggo46c57472015-09-30 08:57:13 -0700689 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.detach()));
msarett99f567e2015-08-05 12:58:26 -0700690
691 // This should return kSuccess because kIndex8 is supported.
692 SkPMColor colorStorage[256];
693 int colorCount;
scroggo46c57472015-09-30 08:57:13 -0700694 SkCodec::Result result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700695 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
msarett99f567e2015-08-05 12:58:26 -0700696 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
697 // The rest of the test is uninteresting if kIndex8 is not supported
698 if (SkCodec::kSuccess != result) {
699 return;
700 }
701
702 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
703 // colorPtr and a valid colorCountPtr.
scroggo46c57472015-09-30 08:57:13 -0700704 result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700705 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
msarett99f567e2015-08-05 12:58:26 -0700706 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
scroggo46c57472015-09-30 08:57:13 -0700707 result = decoder->startScanlineDecode(
msarett99f567e2015-08-05 12:58:26 -0700708 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
709 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
710}
711
712DEF_TEST(Codec_Params, r) {
713 test_invalid_parameters(r, "index8.png");
714 test_invalid_parameters(r, "mandrill.wbmp");
715}
scroggocf98fa92015-11-23 08:14:40 -0800716
717static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
718 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
719 if (!sk_stream->write(data, len)) {
720 png_error(png_ptr, "sk_write_fn Error!");
721 }
722}
723
724#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
725DEF_TEST(Codec_pngChunkReader, r) {
726 // Create a dummy bitmap. Use unpremul RGBA for libpng.
727 SkBitmap bm;
728 const int w = 1;
729 const int h = 1;
730 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
731 kUnpremul_SkAlphaType);
732 bm.setInfo(bmInfo);
733 bm.allocPixels();
734 bm.eraseColor(SK_ColorBLUE);
735 SkMD5::Digest goodDigest;
736 md5(bm, &goodDigest);
737
738 // Write to a png file.
739 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
740 REPORTER_ASSERT(r, png);
741 if (!png) {
742 return;
743 }
744
745 png_infop info = png_create_info_struct(png);
746 REPORTER_ASSERT(r, info);
747 if (!info) {
748 png_destroy_write_struct(&png, nullptr);
749 return;
750 }
751
752 if (setjmp(png_jmpbuf(png))) {
753 ERRORF(r, "failed writing png");
754 png_destroy_write_struct(&png, &info);
755 return;
756 }
757
758 SkDynamicMemoryWStream wStream;
759 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
760
761 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
762 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
763 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
764
765 // Create some chunks that match the Android framework's use.
766 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800767 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
768 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
769 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800770 };
771
772 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
773 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
774#if PNG_LIBPNG_VER < 10600
775 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800776 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
777 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800778#endif
779
780 png_write_info(png, info);
781
782 for (int j = 0; j < h; j++) {
783 png_bytep row = (png_bytep)(bm.getAddr(0, j));
784 png_write_rows(png, &row, 1);
785 }
786 png_write_end(png, info);
787 png_destroy_write_struct(&png, &info);
788
789 class ChunkReader : public SkPngChunkReader {
790 public:
791 ChunkReader(skiatest::Reporter* r)
792 : fReporter(r)
793 {
794 this->reset();
795 }
796
797 bool readChunk(const char tag[], const void* data, size_t length) override {
798 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
799 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
800 // Tag matches. This should have been the first time we see it.
801 REPORTER_ASSERT(fReporter, !fSeen[i]);
802 fSeen[i] = true;
803
804 // Data and length should match
805 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
806 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
807 (const char*) gUnknowns[i].data));
808 return true;
809 }
810 }
811 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
812 return true;
813 }
814
815 bool allHaveBeenSeen() {
816 bool ret = true;
817 for (auto seen : fSeen) {
818 ret &= seen;
819 }
820 return ret;
821 }
822
823 void reset() {
824 sk_bzero(fSeen, sizeof(fSeen));
825 }
826
827 private:
828 skiatest::Reporter* fReporter; // Unowned
829 bool fSeen[3];
830 };
831
832 ChunkReader chunkReader(r);
833
834 // Now read the file with SkCodec.
835 SkAutoTUnref<SkData> data(wStream.copyToData());
836 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data, &chunkReader));
837 REPORTER_ASSERT(r, codec);
838 if (!codec) {
839 return;
840 }
841
842 // Now compare to the original.
843 SkBitmap decodedBm;
844 decodedBm.setInfo(codec->getInfo());
845 decodedBm.allocPixels();
846 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
847 decodedBm.rowBytes());
848 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
849
850 if (decodedBm.colorType() != bm.colorType()) {
851 SkBitmap tmp;
852 bool success = decodedBm.copyTo(&tmp, bm.colorType());
853 REPORTER_ASSERT(r, success);
854 if (!success) {
855 return;
856 }
857
858 tmp.swap(decodedBm);
859 }
860
861 compare_to_good_digest(r, goodDigest, decodedBm);
862 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
863
864 // Decoding again will read the chunks again.
865 chunkReader.reset();
866 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
867 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
868 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
869 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
870}
871#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800872
scroggodb30be22015-12-08 18:54:13 -0800873// Stream that can only peek up to a limit
874class LimitedPeekingMemStream : public SkStream {
875public:
876 LimitedPeekingMemStream(SkData* data, size_t limit)
877 : fStream(data)
878 , fLimit(limit) {}
879
880 size_t peek(void* buf, size_t bytes) const override {
881 return fStream.peek(buf, SkTMin(bytes, fLimit));
882 }
883 size_t read(void* buf, size_t bytes) override {
884 return fStream.read(buf, bytes);
885 }
886 bool rewind() override {
887 return fStream.rewind();
888 }
889 bool isAtEnd() const override {
890 return false;
891 }
892private:
893 SkMemoryStream fStream;
894 const size_t fLimit;
895};
896
897// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
898// + rewind() and succeed.
899DEF_TEST(Codec_webp_peek, r) {
900 const char* path = "baby_tux.webp";
901 SkString fullPath(GetResourcePath(path));
902 SkAutoTUnref<SkData> data(SkData::NewFromFileName(fullPath.c_str()));
903 if (!data) {
904 SkDebugf("Missing resource '%s'\n", path);
905 return;
906 }
907
908 // The limit is less than webp needs to peek or read.
909 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 25)));
910 REPORTER_ASSERT(r, codec);
911
912 test_info(r, codec, codec->getInfo(), SkCodec::kSuccess, nullptr);
913
914 // Similarly, a stream which does not peek should still succeed.
915 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0)));
916 REPORTER_ASSERT(r, codec);
917
918 test_info(r, codec, codec->getInfo(), SkCodec::kSuccess, nullptr);
919}
920
scroggob9a1e342015-11-30 06:25:31 -0800921// SkCodec's wbmp decoder was initially more restrictive than SkImageDecoder.
922// It required the second byte to be zero. But SkImageDecoder allowed a couple
923// of bits to be 1 (so long as they do not overlap with 0x9F). Test that
924// SkCodec now supports an image with these bits set.
925DEF_TEST(Codec_wbmp, r) {
926 const char* path = "mandrill.wbmp";
927 SkAutoTDelete<SkStream> stream(resource(path));
928 if (!stream) {
929 SkDebugf("Missing resource '%s'\n", path);
930 return;
931 }
932
933 // Modify the stream to contain a second byte with some bits set.
934 SkAutoTUnref<SkData> data(SkCopyStreamToData(stream));
935 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
936 writeableData[1] = static_cast<uint8_t>(~0x9F);
937
938 // SkImageDecoder supports this.
939 SkBitmap bitmap;
940 REPORTER_ASSERT(r, SkImageDecoder::DecodeMemory(data->data(), data->size(), &bitmap));
941
942 // So SkCodec should, too.
943 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
944 REPORTER_ASSERT(r, codec);
945 if (!codec) {
946 return;
947 }
948 test_info(r, codec, codec->getInfo(), SkCodec::kSuccess, nullptr);
949}
scroggodb30be22015-12-08 18:54:13 -0800950
951// wbmp images have a header that can be arbitrarily large, depending on the
952// size of the image. We cap the size at 65535, meaning we only need to look at
953// 8 bytes to determine whether we can read the image. This is important
954// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
955// image is a wbmp.
956DEF_TEST(Codec_wbmp_max_size, r) {
957 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
958 0x83, 0xFF, 0x7F, // W: 65535
959 0x83, 0xFF, 0x7F }; // H: 65535
960 SkAutoTDelete<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
961 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
962
963 REPORTER_ASSERT(r, codec);
964 if (!codec) return;
965
966 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
967 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
968
969 // Now test an image which is too big. Any image with a larger header (i.e.
970 // has bigger width/height) is also too big.
971 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
972 0x84, 0x80, 0x00, // W: 65536
973 0x84, 0x80, 0x00 }; // H: 65536
974 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
975 codec.reset(SkCodec::NewFromStream(stream.detach()));
976
977 REPORTER_ASSERT(r, !codec);
978}