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