KiCad PCB EDA Suite
bitmap.h File Reference
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stddef.h>
#include "potracelib.h"

Go to the source code of this file.

Macros

#define BM_WORDSIZE   ( (int) sizeof(potrace_word) )
 
#define BM_WORDBITS   (8 * BM_WORDSIZE)
 
#define BM_HIBIT   ( ( (potrace_word) 1 ) << (BM_WORDBITS - 1) )
 
#define BM_ALLBITS   (~(potrace_word) 0)
 
#define bm_scanline(bm, y)   ( (bm)->map + (ptrdiff_t) (y) * (ptrdiff_t) (bm)->dy )
 
#define bm_index(bm, x, y)   (&bm_scanline( bm, y )[(x) / BM_WORDBITS])
 
#define bm_mask(x)   ( BM_HIBIT >> ( (x) & (BM_WORDBITS - 1) ) )
 
#define bm_range(x, a)   ( (int) (x) >= 0 && (int) (x) < (a) )
 
#define bm_safe(bm, x, y)   ( bm_range( x, (bm)->w ) && bm_range( y, (bm)->h ) )
 
#define BM_UGET(bm, x, y)   ( ( *bm_index( bm, x, y ) & bm_mask( x ) ) != 0 )
 
#define BM_USET(bm, x, y)   ( *bm_index( bm, x, y ) |= bm_mask( x ) )
 
#define BM_UCLR(bm, x, y)   ( *bm_index( bm, x, y ) &= ~bm_mask( x ) )
 
#define BM_UINV(bm, x, y)   ( *bm_index( bm, x, y ) ^= bm_mask( x ) )
 
#define BM_UPUT(bm, x, y, b)   ( (b) ? BM_USET( bm, x, y ) : BM_UCLR( bm, x, y ) )
 
#define BM_GET(bm, x, y)   (bm_safe( bm, x, y ) ? BM_UGET( bm, x, y ) : 0)
 
#define BM_SET(bm, x, y)   (bm_safe( bm, x, y ) ? BM_USET( bm, x, y ) : 0)
 
#define BM_CLR(bm, x, y)   (bm_safe( bm, x, y ) ? BM_UCLR( bm, x, y ) : 0)
 
#define BM_INV(bm, x, y)   (bm_safe( bm, x, y ) ? BM_UINV( bm, x, y ) : 0)
 
#define BM_PUT(bm, x, y, b)   (bm_safe( bm, x, y ) ? BM_UPUT( bm, x, y, b ) : 0)
 

Functions

static void bm_free (potrace_bitmap_t *bm)
 
static potrace_bitmap_tbm_new (int w, int h)
 
static void bm_clear (potrace_bitmap_t *bm, int c)
 
static potrace_bitmap_tbm_dup (const potrace_bitmap_t *bm)
 
static void bm_invert (potrace_bitmap_t *bm)
 

Macro Definition Documentation

#define BM_ALLBITS   (~(potrace_word) 0)

Definition at line 27 of file bitmap.h.

Referenced by bm_clearexcess(), bm_invert(), and xor_to_ref().

#define BM_CLR (   bm,
  x,
 
)    (bm_safe( bm, x, y ) ? BM_UCLR( bm, x, y ) : 0)

Definition at line 44 of file bitmap.h.

#define BM_GET (   bm,
  x,
 
)    (bm_safe( bm, x, y ) ? BM_UGET( bm, x, y ) : 0)

Definition at line 42 of file bitmap.h.

Referenced by bm_print(), bm_to_pathlist(), findnext(), findpath(), majority(), and pathlist_to_tree().

#define BM_HIBIT   ( ( (potrace_word) 1 ) << (BM_WORDBITS - 1) )

Definition at line 26 of file bitmap.h.

#define bm_index (   bm,
  x,
 
)    (&bm_scanline( bm, y )[(x) / BM_WORDBITS])
#define BM_INV (   bm,
  x,
 
)    (bm_safe( bm, x, y ) ? BM_UINV( bm, x, y ) : 0)

Definition at line 45 of file bitmap.h.

#define bm_mask (   x)    ( BM_HIBIT >> ( (x) & (BM_WORDBITS - 1) ) )

Definition at line 34 of file bitmap.h.

#define BM_PUT (   bm,
  x,
  y,
 
)    (bm_safe( bm, x, y ) ? BM_UPUT( bm, x, y, b ) : 0)

Definition at line 46 of file bitmap.h.

Referenced by bm_readbody_bmp().

#define bm_range (   x,
 
)    ( (int) (x) >= 0 && (int) (x) < (a) )

Definition at line 35 of file bitmap.h.

#define bm_safe (   bm,
  x,
 
)    ( bm_range( x, (bm)->w ) && bm_range( y, (bm)->h ) )

Definition at line 36 of file bitmap.h.

#define bm_scanline (   bm,
 
)    ( (bm)->map + (ptrdiff_t) (y) * (ptrdiff_t) (bm)->dy )

Definition at line 32 of file bitmap.h.

Referenced by clear_bm_with_bbox().

#define BM_SET (   bm,
  x,
 
)    (bm_safe( bm, x, y ) ? BM_USET( bm, x, y ) : 0)

Definition at line 43 of file bitmap.h.

#define BM_UCLR (   bm,
  x,
 
)    ( *bm_index( bm, x, y ) &= ~bm_mask( x ) )

Definition at line 39 of file bitmap.h.

#define BM_UGET (   bm,
  x,
 
)    ( ( *bm_index( bm, x, y ) & bm_mask( x ) ) != 0 )

Definition at line 37 of file bitmap.h.

#define BM_UINV (   bm,
  x,
 
)    ( *bm_index( bm, x, y ) ^= bm_mask( x ) )

