Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkImageInfo.h
1 /*
2  * Copyright 2013 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 SkImageInfo_DEFINED
9 #define SkImageInfo_DEFINED
10 
11 #include "SkColorSpace.h"
12 #include "SkMath.h"
13 #include "SkRect.h"
14 #include "SkSize.h"
15 
16 class SkReadBuffer;
17 class SkWriteBuffer;
18 
22 enum SkAlphaType {
23  kUnknown_SkAlphaType,
24 
31  kOpaque_SkAlphaType,
32 
37  kPremul_SkAlphaType,
38 
46  kUnpremul_SkAlphaType,
47 
48  kLastEnum_SkAlphaType = kUnpremul_SkAlphaType
49 };
50 
51 static inline bool SkAlphaTypeIsOpaque(SkAlphaType at) {
52  return kOpaque_SkAlphaType == at;
53 }
54 
55 static inline bool SkAlphaTypeIsValid(unsigned value) {
56  return value <= kLastEnum_SkAlphaType;
57 }
58 
60 
68 enum SkColorType {
69  kUnknown_SkColorType,
70  kAlpha_8_SkColorType,
71  kRGB_565_SkColorType,
72  kARGB_4444_SkColorType,
73  kRGBA_8888_SkColorType,
74  kBGRA_8888_SkColorType,
75  kIndex_8_SkColorType,
76  kGray_8_SkColorType,
77  kRGBA_F16_SkColorType,
78 
79  kLastEnum_SkColorType = kRGBA_F16_SkColorType,
80 
81 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
82  kN32_SkColorType = kBGRA_8888_SkColorType,
83 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
84  kN32_SkColorType = kRGBA_8888_SkColorType,
85 #else
86  #error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order"
87 #endif
88 };
89 
90 static int SkColorTypeBytesPerPixel(SkColorType ct) {
91  static const uint8_t gSize[] = {
92  0, // Unknown
93  1, // Alpha_8
94  2, // RGB_565
95  2, // ARGB_4444
96  4, // RGBA_8888
97  4, // BGRA_8888
98  1, // kIndex_8
99  1, // kGray_8
100  8, // kRGBA_F16
101  };
102  static_assert(SK_ARRAY_COUNT(gSize) == (size_t)(kLastEnum_SkColorType + 1),
103  "size_mismatch_with_SkColorType_enum");
104 
105  SkASSERT((size_t)ct < SK_ARRAY_COUNT(gSize));
106  return gSize[ct];
107 }
108 
109 static int SkColorTypeShiftPerPixel(SkColorType ct) {
110  static const uint8_t gShift[] = {
111  0, // Unknown
112  0, // Alpha_8
113  1, // RGB_565
114  1, // ARGB_4444
115  2, // RGBA_8888
116  2, // BGRA_8888
117  0, // kIndex_8
118  0, // kGray_8
119  3, // kRGBA_F16
120  };
121  static_assert(SK_ARRAY_COUNT(gShift) == (size_t)(kLastEnum_SkColorType + 1),
122  "size_mismatch_with_SkColorType_enum");
123 
124  SkASSERT((size_t)ct < SK_ARRAY_COUNT(gShift));
125  return gShift[ct];
126 }
127 
128 static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) {
129  return width * SkColorTypeBytesPerPixel(ct);
130 }
131 
132 static inline bool SkColorTypeIsValid(unsigned value) {
133  return value <= kLastEnum_SkColorType;
134 }
135 
136 static inline size_t SkColorTypeComputeOffset(SkColorType ct, int x, int y, size_t rowBytes) {
137  if (kUnknown_SkColorType == ct) {
138  return 0;
139  }
140  return y * rowBytes + (x << SkColorTypeShiftPerPixel(ct));
141 }
142 
144 
149 bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
150  SkAlphaType* canonical = NULL);
151 
153 
157 enum SkYUVColorSpace {
159  kJPEG_SkYUVColorSpace,
162  kRec601_SkYUVColorSpace,
165  kRec709_SkYUVColorSpace,
166 
167  kLastEnum_SkYUVColorSpace = kRec709_SkYUVColorSpace
168 };
169 
171 
172 enum class SkSourceGammaTreatment {
173  kRespect,
174  kIgnore,
175 };
176 
181 struct SK_API SkImageInfo {
182 public:
183  SkImageInfo()
184  : fColorSpace(nullptr)
185  , fWidth(0)
186  , fHeight(0)
187  , fColorType(kUnknown_SkColorType)
188  , fAlphaType(kUnknown_SkAlphaType)
189  {}
190 
191  static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at,
192  sk_sp<SkColorSpace> cs = nullptr) {
193  return SkImageInfo(width, height, ct, at, std::move(cs));
194  }
195 
199  static SkImageInfo MakeN32(int width, int height, SkAlphaType at,
200  sk_sp<SkColorSpace> cs = nullptr) {
201  return Make(width, height, kN32_SkColorType, at, cs);
202  }
203 
207  static SkImageInfo MakeS32(int width, int height, SkAlphaType at);
208 
212  static SkImageInfo MakeN32Premul(int width, int height, sk_sp<SkColorSpace> cs = nullptr) {
213  return Make(width, height, kN32_SkColorType, kPremul_SkAlphaType, cs);
214  }
215 
216  static SkImageInfo MakeN32Premul(const SkISize& size) {
217  return MakeN32Premul(size.width(), size.height());
218  }
219 
220  static SkImageInfo MakeA8(int width, int height) {
221  return Make(width, height, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr);
222  }
223 
224  static SkImageInfo MakeUnknown(int width, int height) {
225  return Make(width, height, kUnknown_SkColorType, kUnknown_SkAlphaType, nullptr);
226  }
227 
228  static SkImageInfo MakeUnknown() {
229  return MakeUnknown(0, 0);
230  }
231 
232  int width() const { return fWidth; }
233  int height() const { return fHeight; }
234  SkColorType colorType() const { return fColorType; }
235  SkAlphaType alphaType() const { return fAlphaType; }
236  SkColorSpace* colorSpace() const { return fColorSpace.get(); }
237 
238  bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; }
239 
240  bool isOpaque() const {
241  return SkAlphaTypeIsOpaque(fAlphaType);
242  }
243 
244  SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); }
245  SkIRect bounds() const { return SkIRect::MakeWH(fWidth, fHeight); }
246 
247  bool gammaCloseToSRGB() const {
248  return fColorSpace && fColorSpace->gammaCloseToSRGB();
249  }
250 
255  SkImageInfo makeWH(int newWidth, int newHeight) const {
256  return Make(newWidth, newHeight, fColorType, fAlphaType, fColorSpace);
257  }
258 
259  SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const {
260  return Make(fWidth, fHeight, fColorType, newAlphaType, fColorSpace);
261  }
262 
263  SkImageInfo makeColorType(SkColorType newColorType) const {
264  return Make(fWidth, fHeight, newColorType, fAlphaType, fColorSpace);
265  }
266 
267  SkImageInfo makeColorSpace(sk_sp<SkColorSpace> cs) const {
268  return Make(fWidth, fHeight, fColorType, fAlphaType, std::move(cs));
269  }
270 
271  int bytesPerPixel() const { return SkColorTypeBytesPerPixel(fColorType); }
272 
273  int shiftPerPixel() const { return SkColorTypeShiftPerPixel(fColorType); }
274 
275  uint64_t minRowBytes64() const {
276  return sk_64_mul(fWidth, this->bytesPerPixel());
277  }
278 
279  size_t minRowBytes() const {
280  return (size_t)this->minRowBytes64();
281  }
282 
283  size_t computeOffset(int x, int y, size_t rowBytes) const {
284  SkASSERT((unsigned)x < (unsigned)fWidth);
285  SkASSERT((unsigned)y < (unsigned)fHeight);
286  return SkColorTypeComputeOffset(fColorType, x, y, rowBytes);
287  }
288 
289  bool operator==(const SkImageInfo& other) const {
290  return fWidth == other.fWidth && fHeight == other.fHeight &&
291  fColorType == other.fColorType && fAlphaType == other.fAlphaType &&
292  SkColorSpace::Equals(fColorSpace.get(), other.fColorSpace.get());
293  }
294  bool operator!=(const SkImageInfo& other) const {
295  return !(*this == other);
296  }
297 
298  void unflatten(SkReadBuffer&);
299  void flatten(SkWriteBuffer&) const;
300 
301  int64_t getSafeSize64(size_t rowBytes) const {
302  if (0 == fHeight) {
303  return 0;
304  }
305  return sk_64_mul(fHeight - 1, rowBytes) + fWidth * this->bytesPerPixel();
306  }
307 
308  size_t getSafeSize(size_t rowBytes) const {
309  int64_t size = this->getSafeSize64(rowBytes);
310  if (!sk_64_isS32(size)) {
311  return 0;
312  }
313  return sk_64_asS32(size);
314  }
315 
316  bool validRowBytes(size_t rowBytes) const {
317  uint64_t rb = sk_64_mul(fWidth, this->bytesPerPixel());
318  return rowBytes >= rb;
319  }
320 
321  void reset() {
322  fColorSpace = nullptr;
323  fWidth = 0;
324  fHeight = 0;
325  fColorType = kUnknown_SkColorType;
326  fAlphaType = kUnknown_SkAlphaType;
327  }
328 
329  SkDEBUGCODE(void validate() const;)
330 
331 private:
332  sk_sp<SkColorSpace> fColorSpace;
333  int fWidth;
334  int fHeight;
335  SkColorType fColorType;
336  SkAlphaType fAlphaType;
337 
338  SkImageInfo(int width, int height, SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs)
339  : fColorSpace(std::move(cs))
340  , fWidth(width)
341  , fHeight(height)
342  , fColorType(ct)
343  , fAlphaType(at)
344  {}
345 };
346 
348 
349 static inline bool SkColorAndColorSpaceAreGammaCorrect(SkColorType ct, SkColorSpace* cs) {
350  // Anything with a color-space attached is gamma-correct, as is F16.
351  // To get legacy behavior, you need to ask for non-F16, with a nullptr color space.
352  return (cs != nullptr) || kRGBA_F16_SkColorType == ct;
353 }
354 
355 static inline bool SkImageInfoIsGammaCorrect(const SkImageInfo& info) {
356  return SkColorAndColorSpaceAreGammaCorrect(info.colorType(), info.colorSpace());
357 }
358 
359 #endif
Definition: SkColorSpace.h:16
Describe an image's dimensions and pixel type.
Definition: SkImageInfo.h:181
static SkImageInfo MakeN32(int width, int height, SkAlphaType at, sk_sp< SkColorSpace > cs=nullptr)
Sets colortype to the native ARGB32 type.
Definition: SkImageInfo.h:199
static SkImageInfo MakeN32Premul(int width, int height, sk_sp< SkColorSpace > cs=nullptr)
Sets colortype to the native ARGB32 type, and the alphatype to premul.
Definition: SkImageInfo.h:212
Definition: SkWriteBuffer.h:26
SkIRect holds four 32 bit integer coordinates for a rectangle.
Definition: SkRect.h:20
SkImageInfo makeWH(int newWidth, int newHeight) const
Return a new ImageInfo with the same colortype and alphatype as this info, but with the specified wid...
Definition: SkImageInfo.h:255
static bool Equals(const SkColorSpace *src, const SkColorSpace *dst)
If both are null, we return true.