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

Unified Diff: src/core/SkConfig8888.cpp

Issue 390693002: Add SkBitmap::readPixels() and reimplement copyTo and SkCanvas::readPixels (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 5 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 | « src/core/SkConfig8888.h ('k') | tests/BitmapCopyTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/core/SkConfig8888.cpp
diff --git a/src/core/SkConfig8888.cpp b/src/core/SkConfig8888.cpp
index 189309dae6e97b51c6d410a222f4880c2a831365..b0572c0544c6a1eb15b4f01ee21c69813e6fceba 100644
--- a/src/core/SkConfig8888.cpp
+++ b/src/core/SkConfig8888.cpp
@@ -1,5 +1,15 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkBitmap.h"
+#include "SkCanvas.h"
#include "SkConfig8888.h"
#include "SkColorPriv.h"
+#include "SkDither.h"
#include "SkMathPriv.h"
#include "SkUnPreMultiply.h"
@@ -115,3 +125,135 @@ bool SkSrcPixelInfo::convertPixelsTo(SkDstPixelInfo* dst, int width, int height)
}
return true;
}
+
+static void rect_memcpy(void* dst, size_t dstRB, const void* src, size_t srcRB, size_t bytesPerRow,
+ int rowCount) {
+ SkASSERT(bytesPerRow <= srcRB);
+ SkASSERT(bytesPerRow <= dstRB);
+ for (int i = 0; i < rowCount; ++i) {
+ memcpy(dst, src, bytesPerRow);
+ dst = (char*)dst + dstRB;
+ src = (const char*)src + srcRB;
+ }
+}
+
+bool SkPixelInfo::CopyPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
+ const SkImageInfo& srcInfo, const void* srcPixels, size_t srcRB,
+ SkColorTable* ctable) {
+ if (srcInfo.dimensions() != dstInfo.dimensions()) {
+ return false;
+ }
+
+ const int width = srcInfo.width();
+ const int height = srcInfo.height();
+
+ // Handle fancy alpha swizzling if both are ARGB32
+ if (4 == srcInfo.bytesPerPixel() && 4 == dstInfo.bytesPerPixel()) {
+ SkDstPixelInfo dstPI;
+ dstPI.fColorType = dstInfo.colorType();
+ dstPI.fAlphaType = dstInfo.alphaType();
+ dstPI.fPixels = dstPixels;
+ dstPI.fRowBytes = dstRB;
+
+ SkSrcPixelInfo srcPI;
+ srcPI.fColorType = srcInfo.colorType();
+ srcPI.fAlphaType = srcInfo.alphaType();
+ srcPI.fPixels = srcPixels;
+ srcPI.fRowBytes = srcRB;
+
+ return srcPI.convertPixelsTo(&dstPI, width, height);
+ }
+
+ // If they agree on colorType and the alphaTypes are compatible, then we just memcpy.
+ // Note: we've already taken care of 32bit colortypes above.
+ if (srcInfo.colorType() == dstInfo.colorType()) {
+ switch (srcInfo.colorType()) {
+ case kRGB_565_SkColorType:
+ case kAlpha_8_SkColorType:
+ break;
+ case kIndex_8_SkColorType:
+ case kARGB_4444_SkColorType:
+ if (srcInfo.alphaType() != dstInfo.alphaType()) {
+ return false;
+ }
+ break;
+ default:
+ return false;
+ }
+ rect_memcpy(dstPixels, dstRB, srcPixels, srcRB, width * srcInfo.bytesPerPixel(), height);
+ return true;
+ }
+
+ /*
+ * Begin section where we try to change colorTypes along the way. Not all combinations
+ * are supported.
+ */
+
+ // Can no longer draw directly into 4444, but we can manually whack it for a few combinations
+ if (kARGB_4444_SkColorType == dstInfo.colorType() &&
+ (kN32_SkColorType == srcInfo.colorType() || kIndex_8_SkColorType == srcInfo.colorType())) {
+ if (srcInfo.alphaType() == kUnpremul_SkAlphaType) {
+ // Our method for converting to 4444 assumes premultiplied.
+ return false;
+ }
+
+ const SkPMColor* table = NULL;
+ if (kIndex_8_SkColorType == srcInfo.colorType()) {
+ if (NULL == ctable) {
+ return false;
+ }
+ table = ctable->lockColors();
+ }
+
+ for (int y = 0; y < height; ++y) {
+ DITHER_4444_SCAN(y);
+ SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*)dstPixels;
+ if (table) {
+ const uint8_t* SK_RESTRICT srcRow = (const uint8_t*)srcPixels;
+ for (int x = 0; x < width; ++x) {
+ dstRow[x] = SkDitherARGB32To4444(table[srcRow[x]], DITHER_VALUE(x));
+ }
+ } else {
+ const SkPMColor* SK_RESTRICT srcRow = (const SkPMColor*)srcPixels;
+ for (int x = 0; x < width; ++x) {
+ dstRow[x] = SkDitherARGB32To4444(srcRow[x], DITHER_VALUE(x));
+ }
+ }
+ dstPixels = (char*)dstPixels + dstRB;
+ srcPixels = (const char*)srcPixels + srcRB;
+ }
+
+ if (table) {
+ ctable->unlockColors();
+ }
+ return true;
+ }
+
+ if (dstInfo.alphaType() == kUnpremul_SkAlphaType) {
+ // We do not support drawing to unpremultiplied bitmaps.
+ return false;
+ }
+
+ // Final fall-back, draw with a canvas
+ //
+ // Always clear the dest in case one of the blitters accesses it
+ // TODO: switch the allocation of tmpDst to call sk_calloc_throw
+ {
+ SkBitmap bm;
+ if (!bm.installPixels(srcInfo, const_cast<void*>(srcPixels), srcRB, ctable, NULL, NULL)) {
+ return false;
+ }
+ SkAutoTUnref<SkCanvas> canvas(SkCanvas::NewRasterDirect(dstInfo, dstPixels, dstRB));
+ if (NULL == canvas.get()) {
+ return false;
+ }
+
+ SkPaint paint;
+ paint.setDither(true);
+
+ canvas->clear(0);
+ canvas->drawBitmap(bm, 0, 0, &paint);
+ return true;
+ }
+}
+
« no previous file with comments | « src/core/SkConfig8888.h ('k') | tests/BitmapCopyTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698