gvsig-raster / libjni-potrace / trunk / libjni-potrace / resources / potrace-1.8 / src / backend_pgm.c @ 1780
History | View | Annotate | Download (3.01 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 |
/* $Id: backend_pgm.c 147 2007-04-09 00:44:09Z selinger $ */
|
6 |
|
7 |
/* The PGM backend of Potrace. Here we custom-render a set of Bezier
|
8 |
curves and output the result as a greymap. This is merely a
|
9 |
convenience, as the same could be achieved by piping the EPS output
|
10 |
through ghostscript. */
|
11 |
|
12 |
#include <math.h> |
13 |
|
14 |
#include "backend_pgm.h" |
15 |
#include "potracelib.h" |
16 |
#include "lists.h" |
17 |
#include "greymap.h" |
18 |
#include "render.h" |
19 |
#include "main.h" |
20 |
#include "auxiliary.h" |
21 |
|
22 |
#ifdef HAVE_CONFIG_H
|
23 |
#include "config.h" |
24 |
#endif
|
25 |
|
26 |
#ifndef M_PI
|
27 |
#define M_PI 3.14159265358979323846 |
28 |
#endif
|
29 |
|
30 |
/* structure to hold an affine coordinate transformation */
|
31 |
struct trans_s {
|
32 |
double ox, oy; /* origin */ |
33 |
double dxx, dxy, dyx, dyy; /* transformation matrix */ |
34 |
}; |
35 |
typedef struct trans_s trans_t; |
36 |
|
37 |
static inline dpoint_t trans(dpoint_t p, trans_t t) { |
38 |
dpoint_t res; |
39 |
|
40 |
res.x = t.ox + p.x * t.dxx + p.y * t.dyx; |
41 |
res.y = t.oy + p.x * t.dxy + p.y * t.dyy; |
42 |
return res;
|
43 |
} |
44 |
|
45 |
static void pgm_path(potrace_curve_t *curve, trans_t t, render_t *rm) { |
46 |
dpoint_t *c, c1[3];
|
47 |
int i;
|
48 |
int m = curve->n;
|
49 |
|
50 |
c = curve->c[m-1];
|
51 |
c1[2] = trans(c[2], t); |
52 |
render_moveto(rm, c1[2].x, c1[2].y); |
53 |
|
54 |
for (i=0; i<m; i++) { |
55 |
c = curve->c[i]; |
56 |
switch (curve->tag[i]) {
|
57 |
case POTRACE_CORNER:
|
58 |
c1[1] = trans(c[1], t); |
59 |
c1[2] = trans(c[2], t); |
60 |
render_lineto(rm, c1[1].x, c1[1].y); |
61 |
render_lineto(rm, c1[2].x, c1[2].y); |
62 |
break;
|
63 |
case POTRACE_CURVETO:
|
64 |
c1[0] = trans(c[0], t); |
65 |
c1[1] = trans(c[1], t); |
66 |
c1[2] = trans(c[2], t); |
67 |
render_curveto(rm, c1[0].x, c1[0].y, c1[1].x, c1[1].y, c1[2].x, c1[2].y); |
68 |
break;
|
69 |
} |
70 |
} |
71 |
} |
72 |
|
73 |
int page_pgm(FILE *fout, potrace_path_t *plist, imginfo_t *imginfo) {
|
74 |
potrace_path_t *p; |
75 |
greymap_t *gm; |
76 |
render_t *rm; |
77 |
int w = (int)ceil(imginfo->trans.bb[0]+imginfo->lmar+imginfo->rmar); |
78 |
int h = (int)ceil(imginfo->trans.bb[1]+imginfo->tmar+imginfo->bmar); |
79 |
double xs, ys; /* scaling factors */ |
80 |
double si, co;
|
81 |
trans_t t; |
82 |
int mode;
|
83 |
char *comment = "created by "POTRACE" "VERSION", written by Peter Selinger 2001-2007"; |
84 |
|
85 |
si = sin(info.angle/180*M_PI);
|
86 |
co = cos(info.angle/180*M_PI);
|
87 |
|
88 |
t.ox = imginfo->trans.orig[0]+imginfo->lmar;
|
89 |
t.oy = imginfo->trans.orig[1]+imginfo->bmar;
|
90 |
|
91 |
xs = imginfo->width / imginfo->pixwidth; |
92 |
ys = imginfo->height / imginfo->pixheight; |
93 |
|
94 |
t.dxx = co * xs; |
95 |
t.dxy = si * xs; |
96 |
t.dyx = -si * ys; |
97 |
t.dyy = co * ys; |
98 |
|
99 |
gm = gm_new(w, h); |
100 |
if (!gm) {
|
101 |
return 1; |
102 |
} |
103 |
rm = render_new(gm); |
104 |
if (!rm) {
|
105 |
return 1; |
106 |
} |
107 |
|
108 |
gm_clear(gm, 255); /* white */ |
109 |
|
110 |
list_forall(p, plist) { |
111 |
pgm_path(&p->curve, t, rm); |
112 |
} |
113 |
|
114 |
render_close(rm); |
115 |
|
116 |
/* if negative orientation, make sure to invert effect of rendering */
|
117 |
mode = xs * ys < 0 ? GM_MODE_NEGATIVE : GM_MODE_POSITIVE;
|
118 |
|
119 |
gm_writepgm(fout, rm->gm, comment, 1, mode, info.gamma);
|
120 |
|
121 |
render_free(rm); |
122 |
gm_free(gm); |
123 |
|
124 |
return 0; |
125 |
} |
126 |
|