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