Index: src/utils/SkBitmapRegionCanvas.cpp |
diff --git a/src/utils/SkBitmapRegionCanvas.cpp b/src/utils/SkBitmapRegionCanvas.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9118c74fd14a67ac6293b4670775dea7fe86d195 |
--- /dev/null |
+++ b/src/utils/SkBitmapRegionCanvas.cpp |
@@ -0,0 +1,177 @@ |
+/* |
+ * 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) |
+{} |
+ |
+/* |
+ * Chooses the correct image subset offsets and dimensions for the partial decode. |
+ */ |
+static inline void set_subset_region(int regionOffset, int regionDimension, |
+ int imageOriginalDimension, int* imageSubsetOffset, int* bitmapOffset, |
+ int* imageSubsetDimension) { |
+ |
+ // This must be at least zero, we can't start decoding the image at a negative coordinate. |
+ *imageSubsetOffset = SkTMax(0, regionOffset); |
+ |
+ // If regionOffset is less than zero, we decode to an offset location in the output bitmap. |
+ *bitmapOffset = *imageSubsetOffset - regionOffset; |
+ |
+ // Use imageSusetOffset to make sure we don't decode pixels past the edge of the image. |
+ // Use bitmapOffset to make sure we don't decode pixels past the edge of the region. |
+ *imageSubsetDimension = SkTMin(imageOriginalDimension - *imageSubsetOffset, |
+ regionDimension - *bitmapOffset); |
+} |
+ |
+static int get_scaled_dimension(int dimension, int sampleSize) { |
+ return SkTMax(1, dimension / sampleSize); |
+} |
+ |
+/* |
+ * Three differences from the Android version: |
+ * Returns a Skia bitmap instead of an Android bitmap. |
+ * Android version attempts to reuse a recycled bitmap. |
+ * Removed the options object and used parameters for color type and |
+ * sample size. |
+ */ |
+SkBitmap* SkBitmapRegionCanvas::decodeRegion(int regionLeft, int regionTop, |
scroggo
2015/09/04 17:55:06
I know Android started using this term "region", b
msarett
2015/09/04 18:54:10
Will do. And I'm replacing Left and Top in all of
|
+ int regionWidth, int regionHeight, |
+ 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; |
+ } |
+ |
+ // The client may not necessarily request a region that is fully within |
scroggo
2015/09/04 17:55:07
I find these comments very helpful :)
msarett
2015/09/04 18:54:10
Great! Third try is the charm haha.
|
+ // the image. We may need to do some calculation to determine what part |
+ // of the image to decode. |
+ |
+ // The left offset of the portion of the image we want, where zero |
+ // indicates the left edge of the image. |
+ int imageSubsetLeft; |
+ // The size of the output bitmap is determined by the size of the |
scroggo
2015/09/04 17:55:07
nit: blank line between code and large comment blo
msarett
2015/09/04 18:54:10
Done.
|
+ // requested region, not by the size of the intersection of the region |
+ // and the image dimensions. If regionLeft is negative, we will need to |
+ // place decoded pixels into the output bitmap starting at a left offset. |
+ // If this is non-zero, imageSubsetLeft must be zero. |
+ int bitmapLeft; |
scroggo
2015/09/04 17:55:06
Maybe this would be better named outLeft? Somethin
msarett
2015/09/04 18:54:10
Yes agreed.
|
+ // The width of the portion of the image that we will write to the output |
+ // bitmap. If the region is not fully contained within the image, this |
+ // will not be the same as regionWidth. |
+ int imageSubsetWidth; |
+ set_subset_region(regionLeft, regionWidth, this->width(), &imageSubsetLeft, &bitmapLeft, |
+ &imageSubsetWidth); |
+ |
+ // The top offset of the portion of the image we want, where zero |
+ // indicates the top edge of the image. |
+ int imageSubsetTop; |
+ // The size of the output bitmap is determined by the size of the |
+ // requested region, not by the size of the intersection of the region |
+ // and the image dimensions. If regionTop is negative, we will need to |
+ // place decoded pixels into the output bitmap starting at a top offset. |
+ // If this is non-zero, imageSubsetTop must be zero. |
+ int bitmapTop; |
+ // The height of the portion of the image that we will write to the output |
+ // bitmap. If the region is not fully contained within the image, this |
+ // will not be the same as regionHeight. |
+ int imageSubsetHeight; |
+ set_subset_region(regionTop, regionHeight, this->height(), &imageSubsetTop, &bitmapTop, |
+ &imageSubsetHeight); |
+ |
+ if (imageSubsetWidth <= 0 || imageSubsetHeight <= 0) { |
+ SkDebugf("Error: Region must intersect part of the image.\n"); |
+ return nullptr; |
+ } |
+ |
+ // Create the image info for 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(), imageSubsetHeight); |
+ if (!tmp.tryAllocPixels(tmpInfo)) { |
+ SkDebugf("Error: Could not allocate pixels.\n"); |
+ return nullptr; |
+ } |
+ |
+ // Skip the unneeded rows |
+ if (SkCodec::kSuccess != fDecoder->skipScanlines(imageSubsetTop)) { |
+ SkDebugf("Error: Failed to skip scanlines.\n"); |
+ return nullptr; |
+ } |
+ |
+ // Decode the necessary rows |
+ SkCodec::Result result = fDecoder->getScanlines(tmp.getAddr(0, 0), imageSubsetHeight, |
+ 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 |
+ const int outWidth = get_scaled_dimension(regionWidth, sampleSize); |
+ const int outHeight = get_scaled_dimension(regionHeight, sampleSize); |
+ |
+ // Initialize the destination bitmap |
+ SkAutoTDelete<SkBitmap> bitmap(new 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 != bitmapLeft || 0 != bitmapTop || |
+ regionLeft + regionWidth > this->width() || |
+ regionTop + regionHeight > this->height()) { |
+ bitmap->eraseColor(0); |
+ } |
+ |
+ // Use a canvas to crop and scale to the destination bitmap |
+ SkCanvas canvas(*bitmap); |
+ SkRect src = SkRect::MakeXYWH(imageSubsetLeft, 0, imageSubsetWidth, imageSubsetHeight); |
+ SkRect dst = SkRect::MakeXYWH(bitmapLeft / sampleSize, bitmapTop / sampleSize, |
+ get_scaled_dimension(imageSubsetWidth, sampleSize), |
+ get_scaled_dimension(imageSubsetHeight, sampleSize)); |
+ SkPaint paint; |
+ // 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(); |
+} |