Skip to content

Commit

Permalink
common/maskApi: removing some c99 dependencies :-/
Browse files Browse the repository at this point in the history
  • Loading branch information
pdollar committed May 6, 2016
1 parent 7ac5017 commit cdbf564
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
36 changes: 18 additions & 18 deletions common/maskApi.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ uint umax( uint a, uint b ) { return (a>b) ? a : b; }

void rleInit( RLE *R, siz h, siz w, siz m, uint *cnts ) {
R->h=h; R->w=w; R->m=m; R->cnts=(m==0)?0:malloc(sizeof(uint)*m);
if(cnts) for(siz j=0; j<m; j++) R->cnts[j]=cnts[j];
siz j; if(cnts) for(j=0; j<m; j++) R->cnts[j]=cnts[j];
}

void rleFree( RLE *R ) {
free(R->cnts); R->cnts=0;
}

void rlesInit( RLE **R, siz n ) {
*R = (RLE*) malloc(sizeof(RLE)*n);
for(siz i=0; i<n; i++) rleInit((*R)+i,0,0,0,0);
siz i; *R = (RLE*) malloc(sizeof(RLE)*n);
for(i=0; i<n; i++) rleInit((*R)+i,0,0,0,0);
}

void rlesFree( RLE **R, siz n ) {
for(siz i=0; i<n; i++) rleFree((*R)+i); free(*R); *R=0;
siz i; for(i=0; i<n; i++) rleFree((*R)+i); free(*R); *R=0;
}

