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"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkSize.h"
18 #include "include/private/base/SkAPI.h"
19 #include "include/private/base/SkAssert.h"
20 
21 #include <cstddef>
22 #include <cstdint>
23 
24 class SkColorSpace;
25 enum SkAlphaType : int;
26 struct SkMask;
27 
40 class SK_API SkPixmap {
41 public:
42 
51  : fPixels(nullptr), fRowBytes(0), fInfo(SkImageInfo::MakeUnknown(0, 0))
52  {}
53 
72  SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes)
73  : fPixels(addr), fRowBytes(rowBytes), fInfo(info)
74  {}
75 
84  void reset();
85 
103  void reset(const SkImageInfo& info, const void* addr, size_t rowBytes);
104 
114 
117  [[nodiscard]] bool reset(const SkMask& mask);
118 
129  [[nodiscard]] bool extractSubset(SkPixmap* subset, const SkIRect& area) const;
130 
135  const SkImageInfo& info() const { return fInfo; }
136 
145  size_t rowBytes() const { return fRowBytes; }
146 
153  const void* addr() const { return fPixels; }
154 
160  int width() const { return fInfo.width(); }
161 
166  int height() const { return fInfo.height(); }
167 
171  SkISize dimensions() const { return fInfo.dimensions(); }
172 
173  SkColorType colorType() const { return fInfo.colorType(); }
174 
175  SkAlphaType alphaType() const { return fInfo.alphaType(); }
176 
184 
194 
201  bool isOpaque() const { return fInfo.isOpaque(); }
202 
207  SkIRect bounds() const { return SkIRect::MakeWH(this->width(), this->height()); }
208 
214  int rowBytesAsPixels() const { return int(fRowBytes >> this->shiftPerPixel()); }
215 
221  int shiftPerPixel() const { return fInfo.shiftPerPixel(); }
222 
231  size_t computeByteSize() const { return fInfo.computeByteSize(fRowBytes); }
232 
251  bool computeIsOpaque() const;
252 
271  SkColor getColor(int x, int y) const;
272 
290  SkColor4f getColor4f(int x, int y) const;
291 
300  float getAlphaf(int x, int y) const;
301 
314  const void* addr(int x, int y) const {
315  return (const char*)fPixels + fInfo.computeOffset(x, y, fRowBytes);
316  }
317 
326  const uint8_t* addr8() const {
327  SkASSERT(1 == fInfo.bytesPerPixel());
328  return reinterpret_cast<const uint8_t*>(fPixels);
329  }
330 
339  const uint16_t* addr16() const {
340  SkASSERT(2 == fInfo.bytesPerPixel());
341  return reinterpret_cast<const uint16_t*>(fPixels);
342  }
343 
352  const uint32_t* addr32() const {
353  SkASSERT(4 == fInfo.bytesPerPixel());
354  return reinterpret_cast<const uint32_t*>(fPixels);
355  }
356 
365  const uint64_t* addr64() const {
366  SkASSERT(8 == fInfo.bytesPerPixel());
367  return reinterpret_cast<const uint64_t*>(fPixels);
368  }
369 
379  const uint16_t* addrF16() const {
380  SkASSERT(8 == fInfo.bytesPerPixel());
381  SkASSERT(kRGBA_F16_SkColorType == fInfo.colorType() ||
382  kRGBA_F16Norm_SkColorType == fInfo.colorType());
383  return reinterpret_cast<const uint16_t*>(fPixels);
384  }
385 
398  const uint8_t* addr8(int x, int y) const {
399  SkASSERT((unsigned)x < (unsigned)fInfo.width());
400  SkASSERT((unsigned)y < (unsigned)fInfo.height());
401  return (const uint8_t*)((const char*)this->addr8() + (size_t)y * fRowBytes + (x << 0));
402  }
403 
416  const uint16_t* addr16(int x, int y) const {
417  SkASSERT((unsigned)x < (unsigned)fInfo.width());
418  SkASSERT((unsigned)y < (unsigned)fInfo.height());
419  return (const uint16_t*)((const char*)this->addr16() + (size_t)y * fRowBytes + (x << 1));
420  }
421 
434  const uint32_t* addr32(int x, int y) const {
435  SkASSERT((unsigned)x < (unsigned)fInfo.width());
436  SkASSERT((unsigned)y < (unsigned)fInfo.height());
437  return (const uint32_t*)((const char*)this->addr32() + (size_t)y * fRowBytes + (x << 2));
438  }
439 
452  const uint64_t* addr64(int x, int y) const {
453  SkASSERT((unsigned)x < (unsigned)fInfo.width());
454  SkASSERT((unsigned)y < (unsigned)fInfo.height());
455  return (const uint64_t*)((const char*)this->addr64() + (size_t)y * fRowBytes + (x << 3));
456  }
457 
473  const uint16_t* addrF16(int x, int y) const {
474  SkASSERT(kRGBA_F16_SkColorType == fInfo.colorType() ||
475  kRGBA_F16Norm_SkColorType == fInfo.colorType());
476  return reinterpret_cast<const uint16_t*>(this->addr64(x, y));
477  }
478 
483  void* writable_addr() const { return const_cast<void*>(fPixels); }
484 
494  void* writable_addr(int x, int y) const {
495  return const_cast<void*>(this->addr(x, y));
496  }
497 
508  uint8_t* writable_addr8(int x, int y) const {
509  return const_cast<uint8_t*>(this->addr8(x, y));
510  }
511 
522  uint16_t* writable_addr16(int x, int y) const {
523  return const_cast<uint16_t*>(this->addr16(x, y));
524  }
525 
537  uint32_t* writable_addr32(int x, int y) const {
538  return const_cast<uint32_t*>(this->addr32(x, y));
539  }
540 
551  uint64_t* writable_addr64(int x, int y) const {
552  return const_cast<uint64_t*>(this->addr64(x, y));
553  }
554 
566  uint16_t* writable_addrF16(int x, int y) const {
567  return reinterpret_cast<uint16_t*>(writable_addr64(x, y));
568  }
569 
592  bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes) const {
593  return this->readPixels(dstInfo, dstPixels, dstRowBytes, 0, 0);
594  }
595 
622  bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, int srcX,
623  int srcY) const;
624 
647  bool readPixels(const SkPixmap& dst, int srcX, int srcY) const {
648  return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), srcX, srcY);
649  }
650 
668  bool readPixels(const SkPixmap& dst) const {
669  return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), 0, 0);
670  }
671 
691  bool scalePixels(const SkPixmap& dst, const SkSamplingOptions&) const;
692 
703  bool erase(SkColor color, const SkIRect& subset) const;
704 
712  bool erase(SkColor color) const { return this->erase(color, this->bounds()); }
713 
723  bool erase(const SkColor4f& color, const SkIRect* subset = nullptr) const;
724 
725 private:
726  const void* fPixels;
727  size_t fRowBytes;
728  SkImageInfo fInfo;
729 };
730 
731 #endif
SkAlphaType
Describes how to interpret the alpha component of a pixel.
Definition: SkAlphaType.h:26
SkColorType
Describes how pixel bits encode color.
Definition: SkColorType.h:19
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition: SkColorType.h:37
@ kRGBA_F16Norm_SkColorType
pixel with half floats in [0,1] for red, green, blue, alpha;
Definition: SkColorType.h:35
Types, consts, functions, and macros for colors.
uint32_t SkColor
32-bit ARGB color value, unpremultiplied.
Definition: SkColor.h:37
Definition: SkColorSpace.h:107
SkPixmap provides a utility to pair SkImageInfo with pixels and row bytes.
Definition: SkPixmap.h:40
uint32_t * writable_addr32(int x, int y) const
Returns writable pixel address at (x, y).
Definition: SkPixmap.h:537
const uint16_t * addr16(int x, int y) const
Returns readable pixel address at (x, y).
Definition: SkPixmap.h:416
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 ...
void reset(const SkImageInfo &info, const void *addr, size_t rowBytes)
Sets width, height, SkAlphaType, and SkColorType from info.
SkColor getColor(int x, int y) const
Returns pixel at (x, y) as unpremultiplied color.
const uint16_t * addrF16(int x, int y) const
Returns readable pixel address at (x, y).
Definition: SkPixmap.h:473
uint8_t * writable_addr8(int x, int y) const
Returns writable pixel address at (x, y).
Definition: SkPixmap.h:508
const uint8_t * addr8(int x, int y) const
Returns readable pixel address at (x, y).
Definition: SkPixmap.h:398
bool computeIsOpaque() const
Returns true if all pixels are opaque.
uint64_t * writable_addr64(int x, int y) const
Returns writable pixel address at (x, y).
Definition: SkPixmap.h:551
SkColorSpace * colorSpace() const
Returns SkColorSpace, the range of colors, associated with SkImageInfo.
SkIRect bounds() const
Returns SkIRect { 0, 0, width(), height() }.
Definition: SkPixmap.h:207
const uint64_t * addr64(int x, int y) const
Returns readable pixel address at (x, y).
Definition: SkPixmap.h:452
bool erase(SkColor color, const SkIRect &subset) const
Writes color to pixels bounded by subset; returns true on success.
uint16_t * writable_addrF16(int x, int y) const
Returns writable pixel address at (x, y).
Definition: SkPixmap.h:566
uint16_t * writable_addr16(int x, int y) const
Returns writable_addr pixel address at (x, y).
Definition: SkPixmap.h:522
size_t rowBytes() const
Returns row bytes, the interval from one pixel row to the next.
Definition: SkPixmap.h:145
int width() const
Returns pixel count in each pixel row.
Definition: SkPixmap.h:160
void * writable_addr(int x, int y) const
Returns writable pixel address at (x, y).
Definition: SkPixmap.h:494
const uint8_t * addr8() const
Returns readable base pixel address.
Definition: SkPixmap.h:326
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes) const
Copies a SkRect of pixels to dstPixels.
Definition: SkPixmap.h:592
const void * addr() const
Returns pixel address, the base address corresponding to the pixel origin.
Definition: SkPixmap.h:153
SkColorType colorType() const
Definition: SkPixmap.h:173
SkPixmap()
Creates an empty SkPixmap without pixels, with kUnknown_SkColorType, with kUnknown_SkAlphaType,...
Definition: SkPixmap.h:50
sk_sp< SkColorSpace > refColorSpace() const
Returns smart pointer to SkColorSpace, the range of colors, associated with SkImageInfo.
const uint32_t * addr32() const
Returns readable base pixel address.
Definition: SkPixmap.h:352
bool isOpaque() const
Returns true if SkAlphaType is kOpaque_SkAlphaType.
Definition: SkPixmap.h:201
const uint16_t * addr16() const
Returns readable base pixel address.
Definition: SkPixmap.h:339
const uint32_t * addr32(int x, int y) const
Returns readable pixel address at (x, y).
Definition: SkPixmap.h:434
bool reset(const SkMask &mask)
Deprecated.
void setColorSpace(sk_sp< SkColorSpace > colorSpace)
Changes SkColorSpace in SkImageInfo; preserves width, height, SkAlphaType, and SkColorType in SkImage...
const void * addr(int x, int y) const
Returns readable pixel address at (x, y).
Definition: SkPixmap.h:314
const uint16_t * addrF16() const
Returns readable base pixel address.
Definition: SkPixmap.h:379
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY) const
Copies a SkRect of pixels to dstPixels.
SkColor4f getColor4f(int x, int y) const
Returns pixel at (x, y) as unpremultiplied color as an SkColor4f.
bool erase(const SkColor4f &color, const SkIRect *subset=nullptr) const
Writes color to pixels bounded by subset; returns true on success.
bool readPixels(const SkPixmap &dst, int srcX, int srcY) const
Copies a SkRect of pixels to dst.
Definition: SkPixmap.h:647
int rowBytesAsPixels() const
Returns number of pixels that fit on row.
Definition: SkPixmap.h:214
size_t computeByteSize() const
Returns minimum memory required for pixel storage.
Definition: SkPixmap.h:231
float getAlphaf(int x, int y) const
Look up the pixel at (x,y) and return its alpha component, normalized to [0..1].
int shiftPerPixel() const
Returns bit shift converting row bytes to row pixels.
Definition: SkPixmap.h:221
const uint64_t * addr64() const
Returns readable base pixel address.
Definition: SkPixmap.h:365
int height() const
Returns pixel row count.
Definition: SkPixmap.h:166
SkAlphaType alphaType() const
Definition: SkPixmap.h:175
SkISize dimensions() const
Return the dimensions of the pixmap (from its ImageInfo)
Definition: SkPixmap.h:171
void * writable_addr() const
Returns writable base pixel address.
Definition: SkPixmap.h:483
bool erase(SkColor color) const
Writes color to pixels inside bounds(); returns true on success.
Definition: SkPixmap.h:712
bool readPixels(const SkPixmap &dst) const
Copies pixels inside bounds() to dst.
Definition: SkPixmap.h:668
const SkImageInfo & info() const
Returns width, height, SkAlphaType, SkColorType, and SkColorSpace.
Definition: SkPixmap.h:135
void reset()
Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to kUnknown_SkColorType;...
bool extractSubset(SkPixmap *subset, const SkIRect &area) const
Sets subset width, height, pixel address to intersection of SkPixmap with area, if intersection is no...
SkPixmap(const SkImageInfo &info, const void *addr, size_t rowBytes)
Creates SkPixmap from info width, height, SkAlphaType, and SkColorType.
Definition: SkPixmap.h:72
RGBA color value, holding four floating point components.
SkIRect holds four 32-bit integer coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:32
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Returns constructed SkIRect set to (0, 0, w, h).
Definition: SkRect.h:56
Definition: SkSize.h:15
Describes pixel dimensions and encoding.
Definition: SkImageInfo.h:194
Definition: SkSamplingOptions.h:58