blob: 12d6ac8d3d2997a164da8219a4bd39f7923e349b [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"
scroggoef0fed32016-02-18 05:59:25 -080014#include "SkFrontBufferedStream.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) {
halcanary1e903042016-04-25 10:29:36 -070035 md5.write(bm.getAddr(0, y), rowLen);
halcanarya096d7a2015-03-27 12:16:53 -070036 }
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 */
scroggo7b5e5532016-02-04 06:14:24 -080059template<typename Codec>
60static void test_info(skiatest::Reporter* r, Codec* codec, const SkImageInfo& info,
scroggod1bc5742015-08-12 08:31:44 -070061 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
62 SkBitmap bm;
63 bm.allocPixels(info);
64 SkAutoLockPixels autoLockPixels(bm);
65
66 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
67 REPORTER_ASSERT(r, result == expectedResult);
68
69 if (goodDigest) {
70 compare_to_good_digest(r, *goodDigest, bm);
71 }
72}
73
scroggob636b452015-07-22 07:16:20 -070074SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
75 SkIRect rect;
76 do {
77 rect.fLeft = rand->nextRangeU(0, w);
78 rect.fTop = rand->nextRangeU(0, h);
79 rect.fRight = rand->nextRangeU(0, w);
80 rect.fBottom = rand->nextRangeU(0, h);
81 rect.sort();
82 } while (rect.isEmpty());
83 return rect;
84}
85
scroggo7b5e5532016-02-04 06:14:24 -080086template<typename Codec>
87static void test_codec(skiatest::Reporter* r, Codec* codec, SkBitmap& bm, const SkImageInfo& info,
scroggo27c17282015-10-27 08:14:46 -070088 const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest,
89 const SkMD5::Digest* goodDigest) {
msarette6dd0042015-10-09 11:07:34 -070090
halcanarya096d7a2015-03-27 12:16:53 -070091 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -070092 bm.allocPixels(info);
93 SkAutoLockPixels autoLockPixels(bm);
msarettcc7f3052015-10-05 14:20:27 -070094
95 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -070096 REPORTER_ASSERT(r, result == expectedResult);
halcanarya096d7a2015-03-27 12:16:53 -070097
msarettcc7f3052015-10-05 14:20:27 -070098 md5(bm, digest);
99 if (goodDigest) {
100 REPORTER_ASSERT(r, *digest == *goodDigest);
101 }
halcanarya096d7a2015-03-27 12:16:53 -0700102
msarett8ff6ca62015-09-18 12:06:04 -0700103 {
104 // Test decoding to 565
105 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggoba584892016-05-20 13:56:13 -0700106 if (info.alphaType() == kOpaque_SkAlphaType) {
107 // Decoding to 565 should succeed.
108 SkBitmap bm565;
109 bm565.allocPixels(info565);
110 SkAutoLockPixels alp(bm565);
111
112 // This will allow comparison even if the image is incomplete.
113 bm565.eraseColor(SK_ColorBLACK);
114
115 REPORTER_ASSERT(r, expectedResult == codec->getPixels(info565,
116 bm565.getPixels(), bm565.rowBytes()));
117
118 SkMD5::Digest digest565;
119 md5(bm565, &digest565);
120
121 // A dumb client's request for non-opaque should also succeed.
122 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
123 info565 = info565.makeAlphaType(alpha);
124 test_info(r, codec, info565, expectedResult, &digest565);
125 }
126 } else {
127 test_info(r, codec, info565, SkCodec::kInvalidConversion, nullptr);
128 }
129 }
130
131 if (codec->getInfo().colorType() == kGray_8_SkColorType) {
132 SkImageInfo grayInfo = codec->getInfo();
133 SkBitmap grayBm;
134 grayBm.allocPixels(grayInfo);
135 SkAutoLockPixels alp(grayBm);
136
137 grayBm.eraseColor(SK_ColorBLACK);
138
139 REPORTER_ASSERT(r, expectedResult == codec->getPixels(grayInfo,
140 grayBm.getPixels(), grayBm.rowBytes()));
141
142 SkMD5::Digest grayDigest;
143 md5(grayBm, &grayDigest);
144
145 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
146 grayInfo = grayInfo.makeAlphaType(alpha);
147 test_info(r, codec, grayInfo, expectedResult, &grayDigest);
148 }
msarett8ff6ca62015-09-18 12:06:04 -0700149 }
150
151 // Verify that re-decoding gives the same result. It is interesting to check this after
152 // a decode to 565, since choosing to decode to 565 may result in some of the decode
153 // options being modified. These options should return to their defaults on another
154 // decode to kN32, so the new digest should match the old digest.
msarette6dd0042015-10-09 11:07:34 -0700155 test_info(r, codec, info, expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700156
157 {
158 // Check alpha type conversions
159 if (info.alphaType() == kOpaque_SkAlphaType) {
160 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800161 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700162 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800163 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700164 } else {
165 // Decoding to opaque should fail
166 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700167 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700168 SkAlphaType otherAt = info.alphaType();
169 if (kPremul_SkAlphaType == otherAt) {
170 otherAt = kUnpremul_SkAlphaType;
171 } else {
172 otherAt = kPremul_SkAlphaType;
173 }
174 // The other non-opaque alpha type should always succeed, but not match.
msarette6dd0042015-10-09 11:07:34 -0700175 test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700176 }
177 }
msarettcc7f3052015-10-05 14:20:27 -0700178}
179
scroggobed1ed62016-02-11 10:24:55 -0800180static bool supports_partial_scanlines(const char path[]) {
scroggo2c3b2182015-10-09 08:40:59 -0700181 static const char* const exts[] = {
182 "jpg", "jpeg", "png", "webp"
183 "JPG", "JPEG", "PNG", "WEBP"
184 };
185
186 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
187 if (SkStrEndsWith(path, exts[i])) {
188 return true;
189 }
190 }
191 return false;
192}
193
msarettcc7f3052015-10-05 14:20:27 -0700194static void check(skiatest::Reporter* r,
195 const char path[],
196 SkISize size,
197 bool supportsScanlineDecoding,
198 bool supportsSubsetDecoding,
msarette6dd0042015-10-09 11:07:34 -0700199 bool supportsIncomplete = true) {
msarettcc7f3052015-10-05 14:20:27 -0700200
201 SkAutoTDelete<SkStream> stream(resource(path));
202 if (!stream) {
203 SkDebugf("Missing resource '%s'\n", path);
204 return;
205 }
msarette6dd0042015-10-09 11:07:34 -0700206
207 SkAutoTDelete<SkCodec> codec(nullptr);
208 bool isIncomplete = supportsIncomplete;
209 if (isIncomplete) {
210 size_t size = stream->getLength();
211 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
212 codec.reset(SkCodec::NewFromData(data));
213 } else {
mtklein18300a32016-03-16 13:53:35 -0700214 codec.reset(SkCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700215 }
msarettcc7f3052015-10-05 14:20:27 -0700216 if (!codec) {
217 ERRORF(r, "Unable to decode '%s'", path);
218 return;
219 }
220
221 // Test full image decodes with SkCodec
222 SkMD5::Digest codecDigest;
scroggoef0fed32016-02-18 05:59:25 -0800223 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
msarettcc7f3052015-10-05 14:20:27 -0700224 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700225 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
scroggo7b5e5532016-02-04 06:14:24 -0800226 test_codec(r, codec.get(), bm, info, size, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700227
228 // Scanline decoding follows.
msarettcc7f3052015-10-05 14:20:27 -0700229 // Need to call startScanlineDecode() first.
scroggo46c57472015-09-30 08:57:13 -0700230 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700231 == 0);
scroggo46c57472015-09-30 08:57:13 -0700232 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700233 == 0);
scroggo46c57472015-09-30 08:57:13 -0700234
235 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700236 if (supportsScanlineDecoding) {
237 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700238
scroggo46c57472015-09-30 08:57:13 -0700239 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700240
scroggo58421542015-04-01 11:25:20 -0700241 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700242 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
243 if (!isIncomplete) {
244 REPORTER_ASSERT(r, 1 == lines);
245 }
scroggo58421542015-04-01 11:25:20 -0700246 }
247 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700248 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700249 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700250 }
scroggo46c57472015-09-30 08:57:13 -0700251
252 // Cannot continue to decode scanlines beyond the end
253 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700254 == 0);
scroggo46c57472015-09-30 08:57:13 -0700255
256 // Interrupting a scanline decode with a full decode starts from
257 // scratch
258 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700259 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
260 if (!isIncomplete) {
261 REPORTER_ASSERT(r, lines == 1);
262 }
scroggo46c57472015-09-30 08:57:13 -0700263 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700264 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700265 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700266 == 0);
scroggo46c57472015-09-30 08:57:13 -0700267 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700268 == 0);
msarett80803ff2015-10-16 10:54:12 -0700269
270 // Test partial scanline decodes
scroggobed1ed62016-02-11 10:24:55 -0800271 if (supports_partial_scanlines(path) && info.width() >= 3) {
msarett80803ff2015-10-16 10:54:12 -0700272 SkCodec::Options options;
273 int width = info.width();
274 int height = info.height();
275 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
276 options.fSubset = &subset;
277
278 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
279 nullptr, nullptr);
280 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
281
282 for (int y = 0; y < height; y++) {
283 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
284 if (!isIncomplete) {
285 REPORTER_ASSERT(r, 1 == lines);
286 }
287 }
288 }
scroggo58421542015-04-01 11:25:20 -0700289 } else {
scroggo46c57472015-09-30 08:57:13 -0700290 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700291 }
scroggob636b452015-07-22 07:16:20 -0700292
293 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
294 // random subsets.
295 // Do not attempt to decode subsets of an image of only once pixel, since there is no
296 // meaningful subset.
297 if (size.width() * size.height() == 1) {
298 return;
299 }
300
301 SkRandom rand;
302 SkIRect subset;
303 SkCodec::Options opts;
304 opts.fSubset = &subset;
305 for (int i = 0; i < 5; i++) {
306 subset = generate_random_subset(&rand, size.width(), size.height());
307 SkASSERT(!subset.isEmpty());
308 const bool supported = codec->getValidSubset(&subset);
309 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
310
311 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
312 SkBitmap bm;
313 bm.allocPixels(subsetInfo);
314 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700315 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700316
317 if (supportsSubsetDecoding) {
msarette6dd0042015-10-09 11:07:34 -0700318 REPORTER_ASSERT(r, result == expectedResult);
scroggob636b452015-07-22 07:16:20 -0700319 // Webp is the only codec that supports subsets, and it will have modified the subset
320 // to have even left/top.
321 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
322 } else {
323 // No subsets will work.
324 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
325 }
326 }
msarettcc7f3052015-10-05 14:20:27 -0700327
scroggobed1ed62016-02-11 10:24:55 -0800328 // SkAndroidCodec tests
329 if (supportsScanlineDecoding || supportsSubsetDecoding) {
scroggo2c3b2182015-10-09 08:40:59 -0700330
msarettcc7f3052015-10-05 14:20:27 -0700331 SkAutoTDelete<SkStream> stream(resource(path));
332 if (!stream) {
333 SkDebugf("Missing resource '%s'\n", path);
334 return;
335 }
msarette6dd0042015-10-09 11:07:34 -0700336
scroggo7b5e5532016-02-04 06:14:24 -0800337 SkAutoTDelete<SkAndroidCodec> androidCodec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700338 if (isIncomplete) {
339 size_t size = stream->getLength();
340 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
scroggo7b5e5532016-02-04 06:14:24 -0800341 androidCodec.reset(SkAndroidCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700342 } else {
mtklein18300a32016-03-16 13:53:35 -0700343 androidCodec.reset(SkAndroidCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700344 }
scroggo7b5e5532016-02-04 06:14:24 -0800345 if (!androidCodec) {
msarettcc7f3052015-10-05 14:20:27 -0700346 ERRORF(r, "Unable to decode '%s'", path);
347 return;
348 }
349
350 SkBitmap bm;
scroggobed1ed62016-02-11 10:24:55 -0800351 SkMD5::Digest androidCodecDigest;
352 test_codec(r, androidCodec.get(), bm, info, size, expectedResult, &androidCodecDigest,
scroggo7b5e5532016-02-04 06:14:24 -0800353 &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700354 }
355
msarettedd2dcf2016-01-14 13:12:26 -0800356 if (!isIncomplete) {
scroggoef0fed32016-02-18 05:59:25 -0800357 // Test SkCodecImageGenerator
msarettedd2dcf2016-01-14 13:12:26 -0800358 SkAutoTDelete<SkStream> stream(resource(path));
359 SkAutoTUnref<SkData> fullData(SkData::NewFromStream(stream, stream->getLength()));
360 SkAutoTDelete<SkImageGenerator> gen(SkCodecImageGenerator::NewFromEncodedCodec(fullData));
361 SkBitmap bm;
362 bm.allocPixels(info);
363 SkAutoLockPixels autoLockPixels(bm);
364 REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes()));
365 compare_to_good_digest(r, codecDigest, bm);
scroggoef0fed32016-02-18 05:59:25 -0800366
367 // Test using SkFrontBufferedStream, as Android does
368 SkStream* bufferedStream = SkFrontBufferedStream::Create(new SkMemoryStream(fullData),
369 SkCodec::MinBufferedBytesNeeded());
370 REPORTER_ASSERT(r, bufferedStream);
371 codec.reset(SkCodec::NewFromStream(bufferedStream));
372 REPORTER_ASSERT(r, codec);
373 if (codec) {
374 test_info(r, codec.get(), info, SkCodec::kSuccess, &codecDigest);
375 }
msarettedd2dcf2016-01-14 13:12:26 -0800376 }
377
msarette6dd0042015-10-09 11:07:34 -0700378 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
379 if (isIncomplete) {
scroggo27c17282015-10-27 08:14:46 -0700380 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false);
msarettcc7f3052015-10-05 14:20:27 -0700381 }
halcanarya096d7a2015-03-27 12:16:53 -0700382}
383
384DEF_TEST(Codec, r) {
385 // WBMP
msarett99f567e2015-08-05 12:58:26 -0700386 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700387
scroggo6f5e6192015-06-18 12:53:43 -0700388 // WEBP
scroggob636b452015-07-22 07:16:20 -0700389 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true);
390 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true);
391 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true);
scroggo6f5e6192015-06-18 12:53:43 -0700392
halcanarya096d7a2015-03-27 12:16:53 -0700393 // BMP
msarett5406d6f2015-08-31 06:55:13 -0700394 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false);
msarett4946b942016-02-11 08:41:01 -0800395 check(r, "rle.bmp", SkISize::Make(320, 240), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700396
397 // ICO
msarette6dd0042015-10-09 11:07:34 -0700398 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700399 // These two tests examine interestingly different behavior:
400 // Decodes an embedded BMP image
msarettbe8216a2015-12-04 08:00:50 -0800401 check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700402 // Decodes an embedded PNG image
msarettbe8216a2015-12-04 08:00:50 -0800403 check(r, "google_chrome.ico", SkISize::Make(256, 256), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700404
msarett438b2ad2015-04-09 12:43:10 -0700405 // GIF
msarette6dd0042015-10-09 11:07:34 -0700406 // FIXME: We are not ready to test incomplete GIFs
scroggo27c17282015-10-27 08:14:46 -0700407 check(r, "box.gif", SkISize::Make(200, 55), true, false, false);
408 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700409 // randPixels.gif is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700410 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, false);
msarett438b2ad2015-04-09 12:43:10 -0700411
msarette16b04a2015-04-15 07:32:19 -0700412 // JPG
scroggo27c17282015-10-27 08:14:46 -0700413 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false);
scroggob636b452015-07-22 07:16:20 -0700414 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
msarette6dd0042015-10-09 11:07:34 -0700415 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700416 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggob636b452015-07-22 07:16:20 -0700417 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
msarette6dd0042015-10-09 11:07:34 -0700418 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700419 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
msarette16b04a2015-04-15 07:32:19 -0700420
halcanarya096d7a2015-03-27 12:16:53 -0700421 // PNG
scroggo27c17282015-10-27 08:14:46 -0700422 check(r, "arrow.png", SkISize::Make(187, 312), true, false, false);
423 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false, false);
424 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false, false);
425 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false, false);
426 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false, false);
427 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false, false);
428 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false, false);
429 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false, false);
430 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false, false);
431 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false, false);
432 check(r, "plane.png", SkISize::Make(250, 126), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700433 // FIXME: We are not ready to test incomplete interlaced pngs
scroggo27c17282015-10-27 08:14:46 -0700434 check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false, false);
435 check(r, "randPixels.png", SkISize::Make(8, 8), true, false, false);
436 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false, false);
yujieqin916de9f2016-01-25 08:26:16 -0800437
438 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800439// Disable RAW tests for Win32.
440#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800441 check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
ebrauer46d2aa82016-02-17 08:04:00 -0800442 check(r, "sample_1mp_rotated.dng", SkISize::Make(600, 338), false, false, false);
yujieqin9c7a8a42016-02-05 08:21:19 -0800443 check(r, "dng_with_preview.dng", SkISize::Make(600, 338), true, false, false);
msarett02cb4d42016-01-25 11:01:34 -0800444#endif
halcanarya096d7a2015-03-27 12:16:53 -0700445}
scroggo0a7e69c2015-04-03 07:22:22 -0700446
scroggo46c57472015-09-30 08:57:13 -0700447// Test interlaced PNG in stripes, similar to DM's kStripe_Mode
448DEF_TEST(Codec_stripes, r) {
449 const char * path = "plane_interlaced.png";
450 SkAutoTDelete<SkStream> stream(resource(path));
451 if (!stream) {
452 SkDebugf("Missing resource '%s'\n", path);
453 }
454
mtklein18300a32016-03-16 13:53:35 -0700455 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
scroggo46c57472015-09-30 08:57:13 -0700456 REPORTER_ASSERT(r, codec);
457
458 if (!codec) {
459 return;
460 }
461
462 switch (codec->getScanlineOrder()) {
463 case SkCodec::kBottomUp_SkScanlineOrder:
464 case SkCodec::kOutOfOrder_SkScanlineOrder:
465 ERRORF(r, "This scanline order will not match the original.");
466 return;
467 default:
468 break;
469 }
470
471 // Baseline for what the image should look like, using N32.
472 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
473
474 SkBitmap bm;
475 bm.allocPixels(info);
476 SkAutoLockPixels autoLockPixels(bm);
477 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
478 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
479
480 SkMD5::Digest digest;
481 md5(bm, &digest);
482
483 // Now decode in stripes
484 const int height = info.height();
485 const int numStripes = 4;
486 int stripeHeight;
487 int remainingLines;
488 SkTDivMod(height, numStripes, &stripeHeight, &remainingLines);
489
490 bm.eraseColor(SK_ColorYELLOW);
491
492 result = codec->startScanlineDecode(info);
493 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
494
495 // Odd stripes
496 for (int i = 1; i < numStripes; i += 2) {
497 // Skip the even stripes
msarette6dd0042015-10-09 11:07:34 -0700498 bool skipResult = codec->skipScanlines(stripeHeight);
499 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700500
msarette6dd0042015-10-09 11:07:34 -0700501 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700502 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700503 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700504 }
505
506 // Even stripes
507 result = codec->startScanlineDecode(info);
508 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
509
510 for (int i = 0; i < numStripes; i += 2) {
msarette6dd0042015-10-09 11:07:34 -0700511 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700512 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700513 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700514
515 // Skip the odd stripes
516 if (i + 1 < numStripes) {
msarette6dd0042015-10-09 11:07:34 -0700517 bool skipResult = codec->skipScanlines(stripeHeight);
518 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700519 }
520 }
521
522 // Remainder at the end
523 if (remainingLines > 0) {
524 result = codec->startScanlineDecode(info);
525 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
526
msarette6dd0042015-10-09 11:07:34 -0700527 bool skipResult = codec->skipScanlines(height - remainingLines);
528 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700529
msarette6dd0042015-10-09 11:07:34 -0700530 int linesDecoded = codec->getScanlines(bm.getAddr(0, height - remainingLines),
scroggo46c57472015-09-30 08:57:13 -0700531 remainingLines, bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700532 REPORTER_ASSERT(r, linesDecoded == remainingLines);
scroggo46c57472015-09-30 08:57:13 -0700533 }
534
535 compare_to_good_digest(r, digest, bm);
536}
537
scroggo0a7e69c2015-04-03 07:22:22 -0700538static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700539 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700540 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700541 REPORTER_ASSERT(r, !codec);
542
msarett3d9d7a72015-10-21 10:27:10 -0700543 SkAndroidCodec* androidCodec =
544 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
545 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700546}
547
548// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
549// even on failure. Test some bad streams.
550DEF_TEST(Codec_leaks, r) {
551 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
552 const char nonSupportedStream[] = "hello world";
553 // The other strings should look like the beginning of a file type, so we'll call some
554 // internal version of NewFromStream, which must also delete the stream on failure.
555 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
556 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
557 const char emptyWebp[] = "RIFF1234WEBPVP";
558 const char emptyBmp[] = { 'B', 'M' };
559 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
560 const char emptyGif[] = "GIFVER";
561
562 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
563 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
564 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
565 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
566 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
567 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
568 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
569}
msarette16b04a2015-04-15 07:32:19 -0700570
scroggo2c3b2182015-10-09 08:40:59 -0700571DEF_TEST(Codec_null, r) {
scroggobed1ed62016-02-11 10:24:55 -0800572 // Attempting to create an SkCodec or an SkAndroidCodec with null should not
scroggo2c3b2182015-10-09 08:40:59 -0700573 // crash.
574 SkCodec* codec = SkCodec::NewFromStream(nullptr);
575 REPORTER_ASSERT(r, !codec);
576
msarett3d9d7a72015-10-21 10:27:10 -0700577 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
578 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700579}
580
msarette16b04a2015-04-15 07:32:19 -0700581static void test_dimensions(skiatest::Reporter* r, const char path[]) {
582 // Create the codec from the resource file
583 SkAutoTDelete<SkStream> stream(resource(path));
584 if (!stream) {
585 SkDebugf("Missing resource '%s'\n", path);
586 return;
587 }
mtklein18300a32016-03-16 13:53:35 -0700588 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarette16b04a2015-04-15 07:32:19 -0700589 if (!codec) {
590 ERRORF(r, "Unable to create codec '%s'", path);
591 return;
592 }
593
594 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800595 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700596 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700597 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700598 SkImageInfo scaledInfo = codec->getInfo()
599 .makeWH(scaledDims.width(), scaledDims.height())
600 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700601
602 // Set up for the decode
603 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
604 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
605 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
606
msarett3d9d7a72015-10-21 10:27:10 -0700607 SkAndroidCodec::AndroidOptions options;
608 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700609 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700610 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700611 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700612 }
613}
614
615// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
616DEF_TEST(Codec_Dimensions, r) {
617 // JPG
618 test_dimensions(r, "CMYK.jpg");
619 test_dimensions(r, "color_wheel.jpg");
620 test_dimensions(r, "grayscale.jpg");
621 test_dimensions(r, "mandrill_512_q075.jpg");
622 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700623
624 // Decoding small images with very large scaling factors is a potential
625 // source of bugs and crashes. We disable these tests in Gold because
626 // tiny images are not very useful to look at.
627 // Here we make sure that we do not crash or access illegal memory when
628 // performing scaled decodes on small images.
629 test_dimensions(r, "1x1.png");
630 test_dimensions(r, "2x2.png");
631 test_dimensions(r, "3x3.png");
632 test_dimensions(r, "3x1.png");
633 test_dimensions(r, "1x1.png");
634 test_dimensions(r, "16x1.png");
635 test_dimensions(r, "1x16.png");
636 test_dimensions(r, "mandrill_16.png");
637
yujieqin916de9f2016-01-25 08:26:16 -0800638 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800639// Disable RAW tests for Win32.
640#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800641 test_dimensions(r, "sample_1mp.dng");
ebrauer46d2aa82016-02-17 08:04:00 -0800642 test_dimensions(r, "sample_1mp_rotated.dng");
yujieqin9c7a8a42016-02-05 08:21:19 -0800643 test_dimensions(r, "dng_with_preview.dng");
msarett8e49ca32016-01-25 13:10:58 -0800644#endif
msarette16b04a2015-04-15 07:32:19 -0700645}
646
msarettd0375bc2015-08-12 08:08:56 -0700647static void test_invalid(skiatest::Reporter* r, const char path[]) {
msarett4b17fa32015-04-23 08:53:39 -0700648 SkAutoTDelete<SkStream> stream(resource(path));
649 if (!stream) {
650 SkDebugf("Missing resource '%s'\n", path);
651 return;
652 }
mtklein18300a32016-03-16 13:53:35 -0700653 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
halcanary96fcdcc2015-08-27 07:41:13 -0700654 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700655}
msarette16b04a2015-04-15 07:32:19 -0700656
msarett4b17fa32015-04-23 08:53:39 -0700657DEF_TEST(Codec_Empty, r) {
658 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700659 test_invalid(r, "empty_images/zero-dims.gif");
660 test_invalid(r, "empty_images/zero-embedded.ico");
661 test_invalid(r, "empty_images/zero-width.bmp");
662 test_invalid(r, "empty_images/zero-height.bmp");
663 test_invalid(r, "empty_images/zero-width.jpg");
664 test_invalid(r, "empty_images/zero-height.jpg");
665 test_invalid(r, "empty_images/zero-width.png");
666 test_invalid(r, "empty_images/zero-height.png");
667 test_invalid(r, "empty_images/zero-width.wbmp");
668 test_invalid(r, "empty_images/zero-height.wbmp");
669 // This image is an ico with an embedded mask-bmp. This is illegal.
670 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700671}
msarett99f567e2015-08-05 12:58:26 -0700672
673static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
674 SkAutoTDelete<SkStream> stream(resource(path));
675 if (!stream) {
676 SkDebugf("Missing resource '%s'\n", path);
677 return;
678 }
mtklein18300a32016-03-16 13:53:35 -0700679 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.release()));
halcanary9d524f22016-03-29 09:03:52 -0700680
msarett99f567e2015-08-05 12:58:26 -0700681 // This should return kSuccess because kIndex8 is supported.
682 SkPMColor colorStorage[256];
683 int colorCount;
scroggo46c57472015-09-30 08:57:13 -0700684 SkCodec::Result result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700685 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
msarett99f567e2015-08-05 12:58:26 -0700686 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
687 // The rest of the test is uninteresting if kIndex8 is not supported
688 if (SkCodec::kSuccess != result) {
689 return;
690 }
691
692 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
693 // colorPtr and a valid colorCountPtr.
scroggo46c57472015-09-30 08:57:13 -0700694 result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700695 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
msarett99f567e2015-08-05 12:58:26 -0700696 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
scroggo46c57472015-09-30 08:57:13 -0700697 result = decoder->startScanlineDecode(
msarett99f567e2015-08-05 12:58:26 -0700698 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
699 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
700}
701
702DEF_TEST(Codec_Params, r) {
703 test_invalid_parameters(r, "index8.png");
704 test_invalid_parameters(r, "mandrill.wbmp");
705}
scroggocf98fa92015-11-23 08:14:40 -0800706
707static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
708 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
709 if (!sk_stream->write(data, len)) {
710 png_error(png_ptr, "sk_write_fn Error!");
711 }
712}
713
714#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
715DEF_TEST(Codec_pngChunkReader, r) {
716 // Create a dummy bitmap. Use unpremul RGBA for libpng.
717 SkBitmap bm;
718 const int w = 1;
719 const int h = 1;
720 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
721 kUnpremul_SkAlphaType);
722 bm.setInfo(bmInfo);
723 bm.allocPixels();
724 bm.eraseColor(SK_ColorBLUE);
725 SkMD5::Digest goodDigest;
726 md5(bm, &goodDigest);
727
728 // Write to a png file.
729 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
730 REPORTER_ASSERT(r, png);
731 if (!png) {
732 return;
733 }
734
735 png_infop info = png_create_info_struct(png);
736 REPORTER_ASSERT(r, info);
737 if (!info) {
738 png_destroy_write_struct(&png, nullptr);
739 return;
740 }
741
742 if (setjmp(png_jmpbuf(png))) {
743 ERRORF(r, "failed writing png");
744 png_destroy_write_struct(&png, &info);
745 return;
746 }
747
748 SkDynamicMemoryWStream wStream;
749 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
750
751 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
752 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
753 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
754
755 // Create some chunks that match the Android framework's use.
756 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800757 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
758 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
759 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800760 };
761
762 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
763 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
764#if PNG_LIBPNG_VER < 10600
765 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800766 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
767 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800768#endif
769
770 png_write_info(png, info);
771
772 for (int j = 0; j < h; j++) {
773 png_bytep row = (png_bytep)(bm.getAddr(0, j));
774 png_write_rows(png, &row, 1);
775 }
776 png_write_end(png, info);
777 png_destroy_write_struct(&png, &info);
778
779 class ChunkReader : public SkPngChunkReader {
780 public:
781 ChunkReader(skiatest::Reporter* r)
782 : fReporter(r)
783 {
784 this->reset();
785 }
786
787 bool readChunk(const char tag[], const void* data, size_t length) override {
788 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
789 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
790 // Tag matches. This should have been the first time we see it.
791 REPORTER_ASSERT(fReporter, !fSeen[i]);
792 fSeen[i] = true;
793
794 // Data and length should match
795 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
796 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
797 (const char*) gUnknowns[i].data));
798 return true;
799 }
800 }
801 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
802 return true;
803 }
804
805 bool allHaveBeenSeen() {
806 bool ret = true;
807 for (auto seen : fSeen) {
808 ret &= seen;
809 }
810 return ret;
811 }
812
813 void reset() {
814 sk_bzero(fSeen, sizeof(fSeen));
815 }
816
817 private:
818 skiatest::Reporter* fReporter; // Unowned
819 bool fSeen[3];
820 };
821
822 ChunkReader chunkReader(r);
823
824 // Now read the file with SkCodec.
825 SkAutoTUnref<SkData> data(wStream.copyToData());
826 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data, &chunkReader));
827 REPORTER_ASSERT(r, codec);
828 if (!codec) {
829 return;
830 }
831
832 // Now compare to the original.
833 SkBitmap decodedBm;
834 decodedBm.setInfo(codec->getInfo());
835 decodedBm.allocPixels();
836 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
837 decodedBm.rowBytes());
838 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
839
840 if (decodedBm.colorType() != bm.colorType()) {
841 SkBitmap tmp;
842 bool success = decodedBm.copyTo(&tmp, bm.colorType());
843 REPORTER_ASSERT(r, success);
844 if (!success) {
845 return;
846 }
847
848 tmp.swap(decodedBm);
849 }
850
851 compare_to_good_digest(r, goodDigest, decodedBm);
852 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
853
854 // Decoding again will read the chunks again.
855 chunkReader.reset();
856 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
857 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
858 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
859 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
860}
861#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800862
scroggodb30be22015-12-08 18:54:13 -0800863// Stream that can only peek up to a limit
864class LimitedPeekingMemStream : public SkStream {
865public:
866 LimitedPeekingMemStream(SkData* data, size_t limit)
867 : fStream(data)
868 , fLimit(limit) {}
869
870 size_t peek(void* buf, size_t bytes) const override {
871 return fStream.peek(buf, SkTMin(bytes, fLimit));
872 }
873 size_t read(void* buf, size_t bytes) override {
874 return fStream.read(buf, bytes);
875 }
876 bool rewind() override {
877 return fStream.rewind();
878 }
879 bool isAtEnd() const override {
880 return false;
881 }
882private:
883 SkMemoryStream fStream;
884 const size_t fLimit;
885};
886
yujieqin9c7a8a42016-02-05 08:21:19 -0800887// Stream that is not an asset stream (!hasPosition() or !hasLength())
888class NotAssetMemStream : public SkStream {
889public:
890 NotAssetMemStream(SkData* data) : fStream(data) {}
891
892 bool hasPosition() const override {
893 return false;
894 }
895
896 bool hasLength() const override {
897 return false;
898 }
899
900 size_t peek(void* buf, size_t bytes) const override {
901 return fStream.peek(buf, bytes);
902 }
903 size_t read(void* buf, size_t bytes) override {
904 return fStream.read(buf, bytes);
905 }
906 bool rewind() override {
907 return fStream.rewind();
908 }
909 bool isAtEnd() const override {
910 return fStream.isAtEnd();
911 }
912private:
913 SkMemoryStream fStream;
914};
915
yujieqinf236ee42016-02-29 07:14:42 -0800916// Disable RAW tests for Win32.
917#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin9c7a8a42016-02-05 08:21:19 -0800918// Test that the RawCodec works also for not asset stream. This will test the code path using
919// SkRawBufferedStream instead of SkRawAssetStream.
yujieqin9c7a8a42016-02-05 08:21:19 -0800920DEF_TEST(Codec_raw_notseekable, r) {
921 const char* path = "dng_with_preview.dng";
922 SkString fullPath(GetResourcePath(path));
923 SkAutoTUnref<SkData> data(SkData::NewFromFileName(fullPath.c_str()));
924 if (!data) {
925 SkDebugf("Missing resource '%s'\n", path);
926 return;
927 }
928
929 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(data)));
930 REPORTER_ASSERT(r, codec);
931
932 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
933}
934#endif
935
scroggodb30be22015-12-08 18:54:13 -0800936// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
937// + rewind() and succeed.
938DEF_TEST(Codec_webp_peek, r) {
939 const char* path = "baby_tux.webp";
940 SkString fullPath(GetResourcePath(path));
reedfde05112016-03-11 13:02:28 -0800941 auto data = SkData::MakeFromFileName(fullPath.c_str());
scroggodb30be22015-12-08 18:54:13 -0800942 if (!data) {
943 SkDebugf("Missing resource '%s'\n", path);
944 return;
945 }
946
947 // The limit is less than webp needs to peek or read.
reedfde05112016-03-11 13:02:28 -0800948 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(
949 new LimitedPeekingMemStream(data.get(), 25)));
scroggodb30be22015-12-08 18:54:13 -0800950 REPORTER_ASSERT(r, codec);
951
scroggo7b5e5532016-02-04 06:14:24 -0800952 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800953
954 // Similarly, a stream which does not peek should still succeed.
reedfde05112016-03-11 13:02:28 -0800955 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data.get(), 0)));
scroggodb30be22015-12-08 18:54:13 -0800956 REPORTER_ASSERT(r, codec);
957
scroggo7b5e5532016-02-04 06:14:24 -0800958 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800959}
960
msarett7f7ec202016-03-01 12:12:27 -0800961// SkCodec's wbmp decoder was initially unnecessarily restrictive.
962// It required the second byte to be zero. The wbmp specification allows
963// a couple of bits to be 1 (so long as they do not overlap with 0x9F).
964// Test that SkCodec now supports an image with these bits set.
scroggob9a1e342015-11-30 06:25:31 -0800965DEF_TEST(Codec_wbmp, r) {
966 const char* path = "mandrill.wbmp";
967 SkAutoTDelete<SkStream> stream(resource(path));
968 if (!stream) {
969 SkDebugf("Missing resource '%s'\n", path);
970 return;
971 }
972
973 // Modify the stream to contain a second byte with some bits set.
reedfde05112016-03-11 13:02:28 -0800974 auto data = SkCopyStreamToData(stream);
scroggob9a1e342015-11-30 06:25:31 -0800975 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
976 writeableData[1] = static_cast<uint8_t>(~0x9F);
977
msarett7f7ec202016-03-01 12:12:27 -0800978 // SkCodec should support this.
reedfde05112016-03-11 13:02:28 -0800979 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data.get()));
scroggob9a1e342015-11-30 06:25:31 -0800980 REPORTER_ASSERT(r, codec);
981 if (!codec) {
982 return;
983 }
scroggo7b5e5532016-02-04 06:14:24 -0800984 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800985}
scroggodb30be22015-12-08 18:54:13 -0800986
987// wbmp images have a header that can be arbitrarily large, depending on the
988// size of the image. We cap the size at 65535, meaning we only need to look at
989// 8 bytes to determine whether we can read the image. This is important
990// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
991// image is a wbmp.
992DEF_TEST(Codec_wbmp_max_size, r) {
993 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
994 0x83, 0xFF, 0x7F, // W: 65535
995 0x83, 0xFF, 0x7F }; // H: 65535
996 SkAutoTDelete<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
mtklein18300a32016-03-16 13:53:35 -0700997 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -0800998
999 REPORTER_ASSERT(r, codec);
1000 if (!codec) return;
1001
1002 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
1003 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
1004
1005 // Now test an image which is too big. Any image with a larger header (i.e.
1006 // has bigger width/height) is also too big.
1007 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
1008 0x84, 0x80, 0x00, // W: 65536
1009 0x84, 0x80, 0x00 }; // H: 65536
1010 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
mtklein18300a32016-03-16 13:53:35 -07001011 codec.reset(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -08001012
1013 REPORTER_ASSERT(r, !codec);
1014}