KiCad PCB EDA Suite
bitmap.h File Reference
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.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 ptrdiff_t getsize (int dy, int h)
 
static ptrdiff_t bm_size (const potrace_bitmap_t *bm)
 
static potrace_wordbm_base (const potrace_bitmap_t *bm)
 
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)
 
static void bm_flip (potrace_bitmap_t *bm)
 
static int bm_resize (potrace_bitmap_t *bm, int h)
 

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 bm_base(), bm_dup(), bm_flip(), bm_invert(), and 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_dup(), bm_readbody_bmp(), bm_readbody_pnm(), bm_writepbm(), and getsize().

Function Documentation

static potrace_word* bm_base ( const potrace_bitmap_t bm)
inlinestatic

Definition at line 86 of file bitmap.h.

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

Referenced by bm_clear(), and bm_free().

87 {
88  int dy = bm->dy;
89 
90  if( dy >= 0 || bm->h == 0 )
91  {
92  return bm->map;
93  }
94  else
95  {
96  return bm_scanline( bm, bm->h - 1 );
97  }
98 }
potrace_word * map
Definition: potracelib.h:63
#define bm_scanline(bm, y)
Definition: bitmap.h:32
static void bm_clear ( potrace_bitmap_t bm,
int  c 
)
inlinestatic

Definition at line 158 of file bitmap.h.

References bm_base(), and bm_size().

Referenced by pathlist_to_tree().

159 {
160  /* Note: if the bitmap was created with bm_new, then it is
161  * guaranteed that size will fit into the ptrdiff_t type. */
162  ptrdiff_t size = bm_size( bm );
163 
164  memset( bm_base( bm ), c ? -1 : 0, size );
165 }
static potrace_word * bm_base(const potrace_bitmap_t *bm)
Definition: bitmap.h:86
static ptrdiff_t bm_size(const potrace_bitmap_t *bm)
Definition: bitmap.h:76
static potrace_bitmap_t* bm_dup ( const potrace_bitmap_t bm)
inlinestatic

Definition at line 170 of file bitmap.h.

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

Referenced by bm_to_pathlist().

171 {
172  potrace_bitmap_t* bm1 = bm_new( bm->w, bm->h );
173  int y;
174 
175  if( !bm1 )
176  {
177  return NULL;
178  }
179 
180  for( y = 0; y < bm->h; y++ )
181  {
182  memcpy( bm_scanline( bm1, y ), bm_scanline( bm, y ),
183  (size_t) bm1->dy * (size_t) BM_WORDSIZE );
184  }
185 
186  return bm1;
187 }
#define BM_WORDSIZE
Definition: bitmap.h:24
#define bm_scanline(bm, y)
Definition: bitmap.h:32
static potrace_bitmap_t * bm_new(int w, int h)
Definition: bitmap.h:115
static void bm_flip ( potrace_bitmap_t bm)
inlinestatic

Definition at line 217 of file bitmap.h.

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

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

218 {
219  int dy = bm->dy;
220 
221  if( bm->h == 0 || bm->h == 1 )
222  {
223  return;
224  }
225 
226  bm->map = bm_scanline( bm, bm->h - 1 );
227  bm->dy = -dy;
228 }
potrace_word * map
Definition: potracelib.h:63
#define bm_scanline(bm, y)
Definition: bitmap.h:32
static void bm_free ( potrace_bitmap_t bm)
inlinestatic

Definition at line 102 of file bitmap.h.

References bm_base(), and potrace_bitmap_s::map.

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

103 {
104  if( bm && bm->map )
105  {
106  free( bm_base( bm ) );
107  }
108 
109  free( bm );
110 }
potrace_word * map
Definition: potracelib.h:63
static potrace_word * bm_base(const potrace_bitmap_t *bm)
Definition: bitmap.h:86
static void bm_invert ( potrace_bitmap_t bm)
inlinestatic

Definition at line 191 of file bitmap.h.

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

192 {
193  int dy = bm->dy;
194  int y;
195  int i;
196  potrace_word* p;
197 
198  if( dy < 0 )
199  {
200  dy = -dy;
201  }
202 
203  for( y = 0; y < bm->h; y++ )
204  {
205  p = bm_scanline( bm, y );
206 
207  for( i = 0; i < dy; i++ )
208  {
209  p[i] ^= BM_ALLBITS;
210  }
211  }
212 }
unsigned long potrace_word
Definition: potracelib.h:53
#define bm_scanline(bm, y)
Definition: bitmap.h:32
#define BM_ALLBITS
Definition: bitmap.h:27
static potrace_bitmap_t* bm_new ( int  w,
int  h 
)
inlinestatic

