blob: bd3e2031c96b7b0264ef3c59ae294ec0c15096ac [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,
scroggo27c17282015-10-27 08:14:46 -070093 const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest,
94 const SkMD5::Digest* goodDigest) {
msarette6dd0042015-10-09 11:07:34 -070095
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);
scroggo27c17282015-10-27 08:14:46 -0700111 SkCodec::Result expected565 = info.alphaType() == kOpaque_SkAlphaType ?
msarette6dd0042015-10-09 11:07:34 -0700112 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,
scroggo27c17282015-10-27 08:14:46 -0700146 const SkImageInfo& info, const SkISize& size, SkCodec::Result expectedResult,
147 SkMD5::Digest* digest, const SkMD5::Digest* goodDigest) {
msarett3d9d7a72015-10-21 10:27:10 -0700148
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);
scroggo27c17282015-10-27 08:14:46 -0700164 SkCodec::Result expected565 = info.alphaType() == kOpaque_SkAlphaType ?
msarett3d9d7a72015-10-21 10:27:10 -0700165 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 supportsIncomplete = true) {
msarettcc7f3052015-10-05 14:20:27 -0700220
221 SkAutoTDelete<SkStream> stream(resource(path));
222 if (!stream) {
223 SkDebugf("Missing resource '%s'\n", path);
224 return;
225 }
msarette6dd0042015-10-09 11:07:34 -0700226
227 SkAutoTDelete<SkCodec> codec(nullptr);
228 bool isIncomplete = supportsIncomplete;
229 if (isIncomplete) {
230 size_t size = stream->getLength();
231 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
232 codec.reset(SkCodec::NewFromData(data));
233 } else {
234 codec.reset(SkCodec::NewFromStream(stream.detach()));
235 }
msarettcc7f3052015-10-05 14:20:27 -0700236 if (!codec) {
237 ERRORF(r, "Unable to decode '%s'", path);
238 return;
239 }
240
241 // Test full image decodes with SkCodec
242 SkMD5::Digest codecDigest;
243 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
244 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700245 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
scroggo27c17282015-10-27 08:14:46 -0700246 test_codec(r, codec, bm, info, size, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700247
248 // Scanline decoding follows.
msarettcc7f3052015-10-05 14:20:27 -0700249 // Need to call startScanlineDecode() first.
scroggo46c57472015-09-30 08:57:13 -0700250 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700251 == 0);
scroggo46c57472015-09-30 08:57:13 -0700252 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700253 == 0);
scroggo46c57472015-09-30 08:57:13 -0700254
255 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700256 if (supportsScanlineDecoding) {
257 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700258
scroggo46c57472015-09-30 08:57:13 -0700259 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700260
scroggo58421542015-04-01 11:25:20 -0700261 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700262 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
263 if (!isIncomplete) {
264 REPORTER_ASSERT(r, 1 == lines);
265 }
scroggo58421542015-04-01 11:25:20 -0700266 }
267 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700268 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700269 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700270 }
scroggo46c57472015-09-30 08:57:13 -0700271
272 // Cannot continue to decode scanlines beyond the end
273 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700274 == 0);
scroggo46c57472015-09-30 08:57:13 -0700275
276 // Interrupting a scanline decode with a full decode starts from
277 // scratch
278 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700279 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
280 if (!isIncomplete) {
281 REPORTER_ASSERT(r, lines == 1);
282 }
scroggo46c57472015-09-30 08:57:13 -0700283 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700284 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700285 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700286 == 0);
scroggo46c57472015-09-30 08:57:13 -0700287 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700288 == 0);
msarett80803ff2015-10-16 10:54:12 -0700289
290 // Test partial scanline decodes
291 if (supports_scaled_codec(path) && info.width() >= 3) {
292 SkCodec::Options options;
293 int width = info.width();
294 int height = info.height();
295 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
296 options.fSubset = &subset;
297
298 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
299 nullptr, nullptr);
300 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
301
302 for (int y = 0; y < height; y++) {
303 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
304 if (!isIncomplete) {
305 REPORTER_ASSERT(r, 1 == lines);
306 }
307 }
308 }
scroggo58421542015-04-01 11:25:20 -0700309 } else {
scroggo46c57472015-09-30 08:57:13 -0700310 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700311 }
scroggob636b452015-07-22 07:16:20 -0700312
313 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
314 // random subsets.
315 // Do not attempt to decode subsets of an image of only once pixel, since there is no
316 // meaningful subset.
317 if (size.width() * size.height() == 1) {
318 return;
319 }
320
321 SkRandom rand;
322 SkIRect subset;
323 SkCodec::Options opts;
324 opts.fSubset = &subset;
325 for (int i = 0; i < 5; i++) {
326 subset = generate_random_subset(&rand, size.width(), size.height());
327 SkASSERT(!subset.isEmpty());
328 const bool supported = codec->getValidSubset(&subset);
329 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
330
331 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
332 SkBitmap bm;
333 bm.allocPixels(subsetInfo);
334 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700335 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700336
337 if (supportsSubsetDecoding) {
msarette6dd0042015-10-09 11:07:34 -0700338 REPORTER_ASSERT(r, result == expectedResult);
scroggob636b452015-07-22 07:16:20 -0700339 // Webp is the only codec that supports subsets, and it will have modified the subset
340 // to have even left/top.
341 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
342 } else {
343 // No subsets will work.
344 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
345 }
346 }
msarettcc7f3052015-10-05 14:20:27 -0700347
348 // SkScaledCodec tests
scroggo2c3b2182015-10-09 08:40:59 -0700349 if ((supportsScanlineDecoding || supportsSubsetDecoding) && supports_scaled_codec(path)) {
350
msarettcc7f3052015-10-05 14:20:27 -0700351 SkAutoTDelete<SkStream> stream(resource(path));
352 if (!stream) {
353 SkDebugf("Missing resource '%s'\n", path);
354 return;
355 }
msarette6dd0042015-10-09 11:07:34 -0700356
msarett3d9d7a72015-10-21 10:27:10 -0700357 SkAutoTDelete<SkAndroidCodec> codec(nullptr);
msarette6dd0042015-10-09 11:07:34 -0700358 if (isIncomplete) {
359 size_t size = stream->getLength();
360 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
msarett3d9d7a72015-10-21 10:27:10 -0700361 codec.reset(SkAndroidCodec::NewFromData(data));
msarette6dd0042015-10-09 11:07:34 -0700362 } else {
msarett3d9d7a72015-10-21 10:27:10 -0700363 codec.reset(SkAndroidCodec::NewFromStream(stream.detach()));
msarette6dd0042015-10-09 11:07:34 -0700364 }
msarettcc7f3052015-10-05 14:20:27 -0700365 if (!codec) {
366 ERRORF(r, "Unable to decode '%s'", path);
367 return;
368 }
369
370 SkBitmap bm;
371 SkMD5::Digest scaledCodecDigest;
scroggo27c17282015-10-27 08:14:46 -0700372 test_android_codec(r, codec, bm, info, size, expectedResult,
msarett3d9d7a72015-10-21 10:27:10 -0700373 &scaledCodecDigest, &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700374 }
375
376 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
377 if (isIncomplete) {
scroggo27c17282015-10-27 08:14:46 -0700378 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, false);
msarettcc7f3052015-10-05 14:20:27 -0700379 }
halcanarya096d7a2015-03-27 12:16:53 -0700380}
381
382DEF_TEST(Codec, r) {
383 // WBMP
msarett99f567e2015-08-05 12:58:26 -0700384 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700385
scroggo6f5e6192015-06-18 12:53:43 -0700386 // WEBP
scroggob636b452015-07-22 07:16:20 -0700387 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true);
388 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true);
389 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true);
scroggo6f5e6192015-06-18 12:53:43 -0700390
halcanarya096d7a2015-03-27 12:16:53 -0700391 // BMP
msarett5406d6f2015-08-31 06:55:13 -0700392 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700393
394 // ICO
msarette6dd0042015-10-09 11:07:34 -0700395 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700396 // These two tests examine interestingly different behavior:
397 // Decodes an embedded BMP image
scroggo27c17282015-10-27 08:14:46 -0700398 check(r, "color_wheel.ico", SkISize::Make(128, 128), false, false, false);
msarett68b204e2015-04-01 12:09:21 -0700399 // Decodes an embedded PNG image
scroggo27c17282015-10-27 08:14:46 -0700400 check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700401
msarett438b2ad2015-04-09 12:43:10 -0700402 // GIF
msarette6dd0042015-10-09 11:07:34 -0700403 // FIXME: We are not ready to test incomplete GIFs
scroggo27c17282015-10-27 08:14:46 -0700404 check(r, "box.gif", SkISize::Make(200, 55), true, false, false);
405 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700406 // randPixels.gif is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700407 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, false);
msarett438b2ad2015-04-09 12:43:10 -0700408
msarette16b04a2015-04-15 07:32:19 -0700409 // JPG
scroggo27c17282015-10-27 08:14:46 -0700410 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false);
scroggob636b452015-07-22 07:16:20 -0700411 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
msarette6dd0042015-10-09 11:07:34 -0700412 // grayscale.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700413 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, false);
scroggob636b452015-07-22 07:16:20 -0700414 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
msarette6dd0042015-10-09 11:07:34 -0700415 // randPixels.jpg is too small to test incomplete
scroggo27c17282015-10-27 08:14:46 -0700416 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, false);
msarette16b04a2015-04-15 07:32:19 -0700417
halcanarya096d7a2015-03-27 12:16:53 -0700418 // PNG
scroggo27c17282015-10-27 08:14:46 -0700419 check(r, "arrow.png", SkISize::Make(187, 312), true, false, false);
420 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false, false);
421 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false, false);
422 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false, false);
423 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false, false);
424 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false, false);
425 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false, false);
426 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false, false);
427 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false, false);
428 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false, false);
429 check(r, "plane.png", SkISize::Make(250, 126), true, false, false);
msarette6dd0042015-10-09 11:07:34 -0700430 // FIXME: We are not ready to test incomplete interlaced pngs
scroggo27c17282015-10-27 08:14:46 -0700431 check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false, false);
432 check(r, "randPixels.png", SkISize::Make(8, 8), true, false, false);
433 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700434}
scroggo0a7e69c2015-04-03 07:22:22 -0700435
scroggo46c57472015-09-30 08:57:13 -0700436// Test interlaced PNG in stripes, similar to DM's kStripe_Mode
437DEF_TEST(Codec_stripes, r) {
438 const char * path = "plane_interlaced.png";
439 SkAutoTDelete<SkStream> stream(resource(path));
440 if (!stream) {
441 SkDebugf("Missing resource '%s'\n", path);
442 }
443
444 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
445 REPORTER_ASSERT(r, codec);
446
447 if (!codec) {
448 return;
449 }
450
451 switch (codec->getScanlineOrder()) {
452 case SkCodec::kBottomUp_SkScanlineOrder:
453 case SkCodec::kOutOfOrder_SkScanlineOrder:
454 ERRORF(r, "This scanline order will not match the original.");
455 return;
456 default:
457 break;
458 }
459
460 // Baseline for what the image should look like, using N32.
461 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
462
463 SkBitmap bm;
464 bm.allocPixels(info);
465 SkAutoLockPixels autoLockPixels(bm);
466 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
467 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
468
469 SkMD5::Digest digest;
470 md5(bm, &digest);
471
472 // Now decode in stripes
473 const int height = info.height();
474 const int numStripes = 4;
475 int stripeHeight;
476 int remainingLines;
477 SkTDivMod(height, numStripes, &stripeHeight, &remainingLines);
478
479 bm.eraseColor(SK_ColorYELLOW);
480
481 result = codec->startScanlineDecode(info);
482 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
483
484 // Odd stripes
485 for (int i = 1; i < numStripes; i += 2) {
486 // Skip the even stripes
msarette6dd0042015-10-09 11:07:34 -0700487 bool skipResult = codec->skipScanlines(stripeHeight);
488 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700489
msarette6dd0042015-10-09 11:07:34 -0700490 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700491 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700492 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700493 }
494
495 // Even stripes
496 result = codec->startScanlineDecode(info);
497 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
498
499 for (int i = 0; i < numStripes; i += 2) {
msarette6dd0042015-10-09 11:07:34 -0700500 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700501 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700502 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700503
504 // Skip the odd stripes
505 if (i + 1 < numStripes) {
msarette6dd0042015-10-09 11:07:34 -0700506 bool skipResult = codec->skipScanlines(stripeHeight);
507 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700508 }
509 }
510
511 // Remainder at the end
512 if (remainingLines > 0) {
513 result = codec->startScanlineDecode(info);
514 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
515
msarette6dd0042015-10-09 11:07:34 -0700516 bool skipResult = codec->skipScanlines(height - remainingLines);
517 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700518
msarette6dd0042015-10-09 11:07:34 -0700519 int linesDecoded = codec->getScanlines(bm.getAddr(0, height - remainingLines),
scroggo46c57472015-09-30 08:57:13 -0700520 remainingLines, bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700521 REPORTER_ASSERT(r, linesDecoded == remainingLines);
scroggo46c57472015-09-30 08:57:13 -0700522 }
523
524 compare_to_good_digest(r, digest, bm);
525}
526
scroggo0a7e69c2015-04-03 07:22:22 -0700527static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700528 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700529 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700530 REPORTER_ASSERT(r, !codec);
531
msarett3d9d7a72015-10-21 10:27:10 -0700532 SkAndroidCodec* androidCodec =
533 SkAndroidCodec::NewFromStream(new SkMemoryStream(stream, len, false));
534 REPORTER_ASSERT(r, !androidCodec);
scroggo0a7e69c2015-04-03 07:22:22 -0700535}
536
537// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
538// even on failure. Test some bad streams.
539DEF_TEST(Codec_leaks, r) {
540 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
541 const char nonSupportedStream[] = "hello world";
542 // The other strings should look like the beginning of a file type, so we'll call some
543 // internal version of NewFromStream, which must also delete the stream on failure.
544 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
545 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
546 const char emptyWebp[] = "RIFF1234WEBPVP";
547 const char emptyBmp[] = { 'B', 'M' };
548 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
549 const char emptyGif[] = "GIFVER";
550
551 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
552 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
553 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
554 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
555 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
556 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
557 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
558}
msarette16b04a2015-04-15 07:32:19 -0700559
scroggo2c3b2182015-10-09 08:40:59 -0700560DEF_TEST(Codec_null, r) {
561 // Attempting to create an SkCodec or an SkScaledCodec with null should not
562 // crash.
563 SkCodec* codec = SkCodec::NewFromStream(nullptr);
564 REPORTER_ASSERT(r, !codec);
565
msarett3d9d7a72015-10-21 10:27:10 -0700566 SkAndroidCodec* androidCodec = SkAndroidCodec::NewFromStream(nullptr);
567 REPORTER_ASSERT(r, !androidCodec);
scroggo2c3b2182015-10-09 08:40:59 -0700568}
569
msarette16b04a2015-04-15 07:32:19 -0700570static void test_dimensions(skiatest::Reporter* r, const char path[]) {
571 // Create the codec from the resource file
572 SkAutoTDelete<SkStream> stream(resource(path));
573 if (!stream) {
574 SkDebugf("Missing resource '%s'\n", path);
575 return;
576 }
msarett3d9d7a72015-10-21 10:27:10 -0700577 SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.detach()));
msarette16b04a2015-04-15 07:32:19 -0700578 if (!codec) {
579 ERRORF(r, "Unable to create codec '%s'", path);
580 return;
581 }
582
583 // Check that the decode is successful for a variety of scales
msarett3d9d7a72015-10-21 10:27:10 -0700584 for (int sampleSize = 1; sampleSize < 10; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700585 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700586 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700587 SkImageInfo scaledInfo = codec->getInfo()
588 .makeWH(scaledDims.width(), scaledDims.height())
589 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700590
591 // Set up for the decode
592 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
593 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
594 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
595
msarett3d9d7a72015-10-21 10:27:10 -0700596 SkAndroidCodec::AndroidOptions options;
597 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700598 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700599 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
scroggoeb602a52015-07-09 08:16:03 -0700600 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700601 }
602}
603
604// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
605DEF_TEST(Codec_Dimensions, r) {
606 // JPG
607 test_dimensions(r, "CMYK.jpg");
608 test_dimensions(r, "color_wheel.jpg");
609 test_dimensions(r, "grayscale.jpg");
610 test_dimensions(r, "mandrill_512_q075.jpg");
611 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700612
613 // Decoding small images with very large scaling factors is a potential
614 // source of bugs and crashes. We disable these tests in Gold because
615 // tiny images are not very useful to look at.
616 // Here we make sure that we do not crash or access illegal memory when
617 // performing scaled decodes on small images.
618 test_dimensions(r, "1x1.png");
619 test_dimensions(r, "2x2.png");
620 test_dimensions(r, "3x3.png");
621 test_dimensions(r, "3x1.png");
622 test_dimensions(r, "1x1.png");
623 test_dimensions(r, "16x1.png");
624 test_dimensions(r, "1x16.png");
625 test_dimensions(r, "mandrill_16.png");
626
msarette16b04a2015-04-15 07:32:19 -0700627}
628
msarettd0375bc2015-08-12 08:08:56 -0700629static void test_invalid(skiatest::Reporter* r, const char path[]) {
msarett4b17fa32015-04-23 08:53:39 -0700630 SkAutoTDelete<SkStream> stream(resource(path));
631 if (!stream) {
632 SkDebugf("Missing resource '%s'\n", path);
633 return;
634 }
635 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700636 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700637}
msarette16b04a2015-04-15 07:32:19 -0700638
msarett4b17fa32015-04-23 08:53:39 -0700639DEF_TEST(Codec_Empty, r) {
640 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700641 test_invalid(r, "empty_images/zero-dims.gif");
642 test_invalid(r, "empty_images/zero-embedded.ico");
643 test_invalid(r, "empty_images/zero-width.bmp");
644 test_invalid(r, "empty_images/zero-height.bmp");
645 test_invalid(r, "empty_images/zero-width.jpg");
646 test_invalid(r, "empty_images/zero-height.jpg");
647 test_invalid(r, "empty_images/zero-width.png");
648 test_invalid(r, "empty_images/zero-height.png");
649 test_invalid(r, "empty_images/zero-width.wbmp");
650 test_invalid(r, "empty_images/zero-height.wbmp");
651 // This image is an ico with an embedded mask-bmp. This is illegal.
652 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700653}
msarett99f567e2015-08-05 12:58:26 -0700654
655static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
656 SkAutoTDelete<SkStream> stream(resource(path));
657 if (!stream) {
658 SkDebugf("Missing resource '%s'\n", path);
659 return;
660 }
scroggo46c57472015-09-30 08:57:13 -0700661 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.detach()));
msarett99f567e2015-08-05 12:58:26 -0700662
663 // This should return kSuccess because kIndex8 is supported.
664 SkPMColor colorStorage[256];
665 int colorCount;
scroggo46c57472015-09-30 08:57:13 -0700666 SkCodec::Result result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700667 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
msarett99f567e2015-08-05 12:58:26 -0700668 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
669 // The rest of the test is uninteresting if kIndex8 is not supported
670 if (SkCodec::kSuccess != result) {
671 return;
672 }
673
674 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
675 // colorPtr and a valid colorCountPtr.
scroggo46c57472015-09-30 08:57:13 -0700676 result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700677 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
msarett99f567e2015-08-05 12:58:26 -0700678 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
scroggo46c57472015-09-30 08:57:13 -0700679 result = decoder->startScanlineDecode(
msarett99f567e2015-08-05 12:58:26 -0700680 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
681 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
682}
683
684DEF_TEST(Codec_Params, r) {
685 test_invalid_parameters(r, "index8.png");
686 test_invalid_parameters(r, "mandrill.wbmp");
687}