diff options
author | 2015-06-05 14:33:17 -0700 | |
---|---|---|
committer | 2015-06-05 14:33:17 -0700 | |
commit | 183b57f3093ccca79a171bd2e8d3ec58adadac5d (patch) | |
tree | 424f19699e3b95fc1452350d0ab962635f70edd4 /src | |
parent | 6e764859dabf265fae03066376e7446a87e0ad09 (diff) |
add extractSubset and SkAutoPixmapStorage
extracted from larger CL in progress: https://codereview.chromium.org/1148793007
BUG=skia:
TBR=
Review URL: https://codereview.chromium.org/1162013008
Diffstat (limited to 'src')
-rw-r--r-- | src/core/SkPixmap.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/core/SkPixmap.cpp b/src/core/SkPixmap.cpp index 3a0fad2a39..9b7f993792 100644 --- a/src/core/SkPixmap.cpp +++ b/src/core/SkPixmap.cpp @@ -6,6 +6,7 @@ */ #include "SkConfig8888.h" +#include "SkMask.h" #include "SkPixmap.h" void SkAutoPixmapUnlock::reset(const SkPixmap& pm, void (*unlock)(void*), void* ctx) { @@ -37,6 +38,37 @@ void SkPixmap::reset(const SkImageInfo& info, const void* addr, size_t rowBytes, fInfo = info; } +bool SkPixmap::reset(const SkMask& src) { + if (SkMask::kA8_Format == src.fFormat) { + this->reset(SkImageInfo::MakeA8(src.fBounds.width(), src.fBounds.height()), + src.fImage, src.fRowBytes, NULL); + return true; + } + this->reset(); + return false; +} + +bool SkPixmap::extractSubset(SkPixmap* result, const SkIRect& subset) const { + SkIRect srcRect, r; + srcRect.set(0, 0, this->width(), this->height()); + if (!r.intersect(srcRect, subset)) { + return false; // r is empty (i.e. no intersection) + } + + // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have + // exited above. + SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width())); + SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height())); + + const void* pixels = NULL; + if (fPixels) { + const size_t bpp = fInfo.bytesPerPixel(); + pixels = (const uint8_t*)fPixels + r.fTop * fRowBytes + r.fLeft * bpp; + } + result->reset(fInfo.makeWH(r.width(), r.height()), pixels, fRowBytes, fCTable); + return true; +} + bool SkPixmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels, size_t dstRB, int x, int y) const { if (kUnknown_SkColorType == requestedDstInfo.colorType()) { @@ -72,3 +104,36 @@ bool SkPixmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels, return SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRB, srcInfo, srcPixels, this->rowBytes(), this->ctable()); } + +////////////////////////////////////////////////////////////////////////////////////////////////// + +SkAutoPixmapStorage::SkAutoPixmapStorage() : fStorage(NULL) {} + +SkAutoPixmapStorage::~SkAutoPixmapStorage() { + sk_free(fStorage); +} + +bool SkAutoPixmapStorage::tryAlloc(const SkImageInfo& info) { + if (fStorage) { + sk_free(fStorage); + fStorage = NULL; + } + + size_t rb = info.minRowBytes(); + size_t size = info.getSafeSize(rb); + if (0 == size) { + return false; + } + fStorage = sk_malloc_flags(size, 0); + if (NULL == fStorage) { + return false; + } + this->reset(info, fStorage, rb); + return true; +} + +void SkAutoPixmapStorage::alloc(const SkImageInfo& info) { + if (!this->tryAlloc(info)) { + sk_throw(); + } +} |