blob: b53cbe1a4d7dd6c468af08f678f4b27087e91be6 [file] [log] [blame]
halcanarya096d7a2015-03-27 12:16:53 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Resources.h"
msarett3d9d7a72015-10-21 10:27:10 -07009#include "SkAndroidCodec.h"
halcanarya096d7a2015-03-27 12:16:53 -070010#include "SkBitmap.h"
11#include "SkCodec.h"
msarette6dd0042015-10-09 11:07:34 -070012#include "SkData.h"
halcanarya096d7a2015-03-27 12:16:53 -070013#include "SkMD5.h"
scroggob636b452015-07-22 07:16:20 -070014#include "SkRandom.h"
scroggocf98fa92015-11-23 08:14:40 -080015#include "SkStream.h"
16#include "SkPngChunkReader.h"
halcanarya096d7a2015-03-27 12:16:53 -070017#include "Test.h"
18
scroggocf98fa92015-11-23 08:14:40 -080019#include "png.h"
20
halcanarya096d7a2015-03-27 12:16:53 -070021static SkStreamAsset* resource(const char path[]) {
22 SkString fullPath = GetResourcePath(path);
23 return SkStream::NewFromFile(fullPath.c_str());
24}
25
26static void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
27 SkAutoLockPixels autoLockPixels(bm);
28 SkASSERT(bm.getPixels());
29 SkMD5 md5;
30 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
31 for (int y = 0; y < bm.height(); ++y) {
32 md5.update(static_cast<uint8_t*>(bm.getAddr(0, y)), rowLen);
33 }
34 md5.finish(*digest);
35}
36
scroggo9b2cdbf42015-07-10 12:07:02 -070037/**
38 * Compute the digest for bm and compare it to a known good digest.
39 * @param r Reporter to assert that bm's digest matches goodDigest.
40 * @param goodDigest The known good digest to compare to.
41 * @param bm The bitmap to test.
42 */
43static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
44 const SkBitmap& bm) {
45 SkMD5::Digest digest;
46 md5(bm, &digest);
47 REPORTER_ASSERT(r, digest == goodDigest);
48}
49
scroggod1bc5742015-08-12 08:31:44 -070050/**
51 * Test decoding an SkCodec to a particular SkImageInfo.
52 *
halcanary96fcdcc2015-08-27 07:41:13 -070053 * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr,
scroggod1bc5742015-08-12 08:31:44 -070054 * the resulting decode should match.
55 */
56static void test_info(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
57 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
msarett3d9d7a72015-10-21 10:27:10 -070070static void test_android_info(skiatest::Reporter* r, SkAndroidCodec* codec, const SkImageInfo& info,
71 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
72 SkBitmap bm;
73 bm.allocPixels(info);
74 SkAutoLockPixels autoLockPixels(bm);
75
76 SkCodec::Result result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes());
77 REPORTER_ASSERT(r, result == expectedResult);
78
79 if (goodDigest) {
80 compare_to_good_digest(r, *goodDigest, bm);
81 }
82}
83
scroggob636b452015-07-22 07:16:20 -070084SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
85 SkIRect rect;
86 do {
87 rect.fLeft = rand->nextRangeU(0, w);
88 rect.fTop = rand->nextRangeU(0, h);
89 rect.fRight = rand->nextRangeU(0, w);
90 rect.fBottom = rand->nextRangeU(0, h);
91 rect.sort();
92 } while (rect.isEmpty());
93 return rect;
94}
95
msarettcc7f3052015-10-05 14:20:27 -070096static void test_codec(skiatest::Reporter* r, SkCodec* codec, SkBitmap& bm, const SkImageInfo& info,
scroggo27c17282015-10-27 08:14:46 -070097 const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest,
98 const SkMD5::Digest* goodDigest) {
msarette6dd0042015-10-09 11:07:34 -070099
halcanarya096d7a2015-03-27 12:16:53 -0700100 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -0700101 bm.allocPixels(info);
102 SkAutoLockPixels autoLockPixels(bm);
msarettcc7f3052015-10-05 14:20:27 -0700103
104 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700105 REPORTER_ASSERT(r, result == expectedResult);
halcanarya096d7a2015-03-27 12:16:53 -0700106
msarettcc7f3052015-10-05 14:20:27 -0700107 md5(bm, digest);
108 if (goodDigest) {
109 REPORTER_ASSERT(r, *digest == *goodDigest);
110 }
halcanarya096d7a2015-03-27 12:16:53 -0700111
msarett8ff6ca62015-09-18 12:06:04 -0700112 {
113 // Test decoding to 565
114 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggo27c17282015-10-27 08:14:46 -0700115 SkCodec::Result expected565 = info.alphaType() == kOpaque_SkAlphaType ?
msarette6dd0042015-10-09 11:07:34 -0700116 expectedResult : SkCodec::kInvalidConversion;
117 test_info(r, codec, info565, expected565, nullptr);
msarett8ff6ca62015-09-18 12:06:04 -0700118 }
119
120 // Verify that re-decoding gives the same result. It is interesting to check this after
121 // a decode to 565, since choosing to decode to 565 may result in some of the decode
122 // options being modified. These options should return to their defaults on another
123 // decode to kN32, so the new digest should match the old digest.
msarette6dd0042015-10-09 11:07:34 -0700124 test_info(r, codec, info, expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700125
126 {
127 // Check alpha type conversions
128 if (info.alphaType() == kOpaque_SkAlphaType) {
129 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700130 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700131 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700132 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700133 } else {
134 // Decoding to opaque should fail
135 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700136 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700137 SkAlphaType otherAt = info.alphaType();
138 if (kPremul_SkAlphaType == otherAt) {
139 otherAt = kUnpremul_SkAlphaType;
140 } else {
141 otherAt = kPremul_SkAlphaType;
142 }
143 // The other non-opaque alpha type should always succeed, but not match.
msarette6dd0042015-10-09 11:07:34 -0700144 test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700145 }
146 }
msarettcc7f3052015-10-05 14:20:27 -0700147}
148
msarett3d9d7a72015-10-21 10:27:10 -0700149static void test_android_codec(skiatest::Reporter* r, SkAndroidCodec* codec, SkBitmap& bm,
scroggo27c17282015-10-27 08:14:46 -0700150 const SkImageInfo& info, const SkISize& size, SkCodec::Result expectedResult,
151 SkMD5::Digest* digest, const SkMD5::Digest* goodDigest) {
msarett3d9d7a72015-10-21 10:27:10 -0700152
153 REPORTER_ASSERT(r, info.dimensions() == size);
154 bm.allocPixels(info);
155 SkAutoLockPixels autoLockPixels(bm);
156
157 SkCodec::Result result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes());
158 REPORTER_ASSERT(r, result == expectedResult);
159
160 md5(bm, digest);
161 if (goodDigest) {
162 REPORTER_ASSERT(r, *digest == *goodDigest);
163 }
164
165 {
166 // Test decoding to 565
167 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggo27c17282015-10-27 08:14:46 -0700168 SkCodec::Result expected565 = info.alphaType() == kOpaque_SkAlphaType ?
msarett3d9d7a72015-10-21 10:27:10 -0700169 expectedResult : SkCodec::kInvalidConversion;
170 test_android_info(r, codec, info565, expected565, nullptr);
171 }
172
173 // Verify that re-decoding gives the same result. It is interesting to check this after
174 // a decode to 565, since choosing to decode to 565 may result in some of the decode
175 // options being modified. These options should return to their defaults on another
176 // decode to kN32, so the new digest should match the old digest.
177 test_android_info(r, codec, info, expectedResult, digest);
178
179 {
180 // Check alpha type conversions
181 if (info.alphaType() == kOpaque_SkAlphaType) {
182 test_android_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
183 SkCodec::kInvalidConversion, nullptr);
184 test_android_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
185 SkCodec::kInvalidConversion, nullptr);
186 } else {
187 // Decoding to opaque should fail
188 test_android_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
189 SkCodec::kInvalidConversion, nullptr);
190 SkAlphaType otherAt = info.alphaType();
191 if (kPremul_SkAlphaType == otherAt) {
192 otherAt = kUnpremul_SkAlphaType;
193 } else {
194 otherAt = kPremul_SkAlphaType;
195 }
196 // The other non-opaque alpha type should always succeed, but not match.
197 test_android_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
198 }
199 }
200}
201
scroggo2c3b2182015-10-09 08:40:59 -0700202// FIXME: SkScaledCodec is currently only supported for types used by BRD
halcanary6950de62015-11-07 05:29:00 -0800203// https://bug.skia.org/4428
scroggo2c3b2182015-10-09 08:40:59 -0700204static bool supports_scaled_codec(const char path[]) {
205 static const char* const exts[] = {
206 "jpg", "jpeg", "png", "webp"
207 "JPG", "JPEG", "PNG", "WEBP"
208 };
209
210 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
211 if (SkStrEndsWith(path, exts[i])) {
212 return true;
213 }
214 }
215 return false;
216}
217
msarettcc7f3052015-10-05 14:20:27 -0700218static void check(skiatest::Reporter* r,
219 const char path[],
220 SkISize size,
221 bool supportsScanlineDecoding,
222 bool supportsSubsetDecoding,
msarette6dd0042015-10-09 11:07:34 -0700223 bool supportsIncomplete = true) {
msarettcc7f3052015-10-05 14:20:27 -0700224
225 SkAutoTDelete<SkStream> stream(resource(path));
226 if (!stream) {
227 SkDebugf("Missing resource '%s'\n", path);
228 return;
229 }
msarette6dd0042015-10-09 11:07:34 -0700230
231 SkAutoTDelete<SkCodec> codec(nullptr);
232 bool isIncomplete = supportsIncomplete;
233 if (isIncomplete) {
234 size_t size = stream->getLength();
235 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
236 codec.reset(SkCodec::NewFromData(data));
237 } else {
238 codec.reset(SkCodec::NewFromStream(stream.detach()));
239 }
msarettcc7f3052015-10-05 14:20:27 -0700240 if (!codec) {
241 ERRORF(r, "Unable to decode '%s'", path);
242 return;
243 }
244
245 // Test full image decodes with SkCodec
246 SkMD5::Digest codecDigest;
247 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
248 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700249 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
scroggo27c17282015-10-27 08:14:46 -0700250 test_codec(r, codec, bm, info, size, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700251
252 // Scanline decoding follows.
msarettcc7f3052015-10-05 14:20:27 -0700253 // Need to call startScanlineDecode() first.
scroggo46c57472015-09-30 08:57:13 -0700254 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 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700257 == 0);
scroggo46c57472015-09-30 08:57:13 -0700258
259 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700260 if (supportsScanlineDecoding) {
261 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700262
scroggo46c57472015-09-30 08:57:13 -0700263 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700264
scroggo58421542015-04-01 11:25:20 -0700265 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700266 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
267 if (!isIncomplete) {
268 REPORTER_ASSERT(r, 1 == lines);
269 }
scroggo58421542015-04-01 11:25:20 -0700270 }
271 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700272 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700273 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700274 }
scroggo46c57472015-09-30 08:57:13 -0700275
276 // Cannot continue to decode scanlines beyond the end
277 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700278 == 0);
scroggo46c57472015-09-30 08:57:13 -0700279
280 // Interrupting a scanline decode with a full decode starts from
281 // scratch
282 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700283 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
284 if (!isIncomplete) {
285 REPORTER_ASSERT(r, lines == 1);
286 }
scroggo46c57472015-09-30 08:57:13 -0700287 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700288 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700289 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700290 == 0);
scroggo46c57472015-09-30 08:57:13 -0700291 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700292 == 0);
msarett80803ff2015-10-16 10:54:12 -0700293
294 // Test partial scanline decodes
295 if (supports_scaled_codec(path) && info.width() >= 3) {
296 SkCodec::Options options;
297 int width = info.width();
298 int height = info.height();
299 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
300 options.fSubset = &subset;
301
302 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
303 nullptr, nullptr);
304 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
305
306 for (int y = 0; y < height; y++) {
307 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
308 if (!isIncomplete) {
309 REPORTER_ASSERT(r, 1 == lines);
310 }
311 }
312 }
scroggo58421542015-04-01 11:25:20 -0700313 } else {
scroggo46c57472015-09-30 08:57:13 -0700314 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700315 }
scroggob636b452015-07-22 07:16:20 -0700316
317 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
318 // random subsets.
319 // Do not attempt to decode subsets of an image of only once pixel, since there is no
320 // meaningful subset.
321 if (size.width() * size.height() == 1) {
322 return;
323 }
324
325 SkRandom rand;
326 SkIRect subset;
327 SkCodec::Options opts;
328 opts.fSubset = &subset;
329 for (int i = 0; i < 5; i++) {
330 subset = generate_random_subset(&rand, size.width(), size.height());
331 SkASSERT(!subset.isEmpty());
332 const bool supported = codec->getValidSubset(&subset);
333 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
334
335 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
336 SkBitmap bm;
337 bm.allocPixels(subsetInfo);
338 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700339 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700340
341 if (supportsSubsetDecoding) {
msarette6dd0042015-10-09 11:07:34 -0700342 REPORTER_ASSERT(r, result == expectedResult);
scroggob636b452015-07-22 07:16:20 -0700343 // Webp is the only codec that supports subsets, and it will have modified the subset
344 // to have even left/top.
345 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
346 } else {
347 // No subsets will work.
348 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
349 }
350 }
msarettcc7f3052015-10-05 14:20:27 -0700351
352 // SkScaledCodec tests
scroggo2c3b2182015-10-09 08:40:59 -0700353 if ((supportsScanlineDecoding || supportsSubsetDecoding) && supports_scaled_codec(path)) {
354
msarettcc7f3052015-10-05 14:20:27 -0700355 SkAutoTDelete<SkStream> stream(resource(path));
356 if (!stream) {
357 SkDebugf("Missing resource '%s'\n", path);
358 return;
359 }
msarette6dd0042015-10-09 11:07:34 -0700360
msarett3d9d7a72015-10-21 10:27:10 -0700361 SkAutoTDelete<SkAndroidCodec> codec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700362 if (isIncomplete) {
363 size_t size = stream->getLength();
364 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
msarett3d9d7a72015-10-21 10:27:10 -0700365 codec.reset(SkAndroidCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700366 } else {
msarett3d9d7a72015-10-21 10:27:10 -0700367 codec.reset(SkAndroidCodec::NewFromStream(stream.detach()));
msarette6dd0042015-10-09 11:07:34 -0700368 }
msarettcc7f3052015-10-05 14:20:27 -0700369 if (!codec) {
370 ERRORF(r, "Unable to decode '%s'", path);
371 return;
372 }
373
374 SkBitmap bm;
375 SkMD5::Digest scaledCodecDigest;
scroggo27c17282015-10-27 08:14:46 -0700376 test_android_codec(r, codec, bm, info, size, expectedResult,
msarett3d9d7a72015-10-21 10:27:10 -0700377 &scaledCodecDigest, &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700378 }
379
380 // 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
msarett99f567e2015-08-05 12:58:26 -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
scroggob636b452015-07-22 07:16:20 -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
msarett5406d6f2015-08-31 06:55:13 -0700396 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700397
398 // ICO
msarette6dd0042015-10-09 11:07:34 -0700399 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700400 // These two tests examine interestingly different behavior:
401 // Decodes an embedded BMP image
scroggo27c17282015-10-27 08:14:46 -0700402 check(r, "color_wheel.ico", SkISize::Make(128, 128), false, false, false);
msarett68b204e2015-04-01 12:09:21 -0700403 // Decodes an embedded PNG image
scroggo27c17282015-10-27 08:14:46 -0700404 check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700405
msarett438b2ad2015-04-09 12:43:10 -0700406 // GIF
msarette6dd0042015-10-09 11:07:34 -0700407 // FIXME: We are not ready to test incomplete GIFs
scroggo27c17282015-10-27 08:14:46 -0700408 check(r, "box.gif", SkISize::Make(200, 55), true, false, false);
409 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700410 // randPixels.gif is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700411 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, false);
msarett438b2ad2015-04-09 12:43:10 -0700412
msarette16b04a2015-04-15 07:32:19 -0700413 // JPG
scroggo27c17282015-10-27 08:14:46 -0700414 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false);
scroggob636b452015-07-22 07:16:20 -0700415 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
msarette6dd0042015-10-09 11:07:34 -0700416 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700417 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggob636b452015-07-22 07:16:20 -0700418 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
msarette6dd0042015-10-09 11:07:34 -0700419 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700420 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
msarette16b04a2015-04-15 07:32:19 -0700421
halcanarya096d7a2015-03-27 12:16:53 -0700422 // PNG
scroggo27c17282015-10-27 08:14:46 -0700423 check(r, "arrow.png", SkISize::Make(187, 312), true, false, false);
424 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false, false);
425 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false, false);
426 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false, false);
427 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false, false);
428 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false, false);
429 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false, false);
430 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false, false);
431 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false, false);
432 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false, false);
433 check(r, "plane.png", SkISize::Make(250, 126), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700434 // FIXME: We are not ready to test incomplete interlaced pngs
scroggo27c17282015-10-27 08:14:46 -0700435 check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false, false);
436 check(r, "randPixels.png", SkISize::Make(8, 8), true, false, false);
437 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700438}
scroggo0a7e69c2015-04-03 07:22:22 -0700439
scroggo46c57472015-09-30 08:57:13 -0700440// Test interlaced PNG in stripes, similar to DM's kStripe_Mode
441DEF_TEST(Codec_stripes, r) {
442 const char * path = "plane_interlaced.png";
443 SkAutoTDelete<SkStream> stream(resource(path));
444 if (!stream) {
445 SkDebugf("Missing resource '%s'\n", path);
446 }
447
448 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
449 REPORTER_ASSERT(r, codec);
450
451 if (!codec) {
452 return;
453 }
454
455 switch (codec->getScanlineOrder()) {
456 case SkCodec::kBottomUp_SkScanlineOrder:
457 case SkCodec::kOutOfOrder_SkScanlineOrder:
458 ERRORF(r, "This scanline order will not match the original.");
459 return;
460 default:
461 break;
462 }
463
464 // Baseline for what the image should look like, using N32.
465 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
466
467 SkBitmap bm;
468 bm.allocPixels(info);
469 SkAutoLockPixels autoLockPixels(bm);
470 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
471 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
472
473 SkMD5::Digest digest;
474 md5(bm, &digest);
475
476 // Now decode in stripes
477 const int height = info.height();
478 const int numStripes = 4;
479 int stripeHeight;
480 int remainingLines;
481 SkTDivMod(height, numStripes, &stripeHeight, &remainingLines);
482
483 bm.eraseColor(SK_ColorYELLOW);
484
485 result = codec->startScanlineDecode(info);
486 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
487
488 // Odd stripes
489 for (int i = 1; i < numStripes; i += 2) {
490 // Skip the even stripes
msarette6dd0042015-10-09 11:07:34 -0700491 bool skipResult = codec->skipScanlines(stripeHeight);
492 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700493
msarette6dd0042015-10-09 11:07:34 -0700494 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700495 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700496 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700497 }
498
499 // Even stripes
500 result = codec->startScanlineDecode(info);
501 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
502
503 for (int i = 0; i < numStripes; i += 2) {
msarette6dd0042015-10-09 11:07:34 -0700504 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700505 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700506 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700507
508 // Skip the odd stripes
509 if (i + 1 < numStripes) {
msarette6dd0042015-10-09 11:07:34 -0700510 bool skipResult = codec->skipScanlines(stripeHeight);
511 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700512 }
513 }
514
515 // Remainder at the end
516 if (remainingLines > 0) {
517 result = codec->startScanlineDecode(info);
518 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
519
msarette6dd0042015-10-09 11:07:34 -0700520 bool skipResult = codec->skipScanlines(height - remainingLines);
521 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700522
msarette6dd0042015-10-09 11:07:34 -0700523 int linesDecoded = codec->getScanlines(bm.getAddr(0, height - remainingLines),
scroggo46c57472015-09-30 08:57:13 -0700524 remainingLines, bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700525 REPORTER_ASSERT(r, linesDecoded == remainingLines);
scroggo46c57472015-09-30 08:57:13 -0700526 }
527
528 compare_to_good_digest(r, digest, bm);
529}
530
scroggo0a7e69c2015-04-03 07:22:22 -0700531static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700532 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700533 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700534 REPORTER_ASSERT(r, !codec);
535
msarett3d9d7a72015-10-21 10:27:10 -0700536 SkAndroidCodec* androidCodec =
537 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
538 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700539}
540
541// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
542// even on failure. Test some bad streams.
543DEF_TEST(Codec_leaks, r) {
544 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
545 const char nonSupportedStream[] = "hello world";
546 // The other strings should look like the beginning of a file type, so we'll call some
547 // internal version of NewFromStream, which must also delete the stream on failure.
548 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
549 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
550 const char emptyWebp[] = "RIFF1234WEBPVP";
551 const char emptyBmp[] = { 'B', 'M' };
552 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
553 const char emptyGif[] = "GIFVER";
554
555 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
556 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
557 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
558 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
559 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
560 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
561 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
562}
msarette16b04a2015-04-15 07:32:19 -0700563
scroggo2c3b2182015-10-09 08:40:59 -0700564DEF_TEST(Codec_null, r) {
565 // Attempting to create an SkCodec or an SkScaledCodec with null should not
566 // crash.
567 SkCodec* codec = SkCodec::NewFromStream(nullptr);
568 REPORTER_ASSERT(r, !codec);
569
msarett3d9d7a72015-10-21 10:27:10 -0700570 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
571 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700572}
573
msarette16b04a2015-04-15 07:32:19 -0700574static void test_dimensions(skiatest::Reporter* r, const char path[]) {
575 // Create the codec from the resource file
576 SkAutoTDelete<SkStream> stream(resource(path));
577 if (!stream) {
578 SkDebugf("Missing resource '%s'\n", path);
579 return;
580 }
msarett3d9d7a72015-10-21 10:27:10 -0700581 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.detach()));
msarette16b04a2015-04-15 07:32:19 -0700582 if (!codec) {
583 ERRORF(r, "Unable to create codec '%s'", path);
584 return;
585 }
586
587 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800588 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700589 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700590 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700591 SkImageInfo scaledInfo = codec->getInfo()
592 .makeWH(scaledDims.width(), scaledDims.height())
593 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700594
595 // Set up for the decode
596 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
597 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
598 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
599
msarett3d9d7a72015-10-21 10:27:10 -0700600 SkAndroidCodec::AndroidOptions options;
601 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700602 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700603 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700604 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700605 }
606}
607
608// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
609DEF_TEST(Codec_Dimensions, r) {
610 // JPG
611 test_dimensions(r, "CMYK.jpg");
612 test_dimensions(r, "color_wheel.jpg");
613 test_dimensions(r, "grayscale.jpg");
614 test_dimensions(r, "mandrill_512_q075.jpg");
615 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700616
617 // Decoding small images with very large scaling factors is a potential
618 // source of bugs and crashes. We disable these tests in Gold because
619 // tiny images are not very useful to look at.
620 // Here we make sure that we do not crash or access illegal memory when
621 // performing scaled decodes on small images.
622 test_dimensions(r, "1x1.png");
623 test_dimensions(r, "2x2.png");
624 test_dimensions(r, "3x3.png");
625 test_dimensions(r, "3x1.png");
626 test_dimensions(r, "1x1.png");
627 test_dimensions(r, "16x1.png");
628 test_dimensions(r, "1x16.png");
629 test_dimensions(r, "mandrill_16.png");
630
msarette16b04a2015-04-15 07:32:19 -0700631}
632
msarettd0375bc2015-08-12 08:08:56 -0700633static void test_invalid(skiatest::Reporter* r, const char path[]) {
msarett4b17fa32015-04-23 08:53:39 -0700634 SkAutoTDelete<SkStream> stream(resource(path));
635 if (!stream) {
636 SkDebugf("Missing resource '%s'\n", path);
637 return;
638 }
639 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700640 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700641}
msarette16b04a2015-04-15 07:32:19 -0700642
msarett4b17fa32015-04-23 08:53:39 -0700643DEF_TEST(Codec_Empty, r) {
644 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700645 test_invalid(r, "empty_images/zero-dims.gif");
646 test_invalid(r, "empty_images/zero-embedded.ico");
647 test_invalid(r, "empty_images/zero-width.bmp");
648 test_invalid(r, "empty_images/zero-height.bmp");
649 test_invalid(r, "empty_images/zero-width.jpg");
650 test_invalid(r, "empty_images/zero-height.jpg");
651 test_invalid(r, "empty_images/zero-width.png");
652 test_invalid(r, "empty_images/zero-height.png");
653 test_invalid(r, "empty_images/zero-width.wbmp");
654 test_invalid(r, "empty_images/zero-height.wbmp");
655 // This image is an ico with an embedded mask-bmp. This is illegal.
656 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700657}
msarett99f567e2015-08-05 12:58:26 -0700658
659static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
660 SkAutoTDelete<SkStream> stream(resource(path));
661 if (!stream) {
662 SkDebugf("Missing resource '%s'\n", path);
663 return;
664 }
scroggo46c57472015-09-30 08:57:13 -0700665 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.detach()));
msarett99f567e2015-08-05 12:58:26 -0700666
667 // This should return kSuccess because kIndex8 is supported.
668 SkPMColor colorStorage[256];
669 int colorCount;
scroggo46c57472015-09-30 08:57:13 -0700670 SkCodec::Result result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700671 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
msarett99f567e2015-08-05 12:58:26 -0700672 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
673 // The rest of the test is uninteresting if kIndex8 is not supported
674 if (SkCodec::kSuccess != result) {
675 return;
676 }
677
678 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
679 // colorPtr and a valid colorCountPtr.
scroggo46c57472015-09-30 08:57:13 -0700680 result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700681 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
msarett99f567e2015-08-05 12:58:26 -0700682 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
scroggo46c57472015-09-30 08:57:13 -0700683 result = decoder->startScanlineDecode(
msarett99f567e2015-08-05 12:58:26 -0700684 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
685 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
686}
687
688DEF_TEST(Codec_Params, r) {
689 test_invalid_parameters(r, "index8.png");
690 test_invalid_parameters(r, "mandrill.wbmp");
691}
scroggocf98fa92015-11-23 08:14:40 -0800692
693static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
694 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
695 if (!sk_stream->write(data, len)) {
696 png_error(png_ptr, "sk_write_fn Error!");
697 }
698}
699
700#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
701DEF_TEST(Codec_pngChunkReader, r) {
702 // Create a dummy bitmap. Use unpremul RGBA for libpng.
703 SkBitmap bm;
704 const int w = 1;
705 const int h = 1;
706 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
707 kUnpremul_SkAlphaType);
708 bm.setInfo(bmInfo);
709 bm.allocPixels();
710 bm.eraseColor(SK_ColorBLUE);
711 SkMD5::Digest goodDigest;
712 md5(bm, &goodDigest);
713
714 // Write to a png file.
715 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
716 REPORTER_ASSERT(r, png);
717 if (!png) {
718 return;
719 }
720
721 png_infop info = png_create_info_struct(png);
722 REPORTER_ASSERT(r, info);
723 if (!info) {
724 png_destroy_write_struct(&png, nullptr);
725 return;
726 }
727
728 if (setjmp(png_jmpbuf(png))) {
729 ERRORF(r, "failed writing png");
730 png_destroy_write_struct(&png, &info);
731 return;
732 }
733
734 SkDynamicMemoryWStream wStream;
735 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
736
737 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
738 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
739 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
740
741 // Create some chunks that match the Android framework's use.
742 static png_unknown_chunk gUnknowns[] = {
743 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_PLTE },
744 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_PLTE },
745 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_PLTE },
746 };
747
748 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
749 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
750#if PNG_LIBPNG_VER < 10600
751 /* Deal with unknown chunk location bug in 1.5.x and earlier */
752 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_PLTE);
753 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_PLTE);
754#endif
755
756 png_write_info(png, info);
757
758 for (int j = 0; j < h; j++) {
759 png_bytep row = (png_bytep)(bm.getAddr(0, j));
760 png_write_rows(png, &row, 1);
761 }
762 png_write_end(png, info);
763 png_destroy_write_struct(&png, &info);
764
765 class ChunkReader : public SkPngChunkReader {
766 public:
767 ChunkReader(skiatest::Reporter* r)
768 : fReporter(r)
769 {
770 this->reset();
771 }
772
773 bool readChunk(const char tag[], const void* data, size_t length) override {
774 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
775 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
776 // Tag matches. This should have been the first time we see it.
777 REPORTER_ASSERT(fReporter, !fSeen[i]);
778 fSeen[i] = true;
779
780 // Data and length should match
781 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
782 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
783 (const char*) gUnknowns[i].data));
784 return true;
785 }
786 }
787 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
788 return true;
789 }
790
791 bool allHaveBeenSeen() {
792 bool ret = true;
793 for (auto seen : fSeen) {
794 ret &= seen;
795 }
796 return ret;
797 }
798
799 void reset() {
800 sk_bzero(fSeen, sizeof(fSeen));
801 }
802
803 private:
804 skiatest::Reporter* fReporter; // Unowned
805 bool fSeen[3];
806 };
807
808 ChunkReader chunkReader(r);
809
810 // Now read the file with SkCodec.
811 SkAutoTUnref<SkData> data(wStream.copyToData());
812 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data, &chunkReader));
813 REPORTER_ASSERT(r, codec);
814 if (!codec) {
815 return;
816 }
817
818 // Now compare to the original.
819 SkBitmap decodedBm;
820 decodedBm.setInfo(codec->getInfo());
821 decodedBm.allocPixels();
822 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
823 decodedBm.rowBytes());
824 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
825
826 if (decodedBm.colorType() != bm.colorType()) {
827 SkBitmap tmp;
828 bool success = decodedBm.copyTo(&tmp, bm.colorType());
829 REPORTER_ASSERT(r, success);
830 if (!success) {
831 return;
832 }
833
834 tmp.swap(decodedBm);
835 }
836
837 compare_to_good_digest(r, goodDigest, decodedBm);
838 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
839
840 // Decoding again will read the chunks again.
841 chunkReader.reset();
842 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
843 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
844 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
845 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
846}
847#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED