blob: 7e1b594791f5315e8c622fedb815e3c4a463372d [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,
scroggod8d68552016-06-06 11:26:17 -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();
bungeman38d909e2016-08-02 14:40:46 -0700211 sk_sp<SkData> data((SkData::MakeFromStream(stream, 2 * size / 3)));
212 codec.reset(SkCodec::NewFromData(data.get()));
msarette6dd0042015-10-09 11:07:34 -0700213 } 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.
scroggo6fb23912016-06-02 14:16:43 -0700229 // Need to call startScanlineDecode() first.
scroggod8d68552016-06-06 11:26:17 -0700230 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
231 == 0);
232 REPORTER_ASSERT(r, codec->skipScanlines(1)
233 == 0);
234
scroggo46c57472015-09-30 08:57:13 -0700235 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
scroggod8d68552016-06-06 11:26:17 -0700329 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();
bungeman38d909e2016-08-02 14:40:46 -0700340 sk_sp<SkData> data((SkData::MakeFromStream(stream, 2 * size / 3)));
341 androidCodec.reset(SkAndroidCodec::NewFromData(data.get()));
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));
bungeman38d909e2016-08-02 14:40:46 -0700359 sk_sp<SkData> fullData(SkData::MakeFromStream(stream, stream->getLength()));
360 SkAutoTDelete<SkImageGenerator> gen(
361 SkCodecImageGenerator::NewFromEncodedCodec(fullData.get()));
msarettedd2dcf2016-01-14 13:12:26 -0800362 SkBitmap bm;
363 bm.allocPixels(info);
364 SkAutoLockPixels autoLockPixels(bm);
365 REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes()));
366 compare_to_good_digest(r, codecDigest, bm);
scroggoef0fed32016-02-18 05:59:25 -0800367
scroggod8d68552016-06-06 11:26:17 -0700368 // Test using SkFrontBufferedStream, as Android does
bungeman38d909e2016-08-02 14:40:46 -0700369 SkStream* bufferedStream = SkFrontBufferedStream::Create(
370 new SkMemoryStream(std::move(fullData)), SkCodec::MinBufferedBytesNeeded());
scroggod8d68552016-06-06 11:26:17 -0700371 REPORTER_ASSERT(r, bufferedStream);
372 codec.reset(SkCodec::NewFromStream(bufferedStream));
373 REPORTER_ASSERT(r, codec);
374 if (codec) {
375 test_info(r, codec.get(), info, SkCodec::kSuccess, &codecDigest);
scroggoef0fed32016-02-18 05:59:25 -0800376 }
msarettedd2dcf2016-01-14 13:12:26 -0800377 }
378
msarette6dd0042015-10-09 11:07:34 -0700379 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
380 if (isIncomplete) {
scroggo27c17282015-10-27 08:14:46 -0700381 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false);
msarettcc7f3052015-10-05 14:20:27 -0700382 }
halcanarya096d7a2015-03-27 12:16:53 -0700383}
384
385DEF_TEST(Codec, r) {
386 // WBMP
scroggod8d68552016-06-06 11:26:17 -0700387 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700388
scroggo6f5e6192015-06-18 12:53:43 -0700389 // WEBP
scroggod8d68552016-06-06 11:26:17 -0700390 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true);
391 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true);
392 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true);
scroggo6f5e6192015-06-18 12:53:43 -0700393
halcanarya096d7a2015-03-27 12:16:53 -0700394 // BMP
scroggod8d68552016-06-06 11:26:17 -0700395 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false);
396 check(r, "rle.bmp", SkISize::Make(320, 240), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700397
398 // ICO
msarette6dd0042015-10-09 11:07:34 -0700399 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700400 // These two tests examine interestingly different behavior:
401 // Decodes an embedded BMP image
msarettbe8216a2015-12-04 08:00:50 -0800402 check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700403 // Decodes an embedded PNG image
scroggod8d68552016-06-06 11:26:17 -0700404 check(r, "google_chrome.ico", SkISize::Make(256, 256), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700405
msarett438b2ad2015-04-09 12:43:10 -0700406 // GIF
msarette6dd0042015-10-09 11:07:34 -0700407 // FIXME: We are not ready to test incomplete GIFs
scroggo27c17282015-10-27 08:14:46 -0700408 check(r, "box.gif", SkISize::Make(200, 55), true, false, false);
409 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700410 // randPixels.gif is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700411 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, false);
msarett438b2ad2015-04-09 12:43:10 -0700412
msarette16b04a2015-04-15 07:32:19 -0700413 // JPG
scroggod8d68552016-06-06 11:26:17 -0700414 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false);
415 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
msarette6dd0042015-10-09 11:07:34 -0700416 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700417 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggod8d68552016-06-06 11:26:17 -0700418 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
msarette6dd0042015-10-09 11:07:34 -0700419 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700420 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
msarette16b04a2015-04-15 07:32:19 -0700421
halcanarya096d7a2015-03-27 12:16:53 -0700422 // PNG
scroggod8d68552016-06-06 11:26:17 -0700423 check(r, "arrow.png", SkISize::Make(187, 312), true, false, false);
424 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false, false);
425 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false, false);
426 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false, false);
427 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false, false);
428 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false, false);
429 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false, false);
430 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false, false);
431 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false, false);
432 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false, false);
433 check(r, "plane.png", SkISize::Make(250, 126), true, false, false);
434 // FIXME: We are not ready to test incomplete interlaced pngs
435 check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false, false);
436 check(r, "randPixels.png", SkISize::Make(8, 8), true, false, false);
437 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false, false);
yujieqin916de9f2016-01-25 08:26:16 -0800438
439 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800440// Disable RAW tests for Win32.
441#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800442 check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
ebrauer46d2aa82016-02-17 08:04:00 -0800443 check(r, "sample_1mp_rotated.dng", SkISize::Make(600, 338), false, false, false);
yujieqin9c7a8a42016-02-05 08:21:19 -0800444 check(r, "dng_with_preview.dng", SkISize::Make(600, 338), true, false, false);
msarett02cb4d42016-01-25 11:01:34 -0800445#endif
halcanarya096d7a2015-03-27 12:16:53 -0700446}
scroggo0a7e69c2015-04-03 07:22:22 -0700447
scroggod8d68552016-06-06 11:26:17 -0700448// Test interlaced PNG in stripes, similar to DM's kStripe_Mode
449DEF_TEST(Codec_stripes, r) {
450 const char * path = "plane_interlaced.png";
451 SkAutoTDelete<SkStream> stream(resource(path));
452 if (!stream) {
453 SkDebugf("Missing resource '%s'\n", path);
454 }
455
456 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
457 REPORTER_ASSERT(r, codec);
458
459 if (!codec) {
460 return;
461 }
462
463 switch (codec->getScanlineOrder()) {
464 case SkCodec::kBottomUp_SkScanlineOrder:
465 case SkCodec::kOutOfOrder_SkScanlineOrder:
466 ERRORF(r, "This scanline order will not match the original.");
467 return;
468 default:
469 break;
470 }
471
472 // Baseline for what the image should look like, using N32.
473 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
474
475 SkBitmap bm;
476 bm.allocPixels(info);
477 SkAutoLockPixels autoLockPixels(bm);
478 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
479 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
480
481 SkMD5::Digest digest;
482 md5(bm, &digest);
483
484 // Now decode in stripes
485 const int height = info.height();
486 const int numStripes = 4;
487 int stripeHeight;
488 int remainingLines;
489 SkTDivMod(height, numStripes, &stripeHeight, &remainingLines);
490
491 bm.eraseColor(SK_ColorYELLOW);
492
493 result = codec->startScanlineDecode(info);
494 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
495
496 // Odd stripes
497 for (int i = 1; i < numStripes; i += 2) {
498 // Skip the even stripes
499 bool skipResult = codec->skipScanlines(stripeHeight);
500 REPORTER_ASSERT(r, skipResult);
501
502 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
503 bm.rowBytes());
504 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
505 }
506
507 // Even stripes
508 result = codec->startScanlineDecode(info);
509 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
510
511 for (int i = 0; i < numStripes; i += 2) {
512 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
513 bm.rowBytes());
514 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
515
516 // Skip the odd stripes
517 if (i + 1 < numStripes) {
518 bool skipResult = codec->skipScanlines(stripeHeight);
519 REPORTER_ASSERT(r, skipResult);
520 }
521 }
522
523 // Remainder at the end
524 if (remainingLines > 0) {
525 result = codec->startScanlineDecode(info);
526 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
527
528 bool skipResult = codec->skipScanlines(height - remainingLines);
529 REPORTER_ASSERT(r, skipResult);
530
531 int linesDecoded = codec->getScanlines(bm.getAddr(0, height - remainingLines),
532 remainingLines, bm.rowBytes());
533 REPORTER_ASSERT(r, linesDecoded == remainingLines);
534 }
535
536 compare_to_good_digest(r, digest, bm);
537}
538
scroggo0a7e69c2015-04-03 07:22:22 -0700539static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700540 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700541 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700542 REPORTER_ASSERT(r, !codec);
543
msarett3d9d7a72015-10-21 10:27:10 -0700544 SkAndroidCodec* androidCodec =
545 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
546 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700547}
548
549// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
550// even on failure. Test some bad streams.
551DEF_TEST(Codec_leaks, r) {
552 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
553 const char nonSupportedStream[] = "hello world";
554 // The other strings should look like the beginning of a file type, so we'll call some
555 // internal version of NewFromStream, which must also delete the stream on failure.
556 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
557 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
558 const char emptyWebp[] = "RIFF1234WEBPVP";
559 const char emptyBmp[] = { 'B', 'M' };
560 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
561 const char emptyGif[] = "GIFVER";
562
563 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
564 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
565 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
566 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
567 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
568 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
569 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
570}
msarette16b04a2015-04-15 07:32:19 -0700571
scroggo2c3b2182015-10-09 08:40:59 -0700572DEF_TEST(Codec_null, r) {
scroggobed1ed62016-02-11 10:24:55 -0800573 // Attempting to create an SkCodec or an SkAndroidCodec with null should not
scroggo2c3b2182015-10-09 08:40:59 -0700574 // crash.
575 SkCodec* codec = SkCodec::NewFromStream(nullptr);
576 REPORTER_ASSERT(r, !codec);
577
msarett3d9d7a72015-10-21 10:27:10 -0700578 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
579 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700580}
581
msarette16b04a2015-04-15 07:32:19 -0700582static void test_dimensions(skiatest::Reporter* r, const char path[]) {
583 // Create the codec from the resource file
584 SkAutoTDelete<SkStream> stream(resource(path));
585 if (!stream) {
586 SkDebugf("Missing resource '%s'\n", path);
587 return;
588 }
mtklein18300a32016-03-16 13:53:35 -0700589 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarette16b04a2015-04-15 07:32:19 -0700590 if (!codec) {
591 ERRORF(r, "Unable to create codec '%s'", path);
592 return;
593 }
594
595 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800596 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700597 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700598 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700599 SkImageInfo scaledInfo = codec->getInfo()
600 .makeWH(scaledDims.width(), scaledDims.height())
601 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700602
603 // Set up for the decode
604 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
605 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
606 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
607
msarett3d9d7a72015-10-21 10:27:10 -0700608 SkAndroidCodec::AndroidOptions options;
609 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700610 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700611 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700612 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700613 }
614}
615
616// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
617DEF_TEST(Codec_Dimensions, r) {
618 // JPG
619 test_dimensions(r, "CMYK.jpg");
620 test_dimensions(r, "color_wheel.jpg");
621 test_dimensions(r, "grayscale.jpg");
622 test_dimensions(r, "mandrill_512_q075.jpg");
623 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700624
625 // Decoding small images with very large scaling factors is a potential
626 // source of bugs and crashes. We disable these tests in Gold because
627 // tiny images are not very useful to look at.
628 // Here we make sure that we do not crash or access illegal memory when
629 // performing scaled decodes on small images.
630 test_dimensions(r, "1x1.png");
631 test_dimensions(r, "2x2.png");
632 test_dimensions(r, "3x3.png");
633 test_dimensions(r, "3x1.png");
634 test_dimensions(r, "1x1.png");
635 test_dimensions(r, "16x1.png");
636 test_dimensions(r, "1x16.png");
637 test_dimensions(r, "mandrill_16.png");
638
yujieqin916de9f2016-01-25 08:26:16 -0800639 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800640// Disable RAW tests for Win32.
641#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800642 test_dimensions(r, "sample_1mp.dng");
ebrauer46d2aa82016-02-17 08:04:00 -0800643 test_dimensions(r, "sample_1mp_rotated.dng");
yujieqin9c7a8a42016-02-05 08:21:19 -0800644 test_dimensions(r, "dng_with_preview.dng");
msarett8e49ca32016-01-25 13:10:58 -0800645#endif
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 }
mtklein18300a32016-03-16 13:53:35 -0700654 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
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 }
mtklein18300a32016-03-16 13:53:35 -0700680 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.release()));
halcanary9d524f22016-03-29 09:03:52 -0700681
msarett99f567e2015-08-05 12:58:26 -0700682 // This should return kSuccess because kIndex8 is supported.
683 SkPMColor colorStorage[256];
684 int colorCount;
scroggod8d68552016-06-06 11:26:17 -0700685 SkCodec::Result result = decoder->startScanlineDecode(
686 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
687 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
688 // The rest of the test is uninteresting if kIndex8 is not supported
689 if (SkCodec::kSuccess != result) {
msarett99f567e2015-08-05 12:58:26 -0700690 return;
691 }
692
scroggod8d68552016-06-06 11:26:17 -0700693 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
694 // colorPtr and a valid colorCountPtr.
695 result = decoder->startScanlineDecode(
696 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
697 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
698 result = decoder->startScanlineDecode(
699 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
700 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
msarett99f567e2015-08-05 12:58:26 -0700701}
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
yujieqin9c7a8a42016-02-05 08:21:19 -0800888// Stream that is not an asset stream (!hasPosition() or !hasLength())
889class NotAssetMemStream : public SkStream {
890public:
bungeman38d909e2016-08-02 14:40:46 -0700891 NotAssetMemStream(sk_sp<SkData> data) : fStream(std::move(data)) {}
yujieqin9c7a8a42016-02-05 08:21:19 -0800892
893 bool hasPosition() const override {
894 return false;
895 }
896
897 bool hasLength() const override {
898 return false;
899 }
900
901 size_t peek(void* buf, size_t bytes) const override {
902 return fStream.peek(buf, bytes);
903 }
904 size_t read(void* buf, size_t bytes) override {
905 return fStream.read(buf, bytes);
906 }
907 bool rewind() override {
908 return fStream.rewind();
909 }
910 bool isAtEnd() const override {
911 return fStream.isAtEnd();
912 }
913private:
914 SkMemoryStream fStream;
915};
916
yujieqinf236ee42016-02-29 07:14:42 -0800917// Disable RAW tests for Win32.
918#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin9c7a8a42016-02-05 08:21:19 -0800919// Test that the RawCodec works also for not asset stream. This will test the code path using
920// SkRawBufferedStream instead of SkRawAssetStream.
yujieqin9c7a8a42016-02-05 08:21:19 -0800921DEF_TEST(Codec_raw_notseekable, r) {
922 const char* path = "dng_with_preview.dng";
923 SkString fullPath(GetResourcePath(path));
bungeman38d909e2016-08-02 14:40:46 -0700924 sk_sp<SkData> data(SkData::MakeFromFileName(fullPath.c_str()));
yujieqin9c7a8a42016-02-05 08:21:19 -0800925 if (!data) {
926 SkDebugf("Missing resource '%s'\n", path);
927 return;
928 }
929
bungeman38d909e2016-08-02 14:40:46 -0700930 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(std::move(data))));
yujieqin9c7a8a42016-02-05 08:21:19 -0800931 REPORTER_ASSERT(r, codec);
932
933 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
934}
935#endif
936
scroggodb30be22015-12-08 18:54:13 -0800937// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
938// + rewind() and succeed.
939DEF_TEST(Codec_webp_peek, r) {
940 const char* path = "baby_tux.webp";
941 SkString fullPath(GetResourcePath(path));
reedfde05112016-03-11 13:02:28 -0800942 auto data = SkData::MakeFromFileName(fullPath.c_str());
scroggodb30be22015-12-08 18:54:13 -0800943 if (!data) {
944 SkDebugf("Missing resource '%s'\n", path);
945 return;
946 }
947
948 // The limit is less than webp needs to peek or read.
reedfde05112016-03-11 13:02:28 -0800949 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(
950 new LimitedPeekingMemStream(data.get(), 25)));
scroggodb30be22015-12-08 18:54:13 -0800951 REPORTER_ASSERT(r, codec);
952
scroggo7b5e5532016-02-04 06:14:24 -0800953 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800954
955 // Similarly, a stream which does not peek should still succeed.
reedfde05112016-03-11 13:02:28 -0800956 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data.get(), 0)));
scroggodb30be22015-12-08 18:54:13 -0800957 REPORTER_ASSERT(r, codec);
958
scroggo7b5e5532016-02-04 06:14:24 -0800959 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800960}
961
msarett7f7ec202016-03-01 12:12:27 -0800962// SkCodec's wbmp decoder was initially unnecessarily restrictive.
963// It required the second byte to be zero. The wbmp specification allows
964// a couple of bits to be 1 (so long as they do not overlap with 0x9F).
965// Test that SkCodec now supports an image with these bits set.
scroggob9a1e342015-11-30 06:25:31 -0800966DEF_TEST(Codec_wbmp, r) {
967 const char* path = "mandrill.wbmp";
968 SkAutoTDelete<SkStream> stream(resource(path));
969 if (!stream) {
970 SkDebugf("Missing resource '%s'\n", path);
971 return;
972 }
973
974 // Modify the stream to contain a second byte with some bits set.
reedfde05112016-03-11 13:02:28 -0800975 auto data = SkCopyStreamToData(stream);
scroggob9a1e342015-11-30 06:25:31 -0800976 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
977 writeableData[1] = static_cast<uint8_t>(~0x9F);
978
msarett7f7ec202016-03-01 12:12:27 -0800979 // SkCodec should support this.
reedfde05112016-03-11 13:02:28 -0800980 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data.get()));
scroggob9a1e342015-11-30 06:25:31 -0800981 REPORTER_ASSERT(r, codec);
982 if (!codec) {
983 return;
984 }
scroggo7b5e5532016-02-04 06:14:24 -0800985 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800986}
scroggodb30be22015-12-08 18:54:13 -0800987
988// wbmp images have a header that can be arbitrarily large, depending on the
989// size of the image. We cap the size at 65535, meaning we only need to look at
990// 8 bytes to determine whether we can read the image. This is important
991// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
992// image is a wbmp.
993DEF_TEST(Codec_wbmp_max_size, r) {
994 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
995 0x83, 0xFF, 0x7F, // W: 65535
996 0x83, 0xFF, 0x7F }; // H: 65535
997 SkAutoTDelete<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
mtklein18300a32016-03-16 13:53:35 -0700998 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -0800999
1000 REPORTER_ASSERT(r, codec);
1001 if (!codec) return;
1002
1003 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
1004 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
1005
1006 // Now test an image which is too big. Any image with a larger header (i.e.
1007 // has bigger width/height) is also too big.
1008 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
1009 0x84, 0x80, 0x00, // W: 65536
1010 0x84, 0x80, 0x00 }; // H: 65536
1011 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
mtklein18300a32016-03-16 13:53:35 -07001012 codec.reset(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -08001013
1014 REPORTER_ASSERT(r, !codec);
1015}
msarett2812f032016-07-18 15:56:08 -07001016
1017DEF_TEST(Codec_jpeg_rewind, r) {
1018 const char* path = "mandrill_512_q075.jpg";
1019 SkAutoTDelete<SkStream> stream(resource(path));
1020 if (!stream) {
1021 SkDebugf("Missing resource '%s'\n", path);
1022 return;
1023 }
1024 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
1025 if (!codec) {
1026 ERRORF(r, "Unable to create codec '%s'.", path);
1027 return;
1028 }
1029
1030 const int width = codec->getInfo().width();
1031 const int height = codec->getInfo().height();
1032 size_t rowBytes = sizeof(SkPMColor) * width;
1033 SkAutoMalloc pixelStorage(height * rowBytes);
1034
1035 // Perform a sampled decode.
1036 SkAndroidCodec::AndroidOptions opts;
1037 opts.fSampleSize = 12;
1038 codec->getAndroidPixels(codec->getInfo().makeWH(width / 12, height / 12), pixelStorage.get(),
1039 rowBytes, &opts);
1040
1041 // Rewind the codec and perform a full image decode.
1042 SkCodec::Result result = codec->getPixels(codec->getInfo(), pixelStorage.get(), rowBytes);
1043 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1044}