split RGB into YUV planes
Change-Id: Ifd1d010068691e187561a25aff45d8b67ae62c39 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/215442 Commit-Queue: Mike Reed <reed@google.com> Reviewed-by: Robert Phillips <robertphillips@google.com>
This commit is contained in:
parent
e85067f29d
commit
6a5f7e29a8
@ -1257,3 +1257,125 @@ private:
|
||||
DEF_GM(return new YUVMakeColorSpaceGM();)
|
||||
|
||||
}
|
||||
|
||||
///////////////
|
||||
|
||||
#include "tools/Resources.h"
|
||||
#include "src/core/SkYUVMath.h"
|
||||
#include "src/core/SkAutoPixmapStorage.h"
|
||||
#include "include/effects/SkColorMatrix.h"
|
||||
|
||||
static void draw_into_alpha(const SkImage* img, sk_sp<SkColorFilter> cf, const SkPixmap& dst) {
|
||||
auto canvas = SkCanvas::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes());
|
||||
canvas->scale(1.0f * dst.width() / img->width(), 1.0f * dst.height() / img->height());
|
||||
SkPaint paint;
|
||||
paint.setFilterQuality(kLow_SkFilterQuality);
|
||||
paint.setColorFilter(cf);
|
||||
paint.setBlendMode(SkBlendMode::kSrc);
|
||||
canvas->drawImage(img, 0, 0, &paint);
|
||||
}
|
||||
|
||||
static void split_into_yuv(const SkImage* img, SkYUVColorSpace cs, const SkPixmap dst[3]) {
|
||||
float m[20];
|
||||
SkColorMatrix_RGB2YUV(cs, m);
|
||||
|
||||
memcpy(m + 15, m + 0, 5 * sizeof(float)); // copy Y into A
|
||||
draw_into_alpha(img, SkColorFilters::Matrix(m), dst[0]);
|
||||
|
||||
memcpy(m + 15, m + 5, 5 * sizeof(float)); // copy U into A
|
||||
draw_into_alpha(img, SkColorFilters::Matrix(m), dst[1]);
|
||||
|
||||
memcpy(m + 15, m + 10, 5 * sizeof(float)); // copy V into A
|
||||
draw_into_alpha(img, SkColorFilters::Matrix(m), dst[2]);
|
||||
}
|
||||
|
||||
static void draw_diff(SkCanvas* canvas, SkScalar x, SkScalar y,
|
||||
const SkImage* a, const SkImage* b) {
|
||||
auto sh = SkShaders::Blend(SkBlendMode::kDifference, a->makeShader(), b->makeShader());
|
||||
SkPaint paint;
|
||||
paint.setShader(sh);
|
||||
canvas->save();
|
||||
canvas->translate(x, y);
|
||||
canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
|
||||
|
||||
SkColorMatrix cm;
|
||||
cm.setScale(64, 64, 64);
|
||||
paint.setShader(sh->makeWithColorFilter(SkColorFilters::Matrix(cm)));
|
||||
canvas->translate(0, a->height());
|
||||
canvas->drawRect(SkRect::MakeWH(a->width(), a->height()), paint);
|
||||
|
||||
canvas->restore();
|
||||
}
|
||||
|
||||
// Exercises SkColorMatrix_RGB2YUV for yuv colorspaces, showing the planes, and the
|
||||
// resulting (recombined) images (gpu only for now).
|
||||
//
|
||||
class YUVSplitterGM : public skiagm::GM {
|
||||
sk_sp<SkImage> fOrig;
|
||||
SkAutoPixmapStorage fStorage[3];
|
||||
SkPixmap fPM[3];
|
||||
|
||||
public:
|
||||
YUVSplitterGM() {}
|
||||
|
||||
protected:
|
||||
|
||||
SkString onShortName() override {
|
||||
return SkString("yuv_splitter");
|
||||
}
|
||||
|
||||
SkISize onISize() override {
|
||||
return SkISize::Make(1024, 768);
|
||||
}
|
||||
|
||||
void onOnceBeforeDraw() override {
|
||||
fOrig = GetResourceAsImage("images/mandrill_256.png");
|
||||
|
||||
SkImageInfo info = SkImageInfo::Make(fOrig->width(), fOrig->height(), kAlpha_8_SkColorType,
|
||||
kPremul_SkAlphaType);
|
||||
fStorage[0].alloc(info);
|
||||
if (0) {
|
||||
// if you want to scale U,V down by 1/2
|
||||
info = info.makeWH(info.width()/2, info.height()/2);
|
||||
}
|
||||
fStorage[1].alloc(info);
|
||||
fStorage[2].alloc(info);
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
fPM[i] = fStorage[i];
|
||||
}
|
||||
}
|
||||
|
||||
void onDraw(SkCanvas* canvas) override {
|
||||
SkYUVAIndex indices[4];
|
||||
indices[SkYUVAIndex::kY_Index] = {0, SkColorChannel::kR};
|
||||
indices[SkYUVAIndex::kU_Index] = {1, SkColorChannel::kR};
|
||||
indices[SkYUVAIndex::kV_Index] = {2, SkColorChannel::kR};
|
||||
indices[SkYUVAIndex::kA_Index] = {-1, SkColorChannel::kR};
|
||||
|
||||
canvas->translate(fOrig->width(), 0);
|
||||
canvas->save();
|
||||
for (auto cs : {kRec709_SkYUVColorSpace, kRec601_SkYUVColorSpace, kJPEG_SkYUVColorSpace}) {
|
||||
split_into_yuv(fOrig.get(), cs, fPM);
|
||||
auto img = SkImage::MakeFromYUVAPixmaps(canvas->getGrContext(), cs, fPM, indices,
|
||||
fPM[0].info().dimensions(),
|
||||
kTopLeft_GrSurfaceOrigin,
|
||||
false, false, nullptr);
|
||||
if (img) {
|
||||
canvas->drawImage(img, 0, 0, nullptr);
|
||||
draw_diff(canvas, 0, fOrig->height(), fOrig.get(), img.get());
|
||||
}
|
||||
canvas->translate(fOrig->width(), 0);
|
||||
}
|
||||
canvas->restore();
|
||||
canvas->translate(-fOrig->width(), 0);
|
||||
|
||||
canvas->drawImage(SkImage::MakeRasterCopy(fPM[0]), 0, 0, nullptr);
|
||||
canvas->drawImage(SkImage::MakeRasterCopy(fPM[1]), 0, fPM[0].height(), nullptr);
|
||||
canvas->drawImage(SkImage::MakeRasterCopy(fPM[2]),
|
||||
0, fPM[0].height() + fPM[1].height(), nullptr);
|
||||
}
|
||||
|
||||
private:
|
||||
typedef GM INHERITED;
|
||||
};
|
||||
DEF_GM( return new YUVSplitterGM; )
|
||||
|
@ -403,6 +403,7 @@ skia_core_sources = [
|
||||
"$_src/core/SkYUVPlanesCache.cpp",
|
||||
"$_src/core/SkYUVPlanesCache.h",
|
||||
"$_src/core/SkYUVASizeInfo.cpp",
|
||||
"$_src/core/SkYUVMath.cpp",
|
||||
|
||||
"$_src/image/SkImage.cpp",
|
||||
|
||||
|
@ -16,6 +16,14 @@ public:
|
||||
void setIdentity();
|
||||
void setScale(float rScale, float gScale, float bScale, float aScale = 1.0f);
|
||||
|
||||
void setRowMajor(const float src[20]) {
|
||||
memcpy(fMat, src, sizeof(fMat));
|
||||
}
|
||||
|
||||
void getRowMajor(float dst[20]) const {
|
||||
memcpy(dst, fMat, sizeof(fMat));
|
||||
}
|
||||
|
||||
enum Axis {
|
||||
kR_Axis = 0,
|
||||
kG_Axis = 1,
|
||||
|
201
src/core/SkYUVMath.cpp
Normal file
201
src/core/SkYUVMath.cpp
Normal file
@ -0,0 +1,201 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "SkYUVMath.h"
|
||||
#include "include/core/SkMatrix44.h"
|
||||
|
||||
// in SkColorMatrix order (row-major)
|
||||
// Created by running SkColorMatrix_DumpYUVMatrixTables()
|
||||
|
||||
const float Rec709_rgb_to_yuv[] = {
|
||||
0.182586f, 0.614231f, 0.062007f, 0.000000f, 0.062745f,
|
||||
-0.100644f, -0.338572f, 0.439216f, 0.000000f, 0.501961f,
|
||||
0.439216f, -0.398942f, -0.040274f, 0.000000f, 0.501961f,
|
||||
0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
|
||||
};
|
||||
const float Rec709_yuv_to_rgb[] = {
|
||||
1.164384f, 0.000000f, 1.792741f, 0.000000f, -0.972945f,
|
||||
1.164384f, -0.213249f, -0.532909f, 0.000000f, 0.301483f,
|
||||
1.164384f, 2.112402f, 0.000000f, 0.000000f, -1.133402f,
|
||||
0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
|
||||
};
|
||||
const float Rec601_rgb_to_yuv[] = {
|
||||
0.256788f, 0.504129f, 0.097906f, 0.000000f, 0.062745f,
|
||||
-0.148223f, -0.290993f, 0.439216f, 0.000000f, 0.501961f,
|
||||
0.439216f, -0.367788f, -0.071427f, 0.000000f, 0.501961f,
|
||||
0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
|
||||
};
|
||||
const float Rec601_yuv_to_rgb[] = {
|
||||
1.164384f, 0.000000f, 1.596027f, 0.000000f, -0.874202f,
|
||||
1.164384f, -0.391762f, -0.812968f, 0.000000f, 0.531668f,
|
||||
1.164384f, 2.017232f, 0.000000f, 0.000000f, -1.085631f,
|
||||
0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
|
||||
};
|
||||
const float JPEG_rgb_to_yuv[] = {
|
||||
0.299000f, 0.587000f, 0.114000f, 0.000000f, 0.000000f,
|
||||
-0.168736f, -0.331264f, 0.500000f, 0.000000f, 0.501961f,
|
||||
0.500000f, -0.418688f, -0.081312f, 0.000000f, 0.501961f,
|
||||
0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
|
||||
};
|
||||
const float JPEG_yuv_to_rgb[] = {
|
||||
1.000000f, 0.000000f, 1.402000f, 0.000000f, -0.703749f,
|
||||
1.000000f, -0.344136f, -0.714136f, 0.000000f, 0.531211f,
|
||||
1.000000f, 1.772000f, 0.000000f, 0.000000f, -0.889475f,
|
||||
0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
|
||||
};
|
||||
|
||||
static_assert(kJPEG_SkYUVColorSpace == 0, "");
|
||||
static_assert(kRec601_SkYUVColorSpace == 1, "");
|
||||
static_assert(kRec709_SkYUVColorSpace == 2, "");
|
||||
|
||||
const float* yuv_to_rgb_array[] = {
|
||||
JPEG_yuv_to_rgb,
|
||||
Rec601_yuv_to_rgb,
|
||||
Rec709_yuv_to_rgb,
|
||||
};
|
||||
|
||||
const float* rgb_to_yuv_array[] = {
|
||||
JPEG_rgb_to_yuv,
|
||||
Rec601_rgb_to_yuv,
|
||||
Rec709_rgb_to_yuv,
|
||||
};
|
||||
|
||||
constexpr size_t kSizeOfColorMatrix = 20 * sizeof(float);
|
||||
|
||||
void SkColorMatrix_RGB2YUV(SkYUVColorSpace cs, float m[20]) {
|
||||
if ((unsigned)cs < (unsigned)kIdentity_SkYUVColorSpace) {
|
||||
memcpy(m, rgb_to_yuv_array[(unsigned)cs], kSizeOfColorMatrix);
|
||||
} else {
|
||||
memset(m, 0, kSizeOfColorMatrix);
|
||||
m[0] = m[6] = m[12] = m[18] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void SkColorMatrix_YUV2RGB(SkYUVColorSpace cs, float m[20]) {
|
||||
if ((unsigned)cs < (unsigned)kIdentity_SkYUVColorSpace) {
|
||||
memcpy(m, yuv_to_rgb_array[(unsigned)cs], kSizeOfColorMatrix);
|
||||
} else {
|
||||
memset(m, 0, kSizeOfColorMatrix);
|
||||
m[0] = m[6] = m[12] = m[18] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// we just drop the alpha rol/col from the colormatrix
|
||||
// output is | tr |
|
||||
// | 3x3 tg |
|
||||
// | tb |
|
||||
// | 0 0 0 1 |
|
||||
static void colormatrix_to_matrix44(const float src[20], SkMatrix44* dst) {
|
||||
for (int r = 0; r < 3; ++r) {
|
||||
for (int c = 0; c < 3; ++c) {
|
||||
dst->set(r, c, src[r*5 + c]);
|
||||
}
|
||||
dst->set(r, 3, src[r*5 + 4]);
|
||||
}
|
||||
dst->set(3, 0, 0);
|
||||
dst->set(3, 1, 0);
|
||||
dst->set(3, 2, 0);
|
||||
dst->set(3, 3, 1);
|
||||
}
|
||||
|
||||
// input: ignore the bottom row
|
||||
// output: inject identity row/column for alpha
|
||||
static void matrix44_to_colormatrix(const SkMatrix44& src, float dst[20]) {
|
||||
for (int r = 0; r < 3; ++r) {
|
||||
for (int c = 0; c < 3; ++c) {
|
||||
dst[r*5 + c] = src.get(r, c);
|
||||
}
|
||||
dst[r*5 + 3] = 0; // scale alpha
|
||||
dst[r*5 + 4] = src.get(r, 3); // translate
|
||||
}
|
||||
dst[15] = dst[16] = dst[17] = dst[19] = 0;
|
||||
dst[18] = 1;
|
||||
}
|
||||
|
||||
static void scale3(float m[], float s) {
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
m[i] *= s;
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
struct YUVCoeff {
|
||||
float Kr, Kb;
|
||||
float Cr, Cb;
|
||||
float scaleY, addY;
|
||||
float scaleUV;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
const YUVCoeff gCoeff[] = {
|
||||
// kJPEG_SkYUVColorSpace
|
||||
{ 0.299f, 0.114f, 1/1.772f, 1/1.402f, 1, 0, 1, },
|
||||
|
||||
// kRec601_SkYUVColorSpace
|
||||
{ 0.299f, 0.114f, 1/1.772f, 1/1.402f, 219/255.f, 16/255.f, 224/255.f, },
|
||||
|
||||
// kRec709_SkYUVColorSpace
|
||||
{ 0.2126f, 0.0722f, 1/1.8556f, 1/1.5748f, 219/255.f, 16/255.f, 224/255.f, },
|
||||
};
|
||||
|
||||
static void make_rgb_to_yuv_matrix(float mx[20], const YUVCoeff& c) {
|
||||
const float Kr = c.Kr;
|
||||
const float Kb = c.Kb;
|
||||
const float Kg = 1.0f - Kr - Kb;
|
||||
|
||||
float m[20] = {
|
||||
Kr, Kg, Kb, 0, c.addY,
|
||||
-Kr, -Kg, 1-Kb, 0, 128/255.f,
|
||||
1-Kr, -Kg, -Kb, 0, 128/255.f,
|
||||
0, 0, 0, 1, 0,
|
||||
};
|
||||
memcpy(mx, m, sizeof(m));
|
||||
scale3(mx + 0, c.scaleY);
|
||||
scale3(mx + 5, c.Cr * c.scaleUV);
|
||||
scale3(mx + 10, c.Cb * c.scaleUV);
|
||||
}
|
||||
|
||||
static void dump(const float m[20], SkYUVColorSpace cs, bool rgb2yuv) {
|
||||
const char* names[] = {
|
||||
"JPEG", "Rec601", "Rec709",
|
||||
};
|
||||
const char* dirnames[] = {
|
||||
"yuv_to_rgb", "rgb_to_yuv",
|
||||
};
|
||||
SkDebugf("const float %s_%s[] = {\n", names[cs], dirnames[rgb2yuv]);
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
SkDebugf(" ");
|
||||
for (int j = 0; j < 5; ++j) {
|
||||
SkDebugf(" %9.6ff,", m[i * 5 + j]);
|
||||
}
|
||||
SkDebugf("\n");
|
||||
}
|
||||
SkDebugf("};\n");
|
||||
}
|
||||
|
||||
// Used to create the prebuilt tables for each colorspace.
|
||||
// Don't remove this function, in case we want to recompute those tables in the future.
|
||||
void SkColorMatrix_DumpYUVMatrixTables() {
|
||||
for (auto cs : {kRec709_SkYUVColorSpace, kRec601_SkYUVColorSpace, kJPEG_SkYUVColorSpace}) {
|
||||
float m[20];
|
||||
make_rgb_to_yuv_matrix(m, gCoeff[(unsigned)cs]);
|
||||
dump(m, cs, true);
|
||||
SkMatrix44 m44, im44;
|
||||
colormatrix_to_matrix44(m, &m44);
|
||||
float im[20];
|
||||
#ifdef SK_DEBUG
|
||||
// be sure our coversion between matrix44 and colormatrix is perfect
|
||||
matrix44_to_colormatrix(m44, im);
|
||||
SkASSERT(memcmp(m, im, sizeof(im)) == 0);
|
||||
#endif
|
||||
SkAssertResult(m44.invert(&im44));
|
||||
matrix44_to_colormatrix(im44, im);
|
||||
dump(im, cs, false);
|
||||
}
|
||||
}
|
19
src/core/SkYUVMath.h
Normal file
19
src/core/SkYUVMath.h
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright 2019 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#ifndef SkYUVMath_DEFINED
|
||||
#define SkYUVMath_DEFINED
|
||||
|
||||
#include "include/core/SkImageInfo.h"
|
||||
|
||||
void SkColorMatrix_RGB2YUV(SkYUVColorSpace, float m[20]);
|
||||
void SkColorMatrix_YUV2RGB(SkYUVColorSpace, float m[20]);
|
||||
|
||||
// Used to create the pre-compiled tables in SkYUVMath.cpp
|
||||
void SkColorMatrix_DumpYUVMatrixTables();
|
||||
|
||||
#endif
|
@ -240,6 +240,10 @@ sk_sp<SkImage> SkImage::MakeFromYUVAPixmaps(
|
||||
GrContext* context, SkYUVColorSpace yuvColorSpace, const SkPixmap yuvaPixmaps[],
|
||||
const SkYUVAIndex yuvaIndices[4], SkISize imageSize, GrSurfaceOrigin imageOrigin,
|
||||
bool buildMips, bool limitToMaxTextureSize, sk_sp<SkColorSpace> imageColorSpace) {
|
||||
if (!context) {
|
||||
return nullptr; // until we impl this for raster backend
|
||||
}
|
||||
|
||||
int numPixmaps;
|
||||
if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numPixmaps)) {
|
||||
return nullptr;
|
||||
|
@ -124,3 +124,42 @@ DEF_TEST(Jpeg_YUV_Codec, r) {
|
||||
// A PNG should fail.
|
||||
codec_yuv(r, "images/arrow.png", nullptr);
|
||||
}
|
||||
|
||||
#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,
|
||||
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) {
|
||||
float r2y[20], y2r[20];
|
||||
SkColorMatrix_RGB2YUV(cs, r2y);
|
||||
SkColorMatrix_YUV2RGB(cs, y2r);
|
||||
|
||||
SkColorMatrix r2ym, y2rm;
|
||||
r2ym.setRowMajor(r2y);
|
||||
y2rm.setRowMajor(y2r);
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user