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

Display window resolution and per-scanline Y/Cb/Cr rendering of LWIR frames. More...

#include "lwir_render.h"
#include <errno.h>
#include <stdio.h>
Include dependency graph for lwir_render.c:

Go to the source code of this file.

Functions

static uint16_t temperature (const uint8_t *frame, size_t pixels, size_t pixel)
 Read one little-endian thermal code.
static bool render_frame_valid (const uint8_t *frame, size_t bytes, unsigned width, unsigned height, const struct lwir_render_options *options)
 Check pointers, native geometry and buffer size for the requested view.
static bool valid_options (const struct lwir_render_options *options)
 Validate enumerations, orientation and view-specific option ranges.
bool lwir_render_dimensions (unsigned width, unsigned height, const struct lwir_render_options *options, unsigned *display_width, unsigned *display_height)
 Display size after orientation.
static ptrdiff_t row_source (unsigned width, unsigned height, const struct lwir_render_options *options, unsigned display_width, unsigned display_height, unsigned row, ptrdiff_t *step)
 Map a display row to its first native pixel and per-column native stride.
bool lwir_render_native_coordinate (unsigned width, unsigned height, const struct lwir_render_options *options, unsigned display_x, unsigned display_y, unsigned *native_x, unsigned *native_y)
 Convert a display coordinate into native sensor coordinates.
static void sensor_row (const uint8_t *frame, ptrdiff_t first, ptrdiff_t step, unsigned width, uint8_t *output)
 Copy the camera's own YUYV image row into Y/Cb/Cr output (LWIR_SENSOR).
static bool bad_pixel (const struct lwir_profile *profile, size_t pixel)
 Test whether a native pixel is listed as bad in the profile.
static bool original_sample (const uint8_t *frame, unsigned width, unsigned height, size_t pixel, const struct lwir_profile *profile, uint16_t *raw)
 Read an unreplaced sample usable as-is or as a neighbour.
static bool display_sample (const uint8_t *frame, unsigned width, unsigned height, size_t pixel, const struct lwir_render_options *options, uint16_t *raw)
 Produce the uncorrected display value of one native pixel.
static int32_t corrected (int32_t raw, const struct lwir_profile *profile)
 Apply the profile's linear display correction.
static bool valid_profile (const struct lwir_profile *profile, unsigned width, unsigned height)
 Check a profile against the native geometry and renderer limits.
bool lwir_render_prepare (const uint8_t *frame, size_t bytes, unsigned width, unsigned height, const struct lwir_render_options *options, struct lwir_render_range *range)
 Validate a render request and resolve its display window and counts.
static void color (unsigned intensity, enum lwir_palette palette, unsigned *red, unsigned *green, unsigned *blue)
 Map a window intensity to RGB for the selected palette.
static uint8_t glyph (char character, unsigned row)
 One row of a built-in 5x7 bitmap font.
static void legend_label (char text[16], int32_t raw)
 Format a window bound as degrees Celsius with one decimal.
static void legend_pixel (unsigned row, unsigned column, unsigned width, unsigned height, const struct lwir_render_options *options, const struct lwir_render_range *range, unsigned *red, unsigned *green, unsigned *blue)
 Colour of one pixel inside the 16-row legend strip at the bottom of the display.
bool lwir_render_row (const uint8_t *frame, size_t bytes, unsigned width, unsigned height, unsigned row, const struct lwir_render_options *options, const struct lwir_render_range *range, uint8_t *output, size_t capacity)
 Render one oriented display row as interleaved Y/Cb/Cr.

Variables

static uint32_t histogram [65536]

Detailed Description

Display window resolution and per-scanline Y/Cb/Cr rendering of LWIR frames.

Input is the Tiny1-C combined frame: the first half is the YUYV image plane (Y0 U Y1 V per pixel pair, chroma shared by the pair), the second half the thermal plane of little-endian uint16 Kelvin*64 codes (raw/64 - 273.15 = degC as reported by the device, calibration unverified). Codes 0 and 65535 are endpoint/invalid codes. LWIR_SENSOR also accepts an image-only buffer of half that size.

Output is one JPEG-ready scanline of interleaved full-range BT.601 (JFIF) Y/Cb/Cr bytes per call. Orientation is resolved once per scanline by row_source() into a first native pixel and a signed stride, so the raw thermal bytes are never reordered or copied.

Optional profile corrections, bad-pixel replacement and denoise act on display values only. The scalar code is the reference; NEON (ARM64) and SSSE3 (x86-64) cover common +/-1-stride cases and must yield identical bytes, falling back to scalar for transposed rows, tails, profiles, denoise, the iron palette and legend rows.

