1 /* 2 * Copyright 2015 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 SkPixmap_DEFINED 9 #define SkPixmap_DEFINED 10 11 #include "include/core/SkColor.h" 12 #include "include/core/SkColorType.h" 13 #include "include/core/SkImageInfo.h" 14 #include "include/core/SkRect.h" 15 #include "include/core/SkRefCnt.h" 16 #include "include/core/SkSamplingOptions.h" 17 #include "include/core/SkSize.h" 18 #include "include/private/base/SkAPI.h" 19 #include "include/private/base/SkAssert.h" 20 21 #include <cstddef> 22 #include <cstdint> 23 24 class SkColorSpace; 25 enum SkAlphaType : int; 26 struct SkMask; 27 28 /** \class SkPixmap 29 SkPixmap provides a utility to pair SkImageInfo with pixels and row bytes. 30 SkPixmap is a low level class which provides convenience functions to access 31 raster destinations. SkCanvas can not draw SkPixmap, nor does SkPixmap provide 32 a direct drawing destination. 33 34 Use SkBitmap to draw pixels referenced by SkPixmap; use SkSurface to draw into 35 pixels referenced by SkPixmap. 36 37 SkPixmap does not try to manage the lifetime of the pixel memory. Use SkPixelRef 38 to manage pixel memory; SkPixelRef is safe across threads. 39 */ 40 class SK_API SkPixmap { 41 public: 42 43 /** Creates an empty SkPixmap without pixels, with kUnknown_SkColorType, with 44 kUnknown_SkAlphaType, and with a width and height of zero. Use 45 reset() to associate pixels, SkColorType, SkAlphaType, width, and height 46 after SkPixmap has been created. 47 48 @return empty SkPixmap 49 */ SkPixmap()50 SkPixmap() 51 : fPixels(nullptr), fRowBytes(0), fInfo(SkImageInfo::MakeUnknown(0, 0)) 52 {} 53 54 /** Creates SkPixmap from info width, height, SkAlphaType, and SkColorType. 55 addr points to pixels, or nullptr. rowBytes should be info.width() times 56 info.bytesPerPixel(), or larger. 57 58 No parameter checking is performed; it is up to the caller to ensure that 59 addr and rowBytes agree with info. 60 61 The memory lifetime of pixels is managed by the caller. When SkPixmap goes 62 out of scope, addr is unaffected. 63 64 SkPixmap may be later modified by reset() to change its size, pixel type, or 65 storage. 66 67 @param info width, height, SkAlphaType, SkColorType of SkImageInfo 68 @param addr pointer to pixels allocated by caller; may be nullptr 69 @param rowBytes size of one row of addr; width times pixel size, or larger 70 @return initialized SkPixmap 71 */ SkPixmap(const SkImageInfo & info,const void * addr,size_t rowBytes)72 SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes) 73 : fPixels(addr), fRowBytes(rowBytes), fInfo(info) 74 {} 75 76 /** Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to 77 kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType. 78 79 The prior pixels are unaffected; it is up to the caller to release pixels 80 memory if desired. 81 82 example: https://fiddle.skia.org/c/@Pixmap_reset 83 */ 84 void reset(); 85 86 /** Sets width, height, SkAlphaType, and SkColorType from info. 87 Sets pixel address from addr, which may be nullptr. 88 Sets row bytes from rowBytes, which should be info.width() times 89 info.bytesPerPixel(), or larger. 90 91 Does not check addr. Asserts if built with SK_DEBUG defined and if rowBytes is 92 too small to hold one row of pixels. 93 94 The memory lifetime pixels are managed by the caller. When SkPixmap goes 95 out of scope, addr is unaffected. 96 97 @param info width, height, SkAlphaType, SkColorType of SkImageInfo 98 @param addr pointer to pixels allocated by caller; may be nullptr 99 @param rowBytes size of one row of addr; width times pixel size, or larger 100 101 example: https://fiddle.skia.org/c/@Pixmap_reset_2 102 */ 103 void reset(const SkImageInfo& info, const void* addr, size_t rowBytes); 104 105 /** Changes SkColorSpace in SkImageInfo; preserves width, height, SkAlphaType, and 106 SkColorType in SkImage, and leaves pixel address and row bytes unchanged. 107 SkColorSpace reference count is incremented. 108 109 @param colorSpace SkColorSpace moved to SkImageInfo 110 111 example: https://fiddle.skia.org/c/@Pixmap_setColorSpace 112 */ 113 void setColorSpace(sk_sp<SkColorSpace> colorSpace); 114 115 /** Deprecated. 116 */ 117 [[nodiscard]] bool reset(const SkMask& mask); 118 119 /** Sets subset width, height, pixel address to intersection of SkPixmap with area, 120 if intersection is not empty; and return true. Otherwise, leave subset unchanged 121 and return false. 122 123 Failing to read the return value generates a compile time warning. 124 125 @param subset storage for width, height, pixel address of intersection 126 @param area bounds to intersect with SkPixmap 127 @return true if intersection of SkPixmap and area is not empty 128 */ 129 [[nodiscard]] bool extractSubset(SkPixmap* subset, const SkIRect& area) const; 130 131 /** Returns width, height, SkAlphaType, SkColorType, and SkColorSpace. 132 133 @return reference to SkImageInfo 134 */ info()135 const SkImageInfo& info() const { return fInfo; } 136 137 /** Returns row bytes, the interval from one pixel row to the next. Row bytes 138 is at least as large as: width() * info().bytesPerPixel(). 139 140 Returns zero if colorType() is kUnknown_SkColorType. 141 It is up to the SkBitmap creator to ensure that row bytes is a useful value. 142 143 @return byte length of pixel row 144 */ rowBytes()145 size_t rowBytes() const { return fRowBytes; } 146 147 /** Returns pixel address, the base address corresponding to the pixel origin. 148 149 It is up to the SkPixmap creator to ensure that pixel address is a useful value. 150 151 @return pixel address 152 */ addr()153 const void* addr() const { return fPixels; } 154 155 /** Returns pixel count in each pixel row. Should be equal or less than: 156 rowBytes() / info().bytesPerPixel(). 157 158 @return pixel width in SkImageInfo 159 */ width()160 int width() const { return fInfo.width(); } 161 162 /** Returns pixel row count. 163 164 @return pixel height in SkImageInfo 165 */ height()166 int height() const { return fInfo.height(); } 167 168 /** 169 * Return the dimensions of the pixmap (from its ImageInfo) 170 */ dimensions()171 SkISize dimensions() const { return fInfo.dimensions(); } 172 colorType()173 SkColorType colorType() const { return fInfo.colorType(); } 174 alphaType()175 SkAlphaType alphaType() const { return fInfo.alphaType(); } 176 177 /** Returns SkColorSpace, the range of colors, associated with SkImageInfo. The 178 reference count of SkColorSpace is unchanged. The returned SkColorSpace is 179 immutable. 180 181 @return SkColorSpace in SkImageInfo, or nullptr 182 */ 183 SkColorSpace* colorSpace() const; 184 185 /** Returns smart pointer to SkColorSpace, the range of colors, associated with 186 SkImageInfo. The smart pointer tracks the number of objects sharing this 187 SkColorSpace reference so the memory is released when the owners destruct. 188 189 The returned SkColorSpace is immutable. 190 191 @return SkColorSpace in SkImageInfo wrapped in a smart pointer 192 */ 193 sk_sp<SkColorSpace> refColorSpace() const; 194 195 /** Returns true if SkAlphaType is kOpaque_SkAlphaType. 196 Does not check if SkColorType allows alpha, or if any pixel value has 197 transparency. 198 199 @return true if SkImageInfo has opaque SkAlphaType 200 */ isOpaque()201 bool isOpaque() const { return fInfo.isOpaque(); } 202 203 /** Returns SkIRect { 0, 0, width(), height() }. 204 205 @return integral rectangle from origin to width() and height() 206 */ bounds()207 SkIRect bounds() const { return SkIRect::MakeWH(this->width(), this->height()); } 208 209 /** Returns number of pixels that fit on row. Should be greater than or equal to 210 width(). 211 212 @return maximum pixels per row 213 */ rowBytesAsPixels()214 int rowBytesAsPixels() const { return int(fRowBytes >> this->shiftPerPixel()); } 215 216 /** Returns bit shift converting row bytes to row pixels. 217 Returns zero for kUnknown_SkColorType. 218 219 @return one of: 0, 1, 2, 3; left shift to convert pixels to bytes 220 */ shiftPerPixel()221 int shiftPerPixel() const { return fInfo.shiftPerPixel(); } 222 223 /** Returns minimum memory required for pixel storage. 224 Does not include unused memory on last row when rowBytesAsPixels() exceeds width(). 225 Returns SIZE_MAX if result does not fit in size_t. 226 Returns zero if height() or width() is 0. 227 Returns height() times rowBytes() if colorType() is kUnknown_SkColorType. 228 229 @return size in bytes of image buffer 230 */ computeByteSize()231 size_t computeByteSize() const { return fInfo.computeByteSize(fRowBytes); } 232 233 /** Returns true if all pixels are opaque. SkColorType determines how pixels 234 are encoded, and whether pixel describes alpha. Returns true for SkColorType 235 without alpha in each pixel; for other SkColorType, returns true if all 236 pixels have alpha values equivalent to 1.0 or greater. 237 238 For SkColorType kRGB_565_SkColorType or kGray_8_SkColorType: always 239 returns true. For SkColorType kAlpha_8_SkColorType, kBGRA_8888_SkColorType, 240 kRGBA_8888_SkColorType: returns true if all pixel alpha values are 255. 241 For SkColorType kARGB_4444_SkColorType: returns true if all pixel alpha values are 15. 242 For kRGBA_F16_SkColorType: returns true if all pixel alpha values are 1.0 or 243 greater. 244 245 Returns false for kUnknown_SkColorType. 246 247 @return true if all pixels have opaque values or SkColorType is opaque 248 249 example: https://fiddle.skia.org/c/@Pixmap_computeIsOpaque 250 */ 251 bool computeIsOpaque() const; 252 253 /** Returns pixel at (x, y) as unpremultiplied color. 254 Returns black with alpha if SkColorType is kAlpha_8_SkColorType. 255 256 Input is not validated: out of bounds values of x or y trigger an assert() if 257 built with SK_DEBUG defined; and returns undefined values or may crash if 258 SK_RELEASE is defined. Fails if SkColorType is kUnknown_SkColorType or 259 pixel address is nullptr. 260 261 SkColorSpace in SkImageInfo is ignored. Some color precision may be lost in the 262 conversion to unpremultiplied color; original pixel data may have additional 263 precision. 264 265 @param x column index, zero or greater, and less than width() 266 @param y row index, zero or greater, and less than height() 267 @return pixel converted to unpremultiplied color 268 269 example: https://fiddle.skia.org/c/@Pixmap_getColor 270 */ 271 SkColor getColor(int x, int y) const; 272 273 /** Returns pixel at (x, y) as unpremultiplied color as an SkColor4f. 274 Returns black with alpha if SkColorType is kAlpha_8_SkColorType. 275 276 Input is not validated: out of bounds values of x or y trigger an assert() if 277 built with SK_DEBUG defined; and returns undefined values or may crash if 278 SK_RELEASE is defined. Fails if SkColorType is kUnknown_SkColorType or 279 pixel address is nullptr. 280 281 SkColorSpace in SkImageInfo is ignored. Some color precision may be lost in the 282 conversion to unpremultiplied color; original pixel data may have additional 283 precision, though this is less likely than for getColor(). Rounding errors may 284 occur if the underlying type has lower precision. 285 286 @param x column index, zero or greater, and less than width() 287 @param y row index, zero or greater, and less than height() 288 @return pixel converted to unpremultiplied float color 289 */ 290 SkColor4f getColor4f(int x, int y) const; 291 292 /** Look up the pixel at (x,y) and return its alpha component, normalized to [0..1]. 293 This is roughly equivalent to SkGetColorA(getColor()), but can be more efficent 294 (and more precise if the pixels store more than 8 bits per component). 295 296 @param x column index, zero or greater, and less than width() 297 @param y row index, zero or greater, and less than height() 298 @return alpha converted to normalized float 299 */ 300 float getAlphaf(int x, int y) const; 301 302 /** Returns readable pixel address at (x, y). Returns nullptr if SkPixelRef is nullptr. 303 304 Input is not validated: out of bounds values of x or y trigger an assert() if 305 built with SK_DEBUG defined. Returns nullptr if SkColorType is kUnknown_SkColorType. 306 307 Performs a lookup of pixel size; for better performance, call 308 one of: addr8, addr16, addr32, addr64, or addrF16(). 309 310 @param x column index, zero or greater, and less than width() 311 @param y row index, zero or greater, and less than height() 312 @return readable generic pointer to pixel 313 */ addr(int x,int y)314 const void* addr(int x, int y) const { 315 return (const char*)fPixels + fInfo.computeOffset(x, y, fRowBytes); 316 } 317 318 /** Returns readable base pixel address. Result is addressable as unsigned 8-bit bytes. 319 Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType or 320 kGray_8_SkColorType, and is built with SK_DEBUG defined. 321 322 One byte corresponds to one pixel. 323 324 @return readable unsigned 8-bit pointer to pixels 325 */ addr8()326 const uint8_t* addr8() const { 327 SkASSERT(1 == fInfo.bytesPerPixel()); 328 return reinterpret_cast<const uint8_t*>(fPixels); 329 } 330 331 /** Returns readable base pixel address. Result is addressable as unsigned 16-bit words. 332 Will trigger an assert() if SkColorType is not kRGB_565_SkColorType or 333 kARGB_4444_SkColorType, and is built with SK_DEBUG defined. 334 335 One word corresponds to one pixel. 336 337 @return readable unsigned 16-bit pointer to pixels 338 */ addr16()339 const uint16_t* addr16() const { 340 SkASSERT(2 == fInfo.bytesPerPixel()); 341 return reinterpret_cast<const uint16_t*>(fPixels); 342 } 343 344 /** Returns readable base pixel address. Result is addressable as unsigned 32-bit words. 345 Will trigger an assert() if SkColorType is not kRGBA_8888_SkColorType or 346 kBGRA_8888_SkColorType, and is built with SK_DEBUG defined. 347 348 One word corresponds to one pixel. 349 350 @return readable unsigned 32-bit pointer to pixels 351 */ addr32()352 const uint32_t* addr32() const { 353 SkASSERT(4 == fInfo.bytesPerPixel()); 354 return reinterpret_cast<const uint32_t*>(fPixels); 355 } 356 357 /** Returns readable base pixel address. Result is addressable as unsigned 64-bit words. 358 Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built 359 with SK_DEBUG defined. 360 361 One word corresponds to one pixel. 362 363 @return readable unsigned 64-bit pointer to pixels 364 */ addr64()365 const uint64_t* addr64() const { 366 SkASSERT(8 == fInfo.bytesPerPixel()); 367 return reinterpret_cast<const uint64_t*>(fPixels); 368 } 369 370 /** Returns readable base pixel address. Result is addressable as unsigned 16-bit words. 371 Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built 372 with SK_DEBUG defined. 373 374 Each word represents one color component encoded as a half float. 375 Four words correspond to one pixel. 376 377 @return readable unsigned 16-bit pointer to first component of pixels 378 */ addrF16()379 const uint16_t* addrF16() const { 380 SkASSERT(8 == fInfo.bytesPerPixel()); 381 SkASSERT(kRGBA_F16_SkColorType == fInfo.colorType() || 382 kRGBA_F16Norm_SkColorType == fInfo.colorType()); 383 return reinterpret_cast<const uint16_t*>(fPixels); 384 } 385 386 /** Returns readable pixel address at (x, y). 387 388 Input is not validated: out of bounds values of x or y trigger an assert() if 389 built with SK_DEBUG defined. 390 391 Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType or 392 kGray_8_SkColorType, and is built with SK_DEBUG defined. 393 394 @param x column index, zero or greater, and less than width() 395 @param y row index, zero or greater, and less than height() 396 @return readable unsigned 8-bit pointer to pixel at (x, y) 397 */ addr8(int x,int y)398 const uint8_t* addr8(int x, int y) const { 399 SkASSERT((unsigned)x < (unsigned)fInfo.width()); 400 SkASSERT((unsigned)y < (unsigned)fInfo.height()); 401 return (const uint8_t*)((const char*)this->addr8() + (size_t)y * fRowBytes + (x << 0)); 402 } 403 404 /** Returns readable pixel address at (x, y). 405 406 Input is not validated: out of bounds values of x or y trigger an assert() if 407 built with SK_DEBUG defined. 408 409 Will trigger an assert() if SkColorType is not kRGB_565_SkColorType or 410 kARGB_4444_SkColorType, and is built with SK_DEBUG defined. 411 412 @param x column index, zero or greater, and less than width() 413 @param y row index, zero or greater, and less than height() 414 @return readable unsigned 16-bit pointer to pixel at (x, y) 415 */ addr16(int x,int y)416 const uint16_t* addr16(int x, int y) const { 417 SkASSERT((unsigned)x < (unsigned)fInfo.width()); 418 SkASSERT((unsigned)y < (unsigned)fInfo.height()); 419 return (const uint16_t*)((const char*)this->addr16() + (size_t)y * fRowBytes + (x << 1)); 420 } 421 422 /** Returns readable pixel address at (x, y). 423 424 Input is not validated: out of bounds values of x or y trigger an assert() if 425 built with SK_DEBUG defined. 426 427 Will trigger an assert() if SkColorType is not kRGBA_8888_SkColorType or 428 kBGRA_8888_SkColorType, and is built with SK_DEBUG defined. 429 430 @param x column index, zero or greater, and less than width() 431 @param y row index, zero or greater, and less than height() 432 @return readable unsigned 32-bit pointer to pixel at (x, y) 433 */ addr32(int x,int y)434 const uint32_t* addr32(int x, int y) const { 435 SkASSERT((unsigned)x < (unsigned)fInfo.width()); 436 SkASSERT((unsigned)y < (unsigned)fInfo.height()); 437 return (const uint32_t*)((const char*)this->addr32() + (size_t)y * fRowBytes + (x << 2)); 438 } 439 440 /** Returns readable pixel address at (x, y). 441 442 Input is not validated: out of bounds values of x or y trigger an assert() if 443 built with SK_DEBUG defined. 444 445 Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built 446 with SK_DEBUG defined. 447 448 @param x column index, zero or greater, and less than width() 449 @param y row index, zero or greater, and less than height() 450 @return readable unsigned 64-bit pointer to pixel at (x, y) 451 */ addr64(int x,int y)452 const uint64_t* addr64(int x, int y) const { 453 SkASSERT((unsigned)x < (unsigned)fInfo.width()); 454 SkASSERT((unsigned)y < (unsigned)fInfo.height()); 455 return (const uint64_t*)((const char*)this->addr64() + (size_t)y * fRowBytes + (x << 3)); 456 } 457 458 /** Returns readable pixel address at (x, y). 459 460 Input is not validated: out of bounds values of x or y trigger an assert() if 461 built with SK_DEBUG defined. 462 463 Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built 464 with SK_DEBUG defined. 465 466 Each unsigned 16-bit word represents one color component encoded as a half float. 467 Four words correspond to one pixel. 468 469 @param x column index, zero or greater, and less than width() 470 @param y row index, zero or greater, and less than height() 471 @return readable unsigned 16-bit pointer to pixel component at (x, y) 472 */ addrF16(int x,int y)473 const uint16_t* addrF16(int x, int y) const { 474 SkASSERT(kRGBA_F16_SkColorType == fInfo.colorType() || 475 kRGBA_F16Norm_SkColorType == fInfo.colorType()); 476 return reinterpret_cast<const uint16_t*>(this->addr64(x, y)); 477 } 478 479 /** Returns writable base pixel address. 480 481 @return writable generic base pointer to pixels 482 */ writable_addr()483 void* writable_addr() const { return const_cast<void*>(fPixels); } 484 485 /** Returns writable pixel address at (x, y). 486 487 Input is not validated: out of bounds values of x or y trigger an assert() if 488 built with SK_DEBUG defined. Returns zero if SkColorType is kUnknown_SkColorType. 489 490 @param x column index, zero or greater, and less than width() 491 @param y row index, zero or greater, and less than height() 492 @return writable generic pointer to pixel 493 */ writable_addr(int x,int y)494 void* writable_addr(int x, int y) const { 495 return const_cast<void*>(this->addr(x, y)); 496 } 497 498 /** Returns writable pixel address at (x, y). Result is addressable as unsigned 499 8-bit bytes. Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType 500 or kGray_8_SkColorType, and is built with SK_DEBUG defined. 501 502 One byte corresponds to one pixel. 503 504 @param x column index, zero or greater, and less than width() 505 @param y row index, zero or greater, and less than height() 506 @return writable unsigned 8-bit pointer to pixels 507 */ writable_addr8(int x,int y)508 uint8_t* writable_addr8(int x, int y) const { 509 return const_cast<uint8_t*>(this->addr8(x, y)); 510 } 511 512 /** Returns writable_addr pixel address at (x, y). Result is addressable as unsigned 513 16-bit words. Will trigger an assert() if SkColorType is not kRGB_565_SkColorType 514 or kARGB_4444_SkColorType, and is built with SK_DEBUG defined. 515 516 One word corresponds to one pixel. 517 518 @param x column index, zero or greater, and less than width() 519 @param y row index, zero or greater, and less than height() 520 @return writable unsigned 16-bit pointer to pixel 521 */ writable_addr16(int x,int y)522 uint16_t* writable_addr16(int x, int y) const { 523 return const_cast<uint16_t*>(this->addr16(x, y)); 524 } 525 526 /** Returns writable pixel address at (x, y). Result is addressable as unsigned 527 32-bit words. Will trigger an assert() if SkColorType is not 528 kRGBA_8888_SkColorType or kBGRA_8888_SkColorType, and is built with SK_DEBUG 529 defined. 530 531 One word corresponds to one pixel. 532 533 @param x column index, zero or greater, and less than width() 534 @param y row index, zero or greater, and less than height() 535 @return writable unsigned 32-bit pointer to pixel 536 */ writable_addr32(int x,int y)537 uint32_t* writable_addr32(int x, int y) const { 538 return const_cast<uint32_t*>(this->addr32(x, y)); 539 } 540 541 /** Returns writable pixel address at (x, y). Result is addressable as unsigned 542 64-bit words. Will trigger an assert() if SkColorType is not 543 kRGBA_F16_SkColorType and is built with SK_DEBUG defined. 544 545 One word corresponds to one pixel. 546 547 @param x column index, zero or greater, and less than width() 548 @param y row index, zero or greater, and less than height() 549 @return writable unsigned 64-bit pointer to pixel 550 */ writable_addr64(int x,int y)551 uint64_t* writable_addr64(int x, int y) const { 552 return const_cast<uint64_t*>(this->addr64(x, y)); 553 } 554 555 /** Returns writable pixel address at (x, y). Result is addressable as unsigned 556 16-bit words. Will trigger an assert() if SkColorType is not 557 kRGBA_F16_SkColorType and is built with SK_DEBUG defined. 558 559 Each word represents one color component encoded as a half float. 560 Four words correspond to one pixel. 561 562 @param x column index, zero or greater, and less than width() 563 @param y row index, zero or greater, and less than height() 564 @return writable unsigned 16-bit pointer to first component of pixel 565 */ writable_addrF16(int x,int y)566 uint16_t* writable_addrF16(int x, int y) const { 567 return reinterpret_cast<uint16_t*>(writable_addr64(x, y)); 568 } 569 570 /** Copies a SkRect of pixels to dstPixels. Copy starts at (0, 0), and does not 571 exceed SkPixmap (width(), height()). 572 573 dstInfo specifies width, height, SkColorType, SkAlphaType, and 574 SkColorSpace of destination. dstRowBytes specifics the gap from one destination 575 row to the next. Returns true if pixels are copied. Returns false if 576 dstInfo address equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes(). 577 578 Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is 579 kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match. 580 If SkPixmap colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match. 581 If SkPixmap alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must 582 match. If SkPixmap colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns 583 false if pixel conversion is not possible. 584 585 Returns false if SkPixmap width() or height() is zero or negative. 586 587 @param dstInfo destination width, height, SkColorType, SkAlphaType, SkColorSpace 588 @param dstPixels destination pixel storage 589 @param dstRowBytes destination row length 590 @return true if pixels are copied to dstPixels 591 */ readPixels(const SkImageInfo & dstInfo,void * dstPixels,size_t dstRowBytes)592 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes) const { 593 return this->readPixels(dstInfo, dstPixels, dstRowBytes, 0, 0); 594 } 595 596 /** Copies a SkRect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not 597 exceed SkPixmap (width(), height()). 598 599 dstInfo specifies width, height, SkColorType, SkAlphaType, and 600 SkColorSpace of destination. dstRowBytes specifics the gap from one destination 601 row to the next. Returns true if pixels are copied. Returns false if 602 dstInfo address equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes(). 603 604 Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is 605 kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match. 606 If SkPixmap colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match. 607 If SkPixmap alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must 608 match. If SkPixmap colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns 609 false if pixel conversion is not possible. 610 611 srcX and srcY may be negative to copy only top or left of source. Returns 612 false if SkPixmap width() or height() is zero or negative. Returns false if: 613 abs(srcX) >= Pixmap width(), or if abs(srcY) >= Pixmap height(). 614 615 @param dstInfo destination width, height, SkColorType, SkAlphaType, SkColorSpace 616 @param dstPixels destination pixel storage 617 @param dstRowBytes destination row length 618 @param srcX column index whose absolute value is less than width() 619 @param srcY row index whose absolute value is less than height() 620 @return true if pixels are copied to dstPixels 621 */ 622 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, int srcX, 623 int srcY) const; 624 625 /** Copies a SkRect of pixels to dst. Copy starts at (srcX, srcY), and does not 626 exceed SkPixmap (width(), height()). dst specifies width, height, SkColorType, 627 SkAlphaType, and SkColorSpace of destination. Returns true if pixels are copied. 628 Returns false if dst address equals nullptr, or dst.rowBytes() is less than 629 dst SkImageInfo::minRowBytes. 630 631 Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is 632 kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.info().colorType must match. 633 If SkPixmap colorType() is kGray_8_SkColorType, dst.info().colorSpace must match. 634 If SkPixmap alphaType() is kOpaque_SkAlphaType, dst.info().alphaType must 635 match. If SkPixmap colorSpace() is nullptr, dst.info().colorSpace must match. Returns 636 false if pixel conversion is not possible. 637 638 srcX and srcY may be negative to copy only top or left of source. Returns 639 false SkPixmap width() or height() is zero or negative. Returns false if: 640 abs(srcX) >= Pixmap width(), or if abs(srcY) >= Pixmap height(). 641 642 @param dst SkImageInfo and pixel address to write to 643 @param srcX column index whose absolute value is less than width() 644 @param srcY row index whose absolute value is less than height() 645 @return true if pixels are copied to dst 646 */ readPixels(const SkPixmap & dst,int srcX,int srcY)647 bool readPixels(const SkPixmap& dst, int srcX, int srcY) const { 648 return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), srcX, srcY); 649 } 650 651 /** Copies pixels inside bounds() to dst. dst specifies width, height, SkColorType, 652 SkAlphaType, and SkColorSpace of destination. Returns true if pixels are copied. 653 Returns false if dst address equals nullptr, or dst.rowBytes() is less than 654 dst SkImageInfo::minRowBytes. 655 656 Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is 657 kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match. 658 If SkPixmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match. 659 If SkPixmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must 660 match. If SkPixmap colorSpace() is nullptr, dst SkColorSpace must match. Returns 661 false if pixel conversion is not possible. 662 663 Returns false if SkPixmap width() or height() is zero or negative. 664 665 @param dst SkImageInfo and pixel address to write to 666 @return true if pixels are copied to dst 667 */ readPixels(const SkPixmap & dst)668 bool readPixels(const SkPixmap& dst) const { 669 return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), 0, 0); 670 } 671 672 /** Copies SkBitmap to dst, scaling pixels to fit dst.width() and dst.height(), and 673 converting pixels to match dst.colorType() and dst.alphaType(). Returns true if 674 pixels are copied. Returns false if dst address is nullptr, or dst.rowBytes() is 675 less than dst SkImageInfo::minRowBytes. 676 677 Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is 678 kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match. 679 If SkPixmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match. 680 If SkPixmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must 681 match. If SkPixmap colorSpace() is nullptr, dst SkColorSpace must match. Returns 682 false if pixel conversion is not possible. 683 684 Returns false if SkBitmap width() or height() is zero or negative. 685 686 @param dst SkImageInfo and pixel address to write to 687 @return true if pixels are scaled to fit dst 688 689 example: https://fiddle.skia.org/c/@Pixmap_scalePixels 690 */ 691 bool scalePixels(const SkPixmap& dst, const SkSamplingOptions&) const; 692 693 /** Writes color to pixels bounded by subset; returns true on success. 694 Returns false if colorType() is kUnknown_SkColorType, or if subset does 695 not intersect bounds(). 696 697 @param color sRGB unpremultiplied color to write 698 @param subset bounding integer SkRect of written pixels 699 @return true if pixels are changed 700 701 example: https://fiddle.skia.org/c/@Pixmap_erase 702 */ 703 bool erase(SkColor color, const SkIRect& subset) const; 704 705 /** Writes color to pixels inside bounds(); returns true on success. 706 Returns false if colorType() is kUnknown_SkColorType, or if bounds() 707 is empty. 708 709 @param color sRGB unpremultiplied color to write 710 @return true if pixels are changed 711 */ erase(SkColor color)712 bool erase(SkColor color) const { return this->erase(color, this->bounds()); } 713 714 /** Writes color to pixels bounded by subset; returns true on success. 715 if subset is nullptr, writes colors pixels inside bounds(). Returns false if 716 colorType() is kUnknown_SkColorType, if subset is not nullptr and does 717 not intersect bounds(), or if subset is nullptr and bounds() is empty. 718 719 @param color unpremultiplied color to write 720 @param subset bounding integer SkRect of pixels to write; may be nullptr 721 @return true if pixels are changed 722 */ 723 bool erase(const SkColor4f& color, const SkIRect* subset = nullptr) const; 724 725 private: 726 const void* fPixels; 727 size_t fRowBytes; 728 SkImageInfo fInfo; 729 }; 730 731 #endif 732