blob: c87e7ba0c84820a2d36c7f3ec0de292fb8928e1f [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
Cary Clark5081eed2018-01-22 07:55:48 -050022# name # description ##
Cary Clarkd0530ba2017-09-14 11:25:39 -040023#Legend ##
Cary Clark5081eed2018-01-22 07:55:48 -050024# Constructors # list of functions that construct SkPath ##
25# Member_Functions # list of static functions and member methods ##
26# Related_Functions # similar methods grouped together ##
27#Table ##
28##
29
30#Subtopic Related_Functions
31#Table
32#Legend
33# name # description ##
34#Legend ##
35# Image_Info_Access # returns all or part of Image_Info ##
36# Initialization # sets fields for use ##
37# Readable_Address # returns read only pixels ##
38# Reader # examine pixel value ##
39# Writable_Address # returns writable pixels ##
40# Writer # copy to pixel values ##
Cary Clarkd0530ba2017-09-14 11:25:39 -040041#Table ##
42#Subtopic ##
43
44#Subtopic Constructors
45#Table
46#Legend
47# # description ##
48#Legend ##
Cary Clark5081eed2018-01-22 07:55:48 -050049# SkPixmap() # constructs with default values ##
50# SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes) # constructs from Image_Info, pixels ##
Cary Clarkd0530ba2017-09-14 11:25:39 -040051#Table ##
52#Subtopic ##
53
54#Subtopic Member_Functions
55#Table
56#Legend
Cary Clark5081eed2018-01-22 07:55:48 -050057# name # description ##
Cary Clarkd0530ba2017-09-14 11:25:39 -040058#Legend ##
Cary Clark5081eed2018-01-22 07:55:48 -050059# addr() # returns readable pixel address as void pointer ##
60# addr16 # returns readable pixel address as 16-bit pointer ##
61# addr32 # returns readable pixel address as 32-bit pointer ##
62# addr64 # returns readable pixel address as 64-bit pointer ##
63# addr8 # returns readable pixel address as 8-bit pointer ##
64# addrF16 # returns readable pixel component address as 16-bit pointer ##
65# alphaType # returns Image_Info Alpha_Type ##
66# bounds() # returns width and height as Rectangle ##
67# colorSpace # returns Image_Info Color_Space ##
68# colorType # returns Image_Info Color_Type ##
69# computeByteSize # returns size required for pixels ##
70# computeIsOpaque # returns true if all pixels are opaque ##
71# erase() # writes Color to pixels ##
72# extractSubset # sets pointer to portion of original ##
73# getColor # returns one pixel as Unpremultiplied Color ##
74# height() # returns pixel row count ##
75# info() # returns Image_Info ##
76# isOpaque # returns true if Image_Info describes opaque pixels ##
77# readPixels # copies and converts pixels ##
78# reset() # reuses existing Pixmap with replacement values ##
79# rowBytes # returns interval between rows in bytes ##
80# rowBytesAsPixels # returns interval between rows in pixels ##
81# scalePixels # scales and converts pixels ##
82# setColorSpace # sets Image_Info Color_Space ##
83# shiftPerPixel # returns bit shift from pixels to bytes ##
84# width() # returns pixel column count ##
85# writable_addr # returns writable pixel address as void pointer ##
86# writable_addr16 # returns writable pixel address as 16-bit pointer ##
87# writable_addr32 # returns writable pixel address as 32-bit pointer ##
88# writable_addr64 # returns writable pixel address as 64-bit pointer ##
89# writable_addr8 # returns writable pixel address as 8-bit pointer ##
90# writable_addrF16 # returns writable pixel component address as 16-bit pointer ##
Cary Clarkd0530ba2017-09-14 11:25:39 -040091#Table ##
92#Subtopic ##
93
94#Topic Overview ##
95
96#Subtopic Initialization
97
98# ------------------------------------------------------------------------------
99
100#Method SkPixmap()
101
Cary Clarkab2621d2018-01-30 10:08:57 -0500102#In Initialization
103#Line # constructs with default values ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400104Creates an empty Pixmap without pixels, with kUnknown_SkColorType, with
105kUnknown_SkAlphaType, and with a width and height of zero. Use
106reset() to associate pixels, SkColorType, SkAlphaType, width, and height
107after Pixmap has been created.
108
109#Return empty Pixmap ##
110
111#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400112void draw(SkCanvas* canvas) {
113 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500114 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
115 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clark6fc50412017-09-21 12:31:06 -0400116 SkPixmap pixmap;
117 for (int i = 0; i < 2; ++i) {
118 SkDebugf("width: %2d height: %2d", pixmap.width(), pixmap.height());
119 SkDebugf(" color: k%s_SkColorType", colors[pixmap.colorType()]);
120 SkDebugf(" alpha: k%s_SkAlphaType\n", alphas[pixmap.alphaType()]);
121 pixmap.reset(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType),
122 nullptr, 0);
123 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400124}
125#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400126width: 0 height: 0 color: kUnknown_SkColorType alpha: kUnknown_SkAlphaType
Cary Clarkd0530ba2017-09-14 11:25:39 -0400127width: 25 height: 35 color: kRGBA_8888_SkColorType alpha: kOpaque_SkAlphaType
128##
129##
130
131#SeeAlso SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes) reset() SkAlphaType SkColorType
132
133##
134
135# ------------------------------------------------------------------------------
136
137#Method SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes)
138
Cary Clarkab2621d2018-01-30 10:08:57 -0500139#In Initialization
140#Line # constructs from Image_Info, pixels ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400141Creates Pixmap from info width, height, SkAlphaType, and SkColorType.
Cary Clark6fc50412017-09-21 12:31:06 -0400142addr points to pixels, or nullptr. rowBytes should be info.width() times
143info.bytesPerPixel(), or larger.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400144
145No parameter checking is performed; it is up to the caller to ensure that
146addr and rowBytes agree with info.
147
Cary Clark6fc50412017-09-21 12:31:06 -0400148The memory lifetime of pixels is managed by the caller. When Pixmap goes
Cary Clarkd0530ba2017-09-14 11:25:39 -0400149out of scope, addr is unaffected.
150
151Pixmap may be later modified by reset() to change its size, pixel type, or
152storage.
153
154#Param info width, height, SkAlphaType, SkColorType of Image_Info ##
155#Param addr pointer to pixels allocated by caller; may be nullptr ##
156#Param rowBytes size of one row of addr; width times pixel size, or larger ##
157
158#Return initialized Pixmap ##
159
160#Example
161#Image 3
162#Description
163SkImage::MakeRasterCopy takes const SkPixmap& as an argument. The example
164constructs a SkPixmap from the brace-delimited parameters.
165##
Cary Clark6fc50412017-09-21 12:31:06 -0400166 SkDebugf("image alpha only = %s\n", image->isAlphaOnly() ? "true" : "false");
167 SkPMColor pmColors = 0;
168 sk_sp<SkImage> copy = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1),
169 (uint8_t*)&pmColors,
170 1});
Cary Clarkd0530ba2017-09-14 11:25:39 -0400171 SkDebugf("copy alpha only = %s\n", copy->isAlphaOnly() ? "true" : "false");
172#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400173image alpha only = false
Cary Clarkd0530ba2017-09-14 11:25:39 -0400174copy alpha only = true
175##
176##
177
178#SeeAlso SkPixmap() reset() SkAlphaType SkColorType
179
180##
181
182# ------------------------------------------------------------------------------
183
184#Method void reset()
185
Cary Clarkab2621d2018-01-30 10:08:57 -0500186#In Initialization
187#Line # reuses existing Pixmap with replacement values ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400188Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to
189kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType.
190
191The prior pixels are unaffected; it is up to the caller to release pixels
192memory if desired.
193
194#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400195void draw(SkCanvas* canvas) {
196 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500197 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
198 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clark6fc50412017-09-21 12:31:06 -0400199 SkPixmap pixmap(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType),
200 nullptr, 0);
201 for (int i = 0; i < 2; ++i) {
202 SkDebugf("width: %2d height: %2d", pixmap.width(), pixmap.height());
203 SkDebugf(" color: k%s_SkColorType", colors[pixmap.colorType()]);
204 SkDebugf(" alpha: k%s_SkAlphaType\n", alphas[pixmap.alphaType()]);
205 pixmap.reset();
206 }
207}
Cary Clarkd0530ba2017-09-14 11:25:39 -0400208#StdOut
209width: 25 height: 35 color: kRGBA_8888_SkColorType alpha: kOpaque_SkAlphaType
Cary Clark6fc50412017-09-21 12:31:06 -0400210width: 0 height: 0 color: kUnknown_SkColorType alpha: kUnknown_SkAlphaType
Cary Clarkd0530ba2017-09-14 11:25:39 -0400211##
212##
213
214#SeeAlso SkPixmap() SkAlphaType SkColorType
215
216##
217
218# ------------------------------------------------------------------------------
219
220#Method void reset(const SkImageInfo& info, const void* addr, size_t rowBytes)
221
Cary Clarkab2621d2018-01-30 10:08:57 -0500222#In Initialization
Cary Clarkd0530ba2017-09-14 11:25:39 -0400223Sets width, height, SkAlphaType, and SkColorType from info.
224Sets pixel address from addr, which may be nullptr.
Cary Clark6fc50412017-09-21 12:31:06 -0400225Sets row bytes from rowBytes, which should be info.width() times
226info.bytesPerPixel(), or larger.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400227
228Does not check addr. Asserts if built with SK_DEBUG defined and if rowBytes is
229too small to hold one row of pixels.
230
231The memory lifetime pixels are managed by the caller. When Pixmap goes
232out of scope, addr is unaffected.
233
234#Param info width, height, SkAlphaType, SkColorType of Image_Info ##
235#Param addr pointer to pixels allocated by caller; may be nullptr ##
236#Param rowBytes size of one row of addr; width times pixel size, or larger ##
237
238#Example
239#Image 4
Cary Clark2ade9972017-11-02 17:49:34 -0400240#Height 64
Cary Clark6fc50412017-09-21 12:31:06 -0400241void draw(SkCanvas* canvas) {
242 std::vector<int32_t> pixels;
243 pixels.resize(image->height() * image->width() * 4);
244 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
245 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
246 image->readPixels(pixmap, 0, 0);
247 int x = 0;
248 for (auto colorType : { kRGBA_8888_SkColorType, kBGRA_8888_SkColorType } ) {
249 pixmap.reset(SkImageInfo::Make(image->width(), image->height(), colorType,
250 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
251 SkBitmap bitmap;
252 bitmap.installPixels(pixmap);
253 canvas->drawBitmap(bitmap, x, 0);
254 x += 128;
255 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400256}
257##
258
259#SeeAlso SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes) reset() SkAlphaType SkColorType
260
261##
262
263# ------------------------------------------------------------------------------
264
265#Method void setColorSpace(sk_sp<SkColorSpace> colorSpace)
266
Cary Clarkab2621d2018-01-30 10:08:57 -0500267#In Initialization
268#Line # sets Image_Info Color_Space ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400269
270Changes Color_Space in Image_Info; preserves width, height, SkAlphaType, and
271SkColorType in Image, and leaves pixel address and row bytes unchanged.
Cary Clark6fc50412017-09-21 12:31:06 -0400272Color_Space reference count is incremented.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400273
274#Param colorSpace Color_Space moved to Image_Info ##
275
276#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400277void draw(SkCanvas* canvas) {
278 SkPixmap pixmap;
279 sk_sp<SkColorSpace> colorSpace1 = SkColorSpace::MakeRGB(SkColorSpace::kLinear_RenderTargetGamma,
280 SkColorSpace::kRec2020_Gamut);
281 SkDebugf("is %sunique\n", colorSpace1->unique() ? "" : "not ");
282 pixmap.setColorSpace(colorSpace1);
283 SkDebugf("is %sunique\n", colorSpace1->unique() ? "" : "not ");
Cary Clarkd0530ba2017-09-14 11:25:39 -0400284}
285#StdOut
286is unique
287is not unique
288##
289##
290
291#SeeAlso Color_Space SkImageInfo::makeColorSpace
292
293##
294
295# ------------------------------------------------------------------------------
296
297#Method bool SK_WARN_UNUSED_RESULT reset(const SkMask& mask)
298
Cary Clarkab2621d2018-01-30 10:08:57 -0500299#In Initialization
Cary Clarkd0530ba2017-09-14 11:25:39 -0400300Sets width, height, pixel address, and row bytes to Mask properties, if Mask
301format is SkMask::kA8_Format; and returns true. Otherwise sets width, height,
302row bytes to zero; pixel address to nullptr; SkColorType to kUnknown_SkColorType;
303and SkAlphaType to kUnknown_SkAlphaType; and returns false.
304
305Failing to read the return value generates a compile time warning.
306
307#Param mask Mask containing pixels and dimensions ##
308
309#Return true if set to Mask properties ##
310
311#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400312 const int width = 2;
313 const int height = 2;
314 uint8_t bytes[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
315 SkMask mask;
316 mask.fFormat = SkMask::kA8_Format;
317 mask.fBounds = {0, 0, width, height};
318 mask.fImage = bytes;
319 mask.fRowBytes = (width + 7) >> 3;
320 SkPixmap pixmap;
321 bool success = pixmap.reset(mask);
322 SkDebugf("success: %s width: %d height: %d\n", success ? "true " : "false",
323 pixmap.width(), pixmap.height());
324 mask.fFormat = SkMask::kBW_Format;
325 success = pixmap.reset(mask);
326 SkDebugf("success: %s width: %d height: %d\n", success ? "true " : "false",
327 pixmap.width(), pixmap.height());
328#StdOut
329success: true width: 2 height: 2
330success: false width: 0 height: 0
331##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400332##
333
334#SeeAlso Mask reset()
335
336##
337
338# ------------------------------------------------------------------------------
339
340#Method bool SK_WARN_UNUSED_RESULT extractSubset(SkPixmap* subset, const SkIRect& area) const
341
Cary Clarkab2621d2018-01-30 10:08:57 -0500342#In Initialization
343#Line # sets pointer to portion of original ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400344Sets subset width, height, pixel address to intersection of Pixmap with area,
345if intersection is not empty; and return true. Otherwise, leave subset unchanged
346and return false.
347
348Failing to read the return value generates a compile time warning.
349
350#Param subset storage for width, height, pixel address of intersection ##
351#Param area bounds to intersect with Pixmap ##
352
353#Return true if intersection of Pixmap and area is not empty ##
354
355#Example
356#Image 3
357#Height 128
Cary Clark6fc50412017-09-21 12:31:06 -0400358void draw(SkCanvas* canvas) {
359 std::vector<int32_t> pixels;
360 pixels.resize(image->height() * image->width() * 4);
361 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
362 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
363 image->readPixels(pixmap, 0, 0);
364 SkPixmap inset;
365 if (pixmap.extractSubset(&inset, {128, 128, 512, 512})) {
366 SkBitmap bitmap;
367 bitmap.installPixels(inset);
368 canvas->drawBitmap(bitmap, 0, 0);
369 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400370}
371##
372
373#SeeAlso reset() SkIRect::intersect
374
375##
376
377#Subtopic Initialization ##
378
379#Subtopic Image_Info_Access
380
381# ------------------------------------------------------------------------------
382
383#Method const SkImageInfo& info() const
384
Cary Clarkab2621d2018-01-30 10:08:57 -0500385#In Image_Info_Access
386#Line # returns Image_Info ##
Cary Clark6fc50412017-09-21 12:31:06 -0400387Returns width, height, Alpha_Type, Color_Type, and Color_Space.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400388
389#Return reference to ImageInfo ##
390
391#Example
392#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -0400393 std::vector<int32_t> pixels;
394 pixels.resize(image->height() * image->width() * 4);
395 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
396 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
397 image->readPixels(pixmap, 0, 0);
398 SkPixmap inset;
399 if (pixmap.extractSubset(&inset, {128, 128, 512, 512})) {
400 const SkImageInfo& info = inset.info();
401 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500402 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888",
403 "RGB_888x", "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clark6fc50412017-09-21 12:31:06 -0400404 SkDebugf("width: %d height: %d color: %s alpha: %s\n", info.width(), info.height(),
405 colors[info.colorType()], alphas[info.alphaType()]);
406 }
407#StdOut
408width: 384 height: 384 color: BGRA_8888 alpha: Opaque
409##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400410##
411
412#SeeAlso Image_Info
413
414##
415
416# ------------------------------------------------------------------------------
417
418#Method size_t rowBytes() const
419
Cary Clarkab2621d2018-01-30 10:08:57 -0500420#In Image_Info_Access
421#Line # returns interval between rows in bytes ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400422Returns row bytes, the interval from one pixel row to the next. Row bytes
Cary Clark154beea2017-10-26 07:58:48 -0400423is at least as large as:
Cary Clarkd0530ba2017-09-14 11:25:39 -0400424#Formula
425width() * info().bytesPerPixel()
426##
427.
428
Cary Clarkbc5697d2017-10-04 14:31:33 -0400429Returns zero if colorType is kUnknown_SkColorType.
430It is up to the Bitmap creator to ensure that row bytes is a useful value.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400431
432#Return byte length of pixel row ##
433
434#Example
435SkPixmap badPixmap = {SkImageInfo::MakeA8(4, 4), nullptr, 2};
436SkPixmap okPixmap = {SkImageInfo::MakeA8(4, 4), nullptr, 8};
437for (auto& pixmap : { badPixmap, okPixmap } ) {
438 SkDebugf("rowBytes: %d minRowBytes: %d\n", pixmap.rowBytes(),
439 pixmap.info().minRowBytes());
440}
441#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400442rowBytes: 2 minRowBytes: 4
Cary Clarkd0530ba2017-09-14 11:25:39 -0400443rowBytes: 8 minRowBytes: 4
444##
445##
446
447#SeeAlso addr() info() SkImageInfo::minRowBytes
448
449##
450
451# ------------------------------------------------------------------------------
452
453#Method const void* addr() const
454
Cary Clarkab2621d2018-01-30 10:08:57 -0500455#In Image_Info_Access
456#Line # returns readable pixel address as void pointer ##
Cary Clark6fc50412017-09-21 12:31:06 -0400457Returns pixel address, the base address corresponding to the pixel origin.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400458
459It is up to the Pixmap creator to ensure that pixel address is a useful value.
460
461#Return pixel address ##
462
463#Example
464#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -0400465 std::vector<int32_t> pixels;
466 pixels.resize(image->height() * image->width() * 4);
467 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
468 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
469 image->readPixels(pixmap, 0, 0);
470 SkDebugf("pixels address: 0x%llx\n", pixmap.addr());
471 SkPixmap inset;
472 if (pixmap.extractSubset(&inset, {128, 128, 512, 512})) {
473 SkDebugf("inset address: 0x%llx\n", inset.addr());
474 }
475#StdOut
476#Volatile
477pixels address: 0x7f2a440bb010
478inset address: 0x7f2a440fb210
479##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400480##
481
482#SeeAlso addr(int x, int y) addr8 addr16 addr32 addr64 info() rowBytes()
483
484##
485
486# ------------------------------------------------------------------------------
487
488#Method int width() const
489
Cary Clarkab2621d2018-01-30 10:08:57 -0500490#In Image_Info_Access
491#Line # returns pixel column count ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400492Returns pixel count in each pixel row. Should be equal or less than:
Cary Clark154beea2017-10-26 07:58:48 -0400493
Cary Clarkd0530ba2017-09-14 11:25:39 -0400494#Formula
Cary Clarkbc5697d2017-10-04 14:31:33 -0400495rowBytes() / info().bytesPerPixel()
Cary Clarkd0530ba2017-09-14 11:25:39 -0400496##
497.
498
499#Return pixel width in Image_Info ##
500
501#Example
502 SkImageInfo info = SkImageInfo::MakeA8(16, 32);
Cary Clark6fc50412017-09-21 12:31:06 -0400503 SkPixmap pixmap(info, nullptr, 64);
504 SkDebugf("pixmap width: %d info width: %d\n", pixmap.width(), info.width());
505#StdOut
506pixmap width: 16 info width: 16
507##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400508##
509
Cary Clarkbc5697d2017-10-04 14:31:33 -0400510#SeeAlso height() SkImageInfo::width()
Cary Clarkd0530ba2017-09-14 11:25:39 -0400511
512##
513
514# ------------------------------------------------------------------------------
515
516#Method int height() const
517
Cary Clarkab2621d2018-01-30 10:08:57 -0500518#In Image_Info_Access
519#Line # returns pixel row count ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400520Returns pixel row count.
521
522#Return pixel height in Image_Info ##
523
524#Example
Cary Clarkbc5697d2017-10-04 14:31:33 -0400525 SkPixmap pixmap(SkImageInfo::MakeA8(16, 32), nullptr, 64);
526 SkDebugf("pixmap height: %d info height: %d\n", pixmap.height(), pixmap.info().height());
Cary Clark6fc50412017-09-21 12:31:06 -0400527#StdOut
528pixmap height: 32 info height: 32
529##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400530##
531
Cary Clarkbc5697d2017-10-04 14:31:33 -0400532#SeeAlso width() ImageInfo::height()
Cary Clarkd0530ba2017-09-14 11:25:39 -0400533
534##
535
536# ------------------------------------------------------------------------------
537
538#Method SkColorType colorType() const
539
Cary Clarkab2621d2018-01-30 10:08:57 -0500540#In Image_Info_Access
541#Line # returns Image_Info Color_Type ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400542Returns Color_Type, one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
543kRGB_565_SkColorType, kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
Cary Clarkab2621d2018-01-30 10:08:57 -0500544kRGB_888x_SkColorType, kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType,
545kRGB_101010x_SkColorType, kGray_8_SkColorType, kRGBA_F16_SkColorType.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400546
547#Return Color_Type in Image_Info ##
548
549#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500550 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
551 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clark6fc50412017-09-21 12:31:06 -0400552 SkPixmap pixmap(SkImageInfo::MakeA8(16, 32), nullptr, 64);
553 SkDebugf("color type: k" "%s" "_SkColorType\n", colors[pixmap.colorType()]);
554#StdOut
Cary Clarkab2621d2018-01-30 10:08:57 -0500555color type: kAlpha_8_SkColorType
Cary Clark6fc50412017-09-21 12:31:06 -0400556##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400557##
558
Cary Clarkbc5697d2017-10-04 14:31:33 -0400559#SeeAlso alphaType() SkImageInfo::colorType
Cary Clarkd0530ba2017-09-14 11:25:39 -0400560
561##
562
563# ------------------------------------------------------------------------------
564
565#Method SkAlphaType alphaType() const
566
Cary Clarkab2621d2018-01-30 10:08:57 -0500567#In Image_Info_Access
568#Line # returns Image_Info Alpha_Type ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400569Returns Alpha_Type, one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
570kPremul_SkAlphaType, kUnpremul_SkAlphaType.
571
572#Return Alpha_Type in Image_Info ##
573
574#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400575 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
576 SkPixmap pixmap(SkImageInfo::MakeA8(16, 32), nullptr, 64);
577 SkDebugf("alpha type: k" "%s" "_SkAlphaType\n", alphas[pixmap.alphaType()]);
Cary Clarkd0530ba2017-09-14 11:25:39 -0400578#StdOut
579alpha type: kPremul_SkAlphaType
580##
581##
582
Cary Clarkbc5697d2017-10-04 14:31:33 -0400583#SeeAlso colorType() SkImageInfo::alphaType
Cary Clarkd0530ba2017-09-14 11:25:39 -0400584
585##
586
587# ------------------------------------------------------------------------------
588
589#Method SkColorSpace* colorSpace() const
590
Cary Clarkab2621d2018-01-30 10:08:57 -0500591#In Image_Info_Access
592#Line # returns Image_Info Color_Space ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400593Returns Color_Space associated with Image_Info. The
594reference count of Color_Space is unchanged. The returned Color_Space is
595immutable.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400596
Cary Clarkbc5697d2017-10-04 14:31:33 -0400597#Return Color_Space, the range of colors, in Image_Info ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400598
599#Example
600#Description
601SkColorSpace::MakeSRGBLinear creates Color_Space with linear gamma
602and an sRGB gamut. This Color_Space gamma is not close to sRGB gamma.
603##
Cary Clark6fc50412017-09-21 12:31:06 -0400604 SkPixmap pixmap(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
605 SkColorSpace::MakeSRGBLinear()), nullptr, 64);
606 SkColorSpace* colorSpace = pixmap.colorSpace();
607 SkDebugf("gammaCloseToSRGB: %s gammaIsLinear: %s isSRGB: %s\n",
608 colorSpace->gammaCloseToSRGB() ? "true" : "false",
609 colorSpace->gammaIsLinear() ? "true" : "false",
610 colorSpace->isSRGB() ? "true" : "false");
611#StdOut
612gammaCloseToSRGB: false gammaIsLinear: true isSRGB: false
613##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400614##
615
Cary Clarkbc5697d2017-10-04 14:31:33 -0400616#SeeAlso Color_Space SkImageInfo::colorSpace
Cary Clarkd0530ba2017-09-14 11:25:39 -0400617
618##
619
620# ------------------------------------------------------------------------------
621
622#Method bool isOpaque() const
623
Cary Clarkab2621d2018-01-30 10:08:57 -0500624#In Image_Info_Access
625#Line # returns true if Image_Info describes opaque pixels ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400626Returns true if Alpha_Type is kOpaque_SkAlphaType.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400627Does not check if Color_Type allows Alpha, or if any pixel value has
628transparency.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400629
630#Return true if Image_Info has opaque Alpha_Type ##
631
632#Example
633#Description
634 isOpaque ignores whether all pixels are opaque or not.
635##
Cary Clark6fc50412017-09-21 12:31:06 -0400636 std::vector<uint32_t> pixels;
637 const int height = 2;
638 const int width = 2;
639 pixels.resize(height * width * 4);
640 SkPixmap pixmap(SkImageInfo::Make(width, height, kN32_SkColorType,
641 kPremul_SkAlphaType), (const void*) &pixels.front(), width * 4);
642 for (int index = 0; index < 2; ++index) {
643 pixmap.erase(0x00000000);
644 SkDebugf("isOpaque: %s\n", pixmap.isOpaque() ? "true" : "false");
645 pixmap.erase(0xFFFFFFFF);
646 SkDebugf("isOpaque: %s\n", pixmap.isOpaque() ? "true" : "false");
647 pixmap.reset(pixmap.info().makeAlphaType(kOpaque_SkAlphaType),
648 (const void*) &pixels.front(), width * 4);
649 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400650#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400651isOpaque: false
652isOpaque: false
653isOpaque: true
Cary Clarkd0530ba2017-09-14 11:25:39 -0400654isOpaque: true
655##
656##
657
658#SeeAlso computeIsOpaque SkImageInfo::isOpaque
659
660##
661
662# ------------------------------------------------------------------------------
663
664#Method SkIRect bounds() const
665
Cary Clarkab2621d2018-01-30 10:08:57 -0500666#In Image_Info_Access
667#Line # returns width and height as Rectangle ##
Cary Clark154beea2017-10-26 07:58:48 -0400668Returns IRect { 0, 0, width(), height() }.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400669
670#Return integral rectangle from origin to width() and height() ##
671
672#Example
673 for (int width : { 0, 2 } ) {
674 for (int height : { 0, 2 } ) {
Cary Clark6fc50412017-09-21 12:31:06 -0400675 SkPixmap pixmap(SkImageInfo::MakeA8(width, height), nullptr, width);
Cary Clarkd0530ba2017-09-14 11:25:39 -0400676 SkDebugf("width: %d height: %d empty: %s\n", width, height,
677 pixmap.bounds().isEmpty() ? "true" : "false");
678 }
679 }
680#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400681width: 0 height: 0 empty: true
682width: 0 height: 2 empty: true
683width: 2 height: 0 empty: true
Cary Clarkd0530ba2017-09-14 11:25:39 -0400684width: 2 height: 2 empty: false
685##
686##
687
688#SeeAlso height() width() IRect
689
690##
691
692# ------------------------------------------------------------------------------
693
694#Method int rowBytesAsPixels() const
695
Cary Clarkab2621d2018-01-30 10:08:57 -0500696#In Image_Info_Access
697#Line # returns interval between rows in pixels ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400698
699Returns number of pixels that fit on row. Should be greater than or equal to
700width().
701
702#Return maximum pixels per row ##
703
704#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400705 for (int rowBytes : { 4, 5, 6, 7, 8} ) {
706 SkPixmap pixmap(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), nullptr, rowBytes);
707 SkDebugf("rowBytes: %d rowBytesAsPixels: %d\n", rowBytes, pixmap.rowBytesAsPixels());
708 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400709#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400710rowBytes: 4 rowBytesAsPixels: 1
711rowBytes: 5 rowBytesAsPixels: 1
712rowBytes: 6 rowBytesAsPixels: 1
713rowBytes: 7 rowBytesAsPixels: 1
Cary Clarkd0530ba2017-09-14 11:25:39 -0400714rowBytes: 8 rowBytesAsPixels: 2
715##
716##
717
718#SeeAlso rowBytes shiftPerPixel width SkImageInfo::bytesPerPixel
719
720##
721
722# ------------------------------------------------------------------------------
723
724#Method int shiftPerPixel() const
725
Cary Clarkab2621d2018-01-30 10:08:57 -0500726#In Image_Info_Access
727#Line # returns bit shift from pixels to bytes ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400728Returns bit shift converting row bytes to row pixels.
729Returns zero for kUnknown_SkColorType.
730
731#Return one of: 0, 1, 2, 3; left shift to convert pixels to bytes ##
732
733#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500734 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
735 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clark6fc50412017-09-21 12:31:06 -0400736 SkImageInfo info = SkImageInfo::MakeA8(1, 1);
737 for (SkColorType colorType : { kUnknown_SkColorType, kAlpha_8_SkColorType,
738 kRGB_565_SkColorType, kARGB_4444_SkColorType,
739 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
740 kGray_8_SkColorType, kRGBA_F16_SkColorType } ) {
741 SkPixmap pixmap(info.makeColorType(colorType), nullptr, 4);
742 SkDebugf("color: k" "%s" "_SkColorType" "%*s" "bytesPerPixel: %d shiftPerPixel: %d\n",
743 colors[colorType], 10 - strlen(colors[colorType]), " ",
744 pixmap.info().bytesPerPixel(), pixmap.shiftPerPixel());
745 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400746#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400747color: kUnknown_SkColorType bytesPerPixel: 0 shiftPerPixel: 0
Cary Clarkab2621d2018-01-30 10:08:57 -0500748color: kAlpha_8_SkColorType bytesPerPixel: 1 shiftPerPixel: 0
Cary Clark6fc50412017-09-21 12:31:06 -0400749color: kRGB_565_SkColorType bytesPerPixel: 2 shiftPerPixel: 1
750color: kARGB_4444_SkColorType bytesPerPixel: 2 shiftPerPixel: 1
751color: kRGBA_8888_SkColorType bytesPerPixel: 4 shiftPerPixel: 2
752color: kBGRA_8888_SkColorType bytesPerPixel: 4 shiftPerPixel: 2
753color: kGray_8_SkColorType bytesPerPixel: 1 shiftPerPixel: 0
Cary Clarkd0530ba2017-09-14 11:25:39 -0400754color: kRGBA_F16_SkColorType bytesPerPixel: 8 shiftPerPixel: 3
755##
756##
757
758#SeeAlso rowBytes rowBytesAsPixels width SkImageInfo::bytesPerPixel
759
760##
761
762# ------------------------------------------------------------------------------
763
Cary Clarkbc5697d2017-10-04 14:31:33 -0400764#Method size_t computeByteSize() const
765
Cary Clarkab2621d2018-01-30 10:08:57 -0500766#In Image_Info_Access
767#Line # returns size required for pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400768Returns minimum memory required for pixel storage.
769Does not include unused memory on last row when rowBytesAsPixels exceeds width().
770Returns zero if result does not fit in size_t.
771Returns zero if height() or width() is 0.
772Returns height() times rowBytes if colorType is kUnknown_SkColorType.
773
774#Return size in bytes of image buffer ##
775
Cary Clarkd0530ba2017-09-14 11:25:39 -0400776#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400777 SkPixmap pixmap;
778 for (int width : { 1, 1000, 1000000 } ) {
779 for (int height: { 1, 1000, 1000000 } ) {
780 SkImageInfo imageInfo = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
Cary Clarkbc5697d2017-10-04 14:31:33 -0400781 pixmap.reset(imageInfo, nullptr, width * 5);
782 SkDebugf("width: %7d height: %7d computeByteSize: %13lld\n", width, height,
783 pixmap.computeByteSize());
Cary Clark6fc50412017-09-21 12:31:06 -0400784 }
785 }
Cary Clark6fc50412017-09-21 12:31:06 -0400786#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400787width: 1 height: 1 computeByteSize: 4
788width: 1 height: 1000 computeByteSize: 4999
789width: 1 height: 1000000 computeByteSize: 4999999
790width: 1000 height: 1 computeByteSize: 4000
791width: 1000 height: 1000 computeByteSize: 4999000
792width: 1000 height: 1000000 computeByteSize: 4999999000
793width: 1000000 height: 1 computeByteSize: 4000000
794width: 1000000 height: 1000 computeByteSize: 4999000000
Cary Clarkbc5697d2017-10-04 14:31:33 -0400795width: 1000000 height: 1000000 computeByteSize: 4999999000000
Cary Clarkd0530ba2017-09-14 11:25:39 -0400796##
797##
798
Cary Clarkbc5697d2017-10-04 14:31:33 -0400799#SeeAlso SkImageInfo::computeByteSize
Cary Clarkd0530ba2017-09-14 11:25:39 -0400800
801##
802
803#Subtopic Image_Info_Access ##
804
805#Subtopic Reader
806
807# ------------------------------------------------------------------------------
808
809#Method bool computeIsOpaque() const
810
Cary Clarkab2621d2018-01-30 10:08:57 -0500811#In Reader
812#Line # returns true if all pixels are opaque ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400813Returns true if all pixels are opaque. Color_Type determines how pixels
814are encoded, and whether pixel describes Alpha. Returns true for Color_Types
Cary Clark6fc50412017-09-21 12:31:06 -0400815without alpha in each pixel; for other Color_Types, returns true if all
Cary Clarkd0530ba2017-09-14 11:25:39 -0400816pixels have alpha values equivalent to 1.0 or greater.
817
818For Color_Types kRGB_565_SkColorType or kGray_8_SkColorType: always
819returns true. For Color_Types kAlpha_8_SkColorType, kBGRA_8888_SkColorType,
820kRGBA_8888_SkColorType: returns true if all pixel Alpha values are 255.
821For Color_Type kARGB_4444_SkColorType: returns true if all pixel Alpha values are 15.
822For kRGBA_F16_SkColorType: returns true if all pixel Alpha values are 1.0 or
823greater.
824
Cary Clark6fc50412017-09-21 12:31:06 -0400825Returns false for kUnknown_SkColorType.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400826
Cary Clarkbc5697d2017-10-04 14:31:33 -0400827#Return true if all pixels have opaque values or Color_Type is opaque ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400828
829#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400830 std::vector<uint32_t> pixels;
831 const int height = 2;
832 const int width = 2;
833 pixels.resize(height * width * 4);
834 SkPixmap pixmap(SkImageInfo::Make(width, height, kN32_SkColorType,
835 kPremul_SkAlphaType), (const void*) &pixels.front(), width * 4);
836 for (int index = 0; index < 2; ++index) {
837 pixmap.erase(0x00000000);
838 SkDebugf("computeIsOpaque: %s\n", pixmap.computeIsOpaque() ? "true" : "false");
839 pixmap.erase(0xFFFFFFFF);
840 SkDebugf("computeIsOpaque: %s\n", pixmap.computeIsOpaque() ? "true" : "false");
841 pixmap.reset(pixmap.info().makeAlphaType(kOpaque_SkAlphaType),
842 (const void*) &pixels.front(), width * 4);
Cary Clarkd0530ba2017-09-14 11:25:39 -0400843 }
844#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400845computeIsOpaque: false
846computeIsOpaque: true
847computeIsOpaque: false
Cary Clarkd0530ba2017-09-14 11:25:39 -0400848computeIsOpaque: true
849##
850##
851
852#SeeAlso isOpaque Color_Type Alpha
853
854##
855
856# ------------------------------------------------------------------------------
857
858#Method SkColor getColor(int x, int y) const
859
Cary Clarkab2621d2018-01-30 10:08:57 -0500860#In Reader
861#Line # returns one pixel as Unpremultiplied Color ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400862Returns pixel at (x, y) as Unpremultiplied Color.
863Returns black with Alpha if Color_Type is kAlpha_8_SkColorType.
864
865Input is not validated: out of bounds values of x or y trigger an assert() if
866built with SK_DEBUG defined; and returns undefined values or may crash if
867SK_RELEASE is defined. Fails if Color_Type is kUnknown_SkColorType or
868pixel address is nullptr.
869
870Color_Space in Image_Info is ignored. Some Color precision may be lost in the
871conversion to Unpremultiplied Color; original pixel data may have additional
872precision.
873
Cary Clark6fc50412017-09-21 12:31:06 -0400874#Param x column index, zero or greater, and less than width() ##
875#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400876
877#Return pixel converted to Unpremultiplied Color ##
878
879#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400880 const int w = 4;
881 const int h = 4;
882 std::vector<SkPMColor> storage;
883 storage.resize(w * h);
884 SkDebugf("Premultiplied:\n");
885 for (int y = 0; y < h; ++y) {
886 SkDebugf("(0, %d) ", y);
887 for (int x = 0; x < w; ++x) {
888 int a = 0xFF * (x + y) / (w - 1 + h - 1);
889 storage[x + y * w] = SkPackARGB32(a, a * x / (w - 1), a * y / (h - 1), a);
890 SkDebugf("0x%08x%c", storage[x + y * w], x == w - 1 ? '\n' : ' ');
891 }
892 }
893 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), &storage.front(), w * 4);
894 SkDebugf("Unpremultiplied:\n");
895 for (int y = 0; y < h; ++y) {
896 SkDebugf("(0, %d) ", y);
897 for (int x = 0; x < w; ++x) {
898 SkDebugf("0x%08x%c", pixmap.getColor(x, y), x == w - 1 ? '\n' : ' ');
899 }
900 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400901#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400902Premultiplied:
903(0, 0) 0x00000000 0x2a0e002a 0x55380055 0x7f7f007f
904(0, 1) 0x2a000e2a 0x551c1c55 0x7f542a7f 0xaaaa38aa
905(0, 2) 0x55003855 0x7f2a547f 0xaa7171aa 0xd4d48dd4
906(0, 3) 0x7f007f7f 0xaa38aaaa 0xd48dd4d4 0xffffffff
907Unpremultiplied:
908(0, 0) 0x00000000 0x2a5500ff 0x55a800ff 0x7fff00ff
909(0, 1) 0x2a0055ff 0x555454ff 0x7fa954ff 0xaaff54ff
910(0, 2) 0x5500a8ff 0x7f54a9ff 0xaaaaaaff 0xd4ffaaff
Cary Clarkd0530ba2017-09-14 11:25:39 -0400911(0, 3) 0x7f00ffff 0xaa54ffff 0xd4aaffff 0xffffffff
912##
913##
914
915#SeeAlso addr() readPixels
916
917##
918
919#Subtopic Reader ##
920
921#Subtopic Readable_Address
922
923# ------------------------------------------------------------------------------
924
925#Method const void* addr(int x, int y) const
926
Cary Clarkab2621d2018-01-30 10:08:57 -0500927#In Readable_Address
Cary Clarkbc5697d2017-10-04 14:31:33 -0400928Returns readable pixel address at (x, y). Returns nullptr if Pixel_Ref is nullptr.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400929
930Input is not validated: out of bounds values of x or y trigger an assert() if
Cary Clarkbc5697d2017-10-04 14:31:33 -0400931built with SK_DEBUG defined. Returns nullptr if Color_Type is kUnknown_SkColorType.
932
933Performs a lookup of pixel size; for better performance, call
934one of: addr8, addr16, addr32, addr64, or addrF16.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400935
Cary Clark6fc50412017-09-21 12:31:06 -0400936#Param x column index, zero or greater, and less than width() ##
937#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400938
939#Return readable generic pointer to pixel ##
940
941#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400942 const int w = 4;
943 const int h = 4;
944 std::vector<SkPMColor> storage;
945 storage.resize(w * h);
946 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), &storage.front(), w * 4);
Cary Clarkd0530ba2017-09-14 11:25:39 -0400947 SkDebugf("pixmap.addr(1, 2) %c= &storage[1 + 2 * w]\n",
948 pixmap.addr(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
949#StdOut
950pixmap.addr(1, 2) == &storage[1 + 2 * w]
951##
952##
953
Cary Clarkbc5697d2017-10-04 14:31:33 -0400954#SeeAlso addr8 addr16 addr32 addr64 addrF16 getColor writable_addr SkBitmap::getAddr
Cary Clarkd0530ba2017-09-14 11:25:39 -0400955
956##
957
958# ------------------------------------------------------------------------------
959
960#Method const uint8_t* addr8() const
961
Cary Clarkab2621d2018-01-30 10:08:57 -0500962#In Readable_Address
963#Line # returns readable pixel address as 8-bit pointer ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400964Returns readable base pixel address. Result is addressable as unsigned 8-bit bytes.
965Will trigger an assert() if Color_Type is not kAlpha_8_SkColorType or
966kGray_8_SkColorType, and is built with SK_DEBUG defined.
967
968One byte corresponds to one pixel.
969
970#Return readable unsigned 8-bit pointer to pixels ##
971
972#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400973 const int w = 4;
974 const int h = 4;
975 uint8_t storage[w * h];
976 SkPixmap pixmap(SkImageInfo::Make(w, h, kGray_8_SkColorType, kPremul_SkAlphaType),
977 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -0400978 SkDebugf("pixmap.addr8() %c= storage\n",
979 pixmap.addr8() == storage ? '=' : '!');
980#StdOut
981pixmap.addr8() == storage
982##
983##
984
985#SeeAlso addr() addr16 addr32 addr64 addrF16 getColor writable_addr writable_addr8
986
987##
988
989# ------------------------------------------------------------------------------
990
991#Method const uint16_t* addr16() const
992
Cary Clarkab2621d2018-01-30 10:08:57 -0500993#In Readable_Address
994#Line # returns readable pixel address as 16-bit pointer ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400995Returns readable base pixel address. Result is addressable as unsigned 16-bit words.
996Will trigger an assert() if Color_Type is not kRGB_565_SkColorType or
997kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
998
999One word corresponds to one pixel.
1000
1001#Return readable unsigned 16-bit pointer to pixels ##
1002
1003#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001004 const int w = 4;
1005 const int h = 4;
1006 uint16_t storage[w * h];
1007 SkPixmap pixmap(SkImageInfo::Make(w, h, kARGB_4444_SkColorType, kPremul_SkAlphaType),
1008 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001009 SkDebugf("pixmap.addr16() %c= storage\n",
1010 pixmap.addr16() == storage ? '=' : '!');
1011#StdOut
1012pixmap.addr16() == storage
1013##
1014##
1015
1016#SeeAlso addr() addr8 addr32 addr64 addrF16 getColor writable_addr writable_addr16
1017
1018##
1019
1020# ------------------------------------------------------------------------------
1021
1022#Method const uint32_t* addr32() const
1023
Cary Clarkab2621d2018-01-30 10:08:57 -05001024#In Readable_Address
1025#Line # returns readable pixel address as 32-bit pointer ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001026Returns readable base pixel address. Result is addressable as unsigned 32-bit words.
1027Will trigger an assert() if Color_Type is not kRGBA_8888_SkColorType or
1028kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.
1029
1030One word corresponds to one pixel.
1031
1032#Return readable unsigned 32-bit pointer to pixels ##
1033
1034#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001035 const int w = 4;
1036 const int h = 4;
1037 uint32_t storage[w * h];
1038 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType),
1039 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001040 SkDebugf("pixmap.addr32() %c= storage\n",
1041 pixmap.addr32() == storage ? '=' : '!');
1042#StdOut
1043pixmap.addr32() == storage
1044##
1045##
1046
1047#SeeAlso addr() addr8 addr16 addr64 addrF16 getColor writable_addr writable_addr32
1048
1049##
1050
1051# ------------------------------------------------------------------------------
1052
1053#Method const uint64_t* addr64() const
1054
Cary Clarkab2621d2018-01-30 10:08:57 -05001055#In Readable_Address
1056#Line # returns readable pixel address as 64-bit pointer ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001057Returns readable base pixel address. Result is addressable as unsigned 64-bit words.
1058Will trigger an assert() if Color_Type is not kRGBA_F16_SkColorType and is built
1059with SK_DEBUG defined.
1060
1061One word corresponds to one pixel.
1062
1063#Return readable unsigned 64-bit pointer to pixels ##
1064
1065#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001066 const int w = 4;
1067 const int h = 4;
1068 uint64_t storage[w * h];
1069 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType),
1070 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001071 SkDebugf("pixmap.addr64() %c= storage\n",
1072 pixmap.addr64() == storage ? '=' : '!');
1073#StdOut
1074pixmap.addr64() == storage
1075##
1076##
1077
1078#SeeAlso addr() addr8 addr16 addr32 addrF16 getColor writable_addr writable_addr64
1079
1080##
1081
1082# ------------------------------------------------------------------------------
1083
1084#Method const uint16_t* addrF16() const
1085
Cary Clarkab2621d2018-01-30 10:08:57 -05001086#In Readable_Address
1087#Line # returns readable pixel component address as 16-bit pointer ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001088Returns readable base pixel address. Result is addressable as unsigned 16-bit words.
1089Will trigger an assert() if Color_Type is not kRGBA_F16_SkColorType and is built
1090with SK_DEBUG defined.
1091
1092Each word represents one color component encoded as a half float.
1093Four words correspond to one pixel.
1094
1095#Return readable unsigned 16-bit pointer to first component of pixels ##
1096
1097#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001098 const int w = 4;
1099 const int h = 4;
1100 uint16_t storage[w * h * 4];
1101 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType),
1102 storage, w * 4 * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001103 SkDebugf("pixmap.addrF16() %c= storage\n",
1104 pixmap.addrF16() == storage ? '=' : '!');
1105#StdOut
1106pixmap.addrF16() == storage
1107##
1108##
1109
1110#SeeAlso addr() addr8 addr16 addr32 addr64 getColor writable_addr writable_addrF16
1111
1112##
1113
1114# ------------------------------------------------------------------------------
1115
1116#Method const uint8_t* addr8(int x, int y) const
1117
Cary Clarkab2621d2018-01-30 10:08:57 -05001118#In Readable_Address
Cary Clarkd0530ba2017-09-14 11:25:39 -04001119Returns readable pixel address at (x, y).
1120
1121Input is not validated: out of bounds values of x or y trigger an assert() if
1122built with SK_DEBUG defined.
1123
1124Will trigger an assert() if Color_Type is not kAlpha_8_SkColorType or
1125kGray_8_SkColorType, and is built with SK_DEBUG defined.
1126
Cary Clark6fc50412017-09-21 12:31:06 -04001127#Param x column index, zero or greater, and less than width() ##
1128#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001129
1130#Return readable unsigned 8-bit pointer to pixel at (x, y) ##
1131
1132#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001133 const int w = 4;
1134 const int h = 4;
1135 uint8_t storage[w * h];
1136 SkPixmap pixmap(SkImageInfo::Make(w, h, kGray_8_SkColorType, kPremul_SkAlphaType),
1137 storage, w * sizeof(storage[0]));
1138 SkDebugf("pixmap.addr8(1, 2) %c= &storage[1 + 2 * w]\n",
1139 pixmap.addr8(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
Cary Clarkd0530ba2017-09-14 11:25:39 -04001140#StdOut
1141pixmap.addr8(1, 2) == &storage[1 + 2 * w]
1142##
1143##
1144
1145#SeeAlso addr() addr16 addr32 addr64 addrF16 getColor writable_addr writable_addr8
1146
1147##
1148
1149# ------------------------------------------------------------------------------
1150
1151#Method const uint16_t* addr16(int x, int y) const
1152
Cary Clarkab2621d2018-01-30 10:08:57 -05001153#In Readable_Address
Cary Clarkd0530ba2017-09-14 11:25:39 -04001154Returns readable pixel address at (x, y).
1155
1156Input is not validated: out of bounds values of x or y trigger an assert() if
1157built with SK_DEBUG defined.
1158
1159Will trigger an assert() if Color_Type is not kRGB_565_SkColorType or
1160kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
1161
Cary Clark6fc50412017-09-21 12:31:06 -04001162#Param x column index, zero or greater, and less than width() ##
1163#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001164
1165#Return readable unsigned 16-bit pointer to pixel at (x, y) ##
1166
1167#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001168 const int w = 4;
1169 const int h = 4;
1170 uint16_t storage[w * h];
1171 SkPixmap pixmap(SkImageInfo::Make(w, h, kARGB_4444_SkColorType, kPremul_SkAlphaType),
1172 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001173 SkDebugf("pixmap.addr16(1, 2) %c= &storage[1 + 2 * w]\n",
1174 pixmap.addr16(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
1175#StdOut
1176pixmap.addr16(1, 2) == &storage[1 + 2 * w]
1177##
1178##
1179
1180#SeeAlso addr() addr8 addr32 addr64 addrF16 getColor writable_addr writable_addr16
1181
1182##
1183
1184# ------------------------------------------------------------------------------
1185
1186#Method const uint32_t* addr32(int x, int y) const
1187
Cary Clarkab2621d2018-01-30 10:08:57 -05001188#In Readable_Address
Cary Clarkd0530ba2017-09-14 11:25:39 -04001189Returns 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_8888_SkColorType or
1195kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.
1196
Cary Clark6fc50412017-09-21 12:31:06 -04001197#Param x column index, zero or greater, and less than width() ##
1198#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001199
1200#Return readable unsigned 32-bit pointer to pixel at (x, y) ##
1201
1202#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001203 const int w = 4;
1204 const int h = 4;
1205 uint32_t storage[w * h];
1206 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
1207 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001208 SkDebugf("pixmap.addr32(1, 2) %c= &storage[1 + 2 * w]\n",
1209 pixmap.addr32(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
1210#StdOut
1211pixmap.addr32(1, 2) == &storage[1 + 2 * w]
1212##
1213##
1214
Cary Clark2ade9972017-11-02 17:49:34 -04001215#SeeAlso addr() addr8 addr16 addr64 addrF16 getColor writable_addr writable_addr64
Cary Clarkd0530ba2017-09-14 11:25:39 -04001216
1217##
1218
1219# ------------------------------------------------------------------------------
1220
1221#Method const uint64_t* addr64(int x, int y) const
1222
Cary Clarkab2621d2018-01-30 10:08:57 -05001223#In Readable_Address
Cary Clarkd0530ba2017-09-14 11:25:39 -04001224Returns readable pixel address at (x, y).
1225
1226Input is not validated: out of bounds values of x or y trigger an assert() if
1227built with SK_DEBUG defined.
1228
1229Will trigger an assert() if Color_Type is not kRGBA_F16_SkColorType and is built
1230with SK_DEBUG defined.
1231
Cary Clark6fc50412017-09-21 12:31:06 -04001232#Param x column index, zero or greater, and less than width() ##
1233#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001234
1235#Return readable unsigned 64-bit pointer to pixel at (x, y) ##
1236
1237#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001238 const int w = 4;
1239 const int h = 4;
1240 uint64_t storage[w * h];
1241 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType),
1242 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001243 SkDebugf("pixmap.addr64(1, 2) %c= &storage[1 + 2 * w]\n",
1244 pixmap.addr64(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
1245#StdOut
1246pixmap.addr64(1, 2) == &storage[1 + 2 * w]
1247##
1248##
1249
1250#SeeAlso addr() addr8 addr16 addr32 addrF16 getColor writable_addr writable_addr64
1251
1252##
1253
1254# ------------------------------------------------------------------------------
1255
1256#Method const uint16_t* addrF16(int x, int y) const
1257
Cary Clarkab2621d2018-01-30 10:08:57 -05001258#In Readable_Address
Cary Clarkd0530ba2017-09-14 11:25:39 -04001259Returns 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
Cary Clarkab2621d2018-01-30 10:08:57 -05001302#In Writable_Address
1303#Line # returns writable pixel address as void pointer ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001304Returns writable base pixel address.
1305
1306#Return writable generic base pointer to pixels ##
1307
1308#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001309 const int w = 4;
1310 const int h = 4;
1311 SkPMColor storage[w * h * 4];
1312 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), storage, w * 4);
1313 SkDebugf("pixmap.writable_addr() %c= (void *)storage\n",
1314 pixmap.writable_addr() == (void *)storage ? '=' : '!');
1315 pixmap.erase(0x00000000);
1316 *(SkPMColor*)pixmap.writable_addr() = 0xFFFFFFFF;
1317 SkDebugf("pixmap.getColor(0, 1) %c= 0x00000000\n",
1318 pixmap.getColor(0, 1) == 0x00000000 ? '=' : '!');
1319 SkDebugf("pixmap.getColor(0, 0) %c= 0xFFFFFFFF\n",
Cary Clarkd0530ba2017-09-14 11:25:39 -04001320 pixmap.getColor(0, 0) == 0xFFFFFFFF ? '=' : '!');
1321#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -04001322pixmap.writable_addr() == (void *)storage
1323pixmap.getColor(0, 1) == 0x00000000
Cary Clarkd0530ba2017-09-14 11:25:39 -04001324pixmap.getColor(0, 0) == 0xFFFFFFFF
1325##
1326##
1327
1328#SeeAlso writable_addr8 writable_addr16 writable_addr32 writable_addr64 writable_addrF16 addr()
1329
1330##
1331
1332# ------------------------------------------------------------------------------
1333
1334#Method void* writable_addr(int x, int y) const
1335
Cary Clarkab2621d2018-01-30 10:08:57 -05001336#In Writable_Address
Cary Clarkd0530ba2017-09-14 11:25:39 -04001337Returns writable pixel address at (x, y).
1338
1339Input is not validated: out of bounds values of x or y trigger an assert() if
1340built with SK_DEBUG defined. Returns zero if Color_Type is kUnknown_SkColorType.
1341
Cary Clark6fc50412017-09-21 12:31:06 -04001342#Param x column index, zero or greater, and less than width() ##
1343#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001344
1345#Return writable generic pointer to pixel ##
1346
1347#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001348 const int w = 4;
1349 const int h = 4;
1350 SkPMColor storage[w * h * 4];
1351 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), storage, w * 4);
1352 SkDebugf("pixmap.writable_addr() %c= (void *)storage\n",
1353 pixmap.writable_addr() == (void *)storage ? '=' : '!');
1354 pixmap.erase(0x00000000);
1355 *(SkPMColor*)pixmap.writable_addr(1, 2) = 0xFFFFFFFF;
1356 SkDebugf("pixmap.getColor(0, 0) %c= 0x00000000\n",
1357 pixmap.getColor(0, 0) == 0x00000000 ? '=' : '!');
1358 SkDebugf("pixmap.getColor(1, 2) %c= 0xFFFFFFFF\n",
Cary Clarkd0530ba2017-09-14 11:25:39 -04001359 pixmap.getColor(1, 2) == 0xFFFFFFFF ? '=' : '!');
1360#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -04001361pixmap.writable_addr() == (void *)storage
1362pixmap.getColor(0, 0) == 0x00000000
Cary Clarkd0530ba2017-09-14 11:25:39 -04001363pixmap.getColor(1, 2) == 0xFFFFFFFF
1364##
1365##
1366
1367#SeeAlso writable_addr8 writable_addr16 writable_addr32 writable_addr64 writable_addrF16 addr()
1368
1369##
1370
1371# ------------------------------------------------------------------------------
1372
1373#Method uint8_t* writable_addr8(int x, int y) const
1374
Cary Clarkab2621d2018-01-30 10:08:57 -05001375#In Writable_Address
1376#Line # returns writable pixel address as 8-bit pointer ##
Cary Clark6fc50412017-09-21 12:31:06 -04001377Returns writable pixel address at (x, y). Result is addressable as unsigned
13788-bit bytes. Will trigger an assert() if Color_Type is not kAlpha_8_SkColorType
1379or kGray_8_SkColorType, and is built with SK_DEBUG defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001380
1381One byte corresponds to one pixel.
1382
Cary Clark6fc50412017-09-21 12:31:06 -04001383#Param x column index, zero or greater, and less than width() ##
1384#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001385
1386#Return writable unsigned 8-bit pointer to pixels ##
1387
1388#Example
1389#Height 64
1390#Description
1391Altering pixels after drawing Bitmap is not guaranteed to affect subsequent
1392drawing on all platforms. Adding a second SkBitmap::installPixels after editing
1393pixel memory is safer.
1394##
Cary Clark6fc50412017-09-21 12:31:06 -04001395void draw(SkCanvas* canvas) {
1396 uint8_t storage[][5] = {{ 0, 0, 64, 0, 0},
1397 { 0, 128, 255, 128, 0},
1398 {64, 255, 255, 255, 64},
1399 { 0, 128, 255, 128, 0},
1400 { 0, 0, 64, 0, 0}};
1401 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kPremul_SkAlphaType);
1402 SkPixmap pixmap(imageInfo, storage[0], 5);
1403 SkBitmap bitmap;
1404 bitmap.installPixels(pixmap);
1405 canvas->scale(10, 10);
1406 canvas->drawBitmap(bitmap, 0, 0);
1407 *pixmap.writable_addr8(2, 2) = 0;
1408// bitmap.installPixels(pixmap); // uncomment to fix on GPU
1409 canvas->drawBitmap(bitmap, 10, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001410}
1411##
1412
1413#SeeAlso writable_addr writable_addr16 writable_addr32 writable_addr64 writable_addrF16 addr() addr8
1414
1415##
1416
1417# ------------------------------------------------------------------------------
1418
1419#Method uint16_t* writable_addr16(int x, int y) const
1420
Cary Clarkab2621d2018-01-30 10:08:57 -05001421#In Writable_Address
1422#Line # returns writable pixel address as 16-bit pointer ##
Cary Clark6fc50412017-09-21 12:31:06 -04001423Returns writable_addr pixel address at (x, y). Result is addressable as unsigned
142416-bit words. Will trigger an assert() if Color_Type is not kRGB_565_SkColorType
1425or kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001426
1427One word corresponds to one pixel.
1428
Cary Clark6fc50412017-09-21 12:31:06 -04001429#Param x column index, zero or greater, and less than width() ##
1430#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001431
1432#Return writable unsigned 16-bit pointer to pixel ##
1433
1434#Example
1435#Description
1436Draw a five by five bitmap, and draw it again with a center black pixel.
1437The low nibble of the 16-bit word is Alpha.
1438##
1439#Height 64
Cary Clark6fc50412017-09-21 12:31:06 -04001440 uint16_t storage[][5] = {{ 0xCABF, 0xDABE, 0xCA9D, 0xC96C, 0xA39B },
1441 { 0xACEE, 0xA87C, 0x893A, 0x4779, 0x8708 },
1442 { 0x4B7C, 0x255B, 0x2559, 0x2557, 0x4656 },
1443 { 0x9099, 0x8128, 0x2557, 0x4124, 0x3323 },
1444 { 0x7547, 0x5505, 0x4434, 0x2012, 0x0000 }};
1445 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kARGB_4444_SkColorType, kPremul_SkAlphaType);
1446 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
1447 SkBitmap bitmap;
1448 bitmap.installPixels(pixmap);
1449 canvas->scale(10, 10);
1450 canvas->drawBitmap(bitmap, 0, 0);
1451 *pixmap.writable_addr16(2, 2) = 0x000F;
1452 bitmap.installPixels(pixmap);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001453 canvas->drawBitmap(bitmap, 10, 0);
1454##
1455
1456#SeeAlso writable_addr writable_addr8 writable_addr32 writable_addr64 writable_addrF16 addr() addr16
1457
1458##
1459
1460# ------------------------------------------------------------------------------
1461
1462#Method uint32_t* writable_addr32(int x, int y) const
1463
Cary Clarkab2621d2018-01-30 10:08:57 -05001464#In Writable_Address
1465#Line # returns writable pixel address as 32-bit pointer ##
Cary Clark6fc50412017-09-21 12:31:06 -04001466Returns writable pixel address at (x, y). Result is addressable as unsigned
146732-bit words. Will trigger an assert() if Color_Type is not
1468kRGBA_8888_SkColorType or kBGRA_8888_SkColorType, and is built with SK_DEBUG
1469defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001470
1471One word corresponds to one pixel.
1472
Cary Clark6fc50412017-09-21 12:31:06 -04001473#Param x column index, zero or greater, and less than width() ##
1474#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001475
1476#Return writable unsigned 32-bit pointer to pixel ##
1477
1478#Example
1479#Image 4
1480#Height 72
Cary Clark6fc50412017-09-21 12:31:06 -04001481 std::vector<int32_t> pixels;
1482 pixels.resize(image->height() * image->width() * 4);
1483 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
1484 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
1485 image->readPixels(pixmap, 0, 0);
1486 for (int y = 0; y < pixmap.height() / 2; ++y) {
1487 for (int x = 0; x < pixmap.width(); ++x) {
1488 if ((x & 4) == (y & 4)) {
1489 SkTSwap(*pixmap.writable_addr32(x, y),
1490 *pixmap.writable_addr32(pixmap.width() - x, pixmap.height() - y));
1491 }
1492 }
1493 }
1494 SkBitmap bitmap;
1495 bitmap.installPixels(pixmap);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001496 canvas->drawBitmap(bitmap, 0, 0);
1497##
1498
1499#SeeAlso writable_addr writable_addr8 writable_addr16 writable_addr64 writable_addrF16 addr() addr32
1500
1501##
1502
1503# ------------------------------------------------------------------------------
1504
1505#Method uint64_t* writable_addr64(int x, int y) const
1506
Cary Clarkab2621d2018-01-30 10:08:57 -05001507#In Writable_Address
1508#Line # returns writable pixel address as 64-bit pointer ##
Cary Clark6fc50412017-09-21 12:31:06 -04001509Returns writable pixel address at (x, y). Result is addressable as unsigned
151064-bit words. Will trigger an assert() if Color_Type is not
1511kRGBA_F16_SkColorType and is built with SK_DEBUG defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001512
1513One word corresponds to one pixel.
1514
Cary Clark6fc50412017-09-21 12:31:06 -04001515#Param x column index, zero or greater, and less than width() ##
1516#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001517
1518#Return writable unsigned 64-bit pointer to pixel ##
1519
1520#Example
Cary Clark2ade9972017-11-02 17:49:34 -04001521#Height 40
Cary Clark6fc50412017-09-21 12:31:06 -04001522 SkImageInfo info = SkImageInfo::Make(3, 3, kRGBA_F16_SkColorType, kPremul_SkAlphaType);
1523 uint64_t storage[9];
1524 SkPixmap pixmap(info, storage, 3 * sizeof(uint64_t));
1525 SkColor4f c4 { 1, 0.45f, 0.25f, 0.65f };
1526 pixmap.erase(c4);
1527 SkBitmap bitmap;
1528 canvas->scale(10, 10);
1529 bitmap.installPixels(pixmap);
1530 canvas->drawBitmap(bitmap, 0, 0);
1531 *pixmap.writable_addr64(1, 1) |= 0x00ff000000000000LL;
1532 bitmap.installPixels(pixmap);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001533 canvas->drawBitmap(bitmap, 10, 0);
1534##
1535
1536#SeeAlso writable_addr writable_addr8 writable_addr16 writable_addr32 writable_addrF16 addr() addr64
1537
1538##
1539
1540# ------------------------------------------------------------------------------
1541
1542#Method uint16_t* writable_addrF16(int x, int y) const
1543
Cary Clarkab2621d2018-01-30 10:08:57 -05001544#In Writable_Address
1545#Line # returns writable pixel component address as 16-bit pointer ##
Cary Clark6fc50412017-09-21 12:31:06 -04001546Returns writable pixel address at (x, y). Result is addressable as unsigned
154716-bit words. Will trigger an assert() if Color_Type is not
1548kRGBA_F16_SkColorType and is built with SK_DEBUG defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001549
1550Each word represents one color component encoded as a half float.
1551Four words correspond to one pixel.
1552
Cary Clark6fc50412017-09-21 12:31:06 -04001553#Param x column index, zero or greater, and less than width() ##
1554#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001555
1556#Return writable unsigned 16-bit pointer to first component of pixel ##
1557
1558#Example
1559#Height 64
1560#Description
1561Left bitmap is drawn with two pixels defined in half float format. Right bitmap
1562is drawn after overwriting bottom half float color with top half float color.
1563##
Cary Clark6fc50412017-09-21 12:31:06 -04001564 SkImageInfo info = SkImageInfo::Make(1, 2, kRGBA_F16_SkColorType, kPremul_SkAlphaType);
1565 uint16_t storage[2][4];
1566 SkPixmap pixmap(info, storage[0], sizeof(uint64_t));
1567 SkIRect topPixelBounds = {0, 0, 1, 1};
1568 pixmap.erase({ 0.65f, 0.45f, 0.25f, 1 }, &topPixelBounds);
1569 SkIRect bottomPixelBounds = {0, 1, 1, 2};
1570 pixmap.erase({ 0.25f, 0.65f, 0.45f, 1 }, &bottomPixelBounds);
1571 SkBitmap bitmap;
1572 canvas->scale(20, 20);
1573 bitmap.installPixels(pixmap);
1574 canvas->drawBitmap(bitmap, 0, 0);
1575 uint16_t* pixel2 = pixmap.writable_addrF16(0, 1);
1576 for (int i = 0; i < 4; ++i) {
1577 pixel2[i] = storage[0][i];
1578 }
1579 bitmap.installPixels(pixmap);
1580 canvas->drawBitmap(bitmap, 4, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001581##
1582
1583#SeeAlso writable_addr writable_addr8 writable_addr16 writable_addr32 writable_addr64 addr() addrF16
1584
1585##
1586
1587#Subtopic Writable_Address ##
1588
1589#Subtopic Writer
1590
1591# ------------------------------------------------------------------------------
1592
1593#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1594 int srcX, int srcY, SkTransferFunctionBehavior behavior) const
Cary Clarkab2621d2018-01-30 10:08:57 -05001595#In Writer
1596#Line # copies and converts pixels ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001597
Cary Clark154beea2017-10-26 07:58:48 -04001598Copies a Rect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not
Cary Clarkac47b882018-01-11 10:35:44 -05001599exceed Pixmap (width(), height()).
Cary Clark6fc50412017-09-21 12:31:06 -04001600
1601dstInfo specifies width, height, Color_Type, Alpha_Type, and
Cary Clarkd0530ba2017-09-14 11:25:39 -04001602Color_Space of destination. dstRowBytes specifics the gap from one destination
1603row to the next. Returns true if pixels are copied. Returns false if
1604dstInfo.addr() equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes.
1605
Cary Clarkac47b882018-01-11 10:35:44 -05001606Pixels are copied only if pixel conversion is possible. If Pixmap colorType is
Cary Clarkd0530ba2017-09-14 11:25:39 -04001607kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
Cary Clarkac47b882018-01-11 10:35:44 -05001608If Pixmap colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
1609If Pixmap alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
1610match. If Pixmap colorSpace is nullptr, dstInfo.colorSpace must match. Returns
Cary Clarkd0530ba2017-09-14 11:25:39 -04001611false if pixel conversion is not possible.
1612
1613srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04001614false if width() or height() is zero or negative. Returns false if:
1615
Cary Clarkd0530ba2017-09-14 11:25:39 -04001616#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05001617abs(srcX) >= Pixmap width()
Cary Clark6fc50412017-09-21 12:31:06 -04001618##
1619, or if
1620#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05001621abs(srcY) >= Pixmap height()
Cary Clarkd0530ba2017-09-14 11:25:39 -04001622##
1623.
1624
1625If behavior is SkTransferFunctionBehavior::kRespect: converts source
1626pixels to a linear space before converting to dstInfo.
1627If behavior is SkTransferFunctionBehavior::kIgnore: source
Cary Clarkbc5697d2017-10-04 14:31:33 -04001628pixels are treated as if they are linear, regardless of how they are encoded.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001629
1630#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1631#Param dstPixels destination pixel storage ##
1632#Param dstRowBytes destination row length ##
1633#Param srcX column index whose absolute value is less than width() ##
1634#Param srcY row index whose absolute value is less than height() ##
1635#Param behavior one of: SkTransferFunctionBehavior::kRespect,
1636 SkTransferFunctionBehavior::kIgnore
1637##
1638
1639#Return true if pixels are copied to dstPixels ##
1640
1641#Example
Cary Clarkbc5697d2017-10-04 14:31:33 -04001642#ToDo example doesn't do anything interesting since info colorSpace is nullptr ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001643#Image 3
1644void draw(SkCanvas* canvas) {
1645 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height(),
1646 canvas->imageInfo().colorSpace() ? SkColorSpace::MakeSRGB() : nullptr);
Cary Clark6fc50412017-09-21 12:31:06 -04001647 std::vector<int32_t> srcPixels;
1648 srcPixels.resize(image->height() * image->width() * 4);
1649 SkPixmap pixmap(info, (const void*) &srcPixels.front(), image->width() * 4);
1650 image->readPixels(pixmap, 0, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001651 SkTransferFunctionBehavior behavior = canvas->imageInfo().colorSpace() ?
1652 SkTransferFunctionBehavior::kRespect : SkTransferFunctionBehavior::kIgnore;
Cary Clark6fc50412017-09-21 12:31:06 -04001653 std::vector<int32_t> dstPixels;
1654 dstPixels.resize(image->height() * image->width() * 4);
1655 int offset = 0;
1656 for (auto behavior : { SkTransferFunctionBehavior::kRespect,
1657 SkTransferFunctionBehavior::kIgnore} ) {
Cary Clarkd0530ba2017-09-14 11:25:39 -04001658 pixmap.readPixels(info, &dstPixels.front(), image->width() * 4, offset, 0, behavior);
1659 offset += 128;
1660 }
1661 SkBitmap bitmap;
1662 SkPixmap dstmap(info, &dstPixels.front(), image->width() * 4);
1663 bitmap.installPixels(dstmap);
1664 canvas->drawBitmap(bitmap, 0, 0);
1665}
1666##
1667
1668#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1669
1670##
1671
1672# ------------------------------------------------------------------------------
1673
1674#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes) const
1675
Cary Clarkab2621d2018-01-30 10:08:57 -05001676#In Writer
Cary Clarkd0530ba2017-09-14 11:25:39 -04001677Copies a Rect of pixels to dstPixels. Copy starts at (0, 0), and does not
Cary Clarkac47b882018-01-11 10:35:44 -05001678exceed Pixmap (width(), height()).
Cary Clark6fc50412017-09-21 12:31:06 -04001679
1680dstInfo specifies width, height, Color_Type, Alpha_Type, and
Cary Clarkd0530ba2017-09-14 11:25:39 -04001681Color_Space of destination. dstRowBytes specifics the gap from one destination
1682row to the next. Returns true if pixels are copied. Returns false if
1683dstInfo.addr() equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes.
1684
Cary Clarkac47b882018-01-11 10:35:44 -05001685Pixels are copied only if pixel conversion is possible. If Pixmap colorType is
Cary Clarkd0530ba2017-09-14 11:25:39 -04001686kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
Cary Clarkac47b882018-01-11 10:35:44 -05001687If Pixmap colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
1688If Pixmap alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
1689match. If Pixmap colorSpace is nullptr, dstInfo.colorSpace must match. Returns
Cary Clarkd0530ba2017-09-14 11:25:39 -04001690false if pixel conversion is not possible.
1691
Cary Clarkac47b882018-01-11 10:35:44 -05001692Returns false if Pixmap width() or height() is zero or negative.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001693
1694#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1695#Param dstPixels destination pixel storage ##
1696#Param dstRowBytes destination row length ##
1697
1698#Return true if pixels are copied to dstPixels ##
1699
1700#Example
1701#Height 128
1702#Description
1703Transferring the gradient from 8 bits per component to 4 bits per component
1704creates visible banding.
1705##
Cary Clark6fc50412017-09-21 12:31:06 -04001706 std::vector<int32_t> pixels;
1707 const int width = 256;
1708 const int height = 64;
1709 pixels.resize(height * width * 4);
1710 SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(width, height);
1711 SkPixmap srcPixmap(srcInfo, (const void*) &pixels.front(), width * 4);
1712 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
1713 SkPoint gradPoints[] = { { 0, 0 }, { 256, 0 } };
1714 SkPaint paint;
1715 paint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
1716 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
1717 SkBitmap bitmap;
1718 bitmap.installPixels(srcPixmap);
1719 SkCanvas srcCanvas(bitmap);
1720 srcCanvas.drawRect(SkRect::MakeWH(width, height), paint);
1721 canvas->drawBitmap(bitmap, 0, 0);
1722 std::vector<int32_t> dstPixels;
1723 dstPixels.resize(height * width * 2);
1724 SkImageInfo dstInfo = srcInfo.makeColorType(kARGB_4444_SkColorType);
1725 srcPixmap.readPixels(dstInfo, &dstPixels.front(), width * 2);
1726 SkPixmap dstPixmap(dstInfo, &dstPixels.front(), width * 2);
1727 bitmap.installPixels(dstPixmap);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001728 canvas->drawBitmap(bitmap, 0, 128);
1729##
1730
1731#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1732
1733##
1734
1735# ------------------------------------------------------------------------------
1736
1737#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, int srcX,
1738 int srcY) const
Cary Clarkab2621d2018-01-30 10:08:57 -05001739#In Writer
Cary Clarkd0530ba2017-09-14 11:25:39 -04001740
1741Copies a Rect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not
Cary Clarkac47b882018-01-11 10:35:44 -05001742exceed Pixmap (width(), height()).
Cary Clark6fc50412017-09-21 12:31:06 -04001743
1744dstInfo specifies width, height, Color_Type, Alpha_Type, and
Cary Clarkd0530ba2017-09-14 11:25:39 -04001745Color_Space of destination. dstRowBytes specifics the gap from one destination
1746row to the next. Returns true if pixels are copied. Returns false if
1747dstInfo.addr() equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes.
1748
Cary Clarkac47b882018-01-11 10:35:44 -05001749Pixels are copied only if pixel conversion is possible. If Pixmap colorType is
Cary Clarkd0530ba2017-09-14 11:25:39 -04001750kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
Cary Clarkac47b882018-01-11 10:35:44 -05001751If Pixmap colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
1752If Pixmap alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
1753match. If Pixmap colorSpace is nullptr, dstInfo.colorSpace must match. Returns
Cary Clarkd0530ba2017-09-14 11:25:39 -04001754false if pixel conversion is not possible.
1755
1756srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clarkac47b882018-01-11 10:35:44 -05001757false if Pixmap width() or height() is zero or negative. Returns false if:
Cary Clark154beea2017-10-26 07:58:48 -04001758
Cary Clarkd0530ba2017-09-14 11:25:39 -04001759#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05001760abs(srcX) >= Pixmap width()
Cary Clark6fc50412017-09-21 12:31:06 -04001761##
1762, or if
1763#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05001764abs(srcY) >= Pixmap height()
Cary Clarkd0530ba2017-09-14 11:25:39 -04001765##
1766.
1767
1768#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1769#Param dstPixels destination pixel storage ##
1770#Param dstRowBytes destination row length ##
1771#Param srcX column index whose absolute value is less than width() ##
1772#Param srcY row index whose absolute value is less than height() ##
1773
1774#Return true if pixels are copied to dstPixels ##
1775
1776#Example
1777#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -04001778void draw(SkCanvas* canvas) {
1779 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
1780 std::vector<int32_t> srcPixels;
1781 const int rowBytes = image->width() * 4;
1782 srcPixels.resize(image->height() * rowBytes);
1783 SkPixmap pixmap(info, (const void*) &srcPixels.front(), rowBytes);
1784 image->readPixels(pixmap, 0, 0);
1785 for (int offset : { 32, 64, 96 } ) {
1786 std::vector<int32_t> dstPixels;
1787 dstPixels.resize(image->height() * rowBytes);
1788 pixmap.readPixels(info, &dstPixels.front(), rowBytes, offset, 0);
1789 SkBitmap bitmap;
1790 SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
1791 bitmap.installPixels(dstmap);
1792 canvas->translate(32, 32);
1793 canvas->drawBitmap(bitmap, 0, 0);
1794 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001795}
1796##
1797
1798#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1799
1800##
1801
1802# ------------------------------------------------------------------------------
1803
1804#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY) const
1805
Cary Clarkab2621d2018-01-30 10:08:57 -05001806#In Writer
Cary Clarkd0530ba2017-09-14 11:25:39 -04001807Copies a Rect of pixels to dst. Copy starts at (srcX, srcY), and does not
Cary Clarkac47b882018-01-11 10:35:44 -05001808exceed Pixmap (width(), height()). dst specifies width, height, Color_Type,
Cary Clarkd0530ba2017-09-14 11:25:39 -04001809Alpha_Type, and Color_Space of destination. Returns true if pixels are copied.
1810Returns false if dst.addr() equals nullptr, or dst.rowBytes is less than
1811dst SkImageInfo::minRowBytes.
1812
Cary Clarkac47b882018-01-11 10:35:44 -05001813Pixels are copied only if pixel conversion is possible. If Pixmap colorType is
Cary Clarkd0530ba2017-09-14 11:25:39 -04001814kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.info().colorType must match.
Cary Clarkac47b882018-01-11 10:35:44 -05001815If Pixmap colorType is kGray_8_SkColorType, dst.info().colorSpace must match.
1816If Pixmap alphaType is kOpaque_SkAlphaType, dst.info().alphaType must
1817match. If Pixmap colorSpace is nullptr, dst.info().colorSpace must match. Returns
Cary Clarkd0530ba2017-09-14 11:25:39 -04001818false if pixel conversion is not possible.
1819
1820srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clarkac47b882018-01-11 10:35:44 -05001821false Pixmap width() or height() is zero or negative. Returns false if:
Cary Clark154beea2017-10-26 07:58:48 -04001822
Cary Clarkd0530ba2017-09-14 11:25:39 -04001823#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05001824abs(srcX) >= Pixmap width()
Cary Clark6fc50412017-09-21 12:31:06 -04001825##
1826, or if
1827#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05001828abs(srcY) >= Pixmap height()
Cary Clarkd0530ba2017-09-14 11:25:39 -04001829##
1830.
1831
1832#Param dst Image_Info and pixel address to write to ##
1833#Param srcX column index whose absolute value is less than width() ##
1834#Param srcY row index whose absolute value is less than height() ##
1835
1836#Return true if pixels are copied to dst ##
1837
1838#Example
1839#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -04001840void draw(SkCanvas* canvas) {
1841 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
1842 std::vector<int32_t> srcPixels;
1843 const int rowBytes = image->width() * 4;
1844 srcPixels.resize(image->height() * rowBytes);
1845 SkPixmap pixmap(info, (const void*) &srcPixels.front(), rowBytes);
1846 image->readPixels(pixmap, 0, 0);
1847 for (int offset : { 32, 64, 96 } ) {
1848 std::vector<int32_t> dstPixels;
1849 dstPixels.resize(image->height() * rowBytes);
1850 SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
1851 pixmap.readPixels(dstmap, offset, 0);
1852 SkBitmap bitmap;
1853 bitmap.installPixels(dstmap);
1854 canvas->translate(32, 32);
1855 canvas->drawBitmap(bitmap, 0, 0);
1856 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001857}
1858##
1859
1860#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1861
1862##
1863
1864# ------------------------------------------------------------------------------
1865
1866#Method bool readPixels(const SkPixmap& dst) const
1867
Cary Clarkab2621d2018-01-30 10:08:57 -05001868#In Writer
Cary Clarkd0530ba2017-09-14 11:25:39 -04001869Copies pixels inside bounds() to dst. dst specifies width, height, Color_Type,
1870Alpha_Type, and Color_Space of destination. Returns true if pixels are copied.
1871Returns false if dst.addr() equals nullptr, or dst.rowBytes is less than
1872dst SkImageInfo::minRowBytes.
1873
Cary Clarkac47b882018-01-11 10:35:44 -05001874Pixels are copied only if pixel conversion is possible. If Pixmap colorType is
Cary Clarkd0530ba2017-09-14 11:25:39 -04001875kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05001876If Pixmap colorType is kGray_8_SkColorType, dst Color_Space must match.
1877If Pixmap alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
1878match. If Pixmap colorSpace is nullptr, dst Color_Space must match. Returns
Cary Clarkd0530ba2017-09-14 11:25:39 -04001879false if pixel conversion is not possible.
1880
Cary Clarkac47b882018-01-11 10:35:44 -05001881Returns false if Pixmap width() or height() is zero or negative.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001882
1883#Param dst Image_Info and pixel address to write to ##
1884
1885#Return true if pixels are copied to dst ##
1886
1887#Example
1888#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -04001889void draw(SkCanvas* canvas) {
1890 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
1891 std::vector<int32_t> srcPixels;
1892 const int rowBytes = image->width() * 4;
1893 srcPixels.resize(image->height() * rowBytes);
1894 SkPixmap pixmap(info, (const void*) &srcPixels.front(), rowBytes);
1895 image->readPixels(pixmap, 0, 0);
1896 for (int index = 0; index < 3; ++index ) {
1897 std::vector<int32_t> dstPixels;
1898 dstPixels.resize(image->height() * rowBytes);
1899 SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
1900 pixmap.readPixels(dstmap);
1901 SkBitmap bitmap;
1902 bitmap.installPixels(dstmap);
1903 canvas->translate(32, 32);
1904 canvas->drawBitmap(bitmap, 0, 0);
1905 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001906}
1907##
1908
1909#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1910
1911##
1912
1913# ------------------------------------------------------------------------------
1914
1915#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality) const
1916
Cary Clarkab2621d2018-01-30 10:08:57 -05001917#In Writer
1918#Line # scales and converts pixels ##
Cary Clarkac47b882018-01-11 10:35:44 -05001919Copies Bitmap to dst, scaling pixels to fit dst.width() and dst.height(), and
Cary Clarkd0530ba2017-09-14 11:25:39 -04001920converting pixels to match dst.colorType and dst.alphaType. Returns true if
1921pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1922less than dst SkImageInfo::minRowBytes.
1923
Cary Clarkac47b882018-01-11 10:35:44 -05001924Pixels are copied only if pixel conversion is possible. If Pixmap colorType is
Cary Clarkd0530ba2017-09-14 11:25:39 -04001925kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05001926If Pixmap colorType is kGray_8_SkColorType, dst Color_Space must match.
1927If Pixmap alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
1928match. If Pixmap colorSpace is nullptr, dst Color_Space must match. Returns
Cary Clarkd0530ba2017-09-14 11:25:39 -04001929false if pixel conversion is not possible.
1930
Cary Clarkac47b882018-01-11 10:35:44 -05001931Returns false if Bitmap width() or height() is zero or negative.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001932
1933Scales the image, with filterQuality, to match dst.width() and dst.height().
1934filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1935Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1936Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1937Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1938kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1939
1940#Param dst Image_Info and pixel address to write to ##
1941#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1942 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1943##
1944
Cary Clarkac47b882018-01-11 10:35:44 -05001945#Return true if pixels are scaled to fit dst ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001946
1947#Example
1948#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -04001949void draw(SkCanvas* canvas) {
1950 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
1951 std::vector<int32_t> srcPixels;
1952 int rowBytes = image->width() * 4;
1953 srcPixels.resize(image->height() * rowBytes);
1954 SkPixmap pixmap(info, (const void*) &srcPixels.front(), rowBytes);
1955 image->readPixels(pixmap, 0, 0);
1956 for (int offset : { 32, 64, 96 } ) {
1957 info = SkImageInfo::MakeN32Premul(image->width() + offset, image->height());
1958 rowBytes = info.width() * 4;
1959 std::vector<int32_t> dstPixels;
1960 dstPixels.resize(image->height() * rowBytes);
1961 SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
1962 pixmap.scalePixels(dstmap, kMedium_SkFilterQuality);
1963 SkBitmap bitmap;
1964 bitmap.installPixels(dstmap);
1965 canvas->translate(32, 32);
1966 canvas->drawBitmap(bitmap, 0, 0);
1967 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001968}
1969##
1970
1971#SeeAlso SkCanvas::drawBitmap SkImage::scalePixels
1972
1973##
1974
1975# ------------------------------------------------------------------------------
1976
1977#Method bool erase(SkColor color, const SkIRect& subset) const
1978
Cary Clarkab2621d2018-01-30 10:08:57 -05001979#In Writer
1980#Line # writes Color to pixels ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001981Writes color to pixels bounded by subset; returns true on success.
1982Returns false if colorType is kUnknown_SkColorType, or if subset does
1983not intersect bounds().
1984
1985#Param color Unpremultiplied Color to write ##
1986#Param subset bounding integer Rect of written pixels ##
1987
1988#Return true if pixels are changed ##
1989
1990#Example
Cary Clark2ade9972017-11-02 17:49:34 -04001991#Height 50
Cary Clark6fc50412017-09-21 12:31:06 -04001992 uint32_t storage[2];
1993 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 2);
1994 SkPixmap pixmap(info, storage, info.minRowBytes());
1995 pixmap.erase(SK_ColorBLUE, {0, 0, 1, 1});
1996 pixmap.erase(SK_ColorRED, {0, 1, 1, 2});
1997 SkBitmap bitmap;
1998 canvas->scale(20, 20);
1999 bitmap.installPixels(pixmap);
2000 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04002001##
2002
2003#SeeAlso SkBitmap::erase SkCanvas::clear SkCanvas::drawColor
2004
2005##
2006
2007# ------------------------------------------------------------------------------
2008
2009#Method bool erase(SkColor color) const
2010
Cary Clarkab2621d2018-01-30 10:08:57 -05002011#In Writer
Cary Clarkd0530ba2017-09-14 11:25:39 -04002012Writes color to pixels inside bounds(); returns true on success.
2013Returns false if colorType is kUnknown_SkColorType, or if bounds()
2014is empty.
2015
2016#Param color Unpremultiplied Color to write ##
2017
2018#Return true if pixels are changed ##
2019
2020#Example
Cary Clark2ade9972017-11-02 17:49:34 -04002021#Height 50
Cary Clark6fc50412017-09-21 12:31:06 -04002022 uint32_t storage[2];
2023 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 2);
2024 SkPixmap pixmap(info, storage, info.minRowBytes());
2025 pixmap.erase(SK_ColorBLUE);
2026 SkBitmap bitmap;
2027 canvas->scale(20, 20);
2028 bitmap.installPixels(pixmap);
2029 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04002030##
2031
2032#SeeAlso SkBitmap::erase SkCanvas::clear SkCanvas::drawColor
2033
2034##
2035
2036# ------------------------------------------------------------------------------
2037
2038#Method bool erase(const SkColor4f& color, const SkIRect* subset = nullptr) const
2039
Cary Clarkab2621d2018-01-30 10:08:57 -05002040#In Writer
Cary Clarkd0530ba2017-09-14 11:25:39 -04002041Writes color to pixels bounded by subset; returns true on success.
2042if subset is nullptr, writes colors pixels inside bounds(). Returns false if
2043colorType is kUnknown_SkColorType, if subset is not nullptr and does
2044not intersect bounds(), or if subset is nullptr and bounds() is empty.
2045
2046#Param color Unpremultiplied Color to write ##
2047#Param subset bounding integer Rect of pixels to write; may be nullptr ##
2048
2049#Return true if pixels are changed ##
2050
2051#Example
Cary Clark2ade9972017-11-02 17:49:34 -04002052#Height 50
Cary Clark6fc50412017-09-21 12:31:06 -04002053 uint32_t storage[2];
2054 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 2);
2055 SkPixmap pixmap(info, storage, info.minRowBytes());
2056 SkIRect topPixelBounds = {0, 0, 1, 1};
2057 pixmap.erase({ 0.65f, 0.45f, 0.25f, 1 }, &topPixelBounds);
2058 SkIRect bottomPixelBounds = {0, 1, 1, 2};
2059 pixmap.erase({ 0.25f, 0.65f, 0.45f, 1 }, &bottomPixelBounds);
2060 SkBitmap bitmap;
2061 canvas->scale(20, 20);
2062 bitmap.installPixels(pixmap);
2063 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04002064##
2065
2066#SeeAlso SkBitmap::erase SkCanvas::clear SkCanvas::drawColor
2067
2068##
2069
2070
2071#Subtopic Writer ##
2072
2073#Class SkPixmap ##
2074
2075#Topic Pixmap ##