Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Unified Diff: src/image/SkImage.cpp

Issue 155763004: add ways to peer into an image to get its pixels (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « include/core/SkImage.h ('k') | src/image/SkImage_Base.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/image/SkImage.cpp
diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp
index 620922f64509ee15dd893de9c44e10d810f4b414..9b0a0b2cf8a3efcb87c312c0cae4ceef52849b5b 100644
--- a/src/image/SkImage.cpp
+++ b/src/image/SkImage.cpp
@@ -39,6 +39,41 @@ void SkImage::draw(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
as_IB(this)->onDrawRectToRect(canvas, src, dst, paint);
}
+const void* SkImage::peekPixels(SkImageInfo* info, size_t* rowBytes) const {
+ SkImageInfo infoStorage;
+ size_t rowBytesStorage;
+ if (NULL == info) {
+ info = &infoStorage;
+ }
+ if (NULL == rowBytes) {
+ rowBytes = &rowBytesStorage;
+ }
+ return as_IB(this)->onPeekPixels(info, rowBytes);
+}
+
+bool SkImage::readPixels(SkBitmap* bitmap, const SkIRect* subset) const {
+ if (NULL == bitmap) {
+ return false;
+ }
+
+ SkIRect bounds = SkIRect::MakeWH(this->width(), this->height());
+
+ // trim against the bitmap, if its already been allocated
+ if (bitmap->pixelRef()) {
+ bounds.fRight = SkMin32(bounds.fRight, bitmap->width());
+ bounds.fBottom = SkMin32(bounds.fBottom, bitmap->height());
+ if (bounds.isEmpty()) {
+ return false;
+ }
+ }
+
+ if (subset && !bounds.intersect(*subset)) {
+ // perhaps we could return true + empty-bitmap?
+ return false;
+ }
+ return as_IB(this)->onReadPixels(bitmap, bounds);
+}
+
GrTexture* SkImage::getTexture() {
return as_IB(this)->onGetTexture();
}
@@ -50,3 +85,55 @@ SkData* SkImage::encode(SkImageEncoder::Type type, int quality) const {
}
return NULL;
}
+
+///////////////////////////////////////////////////////////////////////////////
+
+static bool raster_canvas_supports(const SkImageInfo& info) {
+ switch (info.fColorType) {
+ case kPMColor_SkColorType:
+ return kUnpremul_SkAlphaType != info.fAlphaType;
+ case kRGB_565_SkColorType:
+ return true;
+ case kAlpha_8_SkColorType:
+ return true;
+ default:
+ break;
+ }
+ return false;
+}
+
+bool SkImage_Base::onReadPixels(SkBitmap* bitmap, const SkIRect& subset) const {
+ SkImageInfo info;
+
+ if (bitmap->pixelRef()) {
+ if (!bitmap->asImageInfo(&info)) {
+ return false;
+ }
+ if (!raster_canvas_supports(info)) {
+ return false;
+ }
+ } else {
+ SkImageInfo info = SkImageInfo::MakeN32Premul(subset.width(),
+ subset.height());
+ SkBitmap tmp;
+ if (!tmp.allocPixels(info)) {
+ return false;
+ }
+ *bitmap = tmp;
+ }
+
+ SkRect srcR, dstR;
+ srcR.set(subset);
+ dstR = srcR;
+ dstR.offset(-dstR.left(), -dstR.top());
+
+ SkCanvas canvas(*bitmap);
+
+ SkPaint paint;
+ paint.setXfermodeMode(SkXfermode::kClear_Mode);
+ canvas.drawRect(dstR, paint);
+
+ const_cast<SkImage_Base*>(this)->onDrawRectToRect(&canvas, &srcR, dstR, NULL);
+ return true;
+}
+
« no previous file with comments | « include/core/SkImage.h ('k') | src/image/SkImage_Base.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698