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

Unified Diff: src/utils/SkBitmapRegionCanvas.cpp

Issue 1288963002: Provides multiple implementations of Android's SkBitmapRegionDecoder (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Response to comments Created 5 years, 3 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
Index: src/utils/SkBitmapRegionCanvas.cpp
diff --git a/src/utils/SkBitmapRegionCanvas.cpp b/src/utils/SkBitmapRegionCanvas.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..5b0da1883e47f9478bd3400dfe33257403abbb68
--- /dev/null
+++ b/src/utils/SkBitmapRegionCanvas.cpp
@@ -0,0 +1,137 @@
+/*
+ * 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)
+{}
+
+/*
+ * Adjusts the image subset dimensions to safely fit within the original image,
+ * and to safely fit within the requested region size.
+ */
+static int get_safe_subset_dimension(int imageOffset, int imageDim, int regionOffset,
scroggo 2015/09/03 20:07:41 These names are pretty opaque to me. They describe
msarett 2015/09/03 22:10:30 I'll add comments, I was hoping the names would ma
+ int regionDim) {
+ int safeImageDim = imageDim - imageOffset;
+ int safeRegionDim = regionDim - regionOffset;
+ return SkTMin(safeImageDim, safeRegionDim);
+}
+
+static int get_scaled_dimension(int dimension, int sampleSize) {
+ return SkTMax(1, dimension / sampleSize);
scroggo 2015/09/03 20:07:41 It seems like this is logically the same as SkScal
msarett 2015/09/03 22:10:30 Yeah I tried to share it in SkCodecPriv.h but that
scroggo 2015/09/04 17:55:06 Ah, because this is in utils? maybe declare it as
msarett 2015/09/04 18:54:10 I'll go with the original implementation in SkScal
+}
+
+/*
+ * 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
+ const int left = SkTMax(0, input_x);
+ const int leftOffset = left - input_x;
+ const int top = SkTMax(0, input_y);
+ const int topOffset = top - input_y;
+ const int width = get_safe_subset_dimension(left, this->width(), leftOffset, input_w);
+ const int height = get_safe_subset_dimension(top, this->height(), topOffset, input_h);
+ if (width <= 0 || height <= 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(), 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
+ const int outWidth = get_scaled_dimension(input_w, sampleSize);
+ const int outHeight = get_scaled_dimension(input_h, 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 != 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
+ SkCanvas canvas(*bitmap);
+ SkRect src = SkRect::MakeXYWH(left, 0, width, height);
+ SkRect dst = SkRect::MakeXYWH(leftOffset / sampleSize, topOffset / sampleSize,
+ get_scaled_dimension(width, sampleSize), get_scaled_dimension(height, 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();
+}

Powered by Google App Engine
This is Rietveld 408576698