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