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
lwir_analysis.c File Reference

Serial saved-image analysis adapter, not part of the camera driver. More...

#include "lwir_analysis.h"
#include "lwir_detection.h"
#include "lwir_geolocation.h"
#include "lwir_container.h"
#include "image_exif.h"
#include "lwir_frame.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
Include dependency graph for lwir_analysis.c:

Go to the source code of this file.

Data Structures

struct  pose_fields
 Intermediate pose values parsed from the ImageDescription key=value list. More...

Functions

static uint16_t read_u16_le (const uint8_t *bytes)
 Decode one unsigned 16-bit little-endian value.
static uint16_t temperature_at (size_t pixel)
 Raw Kelvin*64 code of one pixel in the static temperature plane.
static bool parse_description_number (const char *value, size_t length, double *number)
 Parse a finite decimal number from a non-NUL-terminated slice.
static bool read_pose (const char *filename, struct lwir_camera_pose *pose)
 Read the observation pose recorded in a saved image's EXIF ImageDescription.
static bool append_record (char *record, size_t capacity, size_t *used, const char *format,...)
 Append printf-formatted text to a fixed record buffer.
static bool set_hotspot_comment (const char *information)
 Store the analysis record in the in-memory EXIF document's UserComment.
static int publish_hotspot_comment (const char *filename, const char *information)
 Rewrite a JPEG's EXIF with the analysis record, keeping image data intact.
int lwir_geolocate_image (const char *filename, const char *calibration_path)
 Detect, optionally geolocate and record hotspots for one saved JPEG.

Variables

static struct exif_document geolocation_exif_document
static unsigned char geolocation_exif_payload [EXIF_MAX_PAYLOAD]
static unsigned char geolocation_description [8192]
static uint8_t temperature_plane [LWIR_MAX_WIDTH *LWIR_MAX_HEIGHT *2]
static struct lwir_detection_workspace detection_workspace

Detailed Description

Serial saved-image analysis adapter, not part of the camera driver.

Built only when LWIR_ENABLE_ANALYSIS=1. Works on an already saved LWIR JPEG: extracts the embedded exact Kelvin*64 temperature plane, detects hotspots, reads the observation pose from EXIF ImageDescription, optionally projects each hotspot to the ground, and writes a text record into EXIF UserComment. The entropy-coded display image is not decoded or recompressed.

Memory: large buffers are file-scope statics (no heap, no large stack frames apart from the 8 KiB record and EXIF payload copy in read_pose()), which is why the adapter is strictly serial.

Definition in file lwir_analysis.c.

Function Documentation

◆ append_record()

bool append_record ( char * record,
size_t capacity,
size_t * used,
const char * format,
... )
static

Append printf-formatted text to a fixed record buffer.

Fails instead of truncating, so a published record is never silently cut in the middle of a key/value pair.

Parameters
recordDestination buffer, NUL-terminated after each successful append.
capacityTotal size of record in bytes.
[in,out]usedBytes already used (excluding the terminator); advanced on success.
formatprintf format string.
...Format arguments.
Returns
True on success; false on encoding error or insufficient space.

Definition at line 247 of file lwir_analysis.c.

◆ lwir_geolocate_image()

int lwir_geolocate_image ( const char * filename,
const char * calibration_path )

Detect, optionally geolocate and record hotspots for one saved JPEG.

Annotate a saved thermal JPEG without recompressing its display pixels.

See lwir_analysis.h for the contract. Order of evaluation:

  1. Read the embedded temperature plane: invalid -> status=invalid_native_frame; absent -> status=no_temperature_plane (both published, return value from publishing).
  2. Read pose and calibration (each may be missing without aborting).
  3. Detect hotspots, then derive a single status with precedence missing pose > missing calibration > invalid calibration > unverified calibration, finally overridden by no_hotspots.
  4. For each hotspot, report the rounded pixel, its nearest-pixel temperature and the subpixel projection input; project only when status is estimated.
    Returns
    0 when a record was published, -1 otherwise.

Definition at line 395 of file lwir_analysis.c.

◆ parse_description_number()

bool parse_description_number ( const char * value,
size_t length,
double * number )
static

Parse a finite decimal number from a non-NUL-terminated slice.

Trims spaces/tabs, copies the slice into a bounded local buffer so strtod() has a terminator (EXIF text is not guaranteed to be NUL-terminated at the slice end), and requires strtod() to consume every remaining character.

Parameters
valueStart of the value slice (may be unterminated).
lengthSlice length in bytes.
[out]numberWritten only on success.
Returns
True for a complete finite number; false for empty, overlong (>= 96 bytes after trimming), partial, out-of-range (ERANGE), NaN or infinite input.
Note
Locale-dependent strtod(); presumably the process uses the "C" locale.

Definition at line 110 of file lwir_analysis.c.

◆ publish_hotspot_comment()

int publish_hotspot_comment ( const char * filename,
const char * information )
static

