Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | List of all members
SkPixmap Class Reference

#include <SkPixmap.h>

Inheritance diagram for SkPixmap:
SkAutoPixmapStorage

Public Member Functions

 SkPixmap ()
 
 SkPixmap (const SkImageInfo &info, const void *addr, size_t rowBytes)
 
void reset ()
 
void reset (const SkImageInfo &info, const void *addr, size_t rowBytes)
 
void setColorSpace (sk_sp< SkColorSpace > colorSpace)
 
bool reset (const SkMask &mask)
 
bool extractSubset (SkPixmap *subset, const SkIRect &area) const
 
const SkImageInfoinfo () const
 
size_t rowBytes () const
 
const void * addr () const
 
int width () const
 
int height () const
 
SkISize dimensions () const
 
SkColorType colorType () const
 
SkAlphaType alphaType () const
 
SkColorSpacecolorSpace () const
 
sk_sp< SkColorSpacerefColorSpace () const
 
bool isOpaque () const
 
SkIRect bounds () const
 
int rowBytesAsPixels () const
 
int shiftPerPixel () const
 
size_t computeByteSize () const
 
bool computeIsOpaque () const
 
SkColor getColor (int x, int y) const
 
SkColor4f getColor4f (int x, int y) const
 
float getAlphaf (int x, int y) const
 
const void * addr (int x, int y) const
 
const uint8_t * addr8 () const
 
const uint16_t * addr16 () const
 
const uint32_t * addr32 () const
 
const uint64_t * addr64 () const
 
const uint16_t * addrF16 () const
 
const uint8_t * addr8 (int x, int y) const
 
const uint16_t * addr16 (int x, int y) const
 
const uint32_t * addr32 (int x, int y) const
 
const uint64_t * addr64 (int x, int y) const
 
const uint16_t * addrF16 (int x, int y) const
 
void * writable_addr () const
 
void * writable_addr (int x, int y) const
 
uint8_t * writable_addr8 (int x, int y) const
 
uint16_t * writable_addr16 (int x, int y) const
 
uint32_t * writable_addr32 (int x, int y) const
 
uint64_t * writable_addr64 (int x, int y) const
 
uint16_t * writable_addrF16 (int x, int y) const
 
bool readPixels (const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes) const
 
bool readPixels (const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY) const
 
bool readPixels (const SkPixmap &dst, int srcX, int srcY) const
 
bool readPixels (const SkPixmap &dst) const
 
bool scalePixels (const SkPixmap &dst, const SkSamplingOptions &) const
 
bool erase (SkColor color, const SkIRect &subset) const
 
bool erase (SkColor color) const
 
bool erase (const SkColor4f &color, const SkIRect *subset=nullptr) const
 

Detailed Description

SkPixmap provides a utility to pair SkImageInfo with pixels and row bytes. SkPixmap is a low level class which provides convenience functions to access raster destinations. SkCanvas can not draw SkPixmap, nor does SkPixmap provide a direct drawing destination.

Use SkBitmap to draw pixels referenced by SkPixmap; use SkSurface to draw into pixels referenced by SkPixmap.

SkPixmap does not try to manage the lifetime of the pixel memory. Use SkPixelRef to manage pixel memory; SkPixelRef is safe across threads.

Definition at line 40 of file SkPixmap.h.

Constructor & Destructor Documentation

◆ SkPixmap() [1/2]

SkPixmap::SkPixmap ( )
inline

Creates an empty SkPixmap without pixels, with kUnknown_SkColorType, with kUnknown_SkAlphaType, and with a width and height of zero. Use reset() to associate pixels, SkColorType, SkAlphaType, width, and height after SkPixmap has been created.

Returns
empty SkPixmap

Definition at line 50 of file SkPixmap.h.

51 : fPixels(nullptr), fRowBytes(0), fInfo(SkImageInfo::MakeUnknown(0, 0))
52 {}
static SkImageInfo MakeUnknown()

◆ SkPixmap() [2/2]

SkPixmap::SkPixmap ( const SkImageInfo info,
const void *  addr,
size_t  rowBytes 
)
inline

Creates SkPixmap from info width, height, SkAlphaType, and SkColorType. addr points to pixels, or nullptr. rowBytes should be info.width() times info.bytesPerPixel(), or larger.

No parameter checking is performed; it is up to the caller to ensure that addr and rowBytes agree with info.

The memory lifetime of pixels is managed by the caller. When SkPixmap goes out of scope, addr is unaffected.

SkPixmap may be later modified by reset() to change its size, pixel type, or storage.

Parameters
infowidth, height, SkAlphaType, SkColorType of SkImageInfo
addrpointer to pixels allocated by caller; may be nullptr
rowBytessize of one row of addr; width times pixel size, or larger
Returns
initialized SkPixmap

Definition at line 72 of file SkPixmap.h.

73 : fPixels(addr), fRowBytes(rowBytes), fInfo(info)
74 {}
size_t rowBytes() const
Definition SkPixmap.h:145
const SkImageInfo & info() const
Definition SkPixmap.h:135
const void * addr() const
Definition SkPixmap.h:153

Member Function Documentation

◆ addr() [1/2]

const void * SkPixmap::addr ( ) const
inline

Returns pixel address, the base address corresponding to the pixel origin.

It is up to the SkPixmap creator to ensure that pixel address is a useful value.

Returns
pixel address

Definition at line 153 of file SkPixmap.h.

153{ return fPixels; }

◆ addr() [2/2]

const void * SkPixmap::addr ( int  x,
int  y 
) const
inline

Returns readable pixel address at (x, y). Returns nullptr if SkPixelRef is nullptr.

Input is not validated: out of bounds values of x or y trigger an assert() if built with SK_DEBUG defined. Returns nullptr if SkColorType is kUnknown_SkColorType.

Performs a lookup of pixel size; for better performance, call one of: addr8, addr16, addr32, addr64, or addrF16().

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
readable generic pointer to pixel

Definition at line 314 of file SkPixmap.h.

314 {
315 return (const char*)fPixels + fInfo.computeOffset(x, y, fRowBytes);
316 }
double y
double x
size_t computeOffset(int x, int y, size_t rowBytes) const

◆ addr16() [1/2]

const uint16_t * SkPixmap::addr16 ( ) const
inline

Returns readable base pixel address. Result is addressable as unsigned 16-bit words. Will trigger an assert() if SkColorType is not kRGB_565_SkColorType or kARGB_4444_SkColorType, and is built with SK_DEBUG defined.

One word corresponds to one pixel.

Returns
readable unsigned 16-bit pointer to pixels

Definition at line 339 of file SkPixmap.h.

339 {
340 SkASSERT(2 == fInfo.bytesPerPixel());
341 return reinterpret_cast<const uint16_t*>(fPixels);
342 }
#define SkASSERT(cond)
Definition SkAssert.h:116
int bytesPerPixel() const

◆ addr16() [2/2]

const uint16_t * SkPixmap::addr16 ( int  x,
int  y 
) const
inline

Returns readable pixel address at (x, y).

Input is not validated: out of bounds values of x or y trigger an assert() if built with SK_DEBUG defined.

Will trigger an assert() if SkColorType is not kRGB_565_SkColorType or kARGB_4444_SkColorType, and is built with SK_DEBUG defined.

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
readable unsigned 16-bit pointer to pixel at (x, y)

Definition at line 416 of file SkPixmap.h.

416 {
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 }
const uint16_t * addr16() const
Definition SkPixmap.h:339
int width() const
int height() const

◆ addr32() [1/2]

const uint32_t * SkPixmap::addr32 ( ) const
inline

Returns readable base pixel address. Result is addressable as unsigned 32-bit words. Will trigger an assert() if SkColorType is not kRGBA_8888_SkColorType or kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.

One word corresponds to one pixel.

Returns
readable unsigned 32-bit pointer to pixels

Definition at line 352 of file SkPixmap.h.

352 {
353 SkASSERT(4 == fInfo.bytesPerPixel());
354 return reinterpret_cast<const uint32_t*>(fPixels);
355 }

◆ addr32() [2/2]

const uint32_t * SkPixmap::addr32 ( int  x,
int  y 
) const
inline

Returns readable pixel address at (x, y).

Input is not validated: out of bounds values of x or y trigger an assert() if built with SK_DEBUG defined.

Will trigger an assert() if SkColorType is not kRGBA_8888_SkColorType or kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
readable unsigned 32-bit pointer to pixel at (x, y)

Definition at line 434 of file SkPixmap.h.

434 {
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 }
const uint32_t * addr32() const
Definition SkPixmap.h:352

