Skip to content

Commit 6eb1cdd

Browse files
committed
avoid large stackframe
1 parent a9667d7 commit 6eb1cdd

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/sdfimage.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,28 @@ edt(double *data, int width, int height, double *f, double *d, int *v, double *z
7474
}
7575
}
7676

77+
struct sdf_context {
78+
double f[MAX_SIZE];
79+
double d[MAX_SIZE];
80+
double z[MAX_SIZE+1];
81+
int v[MAX_SIZE];
82+
};
83+
7784
static void
7885
sdf_convert(unsigned char *bytes, int x, int y, double radius, double cutoff) {
7986
int i;
8087
int size = (x > y) ? x : y;
8188
assert(size <= MAX_SIZE);
8289
int length = x * y;
83-
double *data = (double *)malloc(length * sizeof(double) * 3);
90+
double *data = (double *)malloc(length * sizeof(double) * 3 + sizeof(struct sdf_context));
8491
for (i = 0; i < length; i++) {
8592
// For white background, negative image
8693
data[i] = (255 - bytes[i]) / 255.0;
8794
}
8895
double *gridOuter = data + length;
8996
double *gridInner = gridOuter + length;
90-
double f[MAX_SIZE];
91-
double d[MAX_SIZE];
92-
double z[MAX_SIZE+1];
93-
int v[MAX_SIZE];
94-
97+
struct sdf_context * ctx = (struct sdf_context *)(gridInner + length);
98+
9599
for (i = 0; i < length; i++) {
96100
double a = data[i];
97101
if (a >= 0.5) {
@@ -113,8 +117,8 @@ sdf_convert(unsigned char *bytes, int x, int y, double radius, double cutoff) {
113117
}
114118
}
115119

116-
edt(gridOuter, x, y, f, d, v, z);
117-
edt(gridInner, x, y, f, d, v, z);
120+
edt(gridOuter, x, y, ctx->f, ctx->d, ctx->v, ctx->z);
121+
edt(gridInner, x, y, ctx->f, ctx->d, ctx->v, ctx->z);
118122

119123
for (i = 0; i < length; i++) {
120124
double v = (gridOuter[i] - gridInner[i]) / radius + cutoff;

0 commit comments

Comments
 (0)