blob: 8023ff219d5873632db3708054a850fe4e8d3025 [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"
msarett549ca322016-08-17 08:54:08 -070014#include "SkImageEncoder.h"
scroggoef0fed32016-02-18 05:59:25 -080015#include "SkFrontBufferedStream.h"
halcanarya096d7a2015-03-27 12:16:53 -070016#include "SkMD5.h"
scroggob636b452015-07-22 07:16:20 -070017#include "SkRandom.h"
scroggocf98fa92015-11-23 08:14:40 -080018#include "SkStream.h"
scroggob9a1e342015-11-30 06:25:31 -080019#include "SkStreamPriv.h"
scroggocf98fa92015-11-23 08:14:40 -080020#include "SkPngChunkReader.h"
halcanarya096d7a2015-03-27 12:16:53 -070021#include "Test.h"
22
scroggocf98fa92015-11-23 08:14:40 -080023#include "png.h"
24
halcanarya096d7a2015-03-27 12:16:53 -070025static SkStreamAsset* resource(const char path[]) {
26 SkString fullPath = GetResourcePath(path);
27 return SkStream::NewFromFile(fullPath.c_str());
28}
29
30static void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
31 SkAutoLockPixels autoLockPixels(bm);
32 SkASSERT(bm.getPixels());
33 SkMD5 md5;
34 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
35 for (int y = 0; y < bm.height(); ++y) {
halcanary1e903042016-04-25 10:29:36 -070036 md5.write(bm.getAddr(0, y), rowLen);
halcanarya096d7a2015-03-27 12:16:53 -070037 }
38 md5.finish(*digest);
39}
40
scroggo9b2cdbf42015-07-10 12:07:02 -070041/**
42 * Compute the digest for bm and compare it to a known good digest.
43 * @param r Reporter to assert that bm's digest matches goodDigest.
44 * @param goodDigest The known good digest to compare to.
45 * @param bm The bitmap to test.
46 */
47static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
48 const SkBitmap& bm) {
49 SkMD5::Digest digest;
50 md5(bm, &digest);
51 REPORTER_ASSERT(r, digest == goodDigest);
52}
53
scroggod1bc5742015-08-12 08:31:44 -070054/**
55 * Test decoding an SkCodec to a particular SkImageInfo.
56 *
halcanary96fcdcc2015-08-27 07:41:13 -070057 * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr,
scroggod1bc5742015-08-12 08:31:44 -070058 * the resulting decode should match.
59 */
scroggo7b5e5532016-02-04 06:14:24 -080060template<typename Codec>
61static void test_info(skiatest::Reporter* r, Codec* codec, const SkImageInfo& info,
scroggod1bc5742015-08-12 08:31:44 -070062 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
63 SkBitmap bm;
64 bm.allocPixels(info);
65 SkAutoLockPixels autoLockPixels(bm);
66
67 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
68 REPORTER_ASSERT(r, result == expectedResult);
69
70 if (goodDigest) {
71 compare_to_good_digest(r, *goodDigest, bm);
72 }
73}
74
scroggob636b452015-07-22 07:16:20 -070075SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
76 SkIRect rect;
77 do {
78 rect.fLeft = rand->nextRangeU(0, w);
79 rect.fTop = rand->nextRangeU(0, h);
80 rect.fRight = rand->nextRangeU(0, w);
81 rect.fBottom = rand->nextRangeU(0, h);
82 rect.sort();
83 } while (rect.isEmpty());
84 return rect;
85}
86
scroggo7b5e5532016-02-04 06:14:24 -080087template<typename Codec>
88static void test_codec(skiatest::Reporter* r, Codec* codec, SkBitmap& bm, const SkImageInfo& info,
scroggo27c17282015-10-27 08:14:46 -070089 const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest,
90 const SkMD5::Digest* goodDigest) {
msarette6dd0042015-10-09 11:07:34 -070091
halcanarya096d7a2015-03-27 12:16:53 -070092 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -070093 bm.allocPixels(info);
94 SkAutoLockPixels autoLockPixels(bm);
msarettcc7f3052015-10-05 14:20:27 -070095
96 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -070097 REPORTER_ASSERT(r, result == expectedResult);
halcanarya096d7a2015-03-27 12:16:53 -070098
msarettcc7f3052015-10-05 14:20:27 -070099 md5(bm, digest);
100 if (goodDigest) {
101 REPORTER_ASSERT(r, *digest == *goodDigest);
102 }
halcanarya096d7a2015-03-27 12:16:53 -0700103
msarett8ff6ca62015-09-18 12:06:04 -0700104 {
105 // Test decoding to 565
106 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggoba584892016-05-20 13:56:13 -0700107 if (info.alphaType() == kOpaque_SkAlphaType) {
108 // Decoding to 565 should succeed.
109 SkBitmap bm565;
110 bm565.allocPixels(info565);
111 SkAutoLockPixels alp(bm565);
112
113 // This will allow comparison even if the image is incomplete.
114 bm565.eraseColor(SK_ColorBLACK);
115
116 REPORTER_ASSERT(r, expectedResult == codec->getPixels(info565,
117 bm565.getPixels(), bm565.rowBytes()));
118
119 SkMD5::Digest digest565;
120 md5(bm565, &digest565);
121
122 // A dumb client's request for non-opaque should also succeed.
123 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
124 info565 = info565.makeAlphaType(alpha);
125 test_info(r, codec, info565, expectedResult, &digest565);
126 }
127 } else {
128 test_info(r, codec, info565, SkCodec::kInvalidConversion, nullptr);
129 }
130 }
131
132 if (codec->getInfo().colorType() == kGray_8_SkColorType) {
133 SkImageInfo grayInfo = codec->getInfo();
134 SkBitmap grayBm;
135 grayBm.allocPixels(grayInfo);
136 SkAutoLockPixels alp(grayBm);
137
138 grayBm.eraseColor(SK_ColorBLACK);
139
140 REPORTER_ASSERT(r, expectedResult == codec->getPixels(grayInfo,
141 grayBm.getPixels(), grayBm.rowBytes()));
142
143 SkMD5::Digest grayDigest;
144 md5(grayBm, &grayDigest);
145
146 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
147 grayInfo = grayInfo.makeAlphaType(alpha);
148 test_info(r, codec, grayInfo, expectedResult, &grayDigest);
149 }
msarett8ff6ca62015-09-18 12:06:04 -0700150 }
151
152 // Verify that re-decoding gives the same result. It is interesting to check this after
153 // a decode to 565, since choosing to decode to 565 may result in some of the decode
154 // options being modified. These options should return to their defaults on another
155 // decode to kN32, so the new digest should match the old digest.
msarette6dd0042015-10-09 11:07:34 -0700156 test_info(r, codec, info, expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700157
158 {
159 // Check alpha type conversions
160 if (info.alphaType() == kOpaque_SkAlphaType) {
161 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800162 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700163 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800164 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700165 } else {
166 // Decoding to opaque should fail
167 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700168 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700169 SkAlphaType otherAt = info.alphaType();
170 if (kPremul_SkAlphaType == otherAt) {
171 otherAt = kUnpremul_SkAlphaType;
172 } else {
173 otherAt = kPremul_SkAlphaType;
174 }
175 // The other non-opaque alpha type should always succeed, but not match.
msarette6dd0042015-10-09 11:07:34 -0700176 test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700177 }
178 }
msarettcc7f3052015-10-05 14:20:27 -0700179}
180
scroggobed1ed62016-02-11 10:24:55 -0800181static bool supports_partial_scanlines(const char path[]) {
scroggo2c3b2182015-10-09 08:40:59 -0700182 static const char* const exts[] = {
183 "jpg", "jpeg", "png", "webp"
184 "JPG", "JPEG", "PNG", "WEBP"
185 };
186
187 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
188 if (SkStrEndsWith(path, exts[i])) {
189 return true;
190 }
191 }
192 return false;
193}
194
msarettcc7f3052015-10-05 14:20:27 -0700195static void check(skiatest::Reporter* r,
196 const char path[],
197 SkISize size,
198 bool supportsScanlineDecoding,
199 bool supportsSubsetDecoding,
scroggod8d68552016-06-06 11:26:17 -0700200 bool supportsIncomplete = true) {
msarettcc7f3052015-10-05 14:20:27 -0700201
202 SkAutoTDelete<SkStream> stream(resource(path));
203 if (!stream) {
204 SkDebugf("Missing resource '%s'\n", path);
205 return;
206 }
msarette6dd0042015-10-09 11:07:34 -0700207
208 SkAutoTDelete<SkCodec> codec(nullptr);
209 bool isIncomplete = supportsIncomplete;
210 if (isIncomplete) {
211 size_t size = stream->getLength();
bungeman38d909e2016-08-02 14:40:46 -0700212 sk_sp<SkData> data((SkData::MakeFromStream(stream, 2 * size / 3)));
213 codec.reset(SkCodec::NewFromData(data.get()));
msarette6dd0042015-10-09 11:07:34 -0700214 } else {
mtklein18300a32016-03-16 13:53:35 -0700215 codec.reset(SkCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700216 }
msarettcc7f3052015-10-05 14:20:27 -0700217 if (!codec) {
218 ERRORF(r, "Unable to decode '%s'", path);
219 return;
220 }
221
222 // Test full image decodes with SkCodec
223 SkMD5::Digest codecDigest;
scroggoef0fed32016-02-18 05:59:25 -0800224 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
msarettcc7f3052015-10-05 14:20:27 -0700225 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700226 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
scroggo7b5e5532016-02-04 06:14:24 -0800227 test_codec(r, codec.get(), bm, info, size, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700228
229 // Scanline decoding follows.
scroggo6fb23912016-06-02 14:16:43 -0700230 // Need to call startScanlineDecode() first.
scroggod8d68552016-06-06 11:26:17 -0700231 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
232 == 0);
233 REPORTER_ASSERT(r, codec->skipScanlines(1)
234 == 0);
235
scroggo46c57472015-09-30 08:57:13 -0700236 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700237 if (supportsScanlineDecoding) {
238 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700239
scroggo46c57472015-09-30 08:57:13 -0700240 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700241
scroggo58421542015-04-01 11:25:20 -0700242 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700243 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
244 if (!isIncomplete) {
245 REPORTER_ASSERT(r, 1 == lines);
246 }
scroggo58421542015-04-01 11:25:20 -0700247 }
248 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700249 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700250 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700251 }
scroggo46c57472015-09-30 08:57:13 -0700252
253 // Cannot continue to decode scanlines beyond the end
254 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700255 == 0);
scroggo46c57472015-09-30 08:57:13 -0700256
257 // Interrupting a scanline decode with a full decode starts from
258 // scratch
259 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700260 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
261 if (!isIncomplete) {
262 REPORTER_ASSERT(r, lines == 1);
263 }
scroggo46c57472015-09-30 08:57:13 -0700264 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700265 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700266 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700267 == 0);
scroggo46c57472015-09-30 08:57:13 -0700268 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700269 == 0);
msarett80803ff2015-10-16 10:54:12 -0700270
271 // Test partial scanline decodes
scroggobed1ed62016-02-11 10:24:55 -0800272 if (supports_partial_scanlines(path) && info.width() >= 3) {
msarett80803ff2015-10-16 10:54:12 -0700273 SkCodec::Options options;
274 int width = info.width();
275 int height = info.height();
276 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
277 options.fSubset = &subset;
278
279 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
280 nullptr, nullptr);
281 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
282
283 for (int y = 0; y < height; y++) {
284 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
285 if (!isIncomplete) {
286 REPORTER_ASSERT(r, 1 == lines);
287 }
288 }
289 }
scroggo58421542015-04-01 11:25:20 -0700290 } else {
scroggo46c57472015-09-30 08:57:13 -0700291 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700292 }
scroggob636b452015-07-22 07:16:20 -0700293
294 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
295 // random subsets.
296 // Do not attempt to decode subsets of an image of only once pixel, since there is no
297 // meaningful subset.
298 if (size.width() * size.height() == 1) {
299 return;
300 }
301
302 SkRandom rand;
303 SkIRect subset;
304 SkCodec::Options opts;
305 opts.fSubset = &subset;
306 for (int i = 0; i < 5; i++) {
307 subset = generate_random_subset(&rand, size.width(), size.height());
308 SkASSERT(!subset.isEmpty());
309 const bool supported = codec->getValidSubset(&subset);
310 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
311
312 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
313 SkBitmap bm;
314 bm.allocPixels(subsetInfo);
315 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700316 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700317
318 if (supportsSubsetDecoding) {
msarette6dd0042015-10-09 11:07:34 -0700319 REPORTER_ASSERT(r, result == expectedResult);
scroggob636b452015-07-22 07:16:20 -0700320 // Webp is the only codec that supports subsets, and it will have modified the subset
321 // to have even left/top.
322 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
323 } else {
324 // No subsets will work.
325 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
326 }
327 }
msarettcc7f3052015-10-05 14:20:27 -0700328
scroggobed1ed62016-02-11 10:24:55 -0800329 // SkAndroidCodec tests
scroggod8d68552016-06-06 11:26:17 -0700330 if (supportsScanlineDecoding || supportsSubsetDecoding) {
scroggo2c3b2182015-10-09 08:40:59 -0700331
msarettcc7f3052015-10-05 14:20:27 -0700332 SkAutoTDelete<SkStream> stream(resource(path));
333 if (!stream) {
334 SkDebugf("Missing resource '%s'\n", path);
335 return;
336 }
msarette6dd0042015-10-09 11:07:34 -0700337
scroggo7b5e5532016-02-04 06:14:24 -0800338 SkAutoTDelete<SkAndroidCodec> androidCodec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700339 if (isIncomplete) {
340 size_t size = stream->getLength();
bungeman38d909e2016-08-02 14:40:46 -0700341 sk_sp<SkData> data((SkData::MakeFromStream(stream, 2 * size / 3)));
342 androidCodec.reset(SkAndroidCodec::NewFromData(data.get()));
msarette6dd0042015-10-09 11:07:34 -0700343 } else {
mtklein18300a32016-03-16 13:53:35 -0700344 androidCodec.reset(SkAndroidCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700345 }
scroggo7b5e5532016-02-04 06:14:24 -0800346 if (!androidCodec) {
msarettcc7f3052015-10-05 14:20:27 -0700347 ERRORF(r, "Unable to decode '%s'", path);
348 return;
349 }
350
351 SkBitmap bm;
scroggobed1ed62016-02-11 10:24:55 -0800352 SkMD5::Digest androidCodecDigest;
353 test_codec(r, androidCodec.get(), bm, info, size, expectedResult, &androidCodecDigest,
scroggo7b5e5532016-02-04 06:14:24 -0800354 &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700355 }
356
msarettedd2dcf2016-01-14 13:12:26 -0800357 if (!isIncomplete) {
scroggoef0fed32016-02-18 05:59:25 -0800358 // Test SkCodecImageGenerator
msarettedd2dcf2016-01-14 13:12:26 -0800359 SkAutoTDelete<SkStream> stream(resource(path));
bungeman38d909e2016-08-02 14:40:46 -0700360 sk_sp<SkData> fullData(SkData::MakeFromStream(stream, stream->getLength()));
361 SkAutoTDelete<SkImageGenerator> gen(
362 SkCodecImageGenerator::NewFromEncodedCodec(fullData.get()));
msarettedd2dcf2016-01-14 13:12:26 -0800363 SkBitmap bm;
364 bm.allocPixels(info);
365 SkAutoLockPixels autoLockPixels(bm);
366 REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes()));
367 compare_to_good_digest(r, codecDigest, bm);
scroggoef0fed32016-02-18 05:59:25 -0800368
scroggod8d68552016-06-06 11:26:17 -0700369 // Test using SkFrontBufferedStream, as Android does
bungeman38d909e2016-08-02 14:40:46 -0700370 SkStream* bufferedStream = SkFrontBufferedStream::Create(
371 new SkMemoryStream(std::move(fullData)), SkCodec::MinBufferedBytesNeeded());
scroggod8d68552016-06-06 11:26:17 -0700372 REPORTER_ASSERT(r, bufferedStream);
373 codec.reset(SkCodec::NewFromStream(bufferedStream));
374 REPORTER_ASSERT(r, codec);
375 if (codec) {
376 test_info(r, codec.get(), info, SkCodec::kSuccess, &codecDigest);
scroggoef0fed32016-02-18 05:59:25 -0800377 }
msarettedd2dcf2016-01-14 13:12:26 -0800378 }
379
msarette6dd0042015-10-09 11:07:34 -0700380 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
381 if (isIncomplete) {
scroggo27c17282015-10-27 08:14:46 -0700382 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false);
msarettcc7f3052015-10-05 14:20:27 -0700383 }
halcanarya096d7a2015-03-27 12:16:53 -0700384}
385
386DEF_TEST(Codec, r) {
387 // WBMP
scroggod8d68552016-06-06 11:26:17 -0700388 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700389
scroggo6f5e6192015-06-18 12:53:43 -0700390 // WEBP
scroggod8d68552016-06-06 11:26:17 -0700391 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true);
392 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true);
393 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true);
scroggo6f5e6192015-06-18 12:53:43 -0700394
halcanarya096d7a2015-03-27 12:16:53 -0700395 // BMP
scroggod8d68552016-06-06 11:26:17 -0700396 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false);
397 check(r, "rle.bmp", SkISize::Make(320, 240), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700398
399 // ICO
msarette6dd0042015-10-09 11:07:34 -0700400 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700401 // These two tests examine interestingly different behavior:
402 // Decodes an embedded BMP image
msarettbe8216a2015-12-04 08:00:50 -0800403 check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700404 // Decodes an embedded PNG image
scroggod8d68552016-06-06 11:26:17 -0700405 check(r, "google_chrome.ico", SkISize::Make(256, 256), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700406
msarett438b2ad2015-04-09 12:43:10 -0700407 // GIF
msarette6dd0042015-10-09 11:07:34 -0700408 // FIXME: We are not ready to test incomplete GIFs
scroggo27c17282015-10-27 08:14:46 -0700409 check(r, "box.gif", SkISize::Make(200, 55), true, false, false);
410 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700411 // randPixels.gif is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700412 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, false);
msarett438b2ad2015-04-09 12:43:10 -0700413
msarette16b04a2015-04-15 07:32:19 -0700414 // JPG
scroggod8d68552016-06-06 11:26:17 -0700415 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false);
416 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
msarette6dd0042015-10-09 11:07:34 -0700417 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700418 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggod8d68552016-06-06 11:26:17 -0700419 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
msarette6dd0042015-10-09 11:07:34 -0700420 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700421 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
msarette16b04a2015-04-15 07:32:19 -0700422
halcanarya096d7a2015-03-27 12:16:53 -0700423 // PNG
scroggod8d68552016-06-06 11:26:17 -0700424 check(r, "arrow.png", SkISize::Make(187, 312), true, false, false);
425 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false, false);
426 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false, false);
427 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false, false);
428 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false, false);
429 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false, false);
430 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false, false);
431 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false, false);
432 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false, false);
433 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false, false);
434 check(r, "plane.png", SkISize::Make(250, 126), true, false, false);
435 // FIXME: We are not ready to test incomplete interlaced pngs
436 check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false, false);
437 check(r, "randPixels.png", SkISize::Make(8, 8), true, false, false);
438 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false, false);
yujieqin916de9f2016-01-25 08:26:16 -0800439
440 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800441// Disable RAW tests for Win32.
442#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800443 check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
ebrauer46d2aa82016-02-17 08:04:00 -0800444 check(r, "sample_1mp_rotated.dng", SkISize::Make(600, 338), false, false, false);
yujieqin9c7a8a42016-02-05 08:21:19 -0800445 check(r, "dng_with_preview.dng", SkISize::Make(600, 338), true, false, false);
msarett02cb4d42016-01-25 11:01:34 -0800446#endif
halcanarya096d7a2015-03-27 12:16:53 -0700447}
scroggo0a7e69c2015-04-03 07:22:22 -0700448
scroggod8d68552016-06-06 11:26:17 -0700449// Test interlaced PNG in stripes, similar to DM's kStripe_Mode
450DEF_TEST(Codec_stripes, r) {
451 const char * path = "plane_interlaced.png";
452 SkAutoTDelete<SkStream> stream(resource(path));
453 if (!stream) {
454 SkDebugf("Missing resource '%s'\n", path);
455 }
456
457 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
458 REPORTER_ASSERT(r, codec);
459
460 if (!codec) {
461 return;
462 }
463
464 switch (codec->getScanlineOrder()) {
465 case SkCodec::kBottomUp_SkScanlineOrder:
466 case SkCodec::kOutOfOrder_SkScanlineOrder:
467 ERRORF(r, "This scanline order will not match the original.");
468 return;
469 default:
470 break;
471 }
472
473 // Baseline for what the image should look like, using N32.
474 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
475
476 SkBitmap bm;
477 bm.allocPixels(info);
478 SkAutoLockPixels autoLockPixels(bm);
479 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
480 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
481
482 SkMD5::Digest digest;
483 md5(bm, &digest);
484
485 // Now decode in stripes
486 const int height = info.height();
487 const int numStripes = 4;
488 int stripeHeight;
489 int remainingLines;
490 SkTDivMod(height, numStripes, &stripeHeight, &remainingLines);
491
492 bm.eraseColor(SK_ColorYELLOW);
493
494 result = codec->startScanlineDecode(info);
495 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
496
497 // Odd stripes
498 for (int i = 1; i < numStripes; i += 2) {
499 // Skip the even stripes
500 bool skipResult = codec->skipScanlines(stripeHeight);
501 REPORTER_ASSERT(r, skipResult);
502
503 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
504 bm.rowBytes());
505 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
506 }
507
508 // Even stripes
509 result = codec->startScanlineDecode(info);
510 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
511
512 for (int i = 0; i < numStripes; i += 2) {
513 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
514 bm.rowBytes());
515 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
516
517 // Skip the odd stripes
518 if (i + 1 < numStripes) {
519 bool skipResult = codec->skipScanlines(stripeHeight);
520 REPORTER_ASSERT(r, skipResult);
521 }
522 }
523
524 // Remainder at the end
525 if (remainingLines > 0) {
526 result = codec->startScanlineDecode(info);
527 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
528
529 bool skipResult = codec->skipScanlines(height - remainingLines);
530 REPORTER_ASSERT(r, skipResult);
531
532 int linesDecoded = codec->getScanlines(bm.getAddr(0, height - remainingLines),
533 remainingLines, bm.rowBytes());
534 REPORTER_ASSERT(r, linesDecoded == remainingLines);
535 }
536
537 compare_to_good_digest(r, digest, bm);
538}
539
scroggo0a7e69c2015-04-03 07:22:22 -0700540static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700541 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700542 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700543 REPORTER_ASSERT(r, !codec);
544
msarett3d9d7a72015-10-21 10:27:10 -0700545 SkAndroidCodec* androidCodec =
546 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
547 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700548}
549
550// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
551// even on failure. Test some bad streams.
552DEF_TEST(Codec_leaks, r) {
553 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
554 const char nonSupportedStream[] = "hello world";
555 // The other strings should look like the beginning of a file type, so we'll call some
556 // internal version of NewFromStream, which must also delete the stream on failure.
557 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
558 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
559 const char emptyWebp[] = "RIFF1234WEBPVP";
560 const char emptyBmp[] = { 'B', 'M' };
561 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
562 const char emptyGif[] = "GIFVER";
563
564 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
565 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
566 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
567 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
568 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
569 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
570 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
571}
msarette16b04a2015-04-15 07:32:19 -0700572
scroggo2c3b2182015-10-09 08:40:59 -0700573DEF_TEST(Codec_null, r) {
scroggobed1ed62016-02-11 10:24:55 -0800574 // Attempting to create an SkCodec or an SkAndroidCodec with null should not
scroggo2c3b2182015-10-09 08:40:59 -0700575 // crash.
576 SkCodec* codec = SkCodec::NewFromStream(nullptr);
577 REPORTER_ASSERT(r, !codec);
578
msarett3d9d7a72015-10-21 10:27:10 -0700579 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
580 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700581}
582
msarette16b04a2015-04-15 07:32:19 -0700583static void test_dimensions(skiatest::Reporter* r, const char path[]) {
584 // Create the codec from the resource file
585 SkAutoTDelete<SkStream> stream(resource(path));
586 if (!stream) {
587 SkDebugf("Missing resource '%s'\n", path);
588 return;
589 }
mtklein18300a32016-03-16 13:53:35 -0700590 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarette16b04a2015-04-15 07:32:19 -0700591 if (!codec) {
592 ERRORF(r, "Unable to create codec '%s'", path);
593 return;
594 }
595
596 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800597 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700598 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700599 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700600 SkImageInfo scaledInfo = codec->getInfo()
601 .makeWH(scaledDims.width(), scaledDims.height())
602 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700603
604 // Set up for the decode
605 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
606 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
607 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
608
msarett3d9d7a72015-10-21 10:27:10 -0700609 SkAndroidCodec::AndroidOptions options;
610 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700611 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700612 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700613 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700614 }
615}
616
617// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
618DEF_TEST(Codec_Dimensions, r) {
619 // JPG
620 test_dimensions(r, "CMYK.jpg");
621 test_dimensions(r, "color_wheel.jpg");
622 test_dimensions(r, "grayscale.jpg");
623 test_dimensions(r, "mandrill_512_q075.jpg");
624 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700625
626 // Decoding small images with very large scaling factors is a potential
627 // source of bugs and crashes. We disable these tests in Gold because
628 // tiny images are not very useful to look at.
629 // Here we make sure that we do not crash or access illegal memory when
630 // performing scaled decodes on small images.
631 test_dimensions(r, "1x1.png");
632 test_dimensions(r, "2x2.png");
633 test_dimensions(r, "3x3.png");
634 test_dimensions(r, "3x1.png");
635 test_dimensions(r, "1x1.png");
636 test_dimensions(r, "16x1.png");
637 test_dimensions(r, "1x16.png");
638 test_dimensions(r, "mandrill_16.png");
639
yujieqin916de9f2016-01-25 08:26:16 -0800640 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800641// Disable RAW tests for Win32.
642#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800643 test_dimensions(r, "sample_1mp.dng");
ebrauer46d2aa82016-02-17 08:04:00 -0800644 test_dimensions(r, "sample_1mp_rotated.dng");
yujieqin9c7a8a42016-02-05 08:21:19 -0800645 test_dimensions(r, "dng_with_preview.dng");
msarett8e49ca32016-01-25 13:10:58 -0800646#endif
msarette16b04a2015-04-15 07:32:19 -0700647}
648
msarettd0375bc2015-08-12 08:08:56 -0700649static void test_invalid(skiatest::Reporter* r, const char path[]) {
msarett4b17fa32015-04-23 08:53:39 -0700650 SkAutoTDelete<SkStream> stream(resource(path));
651 if (!stream) {
652 SkDebugf("Missing resource '%s'\n", path);
653 return;
654 }
mtklein18300a32016-03-16 13:53:35 -0700655 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
halcanary96fcdcc2015-08-27 07:41:13 -0700656 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700657}
msarette16b04a2015-04-15 07:32:19 -0700658
msarett4b17fa32015-04-23 08:53:39 -0700659DEF_TEST(Codec_Empty, r) {
660 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700661 test_invalid(r, "empty_images/zero-dims.gif");
662 test_invalid(r, "empty_images/zero-embedded.ico");
663 test_invalid(r, "empty_images/zero-width.bmp");
664 test_invalid(r, "empty_images/zero-height.bmp");
665 test_invalid(r, "empty_images/zero-width.jpg");
666 test_invalid(r, "empty_images/zero-height.jpg");
667 test_invalid(r, "empty_images/zero-width.png");
668 test_invalid(r, "empty_images/zero-height.png");
669 test_invalid(r, "empty_images/zero-width.wbmp");
670 test_invalid(r, "empty_images/zero-height.wbmp");
671 // This image is an ico with an embedded mask-bmp. This is illegal.
672 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700673}
msarett99f567e2015-08-05 12:58:26 -0700674
675static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
676 SkAutoTDelete<SkStream> stream(resource(path));
677 if (!stream) {
678 SkDebugf("Missing resource '%s'\n", path);
679 return;
680 }
mtklein18300a32016-03-16 13:53:35 -0700681 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.release()));
halcanary9d524f22016-03-29 09:03:52 -0700682
msarett99f567e2015-08-05 12:58:26 -0700683 // This should return kSuccess because kIndex8 is supported.
684 SkPMColor colorStorage[256];
685 int colorCount;
scroggod8d68552016-06-06 11:26:17 -0700686 SkCodec::Result result = decoder->startScanlineDecode(
687 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
688 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
689 // The rest of the test is uninteresting if kIndex8 is not supported
690 if (SkCodec::kSuccess != result) {
msarett99f567e2015-08-05 12:58:26 -0700691 return;
692 }
693
scroggod8d68552016-06-06 11:26:17 -0700694 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
695 // colorPtr and a valid colorCountPtr.
696 result = decoder->startScanlineDecode(
697 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
698 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
699 result = decoder->startScanlineDecode(
700 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
701 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
msarett99f567e2015-08-05 12:58:26 -0700702}
703
704DEF_TEST(Codec_Params, r) {
705 test_invalid_parameters(r, "index8.png");
706 test_invalid_parameters(r, "mandrill.wbmp");
707}
scroggocf98fa92015-11-23 08:14:40 -0800708
709static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
710 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
711 if (!sk_stream->write(data, len)) {
712 png_error(png_ptr, "sk_write_fn Error!");
713 }
714}
715
716#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
717DEF_TEST(Codec_pngChunkReader, r) {
718 // Create a dummy bitmap. Use unpremul RGBA for libpng.
719 SkBitmap bm;
720 const int w = 1;
721 const int h = 1;
722 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
723 kUnpremul_SkAlphaType);
724 bm.setInfo(bmInfo);
725 bm.allocPixels();
726 bm.eraseColor(SK_ColorBLUE);
727 SkMD5::Digest goodDigest;
728 md5(bm, &goodDigest);
729
730 // Write to a png file.
731 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
732 REPORTER_ASSERT(r, png);
733 if (!png) {
734 return;
735 }
736
737 png_infop info = png_create_info_struct(png);
738 REPORTER_ASSERT(r, info);
739 if (!info) {
740 png_destroy_write_struct(&png, nullptr);
741 return;
742 }
743
744 if (setjmp(png_jmpbuf(png))) {
745 ERRORF(r, "failed writing png");
746 png_destroy_write_struct(&png, &info);
747 return;
748 }
749
750 SkDynamicMemoryWStream wStream;
751 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
752
753 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
754 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
755 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
756
757 // Create some chunks that match the Android framework's use.
758 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800759 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
760 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
761 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800762 };
763
764 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
765 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
766#if PNG_LIBPNG_VER < 10600
767 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800768 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
769 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800770#endif
771
772 png_write_info(png, info);
773
774 for (int j = 0; j < h; j++) {
775 png_bytep row = (png_bytep)(bm.getAddr(0, j));
776 png_write_rows(png, &row, 1);
777 }
778 png_write_end(png, info);
779 png_destroy_write_struct(&png, &info);
780
781 class ChunkReader : public SkPngChunkReader {
782 public:
783 ChunkReader(skiatest::Reporter* r)
784 : fReporter(r)
785 {
786 this->reset();
787 }
788
789 bool readChunk(const char tag[], const void* data, size_t length) override {
790 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
791 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
792 // Tag matches. This should have been the first time we see it.
793 REPORTER_ASSERT(fReporter, !fSeen[i]);
794 fSeen[i] = true;
795
796 // Data and length should match
797 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
798 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
799 (const char*) gUnknowns[i].data));
800 return true;
801 }
802 }
803 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
804 return true;
805 }
806
807 bool allHaveBeenSeen() {
808 bool ret = true;
809 for (auto seen : fSeen) {
810 ret &= seen;
811 }
812 return ret;
813 }
814
815 void reset() {
816 sk_bzero(fSeen, sizeof(fSeen));
817 }
818
819 private:
820 skiatest::Reporter* fReporter; // Unowned
821 bool fSeen[3];
822 };
823
824 ChunkReader chunkReader(r);
825
826 // Now read the file with SkCodec.
bungemanffae30d2016-08-03 13:32:32 -0700827 sk_sp<SkData> data(wStream.copyToData());
828 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data.get(), &chunkReader));
scroggocf98fa92015-11-23 08:14:40 -0800829 REPORTER_ASSERT(r, codec);
830 if (!codec) {
831 return;
832 }
833
834 // Now compare to the original.
835 SkBitmap decodedBm;
836 decodedBm.setInfo(codec->getInfo());
837 decodedBm.allocPixels();
838 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
839 decodedBm.rowBytes());
840 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
841
842 if (decodedBm.colorType() != bm.colorType()) {
843 SkBitmap tmp;
844 bool success = decodedBm.copyTo(&tmp, bm.colorType());
845 REPORTER_ASSERT(r, success);
846 if (!success) {
847 return;
848 }
849
850 tmp.swap(decodedBm);
851 }
852
853 compare_to_good_digest(r, goodDigest, decodedBm);
854 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
855
856 // Decoding again will read the chunks again.
857 chunkReader.reset();
858 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
859 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
860 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
861 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
862}
863#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800864
scroggodb30be22015-12-08 18:54:13 -0800865// Stream that can only peek up to a limit
866class LimitedPeekingMemStream : public SkStream {
867public:
868 LimitedPeekingMemStream(SkData* data, size_t limit)
869 : fStream(data)
870 , fLimit(limit) {}
871
872 size_t peek(void* buf, size_t bytes) const override {
873 return fStream.peek(buf, SkTMin(bytes, fLimit));
874 }
875 size_t read(void* buf, size_t bytes) override {
876 return fStream.read(buf, bytes);
877 }
878 bool rewind() override {
879 return fStream.rewind();
880 }
881 bool isAtEnd() const override {
882 return false;
883 }
884private:
885 SkMemoryStream fStream;
886 const size_t fLimit;
887};
888
yujieqin9c7a8a42016-02-05 08:21:19 -0800889// Stream that is not an asset stream (!hasPosition() or !hasLength())
890class NotAssetMemStream : public SkStream {
891public:
bungeman38d909e2016-08-02 14:40:46 -0700892 NotAssetMemStream(sk_sp<SkData> data) : fStream(std::move(data)) {}
yujieqin9c7a8a42016-02-05 08:21:19 -0800893
894 bool hasPosition() const override {
895 return false;
896 }
897
898 bool hasLength() const override {
899 return false;
900 }
901
902 size_t peek(void* buf, size_t bytes) const override {
903 return fStream.peek(buf, bytes);
904 }
905 size_t read(void* buf, size_t bytes) override {
906 return fStream.read(buf, bytes);
907 }
908 bool rewind() override {
909 return fStream.rewind();
910 }
911 bool isAtEnd() const override {
912 return fStream.isAtEnd();
913 }
914private:
915 SkMemoryStream fStream;
916};
917
yujieqinf236ee42016-02-29 07:14:42 -0800918// Disable RAW tests for Win32.
919#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin9c7a8a42016-02-05 08:21:19 -0800920// Test that the RawCodec works also for not asset stream. This will test the code path using
921// SkRawBufferedStream instead of SkRawAssetStream.
yujieqin9c7a8a42016-02-05 08:21:19 -0800922DEF_TEST(Codec_raw_notseekable, r) {
923 const char* path = "dng_with_preview.dng";
924 SkString fullPath(GetResourcePath(path));
bungeman38d909e2016-08-02 14:40:46 -0700925 sk_sp<SkData> data(SkData::MakeFromFileName(fullPath.c_str()));
yujieqin9c7a8a42016-02-05 08:21:19 -0800926 if (!data) {
927 SkDebugf("Missing resource '%s'\n", path);
928 return;
929 }
930
bungeman38d909e2016-08-02 14:40:46 -0700931 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(std::move(data))));
yujieqin9c7a8a42016-02-05 08:21:19 -0800932 REPORTER_ASSERT(r, codec);
933
934 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
935}
936#endif
937
scroggodb30be22015-12-08 18:54:13 -0800938// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
939// + rewind() and succeed.
940DEF_TEST(Codec_webp_peek, r) {
941 const char* path = "baby_tux.webp";
942 SkString fullPath(GetResourcePath(path));
reedfde05112016-03-11 13:02:28 -0800943 auto data = SkData::MakeFromFileName(fullPath.c_str());
scroggodb30be22015-12-08 18:54:13 -0800944 if (!data) {
945 SkDebugf("Missing resource '%s'\n", path);
946 return;
947 }
948
949 // The limit is less than webp needs to peek or read.
reedfde05112016-03-11 13:02:28 -0800950 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(
951 new LimitedPeekingMemStream(data.get(), 25)));
scroggodb30be22015-12-08 18:54:13 -0800952 REPORTER_ASSERT(r, codec);
953
scroggo7b5e5532016-02-04 06:14:24 -0800954 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800955
956 // Similarly, a stream which does not peek should still succeed.
reedfde05112016-03-11 13:02:28 -0800957 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data.get(), 0)));
scroggodb30be22015-12-08 18:54:13 -0800958 REPORTER_ASSERT(r, codec);
959
scroggo7b5e5532016-02-04 06:14:24 -0800960 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800961}
962
msarett7f7ec202016-03-01 12:12:27 -0800963// SkCodec's wbmp decoder was initially unnecessarily restrictive.
964// It required the second byte to be zero. The wbmp specification allows
965// a couple of bits to be 1 (so long as they do not overlap with 0x9F).
966// Test that SkCodec now supports an image with these bits set.
scroggob9a1e342015-11-30 06:25:31 -0800967DEF_TEST(Codec_wbmp, r) {
968 const char* path = "mandrill.wbmp";
969 SkAutoTDelete<SkStream> stream(resource(path));
970 if (!stream) {
971 SkDebugf("Missing resource '%s'\n", path);
972 return;
973 }
974
975 // Modify the stream to contain a second byte with some bits set.
reedfde05112016-03-11 13:02:28 -0800976 auto data = SkCopyStreamToData(stream);
scroggob9a1e342015-11-30 06:25:31 -0800977 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
978 writeableData[1] = static_cast<uint8_t>(~0x9F);
979
msarett7f7ec202016-03-01 12:12:27 -0800980 // SkCodec should support this.
reedfde05112016-03-11 13:02:28 -0800981 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data.get()));
scroggob9a1e342015-11-30 06:25:31 -0800982 REPORTER_ASSERT(r, codec);
983 if (!codec) {
984 return;
985 }
scroggo7b5e5532016-02-04 06:14:24 -0800986 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800987}
scroggodb30be22015-12-08 18:54:13 -0800988
989// wbmp images have a header that can be arbitrarily large, depending on the
990// size of the image. We cap the size at 65535, meaning we only need to look at
991// 8 bytes to determine whether we can read the image. This is important
992// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
993// image is a wbmp.
994DEF_TEST(Codec_wbmp_max_size, r) {
995 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
996 0x83, 0xFF, 0x7F, // W: 65535
997 0x83, 0xFF, 0x7F }; // H: 65535
998 SkAutoTDelete<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
mtklein18300a32016-03-16 13:53:35 -0700999 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -08001000
1001 REPORTER_ASSERT(r, codec);
1002 if (!codec) return;
1003
1004 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
1005 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
1006
1007 // Now test an image which is too big. Any image with a larger header (i.e.
1008 // has bigger width/height) is also too big.
1009 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
1010 0x84, 0x80, 0x00, // W: 65536
1011 0x84, 0x80, 0x00 }; // H: 65536
1012 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
mtklein18300a32016-03-16 13:53:35 -07001013 codec.reset(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -08001014
1015 REPORTER_ASSERT(r, !codec);
1016}
msarett2812f032016-07-18 15:56:08 -07001017
1018DEF_TEST(Codec_jpeg_rewind, r) {
1019 const char* path = "mandrill_512_q075.jpg";
1020 SkAutoTDelete<SkStream> stream(resource(path));
1021 if (!stream) {
1022 SkDebugf("Missing resource '%s'\n", path);
1023 return;
1024 }
1025 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
1026 if (!codec) {
1027 ERRORF(r, "Unable to create codec '%s'.", path);
1028 return;
1029 }
1030
1031 const int width = codec->getInfo().width();
1032 const int height = codec->getInfo().height();
1033 size_t rowBytes = sizeof(SkPMColor) * width;
1034 SkAutoMalloc pixelStorage(height * rowBytes);
1035
1036 // Perform a sampled decode.
1037 SkAndroidCodec::AndroidOptions opts;
1038 opts.fSampleSize = 12;
1039 codec->getAndroidPixels(codec->getInfo().makeWH(width / 12, height / 12), pixelStorage.get(),
1040 rowBytes, &opts);
1041
1042 // Rewind the codec and perform a full image decode.
1043 SkCodec::Result result = codec->getPixels(codec->getInfo(), pixelStorage.get(), rowBytes);
1044 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1045}
msarett549ca322016-08-17 08:54:08 -07001046
msarett35bb74b2016-08-22 07:41:28 -07001047static void check_color_xform(skiatest::Reporter* r, const char* path) {
1048 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(resource(path)));
1049
1050 SkAndroidCodec::AndroidOptions opts;
1051 opts.fSampleSize = 3;
1052 const int subsetWidth = codec->getInfo().width() / 2;
1053 const int subsetHeight = codec->getInfo().height() / 2;
1054 SkIRect subset = SkIRect::MakeWH(subsetWidth, subsetHeight);
1055 opts.fSubset = &subset;
1056
1057 const int dstWidth = subsetWidth / opts.fSampleSize;
1058 const int dstHeight = subsetHeight / opts.fSampleSize;
1059 sk_sp<SkData> data = SkData::MakeFromFileName(
1060 GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str());
1061 sk_sp<SkColorSpace> colorSpace = SkColorSpace::NewICC(data->data(), data->size());
1062 SkImageInfo dstInfo = codec->getInfo().makeWH(dstWidth, dstHeight)
1063 .makeColorType(kN32_SkColorType)
1064 .makeColorSpace(colorSpace);
1065
1066 size_t rowBytes = dstInfo.minRowBytes();
1067 SkAutoMalloc pixelStorage(dstInfo.getSafeSize(rowBytes));
1068 SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get(), rowBytes, &opts);
1069 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1070}
1071
1072DEF_TEST(Codec_ColorXform, r) {
1073 check_color_xform(r, "mandrill_512_q075.jpg");
1074 check_color_xform(r, "mandrill_512.png");
1075}
1076
msarett549ca322016-08-17 08:54:08 -07001077DEF_TEST(Codec_Png565, r) {
1078 // Create an arbitrary 565 bitmap.
1079 const char* path = "mandrill_512_q075.jpg";
1080 SkAutoTDelete<SkStream> stream(resource(path));
1081 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
1082 SkImageInfo info565 = codec->getInfo().makeColorType(kRGB_565_SkColorType);
1083 SkBitmap bm1;
1084 bm1.allocPixels(info565);
1085 SkCodec::Result result = codec->getPixels(info565, bm1.getPixels(), bm1.rowBytes());
1086 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1087
1088 // Encode the image to png.
1089 sk_sp<SkData> data =
1090 sk_sp<SkData>(SkImageEncoder::EncodeData(bm1, SkImageEncoder::kPNG_Type, 100));
1091
1092 // Prepare to decode. The codec should recognize that the PNG is 565.
1093 codec.reset(SkCodec::NewFromData(data.get()));
1094 REPORTER_ASSERT(r, kRGB_565_SkColorType == codec->getInfo().colorType());
1095 REPORTER_ASSERT(r, kOpaque_SkAlphaType == codec->getInfo().alphaType());
1096
1097 SkBitmap bm2;
1098 bm2.allocPixels(codec->getInfo());
1099 result = codec->getPixels(codec->getInfo(), bm2.getPixels(), bm2.rowBytes());
1100 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1101
1102 SkMD5::Digest d1, d2;
1103 md5(bm1, &d1);
1104 md5(bm2, &d2);
1105 REPORTER_ASSERT(r, d1 == d2);
1106}