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 "SkBitmap.h"
9 #include "SkCanvas.h"
10 #include "SkColorPriv.h"
11 #include "SkConvertPixels.h"
12 #include "SkData.h"
13 #include "SkImageInfoPriv.h"
14 #include "SkHalf.h"
15 #include "SkMask.h"
16 #include "SkNx.h"
17 #include "SkPM4f.h"
18 #include "SkPixmap.h"
19 #include "SkReadPixelsRec.h"
20 #include "SkSurface.h"
21 #include "SkUtils.h"
22
reset(const SkPixmap & pm,void (* unlock)(void *),void * ctx)23 void SkAutoPixmapUnlock::reset(const SkPixmap& pm, void (*unlock)(void*), void* ctx) {
24 SkASSERT(pm.addr() != nullptr);
25
26 this->unlock();
27 fPixmap = pm;
28 fUnlockProc = unlock;
29 fUnlockContext = ctx;
30 fIsLocked = true;
31 }
32
33 /////////////////////////////////////////////////////////////////////////////////////////////////
34
reset()35 void SkPixmap::reset() {
36 fPixels = nullptr;
37 fCTable = nullptr;
38 fRowBytes = 0;
39 fInfo = SkImageInfo::MakeUnknown();
40 }
41
reset(const SkImageInfo & info,const void * addr,size_t rowBytes,SkColorTable * ct)42 void SkPixmap::reset(const SkImageInfo& info, const void* addr, size_t rowBytes, SkColorTable* ct) {
43 if (addr) {
44 SkASSERT(info.validRowBytes(rowBytes));
45 }
46 fPixels = addr;
47 fCTable = ct;
48 fRowBytes = rowBytes;
49 fInfo = info;
50 }
51
reset(const SkMask & src)52 bool SkPixmap::reset(const SkMask& src) {
53 if (SkMask::kA8_Format == src.fFormat) {
54 this->reset(SkImageInfo::MakeA8(src.fBounds.width(), src.fBounds.height()),
55 src.fImage, src.fRowBytes, nullptr);
56 return true;
57 }
58 this->reset();
59 return false;
60 }
61
setColorSpace(sk_sp<SkColorSpace> cs)62 void SkPixmap::setColorSpace(sk_sp<SkColorSpace> cs) {
63 fInfo = fInfo.makeColorSpace(std::move(cs));
64 }
65
extractSubset(SkPixmap * result,const SkIRect & subset) const66 bool SkPixmap::extractSubset(SkPixmap* result, const SkIRect& subset) const {
67 SkIRect srcRect, r;
68 srcRect.set(0, 0, this->width(), this->height());
69 if (!r.intersect(srcRect, subset)) {
70 return false; // r is empty (i.e. no intersection)
71 }
72
73 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
74 // exited above.
75 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
76 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
77
78 const void* pixels = nullptr;
79 if (fPixels) {
80 const size_t bpp = fInfo.bytesPerPixel();
81 pixels = (const uint8_t*)fPixels + r.fTop * fRowBytes + r.fLeft * bpp;
82 }
83 result->reset(fInfo.makeWH(r.width(), r.height()), pixels, fRowBytes, fCTable);
84 return true;
85 }
86
readPixels(const SkImageInfo & dstInfo,void * dstPixels,size_t dstRB,int x,int y) const87 bool SkPixmap::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, int x, int y)
88 const {
89 if (!SkImageInfoValidConversion(dstInfo, fInfo)) {
90 return false;
91 }
92
93 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, x, y);
94 if (!rec.trim(fInfo.width(), fInfo.height())) {
95 return false;
96 }
97
98 const void* srcPixels = this->addr(rec.fX, rec.fY);
99 const SkImageInfo srcInfo = fInfo.makeWH(rec.fInfo.width(), rec.fInfo.height());
100 SkConvertPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, srcInfo, srcPixels, this->rowBytes(),
101 this->ctable(), SkTransferFunctionBehavior::kRespect);
102 return true;
103 }
104
pack_8888_to_4444(unsigned a,unsigned r,unsigned g,unsigned b)105 static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) {
106 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) |
107 (SkR32To4444(r) << SK_R4444_SHIFT) |
108 (SkG32To4444(g) << SK_G4444_SHIFT) |
109 (SkB32To4444(b) << SK_B4444_SHIFT);
110 return SkToU16(pixel);
111 }
112
erase(SkColor color,const SkIRect & inArea) const113 bool SkPixmap::erase(SkColor color, const SkIRect& inArea) const {
114 if (nullptr == fPixels) {
115 return false;
116 }
117 SkIRect area;
118 if (!area.intersect(this->bounds(), inArea)) {
119 return false;
120 }
121
122 U8CPU a = SkColorGetA(color);
123 U8CPU r = SkColorGetR(color);
124 U8CPU g = SkColorGetG(color);
125 U8CPU b = SkColorGetB(color);
126
127 int height = area.height();
128 const int width = area.width();
129 const int rowBytes = this->rowBytes();
130
131 switch (this->colorType()) {
132 case kGray_8_SkColorType: {
133 if (255 != a) {
134 r = SkMulDiv255Round(r, a);
135 g = SkMulDiv255Round(g, a);
136 b = SkMulDiv255Round(b, a);
137 }
138 int gray = SkComputeLuminance(r, g, b);
139 uint8_t* p = this->writable_addr8(area.fLeft, area.fTop);
140 while (--height >= 0) {
141 memset(p, gray, width);
142 p += rowBytes;
143 }
144 break;
145 }
146 case kAlpha_8_SkColorType: {
147 uint8_t* p = this->writable_addr8(area.fLeft, area.fTop);
148 while (--height >= 0) {
149 memset(p, a, width);
150 p += rowBytes;
151 }
152 break;
153 }
154 case kARGB_4444_SkColorType:
155 case kRGB_565_SkColorType: {
156 uint16_t* p = this->writable_addr16(area.fLeft, area.fTop);
157 uint16_t v;
158
159 // make rgb premultiplied
160 if (255 != a) {
161 r = SkMulDiv255Round(r, a);
162 g = SkMulDiv255Round(g, a);
163 b = SkMulDiv255Round(b, a);
164 }
165
166 if (kARGB_4444_SkColorType == this->colorType()) {
167 v = pack_8888_to_4444(a, r, g, b);
168 } else {
169 v = SkPackRGB16(r >> (8 - SK_R16_BITS),
170 g >> (8 - SK_G16_BITS),
171 b >> (8 - SK_B16_BITS));
172 }
173 while (--height >= 0) {
174 sk_memset16(p, v, width);
175 p = (uint16_t*)((char*)p + rowBytes);
176 }
177 break;
178 }
179 case kBGRA_8888_SkColorType:
180 case kRGBA_8888_SkColorType: {
181 uint32_t* p = this->writable_addr32(area.fLeft, area.fTop);
182
183 if (255 != a && kPremul_SkAlphaType == this->alphaType()) {
184 r = SkMulDiv255Round(r, a);
185 g = SkMulDiv255Round(g, a);
186 b = SkMulDiv255Round(b, a);
187 }
188 uint32_t v = kRGBA_8888_SkColorType == this->colorType()
189 ? SkPackARGB_as_RGBA(a, r, g, b)
190 : SkPackARGB_as_BGRA(a, r, g, b);
191
192 while (--height >= 0) {
193 sk_memset32(p, v, width);
194 p = (uint32_t*)((char*)p + rowBytes);
195 }
196 break;
197 }
198 case kRGBA_F16_SkColorType:
199 // The colorspace is unspecified, so assume linear just like getColor().
200 this->erase(SkColor4f{(1 / 255.0f) * r,
201 (1 / 255.0f) * g,
202 (1 / 255.0f) * b,
203 (1 / 255.0f) * a}, &area);
204 break;
205 default:
206 return false; // no change, so don't call notifyPixelsChanged()
207 }
208 return true;
209 }
210
erase(const SkColor4f & origColor,const SkIRect * subset) const211 bool SkPixmap::erase(const SkColor4f& origColor, const SkIRect* subset) const {
212 SkPixmap pm;
213 if (subset) {
214 if (!this->extractSubset(&pm, *subset)) {
215 return false;
216 }
217 } else {
218 pm = *this;
219 }
220
221 const SkColor4f color = origColor.pin();
222
223 if (kRGBA_F16_SkColorType != pm.colorType()) {
224 return pm.erase(color.toSkColor());
225 }
226
227 const uint64_t half4 = color.premul().toF16();
228 for (int y = 0; y < pm.height(); ++y) {
229 sk_memset64(pm.writable_addr64(0, y), half4, pm.width());
230 }
231 return true;
232 }
233
scalePixels(const SkPixmap & dst,SkFilterQuality quality) const234 bool SkPixmap::scalePixels(const SkPixmap& dst, SkFilterQuality quality) const {
235 // Can't do anthing with empty src or dst
236 if (this->width() <= 0 || this->height() <= 0 || dst.width() <= 0 || dst.height() <= 0) {
237 return false;
238 }
239
240 // no scaling involved?
241 if (dst.width() == this->width() && dst.height() == this->height()) {
242 return this->readPixels(dst);
243 }
244
245 SkBitmap bitmap;
246 if (!bitmap.installPixels(*this)) {
247 return false;
248 }
249 bitmap.setIsVolatile(true); // so we don't try to cache it
250
251 auto surface(SkSurface::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes()));
252 if (!surface) {
253 return false;
254 }
255
256 SkPaint paint;
257 paint.setFilterQuality(quality);
258 paint.setBlendMode(SkBlendMode::kSrc);
259 surface->getCanvas()->drawBitmapRect(bitmap, SkRect::MakeIWH(dst.width(), dst.height()),
260 &paint);
261 return true;
262 }
263
264 //////////////////////////////////////////////////////////////////////////////////////////////////
265
getColor(int x,int y) const266 SkColor SkPixmap::getColor(int x, int y) const {
267 SkASSERT(this->addr());
268 SkASSERT((unsigned)x < (unsigned)this->width());
269 SkASSERT((unsigned)y < (unsigned)this->height());
270 switch (this->colorType()) {
271 case kGray_8_SkColorType: {
272 uint8_t value = *this->addr8(x, y);
273 return SkColorSetRGB(value, value, value);
274 }
275 case kAlpha_8_SkColorType: {
276 return SkColorSetA(0, *this->addr8(x, y));
277 }
278 case kIndex_8_SkColorType: {
279 SkASSERT(this->ctable());
280 SkPMColor pmColor = (*this->ctable())[*this->addr8(x, y)];
281 return SkUnPreMultiply::PMColorToColor(pmColor);
282 }
283 case kRGB_565_SkColorType: {
284 return SkPixel16ToColor(*this->addr16(x, y));
285 }
286 case kARGB_4444_SkColorType: {
287 uint16_t value = *this->addr16(x, y);
288 SkPMColor c = SkPixel4444ToPixel32(value);
289 return SkUnPreMultiply::PMColorToColor(c);
290 }
291 case kBGRA_8888_SkColorType: {
292 uint32_t value = *this->addr32(x, y);
293 SkPMColor c = SkSwizzle_BGRA_to_PMColor(value);
294 return SkUnPreMultiply::PMColorToColor(c);
295 }
296 case kRGBA_8888_SkColorType: {
297 uint32_t value = *this->addr32(x, y);
298 SkPMColor c = SkSwizzle_RGBA_to_PMColor(value);
299 return SkUnPreMultiply::PMColorToColor(c);
300 }
301 case kRGBA_F16_SkColorType: {
302 const uint64_t* addr =
303 (const uint64_t*)fPixels + y * (fRowBytes >> 3) + x;
304 Sk4f p4 = SkHalfToFloat_finite_ftz(*addr);
305 if (p4[3]) {
306 float inva = 1 / p4[3];
307 p4 = p4 * Sk4f(inva, inva, inva, 1);
308 }
309 SkColor c;
310 SkNx_cast<uint8_t>(p4 * Sk4f(255) + Sk4f(0.5f)).store(&c);
311 // p4 is RGBA, but we want BGRA, so we need to swap next
312 return SkSwizzle_RB(c);
313 }
314 default:
315 SkDEBUGFAIL("");
316 return SkColorSetARGB(0, 0, 0, 0);
317 }
318 }
319
computeIsOpaque() const320 bool SkPixmap::computeIsOpaque() const {
321 const int height = this->height();
322 const int width = this->width();
323
324 switch (this->colorType()) {
325 case kAlpha_8_SkColorType: {
326 unsigned a = 0xFF;
327 for (int y = 0; y < height; ++y) {
328 const uint8_t* row = this->addr8(0, y);
329 for (int x = 0; x < width; ++x) {
330 a &= row[x];
331 }
332 if (0xFF != a) {
333 return false;
334 }
335 }
336 return true;
337 } break;
338 case kIndex_8_SkColorType: {
339 const SkColorTable* ctable = this->ctable();
340 if (nullptr == ctable) {
341 return false;
342 }
343 const SkPMColor* table = ctable->readColors();
344 SkPMColor c = (SkPMColor)~0;
345 for (int i = ctable->count() - 1; i >= 0; --i) {
346 c &= table[i];
347 }
348 return 0xFF == SkGetPackedA32(c);
349 } break;
350 case kRGB_565_SkColorType:
351 case kGray_8_SkColorType:
352 return true;
353 break;
354 case kARGB_4444_SkColorType: {
355 unsigned c = 0xFFFF;
356 for (int y = 0; y < height; ++y) {
357 const SkPMColor16* row = this->addr16(0, y);
358 for (int x = 0; x < width; ++x) {
359 c &= row[x];
360 }
361 if (0xF != SkGetPackedA4444(c)) {
362 return false;
363 }
364 }
365 return true;
366 } break;
367 case kBGRA_8888_SkColorType:
368 case kRGBA_8888_SkColorType: {
369 SkPMColor c = (SkPMColor)~0;
370 for (int y = 0; y < height; ++y) {
371 const SkPMColor* row = this->addr32(0, y);
372 for (int x = 0; x < width; ++x) {
373 c &= row[x];
374 }
375 if (0xFF != SkGetPackedA32(c)) {
376 return false;
377 }
378 }
379 return true;
380 }
381 case kRGBA_F16_SkColorType: {
382 const SkHalf* row = (const SkHalf*)this->addr();
383 for (int y = 0; y < height; ++y) {
384 for (int x = 0; x < width; ++x) {
385 if (row[4 * x + 3] < SK_Half1) {
386 return false;
387 }
388 }
389 row += this->rowBytes() >> 1;
390 }
391 return true;
392 }
393 default:
394 break;
395 }
396 return false;
397 }
398