00001
00002
00003
00004
00005 #ifndef BITMAP_H
00006 #define BITMAP_H
00007
00008 #ifdef HAVE_CONFIG_H
00009 #include "config.h"
00010 #endif
00011
00012 #include <string.h>
00013 #include <stdlib.h>
00014
00015
00016 #include "potracelib.h"
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #define BM_WORDSIZE ((int)sizeof(potrace_word))
00027 #define BM_WORDBITS (8*BM_WORDSIZE)
00028 #define BM_HIBIT (((potrace_word)1)<<(BM_WORDBITS-1))
00029 #define BM_ALLBITS (~(potrace_word)0)
00030
00031
00032
00033
00034 #define bm_scanline(bm, y) ((bm)->map + (y)*(bm)->dy)
00035 #define bm_index(bm, x, y) (&bm_scanline(bm, y)[(x)/BM_WORDBITS])
00036 #define bm_mask(x) (BM_HIBIT >> ((x) & (BM_WORDBITS-1)))
00037 #define bm_range(x, a) ((int)(x) >= 0 && (int)(x) < (a))
00038 #define bm_safe(bm, x, y) (bm_range(x, (bm)->w) && bm_range(y, (bm)->h))
00039 #define BM_UGET(bm, x, y) ((*bm_index(bm, x, y) & bm_mask(x)) != 0)
00040 #define BM_USET(bm, x, y) (*bm_index(bm, x, y) |= bm_mask(x))
00041 #define BM_UCLR(bm, x, y) (*bm_index(bm, x, y) &= ~bm_mask(x))
00042 #define BM_UINV(bm, x, y) (*bm_index(bm, x, y) ^= bm_mask(x))
00043 #define BM_UPUT(bm, x, y, b) ((b) ? BM_USET(bm, x, y) : BM_UCLR(bm, x, y))
00044 #define BM_GET(bm, x, y) (bm_safe(bm, x, y) ? BM_UGET(bm, x, y) : 0)
00045 #define BM_SET(bm, x, y) (bm_safe(bm, x, y) ? BM_USET(bm, x, y) : 0)
00046 #define BM_CLR(bm, x, y) (bm_safe(bm, x, y) ? BM_UCLR(bm, x, y) : 0)
00047 #define BM_INV(bm, x, y) (bm_safe(bm, x, y) ? BM_UINV(bm, x, y) : 0)
00048 #define BM_PUT(bm, x, y, b) (bm_safe(bm, x, y) ? BM_UPUT(bm, x, y, b) : 0)
00049
00050
00051 static inline void bm_free(potrace_bitmap_t *bm) {
00052 if (bm) {
00053 free(bm->map);
00054 }
00055 free(bm);
00056 }
00057
00058
00059 static inline potrace_bitmap_t *bm_new(int w, int h) {
00060 potrace_bitmap_t *bm;
00061 int dy = (w + BM_WORDBITS - 1) / BM_WORDBITS;
00062
00063 bm = (potrace_bitmap_t *) malloc(sizeof(potrace_bitmap_t));
00064 if (!bm) {
00065 return NULL;
00066 }
00067 bm->w = w;
00068 bm->h = h;
00069 bm->dy = dy;
00070 bm->map = (potrace_word *) malloc(dy * h * BM_WORDSIZE);
00071 if (!bm->map) {
00072 free(bm);
00073 return NULL;
00074 }
00075 return bm;
00076 }
00077
00078
00079 static inline void bm_clear(potrace_bitmap_t *bm, int c) {
00080 memset(bm->map, c ? -1 : 0, bm->dy * bm->h * BM_WORDSIZE);
00081 }
00082
00083
00084 static inline potrace_bitmap_t *bm_dup(const potrace_bitmap_t *bm) {
00085 potrace_bitmap_t *bm1 = bm_new(bm->w, bm->h);
00086 if (!bm1) {
00087 return NULL;
00088 }
00089 memcpy(bm1->map, bm->map, bm->dy * bm->h * BM_WORDSIZE);
00090 return bm1;
00091 }
00092
00093
00094 static inline void bm_invert(potrace_bitmap_t *bm) {
00095 int i;
00096 for (i = 0; i < bm->dy * bm->h; i++) {
00097 bm->map[i] ^= BM_ALLBITS;
00098 }
00099 }
00100
00101 #endif