SkPixmap.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. /* Generated by tools/bookmaker from include/core/SkPixmap.h and docs/SkPixmap_Reference.bmh
  8. on 2018-06-08 11:48:28. Additional documentation and examples can be found at:
  9. https://skia.org/user/api/SkPixmap_Reference
  10. You may edit either file directly. Structural changes to public interfaces require
  11. editing both files. After editing docs/SkPixmap_Reference.bmh, run:
  12. bookmaker -b docs -i include/core/SkPixmap.h -p
  13. to create an updated version of this file.
  14. */
  15. #ifndef SkPixmap_DEFINED
  16. #define SkPixmap_DEFINED
  17. #include "SkColor.h"
  18. #include "SkFilterQuality.h"
  19. #include "SkImageInfo.h"
  20. class SkData;
  21. struct SkMask;
  22. /** \class SkPixmap
  23. SkPixmap provides a utility to pair SkImageInfo with pixels and row bytes.
  24. SkPixmap is a low level class which provides convenience functions to access
  25. raster destinations. SkCanvas can not draw SkPixmap, nor does SkPixmap provide
  26. a direct drawing destination.
  27. Use SkBitmap to draw pixels referenced by SkPixmap; use SkSurface to draw into
  28. pixels referenced by SkPixmap.
  29. SkPixmap does not try to manage the lifetime of the pixel memory. Use SkPixelRef
  30. to manage pixel memory; SkPixelRef is safe across threads.
  31. */
  32. class SK_API SkPixmap {
  33. public:
  34. /** Creates an empty SkPixmap without pixels, with kUnknown_SkColorType, with
  35. kUnknown_SkAlphaType, and with a width and height of zero. Use
  36. reset() to associate pixels, SkColorType, SkAlphaType, width, and height
  37. after SkPixmap has been created.
  38. @return empty SkPixmap
  39. */
  40. SkPixmap()
  41. : fPixels(nullptr), fRowBytes(0), fInfo(SkImageInfo::MakeUnknown(0, 0))
  42. {}
  43. /** Creates SkPixmap from info width, height, SkAlphaType, and SkColorType.
  44. addr points to pixels, or nullptr. rowBytes should be info.width() times
  45. info.bytesPerPixel(), or larger.
  46. No parameter checking is performed; it is up to the caller to ensure that
  47. addr and rowBytes agree with info.
  48. The memory lifetime of pixels is managed by the caller. When SkPixmap goes
  49. out of scope, addr is unaffected.
  50. SkPixmap may be later modified by reset() to change its size, pixel type, or
  51. storage.
  52. @param info width, height, SkAlphaType, SkColorType of SkImageInfo
  53. @param addr pointer to pixels allocated by caller; may be nullptr
  54. @param rowBytes size of one row of addr; width times pixel size, or larger
  55. @return initialized SkPixmap
  56. */
  57. SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes)
  58. : fPixels(addr), fRowBytes(rowBytes), fInfo(info)
  59. {}
  60. /** Sets width, height, row bytes to zero; pixel address to nullptr; SkColorType to
  61. kUnknown_SkColorType; and SkAlphaType to kUnknown_SkAlphaType.
  62. The prior pixels are unaffected; it is up to the caller to release pixels
  63. memory if desired.
  64. */
  65. void reset();
  66. /** Sets width, height, SkAlphaType, and SkColorType from info.
  67. Sets pixel address from addr, which may be nullptr.
  68. Sets row bytes from rowBytes, which should be info.width() times
  69. info.bytesPerPixel(), or larger.
  70. Does not check addr. Asserts if built with SK_DEBUG defined and if rowBytes is
  71. too small to hold one row of pixels.
  72. The memory lifetime pixels are managed by the caller. When SkPixmap goes
  73. out of scope, addr is unaffected.
  74. @param info width, height, SkAlphaType, SkColorType of SkImageInfo
  75. @param addr pointer to pixels allocated by caller; may be nullptr
  76. @param rowBytes size of one row of addr; width times pixel size, or larger
  77. */
  78. void reset(const SkImageInfo& info, const void* addr, size_t rowBytes);
  79. /** Changes SkColorSpace in SkImageInfo; preserves width, height, SkAlphaType, and
  80. SkColorType in SkImage, and leaves pixel address and row bytes unchanged.
  81. SkColorSpace reference count is incremented.
  82. @param colorSpace SkColorSpace moved to SkImageInfo
  83. */
  84. void setColorSpace(sk_sp<SkColorSpace> colorSpace);
  85. /** Deprecated.
  86. */
  87. bool SK_WARN_UNUSED_RESULT reset(const SkMask& mask);
  88. /** Sets subset width, height, pixel address to intersection of SkPixmap with area,
  89. if intersection is not empty; and return true. Otherwise, leave subset unchanged
  90. and return false.
  91. Failing to read the return value generates a compile time warning.
  92. @param subset storage for width, height, pixel address of intersection
  93. @param area bounds to intersect with SkPixmap
  94. @return true if intersection of SkPixmap and area is not empty
  95. */
  96. bool SK_WARN_UNUSED_RESULT extractSubset(SkPixmap* subset, const SkIRect& area) const;
  97. /** Returns width, height, SkAlphaType, SkColorType, and SkColorSpace.
  98. @return reference to SkImageInfo
  99. */
  100. const SkImageInfo& info() const { return fInfo; }
  101. /** Returns row bytes, the interval from one pixel row to the next. Row bytes
  102. is at least as large as: width() * info().bytesPerPixel().
  103. Returns zero if colorType() is kUnknown_SkColorType.
  104. It is up to the SkBitmap creator to ensure that row bytes is a useful value.
  105. @return byte length of pixel row
  106. */
  107. size_t rowBytes() const { return fRowBytes; }
  108. /** Returns pixel address, the base address corresponding to the pixel origin.
  109. It is up to the SkPixmap creator to ensure that pixel address is a useful value.
  110. @return pixel address
  111. */
  112. const void* addr() const { return fPixels; }
  113. /** Returns pixel count in each pixel row. Should be equal or less than:
  114. rowBytes() / info().bytesPerPixel().
  115. @return pixel width in SkImageInfo
  116. */
  117. int width() const { return fInfo.width(); }
  118. /** Returns pixel row count.
  119. @return pixel height in SkImageInfo
  120. */
  121. int height() const { return fInfo.height(); }
  122. /** Returns SkColorType, one of:
  123. kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
  124. kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kRGB_888x_SkColorType,
  125. kBGRA_8888_SkColorType, kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType,
  126. kGray_8_SkColorType, kRGBA_F16_SkColorType.
  127. @return SkColorType in SkImageInfo
  128. */
  129. SkColorType colorType() const { return fInfo.colorType(); }
  130. /** Returns SkAlphaType, one of:
  131. kUnknown_SkAlphaType, kOpaque_SkAlphaType, kPremul_SkAlphaType,
  132. kUnpremul_SkAlphaType.
  133. @return SkAlphaType in SkImageInfo
  134. */
  135. SkAlphaType alphaType() const { return fInfo.alphaType(); }
  136. /** Returns SkColorSpace, the range of colors, associated with SkImageInfo. The
  137. reference count of SkColorSpace is unchanged. The returned SkColorSpace is
  138. immutable.
  139. @return SkColorSpace in SkImageInfo, or nullptr
  140. */
  141. SkColorSpace* colorSpace() const { return fInfo.colorSpace(); }
  142. /** Returns true if SkAlphaType is kOpaque_SkAlphaType.
  143. Does not check if SkColorType allows alpha, or if any pixel value has
  144. transparency.
  145. @return true if SkImageInfo has opaque SkAlphaType
  146. */
  147. bool isOpaque() const { return fInfo.isOpaque(); }
  148. /** Returns SkIRect { 0, 0, width(), height() }.
  149. @return integral rectangle from origin to width() and height()
  150. */
  151. SkIRect bounds() const { return SkIRect::MakeWH(this->width(), this->height()); }
  152. /** Returns number of pixels that fit on row. Should be greater than or equal to
  153. width().
  154. @return maximum pixels per row
  155. */
  156. int rowBytesAsPixels() const { return int(fRowBytes >> this->shiftPerPixel()); }
  157. /** Returns bit shift converting row bytes to row pixels.
  158. Returns zero for kUnknown_SkColorType.
  159. @return one of: 0, 1, 2, 3; left shift to convert pixels to bytes
  160. */
  161. int shiftPerPixel() const { return fInfo.shiftPerPixel(); }
  162. /** Returns minimum memory required for pixel storage.
  163. Does not include unused memory on last row when rowBytesAsPixels() exceeds width().
  164. Returns zero if result does not fit in size_t.
  165. Returns zero if height() or width() is 0.
  166. Returns height() times rowBytes() if colorType() is kUnknown_SkColorType.
  167. @return size in bytes of image buffer
  168. */
  169. size_t computeByteSize() const { return fInfo.computeByteSize(fRowBytes); }
  170. /** Returns true if all pixels are opaque. SkColorType determines how pixels
  171. are encoded, and whether pixel describes alpha. Returns true for SkColorType
  172. without alpha in each pixel; for other SkColorType, returns true if all
  173. pixels have alpha values equivalent to 1.0 or greater.
  174. For SkColorType kRGB_565_SkColorType or kGray_8_SkColorType: always
  175. returns true. For SkColorType kAlpha_8_SkColorType, kBGRA_8888_SkColorType,
  176. kRGBA_8888_SkColorType: returns true if all pixel alpha values are 255.
  177. For SkColorType kARGB_4444_SkColorType: returns true if all pixel alpha values are 15.
  178. For kRGBA_F16_SkColorType: returns true if all pixel alpha values are 1.0 or
  179. greater.
  180. Returns false for kUnknown_SkColorType.
  181. @return true if all pixels have opaque values or SkColorType is opaque
  182. */
  183. bool computeIsOpaque() const;
  184. /** Returns pixel at (x, y) as unpremultiplied color.
  185. Returns black with alpha if SkColorType is kAlpha_8_SkColorType.
  186. Input is not validated: out of bounds values of x or y trigger an assert() if
  187. built with SK_DEBUG defined; and returns undefined values or may crash if
  188. SK_RELEASE is defined. Fails if SkColorType is kUnknown_SkColorType or
  189. pixel address is nullptr.
  190. SkColorSpace in SkImageInfo is ignored. Some color precision may be lost in the
  191. conversion to unpremultiplied color; original pixel data may have additional
  192. precision.
  193. @param x column index, zero or greater, and less than width()
  194. @param y row index, zero or greater, and less than height()
  195. @return pixel converted to unpremultiplied color
  196. */
  197. SkColor getColor(int x, int y) const;
  198. /** Look up the pixel at (x,y) and return its alpha component, normalized to [0..1].
  199. This is roughly equivalent to SkGetColorA(getColor()), but can be more efficent
  200. (and more precise if the pixels store more than 8 bits per component).
  201. @param x column index, zero or greater, and less than width()
  202. @param y row index, zero or greater, and less than height()
  203. @return alpha converted to normalized float
  204. */
  205. float getAlphaf(int x, int y) const;
  206. /** Returns readable pixel address at (x, y). Returns nullptr if SkPixelRef is nullptr.
  207. Input is not validated: out of bounds values of x or y trigger an assert() if
  208. built with SK_DEBUG defined. Returns nullptr if SkColorType is kUnknown_SkColorType.
  209. Performs a lookup of pixel size; for better performance, call
  210. one of: addr8, addr16, addr32, addr64, or addrF16().
  211. @param x column index, zero or greater, and less than width()
  212. @param y row index, zero or greater, and less than height()
  213. @return readable generic pointer to pixel
  214. */
  215. const void* addr(int x, int y) const {
  216. return (const char*)fPixels + fInfo.computeOffset(x, y, fRowBytes);
  217. }
  218. /** Returns readable base pixel address. Result is addressable as unsigned 8-bit bytes.
  219. Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType or
  220. kGray_8_SkColorType, and is built with SK_DEBUG defined.
  221. One byte corresponds to one pixel.
  222. @return readable unsigned 8-bit pointer to pixels
  223. */
  224. const uint8_t* addr8() const {
  225. SkASSERT(1 == fInfo.bytesPerPixel());
  226. return reinterpret_cast<const uint8_t*>(fPixels);
  227. }
  228. /** Returns readable base pixel address. Result is addressable as unsigned 16-bit words.
  229. Will trigger an assert() if SkColorType is not kRGB_565_SkColorType or
  230. kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
  231. One word corresponds to one pixel.
  232. @return readable unsigned 16-bit pointer to pixels
  233. */
  234. const uint16_t* addr16() const {
  235. SkASSERT(2 == fInfo.bytesPerPixel());
  236. return reinterpret_cast<const uint16_t*>(fPixels);
  237. }
  238. /** Returns readable base pixel address. Result is addressable as unsigned 32-bit words.
  239. Will trigger an assert() if SkColorType is not kRGBA_8888_SkColorType or
  240. kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.
  241. One word corresponds to one pixel.
  242. @return readable unsigned 32-bit pointer to pixels
  243. */
  244. const uint32_t* addr32() const {
  245. SkASSERT(4 == fInfo.bytesPerPixel());
  246. return reinterpret_cast<const uint32_t*>(fPixels);
  247. }
  248. /** Returns readable base pixel address. Result is addressable as unsigned 64-bit words.
  249. Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built
  250. with SK_DEBUG defined.
  251. One word corresponds to one pixel.
  252. @return readable unsigned 64-bit pointer to pixels
  253. */
  254. const uint64_t* addr64() const {
  255. SkASSERT(8 == fInfo.bytesPerPixel());
  256. return reinterpret_cast<const uint64_t*>(fPixels);
  257. }
  258. /** Returns readable base pixel address. Result is addressable as unsigned 16-bit words.
  259. Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built
  260. with SK_DEBUG defined.
  261. Each word represents one color component encoded as a half float.
  262. Four words correspond to one pixel.
  263. @return readable unsigned 16-bit pointer to first component of pixels
  264. */
  265. const uint16_t* addrF16() const {
  266. SkASSERT(8 == fInfo.bytesPerPixel());
  267. SkASSERT(kRGBA_F16_SkColorType == fInfo.colorType());
  268. return reinterpret_cast<const uint16_t*>(fPixels);
  269. }
  270. /** Returns readable pixel address at (x, y).
  271. Input is not validated: out of bounds values of x or y trigger an assert() if
  272. built with SK_DEBUG defined.
  273. Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType or
  274. kGray_8_SkColorType, and is built with SK_DEBUG defined.
  275. @param x column index, zero or greater, and less than width()
  276. @param y row index, zero or greater, and less than height()
  277. @return readable unsigned 8-bit pointer to pixel at (x, y)
  278. */
  279. const uint8_t* addr8(int x, int y) const {
  280. SkASSERT((unsigned)x < (unsigned)fInfo.width());
  281. SkASSERT((unsigned)y < (unsigned)fInfo.height());
  282. return (const uint8_t*)((const char*)this->addr8() + y * fRowBytes + (x << 0));
  283. }
  284. /** Returns readable pixel address at (x, y).
  285. Input is not validated: out of bounds values of x or y trigger an assert() if
  286. built with SK_DEBUG defined.
  287. Will trigger an assert() if SkColorType is not kRGB_565_SkColorType or
  288. kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
  289. @param x column index, zero or greater, and less than width()
  290. @param y row index, zero or greater, and less than height()
  291. @return readable unsigned 16-bit pointer to pixel at (x, y)
  292. */
  293. const uint16_t* addr16(int x, int y) const {
  294. SkASSERT((unsigned)x < (unsigned)fInfo.width());
  295. SkASSERT((unsigned)y < (unsigned)fInfo.height());
  296. return (const uint16_t*)((const char*)this->addr16() + y * fRowBytes + (x << 1));
  297. }
  298. /** Returns readable pixel address at (x, y).
  299. Input is not validated: out of bounds values of x or y trigger an assert() if
  300. built with SK_DEBUG defined.
  301. Will trigger an assert() if SkColorType is not kRGBA_8888_SkColorType or
  302. kBGRA_8888_SkColorType, and is built with SK_DEBUG defined.
  303. @param x column index, zero or greater, and less than width()
  304. @param y row index, zero or greater, and less than height()
  305. @return readable unsigned 32-bit pointer to pixel at (x, y)
  306. */
  307. const uint32_t* addr32(int x, int y) const {
  308. SkASSERT((unsigned)x < (unsigned)fInfo.width());
  309. SkASSERT((unsigned)y < (unsigned)fInfo.height());
  310. return (const uint32_t*)((const char*)this->addr32() + y * fRowBytes + (x << 2));
  311. }
  312. /** Returns readable pixel address at (x, y).
  313. Input is not validated: out of bounds values of x or y trigger an assert() if
  314. built with SK_DEBUG defined.
  315. Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built
  316. with SK_DEBUG defined.
  317. @param x column index, zero or greater, and less than width()
  318. @param y row index, zero or greater, and less than height()
  319. @return readable unsigned 64-bit pointer to pixel at (x, y)
  320. */
  321. const uint64_t* addr64(int x, int y) const {
  322. SkASSERT((unsigned)x < (unsigned)fInfo.width());
  323. SkASSERT((unsigned)y < (unsigned)fInfo.height());
  324. return (const uint64_t*)((const char*)this->addr64() + y * fRowBytes + (x << 3));
  325. }
  326. /** Returns readable pixel address at (x, y).
  327. Input is not validated: out of bounds values of x or y trigger an assert() if
  328. built with SK_DEBUG defined.
  329. Will trigger an assert() if SkColorType is not kRGBA_F16_SkColorType and is built
  330. with SK_DEBUG defined.
  331. Each unsigned 16-bit word represents one color component encoded as a half float.
  332. Four words correspond to one pixel.
  333. @param x column index, zero or greater, and less than width()
  334. @param y row index, zero or greater, and less than height()
  335. @return readable unsigned 16-bit pointer to pixel component at (x, y)
  336. */
  337. const uint16_t* addrF16(int x, int y) const {
  338. SkASSERT(kRGBA_F16_SkColorType == fInfo.colorType());
  339. return reinterpret_cast<const uint16_t*>(this->addr64(x, y));
  340. }
  341. /** Returns writable base pixel address.
  342. @return writable generic base pointer to pixels
  343. */
  344. void* writable_addr() const { return const_cast<void*>(fPixels); }
  345. /** Returns writable pixel address at (x, y).
  346. Input is not validated: out of bounds values of x or y trigger an assert() if
  347. built with SK_DEBUG defined. Returns zero if SkColorType is kUnknown_SkColorType.
  348. @param x column index, zero or greater, and less than width()
  349. @param y row index, zero or greater, and less than height()
  350. @return writable generic pointer to pixel
  351. */
  352. void* writable_addr(int x, int y) const {
  353. return const_cast<void*>(this->addr(x, y));
  354. }
  355. /** Returns writable pixel address at (x, y). Result is addressable as unsigned
  356. 8-bit bytes. Will trigger an assert() if SkColorType is not kAlpha_8_SkColorType
  357. or kGray_8_SkColorType, and is built with SK_DEBUG defined.
  358. One byte corresponds to one pixel.
  359. @param x column index, zero or greater, and less than width()
  360. @param y row index, zero or greater, and less than height()
  361. @return writable unsigned 8-bit pointer to pixels
  362. */
  363. uint8_t* writable_addr8(int x, int y) const {
  364. return const_cast<uint8_t*>(this->addr8(x, y));
  365. }
  366. /** Returns writable_addr pixel address at (x, y). Result is addressable as unsigned
  367. 16-bit words. Will trigger an assert() if SkColorType is not kRGB_565_SkColorType
  368. or kARGB_4444_SkColorType, and is built with SK_DEBUG defined.
  369. One word corresponds to one pixel.
  370. @param x column index, zero or greater, and less than width()
  371. @param y row index, zero or greater, and less than height()
  372. @return writable unsigned 16-bit pointer to pixel
  373. */
  374. uint16_t* writable_addr16(int x, int y) const {
  375. return const_cast<uint16_t*>(this->addr16(x, y));
  376. }
  377. /** Returns writable pixel address at (x, y). Result is addressable as unsigned
  378. 32-bit words. Will trigger an assert() if SkColorType is not
  379. kRGBA_8888_SkColorType or kBGRA_8888_SkColorType, and is built with SK_DEBUG
  380. defined.
  381. One word corresponds to one pixel.
  382. @param x column index, zero or greater, and less than width()
  383. @param y row index, zero or greater, and less than height()
  384. @return writable unsigned 32-bit pointer to pixel
  385. */
  386. uint32_t* writable_addr32(int x, int y) const {
  387. return const_cast<uint32_t*>(this->addr32(x, y));
  388. }
  389. /** Returns writable pixel address at (x, y). Result is addressable as unsigned
  390. 64-bit words. Will trigger an assert() if SkColorType is not
  391. kRGBA_F16_SkColorType and is built with SK_DEBUG defined.
  392. One word corresponds to one pixel.
  393. @param x column index, zero or greater, and less than width()
  394. @param y row index, zero or greater, and less than height()
  395. @return writable unsigned 64-bit pointer to pixel
  396. */
  397. uint64_t* writable_addr64(int x, int y) const {
  398. return const_cast<uint64_t*>(this->addr64(x, y));
  399. }
  400. /** Returns writable pixel address at (x, y). Result is addressable as unsigned
  401. 16-bit words. Will trigger an assert() if SkColorType is not
  402. kRGBA_F16_SkColorType and is built with SK_DEBUG defined.
  403. Each word represents one color component encoded as a half float.
  404. Four words correspond to one pixel.
  405. @param x column index, zero or greater, and less than width()
  406. @param y row index, zero or greater, and less than height()
  407. @return writable unsigned 16-bit pointer to first component of pixel
  408. */
  409. uint16_t* writable_addrF16(int x, int y) const {
  410. return reinterpret_cast<uint16_t*>(writable_addr64(x, y));
  411. }
  412. /** Copies a SkRect of pixels to dstPixels. Copy starts at (0, 0), and does not
  413. exceed SkPixmap (width(), height()).
  414. dstInfo specifies width, height, SkColorType, SkAlphaType, and
  415. SkColorSpace of destination. dstRowBytes specifics the gap from one destination
  416. row to the next. Returns true if pixels are copied. Returns false if
  417. dstInfo address equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes().
  418. Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is
  419. kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match.
  420. If SkPixmap colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match.
  421. If SkPixmap alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must
  422. match. If SkPixmap colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns
  423. false if pixel conversion is not possible.
  424. Returns false if SkPixmap width() or height() is zero or negative.
  425. @param dstInfo destination width, height, SkColorType, SkAlphaType, SkColorSpace
  426. @param dstPixels destination pixel storage
  427. @param dstRowBytes destination row length
  428. @return true if pixels are copied to dstPixels
  429. */
  430. bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes) const {
  431. return this->readPixels(dstInfo, dstPixels, dstRowBytes, 0, 0);
  432. }
  433. /** Copies a SkRect of pixels to dstPixels. Copy starts at (srcX, srcY), and does not
  434. exceed SkPixmap (width(), height()).
  435. dstInfo specifies width, height, SkColorType, SkAlphaType, and
  436. SkColorSpace of destination. dstRowBytes specifics the gap from one destination
  437. row to the next. Returns true if pixels are copied. Returns false if
  438. dstInfo address equals nullptr, or dstRowBytes is less than dstInfo.minRowBytes().
  439. Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is
  440. kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match.
  441. If SkPixmap colorType() is kGray_8_SkColorType, dstInfo.colorSpace() must match.
  442. If SkPixmap alphaType() is kOpaque_SkAlphaType, dstInfo.alphaType() must
  443. match. If SkPixmap colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns
  444. false if pixel conversion is not possible.
  445. srcX and srcY may be negative to copy only top or left of source. Returns
  446. false if SkPixmap width() or height() is zero or negative. Returns false if:
  447. abs(srcX) >= Pixmap width(), or if abs(srcY) >= Pixmap height().
  448. @param dstInfo destination width, height, SkColorType, SkAlphaType, SkColorSpace
  449. @param dstPixels destination pixel storage
  450. @param dstRowBytes destination row length
  451. @param srcX column index whose absolute value is less than width()
  452. @param srcY row index whose absolute value is less than height()
  453. @return true if pixels are copied to dstPixels
  454. */
  455. bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, int srcX,
  456. int srcY) const;
  457. /** Copies a SkRect of pixels to dst. Copy starts at (srcX, srcY), and does not
  458. exceed SkPixmap (width(), height()). dst specifies width, height, SkColorType,
  459. SkAlphaType, and SkColorSpace of destination. Returns true if pixels are copied.
  460. Returns false if dst address equals nullptr, or dst.rowBytes() is less than
  461. dst SkImageInfo::minRowBytes.
  462. Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is
  463. kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.info().colorType must match.
  464. If SkPixmap colorType() is kGray_8_SkColorType, dst.info().colorSpace must match.
  465. If SkPixmap alphaType() is kOpaque_SkAlphaType, dst.info().alphaType must
  466. match. If SkPixmap colorSpace() is nullptr, dst.info().colorSpace must match. Returns
  467. false if pixel conversion is not possible.
  468. srcX and srcY may be negative to copy only top or left of source. Returns
  469. false SkPixmap width() or height() is zero or negative. Returns false if:
  470. abs(srcX) >= Pixmap width(), or if abs(srcY) >= Pixmap height().
  471. @param dst SkImageInfo and pixel address to write to
  472. @param srcX column index whose absolute value is less than width()
  473. @param srcY row index whose absolute value is less than height()
  474. @return true if pixels are copied to dst
  475. */
  476. bool readPixels(const SkPixmap& dst, int srcX, int srcY) const {
  477. return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), srcX, srcY);
  478. }
  479. /** Copies pixels inside bounds() to dst. dst specifies width, height, SkColorType,
  480. SkAlphaType, and SkColorSpace of destination. Returns true if pixels are copied.
  481. Returns false if dst address equals nullptr, or dst.rowBytes() is less than
  482. dst SkImageInfo::minRowBytes.
  483. Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is
  484. kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match.
  485. If SkPixmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match.
  486. If SkPixmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must
  487. match. If SkPixmap colorSpace() is nullptr, dst SkColorSpace must match. Returns
  488. false if pixel conversion is not possible.
  489. Returns false if SkPixmap width() or height() is zero or negative.
  490. @param dst SkImageInfo and pixel address to write to
  491. @return true if pixels are copied to dst
  492. */
  493. bool readPixels(const SkPixmap& dst) const {
  494. return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), 0, 0);
  495. }
  496. /** Copies SkBitmap to dst, scaling pixels to fit dst.width() and dst.height(), and
  497. converting pixels to match dst.colorType() and dst.alphaType(). Returns true if
  498. pixels are copied. Returns false if dst address is nullptr, or dst.rowBytes() is
  499. less than dst SkImageInfo::minRowBytes.
  500. Pixels are copied only if pixel conversion is possible. If SkPixmap colorType() is
  501. kGray_8_SkColorType, or kAlpha_8_SkColorType; dst SkColorType must match.
  502. If SkPixmap colorType() is kGray_8_SkColorType, dst SkColorSpace must match.
  503. If SkPixmap alphaType() is kOpaque_SkAlphaType, dst SkAlphaType must
  504. match. If SkPixmap colorSpace() is nullptr, dst SkColorSpace must match. Returns
  505. false if pixel conversion is not possible.
  506. Returns false if SkBitmap width() or height() is zero or negative.
  507. Scales the image, with filterQuality, to match dst.width() and dst.height().
  508. filterQuality kNone_SkFilterQuality is fastest, typically implemented with
  509. nearest neighbor filter. kLow_SkFilterQuality is typically implemented with
  510. bilerp filter. kMedium_SkFilterQuality is typically implemented with
  511. bilerp filter, and mip-map filter when size is reduced.
  512. kHigh_SkFilterQuality is slowest, typically implemented with bicubic filter.
  513. @param dst SkImageInfo and pixel address to write to
  514. @param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
  515. kMedium_SkFilterQuality, kHigh_SkFilterQuality
  516. @return true if pixels are scaled to fit dst
  517. */
  518. bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality) const;
  519. /** Writes color to pixels bounded by subset; returns true on success.
  520. Returns false if colorType() is kUnknown_SkColorType, or if subset does
  521. not intersect bounds().
  522. @param color unpremultiplied color to write
  523. @param subset bounding integer SkRect of written pixels
  524. @return true if pixels are changed
  525. */
  526. bool erase(SkColor color, const SkIRect& subset) const;
  527. /** Writes color to pixels inside bounds(); returns true on success.
  528. Returns false if colorType() is kUnknown_SkColorType, or if bounds()
  529. is empty.
  530. @param color unpremultiplied color to write
  531. @return true if pixels are changed
  532. */
  533. bool erase(SkColor color) const { return this->erase(color, this->bounds()); }
  534. /** Writes color to pixels bounded by subset; returns true on success.
  535. if subset is nullptr, writes colors pixels inside bounds(). Returns false if
  536. colorType() is kUnknown_SkColorType, if subset is not nullptr and does
  537. not intersect bounds(), or if subset is nullptr and bounds() is empty.
  538. @param color unpremultiplied color to write
  539. @param subset bounding integer SkRect of pixels to write; may be nullptr
  540. @return true if pixels are changed
  541. */
  542. bool erase(const SkColor4f& color, const SkIRect* subset = nullptr) const;
  543. private:
  544. const void* fPixels;
  545. size_t fRowBytes;
  546. SkImageInfo fInfo;
  547. friend class SkPixmapPriv;
  548. };
  549. #endif