blob: d17dd649d3278e3d31bc9fe38ba47d60bfcaf1c0 [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
6
7Bitmap describes a two-dimensional raster pixel array. Bitmap is built on
8Image_Info, containing integer width and height, Color_Type and Alpha_Type
9describing the pixel format, and Color_Space describing the range of colors.
10Bitmap points to Pixel_Ref, which describes the physical array of pixels.
11Image_Info bounds may be located anywhere fully inside Pixel_Ref bounds.
12
13Bitmap can be drawn using Canvas. Bitmap can be a drawing destination for Canvas
Cary Clark137b8742018-05-30 09:21:49 -040014draw member functions. Bitmap flexibility as a pixel container limits some
Cary Clark682c58d2018-05-16 07:07:07 -040015optimizations available to the target platform.
Cary Clarkbc5697d2017-10-04 14:31:33 -040016
17If pixel array is primarily read-only, use Image for better performance.
18If pixel array is primarily written to, use Surface for better performance.
19
20Declaring SkBitmap const prevents altering Image_Info: the Bitmap height, width,
21and so on cannot change. It does not affect Pixel_Ref: a caller may write its
22pixels. Declaring SkBitmap const affects Bitmap configuration, not its contents.
23
Cary Clarkbef063a2017-10-31 15:44:45 -040024Bitmap is not thread safe. Each thread must have its own copy of Bitmap fields,
Cary Clarkbc5697d2017-10-04 14:31:33 -040025although threads may share the underlying pixel array.
26
Cary Clark08895c42018-02-01 09:37:32 -050027#Subtopic Row_Bytes
28#Line # interval from one row to the next ##
Cary Clarkbc5697d2017-10-04 14:31:33 -040029Bitmap pixels may be contiguous, or may have a gap at the end of each row.
30Row_Bytes is the interval from one row to the next. Row_Bytes may be specified;
31sometimes passing zero will compute the Row_Bytes from the row width and the
32number of bytes in a pixel. Row_Bytes may be larger than the row requires. This
33is useful to position one or more Bitmaps within a shared pixel array.
34##
35
Cary Clark682c58d2018-05-16 07:07:07 -040036#Subtopic Overview
37 #Populate
38##
39
Cary Clark4855f782018-02-06 09:41:53 -050040#Subtopic Related_Function
Cary Clark08895c42018-02-01 09:37:32 -050041#Populate
Cary Clarkbc5697d2017-10-04 14:31:33 -040042##
43
Cary Clark4855f782018-02-06 09:41:53 -050044#Subtopic Constant
Cary Clark08895c42018-02-01 09:37:32 -050045#Populate
46##
Cary Clarkbc5697d2017-10-04 14:31:33 -040047
Cary Clark682c58d2018-05-16 07:07:07 -040048#Subtopic Class
Cary Clark08895c42018-02-01 09:37:32 -050049#Populate
50##
Cary Clarkbc5697d2017-10-04 14:31:33 -040051
Cary Clark4855f782018-02-06 09:41:53 -050052#Subtopic Constructor
Cary Clark08895c42018-02-01 09:37:32 -050053#Populate
54##
Cary Clarkbc5697d2017-10-04 14:31:33 -040055
Cary Clark4855f782018-02-06 09:41:53 -050056#Subtopic Operator
57#Populate
58##
59
60#Subtopic Member_Function
Cary Clark08895c42018-02-01 09:37:32 -050061#Populate
62##
Cary Clarkbc5697d2017-10-04 14:31:33 -040063
64# ------------------------------------------------------------------------------
65
66#Class Allocator
Cary Clark08895c42018-02-01 09:37:32 -050067#Line # abstract subclass of HeapAllocator ##
Cary Clarkbc5697d2017-10-04 14:31:33 -040068#Code
69 class Allocator : public SkRefCnt {
70 public:
71 virtual bool allocPixelRef(SkBitmap* bitmap) = 0;
72 };
73##
74
75Abstract subclass of HeapAllocator.
76
77# ------------------------------------------------------------------------------
78
79#Method virtual bool allocPixelRef(SkBitmap* bitmap) = 0
Cary Clarkd2ca79c2018-08-10 13:09:13 -040080#Line # allocates pixel memory ##
Cary Clarkbc5697d2017-10-04 14:31:33 -040081
82Allocates the pixel memory for the bitmap, given its dimensions and
83Color_Type. Returns true on success, where success means either setPixels
84or setPixelRef was called.
85
86#Param bitmap Bitmap containing Image_Info as input, and Pixel_Ref as output ##
87
88#Return true if Pixel_Ref was allocated ##
89
90#NoExample
91##
92
93#SeeAlso HeapAllocator
94
95##
96
97#Class Allocator ##
98
99# ------------------------------------------------------------------------------
100
101#Class HeapAllocator
Cary Clark08895c42018-02-01 09:37:32 -0500102#Line # allocates pixel memory from heap ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400103
104#Code
105 class HeapAllocator : public Allocator {
106 public:
107 bool allocPixelRef(SkBitmap* bitmap) override;
108 };
109##
110
Cary Clark78c110e2018-02-09 16:49:09 -0500111Subclass of SkBitmap::Allocator that returns a Pixel_Ref that allocates its pixel
112memory from the heap. This is the default SkBitmap::Allocator invoked by
Cary Clarkbc5697d2017-10-04 14:31:33 -0400113allocPixels.
114
115# ------------------------------------------------------------------------------
116
117#Method bool allocPixelRef(SkBitmap* bitmap) override
Cary Clark682c58d2018-05-16 07:07:07 -0400118#Line # allocates pixel memory ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400119Allocates the pixel memory for the bitmap, given its dimensions and
120Color_Type. Returns true on success, where success means either setPixels
121or setPixelRef was called.
122
123#Param bitmap Bitmap containing Image_Info as input, and Pixel_Ref as output ##
124
125#Return true if pixels are allocated ##
126
127#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400128 SkBitmap bitmap;
129 bitmap.setInfo(SkImageInfo::MakeN32(16, 16, kPremul_SkAlphaType));
130 SkDebugf("pixel address = %p\n", bitmap.getPixels());
131 SkBitmap::HeapAllocator stdalloc;
132 if (!stdalloc.allocPixelRef(&bitmap)) {
133 SkDebugf("pixel allocation failed\n");
134 } else {
135 SkDebugf("pixel address = %p\n", bitmap.getPixels());
136 }
137#StdOut
Cary Clark884dd7d2017-10-11 10:37:52 -0400138#Volatile
Ben Wagner29380bd2017-10-09 14:43:00 -0400139pixel address = (nil)
140pixel address = 0x560ddd0ac670
141##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400142##
143
Cary Clark78c110e2018-02-09 16:49:09 -0500144#SeeAlso SkBitmap::Allocator tryAllocPixels
Cary Clarkbc5697d2017-10-04 14:31:33 -0400145
146##
147
Cary Clark08895c42018-02-01 09:37:32 -0500148#Class HeapAllocator ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400149
150# ------------------------------------------------------------------------------
151
152#Method SkBitmap()
153
Cary Clarkab2621d2018-01-30 10:08:57 -0500154#Line # constructs with default values ##
Cary Clark682c58d2018-05-16 07:07:07 -0400155Creates an empty Bitmap without pixels, with kUnknown_SkColorType,
Cary Clarkbc5697d2017-10-04 14:31:33 -0400156kUnknown_SkAlphaType, and with a width and height of zero. Pixel_Ref origin is
157set to (0, 0). Bitmap is not volatile.
158
159Use setInfo to associate SkColorType, SkAlphaType, width, and height
160after Bitmap has been created.
161
162#Return empty Bitmap ##
163
164#Example
165void draw(SkCanvas* canvas) {
166 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500167 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
168 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clarkbc5697d2017-10-04 14:31:33 -0400169 SkBitmap bitmap;
170 for (int i = 0; i < 2; ++i) {
171 SkDebugf("width: %2d height: %2d", bitmap.width(), bitmap.height());
172 SkDebugf(" color: k%s_SkColorType", colors[bitmap.colorType()]);
173 SkDebugf(" alpha: k%s_SkAlphaType\n", alphas[bitmap.alphaType()]);
174 bitmap.setInfo(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType),
175 0);
176 }
177}
178#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400179width: 0 height: 0 color: kUnknown_SkColorType alpha: kUnknown_SkAlphaType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400180width: 25 height: 35 color: kRGBA_8888_SkColorType alpha: kOpaque_SkAlphaType
181##
182##
183
184#SeeAlso setInfo
185
186##
187
188# ------------------------------------------------------------------------------
189
190#Method SkBitmap(const SkBitmap& src)
191
Cary Clarkab2621d2018-01-30 10:08:57 -0500192#Line # shares ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400193Copies settings from src to returned Bitmap. Shares pixels if src has pixels
194allocated, so both bitmaps reference the same pixels.
195
196#Param src Bitmap to copy Image_Info, and share Pixel_Ref ##
197
198#Return copy of src ##
199
200#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400201void draw(SkCanvas* canvas) {
202 SkBitmap original;
Cary Clark681287e2018-03-16 11:34:15 -0400203 if (original.tryAllocPixels(
204 SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
205 SkDebugf("original has pixels before copy: %s\n", original.getPixels() ? "true" : "false");
Cary Clark682c58d2018-05-16 07:07:07 -0400206 SkBitmap copy(original);
Cary Clark681287e2018-03-16 11:34:15 -0400207 SkDebugf("original has pixels after copy: %s\n", original.getPixels() ? "true" : "false");
208 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
209 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400210}
211#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400212original has pixels before copy: true
213original has pixels after copy: true
Cary Clarkbc5697d2017-10-04 14:31:33 -0400214copy has pixels: true
215##
216##
217
218#SeeAlso setInfo setPixelRef setPixels swap
219
220##
221
222# ------------------------------------------------------------------------------
223
224#Method SkBitmap(SkBitmap&& src)
225
Cary Clarkab2621d2018-01-30 10:08:57 -0500226#Line # takes ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400227Copies settings from src to returned Bitmap. Moves ownership of src pixels to
228Bitmap.
229
230#Param src Bitmap to copy Image_Info, and reassign Pixel_Ref ##
231
232#Return copy of src ##
233
234#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400235void draw(SkCanvas* canvas) {
236 SkBitmap original;
Cary Clark681287e2018-03-16 11:34:15 -0400237 if (original.tryAllocPixels(
238 SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
239 SkDebugf("original has pixels before move: %s\n", original.getPixels() ? "true" : "false");
Cary Clark682c58d2018-05-16 07:07:07 -0400240 SkBitmap copy(std::move(original));
Cary Clark681287e2018-03-16 11:34:15 -0400241 SkDebugf("original has pixels after move: %s\n", original.getPixels() ? "true" : "false");
242 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
243 }
Ben Wagner29380bd2017-10-09 14:43:00 -0400244}
Cary Clarkbc5697d2017-10-04 14:31:33 -0400245#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400246original has pixels before move: true
247original has pixels after move: false
Cary Clarkbc5697d2017-10-04 14:31:33 -0400248copy has pixels: true
249##
250##
251
252#SeeAlso setInfo setPixelRef setPixels swap
253
254##
255
256# ------------------------------------------------------------------------------
257
258#Method ~SkBitmap()
259
Cary Clarkab2621d2018-01-30 10:08:57 -0500260#Line # releases ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400261Decrements Pixel_Ref reference count, if Pixel_Ref is not nullptr.
262
263#NoExample
264##
265
266#SeeAlso Pixel_Ref
267
268##
269
270# ------------------------------------------------------------------------------
271
272#Method SkBitmap& operator=(const SkBitmap& src)
273
Cary Clarkab2621d2018-01-30 10:08:57 -0500274#Line # shares ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400275Copies settings from src to returned Bitmap. Shares pixels if src has pixels
276allocated, so both bitmaps reference the same pixels.
277
278#Param src Bitmap to copy Image_Info, and share Pixel_Ref ##
279
280#Return copy of src ##
281
282#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400283void draw(SkCanvas* canvas) {
284 SkBitmap original;
Cary Clark681287e2018-03-16 11:34:15 -0400285 if (original.tryAllocPixels(
286 SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
287 SkDebugf("original has pixels before copy: %s\n", original.getPixels() ? "true" : "false");
Cary Clark682c58d2018-05-16 07:07:07 -0400288 SkBitmap copy = original;
Cary Clark681287e2018-03-16 11:34:15 -0400289 SkDebugf("original has pixels after copy: %s\n", original.getPixels() ? "true" : "false");
290 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
291 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400292}
293#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400294original has pixels before copy: true
295original has pixels after copy: true
Cary Clarkbc5697d2017-10-04 14:31:33 -0400296copy has pixels: true
297##
298##
299
300#SeeAlso setInfo setPixelRef setPixels swap
301
302##
303
304# ------------------------------------------------------------------------------
305
306#Method SkBitmap& operator=(SkBitmap&& src)
307
Cary Clarkab2621d2018-01-30 10:08:57 -0500308#Line # takes ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400309Copies settings from src to returned Bitmap. Moves ownership of src pixels to
310Bitmap.
311
312#Param src Bitmap to copy Image_Info, and reassign Pixel_Ref ##
313
314#Return copy of src ##
315
316#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400317void draw(SkCanvas* canvas) {
318 SkBitmap original;
Cary Clark681287e2018-03-16 11:34:15 -0400319 if (original.tryAllocPixels(
320 SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
321 SkDebugf("original has pixels before move: %s\n", original.getPixels() ? "true" : "false");
Cary Clark682c58d2018-05-16 07:07:07 -0400322 SkBitmap copy = std::move(original);
Cary Clark681287e2018-03-16 11:34:15 -0400323 SkDebugf("original has pixels after move: %s\n", original.getPixels() ? "true" : "false");
324 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
325 }
Ben Wagner29380bd2017-10-09 14:43:00 -0400326}
Cary Clarkbc5697d2017-10-04 14:31:33 -0400327#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400328original has pixels before move: true
329original has pixels after move: false
Cary Clarkbc5697d2017-10-04 14:31:33 -0400330copy has pixels: true
331##
332##
333
334#SeeAlso setInfo setPixelRef setPixels swap
335
336##
337
338# ------------------------------------------------------------------------------
339
340#Method void swap(SkBitmap& other)
Cary Clark78de7512018-02-07 07:27:09 -0500341#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -0500342#Line # exchanges Bitmap pair ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400343Swaps the fields of the two bitmaps.
344
345#Param other Bitmap exchanged with original ##
346
347#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400348void draw(SkCanvas* canvas) {
349 auto debugster = [](const char* prefix, const SkBitmap& b) -> void {
350 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500351 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
352 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Ben Wagner29380bd2017-10-09 14:43:00 -0400353 SkDebugf("%s width:%d height:%d colorType:k%s_SkColorType alphaType:k%s_SkAlphaType\n",
354 prefix, b.width(), b.height(), colors[b.colorType()], alphas[b.alphaType()]);
355 };
356 SkBitmap one, two;
Cary Clark681287e2018-03-16 11:34:15 -0400357 if (!one.tryAllocPixels(
358 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
359 return;
360 }
361 if (!two.tryAllocPixels(
362 SkImageInfo::Make(2, 2, kBGRA_8888_SkColorType, kPremul_SkAlphaType))) {
363 return;
364 }
Ben Wagner29380bd2017-10-09 14:43:00 -0400365 for (int index = 0; index < 2; ++index) {
366 debugster("one", one);
367 debugster("two", two);
368 one.swap(two);
369 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400370}
371#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400372one width:1 height:1 colorType:kRGBA_8888_SkColorType alphaType:kOpaque_SkAlphaType
373two width:2 height:2 colorType:kBGRA_8888_SkColorType alphaType:kPremul_SkAlphaType
374one width:2 height:2 colorType:kBGRA_8888_SkColorType alphaType:kPremul_SkAlphaType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400375two width:1 height:1 colorType:kRGBA_8888_SkColorType alphaType:kOpaque_SkAlphaType
376##
377##
378
379#SeeAlso SkBitmap(SkBitmap&& src) operator=(SkBitmap&& src)
380
381##
382
383# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -0500384#Subtopic Property
385#Populate
386#Line # metrics and attributes ##
387##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400388
Hal Canary99578d22017-12-14 21:13:47 -0500389#Method const SkPixmap& pixmap() const
Cary Clark78de7512018-02-07 07:27:09 -0500390#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500391#Line # returns Pixmap ##
Hal Canary99578d22017-12-14 21:13:47 -0500392Returns a constant reference to the Pixmap holding the Bitmap pixel
393address, row bytes, and Image_Info.
Cary Clark0c5f5462017-12-15 11:21:51 -0500394
Cary Clarkac47b882018-01-11 10:35:44 -0500395#Return reference to Pixmap describing this Bitmap ##
Cary Clark0c5f5462017-12-15 11:21:51 -0500396
397#Example
398 SkBitmap bitmap;
399 bitmap.allocPixels(SkImageInfo::MakeN32Premul(10, 11));
400 SkCanvas offscreen(bitmap);
401 offscreen.clear(SK_ColorWHITE);
402 SkPaint paint;
403 offscreen.drawString("&", 0, 10, paint);
Hal Canary99578d22017-12-14 21:13:47 -0500404 const SkPixmap& pixmap = bitmap.pixmap();
Cary Clark0c5f5462017-12-15 11:21:51 -0500405 if (pixmap.addr()) {
Hal Canary99578d22017-12-14 21:13:47 -0500406 SkPMColor pmWhite = *pixmap.addr32(0, 0);
407 for (int y = 0; y < pixmap.height(); ++y) {
408 for (int x = 0; x < pixmap.width(); ++x) {
409 SkDebugf("%c", *pixmap.addr32(x, y) == pmWhite ? '-' : 'x');
Cary Clark0c5f5462017-12-15 11:21:51 -0500410 }
411 SkDebugf("\n");
412 }
413 }
414 #StdOut
415----------
416---xx-----
417--x--x----
418--x-------
419--xx------
420--x-x---x-
421-x---x--x-
422-x----xx--
423-xx---x---
424--xxxx-xx-
425----------
426 #StdOut ##
427
428##
429
430#SeeAlso peekPixels installPixels readPixels writePixels
431
432##
433
434# ------------------------------------------------------------------------------
435
Cary Clarkbc5697d2017-10-04 14:31:33 -0400436#Method const SkImageInfo& info() const
Cary Clark78de7512018-02-07 07:27:09 -0500437#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500438#Line # returns Image_Info ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400439Returns width, height, Alpha_Type, Color_Type, and Color_Space.
440
441#Return reference to Image_Info ##
442
443#Example
444#Image 4
Ben Wagner29380bd2017-10-09 14:43:00 -0400445void draw(SkCanvas* canvas) {
446 // SkBitmap source; // pre-populated with soccer ball by fiddle.skia.org
447 const SkImageInfo& info = source.info();
448 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500449 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
450 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Ben Wagner29380bd2017-10-09 14:43:00 -0400451 SkDebugf("width: %d height: %d color: %s alpha: %s\n", info.width(), info.height(),
452 colors[info.colorType()], alphas[info.alphaType()]);
453#StdOut
454width: 56 height: 56 color: BGRA_8888 alpha: Opaque
455##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400456}
457##
458
459#SeeAlso Image_Info
460
461##
462
463# ------------------------------------------------------------------------------
464
465#Method int width() const
Cary Clark78de7512018-02-07 07:27:09 -0500466#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500467#Line # returns pixel column count ##
Cary Clark2be81cf2018-09-13 12:04:30 -0400468Returns pixel count in each row. Should be equal or less than
469#Formula # rowBytes() / info().bytesPerPixel() ##.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400470
Cary Clark80247e52018-07-11 16:18:41 -0400471May be less than pixelRef().width(). Will not exceed pixelRef().width() less
Cary Clarkbc5697d2017-10-04 14:31:33 -0400472pixelRefOrigin().fX.
473
474#Return pixel width in Image_Info ##
475
476#Example
477 SkImageInfo info = SkImageInfo::MakeA8(16, 32);
478 SkBitmap bitmap;
479 bitmap.setInfo(info);
480 SkDebugf("bitmap width: %d info width: %d\n", bitmap.width(), info.width());
481#StdOut
482bitmap width: 16 info width: 16
483##
484##
485
486#SeeAlso height() SkPixelRef::width() SkImageInfo::width()
487
488##
489
490# ------------------------------------------------------------------------------
491
492#Method int height() const
Cary Clark78de7512018-02-07 07:27:09 -0500493#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500494#Line # returns pixel row count ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400495Returns pixel row count.
496
497Maybe be less than pixelRef().height(). Will not exceed pixelRef().height() less
498pixelRefOrigin().fY.
499
500#Return pixel height in Image_Info ##
501
502#Example
503 SkImageInfo info = SkImageInfo::MakeA8(16, 32);
504 SkBitmap bitmap;
505 bitmap.setInfo(info);
506 SkDebugf("bitmap height: %d info height: %d\n", bitmap.height(), info.height());
507#StdOut
508bitmap height: 32 info height: 32
509##
510##
511
512#SeeAlso width() SkPixelRef::height() SkImageInfo::height()
513
514##
515
516# ------------------------------------------------------------------------------
517
518#Method SkColorType colorType() const
Cary Clark78de7512018-02-07 07:27:09 -0500519#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500520#Line # returns Image_Info Color_Type ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500521Returns Color_Type, one of: #list_of_color_types#.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400522
523#Return Color_Type in Image_Info ##
524
525#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500526 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
527 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Ben Wagner29380bd2017-10-09 14:43:00 -0400528 SkBitmap bitmap;
529 bitmap.setInfo(SkImageInfo::MakeA8(16, 32));
530 SkDebugf("color type: k" "%s" "_SkColorType\n", colors[bitmap.colorType()]);
Cary Clarkbc5697d2017-10-04 14:31:33 -0400531#StdOut
Cary Clarkab2621d2018-01-30 10:08:57 -0500532color type: kAlpha_8_SkColorType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400533##
534##
535
536#SeeAlso alphaType() SkImageInfo::colorType
537
538##
539
540# ------------------------------------------------------------------------------
541
542#Method SkAlphaType alphaType() const
Cary Clark78de7512018-02-07 07:27:09 -0500543#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500544#Line # returns Image_Info Alpha_Type ##
Cary Clark681287e2018-03-16 11:34:15 -0400545Returns Alpha_Type, one of: #list_of_alpha_types#.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400546
547#Return Alpha_Type in Image_Info ##
548
549#Example
550 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
551 SkPixmap pixmap(SkImageInfo::MakeA8(16, 32), nullptr, 64);
552 SkDebugf("alpha type: k" "%s" "_SkAlphaType\n", alphas[pixmap.alphaType()]);
553#StdOut
554alpha type: kPremul_SkAlphaType
555##
556##
557
558#SeeAlso colorType() SkImageInfo::alphaType
559
560##
561
562# ------------------------------------------------------------------------------
563
564#Method SkColorSpace* colorSpace() const
Cary Clark78de7512018-02-07 07:27:09 -0500565#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500566#Line # returns Image_Info Color_Space ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400567Returns Color_Space, the range of colors, associated with Image_Info. The
568reference count of Color_Space is unchanged. The returned Color_Space is
569immutable.
570
Cary Clark2f466242017-12-11 16:03:17 -0500571#Return Color_Space in Image_Info, or nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400572
573#Example
574#Description
575SkColorSpace::MakeSRGBLinear creates Color_Space with linear gamma
576and an sRGB gamut. This Color_Space gamma is not close to sRGB gamma.
577##
Ben Wagner29380bd2017-10-09 14:43:00 -0400578 SkBitmap bitmap;
Cary Clark682c58d2018-05-16 07:07:07 -0400579 bitmap.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
Cary Clarkbc5697d2017-10-04 14:31:33 -0400580 SkColorSpace::MakeSRGBLinear()));
581 SkColorSpace* colorSpace = bitmap.colorSpace();
582 SkDebugf("gammaCloseToSRGB: %s gammaIsLinear: %s isSRGB: %s\n",
583 colorSpace->gammaCloseToSRGB() ? "true" : "false",
584 colorSpace->gammaIsLinear() ? "true" : "false",
585 colorSpace->isSRGB() ? "true" : "false");
586#StdOut
587gammaCloseToSRGB: false gammaIsLinear: true isSRGB: false
588##
589##
590
591#SeeAlso Color_Space SkImageInfo::colorSpace
592
593##
594
595# ------------------------------------------------------------------------------
596
597#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark78de7512018-02-07 07:27:09 -0500598#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500599#Line # returns Image_Info Color_Space ##
Cary Clark681287e2018-03-16 11:34:15 -0400600Returns smart pointer to Color_Space, the range of colors, associated with
Cary Clarkbc5697d2017-10-04 14:31:33 -0400601Image_Info. The smart pointer tracks the number of objects sharing this
602Color_Space reference so the memory is released when the owners destruct.
603
604The returned Color_Space is immutable.
605
606#Return Color_Space in Image_Info wrapped in a smart pointer ##
607
608#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400609 SkBitmap bitmap1, bitmap2;
Cary Clark682c58d2018-05-16 07:07:07 -0400610 bitmap1.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
Cary Clarkbc5697d2017-10-04 14:31:33 -0400611 SkColorSpace::MakeSRGBLinear()));
612 bitmap2.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
613 bitmap1.refColorSpace()));
614 SkColorSpace* colorSpace = bitmap2.colorSpace();
615 SkDebugf("gammaCloseToSRGB: %s gammaIsLinear: %s isSRGB: %s\n",
616 colorSpace->gammaCloseToSRGB() ? "true" : "false",
617 colorSpace->gammaIsLinear() ? "true" : "false",
618 colorSpace->isSRGB() ? "true" : "false");
619#StdOut
620gammaCloseToSRGB: false gammaIsLinear: true isSRGB: false
621##
622##
623
624#SeeAlso Color_Space SkImageInfo::colorSpace
625
626##
627
628# ------------------------------------------------------------------------------
629
630#Method int bytesPerPixel() const
Cary Clark78de7512018-02-07 07:27:09 -0500631#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500632#Line # returns number of bytes in pixel based on Color_Type ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400633Returns number of bytes per pixel required by Color_Type.
634Returns zero if colorType( is kUnknown_SkColorType.
635
636#Return bytes in pixel ##
637
638#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500639 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
640 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clarkbc5697d2017-10-04 14:31:33 -0400641 SkImageInfo info = SkImageInfo::MakeA8(1, 1);
642 SkBitmap bitmap;
Cary Clark1a8d7622018-03-05 13:26:16 -0500643 for (SkColorType colorType : { #list_of_color_types#
644 } ) {
Cary Clarkbc5697d2017-10-04 14:31:33 -0400645 bitmap.setInfo(info.makeColorType(colorType));
646 SkDebugf("color: k" "%s" "_SkColorType" "%*s" "bytesPerPixel: %d\n",
Cary Clarkab2621d2018-01-30 10:08:57 -0500647 colors[colorType], 13 - strlen(colors[colorType]), " ",
Cary Clarkbc5697d2017-10-04 14:31:33 -0400648 bitmap.bytesPerPixel());
649 }
650#StdOut
Cary Clark98aebac2018-03-13 09:02:35 -0400651color: kUnknown_SkColorType bytesPerPixel: 0
652color: kAlpha_8_SkColorType bytesPerPixel: 1
653color: kRGB_565_SkColorType bytesPerPixel: 2
654color: kARGB_4444_SkColorType bytesPerPixel: 2
655color: kRGBA_8888_SkColorType bytesPerPixel: 4
656color: kRGB_888x_SkColorType bytesPerPixel: 4
657color: kBGRA_8888_SkColorType bytesPerPixel: 4
658color: kRGBA_1010102_SkColorType bytesPerPixel: 4
659color: kRGB_101010x_SkColorType bytesPerPixel: 4
660color: kGray_8_SkColorType bytesPerPixel: 1
Cary Clarkab2621d2018-01-30 10:08:57 -0500661color: kRGBA_F16_SkColorType bytesPerPixel: 8
Cary Clarkbc5697d2017-10-04 14:31:33 -0400662##
663##
664
Cary Clark681287e2018-03-16 11:34:15 -0400665#SeeAlso rowBytes rowBytesAsPixels width shiftPerPixel SkImageInfo::bytesPerPixel
Cary Clarkbc5697d2017-10-04 14:31:33 -0400666
667##
668
669# ------------------------------------------------------------------------------
670
671#Method int rowBytesAsPixels() const
Cary Clark78de7512018-02-07 07:27:09 -0500672#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500673#Line # returns interval between rows in pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400674Returns number of pixels that fit on row. Should be greater than or equal to
675width().
676
677#Return maximum pixels per row ##
678
679#Example
680 SkBitmap bitmap;
681 for (int rowBytes : { 4, 5, 6, 7, 8} ) {
682 bitmap.setInfo(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), rowBytes);
683 SkDebugf("rowBytes: %d rowBytesAsPixels: %d\n", rowBytes, bitmap.rowBytesAsPixels());
684 }
685#StdOut
686rowBytes: 4 rowBytesAsPixels: 1
687rowBytes: 5 rowBytesAsPixels: 1
688rowBytes: 6 rowBytesAsPixels: 1
689rowBytes: 7 rowBytesAsPixels: 1
690rowBytes: 8 rowBytesAsPixels: 2
691##
692##
693
694#SeeAlso rowBytes shiftPerPixel width bytesPerPixel
695
696##
697
698# ------------------------------------------------------------------------------
699
700#Method int shiftPerPixel() const
Cary Clark78de7512018-02-07 07:27:09 -0500701#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500702#Line # returns bit shift from pixels to bytes ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400703Returns bit shift converting row bytes to row pixels.
704Returns zero for kUnknown_SkColorType.
705
706#Return one of: 0, 1, 2, 3; left shift to convert pixels to bytes ##
707
708#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500709 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
710 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clarkbc5697d2017-10-04 14:31:33 -0400711 SkImageInfo info = SkImageInfo::MakeA8(1, 1);
712 SkBitmap bitmap;
Cary Clark1a8d7622018-03-05 13:26:16 -0500713 for (SkColorType colorType : { #list_of_color_types#
714 } ) {
Cary Clarkbc5697d2017-10-04 14:31:33 -0400715 bitmap.setInfo(info.makeColorType(colorType));
716 SkDebugf("color: k" "%s" "_SkColorType" "%*s" "shiftPerPixel: %d\n",
Cary Clark1a8d7622018-03-05 13:26:16 -0500717 colors[colorType], 14 - strlen(colors[colorType]), " ",
Cary Clarkbc5697d2017-10-04 14:31:33 -0400718 bitmap.shiftPerPixel());
719 }
720#StdOut
Cary Clark98aebac2018-03-13 09:02:35 -0400721color: kUnknown_SkColorType shiftPerPixel: 0
722color: kAlpha_8_SkColorType shiftPerPixel: 0
723color: kRGB_565_SkColorType shiftPerPixel: 1
724color: kARGB_4444_SkColorType shiftPerPixel: 1
725color: kRGBA_8888_SkColorType shiftPerPixel: 2
726color: kRGB_888x_SkColorType shiftPerPixel: 2
727color: kBGRA_8888_SkColorType shiftPerPixel: 2
728color: kRGBA_1010102_SkColorType shiftPerPixel: 2
729color: kRGB_101010x_SkColorType shiftPerPixel: 2
730color: kGray_8_SkColorType shiftPerPixel: 0
Cary Clark1a8d7622018-03-05 13:26:16 -0500731color: kRGBA_F16_SkColorType shiftPerPixel: 3
Cary Clarkbc5697d2017-10-04 14:31:33 -0400732##
733##
734
735#SeeAlso rowBytes rowBytesAsPixels width bytesPerPixel
736
737##
738
739# ------------------------------------------------------------------------------
740
741#Method bool empty() const
Cary Clark78de7512018-02-07 07:27:09 -0500742#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500743#Line # returns true if Image_Info has zero width() or height() ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400744Returns true if either width() or height() are zero.
745
746Does not check if Pixel_Ref is nullptr; call drawsNothing to check width(),
747height(), and Pixel_Ref.
748
749#Return true if dimensions do not enclose area ##
750
751#Example
752 SkBitmap bitmap;
753 for (int width : { 0, 2 } ) {
754 for (int height : { 0, 2 } ) {
755 bitmap.setInfo(SkImageInfo::MakeA8(width, height));
756 SkDebugf("width: %d height: %d empty: %s\n", width, height,
757 bitmap.empty() ? "true" : "false");
758 }
759 }
760#StdOut
761width: 0 height: 0 empty: true
762width: 0 height: 2 empty: true
763width: 2 height: 0 empty: true
764width: 2 height: 2 empty: false
765##
766##
767
Cary Clark682c58d2018-05-16 07:07:07 -0400768#SeeAlso height() width() drawsNothing
Cary Clarkbc5697d2017-10-04 14:31:33 -0400769
770##
771
772# ------------------------------------------------------------------------------
773
774#Method bool isNull() const
Cary Clark78de7512018-02-07 07:27:09 -0500775#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500776#Line # returns true if Pixel_Ref is nullptr ##
Cary Clark80247e52018-07-11 16:18:41 -0400777Returns true if Pixel_Ref is nullptr.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400778
779Does not check if width() or height() are zero; call drawsNothing to check
780width(), height(), and Pixel_Ref.
781
782#Return true if no Pixel_Ref is associated ##
783
784#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400785 SkBitmap bitmap;
786 SkDebugf("empty bitmap does %shave pixels\n", bitmap.isNull() ? "not " : "");
787 bitmap.setInfo(SkImageInfo::MakeA8(8, 8));
788 SkDebugf("bitmap with dimensions does %shave pixels\n", bitmap.isNull() ? "not " : "");
789 bitmap.allocPixels();
790 SkDebugf("allocated bitmap does %shave pixels\n", bitmap.isNull() ? "not " : "");
Cary Clarkbc5697d2017-10-04 14:31:33 -0400791#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400792empty bitmap does not have pixels
793bitmap with dimensions does not have pixels
Cary Clarkbc5697d2017-10-04 14:31:33 -0400794allocated bitmap does have pixels
795##
796##
797
798#SeeAlso empty() drawsNothing pixelRef
799
800##
801
802# ------------------------------------------------------------------------------
803
804#Method bool drawsNothing() const
Cary Clark78de7512018-02-07 07:27:09 -0500805#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500806#Line # returns true if no width(), no height(), or no Pixel_Ref ##
Cary Clark80247e52018-07-11 16:18:41 -0400807Returns true if width() or height() are zero, or if Pixel_Ref is nullptr.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400808If true, Bitmap has no effect when drawn or drawn into.
809
810#Return true if drawing has no effect ##
811
812#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400813 SkBitmap bitmap;
814 for (int w : { 0, 8 } ) {
815 for (bool allocate : { false, true} ) {
816 bitmap.setInfo(SkImageInfo::MakeA8(w, 8));
817 allocate ? bitmap.allocPixels() : (void) 0 ;
818 SkDebugf("empty:%s isNull:%s drawsNothing:%s\n", bitmap.empty() ? "true " : "false",
819 bitmap.isNull() ? "true " : "false", bitmap.drawsNothing() ? "true" : "false");
820 }
821 }
822#StdOut
823empty:true isNull:true drawsNothing:true
824empty:true isNull:false drawsNothing:true
825empty:false isNull:true drawsNothing:true
826empty:false isNull:false drawsNothing:false
827##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400828##
829
830#SeeAlso empty() isNull pixelRef
831
832##
833
834# ------------------------------------------------------------------------------
835
836#Method size_t rowBytes() const
Cary Clark78de7512018-02-07 07:27:09 -0500837#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500838#Line # returns interval between rows in bytes ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400839Returns row bytes, the interval from one pixel row to the next. Row bytes
Cary Clark2be81cf2018-09-13 12:04:30 -0400840is at least as large as: #Formula # width() * info().bytesPerPixel() ##.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400841
842Returns zero if colorType is kUnknown_SkColorType, or if row bytes supplied to
843setInfo is not large enough to hold a row of pixels.
844
845#Return byte length of pixel row ##
846
847#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400848 SkBitmap bitmap;
849 for (int rowBytes : { 2, 8 } ) {
850 bool result = bitmap.setInfo(SkImageInfo::MakeA8(4, 4), rowBytes);
851 SkDebugf("setInfo returned:%s rowBytes:%d\n", result ? "true " : "false", bitmap.rowBytes());
852 }
853#StdOut
854setInfo returned:false rowBytes:0
855setInfo returned:true rowBytes:8
856##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400857##
858
859#SeeAlso info() setInfo SkImageInfo::minRowBytes
860
861##
862
863# ------------------------------------------------------------------------------
864
865#Method bool setAlphaType(SkAlphaType alphaType)
Cary Clark78de7512018-02-07 07:27:09 -0500866#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -0500867#Line # sets Alpha_Type of shared pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400868Sets Alpha_Type, if alphaType is compatible with Color_Type.
869Returns true unless alphaType is kUnknown_SkAlphaType and current Alpha_Type
870is not kUnknown_SkAlphaType.
871
872Returns true if Color_Type is kUnknown_SkColorType. alphaType is ignored, and
873Alpha_Type remains kUnknown_SkAlphaType.
874
875Returns true if Color_Type is kRGB_565_SkColorType or kGray_8_SkColorType.
876alphaType is ignored, and Alpha_Type remains kOpaque_SkAlphaType.
877
878If Color_Type is kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
879kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: returns true unless
880alphaType is kUnknown_SkAlphaType and Alpha_Type is not kUnknown_SkAlphaType.
881If Alpha_Type is kUnknown_SkAlphaType, alphaType is ignored.
882
883If Color_Type is kAlpha_8_SkColorType, returns true unless
884alphaType is kUnknown_SkAlphaType and Alpha_Type is not kUnknown_SkAlphaType.
885If Alpha_Type is kUnknown_SkAlphaType, alphaType is ignored. If alphaType is
Cary Clark682c58d2018-05-16 07:07:07 -0400886kUnpremul_SkAlphaType, it is treated as kPremul_SkAlphaType.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400887
888This changes Alpha_Type in Pixel_Ref; all bitmaps sharing Pixel_Ref
889are affected.
890
Cary Clark681287e2018-03-16 11:34:15 -0400891#Param alphaType one of: #list_of_alpha_types#
Cary Clarkbc5697d2017-10-04 14:31:33 -0400892##
893
894#Return true if Alpha_Type is set ##
895
896#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400897void draw(SkCanvas* canvas) {
Cary Clarkab2621d2018-01-30 10:08:57 -0500898 const char* colors[] = { "Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
899 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16" };
Cary Clark682c58d2018-05-16 07:07:07 -0400900 const char* alphas[] = {"Unknown ", "Opaque ", "Premul ", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500901 SkBitmap bitmap;
Cary Clark681287e2018-03-16 11:34:15 -0400902 SkAlphaType alphaTypes[] = { #list_of_alpha_types#
903 };
Cary Clarkab2621d2018-01-30 10:08:57 -0500904 SkDebugf("%88s", "Canonical Unknown Opaque Premul Unpremul\n");
Cary Clark1a8d7622018-03-05 13:26:16 -0500905 for (SkColorType colorType : { #list_of_color_types#
906 } ) {
Ben Wagner29380bd2017-10-09 14:43:00 -0400907 for (SkAlphaType canonicalAlphaType : alphaTypes) {
908 SkColorTypeValidateAlphaType(colorType, kUnknown_SkAlphaType, &canonicalAlphaType );
909 SkDebugf("%10s %10s ", colors[(int) colorType], alphas[(int) canonicalAlphaType ]);
910 for (SkAlphaType alphaType : alphaTypes) {
911 bitmap.setInfo(SkImageInfo::Make(4, 4, colorType, canonicalAlphaType));
912 bool result = bitmap.setAlphaType(alphaType);
913 SkDebugf("%s %s ", result ? "true " : "false", alphas[(int) bitmap.alphaType()]);
914 }
915 SkDebugf("\n");
916 }
917 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400918}
919##
920
921#SeeAlso Alpha_Type Color_Type Image_Info setInfo
922
923##
924
925# ------------------------------------------------------------------------------
926
927#Method void* getPixels() const
Cary Clark78de7512018-02-07 07:27:09 -0500928#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500929#Line # returns address of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400930Returns pixel address, the base address corresponding to the pixel origin.
931
932#Return pixel address ##
933
934#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400935 SkBitmap bitmap;
936 bitmap.setInfo(SkImageInfo::MakeN32(4, 4, kPremul_SkAlphaType));
937 bitmap.allocPixels();
938 bitmap.eraseColor(0x00000000);
939 void* baseAddr = bitmap.getPixels();
940 *(SkPMColor*)baseAddr = 0xFFFFFFFF;
941 SkDebugf("bitmap.getColor(0, 1) %c= 0x00000000\n",
942 bitmap.getColor(0, 1) == 0x00000000 ? '=' : '!');
943 SkDebugf("bitmap.getColor(0, 0) %c= 0xFFFFFFFF\n",
944 bitmap.getColor(0, 0) == 0xFFFFFFFF ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -0400945#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400946bitmap.getColor(0, 1) == 0x00000000
Cary Clarkbc5697d2017-10-04 14:31:33 -0400947bitmap.getColor(0, 0) == 0xFFFFFFFF
948##
949##
950
951#SeeAlso isNull drawsNothing
952
953##
954
955# ------------------------------------------------------------------------------
956
957#Method size_t computeByteSize() const
Cary Clark78de7512018-02-07 07:27:09 -0500958#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -0500959#Line # returns size required for pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400960Returns minimum memory required for pixel storage.
961Does not include unused memory on last row when rowBytesAsPixels exceeds width().
962Returns zero if result does not fit in size_t.
963Returns zero if height() or width() is 0.
964Returns height() times rowBytes if colorType is kUnknown_SkColorType.
965
966#Return size in bytes of image buffer ##
967
968#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400969 SkBitmap bitmap;
Cary Clarkbc5697d2017-10-04 14:31:33 -0400970 for (int width : { 1, 1000, 1000000 } ) {
971 for (int height: { 1, 1000, 1000000 } ) {
972 SkImageInfo imageInfo = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
973 bitmap.setInfo(imageInfo, width * 5);
974 SkDebugf("width: %7d height: %7d computeByteSize: %13lld\n", width, height,
975 bitmap.computeByteSize());
976 }
977 }
978#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400979width: 1 height: 1 computeByteSize: 4
980width: 1 height: 1000 computeByteSize: 4999
981width: 1 height: 1000000 computeByteSize: 4999999
982width: 1000 height: 1 computeByteSize: 4000
983width: 1000 height: 1000 computeByteSize: 4999000
984width: 1000 height: 1000000 computeByteSize: 4999999000
985width: 1000000 height: 1 computeByteSize: 4000000
986width: 1000000 height: 1000 computeByteSize: 4999000000
Cary Clarkbc5697d2017-10-04 14:31:33 -0400987width: 1000000 height: 1000000 computeByteSize: 4999999000000
988##
989##
990
991#SeeAlso SkImageInfo::computeByteSize
992
993##
994
995# ------------------------------------------------------------------------------
996
Cary Clarkbc5697d2017-10-04 14:31:33 -0400997#Method bool isImmutable() const
Cary Clark78de7512018-02-07 07:27:09 -0500998#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500999#Line # returns true if pixels will not change ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001000Returns true if pixels can not change.
1001
1002Most immutable Bitmap checks trigger an assert only on debug builds.
1003
1004#Return true if pixels are immutable ##
1005
1006#Example
Cary Clark682c58d2018-05-16 07:07:07 -04001007 SkBitmap original;
Ben Wagner29380bd2017-10-09 14:43:00 -04001008 SkImageInfo info = SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1009 if (original.tryAllocPixels(info)) {
1010 original.setImmutable();
1011 SkBitmap copy;
Cary Clark682c58d2018-05-16 07:07:07 -04001012 original.extractSubset(&copy, {5, 10, 15, 20});
Ben Wagner29380bd2017-10-09 14:43:00 -04001013 SkDebugf("original is " "%s" "immutable\n", original.isImmutable() ? "" : "not ");
1014 SkDebugf("copy is " "%s" "immutable\n", copy.isImmutable() ? "" : "not ");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001015 }
1016#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001017original is immutable
Cary Clarkbc5697d2017-10-04 14:31:33 -04001018copy is immutable
1019##
1020##
1021
1022#SeeAlso setImmutable SkPixelRef::isImmutable SkImage
1023
1024##
1025
1026# ------------------------------------------------------------------------------
1027
1028#Method void setImmutable()
Cary Clark78de7512018-02-07 07:27:09 -05001029#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05001030#Line # marks that pixels will not change ##
Cary Clark154beea2017-10-26 07:58:48 -04001031Sets internal flag to mark Bitmap as immutable. Once set, pixels can not change.
Cary Clark682c58d2018-05-16 07:07:07 -04001032Any other bitmap sharing the same Pixel_Ref are also marked as immutable.
Cary Clark154beea2017-10-26 07:58:48 -04001033Once Pixel_Ref is marked immutable, the setting cannot be cleared.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001034
1035Writing to immutable Bitmap pixels triggers an assert on debug builds.
Cary Clark682c58d2018-05-16 07:07:07 -04001036
Cary Clarkbc5697d2017-10-04 14:31:33 -04001037#Example
1038#Description
1039Triggers assert if SK_DEBUG is true, runs fine otherwise.
1040##
Ben Wagner29380bd2017-10-09 14:43:00 -04001041 SkBitmap bitmap;
1042 bitmap.setInfo(SkImageInfo::MakeN32(4, 4, kPremul_SkAlphaType));
1043 bitmap.allocPixels();
1044 SkCanvas offscreen(bitmap);
1045 SkDebugf("draw white\n");
1046 offscreen.clear(SK_ColorWHITE);
1047 bitmap.setImmutable();
1048 SkDebugf("draw black\n");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001049 offscreen.clear(SK_ColorBLACK);
1050##
1051
1052#SeeAlso isImmutable SkPixelRef::setImmutable SkImage
1053
1054##
1055
1056# ------------------------------------------------------------------------------
1057
1058#Method bool isOpaque() const
Cary Clark78de7512018-02-07 07:27:09 -05001059#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001060#Line # returns true if Image_Info describes opaque pixels ##
Cary Clark681287e2018-03-16 11:34:15 -04001061
1062Returns true if Alpha_Type is set to hint that all pixels are opaque; their
1063Color_Alpha value is implicitly or explicitly 1.0. If true, and all pixels are
Cary Clark682c58d2018-05-16 07:07:07 -04001064not opaque, Skia may draw incorrectly.
Cary Clark681287e2018-03-16 11:34:15 -04001065
Cary Clarkbc5697d2017-10-04 14:31:33 -04001066Does not check if Color_Type allows Alpha, or if any pixel value has
1067transparency.
1068
Cary Clark681287e2018-03-16 11:34:15 -04001069#Return true if Image_Info Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001070
1071#Example
1072#Description
1073 isOpaque ignores whether all pixels are opaque or not.
1074##
Ben Wagner29380bd2017-10-09 14:43:00 -04001075 const int height = 2;
1076 const int width = 2;
1077 SkBitmap bitmap;
1078 bitmap.setInfo(SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType));
1079 for (int index = 0; index < 2; ++index) {
1080 bitmap.allocPixels();
1081 bitmap.eraseColor(0x00000000);
1082 SkDebugf("isOpaque: %s\n", bitmap.isOpaque() ? "true" : "false");
1083 bitmap.eraseColor(0xFFFFFFFF);
1084 SkDebugf("isOpaque: %s\n", bitmap.isOpaque() ? "true" : "false");
1085 bitmap.setInfo(bitmap.info().makeAlphaType(kOpaque_SkAlphaType));
1086 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001087#StdOut
1088isOpaque: false
1089isOpaque: false
1090isOpaque: true
1091isOpaque: true
1092##
1093##
1094
1095#SeeAlso ComputeIsOpaque SkImageInfo::isOpaque
1096
1097##
1098
1099# ------------------------------------------------------------------------------
1100
1101#Method bool isVolatile() const
Cary Clark78de7512018-02-07 07:27:09 -05001102#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001103#Line # returns true if pixels should not be cached ##
Cary Clark80247e52018-07-11 16:18:41 -04001104Provides a hint to caller that pixels should not be cached. Only true if
1105setIsVolatile has been called to mark as volatile.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001106
1107Volatile state is not shared by other bitmaps sharing the same Pixel_Ref.
1108
1109#Return true if marked volatile ##
1110
1111#Example
Cary Clark682c58d2018-05-16 07:07:07 -04001112 SkBitmap original;
Ben Wagner29380bd2017-10-09 14:43:00 -04001113 SkImageInfo info = SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1114 if (original.tryAllocPixels(info)) {
1115 original.setIsVolatile(true);
1116 SkBitmap copy;
Cary Clark682c58d2018-05-16 07:07:07 -04001117 original.extractSubset(&copy, {5, 10, 15, 20});
Ben Wagner29380bd2017-10-09 14:43:00 -04001118 SkDebugf("original is " "%s" "volatile\n", original.isVolatile() ? "" : "not ");
1119 SkDebugf("copy is " "%s" "volatile\n", copy.isImmutable() ? "" : "not ");
1120 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001121#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001122original is volatile
Cary Clarkbc5697d2017-10-04 14:31:33 -04001123copy is not volatile
1124##
1125##
1126
1127#SeeAlso setIsVolatile
1128
1129##
1130
1131# ------------------------------------------------------------------------------
1132
1133#Method void setIsVolatile(bool isVolatile)
Cary Clark78de7512018-02-07 07:27:09 -05001134#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05001135#Line # marks if pixels should not be cached ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001136Sets if pixels should be read from Pixel_Ref on every access. Bitmaps are not
1137volatile by default; a GPU back end may upload pixel values expecting them to be
1138accessed repeatedly. Marking temporary Bitmaps as volatile provides a hint to
1139Device that the Bitmap pixels should not be cached. This can
1140improve performance by avoiding overhead and reducing resource
1141consumption on Device.
1142
1143#Param isVolatile true if backing pixels are temporary ##
1144
1145#Example
1146#Height 20
Cary Clark682c58d2018-05-16 07:07:07 -04001147 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04001148 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
1149 bitmap.allocPixels();
1150 bitmap.eraseColor(SK_ColorRED);
1151 canvas->scale(16, 16);
1152 canvas->drawBitmap(bitmap, 0, 0);
1153 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorBLUE);
1154 canvas->drawBitmap(bitmap, 2, 0);
1155 bitmap.setIsVolatile(true);
1156 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorGREEN);
1157 canvas->drawBitmap(bitmap, 4, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001158##
1159
1160#SeeAlso isVolatile
1161
1162##
1163
1164# ------------------------------------------------------------------------------
1165
1166#Method void reset()
Cary Clark78de7512018-02-07 07:27:09 -05001167#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -05001168#Line # sets to default values, releases pixel ownership ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001169Resets to its initial state; all fields are set to zero, as if Bitmap had
1170been initialized by SkBitmap().
1171
Cary Clark682c58d2018-05-16 07:07:07 -04001172Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to
Cary Clarkbc5697d2017-10-04 14:31:33 -04001173kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType.
1174
1175If Pixel_Ref is allocated, its reference count is decreased by one, releasing
1176its memory if Bitmap is the sole owner.
1177
1178#Example
Cary Clark682c58d2018-05-16 07:07:07 -04001179 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04001180 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
1181 bitmap.allocPixels();
1182 SkDebugf("width:%d height:%d isNull:%s\n", bitmap.width(), bitmap.height(),
1183 bitmap.isNull() ? "true" : "false");
1184 bitmap.reset();
1185 SkDebugf("width:%d height:%d isNull:%s\n", bitmap.width(), bitmap.height(),
1186 bitmap.isNull() ? "true" : "false");
1187#StdOut
1188width:1 height:1 isNull:false
1189width:0 height:0 isNull:true
1190##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001191##
1192
1193#SeeAlso SkBitmap() SkAlphaType SkColorType
1194
1195##
1196
1197# ------------------------------------------------------------------------------
1198
1199#Method static bool ComputeIsOpaque(const SkBitmap& bm)
Cary Clark78de7512018-02-07 07:27:09 -05001200#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05001201#Line # returns true if all pixels are opaque ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001202Returns true if all pixels are opaque. Color_Type determines how pixels
1203are encoded, and whether pixel describes Alpha. Returns true for Color_Types
1204without alpha in each pixel; for other Color_Types, returns true if all
1205pixels have alpha values equivalent to 1.0 or greater.
1206
1207For Color_Types kRGB_565_SkColorType or kGray_8_SkColorType: always
1208returns true. For Color_Types kAlpha_8_SkColorType, kBGRA_8888_SkColorType,
1209kRGBA_8888_SkColorType: returns true if all pixel Alpha values are 255.
1210For Color_Type kARGB_4444_SkColorType: returns true if all pixel Alpha values are 15.
1211For kRGBA_F16_SkColorType: returns true if all pixel Alpha values are 1.0 or
1212greater.
1213
Cary Clark682c58d2018-05-16 07:07:07 -04001214Returns false for kUnknown_SkColorType.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001215
1216#Param bm Bitmap to check ##
1217
1218#Return true if all pixels have opaque values or Color_Type is opaque ##
1219
1220#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001221 SkBitmap bitmap;
1222 bitmap.setInfo(SkImageInfo::Make(2, 2, kN32_SkColorType, kPremul_SkAlphaType));
1223 for (int index = 0; index < 2; ++index) {
1224 bitmap.allocPixels();
1225 bitmap.eraseColor(0x00000000);
1226 SkDebugf("computeIsOpaque: %s\n", SkBitmap::ComputeIsOpaque(bitmap) ? "true" : "false");
1227 bitmap.eraseColor(0xFFFFFFFF);
1228 SkDebugf("computeIsOpaque: %s\n", SkBitmap::ComputeIsOpaque(bitmap) ? "true" : "false");
1229 bitmap.setInfo(bitmap.info().makeAlphaType(kOpaque_SkAlphaType));
Cary Clarkbc5697d2017-10-04 14:31:33 -04001230 }
1231#StdOut
1232computeIsOpaque: false
1233computeIsOpaque: true
1234computeIsOpaque: false
1235computeIsOpaque: true
1236##
1237##
1238
1239#SeeAlso isOpaque Color_Type Alpha
1240
1241##
1242
1243# ------------------------------------------------------------------------------
1244
1245#Method void getBounds(SkRect* bounds) const
Cary Clark78de7512018-02-07 07:27:09 -05001246#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001247#Line # returns width() and height() as Rectangle ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001248Returns Rect { 0, 0, width(), height() }.
1249
1250#Param bounds container for floating point rectangle ##
1251
1252#Example
1253#Height 160
1254#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001255 SkRect bounds;
1256 source.getBounds(&bounds);
1257 bounds.offset(100, 100);
1258 SkPaint paint;
1259 paint.setColor(SK_ColorGRAY);
1260 canvas->scale(.25f, .25f);
1261 canvas->drawRect(bounds, paint);
1262 canvas->drawBitmap(source, 40, 40);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001263##
1264
Cary Clark682c58d2018-05-16 07:07:07 -04001265#SeeAlso bounds()
Cary Clarkbc5697d2017-10-04 14:31:33 -04001266
1267##
1268
1269# ------------------------------------------------------------------------------
1270
1271#Method void getBounds(SkIRect* bounds) const
1272
1273Returns IRect { 0, 0, width(), height() }.
1274
1275#Param bounds container for integral rectangle ##
1276
1277#Example
1278#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001279 SkIRect bounds;
1280 source.getBounds(&bounds);
1281 bounds.inset(100, 100);
1282 SkBitmap bitmap;
1283 source.extractSubset(&bitmap, bounds);
1284 canvas->scale(.5f, .5f);
1285 canvas->drawBitmap(bitmap, 10, 10);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001286##
1287
Cary Clark682c58d2018-05-16 07:07:07 -04001288#SeeAlso bounds()
Cary Clarkbc5697d2017-10-04 14:31:33 -04001289
1290##
1291
1292# ------------------------------------------------------------------------------
1293
1294#Method SkIRect bounds() const
Cary Clark78de7512018-02-07 07:27:09 -05001295#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001296#Line # returns width() and height() as Rectangle ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001297Returns IRect { 0, 0, width(), height() }.
1298
1299#Return integral rectangle from origin to width() and height() ##
1300
1301#Example
Cary Clark681287e2018-03-16 11:34:15 -04001302#Height 64
Cary Clarkbc5697d2017-10-04 14:31:33 -04001303#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -05001304 canvas->scale(.5f, .5f);
Ben Wagner29380bd2017-10-09 14:43:00 -04001305 SkIRect bounds = source.bounds();
1306 for (int x : { 0, bounds.width() } ) {
1307 for (int y : { 0, bounds.height() } ) {
1308 canvas->drawBitmap(source, x, y);
1309 }
1310 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001311##
1312
Cary Clark682c58d2018-05-16 07:07:07 -04001313#SeeAlso getBounds
Cary Clarkbc5697d2017-10-04 14:31:33 -04001314
1315##
1316
1317# ------------------------------------------------------------------------------
1318
1319#Method SkISize dimensions() const
Cary Clark78de7512018-02-07 07:27:09 -05001320#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001321#Line # returns width() and height() ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001322Returns ISize { width(), height() }.
1323
1324#Return integral size of width() and height() ##
1325
1326#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001327 SkBitmap bitmap;
1328 bitmap.setInfo(SkImageInfo::MakeN32(33, 55, kOpaque_SkAlphaType));
1329 SkISize dimensions = bitmap.dimensions();
1330 SkRect bounds;
1331 bitmap.getBounds(&bounds);
1332 SkRect dimensionsAsBounds = SkRect::Make(dimensions);
1333 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -04001334##
1335
1336#SeeAlso height() width()
1337
1338##
1339
1340# ------------------------------------------------------------------------------
1341
1342#Method SkIRect getSubset() const
Cary Clark78de7512018-02-07 07:27:09 -05001343#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001344#Line # returns bounds offset by origin ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001345Returns the bounds of this bitmap, offset by its Pixel_Ref origin.
1346
1347#Return bounds within Pixel_Ref bounds ##
1348
1349#Example
1350#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001351 SkIRect bounds;
1352 source.getBounds(&bounds);
1353 bounds.inset(100, 100);
1354 SkBitmap subset;
1355 source.extractSubset(&subset, bounds);
1356 SkIRect r = source.getSubset();
1357 SkDebugf("source: %d, %d, %d, %d\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
1358 r = subset.getSubset();
1359 SkDebugf("subset: %d, %d, %d, %d\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
1360#StdOut
1361source: 0, 0, 512, 512
1362subset: 100, 100, 412, 412
1363##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001364##
1365
1366#SeeAlso extractSubset getBounds
1367
1368##
1369
1370# ------------------------------------------------------------------------------
1371
1372#Method bool setInfo(const SkImageInfo& imageInfo, size_t rowBytes = 0)
Cary Clark78de7512018-02-07 07:27:09 -05001373#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05001374#Line # sets height, width, Color_Type, and so on, releasing pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001375Sets width, height, Alpha_Type, Color_Type, Color_Space, and optional
1376rowBytes. Frees pixels, and returns true if successful.
1377
1378imageInfo.alphaType may be altered to a value permitted by imageInfo.colorSpace.
Cary Clark682c58d2018-05-16 07:07:07 -04001379If imageInfo.colorType is kUnknown_SkColorType, imageInfo.alphaType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04001380set to kUnknown_SkAlphaType.
1381If imageInfo.colorType is kAlpha_8_SkColorType and imageInfo.alphaType is
1382kUnpremul_SkAlphaType, imageInfo.alphaType is replaced by kPremul_SkAlphaType.
1383If imageInfo.colorType is kRGB_565_SkColorType or kGray_8_SkColorType,
1384imageInfo.alphaType is set to kOpaque_SkAlphaType.
1385If imageInfo.colorType is kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
1386kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: imageInfo.alphaType remains
1387unchanged.
1388
1389rowBytes must equal or exceed imageInfo.minRowBytes. If imageInfo.colorSpace is
1390kUnknown_SkColorType, rowBytes is ignored and treated as zero; for all other
1391Color_Space values, rowBytes of zero is treated as imageInfo.minRowBytes.
1392
1393Calls reset() and returns false if:
1394#List
1395# rowBytes exceeds 31 bits ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001396# imageInfo.width() is negative ##
1397# imageInfo.height() is negative ##
1398# rowBytes is positive and less than imageInfo.width() times imageInfo.bytesPerPixel ##
1399##
1400
1401#Param imageInfo contains width, height, Alpha_Type, Color_Type, Color_Space ##
1402#Param rowBytes imageInfo.minRowBytes or larger; or zero ##
1403
1404#Return true if Image_Info set successfully ##
1405
1406#Example
1407#Height 96
1408###^
Ben Wagner29380bd2017-10-09 14:43:00 -04001409SkBitmap bitmap;
1410bitmap.setInfo(SkImageInfo::MakeN32(44, 16, kOpaque_SkAlphaType));
1411bitmap.allocPixels();
1412bitmap.eraseColor(SK_ColorGREEN);
1413SkCanvas offscreen(bitmap);
1414SkPaint paint;
1415offscreen.drawString("!@#$%", 0, 12, paint);
1416canvas->scale(6, 6);
1417canvas->drawBitmap(bitmap, 0, 0);
1418^^^#
Cary Clarkbc5697d2017-10-04 14:31:33 -04001419##
1420
1421#SeeAlso Alpha_Type Color_Type Color_Space height rowBytes width
1422
1423##
1424
1425# ------------------------------------------------------------------------------
1426
1427#Enum AllocFlags
Cary Clark4855f782018-02-06 09:41:53 -05001428#Line # zero pixel memory ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001429#Code
1430 enum AllocFlags {
1431 kZeroPixels_AllocFlag = 1 << 0,
1432 };
1433##
1434
1435AllocFlags provides the option to zero pixel memory when allocated.
1436
1437#Const kZeroPixels_AllocFlag 1
Cary Clark682c58d2018-05-16 07:07:07 -04001438#Line # zero pixel memory ##
1439 Instructs tryAllocPixelsFlags and allocPixelsFlags to zero pixel memory.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001440##
1441
1442#NoExample
1443##
1444
1445#SeeAlso tryAllocPixelsFlags allocPixelsFlags erase() eraseColor
1446
1447##
1448
1449# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001450#Subtopic Allocate
1451#Populate
1452#Line # allocates storage for pixels ##
1453##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001454
1455#Method bool SK_WARN_UNUSED_RESULT tryAllocPixelsFlags(const SkImageInfo& info, uint32_t flags)
Cary Clark78de7512018-02-07 07:27:09 -05001456#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001457#Line # allocates pixels from Image_Info with options if possible ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001458Sets Image_Info to info following the rules in setInfo and allocates pixel
Cary Clark682c58d2018-05-16 07:07:07 -04001459memory. If flags is kZeroPixels_AllocFlag, memory is zeroed.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001460
1461Returns false and calls reset() if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001462not be allocated, or memory could not optionally be zeroed.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001463
1464On most platforms, allocating pixel memory may succeed even though there is
1465not sufficient memory to hold pixels; allocation does not take place
1466until the pixels are written to. The actual behavior depends on the platform
1467implementation of malloc(), if flags is zero, and calloc(), if flags is
1468kZeroPixels_AllocFlag.
1469
Cary Clark186d08f2018-04-03 08:43:27 -04001470flags set to kZeroPixels_AllocFlag offers equal or better performance than
1471subsequently calling eraseColor with SK_ColorTRANSPARENT.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001472
1473#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1474#Param flags kZeroPixels_AllocFlag, or zero ##
1475
1476#Return true if pixels allocation is successful ##
1477
1478#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001479 SkBitmap bitmap;
Cary Clark682c58d2018-05-16 07:07:07 -04001480 if (!bitmap.tryAllocPixelsFlags(SkImageInfo::MakeN32(10000, 10000, kOpaque_SkAlphaType),
Cary Clarka560c472017-11-27 10:44:06 -05001481 SkBitmap::kZeroPixels_AllocFlag)) {
1482 SkDebugf("bitmap allocation failed!\n");
1483 } else {
1484 SkDebugf("bitmap allocation succeeded!\n");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001485 }
1486#StdOut
Cary Clarka560c472017-11-27 10:44:06 -05001487bitmap allocation succeeded!
Cary Clarkbc5697d2017-10-04 14:31:33 -04001488##
1489##
1490
1491#SeeAlso allocPixelsFlags tryAllocPixels SkMallocPixelRef::MakeZeroed
1492
1493##
1494
1495# ------------------------------------------------------------------------------
1496
1497#Method void allocPixelsFlags(const SkImageInfo& info, uint32_t flags)
Cary Clark78de7512018-02-07 07:27:09 -05001498#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001499#Line # allocates pixels from Image_Info with options, or aborts ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001500Sets Image_Info to info following the rules in setInfo and allocates pixel
Cary Clark682c58d2018-05-16 07:07:07 -04001501memory. If flags is kZeroPixels_AllocFlag, memory is zeroed.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001502
1503Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001504not be allocated, or memory could not optionally
Cary Clarkbc5697d2017-10-04 14:31:33 -04001505be zeroed. Abort steps may be provided by the user at compile time by defining
1506SK_ABORT.
1507
1508On most platforms, allocating pixel memory may succeed even though there is
1509not sufficient memory to hold pixels; allocation does not take place
1510until the pixels are written to. The actual behavior depends on the platform
1511implementation of malloc(), if flags is zero, and calloc(), if flags is
1512kZeroPixels_AllocFlag.
1513
Cary Clark186d08f2018-04-03 08:43:27 -04001514flags set to kZeroPixels_AllocFlag offers equal or better performance than
1515subsequently calling eraseColor with SK_ColorTRANSPARENT.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001516
1517#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1518#Param flags kZeroPixels_AllocFlag, or zero ##
1519
1520#Example
1521#Height 128
1522#Description
1523Text is drawn on a transparent background; drawing the bitmap a second time
1524lets the first draw show through.
1525##
1526###^
Ben Wagner29380bd2017-10-09 14:43:00 -04001527SkBitmap bitmap;
Cary Clark682c58d2018-05-16 07:07:07 -04001528bitmap.allocPixelsFlags(SkImageInfo::MakeN32(44, 16, kPremul_SkAlphaType),
Ben Wagner29380bd2017-10-09 14:43:00 -04001529 SkBitmap::kZeroPixels_AllocFlag);
1530SkCanvas offscreen(bitmap);
1531SkPaint paint;
1532offscreen.drawString("!@#$%", 0, 12, paint);
1533canvas->scale(6, 6);
1534canvas->drawBitmap(bitmap, 0, 0);
1535canvas->drawBitmap(bitmap, 8, 8);
1536^^^#
Cary Clarkbc5697d2017-10-04 14:31:33 -04001537##
1538
1539#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeZeroed
1540
1541##
1542
1543# ------------------------------------------------------------------------------
1544
1545#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info, size_t rowBytes)
Cary Clark78de7512018-02-07 07:27:09 -05001546#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001547#Line # allocates pixels from Image_Info if possible ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001548#ToDo am I ever conflicted about setInfo rules. It needs to be able to be replicated
1549 if, for instance, I generate one-page-per-method HTML-style documentation
1550 I'm not so sure it makes sense to put the indirection in for .h either unless
1551 my mantra is that .h should abbreviate full documentation. And, what to do
1552 for generated markdown? At least there the rules are a click away, although
1553 a pop-down in place would be way better. Hmmm.
1554##
1555
1556Sets Image_Info to info following the rules in setInfo and allocates pixel
1557memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(),
1558or equal zero. Pass in zero for rowBytes to compute the minimum valid value.
1559
1560Returns false and calls reset() if Image_Info could not be set, or memory could
1561not be allocated.
1562
1563On most platforms, allocating pixel memory may succeed even though there is
1564not sufficient memory to hold pixels; allocation does not take place
1565until the pixels are written to. The actual behavior depends on the platform
1566implementation of malloc().
1567
1568#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1569#Param rowBytes size of pixel row or larger; may be zero ##
1570
1571#Return true if pixel storage is allocated ##
1572
1573#Example
1574#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001575SkBitmap bitmap;
1576SkImageInfo info = SkImageInfo::Make(64, 256, kGray_8_SkColorType, kOpaque_SkAlphaType);
1577if (bitmap.tryAllocPixels(info, 0)) {
1578 SkCanvas offscreen(bitmap);
1579 offscreen.scale(.5f, .5f);
1580 for (int x : { 0, 64, 128, 192 } ) {
1581 offscreen.drawBitmap(source, -x, 0);
1582 canvas->drawBitmap(bitmap, x, 0);
1583 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001584}
1585##
1586
1587#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeAllocate
1588
1589##
1590
1591# ------------------------------------------------------------------------------
1592
1593#Method void allocPixels(const SkImageInfo& info, size_t rowBytes)
Cary Clark78de7512018-02-07 07:27:09 -05001594#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001595#Line # allocates pixels from Image_Info, or aborts ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001596Sets Image_Info to info following the rules in setInfo and allocates pixel
1597memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(),
1598or equal zero. Pass in zero for rowBytes to compute the minimum valid value.
1599
1600Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001601not be allocated. Abort steps may be provided by
Cary Clarkbc5697d2017-10-04 14:31:33 -04001602the user at compile time by defining SK_ABORT.
1603
1604On most platforms, allocating pixel memory may succeed even though there is
1605not sufficient memory to hold pixels; allocation does not take place
1606until the pixels are written to. The actual behavior depends on the platform
1607implementation of malloc().
1608
1609#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1610#Param rowBytes size of pixel row or larger; may be zero ##
1611
1612#Example
1613#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001614SkBitmap bitmap;
1615SkImageInfo info = SkImageInfo::Make(256, 64, kGray_8_SkColorType, kOpaque_SkAlphaType);
1616bitmap.allocPixels(info, info.width() * info.bytesPerPixel() + 64);
1617SkCanvas offscreen(bitmap);
1618offscreen.scale(.5f, .5f);
1619for (int y : { 0, 64, 128, 192 } ) {
1620 offscreen.drawBitmap(source, 0, -y);
1621 canvas->drawBitmap(bitmap, 0, y);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001622}
1623##
1624
1625#SeeAlso tryAllocPixels allocPixelsFlags SkMallocPixelRef::MakeAllocate
1626
1627##
1628
1629# ------------------------------------------------------------------------------
1630
1631#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info)
1632
1633Sets Image_Info to info following the rules in setInfo and allocates pixel
Cary Clark682c58d2018-05-16 07:07:07 -04001634memory.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001635
1636Returns false and calls reset() if Image_Info could not be set, or memory could
1637not be allocated.
1638
1639On most platforms, allocating pixel memory may succeed even though there is
1640not sufficient memory to hold pixels; allocation does not take place
1641until the pixels are written to. The actual behavior depends on the platform
1642implementation of malloc().
1643
1644#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1645
1646#Return true if pixel storage is allocated ##
1647
1648#Example
1649#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001650SkBitmap bitmap;
1651if (bitmap.tryAllocPixels(SkImageInfo::Make(64, 64, kGray_8_SkColorType, kOpaque_SkAlphaType))) {
1652 SkCanvas offscreen(bitmap);
1653 offscreen.scale(.25f, .5f);
1654 for (int y : { 0, 64, 128, 192 } ) {
1655 offscreen.drawBitmap(source, -y, -y);
1656 canvas->drawBitmap(bitmap, y, y);
1657 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001658}
1659##
1660
1661#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeAllocate
1662
1663##
1664
1665# ------------------------------------------------------------------------------
1666
1667#Method void allocPixels(const SkImageInfo& info)
1668
1669Sets Image_Info to info following the rules in setInfo and allocates pixel
Cary Clark682c58d2018-05-16 07:07:07 -04001670memory.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001671
1672Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001673not be allocated. Abort steps may be provided by
Cary Clarkbc5697d2017-10-04 14:31:33 -04001674the user at compile time by defining SK_ABORT.
1675
1676On most platforms, allocating pixel memory may succeed even though there is
1677not sufficient memory to hold pixels; allocation does not take place
1678until the pixels are written to. The actual behavior depends on the platform
1679implementation of malloc().
1680
1681#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1682
1683#Example
1684#Image 4
Ben Wagner29380bd2017-10-09 14:43:00 -04001685SkBitmap bitmap;
1686bitmap.allocPixels(SkImageInfo::Make(64, 64, kGray_8_SkColorType, kOpaque_SkAlphaType));
1687SkCanvas offscreen(bitmap);
1688offscreen.scale(.5f, .5f);
1689for (int y : { 0, 64, 128, 192 } ) {
1690 offscreen.drawBitmap(source, -y, -y);
1691 canvas->drawBitmap(bitmap, y, y);
1692}
Cary Clarkbc5697d2017-10-04 14:31:33 -04001693##
1694
1695#SeeAlso tryAllocPixels allocPixelsFlags SkMallocPixelRef::MakeAllocate
1696
1697##
1698
1699# ------------------------------------------------------------------------------
1700
1701#Method bool SK_WARN_UNUSED_RESULT tryAllocN32Pixels(int width, int height, bool isOpaque = false)
Cary Clark78de7512018-02-07 07:27:09 -05001702#In Allocate
Cary Clarkffb3d682018-05-17 12:17:28 -04001703#Line # allocates compatible ARGB pixels if possible ##
Cary Clark682c58d2018-05-16 07:07:07 -04001704Sets Image_Info to width, height, and Native_Color_Type; and allocates
Cary Clarkbc5697d2017-10-04 14:31:33 -04001705pixel memory. If isOpaque is true, sets Image_Info to kOpaque_SkAlphaType;
1706otherwise, sets to kPremul_SkAlphaType.
1707
1708Calls reset() and returns false if width exceeds 29 bits or is negative,
1709or height is negative.
1710
1711Returns false if allocation fails.
1712
Cary Clarka560c472017-11-27 10:44:06 -05001713Use to create Bitmap that matches SkPMColor, the native pixel arrangement on
1714the platform. Bitmap drawn to output device skips converting its pixel format.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001715
1716#Param width pixel column count; must be zero or greater ##
1717#Param height pixel row count; must be zero or greater ##
1718#Param isOpaque true if pixels do not have transparency ##
1719
1720#Return true if pixel storage is allocated ##
1721
1722#Example
1723#Height 160
Ben Wagner29380bd2017-10-09 14:43:00 -04001724 SkBitmap bitmap;
1725 if (bitmap.tryAllocN32Pixels(80, 80)) {
1726 bitmap.eraseColor(SK_ColorTRANSPARENT);
1727 bitmap.erase(0x7f3f7fff, SkIRect::MakeWH(50, 30));
1728 bitmap.erase(0x3f7fff3f, SkIRect::MakeXYWH(20, 10, 50, 30));
1729 bitmap.erase(0x5fff3f7f, SkIRect::MakeXYWH(40, 20, 50, 30));
1730 canvas->drawBitmap(bitmap, 0, 0);
1731 for (int x : { 0, 30, 60, 90 } ) {
1732 canvas->drawBitmap(bitmap, x, 70);
1733 }
1734 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001735##
1736
1737#SeeAlso tryAllocPixels allocN32Pixels SkMallocPixelRef::MakeAllocate
1738
1739##
1740
1741# ------------------------------------------------------------------------------
1742
1743#Method void allocN32Pixels(int width, int height, bool isOpaque = false)
Cary Clark78de7512018-02-07 07:27:09 -05001744#In Allocate
Cary Clarkffb3d682018-05-17 12:17:28 -04001745#Line # allocates compatible ARGB pixels, or aborts ##
Cary Clark682c58d2018-05-16 07:07:07 -04001746Sets Image_Info to width, height, and the Native_Color_Type; and allocates
Cary Clarkbc5697d2017-10-04 14:31:33 -04001747pixel memory. If isOpaque is true, sets Image_Info to kPremul_SkAlphaType;
1748otherwise, sets to kOpaque_SkAlphaType.
1749
1750Aborts if width exceeds 29 bits or is negative, or height is negative, or
1751allocation fails. Abort steps may be provided by the user at compile time by
1752defining SK_ABORT.
1753
Cary Clarka560c472017-11-27 10:44:06 -05001754Use to create Bitmap that matches SkPMColor, the native pixel arrangement on
1755the platform. Bitmap drawn to output device skips converting its pixel format.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001756
1757#Param width pixel column count; must be zero or greater ##
1758#Param height pixel row count; must be zero or greater ##
1759#Param isOpaque true if pixels do not have transparency ##
1760
1761#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001762 SkRandom random;
1763 SkBitmap bitmap;
1764 bitmap.allocN32Pixels(64, 64);
1765 bitmap.eraseColor(SK_ColorTRANSPARENT);
1766 for (int y = 0; y < 256; y += 64) {
1767 for (int x = 0; x < 256; x += 64) {
1768 SkColor color = random.nextU();
1769 uint32_t w = random.nextRangeU(4, 32);
1770 uint32_t cx = random.nextRangeU(0, 64 - w);
1771 uint32_t h = random.nextRangeU(4, 32);
1772 uint32_t cy = random.nextRangeU(0, 64 - h);
1773 bitmap.erase(color, SkIRect::MakeXYWH(cx, cy, w, h));
1774 canvas->drawBitmap(bitmap, x, y);
1775 }
1776 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001777##
1778
1779#SeeAlso allocPixels tryAllocN32Pixels SkMallocPixelRef::MakeAllocate
1780
1781##
1782
1783# ------------------------------------------------------------------------------
1784
1785#Method bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
1786 void (*releaseProc)(void* addr, void* context), void* context)
Cary Clark78de7512018-02-07 07:27:09 -05001787#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001788#Line # creates Pixel_Ref, with optional release function ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001789
1790Sets Image_Info to info following the rules in setInfo, and creates Pixel_Ref
Cary Clark682c58d2018-05-16 07:07:07 -04001791containing pixels and rowBytes. releaseProc, if not nullptr, is called
Cary Clarkbc5697d2017-10-04 14:31:33 -04001792immediately on failure or when pixels are no longer referenced. context may be
1793nullptr.
1794
1795If Image_Info could not be set, or rowBytes is less than info.minRowBytes:
1796calls releaseProc if present, calls reset(), and returns false.
1797
1798Otherwise, if pixels equals nullptr: sets Image_Info, calls releaseProc if
1799present, returns true.
1800
1801If Image_Info is set, pixels is not nullptr, and releaseProc is not nullptr:
1802when pixels are no longer referenced, calls releaseProc with pixels and context
1803as parameters.
1804
1805#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1806#Param pixels address or pixel storage; may be nullptr ##
1807#Param rowBytes size of pixel row or larger ##
1808#Param releaseProc function called when pixels can be deleted; may be nullptr ##
1809#Param context caller state passed to releaseProc; may be nullptr ##
1810
1811#Return true if Image_Info is set to info ##
1812
1813#Example
1814#Description
1815releaseProc is called immediately because rowBytes is too small for Pixel_Ref.
1816##
1817#Function
Ben Wagner29380bd2017-10-09 14:43:00 -04001818static void releaseProc(void* addr, void* ) {
1819 SkDebugf("releaseProc called\n");
Cary Clark682c58d2018-05-16 07:07:07 -04001820 delete[] (uint32_t*) addr;
Ben Wagner29380bd2017-10-09 14:43:00 -04001821}
1822
1823##
1824
1825void draw(SkCanvas* canvas) {
1826 SkBitmap bitmap;
1827 void* pixels = new uint32_t[8 * 8];
1828 SkImageInfo info = SkImageInfo::MakeN32(8, 8, kOpaque_SkAlphaType);
1829 SkDebugf("before installPixels\n");
1830 bool installed = bitmap.installPixels(info, pixels, 16, releaseProc, nullptr);
1831 SkDebugf("install " "%s" "successful\n", installed ? "" : "not ");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001832}
1833#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001834before installPixels
1835releaseProc called
Cary Clarkbc5697d2017-10-04 14:31:33 -04001836install not successful
1837##
1838##
1839
1840#SeeAlso allocPixels
1841
1842##
1843
1844# ------------------------------------------------------------------------------
1845
1846#Method bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes)
1847
1848Sets Image_Info to info following the rules in setInfo, and creates Pixel_Ref
1849containing pixels and rowBytes.
1850
1851If Image_Info could not be set, or rowBytes is less than info.minRowBytes:
1852calls reset(), and returns false.
1853
1854Otherwise, if pixels equals nullptr: sets Image_Info, returns true.
1855
1856Caller must ensure that pixels are valid for the lifetime of Bitmap and Pixel_Ref.
1857
1858#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1859#Param pixels address or pixel storage; may be nullptr ##
1860#Param rowBytes size of pixel row or larger ##
1861
1862#Return true if Image_Info is set to info ##
1863
1864#Example
Cary Clark4855f782018-02-06 09:41:53 -05001865#Bug 7079
Cary Clarkbc5697d2017-10-04 14:31:33 -04001866#Description
Cary Clarkbc5697d2017-10-04 14:31:33 -04001867GPU does not support kUnpremul_SkAlphaType, does not assert that it does not.
1868##
Ben Wagner29380bd2017-10-09 14:43:00 -04001869void draw(SkCanvas* canvas) {
1870 SkRandom random;
1871 SkBitmap bitmap;
1872 const int width = 8;
1873 const int height = 8;
1874 uint32_t pixels[width * height];
1875 for (unsigned x = 0; x < width * height; ++x) {
1876 pixels[x] = random.nextU();
1877 }
1878 SkImageInfo info = SkImageInfo::MakeN32(width, height, kUnpremul_SkAlphaType);
1879 if (bitmap.installPixels(info, pixels, info.minRowBytes())) {
1880 canvas->scale(32, 32);
1881 canvas->drawBitmap(bitmap, 0, 0);
1882 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001883}
1884##
1885
1886#SeeAlso allocPixels
1887
1888##
1889
1890# ------------------------------------------------------------------------------
1891
1892#Method bool installPixels(const SkPixmap& pixmap)
1893
1894Sets Image_Info to pixmap.info() following the rules in setInfo, and creates
1895Pixel_Ref containing pixmap.addr() and pixmap.rowBytes.
1896
1897If Image_Info could not be set, or pixmap.rowBytes is less than
1898SkImageInfo::minRowBytes: calls reset(), and returns false.
1899
1900Otherwise, if pixmap.addr() equals nullptr: sets Image_Info, returns true.
1901
1902Caller must ensure that pixmap is valid for the lifetime of Bitmap and Pixel_Ref.
1903
1904#Param pixmap Image_Info, pixel address, and rowBytes ##
1905
1906#Return true if Image_Info was set to pixmap.info() ##
1907
1908#Example
1909#Description
1910Draw a five by five bitmap, and draw it again with a center white pixel.
1911##
1912#Height 64
1913 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
1914 { 0xAC, 0xA8, 0x89, 0x47, 0x87 },
1915 { 0x4B, 0x25, 0x25, 0x25, 0x46 },
1916 { 0x90, 0x81, 0x25, 0x41, 0x33 },
1917 { 0x75, 0x55, 0x44, 0x20, 0x00 }};
1918 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
1919 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
1920 SkBitmap bitmap;
1921 bitmap.installPixels(pixmap);
1922 canvas->scale(10, 10);
1923 canvas->drawBitmap(bitmap, 0, 0);
1924 *pixmap.writable_addr8(2, 2) = 0xFF;
1925 bitmap.installPixels(pixmap);
1926 canvas->drawBitmap(bitmap, 10, 0);
1927##
1928
1929#SeeAlso allocPixels
1930
1931##
1932
1933# ------------------------------------------------------------------------------
1934
1935#Method bool installMaskPixels(const SkMask& mask)
Cary Clark4855f782018-02-06 09:41:53 -05001936#Deprecated soon
Cary Clarkbc5697d2017-10-04 14:31:33 -04001937##
1938
1939# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001940#Subtopic Pixels
1941#Populate
1942#Line # read and write pixel values ##
1943##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001944
1945#Method void setPixels(void* pixels)
Cary Clark78de7512018-02-07 07:27:09 -05001946#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05001947#Line # sets Pixel_Ref without an offset ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001948Replaces Pixel_Ref with pixels, preserving Image_Info and rowBytes.
1949Sets Pixel_Ref origin to (0, 0).
1950
1951If pixels is nullptr, or if info().colorType equals kUnknown_SkColorType;
1952release reference to Pixel_Ref, and set Pixel_Ref to nullptr.
1953
1954Caller is responsible for handling ownership pixel memory for the lifetime
1955of Bitmap and Pixel_Ref.
1956
1957#Param pixels address of pixel storage, managed by caller ##
1958
1959#Example
1960#Height 50
Ben Wagner29380bd2017-10-09 14:43:00 -04001961 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
1962 uint8_t set2[5] = { 0xAC, 0xA8, 0x89, 0x47, 0x87 };
1963 SkBitmap bitmap;
1964 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
1965 canvas->scale(10, 50);
1966 canvas->drawBitmap(bitmap, 0, 0);
1967 bitmap.setPixels(set2);
1968 canvas->drawBitmap(bitmap, 10, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001969##
1970
1971#SeeAlso installPixels allocPixels
1972
1973##
1974
1975# ------------------------------------------------------------------------------
1976
1977#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels()
Cary Clark78de7512018-02-07 07:27:09 -05001978#In Allocate
Cary Clarkbc5697d2017-10-04 14:31:33 -04001979Allocates pixel memory with HeapAllocator, and replaces existing Pixel_Ref.
1980The allocation size is determined by Image_Info width, height, and Color_Type.
1981
Cary Clarka560c472017-11-27 10:44:06 -05001982Returns false if info().colorType is kUnknown_SkColorType, or allocation fails.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001983
1984#Return true if the allocation succeeds
1985##
1986
1987#Example
1988#Height 50
Cary Clark682c58d2018-05-16 07:07:07 -04001989#Description
Cary Clarkbc5697d2017-10-04 14:31:33 -04001990Bitmap hosts and draws gray values in set1. tryAllocPixels replaces Pixel_Ref
1991and erases it to black, but does not alter set1. setPixels replaces black
Cary Clark682c58d2018-05-16 07:07:07 -04001992Pixel_Ref with set1.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001993##
Ben Wagner29380bd2017-10-09 14:43:00 -04001994 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
1995 SkBitmap bitmap;
1996 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
1997 canvas->scale(10, 50);
1998 canvas->drawBitmap(bitmap, 0, 0);
1999 if (bitmap.tryAllocPixels()) {
2000 bitmap.eraseColor(SK_ColorBLACK);
2001 canvas->drawBitmap(bitmap, 8, 0);
2002 bitmap.setPixels(set1);
2003 canvas->drawBitmap(bitmap, 16, 0);
2004 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002005##
2006
2007#SeeAlso allocPixels installPixels setPixels
2008
2009##
2010
2011# ------------------------------------------------------------------------------
2012
2013#Method void allocPixels()
Cary Clark78de7512018-02-07 07:27:09 -05002014#In Allocate
Cary Clarkbc5697d2017-10-04 14:31:33 -04002015Allocates pixel memory with HeapAllocator, and replaces existing Pixel_Ref.
2016The allocation size is determined by Image_Info width, height, and Color_Type.
2017
Cary Clarka560c472017-11-27 10:44:06 -05002018Aborts if info().colorType is kUnknown_SkColorType, or allocation fails.
2019Abort steps may be provided by the user at compile
Cary Clarkbc5697d2017-10-04 14:31:33 -04002020time by defining SK_ABORT.
2021
2022#Example
2023#Height 50
Cary Clark682c58d2018-05-16 07:07:07 -04002024#Description
Cary Clarkbc5697d2017-10-04 14:31:33 -04002025Bitmap hosts and draws gray values in set1. allocPixels replaces Pixel_Ref
2026and erases it to black, but does not alter set1. setPixels replaces black
Cary Clark682c58d2018-05-16 07:07:07 -04002027Pixel_Ref with set2.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002028##
Ben Wagner29380bd2017-10-09 14:43:00 -04002029 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
2030 uint8_t set2[5] = { 0xAC, 0xA8, 0x89, 0x47, 0x87 };
2031 SkBitmap bitmap;
2032 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
2033 canvas->scale(10, 50);
2034 canvas->drawBitmap(bitmap, 0, 0);
2035 bitmap.allocPixels();
2036 bitmap.eraseColor(SK_ColorBLACK);
2037 canvas->drawBitmap(bitmap, 8, 0);
2038 bitmap.setPixels(set2);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002039 canvas->drawBitmap(bitmap, 16, 0);
2040##
2041
2042#SeeAlso tryAllocPixels installPixels setPixels
2043
2044##
2045
2046# ------------------------------------------------------------------------------
2047
2048#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(Allocator* allocator)
2049
2050Allocates pixel memory with allocator, and replaces existing Pixel_Ref.
2051The allocation size is determined by Image_Info width, height, and Color_Type.
2052If allocator is nullptr, use HeapAllocator instead.
2053
Cary Clark78de7512018-02-07 07:27:09 -05002054Returns false if Allocator::allocPixelRef return false.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002055
2056#Param allocator instance of SkBitmap::Allocator instantiation ##
2057
2058#Return true if custom allocator reports success
2059##
2060
2061#Example
2062#Height 100
2063#Description
2064HeapAllocator limits the maximum size of Bitmap to two gigabytes. Using
2065a custom allocator, this limitation may be relaxed. This example can be
Cary Clark682c58d2018-05-16 07:07:07 -04002066modified to allocate an eight gigabyte Bitmap on a 64-bit platform with
Cary Clarkbc5697d2017-10-04 14:31:33 -04002067sufficient memory.
2068##
2069#Function
2070class LargePixelRef : public SkPixelRef {
2071public:
2072 LargePixelRef(const SkImageInfo& info, char* storage, size_t rowBytes)
2073 : SkPixelRef(info.width(), info.height(), storage, rowBytes) {
2074 }
2075
2076 ~LargePixelRef() override {
2077 delete[] (char* ) this->pixels();
2078 }
2079};
2080
2081class LargeAllocator : public SkBitmap::Allocator {
2082public:
2083 bool allocPixelRef(SkBitmap* bitmap) override {
2084 const SkImageInfo& info = bitmap->info();
2085 uint64_t rowBytes = info.minRowBytes64();
2086 uint64_t size = info.height() * rowBytes;
2087 char* addr = new char[size];
2088 if (nullptr == addr) {
2089 return false;
2090 }
2091 sk_sp<SkPixelRef> pr = sk_sp<SkPixelRef>(new LargePixelRef(info, addr, rowBytes));
2092 if (!pr) {
2093 return false;
2094 }
2095 bitmap->setPixelRef(std::move(pr), 0, 0);
2096 return true;
2097 }
2098};
2099
2100##
2101
2102void draw(SkCanvas* canvas) {
2103 LargeAllocator largeAllocator;
2104 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002105 int width = 100; // make this 20000
2106 int height = 100; // and this 100000 to allocate 8 gigs on a 64-bit platform
2107 bitmap.setInfo(SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType));
2108 if (bitmap.tryAllocPixels(&largeAllocator)) {
2109 bitmap.eraseColor(0xff55aa33);
2110 canvas->drawBitmap(bitmap, 0, 0);
2111 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002112}
2113
2114##
2115
2116#SeeAlso allocPixels Allocator Pixel_Ref
2117
2118##
2119
2120# ------------------------------------------------------------------------------
2121
2122#Method void allocPixels(Allocator* allocator)
2123
2124Allocates pixel memory with allocator, and replaces existing Pixel_Ref.
2125The allocation size is determined by Image_Info width, height, and Color_Type.
2126If allocator is nullptr, use HeapAllocator instead.
2127
Cary Clark78de7512018-02-07 07:27:09 -05002128Aborts if Allocator::allocPixelRef return false. Abort steps may be provided by
Cary Clarkbc5697d2017-10-04 14:31:33 -04002129the user at compile time by defining SK_ABORT.
2130
2131#Param allocator instance of SkBitmap::Allocator instantiation ##
2132
2133#Example
2134#Height 32
2135#Function
2136class TinyAllocator : public SkBitmap::Allocator {
2137public:
2138 bool allocPixelRef(SkBitmap* bitmap) override {
2139 const SkImageInfo& info = bitmap->info();
2140 if (info.height() * info.minRowBytes() > sizeof(storage)) {
2141 return false;
2142 }
2143 sk_sp<SkPixelRef> pr = sk_sp<SkPixelRef>(
2144 new SkPixelRef(info.width(), info.height(), storage, info.minRowBytes()));
2145 bitmap->setPixelRef(std::move(pr), 0, 0);
2146 return true;
2147 }
2148
2149 char storage[16];
2150};
2151
2152##
2153
2154void draw(SkCanvas* canvas) {
2155 TinyAllocator tinyAllocator;
2156 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002157 bitmap.setInfo(SkImageInfo::MakeN32(2, 2, kOpaque_SkAlphaType));
2158 if (bitmap.tryAllocPixels(&tinyAllocator)) {
2159 bitmap.eraseColor(0xff55aa33);
2160 bitmap.erase(0xffaa3355, SkIRect::MakeXYWH(1, 1, 1, 1));
2161 canvas->scale(16, 16);
2162 canvas->drawBitmap(bitmap, 0, 0);
2163 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002164}
2165##
2166
2167#SeeAlso allocPixels Allocator Pixel_Ref
2168
2169##
2170
2171# ------------------------------------------------------------------------------
2172
2173#Method SkPixelRef* pixelRef() const
Cary Clark78de7512018-02-07 07:27:09 -05002174#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002175#Line # returns Pixel_Ref, or nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002176Returns Pixel_Ref, which contains: pixel base address; its dimensions; and
2177rowBytes, the interval from one row to the next. Does not change Pixel_Ref
2178reference count. Pixel_Ref may be shared by multiple bitmaps.
2179If Pixel_Ref has not been set, returns nullptr.
2180
2181#Return Pixel_Ref, or nullptr ##
2182
2183#Example
2184#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002185 SkBitmap subset;
2186 source.extractSubset(&subset, SkIRect::MakeXYWH(32, 64, 128, 256));
2187 SkDebugf("src ref %c= sub ref\n", source.pixelRef() == subset.pixelRef() ? '=' : '!');
2188 SkDebugf("src pixels %c= sub pixels\n", source.getPixels() == subset.getPixels() ? '=' : '!');
2189 SkDebugf("src addr %c= sub addr\n", source.getAddr(32, 64) == subset.getAddr(0, 0) ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -04002190##
2191
2192#SeeAlso getPixels getAddr
2193
2194##
2195
2196# ------------------------------------------------------------------------------
2197
2198#Method SkIPoint pixelRefOrigin() const
Cary Clark78de7512018-02-07 07:27:09 -05002199#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002200#Line # returns offset within Pixel_Ref ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002201Returns origin of pixels within Pixel_Ref. Bitmap bounds is always contained
2202by Pixel_Ref bounds, which may be the same size or larger. Multiple Bitmaps
2203can share the same Pixel_Ref, where each Bitmap has different bounds.
2204
2205The returned origin added to Bitmap dimensions equals or is smaller than the
2206Pixel_Ref dimensions.
2207
2208Returns (0, 0) if Pixel_Ref is nullptr.
2209
2210#Return pixel origin within Pixel_Ref ##
2211
2212#Example
2213#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002214 SkBitmap subset;
2215 source.extractSubset(&subset, SkIRect::MakeXYWH(32, 64, 128, 256));
2216 SkIPoint sourceOrigin = source.pixelRefOrigin();
2217 SkIPoint subsetOrigin = subset.pixelRefOrigin();
2218 SkDebugf("source origin: %d, %d\n", sourceOrigin.fX, sourceOrigin.fY);
2219 SkDebugf("subset origin: %d, %d\n", subsetOrigin.fX, subsetOrigin.fY);
2220#StdOut
2221source origin: 0, 0
2222subset origin: 32, 64
2223##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002224##
2225
Cary Clark2ade9972017-11-02 17:49:34 -04002226#SeeAlso SkPixelRef getSubset setPixelRef
Cary Clarkbc5697d2017-10-04 14:31:33 -04002227
2228##
2229
2230# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05002231#Subtopic Set
2232#Line # updates values and attributes ##
2233#Populate
2234##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002235
2236#Method void setPixelRef(sk_sp<SkPixelRef> pixelRef, int dx, int dy)
Cary Clark78de7512018-02-07 07:27:09 -05002237#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05002238#Line # sets Pixel_Ref and offset ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002239Replaces pixelRef and origin in Bitmap. dx and dy specify the offset
2240within the Pixel_Ref pixels for the top-left corner of the bitmap.
2241
2242Asserts in debug builds if dx or dy are out of range. Pins dx and dy
2243to legal range in release builds.
2244
2245The caller is responsible for ensuring that the pixels match the
2246Color_Type and Alpha_Type in Image_Info.
2247
2248#Param pixelRef Pixel_Ref describing pixel address and rowBytes ##
2249#Param dx column offset in Pixel_Ref for bitmap origin ##
2250#Param dy row offset in Pixel_Ref for bitmap origin ##
2251
2252#Example
2253#Height 140
2254#Image 5
2255#Description
Cary Clark682c58d2018-05-16 07:07:07 -04002256Treating 32-bit data as 8-bit data is unlikely to produce useful results.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002257##
Ben Wagner29380bd2017-10-09 14:43:00 -04002258 SkBitmap bitmap;
Cary Clark682c58d2018-05-16 07:07:07 -04002259 bitmap.setInfo(SkImageInfo::Make(source.width() - 5, source.height() - 5,
Ben Wagner29380bd2017-10-09 14:43:00 -04002260 kGray_8_SkColorType, kOpaque_SkAlphaType), source.rowBytes());
2261 bitmap.setPixelRef(sk_ref_sp(source.pixelRef()), 5, 5);
2262 canvas->drawBitmap(bitmap, 10, 10);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002263##
2264
2265#SeeAlso setInfo
2266
2267##
2268
2269# ------------------------------------------------------------------------------
2270
2271#Method bool readyToDraw() const
Cary Clark78de7512018-02-07 07:27:09 -05002272#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05002273#Line # returns true if address of pixels is not nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002274Returns true if Bitmap is can be drawn.
2275
2276#Return true if getPixels() is not nullptr ##
2277
2278#Example
2279#Image 5
2280#Height 160
Ben Wagner29380bd2017-10-09 14:43:00 -04002281 if (source.readyToDraw()) {
2282 canvas->drawBitmap(source, 10, 10);
2283 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002284##
2285
2286#SeeAlso getPixels drawsNothing
2287
2288##
2289
2290# ------------------------------------------------------------------------------
2291
2292#Method uint32_t getGenerationID() const
Cary Clark78de7512018-02-07 07:27:09 -05002293#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05002294#Line # returns unique ID ##
Cary Clark682c58d2018-05-16 07:07:07 -04002295Returns a unique value corresponding to the pixels in Pixel_Ref.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002296Returns a different value after notifyPixelsChanged has been called.
2297Returns zero if Pixel_Ref is nullptr.
2298
2299Determines if pixels have changed since last examined.
2300
2301#Return unique value for pixels in Pixel_Ref ##
2302
2303#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04002304 SkBitmap bitmap;
2305 SkDebugf("empty id %u\n", bitmap.getGenerationID());
2306 bitmap.allocPixels(SkImageInfo::MakeN32(64, 64, kOpaque_SkAlphaType));
2307 SkDebugf("alloc id %u\n", bitmap.getGenerationID());
2308 bitmap.eraseColor(SK_ColorRED);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002309 SkDebugf("erase id %u\n", bitmap.getGenerationID());
2310#StdOut
2311#Volatile
Ben Wagner29380bd2017-10-09 14:43:00 -04002312empty id 0
2313alloc id 4
Cary Clarkbc5697d2017-10-04 14:31:33 -04002314erase id 6
Cary Clark682c58d2018-05-16 07:07:07 -04002315##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002316##
2317
2318#SeeAlso notifyPixelsChanged Pixel_Ref
2319
2320##
2321
2322# ------------------------------------------------------------------------------
2323
2324#Method void notifyPixelsChanged() const
Cary Clark78de7512018-02-07 07:27:09 -05002325#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05002326#Line # marks pixels as changed, altering the unique ID ##
Cary Clark682c58d2018-05-16 07:07:07 -04002327Marks that pixels in Pixel_Ref have changed. Subsequent calls to
Cary Clarkbc5697d2017-10-04 14:31:33 -04002328getGenerationID() return a different value.
2329
2330#Example
2331#Height 20
Cary Clark682c58d2018-05-16 07:07:07 -04002332 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002333 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
2334 bitmap.allocPixels();
2335 bitmap.eraseColor(SK_ColorRED);
2336 canvas->scale(16, 16);
2337 canvas->drawBitmap(bitmap, 0, 0);
2338 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorBLUE);
2339 canvas->drawBitmap(bitmap, 2, 0);
2340 bitmap.notifyPixelsChanged();
2341 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorGREEN);
2342 canvas->drawBitmap(bitmap, 4, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002343##
2344
2345#SeeAlso getGenerationID isVolatile Pixel_Ref
2346
2347##
2348
2349# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05002350#Subtopic Draw
2351#Populate
Cary Clark682c58d2018-05-16 07:07:07 -04002352#Line # sets pixels to Color ##
Cary Clark78de7512018-02-07 07:27:09 -05002353##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002354
2355#Method void eraseColor(SkColor c) const
Cary Clark78de7512018-02-07 07:27:09 -05002356#In Draw
Cary Clarkab2621d2018-01-30 10:08:57 -05002357#Line # writes Color to pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002358Replaces pixel values with c. All pixels contained by bounds() are affected.
2359If the colorType is kGray_8_SkColorType or k565_SkColorType, then Color_Alpha
Cary Clarkffb3d682018-05-17 12:17:28 -04002360is ignored; RGB is treated as opaque. If colorType is kAlpha_8_SkColorType,
2361then RGB is ignored.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002362
2363#Param c Unpremultiplied Color ##
2364
2365#Example
2366#Height 20
Cary Clark682c58d2018-05-16 07:07:07 -04002367 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002368 bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kOpaque_SkAlphaType));
2369 bitmap.eraseColor(SK_ColorRED);
2370 canvas->scale(16, 16);
2371 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002372##
2373
Cary Clark154beea2017-10-26 07:58:48 -04002374#SeeAlso eraseARGB erase
Cary Clarkbc5697d2017-10-04 14:31:33 -04002375
2376##
2377
2378# ------------------------------------------------------------------------------
2379
2380#Method void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const
Cary Clark78de7512018-02-07 07:27:09 -05002381#In Draw
Cary Clarkab2621d2018-01-30 10:08:57 -05002382#Line # writes Color to pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002383Replaces pixel values with Unpremultiplied Color built from a, r, g, and b.
2384All pixels contained by bounds() are affected.
2385If the colorType is kGray_8_SkColorType or k565_SkColorType, then a
2386is ignored; r, g, and b are treated as opaque. If colorType is kAlpha_8_SkColorType,
2387then r, g, and b are ignored.
2388
2389#Param a amount of Color_Alpha, from fully transparent (0) to fully opaque (255) ##
Cary Clarkffb3d682018-05-17 12:17:28 -04002390#Param r amount of red, from no red (0) to full red (255) ##
2391#Param g amount of green, from no green (0) to full green (255) ##
2392#Param b amount of blue, from no blue (0) to full blue (255) ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002393
2394#Example
2395#Height 80
Cary Clark682c58d2018-05-16 07:07:07 -04002396 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002397 bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType));
2398 bitmap.eraseARGB(0x7f, 0xff, 0x7f, 0x3f);
2399 canvas->scale(50, 50);
2400 canvas->drawBitmap(bitmap, 0, 0);
2401 canvas->drawBitmap(bitmap, .5f, .5f);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002402##
2403
Cary Clark154beea2017-10-26 07:58:48 -04002404#SeeAlso eraseColor erase
Cary Clarkbc5697d2017-10-04 14:31:33 -04002405
2406##
2407
2408# ------------------------------------------------------------------------------
2409
2410#Method void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const
Cary Clark682c58d2018-05-16 07:07:07 -04002411#Deprecated
Cary Clarkbc5697d2017-10-04 14:31:33 -04002412##
2413
2414# ------------------------------------------------------------------------------
2415
2416#Method void erase(SkColor c, const SkIRect& area) const
Cary Clark78de7512018-02-07 07:27:09 -05002417#In Draw
Cary Clarkab2621d2018-01-30 10:08:57 -05002418#Line # writes Color to rectangle of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002419Replaces pixel values inside area with c. If area does not intersect bounds(),
2420call has no effect.
2421
2422If the colorType is kGray_8_SkColorType or k565_SkColorType, then Color_Alpha
Cary Clarkffb3d682018-05-17 12:17:28 -04002423is ignored; RGB is treated as opaque. If colorType is kAlpha_8_SkColorType,
2424then RGB is ignored.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002425
2426#Param c Unpremultiplied Color ##
2427#Param area rectangle to fill ##
2428
2429#Example
2430#Height 70
Cary Clark682c58d2018-05-16 07:07:07 -04002431 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002432 bitmap.allocPixels(SkImageInfo::MakeN32(2, 2, kPremul_SkAlphaType));
2433 bitmap.erase(0x7fff7f3f, SkIRect::MakeWH(1, 1));
2434 bitmap.erase(0x7f7f3fff, SkIRect::MakeXYWH(0, 1, 1, 1));
2435 bitmap.erase(0x7f3fff7f, SkIRect::MakeXYWH(1, 0, 1, 1));
2436 bitmap.erase(0x7f1fbf5f, SkIRect::MakeXYWH(1, 1, 1, 1));
2437 canvas->scale(25, 25);
2438 canvas->drawBitmap(bitmap, 0, 0);
2439 canvas->drawBitmap(bitmap, .5f, .5f);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002440
2441##
2442
2443#SeeAlso eraseColor eraseARGB eraseRGB SkCanvas::drawRect
2444
2445##
2446
2447# ------------------------------------------------------------------------------
2448
2449#Method void eraseArea(const SkIRect& area, SkColor c) const
Cary Clark682c58d2018-05-16 07:07:07 -04002450#Deprecated
Cary Clarkbc5697d2017-10-04 14:31:33 -04002451##
2452
Cary Clarkbc5697d2017-10-04 14:31:33 -04002453# ------------------------------------------------------------------------------
2454
2455#Method SkColor getColor(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002456#In Property
2457#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05002458#Line # returns one pixel as Unpremultiplied Color ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002459Returns pixel at (x, y) as Unpremultiplied Color.
2460Returns black with Alpha if Color_Type is kAlpha_8_SkColorType.
2461
2462Input is not validated: out of bounds values of x or y trigger an assert() if
2463built with SK_DEBUG defined; and returns undefined values or may crash if
2464SK_RELEASE is defined. Fails if Color_Type is kUnknown_SkColorType or
2465pixel address is nullptr.
2466
2467Color_Space in Image_Info is ignored. Some Color precision may be lost in the
Cary Clark682c58d2018-05-16 07:07:07 -04002468conversion to Unpremultiplied Color; original pixel data may have additional
Cary Clarkbc5697d2017-10-04 14:31:33 -04002469precision.
2470
2471#Param x column index, zero or greater, and less than width() ##
2472#Param y row index, zero or greater, and less than height() ##
2473
2474#Return pixel converted to Unpremultiplied Color ##
2475
2476#Example
2477 const int w = 4;
2478 const int h = 4;
2479 SkColor colors[][w] = {
Cary Clark75fd4492018-06-20 12:45:16 -04002480 { 0x00000000, 0x2a0e002a, 0x55380055, 0x7f7f007f },
2481 { 0x2a000e2a, 0x551c1c55, 0x7f542a7f, 0xaaaa38aa },
2482 { 0x55003855, 0x7f2a547f, 0xaa7171aa, 0xd4d48dd4 },
2483 { 0x7f007f7f, 0xaa38aaaa, 0xd48dd4d4, 0xffffffff }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002484 };
2485 SkDebugf("Premultiplied:\n");
2486 for (int y = 0; y < h; ++y) {
2487 SkDebugf("(0, %d) ", y);
2488 for (int x = 0; x < w; ++x) {
2489 SkDebugf("0x%08x%c", colors[y][x], x == w - 1 ? '\n' : ' ');
2490 }
2491 }
2492 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), colors, w * 4);
2493 SkBitmap bitmap;
2494 bitmap.installPixels(pixmap);
2495 SkDebugf("Unpremultiplied:\n");
2496 for (int y = 0; y < h; ++y) {
2497 SkDebugf("(0, %d) ", y);
2498 for (int x = 0; x < w; ++x) {
2499 SkDebugf("0x%08x%c", bitmap.getColor(x, y), x == w - 1 ? '\n' : ' ');
2500 }
2501 }
2502#StdOut
2503Premultiplied:
Cary Clark682c58d2018-05-16 07:07:07 -04002504(0, 0) 0x00000000 0x2a0e002a 0x55380055 0x7f7f007f
2505(0, 1) 0x2a000e2a 0x551c1c55 0x7f542a7f 0xaaaa38aa
2506(0, 2) 0x55003855 0x7f2a547f 0xaa7171aa 0xd4d48dd4
2507(0, 3) 0x7f007f7f 0xaa38aaaa 0xd48dd4d4 0xffffffff
Cary Clarkbc5697d2017-10-04 14:31:33 -04002508Unpremultiplied:
Cary Clark682c58d2018-05-16 07:07:07 -04002509(0, 0) 0x00000000 0x2a5500ff 0x55a800ff 0x7fff00ff
2510(0, 1) 0x2a0055ff 0x555454ff 0x7fa954ff 0xaaff54ff
2511(0, 2) 0x5500a8ff 0x7f54a9ff 0xaaaaaaff 0xd4ffaaff
2512(0, 3) 0x7f00ffff 0xaa54ffff 0xd4aaffff 0xffffffff
Cary Clarkbc5697d2017-10-04 14:31:33 -04002513##
2514##
2515
2516#SeeAlso getAddr readPixels
2517
2518##
2519
2520# ------------------------------------------------------------------------------
2521
2522#Method void* getAddr(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002523#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002524#Line # returns readable pixel address as void pointer ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002525Returns pixel address at (x, y).
2526
2527Input is not validated: out of bounds values of x or y, or kUnknown_SkColorType,
2528trigger an assert() if built with SK_DEBUG defined. Returns nullptr if
2529Color_Type is kUnknown_SkColorType, or Pixel_Ref is nullptr.
2530
Cary Clark682c58d2018-05-16 07:07:07 -04002531Performs a lookup of pixel size; for better performance, call
Cary Clarkbc5697d2017-10-04 14:31:33 -04002532one of: getAddr8, getAddr16, or getAddr32.
2533
2534#Param x column index, zero or greater, and less than width() ##
2535#Param y row index, zero or greater, and less than height() ##
2536
2537#Return generic pointer to pixel ##
2538
2539#Example
2540#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002541 char* row0 = (char* ) source.getAddr(0, 0);
2542 char* row1 = (char* ) source.getAddr(0, 1);
Cary Clark681287e2018-03-16 11:34:15 -04002543 SkDebugf("addr interval %c= rowBytes\n",
2544 (size_t) (row1 - row0) == source.rowBytes() ? '=' : '!');
Ben Wagner29380bd2017-10-09 14:43:00 -04002545#StdOut
2546addr interval == rowBytes
2547##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002548##
2549
2550#SeeAlso getAddr8 getAddr16 getAddr32 readPixels SkPixmap::addr
2551
2552##
2553
2554# ------------------------------------------------------------------------------
2555
2556#Method inline uint32_t* getAddr32(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002557#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002558#Line # returns readable pixel address as 32-bit pointer ##
Cary Clark682c58d2018-05-16 07:07:07 -04002559Returns address at (x, y).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002560
2561Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2562#List
2563# Pixel_Ref is nullptr ##
2564# bytesPerPixel() is not four ##
2565# x is negative, or not less than width() ##
2566# y is negative, or not less than height() ##
2567##
2568
2569#Param x column index, zero or greater, and less than width() ##
2570#Param y row index, zero or greater, and less than height() ##
2571
2572#Return unsigned 32-bit pointer to pixel at (x, y) ##
2573
2574#Example
2575#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002576 uint32_t* row0 = source.getAddr32(0, 0);
2577 uint32_t* row1 = source.getAddr32(0, 1);
2578 size_t interval = (row1 - row0) * source.bytesPerPixel();
2579 SkDebugf("addr interval %c= rowBytes\n", interval == source.rowBytes() ? '=' : '!');
2580#StdOut
2581addr interval == rowBytes
2582##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002583##
2584
2585#SeeAlso getAddr8 getAddr16 getAddr readPixels SkPixmap::addr32
2586
2587##
2588
2589# ------------------------------------------------------------------------------
2590
2591#Method inline uint16_t* getAddr16(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002592#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002593#Line # returns readable pixel address as 16-bit pointer ##
Cary Clark682c58d2018-05-16 07:07:07 -04002594Returns address at (x, y).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002595
2596Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2597#List
2598# Pixel_Ref is nullptr ##
2599# bytesPerPixel() is not two ##
2600# x is negative, or not less than width() ##
2601# y is negative, or not less than height() ##
2602##
2603
2604#Param x column index, zero or greater, and less than width() ##
2605#Param y row index, zero or greater, and less than height() ##
2606
2607#Return unsigned 16-bit pointer to pixel at (x, y)##
2608
2609#Example
2610#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002611 SkBitmap bitmap16;
Cary Clark682c58d2018-05-16 07:07:07 -04002612 SkImageInfo dstInfo = SkImageInfo::Make(source.width(), source.height(), kARGB_4444_SkColorType,
Ben Wagner29380bd2017-10-09 14:43:00 -04002613 kPremul_SkAlphaType);
2614 bitmap16.allocPixels(dstInfo);
2615 if (source.readPixels(dstInfo, bitmap16.getPixels(), bitmap16.rowBytes(), 0, 0)) {
2616 uint16_t* row0 = bitmap16.getAddr16(0, 0);
2617 uint16_t* row1 = bitmap16.getAddr16(0, 1);
2618 size_t interval = (row1 - row0) * bitmap16.bytesPerPixel();
2619 SkDebugf("addr interval %c= rowBytes\n", interval == bitmap16.rowBytes() ? '=' : '!');
2620 }
2621#StdOut
2622addr interval == rowBytes
2623##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002624##
2625
2626#SeeAlso getAddr8 getAddr getAddr32 readPixels SkPixmap::addr16
2627
2628##
2629
2630# ------------------------------------------------------------------------------
2631
2632#Method inline uint8_t* getAddr8(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002633#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002634#Line # returns readable pixel address as 8-bit pointer ##
Cary Clark682c58d2018-05-16 07:07:07 -04002635Returns address at (x, y).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002636
2637Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2638#List
2639# Pixel_Ref is nullptr ##
2640# bytesPerPixel() is not one ##
2641# x is negative, or not less than width() ##
2642# y is negative, or not less than height() ##
2643##
2644
2645#Param x column index, zero or greater, and less than width() ##
2646#Param y row index, zero or greater, and less than height() ##
2647
2648#Return unsigned 8-bit pointer to pixel at (x, y) ##
2649
2650#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04002651 SkBitmap bitmap;
2652 const int width = 8;
2653 const int height = 8;
2654 uint8_t pixels[height][width];
2655 SkImageInfo info = SkImageInfo::Make(width, height, kGray_8_SkColorType, kOpaque_SkAlphaType);
2656 if (bitmap.installPixels(info, pixels, info.minRowBytes())) {
2657 SkDebugf("&pixels[4][2] %c= bitmap.getAddr8(2, 4)\n",
2658 &pixels[4][2] == bitmap.getAddr8(2, 4) ? '=' : '!');
2659 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002660#StdOut
2661&pixels[4][2] == bitmap.getAddr8(2, 4)
2662##
2663##
2664
2665#SeeAlso getAddr getAddr16 getAddr32 readPixels SkPixmap::addr8
2666
2667##
2668
2669# ------------------------------------------------------------------------------
2670
2671#Method bool extractSubset(SkBitmap* dst, const SkIRect& subset) const
Cary Clark78de7512018-02-07 07:27:09 -05002672#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -05002673#Line # creates Bitmap, sharing pixels if possible ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002674Shares Pixel_Ref with dst. Pixels are not copied; Bitmap and dst point
2675to the same pixels; dst bounds() are set to the intersection of subset
2676and the original bounds().
2677
2678subset may be larger than bounds(). Any area outside of bounds() is ignored.
2679
2680Any contents of dst are discarded. isVolatile setting is copied to dst.
2681dst is set to colorType, alphaType, and colorSpace.
2682
2683Return false if:
2684#List
2685# dst is nullptr ##
2686# Pixel_Ref is nullptr ##
2687# subset does not intersect bounds() ##
Cary Clark682c58d2018-05-16 07:07:07 -04002688##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002689
2690#Param dst Bitmap set to subset ##
2691#Param subset rectangle of pixels to reference ##
2692
2693#Return true if dst is replaced by subset
2694##
2695
2696#Example
2697#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002698 SkIRect bounds, s;
2699 source.getBounds(&bounds);
2700 SkDebugf("bounds: %d, %d, %d, %d\n", bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
2701 SkBitmap subset;
2702 for (int left: { -100, 0, 100, 1000 } ) {
2703 for (int right: { 0, 100, 1000 } ) {
2704 SkIRect b = SkIRect::MakeLTRB(left, 100, right, 200);
2705 bool success = source.extractSubset(&subset, b);
2706 SkDebugf("subset: %4d, %4d, %4d, %4d ", b.fLeft, b.fTop, b.fRight, b.fBottom);
2707 SkDebugf("success; %s", success ? "true" : "false");
2708 if (success) {
Cary Clark682c58d2018-05-16 07:07:07 -04002709 subset.getBounds(&s);
Ben Wagner29380bd2017-10-09 14:43:00 -04002710 SkDebugf(" subset: %d, %d, %d, %d", s.fLeft, s.fTop, s.fRight, s.fBottom);
2711 }
2712 SkDebugf("\n");
Cary Clark682c58d2018-05-16 07:07:07 -04002713 }
Ben Wagner29380bd2017-10-09 14:43:00 -04002714 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002715#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04002716bounds: 0, 0, 512, 512
2717subset: -100, 100, 0, 200 success; false
2718subset: -100, 100, 100, 200 success; true subset: 0, 0, 100, 100
2719subset: -100, 100, 1000, 200 success; true subset: 0, 0, 512, 100
2720subset: 0, 100, 0, 200 success; false
2721subset: 0, 100, 100, 200 success; true subset: 0, 0, 100, 100
2722subset: 0, 100, 1000, 200 success; true subset: 0, 0, 512, 100
2723subset: 100, 100, 0, 200 success; false
2724subset: 100, 100, 100, 200 success; false
2725subset: 100, 100, 1000, 200 success; true subset: 0, 0, 412, 100
2726subset: 1000, 100, 0, 200 success; false
2727subset: 1000, 100, 100, 200 success; false
Cary Clarkbc5697d2017-10-04 14:31:33 -04002728subset: 1000, 100, 1000, 200 success; false
2729##
2730##
2731
2732#SeeAlso readPixels writePixels SkCanvas::drawBitmap
2733
2734##
2735
2736# ------------------------------------------------------------------------------
2737
2738#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
Cary Clarkbc5697d2017-10-04 14:31:33 -04002739 int srcX, int srcY) const
Cary Clarke80cd442018-07-17 13:19:56 -04002740#In Pixels
2741#Line # copies and converts pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002742
Cary Clarkac47b882018-01-11 10:35:44 -05002743Copies a Rect of pixels from Bitmap to dstPixels. Copy starts at (srcX, srcY),
Cary Clark682c58d2018-05-16 07:07:07 -04002744and does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002745
Cary Clarkac47b882018-01-11 10:35:44 -05002746dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
2747destination. dstRowBytes specifics the gap from one destination row to the next.
2748Returns true if pixels are copied. Returns false if:
Cary Clarkbc5697d2017-10-04 14:31:33 -04002749#List
2750# dstInfo.addr() equals nullptr ##
2751# dstRowBytes is less than dstInfo.minRowBytes ##
2752# Pixel_Ref is nullptr ##
2753##
2754
Cary Clarkac47b882018-01-11 10:35:44 -05002755Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002756kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002757If Bitmap colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
2758If Bitmap alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
2759match. If Bitmap colorSpace is nullptr, dstInfo.colorSpace must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002760false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04002761
Cary Clarkbc5697d2017-10-04 14:31:33 -04002762srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04002763false if width() or height() is zero or negative.
Cary Clark2be81cf2018-09-13 12:04:30 -04002764Returns false if #Formula # abs(srcX) >= Bitmap width() ##, or if #Formula # abs(srcY) >= Bitmap height() ##.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002765
2766#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
2767#Param dstPixels destination pixel storage ##
2768#Param dstRowBytes destination row length ##
2769#Param srcX column index whose absolute value is less than width() ##
2770#Param srcY row index whose absolute value is less than height() ##
2771
2772#Return true if pixels are copied to dstPixels ##
2773
2774#Example
2775#Height 128
2776#Description
2777Transferring the gradient from 8 bits per component to 4 bits per component
2778creates visible banding.
2779##
Ben Wagner29380bd2017-10-09 14:43:00 -04002780 const int width = 256;
2781 const int height = 64;
2782 SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(width, height);
2783 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
2784 SkPoint gradPoints[] = { { 0, 0 }, { 256, 0 } };
2785 SkPaint paint;
2786 paint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
2787 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
2788 SkBitmap bitmap;
2789 bitmap.allocPixels(srcInfo);
2790 SkCanvas srcCanvas(bitmap);
2791 srcCanvas.drawRect(SkRect::MakeWH(width, height), paint);
2792 canvas->drawBitmap(bitmap, 0, 0);
2793 SkImageInfo dstInfo = srcInfo.makeColorType(kARGB_4444_SkColorType);
2794 std::vector<int16_t> dstPixels;
2795 dstPixels.resize(height * width);
2796 bitmap.readPixels(dstInfo, &dstPixels.front(), width * 2, 0, 0);
2797 SkPixmap dstPixmap(dstInfo, &dstPixels.front(), width * 2);
2798 bitmap.installPixels(dstPixmap);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002799 canvas->drawBitmap(bitmap, 0, 64);
2800##
2801
2802#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2803
2804##
2805
2806# ------------------------------------------------------------------------------
2807
2808#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY) const
2809
Cary Clarkac47b882018-01-11 10:35:44 -05002810Copies a Rect of pixels from Bitmap to dst. Copy starts at (srcX, srcY), and
Cary Clark682c58d2018-05-16 07:07:07 -04002811does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002812
2813dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2814and row bytes of destination. dst.rowBytes specifics the gap from one destination
2815row to the next. Returns true if pixels are copied. Returns false if:
2816#List
2817# dst pixel storage equals nullptr ##
2818# dst.rowBytes is less than SkImageInfo::minRowBytes ##
2819# Pixel_Ref is nullptr ##
2820##
2821
Cary Clarkac47b882018-01-11 10:35:44 -05002822Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002823kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002824If Bitmap colorType is kGray_8_SkColorType, dst Color_Space must match.
2825If Bitmap alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
2826match. If Bitmap colorSpace is nullptr, dst Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002827false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04002828
Cary Clarkbc5697d2017-10-04 14:31:33 -04002829srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark682c58d2018-05-16 07:07:07 -04002830false if width() or height() is zero or negative.
Cary Clark2be81cf2018-09-13 12:04:30 -04002831Returns false if #Formula # abs(srcX) >= Bitmap width() ##, or if #Formula # abs(srcY) >= Bitmap height() ##.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002832
2833#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
2834#Param srcX column index whose absolute value is less than width() ##
2835#Param srcY row index whose absolute value is less than height() ##
2836
2837#Return true if pixels are copied to dst ##
2838
2839#Example
2840#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002841 std::vector<int32_t> srcPixels;
2842 srcPixels.resize(source.height() * source.rowBytes());
2843 for (int y = 0; y < 4; ++y) {
2844 for (int x = 0; x < 4; ++x) {
2845 SkPixmap pixmap(SkImageInfo::MakeN32Premul(source.width() / 4, source.height() / 4),
2846 &srcPixels.front() + x * source.height() * source.width() / 4 +
2847 y * source.width() / 4, source.rowBytes());
2848 source.readPixels(pixmap, x * source.width() / 4, y * source.height() / 4);
2849 }
2850 }
2851 canvas->scale(.5f, .5f);
2852 SkBitmap bitmap;
2853 bitmap.installPixels(SkImageInfo::MakeN32Premul(source.width(), source.height()),
2854 &srcPixels.front(), source.rowBytes());
Cary Clarkbc5697d2017-10-04 14:31:33 -04002855 canvas->drawBitmap(bitmap, 0, 0);
2856##
2857
2858#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2859
2860##
2861
2862# ------------------------------------------------------------------------------
2863
2864#Method bool readPixels(const SkPixmap& dst) const
2865
Cary Clarkac47b882018-01-11 10:35:44 -05002866Copies a Rect of pixels from Bitmap to dst. Copy starts at (0, 0), and
Cary Clark682c58d2018-05-16 07:07:07 -04002867does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002868
2869dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2870and row bytes of destination. dst.rowBytes specifics the gap from one destination
2871row to the next. Returns true if pixels are copied. Returns false if:
2872#List
2873# dst pixel storage equals nullptr ##
2874# dst.rowBytes is less than SkImageInfo::minRowBytes ##
2875# Pixel_Ref is nullptr ##
2876##
2877
Cary Clarkac47b882018-01-11 10:35:44 -05002878Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002879kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002880If Bitmap colorType is kGray_8_SkColorType, dst Color_Space must match.
2881If Bitmap alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
2882match. If Bitmap colorSpace is nullptr, dst Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002883false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04002884
Cary Clarkbc5697d2017-10-04 14:31:33 -04002885#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
2886
2887#Return true if pixels are copied to dst ##
2888
2889#Example
2890#Height 128
2891#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002892 std::vector<int32_t> srcPixels;
2893 srcPixels.resize(source.height() * source.width() * 8);
2894 for (int i = 0; i < 2; ++i) {
Cary Clark682c58d2018-05-16 07:07:07 -04002895 SkPixmap pixmap(SkImageInfo::Make(source.width() * 2, source.height(),
Ben Wagner29380bd2017-10-09 14:43:00 -04002896 i ? kRGBA_8888_SkColorType : kBGRA_8888_SkColorType, kPremul_SkAlphaType),
2897 &srcPixels.front() + i * source.width(), source.rowBytes() * 2);
2898 source.readPixels(pixmap);
2899 }
2900 canvas->scale(.25f, .25f);
2901 SkBitmap bitmap;
2902 bitmap.installPixels(SkImageInfo::MakeN32Premul(source.width() * 2, source.height()),
2903 &srcPixels.front(), source.rowBytes() * 2);
2904 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002905##
2906
2907#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2908
2909##
2910
2911# ------------------------------------------------------------------------------
2912
2913#Method bool writePixels(const SkPixmap& src, int dstX, int dstY)
Cary Clark78de7512018-02-07 07:27:09 -05002914#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05002915#Line # copies and converts pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002916Copies a Rect of pixels from src. Copy starts at (dstX, dstY), and does not exceed
Cary Clark682c58d2018-05-16 07:07:07 -04002917(src.width(), src.height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002918
2919src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2920and row bytes of source. src.rowBytes specifics the gap from one source
2921row to the next. Returns true if pixels are copied. Returns false if:
2922#List
2923# src pixel storage equals nullptr ##
2924# src.rowBytes is less than SkImageInfo::minRowBytes ##
2925# Pixel_Ref is nullptr ##
2926##
2927
Cary Clarkac47b882018-01-11 10:35:44 -05002928Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002929kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002930If Bitmap colorType is kGray_8_SkColorType, src Color_Space must match.
2931If Bitmap alphaType is kOpaque_SkAlphaType, src Alpha_Type must
2932match. If Bitmap colorSpace is nullptr, src Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002933false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04002934
Cary Clarkbc5697d2017-10-04 14:31:33 -04002935dstX and dstY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04002936false if width() or height() is zero or negative.
Cary Clark2be81cf2018-09-13 12:04:30 -04002937Returns false if #Formula # abs(dstX) >= Bitmap width() ##, or if #Formula # abs(dstY) >= Bitmap height() ##.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002938
2939#Param src source Pixmap: Image_Info, pixels, row bytes ##
2940#Param dstX column index whose absolute value is less than width() ##
2941#Param dstY row index whose absolute value is less than height() ##
2942
2943#Return true if src pixels are copied to Bitmap ##
2944
2945#Example
2946#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002947 std::vector<int32_t> srcPixels;
2948 int width = image->width();
2949 int height = image->height();
2950 srcPixels.resize(height * width * 4);
2951 SkPixmap pixmap(SkImageInfo::MakeN32Premul(width, height), (const void*) &srcPixels.front(),
2952 width * 4);
2953 image->readPixels(pixmap, 0, 0);
2954 canvas->scale(.5f, .5f);
2955 width /= 4;
2956 height /= 4;
2957 for (int y = 0; y < 4; ++y) {
2958 for (int x = 0; x < 4; ++x) {
2959 SkBitmap bitmap;
2960 bitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height));
2961 bitmap.writePixels(pixmap, -y * width, -x * height);
2962 canvas->drawBitmap(bitmap, x * width, y * height);
2963 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002964 }
2965##
2966
Cary Clark682c58d2018-05-16 07:07:07 -04002967#SeeAlso readPixels
Cary Clarkbc5697d2017-10-04 14:31:33 -04002968
2969##
2970
2971# ------------------------------------------------------------------------------
2972
2973#Method bool writePixels(const SkPixmap& src)
2974
2975Copies a Rect of pixels from src. Copy starts at (0, 0), and does not exceed
Cary Clark682c58d2018-05-16 07:07:07 -04002976(src.width(), src.height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002977
2978src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2979and row bytes of source. src.rowBytes specifics the gap from one source
2980row to the next. Returns true if pixels are copied. Returns false if:
2981#List
2982# src pixel storage equals nullptr ##
2983# src.rowBytes is less than SkImageInfo::minRowBytes ##
2984# Pixel_Ref is nullptr ##
2985##
2986
Cary Clarkac47b882018-01-11 10:35:44 -05002987Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002988kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002989If Bitmap colorType is kGray_8_SkColorType, src Color_Space must match.
2990If Bitmap alphaType is kOpaque_SkAlphaType, src Alpha_Type must
2991match. If Bitmap colorSpace is nullptr, src Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002992false if pixel conversion is not possible.
2993
2994#Param src source Pixmap: Image_Info, pixels, row bytes ##
2995
2996#Return true if src pixels are copied to Bitmap ##
2997
2998#Example
2999#Height 80
Ben Wagner29380bd2017-10-09 14:43:00 -04003000 SkBitmap bitmap;
3001 bitmap.allocPixels(SkImageInfo::MakeN32Premul(2, 2));
3002 bitmap.eraseColor(SK_ColorGREEN);
3003 SkPMColor color = 0xFF5599BB;
3004 SkPixmap src(SkImageInfo::MakeN32Premul(1, 1), &color, 4);
3005 bitmap.writePixels(src);
3006 canvas->scale(40, 40);
3007 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003008##
3009
3010#SeeAlso readPixels
3011
3012##
3013
3014# ------------------------------------------------------------------------------
3015
Cary Clarkbc5697d2017-10-04 14:31:33 -04003016#Method bool hasHardwareMipMap() const
Cary Clark78de7512018-02-07 07:27:09 -05003017#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05003018#Line # returns Mip_Map support present; Android only ##
Cary Clark224c7002018-06-27 11:00:21 -04003019
3020For use by Android framework only.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003021
3022#Return true if setHasHardwareMipMap has been called with true ##
3023
3024#NoExample
3025##
3026
3027#SeeAlso setHasHardwareMipMap
3028
3029##
3030
3031# ------------------------------------------------------------------------------
3032
3033#Method void setHasHardwareMipMap(bool hasHardwareMipMap)
Cary Clark78de7512018-02-07 07:27:09 -05003034#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05003035#Line # sets Mip_Map support present; Android only ##
Cary Clark224c7002018-06-27 11:00:21 -04003036
3037For use by Android framework only.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003038
3039#Param hasHardwareMipMap sets state ##
3040
3041#NoExample
3042##
3043
3044#SeeAlso hasHardwareMipMap
3045
3046##
3047
3048# ------------------------------------------------------------------------------
3049
3050#Method bool extractAlpha(SkBitmap* dst) const
Cary Clark78de7512018-02-07 07:27:09 -05003051#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -05003052#Line # creates Bitmap containing Alpha of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003053Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3054or dst pixels cannot be allocated.
3055
3056Uses HeapAllocator to reserve memory for dst Pixel_Ref.
3057
3058#Param dst holds Pixel_Ref to fill with alpha layer ##
3059
3060#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3061
3062#Example
3063#Height 100
Ben Wagner29380bd2017-10-09 14:43:00 -04003064 SkBitmap alpha, bitmap;
3065 bitmap.allocN32Pixels(100, 100);
3066 SkCanvas offscreen(bitmap);
3067 offscreen.clear(0);
3068 SkPaint paint;
3069 paint.setAntiAlias(true);
3070 paint.setColor(SK_ColorBLUE);
3071 paint.setStyle(SkPaint::kStroke_Style);
3072 paint.setStrokeWidth(20);
3073 offscreen.drawCircle(50, 50, 39, paint);
3074 offscreen.flush();
3075 bitmap.extractAlpha(&alpha);
3076 paint.setColor(SK_ColorRED);
3077 canvas->drawBitmap(bitmap, 0, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003078 canvas->drawBitmap(alpha, 100, 0, &paint);
3079##
3080
3081#SeeAlso extractSubset
3082
3083##
3084
3085# ------------------------------------------------------------------------------
3086
3087#Method bool extractAlpha(SkBitmap* dst, const SkPaint* paint,
3088 SkIPoint* offset) const
3089
3090Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3091or dst pixels cannot be allocated.
3092
Cary Clark682c58d2018-05-16 07:07:07 -04003093If paint is not nullptr and contains Mask_Filter, SkMaskFilter
Cary Clarkbc5697d2017-10-04 14:31:33 -04003094generates Mask_Alpha from Bitmap. Uses HeapAllocator to reserve memory for dst
3095Pixel_Ref. Sets offset to top-left position for dst for alignment with Bitmap;
3096(0, 0) unless SkMaskFilter generates mask.
3097
3098#Param dst holds Pixel_Ref to fill with alpha layer ##
3099#Param paint holds optional Mask_Filter; may be nullptr ##
3100#Param offset top-left position for dst; may be nullptr ##
3101
3102#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3103
Cary Clark4855f782018-02-06 09:41:53 -05003104#Bug 7103
Cary Clarkbc5697d2017-10-04 14:31:33 -04003105#Example
3106#Height 160
Cary Clark681287e2018-03-16 11:34:15 -04003107 auto radiusToSigma = [](SkScalar radius) -> SkScalar {
3108 static const SkScalar kBLUR_SIGMA_SCALE = 0.57735f;
3109 return radius > 0 ? kBLUR_SIGMA_SCALE * radius + 0.5f : 0.0f;
3110 };
3111 SkBitmap alpha, bitmap;
3112 bitmap.allocN32Pixels(100, 100);
3113 SkCanvas offscreen(bitmap);
3114 offscreen.clear(0);
3115 SkPaint paint;
3116 paint.setAntiAlias(true);
3117 paint.setColor(SK_ColorBLUE);
3118 paint.setStyle(SkPaint::kStroke_Style);
3119 paint.setStrokeWidth(20);
3120 offscreen.drawCircle(50, 50, 39, paint);
3121 offscreen.flush();
3122 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, radiusToSigma(25)));
3123 SkIPoint offset;
3124 bitmap.extractAlpha(&alpha, &paint, &offset);
3125 paint.setColor(SK_ColorRED);
3126 canvas->drawBitmap(bitmap, 0, -offset.fY, &paint);
3127 canvas->drawBitmap(alpha, 100 + offset.fX, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003128##
3129
3130#SeeAlso extractSubset
3131
3132##
3133
3134# ------------------------------------------------------------------------------
3135
3136#Method bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator,
3137 SkIPoint* offset) const
3138
3139Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3140or dst pixels cannot be allocated.
3141
Cary Clark682c58d2018-05-16 07:07:07 -04003142If paint is not nullptr and contains Mask_Filter, SkMaskFilter
Cary Clarkbc5697d2017-10-04 14:31:33 -04003143generates Mask_Alpha from Bitmap. allocator may reference a custom allocation
Cary Clark682c58d2018-05-16 07:07:07 -04003144class or be set to nullptr to use HeapAllocator. Sets offset to top-left
Cary Clarkbc5697d2017-10-04 14:31:33 -04003145position for dst for alignment with Bitmap; (0, 0) unless SkMaskFilter generates
3146mask.
3147
3148#Param dst holds Pixel_Ref to fill with alpha layer ##
3149#Param paint holds optional Mask_Filter; may be nullptr ##
Cary Clark682c58d2018-05-16 07:07:07 -04003150#Param allocator function to reserve memory for Pixel_Ref; may be nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003151#Param offset top-left position for dst; may be nullptr ##
3152
3153#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3154
Cary Clark4855f782018-02-06 09:41:53 -05003155#Bug 7104
Cary Clarkbc5697d2017-10-04 14:31:33 -04003156#Example
3157#Height 128
Ben Wagner29380bd2017-10-09 14:43:00 -04003158 SkBitmap alpha, bitmap;
3159 bitmap.allocN32Pixels(100, 100);
3160 SkCanvas offscreen(bitmap);
3161 offscreen.clear(0);
3162 SkPaint paint;
3163 paint.setAntiAlias(true);
3164 paint.setColor(SK_ColorBLUE);
3165 paint.setStyle(SkPaint::kStroke_Style);
3166 paint.setStrokeWidth(20);
3167 offscreen.drawCircle(50, 50, 39, paint);
3168 offscreen.flush();
Cary Clark681287e2018-03-16 11:34:15 -04003169 paint.setMaskFilter(SkMaskFilter::MakeBlur(kOuter_SkBlurStyle, 3));
Ben Wagner29380bd2017-10-09 14:43:00 -04003170 SkIPoint offset;
3171 bitmap.extractAlpha(&alpha, &paint, nullptr, &offset);
3172 paint.setColor(SK_ColorRED);
3173 canvas->drawBitmap(bitmap, 0, -offset.fY, &paint);
3174 canvas->drawBitmap(alpha, 100 + offset.fX, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003175##
3176
3177#SeeAlso extractSubset
3178
3179##
3180
3181# ------------------------------------------------------------------------------
3182
3183#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05003184#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05003185#Line # returns Pixmap if possible ##
Cary Clark154beea2017-10-26 07:58:48 -04003186Copies Bitmap pixel address, row bytes, and Image_Info to pixmap, if address
3187is available, and returns true. If pixel address is not available, return
3188false and leave pixmap unchanged.
3189
3190pixmap contents become invalid on any future change to Bitmap.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003191
3192#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
3193
3194#Return true if Bitmap has direct access to pixels ##
3195
3196#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04003197 SkBitmap bitmap;
3198 bitmap.allocPixels(SkImageInfo::MakeN32Premul(6, 11));
3199 SkCanvas offscreen(bitmap);
3200 offscreen.clear(SK_ColorWHITE);
3201 SkPaint paint;
3202 offscreen.drawString("?", 0, 10, paint);
3203 SkPixmap pixmap;
3204 if (bitmap.peekPixels(&pixmap)) {
3205 const SkPMColor* pixels = pixmap.addr32();
3206 SkPMColor pmWhite = pixels[0];
3207 for (int y = 0; y < bitmap.height(); ++y) {
3208 for (int x = 0; x < bitmap.width(); ++x) {
3209 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
3210 }
3211 SkDebugf("\n");
3212 }
3213 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003214 #StdOut
Cary Clark2f466242017-12-11 16:03:17 -05003215------
3216-xxx--
3217x---x-
3218----x-
3219---x--
3220--x---
3221--x---
3222------
3223--x---
3224--x---
Cary Clarka560c472017-11-27 10:44:06 -05003225------
Cary Clarkbc5697d2017-10-04 14:31:33 -04003226 #StdOut ##
3227##
3228
Cary Clark682c58d2018-05-16 07:07:07 -04003229#SeeAlso pixmap() installPixels readPixels writePixels
Cary Clarkbc5697d2017-10-04 14:31:33 -04003230
3231##
3232
3233# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05003234#Subtopic Utility
3235#Populate
3236#Line # rarely called management functions ##
3237##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003238
Cary Clark154beea2017-10-26 07:58:48 -04003239#Method void validate() const;
Cary Clark78de7512018-02-07 07:27:09 -05003240#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05003241#Line # asserts if Bitmap is invalid (debug only) ##
Cary Clark682c58d2018-05-16 07:07:07 -04003242Asserts if internal values are illegal or inconsistent. Only available if
Cary Clark154beea2017-10-26 07:58:48 -04003243SK_DEBUG is defined at compile time.
3244
3245#NoExample
3246##
3247
Cary Clark06c20f32018-03-20 15:53:27 -04003248#SeeAlso SkImageInfo::validate
Cary Clark154beea2017-10-26 07:58:48 -04003249
3250##
3251
3252# ------------------------------------------------------------------------------
3253
Cary Clarkbc5697d2017-10-04 14:31:33 -04003254#Class SkBitmap ##
3255
3256#Topic Bitmap ##
Cary Clark4855f782018-02-06 09:41:53 -05003257