blob: 72138aebe44873b57b0dfe8278cae4a9669eb5e7 [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
8/**
9 * Returns true if |info| contains a valid combination of width, height, colorType, alphaType,
10 * colorSpace. Returns false otherwise.
11 */
12static inline bool SkImageInfoIsValid(const SkImageInfo& info) {
13 if (info.width() <= 0 || info.height() <= 0) {
14 return false;
15 }
16
17 if (kUnknown_SkColorType == info.colorType() || kUnknown_SkAlphaType == info.alphaType()) {
18 return false;
19 }
20
21 if (kOpaque_SkAlphaType != info.alphaType() &&
22 (kRGB_565_SkColorType == info.colorType() || kGray_8_SkColorType == info.colorType())) {
23 return false;
24 }
25
26 if (kRGBA_F16_SkColorType == info.colorType() &&
27 (!info.colorSpace() || !info.colorSpace()->gammaIsLinear())) {
28 return false;
29 }
30
31 if (info.colorSpace() &&
32 (!info.colorSpace()->gammaCloseToSRGB() && !info.colorSpace()->gammaIsLinear())) {
33 return false;
34 }
35
36 return true;
37}
38
39/**
40 * Returns true if Skia has defined a pixel conversion from the |src| to the |dst|.
41 * Returns false otherwise. Some discussion of false cases:
42 * We will not convert to kIndex8 unless |src| is kIndex8. This is possible only
43 * in some cases and likley inefficient.
44 * We do not convert to kGray8 when the |src| is not kGray8. We may add this
45 * feature - it just requires some work to convert to luminance while handling color
46 * spaces correctly. Currently no one is asking for this.
47 * We will not convert from kAlpha8 when the |dst| is not kAlpha8. This would require
48 * inventing color information.
49 * We will not convert to kOpaque when the |src| is not kOpaque. This could be
50 * implemented to set all the alpha values to 1, but there is still some ambiguity -
51 * should we use kPremul or kUnpremul color values with the opaque alphas? Or should
52 * we just use whatever the |src| alpha is? In the future, we could choose to clearly
53 * define this, but currently no one is asking for this feature.
54 */
55static inline bool SkImageInfoValidConversion(const SkImageInfo& dst, const SkImageInfo& src) {
56 if (!SkImageInfoIsValid(dst) || !SkImageInfoIsValid(src)) {
57 return false;
58 }
59
60 if (kIndex_8_SkColorType == dst.colorType() && kIndex_8_SkColorType != src.colorType()) {
61 return false;
62 }
63
64 if (kGray_8_SkColorType == dst.colorType() && kGray_8_SkColorType != src.colorType()) {
65 return false;
66 }
67
68 if (kAlpha_8_SkColorType != dst.colorType() && kAlpha_8_SkColorType == src.colorType()) {
69 return false;
70 }
71
Matt Sarett94bd50c2017-01-17 13:57:45 -050072 if (kOpaque_SkAlphaType == dst.alphaType() && kOpaque_SkAlphaType != src.alphaType()) {
73 return false;
74 }
Matt Sarettcb6266b2017-01-17 10:48:53 -050075
76 return true;
77}