blob: f1c5bbaa128e1c989bf06af39f27f7a22e211864 [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
halcanarya096d7a2015-03-27 12:16:53 -070077static void check(skiatest::Reporter* r,
78 const char path[],
79 SkISize size,
scroggob636b452015-07-22 07:16:20 -070080 bool supportsScanlineDecoding,
scroggocc2feb12015-08-14 08:32:46 -070081 bool supportsSubsetDecoding,
82 bool supports565 = true) {
halcanarya096d7a2015-03-27 12:16:53 -070083 SkAutoTDelete<SkStream> stream(resource(path));
84 if (!stream) {
85 SkDebugf("Missing resource '%s'\n", path);
86 return;
87 }
scroggo58421542015-04-01 11:25:20 -070088 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
89 if (!codec) {
halcanarya096d7a2015-03-27 12:16:53 -070090 ERRORF(r, "Unable to decode '%s'", path);
91 return;
92 }
msarett438b2ad2015-04-09 12:43:10 -070093
94 // This test is used primarily to verify rewinding works properly. Using kN32 allows
95 // us to test this without the added overhead of creating different bitmaps depending
96 // on the color type (ex: building a color table for kIndex8). DM is where we test
97 // decodes to all possible destination color types.
98 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
halcanarya096d7a2015-03-27 12:16:53 -070099 REPORTER_ASSERT(r, info.dimensions() == size);
scroggocc2feb12015-08-14 08:32:46 -0700100
halcanarya096d7a2015-03-27 12:16:53 -0700101 SkBitmap bm;
102 bm.allocPixels(info);
103 SkAutoLockPixels autoLockPixels(bm);
scroggoeb602a52015-07-09 08:16:03 -0700104 SkCodec::Result result =
halcanary96fcdcc2015-08-27 07:41:13 -0700105 codec->getPixels(info, bm.getPixels(), bm.rowBytes(), nullptr, nullptr, nullptr);
scroggoeb602a52015-07-09 08:16:03 -0700106 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
halcanarya096d7a2015-03-27 12:16:53 -0700107
scroggo9b2cdbf42015-07-10 12:07:02 -0700108 SkMD5::Digest digest;
109 md5(bm, &digest);
halcanarya096d7a2015-03-27 12:16:53 -0700110
msarett8ff6ca62015-09-18 12:06:04 -0700111 {
112 // Test decoding to 565
113 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
114 SkCodec::Result expected = (supports565 && info.alphaType() == kOpaque_SkAlphaType) ?
115 SkCodec::kSuccess : SkCodec::kInvalidConversion;
116 test_info(r, codec, info565, expected, nullptr);
117 }
118
119 // Verify that re-decoding gives the same result. It is interesting to check this after
120 // a decode to 565, since choosing to decode to 565 may result in some of the decode
121 // options being modified. These options should return to their defaults on another
122 // decode to kN32, so the new digest should match the old digest.
scroggod1bc5742015-08-12 08:31:44 -0700123 test_info(r, codec, info, SkCodec::kSuccess, &digest);
124
125 {
126 // Check alpha type conversions
127 if (info.alphaType() == kOpaque_SkAlphaType) {
128 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700129 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700130 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700131 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700132 } else {
133 // Decoding to opaque should fail
134 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700135 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700136 SkAlphaType otherAt = info.alphaType();
137 if (kPremul_SkAlphaType == otherAt) {
138 otherAt = kUnpremul_SkAlphaType;
139 } else {
140 otherAt = kPremul_SkAlphaType;
141 }
142 // The other non-opaque alpha type should always succeed, but not match.
halcanary96fcdcc2015-08-27 07:41:13 -0700143 test_info(r, codec, info.makeAlphaType(otherAt), SkCodec::kSuccess, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700144 }
145 }
146
147 // Scanline decoding follows.
scroggo58421542015-04-01 11:25:20 -0700148
scroggo46c57472015-09-30 08:57:13 -0700149 // Need to call start() first.
150 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
151 == SkCodec::kScanlineDecodingNotStarted);
152 REPORTER_ASSERT(r, codec->skipScanlines(1)
153 == SkCodec::kScanlineDecodingNotStarted);
154
155 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700156 if (supportsScanlineDecoding) {
157 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700158
scroggo46c57472015-09-30 08:57:13 -0700159 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700160
scroggo58421542015-04-01 11:25:20 -0700161 for (int y = 0; y < info.height(); y++) {
scroggo46c57472015-09-30 08:57:13 -0700162 result = codec->getScanlines(bm.getAddr(0, y), 1, 0);
scroggoeb602a52015-07-09 08:16:03 -0700163 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
scroggo58421542015-04-01 11:25:20 -0700164 }
165 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700166 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarett5406d6f2015-08-31 06:55:13 -0700167 compare_to_good_digest(r, digest, bm);
168 }
scroggo46c57472015-09-30 08:57:13 -0700169
170 // Cannot continue to decode scanlines beyond the end
171 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
172 == SkCodec::kInvalidParameters);
173
174 // Interrupting a scanline decode with a full decode starts from
175 // scratch
176 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
177 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
178 == SkCodec::kSuccess);
179 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
180 == SkCodec::kSuccess);
181 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
182 == SkCodec::kScanlineDecodingNotStarted);
183 REPORTER_ASSERT(r, codec->skipScanlines(1)
184 == SkCodec::kScanlineDecodingNotStarted);
scroggo58421542015-04-01 11:25:20 -0700185 } else {
scroggo46c57472015-09-30 08:57:13 -0700186 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700187 }
scroggob636b452015-07-22 07:16:20 -0700188
189 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
190 // random subsets.
191 // Do not attempt to decode subsets of an image of only once pixel, since there is no
192 // meaningful subset.
193 if (size.width() * size.height() == 1) {
194 return;
195 }
196
197 SkRandom rand;
198 SkIRect subset;
199 SkCodec::Options opts;
200 opts.fSubset = &subset;
201 for (int i = 0; i < 5; i++) {
202 subset = generate_random_subset(&rand, size.width(), size.height());
203 SkASSERT(!subset.isEmpty());
204 const bool supported = codec->getValidSubset(&subset);
205 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
206
207 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
208 SkBitmap bm;
209 bm.allocPixels(subsetInfo);
210 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700211 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700212
213 if (supportsSubsetDecoding) {
214 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
215 // Webp is the only codec that supports subsets, and it will have modified the subset
216 // to have even left/top.
217 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
218 } else {
219 // No subsets will work.
220 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
221 }
222 }
halcanarya096d7a2015-03-27 12:16:53 -0700223}
224
225DEF_TEST(Codec, r) {
226 // WBMP
msarett99f567e2015-08-05 12:58:26 -0700227 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700228
scroggo6f5e6192015-06-18 12:53:43 -0700229 // WEBP
scroggob636b452015-07-22 07:16:20 -0700230 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true);
231 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true);
232 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true);
scroggo6f5e6192015-06-18 12:53:43 -0700233
halcanarya096d7a2015-03-27 12:16:53 -0700234 // BMP
msarett5406d6f2015-08-31 06:55:13 -0700235 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700236
237 // ICO
msarett68b204e2015-04-01 12:09:21 -0700238 // These two tests examine interestingly different behavior:
239 // Decodes an embedded BMP image
scroggob636b452015-07-22 07:16:20 -0700240 check(r, "color_wheel.ico", SkISize::Make(128, 128), false, false);
msarett68b204e2015-04-01 12:09:21 -0700241 // Decodes an embedded PNG image
scroggob636b452015-07-22 07:16:20 -0700242 check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700243
msarett438b2ad2015-04-09 12:43:10 -0700244 // GIF
msarett10522ff2015-09-07 08:54:01 -0700245 check(r, "box.gif", SkISize::Make(200, 55), true, false);
246 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false);
247 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false);
msarett438b2ad2015-04-09 12:43:10 -0700248
msarette16b04a2015-04-15 07:32:19 -0700249 // JPG
scroggocc2feb12015-08-14 08:32:46 -0700250 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false, false);
scroggob636b452015-07-22 07:16:20 -0700251 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
252 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false);
253 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
254 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false);
msarette16b04a2015-04-15 07:32:19 -0700255
halcanarya096d7a2015-03-27 12:16:53 -0700256 // PNG
scroggob636b452015-07-22 07:16:20 -0700257 check(r, "arrow.png", SkISize::Make(187, 312), true, false);
258 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false);
259 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false);
260 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false);
261 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false);
262 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false);
263 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false);
264 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false);
265 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false);
266 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false);
267 check(r, "plane.png", SkISize::Make(250, 126), true, false);
scroggo46c57472015-09-30 08:57:13 -0700268 check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false);
scroggob636b452015-07-22 07:16:20 -0700269 check(r, "randPixels.png", SkISize::Make(8, 8), true, false);
270 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700271}
scroggo0a7e69c2015-04-03 07:22:22 -0700272
scroggo46c57472015-09-30 08:57:13 -0700273// Test interlaced PNG in stripes, similar to DM's kStripe_Mode
274DEF_TEST(Codec_stripes, r) {
275 const char * path = "plane_interlaced.png";
276 SkAutoTDelete<SkStream> stream(resource(path));
277 if (!stream) {
278 SkDebugf("Missing resource '%s'\n", path);
279 }
280
281 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
282 REPORTER_ASSERT(r, codec);
283
284 if (!codec) {
285 return;
286 }
287
288 switch (codec->getScanlineOrder()) {
289 case SkCodec::kBottomUp_SkScanlineOrder:
290 case SkCodec::kOutOfOrder_SkScanlineOrder:
291 ERRORF(r, "This scanline order will not match the original.");
292 return;
293 default:
294 break;
295 }
296
297 // Baseline for what the image should look like, using N32.
298 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
299
300 SkBitmap bm;
301 bm.allocPixels(info);
302 SkAutoLockPixels autoLockPixels(bm);
303 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
304 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
305
306 SkMD5::Digest digest;
307 md5(bm, &digest);
308
309 // Now decode in stripes
310 const int height = info.height();
311 const int numStripes = 4;
312 int stripeHeight;
313 int remainingLines;
314 SkTDivMod(height, numStripes, &stripeHeight, &remainingLines);
315
316 bm.eraseColor(SK_ColorYELLOW);
317
318 result = codec->startScanlineDecode(info);
319 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
320
321 // Odd stripes
322 for (int i = 1; i < numStripes; i += 2) {
323 // Skip the even stripes
324 result = codec->skipScanlines(stripeHeight);
325 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
326
327 result = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
328 bm.rowBytes());
329 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
330 }
331
332 // Even stripes
333 result = codec->startScanlineDecode(info);
334 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
335
336 for (int i = 0; i < numStripes; i += 2) {
337 result = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
338 bm.rowBytes());
339 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
340
341 // Skip the odd stripes
342 if (i + 1 < numStripes) {
343 result = codec->skipScanlines(stripeHeight);
344 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
345 }
346 }
347
348 // Remainder at the end
349 if (remainingLines > 0) {
350 result = codec->startScanlineDecode(info);
351 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
352
353 result = codec->skipScanlines(height - remainingLines);
354 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
355
356 result = codec->getScanlines(bm.getAddr(0, height - remainingLines),
357 remainingLines, bm.rowBytes());
358 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
359 }
360
361 compare_to_good_digest(r, digest, bm);
362}
363
scroggo0a7e69c2015-04-03 07:22:22 -0700364static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
365 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
366 // We should not have gotten a codec. Bots should catch us if we leaked anything.
367 REPORTER_ASSERT(r, !codec);
368}
369
370// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
371// even on failure. Test some bad streams.
372DEF_TEST(Codec_leaks, r) {
373 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
374 const char nonSupportedStream[] = "hello world";
375 // The other strings should look like the beginning of a file type, so we'll call some
376 // internal version of NewFromStream, which must also delete the stream on failure.
377 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
378 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
379 const char emptyWebp[] = "RIFF1234WEBPVP";
380 const char emptyBmp[] = { 'B', 'M' };
381 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
382 const char emptyGif[] = "GIFVER";
383
384 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
385 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
386 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
387 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
388 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
389 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
390 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
391}
msarette16b04a2015-04-15 07:32:19 -0700392
393static void test_dimensions(skiatest::Reporter* r, const char path[]) {
394 // Create the codec from the resource file
395 SkAutoTDelete<SkStream> stream(resource(path));
396 if (!stream) {
397 SkDebugf("Missing resource '%s'\n", path);
398 return;
399 }
msarettb32758a2015-08-18 13:22:46 -0700400 SkAutoTDelete<SkCodec> codec(SkScaledCodec::NewFromStream(stream.detach()));
msarette16b04a2015-04-15 07:32:19 -0700401 if (!codec) {
402 ERRORF(r, "Unable to create codec '%s'", path);
403 return;
404 }
405
406 // Check that the decode is successful for a variety of scales
msarettb32758a2015-08-18 13:22:46 -0700407 for (float scale = 0.05f; scale < 2.0f; scale += 0.05f) {
msarette16b04a2015-04-15 07:32:19 -0700408 // Scale the output dimensions
409 SkISize scaledDims = codec->getScaledDimensions(scale);
msarettb32758a2015-08-18 13:22:46 -0700410 SkImageInfo scaledInfo = codec->getInfo()
411 .makeWH(scaledDims.width(), scaledDims.height())
412 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700413
414 // Set up for the decode
415 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
416 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
417 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
418
scroggoeb602a52015-07-09 08:16:03 -0700419 SkCodec::Result result =
halcanary96fcdcc2015-08-27 07:41:13 -0700420 codec->getPixels(scaledInfo, pixels.get(), rowBytes, nullptr, nullptr, nullptr);
scroggoeb602a52015-07-09 08:16:03 -0700421 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700422 }
423}
424
425// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
426DEF_TEST(Codec_Dimensions, r) {
427 // JPG
428 test_dimensions(r, "CMYK.jpg");
429 test_dimensions(r, "color_wheel.jpg");
430 test_dimensions(r, "grayscale.jpg");
431 test_dimensions(r, "mandrill_512_q075.jpg");
432 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700433
434 // Decoding small images with very large scaling factors is a potential
435 // source of bugs and crashes. We disable these tests in Gold because
436 // tiny images are not very useful to look at.
437 // Here we make sure that we do not crash or access illegal memory when
438 // performing scaled decodes on small images.
439 test_dimensions(r, "1x1.png");
440 test_dimensions(r, "2x2.png");
441 test_dimensions(r, "3x3.png");
442 test_dimensions(r, "3x1.png");
443 test_dimensions(r, "1x1.png");
444 test_dimensions(r, "16x1.png");
445 test_dimensions(r, "1x16.png");
446 test_dimensions(r, "mandrill_16.png");
447
msarette16b04a2015-04-15 07:32:19 -0700448}
449
msarettd0375bc2015-08-12 08:08:56 -0700450static void test_invalid(skiatest::Reporter* r, const char path[]) {
msarett4b17fa32015-04-23 08:53:39 -0700451 SkAutoTDelete<SkStream> stream(resource(path));
452 if (!stream) {
453 SkDebugf("Missing resource '%s'\n", path);
454 return;
455 }
456 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700457 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700458}
msarette16b04a2015-04-15 07:32:19 -0700459
msarett4b17fa32015-04-23 08:53:39 -0700460DEF_TEST(Codec_Empty, r) {
461 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700462 test_invalid(r, "empty_images/zero-dims.gif");
463 test_invalid(r, "empty_images/zero-embedded.ico");
464 test_invalid(r, "empty_images/zero-width.bmp");
465 test_invalid(r, "empty_images/zero-height.bmp");
466 test_invalid(r, "empty_images/zero-width.jpg");
467 test_invalid(r, "empty_images/zero-height.jpg");
468 test_invalid(r, "empty_images/zero-width.png");
469 test_invalid(r, "empty_images/zero-height.png");
470 test_invalid(r, "empty_images/zero-width.wbmp");
471 test_invalid(r, "empty_images/zero-height.wbmp");
472 // This image is an ico with an embedded mask-bmp. This is illegal.
473 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700474}
msarett99f567e2015-08-05 12:58:26 -0700475
476static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
477 SkAutoTDelete<SkStream> stream(resource(path));
478 if (!stream) {
479 SkDebugf("Missing resource '%s'\n", path);
480 return;
481 }
scroggo46c57472015-09-30 08:57:13 -0700482 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.detach()));
msarett99f567e2015-08-05 12:58:26 -0700483
484 // This should return kSuccess because kIndex8 is supported.
485 SkPMColor colorStorage[256];
486 int colorCount;
scroggo46c57472015-09-30 08:57:13 -0700487 SkCodec::Result result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700488 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
msarett99f567e2015-08-05 12:58:26 -0700489 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
490 // The rest of the test is uninteresting if kIndex8 is not supported
491 if (SkCodec::kSuccess != result) {
492 return;
493 }
494
495 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
496 // colorPtr and a valid colorCountPtr.
scroggo46c57472015-09-30 08:57:13 -0700497 result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700498 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
msarett99f567e2015-08-05 12:58:26 -0700499 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
scroggo46c57472015-09-30 08:57:13 -0700500 result = decoder->startScanlineDecode(
msarett99f567e2015-08-05 12:58:26 -0700501 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
502 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
503}
504
505DEF_TEST(Codec_Params, r) {
506 test_invalid_parameters(r, "index8.png");
507 test_invalid_parameters(r, "mandrill.wbmp");
508}