◆ addr64() [1/2]

const uint64_t * SkPixmap::addr64 ( ) const
inline

Returns readable base pixel address. Result is addressable as unsigned 64-bit words. Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built with SK_DEBUG defined.

One word corresponds to one pixel.

Returns
readable unsigned 64-bit pointer to pixels

Definition at line 365 of file SkPixmap.h.

365 {
366 SkASSERT(8 == fInfo.bytesPerPixel());
367 return reinterpret_cast<const uint64_t*>(fPixels);
368 }

◆ addr64() [2/2]

const uint64_t * SkPixmap::addr64 ( int  x,
int  y 
) const
inline

Returns readable pixel address at (x, y).

Input is not validated: out of bounds values of x or y trigger an assert() if built with SK_DEBUG defined.

Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built with SK_DEBUG defined.

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
readable unsigned 64-bit pointer to pixel at (x, y)

Definition at line 452 of file SkPixmap.h.

452 {
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 }
const uint64_t * addr64() const
Definition SkPixmap.h:365

◆ addr8() [1/2]

const uint8_t * SkPixmap::addr8 ( ) const
inline

Returns readable base pixel address. Result is addressable as unsigned 8-bit bytes. Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType or kGray_8_SkColorType, and is built with SK_DEBUG defined.

One byte corresponds to one pixel.

Returns
readable unsigned 8-bit pointer to pixels

Definition at line 326 of file SkPixmap.h.

326 {
327 SkASSERT(1 == fInfo.bytesPerPixel());
328 return reinterpret_cast<const uint8_t*>(fPixels);
329 }

◆ addr8() [2/2]

const uint8_t * SkPixmap::addr8 ( int  x,
int  y 
) const
inline

Returns readable pixel address at (x, y).

Input is not validated: out of bounds values of x or y trigger an assert() if built with SK_DEBUG defined.

Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType or kGray_8_SkColorType, and is built with SK_DEBUG defined.

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
readable unsigned 8-bit pointer to pixel at (x, y)

Definition at line 398 of file SkPixmap.h.

398 {
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 }
const uint8_t * addr8() const
Definition SkPixmap.h:326

◆ addrF16() [1/2]

const uint16_t * SkPixmap::addrF16 ( ) const
inline

Returns readable base pixel address. Result is addressable as unsigned 16-bit words. Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built with SK_DEBUG defined.

Each word represents one color component encoded as a half float. Four words correspond to one pixel.

Returns
readable unsigned 16-bit pointer to first component of pixels

Definition at line 379 of file SkPixmap.h.

379 {
380 SkASSERT(8 == fInfo.bytesPerPixel());
383 return reinterpret_cast<const uint16_t*>(fPixels);
384 }
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition SkColorType.h:38
@ kRGBA_F16Norm_SkColorType
pixel with half floats in [0,1] for red, green, blue, alpha;
Definition SkColorType.h:36
SkColorType colorType() const

◆ addrF16() [2/2]

const uint16_t * SkPixmap::addrF16 ( int  x,
int  y 
) const
inline

Returns readable pixel address at (x, y).

Input is not validated: out of bounds values of x or y trigger an assert() if built with SK_DEBUG defined.

Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built with SK_DEBUG defined.

Each unsigned 16-bit word represents one color component encoded as a half float. Four words correspond to one pixel.

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
readable unsigned 16-bit pointer to pixel component at (x, y)

Definition at line 473 of file SkPixmap.h.

473 {
476 return reinterpret_cast<const uint16_t*>(this->addr64(x, y));
477 }

◆ alphaType()

SkAlphaType SkPixmap::alphaType ( ) const
inline

Definition at line 175 of file SkPixmap.h.

175{ return fInfo.alphaType(); }
SkAlphaType alphaType() const

◆ bounds()

SkIRect SkPixmap::bounds ( ) const
inline

Returns SkIRect { 0, 0, width(), height() }.

Returns
integral rectangle from origin to width() and height()

Definition at line 207 of file SkPixmap.h.

207{ return SkIRect::MakeWH(this->width(), this->height()); }
int32_t height
int32_t width
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition SkRect.h:56

◆ colorSpace()

SkColorSpace * SkPixmap::colorSpace ( ) const

Returns SkColorSpace, the range of colors, associated with SkImageInfo. The reference count of SkColorSpace is unchanged. The returned SkColorSpace is immutable.

Returns
SkColorSpace in SkImageInfo, or nullptr

Definition at line 61 of file SkPixmap.cpp.

61{ return fInfo.colorSpace(); }
SkColorSpace * colorSpace() const

◆ colorType()

SkColorType SkPixmap::colorType ( ) const
inline

Definition at line 173 of file SkPixmap.h.

173{ return fInfo.colorType(); }

◆ computeByteSize()

size_t SkPixmap::computeByteSize ( ) const
inline

Returns minimum memory required for pixel storage. Does not include unused memory on last row when rowBytesAsPixels() exceeds width(). Returns SIZE_MAX if result does not fit in size_t. Returns zero if height() or width() is 0. Returns height() times rowBytes() if colorType() is kUnknown_SkColorType.

Returns
size in bytes of image buffer

Definition at line 231 of file SkPixmap.h.

231{ return fInfo.computeByteSize(fRowBytes); }
size_t computeByteSize(size_t rowBytes) const

◆ computeIsOpaque()

bool SkPixmap::computeIsOpaque ( ) const

Returns true if all pixels are opaque. SkColorType determines how pixels are encoded, and whether pixel describes alpha. Returns true for SkColorType without alpha in each pixel; for other SkColorType, returns true if all pixels have alpha values equivalent to 1.0 or greater.

For SkColorType kRGB_565_SkColorType or kGray_8_SkColorType: always returns true. For SkColorType kAlpha_8_SkColorType, kBGRA_8888_SkColorType, kRGBA_8888_SkColorType: returns true if all pixel alpha values are 255. For SkColorType kARGB_4444_SkColorType: returns true if all pixel alpha values are 15. For kRGBA_F16_SkColorType: returns true if all pixel alpha values are 1.0 or greater.

Returns false for kUnknown_SkColorType.

Returns
true if all pixels have opaque values or SkColorType is opaque

example: https://fiddle.skia.org/c/@Pixmap_computeIsOpaque

Definition at line 577 of file SkPixmap.cpp.

