blob: 521d6dea15124cc173c708efbd77e4a44418872a [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
14Pixmap does not try to manage the lifetime of the pixel memory. Use PixelRef
15to manage pixel memory; PixelRef is safe across threads.
16
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. ##
58# computeIsOpaque # Returns true if all pixels are opaque. ##
59# erase() # Writes Color to pixels. ##
60# extractSubset # Sets pointer to portion of original. ##
61# getColor # Returns one pixel as Unpremultiplied Color. ##
62# getSafeSize # Returns minimum size required for pixels in 32 bits. ##
63# getSafeSize64 # Returns minimum size required for pixels in 64 bits. ##
64# getSize64 # Returns conservative size required for pixels. ##
65# height() # Returns pixel row count. ##
66# info() # Returns Image_Info. ##
67# isOpaque # Returns true if Image_Info describes opaque pixels. ##
68# readPixels # Copies and converts pixels. ##
69# reset() # Reuses existing Pixmap with replacement values. ##
70# rowBytes # Returns interval between rows in bytes. ##
71# rowBytesAsPixels # Returns interval between rows in pixels. ##
72# scalePixels # Scales and converts pixels. ##
73# setColorSpace # Sets Image_Info Color_Space. ##
74# shiftPerPixel # Returns bit shift from pixels to bytes. ##
75# width() # Returns pixel column count. ##
76# writable_addr # Returns writable pixel address as void pointer. ##
77# writable_addr16 # Returns writable pixel address as 16-bit pointer. ##
78# writable_addr32 # Returns writable pixel address as 32-bit pointer. ##
79# writable_addr64 # Returns writable pixel address as 64-bit pointer. ##
80# writable_addr8 # Returns writable pixel address as 8-bit pointer. ##
81# writable_addrF16 # Returns writable pixel component address as 16-bit pointer. ##
82#Table ##
83#Subtopic ##
84
85#Topic Overview ##
86
87#Subtopic Initialization
88
89# ------------------------------------------------------------------------------
90
91#Method SkPixmap()
92
93Creates an empty Pixmap without pixels, with kUnknown_SkColorType, with
94kUnknown_SkAlphaType, and with a width and height of zero. Use
95reset() to associate pixels, SkColorType, SkAlphaType, width, and height
96after Pixmap has been created.
97
98#Return empty Pixmap ##
99
100#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400101void draw(SkCanvas* canvas) {
102 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
103 const char* colors[] = {"Unknown", "Alpha", "RGB_565", "ARGB_4444", "RGBA_8888", "BGRA_8888",
104 "Gray_8", "RGBA_F16"};
105 SkPixmap pixmap;
106 for (int i = 0; i < 2; ++i) {
107 SkDebugf("width: %2d height: %2d", pixmap.width(), pixmap.height());
108 SkDebugf(" color: k%s_SkColorType", colors[pixmap.colorType()]);
109 SkDebugf(" alpha: k%s_SkAlphaType\n", alphas[pixmap.alphaType()]);
110 pixmap.reset(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType),
111 nullptr, 0);
112 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400113}
114#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400115width: 0 height: 0 color: kUnknown_SkColorType alpha: kUnknown_SkAlphaType
Cary Clarkd0530ba2017-09-14 11:25:39 -0400116width: 25 height: 35 color: kRGBA_8888_SkColorType alpha: kOpaque_SkAlphaType
117##
118##
119
120#SeeAlso SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes) reset() SkAlphaType SkColorType
121
122##
123
124# ------------------------------------------------------------------------------
125
126#Method SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes)
127
128Creates Pixmap from info width, height, SkAlphaType, and SkColorType.
Cary Clark6fc50412017-09-21 12:31:06 -0400129addr points to pixels, or nullptr. rowBytes should be info.width() times
130info.bytesPerPixel(), or larger.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400131
132No parameter checking is performed; it is up to the caller to ensure that
133addr and rowBytes agree with info.
134
Cary Clark6fc50412017-09-21 12:31:06 -0400135The memory lifetime of pixels is managed by the caller. When Pixmap goes
Cary Clarkd0530ba2017-09-14 11:25:39 -0400136out of scope, addr is unaffected.
137
138Pixmap may be later modified by reset() to change its size, pixel type, or
139storage.
140
141#Param info width, height, SkAlphaType, SkColorType of Image_Info ##
142#Param addr pointer to pixels allocated by caller; may be nullptr ##
143#Param rowBytes size of one row of addr; width times pixel size, or larger ##
144
145#Return initialized Pixmap ##
146
147#Example
148#Image 3
149#Description
150SkImage::MakeRasterCopy takes const SkPixmap& as an argument. The example
151constructs a SkPixmap from the brace-delimited parameters.
152##
Cary Clark6fc50412017-09-21 12:31:06 -0400153 SkDebugf("image alpha only = %s\n", image->isAlphaOnly() ? "true" : "false");
154 SkPMColor pmColors = 0;
155 sk_sp<SkImage> copy = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1),
156 (uint8_t*)&pmColors,
157 1});
Cary Clarkd0530ba2017-09-14 11:25:39 -0400158 SkDebugf("copy alpha only = %s\n", copy->isAlphaOnly() ? "true" : "false");
159#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400160image alpha only = false
Cary Clarkd0530ba2017-09-14 11:25:39 -0400161copy alpha only = true
162##
163##
164
165#SeeAlso SkPixmap() reset() SkAlphaType SkColorType
166
167##
168
169# ------------------------------------------------------------------------------
170
171#Method void reset()
172
173Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to
174kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType.
175
176The prior pixels are unaffected; it is up to the caller to release pixels
177memory if desired.
178
179#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400180void draw(SkCanvas* canvas) {
181 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
182 const char* colors[] = {"Unknown", "Alpha", "RGB_565", "ARGB_4444", "RGBA_8888", "BGRA_8888",
183 "Gray_8", "RGBA_F16"};
184 SkPixmap pixmap(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType),
185 nullptr, 0);
186 for (int i = 0; i < 2; ++i) {
187 SkDebugf("width: %2d height: %2d", pixmap.width(), pixmap.height());
188 SkDebugf(" color: k%s_SkColorType", colors[pixmap.colorType()]);
189 SkDebugf(" alpha: k%s_SkAlphaType\n", alphas[pixmap.alphaType()]);
190 pixmap.reset();
191 }
192}
Cary Clarkd0530ba2017-09-14 11:25:39 -0400193#StdOut
194width: 25 height: 35 color: kRGBA_8888_SkColorType alpha: kOpaque_SkAlphaType
Cary Clark6fc50412017-09-21 12:31:06 -0400195width: 0 height: 0 color: kUnknown_SkColorType alpha: kUnknown_SkAlphaType
Cary Clarkd0530ba2017-09-14 11:25:39 -0400196##
197##
198
199#SeeAlso SkPixmap() SkAlphaType SkColorType
200
201##
202
203# ------------------------------------------------------------------------------
204
205#Method void reset(const SkImageInfo& info, const void* addr, size_t rowBytes)
206
207Sets width, height, SkAlphaType, and SkColorType from info.
208Sets pixel address from addr, which may be nullptr.
Cary Clark6fc50412017-09-21 12:31:06 -0400209Sets row bytes from rowBytes, which should be info.width() times
210info.bytesPerPixel(), or larger.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400211
212Does not check addr. Asserts if built with SK_DEBUG defined and if rowBytes is
213too small to hold one row of pixels.
214
215The memory lifetime pixels are managed by the caller. When Pixmap goes
216out of scope, addr is unaffected.
217
218#Param info width, height, SkAlphaType, SkColorType of Image_Info ##
219#Param addr pointer to pixels allocated by caller; may be nullptr ##
220#Param rowBytes size of one row of addr; width times pixel size, or larger ##
221
222#Example
223#Image 4
224#Height 128
Cary Clark6fc50412017-09-21 12:31:06 -0400225void draw(SkCanvas* canvas) {
226 std::vector<int32_t> pixels;
227 pixels.resize(image->height() * image->width() * 4);
228 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
229 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
230 image->readPixels(pixmap, 0, 0);
231 int x = 0;
232 for (auto colorType : { kRGBA_8888_SkColorType, kBGRA_8888_SkColorType } ) {
233 pixmap.reset(SkImageInfo::Make(image->width(), image->height(), colorType,
234 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
235 SkBitmap bitmap;
236 bitmap.installPixels(pixmap);
237 canvas->drawBitmap(bitmap, x, 0);
238 x += 128;
239 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400240}
241##
242
243#SeeAlso SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes) reset() SkAlphaType SkColorType
244
245##
246
247# ------------------------------------------------------------------------------
248
249#Method void setColorSpace(sk_sp<SkColorSpace> colorSpace)
250
251
252Changes Color_Space in Image_Info; preserves width, height, SkAlphaType, and
253SkColorType in Image, and leaves pixel address and row bytes unchanged.
Cary Clark6fc50412017-09-21 12:31:06 -0400254Color_Space reference count is incremented.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400255
256#Param colorSpace Color_Space moved to Image_Info ##
257
258#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400259void draw(SkCanvas* canvas) {
260 SkPixmap pixmap;
261 sk_sp<SkColorSpace> colorSpace1 = SkColorSpace::MakeRGB(SkColorSpace::kLinear_RenderTargetGamma,
262 SkColorSpace::kRec2020_Gamut);
263 SkDebugf("is %sunique\n", colorSpace1->unique() ? "" : "not ");
264 pixmap.setColorSpace(colorSpace1);
265 SkDebugf("is %sunique\n", colorSpace1->unique() ? "" : "not ");
Cary Clarkd0530ba2017-09-14 11:25:39 -0400266}
267#StdOut
268is unique
269is not unique
270##
271##
272
273#SeeAlso Color_Space SkImageInfo::makeColorSpace
274
275##
276
277# ------------------------------------------------------------------------------
278
279#Method bool SK_WARN_UNUSED_RESULT reset(const SkMask& mask)
280
281Sets width, height, pixel address, and row bytes to Mask properties, if Mask
282format is SkMask::kA8_Format; and returns true. Otherwise sets width, height,
283row bytes to zero; pixel address to nullptr; SkColorType to kUnknown_SkColorType;
284and SkAlphaType to kUnknown_SkAlphaType; and returns false.
285
286Failing to read the return value generates a compile time warning.
287
288#Param mask Mask containing pixels and dimensions ##
289
290#Return true if set to Mask properties ##
291
292#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400293 const int width = 2;
294 const int height = 2;
295 uint8_t bytes[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
296 SkMask mask;
297 mask.fFormat = SkMask::kA8_Format;
298 mask.fBounds = {0, 0, width, height};
299 mask.fImage = bytes;
300 mask.fRowBytes = (width + 7) >> 3;
301 SkPixmap pixmap;
302 bool success = pixmap.reset(mask);
303 SkDebugf("success: %s width: %d height: %d\n", success ? "true " : "false",
304 pixmap.width(), pixmap.height());
305 mask.fFormat = SkMask::kBW_Format;
306 success = pixmap.reset(mask);
307 SkDebugf("success: %s width: %d height: %d\n", success ? "true " : "false",
308 pixmap.width(), pixmap.height());
309#StdOut
310success: true width: 2 height: 2
311success: false width: 0 height: 0
312##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400313##
314
315#SeeAlso Mask reset()
316
317##
318
319# ------------------------------------------------------------------------------
320
321#Method bool SK_WARN_UNUSED_RESULT extractSubset(SkPixmap* subset, const SkIRect& area) const
322
323Sets subset width, height, pixel address to intersection of Pixmap with area,
324if intersection is not empty; and return true. Otherwise, leave subset unchanged
325and return false.
326
327Failing to read the return value generates a compile time warning.
328
329#Param subset storage for width, height, pixel address of intersection ##
330#Param area bounds to intersect with Pixmap ##
331
332#Return true if intersection of Pixmap and area is not empty ##
333
334#Example
335#Image 3
336#Height 128
Cary Clark6fc50412017-09-21 12:31:06 -0400337void draw(SkCanvas* canvas) {
338 std::vector<int32_t> pixels;
339 pixels.resize(image->height() * image->width() * 4);
340 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
341 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
342 image->readPixels(pixmap, 0, 0);
343 SkPixmap inset;
344 if (pixmap.extractSubset(&inset, {128, 128, 512, 512})) {
345 SkBitmap bitmap;
346 bitmap.installPixels(inset);
347 canvas->drawBitmap(bitmap, 0, 0);
348 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400349}
350##
351
352#SeeAlso reset() SkIRect::intersect
353
354##
355
356#Subtopic Initialization ##
357
358#Subtopic Image_Info_Access
359
360# ------------------------------------------------------------------------------
361
362#Method const SkImageInfo& info() const
363
Cary Clark6fc50412017-09-21 12:31:06 -0400364Returns width, height, Alpha_Type, Color_Type, and Color_Space.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400365
366#Return reference to ImageInfo ##
367
368#Example
369#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -0400370 std::vector<int32_t> pixels;
371 pixels.resize(image->height() * image->width() * 4);
372 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
373 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
374 image->readPixels(pixmap, 0, 0);
375 SkPixmap inset;
376 if (pixmap.extractSubset(&inset, {128, 128, 512, 512})) {
377 const SkImageInfo& info = inset.info();
378 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
379 const char* colors[] = {"Unknown", "Alpha", "RGB_565", "ARGB_4444",
380 "RGBA_8888", "BGRA_8888", "Gray_8", "RGBA_F16"};
381 SkDebugf("width: %d height: %d color: %s alpha: %s\n", info.width(), info.height(),
382 colors[info.colorType()], alphas[info.alphaType()]);
383 }
384#StdOut
385width: 384 height: 384 color: BGRA_8888 alpha: Opaque
386##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400387##
388
389#SeeAlso Image_Info
390
391##
392
393# ------------------------------------------------------------------------------
394
395#Method size_t rowBytes() const
396
397Returns row bytes, the interval from one pixel row to the next. Row bytes
398is at least as large as
399#Formula
400width() * info().bytesPerPixel()
401##
402.
403
404It is up to the Pixmap creator to ensure that row bytes is a useful value.
405
406#Return byte length of pixel row ##
407
408#Example
409SkPixmap badPixmap = {SkImageInfo::MakeA8(4, 4), nullptr, 2};
410SkPixmap okPixmap = {SkImageInfo::MakeA8(4, 4), nullptr, 8};
411for (auto& pixmap : { badPixmap, okPixmap } ) {
412 SkDebugf("rowBytes: %d minRowBytes: %d\n", pixmap.rowBytes(),
413 pixmap.info().minRowBytes());
414}
415#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400416rowBytes: 2 minRowBytes: 4
Cary Clarkd0530ba2017-09-14 11:25:39 -0400417rowBytes: 8 minRowBytes: 4
418##
419##
420
421#SeeAlso addr() info() SkImageInfo::minRowBytes
422
423##
424
425# ------------------------------------------------------------------------------
426
427#Method const void* addr() const
428
Cary Clark6fc50412017-09-21 12:31:06 -0400429Returns pixel address, the base address corresponding to the pixel origin.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400430
431It is up to the Pixmap creator to ensure that pixel address is a useful value.
432
433#Return pixel address ##
434
435#Example
436#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -0400437 std::vector<int32_t> pixels;
438 pixels.resize(image->height() * image->width() * 4);
439 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
440 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
441 image->readPixels(pixmap, 0, 0);
442 SkDebugf("pixels address: 0x%llx\n", pixmap.addr());
443 SkPixmap inset;
444 if (pixmap.extractSubset(&inset, {128, 128, 512, 512})) {
445 SkDebugf("inset address: 0x%llx\n", inset.addr());
446 }
447#StdOut
448#Volatile
449pixels address: 0x7f2a440bb010
450inset address: 0x7f2a440fb210
451##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400452##
453
454#SeeAlso addr(int x, int y) addr8 addr16 addr32 addr64 info() rowBytes()
455
456##
457
458# ------------------------------------------------------------------------------
459
460#Method int width() const
461
462Returns pixel count in each pixel row. Should be equal or less than:
463#Formula
464rowBytes() / info.bytesPerPixel()
465##
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
479#SeeAlso height()
480
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
492 SkImageInfo info = SkImageInfo::MakeA8(16, 32);
Cary Clark6fc50412017-09-21 12:31:06 -0400493 SkPixmap pixmap(info, nullptr, 64);
494 SkDebugf("pixmap height: %d info height: %d\n", pixmap.height(), info.height());
495#StdOut
496pixmap height: 32 info height: 32
497##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400498##
499
500#SeeAlso width()
501
502##
503
504# ------------------------------------------------------------------------------
505
506#Method SkColorType colorType() const
507
508Returns Color_Type, one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
509kRGB_565_SkColorType, kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
510kBGRA_8888_SkColorType, kGray_8_SkColorType, kRGBA_F16_SkColorType.
511
512#Return Color_Type in Image_Info ##
513
514#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400515 const char* colors[] = {"Unknown", "Alpha", "RGB_565", "ARGB_4444",
516 "RGBA_8888", "BGRA_8888", "Gray_8", "RGBA_F16"};
517 SkPixmap pixmap(SkImageInfo::MakeA8(16, 32), nullptr, 64);
518 SkDebugf("color type: k" "%s" "_SkColorType\n", colors[pixmap.colorType()]);
519#StdOut
Cary Clarkd0530ba2017-09-14 11:25:39 -0400520color type: kAlpha_SkColorType
Cary Clark6fc50412017-09-21 12:31:06 -0400521##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400522##
523
524#SeeAlso alphaType()
525
526##
527
528# ------------------------------------------------------------------------------
529
530#Method SkAlphaType alphaType() const
531
532Returns Alpha_Type, one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
533kPremul_SkAlphaType, kUnpremul_SkAlphaType.
534
535#Return Alpha_Type in Image_Info ##
536
537#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400538 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
539 SkPixmap pixmap(SkImageInfo::MakeA8(16, 32), nullptr, 64);
540 SkDebugf("alpha type: k" "%s" "_SkAlphaType\n", alphas[pixmap.alphaType()]);
Cary Clarkd0530ba2017-09-14 11:25:39 -0400541#StdOut
542alpha type: kPremul_SkAlphaType
543##
544##
545
546#SeeAlso colorType()
547
548##
549
550# ------------------------------------------------------------------------------
551
552#Method SkColorSpace* colorSpace() const
553
554Returns Color_Space associated with Image_Info.
555
556#Return Color_Space in Image_Info ##
557
558#Example
559#Description
560SkColorSpace::MakeSRGBLinear creates Color_Space with linear gamma
561and an sRGB gamut. This Color_Space gamma is not close to sRGB gamma.
562##
Cary Clark6fc50412017-09-21 12:31:06 -0400563 SkPixmap pixmap(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
564 SkColorSpace::MakeSRGBLinear()), nullptr, 64);
565 SkColorSpace* colorSpace = pixmap.colorSpace();
566 SkDebugf("gammaCloseToSRGB: %s gammaIsLinear: %s isSRGB: %s\n",
567 colorSpace->gammaCloseToSRGB() ? "true" : "false",
568 colorSpace->gammaIsLinear() ? "true" : "false",
569 colorSpace->isSRGB() ? "true" : "false");
570#StdOut
571gammaCloseToSRGB: false gammaIsLinear: true isSRGB: false
572##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400573##
574
575#SeeAlso Color_Space
576
577##
578
579# ------------------------------------------------------------------------------
580
581#Method bool isOpaque() const
582
583Returns true if Alpha_Type is kOpaque_SkAlphaType.
584Does not check if Color_Type allows Alpha, or Alpha in pixel values.
585
586#Return true if Image_Info has opaque Alpha_Type ##
587
588#Example
589#Description
590 isOpaque ignores whether all pixels are opaque or not.
591##
Cary Clark6fc50412017-09-21 12:31:06 -0400592 std::vector<uint32_t> pixels;
593 const int height = 2;
594 const int width = 2;
595 pixels.resize(height * width * 4);
596 SkPixmap pixmap(SkImageInfo::Make(width, height, kN32_SkColorType,
597 kPremul_SkAlphaType), (const void*) &pixels.front(), width * 4);
598 for (int index = 0; index < 2; ++index) {
599 pixmap.erase(0x00000000);
600 SkDebugf("isOpaque: %s\n", pixmap.isOpaque() ? "true" : "false");
601 pixmap.erase(0xFFFFFFFF);
602 SkDebugf("isOpaque: %s\n", pixmap.isOpaque() ? "true" : "false");
603 pixmap.reset(pixmap.info().makeAlphaType(kOpaque_SkAlphaType),
604 (const void*) &pixels.front(), width * 4);
605 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400606#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400607isOpaque: false
608isOpaque: false
609isOpaque: true
Cary Clarkd0530ba2017-09-14 11:25:39 -0400610isOpaque: true
611##
612##
613
614#SeeAlso computeIsOpaque SkImageInfo::isOpaque
615
616##
617
618# ------------------------------------------------------------------------------
619
620#Method SkIRect bounds() const
621
622Returns IRect
623#Formula
624{ 0, 0, width(), height() }
625##
626.
627
628#Return integral rectangle from origin to width() and height() ##
629
630#Example
631 for (int width : { 0, 2 } ) {
632 for (int height : { 0, 2 } ) {
Cary Clark6fc50412017-09-21 12:31:06 -0400633 SkPixmap pixmap(SkImageInfo::MakeA8(width, height), nullptr, width);
Cary Clarkd0530ba2017-09-14 11:25:39 -0400634 SkDebugf("width: %d height: %d empty: %s\n", width, height,
635 pixmap.bounds().isEmpty() ? "true" : "false");
636 }
637 }
638#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400639width: 0 height: 0 empty: true
640width: 0 height: 2 empty: true
641width: 2 height: 0 empty: true
Cary Clarkd0530ba2017-09-14 11:25:39 -0400642width: 2 height: 2 empty: false
643##
644##
645
646#SeeAlso height() width() IRect
647
648##
649
650# ------------------------------------------------------------------------------
651
652#Method int rowBytesAsPixels() const
653
654
655Returns number of pixels that fit on row. Should be greater than or equal to
656width().
657
658#Return maximum pixels per row ##
659
660#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400661 for (int rowBytes : { 4, 5, 6, 7, 8} ) {
662 SkPixmap pixmap(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), nullptr, rowBytes);
663 SkDebugf("rowBytes: %d rowBytesAsPixels: %d\n", rowBytes, pixmap.rowBytesAsPixels());
664 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400665#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400666rowBytes: 4 rowBytesAsPixels: 1
667rowBytes: 5 rowBytesAsPixels: 1
668rowBytes: 6 rowBytesAsPixels: 1
669rowBytes: 7 rowBytesAsPixels: 1
Cary Clarkd0530ba2017-09-14 11:25:39 -0400670rowBytes: 8 rowBytesAsPixels: 2
671##
672##
673
674#SeeAlso rowBytes shiftPerPixel width SkImageInfo::bytesPerPixel
675
676##
677
678# ------------------------------------------------------------------------------
679
680#Method int shiftPerPixel() const
681
682Returns bit shift converting row bytes to row pixels.
683Returns zero for kUnknown_SkColorType.
684
685#Return one of: 0, 1, 2, 3; left shift to convert pixels to bytes ##
686
687#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400688 const char* colors[] = {"Unknown", "Alpha", "RGB_565", "ARGB_4444",
689 "RGBA_8888", "BGRA_8888", "Gray_8", "RGBA_F16"};
690 SkImageInfo info = SkImageInfo::MakeA8(1, 1);
691 for (SkColorType colorType : { kUnknown_SkColorType, kAlpha_8_SkColorType,
692 kRGB_565_SkColorType, kARGB_4444_SkColorType,
693 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
694 kGray_8_SkColorType, kRGBA_F16_SkColorType } ) {
695 SkPixmap pixmap(info.makeColorType(colorType), nullptr, 4);
696 SkDebugf("color: k" "%s" "_SkColorType" "%*s" "bytesPerPixel: %d shiftPerPixel: %d\n",
697 colors[colorType], 10 - strlen(colors[colorType]), " ",
698 pixmap.info().bytesPerPixel(), pixmap.shiftPerPixel());
699 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400700#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400701color: kUnknown_SkColorType bytesPerPixel: 0 shiftPerPixel: 0
702color: kAlpha_SkColorType bytesPerPixel: 1 shiftPerPixel: 0
703color: kRGB_565_SkColorType bytesPerPixel: 2 shiftPerPixel: 1
704color: kARGB_4444_SkColorType bytesPerPixel: 2 shiftPerPixel: 1
705color: kRGBA_8888_SkColorType bytesPerPixel: 4 shiftPerPixel: 2
706color: kBGRA_8888_SkColorType bytesPerPixel: 4 shiftPerPixel: 2
707color: kGray_8_SkColorType bytesPerPixel: 1 shiftPerPixel: 0
Cary Clarkd0530ba2017-09-14 11:25:39 -0400708color: kRGBA_F16_SkColorType bytesPerPixel: 8 shiftPerPixel: 3
709##
710##
711
712#SeeAlso rowBytes rowBytesAsPixels width SkImageInfo::bytesPerPixel
713
714##
715
716# ------------------------------------------------------------------------------
717
718#Method uint64_t getSize64() const
719
720Returns conservative memory required for pixel storage.
721Includes unused memory on last row when rowBytesAsPixels exceeds width().
722
723#Return conservative pixel storage size ##
724
725#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400726 SkPixmap pixmap;
727 for (int width : { 1, 1000, 1000000 } ) {
728 for (int height: { 1, 1000, 1000000 } ) {
729 SkImageInfo imageInfo = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
730 pixmap.reset(imageInfo , nullptr, width * 5);
731 SkDebugf("width: %7d height: %7d getSize64: %13lld\n", width, height, pixmap.getSize64());
732 }
733 }
734#StdOut
735width: 1 height: 1 getSize64: 5
736width: 1 height: 1000 getSize64: 5000
737width: 1 height: 1000000 getSize64: 5000000
738width: 1000 height: 1 getSize64: 5000
739width: 1000 height: 1000 getSize64: 5000000
740width: 1000 height: 1000000 getSize64: 5000000000
741width: 1000000 height: 1 getSize64: 5000000
742width: 1000000 height: 1000 getSize64: 5000000000
743width: 1000000 height: 1000000 getSize64: 5000000000000
744##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400745##
746
747#SeeAlso getSafeSize64 getSafeSize height() rowBytes width() SkImageInfo::bytesPerPixel
748
749##
750
751# ------------------------------------------------------------------------------
752
753#Method uint64_t getSafeSize64() const
754
755Returns minimum memory required for pixel storage.
756Does not include unused memory on last row when rowBytesAsPixels exceeds width().
757
758#Return exact pixel storage size ##
759
760#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400761 SkPixmap pixmap;
762 for (int width : { 1, 1000, 1000000 } ) {
763 for (int height: { 1, 1000, 1000000 } ) {
764 SkImageInfo imageInfo = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
765 pixmap.reset(imageInfo , nullptr, width * 5);
766 SkDebugf("width: %7d height: %7d getSafeSize64: %13lld\n", width, height,
767 pixmap.getSafeSize64());
768 }
769 }
770#StdOut
771width: 1 height: 1 getSafeSize64: 4
772width: 1 height: 1000 getSafeSize64: 4999
773width: 1 height: 1000000 getSafeSize64: 4999999
774width: 1000 height: 1 getSafeSize64: 4000
775width: 1000 height: 1000 getSafeSize64: 4999000
776width: 1000 height: 1000000 getSafeSize64: 4999999000
777width: 1000000 height: 1 getSafeSize64: 4000000
778width: 1000000 height: 1000 getSafeSize64: 4999000000
Cary Clarkd0530ba2017-09-14 11:25:39 -0400779width: 1000000 height: 1000000 getSafeSize64: 4999999000000
780##
781##
782
783#SeeAlso getSize64 getSafeSize height() rowBytes width() SkImageInfo::bytesPerPixel
784
785##
786
787# ------------------------------------------------------------------------------
788
789#Method size_t getSafeSize() const
790
791Returns minimum memory required for pixel storage.
792Does not include unused memory on last row when rowBytesAsPixels exceeds width().
793Returns zero if value is does not fit in a signed 32-bit integer.
794The largest value than can be returned is 2,147,483,647.
795
796#Return exact pixel storage size if size fits in signed 32 bits ##
797
798#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400799void draw(SkCanvas* canvas) {
800 SkPixmap pixmap;
801 for (int width : { 1, 1000, 1000000 } ) {
802 for (int height: { 1, 1000, 1000000 } ) {
803 SkImageInfo imageInfo = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
804 pixmap.reset(imageInfo , nullptr, width * 5);
805 SkDebugf("width: %7d height: %7d getSafeSize: %7d\n", width, height, pixmap.getSafeSize());
806 }
807 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400808}
Cary Clark6fc50412017-09-21 12:31:06 -0400809#StdOut
810width: 1 height: 1 getSafeSize: 4
811width: 1 height: 1000 getSafeSize: 4999
812width: 1 height: 1000000 getSafeSize: 4999999
813width: 1000 height: 1 getSafeSize: 4000
814width: 1000 height: 1000 getSafeSize: 4999000
815width: 1000 height: 1000000 getSafeSize: 0
816width: 1000000 height: 1 getSafeSize: 4000000
817width: 1000000 height: 1000 getSafeSize: 0
818width: 1000000 height: 1000000 getSafeSize: 0
Cary Clarkd0530ba2017-09-14 11:25:39 -0400819##
820##
821
822#SeeAlso getSize64 getSafeSize64 height() rowBytes width() SkImageInfo::bytesPerPixel sk_64_isS32
823
824##
825
826#Subtopic Image_Info_Access ##
827
828#Subtopic Reader
829
830# ------------------------------------------------------------------------------
831
832#Method bool computeIsOpaque() const
833
834Returns true if all pixels are opaque. Color_Type determines how pixels
835are encoded, and whether pixel describes Alpha. Returns true for Color_Types
Cary Clark6fc50412017-09-21 12:31:06 -0400836without alpha in each pixel; for other Color_Types, returns true if all
Cary Clarkd0530ba2017-09-14 11:25:39 -0400837pixels have alpha values equivalent to 1.0 or greater.
838
839For Color_Types kRGB_565_SkColorType or kGray_8_SkColorType: always
840returns true. For Color_Types kAlpha_8_SkColorType, kBGRA_8888_SkColorType,
841kRGBA_8888_SkColorType: returns true if all pixel Alpha values are 255.
842For Color_Type kARGB_4444_SkColorType: returns true if all pixel Alpha values are 15.
843For kRGBA_F16_SkColorType: returns true if all pixel Alpha values are 1.0 or
844greater.
845
Cary Clark6fc50412017-09-21 12:31:06 -0400846Returns false for kUnknown_SkColorType.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400847
848#Return true all pixels have opaque values or Color_Type is opaque ##
849
850#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400851 std::vector<uint32_t> pixels;
852 const int height = 2;
853 const int width = 2;
854 pixels.resize(height * width * 4);
855 SkPixmap pixmap(SkImageInfo::Make(width, height, kN32_SkColorType,
856 kPremul_SkAlphaType), (const void*) &pixels.front(), width * 4);
857 for (int index = 0; index < 2; ++index) {
858 pixmap.erase(0x00000000);
859 SkDebugf("computeIsOpaque: %s\n", pixmap.computeIsOpaque() ? "true" : "false");
860 pixmap.erase(0xFFFFFFFF);
861 SkDebugf("computeIsOpaque: %s\n", pixmap.computeIsOpaque() ? "true" : "false");
862 pixmap.reset(pixmap.info().makeAlphaType(kOpaque_SkAlphaType),
863 (const void*) &pixels.front(), width * 4);
Cary Clarkd0530ba2017-09-14 11:25:39 -0400864 }
865#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400866computeIsOpaque: false
867computeIsOpaque: true
868computeIsOpaque: false
Cary Clarkd0530ba2017-09-14 11:25:39 -0400869computeIsOpaque: true
870##
871##
872
873#SeeAlso isOpaque Color_Type Alpha
874
875##
876
877# ------------------------------------------------------------------------------
878
879#Method SkColor getColor(int x, int y) const
880
881Returns pixel at (x, y) as Unpremultiplied Color.
882Returns black with Alpha if Color_Type is kAlpha_8_SkColorType.
883
884Input is not validated: out of bounds values of x or y trigger an assert() if
885built with SK_DEBUG defined; and returns undefined values or may crash if
886SK_RELEASE is defined. Fails if Color_Type is kUnknown_SkColorType or
887pixel address is nullptr.
888
889Color_Space in Image_Info is ignored. Some Color precision may be lost in the
890conversion to Unpremultiplied Color; original pixel data may have additional
891precision.
892
Cary Clark6fc50412017-09-21 12:31:06 -0400893#Param x column index, zero or greater, and less than width() ##
894#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400895
896#Return pixel converted to Unpremultiplied Color ##
897
898#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400899 const int w = 4;
900 const int h = 4;
901 std::vector<SkPMColor> storage;
902 storage.resize(w * h);
903 SkDebugf("Premultiplied:\n");
904 for (int y = 0; y < h; ++y) {
905 SkDebugf("(0, %d) ", y);
906 for (int x = 0; x < w; ++x) {
907 int a = 0xFF * (x + y) / (w - 1 + h - 1);
908 storage[x + y * w] = SkPackARGB32(a, a * x / (w - 1), a * y / (h - 1), a);
909 SkDebugf("0x%08x%c", storage[x + y * w], x == w - 1 ? '\n' : ' ');
910 }
911 }
912 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), &storage.front(), w * 4);
913 SkDebugf("Unpremultiplied:\n");
914 for (int y = 0; y < h; ++y) {
915 SkDebugf("(0, %d) ", y);
916 for (int x = 0; x < w; ++x) {
917 SkDebugf("0x%08x%c", pixmap.getColor(x, y), x == w - 1 ? '\n' : ' ');
918 }
919 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400920#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400921Premultiplied:
922(0, 0) 0x00000000 0x2a0e002a 0x55380055 0x7f7f007f
923(0, 1) 0x2a000e2a 0x551c1c55 0x7f542a7f 0xaaaa38aa
924(0, 2) 0x55003855 0x7f2a547f 0xaa7171aa 0xd4d48dd4
925(0, 3) 0x7f007f7f 0xaa38aaaa 0xd48dd4d4 0xffffffff
926Unpremultiplied:
927(0, 0) 0x00000000 0x2a5500ff 0x55a800ff 0x7fff00ff
928(0, 1) 0x2a0055ff 0x555454ff 0x7fa954ff 0xaaff54ff
929(0, 2) 0x5500a8ff 0x7f54a9ff 0xaaaaaaff 0xd4ffaaff
Cary Clarkd0530ba2017-09-14 11:25:39 -0400930(0, 3) 0x7f00ffff 0xaa54ffff 0xd4aaffff 0xffffffff
931##
932##
933
934#SeeAlso addr() readPixels
935
936##
937
938#Subtopic Reader ##
939
940#Subtopic Readable_Address
941
942# ------------------------------------------------------------------------------
943
944#Method const void* addr(int x, int y) const
945
946Returns readable pixel address at (x, y).
947
948Input is not validated: out of bounds values of x or y trigger an assert() if
949built with SK_DEBUG defined. Returns zero if Color_Type is kUnknown_SkColorType.
950
Cary Clark6fc50412017-09-21 12:31:06 -0400951#Param x column index, zero or greater, and less than width() ##
952#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400953
954#Return readable generic pointer to pixel ##
955
956#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400957 const int w = 4;
958 const int h = 4;
959 std::vector<SkPMColor> storage;
960 storage.resize(w * h);
961 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), &storage.front(), w * 4);
Cary Clarkd0530ba2017-09-14 11:25:39 -0400962 SkDebugf("pixmap.addr(1, 2) %c= &storage[1 + 2 * w]\n",
963 pixmap.addr(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
964#StdOut
965pixmap.addr(1, 2) == &storage[1 + 2 * w]
966##
967##
968
969#SeeAlso addr8 addr16 addr32 addr64 addrF16 getColor writable_addr
970
971##
972
973# ------------------------------------------------------------------------------
974
975#Method const uint8_t* addr8() const
976
977Returns readable base pixel address. Result is addressable as unsigned 8-bit bytes.
978Will trigger an assert() if Color_Type is not kAlpha_8_SkColorType or
979kGray_8_SkColorType, and is built with SK_DEBUG defined.
980
981One byte corresponds to one pixel.
982
983#Return readable unsigned 8-bit pointer to pixels ##
984
985#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400986 const int w = 4;
987 const int h = 4;
988 uint8_t storage[w * h];
989 SkPixmap pixmap(SkImageInfo::Make(w, h, kGray_8_SkColorType, kPremul_SkAlphaType),
990 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -0400991 SkDebugf("pixmap.addr8() %c= storage\n",
992 pixmap.addr8() == storage ? '=' : '!');
993#StdOut
994pixmap.addr8() == storage
995##
996##
997
998#SeeAlso addr() addr16 addr32 addr64 addrF16 getColor writable_addr writable_addr8
999
1000##
1001
1002# ------------------------------------------------------------------------------
1003
1004#Method const uint16_t* addr16() const
1005
1006Returns readable base pixel address. Result is addressable as unsigned 16-bit words.
1007Will trigger an assert() if Color_Type is not kRGB_565_SkColorType or
1008kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
1009
1010One word corresponds to one pixel.
1011
1012#Return readable unsigned 16-bit pointer to pixels ##
1013
1014#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001015 const int w = 4;
1016 const int h = 4;
1017 uint16_t storage[w * h];
1018 SkPixmap pixmap(SkImageInfo::Make(w, h, kARGB_4444_SkColorType, kPremul_SkAlphaType),
1019 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001020 SkDebugf("pixmap.addr16() %c= storage\n",
1021 pixmap.addr16() == storage ? '=' : '!');
1022#StdOut
1023pixmap.addr16() == storage
1024##
1025##
1026
1027#SeeAlso addr() addr8 addr32 addr64 addrF16 getColor writable_addr writable_addr16
1028
1029##
1030
1031# ------------------------------------------------------------------------------
1032
1033#Method const uint32_t* addr32() const
1034
1035Returns readable base pixel address. Result is addressable as unsigned 32-bit words.
1036Will trigger an assert() if Color_Type is not kRGBA_8888_SkColorType or
1037kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.
1038
1039One word corresponds to one pixel.
1040
1041#Return readable unsigned 32-bit pointer to pixels ##
1042
1043#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001044 const int w = 4;
1045 const int h = 4;
1046 uint32_t storage[w * h];
1047 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType),
1048 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001049 SkDebugf("pixmap.addr32() %c= storage\n",
1050 pixmap.addr32() == storage ? '=' : '!');
1051#StdOut
1052pixmap.addr32() == storage
1053##
1054##
1055
1056#SeeAlso addr() addr8 addr16 addr64 addrF16 getColor writable_addr writable_addr32
1057
1058##
1059
1060# ------------------------------------------------------------------------------
1061
1062#Method const uint64_t* addr64() const
1063
1064Returns readable base pixel address. Result is addressable as unsigned 64-bit words.
1065Will trigger an assert() if Color_Type is not kRGBA_F16_SkColorType and is built
1066with SK_DEBUG defined.
1067
1068One word corresponds to one pixel.
1069
1070#Return readable unsigned 64-bit pointer to pixels ##
1071
1072#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001073 const int w = 4;
1074 const int h = 4;
1075 uint64_t storage[w * h];
1076 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType),
1077 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001078 SkDebugf("pixmap.addr64() %c= storage\n",
1079 pixmap.addr64() == storage ? '=' : '!');
1080#StdOut
1081pixmap.addr64() == storage
1082##
1083##
1084
1085#SeeAlso addr() addr8 addr16 addr32 addrF16 getColor writable_addr writable_addr64
1086
1087##
1088
1089# ------------------------------------------------------------------------------
1090
1091#Method const uint16_t* addrF16() const
1092
1093Returns readable base pixel address. Result is addressable as unsigned 16-bit words.
1094Will trigger an assert() if Color_Type is not kRGBA_F16_SkColorType and is built
1095with SK_DEBUG defined.
1096
1097Each word represents one color component encoded as a half float.
1098Four words correspond to one pixel.
1099
1100#Return readable unsigned 16-bit pointer to first component of pixels ##
1101
1102#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001103 const int w = 4;
1104 const int h = 4;
1105 uint16_t storage[w * h * 4];
1106 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType),
1107 storage, w * 4 * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001108 SkDebugf("pixmap.addrF16() %c= storage\n",
1109 pixmap.addrF16() == storage ? '=' : '!');
1110#StdOut
1111pixmap.addrF16() == storage
1112##
1113##
1114
1115#SeeAlso addr() addr8 addr16 addr32 addr64 getColor writable_addr writable_addrF16
1116
1117##
1118
1119# ------------------------------------------------------------------------------
1120
1121#Method const uint8_t* addr8(int x, int y) const
1122
1123Returns readable pixel address at (x, y).
1124
1125Input is not validated: out of bounds values of x or y trigger an assert() if
1126built with SK_DEBUG defined.
1127
1128Will trigger an assert() if Color_Type is not kAlpha_8_SkColorType or
1129kGray_8_SkColorType, and is built with SK_DEBUG defined.
1130
Cary Clark6fc50412017-09-21 12:31:06 -04001131#Param x column index, zero or greater, and less than width() ##
1132#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001133
1134#Return readable unsigned 8-bit pointer to pixel at (x, y) ##
1135
1136#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001137 const int w = 4;
1138 const int h = 4;
1139 uint8_t storage[w * h];
1140 SkPixmap pixmap(SkImageInfo::Make(w, h, kGray_8_SkColorType, kPremul_SkAlphaType),
1141 storage, w * sizeof(storage[0]));
1142 SkDebugf("pixmap.addr8(1, 2) %c= &storage[1 + 2 * w]\n",
1143 pixmap.addr8(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
Cary Clarkd0530ba2017-09-14 11:25:39 -04001144#StdOut
1145pixmap.addr8(1, 2) == &storage[1 + 2 * w]
1146##
1147##
1148
1149#SeeAlso addr() addr16 addr32 addr64 addrF16 getColor writable_addr writable_addr8
1150
1151##
1152
1153# ------------------------------------------------------------------------------
1154
1155#Method const uint16_t* addr16(int x, int y) const
1156
1157Returns readable pixel address at (x, y).
1158
1159Input is not validated: out of bounds values of x or y trigger an assert() if
1160built with SK_DEBUG defined.
1161
1162Will trigger an assert() if Color_Type is not kRGB_565_SkColorType or
1163kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
1164
Cary Clark6fc50412017-09-21 12:31:06 -04001165#Param x column index, zero or greater, and less than width() ##
1166#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001167
1168#Return readable unsigned 16-bit pointer to pixel at (x, y) ##
1169
1170#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001171 const int w = 4;
1172 const int h = 4;
1173 uint16_t storage[w * h];
1174 SkPixmap pixmap(SkImageInfo::Make(w, h, kARGB_4444_SkColorType, kPremul_SkAlphaType),
1175 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001176 SkDebugf("pixmap.addr16(1, 2) %c= &storage[1 + 2 * w]\n",
1177 pixmap.addr16(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
1178#StdOut
1179pixmap.addr16(1, 2) == &storage[1 + 2 * w]
1180##
1181##
1182
1183#SeeAlso addr() addr8 addr32 addr64 addrF16 getColor writable_addr writable_addr16
1184
1185##
1186
1187# ------------------------------------------------------------------------------
1188
1189#Method const uint32_t* addr32(int x, int y) const
1190
1191Returns readable pixel address at (x, y).
1192
1193Input is not validated: out of bounds values of x or y trigger an assert() if
1194built with SK_DEBUG defined.
1195
1196Will trigger an assert() if Color_Type is not kRGBA_8888_SkColorType or
1197kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.
1198
Cary Clark6fc50412017-09-21 12:31:06 -04001199#Param x column index, zero or greater, and less than width() ##
1200#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001201
1202#Return readable unsigned 32-bit pointer to pixel at (x, y) ##
1203
1204#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001205 const int w = 4;
1206 const int h = 4;
1207 uint32_t storage[w * h];
1208 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
1209 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001210 SkDebugf("pixmap.addr32(1, 2) %c= &storage[1 + 2 * w]\n",
1211 pixmap.addr32(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
1212#StdOut
1213pixmap.addr32(1, 2) == &storage[1 + 2 * w]
1214##
1215##
1216
1217#ToDo incomplete ##
1218
1219##
1220
1221# ------------------------------------------------------------------------------
1222
1223#Method const uint64_t* addr64(int x, int y) const
1224
1225Returns readable pixel address at (x, y).
1226
1227Input is not validated: out of bounds values of x or y trigger an assert() if
1228built with SK_DEBUG defined.
1229
1230Will trigger an assert() if Color_Type is not kRGBA_F16_SkColorType and is built
1231with SK_DEBUG defined.
1232
Cary Clark6fc50412017-09-21 12:31:06 -04001233#Param x column index, zero or greater, and less than width() ##
1234#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001235
1236#Return readable unsigned 64-bit pointer to pixel at (x, y) ##
1237
1238#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001239 const int w = 4;
1240 const int h = 4;
1241 uint64_t storage[w * h];
1242 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType),
1243 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001244 SkDebugf("pixmap.addr64(1, 2) %c= &storage[1 + 2 * w]\n",
1245 pixmap.addr64(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
1246#StdOut
1247pixmap.addr64(1, 2) == &storage[1 + 2 * w]
1248##
1249##
1250
1251#SeeAlso addr() addr8 addr16 addr32 addrF16 getColor writable_addr writable_addr64
1252
1253##
1254
1255# ------------------------------------------------------------------------------
1256
1257#Method const uint16_t* addrF16(int x, int y) const
1258
1259Returns readable pixel address at (x, y).
1260
1261Input is not validated: out of bounds values of x or y trigger an assert() if
1262built with SK_DEBUG defined.
1263
1264Will trigger an assert() if Color_Type is not kRGBA_F16_SkColorType and is built
1265with SK_DEBUG defined.
1266
1267Each unsigned 16-bit word represents one color component encoded as a half float.
1268Four words correspond to one pixel.
1269
Cary Clark6fc50412017-09-21 12:31:06 -04001270#Param x column index, zero or greater, and less than width() ##
1271#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001272
1273#Return readable unsigned 16-bit pointer to pixel component at (x, y) ##
1274
1275#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001276 const int w = 4;
1277 const int h = 4;
1278 const int wordsPerPixel = 4;
1279 const int rowWords = w * wordsPerPixel;
1280 uint16_t storage[rowWords * h];
1281 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType),
1282 storage, rowWords * sizeof(storage[0]));
1283 SkDebugf("pixmap.addrF16(1, 2) %c= &storage[1 * wordsPerPixel + 2 * rowWords]\n",
1284 pixmap.addrF16(1, 2) == &storage[1 * wordsPerPixel + 2 * rowWords] ? '=' : '!');
Cary Clarkd0530ba2017-09-14 11:25:39 -04001285#StdOut
1286pixmap.addrF16(1, 2) == &storage[1 * wordsPerPixel + 2 * rowWords]
1287##
1288##
1289
1290#SeeAlso addr() addr8 addr16 addr32 addr64 getColor writable_addr writable_addrF16
1291
1292##
1293
1294#Subtopic Readable_Address ##
1295
1296#Subtopic Writable_Address
1297
1298# ------------------------------------------------------------------------------
1299
1300#Method void* writable_addr() const
1301
1302Returns writable base pixel address.
1303
1304#Return writable generic base pointer to pixels ##
1305
1306#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001307 const int w = 4;
1308 const int h = 4;
1309 SkPMColor storage[w * h * 4];
1310 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), storage, w * 4);
1311 SkDebugf("pixmap.writable_addr() %c= (void *)storage\n",
1312 pixmap.writable_addr() == (void *)storage ? '=' : '!');
1313 pixmap.erase(0x00000000);
1314 *(SkPMColor*)pixmap.writable_addr() = 0xFFFFFFFF;
1315 SkDebugf("pixmap.getColor(0, 1) %c= 0x00000000\n",
1316 pixmap.getColor(0, 1) == 0x00000000 ? '=' : '!');
1317 SkDebugf("pixmap.getColor(0, 0) %c= 0xFFFFFFFF\n",
Cary Clarkd0530ba2017-09-14 11:25:39 -04001318 pixmap.getColor(0, 0) == 0xFFFFFFFF ? '=' : '!');
1319#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -04001320pixmap.writable_addr() == (void *)storage
1321pixmap.getColor(0, 1) == 0x00000000
Cary Clarkd0530ba2017-09-14 11:25:39 -04001322pixmap.getColor(0, 0) == 0xFFFFFFFF
1323##
1324##
1325
1326#SeeAlso writable_addr8 writable_addr16 writable_addr32 writable_addr64 writable_addrF16 addr()
1327
1328##
1329
1330# ------------------------------------------------------------------------------
1331
1332#Method void* writable_addr(int x, int y) const
1333
1334Returns writable pixel address at (x, y).
1335
1336Input is not validated: out of bounds values of x or y trigger an assert() if
1337built with SK_DEBUG defined. Returns zero if Color_Type is kUnknown_SkColorType.
1338
Cary Clark6fc50412017-09-21 12:31:06 -04001339#Param x column index, zero or greater, and less than width() ##
1340#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001341
1342#Return writable generic pointer to pixel ##
1343
1344#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001345 const int w = 4;
1346 const int h = 4;
1347 SkPMColor storage[w * h * 4];
1348 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), storage, w * 4);
1349 SkDebugf("pixmap.writable_addr() %c= (void *)storage\n",
1350 pixmap.writable_addr() == (void *)storage ? '=' : '!');
1351 pixmap.erase(0x00000000);
1352 *(SkPMColor*)pixmap.writable_addr(1, 2) = 0xFFFFFFFF;
1353 SkDebugf("pixmap.getColor(0, 0) %c= 0x00000000\n",
1354 pixmap.getColor(0, 0) == 0x00000000 ? '=' : '!');
1355 SkDebugf("pixmap.getColor(1, 2) %c= 0xFFFFFFFF\n",
Cary Clarkd0530ba2017-09-14 11:25:39 -04001356 pixmap.getColor(1, 2) == 0xFFFFFFFF ? '=' : '!');
1357#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -04001358pixmap.writable_addr() == (void *)storage
1359pixmap.getColor(0, 0) == 0x00000000
Cary Clarkd0530ba2017-09-14 11:25:39 -04001360pixmap.getColor(1, 2) == 0xFFFFFFFF
1361##
1362##
1363
1364#SeeAlso writable_addr8 writable_addr16 writable_addr32 writable_addr64 writable_addrF16 addr()
1365
1366##
1367
1368# ------------------------------------------------------------------------------
1369
1370#Method uint8_t* writable_addr8(int x, int y) const
1371
Cary Clark6fc50412017-09-21 12:31:06 -04001372Returns writable pixel address at (x, y). Result is addressable as unsigned
13738-bit bytes. Will trigger an assert() if Color_Type is not kAlpha_8_SkColorType
1374or kGray_8_SkColorType, and is built with SK_DEBUG defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001375
1376One byte corresponds to one pixel.
1377
Cary Clark6fc50412017-09-21 12:31:06 -04001378#Param x column index, zero or greater, and less than width() ##
1379#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001380
1381#Return writable unsigned 8-bit pointer to pixels ##
1382
1383#Example
1384#Height 64
1385#Description
1386Altering pixels after drawing Bitmap is not guaranteed to affect subsequent
1387drawing on all platforms. Adding a second SkBitmap::installPixels after editing
1388pixel memory is safer.
1389##
Cary Clark6fc50412017-09-21 12:31:06 -04001390void draw(SkCanvas* canvas) {
1391 uint8_t storage[][5] = {{ 0, 0, 64, 0, 0},
1392 { 0, 128, 255, 128, 0},
1393 {64, 255, 255, 255, 64},
1394 { 0, 128, 255, 128, 0},
1395 { 0, 0, 64, 0, 0}};
1396 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kPremul_SkAlphaType);
1397 SkPixmap pixmap(imageInfo, storage[0], 5);
1398 SkBitmap bitmap;
1399 bitmap.installPixels(pixmap);
1400 canvas->scale(10, 10);
1401 canvas->drawBitmap(bitmap, 0, 0);
1402 *pixmap.writable_addr8(2, 2) = 0;
1403// bitmap.installPixels(pixmap); // uncomment to fix on GPU
1404 canvas->drawBitmap(bitmap, 10, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001405}
1406##
1407
1408#SeeAlso writable_addr writable_addr16 writable_addr32 writable_addr64 writable_addrF16 addr() addr8
1409
1410##
1411
1412# ------------------------------------------------------------------------------
1413
1414#Method uint16_t* writable_addr16(int x, int y) const
1415
Cary Clark6fc50412017-09-21 12:31:06 -04001416Returns writable_addr pixel address at (x, y). Result is addressable as unsigned
141716-bit words. Will trigger an assert() if Color_Type is not kRGB_565_SkColorType
1418or kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001419
1420One word corresponds to one pixel.
1421
Cary Clark6fc50412017-09-21 12:31:06 -04001422#Param x column index, zero or greater, and less than width() ##
1423#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001424
1425#Return writable unsigned 16-bit pointer to pixel ##
1426
1427#Example
1428#Description
1429Draw a five by five bitmap, and draw it again with a center black pixel.
1430The low nibble of the 16-bit word is Alpha.
1431##
1432#Height 64
Cary Clark6fc50412017-09-21 12:31:06 -04001433 uint16_t storage[][5] = {{ 0xCABF, 0xDABE, 0xCA9D, 0xC96C, 0xA39B },
1434 { 0xACEE, 0xA87C, 0x893A, 0x4779, 0x8708 },
1435 { 0x4B7C, 0x255B, 0x2559, 0x2557, 0x4656 },
1436 { 0x9099, 0x8128, 0x2557, 0x4124, 0x3323 },
1437 { 0x7547, 0x5505, 0x4434, 0x2012, 0x0000 }};
1438 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kARGB_4444_SkColorType, kPremul_SkAlphaType);
1439 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
1440 SkBitmap bitmap;
1441 bitmap.installPixels(pixmap);
1442 canvas->scale(10, 10);
1443 canvas->drawBitmap(bitmap, 0, 0);
1444 *pixmap.writable_addr16(2, 2) = 0x000F;
1445 bitmap.installPixels(pixmap);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001446 canvas->drawBitmap(bitmap, 10, 0);
1447##
1448
1449#SeeAlso writable_addr writable_addr8 writable_addr32 writable_addr64 writable_addrF16 addr() addr16
1450
1451##
1452
1453# ------------------------------------------------------------------------------
1454
1455#Method uint32_t* writable_addr32(int x, int y) const
1456
Cary Clark6fc50412017-09-21 12:31:06 -04001457Returns writable pixel address at (x, y). Result is addressable as unsigned
145832-bit words. Will trigger an assert() if Color_Type is not
1459kRGBA_8888_SkColorType or kBGRA_8888_SkColorType, and is built with SK_DEBUG
1460defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001461
1462One word corresponds to one pixel.
1463
Cary Clark6fc50412017-09-21 12:31:06 -04001464#Param x column index, zero or greater, and less than width() ##
1465#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001466
1467#Return writable unsigned 32-bit pointer to pixel ##
1468
1469#Example
1470#Image 4
1471#Height 72
Cary Clark6fc50412017-09-21 12:31:06 -04001472 std::vector<int32_t> pixels;
1473 pixels.resize(image->height() * image->width() * 4);
1474 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
1475 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
1476 image->readPixels(pixmap, 0, 0);
1477 for (int y = 0; y < pixmap.height() / 2; ++y) {
1478 for (int x = 0; x < pixmap.width(); ++x) {
1479 if ((x & 4) == (y & 4)) {
1480 SkTSwap(*pixmap.writable_addr32(x, y),
1481 *pixmap.writable_addr32(pixmap.width() - x, pixmap.height() - y));
1482 }
1483 }
1484 }
1485 SkBitmap bitmap;
1486 bitmap.installPixels(pixmap);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001487 canvas->drawBitmap(bitmap, 0, 0);
1488##
1489
1490#SeeAlso writable_addr writable_addr8 writable_addr16 writable_addr64 writable_addrF16 addr() addr32
1491
1492##
1493
1494# ------------------------------------------------------------------------------
1495
1496#Method uint64_t* writable_addr64(int x, int y) const
1497
Cary Clark6fc50412017-09-21 12:31:06 -04001498Returns writable pixel address at (x, y). Result is addressable as unsigned
149964-bit words. Will trigger an assert() if Color_Type is not
1500kRGBA_F16_SkColorType and is built with SK_DEBUG defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001501
1502One word corresponds to one pixel.
1503
Cary Clark6fc50412017-09-21 12:31:06 -04001504#Param x column index, zero or greater, and less than width() ##
1505#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001506
1507#Return writable unsigned 64-bit pointer to pixel ##
1508
1509#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001510 SkImageInfo info = SkImageInfo::Make(3, 3, kRGBA_F16_SkColorType, kPremul_SkAlphaType);
1511 uint64_t storage[9];
1512 SkPixmap pixmap(info, storage, 3 * sizeof(uint64_t));
1513 SkColor4f c4 { 1, 0.45f, 0.25f, 0.65f };
1514 pixmap.erase(c4);
1515 SkBitmap bitmap;
1516 canvas->scale(10, 10);
1517 bitmap.installPixels(pixmap);
1518 canvas->drawBitmap(bitmap, 0, 0);
1519 *pixmap.writable_addr64(1, 1) |= 0x00ff000000000000LL;
1520 bitmap.installPixels(pixmap);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001521 canvas->drawBitmap(bitmap, 10, 0);
1522##
1523
1524#SeeAlso writable_addr writable_addr8 writable_addr16 writable_addr32 writable_addrF16 addr() addr64
1525
1526##
1527
1528# ------------------------------------------------------------------------------
1529
1530#Method uint16_t* writable_addrF16(int x, int y) const
1531
Cary Clark6fc50412017-09-21 12:31:06 -04001532Returns writable pixel address at (x, y). Result is addressable as unsigned
153316-bit words. Will trigger an assert() if Color_Type is not
1534kRGBA_F16_SkColorType and is built with SK_DEBUG defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001535
1536Each word represents one color component encoded as a half float.
1537Four words correspond to one pixel.
1538
Cary Clark6fc50412017-09-21 12:31:06 -04001539#Param x column index, zero or greater, and less than width() ##
1540#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001541
1542#Return writable unsigned 16-bit pointer to first component of pixel ##
1543
1544#Example
1545#Height 64
1546#Description
1547Left bitmap is drawn with two pixels defined in half float format. Right bitmap
1548is drawn after overwriting bottom half float color with top half float color.
1549##
Cary Clark6fc50412017-09-21 12:31:06 -04001550 SkImageInfo info = SkImageInfo::Make(1, 2, kRGBA_F16_SkColorType, kPremul_SkAlphaType);
1551 uint16_t storage[2][4];
1552 SkPixmap pixmap(info, storage[0], sizeof(uint64_t));
1553 SkIRect topPixelBounds = {0, 0, 1, 1};
1554 pixmap.erase({ 0.65f, 0.45f, 0.25f, 1 }, &topPixelBounds);
1555 SkIRect bottomPixelBounds = {0, 1, 1, 2};
1556 pixmap.erase({ 0.25f, 0.65f, 0.45f, 1 }, &bottomPixelBounds);
1557 SkBitmap bitmap;
1558 canvas->scale(20, 20);
1559 bitmap.installPixels(pixmap);
1560 canvas->drawBitmap(bitmap, 0, 0);
1561 uint16_t* pixel2 = pixmap.writable_addrF16(0, 1);
1562 for (int i = 0; i < 4; ++i) {
1563 pixel2[i] = storage[0][i];
1564 }
1565 bitmap.installPixels(pixmap);
1566 canvas->drawBitmap(bitmap, 4, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001567##
1568
1569#SeeAlso writable_addr writable_addr8 writable_addr16 writable_addr32 writable_addr64 addr() addrF16
1570
1571##
1572
1573#Subtopic Writable_Address ##
1574
1575#Subtopic Writer
1576
1577# ------------------------------------------------------------------------------
1578
1579#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1580 int srcX, int srcY, SkTransferFunctionBehavior behavior) const
1581
Cary Clark6fc50412017-09-21 12:31:06 -04001582Copies a Rect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not exceed
Cary Clarkd0530ba2017-09-14 11:25:39 -04001583#Formula
1584(this->width(), this->height())
1585##
Cary Clark6fc50412017-09-21 12:31:06 -04001586.
1587
1588dstInfo specifies width, height, Color_Type, Alpha_Type, and
Cary Clarkd0530ba2017-09-14 11:25:39 -04001589Color_Space of destination. dstRowBytes specifics the gap from one destination
1590row to the next. Returns true if pixels are copied. Returns false if
1591dstInfo.addr() equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes.
1592
1593Pixels are copied only if pixel conversion is possible. If this->colorType is
1594kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1595If this->colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
1596If this->alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
1597match. If this->colorSpace is nullptr, dstInfo.colorSpace must match. Returns
1598false if pixel conversion is not possible.
1599
1600srcX and srcY may be negative to copy only top or left of source. Returns
1601false if width() or height() is zero or negative. Returns false if
1602#Formula
Cary Clark6fc50412017-09-21 12:31:06 -04001603abs(srcX) >= this->width()
1604##
1605, or if
1606#Formula
1607abs(srcY) >= this->height()
Cary Clarkd0530ba2017-09-14 11:25:39 -04001608##
1609.
1610
1611If behavior is SkTransferFunctionBehavior::kRespect: converts source
1612pixels to a linear space before converting to dstInfo.
1613If behavior is SkTransferFunctionBehavior::kIgnore: source
1614pixels are treated as if they are linear, regardless of their encoding.
1615
1616#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1617#Param dstPixels destination pixel storage ##
1618#Param dstRowBytes destination row length ##
1619#Param srcX column index whose absolute value is less than width() ##
1620#Param srcY row index whose absolute value is less than height() ##
1621#Param behavior one of: SkTransferFunctionBehavior::kRespect,
1622 SkTransferFunctionBehavior::kIgnore
1623##
1624
1625#Return true if pixels are copied to dstPixels ##
1626
1627#Example
1628#Image 3
1629void draw(SkCanvas* canvas) {
1630 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height(),
1631 canvas->imageInfo().colorSpace() ? SkColorSpace::MakeSRGB() : nullptr);
Cary Clark6fc50412017-09-21 12:31:06 -04001632 std::vector<int32_t> srcPixels;
1633 srcPixels.resize(image->height() * image->width() * 4);
1634 SkPixmap pixmap(info, (const void*) &srcPixels.front(), image->width() * 4);
1635 image->readPixels(pixmap, 0, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001636 SkTransferFunctionBehavior behavior = canvas->imageInfo().colorSpace() ?
1637 SkTransferFunctionBehavior::kRespect : SkTransferFunctionBehavior::kIgnore;
Cary Clark6fc50412017-09-21 12:31:06 -04001638 std::vector<int32_t> dstPixels;
1639 dstPixels.resize(image->height() * image->width() * 4);
1640 int offset = 0;
1641 for (auto behavior : { SkTransferFunctionBehavior::kRespect,
1642 SkTransferFunctionBehavior::kIgnore} ) {
Cary Clarkd0530ba2017-09-14 11:25:39 -04001643 pixmap.readPixels(info, &dstPixels.front(), image->width() * 4, offset, 0, behavior);
1644 offset += 128;
1645 }
1646 SkBitmap bitmap;
1647 SkPixmap dstmap(info, &dstPixels.front(), image->width() * 4);
1648 bitmap.installPixels(dstmap);
1649 canvas->drawBitmap(bitmap, 0, 0);
1650}
1651##
1652
1653#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1654
1655##
1656
1657# ------------------------------------------------------------------------------
1658
1659#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes) const
1660
1661Copies a Rect of pixels to dstPixels. Copy starts at (0, 0), and does not
1662exceed
1663#Formula
1664(this->width(), this->height())
1665##
Cary Clark6fc50412017-09-21 12:31:06 -04001666.
1667
1668dstInfo specifies width, height, Color_Type, Alpha_Type, and
Cary Clarkd0530ba2017-09-14 11:25:39 -04001669Color_Space of destination. dstRowBytes specifics the gap from one destination
1670row to the next. Returns true if pixels are copied. Returns false if
1671dstInfo.addr() equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes.
1672
1673Pixels are copied only if pixel conversion is possible. If this->colorType is
1674kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1675If this->colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
1676If this->alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
1677match. If this->colorSpace is nullptr, dstInfo.colorSpace must match. Returns
1678false if pixel conversion is not possible.
1679
1680Returns false if this->width() or this->height() is zero or negative.
1681
1682#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1683#Param dstPixels destination pixel storage ##
1684#Param dstRowBytes destination row length ##
1685
1686#Return true if pixels are copied to dstPixels ##
1687
1688#Example
1689#Height 128
1690#Description
1691Transferring the gradient from 8 bits per component to 4 bits per component
1692creates visible banding.
1693##
Cary Clark6fc50412017-09-21 12:31:06 -04001694 std::vector<int32_t> pixels;
1695 const int width = 256;
1696 const int height = 64;
1697 pixels.resize(height * width * 4);
1698 SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(width, height);
1699 SkPixmap srcPixmap(srcInfo, (const void*) &pixels.front(), width * 4);
1700 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
1701 SkPoint gradPoints[] = { { 0, 0 }, { 256, 0 } };
1702 SkPaint paint;
1703 paint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
1704 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
1705 SkBitmap bitmap;
1706 bitmap.installPixels(srcPixmap);
1707 SkCanvas srcCanvas(bitmap);
1708 srcCanvas.drawRect(SkRect::MakeWH(width, height), paint);
1709 canvas->drawBitmap(bitmap, 0, 0);
1710 std::vector<int32_t> dstPixels;
1711 dstPixels.resize(height * width * 2);
1712 SkImageInfo dstInfo = srcInfo.makeColorType(kARGB_4444_SkColorType);
1713 srcPixmap.readPixels(dstInfo, &dstPixels.front(), width * 2);
1714 SkPixmap dstPixmap(dstInfo, &dstPixels.front(), width * 2);
1715 bitmap.installPixels(dstPixmap);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001716 canvas->drawBitmap(bitmap, 0, 128);
1717##
1718
1719#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1720
1721##
1722
1723# ------------------------------------------------------------------------------
1724
1725#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, int srcX,
1726 int srcY) const
1727
1728Copies a Rect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not
1729exceed
1730#Formula
1731(this->width(), this->height())
1732##
Cary Clark6fc50412017-09-21 12:31:06 -04001733.
1734
1735dstInfo specifies width, height, Color_Type, Alpha_Type, and
Cary Clarkd0530ba2017-09-14 11:25:39 -04001736Color_Space of destination. dstRowBytes specifics the gap from one destination
1737row to the next. Returns true if pixels are copied. Returns false if
1738dstInfo.addr() equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes.
1739
1740Pixels are copied only if pixel conversion is possible. If this->colorType is
1741kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1742If this->colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
1743If this->alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
1744match. If this->colorSpace is nullptr, dstInfo.colorSpace must match. Returns
1745false if pixel conversion is not possible.
1746
1747srcX and srcY may be negative to copy only top or left of source. Returns
1748false if this->width() or this->height() is zero or negative. Returns false if
1749#Formula
Cary Clark6fc50412017-09-21 12:31:06 -04001750abs(srcX) >= this->width()
1751##
1752, or if
1753#Formula
1754abs(srcY) >= this->height()
Cary Clarkd0530ba2017-09-14 11:25:39 -04001755##
1756.
1757
1758#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1759#Param dstPixels destination pixel storage ##
1760#Param dstRowBytes destination row length ##
1761#Param srcX column index whose absolute value is less than width() ##
1762#Param srcY row index whose absolute value is less than height() ##
1763
1764#Return true if pixels are copied to dstPixels ##
1765
1766#Example
1767#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -04001768void draw(SkCanvas* canvas) {
1769 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
1770 std::vector<int32_t> srcPixels;
1771 const int rowBytes = image->width() * 4;
1772 srcPixels.resize(image->height() * rowBytes);
1773 SkPixmap pixmap(info, (const void*) &srcPixels.front(), rowBytes);
1774 image->readPixels(pixmap, 0, 0);
1775 for (int offset : { 32, 64, 96 } ) {
1776 std::vector<int32_t> dstPixels;
1777 dstPixels.resize(image->height() * rowBytes);
1778 pixmap.readPixels(info, &dstPixels.front(), rowBytes, offset, 0);
1779 SkBitmap bitmap;
1780 SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
1781 bitmap.installPixels(dstmap);
1782 canvas->translate(32, 32);
1783 canvas->drawBitmap(bitmap, 0, 0);
1784 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001785}
1786##
1787
1788#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1789
1790##
1791
1792# ------------------------------------------------------------------------------
1793
1794#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY) const
1795
1796Copies a Rect of pixels to dst. Copy starts at (srcX, srcY), and does not
1797exceed (this->width(), this->height()). dst specifies width, height, Color_Type,
1798Alpha_Type, and Color_Space of destination. Returns true if pixels are copied.
1799Returns false if dst.addr() equals nullptr, or dst.rowBytes is less than
1800dst SkImageInfo::minRowBytes.
1801
1802Pixels are copied only if pixel conversion is possible. If this->colorType is
1803kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.info().colorType must match.
1804If this->colorType is kGray_8_SkColorType, dst.info().colorSpace must match.
1805If this->alphaType is kOpaque_SkAlphaType, dst.info().alphaType must
1806match. If this->colorSpace is nullptr, dst.info().colorSpace must match. Returns
1807false if pixel conversion is not possible.
1808
1809srcX and srcY may be negative to copy only top or left of source. Returns
1810false this->width() or this->height() is zero or negative. Returns false if
1811#Formula
Cary Clark6fc50412017-09-21 12:31:06 -04001812abs(srcX) >= this->width()
1813##
1814, or if
1815#Formula
1816abs(srcY) >= this->height()
Cary Clarkd0530ba2017-09-14 11:25:39 -04001817##
1818.
1819
1820#Param dst Image_Info and pixel address to write to ##
1821#Param srcX column index whose absolute value is less than width() ##
1822#Param srcY row index whose absolute value is less than height() ##
1823
1824#Return true if pixels are copied to dst ##
1825
1826#Example
1827#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -04001828void draw(SkCanvas* canvas) {
1829 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
1830 std::vector<int32_t> srcPixels;
1831 const int rowBytes = image->width() * 4;
1832 srcPixels.resize(image->height() * rowBytes);
1833 SkPixmap pixmap(info, (const void*) &srcPixels.front(), rowBytes);
1834 image->readPixels(pixmap, 0, 0);
1835 for (int offset : { 32, 64, 96 } ) {
1836 std::vector<int32_t> dstPixels;
1837 dstPixels.resize(image->height() * rowBytes);
1838 SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
1839 pixmap.readPixels(dstmap, offset, 0);
1840 SkBitmap bitmap;
1841 bitmap.installPixels(dstmap);
1842 canvas->translate(32, 32);
1843 canvas->drawBitmap(bitmap, 0, 0);
1844 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001845}
1846##
1847
1848#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1849
1850##
1851
1852# ------------------------------------------------------------------------------
1853
1854#Method bool readPixels(const SkPixmap& dst) const
1855
1856Copies pixels inside bounds() to dst. dst specifies width, height, Color_Type,
1857Alpha_Type, and Color_Space of destination. Returns true if pixels are copied.
1858Returns false if dst.addr() equals nullptr, or dst.rowBytes is less than
1859dst SkImageInfo::minRowBytes.
1860
1861Pixels are copied only if pixel conversion is possible. If this->colorType is
1862kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
1863If this->colorType is kGray_8_SkColorType, dst Color_Space must match.
1864If this->alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
1865match. If this->colorSpace is nullptr, dst Color_Space must match. Returns
1866false if pixel conversion is not possible.
1867
1868Returns false if this->width() or this->height() is zero or negative.
1869
1870#Param dst Image_Info and pixel address to write to ##
1871
1872#Return true if pixels are copied to dst ##
1873
1874#Example
1875#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -04001876void draw(SkCanvas* canvas) {
1877 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
1878 std::vector<int32_t> srcPixels;
1879 const int rowBytes = image->width() * 4;
1880 srcPixels.resize(image->height() * rowBytes);
1881 SkPixmap pixmap(info, (const void*) &srcPixels.front(), rowBytes);
1882 image->readPixels(pixmap, 0, 0);
1883 for (int index = 0; index < 3; ++index ) {
1884 std::vector<int32_t> dstPixels;
1885 dstPixels.resize(image->height() * rowBytes);
1886 SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
1887 pixmap.readPixels(dstmap);
1888 SkBitmap bitmap;
1889 bitmap.installPixels(dstmap);
1890 canvas->translate(32, 32);
1891 canvas->drawBitmap(bitmap, 0, 0);
1892 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001893}
1894##
1895
1896#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1897
1898##
1899
1900# ------------------------------------------------------------------------------
1901
1902#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality) const
1903
1904Copies this to dst, scaling pixels to fit dst.width() and dst.height(), and
1905converting pixels to match dst.colorType and dst.alphaType. Returns true if
1906pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1907less than dst SkImageInfo::minRowBytes.
1908
1909Pixels are copied only if pixel conversion is possible. If this->colorType is
1910kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
1911If this->colorType is kGray_8_SkColorType, dst Color_Space must match.
1912If this->alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
1913match. If this->colorSpace is nullptr, dst Color_Space must match. Returns
1914false if pixel conversion is not possible.
1915
1916Returns false if this->width() or this->height() is zero or negative.
1917
1918Scales the image, with filterQuality, to match dst.width() and dst.height().
1919filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1920Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1921Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1922Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1923kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1924
1925#Param dst Image_Info and pixel address to write to ##
1926#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1927 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1928##
1929
1930#Return true if pixels are copied to dst ##
1931
1932#Example
1933#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -04001934void draw(SkCanvas* canvas) {
1935 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
1936 std::vector<int32_t> srcPixels;
1937 int rowBytes = image->width() * 4;
1938 srcPixels.resize(image->height() * rowBytes);
1939 SkPixmap pixmap(info, (const void*) &srcPixels.front(), rowBytes);
1940 image->readPixels(pixmap, 0, 0);
1941 for (int offset : { 32, 64, 96 } ) {
1942 info = SkImageInfo::MakeN32Premul(image->width() + offset, image->height());
1943 rowBytes = info.width() * 4;
1944 std::vector<int32_t> dstPixels;
1945 dstPixels.resize(image->height() * rowBytes);
1946 SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
1947 pixmap.scalePixels(dstmap, kMedium_SkFilterQuality);
1948 SkBitmap bitmap;
1949 bitmap.installPixels(dstmap);
1950 canvas->translate(32, 32);
1951 canvas->drawBitmap(bitmap, 0, 0);
1952 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001953}
1954##
1955
1956#SeeAlso SkCanvas::drawBitmap SkImage::scalePixels
1957
1958##
1959
1960# ------------------------------------------------------------------------------
1961
1962#Method bool erase(SkColor color, const SkIRect& subset) const
1963
1964Writes color to pixels bounded by subset; returns true on success.
1965Returns false if colorType is kUnknown_SkColorType, or if subset does
1966not intersect bounds().
1967
1968#Param color Unpremultiplied Color to write ##
1969#Param subset bounding integer Rect of written pixels ##
1970
1971#Return true if pixels are changed ##
1972
1973#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001974 uint32_t storage[2];
1975 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 2);
1976 SkPixmap pixmap(info, storage, info.minRowBytes());
1977 pixmap.erase(SK_ColorBLUE, {0, 0, 1, 1});
1978 pixmap.erase(SK_ColorRED, {0, 1, 1, 2});
1979 SkBitmap bitmap;
1980 canvas->scale(20, 20);
1981 bitmap.installPixels(pixmap);
1982 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001983##
1984
1985#SeeAlso SkBitmap::erase SkCanvas::clear SkCanvas::drawColor
1986
1987##
1988
1989# ------------------------------------------------------------------------------
1990
1991#Method bool erase(SkColor color) const
1992
1993Writes color to pixels inside bounds(); returns true on success.
1994Returns false if colorType is kUnknown_SkColorType, or if bounds()
1995is empty.
1996
1997#Param color Unpremultiplied Color to write ##
1998
1999#Return true if pixels are changed ##
2000
2001#Example
Cary Clark6fc50412017-09-21 12:31:06 -04002002 uint32_t storage[2];
2003 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 2);
2004 SkPixmap pixmap(info, storage, info.minRowBytes());
2005 pixmap.erase(SK_ColorBLUE);
2006 SkBitmap bitmap;
2007 canvas->scale(20, 20);
2008 bitmap.installPixels(pixmap);
2009 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04002010##
2011
2012#SeeAlso SkBitmap::erase SkCanvas::clear SkCanvas::drawColor
2013
2014##
2015
2016# ------------------------------------------------------------------------------
2017
2018#Method bool erase(const SkColor4f& color, const SkIRect* subset = nullptr) const
2019
2020Writes color to pixels bounded by subset; returns true on success.
2021if subset is nullptr, writes colors pixels inside bounds(). Returns false if
2022colorType is kUnknown_SkColorType, if subset is not nullptr and does
2023not intersect bounds(), or if subset is nullptr and bounds() is empty.
2024
2025#Param color Unpremultiplied Color to write ##
2026#Param subset bounding integer Rect of pixels to write; may be nullptr ##
2027
2028#Return true if pixels are changed ##
2029
2030#Example
Cary Clark6fc50412017-09-21 12:31:06 -04002031 uint32_t storage[2];
2032 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 2);
2033 SkPixmap pixmap(info, storage, info.minRowBytes());
2034 SkIRect topPixelBounds = {0, 0, 1, 1};
2035 pixmap.erase({ 0.65f, 0.45f, 0.25f, 1 }, &topPixelBounds);
2036 SkIRect bottomPixelBounds = {0, 1, 1, 2};
2037 pixmap.erase({ 0.25f, 0.65f, 0.45f, 1 }, &bottomPixelBounds);
2038 SkBitmap bitmap;
2039 canvas->scale(20, 20);
2040 bitmap.installPixels(pixmap);
2041 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04002042##
2043
2044#SeeAlso SkBitmap::erase SkCanvas::clear SkCanvas::drawColor
2045
2046##
2047
2048
2049#Subtopic Writer ##
2050
2051#Class SkPixmap ##
2052
2053#Topic Pixmap ##