1/*
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#ifndef SkImageInfoPriv_DEFINED
9#define SkImageInfoPriv_DEFINED
10
11#include "include/core/SkColor.h"
12#include "include/core/SkImageInfo.h"
13
14static inline uint32_t SkColorTypeChannelFlags(SkColorType ct) {
15 switch (ct) {
16 case kUnknown_SkColorType: return 0;
17 case kAlpha_8_SkColorType: return kAlpha_SkColorChannelFlag;
18 case kRGB_565_SkColorType: return kRGB_SkColorChannelFlags;
19 case kARGB_4444_SkColorType: return kRGBA_SkColorChannelFlags;
20 case kRGBA_8888_SkColorType: return kRGBA_SkColorChannelFlags;
21 case kRGB_888x_SkColorType: return kRGB_SkColorChannelFlags;
22 case kBGRA_8888_SkColorType: return kRGBA_SkColorChannelFlags;
23 case kRGBA_1010102_SkColorType: return kRGBA_SkColorChannelFlags;
24 case kRGB_101010x_SkColorType: return kRGB_SkColorChannelFlags;
25 case kBGRA_1010102_SkColorType: return kRGBA_SkColorChannelFlags;
26 case kBGR_101010x_SkColorType: return kRGB_SkColorChannelFlags;
27 case kGray_8_SkColorType: return kGray_SkColorChannelFlag;
28 case kRGBA_F16Norm_SkColorType: return kRGBA_SkColorChannelFlags;
29 case kRGBA_F16_SkColorType: return kRGBA_SkColorChannelFlags;
30 case kRGBA_F32_SkColorType: return kRGBA_SkColorChannelFlags;
31 case kR8G8_unorm_SkColorType: return kRG_SkColorChannelFlags;
32 case kA16_unorm_SkColorType: return kAlpha_SkColorChannelFlag;
33 case kR16G16_unorm_SkColorType: return kRG_SkColorChannelFlags;
34 case kA16_float_SkColorType: return kAlpha_SkColorChannelFlag;
35 case kR16G16_float_SkColorType: return kRG_SkColorChannelFlags;
36 case kR16G16B16A16_unorm_SkColorType: return kRGBA_SkColorChannelFlags;
37 }
38 SkUNREACHABLE;
39}
40
41static inline bool SkColorTypeIsAlphaOnly(SkColorType ct) {
42 return SkColorTypeChannelFlags(ct) == kAlpha_SkColorChannelFlag;
43}
44
45static inline bool SkAlphaTypeIsValid(unsigned value) {
46 return value <= kLastEnum_SkAlphaType;
47}
48
49static int SkColorTypeShiftPerPixel(SkColorType ct) {
50 switch (ct) {
51 case kUnknown_SkColorType: return 0;
52 case kAlpha_8_SkColorType: return 0;
53 case kRGB_565_SkColorType: return 1;
54 case kARGB_4444_SkColorType: return 1;
55 case kRGBA_8888_SkColorType: return 2;
56 case kRGB_888x_SkColorType: return 2;
57 case kBGRA_8888_SkColorType: return 2;
58 case kRGBA_1010102_SkColorType: return 2;
59 case kRGB_101010x_SkColorType: return 2;
60 case kBGRA_1010102_SkColorType: return 2;
61 case kBGR_101010x_SkColorType: return 2;
62 case kGray_8_SkColorType: return 0;
63 case kRGBA_F16Norm_SkColorType: return 3;
64 case kRGBA_F16_SkColorType: return 3;
65 case kRGBA_F32_SkColorType: return 4;
66 case kR8G8_unorm_SkColorType: return 1;
67 case kA16_unorm_SkColorType: return 1;
68 case kR16G16_unorm_SkColorType: return 2;
69 case kA16_float_SkColorType: return 1;
70 case kR16G16_float_SkColorType: return 2;
71 case kR16G16B16A16_unorm_SkColorType: return 3;
72 }
73 SkUNREACHABLE;
74}
75
76static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) {
77 return width * SkColorTypeBytesPerPixel(ct);
78}
79
80static inline bool SkColorTypeIsValid(unsigned value) {
81 return value <= kLastEnum_SkColorType;
82}
83
84static inline size_t SkColorTypeComputeOffset(SkColorType ct, int x, int y, size_t rowBytes) {
85 if (kUnknown_SkColorType == ct) {
86 return 0;
87 }
88 return y * rowBytes + (x << SkColorTypeShiftPerPixel(ct));
89}
90
91static inline bool SkColorTypeIsNormalized(SkColorType ct) {
92 switch (ct) {
93 case kUnknown_SkColorType:
94 case kAlpha_8_SkColorType:
95 case kRGB_565_SkColorType:
96 case kARGB_4444_SkColorType:
97 case kRGBA_8888_SkColorType:
98 case kRGB_888x_SkColorType:
99 case kBGRA_8888_SkColorType:
100 case kRGBA_1010102_SkColorType:
101 case kRGB_101010x_SkColorType:
102 case kBGRA_1010102_SkColorType:
103 case kBGR_101010x_SkColorType:
104 case kGray_8_SkColorType:
105 case kRGBA_F16Norm_SkColorType:
106 case kR8G8_unorm_SkColorType:
107 case kA16_unorm_SkColorType:
108 case kA16_float_SkColorType: /*subtle... alpha is always [0,1]*/
109 case kR16G16_unorm_SkColorType:
110 case kR16G16B16A16_unorm_SkColorType: return true;
111
112 case kRGBA_F16_SkColorType:
113 case kRGBA_F32_SkColorType:
114 case kR16G16_float_SkColorType: return false;
115 }
116 SkUNREACHABLE;
117}
118
119/**
120 * Returns true if |info| contains a valid colorType and alphaType.
121 */
122static inline bool SkColorInfoIsValid(const SkColorInfo& info) {
123 return info.colorType() != kUnknown_SkColorType && info.alphaType() != kUnknown_SkAlphaType;
124}
125
126/**
127 * Returns true if |info| contains a valid combination of width, height and colorInfo.
128 */
129static inline bool SkImageInfoIsValid(const SkImageInfo& info) {
130 if (info.width() <= 0 || info.height() <= 0) {
131 return false;
132 }
133
134 const int kMaxDimension = SK_MaxS32 >> 2;
135 if (info.width() > kMaxDimension || info.height() > kMaxDimension) {
136 return false;
137 }
138
139 return SkColorInfoIsValid(info.colorInfo());
140}
141
142/**
143 * Returns true if Skia has defined a pixel conversion from the |src| to the |dst|.
144 * Returns false otherwise.
145 */
146static inline bool SkImageInfoValidConversion(const SkImageInfo& dst, const SkImageInfo& src) {
147 return SkImageInfoIsValid(dst) && SkImageInfoIsValid(src);
148}
149#endif // SkImageInfoPriv_DEFINED
150