halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 1 | /* |
| 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" |
| 12 | #include "Test.h" |
| 13 | |
| 14 | static SkStreamAsset* resource(const char path[]) { |
| 15 | SkString fullPath = GetResourcePath(path); |
| 16 | return SkStream::NewFromFile(fullPath.c_str()); |
| 17 | } |
| 18 | |
| 19 | static void md5(const SkBitmap& bm, SkMD5::Digest* digest) { |
| 20 | SkAutoLockPixels autoLockPixels(bm); |
| 21 | SkASSERT(bm.getPixels()); |
| 22 | SkMD5 md5; |
| 23 | size_t rowLen = bm.info().bytesPerPixel() * bm.width(); |
| 24 | for (int y = 0; y < bm.height(); ++y) { |
| 25 | md5.update(static_cast<uint8_t*>(bm.getAddr(0, y)), rowLen); |
| 26 | } |
| 27 | md5.finish(*digest); |
| 28 | } |
| 29 | |
| 30 | static void check(skiatest::Reporter* r, |
| 31 | const char path[], |
| 32 | SkISize size, |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 33 | bool supportsScanlineDecoding) { |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 34 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 35 | if (!stream) { |
| 36 | SkDebugf("Missing resource '%s'\n", path); |
| 37 | return; |
| 38 | } |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 39 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach())); |
| 40 | if (!codec) { |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 41 | ERRORF(r, "Unable to decode '%s'", path); |
| 42 | return; |
| 43 | } |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 44 | SkImageInfo info = codec->getInfo(); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 45 | REPORTER_ASSERT(r, info.dimensions() == size); |
| 46 | SkBitmap bm; |
| 47 | bm.allocPixels(info); |
| 48 | SkAutoLockPixels autoLockPixels(bm); |
| 49 | SkImageGenerator::Result result = |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 50 | codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 51 | REPORTER_ASSERT(r, result == SkImageGenerator::kSuccess); |
| 52 | |
| 53 | SkMD5::Digest digest1, digest2; |
| 54 | md5(bm, &digest1); |
| 55 | |
| 56 | bm.eraseColor(SK_ColorYELLOW); |
| 57 | |
| 58 | result = |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 59 | codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 60 | |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 61 | REPORTER_ASSERT(r, result == SkImageGenerator::kSuccess); |
| 62 | // verify that re-decoding gives the same result. |
| 63 | md5(bm, &digest2); |
| 64 | REPORTER_ASSERT(r, digest1 == digest2); |
| 65 | |
| 66 | SkScanlineDecoder* scanlineDecoder = codec->getScanlineDecoder(info); |
| 67 | if (supportsScanlineDecoding) { |
| 68 | bm.eraseColor(SK_ColorYELLOW); |
| 69 | REPORTER_ASSERT(r, scanlineDecoder); |
| 70 | for (int y = 0; y < info.height(); y++) { |
| 71 | result = scanlineDecoder->getScanlines(bm.getAddr(0, y), 1, 0); |
| 72 | REPORTER_ASSERT(r, result == SkImageGenerator::kSuccess); |
| 73 | } |
| 74 | // verify that scanline decoding gives the same result. |
| 75 | SkMD5::Digest digest3; |
| 76 | md5(bm, &digest3); |
| 77 | REPORTER_ASSERT(r, digest3 == digest1); |
| 78 | } else { |
| 79 | REPORTER_ASSERT(r, !scanlineDecoder); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | DEF_TEST(Codec, r) { |
| 84 | // WBMP |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 85 | check(r, "mandrill.wbmp", SkISize::Make(512, 512), false); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 86 | |
| 87 | // BMP |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 88 | check(r, "randPixels.bmp", SkISize::Make(8, 8), false); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 89 | |
| 90 | // ICO |
msarett | 68b204e | 2015-04-01 12:09:21 -0700 | [diff] [blame] | 91 | // These two tests examine interestingly different behavior: |
| 92 | // Decodes an embedded BMP image |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 93 | check(r, "color_wheel.ico", SkISize::Make(128, 128), false); |
msarett | 68b204e | 2015-04-01 12:09:21 -0700 | [diff] [blame] | 94 | // Decodes an embedded PNG image |
| 95 | check(r, "google_chrome.ico", SkISize::Make(256, 256), false); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 96 | |
| 97 | // PNG |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 98 | check(r, "arrow.png", SkISize::Make(187, 312), true); |
| 99 | check(r, "baby_tux.png", SkISize::Make(240, 246), true); |
| 100 | check(r, "color_wheel.png", SkISize::Make(128, 128), true); |
| 101 | check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true); |
| 102 | check(r, "mandrill_128.png", SkISize::Make(128, 128), true); |
| 103 | check(r, "mandrill_16.png", SkISize::Make(16, 16), true); |
| 104 | check(r, "mandrill_256.png", SkISize::Make(256, 256), true); |
| 105 | check(r, "mandrill_32.png", SkISize::Make(32, 32), true); |
| 106 | check(r, "mandrill_512.png", SkISize::Make(512, 512), true); |
| 107 | check(r, "mandrill_64.png", SkISize::Make(64, 64), true); |
| 108 | check(r, "plane.png", SkISize::Make(250, 126), true); |
| 109 | check(r, "randPixels.png", SkISize::Make(8, 8), true); |
| 110 | check(r, "yellow_rose.png", SkISize::Make(400, 301), true); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 111 | } |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame^] | 112 | |
| 113 | static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) { |
| 114 | SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false)); |
| 115 | // We should not have gotten a codec. Bots should catch us if we leaked anything. |
| 116 | REPORTER_ASSERT(r, !codec); |
| 117 | } |
| 118 | |
| 119 | // Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream, |
| 120 | // even on failure. Test some bad streams. |
| 121 | DEF_TEST(Codec_leaks, r) { |
| 122 | // No codec should claim this as their format, so this tests SkCodec::NewFromStream. |
| 123 | const char nonSupportedStream[] = "hello world"; |
| 124 | // The other strings should look like the beginning of a file type, so we'll call some |
| 125 | // internal version of NewFromStream, which must also delete the stream on failure. |
| 126 | const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }; |
| 127 | const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF }; |
| 128 | const char emptyWebp[] = "RIFF1234WEBPVP"; |
| 129 | const char emptyBmp[] = { 'B', 'M' }; |
| 130 | const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' }; |
| 131 | const char emptyGif[] = "GIFVER"; |
| 132 | |
| 133 | test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream)); |
| 134 | test_invalid_stream(r, emptyPng, sizeof(emptyPng)); |
| 135 | test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg)); |
| 136 | test_invalid_stream(r, emptyWebp, sizeof(emptyWebp)); |
| 137 | test_invalid_stream(r, emptyBmp, sizeof(emptyBmp)); |
| 138 | test_invalid_stream(r, emptyIco, sizeof(emptyIco)); |
| 139 | test_invalid_stream(r, emptyGif, sizeof(emptyGif)); |
| 140 | } |