SkPixmap.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. #include "include/core/SkPixmap.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkData.h"
  10. #include "include/core/SkSurface.h"
  11. #include "include/core/SkUnPreMultiply.h"
  12. #include "include/private/SkColorData.h"
  13. #include "include/private/SkHalf.h"
  14. #include "include/private/SkImageInfoPriv.h"
  15. #include "include/private/SkNx.h"
  16. #include "include/private/SkTemplates.h"
  17. #include "include/private/SkTo.h"
  18. #include "src/core/SkConvertPixels.h"
  19. #include "src/core/SkDraw.h"
  20. #include "src/core/SkMask.h"
  21. #include "src/core/SkPixmapPriv.h"
  22. #include "src/core/SkRasterClip.h"
  23. #include "src/core/SkUtils.h"
  24. #include "src/image/SkReadPixelsRec.h"
  25. #include "src/shaders/SkImageShader.h"
  26. #include <utility>
  27. /////////////////////////////////////////////////////////////////////////////////////////////////
  28. void SkPixmap::reset() {
  29. fPixels = nullptr;
  30. fRowBytes = 0;
  31. fInfo = SkImageInfo::MakeUnknown();
  32. }
  33. void SkPixmap::reset(const SkImageInfo& info, const void* addr, size_t rowBytes) {
  34. if (addr) {
  35. SkASSERT(info.validRowBytes(rowBytes));
  36. }
  37. fPixels = addr;
  38. fRowBytes = rowBytes;
  39. fInfo = info;
  40. }
  41. bool SkPixmap::reset(const SkMask& src) {
  42. if (SkMask::kA8_Format == src.fFormat) {
  43. this->reset(SkImageInfo::MakeA8(src.fBounds.width(), src.fBounds.height()),
  44. src.fImage, src.fRowBytes);
  45. return true;
  46. }
  47. this->reset();
  48. return false;
  49. }
  50. void SkPixmap::setColorSpace(sk_sp<SkColorSpace> cs) {
  51. fInfo = fInfo.makeColorSpace(std::move(cs));
  52. }
  53. bool SkPixmap::extractSubset(SkPixmap* result, const SkIRect& subset) const {
  54. SkIRect srcRect, r;
  55. srcRect.set(0, 0, this->width(), this->height());
  56. if (!r.intersect(srcRect, subset)) {
  57. return false; // r is empty (i.e. no intersection)
  58. }
  59. // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
  60. // exited above.
  61. SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
  62. SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
  63. const void* pixels = nullptr;
  64. if (fPixels) {
  65. const size_t bpp = fInfo.bytesPerPixel();
  66. pixels = (const uint8_t*)fPixels + r.fTop * fRowBytes + r.fLeft * bpp;
  67. }
  68. result->reset(fInfo.makeWH(r.width(), r.height()), pixels, fRowBytes);
  69. return true;
  70. }
  71. // This is the same as SkPixmap::addr(x,y), but this version gets inlined, while the public
  72. // method does not. Perhaps we could bloat it so it can be inlined, but that would grow code-size
  73. // everywhere, instead of just here (on behalf of getAlphaf()).
  74. static const void* fast_getaddr(const SkPixmap& pm, int x, int y) {
  75. x <<= SkColorTypeShiftPerPixel(pm.colorType());
  76. return static_cast<const char*>(pm.addr()) + y * pm.rowBytes() + x;
  77. }
  78. float SkPixmap::getAlphaf(int x, int y) const {
  79. SkASSERT(this->addr());
  80. SkASSERT((unsigned)x < (unsigned)this->width());
  81. SkASSERT((unsigned)y < (unsigned)this->height());
  82. float value = 0;
  83. const void* srcPtr = fast_getaddr(*this, x, y);
  84. switch (this->colorType()) {
  85. case kUnknown_SkColorType:
  86. return 0;
  87. case kGray_8_SkColorType:
  88. case kRGB_565_SkColorType:
  89. case kRGB_888x_SkColorType:
  90. case kRGB_101010x_SkColorType:
  91. return 1;
  92. case kAlpha_8_SkColorType:
  93. value = static_cast<const uint8_t*>(srcPtr)[0] * (1.0f/255);
  94. break;
  95. case kARGB_4444_SkColorType: {
  96. uint16_t u16 = static_cast<const uint16_t*>(srcPtr)[0];
  97. value = SkGetPackedA4444(u16) * (1.0f/15);
  98. } break;
  99. case kRGBA_8888_SkColorType:
  100. case kBGRA_8888_SkColorType:
  101. value = static_cast<const uint8_t*>(srcPtr)[3] * (1.0f/255);
  102. break;
  103. case kRGBA_1010102_SkColorType: {
  104. uint32_t u32 = static_cast<const uint32_t*>(srcPtr)[0];
  105. value = (u32 >> 30) * (1.0f/3);
  106. } break;
  107. case kRGBA_F16Norm_SkColorType:
  108. case kRGBA_F16_SkColorType: {
  109. uint64_t px;
  110. memcpy(&px, srcPtr, sizeof(px));
  111. value = SkHalfToFloat_finite_ftz(px)[3];
  112. } break;
  113. case kRGBA_F32_SkColorType:
  114. value = static_cast<const float*>(srcPtr)[3];
  115. break;
  116. }
  117. return value;
  118. }
  119. bool SkPixmap::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
  120. int x, int y) const {
  121. if (!SkImageInfoValidConversion(dstInfo, fInfo)) {
  122. return false;
  123. }
  124. SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, x, y);
  125. if (!rec.trim(fInfo.width(), fInfo.height())) {
  126. return false;
  127. }
  128. const void* srcPixels = this->addr(rec.fX, rec.fY);
  129. const SkImageInfo srcInfo = fInfo.makeWH(rec.fInfo.width(), rec.fInfo.height());
  130. SkConvertPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, srcInfo, srcPixels, this->rowBytes());
  131. return true;
  132. }
  133. bool SkPixmap::erase(SkColor color, const SkIRect& subset) const {
  134. return this->erase(SkColor4f::FromColor(color), &subset);
  135. }
  136. bool SkPixmap::erase(const SkColor4f& color, const SkIRect* subset) const {
  137. SkPaint paint;
  138. paint.setBlendMode(SkBlendMode::kSrc);
  139. paint.setColor4f(color, this->colorSpace());
  140. SkIRect clip = this->bounds();
  141. if (subset && !clip.intersect(*subset)) {
  142. return false;
  143. }
  144. SkRasterClip rc{clip};
  145. SkDraw draw;
  146. draw.fDst = *this;
  147. draw.fMatrix = &SkMatrix::I();
  148. draw.fRC = &rc;
  149. draw.drawPaint(paint);
  150. return true;
  151. }
  152. bool SkPixmap::scalePixels(const SkPixmap& actualDst, SkFilterQuality quality) const {
  153. // We may need to tweak how we interpret these just a little below, so we make copies.
  154. SkPixmap src = *this,
  155. dst = actualDst;
  156. // Can't do anthing with empty src or dst
  157. if (src.width() <= 0 || src.height() <= 0 ||
  158. dst.width() <= 0 || dst.height() <= 0) {
  159. return false;
  160. }
  161. // no scaling involved?
  162. if (src.width() == dst.width() && src.height() == dst.height()) {
  163. return src.readPixels(dst);
  164. }
  165. // If src and dst are both unpremul, we'll fake the source out to appear as if premul,
  166. // and mark the destination as opaque. This odd combination allows us to scale unpremul
  167. // pixels without ever premultiplying them (perhaps losing information in the color channels).
  168. // This is an idiosyncratic feature of scalePixels(), and is tested by scalepixels_unpremul GM.
  169. bool clampAsIfUnpremul = false;
  170. if (src.alphaType() == kUnpremul_SkAlphaType &&
  171. dst.alphaType() == kUnpremul_SkAlphaType) {
  172. src.reset(src.info().makeAlphaType(kPremul_SkAlphaType), src.addr(), src.rowBytes());
  173. dst.reset(dst.info().makeAlphaType(kOpaque_SkAlphaType), dst.addr(), dst.rowBytes());
  174. // We'll need to tell the image shader to clamp to [0,1] instead of the
  175. // usual [0,a] when using a bicubic scaling (kHigh_SkFilterQuality).
  176. clampAsIfUnpremul = true;
  177. }
  178. SkBitmap bitmap;
  179. if (!bitmap.installPixels(src)) {
  180. return false;
  181. }
  182. bitmap.setImmutable(); // Don't copy when we create an image.
  183. bitmap.setIsVolatile(true); // Disable any caching.
  184. SkMatrix scale = SkMatrix::MakeRectToRect(SkRect::Make(src.bounds()),
  185. SkRect::Make(dst.bounds()),
  186. SkMatrix::kFill_ScaleToFit);
  187. // We'll create a shader to do this draw so we have control over the bicubic clamp.
  188. sk_sp<SkShader> shader = SkImageShader::Make(SkImage::MakeFromBitmap(bitmap),
  189. SkTileMode::kClamp,
  190. SkTileMode::kClamp,
  191. &scale,
  192. clampAsIfUnpremul);
  193. sk_sp<SkSurface> surface = SkSurface::MakeRasterDirect(dst.info(),
  194. dst.writable_addr(),
  195. dst.rowBytes());
  196. if (!shader || !surface) {
  197. return false;
  198. }
  199. SkPaint paint;
  200. paint.setBlendMode(SkBlendMode::kSrc);
  201. paint.setFilterQuality(quality);
  202. paint.setShader(std::move(shader));
  203. surface->getCanvas()->drawPaint(paint);
  204. return true;
  205. }
  206. //////////////////////////////////////////////////////////////////////////////////////////////////
  207. SkColor SkPixmap::getColor(int x, int y) const {
  208. SkASSERT(this->addr());
  209. SkASSERT((unsigned)x < (unsigned)this->width());
  210. SkASSERT((unsigned)y < (unsigned)this->height());
  211. const bool needsUnpremul = (kPremul_SkAlphaType == fInfo.alphaType());
  212. auto toColor = [needsUnpremul](uint32_t maybePremulColor) {
  213. return needsUnpremul ? SkUnPreMultiply::PMColorToColor(maybePremulColor)
  214. : SkSwizzle_BGRA_to_PMColor(maybePremulColor);
  215. };
  216. switch (this->colorType()) {
  217. case kGray_8_SkColorType: {
  218. uint8_t value = *this->addr8(x, y);
  219. return SkColorSetRGB(value, value, value);
  220. }
  221. case kAlpha_8_SkColorType: {
  222. return SkColorSetA(0, *this->addr8(x, y));
  223. }
  224. case kRGB_565_SkColorType: {
  225. return SkPixel16ToColor(*this->addr16(x, y));
  226. }
  227. case kARGB_4444_SkColorType: {
  228. uint16_t value = *this->addr16(x, y);
  229. SkPMColor c = SkPixel4444ToPixel32(value);
  230. return toColor(c);
  231. }
  232. case kRGB_888x_SkColorType: {
  233. uint32_t value = *this->addr32(x, y);
  234. return SkSwizzle_RB(value | 0xff000000);
  235. }
  236. case kBGRA_8888_SkColorType: {
  237. uint32_t value = *this->addr32(x, y);
  238. SkPMColor c = SkSwizzle_BGRA_to_PMColor(value);
  239. return toColor(c);
  240. }
  241. case kRGBA_8888_SkColorType: {
  242. uint32_t value = *this->addr32(x, y);
  243. SkPMColor c = SkSwizzle_RGBA_to_PMColor(value);
  244. return toColor(c);
  245. }
  246. case kRGB_101010x_SkColorType: {
  247. uint32_t value = *this->addr32(x, y);
  248. // Convert 10-bit rgb to 8-bit bgr, and mask in 0xff alpha at the top.
  249. return (uint32_t)( ((value >> 0) & 0x3ff) * (255/1023.0f) ) << 16
  250. | (uint32_t)( ((value >> 10) & 0x3ff) * (255/1023.0f) ) << 8
  251. | (uint32_t)( ((value >> 20) & 0x3ff) * (255/1023.0f) ) << 0
  252. | 0xff000000;
  253. }
  254. case kRGBA_1010102_SkColorType: {
  255. uint32_t value = *this->addr32(x, y);
  256. float r = ((value >> 0) & 0x3ff) * (1/1023.0f),
  257. g = ((value >> 10) & 0x3ff) * (1/1023.0f),
  258. b = ((value >> 20) & 0x3ff) * (1/1023.0f),
  259. a = ((value >> 30) & 0x3 ) * (1/ 3.0f);
  260. if (a != 0 && needsUnpremul) {
  261. r *= (1.0f/a);
  262. g *= (1.0f/a);
  263. b *= (1.0f/a);
  264. }
  265. return (uint32_t)( r * 255.0f ) << 16
  266. | (uint32_t)( g * 255.0f ) << 8
  267. | (uint32_t)( b * 255.0f ) << 0
  268. | (uint32_t)( a * 255.0f ) << 24;
  269. }
  270. case kRGBA_F16Norm_SkColorType:
  271. case kRGBA_F16_SkColorType: {
  272. const uint64_t* addr =
  273. (const uint64_t*)fPixels + y * (fRowBytes >> 3) + x;
  274. Sk4f p4 = SkHalfToFloat_finite_ftz(*addr);
  275. if (p4[3] && needsUnpremul) {
  276. float inva = 1 / p4[3];
  277. p4 = p4 * Sk4f(inva, inva, inva, 1);
  278. }
  279. SkColor c;
  280. SkNx_cast<uint8_t>(p4 * Sk4f(255) + Sk4f(0.5f)).store(&c);
  281. // p4 is RGBA, but we want BGRA, so we need to swap next
  282. return SkSwizzle_RB(c);
  283. }
  284. case kRGBA_F32_SkColorType: {
  285. const float* rgba =
  286. (const float*)fPixels + 4*y*(fRowBytes >> 4) + 4*x;
  287. Sk4f p4 = Sk4f::Load(rgba);
  288. // From here on, just like F16:
  289. if (p4[3] && needsUnpremul) {
  290. float inva = 1 / p4[3];
  291. p4 = p4 * Sk4f(inva, inva, inva, 1);
  292. }
  293. SkColor c;
  294. SkNx_cast<uint8_t>(p4 * Sk4f(255) + Sk4f(0.5f)).store(&c);
  295. // p4 is RGBA, but we want BGRA, so we need to swap next
  296. return SkSwizzle_RB(c);
  297. }
  298. default:
  299. SkDEBUGFAIL("");
  300. return SkColorSetARGB(0, 0, 0, 0);
  301. }
  302. }
  303. bool SkPixmap::computeIsOpaque() const {
  304. const int height = this->height();
  305. const int width = this->width();
  306. switch (this->colorType()) {
  307. case kAlpha_8_SkColorType: {
  308. unsigned a = 0xFF;
  309. for (int y = 0; y < height; ++y) {
  310. const uint8_t* row = this->addr8(0, y);
  311. for (int x = 0; x < width; ++x) {
  312. a &= row[x];
  313. }
  314. if (0xFF != a) {
  315. return false;
  316. }
  317. }
  318. return true;
  319. } break;
  320. case kRGB_565_SkColorType:
  321. case kGray_8_SkColorType:
  322. case kRGB_888x_SkColorType:
  323. case kRGB_101010x_SkColorType:
  324. return true;
  325. break;
  326. case kARGB_4444_SkColorType: {
  327. unsigned c = 0xFFFF;
  328. for (int y = 0; y < height; ++y) {
  329. const SkPMColor16* row = this->addr16(0, y);
  330. for (int x = 0; x < width; ++x) {
  331. c &= row[x];
  332. }
  333. if (0xF != SkGetPackedA4444(c)) {
  334. return false;
  335. }
  336. }
  337. return true;
  338. } break;
  339. case kBGRA_8888_SkColorType:
  340. case kRGBA_8888_SkColorType: {
  341. SkPMColor c = (SkPMColor)~0;
  342. for (int y = 0; y < height; ++y) {
  343. const SkPMColor* row = this->addr32(0, y);
  344. for (int x = 0; x < width; ++x) {
  345. c &= row[x];
  346. }
  347. if (0xFF != SkGetPackedA32(c)) {
  348. return false;
  349. }
  350. }
  351. return true;
  352. }
  353. case kRGBA_F16Norm_SkColorType:
  354. case kRGBA_F16_SkColorType: {
  355. const SkHalf* row = (const SkHalf*)this->addr();
  356. for (int y = 0; y < height; ++y) {
  357. for (int x = 0; x < width; ++x) {
  358. if (row[4 * x + 3] < SK_Half1) {
  359. return false;
  360. }
  361. }
  362. row += this->rowBytes() >> 1;
  363. }
  364. return true;
  365. }
  366. case kRGBA_F32_SkColorType: {
  367. const float* row = (const float*)this->addr();
  368. for (int y = 0; y < height; ++y) {
  369. for (int x = 0; x < width; ++x) {
  370. if (row[4 * x + 3] < 1.0f) {
  371. return false;
  372. }
  373. }
  374. row += this->rowBytes() >> 2;
  375. }
  376. return true;
  377. }
  378. case kRGBA_1010102_SkColorType: {
  379. uint32_t c = ~0;
  380. for (int y = 0; y < height; ++y) {
  381. const uint32_t* row = this->addr32(0, y);
  382. for (int x = 0; x < width; ++x) {
  383. c &= row[x];
  384. }
  385. if (0b11 != c >> 30) {
  386. return false;
  387. }
  388. }
  389. return true;
  390. }
  391. case kUnknown_SkColorType:
  392. SkDEBUGFAIL("");
  393. break;
  394. }
  395. return false;
  396. }
  397. //////////////////////////////////////////////////////////////////////////////////////////////////
  398. static bool draw_orientation(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin origin) {
  399. auto surf = SkSurface::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes());
  400. if (!surf) {
  401. return false;
  402. }
  403. SkBitmap bm;
  404. bm.installPixels(src);
  405. SkMatrix m = SkEncodedOriginToMatrix(origin, src.width(), src.height());
  406. SkPaint p;
  407. p.setBlendMode(SkBlendMode::kSrc);
  408. surf->getCanvas()->concat(m);
  409. surf->getCanvas()->drawBitmap(bm, 0, 0, &p);
  410. return true;
  411. }
  412. bool SkPixmapPriv::Orient(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin origin) {
  413. if (src.colorType() != dst.colorType()) {
  414. return false;
  415. }
  416. // note: we just ignore alphaType and colorSpace for this transformation
  417. int w = src.width();
  418. int h = src.height();
  419. if (ShouldSwapWidthHeight(origin)) {
  420. using std::swap;
  421. swap(w, h);
  422. }
  423. if (dst.width() != w || dst.height() != h) {
  424. return false;
  425. }
  426. if (w == 0 || h == 0) {
  427. return true;
  428. }
  429. // check for aliasing to self
  430. if (src.addr() == dst.addr()) {
  431. return kTopLeft_SkEncodedOrigin == origin;
  432. }
  433. return draw_orientation(dst, src, origin);
  434. }
  435. bool SkPixmapPriv::ShouldSwapWidthHeight(SkEncodedOrigin origin) {
  436. // The last four SkEncodedOrigin values involve 90 degree rotations
  437. return origin >= kLeftTop_SkEncodedOrigin;
  438. }
  439. SkImageInfo SkPixmapPriv::SwapWidthHeight(const SkImageInfo& info) {
  440. return info.makeWH(info.height(), info.width());
  441. }