Storage: no heap. The percentile histogram is a 256 KiB static array, chosen over the stack (too large for thread stacks) and heap (project policy); this makes lwir_render_prepare() non-re-entrant.

Definition in file lwir_render.c.

Function Documentation

◆ bad_pixel()

bool bad_pixel ( const struct lwir_profile * profile,
size_t pixel )
static

Test whether a native pixel is listed as bad in the profile.

Lower-bound binary search over the strictly ascending bad_pixels list (ordering is enforced by valid_profile()); O(log n) for at most 128 entries.

Parameters
profileOptional profile; NULL means no bad pixels.
pixelNative raster index.
Returns
True when the pixel is listed.

Definition at line 279 of file lwir_render.c.

◆ color()

void color ( unsigned intensity,
enum lwir_palette palette,
unsigned * red,
unsigned * green,
unsigned * blue )
static

Map a window intensity to RGB for the selected palette.

White-hot is grey i, black-hot grey 255-i. Iron splits 0..255 into five equal segments between six anchors: scaled = 5i, segment scaled / 255, weight scaled % 255, then rounded linear interpolation (a*(255-f) + b*f + 127) / 255. At i = 255 the segment is 5 with weight 0, so the last anchor is returned exactly.

Parameters
intensityWindow position 0..255.
palettePalette.
redOutput red 0..255.
greenOutput green 0..255.
blueOutput blue 0..255.

Definition at line 490 of file lwir_render.c.

◆ corrected()

int32_t corrected ( int32_t raw,
const struct lwir_profile * profile )
static

Apply the profile's linear display correction.

round_half_up(raw * scale_ppm / 1e6) + trunc(offset_mk * 64 / 1000): one kelvin is 64 raw units, so one millikelvin is 0.064 raw. 64-bit intermediates avoid overflow. For the accepted positive scale the mapping is monotone non-decreasing, so ranking raw values (percentiles) and correcting afterwards preserves order.

Parameters
rawRaw or replaced/denoised display value.
profileOptional profile; NULL returns raw unchanged.
Returns
Corrected value in Kelvin*64 units; may fall outside 0..65535.

Definition at line 381 of file lwir_render.c.

◆ display_sample()

bool display_sample ( const uint8_t * frame,
unsigned width,
unsigned height,
size_t pixel,
const struct lwir_render_options * options,
uint16_t * raw )
static

Produce the uncorrected display value of one native pixel.

  1. Ordinary pixels use original_sample().
  2. Listed bad pixels take the median of their usable 4-connected neighbours (bad, endpoint and out-of-support neighbours are skipped; edges have fewer candidates).
  3. If denoise_raw is set, average the value with those of its 8 neighbours whose original sample lies within +/-denoise_raw of it (edge-preserving: neighbours across a stronger edge are excluded), rounded to nearest.
    Parameters
    frameCombined frame.
    widthNative width.
    heightNative height.
    pixelNative raster index.
    optionsProfile and denoise policy.
    rawOutput display value (still uncorrected Kelvin*64).
    Returns
    False when no usable value exists; the pixel renders as invalid.
    Note
    Neighbourhoods read original samples only, never replaced or denoised values, so the result is independent of traversal order.

Definition at line 328 of file lwir_render.c.

◆ glyph()

uint8_t glyph ( char character,
unsigned row )
static

One row of a built-in 5x7 bitmap font.

Parameters
characterDigit, 'C', '-', '.' or anything else (blank).
rowFont row 0..6.
Returns
Five-bit row mask; bit 4 is the leftmost column.

Definition at line 512 of file lwir_render.c.

◆ legend_label()

void legend_label ( char text[16],
int32_t raw )
static

Format a window bound as degrees Celsius with one decimal.

raw * 1000 / 64 gives millikelvin, minus 273150 gives milli-degC, / 100 gives tenths. Integer division truncates toward zero, so labels are truncated, not rounded. The value is the device-reported (optionally profile-corrected) scale, not a verified temperature.

Parameters
textOutput buffer of 16 bytes, always NUL-terminated by snprintf.
rawWindow bound in Kelvin*64 units.

Definition at line 534 of file lwir_render.c.

◆ legend_pixel()

void legend_pixel ( unsigned row,
unsigned column,
unsigned width,
unsigned height,
const struct lwir_render_options * options,
const struct lwir_render_range * range,
unsigned * red,
unsigned * green,
unsigned * blue )
static

