#include "overlay.h" #include "draw.h" #include #include static const vix_rgb GREEN = {80, 220, 80}; static void draw_face(const vix_canvas *canvas, const facedet_detection *face, int index) { double left = face->x, top = face->y; double right = left + face->w, bottom = top + face->h; if (!isfinite(left) || !isfinite(top) || !isfinite(right) || !isfinite(bottom) || face->w <= 0 || face->h <= 0 || right <= 0 || bottom <= 0 || left >= canvas->w || top >= canvas->h) return; int x = (int)floor(fmax(0, left)); int y = (int)floor(fmax(0, top)); int w = (int)ceil(fmin(canvas->w, right)) - x; int h = (int)ceil(fmin(canvas->h, bottom)) - y; vix_draw_rect(canvas, x, y, w, h, GREEN, 2); char label[32]; snprintf(label, sizeof label, "face %d %.2f", index + 1, face->score); int label_width = vix_text_width(label, 1); int label_x = x; if (label_x + label_width > canvas->w) label_x = canvas->w > label_width ? canvas->w - label_width : 0; vix_draw_text(canvas, label_x, y - 9 < 2 ? 2 : y - 9, label, GREEN, 1); for (int k = 0; k < 5; k++) { float lx = face->lmk[k][0], ly = face->lmk[k][1]; if (!isfinite(lx) || !isfinite(ly) || lx < -2 || ly < -2 || lx >= canvas->w + 2 || ly >= canvas->h + 2) continue; vix_draw_dot(canvas, (int)lroundf(lx), (int)lroundf(ly), 2, GREEN); } } int overlay_write_ppm(const char *path, unsigned char *rgb, int width, int height, const facedet_detection *faces, int count) { if (!path || !*path || !rgb || width < 1 || height < 1 || width > 4096 || height > 4096 || count < 0 || (count && !faces)) return -1; vix_canvas canvas = {rgb, width, height}; for (int i = 0; i < count; i++) draw_face(&canvas, &faces[i], i); FILE *file = fopen(path, "wb"); if (!file) return -1; size_t pixels = (size_t)canvas.w * canvas.h; int failed = fprintf(file, "P6\n%d %d\n255\n", canvas.w, canvas.h) < 0 || fwrite(canvas.px, 3, pixels, file) != pixels; if (fclose(file) != 0) failed = 1; return failed ? -1 : 0; }