blob: 32968fdb7a91b06ce62066e4dec51e9d6a2a86bf [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
31static void check(skiatest::Reporter* r,
32 const char path[],
33 SkISize size,
scroggo58421542015-04-01 11:25:20 -070034 bool supportsScanlineDecoding) {
halcanarya096d7a2015-03-27 12:16:53 -070035 SkAutoTDelete<SkStream> stream(resource(path));
36 if (!stream) {
37 SkDebugf("Missing resource '%s'\n", path);
38 return;
39 }
scroggo58421542015-04-01 11:25:20 -070040 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
41 if (!codec) {
halcanarya096d7a2015-03-27 12:16:53 -070042 ERRORF(r, "Unable to decode '%s'", path);
43 return;
44 }
msarett438b2ad2015-04-09 12:43:10 -070045
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);
halcanarya096d7a2015-03-27 12:16:53 -070051 REPORTER_ASSERT(r, info.dimensions() == size);
52 SkBitmap bm;
53 bm.allocPixels(info);
54 SkAutoLockPixels autoLockPixels(bm);
scroggoeb602a52015-07-09 08:16:03 -070055 SkCodec::Result result =
scroggo58421542015-04-01 11:25:20 -070056 codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL);
scroggoeb602a52015-07-09 08:16:03 -070057 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
halcanarya096d7a2015-03-27 12:16:53 -070058
59 SkMD5::Digest digest1, digest2;
60 md5(bm, &digest1);
61
62 bm.eraseColor(SK_ColorYELLOW);
63
64 result =
scroggo58421542015-04-01 11:25:20 -070065 codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL);
halcanarya096d7a2015-03-27 12:16:53 -070066
scroggoeb602a52015-07-09 08:16:03 -070067 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
scroggo58421542015-04-01 11:25:20 -070068 // 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);
msarettc0e80c12015-07-01 06:50:35 -070076
77 // Regular decodes should be disabled after creating a scanline decoder
78 result = codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL);
scroggoeb602a52015-07-09 08:16:03 -070079 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result);
scroggo58421542015-04-01 11:25:20 -070080 for (int y = 0; y < info.height(); y++) {
81 result = scanlineDecoder->getScanlines(bm.getAddr(0, y), 1, 0);
scroggoeb602a52015-07-09 08:16:03 -070082 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
scroggo58421542015-04-01 11:25:20 -070083 }
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);
halcanarya096d7a2015-03-27 12:16:53 -070090 }
91}
92
93DEF_TEST(Codec, r) {
94 // WBMP
scroggo58421542015-04-01 11:25:20 -070095 check(r, "mandrill.wbmp", SkISize::Make(512, 512), false);
halcanarya096d7a2015-03-27 12:16:53 -070096
scroggo6f5e6192015-06-18 12:53:43 -070097 // 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
halcanarya096d7a2015-03-27 12:16:53 -0700102 // BMP
scroggo58421542015-04-01 11:25:20 -0700103 check(r, "randPixels.bmp", SkISize::Make(8, 8), false);
halcanarya096d7a2015-03-27 12:16:53 -0700104
105 // ICO
msarett68b204e2015-04-01 12:09:21 -0700106 // These two tests examine interestingly different behavior:
107 // Decodes an embedded BMP image
scroggo58421542015-04-01 11:25:20 -0700108 check(r, "color_wheel.ico", SkISize::Make(128, 128), false);
msarett68b204e2015-04-01 12:09:21 -0700109 // Decodes an embedded PNG image
110 check(r, "google_chrome.ico", SkISize::Make(256, 256), false);
halcanarya096d7a2015-03-27 12:16:53 -0700111
msarett438b2ad2015-04-09 12:43:10 -0700112 // 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
msarette16b04a2015-04-15 07:32:19 -0700117 // JPG
msarett97fdea62015-04-29 08:17:15 -0700118 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);
msarette16b04a2015-04-15 07:32:19 -0700123
halcanarya096d7a2015-03-27 12:16:53 -0700124 // PNG
scroggo3eada2a2015-04-01 09:33:23 -0700125 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);
halcanarya096d7a2015-03-27 12:16:53 -0700138}
scroggo0a7e69c2015-04-03 07:22:22 -0700139
140static 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.
148DEF_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}
msarette16b04a2015-04-15 07:32:19 -0700168
169static 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
scroggoeb602a52015-07-09 08:16:03 -0700193 SkCodec::Result result =
msarette16b04a2015-04-15 07:32:19 -0700194 codec->getPixels(scaledInfo, pixels.get(), rowBytes, NULL, NULL, NULL);
scroggoeb602a52015-07-09 08:16:03 -0700195 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarette16b04a2015-04-15 07:32:19 -0700196 }
197}
198
199// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
200DEF_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
msarett4b17fa32015-04-23 08:53:39 -0700209static 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}
msarette16b04a2015-04-15 07:32:19 -0700218
msarett4b17fa32015-04-23 08:53:39 -0700219DEF_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}