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_F16Norm_SkColorType, //!< pixel with half floats in [0,1] for red, green, blue, alpha; in 64-bit word
96     kRGBA_F16_SkColorType,     //!< pixel with half floats for red, green, blue, alpha; in 64-bit word
97     kRGBA_F32_SkColorType,     //!< pixel using C float for red, green, blue, alpha; in 128-bit word
98     kLastEnum_SkColorType     = kRGBA_F32_SkColorType,//!< last valid value
99 
100 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
101     kN32_SkColorType          = kBGRA_8888_SkColorType,//!< native ARGB 32-bit encoding
102 
103 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
104     kN32_SkColorType          = kRGBA_8888_SkColorType,//!< native ARGB 32-bit encoding
105 
106 #else
107     #error "SK_*32_SHIFT values must correspond to BGRA or RGBA byte order"
108 #endif
109 };
110 
111 /** Returns the number of bytes required to store a pixel, including unused padding.
112     Returns zero if ct is kUnknown_SkColorType or invalid.
113 
114     @param ct  one of:
115                kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
116                kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType,
117                kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType,
118                kGray_8_SkColorType, kRGBA_F16_SkColorType
119     @return    bytes per pixel
120 */
121 SK_API int SkColorTypeBytesPerPixel(SkColorType ct);
122 
123 /** Returns true if SkColorType always decodes alpha to 1.0, making the pixel
124     fully opaque. If true, SkColorType does not reserve bits to encode alpha.
125 
126     @param ct  one of:
127                kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
128                kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType,
129                kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType,
130                kGray_8_SkColorType, kRGBA_F16_SkColorType
131     @return    true if alpha is always set to 1.0
132 */
133 SK_API bool SkColorTypeIsAlwaysOpaque(SkColorType ct);
134 
135 /** Returns true if canonical can be set to a valid SkAlphaType for colorType. If
136     there is more than one valid canonical SkAlphaType, set to alphaType, if valid.
137     If true is returned and canonical is not nullptr, store valid SkAlphaType.
138 
139     Returns false only if alphaType is kUnknown_SkAlphaType, color type is not
140     kUnknown_SkColorType, and SkColorType is not always opaque. If false is returned,
141     canonical is ignored.
142 
143     For kUnknown_SkColorType: set canonical to kUnknown_SkAlphaType and return true.
144     For kAlpha_8_SkColorType: set canonical to kPremul_SkAlphaType or
145     kOpaque_SkAlphaType and return true if alphaType is not kUnknown_SkAlphaType.
146     For kRGB_565_SkColorType, kRGB_888x_SkColorType, kRGB_101010x_SkColorType, and
147     kGray_8_SkColorType: set canonical to kOpaque_SkAlphaType and return true.
148     For kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
149     kRGBA_1010102_SkColorType, and kRGBA_F16_SkColorType: set canonical to alphaType
150     and return true if alphaType is not kUnknown_SkAlphaType.
151 
152     @param colorType  one of:
153                       kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
154                       kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType,
155                       kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType,
156                       kGray_8_SkColorType, kRGBA_F16_SkColorType
157     @param alphaType  one of:
158                       kUnknown_SkAlphaType, kOpaque_SkAlphaType, kPremul_SkAlphaType,
159                       kUnpremul_SkAlphaType
160     @param canonical  storage for SkAlphaType
161     @return           true if valid SkAlphaType can be associated with colorType
162 */
163 SK_API bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
164                                          SkAlphaType* canonical = nullptr);
165 
166 /** \enum SkImageInfo::SkYUVColorSpace
167     Describes color range of YUV pixels. The color mapping from YUV to RGB varies
168     depending on the source. YUV pixels may be generated by JPEG images, standard
169     video streams, or high definition video streams. Each has its own mapping from
170     YUV and RGB.
171 
172     JPEG YUV values encode the full range of 0 to 255 for all three components.
173     Video YUV values range from 16 to 235 for all three components. Details of
174     encoding and conversion to RGB are described in YCbCr color space.
175 
176     The identity colorspace exists to provide a utility mapping from Y to R, U to G and V to B.
177     It can be used to visualize the YUV planes or to explicitly post process the YUV channels.
178 */
179 enum SkYUVColorSpace {
180     kJPEG_SkYUVColorSpace,                               //!< describes full range
181     kRec601_SkYUVColorSpace,                             //!< describes SDTV range
182     kRec709_SkYUVColorSpace,                             //!< describes HDTV range
183     kIdentity_SkYUVColorSpace,                           //!< maps Y->R, U->G, V->B
184 
185     kLastEnum_SkYUVColorSpace = kIdentity_SkYUVColorSpace, //!< last valid value
186 };
187 
188 /** \struct SkImageInfo
189     Describes pixel dimensions and encoding. SkBitmap, SkImage, PixMap, and SkSurface
190     can be created from SkImageInfo. SkImageInfo can be retrieved from SkBitmap and
191     SkPixmap, but not from SkImage and SkSurface. For example, SkImage and SkSurface
192     implementations may defer pixel depth, so may not completely specify SkImageInfo.
193 
194     SkImageInfo contains dimensions, the pixel integral width and height. It encodes
195     how pixel bits describe alpha, transparency; color components red, blue,
196     and green; and SkColorSpace, the range and linearity of colors.
197 */
198 struct SK_API SkImageInfo {
199 public:
200 
201     /** Creates an empty SkImageInfo with kUnknown_SkColorType, kUnknown_SkAlphaType,
202         a width and height of zero, and no SkColorSpace.
203 
204         @return  empty SkImageInfo
205     */
SkImageInfoSkImageInfo206     SkImageInfo()
207         : fColorSpace(nullptr)
208         , fDimensions{0, 0}
209         , fColorType(kUnknown_SkColorType)
210         , fAlphaType(kUnknown_SkAlphaType)
211     {}
212 
213     /** Creates SkImageInfo from integral dimensions width and height, SkColorType ct,
214         SkAlphaType at, and optionally SkColorSpace cs.
215 
216         If SkColorSpace cs is nullptr and SkImageInfo is part of drawing source: SkColorSpace
217         defaults to sRGB, mapping into SkSurface SkColorSpace.
218 
219         Parameters are not validated to see if their values are legal, or that the
220         combination is supported.
221 
222         @param width   pixel column count; must be zero or greater
223         @param height  pixel row count; must be zero or greater
224         @param ct      one of:
225                        kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
226                        kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType,
227                        kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType,
228                        kGray_8_SkColorType, kRGBA_F16_SkColorType
229         @param at      one of:
230                        kUnknown_SkAlphaType, kOpaque_SkAlphaType, kPremul_SkAlphaType,
231                        kUnpremul_SkAlphaType
232         @param cs      range of colors; may be nullptr
233         @return        created SkImageInfo
234     */
235     static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at,
236                             sk_sp<SkColorSpace> cs = nullptr) {
237         return SkImageInfo(width, height, ct, at, std::move(cs));
238     }
239 
240     /** Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType,
241         SkAlphaType at, and optionally SkColorSpace cs. kN32_SkColorType will equal either
242         kBGRA_8888_SkColorType or kRGBA_8888_SkColorType, whichever is optimal.
243 
244         If SkColorSpace cs is nullptr and SkImageInfo is part of drawing source: SkColorSpace
245         defaults to sRGB, mapping into SkSurface SkColorSpace.
246 
247         Parameters are not validated to see if their values are legal, or that the
248         combination is supported.
249 
250         @param width   pixel column count; must be zero or greater
251         @param height  pixel row count; must be zero or greater
252         @param at      one of:
253                        kUnknown_SkAlphaType, kOpaque_SkAlphaType, kPremul_SkAlphaType,
254                        kUnpremul_SkAlphaType
255         @param cs      range of colors; may be nullptr
256         @return        created SkImageInfo
257     */
258     static SkImageInfo MakeN32(int width, int height, SkAlphaType at,
259                                sk_sp<SkColorSpace> cs = nullptr) {
260         return Make(width, height, kN32_SkColorType, at, std::move(cs));
261     }
262 
263     /** Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType,
264         SkAlphaType at, with sRGB SkColorSpace.
265 
266         Parameters are not validated to see if their values are legal, or that the
267         combination is supported.
268 
269         @param width   pixel column count; must be zero or greater
270         @param height  pixel row count; must be zero or greater
271         @param at      one of:
272                        kUnknown_SkAlphaType, kOpaque_SkAlphaType, kPremul_SkAlphaType,
273                        kUnpremul_SkAlphaType
274         @return        created SkImageInfo
275     */
276     static SkImageInfo MakeS32(int width, int height, SkAlphaType at);
277 
278     /** Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType,
279         kPremul_SkAlphaType, with optional SkColorSpace.
280 
281         If SkColorSpace cs is nullptr and SkImageInfo is part of drawing source: SkColorSpace
282         defaults to sRGB, mapping into SkSurface SkColorSpace.
283 
284         Parameters are not validated to see if their values are legal, or that the
285         combination is supported.
286 
287         @param width   pixel column count; must be zero or greater
288         @param height  pixel row count; must be zero or greater
289         @param cs      range of colors; may be nullptr
290         @return        created SkImageInfo
291     */
292     static SkImageInfo MakeN32Premul(int width, int height, sk_sp<SkColorSpace> cs = nullptr) {
293         return Make(width, height, kN32_SkColorType, kPremul_SkAlphaType, std::move(cs));
294     }
295 
296     /** Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType,
297         kPremul_SkAlphaType, with SkColorSpace set to nullptr.
298 
299         If SkImageInfo is part of drawing source: SkColorSpace defaults to sRGB, mapping
300         into SkSurface SkColorSpace.
301 
302         Parameters are not validated to see if their values are legal, or that the
303         combination is supported.
304 
305         @param size  width and height, each must be zero or greater
306         @return      created SkImageInfo
307     */
MakeN32PremulSkImageInfo308     static SkImageInfo MakeN32Premul(const SkISize& size) {
309         return MakeN32Premul(size.width(), size.height());
310     }
311 
312     /** Creates SkImageInfo from integral dimensions width and height, kAlpha_8_SkColorType,
313         kPremul_SkAlphaType, with SkColorSpace set to nullptr.
314 
315         @param width   pixel column count; must be zero or greater
316         @param height  pixel row count; must be zero or greater
317         @return        created SkImageInfo
318     */
MakeA8SkImageInfo319     static SkImageInfo MakeA8(int width, int height) {
320         return Make(width, height, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr);
321     }
322 
323     /** Creates SkImageInfo from integral dimensions width and height, kUnknown_SkColorType,
324         kUnknown_SkAlphaType, with SkColorSpace set to nullptr.
325 
326         Returned SkImageInfo as part of source does not draw, and as part of destination
327         can not be drawn to.
328 
329         @param width   pixel column count; must be zero or greater
330         @param height  pixel row count; must be zero or greater
331         @return        created SkImageInfo
332     */
MakeUnknownSkImageInfo333     static SkImageInfo MakeUnknown(int width, int height) {
334         return Make(width, height, kUnknown_SkColorType, kUnknown_SkAlphaType, nullptr);
335     }
336 
337     /** Creates SkImageInfo from integral dimensions width and height set to zero,
338         kUnknown_SkColorType, kUnknown_SkAlphaType, with SkColorSpace set to nullptr.
339 
340         Returned SkImageInfo as part of source does not draw, and as part of destination
341         can not be drawn to.
342 
343         @return  created SkImageInfo
344     */
MakeUnknownSkImageInfo345     static SkImageInfo MakeUnknown() {
346         return MakeUnknown(0, 0);
347     }
348 
349     /** Returns pixel count in each row.
350 
351         @return  pixel width
352     */
widthSkImageInfo353     int width() const { return fDimensions.width(); }
354 
355     /** Returns pixel row count.
356 
357         @return  pixel height
358     */
heightSkImageInfo359     int height() const { return fDimensions.height(); }
360 
361     /** Returns SkColorType, one of:
362         kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
363         kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType,
364         kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType,
365         kGray_8_SkColorType, kRGBA_F16_SkColorType.
366 
367         @return  SkColorType
368     */
colorTypeSkImageInfo369     SkColorType colorType() const { return fColorType; }
370 
371     /** Returns SkAlphaType, one of:
372         kUnknown_SkAlphaType, kOpaque_SkAlphaType, kPremul_SkAlphaType,
373         kUnpremul_SkAlphaType.
374 
375         @return  SkAlphaType
376     */
alphaTypeSkImageInfo377     SkAlphaType alphaType() const { return fAlphaType; }
378 
379     /** Returns SkColorSpace, the range of colors. The reference count of
380         SkColorSpace is unchanged. The returned SkColorSpace is immutable.
381 
382         @return  SkColorSpace, or nullptr
383     */
colorSpaceSkImageInfo384     SkColorSpace* colorSpace() const { return fColorSpace.get(); }
385 
386     /** Returns smart pointer to SkColorSpace, the range of colors. The smart pointer
387         tracks the number of objects sharing this SkColorSpace reference so the memory
388         is released when the owners destruct.
389 
390         The returned SkColorSpace is immutable.
391 
392         @return  SkColorSpace wrapped in a smart pointer
393     */
refColorSpaceSkImageInfo394     sk_sp<SkColorSpace> refColorSpace() const { return fColorSpace; }
395 
396     /** Returns if SkImageInfo describes an empty area of pixels by checking if either
397         width or height is zero or smaller.
398 
399         @return  true if either dimension is zero or smaller
400     */
isEmptySkImageInfo401     bool isEmpty() const { return fDimensions.isEmpty(); }
402 
403     /** Returns true if SkAlphaType is set to hint that all pixels are opaque; their
404         alpha value is implicitly or explicitly 1.0. If true, and all pixels are
405         not opaque, Skia may draw incorrectly.
406 
407         Does not check if SkColorType allows alpha, or if any pixel value has
408         transparency.
409 
410         @return  true if SkAlphaType is kOpaque_SkAlphaType
411     */
isOpaqueSkImageInfo412     bool isOpaque() const {
413         return SkAlphaTypeIsOpaque(fAlphaType);
414     }
415 
416     /** Returns SkISize { width(), height() }.
417 
418         @return  integral size of width() and height()
419     */
dimensionsSkImageInfo420     SkISize dimensions() const { return fDimensions; }
421 
422     /** Returns SkIRect { 0, 0, width(), height() }.
423 
424         @return  integral rectangle from origin to width() and height()
425     */
boundsSkImageInfo426     SkIRect bounds() const { return SkIRect::MakeSize(fDimensions); }
427 
428     /** Returns true if associated SkColorSpace is not nullptr, and SkColorSpace gamma
429         is approximately the same as sRGB.
430         This includes the
431 
432         @return  true if SkColorSpace gamma is approximately the same as sRGB
433     */
gammaCloseToSRGBSkImageInfo434     bool gammaCloseToSRGB() const {
435         return fColorSpace && fColorSpace->gammaCloseToSRGB();
436     }
437 
438     /** Creates SkImageInfo with the same SkColorType, SkColorSpace, and SkAlphaType,
439         with dimensions set to width and height.
440 
441         @param newWidth   pixel column count; must be zero or greater
442         @param newHeight  pixel row count; must be zero or greater
443         @return           created SkImageInfo
444     */
makeWHSkImageInfo445     SkImageInfo makeWH(int newWidth, int newHeight) const {
446         return Make(newWidth, newHeight, fColorType, fAlphaType, fColorSpace);
447     }
448 
449     /** Creates SkImageInfo with same SkColorType, SkColorSpace, width, and height,
450         with SkAlphaType set to newAlphaType.
451 
452         Created SkImageInfo contains newAlphaType even if it is incompatible with
453         SkColorType, in which case SkAlphaType in SkImageInfo is ignored.
454 
455         @param newAlphaType  one of:
456                              kUnknown_SkAlphaType, kOpaque_SkAlphaType, kPremul_SkAlphaType,
457                              kUnpremul_SkAlphaType
458         @return              created SkImageInfo
459     */
makeAlphaTypeSkImageInfo460     SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const {
461         return Make(this->width(), this->height(), fColorType, newAlphaType, fColorSpace);
462     }
463 
464     /** Creates SkImageInfo with same SkAlphaType, SkColorSpace, width, and height,
465         with SkColorType set to newColorType.
466 
467         @param newColorType  one of:
468                              kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
469                              kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType,
470                              kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType,
471                              kRGB_101010x_SkColorType, kGray_8_SkColorType, kRGBA_F16_SkColorType
472         @return              created SkImageInfo
473     */
makeColorTypeSkImageInfo474     SkImageInfo makeColorType(SkColorType newColorType) const {
475         return Make(this->width(), this->height(), newColorType, fAlphaType, fColorSpace);
476     }
477 
478     /** Creates SkImageInfo with same SkAlphaType, SkColorType, width, and height,
479         with SkColorSpace set to cs.
480 
481         @param cs  range of colors; may be nullptr
482         @return    created SkImageInfo
483     */
makeColorSpaceSkImageInfo484     SkImageInfo makeColorSpace(sk_sp<SkColorSpace> cs) const {
485         return Make(this->width(), this->height(), fColorType, fAlphaType, std::move(cs));
486     }
487 
488     /** Returns number of bytes per pixel required by SkColorType.
489         Returns zero if colorType( is kUnknown_SkColorType.
490 
491         @return  bytes in pixel
492     */
493     int bytesPerPixel() const;
494 
495     /** Returns bit shift converting row bytes to row pixels.
496         Returns zero for kUnknown_SkColorType.
497 
498         @return  one of: 0, 1, 2, 3; left shift to convert pixels to bytes
499     */
500     int shiftPerPixel() const;
501 
502     /** Returns minimum bytes per row, computed from pixel width() and SkColorType, which
503         specifies bytesPerPixel(). SkBitmap maximum value for row bytes must fit
504         in 31 bits.
505 
506         @return  width() times bytesPerPixel() as unsigned 64-bit integer
507     */
minRowBytes64SkImageInfo508     uint64_t minRowBytes64() const { return sk_64_mul(this->width(), this->bytesPerPixel()); }
509 
510     /** Returns minimum bytes per row, computed from pixel width() and SkColorType, which
511         specifies bytesPerPixel(). SkBitmap maximum value for row bytes must fit
512         in 31 bits.
513 
514         @return  width() times bytesPerPixel() as signed 32-bit integer
515     */
minRowBytesSkImageInfo516     size_t minRowBytes() const {
517         uint64_t minRowBytes = this->minRowBytes64();
518         if (!SkTFitsIn<int32_t>(minRowBytes)) {
519             return 0;
520         }
521         return SkTo<int32_t>(minRowBytes);
522     }
523 
524     /** Returns byte offset of pixel from pixel base address.
525 
526         Asserts in debug build if x or y is outside of bounds. Does not assert if
527         rowBytes is smaller than minRowBytes(), even though result may be incorrect.
528 
529         @param x         column index, zero or greater, and less than width()
530         @param y         row index, zero or greater, and less than height()
531         @param rowBytes  size of pixel row or larger
532         @return          offset within pixel array
533     */
534     size_t computeOffset(int x, int y, size_t rowBytes) const;
535 
536     /** Compares SkImageInfo with other, and returns true if width, height, SkColorType,
537         SkAlphaType, and SkColorSpace are equivalent.
538 
539         @param other  SkImageInfo to compare
540         @return       true if SkImageInfo equals other
541     */
542     bool operator==(const SkImageInfo& other) const {
543         return fDimensions == other.fDimensions &&
544                fColorType == other.fColorType && fAlphaType == other.fAlphaType &&
545                SkColorSpace::Equals(fColorSpace.get(), other.fColorSpace.get());
546     }
547 
548     /** Compares SkImageInfo with other, and returns true if width, height, SkColorType,
549         SkAlphaType, and SkColorSpace are not equivalent.
550 
551         @param other  SkImageInfo to compare
552         @return       true if SkImageInfo is not equal to other
553     */
554     bool operator!=(const SkImageInfo& other) const {
555         return !(*this == other);
556     }
557 
558     /** Returns storage required by pixel array, given SkImageInfo dimensions, SkColorType,
559         and rowBytes. rowBytes is assumed to be at least as large as minRowBytes().
560 
561         Returns zero if height is zero.
562         Returns SIZE_MAX if answer exceeds the range of size_t.
563 
564         @param rowBytes  size of pixel row or larger
565         @return          memory required by pixel buffer
566     */
567     size_t computeByteSize(size_t rowBytes) const;
568 
569     /** Returns storage required by pixel array, given SkImageInfo dimensions, and
570         SkColorType. Uses minRowBytes() to compute bytes for pixel row.
571 
572         Returns zero if height is zero.
573         Returns SIZE_MAX if answer exceeds the range of size_t.
574 
575         @return  least memory required by pixel buffer
576     */
computeMinByteSizeSkImageInfo577     size_t computeMinByteSize() const {
578         return this->computeByteSize(this->minRowBytes());
579     }
580 
581     /** Returns true if byteSize equals SIZE_MAX. computeByteSize() and
582         computeMinByteSize() return SIZE_MAX if size_t can not hold buffer size.
583 
584         @param byteSize  result of computeByteSize() or computeMinByteSize()
585         @return          true if computeByteSize() or computeMinByteSize() result exceeds size_t
586     */
ByteSizeOverflowedSkImageInfo587     static bool ByteSizeOverflowed(size_t byteSize) {
588         return SIZE_MAX == byteSize;
589     }
590 
591     /** Returns true if rowBytes is smaller than width times pixel size.
592 
593         @param rowBytes  size of pixel row or larger
594         @return          true if rowBytes is large enough to contain pixel row
595     */
validRowBytesSkImageInfo596     bool validRowBytes(size_t rowBytes) const {
597         return rowBytes >= this->minRowBytes64();
598     }
599 
600     /** Creates an empty SkImageInfo with kUnknown_SkColorType, kUnknown_SkAlphaType,
601         a width and height of zero, and no SkColorSpace.
602     */
resetSkImageInfo603     void reset() {
604         fColorSpace = nullptr;
605         fDimensions = {0, 0};
606         fColorType = kUnknown_SkColorType;
607         fAlphaType = kUnknown_SkAlphaType;
608     }
609 
610     /** Asserts if internal values are illegal or inconsistent. Only available if
611         SK_DEBUG is defined at compile time.
612     */
613     SkDEBUGCODE(void validate() const;)
614 
615 private:
616     sk_sp<SkColorSpace> fColorSpace;
617     SkISize             fDimensions;
618     SkColorType         fColorType;
619     SkAlphaType         fAlphaType;
620 
SkImageInfoSkImageInfo621     SkImageInfo(int width, int height, SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs)
622         : fColorSpace(std::move(cs))
623         , fDimensions{width, height}
624         , fColorType(ct)
625         , fAlphaType(at)
626     {}
627 };
628 
629 #endif
630