blob: f4ed0a394c0a2041a912ba3e6edb3daa776b31c7 [file] [log] [blame]
Cary Clarkbc5697d2017-10-04 14:31:33 -04001#Topic Bitmap
Cary Clark137b8742018-05-30 09:21:49 -04002#Alias Bitmaps ##
3#Alias Bitmap_Reference ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004
5#Class SkBitmap
Cary Clark61313f32018-10-08 14:57:48 -04006#Line # two-dimensional raster pixel array ##
7
8#Code
9#Populate
10##
Cary Clarkbc5697d2017-10-04 14:31:33 -040011
12Bitmap describes a two-dimensional raster pixel array. Bitmap is built on
13Image_Info, containing integer width and height, Color_Type and Alpha_Type
14describing the pixel format, and Color_Space describing the range of colors.
15Bitmap points to Pixel_Ref, which describes the physical array of pixels.
16Image_Info bounds may be located anywhere fully inside Pixel_Ref bounds.
17
18Bitmap can be drawn using Canvas. Bitmap can be a drawing destination for Canvas
Cary Clark137b8742018-05-30 09:21:49 -040019draw member functions. Bitmap flexibility as a pixel container limits some
Cary Clark682c58d2018-05-16 07:07:07 -040020optimizations available to the target platform.
Cary Clarkbc5697d2017-10-04 14:31:33 -040021
22If pixel array is primarily read-only, use Image for better performance.
23If pixel array is primarily written to, use Surface for better performance.
24
25Declaring SkBitmap const prevents altering Image_Info: the Bitmap height, width,
26and so on cannot change. It does not affect Pixel_Ref: a caller may write its
27pixels. Declaring SkBitmap const affects Bitmap configuration, not its contents.
28
Cary Clarkbef063a2017-10-31 15:44:45 -040029Bitmap is not thread safe. Each thread must have its own copy of Bitmap fields,
Cary Clarkbc5697d2017-10-04 14:31:33 -040030although threads may share the underlying pixel array.
31
Cary Clark08895c42018-02-01 09:37:32 -050032#Subtopic Row_Bytes
33#Line # interval from one row to the next ##
Cary Clarkbc5697d2017-10-04 14:31:33 -040034Bitmap pixels may be contiguous, or may have a gap at the end of each row.
35Row_Bytes is the interval from one row to the next. Row_Bytes may be specified;
36sometimes passing zero will compute the Row_Bytes from the row width and the
37number of bytes in a pixel. Row_Bytes may be larger than the row requires. This
38is useful to position one or more Bitmaps within a shared pixel array.
39##
40
Cary Clarkbc5697d2017-10-04 14:31:33 -040041# ------------------------------------------------------------------------------
42
43#Class Allocator
Cary Clark08895c42018-02-01 09:37:32 -050044#Line # abstract subclass of HeapAllocator ##
Cary Clarkbc5697d2017-10-04 14:31:33 -040045#Code
46 class Allocator : public SkRefCnt {
47 public:
48 virtual bool allocPixelRef(SkBitmap* bitmap) = 0;
49 };
50##
51
52Abstract subclass of HeapAllocator.
53
54# ------------------------------------------------------------------------------
55
56#Method virtual bool allocPixelRef(SkBitmap* bitmap) = 0
Cary Clarkd2ca79c2018-08-10 13:09:13 -040057#Line # allocates pixel memory ##
Cary Clarkbc5697d2017-10-04 14:31:33 -040058
59Allocates the pixel memory for the bitmap, given its dimensions and
60Color_Type. Returns true on success, where success means either setPixels
61or setPixelRef was called.
62
63#Param bitmap Bitmap containing Image_Info as input, and Pixel_Ref as output ##
64
65#Return true if Pixel_Ref was allocated ##
66
67#NoExample
68##
69
70#SeeAlso HeapAllocator
71
72##
73
74#Class Allocator ##
75
76# ------------------------------------------------------------------------------
77
78#Class HeapAllocator
Cary Clark08895c42018-02-01 09:37:32 -050079#Line # allocates pixel memory from heap ##
Cary Clarkbc5697d2017-10-04 14:31:33 -040080
81#Code
82 class HeapAllocator : public Allocator {
83 public:
84 bool allocPixelRef(SkBitmap* bitmap) override;
85 };
86##
87
Cary Clark78c110e2018-02-09 16:49:09 -050088Subclass of SkBitmap::Allocator that returns a Pixel_Ref that allocates its pixel
89memory from the heap. This is the default SkBitmap::Allocator invoked by
Cary Clarkbc5697d2017-10-04 14:31:33 -040090allocPixels.
91
92# ------------------------------------------------------------------------------
93
94#Method bool allocPixelRef(SkBitmap* bitmap) override
Cary Clark682c58d2018-05-16 07:07:07 -040095#Line # allocates pixel memory ##
Cary Clarkbc5697d2017-10-04 14:31:33 -040096Allocates the pixel memory for the bitmap, given its dimensions and
97Color_Type. Returns true on success, where success means either setPixels
98or setPixelRef was called.
99
100#Param bitmap Bitmap containing Image_Info as input, and Pixel_Ref as output ##
101
102#Return true if pixels are allocated ##
103
104#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400105 SkBitmap bitmap;
106 bitmap.setInfo(SkImageInfo::MakeN32(16, 16, kPremul_SkAlphaType));
107 SkDebugf("pixel address = %p\n", bitmap.getPixels());
108 SkBitmap::HeapAllocator stdalloc;
109 if (!stdalloc.allocPixelRef(&bitmap)) {
110 SkDebugf("pixel allocation failed\n");
111 } else {
112 SkDebugf("pixel address = %p\n", bitmap.getPixels());
113 }
114#StdOut
Cary Clark884dd7d2017-10-11 10:37:52 -0400115#Volatile
Ben Wagner29380bd2017-10-09 14:43:00 -0400116pixel address = (nil)
117pixel address = 0x560ddd0ac670
118##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400119##
120
Cary Clark78c110e2018-02-09 16:49:09 -0500121#SeeAlso SkBitmap::Allocator tryAllocPixels
Cary Clarkbc5697d2017-10-04 14:31:33 -0400122
123##
124
Cary Clark08895c42018-02-01 09:37:32 -0500125#Class HeapAllocator ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400126
127# ------------------------------------------------------------------------------
128
129#Method SkBitmap()
130
Cary Clarkab2621d2018-01-30 10:08:57 -0500131#Line # constructs with default values ##
Cary Clark682c58d2018-05-16 07:07:07 -0400132Creates an empty Bitmap without pixels, with kUnknown_SkColorType,
Cary Clarkbc5697d2017-10-04 14:31:33 -0400133kUnknown_SkAlphaType, and with a width and height of zero. Pixel_Ref origin is
134set to (0, 0). Bitmap is not volatile.
135
136Use setInfo to associate SkColorType, SkAlphaType, width, and height
137after Bitmap has been created.
138
139#Return empty Bitmap ##
140
141#Example
142void draw(SkCanvas* canvas) {
143 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500144 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
145 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clarkbc5697d2017-10-04 14:31:33 -0400146 SkBitmap bitmap;
147 for (int i = 0; i < 2; ++i) {
148 SkDebugf("width: %2d height: %2d", bitmap.width(), bitmap.height());
149 SkDebugf(" color: k%s_SkColorType", colors[bitmap.colorType()]);
150 SkDebugf(" alpha: k%s_SkAlphaType\n", alphas[bitmap.alphaType()]);
151 bitmap.setInfo(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType),
152 0);
153 }
154}
155#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400156width: 0 height: 0 color: kUnknown_SkColorType alpha: kUnknown_SkAlphaType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400157width: 25 height: 35 color: kRGBA_8888_SkColorType alpha: kOpaque_SkAlphaType
158##
159##
160
161#SeeAlso setInfo
162
163##
164
165# ------------------------------------------------------------------------------
166
167#Method SkBitmap(const SkBitmap& src)
168
Cary Clarkab2621d2018-01-30 10:08:57 -0500169#Line # shares ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400170Copies settings from src to returned Bitmap. Shares pixels if src has pixels
171allocated, so both bitmaps reference the same pixels.
172
173#Param src Bitmap to copy Image_Info, and share Pixel_Ref ##
174
175#Return copy of src ##
176
177#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400178void draw(SkCanvas* canvas) {
179 SkBitmap original;
Cary Clark681287e2018-03-16 11:34:15 -0400180 if (original.tryAllocPixels(
181 SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
182 SkDebugf("original has pixels before copy: %s\n", original.getPixels() ? "true" : "false");
Cary Clark682c58d2018-05-16 07:07:07 -0400183 SkBitmap copy(original);
Cary Clark681287e2018-03-16 11:34:15 -0400184 SkDebugf("original has pixels after copy: %s\n", original.getPixels() ? "true" : "false");
185 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
186 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400187}
188#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400189original has pixels before copy: true
190original has pixels after copy: true
Cary Clarkbc5697d2017-10-04 14:31:33 -0400191copy has pixels: true
192##
193##
194
195#SeeAlso setInfo setPixelRef setPixels swap
196
197##
198
199# ------------------------------------------------------------------------------
200
201#Method SkBitmap(SkBitmap&& src)
202
Cary Clarkab2621d2018-01-30 10:08:57 -0500203#Line # takes ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400204Copies settings from src to returned Bitmap. Moves ownership of src pixels to
205Bitmap.
206
207#Param src Bitmap to copy Image_Info, and reassign Pixel_Ref ##
208
209#Return copy of src ##
210
211#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400212void draw(SkCanvas* canvas) {
213 SkBitmap original;
Cary Clark681287e2018-03-16 11:34:15 -0400214 if (original.tryAllocPixels(
215 SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
216 SkDebugf("original has pixels before move: %s\n", original.getPixels() ? "true" : "false");
Cary Clark682c58d2018-05-16 07:07:07 -0400217 SkBitmap copy(std::move(original));
Cary Clark681287e2018-03-16 11:34:15 -0400218 SkDebugf("original has pixels after move: %s\n", original.getPixels() ? "true" : "false");
219 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
220 }
Ben Wagner29380bd2017-10-09 14:43:00 -0400221}
Cary Clarkbc5697d2017-10-04 14:31:33 -0400222#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400223original has pixels before move: true
224original has pixels after move: false
Cary Clarkbc5697d2017-10-04 14:31:33 -0400225copy has pixels: true
226##
227##
228
229#SeeAlso setInfo setPixelRef setPixels swap
230
231##
232
233# ------------------------------------------------------------------------------
234
235#Method ~SkBitmap()
236
Cary Clarkab2621d2018-01-30 10:08:57 -0500237#Line # releases ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400238Decrements Pixel_Ref reference count, if Pixel_Ref is not nullptr.
239
240#NoExample
241##
242
243#SeeAlso Pixel_Ref
244
245##
246
247# ------------------------------------------------------------------------------
248
249#Method SkBitmap& operator=(const SkBitmap& src)
250
Cary Clarkab2621d2018-01-30 10:08:57 -0500251#Line # shares ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400252Copies settings from src to returned Bitmap. Shares pixels if src has pixels
253allocated, so both bitmaps reference the same pixels.
254
255#Param src Bitmap to copy Image_Info, and share Pixel_Ref ##
256
257#Return copy of src ##
258
259#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400260void draw(SkCanvas* canvas) {
261 SkBitmap original;
Cary Clark681287e2018-03-16 11:34:15 -0400262 if (original.tryAllocPixels(
263 SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
264 SkDebugf("original has pixels before copy: %s\n", original.getPixels() ? "true" : "false");
Cary Clark682c58d2018-05-16 07:07:07 -0400265 SkBitmap copy = original;
Cary Clark681287e2018-03-16 11:34:15 -0400266 SkDebugf("original has pixels after copy: %s\n", original.getPixels() ? "true" : "false");
267 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
268 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400269}
270#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400271original has pixels before copy: true
272original has pixels after copy: true
Cary Clarkbc5697d2017-10-04 14:31:33 -0400273copy has pixels: true
274##
275##
276
277#SeeAlso setInfo setPixelRef setPixels swap
278
279##
280
281# ------------------------------------------------------------------------------
282
283#Method SkBitmap& operator=(SkBitmap&& src)
284
Cary Clarkab2621d2018-01-30 10:08:57 -0500285#Line # takes ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400286Copies settings from src to returned Bitmap. Moves ownership of src pixels to
287Bitmap.
288
289#Param src Bitmap to copy Image_Info, and reassign Pixel_Ref ##
290
291#Return copy of src ##
292
293#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400294void draw(SkCanvas* canvas) {
295 SkBitmap original;
Cary Clark681287e2018-03-16 11:34:15 -0400296 if (original.tryAllocPixels(
297 SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
298 SkDebugf("original has pixels before move: %s\n", original.getPixels() ? "true" : "false");
Cary Clark682c58d2018-05-16 07:07:07 -0400299 SkBitmap copy = std::move(original);
Cary Clark681287e2018-03-16 11:34:15 -0400300 SkDebugf("original has pixels after move: %s\n", original.getPixels() ? "true" : "false");
301 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
302 }
Ben Wagner29380bd2017-10-09 14:43:00 -0400303}
Cary Clarkbc5697d2017-10-04 14:31:33 -0400304#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400305original has pixels before move: true
306original has pixels after move: false
Cary Clarkbc5697d2017-10-04 14:31:33 -0400307copy has pixels: true
308##
309##
310
311#SeeAlso setInfo setPixelRef setPixels swap
312
313##
314
315# ------------------------------------------------------------------------------
316
317#Method void swap(SkBitmap& other)
Cary Clark78de7512018-02-07 07:27:09 -0500318#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -0500319#Line # exchanges Bitmap pair ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400320Swaps the fields of the two bitmaps.
321
322#Param other Bitmap exchanged with original ##
323
324#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400325void draw(SkCanvas* canvas) {
326 auto debugster = [](const char* prefix, const SkBitmap& b) -> void {
327 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500328 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
329 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Ben Wagner29380bd2017-10-09 14:43:00 -0400330 SkDebugf("%s width:%d height:%d colorType:k%s_SkColorType alphaType:k%s_SkAlphaType\n",
331 prefix, b.width(), b.height(), colors[b.colorType()], alphas[b.alphaType()]);
332 };
333 SkBitmap one, two;
Cary Clark681287e2018-03-16 11:34:15 -0400334 if (!one.tryAllocPixels(
335 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
336 return;
337 }
338 if (!two.tryAllocPixels(
339 SkImageInfo::Make(2, 2, kBGRA_8888_SkColorType, kPremul_SkAlphaType))) {
340 return;
341 }
Ben Wagner29380bd2017-10-09 14:43:00 -0400342 for (int index = 0; index < 2; ++index) {
343 debugster("one", one);
344 debugster("two", two);
345 one.swap(two);
346 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400347}
348#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400349one width:1 height:1 colorType:kRGBA_8888_SkColorType alphaType:kOpaque_SkAlphaType
350two width:2 height:2 colorType:kBGRA_8888_SkColorType alphaType:kPremul_SkAlphaType
351one width:2 height:2 colorType:kBGRA_8888_SkColorType alphaType:kPremul_SkAlphaType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400352two width:1 height:1 colorType:kRGBA_8888_SkColorType alphaType:kOpaque_SkAlphaType
353##
354##
355
356#SeeAlso SkBitmap(SkBitmap&& src) operator=(SkBitmap&& src)
357
358##
359
360# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -0500361#Subtopic Property
Cary Clark78de7512018-02-07 07:27:09 -0500362#Line # metrics and attributes ##
363##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400364
Hal Canary99578d22017-12-14 21:13:47 -0500365#Method const SkPixmap& pixmap() const
Cary Clark78de7512018-02-07 07:27:09 -0500366#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500367#Line # returns Pixmap ##
Hal Canary99578d22017-12-14 21:13:47 -0500368Returns a constant reference to the Pixmap holding the Bitmap pixel
369address, row bytes, and Image_Info.
Cary Clark0c5f5462017-12-15 11:21:51 -0500370
Cary Clarkac47b882018-01-11 10:35:44 -0500371#Return reference to Pixmap describing this Bitmap ##
Cary Clark0c5f5462017-12-15 11:21:51 -0500372
373#Example
374 SkBitmap bitmap;
375 bitmap.allocPixels(SkImageInfo::MakeN32Premul(10, 11));
376 SkCanvas offscreen(bitmap);
377 offscreen.clear(SK_ColorWHITE);
378 SkPaint paint;
379 offscreen.drawString("&", 0, 10, paint);
Hal Canary99578d22017-12-14 21:13:47 -0500380 const SkPixmap& pixmap = bitmap.pixmap();
Cary Clark0c5f5462017-12-15 11:21:51 -0500381 if (pixmap.addr()) {
Hal Canary99578d22017-12-14 21:13:47 -0500382 SkPMColor pmWhite = *pixmap.addr32(0, 0);
383 for (int y = 0; y < pixmap.height(); ++y) {
384 for (int x = 0; x < pixmap.width(); ++x) {
385 SkDebugf("%c", *pixmap.addr32(x, y) == pmWhite ? '-' : 'x');
Cary Clark0c5f5462017-12-15 11:21:51 -0500386 }
387 SkDebugf("\n");
388 }
389 }
390 #StdOut
391----------
392---xx-----
393--x--x----
394--x-------
395--xx------
396--x-x---x-
397-x---x--x-
398-x----xx--
399-xx---x---
400--xxxx-xx-
401----------
402 #StdOut ##
403
404##
405
406#SeeAlso peekPixels installPixels readPixels writePixels
407
408##
409
410# ------------------------------------------------------------------------------
411
Cary Clarkbc5697d2017-10-04 14:31:33 -0400412#Method const SkImageInfo& info() const
Cary Clark78de7512018-02-07 07:27:09 -0500413#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500414#Line # returns Image_Info ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400415Returns width, height, Alpha_Type, Color_Type, and Color_Space.
416
417#Return reference to Image_Info ##
418
419#Example
420#Image 4
Ben Wagner29380bd2017-10-09 14:43:00 -0400421void draw(SkCanvas* canvas) {
422 // SkBitmap source; // pre-populated with soccer ball by fiddle.skia.org
423 const SkImageInfo& info = source.info();
424 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500425 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
426 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Ben Wagner29380bd2017-10-09 14:43:00 -0400427 SkDebugf("width: %d height: %d color: %s alpha: %s\n", info.width(), info.height(),
428 colors[info.colorType()], alphas[info.alphaType()]);
429#StdOut
430width: 56 height: 56 color: BGRA_8888 alpha: Opaque
431##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400432}
433##
434
435#SeeAlso Image_Info
436
437##
438
439# ------------------------------------------------------------------------------
440
441#Method int width() const
Cary Clark78de7512018-02-07 07:27:09 -0500442#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500443#Line # returns pixel column count ##
Cary Clark2be81cf2018-09-13 12:04:30 -0400444Returns pixel count in each row. Should be equal or less than
445#Formula # rowBytes() / info().bytesPerPixel() ##.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400446
Cary Clark80247e52018-07-11 16:18:41 -0400447May be less than pixelRef().width(). Will not exceed pixelRef().width() less
Cary Clarkbc5697d2017-10-04 14:31:33 -0400448pixelRefOrigin().fX.
449
450#Return pixel width in Image_Info ##
451
452#Example
453 SkImageInfo info = SkImageInfo::MakeA8(16, 32);
454 SkBitmap bitmap;
455 bitmap.setInfo(info);
456 SkDebugf("bitmap width: %d info width: %d\n", bitmap.width(), info.width());
457#StdOut
458bitmap width: 16 info width: 16
459##
460##
461
462#SeeAlso height() SkPixelRef::width() SkImageInfo::width()
463
464##
465
466# ------------------------------------------------------------------------------
467
468#Method int height() const
Cary Clark78de7512018-02-07 07:27:09 -0500469#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500470#Line # returns pixel row count ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400471Returns pixel row count.
472
473Maybe be less than pixelRef().height(). Will not exceed pixelRef().height() less
474pixelRefOrigin().fY.
475
476#Return pixel height in Image_Info ##
477
478#Example
479 SkImageInfo info = SkImageInfo::MakeA8(16, 32);
480 SkBitmap bitmap;
481 bitmap.setInfo(info);
482 SkDebugf("bitmap height: %d info height: %d\n", bitmap.height(), info.height());
483#StdOut
484bitmap height: 32 info height: 32
485##
486##
487
488#SeeAlso width() SkPixelRef::height() SkImageInfo::height()
489
490##
491
492# ------------------------------------------------------------------------------
493
494#Method SkColorType colorType() const
Cary Clark78de7512018-02-07 07:27:09 -0500495#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500496#Line # returns Image_Info Color_Type ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500497Returns Color_Type, one of: #list_of_color_types#.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400498
499#Return Color_Type in Image_Info ##
500
501#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500502 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
503 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Ben Wagner29380bd2017-10-09 14:43:00 -0400504 SkBitmap bitmap;
505 bitmap.setInfo(SkImageInfo::MakeA8(16, 32));
506 SkDebugf("color type: k" "%s" "_SkColorType\n", colors[bitmap.colorType()]);
Cary Clarkbc5697d2017-10-04 14:31:33 -0400507#StdOut
Cary Clarkab2621d2018-01-30 10:08:57 -0500508color type: kAlpha_8_SkColorType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400509##
510##
511
512#SeeAlso alphaType() SkImageInfo::colorType
513
514##
515
516# ------------------------------------------------------------------------------
517
518#Method SkAlphaType alphaType() const
Cary Clark78de7512018-02-07 07:27:09 -0500519#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500520#Line # returns Image_Info Alpha_Type ##
Cary Clark681287e2018-03-16 11:34:15 -0400521Returns Alpha_Type, one of: #list_of_alpha_types#.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400522
523#Return Alpha_Type in Image_Info ##
524
525#Example
526 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
527 SkPixmap pixmap(SkImageInfo::MakeA8(16, 32), nullptr, 64);
528 SkDebugf("alpha type: k" "%s" "_SkAlphaType\n", alphas[pixmap.alphaType()]);
529#StdOut
530alpha type: kPremul_SkAlphaType
531##
532##
533
534#SeeAlso colorType() SkImageInfo::alphaType
535
536##
537
538# ------------------------------------------------------------------------------
539
540#Method SkColorSpace* colorSpace() const
Cary Clark78de7512018-02-07 07:27:09 -0500541#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500542#Line # returns Image_Info Color_Space ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400543Returns Color_Space, the range of colors, associated with Image_Info. The
544reference count of Color_Space is unchanged. The returned Color_Space is
545immutable.
546
Cary Clark2f466242017-12-11 16:03:17 -0500547#Return Color_Space in Image_Info, or nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400548
549#Example
550#Description
551SkColorSpace::MakeSRGBLinear creates Color_Space with linear gamma
552and an sRGB gamut. This Color_Space gamma is not close to sRGB gamma.
553##
Ben Wagner29380bd2017-10-09 14:43:00 -0400554 SkBitmap bitmap;
Cary Clark682c58d2018-05-16 07:07:07 -0400555 bitmap.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
Cary Clarkbc5697d2017-10-04 14:31:33 -0400556 SkColorSpace::MakeSRGBLinear()));
557 SkColorSpace* colorSpace = bitmap.colorSpace();
558 SkDebugf("gammaCloseToSRGB: %s gammaIsLinear: %s isSRGB: %s\n",
559 colorSpace->gammaCloseToSRGB() ? "true" : "false",
560 colorSpace->gammaIsLinear() ? "true" : "false",
561 colorSpace->isSRGB() ? "true" : "false");
562#StdOut
563gammaCloseToSRGB: false gammaIsLinear: true isSRGB: false
564##
565##
566
567#SeeAlso Color_Space SkImageInfo::colorSpace
568
569##
570
571# ------------------------------------------------------------------------------
572
573#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark78de7512018-02-07 07:27:09 -0500574#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500575#Line # returns Image_Info Color_Space ##
Cary Clark681287e2018-03-16 11:34:15 -0400576Returns smart pointer to Color_Space, the range of colors, associated with
Cary Clarkbc5697d2017-10-04 14:31:33 -0400577Image_Info. The smart pointer tracks the number of objects sharing this
578Color_Space reference so the memory is released when the owners destruct.
579
580The returned Color_Space is immutable.
581
582#Return Color_Space in Image_Info wrapped in a smart pointer ##
583
584#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400585 SkBitmap bitmap1, bitmap2;
Cary Clark682c58d2018-05-16 07:07:07 -0400586 bitmap1.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
Cary Clarkbc5697d2017-10-04 14:31:33 -0400587 SkColorSpace::MakeSRGBLinear()));
588 bitmap2.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
589 bitmap1.refColorSpace()));
590 SkColorSpace* colorSpace = bitmap2.colorSpace();
591 SkDebugf("gammaCloseToSRGB: %s gammaIsLinear: %s isSRGB: %s\n",
592 colorSpace->gammaCloseToSRGB() ? "true" : "false",
593 colorSpace->gammaIsLinear() ? "true" : "false",
594 colorSpace->isSRGB() ? "true" : "false");
595#StdOut
596gammaCloseToSRGB: false gammaIsLinear: true isSRGB: false
597##
598##
599
600#SeeAlso Color_Space SkImageInfo::colorSpace
601
602##
603
604# ------------------------------------------------------------------------------
605
606#Method int bytesPerPixel() const
Cary Clark78de7512018-02-07 07:27:09 -0500607#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500608#Line # returns number of bytes in pixel based on Color_Type ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400609Returns number of bytes per pixel required by Color_Type.
610Returns zero if colorType( is kUnknown_SkColorType.
611
612#Return bytes in pixel ##
613
614#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500615 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
616 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clarkbc5697d2017-10-04 14:31:33 -0400617 SkImageInfo info = SkImageInfo::MakeA8(1, 1);
618 SkBitmap bitmap;
Cary Clark1a8d7622018-03-05 13:26:16 -0500619 for (SkColorType colorType : { #list_of_color_types#
620 } ) {
Cary Clarkbc5697d2017-10-04 14:31:33 -0400621 bitmap.setInfo(info.makeColorType(colorType));
622 SkDebugf("color: k" "%s" "_SkColorType" "%*s" "bytesPerPixel: %d\n",
Cary Clarkab2621d2018-01-30 10:08:57 -0500623 colors[colorType], 13 - strlen(colors[colorType]), " ",
Cary Clarkbc5697d2017-10-04 14:31:33 -0400624 bitmap.bytesPerPixel());
625 }
626#StdOut
Cary Clark98aebac2018-03-13 09:02:35 -0400627color: kUnknown_SkColorType bytesPerPixel: 0
628color: kAlpha_8_SkColorType bytesPerPixel: 1
629color: kRGB_565_SkColorType bytesPerPixel: 2
630color: kARGB_4444_SkColorType bytesPerPixel: 2
631color: kRGBA_8888_SkColorType bytesPerPixel: 4
632color: kRGB_888x_SkColorType bytesPerPixel: 4
633color: kBGRA_8888_SkColorType bytesPerPixel: 4
634color: kRGBA_1010102_SkColorType bytesPerPixel: 4
635color: kRGB_101010x_SkColorType bytesPerPixel: 4
636color: kGray_8_SkColorType bytesPerPixel: 1
Cary Clarkab2621d2018-01-30 10:08:57 -0500637color: kRGBA_F16_SkColorType bytesPerPixel: 8
Cary Clarkbc5697d2017-10-04 14:31:33 -0400638##
639##
640
Cary Clark681287e2018-03-16 11:34:15 -0400641#SeeAlso rowBytes rowBytesAsPixels width shiftPerPixel SkImageInfo::bytesPerPixel
Cary Clarkbc5697d2017-10-04 14:31:33 -0400642
643##
644
645# ------------------------------------------------------------------------------
646
647#Method int rowBytesAsPixels() const
Cary Clark78de7512018-02-07 07:27:09 -0500648#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500649#Line # returns interval between rows in pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400650Returns number of pixels that fit on row. Should be greater than or equal to
651width().
652
653#Return maximum pixels per row ##
654
655#Example
656 SkBitmap bitmap;
657 for (int rowBytes : { 4, 5, 6, 7, 8} ) {
658 bitmap.setInfo(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), rowBytes);
659 SkDebugf("rowBytes: %d rowBytesAsPixels: %d\n", rowBytes, bitmap.rowBytesAsPixels());
660 }
661#StdOut
662rowBytes: 4 rowBytesAsPixels: 1
663rowBytes: 5 rowBytesAsPixels: 1
664rowBytes: 6 rowBytesAsPixels: 1
665rowBytes: 7 rowBytesAsPixels: 1
666rowBytes: 8 rowBytesAsPixels: 2
667##
668##
669
670#SeeAlso rowBytes shiftPerPixel width bytesPerPixel
671
672##
673
674# ------------------------------------------------------------------------------
675
676#Method int shiftPerPixel() const
Cary Clark78de7512018-02-07 07:27:09 -0500677#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500678#Line # returns bit shift from pixels to bytes ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400679Returns bit shift converting row bytes to row pixels.
680Returns zero for kUnknown_SkColorType.
681
682#Return one of: 0, 1, 2, 3; left shift to convert pixels to bytes ##
683
684#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500685 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
686 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clarkbc5697d2017-10-04 14:31:33 -0400687 SkImageInfo info = SkImageInfo::MakeA8(1, 1);
688 SkBitmap bitmap;
Cary Clark1a8d7622018-03-05 13:26:16 -0500689 for (SkColorType colorType : { #list_of_color_types#
690 } ) {
Cary Clarkbc5697d2017-10-04 14:31:33 -0400691 bitmap.setInfo(info.makeColorType(colorType));
692 SkDebugf("color: k" "%s" "_SkColorType" "%*s" "shiftPerPixel: %d\n",
Cary Clark1a8d7622018-03-05 13:26:16 -0500693 colors[colorType], 14 - strlen(colors[colorType]), " ",
Cary Clarkbc5697d2017-10-04 14:31:33 -0400694 bitmap.shiftPerPixel());
695 }
696#StdOut
Cary Clark98aebac2018-03-13 09:02:35 -0400697color: kUnknown_SkColorType shiftPerPixel: 0
698color: kAlpha_8_SkColorType shiftPerPixel: 0
699color: kRGB_565_SkColorType shiftPerPixel: 1
700color: kARGB_4444_SkColorType shiftPerPixel: 1
701color: kRGBA_8888_SkColorType shiftPerPixel: 2
702color: kRGB_888x_SkColorType shiftPerPixel: 2
703color: kBGRA_8888_SkColorType shiftPerPixel: 2
704color: kRGBA_1010102_SkColorType shiftPerPixel: 2
705color: kRGB_101010x_SkColorType shiftPerPixel: 2
706color: kGray_8_SkColorType shiftPerPixel: 0
Cary Clark1a8d7622018-03-05 13:26:16 -0500707color: kRGBA_F16_SkColorType shiftPerPixel: 3
Cary Clarkbc5697d2017-10-04 14:31:33 -0400708##
709##
710
711#SeeAlso rowBytes rowBytesAsPixels width bytesPerPixel
712
713##
714
715# ------------------------------------------------------------------------------
716
717#Method bool empty() const
Cary Clark78de7512018-02-07 07:27:09 -0500718#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500719#Line # returns true if Image_Info has zero width() or height() ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400720Returns true if either width() or height() are zero.
721
722Does not check if Pixel_Ref is nullptr; call drawsNothing to check width(),
723height(), and Pixel_Ref.
724
725#Return true if dimensions do not enclose area ##
726
727#Example
728 SkBitmap bitmap;
729 for (int width : { 0, 2 } ) {
730 for (int height : { 0, 2 } ) {
731 bitmap.setInfo(SkImageInfo::MakeA8(width, height));
732 SkDebugf("width: %d height: %d empty: %s\n", width, height,
733 bitmap.empty() ? "true" : "false");
734 }
735 }
736#StdOut
737width: 0 height: 0 empty: true
738width: 0 height: 2 empty: true
739width: 2 height: 0 empty: true
740width: 2 height: 2 empty: false
741##
742##
743
Cary Clark682c58d2018-05-16 07:07:07 -0400744#SeeAlso height() width() drawsNothing
Cary Clarkbc5697d2017-10-04 14:31:33 -0400745
746##
747
748# ------------------------------------------------------------------------------
749
750#Method bool isNull() const
Cary Clark78de7512018-02-07 07:27:09 -0500751#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500752#Line # returns true if Pixel_Ref is nullptr ##
Cary Clark80247e52018-07-11 16:18:41 -0400753Returns true if Pixel_Ref is nullptr.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400754
755Does not check if width() or height() are zero; call drawsNothing to check
756width(), height(), and Pixel_Ref.
757
758#Return true if no Pixel_Ref is associated ##
759
760#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400761 SkBitmap bitmap;
762 SkDebugf("empty bitmap does %shave pixels\n", bitmap.isNull() ? "not " : "");
763 bitmap.setInfo(SkImageInfo::MakeA8(8, 8));
764 SkDebugf("bitmap with dimensions does %shave pixels\n", bitmap.isNull() ? "not " : "");
765 bitmap.allocPixels();
766 SkDebugf("allocated bitmap does %shave pixels\n", bitmap.isNull() ? "not " : "");
Cary Clarkbc5697d2017-10-04 14:31:33 -0400767#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400768empty bitmap does not have pixels
769bitmap with dimensions does not have pixels
Cary Clarkbc5697d2017-10-04 14:31:33 -0400770allocated bitmap does have pixels
771##
772##
773
774#SeeAlso empty() drawsNothing pixelRef
775
776##
777
778# ------------------------------------------------------------------------------
779
780#Method bool drawsNothing() const
Cary Clark78de7512018-02-07 07:27:09 -0500781#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500782#Line # returns true if no width(), no height(), or no Pixel_Ref ##
Cary Clark80247e52018-07-11 16:18:41 -0400783Returns true if width() or height() are zero, or if Pixel_Ref is nullptr.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400784If true, Bitmap has no effect when drawn or drawn into.
785
786#Return true if drawing has no effect ##
787
788#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400789 SkBitmap bitmap;
790 for (int w : { 0, 8 } ) {
791 for (bool allocate : { false, true} ) {
792 bitmap.setInfo(SkImageInfo::MakeA8(w, 8));
793 allocate ? bitmap.allocPixels() : (void) 0 ;
794 SkDebugf("empty:%s isNull:%s drawsNothing:%s\n", bitmap.empty() ? "true " : "false",
795 bitmap.isNull() ? "true " : "false", bitmap.drawsNothing() ? "true" : "false");
796 }
797 }
798#StdOut
799empty:true isNull:true drawsNothing:true
800empty:true isNull:false drawsNothing:true
801empty:false isNull:true drawsNothing:true
802empty:false isNull:false drawsNothing:false
803##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400804##
805
806#SeeAlso empty() isNull pixelRef
807
808##
809
810# ------------------------------------------------------------------------------
811
812#Method size_t rowBytes() const
Cary Clark78de7512018-02-07 07:27:09 -0500813#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500814#Line # returns interval between rows in bytes ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400815Returns row bytes, the interval from one pixel row to the next. Row bytes
Cary Clark2be81cf2018-09-13 12:04:30 -0400816is at least as large as: #Formula # width() * info().bytesPerPixel() ##.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400817
818Returns zero if colorType is kUnknown_SkColorType, or if row bytes supplied to
819setInfo is not large enough to hold a row of pixels.
820
821#Return byte length of pixel row ##
822
823#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400824 SkBitmap bitmap;
825 for (int rowBytes : { 2, 8 } ) {
826 bool result = bitmap.setInfo(SkImageInfo::MakeA8(4, 4), rowBytes);
827 SkDebugf("setInfo returned:%s rowBytes:%d\n", result ? "true " : "false", bitmap.rowBytes());
828 }
829#StdOut
830setInfo returned:false rowBytes:0
831setInfo returned:true rowBytes:8
832##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400833##
834
835#SeeAlso info() setInfo SkImageInfo::minRowBytes
836
837##
838
839# ------------------------------------------------------------------------------
840
841#Method bool setAlphaType(SkAlphaType alphaType)
Cary Clark78de7512018-02-07 07:27:09 -0500842#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -0500843#Line # sets Alpha_Type of shared pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400844Sets Alpha_Type, if alphaType is compatible with Color_Type.
845Returns true unless alphaType is kUnknown_SkAlphaType and current Alpha_Type
846is not kUnknown_SkAlphaType.
847
848Returns true if Color_Type is kUnknown_SkColorType. alphaType is ignored, and
849Alpha_Type remains kUnknown_SkAlphaType.
850
851Returns true if Color_Type is kRGB_565_SkColorType or kGray_8_SkColorType.
852alphaType is ignored, and Alpha_Type remains kOpaque_SkAlphaType.
853
854If Color_Type is kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
855kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: returns true unless
856alphaType is kUnknown_SkAlphaType and Alpha_Type is not kUnknown_SkAlphaType.
857If Alpha_Type is kUnknown_SkAlphaType, alphaType is ignored.
858
859If Color_Type is kAlpha_8_SkColorType, returns true unless
860alphaType is kUnknown_SkAlphaType and Alpha_Type is not kUnknown_SkAlphaType.
861If Alpha_Type is kUnknown_SkAlphaType, alphaType is ignored. If alphaType is
Cary Clark682c58d2018-05-16 07:07:07 -0400862kUnpremul_SkAlphaType, it is treated as kPremul_SkAlphaType.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400863
864This changes Alpha_Type in Pixel_Ref; all bitmaps sharing Pixel_Ref
865are affected.
866
Cary Clark681287e2018-03-16 11:34:15 -0400867#Param alphaType one of: #list_of_alpha_types#
Cary Clarkbc5697d2017-10-04 14:31:33 -0400868##
869
870#Return true if Alpha_Type is set ##
871
872#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400873void draw(SkCanvas* canvas) {
Cary Clarkab2621d2018-01-30 10:08:57 -0500874 const char* colors[] = { "Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
875 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16" };
Cary Clark682c58d2018-05-16 07:07:07 -0400876 const char* alphas[] = {"Unknown ", "Opaque ", "Premul ", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500877 SkBitmap bitmap;
Cary Clark681287e2018-03-16 11:34:15 -0400878 SkAlphaType alphaTypes[] = { #list_of_alpha_types#
879 };
Cary Clarkab2621d2018-01-30 10:08:57 -0500880 SkDebugf("%88s", "Canonical Unknown Opaque Premul Unpremul\n");
Cary Clark1a8d7622018-03-05 13:26:16 -0500881 for (SkColorType colorType : { #list_of_color_types#
882 } ) {
Ben Wagner29380bd2017-10-09 14:43:00 -0400883 for (SkAlphaType canonicalAlphaType : alphaTypes) {
884 SkColorTypeValidateAlphaType(colorType, kUnknown_SkAlphaType, &canonicalAlphaType );
885 SkDebugf("%10s %10s ", colors[(int) colorType], alphas[(int) canonicalAlphaType ]);
886 for (SkAlphaType alphaType : alphaTypes) {
887 bitmap.setInfo(SkImageInfo::Make(4, 4, colorType, canonicalAlphaType));
888 bool result = bitmap.setAlphaType(alphaType);
889 SkDebugf("%s %s ", result ? "true " : "false", alphas[(int) bitmap.alphaType()]);
890 }
891 SkDebugf("\n");
892 }
893 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400894}
895##
896
897#SeeAlso Alpha_Type Color_Type Image_Info setInfo
898
899##
900
901# ------------------------------------------------------------------------------
902
903#Method void* getPixels() const
Cary Clark78de7512018-02-07 07:27:09 -0500904#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500905#Line # returns address of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400906Returns pixel address, the base address corresponding to the pixel origin.
907
908#Return pixel address ##
909
910#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400911 SkBitmap bitmap;
912 bitmap.setInfo(SkImageInfo::MakeN32(4, 4, kPremul_SkAlphaType));
913 bitmap.allocPixels();
914 bitmap.eraseColor(0x00000000);
915 void* baseAddr = bitmap.getPixels();
916 *(SkPMColor*)baseAddr = 0xFFFFFFFF;
917 SkDebugf("bitmap.getColor(0, 1) %c= 0x00000000\n",
918 bitmap.getColor(0, 1) == 0x00000000 ? '=' : '!');
919 SkDebugf("bitmap.getColor(0, 0) %c= 0xFFFFFFFF\n",
920 bitmap.getColor(0, 0) == 0xFFFFFFFF ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -0400921#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400922bitmap.getColor(0, 1) == 0x00000000
Cary Clarkbc5697d2017-10-04 14:31:33 -0400923bitmap.getColor(0, 0) == 0xFFFFFFFF
924##
925##
926
927#SeeAlso isNull drawsNothing
928
929##
930
931# ------------------------------------------------------------------------------
932
933#Method size_t computeByteSize() const
Cary Clark78de7512018-02-07 07:27:09 -0500934#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -0500935#Line # returns size required for pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400936Returns minimum memory required for pixel storage.
937Does not include unused memory on last row when rowBytesAsPixels exceeds width().
938Returns zero if result does not fit in size_t.
939Returns zero if height() or width() is 0.
940Returns height() times rowBytes if colorType is kUnknown_SkColorType.
941
942#Return size in bytes of image buffer ##
943
944#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400945 SkBitmap bitmap;
Cary Clarkbc5697d2017-10-04 14:31:33 -0400946 for (int width : { 1, 1000, 1000000 } ) {
947 for (int height: { 1, 1000, 1000000 } ) {
948 SkImageInfo imageInfo = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
949 bitmap.setInfo(imageInfo, width * 5);
950 SkDebugf("width: %7d height: %7d computeByteSize: %13lld\n", width, height,
951 bitmap.computeByteSize());
952 }
953 }
954#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400955width: 1 height: 1 computeByteSize: 4
956width: 1 height: 1000 computeByteSize: 4999
957width: 1 height: 1000000 computeByteSize: 4999999
958width: 1000 height: 1 computeByteSize: 4000
959width: 1000 height: 1000 computeByteSize: 4999000
960width: 1000 height: 1000000 computeByteSize: 4999999000
961width: 1000000 height: 1 computeByteSize: 4000000
962width: 1000000 height: 1000 computeByteSize: 4999000000
Cary Clarkbc5697d2017-10-04 14:31:33 -0400963width: 1000000 height: 1000000 computeByteSize: 4999999000000
964##
965##
966
967#SeeAlso SkImageInfo::computeByteSize
968
969##
970
971# ------------------------------------------------------------------------------
972
Cary Clarkbc5697d2017-10-04 14:31:33 -0400973#Method bool isImmutable() const
Cary Clark78de7512018-02-07 07:27:09 -0500974#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500975#Line # returns true if pixels will not change ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400976Returns true if pixels can not change.
977
978Most immutable Bitmap checks trigger an assert only on debug builds.
979
980#Return true if pixels are immutable ##
981
982#Example
Cary Clark682c58d2018-05-16 07:07:07 -0400983 SkBitmap original;
Ben Wagner29380bd2017-10-09 14:43:00 -0400984 SkImageInfo info = SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
985 if (original.tryAllocPixels(info)) {
986 original.setImmutable();
987 SkBitmap copy;
Cary Clark682c58d2018-05-16 07:07:07 -0400988 original.extractSubset(&copy, {5, 10, 15, 20});
Ben Wagner29380bd2017-10-09 14:43:00 -0400989 SkDebugf("original is " "%s" "immutable\n", original.isImmutable() ? "" : "not ");
990 SkDebugf("copy is " "%s" "immutable\n", copy.isImmutable() ? "" : "not ");
Cary Clarkbc5697d2017-10-04 14:31:33 -0400991 }
992#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400993original is immutable
Cary Clarkbc5697d2017-10-04 14:31:33 -0400994copy is immutable
995##
996##
997
998#SeeAlso setImmutable SkPixelRef::isImmutable SkImage
999
1000##
1001
1002# ------------------------------------------------------------------------------
1003
1004#Method void setImmutable()
Cary Clark78de7512018-02-07 07:27:09 -05001005#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05001006#Line # marks that pixels will not change ##
Cary Clark154beea2017-10-26 07:58:48 -04001007Sets internal flag to mark Bitmap as immutable. Once set, pixels can not change.
Cary Clark682c58d2018-05-16 07:07:07 -04001008Any other bitmap sharing the same Pixel_Ref are also marked as immutable.
Cary Clark154beea2017-10-26 07:58:48 -04001009Once Pixel_Ref is marked immutable, the setting cannot be cleared.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001010
1011Writing to immutable Bitmap pixels triggers an assert on debug builds.
Cary Clark682c58d2018-05-16 07:07:07 -04001012
Cary Clarkbc5697d2017-10-04 14:31:33 -04001013#Example
1014#Description
1015Triggers assert if SK_DEBUG is true, runs fine otherwise.
1016##
Ben Wagner29380bd2017-10-09 14:43:00 -04001017 SkBitmap bitmap;
1018 bitmap.setInfo(SkImageInfo::MakeN32(4, 4, kPremul_SkAlphaType));
1019 bitmap.allocPixels();
1020 SkCanvas offscreen(bitmap);
1021 SkDebugf("draw white\n");
1022 offscreen.clear(SK_ColorWHITE);
1023 bitmap.setImmutable();
1024 SkDebugf("draw black\n");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001025 offscreen.clear(SK_ColorBLACK);
1026##
1027
1028#SeeAlso isImmutable SkPixelRef::setImmutable SkImage
1029
1030##
1031
1032# ------------------------------------------------------------------------------
1033
1034#Method bool isOpaque() const
Cary Clark78de7512018-02-07 07:27:09 -05001035#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001036#Line # returns true if Image_Info describes opaque pixels ##
Cary Clark681287e2018-03-16 11:34:15 -04001037
1038Returns true if Alpha_Type is set to hint that all pixels are opaque; their
1039Color_Alpha value is implicitly or explicitly 1.0. If true, and all pixels are
Cary Clark682c58d2018-05-16 07:07:07 -04001040not opaque, Skia may draw incorrectly.
Cary Clark681287e2018-03-16 11:34:15 -04001041
Cary Clarkbc5697d2017-10-04 14:31:33 -04001042Does not check if Color_Type allows Alpha, or if any pixel value has
1043transparency.
1044
Cary Clark681287e2018-03-16 11:34:15 -04001045#Return true if Image_Info Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001046
1047#Example
1048#Description
1049 isOpaque ignores whether all pixels are opaque or not.
1050##
Ben Wagner29380bd2017-10-09 14:43:00 -04001051 const int height = 2;
1052 const int width = 2;
1053 SkBitmap bitmap;
1054 bitmap.setInfo(SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType));
1055 for (int index = 0; index < 2; ++index) {
1056 bitmap.allocPixels();
1057 bitmap.eraseColor(0x00000000);
1058 SkDebugf("isOpaque: %s\n", bitmap.isOpaque() ? "true" : "false");
1059 bitmap.eraseColor(0xFFFFFFFF);
1060 SkDebugf("isOpaque: %s\n", bitmap.isOpaque() ? "true" : "false");
1061 bitmap.setInfo(bitmap.info().makeAlphaType(kOpaque_SkAlphaType));
1062 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001063#StdOut
1064isOpaque: false
1065isOpaque: false
1066isOpaque: true
1067isOpaque: true
1068##
1069##
1070
1071#SeeAlso ComputeIsOpaque SkImageInfo::isOpaque
1072
1073##
1074
1075# ------------------------------------------------------------------------------
1076
1077#Method bool isVolatile() const
Cary Clark78de7512018-02-07 07:27:09 -05001078#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001079#Line # returns true if pixels should not be cached ##
Cary Clark80247e52018-07-11 16:18:41 -04001080Provides a hint to caller that pixels should not be cached. Only true if
1081setIsVolatile has been called to mark as volatile.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001082
1083Volatile state is not shared by other bitmaps sharing the same Pixel_Ref.
1084
1085#Return true if marked volatile ##
1086
1087#Example
Cary Clark682c58d2018-05-16 07:07:07 -04001088 SkBitmap original;
Ben Wagner29380bd2017-10-09 14:43:00 -04001089 SkImageInfo info = SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1090 if (original.tryAllocPixels(info)) {
1091 original.setIsVolatile(true);
1092 SkBitmap copy;
Cary Clark682c58d2018-05-16 07:07:07 -04001093 original.extractSubset(&copy, {5, 10, 15, 20});
Ben Wagner29380bd2017-10-09 14:43:00 -04001094 SkDebugf("original is " "%s" "volatile\n", original.isVolatile() ? "" : "not ");
1095 SkDebugf("copy is " "%s" "volatile\n", copy.isImmutable() ? "" : "not ");
1096 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001097#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001098original is volatile
Cary Clarkbc5697d2017-10-04 14:31:33 -04001099copy is not volatile
1100##
1101##
1102
1103#SeeAlso setIsVolatile
1104
1105##
1106
1107# ------------------------------------------------------------------------------
1108
1109#Method void setIsVolatile(bool isVolatile)
Cary Clark78de7512018-02-07 07:27:09 -05001110#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05001111#Line # marks if pixels should not be cached ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001112Sets if pixels should be read from Pixel_Ref on every access. Bitmaps are not
1113volatile by default; a GPU back end may upload pixel values expecting them to be
1114accessed repeatedly. Marking temporary Bitmaps as volatile provides a hint to
1115Device that the Bitmap pixels should not be cached. This can
1116improve performance by avoiding overhead and reducing resource
1117consumption on Device.
1118
1119#Param isVolatile true if backing pixels are temporary ##
1120
1121#Example
1122#Height 20
Cary Clark682c58d2018-05-16 07:07:07 -04001123 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04001124 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
1125 bitmap.allocPixels();
1126 bitmap.eraseColor(SK_ColorRED);
1127 canvas->scale(16, 16);
1128 canvas->drawBitmap(bitmap, 0, 0);
1129 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorBLUE);
1130 canvas->drawBitmap(bitmap, 2, 0);
1131 bitmap.setIsVolatile(true);
1132 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorGREEN);
1133 canvas->drawBitmap(bitmap, 4, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001134##
1135
1136#SeeAlso isVolatile
1137
1138##
1139
1140# ------------------------------------------------------------------------------
1141
1142#Method void reset()
Cary Clark61313f32018-10-08 14:57:48 -04001143#In Constructors
Cary Clarkab2621d2018-01-30 10:08:57 -05001144#Line # sets to default values, releases pixel ownership ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001145Resets to its initial state; all fields are set to zero, as if Bitmap had
1146been initialized by SkBitmap().
1147
Cary Clark682c58d2018-05-16 07:07:07 -04001148Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to
Cary Clarkbc5697d2017-10-04 14:31:33 -04001149kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType.
1150
1151If Pixel_Ref is allocated, its reference count is decreased by one, releasing
1152its memory if Bitmap is the sole owner.
1153
1154#Example
Cary Clark682c58d2018-05-16 07:07:07 -04001155 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04001156 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
1157 bitmap.allocPixels();
1158 SkDebugf("width:%d height:%d isNull:%s\n", bitmap.width(), bitmap.height(),
1159 bitmap.isNull() ? "true" : "false");
1160 bitmap.reset();
1161 SkDebugf("width:%d height:%d isNull:%s\n", bitmap.width(), bitmap.height(),
1162 bitmap.isNull() ? "true" : "false");
1163#StdOut
1164width:1 height:1 isNull:false
1165width:0 height:0 isNull:true
1166##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001167##
1168
1169#SeeAlso SkBitmap() SkAlphaType SkColorType
1170
1171##
1172
1173# ------------------------------------------------------------------------------
1174
1175#Method static bool ComputeIsOpaque(const SkBitmap& bm)
Cary Clark78de7512018-02-07 07:27:09 -05001176#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05001177#Line # returns true if all pixels are opaque ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001178Returns true if all pixels are opaque. Color_Type determines how pixels
1179are encoded, and whether pixel describes Alpha. Returns true for Color_Types
1180without alpha in each pixel; for other Color_Types, returns true if all
1181pixels have alpha values equivalent to 1.0 or greater.
1182
1183For Color_Types kRGB_565_SkColorType or kGray_8_SkColorType: always
1184returns true. For Color_Types kAlpha_8_SkColorType, kBGRA_8888_SkColorType,
1185kRGBA_8888_SkColorType: returns true if all pixel Alpha values are 255.
1186For Color_Type kARGB_4444_SkColorType: returns true if all pixel Alpha values are 15.
1187For kRGBA_F16_SkColorType: returns true if all pixel Alpha values are 1.0 or
1188greater.
1189
Cary Clark682c58d2018-05-16 07:07:07 -04001190Returns false for kUnknown_SkColorType.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001191
1192#Param bm Bitmap to check ##
1193
1194#Return true if all pixels have opaque values or Color_Type is opaque ##
1195
1196#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001197 SkBitmap bitmap;
1198 bitmap.setInfo(SkImageInfo::Make(2, 2, kN32_SkColorType, kPremul_SkAlphaType));
1199 for (int index = 0; index < 2; ++index) {
1200 bitmap.allocPixels();
1201 bitmap.eraseColor(0x00000000);
1202 SkDebugf("computeIsOpaque: %s\n", SkBitmap::ComputeIsOpaque(bitmap) ? "true" : "false");
1203 bitmap.eraseColor(0xFFFFFFFF);
1204 SkDebugf("computeIsOpaque: %s\n", SkBitmap::ComputeIsOpaque(bitmap) ? "true" : "false");
1205 bitmap.setInfo(bitmap.info().makeAlphaType(kOpaque_SkAlphaType));
Cary Clarkbc5697d2017-10-04 14:31:33 -04001206 }
1207#StdOut
1208computeIsOpaque: false
1209computeIsOpaque: true
1210computeIsOpaque: false
1211computeIsOpaque: true
1212##
1213##
1214
1215#SeeAlso isOpaque Color_Type Alpha
1216
1217##
1218
1219# ------------------------------------------------------------------------------
1220
1221#Method void getBounds(SkRect* bounds) const
Cary Clark78de7512018-02-07 07:27:09 -05001222#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001223#Line # returns width() and height() as Rectangle ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001224Returns Rect { 0, 0, width(), height() }.
1225
1226#Param bounds container for floating point rectangle ##
1227
1228#Example
1229#Height 160
1230#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001231 SkRect bounds;
1232 source.getBounds(&bounds);
1233 bounds.offset(100, 100);
1234 SkPaint paint;
1235 paint.setColor(SK_ColorGRAY);
1236 canvas->scale(.25f, .25f);
1237 canvas->drawRect(bounds, paint);
1238 canvas->drawBitmap(source, 40, 40);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001239##
1240
Cary Clark682c58d2018-05-16 07:07:07 -04001241#SeeAlso bounds()
Cary Clarkbc5697d2017-10-04 14:31:33 -04001242
1243##
1244
1245# ------------------------------------------------------------------------------
1246
1247#Method void getBounds(SkIRect* bounds) const
1248
1249Returns IRect { 0, 0, width(), height() }.
1250
1251#Param bounds container for integral rectangle ##
1252
1253#Example
1254#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001255 SkIRect bounds;
1256 source.getBounds(&bounds);
1257 bounds.inset(100, 100);
1258 SkBitmap bitmap;
1259 source.extractSubset(&bitmap, bounds);
1260 canvas->scale(.5f, .5f);
1261 canvas->drawBitmap(bitmap, 10, 10);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001262##
1263
Cary Clark682c58d2018-05-16 07:07:07 -04001264#SeeAlso bounds()
Cary Clarkbc5697d2017-10-04 14:31:33 -04001265
1266##
1267
1268# ------------------------------------------------------------------------------
1269
1270#Method SkIRect bounds() const
Cary Clark78de7512018-02-07 07:27:09 -05001271#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001272#Line # returns width() and height() as Rectangle ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001273Returns IRect { 0, 0, width(), height() }.
1274
1275#Return integral rectangle from origin to width() and height() ##
1276
1277#Example
Cary Clark681287e2018-03-16 11:34:15 -04001278#Height 64
Cary Clarkbc5697d2017-10-04 14:31:33 -04001279#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -05001280 canvas->scale(.5f, .5f);
Ben Wagner29380bd2017-10-09 14:43:00 -04001281 SkIRect bounds = source.bounds();
1282 for (int x : { 0, bounds.width() } ) {
1283 for (int y : { 0, bounds.height() } ) {
1284 canvas->drawBitmap(source, x, y);
1285 }
1286 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001287##
1288
Cary Clark682c58d2018-05-16 07:07:07 -04001289#SeeAlso getBounds
Cary Clarkbc5697d2017-10-04 14:31:33 -04001290
1291##
1292
1293# ------------------------------------------------------------------------------
1294
1295#Method SkISize dimensions() const
Cary Clark78de7512018-02-07 07:27:09 -05001296#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001297#Line # returns width() and height() ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001298Returns ISize { width(), height() }.
1299
1300#Return integral size of width() and height() ##
1301
1302#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001303 SkBitmap bitmap;
1304 bitmap.setInfo(SkImageInfo::MakeN32(33, 55, kOpaque_SkAlphaType));
1305 SkISize dimensions = bitmap.dimensions();
1306 SkRect bounds;
1307 bitmap.getBounds(&bounds);
1308 SkRect dimensionsAsBounds = SkRect::Make(dimensions);
1309 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -04001310##
1311
1312#SeeAlso height() width()
1313
1314##
1315
1316# ------------------------------------------------------------------------------
1317
1318#Method SkIRect getSubset() const
Cary Clark78de7512018-02-07 07:27:09 -05001319#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001320#Line # returns bounds offset by origin ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001321Returns the bounds of this bitmap, offset by its Pixel_Ref origin.
1322
1323#Return bounds within Pixel_Ref bounds ##
1324
1325#Example
1326#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001327 SkIRect bounds;
1328 source.getBounds(&bounds);
1329 bounds.inset(100, 100);
1330 SkBitmap subset;
1331 source.extractSubset(&subset, bounds);
1332 SkIRect r = source.getSubset();
1333 SkDebugf("source: %d, %d, %d, %d\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
1334 r = subset.getSubset();
1335 SkDebugf("subset: %d, %d, %d, %d\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
1336#StdOut
1337source: 0, 0, 512, 512
1338subset: 100, 100, 412, 412
1339##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001340##
1341
1342#SeeAlso extractSubset getBounds
1343
1344##
1345
1346# ------------------------------------------------------------------------------
1347
1348#Method bool setInfo(const SkImageInfo& imageInfo, size_t rowBytes = 0)
Cary Clark78de7512018-02-07 07:27:09 -05001349#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05001350#Line # sets height, width, Color_Type, and so on, releasing pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001351Sets width, height, Alpha_Type, Color_Type, Color_Space, and optional
1352rowBytes. Frees pixels, and returns true if successful.
1353
1354imageInfo.alphaType may be altered to a value permitted by imageInfo.colorSpace.
Cary Clark682c58d2018-05-16 07:07:07 -04001355If imageInfo.colorType is kUnknown_SkColorType, imageInfo.alphaType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04001356set to kUnknown_SkAlphaType.
1357If imageInfo.colorType is kAlpha_8_SkColorType and imageInfo.alphaType is
1358kUnpremul_SkAlphaType, imageInfo.alphaType is replaced by kPremul_SkAlphaType.
1359If imageInfo.colorType is kRGB_565_SkColorType or kGray_8_SkColorType,
1360imageInfo.alphaType is set to kOpaque_SkAlphaType.
1361If imageInfo.colorType is kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
1362kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: imageInfo.alphaType remains
1363unchanged.
1364
1365rowBytes must equal or exceed imageInfo.minRowBytes. If imageInfo.colorSpace is
1366kUnknown_SkColorType, rowBytes is ignored and treated as zero; for all other
1367Color_Space values, rowBytes of zero is treated as imageInfo.minRowBytes.
1368
1369Calls reset() and returns false if:
1370#List
1371# rowBytes exceeds 31 bits ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001372# imageInfo.width() is negative ##
1373# imageInfo.height() is negative ##
1374# rowBytes is positive and less than imageInfo.width() times imageInfo.bytesPerPixel ##
1375##
1376
1377#Param imageInfo contains width, height, Alpha_Type, Color_Type, Color_Space ##
1378#Param rowBytes imageInfo.minRowBytes or larger; or zero ##
1379
1380#Return true if Image_Info set successfully ##
1381
1382#Example
1383#Height 96
1384###^
Ben Wagner29380bd2017-10-09 14:43:00 -04001385SkBitmap bitmap;
1386bitmap.setInfo(SkImageInfo::MakeN32(44, 16, kOpaque_SkAlphaType));
1387bitmap.allocPixels();
1388bitmap.eraseColor(SK_ColorGREEN);
1389SkCanvas offscreen(bitmap);
1390SkPaint paint;
1391offscreen.drawString("!@#$%", 0, 12, paint);
1392canvas->scale(6, 6);
1393canvas->drawBitmap(bitmap, 0, 0);
1394^^^#
Cary Clarkbc5697d2017-10-04 14:31:33 -04001395##
1396
1397#SeeAlso Alpha_Type Color_Type Color_Space height rowBytes width
1398
1399##
1400
1401# ------------------------------------------------------------------------------
1402
1403#Enum AllocFlags
Cary Clark4855f782018-02-06 09:41:53 -05001404#Line # zero pixel memory ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001405#Code
1406 enum AllocFlags {
1407 kZeroPixels_AllocFlag = 1 << 0,
1408 };
1409##
1410
1411AllocFlags provides the option to zero pixel memory when allocated.
1412
1413#Const kZeroPixels_AllocFlag 1
Cary Clark682c58d2018-05-16 07:07:07 -04001414#Line # zero pixel memory ##
1415 Instructs tryAllocPixelsFlags and allocPixelsFlags to zero pixel memory.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001416##
1417
1418#NoExample
1419##
1420
1421#SeeAlso tryAllocPixelsFlags allocPixelsFlags erase() eraseColor
1422
1423##
1424
1425# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001426#Subtopic Allocate
Cary Clark78de7512018-02-07 07:27:09 -05001427#Line # allocates storage for pixels ##
1428##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001429
Cary Clark61313f32018-10-08 14:57:48 -04001430#Method bool tryAllocPixelsFlags(const SkImageInfo& info, uint32_t flags)
Cary Clark78de7512018-02-07 07:27:09 -05001431#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001432#Line # allocates pixels from Image_Info with options if possible ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001433Sets Image_Info to info following the rules in setInfo and allocates pixel
Cary Clark682c58d2018-05-16 07:07:07 -04001434memory. If flags is kZeroPixels_AllocFlag, memory is zeroed.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001435
1436Returns false and calls reset() if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001437not be allocated, or memory could not optionally be zeroed.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001438
1439On most platforms, allocating pixel memory may succeed even though there is
1440not sufficient memory to hold pixels; allocation does not take place
1441until the pixels are written to. The actual behavior depends on the platform
1442implementation of malloc(), if flags is zero, and calloc(), if flags is
1443kZeroPixels_AllocFlag.
1444
Cary Clark186d08f2018-04-03 08:43:27 -04001445flags set to kZeroPixels_AllocFlag offers equal or better performance than
1446subsequently calling eraseColor with SK_ColorTRANSPARENT.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001447
1448#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1449#Param flags kZeroPixels_AllocFlag, or zero ##
1450
1451#Return true if pixels allocation is successful ##
1452
1453#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001454 SkBitmap bitmap;
Cary Clark682c58d2018-05-16 07:07:07 -04001455 if (!bitmap.tryAllocPixelsFlags(SkImageInfo::MakeN32(10000, 10000, kOpaque_SkAlphaType),
Cary Clarka560c472017-11-27 10:44:06 -05001456 SkBitmap::kZeroPixels_AllocFlag)) {
1457 SkDebugf("bitmap allocation failed!\n");
1458 } else {
1459 SkDebugf("bitmap allocation succeeded!\n");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001460 }
1461#StdOut
Cary Clarka560c472017-11-27 10:44:06 -05001462bitmap allocation succeeded!
Cary Clarkbc5697d2017-10-04 14:31:33 -04001463##
1464##
1465
1466#SeeAlso allocPixelsFlags tryAllocPixels SkMallocPixelRef::MakeZeroed
1467
1468##
1469
1470# ------------------------------------------------------------------------------
1471
1472#Method void allocPixelsFlags(const SkImageInfo& info, uint32_t flags)
Cary Clark78de7512018-02-07 07:27:09 -05001473#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001474#Line # allocates pixels from Image_Info with options, or aborts ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001475Sets Image_Info to info following the rules in setInfo and allocates pixel
Cary Clark682c58d2018-05-16 07:07:07 -04001476memory. If flags is kZeroPixels_AllocFlag, memory is zeroed.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001477
1478Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001479not be allocated, or memory could not optionally
Cary Clarkbc5697d2017-10-04 14:31:33 -04001480be zeroed. Abort steps may be provided by the user at compile time by defining
1481SK_ABORT.
1482
1483On most platforms, allocating pixel memory may succeed even though there is
1484not sufficient memory to hold pixels; allocation does not take place
1485until the pixels are written to. The actual behavior depends on the platform
1486implementation of malloc(), if flags is zero, and calloc(), if flags is
1487kZeroPixels_AllocFlag.
1488
Cary Clark186d08f2018-04-03 08:43:27 -04001489flags set to kZeroPixels_AllocFlag offers equal or better performance than
1490subsequently calling eraseColor with SK_ColorTRANSPARENT.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001491
1492#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1493#Param flags kZeroPixels_AllocFlag, or zero ##
1494
1495#Example
1496#Height 128
1497#Description
1498Text is drawn on a transparent background; drawing the bitmap a second time
1499lets the first draw show through.
1500##
1501###^
Ben Wagner29380bd2017-10-09 14:43:00 -04001502SkBitmap bitmap;
Cary Clark682c58d2018-05-16 07:07:07 -04001503bitmap.allocPixelsFlags(SkImageInfo::MakeN32(44, 16, kPremul_SkAlphaType),
Ben Wagner29380bd2017-10-09 14:43:00 -04001504 SkBitmap::kZeroPixels_AllocFlag);
1505SkCanvas offscreen(bitmap);
1506SkPaint paint;
1507offscreen.drawString("!@#$%", 0, 12, paint);
1508canvas->scale(6, 6);
1509canvas->drawBitmap(bitmap, 0, 0);
1510canvas->drawBitmap(bitmap, 8, 8);
1511^^^#
Cary Clarkbc5697d2017-10-04 14:31:33 -04001512##
1513
1514#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeZeroed
1515
1516##
1517
1518# ------------------------------------------------------------------------------
1519
Cary Clark61313f32018-10-08 14:57:48 -04001520#Method bool tryAllocPixels(const SkImageInfo& info, size_t rowBytes)
Cary Clark78de7512018-02-07 07:27:09 -05001521#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001522#Line # allocates pixels from Image_Info if possible ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001523#ToDo am I ever conflicted about setInfo rules. It needs to be able to be replicated
1524 if, for instance, I generate one-page-per-method HTML-style documentation
1525 I'm not so sure it makes sense to put the indirection in for .h either unless
1526 my mantra is that .h should abbreviate full documentation. And, what to do
1527 for generated markdown? At least there the rules are a click away, although
1528 a pop-down in place would be way better. Hmmm.
1529##
1530
1531Sets Image_Info to info following the rules in setInfo and allocates pixel
1532memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(),
1533or equal zero. Pass in zero for rowBytes to compute the minimum valid value.
1534
1535Returns false and calls reset() if Image_Info could not be set, or memory could
1536not be allocated.
1537
1538On most platforms, allocating pixel memory may succeed even though there is
1539not sufficient memory to hold pixels; allocation does not take place
1540until the pixels are written to. The actual behavior depends on the platform
1541implementation of malloc().
1542
1543#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1544#Param rowBytes size of pixel row or larger; may be zero ##
1545
1546#Return true if pixel storage is allocated ##
1547
1548#Example
1549#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001550SkBitmap bitmap;
1551SkImageInfo info = SkImageInfo::Make(64, 256, kGray_8_SkColorType, kOpaque_SkAlphaType);
1552if (bitmap.tryAllocPixels(info, 0)) {
1553 SkCanvas offscreen(bitmap);
1554 offscreen.scale(.5f, .5f);
1555 for (int x : { 0, 64, 128, 192 } ) {
1556 offscreen.drawBitmap(source, -x, 0);
1557 canvas->drawBitmap(bitmap, x, 0);
1558 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001559}
1560##
1561
1562#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeAllocate
1563
1564##
1565
1566# ------------------------------------------------------------------------------
1567
1568#Method void allocPixels(const SkImageInfo& info, size_t rowBytes)
Cary Clark78de7512018-02-07 07:27:09 -05001569#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001570#Line # allocates pixels from Image_Info, or aborts ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001571Sets Image_Info to info following the rules in setInfo and allocates pixel
1572memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(),
1573or equal zero. Pass in zero for rowBytes to compute the minimum valid value.
1574
1575Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001576not be allocated. Abort steps may be provided by
Cary Clarkbc5697d2017-10-04 14:31:33 -04001577the user at compile time by defining SK_ABORT.
1578
1579On most platforms, allocating pixel memory may succeed even though there is
1580not sufficient memory to hold pixels; allocation does not take place
1581until the pixels are written to. The actual behavior depends on the platform
1582implementation of malloc().
1583
1584#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1585#Param rowBytes size of pixel row or larger; may be zero ##
1586
1587#Example
1588#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001589SkBitmap bitmap;
1590SkImageInfo info = SkImageInfo::Make(256, 64, kGray_8_SkColorType, kOpaque_SkAlphaType);
1591bitmap.allocPixels(info, info.width() * info.bytesPerPixel() + 64);
1592SkCanvas offscreen(bitmap);
1593offscreen.scale(.5f, .5f);
1594for (int y : { 0, 64, 128, 192 } ) {
1595 offscreen.drawBitmap(source, 0, -y);
1596 canvas->drawBitmap(bitmap, 0, y);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001597}
1598##
1599
1600#SeeAlso tryAllocPixels allocPixelsFlags SkMallocPixelRef::MakeAllocate
1601
1602##
1603
1604# ------------------------------------------------------------------------------
1605
Cary Clark61313f32018-10-08 14:57:48 -04001606#Method bool tryAllocPixels(const SkImageInfo& info)
Cary Clarkbc5697d2017-10-04 14:31:33 -04001607
1608Sets Image_Info to info following the rules in setInfo and allocates pixel
Cary Clark682c58d2018-05-16 07:07:07 -04001609memory.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001610
1611Returns false and calls reset() if Image_Info could not be set, or memory could
1612not be allocated.
1613
1614On most platforms, allocating pixel memory may succeed even though there is
1615not sufficient memory to hold pixels; allocation does not take place
1616until the pixels are written to. The actual behavior depends on the platform
1617implementation of malloc().
1618
1619#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1620
1621#Return true if pixel storage is allocated ##
1622
1623#Example
1624#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001625SkBitmap bitmap;
1626if (bitmap.tryAllocPixels(SkImageInfo::Make(64, 64, kGray_8_SkColorType, kOpaque_SkAlphaType))) {
1627 SkCanvas offscreen(bitmap);
1628 offscreen.scale(.25f, .5f);
1629 for (int y : { 0, 64, 128, 192 } ) {
1630 offscreen.drawBitmap(source, -y, -y);
1631 canvas->drawBitmap(bitmap, y, y);
1632 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001633}
1634##
1635
1636#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeAllocate
1637
1638##
1639
1640# ------------------------------------------------------------------------------
1641
1642#Method void allocPixels(const SkImageInfo& info)
1643
1644Sets Image_Info to info following the rules in setInfo and allocates pixel
Cary Clark682c58d2018-05-16 07:07:07 -04001645memory.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001646
1647Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001648not be allocated. Abort steps may be provided by
Cary Clarkbc5697d2017-10-04 14:31:33 -04001649the user at compile time by defining SK_ABORT.
1650
1651On most platforms, allocating pixel memory may succeed even though there is
1652not sufficient memory to hold pixels; allocation does not take place
1653until the pixels are written to. The actual behavior depends on the platform
1654implementation of malloc().
1655
1656#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1657
1658#Example
1659#Image 4
Ben Wagner29380bd2017-10-09 14:43:00 -04001660SkBitmap bitmap;
1661bitmap.allocPixels(SkImageInfo::Make(64, 64, kGray_8_SkColorType, kOpaque_SkAlphaType));
1662SkCanvas offscreen(bitmap);
1663offscreen.scale(.5f, .5f);
1664for (int y : { 0, 64, 128, 192 } ) {
1665 offscreen.drawBitmap(source, -y, -y);
1666 canvas->drawBitmap(bitmap, y, y);
1667}
Cary Clarkbc5697d2017-10-04 14:31:33 -04001668##
1669
1670#SeeAlso tryAllocPixels allocPixelsFlags SkMallocPixelRef::MakeAllocate
1671
1672##
1673
1674# ------------------------------------------------------------------------------
1675
Cary Clark61313f32018-10-08 14:57:48 -04001676#Method bool tryAllocN32Pixels(int width, int height, bool isOpaque = false)
Cary Clark78de7512018-02-07 07:27:09 -05001677#In Allocate
Cary Clarkffb3d682018-05-17 12:17:28 -04001678#Line # allocates compatible ARGB pixels if possible ##
Cary Clark682c58d2018-05-16 07:07:07 -04001679Sets Image_Info to width, height, and Native_Color_Type; and allocates
Cary Clarkbc5697d2017-10-04 14:31:33 -04001680pixel memory. If isOpaque is true, sets Image_Info to kOpaque_SkAlphaType;
1681otherwise, sets to kPremul_SkAlphaType.
1682
1683Calls reset() and returns false if width exceeds 29 bits or is negative,
1684or height is negative.
1685
1686Returns false if allocation fails.
1687
Cary Clarka560c472017-11-27 10:44:06 -05001688Use to create Bitmap that matches SkPMColor, the native pixel arrangement on
1689the platform. Bitmap drawn to output device skips converting its pixel format.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001690
1691#Param width pixel column count; must be zero or greater ##
1692#Param height pixel row count; must be zero or greater ##
1693#Param isOpaque true if pixels do not have transparency ##
1694
1695#Return true if pixel storage is allocated ##
1696
1697#Example
1698#Height 160
Ben Wagner29380bd2017-10-09 14:43:00 -04001699 SkBitmap bitmap;
1700 if (bitmap.tryAllocN32Pixels(80, 80)) {
1701 bitmap.eraseColor(SK_ColorTRANSPARENT);
1702 bitmap.erase(0x7f3f7fff, SkIRect::MakeWH(50, 30));
1703 bitmap.erase(0x3f7fff3f, SkIRect::MakeXYWH(20, 10, 50, 30));
1704 bitmap.erase(0x5fff3f7f, SkIRect::MakeXYWH(40, 20, 50, 30));
1705 canvas->drawBitmap(bitmap, 0, 0);
1706 for (int x : { 0, 30, 60, 90 } ) {
1707 canvas->drawBitmap(bitmap, x, 70);
1708 }
1709 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001710##
1711
1712#SeeAlso tryAllocPixels allocN32Pixels SkMallocPixelRef::MakeAllocate
1713
1714##
1715
1716# ------------------------------------------------------------------------------
1717
1718#Method void allocN32Pixels(int width, int height, bool isOpaque = false)
Cary Clark78de7512018-02-07 07:27:09 -05001719#In Allocate
Cary Clarkffb3d682018-05-17 12:17:28 -04001720#Line # allocates compatible ARGB pixels, or aborts ##
Cary Clark682c58d2018-05-16 07:07:07 -04001721Sets Image_Info to width, height, and the Native_Color_Type; and allocates
Cary Clarkbc5697d2017-10-04 14:31:33 -04001722pixel memory. If isOpaque is true, sets Image_Info to kPremul_SkAlphaType;
1723otherwise, sets to kOpaque_SkAlphaType.
1724
1725Aborts if width exceeds 29 bits or is negative, or height is negative, or
1726allocation fails. Abort steps may be provided by the user at compile time by
1727defining SK_ABORT.
1728
Cary Clarka560c472017-11-27 10:44:06 -05001729Use to create Bitmap that matches SkPMColor, the native pixel arrangement on
1730the platform. Bitmap drawn to output device skips converting its pixel format.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001731
1732#Param width pixel column count; must be zero or greater ##
1733#Param height pixel row count; must be zero or greater ##
1734#Param isOpaque true if pixels do not have transparency ##
1735
1736#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001737 SkRandom random;
1738 SkBitmap bitmap;
1739 bitmap.allocN32Pixels(64, 64);
1740 bitmap.eraseColor(SK_ColorTRANSPARENT);
1741 for (int y = 0; y < 256; y += 64) {
1742 for (int x = 0; x < 256; x += 64) {
1743 SkColor color = random.nextU();
1744 uint32_t w = random.nextRangeU(4, 32);
1745 uint32_t cx = random.nextRangeU(0, 64 - w);
1746 uint32_t h = random.nextRangeU(4, 32);
1747 uint32_t cy = random.nextRangeU(0, 64 - h);
1748 bitmap.erase(color, SkIRect::MakeXYWH(cx, cy, w, h));
1749 canvas->drawBitmap(bitmap, x, y);
1750 }
1751 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001752##
1753
1754#SeeAlso allocPixels tryAllocN32Pixels SkMallocPixelRef::MakeAllocate
1755
1756##
1757
1758# ------------------------------------------------------------------------------
1759
1760#Method bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
1761 void (*releaseProc)(void* addr, void* context), void* context)
Cary Clark78de7512018-02-07 07:27:09 -05001762#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001763#Line # creates Pixel_Ref, with optional release function ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001764
1765Sets Image_Info to info following the rules in setInfo, and creates Pixel_Ref
Cary Clark682c58d2018-05-16 07:07:07 -04001766containing pixels and rowBytes. releaseProc, if not nullptr, is called
Cary Clarkbc5697d2017-10-04 14:31:33 -04001767immediately on failure or when pixels are no longer referenced. context may be
1768nullptr.
1769
1770If Image_Info could not be set, or rowBytes is less than info.minRowBytes:
1771calls releaseProc if present, calls reset(), and returns false.
1772
1773Otherwise, if pixels equals nullptr: sets Image_Info, calls releaseProc if
1774present, returns true.
1775
1776If Image_Info is set, pixels is not nullptr, and releaseProc is not nullptr:
1777when pixels are no longer referenced, calls releaseProc with pixels and context
1778as parameters.
1779
1780#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1781#Param pixels address or pixel storage; may be nullptr ##
1782#Param rowBytes size of pixel row or larger ##
1783#Param releaseProc function called when pixels can be deleted; may be nullptr ##
1784#Param context caller state passed to releaseProc; may be nullptr ##
1785
1786#Return true if Image_Info is set to info ##
1787
1788#Example
1789#Description
1790releaseProc is called immediately because rowBytes is too small for Pixel_Ref.
1791##
1792#Function
Ben Wagner29380bd2017-10-09 14:43:00 -04001793static void releaseProc(void* addr, void* ) {
1794 SkDebugf("releaseProc called\n");
Cary Clark682c58d2018-05-16 07:07:07 -04001795 delete[] (uint32_t*) addr;
Ben Wagner29380bd2017-10-09 14:43:00 -04001796}
1797
1798##
1799
1800void draw(SkCanvas* canvas) {
1801 SkBitmap bitmap;
1802 void* pixels = new uint32_t[8 * 8];
1803 SkImageInfo info = SkImageInfo::MakeN32(8, 8, kOpaque_SkAlphaType);
1804 SkDebugf("before installPixels\n");
1805 bool installed = bitmap.installPixels(info, pixels, 16, releaseProc, nullptr);
1806 SkDebugf("install " "%s" "successful\n", installed ? "" : "not ");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001807}
1808#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001809before installPixels
1810releaseProc called
Cary Clarkbc5697d2017-10-04 14:31:33 -04001811install not successful
1812##
1813##
1814
1815#SeeAlso allocPixels
1816
1817##
1818
1819# ------------------------------------------------------------------------------
1820
1821#Method bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes)
1822
1823Sets Image_Info to info following the rules in setInfo, and creates Pixel_Ref
1824containing pixels and rowBytes.
1825
1826If Image_Info could not be set, or rowBytes is less than info.minRowBytes:
1827calls reset(), and returns false.
1828
1829Otherwise, if pixels equals nullptr: sets Image_Info, returns true.
1830
1831Caller must ensure that pixels are valid for the lifetime of Bitmap and Pixel_Ref.
1832
1833#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1834#Param pixels address or pixel storage; may be nullptr ##
1835#Param rowBytes size of pixel row or larger ##
1836
1837#Return true if Image_Info is set to info ##
1838
1839#Example
Cary Clark4855f782018-02-06 09:41:53 -05001840#Bug 7079
Cary Clarkbc5697d2017-10-04 14:31:33 -04001841#Description
Cary Clarkbc5697d2017-10-04 14:31:33 -04001842GPU does not support kUnpremul_SkAlphaType, does not assert that it does not.
1843##
Ben Wagner29380bd2017-10-09 14:43:00 -04001844void draw(SkCanvas* canvas) {
1845 SkRandom random;
1846 SkBitmap bitmap;
1847 const int width = 8;
1848 const int height = 8;
1849 uint32_t pixels[width * height];
1850 for (unsigned x = 0; x < width * height; ++x) {
1851 pixels[x] = random.nextU();
1852 }
1853 SkImageInfo info = SkImageInfo::MakeN32(width, height, kUnpremul_SkAlphaType);
1854 if (bitmap.installPixels(info, pixels, info.minRowBytes())) {
1855 canvas->scale(32, 32);
1856 canvas->drawBitmap(bitmap, 0, 0);
1857 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001858}
1859##
1860
1861#SeeAlso allocPixels
1862
1863##
1864
1865# ------------------------------------------------------------------------------
1866
1867#Method bool installPixels(const SkPixmap& pixmap)
1868
1869Sets Image_Info to pixmap.info() following the rules in setInfo, and creates
1870Pixel_Ref containing pixmap.addr() and pixmap.rowBytes.
1871
1872If Image_Info could not be set, or pixmap.rowBytes is less than
1873SkImageInfo::minRowBytes: calls reset(), and returns false.
1874
1875Otherwise, if pixmap.addr() equals nullptr: sets Image_Info, returns true.
1876
1877Caller must ensure that pixmap is valid for the lifetime of Bitmap and Pixel_Ref.
1878
1879#Param pixmap Image_Info, pixel address, and rowBytes ##
1880
1881#Return true if Image_Info was set to pixmap.info() ##
1882
1883#Example
1884#Description
1885Draw a five by five bitmap, and draw it again with a center white pixel.
1886##
1887#Height 64
1888 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
1889 { 0xAC, 0xA8, 0x89, 0x47, 0x87 },
1890 { 0x4B, 0x25, 0x25, 0x25, 0x46 },
1891 { 0x90, 0x81, 0x25, 0x41, 0x33 },
1892 { 0x75, 0x55, 0x44, 0x20, 0x00 }};
1893 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
1894 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
1895 SkBitmap bitmap;
1896 bitmap.installPixels(pixmap);
1897 canvas->scale(10, 10);
1898 canvas->drawBitmap(bitmap, 0, 0);
1899 *pixmap.writable_addr8(2, 2) = 0xFF;
1900 bitmap.installPixels(pixmap);
1901 canvas->drawBitmap(bitmap, 10, 0);
1902##
1903
1904#SeeAlso allocPixels
1905
1906##
1907
1908# ------------------------------------------------------------------------------
1909
1910#Method bool installMaskPixels(const SkMask& mask)
Cary Clark4855f782018-02-06 09:41:53 -05001911#Deprecated soon
Cary Clarkbc5697d2017-10-04 14:31:33 -04001912##
1913
1914# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001915#Subtopic Pixels
Cary Clark78de7512018-02-07 07:27:09 -05001916#Line # read and write pixel values ##
1917##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001918
1919#Method void setPixels(void* pixels)
Cary Clark78de7512018-02-07 07:27:09 -05001920#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05001921#Line # sets Pixel_Ref without an offset ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001922Replaces Pixel_Ref with pixels, preserving Image_Info and rowBytes.
1923Sets Pixel_Ref origin to (0, 0).
1924
1925If pixels is nullptr, or if info().colorType equals kUnknown_SkColorType;
1926release reference to Pixel_Ref, and set Pixel_Ref to nullptr.
1927
1928Caller is responsible for handling ownership pixel memory for the lifetime
1929of Bitmap and Pixel_Ref.
1930
1931#Param pixels address of pixel storage, managed by caller ##
1932
1933#Example
1934#Height 50
Ben Wagner29380bd2017-10-09 14:43:00 -04001935 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
1936 uint8_t set2[5] = { 0xAC, 0xA8, 0x89, 0x47, 0x87 };
1937 SkBitmap bitmap;
1938 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
1939 canvas->scale(10, 50);
1940 canvas->drawBitmap(bitmap, 0, 0);
1941 bitmap.setPixels(set2);
1942 canvas->drawBitmap(bitmap, 10, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001943##
1944
1945#SeeAlso installPixels allocPixels
1946
1947##
1948
1949# ------------------------------------------------------------------------------
1950
Cary Clark61313f32018-10-08 14:57:48 -04001951#Method bool tryAllocPixels()
Cary Clark78de7512018-02-07 07:27:09 -05001952#In Allocate
Cary Clarkbc5697d2017-10-04 14:31:33 -04001953Allocates pixel memory with HeapAllocator, and replaces existing Pixel_Ref.
1954The allocation size is determined by Image_Info width, height, and Color_Type.
1955
Cary Clarka560c472017-11-27 10:44:06 -05001956Returns false if info().colorType is kUnknown_SkColorType, or allocation fails.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001957
1958#Return true if the allocation succeeds
1959##
1960
1961#Example
1962#Height 50
Cary Clark682c58d2018-05-16 07:07:07 -04001963#Description
Cary Clarkbc5697d2017-10-04 14:31:33 -04001964Bitmap hosts and draws gray values in set1. tryAllocPixels replaces Pixel_Ref
1965and erases it to black, but does not alter set1. setPixels replaces black
Cary Clark682c58d2018-05-16 07:07:07 -04001966Pixel_Ref with set1.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001967##
Ben Wagner29380bd2017-10-09 14:43:00 -04001968 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
1969 SkBitmap bitmap;
1970 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
1971 canvas->scale(10, 50);
1972 canvas->drawBitmap(bitmap, 0, 0);
1973 if (bitmap.tryAllocPixels()) {
1974 bitmap.eraseColor(SK_ColorBLACK);
1975 canvas->drawBitmap(bitmap, 8, 0);
1976 bitmap.setPixels(set1);
1977 canvas->drawBitmap(bitmap, 16, 0);
1978 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001979##
1980
1981#SeeAlso allocPixels installPixels setPixels
1982
1983##
1984
1985# ------------------------------------------------------------------------------
1986
1987#Method void allocPixels()
Cary Clark78de7512018-02-07 07:27:09 -05001988#In Allocate
Cary Clarkbc5697d2017-10-04 14:31:33 -04001989Allocates pixel memory with HeapAllocator, and replaces existing Pixel_Ref.
1990The allocation size is determined by Image_Info width, height, and Color_Type.
1991
Cary Clarka560c472017-11-27 10:44:06 -05001992Aborts if info().colorType is kUnknown_SkColorType, or allocation fails.
1993Abort steps may be provided by the user at compile
Cary Clarkbc5697d2017-10-04 14:31:33 -04001994time by defining SK_ABORT.
1995
1996#Example
1997#Height 50
Cary Clark682c58d2018-05-16 07:07:07 -04001998#Description
Cary Clarkbc5697d2017-10-04 14:31:33 -04001999Bitmap hosts and draws gray values in set1. allocPixels replaces Pixel_Ref
2000and erases it to black, but does not alter set1. setPixels replaces black
Cary Clark682c58d2018-05-16 07:07:07 -04002001Pixel_Ref with set2.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002002##
Ben Wagner29380bd2017-10-09 14:43:00 -04002003 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
2004 uint8_t set2[5] = { 0xAC, 0xA8, 0x89, 0x47, 0x87 };
2005 SkBitmap bitmap;
2006 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
2007 canvas->scale(10, 50);
2008 canvas->drawBitmap(bitmap, 0, 0);
2009 bitmap.allocPixels();
2010 bitmap.eraseColor(SK_ColorBLACK);
2011 canvas->drawBitmap(bitmap, 8, 0);
2012 bitmap.setPixels(set2);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002013 canvas->drawBitmap(bitmap, 16, 0);
2014##
2015
2016#SeeAlso tryAllocPixels installPixels setPixels
2017
2018##
2019
2020# ------------------------------------------------------------------------------
2021
Cary Clark61313f32018-10-08 14:57:48 -04002022#Method bool tryAllocPixels(Allocator* allocator)
Cary Clarkbc5697d2017-10-04 14:31:33 -04002023
2024Allocates pixel memory with allocator, and replaces existing Pixel_Ref.
2025The allocation size is determined by Image_Info width, height, and Color_Type.
2026If allocator is nullptr, use HeapAllocator instead.
2027
Cary Clark78de7512018-02-07 07:27:09 -05002028Returns false if Allocator::allocPixelRef return false.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002029
2030#Param allocator instance of SkBitmap::Allocator instantiation ##
2031
2032#Return true if custom allocator reports success
2033##
2034
2035#Example
2036#Height 100
2037#Description
2038HeapAllocator limits the maximum size of Bitmap to two gigabytes. Using
2039a custom allocator, this limitation may be relaxed. This example can be
Cary Clark682c58d2018-05-16 07:07:07 -04002040modified to allocate an eight gigabyte Bitmap on a 64-bit platform with
Cary Clarkbc5697d2017-10-04 14:31:33 -04002041sufficient memory.
2042##
2043#Function
2044class LargePixelRef : public SkPixelRef {
2045public:
2046 LargePixelRef(const SkImageInfo& info, char* storage, size_t rowBytes)
2047 : SkPixelRef(info.width(), info.height(), storage, rowBytes) {
2048 }
2049
2050 ~LargePixelRef() override {
2051 delete[] (char* ) this->pixels();
2052 }
2053};
2054
2055class LargeAllocator : public SkBitmap::Allocator {
2056public:
2057 bool allocPixelRef(SkBitmap* bitmap) override {
2058 const SkImageInfo& info = bitmap->info();
2059 uint64_t rowBytes = info.minRowBytes64();
2060 uint64_t size = info.height() * rowBytes;
2061 char* addr = new char[size];
2062 if (nullptr == addr) {
2063 return false;
2064 }
2065 sk_sp<SkPixelRef> pr = sk_sp<SkPixelRef>(new LargePixelRef(info, addr, rowBytes));
2066 if (!pr) {
2067 return false;
2068 }
2069 bitmap->setPixelRef(std::move(pr), 0, 0);
2070 return true;
2071 }
2072};
2073
2074##
2075
2076void draw(SkCanvas* canvas) {
2077 LargeAllocator largeAllocator;
2078 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002079 int width = 100; // make this 20000
2080 int height = 100; // and this 100000 to allocate 8 gigs on a 64-bit platform
2081 bitmap.setInfo(SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType));
2082 if (bitmap.tryAllocPixels(&largeAllocator)) {
2083 bitmap.eraseColor(0xff55aa33);
2084 canvas->drawBitmap(bitmap, 0, 0);
2085 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002086}
2087
2088##
2089
2090#SeeAlso allocPixels Allocator Pixel_Ref
2091
2092##
2093
2094# ------------------------------------------------------------------------------
2095
2096#Method void allocPixels(Allocator* allocator)
2097
2098Allocates pixel memory with allocator, and replaces existing Pixel_Ref.
2099The allocation size is determined by Image_Info width, height, and Color_Type.
2100If allocator is nullptr, use HeapAllocator instead.
2101
Cary Clark78de7512018-02-07 07:27:09 -05002102Aborts if Allocator::allocPixelRef return false. Abort steps may be provided by
Cary Clarkbc5697d2017-10-04 14:31:33 -04002103the user at compile time by defining SK_ABORT.
2104
2105#Param allocator instance of SkBitmap::Allocator instantiation ##
2106
2107#Example
2108#Height 32
2109#Function
2110class TinyAllocator : public SkBitmap::Allocator {
2111public:
2112 bool allocPixelRef(SkBitmap* bitmap) override {
2113 const SkImageInfo& info = bitmap->info();
2114 if (info.height() * info.minRowBytes() > sizeof(storage)) {
2115 return false;
2116 }
2117 sk_sp<SkPixelRef> pr = sk_sp<SkPixelRef>(
2118 new SkPixelRef(info.width(), info.height(), storage, info.minRowBytes()));
2119 bitmap->setPixelRef(std::move(pr), 0, 0);
2120 return true;
2121 }
2122
2123 char storage[16];
2124};
2125
2126##
2127
2128void draw(SkCanvas* canvas) {
2129 TinyAllocator tinyAllocator;
2130 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002131 bitmap.setInfo(SkImageInfo::MakeN32(2, 2, kOpaque_SkAlphaType));
2132 if (bitmap.tryAllocPixels(&tinyAllocator)) {
2133 bitmap.eraseColor(0xff55aa33);
2134 bitmap.erase(0xffaa3355, SkIRect::MakeXYWH(1, 1, 1, 1));
2135 canvas->scale(16, 16);
2136 canvas->drawBitmap(bitmap, 0, 0);
2137 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002138}
2139##
2140
2141#SeeAlso allocPixels Allocator Pixel_Ref
2142
2143##
2144
2145# ------------------------------------------------------------------------------
2146
2147#Method SkPixelRef* pixelRef() const
Cary Clark78de7512018-02-07 07:27:09 -05002148#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002149#Line # returns Pixel_Ref, or nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002150Returns Pixel_Ref, which contains: pixel base address; its dimensions; and
2151rowBytes, the interval from one row to the next. Does not change Pixel_Ref
2152reference count. Pixel_Ref may be shared by multiple bitmaps.
2153If Pixel_Ref has not been set, returns nullptr.
2154
2155#Return Pixel_Ref, or nullptr ##
2156
2157#Example
2158#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002159 SkBitmap subset;
2160 source.extractSubset(&subset, SkIRect::MakeXYWH(32, 64, 128, 256));
2161 SkDebugf("src ref %c= sub ref\n", source.pixelRef() == subset.pixelRef() ? '=' : '!');
2162 SkDebugf("src pixels %c= sub pixels\n", source.getPixels() == subset.getPixels() ? '=' : '!');
2163 SkDebugf("src addr %c= sub addr\n", source.getAddr(32, 64) == subset.getAddr(0, 0) ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -04002164##
2165
2166#SeeAlso getPixels getAddr
2167
2168##
2169
2170# ------------------------------------------------------------------------------
2171
2172#Method SkIPoint pixelRefOrigin() const
Cary Clark78de7512018-02-07 07:27:09 -05002173#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002174#Line # returns offset within Pixel_Ref ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002175Returns origin of pixels within Pixel_Ref. Bitmap bounds is always contained
2176by Pixel_Ref bounds, which may be the same size or larger. Multiple Bitmaps
2177can share the same Pixel_Ref, where each Bitmap has different bounds.
2178
2179The returned origin added to Bitmap dimensions equals or is smaller than the
2180Pixel_Ref dimensions.
2181
2182Returns (0, 0) if Pixel_Ref is nullptr.
2183
2184#Return pixel origin within Pixel_Ref ##
2185
2186#Example
2187#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002188 SkBitmap subset;
2189 source.extractSubset(&subset, SkIRect::MakeXYWH(32, 64, 128, 256));
2190 SkIPoint sourceOrigin = source.pixelRefOrigin();
2191 SkIPoint subsetOrigin = subset.pixelRefOrigin();
2192 SkDebugf("source origin: %d, %d\n", sourceOrigin.fX, sourceOrigin.fY);
2193 SkDebugf("subset origin: %d, %d\n", subsetOrigin.fX, subsetOrigin.fY);
2194#StdOut
2195source origin: 0, 0
2196subset origin: 32, 64
2197##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002198##
2199
Cary Clark2ade9972017-11-02 17:49:34 -04002200#SeeAlso SkPixelRef getSubset setPixelRef
Cary Clarkbc5697d2017-10-04 14:31:33 -04002201
2202##
2203
2204# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05002205#Subtopic Set
2206#Line # updates values and attributes ##
Cary Clark78de7512018-02-07 07:27:09 -05002207##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002208
2209#Method void setPixelRef(sk_sp<SkPixelRef> pixelRef, int dx, int dy)
Cary Clark78de7512018-02-07 07:27:09 -05002210#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05002211#Line # sets Pixel_Ref and offset ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002212Replaces pixelRef and origin in Bitmap. dx and dy specify the offset
2213within the Pixel_Ref pixels for the top-left corner of the bitmap.
2214
2215Asserts in debug builds if dx or dy are out of range. Pins dx and dy
2216to legal range in release builds.
2217
2218The caller is responsible for ensuring that the pixels match the
2219Color_Type and Alpha_Type in Image_Info.
2220
2221#Param pixelRef Pixel_Ref describing pixel address and rowBytes ##
2222#Param dx column offset in Pixel_Ref for bitmap origin ##
2223#Param dy row offset in Pixel_Ref for bitmap origin ##
2224
2225#Example
2226#Height 140
2227#Image 5
2228#Description
Cary Clark682c58d2018-05-16 07:07:07 -04002229Treating 32-bit data as 8-bit data is unlikely to produce useful results.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002230##
Ben Wagner29380bd2017-10-09 14:43:00 -04002231 SkBitmap bitmap;
Cary Clark682c58d2018-05-16 07:07:07 -04002232 bitmap.setInfo(SkImageInfo::Make(source.width() - 5, source.height() - 5,
Ben Wagner29380bd2017-10-09 14:43:00 -04002233 kGray_8_SkColorType, kOpaque_SkAlphaType), source.rowBytes());
2234 bitmap.setPixelRef(sk_ref_sp(source.pixelRef()), 5, 5);
2235 canvas->drawBitmap(bitmap, 10, 10);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002236##
2237
2238#SeeAlso setInfo
2239
2240##
2241
2242# ------------------------------------------------------------------------------
2243
2244#Method bool readyToDraw() const
Cary Clark78de7512018-02-07 07:27:09 -05002245#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05002246#Line # returns true if address of pixels is not nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002247Returns true if Bitmap is can be drawn.
2248
2249#Return true if getPixels() is not nullptr ##
2250
2251#Example
2252#Image 5
2253#Height 160
Ben Wagner29380bd2017-10-09 14:43:00 -04002254 if (source.readyToDraw()) {
2255 canvas->drawBitmap(source, 10, 10);
2256 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002257##
2258
2259#SeeAlso getPixels drawsNothing
2260
2261##
2262
2263# ------------------------------------------------------------------------------
2264
2265#Method uint32_t getGenerationID() const
Cary Clark78de7512018-02-07 07:27:09 -05002266#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05002267#Line # returns unique ID ##
Cary Clark682c58d2018-05-16 07:07:07 -04002268Returns a unique value corresponding to the pixels in Pixel_Ref.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002269Returns a different value after notifyPixelsChanged has been called.
2270Returns zero if Pixel_Ref is nullptr.
2271
2272Determines if pixels have changed since last examined.
2273
2274#Return unique value for pixels in Pixel_Ref ##
2275
2276#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04002277 SkBitmap bitmap;
2278 SkDebugf("empty id %u\n", bitmap.getGenerationID());
2279 bitmap.allocPixels(SkImageInfo::MakeN32(64, 64, kOpaque_SkAlphaType));
2280 SkDebugf("alloc id %u\n", bitmap.getGenerationID());
2281 bitmap.eraseColor(SK_ColorRED);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002282 SkDebugf("erase id %u\n", bitmap.getGenerationID());
2283#StdOut
2284#Volatile
Ben Wagner29380bd2017-10-09 14:43:00 -04002285empty id 0
2286alloc id 4
Cary Clarkbc5697d2017-10-04 14:31:33 -04002287erase id 6
Cary Clark682c58d2018-05-16 07:07:07 -04002288##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002289##
2290
2291#SeeAlso notifyPixelsChanged Pixel_Ref
2292
2293##
2294
2295# ------------------------------------------------------------------------------
2296
2297#Method void notifyPixelsChanged() const
Cary Clark78de7512018-02-07 07:27:09 -05002298#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05002299#Line # marks pixels as changed, altering the unique ID ##
Cary Clark682c58d2018-05-16 07:07:07 -04002300Marks that pixels in Pixel_Ref have changed. Subsequent calls to
Cary Clarkbc5697d2017-10-04 14:31:33 -04002301getGenerationID() return a different value.
2302
2303#Example
2304#Height 20
Cary Clark682c58d2018-05-16 07:07:07 -04002305 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002306 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
2307 bitmap.allocPixels();
2308 bitmap.eraseColor(SK_ColorRED);
2309 canvas->scale(16, 16);
2310 canvas->drawBitmap(bitmap, 0, 0);
2311 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorBLUE);
2312 canvas->drawBitmap(bitmap, 2, 0);
2313 bitmap.notifyPixelsChanged();
2314 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorGREEN);
2315 canvas->drawBitmap(bitmap, 4, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002316##
2317
2318#SeeAlso getGenerationID isVolatile Pixel_Ref
2319
2320##
2321
2322# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05002323#Subtopic Draw
Cary Clark682c58d2018-05-16 07:07:07 -04002324#Line # sets pixels to Color ##
Cary Clark78de7512018-02-07 07:27:09 -05002325##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002326
2327#Method void eraseColor(SkColor c) const
Cary Clark78de7512018-02-07 07:27:09 -05002328#In Draw
Cary Clarkab2621d2018-01-30 10:08:57 -05002329#Line # writes Color to pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002330Replaces pixel values with c. All pixels contained by bounds() are affected.
2331If the colorType is kGray_8_SkColorType or k565_SkColorType, then Color_Alpha
Cary Clarkffb3d682018-05-17 12:17:28 -04002332is ignored; RGB is treated as opaque. If colorType is kAlpha_8_SkColorType,
2333then RGB is ignored.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002334
2335#Param c Unpremultiplied Color ##
2336
2337#Example
2338#Height 20
Cary Clark682c58d2018-05-16 07:07:07 -04002339 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002340 bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kOpaque_SkAlphaType));
2341 bitmap.eraseColor(SK_ColorRED);
2342 canvas->scale(16, 16);
2343 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002344##
2345
Cary Clark154beea2017-10-26 07:58:48 -04002346#SeeAlso eraseARGB erase
Cary Clarkbc5697d2017-10-04 14:31:33 -04002347
2348##
2349
2350# ------------------------------------------------------------------------------
2351
2352#Method void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const
Cary Clark78de7512018-02-07 07:27:09 -05002353#In Draw
Cary Clarkab2621d2018-01-30 10:08:57 -05002354#Line # writes Color to pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002355Replaces pixel values with Unpremultiplied Color built from a, r, g, and b.
2356All pixels contained by bounds() are affected.
2357If the colorType is kGray_8_SkColorType or k565_SkColorType, then a
2358is ignored; r, g, and b are treated as opaque. If colorType is kAlpha_8_SkColorType,
2359then r, g, and b are ignored.
2360
2361#Param a amount of Color_Alpha, from fully transparent (0) to fully opaque (255) ##
Cary Clarkffb3d682018-05-17 12:17:28 -04002362#Param r amount of red, from no red (0) to full red (255) ##
2363#Param g amount of green, from no green (0) to full green (255) ##
2364#Param b amount of blue, from no blue (0) to full blue (255) ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002365
2366#Example
2367#Height 80
Cary Clark682c58d2018-05-16 07:07:07 -04002368 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002369 bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType));
2370 bitmap.eraseARGB(0x7f, 0xff, 0x7f, 0x3f);
2371 canvas->scale(50, 50);
2372 canvas->drawBitmap(bitmap, 0, 0);
2373 canvas->drawBitmap(bitmap, .5f, .5f);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002374##
2375
Cary Clark154beea2017-10-26 07:58:48 -04002376#SeeAlso eraseColor erase
Cary Clarkbc5697d2017-10-04 14:31:33 -04002377
2378##
2379
2380# ------------------------------------------------------------------------------
2381
Cary Clarkbc5697d2017-10-04 14:31:33 -04002382#Method void erase(SkColor c, const SkIRect& area) const
Cary Clark78de7512018-02-07 07:27:09 -05002383#In Draw
Cary Clarkab2621d2018-01-30 10:08:57 -05002384#Line # writes Color to rectangle of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002385Replaces pixel values inside area with c. If area does not intersect bounds(),
2386call has no effect.
2387
2388If the colorType is kGray_8_SkColorType or k565_SkColorType, then Color_Alpha
Cary Clarkffb3d682018-05-17 12:17:28 -04002389is ignored; RGB is treated as opaque. If colorType is kAlpha_8_SkColorType,
2390then RGB is ignored.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002391
2392#Param c Unpremultiplied Color ##
2393#Param area rectangle to fill ##
2394
2395#Example
2396#Height 70
Cary Clark682c58d2018-05-16 07:07:07 -04002397 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002398 bitmap.allocPixels(SkImageInfo::MakeN32(2, 2, kPremul_SkAlphaType));
2399 bitmap.erase(0x7fff7f3f, SkIRect::MakeWH(1, 1));
2400 bitmap.erase(0x7f7f3fff, SkIRect::MakeXYWH(0, 1, 1, 1));
2401 bitmap.erase(0x7f3fff7f, SkIRect::MakeXYWH(1, 0, 1, 1));
2402 bitmap.erase(0x7f1fbf5f, SkIRect::MakeXYWH(1, 1, 1, 1));
2403 canvas->scale(25, 25);
2404 canvas->drawBitmap(bitmap, 0, 0);
2405 canvas->drawBitmap(bitmap, .5f, .5f);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002406
2407##
2408
Mike Kleinfe5ad672018-10-04 10:18:13 -04002409#SeeAlso eraseColor eraseARGB SkCanvas::drawRect
Cary Clarkbc5697d2017-10-04 14:31:33 -04002410
2411##
2412
2413# ------------------------------------------------------------------------------
2414
2415#Method void eraseArea(const SkIRect& area, SkColor c) const
Cary Clark682c58d2018-05-16 07:07:07 -04002416#Deprecated
Cary Clarkbc5697d2017-10-04 14:31:33 -04002417##
2418
Cary Clarkbc5697d2017-10-04 14:31:33 -04002419# ------------------------------------------------------------------------------
2420
2421#Method SkColor getColor(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002422#In Property
2423#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05002424#Line # returns one pixel as Unpremultiplied Color ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002425Returns pixel at (x, y) as Unpremultiplied Color.
2426Returns black with Alpha if Color_Type is kAlpha_8_SkColorType.
2427
2428Input is not validated: out of bounds values of x or y trigger an assert() if
2429built with SK_DEBUG defined; and returns undefined values or may crash if
2430SK_RELEASE is defined. Fails if Color_Type is kUnknown_SkColorType or
2431pixel address is nullptr.
2432
2433Color_Space in Image_Info is ignored. Some Color precision may be lost in the
Cary Clark682c58d2018-05-16 07:07:07 -04002434conversion to Unpremultiplied Color; original pixel data may have additional
Cary Clarkbc5697d2017-10-04 14:31:33 -04002435precision.
2436
2437#Param x column index, zero or greater, and less than width() ##
2438#Param y row index, zero or greater, and less than height() ##
2439
2440#Return pixel converted to Unpremultiplied Color ##
2441
2442#Example
2443 const int w = 4;
2444 const int h = 4;
2445 SkColor colors[][w] = {
Cary Clark75fd4492018-06-20 12:45:16 -04002446 { 0x00000000, 0x2a0e002a, 0x55380055, 0x7f7f007f },
2447 { 0x2a000e2a, 0x551c1c55, 0x7f542a7f, 0xaaaa38aa },
2448 { 0x55003855, 0x7f2a547f, 0xaa7171aa, 0xd4d48dd4 },
2449 { 0x7f007f7f, 0xaa38aaaa, 0xd48dd4d4, 0xffffffff }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002450 };
2451 SkDebugf("Premultiplied:\n");
2452 for (int y = 0; y < h; ++y) {
2453 SkDebugf("(0, %d) ", y);
2454 for (int x = 0; x < w; ++x) {
2455 SkDebugf("0x%08x%c", colors[y][x], x == w - 1 ? '\n' : ' ');
2456 }
2457 }
2458 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), colors, w * 4);
2459 SkBitmap bitmap;
2460 bitmap.installPixels(pixmap);
2461 SkDebugf("Unpremultiplied:\n");
2462 for (int y = 0; y < h; ++y) {
2463 SkDebugf("(0, %d) ", y);
2464 for (int x = 0; x < w; ++x) {
2465 SkDebugf("0x%08x%c", bitmap.getColor(x, y), x == w - 1 ? '\n' : ' ');
2466 }
2467 }
2468#StdOut
2469Premultiplied:
Cary Clark682c58d2018-05-16 07:07:07 -04002470(0, 0) 0x00000000 0x2a0e002a 0x55380055 0x7f7f007f
2471(0, 1) 0x2a000e2a 0x551c1c55 0x7f542a7f 0xaaaa38aa
2472(0, 2) 0x55003855 0x7f2a547f 0xaa7171aa 0xd4d48dd4
2473(0, 3) 0x7f007f7f 0xaa38aaaa 0xd48dd4d4 0xffffffff
Cary Clarkbc5697d2017-10-04 14:31:33 -04002474Unpremultiplied:
Cary Clark682c58d2018-05-16 07:07:07 -04002475(0, 0) 0x00000000 0x2a5500ff 0x55a800ff 0x7fff00ff
2476(0, 1) 0x2a0055ff 0x555454ff 0x7fa954ff 0xaaff54ff
2477(0, 2) 0x5500a8ff 0x7f54a9ff 0xaaaaaaff 0xd4ffaaff
2478(0, 3) 0x7f00ffff 0xaa54ffff 0xd4aaffff 0xffffffff
Cary Clarkbc5697d2017-10-04 14:31:33 -04002479##
2480##
2481
Cary Clark8fe29402018-09-20 17:31:43 -04002482#SeeAlso getAlphaf getAddr readPixels
Cary Clarkbc5697d2017-10-04 14:31:33 -04002483
2484##
2485
Cary Clark8fe29402018-09-20 17:31:43 -04002486#Method float getAlphaf(int x, int y) const
2487#In Property
2488#Line # returns Alpha normalized from zero to one ##
2489
2490Looks up the pixel at (x,y) and return its alpha component, normalized to [0..1].
2491This is roughly equivalent to #Formula # SkGetColorA(getColor()) ##, but can be more efficent
2492(and more precise if the pixels store more than 8 bits per component).
2493
2494#Param x column index, zero or greater, and less than width() ##
2495#Param y row index, zero or greater, and less than height() ##
2496
2497#Return alpha converted to normalized float ##
2498
2499#NoExample
2500##
2501
2502#SeeAlso getColor
2503
2504##
2505
2506
Cary Clarkbc5697d2017-10-04 14:31:33 -04002507# ------------------------------------------------------------------------------
2508
2509#Method void* getAddr(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002510#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002511#Line # returns readable pixel address as void pointer ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002512Returns pixel address at (x, y).
2513
2514Input is not validated: out of bounds values of x or y, or kUnknown_SkColorType,
2515trigger an assert() if built with SK_DEBUG defined. Returns nullptr if
2516Color_Type is kUnknown_SkColorType, or Pixel_Ref is nullptr.
2517
Cary Clark682c58d2018-05-16 07:07:07 -04002518Performs a lookup of pixel size; for better performance, call
Cary Clarkbc5697d2017-10-04 14:31:33 -04002519one of: getAddr8, getAddr16, or getAddr32.
2520
2521#Param x column index, zero or greater, and less than width() ##
2522#Param y row index, zero or greater, and less than height() ##
2523
2524#Return generic pointer to pixel ##
2525
2526#Example
2527#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002528 char* row0 = (char* ) source.getAddr(0, 0);
2529 char* row1 = (char* ) source.getAddr(0, 1);
Cary Clark681287e2018-03-16 11:34:15 -04002530 SkDebugf("addr interval %c= rowBytes\n",
2531 (size_t) (row1 - row0) == source.rowBytes() ? '=' : '!');
Ben Wagner29380bd2017-10-09 14:43:00 -04002532#StdOut
2533addr interval == rowBytes
2534##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002535##
2536
2537#SeeAlso getAddr8 getAddr16 getAddr32 readPixels SkPixmap::addr
2538
2539##
2540
2541# ------------------------------------------------------------------------------
2542
Cary Clark61313f32018-10-08 14:57:48 -04002543#Method uint32_t* getAddr32(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002544#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002545#Line # returns readable pixel address as 32-bit pointer ##
Cary Clark682c58d2018-05-16 07:07:07 -04002546Returns address at (x, y).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002547
2548Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2549#List
2550# Pixel_Ref is nullptr ##
2551# bytesPerPixel() is not four ##
2552# x is negative, or not less than width() ##
2553# y is negative, or not less than height() ##
2554##
2555
2556#Param x column index, zero or greater, and less than width() ##
2557#Param y row index, zero or greater, and less than height() ##
2558
2559#Return unsigned 32-bit pointer to pixel at (x, y) ##
2560
2561#Example
2562#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002563 uint32_t* row0 = source.getAddr32(0, 0);
2564 uint32_t* row1 = source.getAddr32(0, 1);
2565 size_t interval = (row1 - row0) * source.bytesPerPixel();
2566 SkDebugf("addr interval %c= rowBytes\n", interval == source.rowBytes() ? '=' : '!');
2567#StdOut
2568addr interval == rowBytes
2569##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002570##
2571
2572#SeeAlso getAddr8 getAddr16 getAddr readPixels SkPixmap::addr32
2573
2574##
2575
2576# ------------------------------------------------------------------------------
2577
Cary Clark61313f32018-10-08 14:57:48 -04002578#Method uint16_t* getAddr16(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002579#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002580#Line # returns readable pixel address as 16-bit pointer ##
Cary Clark682c58d2018-05-16 07:07:07 -04002581Returns address at (x, y).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002582
2583Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2584#List
2585# Pixel_Ref is nullptr ##
2586# bytesPerPixel() is not two ##
2587# x is negative, or not less than width() ##
2588# y is negative, or not less than height() ##
2589##
2590
2591#Param x column index, zero or greater, and less than width() ##
2592#Param y row index, zero or greater, and less than height() ##
2593
2594#Return unsigned 16-bit pointer to pixel at (x, y)##
2595
2596#Example
2597#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002598 SkBitmap bitmap16;
Cary Clark682c58d2018-05-16 07:07:07 -04002599 SkImageInfo dstInfo = SkImageInfo::Make(source.width(), source.height(), kARGB_4444_SkColorType,
Ben Wagner29380bd2017-10-09 14:43:00 -04002600 kPremul_SkAlphaType);
2601 bitmap16.allocPixels(dstInfo);
2602 if (source.readPixels(dstInfo, bitmap16.getPixels(), bitmap16.rowBytes(), 0, 0)) {
2603 uint16_t* row0 = bitmap16.getAddr16(0, 0);
2604 uint16_t* row1 = bitmap16.getAddr16(0, 1);
2605 size_t interval = (row1 - row0) * bitmap16.bytesPerPixel();
2606 SkDebugf("addr interval %c= rowBytes\n", interval == bitmap16.rowBytes() ? '=' : '!');
2607 }
2608#StdOut
2609addr interval == rowBytes
2610##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002611##
2612
2613#SeeAlso getAddr8 getAddr getAddr32 readPixels SkPixmap::addr16
2614
2615##
2616
2617# ------------------------------------------------------------------------------
2618
Cary Clark61313f32018-10-08 14:57:48 -04002619#Method uint8_t* getAddr8(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002620#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002621#Line # returns readable pixel address as 8-bit pointer ##
Cary Clark682c58d2018-05-16 07:07:07 -04002622Returns address at (x, y).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002623
2624Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2625#List
2626# Pixel_Ref is nullptr ##
2627# bytesPerPixel() is not one ##
2628# x is negative, or not less than width() ##
2629# y is negative, or not less than height() ##
2630##
2631
2632#Param x column index, zero or greater, and less than width() ##
2633#Param y row index, zero or greater, and less than height() ##
2634
2635#Return unsigned 8-bit pointer to pixel at (x, y) ##
2636
2637#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04002638 SkBitmap bitmap;
2639 const int width = 8;
2640 const int height = 8;
2641 uint8_t pixels[height][width];
2642 SkImageInfo info = SkImageInfo::Make(width, height, kGray_8_SkColorType, kOpaque_SkAlphaType);
2643 if (bitmap.installPixels(info, pixels, info.minRowBytes())) {
2644 SkDebugf("&pixels[4][2] %c= bitmap.getAddr8(2, 4)\n",
2645 &pixels[4][2] == bitmap.getAddr8(2, 4) ? '=' : '!');
2646 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002647#StdOut
2648&pixels[4][2] == bitmap.getAddr8(2, 4)
2649##
2650##
2651
2652#SeeAlso getAddr getAddr16 getAddr32 readPixels SkPixmap::addr8
2653
2654##
2655
2656# ------------------------------------------------------------------------------
2657
2658#Method bool extractSubset(SkBitmap* dst, const SkIRect& subset) const
Cary Clark61313f32018-10-08 14:57:48 -04002659#In Constructors
Cary Clarkab2621d2018-01-30 10:08:57 -05002660#Line # creates Bitmap, sharing pixels if possible ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002661Shares Pixel_Ref with dst. Pixels are not copied; Bitmap and dst point
2662to the same pixels; dst bounds() are set to the intersection of subset
2663and the original bounds().
2664
2665subset may be larger than bounds(). Any area outside of bounds() is ignored.
2666
2667Any contents of dst are discarded. isVolatile setting is copied to dst.
2668dst is set to colorType, alphaType, and colorSpace.
2669
2670Return false if:
2671#List
2672# dst is nullptr ##
2673# Pixel_Ref is nullptr ##
2674# subset does not intersect bounds() ##
Cary Clark682c58d2018-05-16 07:07:07 -04002675##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002676
2677#Param dst Bitmap set to subset ##
2678#Param subset rectangle of pixels to reference ##
2679
2680#Return true if dst is replaced by subset
2681##
2682
2683#Example
2684#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002685 SkIRect bounds, s;
2686 source.getBounds(&bounds);
2687 SkDebugf("bounds: %d, %d, %d, %d\n", bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
2688 SkBitmap subset;
2689 for (int left: { -100, 0, 100, 1000 } ) {
2690 for (int right: { 0, 100, 1000 } ) {
2691 SkIRect b = SkIRect::MakeLTRB(left, 100, right, 200);
2692 bool success = source.extractSubset(&subset, b);
2693 SkDebugf("subset: %4d, %4d, %4d, %4d ", b.fLeft, b.fTop, b.fRight, b.fBottom);
2694 SkDebugf("success; %s", success ? "true" : "false");
2695 if (success) {
Cary Clark682c58d2018-05-16 07:07:07 -04002696 subset.getBounds(&s);
Ben Wagner29380bd2017-10-09 14:43:00 -04002697 SkDebugf(" subset: %d, %d, %d, %d", s.fLeft, s.fTop, s.fRight, s.fBottom);
2698 }
2699 SkDebugf("\n");
Cary Clark682c58d2018-05-16 07:07:07 -04002700 }
Ben Wagner29380bd2017-10-09 14:43:00 -04002701 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002702#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04002703bounds: 0, 0, 512, 512
2704subset: -100, 100, 0, 200 success; false
2705subset: -100, 100, 100, 200 success; true subset: 0, 0, 100, 100
2706subset: -100, 100, 1000, 200 success; true subset: 0, 0, 512, 100
2707subset: 0, 100, 0, 200 success; false
2708subset: 0, 100, 100, 200 success; true subset: 0, 0, 100, 100
2709subset: 0, 100, 1000, 200 success; true subset: 0, 0, 512, 100
2710subset: 100, 100, 0, 200 success; false
2711subset: 100, 100, 100, 200 success; false
2712subset: 100, 100, 1000, 200 success; true subset: 0, 0, 412, 100
2713subset: 1000, 100, 0, 200 success; false
2714subset: 1000, 100, 100, 200 success; false
Cary Clarkbc5697d2017-10-04 14:31:33 -04002715subset: 1000, 100, 1000, 200 success; false
2716##
2717##
2718
2719#SeeAlso readPixels writePixels SkCanvas::drawBitmap
2720
2721##
2722
2723# ------------------------------------------------------------------------------
2724
2725#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
Cary Clarkbc5697d2017-10-04 14:31:33 -04002726 int srcX, int srcY) const
Cary Clarke80cd442018-07-17 13:19:56 -04002727#In Pixels
2728#Line # copies and converts pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002729
Cary Clarkac47b882018-01-11 10:35:44 -05002730Copies a Rect of pixels from Bitmap to dstPixels. Copy starts at (srcX, srcY),
Cary Clark682c58d2018-05-16 07:07:07 -04002731and does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002732
Cary Clarkac47b882018-01-11 10:35:44 -05002733dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
2734destination. dstRowBytes specifics the gap from one destination row to the next.
2735Returns true if pixels are copied. Returns false if:
Cary Clarkbc5697d2017-10-04 14:31:33 -04002736#List
2737# dstInfo.addr() equals nullptr ##
2738# dstRowBytes is less than dstInfo.minRowBytes ##
2739# Pixel_Ref is nullptr ##
2740##
2741
Cary Clarkac47b882018-01-11 10:35:44 -05002742Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002743kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002744If Bitmap colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
2745If Bitmap alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
2746match. If Bitmap colorSpace is nullptr, dstInfo.colorSpace must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002747false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04002748
Cary Clarkbc5697d2017-10-04 14:31:33 -04002749srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04002750false if width() or height() is zero or negative.
Cary Clark2be81cf2018-09-13 12:04:30 -04002751Returns false if #Formula # abs(srcX) >= Bitmap width() ##, or if #Formula # abs(srcY) >= Bitmap height() ##.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002752
2753#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
2754#Param dstPixels destination pixel storage ##
2755#Param dstRowBytes destination row length ##
2756#Param srcX column index whose absolute value is less than width() ##
2757#Param srcY row index whose absolute value is less than height() ##
2758
2759#Return true if pixels are copied to dstPixels ##
2760
2761#Example
2762#Height 128
2763#Description
2764Transferring the gradient from 8 bits per component to 4 bits per component
2765creates visible banding.
2766##
Ben Wagner29380bd2017-10-09 14:43:00 -04002767 const int width = 256;
2768 const int height = 64;
2769 SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(width, height);
2770 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
2771 SkPoint gradPoints[] = { { 0, 0 }, { 256, 0 } };
2772 SkPaint paint;
2773 paint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
2774 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
2775 SkBitmap bitmap;
2776 bitmap.allocPixels(srcInfo);
2777 SkCanvas srcCanvas(bitmap);
2778 srcCanvas.drawRect(SkRect::MakeWH(width, height), paint);
2779 canvas->drawBitmap(bitmap, 0, 0);
2780 SkImageInfo dstInfo = srcInfo.makeColorType(kARGB_4444_SkColorType);
2781 std::vector<int16_t> dstPixels;
2782 dstPixels.resize(height * width);
2783 bitmap.readPixels(dstInfo, &dstPixels.front(), width * 2, 0, 0);
2784 SkPixmap dstPixmap(dstInfo, &dstPixels.front(), width * 2);
2785 bitmap.installPixels(dstPixmap);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002786 canvas->drawBitmap(bitmap, 0, 64);
2787##
2788
2789#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2790
2791##
2792
2793# ------------------------------------------------------------------------------
2794
2795#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY) const
2796
Cary Clarkac47b882018-01-11 10:35:44 -05002797Copies a Rect of pixels from Bitmap to dst. Copy starts at (srcX, srcY), and
Cary Clark682c58d2018-05-16 07:07:07 -04002798does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002799
2800dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2801and row bytes of destination. dst.rowBytes specifics the gap from one destination
2802row to the next. Returns true if pixels are copied. Returns false if:
2803#List
2804# dst pixel storage equals nullptr ##
2805# dst.rowBytes is less than SkImageInfo::minRowBytes ##
2806# Pixel_Ref is nullptr ##
2807##
2808
Cary Clarkac47b882018-01-11 10:35:44 -05002809Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002810kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002811If Bitmap colorType is kGray_8_SkColorType, dst Color_Space must match.
2812If Bitmap alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
2813match. If Bitmap colorSpace is nullptr, dst Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002814false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04002815
Cary Clarkbc5697d2017-10-04 14:31:33 -04002816srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark682c58d2018-05-16 07:07:07 -04002817false if width() or height() is zero or negative.
Cary Clark2be81cf2018-09-13 12:04:30 -04002818Returns false if #Formula # abs(srcX) >= Bitmap width() ##, or if #Formula # abs(srcY) >= Bitmap height() ##.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002819
2820#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
2821#Param srcX column index whose absolute value is less than width() ##
2822#Param srcY row index whose absolute value is less than height() ##
2823
2824#Return true if pixels are copied to dst ##
2825
2826#Example
2827#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002828 std::vector<int32_t> srcPixels;
2829 srcPixels.resize(source.height() * source.rowBytes());
2830 for (int y = 0; y < 4; ++y) {
2831 for (int x = 0; x < 4; ++x) {
2832 SkPixmap pixmap(SkImageInfo::MakeN32Premul(source.width() / 4, source.height() / 4),
2833 &srcPixels.front() + x * source.height() * source.width() / 4 +
2834 y * source.width() / 4, source.rowBytes());
2835 source.readPixels(pixmap, x * source.width() / 4, y * source.height() / 4);
2836 }
2837 }
2838 canvas->scale(.5f, .5f);
2839 SkBitmap bitmap;
2840 bitmap.installPixels(SkImageInfo::MakeN32Premul(source.width(), source.height()),
2841 &srcPixels.front(), source.rowBytes());
Cary Clarkbc5697d2017-10-04 14:31:33 -04002842 canvas->drawBitmap(bitmap, 0, 0);
2843##
2844
2845#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2846
2847##
2848
2849# ------------------------------------------------------------------------------
2850
2851#Method bool readPixels(const SkPixmap& dst) const
2852
Cary Clarkac47b882018-01-11 10:35:44 -05002853Copies a Rect of pixels from Bitmap to dst. Copy starts at (0, 0), and
Cary Clark682c58d2018-05-16 07:07:07 -04002854does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002855
2856dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2857and row bytes of destination. dst.rowBytes specifics the gap from one destination
2858row to the next. Returns true if pixels are copied. Returns false if:
2859#List
2860# dst pixel storage equals nullptr ##
2861# dst.rowBytes is less than SkImageInfo::minRowBytes ##
2862# Pixel_Ref is nullptr ##
2863##
2864
Cary Clarkac47b882018-01-11 10:35:44 -05002865Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002866kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002867If Bitmap colorType is kGray_8_SkColorType, dst Color_Space must match.
2868If Bitmap alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
2869match. If Bitmap colorSpace is nullptr, dst Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002870false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04002871
Cary Clarkbc5697d2017-10-04 14:31:33 -04002872#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
2873
2874#Return true if pixels are copied to dst ##
2875
2876#Example
2877#Height 128
2878#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002879 std::vector<int32_t> srcPixels;
2880 srcPixels.resize(source.height() * source.width() * 8);
2881 for (int i = 0; i < 2; ++i) {
Cary Clark682c58d2018-05-16 07:07:07 -04002882 SkPixmap pixmap(SkImageInfo::Make(source.width() * 2, source.height(),
Ben Wagner29380bd2017-10-09 14:43:00 -04002883 i ? kRGBA_8888_SkColorType : kBGRA_8888_SkColorType, kPremul_SkAlphaType),
2884 &srcPixels.front() + i * source.width(), source.rowBytes() * 2);
2885 source.readPixels(pixmap);
2886 }
2887 canvas->scale(.25f, .25f);
2888 SkBitmap bitmap;
2889 bitmap.installPixels(SkImageInfo::MakeN32Premul(source.width() * 2, source.height()),
2890 &srcPixels.front(), source.rowBytes() * 2);
2891 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002892##
2893
2894#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2895
2896##
2897
2898# ------------------------------------------------------------------------------
2899
2900#Method bool writePixels(const SkPixmap& src, int dstX, int dstY)
Cary Clark78de7512018-02-07 07:27:09 -05002901#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05002902#Line # copies and converts pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002903Copies a Rect of pixels from src. Copy starts at (dstX, dstY), and does not exceed
Cary Clark682c58d2018-05-16 07:07:07 -04002904(src.width(), src.height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002905
2906src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2907and row bytes of source. src.rowBytes specifics the gap from one source
2908row to the next. Returns true if pixels are copied. Returns false if:
2909#List
2910# src pixel storage equals nullptr ##
2911# src.rowBytes is less than SkImageInfo::minRowBytes ##
2912# Pixel_Ref is nullptr ##
2913##
2914
Cary Clarkac47b882018-01-11 10:35:44 -05002915Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002916kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002917If Bitmap colorType is kGray_8_SkColorType, src Color_Space must match.
2918If Bitmap alphaType is kOpaque_SkAlphaType, src Alpha_Type must
2919match. If Bitmap colorSpace is nullptr, src Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002920false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04002921
Cary Clarkbc5697d2017-10-04 14:31:33 -04002922dstX and dstY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04002923false if width() or height() is zero or negative.
Cary Clark2be81cf2018-09-13 12:04:30 -04002924Returns false if #Formula # abs(dstX) >= Bitmap width() ##, or if #Formula # abs(dstY) >= Bitmap height() ##.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002925
2926#Param src source Pixmap: Image_Info, pixels, row bytes ##
2927#Param dstX column index whose absolute value is less than width() ##
2928#Param dstY row index whose absolute value is less than height() ##
2929
2930#Return true if src pixels are copied to Bitmap ##
2931
2932#Example
2933#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002934 std::vector<int32_t> srcPixels;
2935 int width = image->width();
2936 int height = image->height();
2937 srcPixels.resize(height * width * 4);
2938 SkPixmap pixmap(SkImageInfo::MakeN32Premul(width, height), (const void*) &srcPixels.front(),
2939 width * 4);
2940 image->readPixels(pixmap, 0, 0);
2941 canvas->scale(.5f, .5f);
2942 width /= 4;
2943 height /= 4;
2944 for (int y = 0; y < 4; ++y) {
2945 for (int x = 0; x < 4; ++x) {
2946 SkBitmap bitmap;
2947 bitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height));
2948 bitmap.writePixels(pixmap, -y * width, -x * height);
2949 canvas->drawBitmap(bitmap, x * width, y * height);
2950 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002951 }
2952##
2953
Cary Clark682c58d2018-05-16 07:07:07 -04002954#SeeAlso readPixels
Cary Clarkbc5697d2017-10-04 14:31:33 -04002955
2956##
2957
2958# ------------------------------------------------------------------------------
2959
2960#Method bool writePixels(const SkPixmap& src)
2961
2962Copies a Rect of pixels from src. Copy starts at (0, 0), and does not exceed
Cary Clark682c58d2018-05-16 07:07:07 -04002963(src.width(), src.height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002964
2965src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2966and row bytes of source. src.rowBytes specifics the gap from one source
2967row to the next. Returns true if pixels are copied. Returns false if:
2968#List
2969# src pixel storage equals nullptr ##
2970# src.rowBytes is less than SkImageInfo::minRowBytes ##
2971# Pixel_Ref is nullptr ##
2972##
2973
Cary Clarkac47b882018-01-11 10:35:44 -05002974Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002975kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002976If Bitmap colorType is kGray_8_SkColorType, src Color_Space must match.
2977If Bitmap alphaType is kOpaque_SkAlphaType, src Alpha_Type must
2978match. If Bitmap colorSpace is nullptr, src Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002979false if pixel conversion is not possible.
2980
2981#Param src source Pixmap: Image_Info, pixels, row bytes ##
2982
2983#Return true if src pixels are copied to Bitmap ##
2984
2985#Example
2986#Height 80
Ben Wagner29380bd2017-10-09 14:43:00 -04002987 SkBitmap bitmap;
2988 bitmap.allocPixels(SkImageInfo::MakeN32Premul(2, 2));
2989 bitmap.eraseColor(SK_ColorGREEN);
2990 SkPMColor color = 0xFF5599BB;
2991 SkPixmap src(SkImageInfo::MakeN32Premul(1, 1), &color, 4);
2992 bitmap.writePixels(src);
2993 canvas->scale(40, 40);
2994 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002995##
2996
2997#SeeAlso readPixels
2998
2999##
3000
3001# ------------------------------------------------------------------------------
3002
Cary Clarkbc5697d2017-10-04 14:31:33 -04003003#Method bool hasHardwareMipMap() const
Cary Clark78de7512018-02-07 07:27:09 -05003004#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05003005#Line # returns Mip_Map support present; Android only ##
Cary Clark224c7002018-06-27 11:00:21 -04003006
3007For use by Android framework only.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003008
3009#Return true if setHasHardwareMipMap has been called with true ##
3010
3011#NoExample
3012##
3013
3014#SeeAlso setHasHardwareMipMap
3015
3016##
3017
3018# ------------------------------------------------------------------------------
3019
3020#Method void setHasHardwareMipMap(bool hasHardwareMipMap)
Cary Clark78de7512018-02-07 07:27:09 -05003021#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05003022#Line # sets Mip_Map support present; Android only ##
Cary Clark224c7002018-06-27 11:00:21 -04003023
3024For use by Android framework only.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003025
3026#Param hasHardwareMipMap sets state ##
3027
3028#NoExample
3029##
3030
3031#SeeAlso hasHardwareMipMap
3032
3033##
3034
3035# ------------------------------------------------------------------------------
3036
3037#Method bool extractAlpha(SkBitmap* dst) const
Cary Clark61313f32018-10-08 14:57:48 -04003038#In Constructors
Cary Clarkab2621d2018-01-30 10:08:57 -05003039#Line # creates Bitmap containing Alpha of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003040Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3041or dst pixels cannot be allocated.
3042
3043Uses HeapAllocator to reserve memory for dst Pixel_Ref.
3044
3045#Param dst holds Pixel_Ref to fill with alpha layer ##
3046
3047#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3048
3049#Example
3050#Height 100
Ben Wagner29380bd2017-10-09 14:43:00 -04003051 SkBitmap alpha, bitmap;
3052 bitmap.allocN32Pixels(100, 100);
3053 SkCanvas offscreen(bitmap);
3054 offscreen.clear(0);
3055 SkPaint paint;
3056 paint.setAntiAlias(true);
3057 paint.setColor(SK_ColorBLUE);
3058 paint.setStyle(SkPaint::kStroke_Style);
3059 paint.setStrokeWidth(20);
3060 offscreen.drawCircle(50, 50, 39, paint);
3061 offscreen.flush();
3062 bitmap.extractAlpha(&alpha);
3063 paint.setColor(SK_ColorRED);
3064 canvas->drawBitmap(bitmap, 0, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003065 canvas->drawBitmap(alpha, 100, 0, &paint);
3066##
3067
3068#SeeAlso extractSubset
3069
3070##
3071
3072# ------------------------------------------------------------------------------
3073
3074#Method bool extractAlpha(SkBitmap* dst, const SkPaint* paint,
3075 SkIPoint* offset) const
3076
3077Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3078or dst pixels cannot be allocated.
3079
Cary Clark682c58d2018-05-16 07:07:07 -04003080If paint is not nullptr and contains Mask_Filter, SkMaskFilter
Cary Clarkbc5697d2017-10-04 14:31:33 -04003081generates Mask_Alpha from Bitmap. Uses HeapAllocator to reserve memory for dst
3082Pixel_Ref. Sets offset to top-left position for dst for alignment with Bitmap;
3083(0, 0) unless SkMaskFilter generates mask.
3084
3085#Param dst holds Pixel_Ref to fill with alpha layer ##
3086#Param paint holds optional Mask_Filter; may be nullptr ##
3087#Param offset top-left position for dst; may be nullptr ##
3088
3089#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3090
Cary Clark4855f782018-02-06 09:41:53 -05003091#Bug 7103
Cary Clarkbc5697d2017-10-04 14:31:33 -04003092#Example
3093#Height 160
Cary Clark681287e2018-03-16 11:34:15 -04003094 auto radiusToSigma = [](SkScalar radius) -> SkScalar {
3095 static const SkScalar kBLUR_SIGMA_SCALE = 0.57735f;
3096 return radius > 0 ? kBLUR_SIGMA_SCALE * radius + 0.5f : 0.0f;
3097 };
3098 SkBitmap alpha, bitmap;
3099 bitmap.allocN32Pixels(100, 100);
3100 SkCanvas offscreen(bitmap);
3101 offscreen.clear(0);
3102 SkPaint paint;
3103 paint.setAntiAlias(true);
3104 paint.setColor(SK_ColorBLUE);
3105 paint.setStyle(SkPaint::kStroke_Style);
3106 paint.setStrokeWidth(20);
3107 offscreen.drawCircle(50, 50, 39, paint);
3108 offscreen.flush();
3109 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, radiusToSigma(25)));
3110 SkIPoint offset;
3111 bitmap.extractAlpha(&alpha, &paint, &offset);
3112 paint.setColor(SK_ColorRED);
3113 canvas->drawBitmap(bitmap, 0, -offset.fY, &paint);
3114 canvas->drawBitmap(alpha, 100 + offset.fX, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003115##
3116
3117#SeeAlso extractSubset
3118
3119##
3120
3121# ------------------------------------------------------------------------------
3122
3123#Method bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator,
3124 SkIPoint* offset) const
3125
3126Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3127or dst pixels cannot be allocated.
3128
Cary Clark682c58d2018-05-16 07:07:07 -04003129If paint is not nullptr and contains Mask_Filter, SkMaskFilter
Cary Clarkbc5697d2017-10-04 14:31:33 -04003130generates Mask_Alpha from Bitmap. allocator may reference a custom allocation
Cary Clark682c58d2018-05-16 07:07:07 -04003131class or be set to nullptr to use HeapAllocator. Sets offset to top-left
Cary Clarkbc5697d2017-10-04 14:31:33 -04003132position for dst for alignment with Bitmap; (0, 0) unless SkMaskFilter generates
3133mask.
3134
3135#Param dst holds Pixel_Ref to fill with alpha layer ##
3136#Param paint holds optional Mask_Filter; may be nullptr ##
Cary Clark682c58d2018-05-16 07:07:07 -04003137#Param allocator function to reserve memory for Pixel_Ref; may be nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003138#Param offset top-left position for dst; may be nullptr ##
3139
3140#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3141
Cary Clark4855f782018-02-06 09:41:53 -05003142#Bug 7104
Cary Clarkbc5697d2017-10-04 14:31:33 -04003143#Example
3144#Height 128
Ben Wagner29380bd2017-10-09 14:43:00 -04003145 SkBitmap alpha, bitmap;
3146 bitmap.allocN32Pixels(100, 100);
3147 SkCanvas offscreen(bitmap);
3148 offscreen.clear(0);
3149 SkPaint paint;
3150 paint.setAntiAlias(true);
3151 paint.setColor(SK_ColorBLUE);
3152 paint.setStyle(SkPaint::kStroke_Style);
3153 paint.setStrokeWidth(20);
3154 offscreen.drawCircle(50, 50, 39, paint);
3155 offscreen.flush();
Cary Clark681287e2018-03-16 11:34:15 -04003156 paint.setMaskFilter(SkMaskFilter::MakeBlur(kOuter_SkBlurStyle, 3));
Ben Wagner29380bd2017-10-09 14:43:00 -04003157 SkIPoint offset;
3158 bitmap.extractAlpha(&alpha, &paint, nullptr, &offset);
3159 paint.setColor(SK_ColorRED);
3160 canvas->drawBitmap(bitmap, 0, -offset.fY, &paint);
3161 canvas->drawBitmap(alpha, 100 + offset.fX, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003162##
3163
3164#SeeAlso extractSubset
3165
3166##
3167
3168# ------------------------------------------------------------------------------
3169
3170#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05003171#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05003172#Line # returns Pixmap if possible ##
Cary Clark154beea2017-10-26 07:58:48 -04003173Copies Bitmap pixel address, row bytes, and Image_Info to pixmap, if address
3174is available, and returns true. If pixel address is not available, return
3175false and leave pixmap unchanged.
3176
3177pixmap contents become invalid on any future change to Bitmap.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003178
3179#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
3180
3181#Return true if Bitmap has direct access to pixels ##
3182
3183#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04003184 SkBitmap bitmap;
3185 bitmap.allocPixels(SkImageInfo::MakeN32Premul(6, 11));
3186 SkCanvas offscreen(bitmap);
3187 offscreen.clear(SK_ColorWHITE);
3188 SkPaint paint;
3189 offscreen.drawString("?", 0, 10, paint);
3190 SkPixmap pixmap;
3191 if (bitmap.peekPixels(&pixmap)) {
3192 const SkPMColor* pixels = pixmap.addr32();
3193 SkPMColor pmWhite = pixels[0];
3194 for (int y = 0; y < bitmap.height(); ++y) {
3195 for (int x = 0; x < bitmap.width(); ++x) {
3196 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
3197 }
3198 SkDebugf("\n");
3199 }
3200 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003201 #StdOut
Cary Clark2f466242017-12-11 16:03:17 -05003202------
3203-xxx--
3204x---x-
3205----x-
3206---x--
3207--x---
3208--x---
3209------
3210--x---
3211--x---
Cary Clarka560c472017-11-27 10:44:06 -05003212------
Cary Clarkbc5697d2017-10-04 14:31:33 -04003213 #StdOut ##
3214##
3215
Cary Clark682c58d2018-05-16 07:07:07 -04003216#SeeAlso pixmap() installPixels readPixels writePixels
Cary Clarkbc5697d2017-10-04 14:31:33 -04003217
3218##
3219
3220# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05003221#Subtopic Utility
Cary Clark78de7512018-02-07 07:27:09 -05003222#Line # rarely called management functions ##
3223##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003224
Cary Clark154beea2017-10-26 07:58:48 -04003225#Method void validate() const;
Cary Clark78de7512018-02-07 07:27:09 -05003226#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05003227#Line # asserts if Bitmap is invalid (debug only) ##
Cary Clark682c58d2018-05-16 07:07:07 -04003228Asserts if internal values are illegal or inconsistent. Only available if
Cary Clark154beea2017-10-26 07:58:48 -04003229SK_DEBUG is defined at compile time.
3230
3231#NoExample
3232##
3233
Cary Clark06c20f32018-03-20 15:53:27 -04003234#SeeAlso SkImageInfo::validate
Cary Clark154beea2017-10-26 07:58:48 -04003235
3236##
3237
3238# ------------------------------------------------------------------------------
3239
Cary Clarkbc5697d2017-10-04 14:31:33 -04003240#Class SkBitmap ##
3241
3242#Topic Bitmap ##
Cary Clark4855f782018-02-06 09:41:53 -05003243