1 /* Copyright (C) 2001-2007 Peter Selinger.
2 This file is part of Potrace. It is free software and it is covered
3 by the GNU General Public License. See the file COPYING for details. */
15 /* The bitmap type is defined in potracelib.h */
16 #include "potracelib.h"
18 /* The present file defines some convenient macros and static inline
19 functions for accessing bitmaps. Since they only produce inline
20 code, they can be conveniently shared by the library and frontends,
23 /* ---------------------------------------------------------------------- */
24 /* some measurements */
26 #define BM_WORDSIZE ((int)sizeof(potrace_word))
27 #define BM_WORDBITS (8*BM_WORDSIZE)
28 #define BM_HIBIT (((potrace_word)1)<<(BM_WORDBITS-1))
29 #define BM_ALLBITS (~(potrace_word)0)
31 /* macros for accessing pixel at index (x,y). U* macros omit the
34 #define bm_scanline(bm, y) ((bm)->map + (y)*(bm)->dy)
35 #define bm_index(bm, x, y) (&bm_scanline(bm, y)[(x)/BM_WORDBITS])
36 #define bm_mask(x) (BM_HIBIT >> ((x) & (BM_WORDBITS-1)))
37 #define bm_range(x, a) ((int)(x) >= 0 && (int)(x) < (a))
38 #define bm_safe(bm, x, y) (bm_range(x, (bm)->w) && bm_range(y, (bm)->h))
39 #define BM_UGET(bm, x, y) ((*bm_index(bm, x, y) & bm_mask(x)) != 0)
40 #define BM_USET(bm, x, y) (*bm_index(bm, x, y) |= bm_mask(x))
41 #define BM_UCLR(bm, x, y) (*bm_index(bm, x, y) &= ~bm_mask(x))
42 #define BM_UINV(bm, x, y) (*bm_index(bm, x, y) ^= bm_mask(x))
43 #define BM_UPUT(bm, x, y, b) ((b) ? BM_USET(bm, x, y) : BM_UCLR(bm, x, y))
44 #define BM_GET(bm, x, y) (bm_safe(bm, x, y) ? BM_UGET(bm, x, y) : 0)
45 #define BM_SET(bm, x, y) (bm_safe(bm, x, y) ? BM_USET(bm, x, y) : 0)
46 #define BM_CLR(bm, x, y) (bm_safe(bm, x, y) ? BM_UCLR(bm, x, y) : 0)
47 #define BM_INV(bm, x, y) (bm_safe(bm, x, y) ? BM_UINV(bm, x, y) : 0)
48 #define BM_PUT(bm, x, y, b) (bm_safe(bm, x, y) ? BM_UPUT(bm, x, y, b) : 0)
50 /* free the given bitmap. Leaves errno untouched. */
52 bm_free (potrace_bitmap_t
* bm
)
61 /* return new un-initialized bitmap. NULL with errno on error */
62 static inline potrace_bitmap_t
*
66 int dy
= (w
+ BM_WORDBITS
- 1) / BM_WORDBITS
;
68 bm
= (potrace_bitmap_t
*) malloc (sizeof (potrace_bitmap_t
));
76 bm
->map
= (potrace_word
*) malloc (dy
* h
* BM_WORDSIZE
);
85 /* clear the given bitmap. Set all bits to c. */
87 bm_clear (potrace_bitmap_t
* bm
, int c
)
89 memset (bm
->map
, c
? -1 : 0, bm
->dy
* bm
->h
* BM_WORDSIZE
);
92 /* duplicate the given bitmap. Return NULL on error with errno set. */
93 static inline potrace_bitmap_t
*
94 bm_dup (const potrace_bitmap_t
* bm
)
96 potrace_bitmap_t
*bm1
= bm_new (bm
->w
, bm
->h
);
101 memcpy (bm1
->map
, bm
->map
, bm
->dy
* bm
->h
* BM_WORDSIZE
);
105 /* invert the given bitmap. */
107 bm_invert (potrace_bitmap_t
* bm
)
110 for (i
= 0; i
< bm
->dy
* bm
->h
; i
++)
112 bm
->map
[i
] ^= BM_ALLBITS
;
116 #endif /* BITMAP_H */