potracelib_demo.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 /* A simple and self-contained demo of the potracelib API */
00006 
00007 #include <stdio.h>
00008 #include <string.h>
00009 #include <errno.h>
00010 #include <stdlib.h>
00011 
00012 #include "potracelib.h"
00013 
00014 #define WIDTH 250
00015 #define HEIGHT 250
00016 
00017 /* ---------------------------------------------------------------------- */
00018 /* auxiliary bitmap functions */
00019 
00020 /* macros for writing individual bitmap pixels */
00021 #define BM_WORDSIZE ((int)sizeof(potrace_word))
00022 #define BM_WORDBITS (8*BM_WORDSIZE)
00023 #define BM_HIBIT (((potrace_word)1)<<(BM_WORDBITS-1))
00024 #define bm_scanline(bm, y) ((bm)->map + (y)*(bm)->dy)
00025 #define bm_index(bm, x, y) (&bm_scanline(bm, y)[(x)/BM_WORDBITS])
00026 #define bm_mask(x) (BM_HIBIT >> ((x) & (BM_WORDBITS-1)))
00027 #define bm_range(x, a) ((int)(x) >= 0 && (int)(x) < (a))
00028 #define bm_safe(bm, x, y) (bm_range(x, (bm)->w) && bm_range(y, (bm)->h))
00029 #define BM_USET(bm, x, y) (*bm_index(bm, x, y) |= bm_mask(x))
00030 #define BM_UCLR(bm, x, y) (*bm_index(bm, x, y) &= ~bm_mask(x))
00031 #define BM_UPUT(bm, x, y, b) ((b) ? BM_USET(bm, x, y) : BM_UCLR(bm, x, y))
00032 #define BM_PUT(bm, x, y, b) (bm_safe(bm, x, y) ? BM_UPUT(bm, x, y, b) : 0)
00033 
00034 /* return new un-initialized bitmap. NULL with errno on error */
00035 static potrace_bitmap_t *bm_new(int w, int h) {
00036   potrace_bitmap_t *bm;
00037   int dy = (w + BM_WORDBITS - 1) / BM_WORDBITS;
00038 
00039   bm = (potrace_bitmap_t *) malloc(sizeof(potrace_bitmap_t));
00040   if (!bm) {
00041     return NULL;
00042   }
00043   bm->w = w;
00044   bm->h = h;
00045   bm->dy = dy;
00046   bm->map = (potrace_word *) malloc(dy * h * BM_WORDSIZE);
00047   if (!bm->map) {
00048     free(bm);
00049     return NULL;
00050   }
00051   return bm;
00052 }
00053 
00054 /* free a bitmap */
00055 static void bm_free(potrace_bitmap_t *bm) {
00056   if (bm != NULL) {
00057     free(bm->map);
00058   }
00059   free(bm);
00060 }
00061 
00062 /* ---------------------------------------------------------------------- */
00063 /* demo */
00064 
00065 int main() {
00066   int x, y, i;
00067   potrace_bitmap_t *bm;
00068   potrace_param_t *param;
00069   potrace_path_t *p;
00070   potrace_state_t *st;
00071   int n, *tag;
00072   potrace_dpoint_t (*c)[3];
00073 
00074   /* create a bitmap */
00075   bm = bm_new(WIDTH, HEIGHT);
00076   if (!bm) {
00077     fprintf(stderr, "Error allocating bitmap: %s\n", strerror(errno)); 
00078     return 1;
00079   }
00080 
00081   /* fill the bitmap with some pattern */
00082   for (y=0; y<HEIGHT; y++) {
00083     for (x=0; x<WIDTH; x++) {
00084       BM_PUT(bm, x, y, ((x*x + y*y*y) % 10000 < 5000) ? 1 : 0);
00085     }
00086   }
00087 
00088   /* set tracing parameters, starting from defaults */
00089   param = potrace_param_default();
00090   if (!param) {
00091     fprintf(stderr, "Error allocating parameters: %s\n", strerror(errno)); 
00092     return 1;
00093   }
00094   param->turdsize = 0;
00095 
00096   /* trace the bitmap */
00097   st = potrace_trace(param, bm);
00098   if (!st || st->status != POTRACE_STATUS_OK) {
00099     fprintf(stderr, "Error tracing bitmap: %s\n", strerror(errno));
00100     return 1;
00101   }
00102   bm_free(bm);
00103   
00104   /* output vector data, e.g. as a rudimentary EPS file */
00105   printf("%%!PS-Adobe-3.0 EPSF-3.0\n");
00106   printf("%%%%BoundingBox: 0 0 %d %d\n", WIDTH, HEIGHT);
00107   printf("gsave\n");
00108 
00109   /* draw each curve */
00110   p = st->plist;
00111   while (p != NULL) {
00112     n = p->curve.n;
00113     tag = p->curve.tag;
00114     c = p->curve.c;
00115     printf("%f %f moveto\n", c[n-1][2].x, c[n-1][2].y);
00116     for (i=0; i<n; i++) {
00117       switch (tag[i]) {
00118       case POTRACE_CORNER:
00119         printf("%f %f lineto\n", c[i][1].x, c[i][1].y);
00120         printf("%f %f lineto\n", c[i][2].x, c[i][2].y);
00121         break;
00122       case POTRACE_CURVETO:
00123         printf("%f %f %f %f %f %f curveto\n", 
00124                c[i][0].x, c[i][0].y,
00125                c[i][1].x, c[i][1].y,
00126                c[i][2].x, c[i][2].y);
00127         break;
00128       }
00129     }
00130     /* at the end of a group of a positive path and its negative
00131        children, fill. */
00132     if (p->next == NULL || p->next->sign == '+') {
00133       printf("0 setgray fill\n");
00134     }
00135     p = p->next;
00136   }
00137   printf("grestore\n");
00138   printf("%%EOF\n");
00139   
00140   potrace_state_free(st);
00141   potrace_param_free(param);
00142 
00143   return 0;
00144 }
 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:43