OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2008 The Android Open Source Project | 3 * Copyright 2008 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
(...skipping 1248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1259 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop); | 1259 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop); |
1260 } | 1260 } |
1261 SkDEBUGCODE(tmpBitmap.validate();) | 1261 SkDEBUGCODE(tmpBitmap.validate();) |
1262 | 1262 |
1263 tmpBitmap.swap(*dst); | 1263 tmpBitmap.swap(*dst); |
1264 return true; | 1264 return true; |
1265 } | 1265 } |
1266 | 1266 |
1267 /////////////////////////////////////////////////////////////////////////////// | 1267 /////////////////////////////////////////////////////////////////////////////// |
1268 | 1268 |
| 1269 void SkBitmap::WriteRawPixels(SkWriteBuffer* buffer, const SkBitmap& bitmap) { |
| 1270 const SkImageInfo info = bitmap.info(); |
| 1271 SkAutoLockPixels alp(bitmap); |
| 1272 if (0 == info.width() || 0 == info.height() || NULL == bitmap.getPixels()) { |
| 1273 buffer->writeUInt(0); // instead of snugRB, signaling no pixels |
| 1274 return; |
| 1275 } |
| 1276 |
| 1277 const size_t snugRB = info.width() * info.bytesPerPixel(); |
| 1278 const char* src = (const char*)bitmap.getPixels(); |
| 1279 const size_t ramRB = bitmap.rowBytes(); |
| 1280 |
| 1281 buffer->write32(SkToU32(snugRB)); |
| 1282 info.flatten(*buffer); |
| 1283 |
| 1284 const size_t size = snugRB * info.height(); |
| 1285 SkAutoMalloc storage(size); |
| 1286 char* dst = (char*)storage.get(); |
| 1287 for (int y = 0; y < info.height(); ++y) { |
| 1288 memcpy(dst, src, snugRB); |
| 1289 dst += snugRB; |
| 1290 src += ramRB; |
| 1291 } |
| 1292 buffer->writeByteArray(storage.get(), size); |
| 1293 |
| 1294 SkColorTable* ct = bitmap.getColorTable(); |
| 1295 if (kIndex_8_SkColorType == info.colorType() && ct) { |
| 1296 buffer->writeBool(true); |
| 1297 ct->writeToBuffer(*buffer); |
| 1298 } else { |
| 1299 buffer->writeBool(false); |
| 1300 } |
| 1301 } |
| 1302 |
| 1303 bool SkBitmap::ReadRawPixels(SkReadBuffer* buffer, SkBitmap* bitmap) { |
| 1304 const size_t snugRB = buffer->readUInt(); |
| 1305 if (0 == snugRB) { // no pixels |
| 1306 return false; |
| 1307 } |
| 1308 |
| 1309 SkImageInfo info; |
| 1310 info.unflatten(*buffer); |
| 1311 |
| 1312 const size_t ramRB = info.minRowBytes(); |
| 1313 const int height = info.height(); |
| 1314 const size_t snugSize = snugRB * height; |
| 1315 const size_t ramSize = ramRB * height; |
| 1316 SkASSERT(snugSize <= ramSize); |
| 1317 |
| 1318 char* dst = (char*)sk_malloc_throw(ramSize); |
| 1319 buffer->readByteArray(dst, snugSize); |
| 1320 SkAutoDataUnref data(SkData::NewFromMalloc(dst, ramSize)); |
| 1321 |
| 1322 if (snugSize != ramSize) { |
| 1323 const char* srcRow = dst + snugRB * (height - 1); |
| 1324 char* dstRow = dst + ramRB * (height - 1); |
| 1325 for (int y = height - 1; y >= 1; --y) { |
| 1326 memmove(dstRow, srcRow, snugRB); |
| 1327 srcRow -= snugRB; |
| 1328 dstRow -= ramRB; |
| 1329 } |
| 1330 SkASSERT(srcRow == dstRow); // first row does not need to be moved |
| 1331 } |
| 1332 |
| 1333 SkAutoTUnref<SkColorTable> ctable; |
| 1334 if (buffer->readBool()) { |
| 1335 ctable.reset(SkNEW_ARGS(SkColorTable, (*buffer))); |
| 1336 } |
| 1337 |
| 1338 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewWithData(info, info.minRowB
ytes(), |
| 1339 ctable.get(), data
.get())); |
| 1340 bitmap->setConfig(pr->info()); |
| 1341 bitmap->setPixelRef(pr, 0, 0); |
| 1342 return true; |
| 1343 } |
| 1344 |
1269 enum { | 1345 enum { |
1270 SERIALIZE_PIXELTYPE_NONE, | 1346 SERIALIZE_PIXELTYPE_NONE, |
1271 SERIALIZE_PIXELTYPE_REF_DATA | 1347 SERIALIZE_PIXELTYPE_REF_DATA |
1272 }; | 1348 }; |
1273 | 1349 |
| 1350 #ifdef SK_SUPPORT_LEGACY_BITMAPFLATTEN |
1274 void SkBitmap::flatten(SkWriteBuffer& buffer) const { | 1351 void SkBitmap::flatten(SkWriteBuffer& buffer) const { |
1275 fInfo.flatten(buffer); | 1352 fInfo.flatten(buffer); |
1276 buffer.writeInt(fRowBytes); | 1353 buffer.writeInt(fRowBytes); |
1277 | 1354 |
1278 if (fPixelRef) { | 1355 if (fPixelRef) { |
1279 if (fPixelRef->getFactory()) { | 1356 if (fPixelRef->getFactory()) { |
1280 buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA); | 1357 buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA); |
1281 buffer.writeInt(fPixelRefOrigin.fX); | 1358 buffer.writeInt(fPixelRefOrigin.fX); |
1282 buffer.writeInt(fPixelRefOrigin.fY); | 1359 buffer.writeInt(fPixelRefOrigin.fY); |
1283 buffer.writeFlattenable(fPixelRef); | 1360 buffer.writeFlattenable(fPixelRef); |
1284 return; | 1361 return; |
1285 } | 1362 } |
1286 // if we get here, we can't record the pixels | 1363 // if we get here, we can't record the pixels |
1287 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE); | 1364 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE); |
1288 } else { | 1365 } else { |
1289 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE); | 1366 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE); |
1290 } | 1367 } |
1291 } | 1368 } |
| 1369 #endif |
1292 | 1370 |
1293 void SkBitmap::unflatten(SkReadBuffer& buffer) { | 1371 void SkBitmap::unflatten(SkReadBuffer& buffer) { |
1294 this->reset(); | 1372 this->reset(); |
1295 | 1373 |
1296 SkImageInfo info; | 1374 SkImageInfo info; |
1297 info.unflatten(buffer); | 1375 info.unflatten(buffer); |
1298 size_t rowBytes = buffer.readInt(); | 1376 size_t rowBytes = buffer.readInt(); |
1299 if (!buffer.validate((info.width() >= 0) && (info.height() >= 0) && | 1377 if (!buffer.validate((info.width() >= 0) && (info.height() >= 0) && |
1300 SkColorTypeIsValid(info.fColorType) && | 1378 SkColorTypeIsValid(info.fColorType) && |
1301 SkAlphaTypeIsValid(info.fAlphaType) && | 1379 SkAlphaTypeIsValid(info.fAlphaType) && |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1423 /////////////////////////////////////////////////////////////////////////////// | 1501 /////////////////////////////////////////////////////////////////////////////// |
1424 | 1502 |
1425 #ifdef SK_DEBUG | 1503 #ifdef SK_DEBUG |
1426 void SkImageInfo::validate() const { | 1504 void SkImageInfo::validate() const { |
1427 SkASSERT(fWidth >= 0); | 1505 SkASSERT(fWidth >= 0); |
1428 SkASSERT(fHeight >= 0); | 1506 SkASSERT(fHeight >= 0); |
1429 SkASSERT(SkColorTypeIsValid(fColorType)); | 1507 SkASSERT(SkColorTypeIsValid(fColorType)); |
1430 SkASSERT(SkAlphaTypeIsValid(fAlphaType)); | 1508 SkASSERT(SkAlphaTypeIsValid(fAlphaType)); |
1431 } | 1509 } |
1432 #endif | 1510 #endif |
OLD | NEW |