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