pcb 4.1.1
An interactive printed circuit board layout editor.

bitmap.h

Go to the documentation of this file.
00001 
00019 #ifndef BITMAP_H
00020 #define BITMAP_H
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #include "config.h"
00024 #endif
00025 
00026 #include <string.h>
00027 #include <stdlib.h>
00028 
00032 #include "potracelib.h"
00033 
00034 /* The present file defines some convenient macros and static inline
00035    functions for accessing bitmaps. Since they only produce inline
00036    code, they can be conveniently shared by the library and frontends,
00037    if desired */
00038 
00039 /* ---------------------------------------------------------------------- */
00040 /* some measurements */
00041 
00042 #define BM_WORDSIZE ((int)sizeof(potrace_word))
00043 #define BM_WORDBITS (8*BM_WORDSIZE)
00044 #define BM_HIBIT (((potrace_word)1)<<(BM_WORDBITS-1))
00045 #define BM_ALLBITS (~(potrace_word)0)
00046 
00047 /* macros for accessing pixel at index (x,y). U* macros omit the
00048    bounds check. */
00049 
00050 #define bm_scanline(bm, y) ((bm)->map + (y)*(bm)->dy)
00051 #define bm_index(bm, x, y) (&bm_scanline(bm, y)[(x)/BM_WORDBITS])
00052 #define bm_mask(x) (BM_HIBIT >> ((x) & (BM_WORDBITS-1)))
00053 #define bm_range(x, a) ((int)(x) >= 0 && (int)(x) < (a))
00054 #define bm_safe(bm, x, y) (bm_range(x, (bm)->w) && bm_range(y, (bm)->h))
00055 #define BM_UGET(bm, x, y) ((*bm_index(bm, x, y) & bm_mask(x)) != 0)
00056 #define BM_USET(bm, x, y) (*bm_index(bm, x, y) |= bm_mask(x))
00057 #define BM_UCLR(bm, x, y) (*bm_index(bm, x, y) &= ~bm_mask(x))
00058 #define BM_UINV(bm, x, y) (*bm_index(bm, x, y) ^= bm_mask(x))
00059 #define BM_UPUT(bm, x, y, b) ((b) ? BM_USET(bm, x, y) : BM_UCLR(bm, x, y))
00060 #define BM_GET(bm, x, y) (bm_safe(bm, x, y) ? BM_UGET(bm, x, y) : 0)
00061 #define BM_SET(bm, x, y) (bm_safe(bm, x, y) ? BM_USET(bm, x, y) : 0)
00062 #define BM_CLR(bm, x, y) (bm_safe(bm, x, y) ? BM_UCLR(bm, x, y) : 0)
00063 #define BM_INV(bm, x, y) (bm_safe(bm, x, y) ? BM_UINV(bm, x, y) : 0)
00064 #define BM_PUT(bm, x, y, b) (bm_safe(bm, x, y) ? BM_UPUT(bm, x, y, b) : 0)
00065 
00069 static inline void
00070 bm_free (potrace_bitmap_t * bm)
00071 {
00072   if (bm)
00073     {
00074       free (bm->map);
00075     }
00076   free (bm);
00077 }
00078 
00084 static inline potrace_bitmap_t *
00085 bm_new (int w, int h)
00086 {
00087   potrace_bitmap_t *bm;
00088   int dy = (w + BM_WORDBITS - 1) / BM_WORDBITS;
00089 
00090   bm = (potrace_bitmap_t *) malloc (sizeof (potrace_bitmap_t));
00091   if (!bm)
00092     {
00093       return NULL;
00094     }
00095   bm->w = w;
00096   bm->h = h;
00097   bm->dy = dy;
00098   bm->map = (potrace_word *) malloc (dy * h * BM_WORDSIZE);
00099   if (!bm->map)
00100     {
00101       free (bm);
00102       return NULL;
00103     }
00104   return bm;
00105 }
00106 
00112 static inline void
00113 bm_clear (potrace_bitmap_t * bm, int c)
00114 {
00115   memset (bm->map, c ? -1 : 0, bm->dy * bm->h * BM_WORDSIZE);
00116 }
00117 
00123 static inline potrace_bitmap_t *
00124 bm_dup (const potrace_bitmap_t * bm)
00125 {
00126   potrace_bitmap_t *bm1 = bm_new (bm->w, bm->h);
00127   if (!bm1)
00128     {
00129       return NULL;
00130     }
00131   memcpy (bm1->map, bm->map, bm->dy * bm->h * BM_WORDSIZE);
00132   return bm1;
00133 }
00134 
00138 static inline void
00139 bm_invert (potrace_bitmap_t * bm)
00140 {
00141   int i;
00142   for (i = 0; i < bm->dy * bm->h; i++)
00143     {
00144       bm->map[i] ^= BM_ALLBITS;
00145     }
00146 }
00147 
00148 #endif /* BITMAP_H */