Index: src/utils/SkBitmapRegionCanvas.cpp |
diff --git a/src/utils/SkBitmapRegionCanvas.cpp b/src/utils/SkBitmapRegionCanvas.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bb7659a92a5100062dc18f85ce4415c2a172a183 |
--- /dev/null |
+++ b/src/utils/SkBitmapRegionCanvas.cpp |
@@ -0,0 +1,123 @@ |
+/* |
+ * 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); |
scroggo
2015/09/02 21:32:24
This is because BRD supports subsets that are not
msarett
2015/09/03 19:20:52
Yes :(
|
+ 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); |
scroggo
2015/09/02 21:32:24
Calculating width and height are very similar. Wha
msarett
2015/09/03 19:20:52
Seems like a good idea. Will do.
|
+ if (width <= 0 || height <= 0) { |
+ SkDebugf("Error: Region must intersect part of the image.\n"); |
+ return nullptr; |
+ } |
+ |
+ // Create the image info the decode |
scroggo
2015/09/02 21:32:24
for* the decode?
msarett
2015/09/03 19:20:52
Done.
|
+ SkAlphaType dstAlphaType = fDecoder->getInfo().alphaType(); |
+ if (kUnpremul_SkAlphaType == dstAlphaType) { |
scroggo
2015/09/02 21:32:24
This is a downside to this version of BRD - we can
msarett
2015/09/03 19:20:52
Yeah agreed. I will file a bug for comparing/choo
|
+ dstAlphaType = kPremul_SkAlphaType; |
+ } |
+ SkImageInfo decodeInfo = SkImageInfo::Make(this->width(), this->height(), |
+ dstColorType, dstAlphaType); |
+ |
+ // Start the scanline decoder |
+ SkCodec::Result r = fDecoder->start(decodeInfo); |
+ SkASSERT(SkScanlineDecoder::kTopDown_SkScanlineOrder == fDecoder->getScanlineOrder()); |
+ 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, |
+ 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); |
scroggo
2015/09/02 21:32:24
It surprised me that this is input_w and not width
msarett
2015/09/03 19:20:51
Yes. No matter what (for backward compatibility)
|
+ int outHeight = SkTMax(1, input_h / sampleSize); |
+ |
+ // Initialize the destination bitmap |
+ SkAutoTDelete<SkBitmap> bitmap(SkNEW(SkBitmap)); |
scroggo
2015/09/02 21:32:24
nit: new
msarett
2015/09/03 19:20:52
Done.
|
+ 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 |
scroggo
2015/09/02 21:32:24
Shouldn't we be using SkSurface? I guess we cannot
msarett
2015/09/03 19:20:52
I don't really know anything about SkSurface vs Sk
scroggo
2015/09/03 20:07:41
Yes.
msarett
2015/09/03 22:10:30
Ok that makes sense. Since we are returning an Sk
|
+ SkAutoTDelete<SkCanvas> canvas(SkNEW_ARGS(SkCanvas, (*bitmap))); |
scroggo
2015/09/02 21:32:24
nit: new
Actually, wait, can you put this on the
msarett
2015/09/03 19:20:51
Yes I believe so!
|
+ SkRect src = SkRect::MakeXYWH(left, 0, width, height); |
+ SkRect dst = SkRect::MakeXYWH(leftOffset / sampleSize, topOffset / sampleSize, |
+ SkTMax(1, width / sampleSize), SkTMax(1, height / sampleSize)); |
scroggo
2015/09/02 21:32:24
SkTMax(1, N / sampleSize)
This pattern gets used
msarett
2015/09/03 19:20:51
Yes!
|
+ SkPaint* paint = new SkPaint(); |
scroggo
2015/09/02 21:32:24
This gets leaked. Why not just put it on the stack
msarett
2015/09/03 19:20:52
Putting on stack!
|
+ // 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(); |
+} |