Skia
2D Graphics Library
SkBitmap.h
Go to the documentation of this file.
1 /*
2  * Copyright 2006 The Android Open Source Project
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 SkBitmap_DEFINED
9 #define SkBitmap_DEFINED
10 
12 #include "include/core/SkColor.h"
14 #include "include/core/SkPixmap.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkSize.h"
20 #include "include/core/SkTypes.h"
21 #include "include/private/base/SkCPUTypes.h"
22 #include "include/private/base/SkDebug.h"
23 
24 #include <cstddef>
25 #include <cstdint>
26 
27 class SkColorSpace;
28 class SkImage;
29 class SkMatrix;
30 class SkMipmap;
31 class SkPaint;
32 class SkPixelRef;
33 class SkShader;
34 enum SkColorType : int;
35 enum class SkTileMode;
36 struct SkMaskBuilder;
37 
59 class SK_API SkBitmap {
60 public:
61  class SK_API Allocator;
62 
75 
84  SkBitmap(const SkBitmap& src);
85 
95 
99 
109 
119 
126  void swap(SkBitmap& other);
127 
133  const SkPixmap& pixmap() const { return fPixmap; }
134 
139  const SkImageInfo& info() const { return fPixmap.info(); }
140 
149  int width() const { return fPixmap.width(); }
150 
158  int height() const { return fPixmap.height(); }
159 
160  SkColorType colorType() const { return fPixmap.colorType(); }
161 
162  SkAlphaType alphaType() const { return fPixmap.alphaType(); }
163 
171 
181 
187  int bytesPerPixel() const { return fPixmap.info().bytesPerPixel(); }
188 
194  int rowBytesAsPixels() const { return fPixmap.rowBytesAsPixels(); }
195 
201  int shiftPerPixel() const { return fPixmap.shiftPerPixel(); }
202 
210  bool empty() const { return fPixmap.info().isEmpty(); }
211 
219  bool isNull() const { return nullptr == fPixelRef; }
220 
226  bool drawsNothing() const {
227  return this->empty() || this->isNull();
228  }
229 
238  size_t rowBytes() const { return fPixmap.rowBytes(); }
239 
267  bool setAlphaType(SkAlphaType alphaType);
268 
273  void* getPixels() const { return fPixmap.writable_addr(); }
274 
283  size_t computeByteSize() const { return fPixmap.computeByteSize(); }
284 
293  bool isImmutable() const;
294 
303  void setImmutable();
304 
314  bool isOpaque() const {
315  return SkAlphaTypeIsOpaque(this->alphaType());
316  }
317 
329  void reset();
330 
348  static bool ComputeIsOpaque(const SkBitmap& bm) {
349  return bm.pixmap().computeIsOpaque();
350  }
351 
358  void getBounds(SkRect* bounds) const;
359 
366  void getBounds(SkIRect* bounds) const;
367 
372  SkIRect bounds() const { return fPixmap.info().bounds(); }
373 
378  SkISize dimensions() const { return fPixmap.info().dimensions(); }
379 
384  SkIRect getSubset() const {
385  SkIPoint origin = this->pixelRefOrigin();
386  return SkIRect::MakeXYWH(origin.x(), origin.y(), this->width(), this->height());
387  }
388 
419  bool setInfo(const SkImageInfo& imageInfo, size_t rowBytes = 0);
420 
424  enum AllocFlags {
425  kZeroPixels_AllocFlag = 1 << 0,
426  };
427 
443  [[nodiscard]] bool tryAllocPixelsFlags(const SkImageInfo& info, uint32_t flags);
444 
463  void allocPixelsFlags(const SkImageInfo& info, uint32_t flags);
464 
481  [[nodiscard]] bool tryAllocPixels(const SkImageInfo& info, size_t rowBytes);
482 
501  void allocPixels(const SkImageInfo& info, size_t rowBytes);
502 
517  [[nodiscard]] bool tryAllocPixels(const SkImageInfo& info) {
518  return this->tryAllocPixels(info, info.minRowBytes());
519  }
520 
537  void allocPixels(const SkImageInfo& info);
538 
556  [[nodiscard]] bool tryAllocN32Pixels(int width, int height, bool isOpaque = false);
557 
575  void allocN32Pixels(int width, int height, bool isOpaque = false);
576 
599  bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
600  void (*releaseProc)(void* addr, void* context), void* context);
601 
617  bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
618  return this->installPixels(info, pixels, rowBytes, nullptr, nullptr);
619  }
620 
636  bool installPixels(const SkPixmap& pixmap);
637 
640  bool installMaskPixels(SkMaskBuilder& mask);
641 
655  void setPixels(void* pixels);
656 
664  [[nodiscard]] bool tryAllocPixels() {
665  return this->tryAllocPixels((Allocator*)nullptr);
666  }
667 
677  void allocPixels();
678 
688  [[nodiscard]] bool tryAllocPixels(Allocator* allocator);
689 
701  void allocPixels(Allocator* allocator);
702 
710  SkPixelRef* pixelRef() const { return fPixelRef.get(); }
711 
725  SkIPoint pixelRefOrigin() const;
726 
742  void setPixelRef(sk_sp<SkPixelRef> pixelRef, int dx, int dy);
743 
748  bool readyToDraw() const {
749  return this->getPixels() != nullptr;
750  }
751 
762  uint32_t getGenerationID() const;
763 
769  void notifyPixelsChanged() const;
770 
780  void eraseColor(SkColor4f) const;
781 
794  void eraseColor(SkColor c) const;
795 
807  void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
808  this->eraseColor(SkColorSetARGB(a, r, g, b));
809  }
810 
823  void erase(SkColor4f c, const SkIRect& area) const;
824 
840  void erase(SkColor c, const SkIRect& area) const;
841 
844  void eraseArea(const SkIRect& area, SkColor c) const {
845  this->erase(c, area);
846  }
847 
864  SkColor getColor(int x, int y) const {
865  return this->pixmap().getColor(x, y);
866  }
867 
883  SkColor4f getColor4f(int x, int y) const { return this->pixmap().getColor4f(x, y); }
884 
893  float getAlphaf(int x, int y) const {
894  return this->pixmap().getAlphaf(x, y);
895  }
896 
912  void* getAddr(int x, int y) const;
913 
926  inline uint32_t* getAddr32(int x, int y) const;
927 
940  inline uint16_t* getAddr16(int x, int y) const;
941 
954  inline uint8_t* getAddr8(int x, int y) const;
955 
975  bool extractSubset(SkBitmap* dst, const SkIRect& subset) const;
976 
1005  bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1006  int srcX, int srcY) const;
1007 
1036  bool readPixels(const SkPixmap& dst, int srcX, int srcY) const;
1037 
1058  bool readPixels(const SkPixmap& dst) const {
1059  return this->readPixels(dst, 0, 0);
1060  }
1061 
1090  bool writePixels(const SkPixmap& src, int dstX, int dstY);
1091 
1112  bool writePixels(const SkPixmap& src) {
1113  return this->writePixels(src, 0, 0);
1114  }
1115 
1124  bool extractAlpha(SkBitmap* dst) const {
1125  return this->extractAlpha(dst, nullptr, nullptr, nullptr);
1126  }
1127 
1141  bool extractAlpha(SkBitmap* dst, const SkPaint* paint,
1142  SkIPoint* offset) const {
1143  return this->extractAlpha(dst, paint, nullptr, offset);
1144  }
1145 
1161  bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator,
1162  SkIPoint* offset) const;
1163 
1175  bool peekPixels(SkPixmap* pixmap) const;
1176 
1181  const SkMatrix* localMatrix = nullptr) const;
1183  const SkMatrix& lm) const;
1185  sk_sp<SkShader> makeShader(const SkSamplingOptions& sampling, const SkMatrix& lm) const;
1187  const SkMatrix* lm = nullptr) const;
1188 
1194 
1198  SkDEBUGCODE(void validate() const;)
1199 
1203  class Allocator : public SkRefCnt {
1204  public:
1205 
1213  virtual bool allocPixelRef(SkBitmap* bitmap) = 0;
1214  private:
1215  using INHERITED = SkRefCnt;
1216  };
1217 
1223  class HeapAllocator : public Allocator {
1224  public:
1225 
1235  bool allocPixelRef(SkBitmap* bitmap) override;
1236  };
1237 
1238 private:
1239  sk_sp<SkPixelRef> fPixelRef;
1240  SkPixmap fPixmap;
1241  sk_sp<SkMipmap> fMips;
1242 
1243  friend class SkImage_Raster;
1244  friend class SkReadBuffer; // unflatten
1245  friend class GrProxyProvider; // fMips
1246 };
1247 
1249 
1250 inline uint32_t* SkBitmap::getAddr32(int x, int y) const {
1251  SkASSERT(fPixmap.addr());
1252  return fPixmap.writable_addr32(x, y);
1253 }
1254 
1255 inline uint16_t* SkBitmap::getAddr16(int x, int y) const {
1256  SkASSERT(fPixmap.addr());
1257  return fPixmap.writable_addr16(x, y);
1258 }
1259 
1260 inline uint8_t* SkBitmap::getAddr8(int x, int y) const {
1261  SkASSERT(fPixmap.addr());
1262  return fPixmap.writable_addr8(x, y);
1263 }
1264 
1265 #endif
static bool SkAlphaTypeIsOpaque(SkAlphaType at)
Returns true if SkAlphaType equals kOpaque_SkAlphaType.
Definition: SkAlphaType.h:41
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
Types, consts, functions, and macros for colors.
uint32_t SkColor
32-bit ARGB color value, unpremultiplied.
Definition: SkColor.h:37
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Returns color value from 8-bit component values.
Definition: SkColor.h:49
SkTileMode
Definition: SkTileMode.h:13
Asserts if internal values are illegal or inconsistent.
Definition: SkBitmap.h:1198
Subclass of SkBitmap::Allocator that returns a SkPixelRef that allocates its pixel memory from the he...
Definition: SkBitmap.h:1223
bool allocPixelRef(SkBitmap *bitmap) override
Allocates the pixel memory for the bitmap, given its dimensions and SkColorType.
SkBitmap describes a two-dimensional raster pixel array.
Definition: SkBitmap.h:59
bool extractAlpha(SkBitmap *dst) const
Sets dst to alpha described by pixels.
Definition: SkBitmap.h:1124
bool installMaskPixels(SkMaskBuilder &mask)
Deprecated.
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Sets SkImageInfo to info following the rules in setInfo() and allocates pixel memory.
void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const
Replaces pixel values with unpremultiplied color built from a, r, g, and b, interpreted as being in t...
Definition: SkBitmap.h:807
SkColorSpace * colorSpace() const
Returns SkColorSpace, the range of colors, associated with SkImageInfo.
int shiftPerPixel() const
Returns bit shift converting row bytes to row pixels.
Definition: SkBitmap.h:201
sk_sp< SkColorSpace > refColorSpace() const
Returns smart pointer to SkColorSpace, the range of colors, associated with SkImageInfo.
void setImmutable()
Sets internal flag to mark SkBitmap as immutable.
bool installPixels(const SkImageInfo &info, void *pixels, size_t rowBytes, void(*releaseProc)(void *addr, void *context), void *context)
Sets SkImageInfo to info following the rules in setInfo(), and creates SkPixelRef containing pixels a...
SkIPoint pixelRefOrigin() const
Returns origin of pixels within SkPixelRef.
void notifyPixelsChanged() const
Marks that pixels in SkPixelRef have changed.
void getBounds(SkRect *bounds) const
Returns SkRect { 0, 0, width(), height() }.
SkAlphaType alphaType() const
Definition: SkBitmap.h:162
bool tryAllocPixels(Allocator *allocator)
Allocates pixel memory with allocator, and replaces existing SkPixelRef.
bool extractAlpha(SkBitmap *dst, const SkPaint *paint, Allocator *allocator, SkIPoint *offset) const
Sets dst to alpha described by pixels.
bool tryAllocPixels()
Allocates pixel memory with HeapAllocator, and replaces existing SkPixelRef.
Definition: SkBitmap.h:664
bool readyToDraw() const
Returns true if SkBitmap is can be drawn.
Definition: SkBitmap.h:748
bool extractSubset(SkBitmap *dst, const SkIRect &subset) const
Shares SkPixelRef with dst.
bool writePixels(const SkPixmap &src)
Copies a SkRect of pixels from src.
Definition: SkBitmap.h:1112
bool installPixels(const SkImageInfo &info, void *pixels, size_t rowBytes)
Sets SkImageInfo to info following the rules in setInfo(), and creates SkPixelRef containing pixels a...
Definition: SkBitmap.h:617
SkBitmap & operator=(SkBitmap &&src)
Copies settings from src to returned SkBitmap.
SkISize dimensions() const
Returns SkISize { width(), height() }.
Definition: SkBitmap.h:378
const SkPixmap & pixmap() const
Returns a constant reference to the SkPixmap holding the SkBitmap pixel address, row bytes,...
Definition: SkBitmap.h:133
void eraseArea(const SkIRect &area, SkColor c) const
Deprecated.
Definition: SkBitmap.h:844
bool isOpaque() const
Returns true if SkAlphaType is set to hint that all pixels are opaque; their alpha value is implicitl...
Definition: SkBitmap.h:314
SkColor getColor(int x, int y) const
Returns pixel at (x, y) as unpremultiplied color.
Definition: SkBitmap.h:864
bool empty() const
Returns true if either width() or height() are zero.
Definition: SkBitmap.h:210
size_t computeByteSize() const
Returns minimum memory required for pixel storage.
Definition: SkBitmap.h:283
void * getAddr(int x, int y) const
Returns pixel address at (x, y).
uint8_t * getAddr8(int x, int y) const
Returns address at (x, y).
Definition: SkBitmap.h:1260
int width() const
Returns pixel count in each row.
Definition: SkBitmap.h:149
bool writePixels(const SkPixmap &src, int dstX, int dstY)
Copies a SkRect of pixels from src.
SkPixelRef * pixelRef() const
Returns SkPixelRef, which contains: pixel base address; its dimensions; and rowBytes(),...
Definition: SkBitmap.h:710
SkColor4f getColor4f(int x, int y) const
Returns pixel at (x, y) as unpremultiplied float color.
Definition: SkBitmap.h:883
bool extractAlpha(SkBitmap *dst, const SkPaint *paint, SkIPoint *offset) const
Sets dst to alpha described by pixels.
Definition: SkBitmap.h:1141
void eraseColor(SkColor c) const
Replaces pixel values with c, interpreted as being in the sRGB SkColorSpace.
bool isImmutable() const
Returns true if pixels can not change.
void allocPixels()
Allocates pixel memory with HeapAllocator, and replaces existing SkPixelRef.
void allocPixels(Allocator *allocator)
Allocates pixel memory with allocator, and replaces existing SkPixelRef.
size_t rowBytes() const
Returns row bytes, the interval from one pixel row to the next.
Definition: SkBitmap.h:238
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &sampling, const SkMatrix &lm) const
bool isNull() const
Returns true if SkPixelRef is nullptr.
Definition: SkBitmap.h:219
const SkImageInfo & info() const
Returns width, height, SkAlphaType, SkColorType, and SkColorSpace.
Definition: SkBitmap.h:139
void reset()
Resets to its initial state; all fields are set to zero, as if SkBitmap had been initialized by SkBit...
SkColorType colorType() const
Definition: SkBitmap.h:160
bool drawsNothing() const
Returns true if width() or height() are zero, or if SkPixelRef is nullptr.
Definition: SkBitmap.h:226
void setPixels(void *pixels)
Replaces SkPixelRef with pixels, preserving SkImageInfo and rowBytes().
~SkBitmap()
Decrements SkPixelRef reference count, if SkPixelRef is not nullptr.
sk_sp< SkShader > makeShader(const SkSamplingOptions &sampling, const SkMatrix *lm=nullptr) const
SkBitmap(SkBitmap &&src)
Copies settings from src to returned SkBitmap.
void getBounds(SkIRect *bounds) const
Returns SkIRect { 0, 0, width(), height() }.
SkIRect getSubset() const
Returns the bounds of this bitmap, offset by its SkPixelRef origin.
Definition: SkBitmap.h:384
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Make a shader with the specified tiling, matrix and sampling.
void * getPixels() const
Returns pixel address, the base address corresponding to the pixel origin.
Definition: SkBitmap.h:273
bool readPixels(const SkPixmap &dst) const
Copies a SkRect of pixels from SkBitmap to dst.
Definition: SkBitmap.h:1058
int bytesPerPixel() const
Returns number of bytes per pixel required by SkColorType.
Definition: SkBitmap.h:187
void erase(SkColor c, const SkIRect &area) const
Replaces pixel values inside area with c.
SkBitmap(const SkBitmap &src)
Copies settings from src to returned SkBitmap.
bool installPixels(const SkPixmap &pixmap)
Sets SkImageInfo to pixmap.info() following the rules in setInfo(), and creates SkPixelRef containing...
SkBitmap()
Creates an empty SkBitmap without pixels, with kUnknown_SkColorType, kUnknown_SkAlphaType,...
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY) const
Copies a SkRect of pixels from SkBitmap to dstPixels.
float getAlphaf(int x, int y) const
Look up the pixel at (x,y) and return its alpha component, normalized to [0..1].
Definition: SkBitmap.h:893
bool tryAllocPixels(const SkImageInfo &info)
Sets SkImageInfo to info following the rules in setInfo() and allocates pixel memory.
Definition: SkBitmap.h:517
uint32_t getGenerationID() const
Returns a unique value corresponding to the pixels in SkPixelRef.
void allocN32Pixels(int width, int height, bool isOpaque=false)
Sets SkImageInfo to width, height, and the native color type; and allocates pixel memory.
SkIRect bounds() const
Returns SkIRect { 0, 0, width(), height() }.
Definition: SkBitmap.h:372
SkBitmap & operator=(const SkBitmap &src)
Copies settings from src to returned SkBitmap.
bool readPixels(const SkPixmap &dst, int srcX, int srcY) const
Copies a SkRect of pixels from SkBitmap to dst.
bool peekPixels(SkPixmap *pixmap) const
Copies SkBitmap pixel address, row bytes, and SkImageInfo to pixmap, if address is available,...
bool setInfo(const SkImageInfo &imageInfo, size_t rowBytes=0)
Sets width, height, SkAlphaType, SkColorType, SkColorSpace, and optional rowBytes.
static bool ComputeIsOpaque(const SkBitmap &bm)
Returns true if all pixels are opaque.
Definition: SkBitmap.h:348
bool setAlphaType(SkAlphaType alphaType)
Sets SkAlphaType, if alphaType is compatible with SkColorType.
AllocFlags
Definition: SkBitmap.h:424
int height() const
Returns pixel row count.
Definition: SkBitmap.h:158
uint32_t * getAddr32(int x, int y) const
Returns address at (x, y).
Definition: SkBitmap.h:1250
void allocPixelsFlags(const SkImageInfo &info, uint32_t flags)
Sets SkImageInfo to info following the rules in setInfo() and allocates pixel memory.
void setPixelRef(sk_sp< SkPixelRef > pixelRef, int dx, int dy)
Replaces pixelRef and origin in SkBitmap.
int rowBytesAsPixels() const
Returns number of pixels that fit on row.
Definition: SkBitmap.h:194
sk_sp< SkImage > asImage() const
Returns a new image from the bitmap.
bool tryAllocN32Pixels(int width, int height, bool isOpaque=false)
Sets SkImageInfo to width, height, and native color type; and allocates pixel memory.
bool tryAllocPixels(const SkImageInfo &info, size_t rowBytes)
Sets SkImageInfo to info following the rules in setInfo() and allocates pixel memory.
void swap(SkBitmap &other)
Swaps the fields of the two bitmaps.
uint16_t * getAddr16(int x, int y) const
Returns address at (x, y).
Definition: SkBitmap.h:1255
void allocPixels(const SkImageInfo &info)
Sets SkImageInfo to info following the rules in setInfo() and allocates pixel memory.
sk_sp< SkShader > makeShader(const SkSamplingOptions &sampling, const SkMatrix &lm) const
Defaults to clamp in both X and Y.
bool tryAllocPixelsFlags(const SkImageInfo &info, uint32_t flags)
Sets SkImageInfo to info following the rules in setInfo() and allocates pixel memory.
void erase(SkColor4f c, const SkIRect &area) const
Replaces pixel values inside area with c.
void eraseColor(SkColor4f) const
Replaces pixel values with c, interpreted as being in the sRGB SkColorSpace.
Definition: SkColorSpace.h:107
SkImage describes a two dimensional array of pixels to draw.
Definition: SkImage.h:270
SkMatrix holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:53
SkPaint controls options applied when drawing.
Definition: SkPaint.h:44
This class is the smart container for pixel memory, and is used with SkBitmap.
Definition: SkPixelRef.h:28
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
uint8_t * writable_addr8(int x, int y) const
Returns writable pixel address at (x, y).
Definition: SkPixmap.h:508
bool computeIsOpaque() const
Returns true if all pixels are opaque.
uint16_t * writable_addr16(int x, int y) const
Returns writable_addr pixel address at (x, y).
Definition: SkPixmap.h:522
const void * addr() const
Returns pixel address, the base address corresponding to the pixel origin.
Definition: SkPixmap.h:153
Definition: SkRefCnt.h:119
Shaders specify the source color(s) for what is being drawn.
Definition: SkShader.h:35
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 MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Returns constructed SkIRect set to: (x, y, x + w, y + h).
Definition: SkRect.h:104
Definition: SkSize.h:15
Describes pixel dimensions and encoding.
Definition: SkImageInfo.h:194
size_t minRowBytes() const
Returns minimum bytes per row, computed from pixel width() and SkColorType, which specifies bytesPerP...
Definition: SkImageInfo.h:503
SkRect holds four float coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:582
Definition: SkSamplingOptions.h:58