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 kBT2020_16bit_Full_SkYUVColorSpace, 80 kBT2020_16bit_Limited_SkYUVColorSpace, 81 kFCC_Full_SkYUVColorSpace, //!< describes FCC range 82 kFCC_Limited_SkYUVColorSpace, 83 kSMPTE240_Full_SkYUVColorSpace, //!< describes SMPTE240M range 84 kSMPTE240_Limited_SkYUVColorSpace, 85 kYDZDX_Full_SkYUVColorSpace, //!< describes YDZDX range 86 kYDZDX_Limited_SkYUVColorSpace, 87 kGBR_Full_SkYUVColorSpace, //!< describes GBR range 88 kGBR_Limited_SkYUVColorSpace, 89 kYCgCo_8bit_Full_SkYUVColorSpace, //!< describes YCgCo matrix 90 kYCgCo_8bit_Limited_SkYUVColorSpace, 91 kYCgCo_10bit_Full_SkYUVColorSpace, 92 kYCgCo_10bit_Limited_SkYUVColorSpace, 93 kYCgCo_12bit_Full_SkYUVColorSpace, 94 kYCgCo_12bit_Limited_SkYUVColorSpace, 95 kYCgCo_16bit_Full_SkYUVColorSpace, 96 kYCgCo_16bit_Limited_SkYUVColorSpace, 97 kIdentity_SkYUVColorSpace, //!< maps Y->R, U->G, V->B 98 99 kLastEnum_SkYUVColorSpace = kIdentity_SkYUVColorSpace, //!< last valid value 100 101 // Legacy (deprecated) names: 102 kJPEG_SkYUVColorSpace = kJPEG_Full_SkYUVColorSpace, 103 kRec601_SkYUVColorSpace = kRec601_Limited_SkYUVColorSpace, 104 kRec709_SkYUVColorSpace = kRec709_Limited_SkYUVColorSpace, 105 kBT2020_SkYUVColorSpace = kBT2020_8bit_Limited_SkYUVColorSpace, 106 }; 107 108 SK_API bool SkYUVColorSpaceIsLimitedRange(SkYUVColorSpace cs); 109 110 /** \struct SkColorInfo 111 Describes pixel and encoding. SkImageInfo can be created from SkColorInfo by 112 providing dimensions. 113 114 It encodes how pixel bits describe alpha, transparency; color components red, blue, 115 and green; and SkColorSpace, the range and linearity of colors. 116 */ 117 class SK_API SkColorInfo { 118 public: 119 /** Creates an SkColorInfo with kUnknown_SkColorType, kUnknown_SkAlphaType, 120 and no SkColorSpace. 121 122 @return empty SkImageInfo 123 */ 124 SkColorInfo(); 125 ~SkColorInfo(); 126 127 /** Creates SkColorInfo from SkColorType ct, SkAlphaType at, and optionally SkColorSpace cs. 128 129 If SkColorSpace cs is nullptr and SkColorInfo is part of drawing source: SkColorSpace 130 defaults to sRGB, mapping into SkSurface SkColorSpace. 131 132 Parameters are not validated to see if their values are legal, or that the 133 combination is supported. 134 @return created SkColorInfo 135 */ 136 SkColorInfo(SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs); 137 138 SkColorInfo(const SkColorInfo&); 139 SkColorInfo(SkColorInfo&&); 140 141 SkColorInfo& operator=(const SkColorInfo&); 142 SkColorInfo& operator=(SkColorInfo&&); 143 144 SkColorSpace* colorSpace() const; 145 sk_sp<SkColorSpace> refColorSpace() const; colorType()146 SkColorType colorType() const { return fColorType; } alphaType()147 SkAlphaType alphaType() const { return fAlphaType; } 148 isOpaque()149 bool isOpaque() const { 150 return SkAlphaTypeIsOpaque(fAlphaType) 151 || SkColorTypeIsAlwaysOpaque(fColorType); 152 } 153 154 bool gammaCloseToSRGB() const; 155 156 /** Does other represent the same color type, alpha type, and color space? */ 157 bool operator==(const SkColorInfo& other) const; 158 159 /** Does other represent a different color type, alpha type, or color space? */ 160 bool operator!=(const SkColorInfo& other) const; 161 162 /** Creates SkColorInfo with same SkColorType, SkColorSpace, with SkAlphaType set 163 to newAlphaType. 164 165 Created SkColorInfo contains newAlphaType even if it is incompatible with 166 SkColorType, in which case SkAlphaType in SkColorInfo is ignored. 167 */ 168 SkColorInfo makeAlphaType(SkAlphaType newAlphaType) const; 169 170 /** Creates new SkColorInfo with same SkAlphaType, SkColorSpace, with SkColorType 171 set to newColorType. 172 */ 173 SkColorInfo makeColorType(SkColorType newColorType) const; 174 175 /** Creates SkColorInfo with same SkAlphaType, SkColorType, with SkColorSpace 176 set to cs. cs may be nullptr. 177 */ 178 SkColorInfo makeColorSpace(sk_sp<SkColorSpace> cs) const; 179 180 /** Returns number of bytes per pixel required by SkColorType. 181 Returns zero if colorType() is kUnknown_SkColorType. 182 183 @return bytes in pixel 184 185 example: https://fiddle.skia.org/c/@ImageInfo_bytesPerPixel 186 */ 187 int bytesPerPixel() const; 188 189 /** Returns bit shift converting row bytes to row pixels. 190 Returns zero for kUnknown_SkColorType. 191 192 @return one of: 0, 1, 2, 3, 4; left shift to convert pixels to bytes 193 194 example: https://fiddle.skia.org/c/@ImageInfo_shiftPerPixel 195 */ 196 int shiftPerPixel() const; 197 198 private: 199 sk_sp<SkColorSpace> fColorSpace; 200 SkColorType fColorType = kUnknown_SkColorType; 201 SkAlphaType fAlphaType = kUnknown_SkAlphaType; 202 }; 203 204 /** \struct SkImageInfo 205 Describes pixel dimensions and encoding. SkBitmap, SkImage, PixMap, and SkSurface 206 can be created from SkImageInfo. SkImageInfo can be retrieved from SkBitmap and 207 SkPixmap, but not from SkImage and SkSurface. For example, SkImage and SkSurface 208 implementations may defer pixel depth, so may not completely specify SkImageInfo. 209 210 SkImageInfo contains dimensions, the pixel integral width and height. It encodes 211 how pixel bits describe alpha, transparency; color components red, blue, 212 and green; and SkColorSpace, the range and linearity of colors. 213 */ 214 struct SK_API SkImageInfo { 215 public: 216 217 /** Creates an empty SkImageInfo with kUnknown_SkColorType, kUnknown_SkAlphaType, 218 a width and height of zero, and no SkColorSpace. 219 220 @return empty SkImageInfo 221 */ 222 SkImageInfo() = default; 223 224 /** Creates SkImageInfo from integral dimensions width and height, SkColorType ct, 225 SkAlphaType at, and optionally SkColorSpace cs. 226 227 If SkColorSpace cs is nullptr and SkImageInfo is part of drawing source: SkColorSpace 228 defaults to sRGB, mapping into SkSurface SkColorSpace. 229 230 Parameters are not validated to see if their values are legal, or that the 231 combination is supported. 232 233 @param width pixel column count; must be zero or greater 234 @param height pixel row count; must be zero or greater 235 @param cs range of colors; may be nullptr 236 @return created SkImageInfo 237 */ 238 static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at); 239 static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at, 240 sk_sp<SkColorSpace> cs); 241 static SkImageInfo Make(SkISize dimensions, SkColorType ct, SkAlphaType at); 242 static SkImageInfo Make(SkISize dimensions, SkColorType ct, SkAlphaType at, 243 sk_sp<SkColorSpace> cs); 244 245 /** Creates SkImageInfo from integral dimensions and SkColorInfo colorInfo, 246 247 Parameters are not validated to see if their values are legal, or that the 248 combination is supported. 249 250 @param dimensions pixel column and row count; must be zeros or greater 251 @param SkColorInfo the pixel encoding consisting of SkColorType, SkAlphaType, and 252 SkColorSpace (which may be nullptr) 253 @return created SkImageInfo 254 */ MakeSkImageInfo255 static SkImageInfo Make(SkISize dimensions, const SkColorInfo& colorInfo) { 256 return SkImageInfo(dimensions, colorInfo); 257 } MakeSkImageInfo258 static SkImageInfo Make(SkISize dimensions, SkColorInfo&& colorInfo) { 259 return SkImageInfo(dimensions, std::move(colorInfo)); 260 } 261 262 /** Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType, 263 SkAlphaType at, and optionally SkColorSpace cs. kN32_SkColorType will equal either 264 kBGRA_8888_SkColorType or kRGBA_8888_SkColorType, whichever is optimal. 265 266 If SkColorSpace cs is nullptr and SkImageInfo is part of drawing source: SkColorSpace 267 defaults to sRGB, mapping into SkSurface SkColorSpace. 268 269 Parameters are not validated to see if their values are legal, or that the 270 combination is supported. 271 272 @param width pixel column count; must be zero or greater 273 @param height pixel row count; must be zero or greater 274 @param cs range of colors; may be nullptr 275 @return created SkImageInfo 276 */ 277 static SkImageInfo MakeN32(int width, int height, SkAlphaType at); 278 static SkImageInfo MakeN32(int width, int height, SkAlphaType at, sk_sp<SkColorSpace> cs); 279 280 /** Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType, 281 SkAlphaType at, with sRGB SkColorSpace. 282 283 Parameters are not validated to see if their values are legal, or that the 284 combination is supported. 285 286 @param width pixel column count; must be zero or greater 287 @param height pixel row count; must be zero or greater 288 @return created SkImageInfo 289 290 example: https://fiddle.skia.org/c/@ImageInfo_MakeS32 291 */ 292 static SkImageInfo MakeS32(int width, int height, SkAlphaType at); 293 294 /** Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType, 295 kPremul_SkAlphaType, with optional SkColorSpace. 296 297 If SkColorSpace cs is nullptr and SkImageInfo is part of drawing source: SkColorSpace 298 defaults to sRGB, mapping into SkSurface SkColorSpace. 299 300 Parameters are not validated to see if their values are legal, or that the 301 combination is supported. 302 303 @param width pixel column count; must be zero or greater 304 @param height pixel row count; must be zero or greater 305 @param cs range of colors; may be nullptr 306 @return created SkImageInfo 307 */ 308 static SkImageInfo MakeN32Premul(int width, int height); 309 static SkImageInfo MakeN32Premul(int width, int height, sk_sp<SkColorSpace> cs); 310 311 /** Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType, 312 kPremul_SkAlphaType, with SkColorSpace set to nullptr. 313 314 If SkImageInfo is part of drawing source: SkColorSpace defaults to sRGB, mapping 315 into SkSurface SkColorSpace. 316 317 Parameters are not validated to see if their values are legal, or that the 318 combination is supported. 319 320 @param dimensions width and height, each must be zero or greater 321 @param cs range of colors; may be nullptr 322 @return created SkImageInfo 323 */ 324 static SkImageInfo MakeN32Premul(SkISize dimensions); 325 static SkImageInfo MakeN32Premul(SkISize dimensions, sk_sp<SkColorSpace> cs); 326 327 /** Creates SkImageInfo from integral dimensions width and height, kAlpha_8_SkColorType, 328 kPremul_SkAlphaType, with SkColorSpace set to nullptr. 329 330 @param width pixel column count; must be zero or greater 331 @param height pixel row count; must be zero or greater 332 @return created SkImageInfo 333 */ 334 static SkImageInfo MakeA8(int width, int height); 335 /** Creates SkImageInfo from integral dimensions, kAlpha_8_SkColorType, 336 kPremul_SkAlphaType, with SkColorSpace set to nullptr. 337 338 @param dimensions pixel row and column count; must be zero or greater 339 @return created SkImageInfo 340 */ 341 static SkImageInfo MakeA8(SkISize dimensions); 342 343 /** Creates SkImageInfo from integral dimensions width and height, kUnknown_SkColorType, 344 kUnknown_SkAlphaType, with SkColorSpace set to nullptr. 345 346 Returned SkImageInfo as part of source does not draw, and as part of destination 347 can not be drawn to. 348 349 @param width pixel column count; must be zero or greater 350 @param height pixel row count; must be zero or greater 351 @return created SkImageInfo 352 */ 353 static SkImageInfo MakeUnknown(int width, int height); 354 355 /** Creates SkImageInfo from integral dimensions width and height set to zero, 356 kUnknown_SkColorType, kUnknown_SkAlphaType, with SkColorSpace set to nullptr. 357 358 Returned SkImageInfo as part of source does not draw, and as part of destination 359 can not be drawn to. 360 361 @return created SkImageInfo 362 */ MakeUnknownSkImageInfo363 static SkImageInfo MakeUnknown() { 364 return MakeUnknown(0, 0); 365 } 366 367 /** Returns pixel count in each row. 368 369 @return pixel width 370 */ widthSkImageInfo371 int width() const { return fDimensions.width(); } 372 373 /** Returns pixel row count. 374 375 @return pixel height 376 */ heightSkImageInfo377 int height() const { return fDimensions.height(); } 378 colorTypeSkImageInfo379 SkColorType colorType() const { return fColorInfo.colorType(); } 380 alphaTypeSkImageInfo381 SkAlphaType alphaType() const { return fColorInfo.alphaType(); } 382 383 /** Returns SkColorSpace, the range of colors. The reference count of 384 SkColorSpace is unchanged. The returned SkColorSpace is immutable. 385 386 @return SkColorSpace, or nullptr 387 */ 388 SkColorSpace* colorSpace() const; 389 390 /** Returns smart pointer to SkColorSpace, the range of colors. The smart pointer 391 tracks the number of objects sharing this SkColorSpace reference so the memory 392 is released when the owners destruct. 393 394 The returned SkColorSpace is immutable. 395 396 @return SkColorSpace wrapped in a smart pointer 397 */ 398 sk_sp<SkColorSpace> refColorSpace() const; 399 400 /** Returns if SkImageInfo describes an empty area of pixels by checking if either 401 width or height is zero or smaller. 402 403 @return true if either dimension is zero or smaller 404 */ isEmptySkImageInfo405 bool isEmpty() const { return fDimensions.isEmpty(); } 406 407 /** Returns the dimensionless SkColorInfo that represents the same color type, 408 alpha type, and color space as this SkImageInfo. 409 */ colorInfoSkImageInfo410 const SkColorInfo& colorInfo() const { return fColorInfo; } 411 412 /** Returns true if SkAlphaType is set to hint that all pixels are opaque; their 413 alpha value is implicitly or explicitly 1.0. If true, and all pixels are 414 not opaque, Skia may draw incorrectly. 415 416 Does not check if SkColorType allows alpha, or if any pixel value has 417 transparency. 418 419 @return true if SkAlphaType is kOpaque_SkAlphaType 420 */ isOpaqueSkImageInfo421 bool isOpaque() const { return fColorInfo.isOpaque(); } 422 423 /** Returns SkISize { width(), height() }. 424 425 @return integral size of width() and height() 426 */ dimensionsSkImageInfo427 SkISize dimensions() const { return fDimensions; } 428 429 /** Returns SkIRect { 0, 0, width(), height() }. 430 431 @return integral rectangle from origin to width() and height() 432 */ boundsSkImageInfo433 SkIRect bounds() const { return SkIRect::MakeSize(fDimensions); } 434 435 /** Returns true if associated SkColorSpace is not nullptr, and SkColorSpace gamma 436 is approximately the same as sRGB. 437 This includes the 438 439 @return true if SkColorSpace gamma is approximately the same as sRGB 440 */ gammaCloseToSRGBSkImageInfo441 bool gammaCloseToSRGB() const { return fColorInfo.gammaCloseToSRGB(); } 442 443 /** Creates SkImageInfo with the same SkColorType, SkColorSpace, and SkAlphaType, 444 with dimensions set to width and height. 445 446 @param newWidth pixel column count; must be zero or greater 447 @param newHeight pixel row count; must be zero or greater 448 @return created SkImageInfo 449 */ makeWHSkImageInfo450 SkImageInfo makeWH(int newWidth, int newHeight) const { 451 return Make({newWidth, newHeight}, fColorInfo); 452 } 453 454 /** Creates SkImageInfo with the same SkColorType, SkColorSpace, and SkAlphaType, 455 with dimensions set to newDimensions. 456 457 @param newSize pixel column and row count; must be zero or greater 458 @return created SkImageInfo 459 */ makeDimensionsSkImageInfo460 SkImageInfo makeDimensions(SkISize newSize) const { 461 return Make(newSize, fColorInfo); 462 } 463 464 /** Creates SkImageInfo with same SkColorType, SkColorSpace, width, and height, 465 with SkAlphaType set to newAlphaType. 466 467 Created SkImageInfo contains newAlphaType even if it is incompatible with 468 SkColorType, in which case SkAlphaType in SkImageInfo is ignored. 469 470 @return created SkImageInfo 471 */ makeAlphaTypeSkImageInfo472 SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const { 473 return Make(fDimensions, fColorInfo.makeAlphaType(newAlphaType)); 474 } 475 476 /** Creates SkImageInfo with same SkAlphaType, SkColorSpace, width, and height, 477 with SkColorType set to newColorType. 478 479 @return created SkImageInfo 480 */ makeColorTypeSkImageInfo481 SkImageInfo makeColorType(SkColorType newColorType) const { 482 return Make(fDimensions, fColorInfo.makeColorType(newColorType)); 483 } 484 485 /** Creates SkImageInfo with same SkAlphaType, SkColorType, width, and height, 486 with SkColorSpace set to cs. 487 488 @param cs range of colors; may be nullptr 489 @return created SkImageInfo 490 */ 491 SkImageInfo makeColorSpace(sk_sp<SkColorSpace> cs) const; 492 493 /** Returns number of bytes per pixel required by SkColorType. 494 Returns zero if colorType( is kUnknown_SkColorType. 495 496 @return bytes in pixel 497 */ bytesPerPixelSkImageInfo498 int bytesPerPixel() const { return fColorInfo.bytesPerPixel(); } 499 500 /** Returns bit shift converting row bytes to row pixels. 501 Returns zero for kUnknown_SkColorType. 502 503 @return one of: 0, 1, 2, 3; left shift to convert pixels to bytes 504 */ shiftPerPixelSkImageInfo505 int shiftPerPixel() const { return fColorInfo.shiftPerPixel(); } 506 507 /** Returns minimum bytes per row, computed from pixel width() and SkColorType, which 508 specifies bytesPerPixel(). SkBitmap maximum value for row bytes must fit 509 in 31 bits. 510 511 @return width() times bytesPerPixel() as unsigned 64-bit integer 512 */ minRowBytes64SkImageInfo513 uint64_t minRowBytes64() const { 514 return (uint64_t)sk_64_mul(this->width(), this->bytesPerPixel()); 515 } 516 517 /** Returns minimum bytes per row, computed from pixel width() and SkColorType, which 518 specifies bytesPerPixel(). SkBitmap maximum value for row bytes must fit 519 in 31 bits. 520 521 @return width() times bytesPerPixel() as size_t 522 */ minRowBytesSkImageInfo523 size_t minRowBytes() const { 524 uint64_t minRowBytes = this->minRowBytes64(); 525 if (!SkTFitsIn<int32_t>(minRowBytes)) { 526 return 0; 527 } 528 return (size_t)minRowBytes; 529 } 530 531 /** Returns byte offset of pixel from pixel base address. 532 533 Asserts in debug build if x or y is outside of bounds. Does not assert if 534 rowBytes is smaller than minRowBytes(), even though result may be incorrect. 535 536 @param x column index, zero or greater, and less than width() 537 @param y row index, zero or greater, and less than height() 538 @param rowBytes size of pixel row or larger 539 @return offset within pixel array 540 541 example: https://fiddle.skia.org/c/@ImageInfo_computeOffset 542 */ 543 size_t computeOffset(int x, int y, size_t rowBytes) const; 544 545 /** Compares SkImageInfo with other, and returns true if width, height, SkColorType, 546 SkAlphaType, and SkColorSpace are equivalent. 547 548 @param other SkImageInfo to compare 549 @return true if SkImageInfo equals other 550 */ 551 bool operator==(const SkImageInfo& other) const { 552 return fDimensions == other.fDimensions && fColorInfo == other.fColorInfo; 553 } 554 555 /** Compares SkImageInfo with other, and returns true if width, height, SkColorType, 556 SkAlphaType, and SkColorSpace are not equivalent. 557 558 @param other SkImageInfo to compare 559 @return true if SkImageInfo is not equal to other 560 */ 561 bool operator!=(const SkImageInfo& other) const { 562 return !(*this == other); 563 } 564 565 /** Returns storage required by pixel array, given SkImageInfo dimensions, SkColorType, 566 and rowBytes. rowBytes is assumed to be at least as large as minRowBytes(). 567 568 Returns zero if height is zero. 569 Returns SIZE_MAX if answer exceeds the range of size_t. 570 571 @param rowBytes size of pixel row or larger 572 @return memory required by pixel buffer 573 */ 574 size_t computeByteSize(size_t rowBytes) const; 575 576 /** Returns storage required by pixel array, given SkImageInfo dimensions, and 577 SkColorType. Uses minRowBytes() to compute bytes for pixel row. 578 579 Returns zero if height is zero. 580 Returns SIZE_MAX if answer exceeds the range of size_t. 581 582 @return least memory required by pixel buffer 583 */ computeMinByteSizeSkImageInfo584 size_t computeMinByteSize() const { 585 return this->computeByteSize(this->minRowBytes()); 586 } 587 588 /** Returns true if byteSize equals SIZE_MAX. computeByteSize() and 589 computeMinByteSize() return SIZE_MAX if size_t can not hold buffer size. 590 591 @param byteSize result of computeByteSize() or computeMinByteSize() 592 @return true if computeByteSize() or computeMinByteSize() result exceeds size_t 593 */ ByteSizeOverflowedSkImageInfo594 static bool ByteSizeOverflowed(size_t byteSize) { 595 return SIZE_MAX == byteSize; 596 } 597 598 /** Returns true if rowBytes is valid for this SkImageInfo. 599 600 @param rowBytes size of pixel row including padding 601 @return true if rowBytes is large enough to contain pixel row and is properly 602 aligned 603 */ validRowBytesSkImageInfo604 bool validRowBytes(size_t rowBytes) const { 605 if (rowBytes < this->minRowBytes64()) { 606 return false; 607 } 608 int shift = this->shiftPerPixel(); 609 size_t alignedRowBytes = rowBytes >> shift << shift; 610 return alignedRowBytes == rowBytes; 611 } 612 613 /** Creates an empty SkImageInfo with kUnknown_SkColorType, kUnknown_SkAlphaType, 614 a width and height of zero, and no SkColorSpace. 615 */ resetSkImageInfo616 void reset() { *this = {}; } 617 618 /** Asserts if internal values are illegal or inconsistent. Only available if 619 SK_DEBUG is defined at compile time. 620 */ 621 SkDEBUGCODE(void validate() const;) 622 623 private: 624 SkColorInfo fColorInfo; 625 SkISize fDimensions = {0, 0}; 626 SkImageInfoSkImageInfo627 SkImageInfo(SkISize dimensions, const SkColorInfo& colorInfo) 628 : fColorInfo(colorInfo), fDimensions(dimensions) {} 629 SkImageInfoSkImageInfo630 SkImageInfo(SkISize dimensions, SkColorInfo&& colorInfo) 631 : fColorInfo(std::move(colorInfo)), fDimensions(dimensions) {} 632 }; 633 634 #endif 635