• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * calibration_parser.cpp - parse fisheye calibration file
3  *
4  *  Copyright (c) 2016-2017 Intel Corporation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Author: Junkai Wu <junkai.wu@intel.com>
19  */
20 
21 #include "calibration_parser.h"
22 #include "file_handle.h"
23 
24 namespace XCam {
25 
CalibrationParser()26 CalibrationParser::CalibrationParser()
27 {
28 }
29 
30 #define CHECK_NULL(ptr) \
31     if(ptr == NULL) { \
32         XCAM_LOG_ERROR("Parse file failed"); \
33         return XCAM_RETURN_ERROR_FILE; \
34     }
35 
36 XCamReturn
parse_intrinsic_param(char * file_body,IntrinsicParameter & intrinsic_param)37 CalibrationParser::parse_intrinsic_param(char *file_body, IntrinsicParameter &intrinsic_param)
38 {
39     char *line_str = NULL;
40     char *line_endptr = NULL;
41     char *tok_str = NULL;
42     char *tok_endptr = NULL;
43     static const char *line_tokens = "\r\n";
44     static const char *str_tokens = " \t";
45 
46     do {
47         line_str = strtok_r(file_body, line_tokens, &line_endptr);
48         CHECK_NULL(line_str);
49         tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
50         while(tok_str == NULL || tok_str[0] == '#') {
51             line_str = strtok_r(NULL, line_tokens, &line_endptr);
52             CHECK_NULL(line_str);
53             tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
54         }
55 
56         line_str = strtok_r(NULL, line_tokens, &line_endptr);
57         CHECK_NULL(line_str);
58         tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
59         while(tok_str == NULL || tok_str[0] == '#') {
60             line_str = strtok_r(NULL, line_tokens, &line_endptr);
61             CHECK_NULL(line_str);
62             tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
63         }
64         intrinsic_param.poly_length = strtol(tok_str, NULL, 10);
65 
66         XCAM_FAIL_RETURN (
67             ERROR, intrinsic_param.poly_length <= XCAM_INTRINSIC_MAX_POLY_SIZE,
68             XCAM_RETURN_ERROR_PARAM,
69             "intrinsic poly length:%d is larger than max_size:%d.",
70             intrinsic_param.poly_length, XCAM_INTRINSIC_MAX_POLY_SIZE);
71 
72         for(uint32_t i = 0; i < intrinsic_param.poly_length; i++) {
73             tok_str = strtok_r(NULL, str_tokens, &tok_endptr);
74             CHECK_NULL(tok_str);
75             intrinsic_param.poly_coeff[i] = (strtof(tok_str, NULL));
76         }
77 
78         line_str = strtok_r(NULL, line_tokens, &line_endptr);
79         CHECK_NULL(line_str);
80         tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
81         while(tok_str == NULL || tok_str[0] == '#') {
82             line_str = strtok_r(NULL, line_tokens, &line_endptr);
83             CHECK_NULL(line_str);
84             tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
85         }
86         intrinsic_param.yc = strtof(tok_str, NULL);
87 
88         tok_str = strtok_r(NULL, str_tokens, &tok_endptr);
89         CHECK_NULL(tok_str);
90         intrinsic_param.xc = strtof(tok_str, NULL);
91 
92         line_str = strtok_r(NULL, line_tokens, &line_endptr);
93         CHECK_NULL(line_str);
94         tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
95         while(tok_str == NULL || tok_str[0] == '#') {
96             line_str = strtok_r(NULL, line_tokens, &line_endptr);
97             CHECK_NULL(line_str);
98             tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
99         }
100         intrinsic_param.c = strtof(tok_str, NULL);
101 
102         tok_str = strtok_r(NULL, str_tokens, &tok_endptr);
103         CHECK_NULL(tok_str);
104         intrinsic_param.d = strtof(tok_str, NULL);
105 
106         tok_str = strtok_r(NULL, str_tokens, &tok_endptr);
107         CHECK_NULL(tok_str);
108         intrinsic_param.e = strtof(tok_str, NULL);
109     } while(0);
110 
111     return XCAM_RETURN_NO_ERROR;
112 }
113 
114 XCamReturn
parse_extrinsic_param(char * file_body,ExtrinsicParameter & extrinsic_param)115 CalibrationParser::parse_extrinsic_param(char *file_body, ExtrinsicParameter &extrinsic_param)
116 {
117     char *line_str = NULL;
118     char *line_endptr = NULL;
119     char *tok_str = NULL;
120     char *tok_endptr = NULL;
121     static const char *line_tokens = "\r\n";
122     static const char *str_tokens = " \t";
123 
124     do {
125         line_str = strtok_r(file_body, line_tokens, &line_endptr);
126         CHECK_NULL(line_str);
127         tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
128         while(tok_str == NULL || tok_str[0] == '#') {
129             line_str = strtok_r(NULL, line_tokens, &line_endptr);
130             CHECK_NULL(line_str);
131             tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
132         }
133         extrinsic_param.trans_x = strtof(tok_str, NULL);
134 
135         line_str = strtok_r(NULL, line_tokens, &line_endptr);
136         CHECK_NULL(line_str);
137         tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
138         while(tok_str == NULL || tok_str[0] == '#') {
139             line_str = strtok_r(NULL, line_tokens, &line_endptr);
140             CHECK_NULL(line_str);
141             tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
142         }
143         extrinsic_param.trans_y = strtof(tok_str, NULL);
144 
145         line_str = strtok_r(NULL, line_tokens, &line_endptr);
146         CHECK_NULL(line_str);
147         tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
148         while(tok_str == NULL || tok_str[0] == '#') {
149             line_str = strtok_r(NULL, line_tokens, &line_endptr);
150             CHECK_NULL(line_str);
151             tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
152         }
153         extrinsic_param.trans_z = strtof(tok_str, NULL);
154 
155         line_str = strtok_r(NULL, line_tokens, &line_endptr);
156         CHECK_NULL(line_str);
157         tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
158         while(tok_str == NULL || tok_str[0] == '#') {
159             line_str = strtok_r(NULL, line_tokens, &line_endptr);
160             CHECK_NULL(line_str);
161             tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
162         }
163         extrinsic_param.roll = strtof(tok_str, NULL);
164 
165         line_str = strtok_r(NULL, line_tokens, &line_endptr);
166         CHECK_NULL(line_str);
167         tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
168         while(tok_str == NULL || tok_str[0] == '#') {
169             line_str = strtok_r(NULL, line_tokens, &line_endptr);
170             CHECK_NULL(line_str);
171             tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
172         }
173         extrinsic_param.pitch = strtof(tok_str, NULL);
174 
175         line_str = strtok_r(NULL, line_tokens, &line_endptr);
176         CHECK_NULL(line_str);
177         tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
178         while(tok_str == NULL || tok_str[0] == '#') {
179             line_str = strtok_r(NULL, line_tokens, &line_endptr);
180             CHECK_NULL(line_str);
181             tok_str = strtok_r(line_str, str_tokens, &tok_endptr);
182         }
183         extrinsic_param.yaw = strtof(tok_str, NULL);
184     } while(0);
185 
186     return XCAM_RETURN_NO_ERROR;
187 }
188 
189 XCamReturn
parse_intrinsic_file(const char * file_path,IntrinsicParameter & intrinsic_param)190 CalibrationParser::parse_intrinsic_file(const char *file_path, IntrinsicParameter &intrinsic_param)
191 {
192     XCAM_ASSERT (file_path);
193 
194     FileHandle file_reader;
195     XCamReturn ret = XCAM_RETURN_NO_ERROR;
196     std::vector<char> context;
197     size_t file_size = 0;
198 
199     XCAM_FAIL_RETURN (
200         WARNING, xcam_ret_is_ok (ret = file_reader.open (file_path, "r")), ret,
201         "open intrinsic file(%s) failed.", file_path);
202     XCAM_FAIL_RETURN (
203         WARNING, xcam_ret_is_ok (ret = file_reader.get_file_size (file_size)), ret,
204         "read intrinsic file(%s) failed to get file size.", file_path);
205     context.resize (file_size + 1);
206     XCAM_FAIL_RETURN (
207         WARNING, xcam_ret_is_ok (ret = file_reader.read_file (&context[0], file_size)), ret,
208         "read intrinsic file(%s) failed, file size:%d.", file_path, (int)file_size);
209     file_reader.close ();
210     context[file_size] = '\0';
211 
212     return parse_intrinsic_param (&context[0], intrinsic_param);
213 }
214 
215 XCamReturn
parse_extrinsic_file(const char * file_path,ExtrinsicParameter & extrinsic_param)216 CalibrationParser::parse_extrinsic_file(const char *file_path, ExtrinsicParameter &extrinsic_param)
217 {
218     XCAM_ASSERT (file_path);
219 
220     FileHandle file_reader;
221     XCamReturn ret = XCAM_RETURN_NO_ERROR;
222     std::vector<char> context;
223     size_t file_size = 0;
224 
225     XCAM_FAIL_RETURN (
226         WARNING, xcam_ret_is_ok (ret = file_reader.open (file_path, "r")), ret,
227         "open extrinsic file(%s) failed.", file_path);
228     XCAM_FAIL_RETURN (
229         WARNING, xcam_ret_is_ok (ret = file_reader.get_file_size (file_size)), ret,
230         "read extrinsic file(%s) failed to get file size.", file_path);
231     context.resize (file_size + 1);
232     XCAM_FAIL_RETURN (
233         WARNING, xcam_ret_is_ok (ret = file_reader.read_file (&context[0], file_size)), ret,
234         "read extrinsic file(%s) failed, file size:%d.", file_path, (int)file_size);
235     file_reader.close ();
236     context[file_size] = '\0';
237 
238     return parse_extrinsic_param (&context[0], extrinsic_param);
239 }
240 
241 }
242