Debian Codegraph @

src/core/SkImageInfo.cpp

raw
1 /*
2 * Copyright 2010 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 #include "include/core/SkImageInfo.h"
9
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/private/base/SkAssert.h"
13 #include "src/base/SkSafeMath.h"
14 #include "src/core/SkImageInfoPriv.h"
15
16 int SkColorTypeBytesPerPixel(SkColorType ct) {
17 switch (ct) {
18 case kUnknown_SkColorType: return 0;
19 case kAlpha_8_SkColorType: return 1;
20 case kRGB_565_SkColorType: return 2;
21 case kARGB_4444_SkColorType: return 2;
22 case kRGBA_8888_SkColorType: return 4;
23 case kBGRA_8888_SkColorType: return 4;
24 case kRGB_888x_SkColorType: return 4;
25 case kRGBA_1010102_SkColorType: return 4;
26 case kRGB_101010x_SkColorType: return 4;
27 case kBGRA_1010102_SkColorType: return 4;
28 case kBGR_101010x_SkColorType: return 4;
29 case kBGR_101010x_XR_SkColorType: return 4;
30 case kBGRA_10101010_XR_SkColorType: return 8;
31 case kRGBA_10x6_SkColorType: return 8;
32 case kGray_8_SkColorType: return 1;
33 case kRGBA_F16Norm_SkColorType: return 8;
34 case kRGBA_F16_SkColorType: return 8;
35 case kRGB_F16F16F16x_SkColorType: return 8;
36 case kRGBA_F32_SkColorType: return 16;
37 case kR8G8_unorm_SkColorType: return 2;
38 case kA16_unorm_SkColorType: return 2;
39 case kR16_unorm_SkColorType: return 2;
40 case kR16G16_unorm_SkColorType: return 4;
41 case kA16_float_SkColorType: return 2;
42 case kR16G16_float_SkColorType: return 4;
43 case kR16G16B16A16_unorm_SkColorType: return 8;
44 case kSRGBA_8888_SkColorType: return 4;
45 case kR8_unorm_SkColorType: return 1;
46 }
47 SkUNREACHABLE;
48 }
49
50 bool SkColorTypeIsAlwaysOpaque(SkColorType ct) {
51 return !(SkColorTypeChannelFlags(ct) & kAlpha_SkColorChannelFlag);
52 }
53
54 ///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 bool SkYUVColorSpaceIsLimitedRange(SkYUVColorSpace cs) {
57 switch (cs) {
58 case kRec601_Limited_SkYUVColorSpace:
59 case kRec709_Limited_SkYUVColorSpace:
60 case kBT2020_8bit_Limited_SkYUVColorSpace:
61 case kBT2020_10bit_Limited_SkYUVColorSpace:
62 case kBT2020_12bit_Limited_SkYUVColorSpace:
63 case kBT2020_16bit_Limited_SkYUVColorSpace:
64 case kFCC_Limited_SkYUVColorSpace:
65 case kSMPTE240_Limited_SkYUVColorSpace:
66 case kYDZDX_Limited_SkYUVColorSpace:
67 case kGBR_Limited_SkYUVColorSpace:
68 case kYCgCo_8bit_Limited_SkYUVColorSpace:
69 case kYCgCo_10bit_Limited_SkYUVColorSpace:
70 case kYCgCo_12bit_Limited_SkYUVColorSpace:
71 case kYCgCo_16bit_Limited_SkYUVColorSpace:
72 return true;
73
74 case kJPEG_Full_SkYUVColorSpace:
75 case kRec709_Full_SkYUVColorSpace:
76 case kBT2020_8bit_Full_SkYUVColorSpace:
77 case kBT2020_10bit_Full_SkYUVColorSpace:
78 case kBT2020_12bit_Full_SkYUVColorSpace:
79 case kBT2020_16bit_Full_SkYUVColorSpace:
80 case kFCC_Full_SkYUVColorSpace:
81 case kSMPTE240_Full_SkYUVColorSpace:
82 case kYDZDX_Full_SkYUVColorSpace:
83 case kGBR_Full_SkYUVColorSpace:
84 case kYCgCo_8bit_Full_SkYUVColorSpace:
85 case kYCgCo_10bit_Full_SkYUVColorSpace:
86 case kYCgCo_12bit_Full_SkYUVColorSpace:
87 case kYCgCo_16bit_Full_SkYUVColorSpace:
88 case kIdentity_SkYUVColorSpace:
89 return false;
90
91 default:
92 SkUNREACHABLE;
93 }
94 }
95
96 ///////////////////////////////////////////////////////////////////////////////////////////////////
97
98 SkColorInfo::SkColorInfo() = default;
99 SkColorInfo::~SkColorInfo() = default;
100
101 SkColorInfo::SkColorInfo(SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs)
102 : fColorSpace(std::move(cs)), fColorType(ct), fAlphaType(at) {}
103
104 SkColorInfo::SkColorInfo(const SkColorInfo&) = default;
105 SkColorInfo::SkColorInfo(SkColorInfo&&) = default;
106
107 SkColorInfo& SkColorInfo::operator=(const SkColorInfo&) = default;
108 SkColorInfo& SkColorInfo::operator=(SkColorInfo&&) = default;
109
110 SkColorSpace* SkColorInfo::colorSpace() const { return fColorSpace.get(); }
111 sk_sp<SkColorSpace> SkColorInfo::refColorSpace() const { return fColorSpace; }
112
113 bool SkColorInfo::operator==(const SkColorInfo& other) const {
114 return fColorType == other.fColorType && fAlphaType == other.fAlphaType &&
115 SkColorSpace::Equals(fColorSpace.get(), other.fColorSpace.get());
116 }
117
118 bool SkColorInfo::operator!=(const SkColorInfo& other) const { return !(*this == other); }
119
120 SkColorInfo SkColorInfo::makeAlphaType(SkAlphaType newAlphaType) const {
121 return SkColorInfo(this->colorType(), newAlphaType, this->refColorSpace());
122 }
123
124 SkColorInfo SkColorInfo::makeColorType(SkColorType newColorType) const {
125 return SkColorInfo(newColorType, this->alphaType(), this->refColorSpace());
126 }
127
128 SkColorInfo SkColorInfo::makeColorSpace(sk_sp<SkColorSpace> cs) const {
129 return SkColorInfo(this->colorType(), this->alphaType(), std::move(cs));
130 }
131
132 int SkColorInfo::bytesPerPixel() const { return SkColorTypeBytesPerPixel(fColorType); }
133
134 bool SkColorInfo::gammaCloseToSRGB() const {
135 return fColorSpace && fColorSpace->gammaCloseToSRGB();
136 }
137
138 int SkColorInfo::shiftPerPixel() const { return SkColorTypeShiftPerPixel(fColorType); }
139
140 ///////////////////////////////////////////////////////////////////////////////////////////////////
141
142 size_t SkImageInfo::computeOffset(int x, int y, size_t rowBytes) const {
143 SkASSERTF(x >= 0 && x < this->width(), "x=%d; width=%d\n", x, this->width());
144 SkASSERTF(y >= 0 && y < this->height(), "y=%d; height=%d\n", y, this->height());
145 return SkColorTypeComputeOffset(this->colorType(), x, y, rowBytes);
146 }
147
148 size_t SkImageInfo::computeByteSize(size_t rowBytes) const {
149 if (0 == this->height()) {
150 return 0;
151 }
152 SkSafeMath safe;
153 size_t bytes = safe.add(safe.mul(safe.addInt(this->height(), -1), rowBytes),
154 safe.mul(this->width(), this->bytesPerPixel()));
155
156 // The CPU backend implements some memory operations on images using instructions that take a
157 // signed 32-bit offset from the base. If we ever make an image larger than that, overflow can
158 // cause us to read/write memory that starts 2GB *before* the buffer. (crbug.com/1264705)
159 constexpr size_t kMaxSigned32BitSize = SK_MaxS32;
160 return (safe.ok() && (bytes <= kMaxSigned32BitSize)) ? bytes : SIZE_MAX;
161 }
162
163 SkColorSpace* SkImageInfo::colorSpace() const { return fColorInfo.colorSpace(); }
164
165 sk_sp<SkColorSpace> SkImageInfo::refColorSpace() const { return fColorInfo.refColorSpace(); }
166
167 SkImageInfo SkImageInfo::makeColorSpace(sk_sp<SkColorSpace> cs) const {
168 return Make(fDimensions, fColorInfo.makeColorSpace(std::move(cs)));
169 }
170
171 SkImageInfo SkImageInfo::Make(int width, int height, SkColorType ct, SkAlphaType at) {
172 return Make(width, height, ct, at, nullptr);
173 }
174
175 SkImageInfo SkImageInfo::Make(int width, int height, SkColorType ct, SkAlphaType at,
176 sk_sp<SkColorSpace> cs) {
177 return SkImageInfo({width, height}, {ct, at, std::move(cs)});
178 }
179
180 SkImageInfo SkImageInfo::Make(SkISize dimensions, SkColorType ct, SkAlphaType at) {
181 return Make(dimensions, ct, at, nullptr);
182 }
183
184 SkImageInfo SkImageInfo::Make(SkISize dimensions, SkColorType ct, SkAlphaType at,
185 sk_sp<SkColorSpace> cs) {
186 return SkImageInfo(dimensions, {ct, at, std::move(cs)});
187 }
188
189 SkImageInfo SkImageInfo::MakeN32(int width, int height, SkAlphaType at) {
190 return MakeN32(width, height, at, nullptr);
191 }
192
193 SkImageInfo SkImageInfo::MakeN32(int width, int height, SkAlphaType at, sk_sp<SkColorSpace> cs) {
194 return Make({width, height}, kN32_SkColorType, at, std::move(cs));
195 }
196
197 SkImageInfo SkImageInfo::MakeS32(int width, int height, SkAlphaType at) {
198 return SkImageInfo({width, height}, {kN32_SkColorType, at, SkColorSpace::MakeSRGB()});
199 }
200
201 SkImageInfo SkImageInfo::MakeN32Premul(int width, int height) {
202 return MakeN32Premul(width, height, nullptr);
203 }
204
205 SkImageInfo SkImageInfo::MakeN32Premul(int width, int height, sk_sp<SkColorSpace> cs) {
206 return Make({width, height}, kN32_SkColorType, kPremul_SkAlphaType, std::move(cs));
207 }
208
209 SkImageInfo SkImageInfo::MakeN32Premul(SkISize dimensions) {
210 return MakeN32Premul(dimensions, nullptr);
211 }
212
213 SkImageInfo SkImageInfo::MakeN32Premul(SkISize dimensions, sk_sp<SkColorSpace> cs) {
214 return Make(dimensions, kN32_SkColorType, kPremul_SkAlphaType, std::move(cs));
215 }
216
217 SkImageInfo SkImageInfo::MakeA8(int width, int height) {
218 return Make({width, height}, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr);
219 }
220
221 SkImageInfo SkImageInfo::MakeA8(SkISize dimensions) {
222 return Make(dimensions, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr);
223 }
224
225 SkImageInfo SkImageInfo::MakeUnknown(int width, int height) {
226 return Make({width, height}, kUnknown_SkColorType, kUnknown_SkAlphaType, nullptr);
227 }
228
229 #ifdef SK_DEBUG
230 void SkImageInfo::validate() const {
231 SkASSERT(fDimensions.width() >= 0);
232 SkASSERT(fDimensions.height() >= 0);
233 SkASSERT(SkColorTypeIsValid(this->colorType()));
234 SkASSERT(SkAlphaTypeIsValid(this->alphaType()));
235 }
236 #endif
237
238 bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
239 SkAlphaType* canonical) {
240 switch (colorType) {
241 case kUnknown_SkColorType:
242 alphaType = kUnknown_SkAlphaType;
243 break;
244 case kAlpha_8_SkColorType: // fall-through
245 case kA16_unorm_SkColorType: // fall-through
246 case kA16_float_SkColorType:
247 if (kUnpremul_SkAlphaType == alphaType) {
248 alphaType = kPremul_SkAlphaType;
249 }
250 [[fallthrough]];
251 case kARGB_4444_SkColorType:
252 case kRGBA_8888_SkColorType:
253 case kSRGBA_8888_SkColorType:
254 case kBGRA_8888_SkColorType:
255 case kRGBA_1010102_SkColorType:
256 case kBGRA_1010102_SkColorType:
257 case kRGBA_10x6_SkColorType:
258 case kRGBA_F16Norm_SkColorType:
259 case kRGBA_F16_SkColorType:
260 case kRGBA_F32_SkColorType:
261 case kBGRA_10101010_XR_SkColorType:
262 case kR16G16B16A16_unorm_SkColorType:
263 if (kUnknown_SkAlphaType == alphaType) {
264 return false;
265 }
266 break;
267 case kGray_8_SkColorType:
268 case kR8G8_unorm_SkColorType:
269 case kR16_unorm_SkColorType:
270 case kR16G16_unorm_SkColorType:
271 case kR16G16_float_SkColorType:
272 case kRGB_565_SkColorType:
273 case kRGB_888x_SkColorType:
274 case kRGB_101010x_SkColorType:
275 case kBGR_101010x_SkColorType:
276 case kBGR_101010x_XR_SkColorType:
277 case kRGB_F16F16F16x_SkColorType:
278 case kR8_unorm_SkColorType:
279 alphaType = kOpaque_SkAlphaType;
280 break;
281 }
282 if (canonical) {
283 *canonical = alphaType;
284 }
285 return true;
286 }