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