577 {
578 const int height = this->height();
579 const int width = this->width();
580
581 switch (this->colorType()) {
583 unsigned a = 0xFF;
584 for (int y = 0; y < height; ++y) {
585 const uint8_t* row = this->addr8(0, y);
586 for (int x = 0; x < width; ++x) {
587 a &= row[x];
588 }
589 if (0xFF != a) {
590 return false;
591 }
592 }
593 return true;
594 }
596 unsigned a = 0xFFFF;
597 for (int y = 0; y < height; ++y) {
598 const uint16_t* row = this->addr16(0, y);
599 for (int x = 0; x < width; ++x) {
600 a &= row[x];
601 }
602 if (0xFFFF != a) {
603 return false;
604 }
605 }
606 return true;
607 }
609 for (int y = 0; y < height; ++y) {
610 const SkHalf* row = this->addr16(0, y);
611 for (int x = 0; x < width; ++x) {
612 if (row[x] < SK_Half1) {
613 return false;
614 }
615 }
616 }
617 return true;
618 }
629 return true;
631 unsigned c = 0xFFFF;
632 for (int y = 0; y < height; ++y) {
633 const SkPMColor16* row = this->addr16(0, y);
634 for (int x = 0; x < width; ++x) {
635 c &= row[x];
636 }
637 if (0xF != SkGetPackedA4444(c)) {
638 return false;
639 }
640 }
641 return true;
642 }
646 SkPMColor c = (SkPMColor)~0;
647 for (int y = 0; y < height; ++y) {
648 const SkPMColor* row = this->addr32(0, y);
649 for (int x = 0; x < width; ++x) {
650 c &= row[x];
651 }
652 if (0xFF != SkGetPackedA32(c)) {
653 return false;
654 }
655 }
656 return true;
657 }
660 const SkHalf* row = (const SkHalf*)this->addr();
661 for (int y = 0; y < height; ++y) {
662 for (int x = 0; x < width; ++x) {
663 if (row[4 * x + 3] < SK_Half1) {
664 return false;
665 }
666 }
667 row += this->rowBytes() >> 1;
668 }
669 return true;
670 }
672 const float* row = (const float*)this->addr();
673 for (int y = 0; y < height; ++y) {
674 for (int x = 0; x < width; ++x) {
675 if (row[4 * x + 3] < 1.0f) {
676 return false;
677 }
678 }
679 row += this->rowBytes() >> 2;
680 }
681 return true;
682 }
685 uint32_t c = ~0;
686 for (int y = 0; y < height; ++y) {
687 const uint32_t* row = this->addr32(0, y);
688 for (int x = 0; x < width; ++x) {
689 c &= row[x];
690 }
691 if (0b11 != c >> 30) {
692 return false;
693 }
694 }
695 return true;
696 }
698 static constexpr uint64_t kOne = 510 + 384;
699 for (int y = 0; y < height; ++y) {
700 const uint64_t* row = this->addr64(0, y);
701 for (int x = 0; x < width; ++x) {
702 if ((row[x] >> 54) < kOne) {
703 return false;
704 }
705 }
706 }
707 return true;
708 }
710 uint16_t acc = 0xFFC0; // Ignore bottom six bits
711 for (int y = 0; y < height; ++y) {
712 const uint64_t* row = this->addr64(0, y);
713 for (int x = 0; x < width; ++x) {
714 acc &= (row[x] >> 48);
715 }
716 if (0xFFC0 != acc) {
717 return false;
718 }
719 }
720 return true;
721 }
723 uint16_t acc = 0xFFFF;
724 for (int y = 0; y < height; ++y) {
725 const uint64_t* row = this->addr64(0, y);
726 for (int x = 0; x < width; ++x) {
727 acc &= (row[x] >> 48);
728 }
729 if (0xFFFF != acc) {
730 return false;
731 }
732 }
733 return true;
734 }
736 SkDEBUGFAIL("");
737 break;
738 }
739 return false;
740}
#define SkDEBUGFAIL(message)
Definition SkAssert.h:118
uint16_t SkPMColor16
#define SkGetPackedA4444(c)
#define SkGetPackedA32(packed)
Definition SkColorPriv.h:92
@ kR16G16B16A16_unorm_SkColorType
pixel with a little endian uint16_t for red, green, blue
Definition SkColorType.h:50
@ kRGBA_10x6_SkColorType
pixel with 10 used bits (most significant) followed by 6 unused
Definition SkColorType.h:33
@ kR8_unorm_SkColorType
Definition SkColorType.h:54
@ kBGR_101010x_SkColorType
pixel with 10 bits each for blue, green, red; in 32-bit word
Definition SkColorType.h:30
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
Definition SkColorType.h:23
@ kR8G8_unorm_SkColorType
pixel with a uint8_t for red and green
Definition SkColorType.h:43
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition SkColorType.h:26
@ kA16_unorm_SkColorType
pixel with a little endian uint16_t for alpha
Definition SkColorType.h:48
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition SkColorType.h:21
@ kRGB_101010x_SkColorType
pixel with 10 bits each for red, green, blue; in 32-bit word
Definition SkColorType.h:29
@ kSRGBA_8888_SkColorType
Definition SkColorType.h:53
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition SkColorType.h:35
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition SkColorType.h:22
@ kBGRA_10101010_XR_SkColorType
pixel with 10 bits each for blue, green, red, alpha; in 64-bit word, extended range
Definition SkColorType.h:32
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
@ kRGB_888x_SkColorType
pixel with 8 bits each for red, green, blue; in 32-bit word
Definition SkColorType.h:25
@ kBGRA_1010102_SkColorType
10 bits for blue, green, red; 2 bits for alpha; in 32-bit word
Definition SkColorType.h:28
@ kA16_float_SkColorType
pixel with a half float for alpha
Definition SkColorType.h:45
@ kRGBA_F32_SkColorType
pixel using C float for red, green, blue, alpha; in 128-bit word
Definition SkColorType.h:40
@ kRGBA_1010102_SkColorType
10 bits for red, green, blue; 2 bits for alpha; in 32-bit word
Definition SkColorType.h:27
@ kBGR_101010x_XR_SkColorType
pixel with 10 bits each for blue, green, red; in 32-bit word, extended range
Definition SkColorType.h:31
@ kR16G16_unorm_SkColorType
pixel with a little endian uint16_t for red and green
Definition SkColorType.h:49
@ kUnknown_SkColorType
uninitialized
Definition SkColorType.h:20
@ kR16G16_float_SkColorType
pixel with a half float for red and green
Definition SkColorType.h:46
uint32_t SkPMColor
Definition SkColor.h:205
static constexpr uint16_t SK_Half1
Definition SkHalf.h:23
uint16_t SkHalf
Definition SkHalf.h:16
SkColorType colorType() const
Definition SkPixmap.h:173
struct MyStruct a[10]

◆ dimensions()

SkISize SkPixmap::dimensions ( ) const
inline

Return the dimensions of the pixmap (from its ImageInfo)

Definition at line 171 of file SkPixmap.h.

171{ return fInfo.dimensions(); }
SkISize dimensions() const

◆ erase() [1/3]

bool SkPixmap::erase ( const SkColor4f color,
const SkIRect subset = nullptr 
) const

Writes color to pixels bounded by subset; returns true on success. if subset is nullptr, writes colors pixels inside bounds(). Returns false if colorType() is kUnknown_SkColorType, if subset is not nullptr and does not intersect bounds(), or if subset is nullptr and bounds() is empty.

Parameters
colorunpremultiplied color to write
subsetbounding integer SkRect of pixels to write; may be nullptr
Returns
true if pixels are changed

Definition at line 746 of file SkPixmap.cpp.

746 {
747 if (this->colorType() == kUnknown_SkColorType) {
748 return false;
749 }
750
751 SkIRect clip = this->bounds();
752 if (subset && !clip.intersect(*subset)) {
753 return false; // is this check really needed (i.e. to return false in this case?)
754 }
755
756 // Erase is meant to simulate drawing in kSRC mode -- which means we have to convert out
757 // unpremul input into premul (which we always do when we draw).
758 const auto c = color.premul();
759
760 const auto dst = SkImageInfo::Make(1, 1, this->colorType(), this->alphaType(),
761 sk_ref_sp(this->colorSpace()));
763
764 uint64_t dstPixel[2] = {}; // be large enough for our widest config (F32 x 4)
765 SkASSERT((size_t)dst.bytesPerPixel() <= sizeof(dstPixel));
766
767 if (!SkConvertPixels(dst, dstPixel, sizeof(dstPixel), src, &c, sizeof(c))) {
768 return false;
769 }
770
771 if (this->colorType() == kRGBA_F32_SkColorType) {
772 SkColor4f dstColor;
773 memcpy(&dstColor, dstPixel, sizeof(dstColor));
774 for (int y = clip.fTop; y < clip.fBottom; ++y) {
775 SkColor4f* addr = (SkColor4f*)this->writable_addr(clip.fLeft, y);
776 SK_OPTS_NS::memsetT(addr, dstColor, clip.width());
777 }
778 } else {
779 using MemSet = void(*)(void*, uint64_t c, int count);
780 const MemSet procs[] = {
781 [](void* addr, uint64_t c, int count) {
782 SkASSERT(c == (uint8_t)c);
783 SK_OPTS_NS::memsetT((uint8_t*)addr, (uint8_t)c, count);
784 },
785 [](void* addr, uint64_t c, int count) {
786 SkASSERT(c == (uint16_t)c);
787 SK_OPTS_NS::memsetT((uint16_t*)addr, (uint16_t)c, count);
788 },
789 [](void* addr, uint64_t c, int count) {
790 SkASSERT(c == (uint32_t)c);
791 SK_OPTS_NS::memsetT((uint32_t*)addr, (uint32_t)c, count);
792 },
793 [](void* addr, uint64_t c, int count) {
794 SK_OPTS_NS::memsetT((uint64_t*)addr, c, count);
795 },
796 };
797
798 unsigned shift = SkColorTypeShiftPerPixel(this->colorType());
799 SkASSERT(shift < std::size(procs));
800 auto proc = procs[shift];
801
802 for (int y = clip.fTop; y < clip.fBottom; ++y) {
803 proc(this->writable_addr(clip.fLeft, y), dstPixel[0], clip.width());
804 }
805 }
806 return true;
807}
int count
SkColor4f color
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
bool SkConvertPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRB, const SkImageInfo &srcInfo, const void *srcPixels, size_t srcRB)
static int SkColorTypeShiftPerPixel(SkColorType ct)
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition SkPath.cpp:3824
sk_sp< T > sk_ref_sp(T *obj)
Definition SkRefCnt.h:381
SkIRect bounds() const
Definition SkPixmap.h:207
SkColorSpace * colorSpace() const
Definition SkPixmap.cpp:61
void * writable_addr() const
Definition SkPixmap.h:483
SkAlphaType alphaType() const
Definition SkPixmap.h:175
static void memsetT(T buffer[], T value, int count)
dst
Definition cp.py:12
int32_t fLeft
smaller x-axis bounds
Definition SkRect.h:33
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)

