Create SkColorSpaceXform to handle color conversions

Also adds testing of qcms color correction, so we can compare
SkColorSpaceXform outputs to qcms outputs.

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1952063002

Review-Url: https://codereview.chromium.org/1952063002
This commit is contained in:
msarett 2016-06-01 07:55:30 -07:00 committed by Commit bot
parent 30e78c9737
commit 740cc88ee3
11 changed files with 258 additions and 73 deletions

View File

@ -724,18 +724,15 @@ static bool gather_srcs() {
return false; return false;
} }
// Load the dstSpace. This particular dst is fairly similar to Adobe RGB.
SkAutoTUnref<SkData> data(SkData::NewFromFileName(
GetResourcePath("monitor_profiles/HP_ZR30w.icc").c_str()));
sk_sp<SkColorSpace> dstSpace = SkColorSpace::NewICC(data->data(), data->size());
SkASSERT(dstSpace);
for (auto colorImage : colorImages) { for (auto colorImage : colorImages) {
ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode, nullptr); ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode);
push_src("image", "color_codec_baseline", src); push_src("image", "color_codec_baseline", src);
src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode, dstSpace); src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode);
push_src("image", "color_codec_HPZR30w", src); push_src("image", "color_codec_HPZR30w", src);
src = new ColorCodecSrc(colorImage, ColorCodecSrc::kQCMS_HPZR30w_Mode);
push_src("image", "color_codec_QCMS_HPZR30w", src);
} }
return true; return true;

View File

