41#define HEATMAP_MIN_CELL_M 1.0
42#define HEATMAP_MAX_CELLS 96U
43#define HEATMAP_MAX_OUTPUT_PX 768U
44#define HEATMAP_QUALITY 90
45#define HEATMAP_MIN_SAMPLES 3U
46#define EARTH_RADIUS_M 6378137.0
47#define DEG_TO_RAD 0.017453292519943295
50#define HEATMAP_RGB_CHANNELS 3
51#define HEATMAP_RGB_CHANNELS_U 3U
52#define HEATMAP_RGB_MAX 255
53#define HEATMAP_RGB_MAX_F 255.0
54#define HEATMAP_PATH_CAPACITY 512
55#define HEATMAP_SUFFIX_LENGTH 4
56#define HEATMAP_SUFFIX_STORAGE 5
57#define HEATMAP_BOUND_INITIALIZER 1e9
58#define HEATMAP_POSE_LATITUDE_EPSILON_DEG 1e-6
59#define HEATMAP_MIN_USABLE_AGL_M 0.5
60#define HEATMAP_FALLBACK_AGL_M 10.0
61#define HEATMAP_CELLS_PER_AGL 4.0
62#define HEATMAP_FOOTPRINT_SIGMA_DIVISOR 2.0
63#define HEATMAP_MIN_HALF_EXTENT_CELLS 3.0
64#define HEATMAP_GAUSSIAN_REACH_SIGMAS 3.0
65#define HEATMAP_MIN_DEPOSIT_WEIGHT 1e-3
66#define HEATMAP_MIN_INTERPOLATION_WEIGHT 1e-6
67#define HEATMAP_DB_LOG_BASE 10.0
68#define HEATMAP_POWER_DB_PER_DECADE 10.0
69#define HEATMAP_FLAT_RANGE_DB 1.0
70#define HEATMAP_DISPLAY_RANGE_DB 20.0
72#define HEATMAP_PALETTE_FIRST_END 0.33
73#define HEATMAP_PALETTE_SECOND_END 0.66
74#define HEATMAP_PALETTE_FIRST_WIDTH 0.33
75#define HEATMAP_PALETTE_SECOND_WIDTH 0.33
76#define HEATMAP_PALETTE_LAST_WIDTH 0.34
77#define HEATMAP_PALETTE_CHANNEL_LOW 0.05
78#define HEATMAP_PALETTE_CHANNEL_HIGH 0.95
79#define HEATMAP_PALETTE_BLUE_START 0.35
80#define HEATMAP_PALETTE_GREEN_START 0.10
81#define HEATMAP_PALETTE_GREEN_FIRST_RISE 0.70
82#define HEATMAP_PALETTE_BLUE_FIRST_RISE 0.55
83#define HEATMAP_PALETTE_RED_SECOND_RISE 0.90
84#define HEATMAP_PALETTE_GREEN_MIDDLE 0.80
85#define HEATMAP_PALETTE_GREEN_SECOND_RISE 0.15
86#define HEATMAP_PALETTE_BLUE_MIDDLE 0.90
87#define HEATMAP_PALETTE_BLUE_SECOND_FALL 0.85
88#define HEATMAP_PALETTE_GREEN_LAST_FALL 0.80
90#define HEATMAP_RING_STROKE_PX 2.0
91#define HEATMAP_DOT_REACH_PX 2.5
92#define HEATMAP_DOT_OUTER_RADIUS_PX 2.0
93#define HEATMAP_SPOT_GREEN 40
94#define HEATMAP_SPOT_RING_RADIUS_PX 9.0
95#define HEATMAP_SPOT_ARM_LENGTH_PX 22.0
96#define HEATMAP_SPOT_ARM_GAP_PX 4.0
97#define HEATMAP_SPOT_OUTLINE_RADIUS_PX 2.4
98#define HEATMAP_SPOT_ARM_RADIUS_PX 1.2
99#define HEATMAP_SPOT_CENTER_RADIUS_PX 1.5
100#define HEATMAP_SCALE_LENGTH_M 50.0
101#define HEATMAP_SCALE_MAX_WIDTH_FRACTION 0.6
102#define HEATMAP_SCALE_LEFT_FRACTION 0.04
103#define HEATMAP_SCALE_BOTTOM_FRACTION 0.06
104#define HEATMAP_SCALE_OUTLINE_RADIUS_PX 2.5
105#define HEATMAP_SCALE_STROKE_RADIUS_PX 1.2
106#define HEATMAP_SCALE_TICK_HALF_HEIGHT_PX 6
134 longjmp(
error->recovery, 1);
146 if (t < 0.0) { t = 0.0; }
147 if (t > 1.0) { t = 1.0; }
183static void draw_disc(uint8_t *pixels,
unsigned int width,
unsigned int height,
184 double cx,
double cy,
double radius,
const uint8_t *rgb,
bool ring)
186 if (!isfinite(cx) || !isfinite(cy) || !isfinite(radius) || radius < 0 ||
187 cx < -radius - 1 || cy < -radius - 1 || cx > width + radius + 1 || cy > height + radius + 1)
return;
188 int x0 = (int)floor(cx - radius - 1.0);
189 int x1 = (int)ceil(cx + radius + 1.0);
190 int y0 = (int)floor(cy - radius - 1.0);
191 int y1 = (int)ceil(cy + radius + 1.0);
192 for (
int y = y0; y <= y1; y++) {
193 if (y < 0 || (
unsigned int)y >= height) {
continue; }
194 for (
int x = x0; x <= x1; x++) {
195 if (x < 0 || (
unsigned int)x >= width) {
continue; }
196 double d = hypot(x + 0.5 - cx, y + 0.5 - cy);
200 p[0] = rgb[0]; p[1] = rgb[1]; p[2] = rgb[2];
213 return v < 0.0 ? 0.0 : (v > 1.0 ? 1.0 : v);
226static void draw_dot(uint8_t *pixels,
unsigned int width,
unsigned int height,
double cx,
double cy)
232 for (
int y = y0; y <= y1; y++) {
233 if (y < 0 || (
unsigned int)y >= height) {
continue; }
234 for (
int x = x0; x <= x1; x++) {
235 if (x < 0 || (
unsigned int)x >= width) {
continue; }
236 double d = hypot(x + 0.5 - cx, y + 0.5 - cy);
237 double white =
clamp01(1.0 - d);
239 if (white + black <= 0.0) {
continue; }
243 p[c] = (uint8_t)(v + 0.5);
265static void write_georef(
const char *filename,
unsigned int width,
unsigned int height,
266 double lat_c,
double lon_c,
double cos_lat,
double half_e,
double half_n,
270 size_t n = strlen(filename);
276 for (
size_t i = 0; i < stem; i++) {
277 path[i] = filename[i];
280 strcat(path,
".geo");
281 FILE *f = fopen(path,
"w");
286 double m_per_deg_lon = m_per_deg_lat * cos_lat;
287 fprintf(f,
"# ear heatmap georeference: equirectangular about the centre, north up\n");
288 fprintf(f,
"width_px=%u\nheight_px=%u\nm_per_px=%.6f\n", width, height, m_per_px);
289 fprintf(f,
"lat_center=%.8f\nlon_center=%.8f\n", lat_c, lon_c);
290 fprintf(f,
"lat_north=%.8f\nlon_west=%.8f\n", lat_c + half_n / m_per_deg_lat, lon_c - half_e / m_per_deg_lon);
291 fprintf(f,
"lat_south=%.8f\nlon_east=%.8f\n", lat_c + (half_n - height * m_per_px) / m_per_deg_lat,
292 lon_c + (width * m_per_px - half_e) / m_per_deg_lon);
293 if (spot != NULL && spot->
valid) {
294 fprintf(f,
"spot_lat=%.8f\nspot_lon=%.8f\nspot_level_db=%.2f\nspot_confidence=%.3f\n",
310static int encode_jpeg(
const char *filename,
const uint8_t *pixels,
311 unsigned int width,
unsigned int height)
313 FILE *
output = fopen(filename,
"wb");
317 struct jpeg_compress_struct
encoder;
321 if (setjmp(
error.recovery) != 0) {
322 jpeg_destroy_compress(&
encoder);
326 jpeg_create_compress(&
encoder);
331 encoder.in_color_space = JCS_RGB;
334 jpeg_start_compress(&
encoder, TRUE);
339 jpeg_finish_compress(&
encoder);
340 jpeg_destroy_compress(&
encoder);
341 return fclose(
output) == 0 ? 0 : -1;
366 double agl_sum = 0.0;
368 for (
size_t index = 0; index <
count; index++) {
372 (isfinite(s->
agl_m) && s->
agl_m > INT32_MAX))
return -1;
378 if (!isfinite(power) || power < DBL_MIN || power > DBL_MAX /
EAR_MAX_SAMPLES)
return -1;
390 double agl = agl_sum / (double)usable;
395 double lat_c = 0.5 * (lat_min + lat_max);
396 double lon_c = 0.5 * (lon_min + lon_max);
398 if (!isfinite(cos_lat) || cos_lat <= 0)
return -1;
403 double grid_x = ceil(2.0 * half_e / cell_m), grid_y = ceil(2.0 * half_n / cell_m);
404 if (!isfinite(grid_x) || !isfinite(grid_y) || grid_x < 1 || grid_y < 1)
return -1;
408 grid_x = ceil(2.0 * half_e / cell_m);
409 grid_y = ceil(2.0 * half_n / cell_m);
411 if (!isfinite(grid_x) || !isfinite(grid_y) || grid_x < 1 || grid_y < 1 ||
413 const unsigned int cells_x = (
unsigned int)grid_x, cells_y = (
unsigned int)grid_y;
423 double inv_two_sigma2 = 1.0 / (2.0 * sigma_m * sigma_m);
425 for (
size_t index = 0; index <
count; index++) {
433 double gx = (e + half_e) / cell_m;
434 double gy = (half_n - n) / cell_m;
436 int cx0 = (int)floor(gx), cy0 = (int)floor(gy);
437 for (
int y = cy0 - reach; y <= cy0 + reach; y++) {
438 if (y < 0 || (
unsigned int)y >= cells_y) {
continue; }
439 for (
int x = cx0 - reach; x <= cx0 + reach; x++) {
440 if (x < 0 || (
unsigned int)x >= cells_x) {
continue; }
441 double dx = (x + 0.5 - gx) * cell_m;
442 double dy = (y + 0.5 - gy) * cell_m;
443 double w = exp(-(dx * dx + dy * dy) * inv_two_sigma2);
444 size_t k = (size_t)y * cells_x + x;
445 field[k] += w * power;
450 for (
size_t k = 0; k < (size_t)cells_x * cells_y; k++) {
453 if (field[k] < level_min) { level_min = field[k]; }
454 if (field[k] > level_max) { level_max = field[k]; }
459 if (!(level_max > level_min)) {
464 double floor_db = level_max - span;
468 unsigned int cells_max = cells_x > cells_y ? cells_x : cells_y;
470 unsigned int width = (
unsigned int)ceil(cells_x * scale);
471 unsigned int height = (
unsigned int)ceil(cells_y * scale);
472 if (width < 1U) { width = 1U; }
473 if (height < 1U) { height = 1U; }
478 for (
unsigned int y = 0; y < height; y++) {
479 double fy = (y + 0.5) / scale - 0.5;
480 int y0 = (int)floor(fy);
double ty = fy - y0;
481 for (
unsigned int x = 0; x < width; x++) {
482 double fx = (x + 0.5) / scale - 0.5;
483 int x0 = (int)floor(fx);
double tx = fx - x0;
484 double sum = 0.0, wsum = 0.0;
485 for (
int j = 0; j < 2; j++) {
487 if (yy < 0 || (
unsigned int)yy >= cells_y) {
continue; }
488 for (
int i = 0; i < 2; i++) {
490 if (xx < 0 || (
unsigned int)xx >= cells_x) {
continue; }
491 double v = field[(size_t)yy * cells_x + xx];
492 if (isnan(v)) {
continue; }
493 double w = (i ? tx : 1.0 - tx) * (j ? ty : 1.0 - ty);
500 colormap((sum / wsum - floor_db) / span, p);
508 for (
size_t index = 0; decorate && index <
count; index++) {
515 double px = (e + half_e) / cell_m * scale;
516 double py = (half_n - n) / cell_m * scale;
517 draw_dot(pixels, width, height, px, py);
519 if (decorate && spot != NULL && spot->
valid) {
525 double px = (e + half_e) / cell_m * scale;
526 double py = (half_n - n) / cell_m * scale;
529 for (
int pass = 0; pass < 2; pass++) {
530 const uint8_t *rgb = pass == 0 ? black : magenta;
532 for (
int t = (
int)gap; t <= (int)arm; t++) {
533 draw_disc(pixels, width, height, px + t, py, r, rgb,
false);
534 draw_disc(pixels, width, height, px - t, py, r, rgb,
false);
535 draw_disc(pixels, width, height, px, py + t, r, rgb,
false);
536 draw_disc(pixels, width, height, px, py - t, r, rgb,
false);
540 draw_disc(pixels, width, height, px, py, ring, magenta,
true);
551 int bar_steps = (int)floor(bar_px);
554 for (
int t = 0; t <= bar_steps; t++) {
557 for (
int t = 0; t <= bar_steps; t++) {
566 int status =
encode_jpeg(filename, pixels, width, height);
568 write_georef(filename, width, height, lat_c, lon_c, cos_lat, half_e, half_n, cell_m / scale, spot);
595 while (isspace((
unsigned char)*text)) { text++; }
596 char *end = text + strlen(text);
597 while (end > text && isspace((
unsigned char)end[-1])) { *--end =
'\0'; }
613 double parsed = strtod(text, &end);
614 if (end == text || errno == ERANGE || !isfinite(parsed) || *
trim_whitespace(end) !=
'\0') {
633 unsigned long parsed = strtoul(text, &end, 10);
634 if (end == text || errno == ERANGE || parsed == 0 || parsed > UINT32_MAX
638 *value = (
unsigned int)parsed;
644 if (filename == NULL || georef == NULL) {
return -1; }
645 FILE *input = fopen(filename,
"r");
646 if (input == NULL) {
return -1; }
649 unsigned int seen = 0;
650 bool spot_lat_seen =
false;
651 bool spot_lon_seen =
false;
655 while (fgets(line,
sizeof(line), input) != NULL) {
656 size_t length = strlen(line);
657 if (length > 0 && line[length - 1] !=
'\n' && !feof(input)) {
662 if (*entry ==
'\0' || *entry ==
'#') {
continue; }
663 char *separator = strchr(entry,
'=');
664 if (separator == NULL) {
valid =
false;
break; }
668 unsigned int bit = 0;
669 bool field_valid =
true;
670 if (strcmp(key,
"width_px") == 0) {
673 }
else if (strcmp(key,
"height_px") == 0) {
676 }
else if (strcmp(key,
"m_per_px") == 0) {
679 }
else if (strcmp(key,
"lat_center") == 0) {
682 }
else if (strcmp(key,
"lon_center") == 0) {
685 }
else if (strcmp(key,
"lat_north") == 0) {
688 }
else if (strcmp(key,
"lon_west") == 0) {
691 }
else if (strcmp(key,
"spot_lat") == 0) {
693 if (field_valid) { spot_lat_seen =
true; }
694 }
else if (strcmp(key,
"spot_lon") == 0) {
696 if (field_valid) { spot_lon_seen =
true; }
697 }
else if (strcmp(key,
"spot_level_db") == 0 || strcmp(key,
"spot_confidence") == 0) {
698 double ignored = 0.0;
701 if (!field_valid || (bit != 0 && (seen & bit) != 0)) {
707 if (ferror(input) != 0) {
valid =
false; }
708 if (fclose(input) != 0) {
valid =
false; }
709 const unsigned int required = (1U << 7) - 1U;
710 if (!
valid || seen != required || spot_lat_seen != spot_lon_seen
713 || (spot_lat_seen && (fabs(parsed.
spot_lat) > 90.0 || fabs(parsed.
spot_lon) > 180.0))) {
#define HEATMAP_SCALE_STROKE_RADIUS_PX
#define HEATMAP_DB_LOG_BASE
static void draw_disc(uint8_t *pixels, unsigned int width, unsigned int height, double cx, double cy, double radius, const uint8_t *rgb, bool ring)
Rasterize a clipped filled disc or annular stroke.
static char * trim_whitespace(char *text)
Trim leading/trailing whitespace in a mutable sidecar token.
#define HEATMAP_PALETTE_FIRST_WIDTH
#define HEATMAP_MIN_INTERPOLATION_WEIGHT
#define HEATMAP_PATH_CAPACITY
static uint8_t heatmap_pixels[HEATMAP_MAX_OUTPUT_PX *HEATMAP_MAX_OUTPUT_PX *HEATMAP_RGB_CHANNELS_U]
static void heatmap_jpeg_error_exit(j_common_ptr info)
Turn a fatal libjpeg callback into a local encoder failure.
#define HEATMAP_RING_STROKE_PX
#define HEATMAP_PALETTE_BLUE_FIRST_RISE
#define HEATMAP_BOUND_INITIALIZER
#define HEATMAP_MIN_CELL_M
#define HEATMAP_PALETTE_CHANNEL_HIGH
#define HEATMAP_POWER_DB_PER_DECADE
#define HEATMAP_PALETTE_BLUE_SECOND_FALL
#define HEATMAP_PALETTE_GREEN_LAST_FALL
static void draw_dot(uint8_t *pixels, unsigned int width, unsigned int height, double cx, double cy)
Draw one observation as a white subpixel core with a dark outline.
#define HEATMAP_DOT_OUTER_RADIUS_PX
#define HEATMAP_PALETTE_SECOND_WIDTH
#define HEATMAP_SCALE_MAX_WIDTH_FRACTION
#define HEATMAP_CELLS_PER_AGL
#define HEATMAP_PALETTE_GREEN_SECOND_RISE
#define HEATMAP_POSE_LATITUDE_EPSILON_DEG
#define HEATMAP_FLAT_RANGE_DB
int ear_heatmap_read_georef(const char *filename, struct ear_heatmap_georef *georef)
Read the georeference sidecar emitted by the heatmap renderer.
#define HEATMAP_RGB_MAX_F
static double heatmap_field[HEATMAP_MAX_CELLS *HEATMAP_MAX_CELLS]
static void colormap(double t, uint8_t *rgb)
Map a normalized relative level to the preserved three-color ramp.
#define HEATMAP_PALETTE_CHANNEL_LOW
#define HEATMAP_SCALE_OUTLINE_RADIUS_PX
static pthread_mutex_t heatmap_mutex
#define HEATMAP_MAX_CELLS
static double clamp01(double v)
Clamp a normalized coverage/intensity value to its legal blend interval.
#define HEATMAP_SUFFIX_LENGTH
#define HEATMAP_SPOT_ARM_GAP_PX
#define HEATMAP_DISPLAY_RANGE_DB
#define HEATMAP_FOOTPRINT_SIGMA_DIVISOR
#define HEATMAP_PALETTE_BLUE_MIDDLE
#define HEATMAP_RGB_CHANNELS
#define HEATMAP_MIN_USABLE_AGL_M
static void write_georef(const char *filename, unsigned int width, unsigned int height, double lat_c, double lon_c, double cos_lat, double half_e, double half_n, double m_per_px, const struct ear_loudest_spot *spot)
Write the georeference sidecar for a successful north-up JPEG.
#define HEATMAP_MIN_SAMPLES
#define HEATMAP_PALETTE_FIRST_END
static int encode_jpeg(const char *filename, const uint8_t *pixels, unsigned int width, unsigned int height)
Encode caller-owned RGB pixels as a JPEG.
#define HEATMAP_SPOT_ARM_RADIUS_PX
#define HEATMAP_MAX_OUTPUT_PX
#define HEATMAP_DOT_REACH_PX
static int heatmap_write(const char *filename, const struct ear_sample *samples, size_t count, const struct ear_loudest_spot *spot, bool decorate)
Render the shared measured field with optional human-facing markers.
#define HEATMAP_PALETTE_SECOND_END
#define HEATMAP_SPOT_OUTLINE_RADIUS_PX
#define HEATMAP_SPOT_CENTER_RADIUS_PX
#define HEATMAP_SPOT_GREEN
#define HEATMAP_MIN_DEPOSIT_WEIGHT
#define HEATMAP_SPOT_RING_RADIUS_PX
#define HEATMAP_MIN_HALF_EXTENT_CELLS
#define HEATMAP_SCALE_TICK_HALF_HEIGHT_PX
static bool parse_geo_double(const char *text, double *value)
Parse one finite number from a bounded sidecar value token.
#define HEATMAP_PALETTE_LAST_WIDTH
#define HEATMAP_SCALE_BOTTOM_FRACTION
#define HEATMAP_FALLBACK_AGL_M
#define HEATMAP_PALETTE_GREEN_FIRST_RISE
static double heatmap_weight[HEATMAP_MAX_CELLS *HEATMAP_MAX_CELLS]
#define HEATMAP_PALETTE_GREEN_MIDDLE
static bool parse_geo_dimension(const char *text, unsigned int *value)
Parse a strictly positive image dimension from a sidecar field.
#define HEATMAP_RGB_CHANNELS_U
#define HEATMAP_PALETTE_RED_SECOND_RISE
int ear_heatmap_write(const char *filename, const struct ear_sample *samples, size_t count, const struct ear_loudest_spot *spot)
Write a decorated north-up JPEG of relative measured acoustic intensity.
#define HEATMAP_SCALE_LENGTH_M
#define HEATMAP_SCALE_LEFT_FRACTION
#define HEATMAP_GAUSSIAN_REACH_SIGMAS
#define HEATMAP_SPOT_ARM_LENGTH_PX
#define HEATMAP_PALETTE_BLUE_START
#define HEATMAP_SUFFIX_STORAGE
#define HEATMAP_PALETTE_GREEN_START
int ear_heatmap_write_field(const char *filename, const struct ear_sample *samples, size_t count, const struct ear_loudest_spot *spot)
Write the same field without track dots, source marker or scale bar.
Render geotagged acoustic measurements into north-up JPEG heatmaps.
#define EAR_HEATMAP_NO_DATA_GREEN
#define EAR_HEATMAP_NO_DATA_RED
#define EAR_HEATMAP_NO_DATA_BLUE
static struct ear_sample samples[EAR_MAX_SAMPLES]
struct jpeg_compress_struct encoder
uint8_t row[LWIR_MAX_WIDTH *3U]
struct jpeg_error_mgr error
Validated geographic placement metadata shared by heatmap consumers.
Estimated ground source, not a surveyed target or guaranteed alarm detection.
One audio window associated with microphone position and provenance.
Per-encode libjpeg callback state and recovery point.
struct jpeg_error_mgr manager