blob: 78677655e8356c4d05a870e56b8b6a9d357741a0 [file] [log] [blame]
Cary Clarkbc5697d2017-10-04 14:31:33 -04001#Topic Bitmap
2#Alias Bitmaps
3#Alias Bitmap_Reference
4
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
14draw methods. Bitmap flexibility as a pixel container limits some optimizations
15available to the target platform.
16
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
27#Topic Row_Bytes
28Bitmap pixels may be contiguous, or may have a gap at the end of each row.
29Row_Bytes is the interval from one row to the next. Row_Bytes may be specified;
30sometimes passing zero will compute the Row_Bytes from the row width and the
31number of bytes in a pixel. Row_Bytes may be larger than the row requires. This
32is useful to position one or more Bitmaps within a shared pixel array.
33##
34
35#Topic Overview
36
37#Subtopic Subtopics
38#ToDo manually add subtopics ##
39#Table
40#Legend
41# topics # description ##
42#Legend ##
43#Table ##
44##
45
46#Subtopic Structs
47#Table
48#Legend
49# description # struct ##
50#Legend ##
51# Allocator # ##
52# HeapAllocator # ##
53#Table ##
54#Subtopic ##
55
56#Subtopic Constructors
57#Table
58#Legend
59# description # function ##
60#Legend ##
61# SkBitmap() # Constructs with default values. ##
62# SkBitmap(SkBitmap&& src) # Takes ownership of pixels. ##
63# SkBitmap(const SkBitmap& src) # Shares ownership of pixels. ##
64# ~SkBitmap() # Releases ownership of pixels. ##
65#Table ##
66#Subtopic ##
67
68#Subtopic Operators
69#Table
70#Legend
71# description # function ##
72#Legend ##
73# SkBitmap& operator=(SkBitmap&& src) # Takes ownership of pixels. ##
74# SkBitmap& operator=(const SkBitmap& src) # Shares ownership of pixels. ##
75#Table ##
76#Subtopic ##
77
78#Subtopic Member_Functions
79#Table
80#Legend
81# description # function ##
82#Legend ##
83# ComputeIsOpaque # Returns true if all pixels are opaque. ##
84# allocN32Pixels # Allocates compatible Color_ARGB pixels, or aborts. ##
85# allocPixels # Allocates pixels from Image_Info, or aborts. ##
86# allocPixelsFlags # Allocates pixels from Image_Info with options, or aborts. ##
87# alphaType # Returns Image_Info Alpha_Type. ##
88# bounds() # Returns width and height as Rectangle. ##
89# bytesPerPixel # Returns number of bytes in pixel based on Color_Type. ##
90# colorSpace # Returns Image_Info Color_Space. ##
91# colorType # Returns Image_Info Color_Type. ##
Cary Clark154beea2017-10-26 07:58:48 -040092# computeByteSize # Returns size required for pixels. ##
Cary Clarkbc5697d2017-10-04 14:31:33 -040093# dimensions # Returns width and height. ##
94# drawsNothing # Returns true if no width, no height, or no Pixel_Ref. ##
95# empty() # Returns true if Image_Info has zero width or height. ##
96# erase() # Writes Color to rectangle of pixels. ##
97# eraseARGB # Writes Color to pixels. ##
98# eraseArea # Deprecated ##
99# eraseColor # Writes Color to pixels. ##
Cary Clark154beea2017-10-26 07:58:48 -0400100# eraseRGB # Deprecated ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400101# extractAlpha # Creates Bitmap containing Alpha of pixels. ##
102# extractSubset # Creates Bitmap, sharing pixels if possible. ##
103# getAddr # Returns readable pixel address as void pointer. ##
104# getAddr16 # Returns readable pixel address as 16-bit pointer. ##
105# getAddr32 # Returns readable pixel address as 32-bit pointer. ##
106# getAddr8 # Returns readable pixel address as 8-bit pointer. ##
107# getBounds # Returns width and height as Rectangle. ##
108# getColor # Returns one pixel as Unpremultiplied Color. ##
109# getGenerationID # Returns unique ID. ##
110# getPixels # Returns address of pixels. ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400111# getSubset # Returns bounds offset by origin. ##
112# hasHardwareMipMap # Returns Mip_Map support present; Android only. ##
113# height # Returns pixel row count. ##
114# info() # Returns Image_Info. ##
115# installMaskPixels # Creates Pixel_Ref from Mask. ##
116# installPixels # Creates Pixel_Ref, with optional release function. ##
117# isImmutable # Returns true if pixels will not change. ##
118# isNull # Returns true if Pixel_Ref is nullptr. ##
119# isOpaque # Returns true if Image_Info describes opaque pixels. ##
120# isVolatile # Returns true if pixels should not be cached. ##
121# notifyPixelsChanged # Marks pixels as changed, altering the unique ID. ##
122# peekPixels # Returns Pixmap if possible. ##
123# pixelRef # Returns Pixel_Ref, or nullptr. ##
124# pixelRefOrigin # Returns offset within Pixel_Ref. ##
125# readPixels # Copies and converts pixels. ##
126# readyToDraw # Returns true if address of pixels is not nullptr. ##
127# refColorSpace # Returns Image_Info Color_Space. ##
128# reset() # Sets to default values, releases pixel ownership. ##
129# rowBytes # Returns interval between rows in bytes. ##
130# rowBytesAsPixels # Returns interval between rows in pixels. ##
131# setAlphaType # Sets Alpha_Type of shared pixels. ##
132# setHasHardwareMipMap # Sets Mip_Map support present; Android only. ##
133# setImmutable # Marks that pixels will not change. ##
134# setInfo # Sets height, width, Color_Type, and so on, releasing pixels. ##
135# setIsVolatile # Marks if pixels should not be cached. ##
136# setPixelRef # Sets Pixel_Ref and offset. ##
137# setPixels # Sets Pixel_Ref without an offset. ##
138# shiftPerPixel # Returns bit shift from pixels to bytes. ##
139# swap() # Exchanges Bitmap pair. ##
140# toString # Converts Bitmap to machine readable form. ##
141# tryAllocN32Pixels # Allocates compatible Color_ARGB pixels if possible. ##
142# tryAllocPixels # Allocates pixels from Image_Info if possible. ##
143# tryAllocPixelsFlags # Allocates pixels from Image_Info with options if possible. ##
Cary Clark154beea2017-10-26 07:58:48 -0400144# validate() # Asserts if Bitmap is invalid (debug only). ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400145# width() # Returns pixel column count. ##
146# writePixels # Copies and converts pixels. ##
147#Table ##
148#Subtopic ##
149
150#Topic ##
151
152# ------------------------------------------------------------------------------
153
154#Class Allocator
155
156#Code
157 class Allocator : public SkRefCnt {
158 public:
159 virtual bool allocPixelRef(SkBitmap* bitmap) = 0;
160 };
161##
162
163Abstract subclass of HeapAllocator.
164
165# ------------------------------------------------------------------------------
166
167#Method virtual bool allocPixelRef(SkBitmap* bitmap) = 0
168
169Allocates the pixel memory for the bitmap, given its dimensions and
170Color_Type. Returns true on success, where success means either setPixels
171or setPixelRef was called.
172
173#Param bitmap Bitmap containing Image_Info as input, and Pixel_Ref as output ##
174
175#Return true if Pixel_Ref was allocated ##
176
177#NoExample
178##
179
180#SeeAlso HeapAllocator
181
182##
183
184#Class Allocator ##
185
186# ------------------------------------------------------------------------------
187
188#Class HeapAllocator
189
190#Code
191 class HeapAllocator : public Allocator {
192 public:
193 bool allocPixelRef(SkBitmap* bitmap) override;
194 };
195##
196
197Subclass of Allocator that returns a Pixel_Ref that allocates its pixel
198memory from the heap. This is the default Allocator invoked by
199allocPixels.
200
201# ------------------------------------------------------------------------------
202
203#Method bool allocPixelRef(SkBitmap* bitmap) override
204
205Allocates the pixel memory for the bitmap, given its dimensions and
206Color_Type. Returns true on success, where success means either setPixels
207or setPixelRef was called.
208
209#Param bitmap Bitmap containing Image_Info as input, and Pixel_Ref as output ##
210
211#Return true if pixels are allocated ##
212
213#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400214 SkBitmap bitmap;
215 bitmap.setInfo(SkImageInfo::MakeN32(16, 16, kPremul_SkAlphaType));
216 SkDebugf("pixel address = %p\n", bitmap.getPixels());
217 SkBitmap::HeapAllocator stdalloc;
218 if (!stdalloc.allocPixelRef(&bitmap)) {
219 SkDebugf("pixel allocation failed\n");
220 } else {
221 SkDebugf("pixel address = %p\n", bitmap.getPixels());
222 }
223#StdOut
Cary Clark884dd7d2017-10-11 10:37:52 -0400224#Volatile
Ben Wagner29380bd2017-10-09 14:43:00 -0400225pixel address = (nil)
226pixel address = 0x560ddd0ac670
227##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400228##
229
230#SeeAlso Allocator tryAllocPixels
231
232##
233
234#class HeapAllocator ##
235
236# ------------------------------------------------------------------------------
237
238#Method SkBitmap()
239
240Creates an empty Bitmap without pixels, with kUnknown_SkColorType,
241kUnknown_SkAlphaType, and with a width and height of zero. Pixel_Ref origin is
242set to (0, 0). Bitmap is not volatile.
243
244Use setInfo to associate SkColorType, SkAlphaType, width, and height
245after Bitmap has been created.
246
247#Return empty Bitmap ##
248
249#Example
250void draw(SkCanvas* canvas) {
251 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
252 const char* colors[] = {"Unknown", "Alpha", "RGB_565", "ARGB_4444", "RGBA_8888", "BGRA_8888",
253 "Gray_8", "RGBA_F16"};
254 SkBitmap bitmap;
255 for (int i = 0; i < 2; ++i) {
256 SkDebugf("width: %2d height: %2d", bitmap.width(), bitmap.height());
257 SkDebugf(" color: k%s_SkColorType", colors[bitmap.colorType()]);
258 SkDebugf(" alpha: k%s_SkAlphaType\n", alphas[bitmap.alphaType()]);
259 bitmap.setInfo(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType),
260 0);
261 }
262}
263#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400264width: 0 height: 0 color: kUnknown_SkColorType alpha: kUnknown_SkAlphaType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400265width: 25 height: 35 color: kRGBA_8888_SkColorType alpha: kOpaque_SkAlphaType
266##
267##
268
269#SeeAlso setInfo
270
271##
272
273# ------------------------------------------------------------------------------
274
275#Method SkBitmap(const SkBitmap& src)
276
277Copies settings from src to returned Bitmap. Shares pixels if src has pixels
278allocated, so both bitmaps reference the same pixels.
279
280#Param src Bitmap to copy Image_Info, and share Pixel_Ref ##
281
282#Return copy of src ##
283
284#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400285void draw(SkCanvas* canvas) {
286 SkBitmap original;
287 original.tryAllocPixels(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
288 SkDebugf("original has pixels before copy: %s\n", original.getPixels() ? "true" : "false");
289 SkBitmap copy(original);
290 SkDebugf("original has pixels after copy: %s\n", original.getPixels() ? "true" : "false");
291 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
Cary Clarkbc5697d2017-10-04 14:31:33 -0400292}
293#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400294original has pixels before copy: true
295original has pixels after copy: true
Cary Clarkbc5697d2017-10-04 14:31:33 -0400296copy has pixels: true
297##
298##
299
300#SeeAlso setInfo setPixelRef setPixels swap
301
302##
303
304# ------------------------------------------------------------------------------
305
306#Method SkBitmap(SkBitmap&& src)
307
308Copies 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;
318 original.tryAllocPixels(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
319 SkDebugf("original has pixels before move: %s\n", original.getPixels() ? "true" : "false");
320 SkBitmap copy(std::move(original));
321 SkDebugf("original has pixels after move: %s\n", original.getPixels() ? "true" : "false");
322 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
323}
Cary Clarkbc5697d2017-10-04 14:31:33 -0400324#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400325original has pixels before move: true
326original has pixels after move: false
Cary Clarkbc5697d2017-10-04 14:31:33 -0400327copy has pixels: true
328##
329##
330
331#SeeAlso setInfo setPixelRef setPixels swap
332
333##
334
335# ------------------------------------------------------------------------------
336
337#Method ~SkBitmap()
338
339Decrements Pixel_Ref reference count, if Pixel_Ref is not nullptr.
340
341#NoExample
342##
343
344#SeeAlso Pixel_Ref
345
346##
347
348# ------------------------------------------------------------------------------
349
350#Method SkBitmap& operator=(const SkBitmap& src)
351
352Copies settings from src to returned Bitmap. Shares pixels if src has pixels
353allocated, so both bitmaps reference the same pixels.
354
355#Param src Bitmap to copy Image_Info, and share Pixel_Ref ##
356
357#Return copy of src ##
358
359#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400360void draw(SkCanvas* canvas) {
361 SkBitmap original;
362 original.tryAllocPixels(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
363 SkDebugf("original has pixels before copy: %s\n", original.getPixels() ? "true" : "false");
364 SkBitmap copy = original;
365 SkDebugf("original has pixels after copy: %s\n", original.getPixels() ? "true" : "false");
366 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
Cary Clarkbc5697d2017-10-04 14:31:33 -0400367}
368#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400369original has pixels before copy: true
370original has pixels after copy: true
Cary Clarkbc5697d2017-10-04 14:31:33 -0400371copy has pixels: true
372##
373##
374
375#SeeAlso setInfo setPixelRef setPixels swap
376
377##
378
379# ------------------------------------------------------------------------------
380
381#Method SkBitmap& operator=(SkBitmap&& src)
382
383Copies settings from src to returned Bitmap. Moves ownership of src pixels to
384Bitmap.
385
386#Param src Bitmap to copy Image_Info, and reassign Pixel_Ref ##
387
388#Return copy of src ##
389
390#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400391void draw(SkCanvas* canvas) {
392 SkBitmap original;
393 original.tryAllocPixels(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
394 SkDebugf("original has pixels before move: %s\n", original.getPixels() ? "true" : "false");
395 SkBitmap copy = std::move(original);
396 SkDebugf("original has pixels after move: %s\n", original.getPixels() ? "true" : "false");
397 SkDebugf("copy has pixels: %s\n", copy.getPixels() ? "true" : "false");
398}
Cary Clarkbc5697d2017-10-04 14:31:33 -0400399#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400400original has pixels before move: true
401original has pixels after move: false
Cary Clarkbc5697d2017-10-04 14:31:33 -0400402copy has pixels: true
403##
404##
405
406#SeeAlso setInfo setPixelRef setPixels swap
407
408##
409
410# ------------------------------------------------------------------------------
411
412#Method void swap(SkBitmap& other)
413
414Swaps the fields of the two bitmaps.
415
416#Param other Bitmap exchanged with original ##
417
418#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400419void draw(SkCanvas* canvas) {
420 auto debugster = [](const char* prefix, const SkBitmap& b) -> void {
421 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
422 const char* colors[] = {"Unknown", "Alpha", "RGB_565", "ARGB_4444", "RGBA_8888", "BGRA_8888",
423 "Gray_8", "RGBA_F16"};
424 SkDebugf("%s width:%d height:%d colorType:k%s_SkColorType alphaType:k%s_SkAlphaType\n",
425 prefix, b.width(), b.height(), colors[b.colorType()], alphas[b.alphaType()]);
426 };
427 SkBitmap one, two;
428 one.tryAllocPixels(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
429 two.tryAllocPixels(SkImageInfo::Make(2, 2, kBGRA_8888_SkColorType, kPremul_SkAlphaType));
430 for (int index = 0; index < 2; ++index) {
431 debugster("one", one);
432 debugster("two", two);
433 one.swap(two);
434 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400435}
436#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400437one width:1 height:1 colorType:kRGBA_8888_SkColorType alphaType:kOpaque_SkAlphaType
438two width:2 height:2 colorType:kBGRA_8888_SkColorType alphaType:kPremul_SkAlphaType
439one width:2 height:2 colorType:kBGRA_8888_SkColorType alphaType:kPremul_SkAlphaType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400440two width:1 height:1 colorType:kRGBA_8888_SkColorType alphaType:kOpaque_SkAlphaType
441##
442##
443
444#SeeAlso SkBitmap(SkBitmap&& src) operator=(SkBitmap&& src)
445
446##
447
448# ------------------------------------------------------------------------------
449
450#Method const SkImageInfo& info() const
451
452Returns width, height, Alpha_Type, Color_Type, and Color_Space.
453
454#Return reference to Image_Info ##
455
456#Example
457#Image 4
Ben Wagner29380bd2017-10-09 14:43:00 -0400458void draw(SkCanvas* canvas) {
459 // SkBitmap source; // pre-populated with soccer ball by fiddle.skia.org
460 const SkImageInfo& info = source.info();
461 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
462 const char* colors[] = {"Unknown", "Alpha", "RGB_565", "ARGB_4444",
463 "RGBA_8888", "BGRA_8888", "Gray_8", "RGBA_F16"};
464 SkDebugf("width: %d height: %d color: %s alpha: %s\n", info.width(), info.height(),
465 colors[info.colorType()], alphas[info.alphaType()]);
466#StdOut
467width: 56 height: 56 color: BGRA_8888 alpha: Opaque
468##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400469}
470##
471
472#SeeAlso Image_Info
473
474##
475
476# ------------------------------------------------------------------------------
477
478#Method int width() const
479
480Returns pixel count in each pixel row. Should be equal or less than:
Cary Clark154beea2017-10-26 07:58:48 -0400481
Cary Clarkbc5697d2017-10-04 14:31:33 -0400482#Formula
483rowBytes() / info().bytesPerPixel()
484##
485.
486
487Maybe be less than pixelRef().width(). Will not exceed pixelRef().width() less
488pixelRefOrigin().fX.
489
490#Return pixel width in Image_Info ##
491
492#Example
493 SkImageInfo info = SkImageInfo::MakeA8(16, 32);
494 SkBitmap bitmap;
495 bitmap.setInfo(info);
496 SkDebugf("bitmap width: %d info width: %d\n", bitmap.width(), info.width());
497#StdOut
498bitmap width: 16 info width: 16
499##
500##
501
502#SeeAlso height() SkPixelRef::width() SkImageInfo::width()
503
504##
505
506# ------------------------------------------------------------------------------
507
508#Method int height() const
509
510Returns pixel row count.
511
512Maybe be less than pixelRef().height(). Will not exceed pixelRef().height() less
513pixelRefOrigin().fY.
514
515#Return pixel height in Image_Info ##
516
517#Example
518 SkImageInfo info = SkImageInfo::MakeA8(16, 32);
519 SkBitmap bitmap;
520 bitmap.setInfo(info);
521 SkDebugf("bitmap height: %d info height: %d\n", bitmap.height(), info.height());
522#StdOut
523bitmap height: 32 info height: 32
524##
525##
526
527#SeeAlso width() SkPixelRef::height() SkImageInfo::height()
528
529##
530
531# ------------------------------------------------------------------------------
532
533#Method SkColorType colorType() const
534
535Returns Color_Type, one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
536kRGB_565_SkColorType, kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
537kBGRA_8888_SkColorType, kGray_8_SkColorType, kRGBA_F16_SkColorType.
538
539#Return Color_Type in Image_Info ##
540
541#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400542 const char* colors[] = {"Unknown", "Alpha", "RGB_565", "ARGB_4444",
543 "RGBA_8888", "BGRA_8888", "Gray_8", "RGBA_F16"};
544 SkBitmap bitmap;
545 bitmap.setInfo(SkImageInfo::MakeA8(16, 32));
546 SkDebugf("color type: k" "%s" "_SkColorType\n", colors[bitmap.colorType()]);
Cary Clarkbc5697d2017-10-04 14:31:33 -0400547#StdOut
548color type: kAlpha_SkColorType
549##
550##
551
552#SeeAlso alphaType() SkImageInfo::colorType
553
554##
555
556# ------------------------------------------------------------------------------
557
558#Method SkAlphaType alphaType() const
559
560Returns Alpha_Type, one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
561kPremul_SkAlphaType, kUnpremul_SkAlphaType.
562
563#Return Alpha_Type in Image_Info ##
564
565#Example
566 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
567 SkPixmap pixmap(SkImageInfo::MakeA8(16, 32), nullptr, 64);
568 SkDebugf("alpha type: k" "%s" "_SkAlphaType\n", alphas[pixmap.alphaType()]);
569#StdOut
570alpha type: kPremul_SkAlphaType
571##
572##
573
574#SeeAlso colorType() SkImageInfo::alphaType
575
576##
577
578# ------------------------------------------------------------------------------
579
580#Method SkColorSpace* colorSpace() const
581
582Returns Color_Space, the range of colors, associated with Image_Info. The
583reference count of Color_Space is unchanged. The returned Color_Space is
584immutable.
585
586#Return Color_Space in Image_Info ##
587
588#Example
589#Description
590SkColorSpace::MakeSRGBLinear creates Color_Space with linear gamma
591and an sRGB gamut. This Color_Space gamma is not close to sRGB gamma.
592##
Ben Wagner29380bd2017-10-09 14:43:00 -0400593 SkBitmap bitmap;
Cary Clarkbc5697d2017-10-04 14:31:33 -0400594 bitmap.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
595 SkColorSpace::MakeSRGBLinear()));
596 SkColorSpace* colorSpace = bitmap.colorSpace();
597 SkDebugf("gammaCloseToSRGB: %s gammaIsLinear: %s isSRGB: %s\n",
598 colorSpace->gammaCloseToSRGB() ? "true" : "false",
599 colorSpace->gammaIsLinear() ? "true" : "false",
600 colorSpace->isSRGB() ? "true" : "false");
601#StdOut
602gammaCloseToSRGB: false gammaIsLinear: true isSRGB: false
603##
604##
605
606#SeeAlso Color_Space SkImageInfo::colorSpace
607
608##
609
610# ------------------------------------------------------------------------------
611
612#Method sk_sp<SkColorSpace> refColorSpace() const
613
614Returns a smart pointer to Color_Space, the range of colors, associated with
615Image_Info. The smart pointer tracks the number of objects sharing this
616Color_Space reference so the memory is released when the owners destruct.
617
618The returned Color_Space is immutable.
619
620#Return Color_Space in Image_Info wrapped in a smart pointer ##
621
622#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400623 SkBitmap bitmap1, bitmap2;
Cary Clarkbc5697d2017-10-04 14:31:33 -0400624 bitmap1.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
625 SkColorSpace::MakeSRGBLinear()));
626 bitmap2.setInfo(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
627 bitmap1.refColorSpace()));
628 SkColorSpace* colorSpace = bitmap2.colorSpace();
629 SkDebugf("gammaCloseToSRGB: %s gammaIsLinear: %s isSRGB: %s\n",
630 colorSpace->gammaCloseToSRGB() ? "true" : "false",
631 colorSpace->gammaIsLinear() ? "true" : "false",
632 colorSpace->isSRGB() ? "true" : "false");
633#StdOut
634gammaCloseToSRGB: false gammaIsLinear: true isSRGB: false
635##
636##
637
638#SeeAlso Color_Space SkImageInfo::colorSpace
639
640##
641
642# ------------------------------------------------------------------------------
643
644#Method int bytesPerPixel() const
645
646Returns number of bytes per pixel required by Color_Type.
647Returns zero if colorType( is kUnknown_SkColorType.
648
649#Return bytes in pixel ##
650
651#Example
652 const char* colors[] = {"Unknown", "Alpha", "RGB_565", "ARGB_4444",
653 "RGBA_8888", "BGRA_8888", "Gray_8", "RGBA_F16"};
654 SkImageInfo info = SkImageInfo::MakeA8(1, 1);
655 SkBitmap bitmap;
656 for (SkColorType colorType : { kUnknown_SkColorType, kAlpha_8_SkColorType,
657 kRGB_565_SkColorType, kARGB_4444_SkColorType,
658 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
659 kGray_8_SkColorType, kRGBA_F16_SkColorType } ) {
660 bitmap.setInfo(info.makeColorType(colorType));
661 SkDebugf("color: k" "%s" "_SkColorType" "%*s" "bytesPerPixel: %d\n",
662 colors[colorType], 10 - strlen(colors[colorType]), " ",
663 bitmap.bytesPerPixel());
664 }
665#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400666color: kUnknown_SkColorType bytesPerPixel: 0
667color: kAlpha_SkColorType bytesPerPixel: 1
668color: kRGB_565_SkColorType bytesPerPixel: 2
669color: kARGB_4444_SkColorType bytesPerPixel: 2
670color: kRGBA_8888_SkColorType bytesPerPixel: 4
671color: kBGRA_8888_SkColorType bytesPerPixel: 4
672color: kGray_8_SkColorType bytesPerPixel: 1
Cary Clarkbc5697d2017-10-04 14:31:33 -0400673color: kRGBA_F16_SkColorType bytesPerPixel: 8
674##
675##
676
677#SeeAlso rowBytes rowBytesAsPixels width shiftPerPixel
678
679##
680
681# ------------------------------------------------------------------------------
682
683#Method int rowBytesAsPixels() const
684
685Returns number of pixels that fit on row. Should be greater than or equal to
686width().
687
688#Return maximum pixels per row ##
689
690#Example
691 SkBitmap bitmap;
692 for (int rowBytes : { 4, 5, 6, 7, 8} ) {
693 bitmap.setInfo(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), rowBytes);
694 SkDebugf("rowBytes: %d rowBytesAsPixels: %d\n", rowBytes, bitmap.rowBytesAsPixels());
695 }
696#StdOut
697rowBytes: 4 rowBytesAsPixels: 1
698rowBytes: 5 rowBytesAsPixels: 1
699rowBytes: 6 rowBytesAsPixels: 1
700rowBytes: 7 rowBytesAsPixels: 1
701rowBytes: 8 rowBytesAsPixels: 2
702##
703##
704
705#SeeAlso rowBytes shiftPerPixel width bytesPerPixel
706
707##
708
709# ------------------------------------------------------------------------------
710
711#Method int shiftPerPixel() const
712
713Returns bit shift converting row bytes to row pixels.
714Returns zero for kUnknown_SkColorType.
715
716#Return one of: 0, 1, 2, 3; left shift to convert pixels to bytes ##
717
718#Example
719 const char* colors[] = {"Unknown", "Alpha", "RGB_565", "ARGB_4444",
720 "RGBA_8888", "BGRA_8888", "Gray_8", "RGBA_F16"};
721 SkImageInfo info = SkImageInfo::MakeA8(1, 1);
722 SkBitmap bitmap;
723 for (SkColorType colorType : { kUnknown_SkColorType, kAlpha_8_SkColorType,
724 kRGB_565_SkColorType, kARGB_4444_SkColorType,
725 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
726 kGray_8_SkColorType, kRGBA_F16_SkColorType } ) {
727 bitmap.setInfo(info.makeColorType(colorType));
728 SkDebugf("color: k" "%s" "_SkColorType" "%*s" "shiftPerPixel: %d\n",
729 colors[colorType], 10 - strlen(colors[colorType]), " ",
730 bitmap.shiftPerPixel());
731 }
732#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400733color: kUnknown_SkColorType shiftPerPixel: 0
734color: kAlpha_SkColorType shiftPerPixel: 0
735color: kRGB_565_SkColorType shiftPerPixel: 1
736color: kARGB_4444_SkColorType shiftPerPixel: 1
737color: kRGBA_8888_SkColorType shiftPerPixel: 2
738color: kBGRA_8888_SkColorType shiftPerPixel: 2
739color: kGray_8_SkColorType shiftPerPixel: 0
Cary Clarkbc5697d2017-10-04 14:31:33 -0400740color: kRGBA_F16_SkColorType shiftPerPixel: 3
741##
742##
743
744#SeeAlso rowBytes rowBytesAsPixels width bytesPerPixel
745
746##
747
748# ------------------------------------------------------------------------------
749
750#Method bool empty() const
751
752Returns true if either width() or height() are zero.
753
754Does not check if Pixel_Ref is nullptr; call drawsNothing to check width(),
755height(), and Pixel_Ref.
756
757#Return true if dimensions do not enclose area ##
758
759#Example
760 SkBitmap bitmap;
761 for (int width : { 0, 2 } ) {
762 for (int height : { 0, 2 } ) {
763 bitmap.setInfo(SkImageInfo::MakeA8(width, height));
764 SkDebugf("width: %d height: %d empty: %s\n", width, height,
765 bitmap.empty() ? "true" : "false");
766 }
767 }
768#StdOut
769width: 0 height: 0 empty: true
770width: 0 height: 2 empty: true
771width: 2 height: 0 empty: true
772width: 2 height: 2 empty: false
773##
774##
775
776#SeeAlso height() width() drawsNothing
777
778##
779
780# ------------------------------------------------------------------------------
781
782#Method bool isNull() const
783
784Return true if Pixel_Ref is nullptr.
785
786Does not check if width() or height() are zero; call drawsNothing to check
787width(), height(), and Pixel_Ref.
788
789#Return true if no Pixel_Ref is associated ##
790
791#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400792 SkBitmap bitmap;
793 SkDebugf("empty bitmap does %shave pixels\n", bitmap.isNull() ? "not " : "");
794 bitmap.setInfo(SkImageInfo::MakeA8(8, 8));
795 SkDebugf("bitmap with dimensions does %shave pixels\n", bitmap.isNull() ? "not " : "");
796 bitmap.allocPixels();
797 SkDebugf("allocated bitmap does %shave pixels\n", bitmap.isNull() ? "not " : "");
Cary Clarkbc5697d2017-10-04 14:31:33 -0400798#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400799empty bitmap does not have pixels
800bitmap with dimensions does not have pixels
Cary Clarkbc5697d2017-10-04 14:31:33 -0400801allocated bitmap does have pixels
802##
803##
804
805#SeeAlso empty() drawsNothing pixelRef
806
807##
808
809# ------------------------------------------------------------------------------
810
811#Method bool drawsNothing() const
812
813Return true if width() or height() are zero, or if Pixel_Ref is nullptr.
814If true, Bitmap has no effect when drawn or drawn into.
815
816#Return true if drawing has no effect ##
817
818#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400819 SkBitmap bitmap;
820 for (int w : { 0, 8 } ) {
821 for (bool allocate : { false, true} ) {
822 bitmap.setInfo(SkImageInfo::MakeA8(w, 8));
823 allocate ? bitmap.allocPixels() : (void) 0 ;
824 SkDebugf("empty:%s isNull:%s drawsNothing:%s\n", bitmap.empty() ? "true " : "false",
825 bitmap.isNull() ? "true " : "false", bitmap.drawsNothing() ? "true" : "false");
826 }
827 }
828#StdOut
829empty:true isNull:true drawsNothing:true
830empty:true isNull:false drawsNothing:true
831empty:false isNull:true drawsNothing:true
832empty:false isNull:false drawsNothing:false
833##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400834##
835
836#SeeAlso empty() isNull pixelRef
837
838##
839
840# ------------------------------------------------------------------------------
841
842#Method size_t rowBytes() const
843
844Returns row bytes, the interval from one pixel row to the next. Row bytes
845is at least as large as
846#Formula
847width() * info().bytesPerPixel()
848##
849.
850
851Returns zero if colorType is kUnknown_SkColorType, or if row bytes supplied to
852setInfo is not large enough to hold a row of pixels.
853
854#Return byte length of pixel row ##
855
856#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400857 SkBitmap bitmap;
858 for (int rowBytes : { 2, 8 } ) {
859 bool result = bitmap.setInfo(SkImageInfo::MakeA8(4, 4), rowBytes);
860 SkDebugf("setInfo returned:%s rowBytes:%d\n", result ? "true " : "false", bitmap.rowBytes());
861 }
862#StdOut
863setInfo returned:false rowBytes:0
864setInfo returned:true rowBytes:8
865##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400866##
867
868#SeeAlso info() setInfo SkImageInfo::minRowBytes
869
870##
871
872# ------------------------------------------------------------------------------
873
874#Method bool setAlphaType(SkAlphaType alphaType)
875
876Sets Alpha_Type, if alphaType is compatible with Color_Type.
877Returns true unless alphaType is kUnknown_SkAlphaType and current Alpha_Type
878is not kUnknown_SkAlphaType.
879
880Returns true if Color_Type is kUnknown_SkColorType. alphaType is ignored, and
881Alpha_Type remains kUnknown_SkAlphaType.
882
883Returns true if Color_Type is kRGB_565_SkColorType or kGray_8_SkColorType.
884alphaType is ignored, and Alpha_Type remains kOpaque_SkAlphaType.
885
886If Color_Type is kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
887kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: returns true unless
888alphaType is kUnknown_SkAlphaType and Alpha_Type is not kUnknown_SkAlphaType.
889If Alpha_Type is kUnknown_SkAlphaType, alphaType is ignored.
890
891If Color_Type is kAlpha_8_SkColorType, returns true unless
892alphaType is kUnknown_SkAlphaType and Alpha_Type is not kUnknown_SkAlphaType.
893If Alpha_Type is kUnknown_SkAlphaType, alphaType is ignored. If alphaType is
894kUnpremul_SkAlphaType, it is treated as kPremul_SkAlphaType.
895
896This changes Alpha_Type in Pixel_Ref; all bitmaps sharing Pixel_Ref
897are affected.
898
899#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
900 kPremul_SkAlphaType, kUnpremul_SkAlphaType
901##
902
903#Return true if Alpha_Type is set ##
904
905#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400906void draw(SkCanvas* canvas) {
907 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444",
908 "RGBA_8888", "BGRA_8888", "Gray_8", "RGBA_F16"};
909 const char* alphas[] = {"Unknown ", "Opaque ", "Premul ", "Unpremul"};
910
911 SkBitmap bitmap;
912 SkAlphaType alphaTypes[] = { kUnknown_SkAlphaType, kOpaque_SkAlphaType,
913 kPremul_SkAlphaType, kUnpremul_SkAlphaType };
914 SkDebugf("%88s", "Canonical Unknown Opaque Premul Unpremul\n");
915 for (SkColorType colorType : { kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
916 kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
917 kGray_8_SkColorType, kRGBA_F16_SkColorType } ) {
918 for (SkAlphaType canonicalAlphaType : alphaTypes) {
919 SkColorTypeValidateAlphaType(colorType, kUnknown_SkAlphaType, &canonicalAlphaType );
920 SkDebugf("%10s %10s ", colors[(int) colorType], alphas[(int) canonicalAlphaType ]);
921 for (SkAlphaType alphaType : alphaTypes) {
922 bitmap.setInfo(SkImageInfo::Make(4, 4, colorType, canonicalAlphaType));
923 bool result = bitmap.setAlphaType(alphaType);
924 SkDebugf("%s %s ", result ? "true " : "false", alphas[(int) bitmap.alphaType()]);
925 }
926 SkDebugf("\n");
927 }
928 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400929}
930##
931
932#SeeAlso Alpha_Type Color_Type Image_Info setInfo
933
934##
935
936# ------------------------------------------------------------------------------
937
938#Method void* getPixels() const
939
940Returns pixel address, the base address corresponding to the pixel origin.
941
942#Return pixel address ##
943
944#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400945 SkBitmap bitmap;
946 bitmap.setInfo(SkImageInfo::MakeN32(4, 4, kPremul_SkAlphaType));
947 bitmap.allocPixels();
948 bitmap.eraseColor(0x00000000);
949 void* baseAddr = bitmap.getPixels();
950 *(SkPMColor*)baseAddr = 0xFFFFFFFF;
951 SkDebugf("bitmap.getColor(0, 1) %c= 0x00000000\n",
952 bitmap.getColor(0, 1) == 0x00000000 ? '=' : '!');
953 SkDebugf("bitmap.getColor(0, 0) %c= 0xFFFFFFFF\n",
954 bitmap.getColor(0, 0) == 0xFFFFFFFF ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -0400955#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400956bitmap.getColor(0, 1) == 0x00000000
Cary Clarkbc5697d2017-10-04 14:31:33 -0400957bitmap.getColor(0, 0) == 0xFFFFFFFF
958##
959##
960
961#SeeAlso isNull drawsNothing
962
963##
964
965# ------------------------------------------------------------------------------
966
967#Method size_t computeByteSize() const
968
969Returns minimum memory required for pixel storage.
970Does not include unused memory on last row when rowBytesAsPixels exceeds width().
971Returns zero if result does not fit in size_t.
972Returns zero if height() or width() is 0.
973Returns height() times rowBytes if colorType is kUnknown_SkColorType.
974
975#Return size in bytes of image buffer ##
976
977#Example
Ben Wagner29380bd2017-10-09 14:43:00 -0400978 SkBitmap bitmap;
Cary Clarkbc5697d2017-10-04 14:31:33 -0400979 for (int width : { 1, 1000, 1000000 } ) {
980 for (int height: { 1, 1000, 1000000 } ) {
981 SkImageInfo imageInfo = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
982 bitmap.setInfo(imageInfo, width * 5);
983 SkDebugf("width: %7d height: %7d computeByteSize: %13lld\n", width, height,
984 bitmap.computeByteSize());
985 }
986 }
987#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400988width: 1 height: 1 computeByteSize: 4
989width: 1 height: 1000 computeByteSize: 4999
990width: 1 height: 1000000 computeByteSize: 4999999
991width: 1000 height: 1 computeByteSize: 4000
992width: 1000 height: 1000 computeByteSize: 4999000
993width: 1000 height: 1000000 computeByteSize: 4999999000
994width: 1000000 height: 1 computeByteSize: 4000000
995width: 1000000 height: 1000 computeByteSize: 4999000000
Cary Clarkbc5697d2017-10-04 14:31:33 -0400996width: 1000000 height: 1000000 computeByteSize: 4999999000000
997##
998##
999
1000#SeeAlso SkImageInfo::computeByteSize
1001
1002##
1003
1004# ------------------------------------------------------------------------------
1005
Cary Clarkbc5697d2017-10-04 14:31:33 -04001006#Method bool isImmutable() const
1007
1008Returns true if pixels can not change.
1009
1010Most immutable Bitmap checks trigger an assert only on debug builds.
1011
1012#Return true if pixels are immutable ##
1013
1014#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001015 SkBitmap original;
1016 SkImageInfo info = SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1017 if (original.tryAllocPixels(info)) {
1018 original.setImmutable();
1019 SkBitmap copy;
1020 original.extractSubset(&copy, {5, 10, 15, 20});
1021 SkDebugf("original is " "%s" "immutable\n", original.isImmutable() ? "" : "not ");
1022 SkDebugf("copy is " "%s" "immutable\n", copy.isImmutable() ? "" : "not ");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001023 }
1024#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001025original is immutable
Cary Clarkbc5697d2017-10-04 14:31:33 -04001026copy is immutable
1027##
1028##
1029
1030#SeeAlso setImmutable SkPixelRef::isImmutable SkImage
1031
1032##
1033
1034# ------------------------------------------------------------------------------
1035
1036#Method void setImmutable()
1037
Cary Clark154beea2017-10-26 07:58:48 -04001038Sets internal flag to mark Bitmap as immutable. Once set, pixels can not change.
1039Any other bitmap sharing the same Pixel_Ref are also marked as immutable.
1040Once 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.
1043
1044#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
1066
1067Returns true if Alpha_Type is kOpaque_SkAlphaType.
1068Does not check if Color_Type allows Alpha, or if any pixel value has
1069transparency.
1070
1071#Return true if Image_Info describes opaque Alpha ##
1072
1073#Example
1074#Description
1075 isOpaque ignores whether all pixels are opaque or not.
1076##
Ben Wagner29380bd2017-10-09 14:43:00 -04001077 const int height = 2;
1078 const int width = 2;
1079 SkBitmap bitmap;
1080 bitmap.setInfo(SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType));
1081 for (int index = 0; index < 2; ++index) {
1082 bitmap.allocPixels();
1083 bitmap.eraseColor(0x00000000);
1084 SkDebugf("isOpaque: %s\n", bitmap.isOpaque() ? "true" : "false");
1085 bitmap.eraseColor(0xFFFFFFFF);
1086 SkDebugf("isOpaque: %s\n", bitmap.isOpaque() ? "true" : "false");
1087 bitmap.setInfo(bitmap.info().makeAlphaType(kOpaque_SkAlphaType));
1088 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001089#StdOut
1090isOpaque: false
1091isOpaque: false
1092isOpaque: true
1093isOpaque: true
1094##
1095##
1096
1097#SeeAlso ComputeIsOpaque SkImageInfo::isOpaque
1098
1099##
1100
1101# ------------------------------------------------------------------------------
1102
1103#Method bool isVolatile() const
1104
1105If true, provides a hint to caller that pixels should not
1106be cached. Only true if setIsVolatile has been called to mark as volatile.
1107
1108Volatile state is not shared by other bitmaps sharing the same Pixel_Ref.
1109
1110#Return true if marked volatile ##
1111
1112#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001113 SkBitmap original;
1114 SkImageInfo info = SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1115 if (original.tryAllocPixels(info)) {
1116 original.setIsVolatile(true);
1117 SkBitmap copy;
1118 original.extractSubset(&copy, {5, 10, 15, 20});
1119 SkDebugf("original is " "%s" "volatile\n", original.isVolatile() ? "" : "not ");
1120 SkDebugf("copy is " "%s" "volatile\n", copy.isImmutable() ? "" : "not ");
1121 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001122#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001123original is volatile
Cary Clarkbc5697d2017-10-04 14:31:33 -04001124copy is not volatile
1125##
1126##
1127
1128#SeeAlso setIsVolatile
1129
1130##
1131
1132# ------------------------------------------------------------------------------
1133
1134#Method void setIsVolatile(bool isVolatile)
1135
1136Sets if pixels should be read from Pixel_Ref on every access. Bitmaps are not
1137volatile by default; a GPU back end may upload pixel values expecting them to be
1138accessed repeatedly. Marking temporary Bitmaps as volatile provides a hint to
1139Device that the Bitmap pixels should not be cached. This can
1140improve performance by avoiding overhead and reducing resource
1141consumption on Device.
1142
1143#Param isVolatile true if backing pixels are temporary ##
1144
1145#Example
1146#Height 20
Ben Wagner29380bd2017-10-09 14:43:00 -04001147 SkBitmap bitmap;
1148 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
1149 bitmap.allocPixels();
1150 bitmap.eraseColor(SK_ColorRED);
1151 canvas->scale(16, 16);
1152 canvas->drawBitmap(bitmap, 0, 0);
1153 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorBLUE);
1154 canvas->drawBitmap(bitmap, 2, 0);
1155 bitmap.setIsVolatile(true);
1156 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorGREEN);
1157 canvas->drawBitmap(bitmap, 4, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001158##
1159
1160#SeeAlso isVolatile
1161
1162##
1163
1164# ------------------------------------------------------------------------------
1165
1166#Method void reset()
1167
1168Resets to its initial state; all fields are set to zero, as if Bitmap had
1169been initialized by SkBitmap().
1170
1171Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to
1172kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType.
1173
1174If Pixel_Ref is allocated, its reference count is decreased by one, releasing
1175its memory if Bitmap is the sole owner.
1176
1177#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001178 SkBitmap bitmap;
1179 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
1180 bitmap.allocPixels();
1181 SkDebugf("width:%d height:%d isNull:%s\n", bitmap.width(), bitmap.height(),
1182 bitmap.isNull() ? "true" : "false");
1183 bitmap.reset();
1184 SkDebugf("width:%d height:%d isNull:%s\n", bitmap.width(), bitmap.height(),
1185 bitmap.isNull() ? "true" : "false");
1186#StdOut
1187width:1 height:1 isNull:false
1188width:0 height:0 isNull:true
1189##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001190##
1191
1192#SeeAlso SkBitmap() SkAlphaType SkColorType
1193
1194##
1195
1196# ------------------------------------------------------------------------------
1197
1198#Method static bool ComputeIsOpaque(const SkBitmap& bm)
1199
1200Returns true if all pixels are opaque. Color_Type determines how pixels
1201are encoded, and whether pixel describes Alpha. Returns true for Color_Types
1202without alpha in each pixel; for other Color_Types, returns true if all
1203pixels have alpha values equivalent to 1.0 or greater.
1204
1205For Color_Types kRGB_565_SkColorType or kGray_8_SkColorType: always
1206returns true. For Color_Types kAlpha_8_SkColorType, kBGRA_8888_SkColorType,
1207kRGBA_8888_SkColorType: returns true if all pixel Alpha values are 255.
1208For Color_Type kARGB_4444_SkColorType: returns true if all pixel Alpha values are 15.
1209For kRGBA_F16_SkColorType: returns true if all pixel Alpha values are 1.0 or
1210greater.
1211
1212Returns false for kUnknown_SkColorType.
1213
1214#Param bm Bitmap to check ##
1215
1216#Return true if all pixels have opaque values or Color_Type is opaque ##
1217
1218#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001219 SkBitmap bitmap;
1220 bitmap.setInfo(SkImageInfo::Make(2, 2, kN32_SkColorType, kPremul_SkAlphaType));
1221 for (int index = 0; index < 2; ++index) {
1222 bitmap.allocPixels();
1223 bitmap.eraseColor(0x00000000);
1224 SkDebugf("computeIsOpaque: %s\n", SkBitmap::ComputeIsOpaque(bitmap) ? "true" : "false");
1225 bitmap.eraseColor(0xFFFFFFFF);
1226 SkDebugf("computeIsOpaque: %s\n", SkBitmap::ComputeIsOpaque(bitmap) ? "true" : "false");
1227 bitmap.setInfo(bitmap.info().makeAlphaType(kOpaque_SkAlphaType));
Cary Clarkbc5697d2017-10-04 14:31:33 -04001228 }
1229#StdOut
1230computeIsOpaque: false
1231computeIsOpaque: true
1232computeIsOpaque: false
1233computeIsOpaque: true
1234##
1235##
1236
1237#SeeAlso isOpaque Color_Type Alpha
1238
1239##
1240
1241# ------------------------------------------------------------------------------
1242
1243#Method void getBounds(SkRect* bounds) const
1244
1245Returns Rect { 0, 0, width(), height() }.
1246
1247#Param bounds container for floating point rectangle ##
1248
1249#Example
1250#Height 160
1251#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001252 SkRect bounds;
1253 source.getBounds(&bounds);
1254 bounds.offset(100, 100);
1255 SkPaint paint;
1256 paint.setColor(SK_ColorGRAY);
1257 canvas->scale(.25f, .25f);
1258 canvas->drawRect(bounds, paint);
1259 canvas->drawBitmap(source, 40, 40);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001260##
1261
1262#SeeAlso bounds()
1263
1264##
1265
1266# ------------------------------------------------------------------------------
1267
1268#Method void getBounds(SkIRect* bounds) const
1269
1270Returns IRect { 0, 0, width(), height() }.
1271
1272#Param bounds container for integral rectangle ##
1273
1274#Example
1275#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001276 SkIRect bounds;
1277 source.getBounds(&bounds);
1278 bounds.inset(100, 100);
1279 SkBitmap bitmap;
1280 source.extractSubset(&bitmap, bounds);
1281 canvas->scale(.5f, .5f);
1282 canvas->drawBitmap(bitmap, 10, 10);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001283##
1284
1285#SeeAlso bounds()
1286
1287##
1288
1289# ------------------------------------------------------------------------------
1290
1291#Method SkIRect bounds() const
1292
1293Returns IRect { 0, 0, width(), height() }.
1294
1295#Return integral rectangle from origin to width() and height() ##
1296
1297#Example
1298#Image 4
Ben Wagner29380bd2017-10-09 14:43:00 -04001299 SkIRect bounds = source.bounds();
1300 for (int x : { 0, bounds.width() } ) {
1301 for (int y : { 0, bounds.height() } ) {
1302 canvas->drawBitmap(source, x, y);
1303 }
1304 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001305##
1306
1307#SeeAlso getBounds
1308
1309##
1310
1311# ------------------------------------------------------------------------------
1312
1313#Method SkISize dimensions() const
1314
1315Returns ISize { width(), height() }.
1316
1317#Return integral size of width() and height() ##
1318
1319#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001320 SkBitmap bitmap;
1321 bitmap.setInfo(SkImageInfo::MakeN32(33, 55, kOpaque_SkAlphaType));
1322 SkISize dimensions = bitmap.dimensions();
1323 SkRect bounds;
1324 bitmap.getBounds(&bounds);
1325 SkRect dimensionsAsBounds = SkRect::Make(dimensions);
1326 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -04001327##
1328
1329#SeeAlso height() width()
1330
1331##
1332
1333# ------------------------------------------------------------------------------
1334
1335#Method SkIRect getSubset() const
1336
1337Returns the bounds of this bitmap, offset by its Pixel_Ref origin.
1338
1339#Return bounds within Pixel_Ref bounds ##
1340
1341#Example
1342#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001343 SkIRect bounds;
1344 source.getBounds(&bounds);
1345 bounds.inset(100, 100);
1346 SkBitmap subset;
1347 source.extractSubset(&subset, bounds);
1348 SkIRect r = source.getSubset();
1349 SkDebugf("source: %d, %d, %d, %d\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
1350 r = subset.getSubset();
1351 SkDebugf("subset: %d, %d, %d, %d\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
1352#StdOut
1353source: 0, 0, 512, 512
1354subset: 100, 100, 412, 412
1355##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001356##
1357
1358#SeeAlso extractSubset getBounds
1359
1360##
1361
1362# ------------------------------------------------------------------------------
1363
1364#Method bool setInfo(const SkImageInfo& imageInfo, size_t rowBytes = 0)
1365
1366Sets width, height, Alpha_Type, Color_Type, Color_Space, and optional
1367rowBytes. Frees pixels, and returns true if successful.
1368
1369imageInfo.alphaType may be altered to a value permitted by imageInfo.colorSpace.
1370If imageInfo.colorType is kUnknown_SkColorType, imageInfo.alphaType is
1371set to kUnknown_SkAlphaType.
1372If imageInfo.colorType is kAlpha_8_SkColorType and imageInfo.alphaType is
1373kUnpremul_SkAlphaType, imageInfo.alphaType is replaced by kPremul_SkAlphaType.
1374If imageInfo.colorType is kRGB_565_SkColorType or kGray_8_SkColorType,
1375imageInfo.alphaType is set to kOpaque_SkAlphaType.
1376If imageInfo.colorType is kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
1377kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: imageInfo.alphaType remains
1378unchanged.
1379
1380rowBytes must equal or exceed imageInfo.minRowBytes. If imageInfo.colorSpace is
1381kUnknown_SkColorType, rowBytes is ignored and treated as zero; for all other
1382Color_Space values, rowBytes of zero is treated as imageInfo.minRowBytes.
1383
1384Calls reset() and returns false if:
1385#List
1386# rowBytes exceeds 31 bits ##
1387# imageInfo.width() times imageInfo.bytesPerPixel exceeds 31 bits ##
1388# imageInfo.width() is negative ##
1389# imageInfo.height() is negative ##
1390# rowBytes is positive and less than imageInfo.width() times imageInfo.bytesPerPixel ##
1391##
1392
1393#Param imageInfo contains width, height, Alpha_Type, Color_Type, Color_Space ##
1394#Param rowBytes imageInfo.minRowBytes or larger; or zero ##
1395
1396#Return true if Image_Info set successfully ##
1397
1398#Example
1399#Height 96
1400###^
Ben Wagner29380bd2017-10-09 14:43:00 -04001401SkBitmap bitmap;
1402bitmap.setInfo(SkImageInfo::MakeN32(44, 16, kOpaque_SkAlphaType));
1403bitmap.allocPixels();
1404bitmap.eraseColor(SK_ColorGREEN);
1405SkCanvas offscreen(bitmap);
1406SkPaint paint;
1407offscreen.drawString("!@#$%", 0, 12, paint);
1408canvas->scale(6, 6);
1409canvas->drawBitmap(bitmap, 0, 0);
1410^^^#
Cary Clarkbc5697d2017-10-04 14:31:33 -04001411##
1412
1413#SeeAlso Alpha_Type Color_Type Color_Space height rowBytes width
1414
1415##
1416
1417# ------------------------------------------------------------------------------
1418
1419#Enum AllocFlags
1420
1421#Code
1422 enum AllocFlags {
1423 kZeroPixels_AllocFlag = 1 << 0,
1424 };
1425##
1426
1427AllocFlags provides the option to zero pixel memory when allocated.
1428
1429#Const kZeroPixels_AllocFlag 1
1430 Instructs tryAllocPixelsFlags and allocPixelsFlags to zero pixel memory.
1431##
1432
1433#NoExample
1434##
1435
1436#SeeAlso tryAllocPixelsFlags allocPixelsFlags erase() eraseColor
1437
1438##
1439
1440# ------------------------------------------------------------------------------
1441
1442#Method bool SK_WARN_UNUSED_RESULT tryAllocPixelsFlags(const SkImageInfo& info, uint32_t flags)
1443
1444Sets Image_Info to info following the rules in setInfo and allocates pixel
1445memory. If flags is kZeroPixels_AllocFlag, memory is zeroed.
1446
1447Returns false and calls reset() if Image_Info could not be set, or memory could
1448not be allocated, or memory size exceeds 31 bits, or memory could not optionally
1449be zeroed.
1450
1451On most platforms, allocating pixel memory may succeed even though there is
1452not sufficient memory to hold pixels; allocation does not take place
1453until the pixels are written to. The actual behavior depends on the platform
1454implementation of malloc(), if flags is zero, and calloc(), if flags is
1455kZeroPixels_AllocFlag.
1456
1457Passing kZeroPixels_AllocFlag is usually faster than separately calling
1458eraseColor(SK_ColorTRANSPARENT).
1459
1460#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1461#Param flags kZeroPixels_AllocFlag, or zero ##
1462
1463#Return true if pixels allocation is successful ##
1464
1465#Example
1466#Description
1467For width of 100,000, height of 100,000, and 4 bytes per pixel, Pixel_Ref
1468requires 40,000,000,000 bytes of storage. Allocator implemented
1469internally by SkMallocPixelRef::MakeZeroed limits the maximum memory allowed;
1470the allocation must fit in 31 bits.
1471##
Ben Wagner29380bd2017-10-09 14:43:00 -04001472 SkBitmap bitmap;
1473 int loop = 0;
1474 while (++loop) {
1475 SkDebugf("attempt %d\n", loop);
1476 if (!bitmap.tryAllocPixelsFlags(SkImageInfo::MakeN32(100000, 100000, kOpaque_SkAlphaType),
1477 SkBitmap::kZeroPixels_AllocFlag)) {
1478 SkDebugf("failed!\n");
1479 break;
1480 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001481 }
1482#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001483attempt 1
Cary Clarkbc5697d2017-10-04 14:31:33 -04001484failed!
1485##
1486##
1487
1488#SeeAlso allocPixelsFlags tryAllocPixels SkMallocPixelRef::MakeZeroed
1489
1490##
1491
1492# ------------------------------------------------------------------------------
1493
1494#Method void allocPixelsFlags(const SkImageInfo& info, uint32_t flags)
1495
1496Sets Image_Info to info following the rules in setInfo and allocates pixel
1497memory. If flags is kZeroPixels_AllocFlag, memory is zeroed.
1498
1499Aborts execution if Image_Info could not be set, or memory could
1500not be allocated, or memory size exceeds 31 bits, or memory could not optionally
1501be zeroed. Abort steps may be provided by the user at compile time by defining
1502SK_ABORT.
1503
1504On most platforms, allocating pixel memory may succeed even though there is
1505not sufficient memory to hold pixels; allocation does not take place
1506until the pixels are written to. The actual behavior depends on the platform
1507implementation of malloc(), if flags is zero, and calloc(), if flags is
1508kZeroPixels_AllocFlag.
1509
1510Passing kZeroPixels_AllocFlag is usually faster than separately calling
1511eraseColor(SK_ColorTRANSPARENT).
1512
1513#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1514#Param flags kZeroPixels_AllocFlag, or zero ##
1515
1516#Example
1517#Height 128
1518#Description
1519Text is drawn on a transparent background; drawing the bitmap a second time
1520lets the first draw show through.
1521##
1522###^
Ben Wagner29380bd2017-10-09 14:43:00 -04001523SkBitmap bitmap;
1524bitmap.allocPixelsFlags(SkImageInfo::MakeN32(44, 16, kPremul_SkAlphaType),
1525 SkBitmap::kZeroPixels_AllocFlag);
1526SkCanvas offscreen(bitmap);
1527SkPaint paint;
1528offscreen.drawString("!@#$%", 0, 12, paint);
1529canvas->scale(6, 6);
1530canvas->drawBitmap(bitmap, 0, 0);
1531canvas->drawBitmap(bitmap, 8, 8);
1532^^^#
Cary Clarkbc5697d2017-10-04 14:31:33 -04001533##
1534
1535#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeZeroed
1536
1537##
1538
1539# ------------------------------------------------------------------------------
1540
1541#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info, size_t rowBytes)
1542
1543#ToDo am I ever conflicted about setInfo rules. It needs to be able to be replicated
1544 if, for instance, I generate one-page-per-method HTML-style documentation
1545 I'm not so sure it makes sense to put the indirection in for .h either unless
1546 my mantra is that .h should abbreviate full documentation. And, what to do
1547 for generated markdown? At least there the rules are a click away, although
1548 a pop-down in place would be way better. Hmmm.
1549##
1550
1551Sets Image_Info to info following the rules in setInfo and allocates pixel
1552memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(),
1553or equal zero. Pass in zero for rowBytes to compute the minimum valid value.
1554
1555Returns false and calls reset() if Image_Info could not be set, or memory could
1556not be allocated.
1557
1558On most platforms, allocating pixel memory may succeed even though there is
1559not sufficient memory to hold pixels; allocation does not take place
1560until the pixels are written to. The actual behavior depends on the platform
1561implementation of malloc().
1562
1563#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1564#Param rowBytes size of pixel row or larger; may be zero ##
1565
1566#Return true if pixel storage is allocated ##
1567
1568#Example
1569#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001570SkBitmap bitmap;
1571SkImageInfo info = SkImageInfo::Make(64, 256, kGray_8_SkColorType, kOpaque_SkAlphaType);
1572if (bitmap.tryAllocPixels(info, 0)) {
1573 SkCanvas offscreen(bitmap);
1574 offscreen.scale(.5f, .5f);
1575 for (int x : { 0, 64, 128, 192 } ) {
1576 offscreen.drawBitmap(source, -x, 0);
1577 canvas->drawBitmap(bitmap, x, 0);
1578 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001579}
1580##
1581
1582#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeAllocate
1583
1584##
1585
1586# ------------------------------------------------------------------------------
1587
1588#Method void allocPixels(const SkImageInfo& info, size_t rowBytes)
1589
1590Sets Image_Info to info following the rules in setInfo and allocates pixel
1591memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(),
1592or equal zero. Pass in zero for rowBytes to compute the minimum valid value.
1593
1594Aborts execution if Image_Info could not be set, or memory could
1595not be allocated, or memory size exceeds 31 bits. Abort steps may be provided by
1596the user at compile time by defining SK_ABORT.
1597
1598On most platforms, allocating pixel memory may succeed even though there is
1599not sufficient memory to hold pixels; allocation does not take place
1600until the pixels are written to. The actual behavior depends on the platform
1601implementation of malloc().
1602
1603#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1604#Param rowBytes size of pixel row or larger; may be zero ##
1605
1606#Example
1607#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001608SkBitmap bitmap;
1609SkImageInfo info = SkImageInfo::Make(256, 64, kGray_8_SkColorType, kOpaque_SkAlphaType);
1610bitmap.allocPixels(info, info.width() * info.bytesPerPixel() + 64);
1611SkCanvas offscreen(bitmap);
1612offscreen.scale(.5f, .5f);
1613for (int y : { 0, 64, 128, 192 } ) {
1614 offscreen.drawBitmap(source, 0, -y);
1615 canvas->drawBitmap(bitmap, 0, y);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001616}
1617##
1618
1619#SeeAlso tryAllocPixels allocPixelsFlags SkMallocPixelRef::MakeAllocate
1620
1621##
1622
1623# ------------------------------------------------------------------------------
1624
1625#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info)
1626
1627Sets Image_Info to info following the rules in setInfo and allocates pixel
1628memory.
1629
1630Returns false and calls reset() if Image_Info could not be set, or memory could
1631not be allocated.
1632
1633On most platforms, allocating pixel memory may succeed even though there is
1634not sufficient memory to hold pixels; allocation does not take place
1635until the pixels are written to. The actual behavior depends on the platform
1636implementation of malloc().
1637
1638#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1639
1640#Return true if pixel storage is allocated ##
1641
1642#Example
1643#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001644SkBitmap bitmap;
1645if (bitmap.tryAllocPixels(SkImageInfo::Make(64, 64, kGray_8_SkColorType, kOpaque_SkAlphaType))) {
1646 SkCanvas offscreen(bitmap);
1647 offscreen.scale(.25f, .5f);
1648 for (int y : { 0, 64, 128, 192 } ) {
1649 offscreen.drawBitmap(source, -y, -y);
1650 canvas->drawBitmap(bitmap, y, y);
1651 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001652}
1653##
1654
1655#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeAllocate
1656
1657##
1658
1659# ------------------------------------------------------------------------------
1660
1661#Method void allocPixels(const SkImageInfo& info)
1662
1663Sets Image_Info to info following the rules in setInfo and allocates pixel
1664memory.
1665
1666Aborts execution if Image_Info could not be set, or memory could
1667not be allocated, or memory size exceeds 31 bits. Abort steps may be provided by
1668the user at compile time by defining SK_ABORT.
1669
1670On most platforms, allocating pixel memory may succeed even though there is
1671not sufficient memory to hold pixels; allocation does not take place
1672until the pixels are written to. The actual behavior depends on the platform
1673implementation of malloc().
1674
1675#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1676
1677#Example
1678#Image 4
Ben Wagner29380bd2017-10-09 14:43:00 -04001679SkBitmap bitmap;
1680bitmap.allocPixels(SkImageInfo::Make(64, 64, kGray_8_SkColorType, kOpaque_SkAlphaType));
1681SkCanvas offscreen(bitmap);
1682offscreen.scale(.5f, .5f);
1683for (int y : { 0, 64, 128, 192 } ) {
1684 offscreen.drawBitmap(source, -y, -y);
1685 canvas->drawBitmap(bitmap, y, y);
1686}
Cary Clarkbc5697d2017-10-04 14:31:33 -04001687##
1688
1689#SeeAlso tryAllocPixels allocPixelsFlags SkMallocPixelRef::MakeAllocate
1690
1691##
1692
1693# ------------------------------------------------------------------------------
1694
1695#Method bool SK_WARN_UNUSED_RESULT tryAllocN32Pixels(int width, int height, bool isOpaque = false)
1696
1697Sets Image_Info to width, height, and the native Color_Type; and allocates
1698pixel memory. If isOpaque is true, sets Image_Info to kOpaque_SkAlphaType;
1699otherwise, sets to kPremul_SkAlphaType.
1700
1701Calls reset() and returns false if width exceeds 29 bits or is negative,
1702or height is negative.
1703
1704Returns false if allocation fails.
1705
1706Use to create Bitmap that matches native pixel arrangement on the platform,
1707to draw without converting its pixel format.
1708
1709#Param width pixel column count; must be zero or greater ##
1710#Param height pixel row count; must be zero or greater ##
1711#Param isOpaque true if pixels do not have transparency ##
1712
1713#Return true if pixel storage is allocated ##
1714
1715#Example
1716#Height 160
Ben Wagner29380bd2017-10-09 14:43:00 -04001717 SkBitmap bitmap;
1718 if (bitmap.tryAllocN32Pixels(80, 80)) {
1719 bitmap.eraseColor(SK_ColorTRANSPARENT);
1720 bitmap.erase(0x7f3f7fff, SkIRect::MakeWH(50, 30));
1721 bitmap.erase(0x3f7fff3f, SkIRect::MakeXYWH(20, 10, 50, 30));
1722 bitmap.erase(0x5fff3f7f, SkIRect::MakeXYWH(40, 20, 50, 30));
1723 canvas->drawBitmap(bitmap, 0, 0);
1724 for (int x : { 0, 30, 60, 90 } ) {
1725 canvas->drawBitmap(bitmap, x, 70);
1726 }
1727 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001728##
1729
1730#SeeAlso tryAllocPixels allocN32Pixels SkMallocPixelRef::MakeAllocate
1731
1732##
1733
1734# ------------------------------------------------------------------------------
1735
1736#Method void allocN32Pixels(int width, int height, bool isOpaque = false)
1737
1738Sets Image_Info to width, height, and the native Color_Type; and allocates
1739pixel memory. If isOpaque is true, sets Image_Info to kPremul_SkAlphaType;
1740otherwise, sets to kOpaque_SkAlphaType.
1741
1742Aborts if width exceeds 29 bits or is negative, or height is negative, or
1743allocation fails. Abort steps may be provided by the user at compile time by
1744defining SK_ABORT.
1745
1746Use to create Bitmap that matches native pixel arrangement on the platform,
1747to draw without converting its pixel format.
1748
1749#Param width pixel column count; must be zero or greater ##
1750#Param height pixel row count; must be zero or greater ##
1751#Param isOpaque true if pixels do not have transparency ##
1752
1753#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001754 SkRandom random;
1755 SkBitmap bitmap;
1756 bitmap.allocN32Pixels(64, 64);
1757 bitmap.eraseColor(SK_ColorTRANSPARENT);
1758 for (int y = 0; y < 256; y += 64) {
1759 for (int x = 0; x < 256; x += 64) {
1760 SkColor color = random.nextU();
1761 uint32_t w = random.nextRangeU(4, 32);
1762 uint32_t cx = random.nextRangeU(0, 64 - w);
1763 uint32_t h = random.nextRangeU(4, 32);
1764 uint32_t cy = random.nextRangeU(0, 64 - h);
1765 bitmap.erase(color, SkIRect::MakeXYWH(cx, cy, w, h));
1766 canvas->drawBitmap(bitmap, x, y);
1767 }
1768 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001769##
1770
1771#SeeAlso allocPixels tryAllocN32Pixels SkMallocPixelRef::MakeAllocate
1772
1773##
1774
1775# ------------------------------------------------------------------------------
1776
1777#Method bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
1778 void (*releaseProc)(void* addr, void* context), void* context)
1779
1780Sets Image_Info to info following the rules in setInfo, and creates Pixel_Ref
1781containing pixels and rowBytes. releaseProc, if not nullptr, is called
1782immediately on failure or when pixels are no longer referenced. context may be
1783nullptr.
1784
1785If Image_Info could not be set, or rowBytes is less than info.minRowBytes:
1786calls releaseProc if present, calls reset(), and returns false.
1787
1788Otherwise, if pixels equals nullptr: sets Image_Info, calls releaseProc if
1789present, returns true.
1790
1791If Image_Info is set, pixels is not nullptr, and releaseProc is not nullptr:
1792when pixels are no longer referenced, calls releaseProc with pixels and context
1793as parameters.
1794
1795#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1796#Param pixels address or pixel storage; may be nullptr ##
1797#Param rowBytes size of pixel row or larger ##
1798#Param releaseProc function called when pixels can be deleted; may be nullptr ##
1799#Param context caller state passed to releaseProc; may be nullptr ##
1800
1801#Return true if Image_Info is set to info ##
1802
1803#Example
1804#Description
1805releaseProc is called immediately because rowBytes is too small for Pixel_Ref.
1806##
1807#Function
Ben Wagner29380bd2017-10-09 14:43:00 -04001808static void releaseProc(void* addr, void* ) {
1809 SkDebugf("releaseProc called\n");
1810 delete[] (uint32_t*) addr;
1811}
1812
1813##
1814
1815void draw(SkCanvas* canvas) {
1816 SkBitmap bitmap;
1817 void* pixels = new uint32_t[8 * 8];
1818 SkImageInfo info = SkImageInfo::MakeN32(8, 8, kOpaque_SkAlphaType);
1819 SkDebugf("before installPixels\n");
1820 bool installed = bitmap.installPixels(info, pixels, 16, releaseProc, nullptr);
1821 SkDebugf("install " "%s" "successful\n", installed ? "" : "not ");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001822}
1823#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001824before installPixels
1825releaseProc called
Cary Clarkbc5697d2017-10-04 14:31:33 -04001826install not successful
1827##
1828##
1829
1830#SeeAlso allocPixels
1831
1832##
1833
1834# ------------------------------------------------------------------------------
1835
1836#Method bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes)
1837
1838Sets Image_Info to info following the rules in setInfo, and creates Pixel_Ref
1839containing pixels and rowBytes.
1840
1841If Image_Info could not be set, or rowBytes is less than info.minRowBytes:
1842calls reset(), and returns false.
1843
1844Otherwise, if pixels equals nullptr: sets Image_Info, returns true.
1845
1846Caller must ensure that pixels are valid for the lifetime of Bitmap and Pixel_Ref.
1847
1848#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1849#Param pixels address or pixel storage; may be nullptr ##
1850#Param rowBytes size of pixel row or larger ##
1851
1852#Return true if Image_Info is set to info ##
1853
1854#Example
1855#Description
1856#Bug 7079 ##
1857GPU does not support kUnpremul_SkAlphaType, does not assert that it does not.
1858##
Ben Wagner29380bd2017-10-09 14:43:00 -04001859void draw(SkCanvas* canvas) {
1860 SkRandom random;
1861 SkBitmap bitmap;
1862 const int width = 8;
1863 const int height = 8;
1864 uint32_t pixels[width * height];
1865 for (unsigned x = 0; x < width * height; ++x) {
1866 pixels[x] = random.nextU();
1867 }
1868 SkImageInfo info = SkImageInfo::MakeN32(width, height, kUnpremul_SkAlphaType);
1869 if (bitmap.installPixels(info, pixels, info.minRowBytes())) {
1870 canvas->scale(32, 32);
1871 canvas->drawBitmap(bitmap, 0, 0);
1872 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001873}
1874##
1875
1876#SeeAlso allocPixels
1877
1878##
1879
1880# ------------------------------------------------------------------------------
1881
1882#Method bool installPixels(const SkPixmap& pixmap)
1883
1884Sets Image_Info to pixmap.info() following the rules in setInfo, and creates
1885Pixel_Ref containing pixmap.addr() and pixmap.rowBytes.
1886
1887If Image_Info could not be set, or pixmap.rowBytes is less than
1888SkImageInfo::minRowBytes: calls reset(), and returns false.
1889
1890Otherwise, if pixmap.addr() equals nullptr: sets Image_Info, returns true.
1891
1892Caller must ensure that pixmap is valid for the lifetime of Bitmap and Pixel_Ref.
1893
1894#Param pixmap Image_Info, pixel address, and rowBytes ##
1895
1896#Return true if Image_Info was set to pixmap.info() ##
1897
1898#Example
1899#Description
1900Draw a five by five bitmap, and draw it again with a center white pixel.
1901##
1902#Height 64
1903 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
1904 { 0xAC, 0xA8, 0x89, 0x47, 0x87 },
1905 { 0x4B, 0x25, 0x25, 0x25, 0x46 },
1906 { 0x90, 0x81, 0x25, 0x41, 0x33 },
1907 { 0x75, 0x55, 0x44, 0x20, 0x00 }};
1908 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
1909 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
1910 SkBitmap bitmap;
1911 bitmap.installPixels(pixmap);
1912 canvas->scale(10, 10);
1913 canvas->drawBitmap(bitmap, 0, 0);
1914 *pixmap.writable_addr8(2, 2) = 0xFF;
1915 bitmap.installPixels(pixmap);
1916 canvas->drawBitmap(bitmap, 10, 0);
1917##
1918
1919#SeeAlso allocPixels
1920
1921##
1922
1923# ------------------------------------------------------------------------------
1924
1925#Method bool installMaskPixels(const SkMask& mask)
1926
1927Sets Image_Info to mask width, mask height, kAlpha_8_SkColorType, and
1928kPremul_SkAlphaType. Sets Pixel_Ref to mask image and mask rowBytes.
1929
1930Returns false and calls reset() if mask format is not SkMask::kA8_Format,
1931or if mask width or mask height is negative, or if mask rowBytes is less
1932than mask width.
1933
1934Caller must ensure that mask is valid for the lifetime of Bitmap and Pixel_Ref.
1935
1936#Param mask Alpha 8-bit bitmap ##
1937
1938#Return true if Image_Info and Pixel_Ref refer to mask ##
1939
1940#Example
1941#Description
1942Draw a five by five bitmap, and draw it again with a center white pixel.
1943##
1944#Height 64
Ben Wagner29380bd2017-10-09 14:43:00 -04001945 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
1946 { 0xAC, 0xA8, 0x89, 0x47, 0x87 },
1947 { 0x4B, 0x25, 0x25, 0x25, 0x46 },
1948 { 0x90, 0x81, 0x25, 0x41, 0x33 },
1949 { 0x75, 0x55, 0x44, 0x20, 0x00 }};
1950 SkMask mask;
1951 mask.fImage = storage[0];
1952 mask.fBounds = SkIRect::MakeWH(5, 5);
1953 mask.fRowBytes = 5;
1954 mask.fFormat = SkMask::kA8_Format;
1955 SkBitmap bitmap;
1956 bitmap.installMaskPixels(mask);
1957 canvas->scale(10, 10);
1958 canvas->drawBitmap(bitmap, 0, 0);
1959 storage[2][2] = 0xFF;
1960 bitmap.installMaskPixels(mask);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001961 canvas->drawBitmap(bitmap, 10, 0);
1962##
1963
1964#SeeAlso installPixels allocPixels
1965
1966##
1967
1968# ------------------------------------------------------------------------------
1969
1970#Method void setPixels(void* pixels)
1971
1972Replaces Pixel_Ref with pixels, preserving Image_Info and rowBytes.
1973Sets Pixel_Ref origin to (0, 0).
1974
1975If pixels is nullptr, or if info().colorType equals kUnknown_SkColorType;
1976release reference to Pixel_Ref, and set Pixel_Ref to nullptr.
1977
1978Caller is responsible for handling ownership pixel memory for the lifetime
1979of Bitmap and Pixel_Ref.
1980
1981#Param pixels address of pixel storage, managed by caller ##
1982
1983#Example
1984#Height 50
Ben Wagner29380bd2017-10-09 14:43:00 -04001985 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
1986 uint8_t set2[5] = { 0xAC, 0xA8, 0x89, 0x47, 0x87 };
1987 SkBitmap bitmap;
1988 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
1989 canvas->scale(10, 50);
1990 canvas->drawBitmap(bitmap, 0, 0);
1991 bitmap.setPixels(set2);
1992 canvas->drawBitmap(bitmap, 10, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001993##
1994
1995#SeeAlso installPixels allocPixels
1996
1997##
1998
1999# ------------------------------------------------------------------------------
2000
2001#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels()
2002
2003Allocates pixel memory with HeapAllocator, and replaces existing Pixel_Ref.
2004The allocation size is determined by Image_Info width, height, and Color_Type.
2005
2006Returns false if info().colorType is kUnknown_SkColorType, or allocation exceeds
200731 bits, or allocation fails.
2008
2009#Return true if the allocation succeeds
2010##
2011
2012#Example
2013#Height 50
2014#Description
2015Bitmap hosts and draws gray values in set1. tryAllocPixels replaces Pixel_Ref
2016and erases it to black, but does not alter set1. setPixels replaces black
2017Pixel_Ref with set1.
2018##
Ben Wagner29380bd2017-10-09 14:43:00 -04002019 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
2020 SkBitmap bitmap;
2021 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
2022 canvas->scale(10, 50);
2023 canvas->drawBitmap(bitmap, 0, 0);
2024 if (bitmap.tryAllocPixels()) {
2025 bitmap.eraseColor(SK_ColorBLACK);
2026 canvas->drawBitmap(bitmap, 8, 0);
2027 bitmap.setPixels(set1);
2028 canvas->drawBitmap(bitmap, 16, 0);
2029 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002030##
2031
2032#SeeAlso allocPixels installPixels setPixels
2033
2034##
2035
2036# ------------------------------------------------------------------------------
2037
2038#Method void allocPixels()
2039
2040Allocates pixel memory with HeapAllocator, and replaces existing Pixel_Ref.
2041The allocation size is determined by Image_Info width, height, and Color_Type.
2042
2043Aborts if info().colorType is kUnknown_SkColorType, or allocation exceeds
204431 bits, or allocation fails. Abort steps may be provided by the user at compile
2045time by defining SK_ABORT.
2046
2047#Example
2048#Height 50
2049#Description
2050Bitmap hosts and draws gray values in set1. allocPixels replaces Pixel_Ref
2051and erases it to black, but does not alter set1. setPixels replaces black
2052Pixel_Ref with set2.
2053##
Ben Wagner29380bd2017-10-09 14:43:00 -04002054 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
2055 uint8_t set2[5] = { 0xAC, 0xA8, 0x89, 0x47, 0x87 };
2056 SkBitmap bitmap;
2057 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
2058 canvas->scale(10, 50);
2059 canvas->drawBitmap(bitmap, 0, 0);
2060 bitmap.allocPixels();
2061 bitmap.eraseColor(SK_ColorBLACK);
2062 canvas->drawBitmap(bitmap, 8, 0);
2063 bitmap.setPixels(set2);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002064 canvas->drawBitmap(bitmap, 16, 0);
2065##
2066
2067#SeeAlso tryAllocPixels installPixels setPixels
2068
2069##
2070
2071# ------------------------------------------------------------------------------
2072
2073#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(Allocator* allocator)
2074
2075Allocates pixel memory with allocator, and replaces existing Pixel_Ref.
2076The allocation size is determined by Image_Info width, height, and Color_Type.
2077If allocator is nullptr, use HeapAllocator instead.
2078
2079Returns false if allocator allocPixelRef return false.
2080
2081#Param allocator instance of SkBitmap::Allocator instantiation ##
2082
2083#Return true if custom allocator reports success
2084##
2085
2086#Example
2087#Height 100
2088#Description
2089HeapAllocator limits the maximum size of Bitmap to two gigabytes. Using
2090a custom allocator, this limitation may be relaxed. This example can be
2091modified to allocate an eight gigabyte Bitmap on a 64 bit platform with
2092sufficient memory.
2093##
2094#Function
2095class LargePixelRef : public SkPixelRef {
2096public:
2097 LargePixelRef(const SkImageInfo& info, char* storage, size_t rowBytes)
2098 : SkPixelRef(info.width(), info.height(), storage, rowBytes) {
2099 }
2100
2101 ~LargePixelRef() override {
2102 delete[] (char* ) this->pixels();
2103 }
2104};
2105
2106class LargeAllocator : public SkBitmap::Allocator {
2107public:
2108 bool allocPixelRef(SkBitmap* bitmap) override {
2109 const SkImageInfo& info = bitmap->info();
2110 uint64_t rowBytes = info.minRowBytes64();
2111 uint64_t size = info.height() * rowBytes;
2112 char* addr = new char[size];
2113 if (nullptr == addr) {
2114 return false;
2115 }
2116 sk_sp<SkPixelRef> pr = sk_sp<SkPixelRef>(new LargePixelRef(info, addr, rowBytes));
2117 if (!pr) {
2118 return false;
2119 }
2120 bitmap->setPixelRef(std::move(pr), 0, 0);
2121 return true;
2122 }
2123};
2124
2125##
2126
2127void draw(SkCanvas* canvas) {
2128 LargeAllocator largeAllocator;
2129 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002130 int width = 100; // make this 20000
2131 int height = 100; // and this 100000 to allocate 8 gigs on a 64-bit platform
2132 bitmap.setInfo(SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType));
2133 if (bitmap.tryAllocPixels(&largeAllocator)) {
2134 bitmap.eraseColor(0xff55aa33);
2135 canvas->drawBitmap(bitmap, 0, 0);
2136 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002137}
2138
2139##
2140
2141#SeeAlso allocPixels Allocator Pixel_Ref
2142
2143##
2144
2145# ------------------------------------------------------------------------------
2146
2147#Method void allocPixels(Allocator* allocator)
2148
2149Allocates pixel memory with allocator, and replaces existing Pixel_Ref.
2150The allocation size is determined by Image_Info width, height, and Color_Type.
2151If allocator is nullptr, use HeapAllocator instead.
2152
2153Aborts if allocator allocPixelRef return false. Abort steps may be provided by
2154the user at compile time by defining SK_ABORT.
2155
2156#Param allocator instance of SkBitmap::Allocator instantiation ##
2157
2158#Example
2159#Height 32
2160#Function
2161class TinyAllocator : public SkBitmap::Allocator {
2162public:
2163 bool allocPixelRef(SkBitmap* bitmap) override {
2164 const SkImageInfo& info = bitmap->info();
2165 if (info.height() * info.minRowBytes() > sizeof(storage)) {
2166 return false;
2167 }
2168 sk_sp<SkPixelRef> pr = sk_sp<SkPixelRef>(
2169 new SkPixelRef(info.width(), info.height(), storage, info.minRowBytes()));
2170 bitmap->setPixelRef(std::move(pr), 0, 0);
2171 return true;
2172 }
2173
2174 char storage[16];
2175};
2176
2177##
2178
2179void draw(SkCanvas* canvas) {
2180 TinyAllocator tinyAllocator;
2181 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002182 bitmap.setInfo(SkImageInfo::MakeN32(2, 2, kOpaque_SkAlphaType));
2183 if (bitmap.tryAllocPixels(&tinyAllocator)) {
2184 bitmap.eraseColor(0xff55aa33);
2185 bitmap.erase(0xffaa3355, SkIRect::MakeXYWH(1, 1, 1, 1));
2186 canvas->scale(16, 16);
2187 canvas->drawBitmap(bitmap, 0, 0);
2188 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002189}
2190##
2191
2192#SeeAlso allocPixels Allocator Pixel_Ref
2193
2194##
2195
2196# ------------------------------------------------------------------------------
2197
2198#Method SkPixelRef* pixelRef() const
2199
2200Returns Pixel_Ref, which contains: pixel base address; its dimensions; and
2201rowBytes, the interval from one row to the next. Does not change Pixel_Ref
2202reference count. Pixel_Ref may be shared by multiple bitmaps.
2203If Pixel_Ref has not been set, returns nullptr.
2204
2205#Return Pixel_Ref, or nullptr ##
2206
2207#Example
2208#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002209 SkBitmap subset;
2210 source.extractSubset(&subset, SkIRect::MakeXYWH(32, 64, 128, 256));
2211 SkDebugf("src ref %c= sub ref\n", source.pixelRef() == subset.pixelRef() ? '=' : '!');
2212 SkDebugf("src pixels %c= sub pixels\n", source.getPixels() == subset.getPixels() ? '=' : '!');
2213 SkDebugf("src addr %c= sub addr\n", source.getAddr(32, 64) == subset.getAddr(0, 0) ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -04002214##
2215
2216#SeeAlso getPixels getAddr
2217
2218##
2219
2220# ------------------------------------------------------------------------------
2221
2222#Method SkIPoint pixelRefOrigin() const
2223
2224Returns origin of pixels within Pixel_Ref. Bitmap bounds is always contained
2225by Pixel_Ref bounds, which may be the same size or larger. Multiple Bitmaps
2226can share the same Pixel_Ref, where each Bitmap has different bounds.
2227
2228The returned origin added to Bitmap dimensions equals or is smaller than the
2229Pixel_Ref dimensions.
2230
2231Returns (0, 0) if Pixel_Ref is nullptr.
2232
2233#Return pixel origin within Pixel_Ref ##
2234
2235#Example
2236#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002237 SkBitmap subset;
2238 source.extractSubset(&subset, SkIRect::MakeXYWH(32, 64, 128, 256));
2239 SkIPoint sourceOrigin = source.pixelRefOrigin();
2240 SkIPoint subsetOrigin = subset.pixelRefOrigin();
2241 SkDebugf("source origin: %d, %d\n", sourceOrigin.fX, sourceOrigin.fY);
2242 SkDebugf("subset origin: %d, %d\n", subsetOrigin.fX, subsetOrigin.fY);
2243#StdOut
2244source origin: 0, 0
2245subset origin: 32, 64
2246##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002247##
2248
2249#ToDo incomplete ##
2250
2251##
2252
2253# ------------------------------------------------------------------------------
2254
2255#Method void setPixelRef(sk_sp<SkPixelRef> pixelRef, int dx, int dy)
2256
2257Replaces pixelRef and origin in Bitmap. dx and dy specify the offset
2258within the Pixel_Ref pixels for the top-left corner of the bitmap.
2259
2260Asserts in debug builds if dx or dy are out of range. Pins dx and dy
2261to legal range in release builds.
2262
2263The caller is responsible for ensuring that the pixels match the
2264Color_Type and Alpha_Type in Image_Info.
2265
2266#Param pixelRef Pixel_Ref describing pixel address and rowBytes ##
2267#Param dx column offset in Pixel_Ref for bitmap origin ##
2268#Param dy row offset in Pixel_Ref for bitmap origin ##
2269
2270#Example
2271#Height 140
2272#Image 5
2273#Description
2274Treating 32 bit data as 8 bit data is unlikely to produce useful results.
2275##
Ben Wagner29380bd2017-10-09 14:43:00 -04002276 SkBitmap bitmap;
2277 bitmap.setInfo(SkImageInfo::Make(source.width() - 5, source.height() - 5,
2278 kGray_8_SkColorType, kOpaque_SkAlphaType), source.rowBytes());
2279 bitmap.setPixelRef(sk_ref_sp(source.pixelRef()), 5, 5);
2280 canvas->drawBitmap(bitmap, 10, 10);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002281##
2282
2283#SeeAlso setInfo
2284
2285##
2286
2287# ------------------------------------------------------------------------------
2288
2289#Method bool readyToDraw() const
2290
2291Returns true if Bitmap is can be drawn.
2292
2293#Return true if getPixels() is not nullptr ##
2294
2295#Example
2296#Image 5
2297#Height 160
Ben Wagner29380bd2017-10-09 14:43:00 -04002298 if (source.readyToDraw()) {
2299 canvas->drawBitmap(source, 10, 10);
2300 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002301##
2302
2303#SeeAlso getPixels drawsNothing
2304
2305##
2306
2307# ------------------------------------------------------------------------------
2308
2309#Method uint32_t getGenerationID() const
2310
2311Returns a unique value corresponding to the pixels in Pixel_Ref.
2312Returns a different value after notifyPixelsChanged has been called.
2313Returns zero if Pixel_Ref is nullptr.
2314
2315Determines if pixels have changed since last examined.
2316
2317#Return unique value for pixels in Pixel_Ref ##
2318
2319#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04002320 SkBitmap bitmap;
2321 SkDebugf("empty id %u\n", bitmap.getGenerationID());
2322 bitmap.allocPixels(SkImageInfo::MakeN32(64, 64, kOpaque_SkAlphaType));
2323 SkDebugf("alloc id %u\n", bitmap.getGenerationID());
2324 bitmap.eraseColor(SK_ColorRED);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002325 SkDebugf("erase id %u\n", bitmap.getGenerationID());
2326#StdOut
2327#Volatile
Ben Wagner29380bd2017-10-09 14:43:00 -04002328empty id 0
2329alloc id 4
Cary Clarkbc5697d2017-10-04 14:31:33 -04002330erase id 6
2331##
2332##
2333
2334#SeeAlso notifyPixelsChanged Pixel_Ref
2335
2336##
2337
2338# ------------------------------------------------------------------------------
2339
2340#Method void notifyPixelsChanged() const
2341
2342Marks that pixels in Pixel_Ref have changed. Subsequent calls to
2343getGenerationID() return a different value.
2344
2345#Example
2346#Height 20
Ben Wagner29380bd2017-10-09 14:43:00 -04002347 SkBitmap bitmap;
2348 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
2349 bitmap.allocPixels();
2350 bitmap.eraseColor(SK_ColorRED);
2351 canvas->scale(16, 16);
2352 canvas->drawBitmap(bitmap, 0, 0);
2353 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorBLUE);
2354 canvas->drawBitmap(bitmap, 2, 0);
2355 bitmap.notifyPixelsChanged();
2356 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorGREEN);
2357 canvas->drawBitmap(bitmap, 4, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002358##
2359
2360#SeeAlso getGenerationID isVolatile Pixel_Ref
2361
2362##
2363
2364# ------------------------------------------------------------------------------
2365
2366#Method void eraseColor(SkColor c) const
2367
2368Replaces pixel values with c. All pixels contained by bounds() are affected.
2369If the colorType is kGray_8_SkColorType or k565_SkColorType, then Color_Alpha
2370is ignored; Color_RGB is treated as opaque. If colorType is kAlpha_8_SkColorType,
2371then Color_RGB is ignored.
2372
2373#Param c Unpremultiplied Color ##
2374
2375#Example
2376#Height 20
Ben Wagner29380bd2017-10-09 14:43:00 -04002377 SkBitmap bitmap;
2378 bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kOpaque_SkAlphaType));
2379 bitmap.eraseColor(SK_ColorRED);
2380 canvas->scale(16, 16);
2381 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002382##
2383
Cary Clark154beea2017-10-26 07:58:48 -04002384#SeeAlso eraseARGB erase
Cary Clarkbc5697d2017-10-04 14:31:33 -04002385
2386##
2387
2388# ------------------------------------------------------------------------------
2389
2390#Method void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const
2391
2392Replaces pixel values with Unpremultiplied Color built from a, r, g, and b.
2393All pixels contained by bounds() are affected.
2394If the colorType is kGray_8_SkColorType or k565_SkColorType, then a
2395is ignored; r, g, and b are treated as opaque. If colorType is kAlpha_8_SkColorType,
2396then r, g, and b are ignored.
2397
2398#Param a amount of Color_Alpha, from fully transparent (0) to fully opaque (255) ##
2399#Param r amount of Color_RGB_Red, from no red (0) to full red (255) ##
2400#Param g amount of Color_RGB_Green, from no green (0) to full green (255) ##
2401#Param b amount of Color_RGB_Blue, from no blue (0) to full blue (255) ##
2402
2403#Example
2404#Height 80
Ben Wagner29380bd2017-10-09 14:43:00 -04002405 SkBitmap bitmap;
2406 bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType));
2407 bitmap.eraseARGB(0x7f, 0xff, 0x7f, 0x3f);
2408 canvas->scale(50, 50);
2409 canvas->drawBitmap(bitmap, 0, 0);
2410 canvas->drawBitmap(bitmap, .5f, .5f);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002411##
2412
Cary Clark154beea2017-10-26 07:58:48 -04002413#SeeAlso eraseColor erase
Cary Clarkbc5697d2017-10-04 14:31:33 -04002414
2415##
2416
2417# ------------------------------------------------------------------------------
2418
2419#Method void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const
2420
Cary Clark154beea2017-10-26 07:58:48 -04002421Deprecated. Use eraseARGB or eraseColor.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002422
Cary Clark154beea2017-10-26 07:58:48 -04002423#Param r amount of red ##
2424#Param g amount of green ##
2425#Param b amount of blue ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002426
Cary Clark154beea2017-10-26 07:58:48 -04002427#NoExample
Cary Clarkbc5697d2017-10-04 14:31:33 -04002428##
2429
2430#SeeAlso eraseColor eraseARGB erase
2431
2432##
2433
2434# ------------------------------------------------------------------------------
2435
2436#Method void erase(SkColor c, const SkIRect& area) const
2437
2438Replaces pixel values inside area with c. If area does not intersect bounds(),
2439call has no effect.
2440
2441If the colorType is kGray_8_SkColorType or k565_SkColorType, then Color_Alpha
2442is ignored; Color_RGB is treated as opaque. If colorType is kAlpha_8_SkColorType,
2443then Color_RGB is ignored.
2444
2445#Param c Unpremultiplied Color ##
2446#Param area rectangle to fill ##
2447
2448#Example
2449#Height 70
Ben Wagner29380bd2017-10-09 14:43:00 -04002450 SkBitmap bitmap;
2451 bitmap.allocPixels(SkImageInfo::MakeN32(2, 2, kPremul_SkAlphaType));
2452 bitmap.erase(0x7fff7f3f, SkIRect::MakeWH(1, 1));
2453 bitmap.erase(0x7f7f3fff, SkIRect::MakeXYWH(0, 1, 1, 1));
2454 bitmap.erase(0x7f3fff7f, SkIRect::MakeXYWH(1, 0, 1, 1));
2455 bitmap.erase(0x7f1fbf5f, SkIRect::MakeXYWH(1, 1, 1, 1));
2456 canvas->scale(25, 25);
2457 canvas->drawBitmap(bitmap, 0, 0);
2458 canvas->drawBitmap(bitmap, .5f, .5f);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002459
2460##
2461
2462#SeeAlso eraseColor eraseARGB eraseRGB SkCanvas::drawRect
2463
2464##
2465
2466# ------------------------------------------------------------------------------
2467
2468#Method void eraseArea(const SkIRect& area, SkColor c) const
2469
2470Legacy call to be deprecated.
2471
2472#Deprecated
2473##
2474
2475##
2476
2477# ------------------------------------------------------------------------------
2478
2479#Method SkColor getColor(int x, int y) const
2480
2481Returns pixel at (x, y) as Unpremultiplied Color.
2482Returns black with Alpha if Color_Type is kAlpha_8_SkColorType.
2483
2484Input is not validated: out of bounds values of x or y trigger an assert() if
2485built with SK_DEBUG defined; and returns undefined values or may crash if
2486SK_RELEASE is defined. Fails if Color_Type is kUnknown_SkColorType or
2487pixel address is nullptr.
2488
2489Color_Space in Image_Info is ignored. Some Color precision may be lost in the
2490conversion to Unpremultiplied Color; original pixel data may have additional
2491precision.
2492
2493#Param x column index, zero or greater, and less than width() ##
2494#Param y row index, zero or greater, and less than height() ##
2495
2496#Return pixel converted to Unpremultiplied Color ##
2497
2498#Example
2499 const int w = 4;
2500 const int h = 4;
2501 SkColor colors[][w] = {
2502 0x00000000, 0x2a0e002a, 0x55380055, 0x7f7f007f,
2503 0x2a000e2a, 0x551c1c55, 0x7f542a7f, 0xaaaa38aa,
2504 0x55003855, 0x7f2a547f, 0xaa7171aa, 0xd4d48dd4,
2505 0x7f007f7f, 0xaa38aaaa, 0xd48dd4d4, 0xffffffff,
2506 };
2507 SkDebugf("Premultiplied:\n");
2508 for (int y = 0; y < h; ++y) {
2509 SkDebugf("(0, %d) ", y);
2510 for (int x = 0; x < w; ++x) {
2511 SkDebugf("0x%08x%c", colors[y][x], x == w - 1 ? '\n' : ' ');
2512 }
2513 }
2514 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), colors, w * 4);
2515 SkBitmap bitmap;
2516 bitmap.installPixels(pixmap);
2517 SkDebugf("Unpremultiplied:\n");
2518 for (int y = 0; y < h; ++y) {
2519 SkDebugf("(0, %d) ", y);
2520 for (int x = 0; x < w; ++x) {
2521 SkDebugf("0x%08x%c", bitmap.getColor(x, y), x == w - 1 ? '\n' : ' ');
2522 }
2523 }
2524#StdOut
2525Premultiplied:
2526(0, 0) 0x00000000 0x2a0e002a 0x55380055 0x7f7f007f
2527(0, 1) 0x2a000e2a 0x551c1c55 0x7f542a7f 0xaaaa38aa
2528(0, 2) 0x55003855 0x7f2a547f 0xaa7171aa 0xd4d48dd4
2529(0, 3) 0x7f007f7f 0xaa38aaaa 0xd48dd4d4 0xffffffff
2530Unpremultiplied:
2531(0, 0) 0x00000000 0x2a5500ff 0x55a800ff 0x7fff00ff
2532(0, 1) 0x2a0055ff 0x555454ff 0x7fa954ff 0xaaff54ff
2533(0, 2) 0x5500a8ff 0x7f54a9ff 0xaaaaaaff 0xd4ffaaff
2534(0, 3) 0x7f00ffff 0xaa54ffff 0xd4aaffff 0xffffffff
2535##
2536##
2537
2538#SeeAlso getAddr readPixels
2539
2540##
2541
2542# ------------------------------------------------------------------------------
2543
2544#Method void* getAddr(int x, int y) const
2545
2546Returns pixel address at (x, y).
2547
2548Input is not validated: out of bounds values of x or y, or kUnknown_SkColorType,
2549trigger an assert() if built with SK_DEBUG defined. Returns nullptr if
2550Color_Type is kUnknown_SkColorType, or Pixel_Ref is nullptr.
2551
2552Performs a lookup of pixel size; for better performance, call
2553one of: getAddr8, getAddr16, or getAddr32.
2554
2555#Param x column index, zero or greater, and less than width() ##
2556#Param y row index, zero or greater, and less than height() ##
2557
2558#Return generic pointer to pixel ##
2559
2560#Example
2561#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002562 char* row0 = (char* ) source.getAddr(0, 0);
2563 char* row1 = (char* ) source.getAddr(0, 1);
2564 SkDebugf("addr interval %c= rowBytes\n", row1 - row0 == source.rowBytes() ? '=' : '!');
2565#StdOut
2566addr interval == rowBytes
2567##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002568##
2569
2570#SeeAlso getAddr8 getAddr16 getAddr32 readPixels SkPixmap::addr
2571
2572##
2573
2574# ------------------------------------------------------------------------------
2575
2576#Method inline uint32_t* getAddr32(int x, int y) const
2577
2578Returns address at (x, y).
2579
2580Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2581#List
2582# Pixel_Ref is nullptr ##
2583# bytesPerPixel() is not four ##
2584# x is negative, or not less than width() ##
2585# y is negative, or not less than height() ##
2586##
2587
2588#Param x column index, zero or greater, and less than width() ##
2589#Param y row index, zero or greater, and less than height() ##
2590
2591#Return unsigned 32-bit pointer to pixel at (x, y) ##
2592
2593#Example
2594#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002595 uint32_t* row0 = source.getAddr32(0, 0);
2596 uint32_t* row1 = source.getAddr32(0, 1);
2597 size_t interval = (row1 - row0) * source.bytesPerPixel();
2598 SkDebugf("addr interval %c= rowBytes\n", interval == source.rowBytes() ? '=' : '!');
2599#StdOut
2600addr interval == rowBytes
2601##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002602##
2603
2604#SeeAlso getAddr8 getAddr16 getAddr readPixels SkPixmap::addr32
2605
2606##
2607
2608# ------------------------------------------------------------------------------
2609
2610#Method inline uint16_t* getAddr16(int x, int y) const
2611
2612Returns address at (x, y).
2613
2614Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2615#List
2616# Pixel_Ref is nullptr ##
2617# bytesPerPixel() is not two ##
2618# x is negative, or not less than width() ##
2619# y is negative, or not less than height() ##
2620##
2621
2622#Param x column index, zero or greater, and less than width() ##
2623#Param y row index, zero or greater, and less than height() ##
2624
2625#Return unsigned 16-bit pointer to pixel at (x, y)##
2626
2627#Example
2628#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002629 SkBitmap bitmap16;
2630 SkImageInfo dstInfo = SkImageInfo::Make(source.width(), source.height(), kARGB_4444_SkColorType,
2631 kPremul_SkAlphaType);
2632 bitmap16.allocPixels(dstInfo);
2633 if (source.readPixels(dstInfo, bitmap16.getPixels(), bitmap16.rowBytes(), 0, 0)) {
2634 uint16_t* row0 = bitmap16.getAddr16(0, 0);
2635 uint16_t* row1 = bitmap16.getAddr16(0, 1);
2636 size_t interval = (row1 - row0) * bitmap16.bytesPerPixel();
2637 SkDebugf("addr interval %c= rowBytes\n", interval == bitmap16.rowBytes() ? '=' : '!');
2638 }
2639#StdOut
2640addr interval == rowBytes
2641##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002642##
2643
2644#SeeAlso getAddr8 getAddr getAddr32 readPixels SkPixmap::addr16
2645
2646##
2647
2648# ------------------------------------------------------------------------------
2649
2650#Method inline uint8_t* getAddr8(int x, int y) const
2651
2652Returns address at (x, y).
2653
2654Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2655#List
2656# Pixel_Ref is nullptr ##
2657# bytesPerPixel() is not one ##
2658# x is negative, or not less than width() ##
2659# y is negative, or not less than height() ##
2660##
2661
2662#Param x column index, zero or greater, and less than width() ##
2663#Param y row index, zero or greater, and less than height() ##
2664
2665#Return unsigned 8-bit pointer to pixel at (x, y) ##
2666
2667#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04002668 SkBitmap bitmap;
2669 const int width = 8;
2670 const int height = 8;
2671 uint8_t pixels[height][width];
2672 SkImageInfo info = SkImageInfo::Make(width, height, kGray_8_SkColorType, kOpaque_SkAlphaType);
2673 if (bitmap.installPixels(info, pixels, info.minRowBytes())) {
2674 SkDebugf("&pixels[4][2] %c= bitmap.getAddr8(2, 4)\n",
2675 &pixels[4][2] == bitmap.getAddr8(2, 4) ? '=' : '!');
2676 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002677#StdOut
2678&pixels[4][2] == bitmap.getAddr8(2, 4)
2679##
2680##
2681
2682#SeeAlso getAddr getAddr16 getAddr32 readPixels SkPixmap::addr8
2683
2684##
2685
2686# ------------------------------------------------------------------------------
2687
2688#Method bool extractSubset(SkBitmap* dst, const SkIRect& subset) const
2689
2690Shares Pixel_Ref with dst. Pixels are not copied; Bitmap and dst point
2691to the same pixels; dst bounds() are set to the intersection of subset
2692and the original bounds().
2693
2694subset may be larger than bounds(). Any area outside of bounds() is ignored.
2695
2696Any contents of dst are discarded. isVolatile setting is copied to dst.
2697dst is set to colorType, alphaType, and colorSpace.
2698
2699Return false if:
2700#List
2701# dst is nullptr ##
2702# Pixel_Ref is nullptr ##
2703# subset does not intersect bounds() ##
2704##
2705
2706
2707#Param dst Bitmap set to subset ##
2708#Param subset rectangle of pixels to reference ##
2709
2710#Return true if dst is replaced by subset
2711##
2712
2713#Example
2714#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002715 SkIRect bounds, s;
2716 source.getBounds(&bounds);
2717 SkDebugf("bounds: %d, %d, %d, %d\n", bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
2718 SkBitmap subset;
2719 for (int left: { -100, 0, 100, 1000 } ) {
2720 for (int right: { 0, 100, 1000 } ) {
2721 SkIRect b = SkIRect::MakeLTRB(left, 100, right, 200);
2722 bool success = source.extractSubset(&subset, b);
2723 SkDebugf("subset: %4d, %4d, %4d, %4d ", b.fLeft, b.fTop, b.fRight, b.fBottom);
2724 SkDebugf("success; %s", success ? "true" : "false");
2725 if (success) {
2726 subset.getBounds(&s);
2727 SkDebugf(" subset: %d, %d, %d, %d", s.fLeft, s.fTop, s.fRight, s.fBottom);
2728 }
2729 SkDebugf("\n");
2730 }
2731 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002732#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04002733bounds: 0, 0, 512, 512
2734subset: -100, 100, 0, 200 success; false
2735subset: -100, 100, 100, 200 success; true subset: 0, 0, 100, 100
2736subset: -100, 100, 1000, 200 success; true subset: 0, 0, 512, 100
2737subset: 0, 100, 0, 200 success; false
2738subset: 0, 100, 100, 200 success; true subset: 0, 0, 100, 100
2739subset: 0, 100, 1000, 200 success; true subset: 0, 0, 512, 100
2740subset: 100, 100, 0, 200 success; false
2741subset: 100, 100, 100, 200 success; false
2742subset: 100, 100, 1000, 200 success; true subset: 0, 0, 412, 100
2743subset: 1000, 100, 0, 200 success; false
2744subset: 1000, 100, 100, 200 success; false
Cary Clarkbc5697d2017-10-04 14:31:33 -04002745subset: 1000, 100, 1000, 200 success; false
2746##
2747##
2748
2749#SeeAlso readPixels writePixels SkCanvas::drawBitmap
2750
2751##
2752
2753# ------------------------------------------------------------------------------
2754
2755#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
2756 int srcX, int srcY, SkTransferFunctionBehavior behavior) const
2757
2758Copies a Rect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not exceed
2759(this->width(), this->height()).
2760
2761dstInfo specifies width, height, Color_Type, Alpha_Type, and
2762Color_Space of destination. dstRowBytes specifics the gap from one destination
2763row to the next. Returns true if pixels are copied. Returns false if:
2764#List
2765# dstInfo.addr() equals nullptr ##
2766# dstRowBytes is less than dstInfo.minRowBytes ##
2767# Pixel_Ref is nullptr ##
2768##
2769
2770Pixels are copied only if pixel conversion is possible. If this->colorType is
2771kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
2772If this->colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
2773If this->alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
2774match. If this->colorSpace is nullptr, dstInfo.colorSpace must match. Returns
2775false if pixel conversion is not possible.
2776
2777srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04002778false if width() or height() is zero or negative.
2779Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04002780#Formula
2781abs(srcX) >= this->width()
2782##
2783, or if
2784#Formula
2785abs(srcY) >= this->height()
2786##
2787.
2788
2789If behavior is SkTransferFunctionBehavior::kRespect: converts source
2790pixels to a linear space before converting to dstInfo.
2791If behavior is SkTransferFunctionBehavior::kIgnore: source
2792pixels are treated as if they are linear, regardless of how they are encoded.
2793
2794#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
2795#Param dstPixels destination pixel storage ##
2796#Param dstRowBytes destination row length ##
2797#Param srcX column index whose absolute value is less than width() ##
2798#Param srcY row index whose absolute value is less than height() ##
2799#Param behavior one of: SkTransferFunctionBehavior::kRespect,
2800 SkTransferFunctionBehavior::kIgnore
2801##
2802
2803#Return true if pixels are copied to dstPixels ##
2804
2805#Example
2806#Height 64
2807void draw(SkCanvas* canvas) {
Ben Wagner29380bd2017-10-09 14:43:00 -04002808 const int width = 256;
2809 const int height = 32;
2810 std::vector<int32_t> dstPixels;
2811 dstPixels.resize(height * width * 4);
2812 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
2813 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
2814 SkPoint gradPoints[] = { { 0, 0 }, { width, 0 } };
2815 SkPaint gradPaint;
2816 gradPaint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
2817 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
2818 for (auto behavior : { SkTransferFunctionBehavior::kRespect,
2819 SkTransferFunctionBehavior::kIgnore} ) {
2820 SkBitmap bitmap;
2821 bitmap.allocPixels(info);
2822 SkCanvas srcCanvas(bitmap);
2823 srcCanvas.drawRect(SkRect::MakeWH(width, height), gradPaint);
2824 if (bitmap.readPixels(info, &dstPixels.front(), width * 4, 0, 0, behavior)) {
2825 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
2826 bitmap.installPixels(dstPixmap);
2827 canvas->drawBitmap(bitmap, 0, 0);
2828 }
2829 canvas->translate(0, height);
2830 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002831}
2832##
2833
2834#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2835
2836##
2837
2838# ------------------------------------------------------------------------------
2839
2840#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
2841 int srcX, int srcY) const
2842
2843Copies a Rect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not exceed
2844(this->width(), this->height()).
2845
2846dstInfo specifies width, height, Color_Type, Alpha_Type, and
2847Color_Space of destination. dstRowBytes specifics the gap from one destination
2848row to the next. Returns true if pixels are copied. Returns false if:
2849#List
2850# dstInfo.addr() equals nullptr ##
2851# dstRowBytes is less than dstInfo.minRowBytes ##
2852# Pixel_Ref is nullptr ##
2853##
2854
2855Pixels are copied only if pixel conversion is possible. If this->colorType is
2856kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
2857If this->colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
2858If this->alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
2859match. If this->colorSpace is nullptr, dstInfo.colorSpace must match. Returns
2860false if pixel conversion is not possible.
2861
2862srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04002863false if width() or height() is zero or negative.
2864Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04002865#Formula
2866abs(srcX) >= this->width()
2867##
2868, or if
2869#Formula
2870abs(srcY) >= this->height()
2871##
2872.
2873
2874#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
2875#Param dstPixels destination pixel storage ##
2876#Param dstRowBytes destination row length ##
2877#Param srcX column index whose absolute value is less than width() ##
2878#Param srcY row index whose absolute value is less than height() ##
2879
2880#Return true if pixels are copied to dstPixels ##
2881
2882#Example
2883#Height 128
2884#Description
2885Transferring the gradient from 8 bits per component to 4 bits per component
2886creates visible banding.
2887##
Ben Wagner29380bd2017-10-09 14:43:00 -04002888 const int width = 256;
2889 const int height = 64;
2890 SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(width, height);
2891 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
2892 SkPoint gradPoints[] = { { 0, 0 }, { 256, 0 } };
2893 SkPaint paint;
2894 paint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
2895 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
2896 SkBitmap bitmap;
2897 bitmap.allocPixels(srcInfo);
2898 SkCanvas srcCanvas(bitmap);
2899 srcCanvas.drawRect(SkRect::MakeWH(width, height), paint);
2900 canvas->drawBitmap(bitmap, 0, 0);
2901 SkImageInfo dstInfo = srcInfo.makeColorType(kARGB_4444_SkColorType);
2902 std::vector<int16_t> dstPixels;
2903 dstPixels.resize(height * width);
2904 bitmap.readPixels(dstInfo, &dstPixels.front(), width * 2, 0, 0);
2905 SkPixmap dstPixmap(dstInfo, &dstPixels.front(), width * 2);
2906 bitmap.installPixels(dstPixmap);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002907 canvas->drawBitmap(bitmap, 0, 64);
2908##
2909
2910#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2911
2912##
2913
2914# ------------------------------------------------------------------------------
2915
2916#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY) const
2917
2918Copies a Rect of pixels to dst. Copy starts at (srcX, srcY), and does not exceed
2919(this->width(), this->height()).
2920
2921dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2922and row bytes of destination. dst.rowBytes specifics the gap from one destination
2923row to the next. Returns true if pixels are copied. Returns false if:
2924#List
2925# dst pixel storage equals nullptr ##
2926# dst.rowBytes is less than SkImageInfo::minRowBytes ##
2927# Pixel_Ref is nullptr ##
2928##
2929
2930Pixels are copied only if pixel conversion is possible. If this->colorType is
2931kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
2932If this->colorType is kGray_8_SkColorType, dst Color_Space must match.
2933If this->alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
2934match. If this->colorSpace is nullptr, dst Color_Space must match. Returns
2935false if pixel conversion is not possible.
2936
2937srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04002938false if width() or height() is zero or negative.
2939Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04002940#Formula
2941abs(srcX) >= this->width()
2942##
2943, or if
2944#Formula
2945abs(srcY) >= this->height()
2946##
2947.
2948
2949#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
2950#Param srcX column index whose absolute value is less than width() ##
2951#Param srcY row index whose absolute value is less than height() ##
2952
2953#Return true if pixels are copied to dst ##
2954
2955#Example
2956#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002957 std::vector<int32_t> srcPixels;
2958 srcPixels.resize(source.height() * source.rowBytes());
2959 for (int y = 0; y < 4; ++y) {
2960 for (int x = 0; x < 4; ++x) {
2961 SkPixmap pixmap(SkImageInfo::MakeN32Premul(source.width() / 4, source.height() / 4),
2962 &srcPixels.front() + x * source.height() * source.width() / 4 +
2963 y * source.width() / 4, source.rowBytes());
2964 source.readPixels(pixmap, x * source.width() / 4, y * source.height() / 4);
2965 }
2966 }
2967 canvas->scale(.5f, .5f);
2968 SkBitmap bitmap;
2969 bitmap.installPixels(SkImageInfo::MakeN32Premul(source.width(), source.height()),
2970 &srcPixels.front(), source.rowBytes());
Cary Clarkbc5697d2017-10-04 14:31:33 -04002971 canvas->drawBitmap(bitmap, 0, 0);
2972##
2973
2974#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2975
2976##
2977
2978# ------------------------------------------------------------------------------
2979
2980#Method bool readPixels(const SkPixmap& dst) const
2981
2982Copies a Rect of pixels to dst. Copy starts at (0, 0), and does not exceed
2983(this->width(), this->height()).
2984
2985dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2986and row bytes of destination. dst.rowBytes specifics the gap from one destination
2987row to the next. Returns true if pixels are copied. Returns false if:
2988#List
2989# dst pixel storage equals nullptr ##
2990# dst.rowBytes is less than SkImageInfo::minRowBytes ##
2991# Pixel_Ref is nullptr ##
2992##
2993
2994Pixels are copied only if pixel conversion is possible. If this->colorType is
2995kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
2996If this->colorType is kGray_8_SkColorType, dst Color_Space must match.
2997If this->alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
2998match. If this->colorSpace is nullptr, dst Color_Space must match. Returns
2999false if pixel conversion is not possible.
3000
3001#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
3002
3003#Return true if pixels are copied to dst ##
3004
3005#Example
3006#Height 128
3007#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04003008 std::vector<int32_t> srcPixels;
3009 srcPixels.resize(source.height() * source.width() * 8);
3010 for (int i = 0; i < 2; ++i) {
3011 SkPixmap pixmap(SkImageInfo::Make(source.width() * 2, source.height(),
3012 i ? kRGBA_8888_SkColorType : kBGRA_8888_SkColorType, kPremul_SkAlphaType),
3013 &srcPixels.front() + i * source.width(), source.rowBytes() * 2);
3014 source.readPixels(pixmap);
3015 }
3016 canvas->scale(.25f, .25f);
3017 SkBitmap bitmap;
3018 bitmap.installPixels(SkImageInfo::MakeN32Premul(source.width() * 2, source.height()),
3019 &srcPixels.front(), source.rowBytes() * 2);
3020 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003021##
3022
3023#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
3024
3025##
3026
3027# ------------------------------------------------------------------------------
3028
3029#Method bool writePixels(const SkPixmap& src, int dstX, int dstY)
3030
3031Copies a Rect of pixels from src. Copy starts at (dstX, dstY), and does not exceed
3032(src.width(), src.height()).
3033
3034src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
3035and row bytes of source. src.rowBytes specifics the gap from one source
3036row to the next. Returns true if pixels are copied. Returns false if:
3037#List
3038# src pixel storage equals nullptr ##
3039# src.rowBytes is less than SkImageInfo::minRowBytes ##
3040# Pixel_Ref is nullptr ##
3041##
3042
3043Pixels are copied only if pixel conversion is possible. If this->colorType is
3044kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
3045If this->colorType is kGray_8_SkColorType, src Color_Space must match.
3046If this->alphaType is kOpaque_SkAlphaType, src Alpha_Type must
3047match. If this->colorSpace is nullptr, src Color_Space must match. Returns
3048false if pixel conversion is not possible.
3049
3050dstX and dstY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04003051false if width() or height() is zero or negative.
3052Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04003053#Formula
3054abs(dstX) >= this->width()
3055##
3056, or if
3057#Formula
3058abs(dstY) >= this->height()
3059##
3060.
3061
3062#Param src source Pixmap: Image_Info, pixels, row bytes ##
3063#Param dstX column index whose absolute value is less than width() ##
3064#Param dstY row index whose absolute value is less than height() ##
3065
3066#Return true if src pixels are copied to Bitmap ##
3067
3068#Example
3069#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04003070 std::vector<int32_t> srcPixels;
3071 int width = image->width();
3072 int height = image->height();
3073 srcPixels.resize(height * width * 4);
3074 SkPixmap pixmap(SkImageInfo::MakeN32Premul(width, height), (const void*) &srcPixels.front(),
3075 width * 4);
3076 image->readPixels(pixmap, 0, 0);
3077 canvas->scale(.5f, .5f);
3078 width /= 4;
3079 height /= 4;
3080 for (int y = 0; y < 4; ++y) {
3081 for (int x = 0; x < 4; ++x) {
3082 SkBitmap bitmap;
3083 bitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height));
3084 bitmap.writePixels(pixmap, -y * width, -x * height);
3085 canvas->drawBitmap(bitmap, x * width, y * height);
3086 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003087 }
3088##
3089
3090#SeeAlso readPixels
3091
3092##
3093
3094# ------------------------------------------------------------------------------
3095
3096#Method bool writePixels(const SkPixmap& src)
3097
3098Copies a Rect of pixels from src. Copy starts at (0, 0), and does not exceed
3099(src.width(), src.height()).
3100
3101src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
3102and row bytes of source. src.rowBytes specifics the gap from one source
3103row to the next. Returns true if pixels are copied. Returns false if:
3104#List
3105# src pixel storage equals nullptr ##
3106# src.rowBytes is less than SkImageInfo::minRowBytes ##
3107# Pixel_Ref is nullptr ##
3108##
3109
3110Pixels are copied only if pixel conversion is possible. If this->colorType is
3111kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
3112If this->colorType is kGray_8_SkColorType, src Color_Space must match.
3113If this->alphaType is kOpaque_SkAlphaType, src Alpha_Type must
3114match. If this->colorSpace is nullptr, src Color_Space must match. Returns
3115false if pixel conversion is not possible.
3116
3117#Param src source Pixmap: Image_Info, pixels, row bytes ##
3118
3119#Return true if src pixels are copied to Bitmap ##
3120
3121#Example
3122#Height 80
Ben Wagner29380bd2017-10-09 14:43:00 -04003123 SkBitmap bitmap;
3124 bitmap.allocPixels(SkImageInfo::MakeN32Premul(2, 2));
3125 bitmap.eraseColor(SK_ColorGREEN);
3126 SkPMColor color = 0xFF5599BB;
3127 SkPixmap src(SkImageInfo::MakeN32Premul(1, 1), &color, 4);
3128 bitmap.writePixels(src);
3129 canvas->scale(40, 40);
3130 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003131##
3132
3133#SeeAlso readPixels
3134
3135##
3136
3137# ------------------------------------------------------------------------------
3138
3139#Method bool writePixels(const SkPixmap& src, int x, int y, SkTransferFunctionBehavior behavior)
3140
3141Copies a Rect of pixels from src. Copy starts at (0, 0), and does not exceed
3142(src.width(), src.height()).
3143
3144src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
3145and row bytes of source. src.rowBytes specifics the gap from one source
3146row to the next. Returns true if pixels are copied. Returns false if:
3147#List
3148# src pixel storage equals nullptr ##
3149# src.rowBytes is less than SkImageInfo::minRowBytes ##
3150# Pixel_Ref is nullptr ##
3151##
3152
3153Pixels are copied only if pixel conversion is possible. If this->colorType is
3154kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
3155If this->colorType is kGray_8_SkColorType, src Color_Space must match.
3156If this->alphaType is kOpaque_SkAlphaType, src Alpha_Type must
3157match. If this->colorSpace is nullptr, src Color_Space must match. Returns
3158false if pixel conversion is not possible. Returns false if width() or height()
3159is zero or negative.
3160
3161If behavior is SkTransferFunctionBehavior::kRespect: converts src
3162pixels to a linear space before converting to Image_Info.
3163If behavior is SkTransferFunctionBehavior::kIgnore: src
3164pixels are treated as if they are linear, regardless of how they are encoded.
3165
3166#Param src source Pixmap: Image_Info, pixels, row bytes ##
3167#Param x column index whose absolute value is less than width() ##
3168#Param y row index whose absolute value is less than height() ##
3169#Param behavior one of: SkTransferFunctionBehavior::kRespect,
3170 SkTransferFunctionBehavior::kIgnore
3171##
3172
3173#Return true if src pixels are copied to Bitmap ##
3174
3175#Example
3176#Height 64
Ben Wagner29380bd2017-10-09 14:43:00 -04003177 const int width = 256;
3178 const int height = 32;
3179 std::vector<int32_t> dstPixels;
3180 dstPixels.resize(height * width * 4);
3181 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
3182 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
3183 SkPoint gradPoints[] = { { 0, 0 }, { width, 0 } };
3184 SkPaint gradPaint;
3185 gradPaint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
3186 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
3187 for (auto behavior : { SkTransferFunctionBehavior::kRespect,
3188 SkTransferFunctionBehavior::kIgnore} ) {
3189 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
3190 SkBitmap bitmap;
3191 bitmap.installPixels(dstPixmap);
3192 SkCanvas srcCanvas(bitmap);
3193 srcCanvas.drawRect(SkRect::MakeWH(width, height), gradPaint);
3194 if (bitmap.writePixels(dstPixmap, 0, 0, behavior)) {
3195 canvas->drawBitmap(bitmap, 0, 0);
3196 }
3197 canvas->translate(0, height);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003198 }
3199##
3200
3201#SeeAlso readPixels
3202
3203##
3204
3205# ------------------------------------------------------------------------------
3206
3207#Method bool hasHardwareMipMap() const
3208
3209#Private
3210Android framework only.
3211##
3212
3213#Return true if setHasHardwareMipMap has been called with true ##
3214
3215#NoExample
3216##
3217
3218#SeeAlso setHasHardwareMipMap
3219
3220##
3221
3222# ------------------------------------------------------------------------------
3223
3224#Method void setHasHardwareMipMap(bool hasHardwareMipMap)
3225
3226#Private
3227Android framework only.
3228##
3229
3230#Param hasHardwareMipMap sets state ##
3231
3232#NoExample
3233##
3234
3235#SeeAlso hasHardwareMipMap
3236
3237##
3238
3239# ------------------------------------------------------------------------------
3240
3241#Method bool extractAlpha(SkBitmap* dst) const
3242
3243Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3244or dst pixels cannot be allocated.
3245
3246Uses HeapAllocator to reserve memory for dst Pixel_Ref.
3247
3248#Param dst holds Pixel_Ref to fill with alpha layer ##
3249
3250#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3251
3252#Example
3253#Height 100
Ben Wagner29380bd2017-10-09 14:43:00 -04003254 SkBitmap alpha, bitmap;
3255 bitmap.allocN32Pixels(100, 100);
3256 SkCanvas offscreen(bitmap);
3257 offscreen.clear(0);
3258 SkPaint paint;
3259 paint.setAntiAlias(true);
3260 paint.setColor(SK_ColorBLUE);
3261 paint.setStyle(SkPaint::kStroke_Style);
3262 paint.setStrokeWidth(20);
3263 offscreen.drawCircle(50, 50, 39, paint);
3264 offscreen.flush();
3265 bitmap.extractAlpha(&alpha);
3266 paint.setColor(SK_ColorRED);
3267 canvas->drawBitmap(bitmap, 0, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003268 canvas->drawBitmap(alpha, 100, 0, &paint);
3269##
3270
3271#SeeAlso extractSubset
3272
3273##
3274
3275# ------------------------------------------------------------------------------
3276
3277#Method bool extractAlpha(SkBitmap* dst, const SkPaint* paint,
3278 SkIPoint* offset) const
3279
3280Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3281or dst pixels cannot be allocated.
3282
3283If paint is not nullptr and contains Mask_Filter, SkMaskFilter::filterMask
3284generates Mask_Alpha from Bitmap. Uses HeapAllocator to reserve memory for dst
3285Pixel_Ref. Sets offset to top-left position for dst for alignment with Bitmap;
3286(0, 0) unless SkMaskFilter generates mask.
3287
3288#Param dst holds Pixel_Ref to fill with alpha layer ##
3289#Param paint holds optional Mask_Filter; may be nullptr ##
3290#Param offset top-left position for dst; may be nullptr ##
3291
3292#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3293
3294#Bug 7103 ##
3295#Example
3296#Height 160
Ben Wagner29380bd2017-10-09 14:43:00 -04003297 SkBitmap alpha, bitmap;
3298 bitmap.allocN32Pixels(100, 100);
3299 SkCanvas offscreen(bitmap);
3300 offscreen.clear(0);
3301 SkPaint paint;
3302 paint.setAntiAlias(true);
3303 paint.setColor(SK_ColorBLUE);
3304 paint.setStyle(SkPaint::kStroke_Style);
3305 paint.setStrokeWidth(20);
3306 offscreen.drawCircle(50, 50, 39, paint);
3307 offscreen.flush();
3308 const SkScalar kBlurSigma = SkBlurMaskFilter::ConvertRadiusToSigma(SkIntToScalar(25));
3309 paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, kBlurSigma,
3310 SkBlurMaskFilter::kHighQuality_BlurFlag));
3311 SkIPoint offset;
3312 bitmap.extractAlpha(&alpha, &paint, &offset);
3313 paint.setColor(SK_ColorRED);
3314 canvas->drawBitmap(bitmap, 0, -offset.fY, &paint);
3315 canvas->drawBitmap(alpha, 100 + offset.fX, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003316##
3317
3318#SeeAlso extractSubset
3319
3320##
3321
3322# ------------------------------------------------------------------------------
3323
3324#Method bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator,
3325 SkIPoint* offset) const
3326
3327Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3328or dst pixels cannot be allocated.
3329
3330If paint is not nullptr and contains Mask_Filter, SkMaskFilter::filterMask
3331generates Mask_Alpha from Bitmap. allocator may reference a custom allocation
3332class or be set to nullptr to use HeapAllocator. Sets offset to top-left
3333position for dst for alignment with Bitmap; (0, 0) unless SkMaskFilter generates
3334mask.
3335
3336#Param dst holds Pixel_Ref to fill with alpha layer ##
3337#Param paint holds optional Mask_Filter; may be nullptr ##
3338#Param allocator method to reserve memory for Pixel_Ref; may be nullptr ##
3339#Param offset top-left position for dst; may be nullptr ##
3340
3341#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3342
3343#Bug 7104 ##
3344#Example
3345#Height 128
Ben Wagner29380bd2017-10-09 14:43:00 -04003346 SkBitmap alpha, bitmap;
3347 bitmap.allocN32Pixels(100, 100);
3348 SkCanvas offscreen(bitmap);
3349 offscreen.clear(0);
3350 SkPaint paint;
3351 paint.setAntiAlias(true);
3352 paint.setColor(SK_ColorBLUE);
3353 paint.setStyle(SkPaint::kStroke_Style);
3354 paint.setStrokeWidth(20);
3355 offscreen.drawCircle(50, 50, 39, paint);
3356 offscreen.flush();
Cary Clarkbc5697d2017-10-04 14:31:33 -04003357 paint.setMaskFilter(SkBlurMaskFilter::Make(kOuter_SkBlurStyle, 3));
Ben Wagner29380bd2017-10-09 14:43:00 -04003358 SkIPoint offset;
3359 bitmap.extractAlpha(&alpha, &paint, nullptr, &offset);
3360 paint.setColor(SK_ColorRED);
3361 canvas->drawBitmap(bitmap, 0, -offset.fY, &paint);
3362 canvas->drawBitmap(alpha, 100 + offset.fX, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003363##
3364
3365#SeeAlso extractSubset
3366
3367##
3368
3369# ------------------------------------------------------------------------------
3370
3371#Method bool peekPixels(SkPixmap* pixmap) const
3372
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
Ben Wagner29380bd2017-10-09 14:43:00 -04003402 ------
3403 --xxx-
3404 -x--x-
3405 ----x-
3406 ---xx-
3407 --xx--
3408 --x---
3409 ------
3410 --x---
3411 --x---
Cary Clarkbc5697d2017-10-04 14:31:33 -04003412 ------
3413 #StdOut ##
3414##
3415
3416#SeeAlso installPixels readPixels writePixels
3417
3418##
3419
3420# ------------------------------------------------------------------------------
3421
Cary Clark154beea2017-10-26 07:58:48 -04003422#Method void validate() const;
3423
3424Asserts if internal values are illegal or inconsistent. Only available if
3425SK_DEBUG is defined at compile time.
3426
3427#NoExample
3428##
3429
3430#SeeAlso SkImageInfo::validate()
3431
3432##
3433
3434# ------------------------------------------------------------------------------
3435
Cary Clarkbc5697d2017-10-04 14:31:33 -04003436#Method void toString(SkString* str) const;
3437
3438#DefinedBy SK_TO_STRING_NONVIRT() ##
3439
3440#Private
3441macro expands to: void toString(SkString* str) const;
3442##
3443
3444Creates string representation. The representation is read by
3445internal debugging tools. The interface and implementation may be
3446suppressed by defining SK_IGNORE_TO_STRING.
3447
3448#Param str storage for string representation ##
3449
3450#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04003451 SkBitmap bitmap;
3452 int width = 6;
3453 int height = 11;
3454 bitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height));
3455 SkString string;
3456 bitmap.toString(&string);
3457 SkString match;
3458 match.printf("(%d, %d)", width, height);
3459 int start = string.find(match.c_str());
3460 if (start >= 0) {
3461 SkString whStr(&string.c_str()[start], match.size());
3462 SkDebugf("bitmap dimensions %s\n", whStr.c_str());
Cary Clarkbc5697d2017-10-04 14:31:33 -04003463 }
3464 #StdOut
3465 bitmap dimensions (6, 11)
3466 ##
3467##
3468
3469#SeeAlso SkPaint::toString
3470
3471##
3472
3473#Class SkBitmap ##
3474
3475#Topic Bitmap ##