• 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 smart pointer to SkColorSpace, the range of colors, associated with
189         SkImageInfo. The smart pointer tracks the number of objects sharing this
190         SkColorSpace reference so the memory is released when the owners destruct.
191 
192         The returned SkColorSpace is immutable.
193 
194         @return  SkColorSpace in SkImageInfo wrapped in a smart pointer
195     */
refColorSpace()196     sk_sp<SkColorSpace> refColorSpace() const { return fInfo.refColorSpace(); }
197 
198     /** Returns true if SkAlphaType is kOpaque_SkAlphaType.
199         Does not check if SkColorType allows alpha, or if any pixel value has
200         transparency.
201 
202         @return  true if SkImageInfo has opaque SkAlphaType
203     */
isOpaque()204     bool isOpaque() const { return fInfo.isOpaque(); }
205 
206     /** Returns SkIRect { 0, 0, width(), height() }.
207 
208         @return  integral rectangle from origin to width() and height()
209     */
bounds()210     SkIRect bounds() const { return SkIRect::MakeWH(this->width(), this->height()); }
211 
212     /** Returns number of pixels that fit on row. Should be greater than or equal to
213         width().
214 
215         @return  maximum pixels per row
216     */
rowBytesAsPixels()217     int rowBytesAsPixels() const { return int(fRowBytes >> this->shiftPerPixel()); }
218 
219     /** Returns bit shift converting row bytes to row pixels.
220         Returns zero for kUnknown_SkColorType.
221 
222         @return  one of: 0, 1, 2, 3; left shift to convert pixels to bytes
223     */
shiftPerPixel()224     int shiftPerPixel() const { return fInfo.shiftPerPixel(); }
225 
226     /** Returns minimum memory required for pixel storage.
227         Does not include unused memory on last row when rowBytesAsPixels() exceeds width().
228         Returns zero if result does not fit in size_t.
229         Returns zero if height() or width() is 0.
230         Returns height() times rowBytes() if colorType() is kUnknown_SkColorType.
231 
232         @return  size in bytes of image buffer
233     */
computeByteSize()234     size_t computeByteSize() const { return fInfo.computeByteSize(fRowBytes); }
235 
236     /** Returns true if all pixels are opaque. SkColorType determines how pixels
237         are encoded, and whether pixel describes alpha. Returns true for SkColorType
238         without alpha in each pixel; for other SkColorType, returns true if all
239         pixels have alpha values equivalent to 1.0 or greater.
240 
241         For SkColorType kRGB_565_SkColorType or kGray_8_SkColorType: always
242         returns true. For SkColorType kAlpha_8_SkColorType, kBGRA_8888_SkColorType,
243         kRGBA_8888_SkColorType: returns true if all pixel alpha values are 255.
244         For SkColorType kARGB_4444_SkColorType: returns true if all pixel alpha values are 15.
245         For kRGBA_F16_SkColorType: returns true if all pixel alpha values are 1.0 or
246         greater.
247 
248         Returns false for kUnknown_SkColorType.
249 
250         @return  true if all pixels have opaque values or SkColorType is opaque
251     */
252     bool computeIsOpaque() const;
253 
254     /** Returns pixel at (x, y) as unpremultiplied color.
255         Returns black with alpha if SkColorType is kAlpha_8_SkColorType.
256 
257         Input is not validated: out of bounds values of x or y trigger an assert() if
258         built with SK_DEBUG defined; and returns undefined values or may crash if
259         SK_RELEASE is defined. Fails if SkColorType is kUnknown_SkColorType or
260         pixel address is nullptr.
261 
262         SkColorSpace in SkImageInfo is ignored. Some color precision may be lost in the
263         conversion to unpremultiplied color; original pixel data may have additional
264         precision.
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   pixel converted to unpremultiplied color
269     */
270     SkColor getColor(int x, int y) const;
271 
272     /** Look up the pixel at (x,y) and return its alpha component, normalized to [0..1].
273         This is roughly equivalent to SkGetColorA(getColor()), but can be more efficent
274         (and more precise if the pixels store more than 8 bits per component).
275 
276         @param x  column index, zero or greater, and less than width()
277         @param y  row index, zero or greater, and less than height()
278         @return   alpha converted to normalized float
279      */
280     float getAlphaf(int x, int y) const;
281 
282     /** Returns readable pixel address at (x, y). Returns nullptr if SkPixelRef is nullptr.
283 
284         Input is not validated: out of bounds values of x or y trigger an assert() if
285         built with SK_DEBUG defined. Returns nullptr if SkColorType is kUnknown_SkColorType.
286 
287         Performs a lookup of pixel size; for better performance, call
288         one of: addr8, addr16, addr32, addr64, or addrF16().
289 
290         @param x  column index, zero or greater, and less than width()
291         @param y  row index, zero or greater, and less than height()
292         @return   readable generic pointer to pixel
293     */
addr(int x,int y)294     const void* addr(int x, int y) const {
295         return (const char*)fPixels + fInfo.computeOffset(x, y, fRowBytes);
296     }
297 
298     /** Returns readable base pixel address. Result is addressable as unsigned 8-bit bytes.
299         Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType or
300         kGray_8_SkColorType, and is built with SK_DEBUG defined.
301 
302         One byte corresponds to one pixel.
303 
304         @return  readable unsigned 8-bit pointer to pixels
305     */
addr8()306     const uint8_t* addr8() const {
307         SkASSERT(1 == fInfo.bytesPerPixel());
308         return reinterpret_cast<const uint8_t*>(fPixels);
309     }
310 
311     /** Returns readable base pixel address. Result is addressable as unsigned 16-bit words.
312         Will trigger an assert() if SkColorType is not kRGB_565_SkColorType or
313         kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
314 
315         One word corresponds to one pixel.
316 
317         @return  readable unsigned 16-bit pointer to pixels
318     */
addr16()319     const uint16_t* addr16() const {
320         SkASSERT(2 == fInfo.bytesPerPixel());
321         return reinterpret_cast<const uint16_t*>(fPixels);
322     }
323 
324     /** Returns readable base pixel address. Result is addressable as unsigned 32-bit words.
325         Will trigger an assert() if SkColorType is not kRGBA_8888_SkColorType or
326         kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.
327 
328         One word corresponds to one pixel.
329 
330         @return  readable unsigned 32-bit pointer to pixels
331     */
addr32()332     const uint32_t* addr32() const {
333         SkASSERT(4 == fInfo.bytesPerPixel());
334         return reinterpret_cast<const uint32_t*>(fPixels);
335     }
336 
337     /** Returns readable base pixel address. Result is addressable as unsigned 64-bit words.
338         Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built
339         with SK_DEBUG defined.
340 
341         One word corresponds to one pixel.
342 
343         @return  readable unsigned 64-bit pointer to pixels
344     */
addr64()345     const uint64_t* addr64() const {
346         SkASSERT(8 == fInfo.bytesPerPixel());
347         return reinterpret_cast<const uint64_t*>(fPixels);
348     }
349 
350     /** Returns readable base pixel address. Result is addressable as unsigned 16-bit words.
351         Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built
352         with SK_DEBUG defined.
353 
354         Each word represents one color component encoded as a half float.
355         Four words correspond to one pixel.
356 
357         @return  readable unsigned 16-bit pointer to first component of pixels
358     */
addrF16()359     const uint16_t* addrF16() const {
360         SkASSERT(8 == fInfo.bytesPerPixel());
361         SkASSERT(kRGBA_F16_SkColorType     == fInfo.colorType() ||
362                  kRGBA_F16Norm_SkColorType == fInfo.colorType());
363         return reinterpret_cast<const uint16_t*>(fPixels);
364     }
365 
366     /** Returns readable pixel address at (x, y).
367 
368         Input is not validated: out of bounds values of x or y trigger an assert() if
369         built with SK_DEBUG defined.
370 
371         Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType or
372         kGray_8_SkColorType, and is built with SK_DEBUG defined.
373 
374         @param x  column index, zero or greater, and less than width()
375         @param y  row index, zero or greater, and less than height()
376         @return   readable unsigned 8-bit pointer to pixel at (x, y)
377     */
addr8(int x,int y)378     const uint8_t* addr8(int x, int y) const {
379         SkASSERT((unsigned)x < (unsigned)fInfo.width());
380         SkASSERT((unsigned)y < (unsigned)fInfo.height());
381         return (const uint8_t*)((const char*)this->addr8() + y * fRowBytes + (x << 0));
382     }
383 
384     /** Returns readable pixel address at (x, y).
385 
386         Input is not validated: out of bounds values of x or y trigger an assert() if
387         built with SK_DEBUG defined.
388 
389         Will trigger an assert() if SkColorType is not kRGB_565_SkColorType or
390         kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
391 
392         @param x  column index, zero or greater, and less than width()
393         @param y  row index, zero or greater, and less than height()
394         @return   readable unsigned 16-bit pointer to pixel at (x, y)
395     */
addr16(int x,int y)396     const uint16_t* addr16(int x, int y) const {
397         SkASSERT((unsigned)x < (unsigned)fInfo.width());
398         SkASSERT((unsigned)y < (unsigned)fInfo.height());
399         return (const uint16_t*)((const char*)this->addr16() + y * fRowBytes + (x << 1));
400     }
401 
402     /** Returns readable pixel address at (x, y).
403 
404         Input is not validated: out of bounds values of x or y trigger an assert() if
405         built with SK_DEBUG defined.
406 
407         Will trigger an assert() if SkColorType is not kRGBA_8888_SkColorType or
408         kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.
409 
410         @param x  column index, zero or greater, and less than width()
411         @param y  row index, zero or greater, and less than height()
412         @return   readable unsigned 32-bit pointer to pixel at (x, y)
413     */
addr32(int x,int y)414     const uint32_t* addr32(int x, int y) const {
415         SkASSERT((unsigned)x < (unsigned)fInfo.width());
416         SkASSERT((unsigned)y < (unsigned)fInfo.height());
417         return (const uint32_t*)((const char*)this->addr32() + y * fRowBytes + (x << 2));
418     }
419 
420     /** Returns readable pixel address at (x, y).
421 
422         Input is not validated: out of bounds values of x or y trigger an assert() if
423         built with SK_DEBUG defined.
424 
425         Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built
426         with SK_DEBUG defined.
427 
428         @param x  column index, zero or greater, and less than width()
429         @param y  row index, zero or greater, and less than height()
430         @return   readable unsigned 64-bit pointer to pixel at (x, y)
431     */
addr64(int x,int y)432     const uint64_t* addr64(int x, int y) const {
433         SkASSERT((unsigned)x < (unsigned)fInfo.width());
434         SkASSERT((unsigned)y < (unsigned)fInfo.height());
435         return (const uint64_t*)((const char*)this->addr64() + y * fRowBytes + (x << 3));
436     }
437 
438     /** Returns readable pixel address at (x, y).
439 
440         Input is not validated: out of bounds values of x or y trigger an assert() if
441         built with SK_DEBUG defined.
442 
443         Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built
444         with SK_DEBUG defined.
445 
446         Each unsigned 16-bit word represents one color component encoded as a half float.
447         Four words correspond to one pixel.
448 
449         @param x  column index, zero or greater, and less than width()
450         @param y  row index, zero or greater, and less than height()
451         @return   readable unsigned 16-bit pointer to pixel component at (x, y)
452     */
addrF16(int x,int y)453     const uint16_t* addrF16(int x, int y) const {
454         SkASSERT(kRGBA_F16_SkColorType     == fInfo.colorType() ||
455                  kRGBA_F16Norm_SkColorType == fInfo.colorType());
456         return reinterpret_cast<const uint16_t*>(this->addr64(x, y));
457     }
458 
459     /** Returns writable base pixel address.
460 
461         @return  writable generic base pointer to pixels
462     */
writable_addr()463     void* writable_addr() const { return const_cast<void*>(fPixels); }
464 
465     /** Returns writable pixel address at (x, y).
466 
467         Input is not validated: out of bounds values of x or y trigger an assert() if
468         built with SK_DEBUG defined. Returns zero if SkColorType is kUnknown_SkColorType.
469 
470         @param x  column index, zero or greater, and less than width()
471         @param y  row index, zero or greater, and less than height()
472         @return   writable generic pointer to pixel
473     */
writable_addr(int x,int y)474     void* writable_addr(int x, int y) const {
475         return const_cast<void*>(this->addr(x, y));
476     }
477 
478     /** Returns writable pixel address at (x, y). Result is addressable as unsigned
479         8-bit bytes. Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType
480         or kGray_8_SkColorType, and is built with SK_DEBUG defined.
481 
482         One byte corresponds to one pixel.
483 
484         @param x  column index, zero or greater, and less than width()
485         @param y  row index, zero or greater, and less than height()
486         @return   writable unsigned 8-bit pointer to pixels
487     */
writable_addr8(int x,int y)488     uint8_t* writable_addr8(int x, int y) const {
489         return const_cast<uint8_t*>(this->addr8(x, y));
490     }
491 
492     /** Returns writable_addr pixel address at (x, y). Result is addressable as unsigned
493         16-bit words. Will trigger an assert() if SkColorType is not kRGB_565_SkColorType
494         or kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
495 
496         One word corresponds to one pixel.
497 
498         @param x  column index, zero or greater, and less than width()
499         @param y  row index, zero or greater, and less than height()
500         @return   writable unsigned 16-bit pointer to pixel
501     */
writable_addr16(int x,int y)502     uint16_t* writable_addr16(int x, int y) const {
503         return const_cast<uint16_t*>(this->addr16(x, y));
504     }
505 
506     /** Returns writable pixel address at (x, y). Result is addressable as unsigned
507         32-bit words. Will trigger an assert() if SkColorType is not
508         kRGBA_8888_SkColorType or kBGRA_8888_SkColorType, and is built with SK_DEBUG
509         defined.
510 
511         One word corresponds to one pixel.
512 
513         @param x  column index, zero or greater, and less than width()
514         @param y  row index, zero or greater, and less than height()
515         @return   writable unsigned 32-bit pointer to pixel
516     */
writable_addr32(int x,int y)517     uint32_t* writable_addr32(int x, int y) const {
518         return const_cast<uint32_t*>(this->addr32(x, y));
519     }
520 
521     /** Returns writable pixel address at (x, y). Result is addressable as unsigned
522         64-bit words. Will trigger an assert() if SkColorType is not
523         kRGBA_F16_SkColorType and is built with SK_DEBUG defined.
524 
525         One word corresponds to one pixel.
526 
527         @param x  column index, zero or greater, and less than width()
528         @param y  row index, zero or greater, and less than height()
529         @return   writable unsigned 64-bit pointer to pixel
530     */
writable_addr64(int x,int y)531     uint64_t* writable_addr64(int x, int y) const {
532         return const_cast<uint64_t*>(this->addr64(x, y));
533     }
534 
535     /** Returns writable pixel address at (x, y). Result is addressable as unsigned
536         16-bit words. Will trigger an assert() if SkColorType is not
537         kRGBA_F16_SkColorType and is built with SK_DEBUG defined.
538 
539         Each word represents one color component encoded as a half float.
540         Four words correspond to one pixel.
541 
542         @param x  column index, zero or greater, and less than width()
543         @param y  row index, zero or greater, and less than height()
544         @return   writable unsigned 16-bit pointer to first component of pixel
545     */
writable_addrF16(int x,int y)546     uint16_t* writable_addrF16(int x, int y) const {
547         return reinterpret_cast<uint16_t*>(writable_addr64(x, y));
548     }
549 
550     /** Copies a SkRect of pixels to dstPixels. Copy starts at (0, 0), and does not
551         exceed SkPixmap (width(), height()).
552 
553         dstInfo specifies width, height, SkColorType, SkAlphaType, and
554         SkColorSpace of destination. dstRowBytes specifics the gap from one destination
555         row to the next. Returns true if pixels are copied. Returns false if
556         dstInfo address equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes().
557 
558         Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is
559         kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match.
560         If SkPixmap colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match.
561         If SkPixmap alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must
562         match. If SkPixmap colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns
563         false if pixel conversion is not possible.
564 
565         Returns false if SkPixmap width() or height() is zero or negative.
566 
567         @param dstInfo      destination width, height, SkColorType, SkAlphaType, SkColorSpace
568         @param dstPixels    destination pixel storage
569         @param dstRowBytes  destination row length
570         @return             true if pixels are copied to dstPixels
571     */
readPixels(const SkImageInfo & dstInfo,void * dstPixels,size_t dstRowBytes)572     bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes) const {
573         return this->readPixels(dstInfo, dstPixels, dstRowBytes, 0, 0);
574     }
575 
576     /** Copies a SkRect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not
577         exceed SkPixmap (width(), height()).
578 
579         dstInfo specifies width, height, SkColorType, SkAlphaType, and
580         SkColorSpace of destination. dstRowBytes specifics the gap from one destination
581         row to the next. Returns true if pixels are copied. Returns false if
582         dstInfo address equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes().
583 
584         Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is
585         kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match.
586         If SkPixmap colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match.
587         If SkPixmap alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must
588         match. If SkPixmap colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns
589         false if pixel conversion is not possible.
590 
591         srcX and srcY may be negative to copy only top or left of source. Returns
592         false if SkPixmap width() or height() is zero or negative. Returns false if:
593         abs(srcX) >= Pixmap width(), or if abs(srcY) >= Pixmap height().
594 
595         @param dstInfo      destination width, height, SkColorType, SkAlphaType, SkColorSpace
596         @param dstPixels    destination pixel storage
597         @param dstRowBytes  destination row length
598         @param srcX         column index whose absolute value is less than width()
599         @param srcY         row index whose absolute value is less than height()
600         @return             true if pixels are copied to dstPixels
601     */
602     bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, int srcX,
603                     int srcY) const;
604 
605     /** Copies a SkRect of pixels to dst. Copy starts at (srcX, srcY), and does not
606         exceed SkPixmap (width(), height()). dst specifies width, height, SkColorType,
607         SkAlphaType, and SkColorSpace of destination.  Returns true if pixels are copied.
608         Returns false if dst address equals nullptr, or dst.rowBytes() is less than
609         dst SkImageInfo::minRowBytes.
610 
611         Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is
612         kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.info().colorType must match.
613         If SkPixmap colorType() is kGray_8_SkColorType, dst.info().colorSpace must match.
614         If SkPixmap alphaType() is kOpaque_SkAlphaType, dst.info().alphaType must
615         match. If SkPixmap colorSpace() is nullptr, dst.info().colorSpace must match. Returns
616         false if pixel conversion is not possible.
617 
618         srcX and srcY may be negative to copy only top or left of source. Returns
619         false SkPixmap width() or height() is zero or negative. Returns false if:
620         abs(srcX) >= Pixmap width(), or if abs(srcY) >= Pixmap height().
621 
622         @param dst   SkImageInfo and pixel address to write to
623         @param srcX  column index whose absolute value is less than width()
624         @param srcY  row index whose absolute value is less than height()
625         @return      true if pixels are copied to dst
626     */
readPixels(const SkPixmap & dst,int srcX,int srcY)627     bool readPixels(const SkPixmap& dst, int srcX, int srcY) const {
628         return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), srcX, srcY);
629     }
630 
631     /** Copies pixels inside bounds() to dst. dst specifies width, height, SkColorType,
632         SkAlphaType, and SkColorSpace of destination.  Returns true if pixels are copied.
633         Returns false if dst address equals nullptr, or dst.rowBytes() is less than
634         dst SkImageInfo::minRowBytes.
635 
636         Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is
637         kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match.
638         If SkPixmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match.
639         If SkPixmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must
640         match. If SkPixmap colorSpace() is nullptr, dst SkColorSpace must match. Returns
641         false if pixel conversion is not possible.
642 
643         Returns false if SkPixmap width() or height() is zero or negative.
644 
645         @param dst  SkImageInfo and pixel address to write to
646         @return     true if pixels are copied to dst
647     */
readPixels(const SkPixmap & dst)648     bool readPixels(const SkPixmap& dst) const {
649         return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), 0, 0);
650     }
651 
652     /** Copies SkBitmap to dst, scaling pixels to fit dst.width() and dst.height(), and
653         converting pixels to match dst.colorType() and dst.alphaType(). Returns true if
654         pixels are copied. Returns false if dst address is nullptr, or dst.rowBytes() is
655         less than dst SkImageInfo::minRowBytes.
656 
657         Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is
658         kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match.
659         If SkPixmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match.
660         If SkPixmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must
661         match. If SkPixmap colorSpace() is nullptr, dst SkColorSpace must match. Returns
662         false if pixel conversion is not possible.
663 
664         Returns false if SkBitmap width() or height() is zero or negative.
665 
666         Scales the image, with filterQuality, to match dst.width() and dst.height().
667         filterQuality kNone_SkFilterQuality is fastest, typically implemented with
668         nearest neighbor filter. kLow_SkFilterQuality is typically implemented with
669         bilerp filter. kMedium_SkFilterQuality is typically implemented with
670         bilerp filter, and mip-map filter when size is reduced.
671         kHigh_SkFilterQuality is slowest, typically implemented with bicubic filter.
672 
673         @param dst            SkImageInfo and pixel address to write to
674         @param filterQuality  one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
675                               kMedium_SkFilterQuality, kHigh_SkFilterQuality
676         @return               true if pixels are scaled to fit dst
677     */
678     bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality) const;
679 
680     /** Writes color to pixels bounded by subset; returns true on success.
681         Returns false if colorType() is kUnknown_SkColorType, or if subset does
682         not intersect bounds().
683 
684         @param color   unpremultiplied color to write
685         @param subset  bounding integer SkRect of written pixels
686         @return        true if pixels are changed
687     */
688     bool erase(SkColor color, const SkIRect& subset) const;
689 
690     /** Writes color to pixels inside bounds(); returns true on success.
691         Returns false if colorType() is kUnknown_SkColorType, or if bounds()
692         is empty.
693 
694         @param color  unpremultiplied color to write
695         @return       true if pixels are changed
696     */
erase(SkColor color)697     bool erase(SkColor color) const { return this->erase(color, this->bounds()); }
698 
699     /** Writes color to pixels bounded by subset; returns true on success.
700         if subset is nullptr, writes colors pixels inside bounds(). Returns false if
701         colorType() is kUnknown_SkColorType, if subset is not nullptr and does
702         not intersect bounds(), or if subset is nullptr and bounds() is empty.
703 
704         @param color   unpremultiplied color to write
705         @param subset  bounding integer SkRect of pixels to write; may be nullptr
706         @return        true if pixels are changed
707     */
708     bool erase(const SkColor4f& color, const SkIRect* subset = nullptr) const;
709 
710 private:
711     const void*     fPixels;
712     size_t          fRowBytes;
713     SkImageInfo     fInfo;
714 
715     friend class SkPixmapPriv;
716 };
717 
718 #endif
719