◆ erase() [2/3]

bool SkPixmap::erase ( SkColor  color) const
inline

Writes color to pixels inside bounds(); returns true on success. Returns false if colorType() is kUnknown_SkColorType, or if bounds() is empty.

Parameters
colorsRGB unpremultiplied color to write
Returns
true if pixels are changed

Definition at line 712 of file SkPixmap.h.

712{ return this->erase(color, this->bounds()); }
bool erase(SkColor color, const SkIRect &subset) const
Definition SkPixmap.cpp:742

◆ erase() [3/3]

bool SkPixmap::erase ( SkColor  color,
const SkIRect subset 
) const

Writes color to pixels bounded by subset; returns true on success. Returns false if colorType() is kUnknown_SkColorType, or if subset does not intersect bounds().

Parameters
colorsRGB unpremultiplied color to write
subsetbounding integer SkRect of written pixels
Returns
true if pixels are changed

example: https://fiddle.skia.org/c/@Pixmap_erase

Definition at line 742 of file SkPixmap.cpp.

742 {
743 return this->erase(SkColor4f::FromColor(color), &subset);
744}
static SkRGBA4f FromColor(SkColor color)

◆ extractSubset()

bool SkPixmap::extractSubset ( SkPixmap subset,
const SkIRect area 
) const

Sets subset width, height, pixel address to intersection of SkPixmap with area, if intersection is not empty; and return true. Otherwise, leave subset unchanged and return false.

Failing to read the return value generates a compile time warning.

Parameters
subsetstorage for width, height, pixel address of intersection
areabounds to intersect with SkPixmap
Returns
true if intersection of SkPixmap and area is not empty

Definition at line 65 of file SkPixmap.cpp.

65 {
66 SkIRect srcRect, r;
67 srcRect.setWH(this->width(), this->height());
68 if (!r.intersect(srcRect, subset)) {
69 return false; // r is empty (i.e. no intersection)
70 }
71
72 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
73 // exited above.
74 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
75 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
76
77 const void* pixels = nullptr;
78 if (fPixels) {
79 const size_t bpp = fInfo.bytesPerPixel();
80 pixels = (const uint8_t*)fPixels + r.fTop * fRowBytes + r.fLeft * bpp;
81 }
82 result->reset(fInfo.makeDimensions(r.size()), pixels, fRowBytes);
83 return true;
84}
GAsyncResult * result
bool intersect(const SkIRect &r)
Definition SkRect.h:513
constexpr SkISize size() const
Definition SkRect.h:172
int32_t fTop
smaller y-axis bounds
Definition SkRect.h:34
void setWH(int32_t width, int32_t height)
Definition SkRect.h:275
SkImageInfo makeDimensions(SkISize newSize) const

◆ getAlphaf()

float SkPixmap::getAlphaf ( int  x,
int  y 
) const

Look up the pixel at (x,y) and return its alpha component, normalized to [0..1]. This is roughly equivalent to SkGetColorA(getColor()), but can be more efficent (and more precise if the pixels store more than 8 bits per component).

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
alpha converted to normalized float

Definition at line 94 of file SkPixmap.cpp.

94 {
95 SkASSERT(this->addr());
96 SkASSERT((unsigned)x < (unsigned)this->width());
97 SkASSERT((unsigned)y < (unsigned)this->height());
98
99 float value = 0;
100 const void* srcPtr = fast_getaddr(*this, x, y);
101
102 switch (this->colorType()) {
104 return 0;
115 return 1;
117 value = static_cast<const uint8_t*>(srcPtr)[0] * (1.0f/255);
118 break;
120 value = static_cast<const uint16_t*>(srcPtr)[0] * (1.0f/65535);
121 break;
123 SkHalf half = static_cast<const SkHalf*>(srcPtr)[0];
124 value = SkHalfToFloat(half);
125 break;
126 }
128 uint16_t u16 = static_cast<const uint16_t*>(srcPtr)[0];
129 value = SkGetPackedA4444(u16) * (1.0f/15);
130 break;
131 }
135 value = static_cast<const uint8_t*>(srcPtr)[3] * (1.0f/255);
136 break;
139 uint32_t u32 = static_cast<const uint32_t*>(srcPtr)[0];
140 value = (u32 >> 30) * (1.0f/3);
141 break;
142 }
144 uint64_t u64 = static_cast<const uint64_t*>(srcPtr)[0];
145 value = ((u64 >> 54) - 384) / 510.f;
146 break;
147 }
149 uint64_t u64 = static_cast<const uint64_t*>(srcPtr)[0];
150 value = (u64 >> 54) * (1.0f/1023);
151 break;
152 }
154 uint64_t u64 = static_cast<const uint64_t*>(srcPtr)[0];
155 value = (u64 >> 48) * (1.0f/65535);
156 break;
157 }
160 value = from_half(skvx::half4::Load(srcPtr))[3];
161 break;
162 }
164 value = static_cast<const float*>(srcPtr)[3];
165 break;
166 }
167 return value;
168}
float SkHalfToFloat(SkHalf h)
Definition SkHalf.cpp:24
static const void * fast_getaddr(const SkPixmap &pm, int x, int y)
Definition SkPixmap.cpp:89
uint8_t value
SIN Vec< N, float > from_half(const Vec< N, uint16_t > &x)
Definition SkVx.h:790
static SKVX_ALWAYS_INLINE Vec Load(const void *ptr)
Definition SkVx.h:109

◆ getColor()

SkColor SkPixmap::getColor ( int  x,
int  y 
) const

Returns pixel at (x, y) as unpremultiplied color. Returns black with alpha if SkColorType is kAlpha_8_SkColorType.

Input is not validated: out of bounds values of x or y trigger an assert() if built with SK_DEBUG defined; and returns undefined values or may crash if SK_RELEASE is defined. Fails if SkColorType is kUnknown_SkColorType or pixel address is nullptr.

SkColorSpace in SkImageInfo is ignored. Some color precision may be lost in the conversion to unpremultiplied color; original pixel data may have additional precision.

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
pixel converted to unpremultiplied color

example: https://fiddle.skia.org/c/@Pixmap_getColor

Definition at line 187 of file SkPixmap.cpp.

