blob: 5e4abb82c1d71b3026835b0bd7d9ef4edb9273d3 [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
Brian Salomon2f23ae62020-03-26 16:17:56 -040011#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkImageInfo.h"
Hal Canary6b20a552017-02-07 14:09:38 -050013
Brian Salomon2f23ae62020-03-26 16:17:56 -040014static inline uint32_t SkColorTypeChannelFlags(SkColorType ct) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -040015 switch (ct) {
Robert Phillipsea1b30b2019-09-19 16:05:48 -040016 case kUnknown_SkColorType: return 0;
Brian Salomon2f23ae62020-03-26 16:17:56 -040017 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;
Brian Osman9f1e06a2021-08-10 14:39:18 -040037 case kSRGBA_8888_SkColorType: return kRGBA_SkColorChannelFlags;
Brian Salomon5fba7ad2018-03-22 10:01:16 -040038 }
Robert Phillipsd470e1b2019-09-04 15:05:35 -040039 SkUNREACHABLE;
Brian Salomon5fba7ad2018-03-22 10:01:16 -040040}
41
42static inline bool SkColorTypeIsAlphaOnly(SkColorType ct) {
Brian Salomon2f23ae62020-03-26 16:17:56 -040043 return SkColorTypeChannelFlags(ct) == kAlpha_SkColorChannelFlag;
Brian Salomon5fba7ad2018-03-22 10:01:16 -040044}
45
Mike Reed7fcfb622018-02-09 13:26:46 -050046static inline bool SkAlphaTypeIsValid(unsigned value) {
47 return value <= kLastEnum_SkAlphaType;
48}
49
50static int SkColorTypeShiftPerPixel(SkColorType ct) {
51 switch (ct) {
Robert Phillipsea1b30b2019-09-19 16:05:48 -040052 case kUnknown_SkColorType: return 0;
53 case kAlpha_8_SkColorType: return 0;
54 case kRGB_565_SkColorType: return 1;
55 case kARGB_4444_SkColorType: return 1;
56 case kRGBA_8888_SkColorType: return 2;
57 case kRGB_888x_SkColorType: return 2;
58 case kBGRA_8888_SkColorType: return 2;
59 case kRGBA_1010102_SkColorType: return 2;
60 case kRGB_101010x_SkColorType: return 2;
Mike Kleinf7eb0542020-02-11 12:19:08 -060061 case kBGRA_1010102_SkColorType: return 2;
62 case kBGR_101010x_SkColorType: return 2;
Robert Phillipsea1b30b2019-09-19 16:05:48 -040063 case kGray_8_SkColorType: return 0;
64 case kRGBA_F16Norm_SkColorType: return 3;
65 case kRGBA_F16_SkColorType: return 3;
66 case kRGBA_F32_SkColorType: return 4;
67 case kR8G8_unorm_SkColorType: return 1;
68 case kA16_unorm_SkColorType: return 1;
69 case kR16G16_unorm_SkColorType: return 2;
70 case kA16_float_SkColorType: return 1;
71 case kR16G16_float_SkColorType: return 2;
72 case kR16G16B16A16_unorm_SkColorType: return 3;
Brian Osman9f1e06a2021-08-10 14:39:18 -040073 case kSRGBA_8888_SkColorType: return 2;
Mike Reed7fcfb622018-02-09 13:26:46 -050074 }
Robert Phillipsd470e1b2019-09-04 15:05:35 -040075 SkUNREACHABLE;
Mike Reed7fcfb622018-02-09 13:26:46 -050076}
77
78static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) {
Mike Klein89c909e2020-06-01 15:50:19 -050079 return (size_t)(width * SkColorTypeBytesPerPixel(ct));
Mike Reed7fcfb622018-02-09 13:26:46 -050080}
81
82static inline bool SkColorTypeIsValid(unsigned value) {
83 return value <= kLastEnum_SkColorType;
84}
85
86static inline size_t SkColorTypeComputeOffset(SkColorType ct, int x, int y, size_t rowBytes) {
87 if (kUnknown_SkColorType == ct) {
88 return 0;
89 }
Mike Klein89c909e2020-06-01 15:50:19 -050090 return (size_t)y * rowBytes + ((size_t)x << SkColorTypeShiftPerPixel(ct));
Mike Reed7fcfb622018-02-09 13:26:46 -050091}
92
Mike Kleinb2ab7d12019-11-08 10:42:13 -060093static inline bool SkColorTypeIsNormalized(SkColorType ct) {
94 switch (ct) {
95 case kUnknown_SkColorType:
96 case kAlpha_8_SkColorType:
97 case kRGB_565_SkColorType:
98 case kARGB_4444_SkColorType:
99 case kRGBA_8888_SkColorType:
100 case kRGB_888x_SkColorType:
101 case kBGRA_8888_SkColorType:
102 case kRGBA_1010102_SkColorType:
103 case kRGB_101010x_SkColorType:
Mike Kleinf7eb0542020-02-11 12:19:08 -0600104 case kBGRA_1010102_SkColorType:
105 case kBGR_101010x_SkColorType:
Mike Kleinb2ab7d12019-11-08 10:42:13 -0600106 case kGray_8_SkColorType:
107 case kRGBA_F16Norm_SkColorType:
108 case kR8G8_unorm_SkColorType:
109 case kA16_unorm_SkColorType:
110 case kA16_float_SkColorType: /*subtle... alpha is always [0,1]*/
111 case kR16G16_unorm_SkColorType:
Brian Osman9f1e06a2021-08-10 14:39:18 -0400112 case kR16G16B16A16_unorm_SkColorType:
113 case kSRGBA_8888_SkColorType: return true;
Mike Kleinb2ab7d12019-11-08 10:42:13 -0600114
115 case kRGBA_F16_SkColorType:
116 case kRGBA_F32_SkColorType:
117 case kR16G16_float_SkColorType: return false;
118 }
119 SkUNREACHABLE;
120}
121
Mike Reedb9bd12d2021-03-04 08:13:56 -0500122static inline int SkColorTypeMaxBitsPerChannel(SkColorType ct) {
123 switch (ct) {
124 case kUnknown_SkColorType:
125 return 0;
126
127 case kARGB_4444_SkColorType:
128 return 4;
129
130 case kRGB_565_SkColorType:
131 return 6;
132
133 case kAlpha_8_SkColorType:
134 case kRGBA_8888_SkColorType:
135 case kRGB_888x_SkColorType:
136 case kBGRA_8888_SkColorType:
137 case kGray_8_SkColorType:
138 case kR8G8_unorm_SkColorType:
Brian Osman9f1e06a2021-08-10 14:39:18 -0400139 case kSRGBA_8888_SkColorType:
Mike Reedb9bd12d2021-03-04 08:13:56 -0500140 return 8;
141
142 case kRGBA_1010102_SkColorType:
143 case kRGB_101010x_SkColorType:
144 case kBGRA_1010102_SkColorType:
145 case kBGR_101010x_SkColorType:
146 return 10;
147
148 case kRGBA_F16Norm_SkColorType:
149 case kA16_unorm_SkColorType:
150 case kA16_float_SkColorType:
151 case kR16G16_unorm_SkColorType:
152 case kR16G16B16A16_unorm_SkColorType:
153 case kRGBA_F16_SkColorType:
154 case kR16G16_float_SkColorType:
155 return 16;
156
157 case kRGBA_F32_SkColorType:
158 return 32;
159 }
160 SkUNREACHABLE;
161}
162
Matt Sarettcb6266b2017-01-17 10:48:53 -0500163/**
Brian Salomon2cae3b02020-03-27 11:18:08 -0400164 * Returns true if |info| contains a valid colorType and alphaType.
165 */
166static inline bool SkColorInfoIsValid(const SkColorInfo& info) {
167 return info.colorType() != kUnknown_SkColorType && info.alphaType() != kUnknown_SkAlphaType;
168}
169
170/**
171 * Returns true if |info| contains a valid combination of width, height and colorInfo.
Matt Sarettcb6266b2017-01-17 10:48:53 -0500172 */
Brian Osmane1adc3a2018-06-04 09:21:17 -0400173static inline bool SkImageInfoIsValid(const SkImageInfo& info) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500174 if (info.width() <= 0 || info.height() <= 0) {
175 return false;
176 }
177
Matt Sarett8572d852017-02-14 11:21:02 -0500178 const int kMaxDimension = SK_MaxS32 >> 2;
179 if (info.width() > kMaxDimension || info.height() > kMaxDimension) {
180 return false;
181 }
182
Brian Salomon2cae3b02020-03-27 11:18:08 -0400183 return SkColorInfoIsValid(info.colorInfo());
Matt Sarett26b44df2017-05-02 16:04:56 -0400184}
185
186/**
Matt Sarettcb6266b2017-01-17 10:48:53 -0500187 * Returns true if Skia has defined a pixel conversion from the |src| to the |dst|.
Mike Klein12d4b6e2018-08-17 14:09:55 -0400188 * Returns false otherwise.
Matt Sarettcb6266b2017-01-17 10:48:53 -0500189 */
190static inline bool SkImageInfoValidConversion(const SkImageInfo& dst, const SkImageInfo& src) {
Mike Klein12d4b6e2018-08-17 14:09:55 -0400191 return SkImageInfoIsValid(dst) && SkImageInfoIsValid(src);
Matt Sarettcb6266b2017-01-17 10:48:53 -0500192}
Hal Canary6b20a552017-02-07 14:09:38 -0500193#endif // SkImageInfoPriv_DEFINED