18#include "potracelib.h"
28#define BM_WORDSIZE ((int)sizeof(potrace_word))
29#define BM_WORDBITS (8*BM_WORDSIZE)
30#define BM_HIBIT (((potrace_word)1)<<(BM_WORDBITS-1))
31#define BM_ALLBITS (~(potrace_word)0)
36#define bm_scanline(bm, y) ((bm)->map + (ptrdiff_t)(y)*(ptrdiff_t)(bm)->dy)
37#define bm_index(bm, x, y) (&bm_scanline(bm, y)[(x)/BM_WORDBITS])
38#define bm_mask(x) (BM_HIBIT >> ((x) & (BM_WORDBITS-1)))
39#define bm_range(x, a) ((int)(x) >= 0 && (int)(x) < (a))
40#define bm_safe(bm, x, y) (bm_range(x, (bm)->w) && bm_range(y, (bm)->h))
41#define BM_UGET(bm, x, y) ((*bm_index(bm, x, y) & bm_mask(x)) != 0)
42#define BM_USET(bm, x, y) (*bm_index(bm, x, y) |= bm_mask(x))
43#define BM_UCLR(bm, x, y) (*bm_index(bm, x, y) &= ~bm_mask(x))
44#define BM_UINV(bm, x, y) (*bm_index(bm, x, y) ^= bm_mask(x))
45#define BM_UPUT(bm, x, y, b) ((b) ? BM_USET(bm, x, y) : BM_UCLR(bm, x, y))
46#define BM_GET(bm, x, y) (bm_safe(bm, x, y) ? BM_UGET(bm, x, y) : 0)
47#define BM_SET(bm, x, y) (bm_safe(bm, x, y) ? BM_USET(bm, x, y) : 0)
48#define BM_CLR(bm, x, y) (bm_safe(bm, x, y) ? BM_UCLR(bm, x, y) : 0)
49#define BM_INV(bm, x, y) (bm_safe(bm, x, y) ? BM_UINV(bm, x, y) : 0)
50#define BM_PUT(bm, x, y, b) (bm_safe(bm, x, y) ? BM_UPUT(bm, x, y, b) : 0)
53static inline void bm_free(potrace_bitmap_t *bm) {
62static inline potrace_bitmap_t *
bm_new(
int w,
int h) {
64 int dy =
w == 0 ? 0 : (
w - 1) / BM_WORDBITS + 1;
65 ptrdiff_t
size = (ptrdiff_t)dy * (ptrdiff_t)h * (ptrdiff_t)BM_WORDSIZE;
68 if (
size < 0 || (h != 0 && dy != 0 &&
size / h / dy != BM_WORDSIZE)) {
73 bm = (potrace_bitmap_t *) malloc(
sizeof(potrace_bitmap_t));
80 bm->map = (potrace_word *) malloc(
size);
82 g_warning(
"bm_new: can not allocate memory for bitmap (%td).",
size);
90static inline void bm_clear(potrace_bitmap_t *bm,
int c) {
93 ptrdiff_t
size = (ptrdiff_t)bm->dy * (ptrdiff_t)bm->h * (ptrdiff_t)BM_WORDSIZE;
94 memset(bm->map,
c ? -1 : 0,
size);
98static inline potrace_bitmap_t *
bm_dup(
const potrace_bitmap_t *bm) {
99 potrace_bitmap_t *bm1 =
bm_new(bm->w, bm->h);
100 ptrdiff_t
size = (ptrdiff_t)bm->dy * (ptrdiff_t)bm->h * (ptrdiff_t)BM_WORDSIZE;
104 memcpy(bm1->map, bm->map,
size);
111 ptrdiff_t
size = (ptrdiff_t)bm->dy * (ptrdiff_t)bm->h;
113 for (i = 0; i <
size; i++) {
114 bm->map[i] ^= BM_ALLBITS;
static void bm_invert(potrace_bitmap_t *bm)
static potrace_bitmap_t * bm_new(int w, int h)
static void bm_free(potrace_bitmap_t *bm)
static potrace_bitmap_t * bm_dup(const potrace_bitmap_t *bm)
static void bm_clear(potrace_bitmap_t *bm, int c)