187 {
188 SkASSERT(this->addr());
189 SkASSERT((unsigned)x < (unsigned)this->width());
190 SkASSERT((unsigned)y < (unsigned)this->height());
191
192 const bool needsUnpremul = (kPremul_SkAlphaType == fInfo.alphaType());
193 auto toColor = [needsUnpremul](uint32_t maybePremulColor) {
194 return needsUnpremul ? SkUnPreMultiply::PMColorToColor(maybePremulColor)
195 : SkSwizzle_BGRA_to_PMColor(maybePremulColor);
196 };
197
198 switch (this->colorType()) {
199 case kGray_8_SkColorType: {
200 uint8_t value = *this->addr8(x, y);
201 return SkColorSetRGB(value, value, value);
202 }
204 uint8_t value = *this->addr8(x, y);
205 return SkColorSetRGB(value, 0, 0);
206 }
208 return SkColorSetA(0, *this->addr8(x, y));
209 }
211 uint16_t value = *this->addr16(x, y);
212 return SkColorSetA(0, value * (255 / 65535.0f));
213 }
215 SkHalf value = *this->addr16(x, y);
216 return SkColorSetA(0, 255 * SkHalfToFloat(value));
217 }
219 return SkPixel16ToColor(*this->addr16(x, y));
220 }
222 uint16_t value = *this->addr16(x, y);
224 return toColor(c);
225 }
227 uint16_t value = *this->addr16(x, y);
228 return (uint32_t)( ((value >> 0) & 0xff) ) << 16
229 | (uint32_t)( ((value >> 8) & 0xff) ) << 8
230 | 0xff000000;
231 }
233 uint32_t value = *this->addr32(x, y);
234 return (uint32_t)( ((value >> 0) & 0xffff) * (255/65535.0f) ) << 16
235 | (uint32_t)( ((value >> 16) & 0xffff) * (255/65535.0f) ) << 8
236 | 0xff000000;
237 }
239 uint32_t value = *this->addr32(x, y);
240 uint32_t r = 255 * SkHalfToFloat((value >> 0) & 0xffff);
241 uint32_t g = 255 * SkHalfToFloat((value >> 16) & 0xffff);
242 return (r << 16) | (g << 8) | 0xff000000;
243 }
245 uint32_t value = *this->addr32(x, y);
246 return SkSwizzle_RB(value | 0xff000000);
247 }
249 uint32_t value = *this->addr32(x, y);
251 return toColor(c);
252 }
254 uint32_t value = *this->addr32(x, y);
256 return toColor(c);
257 }
259 auto srgb_to_linear = [](float x) {
260 return (x <= 0.04045f) ? x * (1 / 12.92f)
261 : std::pow(x * (1 / 1.055f) + (0.055f / 1.055f), 2.4f);
262 };
263
264 uint32_t value = *this->addr32(x, y);
265 float r = ((value >> 0) & 0xff) * (1/255.0f),
266 g = ((value >> 8) & 0xff) * (1/255.0f),
267 b = ((value >> 16) & 0xff) * (1/255.0f),
268 a = ((value >> 24) & 0xff) * (1/255.0f);
269 r = srgb_to_linear(r);
270 g = srgb_to_linear(g);
271 b = srgb_to_linear(b);
272 if (a != 0 && needsUnpremul) {
273 r = SkTPin(r/a, 0.0f, 1.0f);
274 g = SkTPin(g/a, 0.0f, 1.0f);
275 b = SkTPin(b/a, 0.0f, 1.0f);
276 }
277 return (uint32_t)( r * 255.0f ) << 16
278 | (uint32_t)( g * 255.0f ) << 8
279 | (uint32_t)( b * 255.0f ) << 0
280 | (uint32_t)( a * 255.0f ) << 24;
281 }
283 uint32_t value = *this->addr32(x, y);
284 // Convert 10-bit rgb to 8-bit bgr, and mask in 0xff alpha at the top.
285 return (uint32_t)( ((value >> 0) & 0x3ff) * (255/1023.0f) ) << 16
286 | (uint32_t)( ((value >> 10) & 0x3ff) * (255/1023.0f) ) << 8
287 | (uint32_t)( ((value >> 20) & 0x3ff) * (255/1023.0f) ) << 0
288 | 0xff000000;
289 }
291 SkASSERT(false);
292 return 0;
293 }
295 uint32_t value = *this->addr32(x, y);
296 // Convert 10-bit bgr to 8-bit bgr, and mask in 0xff alpha at the top.
297 return (uint32_t)( ((value >> 0) & 0x3ff) * (255/1023.0f) ) << 0
298 | (uint32_t)( ((value >> 10) & 0x3ff) * (255/1023.0f) ) << 8
299 | (uint32_t)( ((value >> 20) & 0x3ff) * (255/1023.0f) ) << 16
300 | 0xff000000;
301 }
304 uint32_t value = *this->addr32(x, y);
305
306 float r = ((value >> 0) & 0x3ff) * (1/1023.0f),
307 g = ((value >> 10) & 0x3ff) * (1/1023.0f),
308 b = ((value >> 20) & 0x3ff) * (1/1023.0f),
309 a = ((value >> 30) & 0x3 ) * (1/ 3.0f);
310 if (this->colorType() == kBGRA_1010102_SkColorType) {
311 std::swap(r,b);
312 }
313 if (a != 0 && needsUnpremul) {
314 r = SkTPin(r/a, 0.0f, 1.0f);
315 g = SkTPin(g/a, 0.0f, 1.0f);
316 b = SkTPin(b/a, 0.0f, 1.0f);
317 }
318 return (uint32_t)( r * 255.0f ) << 16
319 | (uint32_t)( g * 255.0f ) << 8
320 | (uint32_t)( b * 255.0f ) << 0
321 | (uint32_t)( a * 255.0f ) << 24;
322 }
324 SkASSERT(false);
325 return 0;
326 }
328 uint64_t value = *this->addr64(x, y);
329 float r = ((value >> 6) & 0x3ff) * (1/1023.0f),
330 g = ((value >> 22) & 0x3ff) * (1/1023.0f),
331 b = ((value >> 38) & 0x3ff) * (1/1023.0f),
332 a = ((value >> 54) & 0x3ff) * (1/1023.0f);
333 return (uint32_t)( r * 255.0f ) << 16
334 | (uint32_t)( g * 255.0f ) << 8
335 | (uint32_t)( b * 255.0f ) << 0
336 | (uint32_t)( a * 255.0f ) << 24;
337 }
339 uint64_t value = *this->addr64(x, y);
340
341 float r = ((value ) & 0xffff) * (1/65535.0f),
342 g = ((value >> 16) & 0xffff) * (1/65535.0f),
343 b = ((value >> 32) & 0xffff) * (1/65535.0f),
344 a = ((value >> 48) & 0xffff) * (1/65535.0f);
345 if (a != 0 && needsUnpremul) {
346 r *= (1.0f/a);
347 g *= (1.0f/a);
348 b *= (1.0f/a);
349 }
350 return (uint32_t)( r * 255.0f ) << 16
351 | (uint32_t)( g * 255.0f ) << 8
352 | (uint32_t)( b * 255.0f ) << 0
353 | (uint32_t)( a * 255.0f ) << 24;
354 }
357 const uint64_t* addr =
358 (const uint64_t*)fPixels + y * (fRowBytes >> 3) + x;
360 if (p4[3] && needsUnpremul) {
361 float inva = 1 / p4[3];
362 p4 = p4 * skvx::float4(inva, inva, inva, 1);
363 }
364 // p4 is RGBA, but we want BGRA, so we need to swap next
365 return Sk4f_toL32(swizzle_rb(p4));
366 }
368 const float* rgba =
369 (const float*)fPixels + 4*y*(fRowBytes >> 4) + 4*x;
371 // From here on, just like F16:
372 if (p4[3] && needsUnpremul) {
373 float inva = 1 / p4[3];
374 p4 = p4 * skvx::float4(inva, inva, inva, 1);
375 }
376 // p4 is RGBA, but we want BGRA, so we need to swap next
377 return Sk4f_toL32(swizzle_rb(p4));
378 }
380 break;
381 }
382 SkDEBUGFAIL("");
383 return SkColorSetARGB(0, 0, 0, 0);
384}
static float srgb_to_linear(float srgb)
static const uint32_t rgba[kNumPixels]
static SkPMColor SkPixel4444ToPixel32(U16CPU c)
static uint32_t SkSwizzle_RB(uint32_t c)
Definition SkColorData.h:57
static SkColor SkPixel16ToColor(U16CPU src)
static SkPMColor SkSwizzle_RGBA_to_PMColor(uint32_t c)
Definition SkColorData.h:83
static SkPMColor SkSwizzle_BGRA_to_PMColor(uint32_t c)
Definition SkColorData.h:91
#define SkColorSetRGB(r, g, b)
Definition SkColor.h:57
static constexpr SkColor SkColorSetA(SkColor c, U8CPU a)
Definition SkColor.h:82
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition SkColor.h:49
static uint32_t Sk4f_toL32(const skvx::float4 &px)
static skvx::float4 swizzle_rb(const skvx::float4 &x)
static constexpr const T & SkTPin(const T &x, const T &lo, const T &hi)
Definition SkTPin.h:19
static SkColor PMColorToColor(SkPMColor c)
static bool b
flutter::DlColor toColor(const float *components)
Vec< 4, float > float4
Definition SkVx.h:1146
Definition ref_ptr.h:256

◆ getColor4f()

SkColor4f SkPixmap::getColor4f ( int  x,
int  y 
) const

Returns pixel at (x, y) as unpremultiplied color as an SkColor4f. Returns black with alpha if SkColorType is kAlpha_8_SkColorType.

Input is not validated: out of bounds values of x or y trigger an assert() if built with SK_DEBUG defined; and returns undefined values or may crash if SK_RELEASE is defined. Fails if SkColorType is kUnknown_SkColorType or pixel address is nullptr.

SkColorSpace in SkImageInfo is ignored. Some color precision may be lost in the conversion to unpremultiplied color; original pixel data may have additional precision, though this is less likely than for getColor(). Rounding errors may occur if the underlying type has lower precision.

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
pixel converted to unpremultiplied float color

Definition at line 388 of file SkPixmap.cpp.

