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
inference_image.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/** @file inference_image.c
19 * @brief Bounded pixel/thermal conversion shared by live frames and decoded JPEG rows.
20 * @details Resize, channel order and physical units are declared model policy, not camera defaults. */
21#include "inference_image.h"
22
23#include <math.h>
24#include <stdint.h>
25#include <stdio.h>
26
27/** @brief Prove the last addressed row fits before any stride-based pointer arithmetic. */
28static bool plane_valid(const struct inference_image *image, size_t plane,
29 unsigned width, unsigned height, unsigned bytes)
30{
31 const size_t row = (size_t)width * bytes;
32 return width && height && image->planes[plane] && image->strides[plane] >= row &&
33 image->strides[plane] <= SIZE_MAX / height &&
34 image->lengths[plane] >= (size_t)(height - 1) * image->strides[plane] + row;
35}
36
37/** @brief Clamp full-range JPEG/YUV color conversion without changing the raw source evidence. */
38static double limited(double value)
39{
40 return value < 0 ? 0 : value > 255 ? 255 : value;
41}
42
43/** @brief Read one native pixel; missing thermal samples never become invented finite temperatures. */
44static bool source_pixel(const struct inference_image *image, const struct inference_image_spec *spec,
45 unsigned column, unsigned row, double pixel[3])
46{
47 const unsigned char *source = image->planes[0] + (size_t)row * image->strides[0];
48 if (image->format == INFERENCE_KELVIN64) {
49 const unsigned raw = (unsigned)source[column * 2] | (unsigned)source[column * 2 + 1] << 8;
50 if (!raw || raw == UINT16_MAX || spec->units == INFERENCE_PIXEL) return false;
51 pixel[0] = spec->units == INFERENCE_RAW_KELVIN64 ? raw : raw / 64.0;
52 if (spec->units == INFERENCE_CELSIUS) pixel[0] -= 273.15;
53 pixel[1] = pixel[2] = pixel[0];
54 } else if (spec->units != INFERENCE_PIXEL) return false;
55 else if (image->format == INFERENCE_GRAY8) pixel[0] = pixel[1] = pixel[2] = source[column];
56 else if (image->format == INFERENCE_RGB8) {
57 for (size_t channel = 0; channel < 3; ++channel) pixel[channel] = source[(size_t)column * 3 + channel];
58 } else {
59 const double cb = image->planes[1][(size_t)(row / 2) * image->strides[1] + column / 2] - 128.0;
60 const double cr = image->planes[2][(size_t)(row / 2) * image->strides[2] + column / 2] - 128.0;
61 const double luma = source[column];
62 pixel[0] = limited(luma + 1.402 * cr);
63 pixel[1] = limited(luma - 0.344136 * cb - 0.714136 * cr);
64 pixel[2] = limited(luma + 1.772 * cb);
65 }
66 return true;
67}
68
70 const struct inference_image_spec *spec,
71 void *tensor, size_t capacity,
72 struct inference_image_transform *transform,
73 char *error, size_t error_capacity)
74{
75 if (!error || !error_capacity) return false;
76 error[0] = '\0';
77 if (!image || !spec || !tensor || !transform || !image->width || !image->height ||
78 image->width > 8192 || image->height > 8192 || !spec->width || !spec->height ||
79 spec->width > 2048 || spec->height > 2048 || (spec->channels != 1 && spec->channels != 3) ||
80 (spec->layout != INFERENCE_NCHW && spec->layout != INFERENCE_NHWC) ||
81 (spec->type != ONNX_FLOAT32 && spec->type != ONNX_UINT8 && spec->type != ONNX_INT8) ||
82 image->format < INFERENCE_RGB8 || image->format > INFERENCE_KELVIN64) goto invalid;
83 const unsigned pixel_bytes = image->format == INFERENCE_RGB8 ? 3 : image->format == INFERENCE_KELVIN64 ? 2 : 1;
84 if (!plane_valid(image, 0, image->width, image->height, pixel_bytes)) goto invalid;
85 if (image->format == INFERENCE_YUV420 &&
86 (image->width % 2 || image->height % 2 ||
87 !plane_valid(image, 1, image->width / 2, image->height / 2, 1) ||
88 !plane_valid(image, 2, image->width / 2, image->height / 2, 1))) goto invalid;
89 const size_t count = (size_t)spec->width * spec->height * spec->channels;
90 const size_t element_bytes = spec->type == ONNX_FLOAT32 ? sizeof(float) : 1;
91 if (count > ONNX_BUFFER_LIMIT / element_bytes || capacity != count * element_bytes || !isfinite(spec->padding)) goto invalid;
92 if (spec->type == ONNX_FLOAT32 && (uintptr_t)tensor % _Alignof(float)) goto invalid;
93 for (size_t channel = 0; channel < spec->channels; ++channel)
94 if (!isfinite(spec->scale[channel]) || !spec->scale[channel] || !isfinite(spec->mean[channel])) goto invalid;
95 *transform = (struct inference_image_transform){
96 .source_width = image->width, .source_height = image->height,
97 .input_width = spec->width, .input_height = spec->height,
98 .content_width = spec->width, .content_height = spec->height
99 };
100 if (spec->letterbox) {
101 /* Retain the rounded content rectangle so later box projection uses the actual resize, not an ideal scale. */
102 const double horizontal = (double)spec->width / image->width;
103 const double vertical = (double)spec->height / image->height;
104 const double scale = horizontal < vertical ? horizontal : vertical;
105 transform->content_width = (unsigned)floor(image->width * scale + 0.5);
106 transform->content_height = (unsigned)floor(image->height * scale + 0.5);
107 if (!transform->content_width) transform->content_width = 1;
108 if (!transform->content_height) transform->content_height = 1;
109 transform->left = (spec->width - transform->content_width) / 2;
110 transform->top = (spec->height - transform->content_height) / 2;
111 }
112 for (unsigned row = 0; row < spec->height; ++row) {
113 for (unsigned column = 0; column < spec->width; ++column) {
114 double pixel[3] = {spec->padding, spec->padding, spec->padding};
115 if (column >= transform->left && row >= transform->top &&
116 column - transform->left < transform->content_width && row - transform->top < transform->content_height) {
117 /* Pixel-center nearest sampling is also used by the streaming JPEG adapter. */
118 unsigned source_x = (unsigned)(((uint64_t)(column - transform->left) * 2 + 1) * image->width /
119 ((uint64_t)transform->content_width * 2));
120 unsigned source_y = (unsigned)(((uint64_t)(row - transform->top) * 2 + 1) * image->height /
121 ((uint64_t)transform->content_height * 2));
122 if (source_x >= image->width) source_x = image->width - 1;
123 if (source_y >= image->height) source_y = image->height - 1;
124 if (!source_pixel(image, spec, source_x, source_y, pixel)) goto invalid;
125 }
126 if (spec->channels == 1 && image->format != INFERENCE_KELVIN64)
127 pixel[0] = 0.299 * pixel[0] + 0.587 * pixel[1] + 0.114 * pixel[2];
128 for (unsigned channel = 0; channel < spec->channels; ++channel) {
129 const unsigned source_channel = spec->bgr && spec->channels == 3 ? 2 - channel : channel;
130 const double value = (pixel[source_channel] - spec->mean[channel]) * spec->scale[channel];
131 const size_t index = spec->layout == INFERENCE_NCHW ?
132 ((size_t)channel * spec->height + row) * spec->width + column :
133 ((size_t)row * spec->width + column) * spec->channels + channel;
134 if (!isfinite(value)) goto invalid;
135 if (spec->type == ONNX_FLOAT32) {
136 ((float *)tensor)[index] = (float)value;
137 if (!isfinite(((float *)tensor)[index])) goto invalid;
138 } else if (spec->type == ONNX_UINT8) {
139 if (value < 0 || value > UINT8_MAX) goto invalid;
140 ((uint8_t *)tensor)[index] = (uint8_t)lround(value);
141 } else {
142 if (value < INT8_MIN || value > INT8_MAX) goto invalid;
143 ((int8_t *)tensor)[index] = (int8_t)lround(value);
144 }
145 }
146 }
147 }
148 return true;
149invalid:
150 snprintf(error, error_capacity, "Invalid image layout, sample or model preprocessing contract");
151 return false;
152}
153
155 double input_x, double input_y, double *source_x, double *source_y)
156{
157 if (!transform || !source_x || !source_y || !transform->content_width || !transform->content_height ||
158 !isfinite(input_x) || !isfinite(input_y) || input_x < transform->left || input_y < transform->top ||
159 input_x > transform->left + transform->content_width || input_y > transform->top + transform->content_height) return false;
160 *source_x = (input_x - transform->left) * transform->source_width / transform->content_width;
161 *source_y = (input_y - transform->top) * transform->source_height / transform->content_height;
162 return true;
163}
static unsigned capacity[COVERAGE_CAMERAS]
bool invalid
Definition chdk_pipe.c:145
uint8_t bytes[CHDK_CONTROL_REPLY_SIZE]
Definition chdk_pipe.c:144
bool inference_image_point(const struct inference_image_transform *transform, double input_x, double input_y, double *source_x, double *source_y)
Map a model-input point back to source pixels, rejecting letterbox-only coordinates.
static bool plane_valid(const struct inference_image *image, size_t plane, unsigned width, unsigned height, unsigned bytes)
Prove the last addressed row fits before any stride-based pointer arithmetic.
static bool source_pixel(const struct inference_image *image, const struct inference_image_spec *spec, unsigned column, unsigned row, double pixel[3])
Read one native pixel; missing thermal samples never become invented finite temperatures.
static double limited(double value)
Clamp full-range JPEG/YUV color conversion without changing the raw source evidence.
bool inference_image_prepare(const struct inference_image *image, const struct inference_image_spec *spec, void *tensor, size_t capacity, struct inference_image_transform *transform, char *error, size_t error_capacity)
Convert with the declared nearest-neighbor policy, not a guessed training transform.
Source-independent, bounded model preprocessing with reversible coordinate bookkeeping.
@ INFERENCE_NHWC
@ INFERENCE_NCHW
@ INFERENCE_PIXEL
@ INFERENCE_RAW_KELVIN64
@ INFERENCE_CELSIUS
@ INFERENCE_KELVIN64
@ INFERENCE_RGB8
@ INFERENCE_GRAY8
@ INFERENCE_YUV420
uint8_t row[LWIR_MAX_WIDTH *3U]
Definition lwir_image.c:63
struct jpeg_error_mgr error
Definition lwir_image.c:60
@ ONNX_FLOAT32
@ ONNX_UINT8
@ ONNX_INT8
#define ONNX_BUFFER_LIMIT
static size_t count
Definition pose_log.c:79
Model-owned preprocessing; normalized samples are (sample - mean) * scale.
enum inference_layout layout
enum inference_units units
enum onnx_element_type type
Exact resized content rectangle, including rounded letterbox offsets.
Borrowed immutable planes; lengths/strides prove all addressed rows are available.
struct captured_image image
Definition usher.c:371