blob: 584672af3fa2790ecc8729479e99438189f0e822 [file] [log] [blame]
Cary Clarkbc5697d2017-10-04 14:31:33 -04001#Topic Bitmap
2#Alias Bitmaps
3#Alias Bitmap_Reference
4
Cary Clark08895c42018-02-01 09:37:32 -05005#Subtopic Overview
Cary Clark4855f782018-02-06 09:41:53 -05006 #Subtopic Subtopic
Cary Clark08895c42018-02-01 09:37:32 -05007 #Populate
8 ##
9##
10
Cary Clarkbc5697d2017-10-04 14:31:33 -040011#Class SkBitmap
12
13Bitmap describes a two-dimensional raster pixel array. Bitmap is built on
14Image_Info, containing integer width and height, Color_Type and Alpha_Type
15describing the pixel format, and Color_Space describing the range of colors.
16Bitmap points to Pixel_Ref, which describes the physical array of pixels.
17Image_Info bounds may be located anywhere fully inside Pixel_Ref bounds.
18
19Bitmap can be drawn using Canvas. Bitmap can be a drawing destination for Canvas
20draw methods. Bitmap flexibility as a pixel container limits some optimizations
21available to the target platform.
22
23If pixel array is primarily read-only, use Image for better performance.
24If pixel array is primarily written to, use Surface for better performance.
25
26Declaring SkBitmap const prevents altering Image_Info: the Bitmap height, width,
27and so on cannot change. It does not affect Pixel_Ref: a caller may write its
28pixels. Declaring SkBitmap const affects Bitmap configuration, not its contents.
29
Cary Clarkbef063a2017-10-31 15:44:45 -040030Bitmap is not thread safe. Each thread must have its own copy of Bitmap fields,
Cary Clarkbc5697d2017-10-04 14:31:33 -040031although threads may share the underlying pixel array.
32
Cary Clark08895c42018-02-01 09:37:32 -050033#Subtopic Row_Bytes
34#Line # interval from one row to the next ##
Cary Clarkbc5697d2017-10-04 14:31:33 -040035Bitmap pixels may be contiguous, or may have a gap at the end of each row.
36Row_Bytes is the interval from one row to the next. Row_Bytes may be specified;
37sometimes passing zero will compute the Row_Bytes from the row width and the
38number of bytes in a pixel. Row_Bytes may be larger than the row requires. This
39is useful to position one or more Bitmaps within a shared pixel array.
40##
41
Cary Clark4855f782018-02-06 09:41:53 -050042#Subtopic Related_Function
Cary Clark08895c42018-02-01 09:37:32 -050043#Populate
Cary Clarkbc5697d2017-10-04 14:31:33 -040044##
45
Cary Clark4855f782018-02-06 09:41:53 -050046#Subtopic Constant
Cary Clark08895c42018-02-01 09:37:32 -050047#Populate
48##
Cary Clarkbc5697d2017-10-04 14:31:33 -040049
Cary Clark4855f782018-02-06 09:41:53 -050050#Subtopic Class_or_Struct
Cary Clark08895c42018-02-01 09:37:32 -050051#Populate
52##
Cary Clarkbc5697d2017-10-04 14:31:33 -040053
Cary Clark4855f782018-02-06 09:41:53 -050054#Subtopic Constructor
Cary Clark08895c42018-02-01 09:37:32 -050055#Populate
56##
Cary Clarkbc5697d2017-10-04 14:31:33 -040057
Cary Clark4855f782018-02-06 09:41:53 -050058#Subtopic Operator
59#Populate
60##
61
62#Subtopic Member_Function
Cary Clark08895c42018-02-01 09:37:32 -050063#Populate
64##
Cary Clarkbc5697d2017-10-04 14:31:33 -040065
66# ------------------------------------------------------------------------------
67
68#Class Allocator
Cary Clark08895c42018-02-01 09:37:32 -050069#Line # abstract subclass of HeapAllocator ##
Cary Clarkbc5697d2017-10-04 14:31:33 -040070#Code
71 class Allocator : public SkRefCnt {
72 public:
73 virtual bool allocPixelRef(SkBitmap* bitmap) = 0;
74 };
75##
76
77Abstract subclass of HeapAllocator.
78
79# ------------------------------------------------------------------------------
80
81#Method virtual bool allocPixelRef(SkBitmap* bitmap) = 0
82
83Allocates the pixel memory for the bitmap, given its dimensions and
84Color_Type. Returns true on success, where success means either setPixels
85or setPixelRef was called.
86
87#Param bitmap Bitmap containing Image_Info as input, and Pixel_Ref as output ##
88
89#Return true if Pixel_Ref was allocated ##
90
91#NoExample
92##
93
94#SeeAlso HeapAllocator
95
96##
97
98#Class Allocator ##
99
100# ------------------------------------------------------------------------------
101
102#Class HeapAllocator
Cary Clark08895c42018-02-01 09:37:32 -0500103#Line # allocates pixel memory from heap ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400104
105#Code
106 class HeapAllocator : public Allocator {
107 public:
108 bool allocPixelRef(SkBitmap* bitmap) override;
109 };
110##
111
Cary Clark78c110e2018-02-09 16:49:09 -0500112Subclass of SkBitmap::Allocator that returns a Pixel_Ref that allocates its pixel
113memory from the heap. This is the default SkBitmap::Allocator invoked by
Cary Clarkbc5697d2017-10-04 14:31:33 -0400114allocPixels.
115
116# ------------------------------------------------------------------------------
117
118#Method bool allocPixelRef(SkBitmap* bitmap) override
119
120Allocates the pixel memory for the bitmap, given its dimensions and
121Color_Type. Returns true on success, where success means either setPixels
122or setPixelRef was called.
123
124#Param bitmap Bitmap containing Image_Info as input, and Pixel_Ref as output ##
125
126#Return true if pixels are allocated ##
127
128#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400129 SkBitmap bitmap;
130 bitmap.setInfo(SkImageInfo::MakeN32(16, 16, kPremul_SkAlphaType));
131 SkDebugf("pixel address = %p\n", bitmap.getPixels());
132 SkBitmap::HeapAllocator stdalloc;
133 if (!stdalloc.allocPixelRef(&bitmap)) {
134 SkDebugf("pixel allocation failed\n");
135 } else {
136 SkDebugf("pixel address = %p\n", bitmap.getPixels());
137 }
138#StdOut
Cary Clark884dd7d2017-10-11 10:37:52 -0400139#Volatile
Ben Wagner29380bd2017-10-09 14:43:00 -0400140pixel address = (nil)
141pixel address = 0x560ddd0ac670
142##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400143##
144
Cary Clark78c110e2018-02-09 16:49:09 -0500145#SeeAlso SkBitmap::Allocator tryAllocPixels
Cary Clarkbc5697d2017-10-04 14:31:33 -0400146
147##
148
Cary Clark08895c42018-02-01 09:37:32 -0500149#Class HeapAllocator ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400150
151# ------------------------------------------------------------------------------
152
153#Method SkBitmap()
154
Cary Clarkab2621d2018-01-30 10:08:57 -0500155#Line # constructs with default values ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400156Creates an empty Bitmap without pixels, with kUnknown_SkColorType,
157kUnknown_SkAlphaType, and with a width and height of zero. Pixel_Ref origin is
158set to (0, 0). Bitmap is not volatile.
159
160Use setInfo to associate SkColorType, SkAlphaType, width, and height
161after Bitmap has been created.
162
163#Return empty Bitmap ##
164
165#Example
166void draw(SkCanvas* canvas) {
167 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500168 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
169 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clarkbc5697d2017-10-04 14:31:33 -0400170 SkBitmap bitmap;
171 for (int i = 0; i < 2; ++i) {
172 SkDebugf("width: %2d height: %2d", bitmap.width(), bitmap.height());
173 SkDebugf(" color: k%s_SkColorType", colors[bitmap.colorType()]);
174 SkDebugf(" alpha: k%s_SkAlphaType\n", alphas[bitmap.alphaType()]);
175 bitmap.setInfo(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType),
176 0);
177 }
178}
179#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400180width: 0 height: 0 color: kUnknown_SkColorType alpha: kUnknown_SkAlphaType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400181width: 25 height: 35 color: kRGBA_8888_SkColorType alpha: kOpaque_SkAlphaType
182##
183##
184
185#SeeAlso setInfo
186
187##
188
189# ------------------------------------------------------------------------------
190
191#Method SkBitmap(const SkBitmap& src)
192
Cary Clarkab2621d2018-01-30 10:08:57 -0500193#Line # shares ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400194Copies settings from src to returned Bitmap. Shares pixels if src has pixels
195allocated, so both bitmaps reference the same pixels.
196
197#Param src Bitmap to copy Image_Info, and share Pixel_Ref ##
198
199#Return copy of src ##
200
201#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400202void draw(SkCanvas* canvas) {
203 SkBitmap original;
Cary Clark681287e2018-03-16 11:34:15 -0400204 if (original.tryAllocPixels(
205 SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
206 SkDebugf("original has pixels before copy: %s\n", original.getPixels() ? "true" : "false");
207 SkBitmap copy(original);
208 SkDebugf("original has pixels after copy: %s\n", original.getPixels() ? "true" : "false");
209 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
210 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400211}
212#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400213original has pixels before copy: true
214original has pixels after copy: true
Cary Clarkbc5697d2017-10-04 14:31:33 -0400215copy has pixels: true
216##
217##
218
219#SeeAlso setInfo setPixelRef setPixels swap
220
221##
222
223# ------------------------------------------------------------------------------
224
225#Method SkBitmap(SkBitmap&& src)
226
Cary Clarkab2621d2018-01-30 10:08:57 -0500227#Line # takes ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400228Copies settings from src to returned Bitmap. Moves ownership of src pixels to
229Bitmap.
230
231#Param src Bitmap to copy Image_Info, and reassign Pixel_Ref ##
232
233#Return copy of src ##
234
235#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400236void draw(SkCanvas* canvas) {
237 SkBitmap original;
Cary Clark681287e2018-03-16 11:34:15 -0400238 if (original.tryAllocPixels(
239 SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
240 SkDebugf("original has pixels before move: %s\n", original.getPixels() ? "true" : "false");
241 SkBitmap copy(std::move(original));
242 SkDebugf("original has pixels after move: %s\n", original.getPixels() ? "true" : "false");
243 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
244 }
Ben Wagner29380bd2017-10-09 14:43:00 -0400245}
Cary Clarkbc5697d2017-10-04 14:31:33 -0400246#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400247original has pixels before move: true
248original has pixels after move: false
Cary Clarkbc5697d2017-10-04 14:31:33 -0400249copy has pixels: true
250##
251##
252
253#SeeAlso setInfo setPixelRef setPixels swap
254
255##
256
257# ------------------------------------------------------------------------------
258
259#Method ~SkBitmap()
260
Cary Clarkab2621d2018-01-30 10:08:57 -0500261#Line # releases ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400262Decrements Pixel_Ref reference count, if Pixel_Ref is not nullptr.
263
264#NoExample
265##
266
267#SeeAlso Pixel_Ref
268
269##
270
271# ------------------------------------------------------------------------------
272
273#Method SkBitmap& operator=(const SkBitmap& src)
274
Cary Clarkab2621d2018-01-30 10:08:57 -0500275#Line # shares ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400276Copies settings from src to returned Bitmap. Shares pixels if src has pixels
277allocated, so both bitmaps reference the same pixels.
278
279#Param src Bitmap to copy Image_Info, and share Pixel_Ref ##
280
281#Return copy of src ##
282
283#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400284void draw(SkCanvas* canvas) {
285 SkBitmap original;
Cary Clark681287e2018-03-16 11:34:15 -0400286 if (original.tryAllocPixels(
287 SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
288 SkDebugf("original has pixels before copy: %s\n", original.getPixels() ? "true" : "false");
289 SkBitmap copy = original;
290 SkDebugf("original has pixels after copy: %s\n", original.getPixels() ? "true" : "false");
291 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
292 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400293}
294#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400295original has pixels before copy: true
296original has pixels after copy: true
Cary Clarkbc5697d2017-10-04 14:31:33 -0400297copy has pixels: true
298##
299##
300
301#SeeAlso setInfo setPixelRef setPixels swap
302
303##
304
305# ------------------------------------------------------------------------------
306
307#Method SkBitmap& operator=(SkBitmap&& src)
308
Cary Clarkab2621d2018-01-30 10:08:57 -0500309#Line # takes ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400310Copies settings from src to returned Bitmap. Moves ownership of src pixels to
311Bitmap.
312
313#Param src Bitmap to copy Image_Info, and reassign Pixel_Ref ##
314
315#Return copy of src ##
316
317#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400318void draw(SkCanvas* canvas) {
319 SkBitmap original;
Cary Clark681287e2018-03-16 11:34:15 -0400320 if (original.tryAllocPixels(
321 SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
322 SkDebugf("original has pixels before move: %s\n", original.getPixels() ? "true" : "false");
323 SkBitmap copy = std::move(original);
324 SkDebugf("original has pixels after move: %s\n", original.getPixels() ? "true" : "false");
325 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
326 }
Ben Wagner29380bd2017-10-09 14:43:00 -0400327}
Cary Clarkbc5697d2017-10-04 14:31:33 -0400328#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400329original has pixels before move: true
330original has pixels after move: false
Cary Clarkbc5697d2017-10-04 14:31:33 -0400331copy has pixels: true
332##
333##
334
335#SeeAlso setInfo setPixelRef setPixels swap
336
337##
338
339# ------------------------------------------------------------------------------
340
341#Method void swap(SkBitmap& other)
Cary Clark78de7512018-02-07 07:27:09 -0500342#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -0500343#Line # exchanges Bitmap pair ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400344Swaps the fields of the two bitmaps.
345
346#Param other Bitmap exchanged with original ##
347
348#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400349void draw(SkCanvas* canvas) {
350 auto debugster = [](const char* prefix, const SkBitmap& b) -> void {
351 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500352 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
353 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Ben Wagner29380bd2017-10-09 14:43:00 -0400354 SkDebugf("%s width:%d height:%d colorType:k%s_SkColorType alphaType:k%s_SkAlphaType\n",
355 prefix, b.width(), b.height(), colors[b.colorType()], alphas[b.alphaType()]);
356 };
357 SkBitmap one, two;
Cary Clark681287e2018-03-16 11:34:15 -0400358 if (!one.tryAllocPixels(
359 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
360 return;
361 }
362 if (!two.tryAllocPixels(
363 SkImageInfo::Make(2, 2, kBGRA_8888_SkColorType, kPremul_SkAlphaType))) {
364 return;
365 }
Ben Wagner29380bd2017-10-09 14:43:00 -0400366 for (int index = 0; index < 2; ++index) {
367 debugster("one", one);
368 debugster("two", two);
369 one.swap(two);
370 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400371}
372#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400373one width:1 height:1 colorType:kRGBA_8888_SkColorType alphaType:kOpaque_SkAlphaType
374two width:2 height:2 colorType:kBGRA_8888_SkColorType alphaType:kPremul_SkAlphaType
375one width:2 height:2 colorType:kBGRA_8888_SkColorType alphaType:kPremul_SkAlphaType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400376two width:1 height:1 colorType:kRGBA_8888_SkColorType alphaType:kOpaque_SkAlphaType
377##
378##
379
380#SeeAlso SkBitmap(SkBitmap&& src) operator=(SkBitmap&& src)
381
382##
383
384# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -0500385#Subtopic Property
386#Populate
387#Line # metrics and attributes ##
388##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400389
Hal Canary99578d22017-12-14 21:13:47 -0500390#Method const SkPixmap& pixmap() const
Cary Clark78de7512018-02-07 07:27:09 -0500391#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500392#Line # returns Pixmap ##
Hal Canary99578d22017-12-14 21:13:47 -0500393Returns a constant reference to the Pixmap holding the Bitmap pixel
394address, row bytes, and Image_Info.
Cary Clark0c5f5462017-12-15 11:21:51 -0500395
Cary Clarkac47b882018-01-11 10:35:44 -0500396#Return reference to Pixmap describing this Bitmap ##
Cary Clark0c5f5462017-12-15 11:21:51 -0500397
398#Example
399 SkBitmap bitmap;
400 bitmap.allocPixels(SkImageInfo::MakeN32Premul(10, 11));
401 SkCanvas offscreen(bitmap);
402 offscreen.clear(SK_ColorWHITE);
403 SkPaint paint;
404 offscreen.drawString("&", 0, 10, paint);
Hal Canary99578d22017-12-14 21:13:47 -0500405 const SkPixmap& pixmap = bitmap.pixmap();
Cary Clark0c5f5462017-12-15 11:21:51 -0500406 if (pixmap.addr()) {
Hal Canary99578d22017-12-14 21:13:47 -0500407 SkPMColor pmWhite = *pixmap.addr32(0, 0);
408 for (int y = 0; y < pixmap.height(); ++y) {
409 for (int x = 0; x < pixmap.width(); ++x) {
410 SkDebugf("%c", *pixmap.addr32(x, y) == pmWhite ? '-' : 'x');
Cary Clark0c5f5462017-12-15 11:21:51 -0500411 }
412 SkDebugf("\n");
413 }
414 }
415 #StdOut
416----------
417---xx-----
418--x--x----
419--x-------
420--xx------
421--x-x---x-
422-x---x--x-
423-x----xx--
424-xx---x---
425--xxxx-xx-
426----------
427 #StdOut ##
428
429##
430
431#SeeAlso peekPixels installPixels readPixels writePixels
432
433##
434
435# ------------------------------------------------------------------------------
436
Cary Clarkbc5697d2017-10-04 14:31:33 -0400437#Method const SkImageInfo& info() const
Cary Clark78de7512018-02-07 07:27:09 -0500438#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500439#Line # returns Image_Info ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400440Returns width, height, Alpha_Type, Color_Type, and Color_Space.
441
442#Return reference to Image_Info ##
443
444#Example
445#Image 4
Ben Wagner29380bd2017-10-09 14:43:00 -0400446void draw(SkCanvas* canvas) {
447 // SkBitmap source; // pre-populated with soccer ball by fiddle.skia.org
448 const SkImageInfo& info = source.info();
449 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500450 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
451 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Ben Wagner29380bd2017-10-09 14:43:00 -0400452 SkDebugf("width: %d height: %d color: %s alpha: %s\n", info.width(), info.height(),
453 colors[info.colorType()], alphas[info.alphaType()]);
454#StdOut
455width: 56 height: 56 color: BGRA_8888 alpha: Opaque
456##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400457}
458##
459
460#SeeAlso Image_Info
461
462##
463
464# ------------------------------------------------------------------------------
465
466#Method int width() const
Cary Clark78de7512018-02-07 07:27:09 -0500467#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500468#Line # returns pixel column count ##
Cary Clarka560c472017-11-27 10:44:06 -0500469Returns pixel count in each row. Should be equal or less than:
Cary Clark154beea2017-10-26 07:58:48 -0400470
Cary Clarkbc5697d2017-10-04 14:31:33 -0400471#Formula
472rowBytes() / info().bytesPerPixel()
473##
474.
475
476Maybe be less than pixelRef().width(). Will not exceed pixelRef().width() less
477pixelRefOrigin().fX.
478
479#Return pixel width in Image_Info ##
480
481#Example
482 SkImageInfo info = SkImageInfo::MakeA8(16, 32);
483 SkBitmap bitmap;
484 bitmap.setInfo(info);
485 SkDebugf("bitmap width: %d info width: %d\n", bitmap.width(), info.width());
486#StdOut
487bitmap width: 16 info width: 16
488##
489##
490
491#SeeAlso height() SkPixelRef::width() SkImageInfo::width()
492
493##
494
495# ------------------------------------------------------------------------------
496
497#Method int height() const
Cary Clark78de7512018-02-07 07:27:09 -0500498#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500499#Line # returns pixel row count ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400500Returns pixel row count.
501
502Maybe be less than pixelRef().height(). Will not exceed pixelRef().height() less
503pixelRefOrigin().fY.
504
505#Return pixel height in Image_Info ##
506
507#Example
508 SkImageInfo info = SkImageInfo::MakeA8(16, 32);
509 SkBitmap bitmap;
510 bitmap.setInfo(info);
511 SkDebugf("bitmap height: %d info height: %d\n", bitmap.height(), info.height());
512#StdOut
513bitmap height: 32 info height: 32
514##
515##
516
517#SeeAlso width() SkPixelRef::height() SkImageInfo::height()
518
519##
520
521# ------------------------------------------------------------------------------
522
523#Method SkColorType colorType() const
Cary Clark78de7512018-02-07 07:27:09 -0500524#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500525#Line # returns Image_Info Color_Type ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500526Returns Color_Type, one of: #list_of_color_types#.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400527
528#Return Color_Type in Image_Info ##
529
530#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500531 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
532 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Ben Wagner29380bd2017-10-09 14:43:00 -0400533 SkBitmap bitmap;
534 bitmap.setInfo(SkImageInfo::MakeA8(16, 32));
535 SkDebugf("color type: k" "%s" "_SkColorType\n", colors[bitmap.colorType()]);
Cary Clarkbc5697d2017-10-04 14:31:33 -0400536#StdOut
Cary Clarkab2621d2018-01-30 10:08:57 -0500537color type: kAlpha_8_SkColorType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400538##
539##
540
541#SeeAlso alphaType() SkImageInfo::colorType
542
543##
544
545# ------------------------------------------------------------------------------
546
547#Method SkAlphaType alphaType() const
Cary Clark78de7512018-02-07 07:27:09 -0500548#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500549#Line # returns Image_Info Alpha_Type ##
Cary Clark681287e2018-03-16 11:34:15 -0400550Returns Alpha_Type, one of: #list_of_alpha_types#.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400551
552#Return Alpha_Type in Image_Info ##
553
554#Example
555 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
556 SkPixmap pixmap(SkImageInfo::MakeA8(16, 32), nullptr, 64);
557 SkDebugf("alpha type: k" "%s" "_SkAlphaType\n", alphas[pixmap.alphaType()]);
558#StdOut
559alpha type: kPremul_SkAlphaType
560##
561##
562
563#SeeAlso colorType() SkImageInfo::alphaType
564
565##
566
567# ------------------------------------------------------------------------------
568
569#Method SkColorSpace* colorSpace() const
Cary Clark78de7512018-02-07 07:27:09 -0500570#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500571#Line # returns Image_Info Color_Space ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400572Returns Color_Space, the range of colors, associated with Image_Info. The
573reference count of Color_Space is unchanged. The returned Color_Space is
574immutable.
575
Cary Clark2f466242017-12-11 16:03:17 -0500576#Return Color_Space in Image_Info, or nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400577
578#Example
579#Description
580SkColorSpace::MakeSRGBLinear creates Color_Space with linear gamma
581and an sRGB gamut. This Color_Space gamma is not close to sRGB gamma.
582##
Ben Wagner29380bd2017-10-09 14:43:00 -0400583 SkBitmap bitmap;
Cary Clarkbc5697d2017-10-04 14:31:33 -0400584 bitmap.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
585 SkColorSpace::MakeSRGBLinear()));
586 SkColorSpace* colorSpace = bitmap.colorSpace();
587 SkDebugf("gammaCloseToSRGB: %s gammaIsLinear: %s isSRGB: %s\n",
588 colorSpace->gammaCloseToSRGB() ? "true" : "false",
589 colorSpace->gammaIsLinear() ? "true" : "false",
590 colorSpace->isSRGB() ? "true" : "false");
591#StdOut
592gammaCloseToSRGB: false gammaIsLinear: true isSRGB: false
593##
594##
595
596#SeeAlso Color_Space SkImageInfo::colorSpace
597
598##
599
600# ------------------------------------------------------------------------------
601
602#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark78de7512018-02-07 07:27:09 -0500603#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500604#Line # returns Image_Info Color_Space ##
Cary Clark681287e2018-03-16 11:34:15 -0400605Returns smart pointer to Color_Space, the range of colors, associated with
Cary Clarkbc5697d2017-10-04 14:31:33 -0400606Image_Info. The smart pointer tracks the number of objects sharing this
607Color_Space reference so the memory is released when the owners destruct.
608
609The returned Color_Space is immutable.
610
611#Return Color_Space in Image_Info wrapped in a smart pointer ##
612
613#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400614 SkBitmap bitmap1, bitmap2;
Cary Clarkbc5697d2017-10-04 14:31:33 -0400615 bitmap1.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
616 SkColorSpace::MakeSRGBLinear()));
617 bitmap2.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
618 bitmap1.refColorSpace()));
619 SkColorSpace* colorSpace = bitmap2.colorSpace();
620 SkDebugf("gammaCloseToSRGB: %s gammaIsLinear: %s isSRGB: %s\n",
621 colorSpace->gammaCloseToSRGB() ? "true" : "false",
622 colorSpace->gammaIsLinear() ? "true" : "false",
623 colorSpace->isSRGB() ? "true" : "false");
624#StdOut
625gammaCloseToSRGB: false gammaIsLinear: true isSRGB: false
626##
627##
628
629#SeeAlso Color_Space SkImageInfo::colorSpace
630
631##
632
633# ------------------------------------------------------------------------------
634
635#Method int bytesPerPixel() const
Cary Clark78de7512018-02-07 07:27:09 -0500636#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500637#Line # returns number of bytes in pixel based on Color_Type ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400638Returns number of bytes per pixel required by Color_Type.
639Returns zero if colorType( is kUnknown_SkColorType.
640
641#Return bytes in pixel ##
642
643#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500644 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
645 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clarkbc5697d2017-10-04 14:31:33 -0400646 SkImageInfo info = SkImageInfo::MakeA8(1, 1);
647 SkBitmap bitmap;
Cary Clark1a8d7622018-03-05 13:26:16 -0500648 for (SkColorType colorType : { #list_of_color_types#
649 } ) {
Cary Clarkbc5697d2017-10-04 14:31:33 -0400650 bitmap.setInfo(info.makeColorType(colorType));
651 SkDebugf("color: k" "%s" "_SkColorType" "%*s" "bytesPerPixel: %d\n",
Cary Clarkab2621d2018-01-30 10:08:57 -0500652 colors[colorType], 13 - strlen(colors[colorType]), " ",
Cary Clarkbc5697d2017-10-04 14:31:33 -0400653 bitmap.bytesPerPixel());
654 }
655#StdOut
Cary Clark98aebac2018-03-13 09:02:35 -0400656color: kUnknown_SkColorType bytesPerPixel: 0
657color: kAlpha_8_SkColorType bytesPerPixel: 1
658color: kRGB_565_SkColorType bytesPerPixel: 2
659color: kARGB_4444_SkColorType bytesPerPixel: 2
660color: kRGBA_8888_SkColorType bytesPerPixel: 4
661color: kRGB_888x_SkColorType bytesPerPixel: 4
662color: kBGRA_8888_SkColorType bytesPerPixel: 4
663color: kRGBA_1010102_SkColorType bytesPerPixel: 4
664color: kRGB_101010x_SkColorType bytesPerPixel: 4
665color: kGray_8_SkColorType bytesPerPixel: 1
Cary Clarkab2621d2018-01-30 10:08:57 -0500666color: kRGBA_F16_SkColorType bytesPerPixel: 8
Cary Clarkbc5697d2017-10-04 14:31:33 -0400667##
668##
669
Cary Clark681287e2018-03-16 11:34:15 -0400670#SeeAlso rowBytes rowBytesAsPixels width shiftPerPixel SkImageInfo::bytesPerPixel
Cary Clarkbc5697d2017-10-04 14:31:33 -0400671
672##
673
674# ------------------------------------------------------------------------------
675
676#Method int rowBytesAsPixels() const
Cary Clark78de7512018-02-07 07:27:09 -0500677#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500678#Line # returns interval between rows in pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400679Returns number of pixels that fit on row. Should be greater than or equal to
680width().
681
682#Return maximum pixels per row ##
683
684#Example
685 SkBitmap bitmap;
686 for (int rowBytes : { 4, 5, 6, 7, 8} ) {
687 bitmap.setInfo(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), rowBytes);
688 SkDebugf("rowBytes: %d rowBytesAsPixels: %d\n", rowBytes, bitmap.rowBytesAsPixels());
689 }
690#StdOut
691rowBytes: 4 rowBytesAsPixels: 1
692rowBytes: 5 rowBytesAsPixels: 1
693rowBytes: 6 rowBytesAsPixels: 1
694rowBytes: 7 rowBytesAsPixels: 1
695rowBytes: 8 rowBytesAsPixels: 2
696##
697##
698
699#SeeAlso rowBytes shiftPerPixel width bytesPerPixel
700
701##
702
703# ------------------------------------------------------------------------------
704
705#Method int shiftPerPixel() const
Cary Clark78de7512018-02-07 07:27:09 -0500706#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500707#Line # returns bit shift from pixels to bytes ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400708Returns bit shift converting row bytes to row pixels.
709Returns zero for kUnknown_SkColorType.
710
711#Return one of: 0, 1, 2, 3; left shift to convert pixels to bytes ##
712
713#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500714 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
715 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clarkbc5697d2017-10-04 14:31:33 -0400716 SkImageInfo info = SkImageInfo::MakeA8(1, 1);
717 SkBitmap bitmap;
Cary Clark1a8d7622018-03-05 13:26:16 -0500718 for (SkColorType colorType : { #list_of_color_types#
719 } ) {
Cary Clarkbc5697d2017-10-04 14:31:33 -0400720 bitmap.setInfo(info.makeColorType(colorType));
721 SkDebugf("color: k" "%s" "_SkColorType" "%*s" "shiftPerPixel: %d\n",
Cary Clark1a8d7622018-03-05 13:26:16 -0500722 colors[colorType], 14 - strlen(colors[colorType]), " ",
Cary Clarkbc5697d2017-10-04 14:31:33 -0400723 bitmap.shiftPerPixel());
724 }
725#StdOut
Cary Clark98aebac2018-03-13 09:02:35 -0400726color: kUnknown_SkColorType shiftPerPixel: 0
727color: kAlpha_8_SkColorType shiftPerPixel: 0
728color: kRGB_565_SkColorType shiftPerPixel: 1
729color: kARGB_4444_SkColorType shiftPerPixel: 1
730color: kRGBA_8888_SkColorType shiftPerPixel: 2
731color: kRGB_888x_SkColorType shiftPerPixel: 2
732color: kBGRA_8888_SkColorType shiftPerPixel: 2
733color: kRGBA_1010102_SkColorType shiftPerPixel: 2
734color: kRGB_101010x_SkColorType shiftPerPixel: 2
735color: kGray_8_SkColorType shiftPerPixel: 0
Cary Clark1a8d7622018-03-05 13:26:16 -0500736color: kRGBA_F16_SkColorType shiftPerPixel: 3
Cary Clarkbc5697d2017-10-04 14:31:33 -0400737##
738##
739
740#SeeAlso rowBytes rowBytesAsPixels width bytesPerPixel
741
742##
743
744# ------------------------------------------------------------------------------
745
746#Method bool empty() const
Cary Clark78de7512018-02-07 07:27:09 -0500747#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500748#Line # returns true if Image_Info has zero width() or height() ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400749Returns true if either width() or height() are zero.
750
751Does not check if Pixel_Ref is nullptr; call drawsNothing to check width(),
752height(), and Pixel_Ref.
753
754#Return true if dimensions do not enclose area ##
755
756#Example
757 SkBitmap bitmap;
758 for (int width : { 0, 2 } ) {
759 for (int height : { 0, 2 } ) {
760 bitmap.setInfo(SkImageInfo::MakeA8(width, height));
761 SkDebugf("width: %d height: %d empty: %s\n", width, height,
762 bitmap.empty() ? "true" : "false");
763 }
764 }
765#StdOut
766width: 0 height: 0 empty: true
767width: 0 height: 2 empty: true
768width: 2 height: 0 empty: true
769width: 2 height: 2 empty: false
770##
771##
772
773#SeeAlso height() width() drawsNothing
774
775##
776
777# ------------------------------------------------------------------------------
778
779#Method bool isNull() const
Cary Clark78de7512018-02-07 07:27:09 -0500780#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500781#Line # returns true if Pixel_Ref is nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400782Return true if Pixel_Ref is nullptr.
783
784Does not check if width() or height() are zero; call drawsNothing to check
785width(), height(), and Pixel_Ref.
786
787#Return true if no Pixel_Ref is associated ##
788
789#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400790 SkBitmap bitmap;
791 SkDebugf("empty bitmap does %shave pixels\n", bitmap.isNull() ? "not " : "");
792 bitmap.setInfo(SkImageInfo::MakeA8(8, 8));
793 SkDebugf("bitmap with dimensions does %shave pixels\n", bitmap.isNull() ? "not " : "");
794 bitmap.allocPixels();
795 SkDebugf("allocated bitmap does %shave pixels\n", bitmap.isNull() ? "not " : "");
Cary Clarkbc5697d2017-10-04 14:31:33 -0400796#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400797empty bitmap does not have pixels
798bitmap with dimensions does not have pixels
Cary Clarkbc5697d2017-10-04 14:31:33 -0400799allocated bitmap does have pixels
800##
801##
802
803#SeeAlso empty() drawsNothing pixelRef
804
805##
806
807# ------------------------------------------------------------------------------
808
809#Method bool drawsNothing() const
Cary Clark78de7512018-02-07 07:27:09 -0500810#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500811#Line # returns true if no width(), no height(), or no Pixel_Ref ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400812Return true if width() or height() are zero, or if Pixel_Ref is nullptr.
813If true, Bitmap has no effect when drawn or drawn into.
814
815#Return true if drawing has no effect ##
816
817#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400818 SkBitmap bitmap;
819 for (int w : { 0, 8 } ) {
820 for (bool allocate : { false, true} ) {
821 bitmap.setInfo(SkImageInfo::MakeA8(w, 8));
822 allocate ? bitmap.allocPixels() : (void) 0 ;
823 SkDebugf("empty:%s isNull:%s drawsNothing:%s\n", bitmap.empty() ? "true " : "false",
824 bitmap.isNull() ? "true " : "false", bitmap.drawsNothing() ? "true" : "false");
825 }
826 }
827#StdOut
828empty:true isNull:true drawsNothing:true
829empty:true isNull:false drawsNothing:true
830empty:false isNull:true drawsNothing:true
831empty:false isNull:false drawsNothing:false
832##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400833##
834
835#SeeAlso empty() isNull pixelRef
836
837##
838
839# ------------------------------------------------------------------------------
840
841#Method size_t rowBytes() const
Cary Clark78de7512018-02-07 07:27:09 -0500842#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500843#Line # returns interval between rows in bytes ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400844Returns row bytes, the interval from one pixel row to the next. Row bytes
845is at least as large as
846#Formula
847width() * info().bytesPerPixel()
848##
849.
850
851Returns zero if colorType is kUnknown_SkColorType, or if row bytes supplied to
852setInfo is not large enough to hold a row of pixels.
853
854#Return byte length of pixel row ##
855
856#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400857 SkBitmap bitmap;
858 for (int rowBytes : { 2, 8 } ) {
859 bool result = bitmap.setInfo(SkImageInfo::MakeA8(4, 4), rowBytes);
860 SkDebugf("setInfo returned:%s rowBytes:%d\n", result ? "true " : "false", bitmap.rowBytes());
861 }
862#StdOut
863setInfo returned:false rowBytes:0
864setInfo returned:true rowBytes:8
865##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400866##
867
868#SeeAlso info() setInfo SkImageInfo::minRowBytes
869
870##
871
872# ------------------------------------------------------------------------------
873
874#Method bool setAlphaType(SkAlphaType alphaType)
Cary Clark78de7512018-02-07 07:27:09 -0500875#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -0500876#Line # sets Alpha_Type of shared pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400877Sets Alpha_Type, if alphaType is compatible with Color_Type.
878Returns true unless alphaType is kUnknown_SkAlphaType and current Alpha_Type
879is not kUnknown_SkAlphaType.
880
881Returns true if Color_Type is kUnknown_SkColorType. alphaType is ignored, and
882Alpha_Type remains kUnknown_SkAlphaType.
883
884Returns true if Color_Type is kRGB_565_SkColorType or kGray_8_SkColorType.
885alphaType is ignored, and Alpha_Type remains kOpaque_SkAlphaType.
886
887If Color_Type is kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
888kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: returns true unless
889alphaType is kUnknown_SkAlphaType and Alpha_Type is not kUnknown_SkAlphaType.
890If Alpha_Type is kUnknown_SkAlphaType, alphaType is ignored.
891
892If Color_Type is kAlpha_8_SkColorType, returns true unless
893alphaType is kUnknown_SkAlphaType and Alpha_Type is not kUnknown_SkAlphaType.
894If Alpha_Type is kUnknown_SkAlphaType, alphaType is ignored. If alphaType is
895kUnpremul_SkAlphaType, it is treated as kPremul_SkAlphaType.
896
897This changes Alpha_Type in Pixel_Ref; all bitmaps sharing Pixel_Ref
898are affected.
899
Cary Clark681287e2018-03-16 11:34:15 -0400900#Param alphaType one of: #list_of_alpha_types#
Cary Clarkbc5697d2017-10-04 14:31:33 -0400901##
902
903#Return true if Alpha_Type is set ##
904
905#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400906void draw(SkCanvas* canvas) {
Cary Clarkab2621d2018-01-30 10:08:57 -0500907 const char* colors[] = { "Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
908 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16" };
909 const char* alphas[] = {"Unknown ", "Opaque ", "Premul ", "Unpremul"};
910 SkBitmap bitmap;
Cary Clark681287e2018-03-16 11:34:15 -0400911 SkAlphaType alphaTypes[] = { #list_of_alpha_types#
912 };
Cary Clarkab2621d2018-01-30 10:08:57 -0500913 SkDebugf("%88s", "Canonical Unknown Opaque Premul Unpremul\n");
Cary Clark1a8d7622018-03-05 13:26:16 -0500914 for (SkColorType colorType : { #list_of_color_types#
915 } ) {
Ben Wagner29380bd2017-10-09 14:43:00 -0400916 for (SkAlphaType canonicalAlphaType : alphaTypes) {
917 SkColorTypeValidateAlphaType(colorType, kUnknown_SkAlphaType, &canonicalAlphaType );
918 SkDebugf("%10s %10s ", colors[(int) colorType], alphas[(int) canonicalAlphaType ]);
919 for (SkAlphaType alphaType : alphaTypes) {
920 bitmap.setInfo(SkImageInfo::Make(4, 4, colorType, canonicalAlphaType));
921 bool result = bitmap.setAlphaType(alphaType);
922 SkDebugf("%s %s ", result ? "true " : "false", alphas[(int) bitmap.alphaType()]);
923 }
924 SkDebugf("\n");
925 }
926 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400927}
928##
929
930#SeeAlso Alpha_Type Color_Type Image_Info setInfo
931
932##
933
934# ------------------------------------------------------------------------------
935
936#Method void* getPixels() const
Cary Clark78de7512018-02-07 07:27:09 -0500937#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500938#Line # returns address of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400939Returns pixel address, the base address corresponding to the pixel origin.
940
941#Return pixel address ##
942
943#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400944 SkBitmap bitmap;
945 bitmap.setInfo(SkImageInfo::MakeN32(4, 4, kPremul_SkAlphaType));
946 bitmap.allocPixels();
947 bitmap.eraseColor(0x00000000);
948 void* baseAddr = bitmap.getPixels();
949 *(SkPMColor*)baseAddr = 0xFFFFFFFF;
950 SkDebugf("bitmap.getColor(0, 1) %c= 0x00000000\n",
951 bitmap.getColor(0, 1) == 0x00000000 ? '=' : '!');
952 SkDebugf("bitmap.getColor(0, 0) %c= 0xFFFFFFFF\n",
953 bitmap.getColor(0, 0) == 0xFFFFFFFF ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -0400954#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400955bitmap.getColor(0, 1) == 0x00000000
Cary Clarkbc5697d2017-10-04 14:31:33 -0400956bitmap.getColor(0, 0) == 0xFFFFFFFF
957##
958##
959
960#SeeAlso isNull drawsNothing
961
962##
963
964# ------------------------------------------------------------------------------
965
966#Method size_t computeByteSize() const
Cary Clark78de7512018-02-07 07:27:09 -0500967#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -0500968#Line # returns size required for pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400969Returns minimum memory required for pixel storage.
970Does not include unused memory on last row when rowBytesAsPixels exceeds width().
971Returns zero if result does not fit in size_t.
972Returns zero if height() or width() is 0.
973Returns height() times rowBytes if colorType is kUnknown_SkColorType.
974
975#Return size in bytes of image buffer ##
976
977#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400978 SkBitmap bitmap;
Cary Clarkbc5697d2017-10-04 14:31:33 -0400979 for (int width : { 1, 1000, 1000000 } ) {
980 for (int height: { 1, 1000, 1000000 } ) {
981 SkImageInfo imageInfo = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
982 bitmap.setInfo(imageInfo, width * 5);
983 SkDebugf("width: %7d height: %7d computeByteSize: %13lld\n", width, height,
984 bitmap.computeByteSize());
985 }
986 }
987#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400988width: 1 height: 1 computeByteSize: 4
989width: 1 height: 1000 computeByteSize: 4999
990width: 1 height: 1000000 computeByteSize: 4999999
991width: 1000 height: 1 computeByteSize: 4000
992width: 1000 height: 1000 computeByteSize: 4999000
993width: 1000 height: 1000000 computeByteSize: 4999999000
994width: 1000000 height: 1 computeByteSize: 4000000
995width: 1000000 height: 1000 computeByteSize: 4999000000
Cary Clarkbc5697d2017-10-04 14:31:33 -0400996width: 1000000 height: 1000000 computeByteSize: 4999999000000
997##
998##
999
1000#SeeAlso SkImageInfo::computeByteSize
1001
1002##
1003
1004# ------------------------------------------------------------------------------
1005
Cary Clarkbc5697d2017-10-04 14:31:33 -04001006#Method bool isImmutable() const
Cary Clark78de7512018-02-07 07:27:09 -05001007#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001008#Line # returns true if pixels will not change ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001009Returns true if pixels can not change.
1010
1011Most immutable Bitmap checks trigger an assert only on debug builds.
1012
1013#Return true if pixels are immutable ##
1014
1015#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001016 SkBitmap original;
1017 SkImageInfo info = SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1018 if (original.tryAllocPixels(info)) {
1019 original.setImmutable();
1020 SkBitmap copy;
1021 original.extractSubset(&copy, {5, 10, 15, 20});
1022 SkDebugf("original is " "%s" "immutable\n", original.isImmutable() ? "" : "not ");
1023 SkDebugf("copy is " "%s" "immutable\n", copy.isImmutable() ? "" : "not ");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001024 }
1025#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001026original is immutable
Cary Clarkbc5697d2017-10-04 14:31:33 -04001027copy is immutable
1028##
1029##
1030
1031#SeeAlso setImmutable SkPixelRef::isImmutable SkImage
1032
1033##
1034
1035# ------------------------------------------------------------------------------
1036
1037#Method void setImmutable()
Cary Clark78de7512018-02-07 07:27:09 -05001038#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05001039#Line # marks that pixels will not change ##
Cary Clark154beea2017-10-26 07:58:48 -04001040Sets internal flag to mark Bitmap as immutable. Once set, pixels can not change.
1041Any other bitmap sharing the same Pixel_Ref are also marked as immutable.
1042Once Pixel_Ref is marked immutable, the setting cannot be cleared.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001043
1044Writing to immutable Bitmap pixels triggers an assert on debug builds.
1045
1046#Example
1047#Description
1048Triggers assert if SK_DEBUG is true, runs fine otherwise.
1049##
Ben Wagner29380bd2017-10-09 14:43:00 -04001050 SkBitmap bitmap;
1051 bitmap.setInfo(SkImageInfo::MakeN32(4, 4, kPremul_SkAlphaType));
1052 bitmap.allocPixels();
1053 SkCanvas offscreen(bitmap);
1054 SkDebugf("draw white\n");
1055 offscreen.clear(SK_ColorWHITE);
1056 bitmap.setImmutable();
1057 SkDebugf("draw black\n");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001058 offscreen.clear(SK_ColorBLACK);
1059##
1060
1061#SeeAlso isImmutable SkPixelRef::setImmutable SkImage
1062
1063##
1064
1065# ------------------------------------------------------------------------------
1066
1067#Method bool isOpaque() const
Cary Clark78de7512018-02-07 07:27:09 -05001068#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001069#Line # returns true if Image_Info describes opaque pixels ##
Cary Clark681287e2018-03-16 11:34:15 -04001070
1071Returns true if Alpha_Type is set to hint that all pixels are opaque; their
1072Color_Alpha value is implicitly or explicitly 1.0. If true, and all pixels are
1073not opaque, Skia may draw incorrectly.
1074
Cary Clarkbc5697d2017-10-04 14:31:33 -04001075Does not check if Color_Type allows Alpha, or if any pixel value has
1076transparency.
1077
Cary Clark681287e2018-03-16 11:34:15 -04001078#Return true if Image_Info Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001079
1080#Example
1081#Description
1082 isOpaque ignores whether all pixels are opaque or not.
1083##
Ben Wagner29380bd2017-10-09 14:43:00 -04001084 const int height = 2;
1085 const int width = 2;
1086 SkBitmap bitmap;
1087 bitmap.setInfo(SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType));
1088 for (int index = 0; index < 2; ++index) {
1089 bitmap.allocPixels();
1090 bitmap.eraseColor(0x00000000);
1091 SkDebugf("isOpaque: %s\n", bitmap.isOpaque() ? "true" : "false");
1092 bitmap.eraseColor(0xFFFFFFFF);
1093 SkDebugf("isOpaque: %s\n", bitmap.isOpaque() ? "true" : "false");
1094 bitmap.setInfo(bitmap.info().makeAlphaType(kOpaque_SkAlphaType));
1095 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001096#StdOut
1097isOpaque: false
1098isOpaque: false
1099isOpaque: true
1100isOpaque: true
1101##
1102##
1103
1104#SeeAlso ComputeIsOpaque SkImageInfo::isOpaque
1105
1106##
1107
1108# ------------------------------------------------------------------------------
1109
1110#Method bool isVolatile() const
Cary Clark78de7512018-02-07 07:27:09 -05001111#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001112#Line # returns true if pixels should not be cached ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001113If true, provides a hint to caller that pixels should not
1114be cached. Only true if setIsVolatile has been called to mark as volatile.
1115
1116Volatile state is not shared by other bitmaps sharing the same Pixel_Ref.
1117
1118#Return true if marked volatile ##
1119
1120#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001121 SkBitmap original;
1122 SkImageInfo info = SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1123 if (original.tryAllocPixels(info)) {
1124 original.setIsVolatile(true);
1125 SkBitmap copy;
1126 original.extractSubset(&copy, {5, 10, 15, 20});
1127 SkDebugf("original is " "%s" "volatile\n", original.isVolatile() ? "" : "not ");
1128 SkDebugf("copy is " "%s" "volatile\n", copy.isImmutable() ? "" : "not ");
1129 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001130#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001131original is volatile
Cary Clarkbc5697d2017-10-04 14:31:33 -04001132copy is not volatile
1133##
1134##
1135
1136#SeeAlso setIsVolatile
1137
1138##
1139
1140# ------------------------------------------------------------------------------
1141
1142#Method void setIsVolatile(bool isVolatile)
Cary Clark78de7512018-02-07 07:27:09 -05001143#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05001144#Line # marks if pixels should not be cached ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001145Sets if pixels should be read from Pixel_Ref on every access. Bitmaps are not
1146volatile by default; a GPU back end may upload pixel values expecting them to be
1147accessed repeatedly. Marking temporary Bitmaps as volatile provides a hint to
1148Device that the Bitmap pixels should not be cached. This can
1149improve performance by avoiding overhead and reducing resource
1150consumption on Device.
1151
1152#Param isVolatile true if backing pixels are temporary ##
1153
1154#Example
1155#Height 20
Ben Wagner29380bd2017-10-09 14:43:00 -04001156 SkBitmap bitmap;
1157 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
1158 bitmap.allocPixels();
1159 bitmap.eraseColor(SK_ColorRED);
1160 canvas->scale(16, 16);
1161 canvas->drawBitmap(bitmap, 0, 0);
1162 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorBLUE);
1163 canvas->drawBitmap(bitmap, 2, 0);
1164 bitmap.setIsVolatile(true);
1165 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorGREEN);
1166 canvas->drawBitmap(bitmap, 4, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001167##
1168
1169#SeeAlso isVolatile
1170
1171##
1172
1173# ------------------------------------------------------------------------------
1174
1175#Method void reset()
Cary Clark78de7512018-02-07 07:27:09 -05001176#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -05001177#Line # sets to default values, releases pixel ownership ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001178Resets to its initial state; all fields are set to zero, as if Bitmap had
1179been initialized by SkBitmap().
1180
1181Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to
1182kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType.
1183
1184If Pixel_Ref is allocated, its reference count is decreased by one, releasing
1185its memory if Bitmap is the sole owner.
1186
1187#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001188 SkBitmap bitmap;
1189 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
1190 bitmap.allocPixels();
1191 SkDebugf("width:%d height:%d isNull:%s\n", bitmap.width(), bitmap.height(),
1192 bitmap.isNull() ? "true" : "false");
1193 bitmap.reset();
1194 SkDebugf("width:%d height:%d isNull:%s\n", bitmap.width(), bitmap.height(),
1195 bitmap.isNull() ? "true" : "false");
1196#StdOut
1197width:1 height:1 isNull:false
1198width:0 height:0 isNull:true
1199##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001200##
1201
1202#SeeAlso SkBitmap() SkAlphaType SkColorType
1203
1204##
1205
1206# ------------------------------------------------------------------------------
1207
1208#Method static bool ComputeIsOpaque(const SkBitmap& bm)
Cary Clark78de7512018-02-07 07:27:09 -05001209#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05001210#Line # returns true if all pixels are opaque ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001211Returns true if all pixels are opaque. Color_Type determines how pixels
1212are encoded, and whether pixel describes Alpha. Returns true for Color_Types
1213without alpha in each pixel; for other Color_Types, returns true if all
1214pixels have alpha values equivalent to 1.0 or greater.
1215
1216For Color_Types kRGB_565_SkColorType or kGray_8_SkColorType: always
1217returns true. For Color_Types kAlpha_8_SkColorType, kBGRA_8888_SkColorType,
1218kRGBA_8888_SkColorType: returns true if all pixel Alpha values are 255.
1219For Color_Type kARGB_4444_SkColorType: returns true if all pixel Alpha values are 15.
1220For kRGBA_F16_SkColorType: returns true if all pixel Alpha values are 1.0 or
1221greater.
1222
1223Returns false for kUnknown_SkColorType.
1224
1225#Param bm Bitmap to check ##
1226
1227#Return true if all pixels have opaque values or Color_Type is opaque ##
1228
1229#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001230 SkBitmap bitmap;
1231 bitmap.setInfo(SkImageInfo::Make(2, 2, kN32_SkColorType, kPremul_SkAlphaType));
1232 for (int index = 0; index < 2; ++index) {
1233 bitmap.allocPixels();
1234 bitmap.eraseColor(0x00000000);
1235 SkDebugf("computeIsOpaque: %s\n", SkBitmap::ComputeIsOpaque(bitmap) ? "true" : "false");
1236 bitmap.eraseColor(0xFFFFFFFF);
1237 SkDebugf("computeIsOpaque: %s\n", SkBitmap::ComputeIsOpaque(bitmap) ? "true" : "false");
1238 bitmap.setInfo(bitmap.info().makeAlphaType(kOpaque_SkAlphaType));
Cary Clarkbc5697d2017-10-04 14:31:33 -04001239 }
1240#StdOut
1241computeIsOpaque: false
1242computeIsOpaque: true
1243computeIsOpaque: false
1244computeIsOpaque: true
1245##
1246##
1247
1248#SeeAlso isOpaque Color_Type Alpha
1249
1250##
1251
1252# ------------------------------------------------------------------------------
1253
1254#Method void getBounds(SkRect* bounds) const
Cary Clark78de7512018-02-07 07:27:09 -05001255#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001256#Line # returns width() and height() as Rectangle ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001257Returns Rect { 0, 0, width(), height() }.
1258
1259#Param bounds container for floating point rectangle ##
1260
1261#Example
1262#Height 160
1263#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001264 SkRect bounds;
1265 source.getBounds(&bounds);
1266 bounds.offset(100, 100);
1267 SkPaint paint;
1268 paint.setColor(SK_ColorGRAY);
1269 canvas->scale(.25f, .25f);
1270 canvas->drawRect(bounds, paint);
1271 canvas->drawBitmap(source, 40, 40);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001272##
1273
1274#SeeAlso bounds()
1275
1276##
1277
1278# ------------------------------------------------------------------------------
1279
1280#Method void getBounds(SkIRect* bounds) const
1281
1282Returns IRect { 0, 0, width(), height() }.
1283
1284#Param bounds container for integral rectangle ##
1285
1286#Example
1287#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001288 SkIRect bounds;
1289 source.getBounds(&bounds);
1290 bounds.inset(100, 100);
1291 SkBitmap bitmap;
1292 source.extractSubset(&bitmap, bounds);
1293 canvas->scale(.5f, .5f);
1294 canvas->drawBitmap(bitmap, 10, 10);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001295##
1296
1297#SeeAlso bounds()
1298
1299##
1300
1301# ------------------------------------------------------------------------------
1302
1303#Method SkIRect bounds() const
Cary Clark78de7512018-02-07 07:27:09 -05001304#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001305#Line # returns width() and height() as Rectangle ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001306Returns IRect { 0, 0, width(), height() }.
1307
1308#Return integral rectangle from origin to width() and height() ##
1309
1310#Example
Cary Clark681287e2018-03-16 11:34:15 -04001311#Height 64
Cary Clarkbc5697d2017-10-04 14:31:33 -04001312#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -05001313 canvas->scale(.5f, .5f);
Ben Wagner29380bd2017-10-09 14:43:00 -04001314 SkIRect bounds = source.bounds();
1315 for (int x : { 0, bounds.width() } ) {
1316 for (int y : { 0, bounds.height() } ) {
1317 canvas->drawBitmap(source, x, y);
1318 }
1319 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001320##
1321
1322#SeeAlso getBounds
1323
1324##
1325
1326# ------------------------------------------------------------------------------
1327
1328#Method SkISize dimensions() const
Cary Clark78de7512018-02-07 07:27:09 -05001329#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001330#Line # returns width() and height() ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001331Returns ISize { width(), height() }.
1332
1333#Return integral size of width() and height() ##
1334
1335#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001336 SkBitmap bitmap;
1337 bitmap.setInfo(SkImageInfo::MakeN32(33, 55, kOpaque_SkAlphaType));
1338 SkISize dimensions = bitmap.dimensions();
1339 SkRect bounds;
1340 bitmap.getBounds(&bounds);
1341 SkRect dimensionsAsBounds = SkRect::Make(dimensions);
1342 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -04001343##
1344
1345#SeeAlso height() width()
1346
1347##
1348
1349# ------------------------------------------------------------------------------
1350
1351#Method SkIRect getSubset() const
Cary Clark78de7512018-02-07 07:27:09 -05001352#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001353#Line # returns bounds offset by origin ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001354Returns the bounds of this bitmap, offset by its Pixel_Ref origin.
1355
1356#Return bounds within Pixel_Ref bounds ##
1357
1358#Example
1359#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001360 SkIRect bounds;
1361 source.getBounds(&bounds);
1362 bounds.inset(100, 100);
1363 SkBitmap subset;
1364 source.extractSubset(&subset, bounds);
1365 SkIRect r = source.getSubset();
1366 SkDebugf("source: %d, %d, %d, %d\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
1367 r = subset.getSubset();
1368 SkDebugf("subset: %d, %d, %d, %d\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
1369#StdOut
1370source: 0, 0, 512, 512
1371subset: 100, 100, 412, 412
1372##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001373##
1374
1375#SeeAlso extractSubset getBounds
1376
1377##
1378
1379# ------------------------------------------------------------------------------
1380
1381#Method bool setInfo(const SkImageInfo& imageInfo, size_t rowBytes = 0)
Cary Clark78de7512018-02-07 07:27:09 -05001382#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05001383#Line # sets height, width, Color_Type, and so on, releasing pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001384Sets width, height, Alpha_Type, Color_Type, Color_Space, and optional
1385rowBytes. Frees pixels, and returns true if successful.
1386
1387imageInfo.alphaType may be altered to a value permitted by imageInfo.colorSpace.
1388If imageInfo.colorType is kUnknown_SkColorType, imageInfo.alphaType is
1389set to kUnknown_SkAlphaType.
1390If imageInfo.colorType is kAlpha_8_SkColorType and imageInfo.alphaType is
1391kUnpremul_SkAlphaType, imageInfo.alphaType is replaced by kPremul_SkAlphaType.
1392If imageInfo.colorType is kRGB_565_SkColorType or kGray_8_SkColorType,
1393imageInfo.alphaType is set to kOpaque_SkAlphaType.
1394If imageInfo.colorType is kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
1395kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: imageInfo.alphaType remains
1396unchanged.
1397
1398rowBytes must equal or exceed imageInfo.minRowBytes. If imageInfo.colorSpace is
1399kUnknown_SkColorType, rowBytes is ignored and treated as zero; for all other
1400Color_Space values, rowBytes of zero is treated as imageInfo.minRowBytes.
1401
1402Calls reset() and returns false if:
1403#List
1404# rowBytes exceeds 31 bits ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001405# imageInfo.width() is negative ##
1406# imageInfo.height() is negative ##
1407# rowBytes is positive and less than imageInfo.width() times imageInfo.bytesPerPixel ##
1408##
1409
1410#Param imageInfo contains width, height, Alpha_Type, Color_Type, Color_Space ##
1411#Param rowBytes imageInfo.minRowBytes or larger; or zero ##
1412
1413#Return true if Image_Info set successfully ##
1414
1415#Example
1416#Height 96
1417###^
Ben Wagner29380bd2017-10-09 14:43:00 -04001418SkBitmap bitmap;
1419bitmap.setInfo(SkImageInfo::MakeN32(44, 16, kOpaque_SkAlphaType));
1420bitmap.allocPixels();
1421bitmap.eraseColor(SK_ColorGREEN);
1422SkCanvas offscreen(bitmap);
1423SkPaint paint;
1424offscreen.drawString("!@#$%", 0, 12, paint);
1425canvas->scale(6, 6);
1426canvas->drawBitmap(bitmap, 0, 0);
1427^^^#
Cary Clarkbc5697d2017-10-04 14:31:33 -04001428##
1429
1430#SeeAlso Alpha_Type Color_Type Color_Space height rowBytes width
1431
1432##
1433
1434# ------------------------------------------------------------------------------
1435
1436#Enum AllocFlags
Cary Clark4855f782018-02-06 09:41:53 -05001437#Line # zero pixel memory ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001438#Code
1439 enum AllocFlags {
1440 kZeroPixels_AllocFlag = 1 << 0,
1441 };
1442##
1443
1444AllocFlags provides the option to zero pixel memory when allocated.
1445
1446#Const kZeroPixels_AllocFlag 1
1447 Instructs tryAllocPixelsFlags and allocPixelsFlags to zero pixel memory.
1448##
1449
1450#NoExample
1451##
1452
1453#SeeAlso tryAllocPixelsFlags allocPixelsFlags erase() eraseColor
1454
1455##
1456
1457# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001458#Subtopic Allocate
1459#Populate
1460#Line # allocates storage for pixels ##
1461##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001462
1463#Method bool SK_WARN_UNUSED_RESULT tryAllocPixelsFlags(const SkImageInfo& info, uint32_t flags)
Cary Clark78de7512018-02-07 07:27:09 -05001464#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001465#Line # allocates pixels from Image_Info with options if possible ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001466Sets Image_Info to info following the rules in setInfo and allocates pixel
1467memory. If flags is kZeroPixels_AllocFlag, memory is zeroed.
1468
1469Returns false and calls reset() if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001470not be allocated, or memory could not optionally be zeroed.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001471
1472On most platforms, allocating pixel memory may succeed even though there is
1473not sufficient memory to hold pixels; allocation does not take place
1474until the pixels are written to. The actual behavior depends on the platform
1475implementation of malloc(), if flags is zero, and calloc(), if flags is
1476kZeroPixels_AllocFlag.
1477
1478Passing kZeroPixels_AllocFlag is usually faster than separately calling
1479eraseColor(SK_ColorTRANSPARENT).
1480
1481#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1482#Param flags kZeroPixels_AllocFlag, or zero ##
1483
1484#Return true if pixels allocation is successful ##
1485
1486#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001487 SkBitmap bitmap;
Cary Clarka560c472017-11-27 10:44:06 -05001488 if (!bitmap.tryAllocPixelsFlags(SkImageInfo::MakeN32(10000, 10000, kOpaque_SkAlphaType),
1489 SkBitmap::kZeroPixels_AllocFlag)) {
1490 SkDebugf("bitmap allocation failed!\n");
1491 } else {
1492 SkDebugf("bitmap allocation succeeded!\n");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001493 }
1494#StdOut
Cary Clarka560c472017-11-27 10:44:06 -05001495bitmap allocation succeeded!
Cary Clarkbc5697d2017-10-04 14:31:33 -04001496##
1497##
1498
1499#SeeAlso allocPixelsFlags tryAllocPixels SkMallocPixelRef::MakeZeroed
1500
1501##
1502
1503# ------------------------------------------------------------------------------
1504
1505#Method void allocPixelsFlags(const SkImageInfo& info, uint32_t flags)
Cary Clark78de7512018-02-07 07:27:09 -05001506#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001507#Line # allocates pixels from Image_Info with options, or aborts ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001508Sets Image_Info to info following the rules in setInfo and allocates pixel
1509memory. If flags is kZeroPixels_AllocFlag, memory is zeroed.
1510
1511Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001512not be allocated, or memory could not optionally
Cary Clarkbc5697d2017-10-04 14:31:33 -04001513be zeroed. Abort steps may be provided by the user at compile time by defining
1514SK_ABORT.
1515
1516On most platforms, allocating pixel memory may succeed even though there is
1517not sufficient memory to hold pixels; allocation does not take place
1518until the pixels are written to. The actual behavior depends on the platform
1519implementation of malloc(), if flags is zero, and calloc(), if flags is
1520kZeroPixels_AllocFlag.
1521
1522Passing kZeroPixels_AllocFlag is usually faster than separately calling
1523eraseColor(SK_ColorTRANSPARENT).
1524
1525#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1526#Param flags kZeroPixels_AllocFlag, or zero ##
1527
1528#Example
1529#Height 128
1530#Description
1531Text is drawn on a transparent background; drawing the bitmap a second time
1532lets the first draw show through.
1533##
1534###^
Ben Wagner29380bd2017-10-09 14:43:00 -04001535SkBitmap bitmap;
1536bitmap.allocPixelsFlags(SkImageInfo::MakeN32(44, 16, kPremul_SkAlphaType),
1537 SkBitmap::kZeroPixels_AllocFlag);
1538SkCanvas offscreen(bitmap);
1539SkPaint paint;
1540offscreen.drawString("!@#$%", 0, 12, paint);
1541canvas->scale(6, 6);
1542canvas->drawBitmap(bitmap, 0, 0);
1543canvas->drawBitmap(bitmap, 8, 8);
1544^^^#
Cary Clarkbc5697d2017-10-04 14:31:33 -04001545##
1546
1547#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeZeroed
1548
1549##
1550
1551# ------------------------------------------------------------------------------
1552
1553#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info, size_t rowBytes)
Cary Clark78de7512018-02-07 07:27:09 -05001554#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001555#Line # allocates pixels from Image_Info if possible ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001556#ToDo am I ever conflicted about setInfo rules. It needs to be able to be replicated
1557 if, for instance, I generate one-page-per-method HTML-style documentation
1558 I'm not so sure it makes sense to put the indirection in for .h either unless
1559 my mantra is that .h should abbreviate full documentation. And, what to do
1560 for generated markdown? At least there the rules are a click away, although
1561 a pop-down in place would be way better. Hmmm.
1562##
1563
1564Sets Image_Info to info following the rules in setInfo and allocates pixel
1565memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(),
1566or equal zero. Pass in zero for rowBytes to compute the minimum valid value.
1567
1568Returns false and calls reset() if Image_Info could not be set, or memory could
1569not be allocated.
1570
1571On most platforms, allocating pixel memory may succeed even though there is
1572not sufficient memory to hold pixels; allocation does not take place
1573until the pixels are written to. The actual behavior depends on the platform
1574implementation of malloc().
1575
1576#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1577#Param rowBytes size of pixel row or larger; may be zero ##
1578
1579#Return true if pixel storage is allocated ##
1580
1581#Example
1582#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001583SkBitmap bitmap;
1584SkImageInfo info = SkImageInfo::Make(64, 256, kGray_8_SkColorType, kOpaque_SkAlphaType);
1585if (bitmap.tryAllocPixels(info, 0)) {
1586 SkCanvas offscreen(bitmap);
1587 offscreen.scale(.5f, .5f);
1588 for (int x : { 0, 64, 128, 192 } ) {
1589 offscreen.drawBitmap(source, -x, 0);
1590 canvas->drawBitmap(bitmap, x, 0);
1591 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001592}
1593##
1594
1595#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeAllocate
1596
1597##
1598
1599# ------------------------------------------------------------------------------
1600
1601#Method void allocPixels(const SkImageInfo& info, size_t rowBytes)
Cary Clark78de7512018-02-07 07:27:09 -05001602#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001603#Line # allocates pixels from Image_Info, or aborts ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001604Sets Image_Info to info following the rules in setInfo and allocates pixel
1605memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(),
1606or equal zero. Pass in zero for rowBytes to compute the minimum valid value.
1607
1608Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001609not be allocated. Abort steps may be provided by
Cary Clarkbc5697d2017-10-04 14:31:33 -04001610the user at compile time by defining SK_ABORT.
1611
1612On most platforms, allocating pixel memory may succeed even though there is
1613not sufficient memory to hold pixels; allocation does not take place
1614until the pixels are written to. The actual behavior depends on the platform
1615implementation of malloc().
1616
1617#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1618#Param rowBytes size of pixel row or larger; may be zero ##
1619
1620#Example
1621#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001622SkBitmap bitmap;
1623SkImageInfo info = SkImageInfo::Make(256, 64, kGray_8_SkColorType, kOpaque_SkAlphaType);
1624bitmap.allocPixels(info, info.width() * info.bytesPerPixel() + 64);
1625SkCanvas offscreen(bitmap);
1626offscreen.scale(.5f, .5f);
1627for (int y : { 0, 64, 128, 192 } ) {
1628 offscreen.drawBitmap(source, 0, -y);
1629 canvas->drawBitmap(bitmap, 0, y);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001630}
1631##
1632
1633#SeeAlso tryAllocPixels allocPixelsFlags SkMallocPixelRef::MakeAllocate
1634
1635##
1636
1637# ------------------------------------------------------------------------------
1638
1639#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info)
1640
1641Sets Image_Info to info following the rules in setInfo and allocates pixel
1642memory.
1643
1644Returns false and calls reset() if Image_Info could not be set, or memory could
1645not be allocated.
1646
1647On most platforms, allocating pixel memory may succeed even though there is
1648not sufficient memory to hold pixels; allocation does not take place
1649until the pixels are written to. The actual behavior depends on the platform
1650implementation of malloc().
1651
1652#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1653
1654#Return true if pixel storage is allocated ##
1655
1656#Example
1657#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001658SkBitmap bitmap;
1659if (bitmap.tryAllocPixels(SkImageInfo::Make(64, 64, kGray_8_SkColorType, kOpaque_SkAlphaType))) {
1660 SkCanvas offscreen(bitmap);
1661 offscreen.scale(.25f, .5f);
1662 for (int y : { 0, 64, 128, 192 } ) {
1663 offscreen.drawBitmap(source, -y, -y);
1664 canvas->drawBitmap(bitmap, y, y);
1665 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001666}
1667##
1668
1669#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeAllocate
1670
1671##
1672
1673# ------------------------------------------------------------------------------
1674
1675#Method void allocPixels(const SkImageInfo& info)
1676
1677Sets Image_Info to info following the rules in setInfo and allocates pixel
1678memory.
1679
1680Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001681not be allocated. Abort steps may be provided by
Cary Clarkbc5697d2017-10-04 14:31:33 -04001682the user at compile time by defining SK_ABORT.
1683
1684On most platforms, allocating pixel memory may succeed even though there is
1685not sufficient memory to hold pixels; allocation does not take place
1686until the pixels are written to. The actual behavior depends on the platform
1687implementation of malloc().
1688
1689#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1690
1691#Example
1692#Image 4
Ben Wagner29380bd2017-10-09 14:43:00 -04001693SkBitmap bitmap;
1694bitmap.allocPixels(SkImageInfo::Make(64, 64, kGray_8_SkColorType, kOpaque_SkAlphaType));
1695SkCanvas offscreen(bitmap);
1696offscreen.scale(.5f, .5f);
1697for (int y : { 0, 64, 128, 192 } ) {
1698 offscreen.drawBitmap(source, -y, -y);
1699 canvas->drawBitmap(bitmap, y, y);
1700}
Cary Clarkbc5697d2017-10-04 14:31:33 -04001701##
1702
1703#SeeAlso tryAllocPixels allocPixelsFlags SkMallocPixelRef::MakeAllocate
1704
1705##
1706
1707# ------------------------------------------------------------------------------
1708
1709#Method bool SK_WARN_UNUSED_RESULT tryAllocN32Pixels(int width, int height, bool isOpaque = false)
Cary Clark78de7512018-02-07 07:27:09 -05001710#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001711#Line # allocates compatible Color_ARGB pixels if possible ##
Cary Clarka560c472017-11-27 10:44:06 -05001712Sets Image_Info to width, height, and Native_Color_Type; and allocates
Cary Clarkbc5697d2017-10-04 14:31:33 -04001713pixel memory. If isOpaque is true, sets Image_Info to kOpaque_SkAlphaType;
1714otherwise, sets to kPremul_SkAlphaType.
1715
1716Calls reset() and returns false if width exceeds 29 bits or is negative,
1717or height is negative.
1718
1719Returns false if allocation fails.
1720
Cary Clarka560c472017-11-27 10:44:06 -05001721Use to create Bitmap that matches SkPMColor, the native pixel arrangement on
1722the platform. Bitmap drawn to output device skips converting its pixel format.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001723
1724#Param width pixel column count; must be zero or greater ##
1725#Param height pixel row count; must be zero or greater ##
1726#Param isOpaque true if pixels do not have transparency ##
1727
1728#Return true if pixel storage is allocated ##
1729
1730#Example
1731#Height 160
Ben Wagner29380bd2017-10-09 14:43:00 -04001732 SkBitmap bitmap;
1733 if (bitmap.tryAllocN32Pixels(80, 80)) {
1734 bitmap.eraseColor(SK_ColorTRANSPARENT);
1735 bitmap.erase(0x7f3f7fff, SkIRect::MakeWH(50, 30));
1736 bitmap.erase(0x3f7fff3f, SkIRect::MakeXYWH(20, 10, 50, 30));
1737 bitmap.erase(0x5fff3f7f, SkIRect::MakeXYWH(40, 20, 50, 30));
1738 canvas->drawBitmap(bitmap, 0, 0);
1739 for (int x : { 0, 30, 60, 90 } ) {
1740 canvas->drawBitmap(bitmap, x, 70);
1741 }
1742 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001743##
1744
1745#SeeAlso tryAllocPixels allocN32Pixels SkMallocPixelRef::MakeAllocate
1746
1747##
1748
1749# ------------------------------------------------------------------------------
1750
1751#Method void allocN32Pixels(int width, int height, bool isOpaque = false)
Cary Clark78de7512018-02-07 07:27:09 -05001752#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001753#Line # allocates compatible Color_ARGB pixels, or aborts ##
Cary Clarka560c472017-11-27 10:44:06 -05001754Sets Image_Info to width, height, and the Native_Color_Type; and allocates
Cary Clarkbc5697d2017-10-04 14:31:33 -04001755pixel memory. If isOpaque is true, sets Image_Info to kPremul_SkAlphaType;
1756otherwise, sets to kOpaque_SkAlphaType.
1757
1758Aborts if width exceeds 29 bits or is negative, or height is negative, or
1759allocation fails. Abort steps may be provided by the user at compile time by
1760defining SK_ABORT.
1761
Cary Clarka560c472017-11-27 10:44:06 -05001762Use to create Bitmap that matches SkPMColor, the native pixel arrangement on
1763the platform. Bitmap drawn to output device skips converting its pixel format.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001764
1765#Param width pixel column count; must be zero or greater ##
1766#Param height pixel row count; must be zero or greater ##
1767#Param isOpaque true if pixels do not have transparency ##
1768
1769#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001770 SkRandom random;
1771 SkBitmap bitmap;
1772 bitmap.allocN32Pixels(64, 64);
1773 bitmap.eraseColor(SK_ColorTRANSPARENT);
1774 for (int y = 0; y < 256; y += 64) {
1775 for (int x = 0; x < 256; x += 64) {
1776 SkColor color = random.nextU();
1777 uint32_t w = random.nextRangeU(4, 32);
1778 uint32_t cx = random.nextRangeU(0, 64 - w);
1779 uint32_t h = random.nextRangeU(4, 32);
1780 uint32_t cy = random.nextRangeU(0, 64 - h);
1781 bitmap.erase(color, SkIRect::MakeXYWH(cx, cy, w, h));
1782 canvas->drawBitmap(bitmap, x, y);
1783 }
1784 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001785##
1786
1787#SeeAlso allocPixels tryAllocN32Pixels SkMallocPixelRef::MakeAllocate
1788
1789##
1790
1791# ------------------------------------------------------------------------------
1792
1793#Method bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
1794 void (*releaseProc)(void* addr, void* context), void* context)
Cary Clark78de7512018-02-07 07:27:09 -05001795#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001796#Line # creates Pixel_Ref, with optional release function ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001797
1798Sets Image_Info to info following the rules in setInfo, and creates Pixel_Ref
1799containing pixels and rowBytes. releaseProc, if not nullptr, is called
1800immediately on failure or when pixels are no longer referenced. context may be
1801nullptr.
1802
1803If Image_Info could not be set, or rowBytes is less than info.minRowBytes:
1804calls releaseProc if present, calls reset(), and returns false.
1805
1806Otherwise, if pixels equals nullptr: sets Image_Info, calls releaseProc if
1807present, returns true.
1808
1809If Image_Info is set, pixels is not nullptr, and releaseProc is not nullptr:
1810when pixels are no longer referenced, calls releaseProc with pixels and context
1811as parameters.
1812
1813#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1814#Param pixels address or pixel storage; may be nullptr ##
1815#Param rowBytes size of pixel row or larger ##
1816#Param releaseProc function called when pixels can be deleted; may be nullptr ##
1817#Param context caller state passed to releaseProc; may be nullptr ##
1818
1819#Return true if Image_Info is set to info ##
1820
1821#Example
1822#Description
1823releaseProc is called immediately because rowBytes is too small for Pixel_Ref.
1824##
1825#Function
Ben Wagner29380bd2017-10-09 14:43:00 -04001826static void releaseProc(void* addr, void* ) {
1827 SkDebugf("releaseProc called\n");
1828 delete[] (uint32_t*) addr;
1829}
1830
1831##
1832
1833void draw(SkCanvas* canvas) {
1834 SkBitmap bitmap;
1835 void* pixels = new uint32_t[8 * 8];
1836 SkImageInfo info = SkImageInfo::MakeN32(8, 8, kOpaque_SkAlphaType);
1837 SkDebugf("before installPixels\n");
1838 bool installed = bitmap.installPixels(info, pixels, 16, releaseProc, nullptr);
1839 SkDebugf("install " "%s" "successful\n", installed ? "" : "not ");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001840}
1841#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001842before installPixels
1843releaseProc called
Cary Clarkbc5697d2017-10-04 14:31:33 -04001844install not successful
1845##
1846##
1847
1848#SeeAlso allocPixels
1849
1850##
1851
1852# ------------------------------------------------------------------------------
1853
1854#Method bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes)
1855
1856Sets Image_Info to info following the rules in setInfo, and creates Pixel_Ref
1857containing pixels and rowBytes.
1858
1859If Image_Info could not be set, or rowBytes is less than info.minRowBytes:
1860calls reset(), and returns false.
1861
1862Otherwise, if pixels equals nullptr: sets Image_Info, returns true.
1863
1864Caller must ensure that pixels are valid for the lifetime of Bitmap and Pixel_Ref.
1865
1866#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1867#Param pixels address or pixel storage; may be nullptr ##
1868#Param rowBytes size of pixel row or larger ##
1869
1870#Return true if Image_Info is set to info ##
1871
1872#Example
Cary Clark4855f782018-02-06 09:41:53 -05001873#Bug 7079
Cary Clarkbc5697d2017-10-04 14:31:33 -04001874#Description
Cary Clarkbc5697d2017-10-04 14:31:33 -04001875GPU does not support kUnpremul_SkAlphaType, does not assert that it does not.
1876##
Ben Wagner29380bd2017-10-09 14:43:00 -04001877void draw(SkCanvas* canvas) {
1878 SkRandom random;
1879 SkBitmap bitmap;
1880 const int width = 8;
1881 const int height = 8;
1882 uint32_t pixels[width * height];
1883 for (unsigned x = 0; x < width * height; ++x) {
1884 pixels[x] = random.nextU();
1885 }
1886 SkImageInfo info = SkImageInfo::MakeN32(width, height, kUnpremul_SkAlphaType);
1887 if (bitmap.installPixels(info, pixels, info.minRowBytes())) {
1888 canvas->scale(32, 32);
1889 canvas->drawBitmap(bitmap, 0, 0);
1890 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001891}
1892##
1893
1894#SeeAlso allocPixels
1895
1896##
1897
1898# ------------------------------------------------------------------------------
1899
1900#Method bool installPixels(const SkPixmap& pixmap)
1901
1902Sets Image_Info to pixmap.info() following the rules in setInfo, and creates
1903Pixel_Ref containing pixmap.addr() and pixmap.rowBytes.
1904
1905If Image_Info could not be set, or pixmap.rowBytes is less than
1906SkImageInfo::minRowBytes: calls reset(), and returns false.
1907
1908Otherwise, if pixmap.addr() equals nullptr: sets Image_Info, returns true.
1909
1910Caller must ensure that pixmap is valid for the lifetime of Bitmap and Pixel_Ref.
1911
1912#Param pixmap Image_Info, pixel address, and rowBytes ##
1913
1914#Return true if Image_Info was set to pixmap.info() ##
1915
1916#Example
1917#Description
1918Draw a five by five bitmap, and draw it again with a center white pixel.
1919##
1920#Height 64
1921 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
1922 { 0xAC, 0xA8, 0x89, 0x47, 0x87 },
1923 { 0x4B, 0x25, 0x25, 0x25, 0x46 },
1924 { 0x90, 0x81, 0x25, 0x41, 0x33 },
1925 { 0x75, 0x55, 0x44, 0x20, 0x00 }};
1926 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
1927 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
1928 SkBitmap bitmap;
1929 bitmap.installPixels(pixmap);
1930 canvas->scale(10, 10);
1931 canvas->drawBitmap(bitmap, 0, 0);
1932 *pixmap.writable_addr8(2, 2) = 0xFF;
1933 bitmap.installPixels(pixmap);
1934 canvas->drawBitmap(bitmap, 10, 0);
1935##
1936
1937#SeeAlso allocPixels
1938
1939##
1940
1941# ------------------------------------------------------------------------------
1942
1943#Method bool installMaskPixels(const SkMask& mask)
Cary Clark4855f782018-02-06 09:41:53 -05001944#Deprecated soon
Cary Clarkbc5697d2017-10-04 14:31:33 -04001945##
1946
1947# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001948#Subtopic Pixels
1949#Populate
1950#Line # read and write pixel values ##
1951##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001952
1953#Method void setPixels(void* pixels)
Cary Clark78de7512018-02-07 07:27:09 -05001954#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05001955#Line # sets Pixel_Ref without an offset ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001956Replaces Pixel_Ref with pixels, preserving Image_Info and rowBytes.
1957Sets Pixel_Ref origin to (0, 0).
1958
1959If pixels is nullptr, or if info().colorType equals kUnknown_SkColorType;
1960release reference to Pixel_Ref, and set Pixel_Ref to nullptr.
1961
1962Caller is responsible for handling ownership pixel memory for the lifetime
1963of Bitmap and Pixel_Ref.
1964
1965#Param pixels address of pixel storage, managed by caller ##
1966
1967#Example
1968#Height 50
Ben Wagner29380bd2017-10-09 14:43:00 -04001969 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
1970 uint8_t set2[5] = { 0xAC, 0xA8, 0x89, 0x47, 0x87 };
1971 SkBitmap bitmap;
1972 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
1973 canvas->scale(10, 50);
1974 canvas->drawBitmap(bitmap, 0, 0);
1975 bitmap.setPixels(set2);
1976 canvas->drawBitmap(bitmap, 10, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001977##
1978
1979#SeeAlso installPixels allocPixels
1980
1981##
1982
1983# ------------------------------------------------------------------------------
1984
1985#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels()
Cary Clark78de7512018-02-07 07:27:09 -05001986#In Allocate
Cary Clarkbc5697d2017-10-04 14:31:33 -04001987Allocates pixel memory with HeapAllocator, and replaces existing Pixel_Ref.
1988The allocation size is determined by Image_Info width, height, and Color_Type.
1989
Cary Clarka560c472017-11-27 10:44:06 -05001990Returns false if info().colorType is kUnknown_SkColorType, or allocation fails.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001991
1992#Return true if the allocation succeeds
1993##
1994
1995#Example
1996#Height 50
1997#Description
1998Bitmap hosts and draws gray values in set1. tryAllocPixels replaces Pixel_Ref
1999and erases it to black, but does not alter set1. setPixels replaces black
2000Pixel_Ref with set1.
2001##
Ben Wagner29380bd2017-10-09 14:43:00 -04002002 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
2003 SkBitmap bitmap;
2004 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
2005 canvas->scale(10, 50);
2006 canvas->drawBitmap(bitmap, 0, 0);
2007 if (bitmap.tryAllocPixels()) {
2008 bitmap.eraseColor(SK_ColorBLACK);
2009 canvas->drawBitmap(bitmap, 8, 0);
2010 bitmap.setPixels(set1);
2011 canvas->drawBitmap(bitmap, 16, 0);
2012 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002013##
2014
2015#SeeAlso allocPixels installPixels setPixels
2016
2017##
2018
2019# ------------------------------------------------------------------------------
2020
2021#Method void allocPixels()
Cary Clark78de7512018-02-07 07:27:09 -05002022#In Allocate
Cary Clarkbc5697d2017-10-04 14:31:33 -04002023Allocates pixel memory with HeapAllocator, and replaces existing Pixel_Ref.
2024The allocation size is determined by Image_Info width, height, and Color_Type.
2025
Cary Clarka560c472017-11-27 10:44:06 -05002026Aborts if info().colorType is kUnknown_SkColorType, or allocation fails.
2027Abort steps may be provided by the user at compile
Cary Clarkbc5697d2017-10-04 14:31:33 -04002028time by defining SK_ABORT.
2029
2030#Example
2031#Height 50
2032#Description
2033Bitmap hosts and draws gray values in set1. allocPixels replaces Pixel_Ref
2034and erases it to black, but does not alter set1. setPixels replaces black
2035Pixel_Ref with set2.
2036##
Ben Wagner29380bd2017-10-09 14:43:00 -04002037 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
2038 uint8_t set2[5] = { 0xAC, 0xA8, 0x89, 0x47, 0x87 };
2039 SkBitmap bitmap;
2040 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
2041 canvas->scale(10, 50);
2042 canvas->drawBitmap(bitmap, 0, 0);
2043 bitmap.allocPixels();
2044 bitmap.eraseColor(SK_ColorBLACK);
2045 canvas->drawBitmap(bitmap, 8, 0);
2046 bitmap.setPixels(set2);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002047 canvas->drawBitmap(bitmap, 16, 0);
2048##
2049
2050#SeeAlso tryAllocPixels installPixels setPixels
2051
2052##
2053
2054# ------------------------------------------------------------------------------
2055
2056#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(Allocator* allocator)
2057
2058Allocates pixel memory with allocator, and replaces existing Pixel_Ref.
2059The allocation size is determined by Image_Info width, height, and Color_Type.
2060If allocator is nullptr, use HeapAllocator instead.
2061
Cary Clark78de7512018-02-07 07:27:09 -05002062Returns false if Allocator::allocPixelRef return false.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002063
2064#Param allocator instance of SkBitmap::Allocator instantiation ##
2065
2066#Return true if custom allocator reports success
2067##
2068
2069#Example
2070#Height 100
2071#Description
2072HeapAllocator limits the maximum size of Bitmap to two gigabytes. Using
2073a custom allocator, this limitation may be relaxed. This example can be
2074modified to allocate an eight gigabyte Bitmap on a 64 bit platform with
2075sufficient memory.
2076##
2077#Function
2078class LargePixelRef : public SkPixelRef {
2079public:
2080 LargePixelRef(const SkImageInfo& info, char* storage, size_t rowBytes)
2081 : SkPixelRef(info.width(), info.height(), storage, rowBytes) {
2082 }
2083
2084 ~LargePixelRef() override {
2085 delete[] (char* ) this->pixels();
2086 }
2087};
2088
2089class LargeAllocator : public SkBitmap::Allocator {
2090public:
2091 bool allocPixelRef(SkBitmap* bitmap) override {
2092 const SkImageInfo& info = bitmap->info();
2093 uint64_t rowBytes = info.minRowBytes64();
2094 uint64_t size = info.height() * rowBytes;
2095 char* addr = new char[size];
2096 if (nullptr == addr) {
2097 return false;
2098 }
2099 sk_sp<SkPixelRef> pr = sk_sp<SkPixelRef>(new LargePixelRef(info, addr, rowBytes));
2100 if (!pr) {
2101 return false;
2102 }
2103 bitmap->setPixelRef(std::move(pr), 0, 0);
2104 return true;
2105 }
2106};
2107
2108##
2109
2110void draw(SkCanvas* canvas) {
2111 LargeAllocator largeAllocator;
2112 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002113 int width = 100; // make this 20000
2114 int height = 100; // and this 100000 to allocate 8 gigs on a 64-bit platform
2115 bitmap.setInfo(SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType));
2116 if (bitmap.tryAllocPixels(&largeAllocator)) {
2117 bitmap.eraseColor(0xff55aa33);
2118 canvas->drawBitmap(bitmap, 0, 0);
2119 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002120}
2121
2122##
2123
2124#SeeAlso allocPixels Allocator Pixel_Ref
2125
2126##
2127
2128# ------------------------------------------------------------------------------
2129
2130#Method void allocPixels(Allocator* allocator)
2131
2132Allocates pixel memory with allocator, and replaces existing Pixel_Ref.
2133The allocation size is determined by Image_Info width, height, and Color_Type.
2134If allocator is nullptr, use HeapAllocator instead.
2135
Cary Clark78de7512018-02-07 07:27:09 -05002136Aborts if Allocator::allocPixelRef return false. Abort steps may be provided by
Cary Clarkbc5697d2017-10-04 14:31:33 -04002137the user at compile time by defining SK_ABORT.
2138
2139#Param allocator instance of SkBitmap::Allocator instantiation ##
2140
2141#Example
2142#Height 32
2143#Function
2144class TinyAllocator : public SkBitmap::Allocator {
2145public:
2146 bool allocPixelRef(SkBitmap* bitmap) override {
2147 const SkImageInfo& info = bitmap->info();
2148 if (info.height() * info.minRowBytes() > sizeof(storage)) {
2149 return false;
2150 }
2151 sk_sp<SkPixelRef> pr = sk_sp<SkPixelRef>(
2152 new SkPixelRef(info.width(), info.height(), storage, info.minRowBytes()));
2153 bitmap->setPixelRef(std::move(pr), 0, 0);
2154 return true;
2155 }
2156
2157 char storage[16];
2158};
2159
2160##
2161
2162void draw(SkCanvas* canvas) {
2163 TinyAllocator tinyAllocator;
2164 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002165 bitmap.setInfo(SkImageInfo::MakeN32(2, 2, kOpaque_SkAlphaType));
2166 if (bitmap.tryAllocPixels(&tinyAllocator)) {
2167 bitmap.eraseColor(0xff55aa33);
2168 bitmap.erase(0xffaa3355, SkIRect::MakeXYWH(1, 1, 1, 1));
2169 canvas->scale(16, 16);
2170 canvas->drawBitmap(bitmap, 0, 0);
2171 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002172}
2173##
2174
2175#SeeAlso allocPixels Allocator Pixel_Ref
2176
2177##
2178
2179# ------------------------------------------------------------------------------
2180
2181#Method SkPixelRef* pixelRef() const
Cary Clark78de7512018-02-07 07:27:09 -05002182#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002183#Line # returns Pixel_Ref, or nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002184Returns Pixel_Ref, which contains: pixel base address; its dimensions; and
2185rowBytes, the interval from one row to the next. Does not change Pixel_Ref
2186reference count. Pixel_Ref may be shared by multiple bitmaps.
2187If Pixel_Ref has not been set, returns nullptr.
2188
2189#Return Pixel_Ref, or nullptr ##
2190
2191#Example
2192#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002193 SkBitmap subset;
2194 source.extractSubset(&subset, SkIRect::MakeXYWH(32, 64, 128, 256));
2195 SkDebugf("src ref %c= sub ref\n", source.pixelRef() == subset.pixelRef() ? '=' : '!');
2196 SkDebugf("src pixels %c= sub pixels\n", source.getPixels() == subset.getPixels() ? '=' : '!');
2197 SkDebugf("src addr %c= sub addr\n", source.getAddr(32, 64) == subset.getAddr(0, 0) ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -04002198##
2199
2200#SeeAlso getPixels getAddr
2201
2202##
2203
2204# ------------------------------------------------------------------------------
2205
2206#Method SkIPoint pixelRefOrigin() const
Cary Clark78de7512018-02-07 07:27:09 -05002207#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002208#Line # returns offset within Pixel_Ref ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002209Returns origin of pixels within Pixel_Ref. Bitmap bounds is always contained
2210by Pixel_Ref bounds, which may be the same size or larger. Multiple Bitmaps
2211can share the same Pixel_Ref, where each Bitmap has different bounds.
2212
2213The returned origin added to Bitmap dimensions equals or is smaller than the
2214Pixel_Ref dimensions.
2215
2216Returns (0, 0) if Pixel_Ref is nullptr.
2217
2218#Return pixel origin within Pixel_Ref ##
2219
2220#Example
2221#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002222 SkBitmap subset;
2223 source.extractSubset(&subset, SkIRect::MakeXYWH(32, 64, 128, 256));
2224 SkIPoint sourceOrigin = source.pixelRefOrigin();
2225 SkIPoint subsetOrigin = subset.pixelRefOrigin();
2226 SkDebugf("source origin: %d, %d\n", sourceOrigin.fX, sourceOrigin.fY);
2227 SkDebugf("subset origin: %d, %d\n", subsetOrigin.fX, subsetOrigin.fY);
2228#StdOut
2229source origin: 0, 0
2230subset origin: 32, 64
2231##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002232##
2233
Cary Clark2ade9972017-11-02 17:49:34 -04002234#SeeAlso SkPixelRef getSubset setPixelRef
Cary Clarkbc5697d2017-10-04 14:31:33 -04002235
2236##
2237
2238# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05002239#Subtopic Set
2240#Line # updates values and attributes ##
2241#Populate
2242##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002243
2244#Method void setPixelRef(sk_sp<SkPixelRef> pixelRef, int dx, int dy)
Cary Clark78de7512018-02-07 07:27:09 -05002245#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05002246#Line # sets Pixel_Ref and offset ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002247Replaces pixelRef and origin in Bitmap. dx and dy specify the offset
2248within the Pixel_Ref pixels for the top-left corner of the bitmap.
2249
2250Asserts in debug builds if dx or dy are out of range. Pins dx and dy
2251to legal range in release builds.
2252
2253The caller is responsible for ensuring that the pixels match the
2254Color_Type and Alpha_Type in Image_Info.
2255
2256#Param pixelRef Pixel_Ref describing pixel address and rowBytes ##
2257#Param dx column offset in Pixel_Ref for bitmap origin ##
2258#Param dy row offset in Pixel_Ref for bitmap origin ##
2259
2260#Example
2261#Height 140
2262#Image 5
2263#Description
2264Treating 32 bit data as 8 bit data is unlikely to produce useful results.
2265##
Ben Wagner29380bd2017-10-09 14:43:00 -04002266 SkBitmap bitmap;
2267 bitmap.setInfo(SkImageInfo::Make(source.width() - 5, source.height() - 5,
2268 kGray_8_SkColorType, kOpaque_SkAlphaType), source.rowBytes());
2269 bitmap.setPixelRef(sk_ref_sp(source.pixelRef()), 5, 5);
2270 canvas->drawBitmap(bitmap, 10, 10);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002271##
2272
2273#SeeAlso setInfo
2274
2275##
2276
2277# ------------------------------------------------------------------------------
2278
2279#Method bool readyToDraw() const
Cary Clark78de7512018-02-07 07:27:09 -05002280#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05002281#Line # returns true if address of pixels is not nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002282Returns true if Bitmap is can be drawn.
2283
2284#Return true if getPixels() is not nullptr ##
2285
2286#Example
2287#Image 5
2288#Height 160
Ben Wagner29380bd2017-10-09 14:43:00 -04002289 if (source.readyToDraw()) {
2290 canvas->drawBitmap(source, 10, 10);
2291 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002292##
2293
2294#SeeAlso getPixels drawsNothing
2295
2296##
2297
2298# ------------------------------------------------------------------------------
2299
2300#Method uint32_t getGenerationID() const
Cary Clark78de7512018-02-07 07:27:09 -05002301#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05002302#Line # returns unique ID ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002303Returns a unique value corresponding to the pixels in Pixel_Ref.
2304Returns a different value after notifyPixelsChanged has been called.
2305Returns zero if Pixel_Ref is nullptr.
2306
2307Determines if pixels have changed since last examined.
2308
2309#Return unique value for pixels in Pixel_Ref ##
2310
2311#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04002312 SkBitmap bitmap;
2313 SkDebugf("empty id %u\n", bitmap.getGenerationID());
2314 bitmap.allocPixels(SkImageInfo::MakeN32(64, 64, kOpaque_SkAlphaType));
2315 SkDebugf("alloc id %u\n", bitmap.getGenerationID());
2316 bitmap.eraseColor(SK_ColorRED);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002317 SkDebugf("erase id %u\n", bitmap.getGenerationID());
2318#StdOut
2319#Volatile
Ben Wagner29380bd2017-10-09 14:43:00 -04002320empty id 0
2321alloc id 4
Cary Clarkbc5697d2017-10-04 14:31:33 -04002322erase id 6
2323##
2324##
2325
2326#SeeAlso notifyPixelsChanged Pixel_Ref
2327
2328##
2329
2330# ------------------------------------------------------------------------------
2331
2332#Method void notifyPixelsChanged() const
Cary Clark78de7512018-02-07 07:27:09 -05002333#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05002334#Line # marks pixels as changed, altering the unique ID ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002335Marks that pixels in Pixel_Ref have changed. Subsequent calls to
2336getGenerationID() return a different value.
2337
2338#Example
2339#Height 20
Ben Wagner29380bd2017-10-09 14:43:00 -04002340 SkBitmap bitmap;
2341 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
2342 bitmap.allocPixels();
2343 bitmap.eraseColor(SK_ColorRED);
2344 canvas->scale(16, 16);
2345 canvas->drawBitmap(bitmap, 0, 0);
2346 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorBLUE);
2347 canvas->drawBitmap(bitmap, 2, 0);
2348 bitmap.notifyPixelsChanged();
2349 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorGREEN);
2350 canvas->drawBitmap(bitmap, 4, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002351##
2352
2353#SeeAlso getGenerationID isVolatile Pixel_Ref
2354
2355##
2356
2357# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05002358#Subtopic Draw
2359#Populate
2360#Line # set pixels to Color ##
2361##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002362
2363#Method void eraseColor(SkColor c) const
Cary Clark78de7512018-02-07 07:27:09 -05002364#In Draw
Cary Clarkab2621d2018-01-30 10:08:57 -05002365#Line # writes Color to pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002366Replaces pixel values with c. All pixels contained by bounds() are affected.
2367If the colorType is kGray_8_SkColorType or k565_SkColorType, then Color_Alpha
2368is ignored; Color_RGB is treated as opaque. If colorType is kAlpha_8_SkColorType,
2369then Color_RGB is ignored.
2370
2371#Param c Unpremultiplied Color ##
2372
2373#Example
2374#Height 20
Ben Wagner29380bd2017-10-09 14:43:00 -04002375 SkBitmap bitmap;
2376 bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kOpaque_SkAlphaType));
2377 bitmap.eraseColor(SK_ColorRED);
2378 canvas->scale(16, 16);
2379 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002380##
2381
Cary Clark154beea2017-10-26 07:58:48 -04002382#SeeAlso eraseARGB erase
Cary Clarkbc5697d2017-10-04 14:31:33 -04002383
2384##
2385
2386# ------------------------------------------------------------------------------
2387
2388#Method void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const
Cary Clark78de7512018-02-07 07:27:09 -05002389#In Draw
Cary Clarkab2621d2018-01-30 10:08:57 -05002390#Line # writes Color to pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002391Replaces pixel values with Unpremultiplied Color built from a, r, g, and b.
2392All pixels contained by bounds() are affected.
2393If the colorType is kGray_8_SkColorType or k565_SkColorType, then a
2394is ignored; r, g, and b are treated as opaque. If colorType is kAlpha_8_SkColorType,
2395then r, g, and b are ignored.
2396
2397#Param a amount of Color_Alpha, from fully transparent (0) to fully opaque (255) ##
2398#Param r amount of Color_RGB_Red, from no red (0) to full red (255) ##
2399#Param g amount of Color_RGB_Green, from no green (0) to full green (255) ##
2400#Param b amount of Color_RGB_Blue, from no blue (0) to full blue (255) ##
2401
2402#Example
2403#Height 80
Ben Wagner29380bd2017-10-09 14:43:00 -04002404 SkBitmap bitmap;
2405 bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType));
2406 bitmap.eraseARGB(0x7f, 0xff, 0x7f, 0x3f);
2407 canvas->scale(50, 50);
2408 canvas->drawBitmap(bitmap, 0, 0);
2409 canvas->drawBitmap(bitmap, .5f, .5f);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002410##
2411
Cary Clark154beea2017-10-26 07:58:48 -04002412#SeeAlso eraseColor erase
Cary Clarkbc5697d2017-10-04 14:31:33 -04002413
2414##
2415
2416# ------------------------------------------------------------------------------
2417
2418#Method void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const
Cary Clark89b14562018-03-19 09:04:10 -04002419#Deprecated
Cary Clarkbc5697d2017-10-04 14:31:33 -04002420##
2421
2422# ------------------------------------------------------------------------------
2423
2424#Method void erase(SkColor c, const SkIRect& area) const
Cary Clark78de7512018-02-07 07:27:09 -05002425#In Draw
Cary Clarkab2621d2018-01-30 10:08:57 -05002426#Line # writes Color to rectangle of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002427Replaces pixel values inside area with c. If area does not intersect bounds(),
2428call has no effect.
2429
2430If the colorType is kGray_8_SkColorType or k565_SkColorType, then Color_Alpha
2431is ignored; Color_RGB is treated as opaque. If colorType is kAlpha_8_SkColorType,
2432then Color_RGB is ignored.
2433
2434#Param c Unpremultiplied Color ##
2435#Param area rectangle to fill ##
2436
2437#Example
2438#Height 70
Ben Wagner29380bd2017-10-09 14:43:00 -04002439 SkBitmap bitmap;
2440 bitmap.allocPixels(SkImageInfo::MakeN32(2, 2, kPremul_SkAlphaType));
2441 bitmap.erase(0x7fff7f3f, SkIRect::MakeWH(1, 1));
2442 bitmap.erase(0x7f7f3fff, SkIRect::MakeXYWH(0, 1, 1, 1));
2443 bitmap.erase(0x7f3fff7f, SkIRect::MakeXYWH(1, 0, 1, 1));
2444 bitmap.erase(0x7f1fbf5f, SkIRect::MakeXYWH(1, 1, 1, 1));
2445 canvas->scale(25, 25);
2446 canvas->drawBitmap(bitmap, 0, 0);
2447 canvas->drawBitmap(bitmap, .5f, .5f);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002448
2449##
2450
2451#SeeAlso eraseColor eraseARGB eraseRGB SkCanvas::drawRect
2452
2453##
2454
2455# ------------------------------------------------------------------------------
2456
2457#Method void eraseArea(const SkIRect& area, SkColor c) const
Cary Clarkbc5697d2017-10-04 14:31:33 -04002458#Deprecated
2459##
2460
Cary Clarkbc5697d2017-10-04 14:31:33 -04002461# ------------------------------------------------------------------------------
2462
2463#Method SkColor getColor(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002464#In Property
2465#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05002466#Line # returns one pixel as Unpremultiplied Color ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002467Returns pixel at (x, y) as Unpremultiplied Color.
2468Returns black with Alpha if Color_Type is kAlpha_8_SkColorType.
2469
2470Input is not validated: out of bounds values of x or y trigger an assert() if
2471built with SK_DEBUG defined; and returns undefined values or may crash if
2472SK_RELEASE is defined. Fails if Color_Type is kUnknown_SkColorType or
2473pixel address is nullptr.
2474
2475Color_Space in Image_Info is ignored. Some Color precision may be lost in the
2476conversion to Unpremultiplied Color; original pixel data may have additional
2477precision.
2478
2479#Param x column index, zero or greater, and less than width() ##
2480#Param y row index, zero or greater, and less than height() ##
2481
2482#Return pixel converted to Unpremultiplied Color ##
2483
2484#Example
2485 const int w = 4;
2486 const int h = 4;
2487 SkColor colors[][w] = {
2488 0x00000000, 0x2a0e002a, 0x55380055, 0x7f7f007f,
2489 0x2a000e2a, 0x551c1c55, 0x7f542a7f, 0xaaaa38aa,
2490 0x55003855, 0x7f2a547f, 0xaa7171aa, 0xd4d48dd4,
2491 0x7f007f7f, 0xaa38aaaa, 0xd48dd4d4, 0xffffffff,
2492 };
2493 SkDebugf("Premultiplied:\n");
2494 for (int y = 0; y < h; ++y) {
2495 SkDebugf("(0, %d) ", y);
2496 for (int x = 0; x < w; ++x) {
2497 SkDebugf("0x%08x%c", colors[y][x], x == w - 1 ? '\n' : ' ');
2498 }
2499 }
2500 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), colors, w * 4);
2501 SkBitmap bitmap;
2502 bitmap.installPixels(pixmap);
2503 SkDebugf("Unpremultiplied:\n");
2504 for (int y = 0; y < h; ++y) {
2505 SkDebugf("(0, %d) ", y);
2506 for (int x = 0; x < w; ++x) {
2507 SkDebugf("0x%08x%c", bitmap.getColor(x, y), x == w - 1 ? '\n' : ' ');
2508 }
2509 }
2510#StdOut
2511Premultiplied:
2512(0, 0) 0x00000000 0x2a0e002a 0x55380055 0x7f7f007f
2513(0, 1) 0x2a000e2a 0x551c1c55 0x7f542a7f 0xaaaa38aa
2514(0, 2) 0x55003855 0x7f2a547f 0xaa7171aa 0xd4d48dd4
2515(0, 3) 0x7f007f7f 0xaa38aaaa 0xd48dd4d4 0xffffffff
2516Unpremultiplied:
2517(0, 0) 0x00000000 0x2a5500ff 0x55a800ff 0x7fff00ff
2518(0, 1) 0x2a0055ff 0x555454ff 0x7fa954ff 0xaaff54ff
2519(0, 2) 0x5500a8ff 0x7f54a9ff 0xaaaaaaff 0xd4ffaaff
2520(0, 3) 0x7f00ffff 0xaa54ffff 0xd4aaffff 0xffffffff
2521##
2522##
2523
2524#SeeAlso getAddr readPixels
2525
2526##
2527
2528# ------------------------------------------------------------------------------
2529
2530#Method void* getAddr(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002531#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002532#Line # returns readable pixel address as void pointer ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002533Returns pixel address at (x, y).
2534
2535Input is not validated: out of bounds values of x or y, or kUnknown_SkColorType,
2536trigger an assert() if built with SK_DEBUG defined. Returns nullptr if
2537Color_Type is kUnknown_SkColorType, or Pixel_Ref is nullptr.
2538
2539Performs a lookup of pixel size; for better performance, call
2540one of: getAddr8, getAddr16, or getAddr32.
2541
2542#Param x column index, zero or greater, and less than width() ##
2543#Param y row index, zero or greater, and less than height() ##
2544
2545#Return generic pointer to pixel ##
2546
2547#Example
2548#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002549 char* row0 = (char* ) source.getAddr(0, 0);
2550 char* row1 = (char* ) source.getAddr(0, 1);
Cary Clark681287e2018-03-16 11:34:15 -04002551 SkDebugf("addr interval %c= rowBytes\n",
2552 (size_t) (row1 - row0) == source.rowBytes() ? '=' : '!');
Ben Wagner29380bd2017-10-09 14:43:00 -04002553#StdOut
2554addr interval == rowBytes
2555##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002556##
2557
2558#SeeAlso getAddr8 getAddr16 getAddr32 readPixels SkPixmap::addr
2559
2560##
2561
2562# ------------------------------------------------------------------------------
2563
2564#Method inline uint32_t* getAddr32(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002565#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002566#Line # returns readable pixel address as 32-bit pointer ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002567Returns address at (x, y).
2568
2569Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2570#List
2571# Pixel_Ref is nullptr ##
2572# bytesPerPixel() is not four ##
2573# x is negative, or not less than width() ##
2574# y is negative, or not less than height() ##
2575##
2576
2577#Param x column index, zero or greater, and less than width() ##
2578#Param y row index, zero or greater, and less than height() ##
2579
2580#Return unsigned 32-bit pointer to pixel at (x, y) ##
2581
2582#Example
2583#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002584 uint32_t* row0 = source.getAddr32(0, 0);
2585 uint32_t* row1 = source.getAddr32(0, 1);
2586 size_t interval = (row1 - row0) * source.bytesPerPixel();
2587 SkDebugf("addr interval %c= rowBytes\n", interval == source.rowBytes() ? '=' : '!');
2588#StdOut
2589addr interval == rowBytes
2590##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002591##
2592
2593#SeeAlso getAddr8 getAddr16 getAddr readPixels SkPixmap::addr32
2594
2595##
2596
2597# ------------------------------------------------------------------------------
2598
2599#Method inline uint16_t* getAddr16(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002600#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002601#Line # returns readable pixel address as 16-bit pointer ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002602Returns address at (x, y).
2603
2604Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2605#List
2606# Pixel_Ref is nullptr ##
2607# bytesPerPixel() is not two ##
2608# x is negative, or not less than width() ##
2609# y is negative, or not less than height() ##
2610##
2611
2612#Param x column index, zero or greater, and less than width() ##
2613#Param y row index, zero or greater, and less than height() ##
2614
2615#Return unsigned 16-bit pointer to pixel at (x, y)##
2616
2617#Example
2618#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002619 SkBitmap bitmap16;
2620 SkImageInfo dstInfo = SkImageInfo::Make(source.width(), source.height(), kARGB_4444_SkColorType,
2621 kPremul_SkAlphaType);
2622 bitmap16.allocPixels(dstInfo);
2623 if (source.readPixels(dstInfo, bitmap16.getPixels(), bitmap16.rowBytes(), 0, 0)) {
2624 uint16_t* row0 = bitmap16.getAddr16(0, 0);
2625 uint16_t* row1 = bitmap16.getAddr16(0, 1);
2626 size_t interval = (row1 - row0) * bitmap16.bytesPerPixel();
2627 SkDebugf("addr interval %c= rowBytes\n", interval == bitmap16.rowBytes() ? '=' : '!');
2628 }
2629#StdOut
2630addr interval == rowBytes
2631##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002632##
2633
2634#SeeAlso getAddr8 getAddr getAddr32 readPixels SkPixmap::addr16
2635
2636##
2637
2638# ------------------------------------------------------------------------------
2639
2640#Method inline uint8_t* getAddr8(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002641#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002642#Line # returns readable pixel address as 8-bit pointer ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002643Returns address at (x, y).
2644
2645Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2646#List
2647# Pixel_Ref is nullptr ##
2648# bytesPerPixel() is not one ##
2649# x is negative, or not less than width() ##
2650# y is negative, or not less than height() ##
2651##
2652
2653#Param x column index, zero or greater, and less than width() ##
2654#Param y row index, zero or greater, and less than height() ##
2655
2656#Return unsigned 8-bit pointer to pixel at (x, y) ##
2657
2658#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04002659 SkBitmap bitmap;
2660 const int width = 8;
2661 const int height = 8;
2662 uint8_t pixels[height][width];
2663 SkImageInfo info = SkImageInfo::Make(width, height, kGray_8_SkColorType, kOpaque_SkAlphaType);
2664 if (bitmap.installPixels(info, pixels, info.minRowBytes())) {
2665 SkDebugf("&pixels[4][2] %c= bitmap.getAddr8(2, 4)\n",
2666 &pixels[4][2] == bitmap.getAddr8(2, 4) ? '=' : '!');
2667 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002668#StdOut
2669&pixels[4][2] == bitmap.getAddr8(2, 4)
2670##
2671##
2672
2673#SeeAlso getAddr getAddr16 getAddr32 readPixels SkPixmap::addr8
2674
2675##
2676
2677# ------------------------------------------------------------------------------
2678
2679#Method bool extractSubset(SkBitmap* dst, const SkIRect& subset) const
Cary Clark78de7512018-02-07 07:27:09 -05002680#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -05002681#Line # creates Bitmap, sharing pixels if possible ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002682Shares Pixel_Ref with dst. Pixels are not copied; Bitmap and dst point
2683to the same pixels; dst bounds() are set to the intersection of subset
2684and the original bounds().
2685
2686subset may be larger than bounds(). Any area outside of bounds() is ignored.
2687
2688Any contents of dst are discarded. isVolatile setting is copied to dst.
2689dst is set to colorType, alphaType, and colorSpace.
2690
2691Return false if:
2692#List
2693# dst is nullptr ##
2694# Pixel_Ref is nullptr ##
2695# subset does not intersect bounds() ##
2696##
2697
2698
2699#Param dst Bitmap set to subset ##
2700#Param subset rectangle of pixels to reference ##
2701
2702#Return true if dst is replaced by subset
2703##
2704
2705#Example
2706#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002707 SkIRect bounds, s;
2708 source.getBounds(&bounds);
2709 SkDebugf("bounds: %d, %d, %d, %d\n", bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
2710 SkBitmap subset;
2711 for (int left: { -100, 0, 100, 1000 } ) {
2712 for (int right: { 0, 100, 1000 } ) {
2713 SkIRect b = SkIRect::MakeLTRB(left, 100, right, 200);
2714 bool success = source.extractSubset(&subset, b);
2715 SkDebugf("subset: %4d, %4d, %4d, %4d ", b.fLeft, b.fTop, b.fRight, b.fBottom);
2716 SkDebugf("success; %s", success ? "true" : "false");
2717 if (success) {
2718 subset.getBounds(&s);
2719 SkDebugf(" subset: %d, %d, %d, %d", s.fLeft, s.fTop, s.fRight, s.fBottom);
2720 }
2721 SkDebugf("\n");
2722 }
2723 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002724#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04002725bounds: 0, 0, 512, 512
2726subset: -100, 100, 0, 200 success; false
2727subset: -100, 100, 100, 200 success; true subset: 0, 0, 100, 100
2728subset: -100, 100, 1000, 200 success; true subset: 0, 0, 512, 100
2729subset: 0, 100, 0, 200 success; false
2730subset: 0, 100, 100, 200 success; true subset: 0, 0, 100, 100
2731subset: 0, 100, 1000, 200 success; true subset: 0, 0, 512, 100
2732subset: 100, 100, 0, 200 success; false
2733subset: 100, 100, 100, 200 success; false
2734subset: 100, 100, 1000, 200 success; true subset: 0, 0, 412, 100
2735subset: 1000, 100, 0, 200 success; false
2736subset: 1000, 100, 100, 200 success; false
Cary Clarkbc5697d2017-10-04 14:31:33 -04002737subset: 1000, 100, 1000, 200 success; false
2738##
2739##
2740
2741#SeeAlso readPixels writePixels SkCanvas::drawBitmap
2742
2743##
2744
2745# ------------------------------------------------------------------------------
2746
2747#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
2748 int srcX, int srcY, SkTransferFunctionBehavior behavior) const
Cary Clark78de7512018-02-07 07:27:09 -05002749#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05002750#Line # copies and converts pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002751
Cary Clarkac47b882018-01-11 10:35:44 -05002752Copies Rect of pixels from Bitmap pixels to dstPixels. Copy starts at (srcX, srcY),
2753and does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002754
2755dstInfo specifies width, height, Color_Type, Alpha_Type, and
2756Color_Space of destination. dstRowBytes specifics the gap from one destination
2757row to the next. Returns true if pixels are copied. Returns false if:
2758#List
2759# dstInfo.addr() equals nullptr ##
2760# dstRowBytes is less than dstInfo.minRowBytes ##
2761# Pixel_Ref is nullptr ##
2762##
2763
Cary Clarkac47b882018-01-11 10:35:44 -05002764Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002765kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002766If Bitmap colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
2767If Bitmap alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
2768match. If Bitmap colorSpace is nullptr, dstInfo.colorSpace must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002769false if pixel conversion is not possible.
2770
2771srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04002772false if width() or height() is zero or negative.
2773Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04002774#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002775abs(srcX) >= Bitmap width()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002776##
2777, or if
2778#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002779abs(srcY) >= Bitmap height()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002780##
2781.
2782
2783If behavior is SkTransferFunctionBehavior::kRespect: converts source
2784pixels to a linear space before converting to dstInfo.
2785If behavior is SkTransferFunctionBehavior::kIgnore: source
2786pixels are treated as if they are linear, regardless of how they are encoded.
2787
2788#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
2789#Param dstPixels destination pixel storage ##
2790#Param dstRowBytes destination row length ##
2791#Param srcX column index whose absolute value is less than width() ##
2792#Param srcY row index whose absolute value is less than height() ##
2793#Param behavior one of: SkTransferFunctionBehavior::kRespect,
2794 SkTransferFunctionBehavior::kIgnore
2795##
2796
2797#Return true if pixels are copied to dstPixels ##
2798
2799#Example
2800#Height 64
2801void draw(SkCanvas* canvas) {
Ben Wagner29380bd2017-10-09 14:43:00 -04002802 const int width = 256;
2803 const int height = 32;
2804 std::vector<int32_t> dstPixels;
2805 dstPixels.resize(height * width * 4);
2806 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
2807 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
2808 SkPoint gradPoints[] = { { 0, 0 }, { width, 0 } };
2809 SkPaint gradPaint;
2810 gradPaint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
2811 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
2812 for (auto behavior : { SkTransferFunctionBehavior::kRespect,
2813 SkTransferFunctionBehavior::kIgnore} ) {
2814 SkBitmap bitmap;
2815 bitmap.allocPixels(info);
2816 SkCanvas srcCanvas(bitmap);
2817 srcCanvas.drawRect(SkRect::MakeWH(width, height), gradPaint);
2818 if (bitmap.readPixels(info, &dstPixels.front(), width * 4, 0, 0, behavior)) {
2819 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
2820 bitmap.installPixels(dstPixmap);
2821 canvas->drawBitmap(bitmap, 0, 0);
2822 }
2823 canvas->translate(0, height);
2824 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002825}
2826##
2827
2828#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2829
2830##
2831
2832# ------------------------------------------------------------------------------
2833
2834#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
2835 int srcX, int srcY) const
2836
Cary Clarkac47b882018-01-11 10:35:44 -05002837Copies a Rect of pixels from Bitmap to dstPixels. Copy starts at (srcX, srcY),
2838and does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002839
Cary Clarkac47b882018-01-11 10:35:44 -05002840dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
2841destination. dstRowBytes specifics the gap from one destination row to the next.
2842Returns true if pixels are copied. Returns false if:
Cary Clarkbc5697d2017-10-04 14:31:33 -04002843#List
2844# dstInfo.addr() equals nullptr ##
2845# dstRowBytes is less than dstInfo.minRowBytes ##
2846# Pixel_Ref is nullptr ##
2847##
2848
Cary Clarkac47b882018-01-11 10:35:44 -05002849Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002850kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002851If Bitmap colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
2852If Bitmap alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
2853match. If Bitmap colorSpace is nullptr, dstInfo.colorSpace must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002854false if pixel conversion is not possible.
2855
2856srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04002857false if width() or height() is zero or negative.
2858Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04002859#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002860abs(srcX) >= Bitmap width()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002861##
2862, or if
2863#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002864abs(srcY) >= Bitmap height()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002865##
2866.
2867
2868#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
2869#Param dstPixels destination pixel storage ##
2870#Param dstRowBytes destination row length ##
2871#Param srcX column index whose absolute value is less than width() ##
2872#Param srcY row index whose absolute value is less than height() ##
2873
2874#Return true if pixels are copied to dstPixels ##
2875
2876#Example
2877#Height 128
2878#Description
2879Transferring the gradient from 8 bits per component to 4 bits per component
2880creates visible banding.
2881##
Ben Wagner29380bd2017-10-09 14:43:00 -04002882 const int width = 256;
2883 const int height = 64;
2884 SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(width, height);
2885 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
2886 SkPoint gradPoints[] = { { 0, 0 }, { 256, 0 } };
2887 SkPaint paint;
2888 paint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
2889 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
2890 SkBitmap bitmap;
2891 bitmap.allocPixels(srcInfo);
2892 SkCanvas srcCanvas(bitmap);
2893 srcCanvas.drawRect(SkRect::MakeWH(width, height), paint);
2894 canvas->drawBitmap(bitmap, 0, 0);
2895 SkImageInfo dstInfo = srcInfo.makeColorType(kARGB_4444_SkColorType);
2896 std::vector<int16_t> dstPixels;
2897 dstPixels.resize(height * width);
2898 bitmap.readPixels(dstInfo, &dstPixels.front(), width * 2, 0, 0);
2899 SkPixmap dstPixmap(dstInfo, &dstPixels.front(), width * 2);
2900 bitmap.installPixels(dstPixmap);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002901 canvas->drawBitmap(bitmap, 0, 64);
2902##
2903
2904#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2905
2906##
2907
2908# ------------------------------------------------------------------------------
2909
2910#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY) const
2911
Cary Clarkac47b882018-01-11 10:35:44 -05002912Copies a Rect of pixels from Bitmap to dst. Copy starts at (srcX, srcY), and
2913does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002914
2915dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2916and row bytes of destination. dst.rowBytes specifics the gap from one destination
2917row to the next. Returns true if pixels are copied. Returns false if:
2918#List
2919# dst pixel storage equals nullptr ##
2920# dst.rowBytes is less than SkImageInfo::minRowBytes ##
2921# Pixel_Ref is nullptr ##
2922##
2923
Cary Clarkac47b882018-01-11 10:35:44 -05002924Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002925kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002926If Bitmap colorType is kGray_8_SkColorType, dst Color_Space must match.
2927If Bitmap alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
2928match. If Bitmap colorSpace is nullptr, dst Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002929false if pixel conversion is not possible.
2930
2931srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04002932false if width() or height() is zero or negative.
2933Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04002934#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002935abs(srcX) >= Bitmap width()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002936##
2937, or if
2938#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002939abs(srcY) >= Bitmap height()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002940##
2941.
2942
2943#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
2944#Param srcX column index whose absolute value is less than width() ##
2945#Param srcY row index whose absolute value is less than height() ##
2946
2947#Return true if pixels are copied to dst ##
2948
2949#Example
2950#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002951 std::vector<int32_t> srcPixels;
2952 srcPixels.resize(source.height() * source.rowBytes());
2953 for (int y = 0; y < 4; ++y) {
2954 for (int x = 0; x < 4; ++x) {
2955 SkPixmap pixmap(SkImageInfo::MakeN32Premul(source.width() / 4, source.height() / 4),
2956 &srcPixels.front() + x * source.height() * source.width() / 4 +
2957 y * source.width() / 4, source.rowBytes());
2958 source.readPixels(pixmap, x * source.width() / 4, y * source.height() / 4);
2959 }
2960 }
2961 canvas->scale(.5f, .5f);
2962 SkBitmap bitmap;
2963 bitmap.installPixels(SkImageInfo::MakeN32Premul(source.width(), source.height()),
2964 &srcPixels.front(), source.rowBytes());
Cary Clarkbc5697d2017-10-04 14:31:33 -04002965 canvas->drawBitmap(bitmap, 0, 0);
2966##
2967
2968#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2969
2970##
2971
2972# ------------------------------------------------------------------------------
2973
2974#Method bool readPixels(const SkPixmap& dst) const
2975
Cary Clarkac47b882018-01-11 10:35:44 -05002976Copies a Rect of pixels from Bitmap to dst. Copy starts at (0, 0), and
2977does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002978
2979dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2980and row bytes of destination. dst.rowBytes specifics the gap from one destination
2981row to the next. Returns true if pixels are copied. Returns false if:
2982#List
2983# dst pixel storage equals nullptr ##
2984# dst.rowBytes is less than SkImageInfo::minRowBytes ##
2985# Pixel_Ref is nullptr ##
2986##
2987
Cary Clarkac47b882018-01-11 10:35:44 -05002988Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002989kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002990If Bitmap colorType is kGray_8_SkColorType, dst Color_Space must match.
2991If Bitmap alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
2992match. If Bitmap colorSpace is nullptr, dst Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002993false if pixel conversion is not possible.
2994
2995#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
2996
2997#Return true if pixels are copied to dst ##
2998
2999#Example
3000#Height 128
3001#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04003002 std::vector<int32_t> srcPixels;
3003 srcPixels.resize(source.height() * source.width() * 8);
3004 for (int i = 0; i < 2; ++i) {
3005 SkPixmap pixmap(SkImageInfo::Make(source.width() * 2, source.height(),
3006 i ? kRGBA_8888_SkColorType : kBGRA_8888_SkColorType, kPremul_SkAlphaType),
3007 &srcPixels.front() + i * source.width(), source.rowBytes() * 2);
3008 source.readPixels(pixmap);
3009 }
3010 canvas->scale(.25f, .25f);
3011 SkBitmap bitmap;
3012 bitmap.installPixels(SkImageInfo::MakeN32Premul(source.width() * 2, source.height()),
3013 &srcPixels.front(), source.rowBytes() * 2);
3014 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003015##
3016
3017#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
3018
3019##
3020
3021# ------------------------------------------------------------------------------
3022
3023#Method bool writePixels(const SkPixmap& src, int dstX, int dstY)
Cary Clark78de7512018-02-07 07:27:09 -05003024#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05003025#Line # copies and converts pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003026Copies a Rect of pixels from src. Copy starts at (dstX, dstY), and does not exceed
3027(src.width(), src.height()).
3028
3029src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
3030and row bytes of source. src.rowBytes specifics the gap from one source
3031row to the next. Returns true if pixels are copied. Returns false if:
3032#List
3033# src pixel storage equals nullptr ##
3034# src.rowBytes is less than SkImageInfo::minRowBytes ##
3035# Pixel_Ref is nullptr ##
3036##
3037
Cary Clarkac47b882018-01-11 10:35:44 -05003038Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04003039kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05003040If Bitmap colorType is kGray_8_SkColorType, src Color_Space must match.
3041If Bitmap alphaType is kOpaque_SkAlphaType, src Alpha_Type must
3042match. If Bitmap colorSpace is nullptr, src Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04003043false if pixel conversion is not possible.
3044
3045dstX and dstY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04003046false if width() or height() is zero or negative.
3047Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04003048#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05003049abs(dstX) >= Bitmap width()
Cary Clarkbc5697d2017-10-04 14:31:33 -04003050##
3051, or if
3052#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05003053abs(dstY) >= Bitmap height()
Cary Clarkbc5697d2017-10-04 14:31:33 -04003054##
3055.
3056
3057#Param src source Pixmap: Image_Info, pixels, row bytes ##
3058#Param dstX column index whose absolute value is less than width() ##
3059#Param dstY row index whose absolute value is less than height() ##
3060
3061#Return true if src pixels are copied to Bitmap ##
3062
3063#Example
3064#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04003065 std::vector<int32_t> srcPixels;
3066 int width = image->width();
3067 int height = image->height();
3068 srcPixels.resize(height * width * 4);
3069 SkPixmap pixmap(SkImageInfo::MakeN32Premul(width, height), (const void*) &srcPixels.front(),
3070 width * 4);
3071 image->readPixels(pixmap, 0, 0);
3072 canvas->scale(.5f, .5f);
3073 width /= 4;
3074 height /= 4;
3075 for (int y = 0; y < 4; ++y) {
3076 for (int x = 0; x < 4; ++x) {
3077 SkBitmap bitmap;
3078 bitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height));
3079 bitmap.writePixels(pixmap, -y * width, -x * height);
3080 canvas->drawBitmap(bitmap, x * width, y * height);
3081 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003082 }
3083##
3084
3085#SeeAlso readPixels
3086
3087##
3088
3089# ------------------------------------------------------------------------------
3090
3091#Method bool writePixels(const SkPixmap& src)
3092
3093Copies a Rect of pixels from src. Copy starts at (0, 0), and does not exceed
3094(src.width(), src.height()).
3095
3096src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
3097and row bytes of source. src.rowBytes specifics the gap from one source
3098row to the next. Returns true if pixels are copied. Returns false if:
3099#List
3100# src pixel storage equals nullptr ##
3101# src.rowBytes is less than SkImageInfo::minRowBytes ##
3102# Pixel_Ref is nullptr ##
3103##
3104
Cary Clarkac47b882018-01-11 10:35:44 -05003105Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04003106kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05003107If Bitmap colorType is kGray_8_SkColorType, src Color_Space must match.
3108If Bitmap alphaType is kOpaque_SkAlphaType, src Alpha_Type must
3109match. If Bitmap colorSpace is nullptr, src Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04003110false if pixel conversion is not possible.
3111
3112#Param src source Pixmap: Image_Info, pixels, row bytes ##
3113
3114#Return true if src pixels are copied to Bitmap ##
3115
3116#Example
3117#Height 80
Ben Wagner29380bd2017-10-09 14:43:00 -04003118 SkBitmap bitmap;
3119 bitmap.allocPixels(SkImageInfo::MakeN32Premul(2, 2));
3120 bitmap.eraseColor(SK_ColorGREEN);
3121 SkPMColor color = 0xFF5599BB;
3122 SkPixmap src(SkImageInfo::MakeN32Premul(1, 1), &color, 4);
3123 bitmap.writePixels(src);
3124 canvas->scale(40, 40);
3125 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003126##
3127
3128#SeeAlso readPixels
3129
3130##
3131
3132# ------------------------------------------------------------------------------
3133
3134#Method bool writePixels(const SkPixmap& src, int x, int y, SkTransferFunctionBehavior behavior)
3135
3136Copies a Rect of pixels from src. Copy starts at (0, 0), and does not exceed
3137(src.width(), src.height()).
3138
3139src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
3140and row bytes of source. src.rowBytes specifics the gap from one source
3141row to the next. Returns true if pixels are copied. Returns false if:
3142#List
3143# src pixel storage equals nullptr ##
3144# src.rowBytes is less than SkImageInfo::minRowBytes ##
3145# Pixel_Ref is nullptr ##
3146##
3147
Cary Clarkac47b882018-01-11 10:35:44 -05003148Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04003149kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05003150If Bitmap colorType is kGray_8_SkColorType, src Color_Space must match.
3151If Bitmap alphaType is kOpaque_SkAlphaType, src Alpha_Type must
3152match. If Bitmap colorSpace is nullptr, src Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04003153false if pixel conversion is not possible. Returns false if width() or height()
3154is zero or negative.
3155
3156If behavior is SkTransferFunctionBehavior::kRespect: converts src
3157pixels to a linear space before converting to Image_Info.
3158If behavior is SkTransferFunctionBehavior::kIgnore: src
3159pixels are treated as if they are linear, regardless of how they are encoded.
3160
3161#Param src source Pixmap: Image_Info, pixels, row bytes ##
3162#Param x column index whose absolute value is less than width() ##
3163#Param y row index whose absolute value is less than height() ##
3164#Param behavior one of: SkTransferFunctionBehavior::kRespect,
3165 SkTransferFunctionBehavior::kIgnore
3166##
3167
3168#Return true if src pixels are copied to Bitmap ##
3169
3170#Example
3171#Height 64
Ben Wagner29380bd2017-10-09 14:43:00 -04003172 const int width = 256;
3173 const int height = 32;
3174 std::vector<int32_t> dstPixels;
3175 dstPixels.resize(height * width * 4);
3176 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
3177 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
3178 SkPoint gradPoints[] = { { 0, 0 }, { width, 0 } };
3179 SkPaint gradPaint;
3180 gradPaint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
3181 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
3182 for (auto behavior : { SkTransferFunctionBehavior::kRespect,
3183 SkTransferFunctionBehavior::kIgnore} ) {
3184 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
3185 SkBitmap bitmap;
3186 bitmap.installPixels(dstPixmap);
3187 SkCanvas srcCanvas(bitmap);
3188 srcCanvas.drawRect(SkRect::MakeWH(width, height), gradPaint);
3189 if (bitmap.writePixels(dstPixmap, 0, 0, behavior)) {
3190 canvas->drawBitmap(bitmap, 0, 0);
3191 }
3192 canvas->translate(0, height);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003193 }
3194##
3195
3196#SeeAlso readPixels
3197
3198##
3199
3200# ------------------------------------------------------------------------------
3201
3202#Method bool hasHardwareMipMap() const
Cary Clark78de7512018-02-07 07:27:09 -05003203#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05003204#Line # returns Mip_Map support present; Android only ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003205#Private
3206Android framework only.
3207##
3208
3209#Return true if setHasHardwareMipMap has been called with true ##
3210
3211#NoExample
3212##
3213
3214#SeeAlso setHasHardwareMipMap
3215
3216##
3217
3218# ------------------------------------------------------------------------------
3219
3220#Method void setHasHardwareMipMap(bool hasHardwareMipMap)
Cary Clark78de7512018-02-07 07:27:09 -05003221#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05003222#Line # sets Mip_Map support present; Android only ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003223#Private
3224Android framework only.
3225##
3226
3227#Param hasHardwareMipMap sets state ##
3228
3229#NoExample
3230##
3231
3232#SeeAlso hasHardwareMipMap
3233
3234##
3235
3236# ------------------------------------------------------------------------------
3237
3238#Method bool extractAlpha(SkBitmap* dst) const
Cary Clark78de7512018-02-07 07:27:09 -05003239#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -05003240#Line # creates Bitmap containing Alpha of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003241Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3242or dst pixels cannot be allocated.
3243
3244Uses HeapAllocator to reserve memory for dst Pixel_Ref.
3245
3246#Param dst holds Pixel_Ref to fill with alpha layer ##
3247
3248#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3249
3250#Example
3251#Height 100
Ben Wagner29380bd2017-10-09 14:43:00 -04003252 SkBitmap alpha, bitmap;
3253 bitmap.allocN32Pixels(100, 100);
3254 SkCanvas offscreen(bitmap);
3255 offscreen.clear(0);
3256 SkPaint paint;
3257 paint.setAntiAlias(true);
3258 paint.setColor(SK_ColorBLUE);
3259 paint.setStyle(SkPaint::kStroke_Style);
3260 paint.setStrokeWidth(20);
3261 offscreen.drawCircle(50, 50, 39, paint);
3262 offscreen.flush();
3263 bitmap.extractAlpha(&alpha);
3264 paint.setColor(SK_ColorRED);
3265 canvas->drawBitmap(bitmap, 0, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003266 canvas->drawBitmap(alpha, 100, 0, &paint);
3267##
3268
3269#SeeAlso extractSubset
3270
3271##
3272
3273# ------------------------------------------------------------------------------
3274
3275#Method bool extractAlpha(SkBitmap* dst, const SkPaint* paint,
3276 SkIPoint* offset) const
3277
3278Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3279or dst pixels cannot be allocated.
3280
Cary Clarkf895a422018-02-27 09:54:21 -05003281If paint is not nullptr and contains Mask_Filter, SkMaskFilter
Cary Clarkbc5697d2017-10-04 14:31:33 -04003282generates Mask_Alpha from Bitmap. Uses HeapAllocator to reserve memory for dst
3283Pixel_Ref. Sets offset to top-left position for dst for alignment with Bitmap;
3284(0, 0) unless SkMaskFilter generates mask.
3285
3286#Param dst holds Pixel_Ref to fill with alpha layer ##
3287#Param paint holds optional Mask_Filter; may be nullptr ##
3288#Param offset top-left position for dst; may be nullptr ##
3289
3290#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3291
Cary Clark4855f782018-02-06 09:41:53 -05003292#Bug 7103
Cary Clarkbc5697d2017-10-04 14:31:33 -04003293#Example
3294#Height 160
Cary Clark681287e2018-03-16 11:34:15 -04003295 auto radiusToSigma = [](SkScalar radius) -> SkScalar {
3296 static const SkScalar kBLUR_SIGMA_SCALE = 0.57735f;
3297 return radius > 0 ? kBLUR_SIGMA_SCALE * radius + 0.5f : 0.0f;
3298 };
3299 SkBitmap alpha, bitmap;
3300 bitmap.allocN32Pixels(100, 100);
3301 SkCanvas offscreen(bitmap);
3302 offscreen.clear(0);
3303 SkPaint paint;
3304 paint.setAntiAlias(true);
3305 paint.setColor(SK_ColorBLUE);
3306 paint.setStyle(SkPaint::kStroke_Style);
3307 paint.setStrokeWidth(20);
3308 offscreen.drawCircle(50, 50, 39, paint);
3309 offscreen.flush();
3310 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, radiusToSigma(25)));
3311 SkIPoint offset;
3312 bitmap.extractAlpha(&alpha, &paint, &offset);
3313 paint.setColor(SK_ColorRED);
3314 canvas->drawBitmap(bitmap, 0, -offset.fY, &paint);
3315 canvas->drawBitmap(alpha, 100 + offset.fX, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003316##
3317
3318#SeeAlso extractSubset
3319
3320##
3321
3322# ------------------------------------------------------------------------------
3323
3324#Method bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator,
3325 SkIPoint* offset) const
3326
3327Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3328or dst pixels cannot be allocated.
3329
Cary Clarkf895a422018-02-27 09:54:21 -05003330If paint is not nullptr and contains Mask_Filter, SkMaskFilter
Cary Clarkbc5697d2017-10-04 14:31:33 -04003331generates Mask_Alpha from Bitmap. allocator may reference a custom allocation
3332class or be set to nullptr to use HeapAllocator. Sets offset to top-left
3333position for dst for alignment with Bitmap; (0, 0) unless SkMaskFilter generates
3334mask.
3335
3336#Param dst holds Pixel_Ref to fill with alpha layer ##
3337#Param paint holds optional Mask_Filter; may be nullptr ##
3338#Param allocator method to reserve memory for Pixel_Ref; may be nullptr ##
3339#Param offset top-left position for dst; may be nullptr ##
3340
3341#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3342
Cary Clark4855f782018-02-06 09:41:53 -05003343#Bug 7104
Cary Clarkbc5697d2017-10-04 14:31:33 -04003344#Example
3345#Height 128
Ben Wagner29380bd2017-10-09 14:43:00 -04003346 SkBitmap alpha, bitmap;
3347 bitmap.allocN32Pixels(100, 100);
3348 SkCanvas offscreen(bitmap);
3349 offscreen.clear(0);
3350 SkPaint paint;
3351 paint.setAntiAlias(true);
3352 paint.setColor(SK_ColorBLUE);
3353 paint.setStyle(SkPaint::kStroke_Style);
3354 paint.setStrokeWidth(20);
3355 offscreen.drawCircle(50, 50, 39, paint);
3356 offscreen.flush();
Cary Clark681287e2018-03-16 11:34:15 -04003357 paint.setMaskFilter(SkMaskFilter::MakeBlur(kOuter_SkBlurStyle, 3));
Ben Wagner29380bd2017-10-09 14:43:00 -04003358 SkIPoint offset;
3359 bitmap.extractAlpha(&alpha, &paint, nullptr, &offset);
3360 paint.setColor(SK_ColorRED);
3361 canvas->drawBitmap(bitmap, 0, -offset.fY, &paint);
3362 canvas->drawBitmap(alpha, 100 + offset.fX, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003363##
3364
3365#SeeAlso extractSubset
3366
3367##
3368
3369# ------------------------------------------------------------------------------
3370
3371#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05003372#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05003373#Line # returns Pixmap if possible ##
Cary Clark154beea2017-10-26 07:58:48 -04003374Copies Bitmap pixel address, row bytes, and Image_Info to pixmap, if address
3375is available, and returns true. If pixel address is not available, return
3376false and leave pixmap unchanged.
3377
3378pixmap contents become invalid on any future change to Bitmap.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003379
3380#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
3381
3382#Return true if Bitmap has direct access to pixels ##
3383
3384#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04003385 SkBitmap bitmap;
3386 bitmap.allocPixels(SkImageInfo::MakeN32Premul(6, 11));
3387 SkCanvas offscreen(bitmap);
3388 offscreen.clear(SK_ColorWHITE);
3389 SkPaint paint;
3390 offscreen.drawString("?", 0, 10, paint);
3391 SkPixmap pixmap;
3392 if (bitmap.peekPixels(&pixmap)) {
3393 const SkPMColor* pixels = pixmap.addr32();
3394 SkPMColor pmWhite = pixels[0];
3395 for (int y = 0; y < bitmap.height(); ++y) {
3396 for (int x = 0; x < bitmap.width(); ++x) {
3397 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
3398 }
3399 SkDebugf("\n");
3400 }
3401 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003402 #StdOut
Cary Clark2f466242017-12-11 16:03:17 -05003403------
3404-xxx--
3405x---x-
3406----x-
3407---x--
3408--x---
3409--x---
3410------
3411--x---
3412--x---
Cary Clarka560c472017-11-27 10:44:06 -05003413------
Cary Clarkbc5697d2017-10-04 14:31:33 -04003414 #StdOut ##
3415##
3416
Cary Clark0c5f5462017-12-15 11:21:51 -05003417#SeeAlso pixmap() installPixels readPixels writePixels
Cary Clarkbc5697d2017-10-04 14:31:33 -04003418
3419##
3420
3421# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05003422#Subtopic Utility
3423#Populate
3424#Line # rarely called management functions ##
3425##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003426
Cary Clark154beea2017-10-26 07:58:48 -04003427#Method void validate() const;
Cary Clark78de7512018-02-07 07:27:09 -05003428#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05003429#Line # asserts if Bitmap is invalid (debug only) ##
Cary Clark154beea2017-10-26 07:58:48 -04003430Asserts if internal values are illegal or inconsistent. Only available if
3431SK_DEBUG is defined at compile time.
3432
3433#NoExample
3434##
3435
Cary Clark06c20f32018-03-20 15:53:27 -04003436#SeeAlso SkImageInfo::validate
Cary Clark154beea2017-10-26 07:58:48 -04003437
3438##
3439
3440# ------------------------------------------------------------------------------
3441
Cary Clarkbc5697d2017-10-04 14:31:33 -04003442#Method void toString(SkString* str) const;
Cary Clark78de7512018-02-07 07:27:09 -05003443#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05003444#Line # converts Bitmap to machine readable form ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003445#DefinedBy SK_TO_STRING_NONVIRT() ##
3446
3447#Private
3448macro expands to: void toString(SkString* str) const;
3449##
3450
Cary Clarkac47b882018-01-11 10:35:44 -05003451Creates string representation of Bitmap. The representation is read by
Cary Clarkbc5697d2017-10-04 14:31:33 -04003452internal debugging tools. The interface and implementation may be
3453suppressed by defining SK_IGNORE_TO_STRING.
3454
3455#Param str storage for string representation ##
3456
3457#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04003458 SkBitmap bitmap;
3459 int width = 6;
3460 int height = 11;
3461 bitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height));
3462 SkString string;
3463 bitmap.toString(&string);
3464 SkString match;
3465 match.printf("(%d, %d)", width, height);
3466 int start = string.find(match.c_str());
3467 if (start >= 0) {
3468 SkString whStr(&string.c_str()[start], match.size());
3469 SkDebugf("bitmap dimensions %s\n", whStr.c_str());
Cary Clarkbc5697d2017-10-04 14:31:33 -04003470 }
3471 #StdOut
3472 bitmap dimensions (6, 11)
3473 ##
3474##
3475
3476#SeeAlso SkPaint::toString
3477
3478##
3479
3480#Class SkBitmap ##
3481
3482#Topic Bitmap ##
Cary Clark4855f782018-02-06 09:41:53 -05003483