blob: 94eea65b938ad3d9ec96663b1e8422fde80f02f8 [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"
msarette6dd0042015-10-09 11:07:34 -070012#include "SkData.h"
scroggob9a1e342015-11-30 06:25:31 -080013#include "SkImageDecoder.h"
halcanarya096d7a2015-03-27 12:16:53 -070014#include "SkMD5.h"
scroggob636b452015-07-22 07:16:20 -070015#include "SkRandom.h"
scroggocf98fa92015-11-23 08:14:40 -080016#include "SkStream.h"
scroggob9a1e342015-11-30 06:25:31 -080017#include "SkStreamPriv.h"
scroggocf98fa92015-11-23 08:14:40 -080018#include "SkPngChunkReader.h"
halcanarya096d7a2015-03-27 12:16:53 -070019#include "Test.h"
20
scroggocf98fa92015-11-23 08:14:40 -080021#include "png.h"
22
halcanarya096d7a2015-03-27 12:16:53 -070023static SkStreamAsset* resource(const char path[]) {
24 SkString fullPath = GetResourcePath(path);
25 return SkStream::NewFromFile(fullPath.c_str());
26}
27
28static void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
29 SkAutoLockPixels autoLockPixels(bm);
30 SkASSERT(bm.getPixels());
31 SkMD5 md5;
32 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
33 for (int y = 0; y < bm.height(); ++y) {
34 md5.update(static_cast<uint8_t*>(bm.getAddr(0, y)), rowLen);
35 }
36 md5.finish(*digest);
37}
38
scroggo9b2cdbf42015-07-10 12:07:02 -070039/**
40 * Compute the digest for bm and compare it to a known good digest.
41 * @param r Reporter to assert that bm's digest matches goodDigest.
42 * @param goodDigest The known good digest to compare to.
43 * @param bm The bitmap to test.
44 */
45static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
46 const SkBitmap& bm) {
47 SkMD5::Digest digest;
48 md5(bm, &digest);
49 REPORTER_ASSERT(r, digest == goodDigest);
50}
51
scroggod1bc5742015-08-12 08:31:44 -070052/**
53 * Test decoding an SkCodec to a particular SkImageInfo.
54 *
halcanary96fcdcc2015-08-27 07:41:13 -070055 * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr,
scroggod1bc5742015-08-12 08:31:44 -070056 * the resulting decode should match.
57 */
58static void test_info(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
59 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
60 SkBitmap bm;
61 bm.allocPixels(info);
62 SkAutoLockPixels autoLockPixels(bm);
63
64 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
65 REPORTER_ASSERT(r, result == expectedResult);
66
67 if (goodDigest) {
68 compare_to_good_digest(r, *goodDigest, bm);
69 }
70}
71
msarett3d9d7a72015-10-21 10:27:10 -070072static void test_android_info(skiatest::Reporter* r, SkAndroidCodec* codec, const SkImageInfo& info,
73 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
74 SkBitmap bm;
75 bm.allocPixels(info);
76 SkAutoLockPixels autoLockPixels(bm);
77
78 SkCodec::Result result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes());
79 REPORTER_ASSERT(r, result == expectedResult);
80
81 if (goodDigest) {
82 compare_to_good_digest(r, *goodDigest, bm);
83 }
84}
85
scroggob636b452015-07-22 07:16:20 -070086SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
87 SkIRect rect;
88 do {
89 rect.fLeft = rand->nextRangeU(0, w);
90 rect.fTop = rand->nextRangeU(0, h);
91 rect.fRight = rand->nextRangeU(0, w);
92 rect.fBottom = rand->nextRangeU(0, h);
93 rect.sort();
94 } while (rect.isEmpty());
95 return rect;
96}
97
msarettcc7f3052015-10-05 14:20:27 -070098static void test_codec(skiatest::Reporter* r, SkCodec* codec, SkBitmap& bm, const SkImageInfo& info,
scroggo27c17282015-10-27 08:14:46 -070099 const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest,
100 const SkMD5::Digest* goodDigest) {
msarette6dd0042015-10-09 11:07:34 -0700101
halcanarya096d7a2015-03-27 12:16:53 -0700102 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -0700103 bm.allocPixels(info);
104 SkAutoLockPixels autoLockPixels(bm);
msarettcc7f3052015-10-05 14:20:27 -0700105
106 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700107 REPORTER_ASSERT(r, result == expectedResult);
halcanarya096d7a2015-03-27 12:16:53 -0700108
msarettcc7f3052015-10-05 14:20:27 -0700109 md5(bm, digest);
110 if (goodDigest) {
111 REPORTER_ASSERT(r, *digest == *goodDigest);
112 }
halcanarya096d7a2015-03-27 12:16:53 -0700113
msarett8ff6ca62015-09-18 12:06:04 -0700114 {
115 // Test decoding to 565
116 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggo27c17282015-10-27 08:14:46 -0700117 SkCodec::Result expected565 = info.alphaType() == kOpaque_SkAlphaType ?
msarette6dd0042015-10-09 11:07:34 -0700118 expectedResult : SkCodec::kInvalidConversion;
119 test_info(r, codec, info565, expected565, nullptr);
msarett8ff6ca62015-09-18 12:06:04 -0700120 }
121
122 // Verify that re-decoding gives the same result. It is interesting to check this after
123 // a decode to 565, since choosing to decode to 565 may result in some of the decode
124 // options being modified. These options should return to their defaults on another
125 // decode to kN32, so the new digest should match the old digest.
msarette6dd0042015-10-09 11:07:34 -0700126 test_info(r, codec, info, expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700127
128 {
129 // Check alpha type conversions
130 if (info.alphaType() == kOpaque_SkAlphaType) {
131 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700132 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700133 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700134 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700135 } else {
136 // Decoding to opaque should fail
137 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700138 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700139 SkAlphaType otherAt = info.alphaType();
140 if (kPremul_SkAlphaType == otherAt) {
141 otherAt = kUnpremul_SkAlphaType;
142 } else {
143 otherAt = kPremul_SkAlphaType;
144 }
145 // The other non-opaque alpha type should always succeed, but not match.
msarette6dd0042015-10-09 11:07:34 -0700146 test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700147 }
148 }
msarettcc7f3052015-10-05 14:20:27 -0700149}
150
msarett3d9d7a72015-10-21 10:27:10 -0700151static void test_android_codec(skiatest::Reporter* r, SkAndroidCodec* codec, SkBitmap& bm,
scroggo27c17282015-10-27 08:14:46 -0700152 const SkImageInfo& info, const SkISize& size, SkCodec::Result expectedResult,
153 SkMD5::Digest* digest, const SkMD5::Digest* goodDigest) {
msarett3d9d7a72015-10-21 10:27:10 -0700154
155 REPORTER_ASSERT(r, info.dimensions() == size);
156 bm.allocPixels(info);
157 SkAutoLockPixels autoLockPixels(bm);
158
159 SkCodec::Result result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes());
160 REPORTER_ASSERT(r, result == expectedResult);
161
162 md5(bm, digest);
163 if (goodDigest) {
164 REPORTER_ASSERT(r, *digest == *goodDigest);
165 }
166
167 {
168 // Test decoding to 565
169 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggo27c17282015-10-27 08:14:46 -0700170 SkCodec::Result expected565 = info.alphaType() == kOpaque_SkAlphaType ?
msarett3d9d7a72015-10-21 10:27:10 -0700171 expectedResult : SkCodec::kInvalidConversion;
172 test_android_info(r, codec, info565, expected565, nullptr);
173 }
174
175 // Verify that re-decoding gives the same result. It is interesting to check this after
176 // a decode to 565, since choosing to decode to 565 may result in some of the decode
177 // options being modified. These options should return to their defaults on another
178 // decode to kN32, so the new digest should match the old digest.
179 test_android_info(r, codec, info, expectedResult, digest);
180
181 {
182 // Check alpha type conversions
183 if (info.alphaType() == kOpaque_SkAlphaType) {
184 test_android_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
185 SkCodec::kInvalidConversion, nullptr);
186 test_android_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
187 SkCodec::kInvalidConversion, nullptr);
188 } else {
189 // Decoding to opaque should fail
190 test_android_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
191 SkCodec::kInvalidConversion, nullptr);
192 SkAlphaType otherAt = info.alphaType();
193 if (kPremul_SkAlphaType == otherAt) {
194 otherAt = kUnpremul_SkAlphaType;
195 } else {
196 otherAt = kPremul_SkAlphaType;
197 }
198 // The other non-opaque alpha type should always succeed, but not match.
199 test_android_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
200 }
201 }
202}
203
scroggo2c3b2182015-10-09 08:40:59 -0700204// FIXME: SkScaledCodec is currently only supported for types used by BRD
halcanary6950de62015-11-07 05:29:00 -0800205// https://bug.skia.org/4428
scroggo2c3b2182015-10-09 08:40:59 -0700206static bool supports_scaled_codec(const char path[]) {
207 static const char* const exts[] = {
208 "jpg", "jpeg", "png", "webp"
209 "JPG", "JPEG", "PNG", "WEBP"
210 };
211
212 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
213 if (SkStrEndsWith(path, exts[i])) {
214 return true;
215 }
216 }
217 return false;
218}
219
msarettcc7f3052015-10-05 14:20:27 -0700220static void check(skiatest::Reporter* r,
221 const char path[],
222 SkISize size,
223 bool supportsScanlineDecoding,
224 bool supportsSubsetDecoding,
msarette6dd0042015-10-09 11:07:34 -0700225 bool supportsIncomplete = true) {
msarettcc7f3052015-10-05 14:20:27 -0700226
227 SkAutoTDelete<SkStream> stream(resource(path));
228 if (!stream) {
229 SkDebugf("Missing resource '%s'\n", path);
230 return;
231 }
msarette6dd0042015-10-09 11:07:34 -0700232
233 SkAutoTDelete<SkCodec> codec(nullptr);
234 bool isIncomplete = supportsIncomplete;
235 if (isIncomplete) {
236 size_t size = stream->getLength();
237 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
238 codec.reset(SkCodec::NewFromData(data));
239 } else {
240 codec.reset(SkCodec::NewFromStream(stream.detach()));
241 }
msarettcc7f3052015-10-05 14:20:27 -0700242 if (!codec) {
243 ERRORF(r, "Unable to decode '%s'", path);
244 return;
245 }
246
247 // Test full image decodes with SkCodec
248 SkMD5::Digest codecDigest;
249 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
250 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700251 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
scroggo27c17282015-10-27 08:14:46 -0700252 test_codec(r, codec, bm, info, size, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700253
254 // Scanline decoding follows.
msarettcc7f3052015-10-05 14:20:27 -0700255 // Need to call startScanlineDecode() first.
scroggo46c57472015-09-30 08:57:13 -0700256 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700257 == 0);
scroggo46c57472015-09-30 08:57:13 -0700258 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700259 == 0);
scroggo46c57472015-09-30 08:57:13 -0700260
261 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700262 if (supportsScanlineDecoding) {
263 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700264
scroggo46c57472015-09-30 08:57:13 -0700265 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700266
scroggo58421542015-04-01 11:25:20 -0700267 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700268 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
269 if (!isIncomplete) {
270 REPORTER_ASSERT(r, 1 == lines);
271 }
scroggo58421542015-04-01 11:25:20 -0700272 }
273 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700274 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700275 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700276 }
scroggo46c57472015-09-30 08:57:13 -0700277
278 // Cannot continue to decode scanlines beyond the end
279 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700280 == 0);
scroggo46c57472015-09-30 08:57:13 -0700281
282 // Interrupting a scanline decode with a full decode starts from
283 // scratch
284 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700285 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
286 if (!isIncomplete) {
287 REPORTER_ASSERT(r, lines == 1);
288 }
scroggo46c57472015-09-30 08:57:13 -0700289 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700290 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700291 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700292 == 0);
scroggo46c57472015-09-30 08:57:13 -0700293 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700294 == 0);
msarett80803ff2015-10-16 10:54:12 -0700295
296 // Test partial scanline decodes
297 if (supports_scaled_codec(path) && info.width() >= 3) {
298 SkCodec::Options options;
299 int width = info.width();
300 int height = info.height();
301 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
302 options.fSubset = &subset;
303
304 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
305 nullptr, nullptr);
306 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
307
308 for (int y = 0; y < height; y++) {
309 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
310 if (!isIncomplete) {
311 REPORTER_ASSERT(r, 1 == lines);
312 }
313 }
314 }
scroggo58421542015-04-01 11:25:20 -0700315 } else {
scroggo46c57472015-09-30 08:57:13 -0700316 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700317 }
scroggob636b452015-07-22 07:16:20 -0700318
319 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
320 // random subsets.
321 // Do not attempt to decode subsets of an image of only once pixel, since there is no
322 // meaningful subset.
323 if (size.width() * size.height() == 1) {
324 return;
325 }
326
327 SkRandom rand;
328 SkIRect subset;
329 SkCodec::Options opts;
330 opts.fSubset = &subset;
331 for (int i = 0; i < 5; i++) {
332 subset = generate_random_subset(&rand, size.width(), size.height());
333 SkASSERT(!subset.isEmpty());
334 const bool supported = codec->getValidSubset(&subset);
335 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
336
337 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
338 SkBitmap bm;
339 bm.allocPixels(subsetInfo);
340 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700341 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700342
343 if (supportsSubsetDecoding) {
msarette6dd0042015-10-09 11:07:34 -0700344 REPORTER_ASSERT(r, result == expectedResult);
scroggob636b452015-07-22 07:16:20 -0700345 // Webp is the only codec that supports subsets, and it will have modified the subset
346 // to have even left/top.
347 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
348 } else {
349 // No subsets will work.
350 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
351 }
352 }
msarettcc7f3052015-10-05 14:20:27 -0700353
354 // SkScaledCodec tests
scroggo2c3b2182015-10-09 08:40:59 -0700355 if ((supportsScanlineDecoding || supportsSubsetDecoding) && supports_scaled_codec(path)) {
356
msarettcc7f3052015-10-05 14:20:27 -0700357 SkAutoTDelete<SkStream> stream(resource(path));
358 if (!stream) {
359 SkDebugf("Missing resource '%s'\n", path);
360 return;
361 }
msarette6dd0042015-10-09 11:07:34 -0700362
msarett3d9d7a72015-10-21 10:27:10 -0700363 SkAutoTDelete<SkAndroidCodec> codec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700364 if (isIncomplete) {
365 size_t size = stream->getLength();
366 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
msarett3d9d7a72015-10-21 10:27:10 -0700367 codec.reset(SkAndroidCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700368 } else {
msarett3d9d7a72015-10-21 10:27:10 -0700369 codec.reset(SkAndroidCodec::NewFromStream(stream.detach()));
msarette6dd0042015-10-09 11:07:34 -0700370 }
msarettcc7f3052015-10-05 14:20:27 -0700371 if (!codec) {
372 ERRORF(r, "Unable to decode '%s'", path);
373 return;
374 }
375
376 SkBitmap bm;
377 SkMD5::Digest scaledCodecDigest;
scroggo27c17282015-10-27 08:14:46 -0700378 test_android_codec(r, codec, bm, info, size, expectedResult,
msarett3d9d7a72015-10-21 10:27:10 -0700379 &scaledCodecDigest, &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700380 }
381
382 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
383 if (isIncomplete) {
scroggo27c17282015-10-27 08:14:46 -0700384 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false);
msarettcc7f3052015-10-05 14:20:27 -0700385 }
halcanarya096d7a2015-03-27 12:16:53 -0700386}
387
388DEF_TEST(Codec, r) {
389 // WBMP
msarett99f567e2015-08-05 12:58:26 -0700390 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700391
scroggo6f5e6192015-06-18 12:53:43 -0700392 // WEBP
scroggob636b452015-07-22 07:16:20 -0700393 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true);
394 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true);
395 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true);
scroggo6f5e6192015-06-18 12:53:43 -0700396
halcanarya096d7a2015-03-27 12:16:53 -0700397 // BMP
msarett5406d6f2015-08-31 06:55:13 -0700398 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700399
400 // ICO
msarette6dd0042015-10-09 11:07:34 -0700401 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700402 // These two tests examine interestingly different behavior:
403 // Decodes an embedded BMP image
msarettbe8216a2015-12-04 08:00:50 -0800404 check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700405 // Decodes an embedded PNG image
msarettbe8216a2015-12-04 08:00:50 -0800406 check(r, "google_chrome.ico", SkISize::Make(256, 256), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700407
msarett438b2ad2015-04-09 12:43:10 -0700408 // GIF
msarette6dd0042015-10-09 11:07:34 -0700409 // FIXME: We are not ready to test incomplete GIFs
scroggo27c17282015-10-27 08:14:46 -0700410 check(r, "box.gif", SkISize::Make(200, 55), true, false, false);
411 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700412 // randPixels.gif is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700413 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, false);
msarett438b2ad2015-04-09 12:43:10 -0700414
msarette16b04a2015-04-15 07:32:19 -0700415 // JPG
scroggo27c17282015-10-27 08:14:46 -0700416 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false);
scroggob636b452015-07-22 07:16:20 -0700417 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
msarette6dd0042015-10-09 11:07:34 -0700418 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700419 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggob636b452015-07-22 07:16:20 -0700420 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
msarette6dd0042015-10-09 11:07:34 -0700421 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700422 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
msarette16b04a2015-04-15 07:32:19 -0700423
halcanarya096d7a2015-03-27 12:16:53 -0700424 // PNG
scroggo27c17282015-10-27 08:14:46 -0700425 check(r, "arrow.png", SkISize::Make(187, 312), true, false, false);
426 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false, false);
427 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false, false);
428 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false, false);
429 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false, false);
430 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false, false);
431 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false, false);
432 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false, false);
433 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false, false);
434 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false, false);
435 check(r, "plane.png", SkISize::Make(250, 126), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700436 // FIXME: We are not ready to test incomplete interlaced pngs
scroggo27c17282015-10-27 08:14:46 -0700437 check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false, false);
438 check(r, "randPixels.png", SkISize::Make(8, 8), true, false, false);
439 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700440}
scroggo0a7e69c2015-04-03 07:22:22 -0700441
scroggo46c57472015-09-30 08:57:13 -0700442// Test interlaced PNG in stripes, similar to DM's kStripe_Mode
443DEF_TEST(Codec_stripes, r) {
444 const char * path = "plane_interlaced.png";
445 SkAutoTDelete<SkStream> stream(resource(path));
446 if (!stream) {
447 SkDebugf("Missing resource '%s'\n", path);
448 }
449
450 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
451 REPORTER_ASSERT(r, codec);
452
453 if (!codec) {
454 return;
455 }
456
457 switch (codec->getScanlineOrder()) {
458 case SkCodec::kBottomUp_SkScanlineOrder:
459 case SkCodec::kOutOfOrder_SkScanlineOrder:
460 ERRORF(r, "This scanline order will not match the original.");
461 return;
462 default:
463 break;
464 }
465
466 // Baseline for what the image should look like, using N32.
467 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
468
469 SkBitmap bm;
470 bm.allocPixels(info);
471 SkAutoLockPixels autoLockPixels(bm);
472 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
473 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
474
475 SkMD5::Digest digest;
476 md5(bm, &digest);
477
478 // Now decode in stripes
479 const int height = info.height();
480 const int numStripes = 4;
481 int stripeHeight;
482 int remainingLines;
483 SkTDivMod(height, numStripes, &stripeHeight, &remainingLines);
484
485 bm.eraseColor(SK_ColorYELLOW);
486
487 result = codec->startScanlineDecode(info);
488 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
489
490 // Odd stripes
491 for (int i = 1; i < numStripes; i += 2) {
492 // Skip the even stripes
msarette6dd0042015-10-09 11:07:34 -0700493 bool skipResult = codec->skipScanlines(stripeHeight);
494 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700495
msarette6dd0042015-10-09 11:07:34 -0700496 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700497 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700498 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700499 }
500
501 // Even stripes
502 result = codec->startScanlineDecode(info);
503 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
504
505 for (int i = 0; i < numStripes; i += 2) {
msarette6dd0042015-10-09 11:07:34 -0700506 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700507 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700508 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700509
510 // Skip the odd stripes
511 if (i + 1 < numStripes) {
msarette6dd0042015-10-09 11:07:34 -0700512 bool skipResult = codec->skipScanlines(stripeHeight);
513 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700514 }
515 }
516
517 // Remainder at the end
518 if (remainingLines > 0) {
519 result = codec->startScanlineDecode(info);
520 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
521
msarette6dd0042015-10-09 11:07:34 -0700522 bool skipResult = codec->skipScanlines(height - remainingLines);
523 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700524
msarette6dd0042015-10-09 11:07:34 -0700525 int linesDecoded = codec->getScanlines(bm.getAddr(0, height - remainingLines),
scroggo46c57472015-09-30 08:57:13 -0700526 remainingLines, bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700527 REPORTER_ASSERT(r, linesDecoded == remainingLines);
scroggo46c57472015-09-30 08:57:13 -0700528 }
529
530 compare_to_good_digest(r, digest, bm);
531}
532
scroggo0a7e69c2015-04-03 07:22:22 -0700533static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700534 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700535 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700536 REPORTER_ASSERT(r, !codec);
537
msarett3d9d7a72015-10-21 10:27:10 -0700538 SkAndroidCodec* androidCodec =
539 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
540 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700541}
542
543// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
544// even on failure. Test some bad streams.
545DEF_TEST(Codec_leaks, r) {
546 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
547 const char nonSupportedStream[] = "hello world";
548 // The other strings should look like the beginning of a file type, so we'll call some
549 // internal version of NewFromStream, which must also delete the stream on failure.
550 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
551 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
552 const char emptyWebp[] = "RIFF1234WEBPVP";
553 const char emptyBmp[] = { 'B', 'M' };
554 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
555 const char emptyGif[] = "GIFVER";
556
557 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
558 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
559 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
560 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
561 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
562 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
563 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
564}
msarette16b04a2015-04-15 07:32:19 -0700565
scroggo2c3b2182015-10-09 08:40:59 -0700566DEF_TEST(Codec_null, r) {
567 // Attempting to create an SkCodec or an SkScaledCodec with null should not
568 // crash.
569 SkCodec* codec = SkCodec::NewFromStream(nullptr);
570 REPORTER_ASSERT(r, !codec);
571
msarett3d9d7a72015-10-21 10:27:10 -0700572 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
573 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700574}
575
msarette16b04a2015-04-15 07:32:19 -0700576static void test_dimensions(skiatest::Reporter* r, const char path[]) {
577 // Create the codec from the resource file
578 SkAutoTDelete<SkStream> stream(resource(path));
579 if (!stream) {
580 SkDebugf("Missing resource '%s'\n", path);
581 return;
582 }
msarett3d9d7a72015-10-21 10:27:10 -0700583 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.detach()));
msarette16b04a2015-04-15 07:32:19 -0700584 if (!codec) {
585 ERRORF(r, "Unable to create codec '%s'", path);
586 return;
587 }
588
589 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800590 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700591 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700592 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700593 SkImageInfo scaledInfo = codec->getInfo()
594 .makeWH(scaledDims.width(), scaledDims.height())
595 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700596
597 // Set up for the decode
598 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
599 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
600 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
601
msarett3d9d7a72015-10-21 10:27:10 -0700602 SkAndroidCodec::AndroidOptions options;
603 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700604 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700605 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700606 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700607 }
608}
609
610// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
611DEF_TEST(Codec_Dimensions, r) {
612 // JPG
613 test_dimensions(r, "CMYK.jpg");
614 test_dimensions(r, "color_wheel.jpg");
615 test_dimensions(r, "grayscale.jpg");
616 test_dimensions(r, "mandrill_512_q075.jpg");
617 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700618
619 // Decoding small images with very large scaling factors is a potential
620 // source of bugs and crashes. We disable these tests in Gold because
621 // tiny images are not very useful to look at.
622 // Here we make sure that we do not crash or access illegal memory when
623 // performing scaled decodes on small images.
624 test_dimensions(r, "1x1.png");
625 test_dimensions(r, "2x2.png");
626 test_dimensions(r, "3x3.png");
627 test_dimensions(r, "3x1.png");
628 test_dimensions(r, "1x1.png");
629 test_dimensions(r, "16x1.png");
630 test_dimensions(r, "1x16.png");
631 test_dimensions(r, "mandrill_16.png");
632
msarette16b04a2015-04-15 07:32:19 -0700633}
634
msarettd0375bc2015-08-12 08:08:56 -0700635static void test_invalid(skiatest::Reporter* r, const char path[]) {
msarett4b17fa32015-04-23 08:53:39 -0700636 SkAutoTDelete<SkStream> stream(resource(path));
637 if (!stream) {
638 SkDebugf("Missing resource '%s'\n", path);
639 return;
640 }
641 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700642 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700643}
msarette16b04a2015-04-15 07:32:19 -0700644
msarett4b17fa32015-04-23 08:53:39 -0700645DEF_TEST(Codec_Empty, r) {
646 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700647 test_invalid(r, "empty_images/zero-dims.gif");
648 test_invalid(r, "empty_images/zero-embedded.ico");
649 test_invalid(r, "empty_images/zero-width.bmp");
650 test_invalid(r, "empty_images/zero-height.bmp");
651 test_invalid(r, "empty_images/zero-width.jpg");
652 test_invalid(r, "empty_images/zero-height.jpg");
653 test_invalid(r, "empty_images/zero-width.png");
654 test_invalid(r, "empty_images/zero-height.png");
655 test_invalid(r, "empty_images/zero-width.wbmp");
656 test_invalid(r, "empty_images/zero-height.wbmp");
657 // This image is an ico with an embedded mask-bmp. This is illegal.
658 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700659}
msarett99f567e2015-08-05 12:58:26 -0700660
661static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
662 SkAutoTDelete<SkStream> stream(resource(path));
663 if (!stream) {
664 SkDebugf("Missing resource '%s'\n", path);
665 return;
666 }
scroggo46c57472015-09-30 08:57:13 -0700667 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.detach()));
msarett99f567e2015-08-05 12:58:26 -0700668
669 // This should return kSuccess because kIndex8 is supported.
670 SkPMColor colorStorage[256];
671 int colorCount;
scroggo46c57472015-09-30 08:57:13 -0700672 SkCodec::Result result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700673 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
msarett99f567e2015-08-05 12:58:26 -0700674 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
675 // The rest of the test is uninteresting if kIndex8 is not supported
676 if (SkCodec::kSuccess != result) {
677 return;
678 }
679
680 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
681 // colorPtr and a valid colorCountPtr.
scroggo46c57472015-09-30 08:57:13 -0700682 result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700683 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
msarett99f567e2015-08-05 12:58:26 -0700684 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
scroggo46c57472015-09-30 08:57:13 -0700685 result = decoder->startScanlineDecode(
msarett99f567e2015-08-05 12:58:26 -0700686 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
687 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
688}
689
690DEF_TEST(Codec_Params, r) {
691 test_invalid_parameters(r, "index8.png");
692 test_invalid_parameters(r, "mandrill.wbmp");
693}
scroggocf98fa92015-11-23 08:14:40 -0800694
695static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
696 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
697 if (!sk_stream->write(data, len)) {
698 png_error(png_ptr, "sk_write_fn Error!");
699 }
700}
701
702#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
703DEF_TEST(Codec_pngChunkReader, r) {
704 // Create a dummy bitmap. Use unpremul RGBA for libpng.
705 SkBitmap bm;
706 const int w = 1;
707 const int h = 1;
708 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
709 kUnpremul_SkAlphaType);
710 bm.setInfo(bmInfo);
711 bm.allocPixels();
712 bm.eraseColor(SK_ColorBLUE);
713 SkMD5::Digest goodDigest;
714 md5(bm, &goodDigest);
715
716 // Write to a png file.
717 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
718 REPORTER_ASSERT(r, png);
719 if (!png) {
720 return;
721 }
722
723 png_infop info = png_create_info_struct(png);
724 REPORTER_ASSERT(r, info);
725 if (!info) {
726 png_destroy_write_struct(&png, nullptr);
727 return;
728 }
729
730 if (setjmp(png_jmpbuf(png))) {
731 ERRORF(r, "failed writing png");
732 png_destroy_write_struct(&png, &info);
733 return;
734 }
735
736 SkDynamicMemoryWStream wStream;
737 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
738
739 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
740 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
741 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
742
743 // Create some chunks that match the Android framework's use.
744 static png_unknown_chunk gUnknowns[] = {
745 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_PLTE },
746 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_PLTE },
747 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_PLTE },
748 };
749
750 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
751 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
752#if PNG_LIBPNG_VER < 10600
753 /* Deal with unknown chunk location bug in 1.5.x and earlier */
754 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_PLTE);
755 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_PLTE);
756#endif
757
758 png_write_info(png, info);
759
760 for (int j = 0; j < h; j++) {
761 png_bytep row = (png_bytep)(bm.getAddr(0, j));
762 png_write_rows(png, &row, 1);
763 }
764 png_write_end(png, info);
765 png_destroy_write_struct(&png, &info);
766
767 class ChunkReader : public SkPngChunkReader {
768 public:
769 ChunkReader(skiatest::Reporter* r)
770 : fReporter(r)
771 {
772 this->reset();
773 }
774
775 bool readChunk(const char tag[], const void* data, size_t length) override {
776 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
777 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
778 // Tag matches. This should have been the first time we see it.
779 REPORTER_ASSERT(fReporter, !fSeen[i]);
780 fSeen[i] = true;
781
782 // Data and length should match
783 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
784 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
785 (const char*) gUnknowns[i].data));
786 return true;
787 }
788 }
789 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
790 return true;
791 }
792
793 bool allHaveBeenSeen() {
794 bool ret = true;
795 for (auto seen : fSeen) {
796 ret &= seen;
797 }
798 return ret;
799 }
800
801 void reset() {
802 sk_bzero(fSeen, sizeof(fSeen));
803 }
804
805 private:
806 skiatest::Reporter* fReporter; // Unowned
807 bool fSeen[3];
808 };
809
810 ChunkReader chunkReader(r);
811
812 // Now read the file with SkCodec.
813 SkAutoTUnref<SkData> data(wStream.copyToData());
814 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data, &chunkReader));
815 REPORTER_ASSERT(r, codec);
816 if (!codec) {
817 return;
818 }
819
820 // Now compare to the original.
821 SkBitmap decodedBm;
822 decodedBm.setInfo(codec->getInfo());
823 decodedBm.allocPixels();
824 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
825 decodedBm.rowBytes());
826 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
827
828 if (decodedBm.colorType() != bm.colorType()) {
829 SkBitmap tmp;
830 bool success = decodedBm.copyTo(&tmp, bm.colorType());
831 REPORTER_ASSERT(r, success);
832 if (!success) {
833 return;
834 }
835
836 tmp.swap(decodedBm);
837 }
838
839 compare_to_good_digest(r, goodDigest, decodedBm);
840 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
841
842 // Decoding again will read the chunks again.
843 chunkReader.reset();
844 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
845 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
846 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
847 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
848}
849#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800850
scroggodb30be22015-12-08 18:54:13 -0800851// Stream that can only peek up to a limit
852class LimitedPeekingMemStream : public SkStream {
853public:
854 LimitedPeekingMemStream(SkData* data, size_t limit)
855 : fStream(data)
856 , fLimit(limit) {}
857
858 size_t peek(void* buf, size_t bytes) const override {
859 return fStream.peek(buf, SkTMin(bytes, fLimit));
860 }
861 size_t read(void* buf, size_t bytes) override {
862 return fStream.read(buf, bytes);
863 }
864 bool rewind() override {
865 return fStream.rewind();
866 }
867 bool isAtEnd() const override {
868 return false;
869 }
870private:
871 SkMemoryStream fStream;
872 const size_t fLimit;
873};
874
875// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
876// + rewind() and succeed.
877DEF_TEST(Codec_webp_peek, r) {
878 const char* path = "baby_tux.webp";
879 SkString fullPath(GetResourcePath(path));
880 SkAutoTUnref<SkData> data(SkData::NewFromFileName(fullPath.c_str()));
881 if (!data) {
882 SkDebugf("Missing resource '%s'\n", path);
883 return;
884 }
885
886 // The limit is less than webp needs to peek or read.
887 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 25)));
888 REPORTER_ASSERT(r, codec);
889
890 test_info(r, codec, codec->getInfo(), SkCodec::kSuccess, nullptr);
891
892 // Similarly, a stream which does not peek should still succeed.
893 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0)));
894 REPORTER_ASSERT(r, codec);
895
896 test_info(r, codec, codec->getInfo(), SkCodec::kSuccess, nullptr);
897}
898
scroggob9a1e342015-11-30 06:25:31 -0800899// SkCodec's wbmp decoder was initially more restrictive than SkImageDecoder.
900// It required the second byte to be zero. But SkImageDecoder allowed a couple
901// of bits to be 1 (so long as they do not overlap with 0x9F). Test that
902// SkCodec now supports an image with these bits set.
903DEF_TEST(Codec_wbmp, r) {
904 const char* path = "mandrill.wbmp";
905 SkAutoTDelete<SkStream> stream(resource(path));
906 if (!stream) {
907 SkDebugf("Missing resource '%s'\n", path);
908 return;
909 }
910
911 // Modify the stream to contain a second byte with some bits set.
912 SkAutoTUnref<SkData> data(SkCopyStreamToData(stream));
913 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
914 writeableData[1] = static_cast<uint8_t>(~0x9F);
915
916 // SkImageDecoder supports this.
917 SkBitmap bitmap;
918 REPORTER_ASSERT(r, SkImageDecoder::DecodeMemory(data->data(), data->size(), &bitmap));
919
920 // So SkCodec should, too.
921 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
922 REPORTER_ASSERT(r, codec);
923 if (!codec) {
924 return;
925 }
926 test_info(r, codec, codec->getInfo(), SkCodec::kSuccess, nullptr);
927}
scroggodb30be22015-12-08 18:54:13 -0800928
929// wbmp images have a header that can be arbitrarily large, depending on the
930// size of the image. We cap the size at 65535, meaning we only need to look at
931// 8 bytes to determine whether we can read the image. This is important
932// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
933// image is a wbmp.
934DEF_TEST(Codec_wbmp_max_size, r) {
935 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
936 0x83, 0xFF, 0x7F, // W: 65535
937 0x83, 0xFF, 0x7F }; // H: 65535
938 SkAutoTDelete<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
939 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
940
941 REPORTER_ASSERT(r, codec);
942 if (!codec) return;
943
944 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
945 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
946
947 // Now test an image which is too big. Any image with a larger header (i.e.
948 // has bigger width/height) is also too big.
949 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
950 0x84, 0x80, 0x00, // W: 65536
951 0x84, 0x80, 0x00 }; // H: 65536
952 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
953 codec.reset(SkCodec::NewFromStream(stream.detach()));
954
955 REPORTER_ASSERT(r, !codec);
956}