388 {
389 SkASSERT(this->addr());
390 SkASSERT((unsigned)x < (unsigned)this->width());
391 SkASSERT((unsigned)y < (unsigned)this->height());
392
393 const bool needsUnpremul = (kPremul_SkAlphaType == fInfo.alphaType());
394 auto toColor = [needsUnpremul](uint32_t maybePremulColor) {
395 return needsUnpremul ? SkUnPreMultiply::PMColorToColor(maybePremulColor)
396 : SkSwizzle_BGRA_to_PMColor(maybePremulColor);
397 };
398
399 switch (this->colorType()) {
400 case kGray_8_SkColorType: {
401 float value = *this->addr8(x, y) / 255.0f;
402 return SkColor4f{value, value, value, 1.0};
403 }
405 float value = *this->addr8(x, y) / 255.0f;
406 return SkColor4f{value, 0.0f, 0.0f, 1.0f};
407 }
409 float value = *this->addr8(x, y) / 255.0f;
410 return SkColor4f{0.0f, 0.0f, 0.0f, value};
411 }
413 float value = *this->addr16(x, y) / 65535.0f;
414 return SkColor4f{0.0f, 0.0f, 0.0f, value};
415 }
417 SkHalf value = *this->addr16(x, y);
418 return SkColor4f{0.0f, 0.0f, 0.0f, SkHalfToFloat(value)};
419 }
421 SkColor c = SkPixel16ToColor(*this->addr16(x, y));
422 return SkColor4f::FromColor(c);
423 }
425 uint16_t value = *this->addr16(x, y);
427 return SkColor4f::FromColor(toColor(c));
428 }
430 uint16_t value = *this->addr16(x, y);
431 SkColor c = (uint32_t)(((value >> 0) & 0xff)) << 16 |
432 (uint32_t)(((value >> 8) & 0xff)) << 8 | 0xff000000;
433 return SkColor4f::FromColor(c);
434 }
436 uint32_t value = *this->addr32(x, y);
437 SkColor c = (uint32_t)(((value >> 0) & 0xffff) * (255 / 65535.0f)) << 16 |
438 (uint32_t)(((value >> 16) & 0xffff) * (255 / 65535.0f)) << 8 | 0xff000000;
439 return SkColor4f::FromColor(c);
440 }
442 uint32_t value = *this->addr32(x, y);
443 float r = SkHalfToFloat((value >> 0) & 0xffff);
444 float g = SkHalfToFloat((value >> 16) & 0xffff);
445 return SkColor4f{r, g, 0.0, 1.0};
446 }
448 uint32_t value = *this->addr32(x, y);
449 SkColor c = SkSwizzle_RB(value | 0xff000000);
450 return SkColor4f::FromColor(c);
451 }
453 uint32_t value = *this->addr32(x, y);
455 return SkColor4f::FromColor(toColor(c));
456 }
458 uint32_t value = *this->addr32(x, y);
460 return SkColor4f::FromColor(toColor(c));
461 }
463 auto srgb_to_linear = [](float x) {
464 return (x <= 0.04045f) ? x * (1 / 12.92f)
465 : std::pow(x * (1 / 1.055f) + (0.055f / 1.055f), 2.4f);
466 };
467
468 uint32_t value = *this->addr32(x, y);
469 float r = ((value >> 0) & 0xff) * (1 / 255.0f),
470 g = ((value >> 8) & 0xff) * (1 / 255.0f),
471 b = ((value >> 16) & 0xff) * (1 / 255.0f),
472 a = ((value >> 24) & 0xff) * (1 / 255.0f);
473 r = srgb_to_linear(r);
474 g = srgb_to_linear(g);
475 b = srgb_to_linear(b);
476 if (a != 0 && needsUnpremul) {
477 r = SkTPin(r / a, 0.0f, 1.0f);
478 g = SkTPin(g / a, 0.0f, 1.0f);
479 b = SkTPin(b / a, 0.0f, 1.0f);
480 }
481 return SkColor4f{r, g, b, a};
482 }
484 SkASSERT(false);
485 return {};
486 }
488 uint32_t value = *this->addr32(x, y);
489 // Convert 10-bit rgb to float rgb, and mask in 0xff alpha at the top.
490 float r = (uint32_t)((value >> 0) & 0x3ff) / (1023.0f);
491 float g = (uint32_t)((value >> 10) & 0x3ff) / (1023.0f);
492 float b = (uint32_t)((value >> 20) & 0x3ff) / (1023.0f);
493 float a = 1.0f;
494 return SkColor4f{r, g, b, a};
495 }
497 uint32_t value = *this->addr32(x, y);
498 // Convert 10-bit bgr to float rgb, and mask in 0xff alpha at the top.
499 float r = (uint32_t)((value >> 20) & 0x3ff) / (1023.0f);
500 float g = (uint32_t)((value >> 10) & 0x3ff) / (1023.0f);
501 float b = (uint32_t)((value >> 0) & 0x3ff) / (1023.0f);
502 float a = 1.0f;
503 return SkColor4f{r, g, b, a};
504 }
507 uint32_t value = *this->addr32(x, y);
508
509 float r = ((value >> 0) & 0x3ff) * (1 / 1023.0f),
510 g = ((value >> 10) & 0x3ff) * (1 / 1023.0f),
511 b = ((value >> 20) & 0x3ff) * (1 / 1023.0f),
512 a = ((value >> 30) & 0x3) * (1 / 3.0f);
513 if (this->colorType() == kBGRA_1010102_SkColorType) {
514 std::swap(r, b);
515 }
516 if (a != 0 && needsUnpremul) {
517 r = SkTPin(r / a, 0.0f, 1.0f);
518 g = SkTPin(g / a, 0.0f, 1.0f);
519 b = SkTPin(b / a, 0.0f, 1.0f);
520 }
521 return SkColor4f{r, g, b, a};
522 }
524 SkASSERT(false);
525 return {};
526 }
528 uint64_t value = *this->addr64(x, y);
529
530 float r = ((value >> 6) & 0x3ff) * (1/1023.0f),
531 g = ((value >> 22) & 0x3ff) * (1/1023.0f),
532 b = ((value >> 38) & 0x3ff) * (1/1023.0f),
533 a = ((value >> 54) & 0x3ff) * (1/1023.0f);
534 return SkColor4f{r, g, b, a};
535 }
537 uint64_t value = *this->addr64(x, y);
538
539 float r = ((value)&0xffff) * (1 / 65535.0f),
540 g = ((value >> 16) & 0xffff) * (1 / 65535.0f),
541 b = ((value >> 32) & 0xffff) * (1 / 65535.0f),
542 a = ((value >> 48) & 0xffff) * (1 / 65535.0f);
543 if (a != 0 && needsUnpremul) {
544 r *= (1.0f / a);
545 g *= (1.0f / a);
546 b *= (1.0f / a);
547 }
548 return SkColor4f{r, g, b, a};
549 }
552 const uint64_t* addr = (const uint64_t*)fPixels + y * (fRowBytes >> 3) + x;
554 if (p4[3] && needsUnpremul) {
555 float inva = 1 / p4[3];
556 p4 = p4 * skvx::float4(inva, inva, inva, 1);
557 }
558 return SkColor4f{p4[0], p4[1], p4[2], p4[3]};
559 }
561 const float* rgba = (const float*)fPixels + 4 * y * (fRowBytes >> 4) + 4 * x;
563 // From here on, just like F16:
564 if (p4[3] && needsUnpremul) {
565 float inva = 1 / p4[3];
566 p4 = p4 * skvx::float4(inva, inva, inva, 1);
567 }
568 return SkColor4f{p4[0], p4[1], p4[2], p4[3]};
569 }
571 break;
572 }
573 SkDEBUGFAIL("");
575}
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor4f kTransparent
Definition SkColor.h:434

◆ height()

int SkPixmap::height ( ) const
inline

Returns pixel row count.

Returns
pixel height in SkImageInfo

Definition at line 166 of file SkPixmap.h.

166{ return fInfo.height(); }

◆ info()

const SkImageInfo & SkPixmap::info ( ) const
inline

Returns width, height, SkAlphaType, SkColorType, and SkColorSpace.

Returns
reference to SkImageInfo

Definition at line 135 of file SkPixmap.h.

135{ return fInfo; }

◆ isOpaque()

bool SkPixmap::isOpaque ( ) const
inline

Returns true if SkAlphaType is kOpaque_SkAlphaType. Does not check if SkColorType allows alpha, or if any pixel value has transparency.

Returns
true if SkImageInfo has opaque SkAlphaType

Definition at line 201 of file SkPixmap.h.

201{ return fInfo.isOpaque(); }
bool isOpaque() const

◆ readPixels() [1/4]

bool SkPixmap::readPixels ( const SkImageInfo dstInfo,
void *  dstPixels,
size_t  dstRowBytes 
) const
inline

