blob: 716c35d43da5aadc63e6269bbc2cebb15ace2464 [file] [log] [blame]
Matt Sarettcb6266b2017-01-17 10:48:53 -05001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Hal Canary6b20a552017-02-07 14:09:38 -05008#ifndef SkImageInfoPriv_DEFINED
9#define SkImageInfoPriv_DEFINED
10
11#include "SkImageInfo.h"
12
Matt Sarettcb6266b2017-01-17 10:48:53 -050013/**
Matt Sarett26b44df2017-05-02 16:04:56 -040014 * This contains shared checks on SkImageInfo. Depending on the desired color space behavior,
15 * the caller should choose one of the two versions below.
Matt Sarettcb6266b2017-01-17 10:48:53 -050016 */
Matt Sarett26b44df2017-05-02 16:04:56 -040017static inline bool SkImageInfoIsValidCommon(const SkImageInfo& info) {
Matt Sarettcb6266b2017-01-17 10:48:53 -050018 if (info.width() <= 0 || info.height() <= 0) {
19 return false;
20 }
21
Matt Sarett8572d852017-02-14 11:21:02 -050022 const int kMaxDimension = SK_MaxS32 >> 2;
23 if (info.width() > kMaxDimension || info.height() > kMaxDimension) {
24 return false;
25 }
26
Matt Sarettcb6266b2017-01-17 10:48:53 -050027 if (kUnknown_SkColorType == info.colorType() || kUnknown_SkAlphaType == info.alphaType()) {
28 return false;
29 }
30
31 if (kOpaque_SkAlphaType != info.alphaType() &&
32 (kRGB_565_SkColorType == info.colorType() || kGray_8_SkColorType == info.colorType())) {
33 return false;
34 }
35
36 if (kRGBA_F16_SkColorType == info.colorType() &&
Stan Iliev4ed9ae42017-07-25 11:59:12 -040037 (info.colorSpace() && (!info.colorSpace()->gammaIsLinear()))) {
Matt Sarettcb6266b2017-01-17 10:48:53 -050038 return false;
39 }
40
Matt Sarett26b44df2017-05-02 16:04:56 -040041 return true;
42}
43
44/**
45 * Returns true if |info| contains a valid combination of width, height, colorType, alphaType,
46 * colorSpace. Allows numerical color spaces. Returns false otherwise.
47 */
48static inline bool SkImageInfoIsValidAllowNumericalCS(const SkImageInfo& info) {
49 if (!SkImageInfoIsValidCommon(info)) {
50 return false;
51 }
52
53 SkColorSpaceTransferFn fn;
54 if (info.colorSpace() && !info.colorSpace()->isNumericalTransferFn(&fn)) {
55 return false;
56 }
57
58 return true;
59}
60
61/**
62 * Returns true if |info| contains a valid combination of width, height, colorType, alphaType,
63 * colorSpace. Only supports rendering color spaces. Returns false otherwise.
64 */
65static inline bool SkImageInfoIsValidRenderingCS(const SkImageInfo& info) {
66 if (!SkImageInfoIsValidCommon(info)) {
67 return false;
68 }
69
Matt Sarettcb6266b2017-01-17 10:48:53 -050070 if (info.colorSpace() &&
71 (!info.colorSpace()->gammaCloseToSRGB() && !info.colorSpace()->gammaIsLinear())) {
72 return false;
73 }
74
75 return true;
76}
77
78/**
Matt Sarettdedac852017-05-12 10:56:49 -040079 * Returns true if |info| contains a valid combination of width, height, colorType, alphaType,
80 * colorSpace. Uses |colorMode| to decide how to treat color spaces.
81 */
82static inline bool SkImageInfoIsValid(const SkImageInfo& info,
83 SkDestinationSurfaceColorMode colorMode) {
84 if (SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware == colorMode) {
85 return SkImageInfoIsValidRenderingCS(info);
86 }
87
88 return SkImageInfoIsValidAllowNumericalCS(info);
89}
90
91/**
Matt Sarettcb6266b2017-01-17 10:48:53 -050092 * Returns true if Skia has defined a pixel conversion from the |src| to the |dst|.
93 * Returns false otherwise. Some discussion of false cases:
Matt Sarett8572d852017-02-14 11:21:02 -050094 * We will not convert to kIndex8 unless it exactly matches the src, since color tables
95 * are immutable.
Matt Sarett51014482017-02-09 13:50:45 -050096 * We do not convert to kGray8 when the |src| is not kGray8 in the same color space.
97 * We may add this feature - it just requires some work to convert to luminance while
98 * handling color spaces correctly. Currently no one is asking for this.
Matt Sarettcb6266b2017-01-17 10:48:53 -050099 * We will not convert from kAlpha8 when the |dst| is not kAlpha8. This would require
100 * inventing color information.
101 * We will not convert to kOpaque when the |src| is not kOpaque. This could be
102 * implemented to set all the alpha values to 1, but there is still some ambiguity -
103 * should we use kPremul or kUnpremul color values with the opaque alphas? Or should
104 * we just use whatever the |src| alpha is? In the future, we could choose to clearly
105 * define this, but currently no one is asking for this feature.
Matt Sarettf5759932017-02-07 21:52:07 +0000106 * We will not convert to a particular color space if |src| is nullptr. The color space
107 * conversion is not well-defined.
Matt Sarettcb6266b2017-01-17 10:48:53 -0500108 */
109static inline bool SkImageInfoValidConversion(const SkImageInfo& dst, const SkImageInfo& src) {
Matt Sarett733340a2017-05-02 16:19:51 -0400110 if (!SkImageInfoIsValidAllowNumericalCS(dst) || !SkImageInfoIsValidAllowNumericalCS(src)) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500111 return false;
112 }
113
Matt Sarett8572d852017-02-14 11:21:02 -0500114 if (kGray_8_SkColorType == dst.colorType()) {
115 if (kGray_8_SkColorType != src.colorType()) {
116 return false;
117 }
118
119 if (dst.colorSpace() && !SkColorSpace::Equals(dst.colorSpace(), src.colorSpace())) {
120 return false;
121 }
Matt Sarettcb6266b2017-01-17 10:48:53 -0500122 }
123
124 if (kAlpha_8_SkColorType != dst.colorType() && kAlpha_8_SkColorType == src.colorType()) {
125 return false;
126 }
127
Matt Saretta8f41392017-02-06 18:28:49 +0000128 if (kOpaque_SkAlphaType == dst.alphaType() && kOpaque_SkAlphaType != src.alphaType()) {
129 return false;
130 }
Matt Sarettcb6266b2017-01-17 10:48:53 -0500131
Matt Sarettf5759932017-02-07 21:52:07 +0000132 if (dst.colorSpace() && !src.colorSpace()) {
133 return false;
134 }
135
Matt Sarettcb6266b2017-01-17 10:48:53 -0500136 return true;
137}
Hal Canary6b20a552017-02-07 14:09:38 -0500138#endif // SkImageInfoPriv_DEFINED