Chromium Code Reviews| Index: dm/DMSrcSink.cpp |
| diff --git a/dm/DMSrcSink.cpp b/dm/DMSrcSink.cpp |
| index 028dff886b444d5b54736794cd357f16b759b5ab..63801ada0f3fc32107ef0f2c9b6f117875e11f32 100644 |
| --- a/dm/DMSrcSink.cpp |
| +++ b/dm/DMSrcSink.cpp |
| @@ -9,6 +9,8 @@ |
| #include "SkAndroidCodec.h" |
| #include "SkCodec.h" |
| #include "SkCodecImageGenerator.h" |
| +#include "SkColorSpace.h" |
| +#include "SkColorSpacePriv.h" |
| #include "SkCommonFlags.h" |
| #include "SkData.h" |
| #include "SkDocument.h" |
| @@ -828,9 +830,10 @@ Name ImageGenSrc::name() const { |
| /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| -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 { |
| @@ -838,6 +841,17 @@ bool ColorCodecSrc::veto(SinkFlags flags) const { |
| 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 { |
| if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) { |
| return Error::Nonfatal("No need to test color correction to 565 backend."); |
| @@ -853,24 +867,85 @@ Error ColorCodecSrc::draw(SkCanvas* canvas) const { |
| return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str()); |
| } |
| - SkImageInfo decodeInfo = codec->getInfo().makeColorType(kN32_SkColorType); |
| + SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType); |
| + uint32_t width = info.width(); |
|
scroggo
2016/05/23 16:12:38
Why did you store these in local variables? It loo
msarett
2016/05/23 16:20:03
Can't remember, I think a draft implementation may
|
| + uint32_t height = info.height(); |
| SkBitmap bitmap; |
| - if (!bitmap.tryAllocPixels(decodeInfo)) { |
| + if (!bitmap.tryAllocPixels(info)) { |
| return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(), |
| - decodeInfo.width(), decodeInfo.height()); |
| + info.width(), info.height()); |
| + } |
| + |
| + SkImageInfo decodeInfo = info; |
| + if (kBaseline_Mode != fMode) { |
| + decodeInfo = decodeInfo.makeColorType(kRGBA_8888_SkColorType); |
| + } |
| + |
| + SkCodec::Result r = codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes()); |
| + if (SkCodec::kSuccess != r) { |
| + return SkStringPrintf("Couldn't getPixels %s. Error code %d", fPath.c_str(), r); |
| } |
| switch (fMode) { |
| case kBaseline_Mode: |
| - switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes())) { |
| - case SkCodec::kSuccess: |
| - break; |
| - default: |
| - // Everything else is considered a failure. |
| - return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str()); |
| + canvas->drawBitmap(bitmap, 0, 0); |
| + break; |
| + case kDst_HPZR30w_Mode: { |
| + sk_sp<SkColorSpace> srcSpace = sk_ref_sp(codec->getColorSpace()); |
| + if (!srcSpace) { |
| + return SkStringPrintf("Cannot test color correction without a src profile."); |
| + } else if (!srcSpace->gammas()->isValues()) { |
| + // 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. |
|
scroggo
2016/05/23 16:12:38
FIXME?
msarett
2016/05/23 16:20:03
Done.
|
| + 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 (uint32_t y = 0; y < height; y++) { |
| + for (uint32_t x = 0; x < 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], srcSpace->gammas()->fRed.fValue); |
| + src[1] = pow(src[1], srcSpace->gammas()->fGreen.fValue); |
| + src[2] = pow(src[2], srcSpace->gammas()->fBlue.fValue); |
| + |
| + // Conver to dst gamut. |
|
scroggo
2016/05/23 16:12:38
Convert*
msarett
2016/05/23 16:20:03
Done.
|
| + 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 / fDstSpace->gammas()->fRed.fValue); |
| + dst[1] = pow(dst[1], 1.0f / fDstSpace->gammas()->fGreen.fValue); |
| + dst[2] = pow(dst[2], 1.0f / fDstSpace->gammas()->fBlue.fValue); |
| + |
| + *pixelPtr = SkPackARGB32NoCheck(((*pixelPtr >> 24) & 0xFF), |
| + clampFloatToByte(dst[0]), |
| + clampFloatToByte(dst[1]), |
| + clampFloatToByte(dst[2])); |
| + } |
| + } |
| + |
| canvas->drawBitmap(bitmap, 0, 0); |
| break; |
| + } |
| default: |
| SkASSERT(false); |
| return "Invalid fMode"; |