blob: 5ef1f8a86e02cb44f862f15e34484e2e85ccf1f8 [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"
9#include "SkBitmap.h"
10#include "SkCodec.h"
msarette6dd0042015-10-09 11:07:34 -070011#include "SkData.h"
halcanarya096d7a2015-03-27 12:16:53 -070012#include "SkMD5.h"
scroggob636b452015-07-22 07:16:20 -070013#include "SkRandom.h"
msarettb32758a2015-08-18 13:22:46 -070014#include "SkScaledCodec.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
scroggob636b452015-07-22 07:16:20 -070066SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
67 SkIRect rect;
68 do {
69 rect.fLeft = rand->nextRangeU(0, w);
70 rect.fTop = rand->nextRangeU(0, h);
71 rect.fRight = rand->nextRangeU(0, w);
72 rect.fBottom = rand->nextRangeU(0, h);
73 rect.sort();
74 } while (rect.isEmpty());
75 return rect;
76}
77
msarettcc7f3052015-10-05 14:20:27 -070078static void test_codec(skiatest::Reporter* r, SkCodec* codec, SkBitmap& bm, const SkImageInfo& info,
msarette6dd0042015-10-09 11:07:34 -070079 const SkISize& size, bool supports565, SkCodec::Result expectedResult,
80 SkMD5::Digest* digest, const SkMD5::Digest* goodDigest) {
81
halcanarya096d7a2015-03-27 12:16:53 -070082 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -070083 bm.allocPixels(info);
84 SkAutoLockPixels autoLockPixels(bm);
msarettcc7f3052015-10-05 14:20:27 -070085
86 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -070087 REPORTER_ASSERT(r, result == expectedResult);
halcanarya096d7a2015-03-27 12:16:53 -070088
msarettcc7f3052015-10-05 14:20:27 -070089 md5(bm, digest);
90 if (goodDigest) {
91 REPORTER_ASSERT(r, *digest == *goodDigest);
92 }
halcanarya096d7a2015-03-27 12:16:53 -070093
msarett8ff6ca62015-09-18 12:06:04 -070094 {
95 // Test decoding to 565
96 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
msarette6dd0042015-10-09 11:07:34 -070097 SkCodec::Result expected565 = (supports565 && info.alphaType() == kOpaque_SkAlphaType) ?
98 expectedResult : SkCodec::kInvalidConversion;
99 test_info(r, codec, info565, expected565, nullptr);
msarett8ff6ca62015-09-18 12:06:04 -0700100 }
101
102 // Verify that re-decoding gives the same result. It is interesting to check this after
103 // a decode to 565, since choosing to decode to 565 may result in some of the decode
104 // options being modified. These options should return to their defaults on another
105 // decode to kN32, so the new digest should match the old digest.
msarette6dd0042015-10-09 11:07:34 -0700106 test_info(r, codec, info, expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700107
108 {
109 // Check alpha type conversions
110 if (info.alphaType() == kOpaque_SkAlphaType) {
111 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700112 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700113 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700114 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700115 } else {
116 // Decoding to opaque should fail
117 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700118 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700119 SkAlphaType otherAt = info.alphaType();
120 if (kPremul_SkAlphaType == otherAt) {
121 otherAt = kUnpremul_SkAlphaType;
122 } else {
123 otherAt = kPremul_SkAlphaType;
124 }
125 // The other non-opaque alpha type should always succeed, but not match.
msarette6dd0042015-10-09 11:07:34 -0700126 test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700127 }
128 }
msarettcc7f3052015-10-05 14:20:27 -0700129}
130
scroggo2c3b2182015-10-09 08:40:59 -0700131// FIXME: SkScaledCodec is currently only supported for types used by BRD
132// skbug.com/4428
133static bool supports_scaled_codec(const char path[]) {
134 static const char* const exts[] = {
135 "jpg", "jpeg", "png", "webp"
136 "JPG", "JPEG", "PNG", "WEBP"
137 };
138
139 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
140 if (SkStrEndsWith(path, exts[i])) {
141 return true;
142 }
143 }
144 return false;
145}
146
msarettcc7f3052015-10-05 14:20:27 -0700147static void check(skiatest::Reporter* r,
148 const char path[],
149 SkISize size,
150 bool supportsScanlineDecoding,
151 bool supportsSubsetDecoding,
msarette6dd0042015-10-09 11:07:34 -0700152 bool supports565 = true,
153 bool supportsIncomplete = true) {
msarettcc7f3052015-10-05 14:20:27 -0700154
155 SkAutoTDelete<SkStream> stream(resource(path));
156 if (!stream) {
157 SkDebugf("Missing resource '%s'\n", path);
158 return;
159 }
msarette6dd0042015-10-09 11:07:34 -0700160
161 SkAutoTDelete<SkCodec> codec(nullptr);
162 bool isIncomplete = supportsIncomplete;
163 if (isIncomplete) {
164 size_t size = stream->getLength();
165 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
166 codec.reset(SkCodec::NewFromData(data));
167 } else {
168 codec.reset(SkCodec::NewFromStream(stream.detach()));
169 }
msarettcc7f3052015-10-05 14:20:27 -0700170 if (!codec) {
171 ERRORF(r, "Unable to decode '%s'", path);
172 return;
173 }
174
175 // Test full image decodes with SkCodec
176 SkMD5::Digest codecDigest;
177 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
178 SkBitmap bm;
msarette6dd0042015-10-09 11:07:34 -0700179 SkCodec::Result expectedResult = isIncomplete ? SkCodec::kIncompleteInput : SkCodec::kSuccess;
180 test_codec(r, codec, bm, info, size, supports565, expectedResult, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700181
182 // Scanline decoding follows.
msarettcc7f3052015-10-05 14:20:27 -0700183 // Need to call startScanlineDecode() first.
scroggo46c57472015-09-30 08:57:13 -0700184 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700185 == 0);
scroggo46c57472015-09-30 08:57:13 -0700186 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700187 == 0);
scroggo46c57472015-09-30 08:57:13 -0700188
189 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700190 if (supportsScanlineDecoding) {
191 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700192
scroggo46c57472015-09-30 08:57:13 -0700193 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700194
scroggo58421542015-04-01 11:25:20 -0700195 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700196 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
197 if (!isIncomplete) {
198 REPORTER_ASSERT(r, 1 == lines);
199 }
scroggo58421542015-04-01 11:25:20 -0700200 }
201 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700202 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700203 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700204 }
scroggo46c57472015-09-30 08:57:13 -0700205
206 // Cannot continue to decode scanlines beyond the end
207 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700208 == 0);
scroggo46c57472015-09-30 08:57:13 -0700209
210 // Interrupting a scanline decode with a full decode starts from
211 // scratch
212 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
msarette6dd0042015-10-09 11:07:34 -0700213 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
214 if (!isIncomplete) {
215 REPORTER_ASSERT(r, lines == 1);
216 }
scroggo46c57472015-09-30 08:57:13 -0700217 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
msarette6dd0042015-10-09 11:07:34 -0700218 == expectedResult);
scroggo46c57472015-09-30 08:57:13 -0700219 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700220 == 0);
scroggo46c57472015-09-30 08:57:13 -0700221 REPORTER_ASSERT(r, codec->skipScanlines(1)
msarette6dd0042015-10-09 11:07:34 -0700222 == 0);
msarett80803ff2015-10-16 10:54:12 -0700223
224 // Test partial scanline decodes
225 if (supports_scaled_codec(path) && info.width() >= 3) {
226 SkCodec::Options options;
227 int width = info.width();
228 int height = info.height();
229 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
230 options.fSubset = &subset;
231
232 const SkCodec::Result partialStartResult = codec->startScanlineDecode(info, &options,
233 nullptr, nullptr);
234 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
235
236 for (int y = 0; y < height; y++) {
237 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
238 if (!isIncomplete) {
239 REPORTER_ASSERT(r, 1 == lines);
240 }
241 }
242 }
scroggo58421542015-04-01 11:25:20 -0700243 } else {
scroggo46c57472015-09-30 08:57:13 -0700244 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700245 }
scroggob636b452015-07-22 07:16:20 -0700246
247 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
248 // random subsets.
249 // Do not attempt to decode subsets of an image of only once pixel, since there is no
250 // meaningful subset.
251 if (size.width() * size.height() == 1) {
252 return;
253 }
254
255 SkRandom rand;
256 SkIRect subset;
257 SkCodec::Options opts;
258 opts.fSubset = &subset;
259 for (int i = 0; i < 5; i++) {
260 subset = generate_random_subset(&rand, size.width(), size.height());
261 SkASSERT(!subset.isEmpty());
262 const bool supported = codec->getValidSubset(&subset);
263 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
264
265 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
266 SkBitmap bm;
267 bm.allocPixels(subsetInfo);
268 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700269 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700270
271 if (supportsSubsetDecoding) {
msarette6dd0042015-10-09 11:07:34 -0700272 REPORTER_ASSERT(r, result == expectedResult);
scroggob636b452015-07-22 07:16:20 -0700273 // Webp is the only codec that supports subsets, and it will have modified the subset
274 // to have even left/top.
275 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
276 } else {
277 // No subsets will work.
278 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
279 }
280 }
msarettcc7f3052015-10-05 14:20:27 -0700281
282 // SkScaledCodec tests
scroggo2c3b2182015-10-09 08:40:59 -0700283 if ((supportsScanlineDecoding || supportsSubsetDecoding) && supports_scaled_codec(path)) {
284
msarettcc7f3052015-10-05 14:20:27 -0700285 SkAutoTDelete<SkStream> stream(resource(path));
286 if (!stream) {
287 SkDebugf("Missing resource '%s'\n", path);
288 return;
289 }
msarette6dd0042015-10-09 11:07:34 -0700290
291 SkAutoTDelete<SkCodec> codec(nullptr);
292 if (isIncomplete) {
293 size_t size = stream->getLength();
294 SkAutoTUnref<SkData> data((SkData::NewFromStream(stream, 2 * size / 3)));
295 codec.reset(SkScaledCodec::NewFromData(data));
296 } else {
297 codec.reset(SkScaledCodec::NewFromStream(stream.detach()));
298 }
msarettcc7f3052015-10-05 14:20:27 -0700299 if (!codec) {
300 ERRORF(r, "Unable to decode '%s'", path);
301 return;
302 }
303
304 SkBitmap bm;
305 SkMD5::Digest scaledCodecDigest;
msarette6dd0042015-10-09 11:07:34 -0700306 test_codec(r, codec, bm, info, size, supports565, expectedResult, &scaledCodecDigest,
307 &codecDigest);
308 }
309
310 // If we've just tested incomplete decodes, let's run the same test again on full decodes.
311 if (isIncomplete) {
312 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding, supports565, false);
msarettcc7f3052015-10-05 14:20:27 -0700313 }
halcanarya096d7a2015-03-27 12:16:53 -0700314}
315
316DEF_TEST(Codec, r) {
317 // WBMP
msarett99f567e2015-08-05 12:58:26 -0700318 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700319
scroggo6f5e6192015-06-18 12:53:43 -0700320 // WEBP
scroggob636b452015-07-22 07:16:20 -0700321 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true);
322 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true);
323 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true);
scroggo6f5e6192015-06-18 12:53:43 -0700324
halcanarya096d7a2015-03-27 12:16:53 -0700325 // BMP
msarett5406d6f2015-08-31 06:55:13 -0700326 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700327
328 // ICO
msarette6dd0042015-10-09 11:07:34 -0700329 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700330 // These two tests examine interestingly different behavior:
331 // Decodes an embedded BMP image
msarette6dd0042015-10-09 11:07:34 -0700332 check(r, "color_wheel.ico", SkISize::Make(128, 128), false, false, true, false);
msarett68b204e2015-04-01 12:09:21 -0700333 // Decodes an embedded PNG image
msarette6dd0042015-10-09 11:07:34 -0700334 check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false, true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700335
msarett438b2ad2015-04-09 12:43:10 -0700336 // GIF
msarette6dd0042015-10-09 11:07:34 -0700337 // FIXME: We are not ready to test incomplete GIFs
338 check(r, "box.gif", SkISize::Make(200, 55), true, false, true, false);
339 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false, true, false);
340 // randPixels.gif is too small to test incomplete
341 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false, true, false);
msarett438b2ad2015-04-09 12:43:10 -0700342
msarette16b04a2015-04-15 07:32:19 -0700343 // JPG
scroggocc2feb12015-08-14 08:32:46 -0700344 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false, false);
scroggob636b452015-07-22 07:16:20 -0700345 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
msarette6dd0042015-10-09 11:07:34 -0700346 // grayscale.jpg is too small to test incomplete
347 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false, true, false);
scroggob636b452015-07-22 07:16:20 -0700348 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
msarette6dd0042015-10-09 11:07:34 -0700349 // randPixels.jpg is too small to test incomplete
350 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false, true, false);
msarette16b04a2015-04-15 07:32:19 -0700351
halcanarya096d7a2015-03-27 12:16:53 -0700352 // PNG
msarette6dd0042015-10-09 11:07:34 -0700353 check(r, "arrow.png", SkISize::Make(187, 312), true, false, true, false);
354 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false, true, false);
355 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false, true, false);
356 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false, true, false);
357 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false, true, false);
358 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false, true, false);
359 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false, true, false);
360 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false, true, false);
361 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false, true, false);
362 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false, true, false);
363 check(r, "plane.png", SkISize::Make(250, 126), true, false, true, false);
364 // FIXME: We are not ready to test incomplete interlaced pngs
365 check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false, true, false);
366 check(r, "randPixels.png", SkISize::Make(8, 8), true, false, true, false);
367 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false, true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700368}
scroggo0a7e69c2015-04-03 07:22:22 -0700369
scroggo46c57472015-09-30 08:57:13 -0700370// Test interlaced PNG in stripes, similar to DM's kStripe_Mode
371DEF_TEST(Codec_stripes, r) {
372 const char * path = "plane_interlaced.png";
373 SkAutoTDelete<SkStream> stream(resource(path));
374 if (!stream) {
375 SkDebugf("Missing resource '%s'\n", path);
376 }
377
378 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
379 REPORTER_ASSERT(r, codec);
380
381 if (!codec) {
382 return;
383 }
384
385 switch (codec->getScanlineOrder()) {
386 case SkCodec::kBottomUp_SkScanlineOrder:
387 case SkCodec::kOutOfOrder_SkScanlineOrder:
388 ERRORF(r, "This scanline order will not match the original.");
389 return;
390 default:
391 break;
392 }
393
394 // Baseline for what the image should look like, using N32.
395 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
396
397 SkBitmap bm;
398 bm.allocPixels(info);
399 SkAutoLockPixels autoLockPixels(bm);
400 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
401 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
402
403 SkMD5::Digest digest;
404 md5(bm, &digest);
405
406 // Now decode in stripes
407 const int height = info.height();
408 const int numStripes = 4;
409 int stripeHeight;
410 int remainingLines;
411 SkTDivMod(height, numStripes, &stripeHeight, &remainingLines);
412
413 bm.eraseColor(SK_ColorYELLOW);
414
415 result = codec->startScanlineDecode(info);
416 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
417
418 // Odd stripes
419 for (int i = 1; i < numStripes; i += 2) {
420 // Skip the even stripes
msarette6dd0042015-10-09 11:07:34 -0700421 bool skipResult = codec->skipScanlines(stripeHeight);
422 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700423
msarette6dd0042015-10-09 11:07:34 -0700424 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700425 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700426 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700427 }
428
429 // Even stripes
430 result = codec->startScanlineDecode(info);
431 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
432
433 for (int i = 0; i < numStripes; i += 2) {
msarette6dd0042015-10-09 11:07:34 -0700434 int linesDecoded = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
scroggo46c57472015-09-30 08:57:13 -0700435 bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700436 REPORTER_ASSERT(r, linesDecoded == stripeHeight);
scroggo46c57472015-09-30 08:57:13 -0700437
438 // Skip the odd stripes
439 if (i + 1 < numStripes) {
msarette6dd0042015-10-09 11:07:34 -0700440 bool skipResult = codec->skipScanlines(stripeHeight);
441 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700442 }
443 }
444
445 // Remainder at the end
446 if (remainingLines > 0) {
447 result = codec->startScanlineDecode(info);
448 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
449
msarette6dd0042015-10-09 11:07:34 -0700450 bool skipResult = codec->skipScanlines(height - remainingLines);
451 REPORTER_ASSERT(r, skipResult);
scroggo46c57472015-09-30 08:57:13 -0700452
msarette6dd0042015-10-09 11:07:34 -0700453 int linesDecoded = codec->getScanlines(bm.getAddr(0, height - remainingLines),
scroggo46c57472015-09-30 08:57:13 -0700454 remainingLines, bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700455 REPORTER_ASSERT(r, linesDecoded == remainingLines);
scroggo46c57472015-09-30 08:57:13 -0700456 }
457
458 compare_to_good_digest(r, digest, bm);
459}
460
scroggo0a7e69c2015-04-03 07:22:22 -0700461static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700462 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700463 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700464 REPORTER_ASSERT(r, !codec);
465
466 codec = SkScaledCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo0a7e69c2015-04-03 07:22:22 -0700467 REPORTER_ASSERT(r, !codec);
468}
469
470// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
471// even on failure. Test some bad streams.
472DEF_TEST(Codec_leaks, r) {
473 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
474 const char nonSupportedStream[] = "hello world";
475 // The other strings should look like the beginning of a file type, so we'll call some
476 // internal version of NewFromStream, which must also delete the stream on failure.
477 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
478 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
479 const char emptyWebp[] = "RIFF1234WEBPVP";
480 const char emptyBmp[] = { 'B', 'M' };
481 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
482 const char emptyGif[] = "GIFVER";
483
484 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
485 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
486 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
487 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
488 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
489 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
490 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
491}
msarette16b04a2015-04-15 07:32:19 -0700492
scroggo2c3b2182015-10-09 08:40:59 -0700493DEF_TEST(Codec_null, r) {
494 // Attempting to create an SkCodec or an SkScaledCodec with null should not
495 // crash.
496 SkCodec* codec = SkCodec::NewFromStream(nullptr);
497 REPORTER_ASSERT(r, !codec);
498
499 codec = SkScaledCodec::NewFromStream(nullptr);
500 REPORTER_ASSERT(r, !codec);
501}
502
msarette16b04a2015-04-15 07:32:19 -0700503static void test_dimensions(skiatest::Reporter* r, const char path[]) {
504 // Create the codec from the resource file
505 SkAutoTDelete<SkStream> stream(resource(path));
506 if (!stream) {
507 SkDebugf("Missing resource '%s'\n", path);
508 return;
509 }
msarettb32758a2015-08-18 13:22:46 -0700510 SkAutoTDelete<SkCodec> codec(SkScaledCodec::NewFromStream(stream.detach()));
msarette16b04a2015-04-15 07:32:19 -0700511 if (!codec) {
512 ERRORF(r, "Unable to create codec '%s'", path);
513 return;
514 }
515
516 // Check that the decode is successful for a variety of scales
msarettb32758a2015-08-18 13:22:46 -0700517 for (float scale = 0.05f; scale < 2.0f; scale += 0.05f) {
msarette16b04a2015-04-15 07:32:19 -0700518 // Scale the output dimensions
519 SkISize scaledDims = codec->getScaledDimensions(scale);
msarettb32758a2015-08-18 13:22:46 -0700520 SkImageInfo scaledInfo = codec->getInfo()
521 .makeWH(scaledDims.width(), scaledDims.height())
522 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700523
524 // Set up for the decode
525 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
526 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
527 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
528
scroggoeb602a52015-07-09 08:16:03 -0700529 SkCodec::Result result =
halcanary96fcdcc2015-08-27 07:41:13 -0700530 codec->getPixels(scaledInfo, pixels.get(), rowBytes, nullptr, nullptr, nullptr);
scroggoeb602a52015-07-09 08:16:03 -0700531 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700532 }
533}
534
535// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
536DEF_TEST(Codec_Dimensions, r) {
537 // JPG
538 test_dimensions(r, "CMYK.jpg");
539 test_dimensions(r, "color_wheel.jpg");
540 test_dimensions(r, "grayscale.jpg");
541 test_dimensions(r, "mandrill_512_q075.jpg");
542 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700543
544 // Decoding small images with very large scaling factors is a potential
545 // source of bugs and crashes. We disable these tests in Gold because
546 // tiny images are not very useful to look at.
547 // Here we make sure that we do not crash or access illegal memory when
548 // performing scaled decodes on small images.
549 test_dimensions(r, "1x1.png");
550 test_dimensions(r, "2x2.png");
551 test_dimensions(r, "3x3.png");
552 test_dimensions(r, "3x1.png");
553 test_dimensions(r, "1x1.png");
554 test_dimensions(r, "16x1.png");
555 test_dimensions(r, "1x16.png");
556 test_dimensions(r, "mandrill_16.png");
557
msarette16b04a2015-04-15 07:32:19 -0700558}
559
msarettd0375bc2015-08-12 08:08:56 -0700560static void test_invalid(skiatest::Reporter* r, const char path[]) {
msarett4b17fa32015-04-23 08:53:39 -0700561 SkAutoTDelete<SkStream> stream(resource(path));
562 if (!stream) {
563 SkDebugf("Missing resource '%s'\n", path);
564 return;
565 }
566 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700567 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700568}
msarette16b04a2015-04-15 07:32:19 -0700569
msarett4b17fa32015-04-23 08:53:39 -0700570DEF_TEST(Codec_Empty, r) {
571 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700572 test_invalid(r, "empty_images/zero-dims.gif");
573 test_invalid(r, "empty_images/zero-embedded.ico");
574 test_invalid(r, "empty_images/zero-width.bmp");
575 test_invalid(r, "empty_images/zero-height.bmp");
576 test_invalid(r, "empty_images/zero-width.jpg");
577 test_invalid(r, "empty_images/zero-height.jpg");
578 test_invalid(r, "empty_images/zero-width.png");
579 test_invalid(r, "empty_images/zero-height.png");
580 test_invalid(r, "empty_images/zero-width.wbmp");
581 test_invalid(r, "empty_images/zero-height.wbmp");
582 // This image is an ico with an embedded mask-bmp. This is illegal.
583 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700584}
msarett99f567e2015-08-05 12:58:26 -0700585
586static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
587 SkAutoTDelete<SkStream> stream(resource(path));
588 if (!stream) {
589 SkDebugf("Missing resource '%s'\n", path);
590 return;
591 }
scroggo46c57472015-09-30 08:57:13 -0700592 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.detach()));
msarett99f567e2015-08-05 12:58:26 -0700593
594 // This should return kSuccess because kIndex8 is supported.
595 SkPMColor colorStorage[256];
596 int colorCount;
scroggo46c57472015-09-30 08:57:13 -0700597 SkCodec::Result result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700598 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
msarett99f567e2015-08-05 12:58:26 -0700599 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
600 // The rest of the test is uninteresting if kIndex8 is not supported
601 if (SkCodec::kSuccess != result) {
602 return;
603 }
604
605 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
606 // colorPtr and a valid colorCountPtr.
scroggo46c57472015-09-30 08:57:13 -0700607 result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700608 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
msarett99f567e2015-08-05 12:58:26 -0700609 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
scroggo46c57472015-09-30 08:57:13 -0700610 result = decoder->startScanlineDecode(
msarett99f567e2015-08-05 12:58:26 -0700611 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
612 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
613}
614
615DEF_TEST(Codec_Params, r) {
616 test_invalid_parameters(r, "index8.png");
617 test_invalid_parameters(r, "mandrill.wbmp");
618}