Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1 | #Topic Bitmap |
| 2 | #Alias Bitmaps |
| 3 | #Alias Bitmap_Reference |
| 4 | |
| 5 | #Class SkBitmap |
| 6 | |
| 7 | Bitmap describes a two-dimensional raster pixel array. Bitmap is built on |
| 8 | Image_Info, containing integer width and height, Color_Type and Alpha_Type |
| 9 | describing the pixel format, and Color_Space describing the range of colors. |
| 10 | Bitmap points to Pixel_Ref, which describes the physical array of pixels. |
| 11 | Image_Info bounds may be located anywhere fully inside Pixel_Ref bounds. |
| 12 | |
| 13 | Bitmap can be drawn using Canvas. Bitmap can be a drawing destination for Canvas |
| 14 | draw methods. Bitmap flexibility as a pixel container limits some optimizations |
| 15 | available to the target platform. |
| 16 | |
| 17 | If pixel array is primarily read-only, use Image for better performance. |
| 18 | If pixel array is primarily written to, use Surface for better performance. |
| 19 | |
| 20 | Declaring SkBitmap const prevents altering Image_Info: the Bitmap height, width, |
| 21 | and so on cannot change. It does not affect Pixel_Ref: a caller may write its |
| 22 | pixels. Declaring SkBitmap const affects Bitmap configuration, not its contents. |
| 23 | |
Cary Clark | bef063a | 2017-10-31 15:44:45 -0400 | [diff] [blame] | 24 | Bitmap is not thread safe. Each thread must have its own copy of Bitmap fields, |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 25 | although threads may share the underlying pixel array. |
| 26 | |
| 27 | #Topic Row_Bytes |
| 28 | Bitmap pixels may be contiguous, or may have a gap at the end of each row. |
| 29 | Row_Bytes is the interval from one row to the next. Row_Bytes may be specified; |
| 30 | sometimes passing zero will compute the Row_Bytes from the row width and the |
| 31 | number of bytes in a pixel. Row_Bytes may be larger than the row requires. This |
| 32 | is 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 Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 92 | # computeByteSize # Returns size required for pixels. ## |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 93 | # 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 Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 100 | # eraseRGB # Deprecated ## |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 101 | # 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 111 | # 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 Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 144 | # validate() # Asserts if Bitmap is invalid (debug only). ## |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 145 | # 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 | |
| 163 | Abstract subclass of HeapAllocator. |
| 164 | |
| 165 | # ------------------------------------------------------------------------------ |
| 166 | |
| 167 | #Method virtual bool allocPixelRef(SkBitmap* bitmap) = 0 |
| 168 | |
| 169 | Allocates the pixel memory for the bitmap, given its dimensions and |
| 170 | Color_Type. Returns true on success, where success means either setPixels |
| 171 | or 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 | |
| 197 | Subclass of Allocator that returns a Pixel_Ref that allocates its pixel |
| 198 | memory from the heap. This is the default Allocator invoked by |
| 199 | allocPixels. |
| 200 | |
| 201 | # ------------------------------------------------------------------------------ |
| 202 | |
| 203 | #Method bool allocPixelRef(SkBitmap* bitmap) override |
| 204 | |
| 205 | Allocates the pixel memory for the bitmap, given its dimensions and |
| 206 | Color_Type. Returns true on success, where success means either setPixels |
| 207 | or 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 214 | 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 Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 224 | #Volatile |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 225 | pixel address = (nil) |
| 226 | pixel address = 0x560ddd0ac670 |
| 227 | ## |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 228 | ## |
| 229 | |
| 230 | #SeeAlso Allocator tryAllocPixels |
| 231 | |
| 232 | ## |
| 233 | |
| 234 | #class HeapAllocator ## |
| 235 | |
| 236 | # ------------------------------------------------------------------------------ |
| 237 | |
| 238 | #Method SkBitmap() |
| 239 | |
| 240 | Creates an empty Bitmap without pixels, with kUnknown_SkColorType, |
| 241 | kUnknown_SkAlphaType, and with a width and height of zero. Pixel_Ref origin is |
| 242 | set to (0, 0). Bitmap is not volatile. |
| 243 | |
| 244 | Use setInfo to associate SkColorType, SkAlphaType, width, and height |
| 245 | after Bitmap has been created. |
| 246 | |
| 247 | #Return empty Bitmap ## |
| 248 | |
| 249 | #Example |
| 250 | void 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 264 | width: 0 height: 0 color: kUnknown_SkColorType alpha: kUnknown_SkAlphaType |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 265 | width: 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 | |
| 277 | Copies settings from src to returned Bitmap. Shares pixels if src has pixels |
| 278 | allocated, 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 285 | void 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 292 | } |
| 293 | #StdOut |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 294 | original has pixels before copy: true |
| 295 | original has pixels after copy: true |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 296 | copy has pixels: true |
| 297 | ## |
| 298 | ## |
| 299 | |
| 300 | #SeeAlso setInfo setPixelRef setPixels swap |
| 301 | |
| 302 | ## |
| 303 | |
| 304 | # ------------------------------------------------------------------------------ |
| 305 | |
| 306 | #Method SkBitmap(SkBitmap&& src) |
| 307 | |
| 308 | Copies settings from src to returned Bitmap. Moves ownership of src pixels to |
| 309 | Bitmap. |
| 310 | |
| 311 | #Param src Bitmap to copy Image_Info, and reassign Pixel_Ref ## |
| 312 | |
| 313 | #Return copy of src ## |
| 314 | |
| 315 | #Example |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 316 | void 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 324 | #StdOut |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 325 | original has pixels before move: true |
| 326 | original has pixels after move: false |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 327 | copy has pixels: true |
| 328 | ## |
| 329 | ## |
| 330 | |
| 331 | #SeeAlso setInfo setPixelRef setPixels swap |
| 332 | |
| 333 | ## |
| 334 | |
| 335 | # ------------------------------------------------------------------------------ |
| 336 | |
| 337 | #Method ~SkBitmap() |
| 338 | |
| 339 | Decrements 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 | |
| 352 | Copies settings from src to returned Bitmap. Shares pixels if src has pixels |
| 353 | allocated, 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 360 | void 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 367 | } |
| 368 | #StdOut |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 369 | original has pixels before copy: true |
| 370 | original has pixels after copy: true |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 371 | copy has pixels: true |
| 372 | ## |
| 373 | ## |
| 374 | |
| 375 | #SeeAlso setInfo setPixelRef setPixels swap |
| 376 | |
| 377 | ## |
| 378 | |
| 379 | # ------------------------------------------------------------------------------ |
| 380 | |
| 381 | #Method SkBitmap& operator=(SkBitmap&& src) |
| 382 | |
| 383 | Copies settings from src to returned Bitmap. Moves ownership of src pixels to |
| 384 | Bitmap. |
| 385 | |
| 386 | #Param src Bitmap to copy Image_Info, and reassign Pixel_Ref ## |
| 387 | |
| 388 | #Return copy of src ## |
| 389 | |
| 390 | #Example |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 391 | void 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 399 | #StdOut |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 400 | original has pixels before move: true |
| 401 | original has pixels after move: false |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 402 | copy has pixels: true |
| 403 | ## |
| 404 | ## |
| 405 | |
| 406 | #SeeAlso setInfo setPixelRef setPixels swap |
| 407 | |
| 408 | ## |
| 409 | |
| 410 | # ------------------------------------------------------------------------------ |
| 411 | |
| 412 | #Method void swap(SkBitmap& other) |
| 413 | |
| 414 | Swaps the fields of the two bitmaps. |
| 415 | |
| 416 | #Param other Bitmap exchanged with original ## |
| 417 | |
| 418 | #Example |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 419 | void 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 435 | } |
| 436 | #StdOut |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 437 | one width:1 height:1 colorType:kRGBA_8888_SkColorType alphaType:kOpaque_SkAlphaType |
| 438 | two width:2 height:2 colorType:kBGRA_8888_SkColorType alphaType:kPremul_SkAlphaType |
| 439 | one width:2 height:2 colorType:kBGRA_8888_SkColorType alphaType:kPremul_SkAlphaType |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 440 | two 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 | |
| 452 | Returns width, height, Alpha_Type, Color_Type, and Color_Space. |
| 453 | |
| 454 | #Return reference to Image_Info ## |
| 455 | |
| 456 | #Example |
| 457 | #Image 4 |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 458 | void 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 |
| 467 | width: 56 height: 56 color: BGRA_8888 alpha: Opaque |
| 468 | ## |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 469 | } |
| 470 | ## |
| 471 | |
| 472 | #SeeAlso Image_Info |
| 473 | |
| 474 | ## |
| 475 | |
| 476 | # ------------------------------------------------------------------------------ |
| 477 | |
| 478 | #Method int width() const |
| 479 | |
| 480 | Returns pixel count in each pixel row. Should be equal or less than: |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 481 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 482 | #Formula |
| 483 | rowBytes() / info().bytesPerPixel() |
| 484 | ## |
| 485 | . |
| 486 | |
| 487 | Maybe be less than pixelRef().width(). Will not exceed pixelRef().width() less |
| 488 | pixelRefOrigin().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 |
| 498 | bitmap 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 | |
| 510 | Returns pixel row count. |
| 511 | |
| 512 | Maybe be less than pixelRef().height(). Will not exceed pixelRef().height() less |
| 513 | pixelRefOrigin().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 |
| 523 | bitmap 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 | |
| 535 | Returns Color_Type, one of: kUnknown_SkColorType, kAlpha_8_SkColorType, |
| 536 | kRGB_565_SkColorType, kARGB_4444_SkColorType, kRGBA_8888_SkColorType, |
| 537 | kBGRA_8888_SkColorType, kGray_8_SkColorType, kRGBA_F16_SkColorType. |
| 538 | |
| 539 | #Return Color_Type in Image_Info ## |
| 540 | |
| 541 | #Example |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 542 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 547 | #StdOut |
| 548 | color type: kAlpha_SkColorType |
| 549 | ## |
| 550 | ## |
| 551 | |
| 552 | #SeeAlso alphaType() SkImageInfo::colorType |
| 553 | |
| 554 | ## |
| 555 | |
| 556 | # ------------------------------------------------------------------------------ |
| 557 | |
| 558 | #Method SkAlphaType alphaType() const |
| 559 | |
| 560 | Returns Alpha_Type, one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType, |
| 561 | kPremul_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 |
| 570 | alpha type: kPremul_SkAlphaType |
| 571 | ## |
| 572 | ## |
| 573 | |
| 574 | #SeeAlso colorType() SkImageInfo::alphaType |
| 575 | |
| 576 | ## |
| 577 | |
| 578 | # ------------------------------------------------------------------------------ |
| 579 | |
| 580 | #Method SkColorSpace* colorSpace() const |
| 581 | |
| 582 | Returns Color_Space, the range of colors, associated with Image_Info. The |
| 583 | reference count of Color_Space is unchanged. The returned Color_Space is |
| 584 | immutable. |
| 585 | |
| 586 | #Return Color_Space in Image_Info ## |
| 587 | |
| 588 | #Example |
| 589 | #Description |
| 590 | SkColorSpace::MakeSRGBLinear creates Color_Space with linear gamma |
| 591 | and an sRGB gamut. This Color_Space gamma is not close to sRGB gamma. |
| 592 | ## |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 593 | SkBitmap bitmap; |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 594 | 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 |
| 602 | gammaCloseToSRGB: 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 | |
| 614 | Returns a smart pointer to Color_Space, the range of colors, associated with |
| 615 | Image_Info. The smart pointer tracks the number of objects sharing this |
| 616 | Color_Space reference so the memory is released when the owners destruct. |
| 617 | |
| 618 | The returned Color_Space is immutable. |
| 619 | |
| 620 | #Return Color_Space in Image_Info wrapped in a smart pointer ## |
| 621 | |
| 622 | #Example |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 623 | SkBitmap bitmap1, bitmap2; |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 624 | 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 |
| 634 | gammaCloseToSRGB: 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 | |
| 646 | Returns number of bytes per pixel required by Color_Type. |
| 647 | Returns 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 666 | color: kUnknown_SkColorType bytesPerPixel: 0 |
| 667 | color: kAlpha_SkColorType bytesPerPixel: 1 |
| 668 | color: kRGB_565_SkColorType bytesPerPixel: 2 |
| 669 | color: kARGB_4444_SkColorType bytesPerPixel: 2 |
| 670 | color: kRGBA_8888_SkColorType bytesPerPixel: 4 |
| 671 | color: kBGRA_8888_SkColorType bytesPerPixel: 4 |
| 672 | color: kGray_8_SkColorType bytesPerPixel: 1 |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 673 | color: 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 | |
| 685 | Returns number of pixels that fit on row. Should be greater than or equal to |
| 686 | width(). |
| 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 |
| 697 | rowBytes: 4 rowBytesAsPixels: 1 |
| 698 | rowBytes: 5 rowBytesAsPixels: 1 |
| 699 | rowBytes: 6 rowBytesAsPixels: 1 |
| 700 | rowBytes: 7 rowBytesAsPixels: 1 |
| 701 | rowBytes: 8 rowBytesAsPixels: 2 |
| 702 | ## |
| 703 | ## |
| 704 | |
| 705 | #SeeAlso rowBytes shiftPerPixel width bytesPerPixel |
| 706 | |
| 707 | ## |
| 708 | |
| 709 | # ------------------------------------------------------------------------------ |
| 710 | |
| 711 | #Method int shiftPerPixel() const |
| 712 | |
| 713 | Returns bit shift converting row bytes to row pixels. |
| 714 | Returns 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 733 | color: kUnknown_SkColorType shiftPerPixel: 0 |
| 734 | color: kAlpha_SkColorType shiftPerPixel: 0 |
| 735 | color: kRGB_565_SkColorType shiftPerPixel: 1 |
| 736 | color: kARGB_4444_SkColorType shiftPerPixel: 1 |
| 737 | color: kRGBA_8888_SkColorType shiftPerPixel: 2 |
| 738 | color: kBGRA_8888_SkColorType shiftPerPixel: 2 |
| 739 | color: kGray_8_SkColorType shiftPerPixel: 0 |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 740 | color: 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 | |
| 752 | Returns true if either width() or height() are zero. |
| 753 | |
| 754 | Does not check if Pixel_Ref is nullptr; call drawsNothing to check width(), |
| 755 | height(), 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 |
| 769 | width: 0 height: 0 empty: true |
| 770 | width: 0 height: 2 empty: true |
| 771 | width: 2 height: 0 empty: true |
| 772 | width: 2 height: 2 empty: false |
| 773 | ## |
| 774 | ## |
| 775 | |
| 776 | #SeeAlso height() width() drawsNothing |
| 777 | |
| 778 | ## |
| 779 | |
| 780 | # ------------------------------------------------------------------------------ |
| 781 | |
| 782 | #Method bool isNull() const |
| 783 | |
| 784 | Return true if Pixel_Ref is nullptr. |
| 785 | |
| 786 | Does not check if width() or height() are zero; call drawsNothing to check |
| 787 | width(), height(), and Pixel_Ref. |
| 788 | |
| 789 | #Return true if no Pixel_Ref is associated ## |
| 790 | |
| 791 | #Example |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 792 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 798 | #StdOut |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 799 | empty bitmap does not have pixels |
| 800 | bitmap with dimensions does not have pixels |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 801 | allocated bitmap does have pixels |
| 802 | ## |
| 803 | ## |
| 804 | |
| 805 | #SeeAlso empty() drawsNothing pixelRef |
| 806 | |
| 807 | ## |
| 808 | |
| 809 | # ------------------------------------------------------------------------------ |
| 810 | |
| 811 | #Method bool drawsNothing() const |
| 812 | |
| 813 | Return true if width() or height() are zero, or if Pixel_Ref is nullptr. |
| 814 | If true, Bitmap has no effect when drawn or drawn into. |
| 815 | |
| 816 | #Return true if drawing has no effect ## |
| 817 | |
| 818 | #Example |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 819 | 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 |
| 829 | empty:true isNull:true drawsNothing:true |
| 830 | empty:true isNull:false drawsNothing:true |
| 831 | empty:false isNull:true drawsNothing:true |
| 832 | empty:false isNull:false drawsNothing:false |
| 833 | ## |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 834 | ## |
| 835 | |
| 836 | #SeeAlso empty() isNull pixelRef |
| 837 | |
| 838 | ## |
| 839 | |
| 840 | # ------------------------------------------------------------------------------ |
| 841 | |
| 842 | #Method size_t rowBytes() const |
| 843 | |
| 844 | Returns row bytes, the interval from one pixel row to the next. Row bytes |
| 845 | is at least as large as |
| 846 | #Formula |
| 847 | width() * info().bytesPerPixel() |
| 848 | ## |
| 849 | . |
| 850 | |
| 851 | Returns zero if colorType is kUnknown_SkColorType, or if row bytes supplied to |
| 852 | setInfo is not large enough to hold a row of pixels. |
| 853 | |
| 854 | #Return byte length of pixel row ## |
| 855 | |
| 856 | #Example |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 857 | 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 |
| 863 | setInfo returned:false rowBytes:0 |
| 864 | setInfo returned:true rowBytes:8 |
| 865 | ## |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 866 | ## |
| 867 | |
| 868 | #SeeAlso info() setInfo SkImageInfo::minRowBytes |
| 869 | |
| 870 | ## |
| 871 | |
| 872 | # ------------------------------------------------------------------------------ |
| 873 | |
| 874 | #Method bool setAlphaType(SkAlphaType alphaType) |
| 875 | |
| 876 | Sets Alpha_Type, if alphaType is compatible with Color_Type. |
| 877 | Returns true unless alphaType is kUnknown_SkAlphaType and current Alpha_Type |
| 878 | is not kUnknown_SkAlphaType. |
| 879 | |
| 880 | Returns true if Color_Type is kUnknown_SkColorType. alphaType is ignored, and |
| 881 | Alpha_Type remains kUnknown_SkAlphaType. |
| 882 | |
| 883 | Returns true if Color_Type is kRGB_565_SkColorType or kGray_8_SkColorType. |
| 884 | alphaType is ignored, and Alpha_Type remains kOpaque_SkAlphaType. |
| 885 | |
| 886 | If Color_Type is kARGB_4444_SkColorType, kRGBA_8888_SkColorType, |
| 887 | kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: returns true unless |
| 888 | alphaType is kUnknown_SkAlphaType and Alpha_Type is not kUnknown_SkAlphaType. |
| 889 | If Alpha_Type is kUnknown_SkAlphaType, alphaType is ignored. |
| 890 | |
| 891 | If Color_Type is kAlpha_8_SkColorType, returns true unless |
| 892 | alphaType is kUnknown_SkAlphaType and Alpha_Type is not kUnknown_SkAlphaType. |
| 893 | If Alpha_Type is kUnknown_SkAlphaType, alphaType is ignored. If alphaType is |
| 894 | kUnpremul_SkAlphaType, it is treated as kPremul_SkAlphaType. |
| 895 | |
| 896 | This changes Alpha_Type in Pixel_Ref; all bitmaps sharing Pixel_Ref |
| 897 | are 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 906 | void 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 929 | } |
| 930 | ## |
| 931 | |
| 932 | #SeeAlso Alpha_Type Color_Type Image_Info setInfo |
| 933 | |
| 934 | ## |
| 935 | |
| 936 | # ------------------------------------------------------------------------------ |
| 937 | |
| 938 | #Method void* getPixels() const |
| 939 | |
| 940 | Returns pixel address, the base address corresponding to the pixel origin. |
| 941 | |
| 942 | #Return pixel address ## |
| 943 | |
| 944 | #Example |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 945 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 955 | #StdOut |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 956 | bitmap.getColor(0, 1) == 0x00000000 |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 957 | bitmap.getColor(0, 0) == 0xFFFFFFFF |
| 958 | ## |
| 959 | ## |
| 960 | |
| 961 | #SeeAlso isNull drawsNothing |
| 962 | |
| 963 | ## |
| 964 | |
| 965 | # ------------------------------------------------------------------------------ |
| 966 | |
| 967 | #Method size_t computeByteSize() const |
| 968 | |
| 969 | Returns minimum memory required for pixel storage. |
| 970 | Does not include unused memory on last row when rowBytesAsPixels exceeds width(). |
| 971 | Returns zero if result does not fit in size_t. |
| 972 | Returns zero if height() or width() is 0. |
| 973 | Returns height() times rowBytes if colorType is kUnknown_SkColorType. |
| 974 | |
| 975 | #Return size in bytes of image buffer ## |
| 976 | |
| 977 | #Example |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 978 | SkBitmap bitmap; |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 979 | 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 988 | width: 1 height: 1 computeByteSize: 4 |
| 989 | width: 1 height: 1000 computeByteSize: 4999 |
| 990 | width: 1 height: 1000000 computeByteSize: 4999999 |
| 991 | width: 1000 height: 1 computeByteSize: 4000 |
| 992 | width: 1000 height: 1000 computeByteSize: 4999000 |
| 993 | width: 1000 height: 1000000 computeByteSize: 4999999000 |
| 994 | width: 1000000 height: 1 computeByteSize: 4000000 |
| 995 | width: 1000000 height: 1000 computeByteSize: 4999000000 |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 996 | width: 1000000 height: 1000000 computeByteSize: 4999999000000 |
| 997 | ## |
| 998 | ## |
| 999 | |
| 1000 | #SeeAlso SkImageInfo::computeByteSize |
| 1001 | |
| 1002 | ## |
| 1003 | |
| 1004 | # ------------------------------------------------------------------------------ |
| 1005 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1006 | #Method bool isImmutable() const |
| 1007 | |
| 1008 | Returns true if pixels can not change. |
| 1009 | |
| 1010 | Most immutable Bitmap checks trigger an assert only on debug builds. |
| 1011 | |
| 1012 | #Return true if pixels are immutable ## |
| 1013 | |
| 1014 | #Example |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1015 | 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(©, {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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1023 | } |
| 1024 | #StdOut |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1025 | original is immutable |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1026 | copy is immutable |
| 1027 | ## |
| 1028 | ## |
| 1029 | |
| 1030 | #SeeAlso setImmutable SkPixelRef::isImmutable SkImage |
| 1031 | |
| 1032 | ## |
| 1033 | |
| 1034 | # ------------------------------------------------------------------------------ |
| 1035 | |
| 1036 | #Method void setImmutable() |
| 1037 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1038 | Sets internal flag to mark Bitmap as immutable. Once set, pixels can not change. |
| 1039 | Any other bitmap sharing the same Pixel_Ref are also marked as immutable. |
| 1040 | Once Pixel_Ref is marked immutable, the setting cannot be cleared. |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1041 | |
| 1042 | Writing to immutable Bitmap pixels triggers an assert on debug builds. |
| 1043 | |
| 1044 | #Example |
| 1045 | #Description |
| 1046 | Triggers assert if SK_DEBUG is true, runs fine otherwise. |
| 1047 | ## |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1048 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1056 | offscreen.clear(SK_ColorBLACK); |
| 1057 | ## |
| 1058 | |
| 1059 | #SeeAlso isImmutable SkPixelRef::setImmutable SkImage |
| 1060 | |
| 1061 | ## |
| 1062 | |
| 1063 | # ------------------------------------------------------------------------------ |
| 1064 | |
| 1065 | #Method bool isOpaque() const |
| 1066 | |
| 1067 | Returns true if Alpha_Type is kOpaque_SkAlphaType. |
| 1068 | Does not check if Color_Type allows Alpha, or if any pixel value has |
| 1069 | transparency. |
| 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1077 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1089 | #StdOut |
| 1090 | isOpaque: false |
| 1091 | isOpaque: false |
| 1092 | isOpaque: true |
| 1093 | isOpaque: true |
| 1094 | ## |
| 1095 | ## |
| 1096 | |
| 1097 | #SeeAlso ComputeIsOpaque SkImageInfo::isOpaque |
| 1098 | |
| 1099 | ## |
| 1100 | |
| 1101 | # ------------------------------------------------------------------------------ |
| 1102 | |
| 1103 | #Method bool isVolatile() const |
| 1104 | |
| 1105 | If true, provides a hint to caller that pixels should not |
| 1106 | be cached. Only true if setIsVolatile has been called to mark as volatile. |
| 1107 | |
| 1108 | Volatile state is not shared by other bitmaps sharing the same Pixel_Ref. |
| 1109 | |
| 1110 | #Return true if marked volatile ## |
| 1111 | |
| 1112 | #Example |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1113 | 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(©, {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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1122 | #StdOut |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1123 | original is volatile |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1124 | copy is not volatile |
| 1125 | ## |
| 1126 | ## |
| 1127 | |
| 1128 | #SeeAlso setIsVolatile |
| 1129 | |
| 1130 | ## |
| 1131 | |
| 1132 | # ------------------------------------------------------------------------------ |
| 1133 | |
| 1134 | #Method void setIsVolatile(bool isVolatile) |
| 1135 | |
| 1136 | Sets if pixels should be read from Pixel_Ref on every access. Bitmaps are not |
| 1137 | volatile by default; a GPU back end may upload pixel values expecting them to be |
| 1138 | accessed repeatedly. Marking temporary Bitmaps as volatile provides a hint to |
| 1139 | Device that the Bitmap pixels should not be cached. This can |
| 1140 | improve performance by avoiding overhead and reducing resource |
| 1141 | consumption on Device. |
| 1142 | |
| 1143 | #Param isVolatile true if backing pixels are temporary ## |
| 1144 | |
| 1145 | #Example |
| 1146 | #Height 20 |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1147 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1158 | ## |
| 1159 | |
| 1160 | #SeeAlso isVolatile |
| 1161 | |
| 1162 | ## |
| 1163 | |
| 1164 | # ------------------------------------------------------------------------------ |
| 1165 | |
| 1166 | #Method void reset() |
| 1167 | |
| 1168 | Resets to its initial state; all fields are set to zero, as if Bitmap had |
| 1169 | been initialized by SkBitmap(). |
| 1170 | |
| 1171 | Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to |
| 1172 | kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType. |
| 1173 | |
| 1174 | If Pixel_Ref is allocated, its reference count is decreased by one, releasing |
| 1175 | its memory if Bitmap is the sole owner. |
| 1176 | |
| 1177 | #Example |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1178 | 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 |
| 1187 | width:1 height:1 isNull:false |
| 1188 | width:0 height:0 isNull:true |
| 1189 | ## |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1190 | ## |
| 1191 | |
| 1192 | #SeeAlso SkBitmap() SkAlphaType SkColorType |
| 1193 | |
| 1194 | ## |
| 1195 | |
| 1196 | # ------------------------------------------------------------------------------ |
| 1197 | |
| 1198 | #Method static bool ComputeIsOpaque(const SkBitmap& bm) |
| 1199 | |
| 1200 | Returns true if all pixels are opaque. Color_Type determines how pixels |
| 1201 | are encoded, and whether pixel describes Alpha. Returns true for Color_Types |
| 1202 | without alpha in each pixel; for other Color_Types, returns true if all |
| 1203 | pixels have alpha values equivalent to 1.0 or greater. |
| 1204 | |
| 1205 | For Color_Types kRGB_565_SkColorType or kGray_8_SkColorType: always |
| 1206 | returns true. For Color_Types kAlpha_8_SkColorType, kBGRA_8888_SkColorType, |
| 1207 | kRGBA_8888_SkColorType: returns true if all pixel Alpha values are 255. |
| 1208 | For Color_Type kARGB_4444_SkColorType: returns true if all pixel Alpha values are 15. |
| 1209 | For kRGBA_F16_SkColorType: returns true if all pixel Alpha values are 1.0 or |
| 1210 | greater. |
| 1211 | |
| 1212 | Returns 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1219 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1228 | } |
| 1229 | #StdOut |
| 1230 | computeIsOpaque: false |
| 1231 | computeIsOpaque: true |
| 1232 | computeIsOpaque: false |
| 1233 | computeIsOpaque: true |
| 1234 | ## |
| 1235 | ## |
| 1236 | |
| 1237 | #SeeAlso isOpaque Color_Type Alpha |
| 1238 | |
| 1239 | ## |
| 1240 | |
| 1241 | # ------------------------------------------------------------------------------ |
| 1242 | |
| 1243 | #Method void getBounds(SkRect* bounds) const |
| 1244 | |
| 1245 | Returns Rect { 0, 0, width(), height() }. |
| 1246 | |
| 1247 | #Param bounds container for floating point rectangle ## |
| 1248 | |
| 1249 | #Example |
| 1250 | #Height 160 |
| 1251 | #Image 3 |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1252 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1260 | ## |
| 1261 | |
| 1262 | #SeeAlso bounds() |
| 1263 | |
| 1264 | ## |
| 1265 | |
| 1266 | # ------------------------------------------------------------------------------ |
| 1267 | |
| 1268 | #Method void getBounds(SkIRect* bounds) const |
| 1269 | |
| 1270 | Returns IRect { 0, 0, width(), height() }. |
| 1271 | |
| 1272 | #Param bounds container for integral rectangle ## |
| 1273 | |
| 1274 | #Example |
| 1275 | #Image 3 |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1276 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1283 | ## |
| 1284 | |
| 1285 | #SeeAlso bounds() |
| 1286 | |
| 1287 | ## |
| 1288 | |
| 1289 | # ------------------------------------------------------------------------------ |
| 1290 | |
| 1291 | #Method SkIRect bounds() const |
| 1292 | |
| 1293 | Returns IRect { 0, 0, width(), height() }. |
| 1294 | |
| 1295 | #Return integral rectangle from origin to width() and height() ## |
| 1296 | |
| 1297 | #Example |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1298 | #Height 128 |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1299 | #Image 4 |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1300 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1306 | ## |
| 1307 | |
| 1308 | #SeeAlso getBounds |
| 1309 | |
| 1310 | ## |
| 1311 | |
| 1312 | # ------------------------------------------------------------------------------ |
| 1313 | |
| 1314 | #Method SkISize dimensions() const |
| 1315 | |
| 1316 | Returns ISize { width(), height() }. |
| 1317 | |
| 1318 | #Return integral size of width() and height() ## |
| 1319 | |
| 1320 | #Example |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1321 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1328 | ## |
| 1329 | |
| 1330 | #SeeAlso height() width() |
| 1331 | |
| 1332 | ## |
| 1333 | |
| 1334 | # ------------------------------------------------------------------------------ |
| 1335 | |
| 1336 | #Method SkIRect getSubset() const |
| 1337 | |
| 1338 | Returns 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1344 | 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 |
| 1354 | source: 0, 0, 512, 512 |
| 1355 | subset: 100, 100, 412, 412 |
| 1356 | ## |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1357 | ## |
| 1358 | |
| 1359 | #SeeAlso extractSubset getBounds |
| 1360 | |
| 1361 | ## |
| 1362 | |
| 1363 | # ------------------------------------------------------------------------------ |
| 1364 | |
| 1365 | #Method bool setInfo(const SkImageInfo& imageInfo, size_t rowBytes = 0) |
| 1366 | |
| 1367 | Sets width, height, Alpha_Type, Color_Type, Color_Space, and optional |
| 1368 | rowBytes. Frees pixels, and returns true if successful. |
| 1369 | |
| 1370 | imageInfo.alphaType may be altered to a value permitted by imageInfo.colorSpace. |
| 1371 | If imageInfo.colorType is kUnknown_SkColorType, imageInfo.alphaType is |
| 1372 | set to kUnknown_SkAlphaType. |
| 1373 | If imageInfo.colorType is kAlpha_8_SkColorType and imageInfo.alphaType is |
| 1374 | kUnpremul_SkAlphaType, imageInfo.alphaType is replaced by kPremul_SkAlphaType. |
| 1375 | If imageInfo.colorType is kRGB_565_SkColorType or kGray_8_SkColorType, |
| 1376 | imageInfo.alphaType is set to kOpaque_SkAlphaType. |
| 1377 | If imageInfo.colorType is kARGB_4444_SkColorType, kRGBA_8888_SkColorType, |
| 1378 | kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: imageInfo.alphaType remains |
| 1379 | unchanged. |
| 1380 | |
| 1381 | rowBytes must equal or exceed imageInfo.minRowBytes. If imageInfo.colorSpace is |
| 1382 | kUnknown_SkColorType, rowBytes is ignored and treated as zero; for all other |
| 1383 | Color_Space values, rowBytes of zero is treated as imageInfo.minRowBytes. |
| 1384 | |
| 1385 | Calls 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1402 | SkBitmap bitmap; |
| 1403 | bitmap.setInfo(SkImageInfo::MakeN32(44, 16, kOpaque_SkAlphaType)); |
| 1404 | bitmap.allocPixels(); |
| 1405 | bitmap.eraseColor(SK_ColorGREEN); |
| 1406 | SkCanvas offscreen(bitmap); |
| 1407 | SkPaint paint; |
| 1408 | offscreen.drawString("!@#$%", 0, 12, paint); |
| 1409 | canvas->scale(6, 6); |
| 1410 | canvas->drawBitmap(bitmap, 0, 0); |
| 1411 | ^^^# |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1412 | ## |
| 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 | |
| 1428 | AllocFlags 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 | |
| 1445 | Sets Image_Info to info following the rules in setInfo and allocates pixel |
| 1446 | memory. If flags is kZeroPixels_AllocFlag, memory is zeroed. |
| 1447 | |
| 1448 | Returns false and calls reset() if Image_Info could not be set, or memory could |
| 1449 | not be allocated, or memory size exceeds 31 bits, or memory could not optionally |
| 1450 | be zeroed. |
| 1451 | |
| 1452 | On most platforms, allocating pixel memory may succeed even though there is |
| 1453 | not sufficient memory to hold pixels; allocation does not take place |
| 1454 | until the pixels are written to. The actual behavior depends on the platform |
| 1455 | implementation of malloc(), if flags is zero, and calloc(), if flags is |
| 1456 | kZeroPixels_AllocFlag. |
| 1457 | |
| 1458 | Passing kZeroPixels_AllocFlag is usually faster than separately calling |
| 1459 | eraseColor(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 |
| 1468 | For width of 100,000, height of 100,000, and 4 bytes per pixel, Pixel_Ref |
| 1469 | requires 40,000,000,000 bytes of storage. Allocator implemented |
| 1470 | internally by SkMallocPixelRef::MakeZeroed limits the maximum memory allowed; |
| 1471 | the allocation must fit in 31 bits. |
| 1472 | ## |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1473 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1482 | } |
| 1483 | #StdOut |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1484 | attempt 1 |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1485 | failed! |
| 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 | |
| 1497 | Sets Image_Info to info following the rules in setInfo and allocates pixel |
| 1498 | memory. If flags is kZeroPixels_AllocFlag, memory is zeroed. |
| 1499 | |
| 1500 | Aborts execution if Image_Info could not be set, or memory could |
| 1501 | not be allocated, or memory size exceeds 31 bits, or memory could not optionally |
| 1502 | be zeroed. Abort steps may be provided by the user at compile time by defining |
| 1503 | SK_ABORT. |
| 1504 | |
| 1505 | On most platforms, allocating pixel memory may succeed even though there is |
| 1506 | not sufficient memory to hold pixels; allocation does not take place |
| 1507 | until the pixels are written to. The actual behavior depends on the platform |
| 1508 | implementation of malloc(), if flags is zero, and calloc(), if flags is |
| 1509 | kZeroPixels_AllocFlag. |
| 1510 | |
| 1511 | Passing kZeroPixels_AllocFlag is usually faster than separately calling |
| 1512 | eraseColor(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 |
| 1520 | Text is drawn on a transparent background; drawing the bitmap a second time |
| 1521 | lets the first draw show through. |
| 1522 | ## |
| 1523 | ###^ |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1524 | SkBitmap bitmap; |
| 1525 | bitmap.allocPixelsFlags(SkImageInfo::MakeN32(44, 16, kPremul_SkAlphaType), |
| 1526 | SkBitmap::kZeroPixels_AllocFlag); |
| 1527 | SkCanvas offscreen(bitmap); |
| 1528 | SkPaint paint; |
| 1529 | offscreen.drawString("!@#$%", 0, 12, paint); |
| 1530 | canvas->scale(6, 6); |
| 1531 | canvas->drawBitmap(bitmap, 0, 0); |
| 1532 | canvas->drawBitmap(bitmap, 8, 8); |
| 1533 | ^^^# |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1534 | ## |
| 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 | |
| 1552 | Sets Image_Info to info following the rules in setInfo and allocates pixel |
| 1553 | memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(), |
| 1554 | or equal zero. Pass in zero for rowBytes to compute the minimum valid value. |
| 1555 | |
| 1556 | Returns false and calls reset() if Image_Info could not be set, or memory could |
| 1557 | not be allocated. |
| 1558 | |
| 1559 | On most platforms, allocating pixel memory may succeed even though there is |
| 1560 | not sufficient memory to hold pixels; allocation does not take place |
| 1561 | until the pixels are written to. The actual behavior depends on the platform |
| 1562 | implementation 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1571 | SkBitmap bitmap; |
| 1572 | SkImageInfo info = SkImageInfo::Make(64, 256, kGray_8_SkColorType, kOpaque_SkAlphaType); |
| 1573 | if (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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1580 | } |
| 1581 | ## |
| 1582 | |
| 1583 | #SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeAllocate |
| 1584 | |
| 1585 | ## |
| 1586 | |
| 1587 | # ------------------------------------------------------------------------------ |
| 1588 | |
| 1589 | #Method void allocPixels(const SkImageInfo& info, size_t rowBytes) |
| 1590 | |
| 1591 | Sets Image_Info to info following the rules in setInfo and allocates pixel |
| 1592 | memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(), |
| 1593 | or equal zero. Pass in zero for rowBytes to compute the minimum valid value. |
| 1594 | |
| 1595 | Aborts execution if Image_Info could not be set, or memory could |
| 1596 | not be allocated, or memory size exceeds 31 bits. Abort steps may be provided by |
| 1597 | the user at compile time by defining SK_ABORT. |
| 1598 | |
| 1599 | On most platforms, allocating pixel memory may succeed even though there is |
| 1600 | not sufficient memory to hold pixels; allocation does not take place |
| 1601 | until the pixels are written to. The actual behavior depends on the platform |
| 1602 | implementation 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1609 | SkBitmap bitmap; |
| 1610 | SkImageInfo info = SkImageInfo::Make(256, 64, kGray_8_SkColorType, kOpaque_SkAlphaType); |
| 1611 | bitmap.allocPixels(info, info.width() * info.bytesPerPixel() + 64); |
| 1612 | SkCanvas offscreen(bitmap); |
| 1613 | offscreen.scale(.5f, .5f); |
| 1614 | for (int y : { 0, 64, 128, 192 } ) { |
| 1615 | offscreen.drawBitmap(source, 0, -y); |
| 1616 | canvas->drawBitmap(bitmap, 0, y); |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1617 | } |
| 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 | |
| 1628 | Sets Image_Info to info following the rules in setInfo and allocates pixel |
| 1629 | memory. |
| 1630 | |
| 1631 | Returns false and calls reset() if Image_Info could not be set, or memory could |
| 1632 | not be allocated. |
| 1633 | |
| 1634 | On most platforms, allocating pixel memory may succeed even though there is |
| 1635 | not sufficient memory to hold pixels; allocation does not take place |
| 1636 | until the pixels are written to. The actual behavior depends on the platform |
| 1637 | implementation 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1645 | SkBitmap bitmap; |
| 1646 | if (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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1653 | } |
| 1654 | ## |
| 1655 | |
| 1656 | #SeeAlso tryAllocPixelsFlags allocPixels SkMallocPixelRef::MakeAllocate |
| 1657 | |
| 1658 | ## |
| 1659 | |
| 1660 | # ------------------------------------------------------------------------------ |
| 1661 | |
| 1662 | #Method void allocPixels(const SkImageInfo& info) |
| 1663 | |
| 1664 | Sets Image_Info to info following the rules in setInfo and allocates pixel |
| 1665 | memory. |
| 1666 | |
| 1667 | Aborts execution if Image_Info could not be set, or memory could |
| 1668 | not be allocated, or memory size exceeds 31 bits. Abort steps may be provided by |
| 1669 | the user at compile time by defining SK_ABORT. |
| 1670 | |
| 1671 | On most platforms, allocating pixel memory may succeed even though there is |
| 1672 | not sufficient memory to hold pixels; allocation does not take place |
| 1673 | until the pixels are written to. The actual behavior depends on the platform |
| 1674 | implementation of malloc(). |
| 1675 | |
| 1676 | #Param info contains width, height, Alpha_Type, Color_Type, Color_Space ## |
| 1677 | |
| 1678 | #Example |
| 1679 | #Image 4 |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1680 | SkBitmap bitmap; |
| 1681 | bitmap.allocPixels(SkImageInfo::Make(64, 64, kGray_8_SkColorType, kOpaque_SkAlphaType)); |
| 1682 | SkCanvas offscreen(bitmap); |
| 1683 | offscreen.scale(.5f, .5f); |
| 1684 | for (int y : { 0, 64, 128, 192 } ) { |
| 1685 | offscreen.drawBitmap(source, -y, -y); |
| 1686 | canvas->drawBitmap(bitmap, y, y); |
| 1687 | } |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1688 | ## |
| 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 | |
| 1698 | Sets Image_Info to width, height, and the native Color_Type; and allocates |
| 1699 | pixel memory. If isOpaque is true, sets Image_Info to kOpaque_SkAlphaType; |
| 1700 | otherwise, sets to kPremul_SkAlphaType. |
| 1701 | |
| 1702 | Calls reset() and returns false if width exceeds 29 bits or is negative, |
| 1703 | or height is negative. |
| 1704 | |
| 1705 | Returns false if allocation fails. |
| 1706 | |
| 1707 | Use to create Bitmap that matches native pixel arrangement on the platform, |
| 1708 | to 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1718 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1729 | ## |
| 1730 | |
| 1731 | #SeeAlso tryAllocPixels allocN32Pixels SkMallocPixelRef::MakeAllocate |
| 1732 | |
| 1733 | ## |
| 1734 | |
| 1735 | # ------------------------------------------------------------------------------ |
| 1736 | |
| 1737 | #Method void allocN32Pixels(int width, int height, bool isOpaque = false) |
| 1738 | |
| 1739 | Sets Image_Info to width, height, and the native Color_Type; and allocates |
| 1740 | pixel memory. If isOpaque is true, sets Image_Info to kPremul_SkAlphaType; |
| 1741 | otherwise, sets to kOpaque_SkAlphaType. |
| 1742 | |
| 1743 | Aborts if width exceeds 29 bits or is negative, or height is negative, or |
| 1744 | allocation fails. Abort steps may be provided by the user at compile time by |
| 1745 | defining SK_ABORT. |
| 1746 | |
| 1747 | Use to create Bitmap that matches native pixel arrangement on the platform, |
| 1748 | to 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1755 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1770 | ## |
| 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 | |
| 1781 | Sets Image_Info to info following the rules in setInfo, and creates Pixel_Ref |
| 1782 | containing pixels and rowBytes. releaseProc, if not nullptr, is called |
| 1783 | immediately on failure or when pixels are no longer referenced. context may be |
| 1784 | nullptr. |
| 1785 | |
| 1786 | If Image_Info could not be set, or rowBytes is less than info.minRowBytes: |
| 1787 | calls releaseProc if present, calls reset(), and returns false. |
| 1788 | |
| 1789 | Otherwise, if pixels equals nullptr: sets Image_Info, calls releaseProc if |
| 1790 | present, returns true. |
| 1791 | |
| 1792 | If Image_Info is set, pixels is not nullptr, and releaseProc is not nullptr: |
| 1793 | when pixels are no longer referenced, calls releaseProc with pixels and context |
| 1794 | as 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 |
| 1806 | releaseProc is called immediately because rowBytes is too small for Pixel_Ref. |
| 1807 | ## |
| 1808 | #Function |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1809 | static void releaseProc(void* addr, void* ) { |
| 1810 | SkDebugf("releaseProc called\n"); |
| 1811 | delete[] (uint32_t*) addr; |
| 1812 | } |
| 1813 | |
| 1814 | ## |
| 1815 | |
| 1816 | void 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1823 | } |
| 1824 | #StdOut |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1825 | before installPixels |
| 1826 | releaseProc called |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1827 | install 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 | |
| 1839 | Sets Image_Info to info following the rules in setInfo, and creates Pixel_Ref |
| 1840 | containing pixels and rowBytes. |
| 1841 | |
| 1842 | If Image_Info could not be set, or rowBytes is less than info.minRowBytes: |
| 1843 | calls reset(), and returns false. |
| 1844 | |
| 1845 | Otherwise, if pixels equals nullptr: sets Image_Info, returns true. |
| 1846 | |
| 1847 | Caller 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 ## |
| 1858 | GPU does not support kUnpremul_SkAlphaType, does not assert that it does not. |
| 1859 | ## |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1860 | void 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1874 | } |
| 1875 | ## |
| 1876 | |
| 1877 | #SeeAlso allocPixels |
| 1878 | |
| 1879 | ## |
| 1880 | |
| 1881 | # ------------------------------------------------------------------------------ |
| 1882 | |
| 1883 | #Method bool installPixels(const SkPixmap& pixmap) |
| 1884 | |
| 1885 | Sets Image_Info to pixmap.info() following the rules in setInfo, and creates |
| 1886 | Pixel_Ref containing pixmap.addr() and pixmap.rowBytes. |
| 1887 | |
| 1888 | If Image_Info could not be set, or pixmap.rowBytes is less than |
| 1889 | SkImageInfo::minRowBytes: calls reset(), and returns false. |
| 1890 | |
| 1891 | Otherwise, if pixmap.addr() equals nullptr: sets Image_Info, returns true. |
| 1892 | |
| 1893 | Caller 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 |
| 1901 | Draw 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 | |
| 1928 | Sets Image_Info to mask width, mask height, kAlpha_8_SkColorType, and |
| 1929 | kPremul_SkAlphaType. Sets Pixel_Ref to mask image and mask rowBytes. |
| 1930 | |
| 1931 | Returns false and calls reset() if mask format is not SkMask::kA8_Format, |
| 1932 | or if mask width or mask height is negative, or if mask rowBytes is less |
| 1933 | than mask width. |
| 1934 | |
| 1935 | Caller 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 |
| 1943 | Draw a five by five bitmap, and draw it again with a center white pixel. |
| 1944 | ## |
| 1945 | #Height 64 |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1946 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1962 | canvas->drawBitmap(bitmap, 10, 0); |
| 1963 | ## |
| 1964 | |
| 1965 | #SeeAlso installPixels allocPixels |
| 1966 | |
| 1967 | ## |
| 1968 | |
| 1969 | # ------------------------------------------------------------------------------ |
| 1970 | |
| 1971 | #Method void setPixels(void* pixels) |
| 1972 | |
| 1973 | Replaces Pixel_Ref with pixels, preserving Image_Info and rowBytes. |
| 1974 | Sets Pixel_Ref origin to (0, 0). |
| 1975 | |
| 1976 | If pixels is nullptr, or if info().colorType equals kUnknown_SkColorType; |
| 1977 | release reference to Pixel_Ref, and set Pixel_Ref to nullptr. |
| 1978 | |
| 1979 | Caller is responsible for handling ownership pixel memory for the lifetime |
| 1980 | of Bitmap and Pixel_Ref. |
| 1981 | |
| 1982 | #Param pixels address of pixel storage, managed by caller ## |
| 1983 | |
| 1984 | #Example |
| 1985 | #Height 50 |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 1986 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 1994 | ## |
| 1995 | |
| 1996 | #SeeAlso installPixels allocPixels |
| 1997 | |
| 1998 | ## |
| 1999 | |
| 2000 | # ------------------------------------------------------------------------------ |
| 2001 | |
| 2002 | #Method bool SK_WARN_UNUSED_RESULT tryAllocPixels() |
| 2003 | |
| 2004 | Allocates pixel memory with HeapAllocator, and replaces existing Pixel_Ref. |
| 2005 | The allocation size is determined by Image_Info width, height, and Color_Type. |
| 2006 | |
| 2007 | Returns false if info().colorType is kUnknown_SkColorType, or allocation exceeds |
| 2008 | 31 bits, or allocation fails. |
| 2009 | |
| 2010 | #Return true if the allocation succeeds |
| 2011 | ## |
| 2012 | |
| 2013 | #Example |
| 2014 | #Height 50 |
| 2015 | #Description |
| 2016 | Bitmap hosts and draws gray values in set1. tryAllocPixels replaces Pixel_Ref |
| 2017 | and erases it to black, but does not alter set1. setPixels replaces black |
| 2018 | Pixel_Ref with set1. |
| 2019 | ## |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2020 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2031 | ## |
| 2032 | |
| 2033 | #SeeAlso allocPixels installPixels setPixels |
| 2034 | |
| 2035 | ## |
| 2036 | |
| 2037 | # ------------------------------------------------------------------------------ |
| 2038 | |
| 2039 | #Method void allocPixels() |
| 2040 | |
| 2041 | Allocates pixel memory with HeapAllocator, and replaces existing Pixel_Ref. |
| 2042 | The allocation size is determined by Image_Info width, height, and Color_Type. |
| 2043 | |
| 2044 | Aborts if info().colorType is kUnknown_SkColorType, or allocation exceeds |
| 2045 | 31 bits, or allocation fails. Abort steps may be provided by the user at compile |
| 2046 | time by defining SK_ABORT. |
| 2047 | |
| 2048 | #Example |
| 2049 | #Height 50 |
| 2050 | #Description |
| 2051 | Bitmap hosts and draws gray values in set1. allocPixels replaces Pixel_Ref |
| 2052 | and erases it to black, but does not alter set1. setPixels replaces black |
| 2053 | Pixel_Ref with set2. |
| 2054 | ## |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2055 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2065 | 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 | |
| 2076 | Allocates pixel memory with allocator, and replaces existing Pixel_Ref. |
| 2077 | The allocation size is determined by Image_Info width, height, and Color_Type. |
| 2078 | If allocator is nullptr, use HeapAllocator instead. |
| 2079 | |
| 2080 | Returns 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 |
| 2090 | HeapAllocator limits the maximum size of Bitmap to two gigabytes. Using |
| 2091 | a custom allocator, this limitation may be relaxed. This example can be |
| 2092 | modified to allocate an eight gigabyte Bitmap on a 64 bit platform with |
| 2093 | sufficient memory. |
| 2094 | ## |
| 2095 | #Function |
| 2096 | class LargePixelRef : public SkPixelRef { |
| 2097 | public: |
| 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 | |
| 2107 | class LargeAllocator : public SkBitmap::Allocator { |
| 2108 | public: |
| 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 | |
| 2128 | void draw(SkCanvas* canvas) { |
| 2129 | LargeAllocator largeAllocator; |
| 2130 | SkBitmap bitmap; |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2131 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | ## |
| 2141 | |
| 2142 | #SeeAlso allocPixels Allocator Pixel_Ref |
| 2143 | |
| 2144 | ## |
| 2145 | |
| 2146 | # ------------------------------------------------------------------------------ |
| 2147 | |
| 2148 | #Method void allocPixels(Allocator* allocator) |
| 2149 | |
| 2150 | Allocates pixel memory with allocator, and replaces existing Pixel_Ref. |
| 2151 | The allocation size is determined by Image_Info width, height, and Color_Type. |
| 2152 | If allocator is nullptr, use HeapAllocator instead. |
| 2153 | |
| 2154 | Aborts if allocator allocPixelRef return false. Abort steps may be provided by |
| 2155 | the 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 |
| 2162 | class TinyAllocator : public SkBitmap::Allocator { |
| 2163 | public: |
| 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 | |
| 2180 | void draw(SkCanvas* canvas) { |
| 2181 | TinyAllocator tinyAllocator; |
| 2182 | SkBitmap bitmap; |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2183 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2190 | } |
| 2191 | ## |
| 2192 | |
| 2193 | #SeeAlso allocPixels Allocator Pixel_Ref |
| 2194 | |
| 2195 | ## |
| 2196 | |
| 2197 | # ------------------------------------------------------------------------------ |
| 2198 | |
| 2199 | #Method SkPixelRef* pixelRef() const |
| 2200 | |
| 2201 | Returns Pixel_Ref, which contains: pixel base address; its dimensions; and |
| 2202 | rowBytes, the interval from one row to the next. Does not change Pixel_Ref |
| 2203 | reference count. Pixel_Ref may be shared by multiple bitmaps. |
| 2204 | If Pixel_Ref has not been set, returns nullptr. |
| 2205 | |
| 2206 | #Return Pixel_Ref, or nullptr ## |
| 2207 | |
| 2208 | #Example |
| 2209 | #Image 3 |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2210 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2215 | ## |
| 2216 | |
| 2217 | #SeeAlso getPixels getAddr |
| 2218 | |
| 2219 | ## |
| 2220 | |
| 2221 | # ------------------------------------------------------------------------------ |
| 2222 | |
| 2223 | #Method SkIPoint pixelRefOrigin() const |
| 2224 | |
| 2225 | Returns origin of pixels within Pixel_Ref. Bitmap bounds is always contained |
| 2226 | by Pixel_Ref bounds, which may be the same size or larger. Multiple Bitmaps |
| 2227 | can share the same Pixel_Ref, where each Bitmap has different bounds. |
| 2228 | |
| 2229 | The returned origin added to Bitmap dimensions equals or is smaller than the |
| 2230 | Pixel_Ref dimensions. |
| 2231 | |
| 2232 | Returns (0, 0) if Pixel_Ref is nullptr. |
| 2233 | |
| 2234 | #Return pixel origin within Pixel_Ref ## |
| 2235 | |
| 2236 | #Example |
| 2237 | #Image 3 |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2238 | 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 |
| 2245 | source origin: 0, 0 |
| 2246 | subset origin: 32, 64 |
| 2247 | ## |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2248 | ## |
| 2249 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2250 | #SeeAlso SkPixelRef getSubset setPixelRef |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2251 | |
| 2252 | ## |
| 2253 | |
| 2254 | # ------------------------------------------------------------------------------ |
| 2255 | |
| 2256 | #Method void setPixelRef(sk_sp<SkPixelRef> pixelRef, int dx, int dy) |
| 2257 | |
| 2258 | Replaces pixelRef and origin in Bitmap. dx and dy specify the offset |
| 2259 | within the Pixel_Ref pixels for the top-left corner of the bitmap. |
| 2260 | |
| 2261 | Asserts in debug builds if dx or dy are out of range. Pins dx and dy |
| 2262 | to legal range in release builds. |
| 2263 | |
| 2264 | The caller is responsible for ensuring that the pixels match the |
| 2265 | Color_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 |
| 2275 | Treating 32 bit data as 8 bit data is unlikely to produce useful results. |
| 2276 | ## |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2277 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2282 | ## |
| 2283 | |
| 2284 | #SeeAlso setInfo |
| 2285 | |
| 2286 | ## |
| 2287 | |
| 2288 | # ------------------------------------------------------------------------------ |
| 2289 | |
| 2290 | #Method bool readyToDraw() const |
| 2291 | |
| 2292 | Returns 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2299 | if (source.readyToDraw()) { |
| 2300 | canvas->drawBitmap(source, 10, 10); |
| 2301 | } |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2302 | ## |
| 2303 | |
| 2304 | #SeeAlso getPixels drawsNothing |
| 2305 | |
| 2306 | ## |
| 2307 | |
| 2308 | # ------------------------------------------------------------------------------ |
| 2309 | |
| 2310 | #Method uint32_t getGenerationID() const |
| 2311 | |
| 2312 | Returns a unique value corresponding to the pixels in Pixel_Ref. |
| 2313 | Returns a different value after notifyPixelsChanged has been called. |
| 2314 | Returns zero if Pixel_Ref is nullptr. |
| 2315 | |
| 2316 | Determines if pixels have changed since last examined. |
| 2317 | |
| 2318 | #Return unique value for pixels in Pixel_Ref ## |
| 2319 | |
| 2320 | #Example |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2321 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2326 | SkDebugf("erase id %u\n", bitmap.getGenerationID()); |
| 2327 | #StdOut |
| 2328 | #Volatile |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2329 | empty id 0 |
| 2330 | alloc id 4 |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2331 | erase id 6 |
| 2332 | ## |
| 2333 | ## |
| 2334 | |
| 2335 | #SeeAlso notifyPixelsChanged Pixel_Ref |
| 2336 | |
| 2337 | ## |
| 2338 | |
| 2339 | # ------------------------------------------------------------------------------ |
| 2340 | |
| 2341 | #Method void notifyPixelsChanged() const |
| 2342 | |
| 2343 | Marks that pixels in Pixel_Ref have changed. Subsequent calls to |
| 2344 | getGenerationID() return a different value. |
| 2345 | |
| 2346 | #Example |
| 2347 | #Height 20 |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2348 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2359 | ## |
| 2360 | |
| 2361 | #SeeAlso getGenerationID isVolatile Pixel_Ref |
| 2362 | |
| 2363 | ## |
| 2364 | |
| 2365 | # ------------------------------------------------------------------------------ |
| 2366 | |
| 2367 | #Method void eraseColor(SkColor c) const |
| 2368 | |
| 2369 | Replaces pixel values with c. All pixels contained by bounds() are affected. |
| 2370 | If the colorType is kGray_8_SkColorType or k565_SkColorType, then Color_Alpha |
| 2371 | is ignored; Color_RGB is treated as opaque. If colorType is kAlpha_8_SkColorType, |
| 2372 | then Color_RGB is ignored. |
| 2373 | |
| 2374 | #Param c Unpremultiplied Color ## |
| 2375 | |
| 2376 | #Example |
| 2377 | #Height 20 |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2378 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2383 | ## |
| 2384 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 2385 | #SeeAlso eraseARGB erase |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2386 | |
| 2387 | ## |
| 2388 | |
| 2389 | # ------------------------------------------------------------------------------ |
| 2390 | |
| 2391 | #Method void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const |
| 2392 | |
| 2393 | Replaces pixel values with Unpremultiplied Color built from a, r, g, and b. |
| 2394 | All pixels contained by bounds() are affected. |
| 2395 | If the colorType is kGray_8_SkColorType or k565_SkColorType, then a |
| 2396 | is ignored; r, g, and b are treated as opaque. If colorType is kAlpha_8_SkColorType, |
| 2397 | then 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2406 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2412 | ## |
| 2413 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 2414 | #SeeAlso eraseColor erase |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2415 | |
| 2416 | ## |
| 2417 | |
| 2418 | # ------------------------------------------------------------------------------ |
| 2419 | |
| 2420 | #Method void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const |
| 2421 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 2422 | Deprecated. Use eraseARGB or eraseColor. |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2423 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 2424 | #Param r amount of red ## |
| 2425 | #Param g amount of green ## |
| 2426 | #Param b amount of blue ## |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2427 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 2428 | #NoExample |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2429 | ## |
| 2430 | |
| 2431 | #SeeAlso eraseColor eraseARGB erase |
| 2432 | |
| 2433 | ## |
| 2434 | |
| 2435 | # ------------------------------------------------------------------------------ |
| 2436 | |
| 2437 | #Method void erase(SkColor c, const SkIRect& area) const |
| 2438 | |
| 2439 | Replaces pixel values inside area with c. If area does not intersect bounds(), |
| 2440 | call has no effect. |
| 2441 | |
| 2442 | If the colorType is kGray_8_SkColorType or k565_SkColorType, then Color_Alpha |
| 2443 | is ignored; Color_RGB is treated as opaque. If colorType is kAlpha_8_SkColorType, |
| 2444 | then Color_RGB is ignored. |
| 2445 | |
| 2446 | #Param c Unpremultiplied Color ## |
| 2447 | #Param area rectangle to fill ## |
| 2448 | |
| 2449 | #Example |
| 2450 | #Height 70 |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2451 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2460 | |
| 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 | |
| 2471 | Legacy call to be deprecated. |
| 2472 | |
| 2473 | #Deprecated |
| 2474 | ## |
| 2475 | |
| 2476 | ## |
| 2477 | |
| 2478 | # ------------------------------------------------------------------------------ |
| 2479 | |
| 2480 | #Method SkColor getColor(int x, int y) const |
| 2481 | |
| 2482 | Returns pixel at (x, y) as Unpremultiplied Color. |
| 2483 | Returns black with Alpha if Color_Type is kAlpha_8_SkColorType. |
| 2484 | |
| 2485 | Input is not validated: out of bounds values of x or y trigger an assert() if |
| 2486 | built with SK_DEBUG defined; and returns undefined values or may crash if |
| 2487 | SK_RELEASE is defined. Fails if Color_Type is kUnknown_SkColorType or |
| 2488 | pixel address is nullptr. |
| 2489 | |
| 2490 | Color_Space in Image_Info is ignored. Some Color precision may be lost in the |
| 2491 | conversion to Unpremultiplied Color; original pixel data may have additional |
| 2492 | precision. |
| 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 |
| 2526 | Premultiplied: |
| 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 |
| 2531 | Unpremultiplied: |
| 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 | |
| 2547 | Returns pixel address at (x, y). |
| 2548 | |
| 2549 | Input is not validated: out of bounds values of x or y, or kUnknown_SkColorType, |
| 2550 | trigger an assert() if built with SK_DEBUG defined. Returns nullptr if |
| 2551 | Color_Type is kUnknown_SkColorType, or Pixel_Ref is nullptr. |
| 2552 | |
| 2553 | Performs a lookup of pixel size; for better performance, call |
| 2554 | one 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2563 | 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 |
| 2567 | addr interval == rowBytes |
| 2568 | ## |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2569 | ## |
| 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 | |
| 2579 | Returns address at (x, y). |
| 2580 | |
| 2581 | Input 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2596 | 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 |
| 2601 | addr interval == rowBytes |
| 2602 | ## |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2603 | ## |
| 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 | |
| 2613 | Returns address at (x, y). |
| 2614 | |
| 2615 | Input 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2630 | 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 |
| 2641 | addr interval == rowBytes |
| 2642 | ## |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2643 | ## |
| 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 | |
| 2653 | Returns address at (x, y). |
| 2654 | |
| 2655 | Input 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2669 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2678 | #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 | |
| 2691 | Shares Pixel_Ref with dst. Pixels are not copied; Bitmap and dst point |
| 2692 | to the same pixels; dst bounds() are set to the intersection of subset |
| 2693 | and the original bounds(). |
| 2694 | |
| 2695 | subset may be larger than bounds(). Any area outside of bounds() is ignored. |
| 2696 | |
| 2697 | Any contents of dst are discarded. isVolatile setting is copied to dst. |
| 2698 | dst is set to colorType, alphaType, and colorSpace. |
| 2699 | |
| 2700 | Return 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2716 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2733 | #StdOut |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2734 | bounds: 0, 0, 512, 512 |
| 2735 | subset: -100, 100, 0, 200 success; false |
| 2736 | subset: -100, 100, 100, 200 success; true subset: 0, 0, 100, 100 |
| 2737 | subset: -100, 100, 1000, 200 success; true subset: 0, 0, 512, 100 |
| 2738 | subset: 0, 100, 0, 200 success; false |
| 2739 | subset: 0, 100, 100, 200 success; true subset: 0, 0, 100, 100 |
| 2740 | subset: 0, 100, 1000, 200 success; true subset: 0, 0, 512, 100 |
| 2741 | subset: 100, 100, 0, 200 success; false |
| 2742 | subset: 100, 100, 100, 200 success; false |
| 2743 | subset: 100, 100, 1000, 200 success; true subset: 0, 0, 412, 100 |
| 2744 | subset: 1000, 100, 0, 200 success; false |
| 2745 | subset: 1000, 100, 100, 200 success; false |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2746 | subset: 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 | |
| 2759 | Copies a Rect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not exceed |
| 2760 | (this->width(), this->height()). |
| 2761 | |
| 2762 | dstInfo specifies width, height, Color_Type, Alpha_Type, and |
| 2763 | Color_Space of destination. dstRowBytes specifics the gap from one destination |
| 2764 | row 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 | |
| 2771 | Pixels are copied only if pixel conversion is possible. If this->colorType is |
| 2772 | kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match. |
| 2773 | If this->colorType is kGray_8_SkColorType, dstInfo.colorSpace must match. |
| 2774 | If this->alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must |
| 2775 | match. If this->colorSpace is nullptr, dstInfo.colorSpace must match. Returns |
| 2776 | false if pixel conversion is not possible. |
| 2777 | |
| 2778 | srcX and srcY may be negative to copy only top or left of source. Returns |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 2779 | false if width() or height() is zero or negative. |
| 2780 | Returns false if |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2781 | #Formula |
| 2782 | abs(srcX) >= this->width() |
| 2783 | ## |
| 2784 | , or if |
| 2785 | #Formula |
| 2786 | abs(srcY) >= this->height() |
| 2787 | ## |
| 2788 | . |
| 2789 | |
| 2790 | If behavior is SkTransferFunctionBehavior::kRespect: converts source |
| 2791 | pixels to a linear space before converting to dstInfo. |
| 2792 | If behavior is SkTransferFunctionBehavior::kIgnore: source |
| 2793 | pixels 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 |
| 2808 | void draw(SkCanvas* canvas) { |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2809 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2832 | } |
| 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 | |
| 2844 | Copies a Rect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not exceed |
| 2845 | (this->width(), this->height()). |
| 2846 | |
| 2847 | dstInfo specifies width, height, Color_Type, Alpha_Type, and |
| 2848 | Color_Space of destination. dstRowBytes specifics the gap from one destination |
| 2849 | row 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 | |
| 2856 | Pixels are copied only if pixel conversion is possible. If this->colorType is |
| 2857 | kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match. |
| 2858 | If this->colorType is kGray_8_SkColorType, dstInfo.colorSpace must match. |
| 2859 | If this->alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must |
| 2860 | match. If this->colorSpace is nullptr, dstInfo.colorSpace must match. Returns |
| 2861 | false if pixel conversion is not possible. |
| 2862 | |
| 2863 | srcX and srcY may be negative to copy only top or left of source. Returns |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 2864 | false if width() or height() is zero or negative. |
| 2865 | Returns false if |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2866 | #Formula |
| 2867 | abs(srcX) >= this->width() |
| 2868 | ## |
| 2869 | , or if |
| 2870 | #Formula |
| 2871 | abs(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 |
| 2886 | Transferring the gradient from 8 bits per component to 4 bits per component |
| 2887 | creates visible banding. |
| 2888 | ## |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2889 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2908 | 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 | |
| 2919 | Copies a Rect of pixels to dst. Copy starts at (srcX, srcY), and does not exceed |
| 2920 | (this->width(), this->height()). |
| 2921 | |
| 2922 | dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage, |
| 2923 | and row bytes of destination. dst.rowBytes specifics the gap from one destination |
| 2924 | row 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 | |
| 2931 | Pixels are copied only if pixel conversion is possible. If this->colorType is |
| 2932 | kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match. |
| 2933 | If this->colorType is kGray_8_SkColorType, dst Color_Space must match. |
| 2934 | If this->alphaType is kOpaque_SkAlphaType, dst Alpha_Type must |
| 2935 | match. If this->colorSpace is nullptr, dst Color_Space must match. Returns |
| 2936 | false if pixel conversion is not possible. |
| 2937 | |
| 2938 | srcX and srcY may be negative to copy only top or left of source. Returns |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 2939 | false if width() or height() is zero or negative. |
| 2940 | Returns false if |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2941 | #Formula |
| 2942 | abs(srcX) >= this->width() |
| 2943 | ## |
| 2944 | , or if |
| 2945 | #Formula |
| 2946 | abs(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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 2958 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2972 | 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 | |
| 2983 | Copies a Rect of pixels to dst. Copy starts at (0, 0), and does not exceed |
| 2984 | (this->width(), this->height()). |
| 2985 | |
| 2986 | dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage, |
| 2987 | and row bytes of destination. dst.rowBytes specifics the gap from one destination |
| 2988 | row 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 | |
| 2995 | Pixels are copied only if pixel conversion is possible. If this->colorType is |
| 2996 | kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match. |
| 2997 | If this->colorType is kGray_8_SkColorType, dst Color_Space must match. |
| 2998 | If this->alphaType is kOpaque_SkAlphaType, dst Alpha_Type must |
| 2999 | match. If this->colorSpace is nullptr, dst Color_Space must match. Returns |
| 3000 | false 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 3009 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3022 | ## |
| 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 | |
| 3032 | Copies a Rect of pixels from src. Copy starts at (dstX, dstY), and does not exceed |
| 3033 | (src.width(), src.height()). |
| 3034 | |
| 3035 | src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage, |
| 3036 | and row bytes of source. src.rowBytes specifics the gap from one source |
| 3037 | row 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 | |
| 3044 | Pixels are copied only if pixel conversion is possible. If this->colorType is |
| 3045 | kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match. |
| 3046 | If this->colorType is kGray_8_SkColorType, src Color_Space must match. |
| 3047 | If this->alphaType is kOpaque_SkAlphaType, src Alpha_Type must |
| 3048 | match. If this->colorSpace is nullptr, src Color_Space must match. Returns |
| 3049 | false if pixel conversion is not possible. |
| 3050 | |
| 3051 | dstX and dstY may be negative to copy only top or left of source. Returns |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 3052 | false if width() or height() is zero or negative. |
| 3053 | Returns false if |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3054 | #Formula |
| 3055 | abs(dstX) >= this->width() |
| 3056 | ## |
| 3057 | , or if |
| 3058 | #Formula |
| 3059 | abs(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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 3071 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3088 | } |
| 3089 | ## |
| 3090 | |
| 3091 | #SeeAlso readPixels |
| 3092 | |
| 3093 | ## |
| 3094 | |
| 3095 | # ------------------------------------------------------------------------------ |
| 3096 | |
| 3097 | #Method bool writePixels(const SkPixmap& src) |
| 3098 | |
| 3099 | Copies a Rect of pixels from src. Copy starts at (0, 0), and does not exceed |
| 3100 | (src.width(), src.height()). |
| 3101 | |
| 3102 | src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage, |
| 3103 | and row bytes of source. src.rowBytes specifics the gap from one source |
| 3104 | row 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 | |
| 3111 | Pixels are copied only if pixel conversion is possible. If this->colorType is |
| 3112 | kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match. |
| 3113 | If this->colorType is kGray_8_SkColorType, src Color_Space must match. |
| 3114 | If this->alphaType is kOpaque_SkAlphaType, src Alpha_Type must |
| 3115 | match. If this->colorSpace is nullptr, src Color_Space must match. Returns |
| 3116 | false 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 3124 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3132 | ## |
| 3133 | |
| 3134 | #SeeAlso readPixels |
| 3135 | |
| 3136 | ## |
| 3137 | |
| 3138 | # ------------------------------------------------------------------------------ |
| 3139 | |
| 3140 | #Method bool writePixels(const SkPixmap& src, int x, int y, SkTransferFunctionBehavior behavior) |
| 3141 | |
| 3142 | Copies a Rect of pixels from src. Copy starts at (0, 0), and does not exceed |
| 3143 | (src.width(), src.height()). |
| 3144 | |
| 3145 | src specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage, |
| 3146 | and row bytes of source. src.rowBytes specifics the gap from one source |
| 3147 | row 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 | |
| 3154 | Pixels are copied only if pixel conversion is possible. If this->colorType is |
| 3155 | kGray_8_SkColorType, or kAlpha_8_SkColorType; src Color_Type must match. |
| 3156 | If this->colorType is kGray_8_SkColorType, src Color_Space must match. |
| 3157 | If this->alphaType is kOpaque_SkAlphaType, src Alpha_Type must |
| 3158 | match. If this->colorSpace is nullptr, src Color_Space must match. Returns |
| 3159 | false if pixel conversion is not possible. Returns false if width() or height() |
| 3160 | is zero or negative. |
| 3161 | |
| 3162 | If behavior is SkTransferFunctionBehavior::kRespect: converts src |
| 3163 | pixels to a linear space before converting to Image_Info. |
| 3164 | If behavior is SkTransferFunctionBehavior::kIgnore: src |
| 3165 | pixels 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 3178 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3199 | } |
| 3200 | ## |
| 3201 | |
| 3202 | #SeeAlso readPixels |
| 3203 | |
| 3204 | ## |
| 3205 | |
| 3206 | # ------------------------------------------------------------------------------ |
| 3207 | |
| 3208 | #Method bool hasHardwareMipMap() const |
| 3209 | |
| 3210 | #Private |
| 3211 | Android 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 |
| 3228 | Android 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 | |
| 3244 | Sets dst to Alpha described by pixels. Returns false if dst cannot be written to |
| 3245 | or dst pixels cannot be allocated. |
| 3246 | |
| 3247 | Uses 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 3255 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3269 | 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 | |
| 3281 | Sets dst to Alpha described by pixels. Returns false if dst cannot be written to |
| 3282 | or dst pixels cannot be allocated. |
| 3283 | |
| 3284 | If paint is not nullptr and contains Mask_Filter, SkMaskFilter::filterMask |
| 3285 | generates Mask_Alpha from Bitmap. Uses HeapAllocator to reserve memory for dst |
| 3286 | Pixel_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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 3298 | SkBitmap alpha, bitmap; |
| 3299 | bitmap.allocN32Pixels(100, 100); |
| 3300 | SkCanvas offscreen(bitmap); |
| 3301 | offscreen.clear(0); |
| 3302 | SkPaint paint; |
| 3303 | paint.setAntiAlias(true); |
| 3304 | paint.setColor(SK_ColorBLUE); |
| 3305 | paint.setStyle(SkPaint::kStroke_Style); |
| 3306 | paint.setStrokeWidth(20); |
| 3307 | offscreen.drawCircle(50, 50, 39, paint); |
| 3308 | offscreen.flush(); |
| 3309 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3317 | ## |
| 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 | |
| 3328 | Sets dst to Alpha described by pixels. Returns false if dst cannot be written to |
| 3329 | or dst pixels cannot be allocated. |
| 3330 | |
| 3331 | If paint is not nullptr and contains Mask_Filter, SkMaskFilter::filterMask |
| 3332 | generates Mask_Alpha from Bitmap. allocator may reference a custom allocation |
| 3333 | class or be set to nullptr to use HeapAllocator. Sets offset to top-left |
| 3334 | position for dst for alignment with Bitmap; (0, 0) unless SkMaskFilter generates |
| 3335 | mask. |
| 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 3347 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3358 | paint.setMaskFilter(SkBlurMaskFilter::Make(kOuter_SkBlurStyle, 3)); |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 3359 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3364 | ## |
| 3365 | |
| 3366 | #SeeAlso extractSubset |
| 3367 | |
| 3368 | ## |
| 3369 | |
| 3370 | # ------------------------------------------------------------------------------ |
| 3371 | |
| 3372 | #Method bool peekPixels(SkPixmap* pixmap) const |
| 3373 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 3374 | Copies Bitmap pixel address, row bytes, and Image_Info to pixmap, if address |
| 3375 | is available, and returns true. If pixel address is not available, return |
| 3376 | false and leave pixmap unchanged. |
| 3377 | |
| 3378 | pixmap contents become invalid on any future change to Bitmap. |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3379 | |
| 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 Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 3385 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3402 | #StdOut |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 3403 | ------ |
| 3404 | --xxx- |
| 3405 | -x--x- |
| 3406 | ----x- |
| 3407 | ---xx- |
| 3408 | --xx-- |
| 3409 | --x--- |
| 3410 | ------ |
| 3411 | --x--- |
| 3412 | --x--- |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3413 | ------ |
| 3414 | #StdOut ## |
| 3415 | ## |
| 3416 | |
| 3417 | #SeeAlso installPixels readPixels writePixels |
| 3418 | |
| 3419 | ## |
| 3420 | |
| 3421 | # ------------------------------------------------------------------------------ |
| 3422 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 3423 | #Method void validate() const; |
| 3424 | |
| 3425 | Asserts if internal values are illegal or inconsistent. Only available if |
| 3426 | SK_DEBUG is defined at compile time. |
| 3427 | |
| 3428 | #NoExample |
| 3429 | ## |
| 3430 | |
| 3431 | #SeeAlso SkImageInfo::validate() |
| 3432 | |
| 3433 | ## |
| 3434 | |
| 3435 | # ------------------------------------------------------------------------------ |
| 3436 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3437 | #Method void toString(SkString* str) const; |
| 3438 | |
| 3439 | #DefinedBy SK_TO_STRING_NONVIRT() ## |
| 3440 | |
| 3441 | #Private |
| 3442 | macro expands to: void toString(SkString* str) const; |
| 3443 | ## |
| 3444 | |
| 3445 | Creates string representation. The representation is read by |
| 3446 | internal debugging tools. The interface and implementation may be |
| 3447 | suppressed by defining SK_IGNORE_TO_STRING. |
| 3448 | |
| 3449 | #Param str storage for string representation ## |
| 3450 | |
| 3451 | #Example |
Ben Wagner | 29380bd | 2017-10-09 14:43:00 -0400 | [diff] [blame] | 3452 | 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 Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3464 | } |
| 3465 | #StdOut |
| 3466 | bitmap dimensions (6, 11) |
| 3467 | ## |
| 3468 | ## |
| 3469 | |
| 3470 | #SeeAlso SkPaint::toString |
| 3471 | |
| 3472 | ## |
| 3473 | |
| 3474 | #Class SkBitmap ## |
| 3475 | |
| 3476 | #Topic Bitmap ## |