blob: 9cec400b4bf17aa0228772a7f2ada828456ea3e7 [file] [log] [blame]
Cary Clarkd0530ba2017-09-14 11:25:39 -04001#Topic Pixmap
Cary Clarke4aa3712017-09-15 02:56:12 -04002#Alias Pixmap_Reference
3
Cary Clarkd0530ba2017-09-14 11:25:39 -04004#Class SkPixmap
5
6Pixmap provides a utility to pair SkImageInfo with pixels and row bytes.
7Pixmap is a low level class which provides convenience functions to access
8raster destinations. Canvas can not draw Pixmap, nor does Pixmap provide
9a direct drawing destination.
10
11Use Bitmap to draw pixels referenced by Pixmap; use Surface to draw into
12pixels referenced by Pixmap.
13
Cary Clarkbc5697d2017-10-04 14:31:33 -040014Pixmap does not try to manage the lifetime of the pixel memory. Use Pixel_Ref
15to manage pixel memory; Pixel_Ref is safe across threads.
Cary Clarkd0530ba2017-09-14 11:25:39 -040016
17#Topic Overview
18
19#Subtopic Subtopics
20#Table
21#Legend
22# topics # description ##
23#Legend ##
24# Image_Info_Access # Returns all or part of Image_Info. ##
25# Initialization # Sets fields for use. ##
26# Reader # Examine pixel value. ##
27# Writer # Copy to pixel values. ##
28# Readable_Address # Returns read only pixels. ##
29# Writable_Address # Returns writable pixels. ##
30#Table ##
31#Subtopic ##
32
33#Subtopic Constructors
34#Table
35#Legend
36# # description ##
37#Legend ##
38# SkPixmap() # Constructs with default values. ##
39# SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes) # Constructs from Image_Info, pixels. ##
40#Table ##
41#Subtopic ##
42
43#Subtopic Member_Functions
44#Table
45#Legend
46# function # description ##
47#Legend ##
48# addr() # Returns readable pixel address as void pointer. ##
49# addr16 # Returns readable pixel address as 16-bit pointer. ##
50# addr32 # Returns readable pixel address as 32-bit pointer. ##
51# addr64 # Returns readable pixel address as 64-bit pointer. ##
52# addr8 # Returns readable pixel address as 8-bit pointer. ##
53# addrF16 # Returns readable pixel component address as 16-bit pointer. ##
54# alphaType # Returns Image_Info Alpha_Type. ##
55# bounds() # Returns width and height as Rectangle. ##
56# colorSpace # Returns Image_Info Color_Space. ##
57# colorType # Returns Image_Info Color_Type. ##
Cary Clark154beea2017-10-26 07:58:48 -040058# computeByteSize # Returns size required for pixels. ##
Cary Clarkd0530ba2017-09-14 11:25:39 -040059# computeIsOpaque # Returns true if all pixels are opaque. ##
60# erase() # Writes Color to pixels. ##
61# extractSubset # Sets pointer to portion of original. ##
62# getColor # Returns one pixel as Unpremultiplied Color. ##
Cary Clarkd0530ba2017-09-14 11:25:39 -040063# height() # Returns pixel row count. ##
64# info() # Returns Image_Info. ##
65# isOpaque # Returns true if Image_Info describes opaque pixels. ##
66# readPixels # Copies and converts pixels. ##
67# reset() # Reuses existing Pixmap with replacement values. ##
68# rowBytes # Returns interval between rows in bytes. ##
69# rowBytesAsPixels # Returns interval between rows in pixels. ##
70# scalePixels # Scales and converts pixels. ##
71# setColorSpace # Sets Image_Info Color_Space. ##
72# shiftPerPixel # Returns bit shift from pixels to bytes. ##
73# width() # Returns pixel column count. ##
74# writable_addr # Returns writable pixel address as void pointer. ##
75# writable_addr16 # Returns writable pixel address as 16-bit pointer. ##
76# writable_addr32 # Returns writable pixel address as 32-bit pointer. ##
77# writable_addr64 # Returns writable pixel address as 64-bit pointer. ##
78# writable_addr8 # Returns writable pixel address as 8-bit pointer. ##
79# writable_addrF16 # Returns writable pixel component address as 16-bit pointer. ##
80#Table ##
81#Subtopic ##
82
83#Topic Overview ##
84
85#Subtopic Initialization
86
87# ------------------------------------------------------------------------------
88
89#Method SkPixmap()
90
91Creates an empty Pixmap without pixels, with kUnknown_SkColorType, with
92kUnknown_SkAlphaType, and with a width and height of zero. Use
93reset() to associate pixels, SkColorType, SkAlphaType, width, and height
94after Pixmap has been created.
95
96#Return empty Pixmap ##
97
98#Example
Cary Clark6fc50412017-09-21 12:31:06 -040099void draw(SkCanvas* canvas) {
100 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
101 const char* colors[] = {"Unknown", "Alpha", "RGB_565", "ARGB_4444", "RGBA_8888", "BGRA_8888",
102 "Gray_8", "RGBA_F16"};
103 SkPixmap pixmap;
104 for (int i = 0; i < 2; ++i) {
105 SkDebugf("width: %2d height: %2d", pixmap.width(), pixmap.height());
106 SkDebugf(" color: k%s_SkColorType", colors[pixmap.colorType()]);
107 SkDebugf(" alpha: k%s_SkAlphaType\n", alphas[pixmap.alphaType()]);
108 pixmap.reset(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType),
109 nullptr, 0);
110 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400111}
112#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400113width: 0 height: 0 color: kUnknown_SkColorType alpha: kUnknown_SkAlphaType
Cary Clarkd0530ba2017-09-14 11:25:39 -0400114width: 25 height: 35 color: kRGBA_8888_SkColorType alpha: kOpaque_SkAlphaType
115##
116##
117
118#SeeAlso SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes) reset() SkAlphaType SkColorType
119
120##
121
122# ------------------------------------------------------------------------------
123
124#Method SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes)
125
126Creates Pixmap from info width, height, SkAlphaType, and SkColorType.
Cary Clark6fc50412017-09-21 12:31:06 -0400127addr points to pixels, or nullptr. rowBytes should be info.width() times
128info.bytesPerPixel(), or larger.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400129
130No parameter checking is performed; it is up to the caller to ensure that
131addr and rowBytes agree with info.
132
Cary Clark6fc50412017-09-21 12:31:06 -0400133The memory lifetime of pixels is managed by the caller. When Pixmap goes
Cary Clarkd0530ba2017-09-14 11:25:39 -0400134out of scope, addr is unaffected.
135
136Pixmap may be later modified by reset() to change its size, pixel type, or
137storage.
138
139#Param info width, height, SkAlphaType, SkColorType of Image_Info ##
140#Param addr pointer to pixels allocated by caller; may be nullptr ##
141#Param rowBytes size of one row of addr; width times pixel size, or larger ##
142
143#Return initialized Pixmap ##
144
145#Example
146#Image 3
147#Description
148SkImage::MakeRasterCopy takes const SkPixmap& as an argument. The example
149constructs a SkPixmap from the brace-delimited parameters.
150##
Cary Clark6fc50412017-09-21 12:31:06 -0400151 SkDebugf("image alpha only = %s\n", image->isAlphaOnly() ? "true" : "false");
152 SkPMColor pmColors = 0;
153 sk_sp<SkImage> copy = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1),
154 (uint8_t*)&pmColors,
155 1});
Cary Clarkd0530ba2017-09-14 11:25:39 -0400156 SkDebugf("copy alpha only = %s\n", copy->isAlphaOnly() ? "true" : "false");
157#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400158image alpha only = false
Cary Clarkd0530ba2017-09-14 11:25:39 -0400159copy alpha only = true
160##
161##
162
163#SeeAlso SkPixmap() reset() SkAlphaType SkColorType
164
165##
166
167# ------------------------------------------------------------------------------
168
169#Method void reset()
170
171Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to
172kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType.
173
174The prior pixels are unaffected; it is up to the caller to release pixels
175memory if desired.
176
177#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400178void draw(SkCanvas* canvas) {
179 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
180 const char* colors[] = {"Unknown", "Alpha", "RGB_565", "ARGB_4444", "RGBA_8888", "BGRA_8888",
181 "Gray_8", "RGBA_F16"};
182 SkPixmap pixmap(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType),
183 nullptr, 0);
184 for (int i = 0; i < 2; ++i) {
185 SkDebugf("width: %2d height: %2d", pixmap.width(), pixmap.height());
186 SkDebugf(" color: k%s_SkColorType", colors[pixmap.colorType()]);
187 SkDebugf(" alpha: k%s_SkAlphaType\n", alphas[pixmap.alphaType()]);
188 pixmap.reset();
189 }
190}
Cary Clarkd0530ba2017-09-14 11:25:39 -0400191#StdOut
192width: 25 height: 35 color: kRGBA_8888_SkColorType alpha: kOpaque_SkAlphaType
Cary Clark6fc50412017-09-21 12:31:06 -0400193width: 0 height: 0 color: kUnknown_SkColorType alpha: kUnknown_SkAlphaType
Cary Clarkd0530ba2017-09-14 11:25:39 -0400194##
195##
196
197#SeeAlso SkPixmap() SkAlphaType SkColorType
198
199##
200
201# ------------------------------------------------------------------------------
202
203#Method void reset(const SkImageInfo& info, const void* addr, size_t rowBytes)
204
205Sets width, height, SkAlphaType, and SkColorType from info.
206Sets pixel address from addr, which may be nullptr.
Cary Clark6fc50412017-09-21 12:31:06 -0400207Sets row bytes from rowBytes, which should be info.width() times
208info.bytesPerPixel(), or larger.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400209
210Does not check addr. Asserts if built with SK_DEBUG defined and if rowBytes is
211too small to hold one row of pixels.
212
213The memory lifetime pixels are managed by the caller. When Pixmap goes
214out of scope, addr is unaffected.
215
216#Param info width, height, SkAlphaType, SkColorType of Image_Info ##
217#Param addr pointer to pixels allocated by caller; may be nullptr ##
218#Param rowBytes size of one row of addr; width times pixel size, or larger ##
219
220#Example
221#Image 4
222#Height 128
Cary Clark6fc50412017-09-21 12:31:06 -0400223void draw(SkCanvas* canvas) {
224 std::vector<int32_t> pixels;
225 pixels.resize(image->height() * image->width() * 4);
226 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
227 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
228 image->readPixels(pixmap, 0, 0);
229 int x = 0;
230 for (auto colorType : { kRGBA_8888_SkColorType, kBGRA_8888_SkColorType } ) {
231 pixmap.reset(SkImageInfo::Make(image->width(), image->height(), colorType,
232 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
233 SkBitmap bitmap;
234 bitmap.installPixels(pixmap);
235 canvas->drawBitmap(bitmap, x, 0);
236 x += 128;
237 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400238}
239##
240
241#SeeAlso SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes) reset() SkAlphaType SkColorType
242
243##
244
245# ------------------------------------------------------------------------------
246
247#Method void setColorSpace(sk_sp<SkColorSpace> colorSpace)
248
249
250Changes Color_Space in Image_Info; preserves width, height, SkAlphaType, and
251SkColorType in Image, and leaves pixel address and row bytes unchanged.
Cary Clark6fc50412017-09-21 12:31:06 -0400252Color_Space reference count is incremented.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400253
254#Param colorSpace Color_Space moved to Image_Info ##
255
256#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400257void draw(SkCanvas* canvas) {
258 SkPixmap pixmap;
259 sk_sp<SkColorSpace> colorSpace1 = SkColorSpace::MakeRGB(SkColorSpace::kLinear_RenderTargetGamma,
260 SkColorSpace::kRec2020_Gamut);
261 SkDebugf("is %sunique\n", colorSpace1->unique() ? "" : "not ");
262 pixmap.setColorSpace(colorSpace1);
263 SkDebugf("is %sunique\n", colorSpace1->unique() ? "" : "not ");
Cary Clarkd0530ba2017-09-14 11:25:39 -0400264}
265#StdOut
266is unique
267is not unique
268##
269##
270
271#SeeAlso Color_Space SkImageInfo::makeColorSpace
272
273##
274
275# ------------------------------------------------------------------------------
276
277#Method bool SK_WARN_UNUSED_RESULT reset(const SkMask& mask)
278
279Sets width, height, pixel address, and row bytes to Mask properties, if Mask
280format is SkMask::kA8_Format; and returns true. Otherwise sets width, height,
281row bytes to zero; pixel address to nullptr; SkColorType to kUnknown_SkColorType;
282and SkAlphaType to kUnknown_SkAlphaType; and returns false.
283
284Failing to read the return value generates a compile time warning.
285
286#Param mask Mask containing pixels and dimensions ##
287
288#Return true if set to Mask properties ##
289
290#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400291 const int width = 2;
292 const int height = 2;
293 uint8_t bytes[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
294 SkMask mask;
295 mask.fFormat = SkMask::kA8_Format;
296 mask.fBounds = {0, 0, width, height};
297 mask.fImage = bytes;
298 mask.fRowBytes = (width + 7) >> 3;
299 SkPixmap pixmap;
300 bool success = pixmap.reset(mask);
301 SkDebugf("success: %s width: %d height: %d\n", success ? "true " : "false",
302 pixmap.width(), pixmap.height());
303 mask.fFormat = SkMask::kBW_Format;
304 success = pixmap.reset(mask);
305 SkDebugf("success: %s width: %d height: %d\n", success ? "true " : "false",
306 pixmap.width(), pixmap.height());
307#StdOut
308success: true width: 2 height: 2
309success: false width: 0 height: 0
310##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400311##
312
313#SeeAlso Mask reset()
314
315##
316
317# ------------------------------------------------------------------------------
318
319#Method bool SK_WARN_UNUSED_RESULT extractSubset(SkPixmap* subset, const SkIRect& area) const
320
321Sets subset width, height, pixel address to intersection of Pixmap with area,
322if intersection is not empty; and return true. Otherwise, leave subset unchanged
323and return false.
324
325Failing to read the return value generates a compile time warning.
326
327#Param subset storage for width, height, pixel address of intersection ##
328#Param area bounds to intersect with Pixmap ##
329
330#Return true if intersection of Pixmap and area is not empty ##
331
332#Example
333#Image 3
334#Height 128
Cary Clark6fc50412017-09-21 12:31:06 -0400335void draw(SkCanvas* canvas) {
336 std::vector<int32_t> pixels;
337 pixels.resize(image->height() * image->width() * 4);
338 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
339 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
340 image->readPixels(pixmap, 0, 0);
341 SkPixmap inset;
342 if (pixmap.extractSubset(&inset, {128, 128, 512, 512})) {
343 SkBitmap bitmap;
344 bitmap.installPixels(inset);
345 canvas->drawBitmap(bitmap, 0, 0);
346 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400347}
348##
349
350#SeeAlso reset() SkIRect::intersect
351
352##
353
354#Subtopic Initialization ##
355
356#Subtopic Image_Info_Access
357
358# ------------------------------------------------------------------------------
359
360#Method const SkImageInfo& info() const
361
Cary Clark6fc50412017-09-21 12:31:06 -0400362Returns width, height, Alpha_Type, Color_Type, and Color_Space.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400363
364#Return reference to ImageInfo ##
365
366#Example
367#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -0400368 std::vector<int32_t> pixels;
369 pixels.resize(image->height() * image->width() * 4);
370 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
371 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
372 image->readPixels(pixmap, 0, 0);
373 SkPixmap inset;
374 if (pixmap.extractSubset(&inset, {128, 128, 512, 512})) {
375 const SkImageInfo& info = inset.info();
376 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
377 const char* colors[] = {"Unknown", "Alpha", "RGB_565", "ARGB_4444",
378 "RGBA_8888", "BGRA_8888", "Gray_8", "RGBA_F16"};
379 SkDebugf("width: %d height: %d color: %s alpha: %s\n", info.width(), info.height(),
380 colors[info.colorType()], alphas[info.alphaType()]);
381 }
382#StdOut
383width: 384 height: 384 color: BGRA_8888 alpha: Opaque
384##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400385##
386
387#SeeAlso Image_Info
388
389##
390
391# ------------------------------------------------------------------------------
392
393#Method size_t rowBytes() const
394
395Returns row bytes, the interval from one pixel row to the next. Row bytes
Cary Clark154beea2017-10-26 07:58:48 -0400396is at least as large as:
Cary Clarkd0530ba2017-09-14 11:25:39 -0400397#Formula
398width() * info().bytesPerPixel()
399##
400.
401
Cary Clarkbc5697d2017-10-04 14:31:33 -0400402Returns zero if colorType is kUnknown_SkColorType.
403It is up to the Bitmap creator to ensure that row bytes is a useful value.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400404
405#Return byte length of pixel row ##
406
407#Example
408SkPixmap badPixmap = {SkImageInfo::MakeA8(4, 4), nullptr, 2};
409SkPixmap okPixmap = {SkImageInfo::MakeA8(4, 4), nullptr, 8};
410for (auto& pixmap : { badPixmap, okPixmap } ) {
411 SkDebugf("rowBytes: %d minRowBytes: %d\n", pixmap.rowBytes(),
412 pixmap.info().minRowBytes());
413}
414#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400415rowBytes: 2 minRowBytes: 4
Cary Clarkd0530ba2017-09-14 11:25:39 -0400416rowBytes: 8 minRowBytes: 4
417##
418##
419
420#SeeAlso addr() info() SkImageInfo::minRowBytes
421
422##
423
424# ------------------------------------------------------------------------------
425
426#Method const void* addr() const
427
Cary Clark6fc50412017-09-21 12:31:06 -0400428Returns pixel address, the base address corresponding to the pixel origin.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400429
430It is up to the Pixmap creator to ensure that pixel address is a useful value.
431
432#Return pixel address ##
433
434#Example
435#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -0400436 std::vector<int32_t> pixels;
437 pixels.resize(image->height() * image->width() * 4);
438 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
439 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
440 image->readPixels(pixmap, 0, 0);
441 SkDebugf("pixels address: 0x%llx\n", pixmap.addr());
442 SkPixmap inset;
443 if (pixmap.extractSubset(&inset, {128, 128, 512, 512})) {
444 SkDebugf("inset address: 0x%llx\n", inset.addr());
445 }
446#StdOut
447#Volatile
448pixels address: 0x7f2a440bb010
449inset address: 0x7f2a440fb210
450##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400451##
452
453#SeeAlso addr(int x, int y) addr8 addr16 addr32 addr64 info() rowBytes()
454
455##
456
457# ------------------------------------------------------------------------------
458
459#Method int width() const
460
461Returns pixel count in each pixel row. Should be equal or less than:
Cary Clark154beea2017-10-26 07:58:48 -0400462
Cary Clarkd0530ba2017-09-14 11:25:39 -0400463#Formula
Cary Clarkbc5697d2017-10-04 14:31:33 -0400464rowBytes() / info().bytesPerPixel()
Cary Clarkd0530ba2017-09-14 11:25:39 -0400465##
466.
467
468#Return pixel width in Image_Info ##
469
470#Example
471 SkImageInfo info = SkImageInfo::MakeA8(16, 32);
Cary Clark6fc50412017-09-21 12:31:06 -0400472 SkPixmap pixmap(info, nullptr, 64);
473 SkDebugf("pixmap width: %d info width: %d\n", pixmap.width(), info.width());
474#StdOut
475pixmap width: 16 info width: 16
476##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400477##
478
Cary Clarkbc5697d2017-10-04 14:31:33 -0400479#SeeAlso height() SkImageInfo::width()
Cary Clarkd0530ba2017-09-14 11:25:39 -0400480
481##
482
483# ------------------------------------------------------------------------------
484
485#Method int height() const
486
487Returns pixel row count.
488
489#Return pixel height in Image_Info ##
490
491#Example
Cary Clarkbc5697d2017-10-04 14:31:33 -0400492 SkPixmap pixmap(SkImageInfo::MakeA8(16, 32), nullptr, 64);
493 SkDebugf("pixmap height: %d info height: %d\n", pixmap.height(), pixmap.info().height());
Cary Clark6fc50412017-09-21 12:31:06 -0400494#StdOut
495pixmap height: 32 info height: 32
496##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400497##
498
Cary Clarkbc5697d2017-10-04 14:31:33 -0400499#SeeAlso width() ImageInfo::height()
Cary Clarkd0530ba2017-09-14 11:25:39 -0400500
501##
502
503# ------------------------------------------------------------------------------
504
505#Method SkColorType colorType() const
506
507Returns Color_Type, one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
508kRGB_565_SkColorType, kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
509kBGRA_8888_SkColorType, kGray_8_SkColorType, kRGBA_F16_SkColorType.
510
511#Return Color_Type in Image_Info ##
512
513#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400514 const char* colors[] = {"Unknown", "Alpha", "RGB_565", "ARGB_4444",
515 "RGBA_8888", "BGRA_8888", "Gray_8", "RGBA_F16"};
516 SkPixmap pixmap(SkImageInfo::MakeA8(16, 32), nullptr, 64);
517 SkDebugf("color type: k" "%s" "_SkColorType\n", colors[pixmap.colorType()]);
518#StdOut
Cary Clarkd0530ba2017-09-14 11:25:39 -0400519color type: kAlpha_SkColorType
Cary Clark6fc50412017-09-21 12:31:06 -0400520##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400521##
522
Cary Clarkbc5697d2017-10-04 14:31:33 -0400523#SeeAlso alphaType() SkImageInfo::colorType
Cary Clarkd0530ba2017-09-14 11:25:39 -0400524
525##
526
527# ------------------------------------------------------------------------------
528
529#Method SkAlphaType alphaType() const
530
531Returns Alpha_Type, one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
532kPremul_SkAlphaType, kUnpremul_SkAlphaType.
533
534#Return Alpha_Type in Image_Info ##
535
536#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400537 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
538 SkPixmap pixmap(SkImageInfo::MakeA8(16, 32), nullptr, 64);
539 SkDebugf("alpha type: k" "%s" "_SkAlphaType\n", alphas[pixmap.alphaType()]);
Cary Clarkd0530ba2017-09-14 11:25:39 -0400540#StdOut
541alpha type: kPremul_SkAlphaType
542##
543##
544
Cary Clarkbc5697d2017-10-04 14:31:33 -0400545#SeeAlso colorType() SkImageInfo::alphaType
Cary Clarkd0530ba2017-09-14 11:25:39 -0400546
547##
548
549# ------------------------------------------------------------------------------
550
551#Method SkColorSpace* colorSpace() const
552
Cary Clarkbc5697d2017-10-04 14:31:33 -0400553Returns Color_Space associated with Image_Info. The
554reference count of Color_Space is unchanged. The returned Color_Space is
555immutable.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400556
Cary Clarkbc5697d2017-10-04 14:31:33 -0400557#Return Color_Space, the range of colors, in Image_Info ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400558
559#Example
560#Description
561SkColorSpace::MakeSRGBLinear creates Color_Space with linear gamma
562and an sRGB gamut. This Color_Space gamma is not close to sRGB gamma.
563##
Cary Clark6fc50412017-09-21 12:31:06 -0400564 SkPixmap pixmap(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
565 SkColorSpace::MakeSRGBLinear()), nullptr, 64);
566 SkColorSpace* colorSpace = pixmap.colorSpace();
567 SkDebugf("gammaCloseToSRGB: %s gammaIsLinear: %s isSRGB: %s\n",
568 colorSpace->gammaCloseToSRGB() ? "true" : "false",
569 colorSpace->gammaIsLinear() ? "true" : "false",
570 colorSpace->isSRGB() ? "true" : "false");
571#StdOut
572gammaCloseToSRGB: false gammaIsLinear: true isSRGB: false
573##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400574##
575
Cary Clarkbc5697d2017-10-04 14:31:33 -0400576#SeeAlso Color_Space SkImageInfo::colorSpace
Cary Clarkd0530ba2017-09-14 11:25:39 -0400577
578##
579
580# ------------------------------------------------------------------------------
581
582#Method bool isOpaque() const
583
584Returns true if Alpha_Type is kOpaque_SkAlphaType.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400585Does not check if Color_Type allows Alpha, or if any pixel value has
586transparency.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400587
588#Return true if Image_Info has opaque Alpha_Type ##
589
590#Example
591#Description
592 isOpaque ignores whether all pixels are opaque or not.
593##
Cary Clark6fc50412017-09-21 12:31:06 -0400594 std::vector<uint32_t> pixels;
595 const int height = 2;
596 const int width = 2;
597 pixels.resize(height * width * 4);
598 SkPixmap pixmap(SkImageInfo::Make(width, height, kN32_SkColorType,
599 kPremul_SkAlphaType), (const void*) &pixels.front(), width * 4);
600 for (int index = 0; index < 2; ++index) {
601 pixmap.erase(0x00000000);
602 SkDebugf("isOpaque: %s\n", pixmap.isOpaque() ? "true" : "false");
603 pixmap.erase(0xFFFFFFFF);
604 SkDebugf("isOpaque: %s\n", pixmap.isOpaque() ? "true" : "false");
605 pixmap.reset(pixmap.info().makeAlphaType(kOpaque_SkAlphaType),
606 (const void*) &pixels.front(), width * 4);
607 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400608#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400609isOpaque: false
610isOpaque: false
611isOpaque: true
Cary Clarkd0530ba2017-09-14 11:25:39 -0400612isOpaque: true
613##
614##
615
616#SeeAlso computeIsOpaque SkImageInfo::isOpaque
617
618##
619
620# ------------------------------------------------------------------------------
621
622#Method SkIRect bounds() const
623
Cary Clark154beea2017-10-26 07:58:48 -0400624Returns IRect { 0, 0, width(), height() }.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400625
626#Return integral rectangle from origin to width() and height() ##
627
628#Example
629 for (int width : { 0, 2 } ) {
630 for (int height : { 0, 2 } ) {
Cary Clark6fc50412017-09-21 12:31:06 -0400631 SkPixmap pixmap(SkImageInfo::MakeA8(width, height), nullptr, width);
Cary Clarkd0530ba2017-09-14 11:25:39 -0400632 SkDebugf("width: %d height: %d empty: %s\n", width, height,
633 pixmap.bounds().isEmpty() ? "true" : "false");
634 }
635 }
636#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400637width: 0 height: 0 empty: true
638width: 0 height: 2 empty: true
639width: 2 height: 0 empty: true
Cary Clarkd0530ba2017-09-14 11:25:39 -0400640width: 2 height: 2 empty: false
641##
642##
643
644#SeeAlso height() width() IRect
645
646##
647
648# ------------------------------------------------------------------------------
649
650#Method int rowBytesAsPixels() const
651
652
653Returns number of pixels that fit on row. Should be greater than or equal to
654width().
655
656#Return maximum pixels per row ##
657
658#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400659 for (int rowBytes : { 4, 5, 6, 7, 8} ) {
660 SkPixmap pixmap(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), nullptr, rowBytes);
661 SkDebugf("rowBytes: %d rowBytesAsPixels: %d\n", rowBytes, pixmap.rowBytesAsPixels());
662 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400663#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400664rowBytes: 4 rowBytesAsPixels: 1
665rowBytes: 5 rowBytesAsPixels: 1
666rowBytes: 6 rowBytesAsPixels: 1
667rowBytes: 7 rowBytesAsPixels: 1
Cary Clarkd0530ba2017-09-14 11:25:39 -0400668rowBytes: 8 rowBytesAsPixels: 2
669##
670##
671
672#SeeAlso rowBytes shiftPerPixel width SkImageInfo::bytesPerPixel
673
674##
675
676# ------------------------------------------------------------------------------
677
678#Method int shiftPerPixel() const
679
680Returns bit shift converting row bytes to row pixels.
681Returns zero for kUnknown_SkColorType.
682
683#Return one of: 0, 1, 2, 3; left shift to convert pixels to bytes ##
684
685#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400686 const char* colors[] = {"Unknown", "Alpha", "RGB_565", "ARGB_4444",
687 "RGBA_8888", "BGRA_8888", "Gray_8", "RGBA_F16"};
688 SkImageInfo info = SkImageInfo::MakeA8(1, 1);
689 for (SkColorType colorType : { kUnknown_SkColorType, kAlpha_8_SkColorType,
690 kRGB_565_SkColorType, kARGB_4444_SkColorType,
691 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
692 kGray_8_SkColorType, kRGBA_F16_SkColorType } ) {
693 SkPixmap pixmap(info.makeColorType(colorType), nullptr, 4);
694 SkDebugf("color: k" "%s" "_SkColorType" "%*s" "bytesPerPixel: %d shiftPerPixel: %d\n",
695 colors[colorType], 10 - strlen(colors[colorType]), " ",
696 pixmap.info().bytesPerPixel(), pixmap.shiftPerPixel());
697 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400698#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400699color: kUnknown_SkColorType bytesPerPixel: 0 shiftPerPixel: 0
700color: kAlpha_SkColorType bytesPerPixel: 1 shiftPerPixel: 0
701color: kRGB_565_SkColorType bytesPerPixel: 2 shiftPerPixel: 1
702color: kARGB_4444_SkColorType bytesPerPixel: 2 shiftPerPixel: 1
703color: kRGBA_8888_SkColorType bytesPerPixel: 4 shiftPerPixel: 2
704color: kBGRA_8888_SkColorType bytesPerPixel: 4 shiftPerPixel: 2
705color: kGray_8_SkColorType bytesPerPixel: 1 shiftPerPixel: 0
Cary Clarkd0530ba2017-09-14 11:25:39 -0400706color: kRGBA_F16_SkColorType bytesPerPixel: 8 shiftPerPixel: 3
707##
708##
709
710#SeeAlso rowBytes rowBytesAsPixels width SkImageInfo::bytesPerPixel
711
712##
713
714# ------------------------------------------------------------------------------
715
Cary Clarkbc5697d2017-10-04 14:31:33 -0400716#Method size_t computeByteSize() const
717
718Returns minimum memory required for pixel storage.
719Does not include unused memory on last row when rowBytesAsPixels exceeds width().
720Returns zero if result does not fit in size_t.
721Returns zero if height() or width() is 0.
722Returns height() times rowBytes if colorType is kUnknown_SkColorType.
723
724#Return size in bytes of image buffer ##
725
Cary Clarkd0530ba2017-09-14 11:25:39 -0400726#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400727 SkPixmap pixmap;
728 for (int width : { 1, 1000, 1000000 } ) {
729 for (int height: { 1, 1000, 1000000 } ) {
730 SkImageInfo imageInfo = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
Cary Clarkbc5697d2017-10-04 14:31:33 -0400731 pixmap.reset(imageInfo, nullptr, width * 5);
732 SkDebugf("width: %7d height: %7d computeByteSize: %13lld\n", width, height,
733 pixmap.computeByteSize());
Cary Clark6fc50412017-09-21 12:31:06 -0400734 }
735 }
Cary Clark6fc50412017-09-21 12:31:06 -0400736#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400737width: 1 height: 1 computeByteSize: 4
738width: 1 height: 1000 computeByteSize: 4999
739width: 1 height: 1000000 computeByteSize: 4999999
740width: 1000 height: 1 computeByteSize: 4000
741width: 1000 height: 1000 computeByteSize: 4999000
742width: 1000 height: 1000000 computeByteSize: 4999999000
743width: 1000000 height: 1 computeByteSize: 4000000
744width: 1000000 height: 1000 computeByteSize: 4999000000
Cary Clarkbc5697d2017-10-04 14:31:33 -0400745width: 1000000 height: 1000000 computeByteSize: 4999999000000
Cary Clarkd0530ba2017-09-14 11:25:39 -0400746##
747##
748
Cary Clarkbc5697d2017-10-04 14:31:33 -0400749#SeeAlso SkImageInfo::computeByteSize
Cary Clarkd0530ba2017-09-14 11:25:39 -0400750
751##
752
753#Subtopic Image_Info_Access ##
754
755#Subtopic Reader
756
757# ------------------------------------------------------------------------------
758
759#Method bool computeIsOpaque() const
760
761Returns true if all pixels are opaque. Color_Type determines how pixels
762are encoded, and whether pixel describes Alpha. Returns true for Color_Types
Cary Clark6fc50412017-09-21 12:31:06 -0400763without alpha in each pixel; for other Color_Types, returns true if all
Cary Clarkd0530ba2017-09-14 11:25:39 -0400764pixels have alpha values equivalent to 1.0 or greater.
765
766For Color_Types kRGB_565_SkColorType or kGray_8_SkColorType: always
767returns true. For Color_Types kAlpha_8_SkColorType, kBGRA_8888_SkColorType,
768kRGBA_8888_SkColorType: returns true if all pixel Alpha values are 255.
769For Color_Type kARGB_4444_SkColorType: returns true if all pixel Alpha values are 15.
770For kRGBA_F16_SkColorType: returns true if all pixel Alpha values are 1.0 or
771greater.
772
Cary Clark6fc50412017-09-21 12:31:06 -0400773Returns false for kUnknown_SkColorType.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400774
Cary Clarkbc5697d2017-10-04 14:31:33 -0400775#Return true if all pixels have opaque values or Color_Type is opaque ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400776
777#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400778 std::vector<uint32_t> pixels;
779 const int height = 2;
780 const int width = 2;
781 pixels.resize(height * width * 4);
782 SkPixmap pixmap(SkImageInfo::Make(width, height, kN32_SkColorType,
783 kPremul_SkAlphaType), (const void*) &pixels.front(), width * 4);
784 for (int index = 0; index < 2; ++index) {
785 pixmap.erase(0x00000000);
786 SkDebugf("computeIsOpaque: %s\n", pixmap.computeIsOpaque() ? "true" : "false");
787 pixmap.erase(0xFFFFFFFF);
788 SkDebugf("computeIsOpaque: %s\n", pixmap.computeIsOpaque() ? "true" : "false");
789 pixmap.reset(pixmap.info().makeAlphaType(kOpaque_SkAlphaType),
790 (const void*) &pixels.front(), width * 4);
Cary Clarkd0530ba2017-09-14 11:25:39 -0400791 }
792#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400793computeIsOpaque: false
794computeIsOpaque: true
795computeIsOpaque: false
Cary Clarkd0530ba2017-09-14 11:25:39 -0400796computeIsOpaque: true
797##
798##
799
800#SeeAlso isOpaque Color_Type Alpha
801
802##
803
804# ------------------------------------------------------------------------------
805
806#Method SkColor getColor(int x, int y) const
807
808Returns pixel at (x, y) as Unpremultiplied Color.
809Returns black with Alpha if Color_Type is kAlpha_8_SkColorType.
810
811Input is not validated: out of bounds values of x or y trigger an assert() if
812built with SK_DEBUG defined; and returns undefined values or may crash if
813SK_RELEASE is defined. Fails if Color_Type is kUnknown_SkColorType or
814pixel address is nullptr.
815
816Color_Space in Image_Info is ignored. Some Color precision may be lost in the
817conversion to Unpremultiplied Color; original pixel data may have additional
818precision.
819
Cary Clark6fc50412017-09-21 12:31:06 -0400820#Param x column index, zero or greater, and less than width() ##
821#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400822
823#Return pixel converted to Unpremultiplied Color ##
824
825#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400826 const int w = 4;
827 const int h = 4;
828 std::vector<SkPMColor> storage;
829 storage.resize(w * h);
830 SkDebugf("Premultiplied:\n");
831 for (int y = 0; y < h; ++y) {
832 SkDebugf("(0, %d) ", y);
833 for (int x = 0; x < w; ++x) {
834 int a = 0xFF * (x + y) / (w - 1 + h - 1);
835 storage[x + y * w] = SkPackARGB32(a, a * x / (w - 1), a * y / (h - 1), a);
836 SkDebugf("0x%08x%c", storage[x + y * w], x == w - 1 ? '\n' : ' ');
837 }
838 }
839 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), &storage.front(), w * 4);
840 SkDebugf("Unpremultiplied:\n");
841 for (int y = 0; y < h; ++y) {
842 SkDebugf("(0, %d) ", y);
843 for (int x = 0; x < w; ++x) {
844 SkDebugf("0x%08x%c", pixmap.getColor(x, y), x == w - 1 ? '\n' : ' ');
845 }
846 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400847#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400848Premultiplied:
849(0, 0) 0x00000000 0x2a0e002a 0x55380055 0x7f7f007f
850(0, 1) 0x2a000e2a 0x551c1c55 0x7f542a7f 0xaaaa38aa
851(0, 2) 0x55003855 0x7f2a547f 0xaa7171aa 0xd4d48dd4
852(0, 3) 0x7f007f7f 0xaa38aaaa 0xd48dd4d4 0xffffffff
853Unpremultiplied:
854(0, 0) 0x00000000 0x2a5500ff 0x55a800ff 0x7fff00ff
855(0, 1) 0x2a0055ff 0x555454ff 0x7fa954ff 0xaaff54ff
856(0, 2) 0x5500a8ff 0x7f54a9ff 0xaaaaaaff 0xd4ffaaff
Cary Clarkd0530ba2017-09-14 11:25:39 -0400857(0, 3) 0x7f00ffff 0xaa54ffff 0xd4aaffff 0xffffffff
858##
859##
860
861#SeeAlso addr() readPixels
862
863##
864
865#Subtopic Reader ##
866
867#Subtopic Readable_Address
868
869# ------------------------------------------------------------------------------
870
871#Method const void* addr(int x, int y) const
872
Cary Clarkbc5697d2017-10-04 14:31:33 -0400873Returns readable pixel address at (x, y). Returns nullptr if Pixel_Ref is nullptr.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400874
875Input is not validated: out of bounds values of x or y trigger an assert() if
Cary Clarkbc5697d2017-10-04 14:31:33 -0400876built with SK_DEBUG defined. Returns nullptr if Color_Type is kUnknown_SkColorType.
877
878Performs a lookup of pixel size; for better performance, call
879one of: addr8, addr16, addr32, addr64, or addrF16.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400880
Cary Clark6fc50412017-09-21 12:31:06 -0400881#Param x column index, zero or greater, and less than width() ##
882#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400883
884#Return readable generic pointer to pixel ##
885
886#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400887 const int w = 4;
888 const int h = 4;
889 std::vector<SkPMColor> storage;
890 storage.resize(w * h);
891 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), &storage.front(), w * 4);
Cary Clarkd0530ba2017-09-14 11:25:39 -0400892 SkDebugf("pixmap.addr(1, 2) %c= &storage[1 + 2 * w]\n",
893 pixmap.addr(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
894#StdOut
895pixmap.addr(1, 2) == &storage[1 + 2 * w]
896##
897##
898
Cary Clarkbc5697d2017-10-04 14:31:33 -0400899#SeeAlso addr8 addr16 addr32 addr64 addrF16 getColor writable_addr SkBitmap::getAddr
Cary Clarkd0530ba2017-09-14 11:25:39 -0400900
901##
902
903# ------------------------------------------------------------------------------
904
905#Method const uint8_t* addr8() const
906
907Returns readable base pixel address. Result is addressable as unsigned 8-bit bytes.
908Will trigger an assert() if Color_Type is not kAlpha_8_SkColorType or
909kGray_8_SkColorType, and is built with SK_DEBUG defined.
910
911One byte corresponds to one pixel.
912
913#Return readable unsigned 8-bit pointer to pixels ##
914
915#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400916 const int w = 4;
917 const int h = 4;
918 uint8_t storage[w * h];
919 SkPixmap pixmap(SkImageInfo::Make(w, h, kGray_8_SkColorType, kPremul_SkAlphaType),
920 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -0400921 SkDebugf("pixmap.addr8() %c= storage\n",
922 pixmap.addr8() == storage ? '=' : '!');
923#StdOut
924pixmap.addr8() == storage
925##
926##
927
928#SeeAlso addr() addr16 addr32 addr64 addrF16 getColor writable_addr writable_addr8
929
930##
931
932# ------------------------------------------------------------------------------
933
934#Method const uint16_t* addr16() const
935
936Returns readable base pixel address. Result is addressable as unsigned 16-bit words.
937Will trigger an assert() if Color_Type is not kRGB_565_SkColorType or
938kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
939
940One word corresponds to one pixel.
941
942#Return readable unsigned 16-bit pointer to pixels ##
943
944#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400945 const int w = 4;
946 const int h = 4;
947 uint16_t storage[w * h];
948 SkPixmap pixmap(SkImageInfo::Make(w, h, kARGB_4444_SkColorType, kPremul_SkAlphaType),
949 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -0400950 SkDebugf("pixmap.addr16() %c= storage\n",
951 pixmap.addr16() == storage ? '=' : '!');
952#StdOut
953pixmap.addr16() == storage
954##
955##
956
957#SeeAlso addr() addr8 addr32 addr64 addrF16 getColor writable_addr writable_addr16
958
959##
960
961# ------------------------------------------------------------------------------
962
963#Method const uint32_t* addr32() const
964
965Returns readable base pixel address. Result is addressable as unsigned 32-bit words.
966Will trigger an assert() if Color_Type is not kRGBA_8888_SkColorType or
967kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.
968
969One word corresponds to one pixel.
970
971#Return readable unsigned 32-bit pointer to pixels ##
972
973#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400974 const int w = 4;
975 const int h = 4;
976 uint32_t storage[w * h];
977 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType),
978 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -0400979 SkDebugf("pixmap.addr32() %c= storage\n",
980 pixmap.addr32() == storage ? '=' : '!');
981#StdOut
982pixmap.addr32() == storage
983##
984##
985
986#SeeAlso addr() addr8 addr16 addr64 addrF16 getColor writable_addr writable_addr32
987
988##
989
990# ------------------------------------------------------------------------------
991
992#Method const uint64_t* addr64() const
993
994Returns readable base pixel address. Result is addressable as unsigned 64-bit words.
995Will trigger an assert() if Color_Type is not kRGBA_F16_SkColorType and is built
996with SK_DEBUG defined.
997
998One word corresponds to one pixel.
999
1000#Return readable unsigned 64-bit pointer to pixels ##
1001
1002#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001003 const int w = 4;
1004 const int h = 4;
1005 uint64_t storage[w * h];
1006 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType),
1007 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001008 SkDebugf("pixmap.addr64() %c= storage\n",
1009 pixmap.addr64() == storage ? '=' : '!');
1010#StdOut
1011pixmap.addr64() == storage
1012##
1013##
1014
1015#SeeAlso addr() addr8 addr16 addr32 addrF16 getColor writable_addr writable_addr64
1016
1017##
1018
1019# ------------------------------------------------------------------------------
1020
1021#Method const uint16_t* addrF16() const
1022
1023Returns readable base pixel address. Result is addressable as unsigned 16-bit words.
1024Will trigger an assert() if Color_Type is not kRGBA_F16_SkColorType and is built
1025with SK_DEBUG defined.
1026
1027Each word represents one color component encoded as a half float.
1028Four words correspond to one pixel.
1029
1030#Return readable unsigned 16-bit pointer to first component of pixels ##
1031
1032#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001033 const int w = 4;
1034 const int h = 4;
1035 uint16_t storage[w * h * 4];
1036 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType),
1037 storage, w * 4 * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001038 SkDebugf("pixmap.addrF16() %c= storage\n",
1039 pixmap.addrF16() == storage ? '=' : '!');
1040#StdOut
1041pixmap.addrF16() == storage
1042##
1043##
1044
1045#SeeAlso addr() addr8 addr16 addr32 addr64 getColor writable_addr writable_addrF16
1046
1047##
1048
1049# ------------------------------------------------------------------------------
1050
1051#Method const uint8_t* addr8(int x, int y) const
1052
1053Returns readable pixel address at (x, y).
1054
1055Input is not validated: out of bounds values of x or y trigger an assert() if
1056built with SK_DEBUG defined.
1057
1058Will trigger an assert() if Color_Type is not kAlpha_8_SkColorType or
1059kGray_8_SkColorType, and is built with SK_DEBUG defined.
1060
Cary Clark6fc50412017-09-21 12:31:06 -04001061#Param x column index, zero or greater, and less than width() ##
1062#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001063
1064#Return readable unsigned 8-bit pointer to pixel at (x, y) ##
1065
1066#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001067 const int w = 4;
1068 const int h = 4;
1069 uint8_t storage[w * h];
1070 SkPixmap pixmap(SkImageInfo::Make(w, h, kGray_8_SkColorType, kPremul_SkAlphaType),
1071 storage, w * sizeof(storage[0]));
1072 SkDebugf("pixmap.addr8(1, 2) %c= &storage[1 + 2 * w]\n",
1073 pixmap.addr8(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
Cary Clarkd0530ba2017-09-14 11:25:39 -04001074#StdOut
1075pixmap.addr8(1, 2) == &storage[1 + 2 * w]
1076##
1077##
1078
1079#SeeAlso addr() addr16 addr32 addr64 addrF16 getColor writable_addr writable_addr8
1080
1081##
1082
1083# ------------------------------------------------------------------------------
1084
1085#Method const uint16_t* addr16(int x, int y) const
1086
1087Returns readable pixel address at (x, y).
1088
1089Input is not validated: out of bounds values of x or y trigger an assert() if
1090built with SK_DEBUG defined.
1091
1092Will trigger an assert() if Color_Type is not kRGB_565_SkColorType or
1093kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
1094
Cary Clark6fc50412017-09-21 12:31:06 -04001095#Param x column index, zero or greater, and less than width() ##
1096#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001097
1098#Return readable unsigned 16-bit pointer to pixel at (x, y) ##
1099
1100#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001101 const int w = 4;
1102 const int h = 4;
1103 uint16_t storage[w * h];
1104 SkPixmap pixmap(SkImageInfo::Make(w, h, kARGB_4444_SkColorType, kPremul_SkAlphaType),
1105 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001106 SkDebugf("pixmap.addr16(1, 2) %c= &storage[1 + 2 * w]\n",
1107 pixmap.addr16(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
1108#StdOut
1109pixmap.addr16(1, 2) == &storage[1 + 2 * w]
1110##
1111##
1112
1113#SeeAlso addr() addr8 addr32 addr64 addrF16 getColor writable_addr writable_addr16
1114
1115##
1116
1117# ------------------------------------------------------------------------------
1118
1119#Method const uint32_t* addr32(int x, int y) const
1120
1121Returns readable pixel address at (x, y).
1122
1123Input is not validated: out of bounds values of x or y trigger an assert() if
1124built with SK_DEBUG defined.
1125
1126Will trigger an assert() if Color_Type is not kRGBA_8888_SkColorType or
1127kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.
1128
Cary Clark6fc50412017-09-21 12:31:06 -04001129#Param x column index, zero or greater, and less than width() ##
1130#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001131
1132#Return readable unsigned 32-bit pointer to pixel at (x, y) ##
1133
1134#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001135 const int w = 4;
1136 const int h = 4;
1137 uint32_t storage[w * h];
1138 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
1139 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001140 SkDebugf("pixmap.addr32(1, 2) %c= &storage[1 + 2 * w]\n",
1141 pixmap.addr32(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
1142#StdOut
1143pixmap.addr32(1, 2) == &storage[1 + 2 * w]
1144##
1145##
1146
1147#ToDo incomplete ##
1148
1149##
1150
1151# ------------------------------------------------------------------------------
1152
1153#Method const uint64_t* addr64(int x, int y) const
1154
1155Returns readable pixel address at (x, y).
1156
1157Input is not validated: out of bounds values of x or y trigger an assert() if
1158built with SK_DEBUG defined.
1159
1160Will trigger an assert() if Color_Type is not kRGBA_F16_SkColorType and is built
1161with SK_DEBUG defined.
1162
Cary Clark6fc50412017-09-21 12:31:06 -04001163#Param x column index, zero or greater, and less than width() ##
1164#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001165
1166#Return readable unsigned 64-bit pointer to pixel at (x, y) ##
1167
1168#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001169 const int w = 4;
1170 const int h = 4;
1171 uint64_t storage[w * h];
1172 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType),
1173 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001174 SkDebugf("pixmap.addr64(1, 2) %c= &storage[1 + 2 * w]\n",
1175 pixmap.addr64(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
1176#StdOut
1177pixmap.addr64(1, 2) == &storage[1 + 2 * w]
1178##
1179##
1180
1181#SeeAlso addr() addr8 addr16 addr32 addrF16 getColor writable_addr writable_addr64
1182
1183##
1184
1185# ------------------------------------------------------------------------------
1186
1187#Method const uint16_t* addrF16(int x, int y) const
1188
1189Returns readable pixel address at (x, y).
1190
1191Input is not validated: out of bounds values of x or y trigger an assert() if
1192built with SK_DEBUG defined.
1193
1194Will trigger an assert() if Color_Type is not kRGBA_F16_SkColorType and is built
1195with SK_DEBUG defined.
1196
1197Each unsigned 16-bit word represents one color component encoded as a half float.
1198Four words correspond to one pixel.
1199
Cary Clark6fc50412017-09-21 12:31:06 -04001200#Param x column index, zero or greater, and less than width() ##
1201#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001202
1203#Return readable unsigned 16-bit pointer to pixel component at (x, y) ##
1204
1205#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001206 const int w = 4;
1207 const int h = 4;
1208 const int wordsPerPixel = 4;
1209 const int rowWords = w * wordsPerPixel;
1210 uint16_t storage[rowWords * h];
1211 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType),
1212 storage, rowWords * sizeof(storage[0]));
1213 SkDebugf("pixmap.addrF16(1, 2) %c= &storage[1 * wordsPerPixel + 2 * rowWords]\n",
1214 pixmap.addrF16(1, 2) == &storage[1 * wordsPerPixel + 2 * rowWords] ? '=' : '!');
Cary Clarkd0530ba2017-09-14 11:25:39 -04001215#StdOut
1216pixmap.addrF16(1, 2) == &storage[1 * wordsPerPixel + 2 * rowWords]
1217##
1218##
1219
1220#SeeAlso addr() addr8 addr16 addr32 addr64 getColor writable_addr writable_addrF16
1221
1222##
1223
1224#Subtopic Readable_Address ##
1225
1226#Subtopic Writable_Address
1227
1228# ------------------------------------------------------------------------------
1229
1230#Method void* writable_addr() const
1231
1232Returns writable base pixel address.
1233
1234#Return writable generic base pointer to pixels ##
1235
1236#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001237 const int w = 4;
1238 const int h = 4;
1239 SkPMColor storage[w * h * 4];
1240 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), storage, w * 4);
1241 SkDebugf("pixmap.writable_addr() %c= (void *)storage\n",
1242 pixmap.writable_addr() == (void *)storage ? '=' : '!');
1243 pixmap.erase(0x00000000);
1244 *(SkPMColor*)pixmap.writable_addr() = 0xFFFFFFFF;
1245 SkDebugf("pixmap.getColor(0, 1) %c= 0x00000000\n",
1246 pixmap.getColor(0, 1) == 0x00000000 ? '=' : '!');
1247 SkDebugf("pixmap.getColor(0, 0) %c= 0xFFFFFFFF\n",
Cary Clarkd0530ba2017-09-14 11:25:39 -04001248 pixmap.getColor(0, 0) == 0xFFFFFFFF ? '=' : '!');
1249#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -04001250pixmap.writable_addr() == (void *)storage
1251pixmap.getColor(0, 1) == 0x00000000
Cary Clarkd0530ba2017-09-14 11:25:39 -04001252pixmap.getColor(0, 0) == 0xFFFFFFFF
1253##
1254##
1255
1256#SeeAlso writable_addr8 writable_addr16 writable_addr32 writable_addr64 writable_addrF16 addr()
1257
1258##
1259
1260# ------------------------------------------------------------------------------
1261
1262#Method void* writable_addr(int x, int y) const
1263
1264Returns writable pixel address at (x, y).
1265
1266Input is not validated: out of bounds values of x or y trigger an assert() if
1267built with SK_DEBUG defined. Returns zero if Color_Type is kUnknown_SkColorType.
1268
Cary Clark6fc50412017-09-21 12:31:06 -04001269#Param x column index, zero or greater, and less than width() ##
1270#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001271
1272#Return writable generic pointer to pixel ##
1273
1274#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001275 const int w = 4;
1276 const int h = 4;
1277 SkPMColor storage[w * h * 4];
1278 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), storage, w * 4);
1279 SkDebugf("pixmap.writable_addr() %c= (void *)storage\n",
1280 pixmap.writable_addr() == (void *)storage ? '=' : '!');
1281 pixmap.erase(0x00000000);
1282 *(SkPMColor*)pixmap.writable_addr(1, 2) = 0xFFFFFFFF;
1283 SkDebugf("pixmap.getColor(0, 0) %c= 0x00000000\n",
1284 pixmap.getColor(0, 0) == 0x00000000 ? '=' : '!');
1285 SkDebugf("pixmap.getColor(1, 2) %c= 0xFFFFFFFF\n",
Cary Clarkd0530ba2017-09-14 11:25:39 -04001286 pixmap.getColor(1, 2) == 0xFFFFFFFF ? '=' : '!');
1287#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -04001288pixmap.writable_addr() == (void *)storage
1289pixmap.getColor(0, 0) == 0x00000000
Cary Clarkd0530ba2017-09-14 11:25:39 -04001290pixmap.getColor(1, 2) == 0xFFFFFFFF
1291##
1292##
1293
1294#SeeAlso writable_addr8 writable_addr16 writable_addr32 writable_addr64 writable_addrF16 addr()
1295
1296##
1297
1298# ------------------------------------------------------------------------------
1299
1300#Method uint8_t* writable_addr8(int x, int y) const
1301
Cary Clark6fc50412017-09-21 12:31:06 -04001302Returns writable pixel address at (x, y). Result is addressable as unsigned
13038-bit bytes. Will trigger an assert() if Color_Type is not kAlpha_8_SkColorType
1304or kGray_8_SkColorType, and is built with SK_DEBUG defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001305
1306One byte corresponds to one pixel.
1307
Cary Clark6fc50412017-09-21 12:31:06 -04001308#Param x column index, zero or greater, and less than width() ##
1309#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001310
1311#Return writable unsigned 8-bit pointer to pixels ##
1312
1313#Example
1314#Height 64
1315#Description
1316Altering pixels after drawing Bitmap is not guaranteed to affect subsequent
1317drawing on all platforms. Adding a second SkBitmap::installPixels after editing
1318pixel memory is safer.
1319##
Cary Clark6fc50412017-09-21 12:31:06 -04001320void draw(SkCanvas* canvas) {
1321 uint8_t storage[][5] = {{ 0, 0, 64, 0, 0},
1322 { 0, 128, 255, 128, 0},
1323 {64, 255, 255, 255, 64},
1324 { 0, 128, 255, 128, 0},
1325 { 0, 0, 64, 0, 0}};
1326 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kPremul_SkAlphaType);
1327 SkPixmap pixmap(imageInfo, storage[0], 5);
1328 SkBitmap bitmap;
1329 bitmap.installPixels(pixmap);
1330 canvas->scale(10, 10);
1331 canvas->drawBitmap(bitmap, 0, 0);
1332 *pixmap.writable_addr8(2, 2) = 0;
1333// bitmap.installPixels(pixmap); // uncomment to fix on GPU
1334 canvas->drawBitmap(bitmap, 10, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001335}
1336##
1337
1338#SeeAlso writable_addr writable_addr16 writable_addr32 writable_addr64 writable_addrF16 addr() addr8
1339
1340##
1341
1342# ------------------------------------------------------------------------------
1343
1344#Method uint16_t* writable_addr16(int x, int y) const
1345
Cary Clark6fc50412017-09-21 12:31:06 -04001346Returns writable_addr pixel address at (x, y). Result is addressable as unsigned
134716-bit words. Will trigger an assert() if Color_Type is not kRGB_565_SkColorType
1348or kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001349
1350One word corresponds to one pixel.
1351
Cary Clark6fc50412017-09-21 12:31:06 -04001352#Param x column index, zero or greater, and less than width() ##
1353#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001354
1355#Return writable unsigned 16-bit pointer to pixel ##
1356
1357#Example
1358#Description
1359Draw a five by five bitmap, and draw it again with a center black pixel.
1360The low nibble of the 16-bit word is Alpha.
1361##
1362#Height 64
Cary Clark6fc50412017-09-21 12:31:06 -04001363 uint16_t storage[][5] = {{ 0xCABF, 0xDABE, 0xCA9D, 0xC96C, 0xA39B },
1364 { 0xACEE, 0xA87C, 0x893A, 0x4779, 0x8708 },
1365 { 0x4B7C, 0x255B, 0x2559, 0x2557, 0x4656 },
1366 { 0x9099, 0x8128, 0x2557, 0x4124, 0x3323 },
1367 { 0x7547, 0x5505, 0x4434, 0x2012, 0x0000 }};
1368 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kARGB_4444_SkColorType, kPremul_SkAlphaType);
1369 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
1370 SkBitmap bitmap;
1371 bitmap.installPixels(pixmap);
1372 canvas->scale(10, 10);
1373 canvas->drawBitmap(bitmap, 0, 0);
1374 *pixmap.writable_addr16(2, 2) = 0x000F;
1375 bitmap.installPixels(pixmap);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001376 canvas->drawBitmap(bitmap, 10, 0);
1377##
1378
1379#SeeAlso writable_addr writable_addr8 writable_addr32 writable_addr64 writable_addrF16 addr() addr16
1380
1381##
1382
1383# ------------------------------------------------------------------------------
1384
1385#Method uint32_t* writable_addr32(int x, int y) const
1386
Cary Clark6fc50412017-09-21 12:31:06 -04001387Returns writable pixel address at (x, y). Result is addressable as unsigned
138832-bit words. Will trigger an assert() if Color_Type is not
1389kRGBA_8888_SkColorType or kBGRA_8888_SkColorType, and is built with SK_DEBUG
1390defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001391
1392One word corresponds to one pixel.
1393
Cary Clark6fc50412017-09-21 12:31:06 -04001394#Param x column index, zero or greater, and less than width() ##
1395#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001396
1397#Return writable unsigned 32-bit pointer to pixel ##
1398
1399#Example
1400#Image 4
1401#Height 72
Cary Clark6fc50412017-09-21 12:31:06 -04001402 std::vector<int32_t> pixels;
1403 pixels.resize(image->height() * image->width() * 4);
1404 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
1405 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
1406 image->readPixels(pixmap, 0, 0);
1407 for (int y = 0; y < pixmap.height() / 2; ++y) {
1408 for (int x = 0; x < pixmap.width(); ++x) {
1409 if ((x & 4) == (y & 4)) {
1410 SkTSwap(*pixmap.writable_addr32(x, y),
1411 *pixmap.writable_addr32(pixmap.width() - x, pixmap.height() - y));
1412 }
1413 }
1414 }
1415 SkBitmap bitmap;
1416 bitmap.installPixels(pixmap);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001417 canvas->drawBitmap(bitmap, 0, 0);
1418##
1419
1420#SeeAlso writable_addr writable_addr8 writable_addr16 writable_addr64 writable_addrF16 addr() addr32
1421
1422##
1423
1424# ------------------------------------------------------------------------------
1425
1426#Method uint64_t* writable_addr64(int x, int y) const
1427
Cary Clark6fc50412017-09-21 12:31:06 -04001428Returns writable pixel address at (x, y). Result is addressable as unsigned
142964-bit words. Will trigger an assert() if Color_Type is not
1430kRGBA_F16_SkColorType and is built with SK_DEBUG defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001431
1432One word corresponds to one pixel.
1433
Cary Clark6fc50412017-09-21 12:31:06 -04001434#Param x column index, zero or greater, and less than width() ##
1435#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001436
1437#Return writable unsigned 64-bit pointer to pixel ##
1438
1439#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001440 SkImageInfo info = SkImageInfo::Make(3, 3, kRGBA_F16_SkColorType, kPremul_SkAlphaType);
1441 uint64_t storage[9];
1442 SkPixmap pixmap(info, storage, 3 * sizeof(uint64_t));
1443 SkColor4f c4 { 1, 0.45f, 0.25f, 0.65f };
1444 pixmap.erase(c4);
1445 SkBitmap bitmap;
1446 canvas->scale(10, 10);
1447 bitmap.installPixels(pixmap);
1448 canvas->drawBitmap(bitmap, 0, 0);
1449 *pixmap.writable_addr64(1, 1) |= 0x00ff000000000000LL;
1450 bitmap.installPixels(pixmap);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001451 canvas->drawBitmap(bitmap, 10, 0);
1452##
1453
1454#SeeAlso writable_addr writable_addr8 writable_addr16 writable_addr32 writable_addrF16 addr() addr64
1455
1456##
1457
1458# ------------------------------------------------------------------------------
1459
1460#Method uint16_t* writable_addrF16(int x, int y) const
1461
Cary Clark6fc50412017-09-21 12:31:06 -04001462Returns writable pixel address at (x, y). Result is addressable as unsigned
146316-bit words. Will trigger an assert() if Color_Type is not
1464kRGBA_F16_SkColorType and is built with SK_DEBUG defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001465
1466Each word represents one color component encoded as a half float.
1467Four words correspond to one pixel.
1468
Cary Clark6fc50412017-09-21 12:31:06 -04001469#Param x column index, zero or greater, and less than width() ##
1470#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001471
1472#Return writable unsigned 16-bit pointer to first component of pixel ##
1473
1474#Example
1475#Height 64
1476#Description
1477Left bitmap is drawn with two pixels defined in half float format. Right bitmap
1478is drawn after overwriting bottom half float color with top half float color.
1479##
Cary Clark6fc50412017-09-21 12:31:06 -04001480 SkImageInfo info = SkImageInfo::Make(1, 2, kRGBA_F16_SkColorType, kPremul_SkAlphaType);
1481 uint16_t storage[2][4];
1482 SkPixmap pixmap(info, storage[0], sizeof(uint64_t));
1483 SkIRect topPixelBounds = {0, 0, 1, 1};
1484 pixmap.erase({ 0.65f, 0.45f, 0.25f, 1 }, &topPixelBounds);
1485 SkIRect bottomPixelBounds = {0, 1, 1, 2};
1486 pixmap.erase({ 0.25f, 0.65f, 0.45f, 1 }, &bottomPixelBounds);
1487 SkBitmap bitmap;
1488 canvas->scale(20, 20);
1489 bitmap.installPixels(pixmap);
1490 canvas->drawBitmap(bitmap, 0, 0);
1491 uint16_t* pixel2 = pixmap.writable_addrF16(0, 1);
1492 for (int i = 0; i < 4; ++i) {
1493 pixel2[i] = storage[0][i];
1494 }
1495 bitmap.installPixels(pixmap);
1496 canvas->drawBitmap(bitmap, 4, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001497##
1498
1499#SeeAlso writable_addr writable_addr8 writable_addr16 writable_addr32 writable_addr64 addr() addrF16
1500
1501##
1502
1503#Subtopic Writable_Address ##
1504
1505#Subtopic Writer
1506
1507# ------------------------------------------------------------------------------
1508
1509#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1510 int srcX, int srcY, SkTransferFunctionBehavior behavior) const
1511
Cary Clark154beea2017-10-26 07:58:48 -04001512Copies a Rect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not
1513exceed (this->width(), this->height()).
Cary Clark6fc50412017-09-21 12:31:06 -04001514
1515dstInfo specifies width, height, Color_Type, Alpha_Type, and
Cary Clarkd0530ba2017-09-14 11:25:39 -04001516Color_Space of destination. dstRowBytes specifics the gap from one destination
1517row to the next. Returns true if pixels are copied. Returns false if
1518dstInfo.addr() equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes.
1519
1520Pixels are copied only if pixel conversion is possible. If this->colorType is
1521kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1522If this->colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
1523If this->alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
1524match. If this->colorSpace is nullptr, dstInfo.colorSpace must match. Returns
1525false if pixel conversion is not possible.
1526
1527srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04001528false if width() or height() is zero or negative. Returns false if:
1529
Cary Clarkd0530ba2017-09-14 11:25:39 -04001530#Formula
Cary Clark6fc50412017-09-21 12:31:06 -04001531abs(srcX) >= this->width()
1532##
1533, or if
1534#Formula
1535abs(srcY) >= this->height()
Cary Clarkd0530ba2017-09-14 11:25:39 -04001536##
1537.
1538
1539If behavior is SkTransferFunctionBehavior::kRespect: converts source
1540pixels to a linear space before converting to dstInfo.
1541If behavior is SkTransferFunctionBehavior::kIgnore: source
Cary Clarkbc5697d2017-10-04 14:31:33 -04001542pixels are treated as if they are linear, regardless of how they are encoded.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001543
1544#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1545#Param dstPixels destination pixel storage ##
1546#Param dstRowBytes destination row length ##
1547#Param srcX column index whose absolute value is less than width() ##
1548#Param srcY row index whose absolute value is less than height() ##
1549#Param behavior one of: SkTransferFunctionBehavior::kRespect,
1550 SkTransferFunctionBehavior::kIgnore
1551##
1552
1553#Return true if pixels are copied to dstPixels ##
1554
1555#Example
Cary Clarkbc5697d2017-10-04 14:31:33 -04001556#ToDo example doesn't do anything interesting since info colorSpace is nullptr ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001557#Image 3
1558void draw(SkCanvas* canvas) {
1559 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height(),
1560 canvas->imageInfo().colorSpace() ? SkColorSpace::MakeSRGB() : nullptr);
Cary Clark6fc50412017-09-21 12:31:06 -04001561 std::vector<int32_t> srcPixels;
1562 srcPixels.resize(image->height() * image->width() * 4);
1563 SkPixmap pixmap(info, (const void*) &srcPixels.front(), image->width() * 4);
1564 image->readPixels(pixmap, 0, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001565 SkTransferFunctionBehavior behavior = canvas->imageInfo().colorSpace() ?
1566 SkTransferFunctionBehavior::kRespect : SkTransferFunctionBehavior::kIgnore;
Cary Clark6fc50412017-09-21 12:31:06 -04001567 std::vector<int32_t> dstPixels;
1568 dstPixels.resize(image->height() * image->width() * 4);
1569 int offset = 0;
1570 for (auto behavior : { SkTransferFunctionBehavior::kRespect,
1571 SkTransferFunctionBehavior::kIgnore} ) {
Cary Clarkd0530ba2017-09-14 11:25:39 -04001572 pixmap.readPixels(info, &dstPixels.front(), image->width() * 4, offset, 0, behavior);
1573 offset += 128;
1574 }
1575 SkBitmap bitmap;
1576 SkPixmap dstmap(info, &dstPixels.front(), image->width() * 4);
1577 bitmap.installPixels(dstmap);
1578 canvas->drawBitmap(bitmap, 0, 0);
1579}
1580##
1581
1582#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1583
1584##
1585
1586# ------------------------------------------------------------------------------
1587
1588#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes) const
1589
1590Copies a Rect of pixels to dstPixels. Copy starts at (0, 0), and does not
Cary Clark154beea2017-10-26 07:58:48 -04001591exceed (this->width(), this->height()).
Cary Clark6fc50412017-09-21 12:31:06 -04001592
1593dstInfo specifies width, height, Color_Type, Alpha_Type, and
Cary Clarkd0530ba2017-09-14 11:25:39 -04001594Color_Space of destination. dstRowBytes specifics the gap from one destination
1595row to the next. Returns true if pixels are copied. Returns false if
1596dstInfo.addr() equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes.
1597
1598Pixels are copied only if pixel conversion is possible. If this->colorType is
1599kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1600If this->colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
1601If this->alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
1602match. If this->colorSpace is nullptr, dstInfo.colorSpace must match. Returns
1603false if pixel conversion is not possible.
1604
1605Returns false if this->width() or this->height() is zero or negative.
1606
1607#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1608#Param dstPixels destination pixel storage ##
1609#Param dstRowBytes destination row length ##
1610
1611#Return true if pixels are copied to dstPixels ##
1612
1613#Example
1614#Height 128
1615#Description
1616Transferring the gradient from 8 bits per component to 4 bits per component
1617creates visible banding.
1618##
Cary Clark6fc50412017-09-21 12:31:06 -04001619 std::vector<int32_t> pixels;
1620 const int width = 256;
1621 const int height = 64;
1622 pixels.resize(height * width * 4);
1623 SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(width, height);
1624 SkPixmap srcPixmap(srcInfo, (const void*) &pixels.front(), width * 4);
1625 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
1626 SkPoint gradPoints[] = { { 0, 0 }, { 256, 0 } };
1627 SkPaint paint;
1628 paint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
1629 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
1630 SkBitmap bitmap;
1631 bitmap.installPixels(srcPixmap);
1632 SkCanvas srcCanvas(bitmap);
1633 srcCanvas.drawRect(SkRect::MakeWH(width, height), paint);
1634 canvas->drawBitmap(bitmap, 0, 0);
1635 std::vector<int32_t> dstPixels;
1636 dstPixels.resize(height * width * 2);
1637 SkImageInfo dstInfo = srcInfo.makeColorType(kARGB_4444_SkColorType);
1638 srcPixmap.readPixels(dstInfo, &dstPixels.front(), width * 2);
1639 SkPixmap dstPixmap(dstInfo, &dstPixels.front(), width * 2);
1640 bitmap.installPixels(dstPixmap);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001641 canvas->drawBitmap(bitmap, 0, 128);
1642##
1643
1644#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1645
1646##
1647
1648# ------------------------------------------------------------------------------
1649
1650#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, int srcX,
1651 int srcY) const
1652
1653Copies a Rect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not
Cary Clark154beea2017-10-26 07:58:48 -04001654exceed (this->width(), this->height()).
Cary Clark6fc50412017-09-21 12:31:06 -04001655
1656dstInfo specifies width, height, Color_Type, Alpha_Type, and
Cary Clarkd0530ba2017-09-14 11:25:39 -04001657Color_Space of destination. dstRowBytes specifics the gap from one destination
1658row to the next. Returns true if pixels are copied. Returns false if
1659dstInfo.addr() equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes.
1660
1661Pixels are copied only if pixel conversion is possible. If this->colorType is
1662kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1663If this->colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
1664If this->alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
1665match. If this->colorSpace is nullptr, dstInfo.colorSpace must match. Returns
1666false if pixel conversion is not possible.
1667
1668srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04001669false if this->width() or this->height() is zero or negative. Returns false if:
1670
Cary Clarkd0530ba2017-09-14 11:25:39 -04001671#Formula
Cary Clark6fc50412017-09-21 12:31:06 -04001672abs(srcX) >= this->width()
1673##
1674, or if
1675#Formula
1676abs(srcY) >= this->height()
Cary Clarkd0530ba2017-09-14 11:25:39 -04001677##
1678.
1679
1680#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1681#Param dstPixels destination pixel storage ##
1682#Param dstRowBytes destination row length ##
1683#Param srcX column index whose absolute value is less than width() ##
1684#Param srcY row index whose absolute value is less than height() ##
1685
1686#Return true if pixels are copied to dstPixels ##
1687
1688#Example
1689#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -04001690void draw(SkCanvas* canvas) {
1691 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
1692 std::vector<int32_t> srcPixels;
1693 const int rowBytes = image->width() * 4;
1694 srcPixels.resize(image->height() * rowBytes);
1695 SkPixmap pixmap(info, (const void*) &srcPixels.front(), rowBytes);
1696 image->readPixels(pixmap, 0, 0);
1697 for (int offset : { 32, 64, 96 } ) {
1698 std::vector<int32_t> dstPixels;
1699 dstPixels.resize(image->height() * rowBytes);
1700 pixmap.readPixels(info, &dstPixels.front(), rowBytes, offset, 0);
1701 SkBitmap bitmap;
1702 SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
1703 bitmap.installPixels(dstmap);
1704 canvas->translate(32, 32);
1705 canvas->drawBitmap(bitmap, 0, 0);
1706 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001707}
1708##
1709
1710#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1711
1712##
1713
1714# ------------------------------------------------------------------------------
1715
1716#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY) const
1717
1718Copies a Rect of pixels to dst. Copy starts at (srcX, srcY), and does not
1719exceed (this->width(), this->height()). dst specifies width, height, Color_Type,
1720Alpha_Type, and Color_Space of destination. Returns true if pixels are copied.
1721Returns false if dst.addr() equals nullptr, or dst.rowBytes is less than
1722dst SkImageInfo::minRowBytes.
1723
1724Pixels are copied only if pixel conversion is possible. If this->colorType is
1725kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.info().colorType must match.
1726If this->colorType is kGray_8_SkColorType, dst.info().colorSpace must match.
1727If this->alphaType is kOpaque_SkAlphaType, dst.info().alphaType must
1728match. If this->colorSpace is nullptr, dst.info().colorSpace must match. Returns
1729false if pixel conversion is not possible.
1730
1731srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04001732false this->width() or this->height() is zero or negative. Returns false if:
1733
Cary Clarkd0530ba2017-09-14 11:25:39 -04001734#Formula
Cary Clark6fc50412017-09-21 12:31:06 -04001735abs(srcX) >= this->width()
1736##
1737, or if
1738#Formula
1739abs(srcY) >= this->height()
Cary Clarkd0530ba2017-09-14 11:25:39 -04001740##
1741.
1742
1743#Param dst Image_Info and pixel address to write to ##
1744#Param srcX column index whose absolute value is less than width() ##
1745#Param srcY row index whose absolute value is less than height() ##
1746
1747#Return true if pixels are copied to dst ##
1748
1749#Example
1750#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -04001751void draw(SkCanvas* canvas) {
1752 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
1753 std::vector<int32_t> srcPixels;
1754 const int rowBytes = image->width() * 4;
1755 srcPixels.resize(image->height() * rowBytes);
1756 SkPixmap pixmap(info, (const void*) &srcPixels.front(), rowBytes);
1757 image->readPixels(pixmap, 0, 0);
1758 for (int offset : { 32, 64, 96 } ) {
1759 std::vector<int32_t> dstPixels;
1760 dstPixels.resize(image->height() * rowBytes);
1761 SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
1762 pixmap.readPixels(dstmap, offset, 0);
1763 SkBitmap bitmap;
1764 bitmap.installPixels(dstmap);
1765 canvas->translate(32, 32);
1766 canvas->drawBitmap(bitmap, 0, 0);
1767 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001768}
1769##
1770
1771#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1772
1773##
1774
1775# ------------------------------------------------------------------------------
1776
1777#Method bool readPixels(const SkPixmap& dst) const
1778
1779Copies pixels inside bounds() to dst. dst specifies width, height, Color_Type,
1780Alpha_Type, and Color_Space of destination. Returns true if pixels are copied.
1781Returns false if dst.addr() equals nullptr, or dst.rowBytes is less than
1782dst SkImageInfo::minRowBytes.
1783
1784Pixels are copied only if pixel conversion is possible. If this->colorType is
1785kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
1786If this->colorType is kGray_8_SkColorType, dst Color_Space must match.
1787If this->alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
1788match. If this->colorSpace is nullptr, dst Color_Space must match. Returns
1789false if pixel conversion is not possible.
1790
1791Returns false if this->width() or this->height() is zero or negative.
1792
1793#Param dst Image_Info and pixel address to write to ##
1794
1795#Return true if pixels are copied to dst ##
1796
1797#Example
1798#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -04001799void draw(SkCanvas* canvas) {
1800 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
1801 std::vector<int32_t> srcPixels;
1802 const int rowBytes = image->width() * 4;
1803 srcPixels.resize(image->height() * rowBytes);
1804 SkPixmap pixmap(info, (const void*) &srcPixels.front(), rowBytes);
1805 image->readPixels(pixmap, 0, 0);
1806 for (int index = 0; index < 3; ++index ) {
1807 std::vector<int32_t> dstPixels;
1808 dstPixels.resize(image->height() * rowBytes);
1809 SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
1810 pixmap.readPixels(dstmap);
1811 SkBitmap bitmap;
1812 bitmap.installPixels(dstmap);
1813 canvas->translate(32, 32);
1814 canvas->drawBitmap(bitmap, 0, 0);
1815 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001816}
1817##
1818
1819#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1820
1821##
1822
1823# ------------------------------------------------------------------------------
1824
1825#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality) const
1826
1827Copies this to dst, scaling pixels to fit dst.width() and dst.height(), and
1828converting pixels to match dst.colorType and dst.alphaType. Returns true if
1829pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1830less than dst SkImageInfo::minRowBytes.
1831
1832Pixels are copied only if pixel conversion is possible. If this->colorType is
1833kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
1834If this->colorType is kGray_8_SkColorType, dst Color_Space must match.
1835If this->alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
1836match. If this->colorSpace is nullptr, dst Color_Space must match. Returns
1837false if pixel conversion is not possible.
1838
1839Returns false if this->width() or this->height() is zero or negative.
1840
1841Scales the image, with filterQuality, to match dst.width() and dst.height().
1842filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1843Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1844Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1845Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1846kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1847
1848#Param dst Image_Info and pixel address to write to ##
1849#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1850 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1851##
1852
1853#Return true if pixels are copied to dst ##
1854
1855#Example
1856#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -04001857void draw(SkCanvas* canvas) {
1858 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
1859 std::vector<int32_t> srcPixels;
1860 int rowBytes = image->width() * 4;
1861 srcPixels.resize(image->height() * rowBytes);
1862 SkPixmap pixmap(info, (const void*) &srcPixels.front(), rowBytes);
1863 image->readPixels(pixmap, 0, 0);
1864 for (int offset : { 32, 64, 96 } ) {
1865 info = SkImageInfo::MakeN32Premul(image->width() + offset, image->height());
1866 rowBytes = info.width() * 4;
1867 std::vector<int32_t> dstPixels;
1868 dstPixels.resize(image->height() * rowBytes);
1869 SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
1870 pixmap.scalePixels(dstmap, kMedium_SkFilterQuality);
1871 SkBitmap bitmap;
1872 bitmap.installPixels(dstmap);
1873 canvas->translate(32, 32);
1874 canvas->drawBitmap(bitmap, 0, 0);
1875 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001876}
1877##
1878
1879#SeeAlso SkCanvas::drawBitmap SkImage::scalePixels
1880
1881##
1882
1883# ------------------------------------------------------------------------------
1884
1885#Method bool erase(SkColor color, const SkIRect& subset) const
1886
1887Writes color to pixels bounded by subset; returns true on success.
1888Returns false if colorType is kUnknown_SkColorType, or if subset does
1889not intersect bounds().
1890
1891#Param color Unpremultiplied Color to write ##
1892#Param subset bounding integer Rect of written pixels ##
1893
1894#Return true if pixels are changed ##
1895
1896#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001897 uint32_t storage[2];
1898 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 2);
1899 SkPixmap pixmap(info, storage, info.minRowBytes());
1900 pixmap.erase(SK_ColorBLUE, {0, 0, 1, 1});
1901 pixmap.erase(SK_ColorRED, {0, 1, 1, 2});
1902 SkBitmap bitmap;
1903 canvas->scale(20, 20);
1904 bitmap.installPixels(pixmap);
1905 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001906##
1907
1908#SeeAlso SkBitmap::erase SkCanvas::clear SkCanvas::drawColor
1909
1910##
1911
1912# ------------------------------------------------------------------------------
1913
1914#Method bool erase(SkColor color) const
1915
1916Writes color to pixels inside bounds(); returns true on success.
1917Returns false if colorType is kUnknown_SkColorType, or if bounds()
1918is empty.
1919
1920#Param color Unpremultiplied Color to write ##
1921
1922#Return true if pixels are changed ##
1923
1924#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001925 uint32_t storage[2];
1926 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 2);
1927 SkPixmap pixmap(info, storage, info.minRowBytes());
1928 pixmap.erase(SK_ColorBLUE);
1929 SkBitmap bitmap;
1930 canvas->scale(20, 20);
1931 bitmap.installPixels(pixmap);
1932 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001933##
1934
1935#SeeAlso SkBitmap::erase SkCanvas::clear SkCanvas::drawColor
1936
1937##
1938
1939# ------------------------------------------------------------------------------
1940
1941#Method bool erase(const SkColor4f& color, const SkIRect* subset = nullptr) const
1942
1943Writes color to pixels bounded by subset; returns true on success.
1944if subset is nullptr, writes colors pixels inside bounds(). Returns false if
1945colorType is kUnknown_SkColorType, if subset is not nullptr and does
1946not intersect bounds(), or if subset is nullptr and bounds() is empty.
1947
1948#Param color Unpremultiplied Color to write ##
1949#Param subset bounding integer Rect of pixels to write; may be nullptr ##
1950
1951#Return true if pixels are changed ##
1952
1953#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001954 uint32_t storage[2];
1955 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 2);
1956 SkPixmap pixmap(info, storage, info.minRowBytes());
1957 SkIRect topPixelBounds = {0, 0, 1, 1};
1958 pixmap.erase({ 0.65f, 0.45f, 0.25f, 1 }, &topPixelBounds);
1959 SkIRect bottomPixelBounds = {0, 1, 1, 2};
1960 pixmap.erase({ 0.25f, 0.65f, 0.45f, 1 }, &bottomPixelBounds);
1961 SkBitmap bitmap;
1962 canvas->scale(20, 20);
1963 bitmap.installPixels(pixmap);
1964 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001965##
1966
1967#SeeAlso SkBitmap::erase SkCanvas::clear SkCanvas::drawColor
1968
1969##
1970
1971
1972#Subtopic Writer ##
1973
1974#Class SkPixmap ##
1975
1976#Topic Pixmap ##