skia2/src/core/SkImageInfoPriv.h

142 lines
4.7 KiB
C
Raw Normal View History

/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkImageInfoPriv_DEFINED
#define SkImageInfoPriv_DEFINED
#include "SkImageInfo.h"
/**
* This contains shared checks on SkImageInfo. Depending on the desired color space behavior,
* the caller should choose one of the two versions below.
*/
static inline bool SkImageInfoIsValidCommon(const SkImageInfo& info) {
if (info.width() <= 0 || info.height() <= 0) {
return false;
}
const int kMaxDimension = SK_MaxS32 >> 2;
if (info.width() > kMaxDimension || info.height() > kMaxDimension) {
return false;
}
if (kUnknown_SkColorType == info.colorType() || kUnknown_SkAlphaType == info.alphaType()) {
return false;
}
if (kOpaque_SkAlphaType != info.alphaType() &&
(kRGB_565_SkColorType == info.colorType() || kGray_8_SkColorType == info.colorType())) {
return false;
}
if (kRGBA_F16_SkColorType == info.colorType() &&
(!info.colorSpace() || !info.colorSpace()->gammaIsLinear())) {
return false;
}
return true;
}
/**
* Returns true if |info| contains a valid combination of width, height, colorType, alphaType,
* colorSpace. Allows numerical color spaces. Returns false otherwise.
*/
static inline bool SkImageInfoIsValidAllowNumericalCS(const SkImageInfo& info) {
if (!SkImageInfoIsValidCommon(info)) {
return false;
}
SkColorSpaceTransferFn fn;
if (info.colorSpace() && !info.colorSpace()->isNumericalTransferFn(&fn)) {
return false;
}
return true;
}
/**
* Returns true if |info| contains a valid combination of width, height, colorType, alphaType,
* colorSpace. Only supports rendering color spaces. Returns false otherwise.
*/
static inline bool SkImageInfoIsValidRenderingCS(const SkImageInfo& info) {
if (!SkImageInfoIsValidCommon(info)) {
return false;
}
if (info.colorSpace() &&
(!info.colorSpace()->gammaCloseToSRGB() && !info.colorSpace()->gammaIsLinear())) {
return false;
}
return true;
}
/**
* Returns true if Skia has defined a pixel conversion from the |src| to the |dst|.
* Returns false otherwise. Some discussion of false cases:
* We will not convert to kIndex8 unless it exactly matches the src, since color tables
* are immutable.
* We do not convert to kGray8 when the |src| is not kGray8 in the same color space.
* We may add this feature - it just requires some work to convert to luminance while
* handling color spaces correctly. Currently no one is asking for this.
* We will not convert from kAlpha8 when the |dst| is not kAlpha8. This would require
* inventing color information.
* We will not convert to kOpaque when the |src| is not kOpaque. This could be
* implemented to set all the alpha values to 1, but there is still some ambiguity -
* should we use kPremul or kUnpremul color values with the opaque alphas? Or should
* we just use whatever the |src| alpha is? In the future, we could choose to clearly
* define this, but currently no one is asking for this feature.
Revert "Revert "Disallow readPixels() conversions from untagged srcs to tagged dsts"" This reverts commit 8461506c8b025522eb89cd754c63fb5dbf0cb7f1. Reason for revert: Fixed some Chrome code, let's try this again. Original change's description: > Revert "Disallow readPixels() conversions from untagged srcs to tagged dsts" > > This reverts commit ccfd1083a7b9d546735ddd1037f3240635cce72d. > > Reason for revert: Roll is failing. > > Original change's description: > > Disallow readPixels() conversions from untagged srcs to tagged dsts > > > > This might break the roll into Chrome. But let's see how. > > > > BUG=skia:6021 > > > > Change-Id: I2698b5d6fe72d01bed0dc64703b592a03d441a80 > > Reviewed-on: https://skia-review.googlesource.com/7127 > > Reviewed-by: Mike Reed <reed@google.com> > > Commit-Queue: Matt Sarett <msarett@google.com> > > > > TBR=msarett@google.com,brianosman@google.com,reed@google.com > NOPRESUBMIT=true > NOTREECHECKS=true > NOTRY=true > BUG=skia:6021 > > Change-Id: I4b62178fd7c23f43cf69ca69fc14526ecd503956 > Reviewed-on: https://skia-review.googlesource.com/7205 > Commit-Queue: Matt Sarett <msarett@google.com> > Reviewed-by: Matt Sarett <msarett@google.com> > TBR=msarett@google.com,brianosman@google.com,reed@google.com # Not skipping CQ checks because original CL landed > 1 day ago. BUG=skia:6021 Change-Id: I3f3f33fe6b84fbd5c537b60ed5c8b9201d529a6a Reviewed-on: https://skia-review.googlesource.com/8156 Commit-Queue: Matt Sarett <msarett@google.com> Reviewed-by: Matt Sarett <msarett@google.com>
2017-02-07 21:52:07 +00:00
* We will not convert to a particular color space if |src| is nullptr. The color space
* conversion is not well-defined.
*/
static inline bool SkImageInfoValidConversion(const SkImageInfo& dst, const SkImageInfo& src) {
if (!SkImageInfoIsValidRenderingCS(dst) || !SkImageInfoIsValidRenderingCS(src)) {
return false;
}
if (kIndex_8_SkColorType == dst.colorType()) {
if (kIndex_8_SkColorType != src.colorType()) {
return false;
}
if ((kPremul_SkAlphaType == dst.alphaType() && kUnpremul_SkAlphaType == src.alphaType()) ||
(kUnpremul_SkAlphaType == dst.alphaType() && kPremul_SkAlphaType == src.alphaType()))
{
return false;
}
if (dst.colorSpace() && !SkColorSpace::Equals(dst.colorSpace(), src.colorSpace())) {
return false;
}
}
if (kGray_8_SkColorType == dst.colorType()) {
if (kGray_8_SkColorType != src.colorType()) {
return false;
}
if (dst.colorSpace() && !SkColorSpace::Equals(dst.colorSpace(), src.colorSpace())) {
return false;
}
}
if (kAlpha_8_SkColorType != dst.colorType() && kAlpha_8_SkColorType == src.colorType()) {
return false;
}
if (kOpaque_SkAlphaType == dst.alphaType() && kOpaque_SkAlphaType != src.alphaType()) {
return false;
}
Revert "Revert "Disallow readPixels() conversions from untagged srcs to tagged dsts"" This reverts commit 8461506c8b025522eb89cd754c63fb5dbf0cb7f1. Reason for revert: Fixed some Chrome code, let's try this again. Original change's description: > Revert "Disallow readPixels() conversions from untagged srcs to tagged dsts" > > This reverts commit ccfd1083a7b9d546735ddd1037f3240635cce72d. > > Reason for revert: Roll is failing. > > Original change's description: > > Disallow readPixels() conversions from untagged srcs to tagged dsts > > > > This might break the roll into Chrome. But let's see how. > > > > BUG=skia:6021 > > > > Change-Id: I2698b5d6fe72d01bed0dc64703b592a03d441a80 > > Reviewed-on: https://skia-review.googlesource.com/7127 > > Reviewed-by: Mike Reed <reed@google.com> > > Commit-Queue: Matt Sarett <msarett@google.com> > > > > TBR=msarett@google.com,brianosman@google.com,reed@google.com > NOPRESUBMIT=true > NOTREECHECKS=true > NOTRY=true > BUG=skia:6021 > > Change-Id: I4b62178fd7c23f43cf69ca69fc14526ecd503956 > Reviewed-on: https://skia-review.googlesource.com/7205 > Commit-Queue: Matt Sarett <msarett@google.com> > Reviewed-by: Matt Sarett <msarett@google.com> > TBR=msarett@google.com,brianosman@google.com,reed@google.com # Not skipping CQ checks because original CL landed > 1 day ago. BUG=skia:6021 Change-Id: I3f3f33fe6b84fbd5c537b60ed5c8b9201d529a6a Reviewed-on: https://skia-review.googlesource.com/8156 Commit-Queue: Matt Sarett <msarett@google.com> Reviewed-by: Matt Sarett <msarett@google.com>
2017-02-07 21:52:07 +00:00
if (dst.colorSpace() && !src.colorSpace()) {
return false;
}
return true;
}
#endif // SkImageInfoPriv_DEFINED