Statistics
| Revision:

gvsig-raster / libjni-potrace / trunk / libjni-potrace / resources / potrace-1.8 / src / bitmap.h @ 1780

History | View | Annotate | Download (3.17 KB)

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. */
4

    
5
#ifndef BITMAP_H
6
#define BITMAP_H
7

    
8
#ifdef HAVE_CONFIG_H
9
#include "config.h"
10
#endif
11

    
12
#include <string.h>
13
#include <stdlib.h>
14

    
15
/* The bitmap type is defined in potracelib.h */
16
#include "potracelib.h"
17

    
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 */
22

    
23
/* ---------------------------------------------------------------------- */
24
/* some measurements */
25

    
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)
30

    
31
/* macros for accessing pixel at index (x,y). U* macros omit the
32
   bounds check. */
33

    
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)
49

    
50
/* free the given bitmap. Leaves errno untouched. */
51
static inline void bm_free(potrace_bitmap_t *bm) {
52
  if (bm) {
53
    free(bm->map);
54
  }
55
  free(bm);
56
}
57

    
58
/* return new un-initialized bitmap. NULL with errno on error */
59
static inline potrace_bitmap_t *bm_new(int w, int h) {
60
  potrace_bitmap_t *bm;
61
  int dy = (w + BM_WORDBITS - 1) / BM_WORDBITS;
62

    
63
  bm = (potrace_bitmap_t *) malloc(sizeof(potrace_bitmap_t));
64
  if (!bm) {
65
    return NULL;
66
  }
67
  bm->w = w;
68
  bm->h = h;
69
  bm->dy = dy;
70
  bm->map = (potrace_word *) malloc(dy * h * BM_WORDSIZE);
71
  if (!bm->map) {
72
    free(bm);
73
    return NULL;
74
  }
75
  return bm;
76
}
77

    
78
/* clear the given bitmap. Set all bits to c. */
79
static inline void bm_clear(potrace_bitmap_t *bm, int c) {
80
  memset(bm->map, c ? -1 : 0, bm->dy * bm->h * BM_WORDSIZE);
81
}
82

    
83
/* duplicate the given bitmap. Return NULL on error with errno set. */
84
static inline potrace_bitmap_t *bm_dup(const potrace_bitmap_t *bm) {
85
  potrace_bitmap_t *bm1 = bm_new(bm->w, bm->h);
86
  if (!bm1) {
87
    return NULL;
88
  }
89
  memcpy(bm1->map, bm->map, bm->dy * bm->h * BM_WORDSIZE);
90
  return bm1;
91
}
92

    
93
/* invert the given bitmap. */
94
static inline void bm_invert(potrace_bitmap_t *bm) {
95
  int i;
96
  for (i = 0; i < bm->dy * bm->h; i++) {
97
    bm->map[i] ^= BM_ALLBITS;
98
  }
99
}
100

    
101
#endif /* BITMAP_H */