• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* Generated by tools/bookmaker from include/core/SkPixmap.h and docs/SkPixmap_Reference.bmh
9    on 2018-06-08 11:48:28. Additional documentation and examples can be found at:
10    https://skia.org/user/api/SkPixmap_Reference
11 
12    You may edit either file directly. Structural changes to public interfaces require
13    editing both files. After editing docs/SkPixmap_Reference.bmh, run:
14        bookmaker -b docs -i include/core/SkPixmap.h -p
15    to create an updated version of this file.
16  */
17 
18 #ifndef SkPixmap_DEFINED
19 #define SkPixmap_DEFINED
20 
21 #include "SkColor.h"
22 #include "SkFilterQuality.h"
23 #include "SkImageInfo.h"
24 
25 class SkData;
26 struct SkMask;
27 
28 /** \class SkPixmap
29     SkPixmap provides a utility to pair SkImageInfo with pixels and row bytes.
30     SkPixmap is a low level class which provides convenience functions to access
31     raster destinations. SkCanvas can not draw SkPixmap, nor does SkPixmap provide
32     a direct drawing destination.
33 
34     Use SkBitmap to draw pixels referenced by SkPixmap; use SkSurface to draw into
35     pixels referenced by SkPixmap.
36 
37     SkPixmap does not try to manage the lifetime of the pixel memory. Use SkPixelRef
38     to manage pixel memory; SkPixelRef is safe across threads.
39 */
40 class SK_API SkPixmap {
41 public:
42 
43     /** Creates an empty SkPixmap without pixels, with kUnknown_SkColorType, with
44         kUnknown_SkAlphaType, and with a width and height of zero. Use
45         reset() to associate pixels, SkColorType, SkAlphaType, width, and height
46         after SkPixmap has been created.
47 
48         @return  empty SkPixmap
49     */
SkPixmap()50     SkPixmap()
51         : fPixels(nullptr), fRowBytes(0), fInfo(SkImageInfo::MakeUnknown(0, 0))
52     {}
53 
54     /** Creates SkPixmap from info width, height, SkAlphaType, and SkColorType.
55         addr points to pixels, or nullptr. rowBytes should be info.width() times
56         info.bytesPerPixel(), or larger.
57 
58         No parameter checking is performed; it is up to the caller to ensure that
59         addr and rowBytes agree with info.
60 
61         The memory lifetime of pixels is managed by the caller. When SkPixmap goes
62         out of scope, addr is unaffected.
63 
64         SkPixmap may be later modified by reset() to change its size, pixel type, or
65         storage.
66 
67         @param info      width, height, SkAlphaType, SkColorType of SkImageInfo
68         @param addr      pointer to pixels allocated by caller; may be nullptr
69         @param rowBytes  size of one row of addr; width times pixel size, or larger
70         @return          initialized SkPixmap
71     */
SkPixmap(const SkImageInfo & info,const void * addr,size_t rowBytes)72     SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes)
73         : fPixels(addr), fRowBytes(rowBytes), fInfo(info)
74     {}
75 
76     /** Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to
77         kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType.
78 
79         The prior pixels are unaffected; it is up to the caller to release pixels
80         memory if desired.
81     */
82     void reset();
83 
84     /** Sets width, height, SkAlphaType, and SkColorType from info.
85         Sets pixel address from addr, which may be nullptr.
86         Sets row bytes from rowBytes, which should be info.width() times
87         info.bytesPerPixel(), or larger.
88 
89         Does not check addr. Asserts if built with SK_DEBUG defined and if rowBytes is
90         too small to hold one row of pixels.
91 
92         The memory lifetime pixels are managed by the caller. When SkPixmap goes
93         out of scope, addr is unaffected.
94 
95         @param info      width, height, SkAlphaType, SkColorType of SkImageInfo
96         @param addr      pointer to pixels allocated by caller; may be nullptr
97         @param rowBytes  size of one row of addr; width times pixel size, or larger
98     */
99     void reset(const SkImageInfo& info, const void* addr, size_t rowBytes);
100 
101     /** Changes SkColorSpace in SkImageInfo; preserves width, height, SkAlphaType, and
102         SkColorType in SkImage, and leaves pixel address and row bytes unchanged.
103         SkColorSpace reference count is incremented.
104 
105         @param colorSpace  SkColorSpace moved to SkImageInfo
106     */
107     void setColorSpace(sk_sp<SkColorSpace> colorSpace);
108 
109     /** Deprecated.
110     */
111     bool SK_WARN_UNUSED_RESULT reset(const SkMask& mask);
112 
113     /** Sets subset width, height, pixel address to intersection of SkPixmap with area,
114         if intersection is not empty; and return true. Otherwise, leave subset unchanged
115         and return false.
116 
117         Failing to read the return value generates a compile time warning.
118 
119         @param subset  storage for width, height, pixel address of intersection
120         @param area    bounds to intersect with SkPixmap
121         @return        true if intersection of SkPixmap and area is not empty
122     */
123     bool SK_WARN_UNUSED_RESULT extractSubset(SkPixmap* subset, const SkIRect& area) const;
124 
125     /** Returns width, height, SkAlphaType, SkColorType, and SkColorSpace.
126 
127         @return  reference to SkImageInfo
128     */
info()129     const SkImageInfo& info() const { return fInfo; }
130 
131     /** Returns row bytes, the interval from one pixel row to the next. Row bytes
132         is at least as large as: width() * info().bytesPerPixel().
133 
134         Returns zero if colorType() is kUnknown_SkColorType.
135         It is up to the SkBitmap creator to ensure that row bytes is a useful value.
136 
137         @return  byte length of pixel row
138     */
rowBytes()139     size_t rowBytes() const { return fRowBytes; }
140 
141     /** Returns pixel address, the base address corresponding to the pixel origin.
142 
143         It is up to the SkPixmap creator to ensure that pixel address is a useful value.
144 
145         @return  pixel address
146     */
addr()147     const void* addr() const { return fPixels; }
148 
149     /** Returns pixel count in each pixel row. Should be equal or less than:
150         rowBytes() / info().bytesPerPixel().
151 
152         @return  pixel width in SkImageInfo
153     */
width()154     int width() const { return fInfo.width(); }
155 
156     /** Returns pixel row count.
157 
158         @return  pixel height in SkImageInfo
159     */
height()160     int height() const { return fInfo.height(); }
161 
162     /** Returns SkColorType, one of:
163         kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
164         kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType,
165         kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType,
166         kGray_8_SkColorType, kRGBA_F16_SkColorType.
167 
168         @return  SkColorType in SkImageInfo
169     */
colorType()170     SkColorType colorType() const { return fInfo.colorType(); }
171 
172     /** Returns SkAlphaType, one of:
173         kUnknown_SkAlphaType, kOpaque_SkAlphaType, kPremul_SkAlphaType,
174         kUnpremul_SkAlphaType.
175 
176         @return  SkAlphaType in SkImageInfo
177     */
alphaType()178     SkAlphaType alphaType() const { return fInfo.alphaType(); }
179 
180     /** Returns SkColorSpace, the range of colors, associated with SkImageInfo. The
181         reference count of SkColorSpace is unchanged. The returned SkColorSpace is
182         immutable.
183 
184         @return  SkColorSpace in SkImageInfo, or nullptr
185     */
colorSpace()186     SkColorSpace* colorSpace() const { return fInfo.colorSpace(); }
187 
188     /** Returns true if SkAlphaType is kOpaque_SkAlphaType.
189         Does not check if SkColorType allows alpha, or if any pixel value has
190         transparency.
191 
192         @return  true if SkImageInfo has opaque SkAlphaType
193     */
isOpaque()194     bool isOpaque() const { return fInfo.isOpaque(); }
195 
196     /** Returns SkIRect { 0, 0, width(), height() }.
197 
198         @return  integral rectangle from origin to width() and height()
199     */
bounds()200     SkIRect bounds() const { return SkIRect::MakeWH(this->width(), this->height()); }
201 
202     /** Returns number of pixels that fit on row. Should be greater than or equal to
203         width().
204 
205         @return  maximum pixels per row
206     */
rowBytesAsPixels()207     int rowBytesAsPixels() const { return int(fRowBytes >> this->shiftPerPixel()); }
208 
209     /** Returns bit shift converting row bytes to row pixels.
210         Returns zero for kUnknown_SkColorType.
211 
212         @return  one of: 0, 1, 2, 3; left shift to convert pixels to bytes
213     */
shiftPerPixel()214     int shiftPerPixel() const { return fInfo.shiftPerPixel(); }
215 
216     /** Returns minimum memory required for pixel storage.
217         Does not include unused memory on last row when rowBytesAsPixels() exceeds width().
218         Returns zero if result does not fit in size_t.
219         Returns zero if height() or width() is 0.
220         Returns height() times rowBytes() if colorType() is kUnknown_SkColorType.
221 
222         @return  size in bytes of image buffer
223     */
computeByteSize()224     size_t computeByteSize() const { return fInfo.computeByteSize(fRowBytes); }
225 
226     /** Returns true if all pixels are opaque. SkColorType determines how pixels
227         are encoded, and whether pixel describes alpha. Returns true for SkColorType
228         without alpha in each pixel; for other SkColorType, returns true if all
229         pixels have alpha values equivalent to 1.0 or greater.
230 
231         For SkColorType kRGB_565_SkColorType or kGray_8_SkColorType: always
232         returns true. For SkColorType kAlpha_8_SkColorType, kBGRA_8888_SkColorType,
233         kRGBA_8888_SkColorType: returns true if all pixel alpha values are 255.
234         For SkColorType kARGB_4444_SkColorType: returns true if all pixel alpha values are 15.
235         For kRGBA_F16_SkColorType: returns true if all pixel alpha values are 1.0 or
236         greater.
237 
238         Returns false for kUnknown_SkColorType.
239 
240         @return  true if all pixels have opaque values or SkColorType is opaque
241     */
242     bool computeIsOpaque() const;
243 
244     /** Returns pixel at (x, y) as unpremultiplied color.
245         Returns black with alpha if SkColorType is kAlpha_8_SkColorType.
246 
247         Input is not validated: out of bounds values of x or y trigger an assert() if
248         built with SK_DEBUG defined; and returns undefined values or may crash if
249         SK_RELEASE is defined. Fails if SkColorType is kUnknown_SkColorType or
250         pixel address is nullptr.
251 
252         SkColorSpace in SkImageInfo is ignored. Some color precision may be lost in the
253         conversion to unpremultiplied color; original pixel data may have additional
254         precision.
255 
256         @param x  column index, zero or greater, and less than width()
257         @param y  row index, zero or greater, and less than height()
258         @return   pixel converted to unpremultiplied color
259     */
260     SkColor getColor(int x, int y) const;
261 
262     /** Look up the pixel at (x,y) and return its alpha component, normalized to [0..1].
263         This is roughly equivalent to SkGetColorA(getColor()), but can be more efficent
264         (and more precise if the pixels store more than 8 bits per component).
265 
266         @param x  column index, zero or greater, and less than width()
267         @param y  row index, zero or greater, and less than height()
268         @return   alpha converted to normalized float
269      */
270     float getAlphaf(int x, int y) const;
271 
272     /** Returns readable pixel address at (x, y). Returns nullptr if SkPixelRef is nullptr.
273 
274         Input is not validated: out of bounds values of x or y trigger an assert() if
275         built with SK_DEBUG defined. Returns nullptr if SkColorType is kUnknown_SkColorType.
276 
277         Performs a lookup of pixel size; for better performance, call
278         one of: addr8, addr16, addr32, addr64, or addrF16().
279 
280         @param x  column index, zero or greater, and less than width()
281         @param y  row index, zero or greater, and less than height()
282         @return   readable generic pointer to pixel
283     */
addr(int x,int y)284     const void* addr(int x, int y) const {
285         return (const char*)fPixels + fInfo.computeOffset(x, y, fRowBytes);
286     }
287 
288     /** Returns readable base pixel address. Result is addressable as unsigned 8-bit bytes.
289         Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType or
290         kGray_8_SkColorType, and is built with SK_DEBUG defined.
291 
292         One byte corresponds to one pixel.
293 
294         @return  readable unsigned 8-bit pointer to pixels
295     */
addr8()296     const uint8_t* addr8() const {
297         SkASSERT(1 == fInfo.bytesPerPixel());
298         return reinterpret_cast<const uint8_t*>(fPixels);
299     }
300 
301     /** Returns readable base pixel address. Result is addressable as unsigned 16-bit words.
302         Will trigger an assert() if SkColorType is not kRGB_565_SkColorType or
303         kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
304 
305         One word corresponds to one pixel.
306 
307         @return  readable unsigned 16-bit pointer to pixels
308     */
addr16()309     const uint16_t* addr16() const {
310         SkASSERT(2 == fInfo.bytesPerPixel());
311         return reinterpret_cast<const uint16_t*>(fPixels);
312     }
313 
314     /** Returns readable base pixel address. Result is addressable as unsigned 32-bit words.
315         Will trigger an assert() if SkColorType is not kRGBA_8888_SkColorType or
316         kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.
317 
318         One word corresponds to one pixel.
319 
320         @return  readable unsigned 32-bit pointer to pixels
321     */
addr32()322     const uint32_t* addr32() const {
323         SkASSERT(4 == fInfo.bytesPerPixel());
324         return reinterpret_cast<const uint32_t*>(fPixels);
325     }
326 
327     /** Returns readable base pixel address. Result is addressable as unsigned 64-bit words.
328         Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built
329         with SK_DEBUG defined.
330 
331         One word corresponds to one pixel.
332 
333         @return  readable unsigned 64-bit pointer to pixels
334     */
addr64()335     const uint64_t* addr64() const {
336         SkASSERT(8 == fInfo.bytesPerPixel());
337         return reinterpret_cast<const uint64_t*>(fPixels);
338     }
339 
340     /** Returns readable base pixel address. Result is addressable as unsigned 16-bit words.
341         Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built
342         with SK_DEBUG defined.
343 
344         Each word represents one color component encoded as a half float.
345         Four words correspond to one pixel.
346 
347         @return  readable unsigned 16-bit pointer to first component of pixels
348     */
addrF16()349     const uint16_t* addrF16() const {
350         SkASSERT(8 == fInfo.bytesPerPixel());
351         SkASSERT(kRGBA_F16_SkColorType == fInfo.colorType());
352         return reinterpret_cast<const uint16_t*>(fPixels);
353     }
354 
355     /** Returns readable pixel address at (x, y).
356 
357         Input is not validated: out of bounds values of x or y trigger an assert() if
358         built with SK_DEBUG defined.
359 
360         Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType or
361         kGray_8_SkColorType, and is built with SK_DEBUG defined.
362 
363         @param x  column index, zero or greater, and less than width()
364         @param y  row index, zero or greater, and less than height()
365         @return   readable unsigned 8-bit pointer to pixel at (x, y)
366     */
addr8(int x,int y)367     const uint8_t* addr8(int x, int y) const {
368         SkASSERT((unsigned)x < (unsigned)fInfo.width());
369         SkASSERT((unsigned)y < (unsigned)fInfo.height());
370         return (const uint8_t*)((const char*)this->addr8() + y * fRowBytes + (x << 0));
371     }
372 
373     /** Returns readable pixel address at (x, y).
374 
375         Input is not validated: out of bounds values of x or y trigger an assert() if
376         built with SK_DEBUG defined.
377 
378         Will trigger an assert() if SkColorType is not kRGB_565_SkColorType or
379         kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
380 
381         @param x  column index, zero or greater, and less than width()
382         @param y  row index, zero or greater, and less than height()
383         @return   readable unsigned 16-bit pointer to pixel at (x, y)
384     */
addr16(int x,int y)385     const uint16_t* addr16(int x, int y) const {
386         SkASSERT((unsigned)x < (unsigned)fInfo.width());
387         SkASSERT((unsigned)y < (unsigned)fInfo.height());
388         return (const uint16_t*)((const char*)this->addr16() + y * fRowBytes + (x << 1));
389     }
390 
391     /** Returns readable pixel address at (x, y).
392 
393         Input is not validated: out of bounds values of x or y trigger an assert() if
394         built with SK_DEBUG defined.
395 
396         Will trigger an assert() if SkColorType is not kRGBA_8888_SkColorType or
397         kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.
398 
399         @param x  column index, zero or greater, and less than width()
400         @param y  row index, zero or greater, and less than height()
401         @return   readable unsigned 32-bit pointer to pixel at (x, y)
402     */
addr32(int x,int y)403     const uint32_t* addr32(int x, int y) const {
404         SkASSERT((unsigned)x < (unsigned)fInfo.width());
405         SkASSERT((unsigned)y < (unsigned)fInfo.height());
406         return (const uint32_t*)((const char*)this->addr32() + y * fRowBytes + (x << 2));
407     }
408 
409     /** Returns readable pixel address at (x, y).
410 
411         Input is not validated: out of bounds values of x or y trigger an assert() if
412         built with SK_DEBUG defined.
413 
414         Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built
415         with SK_DEBUG defined.
416 
417         @param x  column index, zero or greater, and less than width()
418         @param y  row index, zero or greater, and less than height()
419         @return   readable unsigned 64-bit pointer to pixel at (x, y)
420     */
addr64(int x,int y)421     const uint64_t* addr64(int x, int y) const {
422         SkASSERT((unsigned)x < (unsigned)fInfo.width());
423         SkASSERT((unsigned)y < (unsigned)fInfo.height());
424         return (const uint64_t*)((const char*)this->addr64() + y * fRowBytes + (x << 3));
425     }
426 
427     /** Returns readable pixel address at (x, y).
428 
429         Input is not validated: out of bounds values of x or y trigger an assert() if
430         built with SK_DEBUG defined.
431 
432         Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built
433         with SK_DEBUG defined.
434 
435         Each unsigned 16-bit word represents one color component encoded as a half float.
436         Four words correspond to one pixel.
437 
438         @param x  column index, zero or greater, and less than width()
439         @param y  row index, zero or greater, and less than height()
440         @return   readable unsigned 16-bit pointer to pixel component at (x, y)
441     */
addrF16(int x,int y)442     const uint16_t* addrF16(int x, int y) const {
443         SkASSERT(kRGBA_F16_SkColorType == fInfo.colorType());
444         return reinterpret_cast<const uint16_t*>(this->addr64(x, y));
445     }
446 
447     /** Returns writable base pixel address.
448 
449         @return  writable generic base pointer to pixels
450     */
writable_addr()451     void* writable_addr() const { return const_cast<void*>(fPixels); }
452 
453     /** Returns writable pixel address at (x, y).
454 
455         Input is not validated: out of bounds values of x or y trigger an assert() if
456         built with SK_DEBUG defined. Returns zero if SkColorType is kUnknown_SkColorType.
457 
458         @param x  column index, zero or greater, and less than width()
459         @param y  row index, zero or greater, and less than height()
460         @return   writable generic pointer to pixel
461     */
writable_addr(int x,int y)462     void* writable_addr(int x, int y) const {
463         return const_cast<void*>(this->addr(x, y));
464     }
465 
466     /** Returns writable pixel address at (x, y). Result is addressable as unsigned
467         8-bit bytes. Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType
468         or kGray_8_SkColorType, and is built with SK_DEBUG defined.
469 
470         One byte corresponds to one pixel.
471 
472         @param x  column index, zero or greater, and less than width()
473         @param y  row index, zero or greater, and less than height()
474         @return   writable unsigned 8-bit pointer to pixels
475     */
writable_addr8(int x,int y)476     uint8_t* writable_addr8(int x, int y) const {
477         return const_cast<uint8_t*>(this->addr8(x, y));
478     }
479 
480     /** Returns writable_addr pixel address at (x, y). Result is addressable as unsigned
481         16-bit words. Will trigger an assert() if SkColorType is not kRGB_565_SkColorType
482         or kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
483 
484         One word corresponds to one pixel.
485 
486         @param x  column index, zero or greater, and less than width()
487         @param y  row index, zero or greater, and less than height()
488         @return   writable unsigned 16-bit pointer to pixel
489     */
writable_addr16(int x,int y)490     uint16_t* writable_addr16(int x, int y) const {
491         return const_cast<uint16_t*>(this->addr16(x, y));
492     }
493 
494     /** Returns writable pixel address at (x, y). Result is addressable as unsigned
495         32-bit words. Will trigger an assert() if SkColorType is not
496         kRGBA_8888_SkColorType or kBGRA_8888_SkColorType, and is built with SK_DEBUG
497         defined.
498 
499         One word corresponds to one pixel.
500 
501         @param x  column index, zero or greater, and less than width()
502         @param y  row index, zero or greater, and less than height()
503         @return   writable unsigned 32-bit pointer to pixel
504     */
writable_addr32(int x,int y)505     uint32_t* writable_addr32(int x, int y) const {
506         return const_cast<uint32_t*>(this->addr32(x, y));
507     }
508 
509     /** Returns writable pixel address at (x, y). Result is addressable as unsigned
510         64-bit words. Will trigger an assert() if SkColorType is not
511         kRGBA_F16_SkColorType and is built with SK_DEBUG defined.
512 
513         One word corresponds to one pixel.
514 
515         @param x  column index, zero or greater, and less than width()
516         @param y  row index, zero or greater, and less than height()
517         @return   writable unsigned 64-bit pointer to pixel
518     */
writable_addr64(int x,int y)519     uint64_t* writable_addr64(int x, int y) const {
520         return const_cast<uint64_t*>(this->addr64(x, y));
521     }
522 
523     /** Returns writable pixel address at (x, y). Result is addressable as unsigned
524         16-bit words. Will trigger an assert() if SkColorType is not
525         kRGBA_F16_SkColorType and is built with SK_DEBUG defined.
526 
527         Each word represents one color component encoded as a half float.
528         Four words correspond to one pixel.
529 
530         @param x  column index, zero or greater, and less than width()
531         @param y  row index, zero or greater, and less than height()
532         @return   writable unsigned 16-bit pointer to first component of pixel
533     */
writable_addrF16(int x,int y)534     uint16_t* writable_addrF16(int x, int y) const {
535         return reinterpret_cast<uint16_t*>(writable_addr64(x, y));
536     }
537 
538     /** Copies a SkRect of pixels to dstPixels. Copy starts at (0, 0), and does not
539         exceed SkPixmap (width(), height()).
540 
541         dstInfo specifies width, height, SkColorType, SkAlphaType, and
542         SkColorSpace of destination. dstRowBytes specifics the gap from one destination
543         row to the next. Returns true if pixels are copied. Returns false if
544         dstInfo address equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes().
545 
546         Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is
547         kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match.
548         If SkPixmap colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match.
549         If SkPixmap alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must
550         match. If SkPixmap colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns
551         false if pixel conversion is not possible.
552 
553         Returns false if SkPixmap width() or height() is zero or negative.
554 
555         @param dstInfo      destination width, height, SkColorType, SkAlphaType, SkColorSpace
556         @param dstPixels    destination pixel storage
557         @param dstRowBytes  destination row length
558         @return             true if pixels are copied to dstPixels
559     */
readPixels(const SkImageInfo & dstInfo,void * dstPixels,size_t dstRowBytes)560     bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes) const {
561         return this->readPixels(dstInfo, dstPixels, dstRowBytes, 0, 0);
562     }
563 
564     /** Copies a SkRect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not
565         exceed SkPixmap (width(), height()).
566 
567         dstInfo specifies width, height, SkColorType, SkAlphaType, and
568         SkColorSpace of destination. dstRowBytes specifics the gap from one destination
569         row to the next. Returns true if pixels are copied. Returns false if
570         dstInfo address equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes().
571 
572         Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is
573         kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match.
574         If SkPixmap colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match.
575         If SkPixmap alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must
576         match. If SkPixmap colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns
577         false if pixel conversion is not possible.
578 
579         srcX and srcY may be negative to copy only top or left of source. Returns
580         false if SkPixmap width() or height() is zero or negative. Returns false if:
581         abs(srcX) >= Pixmap width(), or if abs(srcY) >= Pixmap height().
582 
583         @param dstInfo      destination width, height, SkColorType, SkAlphaType, SkColorSpace
584         @param dstPixels    destination pixel storage
585         @param dstRowBytes  destination row length
586         @param srcX         column index whose absolute value is less than width()
587         @param srcY         row index whose absolute value is less than height()
588         @return             true if pixels are copied to dstPixels
589     */
590     bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, int srcX,
591                     int srcY) const;
592 
593     /** Copies a SkRect of pixels to dst. Copy starts at (srcX, srcY), and does not
594         exceed SkPixmap (width(), height()). dst specifies width, height, SkColorType,
595         SkAlphaType, and SkColorSpace of destination.  Returns true if pixels are copied.
596         Returns false if dst address equals nullptr, or dst.rowBytes() is less than
597         dst SkImageInfo::minRowBytes.
598 
599         Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is
600         kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.info().colorType must match.
601         If SkPixmap colorType() is kGray_8_SkColorType, dst.info().colorSpace must match.
602         If SkPixmap alphaType() is kOpaque_SkAlphaType, dst.info().alphaType must
603         match. If SkPixmap colorSpace() is nullptr, dst.info().colorSpace must match. Returns
604         false if pixel conversion is not possible.
605 
606         srcX and srcY may be negative to copy only top or left of source. Returns
607         false SkPixmap width() or height() is zero or negative. Returns false if:
608         abs(srcX) >= Pixmap width(), or if abs(srcY) >= Pixmap height().
609 
610         @param dst   SkImageInfo and pixel address to write to
611         @param srcX  column index whose absolute value is less than width()
612         @param srcY  row index whose absolute value is less than height()
613         @return      true if pixels are copied to dst
614     */
readPixels(const SkPixmap & dst,int srcX,int srcY)615     bool readPixels(const SkPixmap& dst, int srcX, int srcY) const {
616         return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), srcX, srcY);
617     }
618 
619     /** Copies pixels inside bounds() to dst. dst specifies width, height, SkColorType,
620         SkAlphaType, and SkColorSpace of destination.  Returns true if pixels are copied.
621         Returns false if dst address equals nullptr, or dst.rowBytes() is less than
622         dst SkImageInfo::minRowBytes.
623 
624         Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is
625         kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match.
626         If SkPixmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match.
627         If SkPixmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must
628         match. If SkPixmap colorSpace() is nullptr, dst SkColorSpace must match. Returns
629         false if pixel conversion is not possible.
630 
631         Returns false if SkPixmap width() or height() is zero or negative.
632 
633         @param dst  SkImageInfo and pixel address to write to
634         @return     true if pixels are copied to dst
635     */
readPixels(const SkPixmap & dst)636     bool readPixels(const SkPixmap& dst) const {
637         return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), 0, 0);
638     }
639 
640     /** Copies SkBitmap to dst, scaling pixels to fit dst.width() and dst.height(), and
641         converting pixels to match dst.colorType() and dst.alphaType(). Returns true if
642         pixels are copied. Returns false if dst address is nullptr, or dst.rowBytes() is
643         less than dst SkImageInfo::minRowBytes.
644 
645         Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is
646         kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match.
647         If SkPixmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match.
648         If SkPixmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must
649         match. If SkPixmap colorSpace() is nullptr, dst SkColorSpace must match. Returns
650         false if pixel conversion is not possible.
651 
652         Returns false if SkBitmap width() or height() is zero or negative.
653 
654         Scales the image, with filterQuality, to match dst.width() and dst.height().
655         filterQuality kNone_SkFilterQuality is fastest, typically implemented with
656         nearest neighbor filter. kLow_SkFilterQuality is typically implemented with
657         bilerp filter. kMedium_SkFilterQuality is typically implemented with
658         bilerp filter, and mip-map filter when size is reduced.
659         kHigh_SkFilterQuality is slowest, typically implemented with bicubic filter.
660 
661         @param dst            SkImageInfo and pixel address to write to
662         @param filterQuality  one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
663                               kMedium_SkFilterQuality, kHigh_SkFilterQuality
664         @return               true if pixels are scaled to fit dst
665     */
666     bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality) const;
667 
668     /** Writes color to pixels bounded by subset; returns true on success.
669         Returns false if colorType() is kUnknown_SkColorType, or if subset does
670         not intersect bounds().
671 
672         @param color   unpremultiplied color to write
673         @param subset  bounding integer SkRect of written pixels
674         @return        true if pixels are changed
675     */
676     bool erase(SkColor color, const SkIRect& subset) const;
677 
678     /** Writes color to pixels inside bounds(); returns true on success.
679         Returns false if colorType() is kUnknown_SkColorType, or if bounds()
680         is empty.
681 
682         @param color  unpremultiplied color to write
683         @return       true if pixels are changed
684     */
erase(SkColor color)685     bool erase(SkColor color) const { return this->erase(color, this->bounds()); }
686 
687     /** Writes color to pixels bounded by subset; returns true on success.
688         if subset is nullptr, writes colors pixels inside bounds(). Returns false if
689         colorType() is kUnknown_SkColorType, if subset is not nullptr and does
690         not intersect bounds(), or if subset is nullptr and bounds() is empty.
691 
692         @param color   unpremultiplied color to write
693         @param subset  bounding integer SkRect of pixels to write; may be nullptr
694         @return        true if pixels are changed
695     */
696     bool erase(const SkColor4f& color, const SkIRect* subset = nullptr) const;
697 
698 private:
699     const void*     fPixels;
700     size_t          fRowBytes;
701     SkImageInfo     fInfo;
702 
703     friend class SkPixmapPriv;
704 };
705 
706 #endif
707