blob: 6f65688d2d22acb9bea624628d74bd653b0343d2 [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. ##
Cary Clarka560c472017-11-27 10:44:06 -0500113# height() # Returns pixel row count. ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400114# 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
Cary Clarka560c472017-11-27 10:44:06 -0500480Returns pixel count in each 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
Cary Clark2ade9972017-11-02 17:49:34 -04001298#Height 128
Cary Clarkbc5697d2017-10-04 14:31:33 -04001299#Image 4
Ben Wagner29380bd2017-10-09 14:43:00 -04001300 SkIRect bounds = source.bounds();
1301 for (int x : { 0, bounds.width() } ) {
1302 for (int y : { 0, bounds.height() } ) {
1303 canvas->drawBitmap(source, x, y);
1304 }
1305 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001306##
1307
1308#SeeAlso getBounds
1309
1310##
1311
1312# ------------------------------------------------------------------------------
1313
1314#Method SkISize dimensions() const
1315
1316Returns ISize { width(), height() }.
1317
1318#Return integral size of width() and height() ##
1319
1320#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001321 SkBitmap bitmap;
1322 bitmap.setInfo(SkImageInfo::MakeN32(33, 55, kOpaque_SkAlphaType));
1323 SkISize dimensions = bitmap.dimensions();
1324 SkRect bounds;
1325 bitmap.getBounds(&bounds);
1326 SkRect dimensionsAsBounds = SkRect::Make(dimensions);
1327 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -04001328##
1329
1330#SeeAlso height() width()
1331
1332##
1333
1334# ------------------------------------------------------------------------------
1335
1336#Method SkIRect getSubset() const
1337
1338Returns the bounds of this bitmap, offset by its Pixel_Ref origin.
1339
1340#Return bounds within Pixel_Ref bounds ##
1341
1342#Example
1343#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001344 SkIRect bounds;
1345 source.getBounds(&bounds);
1346 bounds.inset(100, 100);
1347 SkBitmap subset;
1348 source.extractSubset(&subset, bounds);
1349 SkIRect r = source.getSubset();
1350 SkDebugf("source: %d, %d, %d, %d\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
1351 r = subset.getSubset();
1352 SkDebugf("subset: %d, %d, %d, %d\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
1353#StdOut
1354source: 0, 0, 512, 512
1355subset: 100, 100, 412, 412
1356##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001357##
1358
1359#SeeAlso extractSubset getBounds
1360
1361##
1362
1363# ------------------------------------------------------------------------------
1364
1365#Method bool setInfo(const SkImageInfo& imageInfo, size_t rowBytes = 0)
1366
1367Sets width, height, Alpha_Type, Color_Type, Color_Space, and optional
1368rowBytes. Frees pixels, and returns true if successful.
1369
1370imageInfo.alphaType may be altered to a value permitted by imageInfo.colorSpace.
1371If imageInfo.colorType is kUnknown_SkColorType, imageInfo.alphaType is
1372set to kUnknown_SkAlphaType.
1373If imageInfo.colorType is kAlpha_8_SkColorType and imageInfo.alphaType is
1374kUnpremul_SkAlphaType, imageInfo.alphaType is replaced by kPremul_SkAlphaType.
1375If imageInfo.colorType is kRGB_565_SkColorType or kGray_8_SkColorType,
1376imageInfo.alphaType is set to kOpaque_SkAlphaType.
1377If imageInfo.colorType is kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
1378kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: imageInfo.alphaType remains
1379unchanged.
1380
1381rowBytes must equal or exceed imageInfo.minRowBytes. If imageInfo.colorSpace is
1382kUnknown_SkColorType, rowBytes is ignored and treated as zero; for all other
1383Color_Space values, rowBytes of zero is treated as imageInfo.minRowBytes.
1384
1385Calls reset() and returns false if:
1386#List
1387# rowBytes exceeds 31 bits ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001388# 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
Cary Clarka560c472017-11-27 10:44:06 -05001448not be allocated, or memory could not optionally be zeroed.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001449
1450On most platforms, allocating pixel memory may succeed even though there is
1451not sufficient memory to hold pixels; allocation does not take place
1452until the pixels are written to. The actual behavior depends on the platform
1453implementation of malloc(), if flags is zero, and calloc(), if flags is
1454kZeroPixels_AllocFlag.
1455
1456Passing kZeroPixels_AllocFlag is usually faster than separately calling
1457eraseColor(SK_ColorTRANSPARENT).
1458
1459#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1460#Param flags kZeroPixels_AllocFlag, or zero ##
1461
1462#Return true if pixels allocation is successful ##
1463
1464#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001465 SkBitmap bitmap;
Cary Clarka560c472017-11-27 10:44:06 -05001466 if (!bitmap.tryAllocPixelsFlags(SkImageInfo::MakeN32(10000, 10000, kOpaque_SkAlphaType),
1467 SkBitmap::kZeroPixels_AllocFlag)) {
1468 SkDebugf("bitmap allocation failed!\n");
1469 } else {
1470 SkDebugf("bitmap allocation succeeded!\n");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001471 }
1472#StdOut
Cary Clarka560c472017-11-27 10:44:06 -05001473bitmap allocation succeeded!
Cary Clarkbc5697d2017-10-04 14:31:33 -04001474##
1475##
1476
1477#SeeAlso allocPixelsFlags tryAllocPixels SkMallocPixelRef::MakeZeroed
1478
1479##
1480
1481# ------------------------------------------------------------------------------
1482
1483#Method void allocPixelsFlags(const SkImageInfo& info, uint32_t flags)
1484
1485Sets Image_Info to info following the rules in setInfo and allocates pixel
1486memory. If flags is kZeroPixels_AllocFlag, memory is zeroed.
1487
1488Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001489not be allocated, or memory could not optionally
Cary Clarkbc5697d2017-10-04 14:31:33 -04001490be zeroed. Abort steps may be provided by the user at compile time by defining
1491SK_ABORT.
1492
1493On most platforms, allocating pixel memory may succeed even though there is
1494not sufficient memory to hold pixels; allocation does not take place
1495until the pixels are written to. The actual behavior depends on the platform
1496implementation of malloc(), if flags is zero, and calloc(), if flags is
1497kZeroPixels_AllocFlag.
1498
1499Passing kZeroPixels_AllocFlag is usually faster than separately calling
1500eraseColor(SK_ColorTRANSPARENT).
1501
1502#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1503#Param flags kZeroPixels_AllocFlag, or zero ##
1504
1505#Example
1506#Height 128
1507#Description
1508Text is drawn on a transparent background; drawing the bitmap a second time
1509lets the first draw show through.
1510##
1511###^
Ben Wagner29380bd2017-10-09 14:43:00 -04001512SkBitmap bitmap;
1513bitmap.allocPixelsFlags(SkImageInfo::MakeN32(44, 16, kPremul_SkAlphaType),
1514 SkBitmap::kZeroPixels_AllocFlag);
1515SkCanvas offscreen(bitmap);
1516SkPaint paint;
1517offscreen.drawString("!@#$%", 0, 12, paint);
1518canvas->scale(6, 6);
1519canvas->drawBitmap(bitmap, 0, 0);
1520canvas->drawBitmap(bitmap, 8, 8);
1521^^^#
Cary Clarkbc5697d2017-10-04 14:31:33 -04001522##
1523
1524#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeZeroed
1525
1526##
1527
1528# ------------------------------------------------------------------------------
1529
1530#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info, size_t rowBytes)
1531
1532#ToDo am I ever conflicted about setInfo rules. It needs to be able to be replicated
1533 if, for instance, I generate one-page-per-method HTML-style documentation
1534 I'm not so sure it makes sense to put the indirection in for .h either unless
1535 my mantra is that .h should abbreviate full documentation. And, what to do
1536 for generated markdown? At least there the rules are a click away, although
1537 a pop-down in place would be way better. Hmmm.
1538##
1539
1540Sets Image_Info to info following the rules in setInfo and allocates pixel
1541memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(),
1542or equal zero. Pass in zero for rowBytes to compute the minimum valid value.
1543
1544Returns false and calls reset() if Image_Info could not be set, or memory could
1545not be allocated.
1546
1547On most platforms, allocating pixel memory may succeed even though there is
1548not sufficient memory to hold pixels; allocation does not take place
1549until the pixels are written to. The actual behavior depends on the platform
1550implementation of malloc().
1551
1552#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1553#Param rowBytes size of pixel row or larger; may be zero ##
1554
1555#Return true if pixel storage is allocated ##
1556
1557#Example
1558#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001559SkBitmap bitmap;
1560SkImageInfo info = SkImageInfo::Make(64, 256, kGray_8_SkColorType, kOpaque_SkAlphaType);
1561if (bitmap.tryAllocPixels(info, 0)) {
1562 SkCanvas offscreen(bitmap);
1563 offscreen.scale(.5f, .5f);
1564 for (int x : { 0, 64, 128, 192 } ) {
1565 offscreen.drawBitmap(source, -x, 0);
1566 canvas->drawBitmap(bitmap, x, 0);
1567 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001568}
1569##
1570
1571#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeAllocate
1572
1573##
1574
1575# ------------------------------------------------------------------------------
1576
1577#Method void allocPixels(const SkImageInfo& info, size_t rowBytes)
1578
1579Sets Image_Info to info following the rules in setInfo and allocates pixel
1580memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(),
1581or equal zero. Pass in zero for rowBytes to compute the minimum valid value.
1582
1583Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001584not be allocated. Abort steps may be provided by
Cary Clarkbc5697d2017-10-04 14:31:33 -04001585the user at compile time by defining SK_ABORT.
1586
1587On most platforms, allocating pixel memory may succeed even though there is
1588not sufficient memory to hold pixels; allocation does not take place
1589until the pixels are written to. The actual behavior depends on the platform
1590implementation of malloc().
1591
1592#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1593#Param rowBytes size of pixel row or larger; may be zero ##
1594
1595#Example
1596#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001597SkBitmap bitmap;
1598SkImageInfo info = SkImageInfo::Make(256, 64, kGray_8_SkColorType, kOpaque_SkAlphaType);
1599bitmap.allocPixels(info, info.width() * info.bytesPerPixel() + 64);
1600SkCanvas offscreen(bitmap);
1601offscreen.scale(.5f, .5f);
1602for (int y : { 0, 64, 128, 192 } ) {
1603 offscreen.drawBitmap(source, 0, -y);
1604 canvas->drawBitmap(bitmap, 0, y);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001605}
1606##
1607
1608#SeeAlso tryAllocPixels allocPixelsFlags SkMallocPixelRef::MakeAllocate
1609
1610##
1611
1612# ------------------------------------------------------------------------------
1613
1614#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info)
1615
1616Sets Image_Info to info following the rules in setInfo and allocates pixel
1617memory.
1618
1619Returns false and calls reset() if Image_Info could not be set, or memory could
1620not be allocated.
1621
1622On most platforms, allocating pixel memory may succeed even though there is
1623not sufficient memory to hold pixels; allocation does not take place
1624until the pixels are written to. The actual behavior depends on the platform
1625implementation of malloc().
1626
1627#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1628
1629#Return true if pixel storage is allocated ##
1630
1631#Example
1632#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04001633SkBitmap bitmap;
1634if (bitmap.tryAllocPixels(SkImageInfo::Make(64, 64, kGray_8_SkColorType, kOpaque_SkAlphaType))) {
1635 SkCanvas offscreen(bitmap);
1636 offscreen.scale(.25f, .5f);
1637 for (int y : { 0, 64, 128, 192 } ) {
1638 offscreen.drawBitmap(source, -y, -y);
1639 canvas->drawBitmap(bitmap, y, y);
1640 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001641}
1642##
1643
1644#SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeAllocate
1645
1646##
1647
1648# ------------------------------------------------------------------------------
1649
1650#Method void allocPixels(const SkImageInfo& info)
1651
1652Sets Image_Info to info following the rules in setInfo and allocates pixel
1653memory.
1654
1655Aborts execution if Image_Info could not be set, or memory could
Cary Clarka560c472017-11-27 10:44:06 -05001656not be allocated. Abort steps may be provided by
Cary Clarkbc5697d2017-10-04 14:31:33 -04001657the user at compile time by defining SK_ABORT.
1658
1659On most platforms, allocating pixel memory may succeed even though there is
1660not sufficient memory to hold pixels; allocation does not take place
1661until the pixels are written to. The actual behavior depends on the platform
1662implementation of malloc().
1663
1664#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1665
1666#Example
1667#Image 4
Ben Wagner29380bd2017-10-09 14:43:00 -04001668SkBitmap bitmap;
1669bitmap.allocPixels(SkImageInfo::Make(64, 64, kGray_8_SkColorType, kOpaque_SkAlphaType));
1670SkCanvas offscreen(bitmap);
1671offscreen.scale(.5f, .5f);
1672for (int y : { 0, 64, 128, 192 } ) {
1673 offscreen.drawBitmap(source, -y, -y);
1674 canvas->drawBitmap(bitmap, y, y);
1675}
Cary Clarkbc5697d2017-10-04 14:31:33 -04001676##
1677
1678#SeeAlso tryAllocPixels allocPixelsFlags SkMallocPixelRef::MakeAllocate
1679
1680##
1681
1682# ------------------------------------------------------------------------------
1683
1684#Method bool SK_WARN_UNUSED_RESULT tryAllocN32Pixels(int width, int height, bool isOpaque = false)
1685
Cary Clarka560c472017-11-27 10:44:06 -05001686Sets Image_Info to width, height, and Native_Color_Type; and allocates
Cary Clarkbc5697d2017-10-04 14:31:33 -04001687pixel memory. If isOpaque is true, sets Image_Info to kOpaque_SkAlphaType;
1688otherwise, sets to kPremul_SkAlphaType.
1689
1690Calls reset() and returns false if width exceeds 29 bits or is negative,
1691or height is negative.
1692
1693Returns false if allocation fails.
1694
Cary Clarka560c472017-11-27 10:44:06 -05001695Use to create Bitmap that matches SkPMColor, the native pixel arrangement on
1696the platform. Bitmap drawn to output device skips converting its pixel format.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001697
1698#Param width pixel column count; must be zero or greater ##
1699#Param height pixel row count; must be zero or greater ##
1700#Param isOpaque true if pixels do not have transparency ##
1701
1702#Return true if pixel storage is allocated ##
1703
1704#Example
1705#Height 160
Ben Wagner29380bd2017-10-09 14:43:00 -04001706 SkBitmap bitmap;
1707 if (bitmap.tryAllocN32Pixels(80, 80)) {
1708 bitmap.eraseColor(SK_ColorTRANSPARENT);
1709 bitmap.erase(0x7f3f7fff, SkIRect::MakeWH(50, 30));
1710 bitmap.erase(0x3f7fff3f, SkIRect::MakeXYWH(20, 10, 50, 30));
1711 bitmap.erase(0x5fff3f7f, SkIRect::MakeXYWH(40, 20, 50, 30));
1712 canvas->drawBitmap(bitmap, 0, 0);
1713 for (int x : { 0, 30, 60, 90 } ) {
1714 canvas->drawBitmap(bitmap, x, 70);
1715 }
1716 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001717##
1718
1719#SeeAlso tryAllocPixels allocN32Pixels SkMallocPixelRef::MakeAllocate
1720
1721##
1722
1723# ------------------------------------------------------------------------------
1724
1725#Method void allocN32Pixels(int width, int height, bool isOpaque = false)
1726
Cary Clarka560c472017-11-27 10:44:06 -05001727Sets Image_Info to width, height, and the Native_Color_Type; and allocates
Cary Clarkbc5697d2017-10-04 14:31:33 -04001728pixel memory. If isOpaque is true, sets Image_Info to kPremul_SkAlphaType;
1729otherwise, sets to kOpaque_SkAlphaType.
1730
1731Aborts if width exceeds 29 bits or is negative, or height is negative, or
1732allocation fails. Abort steps may be provided by the user at compile time by
1733defining SK_ABORT.
1734
Cary Clarka560c472017-11-27 10:44:06 -05001735Use to create Bitmap that matches SkPMColor, the native pixel arrangement on
1736the platform. Bitmap drawn to output device skips converting its pixel format.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001737
1738#Param width pixel column count; must be zero or greater ##
1739#Param height pixel row count; must be zero or greater ##
1740#Param isOpaque true if pixels do not have transparency ##
1741
1742#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04001743 SkRandom random;
1744 SkBitmap bitmap;
1745 bitmap.allocN32Pixels(64, 64);
1746 bitmap.eraseColor(SK_ColorTRANSPARENT);
1747 for (int y = 0; y < 256; y += 64) {
1748 for (int x = 0; x < 256; x += 64) {
1749 SkColor color = random.nextU();
1750 uint32_t w = random.nextRangeU(4, 32);
1751 uint32_t cx = random.nextRangeU(0, 64 - w);
1752 uint32_t h = random.nextRangeU(4, 32);
1753 uint32_t cy = random.nextRangeU(0, 64 - h);
1754 bitmap.erase(color, SkIRect::MakeXYWH(cx, cy, w, h));
1755 canvas->drawBitmap(bitmap, x, y);
1756 }
1757 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001758##
1759
1760#SeeAlso allocPixels tryAllocN32Pixels SkMallocPixelRef::MakeAllocate
1761
1762##
1763
1764# ------------------------------------------------------------------------------
1765
1766#Method bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
1767 void (*releaseProc)(void* addr, void* context), void* context)
1768
1769Sets Image_Info to info following the rules in setInfo, and creates Pixel_Ref
1770containing pixels and rowBytes. releaseProc, if not nullptr, is called
1771immediately on failure or when pixels are no longer referenced. context may be
1772nullptr.
1773
1774If Image_Info could not be set, or rowBytes is less than info.minRowBytes:
1775calls releaseProc if present, calls reset(), and returns false.
1776
1777Otherwise, if pixels equals nullptr: sets Image_Info, calls releaseProc if
1778present, returns true.
1779
1780If Image_Info is set, pixels is not nullptr, and releaseProc is not nullptr:
1781when pixels are no longer referenced, calls releaseProc with pixels and context
1782as parameters.
1783
1784#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1785#Param pixels address or pixel storage; may be nullptr ##
1786#Param rowBytes size of pixel row or larger ##
1787#Param releaseProc function called when pixels can be deleted; may be nullptr ##
1788#Param context caller state passed to releaseProc; may be nullptr ##
1789
1790#Return true if Image_Info is set to info ##
1791
1792#Example
1793#Description
1794releaseProc is called immediately because rowBytes is too small for Pixel_Ref.
1795##
1796#Function
Ben Wagner29380bd2017-10-09 14:43:00 -04001797static void releaseProc(void* addr, void* ) {
1798 SkDebugf("releaseProc called\n");
1799 delete[] (uint32_t*) addr;
1800}
1801
1802##
1803
1804void draw(SkCanvas* canvas) {
1805 SkBitmap bitmap;
1806 void* pixels = new uint32_t[8 * 8];
1807 SkImageInfo info = SkImageInfo::MakeN32(8, 8, kOpaque_SkAlphaType);
1808 SkDebugf("before installPixels\n");
1809 bool installed = bitmap.installPixels(info, pixels, 16, releaseProc, nullptr);
1810 SkDebugf("install " "%s" "successful\n", installed ? "" : "not ");
Cary Clarkbc5697d2017-10-04 14:31:33 -04001811}
1812#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04001813before installPixels
1814releaseProc called
Cary Clarkbc5697d2017-10-04 14:31:33 -04001815install not successful
1816##
1817##
1818
1819#SeeAlso allocPixels
1820
1821##
1822
1823# ------------------------------------------------------------------------------
1824
1825#Method bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes)
1826
1827Sets Image_Info to info following the rules in setInfo, and creates Pixel_Ref
1828containing pixels and rowBytes.
1829
1830If Image_Info could not be set, or rowBytes is less than info.minRowBytes:
1831calls reset(), and returns false.
1832
1833Otherwise, if pixels equals nullptr: sets Image_Info, returns true.
1834
1835Caller must ensure that pixels are valid for the lifetime of Bitmap and Pixel_Ref.
1836
1837#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
1838#Param pixels address or pixel storage; may be nullptr ##
1839#Param rowBytes size of pixel row or larger ##
1840
1841#Return true if Image_Info is set to info ##
1842
1843#Example
1844#Description
1845#Bug 7079 ##
1846GPU does not support kUnpremul_SkAlphaType, does not assert that it does not.
1847##
Ben Wagner29380bd2017-10-09 14:43:00 -04001848void draw(SkCanvas* canvas) {
1849 SkRandom random;
1850 SkBitmap bitmap;
1851 const int width = 8;
1852 const int height = 8;
1853 uint32_t pixels[width * height];
1854 for (unsigned x = 0; x < width * height; ++x) {
1855 pixels[x] = random.nextU();
1856 }
1857 SkImageInfo info = SkImageInfo::MakeN32(width, height, kUnpremul_SkAlphaType);
1858 if (bitmap.installPixels(info, pixels, info.minRowBytes())) {
1859 canvas->scale(32, 32);
1860 canvas->drawBitmap(bitmap, 0, 0);
1861 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001862}
1863##
1864
1865#SeeAlso allocPixels
1866
1867##
1868
1869# ------------------------------------------------------------------------------
1870
1871#Method bool installPixels(const SkPixmap& pixmap)
1872
1873Sets Image_Info to pixmap.info() following the rules in setInfo, and creates
1874Pixel_Ref containing pixmap.addr() and pixmap.rowBytes.
1875
1876If Image_Info could not be set, or pixmap.rowBytes is less than
1877SkImageInfo::minRowBytes: calls reset(), and returns false.
1878
1879Otherwise, if pixmap.addr() equals nullptr: sets Image_Info, returns true.
1880
1881Caller must ensure that pixmap is valid for the lifetime of Bitmap and Pixel_Ref.
1882
1883#Param pixmap Image_Info, pixel address, and rowBytes ##
1884
1885#Return true if Image_Info was set to pixmap.info() ##
1886
1887#Example
1888#Description
1889Draw a five by five bitmap, and draw it again with a center white pixel.
1890##
1891#Height 64
1892 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
1893 { 0xAC, 0xA8, 0x89, 0x47, 0x87 },
1894 { 0x4B, 0x25, 0x25, 0x25, 0x46 },
1895 { 0x90, 0x81, 0x25, 0x41, 0x33 },
1896 { 0x75, 0x55, 0x44, 0x20, 0x00 }};
1897 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
1898 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
1899 SkBitmap bitmap;
1900 bitmap.installPixels(pixmap);
1901 canvas->scale(10, 10);
1902 canvas->drawBitmap(bitmap, 0, 0);
1903 *pixmap.writable_addr8(2, 2) = 0xFF;
1904 bitmap.installPixels(pixmap);
1905 canvas->drawBitmap(bitmap, 10, 0);
1906##
1907
1908#SeeAlso allocPixels
1909
1910##
1911
1912# ------------------------------------------------------------------------------
1913
1914#Method bool installMaskPixels(const SkMask& mask)
1915
1916Sets Image_Info to mask width, mask height, kAlpha_8_SkColorType, and
1917kPremul_SkAlphaType. Sets Pixel_Ref to mask image and mask rowBytes.
1918
1919Returns false and calls reset() if mask format is not SkMask::kA8_Format,
1920or if mask width or mask height is negative, or if mask rowBytes is less
1921than mask width.
1922
1923Caller must ensure that mask is valid for the lifetime of Bitmap and Pixel_Ref.
1924
1925#Param mask Alpha 8-bit bitmap ##
1926
1927#Return true if Image_Info and Pixel_Ref refer to mask ##
1928
1929#Example
1930#Description
1931Draw a five by five bitmap, and draw it again with a center white pixel.
1932##
1933#Height 64
Ben Wagner29380bd2017-10-09 14:43:00 -04001934 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
1935 { 0xAC, 0xA8, 0x89, 0x47, 0x87 },
1936 { 0x4B, 0x25, 0x25, 0x25, 0x46 },
1937 { 0x90, 0x81, 0x25, 0x41, 0x33 },
1938 { 0x75, 0x55, 0x44, 0x20, 0x00 }};
1939 SkMask mask;
1940 mask.fImage = storage[0];
1941 mask.fBounds = SkIRect::MakeWH(5, 5);
1942 mask.fRowBytes = 5;
1943 mask.fFormat = SkMask::kA8_Format;
1944 SkBitmap bitmap;
1945 bitmap.installMaskPixels(mask);
1946 canvas->scale(10, 10);
1947 canvas->drawBitmap(bitmap, 0, 0);
1948 storage[2][2] = 0xFF;
1949 bitmap.installMaskPixels(mask);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001950 canvas->drawBitmap(bitmap, 10, 0);
1951##
1952
1953#SeeAlso installPixels allocPixels
1954
1955##
1956
1957# ------------------------------------------------------------------------------
1958
1959#Method void setPixels(void* pixels)
1960
1961Replaces Pixel_Ref with pixels, preserving Image_Info and rowBytes.
1962Sets Pixel_Ref origin to (0, 0).
1963
1964If pixels is nullptr, or if info().colorType equals kUnknown_SkColorType;
1965release reference to Pixel_Ref, and set Pixel_Ref to nullptr.
1966
1967Caller is responsible for handling ownership pixel memory for the lifetime
1968of Bitmap and Pixel_Ref.
1969
1970#Param pixels address of pixel storage, managed by caller ##
1971
1972#Example
1973#Height 50
Ben Wagner29380bd2017-10-09 14:43:00 -04001974 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
1975 uint8_t set2[5] = { 0xAC, 0xA8, 0x89, 0x47, 0x87 };
1976 SkBitmap bitmap;
1977 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
1978 canvas->scale(10, 50);
1979 canvas->drawBitmap(bitmap, 0, 0);
1980 bitmap.setPixels(set2);
1981 canvas->drawBitmap(bitmap, 10, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001982##
1983
1984#SeeAlso installPixels allocPixels
1985
1986##
1987
1988# ------------------------------------------------------------------------------
1989
1990#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels()
1991
1992Allocates pixel memory with HeapAllocator, and replaces existing Pixel_Ref.
1993The allocation size is determined by Image_Info width, height, and Color_Type.
1994
Cary Clarka560c472017-11-27 10:44:06 -05001995Returns false if info().colorType is kUnknown_SkColorType, or allocation fails.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001996
1997#Return true if the allocation succeeds
1998##
1999
2000#Example
2001#Height 50
2002#Description
2003Bitmap hosts and draws gray values in set1. tryAllocPixels replaces Pixel_Ref
2004and erases it to black, but does not alter set1. setPixels replaces black
2005Pixel_Ref with set1.
2006##
Ben Wagner29380bd2017-10-09 14:43:00 -04002007 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
2008 SkBitmap bitmap;
2009 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
2010 canvas->scale(10, 50);
2011 canvas->drawBitmap(bitmap, 0, 0);
2012 if (bitmap.tryAllocPixels()) {
2013 bitmap.eraseColor(SK_ColorBLACK);
2014 canvas->drawBitmap(bitmap, 8, 0);
2015 bitmap.setPixels(set1);
2016 canvas->drawBitmap(bitmap, 16, 0);
2017 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002018##
2019
2020#SeeAlso allocPixels installPixels setPixels
2021
2022##
2023
2024# ------------------------------------------------------------------------------
2025
2026#Method void allocPixels()
2027
2028Allocates pixel memory with HeapAllocator, and replaces existing Pixel_Ref.
2029The allocation size is determined by Image_Info width, height, and Color_Type.
2030
Cary Clarka560c472017-11-27 10:44:06 -05002031Aborts if info().colorType is kUnknown_SkColorType, or allocation fails.
2032Abort steps may be provided by the user at compile
Cary Clarkbc5697d2017-10-04 14:31:33 -04002033time by defining SK_ABORT.
2034
2035#Example
2036#Height 50
2037#Description
2038Bitmap hosts and draws gray values in set1. allocPixels replaces Pixel_Ref
2039and erases it to black, but does not alter set1. setPixels replaces black
2040Pixel_Ref with set2.
2041##
Ben Wagner29380bd2017-10-09 14:43:00 -04002042 uint8_t set1[5] = { 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 };
2043 uint8_t set2[5] = { 0xAC, 0xA8, 0x89, 0x47, 0x87 };
2044 SkBitmap bitmap;
2045 bitmap.installPixels(SkImageInfo::Make(5, 1, kGray_8_SkColorType, kOpaque_SkAlphaType), set1, 5);
2046 canvas->scale(10, 50);
2047 canvas->drawBitmap(bitmap, 0, 0);
2048 bitmap.allocPixels();
2049 bitmap.eraseColor(SK_ColorBLACK);
2050 canvas->drawBitmap(bitmap, 8, 0);
2051 bitmap.setPixels(set2);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002052 canvas->drawBitmap(bitmap, 16, 0);
2053##
2054
2055#SeeAlso tryAllocPixels installPixels setPixels
2056
2057##
2058
2059# ------------------------------------------------------------------------------
2060
2061#Method bool SK_WARN_UNUSED_RESULT tryAllocPixels(Allocator* allocator)
2062
2063Allocates pixel memory with allocator, and replaces existing Pixel_Ref.
2064The allocation size is determined by Image_Info width, height, and Color_Type.
2065If allocator is nullptr, use HeapAllocator instead.
2066
2067Returns false if allocator allocPixelRef return false.
2068
2069#Param allocator instance of SkBitmap::Allocator instantiation ##
2070
2071#Return true if custom allocator reports success
2072##
2073
2074#Example
2075#Height 100
2076#Description
2077HeapAllocator limits the maximum size of Bitmap to two gigabytes. Using
2078a custom allocator, this limitation may be relaxed. This example can be
2079modified to allocate an eight gigabyte Bitmap on a 64 bit platform with
2080sufficient memory.
2081##
2082#Function
2083class LargePixelRef : public SkPixelRef {
2084public:
2085 LargePixelRef(const SkImageInfo& info, char* storage, size_t rowBytes)
2086 : SkPixelRef(info.width(), info.height(), storage, rowBytes) {
2087 }
2088
2089 ~LargePixelRef() override {
2090 delete[] (char* ) this->pixels();
2091 }
2092};
2093
2094class LargeAllocator : public SkBitmap::Allocator {
2095public:
2096 bool allocPixelRef(SkBitmap* bitmap) override {
2097 const SkImageInfo& info = bitmap->info();
2098 uint64_t rowBytes = info.minRowBytes64();
2099 uint64_t size = info.height() * rowBytes;
2100 char* addr = new char[size];
2101 if (nullptr == addr) {
2102 return false;
2103 }
2104 sk_sp<SkPixelRef> pr = sk_sp<SkPixelRef>(new LargePixelRef(info, addr, rowBytes));
2105 if (!pr) {
2106 return false;
2107 }
2108 bitmap->setPixelRef(std::move(pr), 0, 0);
2109 return true;
2110 }
2111};
2112
2113##
2114
2115void draw(SkCanvas* canvas) {
2116 LargeAllocator largeAllocator;
2117 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002118 int width = 100; // make this 20000
2119 int height = 100; // and this 100000 to allocate 8 gigs on a 64-bit platform
2120 bitmap.setInfo(SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType));
2121 if (bitmap.tryAllocPixels(&largeAllocator)) {
2122 bitmap.eraseColor(0xff55aa33);
2123 canvas->drawBitmap(bitmap, 0, 0);
2124 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002125}
2126
2127##
2128
2129#SeeAlso allocPixels Allocator Pixel_Ref
2130
2131##
2132
2133# ------------------------------------------------------------------------------
2134
2135#Method void allocPixels(Allocator* allocator)
2136
2137Allocates pixel memory with allocator, and replaces existing Pixel_Ref.
2138The allocation size is determined by Image_Info width, height, and Color_Type.
2139If allocator is nullptr, use HeapAllocator instead.
2140
2141Aborts if allocator allocPixelRef return false. Abort steps may be provided by
2142the user at compile time by defining SK_ABORT.
2143
2144#Param allocator instance of SkBitmap::Allocator instantiation ##
2145
2146#Example
2147#Height 32
2148#Function
2149class TinyAllocator : public SkBitmap::Allocator {
2150public:
2151 bool allocPixelRef(SkBitmap* bitmap) override {
2152 const SkImageInfo& info = bitmap->info();
2153 if (info.height() * info.minRowBytes() > sizeof(storage)) {
2154 return false;
2155 }
2156 sk_sp<SkPixelRef> pr = sk_sp<SkPixelRef>(
2157 new SkPixelRef(info.width(), info.height(), storage, info.minRowBytes()));
2158 bitmap->setPixelRef(std::move(pr), 0, 0);
2159 return true;
2160 }
2161
2162 char storage[16];
2163};
2164
2165##
2166
2167void draw(SkCanvas* canvas) {
2168 TinyAllocator tinyAllocator;
2169 SkBitmap bitmap;
Ben Wagner29380bd2017-10-09 14:43:00 -04002170 bitmap.setInfo(SkImageInfo::MakeN32(2, 2, kOpaque_SkAlphaType));
2171 if (bitmap.tryAllocPixels(&tinyAllocator)) {
2172 bitmap.eraseColor(0xff55aa33);
2173 bitmap.erase(0xffaa3355, SkIRect::MakeXYWH(1, 1, 1, 1));
2174 canvas->scale(16, 16);
2175 canvas->drawBitmap(bitmap, 0, 0);
2176 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002177}
2178##
2179
2180#SeeAlso allocPixels Allocator Pixel_Ref
2181
2182##
2183
2184# ------------------------------------------------------------------------------
2185
2186#Method SkPixelRef* pixelRef() const
2187
2188Returns Pixel_Ref, which contains: pixel base address; its dimensions; and
2189rowBytes, the interval from one row to the next. Does not change Pixel_Ref
2190reference count. Pixel_Ref may be shared by multiple bitmaps.
2191If Pixel_Ref has not been set, returns nullptr.
2192
2193#Return Pixel_Ref, or nullptr ##
2194
2195#Example
2196#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002197 SkBitmap subset;
2198 source.extractSubset(&subset, SkIRect::MakeXYWH(32, 64, 128, 256));
2199 SkDebugf("src ref %c= sub ref\n", source.pixelRef() == subset.pixelRef() ? '=' : '!');
2200 SkDebugf("src pixels %c= sub pixels\n", source.getPixels() == subset.getPixels() ? '=' : '!');
2201 SkDebugf("src addr %c= sub addr\n", source.getAddr(32, 64) == subset.getAddr(0, 0) ? '=' : '!');
Cary Clarkbc5697d2017-10-04 14:31:33 -04002202##
2203
2204#SeeAlso getPixels getAddr
2205
2206##
2207
2208# ------------------------------------------------------------------------------
2209
2210#Method SkIPoint pixelRefOrigin() const
2211
2212Returns origin of pixels within Pixel_Ref. Bitmap bounds is always contained
2213by Pixel_Ref bounds, which may be the same size or larger. Multiple Bitmaps
2214can share the same Pixel_Ref, where each Bitmap has different bounds.
2215
2216The returned origin added to Bitmap dimensions equals or is smaller than the
2217Pixel_Ref dimensions.
2218
2219Returns (0, 0) if Pixel_Ref is nullptr.
2220
2221#Return pixel origin within Pixel_Ref ##
2222
2223#Example
2224#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002225 SkBitmap subset;
2226 source.extractSubset(&subset, SkIRect::MakeXYWH(32, 64, 128, 256));
2227 SkIPoint sourceOrigin = source.pixelRefOrigin();
2228 SkIPoint subsetOrigin = subset.pixelRefOrigin();
2229 SkDebugf("source origin: %d, %d\n", sourceOrigin.fX, sourceOrigin.fY);
2230 SkDebugf("subset origin: %d, %d\n", subsetOrigin.fX, subsetOrigin.fY);
2231#StdOut
2232source origin: 0, 0
2233subset origin: 32, 64
2234##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002235##
2236
Cary Clark2ade9972017-11-02 17:49:34 -04002237#SeeAlso SkPixelRef getSubset setPixelRef
Cary Clarkbc5697d2017-10-04 14:31:33 -04002238
2239##
2240
2241# ------------------------------------------------------------------------------
2242
2243#Method void setPixelRef(sk_sp<SkPixelRef> pixelRef, int dx, int dy)
2244
2245Replaces pixelRef and origin in Bitmap. dx and dy specify the offset
2246within the Pixel_Ref pixels for the top-left corner of the bitmap.
2247
2248Asserts in debug builds if dx or dy are out of range. Pins dx and dy
2249to legal range in release builds.
2250
2251The caller is responsible for ensuring that the pixels match the
2252Color_Type and Alpha_Type in Image_Info.
2253
2254#Param pixelRef Pixel_Ref describing pixel address and rowBytes ##
2255#Param dx column offset in Pixel_Ref for bitmap origin ##
2256#Param dy row offset in Pixel_Ref for bitmap origin ##
2257
2258#Example
2259#Height 140
2260#Image 5
2261#Description
2262Treating 32 bit data as 8 bit data is unlikely to produce useful results.
2263##
Ben Wagner29380bd2017-10-09 14:43:00 -04002264 SkBitmap bitmap;
2265 bitmap.setInfo(SkImageInfo::Make(source.width() - 5, source.height() - 5,
2266 kGray_8_SkColorType, kOpaque_SkAlphaType), source.rowBytes());
2267 bitmap.setPixelRef(sk_ref_sp(source.pixelRef()), 5, 5);
2268 canvas->drawBitmap(bitmap, 10, 10);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002269##
2270
2271#SeeAlso setInfo
2272
2273##
2274
2275# ------------------------------------------------------------------------------
2276
2277#Method bool readyToDraw() const
2278
2279Returns true if Bitmap is can be drawn.
2280
2281#Return true if getPixels() is not nullptr ##
2282
2283#Example
2284#Image 5
2285#Height 160
Ben Wagner29380bd2017-10-09 14:43:00 -04002286 if (source.readyToDraw()) {
2287 canvas->drawBitmap(source, 10, 10);
2288 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002289##
2290
2291#SeeAlso getPixels drawsNothing
2292
2293##
2294
2295# ------------------------------------------------------------------------------
2296
2297#Method uint32_t getGenerationID() const
2298
2299Returns a unique value corresponding to the pixels in Pixel_Ref.
2300Returns a different value after notifyPixelsChanged has been called.
2301Returns zero if Pixel_Ref is nullptr.
2302
2303Determines if pixels have changed since last examined.
2304
2305#Return unique value for pixels in Pixel_Ref ##
2306
2307#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04002308 SkBitmap bitmap;
2309 SkDebugf("empty id %u\n", bitmap.getGenerationID());
2310 bitmap.allocPixels(SkImageInfo::MakeN32(64, 64, kOpaque_SkAlphaType));
2311 SkDebugf("alloc id %u\n", bitmap.getGenerationID());
2312 bitmap.eraseColor(SK_ColorRED);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002313 SkDebugf("erase id %u\n", bitmap.getGenerationID());
2314#StdOut
2315#Volatile
Ben Wagner29380bd2017-10-09 14:43:00 -04002316empty id 0
2317alloc id 4
Cary Clarkbc5697d2017-10-04 14:31:33 -04002318erase id 6
2319##
2320##
2321
2322#SeeAlso notifyPixelsChanged Pixel_Ref
2323
2324##
2325
2326# ------------------------------------------------------------------------------
2327
2328#Method void notifyPixelsChanged() const
2329
2330Marks that pixels in Pixel_Ref have changed. Subsequent calls to
2331getGenerationID() return a different value.
2332
2333#Example
2334#Height 20
Ben Wagner29380bd2017-10-09 14:43:00 -04002335 SkBitmap bitmap;
2336 bitmap.setInfo(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kOpaque_SkAlphaType));
2337 bitmap.allocPixels();
2338 bitmap.eraseColor(SK_ColorRED);
2339 canvas->scale(16, 16);
2340 canvas->drawBitmap(bitmap, 0, 0);
2341 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorBLUE);
2342 canvas->drawBitmap(bitmap, 2, 0);
2343 bitmap.notifyPixelsChanged();
2344 *(SkPMColor*) bitmap.getPixels() = SkPreMultiplyColor(SK_ColorGREEN);
2345 canvas->drawBitmap(bitmap, 4, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002346##
2347
2348#SeeAlso getGenerationID isVolatile Pixel_Ref
2349
2350##
2351
2352# ------------------------------------------------------------------------------
2353
2354#Method void eraseColor(SkColor c) const
2355
2356Replaces pixel values with c. All pixels contained by bounds() are affected.
2357If the colorType is kGray_8_SkColorType or k565_SkColorType, then Color_Alpha
2358is ignored; Color_RGB is treated as opaque. If colorType is kAlpha_8_SkColorType,
2359then Color_RGB is ignored.
2360
2361#Param c Unpremultiplied Color ##
2362
2363#Example
2364#Height 20
Ben Wagner29380bd2017-10-09 14:43:00 -04002365 SkBitmap bitmap;
2366 bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kOpaque_SkAlphaType));
2367 bitmap.eraseColor(SK_ColorRED);
2368 canvas->scale(16, 16);
2369 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002370##
2371
Cary Clark154beea2017-10-26 07:58:48 -04002372#SeeAlso eraseARGB erase
Cary Clarkbc5697d2017-10-04 14:31:33 -04002373
2374##
2375
2376# ------------------------------------------------------------------------------
2377
2378#Method void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const
2379
2380Replaces pixel values with Unpremultiplied Color built from a, r, g, and b.
2381All pixels contained by bounds() are affected.
2382If the colorType is kGray_8_SkColorType or k565_SkColorType, then a
2383is ignored; r, g, and b are treated as opaque. If colorType is kAlpha_8_SkColorType,
2384then r, g, and b are ignored.
2385
2386#Param a amount of Color_Alpha, from fully transparent (0) to fully opaque (255) ##
2387#Param r amount of Color_RGB_Red, from no red (0) to full red (255) ##
2388#Param g amount of Color_RGB_Green, from no green (0) to full green (255) ##
2389#Param b amount of Color_RGB_Blue, from no blue (0) to full blue (255) ##
2390
2391#Example
2392#Height 80
Ben Wagner29380bd2017-10-09 14:43:00 -04002393 SkBitmap bitmap;
2394 bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType));
2395 bitmap.eraseARGB(0x7f, 0xff, 0x7f, 0x3f);
2396 canvas->scale(50, 50);
2397 canvas->drawBitmap(bitmap, 0, 0);
2398 canvas->drawBitmap(bitmap, .5f, .5f);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002399##
2400
Cary Clark154beea2017-10-26 07:58:48 -04002401#SeeAlso eraseColor erase
Cary Clarkbc5697d2017-10-04 14:31:33 -04002402
2403##
2404
2405# ------------------------------------------------------------------------------
2406
2407#Method void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const
2408
Cary Clark154beea2017-10-26 07:58:48 -04002409Deprecated. Use eraseARGB or eraseColor.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002410
Cary Clark154beea2017-10-26 07:58:48 -04002411#Param r amount of red ##
2412#Param g amount of green ##
2413#Param b amount of blue ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002414
Cary Clark154beea2017-10-26 07:58:48 -04002415#NoExample
Cary Clarkbc5697d2017-10-04 14:31:33 -04002416##
2417
2418#SeeAlso eraseColor eraseARGB erase
2419
2420##
2421
2422# ------------------------------------------------------------------------------
2423
2424#Method void erase(SkColor c, const SkIRect& area) const
2425
2426Replaces pixel values inside area with c. If area does not intersect bounds(),
2427call has no effect.
2428
2429If the colorType is kGray_8_SkColorType or k565_SkColorType, then Color_Alpha
2430is ignored; Color_RGB is treated as opaque. If colorType is kAlpha_8_SkColorType,
2431then Color_RGB is ignored.
2432
2433#Param c Unpremultiplied Color ##
2434#Param area rectangle to fill ##
2435
2436#Example
2437#Height 70
Ben Wagner29380bd2017-10-09 14:43:00 -04002438 SkBitmap bitmap;
2439 bitmap.allocPixels(SkImageInfo::MakeN32(2, 2, kPremul_SkAlphaType));
2440 bitmap.erase(0x7fff7f3f, SkIRect::MakeWH(1, 1));
2441 bitmap.erase(0x7f7f3fff, SkIRect::MakeXYWH(0, 1, 1, 1));
2442 bitmap.erase(0x7f3fff7f, SkIRect::MakeXYWH(1, 0, 1, 1));
2443 bitmap.erase(0x7f1fbf5f, SkIRect::MakeXYWH(1, 1, 1, 1));
2444 canvas->scale(25, 25);
2445 canvas->drawBitmap(bitmap, 0, 0);
2446 canvas->drawBitmap(bitmap, .5f, .5f);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002447
2448##
2449
2450#SeeAlso eraseColor eraseARGB eraseRGB SkCanvas::drawRect
2451
2452##
2453
2454# ------------------------------------------------------------------------------
2455
2456#Method void eraseArea(const SkIRect& area, SkColor c) const
2457
2458Legacy call to be deprecated.
2459
2460#Deprecated
2461##
2462
2463##
2464
2465# ------------------------------------------------------------------------------
2466
2467#Method SkColor getColor(int x, int y) const
2468
2469Returns pixel at (x, y) as Unpremultiplied Color.
2470Returns black with Alpha if Color_Type is kAlpha_8_SkColorType.
2471
2472Input is not validated: out of bounds values of x or y trigger an assert() if
2473built with SK_DEBUG defined; and returns undefined values or may crash if
2474SK_RELEASE is defined. Fails if Color_Type is kUnknown_SkColorType or
2475pixel address is nullptr.
2476
2477Color_Space in Image_Info is ignored. Some Color precision may be lost in the
2478conversion to Unpremultiplied Color; original pixel data may have additional
2479precision.
2480
2481#Param x column index, zero or greater, and less than width() ##
2482#Param y row index, zero or greater, and less than height() ##
2483
2484#Return pixel converted to Unpremultiplied Color ##
2485
2486#Example
2487 const int w = 4;
2488 const int h = 4;
2489 SkColor colors[][w] = {
2490 0x00000000, 0x2a0e002a, 0x55380055, 0x7f7f007f,
2491 0x2a000e2a, 0x551c1c55, 0x7f542a7f, 0xaaaa38aa,
2492 0x55003855, 0x7f2a547f, 0xaa7171aa, 0xd4d48dd4,
2493 0x7f007f7f, 0xaa38aaaa, 0xd48dd4d4, 0xffffffff,
2494 };
2495 SkDebugf("Premultiplied:\n");
2496 for (int y = 0; y < h; ++y) {
2497 SkDebugf("(0, %d) ", y);
2498 for (int x = 0; x < w; ++x) {
2499 SkDebugf("0x%08x%c", colors[y][x], x == w - 1 ? '\n' : ' ');
2500 }
2501 }
2502 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), colors, w * 4);
2503 SkBitmap bitmap;
2504 bitmap.installPixels(pixmap);
2505 SkDebugf("Unpremultiplied:\n");
2506 for (int y = 0; y < h; ++y) {
2507 SkDebugf("(0, %d) ", y);
2508 for (int x = 0; x < w; ++x) {
2509 SkDebugf("0x%08x%c", bitmap.getColor(x, y), x == w - 1 ? '\n' : ' ');
2510 }
2511 }
2512#StdOut
2513Premultiplied:
2514(0, 0) 0x00000000 0x2a0e002a 0x55380055 0x7f7f007f
2515(0, 1) 0x2a000e2a 0x551c1c55 0x7f542a7f 0xaaaa38aa
2516(0, 2) 0x55003855 0x7f2a547f 0xaa7171aa 0xd4d48dd4
2517(0, 3) 0x7f007f7f 0xaa38aaaa 0xd48dd4d4 0xffffffff
2518Unpremultiplied:
2519(0, 0) 0x00000000 0x2a5500ff 0x55a800ff 0x7fff00ff
2520(0, 1) 0x2a0055ff 0x555454ff 0x7fa954ff 0xaaff54ff
2521(0, 2) 0x5500a8ff 0x7f54a9ff 0xaaaaaaff 0xd4ffaaff
2522(0, 3) 0x7f00ffff 0xaa54ffff 0xd4aaffff 0xffffffff
2523##
2524##
2525
2526#SeeAlso getAddr readPixels
2527
2528##
2529
2530# ------------------------------------------------------------------------------
2531
2532#Method void* getAddr(int x, int y) const
2533
2534Returns pixel address at (x, y).
2535
2536Input is not validated: out of bounds values of x or y, or kUnknown_SkColorType,
2537trigger an assert() if built with SK_DEBUG defined. Returns nullptr if
2538Color_Type is kUnknown_SkColorType, or Pixel_Ref is nullptr.
2539
2540Performs a lookup of pixel size; for better performance, call
2541one of: getAddr8, getAddr16, or getAddr32.
2542
2543#Param x column index, zero or greater, and less than width() ##
2544#Param y row index, zero or greater, and less than height() ##
2545
2546#Return generic pointer to pixel ##
2547
2548#Example
2549#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002550 char* row0 = (char* ) source.getAddr(0, 0);
2551 char* row1 = (char* ) source.getAddr(0, 1);
2552 SkDebugf("addr interval %c= rowBytes\n", row1 - row0 == source.rowBytes() ? '=' : '!');
2553#StdOut
2554addr interval == rowBytes
2555##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002556##
2557
2558#SeeAlso getAddr8 getAddr16 getAddr32 readPixels SkPixmap::addr
2559
2560##
2561
2562# ------------------------------------------------------------------------------
2563
2564#Method inline uint32_t* getAddr32(int x, int y) const
2565
2566Returns address at (x, y).
2567
2568Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2569#List
2570# Pixel_Ref is nullptr ##
2571# bytesPerPixel() is not four ##
2572# x is negative, or not less than width() ##
2573# y is negative, or not less than height() ##
2574##
2575
2576#Param x column index, zero or greater, and less than width() ##
2577#Param y row index, zero or greater, and less than height() ##
2578
2579#Return unsigned 32-bit pointer to pixel at (x, y) ##
2580
2581#Example
2582#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002583 uint32_t* row0 = source.getAddr32(0, 0);
2584 uint32_t* row1 = source.getAddr32(0, 1);
2585 size_t interval = (row1 - row0) * source.bytesPerPixel();
2586 SkDebugf("addr interval %c= rowBytes\n", interval == source.rowBytes() ? '=' : '!');
2587#StdOut
2588addr interval == rowBytes
2589##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002590##
2591
2592#SeeAlso getAddr8 getAddr16 getAddr readPixels SkPixmap::addr32
2593
2594##
2595
2596# ------------------------------------------------------------------------------
2597
2598#Method inline uint16_t* getAddr16(int x, int y) const
2599
2600Returns address at (x, y).
2601
2602Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2603#List
2604# Pixel_Ref is nullptr ##
2605# bytesPerPixel() is not two ##
2606# x is negative, or not less than width() ##
2607# y is negative, or not less than height() ##
2608##
2609
2610#Param x column index, zero or greater, and less than width() ##
2611#Param y row index, zero or greater, and less than height() ##
2612
2613#Return unsigned 16-bit pointer to pixel at (x, y)##
2614
2615#Example
2616#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002617 SkBitmap bitmap16;
2618 SkImageInfo dstInfo = SkImageInfo::Make(source.width(), source.height(), kARGB_4444_SkColorType,
2619 kPremul_SkAlphaType);
2620 bitmap16.allocPixels(dstInfo);
2621 if (source.readPixels(dstInfo, bitmap16.getPixels(), bitmap16.rowBytes(), 0, 0)) {
2622 uint16_t* row0 = bitmap16.getAddr16(0, 0);
2623 uint16_t* row1 = bitmap16.getAddr16(0, 1);
2624 size_t interval = (row1 - row0) * bitmap16.bytesPerPixel();
2625 SkDebugf("addr interval %c= rowBytes\n", interval == bitmap16.rowBytes() ? '=' : '!');
2626 }
2627#StdOut
2628addr interval == rowBytes
2629##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002630##
2631
2632#SeeAlso getAddr8 getAddr getAddr32 readPixels SkPixmap::addr16
2633
2634##
2635
2636# ------------------------------------------------------------------------------
2637
2638#Method inline uint8_t* getAddr8(int x, int y) const
2639
2640Returns address at (x, y).
2641
2642Input is not validated. Triggers an assert() if built with SK_DEBUG defined and:
2643#List
2644# Pixel_Ref is nullptr ##
2645# bytesPerPixel() is not one ##
2646# x is negative, or not less than width() ##
2647# y is negative, or not less than height() ##
2648##
2649
2650#Param x column index, zero or greater, and less than width() ##
2651#Param y row index, zero or greater, and less than height() ##
2652
2653#Return unsigned 8-bit pointer to pixel at (x, y) ##
2654
2655#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04002656 SkBitmap bitmap;
2657 const int width = 8;
2658 const int height = 8;
2659 uint8_t pixels[height][width];
2660 SkImageInfo info = SkImageInfo::Make(width, height, kGray_8_SkColorType, kOpaque_SkAlphaType);
2661 if (bitmap.installPixels(info, pixels, info.minRowBytes())) {
2662 SkDebugf("&pixels[4][2] %c= bitmap.getAddr8(2, 4)\n",
2663 &pixels[4][2] == bitmap.getAddr8(2, 4) ? '=' : '!');
2664 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002665#StdOut
2666&pixels[4][2] == bitmap.getAddr8(2, 4)
2667##
2668##
2669
2670#SeeAlso getAddr getAddr16 getAddr32 readPixels SkPixmap::addr8
2671
2672##
2673
2674# ------------------------------------------------------------------------------
2675
2676#Method bool extractSubset(SkBitmap* dst, const SkIRect& subset) const
2677
2678Shares Pixel_Ref with dst. Pixels are not copied; Bitmap and dst point
2679to the same pixels; dst bounds() are set to the intersection of subset
2680and the original bounds().
2681
2682subset may be larger than bounds(). Any area outside of bounds() is ignored.
2683
2684Any contents of dst are discarded. isVolatile setting is copied to dst.
2685dst is set to colorType, alphaType, and colorSpace.
2686
2687Return false if:
2688#List
2689# dst is nullptr ##
2690# Pixel_Ref is nullptr ##
2691# subset does not intersect bounds() ##
2692##
2693
2694
2695#Param dst Bitmap set to subset ##
2696#Param subset rectangle of pixels to reference ##
2697
2698#Return true if dst is replaced by subset
2699##
2700
2701#Example
2702#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002703 SkIRect bounds, s;
2704 source.getBounds(&bounds);
2705 SkDebugf("bounds: %d, %d, %d, %d\n", bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
2706 SkBitmap subset;
2707 for (int left: { -100, 0, 100, 1000 } ) {
2708 for (int right: { 0, 100, 1000 } ) {
2709 SkIRect b = SkIRect::MakeLTRB(left, 100, right, 200);
2710 bool success = source.extractSubset(&subset, b);
2711 SkDebugf("subset: %4d, %4d, %4d, %4d ", b.fLeft, b.fTop, b.fRight, b.fBottom);
2712 SkDebugf("success; %s", success ? "true" : "false");
2713 if (success) {
2714 subset.getBounds(&s);
2715 SkDebugf(" subset: %d, %d, %d, %d", s.fLeft, s.fTop, s.fRight, s.fBottom);
2716 }
2717 SkDebugf("\n");
2718 }
2719 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002720#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -04002721bounds: 0, 0, 512, 512
2722subset: -100, 100, 0, 200 success; false
2723subset: -100, 100, 100, 200 success; true subset: 0, 0, 100, 100
2724subset: -100, 100, 1000, 200 success; true subset: 0, 0, 512, 100
2725subset: 0, 100, 0, 200 success; false
2726subset: 0, 100, 100, 200 success; true subset: 0, 0, 100, 100
2727subset: 0, 100, 1000, 200 success; true subset: 0, 0, 512, 100
2728subset: 100, 100, 0, 200 success; false
2729subset: 100, 100, 100, 200 success; false
2730subset: 100, 100, 1000, 200 success; true subset: 0, 0, 412, 100
2731subset: 1000, 100, 0, 200 success; false
2732subset: 1000, 100, 100, 200 success; false
Cary Clarkbc5697d2017-10-04 14:31:33 -04002733subset: 1000, 100, 1000, 200 success; false
2734##
2735##
2736
2737#SeeAlso readPixels writePixels SkCanvas::drawBitmap
2738
2739##
2740
2741# ------------------------------------------------------------------------------
2742
2743#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
2744 int srcX, int srcY, SkTransferFunctionBehavior behavior) const
2745
2746Copies a Rect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not exceed
2747(this->width(), this->height()).
2748
2749dstInfo specifies width, height, Color_Type, Alpha_Type, and
2750Color_Space of destination. dstRowBytes specifics the gap from one destination
2751row to the next. Returns true if pixels are copied. Returns false if:
2752#List
2753# dstInfo.addr() equals nullptr ##
2754# dstRowBytes is less than dstInfo.minRowBytes ##
2755# Pixel_Ref is nullptr ##
2756##
2757
2758Pixels are copied only if pixel conversion is possible. If this->colorType is
2759kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
2760If this->colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
2761If this->alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
2762match. If this->colorSpace is nullptr, dstInfo.colorSpace must match. Returns
2763false if pixel conversion is not possible.
2764
2765srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04002766false if width() or height() is zero or negative.
2767Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04002768#Formula
2769abs(srcX) >= this->width()
2770##
2771, or if
2772#Formula
2773abs(srcY) >= this->height()
2774##
2775.
2776
2777If behavior is SkTransferFunctionBehavior::kRespect: converts source
2778pixels to a linear space before converting to dstInfo.
2779If behavior is SkTransferFunctionBehavior::kIgnore: source
2780pixels are treated as if they are linear, regardless of how they are encoded.
2781
2782#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
2783#Param dstPixels destination pixel storage ##
2784#Param dstRowBytes destination row length ##
2785#Param srcX column index whose absolute value is less than width() ##
2786#Param srcY row index whose absolute value is less than height() ##
2787#Param behavior one of: SkTransferFunctionBehavior::kRespect,
2788 SkTransferFunctionBehavior::kIgnore
2789##
2790
2791#Return true if pixels are copied to dstPixels ##
2792
2793#Example
2794#Height 64
2795void draw(SkCanvas* canvas) {
Ben Wagner29380bd2017-10-09 14:43:00 -04002796 const int width = 256;
2797 const int height = 32;
2798 std::vector<int32_t> dstPixels;
2799 dstPixels.resize(height * width * 4);
2800 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
2801 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
2802 SkPoint gradPoints[] = { { 0, 0 }, { width, 0 } };
2803 SkPaint gradPaint;
2804 gradPaint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
2805 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
2806 for (auto behavior : { SkTransferFunctionBehavior::kRespect,
2807 SkTransferFunctionBehavior::kIgnore} ) {
2808 SkBitmap bitmap;
2809 bitmap.allocPixels(info);
2810 SkCanvas srcCanvas(bitmap);
2811 srcCanvas.drawRect(SkRect::MakeWH(width, height), gradPaint);
2812 if (bitmap.readPixels(info, &dstPixels.front(), width * 4, 0, 0, behavior)) {
2813 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
2814 bitmap.installPixels(dstPixmap);
2815 canvas->drawBitmap(bitmap, 0, 0);
2816 }
2817 canvas->translate(0, height);
2818 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002819}
2820##
2821
2822#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2823
2824##
2825
2826# ------------------------------------------------------------------------------
2827
2828#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
2829 int srcX, int srcY) const
2830
2831Copies a Rect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not exceed
2832(this->width(), this->height()).
2833
2834dstInfo specifies width, height, Color_Type, Alpha_Type, and
2835Color_Space of destination. dstRowBytes specifics the gap from one destination
2836row to the next. Returns true if pixels are copied. Returns false if:
2837#List
2838# dstInfo.addr() equals nullptr ##
2839# dstRowBytes is less than dstInfo.minRowBytes ##
2840# Pixel_Ref is nullptr ##
2841##
2842
2843Pixels are copied only if pixel conversion is possible. If this->colorType is
2844kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
2845If this->colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
2846If this->alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
2847match. If this->colorSpace is nullptr, dstInfo.colorSpace must match. Returns
2848false if pixel conversion is not possible.
2849
2850srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04002851false if width() or height() is zero or negative.
2852Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04002853#Formula
2854abs(srcX) >= this->width()
2855##
2856, or if
2857#Formula
2858abs(srcY) >= this->height()
2859##
2860.
2861
2862#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
2863#Param dstPixels destination pixel storage ##
2864#Param dstRowBytes destination row length ##
2865#Param srcX column index whose absolute value is less than width() ##
2866#Param srcY row index whose absolute value is less than height() ##
2867
2868#Return true if pixels are copied to dstPixels ##
2869
2870#Example
2871#Height 128
2872#Description
2873Transferring the gradient from 8 bits per component to 4 bits per component
2874creates visible banding.
2875##
Ben Wagner29380bd2017-10-09 14:43:00 -04002876 const int width = 256;
2877 const int height = 64;
2878 SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(width, height);
2879 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
2880 SkPoint gradPoints[] = { { 0, 0 }, { 256, 0 } };
2881 SkPaint paint;
2882 paint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
2883 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
2884 SkBitmap bitmap;
2885 bitmap.allocPixels(srcInfo);
2886 SkCanvas srcCanvas(bitmap);
2887 srcCanvas.drawRect(SkRect::MakeWH(width, height), paint);
2888 canvas->drawBitmap(bitmap, 0, 0);
2889 SkImageInfo dstInfo = srcInfo.makeColorType(kARGB_4444_SkColorType);
2890 std::vector<int16_t> dstPixels;
2891 dstPixels.resize(height * width);
2892 bitmap.readPixels(dstInfo, &dstPixels.front(), width * 2, 0, 0);
2893 SkPixmap dstPixmap(dstInfo, &dstPixels.front(), width * 2);
2894 bitmap.installPixels(dstPixmap);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002895 canvas->drawBitmap(bitmap, 0, 64);
2896##
2897
2898#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2899
2900##
2901
2902# ------------------------------------------------------------------------------
2903
2904#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY) const
2905
2906Copies a Rect of pixels to dst. Copy starts at (srcX, srcY), and does not exceed
2907(this->width(), this->height()).
2908
2909dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2910and row bytes of destination. dst.rowBytes specifics the gap from one destination
2911row to the next. Returns true if pixels are copied. Returns false if:
2912#List
2913# dst pixel storage equals nullptr ##
2914# dst.rowBytes is less than SkImageInfo::minRowBytes ##
2915# Pixel_Ref is nullptr ##
2916##
2917
2918Pixels are copied only if pixel conversion is possible. If this->colorType is
2919kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
2920If this->colorType is kGray_8_SkColorType, dst Color_Space must match.
2921If this->alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
2922match. If this->colorSpace is nullptr, dst Color_Space must match. Returns
2923false if pixel conversion is not possible.
2924
2925srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04002926false if width() or height() is zero or negative.
2927Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04002928#Formula
2929abs(srcX) >= this->width()
2930##
2931, or if
2932#Formula
2933abs(srcY) >= this->height()
2934##
2935.
2936
2937#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
2938#Param srcX column index whose absolute value is less than width() ##
2939#Param srcY row index whose absolute value is less than height() ##
2940
2941#Return true if pixels are copied to dst ##
2942
2943#Example
2944#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002945 std::vector<int32_t> srcPixels;
2946 srcPixels.resize(source.height() * source.rowBytes());
2947 for (int y = 0; y < 4; ++y) {
2948 for (int x = 0; x < 4; ++x) {
2949 SkPixmap pixmap(SkImageInfo::MakeN32Premul(source.width() / 4, source.height() / 4),
2950 &srcPixels.front() + x * source.height() * source.width() / 4 +
2951 y * source.width() / 4, source.rowBytes());
2952 source.readPixels(pixmap, x * source.width() / 4, y * source.height() / 4);
2953 }
2954 }
2955 canvas->scale(.5f, .5f);
2956 SkBitmap bitmap;
2957 bitmap.installPixels(SkImageInfo::MakeN32Premul(source.width(), source.height()),
2958 &srcPixels.front(), source.rowBytes());
Cary Clarkbc5697d2017-10-04 14:31:33 -04002959 canvas->drawBitmap(bitmap, 0, 0);
2960##
2961
2962#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
2963
2964##
2965
2966# ------------------------------------------------------------------------------
2967
2968#Method bool readPixels(const SkPixmap& dst) const
2969
2970Copies a Rect of pixels to dst. Copy starts at (0, 0), and does not exceed
2971(this->width(), this->height()).
2972
2973dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
2974and row bytes of destination. dst.rowBytes specifics the gap from one destination
2975row to the next. Returns true if pixels are copied. Returns false if:
2976#List
2977# dst pixel storage equals nullptr ##
2978# dst.rowBytes is less than SkImageInfo::minRowBytes ##
2979# Pixel_Ref is nullptr ##
2980##
2981
2982Pixels are copied only if pixel conversion is possible. If this->colorType is
2983kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
2984If this->colorType is kGray_8_SkColorType, dst Color_Space must match.
2985If this->alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
2986match. If this->colorSpace is nullptr, dst Color_Space must match. Returns
2987false if pixel conversion is not possible.
2988
2989#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
2990
2991#Return true if pixels are copied to dst ##
2992
2993#Example
2994#Height 128
2995#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04002996 std::vector<int32_t> srcPixels;
2997 srcPixels.resize(source.height() * source.width() * 8);
2998 for (int i = 0; i < 2; ++i) {
2999 SkPixmap pixmap(SkImageInfo::Make(source.width() * 2, source.height(),
3000 i ? kRGBA_8888_SkColorType : kBGRA_8888_SkColorType, kPremul_SkAlphaType),
3001 &srcPixels.front() + i * source.width(), source.rowBytes() * 2);
3002 source.readPixels(pixmap);
3003 }
3004 canvas->scale(.25f, .25f);
3005 SkBitmap bitmap;
3006 bitmap.installPixels(SkImageInfo::MakeN32Premul(source.width() * 2, source.height()),
3007 &srcPixels.front(), source.rowBytes() * 2);
3008 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003009##
3010
3011#SeeAlso writePixels SkPixmap::readPixels SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
3012
3013##
3014
3015# ------------------------------------------------------------------------------
3016
3017#Method bool writePixels(const SkPixmap& src, int dstX, int dstY)
3018
3019Copies a Rect of pixels from src. Copy starts at (dstX, dstY), and does not exceed
3020(src.width(), src.height()).
3021
3022src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
3023and row bytes of source. src.rowBytes specifics the gap from one source
3024row to the next. Returns true if pixels are copied. Returns false if:
3025#List
3026# src pixel storage equals nullptr ##
3027# src.rowBytes is less than SkImageInfo::minRowBytes ##
3028# Pixel_Ref is nullptr ##
3029##
3030
3031Pixels are copied only if pixel conversion is possible. If this->colorType is
3032kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
3033If this->colorType is kGray_8_SkColorType, src Color_Space must match.
3034If this->alphaType is kOpaque_SkAlphaType, src Alpha_Type must
3035match. If this->colorSpace is nullptr, src Color_Space must match. Returns
3036false if pixel conversion is not possible.
3037
3038dstX and dstY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04003039false if width() or height() is zero or negative.
3040Returns false if
Cary Clarkbc5697d2017-10-04 14:31:33 -04003041#Formula
3042abs(dstX) >= this->width()
3043##
3044, or if
3045#Formula
3046abs(dstY) >= this->height()
3047##
3048.
3049
3050#Param src source Pixmap: Image_Info, pixels, row bytes ##
3051#Param dstX column index whose absolute value is less than width() ##
3052#Param dstY row index whose absolute value is less than height() ##
3053
3054#Return true if src pixels are copied to Bitmap ##
3055
3056#Example
3057#Image 3
Ben Wagner29380bd2017-10-09 14:43:00 -04003058 std::vector<int32_t> srcPixels;
3059 int width = image->width();
3060 int height = image->height();
3061 srcPixels.resize(height * width * 4);
3062 SkPixmap pixmap(SkImageInfo::MakeN32Premul(width, height), (const void*) &srcPixels.front(),
3063 width * 4);
3064 image->readPixels(pixmap, 0, 0);
3065 canvas->scale(.5f, .5f);
3066 width /= 4;
3067 height /= 4;
3068 for (int y = 0; y < 4; ++y) {
3069 for (int x = 0; x < 4; ++x) {
3070 SkBitmap bitmap;
3071 bitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height));
3072 bitmap.writePixels(pixmap, -y * width, -x * height);
3073 canvas->drawBitmap(bitmap, x * width, y * height);
3074 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003075 }
3076##
3077
3078#SeeAlso readPixels
3079
3080##
3081
3082# ------------------------------------------------------------------------------
3083
3084#Method bool writePixels(const SkPixmap& src)
3085
3086Copies a Rect of pixels from src. Copy starts at (0, 0), and does not exceed
3087(src.width(), src.height()).
3088
3089src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
3090and row bytes of source. src.rowBytes specifics the gap from one source
3091row to the next. Returns true if pixels are copied. Returns false if:
3092#List
3093# src pixel storage equals nullptr ##
3094# src.rowBytes is less than SkImageInfo::minRowBytes ##
3095# Pixel_Ref is nullptr ##
3096##
3097
3098Pixels are copied only if pixel conversion is possible. If this->colorType is
3099kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
3100If this->colorType is kGray_8_SkColorType, src Color_Space must match.
3101If this->alphaType is kOpaque_SkAlphaType, src Alpha_Type must
3102match. If this->colorSpace is nullptr, src Color_Space must match. Returns
3103false if pixel conversion is not possible.
3104
3105#Param src source Pixmap: Image_Info, pixels, row bytes ##
3106
3107#Return true if src pixels are copied to Bitmap ##
3108
3109#Example
3110#Height 80
Ben Wagner29380bd2017-10-09 14:43:00 -04003111 SkBitmap bitmap;
3112 bitmap.allocPixels(SkImageInfo::MakeN32Premul(2, 2));
3113 bitmap.eraseColor(SK_ColorGREEN);
3114 SkPMColor color = 0xFF5599BB;
3115 SkPixmap src(SkImageInfo::MakeN32Premul(1, 1), &color, 4);
3116 bitmap.writePixels(src);
3117 canvas->scale(40, 40);
3118 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003119##
3120
3121#SeeAlso readPixels
3122
3123##
3124
3125# ------------------------------------------------------------------------------
3126
3127#Method bool writePixels(const SkPixmap& src, int x, int y, SkTransferFunctionBehavior behavior)
3128
3129Copies a Rect of pixels from src. Copy starts at (0, 0), and does not exceed
3130(src.width(), src.height()).
3131
3132src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
3133and row bytes of source. src.rowBytes specifics the gap from one source
3134row to the next. Returns true if pixels are copied. Returns false if:
3135#List
3136# src pixel storage equals nullptr ##
3137# src.rowBytes is less than SkImageInfo::minRowBytes ##
3138# Pixel_Ref is nullptr ##
3139##
3140
3141Pixels are copied only if pixel conversion is possible. If this->colorType is
3142kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match.
3143If this->colorType is kGray_8_SkColorType, src Color_Space must match.
3144If this->alphaType is kOpaque_SkAlphaType, src Alpha_Type must
3145match. If this->colorSpace is nullptr, src Color_Space must match. Returns
3146false if pixel conversion is not possible. Returns false if width() or height()
3147is zero or negative.
3148
3149If behavior is SkTransferFunctionBehavior::kRespect: converts src
3150pixels to a linear space before converting to Image_Info.
3151If behavior is SkTransferFunctionBehavior::kIgnore: src
3152pixels are treated as if they are linear, regardless of how they are encoded.
3153
3154#Param src source Pixmap: Image_Info, pixels, row bytes ##
3155#Param x column index whose absolute value is less than width() ##
3156#Param y row index whose absolute value is less than height() ##
3157#Param behavior one of: SkTransferFunctionBehavior::kRespect,
3158 SkTransferFunctionBehavior::kIgnore
3159##
3160
3161#Return true if src pixels are copied to Bitmap ##
3162
3163#Example
3164#Height 64
Ben Wagner29380bd2017-10-09 14:43:00 -04003165 const int width = 256;
3166 const int height = 32;
3167 std::vector<int32_t> dstPixels;
3168 dstPixels.resize(height * width * 4);
3169 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
3170 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
3171 SkPoint gradPoints[] = { { 0, 0 }, { width, 0 } };
3172 SkPaint gradPaint;
3173 gradPaint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
3174 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
3175 for (auto behavior : { SkTransferFunctionBehavior::kRespect,
3176 SkTransferFunctionBehavior::kIgnore} ) {
3177 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
3178 SkBitmap bitmap;
3179 bitmap.installPixels(dstPixmap);
3180 SkCanvas srcCanvas(bitmap);
3181 srcCanvas.drawRect(SkRect::MakeWH(width, height), gradPaint);
3182 if (bitmap.writePixels(dstPixmap, 0, 0, behavior)) {
3183 canvas->drawBitmap(bitmap, 0, 0);
3184 }
3185 canvas->translate(0, height);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003186 }
3187##
3188
3189#SeeAlso readPixels
3190
3191##
3192
3193# ------------------------------------------------------------------------------
3194
3195#Method bool hasHardwareMipMap() const
3196
3197#Private
3198Android framework only.
3199##
3200
3201#Return true if setHasHardwareMipMap has been called with true ##
3202
3203#NoExample
3204##
3205
3206#SeeAlso setHasHardwareMipMap
3207
3208##
3209
3210# ------------------------------------------------------------------------------
3211
3212#Method void setHasHardwareMipMap(bool hasHardwareMipMap)
3213
3214#Private
3215Android framework only.
3216##
3217
3218#Param hasHardwareMipMap sets state ##
3219
3220#NoExample
3221##
3222
3223#SeeAlso hasHardwareMipMap
3224
3225##
3226
3227# ------------------------------------------------------------------------------
3228
3229#Method bool extractAlpha(SkBitmap* dst) const
3230
3231Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3232or dst pixels cannot be allocated.
3233
3234Uses HeapAllocator to reserve memory for dst Pixel_Ref.
3235
3236#Param dst holds Pixel_Ref to fill with alpha layer ##
3237
3238#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3239
3240#Example
3241#Height 100
Ben Wagner29380bd2017-10-09 14:43:00 -04003242 SkBitmap alpha, bitmap;
3243 bitmap.allocN32Pixels(100, 100);
3244 SkCanvas offscreen(bitmap);
3245 offscreen.clear(0);
3246 SkPaint paint;
3247 paint.setAntiAlias(true);
3248 paint.setColor(SK_ColorBLUE);
3249 paint.setStyle(SkPaint::kStroke_Style);
3250 paint.setStrokeWidth(20);
3251 offscreen.drawCircle(50, 50, 39, paint);
3252 offscreen.flush();
3253 bitmap.extractAlpha(&alpha);
3254 paint.setColor(SK_ColorRED);
3255 canvas->drawBitmap(bitmap, 0, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003256 canvas->drawBitmap(alpha, 100, 0, &paint);
3257##
3258
3259#SeeAlso extractSubset
3260
3261##
3262
3263# ------------------------------------------------------------------------------
3264
3265#Method bool extractAlpha(SkBitmap* dst, const SkPaint* paint,
3266 SkIPoint* offset) const
3267
3268Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3269or dst pixels cannot be allocated.
3270
3271If paint is not nullptr and contains Mask_Filter, SkMaskFilter::filterMask
3272generates Mask_Alpha from Bitmap. Uses HeapAllocator to reserve memory for dst
3273Pixel_Ref. Sets offset to top-left position for dst for alignment with Bitmap;
3274(0, 0) unless SkMaskFilter generates mask.
3275
3276#Param dst holds Pixel_Ref to fill with alpha layer ##
3277#Param paint holds optional Mask_Filter; may be nullptr ##
3278#Param offset top-left position for dst; may be nullptr ##
3279
3280#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3281
3282#Bug 7103 ##
3283#Example
3284#Height 160
Ben Wagner29380bd2017-10-09 14:43:00 -04003285 SkBitmap alpha, bitmap;
3286 bitmap.allocN32Pixels(100, 100);
3287 SkCanvas offscreen(bitmap);
3288 offscreen.clear(0);
3289 SkPaint paint;
3290 paint.setAntiAlias(true);
3291 paint.setColor(SK_ColorBLUE);
3292 paint.setStyle(SkPaint::kStroke_Style);
3293 paint.setStrokeWidth(20);
3294 offscreen.drawCircle(50, 50, 39, paint);
3295 offscreen.flush();
3296 const SkScalar kBlurSigma = SkBlurMaskFilter::ConvertRadiusToSigma(SkIntToScalar(25));
3297 paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, kBlurSigma,
3298 SkBlurMaskFilter::kHighQuality_BlurFlag));
3299 SkIPoint offset;
3300 bitmap.extractAlpha(&alpha, &paint, &offset);
3301 paint.setColor(SK_ColorRED);
3302 canvas->drawBitmap(bitmap, 0, -offset.fY, &paint);
3303 canvas->drawBitmap(alpha, 100 + offset.fX, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003304##
3305
3306#SeeAlso extractSubset
3307
3308##
3309
3310# ------------------------------------------------------------------------------
3311
3312#Method bool extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator,
3313 SkIPoint* offset) const
3314
3315Sets dst to Alpha described by pixels. Returns false if dst cannot be written to
3316or dst pixels cannot be allocated.
3317
3318If paint is not nullptr and contains Mask_Filter, SkMaskFilter::filterMask
3319generates Mask_Alpha from Bitmap. allocator may reference a custom allocation
3320class or be set to nullptr to use HeapAllocator. Sets offset to top-left
3321position for dst for alignment with Bitmap; (0, 0) unless SkMaskFilter generates
3322mask.
3323
3324#Param dst holds Pixel_Ref to fill with alpha layer ##
3325#Param paint holds optional Mask_Filter; may be nullptr ##
3326#Param allocator method to reserve memory for Pixel_Ref; may be nullptr ##
3327#Param offset top-left position for dst; may be nullptr ##
3328
3329#Return true if Alpha layer was constructed in dst Pixel_Ref ##
3330
3331#Bug 7104 ##
3332#Example
3333#Height 128
Ben Wagner29380bd2017-10-09 14:43:00 -04003334 SkBitmap alpha, bitmap;
3335 bitmap.allocN32Pixels(100, 100);
3336 SkCanvas offscreen(bitmap);
3337 offscreen.clear(0);
3338 SkPaint paint;
3339 paint.setAntiAlias(true);
3340 paint.setColor(SK_ColorBLUE);
3341 paint.setStyle(SkPaint::kStroke_Style);
3342 paint.setStrokeWidth(20);
3343 offscreen.drawCircle(50, 50, 39, paint);
3344 offscreen.flush();
Cary Clarkbc5697d2017-10-04 14:31:33 -04003345 paint.setMaskFilter(SkBlurMaskFilter::Make(kOuter_SkBlurStyle, 3));
Ben Wagner29380bd2017-10-09 14:43:00 -04003346 SkIPoint offset;
3347 bitmap.extractAlpha(&alpha, &paint, nullptr, &offset);
3348 paint.setColor(SK_ColorRED);
3349 canvas->drawBitmap(bitmap, 0, -offset.fY, &paint);
3350 canvas->drawBitmap(alpha, 100 + offset.fX, 0, &paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003351##
3352
3353#SeeAlso extractSubset
3354
3355##
3356
3357# ------------------------------------------------------------------------------
3358
3359#Method bool peekPixels(SkPixmap* pixmap) const
3360
Cary Clark154beea2017-10-26 07:58:48 -04003361Copies Bitmap pixel address, row bytes, and Image_Info to pixmap, if address
3362is available, and returns true. If pixel address is not available, return
3363false and leave pixmap unchanged.
3364
3365pixmap contents become invalid on any future change to Bitmap.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003366
3367#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
3368
3369#Return true if Bitmap has direct access to pixels ##
3370
3371#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04003372 SkBitmap bitmap;
3373 bitmap.allocPixels(SkImageInfo::MakeN32Premul(6, 11));
3374 SkCanvas offscreen(bitmap);
3375 offscreen.clear(SK_ColorWHITE);
3376 SkPaint paint;
3377 offscreen.drawString("?", 0, 10, paint);
3378 SkPixmap pixmap;
3379 if (bitmap.peekPixels(&pixmap)) {
3380 const SkPMColor* pixels = pixmap.addr32();
3381 SkPMColor pmWhite = pixels[0];
3382 for (int y = 0; y < bitmap.height(); ++y) {
3383 for (int x = 0; x < bitmap.width(); ++x) {
3384 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
3385 }
3386 SkDebugf("\n");
3387 }
3388 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003389 #StdOut
Cary Clarka560c472017-11-27 10:44:06 -05003390------
3391-xxx--
3392x---x-
3393----x-
3394---x--
3395--x---
3396--x---
3397------
3398--x---
3399--x---
3400------
Cary Clarkbc5697d2017-10-04 14:31:33 -04003401 #StdOut ##
3402##
3403
3404#SeeAlso installPixels readPixels writePixels
3405
3406##
3407
3408# ------------------------------------------------------------------------------
3409
Cary Clark154beea2017-10-26 07:58:48 -04003410#Method void validate() const;
3411
3412Asserts if internal values are illegal or inconsistent. Only available if
3413SK_DEBUG is defined at compile time.
3414
3415#NoExample
3416##
3417
3418#SeeAlso SkImageInfo::validate()
3419
3420##
3421
3422# ------------------------------------------------------------------------------
3423
Cary Clarkbc5697d2017-10-04 14:31:33 -04003424#Method void toString(SkString* str) const;
3425
3426#DefinedBy SK_TO_STRING_NONVIRT() ##
3427
3428#Private
3429macro expands to: void toString(SkString* str) const;
3430##
3431
3432Creates string representation. The representation is read by
3433internal debugging tools. The interface and implementation may be
3434suppressed by defining SK_IGNORE_TO_STRING.
3435
3436#Param str storage for string representation ##
3437
3438#Example
Ben Wagner29380bd2017-10-09 14:43:00 -04003439 SkBitmap bitmap;
3440 int width = 6;
3441 int height = 11;
3442 bitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height));
3443 SkString string;
3444 bitmap.toString(&string);
3445 SkString match;
3446 match.printf("(%d, %d)", width, height);
3447 int start = string.find(match.c_str());
3448 if (start >= 0) {
3449 SkString whStr(&string.c_str()[start], match.size());
3450 SkDebugf("bitmap dimensions %s\n", whStr.c_str());
Cary Clarkbc5697d2017-10-04 14:31:33 -04003451 }
3452 #StdOut
3453 bitmap dimensions (6, 11)
3454 ##
3455##
3456
3457#SeeAlso SkPaint::toString
3458
3459##
3460
3461#Class SkBitmap ##
3462
3463#Topic Bitmap ##