1 /*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "tests/Test.h"
9 #include "tools/Resources.h"
10
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkColorPriv.h"
13 #include "include/core/SkEncodedImageFormat.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkStream.h"
16 #include "include/core/SkSurface.h"
17 #include "include/encode/SkJpegEncoder.h"
18 #include "include/encode/SkPngEncoder.h"
19 #include "include/encode/SkWebpEncoder.h"
20
21 #include "png.h"
22
23 #include <algorithm>
24 #include <string>
25 #include <vector>
26
27 // FIXME: Update the Google3 build's dependencies so it can run this test.
28 #ifndef SK_BUILD_FOR_GOOGLE3
29 #include "webp/decode.h"
30 #endif
31
encode(SkEncodedImageFormat format,SkWStream * dst,const SkPixmap & src)32 static bool encode(SkEncodedImageFormat format, SkWStream* dst, const SkPixmap& src) {
33 switch (format) {
34 case SkEncodedImageFormat::kJPEG:
35 return SkJpegEncoder::Encode(dst, src, SkJpegEncoder::Options());
36 case SkEncodedImageFormat::kPNG:
37 return SkPngEncoder::Encode(dst, src, SkPngEncoder::Options());
38 default:
39 return false;
40 }
41 }
42
make(SkEncodedImageFormat format,SkWStream * dst,const SkPixmap & src)43 static std::unique_ptr<SkEncoder> make(SkEncodedImageFormat format, SkWStream* dst,
44 const SkPixmap& src) {
45 switch (format) {
46 case SkEncodedImageFormat::kJPEG:
47 return SkJpegEncoder::Make(dst, src, SkJpegEncoder::Options());
48 case SkEncodedImageFormat::kPNG:
49 return SkPngEncoder::Make(dst, src, SkPngEncoder::Options());
50 default:
51 return nullptr;
52 }
53 }
54
test_encode(skiatest::Reporter * r,SkEncodedImageFormat format)55 static void test_encode(skiatest::Reporter* r, SkEncodedImageFormat format) {
56 SkBitmap bitmap;
57 bool success = GetResourceAsBitmap("images/mandrill_128.png", &bitmap);
58 if (!success) {
59 return;
60 }
61
62 SkPixmap src;
63 success = bitmap.peekPixels(&src);
64 REPORTER_ASSERT(r, success);
65 if (!success) {
66 return;
67 }
68
69 SkDynamicMemoryWStream dst0, dst1, dst2, dst3;
70 success = encode(format, &dst0, src);
71 REPORTER_ASSERT(r, success);
72
73 auto encoder1 = make(format, &dst1, src);
74 for (int i = 0; i < src.height(); i++) {
75 success = encoder1->encodeRows(1);
76 REPORTER_ASSERT(r, success);
77 }
78
79 auto encoder2 = make(format, &dst2, src);
80 for (int i = 0; i < src.height(); i+=3) {
81 success = encoder2->encodeRows(3);
82 REPORTER_ASSERT(r, success);
83 }
84
85 auto encoder3 = make(format, &dst3, src);
86 success = encoder3->encodeRows(200);
87 REPORTER_ASSERT(r, success);
88
89 sk_sp<SkData> data0 = dst0.detachAsData();
90 sk_sp<SkData> data1 = dst1.detachAsData();
91 sk_sp<SkData> data2 = dst2.detachAsData();
92 sk_sp<SkData> data3 = dst3.detachAsData();
93 REPORTER_ASSERT(r, data0->equals(data1.get()));
94 REPORTER_ASSERT(r, data0->equals(data2.get()));
95 REPORTER_ASSERT(r, data0->equals(data3.get()));
96 }
97
DEF_TEST(Encode,r)98 DEF_TEST(Encode, r) {
99 test_encode(r, SkEncodedImageFormat::kJPEG);
100 test_encode(r, SkEncodedImageFormat::kPNG);
101 }
102
almost_equals(SkPMColor a,SkPMColor b,int tolerance)103 static inline bool almost_equals(SkPMColor a, SkPMColor b, int tolerance) {
104 if (SkTAbs((int)SkGetPackedR32(a) - (int)SkGetPackedR32(b)) > tolerance) {
105 return false;
106 }
107
108 if (SkTAbs((int)SkGetPackedG32(a) - (int)SkGetPackedG32(b)) > tolerance) {
109 return false;
110 }
111
112 if (SkTAbs((int)SkGetPackedB32(a) - (int)SkGetPackedB32(b)) > tolerance) {
113 return false;
114 }
115
116 if (SkTAbs((int)SkGetPackedA32(a) - (int)SkGetPackedA32(b)) > tolerance) {
117 return false;
118 }
119
120 return true;
121 }
122
almost_equals(const SkBitmap & a,const SkBitmap & b,int tolerance)123 static inline bool almost_equals(const SkBitmap& a, const SkBitmap& b, int tolerance) {
124 if (a.info() != b.info()) {
125 return false;
126 }
127
128 SkASSERT(kN32_SkColorType == a.colorType());
129 for (int y = 0; y < a.height(); y++) {
130 for (int x = 0; x < a.width(); x++) {
131 if (!almost_equals(*a.getAddr32(x, y), *b.getAddr32(x, y), tolerance)) {
132 return false;
133 }
134 }
135 }
136
137 return true;
138 }
139
DEF_TEST(Encode_JPG,r)140 DEF_TEST(Encode_JPG, r) {
141 auto image = GetResourceAsImage("images/mandrill_128.png");
142 if (!image) {
143 return;
144 }
145
146 for (auto ct : { kRGBA_8888_SkColorType,
147 kBGRA_8888_SkColorType,
148 kRGB_565_SkColorType,
149 kARGB_4444_SkColorType,
150 kGray_8_SkColorType,
151 kRGBA_F16_SkColorType }) {
152 for (auto at : { kPremul_SkAlphaType, kUnpremul_SkAlphaType, kOpaque_SkAlphaType }) {
153 auto info = SkImageInfo::Make(image->width(), image->height(), ct, at);
154 auto surface = SkSurface::MakeRaster(info);
155 auto canvas = surface->getCanvas();
156 canvas->drawImage(image, 0, 0);
157
158 SkBitmap bm;
159 bm.allocPixels(info);
160 if (!surface->makeImageSnapshot()->readPixels(bm.pixmap(), 0, 0)) {
161 ERRORF(r, "failed to readPixels! ct: %i\tat: %i\n", ct, at);
162 continue;
163 }
164 for (auto alphaOption : { SkJpegEncoder::AlphaOption::kIgnore,
165 SkJpegEncoder::AlphaOption::kBlendOnBlack }) {
166 SkJpegEncoder::Options opts;
167 opts.fAlphaOption = alphaOption;
168 SkNullWStream dummy;
169 if (!SkJpegEncoder::Encode(&dummy, bm.pixmap(), opts)) {
170 REPORTER_ASSERT(r, ct == kARGB_4444_SkColorType
171 && alphaOption == SkJpegEncoder::AlphaOption::kBlendOnBlack);
172 }
173 }
174 }
175 }
176 }
177
DEF_TEST(Encode_JpegDownsample,r)178 DEF_TEST(Encode_JpegDownsample, r) {
179 SkBitmap bitmap;
180 bool success = GetResourceAsBitmap("images/mandrill_128.png", &bitmap);
181 if (!success) {
182 return;
183 }
184
185 SkPixmap src;
186 success = bitmap.peekPixels(&src);
187 REPORTER_ASSERT(r, success);
188 if (!success) {
189 return;
190 }
191
192 SkDynamicMemoryWStream dst0, dst1, dst2;
193 SkJpegEncoder::Options options;
194 success = SkJpegEncoder::Encode(&dst0, src, options);
195 REPORTER_ASSERT(r, success);
196
197 options.fDownsample = SkJpegEncoder::Downsample::k422;
198 success = SkJpegEncoder::Encode(&dst1, src, options);
199 REPORTER_ASSERT(r, success);
200
201 options.fDownsample = SkJpegEncoder::Downsample::k444;
202 success = SkJpegEncoder::Encode(&dst2, src, options);
203 REPORTER_ASSERT(r, success);
204
205 sk_sp<SkData> data0 = dst0.detachAsData();
206 sk_sp<SkData> data1 = dst1.detachAsData();
207 sk_sp<SkData> data2 = dst2.detachAsData();
208 REPORTER_ASSERT(r, data0->size() < data1->size());
209 REPORTER_ASSERT(r, data1->size() < data2->size());
210
211 SkBitmap bm0, bm1, bm2;
212 SkImage::MakeFromEncoded(data0)->asLegacyBitmap(&bm0);
213 SkImage::MakeFromEncoded(data1)->asLegacyBitmap(&bm1);
214 SkImage::MakeFromEncoded(data2)->asLegacyBitmap(&bm2);
215 REPORTER_ASSERT(r, almost_equals(bm0, bm1, 60));
216 REPORTER_ASSERT(r, almost_equals(bm1, bm2, 60));
217 }
218
pushComment(std::vector<std::string> & comments,const char * keyword,const char * text)219 static inline void pushComment(
220 std::vector<std::string>& comments, const char* keyword, const char* text) {
221 comments.push_back(keyword);
222 comments.push_back(text);
223 }
224
testPngComments(const SkPixmap & src,SkPngEncoder::Options & options,skiatest::Reporter * r)225 static void testPngComments(const SkPixmap& src, SkPngEncoder::Options& options,
226 skiatest::Reporter* r) {
227 std::vector<std::string> commentStrings;
228 pushComment(commentStrings, "key", "text");
229 pushComment(commentStrings, "test", "something");
230 pushComment(commentStrings, "have some", "spaces in both");
231
232 std::string longKey(PNG_KEYWORD_MAX_LENGTH, 'x');
233 #ifdef SK_DEBUG
234 commentStrings.push_back(longKey);
235 #else
236 // We call SkDEBUGFAILF it the key is too long so we'll only test this in release mode.
237 commentStrings.push_back(longKey + "x");
238 #endif
239 commentStrings.push_back("");
240
241 std::vector<const char*> commentPointers;
242 std::vector<size_t> commentSizes;
243 for(auto& str : commentStrings) {
244 commentPointers.push_back(str.c_str());
245 commentSizes.push_back(str.length() + 1);
246 }
247
248 options.fComments = SkDataTable::MakeCopyArrays((void const *const *)commentPointers.data(),
249 commentSizes.data(), commentStrings.size());
250
251
252 SkDynamicMemoryWStream dst;
253 bool success = SkPngEncoder::Encode(&dst, src, options);
254 REPORTER_ASSERT(r, success);
255
256 std::vector<char> output(dst.bytesWritten());
257 dst.copyTo(output.data());
258
259 // Each chunk is of the form length (4 bytes), chunk type (tEXt), data,
260 // checksum (4 bytes). Make sure we find all of them in the encoded
261 // results.
262 const char kExpected1[] =
263 "\x00\x00\x00\x08tEXtkey\x00text\x9e\xe7\x66\x51";
264 const char kExpected2[] =
265 "\x00\x00\x00\x0etEXttest\x00something\x29\xba\xef\xac";
266 const char kExpected3[] =
267 "\x00\x00\x00\x18tEXthave some\x00spaces in both\x8d\x69\x34\x2d";
268 std::string longKeyRecord = "tEXt" + longKey; // A snippet of our long key comment
269 std::string tooLongRecord = "tExt" + longKey + "x"; // A snippet whose key is too long
270
271 auto search1 = std::search(output.begin(), output.end(),
272 kExpected1, kExpected1 + sizeof(kExpected1));
273 auto search2 = std::search(output.begin(), output.end(),
274 kExpected2, kExpected2 + sizeof(kExpected2));
275 auto search3 = std::search(output.begin(), output.end(),
276 kExpected3, kExpected3 + sizeof(kExpected3));
277 auto search4 = std::search(output.begin(), output.end(),
278 longKeyRecord.begin(), longKeyRecord.end());
279 auto search5 = std::search(output.begin(), output.end(),
280 tooLongRecord.begin(), tooLongRecord.end());
281
282 REPORTER_ASSERT(r, search1 != output.end());
283 REPORTER_ASSERT(r, search2 != output.end());
284 REPORTER_ASSERT(r, search3 != output.end());
285 REPORTER_ASSERT(r, search4 != output.end());
286 REPORTER_ASSERT(r, search5 == output.end());
287 // Comments test ends
288 }
289
DEF_TEST(Encode_PngOptions,r)290 DEF_TEST(Encode_PngOptions, r) {
291 SkBitmap bitmap;
292 bool success = GetResourceAsBitmap("images/mandrill_128.png", &bitmap);
293 if (!success) {
294 return;
295 }
296
297 SkPixmap src;
298 success = bitmap.peekPixels(&src);
299 REPORTER_ASSERT(r, success);
300 if (!success) {
301 return;
302 }
303
304 SkDynamicMemoryWStream dst0, dst1, dst2;
305 SkPngEncoder::Options options;
306 success = SkPngEncoder::Encode(&dst0, src, options);
307 REPORTER_ASSERT(r, success);
308
309 options.fFilterFlags = SkPngEncoder::FilterFlag::kUp;
310 success = SkPngEncoder::Encode(&dst1, src, options);
311 REPORTER_ASSERT(r, success);
312
313 options.fZLibLevel = 3;
314 success = SkPngEncoder::Encode(&dst2, src, options);
315 REPORTER_ASSERT(r, success);
316
317 testPngComments(src, options, r);
318
319 sk_sp<SkData> data0 = dst0.detachAsData();
320 sk_sp<SkData> data1 = dst1.detachAsData();
321 sk_sp<SkData> data2 = dst2.detachAsData();
322 REPORTER_ASSERT(r, data0->size() < data1->size());
323 REPORTER_ASSERT(r, data1->size() < data2->size());
324
325 SkBitmap bm0, bm1, bm2;
326 SkImage::MakeFromEncoded(data0)->asLegacyBitmap(&bm0);
327 SkImage::MakeFromEncoded(data1)->asLegacyBitmap(&bm1);
328 SkImage::MakeFromEncoded(data2)->asLegacyBitmap(&bm2);
329 REPORTER_ASSERT(r, almost_equals(bm0, bm1, 0));
330 REPORTER_ASSERT(r, almost_equals(bm0, bm2, 0));
331 }
332
333 #ifndef SK_BUILD_FOR_GOOGLE3
DEF_TEST(Encode_WebpQuality,r)334 DEF_TEST(Encode_WebpQuality, r) {
335 SkBitmap bm;
336 bm.allocN32Pixels(100, 100);
337 bm.eraseColor(SK_ColorBLUE);
338
339 auto dataLossy = SkEncodeBitmap(bm, SkEncodedImageFormat::kWEBP, 99);
340 auto dataLossLess = SkEncodeBitmap(bm, SkEncodedImageFormat::kWEBP, 100);
341
342 enum Format {
343 kMixed = 0,
344 kLossy = 1,
345 kLossless = 2,
346 };
347
348 auto test = [&r](const sk_sp<SkData>& data, Format expected) {
349 auto printFormat = [](int f) {
350 switch (f) {
351 case kMixed: return "mixed";
352 case kLossy: return "lossy";
353 case kLossless: return "lossless";
354 default: return "unknown";
355 }
356 };
357
358 if (!data) {
359 ERRORF(r, "Failed to encode. Expected %s", printFormat(expected));
360 return;
361 }
362
363 WebPBitstreamFeatures features;
364 auto status = WebPGetFeatures(data->bytes(), data->size(), &features);
365 if (status != VP8_STATUS_OK) {
366 ERRORF(r, "Encode had an error %i. Expected %s", status, printFormat(expected));
367 return;
368 }
369
370 if (expected != features.format) {
371 ERRORF(r, "Expected %s encode, but got format %s", printFormat(expected),
372 printFormat(features.format));
373 }
374 };
375
376 test(dataLossy, kLossy);
377 test(dataLossLess, kLossless);
378 }
379 #endif
380
DEF_TEST(Encode_WebpOptions,r)381 DEF_TEST(Encode_WebpOptions, r) {
382 SkBitmap bitmap;
383 bool success = GetResourceAsBitmap("images/google_chrome.ico", &bitmap);
384 if (!success) {
385 return;
386 }
387
388 SkPixmap src;
389 success = bitmap.peekPixels(&src);
390 REPORTER_ASSERT(r, success);
391 if (!success) {
392 return;
393 }
394
395 SkDynamicMemoryWStream dst0, dst1, dst2, dst3;
396 SkWebpEncoder::Options options;
397 options.fCompression = SkWebpEncoder::Compression::kLossless;
398 options.fQuality = 0.0f;
399 success = SkWebpEncoder::Encode(&dst0, src, options);
400 REPORTER_ASSERT(r, success);
401
402 options.fQuality = 100.0f;
403 success = SkWebpEncoder::Encode(&dst1, src, options);
404 REPORTER_ASSERT(r, success);
405
406 options.fCompression = SkWebpEncoder::Compression::kLossy;
407 options.fQuality = 100.0f;
408 success = SkWebpEncoder::Encode(&dst2, src, options);
409 REPORTER_ASSERT(r, success);
410
411 options.fCompression = SkWebpEncoder::Compression::kLossy;
412 options.fQuality = 50.0f;
413 success = SkWebpEncoder::Encode(&dst3, src, options);
414 REPORTER_ASSERT(r, success);
415
416 sk_sp<SkData> data0 = dst0.detachAsData();
417 sk_sp<SkData> data1 = dst1.detachAsData();
418 sk_sp<SkData> data2 = dst2.detachAsData();
419 sk_sp<SkData> data3 = dst3.detachAsData();
420 REPORTER_ASSERT(r, data0->size() > data1->size());
421 REPORTER_ASSERT(r, data1->size() > data2->size());
422 REPORTER_ASSERT(r, data2->size() > data3->size());
423
424 SkBitmap bm0, bm1, bm2, bm3;
425 SkImage::MakeFromEncoded(data0)->asLegacyBitmap(&bm0);
426 SkImage::MakeFromEncoded(data1)->asLegacyBitmap(&bm1);
427 SkImage::MakeFromEncoded(data2)->asLegacyBitmap(&bm2);
428 SkImage::MakeFromEncoded(data3)->asLegacyBitmap(&bm3);
429 REPORTER_ASSERT(r, almost_equals(bm0, bm1, 0));
430 REPORTER_ASSERT(r, almost_equals(bm0, bm2, 90));
431 REPORTER_ASSERT(r, almost_equals(bm2, bm3, 50));
432 }
433