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