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