Flutter Engine
The Flutter Engine
SkImageInfo.cpp
Go to the documentation of this file.
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
9
13#include "src/base/SkSafeMath.h"
15
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 kRGBA_F32_SkColorType: return 16;
36 case kR8G8_unorm_SkColorType: return 2;
37 case kA16_unorm_SkColorType: return 2;
38 case kR16G16_unorm_SkColorType: return 4;
39 case kA16_float_SkColorType: return 2;
40 case kR16G16_float_SkColorType: return 4;
42 case kSRGBA_8888_SkColorType: return 4;
43 case kR8_unorm_SkColorType: return 1;
44 }
46}
47
50}
51
52///////////////////////////////////////////////////////////////////////////////////////////////////
53
54SkColorInfo::SkColorInfo() = default;
56
58 : fColorSpace(std::move(cs)), fColorType(ct), fAlphaType(at) {}
59
62
65
66SkColorSpace* SkColorInfo::colorSpace() const { return fColorSpace.get(); }
67sk_sp<SkColorSpace> SkColorInfo::refColorSpace() const { return fColorSpace; }
68
69bool SkColorInfo::operator==(const SkColorInfo& other) const {
70 return fColorType == other.fColorType && fAlphaType == other.fAlphaType &&
71 SkColorSpace::Equals(fColorSpace.get(), other.fColorSpace.get());
72}
73
74bool SkColorInfo::operator!=(const SkColorInfo& other) const { return !(*this == other); }
75
77 return SkColorInfo(this->colorType(), newAlphaType, this->refColorSpace());
78}
79
81 return SkColorInfo(newColorType, this->alphaType(), this->refColorSpace());
82}
83
85 return SkColorInfo(this->colorType(), this->alphaType(), std::move(cs));
86}
87
88int SkColorInfo::bytesPerPixel() const { return SkColorTypeBytesPerPixel(fColorType); }
89
91 return fColorSpace && fColorSpace->gammaCloseToSRGB();
92}
93
94int SkColorInfo::shiftPerPixel() const { return SkColorTypeShiftPerPixel(fColorType); }
95
96///////////////////////////////////////////////////////////////////////////////////////////////////
97
98size_t SkImageInfo::computeOffset(int x, int y, size_t rowBytes) const {
99 SkASSERT((unsigned)x < (unsigned)this->width());
100 SkASSERT((unsigned)y < (unsigned)this->height());
101 return SkColorTypeComputeOffset(this->colorType(), x, y, rowBytes);
102}
103
104size_t SkImageInfo::computeByteSize(size_t rowBytes) const {
105 if (0 == this->height()) {
106 return 0;
107 }
108 SkSafeMath safe;
109 size_t bytes = safe.add(safe.mul(safe.addInt(this->height(), -1), rowBytes),
110 safe.mul(this->width(), this->bytesPerPixel()));
111
112 // The CPU backend implements some memory operations on images using instructions that take a
113 // signed 32-bit offset from the base. If we ever make an image larger than that, overflow can
114 // cause us to read/write memory that starts 2GB *before* the buffer. (crbug.com/1264705)
115 constexpr size_t kMaxSigned32BitSize = SK_MaxS32;
116 return (safe.ok() && (bytes <= kMaxSigned32BitSize)) ? bytes : SIZE_MAX;
117}
118
119SkColorSpace* SkImageInfo::colorSpace() const { return fColorInfo.colorSpace(); }
120
121sk_sp<SkColorSpace> SkImageInfo::refColorSpace() const { return fColorInfo.refColorSpace(); }
122
124 return Make(fDimensions, fColorInfo.makeColorSpace(std::move(cs)));
125}
126
128 return Make(width, height, ct, at, nullptr);
129}
130
133 return SkImageInfo({width, height}, {ct, at, std::move(cs)});
134}
135
137 return Make(dimensions, ct, at, nullptr);
138}
139
142 return SkImageInfo(dimensions, {ct, at, std::move(cs)});
143}
144
146 return MakeN32(width, height, at, nullptr);
147}
148
150 return Make({width, height}, kN32_SkColorType, at, std::move(cs));
151}
152
154 return SkImageInfo({width, height}, {kN32_SkColorType, at, SkColorSpace::MakeSRGB()});
155}
156
158 return MakeN32Premul(width, height, nullptr);
159}
160
162 return Make({width, height}, kN32_SkColorType, kPremul_SkAlphaType, std::move(cs));
163}
164
166 return MakeN32Premul(dimensions, nullptr);
167}
168
170 return Make(dimensions, kN32_SkColorType, kPremul_SkAlphaType, std::move(cs));
171}
172
175}
176
179}
180
183}
184
185#ifdef SK_DEBUG
186void SkImageInfo::validate() const {
187 SkASSERT(fDimensions.width() >= 0);
191}
192#endif
193
195 SkAlphaType* canonical) {
196 switch (colorType) {
198 alphaType = kUnknown_SkAlphaType;
199 break;
200 case kAlpha_8_SkColorType: // fall-through
201 case kA16_unorm_SkColorType: // fall-through
203 if (kUnpremul_SkAlphaType == alphaType) {
204 alphaType = kPremul_SkAlphaType;
205 }
206 [[fallthrough]];
219 if (kUnknown_SkAlphaType == alphaType) {
220 return false;
221 }
222 break;
233 alphaType = kOpaque_SkAlphaType;
234 break;
235 }
236 if (canonical) {
237 *canonical = alphaType;
238 }
239 return true;
240}
SkColorType fColorType
kUnpremul_SkAlphaType
SkAlphaType fAlphaType
SkAlphaType
Definition: SkAlphaType.h:26
@ kUnknown_SkAlphaType
uninitialized
Definition: SkAlphaType.h:27
@ kOpaque_SkAlphaType
pixel is opaque
Definition: SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
#define SkUNREACHABLE
Definition: SkAssert.h:135
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkColorType
Definition: SkColorType.h:19
@ kR16G16B16A16_unorm_SkColorType
pixel with a little endian uint16_t for red, green, blue
Definition: SkColorType.h:50
@ kRGBA_10x6_SkColorType
pixel with 10 used bits (most significant) followed by 6 unused
Definition: SkColorType.h:33
@ kR8_unorm_SkColorType
Definition: SkColorType.h:54
@ kBGR_101010x_SkColorType
pixel with 10 bits each for blue, green, red; in 32-bit word
Definition: SkColorType.h:30
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
Definition: SkColorType.h:23
@ kR8G8_unorm_SkColorType
pixel with a uint8_t for red and green
Definition: SkColorType.h:43
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
@ kA16_unorm_SkColorType
pixel with a little endian uint16_t for alpha
Definition: SkColorType.h:48
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition: SkColorType.h:38
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition: SkColorType.h:21
@ kRGB_101010x_SkColorType
pixel with 10 bits each for red, green, blue; in 32-bit word
Definition: SkColorType.h:29
@ kSRGBA_8888_SkColorType
Definition: SkColorType.h:53
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition: SkColorType.h:35
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition: SkColorType.h:22
@ kBGRA_10101010_XR_SkColorType
pixel with 10 bits each for blue, green, red, alpha; in 64-bit word, extended range
Definition: SkColorType.h:32
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
@ kRGB_888x_SkColorType
pixel with 8 bits each for red, green, blue; in 32-bit word
Definition: SkColorType.h:25
@ kBGRA_1010102_SkColorType
10 bits for blue, green, red; 2 bits for alpha; in 32-bit word
Definition: SkColorType.h:28
@ kA16_float_SkColorType
pixel with a half float for alpha
Definition: SkColorType.h:45
@ kRGBA_F32_SkColorType
pixel using C float for red, green, blue, alpha; in 128-bit word
Definition: SkColorType.h:40
@ kRGBA_1010102_SkColorType
10 bits for red, green, blue; 2 bits for alpha; in 32-bit word
Definition: SkColorType.h:27
@ kBGR_101010x_XR_SkColorType
pixel with 10 bits each for blue, green, red; in 32-bit word, extended range
Definition: SkColorType.h:31
@ kR16G16_unorm_SkColorType
pixel with a little endian uint16_t for red and green
Definition: SkColorType.h:49
@ kRGBA_F16Norm_SkColorType
pixel with half floats in [0,1] for red, green, blue, alpha;
Definition: SkColorType.h:36
@ kUnknown_SkColorType
uninitialized
Definition: SkColorType.h:20
@ kR16G16_float_SkColorType
pixel with a half float for red and green
Definition: SkColorType.h:46
@ kAlpha_SkColorChannelFlag
Definition: SkColor.h:242
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
static uint32_t SkColorTypeChannelFlags(SkColorType ct)
static bool SkColorTypeIsValid(unsigned value)
static size_t SkColorTypeComputeOffset(SkColorType ct, int x, int y, size_t rowBytes)
static int SkColorTypeShiftPerPixel(SkColorType ct)
static bool SkAlphaTypeIsValid(unsigned value)
bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType, SkAlphaType *canonical)
bool SkColorTypeIsAlwaysOpaque(SkColorType ct)
Definition: SkImageInfo.cpp:48
int SkColorTypeBytesPerPixel(SkColorType ct)
Definition: SkImageInfo.cpp:16
static constexpr int32_t SK_MaxS32
Definition: SkMath.h:21
int bytesPerPixel() const
Definition: SkImageInfo.cpp:88
SkColorInfo makeAlphaType(SkAlphaType newAlphaType) const
Definition: SkImageInfo.cpp:76
SkAlphaType alphaType() const
Definition: SkImageInfo.h:141
bool operator!=(const SkColorInfo &other) const
Definition: SkImageInfo.cpp:74
SkColorInfo makeColorSpace(sk_sp< SkColorSpace > cs) const
Definition: SkImageInfo.cpp:84
int shiftPerPixel() const
Definition: SkImageInfo.cpp:94
SkColorInfo makeColorType(SkColorType newColorType) const
Definition: SkImageInfo.cpp:80
bool gammaCloseToSRGB() const
Definition: SkImageInfo.cpp:90
sk_sp< SkColorSpace > refColorSpace() const
Definition: SkImageInfo.cpp:67
SkColorSpace * colorSpace() const
Definition: SkImageInfo.cpp:66
SkColorInfo & operator=(const SkColorInfo &)
bool operator==(const SkColorInfo &other) const
Definition: SkImageInfo.cpp:69
SkColorType colorType() const
Definition: SkImageInfo.h:140
bool gammaCloseToSRGB() const
static bool Equals(const SkColorSpace *, const SkColorSpace *)
static sk_sp< SkColorSpace > MakeSRGB()
int addInt(int a, int b)
Definition: SkSafeMath.h:43
size_t add(size_t x, size_t y)
Definition: SkSafeMath.h:33
bool ok() const
Definition: SkSafeMath.h:26
size_t mul(size_t x, size_t y)
Definition: SkSafeMath.h:29
T * get() const
Definition: SkRefCnt.h:303
double y
double x
Definition: ref_ptr.h:256
int32_t height
int32_t width
Definition: SkSize.h:16
constexpr int32_t width() const
Definition: SkSize.h:36
constexpr int32_t height() const
Definition: SkSize.h:37
static SkImageInfo MakeN32Premul(int width, int height)
sk_sp< SkColorSpace > refColorSpace() const
static SkImageInfo MakeS32(int width, int height, SkAlphaType at)
size_t computeByteSize(size_t rowBytes) const
SkColorSpace * colorSpace() const
SkISize dimensions() const
Definition: SkImageInfo.h:421
SkDEBUGCODE(void validate() const ;) private SkISize fDimensions
Definition: SkImageInfo.h:619
SkImageInfo makeColorSpace(sk_sp< SkColorSpace > cs) const
static SkImageInfo MakeUnknown()
Definition: SkImageInfo.h:357
int width() const
Definition: SkImageInfo.h:365
static SkImageInfo MakeN32(int width, int height, SkAlphaType at)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
SkAlphaType alphaType() const
Definition: SkImageInfo.h:375
SkColorType colorType() const
Definition: SkImageInfo.h:373
int height() const
Definition: SkImageInfo.h:371
size_t computeOffset(int x, int y, size_t rowBytes) const
Definition: SkImageInfo.cpp:98
SkImageInfo()=default
static SkImageInfo MakeA8(int width, int height)