Skia
2D Graphics Library
SkImageInfo.h
Go to the documentation of this file.
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 
13 #include "include/core/SkRect.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkSize.h"
16 #include "include/private/base/SkAPI.h"
17 #include "include/private/base/SkDebug.h"
18 #include "include/private/base/SkMath.h"
19 #include "include/private/base/SkTFitsIn.h"
20 
21 #include <cstddef>
22 #include <cstdint>
23 #include <utility>
24 
25 class SkColorSpace;
26 
33 
40 
52 SK_API bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
53  SkAlphaType* canonical = nullptr);
54 
68 enum SkYUVColorSpace : int {
80 
82 
83  // Legacy (deprecated) names:
88 };
89 
97 class SK_API SkColorInfo {
98 public:
106 
117 
120 
123 
126  SkColorType colorType() const { return fColorType; }
127  SkAlphaType alphaType() const { return fAlphaType; }
128 
129  bool isOpaque() const {
130  return SkAlphaTypeIsOpaque(fAlphaType)
131  || SkColorTypeIsAlwaysOpaque(fColorType);
132  }
133 
134  bool gammaCloseToSRGB() const;
135 
137  bool operator==(const SkColorInfo& other) const;
138 
140  bool operator!=(const SkColorInfo& other) const;
141 
148  SkColorInfo makeAlphaType(SkAlphaType newAlphaType) const;
149 
153  SkColorInfo makeColorType(SkColorType newColorType) const;
154 
159 
167  int bytesPerPixel() const;
168 
176  int shiftPerPixel() const;
177 
178 private:
179  sk_sp<SkColorSpace> fColorSpace;
180  SkColorType fColorType = kUnknown_SkColorType;
181  SkAlphaType fAlphaType = kUnknown_SkAlphaType;
182 };
183 
194 struct SK_API SkImageInfo {
195 public:
196 
202  SkImageInfo() = default;
203 
218  static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at);
219  static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at,
221  static SkImageInfo Make(SkISize dimensions, SkColorType ct, SkAlphaType at);
222  static SkImageInfo Make(SkISize dimensions, SkColorType ct, SkAlphaType at,
224 
235  static SkImageInfo Make(SkISize dimensions, const SkColorInfo& colorInfo) {
236  return SkImageInfo(dimensions, colorInfo);
237  }
238  static SkImageInfo Make(SkISize dimensions, SkColorInfo&& colorInfo) {
239  return SkImageInfo(dimensions, std::move(colorInfo));
240  }
241 
257  static SkImageInfo MakeN32(int width, int height, SkAlphaType at);
258  static SkImageInfo MakeN32(int width, int height, SkAlphaType at, sk_sp<SkColorSpace> cs);
259 
272  static SkImageInfo MakeS32(int width, int height, SkAlphaType at);
273 
288  static SkImageInfo MakeN32Premul(int width, int height);
289  static SkImageInfo MakeN32Premul(int width, int height, sk_sp<SkColorSpace> cs);
290 
304  static SkImageInfo MakeN32Premul(SkISize dimensions);
306 
314  static SkImageInfo MakeA8(int width, int height);
321  static SkImageInfo MakeA8(SkISize dimensions);
322 
333  static SkImageInfo MakeUnknown(int width, int height);
334 
344  return MakeUnknown(0, 0);
345  }
346 
351  int width() const { return fDimensions.width(); }
352 
357  int height() const { return fDimensions.height(); }
358 
359  SkColorType colorType() const { return fColorInfo.colorType(); }
360 
361  SkAlphaType alphaType() const { return fColorInfo.alphaType(); }
362 
369 
379 
385  bool isEmpty() const { return fDimensions.isEmpty(); }
386 
390  const SkColorInfo& colorInfo() const { return fColorInfo; }
391 
401  bool isOpaque() const { return fColorInfo.isOpaque(); }
402 
407  SkISize dimensions() const { return fDimensions; }
408 
413  SkIRect bounds() const { return SkIRect::MakeSize(fDimensions); }
414 
421  bool gammaCloseToSRGB() const { return fColorInfo.gammaCloseToSRGB(); }
422 
430  SkImageInfo makeWH(int newWidth, int newHeight) const {
431  return Make({newWidth, newHeight}, fColorInfo);
432  }
433 
441  return Make(newSize, fColorInfo);
442  }
443 
452  SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const {
453  return Make(fDimensions, fColorInfo.makeAlphaType(newAlphaType));
454  }
455 
461  SkImageInfo makeColorType(SkColorType newColorType) const {
462  return Make(fDimensions, fColorInfo.makeColorType(newColorType));
463  }
464 
472 
478  int bytesPerPixel() const { return fColorInfo.bytesPerPixel(); }
479 
485  int shiftPerPixel() const { return fColorInfo.shiftPerPixel(); }
486 
493  uint64_t minRowBytes64() const {
494  return (uint64_t)sk_64_mul(this->width(), this->bytesPerPixel());
495  }
496 
503  size_t minRowBytes() const {
504  uint64_t minRowBytes = this->minRowBytes64();
505  if (!SkTFitsIn<int32_t>(minRowBytes)) {
506  return 0;
507  }
508  return (size_t)minRowBytes;
509  }
510 
523  size_t computeOffset(int x, int y, size_t rowBytes) const;
524 
531  bool operator==(const SkImageInfo& other) const {
532  return fDimensions == other.fDimensions && fColorInfo == other.fColorInfo;
533  }
534 
541  bool operator!=(const SkImageInfo& other) const {
542  return !(*this == other);
543  }
544 
554  size_t computeByteSize(size_t rowBytes) const;
555 
564  size_t computeMinByteSize() const {
565  return this->computeByteSize(this->minRowBytes());
566  }
567 
574  static bool ByteSizeOverflowed(size_t byteSize) {
575  return SIZE_MAX == byteSize;
576  }
577 
584  bool validRowBytes(size_t rowBytes) const {
585  if (rowBytes < this->minRowBytes64()) {
586  return false;
587  }
588  int shift = this->shiftPerPixel();
589  size_t alignedRowBytes = rowBytes >> shift << shift;
590  return alignedRowBytes == rowBytes;
591  }
592 
596  void reset() { *this = {}; }
597 
601  SkDEBUGCODE(void validate() const;)
602 
603 private:
604  SkColorInfo fColorInfo;
605  SkISize fDimensions = {0, 0};
606 
607  SkImageInfo(SkISize dimensions, const SkColorInfo& colorInfo)
608  : fColorInfo(colorInfo), fDimensions(dimensions) {}
609 
610  SkImageInfo(SkISize dimensions, SkColorInfo&& colorInfo)
611  : fColorInfo(std::move(colorInfo)), fDimensions(dimensions) {}
612 };
613 
614 #endif
static bool SkAlphaTypeIsOpaque(SkAlphaType at)
Returns true if SkAlphaType equals kOpaque_SkAlphaType.
Definition: SkAlphaType.h:41
SkAlphaType
Describes how to interpret the alpha component of a pixel.
Definition: SkAlphaType.h:26
@ kUnknown_SkAlphaType
uninitialized
Definition: SkAlphaType.h:27
SkColorType
Describes how pixel bits encode color.
Definition: SkColorType.h:19
@ kUnknown_SkColorType
uninitialized
Definition: SkColorType.h:20
SK_API int SkColorTypeBytesPerPixel(SkColorType ct)
Returns the number of bytes required to store a pixel, including unused padding.
SkYUVColorSpace
Definition: SkImageInfo.h:68
@ kRec709_Full_SkYUVColorSpace
describes HDTV range
Definition: SkImageInfo.h:71
@ kBT2020_8bit_Limited_SkYUVColorSpace
Definition: SkImageInfo.h:74
@ kBT2020_SkYUVColorSpace
Definition: SkImageInfo.h:87
@ kJPEG_SkYUVColorSpace
Definition: SkImageInfo.h:84
@ kLastEnum_SkYUVColorSpace
last valid value
Definition: SkImageInfo.h:81
@ kRec601_SkYUVColorSpace
Definition: SkImageInfo.h:85
@ kBT2020_12bit_Limited_SkYUVColorSpace
Definition: SkImageInfo.h:78
@ kBT2020_8bit_Full_SkYUVColorSpace
describes UHDTV range, non-constant-luminance
Definition: SkImageInfo.h:73
@ kRec601_Limited_SkYUVColorSpace
describes SDTV range
Definition: SkImageInfo.h:70
@ kRec709_Limited_SkYUVColorSpace
Definition: SkImageInfo.h:72
@ kBT2020_12bit_Full_SkYUVColorSpace
Definition: SkImageInfo.h:77
@ kBT2020_10bit_Full_SkYUVColorSpace
Definition: SkImageInfo.h:75
@ kRec709_SkYUVColorSpace
Definition: SkImageInfo.h:86
@ kIdentity_SkYUVColorSpace
maps Y->R, U->G, V->B
Definition: SkImageInfo.h:79
@ kBT2020_10bit_Limited_SkYUVColorSpace
Definition: SkImageInfo.h:76
@ kJPEG_Full_SkYUVColorSpace
describes full range
Definition: SkImageInfo.h:69
SK_API bool SkColorTypeIsAlwaysOpaque(SkColorType ct)
Returns true if SkColorType always decodes alpha to 1.0, making the pixel fully opaque.
SK_API bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType, SkAlphaType *canonical=nullptr)
Returns true if canonical can be set to a valid SkAlphaType for colorType.
Describes pixel and encoding.
Definition: SkImageInfo.h:97
SkColorInfo()
Creates an SkColorInfo with kUnknown_SkColorType, kUnknown_SkAlphaType, and no SkColorSpace.
int bytesPerPixel() const
Returns number of bytes per pixel required by SkColorType.
SkColorInfo makeAlphaType(SkAlphaType newAlphaType) const
Creates SkColorInfo with same SkColorType, SkColorSpace, with SkAlphaType set to newAlphaType.
SkColorInfo(SkColorType ct, SkAlphaType at, sk_sp< SkColorSpace > cs)
Creates SkColorInfo from SkColorType ct, SkAlphaType at, and optionally SkColorSpace cs.
SkAlphaType alphaType() const
Definition: SkImageInfo.h:127
SkColorInfo(const SkColorInfo &)
bool isOpaque() const
Definition: SkImageInfo.h:129
bool operator!=(const SkColorInfo &other) const
Does other represent a different color type, alpha type, or color space?
sk_sp< SkColorSpace > refColorSpace() const
SkColorInfo makeColorSpace(sk_sp< SkColorSpace > cs) const
Creates SkColorInfo with same SkAlphaType, SkColorType, with SkColorSpace set to cs.
int shiftPerPixel() const
Returns bit shift converting row bytes to row pixels.
SkColorInfo makeColorType(SkColorType newColorType) const
Creates new SkColorInfo with same SkAlphaType, SkColorSpace, with SkColorType set to newColorType.
bool gammaCloseToSRGB() const
SkColorInfo & operator=(const SkColorInfo &)
SkColorInfo & operator=(SkColorInfo &&)
SkColorInfo(SkColorInfo &&)
bool operator==(const SkColorInfo &other) const
Does other represent the same color type, alpha type, and color space?
SkColorSpace * colorSpace() const
SkColorType colorType() const
Definition: SkImageInfo.h:126
Definition: SkColorSpace.h:107
sk_sp< SkDrawLooper > SK_API Make(SkColor4f color, SkColorSpace *cs, SkScalar sigma, SkScalar dx, SkScalar dy)
SkIRect holds four 32-bit integer coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:32
static constexpr SkIRect MakeSize(const SkISize &size)
Returns constructed SkIRect set to (0, 0, size.width(), size.height()).
Definition: SkRect.h:66
Definition: SkSize.h:15
Describes pixel dimensions and encoding.
Definition: SkImageInfo.h:194
SkImageInfo makeWH(int newWidth, int newHeight) const
Creates SkImageInfo with the same SkColorType, SkColorSpace, and SkAlphaType, with dimensions set to ...
Definition: SkImageInfo.h:430
static bool ByteSizeOverflowed(size_t byteSize)
Returns true if byteSize equals SIZE_MAX.
Definition: SkImageInfo.h:574
static SkImageInfo MakeA8(SkISize dimensions)
Creates SkImageInfo from integral dimensions, kAlpha_8_SkColorType, kPremul_SkAlphaType,...
static SkImageInfo Make(SkISize dimensions, SkColorType ct, SkAlphaType at)
bool isEmpty() const
Returns if SkImageInfo describes an empty area of pixels by checking if either width or height is zer...
Definition: SkImageInfo.h:385
bool isOpaque() const
Returns true if SkAlphaType is set to hint that all pixels are opaque; their alpha value is implicitl...
Definition: SkImageInfo.h:401
bool operator!=(const SkImageInfo &other) const
Compares SkImageInfo with other, and returns true if width, height, SkColorType, SkAlphaType,...
Definition: SkImageInfo.h:541
SkImageInfo(SkISize dimensions, SkColorInfo &&colorInfo)
Definition: SkImageInfo.h:610
static SkImageInfo Make(SkISize dimensions, SkColorType ct, SkAlphaType at, sk_sp< SkColorSpace > cs)
static SkImageInfo Make(SkISize dimensions, SkColorInfo &&colorInfo)
Definition: SkImageInfo.h:238
SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const
Creates SkImageInfo with same SkColorType, SkColorSpace, width, and height, with SkAlphaType set to n...
Definition: SkImageInfo.h:452
bool operator==(const SkImageInfo &other) const
Compares SkImageInfo with other, and returns true if width, height, SkColorType, SkAlphaType,...
Definition: SkImageInfo.h:531
static SkImageInfo MakeN32Premul(SkISize dimensions, sk_sp< SkColorSpace > cs)
SkIRect bounds() const
Returns SkIRect { 0, 0, width(), height() }.
Definition: SkImageInfo.h:413
SkColorSpace * colorSpace() const
Returns SkColorSpace, the range of colors.
void reset()
Creates an empty SkImageInfo with kUnknown_SkColorType, kUnknown_SkAlphaType, a width and height of z...
Definition: SkImageInfo.h:596
const SkColorInfo & colorInfo() const
Returns the dimensionless SkColorInfo that represents the same color type, alpha type,...
Definition: SkImageInfo.h:390
size_t minRowBytes() const
Returns minimum bytes per row, computed from pixel width() and SkColorType, which specifies bytesPerP...
Definition: SkImageInfo.h:503
static SkImageInfo Make(SkISize dimensions, const SkColorInfo &colorInfo)
Creates SkImageInfo from integral dimensions and SkColorInfo colorInfo,.
Definition: SkImageInfo.h:235
static SkImageInfo MakeS32(int width, int height, SkAlphaType at)
Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType, SkAlphaType at,...
size_t computeByteSize(size_t rowBytes) const
Returns storage required by pixel array, given SkImageInfo dimensions, SkColorType,...
static SkImageInfo MakeN32Premul(int width, int height)
Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType, kPremul_SkAlphaType,...
SkImageInfo makeDimensions(SkISize newSize) const
Creates SkImageInfo with the same SkColorType, SkColorSpace, and SkAlphaType, with dimensions set to ...
Definition: SkImageInfo.h:440
static SkImageInfo MakeUnknown(int width, int height)
Creates SkImageInfo from integral dimensions width and height, kUnknown_SkColorType,...
sk_sp< SkColorSpace > refColorSpace() const
Returns smart pointer to SkColorSpace, the range of colors.
int bytesPerPixel() const
Returns number of bytes per pixel required by SkColorType.
Definition: SkImageInfo.h:478
SkISize dimensions() const
Returns SkISize { width(), height() }.
Definition: SkImageInfo.h:407
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
Creates SkImageInfo from integral dimensions width and height, SkColorType ct, SkAlphaType at,...
SkDEBUGCODE(void validate() const ;) private SkISize fDimensions
Asserts if internal values are illegal or inconsistent.
Definition: SkImageInfo.h:605
bool gammaCloseToSRGB() const
Returns true if associated SkColorSpace is not nullptr, and SkColorSpace gamma is approximately the s...
Definition: SkImageInfo.h:421
SkImageInfo makeColorSpace(sk_sp< SkColorSpace > cs) const
Creates SkImageInfo with same SkAlphaType, SkColorType, width, and height, with SkColorSpace set to c...
size_t computeMinByteSize() const
Returns storage required by pixel array, given SkImageInfo dimensions, and SkColorType.
Definition: SkImageInfo.h:564
static SkImageInfo MakeUnknown()
Creates SkImageInfo from integral dimensions width and height set to zero, kUnknown_SkColorType,...
Definition: SkImageInfo.h:343
static SkImageInfo MakeN32Premul(SkISize dimensions)
Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType, kPremul_SkAlphaType,...
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at, sk_sp< SkColorSpace > cs)
int width() const
Returns pixel count in each row.
Definition: SkImageInfo.h:351
static SkImageInfo MakeN32(int width, int height, SkAlphaType at)
Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType, SkAlphaType at,...
SkImageInfo(SkISize dimensions, const SkColorInfo &colorInfo)
Definition: SkImageInfo.h:607
SkAlphaType alphaType() const
Definition: SkImageInfo.h:361
SkColorType colorType() const
Definition: SkImageInfo.h:359
static SkImageInfo MakeA8(int width, int height)
Creates SkImageInfo from integral dimensions width and height, kAlpha_8_SkColorType,...
static SkImageInfo MakeN32(int width, int height, SkAlphaType at, sk_sp< SkColorSpace > cs)
uint64_t minRowBytes64() const
Returns minimum bytes per row, computed from pixel width() and SkColorType, which specifies bytesPerP...
Definition: SkImageInfo.h:493
int shiftPerPixel() const
Returns bit shift converting row bytes to row pixels.
Definition: SkImageInfo.h:485
int height() const
Returns pixel row count.
Definition: SkImageInfo.h:357
static SkImageInfo MakeN32Premul(int width, int height, sk_sp< SkColorSpace > cs)
size_t computeOffset(int x, int y, size_t rowBytes) const
Returns byte offset of pixel from pixel base address.
SkImageInfo()=default
Creates an empty SkImageInfo with kUnknown_SkColorType, kUnknown_SkAlphaType, a width and height of z...
SkImageInfo makeColorType(SkColorType newColorType) const
Creates SkImageInfo with same SkAlphaType, SkColorSpace, width, and height, with SkColorType set to n...
Definition: SkImageInfo.h:461
bool validRowBytes(size_t rowBytes) const
Returns true if rowBytes is valid for this SkImageInfo.
Definition: SkImageInfo.h:584