blob: a7cfb1dda1d6c3db9590daac0b741e836c0ad484 [file] [log] [blame]
Cary Clarkbc5697d2017-10-04 14:31:33 -04001#Topic Bitmap
Cary Clark137b8742018-05-30 09:21:49 -04002#Alias Bitmaps ##
3#Alias Bitmap_Reference ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004
5#Class SkBitmap
6
7Bitmap describes a two-dimensional raster pixel array. Bitmap is built on
8Image_Info, containing integer width and height, Color_Type and Alpha_Type
9describing the pixel format, and Color_Space describing the range of colors.
10Bitmap points to Pixel_Ref, which describes the physical array of pixels.
11Image_Info bounds may be located anywhere fully inside Pixel_Ref bounds.
12
13Bitmap can be drawn using Canvas. Bitmap can be a drawing destination for Canvas
Cary Clark137b8742018-05-30 09:21:49 -040014draw member functions. Bitmap flexibility as a pixel container limits some
Cary Clark682c58d2018-05-16 07:07:07 -040015optimizations available to the target platform.
Cary Clarkbc5697d2017-10-04 14:31:33 -040016
17If pixel array is primarily read-only, use Image for better performance.
18If pixel array is primarily written to, use Surface for better performance.
19
20Declaring SkBitmap const prevents altering Image_Info: the Bitmap height, width,
21and so on cannot change. It does not affect Pixel_Ref: a caller may write its
22pixels. Declaring SkBitmap const affects Bitmap configuration, not its contents.
23
Cary Clarkbef063a2017-10-31 15:44:45 -040024Bitmap is not thread safe. Each thread must have its own copy of Bitmap fields,
Cary Clarkbc5697d2017-10-04 14:31:33 -040025although threads may share the underlying pixel array.
26
Cary Clark08895c42018-02-01 09:37:32 -050027#Subtopic Row_Bytes
28#Line # interval from one row to the next ##
Cary Clarkbc5697d2017-10-04 14:31:33 -040029Bitmap pixels may be contiguous, or may have a gap at the end of each row.
30Row_Bytes is the interval from one row to the next. Row_Bytes may be specified;
31sometimes passing zero will compute the Row_Bytes from the row width and the
32number of bytes in a pixel. Row_Bytes may be larger than the row requires. This
33is useful to position one or more Bitmaps within a shared pixel array.
34##
35
Cary Clark682c58d2018-05-16 07:07:07 -040036#Subtopic Overview
37 #Populate
38##
39
Cary Clark4855f782018-02-06 09:41:53 -050040#Subtopic Related_Function
Cary Clark08895c42018-02-01 09:37:32 -050041#Populate
Cary Clarkbc5697d2017-10-04 14:31:33 -040042##
43
Cary Clark4855f782018-02-06 09:41:53 -050044#Subtopic Constant
Cary Clark08895c42018-02-01 09:37:32 -050045#Populate
46##
Cary Clarkbc5697d2017-10-04 14:31:33 -040047
Cary Clark682c58d2018-05-16 07:07:07 -040048#Subtopic Class
Cary Clark08895c42018-02-01 09:37:32 -050049#Populate
50##
Cary Clarkbc5697d2017-10-04 14:31:33 -040051
Cary Clark4855f782018-02-06 09:41:53 -050052#Subtopic Constructor
Cary Clark08895c42018-02-01 09:37:32 -050053#Populate
54##
Cary Clarkbc5697d2017-10-04 14:31:33 -040055
Cary Clark4855f782018-02-06 09:41:53 -050056#Subtopic Operator
57#Populate
58##
59
60#Subtopic Member_Function
Cary Clark08895c42018-02-01 09:37:32 -050061#Populate
62##
Cary Clarkbc5697d2017-10-04 14:31:33 -040063
64# ------------------------------------------------------------------------------
65
66#Class Allocator
Cary Clark08895c42018-02-01 09:37:32 -050067#Line # abstract subclass of HeapAllocator ##
Cary Clarkbc5697d2017-10-04 14:31:33 -040068#Code
69 class Allocator : public SkRefCnt {
70 public:
71 virtual bool allocPixelRef(SkBitmap* bitmap) = 0;
72 };
73##
74
75Abstract subclass of HeapAllocator.
76
77# ------------------------------------------------------------------------------
78
79#Method virtual bool allocPixelRef(SkBitmap* bitmap) = 0
80
81Allocates the pixel memory for the bitmap, given its dimensions and
82Color_Type. Returns true on success, where success means either setPixels
83or setPixelRef was called.
84
85#Param bitmap Bitmap containing Image_Info as input, and Pixel_Ref as output ##
86
87#Return true if Pixel_Ref was allocated ##
88
89#NoExample
90##
91
92#SeeAlso HeapAllocator
93
94##
95
96#Class Allocator ##
97
98# ------------------------------------------------------------------------------
99
100#Class HeapAllocator
Cary Clark08895c42018-02-01 09:37:32 -0500101#Line # allocates pixel memory from heap ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400102
103#Code
104 class HeapAllocator : public Allocator {
105 public:
106 bool allocPixelRef(SkBitmap* bitmap) override;
107 };
108##
109
Cary Clark78c110e2018-02-09 16:49:09 -0500110Subclass of SkBitmap::Allocator that returns a Pixel_Ref that allocates its pixel
111memory from the heap. This is the default SkBitmap::Allocator invoked by
Cary Clarkbc5697d2017-10-04 14:31:33 -0400112allocPixels.
113
114# ------------------------------------------------------------------------------
115
116#Method bool allocPixelRef(SkBitmap* bitmap) override
Cary Clark682c58d2018-05-16 07:07:07 -0400117#Line # allocates pixel memory ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400118Allocates the pixel memory for the bitmap, given its dimensions and
119Color_Type. Returns true on success, where success means either setPixels
120or setPixelRef was called.
121
122#Param bitmap Bitmap containing Image_Info as input, and Pixel_Ref as output ##
123
124#Return true if pixels are allocated ##
125
126#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400127 SkBitmap bitmap;
128 bitmap.setInfo(SkImageInfo::MakeN32(16, 16, kPremul_SkAlphaType));
129 SkDebugf("pixel address = %p\n", bitmap.getPixels());
130 SkBitmap::HeapAllocator stdalloc;
131 if (!stdalloc.allocPixelRef(&bitmap)) {
132 SkDebugf("pixel allocation failed\n");
133 } else {
134 SkDebugf("pixel address = %p\n", bitmap.getPixels());
135 }
136#StdOut
Cary Clark884dd7d2017-10-11 10:37:52 -0400137#Volatile
Ben Wagner29380bd2017-10-09 14:43:00 -0400138pixel address = (nil)
139pixel address = 0x560ddd0ac670
140##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400141##
142
Cary Clark78c110e2018-02-09 16:49:09 -0500143#SeeAlso SkBitmap::Allocator tryAllocPixels
Cary Clarkbc5697d2017-10-04 14:31:33 -0400144
145##
146
Cary Clark08895c42018-02-01 09:37:32 -0500147#Class HeapAllocator ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400148
149# ------------------------------------------------------------------------------
150
151#Method SkBitmap()
152
Cary Clarkab2621d2018-01-30 10:08:57 -0500153#Line # constructs with default values ##
Cary Clark682c58d2018-05-16 07:07:07 -0400154Creates an empty Bitmap without pixels, with kUnknown_SkColorType,
Cary Clarkbc5697d2017-10-04 14:31:33 -0400155kUnknown_SkAlphaType, and with a width and height of zero. Pixel_Ref origin is
156set to (0, 0). Bitmap is not volatile.
157
158Use setInfo to associate SkColorType, SkAlphaType, width, and height
159after Bitmap has been created.
160
161#Return empty Bitmap ##
162
163#Example
164void draw(SkCanvas* canvas) {
165 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500166 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
167 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clarkbc5697d2017-10-04 14:31:33 -0400168 SkBitmap bitmap;
169 for (int i = 0; i < 2; ++i) {
170 SkDebugf("width: %2d height: %2d", bitmap.width(), bitmap.height());
171 SkDebugf(" color: k%s_SkColorType", colors[bitmap.colorType()]);
172 SkDebugf(" alpha: k%s_SkAlphaType\n", alphas[bitmap.alphaType()]);
173 bitmap.setInfo(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType),
174 0);
175 }
176}
177#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400178width: 0 height: 0 color: kUnknown_SkColorType alpha: kUnknown_SkAlphaType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400179width: 25 height: 35 color: kRGBA_8888_SkColorType alpha: kOpaque_SkAlphaType
180##
181##
182
183#SeeAlso setInfo
184
185##
186
187# ------------------------------------------------------------------------------
188
189#Method SkBitmap(const SkBitmap& src)
190
Cary Clarkab2621d2018-01-30 10:08:57 -0500191#Line # shares ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400192Copies settings from src to returned Bitmap. Shares pixels if src has pixels
193allocated, so both bitmaps reference the same pixels.
194
195#Param src Bitmap to copy Image_Info, and share Pixel_Ref ##
196
197#Return copy of src ##
198
199#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400200void draw(SkCanvas* canvas) {
201 SkBitmap original;
Cary Clark681287e2018-03-16 11:34:15 -0400202 if (original.tryAllocPixels(
203 SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
204 SkDebugf("original has pixels before copy: %s\n", original.getPixels() ? "true" : "false");
Cary Clark682c58d2018-05-16 07:07:07 -0400205 SkBitmap copy(original);
Cary Clark681287e2018-03-16 11:34:15 -0400206 SkDebugf("original has pixels after copy: %s\n", original.getPixels() ? "true" : "false");
207 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
208 }
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;
Cary Clark681287e2018-03-16 11:34:15 -0400236 if (original.tryAllocPixels(
237 SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
238 SkDebugf("original has pixels before move: %s\n", original.getPixels() ? "true" : "false");
Cary Clark682c58d2018-05-16 07:07:07 -0400239 SkBitmap copy(std::move(original));
Cary Clark681287e2018-03-16 11:34:15 -0400240 SkDebugf("original has pixels after move: %s\n", original.getPixels() ? "true" : "false");
241 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
242 }
Ben Wagner29380bd2017-10-09 14:43:00 -0400243}
Cary Clarkbc5697d2017-10-04 14:31:33 -0400244#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400245original has pixels before move: true
246original has pixels after move: false
Cary Clarkbc5697d2017-10-04 14:31:33 -0400247copy has pixels: true
248##
249##
250
251#SeeAlso setInfo setPixelRef setPixels swap
252
253##
254
255# ------------------------------------------------------------------------------
256
257#Method ~SkBitmap()
258
Cary Clarkab2621d2018-01-30 10:08:57 -0500259#Line # releases ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400260Decrements Pixel_Ref reference count, if Pixel_Ref is not nullptr.
261
262#NoExample
263##
264
265#SeeAlso Pixel_Ref
266
267##
268
269# ------------------------------------------------------------------------------
270
271#Method SkBitmap& operator=(const SkBitmap& src)
272
Cary Clarkab2621d2018-01-30 10:08:57 -0500273#Line # shares ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400274Copies settings from src to returned Bitmap. Shares pixels if src has pixels
275allocated, so both bitmaps reference the same pixels.
276
277#Param src Bitmap to copy Image_Info, and share Pixel_Ref ##
278
279#Return copy of src ##
280
281#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400282void draw(SkCanvas* canvas) {
283 SkBitmap original;
Cary Clark681287e2018-03-16 11:34:15 -0400284 if (original.tryAllocPixels(
285 SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
286 SkDebugf("original has pixels before copy: %s\n", original.getPixels() ? "true" : "false");
Cary Clark682c58d2018-05-16 07:07:07 -0400287 SkBitmap copy = original;
Cary Clark681287e2018-03-16 11:34:15 -0400288 SkDebugf("original has pixels after copy: %s\n", original.getPixels() ? "true" : "false");
289 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
290 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400291}
292#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400293original has pixels before copy: true
294original has pixels after copy: true
Cary Clarkbc5697d2017-10-04 14:31:33 -0400295copy has pixels: true
296##
297##
298
299#SeeAlso setInfo setPixelRef setPixels swap
300
301##
302
303# ------------------------------------------------------------------------------
304
305#Method SkBitmap& operator=(SkBitmap&& src)
306
Cary Clarkab2621d2018-01-30 10:08:57 -0500307#Line # takes ownership of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400308Copies settings from src to returned Bitmap. Moves ownership of src pixels to
309Bitmap.
310
311#Param src Bitmap to copy Image_Info, and reassign Pixel_Ref ##
312
313#Return copy of src ##
314
315#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400316void draw(SkCanvas* canvas) {
317 SkBitmap original;
Cary Clark681287e2018-03-16 11:34:15 -0400318 if (original.tryAllocPixels(
319 SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
320 SkDebugf("original has pixels before move: %s\n", original.getPixels() ? "true" : "false");
Cary Clark682c58d2018-05-16 07:07:07 -0400321 SkBitmap copy = std::move(original);
Cary Clark681287e2018-03-16 11:34:15 -0400322 SkDebugf("original has pixels after move: %s\n", original.getPixels() ? "true" : "false");
323 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
324 }
Ben Wagner29380bd2017-10-09 14:43:00 -0400325}
Cary Clarkbc5697d2017-10-04 14:31:33 -0400326#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400327original has pixels before move: true
328original has pixels after move: false
Cary Clarkbc5697d2017-10-04 14:31:33 -0400329copy has pixels: true
330##
331##
332
333#SeeAlso setInfo setPixelRef setPixels swap
334
335##
336
337# ------------------------------------------------------------------------------
338
339#Method void swap(SkBitmap& other)
Cary Clark78de7512018-02-07 07:27:09 -0500340#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -0500341#Line # exchanges Bitmap pair ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400342Swaps the fields of the two bitmaps.
343
344#Param other Bitmap exchanged with original ##
345
346#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400347void draw(SkCanvas* canvas) {
348 auto debugster = [](const char* prefix, const SkBitmap& b) -> void {
349 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500350 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
351 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Ben Wagner29380bd2017-10-09 14:43:00 -0400352 SkDebugf("%s width:%d height:%d colorType:k%s_SkColorType alphaType:k%s_SkAlphaType\n",
353 prefix, b.width(), b.height(), colors[b.colorType()], alphas[b.alphaType()]);
354 };
355 SkBitmap one, two;
Cary Clark681287e2018-03-16 11:34:15 -0400356 if (!one.tryAllocPixels(
357 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType))) {
358 return;
359 }
360 if (!two.tryAllocPixels(
361 SkImageInfo::Make(2, 2, kBGRA_8888_SkColorType, kPremul_SkAlphaType))) {
362 return;
363 }
Ben Wagner29380bd2017-10-09 14:43:00 -0400364 for (int index = 0; index < 2; ++index) {
365 debugster("one", one);
366 debugster("two", two);
367 one.swap(two);
368 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400369}
370#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400371one width:1 height:1 colorType:kRGBA_8888_SkColorType alphaType:kOpaque_SkAlphaType
372two width:2 height:2 colorType:kBGRA_8888_SkColorType alphaType:kPremul_SkAlphaType
373one width:2 height:2 colorType:kBGRA_8888_SkColorType alphaType:kPremul_SkAlphaType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400374two width:1 height:1 colorType:kRGBA_8888_SkColorType alphaType:kOpaque_SkAlphaType
375##
376##
377
378#SeeAlso SkBitmap(SkBitmap&& src) operator=(SkBitmap&& src)
379
380##
381
382# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -0500383#Subtopic Property
384#Populate
385#Line # metrics and attributes ##
386##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400387
Hal Canary99578d22017-12-14 21:13:47 -0500388#Method const SkPixmap& pixmap() const
Cary Clark78de7512018-02-07 07:27:09 -0500389#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500390#Line # returns Pixmap ##
Hal Canary99578d22017-12-14 21:13:47 -0500391Returns a constant reference to the Pixmap holding the Bitmap pixel
392address, row bytes, and Image_Info.
Cary Clark0c5f5462017-12-15 11:21:51 -0500393
Cary Clarkac47b882018-01-11 10:35:44 -0500394#Return reference to Pixmap describing this Bitmap ##
Cary Clark0c5f5462017-12-15 11:21:51 -0500395
396#Example
397 SkBitmap bitmap;
398 bitmap.allocPixels(SkImageInfo::MakeN32Premul(10, 11));
399 SkCanvas offscreen(bitmap);
400 offscreen.clear(SK_ColorWHITE);
401 SkPaint paint;
402 offscreen.drawString("&", 0, 10, paint);
Hal Canary99578d22017-12-14 21:13:47 -0500403 const SkPixmap& pixmap = bitmap.pixmap();
Cary Clark0c5f5462017-12-15 11:21:51 -0500404 if (pixmap.addr()) {
Hal Canary99578d22017-12-14 21:13:47 -0500405 SkPMColor pmWhite = *pixmap.addr32(0, 0);
406 for (int y = 0; y < pixmap.height(); ++y) {
407 for (int x = 0; x < pixmap.width(); ++x) {
408 SkDebugf("%c", *pixmap.addr32(x, y) == pmWhite ? '-' : 'x');
Cary Clark0c5f5462017-12-15 11:21:51 -0500409 }
410 SkDebugf("\n");
411 }
412 }
413 #StdOut
414----------
415---xx-----
416--x--x----
417--x-------
418--xx------
419--x-x---x-
420-x---x--x-
421-x----xx--
422-xx---x---
423--xxxx-xx-
424----------
425 #StdOut ##
426
427##
428
429#SeeAlso peekPixels installPixels readPixels writePixels
430
431##
432
433# ------------------------------------------------------------------------------
434
Cary Clarkbc5697d2017-10-04 14:31:33 -0400435#Method const SkImageInfo& info() const
Cary Clark78de7512018-02-07 07:27:09 -0500436#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500437#Line # returns Image_Info ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400438Returns width, height, Alpha_Type, Color_Type, and Color_Space.
439
440#Return reference to Image_Info ##
441
442#Example
443#Image 4
Ben Wagner29380bd2017-10-09 14:43:00 -0400444void draw(SkCanvas* canvas) {
445 // SkBitmap source; // pre-populated with soccer ball by fiddle.skia.org
446 const SkImageInfo& info = source.info();
447 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500448 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
449 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Ben Wagner29380bd2017-10-09 14:43:00 -0400450 SkDebugf("width: %d height: %d color: %s alpha: %s\n", info.width(), info.height(),
451 colors[info.colorType()], alphas[info.alphaType()]);
452#StdOut
453width: 56 height: 56 color: BGRA_8888 alpha: Opaque
454##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400455}
456##
457
458#SeeAlso Image_Info
459
460##
461
462# ------------------------------------------------------------------------------
463
464#Method int width() const
Cary Clark78de7512018-02-07 07:27:09 -0500465#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500466#Line # returns pixel column count ##
Cary Clarka560c472017-11-27 10:44:06 -0500467Returns pixel count in each row. Should be equal or less than:
Cary Clark154beea2017-10-26 07:58:48 -0400468
Cary Clarkbc5697d2017-10-04 14:31:33 -0400469#Formula
470rowBytes() / info().bytesPerPixel()
471##
Cary Clark682c58d2018-05-16 07:07:07 -0400472.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400473
474Maybe be less than pixelRef().width(). Will not exceed pixelRef().width() less
475pixelRefOrigin().fX.
476
477#Return pixel width in Image_Info ##
478
479#Example
480 SkImageInfo info = SkImageInfo::MakeA8(16, 32);
481 SkBitmap bitmap;
482 bitmap.setInfo(info);
483 SkDebugf("bitmap width: %d info width: %d\n", bitmap.width(), info.width());
484#StdOut
485bitmap width: 16 info width: 16
486##
487##
488
489#SeeAlso height() SkPixelRef::width() SkImageInfo::width()
490
491##
492
493# ------------------------------------------------------------------------------
494
495#Method int height() const
Cary Clark78de7512018-02-07 07:27:09 -0500496#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500497#Line # returns pixel row count ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400498Returns pixel row count.
499
500Maybe be less than pixelRef().height(). Will not exceed pixelRef().height() less
501pixelRefOrigin().fY.
502
503#Return pixel height in Image_Info ##
504
505#Example
506 SkImageInfo info = SkImageInfo::MakeA8(16, 32);
507 SkBitmap bitmap;
508 bitmap.setInfo(info);
509 SkDebugf("bitmap height: %d info height: %d\n", bitmap.height(), info.height());
510#StdOut
511bitmap height: 32 info height: 32
512##
513##
514
515#SeeAlso width() SkPixelRef::height() SkImageInfo::height()
516
517##
518
519# ------------------------------------------------------------------------------
520
521#Method SkColorType colorType() const
Cary Clark78de7512018-02-07 07:27:09 -0500522#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500523#Line # returns Image_Info Color_Type ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500524Returns Color_Type, one of: #list_of_color_types#.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400525
526#Return Color_Type in Image_Info ##
527
528#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500529 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
530 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Ben Wagner29380bd2017-10-09 14:43:00 -0400531 SkBitmap bitmap;
532 bitmap.setInfo(SkImageInfo::MakeA8(16, 32));
533 SkDebugf("color type: k" "%s" "_SkColorType\n", colors[bitmap.colorType()]);
Cary Clarkbc5697d2017-10-04 14:31:33 -0400534#StdOut
Cary Clarkab2621d2018-01-30 10:08:57 -0500535color type: kAlpha_8_SkColorType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400536##
537##
538
539#SeeAlso alphaType() SkImageInfo::colorType
540
541##
542
543# ------------------------------------------------------------------------------
544
545#Method SkAlphaType alphaType() const
Cary Clark78de7512018-02-07 07:27:09 -0500546#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500547#Line # returns Image_Info Alpha_Type ##
Cary Clark681287e2018-03-16 11:34:15 -0400548Returns Alpha_Type, one of: #list_of_alpha_types#.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400549
550#Return Alpha_Type in Image_Info ##
551
552#Example
553 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
554 SkPixmap pixmap(SkImageInfo::MakeA8(16, 32), nullptr, 64);
555 SkDebugf("alpha type: k" "%s" "_SkAlphaType\n", alphas[pixmap.alphaType()]);
556#StdOut
557alpha type: kPremul_SkAlphaType
558##
559##
560
561#SeeAlso colorType() SkImageInfo::alphaType
562
563##
564
565# ------------------------------------------------------------------------------
566
567#Method SkColorSpace* colorSpace() const
Cary Clark78de7512018-02-07 07:27:09 -0500568#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500569#Line # returns Image_Info Color_Space ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400570Returns Color_Space, the range of colors, associated with Image_Info. The
571reference count of Color_Space is unchanged. The returned Color_Space is
572immutable.
573
Cary Clark2f466242017-12-11 16:03:17 -0500574#Return Color_Space in Image_Info, or nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400575
576#Example
577#Description
578SkColorSpace::MakeSRGBLinear creates Color_Space with linear gamma
579and an sRGB gamut. This Color_Space gamma is not close to sRGB gamma.
580##
Ben Wagner29380bd2017-10-09 14:43:00 -0400581 SkBitmap bitmap;
Cary Clark682c58d2018-05-16 07:07:07 -0400582 bitmap.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
Cary Clarkbc5697d2017-10-04 14:31:33 -0400583 SkColorSpace::MakeSRGBLinear()));
584 SkColorSpace* colorSpace = bitmap.colorSpace();
585 SkDebugf("gammaCloseToSRGB: %s gammaIsLinear: %s isSRGB: %s\n",
586 colorSpace->gammaCloseToSRGB() ? "true" : "false",
587 colorSpace->gammaIsLinear() ? "true" : "false",
588 colorSpace->isSRGB() ? "true" : "false");
589#StdOut
590gammaCloseToSRGB: false gammaIsLinear: true isSRGB: false
591##
592##
593
594#SeeAlso Color_Space SkImageInfo::colorSpace
595
596##
597
598# ------------------------------------------------------------------------------
599
600#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark78de7512018-02-07 07:27:09 -0500601#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500602#Line # returns Image_Info Color_Space ##
Cary Clark681287e2018-03-16 11:34:15 -0400603Returns smart pointer to Color_Space, the range of colors, associated with
Cary Clarkbc5697d2017-10-04 14:31:33 -0400604Image_Info. The smart pointer tracks the number of objects sharing this
605Color_Space reference so the memory is released when the owners destruct.
606
607The returned Color_Space is immutable.
608
609#Return Color_Space in Image_Info wrapped in a smart pointer ##
610
611#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400612 SkBitmap bitmap1, bitmap2;
Cary Clark682c58d2018-05-16 07:07:07 -0400613 bitmap1.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
Cary Clarkbc5697d2017-10-04 14:31:33 -0400614 SkColorSpace::MakeSRGBLinear()));
615 bitmap2.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
616 bitmap1.refColorSpace()));
617 SkColorSpace* colorSpace = bitmap2.colorSpace();
618 SkDebugf("gammaCloseToSRGB: %s gammaIsLinear: %s isSRGB: %s\n",
619 colorSpace->gammaCloseToSRGB() ? "true" : "false",
620 colorSpace->gammaIsLinear() ? "true" : "false",
621 colorSpace->isSRGB() ? "true" : "false");
622#StdOut
623gammaCloseToSRGB: false gammaIsLinear: true isSRGB: false
624##
625##
626
627#SeeAlso Color_Space SkImageInfo::colorSpace
628
629##
630
631# ------------------------------------------------------------------------------
632
633#Method int bytesPerPixel() const
Cary Clark78de7512018-02-07 07:27:09 -0500634#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500635#Line # returns number of bytes in pixel based on Color_Type ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400636Returns number of bytes per pixel required by Color_Type.
637Returns zero if colorType( is kUnknown_SkColorType.
638
639#Return bytes in pixel ##
640
641#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500642 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
643 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clarkbc5697d2017-10-04 14:31:33 -0400644 SkImageInfo info = SkImageInfo::MakeA8(1, 1);
645 SkBitmap bitmap;
Cary Clark1a8d7622018-03-05 13:26:16 -0500646 for (SkColorType colorType : { #list_of_color_types#
647 } ) {
Cary Clarkbc5697d2017-10-04 14:31:33 -0400648 bitmap.setInfo(info.makeColorType(colorType));
649 SkDebugf("color: k" "%s" "_SkColorType" "%*s" "bytesPerPixel: %d\n",
Cary Clarkab2621d2018-01-30 10:08:57 -0500650 colors[colorType], 13 - strlen(colors[colorType]), " ",
Cary Clarkbc5697d2017-10-04 14:31:33 -0400651 bitmap.bytesPerPixel());
652 }
653#StdOut
Cary Clark98aebac2018-03-13 09:02:35 -0400654color: kUnknown_SkColorType bytesPerPixel: 0
655color: kAlpha_8_SkColorType bytesPerPixel: 1
656color: kRGB_565_SkColorType bytesPerPixel: 2
657color: kARGB_4444_SkColorType bytesPerPixel: 2
658color: kRGBA_8888_SkColorType bytesPerPixel: 4
659color: kRGB_888x_SkColorType bytesPerPixel: 4
660color: kBGRA_8888_SkColorType bytesPerPixel: 4
661color: kRGBA_1010102_SkColorType bytesPerPixel: 4
662color: kRGB_101010x_SkColorType bytesPerPixel: 4
663color: kGray_8_SkColorType bytesPerPixel: 1
Cary Clarkab2621d2018-01-30 10:08:57 -0500664color: kRGBA_F16_SkColorType bytesPerPixel: 8
Cary Clarkbc5697d2017-10-04 14:31:33 -0400665##
666##
667
Cary Clark681287e2018-03-16 11:34:15 -0400668#SeeAlso rowBytes rowBytesAsPixels width shiftPerPixel SkImageInfo::bytesPerPixel
Cary Clarkbc5697d2017-10-04 14:31:33 -0400669
670##
671
672# ------------------------------------------------------------------------------
673
674#Method int rowBytesAsPixels() const
Cary Clark78de7512018-02-07 07:27:09 -0500675#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500676#Line # returns interval between rows in pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400677Returns number of pixels that fit on row. Should be greater than or equal to
678width().
679
680#Return maximum pixels per row ##
681
682#Example
683 SkBitmap bitmap;
684 for (int rowBytes : { 4, 5, 6, 7, 8} ) {
685 bitmap.setInfo(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), rowBytes);
686 SkDebugf("rowBytes: %d rowBytesAsPixels: %d\n", rowBytes, bitmap.rowBytesAsPixels());
687 }
688#StdOut
689rowBytes: 4 rowBytesAsPixels: 1
690rowBytes: 5 rowBytesAsPixels: 1
691rowBytes: 6 rowBytesAsPixels: 1
692rowBytes: 7 rowBytesAsPixels: 1
693rowBytes: 8 rowBytesAsPixels: 2
694##
695##
696
697#SeeAlso rowBytes shiftPerPixel width bytesPerPixel
698
699##
700
701# ------------------------------------------------------------------------------
702
703#Method int shiftPerPixel() const
Cary Clark78de7512018-02-07 07:27:09 -0500704#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500705#Line # returns bit shift from pixels to bytes ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400706Returns bit shift converting row bytes to row pixels.
707Returns zero for kUnknown_SkColorType.
708
709#Return one of: 0, 1, 2, 3; left shift to convert pixels to bytes ##
710
711#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500712 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
713 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clarkbc5697d2017-10-04 14:31:33 -0400714 SkImageInfo info = SkImageInfo::MakeA8(1, 1);
715 SkBitmap bitmap;
Cary Clark1a8d7622018-03-05 13:26:16 -0500716 for (SkColorType colorType : { #list_of_color_types#
717 } ) {
Cary Clarkbc5697d2017-10-04 14:31:33 -0400718 bitmap.setInfo(info.makeColorType(colorType));
719 SkDebugf("color: k" "%s" "_SkColorType" "%*s" "shiftPerPixel: %d\n",
Cary Clark1a8d7622018-03-05 13:26:16 -0500720 colors[colorType], 14 - strlen(colors[colorType]), " ",
Cary Clarkbc5697d2017-10-04 14:31:33 -0400721 bitmap.shiftPerPixel());
722 }
723#StdOut
Cary Clark98aebac2018-03-13 09:02:35 -0400724color: kUnknown_SkColorType shiftPerPixel: 0
725color: kAlpha_8_SkColorType shiftPerPixel: 0
726color: kRGB_565_SkColorType shiftPerPixel: 1
727color: kARGB_4444_SkColorType shiftPerPixel: 1
728color: kRGBA_8888_SkColorType shiftPerPixel: 2
729color: kRGB_888x_SkColorType shiftPerPixel: 2
730color: kBGRA_8888_SkColorType shiftPerPixel: 2
731color: kRGBA_1010102_SkColorType shiftPerPixel: 2
732color: kRGB_101010x_SkColorType shiftPerPixel: 2
733color: kGray_8_SkColorType shiftPerPixel: 0
Cary Clark1a8d7622018-03-05 13:26:16 -0500734color: kRGBA_F16_SkColorType shiftPerPixel: 3
Cary Clarkbc5697d2017-10-04 14:31:33 -0400735##
736##
737
738#SeeAlso rowBytes rowBytesAsPixels width bytesPerPixel
739
740##
741
742# ------------------------------------------------------------------------------
743
744#Method bool empty() const
Cary Clark78de7512018-02-07 07:27:09 -0500745#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500746#Line # returns true if Image_Info has zero width() or height() ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400747Returns true if either width() or height() are zero.
748
749Does not check if Pixel_Ref is nullptr; call drawsNothing to check width(),
750height(), and Pixel_Ref.
751
752#Return true if dimensions do not enclose area ##
753
754#Example
755 SkBitmap bitmap;
756 for (int width : { 0, 2 } ) {
757 for (int height : { 0, 2 } ) {
758 bitmap.setInfo(SkImageInfo::MakeA8(width, height));
759 SkDebugf("width: %d height: %d empty: %s\n", width, height,
760 bitmap.empty() ? "true" : "false");
761 }
762 }
763#StdOut
764width: 0 height: 0 empty: true
765width: 0 height: 2 empty: true
766width: 2 height: 0 empty: true
767width: 2 height: 2 empty: false
768##
769##
770
Cary Clark682c58d2018-05-16 07:07:07 -0400771#SeeAlso height() width() drawsNothing
Cary Clarkbc5697d2017-10-04 14:31:33 -0400772
773##
774
775# ------------------------------------------------------------------------------
776
777#Method bool isNull() const
Cary Clark78de7512018-02-07 07:27:09 -0500778#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500779#Line # returns true if Pixel_Ref is nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400780Return true if Pixel_Ref is nullptr.
781
782Does not check if width() or height() are zero; call drawsNothing to check
783width(), height(), and Pixel_Ref.
784
785#Return true if no Pixel_Ref is associated ##
786
787#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400788 SkBitmap bitmap;
789 SkDebugf("empty bitmap does %shave pixels\n", bitmap.isNull() ? "not " : "");
790 bitmap.setInfo(SkImageInfo::MakeA8(8, 8));
791 SkDebugf("bitmap with dimensions does %shave pixels\n", bitmap.isNull() ? "not " : "");
792 bitmap.allocPixels();
793 SkDebugf("allocated bitmap does %shave pixels\n", bitmap.isNull() ? "not " : "");
Cary Clarkbc5697d2017-10-04 14:31:33 -0400794#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400795empty bitmap does not have pixels
796bitmap with dimensions does not have pixels
Cary Clarkbc5697d2017-10-04 14:31:33 -0400797allocated bitmap does have pixels
798##
799##
800
801#SeeAlso empty() drawsNothing pixelRef
802
803##
804
805# ------------------------------------------------------------------------------
806
807#Method bool drawsNothing() const
Cary Clark78de7512018-02-07 07:27:09 -0500808#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500809#Line # returns true if no width(), no height(), or no Pixel_Ref ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400810Return true if width() or height() are zero, or if Pixel_Ref is nullptr.
811If true, Bitmap has no effect when drawn or drawn into.
812
813#Return true if drawing has no effect ##
814
815#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400816 SkBitmap bitmap;
817 for (int w : { 0, 8 } ) {
818 for (bool allocate : { false, true} ) {
819 bitmap.setInfo(SkImageInfo::MakeA8(w, 8));
820 allocate ? bitmap.allocPixels() : (void) 0 ;
821 SkDebugf("empty:%s isNull:%s drawsNothing:%s\n", bitmap.empty() ? "true " : "false",
822 bitmap.isNull() ? "true " : "false", bitmap.drawsNothing() ? "true" : "false");
823 }
824 }
825#StdOut
826empty:true isNull:true drawsNothing:true
827empty:true isNull:false drawsNothing:true
828empty:false isNull:true drawsNothing:true
829empty:false isNull:false drawsNothing:false
830##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400831##
832
833#SeeAlso empty() isNull pixelRef
834
835##
836
837# ------------------------------------------------------------------------------
838
839#Method size_t rowBytes() const
Cary Clark78de7512018-02-07 07:27:09 -0500840#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500841#Line # returns interval between rows in bytes ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400842Returns row bytes, the interval from one pixel row to the next. Row bytes
Cary Clark682c58d2018-05-16 07:07:07 -0400843is at least as large as
Cary Clarkbc5697d2017-10-04 14:31:33 -0400844#Formula
845width() * info().bytesPerPixel()
846##
847.
848
849Returns zero if colorType is kUnknown_SkColorType, or if row bytes supplied to
850setInfo is not large enough to hold a row of pixels.
851
852#Return byte length of pixel row ##
853
854#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400855 SkBitmap bitmap;
856 for (int rowBytes : { 2, 8 } ) {
857 bool result = bitmap.setInfo(SkImageInfo::MakeA8(4, 4), rowBytes);
858 SkDebugf("setInfo returned:%s rowBytes:%d\n", result ? "true " : "false", bitmap.rowBytes());
859 }
860#StdOut
861setInfo returned:false rowBytes:0
862setInfo returned:true rowBytes:8
863##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400864##
865
866#SeeAlso info() setInfo SkImageInfo::minRowBytes
867
868##
869
870# ------------------------------------------------------------------------------
871
872#Method bool setAlphaType(SkAlphaType alphaType)
Cary Clark78de7512018-02-07 07:27:09 -0500873#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -0500874#Line # sets Alpha_Type of shared pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400875Sets Alpha_Type, if alphaType is compatible with Color_Type.
876Returns true unless alphaType is kUnknown_SkAlphaType and current Alpha_Type
877is not kUnknown_SkAlphaType.
878
879Returns true if Color_Type is kUnknown_SkColorType. alphaType is ignored, and
880Alpha_Type remains kUnknown_SkAlphaType.
881
882Returns true if Color_Type is kRGB_565_SkColorType or kGray_8_SkColorType.
883alphaType is ignored, and Alpha_Type remains kOpaque_SkAlphaType.
884
885If Color_Type is kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
886kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: returns true unless
887alphaType is kUnknown_SkAlphaType and Alpha_Type is not kUnknown_SkAlphaType.
888If Alpha_Type is kUnknown_SkAlphaType, alphaType is ignored.
889
890If Color_Type is kAlpha_8_SkColorType, returns true unless
891alphaType is kUnknown_SkAlphaType and Alpha_Type is not kUnknown_SkAlphaType.
892If Alpha_Type is kUnknown_SkAlphaType, alphaType is ignored. If alphaType is
Cary Clark682c58d2018-05-16 07:07:07 -0400893kUnpremul_SkAlphaType, it is treated as kPremul_SkAlphaType.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400894
895This changes Alpha_Type in Pixel_Ref; all bitmaps sharing Pixel_Ref
896are affected.
897
Cary Clark681287e2018-03-16 11:34:15 -0400898#Param alphaType one of: #list_of_alpha_types#
Cary Clarkbc5697d2017-10-04 14:31:33 -0400899##
900
901#Return true if Alpha_Type is set ##
902
903#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400904void draw(SkCanvas* canvas) {
Cary Clarkab2621d2018-01-30 10:08:57 -0500905 const char* colors[] = { "Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
906 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16" };
Cary Clark682c58d2018-05-16 07:07:07 -0400907 const char* alphas[] = {"Unknown ", "Opaque ", "Premul ", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500908 SkBitmap bitmap;
Cary Clark681287e2018-03-16 11:34:15 -0400909 SkAlphaType alphaTypes[] = { #list_of_alpha_types#
910 };
Cary Clarkab2621d2018-01-30 10:08:57 -0500911 SkDebugf("%88s", "Canonical Unknown Opaque Premul Unpremul\n");
Cary Clark1a8d7622018-03-05 13:26:16 -0500912 for (SkColorType colorType : { #list_of_color_types#
913 } ) {
Ben Wagner29380bd2017-10-09 14:43:00 -0400914 for (SkAlphaType canonicalAlphaType : alphaTypes) {
915 SkColorTypeValidateAlphaType(colorType, kUnknown_SkAlphaType, &canonicalAlphaType );
916 SkDebugf("%10s %10s ", colors[(int) colorType], alphas[(int) canonicalAlphaType ]);
917 for (SkAlphaType alphaType : alphaTypes) {
918 bitmap.setInfo(SkImageInfo::Make(4, 4, colorType, canonicalAlphaType));
919 bool result = bitmap.setAlphaType(alphaType);
920 SkDebugf("%s %s ", result ? "true " : "false", alphas[(int) bitmap.alphaType()]);
921 }
922 SkDebugf("\n");
923 }
924 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400925}
926##
927
928#SeeAlso Alpha_Type Color_Type Image_Info setInfo
929
930##
931
932# ------------------------------------------------------------------------------
933
934#Method void* getPixels() const
Cary Clark78de7512018-02-07 07:27:09 -0500935#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500936#Line # returns address of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400937Returns pixel address, the base address corresponding to the pixel origin.
938
939#Return pixel address ##
940
941#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400942 SkBitmap bitmap;
943 bitmap.setInfo(SkImageInfo::MakeN32(4, 4, kPremul_SkAlphaType));
944 bitmap.allocPixels();
945 bitmap.eraseColor(0x00000000);
946 void* baseAddr = bitmap.getPixels();
947 *(SkPMColor*)baseAddr = 0xFFFFFFFF;
948 SkDebugf("bitmap.getColor(0, 1) %c= 0x00000000\n",
949 bitmap.getColor(0, 1) == 0x00000000 ? '=' : '!');
950 SkDebugf("bitmap.getColor(0, 0) %c= 0xFFFFFFFF\n",
951 bitmap.getColor(0, 0) == 0xFFFFFFFF ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -0400952#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400953bitmap.getColor(0, 1) == 0x00000000
Cary Clarkbc5697d2017-10-04 14:31:33 -0400954bitmap.getColor(0, 0) == 0xFFFFFFFF
955##
956##
957
958#SeeAlso isNull drawsNothing
959
960##
961
962# ------------------------------------------------------------------------------
963
964#Method size_t computeByteSize() const
Cary Clark78de7512018-02-07 07:27:09 -0500965#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -0500966#Line # returns size required for pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400967Returns minimum memory required for pixel storage.
968Does not include unused memory on last row when rowBytesAsPixels exceeds width().
969Returns zero if result does not fit in size_t.
970Returns zero if height() or width() is 0.
971Returns height() times rowBytes if colorType is kUnknown_SkColorType.
972
973#Return size in bytes of image buffer ##
974
975#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400976 SkBitmap bitmap;
Cary Clarkbc5697d2017-10-04 14:31:33 -0400977 for (int width : { 1, 1000, 1000000 } ) {
978 for (int height: { 1, 1000, 1000000 } ) {
979 SkImageInfo imageInfo = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
980 bitmap.setInfo(imageInfo, width * 5);
981 SkDebugf("width: %7d height: %7d computeByteSize: %13lld\n", width, height,
982 bitmap.computeByteSize());
983 }
984 }
985#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400986width: 1 height: 1 computeByteSize: 4
987width: 1 height: 1000 computeByteSize: 4999
988width: 1 height: 1000000 computeByteSize: 4999999
989width: 1000 height: 1 computeByteSize: 4000
990width: 1000 height: 1000 computeByteSize: 4999000
991width: 1000 height: 1000000 computeByteSize: 4999999000
992width: 1000000 height: 1 computeByteSize: 4000000
993width: 1000000 height: 1000 computeByteSize: 4999000000
Cary Clarkbc5697d2017-10-04 14:31:33 -0400994width: 1000000 height: 1000000 computeByteSize: 4999999000000
995##
996##
997
998#SeeAlso SkImageInfo::computeByteSize
999
1000##
1001
1002# ------------------------------------------------------------------------------
1003
Cary Clarkbc5697d2017-10-04 14:31:33 -04001004#Method bool isImmutable() const
Cary Clark78de7512018-02-07 07:27:09 -05001005#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001006#Line # returns true if pixels will not change ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001007Returns true if pixels can not change.
1008
1009Most immutable Bitmap checks trigger an assert only on debug builds.
1010
1011#Return true if pixels are immutable ##
1012
1013#Example
Cary Clark682c58d2018-05-16 07:07:07 -04001014 SkBitmap original;
Ben Wagner29380bd2017-10-09 14:43:00 -04001015 SkImageInfo info = SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1016 if (original.tryAllocPixels(info)) {
1017 original.setImmutable();
1018 SkBitmap copy;
Cary Clark682c58d2018-05-16 07:07:07 -04001019 original.extractSubset(&copy, {5, 10, 15, 20});
Ben Wagner29380bd2017-10-09 14:43:00 -04001020 SkDebugf("original is " "%s" "immutable\n", original.isImmutable() ? "" : "not ");
1021 SkDebugf("copy is " "%s" "immutable\n", copy.isImmutable() ? "" : "not ");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001022 }
1023#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001024original is immutable
Cary Clarkbc5697d2017-10-04 14:31:33 -04001025copy is immutable
1026##
1027##
1028
1029#SeeAlso setImmutable SkPixelRef::isImmutable SkImage
1030
1031##
1032
1033# ------------------------------------------------------------------------------
1034
1035#Method void setImmutable()
Cary Clark78de7512018-02-07 07:27:09 -05001036#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05001037#Line # marks that pixels will not change ##
Cary Clark154beea2017-10-26 07:58:48 -04001038Sets internal flag to mark Bitmap as immutable. Once set, pixels can not change.
Cary Clark682c58d2018-05-16 07:07:07 -04001039Any other bitmap sharing the same Pixel_Ref are also marked as immutable.
Cary Clark154beea2017-10-26 07:58:48 -04001040Once Pixel_Ref is marked immutable, the setting cannot be cleared.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001041
1042Writing to immutable Bitmap pixels triggers an assert on debug builds.
Cary Clark682c58d2018-05-16 07:07:07 -04001043
Cary Clarkbc5697d2017-10-04 14:31:33 -04001044#Example
1045#Description
1046Triggers assert if SK_DEBUG is true, runs fine otherwise.
1047##
Ben Wagner29380bd2017-10-09 14:43:00 -04001048 SkBitmap bitmap;
1049 bitmap.setInfo(SkImageInfo::MakeN32(4, 4, kPremul_SkAlphaType));
1050 bitmap.allocPixels();
1051 SkCanvas offscreen(bitmap);
1052 SkDebugf("draw white\n");
1053 offscreen.clear(SK_ColorWHITE);
1054 bitmap.setImmutable();
1055 SkDebugf("draw black\n");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001056 offscreen.clear(SK_ColorBLACK);
1057##
1058
1059#SeeAlso isImmutable SkPixelRef::setImmutable SkImage
1060
1061##
1062
1063# ------------------------------------------------------------------------------
1064
1065#Method bool isOpaque() const
Cary Clark78de7512018-02-07 07:27:09 -05001066#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001067#Line # returns true if Image_Info describes opaque pixels ##
Cary Clark681287e2018-03-16 11:34:15 -04001068
1069Returns true if Alpha_Type is set to hint that all pixels are opaque; their
1070Color_Alpha value is implicitly or explicitly 1.0. If true, and all pixels are
Cary Clark682c58d2018-05-16 07:07:07 -04001071not opaque, Skia may draw incorrectly.
Cary Clark681287e2018-03-16 11:34:15 -04001072
Cary Clarkbc5697d2017-10-04 14:31:33 -04001073Does not check if Color_Type allows Alpha, or if any pixel value has
1074transparency.
1075
Cary Clark681287e2018-03-16 11:34:15 -04001076#Return true if Image_Info Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001077
1078#Example
1079#Description
1080 isOpaque ignores whether all pixels are opaque or not.
1081##
Ben Wagner29380bd2017-10-09 14:43:00 -04001082 const int height = 2;
1083 const int width = 2;
1084 SkBitmap bitmap;
1085 bitmap.setInfo(SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType));
1086 for (int index = 0; index < 2; ++index) {
1087 bitmap.allocPixels();
1088 bitmap.eraseColor(0x00000000);
1089 SkDebugf("isOpaque: %s\n", bitmap.isOpaque() ? "true" : "false");
1090 bitmap.eraseColor(0xFFFFFFFF);
1091 SkDebugf("isOpaque: %s\n", bitmap.isOpaque() ? "true" : "false");
1092 bitmap.setInfo(bitmap.info().makeAlphaType(kOpaque_SkAlphaType));
1093 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001094#StdOut
1095isOpaque: false
1096isOpaque: false
1097isOpaque: true
1098isOpaque: true
1099##
1100##
1101
1102#SeeAlso ComputeIsOpaque SkImageInfo::isOpaque
1103
1104##
1105
1106# ------------------------------------------------------------------------------
1107
1108#Method bool isVolatile() const
Cary Clark78de7512018-02-07 07:27:09 -05001109#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001110#Line # returns true if pixels should not be cached ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001111If true, provides a hint to caller that pixels should not
1112be cached. Only true if setIsVolatile has been called to mark as volatile.
1113
1114Volatile state is not shared by other bitmaps sharing the same Pixel_Ref.
1115
1116#Return true if marked volatile ##
1117
1118#Example
Cary Clark682c58d2018-05-16 07:07:07 -04001119 SkBitmap original;
Ben Wagner29380bd2017-10-09 14:43:00 -04001120 SkImageInfo info = SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1121 if (original.tryAllocPixels(info)) {
1122 original.setIsVolatile(true);
1123 SkBitmap copy;
Cary Clark682c58d2018-05-16 07:07:07 -04001124 original.extractSubset(&copy, {5, 10, 15, 20});
Ben Wagner29380bd2017-10-09 14:43:00 -04001125 SkDebugf("original is " "%s" "volatile\n", original.isVolatile() ? "" : "not ");
1126 SkDebugf("copy is " "%s" "volatile\n", copy.isImmutable() ? "" : "not ");
1127 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001128#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001129original is volatile
Cary Clarkbc5697d2017-10-04 14:31:33 -04001130copy is not volatile
1131##
1132##
1133
1134#SeeAlso setIsVolatile
1135
1136##
1137
1138# ------------------------------------------------------------------------------
1139
1140#Method void setIsVolatile(bool isVolatile)
Cary Clark78de7512018-02-07 07:27:09 -05001141#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05001142#Line # marks if pixels should not be cached ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001143Sets if pixels should be read from Pixel_Ref on every access. Bitmaps are not
1144volatile by default; a GPU back end may upload pixel values expecting them to be
1145accessed repeatedly. Marking temporary Bitmaps as volatile provides a hint to
1146Device that the Bitmap pixels should not be cached. This can
1147improve performance by avoiding overhead and reducing resource
1148consumption on Device.
1149
1150#Param isVolatile true if backing pixels are temporary ##
1151
1152#Example
1153#Height 20
Cary Clark682c58d2018-05-16 07:07:07 -04001154 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04001155 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
1156 bitmap.allocPixels();
1157 bitmap.eraseColor(SK_ColorRED);
1158 canvas->scale(16, 16);
1159 canvas->drawBitmap(bitmap, 0, 0);
1160 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorBLUE);
1161 canvas->drawBitmap(bitmap, 2, 0);
1162 bitmap.setIsVolatile(true);
1163 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorGREEN);
1164 canvas->drawBitmap(bitmap, 4, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001165##
1166
1167#SeeAlso isVolatile
1168
1169##
1170
1171# ------------------------------------------------------------------------------
1172
1173#Method void reset()
Cary Clark78de7512018-02-07 07:27:09 -05001174#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -05001175#Line # sets to default values, releases pixel ownership ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001176Resets to its initial state; all fields are set to zero, as if Bitmap had
1177been initialized by SkBitmap().
1178
Cary Clark682c58d2018-05-16 07:07:07 -04001179Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to
Cary Clarkbc5697d2017-10-04 14:31:33 -04001180kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType.
1181
1182If Pixel_Ref is allocated, its reference count is decreased by one, releasing
1183its memory if Bitmap is the sole owner.
1184
1185#Example
Cary Clark682c58d2018-05-16 07:07:07 -04001186 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04001187 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
1188 bitmap.allocPixels();
1189 SkDebugf("width:%d height:%d isNull:%s\n", bitmap.width(), bitmap.height(),
1190 bitmap.isNull() ? "true" : "false");
1191 bitmap.reset();
1192 SkDebugf("width:%d height:%d isNull:%s\n", bitmap.width(), bitmap.height(),
1193 bitmap.isNull() ? "true" : "false");
1194#StdOut
1195width:1 height:1 isNull:false
1196width:0 height:0 isNull:true
1197##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001198##
1199
1200#SeeAlso SkBitmap() SkAlphaType SkColorType
1201
1202##
1203
1204# ------------------------------------------------------------------------------
1205
1206#Method static bool ComputeIsOpaque(const SkBitmap& bm)
Cary Clark78de7512018-02-07 07:27:09 -05001207#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05001208#Line # returns true if all pixels are opaque ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001209Returns true if all pixels are opaque. Color_Type determines how pixels
1210are encoded, and whether pixel describes Alpha. Returns true for Color_Types
1211without alpha in each pixel; for other Color_Types, returns true if all
1212pixels have alpha values equivalent to 1.0 or greater.
1213
1214For Color_Types kRGB_565_SkColorType or kGray_8_SkColorType: always
1215returns true. For Color_Types kAlpha_8_SkColorType, kBGRA_8888_SkColorType,
1216kRGBA_8888_SkColorType: returns true if all pixel Alpha values are 255.
1217For Color_Type kARGB_4444_SkColorType: returns true if all pixel Alpha values are 15.
1218For kRGBA_F16_SkColorType: returns true if all pixel Alpha values are 1.0 or
1219greater.
1220
Cary Clark682c58d2018-05-16 07:07:07 -04001221Returns false for kUnknown_SkColorType.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001222
1223#Param bm Bitmap to check ##
1224
1225#Return true if all pixels have opaque values or Color_Type is opaque ##
1226
1227#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001228 SkBitmap bitmap;
1229 bitmap.setInfo(SkImageInfo::Make(2, 2, kN32_SkColorType, kPremul_SkAlphaType));
1230 for (int index = 0; index < 2; ++index) {
1231 bitmap.allocPixels();
1232 bitmap.eraseColor(0x00000000);
1233 SkDebugf("computeIsOpaque: %s\n", SkBitmap::ComputeIsOpaque(bitmap) ? "true" : "false");
1234 bitmap.eraseColor(0xFFFFFFFF);
1235 SkDebugf("computeIsOpaque: %s\n", SkBitmap::ComputeIsOpaque(bitmap) ? "true" : "false");
1236 bitmap.setInfo(bitmap.info().makeAlphaType(kOpaque_SkAlphaType));
Cary Clarkbc5697d2017-10-04 14:31:33 -04001237 }
1238#StdOut
1239computeIsOpaque: false
1240computeIsOpaque: true
1241computeIsOpaque: false
1242computeIsOpaque: true
1243##
1244##
1245
1246#SeeAlso isOpaque Color_Type Alpha
1247
1248##
1249
1250# ------------------------------------------------------------------------------
1251
1252#Method void getBounds(SkRect* bounds) const
Cary Clark78de7512018-02-07 07:27:09 -05001253#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001254#Line # returns width() and height() as Rectangle ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001255Returns Rect { 0, 0, width(), height() }.
1256
1257#Param bounds container for floating point rectangle ##
1258
1259#Example
1260#Height 160
1261#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001262 SkRect bounds;
1263 source.getBounds(&bounds);
1264 bounds.offset(100, 100);
1265 SkPaint paint;
1266 paint.setColor(SK_ColorGRAY);
1267 canvas->scale(.25f, .25f);
1268 canvas->drawRect(bounds, paint);
1269 canvas->drawBitmap(source, 40, 40);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001270##
1271
Cary Clark682c58d2018-05-16 07:07:07 -04001272#SeeAlso bounds()
Cary Clarkbc5697d2017-10-04 14:31:33 -04001273
1274##
1275
1276# ------------------------------------------------------------------------------
1277
1278#Method void getBounds(SkIRect* bounds) const
1279
1280Returns IRect { 0, 0, width(), height() }.
1281
1282#Param bounds container for integral rectangle ##
1283
1284#Example
1285#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001286 SkIRect bounds;
1287 source.getBounds(&bounds);
1288 bounds.inset(100, 100);
1289 SkBitmap bitmap;
1290 source.extractSubset(&bitmap, bounds);
1291 canvas->scale(.5f, .5f);
1292 canvas->drawBitmap(bitmap, 10, 10);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001293##
1294
Cary Clark682c58d2018-05-16 07:07:07 -04001295#SeeAlso bounds()
Cary Clarkbc5697d2017-10-04 14:31:33 -04001296
1297##
1298
1299# ------------------------------------------------------------------------------
1300
1301#Method SkIRect bounds() const
Cary Clark78de7512018-02-07 07:27:09 -05001302#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001303#Line # returns width() and height() as Rectangle ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001304Returns IRect { 0, 0, width(), height() }.
1305
1306#Return integral rectangle from origin to width() and height() ##
1307
1308#Example
Cary Clark681287e2018-03-16 11:34:15 -04001309#Height 64
Cary Clarkbc5697d2017-10-04 14:31:33 -04001310#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -05001311 canvas->scale(.5f, .5f);
Ben Wagner29380bd2017-10-09 14:43:00 -04001312 SkIRect bounds = source.bounds();
1313 for (int x : { 0, bounds.width() } ) {
1314 for (int y : { 0, bounds.height() } ) {
1315 canvas->drawBitmap(source, x, y);
1316 }
1317 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001318##
1319
Cary Clark682c58d2018-05-16 07:07:07 -04001320#SeeAlso getBounds
Cary Clarkbc5697d2017-10-04 14:31:33 -04001321
1322##
1323
1324# ------------------------------------------------------------------------------
1325
1326#Method SkISize dimensions() const
Cary Clark78de7512018-02-07 07:27:09 -05001327#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001328#Line # returns width() and height() ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001329Returns ISize { width(), height() }.
1330
1331#Return integral size of width() and height() ##
1332
1333#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001334 SkBitmap bitmap;
1335 bitmap.setInfo(SkImageInfo::MakeN32(33, 55, kOpaque_SkAlphaType));
1336 SkISize dimensions = bitmap.dimensions();
1337 SkRect bounds;
1338 bitmap.getBounds(&bounds);
1339 SkRect dimensionsAsBounds = SkRect::Make(dimensions);
1340 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -04001341##
1342
1343#SeeAlso height() width()
1344
1345##
1346
1347# ------------------------------------------------------------------------------
1348
1349#Method SkIRect getSubset() const
Cary Clark78de7512018-02-07 07:27:09 -05001350#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001351#Line # returns bounds offset by origin ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001352Returns the bounds of this bitmap, offset by its Pixel_Ref origin.
1353
1354#Return bounds within Pixel_Ref bounds ##
1355
1356#Example
1357#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001358 SkIRect bounds;
1359 source.getBounds(&bounds);
1360 bounds.inset(100, 100);
1361 SkBitmap subset;
1362 source.extractSubset(&subset, bounds);
1363 SkIRect r = source.getSubset();
1364 SkDebugf("source: %d, %d, %d, %d\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
1365 r = subset.getSubset();
1366 SkDebugf("subset: %d, %d, %d, %d\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
1367#StdOut
1368source: 0, 0, 512, 512
1369subset: 100, 100, 412, 412
1370##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001371##
1372
1373#SeeAlso extractSubset getBounds
1374
1375##
1376
1377# ------------------------------------------------------------------------------
1378
1379#Method bool setInfo(const SkImageInfo& imageInfo, size_t rowBytes = 0)
Cary Clark78de7512018-02-07 07:27:09 -05001380#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05001381#Line # sets height, width, Color_Type, and so on, releasing pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001382Sets width, height, Alpha_Type, Color_Type, Color_Space, and optional
1383rowBytes. Frees pixels, and returns true if successful.
1384
1385imageInfo.alphaType may be altered to a value permitted by imageInfo.colorSpace.
Cary Clark682c58d2018-05-16 07:07:07 -04001386If imageInfo.colorType is kUnknown_SkColorType, imageInfo.alphaType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04001387set to kUnknown_SkAlphaType.
1388If imageInfo.colorType is kAlpha_8_SkColorType and imageInfo.alphaType is
1389kUnpremul_SkAlphaType, imageInfo.alphaType is replaced by kPremul_SkAlphaType.
1390If imageInfo.colorType is kRGB_565_SkColorType or kGray_8_SkColorType,
1391imageInfo.alphaType is set to kOpaque_SkAlphaType.
1392If imageInfo.colorType is kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
1393kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: imageInfo.alphaType remains
1394unchanged.
1395
1396rowBytes must equal or exceed imageInfo.minRowBytes. If imageInfo.colorSpace is
1397kUnknown_SkColorType, rowBytes is ignored and treated as zero; for all other
1398Color_Space values, rowBytes of zero is treated as imageInfo.minRowBytes.
1399
1400Calls reset() and returns false if:
1401#List
1402# rowBytes exceeds 31 bits ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001403# imageInfo.width() is negative ##
1404# imageInfo.height() is negative ##
1405# rowBytes is positive and less than imageInfo.width() times imageInfo.bytesPerPixel ##
1406##
1407
1408#Param imageInfo contains width, height, Alpha_Type, Color_Type, Color_Space ##
1409#Param rowBytes imageInfo.minRowBytes or larger; or zero ##
1410
1411#Return true if Image_Info set successfully ##
1412
1413#Example
1414#Height 96
1415###^
Ben Wagner29380bd2017-10-09 14:43:00 -04001416SkBitmap bitmap;
1417bitmap.setInfo(SkImageInfo::MakeN32(44, 16, kOpaque_SkAlphaType));
1418bitmap.allocPixels();
1419bitmap.eraseColor(SK_ColorGREEN);
1420SkCanvas offscreen(bitmap);
1421SkPaint paint;
1422offscreen.drawString("!@#$%", 0, 12, paint);
1423canvas->scale(6, 6);
1424canvas->drawBitmap(bitmap, 0, 0);
1425^^^#
Cary Clarkbc5697d2017-10-04 14:31:33 -04001426##
1427
1428#SeeAlso Alpha_Type Color_Type Color_Space height rowBytes width
1429
1430##
1431
1432# ------------------------------------------------------------------------------
1433
1434#Enum AllocFlags
Cary Clark4855f782018-02-06 09:41:53 -05001435#Line # zero pixel memory ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001436#Code
1437 enum AllocFlags {
1438 kZeroPixels_AllocFlag = 1 << 0,
1439 };
1440##
1441
1442AllocFlags provides the option to zero pixel memory when allocated.
1443
1444#Const kZeroPixels_AllocFlag 1
Cary Clark682c58d2018-05-16 07:07:07 -04001445#Line # zero pixel memory ##
1446 Instructs tryAllocPixelsFlags and allocPixelsFlags to zero pixel memory.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001447##
1448
1449#NoExample
1450##
1451
1452#SeeAlso tryAllocPixelsFlags allocPixelsFlags erase() eraseColor
1453
1454##
1455
1456# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001457#Subtopic Allocate
1458#Populate
1459#Line # allocates storage for pixels ##
1460##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001461
1462#Method bool SK_WARN_UNUSED_RESULT tryAllocPixelsFlags(const SkImageInfo& info, uint32_t flags)
Cary Clark78de7512018-02-07 07:27:09 -05001463#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001464#Line # allocates pixels from Image_Info with options if possible ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001465Sets Image_Info to info following the rules in setInfo and allocates pixel
Cary Clark682c58d2018-05-16 07:07:07 -04001466memory. If flags is kZeroPixels_AllocFlag, memory is zeroed.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001467
1468Returns false and calls reset() if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001469not be allocated, or memory could not optionally be zeroed.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001470
1471On most platforms, allocating pixel memory may succeed even though there is
1472not sufficient memory to hold pixels; allocation does not take place
1473until the pixels are written to. The actual behavior depends on the platform
1474implementation of malloc(), if flags is zero, and calloc(), if flags is
1475kZeroPixels_AllocFlag.
1476
Cary Clark186d08f2018-04-03 08:43:27 -04001477flags set to kZeroPixels_AllocFlag offers equal or better performance than
1478subsequently calling eraseColor with SK_ColorTRANSPARENT.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001479
1480#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1481#Param flags kZeroPixels_AllocFlag, or zero ##
1482
1483#Return true if pixels allocation is successful ##
1484
1485#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001486 SkBitmap bitmap;
Cary Clark682c58d2018-05-16 07:07:07 -04001487 if (!bitmap.tryAllocPixelsFlags(SkImageInfo::MakeN32(10000, 10000, kOpaque_SkAlphaType),
Cary Clarka560c472017-11-27 10:44:06 -05001488 SkBitmap::kZeroPixels_AllocFlag)) {
1489 SkDebugf("bitmap allocation failed!\n");
1490 } else {
1491 SkDebugf("bitmap allocation succeeded!\n");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001492 }
1493#StdOut
Cary Clarka560c472017-11-27 10:44:06 -05001494bitmap allocation succeeded!
Cary Clarkbc5697d2017-10-04 14:31:33 -04001495##
1496##
1497
1498#SeeAlso allocPixelsFlags tryAllocPixels SkMallocPixelRef::MakeZeroed
1499
1500##
1501
1502# ------------------------------------------------------------------------------
1503
1504#Method void allocPixelsFlags(const SkImageInfo& info, uint32_t flags)
Cary Clark78de7512018-02-07 07:27:09 -05001505#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001506#Line # allocates pixels from Image_Info with options, or aborts ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001507Sets Image_Info to info following the rules in setInfo and allocates pixel
Cary Clark682c58d2018-05-16 07:07:07 -04001508memory. If flags is kZeroPixels_AllocFlag, memory is zeroed.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001509
1510Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001511not be allocated, or memory could not optionally
Cary Clarkbc5697d2017-10-04 14:31:33 -04001512be zeroed. Abort steps may be provided by the user at compile time by defining
1513SK_ABORT.
1514
1515On most platforms, allocating pixel memory may succeed even though there is
1516not sufficient memory to hold pixels; allocation does not take place
1517until the pixels are written to. The actual behavior depends on the platform
1518implementation of malloc(), if flags is zero, and calloc(), if flags is
1519kZeroPixels_AllocFlag.
1520
Cary Clark186d08f2018-04-03 08:43:27 -04001521flags set to kZeroPixels_AllocFlag offers equal or better performance than
1522subsequently calling eraseColor with SK_ColorTRANSPARENT.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001523
1524#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1525#Param flags kZeroPixels_AllocFlag, or zero ##
1526
1527#Example
1528#Height 128
1529#Description
1530Text is drawn on a transparent background; drawing the bitmap a second time
1531lets the first draw show through.
1532##
1533###^
Ben Wagner29380bd2017-10-09 14:43:00 -04001534SkBitmap bitmap;
Cary Clark682c58d2018-05-16 07:07:07 -04001535bitmap.allocPixelsFlags(SkImageInfo::MakeN32(44, 16, kPremul_SkAlphaType),
Ben Wagner29380bd2017-10-09 14:43:00 -04001536 SkBitmap::kZeroPixels_AllocFlag);
1537SkCanvas offscreen(bitmap);
1538SkPaint paint;
1539offscreen.drawString("!@#$%", 0, 12, paint);
1540canvas->scale(6, 6);
1541canvas->drawBitmap(bitmap, 0, 0);
1542canvas->drawBitmap(bitmap, 8, 8);
1543^^^#
Cary Clarkbc5697d2017-10-04 14:31:33 -04001544##
1545
1546#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeZeroed
1547
1548##
1549
1550# ------------------------------------------------------------------------------
1551
1552#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info, size_t rowBytes)
Cary Clark78de7512018-02-07 07:27:09 -05001553#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001554#Line # allocates pixels from Image_Info if possible ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001555#ToDo am I ever conflicted about setInfo rules. It needs to be able to be replicated
1556 if, for instance, I generate one-page-per-method HTML-style documentation
1557 I'm not so sure it makes sense to put the indirection in for .h either unless
1558 my mantra is that .h should abbreviate full documentation. And, what to do
1559 for generated markdown? At least there the rules are a click away, although
1560 a pop-down in place would be way better. Hmmm.
1561##
1562
1563Sets Image_Info to info following the rules in setInfo and allocates pixel
1564memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(),
1565or equal zero. Pass in zero for rowBytes to compute the minimum valid value.
1566
1567Returns false and calls reset() if Image_Info could not be set, or memory could
1568not be allocated.
1569
1570On most platforms, allocating pixel memory may succeed even though there is
1571not sufficient memory to hold pixels; allocation does not take place
1572until the pixels are written to. The actual behavior depends on the platform
1573implementation of malloc().
1574
1575#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1576#Param rowBytes size of pixel row or larger; may be zero ##
1577
1578#Return true if pixel storage is allocated ##
1579
1580#Example
1581#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001582SkBitmap bitmap;
1583SkImageInfo info = SkImageInfo::Make(64, 256, kGray_8_SkColorType, kOpaque_SkAlphaType);
1584if (bitmap.tryAllocPixels(info, 0)) {
1585 SkCanvas offscreen(bitmap);
1586 offscreen.scale(.5f, .5f);
1587 for (int x : { 0, 64, 128, 192 } ) {
1588 offscreen.drawBitmap(source, -x, 0);
1589 canvas->drawBitmap(bitmap, x, 0);
1590 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001591}
1592##
1593
1594#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeAllocate
1595
1596##
1597
1598# ------------------------------------------------------------------------------
1599
1600#Method void allocPixels(const SkImageInfo& info, size_t rowBytes)
Cary Clark78de7512018-02-07 07:27:09 -05001601#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001602#Line # allocates pixels from Image_Info, or aborts ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001603Sets Image_Info to info following the rules in setInfo and allocates pixel
1604memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(),
1605or equal zero. Pass in zero for rowBytes to compute the minimum valid value.
1606
1607Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001608not be allocated. Abort steps may be provided by
Cary Clarkbc5697d2017-10-04 14:31:33 -04001609the user at compile time by defining SK_ABORT.
1610
1611On most platforms, allocating pixel memory may succeed even though there is
1612not sufficient memory to hold pixels; allocation does not take place
1613until the pixels are written to. The actual behavior depends on the platform
1614implementation of malloc().
1615
1616#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1617#Param rowBytes size of pixel row or larger; may be zero ##
1618
1619#Example
1620#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001621SkBitmap bitmap;
1622SkImageInfo info = SkImageInfo::Make(256, 64, kGray_8_SkColorType, kOpaque_SkAlphaType);
1623bitmap.allocPixels(info, info.width() * info.bytesPerPixel() + 64);
1624SkCanvas offscreen(bitmap);
1625offscreen.scale(.5f, .5f);
1626for (int y : { 0, 64, 128, 192 } ) {
1627 offscreen.drawBitmap(source, 0, -y);
1628 canvas->drawBitmap(bitmap, 0, y);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001629}
1630##
1631
1632#SeeAlso tryAllocPixels allocPixelsFlags SkMallocPixelRef::MakeAllocate
1633
1634##
1635
1636# ------------------------------------------------------------------------------
1637
1638#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info)
1639
1640Sets Image_Info to info following the rules in setInfo and allocates pixel
Cary Clark682c58d2018-05-16 07:07:07 -04001641memory.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001642
1643Returns false and calls reset() if Image_Info could not be set, or memory could
1644not be allocated.
1645
1646On most platforms, allocating pixel memory may succeed even though there is
1647not sufficient memory to hold pixels; allocation does not take place
1648until the pixels are written to. The actual behavior depends on the platform
1649implementation of malloc().
1650
1651#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1652
1653#Return true if pixel storage is allocated ##
1654
1655#Example
1656#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001657SkBitmap bitmap;
1658if (bitmap.tryAllocPixels(SkImageInfo::Make(64, 64, kGray_8_SkColorType, kOpaque_SkAlphaType))) {
1659 SkCanvas offscreen(bitmap);
1660 offscreen.scale(.25f, .5f);
1661 for (int y : { 0, 64, 128, 192 } ) {
1662 offscreen.drawBitmap(source, -y, -y);
1663 canvas->drawBitmap(bitmap, y, y);
1664 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001665}
1666##
1667
1668#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeAllocate
1669
1670##
1671
1672# ------------------------------------------------------------------------------
1673
1674#Method void allocPixels(const SkImageInfo& info)
1675
1676Sets Image_Info to info following the rules in setInfo and allocates pixel
Cary Clark682c58d2018-05-16 07:07:07 -04001677memory.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001678
1679Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001680not be allocated. Abort steps may be provided by
Cary Clarkbc5697d2017-10-04 14:31:33 -04001681the user at compile time by defining SK_ABORT.
1682
1683On most platforms, allocating pixel memory may succeed even though there is
1684not sufficient memory to hold pixels; allocation does not take place
1685until the pixels are written to. The actual behavior depends on the platform
1686implementation of malloc().
1687
1688#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1689
1690#Example
1691#Image 4
Ben Wagner29380bd2017-10-09 14:43:00 -04001692SkBitmap bitmap;
1693bitmap.allocPixels(SkImageInfo::Make(64, 64, kGray_8_SkColorType, kOpaque_SkAlphaType));
1694SkCanvas offscreen(bitmap);
1695offscreen.scale(.5f, .5f);
1696for (int y : { 0, 64, 128, 192 } ) {
1697 offscreen.drawBitmap(source, -y, -y);
1698 canvas->drawBitmap(bitmap, y, y);
1699}
Cary Clarkbc5697d2017-10-04 14:31:33 -04001700##
1701
1702#SeeAlso tryAllocPixels allocPixelsFlags SkMallocPixelRef::MakeAllocate
1703
1704##
1705
1706# ------------------------------------------------------------------------------
1707
1708#Method bool SK_WARN_UNUSED_RESULT tryAllocN32Pixels(int width, int height, bool isOpaque = false)
Cary Clark78de7512018-02-07 07:27:09 -05001709#In Allocate
Cary Clarkffb3d682018-05-17 12:17:28 -04001710#Line # allocates compatible ARGB pixels if possible ##
Cary Clark682c58d2018-05-16 07:07:07 -04001711Sets Image_Info to width, height, and Native_Color_Type; and allocates
Cary Clarkbc5697d2017-10-04 14:31:33 -04001712pixel memory. If isOpaque is true, sets Image_Info to kOpaque_SkAlphaType;
1713otherwise, sets to kPremul_SkAlphaType.
1714
1715Calls reset() and returns false if width exceeds 29 bits or is negative,
1716or height is negative.
1717
1718Returns false if allocation fails.
1719
Cary Clarka560c472017-11-27 10:44:06 -05001720Use to create Bitmap that matches SkPMColor, the native pixel arrangement on
1721the platform. Bitmap drawn to output device skips converting its pixel format.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001722
1723#Param width pixel column count; must be zero or greater ##
1724#Param height pixel row count; must be zero or greater ##
1725#Param isOpaque true if pixels do not have transparency ##
1726
1727#Return true if pixel storage is allocated ##
1728
1729#Example
1730#Height 160
Ben Wagner29380bd2017-10-09 14:43:00 -04001731 SkBitmap bitmap;
1732 if (bitmap.tryAllocN32Pixels(80, 80)) {
1733 bitmap.eraseColor(SK_ColorTRANSPARENT);
1734 bitmap.erase(0x7f3f7fff, SkIRect::MakeWH(50, 30));
1735 bitmap.erase(0x3f7fff3f, SkIRect::MakeXYWH(20, 10, 50, 30));
1736 bitmap.erase(0x5fff3f7f, SkIRect::MakeXYWH(40, 20, 50, 30));
1737 canvas->drawBitmap(bitmap, 0, 0);
1738 for (int x : { 0, 30, 60, 90 } ) {
1739 canvas->drawBitmap(bitmap, x, 70);
1740 }
1741 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001742##
1743
1744#SeeAlso tryAllocPixels allocN32Pixels SkMallocPixelRef::MakeAllocate
1745
1746##
1747
1748# ------------------------------------------------------------------------------
1749
1750#Method void allocN32Pixels(int width, int height, bool isOpaque = false)
Cary Clark78de7512018-02-07 07:27:09 -05001751#In Allocate
Cary Clarkffb3d682018-05-17 12:17:28 -04001752#Line # allocates compatible ARGB pixels, or aborts ##
Cary Clark682c58d2018-05-16 07:07:07 -04001753Sets Image_Info to width, height, and the Native_Color_Type; and allocates
Cary Clarkbc5697d2017-10-04 14:31:33 -04001754pixel memory. If isOpaque is true, sets Image_Info to kPremul_SkAlphaType;
1755otherwise, sets to kOpaque_SkAlphaType.
1756
1757Aborts if width exceeds 29 bits or is negative, or height is negative, or
1758allocation fails. Abort steps may be provided by the user at compile time by
1759defining SK_ABORT.
1760
Cary Clarka560c472017-11-27 10:44:06 -05001761Use to create Bitmap that matches SkPMColor, the native pixel arrangement on
1762the platform. Bitmap drawn to output device skips converting its pixel format.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001763
1764#Param width pixel column count; must be zero or greater ##
1765#Param height pixel row count; must be zero or greater ##
1766#Param isOpaque true if pixels do not have transparency ##
1767
1768#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001769 SkRandom random;
1770 SkBitmap bitmap;
1771 bitmap.allocN32Pixels(64, 64);
1772 bitmap.eraseColor(SK_ColorTRANSPARENT);
1773 for (int y = 0; y < 256; y += 64) {
1774 for (int x = 0; x < 256; x += 64) {
1775 SkColor color = random.nextU();
1776 uint32_t w = random.nextRangeU(4, 32);
1777 uint32_t cx = random.nextRangeU(0, 64 - w);
1778 uint32_t h = random.nextRangeU(4, 32);
1779 uint32_t cy = random.nextRangeU(0, 64 - h);
1780 bitmap.erase(color, SkIRect::MakeXYWH(cx, cy, w, h));
1781 canvas->drawBitmap(bitmap, x, y);
1782 }
1783 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001784##
1785
1786#SeeAlso allocPixels tryAllocN32Pixels SkMallocPixelRef::MakeAllocate
1787
1788##
1789
1790# ------------------------------------------------------------------------------
1791
1792#Method bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
1793 void (*releaseProc)(void* addr, void* context), void* context)
Cary Clark78de7512018-02-07 07:27:09 -05001794#In Allocate
Cary Clarkab2621d2018-01-30 10:08:57 -05001795#Line # creates Pixel_Ref, with optional release function ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001796
1797Sets Image_Info to info following the rules in setInfo, and creates Pixel_Ref
Cary Clark682c58d2018-05-16 07:07:07 -04001798containing pixels and rowBytes. releaseProc, if not nullptr, is called
Cary Clarkbc5697d2017-10-04 14:31:33 -04001799immediately on failure or when pixels are no longer referenced. context may be
1800nullptr.
1801
1802If Image_Info could not be set, or rowBytes is less than info.minRowBytes:
1803calls releaseProc if present, calls reset(), and returns false.
1804
1805Otherwise, if pixels equals nullptr: sets Image_Info, calls releaseProc if
1806present, returns true.
1807
1808If Image_Info is set, pixels is not nullptr, and releaseProc is not nullptr:
1809when pixels are no longer referenced, calls releaseProc with pixels and context
1810as parameters.
1811
1812#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1813#Param pixels address or pixel storage; may be nullptr ##
1814#Param rowBytes size of pixel row or larger ##
1815#Param releaseProc function called when pixels can be deleted; may be nullptr ##
1816#Param context caller state passed to releaseProc; may be nullptr ##
1817
1818#Return true if Image_Info is set to info ##
1819
1820#Example
1821#Description
1822releaseProc is called immediately because rowBytes is too small for Pixel_Ref.
1823##
1824#Function
Ben Wagner29380bd2017-10-09 14:43:00 -04001825static void releaseProc(void* addr, void* ) {
1826 SkDebugf("releaseProc called\n");
Cary Clark682c58d2018-05-16 07:07:07 -04001827 delete[] (uint32_t*) addr;
Ben Wagner29380bd2017-10-09 14:43:00 -04001828}
1829
1830##
1831
1832void draw(SkCanvas* canvas) {
1833 SkBitmap bitmap;
1834 void* pixels = new uint32_t[8 * 8];
1835 SkImageInfo info = SkImageInfo::MakeN32(8, 8, kOpaque_SkAlphaType);
1836 SkDebugf("before installPixels\n");
1837 bool installed = bitmap.installPixels(info, pixels, 16, releaseProc, nullptr);
1838 SkDebugf("install " "%s" "successful\n", installed ? "" : "not ");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001839}
1840#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001841before installPixels
1842releaseProc called
Cary Clarkbc5697d2017-10-04 14:31:33 -04001843install not successful
1844##
1845##
1846
1847#SeeAlso allocPixels
1848
1849##
1850
1851# ------------------------------------------------------------------------------
1852
1853#Method bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes)
1854
1855Sets Image_Info to info following the rules in setInfo, and creates Pixel_Ref
1856containing pixels and rowBytes.
1857
1858If Image_Info could not be set, or rowBytes is less than info.minRowBytes:
1859calls reset(), and returns false.
1860
1861Otherwise, if pixels equals nullptr: sets Image_Info, returns true.
1862
1863Caller must ensure that pixels are valid for the lifetime of Bitmap and Pixel_Ref.
1864
1865#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1866#Param pixels address or pixel storage; may be nullptr ##
1867#Param rowBytes size of pixel row or larger ##
1868
1869#Return true if Image_Info is set to info ##
1870
1871#Example
Cary Clark4855f782018-02-06 09:41:53 -05001872#Bug 7079
Cary Clarkbc5697d2017-10-04 14:31:33 -04001873#Description
Cary Clarkbc5697d2017-10-04 14:31:33 -04001874GPU does not support kUnpremul_SkAlphaType, does not assert that it does not.
1875##
Ben Wagner29380bd2017-10-09 14:43:00 -04001876void draw(SkCanvas* canvas) {
1877 SkRandom random;
1878 SkBitmap bitmap;
1879 const int width = 8;
1880 const int height = 8;
1881 uint32_t pixels[width * height];
1882 for (unsigned x = 0; x < width * height; ++x) {
1883 pixels[x] = random.nextU();
1884 }
1885 SkImageInfo info = SkImageInfo::MakeN32(width, height, kUnpremul_SkAlphaType);
1886 if (bitmap.installPixels(info, pixels, info.minRowBytes())) {
1887 canvas->scale(32, 32);
1888 canvas->drawBitmap(bitmap, 0, 0);
1889 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001890}
1891##
1892
1893#SeeAlso allocPixels
1894
1895##
1896
1897# ------------------------------------------------------------------------------
1898
1899#Method bool installPixels(const SkPixmap& pixmap)
1900
1901Sets Image_Info to pixmap.info() following the rules in setInfo, and creates
1902Pixel_Ref containing pixmap.addr() and pixmap.rowBytes.
1903
1904If Image_Info could not be set, or pixmap.rowBytes is less than
1905SkImageInfo::minRowBytes: calls reset(), and returns false.
1906
1907Otherwise, if pixmap.addr() equals nullptr: sets Image_Info, returns true.
1908
1909Caller must ensure that pixmap is valid for the lifetime of Bitmap and Pixel_Ref.
1910
1911#Param pixmap Image_Info, pixel address, and rowBytes ##
1912
1913#Return true if Image_Info was set to pixmap.info() ##
1914
1915#Example
1916#Description
1917Draw a five by five bitmap, and draw it again with a center white pixel.
1918##
1919#Height 64
1920 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
1921 { 0xAC, 0xA8, 0x89, 0x47, 0x87 },
1922 { 0x4B, 0x25, 0x25, 0x25, 0x46 },
1923 { 0x90, 0x81, 0x25, 0x41, 0x33 },
1924 { 0x75, 0x55, 0x44, 0x20, 0x00 }};
1925 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
1926 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
1927 SkBitmap bitmap;
1928 bitmap.installPixels(pixmap);
1929 canvas->scale(10, 10);
1930 canvas->drawBitmap(bitmap, 0, 0);
1931 *pixmap.writable_addr8(2, 2) = 0xFF;
1932 bitmap.installPixels(pixmap);
1933 canvas->drawBitmap(bitmap, 10, 0);
1934##
1935
1936#SeeAlso allocPixels
1937
1938##
1939
1940# ------------------------------------------------------------------------------
1941
1942#Method bool installMaskPixels(const SkMask& mask)
Cary Clark4855f782018-02-06 09:41:53 -05001943#Deprecated soon
Cary Clarkbc5697d2017-10-04 14:31:33 -04001944##
1945
1946# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001947#Subtopic Pixels
1948#Populate
1949#Line # read and write pixel values ##
1950##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001951
1952#Method void setPixels(void* pixels)
Cary Clark78de7512018-02-07 07:27:09 -05001953#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05001954#Line # sets Pixel_Ref without an offset ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001955Replaces Pixel_Ref with pixels, preserving Image_Info and rowBytes.
1956Sets Pixel_Ref origin to (0, 0).
1957
1958If pixels is nullptr, or if info().colorType equals kUnknown_SkColorType;
1959release reference to Pixel_Ref, and set Pixel_Ref to nullptr.
1960
1961Caller is responsible for handling ownership pixel memory for the lifetime
1962of Bitmap and Pixel_Ref.
1963
1964#Param pixels address of pixel storage, managed by caller ##
1965
1966#Example
1967#Height 50
Ben Wagner29380bd2017-10-09 14:43:00 -04001968 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
1969 uint8_t set2[5] = { 0xAC, 0xA8, 0x89, 0x47, 0x87 };
1970 SkBitmap bitmap;
1971 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
1972 canvas->scale(10, 50);
1973 canvas->drawBitmap(bitmap, 0, 0);
1974 bitmap.setPixels(set2);
1975 canvas->drawBitmap(bitmap, 10, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001976##
1977
1978#SeeAlso installPixels allocPixels
1979
1980##
1981
1982# ------------------------------------------------------------------------------
1983
1984#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels()
Cary Clark78de7512018-02-07 07:27:09 -05001985#In Allocate
Cary Clarkbc5697d2017-10-04 14:31:33 -04001986Allocates pixel memory with HeapAllocator, and replaces existing Pixel_Ref.
1987The allocation size is determined by Image_Info width, height, and Color_Type.
1988
Cary Clarka560c472017-11-27 10:44:06 -05001989Returns false if info().colorType is kUnknown_SkColorType, or allocation fails.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001990
1991#Return true if the allocation succeeds
1992##
1993
1994#Example
1995#Height 50
Cary Clark682c58d2018-05-16 07:07:07 -04001996#Description
Cary Clarkbc5697d2017-10-04 14:31:33 -04001997Bitmap hosts and draws gray values in set1. tryAllocPixels replaces Pixel_Ref
1998and erases it to black, but does not alter set1. setPixels replaces black
Cary Clark682c58d2018-05-16 07:07:07 -04001999Pixel_Ref with set1.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002000##
Ben Wagner29380bd2017-10-09 14:43:00 -04002001 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
2002 SkBitmap bitmap;
2003 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
2004 canvas->scale(10, 50);
2005 canvas->drawBitmap(bitmap, 0, 0);
2006 if (bitmap.tryAllocPixels()) {
2007 bitmap.eraseColor(SK_ColorBLACK);
2008 canvas->drawBitmap(bitmap, 8, 0);
2009 bitmap.setPixels(set1);
2010 canvas->drawBitmap(bitmap, 16, 0);
2011 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002012##
2013
2014#SeeAlso allocPixels installPixels setPixels
2015
2016##
2017
2018# ------------------------------------------------------------------------------
2019
2020#Method void allocPixels()
Cary Clark78de7512018-02-07 07:27:09 -05002021#In Allocate
Cary Clarkbc5697d2017-10-04 14:31:33 -04002022Allocates pixel memory with HeapAllocator, and replaces existing Pixel_Ref.
2023The allocation size is determined by Image_Info width, height, and Color_Type.
2024
Cary Clarka560c472017-11-27 10:44:06 -05002025Aborts if info().colorType is kUnknown_SkColorType, or allocation fails.
2026Abort steps may be provided by the user at compile
Cary Clarkbc5697d2017-10-04 14:31:33 -04002027time by defining SK_ABORT.
2028
2029#Example
2030#Height 50
Cary Clark682c58d2018-05-16 07:07:07 -04002031#Description
Cary Clarkbc5697d2017-10-04 14:31:33 -04002032Bitmap hosts and draws gray values in set1. allocPixels replaces Pixel_Ref
2033and erases it to black, but does not alter set1. setPixels replaces black
Cary Clark682c58d2018-05-16 07:07:07 -04002034Pixel_Ref with set2.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002035##
Ben Wagner29380bd2017-10-09 14:43:00 -04002036 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
2037 uint8_t set2[5] = { 0xAC, 0xA8, 0x89, 0x47, 0x87 };
2038 SkBitmap bitmap;
2039 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
2040 canvas->scale(10, 50);
2041 canvas->drawBitmap(bitmap, 0, 0);
2042 bitmap.allocPixels();
2043 bitmap.eraseColor(SK_ColorBLACK);
2044 canvas->drawBitmap(bitmap, 8, 0);
2045 bitmap.setPixels(set2);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002046 canvas->drawBitmap(bitmap, 16, 0);
2047##
2048
2049#SeeAlso tryAllocPixels installPixels setPixels
2050
2051##
2052
2053# ------------------------------------------------------------------------------
2054
2055#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(Allocator* allocator)
2056
2057Allocates pixel memory with allocator, and replaces existing Pixel_Ref.
2058The allocation size is determined by Image_Info width, height, and Color_Type.
2059If allocator is nullptr, use HeapAllocator instead.
2060
Cary Clark78de7512018-02-07 07:27:09 -05002061Returns false if Allocator::allocPixelRef return false.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002062
2063#Param allocator instance of SkBitmap::Allocator instantiation ##
2064
2065#Return true if custom allocator reports success
2066##
2067
2068#Example
2069#Height 100
2070#Description
2071HeapAllocator limits the maximum size of Bitmap to two gigabytes. Using
2072a custom allocator, this limitation may be relaxed. This example can be
Cary Clark682c58d2018-05-16 07:07:07 -04002073modified to allocate an eight gigabyte Bitmap on a 64-bit platform with
Cary Clarkbc5697d2017-10-04 14:31:33 -04002074sufficient memory.
2075##
2076#Function
2077class LargePixelRef : public SkPixelRef {
2078public:
2079 LargePixelRef(const SkImageInfo& info, char* storage, size_t rowBytes)
2080 : SkPixelRef(info.width(), info.height(), storage, rowBytes) {
2081 }
2082
2083 ~LargePixelRef() override {
2084 delete[] (char* ) this->pixels();
2085 }
2086};
2087
2088class LargeAllocator : public SkBitmap::Allocator {
2089public:
2090 bool allocPixelRef(SkBitmap* bitmap) override {
2091 const SkImageInfo& info = bitmap->info();
2092 uint64_t rowBytes = info.minRowBytes64();
2093 uint64_t size = info.height() * rowBytes;
2094 char* addr = new char[size];
2095 if (nullptr == addr) {
2096 return false;
2097 }
2098 sk_sp<SkPixelRef> pr = sk_sp<SkPixelRef>(new LargePixelRef(info, addr, rowBytes));
2099 if (!pr) {
2100 return false;
2101 }
2102 bitmap->setPixelRef(std::move(pr), 0, 0);
2103 return true;
2104 }
2105};
2106
2107##
2108
2109void draw(SkCanvas* canvas) {
2110 LargeAllocator largeAllocator;
2111 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002112 int width = 100; // make this 20000
2113 int height = 100; // and this 100000 to allocate 8 gigs on a 64-bit platform
2114 bitmap.setInfo(SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType));
2115 if (bitmap.tryAllocPixels(&largeAllocator)) {
2116 bitmap.eraseColor(0xff55aa33);
2117 canvas->drawBitmap(bitmap, 0, 0);
2118 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002119}
2120
2121##
2122
2123#SeeAlso allocPixels Allocator Pixel_Ref
2124
2125##
2126
2127# ------------------------------------------------------------------------------
2128
2129#Method void allocPixels(Allocator* allocator)
2130
2131Allocates pixel memory with allocator, and replaces existing Pixel_Ref.
2132The allocation size is determined by Image_Info width, height, and Color_Type.
2133If allocator is nullptr, use HeapAllocator instead.
2134
Cary Clark78de7512018-02-07 07:27:09 -05002135Aborts if Allocator::allocPixelRef return false. Abort steps may be provided by
Cary Clarkbc5697d2017-10-04 14:31:33 -04002136the user at compile time by defining SK_ABORT.
2137
2138#Param allocator instance of SkBitmap::Allocator instantiation ##
2139
2140#Example
2141#Height 32
2142#Function
2143class TinyAllocator : public SkBitmap::Allocator {
2144public:
2145 bool allocPixelRef(SkBitmap* bitmap) override {
2146 const SkImageInfo& info = bitmap->info();
2147 if (info.height() * info.minRowBytes() > sizeof(storage)) {
2148 return false;
2149 }
2150 sk_sp<SkPixelRef> pr = sk_sp<SkPixelRef>(
2151 new SkPixelRef(info.width(), info.height(), storage, info.minRowBytes()));
2152 bitmap->setPixelRef(std::move(pr), 0, 0);
2153 return true;
2154 }
2155
2156 char storage[16];
2157};
2158
2159##
2160
2161void draw(SkCanvas* canvas) {
2162 TinyAllocator tinyAllocator;
2163 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002164 bitmap.setInfo(SkImageInfo::MakeN32(2, 2, kOpaque_SkAlphaType));
2165 if (bitmap.tryAllocPixels(&tinyAllocator)) {
2166 bitmap.eraseColor(0xff55aa33);
2167 bitmap.erase(0xffaa3355, SkIRect::MakeXYWH(1, 1, 1, 1));
2168 canvas->scale(16, 16);
2169 canvas->drawBitmap(bitmap, 0, 0);
2170 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002171}
2172##
2173
2174#SeeAlso allocPixels Allocator Pixel_Ref
2175
2176##
2177
2178# ------------------------------------------------------------------------------
2179
2180#Method SkPixelRef* pixelRef() const
Cary Clark78de7512018-02-07 07:27:09 -05002181#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002182#Line # returns Pixel_Ref, or nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002183Returns Pixel_Ref, which contains: pixel base address; its dimensions; and
2184rowBytes, the interval from one row to the next. Does not change Pixel_Ref
2185reference count. Pixel_Ref may be shared by multiple bitmaps.
2186If Pixel_Ref has not been set, returns nullptr.
2187
2188#Return Pixel_Ref, or nullptr ##
2189
2190#Example
2191#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002192 SkBitmap subset;
2193 source.extractSubset(&subset, SkIRect::MakeXYWH(32, 64, 128, 256));
2194 SkDebugf("src ref %c= sub ref\n", source.pixelRef() == subset.pixelRef() ? '=' : '!');
2195 SkDebugf("src pixels %c= sub pixels\n", source.getPixels() == subset.getPixels() ? '=' : '!');
2196 SkDebugf("src addr %c= sub addr\n", source.getAddr(32, 64) == subset.getAddr(0, 0) ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -04002197##
2198
2199#SeeAlso getPixels getAddr
2200
2201##
2202
2203# ------------------------------------------------------------------------------
2204
2205#Method SkIPoint pixelRefOrigin() const
Cary Clark78de7512018-02-07 07:27:09 -05002206#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002207#Line # returns offset within Pixel_Ref ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002208Returns origin of pixels within Pixel_Ref. Bitmap bounds is always contained
2209by Pixel_Ref bounds, which may be the same size or larger. Multiple Bitmaps
2210can share the same Pixel_Ref, where each Bitmap has different bounds.
2211
2212The returned origin added to Bitmap dimensions equals or is smaller than the
2213Pixel_Ref dimensions.
2214
2215Returns (0, 0) if Pixel_Ref is nullptr.
2216
2217#Return pixel origin within Pixel_Ref ##
2218
2219#Example
2220#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002221 SkBitmap subset;
2222 source.extractSubset(&subset, SkIRect::MakeXYWH(32, 64, 128, 256));
2223 SkIPoint sourceOrigin = source.pixelRefOrigin();
2224 SkIPoint subsetOrigin = subset.pixelRefOrigin();
2225 SkDebugf("source origin: %d, %d\n", sourceOrigin.fX, sourceOrigin.fY);
2226 SkDebugf("subset origin: %d, %d\n", subsetOrigin.fX, subsetOrigin.fY);
2227#StdOut
2228source origin: 0, 0
2229subset origin: 32, 64
2230##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002231##
2232
Cary Clark2ade9972017-11-02 17:49:34 -04002233#SeeAlso SkPixelRef getSubset setPixelRef
Cary Clarkbc5697d2017-10-04 14:31:33 -04002234
2235##
2236
2237# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05002238#Subtopic Set
2239#Line # updates values and attributes ##
2240#Populate
2241##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002242
2243#Method void setPixelRef(sk_sp<SkPixelRef> pixelRef, int dx, int dy)
Cary Clark78de7512018-02-07 07:27:09 -05002244#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05002245#Line # sets Pixel_Ref and offset ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002246Replaces pixelRef and origin in Bitmap. dx and dy specify the offset
2247within the Pixel_Ref pixels for the top-left corner of the bitmap.
2248
2249Asserts in debug builds if dx or dy are out of range. Pins dx and dy
2250to legal range in release builds.
2251
2252The caller is responsible for ensuring that the pixels match the
2253Color_Type and Alpha_Type in Image_Info.
2254
2255#Param pixelRef Pixel_Ref describing pixel address and rowBytes ##
2256#Param dx column offset in Pixel_Ref for bitmap origin ##
2257#Param dy row offset in Pixel_Ref for bitmap origin ##
2258
2259#Example
2260#Height 140
2261#Image 5
2262#Description
Cary Clark682c58d2018-05-16 07:07:07 -04002263Treating 32-bit data as 8-bit data is unlikely to produce useful results.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002264##
Ben Wagner29380bd2017-10-09 14:43:00 -04002265 SkBitmap bitmap;
Cary Clark682c58d2018-05-16 07:07:07 -04002266 bitmap.setInfo(SkImageInfo::Make(source.width() - 5, source.height() - 5,
Ben Wagner29380bd2017-10-09 14:43:00 -04002267 kGray_8_SkColorType, kOpaque_SkAlphaType), source.rowBytes());
2268 bitmap.setPixelRef(sk_ref_sp(source.pixelRef()), 5, 5);
2269 canvas->drawBitmap(bitmap, 10, 10);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002270##
2271
2272#SeeAlso setInfo
2273
2274##
2275
2276# ------------------------------------------------------------------------------
2277
2278#Method bool readyToDraw() const
Cary Clark78de7512018-02-07 07:27:09 -05002279#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05002280#Line # returns true if address of pixels is not nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002281Returns true if Bitmap is can be drawn.
2282
2283#Return true if getPixels() is not nullptr ##
2284
2285#Example
2286#Image 5
2287#Height 160
Ben Wagner29380bd2017-10-09 14:43:00 -04002288 if (source.readyToDraw()) {
2289 canvas->drawBitmap(source, 10, 10);
2290 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002291##
2292
2293#SeeAlso getPixels drawsNothing
2294
2295##
2296
2297# ------------------------------------------------------------------------------
2298
2299#Method uint32_t getGenerationID() const
Cary Clark78de7512018-02-07 07:27:09 -05002300#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05002301#Line # returns unique ID ##
Cary Clark682c58d2018-05-16 07:07:07 -04002302Returns a unique value corresponding to the pixels in Pixel_Ref.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002303Returns a different value after notifyPixelsChanged has been called.
2304Returns zero if Pixel_Ref is nullptr.
2305
2306Determines if pixels have changed since last examined.
2307
2308#Return unique value for pixels in Pixel_Ref ##
2309
2310#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04002311 SkBitmap bitmap;
2312 SkDebugf("empty id %u\n", bitmap.getGenerationID());
2313 bitmap.allocPixels(SkImageInfo::MakeN32(64, 64, kOpaque_SkAlphaType));
2314 SkDebugf("alloc id %u\n", bitmap.getGenerationID());
2315 bitmap.eraseColor(SK_ColorRED);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002316 SkDebugf("erase id %u\n", bitmap.getGenerationID());
2317#StdOut
2318#Volatile
Ben Wagner29380bd2017-10-09 14:43:00 -04002319empty id 0
2320alloc id 4
Cary Clarkbc5697d2017-10-04 14:31:33 -04002321erase id 6
Cary Clark682c58d2018-05-16 07:07:07 -04002322##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002323##
2324
2325#SeeAlso notifyPixelsChanged Pixel_Ref
2326
2327##
2328
2329# ------------------------------------------------------------------------------
2330
2331#Method void notifyPixelsChanged() const
Cary Clark78de7512018-02-07 07:27:09 -05002332#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05002333#Line # marks pixels as changed, altering the unique ID ##
Cary Clark682c58d2018-05-16 07:07:07 -04002334Marks that pixels in Pixel_Ref have changed. Subsequent calls to
Cary Clarkbc5697d2017-10-04 14:31:33 -04002335getGenerationID() return a different value.
2336
2337#Example
2338#Height 20
Cary Clark682c58d2018-05-16 07:07:07 -04002339 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002340 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
2341 bitmap.allocPixels();
2342 bitmap.eraseColor(SK_ColorRED);
2343 canvas->scale(16, 16);
2344 canvas->drawBitmap(bitmap, 0, 0);
2345 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorBLUE);
2346 canvas->drawBitmap(bitmap, 2, 0);
2347 bitmap.notifyPixelsChanged();
2348 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorGREEN);
2349 canvas->drawBitmap(bitmap, 4, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002350##
2351
2352#SeeAlso getGenerationID isVolatile Pixel_Ref
2353
2354##
2355
2356# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05002357#Subtopic Draw
2358#Populate
Cary Clark682c58d2018-05-16 07:07:07 -04002359#Line # sets pixels to Color ##
Cary Clark78de7512018-02-07 07:27:09 -05002360##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002361
2362#Method void eraseColor(SkColor c) const
Cary Clark78de7512018-02-07 07:27:09 -05002363#In Draw
Cary Clarkab2621d2018-01-30 10:08:57 -05002364#Line # writes Color to pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002365Replaces pixel values with c. All pixels contained by bounds() are affected.
2366If the colorType is kGray_8_SkColorType or k565_SkColorType, then Color_Alpha
Cary Clarkffb3d682018-05-17 12:17:28 -04002367is ignored; RGB is treated as opaque. If colorType is kAlpha_8_SkColorType,
2368then RGB is ignored.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002369
2370#Param c Unpremultiplied Color ##
2371
2372#Example
2373#Height 20
Cary Clark682c58d2018-05-16 07:07:07 -04002374 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002375 bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kOpaque_SkAlphaType));
2376 bitmap.eraseColor(SK_ColorRED);
2377 canvas->scale(16, 16);
2378 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002379##
2380
Cary Clark154beea2017-10-26 07:58:48 -04002381#SeeAlso eraseARGB erase
Cary Clarkbc5697d2017-10-04 14:31:33 -04002382
2383##
2384
2385# ------------------------------------------------------------------------------
2386
2387#Method void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const
Cary Clark78de7512018-02-07 07:27:09 -05002388#In Draw
Cary Clarkab2621d2018-01-30 10:08:57 -05002389#Line # writes Color to pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002390Replaces pixel values with Unpremultiplied Color built from a, r, g, and b.
2391All pixels contained by bounds() are affected.
2392If the colorType is kGray_8_SkColorType or k565_SkColorType, then a
2393is ignored; r, g, and b are treated as opaque. If colorType is kAlpha_8_SkColorType,
2394then r, g, and b are ignored.
2395
2396#Param a amount of Color_Alpha, from fully transparent (0) to fully opaque (255) ##
Cary Clarkffb3d682018-05-17 12:17:28 -04002397#Param r amount of red, from no red (0) to full red (255) ##
2398#Param g amount of green, from no green (0) to full green (255) ##
2399#Param b amount of blue, from no blue (0) to full blue (255) ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002400
2401#Example
2402#Height 80
Cary Clark682c58d2018-05-16 07:07:07 -04002403 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002404 bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType));
2405 bitmap.eraseARGB(0x7f, 0xff, 0x7f, 0x3f);
2406 canvas->scale(50, 50);
2407 canvas->drawBitmap(bitmap, 0, 0);
2408 canvas->drawBitmap(bitmap, .5f, .5f);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002409##
2410
Cary Clark154beea2017-10-26 07:58:48 -04002411#SeeAlso eraseColor erase
Cary Clarkbc5697d2017-10-04 14:31:33 -04002412
2413##
2414
2415# ------------------------------------------------------------------------------
2416
2417#Method void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const
Cary Clark682c58d2018-05-16 07:07:07 -04002418#Deprecated
Cary Clarkbc5697d2017-10-04 14:31:33 -04002419##
2420
2421# ------------------------------------------------------------------------------
2422
2423#Method void erase(SkColor c, const SkIRect& area) const
Cary Clark78de7512018-02-07 07:27:09 -05002424#In Draw
Cary Clarkab2621d2018-01-30 10:08:57 -05002425#Line # writes Color to rectangle of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002426Replaces pixel values inside area with c. If area does not intersect bounds(),
2427call has no effect.
2428
2429If the colorType is kGray_8_SkColorType or k565_SkColorType, then Color_Alpha
Cary Clarkffb3d682018-05-17 12:17:28 -04002430is ignored; RGB is treated as opaque. If colorType is kAlpha_8_SkColorType,
2431then RGB is ignored.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002432
2433#Param c Unpremultiplied Color ##
2434#Param area rectangle to fill ##
2435
2436#Example
2437#Height 70
Cary Clark682c58d2018-05-16 07:07:07 -04002438 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002439 bitmap.allocPixels(SkImageInfo::MakeN32(2, 2, kPremul_SkAlphaType));
2440 bitmap.erase(0x7fff7f3f, SkIRect::MakeWH(1, 1));
2441 bitmap.erase(0x7f7f3fff, SkIRect::MakeXYWH(0, 1, 1, 1));
2442 bitmap.erase(0x7f3fff7f, SkIRect::MakeXYWH(1, 0, 1, 1));
2443 bitmap.erase(0x7f1fbf5f, SkIRect::MakeXYWH(1, 1, 1, 1));
2444 canvas->scale(25, 25);
2445 canvas->drawBitmap(bitmap, 0, 0);
2446 canvas->drawBitmap(bitmap, .5f, .5f);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002447
2448##
2449
2450#SeeAlso eraseColor eraseARGB eraseRGB SkCanvas::drawRect
2451
2452##
2453
2454# ------------------------------------------------------------------------------
2455
2456#Method void eraseArea(const SkIRect& area, SkColor c) const
Cary Clark682c58d2018-05-16 07:07:07 -04002457#Deprecated
Cary Clarkbc5697d2017-10-04 14:31:33 -04002458##
2459
Cary Clarkbc5697d2017-10-04 14:31:33 -04002460# ------------------------------------------------------------------------------
2461
2462#Method SkColor getColor(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002463#In Property
2464#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05002465#Line # returns one pixel as Unpremultiplied Color ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002466Returns pixel at (x, y) as Unpremultiplied Color.
2467Returns black with Alpha if Color_Type is kAlpha_8_SkColorType.
2468
2469Input is not validated: out of bounds values of x or y trigger an assert() if
2470built with SK_DEBUG defined; and returns undefined values or may crash if
2471SK_RELEASE is defined. Fails if Color_Type is kUnknown_SkColorType or
2472pixel address is nullptr.
2473
2474Color_Space in Image_Info is ignored. Some Color precision may be lost in the
Cary Clark682c58d2018-05-16 07:07:07 -04002475conversion to Unpremultiplied Color; original pixel data may have additional
Cary Clarkbc5697d2017-10-04 14:31:33 -04002476precision.
2477
2478#Param x column index, zero or greater, and less than width() ##
2479#Param y row index, zero or greater, and less than height() ##
2480
2481#Return pixel converted to Unpremultiplied Color ##
2482
2483#Example
2484 const int w = 4;
2485 const int h = 4;
2486 SkColor colors[][w] = {
2487 0x00000000, 0x2a0e002a, 0x55380055, 0x7f7f007f,
2488 0x2a000e2a, 0x551c1c55, 0x7f542a7f, 0xaaaa38aa,
2489 0x55003855, 0x7f2a547f, 0xaa7171aa, 0xd4d48dd4,
Cary Clark682c58d2018-05-16 07:07:07 -04002490 0x7f007f7f, 0xaa38aaaa, 0xd48dd4d4, 0xffffffff,
Cary Clarkbc5697d2017-10-04 14:31:33 -04002491 };
2492 SkDebugf("Premultiplied:\n");
2493 for (int y = 0; y < h; ++y) {
2494 SkDebugf("(0, %d) ", y);
2495 for (int x = 0; x < w; ++x) {
2496 SkDebugf("0x%08x%c", colors[y][x], x == w - 1 ? '\n' : ' ');
2497 }
2498 }
2499 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), colors, w * 4);
2500 SkBitmap bitmap;
2501 bitmap.installPixels(pixmap);
2502 SkDebugf("Unpremultiplied:\n");
2503 for (int y = 0; y < h; ++y) {
2504 SkDebugf("(0, %d) ", y);
2505 for (int x = 0; x < w; ++x) {
2506 SkDebugf("0x%08x%c", bitmap.getColor(x, y), x == w - 1 ? '\n' : ' ');
2507 }
2508 }
2509#StdOut
2510Premultiplied:
Cary Clark682c58d2018-05-16 07:07:07 -04002511(0, 0) 0x00000000 0x2a0e002a 0x55380055 0x7f7f007f
2512(0, 1) 0x2a000e2a 0x551c1c55 0x7f542a7f 0xaaaa38aa
2513(0, 2) 0x55003855 0x7f2a547f 0xaa7171aa 0xd4d48dd4
2514(0, 3) 0x7f007f7f 0xaa38aaaa 0xd48dd4d4 0xffffffff
Cary Clarkbc5697d2017-10-04 14:31:33 -04002515Unpremultiplied:
Cary Clark682c58d2018-05-16 07:07:07 -04002516(0, 0) 0x00000000 0x2a5500ff 0x55a800ff 0x7fff00ff
2517(0, 1) 0x2a0055ff 0x555454ff 0x7fa954ff 0xaaff54ff
2518(0, 2) 0x5500a8ff 0x7f54a9ff 0xaaaaaaff 0xd4ffaaff
2519(0, 3) 0x7f00ffff 0xaa54ffff 0xd4aaffff 0xffffffff
Cary Clarkbc5697d2017-10-04 14:31:33 -04002520##
2521##
2522
2523#SeeAlso getAddr readPixels
2524
2525##
2526
2527# ------------------------------------------------------------------------------
2528
2529#Method void* getAddr(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002530#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002531#Line # returns readable pixel address as void pointer ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002532Returns pixel address at (x, y).
2533
2534Input is not validated: out of bounds values of x or y, or kUnknown_SkColorType,
2535trigger an assert() if built with SK_DEBUG defined. Returns nullptr if
2536Color_Type is kUnknown_SkColorType, or Pixel_Ref is nullptr.
2537
Cary Clark682c58d2018-05-16 07:07:07 -04002538Performs a lookup of pixel size; for better performance, call
Cary Clarkbc5697d2017-10-04 14:31:33 -04002539one of: getAddr8, getAddr16, or getAddr32.
2540
2541#Param x column index, zero or greater, and less than width() ##
2542#Param y row index, zero or greater, and less than height() ##
2543
2544#Return generic pointer to pixel ##
2545
2546#Example
2547#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002548 char* row0 = (char* ) source.getAddr(0, 0);
2549 char* row1 = (char* ) source.getAddr(0, 1);
Cary Clark681287e2018-03-16 11:34:15 -04002550 SkDebugf("addr interval %c= rowBytes\n",
2551 (size_t) (row1 - row0) == source.rowBytes() ? '=' : '!');
Ben Wagner29380bd2017-10-09 14:43:00 -04002552#StdOut
2553addr interval == rowBytes
2554##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002555##
2556
2557#SeeAlso getAddr8 getAddr16 getAddr32 readPixels SkPixmap::addr
2558
2559##
2560
2561# ------------------------------------------------------------------------------
2562
2563#Method inline uint32_t* getAddr32(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002564#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002565#Line # returns readable pixel address as 32-bit pointer ##
Cary Clark682c58d2018-05-16 07:07:07 -04002566Returns address at (x, y).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002567
2568Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2569#List
2570# Pixel_Ref is nullptr ##
2571# bytesPerPixel() is not four ##
2572# x is negative, or not less than width() ##
2573# y is negative, or not less than height() ##
2574##
2575
2576#Param x column index, zero or greater, and less than width() ##
2577#Param y row index, zero or greater, and less than height() ##
2578
2579#Return unsigned 32-bit pointer to pixel at (x, y) ##
2580
2581#Example
2582#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002583 uint32_t* row0 = source.getAddr32(0, 0);
2584 uint32_t* row1 = source.getAddr32(0, 1);
2585 size_t interval = (row1 - row0) * source.bytesPerPixel();
2586 SkDebugf("addr interval %c= rowBytes\n", interval == source.rowBytes() ? '=' : '!');
2587#StdOut
2588addr interval == rowBytes
2589##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002590##
2591
2592#SeeAlso getAddr8 getAddr16 getAddr readPixels SkPixmap::addr32
2593
2594##
2595
2596# ------------------------------------------------------------------------------
2597
2598#Method inline uint16_t* getAddr16(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002599#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002600#Line # returns readable pixel address as 16-bit pointer ##
Cary Clark682c58d2018-05-16 07:07:07 -04002601Returns address at (x, y).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002602
2603Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2604#List
2605# Pixel_Ref is nullptr ##
2606# bytesPerPixel() is not two ##
2607# x is negative, or not less than width() ##
2608# y is negative, or not less than height() ##
2609##
2610
2611#Param x column index, zero or greater, and less than width() ##
2612#Param y row index, zero or greater, and less than height() ##
2613
2614#Return unsigned 16-bit pointer to pixel at (x, y)##
2615
2616#Example
2617#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002618 SkBitmap bitmap16;
Cary Clark682c58d2018-05-16 07:07:07 -04002619 SkImageInfo dstInfo = SkImageInfo::Make(source.width(), source.height(), kARGB_4444_SkColorType,
Ben Wagner29380bd2017-10-09 14:43:00 -04002620 kPremul_SkAlphaType);
2621 bitmap16.allocPixels(dstInfo);
2622 if (source.readPixels(dstInfo, bitmap16.getPixels(), bitmap16.rowBytes(), 0, 0)) {
2623 uint16_t* row0 = bitmap16.getAddr16(0, 0);
2624 uint16_t* row1 = bitmap16.getAddr16(0, 1);
2625 size_t interval = (row1 - row0) * bitmap16.bytesPerPixel();
2626 SkDebugf("addr interval %c= rowBytes\n", interval == bitmap16.rowBytes() ? '=' : '!');
2627 }
2628#StdOut
2629addr interval == rowBytes
2630##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002631##
2632
2633#SeeAlso getAddr8 getAddr getAddr32 readPixels SkPixmap::addr16
2634
2635##
2636
2637# ------------------------------------------------------------------------------
2638
2639#Method inline uint8_t* getAddr8(int x, int y) const
Cary Clark78de7512018-02-07 07:27:09 -05002640#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05002641#Line # returns readable pixel address as 8-bit pointer ##
Cary Clark682c58d2018-05-16 07:07:07 -04002642Returns address at (x, y).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002643
2644Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2645#List
2646# Pixel_Ref is nullptr ##
2647# bytesPerPixel() is not one ##
2648# x is negative, or not less than width() ##
2649# y is negative, or not less than height() ##
2650##
2651
2652#Param x column index, zero or greater, and less than width() ##
2653#Param y row index, zero or greater, and less than height() ##
2654
2655#Return unsigned 8-bit pointer to pixel at (x, y) ##
2656
2657#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04002658 SkBitmap bitmap;
2659 const int width = 8;
2660 const int height = 8;
2661 uint8_t pixels[height][width];
2662 SkImageInfo info = SkImageInfo::Make(width, height, kGray_8_SkColorType, kOpaque_SkAlphaType);
2663 if (bitmap.installPixels(info, pixels, info.minRowBytes())) {
2664 SkDebugf("&pixels[4][2] %c= bitmap.getAddr8(2, 4)\n",
2665 &pixels[4][2] == bitmap.getAddr8(2, 4) ? '=' : '!');
2666 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002667#StdOut
2668&pixels[4][2] == bitmap.getAddr8(2, 4)
2669##
2670##
2671
2672#SeeAlso getAddr getAddr16 getAddr32 readPixels SkPixmap::addr8
2673
2674##
2675
2676# ------------------------------------------------------------------------------
2677
2678#Method bool extractSubset(SkBitmap* dst, const SkIRect& subset) const
Cary Clark78de7512018-02-07 07:27:09 -05002679#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -05002680#Line # creates Bitmap, sharing pixels if possible ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002681Shares Pixel_Ref with dst. Pixels are not copied; Bitmap and dst point
2682to the same pixels; dst bounds() are set to the intersection of subset
2683and the original bounds().
2684
2685subset may be larger than bounds(). Any area outside of bounds() is ignored.
2686
2687Any contents of dst are discarded. isVolatile setting is copied to dst.
2688dst is set to colorType, alphaType, and colorSpace.
2689
2690Return false if:
2691#List
2692# dst is nullptr ##
2693# Pixel_Ref is nullptr ##
2694# subset does not intersect bounds() ##
Cary Clark682c58d2018-05-16 07:07:07 -04002695##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002696
2697
2698#Param dst Bitmap set to subset ##
2699#Param subset rectangle of pixels to reference ##
2700
2701#Return true if dst is replaced by subset
2702##
2703
2704#Example
2705#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002706 SkIRect bounds, s;
2707 source.getBounds(&bounds);
2708 SkDebugf("bounds: %d, %d, %d, %d\n", bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
2709 SkBitmap subset;
2710 for (int left: { -100, 0, 100, 1000 } ) {
2711 for (int right: { 0, 100, 1000 } ) {
2712 SkIRect b = SkIRect::MakeLTRB(left, 100, right, 200);
2713 bool success = source.extractSubset(&subset, b);
2714 SkDebugf("subset: %4d, %4d, %4d, %4d ", b.fLeft, b.fTop, b.fRight, b.fBottom);
2715 SkDebugf("success; %s", success ? "true" : "false");
2716 if (success) {
Cary Clark682c58d2018-05-16 07:07:07 -04002717 subset.getBounds(&s);
Ben Wagner29380bd2017-10-09 14:43:00 -04002718 SkDebugf(" subset: %d, %d, %d, %d", s.fLeft, s.fTop, s.fRight, s.fBottom);
2719 }
2720 SkDebugf("\n");
Cary Clark682c58d2018-05-16 07:07:07 -04002721 }
Ben Wagner29380bd2017-10-09 14:43:00 -04002722 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002723#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04002724bounds: 0, 0, 512, 512
2725subset: -100, 100, 0, 200 success; false
2726subset: -100, 100, 100, 200 success; true subset: 0, 0, 100, 100
2727subset: -100, 100, 1000, 200 success; true subset: 0, 0, 512, 100
2728subset: 0, 100, 0, 200 success; false
2729subset: 0, 100, 100, 200 success; true subset: 0, 0, 100, 100
2730subset: 0, 100, 1000, 200 success; true subset: 0, 0, 512, 100
2731subset: 100, 100, 0, 200 success; false
2732subset: 100, 100, 100, 200 success; false
2733subset: 100, 100, 1000, 200 success; true subset: 0, 0, 412, 100
2734subset: 1000, 100, 0, 200 success; false
2735subset: 1000, 100, 100, 200 success; false
Cary Clarkbc5697d2017-10-04 14:31:33 -04002736subset: 1000, 100, 1000, 200 success; false
2737##
2738##
2739
2740#SeeAlso readPixels writePixels SkCanvas::drawBitmap
2741
2742##
2743
2744# ------------------------------------------------------------------------------
2745
2746#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
2747 int srcX, int srcY, SkTransferFunctionBehavior behavior) const
Cary Clark78de7512018-02-07 07:27:09 -05002748#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05002749#Line # copies and converts pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002750
Cary Clarkac47b882018-01-11 10:35:44 -05002751Copies Rect of pixels from Bitmap pixels to dstPixels. Copy starts at (srcX, srcY),
Cary Clark682c58d2018-05-16 07:07:07 -04002752and does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002753
Cary Clark682c58d2018-05-16 07:07:07 -04002754dstInfo specifies width, height, Color_Type, Alpha_Type, and
Cary Clarkbc5697d2017-10-04 14:31:33 -04002755Color_Space of destination. dstRowBytes specifics the gap from one destination
2756row to the next. Returns true if pixels are copied. Returns false if:
2757#List
2758# dstInfo.addr() equals nullptr ##
2759# dstRowBytes is less than dstInfo.minRowBytes ##
2760# Pixel_Ref is nullptr ##
2761##
2762
Cary Clarkac47b882018-01-11 10:35:44 -05002763Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002764kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002765If Bitmap colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
2766If Bitmap alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
2767match. If Bitmap colorSpace is nullptr, dstInfo.colorSpace must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002768false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04002769
Cary Clarkbc5697d2017-10-04 14:31:33 -04002770srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark682c58d2018-05-16 07:07:07 -04002771false if width() or height() is zero or negative.
2772Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04002773#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002774abs(srcX) >= Bitmap width()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002775##
2776, or if
2777#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002778abs(srcY) >= Bitmap height()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002779##
2780.
2781
2782If behavior is SkTransferFunctionBehavior::kRespect: converts source
2783pixels to a linear space before converting to dstInfo.
2784If behavior is SkTransferFunctionBehavior::kIgnore: source
2785pixels are treated as if they are linear, regardless of how they are encoded.
2786
2787#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
2788#Param dstPixels destination pixel storage ##
2789#Param dstRowBytes destination row length ##
2790#Param srcX column index whose absolute value is less than width() ##
2791#Param srcY row index whose absolute value is less than height() ##
2792#Param behavior one of: SkTransferFunctionBehavior::kRespect,
Cary Clark682c58d2018-05-16 07:07:07 -04002793 SkTransferFunctionBehavior::kIgnore
Cary Clarkbc5697d2017-10-04 14:31:33 -04002794##
2795
2796#Return true if pixels are copied to dstPixels ##
2797
2798#Example
2799#Height 64
2800void draw(SkCanvas* canvas) {
Ben Wagner29380bd2017-10-09 14:43:00 -04002801 const int width = 256;
2802 const int height = 32;
2803 std::vector<int32_t> dstPixels;
2804 dstPixels.resize(height * width * 4);
2805 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
2806 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
2807 SkPoint gradPoints[] = { { 0, 0 }, { width, 0 } };
2808 SkPaint gradPaint;
2809 gradPaint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
2810 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
2811 for (auto behavior : { SkTransferFunctionBehavior::kRespect,
2812 SkTransferFunctionBehavior::kIgnore} ) {
2813 SkBitmap bitmap;
2814 bitmap.allocPixels(info);
2815 SkCanvas srcCanvas(bitmap);
2816 srcCanvas.drawRect(SkRect::MakeWH(width, height), gradPaint);
2817 if (bitmap.readPixels(info, &dstPixels.front(), width * 4, 0, 0, behavior)) {
2818 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
2819 bitmap.installPixels(dstPixmap);
2820 canvas->drawBitmap(bitmap, 0, 0);
2821 }
2822 canvas->translate(0, height);
2823 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002824}
2825##
2826
2827#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2828
2829##
2830
2831# ------------------------------------------------------------------------------
2832
2833#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
2834 int srcX, int srcY) const
2835
Cary Clarkac47b882018-01-11 10:35:44 -05002836Copies a Rect of pixels from Bitmap to dstPixels. Copy starts at (srcX, srcY),
Cary Clark682c58d2018-05-16 07:07:07 -04002837and does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002838
Cary Clarkac47b882018-01-11 10:35:44 -05002839dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
2840destination. dstRowBytes specifics the gap from one destination row to the next.
2841Returns true if pixels are copied. Returns false if:
Cary Clarkbc5697d2017-10-04 14:31:33 -04002842#List
2843# dstInfo.addr() equals nullptr ##
2844# dstRowBytes is less than dstInfo.minRowBytes ##
2845# Pixel_Ref is nullptr ##
2846##
2847
Cary Clarkac47b882018-01-11 10:35:44 -05002848Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002849kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002850If Bitmap colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
2851If Bitmap alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
2852match. If Bitmap colorSpace is nullptr, dstInfo.colorSpace must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002853false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04002854
Cary Clarkbc5697d2017-10-04 14:31:33 -04002855srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04002856false if width() or height() is zero or negative.
Cary Clark682c58d2018-05-16 07:07:07 -04002857Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04002858#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002859abs(srcX) >= Bitmap width()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002860##
2861, or if
2862#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002863abs(srcY) >= Bitmap height()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002864##
2865.
2866
2867#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
2868#Param dstPixels destination pixel storage ##
2869#Param dstRowBytes destination row length ##
2870#Param srcX column index whose absolute value is less than width() ##
2871#Param srcY row index whose absolute value is less than height() ##
2872
2873#Return true if pixels are copied to dstPixels ##
2874
2875#Example
2876#Height 128
2877#Description
2878Transferring the gradient from 8 bits per component to 4 bits per component
2879creates visible banding.
2880##
Ben Wagner29380bd2017-10-09 14:43:00 -04002881 const int width = 256;
2882 const int height = 64;
2883 SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(width, height);
2884 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
2885 SkPoint gradPoints[] = { { 0, 0 }, { 256, 0 } };
2886 SkPaint paint;
2887 paint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
2888 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
2889 SkBitmap bitmap;
2890 bitmap.allocPixels(srcInfo);
2891 SkCanvas srcCanvas(bitmap);
2892 srcCanvas.drawRect(SkRect::MakeWH(width, height), paint);
2893 canvas->drawBitmap(bitmap, 0, 0);
2894 SkImageInfo dstInfo = srcInfo.makeColorType(kARGB_4444_SkColorType);
2895 std::vector<int16_t> dstPixels;
2896 dstPixels.resize(height * width);
2897 bitmap.readPixels(dstInfo, &dstPixels.front(), width * 2, 0, 0);
2898 SkPixmap dstPixmap(dstInfo, &dstPixels.front(), width * 2);
2899 bitmap.installPixels(dstPixmap);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002900 canvas->drawBitmap(bitmap, 0, 64);
2901##
2902
2903#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2904
2905##
2906
2907# ------------------------------------------------------------------------------
2908
2909#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY) const
2910
Cary Clarkac47b882018-01-11 10:35:44 -05002911Copies a Rect of pixels from Bitmap to dst. Copy starts at (srcX, srcY), and
Cary Clark682c58d2018-05-16 07:07:07 -04002912does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002913
2914dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2915and row bytes of destination. dst.rowBytes specifics the gap from one destination
2916row to the next. Returns true if pixels are copied. Returns false if:
2917#List
2918# dst pixel storage equals nullptr ##
2919# dst.rowBytes is less than SkImageInfo::minRowBytes ##
2920# Pixel_Ref is nullptr ##
2921##
2922
Cary Clarkac47b882018-01-11 10:35:44 -05002923Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002924kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002925If Bitmap colorType is kGray_8_SkColorType, dst Color_Space must match.
2926If Bitmap alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
2927match. If Bitmap colorSpace is nullptr, dst Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002928false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04002929
Cary Clarkbc5697d2017-10-04 14:31:33 -04002930srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark682c58d2018-05-16 07:07:07 -04002931false if width() or height() is zero or negative.
2932Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04002933#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002934abs(srcX) >= Bitmap width()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002935##
2936, or if
2937#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05002938abs(srcY) >= Bitmap height()
Cary Clarkbc5697d2017-10-04 14:31:33 -04002939##
2940.
2941
2942#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
2943#Param srcX column index whose absolute value is less than width() ##
2944#Param srcY row index whose absolute value is less than height() ##
2945
2946#Return true if pixels are copied to dst ##
2947
2948#Example
2949#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002950 std::vector<int32_t> srcPixels;
2951 srcPixels.resize(source.height() * source.rowBytes());
2952 for (int y = 0; y < 4; ++y) {
2953 for (int x = 0; x < 4; ++x) {
2954 SkPixmap pixmap(SkImageInfo::MakeN32Premul(source.width() / 4, source.height() / 4),
2955 &srcPixels.front() + x * source.height() * source.width() / 4 +
2956 y * source.width() / 4, source.rowBytes());
2957 source.readPixels(pixmap, x * source.width() / 4, y * source.height() / 4);
2958 }
2959 }
2960 canvas->scale(.5f, .5f);
2961 SkBitmap bitmap;
2962 bitmap.installPixels(SkImageInfo::MakeN32Premul(source.width(), source.height()),
2963 &srcPixels.front(), source.rowBytes());
Cary Clarkbc5697d2017-10-04 14:31:33 -04002964 canvas->drawBitmap(bitmap, 0, 0);
2965##
2966
2967#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2968
2969##
2970
2971# ------------------------------------------------------------------------------
2972
2973#Method bool readPixels(const SkPixmap& dst) const
2974
Cary Clarkac47b882018-01-11 10:35:44 -05002975Copies a Rect of pixels from Bitmap to dst. Copy starts at (0, 0), and
Cary Clark682c58d2018-05-16 07:07:07 -04002976does not exceed Bitmap (width(), height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04002977
2978dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2979and row bytes of destination. dst.rowBytes specifics the gap from one destination
2980row to the next. Returns true if pixels are copied. Returns false if:
2981#List
2982# dst pixel storage equals nullptr ##
2983# dst.rowBytes is less than SkImageInfo::minRowBytes ##
2984# Pixel_Ref is nullptr ##
2985##
2986
Cary Clarkac47b882018-01-11 10:35:44 -05002987Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04002988kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05002989If Bitmap colorType is kGray_8_SkColorType, dst Color_Space must match.
2990If Bitmap alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
2991match. If Bitmap colorSpace is nullptr, dst Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04002992false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04002993
Cary Clarkbc5697d2017-10-04 14:31:33 -04002994#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
2995
2996#Return true if pixels are copied to dst ##
2997
2998#Example
2999#Height 128
3000#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04003001 std::vector<int32_t> srcPixels;
3002 srcPixels.resize(source.height() * source.width() * 8);
3003 for (int i = 0; i < 2; ++i) {
Cary Clark682c58d2018-05-16 07:07:07 -04003004 SkPixmap pixmap(SkImageInfo::Make(source.width() * 2, source.height(),
Ben Wagner29380bd2017-10-09 14:43:00 -04003005 i ? kRGBA_8888_SkColorType : kBGRA_8888_SkColorType, kPremul_SkAlphaType),
3006 &srcPixels.front() + i * source.width(), source.rowBytes() * 2);
3007 source.readPixels(pixmap);
3008 }
3009 canvas->scale(.25f, .25f);
3010 SkBitmap bitmap;
3011 bitmap.installPixels(SkImageInfo::MakeN32Premul(source.width() * 2, source.height()),
3012 &srcPixels.front(), source.rowBytes() * 2);
3013 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003014##
3015
3016#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
3017
3018##
3019
3020# ------------------------------------------------------------------------------
3021
3022#Method bool writePixels(const SkPixmap& src, int dstX, int dstY)
Cary Clark78de7512018-02-07 07:27:09 -05003023#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05003024#Line # copies and converts pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003025Copies a Rect of pixels from src. Copy starts at (dstX, dstY), and does not exceed
Cary Clark682c58d2018-05-16 07:07:07 -04003026(src.width(), src.height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04003027
3028src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
3029and row bytes of source. src.rowBytes specifics the gap from one source
3030row to the next. Returns true if pixels are copied. Returns false if:
3031#List
3032# src pixel storage equals nullptr ##
3033# src.rowBytes is less than SkImageInfo::minRowBytes ##
3034# Pixel_Ref is nullptr ##
3035##
3036
Cary Clarkac47b882018-01-11 10:35:44 -05003037Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04003038kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05003039If Bitmap colorType is kGray_8_SkColorType, src Color_Space must match.
3040If Bitmap alphaType is kOpaque_SkAlphaType, src Alpha_Type must
3041match. If Bitmap colorSpace is nullptr, src Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04003042false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04003043
Cary Clarkbc5697d2017-10-04 14:31:33 -04003044dstX and dstY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04003045false if width() or height() is zero or negative.
Cary Clark682c58d2018-05-16 07:07:07 -04003046Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04003047#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05003048abs(dstX) >= Bitmap width()
Cary Clarkbc5697d2017-10-04 14:31:33 -04003049##
3050, or if
3051#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05003052abs(dstY) >= Bitmap height()
Cary Clarkbc5697d2017-10-04 14:31:33 -04003053##
3054.
3055
3056#Param src source Pixmap: Image_Info, pixels, row bytes ##
3057#Param dstX column index whose absolute value is less than width() ##
3058#Param dstY row index whose absolute value is less than height() ##
3059
3060#Return true if src pixels are copied to Bitmap ##
3061
3062#Example
3063#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04003064 std::vector<int32_t> srcPixels;
3065 int width = image->width();
3066 int height = image->height();
3067 srcPixels.resize(height * width * 4);
3068 SkPixmap pixmap(SkImageInfo::MakeN32Premul(width, height), (const void*) &srcPixels.front(),
3069 width * 4);
3070 image->readPixels(pixmap, 0, 0);
3071 canvas->scale(.5f, .5f);
3072 width /= 4;
3073 height /= 4;
3074 for (int y = 0; y < 4; ++y) {
3075 for (int x = 0; x < 4; ++x) {
3076 SkBitmap bitmap;
3077 bitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height));
3078 bitmap.writePixels(pixmap, -y * width, -x * height);
3079 canvas->drawBitmap(bitmap, x * width, y * height);
3080 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003081 }
3082##
3083
Cary Clark682c58d2018-05-16 07:07:07 -04003084#SeeAlso readPixels
Cary Clarkbc5697d2017-10-04 14:31:33 -04003085
3086##
3087
3088# ------------------------------------------------------------------------------
3089
3090#Method bool writePixels(const SkPixmap& src)
3091
3092Copies a Rect of pixels from src. Copy starts at (0, 0), and does not exceed
Cary Clark682c58d2018-05-16 07:07:07 -04003093(src.width(), src.height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04003094
3095src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
3096and row bytes of source. src.rowBytes specifics the gap from one source
3097row to the next. Returns true if pixels are copied. Returns false if:
3098#List
3099# src pixel storage equals nullptr ##
3100# src.rowBytes is less than SkImageInfo::minRowBytes ##
3101# Pixel_Ref is nullptr ##
3102##
3103
Cary Clarkac47b882018-01-11 10:35:44 -05003104Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04003105kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05003106If Bitmap colorType is kGray_8_SkColorType, src Color_Space must match.
3107If Bitmap alphaType is kOpaque_SkAlphaType, src Alpha_Type must
3108match. If Bitmap colorSpace is nullptr, src Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04003109false if pixel conversion is not possible.
3110
3111#Param src source Pixmap: Image_Info, pixels, row bytes ##
3112
3113#Return true if src pixels are copied to Bitmap ##
3114
3115#Example
3116#Height 80
Ben Wagner29380bd2017-10-09 14:43:00 -04003117 SkBitmap bitmap;
3118 bitmap.allocPixels(SkImageInfo::MakeN32Premul(2, 2));
3119 bitmap.eraseColor(SK_ColorGREEN);
3120 SkPMColor color = 0xFF5599BB;
3121 SkPixmap src(SkImageInfo::MakeN32Premul(1, 1), &color, 4);
3122 bitmap.writePixels(src);
3123 canvas->scale(40, 40);
3124 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003125##
3126
3127#SeeAlso readPixels
3128
3129##
3130
3131# ------------------------------------------------------------------------------
3132
3133#Method bool writePixels(const SkPixmap& src, int x, int y, SkTransferFunctionBehavior behavior)
3134
3135Copies a Rect of pixels from src. Copy starts at (0, 0), and does not exceed
Cary Clark682c58d2018-05-16 07:07:07 -04003136(src.width(), src.height()).
Cary Clarkbc5697d2017-10-04 14:31:33 -04003137
3138src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
3139and row bytes of source. src.rowBytes specifics the gap from one source
3140row to the next. Returns true if pixels are copied. Returns false if:
3141#List
3142# src pixel storage equals nullptr ##
3143# src.rowBytes is less than SkImageInfo::minRowBytes ##
3144# Pixel_Ref is nullptr ##
3145##
3146
Cary Clarkac47b882018-01-11 10:35:44 -05003147Pixels are copied only if pixel conversion is possible. If Bitmap colorType is
Cary Clarkbc5697d2017-10-04 14:31:33 -04003148kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05003149If Bitmap colorType is kGray_8_SkColorType, src Color_Space must match.
3150If Bitmap alphaType is kOpaque_SkAlphaType, src Alpha_Type must
3151match. If Bitmap colorSpace is nullptr, src Color_Space must match. Returns
Cary Clarkbc5697d2017-10-04 14:31:33 -04003152false if pixel conversion is not possible. Returns false if width() or height()
3153is zero or negative.
3154
3155If behavior is SkTransferFunctionBehavior::kRespect: converts src
3156pixels to a linear space before converting to Image_Info.
3157If behavior is SkTransferFunctionBehavior::kIgnore: src
3158pixels are treated as if they are linear, regardless of how they are encoded.
3159
3160#Param src source Pixmap: Image_Info, pixels, row bytes ##
3161#Param x column index whose absolute value is less than width() ##
3162#Param y row index whose absolute value is less than height() ##
3163#Param behavior one of: SkTransferFunctionBehavior::kRespect,
Cary Clark682c58d2018-05-16 07:07:07 -04003164 SkTransferFunctionBehavior::kIgnore
Cary Clarkbc5697d2017-10-04 14:31:33 -04003165##
3166
3167#Return true if src pixels are copied to Bitmap ##
3168
3169#Example
3170#Height 64
Ben Wagner29380bd2017-10-09 14:43:00 -04003171 const int width = 256;
3172 const int height = 32;
3173 std::vector<int32_t> dstPixels;
3174 dstPixels.resize(height * width * 4);
3175 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
3176 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
3177 SkPoint gradPoints[] = { { 0, 0 }, { width, 0 } };
3178 SkPaint gradPaint;
3179 gradPaint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
3180 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
3181 for (auto behavior : { SkTransferFunctionBehavior::kRespect,
3182 SkTransferFunctionBehavior::kIgnore} ) {
3183 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
3184 SkBitmap bitmap;
3185 bitmap.installPixels(dstPixmap);
3186 SkCanvas srcCanvas(bitmap);
3187 srcCanvas.drawRect(SkRect::MakeWH(width, height), gradPaint);
3188 if (bitmap.writePixels(dstPixmap, 0, 0, behavior)) {
3189 canvas->drawBitmap(bitmap, 0, 0);
3190 }
3191 canvas->translate(0, height);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003192 }
3193##
3194
3195#SeeAlso readPixels
3196
3197##
3198
3199# ------------------------------------------------------------------------------
3200
3201#Method bool hasHardwareMipMap() const
Cary Clark78de7512018-02-07 07:27:09 -05003202#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05003203#Line # returns Mip_Map support present; Android only ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003204#Private
3205Android framework only.
3206##
3207
3208#Return true if setHasHardwareMipMap has been called with true ##
3209
3210#NoExample
3211##
3212
3213#SeeAlso setHasHardwareMipMap
3214
3215##
3216
3217# ------------------------------------------------------------------------------
3218
3219#Method void setHasHardwareMipMap(bool hasHardwareMipMap)
Cary Clark78de7512018-02-07 07:27:09 -05003220#In Set
Cary Clarkab2621d2018-01-30 10:08:57 -05003221#Line # sets Mip_Map support present; Android only ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003222#Private
3223Android framework only.
3224##
3225
3226#Param hasHardwareMipMap sets state ##
3227
3228#NoExample
3229##
3230
3231#SeeAlso hasHardwareMipMap
3232
3233##
3234
3235# ------------------------------------------------------------------------------
3236
3237#Method bool extractAlpha(SkBitmap* dst) const
Cary Clark78de7512018-02-07 07:27:09 -05003238#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -05003239#Line # creates Bitmap containing Alpha of pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003240Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3241or dst pixels cannot be allocated.
3242
3243Uses HeapAllocator to reserve memory for dst Pixel_Ref.
3244
3245#Param dst holds Pixel_Ref to fill with alpha layer ##
3246
3247#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3248
3249#Example
3250#Height 100
Ben Wagner29380bd2017-10-09 14:43:00 -04003251 SkBitmap alpha, bitmap;
3252 bitmap.allocN32Pixels(100, 100);
3253 SkCanvas offscreen(bitmap);
3254 offscreen.clear(0);
3255 SkPaint paint;
3256 paint.setAntiAlias(true);
3257 paint.setColor(SK_ColorBLUE);
3258 paint.setStyle(SkPaint::kStroke_Style);
3259 paint.setStrokeWidth(20);
3260 offscreen.drawCircle(50, 50, 39, paint);
3261 offscreen.flush();
3262 bitmap.extractAlpha(&alpha);
3263 paint.setColor(SK_ColorRED);
3264 canvas->drawBitmap(bitmap, 0, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003265 canvas->drawBitmap(alpha, 100, 0, &paint);
3266##
3267
3268#SeeAlso extractSubset
3269
3270##
3271
3272# ------------------------------------------------------------------------------
3273
3274#Method bool extractAlpha(SkBitmap* dst, const SkPaint* paint,
3275 SkIPoint* offset) const
3276
3277Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3278or dst pixels cannot be allocated.
3279
Cary Clark682c58d2018-05-16 07:07:07 -04003280If paint is not nullptr and contains Mask_Filter, SkMaskFilter
Cary Clarkbc5697d2017-10-04 14:31:33 -04003281generates Mask_Alpha from Bitmap. Uses HeapAllocator to reserve memory for dst
3282Pixel_Ref. Sets offset to top-left position for dst for alignment with Bitmap;
3283(0, 0) unless SkMaskFilter generates mask.
3284
3285#Param dst holds Pixel_Ref to fill with alpha layer ##
3286#Param paint holds optional Mask_Filter; may be nullptr ##
3287#Param offset top-left position for dst; may be nullptr ##
3288
3289#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3290
Cary Clark4855f782018-02-06 09:41:53 -05003291#Bug 7103
Cary Clarkbc5697d2017-10-04 14:31:33 -04003292#Example
3293#Height 160
Cary Clark681287e2018-03-16 11:34:15 -04003294 auto radiusToSigma = [](SkScalar radius) -> SkScalar {
3295 static const SkScalar kBLUR_SIGMA_SCALE = 0.57735f;
3296 return radius > 0 ? kBLUR_SIGMA_SCALE * radius + 0.5f : 0.0f;
3297 };
3298 SkBitmap alpha, bitmap;
3299 bitmap.allocN32Pixels(100, 100);
3300 SkCanvas offscreen(bitmap);
3301 offscreen.clear(0);
3302 SkPaint paint;
3303 paint.setAntiAlias(true);
3304 paint.setColor(SK_ColorBLUE);
3305 paint.setStyle(SkPaint::kStroke_Style);
3306 paint.setStrokeWidth(20);
3307 offscreen.drawCircle(50, 50, 39, paint);
3308 offscreen.flush();
3309 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, radiusToSigma(25)));
3310 SkIPoint offset;
3311 bitmap.extractAlpha(&alpha, &paint, &offset);
3312 paint.setColor(SK_ColorRED);
3313 canvas->drawBitmap(bitmap, 0, -offset.fY, &paint);
3314 canvas->drawBitmap(alpha, 100 + offset.fX, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003315##
3316
3317#SeeAlso extractSubset
3318
3319##
3320
3321# ------------------------------------------------------------------------------
3322
3323#Method bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator,
3324 SkIPoint* offset) const
3325
3326Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3327or dst pixels cannot be allocated.
3328
Cary Clark682c58d2018-05-16 07:07:07 -04003329If paint is not nullptr and contains Mask_Filter, SkMaskFilter
Cary Clarkbc5697d2017-10-04 14:31:33 -04003330generates Mask_Alpha from Bitmap. allocator may reference a custom allocation
Cary Clark682c58d2018-05-16 07:07:07 -04003331class or be set to nullptr to use HeapAllocator. Sets offset to top-left
Cary Clarkbc5697d2017-10-04 14:31:33 -04003332position for dst for alignment with Bitmap; (0, 0) unless SkMaskFilter generates
3333mask.
3334
3335#Param dst holds Pixel_Ref to fill with alpha layer ##
3336#Param paint holds optional Mask_Filter; may be nullptr ##
Cary Clark682c58d2018-05-16 07:07:07 -04003337#Param allocator function to reserve memory for Pixel_Ref; may be nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003338#Param offset top-left position for dst; may be nullptr ##
3339
3340#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3341
Cary Clark4855f782018-02-06 09:41:53 -05003342#Bug 7104
Cary Clarkbc5697d2017-10-04 14:31:33 -04003343#Example
3344#Height 128
Ben Wagner29380bd2017-10-09 14:43:00 -04003345 SkBitmap alpha, bitmap;
3346 bitmap.allocN32Pixels(100, 100);
3347 SkCanvas offscreen(bitmap);
3348 offscreen.clear(0);
3349 SkPaint paint;
3350 paint.setAntiAlias(true);
3351 paint.setColor(SK_ColorBLUE);
3352 paint.setStyle(SkPaint::kStroke_Style);
3353 paint.setStrokeWidth(20);
3354 offscreen.drawCircle(50, 50, 39, paint);
3355 offscreen.flush();
Cary Clark681287e2018-03-16 11:34:15 -04003356 paint.setMaskFilter(SkMaskFilter::MakeBlur(kOuter_SkBlurStyle, 3));
Ben Wagner29380bd2017-10-09 14:43:00 -04003357 SkIPoint offset;
3358 bitmap.extractAlpha(&alpha, &paint, nullptr, &offset);
3359 paint.setColor(SK_ColorRED);
3360 canvas->drawBitmap(bitmap, 0, -offset.fY, &paint);
3361 canvas->drawBitmap(alpha, 100 + offset.fX, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003362##
3363
3364#SeeAlso extractSubset
3365
3366##
3367
3368# ------------------------------------------------------------------------------
3369
3370#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05003371#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05003372#Line # returns Pixmap if possible ##
Cary Clark154beea2017-10-26 07:58:48 -04003373Copies Bitmap pixel address, row bytes, and Image_Info to pixmap, if address
3374is available, and returns true. If pixel address is not available, return
3375false and leave pixmap unchanged.
3376
3377pixmap contents become invalid on any future change to Bitmap.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003378
3379#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
3380
3381#Return true if Bitmap has direct access to pixels ##
3382
3383#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04003384 SkBitmap bitmap;
3385 bitmap.allocPixels(SkImageInfo::MakeN32Premul(6, 11));
3386 SkCanvas offscreen(bitmap);
3387 offscreen.clear(SK_ColorWHITE);
3388 SkPaint paint;
3389 offscreen.drawString("?", 0, 10, paint);
3390 SkPixmap pixmap;
3391 if (bitmap.peekPixels(&pixmap)) {
3392 const SkPMColor* pixels = pixmap.addr32();
3393 SkPMColor pmWhite = pixels[0];
3394 for (int y = 0; y < bitmap.height(); ++y) {
3395 for (int x = 0; x < bitmap.width(); ++x) {
3396 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
3397 }
3398 SkDebugf("\n");
3399 }
3400 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003401 #StdOut
Cary Clark2f466242017-12-11 16:03:17 -05003402------
3403-xxx--
3404x---x-
3405----x-
3406---x--
3407--x---
3408--x---
3409------
3410--x---
3411--x---
Cary Clarka560c472017-11-27 10:44:06 -05003412------
Cary Clarkbc5697d2017-10-04 14:31:33 -04003413 #StdOut ##
3414##
3415
Cary Clark682c58d2018-05-16 07:07:07 -04003416#SeeAlso pixmap() installPixels readPixels writePixels
Cary Clarkbc5697d2017-10-04 14:31:33 -04003417
3418##
3419
3420# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05003421#Subtopic Utility
3422#Populate
3423#Line # rarely called management functions ##
3424##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003425
Cary Clark154beea2017-10-26 07:58:48 -04003426#Method void validate() const;
Cary Clark78de7512018-02-07 07:27:09 -05003427#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05003428#Line # asserts if Bitmap is invalid (debug only) ##
Cary Clark682c58d2018-05-16 07:07:07 -04003429Asserts if internal values are illegal or inconsistent. Only available if
Cary Clark154beea2017-10-26 07:58:48 -04003430SK_DEBUG is defined at compile time.
3431
3432#NoExample
3433##
3434
Cary Clark06c20f32018-03-20 15:53:27 -04003435#SeeAlso SkImageInfo::validate
Cary Clark154beea2017-10-26 07:58:48 -04003436
3437##
3438
3439# ------------------------------------------------------------------------------
3440
Cary Clarkbc5697d2017-10-04 14:31:33 -04003441#Class SkBitmap ##
3442
3443#Topic Bitmap ##
Cary Clark4855f782018-02-06 09:41:53 -05003444