blob: f304bde93357324f3d623dc16262c0daace049bc [file] [log] [blame]
reed92fc2ae2015-05-22 08:06:21 -07001/*
2 * Copyright 2015 Google Inc.
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 SkPixmap_DEFINED
9#define SkPixmap_DEFINED
10
reed7aefe032015-06-08 10:22:22 -070011#include "SkColor.h"
reed09553032015-11-23 12:32:16 -080012#include "SkFilterQuality.h"
reed92fc2ae2015-05-22 08:06:21 -070013#include "SkImageInfo.h"
14
fmalita3a94c6c2016-02-04 13:09:59 -080015class SkData;
reed183b57f2015-06-05 14:33:17 -070016struct SkMask;
reed92fc2ae2015-05-22 08:06:21 -070017
Cary Clark8a02b0b2017-09-21 12:28:43 -040018/** \class SkPixmap
19 SkPixmap provides a utility to pair SkImageInfo with pixels and row bytes.
20 SkPixmap is a low level class which provides convenience functions to access
21 raster destinations. SkCanvas can not draw SkPixmap, nor does SkPixmap provide
22 a direct drawing destination.
23
24 Use SkBitmap to draw pixels referenced by SkPixmap; use SkSurface to draw into
25 pixels referenced by SkPixmap.
26
27 SkPixmap does not try to manage the lifetime of the pixel memory. Use PixelRef
28 to manage pixel memory; PixelRef is safe across threads.
29*/
fmalita9a5d1ab2015-07-27 10:27:28 -070030class SK_API SkPixmap {
reed92fc2ae2015-05-22 08:06:21 -070031public:
Cary Clark8a02b0b2017-09-21 12:28:43 -040032
33 /** Creates an empty SkPixmap without pixels, with kUnknown_SkColorType, with
34 kUnknown_SkAlphaType, and with a width and height of zero. Use
35 reset() to associate pixels, SkColorType, SkAlphaType, width, and height
36 after SkPixmap has been created.
37
38 @return empty SkPixmap
39 */
reed92fc2ae2015-05-22 08:06:21 -070040 SkPixmap()
Ben Wagnera93a14a2017-08-28 10:34:05 -040041 : fPixels(nullptr), fRowBytes(0), fInfo(SkImageInfo::MakeUnknown(0, 0))
reed92fc2ae2015-05-22 08:06:21 -070042 {}
43
Cary Clark8a02b0b2017-09-21 12:28:43 -040044 /** Creates SkPixmap from info width, height, SkAlphaType, and SkColorType.
45 addr points to pixels, or nullptr. rowBytes should be info.width() times
46 info.bytesPerPixel(), or larger.
47
48 No parameter checking is performed; it is up to the caller to ensure that
49 addr and rowBytes agree with info.
50
51 The memory lifetime of pixels is managed by the caller. When SkPixmap goes
52 out of scope, addr is unaffected.
53
54 SkPixmap may be later modified by reset() to change its size, pixel type, or
55 storage.
56
57 @param info width, height, SkAlphaType, SkColorType of SkImageInfo
58 @param addr pointer to pixels allocated by caller; may be nullptr
59 @param rowBytes size of one row of addr; width times pixel size, or larger
60 @return initialized SkPixmap
61 */
Mike Reed086a4272017-07-18 10:53:11 -040062 SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes)
Mike Reed26e9ddd2017-07-17 17:33:53 -040063 : fPixels(addr), fRowBytes(rowBytes), fInfo(info)
64 {}
reed92fc2ae2015-05-22 08:06:21 -070065
Cary Clark8a02b0b2017-09-21 12:28:43 -040066 /** Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to
67 kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType.
68
69 The prior pixels are unaffected; it is up to the caller to release pixels
70 memory if desired.
71 */
reed884e97c2015-05-26 11:31:54 -070072 void reset();
Cary Clark8a02b0b2017-09-21 12:28:43 -040073
74 /** Sets width, height, SkAlphaType, and SkColorType from info.
75 Sets pixel address from addr, which may be nullptr.
76 Sets row bytes from rowBytes, which should be info.width() times
77 info.bytesPerPixel(), or larger.
78
79 Does not check addr. Asserts if built with SK_DEBUG defined and if rowBytes is
80 too small to hold one row of pixels.
81
82 The memory lifetime pixels are managed by the caller. When SkPixmap goes
83 out of scope, addr is unaffected.
84
85 @param info width, height, SkAlphaType, SkColorType of SkImageInfo
86 @param addr pointer to pixels allocated by caller; may be nullptr
87 @param rowBytes size of one row of addr; width times pixel size, or larger
88 */
Mike Reed086a4272017-07-18 10:53:11 -040089 void reset(const SkImageInfo& info, const void* addr, size_t rowBytes);
reed183b57f2015-06-05 14:33:17 -070090
Cary Clark8a02b0b2017-09-21 12:28:43 -040091 /** Changes SkColorSpace in SkImageInfo; preserves width, height, SkAlphaType, and
92 SkColorType in SkImage, and leaves pixel address and row bytes unchanged.
93 SkColorSpace reference count is incremented.
94
95 @param colorSpace SkColorSpace moved to SkImageInfo
96 */
Cary Clarkec2455b2017-09-05 09:42:43 -040097 void setColorSpace(sk_sp<SkColorSpace> colorSpace);
msarett804b4612016-06-09 11:03:45 -070098
Cary Clark8a02b0b2017-09-21 12:28:43 -040099 /** Sets width, height, pixel address, and row bytes to SkMask properties, if SkMask
100 format is SkMask::kA8_Format; and returns true. Otherwise sets width, height,
101 row bytes to zero; pixel address to nullptr; SkColorType to kUnknown_SkColorType;
102 and SkAlphaType to kUnknown_SkAlphaType; and returns false.
103
104 Failing to read the return value generates a compile time warning.
105
106 @param mask SkMask containing pixels and dimensions
107 @return true if set to SkMask properties
108 */
Cary Clarkec2455b2017-09-05 09:42:43 -0400109 bool SK_WARN_UNUSED_RESULT reset(const SkMask& mask);
reed183b57f2015-06-05 14:33:17 -0700110
Cary Clark8a02b0b2017-09-21 12:28:43 -0400111 /** Sets subset width, height, pixel address to intersection of SkPixmap with area,
112 if intersection is not empty; and return true. Otherwise, leave subset unchanged
113 and return false.
114
115 Failing to read the return value generates a compile time warning.
116
117 @param subset storage for width, height, pixel address of intersection
118 @param area bounds to intersect with SkPixmap
119 @return true if intersection of SkPixmap and area is not empty
120 */
reed183b57f2015-06-05 14:33:17 -0700121 bool SK_WARN_UNUSED_RESULT extractSubset(SkPixmap* subset, const SkIRect& area) const;
reed884e97c2015-05-26 11:31:54 -0700122
Cary Clark8a02b0b2017-09-21 12:28:43 -0400123 /** Returns width, height, SkAlphaType, SkColorType, and SkColorSpace.
124
125 @return reference to ImageInfo
126 */
reed92fc2ae2015-05-22 08:06:21 -0700127 const SkImageInfo& info() const { return fInfo; }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400128
129 /** Returns row bytes, the interval from one pixel row to the next. Row bytes
130 is at least as large as
131 width() * info().bytesPerPixel().
132
133 It is up to the SkPixmap creator to ensure that row bytes is a useful value.
134
135 @return byte length of pixel row
136 */
reed92fc2ae2015-05-22 08:06:21 -0700137 size_t rowBytes() const { return fRowBytes; }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400138
139 /** Returns pixel address, the base address corresponding to the pixel origin.
140
141 It is up to the SkPixmap creator to ensure that pixel address is a useful value.
142
143 @return pixel address
144 */
reed92fc2ae2015-05-22 08:06:21 -0700145 const void* addr() const { return fPixels; }
reed92fc2ae2015-05-22 08:06:21 -0700146
Cary Clark8a02b0b2017-09-21 12:28:43 -0400147 /** Returns pixel count in each pixel row. Should be equal or less than:
148 rowBytes() / info.bytesPerPixel().
149
150 @return pixel width in SkImageInfo
151 */
reed92fc2ae2015-05-22 08:06:21 -0700152 int width() const { return fInfo.width(); }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400153
154 /** Returns pixel row count.
155
156 @return pixel height in SkImageInfo
157 */
reed92fc2ae2015-05-22 08:06:21 -0700158 int height() const { return fInfo.height(); }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400159
160 /** Returns SkColorType, one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
161 kRGB_565_SkColorType, kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
162 kBGRA_8888_SkColorType, kGray_8_SkColorType, kRGBA_F16_SkColorType.
163
164 @return SkColorType in SkImageInfo
165 */
reed92fc2ae2015-05-22 08:06:21 -0700166 SkColorType colorType() const { return fInfo.colorType(); }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400167
168 /** Returns SkAlphaType, one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
169 kPremul_SkAlphaType, kUnpremul_SkAlphaType.
170
171 @return SkAlphaType in SkImageInfo
172 */
reed92fc2ae2015-05-22 08:06:21 -0700173 SkAlphaType alphaType() const { return fInfo.alphaType(); }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400174
175 /** Returns SkColorSpace associated with SkImageInfo.
176
177 @return SkColorSpace in SkImageInfo
178 */
Mike Klein744908e2016-11-11 12:51:36 -0500179 SkColorSpace* colorSpace() const { return fInfo.colorSpace(); }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400180
181 /** Returns true if SkAlphaType is kOpaque_SkAlphaType.
182 Does not check if SkColorType allows alpha, or alpha in pixel values.
183
184 @return true if SkImageInfo has opaque SkAlphaType
185 */
reed92fc2ae2015-05-22 08:06:21 -0700186 bool isOpaque() const { return fInfo.isOpaque(); }
187
Cary Clark8a02b0b2017-09-21 12:28:43 -0400188 /** Returns SkIRect
189 { 0, 0, width(), height() }.
190
191 @return integral rectangle from origin to width() and height()
192 */
reedad7ae6c2015-06-04 14:12:25 -0700193 SkIRect bounds() const { return SkIRect::MakeWH(this->width(), this->height()); }
194
Cary Clark8a02b0b2017-09-21 12:28:43 -0400195 /** Returns number of pixels that fit on row. Should be greater than or equal to
196 width().
197
198 @return maximum pixels per row
199 */
robertphillips3b5bea92016-03-24 11:21:25 -0700200 int rowBytesAsPixels() const { return int(fRowBytes >> this->shiftPerPixel()); }
201
Cary Clark8a02b0b2017-09-21 12:28:43 -0400202 /** Returns bit shift converting row bytes to row pixels.
203 Returns zero for kUnknown_SkColorType.
204
205 @return one of: 0, 1, 2, 3; left shift to convert pixels to bytes
206 */
reed8c3fd4f2016-04-15 06:59:38 -0700207 int shiftPerPixel() const { return fInfo.shiftPerPixel(); }
robertphillips3b5bea92016-03-24 11:21:25 -0700208
Mike Reedcd284c52017-09-29 15:22:56 -0400209#ifdef SK_SUPPORT_LEGACY_SAFESIZE64
Cary Clark8a02b0b2017-09-21 12:28:43 -0400210 /** Returns conservative memory required for pixel storage.
211 Includes unused memory on last row when rowBytesAsPixels() exceeds width().
212
213 @return conservative pixel storage size
214 */
reed95d343f2015-05-23 13:21:06 -0700215 uint64_t getSize64() const { return sk_64_mul(fInfo.height(), fRowBytes); }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400216
217 /** Returns minimum memory required for pixel storage.
218 Does not include unused memory on last row when rowBytesAsPixels() exceeds width().
219
220 @return exact pixel storage size
221 */
reed95d343f2015-05-23 13:21:06 -0700222 uint64_t getSafeSize64() const { return fInfo.getSafeSize64(fRowBytes); }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400223
224 /** Returns minimum memory required for pixel storage.
225 Does not include unused memory on last row when rowBytesAsPixels() exceeds width().
226 Returns zero if value is does not fit in a signed 32-bit integer.
227 The largest value than can be returned is 2,147,483,647.
228
229 @return exact pixel storage size if size fits in signed 32 bits
230 */
reed92fc2ae2015-05-22 08:06:21 -0700231 size_t getSafeSize() const { return fInfo.getSafeSize(fRowBytes); }
Mike Reedcd284c52017-09-29 15:22:56 -0400232#endif
reed92fc2ae2015-05-22 08:06:21 -0700233
Mike Reede74dafc2017-09-29 13:41:58 -0400234 /**
235 * Returns the size (in bytes) of the pixmap's image buffer.
236 * If the calculation overflows, or if the height is 0, this returns 0.
237 */
238 size_t computeByteSize() const { return fInfo.computeByteSize(fRowBytes); }
239
Cary Clark8a02b0b2017-09-21 12:28:43 -0400240 /** Returns true if all pixels are opaque. SkColorType determines how pixels
241 are encoded, and whether pixel describes alpha. Returns true for SkColorType
242 without alpha in each pixel; for other SkColorType, returns true if all
243 pixels have alpha values equivalent to 1.0 or greater.
244
245 For SkColorType kRGB_565_SkColorType or kGray_8_SkColorType: always
246 returns true. For SkColorType kAlpha_8_SkColorType, kBGRA_8888_SkColorType,
247 kRGBA_8888_SkColorType: returns true if all pixel alpha values are 255.
248 For SkColorType kARGB_4444_SkColorType: returns true if all pixel alpha values are 15.
249 For kRGBA_F16_SkColorType: returns true if all pixel alpha values are 1.0 or
250 greater.
251
252 Returns false for kUnknown_SkColorType.
253
254 @return true all pixels have opaque values or SkColorType is opaque
255 */
Hal Canary58a76942016-12-07 15:24:59 -0500256 bool computeIsOpaque() const;
257
Cary Clark8a02b0b2017-09-21 12:28:43 -0400258 /** Returns pixel at (x, y) as unpremultiplied color.
259 Returns black with alpha if SkColorType is kAlpha_8_SkColorType.
260
261 Input is not validated: out of bounds values of x or y trigger an assert() if
262 built with SK_DEBUG defined; and returns undefined values or may crash if
263 SK_RELEASE is defined. Fails if SkColorType is kUnknown_SkColorType or
264 pixel address is nullptr.
265
266 SkColorSpace in SkImageInfo is ignored. Some color precision may be lost in the
267 conversion to unpremultiplied color; original pixel data may have additional
268 precision.
269
270 @param x column index, zero or greater, and less than width()
271 @param y row index, zero or greater, and less than height()
272 @return pixel converted to unpremultiplied color
273 */
Hal Canary94e1a2f2016-10-31 09:38:12 -0400274 SkColor getColor(int x, int y) const;
275
Cary Clark8a02b0b2017-09-21 12:28:43 -0400276 /** Returns readable pixel address at (x, y).
277
278 Input is not validated: out of bounds values of x or y trigger an assert() if
279 built with SK_DEBUG defined. Returns zero if SkColorType is kUnknown_SkColorType.
280
281 @param x column index, zero or greater, and less than width()
282 @param y row index, zero or greater, and less than height()
283 @return readable generic pointer to pixel
284 */
reed3601f282016-02-05 11:18:39 -0800285 const void* addr(int x, int y) const {
286 return (const char*)fPixels + fInfo.computeOffset(x, y, fRowBytes);
reed92fc2ae2015-05-22 08:06:21 -0700287 }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400288
289 /** Returns readable base pixel address. Result is addressable as unsigned 8-bit bytes.
290 Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType or
291 kGray_8_SkColorType, and is built with SK_DEBUG defined.
292
293 One byte corresponds to one pixel.
294
295 @return readable unsigned 8-bit pointer to pixels
296 */
reed92fc2ae2015-05-22 08:06:21 -0700297 const uint8_t* addr8() const {
298 SkASSERT(1 == SkColorTypeBytesPerPixel(fInfo.colorType()));
299 return reinterpret_cast<const uint8_t*>(fPixels);
300 }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400301
302 /** Returns readable base pixel address. Result is addressable as unsigned 16-bit words.
303 Will trigger an assert() if SkColorType is not kRGB_565_SkColorType or
304 kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
305
306 One word corresponds to one pixel.
307
308 @return readable unsigned 16-bit pointer to pixels
309 */
reed3601f282016-02-05 11:18:39 -0800310 const uint16_t* addr16() const {
311 SkASSERT(2 == SkColorTypeBytesPerPixel(fInfo.colorType()));
312 return reinterpret_cast<const uint16_t*>(fPixels);
313 }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400314
315 /** Returns readable base pixel address. Result is addressable as unsigned 32-bit words.
316 Will trigger an assert() if SkColorType is not kRGBA_8888_SkColorType or
317 kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.
318
319 One word corresponds to one pixel.
320
321 @return readable unsigned 32-bit pointer to pixels
322 */
reed3601f282016-02-05 11:18:39 -0800323 const uint32_t* addr32() const {
324 SkASSERT(4 == SkColorTypeBytesPerPixel(fInfo.colorType()));
325 return reinterpret_cast<const uint32_t*>(fPixels);
326 }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400327
328 /** Returns readable base pixel address. Result is addressable as unsigned 64-bit words.
329 Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built
330 with SK_DEBUG defined.
331
332 One word corresponds to one pixel.
333
334 @return readable unsigned 64-bit pointer to pixels
335 */
reed3601f282016-02-05 11:18:39 -0800336 const uint64_t* addr64() const {
337 SkASSERT(8 == SkColorTypeBytesPerPixel(fInfo.colorType()));
338 return reinterpret_cast<const uint64_t*>(fPixels);
339 }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400340
341 /** Returns readable base pixel address. Result is addressable as unsigned 16-bit words.
342 Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built
343 with SK_DEBUG defined.
344
345 Each word represents one color component encoded as a half float.
346 Four words correspond to one pixel.
347
348 @return readable unsigned 16-bit pointer to first component of pixels
349 */
reed3601f282016-02-05 11:18:39 -0800350 const uint16_t* addrF16() const {
351 SkASSERT(8 == SkColorTypeBytesPerPixel(fInfo.colorType()));
352 SkASSERT(kRGBA_F16_SkColorType == fInfo.colorType());
353 return reinterpret_cast<const uint16_t*>(fPixels);
354 }
reed92fc2ae2015-05-22 08:06:21 -0700355
Cary Clark8a02b0b2017-09-21 12:28:43 -0400356 /** Returns readable pixel address at (x, y).
reed3601f282016-02-05 11:18:39 -0800357
Cary Clark8a02b0b2017-09-21 12:28:43 -0400358 Input is not validated: out of bounds values of x or y trigger an assert() if
359 built with SK_DEBUG defined.
360
361 Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType or
362 kGray_8_SkColorType, and is built with SK_DEBUG defined.
363
364 @param x column index, zero or greater, and less than width()
365 @param y row index, zero or greater, and less than height()
366 @return readable unsigned 8-bit pointer to pixel at (x, y)
367 */
reed3601f282016-02-05 11:18:39 -0800368 const uint8_t* addr8(int x, int y) const {
reed92fc2ae2015-05-22 08:06:21 -0700369 SkASSERT((unsigned)x < (unsigned)fInfo.width());
370 SkASSERT((unsigned)y < (unsigned)fInfo.height());
reed3601f282016-02-05 11:18:39 -0800371 return (const uint8_t*)((const char*)this->addr8() + y * fRowBytes + (x << 0));
reed92fc2ae2015-05-22 08:06:21 -0700372 }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400373
374 /** Returns readable pixel address at (x, y).
375
376 Input is not validated: out of bounds values of x or y trigger an assert() if
377 built with SK_DEBUG defined.
378
379 Will trigger an assert() if SkColorType is not kRGB_565_SkColorType or
380 kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
381
382 @param x column index, zero or greater, and less than width()
383 @param y row index, zero or greater, and less than height()
384 @return readable unsigned 16-bit pointer to pixel at (x, y)
385 */
reed92fc2ae2015-05-22 08:06:21 -0700386 const uint16_t* addr16(int x, int y) const {
387 SkASSERT((unsigned)x < (unsigned)fInfo.width());
388 SkASSERT((unsigned)y < (unsigned)fInfo.height());
389 return (const uint16_t*)((const char*)this->addr16() + y * fRowBytes + (x << 1));
390 }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400391
392 /** Returns readable pixel address at (x, y).
393
394 Input is not validated: out of bounds values of x or y trigger an assert() if
395 built with SK_DEBUG defined.
396
397 Will trigger an assert() if SkColorType is not kRGBA_8888_SkColorType or
398 kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.
399
400 @param x column index, zero or greater, and less than width()
401 @param y row index, zero or greater, and less than height()
402 @return readable unsigned 32-bit pointer to pixel at (x, y)
403 */
reed3601f282016-02-05 11:18:39 -0800404 const uint32_t* addr32(int x, int y) const {
reed92fc2ae2015-05-22 08:06:21 -0700405 SkASSERT((unsigned)x < (unsigned)fInfo.width());
406 SkASSERT((unsigned)y < (unsigned)fInfo.height());
reed3601f282016-02-05 11:18:39 -0800407 return (const uint32_t*)((const char*)this->addr32() + y * fRowBytes + (x << 2));
reed92fc2ae2015-05-22 08:06:21 -0700408 }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400409
410 /** Returns readable pixel address at (x, y).
411
412 Input is not validated: out of bounds values of x or y trigger an assert() if
413 built with SK_DEBUG defined.
414
415 Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built
416 with SK_DEBUG defined.
417
418 @param x column index, zero or greater, and less than width()
419 @param y row index, zero or greater, and less than height()
420 @return readable unsigned 64-bit pointer to pixel at (x, y)
421 */
reed3601f282016-02-05 11:18:39 -0800422 const uint64_t* addr64(int x, int y) const {
423 SkASSERT((unsigned)x < (unsigned)fInfo.width());
424 SkASSERT((unsigned)y < (unsigned)fInfo.height());
425 return (const uint64_t*)((const char*)this->addr64() + y * fRowBytes + (x << 3));
426 }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400427
428 /** Returns readable pixel address at (x, y).
429
430 Input is not validated: out of bounds values of x or y trigger an assert() if
431 built with SK_DEBUG defined.
432
433 Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built
434 with SK_DEBUG defined.
435
436 Each unsigned 16-bit word represents one color component encoded as a half float.
437 Four words correspond to one pixel.
438
439 @param x column index, zero or greater, and less than width()
440 @param y row index, zero or greater, and less than height()
441 @return readable unsigned 16-bit pointer to pixel component at (x, y)
442 */
reed3601f282016-02-05 11:18:39 -0800443 const uint16_t* addrF16(int x, int y) const {
444 SkASSERT(kRGBA_F16_SkColorType == fInfo.colorType());
445 return reinterpret_cast<const uint16_t*>(this->addr64(x, y));
reed92fc2ae2015-05-22 08:06:21 -0700446 }
447
Cary Clark8a02b0b2017-09-21 12:28:43 -0400448 /** Returns writable base pixel address.
reed92fc2ae2015-05-22 08:06:21 -0700449
Cary Clark8a02b0b2017-09-21 12:28:43 -0400450 @return writable generic base pointer to pixels
451 */
reed92fc2ae2015-05-22 08:06:21 -0700452 void* writable_addr() const { return const_cast<void*>(fPixels); }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400453
454 /** Returns writable pixel address at (x, y).
455
456 Input is not validated: out of bounds values of x or y trigger an assert() if
457 built with SK_DEBUG defined. Returns zero if SkColorType is kUnknown_SkColorType.
458
459 @param x column index, zero or greater, and less than width()
460 @param y row index, zero or greater, and less than height()
461 @return writable generic pointer to pixel
462 */
reed8c3fd4f2016-04-15 06:59:38 -0700463 void* writable_addr(int x, int y) const {
464 return const_cast<void*>(this->addr(x, y));
465 }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400466
467 /** Returns writable pixel address at (x, y). Result is addressable as unsigned
468 8-bit bytes. Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType
469 or kGray_8_SkColorType, and is built with SK_DEBUG defined.
470
471 One byte corresponds to one pixel.
472
473 @param x column index, zero or greater, and less than width()
474 @param y row index, zero or greater, and less than height()
475 @return writable unsigned 8-bit pointer to pixels
476 */
reed3601f282016-02-05 11:18:39 -0800477 uint8_t* writable_addr8(int x, int y) const {
478 return const_cast<uint8_t*>(this->addr8(x, y));
reed92fc2ae2015-05-22 08:06:21 -0700479 }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400480
481 /** Returns writable_addr pixel address at (x, y). Result is addressable as unsigned
482 16-bit words. Will trigger an assert() if SkColorType is not kRGB_565_SkColorType
483 or kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
484
485 One word corresponds to one pixel.
486
487 @param x column index, zero or greater, and less than width()
488 @param y row index, zero or greater, and less than height()
489 @return writable unsigned 16-bit pointer to pixel
490 */
reed92fc2ae2015-05-22 08:06:21 -0700491 uint16_t* writable_addr16(int x, int y) const {
492 return const_cast<uint16_t*>(this->addr16(x, y));
493 }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400494
495 /** Returns writable pixel address at (x, y). Result is addressable as unsigned
496 32-bit words. Will trigger an assert() if SkColorType is not
497 kRGBA_8888_SkColorType or kBGRA_8888_SkColorType, and is built with SK_DEBUG
498 defined.
499
500 One word corresponds to one pixel.
501
502 @param x column index, zero or greater, and less than width()
503 @param y row index, zero or greater, and less than height()
504 @return writable unsigned 32-bit pointer to pixel
505 */
reed3601f282016-02-05 11:18:39 -0800506 uint32_t* writable_addr32(int x, int y) const {
507 return const_cast<uint32_t*>(this->addr32(x, y));
508 }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400509
510 /** Returns writable pixel address at (x, y). Result is addressable as unsigned
511 64-bit words. Will trigger an assert() if SkColorType is not
512 kRGBA_F16_SkColorType and is built with SK_DEBUG defined.
513
514 One word corresponds to one pixel.
515
516 @param x column index, zero or greater, and less than width()
517 @param y row index, zero or greater, and less than height()
518 @return writable unsigned 64-bit pointer to pixel
519 */
reed3601f282016-02-05 11:18:39 -0800520 uint64_t* writable_addr64(int x, int y) const {
521 return const_cast<uint64_t*>(this->addr64(x, y));
522 }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400523
524 /** Returns writable pixel address at (x, y). Result is addressable as unsigned
525 16-bit words. Will trigger an assert() if SkColorType is not
526 kRGBA_F16_SkColorType and is built with SK_DEBUG defined.
527
528 Each word represents one color component encoded as a half float.
529 Four words correspond to one pixel.
530
531 @param x column index, zero or greater, and less than width()
532 @param y row index, zero or greater, and less than height()
533 @return writable unsigned 16-bit pointer to first component of pixel
534 */
reed3601f282016-02-05 11:18:39 -0800535 uint16_t* writable_addrF16(int x, int y) const {
536 return reinterpret_cast<uint16_t*>(writable_addr64(x, y));
reed92fc2ae2015-05-22 08:06:21 -0700537 }
538
Cary Clark8a02b0b2017-09-21 12:28:43 -0400539 /** Copies a SkRect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not exceed
540 (this->width(), this->height()).
reed95d343f2015-05-23 13:21:06 -0700541
Cary Clark8a02b0b2017-09-21 12:28:43 -0400542 dstInfo specifies width, height, SkColorType, SkAlphaType, and
543 SkColorSpace of destination. dstRowBytes specifics the gap from one destination
544 row to the next. Returns true if pixels are copied. Returns false if
545 dstInfo.addr() equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes().
546
547 Pixels are copied only if pixel conversion is possible. If this->colorType() is
548 kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match.
549 If this->colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match.
550 If this->alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must
551 match. If this->colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns
552 false if pixel conversion is not possible.
553
554 srcX and srcY may be negative to copy only top or left of source. Returns
555 false if width() or height() is zero or negative. Returns false if
556 abs(srcX) >= this->width(), or if
557 abs(srcY) >= this->height().
558
559 If behavior is SkTransferFunctionBehavior::kRespect: converts source
560 pixels to a linear space before converting to dstInfo.
561 If behavior is SkTransferFunctionBehavior::kIgnore: source
562 pixels are treated as if they are linear, regardless of their encoding.
563
564 @param dstInfo destination width, height, SkColorType, SkAlphaType, SkColorSpace
565 @param dstPixels destination pixel storage
566 @param dstRowBytes destination row length
567 @param srcX column index whose absolute value is less than width()
568 @param srcY row index whose absolute value is less than height()
569 @param behavior one of: SkTransferFunctionBehavior::kRespect,
570 SkTransferFunctionBehavior::kIgnore
571 @return true if pixels are copied to dstPixels
572 */
reed95d343f2015-05-23 13:21:06 -0700573 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
Matt Sarett3928ff82017-06-06 10:11:34 -0400574 int srcX, int srcY, SkTransferFunctionBehavior behavior) const;
Cary Clark8a02b0b2017-09-21 12:28:43 -0400575
576 /** Copies a SkRect of pixels to dstPixels. Copy starts at (0, 0), and does not
577 exceed
578 (this->width(), this->height()).
579
580 dstInfo specifies width, height, SkColorType, SkAlphaType, and
581 SkColorSpace of destination. dstRowBytes specifics the gap from one destination
582 row to the next. Returns true if pixels are copied. Returns false if
583 dstInfo.addr() equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes().
584
585 Pixels are copied only if pixel conversion is possible. If this->colorType() is
586 kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match.
587 If this->colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match.
588 If this->alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must
589 match. If this->colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns
590 false if pixel conversion is not possible.
591
592 Returns false if this->width() or this->height() is zero or negative.
593
594 @param dstInfo destination width, height, SkColorType, SkAlphaType, SkColorSpace
595 @param dstPixels destination pixel storage
596 @param dstRowBytes destination row length
597 @return true if pixels are copied to dstPixels
598 */
reed95d343f2015-05-23 13:21:06 -0700599 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes) const {
600 return this->readPixels(dstInfo, dstPixels, dstRowBytes, 0, 0);
601 }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400602
603 /** Copies a SkRect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not
604 exceed
605 (this->width(), this->height()).
606
607 dstInfo specifies width, height, SkColorType, SkAlphaType, and
608 SkColorSpace of destination. dstRowBytes specifics the gap from one destination
609 row to the next. Returns true if pixels are copied. Returns false if
610 dstInfo.addr() equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes().
611
612 Pixels are copied only if pixel conversion is possible. If this->colorType() is
613 kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match.
614 If this->colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match.
615 If this->alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must
616 match. If this->colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns
617 false if pixel conversion is not possible.
618
619 srcX and srcY may be negative to copy only top or left of source. Returns
620 false if this->width() or this->height() is zero or negative. Returns false if
621 abs(srcX) >= this->width(), or if
622 abs(srcY) >= this->height().
623
624 @param dstInfo destination width, height, SkColorType, SkAlphaType, SkColorSpace
625 @param dstPixels destination pixel storage
626 @param dstRowBytes destination row length
627 @param srcX column index whose absolute value is less than width()
628 @param srcY row index whose absolute value is less than height()
629 @return true if pixels are copied to dstPixels
630 */
Matt Sarett3928ff82017-06-06 10:11:34 -0400631 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, int srcX,
632 int srcY) const {
633 return this->readPixels(dstInfo, dstPixels, dstRowBytes, srcX, srcY,
634 SkTransferFunctionBehavior::kRespect);
635 }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400636
637 /** Copies a SkRect of pixels to dst. Copy starts at (srcX, srcY), and does not
638 exceed (this->width(), this->height()). dst specifies width, height, SkColorType,
639 SkAlphaType, and SkColorSpace of destination. Returns true if pixels are copied.
640 Returns false if dst.addr() equals nullptr, or dst.rowBytes() is less than
641 dst SkImageInfo::minRowBytes.
642
643 Pixels are copied only if pixel conversion is possible. If this->colorType() is
644 kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.info().colorType must match.
645 If this->colorType() is kGray_8_SkColorType, dst.info().colorSpace must match.
646 If this->alphaType() is kOpaque_SkAlphaType, dst.info().alphaType must
647 match. If this->colorSpace() is nullptr, dst.info().colorSpace must match. Returns
648 false if pixel conversion is not possible.
649
650 srcX and srcY may be negative to copy only top or left of source. Returns
651 false this->width() or this->height() is zero or negative. Returns false if
652 abs(srcX) >= this->width(), or if
653 abs(srcY) >= this->height().
654
655 @param dst SkImageInfo and pixel address to write to
656 @param srcX column index whose absolute value is less than width()
657 @param srcY row index whose absolute value is less than height()
658 @return true if pixels are copied to dst
659 */
reed95d343f2015-05-23 13:21:06 -0700660 bool readPixels(const SkPixmap& dst, int srcX, int srcY) const {
661 return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), srcX, srcY);
662 }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400663
664 /** Copies pixels inside bounds() to dst. dst specifies width, height, SkColorType,
665 SkAlphaType, and SkColorSpace of destination. Returns true if pixels are copied.
666 Returns false if dst.addr() equals nullptr, or dst.rowBytes() is less than
667 dst SkImageInfo::minRowBytes.
668
669 Pixels are copied only if pixel conversion is possible. If this->colorType() is
670 kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match.
671 If this->colorType() is kGray_8_SkColorType, dst SkColorSpace must match.
672 If this->alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must
673 match. If this->colorSpace() is nullptr, dst SkColorSpace must match. Returns
674 false if pixel conversion is not possible.
675
676 Returns false if this->width() or this->height() is zero or negative.
677
678 @param dst SkImageInfo and pixel address to write to
679 @return true if pixels are copied to dst
680 */
reed95d343f2015-05-23 13:21:06 -0700681 bool readPixels(const SkPixmap& dst) const {
682 return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), 0, 0);
683 }
684
Cary Clark8a02b0b2017-09-21 12:28:43 -0400685 /** Copies this to dst, scaling pixels to fit dst.width() and dst.height(), and
686 converting pixels to match dst.colorType() and dst.alphaType(). Returns true if
687 pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes() is
688 less than dst SkImageInfo::minRowBytes.
689
690 Pixels are copied only if pixel conversion is possible. If this->colorType() is
691 kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match.
692 If this->colorType() is kGray_8_SkColorType, dst SkColorSpace must match.
693 If this->alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must
694 match. If this->colorSpace() is nullptr, dst SkColorSpace must match. Returns
695 false if pixel conversion is not possible.
696
697 Returns false if this->width() or this->height() is zero or negative.
698
699 Scales the image, with filterQuality, to match dst.width() and dst.height().
700 filterQuality kNone_SkFilterQuality is fastest, typically implemented with
701 nearest neighbor filter. kLow_SkFilterQuality is typically implemented with
702 bilerp filter. kMedium_SkFilterQuality is typically implemented with
703 bilerp filter, and Filter_Quality_MipMap when size is reduced.
704 kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
705
706 @param dst SkImageInfo and pixel address to write to
707 @param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
708 kMedium_SkFilterQuality, kHigh_SkFilterQuality
709 @return true if pixels are copied to dst
710 */
Cary Clarked294b42017-09-14 10:36:51 -0400711 bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality) const;
reed09553032015-11-23 12:32:16 -0800712
Cary Clark8a02b0b2017-09-21 12:28:43 -0400713 /** Writes color to pixels bounded by subset; returns true on success.
714 Returns false if colorType() is kUnknown_SkColorType, or if subset does
715 not intersect bounds().
716
717 @param color unpremultiplied color to write
718 @param subset bounding integer SkRect of written pixels
719 @return true if pixels are changed
720 */
Cary Clarked294b42017-09-14 10:36:51 -0400721 bool erase(SkColor color, const SkIRect& subset) const;
reed7aefe032015-06-08 10:22:22 -0700722
Cary Clark8a02b0b2017-09-21 12:28:43 -0400723 /** Writes color to pixels inside bounds(); returns true on success.
724 Returns false if colorType() is kUnknown_SkColorType, or if bounds()
725 is empty.
726
727 @param color unpremultiplied color to write
728 @return true if pixels are changed
729 */
reed7aefe032015-06-08 10:22:22 -0700730 bool erase(SkColor color) const { return this->erase(color, this->bounds()); }
Cary Clark8a02b0b2017-09-21 12:28:43 -0400731
732 /** Writes color to pixels bounded by subset; returns true on success.
733 if subset is nullptr, writes colors pixels inside bounds(). Returns false if
734 colorType() is kUnknown_SkColorType, if subset is not nullptr and does
735 not intersect bounds(), or if subset is nullptr and bounds() is empty.
736
737 @param color unpremultiplied color to write
738 @param subset bounding integer SkRect of pixels to write; may be nullptr
739 @return true if pixels are changed
740 */
Cary Clarkec2455b2017-09-05 09:42:43 -0400741 bool erase(const SkColor4f& color, const SkIRect* subset = nullptr) const;
reed7aefe032015-06-08 10:22:22 -0700742
reed92fc2ae2015-05-22 08:06:21 -0700743private:
744 const void* fPixels;
reed92fc2ae2015-05-22 08:06:21 -0700745 size_t fRowBytes;
746 SkImageInfo fInfo;
747};
748
reed92fc2ae2015-05-22 08:06:21 -0700749#endif