| 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkBitmap_DEFINED |
| 9 | #define SkBitmap_DEFINED |
| 10 | |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkImageInfo.h" |
| 13 | #include "include/core/SkMatrix.h" |
| 14 | #include "include/core/SkPixmap.h" |
| 15 | #include "include/core/SkPoint.h" |
| 16 | #include "include/core/SkRefCnt.h" |
| 17 | #include "include/core/SkTileMode.h" |
| 18 | |
| 19 | struct SkMask; |
| 20 | class SkMipmap; |
| 21 | struct SkIRect; |
| 22 | struct SkRect; |
| 23 | class SkPaint; |
| 24 | class SkPixelRef; |
| 25 | class SkShader; |
| 26 | |
| 27 | /** \class SkBitmap |
| 28 | SkBitmap describes a two-dimensional raster pixel array. SkBitmap is built on |
| 29 | SkImageInfo, containing integer width and height, SkColorType and SkAlphaType |
| 30 | describing the pixel format, and SkColorSpace describing the range of colors. |
| 31 | SkBitmap points to SkPixelRef, which describes the physical array of pixels. |
| 32 | SkImageInfo bounds may be located anywhere fully inside SkPixelRef bounds. |
| 33 | |
| 34 | SkBitmap can be drawn using SkCanvas. SkBitmap can be a drawing destination for SkCanvas |
| 35 | draw member functions. SkBitmap flexibility as a pixel container limits some |
| 36 | optimizations available to the target platform. |
| 37 | |
| 38 | If pixel array is primarily read-only, use SkImage for better performance. |
| 39 | If pixel array is primarily written to, use SkSurface for better performance. |
| 40 | |
| 41 | Declaring SkBitmap const prevents altering SkImageInfo: the SkBitmap height, width, |
| 42 | and so on cannot change. It does not affect SkPixelRef: a caller may write its |
| 43 | pixels. Declaring SkBitmap const affects SkBitmap configuration, not its contents. |
| 44 | |
| 45 | SkBitmap is not thread safe. Each thread must have its own copy of SkBitmap fields, |
| 46 | although threads may share the underlying pixel array. |
| 47 | */ |
| 48 | class SK_API SkBitmap { |
| 49 | public: |
| 50 | class SK_API Allocator; |
| 51 | |
| 52 | /** Creates an empty SkBitmap without pixels, with kUnknown_SkColorType, |
| 53 | kUnknown_SkAlphaType, and with a width and height of zero. SkPixelRef origin is |
| 54 | set to (0, 0). |
| 55 | |
| 56 | Use setInfo() to associate SkColorType, SkAlphaType, width, and height |
| 57 | after SkBitmap has been created. |
| 58 | |
| 59 | @return empty SkBitmap |
| 60 | |
| 61 | example: https://fiddle.skia.org/c/@Bitmap_empty_constructor |
| 62 | */ |
| 63 | SkBitmap(); |
| 64 | |
| 65 | /** Copies settings from src to returned SkBitmap. Shares pixels if src has pixels |
| 66 | allocated, so both bitmaps reference the same pixels. |
| 67 | |
| 68 | @param src SkBitmap to copy SkImageInfo, and share SkPixelRef |
| 69 | @return copy of src |
| 70 | |
| 71 | example: https://fiddle.skia.org/c/@Bitmap_copy_const_SkBitmap |
| 72 | */ |
| 73 | SkBitmap(const SkBitmap& src); |
| 74 | |
| 75 | /** Copies settings from src to returned SkBitmap. Moves ownership of src pixels to |
| 76 | SkBitmap. |
| 77 | |
| 78 | @param src SkBitmap to copy SkImageInfo, and reassign SkPixelRef |
| 79 | @return copy of src |
| 80 | |
| 81 | example: https://fiddle.skia.org/c/@Bitmap_move_SkBitmap |
| 82 | */ |
| 83 | SkBitmap(SkBitmap&& src); |
| 84 | |
| 85 | /** Decrements SkPixelRef reference count, if SkPixelRef is not nullptr. |
| 86 | */ |
| 87 | ~SkBitmap(); |
| 88 | |
| 89 | /** Copies settings from src to returned SkBitmap. Shares pixels if src has pixels |
| 90 | allocated, so both bitmaps reference the same pixels. |
| 91 | |
| 92 | @param src SkBitmap to copy SkImageInfo, and share SkPixelRef |
| 93 | @return copy of src |
| 94 | |
| 95 | example: https://fiddle.skia.org/c/@Bitmap_copy_operator |
| 96 | */ |
| 97 | SkBitmap& operator=(const SkBitmap& src); |
| 98 | |
| 99 | /** Copies settings from src to returned SkBitmap. Moves ownership of src pixels to |
| 100 | SkBitmap. |
| 101 | |
| 102 | @param src SkBitmap to copy SkImageInfo, and reassign SkPixelRef |
| 103 | @return copy of src |
| 104 | |
| 105 | example: https://fiddle.skia.org/c/@Bitmap_move_operator |
| 106 | */ |
| 107 | SkBitmap& operator=(SkBitmap&& src); |
| 108 | |
| 109 | /** Swaps the fields of the two bitmaps. |
| 110 | |
| 111 | @param other SkBitmap exchanged with original |
| 112 | |
| 113 | example: https://fiddle.skia.org/c/@Bitmap_swap |
| 114 | */ |
| 115 | void swap(SkBitmap& other); |
| 116 | |
| 117 | /** Returns a constant reference to the SkPixmap holding the SkBitmap pixel |
| 118 | address, row bytes, and SkImageInfo. |
| 119 | |
| 120 | @return reference to SkPixmap describing this SkBitmap |
| 121 | */ |
| 122 | const SkPixmap& pixmap() const { return fPixmap; } |
| 123 | |
| 124 | /** Returns width, height, SkAlphaType, SkColorType, and SkColorSpace. |
| 125 | |
| 126 | @return reference to SkImageInfo |
| 127 | */ |
| 128 | const SkImageInfo& info() const { return fPixmap.info(); } |
| 129 | |
| 130 | /** Returns pixel count in each row. Should be equal or less than |
| 131 | rowBytes() / info().bytesPerPixel(). |
| 132 | |
| 133 | May be less than pixelRef().width(). Will not exceed pixelRef().width() less |
| 134 | pixelRefOrigin().fX. |
| 135 | |
| 136 | @return pixel width in SkImageInfo |
| 137 | */ |
| 138 | int width() const { return fPixmap.width(); } |
| 139 | |
| 140 | /** Returns pixel row count. |
| 141 | |
| 142 | Maybe be less than pixelRef().height(). Will not exceed pixelRef().height() less |
| 143 | pixelRefOrigin().fY. |
| 144 | |
| 145 | @return pixel height in SkImageInfo |
| 146 | */ |
| 147 | int height() const { return fPixmap.height(); } |
| 148 | |
| 149 | SkColorType colorType() const { return fPixmap.colorType(); } |
| 150 | |
| 151 | SkAlphaType alphaType() const { return fPixmap.alphaType(); } |
| 152 | |
| 153 | /** Returns SkColorSpace, the range of colors, associated with SkImageInfo. The |
| 154 | reference count of SkColorSpace is unchanged. The returned SkColorSpace is |
| 155 | immutable. |
| 156 | |
| 157 | @return SkColorSpace in SkImageInfo, or nullptr |
| 158 | */ |
| 159 | SkColorSpace* colorSpace() const { return fPixmap.colorSpace(); } |
| 160 | |
| 161 | /** Returns smart pointer to SkColorSpace, the range of colors, associated with |
| 162 | SkImageInfo. The smart pointer tracks the number of objects sharing this |
| 163 | SkColorSpace reference so the memory is released when the owners destruct. |
| 164 | |
| 165 | The returned SkColorSpace is immutable. |
| 166 | |
| 167 | @return SkColorSpace in SkImageInfo wrapped in a smart pointer |
| 168 | */ |
| 169 | sk_sp<SkColorSpace> refColorSpace() const { return fPixmap.info().refColorSpace(); } |
| 170 | |
| 171 | /** Returns number of bytes per pixel required by SkColorType. |
| 172 | Returns zero if colorType( is kUnknown_SkColorType. |
| 173 | |
| 174 | @return bytes in pixel |
| 175 | */ |
| 176 | int bytesPerPixel() const { return fPixmap.info().bytesPerPixel(); } |
| 177 | |
| 178 | /** Returns number of pixels that fit on row. Should be greater than or equal to |
| 179 | width(). |
| 180 | |
| 181 | @return maximum pixels per row |
| 182 | */ |
| 183 | int rowBytesAsPixels() const { return fPixmap.rowBytesAsPixels(); } |
| 184 | |
| 185 | /** Returns bit shift converting row bytes to row pixels. |
| 186 | Returns zero for kUnknown_SkColorType. |
| 187 | |
| 188 | @return one of: 0, 1, 2, 3; left shift to convert pixels to bytes |
| 189 | */ |
| 190 | int shiftPerPixel() const { return fPixmap.shiftPerPixel(); } |
| 191 | |
| 192 | /** Returns true if either width() or height() are zero. |
| 193 | |
| 194 | Does not check if SkPixelRef is nullptr; call drawsNothing() to check width(), |
| 195 | height(), and SkPixelRef. |
| 196 | |
| 197 | @return true if dimensions do not enclose area |
| 198 | */ |
| 199 | bool empty() const { return fPixmap.info().isEmpty(); } |
| 200 | |
| 201 | /** Returns true if SkPixelRef is nullptr. |
| 202 | |
| 203 | Does not check if width() or height() are zero; call drawsNothing() to check |
| 204 | width(), height(), and SkPixelRef. |
| 205 | |
| 206 | @return true if no SkPixelRef is associated |
| 207 | */ |
| 208 | bool isNull() const { return nullptr == fPixelRef; } |
| 209 | |
| 210 | /** Returns true if width() or height() are zero, or if SkPixelRef is nullptr. |
| 211 | If true, SkBitmap has no effect when drawn or drawn into. |
| 212 | |
| 213 | @return true if drawing has no effect |
| 214 | */ |
| 215 | bool drawsNothing() const { |
| 216 | return this->empty() || this->isNull(); |
| 217 | } |
| 218 | |
| 219 | /** Returns row bytes, the interval from one pixel row to the next. Row bytes |
| 220 | is at least as large as: width() * info().bytesPerPixel(). |
| 221 | |
| 222 | Returns zero if colorType() is kUnknown_SkColorType, or if row bytes supplied to |
| 223 | setInfo() is not large enough to hold a row of pixels. |
| 224 | |
| 225 | @return byte length of pixel row |
| 226 | */ |
| 227 | size_t rowBytes() const { return fPixmap.rowBytes(); } |
| 228 | |
| 229 | /** Sets SkAlphaType, if alphaType is compatible with SkColorType. |
| 230 | Returns true unless alphaType is kUnknown_SkAlphaType and current SkAlphaType |
| 231 | is not kUnknown_SkAlphaType. |
| 232 | |
| 233 | Returns true if SkColorType is kUnknown_SkColorType. alphaType is ignored, and |
| 234 | SkAlphaType remains kUnknown_SkAlphaType. |
| 235 | |
| 236 | Returns true if SkColorType is kRGB_565_SkColorType or kGray_8_SkColorType. |
| 237 | alphaType is ignored, and SkAlphaType remains kOpaque_SkAlphaType. |
| 238 | |
| 239 | If SkColorType is kARGB_4444_SkColorType, kRGBA_8888_SkColorType, |
| 240 | kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: returns true unless |
| 241 | alphaType is kUnknown_SkAlphaType and SkAlphaType is not kUnknown_SkAlphaType. |
| 242 | If SkAlphaType is kUnknown_SkAlphaType, alphaType is ignored. |
| 243 | |
| 244 | If SkColorType is kAlpha_8_SkColorType, returns true unless |
| 245 | alphaType is kUnknown_SkAlphaType and SkAlphaType is not kUnknown_SkAlphaType. |
| 246 | If SkAlphaType is kUnknown_SkAlphaType, alphaType is ignored. If alphaType is |
| 247 | kUnpremul_SkAlphaType, it is treated as kPremul_SkAlphaType. |
| 248 | |
| 249 | This changes SkAlphaType in SkPixelRef; all bitmaps sharing SkPixelRef |
| 250 | are affected. |
| 251 | |
| 252 | @return true if SkAlphaType is set |
| 253 | |
| 254 | example: https://fiddle.skia.org/c/@Bitmap_setAlphaType |
| 255 | */ |
| 256 | bool setAlphaType(SkAlphaType alphaType); |
| 257 | |
| 258 | /** Returns pixel address, the base address corresponding to the pixel origin. |
| 259 | |
| 260 | @return pixel address |
| 261 | */ |
| 262 | void* getPixels() const { return fPixmap.writable_addr(); } |
| 263 | |
| 264 | /** Returns minimum memory required for pixel storage. |
| 265 | Does not include unused memory on last row when rowBytesAsPixels() exceeds width(). |
| 266 | Returns SIZE_MAX if result does not fit in size_t. |
| 267 | Returns zero if height() or width() is 0. |
| 268 | Returns height() times rowBytes() if colorType() is kUnknown_SkColorType. |
| 269 | |
| 270 | @return size in bytes of image buffer |
| 271 | */ |
| 272 | size_t computeByteSize() const { return fPixmap.computeByteSize(); } |
| 273 | |
| 274 | /** Returns true if pixels can not change. |
| 275 | |
| 276 | Most immutable SkBitmap checks trigger an assert only on debug builds. |
| 277 | |
| 278 | @return true if pixels are immutable |
| 279 | |
| 280 | example: https://fiddle.skia.org/c/@Bitmap_isImmutable |
| 281 | */ |
| 282 | bool isImmutable() const; |
| 283 | |
| 284 | /** Sets internal flag to mark SkBitmap as immutable. Once set, pixels can not change. |
| 285 | Any other bitmap sharing the same SkPixelRef are also marked as immutable. |
| 286 | Once SkPixelRef is marked immutable, the setting cannot be cleared. |
| 287 | |
| 288 | Writing to immutable SkBitmap pixels triggers an assert on debug builds. |
| 289 | |
| 290 | example: https://fiddle.skia.org/c/@Bitmap_setImmutable |
| 291 | */ |
| 292 | void setImmutable(); |
| 293 | |
| 294 | /** Returns true if SkAlphaType is set to hint that all pixels are opaque; their |
| 295 | alpha value is implicitly or explicitly 1.0. If true, and all pixels are |
| 296 | not opaque, Skia may draw incorrectly. |
| 297 | |
| 298 | Does not check if SkColorType allows alpha, or if any pixel value has |
| 299 | transparency. |
| 300 | |
| 301 | @return true if SkImageInfo SkAlphaType is kOpaque_SkAlphaType |
| 302 | */ |
| 303 | bool isOpaque() const { |
| 304 | return SkAlphaTypeIsOpaque(this->alphaType()); |
| 305 | } |
| 306 | |
| 307 | /** Resets to its initial state; all fields are set to zero, as if SkBitmap had |
| 308 | been initialized by SkBitmap(). |
| 309 | |
| 310 | Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to |
| 311 | kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType. |
| 312 | |
| 313 | If SkPixelRef is allocated, its reference count is decreased by one, releasing |
| 314 | its memory if SkBitmap is the sole owner. |
| 315 | |
| 316 | example: https://fiddle.skia.org/c/@Bitmap_reset |
| 317 | */ |
| 318 | void reset(); |
| 319 | |
| 320 | /** Returns true if all pixels are opaque. SkColorType determines how pixels |
| 321 | are encoded, and whether pixel describes alpha. Returns true for SkColorType |
| 322 | without alpha in each pixel; for other SkColorType, returns true if all |
| 323 | pixels have alpha values equivalent to 1.0 or greater. |
| 324 | |
| 325 | For SkColorType kRGB_565_SkColorType or kGray_8_SkColorType: always |
| 326 | returns true. For SkColorType kAlpha_8_SkColorType, kBGRA_8888_SkColorType, |
| 327 | kRGBA_8888_SkColorType: returns true if all pixel alpha values are 255. |
| 328 | For SkColorType kARGB_4444_SkColorType: returns true if all pixel alpha values are 15. |
| 329 | For kRGBA_F16_SkColorType: returns true if all pixel alpha values are 1.0 or |
| 330 | greater. |
| 331 | |
| 332 | Returns false for kUnknown_SkColorType. |
| 333 | |
| 334 | @param bm SkBitmap to check |
| 335 | @return true if all pixels have opaque values or SkColorType is opaque |
| 336 | */ |
| 337 | static bool ComputeIsOpaque(const SkBitmap& bm) { |
| 338 | return bm.pixmap().computeIsOpaque(); |
| 339 | } |
| 340 | |
| 341 | /** Returns SkRect { 0, 0, width(), height() }. |
| 342 | |
| 343 | @param bounds container for floating point rectangle |
| 344 | |
| 345 | example: https://fiddle.skia.org/c/@Bitmap_getBounds |
| 346 | */ |
| 347 | void getBounds(SkRect* bounds) const; |
| 348 | |
| 349 | /** Returns SkIRect { 0, 0, width(), height() }. |
| 350 | |
| 351 | @param bounds container for integral rectangle |
| 352 | |
| 353 | example: https://fiddle.skia.org/c/@Bitmap_getBounds_2 |
| 354 | */ |
| 355 | void getBounds(SkIRect* bounds) const; |
| 356 | |
| 357 | /** Returns SkIRect { 0, 0, width(), height() }. |
| 358 | |
| 359 | @return integral rectangle from origin to width() and height() |
| 360 | */ |
| 361 | SkIRect bounds() const { return fPixmap.info().bounds(); } |
| 362 | |
| 363 | /** Returns SkISize { width(), height() }. |
| 364 | |
| 365 | @return integral size of width() and height() |
| 366 | */ |
| 367 | SkISize dimensions() const { return fPixmap.info().dimensions(); } |
| 368 | |
| 369 | /** Returns the bounds of this bitmap, offset by its SkPixelRef origin. |
| 370 | |
| 371 | @return bounds within SkPixelRef bounds |
| 372 | */ |
| 373 | SkIRect getSubset() const { |
| 374 | SkIPoint origin = this->pixelRefOrigin(); |
| 375 | return SkIRect::MakeXYWH(origin.x(), origin.y(), this->width(), this->height()); |
| 376 | } |
| 377 | |
| 378 | /** Sets width, height, SkAlphaType, SkColorType, SkColorSpace, and optional |
| 379 | rowBytes. Frees pixels, and returns true if successful. |
| 380 | |
| 381 | imageInfo.alphaType() may be altered to a value permitted by imageInfo.colorSpace(). |
| 382 | If imageInfo.colorType() is kUnknown_SkColorType, imageInfo.alphaType() is |
| 383 | set to kUnknown_SkAlphaType. |
| 384 | If imageInfo.colorType() is kAlpha_8_SkColorType and imageInfo.alphaType() is |
| 385 | kUnpremul_SkAlphaType, imageInfo.alphaType() is replaced by kPremul_SkAlphaType. |
| 386 | If imageInfo.colorType() is kRGB_565_SkColorType or kGray_8_SkColorType, |
| 387 | imageInfo.alphaType() is set to kOpaque_SkAlphaType. |
| 388 | If imageInfo.colorType() is kARGB_4444_SkColorType, kRGBA_8888_SkColorType, |
| 389 | kBGRA_8888_SkColorType, or kRGBA_F16_SkColorType: imageInfo.alphaType() remains |
| 390 | unchanged. |
| 391 | |
| 392 | rowBytes must equal or exceed imageInfo.minRowBytes(). If imageInfo.colorSpace() is |
| 393 | kUnknown_SkColorType, rowBytes is ignored and treated as zero; for all other |
| 394 | SkColorSpace values, rowBytes of zero is treated as imageInfo.minRowBytes(). |
| 395 | |
| 396 | Calls reset() and returns false if: |
| 397 | - rowBytes exceeds 31 bits |
| 398 | - imageInfo.width() is negative |
| 399 | - imageInfo.height() is negative |
| 400 | - rowBytes is positive and less than imageInfo.width() times imageInfo.bytesPerPixel() |
| 401 | |
| 402 | @param imageInfo contains width, height, SkAlphaType, SkColorType, SkColorSpace |
| 403 | @param rowBytes imageInfo.minRowBytes() or larger; or zero |
| 404 | @return true if SkImageInfo set successfully |
| 405 | |
| 406 | example: https://fiddle.skia.org/c/@Bitmap_setInfo |
| 407 | */ |
| 408 | bool setInfo(const SkImageInfo& imageInfo, size_t rowBytes = 0); |
| 409 | |
| 410 | /** \enum SkBitmap::AllocFlags |
| 411 | AllocFlags is obsolete. We always zero pixel memory when allocated. |
| 412 | */ |
| 413 | enum AllocFlags { |
| 414 | kZeroPixels_AllocFlag = 1 << 0, //!< zero pixel memory. No effect. This is the default. |
| 415 | }; |
| 416 | |
| 417 | /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel |
| 418 | memory. Memory is zeroed. |
| 419 | |
| 420 | Returns false and calls reset() if SkImageInfo could not be set, or memory could |
| 421 | not be allocated, or memory could not optionally be zeroed. |
| 422 | |
| 423 | On most platforms, allocating pixel memory may succeed even though there is |
| 424 | not sufficient memory to hold pixels; allocation does not take place |
| 425 | until the pixels are written to. The actual behavior depends on the platform |
| 426 | implementation of calloc(). |
| 427 | |
| 428 | @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace |
| 429 | @param flags kZeroPixels_AllocFlag, or zero |
| 430 | @return true if pixels allocation is successful |
| 431 | */ |
| 432 | bool SK_WARN_UNUSED_RESULT tryAllocPixelsFlags(const SkImageInfo& info, uint32_t flags); |
| 433 | |
| 434 | /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel |
| 435 | memory. Memory is zeroed. |
| 436 | |
| 437 | Aborts execution if SkImageInfo could not be set, or memory could |
| 438 | not be allocated, or memory could not optionally |
| 439 | be zeroed. Abort steps may be provided by the user at compile time by defining |
| 440 | SK_ABORT. |
| 441 | |
| 442 | On most platforms, allocating pixel memory may succeed even though there is |
| 443 | not sufficient memory to hold pixels; allocation does not take place |
| 444 | until the pixels are written to. The actual behavior depends on the platform |
| 445 | implementation of calloc(). |
| 446 | |
| 447 | @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace |
| 448 | @param flags kZeroPixels_AllocFlag, or zero |
| 449 | |
| 450 | example: https://fiddle.skia.org/c/@Bitmap_allocPixelsFlags |
| 451 | */ |
| 452 | void allocPixelsFlags(const SkImageInfo& info, uint32_t flags); |
| 453 | |
| 454 | /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel |
| 455 | memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(), |
| 456 | or equal zero. Pass in zero for rowBytes to compute the minimum valid value. |
| 457 | |
| 458 | Returns false and calls reset() if SkImageInfo could not be set, or memory could |
| 459 | not be allocated. |
| 460 | |
| 461 | On most platforms, allocating pixel memory may succeed even though there is |
| 462 | not sufficient memory to hold pixels; allocation does not take place |
| 463 | until the pixels are written to. The actual behavior depends on the platform |
| 464 | implementation of malloc(). |
| 465 | |
| 466 | @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace |
| 467 | @param rowBytes size of pixel row or larger; may be zero |
| 468 | @return true if pixel storage is allocated |
| 469 | */ |
| 470 | bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info, size_t rowBytes); |
| 471 | |
| 472 | /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel |
| 473 | memory. rowBytes must equal or exceed info.width() times info.bytesPerPixel(), |
| 474 | or equal zero. Pass in zero for rowBytes to compute the minimum valid value. |
| 475 | |
| 476 | Aborts execution if SkImageInfo could not be set, or memory could |
| 477 | not be allocated. Abort steps may be provided by |
| 478 | the user at compile time by defining SK_ABORT. |
| 479 | |
| 480 | On most platforms, allocating pixel memory may succeed even though there is |
| 481 | not sufficient memory to hold pixels; allocation does not take place |
| 482 | until the pixels are written to. The actual behavior depends on the platform |
| 483 | implementation of malloc(). |
| 484 | |
| 485 | @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace |
| 486 | @param rowBytes size of pixel row or larger; may be zero |
| 487 | |
| 488 | example: https://fiddle.skia.org/c/@Bitmap_allocPixels |
| 489 | */ |
| 490 | void allocPixels(const SkImageInfo& info, size_t rowBytes); |
| 491 | |
| 492 | /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel |
| 493 | memory. |
| 494 | |
| 495 | Returns false and calls reset() if SkImageInfo could not be set, or memory could |
| 496 | not be allocated. |
| 497 | |
| 498 | On most platforms, allocating pixel memory may succeed even though there is |
| 499 | not sufficient memory to hold pixels; allocation does not take place |
| 500 | until the pixels are written to. The actual behavior depends on the platform |
| 501 | implementation of malloc(). |
| 502 | |
| 503 | @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace |
| 504 | @return true if pixel storage is allocated |
| 505 | */ |
| 506 | bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info) { |
| 507 | return this->tryAllocPixels(info, info.minRowBytes()); |
| 508 | } |
| 509 | |
| 510 | /** Sets SkImageInfo to info following the rules in setInfo() and allocates pixel |
| 511 | memory. |
| 512 | |
| 513 | Aborts execution if SkImageInfo could not be set, or memory could |
| 514 | not be allocated. Abort steps may be provided by |
| 515 | the user at compile time by defining SK_ABORT. |
| 516 | |
| 517 | On most platforms, allocating pixel memory may succeed even though there is |
| 518 | not sufficient memory to hold pixels; allocation does not take place |
| 519 | until the pixels are written to. The actual behavior depends on the platform |
| 520 | implementation of malloc(). |
| 521 | |
| 522 | @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace |
| 523 | |
| 524 | example: https://fiddle.skia.org/c/@Bitmap_allocPixels_2 |
| 525 | */ |
| 526 | void allocPixels(const SkImageInfo& info); |
| 527 | |
| 528 | /** Sets SkImageInfo to width, height, and native color type; and allocates |
| 529 | pixel memory. If isOpaque is true, sets SkImageInfo to kOpaque_SkAlphaType; |
| 530 | otherwise, sets to kPremul_SkAlphaType. |
| 531 | |
| 532 | Calls reset() and returns false if width exceeds 29 bits or is negative, |
| 533 | or height is negative. |
| 534 | |
| 535 | Returns false if allocation fails. |
| 536 | |
| 537 | Use to create SkBitmap that matches SkPMColor, the native pixel arrangement on |
| 538 | the platform. SkBitmap drawn to output device skips converting its pixel format. |
| 539 | |
| 540 | @param width pixel column count; must be zero or greater |
| 541 | @param height pixel row count; must be zero or greater |
| 542 | @param isOpaque true if pixels do not have transparency |
| 543 | @return true if pixel storage is allocated |
| 544 | */ |
| 545 | bool SK_WARN_UNUSED_RESULT tryAllocN32Pixels(int width, int height, bool isOpaque = false); |
| 546 | |
| 547 | /** Sets SkImageInfo to width, height, and the native color type; and allocates |
| 548 | pixel memory. If isOpaque is true, sets SkImageInfo to kOpaque_SkAlphaType; |
| 549 | otherwise, sets to kPremul_SkAlphaType. |
| 550 | |
| 551 | Aborts if width exceeds 29 bits or is negative, or height is negative, or |
| 552 | allocation fails. Abort steps may be provided by the user at compile time by |
| 553 | defining SK_ABORT. |
| 554 | |
| 555 | Use to create SkBitmap that matches SkPMColor, the native pixel arrangement on |
| 556 | the platform. SkBitmap drawn to output device skips converting its pixel format. |
| 557 | |
| 558 | @param width pixel column count; must be zero or greater |
| 559 | @param height pixel row count; must be zero or greater |
| 560 | @param isOpaque true if pixels do not have transparency |
| 561 | |
| 562 | example: https://fiddle.skia.org/c/@Bitmap_allocN32Pixels |
| 563 | */ |
| 564 | void allocN32Pixels(int width, int height, bool isOpaque = false); |
| 565 | |
| 566 | /** Sets SkImageInfo to info following the rules in setInfo(), and creates SkPixelRef |
| 567 | containing pixels and rowBytes. releaseProc, if not nullptr, is called |
| 568 | immediately on failure or when pixels are no longer referenced. context may be |
| 569 | nullptr. |
| 570 | |
| 571 | If SkImageInfo could not be set, or rowBytes is less than info.minRowBytes(): |
| 572 | calls releaseProc if present, calls reset(), and returns false. |
| 573 | |
| 574 | Otherwise, if pixels equals nullptr: sets SkImageInfo, calls releaseProc if |
| 575 | present, returns true. |
| 576 | |
| 577 | If SkImageInfo is set, pixels is not nullptr, and releaseProc is not nullptr: |
| 578 | when pixels are no longer referenced, calls releaseProc with pixels and context |
| 579 | as parameters. |
| 580 | |
| 581 | @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace |
| 582 | @param pixels address or pixel storage; may be nullptr |
| 583 | @param rowBytes size of pixel row or larger |
| 584 | @param releaseProc function called when pixels can be deleted; may be nullptr |
| 585 | @param context caller state passed to releaseProc; may be nullptr |
| 586 | @return true if SkImageInfo is set to info |
| 587 | */ |
| 588 | bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 589 | void (*releaseProc)(void* addr, void* context), void* context); |
| 590 | |
| 591 | /** Sets SkImageInfo to info following the rules in setInfo(), and creates SkPixelRef |
| 592 | containing pixels and rowBytes. |
| 593 | |
| 594 | If SkImageInfo could not be set, or rowBytes is less than info.minRowBytes(): |
| 595 | calls reset(), and returns false. |
| 596 | |
| 597 | Otherwise, if pixels equals nullptr: sets SkImageInfo, returns true. |
| 598 | |
| 599 | Caller must ensure that pixels are valid for the lifetime of SkBitmap and SkPixelRef. |
| 600 | |
| 601 | @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace |
| 602 | @param pixels address or pixel storage; may be nullptr |
| 603 | @param rowBytes size of pixel row or larger |
| 604 | @return true if SkImageInfo is set to info |
| 605 | */ |
| 606 | bool installPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 607 | return this->installPixels(info, pixels, rowBytes, nullptr, nullptr); |
| 608 | } |
| 609 | |
| 610 | /** Sets SkImageInfo to pixmap.info() following the rules in setInfo(), and creates |
| 611 | SkPixelRef containing pixmap.addr() and pixmap.rowBytes(). |
| 612 | |
| 613 | If SkImageInfo could not be set, or pixmap.rowBytes() is less than |
| 614 | SkImageInfo::minRowBytes(): calls reset(), and returns false. |
| 615 | |
| 616 | Otherwise, if pixmap.addr() equals nullptr: sets SkImageInfo, returns true. |
| 617 | |
| 618 | Caller must ensure that pixmap is valid for the lifetime of SkBitmap and SkPixelRef. |
| 619 | |
| 620 | @param pixmap SkImageInfo, pixel address, and rowBytes() |
| 621 | @return true if SkImageInfo was set to pixmap.info() |
| 622 | |
| 623 | example: https://fiddle.skia.org/c/@Bitmap_installPixels_3 |
| 624 | */ |
| 625 | bool installPixels(const SkPixmap& pixmap); |
| 626 | |
| 627 | /** Deprecated. |
| 628 | */ |
| 629 | bool installMaskPixels(const SkMask& mask); |
| 630 | |
| 631 | /** Replaces SkPixelRef with pixels, preserving SkImageInfo and rowBytes(). |
| 632 | Sets SkPixelRef origin to (0, 0). |
| 633 | |
| 634 | If pixels is nullptr, or if info().colorType() equals kUnknown_SkColorType; |
| 635 | release reference to SkPixelRef, and set SkPixelRef to nullptr. |
| 636 | |
| 637 | Caller is responsible for handling ownership pixel memory for the lifetime |
| 638 | of SkBitmap and SkPixelRef. |
| 639 | |
| 640 | @param pixels address of pixel storage, managed by caller |
| 641 | |
| 642 | example: https://fiddle.skia.org/c/@Bitmap_setPixels |
| 643 | */ |
| 644 | void setPixels(void* pixels); |
| 645 | |
| 646 | /** Allocates pixel memory with HeapAllocator, and replaces existing SkPixelRef. |
| 647 | The allocation size is determined by SkImageInfo width, height, and SkColorType. |
| 648 | |
| 649 | Returns false if info().colorType() is kUnknown_SkColorType, or allocation fails. |
| 650 | |
| 651 | @return true if the allocation succeeds |
| 652 | */ |
| 653 | bool SK_WARN_UNUSED_RESULT tryAllocPixels() { |
| 654 | return this->tryAllocPixels((Allocator*)nullptr); |
| 655 | } |
| 656 | |
| 657 | /** Allocates pixel memory with HeapAllocator, and replaces existing SkPixelRef. |
| 658 | The allocation size is determined by SkImageInfo width, height, and SkColorType. |
| 659 | |
| 660 | Aborts if info().colorType() is kUnknown_SkColorType, or allocation fails. |
| 661 | Abort steps may be provided by the user at compile |
| 662 | time by defining SK_ABORT. |
| 663 | |
| 664 | example: https://fiddle.skia.org/c/@Bitmap_allocPixels_3 |
| 665 | */ |
| 666 | void allocPixels(); |
| 667 | |
| 668 | /** Allocates pixel memory with allocator, and replaces existing SkPixelRef. |
| 669 | The allocation size is determined by SkImageInfo width, height, and SkColorType. |
| 670 | If allocator is nullptr, use HeapAllocator instead. |
| 671 | |
| 672 | Returns false if Allocator::allocPixelRef return false. |
| 673 | |
| 674 | @param allocator instance of SkBitmap::Allocator instantiation |
| 675 | @return true if custom allocator reports success |
| 676 | */ |
| 677 | bool SK_WARN_UNUSED_RESULT tryAllocPixels(Allocator* allocator); |
| 678 | |
| 679 | /** Allocates pixel memory with allocator, and replaces existing SkPixelRef. |
| 680 | The allocation size is determined by SkImageInfo width, height, and SkColorType. |
| 681 | If allocator is nullptr, use HeapAllocator instead. |
| 682 | |
| 683 | Aborts if Allocator::allocPixelRef return false. Abort steps may be provided by |
| 684 | the user at compile time by defining SK_ABORT. |
| 685 | |
| 686 | @param allocator instance of SkBitmap::Allocator instantiation |
| 687 | |
| 688 | example: https://fiddle.skia.org/c/@Bitmap_allocPixels_4 |
| 689 | */ |
| 690 | void allocPixels(Allocator* allocator); |
| 691 | |
| 692 | /** Returns SkPixelRef, which contains: pixel base address; its dimensions; and |
| 693 | rowBytes(), the interval from one row to the next. Does not change SkPixelRef |
| 694 | reference count. SkPixelRef may be shared by multiple bitmaps. |
| 695 | If SkPixelRef has not been set, returns nullptr. |
| 696 | |
| 697 | @return SkPixelRef, or nullptr |
| 698 | */ |
| 699 | SkPixelRef* pixelRef() const { return fPixelRef.get(); } |
| 700 | |
| 701 | /** Returns origin of pixels within SkPixelRef. SkBitmap bounds is always contained |
| 702 | by SkPixelRef bounds, which may be the same size or larger. Multiple SkBitmap |
| 703 | can share the same SkPixelRef, where each SkBitmap has different bounds. |
| 704 | |
| 705 | The returned origin added to SkBitmap dimensions equals or is smaller than the |
| 706 | SkPixelRef dimensions. |
| 707 | |
| 708 | Returns (0, 0) if SkPixelRef is nullptr. |
| 709 | |
| 710 | @return pixel origin within SkPixelRef |
| 711 | |
| 712 | example: https://fiddle.skia.org/c/@Bitmap_pixelRefOrigin |
| 713 | */ |
| 714 | SkIPoint pixelRefOrigin() const; |
| 715 | |
| 716 | /** Replaces pixelRef and origin in SkBitmap. dx and dy specify the offset |
| 717 | within the SkPixelRef pixels for the top-left corner of the bitmap. |
| 718 | |
| 719 | Asserts in debug builds if dx or dy are out of range. Pins dx and dy |
| 720 | to legal range in release builds. |
| 721 | |
| 722 | The caller is responsible for ensuring that the pixels match the |
| 723 | SkColorType and SkAlphaType in SkImageInfo. |
| 724 | |
| 725 | @param pixelRef SkPixelRef describing pixel address and rowBytes() |
| 726 | @param dx column offset in SkPixelRef for bitmap origin |
| 727 | @param dy row offset in SkPixelRef for bitmap origin |
| 728 | |
| 729 | example: https://fiddle.skia.org/c/@Bitmap_setPixelRef |
| 730 | */ |
| 731 | void setPixelRef(sk_sp<SkPixelRef> pixelRef, int dx, int dy); |
| 732 | |
| 733 | /** Returns true if SkBitmap is can be drawn. |
| 734 | |
| 735 | @return true if getPixels() is not nullptr |
| 736 | */ |
| 737 | bool readyToDraw() const { |
| 738 | return this->getPixels() != nullptr; |
| 739 | } |
| 740 | |
| 741 | /** Returns a unique value corresponding to the pixels in SkPixelRef. |
| 742 | Returns a different value after notifyPixelsChanged() has been called. |
| 743 | Returns zero if SkPixelRef is nullptr. |
| 744 | |
| 745 | Determines if pixels have changed since last examined. |
| 746 | |
| 747 | @return unique value for pixels in SkPixelRef |
| 748 | |
| 749 | example: https://fiddle.skia.org/c/@Bitmap_getGenerationID |
| 750 | */ |
| 751 | uint32_t getGenerationID() const; |
| 752 | |
| 753 | /** Marks that pixels in SkPixelRef have changed. Subsequent calls to |
| 754 | getGenerationID() return a different value. |
| 755 | |
| 756 | example: https://fiddle.skia.org/c/@Bitmap_notifyPixelsChanged |
| 757 | */ |
| 758 | void notifyPixelsChanged() const; |
| 759 | |
| 760 | /** Replaces pixel values with c, interpreted as being in the sRGB SkColorSpace. |
| 761 | All pixels contained by bounds() are affected. If the colorType() is |
| 762 | kGray_8_SkColorType or kRGB_565_SkColorType, then alpha is ignored; RGB is |
| 763 | treated as opaque. If colorType() is kAlpha_8_SkColorType, then RGB is ignored. |
| 764 | |
| 765 | @param c unpremultiplied color |
| 766 | |
| 767 | example: https://fiddle.skia.org/c/@Bitmap_eraseColor |
| 768 | */ |
| 769 | void eraseColor(SkColor c) const; |
| 770 | |
| 771 | /** Replaces pixel values with unpremultiplied color built from a, r, g, and b, |
| 772 | interpreted as being in the sRGB SkColorSpace. All pixels contained by |
| 773 | bounds() are affected. If the colorType() is kGray_8_SkColorType or |
| 774 | kRGB_565_SkColorType, then a is ignored; r, g, and b are treated as opaque. |
| 775 | If colorType() is kAlpha_8_SkColorType, then r, g, and b are ignored. |
| 776 | |
| 777 | @param a amount of alpha, from fully transparent (0) to fully opaque (255) |
| 778 | @param r amount of red, from no red (0) to full red (255) |
| 779 | @param g amount of green, from no green (0) to full green (255) |
| 780 | @param b amount of blue, from no blue (0) to full blue (255) |
| 781 | */ |
| 782 | void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const { |
| 783 | this->eraseColor(SkColorSetARGB(a, r, g, b)); |
| 784 | } |
| 785 | |
| 786 | /** Replaces pixel values inside area with c. interpreted as being in the sRGB |
| 787 | SkColorSpace. If area does not intersect bounds(), call has no effect. |
| 788 | |
| 789 | If the colorType() is kGray_8_SkColorType or kRGB_565_SkColorType, then alpha |
| 790 | is ignored; RGB is treated as opaque. If colorType() is kAlpha_8_SkColorType, |
| 791 | then RGB is ignored. |
| 792 | |
| 793 | @param c unpremultiplied color |
| 794 | @param area rectangle to fill |
| 795 | |
| 796 | example: https://fiddle.skia.org/c/@Bitmap_erase |
| 797 | */ |
| 798 | void erase(SkColor c, const SkIRect& area) const; |
| 799 | |
| 800 | /** Deprecated. |
| 801 | */ |
| 802 | void eraseArea(const SkIRect& area, SkColor c) const { |
| 803 | this->erase(c, area); |
| 804 | } |
| 805 | |
| 806 | /** Returns pixel at (x, y) as unpremultiplied color. |
| 807 | Returns black with alpha if SkColorType is kAlpha_8_SkColorType. |
| 808 | |
| 809 | Input is not validated: out of bounds values of x or y trigger an assert() if |
| 810 | built with SK_DEBUG defined; and returns undefined values or may crash if |
| 811 | SK_RELEASE is defined. Fails if SkColorType is kUnknown_SkColorType or |
| 812 | pixel address is nullptr. |
| 813 | |
| 814 | SkColorSpace in SkImageInfo is ignored. Some color precision may be lost in the |
| 815 | conversion to unpremultiplied color; original pixel data may have additional |
| 816 | precision. |
| 817 | |
| 818 | @param x column index, zero or greater, and less than width() |
| 819 | @param y row index, zero or greater, and less than height() |
| 820 | @return pixel converted to unpremultiplied color |
| 821 | */ |
| 822 | SkColor getColor(int x, int y) const { |
| 823 | return this->pixmap().getColor(x, y); |
| 824 | } |
| 825 | |
| 826 | /** Look up the pixel at (x,y) and return its alpha component, normalized to [0..1]. |
| 827 | This is roughly equivalent to SkGetColorA(getColor()), but can be more efficent |
| 828 | (and more precise if the pixels store more than 8 bits per component). |
| 829 | |
| 830 | @param x column index, zero or greater, and less than width() |
| 831 | @param y row index, zero or greater, and less than height() |
| 832 | @return alpha converted to normalized float |
| 833 | */ |
| 834 | float getAlphaf(int x, int y) const { |
| 835 | return this->pixmap().getAlphaf(x, y); |
| 836 | } |
| 837 | |
| 838 | /** Returns pixel address at (x, y). |
| 839 | |
| 840 | Input is not validated: out of bounds values of x or y, or kUnknown_SkColorType, |
| 841 | trigger an assert() if built with SK_DEBUG defined. Returns nullptr if |
| 842 | SkColorType is kUnknown_SkColorType, or SkPixelRef is nullptr. |
| 843 | |
| 844 | Performs a lookup of pixel size; for better performance, call |
| 845 | one of: getAddr8(), getAddr16(), or getAddr32(). |
| 846 | |
| 847 | @param x column index, zero or greater, and less than width() |
| 848 | @param y row index, zero or greater, and less than height() |
| 849 | @return generic pointer to pixel |
| 850 | |
| 851 | example: https://fiddle.skia.org/c/@Bitmap_getAddr |
| 852 | */ |
| 853 | void* getAddr(int x, int y) const; |
| 854 | |
| 855 | /** Returns address at (x, y). |
| 856 | |
| 857 | Input is not validated. Triggers an assert() if built with SK_DEBUG defined and: |
| 858 | - SkPixelRef is nullptr |
| 859 | - bytesPerPixel() is not four |
| 860 | - x is negative, or not less than width() |
| 861 | - y is negative, or not less than height() |
| 862 | |
| 863 | @param x column index, zero or greater, and less than width() |
| 864 | @param y row index, zero or greater, and less than height() |
| 865 | @return unsigned 32-bit pointer to pixel at (x, y) |
| 866 | */ |
| 867 | inline uint32_t* getAddr32(int x, int y) const; |
| 868 | |
| 869 | /** Returns address at (x, y). |
| 870 | |
| 871 | Input is not validated. Triggers an assert() if built with SK_DEBUG defined and: |
| 872 | - SkPixelRef is nullptr |
| 873 | - bytesPerPixel() is not two |
| 874 | - x is negative, or not less than width() |
| 875 | - y is negative, or not less than height() |
| 876 | |
| 877 | @param x column index, zero or greater, and less than width() |
| 878 | @param y row index, zero or greater, and less than height() |
| 879 | @return unsigned 16-bit pointer to pixel at (x, y) |
| 880 | */ |
| 881 | inline uint16_t* getAddr16(int x, int y) const; |
| 882 | |
| 883 | /** Returns address at (x, y). |
| 884 | |
| 885 | Input is not validated. Triggers an assert() if built with SK_DEBUG defined and: |
| 886 | - SkPixelRef is nullptr |
| 887 | - bytesPerPixel() is not one |
| 888 | - x is negative, or not less than width() |
| 889 | - y is negative, or not less than height() |
| 890 | |
| 891 | @param x column index, zero or greater, and less than width() |
| 892 | @param y row index, zero or greater, and less than height() |
| 893 | @return unsigned 8-bit pointer to pixel at (x, y) |
| 894 | */ |
| 895 | inline uint8_t* getAddr8(int x, int y) const; |
| 896 | |
| 897 | /** Shares SkPixelRef with dst. Pixels are not copied; SkBitmap and dst point |
| 898 | to the same pixels; dst bounds() are set to the intersection of subset |
| 899 | and the original bounds(). |
| 900 | |
| 901 | subset may be larger than bounds(). Any area outside of bounds() is ignored. |
| 902 | |
| 903 | Any contents of dst are discarded. |
| 904 | |
| 905 | Return false if: |
| 906 | - dst is nullptr |
| 907 | - SkPixelRef is nullptr |
| 908 | - subset does not intersect bounds() |
| 909 | |
| 910 | @param dst SkBitmap set to subset |
| 911 | @param subset rectangle of pixels to reference |
| 912 | @return true if dst is replaced by subset |
| 913 | |
| 914 | example: https://fiddle.skia.org/c/@Bitmap_extractSubset |
| 915 | */ |
| 916 | bool (SkBitmap* dst, const SkIRect& subset) const; |
| 917 | |
| 918 | /** Copies a SkRect of pixels from SkBitmap to dstPixels. Copy starts at (srcX, srcY), |
| 919 | and does not exceed SkBitmap (width(), height()). |
| 920 | |
| 921 | dstInfo specifies width, height, SkColorType, SkAlphaType, and SkColorSpace of |
| 922 | destination. dstRowBytes specifics the gap from one destination row to the next. |
| 923 | Returns true if pixels are copied. Returns false if: |
| 924 | - dstInfo has no address |
| 925 | - dstRowBytes is less than dstInfo.minRowBytes() |
| 926 | - SkPixelRef is nullptr |
| 927 | |
| 928 | Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is |
| 929 | kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match. |
| 930 | If SkBitmap colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match. |
| 931 | If SkBitmap alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must |
| 932 | match. If SkBitmap colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns |
| 933 | false if pixel conversion is not possible. |
| 934 | |
| 935 | srcX and srcY may be negative to copy only top or left of source. Returns |
| 936 | false if width() or height() is zero or negative. |
| 937 | Returns false if abs(srcX) >= Bitmap width(), or if abs(srcY) >= Bitmap height(). |
| 938 | |
| 939 | @param dstInfo destination width, height, SkColorType, SkAlphaType, SkColorSpace |
| 940 | @param dstPixels destination pixel storage |
| 941 | @param dstRowBytes destination row length |
| 942 | @param srcX column index whose absolute value is less than width() |
| 943 | @param srcY row index whose absolute value is less than height() |
| 944 | @return true if pixels are copied to dstPixels |
| 945 | */ |
| 946 | bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
| 947 | int srcX, int srcY) const; |
| 948 | |
| 949 | /** Copies a SkRect of pixels from SkBitmap to dst. Copy starts at (srcX, srcY), and |
| 950 | does not exceed SkBitmap (width(), height()). |
| 951 | |
| 952 | dst specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, |
| 953 | and row bytes of destination. dst.rowBytes() specifics the gap from one destination |
| 954 | row to the next. Returns true if pixels are copied. Returns false if: |
| 955 | - dst pixel storage equals nullptr |
| 956 | - dst.rowBytes is less than SkImageInfo::minRowBytes() |
| 957 | - SkPixelRef is nullptr |
| 958 | |
| 959 | Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is |
| 960 | kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match. |
| 961 | If SkBitmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match. |
| 962 | If SkBitmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must |
| 963 | match. If SkBitmap colorSpace() is nullptr, dst SkColorSpace must match. Returns |
| 964 | false if pixel conversion is not possible. |
| 965 | |
| 966 | srcX and srcY may be negative to copy only top or left of source. Returns |
| 967 | false if width() or height() is zero or negative. |
| 968 | Returns false if abs(srcX) >= Bitmap width(), or if abs(srcY) >= Bitmap height(). |
| 969 | |
| 970 | @param dst destination SkPixmap: SkImageInfo, pixels, row bytes |
| 971 | @param srcX column index whose absolute value is less than width() |
| 972 | @param srcY row index whose absolute value is less than height() |
| 973 | @return true if pixels are copied to dst |
| 974 | |
| 975 | example: https://fiddle.skia.org/c/@Bitmap_readPixels_2 |
| 976 | */ |
| 977 | bool readPixels(const SkPixmap& dst, int srcX, int srcY) const; |
| 978 | |
| 979 | /** Copies a SkRect of pixels from SkBitmap to dst. Copy starts at (0, 0), and |
| 980 | does not exceed SkBitmap (width(), height()). |
| 981 | |
| 982 | dst specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, |
| 983 | and row bytes of destination. dst.rowBytes() specifics the gap from one destination |
| 984 | row to the next. Returns true if pixels are copied. Returns false if: |
| 985 | - dst pixel storage equals nullptr |
| 986 | - dst.rowBytes is less than SkImageInfo::minRowBytes() |
| 987 | - SkPixelRef is nullptr |
| 988 | |
| 989 | Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is |
| 990 | kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match. |
| 991 | If SkBitmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match. |
| 992 | If SkBitmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must |
| 993 | match. If SkBitmap colorSpace() is nullptr, dst SkColorSpace must match. Returns |
| 994 | false if pixel conversion is not possible. |
| 995 | |
| 996 | @param dst destination SkPixmap: SkImageInfo, pixels, row bytes |
| 997 | @return true if pixels are copied to dst |
| 998 | */ |
| 999 | bool readPixels(const SkPixmap& dst) const { |
| 1000 | return this->readPixels(dst, 0, 0); |
| 1001 | } |
| 1002 | |
| 1003 | /** Copies a SkRect of pixels from src. Copy starts at (dstX, dstY), and does not exceed |
| 1004 | (src.width(), src.height()). |
| 1005 | |
| 1006 | src specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, |
| 1007 | and row bytes of source. src.rowBytes() specifics the gap from one source |
| 1008 | row to the next. Returns true if pixels are copied. Returns false if: |
| 1009 | - src pixel storage equals nullptr |
| 1010 | - src.rowBytes is less than SkImageInfo::minRowBytes() |
| 1011 | - SkPixelRef is nullptr |
| 1012 | |
| 1013 | Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is |
| 1014 | kGray_8_SkColorType, or kAlpha_8_SkColorType; src SkColorType must match. |
| 1015 | If SkBitmap colorType() is kGray_8_SkColorType, src SkColorSpace must match. |
| 1016 | If SkBitmap alphaType() is kOpaque_SkAlphaType, src SkAlphaType must |
| 1017 | match. If SkBitmap colorSpace() is nullptr, src SkColorSpace must match. Returns |
| 1018 | false if pixel conversion is not possible. |
| 1019 | |
| 1020 | dstX and dstY may be negative to copy only top or left of source. Returns |
| 1021 | false if width() or height() is zero or negative. |
| 1022 | Returns false if abs(dstX) >= Bitmap width(), or if abs(dstY) >= Bitmap height(). |
| 1023 | |
| 1024 | @param src source SkPixmap: SkImageInfo, pixels, row bytes |
| 1025 | @param dstX column index whose absolute value is less than width() |
| 1026 | @param dstY row index whose absolute value is less than height() |
| 1027 | @return true if src pixels are copied to SkBitmap |
| 1028 | |
| 1029 | example: https://fiddle.skia.org/c/@Bitmap_writePixels |
| 1030 | */ |
| 1031 | bool writePixels(const SkPixmap& src, int dstX, int dstY); |
| 1032 | |
| 1033 | /** Copies a SkRect of pixels from src. Copy starts at (0, 0), and does not exceed |
| 1034 | (src.width(), src.height()). |
| 1035 | |
| 1036 | src specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, |
| 1037 | and row bytes of source. src.rowBytes() specifics the gap from one source |
| 1038 | row to the next. Returns true if pixels are copied. Returns false if: |
| 1039 | - src pixel storage equals nullptr |
| 1040 | - src.rowBytes is less than SkImageInfo::minRowBytes() |
| 1041 | - SkPixelRef is nullptr |
| 1042 | |
| 1043 | Pixels are copied only if pixel conversion is possible. If SkBitmap colorType() is |
| 1044 | kGray_8_SkColorType, or kAlpha_8_SkColorType; src SkColorType must match. |
| 1045 | If SkBitmap colorType() is kGray_8_SkColorType, src SkColorSpace must match. |
| 1046 | If SkBitmap alphaType() is kOpaque_SkAlphaType, src SkAlphaType must |
| 1047 | match. If SkBitmap colorSpace() is nullptr, src SkColorSpace must match. Returns |
| 1048 | false if pixel conversion is not possible. |
| 1049 | |
| 1050 | @param src source SkPixmap: SkImageInfo, pixels, row bytes |
| 1051 | @return true if src pixels are copied to SkBitmap |
| 1052 | */ |
| 1053 | bool writePixels(const SkPixmap& src) { |
| 1054 | return this->writePixels(src, 0, 0); |
| 1055 | } |
| 1056 | |
| 1057 | /** Sets dst to alpha described by pixels. Returns false if dst cannot be written to |
| 1058 | or dst pixels cannot be allocated. |
| 1059 | |
| 1060 | Uses HeapAllocator to reserve memory for dst SkPixelRef. |
| 1061 | |
| 1062 | @param dst holds SkPixelRef to fill with alpha layer |
| 1063 | @return true if alpha layer was constructed in dst SkPixelRef |
| 1064 | */ |
| 1065 | bool (SkBitmap* dst) const { |
| 1066 | return this->extractAlpha(dst, nullptr, nullptr, nullptr); |
| 1067 | } |
| 1068 | |
| 1069 | /** Sets dst to alpha described by pixels. Returns false if dst cannot be written to |
| 1070 | or dst pixels cannot be allocated. |
| 1071 | |
| 1072 | If paint is not nullptr and contains SkMaskFilter, SkMaskFilter |
| 1073 | generates mask alpha from SkBitmap. Uses HeapAllocator to reserve memory for dst |
| 1074 | SkPixelRef. Sets offset to top-left position for dst for alignment with SkBitmap; |
| 1075 | (0, 0) unless SkMaskFilter generates mask. |
| 1076 | |
| 1077 | @param dst holds SkPixelRef to fill with alpha layer |
| 1078 | @param paint holds optional SkMaskFilter; may be nullptr |
| 1079 | @param offset top-left position for dst; may be nullptr |
| 1080 | @return true if alpha layer was constructed in dst SkPixelRef |
| 1081 | */ |
| 1082 | bool (SkBitmap* dst, const SkPaint* paint, |
| 1083 | SkIPoint* offset) const { |
| 1084 | return this->extractAlpha(dst, paint, nullptr, offset); |
| 1085 | } |
| 1086 | |
| 1087 | /** Sets dst to alpha described by pixels. Returns false if dst cannot be written to |
| 1088 | or dst pixels cannot be allocated. |
| 1089 | |
| 1090 | If paint is not nullptr and contains SkMaskFilter, SkMaskFilter |
| 1091 | generates mask alpha from SkBitmap. allocator may reference a custom allocation |
| 1092 | class or be set to nullptr to use HeapAllocator. Sets offset to top-left |
| 1093 | position for dst for alignment with SkBitmap; (0, 0) unless SkMaskFilter generates |
| 1094 | mask. |
| 1095 | |
| 1096 | @param dst holds SkPixelRef to fill with alpha layer |
| 1097 | @param paint holds optional SkMaskFilter; may be nullptr |
| 1098 | @param allocator function to reserve memory for SkPixelRef; may be nullptr |
| 1099 | @param offset top-left position for dst; may be nullptr |
| 1100 | @return true if alpha layer was constructed in dst SkPixelRef |
| 1101 | */ |
| 1102 | bool (SkBitmap* dst, const SkPaint* paint, Allocator* allocator, |
| 1103 | SkIPoint* offset) const; |
| 1104 | |
| 1105 | /** Copies SkBitmap pixel address, row bytes, and SkImageInfo to pixmap, if address |
| 1106 | is available, and returns true. If pixel address is not available, return |
| 1107 | false and leave pixmap unchanged. |
| 1108 | |
| 1109 | pixmap contents become invalid on any future change to SkBitmap. |
| 1110 | |
| 1111 | @param pixmap storage for pixel state if pixels are readable; otherwise, ignored |
| 1112 | @return true if SkBitmap has direct access to pixels |
| 1113 | |
| 1114 | example: https://fiddle.skia.org/c/@Bitmap_peekPixels |
| 1115 | */ |
| 1116 | bool peekPixels(SkPixmap* pixmap) const; |
| 1117 | |
| 1118 | sk_sp<SkShader> makeShader(SkTileMode tmx, SkTileMode tmy, |
| 1119 | const SkMatrix* localMatrix = nullptr) const; |
| 1120 | // defaults to Clamp in x, and y |
| 1121 | sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const; |
| 1122 | |
| 1123 | /** Asserts if internal values are illegal or inconsistent. Only available if |
| 1124 | SK_DEBUG is defined at compile time. |
| 1125 | */ |
| 1126 | SkDEBUGCODE(void validate() const;) |
| 1127 | |
| 1128 | /** \class SkBitmap::Allocator |
| 1129 | Abstract subclass of HeapAllocator. |
| 1130 | */ |
| 1131 | class Allocator : public SkRefCnt { |
| 1132 | public: |
| 1133 | |
| 1134 | /** Allocates the pixel memory for the bitmap, given its dimensions and |
| 1135 | SkColorType. Returns true on success, where success means either setPixels() |
| 1136 | or setPixelRef() was called. |
| 1137 | |
| 1138 | @param bitmap SkBitmap containing SkImageInfo as input, and SkPixelRef as output |
| 1139 | @return true if SkPixelRef was allocated |
| 1140 | */ |
| 1141 | virtual bool allocPixelRef(SkBitmap* bitmap) = 0; |
| 1142 | private: |
| 1143 | typedef SkRefCnt INHERITED; |
| 1144 | }; |
| 1145 | |
| 1146 | /** \class SkBitmap::HeapAllocator |
| 1147 | Subclass of SkBitmap::Allocator that returns a SkPixelRef that allocates its pixel |
| 1148 | memory from the heap. This is the default SkBitmap::Allocator invoked by |
| 1149 | allocPixels(). |
| 1150 | */ |
| 1151 | class HeapAllocator : public Allocator { |
| 1152 | public: |
| 1153 | |
| 1154 | /** Allocates the pixel memory for the bitmap, given its dimensions and |
| 1155 | SkColorType. Returns true on success, where success means either setPixels() |
| 1156 | or setPixelRef() was called. |
| 1157 | |
| 1158 | @param bitmap SkBitmap containing SkImageInfo as input, and SkPixelRef as output |
| 1159 | @return true if pixels are allocated |
| 1160 | |
| 1161 | example: https://fiddle.skia.org/c/@Bitmap_HeapAllocator_allocPixelRef |
| 1162 | */ |
| 1163 | bool allocPixelRef(SkBitmap* bitmap) override; |
| 1164 | }; |
| 1165 | |
| 1166 | private: |
| 1167 | sk_sp<SkPixelRef> fPixelRef; |
| 1168 | SkPixmap fPixmap; |
| 1169 | sk_sp<SkMipmap> fMips; |
| 1170 | |
| 1171 | friend class SkImage_Raster; |
| 1172 | friend class SkReadBuffer; // unflatten |
| 1173 | }; |
| 1174 | |
| 1175 | /////////////////////////////////////////////////////////////////////////////// |
| 1176 | |
| 1177 | inline uint32_t* SkBitmap::getAddr32(int x, int y) const { |
| 1178 | SkASSERT(fPixmap.addr()); |
| 1179 | return fPixmap.writable_addr32(x, y); |
| 1180 | } |
| 1181 | |
| 1182 | inline uint16_t* SkBitmap::getAddr16(int x, int y) const { |
| 1183 | SkASSERT(fPixmap.addr()); |
| 1184 | return fPixmap.writable_addr16(x, y); |
| 1185 | } |
| 1186 | |
| 1187 | inline uint8_t* SkBitmap::getAddr8(int x, int y) const { |
| 1188 | SkASSERT(fPixmap.addr()); |
| 1189 | return fPixmap.writable_addr8(x, y); |
| 1190 | } |
| 1191 | |
| 1192 | #endif |
| 1193 | |