blob: 63631822de45b16c3f3a31bd0406139a7d8b7343 [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
129static void check(skiatest::Reporter* r,
130 const char path[],
131 SkISize size,
132 bool supportsScanlineDecoding,
133 bool supportsSubsetDecoding,
134 bool supports565 = true) {
135
136 SkAutoTDelete<SkStream> stream(resource(path));
137 if (!stream) {
138 SkDebugf("Missing resource '%s'\n", path);
139 return;
140 }
141 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
142 if (!codec) {
143 ERRORF(r, "Unable to decode '%s'", path);
144 return;
145 }
146
147 // Test full image decodes with SkCodec
148 SkMD5::Digest codecDigest;
149 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
150 SkBitmap bm;
151 test_codec(r, codec, bm, info, size, supports565, &codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700152
153 // Scanline decoding follows.
msarettcc7f3052015-10-05 14:20:27 -0700154 // Need to call startScanlineDecode() first.
scroggo46c57472015-09-30 08:57:13 -0700155 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
156 == SkCodec::kScanlineDecodingNotStarted);
157 REPORTER_ASSERT(r, codec->skipScanlines(1)
158 == SkCodec::kScanlineDecodingNotStarted);
159
160 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700161 if (supportsScanlineDecoding) {
162 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700163
scroggo46c57472015-09-30 08:57:13 -0700164 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700165
scroggo58421542015-04-01 11:25:20 -0700166 for (int y = 0; y < info.height(); y++) {
msarettcc7f3052015-10-05 14:20:27 -0700167 SkCodec::Result result = codec->getScanlines(bm.getAddr(0, y), 1, 0);
scroggoeb602a52015-07-09 08:16:03 -0700168 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
scroggo58421542015-04-01 11:25:20 -0700169 }
170 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700171 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
msarettcc7f3052015-10-05 14:20:27 -0700172 compare_to_good_digest(r, codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700173 }
scroggo46c57472015-09-30 08:57:13 -0700174
175 // Cannot continue to decode scanlines beyond the end
176 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
177 == SkCodec::kInvalidParameters);
178
179 // Interrupting a scanline decode with a full decode starts from
180 // scratch
181 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
182 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
183 == SkCodec::kSuccess);
184 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
185 == SkCodec::kSuccess);
186 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
187 == SkCodec::kScanlineDecodingNotStarted);
188 REPORTER_ASSERT(r, codec->skipScanlines(1)
189 == SkCodec::kScanlineDecodingNotStarted);
scroggo58421542015-04-01 11:25:20 -0700190 } else {
scroggo46c57472015-09-30 08:57:13 -0700191 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700192 }
scroggob636b452015-07-22 07:16:20 -0700193
194 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
195 // random subsets.
196 // Do not attempt to decode subsets of an image of only once pixel, since there is no
197 // meaningful subset.
198 if (size.width() * size.height() == 1) {
199 return;
200 }
201
202 SkRandom rand;
203 SkIRect subset;
204 SkCodec::Options opts;
205 opts.fSubset = &subset;
206 for (int i = 0; i < 5; i++) {
207 subset = generate_random_subset(&rand, size.width(), size.height());
208 SkASSERT(!subset.isEmpty());
209 const bool supported = codec->getValidSubset(&subset);
210 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
211
212 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
213 SkBitmap bm;
214 bm.allocPixels(subsetInfo);
215 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700216 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700217
218 if (supportsSubsetDecoding) {
219 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
220 // Webp is the only codec that supports subsets, and it will have modified the subset
221 // to have even left/top.
222 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
223 } else {
224 // No subsets will work.
225 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
226 }
227 }
msarettcc7f3052015-10-05 14:20:27 -0700228
229 // SkScaledCodec tests
230 if (supportsScanlineDecoding || supportsSubsetDecoding){
231 SkAutoTDelete<SkStream> stream(resource(path));
232 if (!stream) {
233 SkDebugf("Missing resource '%s'\n", path);
234 return;
235 }
236 SkAutoTDelete<SkCodec> codec(SkScaledCodec::NewFromStream(stream.detach()));
237 if (!codec) {
238 ERRORF(r, "Unable to decode '%s'", path);
239 return;
240 }
241
242 SkBitmap bm;
243 SkMD5::Digest scaledCodecDigest;
244 test_codec(r, codec, bm, info, size, supports565, &scaledCodecDigest, &codecDigest);
245 }
halcanarya096d7a2015-03-27 12:16:53 -0700246}
247
248DEF_TEST(Codec, r) {
249 // WBMP
msarett99f567e2015-08-05 12:58:26 -0700250 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700251
scroggo6f5e6192015-06-18 12:53:43 -0700252 // WEBP
scroggob636b452015-07-22 07:16:20 -0700253 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true);
254 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true);
255 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true);
scroggo6f5e6192015-06-18 12:53:43 -0700256
halcanarya096d7a2015-03-27 12:16:53 -0700257 // BMP
msarett5406d6f2015-08-31 06:55:13 -0700258 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700259
260 // ICO
msarett68b204e2015-04-01 12:09:21 -0700261 // These two tests examine interestingly different behavior:
262 // Decodes an embedded BMP image
scroggob636b452015-07-22 07:16:20 -0700263 check(r, "color_wheel.ico", SkISize::Make(128, 128), false, false);
msarett68b204e2015-04-01 12:09:21 -0700264 // Decodes an embedded PNG image
scroggob636b452015-07-22 07:16:20 -0700265 check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700266
msarett438b2ad2015-04-09 12:43:10 -0700267 // GIF
msarett10522ff2015-09-07 08:54:01 -0700268 check(r, "box.gif", SkISize::Make(200, 55), true, false);
269 check(r, "color_wheel.gif", SkISize::Make(128, 128), true, false);
270 check(r, "randPixels.gif", SkISize::Make(8, 8), true, false);
msarett438b2ad2015-04-09 12:43:10 -0700271
msarette16b04a2015-04-15 07:32:19 -0700272 // JPG
scroggocc2feb12015-08-14 08:32:46 -0700273 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false, false);
scroggob636b452015-07-22 07:16:20 -0700274 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
275 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false);
276 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
277 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false);
msarette16b04a2015-04-15 07:32:19 -0700278
halcanarya096d7a2015-03-27 12:16:53 -0700279 // PNG
scroggob636b452015-07-22 07:16:20 -0700280 check(r, "arrow.png", SkISize::Make(187, 312), true, false);
281 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false);
282 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false);
283 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false);
284 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false);
285 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false);
286 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false);
287 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false);
288 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false);
289 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false);
290 check(r, "plane.png", SkISize::Make(250, 126), true, false);
scroggo46c57472015-09-30 08:57:13 -0700291 check(r, "plane_interlaced.png", SkISize::Make(250, 126), true, false);
scroggob636b452015-07-22 07:16:20 -0700292 check(r, "randPixels.png", SkISize::Make(8, 8), true, false);
293 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700294}
scroggo0a7e69c2015-04-03 07:22:22 -0700295
scroggo46c57472015-09-30 08:57:13 -0700296// Test interlaced PNG in stripes, similar to DM's kStripe_Mode
297DEF_TEST(Codec_stripes, r) {
298 const char * path = "plane_interlaced.png";
299 SkAutoTDelete<SkStream> stream(resource(path));
300 if (!stream) {
301 SkDebugf("Missing resource '%s'\n", path);
302 }
303
304 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
305 REPORTER_ASSERT(r, codec);
306
307 if (!codec) {
308 return;
309 }
310
311 switch (codec->getScanlineOrder()) {
312 case SkCodec::kBottomUp_SkScanlineOrder:
313 case SkCodec::kOutOfOrder_SkScanlineOrder:
314 ERRORF(r, "This scanline order will not match the original.");
315 return;
316 default:
317 break;
318 }
319
320 // Baseline for what the image should look like, using N32.
321 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
322
323 SkBitmap bm;
324 bm.allocPixels(info);
325 SkAutoLockPixels autoLockPixels(bm);
326 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
327 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
328
329 SkMD5::Digest digest;
330 md5(bm, &digest);
331
332 // Now decode in stripes
333 const int height = info.height();
334 const int numStripes = 4;
335 int stripeHeight;
336 int remainingLines;
337 SkTDivMod(height, numStripes, &stripeHeight, &remainingLines);
338
339 bm.eraseColor(SK_ColorYELLOW);
340
341 result = codec->startScanlineDecode(info);
342 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
343
344 // Odd stripes
345 for (int i = 1; i < numStripes; i += 2) {
346 // Skip the even stripes
347 result = codec->skipScanlines(stripeHeight);
348 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
349
350 result = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
351 bm.rowBytes());
352 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
353 }
354
355 // Even stripes
356 result = codec->startScanlineDecode(info);
357 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
358
359 for (int i = 0; i < numStripes; i += 2) {
360 result = codec->getScanlines(bm.getAddr(0, i * stripeHeight), stripeHeight,
361 bm.rowBytes());
362 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
363
364 // Skip the odd stripes
365 if (i + 1 < numStripes) {
366 result = codec->skipScanlines(stripeHeight);
367 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
368 }
369 }
370
371 // Remainder at the end
372 if (remainingLines > 0) {
373 result = codec->startScanlineDecode(info);
374 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
375
376 result = codec->skipScanlines(height - remainingLines);
377 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
378
379 result = codec->getScanlines(bm.getAddr(0, height - remainingLines),
380 remainingLines, bm.rowBytes());
381 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
382 }
383
384 compare_to_good_digest(r, digest, bm);
385}
386
scroggo0a7e69c2015-04-03 07:22:22 -0700387static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
388 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
389 // We should not have gotten a codec. Bots should catch us if we leaked anything.
390 REPORTER_ASSERT(r, !codec);
391}
392
393// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
394// even on failure. Test some bad streams.
395DEF_TEST(Codec_leaks, r) {
396 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
397 const char nonSupportedStream[] = "hello world";
398 // The other strings should look like the beginning of a file type, so we'll call some
399 // internal version of NewFromStream, which must also delete the stream on failure.
400 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
401 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
402 const char emptyWebp[] = "RIFF1234WEBPVP";
403 const char emptyBmp[] = { 'B', 'M' };
404 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
405 const char emptyGif[] = "GIFVER";
406
407 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
408 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
409 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
410 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
411 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
412 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
413 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
414}
msarette16b04a2015-04-15 07:32:19 -0700415
416static void test_dimensions(skiatest::Reporter* r, const char path[]) {
417 // Create the codec from the resource file
418 SkAutoTDelete<SkStream> stream(resource(path));
419 if (!stream) {
420 SkDebugf("Missing resource '%s'\n", path);
421 return;
422 }
msarettb32758a2015-08-18 13:22:46 -0700423 SkAutoTDelete<SkCodec> codec(SkScaledCodec::NewFromStream(stream.detach()));
msarette16b04a2015-04-15 07:32:19 -0700424 if (!codec) {
425 ERRORF(r, "Unable to create codec '%s'", path);
426 return;
427 }
428
429 // Check that the decode is successful for a variety of scales
msarettb32758a2015-08-18 13:22:46 -0700430 for (float scale = 0.05f; scale < 2.0f; scale += 0.05f) {
msarette16b04a2015-04-15 07:32:19 -0700431 // Scale the output dimensions
432 SkISize scaledDims = codec->getScaledDimensions(scale);
msarettb32758a2015-08-18 13:22:46 -0700433 SkImageInfo scaledInfo = codec->getInfo()
434 .makeWH(scaledDims.width(), scaledDims.height())
435 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700436
437 // Set up for the decode
438 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
439 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
440 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
441
scroggoeb602a52015-07-09 08:16:03 -0700442 SkCodec::Result result =
halcanary96fcdcc2015-08-27 07:41:13 -0700443 codec->getPixels(scaledInfo, pixels.get(), rowBytes, nullptr, nullptr, nullptr);
scroggoeb602a52015-07-09 08:16:03 -0700444 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700445 }
446}
447
448// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
449DEF_TEST(Codec_Dimensions, r) {
450 // JPG
451 test_dimensions(r, "CMYK.jpg");
452 test_dimensions(r, "color_wheel.jpg");
453 test_dimensions(r, "grayscale.jpg");
454 test_dimensions(r, "mandrill_512_q075.jpg");
455 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700456
457 // Decoding small images with very large scaling factors is a potential
458 // source of bugs and crashes. We disable these tests in Gold because
459 // tiny images are not very useful to look at.
460 // Here we make sure that we do not crash or access illegal memory when
461 // performing scaled decodes on small images.
462 test_dimensions(r, "1x1.png");
463 test_dimensions(r, "2x2.png");
464 test_dimensions(r, "3x3.png");
465 test_dimensions(r, "3x1.png");
466 test_dimensions(r, "1x1.png");
467 test_dimensions(r, "16x1.png");
468 test_dimensions(r, "1x16.png");
469 test_dimensions(r, "mandrill_16.png");
470
msarette16b04a2015-04-15 07:32:19 -0700471}
472
msarettd0375bc2015-08-12 08:08:56 -0700473static void test_invalid(skiatest::Reporter* r, const char path[]) {
msarett4b17fa32015-04-23 08:53:39 -0700474 SkAutoTDelete<SkStream> stream(resource(path));
475 if (!stream) {
476 SkDebugf("Missing resource '%s'\n", path);
477 return;
478 }
479 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700480 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700481}
msarette16b04a2015-04-15 07:32:19 -0700482
msarett4b17fa32015-04-23 08:53:39 -0700483DEF_TEST(Codec_Empty, r) {
484 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700485 test_invalid(r, "empty_images/zero-dims.gif");
486 test_invalid(r, "empty_images/zero-embedded.ico");
487 test_invalid(r, "empty_images/zero-width.bmp");
488 test_invalid(r, "empty_images/zero-height.bmp");
489 test_invalid(r, "empty_images/zero-width.jpg");
490 test_invalid(r, "empty_images/zero-height.jpg");
491 test_invalid(r, "empty_images/zero-width.png");
492 test_invalid(r, "empty_images/zero-height.png");
493 test_invalid(r, "empty_images/zero-width.wbmp");
494 test_invalid(r, "empty_images/zero-height.wbmp");
495 // This image is an ico with an embedded mask-bmp. This is illegal.
496 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700497}
msarett99f567e2015-08-05 12:58:26 -0700498
499static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
500 SkAutoTDelete<SkStream> stream(resource(path));
501 if (!stream) {
502 SkDebugf("Missing resource '%s'\n", path);
503 return;
504 }
scroggo46c57472015-09-30 08:57:13 -0700505 SkAutoTDelete<SkCodec> decoder(SkCodec::NewFromStream(stream.detach()));
msarett99f567e2015-08-05 12:58:26 -0700506
507 // This should return kSuccess because kIndex8 is supported.
508 SkPMColor colorStorage[256];
509 int colorCount;
scroggo46c57472015-09-30 08:57:13 -0700510 SkCodec::Result result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700511 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
msarett99f567e2015-08-05 12:58:26 -0700512 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
513 // The rest of the test is uninteresting if kIndex8 is not supported
514 if (SkCodec::kSuccess != result) {
515 return;
516 }
517
518 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
519 // colorPtr and a valid colorCountPtr.
scroggo46c57472015-09-30 08:57:13 -0700520 result = decoder->startScanlineDecode(
halcanary96fcdcc2015-08-27 07:41:13 -0700521 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
msarett99f567e2015-08-05 12:58:26 -0700522 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
scroggo46c57472015-09-30 08:57:13 -0700523 result = decoder->startScanlineDecode(
msarett99f567e2015-08-05 12:58:26 -0700524 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
525 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
526}
527
528DEF_TEST(Codec_Params, r) {
529 test_invalid_parameters(r, "index8.png");
530 test_invalid_parameters(r, "mandrill.wbmp");
531}