KiCad PCB EDA Suite
decompose.h File Reference
#include "curve.h"
#include "potracelib.h"
#include "progress.h"

Go to the source code of this file.

Functions

int bm_to_pathlist (const potrace_bitmap_t *bm, path_t **plistp, const potrace_param_t *param, progress_t *progress)
 

Function Documentation

int bm_to_pathlist ( const potrace_bitmap_t bm,
path_t **  plistp,
const potrace_param_t param,
progress_t progress 
)

Definition at line 565 of file decompose.cpp.

References potrace_path_s::area, bm_clearexcess(), bm_dup(), bm_free(), BM_GET, findnext(), findpath(), potrace_bitmap_s::h, list_forall_unlink, list_insert_beforehook, path_free(), pathlist_to_tree(), progress_update(), sign(), potrace_param_s::turdsize, potrace_param_s::turnpolicy, and xor_path().

Referenced by potrace_trace().

567 {
568  int x;
569  int y;
570  path_t* p;
571  path_t* plist = NULL; /* linked list of path objects */
572  path_t** plist_hook = &plist; /* used to speed up appending to linked list */
573  potrace_bitmap_t* bm1 = NULL;
574  int sign;
575 
576  bm1 = bm_dup( bm );
577 
578  if( !bm1 )
579  {
580  goto error;
581  }
582 
583  /* be sure the byte padding on the right is set to 0, as the fast
584  * pixel search below relies on it */
585  bm_clearexcess( bm1 );
586 
587  /* iterate through components */
588  x = 0;
589  y = bm1->h - 1;
590 
591  while( findnext( bm1, &x, &y ) == 0 )
592  {
593  /* calculate the sign by looking at the original */
594  sign = BM_GET( bm, x, y ) ? '+' : '-';
595 
596  /* calculate the path */
597  p = findpath( bm1, x, y + 1, sign, param->turnpolicy );
598 
599  if( p == NULL )
600  {
601  goto error;
602  }
603 
604  /* update buffered image */
605  xor_path( bm1, p );
606 
607  /* if it's a turd, eliminate it, else append it to the list */
608  if( p->area <= param->turdsize )
609  {
610  path_free( p );
611  }
612  else
613  {
614  list_insert_beforehook( p, plist_hook );
615  }
616 
617  if( bm1->h > 0 )
618  {
619  /* to be sure */
620  progress_update( 1 - y / (double) bm1->h, progress );
621  }
622  }
623 
624  pathlist_to_tree( plist, bm1 );
625  bm_free( bm1 );
626  *plistp = plist;
627 
628  progress_update( 1.0, progress );
629 
630  return 0;
631 
632 error:
633  bm_free( bm1 );
634  list_forall_unlink( p, plist ) {
635  path_free( p );
636  }
637  return -1;
638 }
static void bm_free(potrace_bitmap_t *bm)
Definition: bitmap.h:102
#define list_forall_unlink(elt, list)
Definition: lists.h:186
static potrace_bitmap_t * bm_dup(const potrace_bitmap_t *bm)
Definition: bitmap.h:170
static path_t * findpath(potrace_bitmap_t *bm, int x0, int y0, int sign, int turnpolicy)
Definition: decompose.cpp:255
static void pathlist_to_tree(path_t *plist, potrace_bitmap_t *bm)
Definition: decompose.cpp:382
#define BM_GET(bm, x, y)
Definition: bitmap.h:42
static int findnext(potrace_bitmap_t *bm, int *xp, int *yp)
Definition: decompose.cpp:526
void path_free(path_t *p)
Definition: curve.cpp:58
static void bm_clearexcess(potrace_bitmap_t *bm)
Definition: decompose.cpp:60
static void xor_path(potrace_bitmap_t *bm, path_t *p)
Definition: decompose.cpp:180
static void progress_update(double d, progress_t *prog)
Definition: progress.h:29
#define list_insert_beforehook(elt, hook)
Definition: lists.h:79
int sign(T val)
Definition: math_util.h:44