blob: 9ad87f0accfdff1a4b6e974b384f567d283ec406 [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 Clark08895c42018-02-01 09:37:32 -05004#Subtopic Overview
Cary Clark4855f782018-02-06 09:41:53 -05005 #Subtopic Subtopic
Cary Clark08895c42018-02-01 09:37:32 -05006 #Populate
7 ##
8##
9
Cary Clarkd0530ba2017-09-14 11:25:39 -040010#Class SkPixmap
11
12Pixmap provides a utility to pair SkImageInfo with pixels and row bytes.
13Pixmap is a low level class which provides convenience functions to access
14raster destinations. Canvas can not draw Pixmap, nor does Pixmap provide
15a direct drawing destination.
16
17Use Bitmap to draw pixels referenced by Pixmap; use Surface to draw into
18pixels referenced by Pixmap.
19
Cary Clarkbc5697d2017-10-04 14:31:33 -040020Pixmap does not try to manage the lifetime of the pixel memory. Use Pixel_Ref
21to manage pixel memory; Pixel_Ref is safe across threads.
Cary Clarkd0530ba2017-09-14 11:25:39 -040022
Cary Clark4855f782018-02-06 09:41:53 -050023#Subtopic Related_Function
Cary Clark08895c42018-02-01 09:37:32 -050024#Populate
Cary Clark5081eed2018-01-22 07:55:48 -050025##
26
Cary Clark4855f782018-02-06 09:41:53 -050027#Subtopic Constructor
Cary Clark08895c42018-02-01 09:37:32 -050028#Populate
29##
Cary Clarkd0530ba2017-09-14 11:25:39 -040030
Cary Clark4855f782018-02-06 09:41:53 -050031#Subtopic Member_Function
Cary Clark08895c42018-02-01 09:37:32 -050032#Populate
33##
Cary Clarkd0530ba2017-09-14 11:25:39 -040034
35#Subtopic Initialization
Cary Clark08895c42018-02-01 09:37:32 -050036#Line # sets fields for use ##
Cary Clarkd0530ba2017-09-14 11:25:39 -040037
38# ------------------------------------------------------------------------------
39
40#Method SkPixmap()
41
Cary Clarkab2621d2018-01-30 10:08:57 -050042#In Initialization
43#Line # constructs with default values ##
Cary Clarkd0530ba2017-09-14 11:25:39 -040044Creates an empty Pixmap without pixels, with kUnknown_SkColorType, with
45kUnknown_SkAlphaType, and with a width and height of zero. Use
46reset() to associate pixels, SkColorType, SkAlphaType, width, and height
47after Pixmap has been created.
48
49#Return empty Pixmap ##
50
51#Example
Cary Clark6fc50412017-09-21 12:31:06 -040052void draw(SkCanvas* canvas) {
53 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -050054 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
55 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clark6fc50412017-09-21 12:31:06 -040056 SkPixmap pixmap;
57 for (int i = 0; i < 2; ++i) {
58 SkDebugf("width: %2d height: %2d", pixmap.width(), pixmap.height());
59 SkDebugf(" color: k%s_SkColorType", colors[pixmap.colorType()]);
60 SkDebugf(" alpha: k%s_SkAlphaType\n", alphas[pixmap.alphaType()]);
61 pixmap.reset(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType),
62 nullptr, 0);
63 }
Cary Clarkd0530ba2017-09-14 11:25:39 -040064}
65#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -040066width: 0 height: 0 color: kUnknown_SkColorType alpha: kUnknown_SkAlphaType
Cary Clarkd0530ba2017-09-14 11:25:39 -040067width: 25 height: 35 color: kRGBA_8888_SkColorType alpha: kOpaque_SkAlphaType
68##
69##
70
71#SeeAlso SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes) reset() SkAlphaType SkColorType
72
73##
74
75# ------------------------------------------------------------------------------
76
77#Method SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes)
78
Cary Clarkab2621d2018-01-30 10:08:57 -050079#In Initialization
80#Line # constructs from Image_Info, pixels ##
Cary Clarkd0530ba2017-09-14 11:25:39 -040081Creates Pixmap from info width, height, SkAlphaType, and SkColorType.
Cary Clark6fc50412017-09-21 12:31:06 -040082addr points to pixels, or nullptr. rowBytes should be info.width() times
83info.bytesPerPixel(), or larger.
Cary Clarkd0530ba2017-09-14 11:25:39 -040084
85No parameter checking is performed; it is up to the caller to ensure that
86addr and rowBytes agree with info.
87
Cary Clark6fc50412017-09-21 12:31:06 -040088The memory lifetime of pixels is managed by the caller. When Pixmap goes
Cary Clarkd0530ba2017-09-14 11:25:39 -040089out of scope, addr is unaffected.
90
91Pixmap may be later modified by reset() to change its size, pixel type, or
92storage.
93
94#Param info width, height, SkAlphaType, SkColorType of Image_Info ##
95#Param addr pointer to pixels allocated by caller; may be nullptr ##
96#Param rowBytes size of one row of addr; width times pixel size, or larger ##
97
98#Return initialized Pixmap ##
99
100#Example
101#Image 3
102#Description
103SkImage::MakeRasterCopy takes const SkPixmap& as an argument. The example
104constructs a SkPixmap from the brace-delimited parameters.
105##
Cary Clark6fc50412017-09-21 12:31:06 -0400106 SkDebugf("image alpha only = %s\n", image->isAlphaOnly() ? "true" : "false");
107 SkPMColor pmColors = 0;
108 sk_sp<SkImage> copy = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1),
109 (uint8_t*)&pmColors,
110 1});
Cary Clarkd0530ba2017-09-14 11:25:39 -0400111 SkDebugf("copy alpha only = %s\n", copy->isAlphaOnly() ? "true" : "false");
112#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400113image alpha only = false
Cary Clarkd0530ba2017-09-14 11:25:39 -0400114copy alpha only = true
115##
116##
117
118#SeeAlso SkPixmap() reset() SkAlphaType SkColorType
119
120##
121
122# ------------------------------------------------------------------------------
123
124#Method void reset()
125
Cary Clarkab2621d2018-01-30 10:08:57 -0500126#In Initialization
127#Line # reuses existing Pixmap with replacement values ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400128Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to
129kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType.
130
131The prior pixels are unaffected; it is up to the caller to release pixels
132memory if desired.
133
134#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400135void draw(SkCanvas* canvas) {
136 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500137 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
138 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clark6fc50412017-09-21 12:31:06 -0400139 SkPixmap pixmap(SkImageInfo::Make(25, 35, kRGBA_8888_SkColorType, kOpaque_SkAlphaType),
140 nullptr, 0);
141 for (int i = 0; i < 2; ++i) {
142 SkDebugf("width: %2d height: %2d", pixmap.width(), pixmap.height());
143 SkDebugf(" color: k%s_SkColorType", colors[pixmap.colorType()]);
144 SkDebugf(" alpha: k%s_SkAlphaType\n", alphas[pixmap.alphaType()]);
145 pixmap.reset();
146 }
147}
Cary Clarkd0530ba2017-09-14 11:25:39 -0400148#StdOut
149width: 25 height: 35 color: kRGBA_8888_SkColorType alpha: kOpaque_SkAlphaType
Cary Clark6fc50412017-09-21 12:31:06 -0400150width: 0 height: 0 color: kUnknown_SkColorType alpha: kUnknown_SkAlphaType
Cary Clarkd0530ba2017-09-14 11:25:39 -0400151##
152##
153
154#SeeAlso SkPixmap() SkAlphaType SkColorType
155
156##
157
158# ------------------------------------------------------------------------------
159
160#Method void reset(const SkImageInfo& info, const void* addr, size_t rowBytes)
161
Cary Clarkab2621d2018-01-30 10:08:57 -0500162#In Initialization
Cary Clarkd0530ba2017-09-14 11:25:39 -0400163Sets width, height, SkAlphaType, and SkColorType from info.
164Sets pixel address from addr, which may be nullptr.
Cary Clark6fc50412017-09-21 12:31:06 -0400165Sets row bytes from rowBytes, which should be info.width() times
166info.bytesPerPixel(), or larger.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400167
168Does not check addr. Asserts if built with SK_DEBUG defined and if rowBytes is
169too small to hold one row of pixels.
170
171The memory lifetime pixels are managed by the caller. When Pixmap goes
172out of scope, addr is unaffected.
173
174#Param info width, height, SkAlphaType, SkColorType of Image_Info ##
175#Param addr pointer to pixels allocated by caller; may be nullptr ##
176#Param rowBytes size of one row of addr; width times pixel size, or larger ##
177
178#Example
179#Image 4
Cary Clark2ade9972017-11-02 17:49:34 -0400180#Height 64
Cary Clark6fc50412017-09-21 12:31:06 -0400181void draw(SkCanvas* canvas) {
182 std::vector<int32_t> pixels;
183 pixels.resize(image->height() * image->width() * 4);
184 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
185 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
186 image->readPixels(pixmap, 0, 0);
187 int x = 0;
188 for (auto colorType : { kRGBA_8888_SkColorType, kBGRA_8888_SkColorType } ) {
189 pixmap.reset(SkImageInfo::Make(image->width(), image->height(), colorType,
190 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
191 SkBitmap bitmap;
192 bitmap.installPixels(pixmap);
193 canvas->drawBitmap(bitmap, x, 0);
194 x += 128;
195 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400196}
197##
198
199#SeeAlso SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes) reset() SkAlphaType SkColorType
200
201##
202
203# ------------------------------------------------------------------------------
204
205#Method void setColorSpace(sk_sp<SkColorSpace> colorSpace)
206
Cary Clarkab2621d2018-01-30 10:08:57 -0500207#In Initialization
208#Line # sets Image_Info Color_Space ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400209
210Changes Color_Space in Image_Info; preserves width, height, SkAlphaType, and
211SkColorType in Image, and leaves pixel address and row bytes unchanged.
Cary Clark6fc50412017-09-21 12:31:06 -0400212Color_Space reference count is incremented.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400213
214#Param colorSpace Color_Space moved to Image_Info ##
215
216#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400217void draw(SkCanvas* canvas) {
218 SkPixmap pixmap;
219 sk_sp<SkColorSpace> colorSpace1 = SkColorSpace::MakeRGB(SkColorSpace::kLinear_RenderTargetGamma,
220 SkColorSpace::kRec2020_Gamut);
221 SkDebugf("is %sunique\n", colorSpace1->unique() ? "" : "not ");
222 pixmap.setColorSpace(colorSpace1);
223 SkDebugf("is %sunique\n", colorSpace1->unique() ? "" : "not ");
Cary Clarkd0530ba2017-09-14 11:25:39 -0400224}
225#StdOut
226is unique
227is not unique
228##
229##
230
231#SeeAlso Color_Space SkImageInfo::makeColorSpace
232
233##
234
235# ------------------------------------------------------------------------------
236
237#Method bool SK_WARN_UNUSED_RESULT reset(const SkMask& mask)
Cary Clark4855f782018-02-06 09:41:53 -0500238#Deprecated soon
Cary Clarkd0530ba2017-09-14 11:25:39 -0400239##
240
241# ------------------------------------------------------------------------------
242
243#Method bool SK_WARN_UNUSED_RESULT extractSubset(SkPixmap* subset, const SkIRect& area) const
244
Cary Clarkab2621d2018-01-30 10:08:57 -0500245#In Initialization
246#Line # sets pointer to portion of original ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400247Sets subset width, height, pixel address to intersection of Pixmap with area,
248if intersection is not empty; and return true. Otherwise, leave subset unchanged
249and return false.
250
251Failing to read the return value generates a compile time warning.
252
253#Param subset storage for width, height, pixel address of intersection ##
254#Param area bounds to intersect with Pixmap ##
255
256#Return true if intersection of Pixmap and area is not empty ##
257
258#Example
259#Image 3
260#Height 128
Cary Clark6fc50412017-09-21 12:31:06 -0400261void draw(SkCanvas* canvas) {
262 std::vector<int32_t> pixels;
263 pixels.resize(image->height() * image->width() * 4);
264 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
265 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
266 image->readPixels(pixmap, 0, 0);
267 SkPixmap inset;
268 if (pixmap.extractSubset(&inset, {128, 128, 512, 512})) {
269 SkBitmap bitmap;
270 bitmap.installPixels(inset);
271 canvas->drawBitmap(bitmap, 0, 0);
272 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400273}
274##
275
276#SeeAlso reset() SkIRect::intersect
277
278##
279
280#Subtopic Initialization ##
281
282#Subtopic Image_Info_Access
Cary Clark08895c42018-02-01 09:37:32 -0500283#Line # returns all or part of Image_Info ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400284
285# ------------------------------------------------------------------------------
286
287#Method const SkImageInfo& info() const
288
Cary Clarkab2621d2018-01-30 10:08:57 -0500289#In Image_Info_Access
290#Line # returns Image_Info ##
Cary Clark6fc50412017-09-21 12:31:06 -0400291Returns width, height, Alpha_Type, Color_Type, and Color_Space.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400292
293#Return reference to ImageInfo ##
294
295#Example
296#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -0400297 std::vector<int32_t> pixels;
298 pixels.resize(image->height() * image->width() * 4);
299 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
300 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
301 image->readPixels(pixmap, 0, 0);
302 SkPixmap inset;
303 if (pixmap.extractSubset(&inset, {128, 128, 512, 512})) {
304 const SkImageInfo& info = inset.info();
305 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
Cary Clarkab2621d2018-01-30 10:08:57 -0500306 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888",
307 "RGB_888x", "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clark6fc50412017-09-21 12:31:06 -0400308 SkDebugf("width: %d height: %d color: %s alpha: %s\n", info.width(), info.height(),
309 colors[info.colorType()], alphas[info.alphaType()]);
310 }
311#StdOut
312width: 384 height: 384 color: BGRA_8888 alpha: Opaque
313##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400314##
315
316#SeeAlso Image_Info
317
318##
319
320# ------------------------------------------------------------------------------
321
322#Method size_t rowBytes() const
323
Cary Clarkab2621d2018-01-30 10:08:57 -0500324#In Image_Info_Access
325#Line # returns interval between rows in bytes ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400326Returns row bytes, the interval from one pixel row to the next. Row bytes
Cary Clark154beea2017-10-26 07:58:48 -0400327is at least as large as:
Cary Clarkd0530ba2017-09-14 11:25:39 -0400328#Formula
329width() * info().bytesPerPixel()
330##
331.
332
Cary Clarkbc5697d2017-10-04 14:31:33 -0400333Returns zero if colorType is kUnknown_SkColorType.
334It is up to the Bitmap creator to ensure that row bytes is a useful value.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400335
336#Return byte length of pixel row ##
337
338#Example
339SkPixmap badPixmap = {SkImageInfo::MakeA8(4, 4), nullptr, 2};
340SkPixmap okPixmap = {SkImageInfo::MakeA8(4, 4), nullptr, 8};
341for (auto& pixmap : { badPixmap, okPixmap } ) {
342 SkDebugf("rowBytes: %d minRowBytes: %d\n", pixmap.rowBytes(),
343 pixmap.info().minRowBytes());
344}
345#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400346rowBytes: 2 minRowBytes: 4
Cary Clarkd0530ba2017-09-14 11:25:39 -0400347rowBytes: 8 minRowBytes: 4
348##
349##
350
351#SeeAlso addr() info() SkImageInfo::minRowBytes
352
353##
354
355# ------------------------------------------------------------------------------
356
357#Method const void* addr() const
358
Cary Clarkab2621d2018-01-30 10:08:57 -0500359#In Image_Info_Access
360#Line # returns readable pixel address as void pointer ##
Cary Clark6fc50412017-09-21 12:31:06 -0400361Returns pixel address, the base address corresponding to the pixel origin.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400362
363It is up to the Pixmap creator to ensure that pixel address is a useful value.
364
365#Return pixel address ##
366
367#Example
368#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -0400369 std::vector<int32_t> pixels;
370 pixels.resize(image->height() * image->width() * 4);
371 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
372 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
373 image->readPixels(pixmap, 0, 0);
374 SkDebugf("pixels address: 0x%llx\n", pixmap.addr());
375 SkPixmap inset;
376 if (pixmap.extractSubset(&inset, {128, 128, 512, 512})) {
377 SkDebugf("inset address: 0x%llx\n", inset.addr());
378 }
379#StdOut
380#Volatile
381pixels address: 0x7f2a440bb010
382inset address: 0x7f2a440fb210
383##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400384##
385
386#SeeAlso addr(int x, int y) addr8 addr16 addr32 addr64 info() rowBytes()
387
388##
389
390# ------------------------------------------------------------------------------
391
392#Method int width() const
393
Cary Clarkab2621d2018-01-30 10:08:57 -0500394#In Image_Info_Access
395#Line # returns pixel column count ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400396Returns pixel count in each pixel row. Should be equal or less than:
Cary Clark154beea2017-10-26 07:58:48 -0400397
Cary Clarkd0530ba2017-09-14 11:25:39 -0400398#Formula
Cary Clarkbc5697d2017-10-04 14:31:33 -0400399rowBytes() / info().bytesPerPixel()
Cary Clarkd0530ba2017-09-14 11:25:39 -0400400##
401.
402
403#Return pixel width in Image_Info ##
404
405#Example
406 SkImageInfo info = SkImageInfo::MakeA8(16, 32);
Cary Clark6fc50412017-09-21 12:31:06 -0400407 SkPixmap pixmap(info, nullptr, 64);
408 SkDebugf("pixmap width: %d info width: %d\n", pixmap.width(), info.width());
409#StdOut
410pixmap width: 16 info width: 16
411##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400412##
413
Cary Clarkbc5697d2017-10-04 14:31:33 -0400414#SeeAlso height() SkImageInfo::width()
Cary Clarkd0530ba2017-09-14 11:25:39 -0400415
416##
417
418# ------------------------------------------------------------------------------
419
420#Method int height() const
421
Cary Clarkab2621d2018-01-30 10:08:57 -0500422#In Image_Info_Access
423#Line # returns pixel row count ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400424Returns pixel row count.
425
426#Return pixel height in Image_Info ##
427
428#Example
Cary Clarkbc5697d2017-10-04 14:31:33 -0400429 SkPixmap pixmap(SkImageInfo::MakeA8(16, 32), nullptr, 64);
430 SkDebugf("pixmap height: %d info height: %d\n", pixmap.height(), pixmap.info().height());
Cary Clark6fc50412017-09-21 12:31:06 -0400431#StdOut
432pixmap height: 32 info height: 32
433##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400434##
435
Cary Clarkbc5697d2017-10-04 14:31:33 -0400436#SeeAlso width() ImageInfo::height()
Cary Clarkd0530ba2017-09-14 11:25:39 -0400437
438##
439
440# ------------------------------------------------------------------------------
441
442#Method SkColorType colorType() const
443
Cary Clarkab2621d2018-01-30 10:08:57 -0500444#In Image_Info_Access
445#Line # returns Image_Info Color_Type ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500446Returns Color_Type, one of: #list_of_color_types#.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400447
448#Return Color_Type in Image_Info ##
449
450#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500451 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
452 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clark6fc50412017-09-21 12:31:06 -0400453 SkPixmap pixmap(SkImageInfo::MakeA8(16, 32), nullptr, 64);
454 SkDebugf("color type: k" "%s" "_SkColorType\n", colors[pixmap.colorType()]);
455#StdOut
Cary Clarkab2621d2018-01-30 10:08:57 -0500456color type: kAlpha_8_SkColorType
Cary Clark6fc50412017-09-21 12:31:06 -0400457##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400458##
459
Cary Clarkbc5697d2017-10-04 14:31:33 -0400460#SeeAlso alphaType() SkImageInfo::colorType
Cary Clarkd0530ba2017-09-14 11:25:39 -0400461
462##
463
464# ------------------------------------------------------------------------------
465
466#Method SkAlphaType alphaType() const
467
Cary Clarkab2621d2018-01-30 10:08:57 -0500468#In Image_Info_Access
469#Line # returns Image_Info Alpha_Type ##
Cary Clark681287e2018-03-16 11:34:15 -0400470Returns Alpha_Type, one of: #list_of_alpha_types#.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400471
472#Return Alpha_Type in Image_Info ##
473
474#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400475 const char* alphas[] = {"Unknown", "Opaque", "Premul", "Unpremul"};
476 SkPixmap pixmap(SkImageInfo::MakeA8(16, 32), nullptr, 64);
477 SkDebugf("alpha type: k" "%s" "_SkAlphaType\n", alphas[pixmap.alphaType()]);
Cary Clarkd0530ba2017-09-14 11:25:39 -0400478#StdOut
479alpha type: kPremul_SkAlphaType
480##
481##
482
Cary Clarkbc5697d2017-10-04 14:31:33 -0400483#SeeAlso colorType() SkImageInfo::alphaType
Cary Clarkd0530ba2017-09-14 11:25:39 -0400484
485##
486
487# ------------------------------------------------------------------------------
488
489#Method SkColorSpace* colorSpace() const
490
Cary Clarkab2621d2018-01-30 10:08:57 -0500491#In Image_Info_Access
492#Line # returns Image_Info Color_Space ##
Cary Clark681287e2018-03-16 11:34:15 -0400493Returns Color_Space, the range of colors, associated with Image_Info. The
Cary Clarkbc5697d2017-10-04 14:31:33 -0400494reference count of Color_Space is unchanged. The returned Color_Space is
495immutable.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400496
Cary Clark681287e2018-03-16 11:34:15 -0400497#Return Color_Space in Image_Info, or nullptr ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400498
499#Example
500#Description
501SkColorSpace::MakeSRGBLinear creates Color_Space with linear gamma
502and an sRGB gamut. This Color_Space gamma is not close to sRGB gamma.
503##
Cary Clark6fc50412017-09-21 12:31:06 -0400504 SkPixmap pixmap(SkImageInfo::MakeN32(16, 32, kPremul_SkAlphaType,
505 SkColorSpace::MakeSRGBLinear()), nullptr, 64);
506 SkColorSpace* colorSpace = pixmap.colorSpace();
507 SkDebugf("gammaCloseToSRGB: %s gammaIsLinear: %s isSRGB: %s\n",
508 colorSpace->gammaCloseToSRGB() ? "true" : "false",
509 colorSpace->gammaIsLinear() ? "true" : "false",
510 colorSpace->isSRGB() ? "true" : "false");
511#StdOut
512gammaCloseToSRGB: false gammaIsLinear: true isSRGB: false
513##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400514##
515
Cary Clarkbc5697d2017-10-04 14:31:33 -0400516#SeeAlso Color_Space SkImageInfo::colorSpace
Cary Clarkd0530ba2017-09-14 11:25:39 -0400517
518##
519
520# ------------------------------------------------------------------------------
521
522#Method bool isOpaque() const
523
Cary Clarkab2621d2018-01-30 10:08:57 -0500524#In Image_Info_Access
525#Line # returns true if Image_Info describes opaque pixels ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400526Returns true if Alpha_Type is kOpaque_SkAlphaType.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400527Does not check if Color_Type allows Alpha, or if any pixel value has
528transparency.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400529
530#Return true if Image_Info has opaque Alpha_Type ##
531
532#Example
533#Description
534 isOpaque ignores whether all pixels are opaque or not.
535##
Cary Clark6fc50412017-09-21 12:31:06 -0400536 std::vector<uint32_t> pixels;
537 const int height = 2;
538 const int width = 2;
539 pixels.resize(height * width * 4);
540 SkPixmap pixmap(SkImageInfo::Make(width, height, kN32_SkColorType,
541 kPremul_SkAlphaType), (const void*) &pixels.front(), width * 4);
542 for (int index = 0; index < 2; ++index) {
543 pixmap.erase(0x00000000);
544 SkDebugf("isOpaque: %s\n", pixmap.isOpaque() ? "true" : "false");
545 pixmap.erase(0xFFFFFFFF);
546 SkDebugf("isOpaque: %s\n", pixmap.isOpaque() ? "true" : "false");
547 pixmap.reset(pixmap.info().makeAlphaType(kOpaque_SkAlphaType),
548 (const void*) &pixels.front(), width * 4);
549 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400550#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400551isOpaque: false
552isOpaque: false
553isOpaque: true
Cary Clarkd0530ba2017-09-14 11:25:39 -0400554isOpaque: true
555##
556##
557
558#SeeAlso computeIsOpaque SkImageInfo::isOpaque
559
560##
561
562# ------------------------------------------------------------------------------
563
564#Method SkIRect bounds() const
565
Cary Clarkab2621d2018-01-30 10:08:57 -0500566#In Image_Info_Access
567#Line # returns width and height as Rectangle ##
Cary Clark154beea2017-10-26 07:58:48 -0400568Returns IRect { 0, 0, width(), height() }.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400569
570#Return integral rectangle from origin to width() and height() ##
571
572#Example
573 for (int width : { 0, 2 } ) {
574 for (int height : { 0, 2 } ) {
Cary Clark6fc50412017-09-21 12:31:06 -0400575 SkPixmap pixmap(SkImageInfo::MakeA8(width, height), nullptr, width);
Cary Clarkd0530ba2017-09-14 11:25:39 -0400576 SkDebugf("width: %d height: %d empty: %s\n", width, height,
577 pixmap.bounds().isEmpty() ? "true" : "false");
578 }
579 }
580#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400581width: 0 height: 0 empty: true
582width: 0 height: 2 empty: true
583width: 2 height: 0 empty: true
Cary Clarkd0530ba2017-09-14 11:25:39 -0400584width: 2 height: 2 empty: false
585##
586##
587
588#SeeAlso height() width() IRect
589
590##
591
592# ------------------------------------------------------------------------------
593
594#Method int rowBytesAsPixels() const
595
Cary Clarkab2621d2018-01-30 10:08:57 -0500596#In Image_Info_Access
597#Line # returns interval between rows in pixels ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400598
599Returns number of pixels that fit on row. Should be greater than or equal to
600width().
601
602#Return maximum pixels per row ##
603
604#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400605 for (int rowBytes : { 4, 5, 6, 7, 8} ) {
606 SkPixmap pixmap(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), nullptr, rowBytes);
607 SkDebugf("rowBytes: %d rowBytesAsPixels: %d\n", rowBytes, pixmap.rowBytesAsPixels());
608 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400609#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400610rowBytes: 4 rowBytesAsPixels: 1
611rowBytes: 5 rowBytesAsPixels: 1
612rowBytes: 6 rowBytesAsPixels: 1
613rowBytes: 7 rowBytesAsPixels: 1
Cary Clarkd0530ba2017-09-14 11:25:39 -0400614rowBytes: 8 rowBytesAsPixels: 2
615##
616##
617
618#SeeAlso rowBytes shiftPerPixel width SkImageInfo::bytesPerPixel
619
620##
621
622# ------------------------------------------------------------------------------
623
624#Method int shiftPerPixel() const
625
Cary Clarkab2621d2018-01-30 10:08:57 -0500626#In Image_Info_Access
627#Line # returns bit shift from pixels to bytes ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400628Returns bit shift converting row bytes to row pixels.
629Returns zero for kUnknown_SkColorType.
630
631#Return one of: 0, 1, 2, 3; left shift to convert pixels to bytes ##
632
633#Example
Cary Clarkab2621d2018-01-30 10:08:57 -0500634 const char* colors[] = {"Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
635 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16"};
Cary Clark6fc50412017-09-21 12:31:06 -0400636 SkImageInfo info = SkImageInfo::MakeA8(1, 1);
637 for (SkColorType colorType : { kUnknown_SkColorType, kAlpha_8_SkColorType,
638 kRGB_565_SkColorType, kARGB_4444_SkColorType,
639 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
640 kGray_8_SkColorType, kRGBA_F16_SkColorType } ) {
641 SkPixmap pixmap(info.makeColorType(colorType), nullptr, 4);
642 SkDebugf("color: k" "%s" "_SkColorType" "%*s" "bytesPerPixel: %d shiftPerPixel: %d\n",
643 colors[colorType], 10 - strlen(colors[colorType]), " ",
644 pixmap.info().bytesPerPixel(), pixmap.shiftPerPixel());
645 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400646#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400647color: kUnknown_SkColorType bytesPerPixel: 0 shiftPerPixel: 0
Cary Clarkab2621d2018-01-30 10:08:57 -0500648color: kAlpha_8_SkColorType bytesPerPixel: 1 shiftPerPixel: 0
Cary Clark6fc50412017-09-21 12:31:06 -0400649color: kRGB_565_SkColorType bytesPerPixel: 2 shiftPerPixel: 1
650color: kARGB_4444_SkColorType bytesPerPixel: 2 shiftPerPixel: 1
651color: kRGBA_8888_SkColorType bytesPerPixel: 4 shiftPerPixel: 2
652color: kBGRA_8888_SkColorType bytesPerPixel: 4 shiftPerPixel: 2
653color: kGray_8_SkColorType bytesPerPixel: 1 shiftPerPixel: 0
Cary Clarkd0530ba2017-09-14 11:25:39 -0400654color: kRGBA_F16_SkColorType bytesPerPixel: 8 shiftPerPixel: 3
655##
656##
657
658#SeeAlso rowBytes rowBytesAsPixels width SkImageInfo::bytesPerPixel
659
660##
661
662# ------------------------------------------------------------------------------
663
Cary Clarkbc5697d2017-10-04 14:31:33 -0400664#Method size_t computeByteSize() const
665
Cary Clarkab2621d2018-01-30 10:08:57 -0500666#In Image_Info_Access
667#Line # returns size required for pixels ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400668Returns minimum memory required for pixel storage.
669Does not include unused memory on last row when rowBytesAsPixels exceeds width().
670Returns zero if result does not fit in size_t.
671Returns zero if height() or width() is 0.
672Returns height() times rowBytes if colorType is kUnknown_SkColorType.
673
674#Return size in bytes of image buffer ##
675
Cary Clarkd0530ba2017-09-14 11:25:39 -0400676#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400677 SkPixmap pixmap;
678 for (int width : { 1, 1000, 1000000 } ) {
679 for (int height: { 1, 1000, 1000000 } ) {
680 SkImageInfo imageInfo = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
Cary Clarkbc5697d2017-10-04 14:31:33 -0400681 pixmap.reset(imageInfo, nullptr, width * 5);
682 SkDebugf("width: %7d height: %7d computeByteSize: %13lld\n", width, height,
683 pixmap.computeByteSize());
Cary Clark6fc50412017-09-21 12:31:06 -0400684 }
685 }
Cary Clark6fc50412017-09-21 12:31:06 -0400686#StdOut
Ben Wagner29380bd2017-10-09 14:43:00 -0400687width: 1 height: 1 computeByteSize: 4
688width: 1 height: 1000 computeByteSize: 4999
689width: 1 height: 1000000 computeByteSize: 4999999
690width: 1000 height: 1 computeByteSize: 4000
691width: 1000 height: 1000 computeByteSize: 4999000
692width: 1000 height: 1000000 computeByteSize: 4999999000
693width: 1000000 height: 1 computeByteSize: 4000000
694width: 1000000 height: 1000 computeByteSize: 4999000000
Cary Clarkbc5697d2017-10-04 14:31:33 -0400695width: 1000000 height: 1000000 computeByteSize: 4999999000000
Cary Clarkd0530ba2017-09-14 11:25:39 -0400696##
697##
698
Cary Clarkbc5697d2017-10-04 14:31:33 -0400699#SeeAlso SkImageInfo::computeByteSize
Cary Clarkd0530ba2017-09-14 11:25:39 -0400700
701##
702
703#Subtopic Image_Info_Access ##
704
705#Subtopic Reader
Cary Clark08895c42018-02-01 09:37:32 -0500706#Line # examine pixel value ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400707
708# ------------------------------------------------------------------------------
709
710#Method bool computeIsOpaque() const
711
Cary Clarkab2621d2018-01-30 10:08:57 -0500712#In Reader
713#Line # returns true if all pixels are opaque ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400714Returns true if all pixels are opaque. Color_Type determines how pixels
715are encoded, and whether pixel describes Alpha. Returns true for Color_Types
Cary Clark6fc50412017-09-21 12:31:06 -0400716without alpha in each pixel; for other Color_Types, returns true if all
Cary Clarkd0530ba2017-09-14 11:25:39 -0400717pixels have alpha values equivalent to 1.0 or greater.
718
719For Color_Types kRGB_565_SkColorType or kGray_8_SkColorType: always
720returns true. For Color_Types kAlpha_8_SkColorType, kBGRA_8888_SkColorType,
721kRGBA_8888_SkColorType: returns true if all pixel Alpha values are 255.
722For Color_Type kARGB_4444_SkColorType: returns true if all pixel Alpha values are 15.
723For kRGBA_F16_SkColorType: returns true if all pixel Alpha values are 1.0 or
724greater.
725
Cary Clark6fc50412017-09-21 12:31:06 -0400726Returns false for kUnknown_SkColorType.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400727
Cary Clarkbc5697d2017-10-04 14:31:33 -0400728#Return true if all pixels have opaque values or Color_Type is opaque ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400729
730#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400731 std::vector<uint32_t> pixels;
732 const int height = 2;
733 const int width = 2;
734 pixels.resize(height * width * 4);
735 SkPixmap pixmap(SkImageInfo::Make(width, height, kN32_SkColorType,
736 kPremul_SkAlphaType), (const void*) &pixels.front(), width * 4);
737 for (int index = 0; index < 2; ++index) {
738 pixmap.erase(0x00000000);
739 SkDebugf("computeIsOpaque: %s\n", pixmap.computeIsOpaque() ? "true" : "false");
740 pixmap.erase(0xFFFFFFFF);
741 SkDebugf("computeIsOpaque: %s\n", pixmap.computeIsOpaque() ? "true" : "false");
742 pixmap.reset(pixmap.info().makeAlphaType(kOpaque_SkAlphaType),
743 (const void*) &pixels.front(), width * 4);
Cary Clarkd0530ba2017-09-14 11:25:39 -0400744 }
745#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400746computeIsOpaque: false
747computeIsOpaque: true
748computeIsOpaque: false
Cary Clarkd0530ba2017-09-14 11:25:39 -0400749computeIsOpaque: true
750##
751##
752
753#SeeAlso isOpaque Color_Type Alpha
754
755##
756
757# ------------------------------------------------------------------------------
758
759#Method SkColor getColor(int x, int y) const
760
Cary Clarkab2621d2018-01-30 10:08:57 -0500761#In Reader
762#Line # returns one pixel as Unpremultiplied Color ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400763Returns pixel at (x, y) as Unpremultiplied Color.
764Returns black with Alpha if Color_Type is kAlpha_8_SkColorType.
765
766Input is not validated: out of bounds values of x or y trigger an assert() if
767built with SK_DEBUG defined; and returns undefined values or may crash if
768SK_RELEASE is defined. Fails if Color_Type is kUnknown_SkColorType or
769pixel address is nullptr.
770
771Color_Space in Image_Info is ignored. Some Color precision may be lost in the
772conversion to Unpremultiplied Color; original pixel data may have additional
773precision.
774
Cary Clark6fc50412017-09-21 12:31:06 -0400775#Param x column index, zero or greater, and less than width() ##
776#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400777
778#Return pixel converted to Unpremultiplied Color ##
779
780#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400781 const int w = 4;
782 const int h = 4;
783 std::vector<SkPMColor> storage;
784 storage.resize(w * h);
785 SkDebugf("Premultiplied:\n");
786 for (int y = 0; y < h; ++y) {
787 SkDebugf("(0, %d) ", y);
788 for (int x = 0; x < w; ++x) {
789 int a = 0xFF * (x + y) / (w - 1 + h - 1);
790 storage[x + y * w] = SkPackARGB32(a, a * x / (w - 1), a * y / (h - 1), a);
791 SkDebugf("0x%08x%c", storage[x + y * w], x == w - 1 ? '\n' : ' ');
792 }
793 }
794 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), &storage.front(), w * 4);
795 SkDebugf("Unpremultiplied:\n");
796 for (int y = 0; y < h; ++y) {
797 SkDebugf("(0, %d) ", y);
798 for (int x = 0; x < w; ++x) {
799 SkDebugf("0x%08x%c", pixmap.getColor(x, y), x == w - 1 ? '\n' : ' ');
800 }
801 }
Cary Clarkd0530ba2017-09-14 11:25:39 -0400802#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -0400803Premultiplied:
804(0, 0) 0x00000000 0x2a0e002a 0x55380055 0x7f7f007f
805(0, 1) 0x2a000e2a 0x551c1c55 0x7f542a7f 0xaaaa38aa
806(0, 2) 0x55003855 0x7f2a547f 0xaa7171aa 0xd4d48dd4
807(0, 3) 0x7f007f7f 0xaa38aaaa 0xd48dd4d4 0xffffffff
808Unpremultiplied:
809(0, 0) 0x00000000 0x2a5500ff 0x55a800ff 0x7fff00ff
810(0, 1) 0x2a0055ff 0x555454ff 0x7fa954ff 0xaaff54ff
811(0, 2) 0x5500a8ff 0x7f54a9ff 0xaaaaaaff 0xd4ffaaff
Cary Clarkd0530ba2017-09-14 11:25:39 -0400812(0, 3) 0x7f00ffff 0xaa54ffff 0xd4aaffff 0xffffffff
813##
814##
815
816#SeeAlso addr() readPixels
817
818##
819
820#Subtopic Reader ##
821
822#Subtopic Readable_Address
Cary Clark08895c42018-02-01 09:37:32 -0500823#Line # returns read only pixels ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400824
825# ------------------------------------------------------------------------------
826
827#Method const void* addr(int x, int y) const
828
Cary Clarkab2621d2018-01-30 10:08:57 -0500829#In Readable_Address
Cary Clarkbc5697d2017-10-04 14:31:33 -0400830Returns readable pixel address at (x, y). Returns nullptr if Pixel_Ref is nullptr.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400831
832Input is not validated: out of bounds values of x or y trigger an assert() if
Cary Clarkbc5697d2017-10-04 14:31:33 -0400833built with SK_DEBUG defined. Returns nullptr if Color_Type is kUnknown_SkColorType.
834
835Performs a lookup of pixel size; for better performance, call
836one of: addr8, addr16, addr32, addr64, or addrF16.
Cary Clarkd0530ba2017-09-14 11:25:39 -0400837
Cary Clark6fc50412017-09-21 12:31:06 -0400838#Param x column index, zero or greater, and less than width() ##
839#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400840
841#Return readable generic pointer to pixel ##
842
843#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400844 const int w = 4;
845 const int h = 4;
846 std::vector<SkPMColor> storage;
847 storage.resize(w * h);
848 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), &storage.front(), w * 4);
Cary Clarkd0530ba2017-09-14 11:25:39 -0400849 SkDebugf("pixmap.addr(1, 2) %c= &storage[1 + 2 * w]\n",
850 pixmap.addr(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
851#StdOut
852pixmap.addr(1, 2) == &storage[1 + 2 * w]
853##
854##
855
Cary Clarkbc5697d2017-10-04 14:31:33 -0400856#SeeAlso addr8 addr16 addr32 addr64 addrF16 getColor writable_addr SkBitmap::getAddr
Cary Clarkd0530ba2017-09-14 11:25:39 -0400857
858##
859
860# ------------------------------------------------------------------------------
861
862#Method const uint8_t* addr8() const
863
Cary Clarkab2621d2018-01-30 10:08:57 -0500864#In Readable_Address
865#Line # returns readable pixel address as 8-bit pointer ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400866Returns readable base pixel address. Result is addressable as unsigned 8-bit bytes.
867Will trigger an assert() if Color_Type is not kAlpha_8_SkColorType or
868kGray_8_SkColorType, and is built with SK_DEBUG defined.
869
870One byte corresponds to one pixel.
871
872#Return readable unsigned 8-bit pointer to pixels ##
873
874#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400875 const int w = 4;
876 const int h = 4;
877 uint8_t storage[w * h];
878 SkPixmap pixmap(SkImageInfo::Make(w, h, kGray_8_SkColorType, kPremul_SkAlphaType),
879 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -0400880 SkDebugf("pixmap.addr8() %c= storage\n",
881 pixmap.addr8() == storage ? '=' : '!');
882#StdOut
883pixmap.addr8() == storage
884##
885##
886
887#SeeAlso addr() addr16 addr32 addr64 addrF16 getColor writable_addr writable_addr8
888
889##
890
891# ------------------------------------------------------------------------------
892
893#Method const uint16_t* addr16() const
894
Cary Clarkab2621d2018-01-30 10:08:57 -0500895#In Readable_Address
896#Line # returns readable pixel address as 16-bit pointer ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400897Returns readable base pixel address. Result is addressable as unsigned 16-bit words.
898Will trigger an assert() if Color_Type is not kRGB_565_SkColorType or
899kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
900
901One word corresponds to one pixel.
902
903#Return readable unsigned 16-bit pointer to pixels ##
904
905#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400906 const int w = 4;
907 const int h = 4;
908 uint16_t storage[w * h];
909 SkPixmap pixmap(SkImageInfo::Make(w, h, kARGB_4444_SkColorType, kPremul_SkAlphaType),
910 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -0400911 SkDebugf("pixmap.addr16() %c= storage\n",
912 pixmap.addr16() == storage ? '=' : '!');
913#StdOut
914pixmap.addr16() == storage
915##
916##
917
918#SeeAlso addr() addr8 addr32 addr64 addrF16 getColor writable_addr writable_addr16
919
920##
921
922# ------------------------------------------------------------------------------
923
924#Method const uint32_t* addr32() const
925
Cary Clarkab2621d2018-01-30 10:08:57 -0500926#In Readable_Address
927#Line # returns readable pixel address as 32-bit pointer ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400928Returns readable base pixel address. Result is addressable as unsigned 32-bit words.
929Will trigger an assert() if Color_Type is not kRGBA_8888_SkColorType or
930kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.
931
932One word corresponds to one pixel.
933
934#Return readable unsigned 32-bit pointer to pixels ##
935
936#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400937 const int w = 4;
938 const int h = 4;
939 uint32_t storage[w * h];
940 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType),
941 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -0400942 SkDebugf("pixmap.addr32() %c= storage\n",
943 pixmap.addr32() == storage ? '=' : '!');
944#StdOut
945pixmap.addr32() == storage
946##
947##
948
949#SeeAlso addr() addr8 addr16 addr64 addrF16 getColor writable_addr writable_addr32
950
951##
952
953# ------------------------------------------------------------------------------
954
955#Method const uint64_t* addr64() const
956
Cary Clarkab2621d2018-01-30 10:08:57 -0500957#In Readable_Address
958#Line # returns readable pixel address as 64-bit pointer ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400959Returns readable base pixel address. Result is addressable as unsigned 64-bit words.
960Will trigger an assert() if Color_Type is not kRGBA_F16_SkColorType and is built
961with SK_DEBUG defined.
962
963One word corresponds to one pixel.
964
965#Return readable unsigned 64-bit pointer to pixels ##
966
967#Example
Cary Clark6fc50412017-09-21 12:31:06 -0400968 const int w = 4;
969 const int h = 4;
970 uint64_t storage[w * h];
971 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType),
972 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -0400973 SkDebugf("pixmap.addr64() %c= storage\n",
974 pixmap.addr64() == storage ? '=' : '!');
975#StdOut
976pixmap.addr64() == storage
977##
978##
979
980#SeeAlso addr() addr8 addr16 addr32 addrF16 getColor writable_addr writable_addr64
981
982##
983
984# ------------------------------------------------------------------------------
985
986#Method const uint16_t* addrF16() const
987
Cary Clarkab2621d2018-01-30 10:08:57 -0500988#In Readable_Address
989#Line # returns readable pixel component address as 16-bit pointer ##
Cary Clarkd0530ba2017-09-14 11:25:39 -0400990Returns readable base pixel address. Result is addressable as unsigned 16-bit words.
991Will trigger an assert() if Color_Type is not kRGBA_F16_SkColorType and is built
992with SK_DEBUG defined.
993
994Each word represents one color component encoded as a half float.
995Four words correspond to one pixel.
996
997#Return readable unsigned 16-bit pointer to first component of pixels ##
998
999#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001000 const int w = 4;
1001 const int h = 4;
1002 uint16_t storage[w * h * 4];
1003 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType),
1004 storage, w * 4 * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001005 SkDebugf("pixmap.addrF16() %c= storage\n",
1006 pixmap.addrF16() == storage ? '=' : '!');
1007#StdOut
1008pixmap.addrF16() == storage
1009##
1010##
1011
1012#SeeAlso addr() addr8 addr16 addr32 addr64 getColor writable_addr writable_addrF16
1013
1014##
1015
1016# ------------------------------------------------------------------------------
1017
1018#Method const uint8_t* addr8(int x, int y) const
1019
Cary Clarkab2621d2018-01-30 10:08:57 -05001020#In Readable_Address
Cary Clarkd0530ba2017-09-14 11:25:39 -04001021Returns readable pixel address at (x, y).
1022
1023Input is not validated: out of bounds values of x or y trigger an assert() if
1024built with SK_DEBUG defined.
1025
1026Will trigger an assert() if Color_Type is not kAlpha_8_SkColorType or
1027kGray_8_SkColorType, and is built with SK_DEBUG defined.
1028
Cary Clark6fc50412017-09-21 12:31:06 -04001029#Param x column index, zero or greater, and less than width() ##
1030#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001031
1032#Return readable unsigned 8-bit pointer to pixel at (x, y) ##
1033
1034#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001035 const int w = 4;
1036 const int h = 4;
1037 uint8_t storage[w * h];
1038 SkPixmap pixmap(SkImageInfo::Make(w, h, kGray_8_SkColorType, kPremul_SkAlphaType),
1039 storage, w * sizeof(storage[0]));
1040 SkDebugf("pixmap.addr8(1, 2) %c= &storage[1 + 2 * w]\n",
1041 pixmap.addr8(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
Cary Clarkd0530ba2017-09-14 11:25:39 -04001042#StdOut
1043pixmap.addr8(1, 2) == &storage[1 + 2 * w]
1044##
1045##
1046
1047#SeeAlso addr() addr16 addr32 addr64 addrF16 getColor writable_addr writable_addr8
1048
1049##
1050
1051# ------------------------------------------------------------------------------
1052
1053#Method const uint16_t* addr16(int x, int y) const
1054
Cary Clarkab2621d2018-01-30 10:08:57 -05001055#In Readable_Address
Cary Clarkd0530ba2017-09-14 11:25:39 -04001056Returns readable pixel address at (x, y).
1057
1058Input is not validated: out of bounds values of x or y trigger an assert() if
1059built with SK_DEBUG defined.
1060
1061Will trigger an assert() if Color_Type is not kRGB_565_SkColorType or
1062kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
1063
Cary Clark6fc50412017-09-21 12:31:06 -04001064#Param x column index, zero or greater, and less than width() ##
1065#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001066
1067#Return readable unsigned 16-bit pointer to pixel at (x, y) ##
1068
1069#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001070 const int w = 4;
1071 const int h = 4;
1072 uint16_t storage[w * h];
1073 SkPixmap pixmap(SkImageInfo::Make(w, h, kARGB_4444_SkColorType, kPremul_SkAlphaType),
1074 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001075 SkDebugf("pixmap.addr16(1, 2) %c= &storage[1 + 2 * w]\n",
1076 pixmap.addr16(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
1077#StdOut
1078pixmap.addr16(1, 2) == &storage[1 + 2 * w]
1079##
1080##
1081
1082#SeeAlso addr() addr8 addr32 addr64 addrF16 getColor writable_addr writable_addr16
1083
1084##
1085
1086# ------------------------------------------------------------------------------
1087
1088#Method const uint32_t* addr32(int x, int y) const
1089
Cary Clarkab2621d2018-01-30 10:08:57 -05001090#In Readable_Address
Cary Clarkd0530ba2017-09-14 11:25:39 -04001091Returns readable pixel address at (x, y).
1092
1093Input is not validated: out of bounds values of x or y trigger an assert() if
1094built with SK_DEBUG defined.
1095
1096Will trigger an assert() if Color_Type is not kRGBA_8888_SkColorType or
1097kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.
1098
Cary Clark6fc50412017-09-21 12:31:06 -04001099#Param x column index, zero or greater, and less than width() ##
1100#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001101
1102#Return readable unsigned 32-bit pointer to pixel at (x, y) ##
1103
1104#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001105 const int w = 4;
1106 const int h = 4;
1107 uint32_t storage[w * h];
1108 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
1109 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001110 SkDebugf("pixmap.addr32(1, 2) %c= &storage[1 + 2 * w]\n",
1111 pixmap.addr32(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
1112#StdOut
1113pixmap.addr32(1, 2) == &storage[1 + 2 * w]
1114##
1115##
1116
Cary Clark2ade9972017-11-02 17:49:34 -04001117#SeeAlso addr() addr8 addr16 addr64 addrF16 getColor writable_addr writable_addr64
Cary Clarkd0530ba2017-09-14 11:25:39 -04001118
1119##
1120
1121# ------------------------------------------------------------------------------
1122
1123#Method const uint64_t* addr64(int x, int y) const
1124
Cary Clarkab2621d2018-01-30 10:08:57 -05001125#In Readable_Address
Cary Clarkd0530ba2017-09-14 11:25:39 -04001126Returns readable pixel address at (x, y).
1127
1128Input is not validated: out of bounds values of x or y trigger an assert() if
1129built with SK_DEBUG defined.
1130
1131Will trigger an assert() if Color_Type is not kRGBA_F16_SkColorType and is built
1132with SK_DEBUG defined.
1133
Cary Clark6fc50412017-09-21 12:31:06 -04001134#Param x column index, zero or greater, and less than width() ##
1135#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001136
1137#Return readable unsigned 64-bit pointer to pixel at (x, y) ##
1138
1139#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001140 const int w = 4;
1141 const int h = 4;
1142 uint64_t storage[w * h];
1143 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType),
1144 storage, w * sizeof(storage[0]));
Cary Clarkd0530ba2017-09-14 11:25:39 -04001145 SkDebugf("pixmap.addr64(1, 2) %c= &storage[1 + 2 * w]\n",
1146 pixmap.addr64(1, 2) == &storage[1 + 2 * w] ? '=' : '!');
1147#StdOut
1148pixmap.addr64(1, 2) == &storage[1 + 2 * w]
1149##
1150##
1151
1152#SeeAlso addr() addr8 addr16 addr32 addrF16 getColor writable_addr writable_addr64
1153
1154##
1155
1156# ------------------------------------------------------------------------------
1157
1158#Method const uint16_t* addrF16(int x, int y) const
1159
Cary Clarkab2621d2018-01-30 10:08:57 -05001160#In Readable_Address
Cary Clarkd0530ba2017-09-14 11:25:39 -04001161Returns readable pixel address at (x, y).
1162
1163Input is not validated: out of bounds values of x or y trigger an assert() if
1164built with SK_DEBUG defined.
1165
1166Will trigger an assert() if Color_Type is not kRGBA_F16_SkColorType and is built
1167with SK_DEBUG defined.
1168
1169Each unsigned 16-bit word represents one color component encoded as a half float.
1170Four words correspond to one pixel.
1171
Cary Clark6fc50412017-09-21 12:31:06 -04001172#Param x column index, zero or greater, and less than width() ##
1173#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001174
1175#Return readable unsigned 16-bit pointer to pixel component at (x, y) ##
1176
1177#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001178 const int w = 4;
1179 const int h = 4;
1180 const int wordsPerPixel = 4;
1181 const int rowWords = w * wordsPerPixel;
1182 uint16_t storage[rowWords * h];
1183 SkPixmap pixmap(SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType),
1184 storage, rowWords * sizeof(storage[0]));
1185 SkDebugf("pixmap.addrF16(1, 2) %c= &storage[1 * wordsPerPixel + 2 * rowWords]\n",
1186 pixmap.addrF16(1, 2) == &storage[1 * wordsPerPixel + 2 * rowWords] ? '=' : '!');
Cary Clarkd0530ba2017-09-14 11:25:39 -04001187#StdOut
1188pixmap.addrF16(1, 2) == &storage[1 * wordsPerPixel + 2 * rowWords]
1189##
1190##
1191
1192#SeeAlso addr() addr8 addr16 addr32 addr64 getColor writable_addr writable_addrF16
1193
1194##
1195
1196#Subtopic Readable_Address ##
1197
1198#Subtopic Writable_Address
Cary Clark08895c42018-02-01 09:37:32 -05001199#Line # returns writable pixels ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001200
1201# ------------------------------------------------------------------------------
1202
1203#Method void* writable_addr() const
1204
Cary Clarkab2621d2018-01-30 10:08:57 -05001205#In Writable_Address
1206#Line # returns writable pixel address as void pointer ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001207Returns writable base pixel address.
1208
1209#Return writable generic base pointer to pixels ##
1210
1211#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001212 const int w = 4;
1213 const int h = 4;
1214 SkPMColor storage[w * h * 4];
1215 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), storage, w * 4);
1216 SkDebugf("pixmap.writable_addr() %c= (void *)storage\n",
1217 pixmap.writable_addr() == (void *)storage ? '=' : '!');
1218 pixmap.erase(0x00000000);
1219 *(SkPMColor*)pixmap.writable_addr() = 0xFFFFFFFF;
1220 SkDebugf("pixmap.getColor(0, 1) %c= 0x00000000\n",
1221 pixmap.getColor(0, 1) == 0x00000000 ? '=' : '!');
1222 SkDebugf("pixmap.getColor(0, 0) %c= 0xFFFFFFFF\n",
Cary Clarkd0530ba2017-09-14 11:25:39 -04001223 pixmap.getColor(0, 0) == 0xFFFFFFFF ? '=' : '!');
1224#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -04001225pixmap.writable_addr() == (void *)storage
1226pixmap.getColor(0, 1) == 0x00000000
Cary Clarkd0530ba2017-09-14 11:25:39 -04001227pixmap.getColor(0, 0) == 0xFFFFFFFF
1228##
1229##
1230
1231#SeeAlso writable_addr8 writable_addr16 writable_addr32 writable_addr64 writable_addrF16 addr()
1232
1233##
1234
1235# ------------------------------------------------------------------------------
1236
1237#Method void* writable_addr(int x, int y) const
1238
Cary Clarkab2621d2018-01-30 10:08:57 -05001239#In Writable_Address
Cary Clarkd0530ba2017-09-14 11:25:39 -04001240Returns writable pixel address at (x, y).
1241
1242Input is not validated: out of bounds values of x or y trigger an assert() if
1243built with SK_DEBUG defined. Returns zero if Color_Type is kUnknown_SkColorType.
1244
Cary Clark6fc50412017-09-21 12:31:06 -04001245#Param x column index, zero or greater, and less than width() ##
1246#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001247
1248#Return writable generic pointer to pixel ##
1249
1250#Example
Cary Clark6fc50412017-09-21 12:31:06 -04001251 const int w = 4;
1252 const int h = 4;
1253 SkPMColor storage[w * h * 4];
1254 SkPixmap pixmap(SkImageInfo::MakeN32(w, h, kPremul_SkAlphaType), storage, w * 4);
1255 SkDebugf("pixmap.writable_addr() %c= (void *)storage\n",
1256 pixmap.writable_addr() == (void *)storage ? '=' : '!');
1257 pixmap.erase(0x00000000);
1258 *(SkPMColor*)pixmap.writable_addr(1, 2) = 0xFFFFFFFF;
1259 SkDebugf("pixmap.getColor(0, 0) %c= 0x00000000\n",
1260 pixmap.getColor(0, 0) == 0x00000000 ? '=' : '!');
1261 SkDebugf("pixmap.getColor(1, 2) %c= 0xFFFFFFFF\n",
Cary Clarkd0530ba2017-09-14 11:25:39 -04001262 pixmap.getColor(1, 2) == 0xFFFFFFFF ? '=' : '!');
1263#StdOut
Cary Clark6fc50412017-09-21 12:31:06 -04001264pixmap.writable_addr() == (void *)storage
1265pixmap.getColor(0, 0) == 0x00000000
Cary Clarkd0530ba2017-09-14 11:25:39 -04001266pixmap.getColor(1, 2) == 0xFFFFFFFF
1267##
1268##
1269
1270#SeeAlso writable_addr8 writable_addr16 writable_addr32 writable_addr64 writable_addrF16 addr()
1271
1272##
1273
1274# ------------------------------------------------------------------------------
1275
1276#Method uint8_t* writable_addr8(int x, int y) const
1277
Cary Clarkab2621d2018-01-30 10:08:57 -05001278#In Writable_Address
1279#Line # returns writable pixel address as 8-bit pointer ##
Cary Clark6fc50412017-09-21 12:31:06 -04001280Returns writable pixel address at (x, y). Result is addressable as unsigned
12818-bit bytes. Will trigger an assert() if Color_Type is not kAlpha_8_SkColorType
1282or kGray_8_SkColorType, and is built with SK_DEBUG defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001283
1284One byte corresponds to one pixel.
1285
Cary Clark6fc50412017-09-21 12:31:06 -04001286#Param x column index, zero or greater, and less than width() ##
1287#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001288
1289#Return writable unsigned 8-bit pointer to pixels ##
1290
1291#Example
1292#Height 64
1293#Description
1294Altering pixels after drawing Bitmap is not guaranteed to affect subsequent
1295drawing on all platforms. Adding a second SkBitmap::installPixels after editing
1296pixel memory is safer.
1297##
Cary Clark6fc50412017-09-21 12:31:06 -04001298void draw(SkCanvas* canvas) {
1299 uint8_t storage[][5] = {{ 0, 0, 64, 0, 0},
1300 { 0, 128, 255, 128, 0},
1301 {64, 255, 255, 255, 64},
1302 { 0, 128, 255, 128, 0},
1303 { 0, 0, 64, 0, 0}};
1304 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kPremul_SkAlphaType);
1305 SkPixmap pixmap(imageInfo, storage[0], 5);
1306 SkBitmap bitmap;
1307 bitmap.installPixels(pixmap);
1308 canvas->scale(10, 10);
1309 canvas->drawBitmap(bitmap, 0, 0);
1310 *pixmap.writable_addr8(2, 2) = 0;
1311// bitmap.installPixels(pixmap); // uncomment to fix on GPU
1312 canvas->drawBitmap(bitmap, 10, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001313}
1314##
1315
1316#SeeAlso writable_addr writable_addr16 writable_addr32 writable_addr64 writable_addrF16 addr() addr8
1317
1318##
1319
1320# ------------------------------------------------------------------------------
1321
1322#Method uint16_t* writable_addr16(int x, int y) const
1323
Cary Clarkab2621d2018-01-30 10:08:57 -05001324#In Writable_Address
1325#Line # returns writable pixel address as 16-bit pointer ##
Cary Clark6fc50412017-09-21 12:31:06 -04001326Returns writable_addr pixel address at (x, y). Result is addressable as unsigned
132716-bit words. Will trigger an assert() if Color_Type is not kRGB_565_SkColorType
1328or kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001329
1330One word corresponds to one pixel.
1331
Cary Clark6fc50412017-09-21 12:31:06 -04001332#Param x column index, zero or greater, and less than width() ##
1333#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001334
1335#Return writable unsigned 16-bit pointer to pixel ##
1336
1337#Example
1338#Description
1339Draw a five by five bitmap, and draw it again with a center black pixel.
1340The low nibble of the 16-bit word is Alpha.
1341##
1342#Height 64
Cary Clark6fc50412017-09-21 12:31:06 -04001343 uint16_t storage[][5] = {{ 0xCABF, 0xDABE, 0xCA9D, 0xC96C, 0xA39B },
1344 { 0xACEE, 0xA87C, 0x893A, 0x4779, 0x8708 },
1345 { 0x4B7C, 0x255B, 0x2559, 0x2557, 0x4656 },
1346 { 0x9099, 0x8128, 0x2557, 0x4124, 0x3323 },
1347 { 0x7547, 0x5505, 0x4434, 0x2012, 0x0000 }};
1348 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kARGB_4444_SkColorType, kPremul_SkAlphaType);
1349 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
1350 SkBitmap bitmap;
1351 bitmap.installPixels(pixmap);
1352 canvas->scale(10, 10);
1353 canvas->drawBitmap(bitmap, 0, 0);
1354 *pixmap.writable_addr16(2, 2) = 0x000F;
1355 bitmap.installPixels(pixmap);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001356 canvas->drawBitmap(bitmap, 10, 0);
1357##
1358
1359#SeeAlso writable_addr writable_addr8 writable_addr32 writable_addr64 writable_addrF16 addr() addr16
1360
1361##
1362
1363# ------------------------------------------------------------------------------
1364
1365#Method uint32_t* writable_addr32(int x, int y) const
1366
Cary Clarkab2621d2018-01-30 10:08:57 -05001367#In Writable_Address
1368#Line # returns writable pixel address as 32-bit pointer ##
Cary Clark6fc50412017-09-21 12:31:06 -04001369Returns writable pixel address at (x, y). Result is addressable as unsigned
137032-bit words. Will trigger an assert() if Color_Type is not
1371kRGBA_8888_SkColorType or kBGRA_8888_SkColorType, and is built with SK_DEBUG
1372defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001373
1374One word corresponds to one pixel.
1375
Cary Clark6fc50412017-09-21 12:31:06 -04001376#Param x column index, zero or greater, and less than width() ##
1377#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001378
1379#Return writable unsigned 32-bit pointer to pixel ##
1380
1381#Example
1382#Image 4
1383#Height 72
Cary Clark6fc50412017-09-21 12:31:06 -04001384 std::vector<int32_t> pixels;
1385 pixels.resize(image->height() * image->width() * 4);
1386 SkPixmap pixmap(SkImageInfo::Make(image->width(), image->height(), kN32_SkColorType,
1387 image->alphaType()), (const void*) &pixels.front(), image->width() * 4);
1388 image->readPixels(pixmap, 0, 0);
1389 for (int y = 0; y < pixmap.height() / 2; ++y) {
1390 for (int x = 0; x < pixmap.width(); ++x) {
1391 if ((x & 4) == (y & 4)) {
1392 SkTSwap(*pixmap.writable_addr32(x, y),
1393 *pixmap.writable_addr32(pixmap.width() - x, pixmap.height() - y));
1394 }
1395 }
1396 }
1397 SkBitmap bitmap;
1398 bitmap.installPixels(pixmap);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001399 canvas->drawBitmap(bitmap, 0, 0);
1400##
1401
1402#SeeAlso writable_addr writable_addr8 writable_addr16 writable_addr64 writable_addrF16 addr() addr32
1403
1404##
1405
1406# ------------------------------------------------------------------------------
1407
1408#Method uint64_t* writable_addr64(int x, int y) const
1409
Cary Clarkab2621d2018-01-30 10:08:57 -05001410#In Writable_Address
1411#Line # returns writable pixel address as 64-bit pointer ##
Cary Clark6fc50412017-09-21 12:31:06 -04001412Returns writable pixel address at (x, y). Result is addressable as unsigned
141364-bit words. Will trigger an assert() if Color_Type is not
1414kRGBA_F16_SkColorType and is built with SK_DEBUG defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001415
1416One word corresponds to one pixel.
1417
Cary Clark6fc50412017-09-21 12:31:06 -04001418#Param x column index, zero or greater, and less than width() ##
1419#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001420
1421#Return writable unsigned 64-bit pointer to pixel ##
1422
1423#Example
Cary Clark2ade9972017-11-02 17:49:34 -04001424#Height 40
Cary Clark6fc50412017-09-21 12:31:06 -04001425 SkImageInfo info = SkImageInfo::Make(3, 3, kRGBA_F16_SkColorType, kPremul_SkAlphaType);
1426 uint64_t storage[9];
1427 SkPixmap pixmap(info, storage, 3 * sizeof(uint64_t));
1428 SkColor4f c4 { 1, 0.45f, 0.25f, 0.65f };
1429 pixmap.erase(c4);
1430 SkBitmap bitmap;
1431 canvas->scale(10, 10);
1432 bitmap.installPixels(pixmap);
1433 canvas->drawBitmap(bitmap, 0, 0);
1434 *pixmap.writable_addr64(1, 1) |= 0x00ff000000000000LL;
1435 bitmap.installPixels(pixmap);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001436 canvas->drawBitmap(bitmap, 10, 0);
1437##
1438
1439#SeeAlso writable_addr writable_addr8 writable_addr16 writable_addr32 writable_addrF16 addr() addr64
1440
1441##
1442
1443# ------------------------------------------------------------------------------
1444
1445#Method uint16_t* writable_addrF16(int x, int y) const
1446
Cary Clarkab2621d2018-01-30 10:08:57 -05001447#In Writable_Address
1448#Line # returns writable pixel component address as 16-bit pointer ##
Cary Clark6fc50412017-09-21 12:31:06 -04001449Returns writable pixel address at (x, y). Result is addressable as unsigned
145016-bit words. Will trigger an assert() if Color_Type is not
1451kRGBA_F16_SkColorType and is built with SK_DEBUG defined.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001452
1453Each word represents one color component encoded as a half float.
1454Four words correspond to one pixel.
1455
Cary Clark6fc50412017-09-21 12:31:06 -04001456#Param x column index, zero or greater, and less than width() ##
1457#Param y row index, zero or greater, and less than height() ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001458
1459#Return writable unsigned 16-bit pointer to first component of pixel ##
1460
1461#Example
1462#Height 64
1463#Description
1464Left bitmap is drawn with two pixels defined in half float format. Right bitmap
1465is drawn after overwriting bottom half float color with top half float color.
1466##
Cary Clark6fc50412017-09-21 12:31:06 -04001467 SkImageInfo info = SkImageInfo::Make(1, 2, kRGBA_F16_SkColorType, kPremul_SkAlphaType);
1468 uint16_t storage[2][4];
1469 SkPixmap pixmap(info, storage[0], sizeof(uint64_t));
1470 SkIRect topPixelBounds = {0, 0, 1, 1};
1471 pixmap.erase({ 0.65f, 0.45f, 0.25f, 1 }, &topPixelBounds);
1472 SkIRect bottomPixelBounds = {0, 1, 1, 2};
1473 pixmap.erase({ 0.25f, 0.65f, 0.45f, 1 }, &bottomPixelBounds);
1474 SkBitmap bitmap;
1475 canvas->scale(20, 20);
1476 bitmap.installPixels(pixmap);
1477 canvas->drawBitmap(bitmap, 0, 0);
1478 uint16_t* pixel2 = pixmap.writable_addrF16(0, 1);
1479 for (int i = 0; i < 4; ++i) {
1480 pixel2[i] = storage[0][i];
1481 }
1482 bitmap.installPixels(pixmap);
1483 canvas->drawBitmap(bitmap, 4, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001484##
1485
1486#SeeAlso writable_addr writable_addr8 writable_addr16 writable_addr32 writable_addr64 addr() addrF16
1487
1488##
1489
1490#Subtopic Writable_Address ##
1491
Cary Clark78de7512018-02-07 07:27:09 -05001492#Subtopic Pixels
1493#Populate
1494#Line # read and write pixel values ##
1495##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001496
1497# ------------------------------------------------------------------------------
1498
1499#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1500 int srcX, int srcY, SkTransferFunctionBehavior behavior) const
Cary Clark78de7512018-02-07 07:27:09 -05001501#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05001502#Line # copies and converts pixels ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001503
Cary Clark154beea2017-10-26 07:58:48 -04001504Copies a Rect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not
Cary Clarkac47b882018-01-11 10:35:44 -05001505exceed Pixmap (width(), height()).
Cary Clark6fc50412017-09-21 12:31:06 -04001506
1507dstInfo specifies width, height, Color_Type, Alpha_Type, and
Cary Clarkd0530ba2017-09-14 11:25:39 -04001508Color_Space of destination. dstRowBytes specifics the gap from one destination
1509row to the next. Returns true if pixels are copied. Returns false if
1510dstInfo.addr() equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes.
1511
Cary Clarkac47b882018-01-11 10:35:44 -05001512Pixels are copied only if pixel conversion is possible. If Pixmap colorType is
Cary Clarkd0530ba2017-09-14 11:25:39 -04001513kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
Cary Clarkac47b882018-01-11 10:35:44 -05001514If Pixmap colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
1515If Pixmap alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
1516match. If Pixmap colorSpace is nullptr, dstInfo.colorSpace must match. Returns
Cary Clarkd0530ba2017-09-14 11:25:39 -04001517false if pixel conversion is not possible.
1518
1519srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark154beea2017-10-26 07:58:48 -04001520false if width() or height() is zero or negative. Returns false if:
1521
Cary Clarkd0530ba2017-09-14 11:25:39 -04001522#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05001523abs(srcX) >= Pixmap width()
Cary Clark6fc50412017-09-21 12:31:06 -04001524##
1525, or if
1526#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05001527abs(srcY) >= Pixmap height()
Cary Clarkd0530ba2017-09-14 11:25:39 -04001528##
1529.
1530
1531If behavior is SkTransferFunctionBehavior::kRespect: converts source
1532pixels to a linear space before converting to dstInfo.
1533If behavior is SkTransferFunctionBehavior::kIgnore: source
Cary Clarkbc5697d2017-10-04 14:31:33 -04001534pixels are treated as if they are linear, regardless of how they are encoded.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001535
1536#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1537#Param dstPixels destination pixel storage ##
1538#Param dstRowBytes destination row length ##
1539#Param srcX column index whose absolute value is less than width() ##
1540#Param srcY row index whose absolute value is less than height() ##
1541#Param behavior one of: SkTransferFunctionBehavior::kRespect,
1542 SkTransferFunctionBehavior::kIgnore
1543##
1544
1545#Return true if pixels are copied to dstPixels ##
1546
1547#Example
Cary Clark681287e2018-03-16 11:34:15 -04001548#ToDo example doesn't do anything interesting since image doesn't have alpha ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001549#Image 3
1550void draw(SkCanvas* canvas) {
Cary Clark681287e2018-03-16 11:34:15 -04001551 SkImageInfo srgb = SkImageInfo::MakeS32(image->width(), image->height(),
1552 kPremul_SkAlphaType);
1553 SkImageInfo linear = srgb.makeColorSpace(srgb.colorSpace()->makeLinearGamma());
Cary Clark6fc50412017-09-21 12:31:06 -04001554 std::vector<int32_t> srcPixels;
Cary Clark681287e2018-03-16 11:34:15 -04001555 size_t rowBytes = image->width() * 4;
1556 srcPixels.resize(image->height() * rowBytes);
1557 SkPixmap pixmap(srgb, (const void*) &srcPixels.front(), rowBytes);
Cary Clark6fc50412017-09-21 12:31:06 -04001558 image->readPixels(pixmap, 0, 0);
Cary Clark681287e2018-03-16 11:34:15 -04001559 int offset = -64;
Cary Clark6fc50412017-09-21 12:31:06 -04001560 std::vector<int32_t> dstPixels;
Cary Clark681287e2018-03-16 11:34:15 -04001561 dstPixels.resize(image->height() * rowBytes);
1562 for (const auto& info : { srgb, linear } ) {
1563 for (auto behavior : { SkTransferFunctionBehavior::kRespect,
1564 SkTransferFunctionBehavior::kIgnore} ) {
1565 pixmap.readPixels(info, &dstPixels.front(), rowBytes, 0, 0, behavior);
1566 SkBitmap bitmap;
1567 SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
1568 bitmap.installPixels(dstmap);
1569 canvas->drawBitmap(bitmap, 0, offset += 64);
1570 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001571 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001572}
1573##
1574
1575#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1576
1577##
1578
1579# ------------------------------------------------------------------------------
1580
1581#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes) const
1582
Cary Clark78de7512018-02-07 07:27:09 -05001583#In Pixels
Cary Clarkd0530ba2017-09-14 11:25:39 -04001584Copies a Rect of pixels to dstPixels. Copy starts at (0, 0), and does not
Cary Clarkac47b882018-01-11 10:35:44 -05001585exceed Pixmap (width(), height()).
Cary Clark6fc50412017-09-21 12:31:06 -04001586
1587dstInfo specifies width, height, Color_Type, Alpha_Type, and
Cary Clarkd0530ba2017-09-14 11:25:39 -04001588Color_Space of destination. dstRowBytes specifics the gap from one destination
1589row to the next. Returns true if pixels are copied. Returns false if
1590dstInfo.addr() equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes.
1591
Cary Clarkac47b882018-01-11 10:35:44 -05001592Pixels are copied only if pixel conversion is possible. If Pixmap colorType is
Cary Clarkd0530ba2017-09-14 11:25:39 -04001593kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
Cary Clarkac47b882018-01-11 10:35:44 -05001594If Pixmap colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
1595If Pixmap alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
1596match. If Pixmap colorSpace is nullptr, dstInfo.colorSpace must match. Returns
Cary Clarkd0530ba2017-09-14 11:25:39 -04001597false if pixel conversion is not possible.
1598
Cary Clarkac47b882018-01-11 10:35:44 -05001599Returns false if Pixmap width() or height() is zero or negative.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001600
1601#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1602#Param dstPixels destination pixel storage ##
1603#Param dstRowBytes destination row length ##
1604
1605#Return true if pixels are copied to dstPixels ##
1606
1607#Example
1608#Height 128
1609#Description
1610Transferring the gradient from 8 bits per component to 4 bits per component
1611creates visible banding.
1612##
Cary Clark6fc50412017-09-21 12:31:06 -04001613 std::vector<int32_t> pixels;
1614 const int width = 256;
1615 const int height = 64;
1616 pixels.resize(height * width * 4);
1617 SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(width, height);
1618 SkPixmap srcPixmap(srcInfo, (const void*) &pixels.front(), width * 4);
1619 SkColor gradColors[] = { 0xFFAA3300, 0x7F881122 };
1620 SkPoint gradPoints[] = { { 0, 0 }, { 256, 0 } };
1621 SkPaint paint;
1622 paint.setShader(SkGradientShader::MakeLinear(gradPoints, gradColors, nullptr,
1623 SK_ARRAY_COUNT(gradColors), SkShader::kClamp_TileMode));
1624 SkBitmap bitmap;
1625 bitmap.installPixels(srcPixmap);
1626 SkCanvas srcCanvas(bitmap);
1627 srcCanvas.drawRect(SkRect::MakeWH(width, height), paint);
1628 canvas->drawBitmap(bitmap, 0, 0);
1629 std::vector<int32_t> dstPixels;
1630 dstPixels.resize(height * width * 2);
1631 SkImageInfo dstInfo = srcInfo.makeColorType(kARGB_4444_SkColorType);
1632 srcPixmap.readPixels(dstInfo, &dstPixels.front(), width * 2);
1633 SkPixmap dstPixmap(dstInfo, &dstPixels.front(), width * 2);
1634 bitmap.installPixels(dstPixmap);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001635 canvas->drawBitmap(bitmap, 0, 128);
1636##
1637
1638#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1639
1640##
1641
1642# ------------------------------------------------------------------------------
1643
1644#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, int srcX,
1645 int srcY) const
1646
1647Copies a Rect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not
Cary Clarkac47b882018-01-11 10:35:44 -05001648exceed Pixmap (width(), height()).
Cary Clark6fc50412017-09-21 12:31:06 -04001649
1650dstInfo specifies width, height, Color_Type, Alpha_Type, and
Cary Clarkd0530ba2017-09-14 11:25:39 -04001651Color_Space of destination. dstRowBytes specifics the gap from one destination
1652row to the next. Returns true if pixels are copied. Returns false if
1653dstInfo.addr() equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes.
1654
Cary Clarkac47b882018-01-11 10:35:44 -05001655Pixels are copied only if pixel conversion is possible. If Pixmap colorType is
Cary Clarkd0530ba2017-09-14 11:25:39 -04001656kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
Cary Clarkac47b882018-01-11 10:35:44 -05001657If Pixmap colorType is kGray_8_SkColorType, dstInfo.colorSpace must match.
1658If Pixmap alphaType is kOpaque_SkAlphaType, dstInfo.alphaType must
1659match. If Pixmap colorSpace is nullptr, dstInfo.colorSpace must match. Returns
Cary Clarkd0530ba2017-09-14 11:25:39 -04001660false if pixel conversion is not possible.
1661
1662srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clarkac47b882018-01-11 10:35:44 -05001663false if Pixmap width() or height() is zero or negative. Returns false if:
Cary Clark154beea2017-10-26 07:58:48 -04001664
Cary Clarkd0530ba2017-09-14 11:25:39 -04001665#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05001666abs(srcX) >= Pixmap width()
Cary Clark6fc50412017-09-21 12:31:06 -04001667##
1668, or if
1669#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05001670abs(srcY) >= Pixmap height()
Cary Clarkd0530ba2017-09-14 11:25:39 -04001671##
1672.
1673
1674#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1675#Param dstPixels destination pixel storage ##
1676#Param dstRowBytes destination row length ##
1677#Param srcX column index whose absolute value is less than width() ##
1678#Param srcY row index whose absolute value is less than height() ##
1679
1680#Return true if pixels are copied to dstPixels ##
1681
1682#Example
1683#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -04001684void draw(SkCanvas* canvas) {
1685 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
1686 std::vector<int32_t> srcPixels;
1687 const int rowBytes = image->width() * 4;
1688 srcPixels.resize(image->height() * rowBytes);
1689 SkPixmap pixmap(info, (const void*) &srcPixels.front(), rowBytes);
1690 image->readPixels(pixmap, 0, 0);
1691 for (int offset : { 32, 64, 96 } ) {
1692 std::vector<int32_t> dstPixels;
1693 dstPixels.resize(image->height() * rowBytes);
1694 pixmap.readPixels(info, &dstPixels.front(), rowBytes, offset, 0);
1695 SkBitmap bitmap;
1696 SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
1697 bitmap.installPixels(dstmap);
1698 canvas->translate(32, 32);
1699 canvas->drawBitmap(bitmap, 0, 0);
1700 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001701}
1702##
1703
1704#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1705
1706##
1707
1708# ------------------------------------------------------------------------------
1709
1710#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY) const
1711
1712Copies a Rect of pixels to dst. Copy starts at (srcX, srcY), and does not
Cary Clarkac47b882018-01-11 10:35:44 -05001713exceed Pixmap (width(), height()). dst specifies width, height, Color_Type,
Cary Clarkd0530ba2017-09-14 11:25:39 -04001714Alpha_Type, and Color_Space of destination. Returns true if pixels are copied.
1715Returns false if dst.addr() equals nullptr, or dst.rowBytes is less than
1716dst SkImageInfo::minRowBytes.
1717
Cary Clarkac47b882018-01-11 10:35:44 -05001718Pixels are copied only if pixel conversion is possible. If Pixmap colorType is
Cary Clarkd0530ba2017-09-14 11:25:39 -04001719kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.info().colorType must match.
Cary Clarkac47b882018-01-11 10:35:44 -05001720If Pixmap colorType is kGray_8_SkColorType, dst.info().colorSpace must match.
1721If Pixmap alphaType is kOpaque_SkAlphaType, dst.info().alphaType must
1722match. If Pixmap colorSpace is nullptr, dst.info().colorSpace must match. Returns
Cary Clarkd0530ba2017-09-14 11:25:39 -04001723false if pixel conversion is not possible.
1724
1725srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clarkac47b882018-01-11 10:35:44 -05001726false Pixmap width() or height() is zero or negative. Returns false if:
Cary Clark154beea2017-10-26 07:58:48 -04001727
Cary Clarkd0530ba2017-09-14 11:25:39 -04001728#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05001729abs(srcX) >= Pixmap width()
Cary Clark6fc50412017-09-21 12:31:06 -04001730##
1731, or if
1732#Formula
Cary Clarkac47b882018-01-11 10:35:44 -05001733abs(srcY) >= Pixmap height()
Cary Clarkd0530ba2017-09-14 11:25:39 -04001734##
1735.
1736
1737#Param dst Image_Info and pixel address to write to ##
1738#Param srcX column index whose absolute value is less than width() ##
1739#Param srcY row index whose absolute value is less than height() ##
1740
1741#Return true if pixels are copied to dst ##
1742
1743#Example
1744#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -04001745void draw(SkCanvas* canvas) {
1746 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
1747 std::vector<int32_t> srcPixels;
1748 const int rowBytes = image->width() * 4;
1749 srcPixels.resize(image->height() * rowBytes);
1750 SkPixmap pixmap(info, (const void*) &srcPixels.front(), rowBytes);
1751 image->readPixels(pixmap, 0, 0);
1752 for (int offset : { 32, 64, 96 } ) {
1753 std::vector<int32_t> dstPixels;
1754 dstPixels.resize(image->height() * rowBytes);
1755 SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
1756 pixmap.readPixels(dstmap, offset, 0);
1757 SkBitmap bitmap;
1758 bitmap.installPixels(dstmap);
1759 canvas->translate(32, 32);
1760 canvas->drawBitmap(bitmap, 0, 0);
1761 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001762}
1763##
1764
1765#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1766
1767##
1768
1769# ------------------------------------------------------------------------------
1770
1771#Method bool readPixels(const SkPixmap& dst) const
1772
1773Copies pixels inside bounds() to dst. dst specifies width, height, Color_Type,
1774Alpha_Type, and Color_Space of destination. Returns true if pixels are copied.
1775Returns false if dst.addr() equals nullptr, or dst.rowBytes is less than
1776dst SkImageInfo::minRowBytes.
1777
Cary Clarkac47b882018-01-11 10:35:44 -05001778Pixels are copied only if pixel conversion is possible. If Pixmap colorType is
Cary Clarkd0530ba2017-09-14 11:25:39 -04001779kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05001780If Pixmap colorType is kGray_8_SkColorType, dst Color_Space must match.
1781If Pixmap alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
1782match. If Pixmap colorSpace is nullptr, dst Color_Space must match. Returns
Cary Clarkd0530ba2017-09-14 11:25:39 -04001783false if pixel conversion is not possible.
1784
Cary Clarkac47b882018-01-11 10:35:44 -05001785Returns false if Pixmap width() or height() is zero or negative.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001786
1787#Param dst Image_Info and pixel address to write to ##
1788
1789#Return true if pixels are copied to dst ##
1790
1791#Example
1792#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -04001793void draw(SkCanvas* canvas) {
1794 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
1795 std::vector<int32_t> srcPixels;
1796 const int rowBytes = image->width() * 4;
1797 srcPixels.resize(image->height() * rowBytes);
1798 SkPixmap pixmap(info, (const void*) &srcPixels.front(), rowBytes);
1799 image->readPixels(pixmap, 0, 0);
1800 for (int index = 0; index < 3; ++index ) {
1801 std::vector<int32_t> dstPixels;
1802 dstPixels.resize(image->height() * rowBytes);
1803 SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
1804 pixmap.readPixels(dstmap);
1805 SkBitmap bitmap;
1806 bitmap.installPixels(dstmap);
1807 canvas->translate(32, 32);
1808 canvas->drawBitmap(bitmap, 0, 0);
1809 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001810}
1811##
1812
1813#SeeAlso erase SkBitmap::readPixels SkCanvas::drawBitmap SkCanvas::readPixels SkImage::readPixels SkSurface::readPixels
1814
1815##
1816
1817# ------------------------------------------------------------------------------
1818
1819#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality) const
1820
Cary Clark78de7512018-02-07 07:27:09 -05001821#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05001822#Line # scales and converts pixels ##
Cary Clarkac47b882018-01-11 10:35:44 -05001823Copies Bitmap to dst, scaling pixels to fit dst.width() and dst.height(), and
Cary Clarkd0530ba2017-09-14 11:25:39 -04001824converting pixels to match dst.colorType and dst.alphaType. Returns true if
1825pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1826less than dst SkImageInfo::minRowBytes.
1827
Cary Clarkac47b882018-01-11 10:35:44 -05001828Pixels are copied only if pixel conversion is possible. If Pixmap colorType is
Cary Clarkd0530ba2017-09-14 11:25:39 -04001829kGray_8_SkColorType, or kAlpha_8_SkColorType; dst Color_Type must match.
Cary Clarkac47b882018-01-11 10:35:44 -05001830If Pixmap colorType is kGray_8_SkColorType, dst Color_Space must match.
1831If Pixmap alphaType is kOpaque_SkAlphaType, dst Alpha_Type must
1832match. If Pixmap colorSpace is nullptr, dst Color_Space must match. Returns
Cary Clarkd0530ba2017-09-14 11:25:39 -04001833false if pixel conversion is not possible.
1834
Cary Clarkac47b882018-01-11 10:35:44 -05001835Returns false if Bitmap width() or height() is zero or negative.
Cary Clarkd0530ba2017-09-14 11:25:39 -04001836
1837Scales the image, with filterQuality, to match dst.width() and dst.height().
1838filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1839Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1840Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1841Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1842kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1843
1844#Param dst Image_Info and pixel address to write to ##
1845#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1846 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1847##
1848
Cary Clarkac47b882018-01-11 10:35:44 -05001849#Return true if pixels are scaled to fit dst ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001850
1851#Example
1852#Image 3
Cary Clark6fc50412017-09-21 12:31:06 -04001853void draw(SkCanvas* canvas) {
1854 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
1855 std::vector<int32_t> srcPixels;
1856 int rowBytes = image->width() * 4;
1857 srcPixels.resize(image->height() * rowBytes);
1858 SkPixmap pixmap(info, (const void*) &srcPixels.front(), rowBytes);
1859 image->readPixels(pixmap, 0, 0);
1860 for (int offset : { 32, 64, 96 } ) {
1861 info = SkImageInfo::MakeN32Premul(image->width() + offset, image->height());
1862 rowBytes = info.width() * 4;
1863 std::vector<int32_t> dstPixels;
1864 dstPixels.resize(image->height() * rowBytes);
1865 SkPixmap dstmap(info, &dstPixels.front(), rowBytes);
1866 pixmap.scalePixels(dstmap, kMedium_SkFilterQuality);
1867 SkBitmap bitmap;
1868 bitmap.installPixels(dstmap);
1869 canvas->translate(32, 32);
1870 canvas->drawBitmap(bitmap, 0, 0);
1871 }
Cary Clarkd0530ba2017-09-14 11:25:39 -04001872}
1873##
1874
1875#SeeAlso SkCanvas::drawBitmap SkImage::scalePixels
1876
1877##
1878
1879# ------------------------------------------------------------------------------
1880
1881#Method bool erase(SkColor color, const SkIRect& subset) const
1882
Cary Clark78de7512018-02-07 07:27:09 -05001883#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05001884#Line # writes Color to pixels ##
Cary Clarkd0530ba2017-09-14 11:25:39 -04001885Writes color to pixels bounded by subset; returns true on success.
1886Returns false if colorType is kUnknown_SkColorType, or if subset does
1887not intersect bounds().
1888
1889#Param color Unpremultiplied Color to write ##
1890#Param subset bounding integer Rect of written pixels ##
1891
1892#Return true if pixels are changed ##
1893
1894#Example
Cary Clark2ade9972017-11-02 17:49:34 -04001895#Height 50
Cary Clark6fc50412017-09-21 12:31:06 -04001896 uint32_t storage[2];
1897 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 2);
1898 SkPixmap pixmap(info, storage, info.minRowBytes());
1899 pixmap.erase(SK_ColorBLUE, {0, 0, 1, 1});
1900 pixmap.erase(SK_ColorRED, {0, 1, 1, 2});
1901 SkBitmap bitmap;
1902 canvas->scale(20, 20);
1903 bitmap.installPixels(pixmap);
1904 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001905##
1906
1907#SeeAlso SkBitmap::erase SkCanvas::clear SkCanvas::drawColor
1908
1909##
1910
1911# ------------------------------------------------------------------------------
1912
1913#Method bool erase(SkColor color) const
1914
1915Writes color to pixels inside bounds(); returns true on success.
1916Returns false if colorType is kUnknown_SkColorType, or if bounds()
1917is empty.
1918
1919#Param color Unpremultiplied Color to write ##
1920
1921#Return true if pixels are changed ##
1922
1923#Example
Cary Clark2ade9972017-11-02 17:49:34 -04001924#Height 50
Cary Clark6fc50412017-09-21 12:31:06 -04001925 uint32_t storage[2];
1926 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 2);
1927 SkPixmap pixmap(info, storage, info.minRowBytes());
1928 pixmap.erase(SK_ColorBLUE);
1929 SkBitmap bitmap;
1930 canvas->scale(20, 20);
1931 bitmap.installPixels(pixmap);
1932 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001933##
1934
1935#SeeAlso SkBitmap::erase SkCanvas::clear SkCanvas::drawColor
1936
1937##
1938
1939# ------------------------------------------------------------------------------
1940
1941#Method bool erase(const SkColor4f& color, const SkIRect* subset = nullptr) const
1942
1943Writes color to pixels bounded by subset; returns true on success.
1944if subset is nullptr, writes colors pixels inside bounds(). Returns false if
1945colorType is kUnknown_SkColorType, if subset is not nullptr and does
1946not intersect bounds(), or if subset is nullptr and bounds() is empty.
1947
1948#Param color Unpremultiplied Color to write ##
1949#Param subset bounding integer Rect of pixels to write; may be nullptr ##
1950
1951#Return true if pixels are changed ##
1952
1953#Example
Cary Clark2ade9972017-11-02 17:49:34 -04001954#Height 50
Cary Clark6fc50412017-09-21 12:31:06 -04001955 uint32_t storage[2];
1956 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 2);
1957 SkPixmap pixmap(info, storage, info.minRowBytes());
1958 SkIRect topPixelBounds = {0, 0, 1, 1};
1959 pixmap.erase({ 0.65f, 0.45f, 0.25f, 1 }, &topPixelBounds);
1960 SkIRect bottomPixelBounds = {0, 1, 1, 2};
1961 pixmap.erase({ 0.25f, 0.65f, 0.45f, 1 }, &bottomPixelBounds);
1962 SkBitmap bitmap;
1963 canvas->scale(20, 20);
1964 bitmap.installPixels(pixmap);
1965 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarkd0530ba2017-09-14 11:25:39 -04001966##
1967
1968#SeeAlso SkBitmap::erase SkCanvas::clear SkCanvas::drawColor
1969
1970##
1971
Cary Clarkd0530ba2017-09-14 11:25:39 -04001972#Class SkPixmap ##
1973
1974#Topic Pixmap ##