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 1240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1251 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop); | 1251 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop); |
1252 } | 1252 } |
1253 SkDEBUGCODE(tmpBitmap.validate();) | 1253 SkDEBUGCODE(tmpBitmap.validate();) |
1254 | 1254 |
1255 tmpBitmap.swap(*dst); | 1255 tmpBitmap.swap(*dst); |
1256 return true; | 1256 return true; |
1257 } | 1257 } |
1258 | 1258 |
1259 /////////////////////////////////////////////////////////////////////////////// | 1259 /////////////////////////////////////////////////////////////////////////////// |
1260 | 1260 |
1261 void SkBitmap::WriteRawPixels(SkWriteBuffer* buffer, const SkBitmap& bitmap) { | |
1262 const SkImageInfo info = bitmap.info(); | |
1263 SkAutoLockPixels alp(bitmap); | |
1264 if (0 == info.width() || 0 == info.height() || NULL == bitmap.getPixels()) { | |
1265 buffer->writeUInt(0); // instead of snugRB, signaling no pixels | |
1266 return; | |
1267 } | |
1268 | |
1269 const size_t snugRB = info.width() * info.bytesPerPixel(); | |
1270 const char* srcP = (const char*)bitmap.getPixels(); | |
1271 const size_t ramRB = bitmap.rowBytes(); | |
1272 | |
1273 buffer->write32(SkToU32(snugRB)); | |
1274 info.flatten(*buffer); | |
1275 | |
1276 const size_t size = snugRB * info.height(); | |
1277 SkAutoMalloc storage(size); | |
1278 char* srcBytes = (char*)storage.get(); | |
scroggo
2014/05/21 14:48:01
srcBytes is confusing here. How about dstBytes?
reed1
2014/05/21 20:33:04
Done.
| |
1279 for (int y = 0; y < info.height(); ++y) { | |
1280 memcpy(srcBytes, srcP, snugRB); | |
1281 srcBytes += snugRB; | |
1282 srcP += ramRB; | |
1283 } | |
1284 buffer->writeByteArray(storage.get(), size); | |
1285 | |
1286 SkColorTable* ct = bitmap.getColorTable(); | |
1287 if (kIndex_8_SkColorType == info.colorType() && ct) { | |
1288 buffer->writeBool(true); | |
1289 ct->writeToBuffer(*buffer); | |
1290 } else { | |
1291 buffer->writeBool(false); | |
1292 } | |
1293 } | |
1294 | |
1295 SkPixelRef* SkBitmap::ReadRawPixels(SkReadBuffer* buffer) { | |
hal.canary
2014/05/21 15:18:11
void SkBitmap::WriteRawPixels(SkWriteBuffer* buffe
reed1
2014/05/21 20:33:04
Done.
| |
1296 const size_t snugRB = buffer->readUInt(); | |
1297 if (0 == snugRB) { // no pixels | |
1298 return NULL; | |
1299 } | |
1300 | |
1301 SkImageInfo info; | |
1302 info.unflatten(*buffer); | |
1303 | |
1304 const size_t ramRB = info.minRowBytes(); | |
hal.canary
2014/05/21 15:20:29
When is ramRB != snugRB?
reed1
2014/05/21 20:33:04
snugRB is the smallest that they *could* be (used
hal.canary
2014/05/21 20:39:49
But you set `ramRB = info.minRowBytes()`, which is
| |
1305 const int height = info.height(); | |
1306 const size_t snugSize = snugRB * height; | |
1307 const size_t ramSize = ramRB * height; | |
1308 SkASSERT(snugSize <= ramSize); | |
1309 | |
1310 char* srcBytes = (char*)sk_malloc_throw(ramSize); | |
scroggo
2014/05/21 14:48:01
Again, I find srcBytes confusing here. dstBytes? r
reed1
2014/05/21 20:33:04
Done.
| |
1311 buffer->readByteArray(srcBytes, snugSize); | |
1312 SkAutoDataUnref data(SkData::NewFromMalloc(srcBytes, ramSize)); | |
1313 | |
1314 if (snugSize != ramSize) { | |
1315 const char* srcRow = srcBytes + snugRB * (height - 1); | |
1316 char* dstRow = srcBytes + ramRB * (height - 1); | |
1317 for (int y = height - 1; y >= 1; --y) { | |
1318 memmove(dstRow, srcRow, snugRB); | |
1319 srcRow -= snugRB; | |
1320 dstRow -= ramRB; | |
1321 } | |
1322 SkASSERT(srcRow == dstRow); // first row does not need to be moved | |
1323 } | |
1324 | |
1325 SkAutoTUnref<SkColorTable> ctable; | |
1326 if (buffer->readBool()) { | |
1327 ctable.reset(SkNEW_ARGS(SkColorTable, (*buffer))); | |
1328 } | |
1329 | |
1330 return SkMallocPixelRef::NewWithData(info, info.minRowBytes(), ctable.get(), data.get()); | |
1331 } | |
1332 | |
1261 enum { | 1333 enum { |
1262 SERIALIZE_PIXELTYPE_NONE, | 1334 SERIALIZE_PIXELTYPE_NONE, |
1263 SERIALIZE_PIXELTYPE_REF_DATA | 1335 SERIALIZE_PIXELTYPE_REF_DATA |
1264 }; | 1336 }; |
1265 | 1337 |
1338 #ifdef SK_SUPPORT_LEGACY_BITMAPFLATTEN | |
1266 void SkBitmap::flatten(SkWriteBuffer& buffer) const { | 1339 void SkBitmap::flatten(SkWriteBuffer& buffer) const { |
1267 fInfo.flatten(buffer); | 1340 fInfo.flatten(buffer); |
1268 buffer.writeInt(fRowBytes); | 1341 buffer.writeInt(fRowBytes); |
1269 | 1342 |
1270 if (fPixelRef) { | 1343 if (fPixelRef) { |
1271 if (fPixelRef->getFactory()) { | 1344 if (fPixelRef->getFactory()) { |
1272 buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA); | 1345 buffer.writeInt(SERIALIZE_PIXELTYPE_REF_DATA); |
1273 buffer.writeInt(fPixelRefOrigin.fX); | 1346 buffer.writeInt(fPixelRefOrigin.fX); |
1274 buffer.writeInt(fPixelRefOrigin.fY); | 1347 buffer.writeInt(fPixelRefOrigin.fY); |
1275 buffer.writeFlattenable(fPixelRef); | 1348 buffer.writeFlattenable(fPixelRef); |
1276 return; | 1349 return; |
1277 } | 1350 } |
1278 // if we get here, we can't record the pixels | 1351 // if we get here, we can't record the pixels |
1279 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE); | 1352 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE); |
1280 } else { | 1353 } else { |
1281 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE); | 1354 buffer.writeInt(SERIALIZE_PIXELTYPE_NONE); |
1282 } | 1355 } |
1283 } | 1356 } |
1357 #endif | |
1284 | 1358 |
1285 void SkBitmap::unflatten(SkReadBuffer& buffer) { | 1359 void SkBitmap::unflatten(SkReadBuffer& buffer) { |
1286 this->reset(); | 1360 this->reset(); |
1287 | 1361 |
1288 SkImageInfo info; | 1362 SkImageInfo info; |
1289 info.unflatten(buffer); | 1363 info.unflatten(buffer); |
1290 size_t rowBytes = buffer.readInt(); | 1364 size_t rowBytes = buffer.readInt(); |
1291 if (!buffer.validate((info.width() >= 0) && (info.height() >= 0) && | 1365 if (!buffer.validate((info.width() >= 0) && (info.height() >= 0) && |
1292 SkColorTypeIsValid(info.fColorType) && | 1366 SkColorTypeIsValid(info.fColorType) && |
1293 SkAlphaTypeIsValid(info.fAlphaType) && | 1367 SkAlphaTypeIsValid(info.fAlphaType) && |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1415 /////////////////////////////////////////////////////////////////////////////// | 1489 /////////////////////////////////////////////////////////////////////////////// |
1416 | 1490 |
1417 #ifdef SK_DEBUG | 1491 #ifdef SK_DEBUG |
1418 void SkImageInfo::validate() const { | 1492 void SkImageInfo::validate() const { |
1419 SkASSERT(fWidth >= 0); | 1493 SkASSERT(fWidth >= 0); |
1420 SkASSERT(fHeight >= 0); | 1494 SkASSERT(fHeight >= 0); |
1421 SkASSERT(SkColorTypeIsValid(fColorType)); | 1495 SkASSERT(SkColorTypeIsValid(fColorType)); |
1422 SkASSERT(SkAlphaTypeIsValid(fAlphaType)); | 1496 SkASSERT(SkAlphaTypeIsValid(fAlphaType)); |
1423 } | 1497 } |
1424 #endif | 1498 #endif |
OLD | NEW |