blob: e5cc0d7fcd7b627bcecf57d4bde26c0567c2ba80 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2008 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkBitmap.h"
11#include "SkColorPriv.h"
12#include "SkDither.h"
13#include "SkFlattenable.h"
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000014#include "SkImagePriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkMallocPixelRef.h"
16#include "SkMask.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000017#include "SkReadBuffer.h"
18#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkPixelRef.h"
20#include "SkThread.h"
vandebo@chromium.org112706d2011-02-24 22:50:55 +000021#include "SkUnPreMultiply.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkUtils.h"
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000023#include "SkValidationUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#include "SkPackBits.h"
25#include <new>
26
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000027static bool reset_return_false(SkBitmap* bm) {
28 bm->reset();
29 return false;
30}
31
reed@android.com8a1c16f2008-12-17 15:59:43 +000032SkBitmap::SkBitmap() {
reed@android.com4516f472009-06-29 16:25:36 +000033 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000034}
35
36SkBitmap::SkBitmap(const SkBitmap& src) {
37 SkDEBUGCODE(src.validate();)
reed@android.com4516f472009-06-29 16:25:36 +000038 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 *this = src;
40 SkDEBUGCODE(this->validate();)
41}
42
43SkBitmap::~SkBitmap() {
44 SkDEBUGCODE(this->validate();)
45 this->freePixels();
46}
47
48SkBitmap& SkBitmap::operator=(const SkBitmap& src) {
49 if (this != &src) {
50 this->freePixels();
51 memcpy(this, &src, sizeof(src));
52
53 // inc src reference counts
reed@android.com83f7bc32009-07-17 02:42:41 +000054 SkSafeRef(src.fPixelRef);
reed@android.com8a1c16f2008-12-17 15:59:43 +000055
56 // we reset our locks if we get blown away
57 fPixelLockCount = 0;
weita@google.comf9ab99a2009-05-03 18:23:30 +000058
reed@google.com5f62ed72014-01-15 19:59:45 +000059 if (fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 // ignore the values from the memcpy
61 fPixels = NULL;
62 fColorTable = NULL;
bsalomon@google.com586f48c2011-04-14 15:07:22 +000063 // Note that what to for genID is somewhat arbitrary. We have no
64 // way to track changes to raw pixels across multiple SkBitmaps.
65 // Would benefit from an SkRawPixelRef type created by
66 // setPixels.
67 // Just leave the memcpy'ed one but they'll get out of sync
68 // as soon either is modified.
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 }
70 }
71
72 SkDEBUGCODE(this->validate();)
73 return *this;
74}
75
76void SkBitmap::swap(SkBitmap& other) {
bsalomon@google.com586f48c2011-04-14 15:07:22 +000077 SkTSwap(fColorTable, other.fColorTable);
78 SkTSwap(fPixelRef, other.fPixelRef);
reed@google.com672588b2014-01-08 15:42:01 +000079 SkTSwap(fPixelRefOrigin, other.fPixelRefOrigin);
bsalomon@google.com586f48c2011-04-14 15:07:22 +000080 SkTSwap(fPixelLockCount, other.fPixelLockCount);
bsalomon@google.com586f48c2011-04-14 15:07:22 +000081 SkTSwap(fPixels, other.fPixels);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000082 SkTSwap(fInfo, other.fInfo);
bsalomon@google.com586f48c2011-04-14 15:07:22 +000083 SkTSwap(fRowBytes, other.fRowBytes);
bsalomon@google.com586f48c2011-04-14 15:07:22 +000084 SkTSwap(fFlags, other.fFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000085
86 SkDEBUGCODE(this->validate();)
87}
88
89void SkBitmap::reset() {
90 this->freePixels();
reed@android.com4516f472009-06-29 16:25:36 +000091 sk_bzero(this, sizeof(*this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000092}
93
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000094SkBitmap::Config SkBitmap::config() const {
95 return SkColorTypeToBitmapConfig(fInfo.colorType());
96}
97
reed@android.com8a1c16f2008-12-17 15:59:43 +000098int SkBitmap::ComputeBytesPerPixel(SkBitmap::Config config) {
99 int bpp;
100 switch (config) {
101 case kNo_Config:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 bpp = 0; // not applicable
103 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 case kA8_Config:
105 case kIndex8_Config:
106 bpp = 1;
107 break;
108 case kRGB_565_Config:
109 case kARGB_4444_Config:
110 bpp = 2;
111 break;
112 case kARGB_8888_Config:
113 bpp = 4;
114 break;
115 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000116 SkDEBUGFAIL("unknown config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 bpp = 0; // error
118 break;
119 }
120 return bpp;
121}
122
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000123size_t SkBitmap::ComputeRowBytes(Config c, int width) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000124 return SkColorTypeMinRowBytes(SkBitmapConfigToColorType(c), width);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125}
126
reed@google.com57212f92013-12-30 14:40:38 +0000127int64_t SkBitmap::ComputeSize64(Config config, int width, int height) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000128 SkColorType ct = SkBitmapConfigToColorType(config);
129 int64_t rowBytes = sk_64_mul(SkColorTypeBytesPerPixel(ct), width);
reed@google.com57212f92013-12-30 14:40:38 +0000130 return rowBytes * height;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131}
132
133size_t SkBitmap::ComputeSize(Config c, int width, int height) {
reed@google.com57212f92013-12-30 14:40:38 +0000134 int64_t size = SkBitmap::ComputeSize64(c, width, height);
135 return sk_64_isS32(size) ? sk_64_asS32(size) : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136}
137
reed@google.com57212f92013-12-30 14:40:38 +0000138int64_t SkBitmap::ComputeSafeSize64(Config config,
139 uint32_t width,
140 uint32_t height,
141 size_t rowBytes) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000142 SkImageInfo info = SkImageInfo::Make(width, height,
143 SkBitmapConfigToColorType(config),
144 kPremul_SkAlphaType);
145 return info.getSafeSize64(rowBytes);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000146}
147
148size_t SkBitmap::ComputeSafeSize(Config config,
149 uint32_t width,
150 uint32_t height,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000151 size_t rowBytes) {
reed@google.com57212f92013-12-30 14:40:38 +0000152 int64_t safeSize = ComputeSafeSize64(config, width, height, rowBytes);
153 int32_t safeSize32 = (int32_t)safeSize;
skia.committer@gmail.comf5e1f632013-12-31 07:01:36 +0000154
reed@google.com57212f92013-12-30 14:40:38 +0000155 if (safeSize32 != safeSize) {
156 safeSize32 = 0;
157 }
158 return safeSize32;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000159}
160
reed@google.com86b2e432012-03-15 21:17:03 +0000161void SkBitmap::getBounds(SkRect* bounds) const {
162 SkASSERT(bounds);
163 bounds->set(0, 0,
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000164 SkIntToScalar(fInfo.fWidth), SkIntToScalar(fInfo.fHeight));
reed@google.com86b2e432012-03-15 21:17:03 +0000165}
166
reed@google.com80e14592012-03-16 14:58:07 +0000167void SkBitmap::getBounds(SkIRect* bounds) const {
168 SkASSERT(bounds);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000169 bounds->set(0, 0, fInfo.fWidth, fInfo.fHeight);
reed@google.com80e14592012-03-16 14:58:07 +0000170}
171
reed@google.com86b2e432012-03-15 21:17:03 +0000172///////////////////////////////////////////////////////////////////////////////
173
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000174static bool validate_alphaType(SkColorType colorType, SkAlphaType alphaType,
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000175 SkAlphaType* canonical = NULL) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000176 switch (colorType) {
177 case kUnknown_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000178 alphaType = kIgnore_SkAlphaType;
179 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000180 case kAlpha_8_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000181 if (kUnpremul_SkAlphaType == alphaType) {
182 alphaType = kPremul_SkAlphaType;
183 }
184 // fall-through
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000185 case kIndex_8_SkColorType:
186 case kARGB_4444_SkColorType:
187 case kRGBA_8888_SkColorType:
188 case kBGRA_8888_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000189 if (kIgnore_SkAlphaType == alphaType) {
190 return false;
191 }
192 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000193 case kRGB_565_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +0000194 alphaType = kOpaque_SkAlphaType;
195 break;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000196 default:
197 return false;
reed@android.com149e2f62009-05-22 14:39:03 +0000198 }
reed@google.com383a6972013-10-21 14:00:07 +0000199 if (canonical) {
200 *canonical = alphaType;
201 }
202 return true;
203}
reed@android.com149e2f62009-05-22 14:39:03 +0000204
commit-bot@chromium.orgd5414e52014-02-13 22:30:38 +0000205bool SkBitmap::setConfig(const SkImageInfo& origInfo, size_t rowBytes) {
206 SkImageInfo info = origInfo;
207
208 if (!validate_alphaType(info.fColorType, info.fAlphaType,
209 &info.fAlphaType)) {
210 return reset_return_false(this);
211 }
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000212
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000213 // require that rowBytes fit in 31bits
214 int64_t mrb = info.minRowBytes64();
215 if ((int32_t)mrb != mrb) {
216 return reset_return_false(this);
reed@google.com383a6972013-10-21 14:00:07 +0000217 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000218 if ((int64_t)rowBytes != (int32_t)rowBytes) {
219 return reset_return_false(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220 }
reed@android.com89bb83a2009-05-29 21:30:42 +0000221
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000222 if (info.width() < 0 || info.height() < 0) {
223 return reset_return_false(this);
224 }
225
226 if (kUnknown_SkColorType == info.colorType()) {
227 rowBytes = 0;
228 } else if (0 == rowBytes) {
229 rowBytes = (size_t)mrb;
230 } else if (rowBytes < info.minRowBytes()) {
231 return reset_return_false(this);
reed@google.com383a6972013-10-21 14:00:07 +0000232 }
233
234 this->freePixels();
235
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000236 fInfo = info;
237 fRowBytes = SkToU32(rowBytes);
reed@google.com383a6972013-10-21 14:00:07 +0000238 return true;
reed@google.com383a6972013-10-21 14:00:07 +0000239}
240
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000241bool SkBitmap::setConfig(Config config, int width, int height, size_t rowBytes,
242 SkAlphaType alphaType) {
243 SkColorType ct = SkBitmapConfigToColorType(config);
244 return this->setConfig(SkImageInfo::Make(width, height, ct, alphaType),
245 rowBytes);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000246}
247
reed@google.com383a6972013-10-21 14:00:07 +0000248bool SkBitmap::setAlphaType(SkAlphaType alphaType) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000249 if (!validate_alphaType(fInfo.fColorType, alphaType, &alphaType)) {
reed@google.com383a6972013-10-21 14:00:07 +0000250 return false;
251 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000252 if (fInfo.fAlphaType != alphaType) {
253 fInfo.fAlphaType = alphaType;
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000254 if (fPixelRef) {
reed@google.comc1587f92014-01-28 16:05:39 +0000255 fPixelRef->changeAlphaType(alphaType);
commit-bot@chromium.org0e8d0d62014-01-27 15:41:07 +0000256 }
257 }
reed@google.com383a6972013-10-21 14:00:07 +0000258 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259}
260
261void SkBitmap::updatePixelsFromRef() const {
262 if (NULL != fPixelRef) {
263 if (fPixelLockCount > 0) {
reed@google.comff0da4f2012-05-17 13:14:52 +0000264 SkASSERT(fPixelRef->isLocked());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000265
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266 void* p = fPixelRef->pixels();
267 if (NULL != p) {
reed@google.com672588b2014-01-08 15:42:01 +0000268 p = (char*)p
reed@google.com303c4752014-01-09 20:00:14 +0000269 + fPixelRefOrigin.fY * fRowBytes
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000270 + fPixelRefOrigin.fX * fInfo.bytesPerPixel();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271 }
272 fPixels = p;
reed@google.com5f62ed72014-01-15 19:59:45 +0000273 fColorTable = fPixelRef->colorTable();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000274 } else {
275 SkASSERT(0 == fPixelLockCount);
276 fPixels = NULL;
reed@google.com5f62ed72014-01-15 19:59:45 +0000277 fColorTable = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000278 }
279 }
280}
281
reed@google.com9230ea22013-12-09 22:01:03 +0000282static bool config_to_colorType(SkBitmap::Config config, SkColorType* ctOut) {
283 SkColorType ct;
284 switch (config) {
285 case SkBitmap::kA8_Config:
286 ct = kAlpha_8_SkColorType;
287 break;
288 case SkBitmap::kIndex8_Config:
289 ct = kIndex_8_SkColorType;
290 break;
291 case SkBitmap::kRGB_565_Config:
292 ct = kRGB_565_SkColorType;
293 break;
294 case SkBitmap::kARGB_4444_Config:
295 ct = kARGB_4444_SkColorType;
296 break;
297 case SkBitmap::kARGB_8888_Config:
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000298 ct = kN32_SkColorType;
reed@google.com9230ea22013-12-09 22:01:03 +0000299 break;
300 case SkBitmap::kNo_Config:
301 default:
302 return false;
303 }
304 if (ctOut) {
305 *ctOut = ct;
306 }
307 return true;
308}
309
reed@google.com672588b2014-01-08 15:42:01 +0000310SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, int dx, int dy) {
reed@google.comdcea5302014-01-03 13:43:01 +0000311#ifdef SK_DEBUG
reed@google.com672588b2014-01-08 15:42:01 +0000312 if (pr) {
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000313 if (kUnknown_SkColorType != fInfo.colorType()) {
reed@google.comdcea5302014-01-03 13:43:01 +0000314 const SkImageInfo& prInfo = pr->info();
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000315 SkASSERT(fInfo.fWidth <= prInfo.fWidth);
316 SkASSERT(fInfo.fHeight <= prInfo.fHeight);
317 SkASSERT(fInfo.fColorType == prInfo.fColorType);
reed@google.comdcea5302014-01-03 13:43:01 +0000318 switch (prInfo.fAlphaType) {
319 case kIgnore_SkAlphaType:
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000320 SkASSERT(fInfo.fAlphaType == kIgnore_SkAlphaType);
reed@google.comdcea5302014-01-03 13:43:01 +0000321 break;
322 case kOpaque_SkAlphaType:
323 case kPremul_SkAlphaType:
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000324 SkASSERT(fInfo.fAlphaType == kOpaque_SkAlphaType ||
325 fInfo.fAlphaType == kPremul_SkAlphaType);
reed@google.comdcea5302014-01-03 13:43:01 +0000326 break;
327 case kUnpremul_SkAlphaType:
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000328 SkASSERT(fInfo.fAlphaType == kOpaque_SkAlphaType ||
329 fInfo.fAlphaType == kUnpremul_SkAlphaType);
reed@google.comdcea5302014-01-03 13:43:01 +0000330 break;
331 }
332 }
333 }
334#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000335
reed@google.com672588b2014-01-08 15:42:01 +0000336 if (pr) {
337 const SkImageInfo& info = pr->info();
338 fPixelRefOrigin.set(SkPin32(dx, 0, info.fWidth),
339 SkPin32(dy, 0, info.fHeight));
340 } else {
341 // ignore dx,dy if there is no pixelref
342 fPixelRefOrigin.setZero();
343 }
344
345 if (fPixelRef != pr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346 if (fPixelRef != pr) {
347 this->freePixels();
348 SkASSERT(NULL == fPixelRef);
weita@google.comf9ab99a2009-05-03 18:23:30 +0000349
reed@google.com82065d62011-02-07 15:30:46 +0000350 SkSafeRef(pr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000351 fPixelRef = pr;
352 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353 this->updatePixelsFromRef();
354 }
355
356 SkDEBUGCODE(this->validate();)
357 return pr;
358}
359
360void SkBitmap::lockPixels() const {
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000361 if (NULL != fPixelRef && 0 == sk_atomic_inc(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000362 fPixelRef->lockPixels();
363 this->updatePixelsFromRef();
364 }
365 SkDEBUGCODE(this->validate();)
366}
367
368void SkBitmap::unlockPixels() const {
369 SkASSERT(NULL == fPixelRef || fPixelLockCount > 0);
370
djsollen@google.com7c6d2642013-08-06 12:19:38 +0000371 if (NULL != fPixelRef && 1 == sk_atomic_dec(&fPixelLockCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000372 fPixelRef->unlockPixels();
373 this->updatePixelsFromRef();
374 }
375 SkDEBUGCODE(this->validate();)
376}
377
reed@google.com9c49bc32011-07-07 13:42:37 +0000378bool SkBitmap::lockPixelsAreWritable() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000379 return (fPixelRef) ? fPixelRef->lockPixelsAreWritable() : false;
reed@google.com9c49bc32011-07-07 13:42:37 +0000380}
381
reed@android.com8a1c16f2008-12-17 15:59:43 +0000382void SkBitmap::setPixels(void* p, SkColorTable* ctable) {
reed@google.com8e1034e2012-07-30 13:16:35 +0000383 if (NULL == p) {
reed@google.com672588b2014-01-08 15:42:01 +0000384 this->setPixelRef(NULL);
reed@google.com8e1034e2012-07-30 13:16:35 +0000385 return;
386 }
387
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000388 if (kUnknown_SkColorType == fInfo.colorType()) {
reed@google.com672588b2014-01-08 15:42:01 +0000389 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000390 return;
391 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000392
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000393 SkPixelRef* pr = SkMallocPixelRef::NewDirect(fInfo, p, fRowBytes, ctable);
reed@google.combf790232013-12-13 19:45:58 +0000394 if (NULL == pr) {
reed@google.com672588b2014-01-08 15:42:01 +0000395 this->setPixelRef(NULL);
reed@google.combf790232013-12-13 19:45:58 +0000396 return;
397 }
398
399 this->setPixelRef(pr)->unref();
400
djsollen@google.comc84b8332012-07-27 13:41:44 +0000401 // since we're already allocated, we lockPixels right away
402 this->lockPixels();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000403 SkDEBUGCODE(this->validate();)
404}
405
406bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) {
407 HeapAllocator stdalloc;
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000408
reed@android.com8a1c16f2008-12-17 15:59:43 +0000409 if (NULL == allocator) {
410 allocator = &stdalloc;
411 }
412 return allocator->allocPixelRef(this, ctable);
413}
414
reed@google.com9ebcac52014-01-24 18:53:42 +0000415///////////////////////////////////////////////////////////////////////////////
416
reed@google.com9ebcac52014-01-24 18:53:42 +0000417bool SkBitmap::allocPixels(const SkImageInfo& info, SkPixelRefFactory* factory,
418 SkColorTable* ctable) {
419 if (kIndex_8_SkColorType == info.fColorType && NULL == ctable) {
420 return reset_return_false(this);
421 }
422 if (!this->setConfig(info)) {
423 return reset_return_false(this);
424 }
425
426 SkMallocPixelRef::PRFactory defaultFactory;
427 if (NULL == factory) {
428 factory = &defaultFactory;
429 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000430
reed@google.com9ebcac52014-01-24 18:53:42 +0000431 SkPixelRef* pr = factory->create(info, ctable);
432 if (NULL == pr) {
433 return reset_return_false(this);
434 }
435 this->setPixelRef(pr)->unref();
436
437 // TODO: lockPixels could/should return bool or void*/NULL
438 this->lockPixels();
439 if (NULL == this->getPixels()) {
440 return reset_return_false(this);
441 }
442 return true;
443}
444
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000445bool SkBitmap::installPixels(const SkImageInfo& info, void* pixels, size_t rb, SkColorTable* ct,
446 void (*releaseProc)(void* addr, void* context), void* context) {
reed@google.comc2d768c2014-02-05 21:02:35 +0000447 if (!this->setConfig(info, rb)) {
reed@google.com9ebcac52014-01-24 18:53:42 +0000448 this->reset();
449 return false;
450 }
451
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000452 SkPixelRef* pr = SkMallocPixelRef::NewWithProc(info, rb, ct, pixels, releaseProc, context);
reed@google.com9ebcac52014-01-24 18:53:42 +0000453 if (!pr) {
454 this->reset();
455 return false;
456 }
457
458 this->setPixelRef(pr)->unref();
mike@reedtribe.org6e58cf32014-02-16 20:54:21 +0000459
460 // since we're already allocated, we lockPixels right away
461 this->lockPixels();
mike@reedtribe.org6e58cf32014-02-16 20:54:21 +0000462 SkDEBUGCODE(this->validate();)
reed@google.com9ebcac52014-01-24 18:53:42 +0000463 return true;
464}
465
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000466bool SkBitmap::installMaskPixels(const SkMask& mask) {
467 if (SkMask::kA8_Format != mask.fFormat) {
468 this->reset();
469 return false;
470 }
471 return this->installPixels(SkImageInfo::MakeA8(mask.fBounds.width(),
472 mask.fBounds.height()),
473 mask.fImage, mask.fRowBytes);
474}
475
reed@google.comeb9a46c2014-01-25 16:46:20 +0000476bool SkBitmap::allocConfigPixels(Config config, int width, int height,
477 bool isOpaque) {
478 SkColorType ct;
479 if (!config_to_colorType(config, &ct)) {
480 return false;
481 }
skia.committer@gmail.com33c749a2014-01-26 07:01:45 +0000482
reed@google.comeb9a46c2014-01-25 16:46:20 +0000483 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
reed@google.comeb9a46c2014-01-25 16:46:20 +0000484 return this->allocPixels(SkImageInfo::Make(width, height, ct, at));
485}
486
487///////////////////////////////////////////////////////////////////////////////
488
reed@android.com8a1c16f2008-12-17 15:59:43 +0000489void SkBitmap::freePixels() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000490 if (NULL != fPixelRef) {
491 if (fPixelLockCount > 0) {
492 fPixelRef->unlockPixels();
493 }
494 fPixelRef->unref();
495 fPixelRef = NULL;
reed@google.com672588b2014-01-08 15:42:01 +0000496 fPixelRefOrigin.setZero();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000497 }
498 fPixelLockCount = 0;
499 fPixels = NULL;
reed@google.com5f62ed72014-01-15 19:59:45 +0000500 fColorTable = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000501}
502
reed@android.com8a1c16f2008-12-17 15:59:43 +0000503uint32_t SkBitmap::getGenerationID() const {
djsollen@google.comc84b8332012-07-27 13:41:44 +0000504 return (fPixelRef) ? fPixelRef->getGenerationID() : 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000505}
506
507void SkBitmap::notifyPixelsChanged() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000508 SkASSERT(!this->isImmutable());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000509 if (fPixelRef) {
510 fPixelRef->notifyPixelsChanged();
511 }
512}
513
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000514GrTexture* SkBitmap::getTexture() const {
reed@android.comce4e53a2010-09-09 16:01:26 +0000515 return fPixelRef ? fPixelRef->getTexture() : NULL;
516}
517
reed@android.com8a1c16f2008-12-17 15:59:43 +0000518///////////////////////////////////////////////////////////////////////////////
519
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520/** We explicitly use the same allocator for our pixels that SkMask does,
521 so that we can freely assign memory allocated by one class to the other.
522 */
523bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst,
524 SkColorTable* ctable) {
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000525 const SkImageInfo info = dst->info();
526 if (kUnknown_SkColorType == info.colorType()) {
reed@google.combf790232013-12-13 19:45:58 +0000527// SkDebugf("unsupported config for info %d\n", dst->config());
528 return false;
529 }
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000530
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000531 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, dst->rowBytes(), ctable);
reed@google.combf790232013-12-13 19:45:58 +0000532 if (NULL == pr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000533 return false;
534 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000535
reed@google.com672588b2014-01-08 15:42:01 +0000536 dst->setPixelRef(pr)->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000537 // since we're already allocated, we lockPixels right away
538 dst->lockPixels();
539 return true;
540}
541
542///////////////////////////////////////////////////////////////////////////////
543
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000544bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize,
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000545 size_t dstRowBytes, bool preserveDstPad) const {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000546
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000547 if (0 == dstRowBytes) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000548 dstRowBytes = fRowBytes;
scroggo@google.com0ba4bf42013-02-25 16:02:36 +0000549 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000550
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000551 if (dstRowBytes < fInfo.minRowBytes() ||
552 dst == NULL || (getPixels() == NULL && pixelRef() == NULL)) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000553 return false;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000554 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000555
bsalomon@google.comc6980972011-11-02 19:57:21 +0000556 if (!preserveDstPad && static_cast<uint32_t>(dstRowBytes) == fRowBytes) {
reed@google.com44699382013-10-31 17:28:30 +0000557 size_t safeSize = this->getSafeSize();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000558 if (safeSize > dstSize || safeSize == 0)
559 return false;
560 else {
561 SkAutoLockPixels lock(*this);
562 // This implementation will write bytes beyond the end of each row,
563 // excluding the last row, if the bitmap's stride is greater than
564 // strictly required by the current config.
565 memcpy(dst, getPixels(), safeSize);
566
567 return true;
568 }
569 } else {
570 // If destination has different stride than us, then copy line by line.
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000571 if (fInfo.getSafeSize(dstRowBytes) > dstSize) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000572 return false;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000573 } else {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000574 // Just copy what we need on each line.
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000575 size_t rowBytes = fInfo.minRowBytes();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000576 SkAutoLockPixels lock(*this);
577 const uint8_t* srcP = reinterpret_cast<const uint8_t*>(getPixels());
578 uint8_t* dstP = reinterpret_cast<uint8_t*>(dst);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000579 for (int row = 0; row < fInfo.fHeight;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000580 row++, srcP += fRowBytes, dstP += dstRowBytes) {
581 memcpy(dstP, srcP, rowBytes);
582 }
583
584 return true;
585 }
586 }
587}
588
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000589///////////////////////////////////////////////////////////////////////////////
590
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000591bool SkBitmap::isImmutable() const {
junov@chromium.orgb0521292011-12-15 20:14:06 +0000592 return fPixelRef ? fPixelRef->isImmutable() :
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000593 fFlags & kImageIsImmutable_Flag;
junov@chromium.orgb0521292011-12-15 20:14:06 +0000594}
595
596void SkBitmap::setImmutable() {
597 if (fPixelRef) {
598 fPixelRef->setImmutable();
599 } else {
600 fFlags |= kImageIsImmutable_Flag;
601 }
602}
603
junov@google.com4ee7ae52011-06-30 17:30:49 +0000604bool SkBitmap::isVolatile() const {
605 return (fFlags & kImageIsVolatile_Flag) != 0;
606}
607
608void SkBitmap::setIsVolatile(bool isVolatile) {
609 if (isVolatile) {
610 fFlags |= kImageIsVolatile_Flag;
611 } else {
612 fFlags &= ~kImageIsVolatile_Flag;
613 }
614}
615
reed@android.com8a1c16f2008-12-17 15:59:43 +0000616void* SkBitmap::getAddr(int x, int y) const {
617 SkASSERT((unsigned)x < (unsigned)this->width());
618 SkASSERT((unsigned)y < (unsigned)this->height());
619
620 char* base = (char*)this->getPixels();
621 if (base) {
622 base += y * this->rowBytes();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000623 switch (this->colorType()) {
624 case kRGBA_8888_SkColorType:
625 case kBGRA_8888_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000626 base += x << 2;
627 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000628 case kARGB_4444_SkColorType:
629 case kRGB_565_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000630 base += x << 1;
631 break;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000632 case kAlpha_8_SkColorType:
633 case kIndex_8_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000634 base += x;
635 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000636 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000637 SkDEBUGFAIL("Can't return addr for config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000638 base = NULL;
639 break;
640 }
641 }
642 return base;
643}
644
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000645SkColor SkBitmap::getColor(int x, int y) const {
646 SkASSERT((unsigned)x < (unsigned)this->width());
647 SkASSERT((unsigned)y < (unsigned)this->height());
648
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000649 switch (this->colorType()) {
650 case kAlpha_8_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000651 uint8_t* addr = this->getAddr8(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000652 return SkColorSetA(0, addr[0]);
653 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000654 case kIndex_8_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000655 SkPMColor c = this->getIndex8Color(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000656 return SkUnPreMultiply::PMColorToColor(c);
657 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000658 case kRGB_565_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000659 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000660 return SkPixel16ToColor(addr[0]);
661 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000662 case kARGB_4444_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000663 uint16_t* addr = this->getAddr16(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000664 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
665 return SkUnPreMultiply::PMColorToColor(c);
666 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000667 case kBGRA_8888_SkColorType:
668 case kRGBA_8888_SkColorType: {
reed@google.com3b521d02011-04-29 11:53:41 +0000669 uint32_t* addr = this->getAddr32(x, y);
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000670 return SkUnPreMultiply::PMColorToColor(addr[0]);
671 }
rmistry@google.comd6bab022013-12-02 13:50:38 +0000672 default:
vandebo@chromium.org112706d2011-02-24 22:50:55 +0000673 SkASSERT(false);
674 return 0;
675 }
676 SkASSERT(false); // Not reached.
677 return 0;
678}
679
reed@google.com2a7579d2012-11-07 18:30:18 +0000680bool SkBitmap::ComputeIsOpaque(const SkBitmap& bm) {
681 SkAutoLockPixels alp(bm);
682 if (!bm.getPixels()) {
683 return false;
684 }
685
686 const int height = bm.height();
687 const int width = bm.width();
688
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000689 switch (bm.colorType()) {
690 case kAlpha_8_SkColorType: {
reed@google.com2a7579d2012-11-07 18:30:18 +0000691 unsigned a = 0xFF;
692 for (int y = 0; y < height; ++y) {
693 const uint8_t* row = bm.getAddr8(0, y);
694 for (int x = 0; x < width; ++x) {
695 a &= row[x];
696 }
697 if (0xFF != a) {
698 return false;
699 }
700 }
701 return true;
702 } break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000703 case kIndex_8_SkColorType: {
reed@google.com2a7579d2012-11-07 18:30:18 +0000704 SkAutoLockColors alc(bm);
705 const SkPMColor* table = alc.colors();
706 if (!table) {
707 return false;
708 }
reed@google.com140d7282013-01-07 20:25:04 +0000709 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000710 for (int i = bm.getColorTable()->count() - 1; i >= 0; --i) {
711 c &= table[i];
712 }
713 return 0xFF == SkGetPackedA32(c);
714 } break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000715 case kRGB_565_SkColorType:
reed@google.com2a7579d2012-11-07 18:30:18 +0000716 return true;
717 break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000718 case kARGB_4444_SkColorType: {
reed@google.com2a7579d2012-11-07 18:30:18 +0000719 unsigned c = 0xFFFF;
720 for (int y = 0; y < height; ++y) {
721 const SkPMColor16* row = bm.getAddr16(0, y);
722 for (int x = 0; x < width; ++x) {
723 c &= row[x];
724 }
725 if (0xF != SkGetPackedA4444(c)) {
726 return false;
727 }
728 }
729 return true;
730 } break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000731 case kBGRA_8888_SkColorType:
732 case kRGBA_8888_SkColorType: {
reed@google.com140d7282013-01-07 20:25:04 +0000733 SkPMColor c = (SkPMColor)~0;
reed@google.com2a7579d2012-11-07 18:30:18 +0000734 for (int y = 0; y < height; ++y) {
735 const SkPMColor* row = bm.getAddr32(0, y);
736 for (int x = 0; x < width; ++x) {
737 c &= row[x];
738 }
739 if (0xFF != SkGetPackedA32(c)) {
740 return false;
741 }
742 }
743 return true;
744 }
745 default:
746 break;
747 }
748 return false;
749}
750
751
reed@android.com8a1c16f2008-12-17 15:59:43 +0000752///////////////////////////////////////////////////////////////////////////////
753///////////////////////////////////////////////////////////////////////////////
754
reed@google.com45f746f2013-06-21 19:51:31 +0000755static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) {
756 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) |
757 (SkR32To4444(r) << SK_R4444_SHIFT) |
758 (SkG32To4444(g) << SK_G4444_SHIFT) |
759 (SkB32To4444(b) << SK_B4444_SHIFT);
760 return SkToU16(pixel);
761}
762
reed@google.com60d32352013-06-28 19:40:50 +0000763void SkBitmap::internalErase(const SkIRect& area,
764 U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
765#ifdef SK_DEBUG
reed@android.com8a1c16f2008-12-17 15:59:43 +0000766 SkDEBUGCODE(this->validate();)
reed@google.com60d32352013-06-28 19:40:50 +0000767 SkASSERT(!area.isEmpty());
768 {
reed@google.com92833f92013-06-28 19:47:43 +0000769 SkIRect total = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000770 SkASSERT(total.contains(area));
771 }
772#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000773
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000774 switch (fInfo.colorType()) {
775 case kUnknown_SkColorType:
776 case kIndex_8_SkColorType:
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000777 return; // can't erase. Should we bzero so the memory is not uninitialized?
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000778 default:
779 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000780 }
781
782 SkAutoLockPixels alp(*this);
783 // perform this check after the lock call
784 if (!this->readyToDraw()) {
785 return;
786 }
787
reed@google.com60d32352013-06-28 19:40:50 +0000788 int height = area.height();
789 const int width = area.width();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000790 const int rowBytes = fRowBytes;
791
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000792 switch (this->colorType()) {
793 case kAlpha_8_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000794 uint8_t* p = this->getAddr8(area.fLeft, area.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000795 while (--height >= 0) {
796 memset(p, a, width);
797 p += rowBytes;
798 }
799 break;
800 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000801 case kARGB_4444_SkColorType:
802 case kRGB_565_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000803 uint16_t* p = this->getAddr16(area.fLeft, area.fTop);;
reed@google.com45f746f2013-06-21 19:51:31 +0000804 uint16_t v;
skia.committer@gmail.com020b25b2013-06-22 07:00:58 +0000805
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000806 // make rgb premultiplied
807 if (255 != a) {
808 r = SkAlphaMul(r, a);
809 g = SkAlphaMul(g, a);
810 b = SkAlphaMul(b, a);
811 }
812
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000813 if (kARGB_4444_SkColorType == this->colorType()) {
reed@google.com45f746f2013-06-21 19:51:31 +0000814 v = pack_8888_to_4444(a, r, g, b);
815 } else {
816 v = SkPackRGB16(r >> (8 - SK_R16_BITS),
817 g >> (8 - SK_G16_BITS),
818 b >> (8 - SK_B16_BITS));
819 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000820 while (--height >= 0) {
821 sk_memset16(p, v, width);
822 p = (uint16_t*)((char*)p + rowBytes);
823 }
824 break;
825 }
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000826 case kBGRA_8888_SkColorType:
827 case kRGBA_8888_SkColorType: {
reed@google.com60d32352013-06-28 19:40:50 +0000828 uint32_t* p = this->getAddr32(area.fLeft, area.fTop);
commit-bot@chromium.org7669a772014-03-27 15:30:35 +0000829
830 if (255 != a && kPremul_SkAlphaType == this->alphaType()) {
831 r = SkAlphaMul(r, a);
832 g = SkAlphaMul(g, a);
833 b = SkAlphaMul(b, a);
834 }
835 uint32_t v = kRGBA_8888_SkColorType == this->colorType() ?
836 SkPackARGB_as_RGBA(a, r, g, b) : SkPackARGB_as_BGRA(a, r, g, b);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000837
838 while (--height >= 0) {
839 sk_memset32(p, v, width);
840 p = (uint32_t*)((char*)p + rowBytes);
841 }
842 break;
843 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000844 default:
845 return; // no change, so don't call notifyPixelsChanged()
reed@android.com8a1c16f2008-12-17 15:59:43 +0000846 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000847
reed@android.com8a1c16f2008-12-17 15:59:43 +0000848 this->notifyPixelsChanged();
849}
850
reed@google.com60d32352013-06-28 19:40:50 +0000851void SkBitmap::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const {
reed@google.com92833f92013-06-28 19:47:43 +0000852 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000853 if (!area.isEmpty()) {
854 this->internalErase(area, a, r, g, b);
855 }
856}
857
858void SkBitmap::eraseArea(const SkIRect& rect, SkColor c) const {
reed@google.com92833f92013-06-28 19:47:43 +0000859 SkIRect area = { 0, 0, this->width(), this->height() };
reed@google.com60d32352013-06-28 19:40:50 +0000860 if (area.intersect(rect)) {
861 this->internalErase(area, SkColorGetA(c), SkColorGetR(c),
862 SkColorGetG(c), SkColorGetB(c));
863 }
864}
865
reed@android.com8a1c16f2008-12-17 15:59:43 +0000866//////////////////////////////////////////////////////////////////////////////////////
867//////////////////////////////////////////////////////////////////////////////////////
868
reed@android.com8a1c16f2008-12-17 15:59:43 +0000869bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
870 SkDEBUGCODE(this->validate();)
871
djsollen@google.comc84b8332012-07-27 13:41:44 +0000872 if (NULL == result || NULL == fPixelRef) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000873 return false; // no src pixels
874 }
875
876 SkIRect srcRect, r;
877 srcRect.set(0, 0, this->width(), this->height());
878 if (!r.intersect(srcRect, subset)) {
879 return false; // r is empty (i.e. no intersection)
880 }
881
scroggo@google.coma2a31922012-12-07 19:14:45 +0000882 if (fPixelRef->getTexture() != NULL) {
883 // Do a deep copy
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +0000884 SkPixelRef* pixelRef = fPixelRef->deepCopy(this->config(), &subset);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000885 if (pixelRef != NULL) {
886 SkBitmap dst;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000887 dst.setConfig(SkImageInfo::Make(subset.width(), subset.height(),
888 this->colorType(), this->alphaType()));
scroggo@google.coma2a31922012-12-07 19:14:45 +0000889 dst.setIsVolatile(this->isVolatile());
scroggo@google.coma2a31922012-12-07 19:14:45 +0000890 dst.setPixelRef(pixelRef)->unref();
891 SkDEBUGCODE(dst.validate());
892 result->swap(dst);
893 return true;
894 }
895 }
896
scroggo@google.coma2a31922012-12-07 19:14:45 +0000897 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
898 // exited above.
899 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
900 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
901
reed@android.com8a1c16f2008-12-17 15:59:43 +0000902 SkBitmap dst;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000903 dst.setConfig(SkImageInfo::Make(r.width(), r.height(), this->colorType(), this->alphaType()),
904 this->rowBytes());
skyostil@google.com0eb75762012-01-16 10:45:53 +0000905 dst.setIsVolatile(this->isVolatile());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000906
907 if (fPixelRef) {
reed@google.com672588b2014-01-08 15:42:01 +0000908 SkIPoint origin = fPixelRefOrigin;
909 origin.fX += r.fLeft;
910 origin.fY += r.fTop;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000911 // share the pixelref with a custom offset
reed@google.com672588b2014-01-08 15:42:01 +0000912 dst.setPixelRef(fPixelRef, origin);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000913 }
914 SkDEBUGCODE(dst.validate();)
915
916 // we know we're good, so commit to result
917 result->swap(dst);
918 return true;
919}
920
921///////////////////////////////////////////////////////////////////////////////
922
923#include "SkCanvas.h"
924#include "SkPaint.h"
925
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000926bool SkBitmap::canCopyTo(SkColorType dstColorType) const {
927 if (this->colorType() == kUnknown_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000928 return false;
929 }
930
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000931 bool sameConfigs = (this->colorType() == dstColorType);
932 switch (dstColorType) {
933 case kAlpha_8_SkColorType:
934 case kRGB_565_SkColorType:
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000935 case kRGBA_8888_SkColorType:
936 case kBGRA_8888_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000937 break;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000938 case kIndex_8_SkColorType:
weita@google.comf9ab99a2009-05-03 18:23:30 +0000939 if (!sameConfigs) {
940 return false;
941 }
942 break;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000943 case kARGB_4444_SkColorType:
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000944 return sameConfigs || kN32_SkColorType == this->colorType();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000945 default:
946 return false;
947 }
reed@android.comfbaa88d2009-05-06 17:44:34 +0000948 return true;
949}
950
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000951bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType,
952 Allocator* alloc) const {
953 if (!this->canCopyTo(dstColorType)) {
reed@android.comfbaa88d2009-05-06 17:44:34 +0000954 return false;
955 }
956
reed@google.com50dfa012011-04-01 19:05:36 +0000957 // if we have a texture, first get those pixels
958 SkBitmap tmpSrc;
959 const SkBitmap* src = this;
960
scroggo@google.coma2a31922012-12-07 19:14:45 +0000961 if (fPixelRef) {
962 SkIRect subset;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000963 subset.setXYWH(fPixelRefOrigin.fX, fPixelRefOrigin.fY,
964 fInfo.width(), fInfo.height());
reed@google.com672588b2014-01-08 15:42:01 +0000965 if (fPixelRef->readPixels(&tmpSrc, &subset)) {
966 SkASSERT(tmpSrc.width() == this->width());
967 SkASSERT(tmpSrc.height() == this->height());
reed@google.com50dfa012011-04-01 19:05:36 +0000968
reed@google.com672588b2014-01-08 15:42:01 +0000969 // did we get lucky and we can just return tmpSrc?
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000970 if (tmpSrc.colorType() == dstColorType && NULL == alloc) {
reed@google.com672588b2014-01-08 15:42:01 +0000971 dst->swap(tmpSrc);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000972 // If the result is an exact copy, clone the gen ID.
973 if (dst->pixelRef() && dst->pixelRef()->info() == fPixelRef->info()) {
reed@google.com672588b2014-01-08 15:42:01 +0000974 dst->pixelRef()->cloneGenID(*fPixelRef);
scroggo@google.coma2a31922012-12-07 19:14:45 +0000975 }
reed@google.com672588b2014-01-08 15:42:01 +0000976 return true;
scroggo@google.comd5764e82012-08-22 15:00:05 +0000977 }
reed@google.com672588b2014-01-08 15:42:01 +0000978
979 // fall through to the raster case
980 src = &tmpSrc;
reed@google.com50dfa012011-04-01 19:05:36 +0000981 }
reed@android.comfbaa88d2009-05-06 17:44:34 +0000982 }
reed@android.com311c82d2009-05-05 23:13:23 +0000983
reed@google.com50dfa012011-04-01 19:05:36 +0000984 // we lock this now, since we may need its colortable
985 SkAutoLockPixels srclock(*src);
986 if (!src->readyToDraw()) {
987 return false;
988 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000989
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000990 // The only way to be readyToDraw is if fPixelRef is non NULL.
991 SkASSERT(fPixelRef != NULL);
992
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000993 SkImageInfo dstInfo = src->info();
994 dstInfo.fColorType = dstColorType;
995
reed@google.com50dfa012011-04-01 19:05:36 +0000996 SkBitmap tmpDst;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000997 if (!tmpDst.setConfig(dstInfo)) {
998 return false;
999 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001000
weita@google.comf9ab99a2009-05-03 18:23:30 +00001001 // allocate colortable if srcConfig == kIndex8_Config
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001002 SkAutoTUnref<SkColorTable> ctable;
1003 if (dstColorType == kIndex_8_SkColorType) {
1004 // TODO: can we just ref() the src colortable? Is it reentrant-safe?
1005 ctable.reset(SkNEW_ARGS(SkColorTable, (*src->getColorTable())));
1006 }
reed@google.com50dfa012011-04-01 19:05:36 +00001007 if (!tmpDst.allocPixels(alloc, ctable)) {
weita@google.comf9ab99a2009-05-03 18:23:30 +00001008 return false;
1009 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001010
reed@google.com50dfa012011-04-01 19:05:36 +00001011 if (!tmpDst.readyToDraw()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001012 // allocator/lock failed
1013 return false;
1014 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001015
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001016 // pixelRef must be non NULL or tmpDst.readyToDraw() would have
1017 // returned false.
1018 SkASSERT(tmpDst.pixelRef() != NULL);
1019
reed@android.comfbaa88d2009-05-06 17:44:34 +00001020 /* do memcpy for the same configs cases, else use drawing
weita@google.comf9ab99a2009-05-03 18:23:30 +00001021 */
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001022 if (src->colorType() == dstColorType) {
reed@google.com50dfa012011-04-01 19:05:36 +00001023 if (tmpDst.getSize() == src->getSize()) {
1024 memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize());
scroggo@google.comd5764e82012-08-22 15:00:05 +00001025 SkPixelRef* pixelRef = tmpDst.pixelRef();
scroggo@google.com5ccae2c2014-01-15 16:56:52 +00001026
1027 // In order to reach this point, we know that the width, config and
1028 // rowbytes of the SkPixelRefs are the same, but it is possible for
1029 // the heights to differ, if this SkBitmap's height is a subset of
1030 // fPixelRef. Only if the SkPixelRefs' heights match are we
1031 // guaranteed that this is an exact copy, meaning we should clone
1032 // the genID.
1033 if (pixelRef->info().fHeight == fPixelRef->info().fHeight) {
1034 // TODO: what to do if the two infos match, BUT
1035 // fPixelRef is premul and pixelRef is opaque?
1036 // skipping assert for now
1037 // https://code.google.com/p/skia/issues/detail?id=2012
1038// SkASSERT(pixelRef->info() == fPixelRef->info());
1039 SkASSERT(pixelRef->info().fWidth == fPixelRef->info().fWidth);
1040 SkASSERT(pixelRef->info().fColorType == fPixelRef->info().fColorType);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00001041 pixelRef->cloneGenID(*fPixelRef);
scroggo@google.comd5764e82012-08-22 15:00:05 +00001042 }
reed@android.com311c82d2009-05-05 23:13:23 +00001043 } else {
reed@google.com50dfa012011-04-01 19:05:36 +00001044 const char* srcP = reinterpret_cast<const char*>(src->getPixels());
1045 char* dstP = reinterpret_cast<char*>(tmpDst.getPixels());
reed@android.com311c82d2009-05-05 23:13:23 +00001046 // to be sure we don't read too much, only copy our logical pixels
reed@google.com50dfa012011-04-01 19:05:36 +00001047 size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel();
1048 for (int y = 0; y < tmpDst.height(); y++) {
reed@android.com311c82d2009-05-05 23:13:23 +00001049 memcpy(dstP, srcP, bytesToCopy);
reed@google.com50dfa012011-04-01 19:05:36 +00001050 srcP += src->rowBytes();
1051 dstP += tmpDst.rowBytes();
reed@android.com311c82d2009-05-05 23:13:23 +00001052 }
1053 }
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001054 } else if (kARGB_4444_SkColorType == dstColorType
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +00001055 && kN32_SkColorType == src->colorType()) {
scroggo@google.com8dc8bc52013-08-07 19:16:05 +00001056 SkASSERT(src->height() == tmpDst.height());
1057 SkASSERT(src->width() == tmpDst.width());
1058 for (int y = 0; y < src->height(); ++y) {
1059 SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*) tmpDst.getAddr16(0, y);
1060 SkPMColor* SK_RESTRICT srcRow = (SkPMColor*) src->getAddr32(0, y);
1061 DITHER_4444_SCAN(y);
1062 for (int x = 0; x < src->width(); ++x) {
1063 dstRow[x] = SkDitherARGB32To4444(srcRow[x],
1064 DITHER_VALUE(x));
1065 }
1066 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001067 } else {
robertphillips@google.com0197b322013-10-10 15:48:16 +00001068 // Always clear the dest in case one of the blitters accesses it
1069 // TODO: switch the allocation of tmpDst to call sk_calloc_throw
1070 tmpDst.eraseColor(SK_ColorTRANSPARENT);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001071
reed@google.com50dfa012011-04-01 19:05:36 +00001072 SkCanvas canvas(tmpDst);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001073 SkPaint paint;
1074
1075 paint.setDither(true);
reed@google.com50dfa012011-04-01 19:05:36 +00001076 canvas.drawBitmap(*src, 0, 0, &paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001077 }
1078
reed@google.com50dfa012011-04-01 19:05:36 +00001079 dst->swap(tmpDst);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001080 return true;
1081}
1082
commit-bot@chromium.orgfab349c2014-03-05 02:34:58 +00001083bool SkBitmap::deepCopyTo(SkBitmap* dst) const {
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001084 const SkBitmap::Config dstConfig = this->config();
1085 const SkColorType dstCT = SkBitmapConfigToColorType(dstConfig);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001086
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001087 if (!this->canCopyTo(dstCT)) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001088 return false;
1089 }
1090
1091 // If we have a PixelRef, and it supports deep copy, use it.
1092 // Currently supported only by texture-backed bitmaps.
1093 if (fPixelRef) {
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001094 SkPixelRef* pixelRef = fPixelRef->deepCopy(dstConfig);
1095 if (pixelRef) {
1096 uint32_t rowBytes;
1097 if (this->colorType() == dstCT) {
1098 // Since there is no subset to pass to deepCopy, and deepCopy
1099 // succeeded, the new pixel ref must be identical.
1100 SkASSERT(fPixelRef->info() == pixelRef->info());
1101 pixelRef->cloneGenID(*fPixelRef);
1102 // Use the same rowBytes as the original.
1103 rowBytes = fRowBytes;
1104 } else {
1105 // With the new config, an appropriate fRowBytes will be computed by setConfig.
1106 rowBytes = 0;
1107 }
1108
1109 SkImageInfo info = fInfo;
1110 info.fColorType = dstCT;
1111 if (!dst->setConfig(info, rowBytes)) {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001112 return false;
1113 }
commit-bot@chromium.orgeeef0cc2014-05-21 15:58:00 +00001114 dst->setPixelRef(pixelRef, fPixelRefOrigin)->unref();
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001115 return true;
1116 }
1117 }
1118
1119 if (this->getTexture()) {
1120 return false;
1121 } else {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +00001122 return this->copyTo(dst, dstCT, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001123 }
1124}
1125
reed@android.com8a1c16f2008-12-17 15:59:43 +00001126///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00001127
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +00001128static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001129 int alphaRowBytes) {
1130 SkASSERT(alpha != NULL);
1131 SkASSERT(alphaRowBytes >= src.width());
1132
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001133 SkColorType colorType = src.colorType();
1134 int w = src.width();
1135 int h = src.height();
1136 size_t rb = src.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001137
reed@android.com1cdcb512009-08-24 19:11:00 +00001138 SkAutoLockPixels alp(src);
1139 if (!src.readyToDraw()) {
1140 // zero out the alpha buffer and return
1141 while (--h >= 0) {
1142 memset(alpha, 0, w);
1143 alpha += alphaRowBytes;
1144 }
1145 return false;
1146 }
reed@google.com82065d62011-02-07 15:30:46 +00001147
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001148 if (kAlpha_8_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001149 const uint8_t* s = src.getAddr8(0, 0);
1150 while (--h >= 0) {
1151 memcpy(alpha, s, w);
1152 s += rb;
1153 alpha += alphaRowBytes;
1154 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001155 } else if (kN32_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001156 const SkPMColor* SK_RESTRICT s = src.getAddr32(0, 0);
1157 while (--h >= 0) {
1158 for (int x = 0; x < w; x++) {
1159 alpha[x] = SkGetPackedA32(s[x]);
1160 }
1161 s = (const SkPMColor*)((const char*)s + rb);
1162 alpha += alphaRowBytes;
1163 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001164 } else if (kARGB_4444_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001165 const SkPMColor16* SK_RESTRICT s = src.getAddr16(0, 0);
1166 while (--h >= 0) {
1167 for (int x = 0; x < w; x++) {
1168 alpha[x] = SkPacked4444ToA32(s[x]);
1169 }
1170 s = (const SkPMColor16*)((const char*)s + rb);
1171 alpha += alphaRowBytes;
1172 }
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001173 } else if (kIndex_8_SkColorType == colorType && !src.isOpaque()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001174 SkColorTable* ct = src.getColorTable();
1175 if (ct) {
1176 const SkPMColor* SK_RESTRICT table = ct->lockColors();
1177 const uint8_t* SK_RESTRICT s = src.getAddr8(0, 0);
1178 while (--h >= 0) {
1179 for (int x = 0; x < w; x++) {
1180 alpha[x] = SkGetPackedA32(table[s[x]]);
1181 }
1182 s += rb;
1183 alpha += alphaRowBytes;
1184 }
reed@google.com0a6151d2013-10-10 14:44:56 +00001185 ct->unlockColors();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001186 }
1187 } else { // src is opaque, so just fill alpha[] with 0xFF
1188 memset(alpha, 0xFF, h * alphaRowBytes);
1189 }
reed@android.com1cdcb512009-08-24 19:11:00 +00001190 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001191}
1192
1193#include "SkPaint.h"
1194#include "SkMaskFilter.h"
1195#include "SkMatrix.h"
1196
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001197bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
djsollen@google.com57f49692011-02-23 20:46:31 +00001198 Allocator *allocator, SkIPoint* offset) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001199 SkDEBUGCODE(this->validate();)
1200
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001201 SkBitmap tmpBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001202 SkMatrix identity;
1203 SkMask srcM, dstM;
1204
1205 srcM.fBounds.set(0, 0, this->width(), this->height());
1206 srcM.fRowBytes = SkAlign4(this->width());
1207 srcM.fFormat = SkMask::kA8_Format;
1208
1209 SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL;
1210
1211 // compute our (larger?) dst bounds if we have a filter
1212 if (NULL != filter) {
1213 identity.reset();
1214 srcM.fImage = NULL;
1215 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1216 goto NO_FILTER_CASE;
1217 }
1218 dstM.fRowBytes = SkAlign4(dstM.fBounds.width());
1219 } else {
1220 NO_FILTER_CASE:
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001221 tmpBitmap.setConfig(SkImageInfo::MakeA8(this->width(), this->height()), srcM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001222 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1223 // Allocation of pixels for alpha bitmap failed.
1224 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1225 tmpBitmap.width(), tmpBitmap.height());
1226 return false;
1227 }
1228 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001229 if (offset) {
1230 offset->set(0, 0);
1231 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001232 tmpBitmap.swap(*dst);
1233 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001234 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001235 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize());
1236 SkAutoMaskFreeImage srcCleanup(srcM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001237
1238 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes);
1239 if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
1240 goto NO_FILTER_CASE;
1241 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001242 SkAutoMaskFreeImage dstCleanup(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001243
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001244 tmpBitmap.setConfig(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
1245 dstM.fRowBytes);
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001246 if (!tmpBitmap.allocPixels(allocator, NULL)) {
1247 // Allocation of pixels for alpha bitmap failed.
1248 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
1249 tmpBitmap.width(), tmpBitmap.height());
1250 return false;
1251 }
1252 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001253 if (offset) {
1254 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop);
1255 }
djsollen@google.comcd9d69b2011-03-14 20:30:14 +00001256 SkDEBUGCODE(tmpBitmap.validate();)
1257
1258 tmpBitmap.swap(*dst);
1259 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001260}
1261
1262///////////////////////////////////////////////////////////////////////////////
1263
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001264void SkBitmap::WriteRawPixels(SkWriteBuffer* buffer, const SkBitmap& bitmap) {
1265 const SkImageInfo info = bitmap.info();
1266 SkAutoLockPixels alp(bitmap);
1267 if (0 == info.width() || 0 == info.height() || NULL == bitmap.getPixels()) {
1268 buffer->writeUInt(0); // instead of snugRB, signaling no pixels
1269 return;
1270 }
1271
1272 const size_t snugRB = info.width() * info.bytesPerPixel();
1273 const char* src = (const char*)bitmap.getPixels();
1274 const size_t ramRB = bitmap.rowBytes();
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001275
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001276 buffer->write32(SkToU32(snugRB));
1277 info.flatten(*buffer);
1278
1279 const size_t size = snugRB * info.height();
1280 SkAutoMalloc storage(size);
1281 char* dst = (char*)storage.get();
1282 for (int y = 0; y < info.height(); ++y) {
1283 memcpy(dst, src, snugRB);
1284 dst += snugRB;
1285 src += ramRB;
1286 }
1287 buffer->writeByteArray(storage.get(), size);
1288
1289 SkColorTable* ct = bitmap.getColorTable();
1290 if (kIndex_8_SkColorType == info.colorType() && ct) {
1291 buffer->writeBool(true);
1292 ct->writeToBuffer(*buffer);
1293 } else {
1294 buffer->writeBool(false);
1295 }
1296}
1297
1298bool SkBitmap::ReadRawPixels(SkReadBuffer* buffer, SkBitmap* bitmap) {
1299 const size_t snugRB = buffer->readUInt();
1300 if (0 == snugRB) { // no pixels
1301 return false;
1302 }
1303
1304 SkImageInfo info;
1305 info.unflatten(*buffer);
1306
1307 const size_t ramRB = info.minRowBytes();
1308 const int height = info.height();
1309 const size_t snugSize = snugRB * height;
1310 const size_t ramSize = ramRB * height;
1311 SkASSERT(snugSize <= ramSize);
1312
1313 char* dst = (char*)sk_malloc_throw(ramSize);
1314 buffer->readByteArray(dst, snugSize);
1315 SkAutoDataUnref data(SkData::NewFromMalloc(dst, ramSize));
1316
1317 if (snugSize != ramSize) {
1318 const char* srcRow = dst + snugRB * (height - 1);
1319 char* dstRow = dst + ramRB * (height - 1);
1320 for (int y = height - 1; y >= 1; --y) {
1321 memmove(dstRow, srcRow, snugRB);
1322 srcRow -= snugRB;
1323 dstRow -= ramRB;
1324 }
1325 SkASSERT(srcRow == dstRow); // first row does not need to be moved
1326 }
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001327
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001328 SkAutoTUnref<SkColorTable> ctable;
1329 if (buffer->readBool()) {
1330 ctable.reset(SkNEW_ARGS(SkColorTable, (*buffer)));
1331 }
skia.committer@gmail.com3c134a92014-05-24 03:05:26 +00001332
commit-bot@chromium.org968edca2014-05-23 13:21:55 +00001333 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewWithData(info, info.minRowBytes(),
1334 ctable.get(), data.get()));
1335 bitmap->setConfig(pr->info());
1336 bitmap->setPixelRef(pr, 0, 0);
1337 return true;
1338}
1339
reed@android.com8a1c16f2008-12-17 15:59:43 +00001340enum {
1341 SERIALIZE_PIXELTYPE_NONE,
djsollen@google.com21830d92012-08-07 19:49:41 +00001342 SERIALIZE_PIXELTYPE_REF_DATA
reed@android.com8a1c16f2008-12-17 15:59:43 +00001343};
1344
commit-bot@chromium.org851155c2014-05-27 14:03:51 +00001345void SkBitmap::legacyUnflatten(SkReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001346 this->reset();
weita@google.comf9ab99a2009-05-03 18:23:30 +00001347
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001348 SkImageInfo info;
1349 info.unflatten(buffer);
1350 size_t rowBytes = buffer.readInt();
commit-bot@chromium.org33fed142014-02-13 18:46:13 +00001351 if (!buffer.validate((info.width() >= 0) && (info.height() >= 0) &&
1352 SkColorTypeIsValid(info.fColorType) &&
1353 SkAlphaTypeIsValid(info.fAlphaType) &&
1354 validate_alphaType(info.fColorType, info.fAlphaType) &&
1355 info.validRowBytes(rowBytes))) {
1356 return;
1357 }
weita@google.comf9ab99a2009-05-03 18:23:30 +00001358
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001359 bool configIsValid = this->setConfig(info, rowBytes);
1360 buffer.validate(configIsValid);
weita@google.comf9ab99a2009-05-03 18:23:30 +00001361
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00001362 int reftype = buffer.readInt();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001363 if (buffer.validate((SERIALIZE_PIXELTYPE_REF_DATA == reftype) ||
1364 (SERIALIZE_PIXELTYPE_NONE == reftype))) {
1365 switch (reftype) {
1366 case SERIALIZE_PIXELTYPE_REF_DATA: {
reed@google.com672588b2014-01-08 15:42:01 +00001367 SkIPoint origin;
1368 origin.fX = buffer.readInt();
1369 origin.fY = buffer.readInt();
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001370 size_t offset = origin.fY * rowBytes + origin.fX * info.bytesPerPixel();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001371 SkPixelRef* pr = buffer.readPixelRef();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001372 if (!buffer.validate((NULL == pr) ||
1373 (pr->getAllocatedSizeInBytes() >= (offset + this->getSafeSize())))) {
reed@google.com672588b2014-01-08 15:42:01 +00001374 origin.setZero();
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +00001375 }
reed@google.com672588b2014-01-08 15:42:01 +00001376 SkSafeUnref(this->setPixelRef(pr, origin));
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +00001377 break;
1378 }
1379 case SERIALIZE_PIXELTYPE_NONE:
1380 break;
1381 default:
1382 SkDEBUGFAIL("unrecognized pixeltype in serialized data");
1383 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001384 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001385 }
1386}
1387
1388///////////////////////////////////////////////////////////////////////////////
1389
1390SkBitmap::RLEPixels::RLEPixels(int width, int height) {
1391 fHeight = height;
commit-bot@chromium.org235002f2013-10-09 18:39:59 +00001392 fYPtrs = (uint8_t**)sk_calloc_throw(height * sizeof(uint8_t*));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001393}
1394
1395SkBitmap::RLEPixels::~RLEPixels() {
1396 sk_free(fYPtrs);
1397}
1398
1399///////////////////////////////////////////////////////////////////////////////
1400
1401#ifdef SK_DEBUG
1402void SkBitmap::validate() const {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001403 fInfo.validate();
commit-bot@chromium.orgd5414e52014-02-13 22:30:38 +00001404
1405 // ImageInfo may not require this, but Bitmap ensures that opaque-only
1406 // colorTypes report opaque for their alphatype
1407 if (kRGB_565_SkColorType == fInfo.colorType()) {
1408 SkASSERT(kOpaque_SkAlphaType == fInfo.alphaType());
1409 }
1410
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001411 SkASSERT(fInfo.validRowBytes(fRowBytes));
scroggo@google.com8e990eb2013-06-14 15:55:56 +00001412 uint8_t allFlags = kImageIsOpaque_Flag | kImageIsVolatile_Flag | kImageIsImmutable_Flag;
1413#ifdef SK_BUILD_FOR_ANDROID
1414 allFlags |= kHasHardwareMipMap_Flag;
1415#endif
1416 SkASSERT(fFlags <= allFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001417 SkASSERT(fPixelLockCount >= 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001418
reed@google.com615316c2014-01-15 19:15:23 +00001419 if (fPixels) {
1420 SkASSERT(fPixelRef);
1421 SkASSERT(fPixelLockCount > 0);
1422 SkASSERT(fPixelRef->isLocked());
1423 SkASSERT(fPixelRef->rowBytes() == fRowBytes);
1424 SkASSERT(fPixelRefOrigin.fX >= 0);
1425 SkASSERT(fPixelRefOrigin.fY >= 0);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001426 SkASSERT(fPixelRef->info().width() >= (int)this->width() + fPixelRefOrigin.fX);
1427 SkASSERT(fPixelRef->info().fHeight >= (int)this->height() + fPixelRefOrigin.fY);
1428 SkASSERT(fPixelRef->rowBytes() >= fInfo.minRowBytes());
reed@google.com615316c2014-01-15 19:15:23 +00001429 } else {
1430 SkASSERT(NULL == fColorTable);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001431 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001432}
1433#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001434
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001435#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001436void SkBitmap::toString(SkString* str) const {
1437
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001438 static const char* gColorTypeNames[kLastEnum_SkColorType + 1] = {
1439 "UNKNOWN", "A8", "565", "4444", "RGBA", "BGRA", "INDEX8",
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001440 };
1441
1442 str->appendf("bitmap: ((%d, %d) %s", this->width(), this->height(),
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +00001443 gColorTypeNames[this->colorType()]);
robertphillips@google.com76f9e932013-01-15 20:17:47 +00001444
1445 str->append(" (");
1446 if (this->isOpaque()) {
1447 str->append("opaque");
1448 } else {
1449 str->append("transparent");
1450 }
1451 if (this->isImmutable()) {
1452 str->append(", immutable");
1453 } else {
1454 str->append(", not-immutable");
1455 }
1456 str->append(")");
1457
1458 SkPixelRef* pr = this->pixelRef();
1459 if (NULL == pr) {
1460 // show null or the explicit pixel address (rare)
1461 str->appendf(" pixels:%p", this->getPixels());
1462 } else {
1463 const char* uri = pr->getURI();
1464 if (NULL != uri) {
1465 str->appendf(" uri:\"%s\"", uri);
1466 } else {
1467 str->appendf(" pixelref:%p", pr);
1468 }
1469 }
1470
1471 str->append(")");
1472}
1473#endif
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +00001474
1475///////////////////////////////////////////////////////////////////////////////
1476
1477#ifdef SK_DEBUG
1478void SkImageInfo::validate() const {
1479 SkASSERT(fWidth >= 0);
1480 SkASSERT(fHeight >= 0);
1481 SkASSERT(SkColorTypeIsValid(fColorType));
1482 SkASSERT(SkAlphaTypeIsValid(fAlphaType));
1483}
1484#endif