Colour of one pixel inside the 16-row legend strip at the bottom of the display.

Strip rows from top: 7 rows of palette gradient across the full width (column * 255 / (width-1)), one black separator row, 7 font rows, one black bottom row. The low label is left-aligned in the left half, the high label right-aligned; each glyph uses a 6-pixel cell (5 pixels plus a gap). Positions outside a label stay black.

Parameters
rowDisplay row within the last 16 rows.
columnDisplay column.
widthDisplay width (>= 112, checked by callers).
heightDisplay height (>= 24, checked by callers).
optionsPalette.
rangeWindow whose bounds are labelled.
redOutput red.
greenOutput green.
blueOutput blue.
Note
Labels are re-formatted per legend pixel: simpler than caching across calls and limited to 16 rows, at the cost of redundant snprintf work.

Definition at line 559 of file lwir_render.c.

◆ lwir_render_dimensions()

bool lwir_render_dimensions ( unsigned width,
unsigned height,
const struct lwir_render_options * options,
unsigned * display_width,
unsigned * display_height )

Display size after orientation.

Derive display dimensions; quarter-turns do not change thermal geometry.

90/270 degree rotations swap width and height; mirror and flip do not.

Returns
False for unsupported geometry, invalid options or NULL outputs (errno untouched).

Definition at line 136 of file lwir_render.c.

◆ lwir_render_native_coordinate()

bool lwir_render_native_coordinate ( unsigned width,
unsigned height,
const struct lwir_render_options * options,
unsigned display_x,
unsigned display_y,
unsigned * native_x,
unsigned * native_y )

Convert a display coordinate into native sensor coordinates.

Invert display orientation before accessing calibration or native samples.

Uses the same row_source() transform as rendering, so overlays and measurements picked on the display address exactly the pixel that was shown.

Returns
False for invalid geometry/options, NULL outputs or out-of-range display points.

Definition at line 191 of file lwir_render.c.

◆ lwir_render_prepare()

bool lwir_render_prepare ( const uint8_t * frame,
size_t bytes,
unsigned width,
unsigned height,
const struct lwir_render_options * options,
struct lwir_render_range * range )

Validate a render request and resolve its display window and counts.

Resolve one deterministic display window while retaining original samples.

Percentile algorithm: histogram every valid uncorrected display value, then pick the values at 0-based ranks floor((samples-1) * percentile / 100) (lower nearest-rank) by scanning cumulative counts. The window is corrected afterwards, which is equivalent to ranking corrected values because correction is monotone. Afterwards every pixel is classified as invalid, below or above the window.

Returns
True on success; false with errno EINVAL for invalid input or ENODATA when a percentile view has no valid sample.
Warning
Not re-entrant: shares the static histogram.

Definition at line 418 of file lwir_render.c.

◆ lwir_render_row()

bool lwir_render_row ( const uint8_t * frame,
size_t bytes,
unsigned width,
unsigned height,
unsigned row,
const struct lwir_render_options * options,
const struct lwir_render_range * range,
uint8_t * output,
size_t capacity )

Render one oriented display row as interleaved Y/Cb/Cr.

Emit one bounded Y/Cb/Cr scanline, without a full RGB or rotated frame.

Resolves the row's first native pixel and stride once, then either copies the camera image (LWIR_SENSOR) or maps each display value through the window: invalid -> yellow, below -> cyan-blue, above -> magenta, otherwise palette colour; legend rows are overdrawn. RGB is converted with integer full-range BT.601 coefficients.

Returns
True on success; false with errno EINVAL for invalid input.

Definition at line 665 of file lwir_render.c.

◆ original_sample()

bool original_sample ( const uint8_t * frame,
unsigned width,
unsigned height,
size_t pixel,
const struct lwir_profile * profile,
uint16_t * raw )
static

Read an unreplaced sample usable as-is or as a neighbour.

Parameters
frameCombined frame.
widthNative width.
heightNative height.
pixelNative raster index.
profileOptional profile supplying bad pixels and the supported raw range.
rawOutput code; written unless the pixel is listed bad.
Returns
False for listed bad pixels, endpoint codes or codes outside the profile's support range (these render as invalid rather than being extrapolated).

Definition at line 301 of file lwir_render.c.

◆ render_frame_valid()

bool render_frame_valid ( const uint8_t * frame,
size_t bytes,
unsigned width,
unsigned height,
const struct lwir_render_options * options )
static

Check pointers, native geometry and buffer size for the requested view.

