blob: 82e490a2bd5676d2eb69be89cdd5e5d69eb33725 [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"
scroggoeb602a52015-07-09 08:16:03 -070013#include "SkScanlineDecoder.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
scroggo9b2cdbf2015-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
scroggob636b452015-07-22 07:16:20 -070045SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
46 SkIRect rect;
47 do {
48 rect.fLeft = rand->nextRangeU(0, w);
49 rect.fTop = rand->nextRangeU(0, h);
50 rect.fRight = rand->nextRangeU(0, w);
51 rect.fBottom = rand->nextRangeU(0, h);
52 rect.sort();
53 } while (rect.isEmpty());
54 return rect;
55}
56
halcanarya096d7a2015-03-27 12:16:53 -070057static void check(skiatest::Reporter* r,
58 const char path[],
59 SkISize size,
scroggob636b452015-07-22 07:16:20 -070060 bool supportsScanlineDecoding,
61 bool supportsSubsetDecoding) {
halcanarya096d7a2015-03-27 12:16:53 -070062 SkAutoTDelete<SkStream> stream(resource(path));
63 if (!stream) {
64 SkDebugf("Missing resource '%s'\n", path);
65 return;
66 }
scroggo58421542015-04-01 11:25:20 -070067 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
68 if (!codec) {
halcanarya096d7a2015-03-27 12:16:53 -070069 ERRORF(r, "Unable to decode '%s'", path);
70 return;
71 }
msarett438b2ad2015-04-09 12:43:10 -070072
73 // This test is used primarily to verify rewinding works properly. Using kN32 allows
74 // us to test this without the added overhead of creating different bitmaps depending
75 // on the color type (ex: building a color table for kIndex8). DM is where we test
76 // decodes to all possible destination color types.
77 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
halcanarya096d7a2015-03-27 12:16:53 -070078 REPORTER_ASSERT(r, info.dimensions() == size);
79 SkBitmap bm;
80 bm.allocPixels(info);
81 SkAutoLockPixels autoLockPixels(bm);
scroggoeb602a52015-07-09 08:16:03 -070082 SkCodec::Result result =
scroggo58421542015-04-01 11:25:20 -070083 codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL);
scroggoeb602a52015-07-09 08:16:03 -070084 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
halcanarya096d7a2015-03-27 12:16:53 -070085
scroggo9b2cdbf2015-07-10 12:07:02 -070086 SkMD5::Digest digest;
87 md5(bm, &digest);
halcanarya096d7a2015-03-27 12:16:53 -070088
89 bm.eraseColor(SK_ColorYELLOW);
90
91 result =
scroggo58421542015-04-01 11:25:20 -070092 codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL);
halcanarya096d7a2015-03-27 12:16:53 -070093
scroggoeb602a52015-07-09 08:16:03 -070094 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
scroggo58421542015-04-01 11:25:20 -070095 // verify that re-decoding gives the same result.
scroggo9b2cdbf2015-07-10 12:07:02 -070096 compare_to_good_digest(r, digest, bm);
scroggo58421542015-04-01 11:25:20 -070097
scroggo9b2cdbf2015-07-10 12:07:02 -070098 SkAutoTDelete<SkScanlineDecoder> scanlineDecoder(codec->getScanlineDecoder(info));
scroggo58421542015-04-01 11:25:20 -070099 if (supportsScanlineDecoding) {
100 bm.eraseColor(SK_ColorYELLOW);
101 REPORTER_ASSERT(r, scanlineDecoder);
msarettc0e80c12015-07-01 06:50:35 -0700102
scroggo9b2cdbf2015-07-10 12:07:02 -0700103 // Regular decodes should not be affected by creating a scanline decoder
msarettc0e80c12015-07-01 06:50:35 -0700104 result = codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL);
scroggo9b2cdbf2015-07-10 12:07:02 -0700105 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
106 compare_to_good_digest(r, digest, bm);
107
108 bm.eraseColor(SK_ColorYELLOW);
109
scroggo58421542015-04-01 11:25:20 -0700110 for (int y = 0; y < info.height(); y++) {
111 result = scanlineDecoder->getScanlines(bm.getAddr(0, y), 1, 0);
scroggoeb602a52015-07-09 08:16:03 -0700112 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
scroggo58421542015-04-01 11:25:20 -0700113 }
114 // verify that scanline decoding gives the same result.
scroggo9b2cdbf2015-07-10 12:07:02 -0700115 compare_to_good_digest(r, digest, bm);
scroggo58421542015-04-01 11:25:20 -0700116 } else {
117 REPORTER_ASSERT(r, !scanlineDecoder);
halcanarya096d7a2015-03-27 12:16:53 -0700118 }
scroggob636b452015-07-22 07:16:20 -0700119
120 // The rest of this function tests decoding subsets, and will decode an arbitrary number of
121 // random subsets.
122 // Do not attempt to decode subsets of an image of only once pixel, since there is no
123 // meaningful subset.
124 if (size.width() * size.height() == 1) {
125 return;
126 }
127
128 SkRandom rand;
129 SkIRect subset;
130 SkCodec::Options opts;
131 opts.fSubset = &subset;
132 for (int i = 0; i < 5; i++) {
133 subset = generate_random_subset(&rand, size.width(), size.height());
134 SkASSERT(!subset.isEmpty());
135 const bool supported = codec->getValidSubset(&subset);
136 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
137
138 SkImageInfo subsetInfo = info.makeWH(subset.width(), subset.height());
139 SkBitmap bm;
140 bm.allocPixels(subsetInfo);
141 const SkCodec::Result result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(),
142 &opts, NULL, NULL);
143
144 if (supportsSubsetDecoding) {
145 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
146 // Webp is the only codec that supports subsets, and it will have modified the subset
147 // to have even left/top.
148 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
149 } else {
150 // No subsets will work.
151 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
152 }
153 }
halcanarya096d7a2015-03-27 12:16:53 -0700154}
155
156DEF_TEST(Codec, r) {
157 // WBMP
scroggob636b452015-07-22 07:16:20 -0700158 check(r, "mandrill.wbmp", SkISize::Make(512, 512), false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700159
scroggo6f5e6192015-06-18 12:53:43 -0700160 // WEBP
scroggob636b452015-07-22 07:16:20 -0700161 check(r, "baby_tux.webp", SkISize::Make(386, 395), false, true);
162 check(r, "color_wheel.webp", SkISize::Make(128, 128), false, true);
163 check(r, "yellow_rose.webp", SkISize::Make(400, 301), false, true);
scroggo6f5e6192015-06-18 12:53:43 -0700164
halcanarya096d7a2015-03-27 12:16:53 -0700165 // BMP
scroggob636b452015-07-22 07:16:20 -0700166 check(r, "randPixels.bmp", SkISize::Make(8, 8), false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700167
168 // ICO
msarett68b204e2015-04-01 12:09:21 -0700169 // These two tests examine interestingly different behavior:
170 // Decodes an embedded BMP image
scroggob636b452015-07-22 07:16:20 -0700171 check(r, "color_wheel.ico", SkISize::Make(128, 128), false, false);
msarett68b204e2015-04-01 12:09:21 -0700172 // Decodes an embedded PNG image
scroggob636b452015-07-22 07:16:20 -0700173 check(r, "google_chrome.ico", SkISize::Make(256, 256), false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700174
msarett438b2ad2015-04-09 12:43:10 -0700175 // GIF
scroggob636b452015-07-22 07:16:20 -0700176 check(r, "box.gif", SkISize::Make(200, 55), false, false);
177 check(r, "color_wheel.gif", SkISize::Make(128, 128), false, false);
178 check(r, "randPixels.gif", SkISize::Make(8, 8), false, false);
msarett438b2ad2015-04-09 12:43:10 -0700179
msarette16b04a2015-04-15 07:32:19 -0700180 // JPG
scroggob636b452015-07-22 07:16:20 -0700181 check(r, "CMYK.jpg", SkISize::Make(642, 516), true, false);
182 check(r, "color_wheel.jpg", SkISize::Make(128, 128), true, false);
183 check(r, "grayscale.jpg", SkISize::Make(128, 128), true, false);
184 check(r, "mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false);
185 check(r, "randPixels.jpg", SkISize::Make(8, 8), true, false);
msarette16b04a2015-04-15 07:32:19 -0700186
halcanarya096d7a2015-03-27 12:16:53 -0700187 // PNG
scroggob636b452015-07-22 07:16:20 -0700188 check(r, "arrow.png", SkISize::Make(187, 312), true, false);
189 check(r, "baby_tux.png", SkISize::Make(240, 246), true, false);
190 check(r, "color_wheel.png", SkISize::Make(128, 128), true, false);
191 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), true, false);
192 check(r, "mandrill_128.png", SkISize::Make(128, 128), true, false);
193 check(r, "mandrill_16.png", SkISize::Make(16, 16), true, false);
194 check(r, "mandrill_256.png", SkISize::Make(256, 256), true, false);
195 check(r, "mandrill_32.png", SkISize::Make(32, 32), true, false);
196 check(r, "mandrill_512.png", SkISize::Make(512, 512), true, false);
197 check(r, "mandrill_64.png", SkISize::Make(64, 64), true, false);
198 check(r, "plane.png", SkISize::Make(250, 126), true, false);
199 check(r, "randPixels.png", SkISize::Make(8, 8), true, false);
200 check(r, "yellow_rose.png", SkISize::Make(400, 301), true, false);
halcanarya096d7a2015-03-27 12:16:53 -0700201}
scroggo0a7e69c2015-04-03 07:22:22 -0700202
203static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
204 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
205 // We should not have gotten a codec. Bots should catch us if we leaked anything.
206 REPORTER_ASSERT(r, !codec);
207}
208
209// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
210// even on failure. Test some bad streams.
211DEF_TEST(Codec_leaks, r) {
212 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
213 const char nonSupportedStream[] = "hello world";
214 // The other strings should look like the beginning of a file type, so we'll call some
215 // internal version of NewFromStream, which must also delete the stream on failure.
216 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
217 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
218 const char emptyWebp[] = "RIFF1234WEBPVP";
219 const char emptyBmp[] = { 'B', 'M' };
220 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
221 const char emptyGif[] = "GIFVER";
222
223 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
224 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
225 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
226 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
227 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
228 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
229 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
230}
msarette16b04a2015-04-15 07:32:19 -0700231
232static void test_dimensions(skiatest::Reporter* r, const char path[]) {
233 // Create the codec from the resource file
234 SkAutoTDelete<SkStream> stream(resource(path));
235 if (!stream) {
236 SkDebugf("Missing resource '%s'\n", path);
237 return;
238 }
239 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
240 if (!codec) {
241 ERRORF(r, "Unable to create codec '%s'", path);
242 return;
243 }
244
245 // Check that the decode is successful for a variety of scales
246 for (float scale = -0.05f; scale < 2.0f; scale += 0.05f) {
247 // Scale the output dimensions
248 SkISize scaledDims = codec->getScaledDimensions(scale);
249 SkImageInfo scaledInfo = codec->getInfo().makeWH(scaledDims.width(), scaledDims.height());
250
251 // Set up for the decode
252 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
253 size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
254 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
255
scroggoeb602a52015-07-09 08:16:03 -0700256 SkCodec::Result result =
msarette16b04a2015-04-15 07:32:19 -0700257 codec->getPixels(scaledInfo, pixels.get(), rowBytes, NULL, NULL, NULL);
scroggoeb602a52015-07-09 08:16:03 -0700258 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700259 }
260}
261
262// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
263DEF_TEST(Codec_Dimensions, r) {
264 // JPG
265 test_dimensions(r, "CMYK.jpg");
266 test_dimensions(r, "color_wheel.jpg");
267 test_dimensions(r, "grayscale.jpg");
268 test_dimensions(r, "mandrill_512_q075.jpg");
269 test_dimensions(r, "randPixels.jpg");
270}
271
msarett4b17fa32015-04-23 08:53:39 -0700272static void test_empty(skiatest::Reporter* r, const char path[]) {
273 SkAutoTDelete<SkStream> stream(resource(path));
274 if (!stream) {
275 SkDebugf("Missing resource '%s'\n", path);
276 return;
277 }
278 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
279 REPORTER_ASSERT(r, NULL == codec);
280}
msarette16b04a2015-04-15 07:32:19 -0700281
msarett4b17fa32015-04-23 08:53:39 -0700282DEF_TEST(Codec_Empty, r) {
283 // Test images that should not be able to create a codec
284 test_empty(r, "empty_images/zero-dims.gif");
285 test_empty(r, "empty_images/zero-embedded.ico");
286 test_empty(r, "empty_images/zero-width.bmp");
287 test_empty(r, "empty_images/zero-height.bmp");
288 test_empty(r, "empty_images/zero-width.jpg");
289 test_empty(r, "empty_images/zero-height.jpg");
290 test_empty(r, "empty_images/zero-width.png");
291 test_empty(r, "empty_images/zero-height.png");
292 test_empty(r, "empty_images/zero-width.wbmp");
293 test_empty(r, "empty_images/zero-height.wbmp");
294}