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

Unified Diff: services/shape_detection/android/java/src/org/chromium/shape_detection/BitmapUtils.java

Issue 2875243002: RELAND: ShapeDetection: use mojom::Bitmap for mojo interface. (Closed)
Patch Set: RELAND2: ShapeDetection: use mojom::Bitmap for mojo interface. Created 3 years, 7 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: services/shape_detection/android/java/src/org/chromium/shape_detection/BitmapUtils.java
diff --git a/services/shape_detection/android/java/src/org/chromium/shape_detection/BitmapUtils.java b/services/shape_detection/android/java/src/org/chromium/shape_detection/BitmapUtils.java
new file mode 100644
index 0000000000000000000000000000000000000000..da32a2c3149ea0d3fd85a928482e6a7baca00110
--- /dev/null
+++ b/services/shape_detection/android/java/src/org/chromium/shape_detection/BitmapUtils.java
@@ -0,0 +1,54 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.shape_detection;
+
+import android.graphics.Bitmap;
+
+import com.google.android.gms.vision.Frame;
+
+import org.chromium.skia.mojom.ColorType;
+
+import java.nio.ByteBuffer;
+
+/**
+ * Utility class to convert a Bitmap to a GMS core YUV Frame.
+ */
+public class BitmapUtils {
+ public static Bitmap createBitmap(org.chromium.skia.mojom.Bitmap bitmapData) {
+ int width = bitmapData.width;
+ int height = bitmapData.height;
+ final long numPixels = (long) width * height;
+ // TODO(mcasas): https://crbug.com/670028 homogeneize overflow checking.
+ if (bitmapData.pixelData == null || width <= 0 || height <= 0
+ || numPixels > (Long.MAX_VALUE / 4)) {
+ return null;
+ }
+
+ // TODO(junwei.fu): https://crbug.com/684921 support other bitmap pixel formats.
+ if (bitmapData.colorType != ColorType.RGBA_8888
+ && bitmapData.colorType != ColorType.BGRA_8888) {
+ return null;
+ }
+
+ ByteBuffer imageBuffer = ByteBuffer.wrap(bitmapData.pixelData);
+ if (imageBuffer.capacity() <= 0) {
+ return null;
+ }
+
+ Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
+ bitmap.copyPixelsFromBuffer(imageBuffer);
+
+ return bitmap;
+ }
+
+ public static Frame convertToFrame(org.chromium.skia.mojom.Bitmap bitmapData) {
mcasas 2017/05/19 17:20:22 This method is called convertToX() whereas the pre
+ try {
+ // This constructor implies a pixel format conversion to YUV.
+ return new Frame.Builder().setBitmap(createBitmap(bitmapData)).build();
+ } catch (IllegalArgumentException | IllegalStateException ex) {
+ return null;
+ }
+ }
+}
mcasas 2017/05/19 17:20:22 We landed recently a Junit test for the predecesso

Powered by Google App Engine
This is Rietveld 408576698