40 lines
1.7 KiB
C

#pragma once
#include "yolo_runtime.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int parse_mask_dilation(int argc, char** argv, int* radius) {
int seen=0;*radius=0;
for(int i=1;i<argc;++i) if(strcmp(argv[i],"--mask-dilation")==0) {
if(seen++ || i+1>=argc) return -1;
const char* text=argv[++i];size_t length=strlen(text);
if(!length || length>3 || strspn(text,"0123456789")!=length) return -1;
*radius=atoi(text);if(*radius>100) return -1;
}
return 0;
}
/* Each output belongs to the caller; free(*owned) after RGB/JSON processing.
Original result-set mask and fit geometry are never changed. */
static int dilate_example_mask(yolo_mask_view_v1_t* mask, int radius,
const yolo_roi_v1_t* region, unsigned char** owned) {
yolo_mask_view_v1_t output;
yolo_error_info_v1_t error;
memset(&output,0,sizeof(output));output.struct_size=sizeof(output);output.version=YOLO_MASK_VIEW_VERSION;
memset(&error,0,sizeof(error));error.struct_size=sizeof(error);error.version=YOLO_ERROR_INFO_VERSION;
*owned=NULL;
if(yolo_mask_dilate(mask,radius,region,NULL,0,0,&output,&error)!=YOLO_STATUS_OK) {
fprintf(stderr,"Dilation query failed: %s\n",error.message);return -1;
}
const uint64_t bytes=(uint64_t)output.width*(uint64_t)output.height;
if(bytes>SIZE_MAX) return -1;
*owned=(unsigned char*)malloc((size_t)bytes);
if(!*owned) return -1;
if(yolo_mask_dilate(mask,radius,region,*owned,output.width,bytes,&output,&error)!=YOLO_STATUS_OK) {
fprintf(stderr,"Dilation failed: %s\n",error.message);free(*owned);*owned=NULL;return -1;
}
*mask=output;
return 0;
}