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