aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/SkBitmap.cpp127
-rw-r--r--src/core/SkBitmapDevice.cpp76
-rw-r--r--src/core/SkConfig8888.cpp142
-rw-r--r--src/core/SkConfig8888.h4
4 files changed, 219 insertions, 130 deletions
diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp
index 789bf11b4..42254b518 100644
--- a/src/core/SkBitmap.cpp
+++ b/src/core/SkBitmap.cpp
@@ -873,11 +873,13 @@ bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const {
#include "SkPaint.h"
bool SkBitmap::canCopyTo(SkColorType dstColorType) const {
- if (this->colorType() == kUnknown_SkColorType) {
+ const SkColorType srcCT = this->colorType();
+
+ if (srcCT == kUnknown_SkColorType) {
return false;
}
- bool sameConfigs = (this->colorType() == dstColorType);
+ bool sameConfigs = (srcCT == dstColorType);
switch (dstColorType) {
case kAlpha_8_SkColorType:
case kRGB_565_SkColorType:
@@ -890,15 +892,66 @@ bool SkBitmap::canCopyTo(SkColorType dstColorType) const {
}
break;
case kARGB_4444_SkColorType:
- return sameConfigs || kN32_SkColorType == this->colorType();
+ return sameConfigs || kN32_SkColorType == srcCT || kIndex_8_SkColorType == srcCT;
default:
return false;
}
return true;
}
-bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType,
- Allocator* alloc) const {
+#include "SkConfig8888.h"
+
+bool SkBitmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels, size_t dstRB,
+ int x, int y) const {
+ if (kUnknown_SkColorType == requestedDstInfo.colorType()) {
+ return false;
+ }
+ if (NULL == dstPixels || dstRB < requestedDstInfo.minRowBytes()) {
+ return false;
+ }
+ if (0 == requestedDstInfo.width() || 0 == requestedDstInfo.height()) {
+ return false;
+ }
+
+ SkIRect srcR = SkIRect::MakeXYWH(x, y, requestedDstInfo.width(), requestedDstInfo.height());
+ if (!srcR.intersect(0, 0, this->width(), this->height())) {
+ return false;
+ }
+
+ SkImageInfo dstInfo = requestedDstInfo;
+ // the intersect may have shrunk info's logical size
+ dstInfo.fWidth = srcR.width();
+ dstInfo.fHeight = srcR.height();
+
+ // if x or y are negative, then we have to adjust pixels
+ if (x > 0) {
+ x = 0;
+ }
+ if (y > 0) {
+ y = 0;
+ }
+ // here x,y are either 0 or negative
+ dstPixels = ((char*)dstPixels - y * dstRB - x * dstInfo.bytesPerPixel());
+
+ //////////////
+
+ SkAutoLockPixels alp(*this);
+
+ // since we don't stop creating un-pixeled devices yet, check for no pixels here
+ if (NULL == this->getPixels()) {
+ return false;
+ }
+
+ SkImageInfo srcInfo = this->info();
+ srcInfo.fWidth = dstInfo.width();
+ srcInfo.fHeight = dstInfo.height();
+
+ const void* srcPixels = this->getAddr(srcR.x(), srcR.y());
+ return SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRB, srcInfo, srcPixels, this->rowBytes(),
+ this->getColorTable());
+}
+
+bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType, Allocator* alloc) const {
if (!this->canCopyTo(dstColorType)) {
return false;
}
@@ -971,59 +1024,21 @@ bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType,
// returned false.
SkASSERT(tmpDst.pixelRef() != NULL);
- /* do memcpy for the same configs cases, else use drawing
- */
- if (src->colorType() == dstColorType) {
- if (tmpDst.getSize() == src->getSize()) {
- memcpy(tmpDst.getPixels(), src->getPixels(), src->getSafeSize());
+ if (!src->readPixels(tmpDst.info(), tmpDst.getPixels(), tmpDst.rowBytes(), 0, 0)) {
+ return false;
+ }
- SkPixelRef* dstPixelRef = tmpDst.pixelRef();
- if (dstPixelRef->info() == fPixelRef->info()) {
- dstPixelRef->cloneGenID(*fPixelRef);
- }
- } else {
- const char* srcP = reinterpret_cast<const char*>(src->getPixels());
- char* dstP = reinterpret_cast<char*>(tmpDst.getPixels());
- // to be sure we don't read too much, only copy our logical pixels
- size_t bytesToCopy = tmpDst.width() * tmpDst.bytesPerPixel();
- for (int y = 0; y < tmpDst.height(); y++) {
- memcpy(dstP, srcP, bytesToCopy);
- srcP += src->rowBytes();
- dstP += tmpDst.rowBytes();
- }
- }
- } else if (kARGB_4444_SkColorType == dstColorType
- && kN32_SkColorType == src->colorType()) {
- if (src->alphaType() == kUnpremul_SkAlphaType) {
- // Our method for converting to 4444 assumes premultiplied.
- return false;
+ // (for BitmapHeap) Clone the pixelref genID even though we have a new pixelref.
+ // The old copyTo impl did this, so we continue it for now.
+ //
+ // TODO: should we ignore rowbytes (i.e. getSize)? Then it could just be
+ // if (src_pixelref->info == dst_pixelref->info)
+ //
+ if (src->colorType() == dstColorType && tmpDst.getSize() == src->getSize()) {
+ SkPixelRef* dstPixelRef = tmpDst.pixelRef();
+ if (dstPixelRef->info() == fPixelRef->info()) {
+ dstPixelRef->cloneGenID(*fPixelRef);
}
- SkASSERT(src->height() == tmpDst.height());
- SkASSERT(src->width() == tmpDst.width());
- for (int y = 0; y < src->height(); ++y) {
- SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*) tmpDst.getAddr16(0, y);
- SkPMColor* SK_RESTRICT srcRow = (SkPMColor*) src->getAddr32(0, y);
- DITHER_4444_SCAN(y);
- for (int x = 0; x < src->width(); ++x) {
- dstRow[x] = SkDitherARGB32To4444(srcRow[x],
- DITHER_VALUE(x));
- }
- }
- } else {
- if (tmpDst.alphaType() == kUnpremul_SkAlphaType) {
- // We do not support drawing to unpremultiplied bitmaps.
- return false;
- }
-
- // Always clear the dest in case one of the blitters accesses it
- // TODO: switch the allocation of tmpDst to call sk_calloc_throw
- tmpDst.eraseColor(SK_ColorTRANSPARENT);
-
- SkCanvas canvas(tmpDst);
- SkPaint paint;
-
- paint.setDither(true);
- canvas.drawBitmap(*src, 0, 0, &paint);
}
dst->swap(tmpDst);
diff --git a/src/core/SkBitmapDevice.cpp b/src/core/SkBitmapDevice.cpp
index 0f3cc2be5..1dfc3a6c0 100644
--- a/src/core/SkBitmapDevice.cpp
+++ b/src/core/SkBitmapDevice.cpp
@@ -155,59 +155,8 @@ void* SkBitmapDevice::onAccessPixels(SkImageInfo* info, size_t* rowBytes) {
return NULL;
}
-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;
- }
-}
-
#include "SkConfig8888.h"
-static bool copy_pixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
- const SkImageInfo& srcInfo, const void* srcPixels, size_t srcRowBytes) {
- if (srcInfo.dimensions() != dstInfo.dimensions()) {
- return false;
- }
- if (4 == srcInfo.bytesPerPixel() && 4 == dstInfo.bytesPerPixel()) {
- SkDstPixelInfo dstPI;
- dstPI.fColorType = dstInfo.colorType();
- dstPI.fAlphaType = dstInfo.alphaType();
- dstPI.fPixels = dstPixels;
- dstPI.fRowBytes = dstRowBytes;
-
- SkSrcPixelInfo srcPI;
- srcPI.fColorType = srcInfo.colorType();
- srcPI.fAlphaType = srcInfo.alphaType();
- srcPI.fPixels = srcPixels;
- srcPI.fRowBytes = srcRowBytes;
-
- return srcPI.convertPixelsTo(&dstPI, srcInfo.width(), srcInfo.height());
- }
- if (srcInfo.colorType() == dstInfo.colorType()) {
- switch (srcInfo.colorType()) {
- case kRGB_565_SkColorType:
- case kAlpha_8_SkColorType:
- break;
- case kARGB_4444_SkColorType:
- if (srcInfo.alphaType() != dstInfo.alphaType()) {
- return false;
- }
- break;
- default:
- return false;
- }
- rect_memcpy(dstPixels, dstRowBytes, srcPixels, srcRowBytes,
- srcInfo.width() * srcInfo.bytesPerPixel(), srcInfo.height());
- }
- // TODO: add support for more conversions as needed
- return false;
-}
-
bool SkBitmapDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPixels,
size_t srcRowBytes, int x, int y) {
// since we don't stop creating un-pixeled devices yet, check for no pixels here
@@ -222,7 +171,7 @@ bool SkBitmapDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPi
void* dstPixels = fBitmap.getAddr(x, y);
size_t dstRowBytes = fBitmap.rowBytes();
- if (copy_pixels(dstInfo, dstPixels, dstRowBytes, srcInfo, srcPixels, srcRowBytes)) {
+ if (SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRowBytes, srcInfo, srcPixels, srcRowBytes)) {
fBitmap.notifyPixelsChanged();
return true;
}
@@ -231,28 +180,7 @@ bool SkBitmapDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPi
bool SkBitmapDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
int x, int y) {
- // since we don't stop creating un-pixeled devices yet, check for no pixels here
- if (NULL == fBitmap.getPixels()) {
- return false;
- }
-
- SkImageInfo srcInfo = fBitmap.info();
-
- // perhaps can relax these in the future
- if (4 != dstInfo.bytesPerPixel()) {
- return false;
- }
- if (4 != srcInfo.bytesPerPixel()) {
- return false;
- }
-
- srcInfo.fWidth = dstInfo.width();
- srcInfo.fHeight = dstInfo.height();
-
- const void* srcPixels = fBitmap.getAddr(x, y);
- const size_t srcRowBytes = fBitmap.rowBytes();
-
- return copy_pixels(dstInfo, dstPixels, dstRowBytes, srcInfo, srcPixels, srcRowBytes);
+ return fBitmap.readPixels(dstInfo, dstPixels, dstRowBytes, x, y);
}
///////////////////////////////////////////////////////////////////////////////
diff --git a/src/core/SkConfig8888.cpp b/src/core/SkConfig8888.cpp
index 189309dae..b0572c054 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;
+ }
+}
+
diff --git a/src/core/SkConfig8888.h b/src/core/SkConfig8888.h
index 97a3433ad..da0cbc668 100644
--- a/src/core/SkConfig8888.h
+++ b/src/core/SkConfig8888.h
@@ -14,6 +14,10 @@ struct SkPixelInfo {
SkColorType fColorType;
SkAlphaType fAlphaType;
size_t fRowBytes;
+
+ static bool CopyPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
+ const SkImageInfo& srcInfo, const void* srcPixels, size_t srcRowBytes,
+ SkColorTable* srcCTable = NULL);
};
struct SkDstPixelInfo : SkPixelInfo {