void rleEncode( RLE *R, const byte *M, siz h, siz w, siz n ) {
Expand All @@ -41,9 +41,9 @@ void rleEncode( RLE *R, const byte *M, siz h, siz w, siz n ) {
}

void rleDecode( const RLE *R, byte *M, siz n ) {
for( siz i=0; i<n; i++ ) {
byte v=0; for( siz j=0; j<R[i].m; j++ ) {
for( siz k=0; k<R[i].cnts[j]; k++ ) *(M++)=v; v=!v; }}
siz i, j, k; for( i=0; i<n; i++ ) {
byte v=0; for( j=0; j<R[i].m; j++ ) {
for( k=0; k<R[i].cnts[j]; k++ ) *(M++)=v; v=!v; }}
}

void rleMerge( const RLE *R, RLE *M, siz n, int intersect ) {
Expand All @@ -70,8 +70,8 @@ void rleMerge( const RLE *R, RLE *M, siz n, int intersect ) {
}

void rleArea( const RLE *R, siz n, uint *a ) {
for( siz i=0; i<n; i++ ) {
a[i]=0; for( siz j=1; j<R[i].m; j+=2 ) a[i]+=R[i].cnts[j]; }
siz i, j; for( i=0; i<n; i++ ) {
a[i]=0; for( j=1; j<R[i].m; j+=2 ) a[i]+=R[i].cnts[j]; }
}

void rleIou( RLE *dt, RLE *gt, siz m, siz n, byte *iscrowd, double *o ) {
Expand Down Expand Up @@ -109,7 +109,7 @@ void bbIou( BB dt, BB gt, siz m, siz n, byte *iscrowd, double *o ) {
}

void rleToBbox( const RLE *R, BB bb, siz n ) {
for( siz i=0; i<n; i++ ) {
siz i; for( i=0; i<n; i++ ) {
uint h, w, x, y, xs, ys, xe, ye, cc, t; siz j, m;
h=(uint)R[i].h; w=(uint)R[i].w; m=R[i].m;
m=((siz)(m/2))*2; xs=w; ys=h; xe=ye=0; cc=0;
Expand All @@ -124,7 +124,7 @@ void rleToBbox( const RLE *R, BB bb, siz n ) {
}

void rleFrBbox( RLE *R, const BB bb, siz h, siz w, siz n ) {
for( siz i=0; i<n; i++ ) {
siz i; for( i=0; i<n; i++ ) {
double xs=bb[4*i+0], xe=xs+bb[4*i+2];
double ys=bb[4*i+1], ye=ys+bb[4*i+3];
double xy[8] = {xs,ys,xs,ye,xe,ye,xe,ys};
Expand All @@ -137,26 +137,26 @@ int uintCompare(const void *a, const void *b) {
}

void rleFrPoly( RLE *R, const double *xy, siz k, siz h, siz w ) {
// upsample and get discrete points densely along entire boundary
/* upsample and get discrete points densely along entire boundary */
siz j, m=0; double scale=5; int *x, *y, *u, *v; uint *a, *b;
x=malloc(sizeof(int)*(k+1)); y=malloc(sizeof(int)*(k+1));
for(j=0; j<k; j++) x[j]=(int)(scale*xy[j*2+0]+.5); x[k]=x[0];
for(j=0; j<k; j++) y[j]=(int)(scale*xy[j*2+1]+.5); y[k]=y[0];
for(j=0; j<k; j++) m+=umax(abs(x[j]-x[j+1]),abs(y[j]-y[j+1]))+1;
u=malloc(sizeof(int)*m); v=malloc(sizeof(int)*m); m=0;
for( j=0; j<k; j++ ) {
int xs=x[j], xe=x[j+1], ys=y[j], ye=y[j+1], dx, dy, t;
int xs=x[j], xe=x[j+1], ys=y[j], ye=y[j+1], dx, dy, t, d;
int flip; double s; dx=abs(xe-xs); dy=abs(ys-ye);
flip = (dx>=dy && xs>xe) || (dx<dy && ys>ye);
if(flip) { t=xs; xs=xe; xe=t; t=ys; ys=ye; ye=t; }
s = dx>=dy ? (double)(ye-ys)/dx : (double)(xe-xs)/dy;
if(dx>=dy) for( int d=0; d<=dx; d++ ) {
if(dx>=dy) for( d=0; d<=dx; d++ ) {
t=flip?dx-d:d; u[m]=t+xs; v[m]=(int)(ys+s*t+.5); m++;
} else for( int d=0; d<=dy; d++ ) {
} else for( d=0; d<=dy; d++ ) {
t=flip?dy-d:d; v[m]=t+ys; u[m]=(int)(xs+s*t+.5); m++;
}
}
// get points along y-boundary and downsample
/* get points along y-boundary and downsample */
free(x); free(y); k=m; m=0; double xd, yd;
x=malloc(sizeof(int)*k); y=malloc(sizeof(int)*k);
for( j=1; j<k; j++ ) if(u[j]!=u[j-1]) {
Expand All @@ -166,7 +166,7 @@ void rleFrPoly( RLE *R, const double *xy, siz k, siz h, siz w ) {
if(yd<0) yd=0; else if(yd>h) yd=h; yd=ceil(yd);
x[m]=(int) xd; y[m]=(int) yd; m++;
}
// compute rle encoding given y-boundary points
/* compute rle encoding given y-boundary points */
k=m; a=malloc(sizeof(uint)*(k+1));
for( j=0; j<k; j++ ) a[j]=(uint)(x[j]*(int)(h)+y[j]);
a[k++]=(uint)(h*w); free(u); free(v); free(x); free(y);
Expand All @@ -179,7 +179,7 @@ void rleFrPoly( RLE *R, const double *xy, siz k, siz h, siz w ) {
}

char* rleToString( const RLE *R ) {
// Similar to LEB128 but using 6 bits/char and ascii chars 48-111.
/* Similar to LEB128 but using 6 bits/char and ascii chars 48-111. */
siz i, m=R->m, p=0; long x; int more;
char *s=malloc(sizeof(char)*m*6);
for( i=0; i<m; i++ ) {
Expand Down
26 changes: 13 additions & 13 deletions common/maskApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,43 @@ typedef unsigned char byte;
typedef double* BB;
typedef struct { siz h, w, m; uint *cnts; } RLE;

// Initialize/destroy RLE.
/* Initialize/destroy RLE. */
void rleInit( RLE *R, siz h, siz w, siz m, uint *cnts );
void rleFree( RLE *R );

// Initialize/destroy RLE array.
/* Initialize/destroy RLE array. */
void rlesInit( RLE **R, siz n );
void rlesFree( RLE **R, siz n );

// Encode binary masks using RLE.
/* Encode binary masks using RLE. */
void rleEncode( RLE *R, const byte *mask, siz h, siz w, siz n );

// Decode binary masks encoded via RLE.
/* Decode binary masks encoded via RLE. */
void rleDecode( const RLE *R, byte *mask, siz n );

// Compute union or intersection of encoded masks.
/* Compute union or intersection of encoded masks. */
void rleMerge( const RLE *R, RLE *M, siz n, int intersect );

// Compute area of encoded masks.
/* Compute area of encoded masks. */
void rleArea( const RLE *R, siz n, uint *a );

// Compute intersection over union between masks.
/* Compute intersection over union between masks. */
void rleIou( RLE *dt, RLE *gt, siz m, siz n, byte *iscrowd, double *o );

// Compute intersection over union between bounding boxes.
/* Compute intersection over union between bounding boxes. */
void bbIou( BB dt, BB gt, siz m, siz n, byte *iscrowd, double *o );

// Get bounding boxes surrounding encoded masks.
/* Get bounding boxes surrounding encoded masks. */
void rleToBbox( const RLE *R, BB bb, siz n );

// Convert bounding boxes to encoded masks.
/* Convert bounding boxes to encoded masks. */
void rleFrBbox( RLE *R, const BB bb, siz h, siz w, siz n );

// Convert polygon to encoded mask.
/* Convert polygon to encoded mask. */
void rleFrPoly( RLE *R, const double *xy, siz k, siz h, siz w );

// Get compressed string representation of encoded mask.
/* Get compressed string representation of encoded mask. */
char* rleToString( const RLE *R );

// Convert from compressed string representation of encoded mask.
/* Convert from compressed string representation of encoded mask. */
void rleFrString( RLE *R, char *s, siz h, siz w );

0 comments on commit cdbf564

Please sign in to comment.