xref: /third_party/skia/tests/WebpTest.cpp (revision cb93a386)
1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2020 Google LLC
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "include/codec/SkCodec.h"
9cb93a386Sopenharmony_ci#include "include/core/SkBitmap.h"
10cb93a386Sopenharmony_ci#include "tests/Test.h"
11cb93a386Sopenharmony_ci#include "tools/Resources.h"
12cb93a386Sopenharmony_ci#include "tools/ToolUtils.h"
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ciDEF_TEST(WebpCodecBlend, r) {
15cb93a386Sopenharmony_ci    const char* path = "images/blendBG.webp";
16cb93a386Sopenharmony_ci    auto codec = SkCodec::MakeFromData(GetResourceAsData(path));
17cb93a386Sopenharmony_ci    if (!codec) {
18cb93a386Sopenharmony_ci        ERRORF(r, "Failed to open/decode %s", path);
19cb93a386Sopenharmony_ci        return;
20cb93a386Sopenharmony_ci    }
21cb93a386Sopenharmony_ci
22cb93a386Sopenharmony_ci    // Previously, a bug in SkWebpCodec resulted in different output depending
23cb93a386Sopenharmony_ci    // on whether kPremul or kOpaque SkAlphaType was passed to getPixels().
24cb93a386Sopenharmony_ci    // Decode each frame twice, once with kPremul and once with kOpaque if the
25cb93a386Sopenharmony_ci    // frame is opaque, and verify they look the same.
26cb93a386Sopenharmony_ci    auto premulInfo = codec->getInfo().makeAlphaType(kPremul_SkAlphaType);
27cb93a386Sopenharmony_ci    SkBitmap premulBm, changeBm;
28cb93a386Sopenharmony_ci    premulBm.allocPixels(premulInfo);
29cb93a386Sopenharmony_ci    changeBm.allocPixels(premulInfo);   // The SkBitmap's SkAlphaType is unrelated to the bug.
30cb93a386Sopenharmony_ci
31cb93a386Sopenharmony_ci    for (int i = 0; i < codec->getFrameCount(); i++) {
32cb93a386Sopenharmony_ci        SkCodec::Options options;
33cb93a386Sopenharmony_ci        options.fFrameIndex = i;
34cb93a386Sopenharmony_ci        auto result = codec->getPixels(premulBm.pixmap(), &options);
35cb93a386Sopenharmony_ci        if (result != SkCodec::kSuccess) {
36cb93a386Sopenharmony_ci            ERRORF(r, "Failed to decode %s frame %i (premul) - error %s", path, i,
37cb93a386Sopenharmony_ci                   SkCodec::ResultToString(result));
38cb93a386Sopenharmony_ci            return;
39cb93a386Sopenharmony_ci        }
40cb93a386Sopenharmony_ci
41cb93a386Sopenharmony_ci        SkCodec::FrameInfo frameInfo;
42cb93a386Sopenharmony_ci        if (!codec->getFrameInfo(i, &frameInfo)) {
43cb93a386Sopenharmony_ci            ERRORF(r, "Failed to getFrameInfo for %s frame %i", path, i);
44cb93a386Sopenharmony_ci            return;
45cb93a386Sopenharmony_ci        }
46cb93a386Sopenharmony_ci
47cb93a386Sopenharmony_ci        auto alphaType = frameInfo.fAlphaType == kOpaque_SkAlphaType ? kOpaque_SkAlphaType
48cb93a386Sopenharmony_ci                                                                     : kPremul_SkAlphaType;
49cb93a386Sopenharmony_ci        result = codec->getPixels(premulInfo.makeAlphaType(alphaType), changeBm.getPixels(),
50cb93a386Sopenharmony_ci                                  changeBm.rowBytes(), &options);
51cb93a386Sopenharmony_ci        if (result != SkCodec::kSuccess) {
52cb93a386Sopenharmony_ci            ERRORF(r, "Failed to decode %s frame %i (change) - error %s", path, i,
53cb93a386Sopenharmony_ci                   SkCodec::ResultToString(result));
54cb93a386Sopenharmony_ci            return;
55cb93a386Sopenharmony_ci        }
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ci        REPORTER_ASSERT(r, ToolUtils::equal_pixels(premulBm, changeBm), "%s frame %i does not match"
58cb93a386Sopenharmony_ci                        " with mismatched SkAlphaType", path, i);
59cb93a386Sopenharmony_ci    }
60cb93a386Sopenharmony_ci}
61