Rewrite a JPEG's EXIF with the analysis record, keeping image data intact.

Opens with O_NOFOLLOW and requires a regular file, so a symlink cannot redirect the write. Existing EXIF is imported (ENODATA means none, which is acceptable); other read/parse failures abort rather than discard metadata.

With IMAGE_EXIF_ATOMIC_REPLACE the JPEG is streamed to a sibling mkstemp() file (same directory, so rename() stays atomic on one filesystem) with the original permission bits, fsync()ed, and renamed over the original only if the path still names the same inode (dev/ino), so a concurrently replaced file is not clobbered. Otherwise the EXIF segment is updated in place.

Parameters
filenameTarget JPEG path.
informationNUL-terminated analysis record.
Returns
0 on success, -1 on any open, stat, EXIF, write, sync or rename failure (errno may reflect the failing call; not guaranteed).
Note
The containing directory is not fsync()ed after rename(), so the rename itself may not survive a sudden power loss.
Warning
In-place mode cannot roll back a partially written update.

Definition at line 322 of file lwir_analysis.c.

◆ read_pose()

bool read_pose ( const char * filename,
struct lwir_camera_pose * pose )
static

Read the observation pose recorded in a saved image's EXIF ImageDescription.

The description is a key=value; key=value list written at capture time. Segments end at ';' or NUL; segments without '=' and unknown keys are ignored, but a duplicated known key or an unparsable number rejects the whole pose rather than guessing which value is right.

Required: lat_deg, lon_deg, alt_m (WGS84 ellipsoidal height, as sent by the flight controller), roll_deg, pitch_deg, yaw_deg, and either altitude_agl_m or groundalt_m. Both carry height above ground: USHER's groundalt_m is the flight controller's groundalt field, which Paparazzi fills with altitude above ground, not the ground's own altitude. When both are present altitude_agl_m wins.

Timing qualification: synchronized is true only when capture_pose_synchronized=true, observation_time_kind names an exposure-bounding interval, and a non-negative pose_uncertainty_m is present. Any other time kind (for example a plain arrival time) leaves it false.

Parameters
filenameSaved JPEG to read; opened independently of the publisher.
[out]poseWritten only on success; angles converted to radians.
Returns
True when a complete pose was parsed; false when EXIF or the description is absent/malformed or required keys are missing. Callers treat false as "missing pose", never as a zero pose.
Note
Uses a stack copy of the EXIF payload (EXIF_MAX_PAYLOAD bytes).

Definition at line 151 of file lwir_analysis.c.

◆ read_u16_le()

uint16_t read_u16_le ( const uint8_t * bytes)
static

Decode one unsigned 16-bit little-endian value.

Byte-wise assembly is independent of host endianness and alignment, so the plane can be read straight from an arbitrary byte buffer.

Parameters
bytesAt least two readable bytes.
Returns
The decoded value.

Definition at line 68 of file lwir_analysis.c.

◆ set_hotspot_comment()

bool set_hotspot_comment ( const char * information)
static

Store the analysis record in the in-memory EXIF document's UserComment.

Layout: the 8-byte EXIF character-code prefix ASCII\0\0\0, the original ImageDescription text (so pose evidence is visible in one field), a \nLWIR_HOTSPOTS_V1\n version separator, the record, and a terminating NUL. The description is copied out first because exif_document_reserve() can move existing values that description.data borrows.

Parameters
informationNUL-terminated record, at most 8192 characters.
Returns
True when the tag was reserved and filled; false for an oversized or non-ASCII-format description, an oversized record, or exhausted document capacity (errno ENOSPC/EINVAL from exif_document_reserve()).
Note
exif_document_reserve() replaces an existing UserComment, so re-running analysis overwrites rather than stacks records.

Definition at line 273 of file lwir_analysis.c.

◆ temperature_at()

uint16_t temperature_at ( size_t pixel)
static

Raw Kelvin*64 code of one pixel in the static temperature plane.

Parameters
pixelRow-major native index (y * width + x); caller bounds-checks it.
Returns
Raw code; 0 and 65535 are sensor endpoint codes, not real temperatures.
Note
Celsius is code / 64 - 273.15.

Definition at line 79 of file lwir_analysis.c.

Variable Documentation

◆ detection_workspace

struct lwir_detection_workspace detection_workspace
static

Definition at line 59 of file lwir_analysis.c.

◆ geolocation_description

unsigned char geolocation_description[8192]
static

Definition at line 56 of file lwir_analysis.c.

◆ geolocation_exif_document

struct exif_document geolocation_exif_document
static

Definition at line 53 of file lwir_analysis.c.

◆ geolocation_exif_payload

unsigned char geolocation_exif_payload[EXIF_MAX_PAYLOAD]
static

Definition at line 54 of file lwir_analysis.c.

◆ temperature_plane

uint8_t temperature_plane[LWIR_MAX_WIDTH *LWIR_MAX_HEIGHT *2]
static

Definition at line 58 of file lwir_analysis.c.