int page_pgm(FILE *fout, potrace_path_t *plist, imginfo_t *imginfo) { potrace_path_t *p; greymap_t *gm; render_t *rm; int w, h; trans_t t; int mode; char *comment = "created by "POTRACE" "VERSION", written by Peter Selinger 2001-2015"; t.bb[0] = imginfo->trans.bb[0]+imginfo->lmar+imginfo->rmar; t.bb[1] = imginfo->trans.bb[1]+imginfo->tmar+imginfo->bmar; t.orig[0] = imginfo->trans.orig[0]+imginfo->lmar; t.orig[1] = imginfo->trans.orig[1]+imginfo->bmar; t.x[0] = imginfo->trans.x[0]; t.x[1] = imginfo->trans.x[1]; t.y[0] = imginfo->trans.y[0]; t.y[1] = imginfo->trans.y[1]; w = (int)ceil(t.bb[0]); h = (int)ceil(t.bb[1]); gm = gm_new(w, h); if (!gm) { return 1; } rm = render_new(gm); if (!rm) { return 1; } gm_clear(gm, 255); /* white */ list_forall(p, plist) { pgm_path(&p->curve, t, rm); }
static void *interpolate_cubic(greymap_t *gm, int s, int bilevel, double c) { int w, h; double4 *poly = NULL; /* poly[s][4]: fixed interpolation polynomials */ double p[4]; /* four current points */ double4 *window = NULL; /* window[s][4]: current state */ double t, v; int k, l, i, j, x, y; double c1 = 0; greymap_t *gm_out = NULL; potrace_bitmap_t *bm_out = NULL; SAFE_MALLOC(poly, s, double4); SAFE_MALLOC(window, s, double4); w = gm->w; h = gm->h; /* allocate output bitmap/greymap */ if (bilevel) { bm_out = bm_new(w*s, h*s); if (!bm_out) { goto malloc_error; } bm_clear(bm_out, 0); c1 = c * 255; } else { gm_out = gm_new(w*s, h*s); if (!gm_out) { goto malloc_error; } } /* pre-calculate interpolation polynomials */ for (k=0; k<s; k++) { t = k/(double)s; poly[k][0] = 0.5 * t * (t-1) * (1-t); poly[k][1] = -(t+1) * (t-1) * (1-t) + 0.5 * (t-1) * (t-2) * t; poly[k][2] = 0.5 * (t+1) * t * (1-t) - t * (t-2) * t; poly[k][3] = 0.5 * t * (t-1) * t; } /* interpolate */ for (y=0; y<h; y++) { x=0; for (i=0; i<4; i++) { for (j=0; j<4; j++) { p[j] = GM_BGET(gm, x+i-1, y+j-1); } for (k=0; k<s; k++) { window[k][i] = 0.0; for (j=0; j<4; j++) { window[k][i] += poly[k][j] * p[j]; } } } while (1) { for (l=0; l<s; l++) { for (k=0; k<s; k++) { v = 0.0; for (i=0; i<4; i++) { v += window[k][i] * poly[l][i]; } if (bilevel) { BM_UPUT(bm_out, x*s+l, y*s+k, v < c1); } else { GM_UPUT(gm_out, x*s+l, y*s+k, v); } } } x++; if (x>=w) { break; } for (i=0; i<3; i++) { for (k=0; k<s; k++) { window[k][i] = window[k][i+1]; } } i=3; for (j=0; j<4; j++) { p[j] = GM_BGET(gm, x+i-1, y+j-1); } for (k=0; k<s; k++) { window[k][i] = 0.0; for (j=0; j<4; j++) { window[k][i] += poly[k][j] * p[j]; } } } } free(poly); free(window); if (bilevel) { return (void *)bm_out; } else { return (void *)gm_out; } malloc_error: free(poly); free(window); return NULL; }
static void *interpolate_linear(greymap_t *gm, int s, int bilevel, double c) { int p00, p01, p10, p11; int i, j, x, y; double xx, yy, av; double c1 = 0; int w, h; double p0, p1; greymap_t *gm_out = NULL; potrace_bitmap_t *bm_out = NULL; w = gm->w; h = gm->h; /* allocate output bitmap/greymap */ if (bilevel) { bm_out = bm_new(w*s, h*s); if (!bm_out) { return NULL; } bm_clear(bm_out, 0); c1 = c * 255; } else { gm_out = gm_new(w*s, h*s); if (!gm_out) { return NULL; } } /* interpolate */ for (i=0; i<w; i++) { for (j=0; j<h; j++) { p00 = GM_BGET(gm, i, j); p01 = GM_BGET(gm, i, j+1); p10 = GM_BGET(gm, i+1, j); p11 = GM_BGET(gm, i+1, j+1); if (bilevel) { /* treat two special cases which are very common */ if (p00 < c1 && p01 < c1 && p10 < c1 && p11 < c1) { for (x=0; x<s; x++) { for (y=0; y<s; y++) { BM_UPUT(bm_out, i*s+x, j*s+y, 1); } } continue; } if (p00 >= c1 && p01 >= c1 && p10 >= c1 && p11 >= c1) { continue; } } /* the general case */ for (x=0; x<s; x++) { xx = x/(double)s; p0 = p00*(1-xx) + p10*xx; p1 = p01*(1-xx) + p11*xx; for (y=0; y<s; y++) { yy = y/(double)s; av = p0*(1-yy) + p1*yy; if (bilevel) { BM_UPUT(bm_out, i*s+x, j*s+y, av < c1); } else { GM_UPUT(gm_out, i*s+x, j*s+y, av); } } } } } if (bilevel) { return (void *)bm_out; } else { return (void *)gm_out; } }