blob: b4e65381784c190727d62fed71e77298ac6384e2 [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
Leon Scroggins III63cfb362020-04-24 13:00:48 -04008// Make sure SkUserConfig.h is included so #defines are available on
9// Android.
10#include "include/core/SkTypes.h"
11#ifdef SK_ENABLE_ANDROID_UTILS
12#include "client_utils/android/FrontBufferedStream.h"
13#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/codec/SkAndroidCodec.h"
15#include "include/codec/SkCodec.h"
16#include "include/core/SkBitmap.h"
17#include "include/core/SkCanvas.h"
18#include "include/core/SkColor.h"
19#include "include/core/SkColorSpace.h"
20#include "include/core/SkData.h"
21#include "include/core/SkEncodedImageFormat.h"
22#include "include/core/SkImage.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040023#include "include/core/SkImageEncoder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/core/SkImageGenerator.h"
25#include "include/core/SkImageInfo.h"
26#include "include/core/SkPixmap.h"
27#include "include/core/SkPngChunkReader.h"
28#include "include/core/SkRect.h"
29#include "include/core/SkRefCnt.h"
30#include "include/core/SkSize.h"
31#include "include/core/SkStream.h"
32#include "include/core/SkString.h"
33#include "include/core/SkTypes.h"
34#include "include/core/SkUnPreMultiply.h"
35#include "include/encode/SkJpegEncoder.h"
36#include "include/encode/SkPngEncoder.h"
37#include "include/encode/SkWebpEncoder.h"
38#include "include/private/SkMalloc.h"
39#include "include/private/SkTemplates.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040040#include "include/third_party/skcms/skcms.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "include/utils/SkRandom.h"
42#include "src/codec/SkCodecImageGenerator.h"
43#include "src/core/SkAutoMalloc.h"
44#include "src/core/SkColorSpacePriv.h"
45#include "src/core/SkMD5.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/core/SkStreamPriv.h"
47#include "tests/FakeStreams.h"
48#include "tests/Test.h"
49#include "tools/Resources.h"
50#include "tools/ToolUtils.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040051
Kevin Lubick7f5b19b2021-11-23 09:08:05 -050052#include <png.h>
Ben Wagner1a462bd2018-03-12 13:46:21 -040053
Ben Wagnerb607a8f2018-03-12 13:46:21 -040054#include <setjmp.h>
55#include <cstring>
Ben Wagner9707a7e2019-05-06 17:17:19 -040056#include <initializer_list>
Ben Wagnerb607a8f2018-03-12 13:46:21 -040057#include <memory>
58#include <utility>
59#include <vector>
60
scroggo8e6c7ad2016-09-16 08:20:38 -070061#if PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 5
62 // FIXME (scroggo): Google3 needs to be updated to use a newer version of libpng. In
63 // the meantime, we had to break some pieces of SkPngCodec in order to support Google3.
64 // The parts that are broken are likely not used by Google3.
65 #define SK_PNG_DISABLE_TESTS
66#endif
67
Hal Canary0f2f5222019-04-03 10:13:45 -040068static SkMD5::Digest md5(const SkBitmap& bm) {
halcanarya096d7a2015-03-27 12:16:53 -070069 SkASSERT(bm.getPixels());
70 SkMD5 md5;
71 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
72 for (int y = 0; y < bm.height(); ++y) {
halcanary1e903042016-04-25 10:29:36 -070073 md5.write(bm.getAddr(0, y), rowLen);
halcanarya096d7a2015-03-27 12:16:53 -070074 }
Hal Canary0f2f5222019-04-03 10:13:45 -040075 return md5.finish();
halcanarya096d7a2015-03-27 12:16:53 -070076}
77
scroggo9b2cdbf42015-07-10 12:07:02 -070078/**
79 * Compute the digest for bm and compare it to a known good digest.
80 * @param r Reporter to assert that bm's digest matches goodDigest.
81 * @param goodDigest The known good digest to compare to.
82 * @param bm The bitmap to test.
83 */
84static void compare_to_good_digest(skiatest::Reporter* r, const SkMD5::Digest& goodDigest,
85 const SkBitmap& bm) {
Hal Canary0f2f5222019-04-03 10:13:45 -040086 SkMD5::Digest digest = md5(bm);
scroggo9b2cdbf42015-07-10 12:07:02 -070087 REPORTER_ASSERT(r, digest == goodDigest);
88}
89
scroggod1bc5742015-08-12 08:31:44 -070090/**
91 * Test decoding an SkCodec to a particular SkImageInfo.
92 *
halcanary96fcdcc2015-08-27 07:41:13 -070093 * Calling getPixels(info) should return expectedResult, and if goodDigest is non nullptr,
scroggod1bc5742015-08-12 08:31:44 -070094 * the resulting decode should match.
95 */
scroggo7b5e5532016-02-04 06:14:24 -080096template<typename Codec>
97static void test_info(skiatest::Reporter* r, Codec* codec, const SkImageInfo& info,
scroggod1bc5742015-08-12 08:31:44 -070098 SkCodec::Result expectedResult, const SkMD5::Digest* goodDigest) {
99 SkBitmap bm;
100 bm.allocPixels(info);
scroggod1bc5742015-08-12 08:31:44 -0700101
102 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
103 REPORTER_ASSERT(r, result == expectedResult);
104
105 if (goodDigest) {
106 compare_to_good_digest(r, *goodDigest, bm);
107 }
108}
109
scroggob636b452015-07-22 07:16:20 -0700110SkIRect generate_random_subset(SkRandom* rand, int w, int h) {
111 SkIRect rect;
112 do {
113 rect.fLeft = rand->nextRangeU(0, w);
114 rect.fTop = rand->nextRangeU(0, h);
115 rect.fRight = rand->nextRangeU(0, w);
116 rect.fBottom = rand->nextRangeU(0, h);
117 rect.sort();
118 } while (rect.isEmpty());
119 return rect;
120}
121
scroggo8e6c7ad2016-09-16 08:20:38 -0700122static void test_incremental_decode(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
123 const SkMD5::Digest& goodDigest) {
124 SkBitmap bm;
125 bm.allocPixels(info);
scroggo8e6c7ad2016-09-16 08:20:38 -0700126
127 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->startIncrementalDecode(info, bm.getPixels(),
128 bm.rowBytes()));
129
130 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->incrementalDecode());
131
132 compare_to_good_digest(r, goodDigest, bm);
133}
134
135// Test in stripes, similar to DM's kStripe_Mode
136static void test_in_stripes(skiatest::Reporter* r, SkCodec* codec, const SkImageInfo& info,
137 const SkMD5::Digest& goodDigest) {
138 SkBitmap bm;
139 bm.allocPixels(info);
140 bm.eraseColor(SK_ColorYELLOW);
141
142 const int height = info.height();
143 // Note that if numStripes does not evenly divide height there will be an extra
144 // stripe.
145 const int numStripes = 4;
146
147 if (numStripes > height) {
148 // Image is too small.
149 return;
150 }
151
152 const int stripeHeight = height / numStripes;
153
154 // Iterate through the image twice. Once to decode odd stripes, and once for even.
155 for (int oddEven = 1; oddEven >= 0; oddEven--) {
156 for (int y = oddEven * stripeHeight; y < height; y += 2 * stripeHeight) {
157 SkIRect subset = SkIRect::MakeLTRB(0, y, info.width(),
Brian Osman788b9162020-02-07 10:36:46 -0500158 std::min(y + stripeHeight, height));
scroggo8e6c7ad2016-09-16 08:20:38 -0700159 SkCodec::Options options;
160 options.fSubset = &subset;
161 if (SkCodec::kSuccess != codec->startIncrementalDecode(info, bm.getAddr(0, y),
162 bm.rowBytes(), &options)) {
163 ERRORF(r, "failed to start incremental decode!\ttop: %i\tbottom%i\n",
164 subset.top(), subset.bottom());
165 return;
166 }
167 if (SkCodec::kSuccess != codec->incrementalDecode()) {
168 ERRORF(r, "failed incremental decode starting from line %i\n", y);
169 return;
170 }
171 }
172 }
173
174 compare_to_good_digest(r, goodDigest, bm);
175}
176
scroggo7b5e5532016-02-04 06:14:24 -0800177template<typename Codec>
Leon Scroggins IIIb41321b2018-12-11 10:37:07 -0500178static void test_codec(skiatest::Reporter* r, const char* path, Codec* codec, SkBitmap& bm,
179 const SkImageInfo& info, const SkISize& size, SkCodec::Result expectedResult,
180 SkMD5::Digest* digest, const SkMD5::Digest* goodDigest) {
msarette6dd0042015-10-09 11:07:34 -0700181
halcanarya096d7a2015-03-27 12:16:53 -0700182 REPORTER_ASSERT(r, info.dimensions() == size);
halcanarya096d7a2015-03-27 12:16:53 -0700183 bm.allocPixels(info);
msarettcc7f3052015-10-05 14:20:27 -0700184
185 SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
msarette6dd0042015-10-09 11:07:34 -0700186 REPORTER_ASSERT(r, result == expectedResult);
halcanarya096d7a2015-03-27 12:16:53 -0700187
Hal Canary0f2f5222019-04-03 10:13:45 -0400188 *digest = md5(bm);
msarettcc7f3052015-10-05 14:20:27 -0700189 if (goodDigest) {
190 REPORTER_ASSERT(r, *digest == *goodDigest);
191 }
halcanarya096d7a2015-03-27 12:16:53 -0700192
msarett8ff6ca62015-09-18 12:06:04 -0700193 {
194 // Test decoding to 565
195 SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
scroggoba584892016-05-20 13:56:13 -0700196 if (info.alphaType() == kOpaque_SkAlphaType) {
197 // Decoding to 565 should succeed.
198 SkBitmap bm565;
199 bm565.allocPixels(info565);
scroggoba584892016-05-20 13:56:13 -0700200
201 // This will allow comparison even if the image is incomplete.
202 bm565.eraseColor(SK_ColorBLACK);
203
Leon Scroggins IIIb41321b2018-12-11 10:37:07 -0500204 auto actualResult = codec->getPixels(info565, bm565.getPixels(), bm565.rowBytes());
205 if (actualResult == expectedResult) {
Hal Canary0f2f5222019-04-03 10:13:45 -0400206 SkMD5::Digest digest565 = md5(bm565);
scroggoba584892016-05-20 13:56:13 -0700207
Leon Scroggins IIIb41321b2018-12-11 10:37:07 -0500208 // A request for non-opaque should also succeed.
209 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
210 info565 = info565.makeAlphaType(alpha);
211 test_info(r, codec, info565, expectedResult, &digest565);
212 }
213 } else {
214 ERRORF(r, "Decoding %s to 565 failed with result \"%s\"\n\t\t\t\texpected:\"%s\"",
215 path,
216 SkCodec::ResultToString(actualResult),
217 SkCodec::ResultToString(expectedResult));
scroggoba584892016-05-20 13:56:13 -0700218 }
219 } else {
220 test_info(r, codec, info565, SkCodec::kInvalidConversion, nullptr);
221 }
222 }
223
224 if (codec->getInfo().colorType() == kGray_8_SkColorType) {
225 SkImageInfo grayInfo = codec->getInfo();
226 SkBitmap grayBm;
227 grayBm.allocPixels(grayInfo);
scroggoba584892016-05-20 13:56:13 -0700228
229 grayBm.eraseColor(SK_ColorBLACK);
230
231 REPORTER_ASSERT(r, expectedResult == codec->getPixels(grayInfo,
232 grayBm.getPixels(), grayBm.rowBytes()));
233
Hal Canary0f2f5222019-04-03 10:13:45 -0400234 SkMD5::Digest grayDigest = md5(grayBm);
scroggoba584892016-05-20 13:56:13 -0700235
236 for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
237 grayInfo = grayInfo.makeAlphaType(alpha);
238 test_info(r, codec, grayInfo, expectedResult, &grayDigest);
239 }
msarett8ff6ca62015-09-18 12:06:04 -0700240 }
241
242 // Verify that re-decoding gives the same result. It is interesting to check this after
243 // a decode to 565, since choosing to decode to 565 may result in some of the decode
244 // options being modified. These options should return to their defaults on another
245 // decode to kN32, so the new digest should match the old digest.
msarette6dd0042015-10-09 11:07:34 -0700246 test_info(r, codec, info, expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700247
248 {
249 // Check alpha type conversions
250 if (info.alphaType() == kOpaque_SkAlphaType) {
251 test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800252 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700253 test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
scroggoc5560be2016-02-03 09:42:42 -0800254 expectedResult, digest);
scroggod1bc5742015-08-12 08:31:44 -0700255 } else {
256 // Decoding to opaque should fail
257 test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700258 SkCodec::kInvalidConversion, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700259 SkAlphaType otherAt = info.alphaType();
260 if (kPremul_SkAlphaType == otherAt) {
261 otherAt = kUnpremul_SkAlphaType;
262 } else {
263 otherAt = kPremul_SkAlphaType;
264 }
265 // The other non-opaque alpha type should always succeed, but not match.
msarette6dd0042015-10-09 11:07:34 -0700266 test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700267 }
268 }
msarettcc7f3052015-10-05 14:20:27 -0700269}
270
scroggobed1ed62016-02-11 10:24:55 -0800271static bool supports_partial_scanlines(const char path[]) {
scroggo2c3b2182015-10-09 08:40:59 -0700272 static const char* const exts[] = {
Brian Salomon7a492f72020-08-21 12:39:33 -0400273 "jpg", "jpeg", "png", "webp",
scroggo2c3b2182015-10-09 08:40:59 -0700274 "JPG", "JPEG", "PNG", "WEBP"
275 };
276
277 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
278 if (SkStrEndsWith(path, exts[i])) {
279 return true;
280 }
281 }
282 return false;
283}
284
John Stiles8a412322021-08-11 15:05:14 -0400285static void check_scanline_decode(skiatest::Reporter* r,
286 SkCodec* codec,
287 SkMD5::Digest* codecDigest,
288 const SkImageInfo& info,
289 const char path[],
290 SkISize size,
291 bool supportsScanlineDecoding,
292 bool supportsIncomplete,
293 bool supportsNewScanlineDecoding) {
msarettcc7f3052015-10-05 14:20:27 -0700294
295 // Test full image decodes with SkCodec
msarettcc7f3052015-10-05 14:20:27 -0700296 SkBitmap bm;
John Stiles8a412322021-08-11 15:05:14 -0400297 const SkCodec::Result expectedResult = supportsIncomplete ? SkCodec::kIncompleteInput
298 : SkCodec::kSuccess;
299 test_codec(r, path, codec, bm, info, size, expectedResult, codecDigest, nullptr);
scroggod1bc5742015-08-12 08:31:44 -0700300
301 // Scanline decoding follows.
Nigel Tao9b9453e2018-08-01 09:59:38 +1000302 if (supportsNewScanlineDecoding && !supportsIncomplete) {
John Stiles8a412322021-08-11 15:05:14 -0400303 test_incremental_decode(r, codec, info, *codecDigest);
scroggo19b91532016-10-24 09:03:26 -0700304 // This is only supported by codecs that use incremental decoding to
305 // support subset decodes - png and jpeg (once SkJpegCodec is
306 // converted).
307 if (SkStrEndsWith(path, "png") || SkStrEndsWith(path, "PNG")) {
John Stiles8a412322021-08-11 15:05:14 -0400308 test_in_stripes(r, codec, info, *codecDigest);
scroggo19b91532016-10-24 09:03:26 -0700309 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700310 }
311
312 // Need to call startScanlineDecode() first.
313 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0) == 0);
314 REPORTER_ASSERT(r, !codec->skipScanlines(1));
scroggo46c57472015-09-30 08:57:13 -0700315 const SkCodec::Result startResult = codec->startScanlineDecode(info);
scroggo58421542015-04-01 11:25:20 -0700316 if (supportsScanlineDecoding) {
317 bm.eraseColor(SK_ColorYELLOW);
msarettc0e80c12015-07-01 06:50:35 -0700318
scroggo46c57472015-09-30 08:57:13 -0700319 REPORTER_ASSERT(r, startResult == SkCodec::kSuccess);
scroggo9b2cdbf42015-07-10 12:07:02 -0700320
scroggo58421542015-04-01 11:25:20 -0700321 for (int y = 0; y < info.height(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700322 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
Nigel Tao9b9453e2018-08-01 09:59:38 +1000323 if (!supportsIncomplete) {
msarette6dd0042015-10-09 11:07:34 -0700324 REPORTER_ASSERT(r, 1 == lines);
325 }
scroggo58421542015-04-01 11:25:20 -0700326 }
327 // verify that scanline decoding gives the same result.
scroggo46c57472015-09-30 08:57:13 -0700328 if (SkCodec::kTopDown_SkScanlineOrder == codec->getScanlineOrder()) {
John Stiles8a412322021-08-11 15:05:14 -0400329 compare_to_good_digest(r, *codecDigest, bm);
msarett5406d6f2015-08-31 06:55:13 -0700330 }
scroggo46c57472015-09-30 08:57:13 -0700331
332 // Cannot continue to decode scanlines beyond the end
333 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
msarette6dd0042015-10-09 11:07:34 -0700334 == 0);
scroggo46c57472015-09-30 08:57:13 -0700335
336 // Interrupting a scanline decode with a full decode starts from
337 // scratch
John Stiles8a412322021-08-11 15:05:14 -0400338 {
339 REPORTER_ASSERT(r, codec->startScanlineDecode(info) == SkCodec::kSuccess);
340 const int lines = codec->getScanlines(bm.getAddr(0, 0), 1, 0);
341 if (!supportsIncomplete) {
342 REPORTER_ASSERT(r, lines == 1);
343 }
344 REPORTER_ASSERT(r, codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())
345 == expectedResult);
346 REPORTER_ASSERT(r, codec->getScanlines(bm.getAddr(0, 0), 1, 0)
347 == 0);
348 REPORTER_ASSERT(r, codec->skipScanlines(1)
349 == 0);
msarette6dd0042015-10-09 11:07:34 -0700350 }
msarett80803ff2015-10-16 10:54:12 -0700351
352 // Test partial scanline decodes
scroggobed1ed62016-02-11 10:24:55 -0800353 if (supports_partial_scanlines(path) && info.width() >= 3) {
msarett80803ff2015-10-16 10:54:12 -0700354 SkCodec::Options options;
355 int width = info.width();
356 int height = info.height();
357 SkIRect subset = SkIRect::MakeXYWH(2 * (width / 3), 0, width / 3, height);
358 options.fSubset = &subset;
359
Leon Scroggins571b30f2017-07-11 17:35:31 +0000360 const auto partialStartResult = codec->startScanlineDecode(info, &options);
msarett80803ff2015-10-16 10:54:12 -0700361 REPORTER_ASSERT(r, partialStartResult == SkCodec::kSuccess);
362
363 for (int y = 0; y < height; y++) {
364 const int lines = codec->getScanlines(bm.getAddr(0, y), 1, 0);
Nigel Tao9b9453e2018-08-01 09:59:38 +1000365 if (!supportsIncomplete) {
msarett80803ff2015-10-16 10:54:12 -0700366 REPORTER_ASSERT(r, 1 == lines);
367 }
368 }
369 }
scroggo58421542015-04-01 11:25:20 -0700370 } else {
scroggo46c57472015-09-30 08:57:13 -0700371 REPORTER_ASSERT(r, startResult == SkCodec::kUnimplemented);
halcanarya096d7a2015-03-27 12:16:53 -0700372 }
John Stiles8a412322021-08-11 15:05:14 -0400373}
scroggob636b452015-07-22 07:16:20 -0700374
John Stiles8a412322021-08-11 15:05:14 -0400375static void check_subset_decode(skiatest::Reporter* r,
376 SkCodec* codec,
377 const SkImageInfo& info,
378 SkISize size,
379 bool supportsSubsetDecoding,
380 bool supportsIncomplete) {
381 // This function tests decoding subsets, and will decode a handful of randomly-sized subsets.
382 // Do not attempt to decode subsets of an image of only one pixel, since there is no
scroggob636b452015-07-22 07:16:20 -0700383 // meaningful subset.
384 if (size.width() * size.height() == 1) {
385 return;
386 }
387
388 SkRandom rand;
389 SkIRect subset;
390 SkCodec::Options opts;
391 opts.fSubset = &subset;
392 for (int i = 0; i < 5; i++) {
393 subset = generate_random_subset(&rand, size.width(), size.height());
394 SkASSERT(!subset.isEmpty());
395 const bool supported = codec->getValidSubset(&subset);
396 REPORTER_ASSERT(r, supported == supportsSubsetDecoding);
397
Brian Salomon9241a6d2019-10-03 13:26:54 -0400398 SkImageInfo subsetInfo = info.makeDimensions(subset.size());
scroggob636b452015-07-22 07:16:20 -0700399 SkBitmap bm;
400 bm.allocPixels(subsetInfo);
Leon Scroggins571b30f2017-07-11 17:35:31 +0000401 const auto result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes(), &opts);
scroggob636b452015-07-22 07:16:20 -0700402
403 if (supportsSubsetDecoding) {
John Stiles8a412322021-08-11 15:05:14 -0400404 if (!supportsIncomplete) {
405 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
Leon Scroggins III58f100c2016-12-20 09:49:25 -0500406 }
scroggob636b452015-07-22 07:16:20 -0700407 // Webp is the only codec that supports subsets, and it will have modified the subset
408 // to have even left/top.
409 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
410 } else {
411 // No subsets will work.
412 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented);
413 }
414 }
John Stiles8a412322021-08-11 15:05:14 -0400415}
msarettcc7f3052015-10-05 14:20:27 -0700416
John Stiles8a412322021-08-11 15:05:14 -0400417static void check_android_codec(skiatest::Reporter* r,
418 std::unique_ptr<SkCodec> codec,
419 const SkMD5::Digest& codecDigest,
420 const SkImageInfo& info,
421 const char path[],
422 SkISize size,
423 bool supportsScanlineDecoding,
424 bool supportsSubsetDecoding,
425 bool supportsIncomplete,
426 bool supportsNewScanlineDecoding) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700427 if (supportsScanlineDecoding || supportsSubsetDecoding || supportsNewScanlineDecoding) {
Leon Scroggins III7397d7a2018-01-04 13:26:30 -0500428 auto androidCodec = SkAndroidCodec::MakeFromCodec(std::move(codec));
scroggo7b5e5532016-02-04 06:14:24 -0800429 if (!androidCodec) {
msarettcc7f3052015-10-05 14:20:27 -0700430 ERRORF(r, "Unable to decode '%s'", path);
431 return;
432 }
433
434 SkBitmap bm;
scroggobed1ed62016-02-11 10:24:55 -0800435 SkMD5::Digest androidCodecDigest;
John Stiles8a412322021-08-11 15:05:14 -0400436 const SkCodec::Result expectedResult = supportsIncomplete ? SkCodec::kIncompleteInput
437 : SkCodec::kSuccess;
Leon Scroggins IIIb41321b2018-12-11 10:37:07 -0500438 test_codec(r, path, androidCodec.get(), bm, info, size, expectedResult, &androidCodecDigest,
scroggo7b5e5532016-02-04 06:14:24 -0800439 &codecDigest);
msarette6dd0042015-10-09 11:07:34 -0700440 }
John Stiles8a412322021-08-11 15:05:14 -0400441}
msarette6dd0042015-10-09 11:07:34 -0700442
John Stiles8a412322021-08-11 15:05:14 -0400443static void check_codec_image_generator(skiatest::Reporter* r,
444 const SkMD5::Digest& codecDigest,
445 const SkImageInfo& info,
446 const char path[],
447 bool supportsIncomplete) {
448 // Test SkCodecImageGenerator
Nigel Tao9b9453e2018-08-01 09:59:38 +1000449 if (!supportsIncomplete) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400450 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
451 sk_sp<SkData> fullData(SkData::MakeFromStream(stream.get(), stream->getLength()));
452 std::unique_ptr<SkImageGenerator> gen(
Mike Reed185130c2017-02-15 15:14:16 -0500453 SkCodecImageGenerator::MakeFromEncodedCodec(fullData));
msarettedd2dcf2016-01-14 13:12:26 -0800454 SkBitmap bm;
455 bm.allocPixels(info);
msarettedd2dcf2016-01-14 13:12:26 -0800456 REPORTER_ASSERT(r, gen->getPixels(info, bm.getPixels(), bm.rowBytes()));
457 compare_to_good_digest(r, codecDigest, bm);
scroggoef0fed32016-02-18 05:59:25 -0800458
Leon Scroggins III63cfb362020-04-24 13:00:48 -0400459#if !defined(SK_PNG_DISABLE_TESTS) && defined(SK_ENABLE_ANDROID_UTILS)
460 // Test using FrontBufferedStream, as Android does
461 auto bufferedStream = android::skia::FrontBufferedStream::Make(
Mike Reed98c5d922017-09-15 21:39:47 -0400462 SkMemoryStream::Make(std::move(fullData)), SkCodec::MinBufferedBytesNeeded());
scroggod8d68552016-06-06 11:26:17 -0700463 REPORTER_ASSERT(r, bufferedStream);
John Stiles8a412322021-08-11 15:05:14 -0400464 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromStream(std::move(bufferedStream));
scroggod8d68552016-06-06 11:26:17 -0700465 REPORTER_ASSERT(r, codec);
466 if (codec) {
467 test_info(r, codec.get(), info, SkCodec::kSuccess, &codecDigest);
scroggoef0fed32016-02-18 05:59:25 -0800468 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700469#endif
msarettedd2dcf2016-01-14 13:12:26 -0800470 }
halcanarya096d7a2015-03-27 12:16:53 -0700471}
472
John Stiles8a412322021-08-11 15:05:14 -0400473static void check(skiatest::Reporter* r,
474 const char path[],
475 SkISize size,
476 bool supportsScanlineDecoding,
477 bool supportsSubsetDecoding,
478 bool supportsIncomplete,
479 bool supportsNewScanlineDecoding = false) {
480 // If we're testing incomplete decodes, let's run the same test on full decodes.
481 if (supportsIncomplete) {
482 check(r, path, size, supportsScanlineDecoding, supportsSubsetDecoding,
483 /*supportsIncomplete=*/false, supportsNewScanlineDecoding);
484 }
485
486 // Initialize a codec with a data stream.
487 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
488 if (!stream) {
489 return;
490 }
491
492 std::unique_ptr<SkCodec> codec;
493 if (supportsIncomplete) {
494 size_t length = stream->getLength();
495 codec = SkCodec::MakeFromData(SkData::MakeFromStream(stream.get(), 2 * length / 3));
496 } else {
497 codec = SkCodec::MakeFromStream(std::move(stream));
498 }
499 if (!codec) {
500 ERRORF(r, "Unable to decode '%s'", path);
501 return;
502 }
503
504 const SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
505
506 // Run tests with this codec.
507 SkMD5::Digest codecDigest;
508 check_scanline_decode(r, codec.get(), &codecDigest, info, path, size, supportsScanlineDecoding,
509 supportsIncomplete, supportsNewScanlineDecoding);
510
511 check_subset_decode(r, codec.get(), info, size, supportsSubsetDecoding, supportsIncomplete);
512
513 check_android_codec(r, std::move(codec), codecDigest, info, path, size,
514 supportsScanlineDecoding, supportsSubsetDecoding, supportsIncomplete,
515 supportsNewScanlineDecoding);
516
517 check_codec_image_generator(r, codecDigest, info, path, supportsIncomplete);
518}
519
Leon Scroggins III83926342016-12-06 10:58:02 -0500520DEF_TEST(Codec_wbmp, r) {
Hal Canaryc465d132017-12-08 10:21:31 -0500521 check(r, "images/mandrill.wbmp", SkISize::Make(512, 512), true, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500522}
halcanarya096d7a2015-03-27 12:16:53 -0700523
Leon Scroggins III83926342016-12-06 10:58:02 -0500524DEF_TEST(Codec_webp, r) {
Hal Canaryc465d132017-12-08 10:21:31 -0500525 check(r, "images/baby_tux.webp", SkISize::Make(386, 395), false, true, true);
526 check(r, "images/color_wheel.webp", SkISize::Make(128, 128), false, true, true);
527 check(r, "images/yellow_rose.webp", SkISize::Make(400, 301), false, true, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500528}
scroggo6f5e6192015-06-18 12:53:43 -0700529
Leon Scroggins III83926342016-12-06 10:58:02 -0500530DEF_TEST(Codec_bmp, r) {
Hal Canaryc465d132017-12-08 10:21:31 -0500531 check(r, "images/randPixels.bmp", SkISize::Make(8, 8), true, false, true);
532 check(r, "images/rle.bmp", SkISize::Make(320, 240), true, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500533}
halcanarya096d7a2015-03-27 12:16:53 -0700534
Leon Scroggins III83926342016-12-06 10:58:02 -0500535DEF_TEST(Codec_ico, r) {
msarette6dd0042015-10-09 11:07:34 -0700536 // FIXME: We are not ready to test incomplete ICOs
msarett68b204e2015-04-01 12:09:21 -0700537 // These two tests examine interestingly different behavior:
538 // Decodes an embedded BMP image
Hal Canaryc465d132017-12-08 10:21:31 -0500539 check(r, "images/color_wheel.ico", SkISize::Make(128, 128), true, false, false);
msarett68b204e2015-04-01 12:09:21 -0700540 // Decodes an embedded PNG image
Hal Canaryc465d132017-12-08 10:21:31 -0500541 check(r, "images/google_chrome.ico", SkISize::Make(256, 256), false, false, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500542}
halcanarya096d7a2015-03-27 12:16:53 -0700543
Leon Scroggins III83926342016-12-06 10:58:02 -0500544DEF_TEST(Codec_gif, r) {
Hal Canaryc465d132017-12-08 10:21:31 -0500545 check(r, "images/box.gif", SkISize::Make(200, 55), false, false, true, true);
546 check(r, "images/color_wheel.gif", SkISize::Make(128, 128), false, false, true, true);
msarette6dd0042015-10-09 11:07:34 -0700547 // randPixels.gif is too small to test incomplete
Hal Canaryc465d132017-12-08 10:21:31 -0500548 check(r, "images/randPixels.gif", SkISize::Make(8, 8), false, false, false, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500549}
msarett438b2ad2015-04-09 12:43:10 -0700550
Leon Scroggins III83926342016-12-06 10:58:02 -0500551DEF_TEST(Codec_jpg, r) {
Hal Canaryc465d132017-12-08 10:21:31 -0500552 check(r, "images/CMYK.jpg", SkISize::Make(642, 516), true, false, true);
553 check(r, "images/color_wheel.jpg", SkISize::Make(128, 128), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700554 // grayscale.jpg is too small to test incomplete
Hal Canaryc465d132017-12-08 10:21:31 -0500555 check(r, "images/grayscale.jpg", SkISize::Make(128, 128), true, false, false);
556 check(r, "images/mandrill_512_q075.jpg", SkISize::Make(512, 512), true, false, true);
msarette6dd0042015-10-09 11:07:34 -0700557 // randPixels.jpg is too small to test incomplete
Hal Canaryc465d132017-12-08 10:21:31 -0500558 check(r, "images/randPixels.jpg", SkISize::Make(8, 8), true, false, false);
Leon Scroggins III83926342016-12-06 10:58:02 -0500559}
msarette16b04a2015-04-15 07:32:19 -0700560
Leon Scroggins III83926342016-12-06 10:58:02 -0500561DEF_TEST(Codec_png, r) {
Hal Canaryc465d132017-12-08 10:21:31 -0500562 check(r, "images/arrow.png", SkISize::Make(187, 312), false, false, true, true);
563 check(r, "images/baby_tux.png", SkISize::Make(240, 246), false, false, true, true);
564 check(r, "images/color_wheel.png", SkISize::Make(128, 128), false, false, true, true);
scroggo8e6c7ad2016-09-16 08:20:38 -0700565 // half-transparent-white-pixel.png is too small to test incomplete
Hal Canaryc465d132017-12-08 10:21:31 -0500566 check(r, "images/half-transparent-white-pixel.png", SkISize::Make(1, 1), false, false, false, true);
567 check(r, "images/mandrill_128.png", SkISize::Make(128, 128), false, false, true, true);
Brian Osmanccb21bf2018-07-09 14:57:48 -0400568 // mandrill_16.png is too small (relative to embedded sRGB profile) to test incomplete
569 check(r, "images/mandrill_16.png", SkISize::Make(16, 16), false, false, false, true);
Hal Canaryc465d132017-12-08 10:21:31 -0500570 check(r, "images/mandrill_256.png", SkISize::Make(256, 256), false, false, true, true);
571 check(r, "images/mandrill_32.png", SkISize::Make(32, 32), false, false, true, true);
572 check(r, "images/mandrill_512.png", SkISize::Make(512, 512), false, false, true, true);
573 check(r, "images/mandrill_64.png", SkISize::Make(64, 64), false, false, true, true);
574 check(r, "images/plane.png", SkISize::Make(250, 126), false, false, true, true);
575 check(r, "images/plane_interlaced.png", SkISize::Make(250, 126), false, false, true, true);
576 check(r, "images/randPixels.png", SkISize::Make(8, 8), false, false, true, true);
577 check(r, "images/yellow_rose.png", SkISize::Make(400, 301), false, false, true, true);
Leon Scroggins III83926342016-12-06 10:58:02 -0500578}
yujieqin916de9f2016-01-25 08:26:16 -0800579
yujieqinf236ee42016-02-29 07:14:42 -0800580// Disable RAW tests for Win32.
581#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Leon Scroggins III83926342016-12-06 10:58:02 -0500582DEF_TEST(Codec_raw, r) {
Hal Canaryc465d132017-12-08 10:21:31 -0500583 check(r, "images/sample_1mp.dng", SkISize::Make(600, 338), false, false, false);
584 check(r, "images/sample_1mp_rotated.dng", SkISize::Make(600, 338), false, false, false);
585 check(r, "images/dng_with_preview.dng", SkISize::Make(600, 338), true, false, false);
halcanarya096d7a2015-03-27 12:16:53 -0700586}
Leon Scroggins III83926342016-12-06 10:58:02 -0500587#endif
scroggo0a7e69c2015-04-03 07:22:22 -0700588
589static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
scroggo2c3b2182015-10-09 08:40:59 -0700590 // Neither of these calls should return a codec. Bots should catch us if we leaked anything.
Mike Reedede7bac2017-07-23 15:30:02 -0400591 REPORTER_ASSERT(r, !SkCodec::MakeFromStream(
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500592 std::make_unique<SkMemoryStream>(stream, len, false)));
Mike Reedede7bac2017-07-23 15:30:02 -0400593 REPORTER_ASSERT(r, !SkAndroidCodec::MakeFromStream(
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500594 std::make_unique<SkMemoryStream>(stream, len, false)));
scroggo0a7e69c2015-04-03 07:22:22 -0700595}
596
597// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
598// even on failure. Test some bad streams.
599DEF_TEST(Codec_leaks, r) {
600 // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
601 const char nonSupportedStream[] = "hello world";
602 // The other strings should look like the beginning of a file type, so we'll call some
603 // internal version of NewFromStream, which must also delete the stream on failure.
604 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
605 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
606 const char emptyWebp[] = "RIFF1234WEBPVP";
607 const char emptyBmp[] = { 'B', 'M' };
608 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
609 const char emptyGif[] = "GIFVER";
610
611 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
612 test_invalid_stream(r, emptyPng, sizeof(emptyPng));
613 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
614 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
615 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
616 test_invalid_stream(r, emptyIco, sizeof(emptyIco));
617 test_invalid_stream(r, emptyGif, sizeof(emptyGif));
618}
msarette16b04a2015-04-15 07:32:19 -0700619
scroggo2c3b2182015-10-09 08:40:59 -0700620DEF_TEST(Codec_null, r) {
scroggobed1ed62016-02-11 10:24:55 -0800621 // Attempting to create an SkCodec or an SkAndroidCodec with null should not
scroggo2c3b2182015-10-09 08:40:59 -0700622 // crash.
Mike Reedede7bac2017-07-23 15:30:02 -0400623 REPORTER_ASSERT(r, !SkCodec::MakeFromStream(nullptr));
624 REPORTER_ASSERT(r, !SkAndroidCodec::MakeFromStream(nullptr));
scroggo2c3b2182015-10-09 08:40:59 -0700625}
626
msarette16b04a2015-04-15 07:32:19 -0700627static void test_dimensions(skiatest::Reporter* r, const char path[]) {
628 // Create the codec from the resource file
Ben Wagner145dbcd2016-11-03 14:40:50 -0400629 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
msarette16b04a2015-04-15 07:32:19 -0700630 if (!stream) {
msarette16b04a2015-04-15 07:32:19 -0700631 return;
632 }
Mike Reedede7bac2017-07-23 15:30:02 -0400633 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromStream(std::move(stream)));
msarette16b04a2015-04-15 07:32:19 -0700634 if (!codec) {
635 ERRORF(r, "Unable to create codec '%s'", path);
636 return;
637 }
638
639 // Check that the decode is successful for a variety of scales
scroggo501b7342015-11-03 07:55:11 -0800640 for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
msarette16b04a2015-04-15 07:32:19 -0700641 // Scale the output dimensions
msarett3d9d7a72015-10-21 10:27:10 -0700642 SkISize scaledDims = codec->getSampledDimensions(sampleSize);
msarettb32758a2015-08-18 13:22:46 -0700643 SkImageInfo scaledInfo = codec->getInfo()
Brian Salomon9241a6d2019-10-03 13:26:54 -0400644 .makeDimensions(scaledDims)
msarettb32758a2015-08-18 13:22:46 -0700645 .makeColorType(kN32_SkColorType);
msarette16b04a2015-04-15 07:32:19 -0700646
647 // Set up for the decode
648 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
Mike Reedf0ffb892017-10-03 14:47:21 -0400649 size_t totalBytes = scaledInfo.computeByteSize(rowBytes);
msarette16b04a2015-04-15 07:32:19 -0700650 SkAutoTMalloc<SkPMColor> pixels(totalBytes);
651
msarett3d9d7a72015-10-21 10:27:10 -0700652 SkAndroidCodec::AndroidOptions options;
653 options.fSampleSize = sampleSize;
scroggoeb602a52015-07-09 08:16:03 -0700654 SkCodec::Result result =
msarett3d9d7a72015-10-21 10:27:10 -0700655 codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500656 if (result != SkCodec::kSuccess) {
657 ERRORF(r, "Failed to decode %s with sample size %i; error: %s", path, sampleSize,
658 SkCodec::ResultToString(result));
659 }
msarette16b04a2015-04-15 07:32:19 -0700660 }
661}
662
663// Ensure that onGetScaledDimensions returns valid image dimensions to use for decodes
664DEF_TEST(Codec_Dimensions, r) {
665 // JPG
Hal Canaryc465d132017-12-08 10:21:31 -0500666 test_dimensions(r, "images/CMYK.jpg");
667 test_dimensions(r, "images/color_wheel.jpg");
668 test_dimensions(r, "images/grayscale.jpg");
669 test_dimensions(r, "images/mandrill_512_q075.jpg");
670 test_dimensions(r, "images/randPixels.jpg");
msarettb32758a2015-08-18 13:22:46 -0700671
672 // Decoding small images with very large scaling factors is a potential
673 // source of bugs and crashes. We disable these tests in Gold because
674 // tiny images are not very useful to look at.
675 // Here we make sure that we do not crash or access illegal memory when
676 // performing scaled decodes on small images.
Hal Canaryc465d132017-12-08 10:21:31 -0500677 test_dimensions(r, "images/1x1.png");
678 test_dimensions(r, "images/2x2.png");
679 test_dimensions(r, "images/3x3.png");
680 test_dimensions(r, "images/3x1.png");
681 test_dimensions(r, "images/1x1.png");
682 test_dimensions(r, "images/16x1.png");
683 test_dimensions(r, "images/1x16.png");
684 test_dimensions(r, "images/mandrill_16.png");
msarettb32758a2015-08-18 13:22:46 -0700685
yujieqin916de9f2016-01-25 08:26:16 -0800686 // RAW
yujieqinf236ee42016-02-29 07:14:42 -0800687// Disable RAW tests for Win32.
688#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
Hal Canaryc465d132017-12-08 10:21:31 -0500689 test_dimensions(r, "images/sample_1mp.dng");
690 test_dimensions(r, "images/sample_1mp_rotated.dng");
691 test_dimensions(r, "images/dng_with_preview.dng");
msarett8e49ca32016-01-25 13:10:58 -0800692#endif
msarette16b04a2015-04-15 07:32:19 -0700693}
694
msarettd0375bc2015-08-12 08:08:56 -0700695static void test_invalid(skiatest::Reporter* r, const char path[]) {
Leon Scroggins IIIfee7cba2018-02-13 16:41:03 -0500696 auto data = GetResourceAsData(path);
697 if (!data) {
Leon Scroggins IIIce6d93a2018-02-15 09:25:11 -0500698 ERRORF(r, "Failed to get resource %s", path);
msarett4b17fa32015-04-23 08:53:39 -0700699 return;
700 }
Leon Scroggins IIIfee7cba2018-02-13 16:41:03 -0500701
702 REPORTER_ASSERT(r, !SkCodec::MakeFromData(data));
msarett4b17fa32015-04-23 08:53:39 -0700703}
msarette16b04a2015-04-15 07:32:19 -0700704
msarett4b17fa32015-04-23 08:53:39 -0700705DEF_TEST(Codec_Empty, r) {
Leon Scroggins IIIfee7cba2018-02-13 16:41:03 -0500706 if (GetResourcePath().isEmpty()) {
707 return;
708 }
709
msarett4b17fa32015-04-23 08:53:39 -0700710 // Test images that should not be able to create a codec
msarettd0375bc2015-08-12 08:08:56 -0700711 test_invalid(r, "empty_images/zero-dims.gif");
712 test_invalid(r, "empty_images/zero-embedded.ico");
713 test_invalid(r, "empty_images/zero-width.bmp");
714 test_invalid(r, "empty_images/zero-height.bmp");
715 test_invalid(r, "empty_images/zero-width.jpg");
716 test_invalid(r, "empty_images/zero-height.jpg");
717 test_invalid(r, "empty_images/zero-width.png");
718 test_invalid(r, "empty_images/zero-height.png");
719 test_invalid(r, "empty_images/zero-width.wbmp");
720 test_invalid(r, "empty_images/zero-height.wbmp");
721 // This image is an ico with an embedded mask-bmp. This is illegal.
722 test_invalid(r, "invalid_images/mask-bmp-ico.ico");
Matt Sarett5c496172017-02-07 17:01:16 -0500723 // It is illegal for a webp frame to not be fully contained by the canvas.
724 test_invalid(r, "invalid_images/invalid-offset.webp");
Leon Scroggins IIId87fbee2016-12-02 16:47:53 -0500725#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
726 test_invalid(r, "empty_images/zero_height.tiff");
727#endif
Leon Scroggins IIIfc4ee222017-07-14 11:48:52 -0400728 test_invalid(r, "invalid_images/b37623797.ico");
Leon Scroggins IIIfee7cba2018-02-13 16:41:03 -0500729 test_invalid(r, "invalid_images/osfuzz6295.webp");
Leon Scroggins IIIce6d93a2018-02-15 09:25:11 -0500730 test_invalid(r, "invalid_images/osfuzz6288.bmp");
Leon Scroggins III31476b72018-02-22 16:09:33 -0500731 test_invalid(r, "invalid_images/ossfuzz6347");
msarett4b17fa32015-04-23 08:53:39 -0700732}
msarett99f567e2015-08-05 12:58:26 -0700733
scroggo8e6c7ad2016-09-16 08:20:38 -0700734#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
735
736#ifndef SK_PNG_DISABLE_TESTS // reading chunks does not work properly with older versions.
737 // It does not appear that anyone in Google3 is reading chunks.
738
scroggocf98fa92015-11-23 08:14:40 -0800739static void codex_test_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
740 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
741 if (!sk_stream->write(data, len)) {
742 png_error(png_ptr, "sk_write_fn Error!");
743 }
744}
745
scroggocf98fa92015-11-23 08:14:40 -0800746DEF_TEST(Codec_pngChunkReader, r) {
Kevin Lubickbe03ef12021-06-16 15:28:00 -0400747 // Create a bitmap for hashing. Use unpremul RGBA for libpng.
scroggocf98fa92015-11-23 08:14:40 -0800748 SkBitmap bm;
749 const int w = 1;
750 const int h = 1;
751 const SkImageInfo bmInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
752 kUnpremul_SkAlphaType);
753 bm.setInfo(bmInfo);
754 bm.allocPixels();
755 bm.eraseColor(SK_ColorBLUE);
Hal Canary0f2f5222019-04-03 10:13:45 -0400756 SkMD5::Digest goodDigest = md5(bm);
scroggocf98fa92015-11-23 08:14:40 -0800757
758 // Write to a png file.
759 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
760 REPORTER_ASSERT(r, png);
761 if (!png) {
762 return;
763 }
764
765 png_infop info = png_create_info_struct(png);
766 REPORTER_ASSERT(r, info);
767 if (!info) {
768 png_destroy_write_struct(&png, nullptr);
769 return;
770 }
771
772 if (setjmp(png_jmpbuf(png))) {
773 ERRORF(r, "failed writing png");
774 png_destroy_write_struct(&png, &info);
775 return;
776 }
777
778 SkDynamicMemoryWStream wStream;
779 png_set_write_fn(png, (void*) (&wStream), codex_test_write_fn, nullptr);
780
781 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
782 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
783 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
784
785 // Create some chunks that match the Android framework's use.
786 static png_unknown_chunk gUnknowns[] = {
msarett133eaaa2016-01-07 11:03:25 -0800787 { "npOl", (png_byte*)"outline", sizeof("outline"), PNG_HAVE_IHDR },
788 { "npLb", (png_byte*)"layoutBounds", sizeof("layoutBounds"), PNG_HAVE_IHDR },
789 { "npTc", (png_byte*)"ninePatchData", sizeof("ninePatchData"), PNG_HAVE_IHDR },
scroggocf98fa92015-11-23 08:14:40 -0800790 };
791
792 png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"npOl\0npLb\0npTc\0", 3);
793 png_set_unknown_chunks(png, info, gUnknowns, SK_ARRAY_COUNT(gUnknowns));
794#if PNG_LIBPNG_VER < 10600
795 /* Deal with unknown chunk location bug in 1.5.x and earlier */
msarett133eaaa2016-01-07 11:03:25 -0800796 png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR);
797 png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR);
scroggocf98fa92015-11-23 08:14:40 -0800798#endif
799
800 png_write_info(png, info);
801
802 for (int j = 0; j < h; j++) {
803 png_bytep row = (png_bytep)(bm.getAddr(0, j));
804 png_write_rows(png, &row, 1);
805 }
806 png_write_end(png, info);
807 png_destroy_write_struct(&png, &info);
808
809 class ChunkReader : public SkPngChunkReader {
810 public:
811 ChunkReader(skiatest::Reporter* r)
812 : fReporter(r)
813 {
814 this->reset();
815 }
816
817 bool readChunk(const char tag[], const void* data, size_t length) override {
818 for (size_t i = 0; i < SK_ARRAY_COUNT(gUnknowns); ++i) {
819 if (!strcmp(tag, (const char*) gUnknowns[i].name)) {
820 // Tag matches. This should have been the first time we see it.
821 REPORTER_ASSERT(fReporter, !fSeen[i]);
822 fSeen[i] = true;
823
824 // Data and length should match
825 REPORTER_ASSERT(fReporter, length == gUnknowns[i].size);
826 REPORTER_ASSERT(fReporter, !strcmp((const char*) data,
827 (const char*) gUnknowns[i].data));
828 return true;
829 }
830 }
831 ERRORF(fReporter, "Saw an unexpected unknown chunk.");
832 return true;
833 }
834
835 bool allHaveBeenSeen() {
836 bool ret = true;
837 for (auto seen : fSeen) {
838 ret &= seen;
839 }
840 return ret;
841 }
842
843 void reset() {
844 sk_bzero(fSeen, sizeof(fSeen));
845 }
846
847 private:
848 skiatest::Reporter* fReporter; // Unowned
849 bool fSeen[3];
850 };
851
852 ChunkReader chunkReader(r);
853
854 // Now read the file with SkCodec.
Mike Reedede7bac2017-07-23 15:30:02 -0400855 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(wStream.detachAsData(), &chunkReader));
scroggocf98fa92015-11-23 08:14:40 -0800856 REPORTER_ASSERT(r, codec);
857 if (!codec) {
858 return;
859 }
860
861 // Now compare to the original.
862 SkBitmap decodedBm;
863 decodedBm.setInfo(codec->getInfo());
864 decodedBm.allocPixels();
865 SkCodec::Result result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(),
866 decodedBm.rowBytes());
867 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
868
869 if (decodedBm.colorType() != bm.colorType()) {
870 SkBitmap tmp;
Mike Kleinea3f0142019-03-20 11:12:10 -0500871 bool success = ToolUtils::copy_to(&tmp, bm.colorType(), decodedBm);
scroggocf98fa92015-11-23 08:14:40 -0800872 REPORTER_ASSERT(r, success);
873 if (!success) {
874 return;
875 }
876
877 tmp.swap(decodedBm);
878 }
879
880 compare_to_good_digest(r, goodDigest, decodedBm);
881 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
882
883 // Decoding again will read the chunks again.
884 chunkReader.reset();
885 REPORTER_ASSERT(r, !chunkReader.allHaveBeenSeen());
886 result = codec->getPixels(codec->getInfo(), decodedBm.getPixels(), decodedBm.rowBytes());
887 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
888 REPORTER_ASSERT(r, chunkReader.allHaveBeenSeen());
889}
scroggo8e6c7ad2016-09-16 08:20:38 -0700890#endif // SK_PNG_DISABLE_TESTS
scroggocf98fa92015-11-23 08:14:40 -0800891#endif // PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
scroggob9a1e342015-11-30 06:25:31 -0800892
scroggodb30be22015-12-08 18:54:13 -0800893// Stream that can only peek up to a limit
894class LimitedPeekingMemStream : public SkStream {
895public:
reed42943c82016-09-12 12:01:44 -0700896 LimitedPeekingMemStream(sk_sp<SkData> data, size_t limit)
897 : fStream(std::move(data))
scroggodb30be22015-12-08 18:54:13 -0800898 , fLimit(limit) {}
899
900 size_t peek(void* buf, size_t bytes) const override {
Brian Osman788b9162020-02-07 10:36:46 -0500901 return fStream.peek(buf, std::min(bytes, fLimit));
scroggodb30be22015-12-08 18:54:13 -0800902 }
903 size_t read(void* buf, size_t bytes) override {
904 return fStream.read(buf, bytes);
905 }
906 bool rewind() override {
907 return fStream.rewind();
908 }
909 bool isAtEnd() const override {
msarettff2a6c82016-09-07 11:23:28 -0700910 return fStream.isAtEnd();
scroggodb30be22015-12-08 18:54:13 -0800911 }
912private:
913 SkMemoryStream fStream;
914 const size_t fLimit;
915};
916
yujieqinf236ee42016-02-29 07:14:42 -0800917// Disable RAW tests for Win32.
918#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
yujieqin9c7a8a42016-02-05 08:21:19 -0800919// Test that the RawCodec works also for not asset stream. This will test the code path using
920// SkRawBufferedStream instead of SkRawAssetStream.
yujieqin9c7a8a42016-02-05 08:21:19 -0800921DEF_TEST(Codec_raw_notseekable, r) {
Mike Reed0933bc92017-12-09 01:27:41 +0000922 constexpr char path[] = "images/dng_with_preview.dng";
923 sk_sp<SkData> data(GetResourceAsData(path));
yujieqin9c7a8a42016-02-05 08:21:19 -0800924 if (!data) {
925 SkDebugf("Missing resource '%s'\n", path);
926 return;
927 }
928
Mike Reedede7bac2017-07-23 15:30:02 -0400929 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500930 std::make_unique<NotAssetMemStream>(std::move(data))));
yujieqin9c7a8a42016-02-05 08:21:19 -0800931 REPORTER_ASSERT(r, codec);
932
933 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
934}
935#endif
936
scroggodb30be22015-12-08 18:54:13 -0800937// Test that even if webp_parse_header fails to peek enough, it will fall back to read()
938// + rewind() and succeed.
939DEF_TEST(Codec_webp_peek, r) {
Mike Reed0933bc92017-12-09 01:27:41 +0000940 constexpr char path[] = "images/baby_tux.webp";
941 auto data = GetResourceAsData(path);
scroggodb30be22015-12-08 18:54:13 -0800942 if (!data) {
943 SkDebugf("Missing resource '%s'\n", path);
944 return;
945 }
946
947 // The limit is less than webp needs to peek or read.
Mike Reedede7bac2017-07-23 15:30:02 -0400948 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500949 std::make_unique<LimitedPeekingMemStream>(data, 25)));
scroggodb30be22015-12-08 18:54:13 -0800950 REPORTER_ASSERT(r, codec);
951
scroggo7b5e5532016-02-04 06:14:24 -0800952 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800953
954 // Similarly, a stream which does not peek should still succeed.
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500955 codec = SkCodec::MakeFromStream(std::make_unique<LimitedPeekingMemStream>(data, 0));
scroggodb30be22015-12-08 18:54:13 -0800956 REPORTER_ASSERT(r, codec);
957
scroggo7b5e5532016-02-04 06:14:24 -0800958 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggodb30be22015-12-08 18:54:13 -0800959}
960
msarett7f7ec202016-03-01 12:12:27 -0800961// SkCodec's wbmp decoder was initially unnecessarily restrictive.
962// It required the second byte to be zero. The wbmp specification allows
963// a couple of bits to be 1 (so long as they do not overlap with 0x9F).
964// Test that SkCodec now supports an image with these bits set.
Leon Scroggins III83926342016-12-06 10:58:02 -0500965DEF_TEST(Codec_wbmp_restrictive, r) {
Hal Canaryc465d132017-12-08 10:21:31 -0500966 const char* path = "images/mandrill.wbmp";
Ben Wagner145dbcd2016-11-03 14:40:50 -0400967 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
scroggob9a1e342015-11-30 06:25:31 -0800968 if (!stream) {
scroggob9a1e342015-11-30 06:25:31 -0800969 return;
970 }
971
972 // Modify the stream to contain a second byte with some bits set.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400973 auto data = SkCopyStreamToData(stream.get());
scroggob9a1e342015-11-30 06:25:31 -0800974 uint8_t* writeableData = static_cast<uint8_t*>(data->writable_data());
975 writeableData[1] = static_cast<uint8_t>(~0x9F);
976
msarett7f7ec202016-03-01 12:12:27 -0800977 // SkCodec should support this.
Mike Reedede7bac2017-07-23 15:30:02 -0400978 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
scroggob9a1e342015-11-30 06:25:31 -0800979 REPORTER_ASSERT(r, codec);
980 if (!codec) {
981 return;
982 }
scroggo7b5e5532016-02-04 06:14:24 -0800983 test_info(r, codec.get(), codec->getInfo(), SkCodec::kSuccess, nullptr);
scroggob9a1e342015-11-30 06:25:31 -0800984}
scroggodb30be22015-12-08 18:54:13 -0800985
986// wbmp images have a header that can be arbitrarily large, depending on the
987// size of the image. We cap the size at 65535, meaning we only need to look at
988// 8 bytes to determine whether we can read the image. This is important
Leon Scroggins III04be2b52017-08-17 15:13:20 -0400989// because SkCodec only passes a limited number of bytes to SkWbmpCodec to
990// determine whether the image is a wbmp.
scroggodb30be22015-12-08 18:54:13 -0800991DEF_TEST(Codec_wbmp_max_size, r) {
992 const unsigned char maxSizeWbmp[] = { 0x00, 0x00, // Header
993 0x83, 0xFF, 0x7F, // W: 65535
994 0x83, 0xFF, 0x7F }; // H: 65535
Ben Wagner145dbcd2016-11-03 14:40:50 -0400995 std::unique_ptr<SkStream> stream(new SkMemoryStream(maxSizeWbmp, sizeof(maxSizeWbmp), false));
Mike Reedede7bac2017-07-23 15:30:02 -0400996 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
scroggodb30be22015-12-08 18:54:13 -0800997
998 REPORTER_ASSERT(r, codec);
999 if (!codec) return;
1000
1001 REPORTER_ASSERT(r, codec->getInfo().width() == 65535);
1002 REPORTER_ASSERT(r, codec->getInfo().height() == 65535);
1003
1004 // Now test an image which is too big. Any image with a larger header (i.e.
1005 // has bigger width/height) is also too big.
1006 const unsigned char tooBigWbmp[] = { 0x00, 0x00, // Header
1007 0x84, 0x80, 0x00, // W: 65536
1008 0x84, 0x80, 0x00 }; // H: 65536
John Stilesfbd050b2020-08-03 13:21:46 -04001009 stream = std::make_unique<SkMemoryStream>(tooBigWbmp, sizeof(tooBigWbmp), false);
Mike Reedede7bac2017-07-23 15:30:02 -04001010 codec = SkCodec::MakeFromStream(std::move(stream));
scroggodb30be22015-12-08 18:54:13 -08001011
1012 REPORTER_ASSERT(r, !codec);
1013}
msarett2812f032016-07-18 15:56:08 -07001014
1015DEF_TEST(Codec_jpeg_rewind, r) {
Hal Canaryc465d132017-12-08 10:21:31 -05001016 const char* path = "images/mandrill_512_q075.jpg";
Leon Scroggins III42886572017-01-27 13:16:28 -05001017 sk_sp<SkData> data(GetResourceAsData(path));
1018 if (!data) {
msarett2812f032016-07-18 15:56:08 -07001019 return;
1020 }
Leon Scroggins III42886572017-01-27 13:16:28 -05001021
1022 data = SkData::MakeSubset(data.get(), 0, data->size() / 2);
Mike Reedede7bac2017-07-23 15:30:02 -04001023 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromData(data));
msarett2812f032016-07-18 15:56:08 -07001024 if (!codec) {
1025 ERRORF(r, "Unable to create codec '%s'.", path);
1026 return;
1027 }
1028
1029 const int width = codec->getInfo().width();
1030 const int height = codec->getInfo().height();
1031 size_t rowBytes = sizeof(SkPMColor) * width;
1032 SkAutoMalloc pixelStorage(height * rowBytes);
1033
1034 // Perform a sampled decode.
1035 SkAndroidCodec::AndroidOptions opts;
1036 opts.fSampleSize = 12;
Leon Scroggins III42886572017-01-27 13:16:28 -05001037 auto sampledInfo = codec->getInfo().makeWH(width / 12, height / 12);
1038 auto result = codec->getAndroidPixels(sampledInfo, pixelStorage.get(), rowBytes, &opts);
1039 REPORTER_ASSERT(r, SkCodec::kIncompleteInput == result);
msarett2812f032016-07-18 15:56:08 -07001040
1041 // Rewind the codec and perform a full image decode.
Matt Sarett74b16ed2017-01-25 11:58:11 -05001042 result = codec->getPixels(codec->getInfo(), pixelStorage.get(), rowBytes);
Leon Scroggins III42886572017-01-27 13:16:28 -05001043 REPORTER_ASSERT(r, SkCodec::kIncompleteInput == result);
Matt Sarett74b16ed2017-01-25 11:58:11 -05001044
1045 // Now perform a subset decode.
1046 {
1047 opts.fSampleSize = 1;
1048 SkIRect subset = SkIRect::MakeWH(100, 100);
1049 opts.fSubset = &subset;
1050 result = codec->getAndroidPixels(codec->getInfo().makeWH(100, 100), pixelStorage.get(),
1051 rowBytes, &opts);
Leon Scroggins III42886572017-01-27 13:16:28 -05001052 // Though we only have half the data, it is enough to decode this subset.
Matt Sarett74b16ed2017-01-25 11:58:11 -05001053 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1054 }
1055
1056 // Perform another full image decode. ASAN will detect if we look at the subset when it is
1057 // out of scope. This would happen if we depend on the old state in the codec.
Leon Scroggins III42886572017-01-27 13:16:28 -05001058 // This tests two layers of bugs: both SkJpegCodec::readRows and SkCodec::fillIncompleteImage
1059 // used to look at the old subset.
Matt Sarett74b16ed2017-01-25 11:58:11 -05001060 opts.fSubset = nullptr;
1061 result = codec->getAndroidPixels(codec->getInfo(), pixelStorage.get(), rowBytes, &opts);
Leon Scroggins III42886572017-01-27 13:16:28 -05001062 REPORTER_ASSERT(r, SkCodec::kIncompleteInput == result);
msarett2812f032016-07-18 15:56:08 -07001063}
msarett549ca322016-08-17 08:54:08 -07001064
msarett35bb74b2016-08-22 07:41:28 -07001065static void check_color_xform(skiatest::Reporter* r, const char* path) {
Mike Reedede7bac2017-07-23 15:30:02 -04001066 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromStream(GetResourceAsStream(path)));
msarett35bb74b2016-08-22 07:41:28 -07001067
1068 SkAndroidCodec::AndroidOptions opts;
1069 opts.fSampleSize = 3;
1070 const int subsetWidth = codec->getInfo().width() / 2;
1071 const int subsetHeight = codec->getInfo().height() / 2;
1072 SkIRect subset = SkIRect::MakeWH(subsetWidth, subsetHeight);
1073 opts.fSubset = &subset;
1074
1075 const int dstWidth = subsetWidth / opts.fSampleSize;
1076 const int dstHeight = subsetHeight / opts.fSampleSize;
Brian Osman82ebe042019-01-04 17:03:00 -05001077 auto colorSpace = SkColorSpace::MakeRGB(SkNamedTransferFn::k2Dot2, SkNamedGamut::kAdobeRGB);
msarett35bb74b2016-08-22 07:41:28 -07001078 SkImageInfo dstInfo = codec->getInfo().makeWH(dstWidth, dstHeight)
1079 .makeColorType(kN32_SkColorType)
1080 .makeColorSpace(colorSpace);
1081
1082 size_t rowBytes = dstInfo.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -04001083 SkAutoMalloc pixelStorage(dstInfo.computeByteSize(rowBytes));
msarett35bb74b2016-08-22 07:41:28 -07001084 SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get(), rowBytes, &opts);
1085 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1086}
1087
1088DEF_TEST(Codec_ColorXform, r) {
Hal Canaryc465d132017-12-08 10:21:31 -05001089 check_color_xform(r, "images/mandrill_512_q075.jpg");
1090 check_color_xform(r, "images/mandrill_512.png");
msarett35bb74b2016-08-22 07:41:28 -07001091}
1092
msarettf17b71f2016-09-12 14:30:03 -07001093static bool color_type_match(SkColorType origColorType, SkColorType codecColorType) {
1094 switch (origColorType) {
1095 case kRGBA_8888_SkColorType:
1096 case kBGRA_8888_SkColorType:
1097 return kRGBA_8888_SkColorType == codecColorType ||
1098 kBGRA_8888_SkColorType == codecColorType;
1099 default:
1100 return origColorType == codecColorType;
1101 }
1102}
1103
1104static bool alpha_type_match(SkAlphaType origAlphaType, SkAlphaType codecAlphaType) {
1105 switch (origAlphaType) {
1106 case kUnpremul_SkAlphaType:
1107 case kPremul_SkAlphaType:
1108 return kUnpremul_SkAlphaType == codecAlphaType ||
1109 kPremul_SkAlphaType == codecAlphaType;
1110 default:
1111 return origAlphaType == codecAlphaType;
1112 }
1113}
1114
1115static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const SkImageInfo& info) {
1116 SkBitmap bm1;
Leon Scroggins571b30f2017-07-11 17:35:31 +00001117 bm1.allocPixels(info);
1118 SkCodec::Result result = origCodec->getPixels(info, bm1.getPixels(), bm1.rowBytes());
msarettf17b71f2016-09-12 14:30:03 -07001119 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
msarett9b09cd82016-08-29 14:47:49 -07001120
1121 // Encode the image to png.
Leon Scroggins III0098ccb2018-09-24 15:24:31 -04001122 auto data = SkEncodeBitmap(bm1, SkEncodedImageFormat::kPNG, 100);
msarett9b09cd82016-08-29 14:47:49 -07001123
Mike Reedede7bac2017-07-23 15:30:02 -04001124 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
msarettf17b71f2016-09-12 14:30:03 -07001125 REPORTER_ASSERT(r, color_type_match(info.colorType(), codec->getInfo().colorType()));
1126 REPORTER_ASSERT(r, alpha_type_match(info.alphaType(), codec->getInfo().alphaType()));
msarett9b09cd82016-08-29 14:47:49 -07001127
1128 SkBitmap bm2;
Leon Scroggins571b30f2017-07-11 17:35:31 +00001129 bm2.allocPixels(info);
1130 result = codec->getPixels(info, bm2.getPixels(), bm2.rowBytes());
msarett9b09cd82016-08-29 14:47:49 -07001131 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1132
Hal Canary0f2f5222019-04-03 10:13:45 -04001133 REPORTER_ASSERT(r, md5(bm1) == md5(bm2));
msarett9b09cd82016-08-29 14:47:49 -07001134}
1135
1136DEF_TEST(Codec_PngRoundTrip, r) {
Hal Canaryc465d132017-12-08 10:21:31 -05001137 auto codec = SkCodec::MakeFromStream(GetResourceAsStream("images/mandrill_512_q075.jpg"));
msarett549ca322016-08-17 08:54:08 -07001138
msarettf17b71f2016-09-12 14:30:03 -07001139 SkColorType colorTypesOpaque[] = {
1140 kRGB_565_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1141 };
1142 for (SkColorType colorType : colorTypesOpaque) {
1143 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType);
1144 check_round_trip(r, codec.get(), newInfo);
1145 }
1146
Hal Canaryc465d132017-12-08 10:21:31 -05001147 codec = SkCodec::MakeFromStream(GetResourceAsStream("images/grayscale.jpg"));
msarettf17b71f2016-09-12 14:30:03 -07001148 check_round_trip(r, codec.get(), codec->getInfo());
1149
Hal Canaryc465d132017-12-08 10:21:31 -05001150 codec = SkCodec::MakeFromStream(GetResourceAsStream("images/yellow_rose.png"));
msarettf17b71f2016-09-12 14:30:03 -07001151
1152 SkColorType colorTypesWithAlpha[] = {
1153 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType
1154 };
1155 SkAlphaType alphaTypes[] = {
1156 kUnpremul_SkAlphaType, kPremul_SkAlphaType
1157 };
1158 for (SkColorType colorType : colorTypesWithAlpha) {
1159 for (SkAlphaType alphaType : alphaTypes) {
1160 // Set color space to nullptr because color correct premultiplies do not round trip.
1161 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType)
1162 .makeAlphaType(alphaType)
1163 .makeColorSpace(nullptr);
1164 check_round_trip(r, codec.get(), newInfo);
1165 }
1166 }
1167
Hal Canaryc465d132017-12-08 10:21:31 -05001168 codec = SkCodec::MakeFromStream(GetResourceAsStream("images/index8.png"));
msarettf17b71f2016-09-12 14:30:03 -07001169
1170 for (SkAlphaType alphaType : alphaTypes) {
1171 SkImageInfo newInfo = codec->getInfo().makeAlphaType(alphaType)
1172 .makeColorSpace(nullptr);
1173 check_round_trip(r, codec.get(), newInfo);
1174 }
msarett549ca322016-08-17 08:54:08 -07001175}
msarett2ecc35f2016-09-08 11:55:16 -07001176
1177static void test_conversion_possible(skiatest::Reporter* r, const char* path,
scroggo8e6c7ad2016-09-16 08:20:38 -07001178 bool supportsScanlineDecoder,
1179 bool supportsIncrementalDecoder) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001180 std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
Leon Scroggins IIIc9942a12017-01-30 09:59:28 -05001181 if (!stream) {
1182 return;
1183 }
1184
Mike Reedede7bac2017-07-23 15:30:02 -04001185 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
Leon Scroggins IIIc9942a12017-01-30 09:59:28 -05001186 if (!codec) {
1187 ERRORF(r, "failed to create a codec for %s", path);
1188 return;
1189 }
1190
msarett2ecc35f2016-09-08 11:55:16 -07001191 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType);
1192
1193 SkBitmap bm;
1194 bm.allocPixels(infoF16);
1195 SkCodec::Result result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
Mike Kleince4cf722018-05-10 11:29:15 -04001196 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001197
1198 result = codec->startScanlineDecode(infoF16);
1199 if (supportsScanlineDecoder) {
Mike Kleince4cf722018-05-10 11:29:15 -04001200 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001201 } else {
Leon Scroggins III07418182017-08-15 12:24:02 -04001202 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result
Mike Kleince4cf722018-05-10 11:29:15 -04001203 || SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001204 }
1205
1206 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1207 if (supportsIncrementalDecoder) {
Mike Kleince4cf722018-05-10 11:29:15 -04001208 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001209 } else {
Leon Scroggins III07418182017-08-15 12:24:02 -04001210 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result
Mike Kleince4cf722018-05-10 11:29:15 -04001211 || SkCodec::kSuccess == result);
msarett2ecc35f2016-09-08 11:55:16 -07001212 }
1213
Brian Osman36703d92017-12-12 14:09:31 -05001214 infoF16 = infoF16.makeColorSpace(infoF16.colorSpace()->makeLinearGamma());
msarett2ecc35f2016-09-08 11:55:16 -07001215 result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
1216 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001217 result = codec->startScanlineDecode(infoF16);
1218 if (supportsScanlineDecoder) {
msarett2ecc35f2016-09-08 11:55:16 -07001219 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
scroggo8e6c7ad2016-09-16 08:20:38 -07001220 } else {
1221 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
1222 }
1223
1224 result = codec->startIncrementalDecode(infoF16, bm.getPixels(), bm.rowBytes());
1225 if (supportsIncrementalDecoder) {
1226 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1227 } else {
1228 REPORTER_ASSERT(r, SkCodec::kUnimplemented == result);
msarett2ecc35f2016-09-08 11:55:16 -07001229 }
1230}
1231
1232DEF_TEST(Codec_F16ConversionPossible, r) {
Hal Canaryc465d132017-12-08 10:21:31 -05001233 test_conversion_possible(r, "images/color_wheel.webp", false, false);
1234 test_conversion_possible(r, "images/mandrill_512_q075.jpg", true, false);
1235 test_conversion_possible(r, "images/yellow_rose.png", false, true);
scroggo8e6c7ad2016-09-16 08:20:38 -07001236}
1237
scroggo19b91532016-10-24 09:03:26 -07001238static void decode_frame(skiatest::Reporter* r, SkCodec* codec, size_t frame) {
1239 SkBitmap bm;
1240 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1241 bm.allocPixels(info);
1242
1243 SkCodec::Options opts;
1244 opts.fFrameIndex = frame;
1245 REPORTER_ASSERT(r, SkCodec::kSuccess == codec->getPixels(info,
Leon Scroggins571b30f2017-07-11 17:35:31 +00001246 bm.getPixels(), bm.rowBytes(), &opts));
scroggo19b91532016-10-24 09:03:26 -07001247}
1248
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -04001249// For an animated GIF, we should only read enough to decode frame 0 if the
1250// client never calls getFrameInfo and only decodes frame 0.
scroggo19b91532016-10-24 09:03:26 -07001251DEF_TEST(Codec_skipFullParse, r) {
Hal Canaryc465d132017-12-08 10:21:31 -05001252 auto path = "images/test640x479.gif";
Mike Reed71f867c2017-07-23 13:14:10 -04001253 auto streamObj = GetResourceAsStream(path);
1254 if (!streamObj) {
scroggo19b91532016-10-24 09:03:26 -07001255 return;
1256 }
Mike Reed71f867c2017-07-23 13:14:10 -04001257 SkStream* stream = streamObj.get();
scroggo19b91532016-10-24 09:03:26 -07001258
1259 // Note that we cheat and hold on to the stream pointer, but SkCodec will
1260 // take ownership. We will not refer to the stream after the SkCodec
1261 // deletes it.
Mike Reedede7bac2017-07-23 15:30:02 -04001262 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(streamObj)));
scroggo19b91532016-10-24 09:03:26 -07001263 if (!codec) {
1264 ERRORF(r, "Failed to create codec for %s", path);
1265 return;
1266 }
1267
1268 REPORTER_ASSERT(r, stream->hasPosition());
1269 const size_t sizePosition = stream->getPosition();
1270 REPORTER_ASSERT(r, stream->hasLength() && sizePosition < stream->getLength());
1271
1272 // This should read more of the stream, but not the whole stream.
1273 decode_frame(r, codec.get(), 0);
1274 const size_t positionAfterFirstFrame = stream->getPosition();
1275 REPORTER_ASSERT(r, positionAfterFirstFrame > sizePosition
1276 && positionAfterFirstFrame < stream->getLength());
1277
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -04001278 // There is more data in the stream.
scroggo19b91532016-10-24 09:03:26 -07001279 auto frameInfo = codec->getFrameInfo();
1280 REPORTER_ASSERT(r, frameInfo.size() == 4);
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -04001281 REPORTER_ASSERT(r, stream->getPosition() > positionAfterFirstFrame);
scroggo19b91532016-10-24 09:03:26 -07001282}
1283
scroggo8e6c7ad2016-09-16 08:20:38 -07001284// Only rewinds up to a limit.
1285class LimitedRewindingStream : public SkStream {
1286public:
Mike Reed71f867c2017-07-23 13:14:10 -04001287 static std::unique_ptr<SkStream> Make(const char path[], size_t limit) {
1288 auto stream = GetResourceAsStream(path);
scroggo8e6c7ad2016-09-16 08:20:38 -07001289 if (!stream) {
1290 return nullptr;
1291 }
Mike Reed71f867c2017-07-23 13:14:10 -04001292 return std::unique_ptr<SkStream>(new LimitedRewindingStream(std::move(stream), limit));
scroggo8e6c7ad2016-09-16 08:20:38 -07001293 }
1294
1295 size_t read(void* buffer, size_t size) override {
1296 const size_t bytes = fStream->read(buffer, size);
1297 fPosition += bytes;
1298 return bytes;
1299 }
1300
1301 bool isAtEnd() const override {
1302 return fStream->isAtEnd();
1303 }
1304
1305 bool rewind() override {
1306 if (fPosition <= fLimit && fStream->rewind()) {
1307 fPosition = 0;
1308 return true;
1309 }
1310
1311 return false;
1312 }
1313
1314private:
Ben Wagner145dbcd2016-11-03 14:40:50 -04001315 std::unique_ptr<SkStream> fStream;
1316 const size_t fLimit;
1317 size_t fPosition;
scroggo8e6c7ad2016-09-16 08:20:38 -07001318
Mike Reed71f867c2017-07-23 13:14:10 -04001319 LimitedRewindingStream(std::unique_ptr<SkStream> stream, size_t limit)
1320 : fStream(std::move(stream))
scroggo8e6c7ad2016-09-16 08:20:38 -07001321 , fLimit(limit)
1322 , fPosition(0)
1323 {
1324 SkASSERT(fStream);
1325 }
1326};
1327
1328DEF_TEST(Codec_fallBack, r) {
1329 // SkAndroidCodec needs to be able to fall back to scanline decoding
1330 // if incremental decoding does not work. Make sure this does not
1331 // require a rewind.
1332
1333 // Formats that currently do not support incremental decoding
1334 auto files = {
Hal Canaryc465d132017-12-08 10:21:31 -05001335 "images/CMYK.jpg",
1336 "images/color_wheel.ico",
1337 "images/mandrill.wbmp",
1338 "images/randPixels.bmp",
scroggo8e6c7ad2016-09-16 08:20:38 -07001339 };
1340 for (auto file : files) {
Leon Scroggins III04be2b52017-08-17 15:13:20 -04001341 auto stream = LimitedRewindingStream::Make(file, SkCodec::MinBufferedBytesNeeded());
scroggo8e6c7ad2016-09-16 08:20:38 -07001342 if (!stream) {
1343 SkDebugf("Missing resources (%s). Set --resourcePath.\n", file);
1344 return;
1345 }
1346
Mike Reedede7bac2017-07-23 15:30:02 -04001347 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
scroggo8e6c7ad2016-09-16 08:20:38 -07001348 if (!codec) {
1349 ERRORF(r, "Failed to create codec for %s,", file);
1350 continue;
1351 }
1352
1353 SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
1354 SkBitmap bm;
1355 bm.allocPixels(info);
1356
1357 if (SkCodec::kUnimplemented != codec->startIncrementalDecode(info, bm.getPixels(),
1358 bm.rowBytes())) {
1359 ERRORF(r, "Is scanline decoding now implemented for %s?", file);
1360 continue;
1361 }
1362
1363 // Scanline decoding should not require a rewind.
1364 SkCodec::Result result = codec->startScanlineDecode(info);
1365 if (SkCodec::kSuccess != result) {
1366 ERRORF(r, "Scanline decoding failed for %s with %i", file, result);
1367 }
1368 }
msarett2ecc35f2016-09-08 11:55:16 -07001369}
scroggoc46cdd42016-10-10 06:45:32 -07001370
1371// This test verifies that we fixed an assert statement that fired when reusing a png codec
1372// after scaling.
1373DEF_TEST(Codec_reusePng, r) {
Hal Canaryc465d132017-12-08 10:21:31 -05001374 std::unique_ptr<SkStream> stream(GetResourceAsStream("images/plane.png"));
scroggoc46cdd42016-10-10 06:45:32 -07001375 if (!stream) {
1376 return;
1377 }
1378
Mike Reedede7bac2017-07-23 15:30:02 -04001379 std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::MakeFromStream(std::move(stream)));
scroggoc46cdd42016-10-10 06:45:32 -07001380 if (!codec) {
1381 ERRORF(r, "Failed to create codec\n");
1382 return;
1383 }
1384
1385 SkAndroidCodec::AndroidOptions opts;
1386 opts.fSampleSize = 5;
1387 auto size = codec->getSampledDimensions(opts.fSampleSize);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001388 auto info = codec->getInfo().makeDimensions(size).makeColorType(kN32_SkColorType);
scroggoc46cdd42016-10-10 06:45:32 -07001389 SkBitmap bm;
1390 bm.allocPixels(info);
1391 auto result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1392 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1393
1394 info = codec->getInfo().makeColorType(kN32_SkColorType);
1395 bm.allocPixels(info);
1396 opts.fSampleSize = 1;
1397 result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &opts);
1398 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1399}
scroggoe61b3b42016-10-10 07:17:32 -07001400
1401DEF_TEST(Codec_rowsDecoded, r) {
Hal Canaryc465d132017-12-08 10:21:31 -05001402 auto file = "images/plane_interlaced.png";
scroggoe61b3b42016-10-10 07:17:32 -07001403 std::unique_ptr<SkStream> stream(GetResourceAsStream(file));
1404 if (!stream) {
1405 return;
1406 }
1407
1408 // This is enough to read the header etc, but no rows.
Mike Reedede7bac2017-07-23 15:30:02 -04001409 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(SkData::MakeFromStream(stream.get(), 99)));
scroggoe61b3b42016-10-10 07:17:32 -07001410 if (!codec) {
1411 ERRORF(r, "Failed to create codec\n");
1412 return;
1413 }
1414
1415 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1416 SkBitmap bm;
1417 bm.allocPixels(info);
1418 auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
1419 REPORTER_ASSERT(r, result == SkCodec::kSuccess);
1420
1421 // This is an arbitrary value. The important fact is that it is not zero, and rowsDecoded
1422 // should get set to zero by incrementalDecode.
1423 int rowsDecoded = 77;
1424 result = codec->incrementalDecode(&rowsDecoded);
1425 REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput);
1426 REPORTER_ASSERT(r, rowsDecoded == 0);
1427}
Matt Sarett29121eb2016-10-17 14:32:46 -04001428
Matt Sarettd59948a2017-04-26 10:59:48 -04001429static void test_invalid_images(skiatest::Reporter* r, const char* path,
1430 SkCodec::Result expectedResult) {
Mike Reed71f867c2017-07-23 13:14:10 -04001431 auto stream = GetResourceAsStream(path);
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -05001432 if (!stream) {
1433 return;
1434 }
1435
Mike Reedede7bac2017-07-23 15:30:02 -04001436 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -05001437 REPORTER_ASSERT(r, codec);
1438
Matt Sarettd59948a2017-04-26 10:59:48 -04001439 test_info(r, codec.get(), codec->getInfo().makeColorType(kN32_SkColorType), expectedResult,
1440 nullptr);
1441}
1442
1443DEF_TEST(Codec_InvalidImages, r) {
1444 // ASAN will complain if there is an issue.
Leon Scroggins III674a1842017-07-06 12:26:09 -04001445 test_invalid_images(r, "invalid_images/skbug5887.gif", SkCodec::kErrorInInput);
Matt Sarettd59948a2017-04-26 10:59:48 -04001446 test_invalid_images(r, "invalid_images/many-progressive-scans.jpg", SkCodec::kInvalidInput);
1447 test_invalid_images(r, "invalid_images/b33251605.bmp", SkCodec::kIncompleteInput);
1448 test_invalid_images(r, "invalid_images/bad_palette.png", SkCodec::kInvalidInput);
1449}
1450
1451static void test_invalid_header(skiatest::Reporter* r, const char* path) {
Mike Reed0933bc92017-12-09 01:27:41 +00001452 auto data = GetResourceAsData(path);
1453 if (!data) {
1454 return;
1455 }
1456 std::unique_ptr<SkStreamAsset> stream(new SkMemoryStream(std::move(data)));
Mike Reed847068c2017-07-26 11:35:53 -04001457 if (!stream) {
Matt Sarettd59948a2017-04-26 10:59:48 -04001458 return;
1459 }
Mike Reedede7bac2017-07-23 15:30:02 -04001460 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
Matt Sarettd59948a2017-04-26 10:59:48 -04001461 REPORTER_ASSERT(r, !codec);
1462}
1463
1464DEF_TEST(Codec_InvalidHeader, r) {
1465 test_invalid_header(r, "invalid_images/int_overflow.ico");
1466
1467 // These files report values that have caused problems with SkFILEStreams.
1468 // They are invalid, and should not create SkCodecs.
1469 test_invalid_header(r, "invalid_images/b33651913.bmp");
1470 test_invalid_header(r, "invalid_images/b34778578.bmp");
Leon Scroggins IIIb3b24532017-01-18 12:39:07 -05001471}
1472
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001473/*
1474For the Codec_InvalidAnimated test, immediately below,
1475resources/invalid_images/skbug6046.gif is:
1476
147700000000: 4749 4638 3961 2000 0000 0000 002c ff00 GIF89a ......,..
147800000010: 7400 0600 0000 4001 0021 f904 0a00 0000 t.....@..!......
147900000020: 002c ff00 0000 ff00 7400 0606 0606 0601 .,......t.......
148000000030: 0021 f904 0000 0000 002c ff00 0000 ffcc .!.......,......
148100000040: 1b36 5266 deba 543d .6Rf..T=
1482
Leon Scroggins980d03b2019-07-11 20:47:16 +00001483It nominally contains 3 frames, but only the first one is valid. It came from a
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001484fuzzer doing random mutations and copies. The breakdown:
1485
1486@000 6 bytes magic "GIF89a"
1487@006 7 bytes Logical Screen Descriptor: 0x20 0x00 ... 0x00
1488 - width = 32
1489 - height = 0
1490 - flags = 0x00
1491 - background color index, pixel aspect ratio bytes ignored
1492@00D 10 bytes Image Descriptor header: 0x2C 0xFF ... 0x40
1493 - origin_x = 255
1494 - origin_y = 116
1495 - width = 6
1496 - height = 0
1497 - flags = 0x40, interlaced
1498@017 2 bytes Image Descriptor body (pixel data): 0x01 0x00
Leon Scroggins980d03b2019-07-11 20:47:16 +00001499 - lit_width = 1
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001500 - 0x00 byte means "end of data" for this frame
1501@019 8 bytes Graphic Control Extension: 0x21 0xF9 ... 0x00
1502 - valid, but irrelevant here.
1503@021 10 bytes Image Descriptor header: 0x2C 0xFF ... 0x06
1504 - origin_x = 255
1505 - origin_y = 0
1506 - width = 255
1507 - height = 116
1508 - flags = 0x06, INVALID, 0x80 BIT ZERO IMPLIES 0x07 BITS SHOULD BE ZERO
1509@02B 14 bytes Image Descriptor body (pixel data): 0x06 0x06 ... 0x00
1510 - lit_width = 6
1511 - 0x06 precedes a 6 byte block of data
1512 - 0x04 precedes a 4 byte block of data
1513 - 0x00 byte means "end of data" for this frame
1514@039 10 bytes Image Descriptor header: 0x2C 0xFF ... 0x06
1515 - origin_x = 255
1516 - origin_y = 0
1517 - width = 52479
1518 - height = 13851
1519 - flags = 0x52, INVALID, 0x80 BIT ZERO IMPLIES 0x07 BITS SHOULD BE ZERO
1520@043 5 bytes Image Descriptor body (pixel data): 0x66 0xDE ... unexpected-EOF
Leon Scroggins980d03b2019-07-11 20:47:16 +00001521 - lit_width = 102, INVALID, GREATER THAN 8
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001522 - 0xDE precedes a 222 byte block of data, INVALIDLY TRUNCATED
1523
1524On Image Descriptor flags INVALIDITY,
1525https://www.w3.org/Graphics/GIF/spec-gif89a.txt section 20.c says that "Size of
1526Local Color Table [the low 3 bits]... should be 0 if there is no Local Color
1527Table specified [the high bit]."
1528
Leon Scroggins980d03b2019-07-11 20:47:16 +00001529On LZW literal width (also known as Minimum Code Size) INVALIDITY,
1530https://www.w3.org/Graphics/GIF/spec-gif89a.txt Appendix F says that "Normally
1531this will be the same as the number of [palette index] bits. Because of some
1532algorithmic constraints however, black & white images which have one color bit
1533must be indicated as having a code size of 2." In practice, some GIF decoders,
1534including both the old third_party/gif code and the Wuffs GIF decoder, don't
1535enforce this "at least 2" constraint. Nonetheless, any width greater than 8 is
1536invalid, as there are only 8 bits in a byte.
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001537*/
1538
Leon Scroggins III56e32092016-12-12 17:10:46 -05001539DEF_TEST(Codec_InvalidAnimated, r) {
1540 // ASAN will complain if there is an issue.
1541 auto path = "invalid_images/skbug6046.gif";
Mike Reed71f867c2017-07-23 13:14:10 -04001542 auto stream = GetResourceAsStream(path);
Leon Scroggins III56e32092016-12-12 17:10:46 -05001543 if (!stream) {
1544 return;
1545 }
1546
Mike Reedede7bac2017-07-23 15:30:02 -04001547 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
Leon Scroggins III56e32092016-12-12 17:10:46 -05001548 REPORTER_ASSERT(r, codec);
1549 if (!codec) {
1550 return;
1551 }
1552
1553 const auto info = codec->getInfo().makeColorType(kN32_SkColorType);
1554 SkBitmap bm;
1555 bm.allocPixels(info);
1556
1557 auto frameInfos = codec->getFrameInfo();
1558 SkCodec::Options opts;
Leon Scroggins III249b8e32017-04-17 12:46:33 -04001559 for (int i = 0; static_cast<size_t>(i) < frameInfos.size(); i++) {
Leon Scroggins III56e32092016-12-12 17:10:46 -05001560 opts.fFrameIndex = i;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -04001561 const auto reqFrame = frameInfos[i].fRequiredFrame;
Nigel Tao66bc5242018-08-22 10:56:03 +10001562 opts.fPriorFrame = reqFrame == i - 1 ? reqFrame : SkCodec::kNoFrame;
Leon Scroggins III56e32092016-12-12 17:10:46 -05001563 auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes(), &opts);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001564
Leon Scroggins III56e32092016-12-12 17:10:46 -05001565 if (result != SkCodec::kSuccess) {
Adlai Holler684838f2020-05-12 10:41:04 -04001566 ERRORF(r, "Failed to start decoding frame %i (out of %zu) with error %i\n", i,
Leon Scroggins III56e32092016-12-12 17:10:46 -05001567 frameInfos.size(), result);
1568 continue;
1569 }
1570
1571 codec->incrementalDecode();
1572 }
1573}
Matt Sarett0e032be2017-03-15 17:50:08 -04001574
Matt Sarett5df93de2017-03-22 21:52:47 +00001575static void encode_format(SkDynamicMemoryWStream* stream, const SkPixmap& pixmap,
Matt Sarettc367d032017-05-05 11:13:26 -04001576 SkEncodedImageFormat format) {
Matt Sarett5df93de2017-03-22 21:52:47 +00001577 switch (format) {
1578 case SkEncodedImageFormat::kPNG:
Brian Osmanb62f50c2018-07-12 14:44:27 -04001579 SkPngEncoder::Encode(stream, pixmap, SkPngEncoder::Options());
Matt Sarett5df93de2017-03-22 21:52:47 +00001580 break;
1581 case SkEncodedImageFormat::kJPEG:
Matt Sarett26b44df2017-05-02 16:04:56 -04001582 SkJpegEncoder::Encode(stream, pixmap, SkJpegEncoder::Options());
Matt Sarett5df93de2017-03-22 21:52:47 +00001583 break;
Matt Sarett3dbef9f2017-04-05 22:33:22 +00001584 case SkEncodedImageFormat::kWEBP:
Brian Osmanb62f50c2018-07-12 14:44:27 -04001585 SkWebpEncoder::Encode(stream, pixmap, SkWebpEncoder::Options());
Matt Sarett3dbef9f2017-04-05 22:33:22 +00001586 break;
Matt Sarett5df93de2017-03-22 21:52:47 +00001587 default:
1588 SkASSERT(false);
1589 break;
1590 }
1591}
1592
Brian Osmanb62f50c2018-07-12 14:44:27 -04001593static void test_encode_icc(skiatest::Reporter* r, SkEncodedImageFormat format) {
Matt Sarett0e032be2017-03-15 17:50:08 -04001594 // Test with sRGB color space.
1595 SkBitmap srgbBitmap;
1596 SkImageInfo srgbInfo = SkImageInfo::MakeS32(1, 1, kOpaque_SkAlphaType);
1597 srgbBitmap.allocPixels(srgbInfo);
1598 *srgbBitmap.getAddr32(0, 0) = 0;
1599 SkPixmap pixmap;
1600 srgbBitmap.peekPixels(&pixmap);
1601 SkDynamicMemoryWStream srgbBuf;
Brian Osmanb62f50c2018-07-12 14:44:27 -04001602 encode_format(&srgbBuf, pixmap, format);
Matt Sarett0e032be2017-03-15 17:50:08 -04001603 sk_sp<SkData> srgbData = srgbBuf.detachAsData();
Mike Reedede7bac2017-07-23 15:30:02 -04001604 std::unique_ptr<SkCodec> srgbCodec(SkCodec::MakeFromData(srgbData));
Mike Kleine28a6b52018-07-25 13:05:17 -04001605 REPORTER_ASSERT(r, srgbCodec->getInfo().colorSpace() == sk_srgb_singleton());
Matt Sarett0e032be2017-03-15 17:50:08 -04001606
1607 // Test with P3 color space.
1608 SkDynamicMemoryWStream p3Buf;
Mike Kleinb147ace2020-01-16 11:11:06 -06001609 sk_sp<SkColorSpace> p3 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3);
Matt Sarett0e032be2017-03-15 17:50:08 -04001610 pixmap.setColorSpace(p3);
Brian Osmanb62f50c2018-07-12 14:44:27 -04001611 encode_format(&p3Buf, pixmap, format);
Matt Sarett0e032be2017-03-15 17:50:08 -04001612 sk_sp<SkData> p3Data = p3Buf.detachAsData();
Mike Reedede7bac2017-07-23 15:30:02 -04001613 std::unique_ptr<SkCodec> p3Codec(SkCodec::MakeFromData(p3Data));
Matt Sarett0e032be2017-03-15 17:50:08 -04001614 REPORTER_ASSERT(r, p3Codec->getInfo().colorSpace()->gammaCloseToSRGB());
Brian Osman82ebe042019-01-04 17:03:00 -05001615 skcms_Matrix3x3 mat0, mat1;
Matt Sarett0e032be2017-03-15 17:50:08 -04001616 bool success = p3->toXYZD50(&mat0);
1617 REPORTER_ASSERT(r, success);
1618 success = p3Codec->getInfo().colorSpace()->toXYZD50(&mat1);
1619 REPORTER_ASSERT(r, success);
1620
Brian Osman82ebe042019-01-04 17:03:00 -05001621 for (int i = 0; i < 3; i++) {
1622 for (int j = 0; j < 3; j++) {
1623 REPORTER_ASSERT(r, color_space_almost_equal(mat0.vals[i][j], mat1.vals[i][j]));
Matt Sarett0e032be2017-03-15 17:50:08 -04001624 }
1625 }
1626}
Matt Sarett5df93de2017-03-22 21:52:47 +00001627
1628DEF_TEST(Codec_EncodeICC, r) {
Brian Osmanb62f50c2018-07-12 14:44:27 -04001629 test_encode_icc(r, SkEncodedImageFormat::kPNG);
1630 test_encode_icc(r, SkEncodedImageFormat::kJPEG);
1631 test_encode_icc(r, SkEncodedImageFormat::kWEBP);
Matt Sarett5df93de2017-03-22 21:52:47 +00001632}
Leon Scroggins IIIe5677462017-09-27 16:31:08 -04001633
1634DEF_TEST(Codec_webp_rowsDecoded, r) {
Hal Canaryc465d132017-12-08 10:21:31 -05001635 const char* path = "images/baby_tux.webp";
Leon Scroggins IIIe5677462017-09-27 16:31:08 -04001636 sk_sp<SkData> data(GetResourceAsData(path));
1637 if (!data) {
1638 return;
1639 }
1640
1641 // Truncate this file so that the header is available but no rows can be
1642 // decoded. This should create a codec but fail to decode.
1643 size_t truncatedSize = 5000;
1644 sk_sp<SkData> subset = SkData::MakeSubset(data.get(), 0, truncatedSize);
1645 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(std::move(subset));
1646 if (!codec) {
1647 ERRORF(r, "Failed to create a codec for %s truncated to only %lu bytes",
1648 path, truncatedSize);
1649 return;
1650 }
1651
1652 test_info(r, codec.get(), codec->getInfo(), SkCodec::kInvalidInput, nullptr);
1653}
Leon Scroggins IIIcbf66a22018-02-16 12:03:03 -05001654
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001655/*
1656For the Codec_ossfuzz6274 test, immediately below,
1657resources/invalid_images/ossfuzz6274.gif is:
1658
165900000000: 4749 4638 3961 2000 2000 f120 2020 2020 GIF89a . ..
166000000010: 2020 2020 2020 2020 2021 f903 ff20 2020 !...
166100000020: 002c 0000 0000 2000 2000 2000 00 .,.... . . ..
1662
1663@000 6 bytes magic "GIF89a"
1664@006 7 bytes Logical Screen Descriptor: 0x20 0x00 ... 0x00
1665 - width = 32
1666 - height = 32
1667 - flags = 0xF1, global color table, 4 RGB entries
1668 - background color index, pixel aspect ratio bytes ignored
1669@00D 12 bytes Color Table: 0x20 0x20 ... 0x20
1670@019 20 bytes Graphic Control Extension: 0x21 0xF9 ... unexpected-EOF
1671 - 0x03 precedes a 3 byte block of data, INVALID, MUST BE 4
1672 - 0x20 precedes a 32 byte block of data, INVALIDly truncated
1673
1674https://www.w3.org/Graphics/GIF/spec-gif89a.txt section 23.c says that the
1675block size (for an 0x21 0xF9 Graphic Control Extension) must be "the fixed
1676value 4".
1677*/
1678
Leon Scroggins IIIcbf66a22018-02-16 12:03:03 -05001679DEF_TEST(Codec_ossfuzz6274, r) {
1680 if (GetResourcePath().isEmpty()) {
1681 return;
1682 }
1683
1684 const char* file = "invalid_images/ossfuzz6274.gif";
1685 auto image = GetResourceAsImage(file);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001686
1687#ifdef SK_HAS_WUFFS_LIBRARY
1688 // We are transitioning from an old GIF implementation to a new (Wuffs) GIF
1689 // implementation.
1690 //
1691 // This test (without SK_HAS_WUFFS_LIBRARY) is overly specific to the old
1692 // implementation. In the new implementation, the MakeFromStream factory
1693 // method returns a nullptr SkImage*, instead of returning a non-null but
1694 // otherwise all-transparent SkImage*.
1695 //
1696 // Either way, the end-to-end result is the same - the source input is
1697 // rejected as an invalid GIF image - but the two implementations differ in
1698 // how that's represented.
1699 //
1700 // Once the transition is complete, we can remove the #ifdef and delete the
1701 // rest of the test function.
1702 //
1703 // See Codec_GifTruncated3 for the equivalent of the rest of the test
1704 // function, on different (but still truncated) source data.
1705 if (image) {
1706 ERRORF(r, "Invalid data gave non-nullptr image");
1707 }
1708 return;
1709#endif
1710
Leon Scroggins IIIcbf66a22018-02-16 12:03:03 -05001711 if (!image) {
1712 ERRORF(r, "Missing %s", file);
1713 return;
1714 }
1715
1716 REPORTER_ASSERT(r, image->width() == 32);
1717 REPORTER_ASSERT(r, image->height() == 32);
1718
1719 SkBitmap bm;
1720 if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(32, 32))) {
1721 ERRORF(r, "Failed to allocate pixels");
1722 return;
1723 }
1724
1725 bm.eraseColor(SK_ColorTRANSPARENT);
1726
1727 SkCanvas canvas(bm);
Mike Reed34c56a52021-01-22 15:26:41 -05001728 canvas.drawImage(image, 0, 0);
Leon Scroggins IIIcbf66a22018-02-16 12:03:03 -05001729
1730 for (int i = 0; i < image->width(); ++i)
1731 for (int j = 0; j < image->height(); ++j) {
1732 SkColor actual = SkUnPreMultiply::PMColorToColor(*bm.getAddr32(i, j));
1733 if (actual != SK_ColorTRANSPARENT) {
1734 ERRORF(r, "did not initialize pixels! %i, %i is %x", i, j, actual);
1735 }
1736 }
1737}
Leon Scroggins III9e8a5942018-02-28 16:24:18 -05001738
Leon Scroggins III665949a2018-06-26 10:49:42 -04001739DEF_TEST(Codec_78329453, r) {
1740 if (GetResourcePath().isEmpty()) {
1741 return;
1742 }
1743
1744 const char* file = "images/b78329453.jpeg";
1745 auto data = GetResourceAsData(file);
1746 if (!data) {
1747 ERRORF(r, "Missing %s", file);
1748 return;
1749 }
1750
1751 auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(data));
1752 if (!codec) {
1753 ERRORF(r, "failed to create codec from %s", file);
1754 return;
1755 }
1756
1757 // A bug in jpeg_skip_scanlines resulted in an infinite loop for this specific
1758 // sample size on this image. Other sample sizes could have had the same result,
1759 // but the ones tested by DM happen to not.
1760 constexpr int kSampleSize = 19;
1761 const auto size = codec->getSampledDimensions(kSampleSize);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001762 auto info = codec->getInfo().makeDimensions(size);
Leon Scroggins III665949a2018-06-26 10:49:42 -04001763 SkBitmap bm;
1764 bm.allocPixels(info);
1765 bm.eraseColor(SK_ColorTRANSPARENT);
1766
1767 SkAndroidCodec::AndroidOptions options;
1768 options.fSampleSize = kSampleSize;
1769 auto result = codec->getAndroidPixels(info, bm.getPixels(), bm.rowBytes(), &options);
1770 if (result != SkCodec::kSuccess) {
1771 ERRORF(r, "failed to decode with error %s", SkCodec::ResultToString(result));
1772 }
1773}
1774
Leon Scroggins III36f7e322018-08-27 11:55:46 -04001775DEF_TEST(Codec_A8, r) {
1776 if (GetResourcePath().isEmpty()) {
1777 return;
1778 }
1779
1780 const char* file = "images/mandrill_cmyk.jpg";
1781 auto data = GetResourceAsData(file);
1782 if (!data) {
1783 ERRORF(r, "missing %s", file);
1784 return;
1785 }
1786
1787 auto codec = SkCodec::MakeFromData(std::move(data));
1788 auto info = codec->getInfo().makeColorType(kAlpha_8_SkColorType);
1789 SkBitmap bm;
1790 bm.allocPixels(info);
1791 REPORTER_ASSERT(r, codec->getPixels(bm.pixmap()) == SkCodec::kInvalidConversion);
1792}
1793
Leon Scroggins III9e8a5942018-02-28 16:24:18 -05001794DEF_TEST(Codec_crbug807324, r) {
1795 if (GetResourcePath().isEmpty()) {
1796 return;
1797 }
1798
1799 const char* file = "images/crbug807324.png";
1800 auto image = GetResourceAsImage(file);
1801 if (!image) {
1802 ERRORF(r, "Missing %s", file);
1803 return;
1804 }
1805
1806 const int kWidth = image->width();
1807 const int kHeight = image->height();
1808
1809 SkBitmap bm;
1810 if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(kWidth, kHeight))) {
1811 ERRORF(r, "Could not allocate pixels (%i x %i)", kWidth, kHeight);
1812 return;
1813 }
1814
1815 bm.eraseColor(SK_ColorTRANSPARENT);
1816
1817 SkCanvas canvas(bm);
Mike Reed34c56a52021-01-22 15:26:41 -05001818 canvas.drawImage(image, 0, 0);
Leon Scroggins III9e8a5942018-02-28 16:24:18 -05001819
1820 for (int i = 0; i < kWidth; ++i)
1821 for (int j = 0; j < kHeight; ++j) {
1822 if (*bm.getAddr32(i, j) == SK_ColorTRANSPARENT) {
1823 ERRORF(r, "image should not be transparent! %i, %i is 0", i, j);
1824 return;
1825 }
1826 }
1827}
Leon Scroggins III196f3192020-02-03 12:39:54 -05001828
1829DEF_TEST(Codec_F16_noColorSpace, r) {
1830 const char* path = "images/color_wheel.png";
1831 auto data = GetResourceAsData(path);
1832 if (!data) {
1833 return;
1834 }
1835
1836 auto codec = SkCodec::MakeFromData(std::move(data));
1837 SkImageInfo info = codec->getInfo().makeColorType(kRGBA_F16_SkColorType)
1838 .makeColorSpace(nullptr);
1839 test_info(r, codec.get(), info, SkCodec::kSuccess, nullptr);
1840}
Leon Scroggins III42a604f2020-02-06 15:47:58 -05001841
1842// These test images have ICC profiles that do not map to an SkColorSpace.
1843// Verify that decoding them with a null destination space does not perform
1844// color space transformations.
1845DEF_TEST(Codec_noConversion, r) {
1846 const struct Rec {
1847 const char* name;
1848 SkColor color;
1849 } recs[] = {
1850 { "images/cmyk_yellow_224_224_32.jpg", 0xFFD8FC04 },
1851 { "images/wide_gamut_yellow_224_224_64.jpeg",0xFFE0E040 },
1852 };
1853
1854 for (const auto& rec : recs) {
1855 auto data = GetResourceAsData(rec.name);
1856 if (!data) {
1857 continue;
1858 }
1859
1860 auto codec = SkCodec::MakeFromData(std::move(data));
1861 if (!codec) {
1862 ERRORF(r, "Failed to create a codec from %s", rec.name);
1863 continue;
1864 }
1865
1866 const auto* profile = codec->getICCProfile();
1867 if (!profile) {
1868 ERRORF(r, "Expected %s to have a profile", rec.name);
1869 continue;
1870 }
1871
1872 auto cs = SkColorSpace::Make(*profile);
1873 REPORTER_ASSERT(r, !cs.get());
1874
1875 SkImageInfo info = codec->getInfo().makeColorSpace(nullptr);
1876 SkBitmap bm;
1877 bm.allocPixels(info);
1878 if (codec->getPixels(info, bm.getPixels(), bm.rowBytes()) != SkCodec::kSuccess) {
1879 ERRORF(r, "Failed to decode %s", rec.name);
1880 continue;
1881 }
1882 REPORTER_ASSERT(r, bm.getColor(0, 0) == rec.color);
1883 }
1884}