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
photo_exif.h
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 photo_exif.h
20 * @brief Encode simulated-camera pose, optics, and uncertainty into EXIF.
21 * @details Metadata distinguishes commanded pose from observed simulator pose,
22 * and labels nominal/emulated camera settings rather than presenting them as
23 * physical measurements. This is important because a complete EXIF record is
24 * provenance, not proof of exposure synchronization or surveyed accuracy.
25 */
26#ifndef PANGEA_PHOTO_EXIF_H
27#define PANGEA_PHOTO_EXIF_H
28
29#include "exifite.h"
30#include "map_projection.h"
31
32#include "camera_optics.h"
33#include <algorithm>
34#include <array>
35#include <cerrno>
36#include <cmath>
37#include <cstdint>
38#include <cstring>
39#include <iomanip>
40#include <limits>
41#include <locale>
42#include <optional>
43#include <sstream>
44#include <stdexcept>
45#include <string>
46#include <system_error>
47
48namespace pangea_survey {
49/** @brief Photo location with AMSL camera and ground heights in metres. */
52 /** @brief Compute height above ground from the shared AMSL datum.
53 * @return Camera-minus-ground height in metres. */
54 double agl() const { return altitudeAmsl - groundAmsl; }
55};
56
57/**
58 * @brief Camera and timing provenance required for a simulated-photo EXIF record.
59 * @details Dimensions are pixels, timestamps are 100 ns monotonic counts, angles
60 * are degrees except `fovRadians`, and the optional map records projected metres.
61 */
63 double fovRadians = 0;
64 double commandedHeading = 0;
65 double pitch = 0, bank = 0, heading = 0;
67 long long observed100ns = 0;
69 double cropX = 0, cropY = 0, cropWidth = 0, cropHeight = 0;
70 bool narrowAngle = false;
71 bool piAiCamera = false;
74 bool fixedStartAltitude = false;
75 double startGroundMsl = 0;
76 long long frame100ns = 0;
77 std::optional<UtcDateTime> captureUtc;
79 long long utcRead100ns = 0;
80 std::optional<MapTile> map;
82 std::optional<unsigned long long> photoNumber;
83};
84
85/**
86 * @brief Build bounded EXIF payloads for simulator-generated source photos.
87 * @details The output buffer is owned by this object and is replaced by each
88 * call to @ref build. Callers must consume or copy the payload before building
89 * another record with the same instance.
90 */
91class PhotoExif {
92protected:
94 std::array<unsigned char, EXIF_MAX_PAYLOAD> payload_{};
95
96 /** @brief Convert a C EXIF status into the mapper's error path.
97 * @param result Zero on success, nonzero on failure.
98 * @param operation Context for the error.
99 * @throws std::runtime_error With the current errno diagnostic on failure. */
100 static void check(int result, const char* operation) {
101 if (result != 0)
102 throw std::runtime_error(std::string(operation) + ": " +
103 std::generic_category().message(errno));
104 }
105 /** @brief Allocate one field in the bounded EXIF document.
106 * @param directory Target EXIF directory.
107 * @param tag Numeric field identifier.
108 * @param format Element encoding.
109 * @param count Number of elements, not the byte count.
110 * @return Borrowed writable field storage.
111 * @throws std::runtime_error If the document cannot reserve the field. */
112 unsigned char* reserve(exif_directory directory, std::uint16_t tag,
113 exif_format format, std::uint32_t count) {
114 auto* value = exif_document_reserve(&document_, directory, tag, format, count);
115 if (!value) check(-1, "Reserve photo EXIF field");
116 return value;
117 }
118 /** @brief Encode a validated geographic coordinate as EXIF GPS rationals.
119 * @param degrees Signed decimal degrees.
120 * @param referenceTag Hemisphere tag ID.
121 * @param valueTag Degrees/minutes/seconds tag ID.
122 * @param positive Hemisphere for a nonnegative coordinate.
123 * @param negative Hemisphere for a negative coordinate.
124 * @throws std::runtime_error If either tag cannot be stored.
125 * @details Round the total once so overflow from the fractional second
126 * carries naturally into minutes and degrees. */
127 void coordinate(double degrees, std::uint16_t referenceTag, std::uint16_t valueTag,
128 char positive, char negative) {
129 const char reference[] = {degrees < 0 ? negative : positive, '\0'};
130 check(exif_document_ascii(&document_, EXIF_GPS, referenceTag, reference), "Set EXIF GPS hemisphere");
131 // Round once in integer microseconds, carrying 60-second/minute boundaries.
132 const auto total = static_cast<std::uint64_t>(std::llround(std::abs(degrees) * 3600 * 1000000));
133 auto* value = reserve(EXIF_GPS, valueTag, EXIF_RATIONAL, 3);
134 exif_encode_long(value, static_cast<std::uint32_t>(total / 3600000000ULL));
135 exif_encode_long(value + 4, 1);
136 exif_encode_long(value + 8, static_cast<std::uint32_t>(total / 60000000ULL % 60));
137 exif_encode_long(value + 12, 1);
138 exif_encode_long(value + 16, static_cast<std::uint32_t>(total % 60000000ULL));
139 exif_encode_long(value + 20, 1000000);
140 }
141 /** @brief Write simulator-observed UTC into consistent EXIF and GPS fields.
142 * @param camera Capture metadata with its optional UTC field engaged.
143 * @throws std::runtime_error If the time sample is unqualified or a tag cannot be written.
144 * @details Check frame/readback timing before publishing a wall-clock label.
145 * Derive both tag families from the same UTC sample. */
147 const auto& utc = *camera.captureUtc;
148 if (!captureUtcSampleValid(utc, camera.utcAbsoluteSeconds, camera.utcRead100ns, camera.frame100ns))
149 throw std::runtime_error("Invalid verified capture UTC metadata");
150 const auto date = utc.text.substr(0, 10);
151 std::string exifDate = date;
152 std::replace(exifDate.begin(), exifDate.end(), '-', ':');
153 const auto timestamp = exifDate + " " + utc.text.substr(11, 5) + ":00";
154 const auto milliseconds = static_cast<std::uint32_t>(
155 std::floor((camera.utcAbsoluteSeconds - utc.absoluteSeconds()) * 1000));
156 std::ostringstream subsecond;
157 subsecond.imbue(std::locale::classic());
158 subsecond << std::setfill('0') << std::setw(3) << milliseconds;
159 check(exif_document_ascii(&document_, EXIF_PRIMARY, 0x0132, timestamp.c_str()), "Set EXIF DateTime");
160 for (const auto tag : {0x9003, 0x9004})
161 check(exif_document_ascii(&document_, EXIF_CAMERA, static_cast<std::uint16_t>(tag), timestamp.c_str()),
162 "Set EXIF original/digitized time");
163 for (const auto tag : {0x9010, 0x9011, 0x9012})
164 check(exif_document_ascii(&document_, EXIF_CAMERA, static_cast<std::uint16_t>(tag), "+00:00"),
165 "Set EXIF UTC offset");
166 for (const auto tag : {0x9290, 0x9291, 0x9292})
167 check(exif_document_ascii(&document_, EXIF_CAMERA, static_cast<std::uint16_t>(tag), subsecond.str().c_str()),
168 "Set EXIF UTC subsecond");
169 check(exif_document_ascii(&document_, EXIF_GPS, 0x001d, exifDate.c_str()), "Set GPS date");
170 auto* time = reserve(EXIF_GPS, 0x0007, EXIF_RATIONAL, 3);
171 exif_encode_long(time, static_cast<std::uint32_t>(utc.hour));
172 exif_encode_long(time + 4, 1);
173 exif_encode_long(time + 8, static_cast<std::uint32_t>(utc.minute));
174 exif_encode_long(time + 12, 1);
175 exif_encode_long(time + 16, milliseconds);
176 exif_encode_long(time + 20, 1000);
177 }
178public:
179 /** @brief Borrowed serialized EXIF bytes held in this object's workspace. */
180 struct Payload {
181 const unsigned char* data;
182 std::size_t size;
183 };
184 /**
185 * @brief Construct EXIF from validated photo position and optional camera evidence.
186 * @param[in] position WGS-84 position with camera/ground AMSL heights in metres.
187 * @param[in] width Encoded image width in pixels.
188 * @param[in] height Encoded image height in pixels.
189 * @param[in] camera Optional pose, optics, UTC-readback, and map provenance.
190 * @return Serialized EXIF payload borrowed from this object.
191 * @throws std::runtime_error If coordinates, optics, timestamps, or payload size
192 * violate the supported metadata contract.
193 */
194 Payload build(const PhotoPosition& position, int width, int height,
195 const std::optional<PhotoCamera>& camera = {}) {
196 if (!std::isfinite(position.latitude) || std::abs(position.latitude) > 90 ||
197 !std::isfinite(position.longitude) || std::abs(position.longitude) > 180 ||
198 !std::isfinite(position.altitudeAmsl) || !std::isfinite(position.groundAmsl) ||
199 !std::isfinite(position.agl()) ||
200 std::abs(position.altitudeAmsl) >
201 static_cast<double>(std::numeric_limits<std::uint32_t>::max()) / 1000 ||
202 width < 1 || height < 1 || width > 16384 || height > 16384)
203 throw std::runtime_error("Invalid position, heights or dimensions for photo EXIF");
205 check(exif_document_ascii(&document_, EXIF_PRIMARY, EXIF_SOFTWARE, "Tessavia / pangea_survey"),
206 "Set EXIF software");
207 check(exif_document_ascii(&document_, EXIF_PRIMARY, 0x010f, "Tessavia"),
208 "Set virtual camera manufacturer");
209 check(exif_document_ascii(&document_, EXIF_PRIMARY, 0x0110, "MSFS 2024 add-on camera"),
210 "Set virtual camera model");
211 auto* version = reserve(EXIF_GPS, EXIF_GPS_VERSION, EXIF_BYTE, 4);
212 version[0] = 2;
213 version[1] = 3;
216 constexpr std::uint16_t gpsMapDatum = 0x0012;
217 check(exif_document_ascii(&document_, EXIF_GPS, gpsMapDatum, "WGS-84"), "Set EXIF GPS datum");
218 *reserve(EXIF_GPS, EXIF_GPS_ALTITUDE_REF, EXIF_BYTE, 1) = position.altitudeAmsl < 0 ? 1 : 0;
219 const auto altitudeMm = static_cast<std::uint32_t>(std::llround(std::abs(position.altitudeAmsl) * 1000));
221 "Set EXIF AMSL altitude");
223 static_cast<std::uint32_t>(width));
225 static_cast<std::uint32_t>(height));
226
227 std::ostringstream description, comment;
228 description.imbue(std::locale::classic());
229 comment.imbue(std::locale::classic());
230 description << std::fixed << std::setprecision(6)
231 << "Pangea simulated camera; AGL=" << position.agl()
232 << (camera && camera->fixedStartAltitude ? " m (unverified estimate); AMSL=" : " m; AMSL=")
233 << position.altitudeAmsl << " m";
234 comment << std::fixed << std::setprecision(10)
235 << "source=pangea_survey/MSFS2024; pose="
236 << (camera && camera->piAiCamera ? "simconnect_observed_camera" : "simconnect_commanded_camera")
237 << "; datum=WGS-84; latitude_deg=" << position.latitude
238 << "; longitude_deg=" << position.longitude << std::setprecision(6)
239 << "; agl_m=" << position.agl() << "; amsl_m=" << position.altitudeAmsl
240 << "; ground_amsl_m=" << position.groundAmsl;
241 if (camera && (camera->piAiCamera || camera->fixedStartAltitude)) {
242 if (!std::isfinite(camera->startGroundMsl))
243 throw std::runtime_error("Invalid initial ground reference in camera metadata");
244 comment << "; agl_verified=" << (camera->fixedStartAltitude ? "false" : "true")
245 << "; agl_basis=" << (camera->fixedStartAltitude ?
246 "unverified_cached_sdk_ground_estimate" : "current_terrain_reference");
247 if (camera->fixedStartAltitude)
248 comment << "; start_ground_amsl_m=" << camera->startGroundMsl
249 << "; reference_plane_height_m=" << position.altitudeAmsl - camera->startGroundMsl;
250 }
251 if (camera && camera->captureUtc) {
253 comment << "; capture_utc=" << camera->captureUtc->text
254 << "; utc_absolute_seconds=" << std::setprecision(17) << camera->utcAbsoluteSeconds
255 << "; utc_read_qpc_100ns=" << camera->utcRead100ns
256 << "; utc_hold_method=repeated_simconnect_clock_resets"
257 << "; utc_tolerance_seconds=1; utc_frame_synchronized=false"
258 << "; exif_time_basis=verified_simulator_utc_readback_not_exposure_clock"
259 << std::setprecision(6);
260 }
261 if (camera) {
262 const auto& c = *camera;
263 c.exifSettings.validate();
264 const auto shutterUs = static_cast<std::uint32_t>(std::llround(c.exifSettings.shutterSeconds * 1000000));
266 "Set nominal simulated exposure time");
268 if (!c.exifSettings.hardwareIdentifier.empty())
270 c.exifSettings.hardwareIdentifier.c_str()),
271 "Set INI camera hardware identifier");
272 comment << "; camera.exposure_basis=nominal_emulation_not_measured"
273 << "; camera.shutter_speed_seconds=" << shutterUs / 1000000.0
274 << "; camera.iso=" << c.exifSettings.iso
275 << "; camera.hardware_identifier="
276 << (c.exifSettings.hardwareIdentifier.empty() ? "undefined" : c.exifSettings.hardwareIdentifier);
277 if (!c.exifSettings.hardwareIdentifier.empty())
278 comment << "; camera.hardware_identifier_basis=ini_setting";
279 if (std::abs(position.groundAmsl) > static_cast<double>(std::numeric_limits<std::int32_t>::max()) / 256)
280 throw std::runtime_error("Ground altitude exceeds Paparazzi EXIF fixed-point range");
281 const double fields[] = {c.fovRadians, c.commandedHeading, c.pitch, c.bank, c.heading,
282 c.observedLatitude, c.observedLongitude, c.observedAltitude,
283 c.cropX, c.cropY, c.cropWidth, c.cropHeight, c.imageRotationDegrees, c.rotationBoundDegrees};
284 if (!std::all_of(std::begin(fields), std::end(fields), [](double value) { return std::isfinite(value); }) ||
285 c.fovRadians <= 0 || c.fovRadians >= 3.141592653589793 ||
286 c.sourceWidth < 1 || c.sourceHeight < 1 || c.cropX < 0 || c.cropY < 0 ||
287 c.cropWidth <= 0 || c.cropHeight <= 0 ||
288 c.cropX + c.cropWidth > c.sourceWidth + 1e-6 ||
289 c.cropY + c.cropHeight > c.sourceHeight + 1e-6 ||
290 std::abs(c.observedLatitude) > 90 || std::abs(c.observedLongitude) > 180 ||
291 c.observed100ns <= 0)
292 throw std::runtime_error("Invalid camera lens/attitude metadata");
293 const double scale = c.cropWidth / width;
294 if (std::abs(c.cropHeight / height - scale) > 1e-6)
295 throw std::runtime_error("Camera intrinsics require proportional image framing");
296 const double radiansToDegrees = 180 / 3.14159265358979323846;
297 const double sourceFocal = c.sourceHeight / (2 * std::tan(c.fovRadians / 2));
298 const double focal = sourceFocal / scale;
299 if (c.piAiCamera && (width != PiAiOptics::width || height != PiAiOptics::height ||
300 std::abs(focal - PiAiOptics::focalPixels) > 0.01 || c.frame100ns <= 0))
301 throw std::runtime_error("Pi AI camera frame does not match its nominal optical profile");
302 const double equivalent35mm = 36 * focal / width;
303 if (equivalent35mm > 65535)
304 throw std::runtime_error("Virtual lens exceeds EXIF 35mm-equivalent range");
305 check(exif_document_ascii(&document_, EXIF_PRIMARY, 0x0110, c.piAiCamera ?
306 "Raspberry Pi AI Camera (nominal emulation)" : "MSFS 2024 add-on camera"),
307 "Set virtual camera model");
309 c.piAiCamera ? "Stock 4.74 mm f/1.79 (estimated lens effects v1)" :
310 "SimConnect rectilinear virtual lens"),
311 "Set virtual lens model");
312 if (c.piAiCamera) {
313 check(exif_document_ascii(&document_, EXIF_PRIMARY, 0x010f, "Raspberry Pi (emulated)"),
314 "Set simulated camera manufacturer");
316 "Set nominal physical lens focal length");
318 "Set nominal lens aperture");
319 auto* lens = reserve(EXIF_CAMERA, 0xa432, EXIF_RATIONAL, 4);
320 for (int i = 0; i < 4; ++i) {
321 exif_encode_long(lens + i * 8, static_cast<std::uint32_t>(std::llround(
322 (i < 2 ? PiAiOptics::focalMm : PiAiOptics::aperture) * 100)));
323 exif_encode_long(lens + i * 8 + 4, 100);
324 }
326 static_cast<std::uint32_t>(width * 100000), 62868), "Set nominal sensor X resolution");
328 static_cast<std::uint32_t>(height * 100000), 47120), "Set nominal sensor Y resolution");
330 }
332 static_cast<std::uint16_t>(std::llround(equivalent35mm)));
334 "Set image direction reference");
335 const double direction = std::fmod(std::fmod(c.commandedHeading + c.imageRotationDegrees, 360.0) + 360.0, 360.0);
336 const double imageHeading = direction / radiansToDegrees;
338 static_cast<std::uint32_t>(std::llround(direction * 1000000)), 1000000),
339 "Set image grid-up direction");
340 check(exif_document_ascii(&document_, EXIF_GPS, EXIF_GPS_SPEED_REF, "K"), "Set GPS speed units");
342 "Set stationary virtual capture speed");
343 comment << "; camera.projection=perspective; camera.mode="
344 << (c.piAiCamera ? "raspberry_pi_ai_nominal" : (c.narrowAngle ? "narrow_angle_mapping" : "normal"))
345 << "; camera.lens_model=" << (c.piAiCamera ? "brown_conrady_estimated_v1" : "ideal_rectilinear")
346 << "; camera.physical_sensor_mm="
347 << (c.piAiCamera ? "6.2868,4.712" : "undefined")
348 << "; camera.fov_axis=vertical; camera.sdk_fov_deg=" << c.fovRadians * radiansToDegrees
349 << "; camera.image_fov_x_deg=" << 2 * std::atan(width / (2 * focal)) * radiansToDegrees
350 << "; camera.image_fov_y_deg=" << 2 * std::atan(height / (2 * focal)) * radiansToDegrees
351 << "; camera.fx_px=" << focal << "; camera.fy_px=" << focal
352 << "; camera.cx_px=" << (c.sourceWidth / 2.0 - c.cropX) / scale - 0.5
353 << "; camera.cy_px=" << (c.sourceHeight / 2.0 - c.cropY) / scale - 0.5
354 << "; camera.equivalent_35mm_focal_mm=" << equivalent35mm
355 << "; camera.source_width=" << c.sourceWidth << "; camera.source_height=" << c.sourceHeight
356 << "; camera.crop_x=" << c.cropX << "; camera.crop_y=" << c.cropY
357 << "; camera.crop_width=" << c.cropWidth << "; camera.crop_height=" << c.cropHeight
358 << "; camera.image_rotation_deg=" << c.imageRotationDegrees
359 << "; camera.framing_rotation_bound_deg=" << c.rotationBoundDegrees
360 << "; camera.intrinsics_basis=vertical_FOV_pinhole_model"
361 << "; camera.attitude_convention=SDK_degrees_positive_pitch_down"
362 << "; camera.commanded_pitch_deg=90.000000; camera.commanded_bank_deg=0.000000"
363 << "; camera.commanded_heading_deg=" << c.commandedHeading
364 << "; camera.commanded_image_to_enu_rotation="
365 << std::cos(imageHeading) << ',' << -std::sin(imageHeading) << ",0,"
366 << -std::sin(imageHeading) << ',' << -std::cos(imageHeading) << ",0,0,0,-1"
367 << "; camera.observed_pitch_deg=" << c.pitch << "; camera.observed_bank_deg=" << c.bank
368 << "; camera.observed_heading_deg=" << c.heading << std::setprecision(10)
369 << "; camera.observed_latitude_deg=" << c.observedLatitude
370 << "; camera.observed_longitude_deg=" << c.observedLongitude << std::setprecision(6)
371 << "; camera.observed_amsl_m=" << c.observedAltitude
372 << "; camera.pose_received_qpc_100ns=" << c.observed100ns
373 << "; camera.pose_frame_synchronized=false";
374 if (c.piAiCamera)
375 comment << "; camera.calibrated=false; camera.distortion_basis=engineering_estimate_not_measured"
376 << "; camera.distortion_coefficients=" << c.lens.k1 << ',' << c.lens.k2 << ",0,0,0"
377 << "; camera.distortion_coefficient_order=k1,k2,p1,p2,k3"
378 << "; camera.chromatic_aberration.model=lateral_channel_magnification"
379 << "; camera.chromatic_aberration.basis=engineering_estimate_not_measured"
380 << "; camera.chromatic_aberration.reference_channel=green"
381 << "; camera.chromatic_aberration.channel_focal_scales_bgr=" << c.lens.blueScale << ','
382 << PiAiOptics::greenScale << ',' << c.lens.redScale
383 << "; camera.chromatic_aberration.longitudinal_simulated=false"
384 << "; camera.render_overscan_factor=" << PiAiOptics::overscan
385 << "; camera.fov_basis=nominal_undistorted_intrinsics"
386 << "; camera.sensor_model=Sony IMX500; camera.sensor_mode=2028x1520_2x2_binned"
387 << "; camera.physical_focal_mm=4.74; camera.aperture_f_number=1.79"
388 << "; camera.render_frame_qpc_100ns=" << c.frame100ns;
389 const auto yawBfp = std::llround(direction / radiansToDegrees * 4096);
390 if (c.photoNumber && *c.photoNumber == 0)
391 throw std::runtime_error("Photo number must be positive");
392 description << "; ";
393 if (c.photoNumber) description << "nr=" << *c.photoNumber << "; ";
394 description << "lat_e7deg=" << std::llround(position.latitude * 10000000)
395 << "; lon_e7deg=" << std::llround(position.longitude * 10000000)
396 << "; alt_mm=" << std::llround(position.altitudeAmsl * 1000)
397 << "; phi_bfp=0; theta_bfp=0; psi_bfp=" << yawBfp
398 << "; vground_bfp=0; course_bfp=undefined; groundalt_bfp="
399 << std::llround(position.groundAmsl * 256)
400 << "; groundalt_verified=" << (c.fixedStartAltitude ? "false" : "true")
401 << "; lat_deg=" << std::setprecision(10) << position.latitude
402 << "; lon_deg=" << position.longitude << std::setprecision(6)
403 << "; alt_m=" << position.altitudeAmsl
404 << "; roll_deg=0; pitch_deg=0; yaw_deg=" << direction
405 << "; speed_m_s=0; course_deg=undefined; groundalt_m=" << position.groundAmsl
406 << (c.fixedStartAltitude ? " (unverified cached estimate)" : "")
407 << "; attitude_source=simulated_level_stationary_body; camera_pitch_deg=90";
408 comment << "; autopilot.schema=pangea_fake_airframe_v1"
409 << "; autopilot.source=simulated_stationary_capture"
410 << "; autopilot.latitude_e7=" << std::llround(position.latitude * 10000000)
411 << "; autopilot.longitude_e7=" << std::llround(position.longitude * 10000000)
412 << "; autopilot.altitude_amsl_m=" << position.altitudeAmsl
413 << "; autopilot.altitude_agl_m=";
414 if (c.fixedStartAltitude) comment << "undefined";
415 else comment << position.agl();
416 comment << "; autopilot.roll_deg=0; autopilot.pitch_deg=0; autopilot.yaw_deg=" << direction
417 << "; autopilot.vground_mps=0; autopilot.heading_deg=" << direction
418 << "; autopilot.course_deg=undefined; autopilot.course_defined=false"
419 << "; autopilot.attitude_convention=level_body_downward_camera_mount,true_north_clockwise"
420 << "; angle_bfp_scale=4096; speed_bfp_scale=524288; position_bfp_scale=256";
421 if (c.map) {
422 const auto& m = *c.map;
423 if (!std::isfinite(m.east) || !std::isfinite(m.north) ||
424 !std::isfinite(m.pixelSize) || m.pixelSize <= 0 || !std::isfinite(m.heading) ||
425 !((m.epsg >= 32601 && m.epsg <= 32660) || (m.epsg >= 32701 && m.epsg <= 32760)))
426 throw std::runtime_error("Invalid map georeferencing metadata");
427 comment << "; map.crs=EPSG:" << m.epsg
428 << "; map.model=narrow_angle_reference_plane_not_true_orthophoto"
429 << "; map.center_easting_m=" << m.east << "; map.center_northing_m=" << m.north
430 << "; map.pixel_size_m=" << m.pixelSize << "; map.grid_up_heading_deg=" << m.heading
431 << "; map.row_direction=north_to_south";
432 }
433 }
434 const auto text = description.str() + "; " + comment.str();
436 "Set EXIF photo description");
437 const unsigned char prefix[] = {'A', 'S', 'C', 'I', 'I', 0, 0, 0};
438 if (text.size() > EXIF_MAX_PAYLOAD - sizeof(prefix) - 1)
439 throw std::runtime_error("Photo provenance exceeds EXIF capacity");
441 static_cast<std::uint32_t>(8 + text.size() + 1));
442 for (std::size_t offset = 0; offset < sizeof(prefix); ++offset) bytes[offset] = prefix[offset];
443 for (std::size_t offset = 0; offset < text.size(); ++offset)
444 bytes[sizeof(prefix) + offset] = static_cast<unsigned char>(text[offset]);
445 bytes[sizeof(prefix) + text.size()] = 0;
446 std::size_t size = 0;
447 check(exif_serialize(&document_, payload_.data(), payload_.size(), &size), "Serialize photo EXIF");
448 return {payload_.data(), size};
449 }
450 /**
451 * @brief Escape a string as a JSON string literal.
452 * @param[in] text UTF-8 byte string without control characters.
453 * @return Quoted JSON string value.
454 * @throws std::runtime_error If the input contains a control character.
455 */
456 static std::string jsonString(const std::string& text) {
457 std::string escaped = "\"";
458 for (const unsigned char ch : text) {
459 if (ch < 32) throw std::runtime_error("Control character in map provenance");
460 if (ch == '\\' || ch == '"') escaped += '\\';
461 escaped += static_cast<char>(ch);
462 }
463 return escaped + '"';
464 }
465};
466}
467#endif
Optical profiles and EXIF settings for simulated survey cameras.
size_t size
Definition chdk_pipe.c:143
uint8_t bytes[CHDK_CONTROL_REPLY_SIZE]
Definition chdk_pipe.c:144
Build bounded EXIF payloads for simulator-generated source photos.
Definition photo_exif.h:91
Payload build(const PhotoPosition &position, int width, int height, const std::optional< PhotoCamera > &camera={})
Construct EXIF from validated photo position and optional camera evidence.
Definition photo_exif.h:194
void captureTime(const PhotoCamera &camera)
Write simulator-observed UTC into consistent EXIF and GPS fields.
Definition photo_exif.h:146
std::array< unsigned char, EXIF_MAX_PAYLOAD > payload_
Definition photo_exif.h:94
exif_document document_
Definition photo_exif.h:93
static std::string jsonString(const std::string &text)
Escape a string as a JSON string literal.
Definition photo_exif.h:456
void coordinate(double degrees, std::uint16_t referenceTag, std::uint16_t valueTag, char positive, char negative)
Encode a validated geographic coordinate as EXIF GPS rationals.
Definition photo_exif.h:127
unsigned char * reserve(exif_directory directory, std::uint16_t tag, exif_format format, std::uint32_t count)
Allocate one field in the bounded EXIF document.
Definition photo_exif.h:112
static void check(int result, const char *operation)
Convert a C EXIF status into the mapper's error path.
Definition photo_exif.h:100
static uint64_t milliseconds(void)
Read elapsed-time evidence unaffected by changes to civil time.
Bounded C11 EXIF metadata and JPEG streaming interfaces.
int exif_serialize(const struct exif_document *document, unsigned char *payload, size_t capacity, size_t *size)
Serialize a document into one complete EXIF APP1 payload.
Definition exifite.c:736
void exif_document_init(struct exif_document *document)
Start a fresh upright sRGB/Y-Cb-Cr document with EXIF 2.32 defaults.
Definition exifite.c:412
unsigned char * exif_document_reserve(struct exif_document *document, enum exif_directory directory, uint16_t tag, enum exif_format format, uint32_t count)
Create or replace a field with zeroed, bounded value storage.
Definition exifite.c:508
void exif_encode_long(unsigned char *data, uint32_t value)
Encode a 32-bit value in the writer's little-endian representation.
Definition exifite.c:106
exif_format
TIFF field-type numbers used by Sensoraia's metadata.
Definition exifite.h:64
exif_directory
Directory identities, not on-disk tag numbers or offsets.
Definition exifite.h:59
int exif_document_rational(struct exif_document *document, enum exif_directory directory, uint16_t tag, uint32_t numerator, uint32_t denominator)
Store an unsigned measurement as an exact numerator/denominator pair.
Definition exifite.c:572
#define EXIF_MAX_PAYLOAD
APP1 payload ceiling: the JPEG length includes its own two bytes.
Definition exifite.h:51
int exif_document_ascii(struct exif_document *document, enum exif_directory directory, uint16_t tag, const char *text)
Replace an ASCII field, including its terminating NUL byte.
Definition exifite.c:543
void exif_encode_short(unsigned char *data, uint16_t value)
Encode a 16-bit value in the writer's little-endian representation.
Definition exifite.c:99
@ EXIF_BYTE
Definition exifite.h:65
@ EXIF_SHORT
Definition exifite.h:67
@ EXIF_LONG
Definition exifite.h:68
@ EXIF_UNDEFINED
Definition exifite.h:70
@ EXIF_RATIONAL
Definition exifite.h:69
@ EXIF_CAMERA
Definition exifite.h:59
@ EXIF_PRIMARY
Definition exifite.h:59
@ EXIF_GPS
Definition exifite.h:59
@ EXIF_GPS_SPEED_REF
Definition exifite.h:108
@ EXIF_GPS_VERSION
Definition exifite.h:101
@ EXIF_FOCAL_LENGTH
Definition exifite.h:92
@ EXIF_GPS_ALTITUDE_REF
Definition exifite.h:106
@ EXIF_GPS_DIRECTION_REF
Definition exifite.h:112
@ EXIF_GPS_LATITUDE
Definition exifite.h:103
@ EXIF_BODY_SERIAL_NUMBER
Definition exifite.h:98
@ EXIF_GPS_LATITUDE_REF
Definition exifite.h:102
@ EXIF_GPS_SPEED
Definition exifite.h:109
@ EXIF_EXPOSURE_TIME
Definition exifite.h:86
@ EXIF_PIXEL_Y_DIMENSION
Definition exifite.h:97
@ EXIF_GPS_LONGITUDE
Definition exifite.h:105
@ EXIF_LENS_MODEL
Definition exifite.h:99
@ EXIF_GPS_ALTITUDE
Definition exifite.h:107
@ EXIF_PIXEL_X_DIMENSION
Definition exifite.h:96
@ EXIF_IMAGE_DESCRIPTION
Definition exifite.h:80
@ EXIF_SOFTWARE
Definition exifite.h:85
@ EXIF_USER_COMMENT
Definition exifite.h:93
@ EXIF_ISO_SPEED
Definition exifite.h:88
@ EXIF_GPS_LONGITUDE_REF
Definition exifite.h:104
@ EXIF_GPS_DIRECTION
Definition exifite.h:113
Convert between WGS-84 coordinates and one local UTM grid.
bool captureUtcSampleValid(const UtcDateTime &target, double actual, long long read100ns, long long frame100ns)
Validate that a frame's UTC readback is recent and in the locked second.
Definition utc_time.h:202
static size_t count
Definition pose_log.c:79
Caller-owned bounded writer with no heap allocation.
Definition exifite.h:156
User-supplied exposure labels written to simulated-photo EXIF.
Engineering lens-distortion and lateral colour-scale parameters.
Camera and timing provenance required for a simulated-photo EXIF record.
Definition photo_exif.h:62
std::optional< MapTile > map
Definition photo_exif.h:80
std::optional< unsigned long long > photoNumber
Definition photo_exif.h:82
CameraExifSettings exifSettings
Definition photo_exif.h:73
std::optional< UtcDateTime > captureUtc
Definition photo_exif.h:77
Borrowed serialized EXIF bytes held in this object's workspace.
Definition photo_exif.h:180
const unsigned char * data
Definition photo_exif.h:181
Photo location with AMSL camera and ground heights in metres.
Definition photo_exif.h:50
double agl() const
Compute height above ground from the shared AMSL datum.
Definition photo_exif.h:54
static constexpr double overscan
static constexpr double focalPixels
static constexpr double aperture
static constexpr int height
static constexpr double greenScale
static constexpr double focalMm
static constexpr int width
static struct camera_backend camera
Definition usher.c:521
struct usher_control_result result
Definition usher.c:377