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