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 "SkPixmap.h"
9
10 #include "SkBitmap.h"
11 #include "SkColorData.h"
12 #include "SkConvertPixels.h"
13 #include "SkData.h"
14 #include "SkDraw.h"
15 #include "SkHalf.h"
16 #include "SkImageInfoPriv.h"
17 #include "SkImageShader.h"
18 #include "SkMask.h"
19 #include "SkNx.h"
20 #include "SkPixmapPriv.h"
21 #include "SkRasterClip.h"
22 #include "SkReadPixelsRec.h"
23 #include "SkSurface.h"
24 #include "SkTemplates.h"
25 #include "SkTo.h"
26 #include "SkUnPreMultiply.h"
27 #include "SkUtils.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.set(0, 0, 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.makeWH(r.width(), r.height()), 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 kRGB_565_SkColorType:
104 case kRGB_888x_SkColorType:
105 case kRGB_101010x_SkColorType:
106 return 1;
107 case kAlpha_8_SkColorType:
108 value = static_cast<const uint8_t*>(srcPtr)[0] * (1.0f/255);
109 break;
110 case kARGB_4444_SkColorType: {
111 uint16_t u16 = static_cast<const uint16_t*>(srcPtr)[0];
112 value = SkGetPackedA4444(u16) * (1.0f/15);
113 } break;
114 case kRGBA_8888_SkColorType:
115 case kBGRA_8888_SkColorType:
116 value = static_cast<const uint8_t*>(srcPtr)[3] * (1.0f/255);
117 break;
118 case kRGBA_1010102_SkColorType: {
119 uint32_t u32 = static_cast<const uint32_t*>(srcPtr)[0];
120 value = (u32 >> 30) * (1.0f/3);
121 } break;
122 case kRGBA_F16_SkColorType: {
123 uint64_t px;
124 memcpy(&px, srcPtr, sizeof(px));
125 value = SkHalfToFloat_finite_ftz(px)[3];
126 } break;
127 case kRGBA_F32_SkColorType:
128 value = static_cast<const float*>(srcPtr)[3];
129 break;
130 }
131 return value;
132 }
133
readPixels(const SkImageInfo & dstInfo,void * dstPixels,size_t dstRB,int x,int y) const134 bool SkPixmap::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
135 int x, int y) const {
136 if (!SkImageInfoValidConversion(dstInfo, fInfo)) {
137 return false;
138 }
139
140 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, x, y);
141 if (!rec.trim(fInfo.width(), fInfo.height())) {
142 return false;
143 }
144
145 const void* srcPixels = this->addr(rec.fX, rec.fY);
146 const SkImageInfo srcInfo = fInfo.makeWH(rec.fInfo.width(), rec.fInfo.height());
147 SkConvertPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, srcInfo, srcPixels, this->rowBytes());
148 return true;
149 }
150
erase(SkColor color,const SkIRect & subset) const151 bool SkPixmap::erase(SkColor color, const SkIRect& subset) const {
152 return this->erase(SkColor4f::FromColor(color), &subset);
153 }
154
erase(const SkColor4f & color,const SkIRect * subset) const155 bool SkPixmap::erase(const SkColor4f& color, const SkIRect* subset) const {
156 SkPaint paint;
157 paint.setBlendMode(SkBlendMode::kSrc);
158 paint.setColor4f(color, this->colorSpace());
159
160 SkIRect clip = this->bounds();
161 if (subset && !clip.intersect(*subset)) {
162 return false;
163 }
164 SkRasterClip rc{clip};
165
166 SkDraw draw;
167 draw.fDst = *this;
168 draw.fMatrix = &SkMatrix::I();
169 draw.fRC = &rc;
170
171 draw.drawPaint(paint);
172 return true;
173 }
174
scalePixels(const SkPixmap & actualDst,SkFilterQuality quality) const175 bool SkPixmap::scalePixels(const SkPixmap& actualDst, SkFilterQuality quality) const {
176 // We may need to tweak how we interpret these just a little below, so we make copies.
177 SkPixmap src = *this,
178 dst = actualDst;
179
180 // Can't do anthing with empty src or dst
181 if (src.width() <= 0 || src.height() <= 0 ||
182 dst.width() <= 0 || dst.height() <= 0) {
183 return false;
184 }
185
186 // no scaling involved?
187 if (src.width() == dst.width() && src.height() == dst.height()) {
188 return src.readPixels(dst);
189 }
190
191 // If src and dst are both unpremul, we'll fake the source out to appear as if premul,
192 // and mark the destination as opaque. This odd combination allows us to scale unpremul
193 // pixels without ever premultiplying them (perhaps losing information in the color channels).
194 // This is an idiosyncratic feature of scalePixels(), and is tested by scalepixels_unpremul GM.
195 bool clampAsIfUnpremul = false;
196 if (src.alphaType() == kUnpremul_SkAlphaType &&
197 dst.alphaType() == kUnpremul_SkAlphaType) {
198 src.reset(src.info().makeAlphaType(kPremul_SkAlphaType), src.addr(), src.rowBytes());
199 dst.reset(dst.info().makeAlphaType(kOpaque_SkAlphaType), dst.addr(), dst.rowBytes());
200
201 // We'll need to tell the image shader to clamp to [0,1] instead of the
202 // usual [0,a] when using a bicubic scaling (kHigh_SkFilterQuality).
203 clampAsIfUnpremul = true;
204 }
205
206 SkBitmap bitmap;
207 if (!bitmap.installPixels(src)) {
208 return false;
209 }
210 bitmap.setImmutable(); // Don't copy when we create an image.
211 bitmap.setIsVolatile(true); // Disable any caching.
212
213 SkMatrix scale = SkMatrix::MakeRectToRect(SkRect::Make(src.bounds()),
214 SkRect::Make(dst.bounds()),
215 SkMatrix::kFill_ScaleToFit);
216
217 // We'll create a shader to do this draw so we have control over the bicubic clamp.
218 sk_sp<SkShader> shader = SkImageShader::Make(SkImage::MakeFromBitmap(bitmap),
219 SkShader::kClamp_TileMode,
220 SkShader::kClamp_TileMode,
221 &scale,
222 clampAsIfUnpremul);
223
224 sk_sp<SkSurface> surface = SkSurface::MakeRasterDirect(dst.info(),
225 dst.writable_addr(),
226 dst.rowBytes());
227 if (!shader || !surface) {
228 return false;
229 }
230
231 SkPaint paint;
232 paint.setBlendMode(SkBlendMode::kSrc);
233 paint.setFilterQuality(quality);
234 paint.setShader(std::move(shader));
235 surface->getCanvas()->drawPaint(paint);
236 return true;
237 }
238
239 //////////////////////////////////////////////////////////////////////////////////////////////////
240
getColor(int x,int y) const241 SkColor SkPixmap::getColor(int x, int y) const {
242 SkASSERT(this->addr());
243 SkASSERT((unsigned)x < (unsigned)this->width());
244 SkASSERT((unsigned)y < (unsigned)this->height());
245
246 const bool needsUnpremul = (kPremul_SkAlphaType == fInfo.alphaType());
247 auto toColor = [needsUnpremul](uint32_t maybePremulColor) {
248 return needsUnpremul ? SkUnPreMultiply::PMColorToColor(maybePremulColor)
249 : SkSwizzle_BGRA_to_PMColor(maybePremulColor);
250 };
251
252 switch (this->colorType()) {
253 case kGray_8_SkColorType: {
254 uint8_t value = *this->addr8(x, y);
255 return SkColorSetRGB(value, value, value);
256 }
257 case kAlpha_8_SkColorType: {
258 return SkColorSetA(0, *this->addr8(x, y));
259 }
260 case kRGB_565_SkColorType: {
261 return SkPixel16ToColor(*this->addr16(x, y));
262 }
263 case kARGB_4444_SkColorType: {
264 uint16_t value = *this->addr16(x, y);
265 SkPMColor c = SkPixel4444ToPixel32(value);
266 return toColor(c);
267 }
268 case kRGB_888x_SkColorType: {
269 uint32_t value = *this->addr32(x, y);
270 return SkSwizzle_RB(value | 0xff000000);
271 }
272 case kBGRA_8888_SkColorType: {
273 uint32_t value = *this->addr32(x, y);
274 SkPMColor c = SkSwizzle_BGRA_to_PMColor(value);
275 return toColor(c);
276 }
277 case kRGBA_8888_SkColorType: {
278 uint32_t value = *this->addr32(x, y);
279 SkPMColor c = SkSwizzle_RGBA_to_PMColor(value);
280 return toColor(c);
281 }
282 case kRGB_101010x_SkColorType: {
283 uint32_t value = *this->addr32(x, y);
284 // Convert 10-bit rgb to 8-bit bgr, and mask in 0xff alpha at the top.
285 return (uint32_t)( ((value >> 0) & 0x3ff) * (255/1023.0f) ) << 16
286 | (uint32_t)( ((value >> 10) & 0x3ff) * (255/1023.0f) ) << 8
287 | (uint32_t)( ((value >> 20) & 0x3ff) * (255/1023.0f) ) << 0
288 | 0xff000000;
289 }
290 case kRGBA_1010102_SkColorType: {
291 uint32_t value = *this->addr32(x, y);
292
293 float r = ((value >> 0) & 0x3ff) * (1/1023.0f),
294 g = ((value >> 10) & 0x3ff) * (1/1023.0f),
295 b = ((value >> 20) & 0x3ff) * (1/1023.0f),
296 a = ((value >> 30) & 0x3 ) * (1/ 3.0f);
297 if (a != 0 && needsUnpremul) {
298 r *= (1.0f/a);
299 g *= (1.0f/a);
300 b *= (1.0f/a);
301 }
302 return (uint32_t)( r * 255.0f ) << 16
303 | (uint32_t)( g * 255.0f ) << 8
304 | (uint32_t)( b * 255.0f ) << 0
305 | (uint32_t)( a * 255.0f ) << 24;
306 }
307 case kRGBA_F16_SkColorType: {
308 const uint64_t* addr =
309 (const uint64_t*)fPixels + y * (fRowBytes >> 3) + x;
310 Sk4f p4 = SkHalfToFloat_finite_ftz(*addr);
311 if (p4[3] && needsUnpremul) {
312 float inva = 1 / p4[3];
313 p4 = p4 * Sk4f(inva, inva, inva, 1);
314 }
315 SkColor c;
316 SkNx_cast<uint8_t>(p4 * Sk4f(255) + Sk4f(0.5f)).store(&c);
317 // p4 is RGBA, but we want BGRA, so we need to swap next
318 return SkSwizzle_RB(c);
319 }
320 case kRGBA_F32_SkColorType: {
321 const float* rgba =
322 (const float*)fPixels + 4*y*(fRowBytes >> 4) + 4*x;
323 Sk4f p4 = Sk4f::Load(rgba);
324 // From here on, just like F16:
325 if (p4[3] && needsUnpremul) {
326 float inva = 1 / p4[3];
327 p4 = p4 * Sk4f(inva, inva, inva, 1);
328 }
329 SkColor c;
330 SkNx_cast<uint8_t>(p4 * Sk4f(255) + Sk4f(0.5f)).store(&c);
331 // p4 is RGBA, but we want BGRA, so we need to swap next
332 return SkSwizzle_RB(c);
333 }
334 default:
335 SkDEBUGFAIL("");
336 return SkColorSetARGB(0, 0, 0, 0);
337 }
338 }
339
computeIsOpaque() const340 bool SkPixmap::computeIsOpaque() const {
341 const int height = this->height();
342 const int width = this->width();
343
344 switch (this->colorType()) {
345 case kAlpha_8_SkColorType: {
346 unsigned a = 0xFF;
347 for (int y = 0; y < height; ++y) {
348 const uint8_t* row = this->addr8(0, y);
349 for (int x = 0; x < width; ++x) {
350 a &= row[x];
351 }
352 if (0xFF != a) {
353 return false;
354 }
355 }
356 return true;
357 } break;
358 case kRGB_565_SkColorType:
359 case kGray_8_SkColorType:
360 case kRGB_888x_SkColorType:
361 case kRGB_101010x_SkColorType:
362 return true;
363 break;
364 case kARGB_4444_SkColorType: {
365 unsigned c = 0xFFFF;
366 for (int y = 0; y < height; ++y) {
367 const SkPMColor16* row = this->addr16(0, y);
368 for (int x = 0; x < width; ++x) {
369 c &= row[x];
370 }
371 if (0xF != SkGetPackedA4444(c)) {
372 return false;
373 }
374 }
375 return true;
376 } break;
377 case kBGRA_8888_SkColorType:
378 case kRGBA_8888_SkColorType: {
379 SkPMColor c = (SkPMColor)~0;
380 for (int y = 0; y < height; ++y) {
381 const SkPMColor* row = this->addr32(0, y);
382 for (int x = 0; x < width; ++x) {
383 c &= row[x];
384 }
385 if (0xFF != SkGetPackedA32(c)) {
386 return false;
387 }
388 }
389 return true;
390 }
391 case kRGBA_F16_SkColorType: {
392 const SkHalf* row = (const SkHalf*)this->addr();
393 for (int y = 0; y < height; ++y) {
394 for (int x = 0; x < width; ++x) {
395 if (row[4 * x + 3] < SK_Half1) {
396 return false;
397 }
398 }
399 row += this->rowBytes() >> 1;
400 }
401 return true;
402 }
403 case kRGBA_F32_SkColorType: {
404 const float* row = (const float*)this->addr();
405 for (int y = 0; y < height; ++y) {
406 for (int x = 0; x < width; ++x) {
407 if (row[4 * x + 3] < 1.0f) {
408 return false;
409 }
410 }
411 row += this->rowBytes() >> 2;
412 }
413 return true;
414 }
415 case kRGBA_1010102_SkColorType: {
416 uint32_t c = ~0;
417 for (int y = 0; y < height; ++y) {
418 const uint32_t* row = this->addr32(0, y);
419 for (int x = 0; x < width; ++x) {
420 c &= row[x];
421 }
422 if (0b11 != c >> 30) {
423 return false;
424 }
425 }
426 return true;
427 }
428 case kUnknown_SkColorType:
429 SkDEBUGFAIL("");
430 break;
431 }
432 return false;
433 }
434
435 //////////////////////////////////////////////////////////////////////////////////////////////////
436
draw_orientation(const SkPixmap & dst,const SkPixmap & src,SkEncodedOrigin origin)437 static bool draw_orientation(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin origin) {
438 auto surf = SkSurface::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes());
439 if (!surf) {
440 return false;
441 }
442
443 SkBitmap bm;
444 bm.installPixels(src);
445
446 SkMatrix m = SkEncodedOriginToMatrix(origin, src.width(), src.height());
447
448 SkPaint p;
449 p.setBlendMode(SkBlendMode::kSrc);
450 surf->getCanvas()->concat(m);
451 surf->getCanvas()->drawBitmap(bm, 0, 0, &p);
452 return true;
453 }
454
Orient(const SkPixmap & dst,const SkPixmap & src,SkEncodedOrigin origin)455 bool SkPixmapPriv::Orient(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin origin) {
456 if (src.colorType() != dst.colorType()) {
457 return false;
458 }
459 // note: we just ignore alphaType and colorSpace for this transformation
460
461 int w = src.width();
462 int h = src.height();
463 if (ShouldSwapWidthHeight(origin)) {
464 using std::swap;
465 swap(w, h);
466 }
467 if (dst.width() != w || dst.height() != h) {
468 return false;
469 }
470 if (w == 0 || h == 0) {
471 return true;
472 }
473
474 // check for aliasing to self
475 if (src.addr() == dst.addr()) {
476 return kTopLeft_SkEncodedOrigin == origin;
477 }
478 return draw_orientation(dst, src, origin);
479 }
480
ShouldSwapWidthHeight(SkEncodedOrigin origin)481 bool SkPixmapPriv::ShouldSwapWidthHeight(SkEncodedOrigin origin) {
482 // The last four SkEncodedOrigin values involve 90 degree rotations
483 return origin >= kLeftTop_SkEncodedOrigin;
484 }
485
SwapWidthHeight(const SkImageInfo & info)486 SkImageInfo SkPixmapPriv::SwapWidthHeight(const SkImageInfo& info) {
487 return info.makeWH(info.height(), info.width());
488 }
489
490