blob: c5651ac0af87cbd16c75cd2c3e1c82627126ef60 [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
scroggobed1ed62016-02-11 10:24:55 -0800140static bool supports_partial_scanlines(const char path[]) {
scroggo2c3b2182015-10-09 08:40:59 -0700141 static const char* const exts[] = {
142 "jpg", "jpeg", "png", "webp"
143 "JPG", "JPEG", "PNG", "WEBP"
144 };
145
146 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
147 if (SkStrEndsWith(path, exts[i])) {
148 return true;
149 }
150 }
151 return false;
152}
153
msarettcc7f3052015-10-05 14:20:27 -0700154static void check(skiatest::Reporter* r,
155 const char path[],
156 SkISize size,
157 bool supportsScanlineDecoding,
158 bool supportsSubsetDecoding,
msarette6dd0042015-10-09 11:07:34 -0700159 bool supportsIncomplete = true) {
msarettcc7f3052015-10-05 14:20:27 -0700160
161 SkAutoTDelete<SkStream> stream(resource(path));
162 if (!stream) {
163 SkDebugf("Missing resource '%s'\n", path);
164 return;
165 }
msarette6dd0042015-10-09 11:07:34 -0700166
167 SkAutoTDelete<SkCodec> codec(nullptr);
168 bool isIncomplete = supportsIncomplete;
169 if (isIncomplete) {
170 size_t size = stream->getLength();
171 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
172 codec.reset(SkCodec::NewFromData(data));
173 } else {
174 codec.reset(SkCodec::NewFromStream(stream.detach()));
175 }
msarettcc7f3052015-10-05 14:20:27 -0700176 if (!codec) {
177 ERRORF(r, "Unable to decode '%s'", path);
178 return;
179 }
180
181 // Test full image decodes with SkCodec
182 SkMD5::Digest codecDigest;
183 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
184 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700185 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
scroggo7b5e5532016-02-04 06:14:24 -0800186 test_codec(r, codec.get(), bm, info, size, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700187
188 // Scanline decoding follows.
msarettcc7f3052015-10-05 14:20:27 -0700189 // Need to call startScanlineDecode() first.
scroggo46c57472015-09-30 08:57:13 -0700190 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700191 == 0);
scroggo46c57472015-09-30 08:57:13 -0700192 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700193 == 0);
scroggo46c57472015-09-30 08:57:13 -0700194
195 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700196 if (supportsScanlineDecoding) {
197 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700198
scroggo46c57472015-09-30 08:57:13 -0700199 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700200
scroggo58421542015-04-01 11:25:20 -0700201 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700202 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
203 if (!isIncomplete) {
204 REPORTER_ASSERT(r, 1 == lines);
205 }
scroggo58421542015-04-01 11:25:20 -0700206 }
207 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700208 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700209 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700210 }
scroggo46c57472015-09-30 08:57:13 -0700211
212 // Cannot continue to decode scanlines beyond the end
213 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700214 == 0);
scroggo46c57472015-09-30 08:57:13 -0700215
216 // Interrupting a scanline decode with a full decode starts from
217 // scratch
218 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700219 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
220 if (!isIncomplete) {
221 REPORTER_ASSERT(r, lines == 1);
222 }
scroggo46c57472015-09-30 08:57:13 -0700223 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700224 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700225 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700226 == 0);
scroggo46c57472015-09-30 08:57:13 -0700227 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700228 == 0);
msarett80803ff2015-10-16 10:54:12 -0700229
230 // Test partial scanline decodes
scroggobed1ed62016-02-11 10:24:55 -0800231 if (supports_partial_scanlines(path) && info.width() >= 3) {
msarett80803ff2015-10-16 10:54:12 -0700232 SkCodec::Options options;
233 int width = info.width();
234 int height = info.height();
235 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
236 options.fSubset = &subset;
237
238 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
239 nullptr, nullptr);
240 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
241
242 for (int y = 0; y < height; y++) {
243 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
244 if (!isIncomplete) {
245 REPORTER_ASSERT(r, 1 == lines);
246 }
247 }
248 }
scroggo58421542015-04-01 11:25:20 -0700249 } else {
scroggo46c57472015-09-30 08:57:13 -0700250 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700251 }
scroggob636b452015-07-22 07:16:20 -0700252
253 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
254 // random subsets.
255 // Do not attempt to decode subsets of an image of only once pixel, since there is no
256 // meaningful subset.
257 if (size.width() * size.height() == 1) {
258 return;
259 }
260
261 SkRandom rand;
262 SkIRect subset;
263 SkCodec::Options opts;
264 opts.fSubset = &subset;
265 for (int i = 0; i < 5; i++) {
266 subset = generate_random_subset(&rand, size.width(), size.height());
267 SkASSERT(!subset.isEmpty());
268 const bool supported = codec->getValidSubset(&subset);
269 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
270
271 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
272 SkBitmap bm;
273 bm.allocPixels(subsetInfo);
274 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700275 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700276
277 if (supportsSubsetDecoding) {
msarette6dd0042015-10-09 11:07:34 -0700278 REPORTER_ASSERT(r, result == expectedResult);
scroggob636b452015-07-22 07:16:20 -0700279 // Webp is the only codec that supports subsets, and it will have modified the subset
280 // to have even left/top.
281 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
282 } else {
283 // No subsets will work.
284 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
285 }
286 }
msarettcc7f3052015-10-05 14:20:27 -0700287
scroggobed1ed62016-02-11 10:24:55 -0800288 // SkAndroidCodec tests
289 if (supportsScanlineDecoding || supportsSubsetDecoding) {
scroggo2c3b2182015-10-09 08:40:59 -0700290
msarettcc7f3052015-10-05 14:20:27 -0700291 SkAutoTDelete<SkStream> stream(resource(path));
292 if (!stream) {
293 SkDebugf("Missing resource '%s'\n", path);
294 return;
295 }
msarette6dd0042015-10-09 11:07:34 -0700296
scroggo7b5e5532016-02-04 06:14:24 -0800297 SkAutoTDelete<SkAndroidCodec> androidCodec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700298 if (isIncomplete) {
299 size_t size = stream->getLength();
300 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
scroggo7b5e5532016-02-04 06:14:24 -0800301 androidCodec.reset(SkAndroidCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700302 } else {
scroggo7b5e5532016-02-04 06:14:24 -0800303 androidCodec.reset(SkAndroidCodec::NewFromStream(stream.detach()));
msarette6dd0042015-10-09 11:07:34 -0700304 }
scroggo7b5e5532016-02-04 06:14:24 -0800305 if (!androidCodec) {
msarettcc7f3052015-10-05 14:20:27 -0700306 ERRORF(r, "Unable to decode '%s'", path);
307 return;
308 }
309
310 SkBitmap bm;
scroggobed1ed62016-02-11 10:24:55 -0800311 SkMD5::Digest androidCodecDigest;
312 test_codec(r, androidCodec.get(), bm, info, size, expectedResult, &androidCodecDigest,
scroggo7b5e5532016-02-04 06:14:24 -0800313 &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700314 }
315
msarettedd2dcf2016-01-14 13:12:26 -0800316 // Test SkCodecImageGenerator
317 if (!isIncomplete) {
318 SkAutoTDelete<SkStream> stream(resource(path));
319 SkAutoTUnref<SkData> fullData(SkData::NewFromStream(stream, stream->getLength()));
320 SkAutoTDelete<SkImageGenerator> gen(SkCodecImageGenerator::NewFromEncodedCodec(fullData));
321 SkBitmap bm;
322 bm.allocPixels(info);
323 SkAutoLockPixels autoLockPixels(bm);
324 REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes()));
325 compare_to_good_digest(r, codecDigest, bm);
326 }
327
msarette6dd0042015-10-09 11:07:34 -0700328 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
329 if (isIncomplete) {
scroggo27c17282015-10-27 08:14:46 -0700330 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false);
msarettcc7f3052015-10-05 14:20:27 -0700331 }
halcanarya096d7a2015-03-27 12:16:53 -0700332}
333
334DEF_TEST(Codec, r) {
335 // WBMP
msarett99f567e2015-08-05 12:58:26 -0700336 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700337
scroggo6f5e6192015-06-18 12:53:43 -0700338 // WEBP
scroggob636b452015-07-22 07:16:20 -0700339 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true);
340 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true);
341 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true);
scroggo6f5e6192015-06-18 12:53:43 -0700342
halcanarya096d7a2015-03-27 12:16:53 -0700343 // BMP
msarett5406d6f2015-08-31 06:55:13 -0700344 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false);
msarett4946b942016-02-11 08:41:01 -0800345 check(r, "rle.bmp", SkISize::Make(320, 240), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700346
347 // ICO
msarette6dd0042015-10-09 11:07:34 -0700348 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700349 // These two tests examine interestingly different behavior:
350 // Decodes an embedded BMP image
msarettbe8216a2015-12-04 08:00:50 -0800351 check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700352 // Decodes an embedded PNG image
msarettbe8216a2015-12-04 08:00:50 -0800353 check(r, "google_chrome.ico", SkISize::Make(256, 256), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700354
msarett438b2ad2015-04-09 12:43:10 -0700355 // GIF
msarette6dd0042015-10-09 11:07:34 -0700356 // FIXME: We are not ready to test incomplete GIFs
scroggo27c17282015-10-27 08:14:46 -0700357 check(r, "box.gif", SkISize::Make(200, 55), true, false, false);
358 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700359 // randPixels.gif is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700360 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, false);
msarett438b2ad2015-04-09 12:43:10 -0700361
msarette16b04a2015-04-15 07:32:19 -0700362 // JPG
scroggo27c17282015-10-27 08:14:46 -0700363 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false);
scroggob636b452015-07-22 07:16:20 -0700364 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
msarette6dd0042015-10-09 11:07:34 -0700365 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700366 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggob636b452015-07-22 07:16:20 -0700367 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
msarette6dd0042015-10-09 11:07:34 -0700368 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700369 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
msarette16b04a2015-04-15 07:32:19 -0700370
halcanarya096d7a2015-03-27 12:16:53 -0700371 // PNG
scroggo27c17282015-10-27 08:14:46 -0700372 check(r, "arrow.png", SkISize::Make(187, 312), true, false, false);
373 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false, false);
374 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false, false);
375 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false, false);
376 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false, false);
377 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false, false);
378 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false, false);
379 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false, false);
380 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false, false);
381 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false, false);
382 check(r, "plane.png", SkISize::Make(250, 126), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700383 // FIXME: We are not ready to test incomplete interlaced pngs
scroggo27c17282015-10-27 08:14:46 -0700384 check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false, false);
385 check(r, "randPixels.png", SkISize::Make(8, 8), true, false, false);
386 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false, false);
yujieqin916de9f2016-01-25 08:26:16 -0800387
388 // RAW
msarett02cb4d42016-01-25 11:01:34 -0800389#if defined(SK_CODEC_DECODES_RAW)
yujieqin916de9f2016-01-25 08:26:16 -0800390 check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
ebrauer46d2aa82016-02-17 08:04:00 -0800391 check(r, "sample_1mp_rotated.dng", SkISize::Make(600, 338), false, false, false);
yujieqin9c7a8a42016-02-05 08:21:19 -0800392 check(r, "dng_with_preview.dng", SkISize::Make(600, 338), true, false, false);
msarett02cb4d42016-01-25 11:01:34 -0800393#endif
halcanarya096d7a2015-03-27 12:16:53 -0700394}
scroggo0a7e69c2015-04-03 07:22:22 -0700395
scroggo46c57472015-09-30 08:57:13 -0700396// Test interlaced PNG in stripes, similar to DM's kStripe_Mode
397DEF_TEST(Codec_stripes, r) {
398 const char * path = "plane_interlaced.png";
399 SkAutoTDelete<SkStream> stream(resource(path));
400 if (!stream) {
401 SkDebugf("Missing resource '%s'\n", path);
402 }
403
404 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
405 REPORTER_ASSERT(r, codec);
406
407 if (!codec) {
408 return;
409 }
410
411 switch (codec->getScanlineOrder()) {
412 case SkCodec::kBottomUp_SkScanlineOrder:
413 case SkCodec::kOutOfOrder_SkScanlineOrder:
414 ERRORF(r, "This scanline order will not match the original.");
415 return;
416 default:
417 break;
418 }
419
420 // Baseline for what the image should look like, using N32.
421 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
422
423 SkBitmap bm;
424 bm.allocPixels(info);
425 SkAutoLockPixels autoLockPixels(bm);
426 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
427 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
428
429 SkMD5::Digest digest;
430 md5(bm, &digest);
431
432 // Now decode in stripes
433 const int height = info.height();
434 const int numStripes = 4;
435 int stripeHeight;
436 int remainingLines;
437 SkTDivMod(height, numStripes, &stripeHeight, &remainingLines);
438
439 bm.eraseColor(SK_ColorYELLOW);
440
441 result = codec->startScanlineDecode(info);
442 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
443
444 // Odd stripes
445 for (int i = 1; i < numStripes; i += 2) {
446 // Skip the even stripes
msarette6dd0042015-10-09 11:07:34 -0700447 bool skipResult = codec->skipScanlines(stripeHeight);
448 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700449
msarette6dd0042015-10-09 11:07:34 -0700450 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700451 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700452 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700453 }
454
455 // Even stripes
456 result = codec->startScanlineDecode(info);
457 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
458
459 for (int i = 0; i < numStripes; i += 2) {
msarette6dd0042015-10-09 11:07:34 -0700460 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700461 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700462 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700463
464 // Skip the odd stripes
465 if (i + 1 < numStripes) {
msarette6dd0042015-10-09 11:07:34 -0700466 bool skipResult = codec->skipScanlines(stripeHeight);
467 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700468 }
469 }
470
471 // Remainder at the end
472 if (remainingLines > 0) {
473 result = codec->startScanlineDecode(info);
474 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
475
msarette6dd0042015-10-09 11:07:34 -0700476 bool skipResult = codec->skipScanlines(height - remainingLines);
477 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700478
msarette6dd0042015-10-09 11:07:34 -0700479 int linesDecoded = codec->getScanlines(bm.getAddr(0, height - remainingLines),
scroggo46c57472015-09-30 08:57:13 -0700480 remainingLines, bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700481 REPORTER_ASSERT(r, linesDecoded == remainingLines);
scroggo46c57472015-09-30 08:57:13 -0700482 }
483
484 compare_to_good_digest(r, digest, bm);
485}
486
scroggo0a7e69c2015-04-03 07:22:22 -0700487static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700488 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700489 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700490 REPORTER_ASSERT(r, !codec);
491
msarett3d9d7a72015-10-21 10:27:10 -0700492 SkAndroidCodec* androidCodec =
493 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
494 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700495}
496
497// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
498// even on failure. Test some bad streams.
499DEF_TEST(Codec_leaks, r) {
500 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
501 const char nonSupportedStream[] = "hello world";
502 // The other strings should look like the beginning of a file type, so we'll call some
503 // internal version of NewFromStream, which must also delete the stream on failure.
504 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
505 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
506 const char emptyWebp[] = "RIFF1234WEBPVP";
507 const char emptyBmp[] = { 'B', 'M' };
508 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
509 const char emptyGif[] = "GIFVER";
510
511 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
512 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
513 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
514 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
515 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
516 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
517 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
518}
msarette16b04a2015-04-15 07:32:19 -0700519
scroggo2c3b2182015-10-09 08:40:59 -0700520DEF_TEST(Codec_null, r) {
scroggobed1ed62016-02-11 10:24:55 -0800521 // Attempting to create an SkCodec or an SkAndroidCodec with null should not
scroggo2c3b2182015-10-09 08:40:59 -0700522 // crash.
523 SkCodec* codec = SkCodec::NewFromStream(nullptr);
524 REPORTER_ASSERT(r, !codec);
525
msarett3d9d7a72015-10-21 10:27:10 -0700526 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
527 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700528}
529
msarette16b04a2015-04-15 07:32:19 -0700530static void test_dimensions(skiatest::Reporter* r, const char path[]) {
531 // Create the codec from the resource file
532 SkAutoTDelete<SkStream> stream(resource(path));
533 if (!stream) {
534 SkDebugf("Missing resource '%s'\n", path);
535 return;
536 }
msarett3d9d7a72015-10-21 10:27:10 -0700537 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.detach()));
msarette16b04a2015-04-15 07:32:19 -0700538 if (!codec) {
539 ERRORF(r, "Unable to create codec '%s'", path);
540 return;
541 }
542
543 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800544 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700545 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700546 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700547 SkImageInfo scaledInfo = codec->getInfo()
548 .makeWH(scaledDims.width(), scaledDims.height())
549 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700550
551 // Set up for the decode
552 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
553 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
554 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
555
msarett3d9d7a72015-10-21 10:27:10 -0700556 SkAndroidCodec::AndroidOptions options;
557 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700558 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700559 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700560 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700561 }
562}
563
564// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
565DEF_TEST(Codec_Dimensions, r) {
566 // JPG
567 test_dimensions(r, "CMYK.jpg");
568 test_dimensions(r, "color_wheel.jpg");
569 test_dimensions(r, "grayscale.jpg");
570 test_dimensions(r, "mandrill_512_q075.jpg");
571 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700572
573 // Decoding small images with very large scaling factors is a potential
574 // source of bugs and crashes. We disable these tests in Gold because
575 // tiny images are not very useful to look at.
576 // Here we make sure that we do not crash or access illegal memory when
577 // performing scaled decodes on small images.
578 test_dimensions(r, "1x1.png");
579 test_dimensions(r, "2x2.png");
580 test_dimensions(r, "3x3.png");
581 test_dimensions(r, "3x1.png");
582 test_dimensions(r, "1x1.png");
583 test_dimensions(r, "16x1.png");
584 test_dimensions(r, "1x16.png");
585 test_dimensions(r, "mandrill_16.png");
586
yujieqin916de9f2016-01-25 08:26:16 -0800587 // RAW
msarett8e49ca32016-01-25 13:10:58 -0800588#if defined(SK_CODEC_DECODES_RAW)
yujieqin916de9f2016-01-25 08:26:16 -0800589 test_dimensions(r, "sample_1mp.dng");
ebrauer46d2aa82016-02-17 08:04:00 -0800590 test_dimensions(r, "sample_1mp_rotated.dng");
yujieqin9c7a8a42016-02-05 08:21:19 -0800591 test_dimensions(r, "dng_with_preview.dng");
msarett8e49ca32016-01-25 13:10:58 -0800592#endif
msarette16b04a2015-04-15 07:32:19 -0700593}
594
msarettd0375bc2015-08-12 08:08:56 -0700595static void test_invalid(skiatest::Reporter* r, const char path[]) {
msarett4b17fa32015-04-23 08:53:39 -0700596 SkAutoTDelete<SkStream> stream(resource(path));
597 if (!stream) {
598 SkDebugf("Missing resource '%s'\n", path);
599 return;
600 }
601 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700602 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700603}
msarette16b04a2015-04-15 07:32:19 -0700604
msarett4b17fa32015-04-23 08:53:39 -0700605DEF_TEST(Codec_Empty, r) {
606 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700607 test_invalid(r, "empty_images/zero-dims.gif");
608 test_invalid(r, "empty_images/zero-embedded.ico");
609 test_invalid(r, "empty_images/zero-width.bmp");
610 test_invalid(r, "empty_images/zero-height.bmp");
611 test_invalid(r, "empty_images/zero-width.jpg");
612 test_invalid(r, "empty_images/zero-height.jpg");
613 test_invalid(r, "empty_images/zero-width.png");
614 test_invalid(r, "empty_images/zero-height.png");
615 test_invalid(r, "empty_images/zero-width.wbmp");
616 test_invalid(r, "empty_images/zero-height.wbmp");
617 // This image is an ico with an embedded mask-bmp. This is illegal.
618 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700619}
msarett99f567e2015-08-05 12:58:26 -0700620
621static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
622 SkAutoTDelete<SkStream> stream(resource(path));
623 if (!stream) {
624 SkDebugf("Missing resource '%s'\n", path);
625 return;
626 }
scroggo46c57472015-09-30 08:57:13 -0700627 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.detach()));
msarett99f567e2015-08-05 12:58:26 -0700628
629 // This should return kSuccess because kIndex8 is supported.
630 SkPMColor colorStorage[256];
631 int colorCount;
scroggo46c57472015-09-30 08:57:13 -0700632 SkCodec::Result result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700633 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
msarett99f567e2015-08-05 12:58:26 -0700634 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
635 // The rest of the test is uninteresting if kIndex8 is not supported
636 if (SkCodec::kSuccess != result) {
637 return;
638 }
639
640 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
641 // colorPtr and a valid colorCountPtr.
scroggo46c57472015-09-30 08:57:13 -0700642 result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700643 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
msarett99f567e2015-08-05 12:58:26 -0700644 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
scroggo46c57472015-09-30 08:57:13 -0700645 result = decoder->startScanlineDecode(
msarett99f567e2015-08-05 12:58:26 -0700646 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
647 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
648}
649
650DEF_TEST(Codec_Params, r) {
651 test_invalid_parameters(r, "index8.png");
652 test_invalid_parameters(r, "mandrill.wbmp");
653}
scroggocf98fa92015-11-23 08:14:40 -0800654
655static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
656 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
657 if (!sk_stream->write(data, len)) {
658 png_error(png_ptr, "sk_write_fn Error!");
659 }
660}
661
662#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
663DEF_TEST(Codec_pngChunkReader, r) {
664 // Create a dummy bitmap. Use unpremul RGBA for libpng.
665 SkBitmap bm;
666 const int w = 1;
667 const int h = 1;
668 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
669 kUnpremul_SkAlphaType);
670 bm.setInfo(bmInfo);
671 bm.allocPixels();
672 bm.eraseColor(SK_ColorBLUE);
673 SkMD5::Digest goodDigest;
674 md5(bm, &goodDigest);
675
676 // Write to a png file.
677 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
678 REPORTER_ASSERT(r, png);
679 if (!png) {
680 return;
681 }
682
683 png_infop info = png_create_info_struct(png);
684 REPORTER_ASSERT(r, info);
685 if (!info) {
686 png_destroy_write_struct(&png, nullptr);
687 return;
688 }
689
690 if (setjmp(png_jmpbuf(png))) {
691 ERRORF(r, "failed writing png");
692 png_destroy_write_struct(&png, &info);
693 return;
694 }
695
696 SkDynamicMemoryWStream wStream;
697 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
698
699 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
700 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
701 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
702
703 // Create some chunks that match the Android framework's use.
704 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800705 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
706 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
707 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800708 };
709
710 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
711 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
712#if PNG_LIBPNG_VER < 10600
713 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800714 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
715 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800716#endif
717
718 png_write_info(png, info);
719
720 for (int j = 0; j < h; j++) {
721 png_bytep row = (png_bytep)(bm.getAddr(0, j));
722 png_write_rows(png, &row, 1);
723 }
724 png_write_end(png, info);
725 png_destroy_write_struct(&png, &info);
726
727 class ChunkReader : public SkPngChunkReader {
728 public:
729 ChunkReader(skiatest::Reporter* r)
730 : fReporter(r)
731 {
732 this->reset();
733 }
734
735 bool readChunk(const char tag[], const void* data, size_t length) override {
736 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
737 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
738 // Tag matches. This should have been the first time we see it.
739 REPORTER_ASSERT(fReporter, !fSeen[i]);
740 fSeen[i] = true;
741
742 // Data and length should match
743 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
744 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
745 (const char*) gUnknowns[i].data));
746 return true;
747 }
748 }
749 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
750 return true;
751 }
752
753 bool allHaveBeenSeen() {
754 bool ret = true;
755 for (auto seen : fSeen) {
756 ret &= seen;
757 }
758 return ret;
759 }
760
761 void reset() {
762 sk_bzero(fSeen, sizeof(fSeen));
763 }
764
765 private:
766 skiatest::Reporter* fReporter; // Unowned
767 bool fSeen[3];
768 };
769
770 ChunkReader chunkReader(r);
771
772 // Now read the file with SkCodec.
773 SkAutoTUnref<SkData> data(wStream.copyToData());
774 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data, &chunkReader));
775 REPORTER_ASSERT(r, codec);
776 if (!codec) {
777 return;
778 }
779
780 // Now compare to the original.
781 SkBitmap decodedBm;
782 decodedBm.setInfo(codec->getInfo());
783 decodedBm.allocPixels();
784 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
785 decodedBm.rowBytes());
786 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
787
788 if (decodedBm.colorType() != bm.colorType()) {
789 SkBitmap tmp;
790 bool success = decodedBm.copyTo(&tmp, bm.colorType());
791 REPORTER_ASSERT(r, success);
792 if (!success) {
793 return;
794 }
795
796 tmp.swap(decodedBm);
797 }
798
799 compare_to_good_digest(r, goodDigest, decodedBm);
800 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
801
802 // Decoding again will read the chunks again.
803 chunkReader.reset();
804 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
805 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
806 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
807 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
808}
809#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800810
scroggodb30be22015-12-08 18:54:13 -0800811// Stream that can only peek up to a limit
812class LimitedPeekingMemStream : public SkStream {
813public:
814 LimitedPeekingMemStream(SkData* data, size_t limit)
815 : fStream(data)
816 , fLimit(limit) {}
817
818 size_t peek(void* buf, size_t bytes) const override {
819 return fStream.peek(buf, SkTMin(bytes, fLimit));
820 }
821 size_t read(void* buf, size_t bytes) override {
822 return fStream.read(buf, bytes);
823 }
824 bool rewind() override {
825 return fStream.rewind();
826 }
827 bool isAtEnd() const override {
828 return false;
829 }
830private:
831 SkMemoryStream fStream;
832 const size_t fLimit;
833};
834
yujieqin9c7a8a42016-02-05 08:21:19 -0800835// Stream that is not an asset stream (!hasPosition() or !hasLength())
836class NotAssetMemStream : public SkStream {
837public:
838 NotAssetMemStream(SkData* data) : fStream(data) {}
839
840 bool hasPosition() const override {
841 return false;
842 }
843
844 bool hasLength() const override {
845 return false;
846 }
847
848 size_t peek(void* buf, size_t bytes) const override {
849 return fStream.peek(buf, bytes);
850 }
851 size_t read(void* buf, size_t bytes) override {
852 return fStream.read(buf, bytes);
853 }
854 bool rewind() override {
855 return fStream.rewind();
856 }
857 bool isAtEnd() const override {
858 return fStream.isAtEnd();
859 }
860private:
861 SkMemoryStream fStream;
862};
863
864// Test that the RawCodec works also for not asset stream. This will test the code path using
865// SkRawBufferedStream instead of SkRawAssetStream.
866#if defined(SK_CODEC_DECODES_RAW)
867DEF_TEST(Codec_raw_notseekable, r) {
868 const char* path = "dng_with_preview.dng";
869 SkString fullPath(GetResourcePath(path));
870 SkAutoTUnref<SkData> data(SkData::NewFromFileName(fullPath.c_str()));
871 if (!data) {
872 SkDebugf("Missing resource '%s'\n", path);
873 return;
874 }
875
876 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(data)));
877 REPORTER_ASSERT(r, codec);
878
879 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
880}
881#endif
882
scroggodb30be22015-12-08 18:54:13 -0800883// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
884// + rewind() and succeed.
885DEF_TEST(Codec_webp_peek, r) {
886 const char* path = "baby_tux.webp";
887 SkString fullPath(GetResourcePath(path));
888 SkAutoTUnref<SkData> data(SkData::NewFromFileName(fullPath.c_str()));
889 if (!data) {
890 SkDebugf("Missing resource '%s'\n", path);
891 return;
892 }
893
894 // The limit is less than webp needs to peek or read.
895 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 25)));
896 REPORTER_ASSERT(r, codec);
897
scroggo7b5e5532016-02-04 06:14:24 -0800898 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800899
900 // Similarly, a stream which does not peek should still succeed.
901 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0)));
902 REPORTER_ASSERT(r, codec);
903
scroggo7b5e5532016-02-04 06:14:24 -0800904 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800905}
906
scroggob9a1e342015-11-30 06:25:31 -0800907// SkCodec's wbmp decoder was initially more restrictive than SkImageDecoder.
908// It required the second byte to be zero. But SkImageDecoder allowed a couple
909// of bits to be 1 (so long as they do not overlap with 0x9F). Test that
910// SkCodec now supports an image with these bits set.
911DEF_TEST(Codec_wbmp, r) {
912 const char* path = "mandrill.wbmp";
913 SkAutoTDelete<SkStream> stream(resource(path));
914 if (!stream) {
915 SkDebugf("Missing resource '%s'\n", path);
916 return;
917 }
918
919 // Modify the stream to contain a second byte with some bits set.
920 SkAutoTUnref<SkData> data(SkCopyStreamToData(stream));
921 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
922 writeableData[1] = static_cast<uint8_t>(~0x9F);
923
924 // SkImageDecoder supports this.
925 SkBitmap bitmap;
926 REPORTER_ASSERT(r, SkImageDecoder::DecodeMemory(data->data(), data->size(), &bitmap));
927
928 // So SkCodec should, too.
929 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
930 REPORTER_ASSERT(r, codec);
931 if (!codec) {
932 return;
933 }
scroggo7b5e5532016-02-04 06:14:24 -0800934 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800935}
scroggodb30be22015-12-08 18:54:13 -0800936
937// wbmp images have a header that can be arbitrarily large, depending on the
938// size of the image. We cap the size at 65535, meaning we only need to look at
939// 8 bytes to determine whether we can read the image. This is important
940// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
941// image is a wbmp.
942DEF_TEST(Codec_wbmp_max_size, r) {
943 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
944 0x83, 0xFF, 0x7F, // W: 65535
945 0x83, 0xFF, 0x7F }; // H: 65535
946 SkAutoTDelete<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
947 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
948
949 REPORTER_ASSERT(r, codec);
950 if (!codec) return;
951
952 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
953 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
954
955 // Now test an image which is too big. Any image with a larger header (i.e.
956 // has bigger width/height) is also too big.
957 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
958 0x84, 0x80, 0x00, // W: 65536
959 0x84, 0x80, 0x00 }; // H: 65536
960 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
961 codec.reset(SkCodec::NewFromStream(stream.detach()));
962
963 REPORTER_ASSERT(r, !codec);
964}