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