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