Index: dm/DMSrcSink.cpp |
diff --git a/dm/DMSrcSink.cpp b/dm/DMSrcSink.cpp |
index a9851895bbb7910df08b380d1fe1177484573b98..755bae378edce7c7760ba7f7b0ef6e10419d0f73 100644 |
--- a/dm/DMSrcSink.cpp |
+++ b/dm/DMSrcSink.cpp |
@@ -6,13 +6,11 @@ |
*/ |
#include "DMSrcSink.h" |
-#include "Resources.h" |
#include "SkAndroidCodec.h" |
#include "SkCodec.h" |
#include "SkCodecImageGenerator.h" |
#include "SkColorSpace.h" |
#include "SkColorSpace_Base.h" |
-#include "SkColorSpaceXform.h" |
#include "SkCommonFlags.h" |
#include "SkData.h" |
#include "SkDocument.h" |
@@ -40,8 +38,6 @@ |
#if defined(SK_BUILD_FOR_WIN) |
#include "SkAutoCoInitialize.h" |
#endif |
- |
-#include "qcms.h" |
DEFINE_bool(multiPage, false, "For document-type backends, render the source" |
" into multiple pages"); |
@@ -855,14 +851,26 @@ |
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
-ColorCodecSrc::ColorCodecSrc(Path path, Mode mode) |
+ColorCodecSrc::ColorCodecSrc(Path path, Mode mode, sk_sp<SkColorSpace> dstSpace) |
: fPath(path) |
, fMode(mode) |
+ , fDstSpace(dstSpace) |
{} |
bool ColorCodecSrc::veto(SinkFlags flags) const { |
// Test to direct raster backends (8888 and 565). |
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 { |
@@ -897,69 +905,62 @@ |
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) { |
case kBaseline_Mode: |
canvas->drawBitmap(bitmap, 0, 0); |
break; |
case kDst_HPZR30w_Mode: { |
sk_sp<SkColorSpace> srcSpace = sk_ref_sp(codec->getColorSpace()); |
- sk_sp<SkColorSpace> dstSpace = SkColorSpace::NewICC(dstData->data(), dstData->size()); |
- SkASSERT(dstSpace); |
- |
- std::unique_ptr<SkColorSpaceXform> xform = SkColorSpaceXform::New(srcSpace, dstSpace); |
- if (!xform) { |
+ if (!srcSpace) { |
+ return SkStringPrintf("Cannot test color correction without a src profile."); |
+ } else if (!as_CSB(srcSpace)->gammas()->isValues()) { |
// FIXME (msarett): |
- // I haven't implemented conversions for all of the images that I've uploaded for |
+ // The conversion here doesn't cover all of the images that I've uploaded for |
// testing. Once we support all of them, this should be a fatal error. |
- return Error::Nonfatal("Unimplemented color conversion."); |
- } |
- |
- uint32_t* row = (uint32_t*) bitmap.getPixels(); |
+ return Error::Nonfatal("Unimplemented gamma conversion."); |
+ } |
+ |
+ // Build a matrix to transform to dst gamut. |
+ // 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++) { |
- xform->xform_RGBA_8888(row, row, info.width()); |
- row = SkTAddOffset<uint32_t>(row, bitmap.rowBytes()); |
- } |
- |
- canvas->drawBitmap(bitmap, 0, 0); |
- break; |
- } |
- 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())); |
- } |
- |
- SkAutoTCallVProc<qcms_profile, qcms_profile_release> |
- dstSpace(qcms_profile_from_memory(dstData->data(), dstData->size())); |
- SkASSERT(dstSpace); |
- SkAutoTCallVProc<qcms_transform, qcms_transform_release> |
- transform (qcms_transform_create(srcSpace, QCMS_DATA_RGBA_8, dstSpace, |
- QCMS_DATA_RGBA_8, QCMS_INTENT_PERCEPTUAL)); |
- if (!transform) { |
- return SkStringPrintf("QCMS cannot create transform for %s.\n", fPath.c_str()); |
- } |
- |
-#ifdef SK_PMCOLOR_IS_RGBA |
- qcms_output_type outType = QCMS_OUTPUT_RGBX; |
-#else |
- qcms_output_type outType = QCMS_OUTPUT_BGRX; |
-#endif |
- |
- // Perform color correction. |
- uint32_t* row = (uint32_t*) bitmap.getPixels(); |
- for (int y = 0; y < info.height(); y++) { |
- qcms_transform_data_type(transform, row, row, info.width(), outType); |
- row = SkTAddOffset<uint32_t>(row, bitmap.rowBytes()); |
+ for (int x = 0; x < info.width(); x++) { |
+ // Extract floats. |
+ 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. |
+ src[0] = pow(src[0], as_CSB(srcSpace)->gammas()->fRed.fValue); |
+ src[1] = pow(src[1], as_CSB(srcSpace)->gammas()->fGreen.fValue); |
+ src[2] = pow(src[2], as_CSB(srcSpace)->gammas()->fBlue.fValue); |
+ |
+ // Convert to dst gamut. |
+ float dst[3]; |
+ dst[0] = src[0]*srcToDst.getFloat(0, 0) + src[1]*srcToDst.getFloat(1, 0) + |
+ src[2]*srcToDst.getFloat(2, 0) + srcToDst.getFloat(3, 0); |
+ dst[1] = src[0]*srcToDst.getFloat(0, 1) + src[1]*srcToDst.getFloat(1, 1) + |
+ src[2]*srcToDst.getFloat(2, 1) + srcToDst.getFloat(3, 1); |
+ dst[2] = src[0]*srcToDst.getFloat(0, 2) + src[1]*srcToDst.getFloat(1, 2) + |
+ src[2]*srcToDst.getFloat(2, 2) + srcToDst.getFloat(3, 2); |
+ |
+ // Convert to dst gamma. |
+ dst[0] = pow(dst[0], 1.0f / as_CSB(fDstSpace)->gammas()->fRed.fValue); |
+ dst[1] = pow(dst[1], 1.0f / as_CSB(fDstSpace)->gammas()->fGreen.fValue); |
+ dst[2] = pow(dst[2], 1.0f / as_CSB(fDstSpace)->gammas()->fBlue.fValue); |
+ |
+ *pixelPtr = SkPackARGB32NoCheck(((*pixelPtr >> 24) & 0xFF), |
+ clampFloatToByte(dst[0]), |
+ clampFloatToByte(dst[1]), |
+ clampFloatToByte(dst[2])); |
+ } |
} |
canvas->drawBitmap(bitmap, 0, 0); |