Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
camera_profile.c
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19 * @file camera_profile.c
20 * @brief Parse the bounded, flat PANGEA camera calibration format.
21 * @details Require all fields, finite geometry values, and a stable regular-file
22 * read before publishing the parsed record. A valid profile can still have an
23 * unknown USHER camera ID; the consumer checks identity and geometry for its use.
24 */
25#define _POSIX_C_SOURCE 200809L
26#include "camera_profile.h"
27
28#include <ctype.h>
29#include <errno.h>
30#include <fcntl.h>
31#include <math.h>
32#include <stdint.h>
33#include <stdlib.h>
34#include <string.h>
35#include <sys/stat.h>
36#include <unistd.h>
37
38#define CAMERA_PROFILE_FIELD_COUNT 11
39
40/** @brief Trim surrounding whitespace in a mutable field.
41 * @param[in,out] text Null-terminated input.
42 * @return Borrowed pointer to the start of the trimmed field.
43 * @details Advance past leading whitespace and terminate in place to keep
44 * parsing within the original file buffer. */
45static char *trim(char *text)
46{
47 while (isspace((unsigned char)*text)) ++text;
48 char *end = text + strlen(text);
49 while (end > text && isspace((unsigned char)end[-1])) --end;
50 *end = '\0';
51 return text;
52}
53
54/** @brief Parse an exact unsigned decimal field.
55 * @param text Null-terminated decimal digits without sign or suffix.
56 * @param[out] number Result written only on success.
57 * @return True for a nonempty value representable as uint64_t.
58 * @details Check headroom before multiplying so an overflowing value
59 * cannot wrap into a plausible raster dimension. */
60static bool parse_uint(const char *text, uint64_t *number)
61{
62 if (!text || !*text || !number) return false;
63 uint64_t value = 0;
64 for (const unsigned char *digit = (const unsigned char *)text; *digit; ++digit) {
65 if (*digit < '0' || *digit > '9') return false;
66 const unsigned int next = *digit - '0';
67 if (value > (UINT64_MAX - next) / 10) return false;
68 value = value * 10 + next;
69 }
70 *number = value;
71 return true;
72}
73
74/** @brief Parse a finite camera geometry value.
75 * @param text Null-terminated numeric field.
76 * @param[out] number Result written only on success.
77 * @return True if the full field converted without a range error.
78 * @details Reject non-finite values before they can propagate into projection. */
79static bool parse_real(const char *text, double *number)
80{
81 if (!text || !*text || !number) return false;
82 errno = 0;
83 char *end = NULL;
84 const double value = strtod(text, &end);
85 if (errno == ERANGE || end == text || *end || !isfinite(value)) return false;
86 *number = value;
87 return true;
88}
89
90/** @brief Read one bounded, text-only snapshot of a profile.
91 * @param filename Path to the profile.
92 * @param[out] buffer Writable storage for the text and terminator.
93 * @param capacity Buffer size, greater than the profile size limit.
94 * @param[out] length Bytes of text before the terminator; use only on success.
95 * @return True for a stable, nonempty regular-file read and successful close.
96 * @details Open nonblocking so a non-regular input does not stall the owner.
97 * Compare size and modification time after reading to detect ordinary
98 * concurrent changes; this is not a content integrity check. */
99static bool read_file(const char *filename, char *buffer, size_t capacity,
100 size_t *length)
101{
102 if (!filename || !*filename || !buffer || !length ||
103 capacity <= CAMERA_PROFILE_MAX_BYTES) return false;
104 const int descriptor = open(filename, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
105 if (descriptor < 0) return false;
106 struct stat before, after;
107 size_t used = 0;
108 bool valid = false;
109 if (fstat(descriptor, &before) || !S_ISREG(before.st_mode) ||
110 before.st_size <= 0 || (uint64_t)before.st_size > CAMERA_PROFILE_MAX_BYTES)
111 goto done;
112 while (used < capacity) {
113 const ssize_t count = read(descriptor, buffer + used, capacity - used);
114 if (count < 0 && errno == EINTR) continue;
115 if (count < 0) goto done;
116 if (count == 0) break;
117 used += (size_t)count;
118 }
119 if (!used || used > CAMERA_PROFILE_MAX_BYTES || used != (size_t)before.st_size ||
120 fstat(descriptor, &after) || after.st_size != before.st_size ||
121 after.st_mtim.tv_sec != before.st_mtim.tv_sec ||
122 after.st_mtim.tv_nsec != before.st_mtim.tv_nsec ||
123 memchr(buffer, '\0', used)) goto done;
124 buffer[used] = '\0';
125 *length = used;
126 valid = true;
127done:
128 if (close(descriptor)) valid = false;
129 return valid;
130}
131
132bool usher_camera_profile_read(const char *filename,
134{
135 static const char *const names[CAMERA_PROFILE_FIELD_COUNT] = {
136 "schema", "filename_prefix", "width", "height", "fx", "fy", "cx", "cy",
137 "mount_roll_deg", "mount_pitch_deg", "mount_yaw_deg"
138 };
139 char buffer[CAMERA_PROFILE_MAX_BYTES + 1];
140 size_t length = 0;
141 if (!profile || !read_file(filename, buffer, sizeof(buffer), &length)) return false;
142
143 uint32_t seen = 0;
144 char prefix[65] = {0};
145 uint64_t width = 0, height = 0;
146 double values[CAMERA_PROFILE_FIELD_COUNT] = {0};
147 char *line = buffer;
148 char *limit = buffer + length;
149 while (line < limit) {
150 char *next = memchr(line, '\n', (size_t)(limit - line));
151 if (next) *next++ = '\0';
152 else next = limit;
153 char *entry = trim(line);
154 line = next;
155 if (!*entry || *entry == '#') continue;
156 char *separator = strchr(entry, '=');
157 if (!separator) return false;
158 *separator++ = '\0';
159 const char *key = trim(entry);
160 const char *value = trim(separator);
161 size_t field = 0;
162 while (field < CAMERA_PROFILE_FIELD_COUNT && strcmp(key, names[field]) != 0)
163 ++field;
164 if (field == CAMERA_PROFILE_FIELD_COUNT || (seen & (UINT32_C(1) << field)))
165 return false;
166 seen |= UINT32_C(1) << field;
167 if (field == 0) {
168 if (strcmp(value, "PANGEA_CAMERA_V1") != 0) return false;
169 } else if (field == 1) {
170 const size_t prefix_length = strlen(value);
171 if (prefix_length == 0 || prefix_length > 64) return false;
172 for (size_t index = 0; index < prefix_length; ++index) {
173 const unsigned char character = (unsigned char)value[index];
174 if (!((character >= 'A' && character <= 'Z') ||
175 (character >= 'a' && character <= 'z') ||
176 (character >= '0' && character <= '9') || character == '_' ||
177 character == '-')) return false;
178 }
179 memcpy(prefix, value, prefix_length + 1);
180 } else if (field == 2 || field == 3) {
181 uint64_t parsed;
182 if (!parse_uint(value, &parsed) || parsed < 2 || parsed > 4096)
183 return false;
184 if (field == 2) width = parsed;
185 else height = parsed;
186 } else if (!parse_real(value, &values[field])) {
187 return false;
188 }
189 }
190
191 const uint32_t required = (UINT32_C(1) << CAMERA_PROFILE_FIELD_COUNT) - 1;
192 const unsigned camera_id = prefix[1] != '\0' ? 0 :
193 prefix[0] == 'c' ? 1 : prefix[0] == 'a' ? 2 :
194 prefix[0] == 'l' ? 3 : 0;
195 if (seen != required || !prefix[0] || !width || !height) return false;
197 .calibration = {
198 .width = (unsigned)width, .height = (unsigned)height,
199 .fx = values[4], .fy = values[5], .cx = values[6], .cy = values[7],
200 .mount_roll_deg = values[8], .mount_pitch_deg = values[9], .mount_yaw_deg = values[10]
201 },
202 .camera_id = camera_id,
203 .filename_prefix = {0}
204 };
205 memcpy(result.filename_prefix, prefix, sizeof(prefix));
206 *profile = result;
207 return true;
208}
static bool parse_uint(const char *text, uint64_t *number)
Parse an exact unsigned decimal field.
#define CAMERA_PROFILE_FIELD_COUNT
static bool read_file(const char *filename, char *buffer, size_t capacity, size_t *length)
Read one bounded, text-only snapshot of a profile.
bool usher_camera_profile_read(const char *filename, struct usher_camera_profile *profile)
Read a bounded PANGEA_CAMERA_V1 key=value calibration file.
static bool parse_real(const char *text, double *number)
Parse a finite camera geometry value.
static char * trim(char *text)
Trim surrounding whitespace in a mutable field.
Public C API for reading PANGEA camera calibration profiles.
#define CAMERA_PROFILE_MAX_BYTES
static unsigned capacity[COVERAGE_CAMERAS]
int descriptor
Definition chdk_pipe.c:142
void(* close)(struct usbfs_handle *)
Definition chdkbridge.c:57
int(* open)(const struct usbfs_device *, struct usbfs_handle *)
Definition chdkbridge.c:56
static struct usher_camera_profile profile
static size_t count
Definition pose_log.c:79
One measured camera calibration loaded from the flat PANGEA profile format.
bool valid
Definition usher.c:537
struct usher_control_result result
Definition usher.c:377