gvsig-raster / libjni-potrace / trunk / libjni-potrace / resources / potrace-1.8 / src / potracelib_demo.c @ 1780
History | View | Annotate | Download (3.83 KB)
1 |
/* Copyright (C) 2001-2007 Peter Selinger.
|
---|---|
2 |
This file is part of Potrace. It is free software and it is covered
|
3 |
by the GNU General Public License. See the file COPYING for details. */
|
4 |
|
5 |
/* A simple and self-contained demo of the potracelib API */
|
6 |
|
7 |
#include <stdio.h> |
8 |
#include <string.h> |
9 |
#include <errno.h> |
10 |
#include <stdlib.h> |
11 |
|
12 |
#include "potracelib.h" |
13 |
|
14 |
#define WIDTH 250 |
15 |
#define HEIGHT 250 |
16 |
|
17 |
/* ---------------------------------------------------------------------- */
|
18 |
/* auxiliary bitmap functions */
|
19 |
|
20 |
/* macros for writing individual bitmap pixels */
|
21 |
#define BM_WORDSIZE ((int)sizeof(potrace_word)) |
22 |
#define BM_WORDBITS (8*BM_WORDSIZE) |
23 |
#define BM_HIBIT (((potrace_word)1)<<(BM_WORDBITS-1)) |
24 |
#define bm_scanline(bm, y) ((bm)->map + (y)*(bm)->dy)
|
25 |
#define bm_index(bm, x, y) (&bm_scanline(bm, y)[(x)/BM_WORDBITS])
|
26 |
#define bm_mask(x) (BM_HIBIT >> ((x) & (BM_WORDBITS-1))) |
27 |
#define bm_range(x, a) ((int)(x) >= 0 && (int)(x) < (a)) |
28 |
#define bm_safe(bm, x, y) (bm_range(x, (bm)->w) && bm_range(y, (bm)->h))
|
29 |
#define BM_USET(bm, x, y) (*bm_index(bm, x, y) |= bm_mask(x))
|
30 |
#define BM_UCLR(bm, x, y) (*bm_index(bm, x, y) &= ~bm_mask(x))
|
31 |
#define BM_UPUT(bm, x, y, b) ((b) ? BM_USET(bm, x, y) : BM_UCLR(bm, x, y))
|
32 |
#define BM_PUT(bm, x, y, b) (bm_safe(bm, x, y) ? BM_UPUT(bm, x, y, b) : 0) |
33 |
|
34 |
/* return new un-initialized bitmap. NULL with errno on error */
|
35 |
static potrace_bitmap_t *bm_new(int w, int h) { |
36 |
potrace_bitmap_t *bm; |
37 |
int dy = (w + BM_WORDBITS - 1) / BM_WORDBITS; |
38 |
|
39 |
bm = (potrace_bitmap_t *) malloc(sizeof(potrace_bitmap_t));
|
40 |
if (!bm) {
|
41 |
return NULL; |
42 |
} |
43 |
bm->w = w; |
44 |
bm->h = h; |
45 |
bm->dy = dy; |
46 |
bm->map = (potrace_word *) malloc(dy * h * BM_WORDSIZE); |
47 |
if (!bm->map) {
|
48 |
free(bm); |
49 |
return NULL; |
50 |
} |
51 |
return bm;
|
52 |
} |
53 |
|
54 |
/* free a bitmap */
|
55 |
static void bm_free(potrace_bitmap_t *bm) { |
56 |
if (bm != NULL) { |
57 |
free(bm->map); |
58 |
} |
59 |
free(bm); |
60 |
} |
61 |
|
62 |
/* ---------------------------------------------------------------------- */
|
63 |
/* demo */
|
64 |
|
65 |
int main() {
|
66 |
int x, y, i;
|
67 |
potrace_bitmap_t *bm; |
68 |
potrace_param_t *param; |
69 |
potrace_path_t *p; |
70 |
potrace_state_t *st; |
71 |
int n, *tag;
|
72 |
potrace_dpoint_t (*c)[3];
|
73 |
|
74 |
/* create a bitmap */
|
75 |
bm = bm_new(WIDTH, HEIGHT); |
76 |
if (!bm) {
|
77 |
fprintf(stderr, "Error allocating bitmap: %s\n", strerror(errno));
|
78 |
return 1; |
79 |
} |
80 |
|
81 |
/* fill the bitmap with some pattern */
|
82 |
for (y=0; y<HEIGHT; y++) { |
83 |
for (x=0; x<WIDTH; x++) { |
84 |
BM_PUT(bm, x, y, ((x*x + y*y*y) % 10000 < 5000) ? 1 : 0); |
85 |
} |
86 |
} |
87 |
|
88 |
/* set tracing parameters, starting from defaults */
|
89 |
param = potrace_param_default(); |
90 |
if (!param) {
|
91 |
fprintf(stderr, "Error allocating parameters: %s\n", strerror(errno));
|
92 |
return 1; |
93 |
} |
94 |
param->turdsize = 0;
|
95 |
|
96 |
/* trace the bitmap */
|
97 |
st = potrace_trace(param, bm); |
98 |
if (!st || st->status != POTRACE_STATUS_OK) {
|
99 |
fprintf(stderr, "Error tracing bitmap: %s\n", strerror(errno));
|
100 |
return 1; |
101 |
} |
102 |
bm_free(bm); |
103 |
|
104 |
/* output vector data, e.g. as a rudimentary EPS file */
|
105 |
printf("%%!PS-Adobe-3.0 EPSF-3.0\n");
|
106 |
printf("%%%%BoundingBox: 0 0 %d %d\n", WIDTH, HEIGHT);
|
107 |
printf("gsave\n");
|
108 |
|
109 |
/* draw each curve */
|
110 |
p = st->plist; |
111 |
while (p != NULL) { |
112 |
n = p->curve.n; |
113 |
tag = p->curve.tag; |
114 |
c = p->curve.c; |
115 |
printf("%f %f moveto\n", c[n-1][2].x, c[n-1][2].y); |
116 |
for (i=0; i<n; i++) { |
117 |
switch (tag[i]) {
|
118 |
case POTRACE_CORNER:
|
119 |
printf("%f %f lineto\n", c[i][1].x, c[i][1].y); |
120 |
printf("%f %f lineto\n", c[i][2].x, c[i][2].y); |
121 |
break;
|
122 |
case POTRACE_CURVETO:
|
123 |
printf("%f %f %f %f %f %f curveto\n",
|
124 |
c[i][0].x, c[i][0].y, |
125 |
c[i][1].x, c[i][1].y, |
126 |
c[i][2].x, c[i][2].y); |
127 |
break;
|
128 |
} |
129 |
} |
130 |
/* at the end of a group of a positive path and its negative
|
131 |
children, fill. */
|
132 |
if (p->next == NULL || p->next->sign == '+') { |
133 |
printf("0 setgray fill\n");
|
134 |
} |
135 |
p = p->next; |
136 |
} |
137 |
printf("grestore\n");
|
138 |
printf("%%EOF\n");
|
139 |
|
140 |
potrace_state_free(st); |
141 |
potrace_param_free(param); |
142 |
|
143 |
return 0; |
144 |
} |