blob: 353d24497654d635fa056a0e259c4c14c5b3e0b8 [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;
204 original.tryAllocPixels(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
205 SkDebugf("original has pixels before copy: %s\n", original.getPixels() ? "true" : "false");
206 SkBitmap copy(original);
207 SkDebugf("original has pixels after copy: %s\n", original.getPixels() ? "true" : "false");
208 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
Cary Clarkbc5697d2017-10-04 14:31:33 -0400209}
210#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400211original has pixels before copy: true
212original has pixels after copy: true
Cary Clarkbc5697d2017-10-04 14:31:33 -0400213copy has pixels: true
214##
215##
216
217#SeeAlso setInfo setPixelRef setPixels swap
218
219##
220
221# ------------------------------------------------------------------------------
222
223#Method SkBitmap(SkBitmap&& src)
224
Cary Clarkab2621d2018-01-30 10:08:57 -0500225#Line # takes ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400226Copies settings from src to returned Bitmap. Moves ownership of src pixels to
227Bitmap.
228
229#Param src Bitmap to copy Image_Info, and reassign Pixel_Ref ##
230
231#Return copy of src ##
232
233#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400234void draw(SkCanvas* canvas) {
235 SkBitmap original;
236 original.tryAllocPixels(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
237 SkDebugf("original has pixels before move: %s\n", original.getPixels() ? "true" : "false");
238 SkBitmap copy(std::move(original));
239 SkDebugf("original has pixels after move: %s\n", original.getPixels() ? "true" : "false");
240 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
241}
Cary Clarkbc5697d2017-10-04 14:31:33 -0400242#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400243original has pixels before move: true
244original has pixels after move: false
Cary Clarkbc5697d2017-10-04 14:31:33 -0400245copy has pixels: true
246##
247##
248
249#SeeAlso setInfo setPixelRef setPixels swap
250
251##
252
253# ------------------------------------------------------------------------------
254
255#Method ~SkBitmap()
256
Cary Clarkab2621d2018-01-30 10:08:57 -0500257#Line # releases ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400258Decrements Pixel_Ref reference count, if Pixel_Ref is not nullptr.
259
260#NoExample
261##
262
263#SeeAlso Pixel_Ref
264
265##
266
267# ------------------------------------------------------------------------------
268
269#Method SkBitmap& operator=(const SkBitmap& src)
270
Cary Clarkab2621d2018-01-30 10:08:57 -0500271#Line # shares ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400272Copies settings from src to returned Bitmap. Shares pixels if src has pixels
273allocated, so both bitmaps reference the same pixels.
274
275#Param src Bitmap to copy Image_Info, and share Pixel_Ref ##
276
277#Return copy of src ##
278
279#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400280void draw(SkCanvas* canvas) {
281 SkBitmap original;
282 original.tryAllocPixels(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
283 SkDebugf("original has pixels before copy: %s\n", original.getPixels() ? "true" : "false");
284 SkBitmap copy = original;
285 SkDebugf("original has pixels after copy: %s\n", original.getPixels() ? "true" : "false");
286 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
Cary Clarkbc5697d2017-10-04 14:31:33 -0400287}
288#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400289original has pixels before copy: true
290original has pixels after copy: true
Cary Clarkbc5697d2017-10-04 14:31:33 -0400291copy has pixels: true
292##
293##
294
295#SeeAlso setInfo setPixelRef setPixels swap
296
297##
298
299# ------------------------------------------------------------------------------
300
301#Method SkBitmap& operator=(SkBitmap&& src)
302
Cary Clarkab2621d2018-01-30 10:08:57 -0500303#Line # takes ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400304Copies settings from src to returned Bitmap. Moves ownership of src pixels to
305Bitmap.
306
307#Param src Bitmap to copy Image_Info, and reassign Pixel_Ref ##
308
309#Return copy of src ##
310
311#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400312void draw(SkCanvas* canvas) {
313 SkBitmap original;
314 original.tryAllocPixels(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
315 SkDebugf("original has pixels before move: %s\n", original.getPixels() ? "true" : "false");
316 SkBitmap copy = std::move(original);
317 SkDebugf("original has pixels after move: %s\n", original.getPixels() ? "true" : "false");
318 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
319}
Cary Clarkbc5697d2017-10-04 14:31:33 -0400320#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400321original has pixels before move: true
322original has pixels after move: false
Cary Clarkbc5697d2017-10-04 14:31:33 -0400323copy has pixels: true
324##
325##
326
327#SeeAlso setInfo setPixelRef setPixels swap
328
329##
330
331# ------------------------------------------------------------------------------
332
333#Method void swap(SkBitmap& other)
Cary Clark78de7512018-02-07 07:27:09 -0500334#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -0500335#Line # exchanges Bitmap pair ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400336Swaps the fields of the two bitmaps.
337
338#Param other Bitmap exchanged with original ##
339
340#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400341void draw(SkCanvas* canvas) {
342 auto debugster = [](const char* prefix, const SkBitmap& b) -> void {
343 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500344 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
345 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Ben Wagner29380bd2017-10-09 14:43:00 -0400346 SkDebugf("%s width:%d height:%d colorType:k%s_SkColorType alphaType:k%s_SkAlphaType\n",
347 prefix, b.width(), b.height(), colors[b.colorType()], alphas[b.alphaType()]);
348 };
349 SkBitmap one, two;
350 one.tryAllocPixels(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
351 two.tryAllocPixels(SkImageInfo::Make(2, 2, kBGRA_8888_SkColorType, kPremul_SkAlphaType));
352 for (int index = 0; index < 2; ++index) {
353 debugster("one", one);
354 debugster("two", two);
355 one.swap(two);
356 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400357}
358#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400359one width:1 height:1 colorType:kRGBA_8888_SkColorType alphaType:kOpaque_SkAlphaType
360two width:2 height:2 colorType:kBGRA_8888_SkColorType alphaType:kPremul_SkAlphaType
361one width:2 height:2 colorType:kBGRA_8888_SkColorType alphaType:kPremul_SkAlphaType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400362two width:1 height:1 colorType:kRGBA_8888_SkColorType alphaType:kOpaque_SkAlphaType
363##
364##
365
366#SeeAlso SkBitmap(SkBitmap&& src) operator=(SkBitmap&& src)
367
368##
369
370# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -0500371#Subtopic Property
372#Populate
373#Line # metrics and attributes ##
374##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400375
Hal Canary99578d22017-12-14 21:13:47 -0500376#Method const SkPixmap& pixmap() const
Cary Clark78de7512018-02-07 07:27:09 -0500377#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500378#Line # returns Pixmap ##
Hal Canary99578d22017-12-14 21:13:47 -0500379Returns a constant reference to the Pixmap holding the Bitmap pixel
380address, row bytes, and Image_Info.
Cary Clark0c5f5462017-12-15 11:21:51 -0500381
Cary Clarkac47b882018-01-11 10:35:44 -0500382#Return reference to Pixmap describing this Bitmap ##
Cary Clark0c5f5462017-12-15 11:21:51 -0500383
384#Example
385 SkBitmap bitmap;
386 bitmap.allocPixels(SkImageInfo::MakeN32Premul(10, 11));
387 SkCanvas offscreen(bitmap);
388 offscreen.clear(SK_ColorWHITE);
389 SkPaint paint;
390 offscreen.drawString("&", 0, 10, paint);
Hal Canary99578d22017-12-14 21:13:47 -0500391 const SkPixmap& pixmap = bitmap.pixmap();
Cary Clark0c5f5462017-12-15 11:21:51 -0500392 if (pixmap.addr()) {
Hal Canary99578d22017-12-14 21:13:47 -0500393 SkPMColor pmWhite = *pixmap.addr32(0, 0);
394 for (int y = 0; y < pixmap.height(); ++y) {
395 for (int x = 0; x < pixmap.width(); ++x) {
396 SkDebugf("%c", *pixmap.addr32(x, y) == pmWhite ? '-' : 'x');
Cary Clark0c5f5462017-12-15 11:21:51 -0500397 }
398 SkDebugf("\n");
399 }
400 }
401 #StdOut
402----------
403---xx-----
404--x--x----
405--x-------
406--xx------
407--x-x---x-
408-x---x--x-
409-x----xx--
410-xx---x---
411--xxxx-xx-
412----------
413 #StdOut ##
414
415##
416
417#SeeAlso peekPixels installPixels readPixels writePixels
418
419##
420
421# ------------------------------------------------------------------------------
422
Cary Clarkbc5697d2017-10-04 14:31:33 -0400423#Method const SkImageInfo& info() const
Cary Clark78de7512018-02-07 07:27:09 -0500424#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500425#Line # returns Image_Info ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400426Returns width, height, Alpha_Type, Color_Type, and Color_Space.
427
428#Return reference to Image_Info ##
429
430#Example
431#Image 4
Ben Wagner29380bd2017-10-09 14:43:00 -0400432void draw(SkCanvas* canvas) {
433 // SkBitmap source; // pre-populated with soccer ball by fiddle.skia.org
434 const SkImageInfo& info = source.info();
435 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500436 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
437 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Ben Wagner29380bd2017-10-09 14:43:00 -0400438 SkDebugf("width: %d height: %d color: %s alpha: %s\n", info.width(), info.height(),
439 colors[info.colorType()], alphas[info.alphaType()]);
440#StdOut
441width: 56 height: 56 color: BGRA_8888 alpha: Opaque
442##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400443}
444##
445
446#SeeAlso Image_Info
447
448##
449
450# ------------------------------------------------------------------------------
451
452#Method int width() const
Cary Clark78de7512018-02-07 07:27:09 -0500453#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500454#Line # returns pixel column count ##
Cary Clarka560c472017-11-27 10:44:06 -0500455Returns pixel count in each row. Should be equal or less than:
Cary Clark154beea2017-10-26 07:58:48 -0400456
Cary Clarkbc5697d2017-10-04 14:31:33 -0400457#Formula
458rowBytes() / info().bytesPerPixel()
459##
460.
461
462Maybe be less than pixelRef().width(). Will not exceed pixelRef().width() less
463pixelRefOrigin().fX.
464
465#Return pixel width in Image_Info ##
466
467#Example
468 SkImageInfo info = SkImageInfo::MakeA8(16, 32);
469 SkBitmap bitmap;
470 bitmap.setInfo(info);
471 SkDebugf("bitmap width: %d info width: %d\n", bitmap.width(), info.width());
472#StdOut
473bitmap width: 16 info width: 16
474##
475##
476
477#SeeAlso height() SkPixelRef::width() SkImageInfo::width()
478
479##
480
481# ------------------------------------------------------------------------------
482
483#Method int height() const
Cary Clark78de7512018-02-07 07:27:09 -0500484#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500485#Line # returns pixel row count ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400486Returns pixel row count.
487
488Maybe be less than pixelRef().height(). Will not exceed pixelRef().height() less
489pixelRefOrigin().fY.
490
491#Return pixel height in Image_Info ##
492
493#Example
494 SkImageInfo info = SkImageInfo::MakeA8(16, 32);
495 SkBitmap bitmap;
496 bitmap.setInfo(info);
497 SkDebugf("bitmap height: %d info height: %d\n", bitmap.height(), info.height());
498#StdOut
499bitmap height: 32 info height: 32
500##
501##
502
503#SeeAlso width() SkPixelRef::height() SkImageInfo::height()
504
505##
506
507# ------------------------------------------------------------------------------
508
509#Method SkColorType colorType() const
Cary Clark78de7512018-02-07 07:27:09 -0500510#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500511#Line # returns Image_Info Color_Type ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500512Returns Color_Type, one of: #list_of_color_types#.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400513
514#Return Color_Type in Image_Info ##
515
516#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500517 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
518 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Ben Wagner29380bd2017-10-09 14:43:00 -0400519 SkBitmap bitmap;
520 bitmap.setInfo(SkImageInfo::MakeA8(16, 32));
521 SkDebugf("color type: k" "%s" "_SkColorType\n", colors[bitmap.colorType()]);
Cary Clarkbc5697d2017-10-04 14:31:33 -0400522#StdOut
Cary Clarkab2621d2018-01-30 10:08:57 -0500523color type: kAlpha_8_SkColorType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400524##
525##
526
527#SeeAlso alphaType() SkImageInfo::colorType
528
529##
530
531# ------------------------------------------------------------------------------
532
533#Method SkAlphaType alphaType() const
Cary Clark78de7512018-02-07 07:27:09 -0500534#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500535#Line # returns Image_Info Alpha_Type ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400536Returns Alpha_Type, one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
537kPremul_SkAlphaType, kUnpremul_SkAlphaType.
538
539#Return Alpha_Type in Image_Info ##
540
541#Example
542 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
543 SkPixmap pixmap(SkImageInfo::MakeA8(16, 32), nullptr, 64);
544 SkDebugf("alpha type: k" "%s" "_SkAlphaType\n", alphas[pixmap.alphaType()]);
545#StdOut
546alpha type: kPremul_SkAlphaType
547##
548##
549
550#SeeAlso colorType() SkImageInfo::alphaType
551
552##
553
554# ------------------------------------------------------------------------------
555
556#Method SkColorSpace* colorSpace() const
Cary Clark78de7512018-02-07 07:27:09 -0500557#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500558#Line # returns Image_Info Color_Space ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400559Returns Color_Space, the range of colors, associated with Image_Info. The
560reference count of Color_Space is unchanged. The returned Color_Space is
561immutable.
562
Cary Clark2f466242017-12-11 16:03:17 -0500563#Return Color_Space in Image_Info, or nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400564
565#Example
566#Description
567SkColorSpace::MakeSRGBLinear creates Color_Space with linear gamma
568and an sRGB gamut. This Color_Space gamma is not close to sRGB gamma.
569##
Ben Wagner29380bd2017-10-09 14:43:00 -0400570 SkBitmap bitmap;
Cary Clarkbc5697d2017-10-04 14:31:33 -0400571 bitmap.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
572 SkColorSpace::MakeSRGBLinear()));
573 SkColorSpace* colorSpace = bitmap.colorSpace();
574 SkDebugf("gammaCloseToSRGB: %s gammaIsLinear: %s isSRGB: %s\n",
575 colorSpace->gammaCloseToSRGB() ? "true" : "false",
576 colorSpace->gammaIsLinear() ? "true" : "false",
577 colorSpace->isSRGB() ? "true" : "false");
578#StdOut
579gammaCloseToSRGB: false gammaIsLinear: true isSRGB: false
580##
581##
582
583#SeeAlso Color_Space SkImageInfo::colorSpace
584
585##
586
587# ------------------------------------------------------------------------------
588
589#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark78de7512018-02-07 07:27:09 -0500590#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500591#Line # returns Image_Info Color_Space ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400592Returns a smart pointer to Color_Space, the range of colors, associated with
593Image_Info. The smart pointer tracks the number of objects sharing this
594Color_Space reference so the memory is released when the owners destruct.
595
596The returned Color_Space is immutable.
597
598#Return Color_Space in Image_Info wrapped in a smart pointer ##
599
600#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400601 SkBitmap bitmap1, bitmap2;
Cary Clarkbc5697d2017-10-04 14:31:33 -0400602 bitmap1.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
603 SkColorSpace::MakeSRGBLinear()));
604 bitmap2.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
605 bitmap1.refColorSpace()));
606 SkColorSpace* colorSpace = bitmap2.colorSpace();
607 SkDebugf("gammaCloseToSRGB: %s gammaIsLinear: %s isSRGB: %s\n",
608 colorSpace->gammaCloseToSRGB() ? "true" : "false",
609 colorSpace->gammaIsLinear() ? "true" : "false",
610 colorSpace->isSRGB() ? "true" : "false");
611#StdOut
612gammaCloseToSRGB: false gammaIsLinear: true isSRGB: false
613##
614##
615
616#SeeAlso Color_Space SkImageInfo::colorSpace
617
618##
619
620# ------------------------------------------------------------------------------
621
622#Method int bytesPerPixel() const
Cary Clark78de7512018-02-07 07:27:09 -0500623#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500624#Line # returns number of bytes in pixel based on Color_Type ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400625Returns number of bytes per pixel required by Color_Type.
626Returns zero if colorType( is kUnknown_SkColorType.
627
628#Return bytes in pixel ##
629
630#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500631 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
632 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clarkbc5697d2017-10-04 14:31:33 -0400633 SkImageInfo info = SkImageInfo::MakeA8(1, 1);
634 SkBitmap bitmap;
Cary Clark1a8d7622018-03-05 13:26:16 -0500635 for (SkColorType colorType : { #list_of_color_types#
636 } ) {
Cary Clarkbc5697d2017-10-04 14:31:33 -0400637 bitmap.setInfo(info.makeColorType(colorType));
638 SkDebugf("color: k" "%s" "_SkColorType" "%*s" "bytesPerPixel: %d\n",
Cary Clarkab2621d2018-01-30 10:08:57 -0500639 colors[colorType], 13 - strlen(colors[colorType]), " ",
Cary Clarkbc5697d2017-10-04 14:31:33 -0400640 bitmap.bytesPerPixel());
641 }
642#StdOut
Cary Clark1a8d7622018-03-05 13:26:16 -0500643color: kUnknown_SkColorType bytesPerPixel: 0
644color: kAlpha_8_SkColorType bytesPerPixel: 1
645color: kRGB_565_SkColorType bytesPerPixel: 2
646color: kARGB_4444_SkColorType bytesPerPixel: 2
647color: kRGBA_8888_SkColorType bytesPerPixel: 4
648color: kRGB_888x_SkColorType bytesPerPixel: 4
649color: kBGRA_8888_SkColorType bytesPerPixel: 4
650color: kRGBA_1010102_SkColorType bytesPerPixel: 4
651color: kRGB_101010x_SkColorType bytesPerPixel: 4
652color: kGray_8_SkColorType bytesPerPixel: 1
Cary Clarkab2621d2018-01-30 10:08:57 -0500653color: kRGBA_F16_SkColorType bytesPerPixel: 8
Cary Clarkbc5697d2017-10-04 14:31:33 -0400654##
655##
656
657#SeeAlso rowBytes rowBytesAsPixels width shiftPerPixel
658
659##
660
661# ------------------------------------------------------------------------------
662
663#Method int rowBytesAsPixels() const
Cary Clark78de7512018-02-07 07:27:09 -0500664#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500665#Line # returns interval between rows in pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400666Returns number of pixels that fit on row. Should be greater than or equal to
667width().
668
669#Return maximum pixels per row ##
670
671#Example
672 SkBitmap bitmap;
673 for (int rowBytes : { 4, 5, 6, 7, 8} ) {
674 bitmap.setInfo(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), rowBytes);
675 SkDebugf("rowBytes: %d rowBytesAsPixels: %d\n", rowBytes, bitmap.rowBytesAsPixels());
676 }
677#StdOut
678rowBytes: 4 rowBytesAsPixels: 1
679rowBytes: 5 rowBytesAsPixels: 1
680rowBytes: 6 rowBytesAsPixels: 1
681rowBytes: 7 rowBytesAsPixels: 1
682rowBytes: 8 rowBytesAsPixels: 2
683##
684##
685
686#SeeAlso rowBytes shiftPerPixel width bytesPerPixel
687
688##
689
690# ------------------------------------------------------------------------------
691
692#Method int shiftPerPixel() const
Cary Clark78de7512018-02-07 07:27:09 -0500693#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500694#Line # returns bit shift from pixels to bytes ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400695Returns bit shift converting row bytes to row pixels.
696Returns zero for kUnknown_SkColorType.
697
698#Return one of: 0, 1, 2, 3; left shift to convert pixels to bytes ##
699
700#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500701 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
702 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clarkbc5697d2017-10-04 14:31:33 -0400703 SkImageInfo info = SkImageInfo::MakeA8(1, 1);
704 SkBitmap bitmap;
Cary Clark1a8d7622018-03-05 13:26:16 -0500705 for (SkColorType colorType : { #list_of_color_types#
706 } ) {
Cary Clarkbc5697d2017-10-04 14:31:33 -0400707 bitmap.setInfo(info.makeColorType(colorType));
708 SkDebugf("color: k" "%s" "_SkColorType" "%*s" "shiftPerPixel: %d\n",
Cary Clark1a8d7622018-03-05 13:26:16 -0500709 colors[colorType], 14 - strlen(colors[colorType]), " ",
Cary Clarkbc5697d2017-10-04 14:31:33 -0400710 bitmap.shiftPerPixel());
711 }
712#StdOut
Cary Clark1a8d7622018-03-05 13:26:16 -0500713color: kUnknown_SkColorType shiftPerPixel: 0
714color: kAlpha_8_SkColorType shiftPerPixel: 0
715color: kRGB_565_SkColorType shiftPerPixel: 1
716color: kARGB_4444_SkColorType shiftPerPixel: 1
717color: kRGBA_8888_SkColorType shiftPerPixel: 2
718color: kRGB_888x_SkColorType shiftPerPixel: 2
719color: kBGRA_8888_SkColorType shiftPerPixel: 2
720color: kRGBA_1010102_SkColorType shiftPerPixel: 2
721color: kRGB_101010x_SkColorType shiftPerPixel: 2
722color: kGray_8_SkColorType shiftPerPixel: 0
723color: kRGBA_F16_SkColorType shiftPerPixel: 3
Cary Clarkbc5697d2017-10-04 14:31:33 -0400724##
725##
726
727#SeeAlso rowBytes rowBytesAsPixels width bytesPerPixel
728
729##
730
731# ------------------------------------------------------------------------------
732
733#Method bool empty() const
Cary Clark78de7512018-02-07 07:27:09 -0500734#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500735#Line # returns true if Image_Info has zero width() or height() ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400736Returns true if either width() or height() are zero.
737
738Does not check if Pixel_Ref is nullptr; call drawsNothing to check width(),
739height(), and Pixel_Ref.
740
741#Return true if dimensions do not enclose area ##
742
743#Example
744 SkBitmap bitmap;
745 for (int width : { 0, 2 } ) {
746 for (int height : { 0, 2 } ) {
747 bitmap.setInfo(SkImageInfo::MakeA8(width, height));
748 SkDebugf("width: %d height: %d empty: %s\n", width, height,
749 bitmap.empty() ? "true" : "false");
750 }
751 }
752#StdOut
753width: 0 height: 0 empty: true
754width: 0 height: 2 empty: true
755width: 2 height: 0 empty: true
756width: 2 height: 2 empty: false
757##
758##
759
760#SeeAlso height() width() drawsNothing
761
762##
763
764# ------------------------------------------------------------------------------
765
766#Method bool isNull() const
Cary Clark78de7512018-02-07 07:27:09 -0500767#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500768#Line # returns true if Pixel_Ref is nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400769Return true if Pixel_Ref is nullptr.
770
771Does not check if width() or height() are zero; call drawsNothing to check
772width(), height(), and Pixel_Ref.
773
774#Return true if no Pixel_Ref is associated ##
775
776#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400777 SkBitmap bitmap;
778 SkDebugf("empty bitmap does %shave pixels\n", bitmap.isNull() ? "not " : "");
779 bitmap.setInfo(SkImageInfo::MakeA8(8, 8));
780 SkDebugf("bitmap with dimensions does %shave pixels\n", bitmap.isNull() ? "not " : "");
781 bitmap.allocPixels();
782 SkDebugf("allocated bitmap does %shave pixels\n", bitmap.isNull() ? "not " : "");
Cary Clarkbc5697d2017-10-04 14:31:33 -0400783#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400784empty bitmap does not have pixels
785bitmap with dimensions does not have pixels
Cary Clarkbc5697d2017-10-04 14:31:33 -0400786allocated bitmap does have pixels
787##
788##
789
790#SeeAlso empty() drawsNothing pixelRef
791
792##
793
794# ------------------------------------------------------------------------------
795
796#Method bool drawsNothing() const
Cary Clark78de7512018-02-07 07:27:09 -0500797#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500798#Line # returns true if no width(), no height(), or no Pixel_Ref ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400799Return true if width() or height() are zero, or if Pixel_Ref is nullptr.
800If true, Bitmap has no effect when drawn or drawn into.
801
802#Return true if drawing has no effect ##
803
804#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400805 SkBitmap bitmap;
806 for (int w : { 0, 8 } ) {
807 for (bool allocate : { false, true} ) {
808 bitmap.setInfo(SkImageInfo::MakeA8(w, 8));
809 allocate ? bitmap.allocPixels() : (void) 0 ;
810 SkDebugf("empty:%s isNull:%s drawsNothing:%s\n", bitmap.empty() ? "true " : "false",
811 bitmap.isNull() ? "true " : "false", bitmap.drawsNothing() ? "true" : "false");
812 }
813 }
814#StdOut
815empty:true isNull:true drawsNothing:true
816empty:true isNull:false drawsNothing:true
817empty:false isNull:true drawsNothing:true
818empty:false isNull:false drawsNothing:false
819##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400820##
821
822#SeeAlso empty() isNull pixelRef
823
824##
825
826# ------------------------------------------------------------------------------
827
828#Method size_t rowBytes() const
Cary Clark78de7512018-02-07 07:27:09 -0500829#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500830#Line # returns interval between rows in bytes ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400831Returns row bytes, the interval from one pixel row to the next. Row bytes
832is at least as large as
833#Formula
834width() * info().bytesPerPixel()
835##
836.
837
838Returns zero if colorType is kUnknown_SkColorType, or if row bytes supplied to
839setInfo is not large enough to hold a row of pixels.
840
841#Return byte length of pixel row ##
842
843#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400844 SkBitmap bitmap;
845 for (int rowBytes : { 2, 8 } ) {
846 bool result = bitmap.setInfo(SkImageInfo::MakeA8(4, 4), rowBytes);
847 SkDebugf("setInfo returned:%s rowBytes:%d\n", result ? "true " : "false", bitmap.rowBytes());
848 }
849#StdOut
850setInfo returned:false rowBytes:0
851setInfo returned:true rowBytes:8
852##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400853##
854
855#SeeAlso info() setInfo SkImageInfo::minRowBytes
856
857##
858
859# ------------------------------------------------------------------------------
860
861#Method bool setAlphaType(SkAlphaType alphaType)
Cary Clark78de7512018-02-07 07:27:09 -0500862#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -0500863#Line # sets Alpha_Type of shared pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400864Sets Alpha_Type, if alphaType is compatible with Color_Type.
865Returns true unless alphaType is kUnknown_SkAlphaType and current Alpha_Type
866is not kUnknown_SkAlphaType.
867
868Returns true if Color_Type is kUnknown_SkColorType. alphaType is ignored, and
869Alpha_Type remains kUnknown_SkAlphaType.
870
871Returns true if Color_Type is kRGB_565_SkColorType or kGray_8_SkColorType.
872alphaType is ignored, and Alpha_Type remains kOpaque_SkAlphaType.
873
874If Color_Type is kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
875kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: returns true unless
876alphaType is kUnknown_SkAlphaType and Alpha_Type is not kUnknown_SkAlphaType.
877If Alpha_Type is kUnknown_SkAlphaType, alphaType is ignored.
878
879If Color_Type is kAlpha_8_SkColorType, returns true unless
880alphaType is kUnknown_SkAlphaType and Alpha_Type is not kUnknown_SkAlphaType.
881If Alpha_Type is kUnknown_SkAlphaType, alphaType is ignored. If alphaType is
882kUnpremul_SkAlphaType, it is treated as kPremul_SkAlphaType.
883
884This changes Alpha_Type in Pixel_Ref; all bitmaps sharing Pixel_Ref
885are affected.
886
887#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
888 kPremul_SkAlphaType, kUnpremul_SkAlphaType
889##
890
891#Return true if Alpha_Type is set ##
892
893#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400894void draw(SkCanvas* canvas) {
Cary Clarkab2621d2018-01-30 10:08:57 -0500895 const char* colors[] = { "Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
896 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16" };
897 const char* alphas[] = {"Unknown ", "Opaque ", "Premul ", "Unpremul"};
898 SkBitmap bitmap;
899 SkAlphaType alphaTypes[] = { kUnknown_SkAlphaType, kOpaque_SkAlphaType,
900 kPremul_SkAlphaType, kUnpremul_SkAlphaType };
901 SkDebugf("%88s", "Canonical Unknown Opaque Premul Unpremul\n");
Cary Clark1a8d7622018-03-05 13:26:16 -0500902 for (SkColorType colorType : { #list_of_color_types#
903 } ) {
Ben Wagner29380bd2017-10-09 14:43:00 -0400904 for (SkAlphaType canonicalAlphaType : alphaTypes) {
905 SkColorTypeValidateAlphaType(colorType, kUnknown_SkAlphaType, &canonicalAlphaType );
906 SkDebugf("%10s %10s ", colors[(int) colorType], alphas[(int) canonicalAlphaType ]);
907 for (SkAlphaType alphaType : alphaTypes) {
908 bitmap.setInfo(SkImageInfo::Make(4, 4, colorType, canonicalAlphaType));
909 bool result = bitmap.setAlphaType(alphaType);
910 SkDebugf("%s %s ", result ? "true " : "false", alphas[(int) bitmap.alphaType()]);
911 }
912 SkDebugf("\n");
913 }
914 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400915}
916##
917
918#SeeAlso Alpha_Type Color_Type Image_Info setInfo
919
920##
921
922# ------------------------------------------------------------------------------
923
924#Method void* getPixels() const
Cary Clark78de7512018-02-07 07:27:09 -0500925#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500926#Line # returns address of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400927Returns pixel address, the base address corresponding to the pixel origin.
928
929#Return pixel address ##
930
931#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400932 SkBitmap bitmap;
933 bitmap.setInfo(SkImageInfo::MakeN32(4, 4, kPremul_SkAlphaType));
934 bitmap.allocPixels();
935 bitmap.eraseColor(0x00000000);
936 void* baseAddr = bitmap.getPixels();
937 *(SkPMColor*)baseAddr = 0xFFFFFFFF;
938 SkDebugf("bitmap.getColor(0, 1) %c= 0x00000000\n",
939 bitmap.getColor(0, 1) == 0x00000000 ? '=' : '!');
940 SkDebugf("bitmap.getColor(0, 0) %c= 0xFFFFFFFF\n",
941 bitmap.getColor(0, 0) == 0xFFFFFFFF ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -0400942#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400943bitmap.getColor(0, 1) == 0x00000000
Cary Clarkbc5697d2017-10-04 14:31:33 -0400944bitmap.getColor(0, 0) == 0xFFFFFFFF
945##
946##
947
948#SeeAlso isNull drawsNothing
949
950##
951
952# ------------------------------------------------------------------------------
953
954#Method size_t computeByteSize() const
Cary Clark78de7512018-02-07 07:27:09 -0500955#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -0500956#Line # returns size required for pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400957Returns minimum memory required for pixel storage.
958Does not include unused memory on last row when rowBytesAsPixels exceeds width().
959Returns zero if result does not fit in size_t.
960Returns zero if height() or width() is 0.
961Returns height() times rowBytes if colorType is kUnknown_SkColorType.
962
963#Return size in bytes of image buffer ##
964
965#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400966 SkBitmap bitmap;
Cary Clarkbc5697d2017-10-04 14:31:33 -0400967 for (int width : { 1, 1000, 1000000 } ) {
968 for (int height: { 1, 1000, 1000000 } ) {
969 SkImageInfo imageInfo = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
970 bitmap.setInfo(imageInfo, width * 5);
971 SkDebugf("width: %7d height: %7d computeByteSize: %13lld\n", width, height,
972 bitmap.computeByteSize());
973 }
974 }
975#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400976width: 1 height: 1 computeByteSize: 4
977width: 1 height: 1000 computeByteSize: 4999
978width: 1 height: 1000000 computeByteSize: 4999999
979width: 1000 height: 1 computeByteSize: 4000
980width: 1000 height: 1000 computeByteSize: 4999000
981width: 1000 height: 1000000 computeByteSize: 4999999000
982width: 1000000 height: 1 computeByteSize: 4000000
983width: 1000000 height: 1000 computeByteSize: 4999000000
Cary Clarkbc5697d2017-10-04 14:31:33 -0400984width: 1000000 height: 1000000 computeByteSize: 4999999000000
985##
986##
987
988#SeeAlso SkImageInfo::computeByteSize
989
990##
991
992# ------------------------------------------------------------------------------
993
Cary Clarkbc5697d2017-10-04 14:31:33 -0400994#Method bool isImmutable() const
Cary Clark78de7512018-02-07 07:27:09 -0500995#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500996#Line # returns true if pixels will not change ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400997Returns true if pixels can not change.
998
999Most immutable Bitmap checks trigger an assert only on debug builds.
1000
1001#Return true if pixels are immutable ##
1002
1003#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001004 SkBitmap original;
1005 SkImageInfo info = SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1006 if (original.tryAllocPixels(info)) {
1007 original.setImmutable();
1008 SkBitmap copy;
1009 original.extractSubset(&copy, {5, 10, 15, 20});
1010 SkDebugf("original is " "%s" "immutable\n", original.isImmutable() ? "" : "not ");
1011 SkDebugf("copy is " "%s" "immutable\n", copy.isImmutable() ? "" : "not ");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001012 }
1013#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001014original is immutable
Cary Clarkbc5697d2017-10-04 14:31:33 -04001015copy is immutable
1016##
1017##
1018
1019#SeeAlso setImmutable SkPixelRef::isImmutable SkImage
1020
1021##
1022
1023# ------------------------------------------------------------------------------
1024
1025#Method void setImmutable()
Cary Clark78de7512018-02-07 07:27:09 -05001026#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05001027#Line # marks that pixels will not change ##
Cary Clark154beea2017-10-26 07:58:48 -04001028Sets internal flag to mark Bitmap as immutable. Once set, pixels can not change.
1029Any other bitmap sharing the same Pixel_Ref are also marked as immutable.
1030Once Pixel_Ref is marked immutable, the setting cannot be cleared.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001031
1032Writing to immutable Bitmap pixels triggers an assert on debug builds.
1033
1034#Example
1035#Description
1036Triggers assert if SK_DEBUG is true, runs fine otherwise.
1037##
Ben Wagner29380bd2017-10-09 14:43:00 -04001038 SkBitmap bitmap;
1039 bitmap.setInfo(SkImageInfo::MakeN32(4, 4, kPremul_SkAlphaType));
1040 bitmap.allocPixels();
1041 SkCanvas offscreen(bitmap);
1042 SkDebugf("draw white\n");
1043 offscreen.clear(SK_ColorWHITE);
1044 bitmap.setImmutable();
1045 SkDebugf("draw black\n");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001046 offscreen.clear(SK_ColorBLACK);
1047##
1048
1049#SeeAlso isImmutable SkPixelRef::setImmutable SkImage
1050
1051##
1052
1053# ------------------------------------------------------------------------------
1054
1055#Method bool isOpaque() const
Cary Clark78de7512018-02-07 07:27:09 -05001056#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001057#Line # returns true if Image_Info describes opaque pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001058Returns true if Alpha_Type is kOpaque_SkAlphaType.
1059Does not check if Color_Type allows Alpha, or if any pixel value has
1060transparency.
1061
1062#Return true if Image_Info describes opaque Alpha ##
1063
1064#Example
1065#Description
1066 isOpaque ignores whether all pixels are opaque or not.
1067##
Ben Wagner29380bd2017-10-09 14:43:00 -04001068 const int height = 2;
1069 const int width = 2;
1070 SkBitmap bitmap;
1071 bitmap.setInfo(SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType));
1072 for (int index = 0; index < 2; ++index) {
1073 bitmap.allocPixels();
1074 bitmap.eraseColor(0x00000000);
1075 SkDebugf("isOpaque: %s\n", bitmap.isOpaque() ? "true" : "false");
1076 bitmap.eraseColor(0xFFFFFFFF);
1077 SkDebugf("isOpaque: %s\n", bitmap.isOpaque() ? "true" : "false");
1078 bitmap.setInfo(bitmap.info().makeAlphaType(kOpaque_SkAlphaType));
1079 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001080#StdOut
1081isOpaque: false
1082isOpaque: false
1083isOpaque: true
1084isOpaque: true
1085##
1086##
1087
1088#SeeAlso ComputeIsOpaque SkImageInfo::isOpaque
1089
1090##
1091
1092# ------------------------------------------------------------------------------
1093
1094#Method bool isVolatile() const
Cary Clark78de7512018-02-07 07:27:09 -05001095#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001096#Line # returns true if pixels should not be cached ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001097If true, provides a hint to caller that pixels should not
1098be cached. Only true if setIsVolatile has been called to mark as volatile.
1099
1100Volatile state is not shared by other bitmaps sharing the same Pixel_Ref.
1101
1102#Return true if marked volatile ##
1103
1104#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001105 SkBitmap original;
1106 SkImageInfo info = SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1107 if (original.tryAllocPixels(info)) {
1108 original.setIsVolatile(true);
1109 SkBitmap copy;
1110 original.extractSubset(&copy, {5, 10, 15, 20});
1111 SkDebugf("original is " "%s" "volatile\n", original.isVolatile() ? "" : "not ");
1112 SkDebugf("copy is " "%s" "volatile\n", copy.isImmutable() ? "" : "not ");
1113 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001114#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001115original is volatile
Cary Clarkbc5697d2017-10-04 14:31:33 -04001116copy is not volatile
1117##
1118##
1119
1120#SeeAlso setIsVolatile
1121
1122##
1123
1124# ------------------------------------------------------------------------------
1125
1126#Method void setIsVolatile(bool isVolatile)
Cary Clark78de7512018-02-07 07:27:09 -05001127#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05001128#Line # marks if pixels should not be cached ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001129Sets if pixels should be read from Pixel_Ref on every access. Bitmaps are not
1130volatile by default; a GPU back end may upload pixel values expecting them to be
1131accessed repeatedly. Marking temporary Bitmaps as volatile provides a hint to
1132Device that the Bitmap pixels should not be cached. This can
1133improve performance by avoiding overhead and reducing resource
1134consumption on Device.
1135
1136#Param isVolatile true if backing pixels are temporary ##
1137
1138#Example
1139#Height 20
Ben Wagner29380bd2017-10-09 14:43:00 -04001140 SkBitmap bitmap;
1141 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
1142 bitmap.allocPixels();
1143 bitmap.eraseColor(SK_ColorRED);
1144 canvas->scale(16, 16);
1145 canvas->drawBitmap(bitmap, 0, 0);
1146 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorBLUE);
1147 canvas->drawBitmap(bitmap, 2, 0);
1148 bitmap.setIsVolatile(true);
1149 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorGREEN);
1150 canvas->drawBitmap(bitmap, 4, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001151##
1152
1153#SeeAlso isVolatile
1154
1155##
1156
1157# ------------------------------------------------------------------------------
1158
1159#Method void reset()
Cary Clark78de7512018-02-07 07:27:09 -05001160#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -05001161#Line # sets to default values, releases pixel ownership ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001162Resets to its initial state; all fields are set to zero, as if Bitmap had
1163been initialized by SkBitmap().
1164
1165Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to
1166kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType.
1167
1168If Pixel_Ref is allocated, its reference count is decreased by one, releasing
1169its memory if Bitmap is the sole owner.
1170
1171#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001172 SkBitmap bitmap;
1173 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
1174 bitmap.allocPixels();
1175 SkDebugf("width:%d height:%d isNull:%s\n", bitmap.width(), bitmap.height(),
1176 bitmap.isNull() ? "true" : "false");
1177 bitmap.reset();
1178 SkDebugf("width:%d height:%d isNull:%s\n", bitmap.width(), bitmap.height(),
1179 bitmap.isNull() ? "true" : "false");
1180#StdOut
1181width:1 height:1 isNull:false
1182width:0 height:0 isNull:true
1183##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001184##
1185
1186#SeeAlso SkBitmap() SkAlphaType SkColorType
1187
1188##
1189
1190# ------------------------------------------------------------------------------
1191
1192#Method static bool ComputeIsOpaque(const SkBitmap& bm)
Cary Clark78de7512018-02-07 07:27:09 -05001193#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05001194#Line # returns true if all pixels are opaque ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001195Returns true if all pixels are opaque. Color_Type determines how pixels
1196are encoded, and whether pixel describes Alpha. Returns true for Color_Types
1197without alpha in each pixel; for other Color_Types, returns true if all
1198pixels have alpha values equivalent to 1.0 or greater.
1199
1200For Color_Types kRGB_565_SkColorType or kGray_8_SkColorType: always
1201returns true. For Color_Types kAlpha_8_SkColorType, kBGRA_8888_SkColorType,
1202kRGBA_8888_SkColorType: returns true if all pixel Alpha values are 255.
1203For Color_Type kARGB_4444_SkColorType: returns true if all pixel Alpha values are 15.
1204For kRGBA_F16_SkColorType: returns true if all pixel Alpha values are 1.0 or
1205greater.
1206
1207Returns false for kUnknown_SkColorType.
1208
1209#Param bm Bitmap to check ##
1210
1211#Return true if all pixels have opaque values or Color_Type is opaque ##
1212
1213#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001214 SkBitmap bitmap;
1215 bitmap.setInfo(SkImageInfo::Make(2, 2, kN32_SkColorType, kPremul_SkAlphaType));
1216 for (int index = 0; index < 2; ++index) {
1217 bitmap.allocPixels();
1218 bitmap.eraseColor(0x00000000);
1219 SkDebugf("computeIsOpaque: %s\n", SkBitmap::ComputeIsOpaque(bitmap) ? "true" : "false");
1220 bitmap.eraseColor(0xFFFFFFFF);
1221 SkDebugf("computeIsOpaque: %s\n", SkBitmap::ComputeIsOpaque(bitmap) ? "true" : "false");
1222 bitmap.setInfo(bitmap.info().makeAlphaType(kOpaque_SkAlphaType));
Cary Clarkbc5697d2017-10-04 14:31:33 -04001223 }
1224#StdOut
1225computeIsOpaque: false
1226computeIsOpaque: true
1227computeIsOpaque: false
1228computeIsOpaque: true
1229##
1230##
1231
1232#SeeAlso isOpaque Color_Type Alpha
1233
1234##
1235
1236# ------------------------------------------------------------------------------
1237
1238#Method void getBounds(SkRect* bounds) const
Cary Clark78de7512018-02-07 07:27:09 -05001239#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001240#Line # returns width() and height() as Rectangle ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001241Returns Rect { 0, 0, width(), height() }.
1242
1243#Param bounds container for floating point rectangle ##
1244
1245#Example
1246#Height 160
1247#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001248 SkRect bounds;
1249 source.getBounds(&bounds);
1250 bounds.offset(100, 100);
1251 SkPaint paint;
1252 paint.setColor(SK_ColorGRAY);
1253 canvas->scale(.25f, .25f);
1254 canvas->drawRect(bounds, paint);
1255 canvas->drawBitmap(source, 40, 40);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001256##
1257
1258#SeeAlso bounds()
1259
1260##
1261
1262# ------------------------------------------------------------------------------
1263
1264#Method void getBounds(SkIRect* bounds) const
1265
1266Returns IRect { 0, 0, width(), height() }.
1267
1268#Param bounds container for integral rectangle ##
1269
1270#Example
1271#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001272 SkIRect bounds;
1273 source.getBounds(&bounds);
1274 bounds.inset(100, 100);
1275 SkBitmap bitmap;
1276 source.extractSubset(&bitmap, bounds);
1277 canvas->scale(.5f, .5f);
1278 canvas->drawBitmap(bitmap, 10, 10);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001279##
1280
1281#SeeAlso bounds()
1282
1283##
1284
1285# ------------------------------------------------------------------------------
1286
1287#Method SkIRect bounds() const
Cary Clark78de7512018-02-07 07:27:09 -05001288#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001289#Line # returns width() and height() as Rectangle ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001290Returns IRect { 0, 0, width(), height() }.
1291
1292#Return integral rectangle from origin to width() and height() ##
1293
1294#Example
Cary Clark2ade9972017-11-02 17:49:34 -04001295#Height 128
Cary Clarkbc5697d2017-10-04 14:31:33 -04001296#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -05001297 canvas->scale(.5f, .5f);
Ben Wagner29380bd2017-10-09 14:43:00 -04001298 SkIRect bounds = source.bounds();
1299 for (int x : { 0, bounds.width() } ) {
1300 for (int y : { 0, bounds.height() } ) {
1301 canvas->drawBitmap(source, x, y);
1302 }
1303 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001304##
1305
1306#SeeAlso getBounds
1307
1308##
1309
1310# ------------------------------------------------------------------------------
1311
1312#Method SkISize dimensions() const
Cary Clark78de7512018-02-07 07:27:09 -05001313#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001314#Line # returns width() and height() ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001315Returns ISize { width(), height() }.
1316
1317#Return integral size of width() and height() ##
1318
1319#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001320 SkBitmap bitmap;
1321 bitmap.setInfo(SkImageInfo::MakeN32(33, 55, kOpaque_SkAlphaType));
1322 SkISize dimensions = bitmap.dimensions();
1323 SkRect bounds;
1324 bitmap.getBounds(&bounds);
1325 SkRect dimensionsAsBounds = SkRect::Make(dimensions);
1326 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -04001327##
1328
1329#SeeAlso height() width()
1330
1331##
1332
1333# ------------------------------------------------------------------------------
1334
1335#Method SkIRect getSubset() const
Cary Clark78de7512018-02-07 07:27:09 -05001336#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001337#Line # returns bounds offset by origin ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001338Returns the bounds of this bitmap, offset by its Pixel_Ref origin.
1339
1340#Return bounds within Pixel_Ref bounds ##
1341
1342#Example
1343#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001344 SkIRect bounds;
1345 source.getBounds(&bounds);
1346 bounds.inset(100, 100);
1347 SkBitmap subset;
1348 source.extractSubset(&subset, bounds);
1349 SkIRect r = source.getSubset();
1350 SkDebugf("source: %d, %d, %d, %d\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
1351 r = subset.getSubset();
1352 SkDebugf("subset: %d, %d, %d, %d\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
1353#StdOut
1354source: 0, 0, 512, 512
1355subset: 100, 100, 412, 412
1356##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001357##
1358
1359#SeeAlso extractSubset getBounds
1360
1361##
1362
1363# ------------------------------------------------------------------------------
1364
1365#Method bool setInfo(const SkImageInfo& imageInfo, size_t rowBytes = 0)
Cary Clark78de7512018-02-07 07:27:09 -05001366#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05001367#Line # sets height, width, Color_Type, and so on, releasing pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001368Sets width, height, Alpha_Type, Color_Type, Color_Space, and optional
1369rowBytes. Frees pixels, and returns true if successful.
1370
1371imageInfo.alphaType may be altered to a value permitted by imageInfo.colorSpace.
1372If imageInfo.colorType is kUnknown_SkColorType, imageInfo.alphaType is
1373set to kUnknown_SkAlphaType.
1374If imageInfo.colorType is kAlpha_8_SkColorType and imageInfo.alphaType is
1375kUnpremul_SkAlphaType, imageInfo.alphaType is replaced by kPremul_SkAlphaType.
1376If imageInfo.colorType is kRGB_565_SkColorType or kGray_8_SkColorType,
1377imageInfo.alphaType is set to kOpaque_SkAlphaType.
1378If imageInfo.colorType is kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
1379kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: imageInfo.alphaType remains
1380unchanged.
1381
1382rowBytes must equal or exceed imageInfo.minRowBytes. If imageInfo.colorSpace is
1383kUnknown_SkColorType, rowBytes is ignored and treated as zero; for all other
1384Color_Space values, rowBytes of zero is treated as imageInfo.minRowBytes.
1385
1386Calls reset() and returns false if:
1387#List
1388# rowBytes exceeds 31 bits ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001389# imageInfo.width() is negative ##
1390# imageInfo.height() is negative ##
1391# rowBytes is positive and less than imageInfo.width() times imageInfo.bytesPerPixel ##
1392##
1393
1394#Param imageInfo contains width, height, Alpha_Type, Color_Type, Color_Space ##
1395#Param rowBytes imageInfo.minRowBytes or larger; or zero ##
1396
1397#Return true if Image_Info set successfully ##
1398
1399#Example
1400#Height 96
1401###^
Ben Wagner29380bd2017-10-09 14:43:00 -04001402SkBitmap bitmap;
1403bitmap.setInfo(SkImageInfo::MakeN32(44, 16, kOpaque_SkAlphaType));
1404bitmap.allocPixels();
1405bitmap.eraseColor(SK_ColorGREEN);
1406SkCanvas offscreen(bitmap);
1407SkPaint paint;
1408offscreen.drawString("!@#$%", 0, 12, paint);
1409canvas->scale(6, 6);
1410canvas->drawBitmap(bitmap, 0, 0);
1411^^^#
Cary Clarkbc5697d2017-10-04 14:31:33 -04001412##
1413
1414#SeeAlso Alpha_Type Color_Type Color_Space height rowBytes width
1415
1416##
1417
1418# ------------------------------------------------------------------------------
1419
1420#Enum AllocFlags
Cary Clark4855f782018-02-06 09:41:53 -05001421#Line # zero pixel memory ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001422#Code
1423 enum AllocFlags {
1424 kZeroPixels_AllocFlag = 1 << 0,
1425 };
1426##
1427
1428AllocFlags provides the option to zero pixel memory when allocated.
1429
1430#Const kZeroPixels_AllocFlag 1
1431 Instructs tryAllocPixelsFlags and allocPixelsFlags to zero pixel memory.
1432##
1433
1434#NoExample
1435##
1436
1437#SeeAlso tryAllocPixelsFlags allocPixelsFlags erase() eraseColor
1438
1439##
1440
1441# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001442#Subtopic Allocate
1443#Populate
1444#Line # allocates storage for pixels ##
1445##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001446
1447#Method bool SK_WARN_UNUSED_RESULT tryAllocPixelsFlags(const SkImageInfo& info, uint32_t flags)
Cary Clark78de7512018-02-07 07:27:09 -05001448#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001449#Line # allocates pixels from Image_Info with options if possible ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001450Sets Image_Info to info following the rules in setInfo and allocates pixel
1451memory. If flags is kZeroPixels_AllocFlag, memory is zeroed.
1452
1453Returns false and calls reset() if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001454not be allocated, or memory could not optionally be zeroed.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001455
1456On most platforms, allocating pixel memory may succeed even though there is
1457not sufficient memory to hold pixels; allocation does not take place
1458until the pixels are written to. The actual behavior depends on the platform
1459implementation of malloc(), if flags is zero, and calloc(), if flags is
1460kZeroPixels_AllocFlag.
1461
1462Passing kZeroPixels_AllocFlag is usually faster than separately calling
1463eraseColor(SK_ColorTRANSPARENT).
1464
1465#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1466#Param flags kZeroPixels_AllocFlag, or zero ##
1467
1468#Return true if pixels allocation is successful ##
1469
1470#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001471 SkBitmap bitmap;
Cary Clarka560c472017-11-27 10:44:06 -05001472 if (!bitmap.tryAllocPixelsFlags(SkImageInfo::MakeN32(10000, 10000, kOpaque_SkAlphaType),
1473 SkBitmap::kZeroPixels_AllocFlag)) {
1474 SkDebugf("bitmap allocation failed!\n");
1475 } else {
1476 SkDebugf("bitmap allocation succeeded!\n");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001477 }
1478#StdOut
Cary Clarka560c472017-11-27 10:44:06 -05001479bitmap allocation succeeded!
Cary Clarkbc5697d2017-10-04 14:31:33 -04001480##
1481##
1482
1483#SeeAlso allocPixelsFlags tryAllocPixels SkMallocPixelRef::MakeZeroed
1484
1485##
1486
1487# ------------------------------------------------------------------------------
1488
1489#Method void allocPixelsFlags(const SkImageInfo& info, uint32_t flags)
Cary Clark78de7512018-02-07 07:27:09 -05001490#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001491#Line # allocates pixels from Image_Info with options, or aborts ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001492Sets Image_Info to info following the rules in setInfo and allocates pixel
1493memory. If flags is kZeroPixels_AllocFlag, memory is zeroed.
1494
1495Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001496not be allocated, or memory could not optionally
Cary Clarkbc5697d2017-10-04 14:31:33 -04001497be zeroed. Abort steps may be provided by the user at compile time by defining
1498SK_ABORT.
1499
1500On most platforms, allocating pixel memory may succeed even though there is
1501not sufficient memory to hold pixels; allocation does not take place
1502until the pixels are written to. The actual behavior depends on the platform
1503implementation of malloc(), if flags is zero, and calloc(), if flags is
1504kZeroPixels_AllocFlag.
1505
1506Passing kZeroPixels_AllocFlag is usually faster than separately calling
1507eraseColor(SK_ColorTRANSPARENT).
1508
1509#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1510#Param flags kZeroPixels_AllocFlag, or zero ##
1511
1512#Example
1513#Height 128
1514#Description
1515Text is drawn on a transparent background; drawing the bitmap a second time
1516lets the first draw show through.
1517##
1518###^
Ben Wagner29380bd2017-10-09 14:43:00 -04001519SkBitmap bitmap;
1520bitmap.allocPixelsFlags(SkImageInfo::MakeN32(44, 16, kPremul_SkAlphaType),
1521 SkBitmap::kZeroPixels_AllocFlag);
1522SkCanvas offscreen(bitmap);
1523SkPaint paint;
1524offscreen.drawString("!@#$%", 0, 12, paint);
1525canvas->scale(6, 6);
1526canvas->drawBitmap(bitmap, 0, 0);
1527canvas->drawBitmap(bitmap, 8, 8);
1528^^^#
Cary Clarkbc5697d2017-10-04 14:31:33 -04001529##
1530
1531#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeZeroed
1532
1533##
1534
1535# ------------------------------------------------------------------------------
1536
1537#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info, size_t rowBytes)
Cary Clark78de7512018-02-07 07:27:09 -05001538#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001539#Line # allocates pixels from Image_Info if possible ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001540#ToDo am I ever conflicted about setInfo rules. It needs to be able to be replicated
1541 if, for instance, I generate one-page-per-method HTML-style documentation
1542 I'm not so sure it makes sense to put the indirection in for .h either unless
1543 my mantra is that .h should abbreviate full documentation. And, what to do
1544 for generated markdown? At least there the rules are a click away, although
1545 a pop-down in place would be way better. Hmmm.
1546##
1547
1548Sets Image_Info to info following the rules in setInfo and allocates pixel
1549memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(),
1550or equal zero. Pass in zero for rowBytes to compute the minimum valid value.
1551
1552Returns false and calls reset() if Image_Info could not be set, or memory could
1553not be allocated.
1554
1555On most platforms, allocating pixel memory may succeed even though there is
1556not sufficient memory to hold pixels; allocation does not take place
1557until the pixels are written to. The actual behavior depends on the platform
1558implementation of malloc().
1559
1560#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1561#Param rowBytes size of pixel row or larger; may be zero ##
1562
1563#Return true if pixel storage is allocated ##
1564
1565#Example
1566#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001567SkBitmap bitmap;
1568SkImageInfo info = SkImageInfo::Make(64, 256, kGray_8_SkColorType, kOpaque_SkAlphaType);
1569if (bitmap.tryAllocPixels(info, 0)) {
1570 SkCanvas offscreen(bitmap);
1571 offscreen.scale(.5f, .5f);
1572 for (int x : { 0, 64, 128, 192 } ) {
1573 offscreen.drawBitmap(source, -x, 0);
1574 canvas->drawBitmap(bitmap, x, 0);
1575 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001576}
1577##
1578
1579#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeAllocate
1580
1581##
1582
1583# ------------------------------------------------------------------------------
1584
1585#Method void allocPixels(const SkImageInfo& info, size_t rowBytes)
Cary Clark78de7512018-02-07 07:27:09 -05001586#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001587#Line # allocates pixels from Image_Info, or aborts ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001588Sets Image_Info to info following the rules in setInfo and allocates pixel
1589memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(),
1590or equal zero. Pass in zero for rowBytes to compute the minimum valid value.
1591
1592Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001593not be allocated. Abort steps may be provided by
Cary Clarkbc5697d2017-10-04 14:31:33 -04001594the user at compile time by defining SK_ABORT.
1595
1596On most platforms, allocating pixel memory may succeed even though there is
1597not sufficient memory to hold pixels; allocation does not take place
1598until the pixels are written to. The actual behavior depends on the platform
1599implementation of malloc().
1600
1601#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1602#Param rowBytes size of pixel row or larger; may be zero ##
1603
1604#Example
1605#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001606SkBitmap bitmap;
1607SkImageInfo info = SkImageInfo::Make(256, 64, kGray_8_SkColorType, kOpaque_SkAlphaType);
1608bitmap.allocPixels(info, info.width() * info.bytesPerPixel() + 64);
1609SkCanvas offscreen(bitmap);
1610offscreen.scale(.5f, .5f);
1611for (int y : { 0, 64, 128, 192 } ) {
1612 offscreen.drawBitmap(source, 0, -y);
1613 canvas->drawBitmap(bitmap, 0, y);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001614}
1615##
1616
1617#SeeAlso tryAllocPixels allocPixelsFlags SkMallocPixelRef::MakeAllocate
1618
1619##
1620
1621# ------------------------------------------------------------------------------
1622
1623#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info)
1624
1625Sets Image_Info to info following the rules in setInfo and allocates pixel
1626memory.
1627
1628Returns false and calls reset() if Image_Info could not be set, or memory could
1629not be allocated.
1630
1631On most platforms, allocating pixel memory may succeed even though there is
1632not sufficient memory to hold pixels; allocation does not take place
1633until the pixels are written to. The actual behavior depends on the platform
1634implementation of malloc().
1635
1636#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1637
1638#Return true if pixel storage is allocated ##
1639
1640#Example
1641#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001642SkBitmap bitmap;
1643if (bitmap.tryAllocPixels(SkImageInfo::Make(64, 64, kGray_8_SkColorType, kOpaque_SkAlphaType))) {
1644 SkCanvas offscreen(bitmap);
1645 offscreen.scale(.25f, .5f);
1646 for (int y : { 0, 64, 128, 192 } ) {
1647 offscreen.drawBitmap(source, -y, -y);
1648 canvas->drawBitmap(bitmap, y, y);
1649 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001650}
1651##
1652
1653#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeAllocate
1654
1655##
1656
1657# ------------------------------------------------------------------------------
1658
1659#Method void allocPixels(const SkImageInfo& info)
1660
1661Sets Image_Info to info following the rules in setInfo and allocates pixel
1662memory.
1663
1664Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001665not be allocated. Abort steps may be provided by
Cary Clarkbc5697d2017-10-04 14:31:33 -04001666the user at compile time by defining SK_ABORT.
1667
1668On most platforms, allocating pixel memory may succeed even though there is
1669not sufficient memory to hold pixels; allocation does not take place
1670until the pixels are written to. The actual behavior depends on the platform
1671implementation of malloc().
1672
1673#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1674
1675#Example
1676#Image 4
Ben Wagner29380bd2017-10-09 14:43:00 -04001677SkBitmap bitmap;
1678bitmap.allocPixels(SkImageInfo::Make(64, 64, kGray_8_SkColorType, kOpaque_SkAlphaType));
1679SkCanvas offscreen(bitmap);
1680offscreen.scale(.5f, .5f);
1681for (int y : { 0, 64, 128, 192 } ) {
1682 offscreen.drawBitmap(source, -y, -y);
1683 canvas->drawBitmap(bitmap, y, y);
1684}
Cary Clarkbc5697d2017-10-04 14:31:33 -04001685##
1686
1687#SeeAlso tryAllocPixels allocPixelsFlags SkMallocPixelRef::MakeAllocate
1688
1689##
1690
1691# ------------------------------------------------------------------------------
1692
1693#Method bool SK_WARN_UNUSED_RESULT tryAllocN32Pixels(int width, int height, bool isOpaque = false)
Cary Clark78de7512018-02-07 07:27:09 -05001694#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001695#Line # allocates compatible Color_ARGB pixels if possible ##
Cary Clarka560c472017-11-27 10:44:06 -05001696Sets Image_Info to width, height, and Native_Color_Type; and allocates
Cary Clarkbc5697d2017-10-04 14:31:33 -04001697pixel memory. If isOpaque is true, sets Image_Info to kOpaque_SkAlphaType;
1698otherwise, sets to kPremul_SkAlphaType.
1699
1700Calls reset() and returns false if width exceeds 29 bits or is negative,
1701or height is negative.
1702
1703Returns false if allocation fails.
1704
Cary Clarka560c472017-11-27 10:44:06 -05001705Use to create Bitmap that matches SkPMColor, the native pixel arrangement on
1706the platform. Bitmap drawn to output device skips converting its pixel format.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001707
1708#Param width pixel column count; must be zero or greater ##
1709#Param height pixel row count; must be zero or greater ##
1710#Param isOpaque true if pixels do not have transparency ##
1711
1712#Return true if pixel storage is allocated ##
1713
1714#Example
1715#Height 160
Ben Wagner29380bd2017-10-09 14:43:00 -04001716 SkBitmap bitmap;
1717 if (bitmap.tryAllocN32Pixels(80, 80)) {
1718 bitmap.eraseColor(SK_ColorTRANSPARENT);
1719 bitmap.erase(0x7f3f7fff, SkIRect::MakeWH(50, 30));
1720 bitmap.erase(0x3f7fff3f, SkIRect::MakeXYWH(20, 10, 50, 30));
1721 bitmap.erase(0x5fff3f7f, SkIRect::MakeXYWH(40, 20, 50, 30));
1722 canvas->drawBitmap(bitmap, 0, 0);
1723 for (int x : { 0, 30, 60, 90 } ) {
1724 canvas->drawBitmap(bitmap, x, 70);
1725 }
1726 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001727##
1728
1729#SeeAlso tryAllocPixels allocN32Pixels SkMallocPixelRef::MakeAllocate
1730
1731##
1732
1733# ------------------------------------------------------------------------------
1734
1735#Method void allocN32Pixels(int width, int height, bool isOpaque = false)
Cary Clark78de7512018-02-07 07:27:09 -05001736#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001737#Line # allocates compatible Color_ARGB pixels, or aborts ##
Cary Clarka560c472017-11-27 10:44:06 -05001738Sets Image_Info to width, height, and the Native_Color_Type; and allocates
Cary Clarkbc5697d2017-10-04 14:31:33 -04001739pixel memory. If isOpaque is true, sets Image_Info to kPremul_SkAlphaType;
1740otherwise, sets to kOpaque_SkAlphaType.
1741
1742Aborts if width exceeds 29 bits or is negative, or height is negative, or
1743allocation fails. Abort steps may be provided by the user at compile time by
1744defining SK_ABORT.
1745
Cary Clarka560c472017-11-27 10:44:06 -05001746Use to create Bitmap that matches SkPMColor, the native pixel arrangement on
1747the platform. Bitmap drawn to output device skips converting its pixel format.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001748
1749#Param width pixel column count; must be zero or greater ##
1750#Param height pixel row count; must be zero or greater ##
1751#Param isOpaque true if pixels do not have transparency ##
1752
1753#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001754 SkRandom random;
1755 SkBitmap bitmap;
1756 bitmap.allocN32Pixels(64, 64);
1757 bitmap.eraseColor(SK_ColorTRANSPARENT);
1758 for (int y = 0; y < 256; y += 64) {
1759 for (int x = 0; x < 256; x += 64) {
1760 SkColor color = random.nextU();
1761 uint32_t w = random.nextRangeU(4, 32);
1762 uint32_t cx = random.nextRangeU(0, 64 - w);
1763 uint32_t h = random.nextRangeU(4, 32);
1764 uint32_t cy = random.nextRangeU(0, 64 - h);
1765 bitmap.erase(color, SkIRect::MakeXYWH(cx, cy, w, h));
1766 canvas->drawBitmap(bitmap, x, y);
1767 }
1768 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001769##
1770
1771#SeeAlso allocPixels tryAllocN32Pixels SkMallocPixelRef::MakeAllocate
1772
1773##
1774
1775# ------------------------------------------------------------------------------
1776
1777#Method bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
1778 void (*releaseProc)(void* addr, void* context), void* context)
Cary Clark78de7512018-02-07 07:27:09 -05001779#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001780#Line # creates Pixel_Ref, with optional release function ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001781
1782Sets Image_Info to info following the rules in setInfo, and creates Pixel_Ref
1783containing pixels and rowBytes. releaseProc, if not nullptr, is called
1784immediately on failure or when pixels are no longer referenced. context may be
1785nullptr.
1786
1787If Image_Info could not be set, or rowBytes is less than info.minRowBytes:
1788calls releaseProc if present, calls reset(), and returns false.
1789
1790Otherwise, if pixels equals nullptr: sets Image_Info, calls releaseProc if
1791present, returns true.
1792
1793If Image_Info is set, pixels is not nullptr, and releaseProc is not nullptr:
1794when pixels are no longer referenced, calls releaseProc with pixels and context
1795as parameters.
1796
1797#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1798#Param pixels address or pixel storage; may be nullptr ##
1799#Param rowBytes size of pixel row or larger ##
1800#Param releaseProc function called when pixels can be deleted; may be nullptr ##
1801#Param context caller state passed to releaseProc; may be nullptr ##
1802
1803#Return true if Image_Info is set to info ##
1804
1805#Example
1806#Description
1807releaseProc is called immediately because rowBytes is too small for Pixel_Ref.
1808##
1809#Function
Ben Wagner29380bd2017-10-09 14:43:00 -04001810static void releaseProc(void* addr, void* ) {
1811 SkDebugf("releaseProc called\n");
1812 delete[] (uint32_t*) addr;
1813}
1814
1815##
1816
1817void draw(SkCanvas* canvas) {
1818 SkBitmap bitmap;
1819 void* pixels = new uint32_t[8 * 8];
1820 SkImageInfo info = SkImageInfo::MakeN32(8, 8, kOpaque_SkAlphaType);
1821 SkDebugf("before installPixels\n");
1822 bool installed = bitmap.installPixels(info, pixels, 16, releaseProc, nullptr);
1823 SkDebugf("install " "%s" "successful\n", installed ? "" : "not ");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001824}
1825#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001826before installPixels
1827releaseProc called
Cary Clarkbc5697d2017-10-04 14:31:33 -04001828install not successful
1829##
1830##
1831
1832#SeeAlso allocPixels
1833
1834##
1835
1836# ------------------------------------------------------------------------------
1837
1838#Method bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes)
1839
1840Sets Image_Info to info following the rules in setInfo, and creates Pixel_Ref
1841containing pixels and rowBytes.
1842
1843If Image_Info could not be set, or rowBytes is less than info.minRowBytes:
1844calls reset(), and returns false.
1845
1846Otherwise, if pixels equals nullptr: sets Image_Info, returns true.
1847
1848Caller must ensure that pixels are valid for the lifetime of Bitmap and Pixel_Ref.
1849
1850#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1851#Param pixels address or pixel storage; may be nullptr ##
1852#Param rowBytes size of pixel row or larger ##
1853
1854#Return true if Image_Info is set to info ##
1855
1856#Example
Cary Clark4855f782018-02-06 09:41:53 -05001857#Bug 7079
Cary Clarkbc5697d2017-10-04 14:31:33 -04001858#Description
Cary Clarkbc5697d2017-10-04 14:31:33 -04001859GPU does not support kUnpremul_SkAlphaType, does not assert that it does not.
1860##
Ben Wagner29380bd2017-10-09 14:43:00 -04001861void draw(SkCanvas* canvas) {
1862 SkRandom random;
1863 SkBitmap bitmap;
1864 const int width = 8;
1865 const int height = 8;
1866 uint32_t pixels[width * height];
1867 for (unsigned x = 0; x < width * height; ++x) {
1868 pixels[x] = random.nextU();
1869 }
1870 SkImageInfo info = SkImageInfo::MakeN32(width, height, kUnpremul_SkAlphaType);
1871 if (bitmap.installPixels(info, pixels, info.minRowBytes())) {
1872 canvas->scale(32, 32);
1873 canvas->drawBitmap(bitmap, 0, 0);
1874 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001875}
1876##
1877
1878#SeeAlso allocPixels
1879
1880##
1881
1882# ------------------------------------------------------------------------------
1883
1884#Method bool installPixels(const SkPixmap& pixmap)
1885
1886Sets Image_Info to pixmap.info() following the rules in setInfo, and creates
1887Pixel_Ref containing pixmap.addr() and pixmap.rowBytes.
1888
1889If Image_Info could not be set, or pixmap.rowBytes is less than
1890SkImageInfo::minRowBytes: calls reset(), and returns false.
1891
1892Otherwise, if pixmap.addr() equals nullptr: sets Image_Info, returns true.
1893
1894Caller must ensure that pixmap is valid for the lifetime of Bitmap and Pixel_Ref.
1895
1896#Param pixmap Image_Info, pixel address, and rowBytes ##
1897
1898#Return true if Image_Info was set to pixmap.info() ##
1899
1900#Example
1901#Description
1902Draw a five by five bitmap, and draw it again with a center white pixel.
1903##
1904#Height 64
1905 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
1906 { 0xAC, 0xA8, 0x89, 0x47, 0x87 },
1907 { 0x4B, 0x25, 0x25, 0x25, 0x46 },
1908 { 0x90, 0x81, 0x25, 0x41, 0x33 },
1909 { 0x75, 0x55, 0x44, 0x20, 0x00 }};
1910 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
1911 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
1912 SkBitmap bitmap;
1913 bitmap.installPixels(pixmap);
1914 canvas->scale(10, 10);
1915 canvas->drawBitmap(bitmap, 0, 0);
1916 *pixmap.writable_addr8(2, 2) = 0xFF;
1917 bitmap.installPixels(pixmap);
1918 canvas->drawBitmap(bitmap, 10, 0);
1919##
1920
1921#SeeAlso allocPixels
1922
1923##
1924
1925# ------------------------------------------------------------------------------
1926
1927#Method bool installMaskPixels(const SkMask& mask)
Cary Clark4855f782018-02-06 09:41:53 -05001928#Deprecated soon
Cary Clarkbc5697d2017-10-04 14:31:33 -04001929##
1930
1931# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001932#Subtopic Pixels
1933#Populate
1934#Line # read and write pixel values ##
1935##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001936
1937#Method void setPixels(void* pixels)
Cary Clark78de7512018-02-07 07:27:09 -05001938#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05001939#Line # sets Pixel_Ref without an offset ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001940Replaces Pixel_Ref with pixels, preserving Image_Info and rowBytes.
1941Sets Pixel_Ref origin to (0, 0).
1942
1943If pixels is nullptr, or if info().colorType equals kUnknown_SkColorType;
1944release reference to Pixel_Ref, and set Pixel_Ref to nullptr.
1945
1946Caller is responsible for handling ownership pixel memory for the lifetime
1947of Bitmap and Pixel_Ref.
1948
1949#Param pixels address of pixel storage, managed by caller ##
1950
1951#Example
1952#Height 50
Ben Wagner29380bd2017-10-09 14:43:00 -04001953 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
1954 uint8_t set2[5] = { 0xAC, 0xA8, 0x89, 0x47, 0x87 };
1955 SkBitmap bitmap;
1956 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
1957 canvas->scale(10, 50);
1958 canvas->drawBitmap(bitmap, 0, 0);
1959 bitmap.setPixels(set2);
1960 canvas->drawBitmap(bitmap, 10, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001961##
1962
1963#SeeAlso installPixels allocPixels
1964
1965##
1966
1967# ------------------------------------------------------------------------------
1968
1969#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels()
Cary Clark78de7512018-02-07 07:27:09 -05001970#In Allocate
Cary Clarkbc5697d2017-10-04 14:31:33 -04001971Allocates pixel memory with HeapAllocator, and replaces existing Pixel_Ref.
1972The allocation size is determined by Image_Info width, height, and Color_Type.
1973
Cary Clarka560c472017-11-27 10:44:06 -05001974Returns false if info().colorType is kUnknown_SkColorType, or allocation fails.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001975
1976#Return true if the allocation succeeds
1977##
1978
1979#Example
1980#Height 50
1981#Description
1982Bitmap hosts and draws gray values in set1. tryAllocPixels replaces Pixel_Ref
1983and erases it to black, but does not alter set1. setPixels replaces black
1984Pixel_Ref with set1.
1985##
Ben Wagner29380bd2017-10-09 14:43:00 -04001986 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
1987 SkBitmap bitmap;
1988 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
1989 canvas->scale(10, 50);
1990 canvas->drawBitmap(bitmap, 0, 0);
1991 if (bitmap.tryAllocPixels()) {
1992 bitmap.eraseColor(SK_ColorBLACK);
1993 canvas->drawBitmap(bitmap, 8, 0);
1994 bitmap.setPixels(set1);
1995 canvas->drawBitmap(bitmap, 16, 0);
1996 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001997##
1998
1999#SeeAlso allocPixels installPixels setPixels
2000
2001##
2002
2003# ------------------------------------------------------------------------------
2004
2005#Method void allocPixels()
Cary Clark78de7512018-02-07 07:27:09 -05002006#In Allocate
Cary Clarkbc5697d2017-10-04 14:31:33 -04002007Allocates pixel memory with HeapAllocator, and replaces existing Pixel_Ref.
2008The allocation size is determined by Image_Info width, height, and Color_Type.
2009
Cary Clarka560c472017-11-27 10:44:06 -05002010Aborts if info().colorType is kUnknown_SkColorType, or allocation fails.
2011Abort steps may be provided by the user at compile
Cary Clarkbc5697d2017-10-04 14:31:33 -04002012time by defining SK_ABORT.
2013
2014#Example
2015#Height 50
2016#Description
2017Bitmap hosts and draws gray values in set1. allocPixels replaces Pixel_Ref
2018and erases it to black, but does not alter set1. setPixels replaces black
2019Pixel_Ref with set2.
2020##
Ben Wagner29380bd2017-10-09 14:43:00 -04002021 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
2022 uint8_t set2[5] = { 0xAC, 0xA8, 0x89, 0x47, 0x87 };
2023 SkBitmap bitmap;
2024 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
2025 canvas->scale(10, 50);
2026 canvas->drawBitmap(bitmap, 0, 0);
2027 bitmap.allocPixels();
2028 bitmap.eraseColor(SK_ColorBLACK);
2029 canvas->drawBitmap(bitmap, 8, 0);
2030 bitmap.setPixels(set2);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002031 canvas->drawBitmap(bitmap, 16, 0);
2032##
2033
2034#SeeAlso tryAllocPixels installPixels setPixels
2035
2036##
2037
2038# ------------------------------------------------------------------------------
2039
2040#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(Allocator* allocator)
2041
2042Allocates pixel memory with allocator, and replaces existing Pixel_Ref.
2043The allocation size is determined by Image_Info width, height, and Color_Type.
2044If allocator is nullptr, use HeapAllocator instead.
2045
Cary Clark78de7512018-02-07 07:27:09 -05002046Returns false if Allocator::allocPixelRef return false.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002047
2048#Param allocator instance of SkBitmap::Allocator instantiation ##
2049
2050#Return true if custom allocator reports success
2051##
2052
2053#Example
2054#Height 100
2055#Description
2056HeapAllocator limits the maximum size of Bitmap to two gigabytes. Using
2057a custom allocator, this limitation may be relaxed. This example can be
2058modified to allocate an eight gigabyte Bitmap on a 64 bit platform with
2059sufficient memory.
2060##
2061#Function
2062class LargePixelRef : public SkPixelRef {
2063public:
2064 LargePixelRef(const SkImageInfo& info, char* storage, size_t rowBytes)
2065 : SkPixelRef(info.width(), info.height(), storage, rowBytes) {
2066 }
2067
2068 ~LargePixelRef() override {
2069 delete[] (char* ) this->pixels();
2070 }
2071};
2072
2073class LargeAllocator : public SkBitmap::Allocator {
2074public:
2075 bool allocPixelRef(SkBitmap* bitmap) override {
2076 const SkImageInfo& info = bitmap->info();
2077 uint64_t rowBytes = info.minRowBytes64();
2078 uint64_t size = info.height() * rowBytes;
2079 char* addr = new char[size];
2080 if (nullptr == addr) {
2081 return false;
2082 }
2083 sk_sp<SkPixelRef> pr = sk_sp<SkPixelRef>(new LargePixelRef(info, addr, rowBytes));
2084 if (!pr) {
2085 return false;
2086 }
2087 bitmap->setPixelRef(std::move(pr), 0, 0);
2088 return true;
2089 }
2090};
2091
2092##
2093
2094void draw(SkCanvas* canvas) {
2095 LargeAllocator largeAllocator;
2096 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002097 int width = 100; // make this 20000
2098 int height = 100; // and this 100000 to allocate 8 gigs on a 64-bit platform
2099 bitmap.setInfo(SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType));
2100 if (bitmap.tryAllocPixels(&largeAllocator)) {
2101 bitmap.eraseColor(0xff55aa33);
2102 canvas->drawBitmap(bitmap, 0, 0);
2103 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002104}
2105
2106##
2107
2108#SeeAlso allocPixels Allocator Pixel_Ref
2109
2110##
2111
2112# ------------------------------------------------------------------------------
2113
2114#Method void allocPixels(Allocator* allocator)
2115
2116Allocates pixel memory with allocator, and replaces existing Pixel_Ref.
2117The allocation size is determined by Image_Info width, height, and Color_Type.
2118If allocator is nullptr, use HeapAllocator instead.
2119
Cary Clark78de7512018-02-07 07:27:09 -05002120Aborts if Allocator::allocPixelRef return false. Abort steps may be provided by
Cary Clarkbc5697d2017-10-04 14:31:33 -04002121the user at compile time by defining SK_ABORT.
2122
2123#Param allocator instance of SkBitmap::Allocator instantiation ##
2124
2125#Example
2126#Height 32
2127#Function
2128class TinyAllocator : public SkBitmap::Allocator {
2129public:
2130 bool allocPixelRef(SkBitmap* bitmap) override {
2131 const SkImageInfo& info = bitmap->info();
2132 if (info.height() * info.minRowBytes() > sizeof(storage)) {
2133 return false;
2134 }
2135 sk_sp<SkPixelRef> pr = sk_sp<SkPixelRef>(
2136 new SkPixelRef(info.width(), info.height(), storage, info.minRowBytes()));
2137 bitmap->setPixelRef(std::move(pr), 0, 0);
2138 return true;
2139 }
2140
2141 char storage[16];
2142};
2143
2144##
2145
2146void draw(SkCanvas* canvas) {
2147 TinyAllocator tinyAllocator;
2148 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002149 bitmap.setInfo(SkImageInfo::MakeN32(2, 2, kOpaque_SkAlphaType));
2150 if (bitmap.tryAllocPixels(&tinyAllocator)) {
2151 bitmap.eraseColor(0xff55aa33);
2152 bitmap.erase(0xffaa3355, SkIRect::MakeXYWH(1, 1, 1, 1));
2153 canvas->scale(16, 16);
2154 canvas->drawBitmap(bitmap, 0, 0);
2155 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002156}
2157##
2158
2159#SeeAlso allocPixels Allocator Pixel_Ref
2160
2161##
2162
2163# ------------------------------------------------------------------------------
2164
2165#Method SkPixelRef* pixelRef() const
Cary Clark78de7512018-02-07 07:27:09 -05002166#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002167#Line # returns Pixel_Ref, or nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002168Returns Pixel_Ref, which contains: pixel base address; its dimensions; and
2169rowBytes, the interval from one row to the next. Does not change Pixel_Ref
2170reference count. Pixel_Ref may be shared by multiple bitmaps.
2171If Pixel_Ref has not been set, returns nullptr.
2172
2173#Return Pixel_Ref, or nullptr ##
2174
2175#Example
2176#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002177 SkBitmap subset;
2178 source.extractSubset(&subset, SkIRect::MakeXYWH(32, 64, 128, 256));
2179 SkDebugf("src ref %c= sub ref\n", source.pixelRef() == subset.pixelRef() ? '=' : '!');
2180 SkDebugf("src pixels %c= sub pixels\n", source.getPixels() == subset.getPixels() ? '=' : '!');
2181 SkDebugf("src addr %c= sub addr\n", source.getAddr(32, 64) == subset.getAddr(0, 0) ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -04002182##
2183
2184#SeeAlso getPixels getAddr
2185
2186##
2187
2188# ------------------------------------------------------------------------------
2189
2190#Method SkIPoint pixelRefOrigin() const
Cary Clark78de7512018-02-07 07:27:09 -05002191#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002192#Line # returns offset within Pixel_Ref ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002193Returns origin of pixels within Pixel_Ref. Bitmap bounds is always contained
2194by Pixel_Ref bounds, which may be the same size or larger. Multiple Bitmaps
2195can share the same Pixel_Ref, where each Bitmap has different bounds.
2196
2197The returned origin added to Bitmap dimensions equals or is smaller than the
2198Pixel_Ref dimensions.
2199
2200Returns (0, 0) if Pixel_Ref is nullptr.
2201
2202#Return pixel origin within Pixel_Ref ##
2203
2204#Example
2205#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002206 SkBitmap subset;
2207 source.extractSubset(&subset, SkIRect::MakeXYWH(32, 64, 128, 256));
2208 SkIPoint sourceOrigin = source.pixelRefOrigin();
2209 SkIPoint subsetOrigin = subset.pixelRefOrigin();
2210 SkDebugf("source origin: %d, %d\n", sourceOrigin.fX, sourceOrigin.fY);
2211 SkDebugf("subset origin: %d, %d\n", subsetOrigin.fX, subsetOrigin.fY);
2212#StdOut
2213source origin: 0, 0
2214subset origin: 32, 64
2215##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002216##
2217
Cary Clark2ade9972017-11-02 17:49:34 -04002218#SeeAlso SkPixelRef getSubset setPixelRef
Cary Clarkbc5697d2017-10-04 14:31:33 -04002219
2220##
2221
2222# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05002223#Subtopic Set
2224#Line # updates values and attributes ##
2225#Populate
2226##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002227
2228#Method void setPixelRef(sk_sp<SkPixelRef> pixelRef, int dx, int dy)
Cary Clark78de7512018-02-07 07:27:09 -05002229#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05002230#Line # sets Pixel_Ref and offset ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002231Replaces pixelRef and origin in Bitmap. dx and dy specify the offset
2232within the Pixel_Ref pixels for the top-left corner of the bitmap.
2233
2234Asserts in debug builds if dx or dy are out of range. Pins dx and dy
2235to legal range in release builds.
2236
2237The caller is responsible for ensuring that the pixels match the
2238Color_Type and Alpha_Type in Image_Info.
2239
2240#Param pixelRef Pixel_Ref describing pixel address and rowBytes ##
2241#Param dx column offset in Pixel_Ref for bitmap origin ##
2242#Param dy row offset in Pixel_Ref for bitmap origin ##
2243
2244#Example
2245#Height 140
2246#Image 5
2247#Description
2248Treating 32 bit data as 8 bit data is unlikely to produce useful results.
2249##
Ben Wagner29380bd2017-10-09 14:43:00 -04002250 SkBitmap bitmap;
2251 bitmap.setInfo(SkImageInfo::Make(source.width() - 5, source.height() - 5,
2252 kGray_8_SkColorType, kOpaque_SkAlphaType), source.rowBytes());
2253 bitmap.setPixelRef(sk_ref_sp(source.pixelRef()), 5, 5);
2254 canvas->drawBitmap(bitmap, 10, 10);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002255##
2256
2257#SeeAlso setInfo
2258
2259##
2260
2261# ------------------------------------------------------------------------------
2262
2263#Method bool readyToDraw() const
Cary Clark78de7512018-02-07 07:27:09 -05002264#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05002265#Line # returns true if address of pixels is not nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002266Returns true if Bitmap is can be drawn.
2267
2268#Return true if getPixels() is not nullptr ##
2269
2270#Example
2271#Image 5
2272#Height 160
Ben Wagner29380bd2017-10-09 14:43:00 -04002273 if (source.readyToDraw()) {
2274 canvas->drawBitmap(source, 10, 10);
2275 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002276##
2277
2278#SeeAlso getPixels drawsNothing
2279
2280##
2281
2282# ------------------------------------------------------------------------------
2283
2284#Method uint32_t getGenerationID() const
Cary Clark78de7512018-02-07 07:27:09 -05002285#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05002286#Line # returns unique ID ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002287Returns a unique value corresponding to the pixels in Pixel_Ref.
2288Returns a different value after notifyPixelsChanged has been called.
2289Returns zero if Pixel_Ref is nullptr.
2290
2291Determines if pixels have changed since last examined.
2292
2293#Return unique value for pixels in Pixel_Ref ##
2294
2295#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04002296 SkBitmap bitmap;
2297 SkDebugf("empty id %u\n", bitmap.getGenerationID());
2298 bitmap.allocPixels(SkImageInfo::MakeN32(64, 64, kOpaque_SkAlphaType));
2299 SkDebugf("alloc id %u\n", bitmap.getGenerationID());
2300 bitmap.eraseColor(SK_ColorRED);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002301 SkDebugf("erase id %u\n", bitmap.getGenerationID());
2302#StdOut
2303#Volatile
Ben Wagner29380bd2017-10-09 14:43:00 -04002304empty id 0
2305alloc id 4
Cary Clarkbc5697d2017-10-04 14:31:33 -04002306erase id 6
2307##
2308##
2309
2310#SeeAlso notifyPixelsChanged Pixel_Ref
2311
2312##
2313
2314# ------------------------------------------------------------------------------
2315
2316#Method void notifyPixelsChanged() const
Cary Clark78de7512018-02-07 07:27:09 -05002317#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05002318#Line # marks pixels as changed, altering the unique ID ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002319Marks that pixels in Pixel_Ref have changed. Subsequent calls to
2320getGenerationID() return a different value.
2321
2322#Example
2323#Height 20
Ben Wagner29380bd2017-10-09 14:43:00 -04002324 SkBitmap bitmap;
2325 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
2326 bitmap.allocPixels();
2327 bitmap.eraseColor(SK_ColorRED);
2328 canvas->scale(16, 16);
2329 canvas->drawBitmap(bitmap, 0, 0);
2330 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorBLUE);
2331 canvas->drawBitmap(bitmap, 2, 0);
2332 bitmap.notifyPixelsChanged();
2333 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorGREEN);
2334 canvas->drawBitmap(bitmap, 4, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002335##
2336
2337#SeeAlso getGenerationID isVolatile Pixel_Ref
2338
2339##
2340
2341# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05002342#Subtopic Draw
2343#Populate
2344#Line # set pixels to Color ##
2345##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002346
2347#Method void eraseColor(SkColor c) const
Cary Clark78de7512018-02-07 07:27:09 -05002348#In Draw
Cary Clarkab2621d2018-01-30 10:08:57 -05002349#Line # writes Color to pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002350Replaces pixel values with c. All pixels contained by bounds() are affected.
2351If the colorType is kGray_8_SkColorType or k565_SkColorType, then Color_Alpha
2352is ignored; Color_RGB is treated as opaque. If colorType is kAlpha_8_SkColorType,
2353then Color_RGB is ignored.
2354
2355#Param c Unpremultiplied Color ##
2356
2357#Example
2358#Height 20
Ben Wagner29380bd2017-10-09 14:43:00 -04002359 SkBitmap bitmap;
2360 bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kOpaque_SkAlphaType));
2361 bitmap.eraseColor(SK_ColorRED);
2362 canvas->scale(16, 16);
2363 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002364##
2365
Cary Clark154beea2017-10-26 07:58:48 -04002366#SeeAlso eraseARGB erase
Cary Clarkbc5697d2017-10-04 14:31:33 -04002367
2368##
2369
2370# ------------------------------------------------------------------------------
2371
2372#Method void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const
Cary Clark78de7512018-02-07 07:27:09 -05002373#In Draw
Cary Clarkab2621d2018-01-30 10:08:57 -05002374#Line # writes Color to pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002375Replaces pixel values with Unpremultiplied Color built from a, r, g, and b.
2376All pixels contained by bounds() are affected.
2377If the colorType is kGray_8_SkColorType or k565_SkColorType, then a
2378is ignored; r, g, and b are treated as opaque. If colorType is kAlpha_8_SkColorType,
2379then r, g, and b are ignored.
2380
2381#Param a amount of Color_Alpha, from fully transparent (0) to fully opaque (255) ##
2382#Param r amount of Color_RGB_Red, from no red (0) to full red (255) ##
2383#Param g amount of Color_RGB_Green, from no green (0) to full green (255) ##
2384#Param b amount of Color_RGB_Blue, from no blue (0) to full blue (255) ##
2385
2386#Example
2387#Height 80
Ben Wagner29380bd2017-10-09 14:43:00 -04002388 SkBitmap bitmap;
2389 bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType));
2390 bitmap.eraseARGB(0x7f, 0xff, 0x7f, 0x3f);
2391 canvas->scale(50, 50);
2392 canvas->drawBitmap(bitmap, 0, 0);
2393 canvas->drawBitmap(bitmap, .5f, .5f);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002394##
2395
Cary Clark154beea2017-10-26 07:58:48 -04002396#SeeAlso eraseColor erase
Cary Clarkbc5697d2017-10-04 14:31:33 -04002397
2398##
2399
2400# ------------------------------------------------------------------------------
2401
2402#Method void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const
Cary Clark78de7512018-02-07 07:27:09 -05002403#In Draw
Cary Clarkab2621d2018-01-30 10:08:57 -05002404#Line # deprecated ##
Cary Clark154beea2017-10-26 07:58:48 -04002405Deprecated. Use eraseARGB or eraseColor.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002406
Cary Clark154beea2017-10-26 07:58:48 -04002407#Param r amount of red ##
2408#Param g amount of green ##
2409#Param b amount of blue ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002410
Cary Clark154beea2017-10-26 07:58:48 -04002411#NoExample
Cary Clarkbc5697d2017-10-04 14:31:33 -04002412##
2413
2414#SeeAlso eraseColor eraseARGB erase
2415
2416##
2417
2418# ------------------------------------------------------------------------------
2419
2420#Method void erase(SkColor c, const SkIRect& area) const
Cary Clark78de7512018-02-07 07:27:09 -05002421#In Draw
Cary Clarkab2621d2018-01-30 10:08:57 -05002422#Line # writes Color to rectangle of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002423Replaces pixel values inside area with c. If area does not intersect bounds(),
2424call has no effect.
2425
2426If the colorType is kGray_8_SkColorType or k565_SkColorType, then Color_Alpha
2427is ignored; Color_RGB is treated as opaque. If colorType is kAlpha_8_SkColorType,
2428then Color_RGB is ignored.
2429
2430#Param c Unpremultiplied Color ##
2431#Param area rectangle to fill ##
2432
2433#Example
2434#Height 70
Ben Wagner29380bd2017-10-09 14:43:00 -04002435 SkBitmap bitmap;
2436 bitmap.allocPixels(SkImageInfo::MakeN32(2, 2, kPremul_SkAlphaType));
2437 bitmap.erase(0x7fff7f3f, SkIRect::MakeWH(1, 1));
2438 bitmap.erase(0x7f7f3fff, SkIRect::MakeXYWH(0, 1, 1, 1));
2439 bitmap.erase(0x7f3fff7f, SkIRect::MakeXYWH(1, 0, 1, 1));
2440 bitmap.erase(0x7f1fbf5f, SkIRect::MakeXYWH(1, 1, 1, 1));
2441 canvas->scale(25, 25);
2442 canvas->drawBitmap(bitmap, 0, 0);
2443 canvas->drawBitmap(bitmap, .5f, .5f);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002444
2445##
2446
2447#SeeAlso eraseColor eraseARGB eraseRGB SkCanvas::drawRect
2448
2449##
2450
2451# ------------------------------------------------------------------------------
2452
2453#Method void eraseArea(const SkIRect& area, SkColor c) const
Cary Clarkbc5697d2017-10-04 14:31:33 -04002454#Deprecated
2455##
2456
Cary Clarkbc5697d2017-10-04 14:31:33 -04002457# ------------------------------------------------------------------------------
2458
2459#Method SkColor getColor(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002460#In Property
2461#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05002462#Line # returns one pixel as Unpremultiplied Color ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002463Returns pixel at (x, y) as Unpremultiplied Color.
2464Returns black with Alpha if Color_Type is kAlpha_8_SkColorType.
2465
2466Input is not validated: out of bounds values of x or y trigger an assert() if
2467built with SK_DEBUG defined; and returns undefined values or may crash if
2468SK_RELEASE is defined. Fails if Color_Type is kUnknown_SkColorType or
2469pixel address is nullptr.
2470
2471Color_Space in Image_Info is ignored. Some Color precision may be lost in the
2472conversion to Unpremultiplied Color; original pixel data may have additional
2473precision.
2474
2475#Param x column index, zero or greater, and less than width() ##
2476#Param y row index, zero or greater, and less than height() ##
2477
2478#Return pixel converted to Unpremultiplied Color ##
2479
2480#Example
2481 const int w = 4;
2482 const int h = 4;
2483 SkColor colors[][w] = {
2484 0x00000000, 0x2a0e002a, 0x55380055, 0x7f7f007f,
2485 0x2a000e2a, 0x551c1c55, 0x7f542a7f, 0xaaaa38aa,
2486 0x55003855, 0x7f2a547f, 0xaa7171aa, 0xd4d48dd4,
2487 0x7f007f7f, 0xaa38aaaa, 0xd48dd4d4, 0xffffffff,
2488 };
2489 SkDebugf("Premultiplied:\n");
2490 for (int y = 0; y < h; ++y) {
2491 SkDebugf("(0, %d) ", y);
2492 for (int x = 0; x < w; ++x) {
2493 SkDebugf("0x%08x%c", colors[y][x], x == w - 1 ? '\n' : ' ');
2494 }
2495 }
2496 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), colors, w * 4);
2497 SkBitmap bitmap;
2498 bitmap.installPixels(pixmap);
2499 SkDebugf("Unpremultiplied:\n");
2500 for (int y = 0; y < h; ++y) {
2501 SkDebugf("(0, %d) ", y);
2502 for (int x = 0; x < w; ++x) {
2503 SkDebugf("0x%08x%c", bitmap.getColor(x, y), x == w - 1 ? '\n' : ' ');
2504 }
2505 }
2506#StdOut
2507Premultiplied:
2508(0, 0) 0x00000000 0x2a0e002a 0x55380055 0x7f7f007f
2509(0, 1) 0x2a000e2a 0x551c1c55 0x7f542a7f 0xaaaa38aa
2510(0, 2) 0x55003855 0x7f2a547f 0xaa7171aa 0xd4d48dd4
2511(0, 3) 0x7f007f7f 0xaa38aaaa 0xd48dd4d4 0xffffffff
2512Unpremultiplied:
2513(0, 0) 0x00000000 0x2a5500ff 0x55a800ff 0x7fff00ff
2514(0, 1) 0x2a0055ff 0x555454ff 0x7fa954ff 0xaaff54ff
2515(0, 2) 0x5500a8ff 0x7f54a9ff 0xaaaaaaff 0xd4ffaaff
2516(0, 3) 0x7f00ffff 0xaa54ffff 0xd4aaffff 0xffffffff
2517##
2518##
2519
2520#SeeAlso getAddr readPixels
2521
2522##
2523
2524# ------------------------------------------------------------------------------
2525
2526#Method void* getAddr(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002527#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002528#Line # returns readable pixel address as void pointer ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002529Returns pixel address at (x, y).
2530
2531Input is not validated: out of bounds values of x or y, or kUnknown_SkColorType,
2532trigger an assert() if built with SK_DEBUG defined. Returns nullptr if
2533Color_Type is kUnknown_SkColorType, or Pixel_Ref is nullptr.
2534
2535Performs a lookup of pixel size; for better performance, call
2536one of: getAddr8, getAddr16, or getAddr32.
2537
2538#Param x column index, zero or greater, and less than width() ##
2539#Param y row index, zero or greater, and less than height() ##
2540
2541#Return generic pointer to pixel ##
2542
2543#Example
2544#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002545 char* row0 = (char* ) source.getAddr(0, 0);
2546 char* row1 = (char* ) source.getAddr(0, 1);
2547 SkDebugf("addr interval %c= rowBytes\n", row1 - row0 == source.rowBytes() ? '=' : '!');
2548#StdOut
2549addr interval == rowBytes
2550##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002551##
2552
2553#SeeAlso getAddr8 getAddr16 getAddr32 readPixels SkPixmap::addr
2554
2555##
2556
2557# ------------------------------------------------------------------------------
2558
2559#Method inline uint32_t* getAddr32(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002560#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002561#Line # returns readable pixel address as 32-bit pointer ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002562Returns address at (x, y).
2563
2564Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2565#List
2566# Pixel_Ref is nullptr ##
2567# bytesPerPixel() is not four ##
2568# x is negative, or not less than width() ##
2569# y is negative, or not less than height() ##
2570##
2571
2572#Param x column index, zero or greater, and less than width() ##
2573#Param y row index, zero or greater, and less than height() ##
2574
2575#Return unsigned 32-bit pointer to pixel at (x, y) ##
2576
2577#Example
2578#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002579 uint32_t* row0 = source.getAddr32(0, 0);
2580 uint32_t* row1 = source.getAddr32(0, 1);
2581 size_t interval = (row1 - row0) * source.bytesPerPixel();
2582 SkDebugf("addr interval %c= rowBytes\n", interval == source.rowBytes() ? '=' : '!');
2583#StdOut
2584addr interval == rowBytes
2585##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002586##
2587
2588#SeeAlso getAddr8 getAddr16 getAddr readPixels SkPixmap::addr32
2589
2590##
2591
2592# ------------------------------------------------------------------------------
2593
2594#Method inline uint16_t* getAddr16(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002595#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002596#Line # returns readable pixel address as 16-bit pointer ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002597Returns address at (x, y).
2598
2599Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2600#List
2601# Pixel_Ref is nullptr ##
2602# bytesPerPixel() is not two ##
2603# x is negative, or not less than width() ##
2604# y is negative, or not less than height() ##
2605##
2606
2607#Param x column index, zero or greater, and less than width() ##
2608#Param y row index, zero or greater, and less than height() ##
2609
2610#Return unsigned 16-bit pointer to pixel at (x, y)##
2611
2612#Example
2613#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002614 SkBitmap bitmap16;
2615 SkImageInfo dstInfo = SkImageInfo::Make(source.width(), source.height(), kARGB_4444_SkColorType,
2616 kPremul_SkAlphaType);
2617 bitmap16.allocPixels(dstInfo);
2618 if (source.readPixels(dstInfo, bitmap16.getPixels(), bitmap16.rowBytes(), 0, 0)) {
2619 uint16_t* row0 = bitmap16.getAddr16(0, 0);
2620 uint16_t* row1 = bitmap16.getAddr16(0, 1);
2621 size_t interval = (row1 - row0) * bitmap16.bytesPerPixel();
2622 SkDebugf("addr interval %c= rowBytes\n", interval == bitmap16.rowBytes() ? '=' : '!');
2623 }
2624#StdOut
2625addr interval == rowBytes
2626##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002627##
2628
2629#SeeAlso getAddr8 getAddr getAddr32 readPixels SkPixmap::addr16
2630
2631##
2632
2633# ------------------------------------------------------------------------------
2634
2635#Method inline uint8_t* getAddr8(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002636#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002637#Line # returns readable pixel address as 8-bit pointer ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002638Returns address at (x, y).
2639
2640Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2641#List
2642# Pixel_Ref is nullptr ##
2643# bytesPerPixel() is not one ##
2644# x is negative, or not less than width() ##
2645# y is negative, or not less than height() ##
2646##
2647
2648#Param x column index, zero or greater, and less than width() ##
2649#Param y row index, zero or greater, and less than height() ##
2650
2651#Return unsigned 8-bit pointer to pixel at (x, y) ##
2652
2653#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04002654 SkBitmap bitmap;
2655 const int width = 8;
2656 const int height = 8;
2657 uint8_t pixels[height][width];
2658 SkImageInfo info = SkImageInfo::Make(width, height, kGray_8_SkColorType, kOpaque_SkAlphaType);
2659 if (bitmap.installPixels(info, pixels, info.minRowBytes())) {
2660 SkDebugf("&pixels[4][2] %c= bitmap.getAddr8(2, 4)\n",
2661 &pixels[4][2] == bitmap.getAddr8(2, 4) ? '=' : '!');
2662 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002663#StdOut
2664&pixels[4][2] == bitmap.getAddr8(2, 4)
2665##
2666##
2667
2668#SeeAlso getAddr getAddr16 getAddr32 readPixels SkPixmap::addr8
2669
2670##
2671
2672# ------------------------------------------------------------------------------
2673
2674#Method bool extractSubset(SkBitmap* dst, const SkIRect& subset) const
Cary Clark78de7512018-02-07 07:27:09 -05002675#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -05002676#Line # creates Bitmap, sharing pixels if possible ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002677Shares Pixel_Ref with dst. Pixels are not copied; Bitmap and dst point
2678to the same pixels; dst bounds() are set to the intersection of subset
2679and the original bounds().
2680
2681subset may be larger than bounds(). Any area outside of bounds() is ignored.
2682
2683Any contents of dst are discarded. isVolatile setting is copied to dst.
2684dst is set to colorType, alphaType, and colorSpace.
2685
2686Return false if:
2687#List
2688# dst is nullptr ##
2689# Pixel_Ref is nullptr ##
2690# subset does not intersect bounds() ##
2691##
2692
2693
2694#Param dst Bitmap set to subset ##
2695#Param subset rectangle of pixels to reference ##
2696
2697#Return true if dst is replaced by subset
2698##
2699
2700#Example
2701#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002702 SkIRect bounds, s;
2703 source.getBounds(&bounds);
2704 SkDebugf("bounds: %d, %d, %d, %d\n", bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
2705 SkBitmap subset;
2706 for (int left: { -100, 0, 100, 1000 } ) {
2707 for (int right: { 0, 100, 1000 } ) {
2708 SkIRect b = SkIRect::MakeLTRB(left, 100, right, 200);
2709 bool success = source.extractSubset(&subset, b);
2710 SkDebugf("subset: %4d, %4d, %4d, %4d ", b.fLeft, b.fTop, b.fRight, b.fBottom);
2711 SkDebugf("success; %s", success ? "true" : "false");
2712 if (success) {
2713 subset.getBounds(&s);
2714 SkDebugf(" subset: %d, %d, %d, %d", s.fLeft, s.fTop, s.fRight, s.fBottom);
2715 }
2716 SkDebugf("\n");
2717 }
2718 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002719#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04002720bounds: 0, 0, 512, 512
2721subset: -100, 100, 0, 200 success; false
2722subset: -100, 100, 100, 200 success; true subset: 0, 0, 100, 100
2723subset: -100, 100, 1000, 200 success; true subset: 0, 0, 512, 100
2724subset: 0, 100, 0, 200 success; false
2725subset: 0, 100, 100, 200 success; true subset: 0, 0, 100, 100
2726subset: 0, 100, 1000, 200 success; true subset: 0, 0, 512, 100
2727subset: 100, 100, 0, 200 success; false
2728subset: 100, 100, 100, 200 success; false
2729subset: 100, 100, 1000, 200 success; true subset: 0, 0, 412, 100
2730subset: 1000, 100, 0, 200 success; false
2731subset: 1000, 100, 100, 200 success; false
Cary Clarkbc5697d2017-10-04 14:31:33 -04002732subset: 1000, 100, 1000, 200 success; false
2733##
2734##
2735
2736#SeeAlso readPixels writePixels SkCanvas::drawBitmap
2737
2738##
2739
2740# ------------------------------------------------------------------------------
2741
2742#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
2743 int srcX, int srcY, SkTransferFunctionBehavior behavior) const
Cary Clark78de7512018-02-07 07:27:09 -05002744#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05002745#Line # copies and converts pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002746
Cary Clarkac47b882018-01-11 10:35:44 -05002747Copies Rect of pixels from Bitmap pixels to dstPixels. Copy starts at (srcX, srcY),
2748and does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002749
2750dstInfo specifies width, height, Color_Type, Alpha_Type, and
2751Color_Space of destination. dstRowBytes specifics the gap from one destination
2752row to the next. Returns true if pixels are copied. Returns false if:
2753#List
2754# dstInfo.addr() equals nullptr ##
2755# dstRowBytes is less than dstInfo.minRowBytes ##
2756# Pixel_Ref is nullptr ##
2757##
2758
Cary Clarkac47b882018-01-11 10:35:44 -05002759Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002760kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002761If Bitmap colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
2762If Bitmap alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
2763match. If Bitmap colorSpace is nullptr, dstInfo.colorSpace must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002764false if pixel conversion is not possible.
2765
2766srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04002767false if width() or height() is zero or negative.
2768Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04002769#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002770abs(srcX) >= Bitmap width()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002771##
2772, or if
2773#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002774abs(srcY) >= Bitmap height()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002775##
2776.
2777
2778If behavior is SkTransferFunctionBehavior::kRespect: converts source
2779pixels to a linear space before converting to dstInfo.
2780If behavior is SkTransferFunctionBehavior::kIgnore: source
2781pixels are treated as if they are linear, regardless of how they are encoded.
2782
2783#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
2784#Param dstPixels destination pixel storage ##
2785#Param dstRowBytes destination row length ##
2786#Param srcX column index whose absolute value is less than width() ##
2787#Param srcY row index whose absolute value is less than height() ##
2788#Param behavior one of: SkTransferFunctionBehavior::kRespect,
2789 SkTransferFunctionBehavior::kIgnore
2790##
2791
2792#Return true if pixels are copied to dstPixels ##
2793
2794#Example
2795#Height 64
2796void draw(SkCanvas* canvas) {
Ben Wagner29380bd2017-10-09 14:43:00 -04002797 const int width = 256;
2798 const int height = 32;
2799 std::vector<int32_t> dstPixels;
2800 dstPixels.resize(height * width * 4);
2801 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
2802 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
2803 SkPoint gradPoints[] = { { 0, 0 }, { width, 0 } };
2804 SkPaint gradPaint;
2805 gradPaint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
2806 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
2807 for (auto behavior : { SkTransferFunctionBehavior::kRespect,
2808 SkTransferFunctionBehavior::kIgnore} ) {
2809 SkBitmap bitmap;
2810 bitmap.allocPixels(info);
2811 SkCanvas srcCanvas(bitmap);
2812 srcCanvas.drawRect(SkRect::MakeWH(width, height), gradPaint);
2813 if (bitmap.readPixels(info, &dstPixels.front(), width * 4, 0, 0, behavior)) {
2814 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
2815 bitmap.installPixels(dstPixmap);
2816 canvas->drawBitmap(bitmap, 0, 0);
2817 }
2818 canvas->translate(0, height);
2819 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002820}
2821##
2822
2823#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2824
2825##
2826
2827# ------------------------------------------------------------------------------
2828
2829#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
2830 int srcX, int srcY) const
2831
Cary Clarkac47b882018-01-11 10:35:44 -05002832Copies a Rect of pixels from Bitmap to dstPixels. Copy starts at (srcX, srcY),
2833and does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002834
Cary Clarkac47b882018-01-11 10:35:44 -05002835dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
2836destination. dstRowBytes specifics the gap from one destination row to the next.
2837Returns true if pixels are copied. Returns false if:
Cary Clarkbc5697d2017-10-04 14:31:33 -04002838#List
2839# dstInfo.addr() equals nullptr ##
2840# dstRowBytes is less than dstInfo.minRowBytes ##
2841# Pixel_Ref is nullptr ##
2842##
2843
Cary Clarkac47b882018-01-11 10:35:44 -05002844Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002845kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002846If Bitmap colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
2847If Bitmap alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
2848match. If Bitmap colorSpace is nullptr, dstInfo.colorSpace must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002849false if pixel conversion is not possible.
2850
2851srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04002852false if width() or height() is zero or negative.
2853Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04002854#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002855abs(srcX) >= Bitmap width()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002856##
2857, or if
2858#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002859abs(srcY) >= Bitmap height()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002860##
2861.
2862
2863#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
2864#Param dstPixels destination pixel storage ##
2865#Param dstRowBytes destination row length ##
2866#Param srcX column index whose absolute value is less than width() ##
2867#Param srcY row index whose absolute value is less than height() ##
2868
2869#Return true if pixels are copied to dstPixels ##
2870
2871#Example
2872#Height 128
2873#Description
2874Transferring the gradient from 8 bits per component to 4 bits per component
2875creates visible banding.
2876##
Ben Wagner29380bd2017-10-09 14:43:00 -04002877 const int width = 256;
2878 const int height = 64;
2879 SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(width, height);
2880 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
2881 SkPoint gradPoints[] = { { 0, 0 }, { 256, 0 } };
2882 SkPaint paint;
2883 paint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
2884 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
2885 SkBitmap bitmap;
2886 bitmap.allocPixels(srcInfo);
2887 SkCanvas srcCanvas(bitmap);
2888 srcCanvas.drawRect(SkRect::MakeWH(width, height), paint);
2889 canvas->drawBitmap(bitmap, 0, 0);
2890 SkImageInfo dstInfo = srcInfo.makeColorType(kARGB_4444_SkColorType);
2891 std::vector<int16_t> dstPixels;
2892 dstPixels.resize(height * width);
2893 bitmap.readPixels(dstInfo, &dstPixels.front(), width * 2, 0, 0);
2894 SkPixmap dstPixmap(dstInfo, &dstPixels.front(), width * 2);
2895 bitmap.installPixels(dstPixmap);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002896 canvas->drawBitmap(bitmap, 0, 64);
2897##
2898
2899#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2900
2901##
2902
2903# ------------------------------------------------------------------------------
2904
2905#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY) const
2906
Cary Clarkac47b882018-01-11 10:35:44 -05002907Copies a Rect of pixels from Bitmap to dst. Copy starts at (srcX, srcY), and
2908does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002909
2910dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2911and row bytes of destination. dst.rowBytes specifics the gap from one destination
2912row to the next. Returns true if pixels are copied. Returns false if:
2913#List
2914# dst pixel storage equals nullptr ##
2915# dst.rowBytes is less than SkImageInfo::minRowBytes ##
2916# Pixel_Ref is nullptr ##
2917##
2918
Cary Clarkac47b882018-01-11 10:35:44 -05002919Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002920kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002921If Bitmap colorType is kGray_8_SkColorType, dst Color_Space must match.
2922If Bitmap alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
2923match. If Bitmap colorSpace is nullptr, dst Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002924false if pixel conversion is not possible.
2925
2926srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04002927false if width() or height() is zero or negative.
2928Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04002929#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002930abs(srcX) >= Bitmap width()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002931##
2932, or if
2933#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002934abs(srcY) >= Bitmap height()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002935##
2936.
2937
2938#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
2939#Param srcX column index whose absolute value is less than width() ##
2940#Param srcY row index whose absolute value is less than height() ##
2941
2942#Return true if pixels are copied to dst ##
2943
2944#Example
2945#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002946 std::vector<int32_t> srcPixels;
2947 srcPixels.resize(source.height() * source.rowBytes());
2948 for (int y = 0; y < 4; ++y) {
2949 for (int x = 0; x < 4; ++x) {
2950 SkPixmap pixmap(SkImageInfo::MakeN32Premul(source.width() / 4, source.height() / 4),
2951 &srcPixels.front() + x * source.height() * source.width() / 4 +
2952 y * source.width() / 4, source.rowBytes());
2953 source.readPixels(pixmap, x * source.width() / 4, y * source.height() / 4);
2954 }
2955 }
2956 canvas->scale(.5f, .5f);
2957 SkBitmap bitmap;
2958 bitmap.installPixels(SkImageInfo::MakeN32Premul(source.width(), source.height()),
2959 &srcPixels.front(), source.rowBytes());
Cary Clarkbc5697d2017-10-04 14:31:33 -04002960 canvas->drawBitmap(bitmap, 0, 0);
2961##
2962
2963#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2964
2965##
2966
2967# ------------------------------------------------------------------------------
2968
2969#Method bool readPixels(const SkPixmap& dst) const
2970
Cary Clarkac47b882018-01-11 10:35:44 -05002971Copies a Rect of pixels from Bitmap to dst. Copy starts at (0, 0), and
2972does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002973
2974dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2975and row bytes of destination. dst.rowBytes specifics the gap from one destination
2976row to the next. Returns true if pixels are copied. Returns false if:
2977#List
2978# dst pixel storage equals nullptr ##
2979# dst.rowBytes is less than SkImageInfo::minRowBytes ##
2980# Pixel_Ref is nullptr ##
2981##
2982
Cary Clarkac47b882018-01-11 10:35:44 -05002983Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002984kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002985If Bitmap colorType is kGray_8_SkColorType, dst Color_Space must match.
2986If Bitmap alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
2987match. If Bitmap colorSpace is nullptr, dst Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002988false if pixel conversion is not possible.
2989
2990#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
2991
2992#Return true if pixels are copied to dst ##
2993
2994#Example
2995#Height 128
2996#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002997 std::vector<int32_t> srcPixels;
2998 srcPixels.resize(source.height() * source.width() * 8);
2999 for (int i = 0; i < 2; ++i) {
3000 SkPixmap pixmap(SkImageInfo::Make(source.width() * 2, source.height(),
3001 i ? kRGBA_8888_SkColorType : kBGRA_8888_SkColorType, kPremul_SkAlphaType),
3002 &srcPixels.front() + i * source.width(), source.rowBytes() * 2);
3003 source.readPixels(pixmap);
3004 }
3005 canvas->scale(.25f, .25f);
3006 SkBitmap bitmap;
3007 bitmap.installPixels(SkImageInfo::MakeN32Premul(source.width() * 2, source.height()),
3008 &srcPixels.front(), source.rowBytes() * 2);
3009 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003010##
3011
3012#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
3013
3014##
3015
3016# ------------------------------------------------------------------------------
3017
3018#Method bool writePixels(const SkPixmap& src, int dstX, int dstY)
Cary Clark78de7512018-02-07 07:27:09 -05003019#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05003020#Line # copies and converts pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003021Copies a Rect of pixels from src. Copy starts at (dstX, dstY), and does not exceed
3022(src.width(), src.height()).
3023
3024src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
3025and row bytes of source. src.rowBytes specifics the gap from one source
3026row to the next. Returns true if pixels are copied. Returns false if:
3027#List
3028# src pixel storage equals nullptr ##
3029# src.rowBytes is less than SkImageInfo::minRowBytes ##
3030# Pixel_Ref is nullptr ##
3031##
3032
Cary Clarkac47b882018-01-11 10:35:44 -05003033Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04003034kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05003035If Bitmap colorType is kGray_8_SkColorType, src Color_Space must match.
3036If Bitmap alphaType is kOpaque_SkAlphaType, src Alpha_Type must
3037match. If Bitmap colorSpace is nullptr, src Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04003038false if pixel conversion is not possible.
3039
3040dstX and dstY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04003041false if width() or height() is zero or negative.
3042Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04003043#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05003044abs(dstX) >= Bitmap width()
Cary Clarkbc5697d2017-10-04 14:31:33 -04003045##
3046, or if
3047#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05003048abs(dstY) >= Bitmap height()
Cary Clarkbc5697d2017-10-04 14:31:33 -04003049##
3050.
3051
3052#Param src source Pixmap: Image_Info, pixels, row bytes ##
3053#Param dstX column index whose absolute value is less than width() ##
3054#Param dstY row index whose absolute value is less than height() ##
3055
3056#Return true if src pixels are copied to Bitmap ##
3057
3058#Example
3059#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04003060 std::vector<int32_t> srcPixels;
3061 int width = image->width();
3062 int height = image->height();
3063 srcPixels.resize(height * width * 4);
3064 SkPixmap pixmap(SkImageInfo::MakeN32Premul(width, height), (const void*) &srcPixels.front(),
3065 width * 4);
3066 image->readPixels(pixmap, 0, 0);
3067 canvas->scale(.5f, .5f);
3068 width /= 4;
3069 height /= 4;
3070 for (int y = 0; y < 4; ++y) {
3071 for (int x = 0; x < 4; ++x) {
3072 SkBitmap bitmap;
3073 bitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height));
3074 bitmap.writePixels(pixmap, -y * width, -x * height);
3075 canvas->drawBitmap(bitmap, x * width, y * height);
3076 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003077 }
3078##
3079
3080#SeeAlso readPixels
3081
3082##
3083
3084# ------------------------------------------------------------------------------
3085
3086#Method bool writePixels(const SkPixmap& src)
3087
3088Copies a Rect of pixels from src. Copy starts at (0, 0), and does not exceed
3089(src.width(), src.height()).
3090
3091src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
3092and row bytes of source. src.rowBytes specifics the gap from one source
3093row to the next. Returns true if pixels are copied. Returns false if:
3094#List
3095# src pixel storage equals nullptr ##
3096# src.rowBytes is less than SkImageInfo::minRowBytes ##
3097# Pixel_Ref is nullptr ##
3098##
3099
Cary Clarkac47b882018-01-11 10:35:44 -05003100Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04003101kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05003102If Bitmap colorType is kGray_8_SkColorType, src Color_Space must match.
3103If Bitmap alphaType is kOpaque_SkAlphaType, src Alpha_Type must
3104match. If Bitmap colorSpace is nullptr, src Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04003105false if pixel conversion is not possible.
3106
3107#Param src source Pixmap: Image_Info, pixels, row bytes ##
3108
3109#Return true if src pixels are copied to Bitmap ##
3110
3111#Example
3112#Height 80
Ben Wagner29380bd2017-10-09 14:43:00 -04003113 SkBitmap bitmap;
3114 bitmap.allocPixels(SkImageInfo::MakeN32Premul(2, 2));
3115 bitmap.eraseColor(SK_ColorGREEN);
3116 SkPMColor color = 0xFF5599BB;
3117 SkPixmap src(SkImageInfo::MakeN32Premul(1, 1), &color, 4);
3118 bitmap.writePixels(src);
3119 canvas->scale(40, 40);
3120 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003121##
3122
3123#SeeAlso readPixels
3124
3125##
3126
3127# ------------------------------------------------------------------------------
3128
3129#Method bool writePixels(const SkPixmap& src, int x, int y, SkTransferFunctionBehavior behavior)
3130
3131Copies a Rect of pixels from src. Copy starts at (0, 0), and does not exceed
3132(src.width(), src.height()).
3133
3134src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
3135and row bytes of source. src.rowBytes specifics the gap from one source
3136row to the next. Returns true if pixels are copied. Returns false if:
3137#List
3138# src pixel storage equals nullptr ##
3139# src.rowBytes is less than SkImageInfo::minRowBytes ##
3140# Pixel_Ref is nullptr ##
3141##
3142
Cary Clarkac47b882018-01-11 10:35:44 -05003143Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04003144kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05003145If Bitmap colorType is kGray_8_SkColorType, src Color_Space must match.
3146If Bitmap alphaType is kOpaque_SkAlphaType, src Alpha_Type must
3147match. If Bitmap colorSpace is nullptr, src Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04003148false if pixel conversion is not possible. Returns false if width() or height()
3149is zero or negative.
3150
3151If behavior is SkTransferFunctionBehavior::kRespect: converts src
3152pixels to a linear space before converting to Image_Info.
3153If behavior is SkTransferFunctionBehavior::kIgnore: src
3154pixels are treated as if they are linear, regardless of how they are encoded.
3155
3156#Param src source Pixmap: Image_Info, pixels, row bytes ##
3157#Param x column index whose absolute value is less than width() ##
3158#Param y row index whose absolute value is less than height() ##
3159#Param behavior one of: SkTransferFunctionBehavior::kRespect,
3160 SkTransferFunctionBehavior::kIgnore
3161##
3162
3163#Return true if src pixels are copied to Bitmap ##
3164
3165#Example
3166#Height 64
Ben Wagner29380bd2017-10-09 14:43:00 -04003167 const int width = 256;
3168 const int height = 32;
3169 std::vector<int32_t> dstPixels;
3170 dstPixels.resize(height * width * 4);
3171 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
3172 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
3173 SkPoint gradPoints[] = { { 0, 0 }, { width, 0 } };
3174 SkPaint gradPaint;
3175 gradPaint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
3176 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
3177 for (auto behavior : { SkTransferFunctionBehavior::kRespect,
3178 SkTransferFunctionBehavior::kIgnore} ) {
3179 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
3180 SkBitmap bitmap;
3181 bitmap.installPixels(dstPixmap);
3182 SkCanvas srcCanvas(bitmap);
3183 srcCanvas.drawRect(SkRect::MakeWH(width, height), gradPaint);
3184 if (bitmap.writePixels(dstPixmap, 0, 0, behavior)) {
3185 canvas->drawBitmap(bitmap, 0, 0);
3186 }
3187 canvas->translate(0, height);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003188 }
3189##
3190
3191#SeeAlso readPixels
3192
3193##
3194
3195# ------------------------------------------------------------------------------
3196
3197#Method bool hasHardwareMipMap() const
Cary Clark78de7512018-02-07 07:27:09 -05003198#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05003199#Line # returns Mip_Map support present; Android only ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003200#Private
3201Android framework only.
3202##
3203
3204#Return true if setHasHardwareMipMap has been called with true ##
3205
3206#NoExample
3207##
3208
3209#SeeAlso setHasHardwareMipMap
3210
3211##
3212
3213# ------------------------------------------------------------------------------
3214
3215#Method void setHasHardwareMipMap(bool hasHardwareMipMap)
Cary Clark78de7512018-02-07 07:27:09 -05003216#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05003217#Line # sets Mip_Map support present; Android only ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003218#Private
3219Android framework only.
3220##
3221
3222#Param hasHardwareMipMap sets state ##
3223
3224#NoExample
3225##
3226
3227#SeeAlso hasHardwareMipMap
3228
3229##
3230
3231# ------------------------------------------------------------------------------
3232
3233#Method bool extractAlpha(SkBitmap* dst) const
Cary Clark78de7512018-02-07 07:27:09 -05003234#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -05003235#Line # creates Bitmap containing Alpha of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003236Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3237or dst pixels cannot be allocated.
3238
3239Uses HeapAllocator to reserve memory for dst Pixel_Ref.
3240
3241#Param dst holds Pixel_Ref to fill with alpha layer ##
3242
3243#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3244
3245#Example
3246#Height 100
Ben Wagner29380bd2017-10-09 14:43:00 -04003247 SkBitmap alpha, bitmap;
3248 bitmap.allocN32Pixels(100, 100);
3249 SkCanvas offscreen(bitmap);
3250 offscreen.clear(0);
3251 SkPaint paint;
3252 paint.setAntiAlias(true);
3253 paint.setColor(SK_ColorBLUE);
3254 paint.setStyle(SkPaint::kStroke_Style);
3255 paint.setStrokeWidth(20);
3256 offscreen.drawCircle(50, 50, 39, paint);
3257 offscreen.flush();
3258 bitmap.extractAlpha(&alpha);
3259 paint.setColor(SK_ColorRED);
3260 canvas->drawBitmap(bitmap, 0, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003261 canvas->drawBitmap(alpha, 100, 0, &paint);
3262##
3263
3264#SeeAlso extractSubset
3265
3266##
3267
3268# ------------------------------------------------------------------------------
3269
3270#Method bool extractAlpha(SkBitmap* dst, const SkPaint* paint,
3271 SkIPoint* offset) const
3272
3273Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3274or dst pixels cannot be allocated.
3275
Cary Clarkf895a422018-02-27 09:54:21 -05003276If paint is not nullptr and contains Mask_Filter, SkMaskFilter
Cary Clarkbc5697d2017-10-04 14:31:33 -04003277generates Mask_Alpha from Bitmap. Uses HeapAllocator to reserve memory for dst
3278Pixel_Ref. Sets offset to top-left position for dst for alignment with Bitmap;
3279(0, 0) unless SkMaskFilter generates mask.
3280
3281#Param dst holds Pixel_Ref to fill with alpha layer ##
3282#Param paint holds optional Mask_Filter; may be nullptr ##
3283#Param offset top-left position for dst; may be nullptr ##
3284
3285#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3286
Cary Clark4855f782018-02-06 09:41:53 -05003287#Bug 7103
Cary Clarkbc5697d2017-10-04 14:31:33 -04003288#Example
3289#Height 160
Ben Wagner29380bd2017-10-09 14:43:00 -04003290 SkBitmap alpha, bitmap;
3291 bitmap.allocN32Pixels(100, 100);
3292 SkCanvas offscreen(bitmap);
3293 offscreen.clear(0);
3294 SkPaint paint;
3295 paint.setAntiAlias(true);
3296 paint.setColor(SK_ColorBLUE);
3297 paint.setStyle(SkPaint::kStroke_Style);
3298 paint.setStrokeWidth(20);
3299 offscreen.drawCircle(50, 50, 39, paint);
3300 offscreen.flush();
3301 const SkScalar kBlurSigma = SkBlurMaskFilter::ConvertRadiusToSigma(SkIntToScalar(25));
3302 paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, kBlurSigma,
3303 SkBlurMaskFilter::kHighQuality_BlurFlag));
3304 SkIPoint offset;
3305 bitmap.extractAlpha(&alpha, &paint, &offset);
3306 paint.setColor(SK_ColorRED);
3307 canvas->drawBitmap(bitmap, 0, -offset.fY, &paint);
3308 canvas->drawBitmap(alpha, 100 + offset.fX, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003309##
3310
3311#SeeAlso extractSubset
3312
3313##
3314
3315# ------------------------------------------------------------------------------
3316
3317#Method bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator,
3318 SkIPoint* offset) const
3319
3320Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3321or dst pixels cannot be allocated.
3322
Cary Clarkf895a422018-02-27 09:54:21 -05003323If paint is not nullptr and contains Mask_Filter, SkMaskFilter
Cary Clarkbc5697d2017-10-04 14:31:33 -04003324generates Mask_Alpha from Bitmap. allocator may reference a custom allocation
3325class or be set to nullptr to use HeapAllocator. Sets offset to top-left
3326position for dst for alignment with Bitmap; (0, 0) unless SkMaskFilter generates
3327mask.
3328
3329#Param dst holds Pixel_Ref to fill with alpha layer ##
3330#Param paint holds optional Mask_Filter; may be nullptr ##
3331#Param allocator method to reserve memory for Pixel_Ref; may be nullptr ##
3332#Param offset top-left position for dst; may be nullptr ##
3333
3334#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3335
Cary Clark4855f782018-02-06 09:41:53 -05003336#Bug 7104
Cary Clarkbc5697d2017-10-04 14:31:33 -04003337#Example
3338#Height 128
Ben Wagner29380bd2017-10-09 14:43:00 -04003339 SkBitmap alpha, bitmap;
3340 bitmap.allocN32Pixels(100, 100);
3341 SkCanvas offscreen(bitmap);
3342 offscreen.clear(0);
3343 SkPaint paint;
3344 paint.setAntiAlias(true);
3345 paint.setColor(SK_ColorBLUE);
3346 paint.setStyle(SkPaint::kStroke_Style);
3347 paint.setStrokeWidth(20);
3348 offscreen.drawCircle(50, 50, 39, paint);
3349 offscreen.flush();
Cary Clarkbc5697d2017-10-04 14:31:33 -04003350 paint.setMaskFilter(SkBlurMaskFilter::Make(kOuter_SkBlurStyle, 3));
Ben Wagner29380bd2017-10-09 14:43:00 -04003351 SkIPoint offset;
3352 bitmap.extractAlpha(&alpha, &paint, nullptr, &offset);
3353 paint.setColor(SK_ColorRED);
3354 canvas->drawBitmap(bitmap, 0, -offset.fY, &paint);
3355 canvas->drawBitmap(alpha, 100 + offset.fX, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003356##
3357
3358#SeeAlso extractSubset
3359
3360##
3361
3362# ------------------------------------------------------------------------------
3363
3364#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05003365#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05003366#Line # returns Pixmap if possible ##
Cary Clark154beea2017-10-26 07:58:48 -04003367Copies Bitmap pixel address, row bytes, and Image_Info to pixmap, if address
3368is available, and returns true. If pixel address is not available, return
3369false and leave pixmap unchanged.
3370
3371pixmap contents become invalid on any future change to Bitmap.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003372
3373#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
3374
3375#Return true if Bitmap has direct access to pixels ##
3376
3377#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04003378 SkBitmap bitmap;
3379 bitmap.allocPixels(SkImageInfo::MakeN32Premul(6, 11));
3380 SkCanvas offscreen(bitmap);
3381 offscreen.clear(SK_ColorWHITE);
3382 SkPaint paint;
3383 offscreen.drawString("?", 0, 10, paint);
3384 SkPixmap pixmap;
3385 if (bitmap.peekPixels(&pixmap)) {
3386 const SkPMColor* pixels = pixmap.addr32();
3387 SkPMColor pmWhite = pixels[0];
3388 for (int y = 0; y < bitmap.height(); ++y) {
3389 for (int x = 0; x < bitmap.width(); ++x) {
3390 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
3391 }
3392 SkDebugf("\n");
3393 }
3394 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003395 #StdOut
Cary Clark2f466242017-12-11 16:03:17 -05003396------
3397-xxx--
3398x---x-
3399----x-
3400---x--
3401--x---
3402--x---
3403------
3404--x---
3405--x---
Cary Clarka560c472017-11-27 10:44:06 -05003406------
Cary Clarkbc5697d2017-10-04 14:31:33 -04003407 #StdOut ##
3408##
3409
Cary Clark0c5f5462017-12-15 11:21:51 -05003410#SeeAlso pixmap() installPixels readPixels writePixels
Cary Clarkbc5697d2017-10-04 14:31:33 -04003411
3412##
3413
3414# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05003415#Subtopic Utility
3416#Populate
3417#Line # rarely called management functions ##
3418##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003419
Cary Clark154beea2017-10-26 07:58:48 -04003420#Method void validate() const;
Cary Clark78de7512018-02-07 07:27:09 -05003421#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05003422#Line # asserts if Bitmap is invalid (debug only) ##
Cary Clark154beea2017-10-26 07:58:48 -04003423Asserts if internal values are illegal or inconsistent. Only available if
3424SK_DEBUG is defined at compile time.
3425
3426#NoExample
3427##
3428
3429#SeeAlso SkImageInfo::validate()
3430
3431##
3432
3433# ------------------------------------------------------------------------------
3434
Cary Clarkbc5697d2017-10-04 14:31:33 -04003435#Method void toString(SkString* str) const;
Cary Clark78de7512018-02-07 07:27:09 -05003436#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05003437#Line # converts Bitmap to machine readable form ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003438#DefinedBy SK_TO_STRING_NONVIRT() ##
3439
3440#Private
3441macro expands to: void toString(SkString* str) const;
3442##
3443
Cary Clarkac47b882018-01-11 10:35:44 -05003444Creates string representation of Bitmap. The representation is read by
Cary Clarkbc5697d2017-10-04 14:31:33 -04003445internal debugging tools. The interface and implementation may be
3446suppressed by defining SK_IGNORE_TO_STRING.
3447
3448#Param str storage for string representation ##
3449
3450#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04003451 SkBitmap bitmap;
3452 int width = 6;
3453 int height = 11;
3454 bitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height));
3455 SkString string;
3456 bitmap.toString(&string);
3457 SkString match;
3458 match.printf("(%d, %d)", width, height);
3459 int start = string.find(match.c_str());
3460 if (start >= 0) {
3461 SkString whStr(&string.c_str()[start], match.size());
3462 SkDebugf("bitmap dimensions %s\n", whStr.c_str());
Cary Clarkbc5697d2017-10-04 14:31:33 -04003463 }
3464 #StdOut
3465 bitmap dimensions (6, 11)
3466 ##
3467##
3468
3469#SeeAlso SkPaint::toString
3470
3471##
3472
3473#Class SkBitmap ##
3474
3475#Topic Bitmap ##
Cary Clark4855f782018-02-06 09:41:53 -05003476