Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkPixmap.h
1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkPixmap_DEFINED
9 #define SkPixmap_DEFINED
10 
11 #include "SkColor.h"
12 #include "SkFilterQuality.h"
13 #include "SkImageInfo.h"
14 
15 class SkColorTable;
16 class SkData;
17 struct SkMask;
18 
23 class SK_API SkPixmap {
24 public:
25  SkPixmap()
26  : fPixels(NULL), fCTable(NULL), fRowBytes(0), fInfo(SkImageInfo::MakeUnknown(0, 0))
27  {}
28 
29  SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes,
30  SkColorTable* ctable = NULL)
31  : fPixels(addr), fCTable(ctable), fRowBytes(rowBytes), fInfo(info)
32  {
33  if (kIndex_8_SkColorType == info.colorType()) {
34  SkASSERT(ctable);
35  } else {
36  SkASSERT(NULL == ctable);
37  }
38  }
39 
40  void reset();
41  void reset(const SkImageInfo& info, const void* addr, size_t rowBytes,
42  SkColorTable* ctable = NULL);
43  void reset(const SkImageInfo& info) {
44  this->reset(info, NULL, 0, NULL);
45  }
46 
47  // overrides the colorspace in the SkImageInfo of the pixmap
48  void setColorSpace(sk_sp<SkColorSpace>);
49 
54  bool SK_WARN_UNUSED_RESULT reset(const SkMask&);
55 
62  bool SK_WARN_UNUSED_RESULT extractSubset(SkPixmap* subset, const SkIRect& area) const;
63 
64  const SkImageInfo& info() const { return fInfo; }
65  size_t rowBytes() const { return fRowBytes; }
66  const void* addr() const { return fPixels; }
67  SkColorTable* ctable() const { return fCTable; }
68 
69  int width() const { return fInfo.width(); }
70  int height() const { return fInfo.height(); }
71  SkColorType colorType() const { return fInfo.colorType(); }
72  SkAlphaType alphaType() const { return fInfo.alphaType(); }
73  bool isOpaque() const { return fInfo.isOpaque(); }
74 
75  SkIRect bounds() const { return SkIRect::MakeWH(this->width(), this->height()); }
76 
80  int rowBytesAsPixels() const { return int(fRowBytes >> this->shiftPerPixel()); }
81 
86  int shiftPerPixel() const { return fInfo.shiftPerPixel(); }
87 
88  uint64_t getSize64() const { return sk_64_mul(fInfo.height(), fRowBytes); }
89  uint64_t getSafeSize64() const { return fInfo.getSafeSize64(fRowBytes); }
90  size_t getSafeSize() const { return fInfo.getSafeSize(fRowBytes); }
91 
92  const void* addr(int x, int y) const {
93  return (const char*)fPixels + fInfo.computeOffset(x, y, fRowBytes);
94  }
95  const uint8_t* addr8() const {
96  SkASSERT(1 == SkColorTypeBytesPerPixel(fInfo.colorType()));
97  return reinterpret_cast<const uint8_t*>(fPixels);
98  }
99  const uint16_t* addr16() const {
100  SkASSERT(2 == SkColorTypeBytesPerPixel(fInfo.colorType()));
101  return reinterpret_cast<const uint16_t*>(fPixels);
102  }
103  const uint32_t* addr32() const {
104  SkASSERT(4 == SkColorTypeBytesPerPixel(fInfo.colorType()));
105  return reinterpret_cast<const uint32_t*>(fPixels);
106  }
107  const uint64_t* addr64() const {
108  SkASSERT(8 == SkColorTypeBytesPerPixel(fInfo.colorType()));
109  return reinterpret_cast<const uint64_t*>(fPixels);
110  }
111  const uint16_t* addrF16() const {
112  SkASSERT(8 == SkColorTypeBytesPerPixel(fInfo.colorType()));
113  SkASSERT(kRGBA_F16_SkColorType == fInfo.colorType());
114  return reinterpret_cast<const uint16_t*>(fPixels);
115  }
116 
117  // Offset by the specified x,y coordinates
118 
119  const uint8_t* addr8(int x, int y) const {
120  SkASSERT((unsigned)x < (unsigned)fInfo.width());
121  SkASSERT((unsigned)y < (unsigned)fInfo.height());
122  return (const uint8_t*)((const char*)this->addr8() + y * fRowBytes + (x << 0));
123  }
124  const uint16_t* addr16(int x, int y) const {
125  SkASSERT((unsigned)x < (unsigned)fInfo.width());
126  SkASSERT((unsigned)y < (unsigned)fInfo.height());
127  return (const uint16_t*)((const char*)this->addr16() + y * fRowBytes + (x << 1));
128  }
129  const uint32_t* addr32(int x, int y) const {
130  SkASSERT((unsigned)x < (unsigned)fInfo.width());
131  SkASSERT((unsigned)y < (unsigned)fInfo.height());
132  return (const uint32_t*)((const char*)this->addr32() + y * fRowBytes + (x << 2));
133  }
134  const uint64_t* addr64(int x, int y) const {
135  SkASSERT((unsigned)x < (unsigned)fInfo.width());
136  SkASSERT((unsigned)y < (unsigned)fInfo.height());
137  return (const uint64_t*)((const char*)this->addr64() + y * fRowBytes + (x << 3));
138  }
139  const uint16_t* addrF16(int x, int y) const {
140  SkASSERT(kRGBA_F16_SkColorType == fInfo.colorType());
141  return reinterpret_cast<const uint16_t*>(this->addr64(x, y));
142  }
143 
144  // Writable versions
145 
146  void* writable_addr() const { return const_cast<void*>(fPixels); }
147  void* writable_addr(int x, int y) const {
148  return const_cast<void*>(this->addr(x, y));
149  }
150  uint8_t* writable_addr8(int x, int y) const {
151  return const_cast<uint8_t*>(this->addr8(x, y));
152  }
153  uint16_t* writable_addr16(int x, int y) const {
154  return const_cast<uint16_t*>(this->addr16(x, y));
155  }
156  uint32_t* writable_addr32(int x, int y) const {
157  return const_cast<uint32_t*>(this->addr32(x, y));
158  }
159  uint64_t* writable_addr64(int x, int y) const {
160  return const_cast<uint64_t*>(this->addr64(x, y));
161  }
162  uint16_t* writable_addrF16(int x, int y) const {
163  return reinterpret_cast<uint16_t*>(writable_addr64(x, y));
164  }
165 
166  // copy methods
167 
168  bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
169  int srcX, int srcY) const;
170  bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes) const {
171  return this->readPixels(dstInfo, dstPixels, dstRowBytes, 0, 0);
172  }
173  bool readPixels(const SkPixmap& dst, int srcX, int srcY) const {
174  return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), srcX, srcY);
175  }
176  bool readPixels(const SkPixmap& dst) const {
177  return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), 0, 0);
178  }
179 
187  bool scalePixels(const SkPixmap& dst, SkFilterQuality) const;
188 
193  bool erase(SkColor, const SkIRect& subset) const;
194 
195  bool erase(SkColor color) const { return this->erase(color, this->bounds()); }
196  bool erase(const SkColor4f&, const SkIRect* subset = nullptr) const;
197 
198 private:
199  const void* fPixels;
200  SkColorTable* fCTable;
201  size_t fRowBytes;
202  SkImageInfo fInfo;
203 };
204 
206 
208 
209 class SK_API SkAutoPixmapUnlock : ::SkNoncopyable {
210 public:
211  SkAutoPixmapUnlock() : fUnlockProc(NULL), fIsLocked(false) {}
212  SkAutoPixmapUnlock(const SkPixmap& pm, void (*unlock)(void*), void* ctx)
213  : fUnlockProc(unlock), fUnlockContext(ctx), fPixmap(pm), fIsLocked(true)
214  {}
215  ~SkAutoPixmapUnlock() { this->unlock(); }
216 
220  const SkPixmap& pixmap() const {
221  SkASSERT(this->isLocked());
222  return fPixmap;
223  }
224 
225  bool isLocked() const { return fIsLocked; }
226 
231  void unlock() {
232  if (fUnlockProc) {
233  SkASSERT(fIsLocked);
234  fUnlockProc(fUnlockContext);
235  fUnlockProc = NULL;
236  fIsLocked = false;
237  }
238  }
239 
244  void reset(const SkPixmap& pm, void (*unlock)(void*), void* ctx);
245 
246 private:
247  void (*fUnlockProc)(void*);
248  void* fUnlockContext;
249  SkPixmap fPixmap;
250  bool fIsLocked;
251 
252  friend class SkBitmap;
253 };
254 
255 #endif
int rowBytesAsPixels() const
Return the rowbytes expressed as a number of pixels (like width and height).
Definition: SkPixmap.h:80
void unlock()
Unlocks the pixmap.
Definition: SkPixmap.h:231
Describe an image's dimensions and pixel type.
Definition: SkImageInfo.h:181
SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by 8-bit bitmaps...
Definition: SkColorTable.h:25
Pairs SkImageInfo with actual pixels and rowbytes.
Definition: SkPixmap.h:23
Definition: SkColor.h:169
const SkPixmap & pixmap() const
Return the currently locked pixmap.
Definition: SkPixmap.h:220
SkMask is used to describe alpha bitmaps, either 1bit, 8bit, or the 3-channel 3D format.
Definition: SkMask.h:19
SkData holds an immutable data buffer.
Definition: SkData.h:22
The SkBitmap class specifies a raster bitmap.
Definition: SkBitmap.h:41
Definition: SkPixmap.h:209
uint32_t SkColor
32 bit ARGB color value, not premultiplied.
Definition: SkColor.h:28
SkIRect holds four 32 bit integer coordinates for a rectangle.
Definition: SkRect.h:20
int shiftPerPixel() const
Return the shift amount per pixel (i.e.
Definition: SkPixmap.h:86
Types and macros for colors.