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