| 1 |
/* From potracelib_demo.c */ |
| 2 |
|
| 3 |
#include "luafunc.h" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
/* ---------------------------------------------------------------------- */ |
| 8 |
/* auxiliary bitmap functions */ |
| 9 |
|
| 10 |
|
| 11 |
/* macros for writing individual bitmap pixels */ |
| 12 |
#define BM_WORDSIZE ((int)sizeof(potrace_word)) |
| 13 |
#define BM_WORDBITS (8*BM_WORDSIZE) |
| 14 |
#define BM_HIBIT (((potrace_word)1)<<(BM_WORDBITS-1)) |
| 15 |
#define bm_scanline(bm, y) ((bm)->map + (y)*(bm)->dy) |
| 16 |
#define bm_index(bm, x, y) (&bm_scanline(bm, y)[(x)/BM_WORDBITS]) |
| 17 |
#define bm_mask(x) (BM_HIBIT >> ((x) & (BM_WORDBITS-1))) |
| 18 |
#define bm_range(x, a) ((int)(x) >= 0 && (int)(x) < (a)) |
| 19 |
#define bm_safe(bm, x, y) (bm_range(x, (bm)->w) && bm_range(y, (bm)->h)) |
| 20 |
#define BM_USET(bm, x, y) (*bm_index(bm, x, y) |= bm_mask(x)) |
| 21 |
#define BM_UCLR(bm, x, y) (*bm_index(bm, x, y) &= ~bm_mask(x)) |
| 22 |
#define BM_UPUT(bm, x, y, b) ((b) ? BM_USET(bm, x, y) : BM_UCLR(bm, x, y)) |
| 23 |
#define BM_PUT(bm, x, y, b) (bm_safe(bm, x, y) ? BM_UPUT(bm, x, y, b) : 0) |
| 24 |
|
| 25 |
/* return new un-initialized bitmap. NULL with errno on error */ |
| 26 |
static potrace_bitmap_t *bm_new(int w, int h) { |
| 27 |
potrace_bitmap_t *bm; |
| 28 |
int dy = (w + BM_WORDBITS - 1) / BM_WORDBITS; |
| 29 |
|
| 30 |
bm = (potrace_bitmap_t *) malloc(sizeof(potrace_bitmap_t)); |
| 31 |
if (!bm) { |
| 32 |
return NULL; |
| 33 |
} |
| 34 |
bm->w = w; |
| 35 |
bm->h = h; |
| 36 |
bm->dy = dy; |
| 37 |
bm->map = (potrace_word *) calloc(h, dy * BM_WORDSIZE); |
| 38 |
if (!bm->map) { |
| 39 |
free(bm); |
| 40 |
return NULL; |
| 41 |
} |
| 42 |
return bm; |
| 43 |
} |
| 44 |
|
| 45 |
/* free a bitmap */ |
| 46 |
static void bm_free(potrace_bitmap_t *bm) { |
| 47 |
if (bm != NULL) { |
| 48 |
free(bm->map); |
| 49 |
} |
| 50 |
free(bm); |
| 51 |
} |
| 52 |
|
| 53 |
//int potrace_getMFoutlines(const uint8_t *raster,int w, int h) { |
| 54 |
int potrace_getMFoutlines(const uint8_t *raster, int w, int h, MFoutlines_param_t opt_param,potrace_state_t **trace) { |
| 55 |
int x, y; |
| 56 |
potrace_bitmap_t *bm; |
| 57 |
potrace_param_t *param; |
| 58 |
potrace_state_t *st; |
| 59 |
int W,X; |
| 60 |
uint8_t data,pix; |
| 61 |
|
| 62 |
/* create a bitmap */ |
| 63 |
bm = bm_new(w, h); |
| 64 |
if (!bm) { |
| 65 |
fprintf(stderr, "! Error allocating bitmap: %s\n", strerror(errno)); |
| 66 |
return 1; |
| 67 |
} |
| 68 |
|
| 69 |
/* fill the bitmap */ |
| 70 |
W= (w%8)==0 ?(w/8):(w/8+1); |
| 71 |
for (y=0; y<h; y++) { |
| 72 |
for (X=0;X<W; X++) { |
| 73 |
data = raster[W*y+X]; |
| 74 |
pix = (data & (1<<7))==0 ? 0:1;x = 0+X*8;BM_PUT(bm, x, y, pix); |
| 75 |
pix = (data & (1<<6))==0 ? 0:1;x = 1+X*8;BM_PUT(bm, x, y, pix); |
| 76 |
pix = (data & (1<<5))==0 ? 0:1;x = 2+X*8;BM_PUT(bm, x, y, pix); |
| 77 |
pix = (data & (1<<4))==0 ? 0:1;x = 3+X*8;BM_PUT(bm, x, y, pix); |
| 78 |
pix = (data & (1<<3))==0 ? 0:1;x = 4+X*8;BM_PUT(bm, x, y, pix); |
| 79 |
pix = (data & (1<<2))==0 ? 0:1;x = 5+X*8;BM_PUT(bm, x, y, pix); |
| 80 |
pix = (data & (1<<1))==0 ? 0:1;x = 6+X*8;BM_PUT(bm, x, y, pix); |
| 81 |
pix = (data & (1<<0))==0 ? 0:1;x = 7+X*8;BM_PUT(bm, x, y, pix); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/* set tracing parameters, starting from defaults */ |
| 86 |
if (opt_param.potrace_param == NULL) { |
| 87 |
param = potrace_param_default();printf("opt_param.potrace_param == NULL\n"); |
| 88 |
if (!param) { |
| 89 |
fprintf(stderr, "! Error allocating parameters: %s\n", strerror(errno)); |
| 90 |
return 1; |
| 91 |
} |
| 92 |
param->turdsize = 0; |
| 93 |
} else { |
| 94 |
param = opt_param.potrace_param; |
| 95 |
} |
| 96 |
/*printf("turdsize=%d;turnpolicy=%d;alphamax=%f;opticurve=%d;opttolerance=%f\n",param->turdsize,param->turnpolicy,param->alphamax,param->opticurve,param->opttolerance);*/ |
| 97 |
|
| 98 |
/* trace the bitmap */ |
| 99 |
st = potrace_trace(param, bm); |
| 100 |
if (!st || st->status != POTRACE_STATUS_OK) { |
| 101 |
fprintf(stderr, "! Error tracing bitmap: %s\n", strerror(errno)); |
| 102 |
bm_free(bm); |
| 103 |
return 1; |
| 104 |
} |
| 105 |
bm_free(bm); |
| 106 |
*trace = st ; |
| 107 |
return 0; |
| 108 |
} |