OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.shape_detection; | 5 package org.chromium.shape_detection; |
6 | 6 |
7 import android.graphics.Bitmap; | 7 import android.graphics.Bitmap; |
8 | 8 |
9 import com.google.android.gms.vision.Frame; | 9 import com.google.android.gms.vision.Frame; |
10 | 10 |
11 import org.chromium.mojo.system.MojoException; | 11 import org.chromium.skia.mojom.ColorType; |
12 import org.chromium.mojo.system.SharedBufferHandle; | |
13 import org.chromium.mojo.system.SharedBufferHandle.MapFlags; | |
14 | 12 |
15 import java.nio.ByteBuffer; | 13 import java.nio.ByteBuffer; |
16 | 14 |
17 /** | 15 /** |
18 * Utility class to convert a SharedBufferHandle to a GMS core YUV Frame. | 16 * Utility class to convert a Bitmap to a GMS core YUV Frame. |
19 */ | 17 */ |
20 public class SharedBufferUtils { | 18 public class BitmapUtils { |
21 public static Frame convertToFrame( | 19 public static Bitmap convertToBitmap(org.chromium.skia.mojom.Bitmap bitmapDa
ta) { |
22 SharedBufferHandle frameData, final int width, final int height) { | 20 int width = bitmapData.width; |
| 21 int height = bitmapData.height; |
23 final long numPixels = (long) width * height; | 22 final long numPixels = (long) width * height; |
24 // TODO(mcasas): https://crbug.com/670028 homogeneize overflow checking. | 23 // TODO(mcasas): https://crbug.com/670028 homogeneize overflow checking. |
25 if (!frameData.isValid() || width <= 0 || height <= 0 || numPixels > (Lo
ng.MAX_VALUE / 4)) { | 24 if (bitmapData.pixelData == null || width <= 0 || height <= 0 |
| 25 || numPixels > (Long.MAX_VALUE / 4)) { |
26 return null; | 26 return null; |
27 } | 27 } |
28 | 28 |
29 // Mapping |frameData| will fail if the intended mapped size is larger | 29 // TODO(junwei.fu): https://crbug.com/684921 support other bitmap pixel
formats. |
30 // than its actual capacity, which is limited by the appropriate | 30 if (bitmapData.colorType != ColorType.RGBA_8888 |
31 // mojo::edk::Configuration entry. | 31 && bitmapData.colorType != ColorType.BGRA_8888) { |
32 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none()
); | 32 return null; |
| 33 } |
| 34 |
| 35 ByteBuffer imageBuffer = ByteBuffer.wrap(bitmapData.pixelData); |
33 if (imageBuffer.capacity() <= 0) { | 36 if (imageBuffer.capacity() <= 0) { |
34 return null; | 37 return null; |
35 } | 38 } |
36 | 39 |
37 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88
88); | 40 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88
88); |
38 bitmap.copyPixelsFromBuffer(imageBuffer); | 41 bitmap.copyPixelsFromBuffer(imageBuffer); |
39 try { | 42 |
40 frameData.unmap(imageBuffer); | 43 return bitmap; |
41 frameData.close(); | 44 } |
42 } catch (MojoException e) { | 45 |
| 46 public static Frame convertToFrame(org.chromium.skia.mojom.Bitmap bitmapData
) { |
| 47 Bitmap bitmap = convertToBitmap(bitmapData); |
| 48 if (bitmap == null) { |
| 49 return null; |
43 } | 50 } |
44 | 51 |
45 try { | 52 try { |
46 // This constructor implies a pixel format conversion to YUV. | 53 // This constructor implies a pixel format conversion to YUV. |
47 return new Frame.Builder().setBitmap(bitmap).build(); | 54 return new Frame.Builder().setBitmap(bitmap).build(); |
48 } catch (IllegalArgumentException | IllegalStateException ex) { | 55 } catch (IllegalArgumentException | IllegalStateException ex) { |
49 return null; | 56 return null; |
50 } | 57 } |
51 } | 58 } |
52 } | 59 } |
OLD | NEW |