hid/gtk (GL): I think the polygon renderer works in mask mode now
[geda-pcb/pcjc2.git] / src / hid / gcode / bitmap.h
blob589b80b4d74c312989ed4e18165aefeae582b801
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. */
5 #ifndef BITMAP_H
6 #define BITMAP_H
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
12 #include <string.h>
13 #include <stdlib.h>
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,
21 if desired */
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
32 bounds check. */
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. */
51 static inline void
52 bm_free (potrace_bitmap_t * bm)
54 if (bm)
56 free (bm->map);
58 free (bm);
61 /* return new un-initialized bitmap. NULL with errno on error */
62 static inline potrace_bitmap_t *
63 bm_new (int w, int h)
65 potrace_bitmap_t *bm;
66 int dy = (w + BM_WORDBITS - 1) / BM_WORDBITS;
68 bm = (potrace_bitmap_t *) malloc (sizeof (potrace_bitmap_t));
69 if (!bm)
71 return NULL;
73 bm->w = w;
74 bm->h = h;
75 bm->dy = dy;
76 bm->map = (potrace_word *) malloc (dy * h * BM_WORDSIZE);
77 if (!bm->map)
79 free (bm);
80 return NULL;
82 return bm;
85 /* clear the given bitmap. Set all bits to c. */
86 static inline void
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);
97 if (!bm1)
99 return NULL;
101 memcpy (bm1->map, bm->map, bm->dy * bm->h * BM_WORDSIZE);
102 return bm1;
105 /* invert the given bitmap. */
106 static inline void
107 bm_invert (potrace_bitmap_t * bm)
109 int i;
110 for (i = 0; i < bm->dy * bm->h; i++)
112 bm->map[i] ^= BM_ALLBITS;
116 #endif /* BITMAP_H */