Definition at line 115 of file bitmap.h.

References BM_WORDBITS, potrace_bitmap_s::dy, getsize(), potrace_bitmap_s::h, potrace_bitmap_s::map, and potrace_bitmap_s::w.

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

116 {
117  potrace_bitmap_t* bm;
118  int dy = w == 0 ? 0 : ( w - 1 ) / BM_WORDBITS + 1;
119  ptrdiff_t size;
120 
121  size = getsize( dy, h );
122 
123  if( size < 0 )
124  {
125  errno = ENOMEM;
126  return NULL;
127  }
128 
129  if( size == 0 )
130  {
131  size = 1; /* make sure calloc() doesn't return NULL */
132  }
133 
134  bm = (potrace_bitmap_t*) malloc( sizeof( potrace_bitmap_t ) );
135 
136  if( !bm )
137  {
138  return NULL;
139  }
140 
141  bm->w = w;
142  bm->h = h;
143  bm->dy = dy;
144  bm->map = (potrace_word*) calloc( 1, size );
145 
146  if( !bm->map )
147  {
148  free( bm );
149  return NULL;
150  }
151 
152  return bm;
153 }
potrace_word * map
Definition: potracelib.h:63
unsigned long potrace_word
Definition: potracelib.h:53
#define BM_WORDBITS
Definition: bitmap.h:25
static ptrdiff_t getsize(int dy, int h)
Definition: bitmap.h:51
static int bm_resize ( potrace_bitmap_t bm,
int  h 
)
inlinestatic

Definition at line 237 of file bitmap.h.

References bm_flip(), potrace_bitmap_s::dy, getsize(), potrace_bitmap_s::h, and potrace_bitmap_s::map.

Referenced by bm_readbody_bmp(), and bm_readbody_pnm().

238 {
239  int dy = bm->dy;
240  ptrdiff_t newsize;
241  potrace_word* newmap;
242 
243  if( dy < 0 )
244  {
245  bm_flip( bm );
246  }
247 
248  newsize = getsize( dy, h );
249 
250  if( newsize < 0 )
251  {
252  errno = ENOMEM;
253  goto error;
254  }
255 
256  if( newsize == 0 )
257  {
258  newsize = 1; /* make sure realloc() doesn't return NULL */
259  }
260 
261  newmap = (potrace_word*) realloc( bm->map, newsize );
262 
263  if( newmap == NULL )
264  {
265  goto error;
266  }
267 
268  bm->map = newmap;
269  bm->h = h;
270 
271  if( dy < 0 )
272  {
273  bm_flip( bm );
274  }
275 
276  return 0;
277 
278 error:
279 
280  if( dy < 0 )
281  {
282  bm_flip( bm );
283  }
284 
285  return 1;
286 }
static void bm_flip(potrace_bitmap_t *bm)
Definition: bitmap.h:217
potrace_word * map
Definition: potracelib.h:63
unsigned long potrace_word
Definition: potracelib.h:53
static ptrdiff_t getsize(int dy, int h)
Definition: bitmap.h:51
static ptrdiff_t bm_size ( const potrace_bitmap_t bm)
inlinestatic

Definition at line 76 of file bitmap.h.

References potrace_bitmap_s::dy, getsize(), and potrace_bitmap_s::h.

Referenced by bm_clear().

77 {
78  return getsize( bm->dy, bm->h );
79 }
static ptrdiff_t getsize(int dy, int h)
Definition: bitmap.h:51
static ptrdiff_t getsize ( int  dy,
int  h 
)
inlinestatic

Definition at line 51 of file bitmap.h.

References BM_WORDSIZE.

Referenced by bm_new(), bm_resize(), and bm_size().

52 {
53  ptrdiff_t size;
54 
55  if( dy < 0 )
56  {
57  dy = -dy;
58  }
59 
60  size = (ptrdiff_t) dy * (ptrdiff_t) h * (ptrdiff_t) BM_WORDSIZE;
61 
62  /* check for overflow error */
63  if( size < 0 || ( h != 0 && dy != 0 && size / h / dy != BM_WORDSIZE ) )
64  {
65  return -1;
66  }
67 
68  return size;
69 }
#define BM_WORDSIZE
Definition: bitmap.h:24