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/SkAlphaType.h"
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkImageInfo.h"
12 #include "tests/Test.h"
13 #include "tools/Resources.h"
14 #include "tools/ToolUtils.h"
15
16 #include <memory>
17
DEF_TEST(WebpCodecBlend,r)18 DEF_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++) {
36 SkCodec::Options options;
37 options.fFrameIndex = i;
38 auto result = codec->getPixels(premulBm.pixmap(), &options);
39 if (result != SkCodec::kSuccess) {
40 ERRORF(r, "Failed to decode %s frame %i (premul) - error %s", path, i,
41 SkCodec::ResultToString(result));
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
52 : kPremul_SkAlphaType;
53 result = codec->getPixels(premulInfo.makeAlphaType(alphaType), changeBm.getPixels(),
54 changeBm.rowBytes(), &options);
55 if (result != SkCodec::kSuccess) {
56 ERRORF(r, "Failed to decode %s frame %i (change) - error %s", path, i,
57 SkCodec::ResultToString(result));
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 }
65