From 966bb348a5bdeec44252ede4cb73ba907af2d92b Mon Sep 17 00:00:00 2001 From: Matt Sarett Date: Mon, 12 Dec 2016 16:30:13 -0500 Subject: [PATCH] Decode to sRGB on Android I want to land this so we can start testing color space aware decoding on Android. In particular, it will be interesting to see how linear premultiplication will affect existing content. This will only modify BitmapRegionDecoder behavior. I'll follow up with a similar change to BitmapFactory.cpp in Android. This will cause image diffs on Gold. BUG=skia: Change-Id: Iffda5f035447f2608ce26945570b503f8971b735 Reviewed-on: https://skia-review.googlesource.com/5698 Commit-Queue: Matt Sarett Reviewed-by: Mike Reed Reviewed-by: Leon Scroggins --- include/codec/SkAndroidCodec.h | 10 ++++++++++ src/android/SkBitmapRegionCodec.cpp | 11 +++-------- src/codec/SkAndroidCodec.cpp | 14 ++++++++++++++ src/codec/SkJpegCodec.cpp | 2 +- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/include/codec/SkAndroidCodec.h b/include/codec/SkAndroidCodec.h index a4f46f939f..ecf0a08c15 100644 --- a/include/codec/SkAndroidCodec.h +++ b/include/codec/SkAndroidCodec.h @@ -71,6 +71,16 @@ public: */ SkAlphaType computeOutputAlphaType(bool requestedUnpremul); + /** + * @param outputColorType Color type that the client will decode to + * + * Returns the appropriate color space to decode to. + * + * For now, this just returns a default. This could be updated to take + * requests for wide gamut modes or specific output spaces. + */ + sk_sp computeOutputColorSpace(SkColorType outputColorType); + /** * Returns the dimensions of the scaled output image, for an input * sampleSize. diff --git a/src/android/SkBitmapRegionCodec.cpp b/src/android/SkBitmapRegionCodec.cpp index 973e3c9470..ffe7ea81f8 100644 --- a/src/android/SkBitmapRegionCodec.cpp +++ b/src/android/SkBitmapRegionCodec.cpp @@ -52,12 +52,9 @@ bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocat // Create the image info for the decode SkColorType dstColorType = fCodec->computeOutputColorType(prefColorType); SkAlphaType dstAlphaType = fCodec->computeOutputAlphaType(requireUnpremul); - - // Enable legacy behavior to avoid any gamma correction. Android's assets are - // adjusted to expect a non-gamma correct premultiply. - sk_sp colorSpace = nullptr; + sk_sp dstColorSpace = fCodec->computeOutputColorSpace(dstColorType); SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.height(), - dstColorType, dstAlphaType, colorSpace); + dstColorType, dstAlphaType, dstColorSpace); // Construct a color table for the decode if necessary sk_sp colorTable(nullptr); @@ -135,8 +132,6 @@ bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocat } bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) { - // Enable legacy behavior. - sk_sp colorSpace = nullptr; - SkImageInfo dstInfo = fCodec->getInfo().makeColorType(colorType).makeColorSpace(colorSpace); + SkImageInfo dstInfo = fCodec->getInfo().makeColorType(colorType); return conversion_possible(dstInfo, fCodec->getInfo()); } diff --git a/src/codec/SkAndroidCodec.cpp b/src/codec/SkAndroidCodec.cpp index c315b032fb..5dddfe355e 100644 --- a/src/codec/SkAndroidCodec.cpp +++ b/src/codec/SkAndroidCodec.cpp @@ -114,6 +114,20 @@ SkAlphaType SkAndroidCodec::computeOutputAlphaType(bool requestedUnpremul) { return requestedUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType; } +sk_sp SkAndroidCodec::computeOutputColorSpace(SkColorType outputColorType) { + switch (outputColorType) { + case kRGBA_8888_SkColorType: + case kBGRA_8888_SkColorType: + case kIndex_8_SkColorType: + return SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); + case kRGBA_F16_SkColorType: + return SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named); + default: + // Color correction not supported for k565 and kGray. + return nullptr; + } +} + SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const { if (!is_valid_sample_size(sampleSize)) { return SkISize::Make(0, 0); diff --git a/src/codec/SkJpegCodec.cpp b/src/codec/SkJpegCodec.cpp index 7867fd0e8b..ad5ce5834d 100644 --- a/src/codec/SkJpegCodec.cpp +++ b/src/codec/SkJpegCodec.cpp @@ -503,7 +503,7 @@ int SkJpegCodec::readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes uint32_t* swizzleDst = (uint32_t*) dst; size_t decodeDstRowBytes = rowBytes; size_t swizzleDstRowBytes = rowBytes; - int dstWidth = dstInfo.width(); + int dstWidth = this->options().fSubset ? this->options().fSubset->width() : dstInfo.width(); if (fSwizzleSrcRow && fColorXformSrcRow) { decodeDst = (JSAMPLE*) fSwizzleSrcRow; swizzleDst = fColorXformSrcRow;