00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <stdio.h>
00012 #include <stdarg.h>
00013 #include <string.h>
00014 #include <math.h>
00015
00016 #include "main.h"
00017 #include "backend_gimp.h"
00018 #include "potracelib.h"
00019 #include "lists.h"
00020 #include "auxiliary.h"
00021
00022 #ifdef HAVE_CONFIG_H
00023 #include "config.h"
00024 #endif
00025
00026 #ifndef M_PI
00027 #define M_PI 3.14159265358979323846
00028 #endif
00029
00030
00031
00032
00033
00034 struct trans_s {
00035 double ox, oy;
00036 double dxx, dxy, dyx, dyy;
00037 };
00038 typedef struct trans_s trans_t;
00039
00040 static inline dpoint_t trans(dpoint_t p, trans_t t) {
00041 dpoint_t res;
00042
00043 res.x = t.ox + p.x * t.dxx + p.y * t.dyx;
00044 res.y = t.oy + p.x * t.dxy + p.y * t.dyy;
00045 return res;
00046 }
00047
00048
00049 static inline point_t unit(dpoint_t p) {
00050 point_t q;
00051
00052 q.x = (long)(floor(p.x*info.unit+.5));
00053 q.y = (long)(floor(p.y*info.unit+.5));
00054 return q;
00055 }
00056
00057 static void gimppath_point(FILE *fout, int typ, dpoint_t p, trans_t t) {
00058 point_t q;
00059
00060 q = unit(trans(p, t));
00061
00062 fprintf(fout, "TYPE: %d X: %ld Y: %ld\n", typ, q.x, q.y);
00063 }
00064
00065
00066
00067
00068
00069 static int gimppath_path(FILE *fout, potrace_curve_t *curve, int first, trans_t t) {
00070 int i;
00071 dpoint_t *c, *c1;
00072 int m = curve->n;
00073
00074 first = first ? 1 : 3;
00075
00076 for (i=0; i<m; i++) {
00077 c = curve->c[i];
00078 c1 = curve->c[mod(i-1,m)];
00079 switch (curve->tag[i]) {
00080 case POTRACE_CORNER:
00081 gimppath_point(fout, first, c1[2], t);
00082 gimppath_point(fout, 2, c1[2], t);
00083 gimppath_point(fout, 2, c[1], t);
00084 gimppath_point(fout, 1, c[1], t);
00085 gimppath_point(fout, 2, c[1], t);
00086 gimppath_point(fout, 2, c[2], t);
00087 break;
00088 case POTRACE_CURVETO:
00089 gimppath_point(fout, first, c1[2], t);
00090 gimppath_point(fout, 2, c[0], t);
00091 gimppath_point(fout, 2, c[1], t);
00092 break;
00093 }
00094 first = 1;
00095 }
00096 return 0;
00097 }
00098
00099
00100 static int npoints(potrace_curve_t *curve) {
00101 int i;
00102 int n=0;
00103 int m = curve->n;
00104
00105 for (i=0; i<m; i++) {
00106 switch (curve->tag[i]) {
00107 case POTRACE_CORNER:
00108 n += 6;
00109 break;
00110 case POTRACE_CURVETO:
00111 n += 3;
00112 break;
00113 }
00114 }
00115 return n;
00116 }
00117
00118
00119
00120
00121
00122 int page_gimp(FILE *fout, potrace_path_t *plist, imginfo_t *imginfo) {
00123 potrace_path_t *p;
00124 int first = 1;
00125 int n;
00126 trans_t t;
00127 double si, co;
00128
00129
00130 n = 0;
00131 list_forall (p, plist) {
00132 n += npoints(&p->curve);
00133 }
00134
00135 si = sin(info.angle/180*M_PI);
00136 co = cos(info.angle/180*M_PI);
00137
00138 t.ox = max(0, si*imginfo->pixheight) + max(0, -co*imginfo->pixwidth);
00139 t.oy = max(0, co*imginfo->pixheight) + max(0, si*imginfo->pixwidth);
00140 t.dxx = co;
00141 t.dxy = -si;
00142 t.dyx = -si;
00143 t.dyy = -co;
00144
00145
00146 fprintf(fout, "Name: Potrace Imported Path\n");
00147 fprintf(fout, "#POINTS: %d\n", n);
00148 fprintf(fout, "CLOSED: 1\n");
00149 fprintf(fout, "DRAW: 0\n");
00150 fprintf(fout, "STATE: 4\n");
00151
00152
00153 list_forall (p, plist) {
00154 gimppath_path(fout, &p->curve, first, t);
00155 first = 0;
00156 }
00157 fflush(fout);
00158
00159 return 0;
00160 }
00161