Index: src/utils/SkBitmapRegionCanvas.cpp |
diff --git a/src/utils/SkBitmapRegionCanvas.cpp b/src/utils/SkBitmapRegionCanvas.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..33e496e763e06b718d0aa6082834c6df6135c9df |
--- /dev/null |
+++ b/src/utils/SkBitmapRegionCanvas.cpp |
@@ -0,0 +1,122 @@ |
+/* |
+ * Copyright 2015 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include "SkBitmapRegionCanvas.h" |
+#include "SkCanvas.h" |
+#include "SkScanlineDecoder.h" |
+ |
+SkBitmapRegionCanvas::SkBitmapRegionCanvas(SkScanlineDecoder* decoder) |
+ : INHERITED(decoder->getInfo().width(), decoder->getInfo().height()) |
+ , fDecoder(decoder) |
+{} |
+ |
+/* |
+ * This has several key differences from the Android version: |
+ * Returns a Skia bitmap instead of an Android bitmap. |
+ * Android version attempts to reuse a recycled bitmap. |
+ */ |
+SkBitmap* SkBitmapRegionCanvas::decodeRegion(int input_x, int input_y, |
+ int input_w, int input_h, |
+ int sampleSize, |
+ SkColorType dstColorType) { |
+ // Reject color types not supported by this method |
+ if (kIndex_8_SkColorType == dstColorType || kGray_8_SkColorType == dstColorType) { |
+ SkDebugf("Error: Color type not supported.\n"); |
+ return nullptr; |
+ } |
+ |
+ // Correct the output region if necessary |
+ int left = SkTMax(0, input_x); |
+ int leftOffset = left - input_x; |
+ int top = SkTMax(0, input_y); |
+ int topOffset = top - input_y; |
+ int width = SkTMin(this->width() - left, input_w - leftOffset); |
+ int height = SkTMin(this->height() - top, input_h - topOffset); |
+ if (width <= 0 || height <= 0) { |
+ SkDebugf("Error: Region must intersect part of the image.\n"); |
+ return nullptr; |
+ } |
+ |
+ // Create the image info the decode |
+ SkAlphaType dstAlphaType = fDecoder->getInfo().alphaType(); |
+ if (kUnpremul_SkAlphaType == dstAlphaType) { |
+ dstAlphaType = kPremul_SkAlphaType; |
+ } |
+ SkImageInfo decodeInfo = SkImageInfo::Make(this->width(), this->height(), |
+ dstColorType, dstAlphaType); |
+ |
+ // Start the scanline decoder |
+ SkCodec::Result r = fDecoder->start(decodeInfo); |
+ if (SkCodec::kSuccess != r) { |
+ SkDebugf("Error: Could not start scanline decoder.\n"); |
+ return nullptr; |
+ } |
+ |
+ // Allocate a bitmap for the unscaled decode |
+ SkBitmap tmp; |
+ SkImageInfo tmpInfo = decodeInfo.makeWH(this->width(), height); |
+ if (!tmp.tryAllocPixels(tmpInfo)) { |
+ SkDebugf("Error: Could not allocate pixels.\n"); |
+ return nullptr; |
+ } |
+ |
+ // Skip the unneeded rows |
+ if (SkCodec::kSuccess != fDecoder->skipScanlines(top)) { |
+ SkDebugf("Error: Failed to skip scanlines.\n"); |
+ return nullptr; |
+ } |
+ |
+ // Decode the necessary rows |
+ SkCodec::Result result = fDecoder->getScanlines(tmp.getAddr(0, 0), height, |
scroggo
2015/09/01 22:05:28
So this won't work for all BMP, for example, but t
msarett
2015/09/02 18:02:23
Correct. This was written before we had kBottomUp
|
+ tmp.rowBytes()); |
+ switch (result) { |
+ case SkCodec::kSuccess: |
+ case SkCodec::kIncompleteInput: |
+ break; |
+ default: |
+ SkDebugf("Error: Failed to get scanlines.\n"); |
+ return nullptr; |
+ } |
+ |
+ // Calculate the size of the output |
+ int outWidth = SkTMax(1, input_w / sampleSize); |
+ int outHeight = SkTMax(1, input_h / sampleSize); |
+ |
+ // Initialize the destination bitmap |
+ SkAutoTDelete<SkBitmap> bitmap(SkNEW(SkBitmap)); |
+ SkImageInfo dstInfo = decodeInfo.makeWH(outWidth, outHeight); |
+ if (!bitmap->tryAllocPixels(dstInfo)) { |
+ SkDebugf("Error: Could not allocate pixels.\n"); |
+ return nullptr; |
+ } |
+ |
+ // Zero the bitmap if the region is not completely within the image. |
+ // TODO (msarett): Can we make this faster by implementing it to only |
+ // zero parts of the image that we won't overwrite with |
+ // pixels? |
+ // TODO (msarett): This could be skipped if memory is zero initialized. |
+ // This would matter if this code is moved to Android and |
+ // uses Android bitmaps. |
+ if (0 != leftOffset || 0 != topOffset || |
+ input_x + input_w > this->width() || |
+ input_y + input_h > this->height()) { |
+ bitmap->eraseColor(0); |
+ } |
+ |
+ // Use a canvas to crop and scale to the destination bitmap |
+ SkAutoTDelete<SkCanvas> canvas(SkNEW_ARGS(SkCanvas, (*bitmap))); |
+ SkRect src = SkRect::MakeXYWH(left, 0, width, height); |
+ SkRect dst = SkRect::MakeXYWH(leftOffset / sampleSize, topOffset / sampleSize, |
+ SkTMax(1, width / sampleSize), SkTMax(1, height / sampleSize)); |
+ SkPaint* paint = new SkPaint(); |
+ // Overwrite the dst with the src pixels |
+ paint->setXfermodeMode(SkXfermode::kSrc_Mode); |
+ // TODO (msarett): Test multiple filter qualities. kNone is the default. |
+ canvas->drawBitmapRect(tmp, src, dst, paint); |
+ |
+ return bitmap.detach(); |
+} |