Copies a SkRect of pixels to dstPixels. Copy starts at (0, 0), and does not exceed SkPixmap (width(), height()).

dstInfo specifies width, height, SkColorType, SkAlphaType, and SkColorSpace of destination. dstRowBytes specifics the gap from one destination row to the next. Returns true if pixels are copied. Returns false if dstInfo address equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes().

Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match. If SkPixmap colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match. If SkPixmap alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must match. If SkPixmap colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns false if pixel conversion is not possible.

Returns false if SkPixmap width() or height() is zero or negative.

Parameters
dstInfodestination width, height, SkColorType, SkAlphaType, SkColorSpace
dstPixelsdestination pixel storage
dstRowBytesdestination row length
Returns
true if pixels are copied to dstPixels

Definition at line 592 of file SkPixmap.h.

592 {
593 return this->readPixels(dstInfo, dstPixels, dstRowBytes, 0, 0);
594 }
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes) const
Definition SkPixmap.h:592

◆ readPixels() [2/4]

bool SkPixmap::readPixels ( const SkImageInfo dstInfo,
void *  dstPixels,
size_t  dstRowBytes,
int  srcX,
int  srcY 
) const

Copies a SkRect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not exceed SkPixmap (width(), height()).

dstInfo specifies width, height, SkColorType, SkAlphaType, and SkColorSpace of destination. dstRowBytes specifics the gap from one destination row to the next. Returns true if pixels are copied. Returns false if dstInfo address equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes().

Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match. If SkPixmap colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match. If SkPixmap alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must match. If SkPixmap colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns false if pixel conversion is not possible.

srcX and srcY may be negative to copy only top or left of source. Returns false if SkPixmap width() or height() is zero or negative. Returns false if: abs(srcX) >= Pixmap width(), or if abs(srcY) >= Pixmap height().

Parameters
dstInfodestination width, height, SkColorType, SkAlphaType, SkColorSpace
dstPixelsdestination pixel storage
dstRowBytesdestination row length
srcXcolumn index whose absolute value is less than width()
srcYrow index whose absolute value is less than height()
Returns
true if pixels are copied to dstPixels

Definition at line 170 of file SkPixmap.cpp.

171 {
172 if (!SkImageInfoValidConversion(dstInfo, fInfo)) {
173 return false;
174 }
175
176 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, x, y);
177 if (!rec.trim(fInfo.width(), fInfo.height())) {
178 return false;
179 }
180
181 const void* srcPixels = this->addr(rec.fX, rec.fY);
182 const SkImageInfo srcInfo = fInfo.makeDimensions(rec.fInfo.dimensions());
183 return SkConvertPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, srcInfo, srcPixels,
184 this->rowBytes());
185}
static bool SkImageInfoValidConversion(const SkImageInfo &dst, const SkImageInfo &src)

◆ readPixels() [3/4]

bool SkPixmap::readPixels ( const SkPixmap dst) const
inline

Copies pixels inside bounds() to dst. dst specifies width, height, SkColorType, SkAlphaType, and SkColorSpace of destination. Returns true if pixels are copied. Returns false if dst address equals nullptr, or dst.rowBytes() is less than dst SkImageInfo::minRowBytes.

Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match. If SkPixmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match. If SkPixmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must match. If SkPixmap colorSpace() is nullptr, dst SkColorSpace must match. Returns false if pixel conversion is not possible.

Returns false if SkPixmap width() or height() is zero or negative.

Parameters
dstSkImageInfo and pixel address to write to
Returns
true if pixels are copied to dst

Definition at line 668 of file SkPixmap.h.

668 {
669 return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), 0, 0);
670 }

◆ readPixels() [4/4]

bool SkPixmap::readPixels ( const SkPixmap dst,
int  srcX,
int  srcY 
) const
inline

Copies a SkRect of pixels to dst. Copy starts at (srcX, srcY), and does not exceed SkPixmap (width(), height()). dst specifies width, height, SkColorType, SkAlphaType, and SkColorSpace of destination. Returns true if pixels are copied. Returns false if dst address equals nullptr, or dst.rowBytes() is less than dst SkImageInfo::minRowBytes.

Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.info().colorType must match. If SkPixmap colorType() is kGray_8_SkColorType, dst.info().colorSpace must match. If SkPixmap alphaType() is kOpaque_SkAlphaType, dst.info().alphaType must match. If SkPixmap colorSpace() is nullptr, dst.info().colorSpace must match. Returns false if pixel conversion is not possible.

srcX and srcY may be negative to copy only top or left of source. Returns false SkPixmap width() or height() is zero or negative. Returns false if: abs(srcX) >= Pixmap width(), or if abs(srcY) >= Pixmap height().

Parameters
dstSkImageInfo and pixel address to write to
srcXcolumn index whose absolute value is less than width()
srcYrow index whose absolute value is less than height()
Returns
true if pixels are copied to dst

Definition at line 647 of file SkPixmap.h.

647 {
648 return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), srcX, srcY);
649 }

◆ refColorSpace()

sk_sp< SkColorSpace > SkPixmap::refColorSpace ( ) const

Returns smart pointer to SkColorSpace, the range of colors, associated with SkImageInfo. The smart pointer tracks the number of objects sharing this SkColorSpace reference so the memory is released when the owners destruct.

The returned SkColorSpace is immutable.

Returns
SkColorSpace in SkImageInfo wrapped in a smart pointer

Definition at line 63 of file SkPixmap.cpp.

63{ return fInfo.refColorSpace(); }
sk_sp< SkColorSpace > refColorSpace() const

◆ reset() [1/3]

void SkPixmap::reset ( )

Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType.

The prior pixels are unaffected; it is up to the caller to release pixels memory if desired.

example: https://fiddle.skia.org/c/@Pixmap_reset

Definition at line 32 of file SkPixmap.cpp.

32 {
33 fPixels = nullptr;
34 fRowBytes = 0;
36}

◆ reset() [2/3]

void SkPixmap::reset ( const SkImageInfo info,
const void *  addr,
size_t  rowBytes 
)

Sets width, height, SkAlphaType, and SkColorType from info. Sets pixel address from addr, which may be nullptr. Sets row bytes from rowBytes, which should be info.width() times info.bytesPerPixel(), or larger.

Does not check addr. Asserts if built with SK_DEBUG defined and if rowBytes is too small to hold one row of pixels.

The memory lifetime pixels are managed by the caller. When SkPixmap goes out of scope, addr is unaffected.

Parameters
infowidth, height, SkAlphaType, SkColorType of SkImageInfo
addrpointer to pixels allocated by caller; may be nullptr
rowBytessize of one row of addr; width times pixel size, or larger

example: https://fiddle.skia.org/c/@Pixmap_reset_2

Definition at line 38 of file SkPixmap.cpp.

38 {
39 if (addr) {
41 }
42 fPixels = addr;
43 fRowBytes = rowBytes;
44 fInfo = info;
45}
bool validRowBytes(size_t rowBytes) const

◆ reset() [3/3]

bool SkPixmap::reset ( const SkMask mask)

Deprecated.

Definition at line 47 of file SkPixmap.cpp.

47 {
48 if (SkMask::kA8_Format == src.fFormat) {
49 this->reset(SkImageInfo::MakeA8(src.fBounds.width(), src.fBounds.height()),
50 src.fImage, src.fRowBytes);
51 return true;
52 }
53 this->reset();
54 return false;
55}
void reset()
Definition SkPixmap.cpp:32
static SkImageInfo MakeA8(int width, int height)
@ kA8_Format
8bits per pixel mask (e.g. antialiasing)
Definition SkMask.h:28

◆ rowBytes()

size_t SkPixmap::rowBytes ( ) const
inline

Returns row bytes, the interval from one pixel row to the next. Row bytes is at least as large as: width() * info().bytesPerPixel().

Returns zero if colorType() is kUnknown_SkColorType. It is up to the SkBitmap creator to ensure that row bytes is a useful value.

Returns
byte length of pixel row

Definition at line 145 of file SkPixmap.h.

145{ return fRowBytes; }

◆ rowBytesAsPixels()

int SkPixmap::rowBytesAsPixels ( ) const
inline

Returns number of pixels that fit on row. Should be greater than or equal to width().

Returns
maximum pixels per row

Definition at line 214 of file SkPixmap.h.

214{ return int(fRowBytes >> this->shiftPerPixel()); }
Type::kYUV Type::kRGBA() int(0.7 *637)
int shiftPerPixel() const
Definition SkPixmap.h:221

