blob: f4682055a42d8a67f17a578781774b6536d7e5cb [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"
scroggoeb602a52015-07-09 08:16:03 -070012#include "SkScanlineDecoder.h"
halcanarya096d7a2015-03-27 12:16:53 -070013#include "Test.h"
14
15static SkStreamAsset* resource(const char path[]) {
16 SkString fullPath = GetResourcePath(path);
17 return SkStream::NewFromFile(fullPath.c_str());
18}
19
20static 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
scroggo9b2cdbf42015-07-10 12:07:02 -070031/**
32 * Compute the digest for bm and compare it to a known good digest.
33 * @param r Reporter to assert that bm's digest matches goodDigest.
34 * @param goodDigest The known good digest to compare to.
35 * @param bm The bitmap to test.
36 */
37static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
38 const SkBitmap& bm) {
39 SkMD5::Digest digest;
40 md5(bm, &digest);
41 REPORTER_ASSERT(r, digest == goodDigest);
42}
43
halcanarya096d7a2015-03-27 12:16:53 -070044static void check(skiatest::Reporter* r,
45 const char path[],
46 SkISize size,
scroggo58421542015-04-01 11:25:20 -070047 bool supportsScanlineDecoding) {
halcanarya096d7a2015-03-27 12:16:53 -070048 SkAutoTDelete<SkStream> stream(resource(path));
49 if (!stream) {
50 SkDebugf("Missing resource '%s'\n", path);
51 return;
52 }
scroggo58421542015-04-01 11:25:20 -070053 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
54 if (!codec) {
halcanarya096d7a2015-03-27 12:16:53 -070055 ERRORF(r, "Unable to decode '%s'", path);
56 return;
57 }
msarett438b2ad2015-04-09 12:43:10 -070058
59 // This test is used primarily to verify rewinding works properly. Using kN32 allows
60 // us to test this without the added overhead of creating different bitmaps depending
61 // on the color type (ex: building a color table for kIndex8). DM is where we test
62 // decodes to all possible destination color types.
63 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
halcanarya096d7a2015-03-27 12:16:53 -070064 REPORTER_ASSERT(r, info.dimensions() == size);
65 SkBitmap bm;
66 bm.allocPixels(info);
67 SkAutoLockPixels autoLockPixels(bm);
scroggoeb602a52015-07-09 08:16:03 -070068 SkCodec::Result result =
scroggo58421542015-04-01 11:25:20 -070069 codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL);
scroggoeb602a52015-07-09 08:16:03 -070070 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
halcanarya096d7a2015-03-27 12:16:53 -070071
scroggo9b2cdbf42015-07-10 12:07:02 -070072 SkMD5::Digest digest;
73 md5(bm, &digest);
halcanarya096d7a2015-03-27 12:16:53 -070074
75 bm.eraseColor(SK_ColorYELLOW);
76
77 result =
scroggo58421542015-04-01 11:25:20 -070078 codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL);
halcanarya096d7a2015-03-27 12:16:53 -070079
scroggoeb602a52015-07-09 08:16:03 -070080 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
scroggo58421542015-04-01 11:25:20 -070081 // verify that re-decoding gives the same result.
scroggo9b2cdbf42015-07-10 12:07:02 -070082 compare_to_good_digest(r, digest, bm);
scroggo58421542015-04-01 11:25:20 -070083
scroggo9b2cdbf42015-07-10 12:07:02 -070084 SkAutoTDelete<SkScanlineDecoder> scanlineDecoder(codec->getScanlineDecoder(info));
scroggo58421542015-04-01 11:25:20 -070085 if (supportsScanlineDecoding) {
86 bm.eraseColor(SK_ColorYELLOW);
87 REPORTER_ASSERT(r, scanlineDecoder);
msarettc0e80c12015-07-01 06:50:35 -070088
scroggo9b2cdbf42015-07-10 12:07:02 -070089 // Regular decodes should not be affected by creating a scanline decoder
msarettc0e80c12015-07-01 06:50:35 -070090 result = codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL);
scroggo9b2cdbf42015-07-10 12:07:02 -070091 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
92 compare_to_good_digest(r, digest, bm);
93
94 bm.eraseColor(SK_ColorYELLOW);
95
scroggo58421542015-04-01 11:25:20 -070096 for (int y = 0; y < info.height(); y++) {
97 result = scanlineDecoder->getScanlines(bm.getAddr(0, y), 1, 0);
scroggoeb602a52015-07-09 08:16:03 -070098 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
scroggo58421542015-04-01 11:25:20 -070099 }
100 // verify that scanline decoding gives the same result.
scroggo9b2cdbf42015-07-10 12:07:02 -0700101 compare_to_good_digest(r, digest, bm);
scroggo58421542015-04-01 11:25:20 -0700102 } else {
103 REPORTER_ASSERT(r, !scanlineDecoder);
halcanarya096d7a2015-03-27 12:16:53 -0700104 }
105}
106
107DEF_TEST(Codec, r) {
108 // WBMP
scroggo58421542015-04-01 11:25:20 -0700109 check(r, "mandrill.wbmp", SkISize::Make(512, 512), false);
halcanarya096d7a2015-03-27 12:16:53 -0700110
scroggo6f5e6192015-06-18 12:53:43 -0700111 // WEBP
112 check(r, "baby_tux.webp", SkISize::Make(386, 395), false);
113 check(r, "color_wheel.webp", SkISize::Make(128, 128), false);
114 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false);
115
halcanarya096d7a2015-03-27 12:16:53 -0700116 // BMP
scroggo58421542015-04-01 11:25:20 -0700117 check(r, "randPixels.bmp", SkISize::Make(8, 8), false);
halcanarya096d7a2015-03-27 12:16:53 -0700118
119 // ICO
msarett68b204e2015-04-01 12:09:21 -0700120 // These two tests examine interestingly different behavior:
121 // Decodes an embedded BMP image
scroggo58421542015-04-01 11:25:20 -0700122 check(r, "color_wheel.ico", SkISize::Make(128, 128), false);
msarett68b204e2015-04-01 12:09:21 -0700123 // Decodes an embedded PNG image
124 check(r, "google_chrome.ico", SkISize::Make(256, 256), false);
halcanarya096d7a2015-03-27 12:16:53 -0700125
msarett438b2ad2015-04-09 12:43:10 -0700126 // GIF
127 check(r, "box.gif", SkISize::Make(200, 55), false);
128 check(r, "color_wheel.gif", SkISize::Make(128, 128), false);
129 check(r, "randPixels.gif", SkISize::Make(8, 8), false);
130
msarette16b04a2015-04-15 07:32:19 -0700131 // JPG
msarett97fdea62015-04-29 08:17:15 -0700132 check(r, "CMYK.jpg", SkISize::Make(642, 516), true);
133 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true);
134 check(r, "grayscale.jpg", SkISize::Make(128, 128), true);
135 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true);
136 check(r, "randPixels.jpg", SkISize::Make(8, 8), true);
msarette16b04a2015-04-15 07:32:19 -0700137
halcanarya096d7a2015-03-27 12:16:53 -0700138 // PNG
scroggo3eada2a2015-04-01 09:33:23 -0700139 check(r, "arrow.png", SkISize::Make(187, 312), true);
140 check(r, "baby_tux.png", SkISize::Make(240, 246), true);
141 check(r, "color_wheel.png", SkISize::Make(128, 128), true);
142 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true);
143 check(r, "mandrill_128.png", SkISize::Make(128, 128), true);
144 check(r, "mandrill_16.png", SkISize::Make(16, 16), true);
145 check(r, "mandrill_256.png", SkISize::Make(256, 256), true);
146 check(r, "mandrill_32.png", SkISize::Make(32, 32), true);
147 check(r, "mandrill_512.png", SkISize::Make(512, 512), true);
148 check(r, "mandrill_64.png", SkISize::Make(64, 64), true);
149 check(r, "plane.png", SkISize::Make(250, 126), true);
150 check(r, "randPixels.png", SkISize::Make(8, 8), true);
151 check(r, "yellow_rose.png", SkISize::Make(400, 301), true);
halcanarya096d7a2015-03-27 12:16:53 -0700152}
scroggo0a7e69c2015-04-03 07:22:22 -0700153
154static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
155 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
156 // We should not have gotten a codec. Bots should catch us if we leaked anything.
157 REPORTER_ASSERT(r, !codec);
158}
159
160// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
161// even on failure. Test some bad streams.
162DEF_TEST(Codec_leaks, r) {
163 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
164 const char nonSupportedStream[] = "hello world";
165 // The other strings should look like the beginning of a file type, so we'll call some
166 // internal version of NewFromStream, which must also delete the stream on failure.
167 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
168 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
169 const char emptyWebp[] = "RIFF1234WEBPVP";
170 const char emptyBmp[] = { 'B', 'M' };
171 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
172 const char emptyGif[] = "GIFVER";
173
174 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
175 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
176 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
177 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
178 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
179 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
180 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
181}
msarette16b04a2015-04-15 07:32:19 -0700182
183static void test_dimensions(skiatest::Reporter* r, const char path[]) {
184 // Create the codec from the resource file
185 SkAutoTDelete<SkStream> stream(resource(path));
186 if (!stream) {
187 SkDebugf("Missing resource '%s'\n", path);
188 return;
189 }
190 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
191 if (!codec) {
192 ERRORF(r, "Unable to create codec '%s'", path);
193 return;
194 }
195
196 // Check that the decode is successful for a variety of scales
197 for (float scale = -0.05f; scale < 2.0f; scale += 0.05f) {
198 // Scale the output dimensions
199 SkISize scaledDims = codec->getScaledDimensions(scale);
200 SkImageInfo scaledInfo = codec->getInfo().makeWH(scaledDims.width(), scaledDims.height());
201
202 // Set up for the decode
203 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
204 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
205 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
206
scroggoeb602a52015-07-09 08:16:03 -0700207 SkCodec::Result result =
msarette16b04a2015-04-15 07:32:19 -0700208 codec->getPixels(scaledInfo, pixels.get(), rowBytes, NULL, NULL, NULL);
scroggoeb602a52015-07-09 08:16:03 -0700209 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700210 }
211}
212
213// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
214DEF_TEST(Codec_Dimensions, r) {
215 // JPG
216 test_dimensions(r, "CMYK.jpg");
217 test_dimensions(r, "color_wheel.jpg");
218 test_dimensions(r, "grayscale.jpg");
219 test_dimensions(r, "mandrill_512_q075.jpg");
220 test_dimensions(r, "randPixels.jpg");
221}
222
msarett4b17fa32015-04-23 08:53:39 -0700223static void test_empty(skiatest::Reporter* r, const char path[]) {
224 SkAutoTDelete<SkStream> stream(resource(path));
225 if (!stream) {
226 SkDebugf("Missing resource '%s'\n", path);
227 return;
228 }
229 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
230 REPORTER_ASSERT(r, NULL == codec);
231}
msarette16b04a2015-04-15 07:32:19 -0700232
msarett4b17fa32015-04-23 08:53:39 -0700233DEF_TEST(Codec_Empty, r) {
234 // Test images that should not be able to create a codec
235 test_empty(r, "empty_images/zero-dims.gif");
236 test_empty(r, "empty_images/zero-embedded.ico");
237 test_empty(r, "empty_images/zero-width.bmp");
238 test_empty(r, "empty_images/zero-height.bmp");
239 test_empty(r, "empty_images/zero-width.jpg");
240 test_empty(r, "empty_images/zero-height.jpg");
241 test_empty(r, "empty_images/zero-width.png");
242 test_empty(r, "empty_images/zero-height.png");
243 test_empty(r, "empty_images/zero-width.wbmp");
244 test_empty(r, "empty_images/zero-height.wbmp");
245}