blob: 747d064481ef9e1f7daa5fbfb23ce8fdd81040d1 [file] [log] [blame]
halcanarya096d7a2015-03-27 12:16:53 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Resources.h"
msarett3d9d7a72015-10-21 10:27:10 -07009#include "SkAndroidCodec.h"
halcanarya096d7a2015-03-27 12:16:53 -070010#include "SkBitmap.h"
11#include "SkCodec.h"
msarettedd2dcf2016-01-14 13:12:26 -080012#include "SkCodecImageGenerator.h"
msarette6dd0042015-10-09 11:07:34 -070013#include "SkData.h"
scroggob9a1e342015-11-30 06:25:31 -080014#include "SkImageDecoder.h"
halcanarya096d7a2015-03-27 12:16:53 -070015#include "SkMD5.h"
scroggob636b452015-07-22 07:16:20 -070016#include "SkRandom.h"
scroggocf98fa92015-11-23 08:14:40 -080017#include "SkStream.h"
scroggob9a1e342015-11-30 06:25:31 -080018#include "SkStreamPriv.h"
scroggocf98fa92015-11-23 08:14:40 -080019#include "SkPngChunkReader.h"
halcanarya096d7a2015-03-27 12:16:53 -070020#include "Test.h"
21
scroggocf98fa92015-11-23 08:14:40 -080022#include "png.h"
23
halcanarya096d7a2015-03-27 12:16:53 -070024static SkStreamAsset* resource(const char path[]) {
25 SkString fullPath = GetResourcePath(path);
26 return SkStream::NewFromFile(fullPath.c_str());
27}
28
29static void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
30 SkAutoLockPixels autoLockPixels(bm);
31 SkASSERT(bm.getPixels());
32 SkMD5 md5;
33 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
34 for (int y = 0; y < bm.height(); ++y) {
35 md5.update(static_cast<uint8_t*>(bm.getAddr(0, y)), rowLen);
36 }
37 md5.finish(*digest);
38}
39
scroggo9b2cdbf42015-07-10 12:07:02 -070040/**
41 * Compute the digest for bm and compare it to a known good digest.
42 * @param r Reporter to assert that bm's digest matches goodDigest.
43 * @param goodDigest The known good digest to compare to.
44 * @param bm The bitmap to test.
45 */
46static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
47 const SkBitmap& bm) {
48 SkMD5::Digest digest;
49 md5(bm, &digest);
50 REPORTER_ASSERT(r, digest == goodDigest);
51}
52
scroggod1bc5742015-08-12 08:31:44 -070053/**
54 * Test decoding an SkCodec to a particular SkImageInfo.
55 *
halcanary96fcdcc2015-08-27 07:41:13 -070056 * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr,
scroggod1bc5742015-08-12 08:31:44 -070057 * the resulting decode should match.
58 */
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);
scroggo27c17282015-10-27 08:14:46 -0700106 SkCodec::Result expected565 = info.alphaType() == kOpaque_SkAlphaType ?
msarette6dd0042015-10-09 11:07:34 -0700107 expectedResult : SkCodec::kInvalidConversion;
108 test_info(r, codec, info565, expected565, nullptr);
msarett8ff6ca62015-09-18 12:06:04 -0700109 }
110
111 // Verify that re-decoding gives the same result. It is interesting to check this after
112 // a decode to 565, since choosing to decode to 565 may result in some of the decode
113 // options being modified. These options should return to their defaults on another
114 // decode to kN32, so the new digest should match the old digest.
msarette6dd0042015-10-09 11:07:34 -0700115 test_info(r, codec, info, expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700116
117 {
118 // Check alpha type conversions
119 if (info.alphaType() == kOpaque_SkAlphaType) {
120 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800121 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700122 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800123 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700124 } else {
125 // Decoding to opaque should fail
126 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700127 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700128 SkAlphaType otherAt = info.alphaType();
129 if (kPremul_SkAlphaType == otherAt) {
130 otherAt = kUnpremul_SkAlphaType;
131 } else {
132 otherAt = kPremul_SkAlphaType;
133 }
134 // The other non-opaque alpha type should always succeed, but not match.
msarette6dd0042015-10-09 11:07:34 -0700135 test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700136 }
137 }
msarettcc7f3052015-10-05 14:20:27 -0700138}
139
scroggo2c3b2182015-10-09 08:40:59 -0700140// FIXME: SkScaledCodec is currently only supported for types used by BRD
halcanary6950de62015-11-07 05:29:00 -0800141// https://bug.skia.org/4428
scroggo2c3b2182015-10-09 08:40:59 -0700142static bool supports_scaled_codec(const char path[]) {
143 static const char* const exts[] = {
144 "jpg", "jpeg", "png", "webp"
145 "JPG", "JPEG", "PNG", "WEBP"
146 };
147
148 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
149 if (SkStrEndsWith(path, exts[i])) {
150 return true;
151 }
152 }
153 return false;
154}
155
msarettcc7f3052015-10-05 14:20:27 -0700156static void check(skiatest::Reporter* r,
157 const char path[],
158 SkISize size,
159 bool supportsScanlineDecoding,
160 bool supportsSubsetDecoding,
msarette6dd0042015-10-09 11:07:34 -0700161 bool supportsIncomplete = true) {
msarettcc7f3052015-10-05 14:20:27 -0700162
163 SkAutoTDelete<SkStream> stream(resource(path));
164 if (!stream) {
165 SkDebugf("Missing resource '%s'\n", path);
166 return;
167 }
msarette6dd0042015-10-09 11:07:34 -0700168
169 SkAutoTDelete<SkCodec> codec(nullptr);
170 bool isIncomplete = supportsIncomplete;
171 if (isIncomplete) {
172 size_t size = stream->getLength();
173 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
174 codec.reset(SkCodec::NewFromData(data));
175 } else {
176 codec.reset(SkCodec::NewFromStream(stream.detach()));
177 }
msarettcc7f3052015-10-05 14:20:27 -0700178 if (!codec) {
179 ERRORF(r, "Unable to decode '%s'", path);
180 return;
181 }
182
183 // Test full image decodes with SkCodec
184 SkMD5::Digest codecDigest;
185 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
186 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700187 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
scroggo7b5e5532016-02-04 06:14:24 -0800188 test_codec(r, codec.get(), bm, info, size, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700189
190 // Scanline decoding follows.
msarettcc7f3052015-10-05 14:20:27 -0700191 // Need to call startScanlineDecode() first.
scroggo46c57472015-09-30 08:57:13 -0700192 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700193 == 0);
scroggo46c57472015-09-30 08:57:13 -0700194 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700195 == 0);
scroggo46c57472015-09-30 08:57:13 -0700196
197 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700198 if (supportsScanlineDecoding) {
199 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700200
scroggo46c57472015-09-30 08:57:13 -0700201 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700202
scroggo58421542015-04-01 11:25:20 -0700203 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700204 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
205 if (!isIncomplete) {
206 REPORTER_ASSERT(r, 1 == lines);
207 }
scroggo58421542015-04-01 11:25:20 -0700208 }
209 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700210 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700211 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700212 }
scroggo46c57472015-09-30 08:57:13 -0700213
214 // Cannot continue to decode scanlines beyond the end
215 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700216 == 0);
scroggo46c57472015-09-30 08:57:13 -0700217
218 // Interrupting a scanline decode with a full decode starts from
219 // scratch
220 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700221 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
222 if (!isIncomplete) {
223 REPORTER_ASSERT(r, lines == 1);
224 }
scroggo46c57472015-09-30 08:57:13 -0700225 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700226 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700227 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700228 == 0);
scroggo46c57472015-09-30 08:57:13 -0700229 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700230 == 0);
msarett80803ff2015-10-16 10:54:12 -0700231
232 // Test partial scanline decodes
233 if (supports_scaled_codec(path) && info.width() >= 3) {
234 SkCodec::Options options;
235 int width = info.width();
236 int height = info.height();
237 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
238 options.fSubset = &subset;
239
240 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
241 nullptr, nullptr);
242 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
243
244 for (int y = 0; y < height; y++) {
245 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
246 if (!isIncomplete) {
247 REPORTER_ASSERT(r, 1 == lines);
248 }
249 }
250 }
scroggo58421542015-04-01 11:25:20 -0700251 } else {
scroggo46c57472015-09-30 08:57:13 -0700252 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700253 }
scroggob636b452015-07-22 07:16:20 -0700254
255 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
256 // random subsets.
257 // Do not attempt to decode subsets of an image of only once pixel, since there is no
258 // meaningful subset.
259 if (size.width() * size.height() == 1) {
260 return;
261 }
262
263 SkRandom rand;
264 SkIRect subset;
265 SkCodec::Options opts;
266 opts.fSubset = &subset;
267 for (int i = 0; i < 5; i++) {
268 subset = generate_random_subset(&rand, size.width(), size.height());
269 SkASSERT(!subset.isEmpty());
270 const bool supported = codec->getValidSubset(&subset);
271 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
272
273 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
274 SkBitmap bm;
275 bm.allocPixels(subsetInfo);
276 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700277 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700278
279 if (supportsSubsetDecoding) {
msarette6dd0042015-10-09 11:07:34 -0700280 REPORTER_ASSERT(r, result == expectedResult);
scroggob636b452015-07-22 07:16:20 -0700281 // Webp is the only codec that supports subsets, and it will have modified the subset
282 // to have even left/top.
283 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
284 } else {
285 // No subsets will work.
286 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
287 }
288 }
msarettcc7f3052015-10-05 14:20:27 -0700289
290 // SkScaledCodec tests
scroggo2c3b2182015-10-09 08:40:59 -0700291 if ((supportsScanlineDecoding || supportsSubsetDecoding) && supports_scaled_codec(path)) {
292
msarettcc7f3052015-10-05 14:20:27 -0700293 SkAutoTDelete<SkStream> stream(resource(path));
294 if (!stream) {
295 SkDebugf("Missing resource '%s'\n", path);
296 return;
297 }
msarette6dd0042015-10-09 11:07:34 -0700298
scroggo7b5e5532016-02-04 06:14:24 -0800299 SkAutoTDelete<SkAndroidCodec> androidCodec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700300 if (isIncomplete) {
301 size_t size = stream->getLength();
302 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
scroggo7b5e5532016-02-04 06:14:24 -0800303 androidCodec.reset(SkAndroidCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700304 } else {
scroggo7b5e5532016-02-04 06:14:24 -0800305 androidCodec.reset(SkAndroidCodec::NewFromStream(stream.detach()));
msarette6dd0042015-10-09 11:07:34 -0700306 }
scroggo7b5e5532016-02-04 06:14:24 -0800307 if (!androidCodec) {
msarettcc7f3052015-10-05 14:20:27 -0700308 ERRORF(r, "Unable to decode '%s'", path);
309 return;
310 }
311
312 SkBitmap bm;
313 SkMD5::Digest scaledCodecDigest;
scroggo7b5e5532016-02-04 06:14:24 -0800314 test_codec(r, androidCodec.get(), bm, info, size, expectedResult, &scaledCodecDigest,
315 &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700316 }
317
msarettedd2dcf2016-01-14 13:12:26 -0800318 // Test SkCodecImageGenerator
319 if (!isIncomplete) {
320 SkAutoTDelete<SkStream> stream(resource(path));
321 SkAutoTUnref<SkData> fullData(SkData::NewFromStream(stream, stream->getLength()));
322 SkAutoTDelete<SkImageGenerator> gen(SkCodecImageGenerator::NewFromEncodedCodec(fullData));
323 SkBitmap bm;
324 bm.allocPixels(info);
325 SkAutoLockPixels autoLockPixels(bm);
326 REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes()));
327 compare_to_good_digest(r, codecDigest, bm);
328 }
329
msarette6dd0042015-10-09 11:07:34 -0700330 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
331 if (isIncomplete) {
scroggo27c17282015-10-27 08:14:46 -0700332 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false);
msarettcc7f3052015-10-05 14:20:27 -0700333 }
halcanarya096d7a2015-03-27 12:16:53 -0700334}
335
336DEF_TEST(Codec, r) {
337 // WBMP
msarett99f567e2015-08-05 12:58:26 -0700338 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700339
scroggo6f5e6192015-06-18 12:53:43 -0700340 // WEBP
scroggob636b452015-07-22 07:16:20 -0700341 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true);
342 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true);
343 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true);
scroggo6f5e6192015-06-18 12:53:43 -0700344
halcanarya096d7a2015-03-27 12:16:53 -0700345 // BMP
msarett5406d6f2015-08-31 06:55:13 -0700346 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700347
348 // ICO
msarette6dd0042015-10-09 11:07:34 -0700349 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700350 // These two tests examine interestingly different behavior:
351 // Decodes an embedded BMP image
msarettbe8216a2015-12-04 08:00:50 -0800352 check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700353 // Decodes an embedded PNG image
msarettbe8216a2015-12-04 08:00:50 -0800354 check(r, "google_chrome.ico", SkISize::Make(256, 256), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700355
msarett438b2ad2015-04-09 12:43:10 -0700356 // GIF
msarette6dd0042015-10-09 11:07:34 -0700357 // FIXME: We are not ready to test incomplete GIFs
scroggo27c17282015-10-27 08:14:46 -0700358 check(r, "box.gif", SkISize::Make(200, 55), true, false, false);
359 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700360 // randPixels.gif is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700361 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, false);
msarett438b2ad2015-04-09 12:43:10 -0700362
msarette16b04a2015-04-15 07:32:19 -0700363 // JPG
scroggo27c17282015-10-27 08:14:46 -0700364 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false);
scroggob636b452015-07-22 07:16:20 -0700365 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
msarette6dd0042015-10-09 11:07:34 -0700366 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700367 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggob636b452015-07-22 07:16:20 -0700368 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
msarette6dd0042015-10-09 11:07:34 -0700369 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700370 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
msarette16b04a2015-04-15 07:32:19 -0700371
halcanarya096d7a2015-03-27 12:16:53 -0700372 // PNG
scroggo27c17282015-10-27 08:14:46 -0700373 check(r, "arrow.png", SkISize::Make(187, 312), true, false, false);
374 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false, false);
375 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false, false);
376 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false, false);
377 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false, false);
378 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false, false);
379 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false, false);
380 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false, false);
381 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false, false);
382 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false, false);
383 check(r, "plane.png", SkISize::Make(250, 126), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700384 // FIXME: We are not ready to test incomplete interlaced pngs
scroggo27c17282015-10-27 08:14:46 -0700385 check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false, false);
386 check(r, "randPixels.png", SkISize::Make(8, 8), true, false, false);
387 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false, false);
yujieqin916de9f2016-01-25 08:26:16 -0800388
389 // RAW
msarett02cb4d42016-01-25 11:01:34 -0800390#if defined(SK_CODEC_DECODES_RAW)
yujieqin916de9f2016-01-25 08:26:16 -0800391 check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
msarett02cb4d42016-01-25 11:01:34 -0800392#endif
halcanarya096d7a2015-03-27 12:16:53 -0700393}
scroggo0a7e69c2015-04-03 07:22:22 -0700394
scroggo46c57472015-09-30 08:57:13 -0700395// Test interlaced PNG in stripes, similar to DM's kStripe_Mode
396DEF_TEST(Codec_stripes, r) {
397 const char * path = "plane_interlaced.png";
398 SkAutoTDelete<SkStream> stream(resource(path));
399 if (!stream) {
400 SkDebugf("Missing resource '%s'\n", path);
401 }
402
403 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
404 REPORTER_ASSERT(r, codec);
405
406 if (!codec) {
407 return;
408 }
409
410 switch (codec->getScanlineOrder()) {
411 case SkCodec::kBottomUp_SkScanlineOrder:
412 case SkCodec::kOutOfOrder_SkScanlineOrder:
413 ERRORF(r, "This scanline order will not match the original.");
414 return;
415 default:
416 break;
417 }
418
419 // Baseline for what the image should look like, using N32.
420 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
421
422 SkBitmap bm;
423 bm.allocPixels(info);
424 SkAutoLockPixels autoLockPixels(bm);
425 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
426 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
427
428 SkMD5::Digest digest;
429 md5(bm, &digest);
430
431 // Now decode in stripes
432 const int height = info.height();
433 const int numStripes = 4;
434 int stripeHeight;
435 int remainingLines;
436 SkTDivMod(height, numStripes, &stripeHeight, &remainingLines);
437
438 bm.eraseColor(SK_ColorYELLOW);
439
440 result = codec->startScanlineDecode(info);
441 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
442
443 // Odd stripes
444 for (int i = 1; i < numStripes; i += 2) {
445 // Skip the even stripes
msarette6dd0042015-10-09 11:07:34 -0700446 bool skipResult = codec->skipScanlines(stripeHeight);
447 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700448
msarette6dd0042015-10-09 11:07:34 -0700449 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700450 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700451 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700452 }
453
454 // Even stripes
455 result = codec->startScanlineDecode(info);
456 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
457
458 for (int i = 0; i < numStripes; i += 2) {
msarette6dd0042015-10-09 11:07:34 -0700459 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700460 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700461 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700462
463 // Skip the odd stripes
464 if (i + 1 < numStripes) {
msarette6dd0042015-10-09 11:07:34 -0700465 bool skipResult = codec->skipScanlines(stripeHeight);
466 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700467 }
468 }
469
470 // Remainder at the end
471 if (remainingLines > 0) {
472 result = codec->startScanlineDecode(info);
473 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
474
msarette6dd0042015-10-09 11:07:34 -0700475 bool skipResult = codec->skipScanlines(height - remainingLines);
476 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700477
msarette6dd0042015-10-09 11:07:34 -0700478 int linesDecoded = codec->getScanlines(bm.getAddr(0, height - remainingLines),
scroggo46c57472015-09-30 08:57:13 -0700479 remainingLines, bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700480 REPORTER_ASSERT(r, linesDecoded == remainingLines);
scroggo46c57472015-09-30 08:57:13 -0700481 }
482
483 compare_to_good_digest(r, digest, bm);
484}
485
scroggo0a7e69c2015-04-03 07:22:22 -0700486static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700487 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700488 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700489 REPORTER_ASSERT(r, !codec);
490
msarett3d9d7a72015-10-21 10:27:10 -0700491 SkAndroidCodec* androidCodec =
492 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
493 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700494}
495
496// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
497// even on failure. Test some bad streams.
498DEF_TEST(Codec_leaks, r) {
499 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
500 const char nonSupportedStream[] = "hello world";
501 // The other strings should look like the beginning of a file type, so we'll call some
502 // internal version of NewFromStream, which must also delete the stream on failure.
503 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
504 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
505 const char emptyWebp[] = "RIFF1234WEBPVP";
506 const char emptyBmp[] = { 'B', 'M' };
507 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
508 const char emptyGif[] = "GIFVER";
509
510 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
511 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
512 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
513 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
514 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
515 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
516 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
517}
msarette16b04a2015-04-15 07:32:19 -0700518
scroggo2c3b2182015-10-09 08:40:59 -0700519DEF_TEST(Codec_null, r) {
520 // Attempting to create an SkCodec or an SkScaledCodec with null should not
521 // crash.
522 SkCodec* codec = SkCodec::NewFromStream(nullptr);
523 REPORTER_ASSERT(r, !codec);
524
msarett3d9d7a72015-10-21 10:27:10 -0700525 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
526 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700527}
528
msarette16b04a2015-04-15 07:32:19 -0700529static void test_dimensions(skiatest::Reporter* r, const char path[]) {
530 // Create the codec from the resource file
531 SkAutoTDelete<SkStream> stream(resource(path));
532 if (!stream) {
533 SkDebugf("Missing resource '%s'\n", path);
534 return;
535 }
msarett3d9d7a72015-10-21 10:27:10 -0700536 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.detach()));
msarette16b04a2015-04-15 07:32:19 -0700537 if (!codec) {
538 ERRORF(r, "Unable to create codec '%s'", path);
539 return;
540 }
541
542 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800543 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700544 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700545 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700546 SkImageInfo scaledInfo = codec->getInfo()
547 .makeWH(scaledDims.width(), scaledDims.height())
548 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700549
550 // Set up for the decode
551 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
552 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
553 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
554
msarett3d9d7a72015-10-21 10:27:10 -0700555 SkAndroidCodec::AndroidOptions options;
556 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700557 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700558 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700559 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700560 }
561}
562
563// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
564DEF_TEST(Codec_Dimensions, r) {
565 // JPG
566 test_dimensions(r, "CMYK.jpg");
567 test_dimensions(r, "color_wheel.jpg");
568 test_dimensions(r, "grayscale.jpg");
569 test_dimensions(r, "mandrill_512_q075.jpg");
570 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700571
572 // Decoding small images with very large scaling factors is a potential
573 // source of bugs and crashes. We disable these tests in Gold because
574 // tiny images are not very useful to look at.
575 // Here we make sure that we do not crash or access illegal memory when
576 // performing scaled decodes on small images.
577 test_dimensions(r, "1x1.png");
578 test_dimensions(r, "2x2.png");
579 test_dimensions(r, "3x3.png");
580 test_dimensions(r, "3x1.png");
581 test_dimensions(r, "1x1.png");
582 test_dimensions(r, "16x1.png");
583 test_dimensions(r, "1x16.png");
584 test_dimensions(r, "mandrill_16.png");
585
yujieqin916de9f2016-01-25 08:26:16 -0800586 // RAW
msarett8e49ca32016-01-25 13:10:58 -0800587#if defined(SK_CODEC_DECODES_RAW)
yujieqin916de9f2016-01-25 08:26:16 -0800588 test_dimensions(r, "sample_1mp.dng");
msarett8e49ca32016-01-25 13:10:58 -0800589#endif
msarette16b04a2015-04-15 07:32:19 -0700590}
591
msarettd0375bc2015-08-12 08:08:56 -0700592static void test_invalid(skiatest::Reporter* r, const char path[]) {
msarett4b17fa32015-04-23 08:53:39 -0700593 SkAutoTDelete<SkStream> stream(resource(path));
594 if (!stream) {
595 SkDebugf("Missing resource '%s'\n", path);
596 return;
597 }
598 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700599 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700600}
msarette16b04a2015-04-15 07:32:19 -0700601
msarett4b17fa32015-04-23 08:53:39 -0700602DEF_TEST(Codec_Empty, r) {
603 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700604 test_invalid(r, "empty_images/zero-dims.gif");
605 test_invalid(r, "empty_images/zero-embedded.ico");
606 test_invalid(r, "empty_images/zero-width.bmp");
607 test_invalid(r, "empty_images/zero-height.bmp");
608 test_invalid(r, "empty_images/zero-width.jpg");
609 test_invalid(r, "empty_images/zero-height.jpg");
610 test_invalid(r, "empty_images/zero-width.png");
611 test_invalid(r, "empty_images/zero-height.png");
612 test_invalid(r, "empty_images/zero-width.wbmp");
613 test_invalid(r, "empty_images/zero-height.wbmp");
614 // This image is an ico with an embedded mask-bmp. This is illegal.
615 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700616}
msarett99f567e2015-08-05 12:58:26 -0700617
618static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
619 SkAutoTDelete<SkStream> stream(resource(path));
620 if (!stream) {
621 SkDebugf("Missing resource '%s'\n", path);
622 return;
623 }
scroggo46c57472015-09-30 08:57:13 -0700624 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.detach()));
msarett99f567e2015-08-05 12:58:26 -0700625
626 // This should return kSuccess because kIndex8 is supported.
627 SkPMColor colorStorage[256];
628 int colorCount;
scroggo46c57472015-09-30 08:57:13 -0700629 SkCodec::Result result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700630 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
msarett99f567e2015-08-05 12:58:26 -0700631 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
632 // The rest of the test is uninteresting if kIndex8 is not supported
633 if (SkCodec::kSuccess != result) {
634 return;
635 }
636
637 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
638 // colorPtr and a valid colorCountPtr.
scroggo46c57472015-09-30 08:57:13 -0700639 result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700640 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
msarett99f567e2015-08-05 12:58:26 -0700641 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
scroggo46c57472015-09-30 08:57:13 -0700642 result = decoder->startScanlineDecode(
msarett99f567e2015-08-05 12:58:26 -0700643 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
644 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
645}
646
647DEF_TEST(Codec_Params, r) {
648 test_invalid_parameters(r, "index8.png");
649 test_invalid_parameters(r, "mandrill.wbmp");
650}
scroggocf98fa92015-11-23 08:14:40 -0800651
652static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
653 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
654 if (!sk_stream->write(data, len)) {
655 png_error(png_ptr, "sk_write_fn Error!");
656 }
657}
658
659#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
660DEF_TEST(Codec_pngChunkReader, r) {
661 // Create a dummy bitmap. Use unpremul RGBA for libpng.
662 SkBitmap bm;
663 const int w = 1;
664 const int h = 1;
665 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
666 kUnpremul_SkAlphaType);
667 bm.setInfo(bmInfo);
668 bm.allocPixels();
669 bm.eraseColor(SK_ColorBLUE);
670 SkMD5::Digest goodDigest;
671 md5(bm, &goodDigest);
672
673 // Write to a png file.
674 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
675 REPORTER_ASSERT(r, png);
676 if (!png) {
677 return;
678 }
679
680 png_infop info = png_create_info_struct(png);
681 REPORTER_ASSERT(r, info);
682 if (!info) {
683 png_destroy_write_struct(&png, nullptr);
684 return;
685 }
686
687 if (setjmp(png_jmpbuf(png))) {
688 ERRORF(r, "failed writing png");
689 png_destroy_write_struct(&png, &info);
690 return;
691 }
692
693 SkDynamicMemoryWStream wStream;
694 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
695
696 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
697 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
698 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
699
700 // Create some chunks that match the Android framework's use.
701 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800702 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
703 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
704 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800705 };
706
707 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
708 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
709#if PNG_LIBPNG_VER < 10600
710 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800711 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
712 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800713#endif
714
715 png_write_info(png, info);
716
717 for (int j = 0; j < h; j++) {
718 png_bytep row = (png_bytep)(bm.getAddr(0, j));
719 png_write_rows(png, &row, 1);
720 }
721 png_write_end(png, info);
722 png_destroy_write_struct(&png, &info);
723
724 class ChunkReader : public SkPngChunkReader {
725 public:
726 ChunkReader(skiatest::Reporter* r)
727 : fReporter(r)
728 {
729 this->reset();
730 }
731
732 bool readChunk(const char tag[], const void* data, size_t length) override {
733 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
734 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
735 // Tag matches. This should have been the first time we see it.
736 REPORTER_ASSERT(fReporter, !fSeen[i]);
737 fSeen[i] = true;
738
739 // Data and length should match
740 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
741 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
742 (const char*) gUnknowns[i].data));
743 return true;
744 }
745 }
746 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
747 return true;
748 }
749
750 bool allHaveBeenSeen() {
751 bool ret = true;
752 for (auto seen : fSeen) {
753 ret &= seen;
754 }
755 return ret;
756 }
757
758 void reset() {
759 sk_bzero(fSeen, sizeof(fSeen));
760 }
761
762 private:
763 skiatest::Reporter* fReporter; // Unowned
764 bool fSeen[3];
765 };
766
767 ChunkReader chunkReader(r);
768
769 // Now read the file with SkCodec.
770 SkAutoTUnref<SkData> data(wStream.copyToData());
771 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data, &chunkReader));
772 REPORTER_ASSERT(r, codec);
773 if (!codec) {
774 return;
775 }
776
777 // Now compare to the original.
778 SkBitmap decodedBm;
779 decodedBm.setInfo(codec->getInfo());
780 decodedBm.allocPixels();
781 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
782 decodedBm.rowBytes());
783 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
784
785 if (decodedBm.colorType() != bm.colorType()) {
786 SkBitmap tmp;
787 bool success = decodedBm.copyTo(&tmp, bm.colorType());
788 REPORTER_ASSERT(r, success);
789 if (!success) {
790 return;
791 }
792
793 tmp.swap(decodedBm);
794 }
795
796 compare_to_good_digest(r, goodDigest, decodedBm);
797 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
798
799 // Decoding again will read the chunks again.
800 chunkReader.reset();
801 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
802 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
803 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
804 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
805}
806#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800807
scroggodb30be22015-12-08 18:54:13 -0800808// Stream that can only peek up to a limit
809class LimitedPeekingMemStream : public SkStream {
810public:
811 LimitedPeekingMemStream(SkData* data, size_t limit)
812 : fStream(data)
813 , fLimit(limit) {}
814
815 size_t peek(void* buf, size_t bytes) const override {
816 return fStream.peek(buf, SkTMin(bytes, fLimit));
817 }
818 size_t read(void* buf, size_t bytes) override {
819 return fStream.read(buf, bytes);
820 }
821 bool rewind() override {
822 return fStream.rewind();
823 }
824 bool isAtEnd() const override {
825 return false;
826 }
827private:
828 SkMemoryStream fStream;
829 const size_t fLimit;
830};
831
832// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
833// + rewind() and succeed.
834DEF_TEST(Codec_webp_peek, r) {
835 const char* path = "baby_tux.webp";
836 SkString fullPath(GetResourcePath(path));
837 SkAutoTUnref<SkData> data(SkData::NewFromFileName(fullPath.c_str()));
838 if (!data) {
839 SkDebugf("Missing resource '%s'\n", path);
840 return;
841 }
842
843 // The limit is less than webp needs to peek or read.
844 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 25)));
845 REPORTER_ASSERT(r, codec);
846
scroggo7b5e5532016-02-04 06:14:24 -0800847 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800848
849 // Similarly, a stream which does not peek should still succeed.
850 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0)));
851 REPORTER_ASSERT(r, codec);
852
scroggo7b5e5532016-02-04 06:14:24 -0800853 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800854}
855
scroggob9a1e342015-11-30 06:25:31 -0800856// SkCodec's wbmp decoder was initially more restrictive than SkImageDecoder.
857// It required the second byte to be zero. But SkImageDecoder allowed a couple
858// of bits to be 1 (so long as they do not overlap with 0x9F). Test that
859// SkCodec now supports an image with these bits set.
860DEF_TEST(Codec_wbmp, r) {
861 const char* path = "mandrill.wbmp";
862 SkAutoTDelete<SkStream> stream(resource(path));
863 if (!stream) {
864 SkDebugf("Missing resource '%s'\n", path);
865 return;
866 }
867
868 // Modify the stream to contain a second byte with some bits set.
869 SkAutoTUnref<SkData> data(SkCopyStreamToData(stream));
870 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
871 writeableData[1] = static_cast<uint8_t>(~0x9F);
872
873 // SkImageDecoder supports this.
874 SkBitmap bitmap;
875 REPORTER_ASSERT(r, SkImageDecoder::DecodeMemory(data->data(), data->size(), &bitmap));
876
877 // So SkCodec should, too.
878 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
879 REPORTER_ASSERT(r, codec);
880 if (!codec) {
881 return;
882 }
scroggo7b5e5532016-02-04 06:14:24 -0800883 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800884}
scroggodb30be22015-12-08 18:54:13 -0800885
886// wbmp images have a header that can be arbitrarily large, depending on the
887// size of the image. We cap the size at 65535, meaning we only need to look at
888// 8 bytes to determine whether we can read the image. This is important
889// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
890// image is a wbmp.
891DEF_TEST(Codec_wbmp_max_size, r) {
892 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
893 0x83, 0xFF, 0x7F, // W: 65535
894 0x83, 0xFF, 0x7F }; // H: 65535
895 SkAutoTDelete<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
896 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
897
898 REPORTER_ASSERT(r, codec);
899 if (!codec) return;
900
901 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
902 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
903
904 // Now test an image which is too big. Any image with a larger header (i.e.
905 // has bigger width/height) is also too big.
906 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
907 0x84, 0x80, 0x00, // W: 65536
908 0x84, 0x80, 0x00 }; // H: 65536
909 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
910 codec.reset(SkCodec::NewFromStream(stream.detach()));
911
912 REPORTER_ASSERT(r, !codec);
913}