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

Side by Side Diff: content/public/android/java/src/org/chromium/content/browser/shapedetection/FaceDetectionImpl.java

Issue 2629433003: ShapeDetection: use mojom::Bitmap for mojo interface. (Closed)
Patch Set: ShapeDetection: use mojom::Bitmap for mojo interface. Created 3 years, 11 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.content.browser.shapedetection; 5 package org.chromium.content.browser.shapedetection;
6 6
7 import android.graphics.Bitmap; 7 import android.graphics.Bitmap;
8 import android.graphics.Bitmap.Config;
8 import android.graphics.PointF; 9 import android.graphics.PointF;
9 import android.media.FaceDetector; 10 import android.media.FaceDetector;
10 import android.media.FaceDetector.Face; 11 import android.media.FaceDetector.Face;
11 12
12 import org.chromium.base.Log; 13 import org.chromium.base.Log;
13 import org.chromium.gfx.mojom.RectF; 14 import org.chromium.gfx.mojom.RectF;
14 import org.chromium.mojo.system.MojoException; 15 import org.chromium.mojo.system.MojoException;
15 import org.chromium.mojo.system.SharedBufferHandle;
16 import org.chromium.mojo.system.SharedBufferHandle.MapFlags;
17 import org.chromium.shape_detection.mojom.FaceDetection; 16 import org.chromium.shape_detection.mojom.FaceDetection;
18 import org.chromium.shape_detection.mojom.FaceDetectionResult; 17 import org.chromium.shape_detection.mojom.FaceDetectionResult;
19 import org.chromium.shape_detection.mojom.FaceDetectorOptions; 18 import org.chromium.shape_detection.mojom.FaceDetectorOptions;
19 import org.chromium.skia.mojom.ColorType;
20 20
21 import java.nio.ByteBuffer; 21 import java.nio.ByteBuffer;
22 22
23 /** 23 /**
24 * Android implementation of the FaceDetection service defined in 24 * Android implementation of the FaceDetection service defined in
25 * services/shape_detection/public/interfaces/facedetection.mojom 25 * services/shape_detection/public/interfaces/facedetection.mojom
26 */ 26 */
27 public class FaceDetectionImpl implements FaceDetection { 27 public class FaceDetectionImpl implements FaceDetection {
28 private static final String TAG = "FaceDetectionImpl"; 28 private static final String TAG = "FaceDetectionImpl";
29 private static final int MAX_FACES = 32; 29 private static final int MAX_FACES = 32;
30 private final boolean mFastMode; 30 private final boolean mFastMode;
31 private final int mMaxFaces; 31 private final int mMaxFaces;
32 32
33 FaceDetectionImpl(FaceDetectorOptions options) { 33 FaceDetectionImpl(FaceDetectorOptions options) {
34 mFastMode = options.fastMode; 34 mFastMode = options.fastMode;
35 mMaxFaces = Math.min(options.maxDetectedFaces, MAX_FACES); 35 mMaxFaces = Math.min(options.maxDetectedFaces, MAX_FACES);
36 } 36 }
37 37
38 private Config validConfig(int colorType) {
39 switch (colorType) {
40 case ColorType.ALPHA_8:
41 return Config.ALPHA_8;
42 case ColorType.RGB_565:
43 return Config.RGB_565;
44 case ColorType.ARGB_4444:
45 return Config.ARGB_4444;
46 case ColorType.RGBA_8888:
47 case ColorType.BGRA_8888:
48 return Config.ARGB_8888;
49 }
50 return null;
51 }
52
38 @Override 53 @Override
39 public void detect( 54 public void detect(org.chromium.skia.mojom.Bitmap bitmapData, DetectResponse callback) {
40 SharedBufferHandle frameData, int width, int height, DetectResponse callback) { 55 int width = bitmapData.width;
56 int height = bitmapData.height;
57 Config bitmapConfig = validConfig(bitmapData.colorType);
41 final long numPixels = (long) width * height; 58 final long numPixels = (long) width * height;
42 // TODO(xianglu): https://crbug.com/670028 homogeneize overflow checking . 59 // TODO(xianglu): https://crbug.com/670028 homogeneize overflow checking .
43 if (!frameData.isValid() || width <= 0 || height <= 0 || numPixels > (Lo ng.MAX_VALUE / 4)) { 60 if (bitmapData.pixelData == null || width <= 0 || height <= 0
61 || numPixels > (Long.MAX_VALUE / 4) || bitmapConfig == null) {
44 Log.d(TAG, "Invalid argument(s)."); 62 Log.d(TAG, "Invalid argument(s).");
45 callback.call(new FaceDetectionResult()); 63 callback.call(new FaceDetectionResult());
46 return; 64 return;
47 } 65 }
48 66
49 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none() ); 67 ByteBuffer imageBuffer = ByteBuffer.wrap(bitmapData.pixelData);
50 if (imageBuffer.capacity() <= 0) { 68 if (imageBuffer.capacity() <= 0) {
51 Log.d(TAG, "Failed to map from SharedBufferHandle."); 69 Log.d(TAG, "Failed to wrap from Bitmap.");
52 callback.call(new FaceDetectionResult()); 70 callback.call(new FaceDetectionResult());
53 return; 71 return;
54 } 72 }
55 73
56 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88 88); 74 Bitmap unPremultipliedBitmap;
75 if (bitmapData.colorType == ColorType.RGB_565) {
76 unPremultipliedBitmap = Bitmap.createBitmap(width, height, bitmapCon fig);
57 77
58 // An int array is needed to construct a Bitmap. However the Bytebuffer 78 // An int array is needed to construct a Bitmap. However the Bytebuf fer
59 // we get from |sharedBufferHandle| is directly allocated and does not 79 // we get from |bitmapData| is directly allocated and does not have a supporting array.
60 // have a supporting array. Therefore we need to copy from |imageBuffer| 80 // Therefore we need to copy from |imageBuffer| to create this inter mediate Bitmap.
61 // to create this intermediate Bitmap. 81 // TODO(xianglu): Consider worker pool as appropriate threads.
62 // TODO(xianglu): Consider worker pool as appropriate threads. 82 // http://crbug.com/655814
63 // http://crbug.com/655814 83 unPremultipliedBitmap.copyPixelsFromBuffer(imageBuffer);
64 bitmap.copyPixelsFromBuffer(imageBuffer); 84 } else {
85 Bitmap bitmap = Bitmap.createBitmap(width, height, bitmapConfig);
65 86
66 // A Bitmap must be in 565 format for findFaces() to work. See 87 bitmap.copyPixelsFromBuffer(imageBuffer);
67 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/media/java/andro id/media/FaceDetector.java#124 88
68 // 89 // A Bitmap must be in 565 format for findFaces() to work. See
69 // It turns out that FaceDetector is not able to detect correctly if 90 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/media/java/a ndroid/media/FaceDetector.java#124
70 // simply using pixmap.setConfig(). The reason might be that findFaces() 91 //
71 // needs non-premultiplied ARGB arrangement, while the alpha type in the 92 // It turns out that FaceDetector is not able to detect correctly if
72 // original image is premultiplied. We can use getPixels() which does 93 // simply using pixmap.setConfig(). The reason might be that findFac es()
73 // the unmultiplication while copying to a new array. See 94 // needs non-premultiplied ARGB arrangement, while the alpha type in the
74 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/graphics/java/an droid/graphics/Bitmap.java#538 95 // original image is premultiplied. We can use getPixels() which doe s
75 int[] pixels = new int[width * height]; 96 // the unmultiplication while copying to a new array. See
76 bitmap.getPixels(pixels, 0, width, 0, 0, width, height); 97 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/graphics/jav a/android/graphics/Bitmap.java#538
77 Bitmap unPremultipliedBitmap = 98 int[] pixels = new int[width * height];
78 Bitmap.createBitmap(pixels, width, height, Bitmap.Config.RGB_565 ); 99 bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
100 unPremultipliedBitmap =
101 Bitmap.createBitmap(pixels, width, height, Bitmap.Config.RGB _565);
102 }
mcasas 2017/01/24 20:48:31 Similar comments to the others in this section: we
79 103
80 FaceDetector detector = new FaceDetector(width, height, mMaxFaces); 104 FaceDetector detector = new FaceDetector(width, height, mMaxFaces);
81 Face[] detectedFaces = new Face[mMaxFaces]; 105 Face[] detectedFaces = new Face[mMaxFaces];
82 // findFaces() will stop at |mMaxFaces|. 106 // findFaces() will stop at |mMaxFaces|.
83 final int numberOfFaces = detector.findFaces(unPremultipliedBitmap, dete ctedFaces); 107 final int numberOfFaces = detector.findFaces(unPremultipliedBitmap, dete ctedFaces);
84 108
85 FaceDetectionResult faceDetectionResult = new FaceDetectionResult(); 109 FaceDetectionResult faceDetectionResult = new FaceDetectionResult();
86 faceDetectionResult.boundingBoxes = new RectF[numberOfFaces]; 110 faceDetectionResult.boundingBoxes = new RectF[numberOfFaces];
87 for (int i = 0; i < numberOfFaces; i++) { 111 for (int i = 0; i < numberOfFaces; i++) {
88 final Face face = detectedFaces[i]; 112 final Face face = detectedFaces[i];
(...skipping 15 matching lines...) Expand all
104 } 128 }
105 129
106 @Override 130 @Override
107 public void close() {} 131 public void close() {}
108 132
109 @Override 133 @Override
110 public void onConnectionError(MojoException e) { 134 public void onConnectionError(MojoException e) {
111 close(); 135 close();
112 } 136 }
113 } 137 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698