@ -6,11 +6,13 @@
*/ */
#include "DMSrcSink.h" #include "DMSrcSink.h"
#include "Resources.h"
#include "SkAndroidCodec.h" #include "SkAndroidCodec.h"
#include "SkCodec.h" #include "SkCodec.h"
#include "SkCodecImageGenerator.h" #include "SkCodecImageGenerator.h"
#include "SkColorSpace.h" #include "SkColorSpace.h"
#include "SkColorSpace_Base.h" #include "SkColorSpace_Base.h"
#include "SkColorSpaceXform.h"
#include "SkCommonFlags.h" #include "SkCommonFlags.h"
#include "SkData.h" #include "SkData.h"
#include "SkDocument.h" #include "SkDocument.h"
@ -39,6 +41,8 @@
#include "SkAutoCoInitialize.h" #include "SkAutoCoInitialize.h"
#endif #endif
#include "qcms.h"
DEFINE_bool(multiPage, false, "For document-type backends, render the source" DEFINE_bool(multiPage, false, "For document-type backends, render the source"
" into multiple pages"); " into multiple pages");
DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?"); DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?");
@ -851,10 +855,9 @@ Name ImageGenSrc::name() const {
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
ColorCodecSrc::ColorCodecSrc(Path path, Mode mode, sk_sp<SkColorSpace> dstSpace) ColorCodecSrc::ColorCodecSrc(Path path, Mode mode)
: fPath(path) : fPath(path)
, fMode(mode) , fMode(mode)
, fDstSpace(dstSpace)
{} {}
bool ColorCodecSrc::veto(SinkFlags flags) const { bool ColorCodecSrc::veto(SinkFlags flags) const {
@ -862,17 +865,6 @@ bool ColorCodecSrc::veto(SinkFlags flags) const {
return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect; return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
} }
static uint8_t clampFloatToByte(float v) {
v = v * 255.0f;
if (v > 255.0f) {
return 255;
} else if (v < 0.0f) {
return 0;
} else {
return (uint8_t) (v + 0.5f);
}
}
Error ColorCodecSrc::draw(SkCanvas* canvas) const { Error ColorCodecSrc::draw(SkCanvas* canvas) const {
if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) { if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
return Error::Nonfatal("No need to test color correction to 565 backend."); return Error::Nonfatal("No need to test color correction to 565 backend.");
@ -905,62 +897,69 @@ Error ColorCodecSrc::draw(SkCanvas* canvas) const {
return SkStringPrintf("Couldn't getPixels %s. Error code %d", fPath.c_str(), r); return SkStringPrintf("Couldn't getPixels %s. Error code %d", fPath.c_str(), r);
} }
// Load the dst ICC profile. This particular dst is fairly similar to Adobe RGB.
sk_sp<SkData> dstData = SkData::MakeFromFileName(
GetResourcePath("monitor_profiles/HP_ZR30w.icc").c_str());
if (!dstData) {
return "Cannot read monitor profile. Is the resource path set correctly?";
}
switch (fMode) { switch (fMode) {
case kBaseline_Mode: case kBaseline_Mode:
canvas->drawBitmap(bitmap, 0, 0); canvas->drawBitmap(bitmap, 0, 0);
break; break;
case kDst_HPZR30w_Mode: { case kDst_HPZR30w_Mode: {
sk_sp<SkColorSpace> srcSpace = sk_ref_sp(codec->getColorSpace()); sk_sp<SkColorSpace> srcSpace = sk_ref_sp(codec->getColorSpace());
if (!srcSpace) { sk_sp<SkColorSpace> dstSpace = SkColorSpace::NewICC(dstData->data(), dstData->size());
return SkStringPrintf("Cannot test color correction without a src profile."); SkASSERT(dstSpace);
} else if (!as_CSB(srcSpace)->gammas()->isValues()) {
std::unique_ptr<SkColorSpaceXform> xform = SkColorSpaceXform::New(srcSpace, dstSpace);
if (!xform) {
// FIXME (msarett): // FIXME (msarett):
// The conversion here doesn't cover all of the images that I've uploaded for // I haven't implemented conversions for all of the images that I've uploaded for
// testing. Once we support all of them, this should be a fatal error. // testing. Once we support all of them, this should be a fatal error.
return Error::Nonfatal("Unimplemented gamma conversion."); return Error::Nonfatal("Unimplemented color conversion.");
} }
// Build a matrix to transform to dst gamut. uint32_t* row = (uint32_t*) bitmap.getPixels();
// srcToDst = inverse(dstToXYZ) * srcToXYZ
const SkMatrix44& srcToXYZ = srcSpace->xyz();
const SkMatrix44& dstToXYZ = fDstSpace->xyz();
SkMatrix44 srcToDst(SkMatrix44::kUninitialized_Constructor);
dstToXYZ.invert(&srcToDst);
srcToDst.postConcat(srcToXYZ);
for (int y = 0; y < info.height(); y++) { for (int y = 0; y < info.height(); y++) {
for (int x = 0; x < info.width(); x++) { xform->xform_RGBA_8888(row, row, info.width());
// Extract floats. row = SkTAddOffset<uint32_t>(row, bitmap.rowBytes());
uint32_t* pixelPtr = (uint32_t*) bitmap.getAddr(x, y); }
float src[3];
src[0] = ((*pixelPtr >> 0) & 0xFF) / 255.0f;
src[1] = ((*pixelPtr >> 8) & 0xFF) / 255.0f;
src[2] = ((*pixelPtr >> 16) & 0xFF) / 255.0f;
// Convert to linear. canvas->drawBitmap(bitmap, 0, 0);
src[0] = pow(src[0], as_CSB(srcSpace)->gammas()->fRed.fValue); break;
src[1] = pow(src[1], as_CSB(srcSpace)->gammas()->fGreen.fValue); }
src[2] = pow(src[2], as_CSB(srcSpace)->gammas()->fBlue.fValue); case kQCMS_HPZR30w_Mode: {
sk_sp<SkData> srcData = codec->getICCData();
SkAutoTCallVProc<qcms_profile, qcms_profile_release>
srcSpace(qcms_profile_from_memory(srcData->data(), srcData->size()));
if (!srcSpace) {
return Error::Nonfatal(SkStringPrintf("QCMS cannot create profile for %s.\n",
fPath.c_str()));
}
// Convert to dst gamut. SkAutoTCallVProc<qcms_profile, qcms_profile_release>
float dst[3]; dstSpace(qcms_profile_from_memory(dstData->data(), dstData->size()));
dst[0] = src[0]*srcToDst.getFloat(0, 0) + src[1]*srcToDst.getFloat(1, 0) + SkASSERT(dstSpace);
src[2]*srcToDst.getFloat(2, 0) + srcToDst.getFloat(3, 0); SkAutoTCallVProc<qcms_transform, qcms_transform_release>
dst[1] = src[0]*srcToDst.getFloat(0, 1) + src[1]*srcToDst.getFloat(1, 1) + transform (qcms_transform_create(srcSpace, QCMS_DATA_RGBA_8, dstSpace,
src[2]*srcToDst.getFloat(2, 1) + srcToDst.getFloat(3, 1); QCMS_DATA_RGBA_8, QCMS_INTENT_PERCEPTUAL));
dst[2] = src[0]*srcToDst.getFloat(0, 2) + src[1]*srcToDst.getFloat(1, 2) + if (!transform) {
src[2]*srcToDst.getFloat(2, 2) + srcToDst.getFloat(3, 2); return SkStringPrintf("QCMS cannot create transform for %s.\n", fPath.c_str());
}
// Convert to dst gamma. #ifdef SK_PMCOLOR_IS_RGBA
dst[0] = pow(dst[0], 1.0f / as_CSB(fDstSpace)->gammas()->fRed.fValue); qcms_output_type outType = QCMS_OUTPUT_RGBX;
dst[1] = pow(dst[1], 1.0f / as_CSB(fDstSpace)->gammas()->fGreen.fValue); #else
dst[2] = pow(dst[2], 1.0f / as_CSB(fDstSpace)->gammas()->fBlue.fValue); qcms_output_type outType = QCMS_OUTPUT_BGRX;
#endif
*pixelPtr = SkPackARGB32NoCheck(((*pixelPtr >> 24) & 0xFF), // Perform color correction.
clampFloatToByte(dst[0]), uint32_t* row = (uint32_t*) bitmap.getPixels();
clampFloatToByte(dst[1]), for (int y = 0; y < info.height(); y++) {
clampFloatToByte(dst[2])); qcms_transform_data_type(transform, row, row, info.width(), outType);
} row = SkTAddOffset<uint32_t>(row, bitmap.rowBytes());
} }
canvas->drawBitmap(bitmap, 0, 0); canvas->drawBitmap(bitmap, 0, 0);

View File

@ -212,9 +212,12 @@ public:
// TODO (msarett): Should we add a new test with a new monitor and verify that outputs // TODO (msarett): Should we add a new test with a new monitor and verify that outputs
// look identical on two different dsts? // look identical on two different dsts?
kDst_HPZR30w_Mode, kDst_HPZR30w_Mode,
// Use QCMS for color correction.
kQCMS_HPZR30w_Mode,
}; };
ColorCodecSrc(Path, Mode, sk_sp<SkColorSpace>); ColorCodecSrc(Path, Mode);
Error draw(SkCanvas*) const override; Error draw(SkCanvas*) const override;
SkISize size() const override; SkISize size() const override;
@ -223,7 +226,6 @@ public:
private: private:
Path fPath; Path fPath;
Mode fMode; Mode fMode;
sk_sp<SkColorSpace> fDstSpace;
}; };
class SKPSrc : public Src { class SKPSrc : public Src {

View File

@ -76,6 +76,7 @@
'<(skia_src_path)/core/SkColorShader.cpp', '<(skia_src_path)/core/SkColorShader.cpp',
'<(skia_src_path)/core/SkColorShader.h', '<(skia_src_path)/core/SkColorShader.h',
'<(skia_src_path)/core/SkColorSpace.cpp', '<(skia_src_path)/core/SkColorSpace.cpp',
'<(skia_src_path)/core/SkColorSpaceXform.cpp',
'<(skia_src_path)/core/SkColorTable.cpp', '<(skia_src_path)/core/SkColorTable.cpp',
'<(skia_src_path)/core/SkComposeShader.cpp', '<(skia_src_path)/core/SkComposeShader.cpp',
'<(skia_src_path)/core/SkConfig8888.cpp', '<(skia_src_path)/core/SkConfig8888.cpp',

View File

@ -14,6 +14,7 @@
'core.gyp:*', 'core.gyp:*',
'qcms.gyp:qcms', 'qcms.gyp:qcms',
], ],
'export_dependent_settings': [ 'qcms.gyp:qcms', ],
'include_dirs': [ 'include_dirs': [
'../include/effects', '../include/effects',
'../include/client/android', '../include/client/android',

View File

@ -19,7 +19,7 @@
'direct_dependent_settings': { 'direct_dependent_settings': {
'include_dirs': [ 'include_dirs': [
'./src', '../third_party/qcms/src/',
], ],
}, },

View File

@ -25,6 +25,7 @@ class SkSampler;
namespace DM { namespace DM {
class CodecSrc; class CodecSrc;
class ColorCodecSrc;
} }
/** /**
@ -695,6 +696,11 @@ protected:
virtual int onOutputScanline(int inputScanline) const; virtual int onOutputScanline(int inputScanline) const;
/**
* Used for testing with qcms.
* FIXME: Remove this when we are done comparing with qcms.
*/
virtual sk_sp<SkData> getICCData() const { return nullptr; }
private: private:
const SkEncodedInfo fEncodedInfo; const SkEncodedInfo fEncodedInfo;
const SkImageInfo fSrcInfo; const SkImageInfo fSrcInfo;
@ -770,6 +776,11 @@ private:
virtual SkSampler* getSampler(bool /*createIfNecessary*/) { return nullptr; } virtual SkSampler* getSampler(bool /*createIfNecessary*/) { return nullptr; }
friend class DM::CodecSrc; // for fillIncompleteImage friend class DM::CodecSrc; // for fillIncompleteImage
// For testing with qcms
// FIXME: Remove this when we are done comparing with qcms.
friend class DM::ColorCodecSrc;
friend class SkSampledCodec; friend class SkSampledCodec;
friend class SkIcoCodec; friend class SkIcoCodec;
}; };

View File

@ -120,7 +120,7 @@ static bool is_icc_marker(jpeg_marker_struct* marker) {
* (1) Discover all ICC profile markers and verify that they are numbered properly. * (1) Discover all ICC profile markers and verify that they are numbered properly.
* (2) Copy the data from each marker into a contiguous ICC profile. * (2) Copy the data from each marker into a contiguous ICC profile.
*/ */
static sk_sp<SkColorSpace> get_icc_profile(jpeg_decompress_struct* dinfo) { static sk_sp<SkData> get_icc_profile(jpeg_decompress_struct* dinfo) {
// Note that 256 will be enough storage space since each markerIndex is stored in 8-bits. // Note that 256 will be enough storage space since each markerIndex is stored in 8-bits.
jpeg_marker_struct* markerSequence[256]; jpeg_marker_struct* markerSequence[256];
memset(markerSequence, 0, sizeof(markerSequence)); memset(markerSequence, 0, sizeof(markerSequence));
@ -165,8 +165,8 @@ static sk_sp<SkColorSpace> get_icc_profile(jpeg_decompress_struct* dinfo) {
} }
// Combine the ICC marker data into a contiguous profile. // Combine the ICC marker data into a contiguous profile.
SkAutoMalloc iccData(totalBytes); sk_sp<SkData> iccData = SkData::MakeUninitialized(totalBytes);
void* dst = iccData.get(); void* dst = iccData->writable_data();
for (uint32_t i = 1; i <= numMarkers; i++) { for (uint32_t i = 1; i <= numMarkers; i++) {
jpeg_marker_struct* marker = markerSequence[i]; jpeg_marker_struct* marker = markerSequence[i];
if (!marker) { if (!marker) {
@ -180,7 +180,7 @@ static sk_sp<SkColorSpace> get_icc_profile(jpeg_decompress_struct* dinfo) {
dst = SkTAddOffset<void>(dst, bytes); dst = SkTAddOffset<void>(dst, bytes);
} }
return SkColorSpace::NewICC(iccData.get(), totalBytes); return iccData;
} }
bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut, bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut,
@ -221,7 +221,14 @@ bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut,
SkEncodedInfo info = SkEncodedInfo::Make(color, SkEncodedInfo::kOpaque_Alpha, 8); SkEncodedInfo info = SkEncodedInfo::Make(color, SkEncodedInfo::kOpaque_Alpha, 8);
Origin orientation = get_exif_orientation(decoderMgr->dinfo()); Origin orientation = get_exif_orientation(decoderMgr->dinfo());
sk_sp<SkColorSpace> colorSpace = get_icc_profile(decoderMgr->dinfo()); sk_sp<SkData> iccData = get_icc_profile(decoderMgr->dinfo());
sk_sp<SkColorSpace> colorSpace = nullptr;
if (iccData) {
colorSpace = SkColorSpace::NewICC(iccData->data(), iccData->size());
if (!colorSpace) {
SkCodecPrintf("Could not create SkColorSpace from ICC data.\n");
}
}
if (!colorSpace) { if (!colorSpace) {
// Treat unmarked jpegs as sRGB. // Treat unmarked jpegs as sRGB.
colorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); colorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
@ -230,7 +237,7 @@ bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut,
const int width = decoderMgr->dinfo()->image_width; const int width = decoderMgr->dinfo()->image_width;
const int height = decoderMgr->dinfo()->image_height; const int height = decoderMgr->dinfo()->image_height;
*codecOut = new SkJpegCodec(width, height, info, stream, decoderMgr.release(), *codecOut = new SkJpegCodec(width, height, info, stream, decoderMgr.release(),
std::move(colorSpace), orientation); std::move(colorSpace), orientation, std::move(iccData));
} else { } else {
SkASSERT(nullptr != decoderMgrOut); SkASSERT(nullptr != decoderMgrOut);
*decoderMgrOut = decoderMgr.release(); *decoderMgrOut = decoderMgr.release();
@ -251,11 +258,13 @@ SkCodec* SkJpegCodec::NewFromStream(SkStream* stream) {
} }
SkJpegCodec::SkJpegCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream, SkJpegCodec::SkJpegCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, Origin origin) JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, Origin origin,
sk_sp<SkData> iccData)
: INHERITED(width, height, info, stream, std::move(colorSpace), origin) : INHERITED(width, height, info, stream, std::move(colorSpace), origin)
, fDecoderMgr(decoderMgr) , fDecoderMgr(decoderMgr)
, fReadyState(decoderMgr->dinfo()->global_state) , fReadyState(decoderMgr->dinfo()->global_state)
, fSwizzlerSubset(SkIRect::MakeEmpty()) , fSwizzlerSubset(SkIRect::MakeEmpty())
, fICCData(std::move(iccData))
{} {}
/* /*

View File

@ -58,6 +58,8 @@ protected:
bool onDimensionsSupported(const SkISize&) override; bool onDimensionsSupported(const SkISize&) override;
sk_sp<SkData> getICCData() const override { return fICCData; }
private: private:
/* /*
@ -92,7 +94,8 @@ private:
* takes ownership * takes ownership
*/ */
SkJpegCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream, SkJpegCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, Origin origin); JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, Origin origin,
sk_sp<SkData> iccData);
/* /*
* Checks if the conversion between the input image and the requested output * Checks if the conversion between the input image and the requested output
@ -123,6 +126,8 @@ private:
SkIRect fSwizzlerSubset; SkIRect fSwizzlerSubset;
SkAutoTDelete<SkSwizzler> fSwizzler; SkAutoTDelete<SkSwizzler> fSwizzler;
sk_sp<SkData> fICCData;
typedef SkCodec INHERITED; typedef SkCodec INHERITED;
}; };

View File

@ -0,0 +1,109 @@
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkColorPriv.h"
#include "SkColorSpace_Base.h"
#include "SkColorSpaceXform.h"
bool compute_gamut_xform(SkMatrix44* srcToDst, const SkMatrix44& srcToXYZ,
const SkMatrix44& dstToXYZ) {
if (!dstToXYZ.invert(srcToDst)) {
return false;
}
srcToDst->postConcat(srcToXYZ);
return true;
}
std::unique_ptr<SkColorSpaceXform> SkColorSpaceXform::New(const sk_sp<SkColorSpace>& srcSpace,
const sk_sp<SkColorSpace>& dstSpace) {
if (!srcSpace || !dstSpace) {
return nullptr;
}
if (as_CSB(srcSpace)->gammas()->isValues() && as_CSB(dstSpace)->gammas()->isValues()) {
SkMatrix44 srcToDst(SkMatrix44::kUninitialized_Constructor);
if (!compute_gamut_xform(&srcToDst, srcSpace->xyz(), dstSpace->xyz())) {
return nullptr;
}
float srcGammas[3];
float dstGammas[3];
srcGammas[0] = as_CSB(srcSpace)->gammas()->fRed.fValue;
srcGammas[1] = as_CSB(srcSpace)->gammas()->fGreen.fValue;
srcGammas[2] = as_CSB(srcSpace)->gammas()->fBlue.fValue;
dstGammas[0] = 1.0f / as_CSB(dstSpace)->gammas()->fRed.fValue;
dstGammas[1] = 1.0f / as_CSB(dstSpace)->gammas()->fGreen.fValue;
dstGammas[2] = 1.0f / as_CSB(dstSpace)->gammas()->fBlue.fValue;
return std::unique_ptr<SkColorSpaceXform>(
new SkGammaByValueXform(srcGammas, srcToDst, dstGammas));
}
// Unimplemeted
return nullptr;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
SkGammaByValueXform::SkGammaByValueXform(float srcGammas[3], const SkMatrix44& srcToDst,
float dstGammas[3])
: fSrcToDst(srcToDst)
{
memcpy(fSrcGammas, srcGammas, 3 * sizeof(float));
memcpy(fDstGammas, dstGammas, 3 * sizeof(float));
}
static uint8_t clamp_float_to_byte(float v) {
v = v * 255.0f;
if (v > 255.0f) {
return 255;
} else if (v <= 0.0f) {
return 0;
} else {
return (uint8_t) (v + 0.5f);
}
}
void SkGammaByValueXform::xform_RGBA_8888(uint32_t* dst, const uint32_t* src, uint32_t len) const {
while (len-- > 0) {
float srcFloats[3];
srcFloats[0] = ((*src >> 0) & 0xFF) * (1.0f / 255.0f);
srcFloats[1] = ((*src >> 8) & 0xFF) * (1.0f / 255.0f);
srcFloats[2] = ((*src >> 16) & 0xFF) * (1.0f / 255.0f);
// Convert to linear.
srcFloats[0] = pow(srcFloats[0], fSrcGammas[0]);
srcFloats[1] = pow(srcFloats[1], fSrcGammas[1]);
srcFloats[2] = pow(srcFloats[2], fSrcGammas[2]);
// Convert to dst gamut.
float dstFloats[3];
dstFloats[0] = srcFloats[0] * fSrcToDst.getFloat(0, 0) +
srcFloats[1] * fSrcToDst.getFloat(1, 0) +
srcFloats[2] * fSrcToDst.getFloat(2, 0) + fSrcToDst.getFloat(3, 0);
dstFloats[1] = srcFloats[0] * fSrcToDst.getFloat(0, 1) +
srcFloats[1] * fSrcToDst.getFloat(1, 1) +
srcFloats[2] * fSrcToDst.getFloat(2, 1) + fSrcToDst.getFloat(3, 1);
dstFloats[2] = srcFloats[0] * fSrcToDst.getFloat(0, 2) +
srcFloats[1] * fSrcToDst.getFloat(1, 2) +
srcFloats[2] * fSrcToDst.getFloat(2, 2) + fSrcToDst.getFloat(3, 2);
// Convert to dst gamma.
dstFloats[0] = pow(dstFloats[0], fDstGammas[0]);
dstFloats[1] = pow(dstFloats[1], fDstGammas[1]);
dstFloats[2] = pow(dstFloats[2], fDstGammas[2]);
*dst = SkPackARGB32NoCheck(((*src >> 24) & 0xFF),
clamp_float_to_byte(dstFloats[0]),
clamp_float_to_byte(dstFloats[1]),
clamp_float_to_byte(dstFloats[2]));
dst++;
src++;
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkColorSpaceXform_DEFINED
#define SkColorSpaceXform_DEFINED
#include "SkColorSpace.h"
class SkColorSpaceXform {
public:
/**
* Create an object to handle color space conversions.
*
* @param srcSpace The encoded color space.
* @param dstSpace The destination color space.
*
*/
static std::unique_ptr<SkColorSpaceXform> New(const sk_sp<SkColorSpace>& srcSpace,
const sk_sp<SkColorSpace>& dstSpace);
/**
* Apply the color conversion to a src buffer, storing the output in the dst buffer.
* The src is stored in RGBA_8888 and the dst is stored in 8888 platform format.
* The output is not premultiplied.
*/
virtual void xform_RGBA_8888(uint32_t* dst, const uint32_t* src, uint32_t len) const = 0;
virtual ~SkColorSpaceXform() {}
};
class SkGammaByValueXform : public SkColorSpaceXform {
public:
void xform_RGBA_8888(uint32_t* dst, const uint32_t* src, uint32_t len) const override;
private:
SkGammaByValueXform(float srcGammas[3], const SkMatrix44& srcToDst, float dstGammas[3]);
float fSrcGammas[3];
const SkMatrix44 fSrcToDst;
float fDstGammas[3];
friend class SkColorSpaceXform;
};
#endif