2016-02-11 22:17:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/codec/SkCodec.h"
|
2020-08-24 13:18:16 +00:00
|
|
|
#include "include/core/SkPixmap.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "include/private/SkTemplates.h"
|
|
|
|
#include "src/core/SkAutoMalloc.h"
|
|
|
|
#include "tests/Test.h"
|
|
|
|
#include "tools/Resources.h"
|
2016-02-11 22:17:17 +00:00
|
|
|
|
|
|
|
static void codec_yuv(skiatest::Reporter* reporter,
|
2018-10-18 18:36:59 +00:00
|
|
|
const char path[],
|
2020-08-24 13:18:16 +00:00
|
|
|
const SkYUVAInfo* expectedInfo) {
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkStream> stream(GetResourceAsStream(path));
|
2016-02-11 22:17:17 +00:00
|
|
|
if (!stream) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-23 19:30:02 +00:00
|
|
|
std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
|
2016-02-11 22:17:17 +00:00
|
|
|
REPORTER_ASSERT(reporter, codec);
|
|
|
|
if (!codec) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-24 13:18:16 +00:00
|
|
|
// Test queryYUBAInfo()
|
2020-08-27 15:00:04 +00:00
|
|
|
SkYUVAPixmapInfo yuvaPixmapInfo;
|
2020-08-24 13:18:16 +00:00
|
|
|
|
2020-09-01 19:01:15 +00:00
|
|
|
static constexpr auto kAllTypes = SkYUVAPixmapInfo::SupportedDataTypes::All();
|
|
|
|
static constexpr auto kNoTypes = SkYUVAPixmapInfo::SupportedDataTypes();
|
|
|
|
|
|
|
|
// SkYUVAInfo param is required to be non-null.
|
|
|
|
bool success = codec->queryYUVAInfo(kAllTypes, nullptr);
|
|
|
|
REPORTER_ASSERT(reporter, !success);
|
|
|
|
// Fails when there is no support for YUVA planes.
|
|
|
|
success = codec->queryYUVAInfo(kNoTypes, &yuvaPixmapInfo);
|
2020-08-24 13:18:16 +00:00
|
|
|
REPORTER_ASSERT(reporter, !success);
|
2020-09-01 19:01:15 +00:00
|
|
|
|
|
|
|
success = codec->queryYUVAInfo(kAllTypes, &yuvaPixmapInfo);
|
2020-08-24 13:18:16 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkToBool(expectedInfo) == success);
|
|
|
|
if (!success) {
|
|
|
|
return;
|
2018-10-18 18:36:59 +00:00
|
|
|
}
|
2020-08-27 15:00:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, *expectedInfo == yuvaPixmapInfo.yuvaInfo());
|
2020-08-24 13:18:16 +00:00
|
|
|
|
2020-08-27 15:00:04 +00:00
|
|
|
int numPlanes = yuvaPixmapInfo.numPlanes();
|
2020-08-24 13:18:16 +00:00
|
|
|
REPORTER_ASSERT(reporter, numPlanes <= SkYUVAInfo::kMaxPlanes);
|
|
|
|
for (int i = 0; i < numPlanes; ++i) {
|
2020-08-27 15:00:04 +00:00
|
|
|
const SkImageInfo& planeInfo = yuvaPixmapInfo.planeInfo(i);
|
2020-09-01 19:01:15 +00:00
|
|
|
SkColorType planeCT = planeInfo.colorType();
|
2020-08-27 15:00:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, !planeInfo.isEmpty());
|
2020-09-01 19:01:15 +00:00
|
|
|
REPORTER_ASSERT(reporter, planeCT != kUnknown_SkColorType);
|
2020-08-27 15:00:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, planeInfo.validRowBytes(yuvaPixmapInfo.rowBytes(i)));
|
2020-09-01 19:01:15 +00:00
|
|
|
// Currently all planes must share a data type, gettable as SkYUVAPixmapInfo::dataType().
|
|
|
|
auto [numChannels, planeDataType] = SkYUVAPixmapInfo::NumChannelsAndDataType(planeCT);
|
|
|
|
REPORTER_ASSERT(reporter, planeDataType == yuvaPixmapInfo.dataType());
|
2020-08-24 13:18:16 +00:00
|
|
|
}
|
|
|
|
for (int i = numPlanes; i < SkYUVAInfo::kMaxPlanes; ++i) {
|
2020-08-27 15:00:04 +00:00
|
|
|
const SkImageInfo& planeInfo = yuvaPixmapInfo.planeInfo(i);
|
|
|
|
REPORTER_ASSERT(reporter, planeInfo.dimensions().isEmpty());
|
|
|
|
REPORTER_ASSERT(reporter, planeInfo.colorType() == kUnknown_SkColorType);
|
|
|
|
REPORTER_ASSERT(reporter, yuvaPixmapInfo.rowBytes(i) == 0);
|
2016-02-11 22:17:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 13:18:16 +00:00
|
|
|
// Allocate the memory for the YUV decode.
|
2020-08-27 15:00:04 +00:00
|
|
|
auto pixmaps = SkYUVAPixmaps::Allocate(yuvaPixmapInfo);
|
|
|
|
REPORTER_ASSERT(reporter, pixmaps.isValid());
|
|
|
|
|
|
|
|
for (int i = 0; i < SkYUVAPixmaps::kMaxPlanes; ++i) {
|
|
|
|
REPORTER_ASSERT(reporter, pixmaps.plane(i).info() == yuvaPixmapInfo.planeInfo(i));
|
|
|
|
}
|
|
|
|
for (int i = numPlanes; i < SkYUVAInfo::kMaxPlanes; ++i) {
|
|
|
|
REPORTER_ASSERT(reporter, pixmaps.plane(i).rowBytes() == 0);
|
2020-08-24 13:18:16 +00:00
|
|
|
}
|
2016-02-11 22:17:17 +00:00
|
|
|
|
2020-08-24 13:18:16 +00:00
|
|
|
// Test getYUVAPlanes()
|
2020-08-27 15:00:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkCodec::kSuccess == codec->getYUVAPlanes(pixmaps));
|
2016-02-11 22:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(Jpeg_YUV_Codec, r) {
|
2020-11-11 21:34:19 +00:00
|
|
|
auto setExpectations = [](SkISize dims, SkYUVAInfo::Subsampling subsampling) {
|
2020-08-24 13:18:16 +00:00
|
|
|
return SkYUVAInfo(dims,
|
2020-11-11 21:34:19 +00:00
|
|
|
SkYUVAInfo::PlaneConfig::kY_U_V,
|
|
|
|
subsampling,
|
2020-08-24 13:18:16 +00:00
|
|
|
kJPEG_Full_SkYUVColorSpace,
|
|
|
|
kTopLeft_SkEncodedOrigin,
|
|
|
|
SkYUVAInfo::Siting::kCentered,
|
|
|
|
SkYUVAInfo::Siting::kCentered);
|
|
|
|
};
|
|
|
|
|
2020-11-11 21:34:19 +00:00
|
|
|
SkYUVAInfo expectations = setExpectations({128, 128}, SkYUVAInfo::Subsampling::k420);
|
2020-08-24 13:18:16 +00:00
|
|
|
codec_yuv(r, "images/color_wheel.jpg", &expectations);
|
2016-02-11 22:17:17 +00:00
|
|
|
|
|
|
|
// H2V2
|
2020-11-11 21:34:19 +00:00
|
|
|
expectations = setExpectations({512, 512}, SkYUVAInfo::Subsampling::k420);
|
2020-08-24 13:18:16 +00:00
|
|
|
codec_yuv(r, "images/mandrill_512_q075.jpg", &expectations);
|
2016-02-11 22:17:17 +00:00
|
|
|
|
|
|
|
// H1V1
|
2020-11-11 21:34:19 +00:00
|
|
|
expectations = setExpectations({512, 512}, SkYUVAInfo::Subsampling::k444);
|
2020-08-24 13:18:16 +00:00
|
|
|
codec_yuv(r, "images/mandrill_h1v1.jpg", &expectations);
|
2016-02-11 22:17:17 +00:00
|
|
|
|
|
|
|
// H2V1
|
2020-11-11 21:34:19 +00:00
|
|
|
expectations = setExpectations({512, 512}, SkYUVAInfo::Subsampling::k422);
|
2020-08-24 13:18:16 +00:00
|
|
|
codec_yuv(r, "images/mandrill_h2v1.jpg", &expectations);
|
2016-02-11 22:17:17 +00:00
|
|
|
|
|
|
|
// Non-power of two dimensions
|
2020-11-11 21:34:19 +00:00
|
|
|
expectations = setExpectations({439, 154}, SkYUVAInfo::Subsampling::k420);
|
2020-08-24 13:18:16 +00:00
|
|
|
codec_yuv(r, "images/cropped_mandrill.jpg", &expectations);
|
2016-02-11 22:17:17 +00:00
|
|
|
|
2020-11-11 21:34:19 +00:00
|
|
|
expectations = setExpectations({8, 8}, SkYUVAInfo::Subsampling::k420);
|
2020-08-24 13:18:16 +00:00
|
|
|
codec_yuv(r, "images/randPixels.jpg", &expectations);
|
2016-02-11 22:17:17 +00:00
|
|
|
|
|
|
|
// Progressive images
|
2020-11-11 21:34:19 +00:00
|
|
|
expectations = setExpectations({512, 512}, SkYUVAInfo::Subsampling::k444);
|
2020-08-24 13:18:16 +00:00
|
|
|
codec_yuv(r, "images/brickwork-texture.jpg", &expectations);
|
|
|
|
codec_yuv(r, "images/brickwork_normal-map.jpg", &expectations);
|
2016-02-11 22:17:17 +00:00
|
|
|
|
|
|
|
// A CMYK encoded image should fail.
|
2017-12-08 15:21:31 +00:00
|
|
|
codec_yuv(r, "images/CMYK.jpg", nullptr);
|
2016-02-11 22:17:17 +00:00
|
|
|
// A grayscale encoded image should fail.
|
2017-12-08 15:21:31 +00:00
|
|
|
codec_yuv(r, "images/grayscale.jpg", nullptr);
|
2016-02-11 22:17:17 +00:00
|
|
|
// A PNG should fail.
|
2017-12-08 15:21:31 +00:00
|
|
|
codec_yuv(r, "images/arrow.png", nullptr);
|
2016-02-11 22:17:17 +00:00
|
|
|
}
|
2019-05-23 19:30:07 +00:00
|
|
|
|
|
|
|
#include "include/effects/SkColorMatrix.h"
|
|
|
|
#include "src/core/SkYUVMath.h"
|
|
|
|
|
|
|
|
// Be sure that the two matrices are inverses of each other
|
|
|
|
// (i.e. rgb2yuv and yuv2rgb
|
|
|
|
DEF_TEST(YUVMath, reporter) {
|
|
|
|
const SkYUVColorSpace spaces[] = {
|
|
|
|
kJPEG_SkYUVColorSpace,
|
|
|
|
kRec601_SkYUVColorSpace,
|
|
|
|
kRec709_SkYUVColorSpace,
|
2019-11-01 14:02:24 +00:00
|
|
|
kBT2020_SkYUVColorSpace,
|
2019-05-23 19:30:07 +00:00
|
|
|
kIdentity_SkYUVColorSpace,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Not sure what the theoretical precision we can hope for is, so pick a big value that
|
|
|
|
// passes (when I think we're correct).
|
|
|
|
const float tolerance = 1.0f/(1 << 18);
|
|
|
|
|
|
|
|
for (auto cs : spaces) {
|
2021-10-20 14:15:46 +00:00
|
|
|
SkColorMatrix r2ym = SkColorMatrix::RGBtoYUV(cs),
|
|
|
|
y2rm = SkColorMatrix::YUVtoRGB(cs);
|
2019-05-23 19:30:07 +00:00
|
|
|
r2ym.postConcat(y2rm);
|
|
|
|
|
|
|
|
float tmp[20];
|
|
|
|
r2ym.getRowMajor(tmp);
|
|
|
|
for (int i = 0; i < 20; ++i) {
|
|
|
|
float expected = 0;
|
|
|
|
if (i % 6 == 0) { // diagonal
|
|
|
|
expected = 1;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(tmp[i], expected, tolerance));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|