Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
WebpTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 Google LLC
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
12#include "tests/Test.h"
13#include "tools/Resources.h"
14#include "tools/ToolUtils.h"
15
16#include <memory>
17
18DEF_TEST(WebpCodecBlend, r) {
19 const char* path = "images/blendBG.webp";
20 auto codec = SkCodec::MakeFromData(GetResourceAsData(path));
21 if (!codec) {
22 ERRORF(r, "Failed to open/decode %s", path);
23 return;
24 }
25
26 // Previously, a bug in SkWebpCodec resulted in different output depending
27 // on whether kPremul or kOpaque SkAlphaType was passed to getPixels().
28 // Decode each frame twice, once with kPremul and once with kOpaque if the
29 // frame is opaque, and verify they look the same.
30 auto premulInfo = codec->getInfo().makeAlphaType(kPremul_SkAlphaType);
31 SkBitmap premulBm, changeBm;
32 premulBm.allocPixels(premulInfo);
33 changeBm.allocPixels(premulInfo); // The SkBitmap's SkAlphaType is unrelated to the bug.
34
35 for (int i = 0; i < codec->getFrameCount(); i++) {
38 auto result = codec->getPixels(premulBm.pixmap(), &options);
40 ERRORF(r, "Failed to decode %s frame %i (premul) - error %s", path, i,
42 return;
43 }
44
45 SkCodec::FrameInfo frameInfo;
46 if (!codec->getFrameInfo(i, &frameInfo)) {
47 ERRORF(r, "Failed to getFrameInfo for %s frame %i", path, i);
48 return;
49 }
50
51 auto alphaType = frameInfo.fAlphaType == kOpaque_SkAlphaType ? kOpaque_SkAlphaType
53 result = codec->getPixels(premulInfo.makeAlphaType(alphaType), changeBm.getPixels(),
54 changeBm.rowBytes(), &options);
56 ERRORF(r, "Failed to decode %s frame %i (change) - error %s", path, i,
58 return;
59 }
60
61 REPORTER_ASSERT(r, ToolUtils::equal_pixels(premulBm, changeBm), "%s frame %i does not match"
62 " with mismatched SkAlphaType", path, i);
63 }
64}
const char * options
sk_sp< SkData > GetResourceAsData(const char *resource)
Definition Resources.cpp:42
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define DEF_TEST(name, reporter)
Definition Test.h:312
#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
const SkPixmap & pixmap() const
Definition SkBitmap.h:133
size_t rowBytes() const
Definition SkBitmap.h:238
void * getPixels() const
Definition SkBitmap.h:283
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
GAsyncResult * result
bool equal_pixels(const SkPixmap &a, const SkPixmap &b)
SkAlphaType fAlphaType
Definition SkCodec.h:689