Flutter Engine
The Flutter Engine
EncodeTest.cpp
Go to the documentation of this file.
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
16#include "include/core/SkData.h"
32#include "tests/Test.h"
33#include "tools/DecodeUtils.h"
34
35#include <png.h>
36#include <webp/decode.h>
37
38#include <algorithm>
39#include <cstddef>
40#include <initializer_list>
41#include <memory>
42#include <string>
43#include <vector>
44
46 switch (format) {
51 default:
52 return false;
53 }
54}
55
56static std::unique_ptr<SkEncoder> make(SkEncodedImageFormat format, SkWStream* dst,
57 const SkPixmap& src) {
58 switch (format) {
63 default:
64 return nullptr;
65 }
66}
67
70 bool success = ToolUtils::GetResourceAsBitmap("images/mandrill_128.png", &bitmap);
71 if (!success) {
72 return;
73 }
74
76 success = bitmap.peekPixels(&src);
77 REPORTER_ASSERT(r, success);
78 if (!success) {
79 return;
80 }
81
82 SkDynamicMemoryWStream dst0, dst1, dst2, dst3;
83 success = encode(format, &dst0, src);
84 REPORTER_ASSERT(r, success);
85
86 auto encoder1 = make(format, &dst1, src);
87 for (int i = 0; i < src.height(); i++) {
88 success = encoder1->encodeRows(1);
89 REPORTER_ASSERT(r, success);
90 }
91
92 auto encoder2 = make(format, &dst2, src);
93 for (int i = 0; i < src.height(); i+=3) {
94 success = encoder2->encodeRows(3);
95 REPORTER_ASSERT(r, success);
96 }
97
98 auto encoder3 = make(format, &dst3, src);
99 success = encoder3->encodeRows(200);
100 REPORTER_ASSERT(r, success);
101
102 sk_sp<SkData> data0 = dst0.detachAsData();
103 sk_sp<SkData> data1 = dst1.detachAsData();
104 sk_sp<SkData> data2 = dst2.detachAsData();
105 sk_sp<SkData> data3 = dst3.detachAsData();
106 REPORTER_ASSERT(r, data0->equals(data1.get()));
107 REPORTER_ASSERT(r, data0->equals(data2.get()));
108 REPORTER_ASSERT(r, data0->equals(data3.get()));
109}
110
114}
115
116static inline bool almost_equals(SkPMColor a, SkPMColor b, int tolerance) {
117 if (SkTAbs((int)SkGetPackedR32(a) - (int)SkGetPackedR32(b)) > tolerance) {
118 return false;
119 }
120
121 if (SkTAbs((int)SkGetPackedG32(a) - (int)SkGetPackedG32(b)) > tolerance) {
122 return false;
123 }
124
125 if (SkTAbs((int)SkGetPackedB32(a) - (int)SkGetPackedB32(b)) > tolerance) {
126 return false;
127 }
128
129 if (SkTAbs((int)SkGetPackedA32(a) - (int)SkGetPackedA32(b)) > tolerance) {
130 return false;
131 }
132
133 return true;
134}
135
136static inline bool almost_equals(const SkBitmap& a, const SkBitmap& b, int tolerance) {
137 if (a.info() != b.info()) {
138 return false;
139 }
140
141 SkASSERT(kN32_SkColorType == a.colorType());
142 for (int y = 0; y < a.height(); y++) {
143 for (int x = 0; x < a.width(); x++) {
144 if (!almost_equals(*a.getAddr32(x, y), *b.getAddr32(x, y), tolerance)) {
145 return false;
146 }
147 }
148 }
149
150 return true;
151}
152
153DEF_TEST(Encode_JPG, r) {
154 auto image = ToolUtils::GetResourceAsImage("images/mandrill_128.png");
155 if (!image) {
156 return;
157 }
158
159 for (auto ct : { kRGBA_8888_SkColorType,
166 auto info = SkImageInfo::Make(image->width(), image->height(), ct, at);
168 auto canvas = surface->getCanvas();
169 canvas->drawImage(image, 0, 0);
170
171 SkBitmap bm;
172 bm.allocPixels(info);
173 if (!surface->makeImageSnapshot()->readPixels(nullptr, bm.pixmap(), 0, 0)) {
174 ERRORF(r, "failed to readPixels! ct: %i\tat: %i\n", ct, at);
175 continue;
176 }
177 for (auto alphaOption : { SkJpegEncoder::AlphaOption::kIgnore,
180 opts.fAlphaOption = alphaOption;
181 SkNullWStream ignored;
182 if (!SkJpegEncoder::Encode(&ignored, bm.pixmap(), opts)) {
185 }
186 }
187 }
188 }
189}
190
191DEF_TEST(Encode_JpegDownsample, r) {
193 bool success = ToolUtils::GetResourceAsBitmap("images/mandrill_128.png", &bitmap);
194 if (!success) {
195 return;
196 }
197
199 success = bitmap.peekPixels(&src);
200 REPORTER_ASSERT(r, success);
201 if (!success) {
202 return;
203 }
204
205 SkDynamicMemoryWStream dst0, dst1, dst2;
207 success = SkJpegEncoder::Encode(&dst0, src, options);
208 REPORTER_ASSERT(r, success);
209
211 success = SkJpegEncoder::Encode(&dst1, src, options);
212 REPORTER_ASSERT(r, success);
213
215 success = SkJpegEncoder::Encode(&dst2, src, options);
216 REPORTER_ASSERT(r, success);
217
218 sk_sp<SkData> data0 = dst0.detachAsData();
219 sk_sp<SkData> data1 = dst1.detachAsData();
220 sk_sp<SkData> data2 = dst2.detachAsData();
221 REPORTER_ASSERT(r, data0->size() < data1->size());
222 REPORTER_ASSERT(r, data1->size() < data2->size());
223
224 SkBitmap bm0, bm1, bm2;
228 REPORTER_ASSERT(r, almost_equals(bm0, bm1, 60));
229 REPORTER_ASSERT(r, almost_equals(bm1, bm2, 60));
230}
231
232static inline void pushComment(
233 std::vector<std::string>& comments, const char* keyword, const char* text) {
234 comments.push_back(keyword);
235 comments.push_back(text);
236}
237
240 std::vector<std::string> commentStrings;
241 pushComment(commentStrings, "key", "text");
242 pushComment(commentStrings, "test", "something");
243 pushComment(commentStrings, "have some", "spaces in both");
244
245 std::string longKey(PNG_KEYWORD_MAX_LENGTH, 'x');
246#ifdef SK_DEBUG
247 commentStrings.push_back(longKey);
248#else
249 // We call SkDEBUGFAILF it the key is too long so we'll only test this in release mode.
250 commentStrings.push_back(longKey + "x");
251#endif
252 commentStrings.push_back("");
253
254 std::vector<const char*> commentPointers;
255 std::vector<size_t> commentSizes;
256 for(auto& str : commentStrings) {
257 commentPointers.push_back(str.c_str());
258 commentSizes.push_back(str.length() + 1);
259 }
260
261 options.fComments = SkDataTable::MakeCopyArrays((void const *const *)commentPointers.data(),
262 commentSizes.data(), commentStrings.size());
263
264
266 bool success = SkPngEncoder::Encode(&dst, src, options);
267 REPORTER_ASSERT(r, success);
268
269 std::vector<char> output(dst.bytesWritten());
270 dst.copyTo(output.data());
271
272 // Each chunk is of the form length (4 bytes), chunk type (tEXt), data,
273 // checksum (4 bytes). Make sure we find all of them in the encoded
274 // results.
275 const char kExpected1[] =
276 "\x00\x00\x00\x08tEXtkey\x00text\x9e\xe7\x66\x51";
277 const char kExpected2[] =
278 "\x00\x00\x00\x0etEXttest\x00something\x29\xba\xef\xac";
279 const char kExpected3[] =
280 "\x00\x00\x00\x18tEXthave some\x00spaces in both\x8d\x69\x34\x2d";
281 std::string longKeyRecord = "tEXt" + longKey; // A snippet of our long key comment
282 std::string tooLongRecord = "tExt" + longKey + "x"; // A snippet whose key is too long
283
284 auto search1 = std::search(output.begin(), output.end(),
285 kExpected1, kExpected1 + sizeof(kExpected1));
286 auto search2 = std::search(output.begin(), output.end(),
287 kExpected2, kExpected2 + sizeof(kExpected2));
288 auto search3 = std::search(output.begin(), output.end(),
289 kExpected3, kExpected3 + sizeof(kExpected3));
290 auto search4 = std::search(output.begin(), output.end(),
291 longKeyRecord.begin(), longKeyRecord.end());
292 auto search5 = std::search(output.begin(), output.end(),
293 tooLongRecord.begin(), tooLongRecord.end());
294
295 REPORTER_ASSERT(r, search1 != output.end());
296 REPORTER_ASSERT(r, search2 != output.end());
297 REPORTER_ASSERT(r, search3 != output.end());
298 REPORTER_ASSERT(r, search4 != output.end());
299 REPORTER_ASSERT(r, search5 == output.end());
300 // Comments test ends
301}
302
303DEF_TEST(Encode_PngOptions, r) {
305 bool success = ToolUtils::GetResourceAsBitmap("images/mandrill_128.png", &bitmap);
306 if (!success) {
307 return;
308 }
309
311 success = bitmap.peekPixels(&src);
312 REPORTER_ASSERT(r, success);
313 if (!success) {
314 return;
315 }
316
317 SkDynamicMemoryWStream dst0, dst1, dst2;
319 success = SkPngEncoder::Encode(&dst0, src, options);
320 REPORTER_ASSERT(r, success);
321
323 success = SkPngEncoder::Encode(&dst1, src, options);
324 REPORTER_ASSERT(r, success);
325
326 options.fZLibLevel = 3;
327 success = SkPngEncoder::Encode(&dst2, src, options);
328 REPORTER_ASSERT(r, success);
329
331
332 sk_sp<SkData> data0 = dst0.detachAsData();
333 sk_sp<SkData> data1 = dst1.detachAsData();
334 sk_sp<SkData> data2 = dst2.detachAsData();
335 REPORTER_ASSERT(r, data0->size() < data1->size());
336 REPORTER_ASSERT(r, data1->size() < data2->size());
337
338 SkBitmap bm0, bm1, bm2;
342 REPORTER_ASSERT(r, almost_equals(bm0, bm1, 0));
343 REPORTER_ASSERT(r, almost_equals(bm0, bm2, 0));
344}
345
346#ifndef SK_BUILD_FOR_GOOGLE3
347DEF_TEST(Encode_WebpQuality, r) {
348 SkBitmap bm;
349 bm.allocN32Pixels(100, 100);
351
356 auto dataLossLess = stream.detachAsData();
357
359 opts.fQuality = 99;
360 stream.reset();
362 auto dataLossy = stream.detachAsData();
363
364 enum Format {
365 kMixed = 0,
366 kLossy = 1,
367 kLossless = 2,
368 };
369
370 auto test = [&r](const sk_sp<SkData>& data, Format expected) {
371 auto printFormat = [](int f) {
372 switch (f) {
373 case kMixed: return "mixed";
374 case kLossy: return "lossy";
375 case kLossless: return "lossless";
376 default: return "unknown";
377 }
378 };
379
380 if (!data) {
381 ERRORF(r, "Failed to encode. Expected %s", printFormat(expected));
382 return;
383 }
384
385 WebPBitstreamFeatures features;
386 auto status = WebPGetFeatures(data->bytes(), data->size(), &features);
387 if (status != VP8_STATUS_OK) {
388 ERRORF(r, "Encode had an error %i. Expected %s", status, printFormat(expected));
389 return;
390 }
391
392 if (expected != features.format) {
393 ERRORF(r, "Expected %s encode, but got format %s", printFormat(expected),
394 printFormat(features.format));
395 }
396 };
397
398 test(dataLossy, kLossy);
399 test(dataLossLess, kLossless);
400}
401#endif
402
403DEF_TEST(Encode_WebpOptions, r) {
405 bool success = ToolUtils::GetResourceAsBitmap("images/google_chrome.ico", &bitmap);
406 if (!success) {
407 return;
408 }
409
411 success = bitmap.peekPixels(&src);
412 REPORTER_ASSERT(r, success);
413 if (!success) {
414 return;
415 }
416
417 SkDynamicMemoryWStream dst0, dst1, dst2, dst3;
420 options.fQuality = 0.0f;
421 success = SkWebpEncoder::Encode(&dst0, src, options);
422 REPORTER_ASSERT(r, success);
423
424 options.fQuality = 100.0f;
425 success = SkWebpEncoder::Encode(&dst1, src, options);
426 REPORTER_ASSERT(r, success);
427
429 options.fQuality = 100.0f;
430 success = SkWebpEncoder::Encode(&dst2, src, options);
431 REPORTER_ASSERT(r, success);
432
434 options.fQuality = 50.0f;
435 success = SkWebpEncoder::Encode(&dst3, src, options);
436 REPORTER_ASSERT(r, success);
437
438 sk_sp<SkData> data0 = dst0.detachAsData();
439 sk_sp<SkData> data1 = dst1.detachAsData();
440 sk_sp<SkData> data2 = dst2.detachAsData();
441 sk_sp<SkData> data3 = dst3.detachAsData();
442 REPORTER_ASSERT(r, data0->size() > data1->size());
443 REPORTER_ASSERT(r, data1->size() > data2->size());
444 REPORTER_ASSERT(r, data2->size() > data3->size());
445
446 SkBitmap bm0, bm1, bm2, bm3;
451 REPORTER_ASSERT(r, almost_equals(bm0, bm1, 0));
452 REPORTER_ASSERT(r, almost_equals(bm0, bm2, 90));
453 REPORTER_ASSERT(r, almost_equals(bm2, bm3, 50));
454}
455
456DEF_TEST(Encode_WebpAnimated, r) {
457 const int frameCount = 3;
458 const int width = 16;
459 const int height = 16;
461 std::vector<SkBitmap> bitmaps(frameCount);
462 std::vector<SkEncoder::Frame> frames(frameCount);
463 std::vector<int> durations = {50, 100, 150};
464 std::vector<SkColor> colors = {SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN};
465
466 for (int i = 0; i < frameCount; i++) {
467 bitmaps[i].allocPixels(info);
468 bitmaps[i].eraseColor(colors[i]);
469 REPORTER_ASSERT(r, bitmaps[i].peekPixels(&frames[i].pixmap));
470 frames[i].duration = durations[i];
471 }
472
476 options.fQuality = 100;
477
479
480 auto codec = SkCodec::MakeFromData(stream.detachAsData());
481 REPORTER_ASSERT(r, !!codec);
482
483 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
484 REPORTER_ASSERT(r, frameInfos.size() == frameCount);
485
486 for (size_t i = 0; i < frameInfos.size(); ++i) {
488 bitmap.allocPixels(info);
489 bitmap.eraseColor(0);
490
491 SkCodec::Options codecOptions;
492 codecOptions.fFrameIndex = (int)i;
493
494 auto result = codec->getPixels(info, bitmap.getPixels(), bitmap.rowBytes(), &codecOptions);
495 if (result != SkCodec::kSuccess) {
496 ERRORF(r, "error in frame %zu: %s", i, SkCodec::ResultToString(result));
497 }
498
499 REPORTER_ASSERT(r, almost_equals(bitmap, bitmaps[i], 0));
500 REPORTER_ASSERT(r, frameInfos[i].fDuration == durations[i]);
501 }
502}
503
504DEF_TEST(Encode_WebpAnimated_FrameUnmatched, r) {
505 // Create two frames with unmatched sizes and verify the encode should fail.
506 SkEncoder::Frame frame1;
507 SkBitmap bm1;
510 REPORTER_ASSERT(r, bm1.peekPixels(&frame1.pixmap));
511 frame1.duration = 200;
512
513 SkEncoder::Frame frame2;
514 SkBitmap bm2;
517 REPORTER_ASSERT(r, bm2.peekPixels(&frame2.pixmap));
518 frame2.duration = 200;
519
523 options.fQuality = 100;
524 std::vector<SkEncoder::Frame> frames = {frame1, frame2};
527}
528
529DEF_TEST(Encode_Alpha, r) {
530 // These formats have no sensible way to encode alpha images.
534 for (int ctAsInt = kUnknown_SkColorType + 1; ctAsInt <= kLastEnum_SkColorType; ctAsInt++) {
535 auto ct = static_cast<SkColorType>(ctAsInt);
536 // Non-alpha-only colortypes are tested elsewhere.
537 if (!SkColorTypeIsAlphaOnly(ct)) continue;
538 SkBitmap bm;
542 bool success = false;
544 success = SkJpegEncoder::Encode(&stream, bm.pixmap(), {});
545 } else if (format == SkEncodedImageFormat::kPNG) {
546 success = SkPngEncoder::Encode(&stream, bm.pixmap(), {});
547 } else {
548 success = SkWebpEncoder::Encode(&stream, bm.pixmap(), {});
549 }
550
552 ct == kAlpha_8_SkColorType) {
553 // We support encoding alpha8 to png and jpeg with our own private meaning.
554 REPORTER_ASSERT(r, success);
555 REPORTER_ASSERT(r, stream.bytesWritten() > 0);
556 } else {
557 REPORTER_ASSERT(r, !success);
558 REPORTER_ASSERT(r, stream.bytesWritten() == 0);
559 }
560 }
561 }
562}
const char * options
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
static bool almost_equals(SkPMColor a, SkPMColor b, int tolerance)
Definition: EncodeTest.cpp:116
static std::unique_ptr< SkEncoder > make(SkEncodedImageFormat format, SkWStream *dst, const SkPixmap &src)
Definition: EncodeTest.cpp:56
DEF_TEST(Encode, r)
Definition: EncodeTest.cpp:111
static void testPngComments(const SkPixmap &src, SkPngEncoder::Options &options, skiatest::Reporter *r)
Definition: EncodeTest.cpp:238
static void pushComment(std::vector< std::string > &comments, const char *keyword, const char *text)
Definition: EncodeTest.cpp:232
static void test_encode(skiatest::Reporter *r, SkEncodedImageFormat format)
Definition: EncodeTest.cpp:68
static bool encode(SkEncodedImageFormat format, SkWStream *dst, const SkPixmap &src)
Definition: EncodeTest.cpp:45
#define test(name)
sk_bzero(glyphs, sizeof(glyphs))
kUnpremul_SkAlphaType
@ kOpaque_SkAlphaType
pixel is opaque
Definition: SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
#define SkASSERT_RELEASE(cond)
Definition: SkAssert.h:100
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define SkGetPackedB32(packed)
Definition: SkColorPriv.h:95
#define SkGetPackedR32(packed)
Definition: SkColorPriv.h:93
#define SkGetPackedA32(packed)
Definition: SkColorPriv.h:92
#define SkGetPackedG32(packed)
Definition: SkColorPriv.h:94
SkColorType
Definition: SkColorType.h:19
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
Definition: SkColorType.h:23
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition: SkColorType.h:38
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition: SkColorType.h:21
@ kLastEnum_SkColorType
last valid value
Definition: SkColorType.h:56
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition: SkColorType.h:35
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition: SkColorType.h:22
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
@ kUnknown_SkColorType
uninitialized
Definition: SkColorType.h:20
constexpr SkColor SK_ColorYELLOW
Definition: SkColor.h:139
uint32_t SkPMColor
Definition: SkColor.h:205
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
SkEncodedImageFormat
static bool SkColorTypeIsAlphaOnly(SkColorType ct)
static T SkTAbs(T value)
Definition: SkTemplates.h:43
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
#define ERRORF(r,...)
Definition: Test.h:293
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
size_t computeByteSize() const
Definition: SkBitmap.h:293
const SkPixmap & pixmap() const
Definition: SkBitmap.h:133
void * getPixels() const
Definition: SkBitmap.h:283
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
bool peekPixels(SkPixmap *pixmap) const
Definition: SkBitmap.cpp:635
void eraseColor(SkColor4f) const
Definition: SkBitmap.cpp:442
static std::unique_ptr< SkCodec > MakeFromData(sk_sp< SkData >, SkSpan< const SkCodecs::Decoder > decoders, SkPngChunkReader *=nullptr)
Definition: SkCodec.cpp:241
static const char * ResultToString(Result)
Definition: SkCodec.cpp:880
@ kSuccess
Definition: SkCodec.h:80
static sk_sp< SkDataTable > MakeCopyArrays(const void *const *ptrs, const size_t sizes[], int count)
Definition: SkDataTable.cpp:92
bool equals(const SkData *other) const
Definition: SkData.cpp:43
size_t size() const
Definition: SkData.h:30
sk_sp< SkData > detachAsData()
Definition: SkStream.cpp:707
int width() const
Definition: SkImage.h:285
int height() const
Definition: SkImage.h:291
bool asLegacyBitmap(SkBitmap *bitmap, LegacyBitmapMode legacyBitmapMode=kRO_LegacyBitmapMode) const
Definition: SkImage.cpp:233
T * get() const
Definition: SkRefCnt.h:303
VkSurfaceKHR surface
Definition: main.cc:49
static bool b
struct MyStruct a[10]
GAsyncResult * result
uint32_t uint32_t * format
std::u16string text
double y
double x
SK_API sk_sp< SkImage > DeferredFromEncodedData(sk_sp< SkData > encoded, std::optional< SkAlphaType > alphaType=std::nullopt)
SK_API std::unique_ptr< SkEncoder > Make(SkWStream *dst, const SkPixmap &src, const Options &options)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
SK_API std::unique_ptr< SkEncoder > Make(SkWStream *dst, const SkPixmap &src, const Options &options)
sk_sp< const SkImage > image
Definition: SkRecords.h:269
PODArray< SkColor > colors
Definition: SkRecords.h:276
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SK_API bool EncodeAnimated(SkWStream *dst, SkSpan< const SkEncoder::Frame > src, const Options &options)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
bool GetResourceAsBitmap(const char *resource, SkBitmap *dst)
Definition: DecodeUtils.h:21
Definition: bitmap.py:1
dst
Definition: cp.py:12
def Format(template, **parameters)
Definition: emitter.py:13
int32_t height
int32_t width
SkPixmap pixmap
Definition: SkEncoder.h:32
static SkImageInfo MakeN32Premul(int width, int height)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
AlphaOption fAlphaOption
Definition: SkJpegEncoder.h:73
Compression fCompression
Definition: SkWebpEncoder.h:43
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63