blob: 37e4f305b5ff086c8926fe2c901fa377c55b8c1e [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"
scroggob9a1e342015-11-30 06:25:31 -080015#include "SkImageDecoder.h"
halcanarya096d7a2015-03-27 12:16:53 -070016#include "SkMD5.h"
scroggob636b452015-07-22 07:16:20 -070017#include "SkRandom.h"
scroggocf98fa92015-11-23 08:14:40 -080018#include "SkStream.h"
scroggob9a1e342015-11-30 06:25:31 -080019#include "SkStreamPriv.h"
scroggocf98fa92015-11-23 08:14:40 -080020#include "SkPngChunkReader.h"
halcanarya096d7a2015-03-27 12:16:53 -070021#include "Test.h"
22
scroggocf98fa92015-11-23 08:14:40 -080023#include "png.h"
24
halcanarya096d7a2015-03-27 12:16:53 -070025static SkStreamAsset* resource(const char path[]) {
26 SkString fullPath = GetResourcePath(path);
27 return SkStream::NewFromFile(fullPath.c_str());
28}
29
30static void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
31 SkAutoLockPixels autoLockPixels(bm);
32 SkASSERT(bm.getPixels());
33 SkMD5 md5;
34 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
35 for (int y = 0; y < bm.height(); ++y) {
36 md5.update(static_cast<uint8_t*>(bm.getAddr(0, y)), rowLen);
37 }
38 md5.finish(*digest);
39}
40
scroggo9b2cdbf42015-07-10 12:07:02 -070041/**
42 * Compute the digest for bm and compare it to a known good digest.
43 * @param r Reporter to assert that bm's digest matches goodDigest.
44 * @param goodDigest The known good digest to compare to.
45 * @param bm The bitmap to test.
46 */
47static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
48 const SkBitmap& bm) {
49 SkMD5::Digest digest;
50 md5(bm, &digest);
51 REPORTER_ASSERT(r, digest == goodDigest);
52}
53
scroggod1bc5742015-08-12 08:31:44 -070054/**
55 * Test decoding an SkCodec to a particular SkImageInfo.
56 *
halcanary96fcdcc2015-08-27 07:41:13 -070057 * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr,
scroggod1bc5742015-08-12 08:31:44 -070058 * the resulting decode should match.
59 */
scroggo7b5e5532016-02-04 06:14:24 -080060template<typename Codec>
61static void test_info(skiatest::Reporter* r, Codec* codec, const SkImageInfo& info,
scroggod1bc5742015-08-12 08:31:44 -070062 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
63 SkBitmap bm;
64 bm.allocPixels(info);
65 SkAutoLockPixels autoLockPixels(bm);
66
67 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
68 REPORTER_ASSERT(r, result == expectedResult);
69
70 if (goodDigest) {
71 compare_to_good_digest(r, *goodDigest, bm);
72 }
73}
74
scroggob636b452015-07-22 07:16:20 -070075SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
76 SkIRect rect;
77 do {
78 rect.fLeft = rand->nextRangeU(0, w);
79 rect.fTop = rand->nextRangeU(0, h);
80 rect.fRight = rand->nextRangeU(0, w);
81 rect.fBottom = rand->nextRangeU(0, h);
82 rect.sort();
83 } while (rect.isEmpty());
84 return rect;
85}
86
scroggo7b5e5532016-02-04 06:14:24 -080087template<typename Codec>
88static void test_codec(skiatest::Reporter* r, Codec* codec, SkBitmap& bm, const SkImageInfo& info,
scroggo27c17282015-10-27 08:14:46 -070089 const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest,
90 const SkMD5::Digest* goodDigest) {
msarette6dd0042015-10-09 11:07:34 -070091
halcanarya096d7a2015-03-27 12:16:53 -070092 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -070093 bm.allocPixels(info);
94 SkAutoLockPixels autoLockPixels(bm);
msarettcc7f3052015-10-05 14:20:27 -070095
96 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -070097 REPORTER_ASSERT(r, result == expectedResult);
halcanarya096d7a2015-03-27 12:16:53 -070098
msarettcc7f3052015-10-05 14:20:27 -070099 md5(bm, digest);
100 if (goodDigest) {
101 REPORTER_ASSERT(r, *digest == *goodDigest);
102 }
halcanarya096d7a2015-03-27 12:16:53 -0700103
msarett8ff6ca62015-09-18 12:06:04 -0700104 {
105 // Test decoding to 565
106 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggo27c17282015-10-27 08:14:46 -0700107 SkCodec::Result expected565 = info.alphaType() == kOpaque_SkAlphaType ?
msarette6dd0042015-10-09 11:07:34 -0700108 expectedResult : SkCodec::kInvalidConversion;
109 test_info(r, codec, info565, expected565, nullptr);
msarett8ff6ca62015-09-18 12:06:04 -0700110 }
111
112 // Verify that re-decoding gives the same result. It is interesting to check this after
113 // a decode to 565, since choosing to decode to 565 may result in some of the decode
114 // options being modified. These options should return to their defaults on another
115 // decode to kN32, so the new digest should match the old digest.
msarette6dd0042015-10-09 11:07:34 -0700116 test_info(r, codec, info, expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700117
118 {
119 // Check alpha type conversions
120 if (info.alphaType() == kOpaque_SkAlphaType) {
121 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800122 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700123 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800124 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700125 } else {
126 // Decoding to opaque should fail
127 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700128 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700129 SkAlphaType otherAt = info.alphaType();
130 if (kPremul_SkAlphaType == otherAt) {
131 otherAt = kUnpremul_SkAlphaType;
132 } else {
133 otherAt = kPremul_SkAlphaType;
134 }
135 // The other non-opaque alpha type should always succeed, but not match.
msarette6dd0042015-10-09 11:07:34 -0700136 test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700137 }
138 }
msarettcc7f3052015-10-05 14:20:27 -0700139}
140
scroggobed1ed62016-02-11 10:24:55 -0800141static bool supports_partial_scanlines(const char path[]) {
scroggo2c3b2182015-10-09 08:40:59 -0700142 static const char* const exts[] = {
143 "jpg", "jpeg", "png", "webp"
144 "JPG", "JPEG", "PNG", "WEBP"
145 };
146
147 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
148 if (SkStrEndsWith(path, exts[i])) {
149 return true;
150 }
151 }
152 return false;
153}
154
msarettcc7f3052015-10-05 14:20:27 -0700155static void check(skiatest::Reporter* r,
156 const char path[],
157 SkISize size,
158 bool supportsScanlineDecoding,
159 bool supportsSubsetDecoding,
msarette6dd0042015-10-09 11:07:34 -0700160 bool supportsIncomplete = true) {
msarettcc7f3052015-10-05 14:20:27 -0700161
162 SkAutoTDelete<SkStream> stream(resource(path));
163 if (!stream) {
164 SkDebugf("Missing resource '%s'\n", path);
165 return;
166 }
msarette6dd0042015-10-09 11:07:34 -0700167
168 SkAutoTDelete<SkCodec> codec(nullptr);
169 bool isIncomplete = supportsIncomplete;
170 if (isIncomplete) {
171 size_t size = stream->getLength();
172 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
173 codec.reset(SkCodec::NewFromData(data));
174 } else {
175 codec.reset(SkCodec::NewFromStream(stream.detach()));
176 }
msarettcc7f3052015-10-05 14:20:27 -0700177 if (!codec) {
178 ERRORF(r, "Unable to decode '%s'", path);
179 return;
180 }
181
182 // Test full image decodes with SkCodec
183 SkMD5::Digest codecDigest;
scroggoef0fed32016-02-18 05:59:25 -0800184 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
msarettcc7f3052015-10-05 14:20:27 -0700185 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700186 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
scroggo7b5e5532016-02-04 06:14:24 -0800187 test_codec(r, codec.get(), bm, info, size, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700188
189 // Scanline decoding follows.
msarettcc7f3052015-10-05 14:20:27 -0700190 // Need to call startScanlineDecode() first.
scroggo46c57472015-09-30 08:57:13 -0700191 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700192 == 0);
scroggo46c57472015-09-30 08:57:13 -0700193 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700194 == 0);
scroggo46c57472015-09-30 08:57:13 -0700195
196 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700197 if (supportsScanlineDecoding) {
198 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700199
scroggo46c57472015-09-30 08:57:13 -0700200 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700201
scroggo58421542015-04-01 11:25:20 -0700202 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700203 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
204 if (!isIncomplete) {
205 REPORTER_ASSERT(r, 1 == lines);
206 }
scroggo58421542015-04-01 11:25:20 -0700207 }
208 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700209 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700210 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700211 }
scroggo46c57472015-09-30 08:57:13 -0700212
213 // Cannot continue to decode scanlines beyond the end
214 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700215 == 0);
scroggo46c57472015-09-30 08:57:13 -0700216
217 // Interrupting a scanline decode with a full decode starts from
218 // scratch
219 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700220 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
221 if (!isIncomplete) {
222 REPORTER_ASSERT(r, lines == 1);
223 }
scroggo46c57472015-09-30 08:57:13 -0700224 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700225 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700226 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700227 == 0);
scroggo46c57472015-09-30 08:57:13 -0700228 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700229 == 0);
msarett80803ff2015-10-16 10:54:12 -0700230
231 // Test partial scanline decodes
scroggobed1ed62016-02-11 10:24:55 -0800232 if (supports_partial_scanlines(path) && info.width() >= 3) {
msarett80803ff2015-10-16 10:54:12 -0700233 SkCodec::Options options;
234 int width = info.width();
235 int height = info.height();
236 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
237 options.fSubset = &subset;
238
239 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
240 nullptr, nullptr);
241 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
242
243 for (int y = 0; y < height; y++) {
244 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
245 if (!isIncomplete) {
246 REPORTER_ASSERT(r, 1 == lines);
247 }
248 }
249 }
scroggo58421542015-04-01 11:25:20 -0700250 } else {
scroggo46c57472015-09-30 08:57:13 -0700251 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700252 }
scroggob636b452015-07-22 07:16:20 -0700253
254 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
255 // random subsets.
256 // Do not attempt to decode subsets of an image of only once pixel, since there is no
257 // meaningful subset.
258 if (size.width() * size.height() == 1) {
259 return;
260 }
261
262 SkRandom rand;
263 SkIRect subset;
264 SkCodec::Options opts;
265 opts.fSubset = &subset;
266 for (int i = 0; i < 5; i++) {
267 subset = generate_random_subset(&rand, size.width(), size.height());
268 SkASSERT(!subset.isEmpty());
269 const bool supported = codec->getValidSubset(&subset);
270 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
271
272 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
273 SkBitmap bm;
274 bm.allocPixels(subsetInfo);
275 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700276 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700277
278 if (supportsSubsetDecoding) {
msarette6dd0042015-10-09 11:07:34 -0700279 REPORTER_ASSERT(r, result == expectedResult);
scroggob636b452015-07-22 07:16:20 -0700280 // Webp is the only codec that supports subsets, and it will have modified the subset
281 // to have even left/top.
282 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
283 } else {
284 // No subsets will work.
285 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
286 }
287 }
msarettcc7f3052015-10-05 14:20:27 -0700288
scroggobed1ed62016-02-11 10:24:55 -0800289 // SkAndroidCodec tests
290 if (supportsScanlineDecoding || supportsSubsetDecoding) {
scroggo2c3b2182015-10-09 08:40:59 -0700291
msarettcc7f3052015-10-05 14:20:27 -0700292 SkAutoTDelete<SkStream> stream(resource(path));
293 if (!stream) {
294 SkDebugf("Missing resource '%s'\n", path);
295 return;
296 }
msarette6dd0042015-10-09 11:07:34 -0700297
scroggo7b5e5532016-02-04 06:14:24 -0800298 SkAutoTDelete<SkAndroidCodec> androidCodec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700299 if (isIncomplete) {
300 size_t size = stream->getLength();
301 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
scroggo7b5e5532016-02-04 06:14:24 -0800302 androidCodec.reset(SkAndroidCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700303 } else {
scroggo7b5e5532016-02-04 06:14:24 -0800304 androidCodec.reset(SkAndroidCodec::NewFromStream(stream.detach()));
msarette6dd0042015-10-09 11:07:34 -0700305 }
scroggo7b5e5532016-02-04 06:14:24 -0800306 if (!androidCodec) {
msarettcc7f3052015-10-05 14:20:27 -0700307 ERRORF(r, "Unable to decode '%s'", path);
308 return;
309 }
310
311 SkBitmap bm;
scroggobed1ed62016-02-11 10:24:55 -0800312 SkMD5::Digest androidCodecDigest;
313 test_codec(r, androidCodec.get(), bm, info, size, expectedResult, &androidCodecDigest,
scroggo7b5e5532016-02-04 06:14:24 -0800314 &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700315 }
316
msarettedd2dcf2016-01-14 13:12:26 -0800317 if (!isIncomplete) {
scroggoef0fed32016-02-18 05:59:25 -0800318 // Test SkCodecImageGenerator
msarettedd2dcf2016-01-14 13:12:26 -0800319 SkAutoTDelete<SkStream> stream(resource(path));
320 SkAutoTUnref<SkData> fullData(SkData::NewFromStream(stream, stream->getLength()));
321 SkAutoTDelete<SkImageGenerator> gen(SkCodecImageGenerator::NewFromEncodedCodec(fullData));
322 SkBitmap bm;
323 bm.allocPixels(info);
324 SkAutoLockPixels autoLockPixels(bm);
325 REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes()));
326 compare_to_good_digest(r, codecDigest, bm);
scroggoef0fed32016-02-18 05:59:25 -0800327
328 // Test using SkFrontBufferedStream, as Android does
329 SkStream* bufferedStream = SkFrontBufferedStream::Create(new SkMemoryStream(fullData),
330 SkCodec::MinBufferedBytesNeeded());
331 REPORTER_ASSERT(r, bufferedStream);
332 codec.reset(SkCodec::NewFromStream(bufferedStream));
333 REPORTER_ASSERT(r, codec);
334 if (codec) {
335 test_info(r, codec.get(), info, SkCodec::kSuccess, &codecDigest);
336 }
msarettedd2dcf2016-01-14 13:12:26 -0800337 }
338
msarette6dd0042015-10-09 11:07:34 -0700339 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
340 if (isIncomplete) {
scroggo27c17282015-10-27 08:14:46 -0700341 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false);
msarettcc7f3052015-10-05 14:20:27 -0700342 }
halcanarya096d7a2015-03-27 12:16:53 -0700343}
344
345DEF_TEST(Codec, r) {
346 // WBMP
msarett99f567e2015-08-05 12:58:26 -0700347 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700348
scroggo6f5e6192015-06-18 12:53:43 -0700349 // WEBP
scroggob636b452015-07-22 07:16:20 -0700350 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true);
351 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true);
352 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true);
scroggo6f5e6192015-06-18 12:53:43 -0700353
halcanarya096d7a2015-03-27 12:16:53 -0700354 // BMP
msarett5406d6f2015-08-31 06:55:13 -0700355 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false);
msarett4946b942016-02-11 08:41:01 -0800356 check(r, "rle.bmp", SkISize::Make(320, 240), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700357
358 // ICO
msarette6dd0042015-10-09 11:07:34 -0700359 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700360 // These two tests examine interestingly different behavior:
361 // Decodes an embedded BMP image
msarettbe8216a2015-12-04 08:00:50 -0800362 check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700363 // Decodes an embedded PNG image
msarettbe8216a2015-12-04 08:00:50 -0800364 check(r, "google_chrome.ico", SkISize::Make(256, 256), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700365
msarett438b2ad2015-04-09 12:43:10 -0700366 // GIF
msarette6dd0042015-10-09 11:07:34 -0700367 // FIXME: We are not ready to test incomplete GIFs
scroggo27c17282015-10-27 08:14:46 -0700368 check(r, "box.gif", SkISize::Make(200, 55), true, false, false);
369 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700370 // randPixels.gif is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700371 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, false);
msarett438b2ad2015-04-09 12:43:10 -0700372
msarette16b04a2015-04-15 07:32:19 -0700373 // JPG
scroggo27c17282015-10-27 08:14:46 -0700374 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false);
scroggob636b452015-07-22 07:16:20 -0700375 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
msarette6dd0042015-10-09 11:07:34 -0700376 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700377 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggob636b452015-07-22 07:16:20 -0700378 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
msarette6dd0042015-10-09 11:07:34 -0700379 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700380 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
msarette16b04a2015-04-15 07:32:19 -0700381
halcanarya096d7a2015-03-27 12:16:53 -0700382 // PNG
scroggo27c17282015-10-27 08:14:46 -0700383 check(r, "arrow.png", SkISize::Make(187, 312), true, false, false);
384 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false, false);
385 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false, false);
386 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false, false);
387 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false, false);
388 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false, false);
389 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false, false);
390 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false, false);
391 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false, false);
392 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false, false);
393 check(r, "plane.png", SkISize::Make(250, 126), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700394 // FIXME: We are not ready to test incomplete interlaced pngs
scroggo27c17282015-10-27 08:14:46 -0700395 check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false, false);
396 check(r, "randPixels.png", SkISize::Make(8, 8), true, false, false);
397 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false, false);
yujieqin916de9f2016-01-25 08:26:16 -0800398
399 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800400// Disable RAW tests for Win32.
401#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800402 check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
ebrauer46d2aa82016-02-17 08:04:00 -0800403 check(r, "sample_1mp_rotated.dng", SkISize::Make(600, 338), false, false, false);
yujieqin9c7a8a42016-02-05 08:21:19 -0800404 check(r, "dng_with_preview.dng", SkISize::Make(600, 338), true, false, false);
msarett02cb4d42016-01-25 11:01:34 -0800405#endif
halcanarya096d7a2015-03-27 12:16:53 -0700406}
scroggo0a7e69c2015-04-03 07:22:22 -0700407
scroggo46c57472015-09-30 08:57:13 -0700408// Test interlaced PNG in stripes, similar to DM's kStripe_Mode
409DEF_TEST(Codec_stripes, r) {
410 const char * path = "plane_interlaced.png";
411 SkAutoTDelete<SkStream> stream(resource(path));
412 if (!stream) {
413 SkDebugf("Missing resource '%s'\n", path);
414 }
415
416 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
417 REPORTER_ASSERT(r, codec);
418
419 if (!codec) {
420 return;
421 }
422
423 switch (codec->getScanlineOrder()) {
424 case SkCodec::kBottomUp_SkScanlineOrder:
425 case SkCodec::kOutOfOrder_SkScanlineOrder:
426 ERRORF(r, "This scanline order will not match the original.");
427 return;
428 default:
429 break;
430 }
431
432 // Baseline for what the image should look like, using N32.
433 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
434
435 SkBitmap bm;
436 bm.allocPixels(info);
437 SkAutoLockPixels autoLockPixels(bm);
438 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
439 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
440
441 SkMD5::Digest digest;
442 md5(bm, &digest);
443
444 // Now decode in stripes
445 const int height = info.height();
446 const int numStripes = 4;
447 int stripeHeight;
448 int remainingLines;
449 SkTDivMod(height, numStripes, &stripeHeight, &remainingLines);
450
451 bm.eraseColor(SK_ColorYELLOW);
452
453 result = codec->startScanlineDecode(info);
454 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
455
456 // Odd stripes
457 for (int i = 1; i < numStripes; i += 2) {
458 // Skip the even stripes
msarette6dd0042015-10-09 11:07:34 -0700459 bool skipResult = codec->skipScanlines(stripeHeight);
460 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700461
msarette6dd0042015-10-09 11:07:34 -0700462 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700463 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700464 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700465 }
466
467 // Even stripes
468 result = codec->startScanlineDecode(info);
469 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
470
471 for (int i = 0; i < numStripes; i += 2) {
msarette6dd0042015-10-09 11:07:34 -0700472 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700473 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700474 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700475
476 // Skip the odd stripes
477 if (i + 1 < numStripes) {
msarette6dd0042015-10-09 11:07:34 -0700478 bool skipResult = codec->skipScanlines(stripeHeight);
479 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700480 }
481 }
482
483 // Remainder at the end
484 if (remainingLines > 0) {
485 result = codec->startScanlineDecode(info);
486 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
487
msarette6dd0042015-10-09 11:07:34 -0700488 bool skipResult = codec->skipScanlines(height - remainingLines);
489 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700490
msarette6dd0042015-10-09 11:07:34 -0700491 int linesDecoded = codec->getScanlines(bm.getAddr(0, height - remainingLines),
scroggo46c57472015-09-30 08:57:13 -0700492 remainingLines, bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700493 REPORTER_ASSERT(r, linesDecoded == remainingLines);
scroggo46c57472015-09-30 08:57:13 -0700494 }
495
496 compare_to_good_digest(r, digest, bm);
497}
498
scroggo0a7e69c2015-04-03 07:22:22 -0700499static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700500 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700501 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700502 REPORTER_ASSERT(r, !codec);
503
msarett3d9d7a72015-10-21 10:27:10 -0700504 SkAndroidCodec* androidCodec =
505 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
506 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700507}
508
509// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
510// even on failure. Test some bad streams.
511DEF_TEST(Codec_leaks, r) {
512 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
513 const char nonSupportedStream[] = "hello world";
514 // The other strings should look like the beginning of a file type, so we'll call some
515 // internal version of NewFromStream, which must also delete the stream on failure.
516 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
517 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
518 const char emptyWebp[] = "RIFF1234WEBPVP";
519 const char emptyBmp[] = { 'B', 'M' };
520 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
521 const char emptyGif[] = "GIFVER";
522
523 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
524 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
525 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
526 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
527 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
528 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
529 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
530}
msarette16b04a2015-04-15 07:32:19 -0700531
scroggo2c3b2182015-10-09 08:40:59 -0700532DEF_TEST(Codec_null, r) {
scroggobed1ed62016-02-11 10:24:55 -0800533 // Attempting to create an SkCodec or an SkAndroidCodec with null should not
scroggo2c3b2182015-10-09 08:40:59 -0700534 // crash.
535 SkCodec* codec = SkCodec::NewFromStream(nullptr);
536 REPORTER_ASSERT(r, !codec);
537
msarett3d9d7a72015-10-21 10:27:10 -0700538 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
539 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700540}
541
msarette16b04a2015-04-15 07:32:19 -0700542static void test_dimensions(skiatest::Reporter* r, const char path[]) {
543 // Create the codec from the resource file
544 SkAutoTDelete<SkStream> stream(resource(path));
545 if (!stream) {
546 SkDebugf("Missing resource '%s'\n", path);
547 return;
548 }
msarett3d9d7a72015-10-21 10:27:10 -0700549 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.detach()));
msarette16b04a2015-04-15 07:32:19 -0700550 if (!codec) {
551 ERRORF(r, "Unable to create codec '%s'", path);
552 return;
553 }
554
555 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800556 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700557 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700558 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700559 SkImageInfo scaledInfo = codec->getInfo()
560 .makeWH(scaledDims.width(), scaledDims.height())
561 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700562
563 // Set up for the decode
564 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
565 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
566 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
567
msarett3d9d7a72015-10-21 10:27:10 -0700568 SkAndroidCodec::AndroidOptions options;
569 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700570 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700571 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700572 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700573 }
574}
575
576// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
577DEF_TEST(Codec_Dimensions, r) {
578 // JPG
579 test_dimensions(r, "CMYK.jpg");
580 test_dimensions(r, "color_wheel.jpg");
581 test_dimensions(r, "grayscale.jpg");
582 test_dimensions(r, "mandrill_512_q075.jpg");
583 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700584
585 // Decoding small images with very large scaling factors is a potential
586 // source of bugs and crashes. We disable these tests in Gold because
587 // tiny images are not very useful to look at.
588 // Here we make sure that we do not crash or access illegal memory when
589 // performing scaled decodes on small images.
590 test_dimensions(r, "1x1.png");
591 test_dimensions(r, "2x2.png");
592 test_dimensions(r, "3x3.png");
593 test_dimensions(r, "3x1.png");
594 test_dimensions(r, "1x1.png");
595 test_dimensions(r, "16x1.png");
596 test_dimensions(r, "1x16.png");
597 test_dimensions(r, "mandrill_16.png");
598
yujieqin916de9f2016-01-25 08:26:16 -0800599 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800600// Disable RAW tests for Win32.
601#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800602 test_dimensions(r, "sample_1mp.dng");
ebrauer46d2aa82016-02-17 08:04:00 -0800603 test_dimensions(r, "sample_1mp_rotated.dng");
yujieqin9c7a8a42016-02-05 08:21:19 -0800604 test_dimensions(r, "dng_with_preview.dng");
msarett8e49ca32016-01-25 13:10:58 -0800605#endif
msarette16b04a2015-04-15 07:32:19 -0700606}
607
msarettd0375bc2015-08-12 08:08:56 -0700608static void test_invalid(skiatest::Reporter* r, const char path[]) {
msarett4b17fa32015-04-23 08:53:39 -0700609 SkAutoTDelete<SkStream> stream(resource(path));
610 if (!stream) {
611 SkDebugf("Missing resource '%s'\n", path);
612 return;
613 }
614 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700615 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700616}
msarette16b04a2015-04-15 07:32:19 -0700617
msarett4b17fa32015-04-23 08:53:39 -0700618DEF_TEST(Codec_Empty, r) {
619 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700620 test_invalid(r, "empty_images/zero-dims.gif");
621 test_invalid(r, "empty_images/zero-embedded.ico");
622 test_invalid(r, "empty_images/zero-width.bmp");
623 test_invalid(r, "empty_images/zero-height.bmp");
624 test_invalid(r, "empty_images/zero-width.jpg");
625 test_invalid(r, "empty_images/zero-height.jpg");
626 test_invalid(r, "empty_images/zero-width.png");
627 test_invalid(r, "empty_images/zero-height.png");
628 test_invalid(r, "empty_images/zero-width.wbmp");
629 test_invalid(r, "empty_images/zero-height.wbmp");
630 // This image is an ico with an embedded mask-bmp. This is illegal.
631 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700632}
msarett99f567e2015-08-05 12:58:26 -0700633
634static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
635 SkAutoTDelete<SkStream> stream(resource(path));
636 if (!stream) {
637 SkDebugf("Missing resource '%s'\n", path);
638 return;
639 }
scroggo46c57472015-09-30 08:57:13 -0700640 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.detach()));
msarett99f567e2015-08-05 12:58:26 -0700641
642 // This should return kSuccess because kIndex8 is supported.
643 SkPMColor colorStorage[256];
644 int colorCount;
scroggo46c57472015-09-30 08:57:13 -0700645 SkCodec::Result result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700646 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
msarett99f567e2015-08-05 12:58:26 -0700647 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
648 // The rest of the test is uninteresting if kIndex8 is not supported
649 if (SkCodec::kSuccess != result) {
650 return;
651 }
652
653 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
654 // colorPtr and a valid colorCountPtr.
scroggo46c57472015-09-30 08:57:13 -0700655 result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700656 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
msarett99f567e2015-08-05 12:58:26 -0700657 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
scroggo46c57472015-09-30 08:57:13 -0700658 result = decoder->startScanlineDecode(
msarett99f567e2015-08-05 12:58:26 -0700659 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
660 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
661}
662
663DEF_TEST(Codec_Params, r) {
664 test_invalid_parameters(r, "index8.png");
665 test_invalid_parameters(r, "mandrill.wbmp");
666}
scroggocf98fa92015-11-23 08:14:40 -0800667
668static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
669 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
670 if (!sk_stream->write(data, len)) {
671 png_error(png_ptr, "sk_write_fn Error!");
672 }
673}
674
675#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
676DEF_TEST(Codec_pngChunkReader, r) {
677 // Create a dummy bitmap. Use unpremul RGBA for libpng.
678 SkBitmap bm;
679 const int w = 1;
680 const int h = 1;
681 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
682 kUnpremul_SkAlphaType);
683 bm.setInfo(bmInfo);
684 bm.allocPixels();
685 bm.eraseColor(SK_ColorBLUE);
686 SkMD5::Digest goodDigest;
687 md5(bm, &goodDigest);
688
689 // Write to a png file.
690 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
691 REPORTER_ASSERT(r, png);
692 if (!png) {
693 return;
694 }
695
696 png_infop info = png_create_info_struct(png);
697 REPORTER_ASSERT(r, info);
698 if (!info) {
699 png_destroy_write_struct(&png, nullptr);
700 return;
701 }
702
703 if (setjmp(png_jmpbuf(png))) {
704 ERRORF(r, "failed writing png");
705 png_destroy_write_struct(&png, &info);
706 return;
707 }
708
709 SkDynamicMemoryWStream wStream;
710 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
711
712 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
713 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
714 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
715
716 // Create some chunks that match the Android framework's use.
717 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800718 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
719 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
720 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800721 };
722
723 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
724 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
725#if PNG_LIBPNG_VER < 10600
726 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800727 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
728 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800729#endif
730
731 png_write_info(png, info);
732
733 for (int j = 0; j < h; j++) {
734 png_bytep row = (png_bytep)(bm.getAddr(0, j));
735 png_write_rows(png, &row, 1);
736 }
737 png_write_end(png, info);
738 png_destroy_write_struct(&png, &info);
739
740 class ChunkReader : public SkPngChunkReader {
741 public:
742 ChunkReader(skiatest::Reporter* r)
743 : fReporter(r)
744 {
745 this->reset();
746 }
747
748 bool readChunk(const char tag[], const void* data, size_t length) override {
749 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
750 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
751 // Tag matches. This should have been the first time we see it.
752 REPORTER_ASSERT(fReporter, !fSeen[i]);
753 fSeen[i] = true;
754
755 // Data and length should match
756 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
757 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
758 (const char*) gUnknowns[i].data));
759 return true;
760 }
761 }
762 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
763 return true;
764 }
765
766 bool allHaveBeenSeen() {
767 bool ret = true;
768 for (auto seen : fSeen) {
769 ret &= seen;
770 }
771 return ret;
772 }
773
774 void reset() {
775 sk_bzero(fSeen, sizeof(fSeen));
776 }
777
778 private:
779 skiatest::Reporter* fReporter; // Unowned
780 bool fSeen[3];
781 };
782
783 ChunkReader chunkReader(r);
784
785 // Now read the file with SkCodec.
786 SkAutoTUnref<SkData> data(wStream.copyToData());
787 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data, &chunkReader));
788 REPORTER_ASSERT(r, codec);
789 if (!codec) {
790 return;
791 }
792
793 // Now compare to the original.
794 SkBitmap decodedBm;
795 decodedBm.setInfo(codec->getInfo());
796 decodedBm.allocPixels();
797 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
798 decodedBm.rowBytes());
799 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
800
801 if (decodedBm.colorType() != bm.colorType()) {
802 SkBitmap tmp;
803 bool success = decodedBm.copyTo(&tmp, bm.colorType());
804 REPORTER_ASSERT(r, success);
805 if (!success) {
806 return;
807 }
808
809 tmp.swap(decodedBm);
810 }
811
812 compare_to_good_digest(r, goodDigest, decodedBm);
813 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
814
815 // Decoding again will read the chunks again.
816 chunkReader.reset();
817 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
818 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
819 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
820 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
821}
822#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800823
scroggodb30be22015-12-08 18:54:13 -0800824// Stream that can only peek up to a limit
825class LimitedPeekingMemStream : public SkStream {
826public:
827 LimitedPeekingMemStream(SkData* data, size_t limit)
828 : fStream(data)
829 , fLimit(limit) {}
830
831 size_t peek(void* buf, size_t bytes) const override {
832 return fStream.peek(buf, SkTMin(bytes, fLimit));
833 }
834 size_t read(void* buf, size_t bytes) override {
835 return fStream.read(buf, bytes);
836 }
837 bool rewind() override {
838 return fStream.rewind();
839 }
840 bool isAtEnd() const override {
841 return false;
842 }
843private:
844 SkMemoryStream fStream;
845 const size_t fLimit;
846};
847
yujieqin9c7a8a42016-02-05 08:21:19 -0800848// Stream that is not an asset stream (!hasPosition() or !hasLength())
849class NotAssetMemStream : public SkStream {
850public:
851 NotAssetMemStream(SkData* data) : fStream(data) {}
852
853 bool hasPosition() const override {
854 return false;
855 }
856
857 bool hasLength() const override {
858 return false;
859 }
860
861 size_t peek(void* buf, size_t bytes) const override {
862 return fStream.peek(buf, bytes);
863 }
864 size_t read(void* buf, size_t bytes) override {
865 return fStream.read(buf, bytes);
866 }
867 bool rewind() override {
868 return fStream.rewind();
869 }
870 bool isAtEnd() const override {
871 return fStream.isAtEnd();
872 }
873private:
874 SkMemoryStream fStream;
875};
876
yujieqinf236ee42016-02-29 07:14:42 -0800877// Disable RAW tests for Win32.
878#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin9c7a8a42016-02-05 08:21:19 -0800879// Test that the RawCodec works also for not asset stream. This will test the code path using
880// SkRawBufferedStream instead of SkRawAssetStream.
yujieqin9c7a8a42016-02-05 08:21:19 -0800881DEF_TEST(Codec_raw_notseekable, r) {
882 const char* path = "dng_with_preview.dng";
883 SkString fullPath(GetResourcePath(path));
884 SkAutoTUnref<SkData> data(SkData::NewFromFileName(fullPath.c_str()));
885 if (!data) {
886 SkDebugf("Missing resource '%s'\n", path);
887 return;
888 }
889
890 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(data)));
891 REPORTER_ASSERT(r, codec);
892
893 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
894}
895#endif
896
scroggodb30be22015-12-08 18:54:13 -0800897// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
898// + rewind() and succeed.
899DEF_TEST(Codec_webp_peek, r) {
900 const char* path = "baby_tux.webp";
901 SkString fullPath(GetResourcePath(path));
902 SkAutoTUnref<SkData> data(SkData::NewFromFileName(fullPath.c_str()));
903 if (!data) {
904 SkDebugf("Missing resource '%s'\n", path);
905 return;
906 }
907
908 // The limit is less than webp needs to peek or read.
909 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 25)));
910 REPORTER_ASSERT(r, codec);
911
scroggo7b5e5532016-02-04 06:14:24 -0800912 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800913
914 // Similarly, a stream which does not peek should still succeed.
915 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0)));
916 REPORTER_ASSERT(r, codec);
917
scroggo7b5e5532016-02-04 06:14:24 -0800918 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800919}
920
scroggob9a1e342015-11-30 06:25:31 -0800921// SkCodec's wbmp decoder was initially more restrictive than SkImageDecoder.
922// It required the second byte to be zero. But SkImageDecoder allowed a couple
923// of bits to be 1 (so long as they do not overlap with 0x9F). Test that
924// SkCodec now supports an image with these bits set.
925DEF_TEST(Codec_wbmp, r) {
926 const char* path = "mandrill.wbmp";
927 SkAutoTDelete<SkStream> stream(resource(path));
928 if (!stream) {
929 SkDebugf("Missing resource '%s'\n", path);
930 return;
931 }
932
933 // Modify the stream to contain a second byte with some bits set.
934 SkAutoTUnref<SkData> data(SkCopyStreamToData(stream));
935 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
936 writeableData[1] = static_cast<uint8_t>(~0x9F);
937
938 // SkImageDecoder supports this.
939 SkBitmap bitmap;
940 REPORTER_ASSERT(r, SkImageDecoder::DecodeMemory(data->data(), data->size(), &bitmap));
941
942 // So SkCodec should, too.
943 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
944 REPORTER_ASSERT(r, codec);
945 if (!codec) {
946 return;
947 }
scroggo7b5e5532016-02-04 06:14:24 -0800948 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800949}
scroggodb30be22015-12-08 18:54:13 -0800950
951// wbmp images have a header that can be arbitrarily large, depending on the
952// size of the image. We cap the size at 65535, meaning we only need to look at
953// 8 bytes to determine whether we can read the image. This is important
954// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
955// image is a wbmp.
956DEF_TEST(Codec_wbmp_max_size, r) {
957 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
958 0x83, 0xFF, 0x7F, // W: 65535
959 0x83, 0xFF, 0x7F }; // H: 65535
960 SkAutoTDelete<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
961 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
962
963 REPORTER_ASSERT(r, codec);
964 if (!codec) return;
965
966 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
967 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
968
969 // Now test an image which is too big. Any image with a larger header (i.e.
970 // has bigger width/height) is also too big.
971 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
972 0x84, 0x80, 0x00, // W: 65536
973 0x84, 0x80, 0x00 }; // H: 65536
974 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
975 codec.reset(SkCodec::NewFromStream(stream.detach()));
976
977 REPORTER_ASSERT(r, !codec);
978}