Loading...
Searching...
No Matches
lwir_image.c
Go to the documentation of this file.
60 struct jpeg_error_mgr error; /**< Standard error manager with error_exit redirected to jpeg_failure(). */
62 uint8_t marker[LWIR_THERMAL_HEADER_BYTES + LWIR_THERMAL_CHUNK_CAPACITY]; /**< One APP15 chunk being assembled. */
63 uint8_t row[LWIR_MAX_WIDTH * 3U]; /**< One interleaved YCbCr display row; the renderer is given its capacity. */
66 char evidence[8192]; /**< APP14 text; well below the 65533-byte APPn limit, so one marker suffices. */
121 "LWIREV02\nsource=%s\nserial=%s\ngain=%s\nfirmware=unknown\nffc=unknown\nenvironment=unknown\nlens_focus=unknown\npower_on_age=unknown\nboot_id=%s\n"
122 "sequence=%llu\nrequest_us=%llu\narrival_us=%llu\nrequest_id=%llu\nexposure=unknown\ncalibration=unverified\n"
124 "display_width=%u\ndisplay_height=%u\nrotation_cw=%u\nmirror=%u\nflip=%u\nraw_coordinates=native\norientation_order=rotate_mirror_flip\n"
125 "view=%u\npalette=%u\nlegend=%u\nwindow_low_kelvin64=%d\nwindow_high_kelvin64=%d\nclipped_low=%zu\nclipped_high=%zu\ninvalid_codes=%zu\n"
126 "profile_sha256=%s\nscale_ppm=%u\noffset_mk=%d\nsupport_min_raw=%u\nsupport_max_raw=%u\nbad_pixel_count=%u\ndenoise_kelvin64=%u\ngain_source=user_or_unknown\n",
130 (unsigned long long)evidence->arrival_us, (unsigned long long)evidence->request_id, width, height, evidence->frame_sha256,
131 evidence->raw_path ? evidence->raw_path : "none", evidence->rendering ? evidence->rendering : "sensor",
132 display_width, display_height, render->rotation, render->mirror ? 1U : 0U, render->flip ? 1U : 0U,
133 (unsigned)render->view, (unsigned)render->palette, render->legend ? 1U : 0U, range->low, range->high, range->below, range->above, range->invalid,
134 render->profile ? render->profile->sha256 : "none", render->profile ? render->profile->scale_ppm : 1000000U,
135 render->profile ? render->profile->offset_mk : 0, render->profile ? render->profile->range_min : 1U,
136 render->profile ? render->profile->range_max : 65534U, render->profile ? render->profile->bad_count : 0U, render->denoise_raw);
142 const int quality_length = snprintf(jpeg_workspace.evidence + evidence_length, sizeof(jpeg_workspace.evidence) - evidence_length,
144 if (quality_length < 0 || (size_t)quality_length >= sizeof(jpeg_workspace.evidence) - evidence_length) return ENOSPC;
147 const int timestamp_length = snprintf(jpeg_workspace.evidence + evidence_length, sizeof(jpeg_workspace.evidence) - evidence_length,
148 "kernel_monotonic_us=%llu\nkernel_timestamp_flags=%u\nkernel_timestamp_is_qualified_exposure=false\n",
150 if (timestamp_length < 0 || (size_t)timestamp_length >= sizeof(jpeg_workspace.evidence) - evidence_length) return ENOSPC;
158 const int added = snprintf(jpeg_workspace.evidence + evidence_length, sizeof(jpeg_workspace.evidence) - evidence_length,
159 "measurement=%s\nmeasurement_coordinates=native\nmeasurement_roi=%u,%u,%u,%u\nmeasurement_line_end=%u,%u\naverage_3x3=%u\n"
160 "measurement_pixels=%zu\nmeasurement_endpoint_codes=%zu\nmeasurement_min_raw=%u\nmeasurement_min_xy=%u,%u\n"
161 "measurement_max_raw=%u\nmeasurement_max_xy=%u,%u\nmeasurement_sum_raw=%llu\nmeasurement_sum_squared_raw=%llu\n"
162 "coverage_enabled=%u\ncoverage_source=original_centers\ncoverage_low_kelvin64=%u\ncoverage_high_kelvin64=%u\n"
165 measurement->roi.x, measurement->roi.y, measurement->kind == LWIR_POINT ? 1U : measurement->roi.width ? measurement->roi.width : width,
166 measurement->kind == LWIR_POINT ? 1U : measurement->roi.width ? measurement->roi.height : height,
167 measurement->end_x, measurement->end_y, measurement->average_3x3 ? 1U : 0U, statistics->pixels, statistics->endpoint_codes,
168 statistics->min_raw, statistics->min_x, statistics->min_y, statistics->max_raw, statistics->max_x, statistics->max_y,
170 thresholds && thresholds->enabled ? 1U : 0U, thresholds ? thresholds->low : 0U, thresholds ? thresholds->high : 0U,
179 const int added = snprintf(jpeg_workspace.evidence + evidence_length, sizeof(jpeg_workspace.evidence) - evidence_length,
180 "device=tdk_5474_5411\nframe_layout=yuyv_image\ntemperature_data=unavailable\nyuv_range=limited\n");
181 if (added < 0 || (size_t)added >= sizeof(jpeg_workspace.evidence) - evidence_length) return ENOSPC;
190 jpeg_write_marker(encoder, JPEG_APP0 + 14, (const JOCTET *)jpeg_workspace.evidence, (unsigned)evidence_length);
225 const char *signature = synthetic ? LWIR_THERMAL_SYNTHETIC_SIGNATURE : LWIR_THERMAL_SENSOR_SIGNATURE;
228 const size_t chunk = remaining < LWIR_THERMAL_CHUNK_CAPACITY ? remaining : LWIR_THERMAL_CHUNK_CAPACITY;
229 for (size_t index = 0; index < LWIR_THERMAL_SIGNATURE_BYTES; ++index) jpeg_workspace.marker[index] = (uint8_t)signature[index];
276static bool encode_jpeg(FILE *output, const uint8_t *frame, size_t bytes, unsigned width, unsigned height,
285 if (image_only && (synthetic || render->view != LWIR_SENSOR || render->legend || render->profile || render->denoise_raw ||
286 render->palette != LWIR_WHITE_HOT || evidence->statistics || evidence->device_state || evidence->raw_path)) {
290 /* Oriented display pixels require native/display provenance; never publish an ambiguous mapping. */
291 if (!evidence && (render->rotation || render->mirror || render->flip)) { errno = EINVAL; return false; }
316 /* Standard Huffman tables avoid an extra statistics pass, trading slightly larger files for speed. */
325 const int provenance_error = write_provenance_markers(encoder, width, height, display_width, display_height,
342 jpeg_workspace.row[column * 3] = expand_video_component(jpeg_workspace.row[column * 3], 16, 219);
344 jpeg_workspace.row[column * 3 + component] = expand_video_component(jpeg_workspace.row[column * 3 + component], 16, 224);
390bool lwir_encode_jpeg(FILE *output, const uint8_t *frame, size_t bytes, unsigned width, unsigned height,
393 const size_t expected = lwir_frame_bytes(width, height) / (evidence && evidence->layout == LWIR_YUYV_IMAGE ? 2U : 1U);
394 if (!output || !frame || !expected || bytes != expected || (evidence && evidence->layout > LWIR_YUYV_IMAGE)) {
398 return encode_jpeg(output, frame, bytes, width, height, synthetic, evidence, render ? render : &sensor);
426 const size_t expected = lwir_frame_bytes(width, height) / (!raw && evidence && evidence->layout == LWIR_YUYV_IMAGE ? 2U : 1U);
442 if (directory < 0) { const int saved_errno = errno; close(descriptor); unlink(temporary); errno = saved_errno; return false; }
516bool lwir_write_raw(const char *path, const uint8_t *frame, size_t bytes, unsigned width, unsigned height)
static bool capture(struct transport *transport, FILE *file, const struct capture_settings *settings)
Capture to the camera's SD card and download the resulting JPEG.
Definition chdkbridge.c:1463
static bool range(size_t total, size_t offset, size_t length)
Check a byte span without forming a potentially overflowing end offset.
Definition exifite.c:133
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
#define EXIF_MAX_PAYLOAD
APP1 payload ceiling: the JPEG length includes its own two bytes.
Definition exifite.h:51
Exact thermal evidence in JPEG containers, independent of analysis.
#define LWIR_THERMAL_SENSOR_SIGNATURE
Signature of a temperature plane read from the live camera.
Definition lwir_container.h:67
#define LWIR_THERMAL_SYNTHETIC_SIGNATURE
Signature of a temperature plane produced by synthetic replay.
Definition lwir_container.h:69
size_t lwir_frame_bytes(unsigned width, unsigned height)
Combined frame size for supported geometry.
Definition lwir_frame.c:56
static void write_thermal_markers(j_compress_ptr encoder, const uint8_t *frame, unsigned width, unsigned height, bool synthetic)
Preserve exact native temperatures independently of display rendering.
Definition lwir_image.c:220
bool lwir_write_raw(const char *path, const uint8_t *frame, size_t bytes, unsigned width, unsigned height)
Publish the combined frame verbatim; see lwir_image.h.
Definition lwir_image.c:516
static bool publish(const char *path, const uint8_t *frame, size_t bytes, unsigned width, unsigned height, bool synthetic, bool raw, const struct lwir_evidence *evidence, const struct lwir_render_options *render)
Durably publish a JPEG or raw companion under a new name.
Definition lwir_image.c:422
bool lwir_write_jpeg(const char *path, const uint8_t *frame, size_t bytes, unsigned width, unsigned height, bool synthetic)
Publish the sensor view without evidence; see lwir_image.h.
Definition lwir_image.c:501
static int open_parent(const char *path)
Open the directory that will contain path, for fsync after linking.
Definition lwir_image.c:367
bool lwir_write_jpeg_rendered(const char *path, const uint8_t *frame, size_t bytes, unsigned width, unsigned height, bool synthetic, const struct lwir_evidence *evidence, const struct lwir_render_options *render)
Publish a rendered view with provenance; see lwir_image.h.
Definition lwir_image.c:522
uint8_t marker[LWIR_THERMAL_HEADER_BYTES+LWIR_THERMAL_CHUNK_CAPACITY]
Definition lwir_image.c:62
static struct @376007165063146230013315373164007004315140235344 jpeg_workspace
Static encoder state shared by every photo.
static void write_u32(uint8_t *destination, uint32_t value)
Store value as four little-endian bytes.
Definition lwir_image.c:74
static int write_provenance_markers(j_compress_ptr encoder, unsigned width, unsigned height, unsigned display_width, unsigned display_height, bool synthetic, const struct lwir_evidence *evidence, const struct lwir_render_options *render, const struct lwir_render_range *range)
Write application provenance without taking ownership of the encoder.
Definition lwir_image.c:112
static uint8_t expand_video_component(uint8_t value, unsigned low, unsigned span)
Expand one limited-range video component to full range.
Definition lwir_image.c:253
static bool encode_jpeg(FILE *output, const uint8_t *frame, size_t bytes, unsigned width, unsigned height, bool synthetic, const struct lwir_evidence *evidence, const struct lwir_render_options *render)
Own the serial codec lifecycle and the ordered marker/scanline stages.
Definition lwir_image.c:276
bool lwir_remove_published(const char *path)
Remove a file this process published and persist the removal; see lwir_image.h.
Definition lwir_image.c:488
bool lwir_write_jpeg_evidence(const char *path, const uint8_t *frame, size_t bytes, unsigned width, unsigned height, bool synthetic, const struct lwir_evidence *evidence)
Publish the sensor view with provenance; see lwir_image.h.
Definition lwir_image.c:508
static void jpeg_failure(j_common_ptr encoder)
libjpeg error_exit replacement: abandon the photo instead of exiting.
Definition lwir_image.c:86
const char * lwir_jpeg_provider(void)
Report the bundled codec; see lwir_image.h.
Definition lwir_image.c:380
bool lwir_encode_jpeg(FILE *output, const uint8_t *frame, size_t bytes, unsigned width, unsigned height, bool synthetic, const struct lwir_evidence *evidence, const struct lwir_render_options *render)
Validate the frame size for its layout, then encode; see lwir_image.h.
Definition lwir_image.c:390
JPEG encoding and durable no-overwrite publication of LWIR frames.
bool lwir_render_dimensions(unsigned width, unsigned height, const struct lwir_render_options *options, unsigned *display_width, unsigned *display_height)
Display size after orientation.
Definition lwir_render.c:136
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.
Definition lwir_render.c:418
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.
Definition lwir_render.c:665
Display rendering of Tiny1-C frames into JPEG-ready Y/Cb/Cr scanlines.
size_t lwir_state_format(const struct lwir_device_state *state, char *output, size_t capacity)
Render snapshot evidence as key=value lines for the JPEG APP14 segment.
Definition lwir_state.c:430
static bool sensor_capture_encode(const struct sensor_capture *record, unsigned char *data, size_t capacity)
Encode a SENSOR02 record in the fixed on-disk layout.
Definition sensor_capture.h:109
Definition ear_heatmap_overlay.c:149
Borrowed provenance for one publication; strings must outlive encoding.
Definition lwir_image.h:63
Display policy for one rendering request; never affects stored samples.
Definition lwir_render.h:103
Resolved display window and diagnostic counts from lwir_render_prepare().
Definition lwir_render.h:127
Raw-unit measurement results; derive mean/variance from the sums and pixels.
Definition lwir_frame.h:98
Optional coverage band counted alongside measurement statistics.
Definition lwir_frame.h:89