backend_pgm.c
Go to the documentation of this file.
00001 /* Copyright (C) 2001-2007 Peter Selinger.
00002    This file is part of Potrace. It is free software and it is covered
00003    by the GNU General Public License. See the file COPYING for details. */
00004 
00005 /* $Id: backend_pgm.c 147 2007-04-09 00:44:09Z selinger $ */
00006 
00007 /* The PGM backend of Potrace. Here we custom-render a set of Bezier
00008    curves and output the result as a greymap. This is merely a
00009    convenience, as the same could be achieved by piping the EPS output
00010    through ghostscript. */
00011 
00012 #include <math.h>
00013 
00014 #include "backend_pgm.h"
00015 #include "potracelib.h"
00016 #include "lists.h"
00017 #include "greymap.h"
00018 #include "render.h"
00019 #include "main.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 /* structure to hold an affine coordinate transformation */
00031 struct trans_s {
00032   double ox, oy;             /* origin */
00033   double dxx, dxy, dyx, dyy; /* transformation matrix */
00034 };
00035 typedef struct trans_s trans_t;
00036 
00037 static inline dpoint_t trans(dpoint_t p, trans_t t) {
00038   dpoint_t res;
00039 
00040   res.x = t.ox + p.x * t.dxx + p.y * t.dyx;
00041   res.y = t.oy + p.x * t.dxy + p.y * t.dyy;
00042   return res;
00043 }
00044 
00045 static void pgm_path(potrace_curve_t *curve, trans_t t, render_t *rm) {
00046   dpoint_t *c, c1[3];
00047   int i;
00048   int m = curve->n;
00049   
00050   c = curve->c[m-1];
00051   c1[2] = trans(c[2], t);
00052   render_moveto(rm, c1[2].x, c1[2].y);
00053   
00054   for (i=0; i<m; i++) {
00055     c = curve->c[i];
00056     switch (curve->tag[i]) {
00057     case POTRACE_CORNER:
00058       c1[1] = trans(c[1], t);
00059       c1[2] = trans(c[2], t);
00060       render_lineto(rm, c1[1].x, c1[1].y);
00061       render_lineto(rm, c1[2].x, c1[2].y);
00062       break;
00063     case POTRACE_CURVETO:
00064       c1[0] = trans(c[0], t);
00065       c1[1] = trans(c[1], t);
00066       c1[2] = trans(c[2], t);
00067       render_curveto(rm, c1[0].x, c1[0].y, c1[1].x, c1[1].y, c1[2].x, c1[2].y);
00068       break;
00069     }
00070   }
00071 }
00072 
00073 int page_pgm(FILE *fout, potrace_path_t *plist, imginfo_t *imginfo) {
00074   potrace_path_t *p;
00075   greymap_t *gm;
00076   render_t *rm;
00077   int w = (int)ceil(imginfo->trans.bb[0]+imginfo->lmar+imginfo->rmar);
00078   int h = (int)ceil(imginfo->trans.bb[1]+imginfo->tmar+imginfo->bmar);
00079   double xs, ys;  /* scaling factors */
00080   double si, co;
00081   trans_t t;
00082   int mode;
00083   char *comment = "created by "POTRACE" "VERSION", written by Peter Selinger 2001-2007";
00084 
00085   si = sin(info.angle/180*M_PI);
00086   co = cos(info.angle/180*M_PI);
00087 
00088   t.ox = imginfo->trans.orig[0]+imginfo->lmar;
00089   t.oy = imginfo->trans.orig[1]+imginfo->bmar;
00090 
00091   xs = imginfo->width / imginfo->pixwidth;
00092   ys = imginfo->height / imginfo->pixheight;
00093 
00094   t.dxx = co * xs;
00095   t.dxy = si * xs;
00096   t.dyx = -si * ys;
00097   t.dyy = co * ys;
00098 
00099   gm = gm_new(w, h);
00100   if (!gm) {
00101     return 1;
00102   }
00103   rm = render_new(gm);
00104   if (!rm) {
00105     return 1;
00106   }
00107 
00108   gm_clear(gm, 255); /* white */
00109 
00110   list_forall(p, plist) {
00111     pgm_path(&p->curve, t, rm);
00112   }
00113 
00114   render_close(rm);
00115 
00116   /* if negative orientation, make sure to invert effect of rendering */
00117   mode = xs * ys < 0 ? GM_MODE_NEGATIVE : GM_MODE_POSITIVE;
00118 
00119   gm_writepgm(fout, rm->gm, comment, 1, mode, info.gamma);
00120 
00121   render_free(rm);
00122   gm_free(gm);
00123 
00124   return 0;
00125 }
00126 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


portrait_painter
Author(s): Niklas Meinzer, Ina Baumgarten
autogenerated on Wed Dec 26 2012 16:00:42