SkImageInfo.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. /* Generated by tools/bookmaker from include/core/SkImageInfo.h and docs/SkImageInfo_Reference.bmh
  8. on 2018-07-13 08:15:11. Additional documentation and examples can be found at:
  9. https://skia.org/user/api/SkImageInfo_Reference
  10. You may edit either file directly. Structural changes to public interfaces require
  11. editing both files. After editing docs/SkImageInfo_Reference.bmh, run:
  12. bookmaker -b docs -i include/core/SkImageInfo.h -p
  13. to create an updated version of this file.
  14. */
  15. #ifndef SkImageInfo_DEFINED
  16. #define SkImageInfo_DEFINED
  17. #include "SkColorSpace.h"
  18. #include "SkMath.h"
  19. #include "SkRect.h"
  20. #include "SkSize.h"
  21. #include "../private/SkTFitsIn.h"
  22. #include "../private/SkTo.h"
  23. class SkReadBuffer;
  24. class SkWriteBuffer;
  25. /** \enum SkImageInfo::SkAlphaType
  26. Describes how to interpret the alpha component of a pixel. A pixel may
  27. be opaque, or alpha, describing multiple levels of transparency.
  28. In simple blending, alpha weights the draw color and the destination
  29. color to create a new color. If alpha describes a weight from zero to one:
  30. new color = draw color * alpha + destination color * (1 - alpha)
  31. In practice alpha is encoded in two or more bits, where 1.0 equals all bits set.
  32. RGB may have alpha included in each component value; the stored
  33. value is the original RGB multiplied by alpha. Premultiplied color
  34. components improve performance.
  35. */
  36. enum SkAlphaType {
  37. kUnknown_SkAlphaType, //!< uninitialized
  38. kOpaque_SkAlphaType, //!< pixel is opaque
  39. kPremul_SkAlphaType, //!< pixel components are premultiplied by alpha
  40. kUnpremul_SkAlphaType, //!< pixel components are independent of alpha
  41. kLastEnum_SkAlphaType = kUnpremul_SkAlphaType, //!< last valid value
  42. };
  43. /** Returns true if SkAlphaType equals kOpaque_SkAlphaType. kOpaque_SkAlphaType is a
  44. hint that the SkColorType is opaque, or that all alpha values are set to
  45. their 1.0 equivalent. If SkAlphaType is kOpaque_SkAlphaType, and SkColorType is not
  46. opaque, then the result of drawing any pixel with a alpha value less than
  47. 1.0 is undefined.
  48. @param at one of:
  49. kUnknown_SkAlphaType, kOpaque_SkAlphaType, kPremul_SkAlphaType,
  50. kUnpremul_SkAlphaType
  51. @return true if at equals kOpaque_SkAlphaType
  52. */
  53. static inline bool SkAlphaTypeIsOpaque(SkAlphaType at) {
  54. return kOpaque_SkAlphaType == at;
  55. }
  56. ///////////////////////////////////////////////////////////////////////////////
  57. /** Temporary macro that allows us to add new color types without breaking Chrome compile. */
  58. #define SK_EXTENDED_COLOR_TYPES
  59. /** \enum SkImageInfo::SkColorType
  60. Describes how pixel bits encode color. A pixel may be an alpha mask, a
  61. grayscale, RGB, or ARGB.
  62. kN32_SkColorType selects the native 32-bit ARGB format. On little endian
  63. processors, pixels containing 8-bit ARGB components pack into 32-bit
  64. kBGRA_8888_SkColorType. On big endian processors, pixels pack into 32-bit
  65. kRGBA_8888_SkColorType.
  66. */
  67. enum SkColorType {
  68. kUnknown_SkColorType, //!< uninitialized
  69. kAlpha_8_SkColorType, //!< pixel with alpha in 8-bit byte
  70. kRGB_565_SkColorType, //!< pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
  71. kARGB_4444_SkColorType, //!< pixel with 4 bits for alpha, red, green, blue; in 16-bit word
  72. kRGBA_8888_SkColorType, //!< pixel with 8 bits for red, green, blue, alpha; in 32-bit word
  73. kRGB_888x_SkColorType, //!< pixel with 8 bits each for red, green, blue; in 32-bit word
  74. kBGRA_8888_SkColorType, //!< pixel with 8 bits for blue, green, red, alpha; in 32-bit word
  75. kRGBA_1010102_SkColorType, //!< 10 bits for red, green, blue; 2 bits for alpha; in 32-bit word
  76. kRGB_101010x_SkColorType, //!< pixel with 10 bits each for red, green, blue; in 32-bit word
  77. kGray_8_SkColorType, //!< pixel with grayscale level in 8-bit byte
  78. kRGBA_F16_SkColorType, //!< pixel with half floats for red, green, blue, alpha; in 64-bit word
  79. kRGBA_F32_SkColorType, //!< pixel using C float for red, green, blue, alpha; in 128-bit word
  80. kLastEnum_SkColorType = kRGBA_F32_SkColorType,//!< last valid value
  81. #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
  82. kN32_SkColorType = kBGRA_8888_SkColorType,//!< native ARGB 32-bit encoding
  83. #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
  84. kN32_SkColorType = kRGBA_8888_SkColorType,//!< native ARGB 32-bit encoding
  85. #else
  86. #error "SK_*32_SHIFT values must correspond to BGRA or RGBA byte order"
  87. #endif
  88. };
  89. /** Returns the number of bytes required to store a pixel, including unused padding.
  90. Returns zero if ct is kUnknown_SkColorType or invalid.
  91. @param ct one of:
  92. kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
  93. kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType,
  94. kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType,
  95. kGray_8_SkColorType, kRGBA_F16_SkColorType
  96. @return bytes per pixel
  97. */
  98. SK_API int SkColorTypeBytesPerPixel(SkColorType ct);
  99. /** Returns true if SkColorType always decodes alpha to 1.0, making the pixel
  100. fully opaque. If true, SkColorType does not reserve bits to encode alpha.
  101. @param ct one of:
  102. kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
  103. kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType,
  104. kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType,
  105. kGray_8_SkColorType, kRGBA_F16_SkColorType
  106. @return true if alpha is always set to 1.0
  107. */
  108. SK_API bool SkColorTypeIsAlwaysOpaque(SkColorType ct);
  109. /** Returns true if canonical can be set to a valid SkAlphaType for colorType. If
  110. there is more than one valid canonical SkAlphaType, set to alphaType, if valid.
  111. If true is returned and canonical is not nullptr, store valid SkAlphaType.
  112. Returns false only if alphaType is kUnknown_SkAlphaType, color type is not
  113. kUnknown_SkColorType, and SkColorType is not always opaque. If false is returned,
  114. canonical is ignored.
  115. For kUnknown_SkColorType: set canonical to kUnknown_SkAlphaType and return true.
  116. For kAlpha_8_SkColorType: set canonical to kPremul_SkAlphaType or
  117. kOpaque_SkAlphaType and return true if alphaType is not kUnknown_SkAlphaType.
  118. For kRGB_565_SkColorType, kRGB_888x_SkColorType, kRGB_101010x_SkColorType, and
  119. kGray_8_SkColorType: set canonical to kOpaque_SkAlphaType and return true.
  120. For kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
  121. kRGBA_1010102_SkColorType, and kRGBA_F16_SkColorType: set canonical to alphaType
  122. and return true if alphaType is not kUnknown_SkAlphaType.
  123. @param colorType one of:
  124. kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
  125. kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType,
  126. kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType,
  127. kGray_8_SkColorType, kRGBA_F16_SkColorType
  128. @param alphaType one of:
  129. kUnknown_SkAlphaType, kOpaque_SkAlphaType, kPremul_SkAlphaType,
  130. kUnpremul_SkAlphaType
  131. @param canonical storage for SkAlphaType
  132. @return true if valid SkAlphaType can be associated with colorType
  133. */
  134. SK_API bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
  135. SkAlphaType* canonical = nullptr);
  136. /** \enum SkImageInfo::SkYUVColorSpace
  137. Describes color range of YUV pixels. The color mapping from YUV to RGB varies
  138. depending on the source. YUV pixels may be generated by JPEG images, standard
  139. video streams, or high definition video streams. Each has its own mapping from
  140. YUV and RGB.
  141. JPEG YUV values encode the full range of 0 to 255 for all three components.
  142. Video YUV values range from 16 to 235 for all three components. Details of
  143. encoding and conversion to RGB are described in YCbCr color space.
  144. */
  145. enum SkYUVColorSpace {
  146. kJPEG_SkYUVColorSpace, //!< describes full range
  147. kRec601_SkYUVColorSpace, //!< describes SDTV range
  148. kRec709_SkYUVColorSpace, //!< describes HDTV range
  149. kLastEnum_SkYUVColorSpace = kRec709_SkYUVColorSpace, //!< last valid value
  150. };
  151. /** \struct SkImageInfo
  152. Describes pixel dimensions and encoding. SkBitmap, SkImage, PixMap, and SkSurface
  153. can be created from SkImageInfo. SkImageInfo can be retrieved from SkBitmap and
  154. SkPixmap, but not from SkImage and SkSurface. For example, SkImage and SkSurface
  155. implementations may defer pixel depth, so may not completely specify SkImageInfo.
  156. SkImageInfo contains dimensions, the pixel integral width and height. It encodes
  157. how pixel bits describe alpha, transparency; color components red, blue,
  158. and green; and SkColorSpace, the range and linearity of colors.
  159. */
  160. struct SK_API SkImageInfo {
  161. public:
  162. /** Creates an empty SkImageInfo with kUnknown_SkColorType, kUnknown_SkAlphaType,
  163. a width and height of zero, and no SkColorSpace.
  164. @return empty SkImageInfo
  165. */
  166. SkImageInfo()
  167. : fColorSpace(nullptr)
  168. , fDimensions{0, 0}
  169. , fColorType(kUnknown_SkColorType)
  170. , fAlphaType(kUnknown_SkAlphaType)
  171. {}
  172. /** Creates SkImageInfo from integral dimensions width and height, SkColorType ct,
  173. SkAlphaType at, and optionally SkColorSpace cs.
  174. If SkColorSpace cs is nullptr and SkImageInfo is part of drawing source: SkColorSpace
  175. defaults to sRGB, mapping into SkSurface SkColorSpace.
  176. Parameters are not validated to see if their values are legal, or that the
  177. combination is supported.
  178. @param width pixel column count; must be zero or greater
  179. @param height pixel row count; must be zero or greater
  180. @param ct one of:
  181. kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
  182. kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType,
  183. kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType,
  184. kGray_8_SkColorType, kRGBA_F16_SkColorType
  185. @param at one of:
  186. kUnknown_SkAlphaType, kOpaque_SkAlphaType, kPremul_SkAlphaType,
  187. kUnpremul_SkAlphaType
  188. @param cs range of colors; may be nullptr
  189. @return created SkImageInfo
  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. /** Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType,
  196. SkAlphaType at, and optionally SkColorSpace cs. kN32_SkColorType will equal either
  197. kBGRA_8888_SkColorType or kRGBA_8888_SkColorType, whichever is optimal.
  198. If SkColorSpace cs is nullptr and SkImageInfo is part of drawing source: SkColorSpace
  199. defaults to sRGB, mapping into SkSurface SkColorSpace.
  200. Parameters are not validated to see if their values are legal, or that the
  201. combination is supported.
  202. @param width pixel column count; must be zero or greater
  203. @param height pixel row count; must be zero or greater
  204. @param at one of:
  205. kUnknown_SkAlphaType, kOpaque_SkAlphaType, kPremul_SkAlphaType,
  206. kUnpremul_SkAlphaType
  207. @param cs range of colors; may be nullptr
  208. @return created SkImageInfo
  209. */
  210. static SkImageInfo MakeN32(int width, int height, SkAlphaType at,
  211. sk_sp<SkColorSpace> cs = nullptr) {
  212. return Make(width, height, kN32_SkColorType, at, std::move(cs));
  213. }
  214. /** Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType,
  215. SkAlphaType at, with sRGB SkColorSpace.
  216. Parameters are not validated to see if their values are legal, or that the
  217. combination is supported.
  218. @param width pixel column count; must be zero or greater
  219. @param height pixel row count; must be zero or greater
  220. @param at one of:
  221. kUnknown_SkAlphaType, kOpaque_SkAlphaType, kPremul_SkAlphaType,
  222. kUnpremul_SkAlphaType
  223. @return created SkImageInfo
  224. */
  225. static SkImageInfo MakeS32(int width, int height, SkAlphaType at);
  226. /** Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType,
  227. kPremul_SkAlphaType, with optional SkColorSpace.
  228. If SkColorSpace cs is nullptr and SkImageInfo is part of drawing source: SkColorSpace
  229. defaults to sRGB, mapping into SkSurface SkColorSpace.
  230. Parameters are not validated to see if their values are legal, or that the
  231. combination is supported.
  232. @param width pixel column count; must be zero or greater
  233. @param height pixel row count; must be zero or greater
  234. @param cs range of colors; may be nullptr
  235. @return created SkImageInfo
  236. */
  237. static SkImageInfo MakeN32Premul(int width, int height, sk_sp<SkColorSpace> cs = nullptr) {
  238. return Make(width, height, kN32_SkColorType, kPremul_SkAlphaType, std::move(cs));
  239. }
  240. /** Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType,
  241. kPremul_SkAlphaType, with SkColorSpace set to nullptr.
  242. If SkImageInfo is part of drawing source: SkColorSpace defaults to sRGB, mapping
  243. into SkSurface SkColorSpace.
  244. Parameters are not validated to see if their values are legal, or that the
  245. combination is supported.
  246. @param size width and height, each must be zero or greater
  247. @return created SkImageInfo
  248. */
  249. static SkImageInfo MakeN32Premul(const SkISize& size) {
  250. return MakeN32Premul(size.width(), size.height());
  251. }
  252. /** Creates SkImageInfo from integral dimensions width and height, kAlpha_8_SkColorType,
  253. kPremul_SkAlphaType, with SkColorSpace set to nullptr.
  254. @param width pixel column count; must be zero or greater
  255. @param height pixel row count; must be zero or greater
  256. @return created SkImageInfo
  257. */
  258. static SkImageInfo MakeA8(int width, int height) {
  259. return Make(width, height, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr);
  260. }
  261. /** Creates SkImageInfo from integral dimensions width and height, kUnknown_SkColorType,
  262. kUnknown_SkAlphaType, with SkColorSpace set to nullptr.
  263. Returned SkImageInfo as part of source does not draw, and as part of destination
  264. can not be drawn to.
  265. @param width pixel column count; must be zero or greater
  266. @param height pixel row count; must be zero or greater
  267. @return created SkImageInfo
  268. */
  269. static SkImageInfo MakeUnknown(int width, int height) {
  270. return Make(width, height, kUnknown_SkColorType, kUnknown_SkAlphaType, nullptr);
  271. }
  272. /** Creates SkImageInfo from integral dimensions width and height set to zero,
  273. kUnknown_SkColorType, kUnknown_SkAlphaType, with SkColorSpace set to nullptr.
  274. Returned SkImageInfo as part of source does not draw, and as part of destination
  275. can not be drawn to.
  276. @return created SkImageInfo
  277. */
  278. static SkImageInfo MakeUnknown() {
  279. return MakeUnknown(0, 0);
  280. }
  281. /** Returns pixel count in each row.
  282. @return pixel width
  283. */
  284. int width() const { return fDimensions.width(); }
  285. /** Returns pixel row count.
  286. @return pixel height
  287. */
  288. int height() const { return fDimensions.height(); }
  289. /** Returns SkColorType, one of:
  290. kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
  291. kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType,
  292. kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType,
  293. kGray_8_SkColorType, kRGBA_F16_SkColorType.
  294. @return SkColorType
  295. */
  296. SkColorType colorType() const { return fColorType; }
  297. /** Returns SkAlphaType, one of:
  298. kUnknown_SkAlphaType, kOpaque_SkAlphaType, kPremul_SkAlphaType,
  299. kUnpremul_SkAlphaType.
  300. @return SkAlphaType
  301. */
  302. SkAlphaType alphaType() const { return fAlphaType; }
  303. /** Returns SkColorSpace, the range of colors. The reference count of
  304. SkColorSpace is unchanged. The returned SkColorSpace is immutable.
  305. @return SkColorSpace, or nullptr
  306. */
  307. SkColorSpace* colorSpace() const { return fColorSpace.get(); }
  308. /** Returns smart pointer to SkColorSpace, the range of colors. The smart pointer
  309. tracks the number of objects sharing this SkColorSpace reference so the memory
  310. is released when the owners destruct.
  311. The returned SkColorSpace is immutable.
  312. @return SkColorSpace wrapped in a smart pointer
  313. */
  314. sk_sp<SkColorSpace> refColorSpace() const { return fColorSpace; }
  315. /** Returns if SkImageInfo describes an empty area of pixels by checking if either
  316. width or height is zero or smaller.
  317. @return true if either dimension is zero or smaller
  318. */
  319. bool isEmpty() const { return fDimensions.isEmpty(); }
  320. /** Returns true if SkAlphaType is set to hint that all pixels are opaque; their
  321. alpha value is implicitly or explicitly 1.0. If true, and all pixels are
  322. not opaque, Skia may draw incorrectly.
  323. Does not check if SkColorType allows alpha, or if any pixel value has
  324. transparency.
  325. @return true if SkAlphaType is kOpaque_SkAlphaType
  326. */
  327. bool isOpaque() const {
  328. return SkAlphaTypeIsOpaque(fAlphaType);
  329. }
  330. /** Returns SkISize { width(), height() }.
  331. @return integral size of width() and height()
  332. */
  333. SkISize dimensions() const { return fDimensions; }
  334. /** Returns SkIRect { 0, 0, width(), height() }.
  335. @return integral rectangle from origin to width() and height()
  336. */
  337. SkIRect bounds() const { return SkIRect::MakeSize(fDimensions); }
  338. /** Returns true if associated SkColorSpace is not nullptr, and SkColorSpace gamma
  339. is approximately the same as sRGB.
  340. This includes the
  341. @return true if SkColorSpace gamma is approximately the same as sRGB
  342. */
  343. bool gammaCloseToSRGB() const {
  344. return fColorSpace && fColorSpace->gammaCloseToSRGB();
  345. }
  346. /** Creates SkImageInfo with the same SkColorType, SkColorSpace, and SkAlphaType,
  347. with dimensions set to width and height.
  348. @param newWidth pixel column count; must be zero or greater
  349. @param newHeight pixel row count; must be zero or greater
  350. @return created SkImageInfo
  351. */
  352. SkImageInfo makeWH(int newWidth, int newHeight) const {
  353. return Make(newWidth, newHeight, fColorType, fAlphaType, fColorSpace);
  354. }
  355. /** Creates SkImageInfo with same SkColorType, SkColorSpace, width, and height,
  356. with SkAlphaType set to newAlphaType.
  357. Created SkImageInfo contains newAlphaType even if it is incompatible with
  358. SkColorType, in which case SkAlphaType in SkImageInfo is ignored.
  359. @param newAlphaType one of:
  360. kUnknown_SkAlphaType, kOpaque_SkAlphaType, kPremul_SkAlphaType,
  361. kUnpremul_SkAlphaType
  362. @return created SkImageInfo
  363. */
  364. SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const {
  365. return Make(this->width(), this->height(), fColorType, newAlphaType, fColorSpace);
  366. }
  367. /** Creates SkImageInfo with same SkAlphaType, SkColorSpace, width, and height,
  368. with SkColorType set to newColorType.
  369. @param newColorType one of:
  370. kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
  371. kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType,
  372. kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType,
  373. kRGB_101010x_SkColorType, kGray_8_SkColorType, kRGBA_F16_SkColorType
  374. @return created SkImageInfo
  375. */
  376. SkImageInfo makeColorType(SkColorType newColorType) const {
  377. return Make(this->width(), this->height(), newColorType, fAlphaType, fColorSpace);
  378. }
  379. /** Creates SkImageInfo with same SkAlphaType, SkColorType, width, and height,
  380. with SkColorSpace set to cs.
  381. @param cs range of colors; may be nullptr
  382. @return created SkImageInfo
  383. */
  384. SkImageInfo makeColorSpace(sk_sp<SkColorSpace> cs) const {
  385. return Make(this->width(), this->height(), fColorType, fAlphaType, std::move(cs));
  386. }
  387. /** Returns number of bytes per pixel required by SkColorType.
  388. Returns zero if colorType( is kUnknown_SkColorType.
  389. @return bytes in pixel
  390. */
  391. int bytesPerPixel() const;
  392. /** Returns bit shift converting row bytes to row pixels.
  393. Returns zero for kUnknown_SkColorType.
  394. @return one of: 0, 1, 2, 3; left shift to convert pixels to bytes
  395. */
  396. int shiftPerPixel() const;
  397. /** Returns minimum bytes per row, computed from pixel width() and SkColorType, which
  398. specifies bytesPerPixel(). SkBitmap maximum value for row bytes must fit
  399. in 31 bits.
  400. @return width() times bytesPerPixel() as unsigned 64-bit integer
  401. */
  402. uint64_t minRowBytes64() const { return sk_64_mul(this->width(), this->bytesPerPixel()); }
  403. /** Returns minimum bytes per row, computed from pixel width() and SkColorType, which
  404. specifies bytesPerPixel(). SkBitmap maximum value for row bytes must fit
  405. in 31 bits.
  406. @return width() times bytesPerPixel() as signed 32-bit integer
  407. */
  408. size_t minRowBytes() const {
  409. uint64_t minRowBytes = this->minRowBytes64();
  410. if (!SkTFitsIn<int32_t>(minRowBytes)) {
  411. return 0;
  412. }
  413. return SkTo<int32_t>(minRowBytes);
  414. }
  415. /** Returns byte offset of pixel from pixel base address.
  416. Asserts in debug build if x or y is outside of bounds. Does not assert if
  417. rowBytes is smaller than minRowBytes(), even though result may be incorrect.
  418. @param x column index, zero or greater, and less than width()
  419. @param y row index, zero or greater, and less than height()
  420. @param rowBytes size of pixel row or larger
  421. @return offset within pixel array
  422. */
  423. size_t computeOffset(int x, int y, size_t rowBytes) const;
  424. /** Compares SkImageInfo with other, and returns true if width, height, SkColorType,
  425. SkAlphaType, and SkColorSpace are equivalent.
  426. @param other SkImageInfo to compare
  427. @return true if SkImageInfo equals other
  428. */
  429. bool operator==(const SkImageInfo& other) const {
  430. return fDimensions == other.fDimensions &&
  431. fColorType == other.fColorType && fAlphaType == other.fAlphaType &&
  432. SkColorSpace::Equals(fColorSpace.get(), other.fColorSpace.get());
  433. }
  434. /** Compares SkImageInfo with other, and returns true if width, height, SkColorType,
  435. SkAlphaType, and SkColorSpace are not equivalent.
  436. @param other SkImageInfo to compare
  437. @return true if SkImageInfo is not equal to other
  438. */
  439. bool operator!=(const SkImageInfo& other) const {
  440. return !(*this == other);
  441. }
  442. /** Returns storage required by pixel array, given SkImageInfo dimensions, SkColorType,
  443. and rowBytes. rowBytes is assumed to be at least as large as minRowBytes().
  444. Returns zero if height is zero.
  445. Returns SIZE_MAX if answer exceeds the range of size_t.
  446. @param rowBytes size of pixel row or larger
  447. @return memory required by pixel buffer
  448. */
  449. size_t computeByteSize(size_t rowBytes) const;
  450. /** Returns storage required by pixel array, given SkImageInfo dimensions, and
  451. SkColorType. Uses minRowBytes() to compute bytes for pixel row.
  452. Returns zero if height is zero.
  453. Returns SIZE_MAX if answer exceeds the range of size_t.
  454. @return least memory required by pixel buffer
  455. */
  456. size_t computeMinByteSize() const {
  457. return this->computeByteSize(this->minRowBytes());
  458. }
  459. /** Returns true if byteSize equals SIZE_MAX. computeByteSize() and
  460. computeMinByteSize() return SIZE_MAX if size_t can not hold buffer size.
  461. @param byteSize result of computeByteSize() or computeMinByteSize()
  462. @return true if computeByteSize() or computeMinByteSize() result exceeds size_t
  463. */
  464. static bool ByteSizeOverflowed(size_t byteSize) {
  465. return SIZE_MAX == byteSize;
  466. }
  467. /** Returns true if rowBytes is smaller than width times pixel size.
  468. @param rowBytes size of pixel row or larger
  469. @return true if rowBytes is large enough to contain pixel row
  470. */
  471. bool validRowBytes(size_t rowBytes) const {
  472. return rowBytes >= this->minRowBytes64();
  473. }
  474. /** Creates an empty SkImageInfo with kUnknown_SkColorType, kUnknown_SkAlphaType,
  475. a width and height of zero, and no SkColorSpace.
  476. */
  477. void reset() {
  478. fColorSpace = nullptr;
  479. fDimensions = {0, 0};
  480. fColorType = kUnknown_SkColorType;
  481. fAlphaType = kUnknown_SkAlphaType;
  482. }
  483. /** Asserts if internal values are illegal or inconsistent. Only available if
  484. SK_DEBUG is defined at compile time.
  485. */
  486. SkDEBUGCODE(void validate() const;)
  487. private:
  488. sk_sp<SkColorSpace> fColorSpace;
  489. SkISize fDimensions;
  490. SkColorType fColorType;
  491. SkAlphaType fAlphaType;
  492. SkImageInfo(int width, int height, SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs)
  493. : fColorSpace(std::move(cs))
  494. , fDimensions{width, height}
  495. , fColorType(ct)
  496. , fAlphaType(at)
  497. {}
  498. };
  499. #endif