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/SkAlphaType.h" 12 #include "include/core/SkColorType.h" 13 #include "include/core/SkRect.h" 14 #include "include/core/SkRefCnt.h" 15 #include "include/core/SkSize.h" 16 #include "include/private/base/SkAPI.h" 17 #include "include/private/base/SkDebug.h" 18 #include "include/private/base/SkMath.h" 19 #include "include/private/base/SkTFitsIn.h" 20 21 #include <cstddef> 22 #include <cstdint> 23 #include <utility> 24 25 class SkColorSpace; 26 27 /** Returns the number of bytes required to store a pixel, including unused padding. 28 Returns zero if ct is kUnknown_SkColorType or invalid. 29 30 @return bytes per pixel 31 */ 32 SK_API int SkColorTypeBytesPerPixel(SkColorType ct); 33 34 /** Returns true if SkColorType always decodes alpha to 1.0, making the pixel 35 fully opaque. If true, SkColorType does not reserve bits to encode alpha. 36 37 @return true if alpha is always set to 1.0 38 */ 39 SK_API bool SkColorTypeIsAlwaysOpaque(SkColorType ct); 40 41 /** Returns true if canonical can be set to a valid SkAlphaType for colorType. If 42 there is more than one valid canonical SkAlphaType, set to alphaType, if valid. 43 If true is returned and canonical is not nullptr, store valid SkAlphaType. 44 45 Returns false only if alphaType is kUnknown_SkAlphaType, color type is not 46 kUnknown_SkColorType, and SkColorType is not always opaque. If false is returned, 47 canonical is ignored. 48 49 @param canonical storage for SkAlphaType 50 @return true if valid SkAlphaType can be associated with colorType 51 */ 52 SK_API bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType, 53 SkAlphaType* canonical = nullptr); 54 55 /** \enum SkImageInfo::SkYUVColorSpace 56 Describes color range of YUV pixels. The color mapping from YUV to RGB varies 57 depending on the source. YUV pixels may be generated by JPEG images, standard 58 video streams, or high definition video streams. Each has its own mapping from 59 YUV to RGB. 60 61 JPEG YUV values encode the full range of 0 to 255 for all three components. 62 Video YUV values often range from 16 to 235 for Y and from 16 to 240 for U and V (limited). 63 Details of encoding and conversion to RGB are described in YCbCr color space. 64 65 The identity colorspace exists to provide a utility mapping from Y to R, U to G and V to B. 66 It can be used to visualize the YUV planes or to explicitly post process the YUV channels. 67 */ 68 enum SkYUVColorSpace : int { 69 kJPEG_Full_SkYUVColorSpace, //!< describes full range 70 kRec601_Limited_SkYUVColorSpace, //!< describes SDTV range 71 kRec709_Full_SkYUVColorSpace, //!< describes HDTV range 72 kRec709_Limited_SkYUVColorSpace, 73 kBT2020_8bit_Full_SkYUVColorSpace, //!< describes UHDTV range, non-constant-luminance 74 kBT2020_8bit_Limited_SkYUVColorSpace, 75 kBT2020_10bit_Full_SkYUVColorSpace, 76 kBT2020_10bit_Limited_SkYUVColorSpace, 77 kBT2020_12bit_Full_SkYUVColorSpace, 78 kBT2020_12bit_Limited_SkYUVColorSpace, 79 kFCC_Full_SkYUVColorSpace, //!< describes FCC range 80 kFCC_Limited_SkYUVColorSpace, 81 kSMPTE240_Full_SkYUVColorSpace, //!< describes SMPTE240M range 82 kSMPTE240_Limited_SkYUVColorSpace, 83 kYDZDX_Full_SkYUVColorSpace, //!< describes YDZDX range 84 kYDZDX_Limited_SkYUVColorSpace, 85 kGBR_Full_SkYUVColorSpace, //!< describes GBR range 86 kGBR_Limited_SkYUVColorSpace, 87 kYCgCo_8bit_Full_SkYUVColorSpace, //!< describes YCgCo matrix 88 kYCgCo_8bit_Limited_SkYUVColorSpace, 89 kYCgCo_10bit_Full_SkYUVColorSpace, 90 kYCgCo_10bit_Limited_SkYUVColorSpace, 91 kYCgCo_12bit_Full_SkYUVColorSpace, 92 kYCgCo_12bit_Limited_SkYUVColorSpace, 93 kIdentity_SkYUVColorSpace, //!< maps Y->R, U->G, V->B 94 95 kLastEnum_SkYUVColorSpace = kIdentity_SkYUVColorSpace, //!< last valid value 96 97 // Legacy (deprecated) names: 98 kJPEG_SkYUVColorSpace = kJPEG_Full_SkYUVColorSpace, 99 kRec601_SkYUVColorSpace = kRec601_Limited_SkYUVColorSpace, 100 kRec709_SkYUVColorSpace = kRec709_Limited_SkYUVColorSpace, 101 kBT2020_SkYUVColorSpace = kBT2020_8bit_Limited_SkYUVColorSpace, 102 }; 103 104 /** \struct SkColorInfo 105 Describes pixel and encoding. SkImageInfo can be created from SkColorInfo by 106 providing dimensions. 107 108 It encodes how pixel bits describe alpha, transparency; color components red, blue, 109 and green; and SkColorSpace, the range and linearity of colors. 110 */ 111 class SK_API SkColorInfo { 112 public: 113 /** Creates an SkColorInfo with kUnknown_SkColorType, kUnknown_SkAlphaType, 114 and no SkColorSpace. 115 116 @return empty SkImageInfo 117 */ 118 SkColorInfo(); 119 ~SkColorInfo(); 120 121 /** Creates SkColorInfo from SkColorType ct, SkAlphaType at, and optionally SkColorSpace cs. 122 123 If SkColorSpace cs is nullptr and SkColorInfo is part of drawing source: SkColorSpace 124 defaults to sRGB, mapping into SkSurface SkColorSpace. 125 126 Parameters are not validated to see if their values are legal, or that the 127 combination is supported. 128 @return created SkColorInfo 129 */ 130 SkColorInfo(SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs); 131 132 SkColorInfo(const SkColorInfo&); 133 SkColorInfo(SkColorInfo&&); 134 135 SkColorInfo& operator=(const SkColorInfo&); 136 SkColorInfo& operator=(SkColorInfo&&); 137 138 SkColorSpace* colorSpace() const; 139 sk_sp<SkColorSpace> refColorSpace() const; colorType()140 SkColorType colorType() const { return fColorType; } alphaType()141 SkAlphaType alphaType() const { return fAlphaType; } 142 isOpaque()143 bool isOpaque() const { 144 return SkAlphaTypeIsOpaque(fAlphaType) 145 || SkColorTypeIsAlwaysOpaque(fColorType); 146 } 147 148 bool gammaCloseToSRGB() const; 149 150 /** Does other represent the same color type, alpha type, and color space? */ 151 bool operator==(const SkColorInfo& other) const; 152 153 /** Does other represent a different color type, alpha type, or color space? */ 154 bool operator!=(const SkColorInfo& other) const; 155 156 /** Creates SkColorInfo with same SkColorType, SkColorSpace, with SkAlphaType set 157 to newAlphaType. 158 159 Created SkColorInfo contains newAlphaType even if it is incompatible with 160 SkColorType, in which case SkAlphaType in SkColorInfo is ignored. 161 */ 162 SkColorInfo makeAlphaType(SkAlphaType newAlphaType) const; 163 164 /** Creates new SkColorInfo with same SkAlphaType, SkColorSpace, with SkColorType 165 set to newColorType. 166 */ 167 SkColorInfo makeColorType(SkColorType newColorType) const; 168 169 /** Creates SkColorInfo with same SkAlphaType, SkColorType, with SkColorSpace 170 set to cs. cs may be nullptr. 171 */ 172 SkColorInfo makeColorSpace(sk_sp<SkColorSpace> cs) const; 173 174 /** Returns number of bytes per pixel required by SkColorType. 175 Returns zero if colorType() is kUnknown_SkColorType. 176 177 @return bytes in pixel 178 179 example: https://fiddle.skia.org/c/@ImageInfo_bytesPerPixel 180 */ 181 int bytesPerPixel() const; 182 183 /** Returns bit shift converting row bytes to row pixels. 184 Returns zero for kUnknown_SkColorType. 185 186 @return one of: 0, 1, 2, 3, 4; left shift to convert pixels to bytes 187 188 example: https://fiddle.skia.org/c/@ImageInfo_shiftPerPixel 189 */ 190 int shiftPerPixel() const; 191 192 private: 193 sk_sp<SkColorSpace> fColorSpace; 194 SkColorType fColorType = kUnknown_SkColorType; 195 SkAlphaType fAlphaType = kUnknown_SkAlphaType; 196 }; 197 198 /** \struct SkImageInfo 199 Describes pixel dimensions and encoding. SkBitmap, SkImage, PixMap, and SkSurface 200 can be created from SkImageInfo. SkImageInfo can be retrieved from SkBitmap and 201 SkPixmap, but not from SkImage and SkSurface. For example, SkImage and SkSurface 202 implementations may defer pixel depth, so may not completely specify SkImageInfo. 203 204 SkImageInfo contains dimensions, the pixel integral width and height. It encodes 205 how pixel bits describe alpha, transparency; color components red, blue, 206 and green; and SkColorSpace, the range and linearity of colors. 207 */ 208 struct SK_API SkImageInfo { 209 public: 210 211 /** Creates an empty SkImageInfo with kUnknown_SkColorType, kUnknown_SkAlphaType, 212 a width and height of zero, and no SkColorSpace. 213 214 @return empty SkImageInfo 215 */ 216 SkImageInfo() = default; 217 218 /** Creates SkImageInfo from integral dimensions width and height, SkColorType ct, 219 SkAlphaType at, and optionally SkColorSpace cs. 220 221 If SkColorSpace cs is nullptr and SkImageInfo is part of drawing source: SkColorSpace 222 defaults to sRGB, mapping into SkSurface SkColorSpace. 223 224 Parameters are not validated to see if their values are legal, or that the 225 combination is supported. 226 227 @param width pixel column count; must be zero or greater 228 @param height pixel row count; must be zero or greater 229 @param cs range of colors; may be nullptr 230 @return created SkImageInfo 231 */ 232 static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at); 233 static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at, 234 sk_sp<SkColorSpace> cs); 235 static SkImageInfo Make(SkISize dimensions, SkColorType ct, SkAlphaType at); 236 static SkImageInfo Make(SkISize dimensions, SkColorType ct, SkAlphaType at, 237 sk_sp<SkColorSpace> cs); 238 239 /** Creates SkImageInfo from integral dimensions and SkColorInfo colorInfo, 240 241 Parameters are not validated to see if their values are legal, or that the 242 combination is supported. 243 244 @param dimensions pixel column and row count; must be zeros or greater 245 @param SkColorInfo the pixel encoding consisting of SkColorType, SkAlphaType, and 246 SkColorSpace (which may be nullptr) 247 @return created SkImageInfo 248 */ MakeSkImageInfo249 static SkImageInfo Make(SkISize dimensions, const SkColorInfo& colorInfo) { 250 return SkImageInfo(dimensions, colorInfo); 251 } MakeSkImageInfo252 static SkImageInfo Make(SkISize dimensions, SkColorInfo&& colorInfo) { 253 return SkImageInfo(dimensions, std::move(colorInfo)); 254 } 255 256 /** Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType, 257 SkAlphaType at, and optionally SkColorSpace cs. kN32_SkColorType will equal either 258 kBGRA_8888_SkColorType or kRGBA_8888_SkColorType, whichever is optimal. 259 260 If SkColorSpace cs is nullptr and SkImageInfo is part of drawing source: SkColorSpace 261 defaults to sRGB, mapping into SkSurface SkColorSpace. 262 263 Parameters are not validated to see if their values are legal, or that the 264 combination is supported. 265 266 @param width pixel column count; must be zero or greater 267 @param height pixel row count; must be zero or greater 268 @param cs range of colors; may be nullptr 269 @return created SkImageInfo 270 */ 271 static SkImageInfo MakeN32(int width, int height, SkAlphaType at); 272 static SkImageInfo MakeN32(int width, int height, SkAlphaType at, sk_sp<SkColorSpace> cs); 273 274 /** Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType, 275 SkAlphaType at, with sRGB SkColorSpace. 276 277 Parameters are not validated to see if their values are legal, or that the 278 combination is supported. 279 280 @param width pixel column count; must be zero or greater 281 @param height pixel row count; must be zero or greater 282 @return created SkImageInfo 283 284 example: https://fiddle.skia.org/c/@ImageInfo_MakeS32 285 */ 286 static SkImageInfo MakeS32(int width, int height, SkAlphaType at); 287 288 /** Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType, 289 kPremul_SkAlphaType, with optional SkColorSpace. 290 291 If SkColorSpace cs is nullptr and SkImageInfo is part of drawing source: SkColorSpace 292 defaults to sRGB, mapping into SkSurface SkColorSpace. 293 294 Parameters are not validated to see if their values are legal, or that the 295 combination is supported. 296 297 @param width pixel column count; must be zero or greater 298 @param height pixel row count; must be zero or greater 299 @param cs range of colors; may be nullptr 300 @return created SkImageInfo 301 */ 302 static SkImageInfo MakeN32Premul(int width, int height); 303 static SkImageInfo MakeN32Premul(int width, int height, sk_sp<SkColorSpace> cs); 304 305 /** Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType, 306 kPremul_SkAlphaType, with SkColorSpace set to nullptr. 307 308 If SkImageInfo is part of drawing source: SkColorSpace defaults to sRGB, mapping 309 into SkSurface SkColorSpace. 310 311 Parameters are not validated to see if their values are legal, or that the 312 combination is supported. 313 314 @param dimensions width and height, each must be zero or greater 315 @param cs range of colors; may be nullptr 316 @return created SkImageInfo 317 */ 318 static SkImageInfo MakeN32Premul(SkISize dimensions); 319 static SkImageInfo MakeN32Premul(SkISize dimensions, sk_sp<SkColorSpace> cs); 320 321 /** Creates SkImageInfo from integral dimensions width and height, kAlpha_8_SkColorType, 322 kPremul_SkAlphaType, with SkColorSpace set to nullptr. 323 324 @param width pixel column count; must be zero or greater 325 @param height pixel row count; must be zero or greater 326 @return created SkImageInfo 327 */ 328 static SkImageInfo MakeA8(int width, int height); 329 /** Creates SkImageInfo from integral dimensions, kAlpha_8_SkColorType, 330 kPremul_SkAlphaType, with SkColorSpace set to nullptr. 331 332 @param dimensions pixel row and column count; must be zero or greater 333 @return created SkImageInfo 334 */ 335 static SkImageInfo MakeA8(SkISize dimensions); 336 337 /** Creates SkImageInfo from integral dimensions width and height, kUnknown_SkColorType, 338 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 @param width pixel column count; must be zero or greater 344 @param height pixel row count; must be zero or greater 345 @return created SkImageInfo 346 */ 347 static SkImageInfo MakeUnknown(int width, int height); 348 349 /** Creates SkImageInfo from integral dimensions width and height set to zero, 350 kUnknown_SkColorType, kUnknown_SkAlphaType, with SkColorSpace set to nullptr. 351 352 Returned SkImageInfo as part of source does not draw, and as part of destination 353 can not be drawn to. 354 355 @return created SkImageInfo 356 */ MakeUnknownSkImageInfo357 static SkImageInfo MakeUnknown() { 358 return MakeUnknown(0, 0); 359 } 360 361 /** Returns pixel count in each row. 362 363 @return pixel width 364 */ widthSkImageInfo365 int width() const { return fDimensions.width(); } 366 367 /** Returns pixel row count. 368 369 @return pixel height 370 */ heightSkImageInfo371 int height() const { return fDimensions.height(); } 372 colorTypeSkImageInfo373 SkColorType colorType() const { return fColorInfo.colorType(); } 374 alphaTypeSkImageInfo375 SkAlphaType alphaType() const { return fColorInfo.alphaType(); } 376 377 /** Returns SkColorSpace, the range of colors. The reference count of 378 SkColorSpace is unchanged. The returned SkColorSpace is immutable. 379 380 @return SkColorSpace, or nullptr 381 */ 382 SkColorSpace* colorSpace() const; 383 384 /** Returns smart pointer to SkColorSpace, the range of colors. The smart pointer 385 tracks the number of objects sharing this SkColorSpace reference so the memory 386 is released when the owners destruct. 387 388 The returned SkColorSpace is immutable. 389 390 @return SkColorSpace wrapped in a smart pointer 391 */ 392 sk_sp<SkColorSpace> refColorSpace() const; 393 394 /** Returns if SkImageInfo describes an empty area of pixels by checking if either 395 width or height is zero or smaller. 396 397 @return true if either dimension is zero or smaller 398 */ isEmptySkImageInfo399 bool isEmpty() const { return fDimensions.isEmpty(); } 400 401 /** Returns the dimensionless SkColorInfo that represents the same color type, 402 alpha type, and color space as this SkImageInfo. 403 */ colorInfoSkImageInfo404 const SkColorInfo& colorInfo() const { return fColorInfo; } 405 406 /** Returns true if SkAlphaType is set to hint that all pixels are opaque; their 407 alpha value is implicitly or explicitly 1.0. If true, and all pixels are 408 not opaque, Skia may draw incorrectly. 409 410 Does not check if SkColorType allows alpha, or if any pixel value has 411 transparency. 412 413 @return true if SkAlphaType is kOpaque_SkAlphaType 414 */ isOpaqueSkImageInfo415 bool isOpaque() const { return fColorInfo.isOpaque(); } 416 417 /** Returns SkISize { width(), height() }. 418 419 @return integral size of width() and height() 420 */ dimensionsSkImageInfo421 SkISize dimensions() const { return fDimensions; } 422 423 /** Returns SkIRect { 0, 0, width(), height() }. 424 425 @return integral rectangle from origin to width() and height() 426 */ boundsSkImageInfo427 SkIRect bounds() const { return SkIRect::MakeSize(fDimensions); } 428 429 /** Returns true if associated SkColorSpace is not nullptr, and SkColorSpace gamma 430 is approximately the same as sRGB. 431 This includes the 432 433 @return true if SkColorSpace gamma is approximately the same as sRGB 434 */ gammaCloseToSRGBSkImageInfo435 bool gammaCloseToSRGB() const { return fColorInfo.gammaCloseToSRGB(); } 436 437 /** Creates SkImageInfo with the same SkColorType, SkColorSpace, and SkAlphaType, 438 with dimensions set to width and height. 439 440 @param newWidth pixel column count; must be zero or greater 441 @param newHeight pixel row count; must be zero or greater 442 @return created SkImageInfo 443 */ makeWHSkImageInfo444 SkImageInfo makeWH(int newWidth, int newHeight) const { 445 return Make({newWidth, newHeight}, fColorInfo); 446 } 447 448 /** Creates SkImageInfo with the same SkColorType, SkColorSpace, and SkAlphaType, 449 with dimensions set to newDimensions. 450 451 @param newSize pixel column and row count; must be zero or greater 452 @return created SkImageInfo 453 */ makeDimensionsSkImageInfo454 SkImageInfo makeDimensions(SkISize newSize) const { 455 return Make(newSize, fColorInfo); 456 } 457 458 /** Creates SkImageInfo with same SkColorType, SkColorSpace, width, and height, 459 with SkAlphaType set to newAlphaType. 460 461 Created SkImageInfo contains newAlphaType even if it is incompatible with 462 SkColorType, in which case SkAlphaType in SkImageInfo is ignored. 463 464 @return created SkImageInfo 465 */ makeAlphaTypeSkImageInfo466 SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const { 467 return Make(fDimensions, fColorInfo.makeAlphaType(newAlphaType)); 468 } 469 470 /** Creates SkImageInfo with same SkAlphaType, SkColorSpace, width, and height, 471 with SkColorType set to newColorType. 472 473 @return created SkImageInfo 474 */ makeColorTypeSkImageInfo475 SkImageInfo makeColorType(SkColorType newColorType) const { 476 return Make(fDimensions, fColorInfo.makeColorType(newColorType)); 477 } 478 479 /** Creates SkImageInfo with same SkAlphaType, SkColorType, width, and height, 480 with SkColorSpace set to cs. 481 482 @param cs range of colors; may be nullptr 483 @return created SkImageInfo 484 */ 485 SkImageInfo makeColorSpace(sk_sp<SkColorSpace> cs) const; 486 487 /** Returns number of bytes per pixel required by SkColorType. 488 Returns zero if colorType( is kUnknown_SkColorType. 489 490 @return bytes in pixel 491 */ bytesPerPixelSkImageInfo492 int bytesPerPixel() const { return fColorInfo.bytesPerPixel(); } 493 494 /** Returns bit shift converting row bytes to row pixels. 495 Returns zero for kUnknown_SkColorType. 496 497 @return one of: 0, 1, 2, 3; left shift to convert pixels to bytes 498 */ shiftPerPixelSkImageInfo499 int shiftPerPixel() const { return fColorInfo.shiftPerPixel(); } 500 501 /** Returns minimum bytes per row, computed from pixel width() and SkColorType, which 502 specifies bytesPerPixel(). SkBitmap maximum value for row bytes must fit 503 in 31 bits. 504 505 @return width() times bytesPerPixel() as unsigned 64-bit integer 506 */ minRowBytes64SkImageInfo507 uint64_t minRowBytes64() const { 508 return (uint64_t)sk_64_mul(this->width(), this->bytesPerPixel()); 509 } 510 511 /** Returns minimum bytes per row, computed from pixel width() and SkColorType, which 512 specifies bytesPerPixel(). SkBitmap maximum value for row bytes must fit 513 in 31 bits. 514 515 @return width() times bytesPerPixel() as size_t 516 */ minRowBytesSkImageInfo517 size_t minRowBytes() const { 518 uint64_t minRowBytes = this->minRowBytes64(); 519 if (!SkTFitsIn<int32_t>(minRowBytes)) { 520 return 0; 521 } 522 return (size_t)minRowBytes; 523 } 524 525 /** Returns byte offset of pixel from pixel base address. 526 527 Asserts in debug build if x or y is outside of bounds. Does not assert if 528 rowBytes is smaller than minRowBytes(), even though result may be incorrect. 529 530 @param x column index, zero or greater, and less than width() 531 @param y row index, zero or greater, and less than height() 532 @param rowBytes size of pixel row or larger 533 @return offset within pixel array 534 535 example: https://fiddle.skia.org/c/@ImageInfo_computeOffset 536 */ 537 size_t computeOffset(int x, int y, size_t rowBytes) const; 538 539 /** Compares SkImageInfo with other, and returns true if width, height, SkColorType, 540 SkAlphaType, and SkColorSpace are equivalent. 541 542 @param other SkImageInfo to compare 543 @return true if SkImageInfo equals other 544 */ 545 bool operator==(const SkImageInfo& other) const { 546 return fDimensions == other.fDimensions && fColorInfo == other.fColorInfo; 547 } 548 549 /** Compares SkImageInfo with other, and returns true if width, height, SkColorType, 550 SkAlphaType, and SkColorSpace are not equivalent. 551 552 @param other SkImageInfo to compare 553 @return true if SkImageInfo is not equal to other 554 */ 555 bool operator!=(const SkImageInfo& other) const { 556 return !(*this == other); 557 } 558 559 /** Returns storage required by pixel array, given SkImageInfo dimensions, SkColorType, 560 and rowBytes. rowBytes is assumed to be at least as large as minRowBytes(). 561 562 Returns zero if height is zero. 563 Returns SIZE_MAX if answer exceeds the range of size_t. 564 565 @param rowBytes size of pixel row or larger 566 @return memory required by pixel buffer 567 */ 568 size_t computeByteSize(size_t rowBytes) const; 569 570 /** Returns storage required by pixel array, given SkImageInfo dimensions, and 571 SkColorType. Uses minRowBytes() to compute bytes for pixel row. 572 573 Returns zero if height is zero. 574 Returns SIZE_MAX if answer exceeds the range of size_t. 575 576 @return least memory required by pixel buffer 577 */ computeMinByteSizeSkImageInfo578 size_t computeMinByteSize() const { 579 return this->computeByteSize(this->minRowBytes()); 580 } 581 582 /** Returns true if byteSize equals SIZE_MAX. computeByteSize() and 583 computeMinByteSize() return SIZE_MAX if size_t can not hold buffer size. 584 585 @param byteSize result of computeByteSize() or computeMinByteSize() 586 @return true if computeByteSize() or computeMinByteSize() result exceeds size_t 587 */ ByteSizeOverflowedSkImageInfo588 static bool ByteSizeOverflowed(size_t byteSize) { 589 return SIZE_MAX == byteSize; 590 } 591 592 /** Returns true if rowBytes is valid for this SkImageInfo. 593 594 @param rowBytes size of pixel row including padding 595 @return true if rowBytes is large enough to contain pixel row and is properly 596 aligned 597 */ validRowBytesSkImageInfo598 bool validRowBytes(size_t rowBytes) const { 599 if (rowBytes < this->minRowBytes64()) { 600 return false; 601 } 602 int shift = this->shiftPerPixel(); 603 size_t alignedRowBytes = rowBytes >> shift << shift; 604 return alignedRowBytes == rowBytes; 605 } 606 607 /** Creates an empty SkImageInfo with kUnknown_SkColorType, kUnknown_SkAlphaType, 608 a width and height of zero, and no SkColorSpace. 609 */ resetSkImageInfo610 void reset() { *this = {}; } 611 612 /** Asserts if internal values are illegal or inconsistent. Only available if 613 SK_DEBUG is defined at compile time. 614 */ 615 SkDEBUGCODE(void validate() const;) 616 617 private: 618 SkColorInfo fColorInfo; 619 SkISize fDimensions = {0, 0}; 620 SkImageInfoSkImageInfo621 SkImageInfo(SkISize dimensions, const SkColorInfo& colorInfo) 622 : fColorInfo(colorInfo), fDimensions(dimensions) {} 623 SkImageInfoSkImageInfo624 SkImageInfo(SkISize dimensions, SkColorInfo&& colorInfo) 625 : fColorInfo(std::move(colorInfo)), fDimensions(dimensions) {} 626 }; 627 628 #endif 629