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" |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame^] | 12 | #include "SkScanlineDecoder.h" |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 13 | #include "Test.h" |
| 14 | |
| 15 | static SkStreamAsset* resource(const char path[]) { |
| 16 | SkString fullPath = GetResourcePath(path); |
| 17 | return SkStream::NewFromFile(fullPath.c_str()); |
| 18 | } |
| 19 | |
| 20 | static void md5(const SkBitmap& bm, SkMD5::Digest* digest) { |
| 21 | SkAutoLockPixels autoLockPixels(bm); |
| 22 | SkASSERT(bm.getPixels()); |
| 23 | SkMD5 md5; |
| 24 | size_t rowLen = bm.info().bytesPerPixel() * bm.width(); |
| 25 | for (int y = 0; y < bm.height(); ++y) { |
| 26 | md5.update(static_cast<uint8_t*>(bm.getAddr(0, y)), rowLen); |
| 27 | } |
| 28 | md5.finish(*digest); |
| 29 | } |
| 30 | |
| 31 | static void check(skiatest::Reporter* r, |
| 32 | const char path[], |
| 33 | SkISize size, |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 34 | bool supportsScanlineDecoding) { |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 35 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 36 | if (!stream) { |
| 37 | SkDebugf("Missing resource '%s'\n", path); |
| 38 | return; |
| 39 | } |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 40 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach())); |
| 41 | if (!codec) { |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 42 | ERRORF(r, "Unable to decode '%s'", path); |
| 43 | return; |
| 44 | } |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 45 | |
| 46 | // This test is used primarily to verify rewinding works properly. Using kN32 allows |
| 47 | // us to test this without the added overhead of creating different bitmaps depending |
| 48 | // on the color type (ex: building a color table for kIndex8). DM is where we test |
| 49 | // decodes to all possible destination color types. |
| 50 | SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 51 | REPORTER_ASSERT(r, info.dimensions() == size); |
| 52 | SkBitmap bm; |
| 53 | bm.allocPixels(info); |
| 54 | SkAutoLockPixels autoLockPixels(bm); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame^] | 55 | SkCodec::Result result = |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 56 | codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame^] | 57 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 58 | |
| 59 | SkMD5::Digest digest1, digest2; |
| 60 | md5(bm, &digest1); |
| 61 | |
| 62 | bm.eraseColor(SK_ColorYELLOW); |
| 63 | |
| 64 | result = |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 65 | codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 66 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame^] | 67 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 68 | // verify that re-decoding gives the same result. |
| 69 | md5(bm, &digest2); |
| 70 | REPORTER_ASSERT(r, digest1 == digest2); |
| 71 | |
| 72 | SkScanlineDecoder* scanlineDecoder = codec->getScanlineDecoder(info); |
| 73 | if (supportsScanlineDecoding) { |
| 74 | bm.eraseColor(SK_ColorYELLOW); |
| 75 | REPORTER_ASSERT(r, scanlineDecoder); |
msarett | c0e80c1 | 2015-07-01 06:50:35 -0700 | [diff] [blame] | 76 | |
| 77 | // Regular decodes should be disabled after creating a scanline decoder |
| 78 | result = codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame^] | 79 | REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 80 | for (int y = 0; y < info.height(); y++) { |
| 81 | result = scanlineDecoder->getScanlines(bm.getAddr(0, y), 1, 0); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame^] | 82 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 83 | } |
| 84 | // verify that scanline decoding gives the same result. |
| 85 | SkMD5::Digest digest3; |
| 86 | md5(bm, &digest3); |
| 87 | REPORTER_ASSERT(r, digest3 == digest1); |
| 88 | } else { |
| 89 | REPORTER_ASSERT(r, !scanlineDecoder); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | DEF_TEST(Codec, r) { |
| 94 | // WBMP |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 95 | check(r, "mandrill.wbmp", SkISize::Make(512, 512), false); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 96 | |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 97 | // WEBP |
| 98 | check(r, "baby_tux.webp", SkISize::Make(386, 395), false); |
| 99 | check(r, "color_wheel.webp", SkISize::Make(128, 128), false); |
| 100 | check(r, "yellow_rose.webp", SkISize::Make(400, 301), false); |
| 101 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 102 | // BMP |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 103 | check(r, "randPixels.bmp", SkISize::Make(8, 8), false); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 104 | |
| 105 | // ICO |
msarett | 68b204e | 2015-04-01 12:09:21 -0700 | [diff] [blame] | 106 | // These two tests examine interestingly different behavior: |
| 107 | // Decodes an embedded BMP image |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 108 | check(r, "color_wheel.ico", SkISize::Make(128, 128), false); |
msarett | 68b204e | 2015-04-01 12:09:21 -0700 | [diff] [blame] | 109 | // Decodes an embedded PNG image |
| 110 | check(r, "google_chrome.ico", SkISize::Make(256, 256), false); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 111 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 112 | // GIF |
| 113 | check(r, "box.gif", SkISize::Make(200, 55), false); |
| 114 | check(r, "color_wheel.gif", SkISize::Make(128, 128), false); |
| 115 | check(r, "randPixels.gif", SkISize::Make(8, 8), false); |
| 116 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 117 | // JPG |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 118 | check(r, "CMYK.jpg", SkISize::Make(642, 516), true); |
| 119 | check(r, "color_wheel.jpg", SkISize::Make(128, 128), true); |
| 120 | check(r, "grayscale.jpg", SkISize::Make(128, 128), true); |
| 121 | check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true); |
| 122 | check(r, "randPixels.jpg", SkISize::Make(8, 8), true); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 123 | |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 124 | // PNG |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 125 | check(r, "arrow.png", SkISize::Make(187, 312), true); |
| 126 | check(r, "baby_tux.png", SkISize::Make(240, 246), true); |
| 127 | check(r, "color_wheel.png", SkISize::Make(128, 128), true); |
| 128 | check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true); |
| 129 | check(r, "mandrill_128.png", SkISize::Make(128, 128), true); |
| 130 | check(r, "mandrill_16.png", SkISize::Make(16, 16), true); |
| 131 | check(r, "mandrill_256.png", SkISize::Make(256, 256), true); |
| 132 | check(r, "mandrill_32.png", SkISize::Make(32, 32), true); |
| 133 | check(r, "mandrill_512.png", SkISize::Make(512, 512), true); |
| 134 | check(r, "mandrill_64.png", SkISize::Make(64, 64), true); |
| 135 | check(r, "plane.png", SkISize::Make(250, 126), true); |
| 136 | check(r, "randPixels.png", SkISize::Make(8, 8), true); |
| 137 | check(r, "yellow_rose.png", SkISize::Make(400, 301), true); |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 138 | } |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 139 | |
| 140 | static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) { |
| 141 | SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false)); |
| 142 | // We should not have gotten a codec. Bots should catch us if we leaked anything. |
| 143 | REPORTER_ASSERT(r, !codec); |
| 144 | } |
| 145 | |
| 146 | // Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream, |
| 147 | // even on failure. Test some bad streams. |
| 148 | DEF_TEST(Codec_leaks, r) { |
| 149 | // No codec should claim this as their format, so this tests SkCodec::NewFromStream. |
| 150 | const char nonSupportedStream[] = "hello world"; |
| 151 | // The other strings should look like the beginning of a file type, so we'll call some |
| 152 | // internal version of NewFromStream, which must also delete the stream on failure. |
| 153 | const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }; |
| 154 | const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF }; |
| 155 | const char emptyWebp[] = "RIFF1234WEBPVP"; |
| 156 | const char emptyBmp[] = { 'B', 'M' }; |
| 157 | const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' }; |
| 158 | const char emptyGif[] = "GIFVER"; |
| 159 | |
| 160 | test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream)); |
| 161 | test_invalid_stream(r, emptyPng, sizeof(emptyPng)); |
| 162 | test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg)); |
| 163 | test_invalid_stream(r, emptyWebp, sizeof(emptyWebp)); |
| 164 | test_invalid_stream(r, emptyBmp, sizeof(emptyBmp)); |
| 165 | test_invalid_stream(r, emptyIco, sizeof(emptyIco)); |
| 166 | test_invalid_stream(r, emptyGif, sizeof(emptyGif)); |
| 167 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 168 | |
| 169 | static void test_dimensions(skiatest::Reporter* r, const char path[]) { |
| 170 | // Create the codec from the resource file |
| 171 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 172 | if (!stream) { |
| 173 | SkDebugf("Missing resource '%s'\n", path); |
| 174 | return; |
| 175 | } |
| 176 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach())); |
| 177 | if (!codec) { |
| 178 | ERRORF(r, "Unable to create codec '%s'", path); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | // Check that the decode is successful for a variety of scales |
| 183 | for (float scale = -0.05f; scale < 2.0f; scale += 0.05f) { |
| 184 | // Scale the output dimensions |
| 185 | SkISize scaledDims = codec->getScaledDimensions(scale); |
| 186 | SkImageInfo scaledInfo = codec->getInfo().makeWH(scaledDims.width(), scaledDims.height()); |
| 187 | |
| 188 | // Set up for the decode |
| 189 | size_t rowBytes = scaledDims.width() * sizeof(SkPMColor); |
| 190 | size_t totalBytes = scaledInfo.getSafeSize(rowBytes); |
| 191 | SkAutoTMalloc<SkPMColor> pixels(totalBytes); |
| 192 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame^] | 193 | SkCodec::Result result = |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 194 | codec->getPixels(scaledInfo, pixels.get(), rowBytes, NULL, NULL, NULL); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame^] | 195 | REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | // Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes |
| 200 | DEF_TEST(Codec_Dimensions, r) { |
| 201 | // JPG |
| 202 | test_dimensions(r, "CMYK.jpg"); |
| 203 | test_dimensions(r, "color_wheel.jpg"); |
| 204 | test_dimensions(r, "grayscale.jpg"); |
| 205 | test_dimensions(r, "mandrill_512_q075.jpg"); |
| 206 | test_dimensions(r, "randPixels.jpg"); |
| 207 | } |
| 208 | |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 209 | static void test_empty(skiatest::Reporter* r, const char path[]) { |
| 210 | SkAutoTDelete<SkStream> stream(resource(path)); |
| 211 | if (!stream) { |
| 212 | SkDebugf("Missing resource '%s'\n", path); |
| 213 | return; |
| 214 | } |
| 215 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach())); |
| 216 | REPORTER_ASSERT(r, NULL == codec); |
| 217 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 218 | |
msarett | 4b17fa3 | 2015-04-23 08:53:39 -0700 | [diff] [blame] | 219 | DEF_TEST(Codec_Empty, r) { |
| 220 | // Test images that should not be able to create a codec |
| 221 | test_empty(r, "empty_images/zero-dims.gif"); |
| 222 | test_empty(r, "empty_images/zero-embedded.ico"); |
| 223 | test_empty(r, "empty_images/zero-width.bmp"); |
| 224 | test_empty(r, "empty_images/zero-height.bmp"); |
| 225 | test_empty(r, "empty_images/zero-width.jpg"); |
| 226 | test_empty(r, "empty_images/zero-height.jpg"); |
| 227 | test_empty(r, "empty_images/zero-width.png"); |
| 228 | test_empty(r, "empty_images/zero-height.png"); |
| 229 | test_empty(r, "empty_images/zero-width.wbmp"); |
| 230 | test_empty(r, "empty_images/zero-height.wbmp"); |
| 231 | } |