Skia
2D Graphics Library
SkPixmap.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkPixmap_DEFINED
9 #define SkPixmap_DEFINED
10 
11 #include "include/core/SkColor.h"
15 
16 class SkData;
17 struct SkMask;
18 
32 public:
33 
42  : fPixels(nullptr), fRowBytes(0), fInfo(SkImageInfo::MakeUnknown(0, 0))
43  {}
44 
63  SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes)
64  : fPixels(addr), fRowBytes(rowBytes), fInfo(info)
65  {}
66 
75  void reset();
76 
94  void reset(const SkImageInfo& info, const void* addr, size_t rowBytes);
95 
105 
108  bool SK_WARN_UNUSED_RESULT reset(const SkMask& mask);
109 
120  bool SK_WARN_UNUSED_RESULT extractSubset(SkPixmap* subset, const SkIRect& area) const;
121 
126  const SkImageInfo& info() const { return fInfo; }
127 
136  size_t rowBytes() const { return fRowBytes; }
137 
144  const void* addr() const { return fPixels; }
145 
151  int width() const { return fInfo.width(); }
152 
157  int height() const { return fInfo.height(); }
158 
162  SkISize dimensions() const { return fInfo.dimensions(); }
163 
164  SkColorType colorType() const { return fInfo.colorType(); }
165 
166  SkAlphaType alphaType() const { return fInfo.alphaType(); }
167 
174  SkColorSpace* colorSpace() const { return fInfo.colorSpace(); }
175 
184  sk_sp<SkColorSpace> refColorSpace() const { return fInfo.refColorSpace(); }
185 
192  bool isOpaque() const { return fInfo.isOpaque(); }
193 
198  SkIRect bounds() const { return SkIRect::MakeWH(this->width(), this->height()); }
199 
205  int rowBytesAsPixels() const { return int(fRowBytes >> this->shiftPerPixel()); }
206 
212  int shiftPerPixel() const { return fInfo.shiftPerPixel(); }
213 
222  size_t computeByteSize() const { return fInfo.computeByteSize(fRowBytes); }
223 
242  bool computeIsOpaque() const;
243 
262  SkColor getColor(int x, int y) const;
263 
272  float getAlphaf(int x, int y) const;
273 
286  const void* addr(int x, int y) const {
287  return (const char*)fPixels + fInfo.computeOffset(x, y, fRowBytes);
288  }
289 
298  const uint8_t* addr8() const {
299  SkASSERT(1 == fInfo.bytesPerPixel());
300  return reinterpret_cast<const uint8_t*>(fPixels);
301  }
302 
311  const uint16_t* addr16() const {
312  SkASSERT(2 == fInfo.bytesPerPixel());
313  return reinterpret_cast<const uint16_t*>(fPixels);
314  }
315 
324  const uint32_t* addr32() const {
325  SkASSERT(4 == fInfo.bytesPerPixel());
326  return reinterpret_cast<const uint32_t*>(fPixels);
327  }
328 
337  const uint64_t* addr64() const {
338  SkASSERT(8 == fInfo.bytesPerPixel());
339  return reinterpret_cast<const uint64_t*>(fPixels);
340  }
341 
351  const uint16_t* addrF16() const {
352  SkASSERT(8 == fInfo.bytesPerPixel());
353  SkASSERT(kRGBA_F16_SkColorType == fInfo.colorType() ||
354  kRGBA_F16Norm_SkColorType == fInfo.colorType());
355  return reinterpret_cast<const uint16_t*>(fPixels);
356  }
357 
370  const uint8_t* addr8(int x, int y) const {
371  SkASSERT((unsigned)x < (unsigned)fInfo.width());
372  SkASSERT((unsigned)y < (unsigned)fInfo.height());
373  return (const uint8_t*)((const char*)this->addr8() + (size_t)y * fRowBytes + (x << 0));
374  }
375 
388  const uint16_t* addr16(int x, int y) const {
389  SkASSERT((unsigned)x < (unsigned)fInfo.width());
390  SkASSERT((unsigned)y < (unsigned)fInfo.height());
391  return (const uint16_t*)((const char*)this->addr16() + (size_t)y * fRowBytes + (x << 1));
392  }
393 
406  const uint32_t* addr32(int x, int y) const {
407  SkASSERT((unsigned)x < (unsigned)fInfo.width());
408  SkASSERT((unsigned)y < (unsigned)fInfo.height());
409  return (const uint32_t*)((const char*)this->addr32() + (size_t)y * fRowBytes + (x << 2));
410  }
411 
424  const uint64_t* addr64(int x, int y) const {
425  SkASSERT((unsigned)x < (unsigned)fInfo.width());
426  SkASSERT((unsigned)y < (unsigned)fInfo.height());
427  return (const uint64_t*)((const char*)this->addr64() + (size_t)y * fRowBytes + (x << 3));
428  }
429 
445  const uint16_t* addrF16(int x, int y) const {
446  SkASSERT(kRGBA_F16_SkColorType == fInfo.colorType() ||
447  kRGBA_F16Norm_SkColorType == fInfo.colorType());
448  return reinterpret_cast<const uint16_t*>(this->addr64(x, y));
449  }
450 
455  void* writable_addr() const { return const_cast<void*>(fPixels); }
456 
466  void* writable_addr(int x, int y) const {
467  return const_cast<void*>(this->addr(x, y));
468  }
469 
480  uint8_t* writable_addr8(int x, int y) const {
481  return const_cast<uint8_t*>(this->addr8(x, y));
482  }
483 
494  uint16_t* writable_addr16(int x, int y) const {
495  return const_cast<uint16_t*>(this->addr16(x, y));
496  }
497 
509  uint32_t* writable_addr32(int x, int y) const {
510  return const_cast<uint32_t*>(this->addr32(x, y));
511  }
512 
523  uint64_t* writable_addr64(int x, int y) const {
524  return const_cast<uint64_t*>(this->addr64(x, y));
525  }
526 
538  uint16_t* writable_addrF16(int x, int y) const {
539  return reinterpret_cast<uint16_t*>(writable_addr64(x, y));
540  }
541 
564  bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes) const {
565  return this->readPixels(dstInfo, dstPixels, dstRowBytes, 0, 0);
566  }
567 
594  bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, int srcX,
595  int srcY) const;
596 
619  bool readPixels(const SkPixmap& dst, int srcX, int srcY) const {
620  return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), srcX, srcY);
621  }
622 
640  bool readPixels(const SkPixmap& dst) const {
641  return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), 0, 0);
642  }
643 
663  bool scalePixels(const SkPixmap& dst, const SkSamplingOptions&) const;
664 
675  bool erase(SkColor color, const SkIRect& subset) const;
676 
684  bool erase(SkColor color) const { return this->erase(color, this->bounds()); }
685 
697  bool erase(const SkColor4f& color, const SkIRect* subset = nullptr) const {
698  return this->erase(color, nullptr, subset);
699  }
700 
711  bool erase(const SkColor4f& color, SkColorSpace* cs, const SkIRect* subset = nullptr) const;
712 
713 private:
714  const void* fPixels;
715  size_t fRowBytes;
716  SkImageInfo fInfo;
717 
718  friend class SkPixmapPriv;
719 };
720 
721 #endif
SkPixmap::reset
bool SK_WARN_UNUSED_RESULT reset(const SkMask &mask)
Deprecated.
SkPixmap::addr
const void * addr(int x, int y) const
Returns readable pixel address at (x, y).
Definition: SkPixmap.h:286
SkPixmap::addrF16
const uint16_t * addrF16(int x, int y) const
Returns readable pixel address at (x, y).
Definition: SkPixmap.h:445
SkPixmap::writable_addr32
uint32_t * writable_addr32(int x, int y) const
Returns writable pixel address at (x, y).
Definition: SkPixmap.h:509
SkPixmap::width
int width() const
Returns pixel count in each pixel row.
Definition: SkPixmap.h:151
SkPixmap::alphaType
SkAlphaType alphaType() const
Definition: SkPixmap.h:166
SkPixmap::readPixels
bool readPixels(const SkPixmap &dst) const
Copies pixels inside bounds() to dst.
Definition: SkPixmap.h:640
SkColor4f
SkPixmap::writable_addr
void * writable_addr(int x, int y) const
Returns writable pixel address at (x, y).
Definition: SkPixmap.h:466
SkPixmap::writable_addr
void * writable_addr() const
Returns writable base pixel address.
Definition: SkPixmap.h:455
SkPixmap::dimensions
SkISize dimensions() const
Return the dimensions of the pixmap (from its ImageInfo)
Definition: SkPixmap.h:162
SkPixmap::shiftPerPixel
int shiftPerPixel() const
Returns bit shift converting row bytes to row pixels.
Definition: SkPixmap.h:212
SkPixmap::addr8
const uint8_t * addr8(int x, int y) const
Returns readable pixel address at (x, y).
Definition: SkPixmap.h:370
SkPixmap::writable_addr64
uint64_t * writable_addr64(int x, int y) const
Returns writable pixel address at (x, y).
Definition: SkPixmap.h:523
SkPixmap::addr64
const uint64_t * addr64() const
Returns readable base pixel address.
Definition: SkPixmap.h:337
SkData
SkData holds an immutable data buffer.
Definition: SkData.h:22
SkPixmap::writable_addr8
uint8_t * writable_addr8(int x, int y) const
Returns writable pixel address at (x, y).
Definition: SkPixmap.h:480
SkPixmap::getColor
SkColor getColor(int x, int y) const
Returns pixel at (x, y) as unpremultiplied color.
sk_sp< SkColorSpace >
SkSamplingOptions
Definition: SkSamplingOptions.h:50
SkPixmap::addr64
const uint64_t * addr64(int x, int y) const
Returns readable pixel address at (x, y).
Definition: SkPixmap.h:424
SkPixmap::rowBytes
size_t rowBytes() const
Returns row bytes, the interval from one pixel row to the next.
Definition: SkPixmap.h:136
SkPixmap::erase
bool erase(const SkColor4f &color, SkColorSpace *cs, const SkIRect *subset=nullptr) const
Writes color to pixels bounded by subset; returns true on success.
SkPixmap::extractSubset
bool SK_WARN_UNUSED_RESULT extractSubset(SkPixmap *subset, const SkIRect &area) const
Sets subset width, height, pixel address to intersection of SkPixmap with area, if intersection is no...
SkPixmap::writable_addrF16
uint16_t * writable_addrF16(int x, int y) const
Returns writable pixel address at (x, y).
Definition: SkPixmap.h:538
SkPixmap::readPixels
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY) const
Copies a SkRect of pixels to dstPixels.
SK_API
#define SK_API
Definition: SkTypes.h:181
SkASSERT
#define SkASSERT(cond)
Definition: SkTypes.h:460
SkPixmap::getAlphaf
float getAlphaf(int x, int y) const
Look up the pixel at (x,y) and return its alpha component, normalized to [0..1].
kRGBA_F16_SkColorType
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition: SkImageInfo.h:79
SkImageInfo
Definition: SkImageInfo.h:283
SkPixmap::addr8
const uint8_t * addr8() const
Returns readable base pixel address.
Definition: SkPixmap.h:298
SkPixmap::isOpaque
bool isOpaque() const
Returns true if SkAlphaType is kOpaque_SkAlphaType.
Definition: SkPixmap.h:192
SkPixmap::erase
bool erase(const SkColor4f &color, const SkIRect *subset=nullptr) const
Writes color to pixels bounded by subset; returns true on success.
Definition: SkPixmap.h:697
SkISize
Definition: SkSize.h:13
SkPixmap::addrF16
const uint16_t * addrF16() const
Returns readable base pixel address.
Definition: SkPixmap.h:351
SkIRect::MakeWH
static constexpr SkIRect SK_WARN_UNUSED_RESULT MakeWH(int32_t w, int32_t h)
Returns constructed SkIRect set to (0, 0, w, h).
Definition: SkRect.h:52
SkFilterQuality.h
SkPixmap::addr16
const uint16_t * addr16(int x, int y) const
Returns readable pixel address at (x, y).
Definition: SkPixmap.h:388
SkPixmap::scalePixels
bool scalePixels(const SkPixmap &dst, const SkSamplingOptions &) const
Copies SkBitmap to dst, scaling pixels to fit dst.width() and dst.height(), and converting pixels to ...
SkPixmap::addr16
const uint16_t * addr16() const
Returns readable base pixel address.
Definition: SkPixmap.h:311
SkPixmap::addr32
const uint32_t * addr32() const
Returns readable base pixel address.
Definition: SkPixmap.h:324
SkPixmap::computeByteSize
size_t computeByteSize() const
Returns minimum memory required for pixel storage.
Definition: SkPixmap.h:222
kRGBA_F16Norm_SkColorType
@ kRGBA_F16Norm_SkColorType
pixel with half floats in [0,1] for red, green, blue, alpha;
Definition: SkImageInfo.h:77
SkPixmap
Definition: SkPixmap.h:31
SkPixmap::addr32
const uint32_t * addr32(int x, int y) const
Returns readable pixel address at (x, y).
Definition: SkPixmap.h:406
SkPixmap::addr
const void * addr() const
Returns pixel address, the base address corresponding to the pixel origin.
Definition: SkPixmap.h:144
SkPixmap::colorSpace
SkColorSpace * colorSpace() const
Returns SkColorSpace, the range of colors, associated with SkImageInfo.
Definition: SkPixmap.h:174
SkPixmap::refColorSpace
sk_sp< SkColorSpace > refColorSpace() const
Returns smart pointer to SkColorSpace, the range of colors, associated with SkImageInfo.
Definition: SkPixmap.h:184
SkIRect
Definition: SkRect.h:28
SkPixmap::writable_addr16
uint16_t * writable_addr16(int x, int y) const
Returns writable_addr pixel address at (x, y).
Definition: SkPixmap.h:494
SK_WARN_UNUSED_RESULT
#define SK_WARN_UNUSED_RESULT
Definition: SkTypes.h:60
SkPixmap::info
const SkImageInfo & info() const
Returns width, height, SkAlphaType, SkColorType, and SkColorSpace.
Definition: SkPixmap.h:126
SkPixmap::readPixels
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes) const
Copies a SkRect of pixels to dstPixels.
Definition: SkPixmap.h:564
SkPixmap::computeIsOpaque
bool computeIsOpaque() const
Returns true if all pixels are opaque.
SkPixmap::SkPixmap
SkPixmap(const SkImageInfo &info, const void *addr, size_t rowBytes)
Creates SkPixmap from info width, height, SkAlphaType, and SkColorType.
Definition: SkPixmap.h:63
SkPixmap::reset
void reset(const SkImageInfo &info, const void *addr, size_t rowBytes)
Sets width, height, SkAlphaType, and SkColorType from info.
SkColor
uint32_t SkColor
32-bit ARGB color value, unpremultiplied.
Definition: SkColor.h:35
SkColorType
SkColorType
Definition: SkImageInfo.h:64
SkPixmap::colorType
SkColorType colorType() const
Definition: SkPixmap.h:164
SkPixmap::height
int height() const
Returns pixel row count.
Definition: SkPixmap.h:157
SkPixmap::setColorSpace
void setColorSpace(sk_sp< SkColorSpace > colorSpace)
Changes SkColorSpace in SkImageInfo; preserves width, height, SkAlphaType, and SkColorType in SkImage...
SkPixmap::erase
bool erase(SkColor color) const
Writes color to pixels inside bounds(); returns true on success.
Definition: SkPixmap.h:684
SkColor.h
SkPixmap::rowBytesAsPixels
int rowBytesAsPixels() const
Returns number of pixels that fit on row.
Definition: SkPixmap.h:205
SkPixmap::bounds
SkIRect bounds() const
Returns SkIRect { 0, 0, width(), height() }.
Definition: SkPixmap.h:198
SkAlphaType
SkAlphaType
Definition: SkImageInfo.h:37
SkPixmap::reset
void reset()
Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to kUnknown_SkColorType;...
SkImageInfo.h
SkPixmap::erase
bool erase(SkColor color, const SkIRect &subset) const
Writes color to pixels bounded by subset; returns true on success.
SkPixmap::readPixels
bool readPixels(const SkPixmap &dst, int srcX, int srcY) const
Copies a SkRect of pixels to dst.
Definition: SkPixmap.h:619
SkPixmap::SkPixmap
SkPixmap()
Creates an empty SkPixmap without pixels, with kUnknown_SkColorType, with kUnknown_SkAlphaType,...
Definition: SkPixmap.h:41
SkColorSpace
Definition: SkColorSpace.h:104
SkSamplingOptions.h