skia2/tests/WebpTest.cpp
Mike Reed ac9f0c9e27 Bitmap.asImage()
... and lots and lots of IWYU

Change-Id: Ie5157dcdd2e6d29b95c71b39153278ab48ef4eb3
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/346778
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
2020-12-23 15:54:57 +00:00

61 lines
2.3 KiB
C++

/*
* Copyright 2020 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/codec/SkCodec.h"
#include "include/core/SkBitmap.h"
#include "tests/Test.h"
#include "tools/Resources.h"
#include "tools/ToolUtils.h"
DEF_TEST(WebpCodecBlend, r) {
const char* path = "images/blendBG.webp";
auto codec = SkCodec::MakeFromData(GetResourceAsData(path));
if (!codec) {
ERRORF(r, "Failed to open/decode %s", path);
return;
}
// Previously, a bug in SkWebpCodec resulted in different output depending
// on whether kPremul or kOpaque SkAlphaType was passed to getPixels().
// Decode each frame twice, once with kPremul and once with kOpaque if the
// frame is opaque, and verify they look the same.
auto premulInfo = codec->getInfo().makeAlphaType(kPremul_SkAlphaType);
SkBitmap premulBm, changeBm;
premulBm.allocPixels(premulInfo);
changeBm.allocPixels(premulInfo); // The SkBitmap's SkAlphaType is unrelated to the bug.
for (int i = 0; i < codec->getFrameCount(); i++) {
SkCodec::Options options;
options.fFrameIndex = i;
auto result = codec->getPixels(premulBm.pixmap(), &options);
if (result != SkCodec::kSuccess) {
ERRORF(r, "Failed to decode %s frame %i (premul) - error %s", path, i,
SkCodec::ResultToString(result));
return;
}
SkCodec::FrameInfo frameInfo;
if (!codec->getFrameInfo(i, &frameInfo)) {
ERRORF(r, "Failed to getFrameInfo for %s frame %i", path, i);
return;
}
auto alphaType = frameInfo.fAlphaType == kOpaque_SkAlphaType ? kOpaque_SkAlphaType
: kPremul_SkAlphaType;
result = codec->getPixels(premulInfo.makeAlphaType(alphaType), changeBm.getPixels(),
changeBm.rowBytes(), &options);
if (result != SkCodec::kSuccess) {
ERRORF(r, "Failed to decode %s frame %i (change) - error %s", path, i,
SkCodec::ResultToString(result));
return;
}
REPORTER_ASSERT(r, ToolUtils::equal_pixels(premulBm, changeBm), "%s frame %i does not match"
" with mismatched SkAlphaType", path, i);
}
}