1 /*
2  * Copyright (C) 2019 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 
17 #include "ConfigManagerUtil.h"
18 
19 #include <string>
20 #include <sstream>
21 #include <linux/videodev2.h>
22 #include <system/graphics-base-v1.0.h>
23 
24 #include <android-base/logging.h>
25 
26 
convertToEvsCameraParam(const string & id,CameraParam & camParam)27 bool ConfigManagerUtil::convertToEvsCameraParam(const string &id,
28                                                 CameraParam &camParam) {
29     string trimmed = ConfigManagerUtil::trimString(id);
30     bool success = true;
31 
32     if (!trimmed.compare("BRIGHTNESS")) {
33         camParam =  CameraParam::BRIGHTNESS;
34     } else if (!trimmed.compare("CONTRAST")) {
35         camParam =  CameraParam::CONTRAST;
36     } else if (!trimmed.compare("AUTOGAIN")) {
37         camParam =  CameraParam::AUTOGAIN;
38     } else if (!trimmed.compare("GAIN")) {
39         camParam =  CameraParam::GAIN;
40     } else if (!trimmed.compare("AUTO_WHITE_BALANCE")) {
41         camParam =  CameraParam::AUTO_WHITE_BALANCE;
42     } else if (!trimmed.compare("WHITE_BALANCE_TEMPERATURE")) {
43         camParam =  CameraParam::WHITE_BALANCE_TEMPERATURE;
44     } else if (!trimmed.compare("SHARPNESS")) {
45         camParam =  CameraParam::SHARPNESS;
46     } else if (!trimmed.compare("AUTO_EXPOSURE")) {
47         camParam =  CameraParam::AUTO_EXPOSURE;
48     } else if (!trimmed.compare("ABSOLUTE_EXPOSURE")) {
49         camParam =  CameraParam::ABSOLUTE_EXPOSURE;
50     } else if (!trimmed.compare("ABSOLUTE_FOCUS")) {
51         camParam =  CameraParam::ABSOLUTE_FOCUS;
52     } else if (!trimmed.compare("AUTO_FOCUS")) {
53         camParam =  CameraParam::AUTO_FOCUS;
54     } else if (!trimmed.compare("ABSOLUTE_ZOOM")) {
55         camParam =  CameraParam::ABSOLUTE_ZOOM;
56     } else {
57         success = false;
58     }
59 
60     return success;
61 }
62 
63 
convertToPixelFormat(const string & format,int32_t & pixFormat)64 bool ConfigManagerUtil::convertToPixelFormat(const string &format,
65                                              int32_t &pixFormat) {
66     string trimmed = ConfigManagerUtil::trimString(format);
67     bool success = true;
68 
69     if (!trimmed.compare("RGBA_8888")) {
70         pixFormat =  HAL_PIXEL_FORMAT_RGBA_8888;
71     } else if (!trimmed.compare("YCRCB_420_SP")) {
72         pixFormat =  HAL_PIXEL_FORMAT_YCRCB_420_SP;
73     } else if (!trimmed.compare("YCBCR_422_I")) {
74         pixFormat =  HAL_PIXEL_FORMAT_YCBCR_422_I;
75     } else {
76         success = false;
77     }
78 
79     return success;
80 }
81 
82 
convertToMetadataTag(const char * name,camera_metadata_tag & aTag)83 bool ConfigManagerUtil::convertToMetadataTag(const char *name,
84                                              camera_metadata_tag &aTag) {
85     if (!strcmp(name, "LENS_DISTORTION")) {
86         aTag =  ANDROID_LENS_DISTORTION;
87     } else if (!strcmp(name, "LENS_INTRINSIC_CALIBRATION")) {
88         aTag =  ANDROID_LENS_INTRINSIC_CALIBRATION;
89     } else if (!strcmp(name, "LENS_POSE_ROTATION")) {
90         aTag =  ANDROID_LENS_POSE_ROTATION;
91     } else if (!strcmp(name, "LENS_POSE_TRANSLATION")) {
92         aTag =  ANDROID_LENS_POSE_TRANSLATION;
93     } else if (!strcmp(name, "REQUEST_AVAILABLE_CAPABILITIES")) {
94         aTag =  ANDROID_REQUEST_AVAILABLE_CAPABILITIES;
95     } else if (!strcmp(name, "LOGICAL_MULTI_CAMERA_PHYSICAL_IDS")) {
96         aTag =  ANDROID_LOGICAL_MULTI_CAMERA_PHYSICAL_IDS;
97     } else {
98         return false;
99     }
100 
101     return true;
102 }
103 
104 
convertToCameraCapability(const char * name,camera_metadata_enum_android_request_available_capabilities_t & cap)105 bool ConfigManagerUtil::convertToCameraCapability(
106     const char *name,
107     camera_metadata_enum_android_request_available_capabilities_t &cap) {
108 
109     if (!strcmp(name, "DEPTH_OUTPUT")) {
110         cap = ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DEPTH_OUTPUT;
111     } else if (!strcmp(name, "LOGICAL_MULTI_CAMERA")) {
112         cap = ANDROID_REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA;
113     } else if (!strcmp(name, "MONOCHROME")) {
114         cap = ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MONOCHROME;
115     } else if (!strcmp(name, "SECURE_IMAGE_DATA")) {
116         cap = ANDROID_REQUEST_AVAILABLE_CAPABILITIES_SECURE_IMAGE_DATA;
117     } else {
118         return false;
119     }
120 
121     return true;
122 }
123 
124 
convertFloatArray(const char * sz,const char * vals,size_t & count,const char delimiter)125 float *ConfigManagerUtil::convertFloatArray(const char *sz, const char *vals,
126                                             size_t &count, const char delimiter) {
127     string size_string(sz);
128     string value_string(vals);
129 
130     count = stoi(size_string);
131     float *result = new float[count];
132     stringstream values(value_string);
133 
134     int32_t idx = 0;
135     string token;
136     while (getline(values, token, delimiter)) {
137         result[idx++] = stof(token);
138     }
139 
140     return result;
141 }
142 
143 
trimString(const string & src,const string & ws)144 string ConfigManagerUtil::trimString(const string &src, const string &ws) {
145     const auto s = src.find_first_not_of(ws);
146     if (s == string::npos) {
147         return "";
148     }
149 
150     const auto e = src.find_last_not_of(ws);
151     const auto r = e - s + 1;
152 
153     return src.substr(s, r);
154 }
155 
156