blob: b2743bfa1f9f248a2c5028f5c9766be866b369af [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"
halcanarya096d7a2015-03-27 12:16:53 -070015#include "Test.h"
16
17static SkStreamAsset* resource(const char path[]) {
18 SkString fullPath = GetResourcePath(path);
19 return SkStream::NewFromFile(fullPath.c_str());
20}
21
22static void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
23 SkAutoLockPixels autoLockPixels(bm);
24 SkASSERT(bm.getPixels());
25 SkMD5 md5;
26 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
27 for (int y = 0; y < bm.height(); ++y) {
28 md5.update(static_cast<uint8_t*>(bm.getAddr(0, y)), rowLen);
29 }
30 md5.finish(*digest);
31}
32
scroggo9b2cdbf42015-07-10 12:07:02 -070033/**
34 * Compute the digest for bm and compare it to a known good digest.
35 * @param r Reporter to assert that bm's digest matches goodDigest.
36 * @param goodDigest The known good digest to compare to.
37 * @param bm The bitmap to test.
38 */
39static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
40 const SkBitmap& bm) {
41 SkMD5::Digest digest;
42 md5(bm, &digest);
43 REPORTER_ASSERT(r, digest == goodDigest);
44}
45
scroggod1bc5742015-08-12 08:31:44 -070046/**
47 * Test decoding an SkCodec to a particular SkImageInfo.
48 *
halcanary96fcdcc2015-08-27 07:41:13 -070049 * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr,
scroggod1bc5742015-08-12 08:31:44 -070050 * the resulting decode should match.
51 */
52static void test_info(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
53 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
54 SkBitmap bm;
55 bm.allocPixels(info);
56 SkAutoLockPixels autoLockPixels(bm);
57
58 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
59 REPORTER_ASSERT(r, result == expectedResult);
60
61 if (goodDigest) {
62 compare_to_good_digest(r, *goodDigest, bm);
63 }
64}
65
msarett3d9d7a72015-10-21 10:27:10 -070066static void test_android_info(skiatest::Reporter* r, SkAndroidCodec* codec, const SkImageInfo& info,
67 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
68 SkBitmap bm;
69 bm.allocPixels(info);
70 SkAutoLockPixels autoLockPixels(bm);
71
72 SkCodec::Result result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes());
73 REPORTER_ASSERT(r, result == expectedResult);
74
75 if (goodDigest) {
76 compare_to_good_digest(r, *goodDigest, bm);
77 }
78}
79
scroggob636b452015-07-22 07:16:20 -070080SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
81 SkIRect rect;
82 do {
83 rect.fLeft = rand->nextRangeU(0, w);
84 rect.fTop = rand->nextRangeU(0, h);
85 rect.fRight = rand->nextRangeU(0, w);
86 rect.fBottom = rand->nextRangeU(0, h);
87 rect.sort();
88 } while (rect.isEmpty());
89 return rect;
90}
91
msarettcc7f3052015-10-05 14:20:27 -070092static void test_codec(skiatest::Reporter* r, SkCodec* codec, SkBitmap& bm, const SkImageInfo& info,
msarette6dd0042015-10-09 11:07:34 -070093 const SkISize& size, bool supports565, SkCodec::Result expectedResult,
94 SkMD5::Digest* digest, const SkMD5::Digest* goodDigest) {
95
halcanarya096d7a2015-03-27 12:16:53 -070096 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -070097 bm.allocPixels(info);
98 SkAutoLockPixels autoLockPixels(bm);
msarettcc7f3052015-10-05 14:20:27 -070099
100 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700101 REPORTER_ASSERT(r, result == expectedResult);
halcanarya096d7a2015-03-27 12:16:53 -0700102
msarettcc7f3052015-10-05 14:20:27 -0700103 md5(bm, digest);
104 if (goodDigest) {
105 REPORTER_ASSERT(r, *digest == *goodDigest);
106 }
halcanarya096d7a2015-03-27 12:16:53 -0700107
msarett8ff6ca62015-09-18 12:06:04 -0700108 {
109 // Test decoding to 565
110 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
msarette6dd0042015-10-09 11:07:34 -0700111 SkCodec::Result expected565 = (supports565 && info.alphaType() == kOpaque_SkAlphaType) ?
112 expectedResult : SkCodec::kInvalidConversion;
113 test_info(r, codec, info565, expected565, nullptr);
msarett8ff6ca62015-09-18 12:06:04 -0700114 }
115
116 // Verify that re-decoding gives the same result. It is interesting to check this after
117 // a decode to 565, since choosing to decode to 565 may result in some of the decode
118 // options being modified. These options should return to their defaults on another
119 // decode to kN32, so the new digest should match the old digest.
msarette6dd0042015-10-09 11:07:34 -0700120 test_info(r, codec, info, expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700121
122 {
123 // Check alpha type conversions
124 if (info.alphaType() == kOpaque_SkAlphaType) {
125 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700126 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700127 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700128 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700129 } else {
130 // Decoding to opaque should fail
131 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700132 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700133 SkAlphaType otherAt = info.alphaType();
134 if (kPremul_SkAlphaType == otherAt) {
135 otherAt = kUnpremul_SkAlphaType;
136 } else {
137 otherAt = kPremul_SkAlphaType;
138 }
139 // The other non-opaque alpha type should always succeed, but not match.
msarette6dd0042015-10-09 11:07:34 -0700140 test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700141 }
142 }
msarettcc7f3052015-10-05 14:20:27 -0700143}
144
msarett3d9d7a72015-10-21 10:27:10 -0700145static void test_android_codec(skiatest::Reporter* r, SkAndroidCodec* codec, SkBitmap& bm,
146 const SkImageInfo& info, const SkISize& size, bool supports565,
147 SkCodec::Result expectedResult, SkMD5::Digest* digest, const SkMD5::Digest* goodDigest) {
148
149 REPORTER_ASSERT(r, info.dimensions() == size);
150 bm.allocPixels(info);
151 SkAutoLockPixels autoLockPixels(bm);
152
153 SkCodec::Result result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes());
154 REPORTER_ASSERT(r, result == expectedResult);
155
156 md5(bm, digest);
157 if (goodDigest) {
158 REPORTER_ASSERT(r, *digest == *goodDigest);
159 }
160
161 {
162 // Test decoding to 565
163 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
164 SkCodec::Result expected565 = (supports565 && info.alphaType() == kOpaque_SkAlphaType) ?
165 expectedResult : SkCodec::kInvalidConversion;
166 test_android_info(r, codec, info565, expected565, nullptr);
167 }
168
169 // Verify that re-decoding gives the same result. It is interesting to check this after
170 // a decode to 565, since choosing to decode to 565 may result in some of the decode
171 // options being modified. These options should return to their defaults on another
172 // decode to kN32, so the new digest should match the old digest.
173 test_android_info(r, codec, info, expectedResult, digest);
174
175 {
176 // Check alpha type conversions
177 if (info.alphaType() == kOpaque_SkAlphaType) {
178 test_android_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
179 SkCodec::kInvalidConversion, nullptr);
180 test_android_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
181 SkCodec::kInvalidConversion, nullptr);
182 } else {
183 // Decoding to opaque should fail
184 test_android_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
185 SkCodec::kInvalidConversion, nullptr);
186 SkAlphaType otherAt = info.alphaType();
187 if (kPremul_SkAlphaType == otherAt) {
188 otherAt = kUnpremul_SkAlphaType;
189 } else {
190 otherAt = kPremul_SkAlphaType;
191 }
192 // The other non-opaque alpha type should always succeed, but not match.
193 test_android_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
194 }
195 }
196}
197
scroggo2c3b2182015-10-09 08:40:59 -0700198// FIXME: SkScaledCodec is currently only supported for types used by BRD
199// skbug.com/4428
200static bool supports_scaled_codec(const char path[]) {
201 static const char* const exts[] = {
202 "jpg", "jpeg", "png", "webp"
203 "JPG", "JPEG", "PNG", "WEBP"
204 };
205
206 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
207 if (SkStrEndsWith(path, exts[i])) {
208 return true;
209 }
210 }
211 return false;
212}
213
msarettcc7f3052015-10-05 14:20:27 -0700214static void check(skiatest::Reporter* r,
215 const char path[],
216 SkISize size,
217 bool supportsScanlineDecoding,
218 bool supportsSubsetDecoding,
msarette6dd0042015-10-09 11:07:34 -0700219 bool supports565 = true,
220 bool supportsIncomplete = true) {
msarettcc7f3052015-10-05 14:20:27 -0700221
222 SkAutoTDelete<SkStream> stream(resource(path));
223 if (!stream) {
224 SkDebugf("Missing resource '%s'\n", path);
225 return;
226 }
msarette6dd0042015-10-09 11:07:34 -0700227
228 SkAutoTDelete<SkCodec> codec(nullptr);
229 bool isIncomplete = supportsIncomplete;
230 if (isIncomplete) {
231 size_t size = stream->getLength();
232 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
233 codec.reset(SkCodec::NewFromData(data));
234 } else {
235 codec.reset(SkCodec::NewFromStream(stream.detach()));
236 }
msarettcc7f3052015-10-05 14:20:27 -0700237 if (!codec) {
238 ERRORF(r, "Unable to decode '%s'", path);
239 return;
240 }
241
242 // Test full image decodes with SkCodec
243 SkMD5::Digest codecDigest;
244 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
245 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700246 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
247 test_codec(r, codec, bm, info, size, supports565, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700248
249 // Scanline decoding follows.
msarettcc7f3052015-10-05 14:20:27 -0700250 // Need to call startScanlineDecode() first.
scroggo46c57472015-09-30 08:57:13 -0700251 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700252 == 0);
scroggo46c57472015-09-30 08:57:13 -0700253 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700254 == 0);
scroggo46c57472015-09-30 08:57:13 -0700255
256 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700257 if (supportsScanlineDecoding) {
258 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700259
scroggo46c57472015-09-30 08:57:13 -0700260 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700261
scroggo58421542015-04-01 11:25:20 -0700262 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700263 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
264 if (!isIncomplete) {
265 REPORTER_ASSERT(r, 1 == lines);
266 }
scroggo58421542015-04-01 11:25:20 -0700267 }
268 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700269 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700270 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700271 }
scroggo46c57472015-09-30 08:57:13 -0700272
273 // Cannot continue to decode scanlines beyond the end
274 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700275 == 0);
scroggo46c57472015-09-30 08:57:13 -0700276
277 // Interrupting a scanline decode with a full decode starts from
278 // scratch
279 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700280 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
281 if (!isIncomplete) {
282 REPORTER_ASSERT(r, lines == 1);
283 }
scroggo46c57472015-09-30 08:57:13 -0700284 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700285 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700286 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700287 == 0);
scroggo46c57472015-09-30 08:57:13 -0700288 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700289 == 0);
msarett80803ff2015-10-16 10:54:12 -0700290
291 // Test partial scanline decodes
292 if (supports_scaled_codec(path) && info.width() >= 3) {
293 SkCodec::Options options;
294 int width = info.width();
295 int height = info.height();
296 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
297 options.fSubset = &subset;
298
299 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
300 nullptr, nullptr);
301 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
302
303 for (int y = 0; y < height; y++) {
304 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
305 if (!isIncomplete) {
306 REPORTER_ASSERT(r, 1 == lines);
307 }
308 }
309 }
scroggo58421542015-04-01 11:25:20 -0700310 } else {
scroggo46c57472015-09-30 08:57:13 -0700311 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700312 }
scroggob636b452015-07-22 07:16:20 -0700313
314 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
315 // random subsets.
316 // Do not attempt to decode subsets of an image of only once pixel, since there is no
317 // meaningful subset.
318 if (size.width() * size.height() == 1) {
319 return;
320 }
321
322 SkRandom rand;
323 SkIRect subset;
324 SkCodec::Options opts;
325 opts.fSubset = &subset;
326 for (int i = 0; i < 5; i++) {
327 subset = generate_random_subset(&rand, size.width(), size.height());
328 SkASSERT(!subset.isEmpty());
329 const bool supported = codec->getValidSubset(&subset);
330 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
331
332 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
333 SkBitmap bm;
334 bm.allocPixels(subsetInfo);
335 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700336 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700337
338 if (supportsSubsetDecoding) {
msarette6dd0042015-10-09 11:07:34 -0700339 REPORTER_ASSERT(r, result == expectedResult);
scroggob636b452015-07-22 07:16:20 -0700340 // Webp is the only codec that supports subsets, and it will have modified the subset
341 // to have even left/top.
342 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
343 } else {
344 // No subsets will work.
345 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
346 }
347 }
msarettcc7f3052015-10-05 14:20:27 -0700348
349 // SkScaledCodec tests
scroggo2c3b2182015-10-09 08:40:59 -0700350 if ((supportsScanlineDecoding || supportsSubsetDecoding) && supports_scaled_codec(path)) {
351
msarettcc7f3052015-10-05 14:20:27 -0700352 SkAutoTDelete<SkStream> stream(resource(path));
353 if (!stream) {
354 SkDebugf("Missing resource '%s'\n", path);
355 return;
356 }
msarette6dd0042015-10-09 11:07:34 -0700357
msarett3d9d7a72015-10-21 10:27:10 -0700358 SkAutoTDelete<SkAndroidCodec> codec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700359 if (isIncomplete) {
360 size_t size = stream->getLength();
361 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
msarett3d9d7a72015-10-21 10:27:10 -0700362 codec.reset(SkAndroidCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700363 } else {
msarett3d9d7a72015-10-21 10:27:10 -0700364 codec.reset(SkAndroidCodec::NewFromStream(stream.detach()));
msarette6dd0042015-10-09 11:07:34 -0700365 }
msarettcc7f3052015-10-05 14:20:27 -0700366 if (!codec) {
367 ERRORF(r, "Unable to decode '%s'", path);
368 return;
369 }
370
371 SkBitmap bm;
372 SkMD5::Digest scaledCodecDigest;
msarett3d9d7a72015-10-21 10:27:10 -0700373 test_android_codec(r, codec, bm, info, size, supports565, expectedResult,
374 &scaledCodecDigest, &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700375 }
376
377 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
378 if (isIncomplete) {
379 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, supports565, false);
msarettcc7f3052015-10-05 14:20:27 -0700380 }
halcanarya096d7a2015-03-27 12:16:53 -0700381}
382
383DEF_TEST(Codec, r) {
384 // WBMP
msarett99f567e2015-08-05 12:58:26 -0700385 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700386
scroggo6f5e6192015-06-18 12:53:43 -0700387 // WEBP
scroggob636b452015-07-22 07:16:20 -0700388 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true);
389 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true);
390 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true);
scroggo6f5e6192015-06-18 12:53:43 -0700391
halcanarya096d7a2015-03-27 12:16:53 -0700392 // BMP
msarett5406d6f2015-08-31 06:55:13 -0700393 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700394
395 // ICO
msarette6dd0042015-10-09 11:07:34 -0700396 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700397 // These two tests examine interestingly different behavior:
398 // Decodes an embedded BMP image
msarette6dd0042015-10-09 11:07:34 -0700399 check(r, "color_wheel.ico", SkISize::Make(128, 128), false, false, true, false);
msarett68b204e2015-04-01 12:09:21 -0700400 // Decodes an embedded PNG image
msarette6dd0042015-10-09 11:07:34 -0700401 check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false, true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700402
msarett438b2ad2015-04-09 12:43:10 -0700403 // GIF
msarette6dd0042015-10-09 11:07:34 -0700404 // FIXME: We are not ready to test incomplete GIFs
405 check(r, "box.gif", SkISize::Make(200, 55), true, false, true, false);
406 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, true, false);
407 // randPixels.gif is too small to test incomplete
408 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, true, false);
msarett438b2ad2015-04-09 12:43:10 -0700409
msarette16b04a2015-04-15 07:32:19 -0700410 // JPG
scroggocc2feb12015-08-14 08:32:46 -0700411 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false, false);
scroggob636b452015-07-22 07:16:20 -0700412 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
msarette6dd0042015-10-09 11:07:34 -0700413 // grayscale.jpg is too small to test incomplete
414 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, true, false);
scroggob636b452015-07-22 07:16:20 -0700415 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
msarette6dd0042015-10-09 11:07:34 -0700416 // randPixels.jpg is too small to test incomplete
417 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, true, false);
msarette16b04a2015-04-15 07:32:19 -0700418
halcanarya096d7a2015-03-27 12:16:53 -0700419 // PNG
msarette6dd0042015-10-09 11:07:34 -0700420 check(r, "arrow.png", SkISize::Make(187, 312), true, false, true, false);
421 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false, true, false);
422 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false, true, false);
423 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false, true, false);
424 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false, true, false);
425 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false, true, false);
426 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false, true, false);
427 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false, true, false);
428 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false, true, false);
429 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false, true, false);
430 check(r, "plane.png", SkISize::Make(250, 126), true, false, true, false);
431 // FIXME: We are not ready to test incomplete interlaced pngs
432 check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false, true, false);
433 check(r, "randPixels.png", SkISize::Make(8, 8), true, false, true, false);
434 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false, true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700435}
scroggo0a7e69c2015-04-03 07:22:22 -0700436
scroggo46c57472015-09-30 08:57:13 -0700437// Test interlaced PNG in stripes, similar to DM's kStripe_Mode
438DEF_TEST(Codec_stripes, r) {
439 const char * path = "plane_interlaced.png";
440 SkAutoTDelete<SkStream> stream(resource(path));
441 if (!stream) {
442 SkDebugf("Missing resource '%s'\n", path);
443 }
444
445 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
446 REPORTER_ASSERT(r, codec);
447
448 if (!codec) {
449 return;
450 }
451
452 switch (codec->getScanlineOrder()) {
453 case SkCodec::kBottomUp_SkScanlineOrder:
454 case SkCodec::kOutOfOrder_SkScanlineOrder:
455 ERRORF(r, "This scanline order will not match the original.");
456 return;
457 default:
458 break;
459 }
460
461 // Baseline for what the image should look like, using N32.
462 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
463
464 SkBitmap bm;
465 bm.allocPixels(info);
466 SkAutoLockPixels autoLockPixels(bm);
467 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
468 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
469
470 SkMD5::Digest digest;
471 md5(bm, &digest);
472
473 // Now decode in stripes
474 const int height = info.height();
475 const int numStripes = 4;
476 int stripeHeight;
477 int remainingLines;
478 SkTDivMod(height, numStripes, &stripeHeight, &remainingLines);
479
480 bm.eraseColor(SK_ColorYELLOW);
481
482 result = codec->startScanlineDecode(info);
483 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
484
485 // Odd stripes
486 for (int i = 1; i < numStripes; i += 2) {
487 // Skip the even stripes
msarette6dd0042015-10-09 11:07:34 -0700488 bool skipResult = codec->skipScanlines(stripeHeight);
489 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700490
msarette6dd0042015-10-09 11:07:34 -0700491 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700492 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700493 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700494 }
495
496 // Even stripes
497 result = codec->startScanlineDecode(info);
498 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
499
500 for (int i = 0; i < numStripes; i += 2) {
msarette6dd0042015-10-09 11:07:34 -0700501 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700502 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700503 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700504
505 // Skip the odd stripes
506 if (i + 1 < numStripes) {
msarette6dd0042015-10-09 11:07:34 -0700507 bool skipResult = codec->skipScanlines(stripeHeight);
508 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700509 }
510 }
511
512 // Remainder at the end
513 if (remainingLines > 0) {
514 result = codec->startScanlineDecode(info);
515 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
516
msarette6dd0042015-10-09 11:07:34 -0700517 bool skipResult = codec->skipScanlines(height - remainingLines);
518 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700519
msarette6dd0042015-10-09 11:07:34 -0700520 int linesDecoded = codec->getScanlines(bm.getAddr(0, height - remainingLines),
scroggo46c57472015-09-30 08:57:13 -0700521 remainingLines, bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700522 REPORTER_ASSERT(r, linesDecoded == remainingLines);
scroggo46c57472015-09-30 08:57:13 -0700523 }
524
525 compare_to_good_digest(r, digest, bm);
526}
527
scroggo0a7e69c2015-04-03 07:22:22 -0700528static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700529 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700530 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700531 REPORTER_ASSERT(r, !codec);
532
msarett3d9d7a72015-10-21 10:27:10 -0700533 SkAndroidCodec* androidCodec =
534 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
535 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700536}
537
538// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
539// even on failure. Test some bad streams.
540DEF_TEST(Codec_leaks, r) {
541 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
542 const char nonSupportedStream[] = "hello world";
543 // The other strings should look like the beginning of a file type, so we'll call some
544 // internal version of NewFromStream, which must also delete the stream on failure.
545 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
546 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
547 const char emptyWebp[] = "RIFF1234WEBPVP";
548 const char emptyBmp[] = { 'B', 'M' };
549 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
550 const char emptyGif[] = "GIFVER";
551
552 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
553 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
554 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
555 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
556 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
557 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
558 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
559}
msarette16b04a2015-04-15 07:32:19 -0700560
scroggo2c3b2182015-10-09 08:40:59 -0700561DEF_TEST(Codec_null, r) {
562 // Attempting to create an SkCodec or an SkScaledCodec with null should not
563 // crash.
564 SkCodec* codec = SkCodec::NewFromStream(nullptr);
565 REPORTER_ASSERT(r, !codec);
566
msarett3d9d7a72015-10-21 10:27:10 -0700567 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
568 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700569}
570
msarette16b04a2015-04-15 07:32:19 -0700571static void test_dimensions(skiatest::Reporter* r, const char path[]) {
572 // Create the codec from the resource file
573 SkAutoTDelete<SkStream> stream(resource(path));
574 if (!stream) {
575 SkDebugf("Missing resource '%s'\n", path);
576 return;
577 }
msarett3d9d7a72015-10-21 10:27:10 -0700578 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.detach()));
msarette16b04a2015-04-15 07:32:19 -0700579 if (!codec) {
580 ERRORF(r, "Unable to create codec '%s'", path);
581 return;
582 }
583
584 // Check that the decode is successful for a variety of scales
msarett3d9d7a72015-10-21 10:27:10 -0700585 for (int sampleSize = 1; sampleSize < 10; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700586 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700587 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700588 SkImageInfo scaledInfo = codec->getInfo()
589 .makeWH(scaledDims.width(), scaledDims.height())
590 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700591
592 // Set up for the decode
593 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
594 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
595 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
596
msarett3d9d7a72015-10-21 10:27:10 -0700597 SkAndroidCodec::AndroidOptions options;
598 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700599 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700600 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700601 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700602 }
603}
604
605// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
606DEF_TEST(Codec_Dimensions, r) {
607 // JPG
608 test_dimensions(r, "CMYK.jpg");
609 test_dimensions(r, "color_wheel.jpg");
610 test_dimensions(r, "grayscale.jpg");
611 test_dimensions(r, "mandrill_512_q075.jpg");
612 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700613
614 // Decoding small images with very large scaling factors is a potential
615 // source of bugs and crashes. We disable these tests in Gold because
616 // tiny images are not very useful to look at.
617 // Here we make sure that we do not crash or access illegal memory when
618 // performing scaled decodes on small images.
619 test_dimensions(r, "1x1.png");
620 test_dimensions(r, "2x2.png");
621 test_dimensions(r, "3x3.png");
622 test_dimensions(r, "3x1.png");
623 test_dimensions(r, "1x1.png");
624 test_dimensions(r, "16x1.png");
625 test_dimensions(r, "1x16.png");
626 test_dimensions(r, "mandrill_16.png");
627
msarette16b04a2015-04-15 07:32:19 -0700628}
629
msarettd0375bc2015-08-12 08:08:56 -0700630static void test_invalid(skiatest::Reporter* r, const char path[]) {
msarett4b17fa32015-04-23 08:53:39 -0700631 SkAutoTDelete<SkStream> stream(resource(path));
632 if (!stream) {
633 SkDebugf("Missing resource '%s'\n", path);
634 return;
635 }
636 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700637 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700638}
msarette16b04a2015-04-15 07:32:19 -0700639
msarett4b17fa32015-04-23 08:53:39 -0700640DEF_TEST(Codec_Empty, r) {
641 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700642 test_invalid(r, "empty_images/zero-dims.gif");
643 test_invalid(r, "empty_images/zero-embedded.ico");
644 test_invalid(r, "empty_images/zero-width.bmp");
645 test_invalid(r, "empty_images/zero-height.bmp");
646 test_invalid(r, "empty_images/zero-width.jpg");
647 test_invalid(r, "empty_images/zero-height.jpg");
648 test_invalid(r, "empty_images/zero-width.png");
649 test_invalid(r, "empty_images/zero-height.png");
650 test_invalid(r, "empty_images/zero-width.wbmp");
651 test_invalid(r, "empty_images/zero-height.wbmp");
652 // This image is an ico with an embedded mask-bmp. This is illegal.
653 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700654}
msarett99f567e2015-08-05 12:58:26 -0700655
656static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
657 SkAutoTDelete<SkStream> stream(resource(path));
658 if (!stream) {
659 SkDebugf("Missing resource '%s'\n", path);
660 return;
661 }
scroggo46c57472015-09-30 08:57:13 -0700662 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.detach()));
msarett99f567e2015-08-05 12:58:26 -0700663
664 // This should return kSuccess because kIndex8 is supported.
665 SkPMColor colorStorage[256];
666 int colorCount;
scroggo46c57472015-09-30 08:57:13 -0700667 SkCodec::Result result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700668 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
msarett99f567e2015-08-05 12:58:26 -0700669 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
670 // The rest of the test is uninteresting if kIndex8 is not supported
671 if (SkCodec::kSuccess != result) {
672 return;
673 }
674
675 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
676 // colorPtr and a valid colorCountPtr.
scroggo46c57472015-09-30 08:57:13 -0700677 result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700678 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
msarett99f567e2015-08-05 12:58:26 -0700679 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
scroggo46c57472015-09-30 08:57:13 -0700680 result = decoder->startScanlineDecode(
msarett99f567e2015-08-05 12:58:26 -0700681 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
682 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
683}
684
685DEF_TEST(Codec_Params, r) {
686 test_invalid_parameters(r, "index8.png");
687 test_invalid_parameters(r, "mandrill.wbmp");
688}