if (kUnpremul_SkAlphaType == decodeInfo.alphaType()) {
decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType);
}
+ if (kRGBA_F16_SkColorType == fColorType) {
+ decodeInfo = decodeInfo.makeColorSpace(decodeInfo.colorSpace()->makeLinearGamma());
+ }
SkImageInfo bitmapInfo = decodeInfo;
if (kRGBA_8888_SkColorType == decodeInfo.colorType() ||
}
bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) {
- return conversion_possible(fCodec->getInfo().makeColorType(colorType), fCodec->getInfo());
+ // Enable legacy behavior.
+ sk_sp<SkColorSpace> colorSpace = nullptr;
+ SkImageInfo dstInfo = fCodec->getInfo().makeColorType(colorType).makeColorSpace(colorSpace);
+ return conversion_possible_ignore_color_space(dstInfo, fCodec->getInfo());
}
SkCodec::Result SkBmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
- if (!conversion_possible(dstInfo, this->getInfo())) {
+ if (!conversion_possible_ignore_color_space(dstInfo, this->getInfo())) {
SkCodecPrintf("Error: cannot convert input type to output type.\n");
return kInvalidConversion;
}
return kInvalidScale;
}
- if (!conversion_possible(dstInfo, this->getInfo())) {
+ if (!conversion_possible_ignore_color_space(dstInfo, this->getInfo())) {
SkCodecPrintf("Error: cannot convert input type to output type.\n");
return kInvalidConversion;
}
// Subsets are not supported.
return kUnimplemented;
}
- if (!conversion_possible(dstInfo, this->getInfo())) {
+ if (!conversion_possible_ignore_color_space(dstInfo, this->getInfo())) {
SkCodecPrintf("Error: cannot convert input type to output type.\n");
return kInvalidConversion;
}
SkCodecPrintf("Error: scaling not supported.\n");
return kInvalidScale;
}
- if (!conversion_possible(dstInfo, this->getInfo())) {
+ if (!conversion_possible_ignore_color_space(dstInfo, this->getInfo())) {
SkCodecPrintf("Error: cannot convert input type to output type.\n");
return kInvalidConversion;
}
}
/*
+ * Original version of conversion_possible that does not account for color spaces.
+ * Used by codecs that have not been updated to support color spaces.
+ *
* Most of our codecs support the same conversions:
* - opaque to any alpha type
* - 565 only if opaque
* - premul to unpremul and vice versa
- * - always support N32
+ * - always support RGBA, BGRA
* - otherwise match the src color type
*/
-static inline bool conversion_possible(const SkImageInfo& dst, const SkImageInfo& src) {
+static inline bool conversion_possible_ignore_color_space(const SkImageInfo& dst,
+ const SkImageInfo& src) {
// Ensure the alpha type is valid
if (!valid_alpha(dst.alphaType(), src.alphaType())) {
return false;
return (kOpaque_SkAlphaType == srcAlphaType) ? kOpaque_SkAlphaType : dstAlphaType;
}
+/*
+ * Alpha Type Conversions
+ * - kOpaque to kOpaque, kUnpremul, kPremul is valid
+ * - kUnpremul to kUnpremul, kPremul is valid
+ *
+ * Color Type Conversions
+ * - Always support kRGBA_8888, kBGRA_8888
+ * - Support kRGBA_F16 when there is a linear dst color space
+ * - Support kIndex8 if it matches the src
+ * - Support k565, kGray8 if kOpaque and color correction is not required
+ */
+static inline bool conversion_possible(const SkImageInfo& dst, const SkImageInfo& src) {
+ // Ensure the alpha type is valid.
+ if (!valid_alpha(dst.alphaType(), src.alphaType())) {
+ return false;
+ }
+
+ // Check for supported color types.
+ switch (dst.colorType()) {
+ case kRGBA_8888_SkColorType:
+ case kBGRA_8888_SkColorType:
+ return true;
+ case kRGBA_F16_SkColorType:
+ return dst.colorSpace() && dst.colorSpace()->gammaIsLinear();
+ case kIndex_8_SkColorType:
+ return kIndex_8_SkColorType == src.colorType();
+ case kRGB_565_SkColorType:
+ case kGray_8_SkColorType:
+ return kOpaque_SkAlphaType == src.alphaType() && !needs_color_xform(dst, src);
+ default:
+ return false;
+ }
+}
+
#endif // SkCodecPriv_DEFINED
SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr,
int* inputColorCount, const Options& opts) {
// Check for valid input parameters
- if (!conversion_possible(dstInfo, this->getInfo())) {
- return gif_error("Cannot convert input type to output type.\n",
- kInvalidConversion);
+ if (!conversion_possible_ignore_color_space(dstInfo, this->getInfo())) {
+ return gif_error("Cannot convert input type to output type.\n", kInvalidConversion);
}
// Initialize color table and copy to the client if necessary
* image has been implemented
* Sets the output color space
*/
-bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dstInfo, bool needsColorXform) {
+bool SkJpegCodec::setOutputColorSpace(const SkImageInfo& dstInfo) {
if (kUnknown_SkAlphaType == dstInfo.alphaType()) {
return false;
}
case kBGRA_8888_SkColorType:
if (isCMYK) {
fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
- } else if (needsColorXform) {
+ } else if (fColorXform) {
// Our color transformation code requires RGBA order inputs, but it'll swizzle
// to BGRA for us.
fDecoderMgr->dinfo()->out_color_space = JCS_EXT_RGBA;
}
return true;
case kRGB_565_SkColorType:
- if (needsColorXform) {
+ if (fColorXform) {
return false;
}
}
return true;
case kGray_8_SkColorType:
- if (needsColorXform || JCS_GRAYSCALE != encodedColorType) {
+ if (fColorXform || JCS_GRAYSCALE != encodedColorType) {
return false;
}
fDecoderMgr->dinfo()->out_color_space = JCS_GRAYSCALE;
return true;
case kRGBA_F16_SkColorType:
- SkASSERT(needsColorXform);
+ SkASSERT(fColorXform);
+ if (!dstInfo.colorSpace()->gammaIsLinear()) {
+ return false;
+ }
if (isCMYK) {
fDecoderMgr->dinfo()->out_color_space = JCS_CMYK;
return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
}
+ this->initializeColorXform(dstInfo);
+
// Check if we can decode to the requested destination and set the output color space
- bool needsColorXform = needs_color_xform(dstInfo, this->getInfo());
- if (!this->setOutputColorSpace(dstInfo, needsColorXform)) {
+ if (!this->setOutputColorSpace(dstInfo)) {
return fDecoderMgr->returnFailure("setOutputColorSpace", kInvalidConversion);
}
- if (!this->initializeColorXform(dstInfo, needsColorXform)) {
- return fDecoderMgr->returnFailure("initializeColorXform", kInvalidParameters);
- }
-
if (!jpeg_start_decompress(dinfo)) {
return fDecoderMgr->returnFailure("startDecompress", kInvalidInput);
}
SkASSERT(fSwizzler);
}
-bool SkJpegCodec::initializeColorXform(const SkImageInfo& dstInfo, bool needsColorXform) {
- if (needsColorXform) {
+void SkJpegCodec::initializeColorXform(const SkImageInfo& dstInfo) {
+ if (needs_color_xform(dstInfo, this->getInfo())) {
fColorXform = SkColorSpaceXform::New(sk_ref_sp(this->getInfo().colorSpace()),
sk_ref_sp(dstInfo.colorSpace()));
- if (!fColorXform && kRGBA_F16_SkColorType == dstInfo.colorType()) {
- return false;
- }
+ SkASSERT(fColorXform);
}
-
- return true;
}
SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) {
return kInvalidInput;
}
- // Check if we can decode to the requested destination and set the output color space
- bool needsColorXform = needs_color_xform(dstInfo, this->getInfo());
- if (!this->setOutputColorSpace(dstInfo, needsColorXform)) {
- return kInvalidConversion;
- }
+ this->initializeColorXform(dstInfo);
- if (!this->initializeColorXform(dstInfo, needsColorXform)) {
- return fDecoderMgr->returnFailure("initializeColorXform", kInvalidParameters);
+ // Check if we can decode to the requested destination and set the output color space
+ if (!this->setOutputColorSpace(dstInfo)) {
+ return fDecoderMgr->returnFailure("setOutputColorSpace", kInvalidConversion);
}
if (!jpeg_start_decompress(fDecoderMgr->dinfo())) {
*
* Sets the output color space.
*/
- bool setOutputColorSpace(const SkImageInfo& dst, bool needsColorXform);
+ bool setOutputColorSpace(const SkImageInfo& dst);
void initializeSwizzler(const SkImageInfo& dstInfo, const Options& options);
- bool initializeColorXform(const SkImageInfo& dstInfo, bool needsColorXform);
+ void initializeColorXform(const SkImageInfo& dstInfo);
void allocateStorage(const SkImageInfo& dstInfo);
int readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count);
return bitsPerPixel / 8;
}
-static bool png_conversion_possible(const SkImageInfo& dst, const SkImageInfo& src) {
- // Ensure the alpha type is valid
- if (!valid_alpha(dst.alphaType(), src.alphaType())) {
- return false;
- }
-
- // Check for supported color types
- switch (dst.colorType()) {
- case kRGBA_8888_SkColorType:
- case kBGRA_8888_SkColorType:
- case kRGBA_F16_SkColorType:
- return true;
- case kRGB_565_SkColorType:
- return kOpaque_SkAlphaType == src.alphaType();
- default:
- return dst.colorType() == src.colorType();
- }
-}
-
void SkPngCodec::allocateStorage(const SkImageInfo& dstInfo) {
switch (fXformMode) {
case kSwizzleOnly_XformMode:
Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options,
SkPMColor ctable[], int* ctableCount) override {
- if (!png_conversion_possible(dstInfo, this->getInfo()) ||
+ if (!conversion_possible(dstInfo, this->getInfo()) ||
!this->initializeXforms(dstInfo, options, ctable, ctableCount))
{
return kInvalidConversion;
Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options,
SkPMColor ctable[], int* ctableCount) override {
- if (!png_conversion_possible(dstInfo, this->getInfo()) ||
+ if (!conversion_possible(dstInfo, this->getInfo()) ||
!this->initializeXforms(dstInfo, options, ctable, ctableCount))
{
return kInvalidConversion;
fSwizzler.reset(nullptr);
fColorXform = nullptr;
- bool needsColorXform = needs_color_xform(dstInfo, this->getInfo());
- if (needsColorXform) {
- if (kGray_8_SkColorType == dstInfo.colorType() ||
- kRGB_565_SkColorType == dstInfo.colorType())
- {
- return false;
- }
-
+ if (needs_color_xform(dstInfo, this->getInfo())) {
fColorXform = SkColorSpaceXform::New(sk_ref_sp(this->getInfo().colorSpace()),
sk_ref_sp(dstInfo.colorSpace()));
-
- if (!fColorXform && kRGBA_F16_SkColorType == dstInfo.colorType()) {
- return false;
- }
+ SkASSERT(fColorXform);
}
// If the image is RGBA and we have a color xform, we can skip the swizzler.
size_t rowBytes, const Options& options,
SkPMColor ctable[], int* ctableCount,
int* rowsDecoded) {
- if (!png_conversion_possible(dstInfo, this->getInfo()) ||
+ if (!conversion_possible(dstInfo, this->getInfo()) ||
!this->initializeXforms(dstInfo, options, ctable, ctableCount))
{
return kInvalidConversion;
size_t dstRowBytes, const Options& options,
SkPMColor ctable[], int* ctableCount,
int* rowsDecoded) {
- if (!conversion_possible(requestedInfo, this->getInfo())) {
+ if (!conversion_possible_ignore_color_space(requestedInfo, this->getInfo())) {
SkCodecPrintf("Error: cannot convert input type to output type.\n");
return kInvalidConversion;
}
streamDeleter.release(), demux.release(), std::move(data));
}
-static bool webp_conversion_possible(const SkImageInfo& dst, const SkImageInfo& src,
- SkColorSpaceXform* colorXform) {
- if (!valid_alpha(dst.alphaType(), src.alphaType())) {
- return false;
- }
-
- switch (dst.colorType()) {
- case kRGBA_F16_SkColorType:
- return nullptr != colorXform;
- case kBGRA_8888_SkColorType:
- case kRGBA_8888_SkColorType:
- return true;
- case kRGB_565_SkColorType:
- return nullptr == colorXform && src.alphaType() == kOpaque_SkAlphaType;
- default:
- return false;
- }
-}
-
SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const {
SkISize dim = this->getInfo().dimensions();
// SkCodec treats zero dimensional images as errors, so the minimum size
SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes,
const Options& options, SkPMColor*, int*,
int* rowsDecodedPtr) {
+ if (!conversion_possible(dstInfo, this->getInfo())) {
+ return kInvalidConversion;
+ }
std::unique_ptr<SkColorSpaceXform> colorXform = nullptr;
if (needs_color_xform(dstInfo, this->getInfo())) {
colorXform = SkColorSpaceXform::New(sk_ref_sp(this->getInfo().colorSpace()),
sk_ref_sp(dstInfo.colorSpace()));
- }
-
- if (!webp_conversion_possible(dstInfo, this->getInfo(), colorXform.get())) {
- return kInvalidConversion;
+ SkASSERT(colorXform);
}
WebPDecoderConfig config;
REPORTER_ASSERT(r, SkCodec::kSuccess == result);
check_round_trip(r, bm2);
}
+
+static void test_conversion_possible(skiatest::Reporter* r, const char* path,
+ bool testScanlineDecoder) {
+ SkAutoTDelete<SkStream> stream(resource(path));
+ SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
+ SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType);
+
+ SkBitmap bm;
+ bm.allocPixels(infoF16);
+ SkCodec::Result result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
+ REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
+ if (testScanlineDecoder) {
+ result = codec->startScanlineDecode(infoF16);
+ REPORTER_ASSERT(r, SkCodec::kInvalidConversion == result);
+ }
+
+ infoF16 = infoF16.makeColorSpace(infoF16.colorSpace()->makeLinearGamma());
+ result = codec->getPixels(infoF16, bm.getPixels(), bm.rowBytes());
+ REPORTER_ASSERT(r, SkCodec::kSuccess == result);
+ if (testScanlineDecoder) {
+ result = codec->startScanlineDecode(infoF16);
+ REPORTER_ASSERT(r, SkCodec::kSuccess == result);
+ }
+}
+
+DEF_TEST(Codec_F16ConversionPossible, r) {
+ test_conversion_possible(r, "color_wheel.webp", false);
+ test_conversion_possible(r, "mandrill_512_q075.jpg", true);
+ test_conversion_possible(r, "yellow_rose.png", true);
+}