Definition at line 40 of file bitmap.h.

#define BM_UPUT (   bm,
  x,
  y,
 
)    ( (b) ? BM_USET( bm, x, y ) : BM_UCLR( bm, x, y ) )

Definition at line 41 of file bitmap.h.

Referenced by bm_readbody_bmp(), and bm_readbody_pnm().

#define BM_USET (   bm,
  x,
 
)    ( *bm_index( bm, x, y ) |= bm_mask( x ) )

Definition at line 38 of file bitmap.h.

#define BM_WORDBITS   (8 * BM_WORDSIZE)

Definition at line 25 of file bitmap.h.

Referenced by bm_clearexcess(), bm_new(), clear_bm_with_bbox(), findnext(), xor_path(), and xor_to_ref().

#define BM_WORDSIZE   ( (int) sizeof(potrace_word) )

Definition at line 24 of file bitmap.h.

Referenced by bm_clear(), bm_dup(), bm_new(), bm_readbody_bmp(), bm_readbody_pnm(), and bm_writepbm().

Function Documentation

static void bm_clear ( potrace_bitmap_t bm,
int  c 
)
inlinestatic

Definition at line 98 of file bitmap.h.

References BM_WORDSIZE, potrace_bitmap_s::dy, potrace_bitmap_s::h, and potrace_bitmap_s::map.

Referenced by bm_readbody_bmp(), bm_readbody_pnm(), and pathlist_to_tree().

99 {
100  /* Note: if the bitmap was created with bm_new, then it is
101  * guaranteed that size will fit into the ptrdiff_t type. */
102  ptrdiff_t size = (ptrdiff_t) bm->dy * (ptrdiff_t) bm->h * (ptrdiff_t) BM_WORDSIZE;
103 
104  memset( bm->map, c ? -1 : 0, size );
105 }
#define BM_WORDSIZE
Definition: bitmap.h:24
potrace_word * map
Definition: potracelib.h:63
static potrace_bitmap_t* bm_dup ( const potrace_bitmap_t bm)
inlinestatic

Definition at line 109 of file bitmap.h.

References bm_new(), BM_WORDSIZE, potrace_bitmap_s::dy, potrace_bitmap_s::h, potrace_bitmap_s::map, and potrace_bitmap_s::w.

Referenced by bm_to_pathlist().

110 {
111  potrace_bitmap_t* bm1 = bm_new( bm->w, bm->h );
112  ptrdiff_t size = (ptrdiff_t) bm->dy * (ptrdiff_t) bm->h * (ptrdiff_t) BM_WORDSIZE;
113 
114  if( !bm1 )
115  {
116  return NULL;
117  }
118 
119  memcpy( bm1->map, bm->map, size );
120  return bm1;
121 }
#define BM_WORDSIZE
Definition: bitmap.h:24
potrace_word * map
Definition: potracelib.h:63
static potrace_bitmap_t * bm_new(int w, int h)
Definition: bitmap.h:62
static void bm_free ( potrace_bitmap_t bm)
inlinestatic

Definition at line 49 of file bitmap.h.

References potrace_bitmap_s::map.

Referenced by bm_readbody_bmp(), bm_readbody_pnm(), and bm_to_pathlist().

50 {
51  if( bm )
52  {
53  free( bm->map );
54  }
55 
56  free( bm );
57 }
potrace_word * map
Definition: potracelib.h:63
static void bm_invert ( potrace_bitmap_t bm)
inlinestatic

Definition at line 125 of file bitmap.h.

References BM_ALLBITS, potrace_bitmap_s::dy, potrace_bitmap_s::h, and potrace_bitmap_s::map.

126 {
127  ptrdiff_t i;
128  ptrdiff_t size = (ptrdiff_t) bm->dy * (ptrdiff_t) bm->h;
129 
130  for( i = 0; i < size; i++ )
131  {
132  bm->map[i] ^= BM_ALLBITS;
133  }
134 }
potrace_word * map
Definition: potracelib.h:63
#define BM_ALLBITS
Definition: bitmap.h:27
static potrace_bitmap_t* bm_new ( int  w,
int  h 
)
inlinestatic

Definition at line 62 of file bitmap.h.

References BM_WORDBITS, BM_WORDSIZE, potrace_bitmap_s::dy, potrace_bitmap_s::h, potrace_bitmap_s::map, and potrace_bitmap_s::w.

Referenced by bm_dup(), bm_readbody_bmp(), and bm_readbody_pnm().

63 {
64  potrace_bitmap_t* bm;
65  int dy = w == 0 ? 0 : (w - 1) / BM_WORDBITS + 1;
66  ptrdiff_t size = (ptrdiff_t) dy * (ptrdiff_t) h * (ptrdiff_t) BM_WORDSIZE;
67 
68  /* check for overflow error */
69  if( size < 0 || (h != 0 && dy != 0 && size / h / dy != BM_WORDSIZE) )
70  {
71  errno = ENOMEM;
72  return NULL;
73  }
74 
75  bm = (potrace_bitmap_t*) malloc( sizeof(potrace_bitmap_t) );
76 
77  if( !bm )
78  {
79  return NULL;
80  }
81 
82  bm->w = w;
83  bm->h = h;
84  bm->dy = dy;
85  bm->map = (potrace_word*) malloc( size );
86 
87  if( !bm->map )
88  {
89  free( bm );
90  return NULL;
91  }
92 
93  return bm;
94 }
#define BM_WORDSIZE
Definition: bitmap.h:24
potrace_word * map
Definition: potracelib.h:63
unsigned long potrace_word
Definition: potracelib.h:53
#define BM_WORDBITS
Definition: bitmap.h:25