◆ scalePixels()

bool SkPixmap::scalePixels ( const SkPixmap dst,
const SkSamplingOptions sampling 
) const

Copies SkBitmap to dst, scaling pixels to fit dst.width() and dst.height(), and converting pixels to match dst.colorType() and dst.alphaType(). Returns true if pixels are copied. Returns false if dst address is nullptr, or dst.rowBytes() is less than dst SkImageInfo::minRowBytes.

Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match. If SkPixmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match. If SkPixmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must match. If SkPixmap colorSpace() is nullptr, dst SkColorSpace must match. Returns false if pixel conversion is not possible.

Returns false if SkBitmap width() or height() is zero or negative.

Parameters
dstSkImageInfo and pixel address to write to
Returns
true if pixels are scaled to fit dst

example: https://fiddle.skia.org/c/@Pixmap_scalePixels

Definition at line 29 of file SkPixmapDraw.cpp.

29 {
30 // We may need to tweak how we interpret these just a little below, so we make copies.
31 SkPixmap src = *this,
32 dst = actualDst;
33
34 // Can't do anthing with empty src or dst
35 if (src.width() <= 0 || src.height() <= 0 ||
36 dst.width() <= 0 || dst.height() <= 0) {
37 return false;
38 }
39
40 // no scaling involved?
41 if (src.width() == dst.width() && src.height() == dst.height()) {
42 return src.readPixels(dst);
43 }
44
45 // If src and dst are both unpremul, we'll fake the source out to appear as if premul,
46 // and mark the destination as opaque. This odd combination allows us to scale unpremul
47 // pixels without ever premultiplying them (perhaps losing information in the color channels).
48 // This is an idiosyncratic feature of scalePixels(), and is tested by scalepixels_unpremul GM.
49 bool clampAsIfUnpremul = false;
50 if (src.alphaType() == kUnpremul_SkAlphaType &&
51 dst.alphaType() == kUnpremul_SkAlphaType) {
52 src.reset(src.info().makeAlphaType(kPremul_SkAlphaType), src.addr(), src.rowBytes());
53 dst.reset(dst.info().makeAlphaType(kOpaque_SkAlphaType), dst.addr(), dst.rowBytes());
54
55 // We'll need to tell the image shader to clamp to [0,1] instead of the
56 // usual [0,a] when using a bicubic scaling (kHigh_SkFilterQuality).
57 clampAsIfUnpremul = true;
58 }
59
61 if (!bitmap.installPixels(src)) {
62 return false;
63 }
64 bitmap.setImmutable(); // Don't copy when we create an image.
65
67
71 sampling,
72 &scale,
73 clampAsIfUnpremul);
74
76 SkSurfaces::WrapPixels(dst.info(), dst.writable_addr(), dst.rowBytes());
77 if (!shader || !surface) {
78 return false;
79 }
80
82 paint.setBlendMode(SkBlendMode::kSrc);
83 paint.setShader(std::move(shader));
84 surface->getCanvas()->drawPaint(paint);
85 return true;
86}
kUnpremul_SkAlphaType
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
static sk_sp< SkShader > Make(sk_sp< SkImage >, SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix, bool clampAsIfUnpremul=false)
static SkMatrix RectToRect(const SkRect &src, const SkRect &dst, ScaleToFit mode=kFill_ScaleToFit)
Definition SkMatrix.h:157
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
SK_API sk_sp< SkSurface > WrapPixels(const SkImageInfo &imageInfo, void *pixels, size_t rowBytes, const SkSurfaceProps *surfaceProps=nullptr)
const Scalar scale
static SkRect Make(const SkISize &size)
Definition SkRect.h:669

◆ setColorSpace()

void SkPixmap::setColorSpace ( sk_sp< SkColorSpace colorSpace)

Changes SkColorSpace in SkImageInfo; preserves width, height, SkAlphaType, and SkColorType in SkImage, and leaves pixel address and row bytes unchanged. SkColorSpace reference count is incremented.

Parameters
colorSpaceSkColorSpace moved to SkImageInfo

example: https://fiddle.skia.org/c/@Pixmap_setColorSpace

Definition at line 57 of file SkPixmap.cpp.

57 {
58 fInfo = fInfo.makeColorSpace(std::move(cs));
59}
SkImageInfo makeColorSpace(sk_sp< SkColorSpace > cs) const

◆ shiftPerPixel()

int SkPixmap::shiftPerPixel ( ) const
inline

Returns bit shift converting row bytes to row pixels. Returns zero for kUnknown_SkColorType.

Returns
one of: 0, 1, 2, 3; left shift to convert pixels to bytes

Definition at line 221 of file SkPixmap.h.

221{ return fInfo.shiftPerPixel(); }
int shiftPerPixel() const

◆ width()

int SkPixmap::width ( ) const
inline

Returns pixel count in each pixel row. Should be equal or less than: rowBytes() / info().bytesPerPixel().

Returns
pixel width in SkImageInfo

Definition at line 160 of file SkPixmap.h.

160{ return fInfo.width(); }

◆ writable_addr() [1/2]

void * SkPixmap::writable_addr ( ) const
inline

Returns writable base pixel address.

Returns
writable generic base pointer to pixels

Definition at line 483 of file SkPixmap.h.

483{ return const_cast<void*>(fPixels); }

◆ writable_addr() [2/2]

void * SkPixmap::writable_addr ( int  x,
int  y 
) const
inline

Returns writable pixel address at (x, y).

Input is not validated: out of bounds values of x or y trigger an assert() if built with SK_DEBUG defined. Returns zero if SkColorType is kUnknown_SkColorType.

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
writable generic pointer to pixel

Definition at line 494 of file SkPixmap.h.

494 {
495 return const_cast<void*>(this->addr(x, y));
496 }

◆ writable_addr16()

uint16_t * SkPixmap::writable_addr16 ( int  x,
int  y 
) const
inline

Returns writable_addr pixel address at (x, y). Result is addressable as unsigned 16-bit words. Will trigger an assert() if SkColorType is not kRGB_565_SkColorType or kARGB_4444_SkColorType, and is built with SK_DEBUG defined.

One word corresponds to one pixel.

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
writable unsigned 16-bit pointer to pixel

Definition at line 522 of file SkPixmap.h.

522 {
523 return const_cast<uint16_t*>(this->addr16(x, y));
524 }

◆ writable_addr32()

uint32_t * SkPixmap::writable_addr32 ( int  x,
int  y 
) const
inline

Returns writable pixel address at (x, y). Result is addressable as unsigned 32-bit words. Will trigger an assert() if SkColorType is not kRGBA_8888_SkColorType or kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.

One word corresponds to one pixel.

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
writable unsigned 32-bit pointer to pixel

Definition at line 537 of file SkPixmap.h.

537 {
538 return const_cast<uint32_t*>(this->addr32(x, y));
539 }

◆ writable_addr64()

uint64_t * SkPixmap::writable_addr64 ( int  x,
int  y 
) const
inline

Returns writable pixel address at (x, y). Result is addressable as unsigned 64-bit words. Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built with SK_DEBUG defined.

One word corresponds to one pixel.

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
writable unsigned 64-bit pointer to pixel

Definition at line 551 of file SkPixmap.h.

551 {
552 return const_cast<uint64_t*>(this->addr64(x, y));
553 }

◆ writable_addr8()

uint8_t * SkPixmap::writable_addr8 ( int  x,
int  y 
) const
inline

Returns writable pixel address at (x, y). Result is addressable as unsigned 8-bit bytes. Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType or kGray_8_SkColorType, and is built with SK_DEBUG defined.

One byte corresponds to one pixel.

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
writable unsigned 8-bit pointer to pixels

Definition at line 508 of file SkPixmap.h.

508 {
509 return const_cast<uint8_t*>(this->addr8(x, y));
510 }

◆ writable_addrF16()

uint16_t * SkPixmap::writable_addrF16 ( int  x,
int  y 
) const
inline

Returns writable pixel address at (x, y). Result is addressable as unsigned 16-bit words. Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built with SK_DEBUG defined.

Each word represents one color component encoded as a half float. Four words correspond to one pixel.

Parameters
xcolumn index, zero or greater, and less than width()
yrow index, zero or greater, and less than height()
Returns
writable unsigned 16-bit pointer to first component of pixel

Definition at line 566 of file SkPixmap.h.

566 {
567 return reinterpret_cast<uint16_t*>(writable_addr64(x, y));
568 }
uint64_t * writable_addr64(int x, int y) const
Definition SkPixmap.h:551

The documentation for this class was generated from the following files: