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