blob: 12b42e3ebbeb0c51f07386039bb909db3adfe90e [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"
11#include "SkMD5.h"
scroggob636b452015-07-22 07:16:20 -070012#include "SkRandom.h"
msarettb32758a2015-08-18 13:22:46 -070013#include "SkScaledCodec.h"
halcanarya096d7a2015-03-27 12:16:53 -070014#include "Test.h"
15
16static SkStreamAsset* resource(const char path[]) {
17 SkString fullPath = GetResourcePath(path);
18 return SkStream::NewFromFile(fullPath.c_str());
19}
20
21static void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
22 SkAutoLockPixels autoLockPixels(bm);
23 SkASSERT(bm.getPixels());
24 SkMD5 md5;
25 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
26 for (int y = 0; y < bm.height(); ++y) {
27 md5.update(static_cast<uint8_t*>(bm.getAddr(0, y)), rowLen);
28 }
29 md5.finish(*digest);
30}
31
scroggo9b2cdbf42015-07-10 12:07:02 -070032/**
33 * Compute the digest for bm and compare it to a known good digest.
34 * @param r Reporter to assert that bm's digest matches goodDigest.
35 * @param goodDigest The known good digest to compare to.
36 * @param bm The bitmap to test.
37 */
38static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
39 const SkBitmap& bm) {
40 SkMD5::Digest digest;
41 md5(bm, &digest);
42 REPORTER_ASSERT(r, digest == goodDigest);
43}
44
scroggod1bc5742015-08-12 08:31:44 -070045/**
46 * Test decoding an SkCodec to a particular SkImageInfo.
47 *
halcanary96fcdcc2015-08-27 07:41:13 -070048 * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr,
scroggod1bc5742015-08-12 08:31:44 -070049 * the resulting decode should match.
50 */
51static void test_info(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
52 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
53 SkBitmap bm;
54 bm.allocPixels(info);
55 SkAutoLockPixels autoLockPixels(bm);
56
57 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
58 REPORTER_ASSERT(r, result == expectedResult);
59
60 if (goodDigest) {
61 compare_to_good_digest(r, *goodDigest, bm);
62 }
63}
64
scroggob636b452015-07-22 07:16:20 -070065SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
66 SkIRect rect;
67 do {
68 rect.fLeft = rand->nextRangeU(0, w);
69 rect.fTop = rand->nextRangeU(0, h);
70 rect.fRight = rand->nextRangeU(0, w);
71 rect.fBottom = rand->nextRangeU(0, h);
72 rect.sort();
73 } while (rect.isEmpty());
74 return rect;
75}
76
msarettcc7f3052015-10-05 14:20:27 -070077static void test_codec(skiatest::Reporter* r, SkCodec* codec, SkBitmap& bm, const SkImageInfo& info,
78 const SkISize& size, bool supports565, SkMD5::Digest* digest,
79 const SkMD5::Digest* goodDigest) {
halcanarya096d7a2015-03-27 12:16:53 -070080 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -070081 bm.allocPixels(info);
82 SkAutoLockPixels autoLockPixels(bm);
msarettcc7f3052015-10-05 14:20:27 -070083
84 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
scroggoeb602a52015-07-09 08:16:03 -070085 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
halcanarya096d7a2015-03-27 12:16:53 -070086
msarettcc7f3052015-10-05 14:20:27 -070087 md5(bm, digest);
88 if (goodDigest) {
89 REPORTER_ASSERT(r, *digest == *goodDigest);
90 }
halcanarya096d7a2015-03-27 12:16:53 -070091
msarett8ff6ca62015-09-18 12:06:04 -070092 {
93 // Test decoding to 565
94 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
95 SkCodec::Result expected = (supports565 && info.alphaType() == kOpaque_SkAlphaType) ?
96 SkCodec::kSuccess : SkCodec::kInvalidConversion;
97 test_info(r, codec, info565, expected, nullptr);
98 }
99
100 // Verify that re-decoding gives the same result. It is interesting to check this after
101 // a decode to 565, since choosing to decode to 565 may result in some of the decode
102 // options being modified. These options should return to their defaults on another
103 // decode to kN32, so the new digest should match the old digest.
msarettcc7f3052015-10-05 14:20:27 -0700104 test_info(r, codec, info, SkCodec::kSuccess, digest);
scroggod1bc5742015-08-12 08:31:44 -0700105
106 {
107 // Check alpha type conversions
108 if (info.alphaType() == kOpaque_SkAlphaType) {
109 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700110 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700111 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700112 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700113 } else {
114 // Decoding to opaque should fail
115 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700116 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700117 SkAlphaType otherAt = info.alphaType();
118 if (kPremul_SkAlphaType == otherAt) {
119 otherAt = kUnpremul_SkAlphaType;
120 } else {
121 otherAt = kPremul_SkAlphaType;
122 }
123 // The other non-opaque alpha type should always succeed, but not match.
halcanary96fcdcc2015-08-27 07:41:13 -0700124 test_info(r, codec, info.makeAlphaType(otherAt), SkCodec::kSuccess, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700125 }
126 }
msarettcc7f3052015-10-05 14:20:27 -0700127}
128
scroggo2c3b2182015-10-09 08:40:59 -0700129// FIXME: SkScaledCodec is currently only supported for types used by BRD
130// skbug.com/4428
131static bool supports_scaled_codec(const char path[]) {
132 static const char* const exts[] = {
133 "jpg", "jpeg", "png", "webp"
134 "JPG", "JPEG", "PNG", "WEBP"
135 };
136
137 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
138 if (SkStrEndsWith(path, exts[i])) {
139 return true;
140 }
141 }
142 return false;
143}
144
msarettcc7f3052015-10-05 14:20:27 -0700145static void check(skiatest::Reporter* r,
146 const char path[],
147 SkISize size,
148 bool supportsScanlineDecoding,
149 bool supportsSubsetDecoding,
150 bool supports565 = true) {
151
152 SkAutoTDelete<SkStream> stream(resource(path));
153 if (!stream) {
154 SkDebugf("Missing resource '%s'\n", path);
155 return;
156 }
157 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
158 if (!codec) {
159 ERRORF(r, "Unable to decode '%s'", path);
160 return;
161 }
162
163 // Test full image decodes with SkCodec
164 SkMD5::Digest codecDigest;
165 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
166 SkBitmap bm;
167 test_codec(r, codec, bm, info, size, supports565, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700168
169 // Scanline decoding follows.
msarettcc7f3052015-10-05 14:20:27 -0700170 // Need to call startScanlineDecode() first.
scroggo46c57472015-09-30 08:57:13 -0700171 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
172 == SkCodec::kScanlineDecodingNotStarted);
173 REPORTER_ASSERT(r, codec->skipScanlines(1)
174 == SkCodec::kScanlineDecodingNotStarted);
175
176 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700177 if (supportsScanlineDecoding) {
178 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700179
scroggo46c57472015-09-30 08:57:13 -0700180 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700181
scroggo58421542015-04-01 11:25:20 -0700182 for (int y = 0; y < info.height(); y++) {
msarettcc7f3052015-10-05 14:20:27 -0700183 SkCodec::Result result = codec->getScanlines(bm.getAddr(0, y), 1, 0);
scroggoeb602a52015-07-09 08:16:03 -0700184 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
scroggo58421542015-04-01 11:25:20 -0700185 }
186 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700187 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700188 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700189 }
scroggo46c57472015-09-30 08:57:13 -0700190
191 // Cannot continue to decode scanlines beyond the end
192 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
193 == SkCodec::kInvalidParameters);
194
195 // Interrupting a scanline decode with a full decode starts from
196 // scratch
197 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
198 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
199 == SkCodec::kSuccess);
200 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
201 == SkCodec::kSuccess);
202 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
203 == SkCodec::kScanlineDecodingNotStarted);
204 REPORTER_ASSERT(r, codec->skipScanlines(1)
205 == SkCodec::kScanlineDecodingNotStarted);
scroggo58421542015-04-01 11:25:20 -0700206 } else {
scroggo46c57472015-09-30 08:57:13 -0700207 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700208 }
scroggob636b452015-07-22 07:16:20 -0700209
210 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
211 // random subsets.
212 // Do not attempt to decode subsets of an image of only once pixel, since there is no
213 // meaningful subset.
214 if (size.width() * size.height() == 1) {
215 return;
216 }
217
218 SkRandom rand;
219 SkIRect subset;
220 SkCodec::Options opts;
221 opts.fSubset = &subset;
222 for (int i = 0; i < 5; i++) {
223 subset = generate_random_subset(&rand, size.width(), size.height());
224 SkASSERT(!subset.isEmpty());
225 const bool supported = codec->getValidSubset(&subset);
226 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
227
228 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
229 SkBitmap bm;
230 bm.allocPixels(subsetInfo);
231 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700232 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700233
234 if (supportsSubsetDecoding) {
235 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
236 // Webp is the only codec that supports subsets, and it will have modified the subset
237 // to have even left/top.
238 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
239 } else {
240 // No subsets will work.
241 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
242 }
243 }
msarettcc7f3052015-10-05 14:20:27 -0700244
245 // SkScaledCodec tests
scroggo2c3b2182015-10-09 08:40:59 -0700246 if ((supportsScanlineDecoding || supportsSubsetDecoding) && supports_scaled_codec(path)) {
247
msarettcc7f3052015-10-05 14:20:27 -0700248 SkAutoTDelete<SkStream> stream(resource(path));
249 if (!stream) {
250 SkDebugf("Missing resource '%s'\n", path);
251 return;
252 }
253 SkAutoTDelete<SkCodec> codec(SkScaledCodec::NewFromStream(stream.detach()));
254 if (!codec) {
255 ERRORF(r, "Unable to decode '%s'", path);
256 return;
257 }
258
259 SkBitmap bm;
260 SkMD5::Digest scaledCodecDigest;
261 test_codec(r, codec, bm, info, size, supports565, &scaledCodecDigest, &codecDigest);
262 }
halcanarya096d7a2015-03-27 12:16:53 -0700263}
264
265DEF_TEST(Codec, r) {
266 // WBMP
msarett99f567e2015-08-05 12:58:26 -0700267 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700268
scroggo6f5e6192015-06-18 12:53:43 -0700269 // WEBP
scroggob636b452015-07-22 07:16:20 -0700270 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true);
271 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true);
272 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true);
scroggo6f5e6192015-06-18 12:53:43 -0700273
halcanarya096d7a2015-03-27 12:16:53 -0700274 // BMP
msarett5406d6f2015-08-31 06:55:13 -0700275 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700276
277 // ICO
msarett68b204e2015-04-01 12:09:21 -0700278 // These two tests examine interestingly different behavior:
279 // Decodes an embedded BMP image
scroggob636b452015-07-22 07:16:20 -0700280 check(r, "color_wheel.ico", SkISize::Make(128, 128), false, false);
msarett68b204e2015-04-01 12:09:21 -0700281 // Decodes an embedded PNG image
scroggob636b452015-07-22 07:16:20 -0700282 check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700283
msarett438b2ad2015-04-09 12:43:10 -0700284 // GIF
msarett10522ff2015-09-07 08:54:01 -0700285 check(r, "box.gif", SkISize::Make(200, 55), true, false);
286 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false);
287 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false);
msarett438b2ad2015-04-09 12:43:10 -0700288
msarette16b04a2015-04-15 07:32:19 -0700289 // JPG
scroggocc2feb12015-08-14 08:32:46 -0700290 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false, false);
scroggob636b452015-07-22 07:16:20 -0700291 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
292 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false);
293 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
294 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false);
msarette16b04a2015-04-15 07:32:19 -0700295
halcanarya096d7a2015-03-27 12:16:53 -0700296 // PNG
scroggob636b452015-07-22 07:16:20 -0700297 check(r, "arrow.png", SkISize::Make(187, 312), true, false);
298 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false);
299 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false);
300 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false);
301 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false);
302 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false);
303 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false);
304 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false);
305 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false);
306 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false);
307 check(r, "plane.png", SkISize::Make(250, 126), true, false);
scroggo46c57472015-09-30 08:57:13 -0700308 check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false);
scroggob636b452015-07-22 07:16:20 -0700309 check(r, "randPixels.png", SkISize::Make(8, 8), true, false);
310 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700311}
scroggo0a7e69c2015-04-03 07:22:22 -0700312
scroggo46c57472015-09-30 08:57:13 -0700313// Test interlaced PNG in stripes, similar to DM's kStripe_Mode
314DEF_TEST(Codec_stripes, r) {
315 const char * path = "plane_interlaced.png";
316 SkAutoTDelete<SkStream> stream(resource(path));
317 if (!stream) {
318 SkDebugf("Missing resource '%s'\n", path);
319 }
320
321 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
322 REPORTER_ASSERT(r, codec);
323
324 if (!codec) {
325 return;
326 }
327
328 switch (codec->getScanlineOrder()) {
329 case SkCodec::kBottomUp_SkScanlineOrder:
330 case SkCodec::kOutOfOrder_SkScanlineOrder:
331 ERRORF(r, "This scanline order will not match the original.");
332 return;
333 default:
334 break;
335 }
336
337 // Baseline for what the image should look like, using N32.
338 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
339
340 SkBitmap bm;
341 bm.allocPixels(info);
342 SkAutoLockPixels autoLockPixels(bm);
343 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
344 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
345
346 SkMD5::Digest digest;
347 md5(bm, &digest);
348
349 // Now decode in stripes
350 const int height = info.height();
351 const int numStripes = 4;
352 int stripeHeight;
353 int remainingLines;
354 SkTDivMod(height, numStripes, &stripeHeight, &remainingLines);
355
356 bm.eraseColor(SK_ColorYELLOW);
357
358 result = codec->startScanlineDecode(info);
359 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
360
361 // Odd stripes
362 for (int i = 1; i < numStripes; i += 2) {
363 // Skip the even stripes
364 result = codec->skipScanlines(stripeHeight);
365 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
366
367 result = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
368 bm.rowBytes());
369 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
370 }
371
372 // Even stripes
373 result = codec->startScanlineDecode(info);
374 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
375
376 for (int i = 0; i < numStripes; i += 2) {
377 result = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
378 bm.rowBytes());
379 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
380
381 // Skip the odd stripes
382 if (i + 1 < numStripes) {
383 result = codec->skipScanlines(stripeHeight);
384 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
385 }
386 }
387
388 // Remainder at the end
389 if (remainingLines > 0) {
390 result = codec->startScanlineDecode(info);
391 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
392
393 result = codec->skipScanlines(height - remainingLines);
394 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
395
396 result = codec->getScanlines(bm.getAddr(0, height - remainingLines),
397 remainingLines, bm.rowBytes());
398 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
399 }
400
401 compare_to_good_digest(r, digest, bm);
402}
403
scroggo0a7e69c2015-04-03 07:22:22 -0700404static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700405 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
scroggo0a7e69c2015-04-03 07:22:22 -0700406 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo2c3b2182015-10-09 08:40:59 -0700407 REPORTER_ASSERT(r, !codec);
408
409 codec = SkScaledCodec::NewFromStream(new SkMemoryStream(stream, len, false));
scroggo0a7e69c2015-04-03 07:22:22 -0700410 REPORTER_ASSERT(r, !codec);
411}
412
413// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
414// even on failure. Test some bad streams.
415DEF_TEST(Codec_leaks, r) {
416 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
417 const char nonSupportedStream[] = "hello world";
418 // The other strings should look like the beginning of a file type, so we'll call some
419 // internal version of NewFromStream, which must also delete the stream on failure.
420 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
421 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
422 const char emptyWebp[] = "RIFF1234WEBPVP";
423 const char emptyBmp[] = { 'B', 'M' };
424 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
425 const char emptyGif[] = "GIFVER";
426
427 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
428 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
429 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
430 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
431 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
432 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
433 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
434}
msarette16b04a2015-04-15 07:32:19 -0700435
scroggo2c3b2182015-10-09 08:40:59 -0700436DEF_TEST(Codec_null, r) {
437 // Attempting to create an SkCodec or an SkScaledCodec with null should not
438 // crash.
439 SkCodec* codec = SkCodec::NewFromStream(nullptr);
440 REPORTER_ASSERT(r, !codec);
441
442 codec = SkScaledCodec::NewFromStream(nullptr);
443 REPORTER_ASSERT(r, !codec);
444}
445
msarette16b04a2015-04-15 07:32:19 -0700446static void test_dimensions(skiatest::Reporter* r, const char path[]) {
447 // Create the codec from the resource file
448 SkAutoTDelete<SkStream> stream(resource(path));
449 if (!stream) {
450 SkDebugf("Missing resource '%s'\n", path);
451 return;
452 }
msarettb32758a2015-08-18 13:22:46 -0700453 SkAutoTDelete<SkCodec> codec(SkScaledCodec::NewFromStream(stream.detach()));
msarette16b04a2015-04-15 07:32:19 -0700454 if (!codec) {
455 ERRORF(r, "Unable to create codec '%s'", path);
456 return;
457 }
458
459 // Check that the decode is successful for a variety of scales
msarettb32758a2015-08-18 13:22:46 -0700460 for (float scale = 0.05f; scale < 2.0f; scale += 0.05f) {
msarette16b04a2015-04-15 07:32:19 -0700461 // Scale the output dimensions
462 SkISize scaledDims = codec->getScaledDimensions(scale);
msarettb32758a2015-08-18 13:22:46 -0700463 SkImageInfo scaledInfo = codec->getInfo()
464 .makeWH(scaledDims.width(), scaledDims.height())
465 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700466
467 // Set up for the decode
468 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
469 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
470 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
471
scroggoeb602a52015-07-09 08:16:03 -0700472 SkCodec::Result result =
halcanary96fcdcc2015-08-27 07:41:13 -0700473 codec->getPixels(scaledInfo, pixels.get(), rowBytes, nullptr, nullptr, nullptr);
scroggoeb602a52015-07-09 08:16:03 -0700474 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700475 }
476}
477
478// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
479DEF_TEST(Codec_Dimensions, r) {
480 // JPG
481 test_dimensions(r, "CMYK.jpg");
482 test_dimensions(r, "color_wheel.jpg");
483 test_dimensions(r, "grayscale.jpg");
484 test_dimensions(r, "mandrill_512_q075.jpg");
485 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700486
487 // Decoding small images with very large scaling factors is a potential
488 // source of bugs and crashes. We disable these tests in Gold because
489 // tiny images are not very useful to look at.
490 // Here we make sure that we do not crash or access illegal memory when
491 // performing scaled decodes on small images.
492 test_dimensions(r, "1x1.png");
493 test_dimensions(r, "2x2.png");
494 test_dimensions(r, "3x3.png");
495 test_dimensions(r, "3x1.png");
496 test_dimensions(r, "1x1.png");
497 test_dimensions(r, "16x1.png");
498 test_dimensions(r, "1x16.png");
499 test_dimensions(r, "mandrill_16.png");
500
msarette16b04a2015-04-15 07:32:19 -0700501}
502
msarettd0375bc2015-08-12 08:08:56 -0700503static void test_invalid(skiatest::Reporter* r, const char path[]) {
msarett4b17fa32015-04-23 08:53:39 -0700504 SkAutoTDelete<SkStream> stream(resource(path));
505 if (!stream) {
506 SkDebugf("Missing resource '%s'\n", path);
507 return;
508 }
509 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700510 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700511}
msarette16b04a2015-04-15 07:32:19 -0700512
msarett4b17fa32015-04-23 08:53:39 -0700513DEF_TEST(Codec_Empty, r) {
514 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700515 test_invalid(r, "empty_images/zero-dims.gif");
516 test_invalid(r, "empty_images/zero-embedded.ico");
517 test_invalid(r, "empty_images/zero-width.bmp");
518 test_invalid(r, "empty_images/zero-height.bmp");
519 test_invalid(r, "empty_images/zero-width.jpg");
520 test_invalid(r, "empty_images/zero-height.jpg");
521 test_invalid(r, "empty_images/zero-width.png");
522 test_invalid(r, "empty_images/zero-height.png");
523 test_invalid(r, "empty_images/zero-width.wbmp");
524 test_invalid(r, "empty_images/zero-height.wbmp");
525 // This image is an ico with an embedded mask-bmp. This is illegal.
526 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700527}
msarett99f567e2015-08-05 12:58:26 -0700528
529static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
530 SkAutoTDelete<SkStream> stream(resource(path));
531 if (!stream) {
532 SkDebugf("Missing resource '%s'\n", path);
533 return;
534 }
scroggo46c57472015-09-30 08:57:13 -0700535 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.detach()));
msarett99f567e2015-08-05 12:58:26 -0700536
537 // This should return kSuccess because kIndex8 is supported.
538 SkPMColor colorStorage[256];
539 int colorCount;
scroggo46c57472015-09-30 08:57:13 -0700540 SkCodec::Result result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700541 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
msarett99f567e2015-08-05 12:58:26 -0700542 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
543 // The rest of the test is uninteresting if kIndex8 is not supported
544 if (SkCodec::kSuccess != result) {
545 return;
546 }
547
548 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
549 // colorPtr and a valid colorCountPtr.
scroggo46c57472015-09-30 08:57:13 -0700550 result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700551 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
msarett99f567e2015-08-05 12:58:26 -0700552 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
scroggo46c57472015-09-30 08:57:13 -0700553 result = decoder->startScanlineDecode(
msarett99f567e2015-08-05 12:58:26 -0700554 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
555 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
556}
557
558DEF_TEST(Codec_Params, r) {
559 test_invalid_parameters(r, "index8.png");
560 test_invalid_parameters(r, "mandrill.wbmp");
561}