• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SkColorTypeChannelFlags(SkColorType ct)14 static 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 kBGR_101010x_XR_SkColorType:     return kRGB_SkColorChannelFlags;
28         case kGray_8_SkColorType:             return kGray_SkColorChannelFlag;
29         case kRGBA_F16Norm_SkColorType:       return kRGBA_SkColorChannelFlags;
30         case kRGBA_F16_SkColorType:           return kRGBA_SkColorChannelFlags;
31         case kRGBA_F32_SkColorType:           return kRGBA_SkColorChannelFlags;
32         case kR8G8_unorm_SkColorType:         return kRG_SkColorChannelFlags;
33         case kA16_unorm_SkColorType:          return kAlpha_SkColorChannelFlag;
34         case kR16G16_unorm_SkColorType:       return kRG_SkColorChannelFlags;
35         case kA16_float_SkColorType:          return kAlpha_SkColorChannelFlag;
36         case kR16G16_float_SkColorType:       return kRG_SkColorChannelFlags;
37         case kR16G16B16A16_unorm_SkColorType: return kRGBA_SkColorChannelFlags;
38         case kSRGBA_8888_SkColorType:         return kRGBA_SkColorChannelFlags;
39         case kR8_unorm_SkColorType:           return kRed_SkColorChannelFlag;
40     }
41     SkUNREACHABLE;
42 }
43 
SkColorTypeIsAlphaOnly(SkColorType ct)44 static inline bool SkColorTypeIsAlphaOnly(SkColorType ct) {
45     return SkColorTypeChannelFlags(ct) == kAlpha_SkColorChannelFlag;
46 }
47 
SkAlphaTypeIsValid(unsigned value)48 static inline bool SkAlphaTypeIsValid(unsigned value) {
49     return value <= kLastEnum_SkAlphaType;
50 }
51 
SkColorTypeShiftPerPixel(SkColorType ct)52 static int SkColorTypeShiftPerPixel(SkColorType ct) {
53     switch (ct) {
54         case kUnknown_SkColorType:            return 0;
55         case kAlpha_8_SkColorType:            return 0;
56         case kRGB_565_SkColorType:            return 1;
57         case kARGB_4444_SkColorType:          return 1;
58         case kRGBA_8888_SkColorType:          return 2;
59         case kRGB_888x_SkColorType:           return 2;
60         case kBGRA_8888_SkColorType:          return 2;
61         case kRGBA_1010102_SkColorType:       return 2;
62         case kRGB_101010x_SkColorType:        return 2;
63         case kBGRA_1010102_SkColorType:       return 2;
64         case kBGR_101010x_SkColorType:        return 2;
65         case kBGR_101010x_XR_SkColorType:     return 2;
66         case kGray_8_SkColorType:             return 0;
67         case kRGBA_F16Norm_SkColorType:       return 3;
68         case kRGBA_F16_SkColorType:           return 3;
69         case kRGBA_F32_SkColorType:           return 4;
70         case kR8G8_unorm_SkColorType:         return 1;
71         case kA16_unorm_SkColorType:          return 1;
72         case kR16G16_unorm_SkColorType:       return 2;
73         case kA16_float_SkColorType:          return 1;
74         case kR16G16_float_SkColorType:       return 2;
75         case kR16G16B16A16_unorm_SkColorType: return 3;
76         case kSRGBA_8888_SkColorType:         return 2;
77         case kR8_unorm_SkColorType:           return 0;
78     }
79     SkUNREACHABLE;
80 }
81 
SkColorTypeMinRowBytes(SkColorType ct,int width)82 static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) {
83     return (size_t)(width * SkColorTypeBytesPerPixel(ct));
84 }
85 
SkColorTypeIsValid(unsigned value)86 static inline bool SkColorTypeIsValid(unsigned value) {
87     return value <= kLastEnum_SkColorType;
88 }
89 
SkColorTypeComputeOffset(SkColorType ct,int x,int y,size_t rowBytes)90 static inline size_t SkColorTypeComputeOffset(SkColorType ct, int x, int y, size_t rowBytes) {
91     if (kUnknown_SkColorType == ct) {
92         return 0;
93     }
94     return (size_t)y * rowBytes + ((size_t)x << SkColorTypeShiftPerPixel(ct));
95 }
96 
SkColorTypeIsNormalized(SkColorType ct)97 static inline bool SkColorTypeIsNormalized(SkColorType ct) {
98     switch (ct) {
99         case kUnknown_SkColorType:
100         case kAlpha_8_SkColorType:
101         case kRGB_565_SkColorType:
102         case kARGB_4444_SkColorType:
103         case kRGBA_8888_SkColorType:
104         case kRGB_888x_SkColorType:
105         case kBGRA_8888_SkColorType:
106         case kRGBA_1010102_SkColorType:
107         case kRGB_101010x_SkColorType:
108         case kBGRA_1010102_SkColorType:
109         case kBGR_101010x_SkColorType:
110         case kGray_8_SkColorType:
111         case kRGBA_F16Norm_SkColorType:
112         case kR8G8_unorm_SkColorType:
113         case kA16_unorm_SkColorType:
114         case kA16_float_SkColorType:          /*subtle... alpha is always [0,1]*/
115         case kR16G16_unorm_SkColorType:
116         case kR16G16B16A16_unorm_SkColorType:
117         case kSRGBA_8888_SkColorType:
118         case kR8_unorm_SkColorType:
119             return true;
120 
121         case kBGR_101010x_XR_SkColorType:
122         case kRGBA_F16_SkColorType:
123         case kRGBA_F32_SkColorType:
124         case kR16G16_float_SkColorType:
125             return false;
126     }
127     SkUNREACHABLE;
128 }
129 
SkColorTypeMaxBitsPerChannel(SkColorType ct)130 static inline int SkColorTypeMaxBitsPerChannel(SkColorType ct) {
131     switch (ct) {
132         case kUnknown_SkColorType:
133             return 0;
134 
135         case kARGB_4444_SkColorType:
136             return 4;
137 
138         case kRGB_565_SkColorType:
139             return 6;
140 
141         case kAlpha_8_SkColorType:
142         case kRGBA_8888_SkColorType:
143         case kRGB_888x_SkColorType:
144         case kBGRA_8888_SkColorType:
145         case kGray_8_SkColorType:
146         case kR8G8_unorm_SkColorType:
147         case kSRGBA_8888_SkColorType:
148         case kR8_unorm_SkColorType:
149             return 8;
150 
151         case kRGBA_1010102_SkColorType:
152         case kRGB_101010x_SkColorType:
153         case kBGRA_1010102_SkColorType:
154         case kBGR_101010x_SkColorType:
155         case kBGR_101010x_XR_SkColorType:
156             return 10;
157 
158         case kRGBA_F16Norm_SkColorType:
159         case kA16_unorm_SkColorType:
160         case kA16_float_SkColorType:
161         case kR16G16_unorm_SkColorType:
162         case kR16G16B16A16_unorm_SkColorType:
163         case kRGBA_F16_SkColorType:
164         case kR16G16_float_SkColorType:
165             return 16;
166 
167         case kRGBA_F32_SkColorType:
168             return 32;
169     }
170     SkUNREACHABLE;
171 }
172 
173 /**
174  *  Returns true if |info| contains a valid colorType and alphaType.
175  */
SkColorInfoIsValid(const SkColorInfo & info)176 static inline bool SkColorInfoIsValid(const SkColorInfo& info) {
177     return info.colorType() != kUnknown_SkColorType && info.alphaType() != kUnknown_SkAlphaType;
178 }
179 
180 /**
181  *  Returns true if |info| contains a valid combination of width, height and colorInfo.
182  */
SkImageInfoIsValid(const SkImageInfo & info)183 static inline bool SkImageInfoIsValid(const SkImageInfo& info) {
184     if (info.width() <= 0 || info.height() <= 0) {
185         return false;
186     }
187 
188     const int kMaxDimension = SK_MaxS32 >> 2;
189     if (info.width() > kMaxDimension || info.height() > kMaxDimension) {
190         return false;
191     }
192 
193     return SkColorInfoIsValid(info.colorInfo());
194 }
195 
196 /**
197  *  Returns true if Skia has defined a pixel conversion from the |src| to the |dst|.
198  *  Returns false otherwise.
199  */
SkImageInfoValidConversion(const SkImageInfo & dst,const SkImageInfo & src)200 static inline bool SkImageInfoValidConversion(const SkImageInfo& dst, const SkImageInfo& src) {
201     return SkImageInfoIsValid(dst) && SkImageInfoIsValid(src);
202 }
203 #endif  // SkImageInfoPriv_DEFINED
204