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