blob: 27c48fb4b88919f22fb88853bb2bf5c626946b43 [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"
scroggoeb602a52015-07-09 08:16:03 -070014#include "SkScanlineDecoder.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
halcanarya096d7a2015-03-27 12:16:53 -070078static void check(skiatest::Reporter* r,
79 const char path[],
80 SkISize size,
scroggob636b452015-07-22 07:16:20 -070081 bool supportsScanlineDecoding,
scroggocc2feb12015-08-14 08:32:46 -070082 bool supportsSubsetDecoding,
83 bool supports565 = true) {
halcanarya096d7a2015-03-27 12:16:53 -070084 SkAutoTDelete<SkStream> stream(resource(path));
85 if (!stream) {
86 SkDebugf("Missing resource '%s'\n", path);
87 return;
88 }
scroggo58421542015-04-01 11:25:20 -070089 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
90 if (!codec) {
halcanarya096d7a2015-03-27 12:16:53 -070091 ERRORF(r, "Unable to decode '%s'", path);
92 return;
93 }
msarett438b2ad2015-04-09 12:43:10 -070094
95 // This test is used primarily to verify rewinding works properly. Using kN32 allows
96 // us to test this without the added overhead of creating different bitmaps depending
97 // on the color type (ex: building a color table for kIndex8). DM is where we test
98 // decodes to all possible destination color types.
99 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
halcanarya096d7a2015-03-27 12:16:53 -0700100 REPORTER_ASSERT(r, info.dimensions() == size);
scroggocc2feb12015-08-14 08:32:46 -0700101
102 {
103 // Test decoding to 565
104 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
105 SkCodec::Result expected = (supports565 && info.alphaType() == kOpaque_SkAlphaType) ?
106 SkCodec::kSuccess : SkCodec::kInvalidConversion;
halcanary96fcdcc2015-08-27 07:41:13 -0700107 test_info(r, codec, info565, expected, nullptr);
scroggocc2feb12015-08-14 08:32:46 -0700108 }
109
halcanarya096d7a2015-03-27 12:16:53 -0700110 SkBitmap bm;
111 bm.allocPixels(info);
112 SkAutoLockPixels autoLockPixels(bm);
scroggoeb602a52015-07-09 08:16:03 -0700113 SkCodec::Result result =
halcanary96fcdcc2015-08-27 07:41:13 -0700114 codec->getPixels(info, bm.getPixels(), bm.rowBytes(), nullptr, nullptr, nullptr);
scroggoeb602a52015-07-09 08:16:03 -0700115 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
halcanarya096d7a2015-03-27 12:16:53 -0700116
scroggo9b2cdbf42015-07-10 12:07:02 -0700117 SkMD5::Digest digest;
118 md5(bm, &digest);
halcanarya096d7a2015-03-27 12:16:53 -0700119
scroggo58421542015-04-01 11:25:20 -0700120 // verify that re-decoding gives the same result.
scroggod1bc5742015-08-12 08:31:44 -0700121 test_info(r, codec, info, SkCodec::kSuccess, &digest);
122
123 {
124 // Check alpha type conversions
125 if (info.alphaType() == kOpaque_SkAlphaType) {
126 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700127 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700128 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700129 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700130 } else {
131 // Decoding to opaque should fail
132 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700133 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700134 SkAlphaType otherAt = info.alphaType();
135 if (kPremul_SkAlphaType == otherAt) {
136 otherAt = kUnpremul_SkAlphaType;
137 } else {
138 otherAt = kPremul_SkAlphaType;
139 }
140 // The other non-opaque alpha type should always succeed, but not match.
halcanary96fcdcc2015-08-27 07:41:13 -0700141 test_info(r, codec, info.makeAlphaType(otherAt), SkCodec::kSuccess, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700142 }
143 }
144
145 // Scanline decoding follows.
scroggo58421542015-04-01 11:25:20 -0700146
scroggo1c005e42015-08-04 09:24:45 -0700147 stream.reset(resource(path));
148 SkAutoTDelete<SkScanlineDecoder> scanlineDecoder(
149 SkScanlineDecoder::NewFromStream(stream.detach()));
scroggo58421542015-04-01 11:25:20 -0700150 if (supportsScanlineDecoding) {
151 bm.eraseColor(SK_ColorYELLOW);
152 REPORTER_ASSERT(r, scanlineDecoder);
msarettc0e80c12015-07-01 06:50:35 -0700153
scroggo1c005e42015-08-04 09:24:45 -0700154 REPORTER_ASSERT(r, scanlineDecoder->start(info) == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700155
scroggo58421542015-04-01 11:25:20 -0700156 for (int y = 0; y < info.height(); y++) {
157 result = scanlineDecoder->getScanlines(bm.getAddr(0, y), 1, 0);
scroggoeb602a52015-07-09 08:16:03 -0700158 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
scroggo58421542015-04-01 11:25:20 -0700159 }
160 // verify that scanline decoding gives the same result.
msarett5406d6f2015-08-31 06:55:13 -0700161 if (SkScanlineDecoder::kTopDown_SkScanlineOrder == scanlineDecoder->getScanlineOrder()) {
162 compare_to_good_digest(r, digest, bm);
163 }
scroggo58421542015-04-01 11:25:20 -0700164 } else {
165 REPORTER_ASSERT(r, !scanlineDecoder);
halcanarya096d7a2015-03-27 12:16:53 -0700166 }
scroggob636b452015-07-22 07:16:20 -0700167
168 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
169 // random subsets.
170 // Do not attempt to decode subsets of an image of only once pixel, since there is no
171 // meaningful subset.
172 if (size.width() * size.height() == 1) {
173 return;
174 }
175
176 SkRandom rand;
177 SkIRect subset;
178 SkCodec::Options opts;
179 opts.fSubset = &subset;
180 for (int i = 0; i < 5; i++) {
181 subset = generate_random_subset(&rand, size.width(), size.height());
182 SkASSERT(!subset.isEmpty());
183 const bool supported = codec->getValidSubset(&subset);
184 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
185
186 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
187 SkBitmap bm;
188 bm.allocPixels(subsetInfo);
189 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
halcanary96fcdcc2015-08-27 07:41:13 -0700190 &opts, nullptr, nullptr);
scroggob636b452015-07-22 07:16:20 -0700191
192 if (supportsSubsetDecoding) {
193 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
194 // Webp is the only codec that supports subsets, and it will have modified the subset
195 // to have even left/top.
196 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
197 } else {
198 // No subsets will work.
199 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
200 }
201 }
halcanarya096d7a2015-03-27 12:16:53 -0700202}
203
204DEF_TEST(Codec, r) {
205 // WBMP
msarett99f567e2015-08-05 12:58:26 -0700206 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700207
scroggo6f5e6192015-06-18 12:53:43 -0700208 // WEBP
scroggob636b452015-07-22 07:16:20 -0700209 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true);
210 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true);
211 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true);
scroggo6f5e6192015-06-18 12:53:43 -0700212
halcanarya096d7a2015-03-27 12:16:53 -0700213 // BMP
msarett5406d6f2015-08-31 06:55:13 -0700214 check(r, "randPixels.bmp", SkISize::Make(8, 8), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700215
216 // ICO
msarett68b204e2015-04-01 12:09:21 -0700217 // These two tests examine interestingly different behavior:
218 // Decodes an embedded BMP image
scroggob636b452015-07-22 07:16:20 -0700219 check(r, "color_wheel.ico", SkISize::Make(128, 128), false, false);
msarett68b204e2015-04-01 12:09:21 -0700220 // Decodes an embedded PNG image
scroggob636b452015-07-22 07:16:20 -0700221 check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700222
msarett438b2ad2015-04-09 12:43:10 -0700223 // GIF
scroggob636b452015-07-22 07:16:20 -0700224 check(r, "box.gif", SkISize::Make(200, 55), false, false);
225 check(r, "color_wheel.gif", SkISize::Make(128, 128), false, false);
226 check(r, "randPixels.gif", SkISize::Make(8, 8), false, false);
msarett438b2ad2015-04-09 12:43:10 -0700227
msarette16b04a2015-04-15 07:32:19 -0700228 // JPG
scroggocc2feb12015-08-14 08:32:46 -0700229 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false, false);
scroggob636b452015-07-22 07:16:20 -0700230 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
231 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false);
232 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
233 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false);
msarette16b04a2015-04-15 07:32:19 -0700234
halcanarya096d7a2015-03-27 12:16:53 -0700235 // PNG
scroggob636b452015-07-22 07:16:20 -0700236 check(r, "arrow.png", SkISize::Make(187, 312), true, false);
237 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false);
238 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false);
239 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false);
240 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false);
241 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false);
242 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false);
243 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false);
244 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false);
245 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false);
246 check(r, "plane.png", SkISize::Make(250, 126), true, false);
247 check(r, "randPixels.png", SkISize::Make(8, 8), true, false);
248 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700249}
scroggo0a7e69c2015-04-03 07:22:22 -0700250
251static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
252 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
253 // We should not have gotten a codec. Bots should catch us if we leaked anything.
254 REPORTER_ASSERT(r, !codec);
255}
256
257// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
258// even on failure. Test some bad streams.
259DEF_TEST(Codec_leaks, r) {
260 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
261 const char nonSupportedStream[] = "hello world";
262 // The other strings should look like the beginning of a file type, so we'll call some
263 // internal version of NewFromStream, which must also delete the stream on failure.
264 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
265 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
266 const char emptyWebp[] = "RIFF1234WEBPVP";
267 const char emptyBmp[] = { 'B', 'M' };
268 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
269 const char emptyGif[] = "GIFVER";
270
271 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
272 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
273 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
274 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
275 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
276 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
277 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
278}
msarette16b04a2015-04-15 07:32:19 -0700279
280static void test_dimensions(skiatest::Reporter* r, const char path[]) {
281 // Create the codec from the resource file
282 SkAutoTDelete<SkStream> stream(resource(path));
283 if (!stream) {
284 SkDebugf("Missing resource '%s'\n", path);
285 return;
286 }
msarettb32758a2015-08-18 13:22:46 -0700287 SkAutoTDelete<SkCodec> codec(SkScaledCodec::NewFromStream(stream.detach()));
msarette16b04a2015-04-15 07:32:19 -0700288 if (!codec) {
289 ERRORF(r, "Unable to create codec '%s'", path);
290 return;
291 }
292
293 // Check that the decode is successful for a variety of scales
msarettb32758a2015-08-18 13:22:46 -0700294 for (float scale = 0.05f; scale < 2.0f; scale += 0.05f) {
msarette16b04a2015-04-15 07:32:19 -0700295 // Scale the output dimensions
296 SkISize scaledDims = codec->getScaledDimensions(scale);
msarettb32758a2015-08-18 13:22:46 -0700297 SkImageInfo scaledInfo = codec->getInfo()
298 .makeWH(scaledDims.width(), scaledDims.height())
299 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700300
301 // Set up for the decode
302 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
303 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
304 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
305
scroggoeb602a52015-07-09 08:16:03 -0700306 SkCodec::Result result =
halcanary96fcdcc2015-08-27 07:41:13 -0700307 codec->getPixels(scaledInfo, pixels.get(), rowBytes, nullptr, nullptr, nullptr);
scroggoeb602a52015-07-09 08:16:03 -0700308 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700309 }
310}
311
312// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
313DEF_TEST(Codec_Dimensions, r) {
314 // JPG
315 test_dimensions(r, "CMYK.jpg");
316 test_dimensions(r, "color_wheel.jpg");
317 test_dimensions(r, "grayscale.jpg");
318 test_dimensions(r, "mandrill_512_q075.jpg");
319 test_dimensions(r, "randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700320
321 // Decoding small images with very large scaling factors is a potential
322 // source of bugs and crashes. We disable these tests in Gold because
323 // tiny images are not very useful to look at.
324 // Here we make sure that we do not crash or access illegal memory when
325 // performing scaled decodes on small images.
326 test_dimensions(r, "1x1.png");
327 test_dimensions(r, "2x2.png");
328 test_dimensions(r, "3x3.png");
329 test_dimensions(r, "3x1.png");
330 test_dimensions(r, "1x1.png");
331 test_dimensions(r, "16x1.png");
332 test_dimensions(r, "1x16.png");
333 test_dimensions(r, "mandrill_16.png");
334
msarette16b04a2015-04-15 07:32:19 -0700335}
336
msarettd0375bc2015-08-12 08:08:56 -0700337static void test_invalid(skiatest::Reporter* r, const char path[]) {
msarett4b17fa32015-04-23 08:53:39 -0700338 SkAutoTDelete<SkStream> stream(resource(path));
339 if (!stream) {
340 SkDebugf("Missing resource '%s'\n", path);
341 return;
342 }
343 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
halcanary96fcdcc2015-08-27 07:41:13 -0700344 REPORTER_ASSERT(r, nullptr == codec);
msarett4b17fa32015-04-23 08:53:39 -0700345}
msarette16b04a2015-04-15 07:32:19 -0700346
msarett4b17fa32015-04-23 08:53:39 -0700347DEF_TEST(Codec_Empty, r) {
348 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700349 test_invalid(r, "empty_images/zero-dims.gif");
350 test_invalid(r, "empty_images/zero-embedded.ico");
351 test_invalid(r, "empty_images/zero-width.bmp");
352 test_invalid(r, "empty_images/zero-height.bmp");
353 test_invalid(r, "empty_images/zero-width.jpg");
354 test_invalid(r, "empty_images/zero-height.jpg");
355 test_invalid(r, "empty_images/zero-width.png");
356 test_invalid(r, "empty_images/zero-height.png");
357 test_invalid(r, "empty_images/zero-width.wbmp");
358 test_invalid(r, "empty_images/zero-height.wbmp");
359 // This image is an ico with an embedded mask-bmp. This is illegal.
360 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
msarett4b17fa32015-04-23 08:53:39 -0700361}
msarett99f567e2015-08-05 12:58:26 -0700362
363static void test_invalid_parameters(skiatest::Reporter* r, const char path[]) {
364 SkAutoTDelete<SkStream> stream(resource(path));
365 if (!stream) {
366 SkDebugf("Missing resource '%s'\n", path);
367 return;
368 }
369 SkAutoTDelete<SkScanlineDecoder> decoder(SkScanlineDecoder::NewFromStream(
370 stream.detach()));
371
372 // This should return kSuccess because kIndex8 is supported.
373 SkPMColor colorStorage[256];
374 int colorCount;
375 SkCodec::Result result = decoder->start(
halcanary96fcdcc2015-08-27 07:41:13 -0700376 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, colorStorage, &colorCount);
msarett99f567e2015-08-05 12:58:26 -0700377 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
378 // The rest of the test is uninteresting if kIndex8 is not supported
379 if (SkCodec::kSuccess != result) {
380 return;
381 }
382
383 // This should return kInvalidParameters because, in kIndex_8 mode, we must pass in a valid
384 // colorPtr and a valid colorCountPtr.
385 result = decoder->start(
halcanary96fcdcc2015-08-27 07:41:13 -0700386 decoder->getInfo().makeColorType(kIndex_8_SkColorType), nullptr, nullptr, nullptr);
msarett99f567e2015-08-05 12:58:26 -0700387 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
388 result = decoder->start(
389 decoder->getInfo().makeColorType(kIndex_8_SkColorType));
390 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
391}
392
393DEF_TEST(Codec_Params, r) {
394 test_invalid_parameters(r, "index8.png");
395 test_invalid_parameters(r, "mandrill.wbmp");
396}