Parameters
frameFrame bytes.
bytesSupplied size.
widthNative width.
heightNative height.
optionsRender options (only the view is consulted here).
Returns
True for an exact combined frame, or an exact image-only half for LWIR_SENSOR.

Definition at line 104 of file lwir_render.c.

◆ row_source()

ptrdiff_t row_source ( unsigned width,
unsigned height,
const struct lwir_render_options * options,
unsigned display_width,
unsigned display_height,
unsigned row,
ptrdiff_t * step )
static

Map a display row to its first native pixel and per-column native stride.

Display = flip(mirror(rotate_cw(native))), so the inverse is applied in reverse order. For display (x, row):

  1. Undo flip: vertical = flip ? display_height-1-row : row.
  2. Undo mirror: rotated column x' = horizontal + direction*x, with horizontal = mirror ? display_width-1 : 0 and direction = mirror ? -1 : +1.
  3. Undo clockwise rotation of (x', vertical) into native (nx, ny):
    • 0: nx = x', ny = vertical -> stride +direction
    • 90: nx = vertical, ny = height-1-x' -> stride -direction*width
    • 180: nx = width-1-x', ny = height-1-vertical -> stride -direction
    • 270: nx = width-1-vertical, ny = x' -> stride +direction*width Native index ny*width + nx is affine in x, so the result is first + x*step.
      Parameters
      widthNative width.
      heightNative height.
      optionsOrientation policy (validated).
      display_widthOriented width.
      display_heightOriented height.
      rowDisplay row, < display_height.
      stepOutput signed native stride per display column.
      Returns
      Native raster index of display column 0.
      Note
      With even native width, forward (+1) rows start at an even native index and reversed (-1) rows start at an odd one, so pixel - 7 is even and 8-pixel SIMD blocks always begin on an intact YUYV pair.

Definition at line 170 of file lwir_render.c.

◆ sensor_row()

void sensor_row ( const uint8_t * frame,
ptrdiff_t first,
ptrdiff_t step,
unsigned width,
uint8_t * output )
static

Copy the camera's own YUYV image row into Y/Cb/Cr output (LWIR_SENSOR).

The camera's chroma is reused directly, so no colour conversion is needed: each output pixel takes its own Y and its pair's shared U (Cb) and V (Cr). SSSE3 or NEON handle leading 8-pixel blocks of +/-1-stride rows; the scalar loop handles the rest and all transposed (+/-width stride) rows.

Parameters
frameImage plane start (combined or image-only buffer).
firstNative index of display column 0.
stepSigned native stride per display column.
widthDisplay width.
outputDestination of width * 3 bytes.

Definition at line 235 of file lwir_render.c.

◆ temperature()

uint16_t temperature ( const uint8_t * frame,
size_t pixels,
size_t pixel )
static

Read one little-endian thermal code.

Parameters
frameCombined frame start.
pixelsNative pixel count (width * height); the thermal plane starts at pixels * 2.
pixelNative raster index.
Returns
Raw Kelvin*64 code, including endpoint codes 0/65535.
Note
Caller guarantees bounds; byte assembly keeps it independent of host endianness and alignment.

Definition at line 63 of file lwir_render.c.

◆ valid_options()

bool valid_options ( const struct lwir_render_options * options)
static

Validate enumerations, orientation and view-specific option ranges.

Rotation must be a quarter turn 0..270. LWIR_SENSOR rejects profile and denoise because the camera image has no raw codes to correct. LWIR_FIXED needs 0 <= low < high <= 65535; LWIR_PERCENTILE needs low < high <= 100. Denoise is capped at 640 raw (10 K).

Parameters
optionsOptions to check, may be NULL.
Returns
True when every field is supported.

Definition at line 121 of file lwir_render.c.

◆ valid_profile()

bool valid_profile ( const struct lwir_profile * profile,
unsigned width,
unsigned height )
static

Check a profile against the native geometry and renderer limits.

Requires matching geometry, at most LWIR_MAX_BAD_PIXELS entries, scale 0.5..1.5, offset +/-50 K, a non-empty support range excluding both endpoint codes, and in-bounds strictly ascending bad pixels (required by bad_pixel()'s binary search).

Parameters
profileOptional profile; NULL is valid.
widthNative width.
heightNative height.
Returns
True when the profile can be applied.

Definition at line 396 of file lwir_render.c.

Variable Documentation

◆ histogram

uint32_t histogram[65536]
static

Percentile workspace: one counter per possible raw code. Shared, so prepare is serial.

Definition at line 53 of file lwir_render.c.