blob: 2011314bff8f9a5164b46c1ad8e9fc19a5a1d236 [file] [log] [blame]
Ruben Brunk370e2432014-10-14 18:33:23 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.cts.verifier.camera.its;
18
19import android.content.Context;
20import android.graphics.ImageFormat;
21import android.hardware.camera2.CameraDevice;
22import android.hardware.camera2.CameraCharacteristics;
23import android.hardware.camera2.CaptureRequest;
24import android.hardware.camera2.CaptureResult;
25import android.hardware.camera2.params.MeteringRectangle;
26import android.hardware.camera2.params.StreamConfigurationMap;
27import android.media.Image;
28import android.media.Image.Plane;
29import android.net.Uri;
30import android.os.Environment;
31import android.util.Log;
32import android.util.Size;
33
34import org.json.JSONArray;
35import org.json.JSONObject;
36
37import java.nio.ByteBuffer;
38import java.nio.charset.Charset;
39import java.util.ArrayList;
40import java.util.List;
41
42public class ItsUtils {
43 public static final String TAG = ItsUtils.class.getSimpleName();
44
45 public static ByteBuffer jsonToByteBuffer(JSONObject jsonObj) {
46 return ByteBuffer.wrap(jsonObj.toString().getBytes(Charset.defaultCharset()));
47 }
48
49 public static MeteringRectangle[] getJsonWeightedRectsFromArray(
50 JSONArray a, boolean normalized, int width, int height)
51 throws ItsException {
52 try {
53 // Returns [x0,y0,x1,y1,wgt, x0,y0,x1,y1,wgt, x0,y0,x1,y1,wgt, ...]
54 assert(a.length() % 5 == 0);
55 MeteringRectangle[] ma = new MeteringRectangle[a.length() / 5];
56 for (int i = 0; i < a.length(); i += 5) {
57 int x,y,w,h;
58 if (normalized) {
59 x = (int)Math.floor(a.getDouble(i+0) * width + 0.5f);
60 y = (int)Math.floor(a.getDouble(i+1) * height + 0.5f);
61 w = (int)Math.floor(a.getDouble(i+2) * width + 0.5f);
62 h = (int)Math.floor(a.getDouble(i+3) * height + 0.5f);
63 } else {
64 x = a.getInt(i+0);
65 y = a.getInt(i+1);
66 w = a.getInt(i+2);
67 h = a.getInt(i+3);
68 }
69 x = Math.max(x, 0);
70 y = Math.max(y, 0);
71 w = Math.min(w, width-x);
72 h = Math.min(h, height-y);
73 int wgt = a.getInt(i+4);
74 ma[i/5] = new MeteringRectangle(x,y,w,h,wgt);
75 }
76 return ma;
77 } catch (org.json.JSONException e) {
78 throw new ItsException("JSON error: ", e);
79 }
80 }
81
82 public static JSONArray getOutputSpecs(JSONObject jsonObjTop)
83 throws ItsException {
84 try {
85 if (jsonObjTop.has("outputSurfaces")) {
86 return jsonObjTop.getJSONArray("outputSurfaces");
87 }
88 return null;
89 } catch (org.json.JSONException e) {
90 throw new ItsException("JSON error: ", e);
91 }
92 }
93
94 public static Size[] getRawOutputSizes(CameraCharacteristics ccs)
95 throws ItsException {
96 return getOutputSizes(ccs, ImageFormat.RAW_SENSOR);
97 }
98
99 public static Size[] getJpegOutputSizes(CameraCharacteristics ccs)
100 throws ItsException {
101 return getOutputSizes(ccs, ImageFormat.JPEG);
102 }
103
104 public static Size[] getYuvOutputSizes(CameraCharacteristics ccs)
105 throws ItsException {
106 return getOutputSizes(ccs, ImageFormat.YUV_420_888);
107 }
108
109 private static Size[] getOutputSizes(CameraCharacteristics ccs, int format)
110 throws ItsException {
111 StreamConfigurationMap configMap = ccs.get(
112 CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
113 if (configMap == null) {
114 throw new ItsException("Failed to get stream config");
115 }
116 return configMap.getOutputSizes(format);
117 }
118
119 public static byte[] getDataFromImage(Image image)
120 throws ItsException {
121 int format = image.getFormat();
122 int width = image.getWidth();
123 int height = image.getHeight();
124 byte[] data = null;
125
126 // Read image data
127 Plane[] planes = image.getPlanes();
128
129 // Check image validity
130 if (!checkAndroidImageFormat(image)) {
131 throw new ItsException(
132 "Invalid image format passed to getDataFromImage: " + image.getFormat());
133 }
134
135 if (format == ImageFormat.JPEG) {
136 // JPEG doesn't have pixelstride and rowstride, treat it as 1D buffer.
137 ByteBuffer buffer = planes[0].getBuffer();
138 data = new byte[buffer.capacity()];
139 buffer.get(data);
140 return data;
141 } else if (format == ImageFormat.YUV_420_888 || format == ImageFormat.RAW_SENSOR
142 || format == ImageFormat.RAW10) {
143 int offset = 0;
144 data = new byte[width * height * ImageFormat.getBitsPerPixel(format) / 8];
Yin-Chia Yehccca9512014-10-28 10:52:37 -0700145 int maxRowSize = planes[0].getRowStride();
146 for (int i = 0; i < planes.length; i++) {
147 if (maxRowSize < planes[i].getRowStride()) {
148 maxRowSize = planes[i].getRowStride();
149 }
150 }
151 byte[] rowData = new byte[maxRowSize];
Ruben Brunk370e2432014-10-14 18:33:23 -0700152 for (int i = 0; i < planes.length; i++) {
153 ByteBuffer buffer = planes[i].getBuffer();
154 int rowStride = planes[i].getRowStride();
155 int pixelStride = planes[i].getPixelStride();
156 int bytesPerPixel = ImageFormat.getBitsPerPixel(format) / 8;
157 Logt.i(TAG, String.format(
158 "Reading image: fmt %d, plane %d, w %d, h %d, rowStride %d, pixStride %d",
159 format, i, width, height, rowStride, pixelStride));
160 // For multi-planar yuv images, assuming yuv420 with 2x2 chroma subsampling.
161 int w = (i == 0) ? width : width / 2;
162 int h = (i == 0) ? height : height / 2;
163 for (int row = 0; row < h; row++) {
164 if (pixelStride == bytesPerPixel) {
165 // Special case: optimized read of the entire row
166 int length = w * bytesPerPixel;
167 buffer.get(data, offset, length);
168 // Advance buffer the remainder of the row stride
Yin-Chia Yeh05dc6182015-02-04 13:30:14 -0800169 if (row < h - 1) {
170 buffer.position(buffer.position() + rowStride - length);
171 }
Ruben Brunk370e2432014-10-14 18:33:23 -0700172 offset += length;
173 } else {
174 // Generic case: should work for any pixelStride but slower.
175 // Use intermediate buffer to avoid read byte-by-byte from
176 // DirectByteBuffer, which is very bad for performance.
177 // Also need avoid access out of bound by only reading the available
178 // bytes in the bytebuffer.
179 int readSize = rowStride;
180 if (buffer.remaining() < readSize) {
181 readSize = buffer.remaining();
182 }
183 buffer.get(rowData, 0, readSize);
184 if (pixelStride >= 1) {
185 for (int col = 0; col < w; col++) {
186 data[offset++] = rowData[col * pixelStride];
187 }
188 } else {
189 // PixelStride of 0 can mean pixel isn't a multiple of 8 bits, for
190 // example with RAW10. Just copy the buffer, dropping any padding at
191 // the end of the row.
192 int length = (w * ImageFormat.getBitsPerPixel(format)) / 8;
193 System.arraycopy(rowData,0,data,offset,length);
194 offset += length;
195 }
196 }
197 }
198 }
199 Logt.i(TAG, String.format("Done reading image, format %d", format));
200 return data;
201 } else {
202 throw new ItsException("Unsupported image format: " + format);
203 }
204 }
205
206 private static boolean checkAndroidImageFormat(Image image) {
207 int format = image.getFormat();
208 Plane[] planes = image.getPlanes();
209 switch (format) {
210 case ImageFormat.YUV_420_888:
211 case ImageFormat.NV21:
212 case ImageFormat.YV12:
213 return 3 == planes.length;
214 case ImageFormat.RAW_SENSOR:
215 case ImageFormat.RAW10:
216 case ImageFormat.JPEG:
217 return 1 == planes.length;
218 default:
219 return false;
220 }
221 }
222}
223