blob: 341433b379674b1db376765d80392bb650947dea [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 void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
26 SkAutoLockPixels autoLockPixels(bm);
27 SkASSERT(bm.getPixels());
28 SkMD5 md5;
29 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
30 for (int y = 0; y < bm.height(); ++y) {
halcanary1e903042016-04-25 10:29:36 -070031 md5.write(bm.getAddr(0, y), rowLen);
halcanarya096d7a2015-03-27 12:16:53 -070032 }
33 md5.finish(*digest);
34}
35
scroggo9b2cdbf42015-07-10 12:07:02 -070036/**
37 * Compute the digest for bm and compare it to a known good digest.
38 * @param r Reporter to assert that bm's digest matches goodDigest.
39 * @param goodDigest The known good digest to compare to.
40 * @param bm The bitmap to test.
41 */
42static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
43 const SkBitmap& bm) {
44 SkMD5::Digest digest;
45 md5(bm, &digest);
46 REPORTER_ASSERT(r, digest == goodDigest);
47}
48
scroggod1bc5742015-08-12 08:31:44 -070049/**
50 * Test decoding an SkCodec to a particular SkImageInfo.
51 *
halcanary96fcdcc2015-08-27 07:41:13 -070052 * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr,
scroggod1bc5742015-08-12 08:31:44 -070053 * the resulting decode should match.
54 */
scroggo7b5e5532016-02-04 06:14:24 -080055template<typename Codec>
56static void test_info(skiatest::Reporter* r, Codec* codec, const SkImageInfo& info,
scroggod1bc5742015-08-12 08:31:44 -070057 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
58 SkBitmap bm;
59 bm.allocPixels(info);
60 SkAutoLockPixels autoLockPixels(bm);
61
62 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
63 REPORTER_ASSERT(r, result == expectedResult);
64
65 if (goodDigest) {
66 compare_to_good_digest(r, *goodDigest, bm);
67 }
68}
69
scroggob636b452015-07-22 07:16:20 -070070SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
71 SkIRect rect;
72 do {
73 rect.fLeft = rand->nextRangeU(0, w);
74 rect.fTop = rand->nextRangeU(0, h);
75 rect.fRight = rand->nextRangeU(0, w);
76 rect.fBottom = rand->nextRangeU(0, h);
77 rect.sort();
78 } while (rect.isEmpty());
79 return rect;
80}
81
scroggo7b5e5532016-02-04 06:14:24 -080082template<typename Codec>
83static void test_codec(skiatest::Reporter* r, Codec* codec, SkBitmap& bm, const SkImageInfo& info,
scroggo27c17282015-10-27 08:14:46 -070084 const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest,
85 const SkMD5::Digest* goodDigest) {
msarette6dd0042015-10-09 11:07:34 -070086
halcanarya096d7a2015-03-27 12:16:53 -070087 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -070088 bm.allocPixels(info);
89 SkAutoLockPixels autoLockPixels(bm);
msarettcc7f3052015-10-05 14:20:27 -070090
91 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -070092 REPORTER_ASSERT(r, result == expectedResult);
halcanarya096d7a2015-03-27 12:16:53 -070093
msarettcc7f3052015-10-05 14:20:27 -070094 md5(bm, digest);
95 if (goodDigest) {
96 REPORTER_ASSERT(r, *digest == *goodDigest);
97 }
halcanarya096d7a2015-03-27 12:16:53 -070098
msarett8ff6ca62015-09-18 12:06:04 -070099 {
100 // Test decoding to 565
101 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggoba584892016-05-20 13:56:13 -0700102 if (info.alphaType() == kOpaque_SkAlphaType) {
103 // Decoding to 565 should succeed.
104 SkBitmap bm565;
105 bm565.allocPixels(info565);
106 SkAutoLockPixels alp(bm565);
107
108 // This will allow comparison even if the image is incomplete.
109 bm565.eraseColor(SK_ColorBLACK);
110
111 REPORTER_ASSERT(r, expectedResult == codec->getPixels(info565,
112 bm565.getPixels(), bm565.rowBytes()));
113
114 SkMD5::Digest digest565;
115 md5(bm565, &digest565);
116
117 // A dumb client's request for non-opaque should also succeed.
118 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
119 info565 = info565.makeAlphaType(alpha);
120 test_info(r, codec, info565, expectedResult, &digest565);
121 }
122 } else {
123 test_info(r, codec, info565, SkCodec::kInvalidConversion, nullptr);
124 }
125 }
126
127 if (codec->getInfo().colorType() == kGray_8_SkColorType) {
128 SkImageInfo grayInfo = codec->getInfo();
129 SkBitmap grayBm;
130 grayBm.allocPixels(grayInfo);
131 SkAutoLockPixels alp(grayBm);
132
133 grayBm.eraseColor(SK_ColorBLACK);
134
135 REPORTER_ASSERT(r, expectedResult == codec->getPixels(grayInfo,
136 grayBm.getPixels(), grayBm.rowBytes()));
137
138 SkMD5::Digest grayDigest;
139 md5(grayBm, &grayDigest);
140
141 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
142 grayInfo = grayInfo.makeAlphaType(alpha);
143 test_info(r, codec, grayInfo, expectedResult, &grayDigest);
144 }
msarett8ff6ca62015-09-18 12:06:04 -0700145 }
146
147 // Verify that re-decoding gives the same result. It is interesting to check this after
148 // a decode to 565, since choosing to decode to 565 may result in some of the decode
149 // options being modified. These options should return to their defaults on another
150 // decode to kN32, so the new digest should match the old digest.
msarette6dd0042015-10-09 11:07:34 -0700151 test_info(r, codec, info, expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700152
153 {
154 // Check alpha type conversions
155 if (info.alphaType() == kOpaque_SkAlphaType) {
156 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800157 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700158 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800159 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700160 } else {
161 // Decoding to opaque should fail
162 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700163 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700164 SkAlphaType otherAt = info.alphaType();
165 if (kPremul_SkAlphaType == otherAt) {
166 otherAt = kUnpremul_SkAlphaType;
167 } else {
168 otherAt = kPremul_SkAlphaType;
169 }
170 // The other non-opaque alpha type should always succeed, but not match.
msarette6dd0042015-10-09 11:07:34 -0700171 test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700172 }
173 }
msarettcc7f3052015-10-05 14:20:27 -0700174}
175
scroggobed1ed62016-02-11 10:24:55 -0800176static bool supports_partial_scanlines(const char path[]) {
scroggo2c3b2182015-10-09 08:40:59 -0700177 static const char* const exts[] = {
178 "jpg", "jpeg", "png", "webp"
179 "JPG", "JPEG", "PNG", "WEBP"
180 };
181
182 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
183 if (SkStrEndsWith(path, exts[i])) {
184 return true;
185 }
186 }
187 return false;
188}
189
msarettcc7f3052015-10-05 14:20:27 -0700190static void check(skiatest::Reporter* r,
191 const char path[],
192 SkISize size,
193 bool supportsScanlineDecoding,
194 bool supportsSubsetDecoding,
scroggod8d68552016-06-06 11:26:17 -0700195 bool supportsIncomplete = true) {
msarettcc7f3052015-10-05 14:20:27 -0700196
bungemanf93d7112016-09-16 06:24:20 -0700197 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarettcc7f3052015-10-05 14:20:27 -0700198 if (!stream) {
msarettcc7f3052015-10-05 14:20:27 -0700199 return;
200 }
msarette6dd0042015-10-09 11:07:34 -0700201
202 SkAutoTDelete<SkCodec> codec(nullptr);
203 bool isIncomplete = supportsIncomplete;
204 if (isIncomplete) {
205 size_t size = stream->getLength();
bungeman38d909e2016-08-02 14:40:46 -0700206 sk_sp<SkData> data((SkData::MakeFromStream(stream, 2 * size / 3)));
reed42943c82016-09-12 12:01:44 -0700207 codec.reset(SkCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700208 } else {
mtklein18300a32016-03-16 13:53:35 -0700209 codec.reset(SkCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700210 }
msarettcc7f3052015-10-05 14:20:27 -0700211 if (!codec) {
212 ERRORF(r, "Unable to decode '%s'", path);
213 return;
214 }
215
216 // Test full image decodes with SkCodec
217 SkMD5::Digest codecDigest;
scroggoef0fed32016-02-18 05:59:25 -0800218 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
msarettcc7f3052015-10-05 14:20:27 -0700219 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700220 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
scroggo7b5e5532016-02-04 06:14:24 -0800221 test_codec(r, codec.get(), bm, info, size, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700222
223 // Scanline decoding follows.
scroggo6fb23912016-06-02 14:16:43 -0700224 // Need to call startScanlineDecode() first.
scroggod8d68552016-06-06 11:26:17 -0700225 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
226 == 0);
227 REPORTER_ASSERT(r, codec->skipScanlines(1)
228 == 0);
229
scroggo46c57472015-09-30 08:57:13 -0700230 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700231 if (supportsScanlineDecoding) {
232 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700233
scroggo46c57472015-09-30 08:57:13 -0700234 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700235
scroggo58421542015-04-01 11:25:20 -0700236 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700237 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
238 if (!isIncomplete) {
239 REPORTER_ASSERT(r, 1 == lines);
240 }
scroggo58421542015-04-01 11:25:20 -0700241 }
242 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700243 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700244 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700245 }
scroggo46c57472015-09-30 08:57:13 -0700246
247 // Cannot continue to decode scanlines beyond the end
248 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700249 == 0);
scroggo46c57472015-09-30 08:57:13 -0700250
251 // Interrupting a scanline decode with a full decode starts from
252 // scratch
253 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700254 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
255 if (!isIncomplete) {
256 REPORTER_ASSERT(r, lines == 1);
257 }
scroggo46c57472015-09-30 08:57:13 -0700258 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700259 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700260 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700261 == 0);
scroggo46c57472015-09-30 08:57:13 -0700262 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700263 == 0);
msarett80803ff2015-10-16 10:54:12 -0700264
265 // Test partial scanline decodes
scroggobed1ed62016-02-11 10:24:55 -0800266 if (supports_partial_scanlines(path) && info.width() >= 3) {
msarett80803ff2015-10-16 10:54:12 -0700267 SkCodec::Options options;
268 int width = info.width();
269 int height = info.height();
270 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
271 options.fSubset = &subset;
272
273 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
274 nullptr, nullptr);
275 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
276
277 for (int y = 0; y < height; y++) {
278 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
279 if (!isIncomplete) {
280 REPORTER_ASSERT(r, 1 == lines);
281 }
282 }
283 }
scroggo58421542015-04-01 11:25:20 -0700284 } else {
scroggo46c57472015-09-30 08:57:13 -0700285 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700286 }
scroggob636b452015-07-22 07:16:20 -0700287
288 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
289 // random subsets.
290 // Do not attempt to decode subsets of an image of only once pixel, since there is no
291 // meaningful subset.
292 if (size.width() * size.height() == 1) {
293 return;
294 }
295
296 SkRandom rand;
297 SkIRect subset;
298 SkCodec::Options opts;
299 opts.fSubset = &subset;
300 for (int i = 0; i < 5; i++) {
301 subset = generate_random_subset(&rand, size.width(), size.height());
302 SkASSERT(!subset.isEmpty());
303 const bool supported = codec->getValidSubset(&subset);
304 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
305
306 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
307 SkBitmap bm;
308 bm.allocPixels(subsetInfo);
309 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700310 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700311
312 if (supportsSubsetDecoding) {
msarette6dd0042015-10-09 11:07:34 -0700313 REPORTER_ASSERT(r, result == expectedResult);
scroggob636b452015-07-22 07:16:20 -0700314 // Webp is the only codec that supports subsets, and it will have modified the subset
315 // to have even left/top.
316 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
317 } else {
318 // No subsets will work.
319 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
320 }
321 }
msarettcc7f3052015-10-05 14:20:27 -0700322
scroggobed1ed62016-02-11 10:24:55 -0800323 // SkAndroidCodec tests
scroggod8d68552016-06-06 11:26:17 -0700324 if (supportsScanlineDecoding || supportsSubsetDecoding) {
scroggo2c3b2182015-10-09 08:40:59 -0700325
bungemanf93d7112016-09-16 06:24:20 -0700326 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarettcc7f3052015-10-05 14:20:27 -0700327 if (!stream) {
msarettcc7f3052015-10-05 14:20:27 -0700328 return;
329 }
msarette6dd0042015-10-09 11:07:34 -0700330
scroggo7b5e5532016-02-04 06:14:24 -0800331 SkAutoTDelete<SkAndroidCodec> androidCodec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700332 if (isIncomplete) {
333 size_t size = stream->getLength();
bungeman38d909e2016-08-02 14:40:46 -0700334 sk_sp<SkData> data((SkData::MakeFromStream(stream, 2 * size / 3)));
reed42943c82016-09-12 12:01:44 -0700335 androidCodec.reset(SkAndroidCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700336 } else {
mtklein18300a32016-03-16 13:53:35 -0700337 androidCodec.reset(SkAndroidCodec::NewFromStream(stream.release()));
msarette6dd0042015-10-09 11:07:34 -0700338 }
scroggo7b5e5532016-02-04 06:14:24 -0800339 if (!androidCodec) {
msarettcc7f3052015-10-05 14:20:27 -0700340 ERRORF(r, "Unable to decode '%s'", path);
341 return;
342 }
343
344 SkBitmap bm;
scroggobed1ed62016-02-11 10:24:55 -0800345 SkMD5::Digest androidCodecDigest;
346 test_codec(r, androidCodec.get(), bm, info, size, expectedResult, &androidCodecDigest,
scroggo7b5e5532016-02-04 06:14:24 -0800347 &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700348 }
349
msarettedd2dcf2016-01-14 13:12:26 -0800350 if (!isIncomplete) {
scroggoef0fed32016-02-18 05:59:25 -0800351 // Test SkCodecImageGenerator
bungemanf93d7112016-09-16 06:24:20 -0700352 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
bungeman38d909e2016-08-02 14:40:46 -0700353 sk_sp<SkData> fullData(SkData::MakeFromStream(stream, stream->getLength()));
354 SkAutoTDelete<SkImageGenerator> gen(
355 SkCodecImageGenerator::NewFromEncodedCodec(fullData.get()));
msarettedd2dcf2016-01-14 13:12:26 -0800356 SkBitmap bm;
357 bm.allocPixels(info);
358 SkAutoLockPixels autoLockPixels(bm);
359 REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes()));
360 compare_to_good_digest(r, codecDigest, bm);
scroggoef0fed32016-02-18 05:59:25 -0800361
scroggod8d68552016-06-06 11:26:17 -0700362 // Test using SkFrontBufferedStream, as Android does
bungeman38d909e2016-08-02 14:40:46 -0700363 SkStream* bufferedStream = SkFrontBufferedStream::Create(
364 new SkMemoryStream(std::move(fullData)), SkCodec::MinBufferedBytesNeeded());
scroggod8d68552016-06-06 11:26:17 -0700365 REPORTER_ASSERT(r, bufferedStream);
366 codec.reset(SkCodec::NewFromStream(bufferedStream));
367 REPORTER_ASSERT(r, codec);
368 if (codec) {
369 test_info(r, codec.get(), info, SkCodec::kSuccess, &codecDigest);
scroggoef0fed32016-02-18 05:59:25 -0800370 }
msarettedd2dcf2016-01-14 13:12:26 -0800371 }
372
msarette6dd0042015-10-09 11:07:34 -0700373 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
374 if (isIncomplete) {
scroggo27c17282015-10-27 08:14:46 -0700375 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false);
msarettcc7f3052015-10-05 14:20:27 -0700376 }
halcanarya096d7a2015-03-27 12:16:53 -0700377}
378
379DEF_TEST(Codec, r) {
380 // WBMP
scroggod8d68552016-06-06 11:26:17 -0700381 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700382
scroggo6f5e6192015-06-18 12:53:43 -0700383 // WEBP
scroggod8d68552016-06-06 11:26:17 -0700384 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true);
385 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true);
386 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true);
scroggo6f5e6192015-06-18 12:53:43 -0700387
halcanarya096d7a2015-03-27 12:16:53 -0700388 // BMP
scroggod8d68552016-06-06 11:26:17 -0700389 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false);
390 check(r, "rle.bmp", SkISize::Make(320, 240), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700391
392 // ICO
msarette6dd0042015-10-09 11:07:34 -0700393 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700394 // These two tests examine interestingly different behavior:
395 // Decodes an embedded BMP image
msarettbe8216a2015-12-04 08:00:50 -0800396 check(r, "color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700397 // Decodes an embedded PNG image
scroggod8d68552016-06-06 11:26:17 -0700398 check(r, "google_chrome.ico", SkISize::Make(256, 256), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700399
msarett438b2ad2015-04-09 12:43:10 -0700400 // GIF
msarette6dd0042015-10-09 11:07:34 -0700401 // FIXME: We are not ready to test incomplete GIFs
scroggo27c17282015-10-27 08:14:46 -0700402 check(r, "box.gif", SkISize::Make(200, 55), true, false, false);
403 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700404 // randPixels.gif is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700405 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, false);
msarett438b2ad2015-04-09 12:43:10 -0700406
msarette16b04a2015-04-15 07:32:19 -0700407 // JPG
scroggod8d68552016-06-06 11:26:17 -0700408 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false);
409 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
msarette6dd0042015-10-09 11:07:34 -0700410 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700411 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggod8d68552016-06-06 11:26:17 -0700412 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
msarette6dd0042015-10-09 11:07:34 -0700413 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700414 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
msarette16b04a2015-04-15 07:32:19 -0700415
halcanarya096d7a2015-03-27 12:16:53 -0700416 // PNG
scroggod8d68552016-06-06 11:26:17 -0700417 check(r, "arrow.png", SkISize::Make(187, 312), true, false, false);
418 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false, false);
419 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false, false);
420 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false, false);
421 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false, false);
422 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false, false);
423 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false, false);
424 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false, false);
425 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false, false);
426 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false, false);
427 check(r, "plane.png", SkISize::Make(250, 126), true, false, false);
428 // FIXME: We are not ready to test incomplete interlaced pngs
429 check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false, false);
430 check(r, "randPixels.png", SkISize::Make(8, 8), true, false, false);
431 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false, false);
yujieqin916de9f2016-01-25 08:26:16 -0800432
433 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800434// Disable RAW tests for Win32.
435#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800436 check(r, "sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
ebrauer46d2aa82016-02-17 08:04:00 -0800437 check(r, "sample_1mp_rotated.dng", SkISize::Make(600, 338), false, false, false);
yujieqin9c7a8a42016-02-05 08:21:19 -0800438 check(r, "dng_with_preview.dng", SkISize::Make(600, 338), true, false, false);
msarett02cb4d42016-01-25 11:01:34 -0800439#endif
halcanarya096d7a2015-03-27 12:16:53 -0700440}
scroggo0a7e69c2015-04-03 07:22:22 -0700441
scroggod8d68552016-06-06 11:26:17 -0700442// Test interlaced PNG in stripes, similar to DM's kStripe_Mode
443DEF_TEST(Codec_stripes, r) {
444 const char * path = "plane_interlaced.png";
bungemanf93d7112016-09-16 06:24:20 -0700445 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
446 REPORTER_ASSERT(r, stream);
scroggod8d68552016-06-06 11:26:17 -0700447 if (!stream) {
bungemanf93d7112016-09-16 06:24:20 -0700448 return;
scroggod8d68552016-06-06 11:26:17 -0700449 }
450
451 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
452 REPORTER_ASSERT(r, codec);
453
454 if (!codec) {
455 return;
456 }
457
458 switch (codec->getScanlineOrder()) {
459 case SkCodec::kBottomUp_SkScanlineOrder:
460 case SkCodec::kOutOfOrder_SkScanlineOrder:
461 ERRORF(r, "This scanline order will not match the original.");
462 return;
463 default:
464 break;
465 }
466
467 // Baseline for what the image should look like, using N32.
468 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
469
470 SkBitmap bm;
471 bm.allocPixels(info);
472 SkAutoLockPixels autoLockPixels(bm);
473 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
474 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
475
476 SkMD5::Digest digest;
477 md5(bm, &digest);
478
479 // Now decode in stripes
480 const int height = info.height();
481 const int numStripes = 4;
482 int stripeHeight;
483 int remainingLines;
484 SkTDivMod(height, numStripes, &stripeHeight, &remainingLines);
485
486 bm.eraseColor(SK_ColorYELLOW);
487
488 result = codec->startScanlineDecode(info);
489 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
490
491 // Odd stripes
492 for (int i = 1; i < numStripes; i += 2) {
493 // Skip the even stripes
494 bool skipResult = codec->skipScanlines(stripeHeight);
495 REPORTER_ASSERT(r, skipResult);
496
497 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
498 bm.rowBytes());
499 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
500 }
501
502 // Even stripes
503 result = codec->startScanlineDecode(info);
504 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
505
506 for (int i = 0; i < numStripes; i += 2) {
507 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
508 bm.rowBytes());
509 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
510
511 // Skip the odd stripes
512 if (i + 1 < numStripes) {
513 bool skipResult = codec->skipScanlines(stripeHeight);
514 REPORTER_ASSERT(r, skipResult);
515 }
516 }
517
518 // Remainder at the end
519 if (remainingLines > 0) {
520 result = codec->startScanlineDecode(info);
521 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
522
523 bool skipResult = codec->skipScanlines(height - remainingLines);
524 REPORTER_ASSERT(r, skipResult);
525
526 int linesDecoded = codec->getScanlines(bm.getAddr(0, height - remainingLines),
527 remainingLines, bm.rowBytes());
528 REPORTER_ASSERT(r, linesDecoded == remainingLines);
529 }
530
531 compare_to_good_digest(r, digest, bm);
532}
533
scroggo0a7e69c2015-04-03 07:22:22 -0700534static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700535 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700536 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700537 REPORTER_ASSERT(r, !codec);
538
msarett3d9d7a72015-10-21 10:27:10 -0700539 SkAndroidCodec* androidCodec =
540 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
541 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700542}
543
544// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
545// even on failure. Test some bad streams.
546DEF_TEST(Codec_leaks, r) {
547 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
548 const char nonSupportedStream[] = "hello world";
549 // The other strings should look like the beginning of a file type, so we'll call some
550 // internal version of NewFromStream, which must also delete the stream on failure.
551 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
552 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
553 const char emptyWebp[] = "RIFF1234WEBPVP";
554 const char emptyBmp[] = { 'B', 'M' };
555 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
556 const char emptyGif[] = "GIFVER";
557
558 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
559 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
560 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
561 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
562 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
563 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
564 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
565}
msarette16b04a2015-04-15 07:32:19 -0700566
scroggo2c3b2182015-10-09 08:40:59 -0700567DEF_TEST(Codec_null, r) {
scroggobed1ed62016-02-11 10:24:55 -0800568 // Attempting to create an SkCodec or an SkAndroidCodec with null should not
scroggo2c3b2182015-10-09 08:40:59 -0700569 // crash.
570 SkCodec* codec = SkCodec::NewFromStream(nullptr);
571 REPORTER_ASSERT(r, !codec);
572
msarett3d9d7a72015-10-21 10:27:10 -0700573 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
574 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700575}
576
msarette16b04a2015-04-15 07:32:19 -0700577static void test_dimensions(skiatest::Reporter* r, const char path[]) {
578 // Create the codec from the resource file
bungemanf93d7112016-09-16 06:24:20 -0700579 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarette16b04a2015-04-15 07:32:19 -0700580 if (!stream) {
msarette16b04a2015-04-15 07:32:19 -0700581 return;
582 }
mtklein18300a32016-03-16 13:53:35 -0700583 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
msarette16b04a2015-04-15 07:32:19 -0700584 if (!codec) {
585 ERRORF(r, "Unable to create codec '%s'", path);
586 return;
587 }
588
589 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800590 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700591 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700592 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700593 SkImageInfo scaledInfo = codec->getInfo()
594 .makeWH(scaledDims.width(), scaledDims.height())
595 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700596
597 // Set up for the decode
598 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
599 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
600 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
601
msarett3d9d7a72015-10-21 10:27:10 -0700602 SkAndroidCodec::AndroidOptions options;
603 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700604 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700605 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700606 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700607 }
608}
609
610// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
611DEF_TEST(Codec_Dimensions, r) {
612 // JPG
613 test_dimensions(r, "CMYK.jpg");
614 test_dimensions(r, "color_wheel.jpg");
615 test_dimensions(r, "grayscale.jpg");
616 test_dimensions(r, "mandrill_512_q075.jpg");
617 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700618
619 // Decoding small images with very large scaling factors is a potential
620 // source of bugs and crashes. We disable these tests in Gold because
621 // tiny images are not very useful to look at.
622 // Here we make sure that we do not crash or access illegal memory when
623 // performing scaled decodes on small images.
624 test_dimensions(r, "1x1.png");
625 test_dimensions(r, "2x2.png");
626 test_dimensions(r, "3x3.png");
627 test_dimensions(r, "3x1.png");
628 test_dimensions(r, "1x1.png");
629 test_dimensions(r, "16x1.png");
630 test_dimensions(r, "1x16.png");
631 test_dimensions(r, "mandrill_16.png");
632
yujieqin916de9f2016-01-25 08:26:16 -0800633 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800634// Disable RAW tests for Win32.
635#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin916de9f2016-01-25 08:26:16 -0800636 test_dimensions(r, "sample_1mp.dng");
ebrauer46d2aa82016-02-17 08:04:00 -0800637 test_dimensions(r, "sample_1mp_rotated.dng");
yujieqin9c7a8a42016-02-05 08:21:19 -0800638 test_dimensions(r, "dng_with_preview.dng");
msarett8e49ca32016-01-25 13:10:58 -0800639#endif
msarette16b04a2015-04-15 07:32:19 -0700640}
641
msarettd0375bc2015-08-12 08:08:56 -0700642static void test_invalid(skiatest::Reporter* r, const char path[]) {
bungemanf93d7112016-09-16 06:24:20 -0700643 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarett4b17fa32015-04-23 08:53:39 -0700644 if (!stream) {
msarett4b17fa32015-04-23 08:53:39 -0700645 return;
646 }
mtklein18300a32016-03-16 13:53:35 -0700647 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
halcanary96fcdcc2015-08-27 07:41:13 -0700648 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700649}
msarette16b04a2015-04-15 07:32:19 -0700650
msarett4b17fa32015-04-23 08:53:39 -0700651DEF_TEST(Codec_Empty, r) {
652 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700653 test_invalid(r, "empty_images/zero-dims.gif");
654 test_invalid(r, "empty_images/zero-embedded.ico");
655 test_invalid(r, "empty_images/zero-width.bmp");
656 test_invalid(r, "empty_images/zero-height.bmp");
657 test_invalid(r, "empty_images/zero-width.jpg");
658 test_invalid(r, "empty_images/zero-height.jpg");
659 test_invalid(r, "empty_images/zero-width.png");
660 test_invalid(r, "empty_images/zero-height.png");
661 test_invalid(r, "empty_images/zero-width.wbmp");
662 test_invalid(r, "empty_images/zero-height.wbmp");
663 // This image is an ico with an embedded mask-bmp. This is illegal.
664 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700665}
msarett99f567e2015-08-05 12:58:26 -0700666
667static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
bungemanf93d7112016-09-16 06:24:20 -0700668 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarett99f567e2015-08-05 12:58:26 -0700669 if (!stream) {
msarett99f567e2015-08-05 12:58:26 -0700670 return;
671 }
mtklein18300a32016-03-16 13:53:35 -0700672 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.release()));
halcanary9d524f22016-03-29 09:03:52 -0700673
msarett99f567e2015-08-05 12:58:26 -0700674 // This should return kSuccess because kIndex8 is supported.
675 SkPMColor colorStorage[256];
676 int colorCount;
scroggod8d68552016-06-06 11:26:17 -0700677 SkCodec::Result result = decoder->startScanlineDecode(
678 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
679 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
680 // The rest of the test is uninteresting if kIndex8 is not supported
681 if (SkCodec::kSuccess != result) {
msarett99f567e2015-08-05 12:58:26 -0700682 return;
683 }
684
scroggod8d68552016-06-06 11:26:17 -0700685 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
686 // colorPtr and a valid colorCountPtr.
687 result = decoder->startScanlineDecode(
688 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
689 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
690 result = decoder->startScanlineDecode(
691 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
692 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
msarett99f567e2015-08-05 12:58:26 -0700693}
694
695DEF_TEST(Codec_Params, r) {
696 test_invalid_parameters(r, "index8.png");
697 test_invalid_parameters(r, "mandrill.wbmp");
698}
scroggocf98fa92015-11-23 08:14:40 -0800699
700static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
701 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
702 if (!sk_stream->write(data, len)) {
703 png_error(png_ptr, "sk_write_fn Error!");
704 }
705}
706
707#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
708DEF_TEST(Codec_pngChunkReader, r) {
709 // Create a dummy bitmap. Use unpremul RGBA for libpng.
710 SkBitmap bm;
711 const int w = 1;
712 const int h = 1;
713 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
714 kUnpremul_SkAlphaType);
715 bm.setInfo(bmInfo);
716 bm.allocPixels();
717 bm.eraseColor(SK_ColorBLUE);
718 SkMD5::Digest goodDigest;
719 md5(bm, &goodDigest);
720
721 // Write to a png file.
722 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
723 REPORTER_ASSERT(r, png);
724 if (!png) {
725 return;
726 }
727
728 png_infop info = png_create_info_struct(png);
729 REPORTER_ASSERT(r, info);
730 if (!info) {
731 png_destroy_write_struct(&png, nullptr);
732 return;
733 }
734
735 if (setjmp(png_jmpbuf(png))) {
736 ERRORF(r, "failed writing png");
737 png_destroy_write_struct(&png, &info);
738 return;
739 }
740
741 SkDynamicMemoryWStream wStream;
742 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
743
744 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
745 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
746 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
747
748 // Create some chunks that match the Android framework's use.
749 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800750 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
751 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
752 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800753 };
754
755 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
756 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
757#if PNG_LIBPNG_VER < 10600
758 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800759 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
760 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800761#endif
762
763 png_write_info(png, info);
764
765 for (int j = 0; j < h; j++) {
766 png_bytep row = (png_bytep)(bm.getAddr(0, j));
767 png_write_rows(png, &row, 1);
768 }
769 png_write_end(png, info);
770 png_destroy_write_struct(&png, &info);
771
772 class ChunkReader : public SkPngChunkReader {
773 public:
774 ChunkReader(skiatest::Reporter* r)
775 : fReporter(r)
776 {
777 this->reset();
778 }
779
780 bool readChunk(const char tag[], const void* data, size_t length) override {
781 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
782 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
783 // Tag matches. This should have been the first time we see it.
784 REPORTER_ASSERT(fReporter, !fSeen[i]);
785 fSeen[i] = true;
786
787 // Data and length should match
788 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
789 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
790 (const char*) gUnknowns[i].data));
791 return true;
792 }
793 }
794 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
795 return true;
796 }
797
798 bool allHaveBeenSeen() {
799 bool ret = true;
800 for (auto seen : fSeen) {
801 ret &= seen;
802 }
803 return ret;
804 }
805
806 void reset() {
807 sk_bzero(fSeen, sizeof(fSeen));
808 }
809
810 private:
811 skiatest::Reporter* fReporter; // Unowned
812 bool fSeen[3];
813 };
814
815 ChunkReader chunkReader(r);
816
817 // Now read the file with SkCodec.
reed42943c82016-09-12 12:01:44 -0700818 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(wStream.detachAsData(), &chunkReader));
scroggocf98fa92015-11-23 08:14:40 -0800819 REPORTER_ASSERT(r, codec);
820 if (!codec) {
821 return;
822 }
823
824 // Now compare to the original.
825 SkBitmap decodedBm;
826 decodedBm.setInfo(codec->getInfo());
827 decodedBm.allocPixels();
828 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
829 decodedBm.rowBytes());
830 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
831
832 if (decodedBm.colorType() != bm.colorType()) {
833 SkBitmap tmp;
834 bool success = decodedBm.copyTo(&tmp, bm.colorType());
835 REPORTER_ASSERT(r, success);
836 if (!success) {
837 return;
838 }
839
840 tmp.swap(decodedBm);
841 }
842
843 compare_to_good_digest(r, goodDigest, decodedBm);
844 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
845
846 // Decoding again will read the chunks again.
847 chunkReader.reset();
848 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
849 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
850 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
851 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
852}
853#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800854
scroggodb30be22015-12-08 18:54:13 -0800855// Stream that can only peek up to a limit
856class LimitedPeekingMemStream : public SkStream {
857public:
reed42943c82016-09-12 12:01:44 -0700858 LimitedPeekingMemStream(sk_sp<SkData> data, size_t limit)
859 : fStream(std::move(data))
scroggodb30be22015-12-08 18:54:13 -0800860 , fLimit(limit) {}
861
862 size_t peek(void* buf, size_t bytes) const override {
863 return fStream.peek(buf, SkTMin(bytes, fLimit));
864 }
865 size_t read(void* buf, size_t bytes) override {
866 return fStream.read(buf, bytes);
867 }
868 bool rewind() override {
869 return fStream.rewind();
870 }
871 bool isAtEnd() const override {
msarettff2a6c82016-09-07 11:23:28 -0700872 return fStream.isAtEnd();
scroggodb30be22015-12-08 18:54:13 -0800873 }
874private:
875 SkMemoryStream fStream;
876 const size_t fLimit;
877};
878
yujieqin9c7a8a42016-02-05 08:21:19 -0800879// Stream that is not an asset stream (!hasPosition() or !hasLength())
880class NotAssetMemStream : public SkStream {
881public:
bungeman38d909e2016-08-02 14:40:46 -0700882 NotAssetMemStream(sk_sp<SkData> data) : fStream(std::move(data)) {}
yujieqin9c7a8a42016-02-05 08:21:19 -0800883
884 bool hasPosition() const override {
885 return false;
886 }
887
888 bool hasLength() const override {
889 return false;
890 }
891
892 size_t peek(void* buf, size_t bytes) const override {
893 return fStream.peek(buf, bytes);
894 }
895 size_t read(void* buf, size_t bytes) override {
896 return fStream.read(buf, bytes);
897 }
898 bool rewind() override {
899 return fStream.rewind();
900 }
901 bool isAtEnd() const override {
902 return fStream.isAtEnd();
903 }
904private:
905 SkMemoryStream fStream;
906};
907
yujieqinf236ee42016-02-29 07:14:42 -0800908// Disable RAW tests for Win32.
909#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin9c7a8a42016-02-05 08:21:19 -0800910// Test that the RawCodec works also for not asset stream. This will test the code path using
911// SkRawBufferedStream instead of SkRawAssetStream.
yujieqin9c7a8a42016-02-05 08:21:19 -0800912DEF_TEST(Codec_raw_notseekable, r) {
913 const char* path = "dng_with_preview.dng";
914 SkString fullPath(GetResourcePath(path));
bungeman38d909e2016-08-02 14:40:46 -0700915 sk_sp<SkData> data(SkData::MakeFromFileName(fullPath.c_str()));
yujieqin9c7a8a42016-02-05 08:21:19 -0800916 if (!data) {
917 SkDebugf("Missing resource '%s'\n", path);
918 return;
919 }
920
bungeman38d909e2016-08-02 14:40:46 -0700921 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(new NotAssetMemStream(std::move(data))));
yujieqin9c7a8a42016-02-05 08:21:19 -0800922 REPORTER_ASSERT(r, codec);
923
924 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
925}
926#endif
927
scroggodb30be22015-12-08 18:54:13 -0800928// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
929// + rewind() and succeed.
930DEF_TEST(Codec_webp_peek, r) {
931 const char* path = "baby_tux.webp";
932 SkString fullPath(GetResourcePath(path));
reedfde05112016-03-11 13:02:28 -0800933 auto data = SkData::MakeFromFileName(fullPath.c_str());
scroggodb30be22015-12-08 18:54:13 -0800934 if (!data) {
935 SkDebugf("Missing resource '%s'\n", path);
936 return;
937 }
938
939 // The limit is less than webp needs to peek or read.
reedfde05112016-03-11 13:02:28 -0800940 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(
reed42943c82016-09-12 12:01:44 -0700941 new LimitedPeekingMemStream(data, 25)));
scroggodb30be22015-12-08 18:54:13 -0800942 REPORTER_ASSERT(r, codec);
943
scroggo7b5e5532016-02-04 06:14:24 -0800944 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800945
946 // Similarly, a stream which does not peek should still succeed.
reed42943c82016-09-12 12:01:44 -0700947 codec.reset(SkCodec::NewFromStream(new LimitedPeekingMemStream(data, 0)));
scroggodb30be22015-12-08 18:54:13 -0800948 REPORTER_ASSERT(r, codec);
949
scroggo7b5e5532016-02-04 06:14:24 -0800950 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800951}
952
msarett7f7ec202016-03-01 12:12:27 -0800953// SkCodec's wbmp decoder was initially unnecessarily restrictive.
954// It required the second byte to be zero. The wbmp specification allows
955// a couple of bits to be 1 (so long as they do not overlap with 0x9F).
956// Test that SkCodec now supports an image with these bits set.
scroggob9a1e342015-11-30 06:25:31 -0800957DEF_TEST(Codec_wbmp, r) {
958 const char* path = "mandrill.wbmp";
bungemanf93d7112016-09-16 06:24:20 -0700959 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
scroggob9a1e342015-11-30 06:25:31 -0800960 if (!stream) {
scroggob9a1e342015-11-30 06:25:31 -0800961 return;
962 }
963
964 // Modify the stream to contain a second byte with some bits set.
reedfde05112016-03-11 13:02:28 -0800965 auto data = SkCopyStreamToData(stream);
scroggob9a1e342015-11-30 06:25:31 -0800966 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
967 writeableData[1] = static_cast<uint8_t>(~0x9F);
968
msarett7f7ec202016-03-01 12:12:27 -0800969 // SkCodec should support this.
reed42943c82016-09-12 12:01:44 -0700970 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
scroggob9a1e342015-11-30 06:25:31 -0800971 REPORTER_ASSERT(r, codec);
972 if (!codec) {
973 return;
974 }
scroggo7b5e5532016-02-04 06:14:24 -0800975 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800976}
scroggodb30be22015-12-08 18:54:13 -0800977
978// wbmp images have a header that can be arbitrarily large, depending on the
979// size of the image. We cap the size at 65535, meaning we only need to look at
980// 8 bytes to determine whether we can read the image. This is important
981// because SkCodec only passes 14 bytes to SkWbmpCodec to determine whether the
982// image is a wbmp.
983DEF_TEST(Codec_wbmp_max_size, r) {
984 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
985 0x83, 0xFF, 0x7F, // W: 65535
986 0x83, 0xFF, 0x7F }; // H: 65535
987 SkAutoTDelete<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
mtklein18300a32016-03-16 13:53:35 -0700988 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -0800989
990 REPORTER_ASSERT(r, codec);
991 if (!codec) return;
992
993 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
994 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
995
996 // Now test an image which is too big. Any image with a larger header (i.e.
997 // has bigger width/height) is also too big.
998 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
999 0x84, 0x80, 0x00, // W: 65536
1000 0x84, 0x80, 0x00 }; // H: 65536
1001 stream.reset(new SkMemoryStream(tooBigWbmp, sizeof(tooBigWbmp), false));
mtklein18300a32016-03-16 13:53:35 -07001002 codec.reset(SkCodec::NewFromStream(stream.release()));
scroggodb30be22015-12-08 18:54:13 -08001003
1004 REPORTER_ASSERT(r, !codec);
1005}
msarett2812f032016-07-18 15:56:08 -07001006
1007DEF_TEST(Codec_jpeg_rewind, r) {
1008 const char* path = "mandrill_512_q075.jpg";
bungemanf93d7112016-09-16 06:24:20 -07001009 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarett2812f032016-07-18 15:56:08 -07001010 if (!stream) {
msarett2812f032016-07-18 15:56:08 -07001011 return;
1012 }
1013 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.release()));
1014 if (!codec) {
1015 ERRORF(r, "Unable to create codec '%s'.", path);
1016 return;
1017 }
1018
1019 const int width = codec->getInfo().width();
1020 const int height = codec->getInfo().height();
1021 size_t rowBytes = sizeof(SkPMColor) * width;
1022 SkAutoMalloc pixelStorage(height * rowBytes);
1023
1024 // Perform a sampled decode.
1025 SkAndroidCodec::AndroidOptions opts;
1026 opts.fSampleSize = 12;
1027 codec->getAndroidPixels(codec->getInfo().makeWH(width / 12, height / 12), pixelStorage.get(),
1028 rowBytes, &opts);
1029
1030 // Rewind the codec and perform a full image decode.
1031 SkCodec::Result result = codec->getPixels(codec->getInfo(), pixelStorage.get(), rowBytes);
1032 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1033}
msarett549ca322016-08-17 08:54:08 -07001034
msarett35bb74b2016-08-22 07:41:28 -07001035static void check_color_xform(skiatest::Reporter* r, const char* path) {
bungemanf93d7112016-09-16 06:24:20 -07001036 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(GetResourceAsStream(path)));
msarett35bb74b2016-08-22 07:41:28 -07001037
1038 SkAndroidCodec::AndroidOptions opts;
1039 opts.fSampleSize = 3;
1040 const int subsetWidth = codec->getInfo().width() / 2;
1041 const int subsetHeight = codec->getInfo().height() / 2;
1042 SkIRect subset = SkIRect::MakeWH(subsetWidth, subsetHeight);
1043 opts.fSubset = &subset;
1044
1045 const int dstWidth = subsetWidth / opts.fSampleSize;
1046 const int dstHeight = subsetHeight / opts.fSampleSize;
1047 sk_sp<SkData> data = SkData::MakeFromFileName(
1048 GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str());
1049 sk_sp<SkColorSpace> colorSpace = SkColorSpace::NewICC(data->data(), data->size());
1050 SkImageInfo dstInfo = codec->getInfo().makeWH(dstWidth, dstHeight)
1051 .makeColorType(kN32_SkColorType)
1052 .makeColorSpace(colorSpace);
1053
1054 size_t rowBytes = dstInfo.minRowBytes();
1055 SkAutoMalloc pixelStorage(dstInfo.getSafeSize(rowBytes));
1056 SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get(), rowBytes, &opts);
1057 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1058}
1059
1060DEF_TEST(Codec_ColorXform, r) {
1061 check_color_xform(r, "mandrill_512_q075.jpg");
1062 check_color_xform(r, "mandrill_512.png");
1063}
1064
msarettf17b71f2016-09-12 14:30:03 -07001065static bool color_type_match(SkColorType origColorType, SkColorType codecColorType) {
1066 switch (origColorType) {
1067 case kRGBA_8888_SkColorType:
1068 case kBGRA_8888_SkColorType:
1069 return kRGBA_8888_SkColorType == codecColorType ||
1070 kBGRA_8888_SkColorType == codecColorType;
1071 default:
1072 return origColorType == codecColorType;
1073 }
1074}
1075
1076static bool alpha_type_match(SkAlphaType origAlphaType, SkAlphaType codecAlphaType) {
1077 switch (origAlphaType) {
1078 case kUnpremul_SkAlphaType:
1079 case kPremul_SkAlphaType:
1080 return kUnpremul_SkAlphaType == codecAlphaType ||
1081 kPremul_SkAlphaType == codecAlphaType;
1082 default:
1083 return origAlphaType == codecAlphaType;
1084 }
1085}
1086
1087static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const SkImageInfo& info) {
1088 SkBitmap bm1;
1089 SkPMColor colors[256];
1090 SkAutoTUnref<SkColorTable> colorTable1(new SkColorTable(colors, 256));
1091 bm1.allocPixels(info, nullptr, colorTable1.get());
1092 int numColors;
1093 SkCodec::Result result = origCodec->getPixels(info, bm1.getPixels(), bm1.rowBytes(), nullptr,
1094 const_cast<SkPMColor*>(colorTable1->readColors()),
1095 &numColors);
1096 // This will fail to update colorTable1->count() but is fine for the purpose of this test.
1097 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarett9b09cd82016-08-29 14:47:49 -07001098
1099 // Encode the image to png.
1100 sk_sp<SkData> data =
1101 sk_sp<SkData>(SkImageEncoder::EncodeData(bm1, SkImageEncoder::kPNG_Type, 100));
1102
reed42943c82016-09-12 12:01:44 -07001103 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
msarettf17b71f2016-09-12 14:30:03 -07001104 REPORTER_ASSERT(r, color_type_match(info.colorType(), codec->getInfo().colorType()));
1105 REPORTER_ASSERT(r, alpha_type_match(info.alphaType(), codec->getInfo().alphaType()));
msarett9b09cd82016-08-29 14:47:49 -07001106
1107 SkBitmap bm2;
msarettf17b71f2016-09-12 14:30:03 -07001108 SkAutoTUnref<SkColorTable> colorTable2(new SkColorTable(colors, 256));
1109 bm2.allocPixels(info, nullptr, colorTable2.get());
1110 result = codec->getPixels(info, bm2.getPixels(), bm2.rowBytes(), nullptr,
1111 const_cast<SkPMColor*>(colorTable2->readColors()), &numColors);
msarett9b09cd82016-08-29 14:47:49 -07001112 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1113
1114 SkMD5::Digest d1, d2;
1115 md5(bm1, &d1);
1116 md5(bm2, &d2);
1117 REPORTER_ASSERT(r, d1 == d2);
1118}
1119
1120DEF_TEST(Codec_PngRoundTrip, r) {
msarett549ca322016-08-17 08:54:08 -07001121 const char* path = "mandrill_512_q075.jpg";
bungemanf93d7112016-09-16 06:24:20 -07001122 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarett549ca322016-08-17 08:54:08 -07001123 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett549ca322016-08-17 08:54:08 -07001124
msarettf17b71f2016-09-12 14:30:03 -07001125 SkColorType colorTypesOpaque[] = {
1126 kRGB_565_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1127 };
1128 for (SkColorType colorType : colorTypesOpaque) {
1129 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType);
1130 check_round_trip(r, codec.get(), newInfo);
1131 }
1132
msarett9b09cd82016-08-29 14:47:49 -07001133 path = "grayscale.jpg";
bungemanf93d7112016-09-16 06:24:20 -07001134 stream.reset(GetResourceAsStream(path));
msarett9b09cd82016-08-29 14:47:49 -07001135 codec.reset(SkCodec::NewFromStream(stream.release()));
msarettf17b71f2016-09-12 14:30:03 -07001136 check_round_trip(r, codec.get(), codec->getInfo());
1137
1138 path = "yellow_rose.png";
bungemanf93d7112016-09-16 06:24:20 -07001139 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001140 codec.reset(SkCodec::NewFromStream(stream.release()));
1141
1142 SkColorType colorTypesWithAlpha[] = {
1143 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1144 };
1145 SkAlphaType alphaTypes[] = {
1146 kUnpremul_SkAlphaType, kPremul_SkAlphaType
1147 };
1148 for (SkColorType colorType : colorTypesWithAlpha) {
1149 for (SkAlphaType alphaType : alphaTypes) {
1150 // Set color space to nullptr because color correct premultiplies do not round trip.
1151 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType)
1152 .makeAlphaType(alphaType)
1153 .makeColorSpace(nullptr);
1154 check_round_trip(r, codec.get(), newInfo);
1155 }
1156 }
1157
1158 path = "index8.png";
bungemanf93d7112016-09-16 06:24:20 -07001159 stream.reset(GetResourceAsStream(path));
msarettf17b71f2016-09-12 14:30:03 -07001160 codec.reset(SkCodec::NewFromStream(stream.release()));
1161
1162 for (SkAlphaType alphaType : alphaTypes) {
1163 SkImageInfo newInfo = codec->getInfo().makeAlphaType(alphaType)
1164 .makeColorSpace(nullptr);
1165 check_round_trip(r, codec.get(), newInfo);
1166 }
msarett549ca322016-08-17 08:54:08 -07001167}
msarett2ecc35f2016-09-08 11:55:16 -07001168
1169static void test_conversion_possible(skiatest::Reporter* r, const char* path,
1170 bool testScanlineDecoder) {
bungemanf93d7112016-09-16 06:24:20 -07001171 SkAutoTDelete<SkStream> stream(GetResourceAsStream(path));
msarett2ecc35f2016-09-08 11:55:16 -07001172 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
1173 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType);
1174
1175 SkBitmap bm;
1176 bm.allocPixels(infoF16);
1177 SkCodec::Result result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1178 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
1179 if (testScanlineDecoder) {
1180 result = codec->startScanlineDecode(infoF16);
1181 REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
1182 }
1183
1184 infoF16 = infoF16.makeColorSpace(infoF16.colorSpace()->makeLinearGamma());
1185 result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1186 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1187 if (testScanlineDecoder) {
1188 result = codec->startScanlineDecode(infoF16);
1189 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1190 }
1191}
1192
1193DEF_TEST(Codec_F16ConversionPossible, r) {
1194 test_conversion_possible(r, "color_wheel.webp", false);
1195 test_conversion_possible(r, "mandrill_512_q075.jpg", true);
1196 test_conversion_possible(r, "yellow_rose.png", true);
1197}